gdb/
[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 36
42429eac
RS
37/* Check assumptions made in this file. */
38typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
39typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
40
252b5132
RH
41#ifdef DEBUG
42#define DBG(x) printf x
43#else
44#define DBG(x)
45#endif
46
252b5132 47/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
48static int mips_output_flavor (void);
49static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
50#undef OBJ_PROCESS_STAB
51#undef OUTPUT_FLAVOR
52#undef S_GET_ALIGN
53#undef S_GET_SIZE
54#undef S_SET_ALIGN
55#undef S_SET_SIZE
252b5132
RH
56#undef obj_frob_file
57#undef obj_frob_file_after_relocs
58#undef obj_frob_symbol
59#undef obj_pop_insert
60#undef obj_sec_sym_ok_for_reloc
61#undef OBJ_COPY_SYMBOL_ATTRIBUTES
62
63#include "obj-elf.h"
64/* Fix any of them that we actually care about. */
65#undef OUTPUT_FLAVOR
66#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 67
252b5132 68#include "elf/mips.h"
252b5132
RH
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
252b5132 88static char *mips_regmask_frag;
252b5132 89
85b51719 90#define ZERO 0
741fe287 91#define ATREG 1
df58fc94
RS
92#define S0 16
93#define S7 23
252b5132
RH
94#define TREG 24
95#define PIC_CALL_REG 25
96#define KT0 26
97#define KT1 27
98#define GP 28
99#define SP 29
100#define FP 30
101#define RA 31
102
103#define ILLEGAL_REG (32)
104
741fe287
MR
105#define AT mips_opts.at
106
252b5132
RH
107extern int target_big_endian;
108
252b5132 109/* The name of the readonly data section. */
e8044f35 110#define RDATA_SECTION_NAME ".rodata"
252b5132 111
a4e06468
RS
112/* Ways in which an instruction can be "appended" to the output. */
113enum append_method {
114 /* Just add it normally. */
115 APPEND_ADD,
116
117 /* Add it normally and then add a nop. */
118 APPEND_ADD_WITH_NOP,
119
120 /* Turn an instruction with a delay slot into a "compact" version. */
121 APPEND_ADD_COMPACT,
122
123 /* Insert the instruction before the last one. */
124 APPEND_SWAP
125};
126
47e39b9d
RS
127/* Information about an instruction, including its format, operands
128 and fixups. */
129struct mips_cl_insn
130{
131 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
132 const struct mips_opcode *insn_mo;
133
47e39b9d 134 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
135 a copy of INSN_MO->match with the operands filled in. If we have
136 decided to use an extended MIPS16 instruction, this includes the
137 extension. */
47e39b9d
RS
138 unsigned long insn_opcode;
139
140 /* The frag that contains the instruction. */
141 struct frag *frag;
142
143 /* The offset into FRAG of the first instruction byte. */
144 long where;
145
146 /* The relocs associated with the instruction, if any. */
147 fixS *fixp[3];
148
a38419a5
RS
149 /* True if this entry cannot be moved from its current position. */
150 unsigned int fixed_p : 1;
47e39b9d 151
708587a4 152 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
153 unsigned int noreorder_p : 1;
154
2fa15973
RS
155 /* True for mips16 instructions that jump to an absolute address. */
156 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
157
158 /* True if this instruction is complete. */
159 unsigned int complete_p : 1;
e407c74b
NC
160
161 /* True if this instruction is cleared from history by unconditional
162 branch. */
163 unsigned int cleared_p : 1;
47e39b9d
RS
164};
165
a325df1d
TS
166/* The ABI to use. */
167enum mips_abi_level
168{
169 NO_ABI = 0,
170 O32_ABI,
171 O64_ABI,
172 N32_ABI,
173 N64_ABI,
174 EABI_ABI
175};
176
177/* MIPS ABI we are using for this output file. */
316f5878 178static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 179
143d77c5
EC
180/* Whether or not we have code that can call pic code. */
181int mips_abicalls = FALSE;
182
aa6975fb
ILT
183/* Whether or not we have code which can be put into a shared
184 library. */
185static bfd_boolean mips_in_shared = TRUE;
186
252b5132
RH
187/* This is the set of options which may be modified by the .set
188 pseudo-op. We use a struct so that .set push and .set pop are more
189 reliable. */
190
e972090a
NC
191struct mips_set_options
192{
252b5132
RH
193 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
194 if it has not been initialized. Changed by `.set mipsN', and the
195 -mipsN command line option, and the default CPU. */
196 int isa;
846ef2d0
RS
197 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
198 <asename>', by command line options, and based on the default
199 architecture. */
200 int ase;
252b5132
RH
201 /* Whether we are assembling for the mips16 processor. 0 if we are
202 not, 1 if we are, and -1 if the value has not been initialized.
203 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
204 -nomips16 command line options, and the default CPU. */
205 int mips16;
df58fc94
RS
206 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
207 1 if we are, and -1 if the value has not been initialized. Changed
208 by `.set micromips' and `.set nomicromips', and the -mmicromips
209 and -mno-micromips command line options, and the default CPU. */
210 int micromips;
252b5132
RH
211 /* Non-zero if we should not reorder instructions. Changed by `.set
212 reorder' and `.set noreorder'. */
213 int noreorder;
741fe287
MR
214 /* Non-zero if we should not permit the register designated "assembler
215 temporary" to be used in instructions. The value is the register
216 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
217 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
218 unsigned int at;
252b5132
RH
219 /* Non-zero if we should warn when a macro instruction expands into
220 more than one machine instruction. Changed by `.set nomacro' and
221 `.set macro'. */
222 int warn_about_macros;
223 /* Non-zero if we should not move instructions. Changed by `.set
224 move', `.set volatile', `.set nomove', and `.set novolatile'. */
225 int nomove;
226 /* Non-zero if we should not optimize branches by moving the target
227 of the branch into the delay slot. Actually, we don't perform
228 this optimization anyhow. Changed by `.set bopt' and `.set
229 nobopt'. */
230 int nobopt;
231 /* Non-zero if we should not autoextend mips16 instructions.
232 Changed by `.set autoextend' and `.set noautoextend'. */
233 int noautoextend;
833794fc
MR
234 /* True if we should only emit 32-bit microMIPS instructions.
235 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
236 and -mno-insn32 command line options. */
237 bfd_boolean insn32;
a325df1d
TS
238 /* Restrict general purpose registers and floating point registers
239 to 32 bit. This is initially determined when -mgp32 or -mfp32
240 is passed but can changed if the assembler code uses .set mipsN. */
241 int gp32;
242 int fp32;
fef14a42
TS
243 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
244 command line option, and the default CPU. */
245 int arch;
aed1a261
RS
246 /* True if ".set sym32" is in effect. */
247 bfd_boolean sym32;
037b32b9
AN
248 /* True if floating-point operations are not allowed. Changed by .set
249 softfloat or .set hardfloat, by command line options -msoft-float or
250 -mhard-float. The default is false. */
251 bfd_boolean soft_float;
252
253 /* True if only single-precision floating-point operations are allowed.
254 Changed by .set singlefloat or .set doublefloat, command-line options
255 -msingle-float or -mdouble-float. The default is false. */
256 bfd_boolean single_float;
252b5132
RH
257};
258
037b32b9
AN
259/* This is the struct we use to hold the current set of options. Note
260 that we must set the isa field to ISA_UNKNOWN and the ASE fields to
261 -1 to indicate that they have not been initialized. */
262
a325df1d 263/* True if -mgp32 was passed. */
a8e8e863 264static int file_mips_gp32 = -1;
a325df1d
TS
265
266/* True if -mfp32 was passed. */
a8e8e863 267static int file_mips_fp32 = -1;
a325df1d 268
037b32b9
AN
269/* 1 if -msoft-float, 0 if -mhard-float. The default is 0. */
270static int file_mips_soft_float = 0;
271
272/* 1 if -msingle-float, 0 if -mdouble-float. The default is 0. */
273static int file_mips_single_float = 0;
252b5132 274
e972090a
NC
275static struct mips_set_options mips_opts =
276{
846ef2d0 277 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 278 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc
MR
279 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
280 /* gp32 */ 0, /* fp32 */ 0, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
b015e599 281 /* soft_float */ FALSE, /* single_float */ FALSE
e7af610e 282};
252b5132 283
846ef2d0
RS
284/* The set of ASEs that were selected on the command line, either
285 explicitly via ASE options or implicitly through things like -march. */
286static unsigned int file_ase;
287
288/* Which bits of file_ase were explicitly set or cleared by ASE options. */
289static unsigned int file_ase_explicit;
290
252b5132
RH
291/* These variables are filled in with the masks of registers used.
292 The object format code reads them and puts them in the appropriate
293 place. */
294unsigned long mips_gprmask;
295unsigned long mips_cprmask[4];
296
297/* MIPS ISA we are using for this output file. */
e7af610e 298static int file_mips_isa = ISA_UNKNOWN;
252b5132 299
738f4d98 300/* True if any MIPS16 code was produced. */
a4672219
TS
301static int file_ase_mips16;
302
3994f87e
TS
303#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
304 || mips_opts.isa == ISA_MIPS32R2 \
305 || mips_opts.isa == ISA_MIPS64 \
306 || mips_opts.isa == ISA_MIPS64R2)
307
df58fc94
RS
308/* True if any microMIPS code was produced. */
309static int file_ase_micromips;
310
b12dd2e4
CF
311/* True if we want to create R_MIPS_JALR for jalr $25. */
312#ifdef TE_IRIX
1180b5a4 313#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 314#else
1180b5a4
RS
315/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
316 because there's no place for any addend, the only acceptable
317 expression is a bare symbol. */
318#define MIPS_JALR_HINT_P(EXPR) \
319 (!HAVE_IN_PLACE_ADDENDS \
320 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
321#endif
322
ec68c924 323/* The argument of the -march= flag. The architecture we are assembling. */
fef14a42 324static int file_mips_arch = CPU_UNKNOWN;
316f5878 325static const char *mips_arch_string;
ec68c924
EC
326
327/* The argument of the -mtune= flag. The architecture for which we
328 are optimizing. */
329static int mips_tune = CPU_UNKNOWN;
316f5878 330static const char *mips_tune_string;
ec68c924 331
316f5878 332/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
333static int mips_32bitmode = 0;
334
316f5878
RS
335/* True if the given ABI requires 32-bit registers. */
336#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
337
338/* Likewise 64-bit registers. */
707bfff6
TS
339#define ABI_NEEDS_64BIT_REGS(ABI) \
340 ((ABI) == N32_ABI \
341 || (ABI) == N64_ABI \
316f5878
RS
342 || (ABI) == O64_ABI)
343
ad3fea08 344/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
345#define ISA_HAS_64BIT_REGS(ISA) \
346 ((ISA) == ISA_MIPS3 \
347 || (ISA) == ISA_MIPS4 \
348 || (ISA) == ISA_MIPS5 \
349 || (ISA) == ISA_MIPS64 \
350 || (ISA) == ISA_MIPS64R2)
9ce8a5dd 351
ad3fea08
TS
352/* Return true if ISA supports 64 bit wide float registers. */
353#define ISA_HAS_64BIT_FPRS(ISA) \
354 ((ISA) == ISA_MIPS3 \
355 || (ISA) == ISA_MIPS4 \
356 || (ISA) == ISA_MIPS5 \
357 || (ISA) == ISA_MIPS32R2 \
358 || (ISA) == ISA_MIPS64 \
359 || (ISA) == ISA_MIPS64R2)
360
af7ee8bf
CD
361/* Return true if ISA supports 64-bit right rotate (dror et al.)
362 instructions. */
707bfff6 363#define ISA_HAS_DROR(ISA) \
df58fc94
RS
364 ((ISA) == ISA_MIPS64R2 \
365 || (mips_opts.micromips \
366 && ISA_HAS_64BIT_REGS (ISA)) \
367 )
af7ee8bf
CD
368
369/* Return true if ISA supports 32-bit right rotate (ror et al.)
370 instructions. */
707bfff6
TS
371#define ISA_HAS_ROR(ISA) \
372 ((ISA) == ISA_MIPS32R2 \
373 || (ISA) == ISA_MIPS64R2 \
846ef2d0 374 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
375 || mips_opts.micromips \
376 )
707bfff6 377
7455baf8
TS
378/* Return true if ISA supports single-precision floats in odd registers. */
379#define ISA_HAS_ODD_SINGLE_FPR(ISA) \
380 ((ISA) == ISA_MIPS32 \
381 || (ISA) == ISA_MIPS32R2 \
382 || (ISA) == ISA_MIPS64 \
383 || (ISA) == ISA_MIPS64R2)
af7ee8bf 384
ad3fea08
TS
385/* Return true if ISA supports move to/from high part of a 64-bit
386 floating-point register. */
387#define ISA_HAS_MXHC1(ISA) \
388 ((ISA) == ISA_MIPS32R2 \
389 || (ISA) == ISA_MIPS64R2)
390
e013f690 391#define HAVE_32BIT_GPRS \
ad3fea08 392 (mips_opts.gp32 || !ISA_HAS_64BIT_REGS (mips_opts.isa))
ca4e0257 393
e013f690 394#define HAVE_32BIT_FPRS \
ad3fea08 395 (mips_opts.fp32 || !ISA_HAS_64BIT_FPRS (mips_opts.isa))
ca4e0257 396
ad3fea08
TS
397#define HAVE_64BIT_GPRS (!HAVE_32BIT_GPRS)
398#define HAVE_64BIT_FPRS (!HAVE_32BIT_FPRS)
ca4e0257 399
316f5878 400#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 401
316f5878 402#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 403
3b91255e
RS
404/* True if relocations are stored in-place. */
405#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
406
aed1a261
RS
407/* The ABI-derived address size. */
408#define HAVE_64BIT_ADDRESSES \
409 (HAVE_64BIT_GPRS && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
410#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 411
aed1a261
RS
412/* The size of symbolic constants (i.e., expressions of the form
413 "SYMBOL" or "SYMBOL + OFFSET"). */
414#define HAVE_32BIT_SYMBOLS \
415 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
416#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 417
b7c7d6c1
TS
418/* Addresses are loaded in different ways, depending on the address size
419 in use. The n32 ABI Documentation also mandates the use of additions
420 with overflow checking, but existing implementations don't follow it. */
f899b4b8 421#define ADDRESS_ADD_INSN \
b7c7d6c1 422 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
423
424#define ADDRESS_ADDI_INSN \
b7c7d6c1 425 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
426
427#define ADDRESS_LOAD_INSN \
428 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
429
430#define ADDRESS_STORE_INSN \
431 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
432
a4672219 433/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
434#define CPU_HAS_MIPS16(cpu) \
435 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
436 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 437
2309ddf2 438/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
439#define CPU_HAS_MICROMIPS(cpu) 0
440
60b63b72
RS
441/* True if CPU has a dror instruction. */
442#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
443
444/* True if CPU has a ror instruction. */
445#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
446
dd6a37e7 447/* True if CPU is in the Octeon family */
432233b3 448#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP || (CPU) == CPU_OCTEON2)
dd6a37e7 449
dd3cbb7e 450/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 451#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 452
0aa27725
RS
453/* True, if CPU has support for ldc1 and sdc1. */
454#define CPU_HAS_LDC1_SDC1(CPU) \
455 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
456
c8978940
CD
457/* True if mflo and mfhi can be immediately followed by instructions
458 which write to the HI and LO registers.
459
460 According to MIPS specifications, MIPS ISAs I, II, and III need
461 (at least) two instructions between the reads of HI/LO and
462 instructions which write them, and later ISAs do not. Contradicting
463 the MIPS specifications, some MIPS IV processor user manuals (e.g.
464 the UM for the NEC Vr5000) document needing the instructions between
465 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
466 MIPS64 and later ISAs to have the interlocks, plus any specific
467 earlier-ISA CPUs for which CPU documentation declares that the
468 instructions are really interlocked. */
469#define hilo_interlocks \
470 (mips_opts.isa == ISA_MIPS32 \
471 || mips_opts.isa == ISA_MIPS32R2 \
472 || mips_opts.isa == ISA_MIPS64 \
473 || mips_opts.isa == ISA_MIPS64R2 \
474 || mips_opts.arch == CPU_R4010 \
e407c74b 475 || mips_opts.arch == CPU_R5900 \
c8978940
CD
476 || mips_opts.arch == CPU_R10000 \
477 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
478 || mips_opts.arch == CPU_R14000 \
479 || mips_opts.arch == CPU_R16000 \
c8978940 480 || mips_opts.arch == CPU_RM7000 \
c8978940 481 || mips_opts.arch == CPU_VR5500 \
df58fc94 482 || mips_opts.micromips \
c8978940 483 )
252b5132
RH
484
485/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
486 from the GPRs after they are loaded from memory, and thus does not
487 require nops to be inserted. This applies to instructions marked
488 INSN_LOAD_MEMORY_DELAY. These nops are only required at MIPS ISA
df58fc94
RS
489 level I and microMIPS mode instructions are always interlocked. */
490#define gpr_interlocks \
491 (mips_opts.isa != ISA_MIPS1 \
492 || mips_opts.arch == CPU_R3900 \
e407c74b 493 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
494 || mips_opts.micromips \
495 )
252b5132 496
81912461
ILT
497/* Whether the processor uses hardware interlocks to avoid delays
498 required by coprocessor instructions, and thus does not require
499 nops to be inserted. This applies to instructions marked
500 INSN_LOAD_COPROC_DELAY, INSN_COPROC_MOVE_DELAY, and to delays
501 between instructions marked INSN_WRITE_COND_CODE and ones marked
502 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
503 levels I, II, and III and microMIPS mode instructions are always
504 interlocked. */
bdaaa2e1 505/* Itbl support may require additional care here. */
81912461
ILT
506#define cop_interlocks \
507 ((mips_opts.isa != ISA_MIPS1 \
508 && mips_opts.isa != ISA_MIPS2 \
509 && mips_opts.isa != ISA_MIPS3) \
510 || mips_opts.arch == CPU_R4300 \
df58fc94 511 || mips_opts.micromips \
81912461
ILT
512 )
513
514/* Whether the processor uses hardware interlocks to protect reads
515 from coprocessor registers after they are loaded from memory, and
516 thus does not require nops to be inserted. This applies to
517 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
518 requires at MIPS ISA level I and microMIPS mode instructions are
519 always interlocked. */
520#define cop_mem_interlocks \
521 (mips_opts.isa != ISA_MIPS1 \
522 || mips_opts.micromips \
523 )
252b5132 524
6b76fefe
CM
525/* Is this a mfhi or mflo instruction? */
526#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
527 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
528
df58fc94
RS
529/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
530 has been selected. This implies, in particular, that addresses of text
531 labels have their LSB set. */
532#define HAVE_CODE_COMPRESSION \
533 ((mips_opts.mips16 | mips_opts.micromips) != 0)
534
42429eac
RS
535/* The minimum and maximum signed values that can be stored in a GPR. */
536#define GPR_SMAX ((offsetT) (((valueT) 1 << (HAVE_64BIT_GPRS ? 63 : 31)) - 1))
537#define GPR_SMIN (-GPR_SMAX - 1)
538
252b5132
RH
539/* MIPS PIC level. */
540
a161fe53 541enum mips_pic_level mips_pic;
252b5132 542
c9914766 543/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 544 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 545static int mips_big_got = 0;
252b5132
RH
546
547/* 1 if trap instructions should used for overflow rather than break
548 instructions. */
c9914766 549static int mips_trap = 0;
252b5132 550
119d663a 551/* 1 if double width floating point constants should not be constructed
b6ff326e 552 by assembling two single width halves into two single width floating
119d663a
NC
553 point registers which just happen to alias the double width destination
554 register. On some architectures this aliasing can be disabled by a bit
d547a75e 555 in the status register, and the setting of this bit cannot be determined
119d663a
NC
556 automatically at assemble time. */
557static int mips_disable_float_construction;
558
252b5132
RH
559/* Non-zero if any .set noreorder directives were used. */
560
561static int mips_any_noreorder;
562
6b76fefe
CM
563/* Non-zero if nops should be inserted when the register referenced in
564 an mfhi/mflo instruction is read in the next two instructions. */
565static int mips_7000_hilo_fix;
566
02ffd3e4 567/* The size of objects in the small data section. */
156c2f8b 568static unsigned int g_switch_value = 8;
252b5132
RH
569/* Whether the -G option was used. */
570static int g_switch_seen = 0;
571
572#define N_RMASK 0xc4
573#define N_VFP 0xd4
574
575/* If we can determine in advance that GP optimization won't be
576 possible, we can skip the relaxation stuff that tries to produce
577 GP-relative references. This makes delay slot optimization work
578 better.
579
580 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
581 gcc output. It needs to guess right for gcc, otherwise gcc
582 will put what it thinks is a GP-relative instruction in a branch
583 delay slot.
252b5132
RH
584
585 I don't know if a fix is needed for the SVR4_PIC mode. I've only
586 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 587static int nopic_need_relax (symbolS *, int);
252b5132
RH
588
589/* handle of the OPCODE hash table */
590static struct hash_control *op_hash = NULL;
591
592/* The opcode hash table we use for the mips16. */
593static struct hash_control *mips16_op_hash = NULL;
594
df58fc94
RS
595/* The opcode hash table we use for the microMIPS ASE. */
596static struct hash_control *micromips_op_hash = NULL;
597
252b5132
RH
598/* This array holds the chars that always start a comment. If the
599 pre-processor is disabled, these aren't very useful */
600const char comment_chars[] = "#";
601
602/* This array holds the chars that only start a comment at the beginning of
603 a line. If the line seems to have the form '# 123 filename'
604 .line and .file directives will appear in the pre-processed output */
605/* Note that input_file.c hand checks for '#' at the beginning of the
606 first line of the input file. This is because the compiler outputs
bdaaa2e1 607 #NO_APP at the beginning of its output. */
252b5132
RH
608/* Also note that C style comments are always supported. */
609const char line_comment_chars[] = "#";
610
bdaaa2e1 611/* This array holds machine specific line separator characters. */
63a0b638 612const char line_separator_chars[] = ";";
252b5132
RH
613
614/* Chars that can be used to separate mant from exp in floating point nums */
615const char EXP_CHARS[] = "eE";
616
617/* Chars that mean this number is a floating point constant */
618/* As in 0f12.456 */
619/* or 0d1.2345e12 */
620const char FLT_CHARS[] = "rRsSfFdDxXpP";
621
622/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
623 changed in read.c . Ideally it shouldn't have to know about it at all,
624 but nothing is ideal around here.
625 */
626
627static char *insn_error;
628
629static int auto_align = 1;
630
631/* When outputting SVR4 PIC code, the assembler needs to know the
632 offset in the stack frame from which to restore the $gp register.
633 This is set by the .cprestore pseudo-op, and saved in this
634 variable. */
635static offsetT mips_cprestore_offset = -1;
636
67c1ffbe 637/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 638 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 639 offset and even an other register than $gp as global pointer. */
6478892d
TS
640static offsetT mips_cpreturn_offset = -1;
641static int mips_cpreturn_register = -1;
642static int mips_gp_register = GP;
def2e0dd 643static int mips_gprel_offset = 0;
6478892d 644
7a621144
DJ
645/* Whether mips_cprestore_offset has been set in the current function
646 (or whether it has already been warned about, if not). */
647static int mips_cprestore_valid = 0;
648
252b5132
RH
649/* This is the register which holds the stack frame, as set by the
650 .frame pseudo-op. This is needed to implement .cprestore. */
651static int mips_frame_reg = SP;
652
7a621144
DJ
653/* Whether mips_frame_reg has been set in the current function
654 (or whether it has already been warned about, if not). */
655static int mips_frame_reg_valid = 0;
656
252b5132
RH
657/* To output NOP instructions correctly, we need to keep information
658 about the previous two instructions. */
659
660/* Whether we are optimizing. The default value of 2 means to remove
661 unneeded NOPs and swap branch instructions when possible. A value
662 of 1 means to not swap branches. A value of 0 means to always
663 insert NOPs. */
664static int mips_optimize = 2;
665
666/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
667 equivalent to seeing no -g option at all. */
668static int mips_debug = 0;
669
7d8e00cf
RS
670/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
671#define MAX_VR4130_NOPS 4
672
673/* The maximum number of NOPs needed to fill delay slots. */
674#define MAX_DELAY_NOPS 2
675
676/* The maximum number of NOPs needed for any purpose. */
677#define MAX_NOPS 4
71400594
RS
678
679/* A list of previous instructions, with index 0 being the most recent.
680 We need to look back MAX_NOPS instructions when filling delay slots
681 or working around processor errata. We need to look back one
682 instruction further if we're thinking about using history[0] to
683 fill a branch delay slot. */
684static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 685
1e915849 686/* Nop instructions used by emit_nop. */
df58fc94
RS
687static struct mips_cl_insn nop_insn;
688static struct mips_cl_insn mips16_nop_insn;
689static struct mips_cl_insn micromips_nop16_insn;
690static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
691
692/* The appropriate nop for the current mode. */
833794fc
MR
693#define NOP_INSN (mips_opts.mips16 \
694 ? &mips16_nop_insn \
695 : (mips_opts.micromips \
696 ? (mips_opts.insn32 \
697 ? &micromips_nop32_insn \
698 : &micromips_nop16_insn) \
699 : &nop_insn))
df58fc94
RS
700
701/* The size of NOP_INSN in bytes. */
833794fc
MR
702#define NOP_INSN_SIZE ((mips_opts.mips16 \
703 || (mips_opts.micromips && !mips_opts.insn32)) \
704 ? 2 : 4)
252b5132 705
252b5132
RH
706/* If this is set, it points to a frag holding nop instructions which
707 were inserted before the start of a noreorder section. If those
708 nops turn out to be unnecessary, the size of the frag can be
709 decreased. */
710static fragS *prev_nop_frag;
711
712/* The number of nop instructions we created in prev_nop_frag. */
713static int prev_nop_frag_holds;
714
715/* The number of nop instructions that we know we need in
bdaaa2e1 716 prev_nop_frag. */
252b5132
RH
717static int prev_nop_frag_required;
718
719/* The number of instructions we've seen since prev_nop_frag. */
720static int prev_nop_frag_since;
721
e8044f35
RS
722/* Relocations against symbols are sometimes done in two parts, with a HI
723 relocation and a LO relocation. Each relocation has only 16 bits of
724 space to store an addend. This means that in order for the linker to
725 handle carries correctly, it must be able to locate both the HI and
726 the LO relocation. This means that the relocations must appear in
727 order in the relocation table.
252b5132
RH
728
729 In order to implement this, we keep track of each unmatched HI
730 relocation. We then sort them so that they immediately precede the
bdaaa2e1 731 corresponding LO relocation. */
252b5132 732
e972090a
NC
733struct mips_hi_fixup
734{
252b5132
RH
735 /* Next HI fixup. */
736 struct mips_hi_fixup *next;
737 /* This fixup. */
738 fixS *fixp;
739 /* The section this fixup is in. */
740 segT seg;
741};
742
743/* The list of unmatched HI relocs. */
744
745static struct mips_hi_fixup *mips_hi_fixup_list;
746
64bdfcaf
RS
747/* The frag containing the last explicit relocation operator.
748 Null if explicit relocations have not been used. */
749
750static fragS *prev_reloc_op_frag;
751
252b5132
RH
752/* Map normal MIPS register numbers to mips16 register numbers. */
753
754#define X ILLEGAL_REG
e972090a
NC
755static const int mips32_to_16_reg_map[] =
756{
252b5132
RH
757 X, X, 2, 3, 4, 5, 6, 7,
758 X, X, X, X, X, X, X, X,
759 0, 1, X, X, X, X, X, X,
760 X, X, X, X, X, X, X, X
761};
762#undef X
763
764/* Map mips16 register numbers to normal MIPS register numbers. */
765
e972090a
NC
766static const unsigned int mips16_to_32_reg_map[] =
767{
252b5132
RH
768 16, 17, 2, 3, 4, 5, 6, 7
769};
60b63b72 770
df58fc94
RS
771/* Map normal MIPS register numbers to microMIPS register numbers. */
772
773#define mips32_to_micromips_reg_b_map mips32_to_16_reg_map
774#define mips32_to_micromips_reg_c_map mips32_to_16_reg_map
775#define mips32_to_micromips_reg_d_map mips32_to_16_reg_map
776#define mips32_to_micromips_reg_e_map mips32_to_16_reg_map
777#define mips32_to_micromips_reg_f_map mips32_to_16_reg_map
778#define mips32_to_micromips_reg_g_map mips32_to_16_reg_map
779#define mips32_to_micromips_reg_l_map mips32_to_16_reg_map
780
781#define X ILLEGAL_REG
782/* reg type h: 4, 5, 6. */
783static const int mips32_to_micromips_reg_h_map[] =
784{
785 X, X, X, X, 4, 5, 6, X,
786 X, X, X, X, X, X, X, X,
787 X, X, X, X, X, X, X, X,
788 X, X, X, X, X, X, X, X
789};
790
791/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
792static const int mips32_to_micromips_reg_m_map[] =
793{
794 0, X, 2, 3, X, X, X, X,
795 X, X, X, X, X, X, X, X,
796 4, 1, 5, 6, 7, X, X, X,
797 X, X, X, X, X, X, X, X
798};
799
800/* reg type q: 0, 2-7. 17. */
801static const int mips32_to_micromips_reg_q_map[] =
802{
803 0, X, 2, 3, 4, 5, 6, 7,
804 X, X, X, X, X, X, X, X,
805 X, 1, X, X, X, X, X, X,
806 X, X, X, X, X, X, X, X
807};
808
809#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
810#undef X
811
812/* Map microMIPS register numbers to normal MIPS register numbers. */
813
814#define micromips_to_32_reg_b_map mips16_to_32_reg_map
815#define micromips_to_32_reg_c_map mips16_to_32_reg_map
816#define micromips_to_32_reg_d_map mips16_to_32_reg_map
817#define micromips_to_32_reg_e_map mips16_to_32_reg_map
818#define micromips_to_32_reg_f_map mips16_to_32_reg_map
819#define micromips_to_32_reg_g_map mips16_to_32_reg_map
820
821/* The microMIPS registers with type h. */
822static const unsigned int micromips_to_32_reg_h_map[] =
823{
824 5, 5, 6, 4, 4, 4, 4, 4
825};
826
827/* The microMIPS registers with type i. */
828static const unsigned int micromips_to_32_reg_i_map[] =
829{
830 6, 7, 7, 21, 22, 5, 6, 7
831};
832
833#define micromips_to_32_reg_l_map mips16_to_32_reg_map
834
835/* The microMIPS registers with type m. */
836static const unsigned int micromips_to_32_reg_m_map[] =
837{
838 0, 17, 2, 3, 16, 18, 19, 20
839};
840
841#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
842
843/* The microMIPS registers with type q. */
844static const unsigned int micromips_to_32_reg_q_map[] =
845{
846 0, 17, 2, 3, 4, 5, 6, 7
847};
848
849/* microMIPS imm type B. */
850static const int micromips_imm_b_map[] =
851{
852 1, 4, 8, 12, 16, 20, 24, -1
853};
854
855/* microMIPS imm type C. */
856static const int micromips_imm_c_map[] =
857{
858 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
859};
860
71400594
RS
861/* Classifies the kind of instructions we're interested in when
862 implementing -mfix-vr4120. */
c67a084a
NC
863enum fix_vr4120_class
864{
71400594
RS
865 FIX_VR4120_MACC,
866 FIX_VR4120_DMACC,
867 FIX_VR4120_MULT,
868 FIX_VR4120_DMULT,
869 FIX_VR4120_DIV,
870 FIX_VR4120_MTHILO,
871 NUM_FIX_VR4120_CLASSES
872};
873
c67a084a
NC
874/* ...likewise -mfix-loongson2f-jump. */
875static bfd_boolean mips_fix_loongson2f_jump;
876
877/* ...likewise -mfix-loongson2f-nop. */
878static bfd_boolean mips_fix_loongson2f_nop;
879
880/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
881static bfd_boolean mips_fix_loongson2f;
882
71400594
RS
883/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
884 there must be at least one other instruction between an instruction
885 of type X and an instruction of type Y. */
886static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
887
888/* True if -mfix-vr4120 is in force. */
d766e8ec 889static int mips_fix_vr4120;
4a6a3df4 890
7d8e00cf
RS
891/* ...likewise -mfix-vr4130. */
892static int mips_fix_vr4130;
893
6a32d874
CM
894/* ...likewise -mfix-24k. */
895static int mips_fix_24k;
896
d954098f
DD
897/* ...likewise -mfix-cn63xxp1 */
898static bfd_boolean mips_fix_cn63xxp1;
899
4a6a3df4
AO
900/* We don't relax branches by default, since this causes us to expand
901 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
902 fail to compute the offset before expanding the macro to the most
903 efficient expansion. */
904
905static int mips_relax_branch;
252b5132 906\f
4d7206a2
RS
907/* The expansion of many macros depends on the type of symbol that
908 they refer to. For example, when generating position-dependent code,
909 a macro that refers to a symbol may have two different expansions,
910 one which uses GP-relative addresses and one which uses absolute
911 addresses. When generating SVR4-style PIC, a macro may have
912 different expansions for local and global symbols.
913
914 We handle these situations by generating both sequences and putting
915 them in variant frags. In position-dependent code, the first sequence
916 will be the GP-relative one and the second sequence will be the
917 absolute one. In SVR4 PIC, the first sequence will be for global
918 symbols and the second will be for local symbols.
919
584892a6
RS
920 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
921 SECOND are the lengths of the two sequences in bytes. These fields
922 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
923 the subtype has the following flags:
4d7206a2 924
584892a6
RS
925 RELAX_USE_SECOND
926 Set if it has been decided that we should use the second
927 sequence instead of the first.
928
929 RELAX_SECOND_LONGER
930 Set in the first variant frag if the macro's second implementation
931 is longer than its first. This refers to the macro as a whole,
932 not an individual relaxation.
933
934 RELAX_NOMACRO
935 Set in the first variant frag if the macro appeared in a .set nomacro
936 block and if one alternative requires a warning but the other does not.
937
938 RELAX_DELAY_SLOT
939 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
940 delay slot.
4d7206a2 941
df58fc94
RS
942 RELAX_DELAY_SLOT_16BIT
943 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
944 16-bit instruction.
945
946 RELAX_DELAY_SLOT_SIZE_FIRST
947 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
948 the macro is of the wrong size for the branch delay slot.
949
950 RELAX_DELAY_SLOT_SIZE_SECOND
951 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
952 the macro is of the wrong size for the branch delay slot.
953
4d7206a2
RS
954 The frag's "opcode" points to the first fixup for relaxable code.
955
956 Relaxable macros are generated using a sequence such as:
957
958 relax_start (SYMBOL);
959 ... generate first expansion ...
960 relax_switch ();
961 ... generate second expansion ...
962 relax_end ();
963
964 The code and fixups for the unwanted alternative are discarded
965 by md_convert_frag. */
584892a6 966#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 967
584892a6
RS
968#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
969#define RELAX_SECOND(X) ((X) & 0xff)
970#define RELAX_USE_SECOND 0x10000
971#define RELAX_SECOND_LONGER 0x20000
972#define RELAX_NOMACRO 0x40000
973#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
974#define RELAX_DELAY_SLOT_16BIT 0x100000
975#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
976#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 977
4a6a3df4
AO
978/* Branch without likely bit. If label is out of range, we turn:
979
980 beq reg1, reg2, label
981 delay slot
982
983 into
984
985 bne reg1, reg2, 0f
986 nop
987 j label
988 0: delay slot
989
990 with the following opcode replacements:
991
992 beq <-> bne
993 blez <-> bgtz
994 bltz <-> bgez
995 bc1f <-> bc1t
996
997 bltzal <-> bgezal (with jal label instead of j label)
998
999 Even though keeping the delay slot instruction in the delay slot of
1000 the branch would be more efficient, it would be very tricky to do
1001 correctly, because we'd have to introduce a variable frag *after*
1002 the delay slot instruction, and expand that instead. Let's do it
1003 the easy way for now, even if the branch-not-taken case now costs
1004 one additional instruction. Out-of-range branches are not supposed
1005 to be common, anyway.
1006
1007 Branch likely. If label is out of range, we turn:
1008
1009 beql reg1, reg2, label
1010 delay slot (annulled if branch not taken)
1011
1012 into
1013
1014 beql reg1, reg2, 1f
1015 nop
1016 beql $0, $0, 2f
1017 nop
1018 1: j[al] label
1019 delay slot (executed only if branch taken)
1020 2:
1021
1022 It would be possible to generate a shorter sequence by losing the
1023 likely bit, generating something like:
b34976b6 1024
4a6a3df4
AO
1025 bne reg1, reg2, 0f
1026 nop
1027 j[al] label
1028 delay slot (executed only if branch taken)
1029 0:
1030
1031 beql -> bne
1032 bnel -> beq
1033 blezl -> bgtz
1034 bgtzl -> blez
1035 bltzl -> bgez
1036 bgezl -> bltz
1037 bc1fl -> bc1t
1038 bc1tl -> bc1f
1039
1040 bltzall -> bgezal (with jal label instead of j label)
1041 bgezall -> bltzal (ditto)
1042
1043
1044 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1045#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1046 ((relax_substateT) \
1047 (0xc0000000 \
1048 | ((at) & 0x1f) \
1049 | ((toofar) ? 0x20 : 0) \
1050 | ((link) ? 0x40 : 0) \
1051 | ((likely) ? 0x80 : 0) \
1052 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1053#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1054#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1055#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1056#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1057#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1058#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1059
252b5132
RH
1060/* For mips16 code, we use an entirely different form of relaxation.
1061 mips16 supports two versions of most instructions which take
1062 immediate values: a small one which takes some small value, and a
1063 larger one which takes a 16 bit value. Since branches also follow
1064 this pattern, relaxing these values is required.
1065
1066 We can assemble both mips16 and normal MIPS code in a single
1067 object. Therefore, we need to support this type of relaxation at
1068 the same time that we support the relaxation described above. We
1069 use the high bit of the subtype field to distinguish these cases.
1070
1071 The information we store for this type of relaxation is the
1072 argument code found in the opcode file for this relocation, whether
1073 the user explicitly requested a small or extended form, and whether
1074 the relocation is in a jump or jal delay slot. That tells us the
1075 size of the value, and how it should be stored. We also store
1076 whether the fragment is considered to be extended or not. We also
1077 store whether this is known to be a branch to a different section,
1078 whether we have tried to relax this frag yet, and whether we have
1079 ever extended a PC relative fragment because of a shift count. */
1080#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1081 (0x80000000 \
1082 | ((type) & 0xff) \
1083 | ((small) ? 0x100 : 0) \
1084 | ((ext) ? 0x200 : 0) \
1085 | ((dslot) ? 0x400 : 0) \
1086 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1087#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1088#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1089#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1090#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1091#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1092#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1093#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1094#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1095#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1096#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1097#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1098#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1099
df58fc94
RS
1100/* For microMIPS code, we use relaxation similar to one we use for
1101 MIPS16 code. Some instructions that take immediate values support
1102 two encodings: a small one which takes some small value, and a
1103 larger one which takes a 16 bit value. As some branches also follow
1104 this pattern, relaxing these values is required.
1105
1106 We can assemble both microMIPS and normal MIPS code in a single
1107 object. Therefore, we need to support this type of relaxation at
1108 the same time that we support the relaxation described above. We
1109 use one of the high bits of the subtype field to distinguish these
1110 cases.
1111
1112 The information we store for this type of relaxation is the argument
1113 code found in the opcode file for this relocation, the register
40209cad
MR
1114 selected as the assembler temporary, whether the branch is
1115 unconditional, whether it is compact, whether it stores the link
1116 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1117 branches to a sequence of instructions is enabled, and whether the
1118 displacement of a branch is too large to fit as an immediate argument
1119 of a 16-bit and a 32-bit branch, respectively. */
1120#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1121 relax32, toofar16, toofar32) \
1122 (0x40000000 \
1123 | ((type) & 0xff) \
1124 | (((at) & 0x1f) << 8) \
1125 | ((uncond) ? 0x2000 : 0) \
1126 | ((compact) ? 0x4000 : 0) \
1127 | ((link) ? 0x8000 : 0) \
1128 | ((relax32) ? 0x10000 : 0) \
1129 | ((toofar16) ? 0x20000 : 0) \
1130 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1131#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1132#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1133#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1134#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1135#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1136#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1137#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1138
1139#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1140#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1141#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1142#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1143#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1144#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1145
43c0598f
RS
1146/* Sign-extend 16-bit value X. */
1147#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1148
885add95
CD
1149/* Is the given value a sign-extended 32-bit value? */
1150#define IS_SEXT_32BIT_NUM(x) \
1151 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1152 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1153
1154/* Is the given value a sign-extended 16-bit value? */
1155#define IS_SEXT_16BIT_NUM(x) \
1156 (((x) &~ (offsetT) 0x7fff) == 0 \
1157 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1158
df58fc94
RS
1159/* Is the given value a sign-extended 12-bit value? */
1160#define IS_SEXT_12BIT_NUM(x) \
1161 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1162
7f3c4072
CM
1163/* Is the given value a sign-extended 9-bit value? */
1164#define IS_SEXT_9BIT_NUM(x) \
1165 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1166
2051e8c4
MR
1167/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1168#define IS_ZEXT_32BIT_NUM(x) \
1169 (((x) &~ (offsetT) 0xffffffff) == 0 \
1170 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1171
bf12938e
RS
1172/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1173 VALUE << SHIFT. VALUE is evaluated exactly once. */
1174#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1175 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1176 | (((VALUE) & (MASK)) << (SHIFT)))
1177
1178/* Extract bits MASK << SHIFT from STRUCT and shift them right
1179 SHIFT places. */
1180#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1181 (((STRUCT) >> (SHIFT)) & (MASK))
1182
1183/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1184 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1185
1186 include/opcode/mips.h specifies operand fields using the macros
1187 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1188 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1189#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1190 do \
1191 if (!(MICROMIPS)) \
1192 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1193 OP_MASK_##FIELD, OP_SH_##FIELD); \
1194 else \
1195 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1196 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1197 while (0)
bf12938e
RS
1198#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1199 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1200 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1201
1202/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1203#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1204 (!(MICROMIPS) \
1205 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1206 : EXTRACT_BITS ((INSN).insn_opcode, \
1207 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1208#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1209 EXTRACT_BITS ((INSN).insn_opcode, \
1210 MIPS16OP_MASK_##FIELD, \
1211 MIPS16OP_SH_##FIELD)
5c04167a
RS
1212
1213/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1214#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1215\f
df58fc94
RS
1216/* Whether or not we are emitting a branch-likely macro. */
1217static bfd_boolean emit_branch_likely_macro = FALSE;
1218
4d7206a2
RS
1219/* Global variables used when generating relaxable macros. See the
1220 comment above RELAX_ENCODE for more details about how relaxation
1221 is used. */
1222static struct {
1223 /* 0 if we're not emitting a relaxable macro.
1224 1 if we're emitting the first of the two relaxation alternatives.
1225 2 if we're emitting the second alternative. */
1226 int sequence;
1227
1228 /* The first relaxable fixup in the current frag. (In other words,
1229 the first fixup that refers to relaxable code.) */
1230 fixS *first_fixup;
1231
1232 /* sizes[0] says how many bytes of the first alternative are stored in
1233 the current frag. Likewise sizes[1] for the second alternative. */
1234 unsigned int sizes[2];
1235
1236 /* The symbol on which the choice of sequence depends. */
1237 symbolS *symbol;
1238} mips_relax;
252b5132 1239\f
584892a6
RS
1240/* Global variables used to decide whether a macro needs a warning. */
1241static struct {
1242 /* True if the macro is in a branch delay slot. */
1243 bfd_boolean delay_slot_p;
1244
df58fc94
RS
1245 /* Set to the length in bytes required if the macro is in a delay slot
1246 that requires a specific length of instruction, otherwise zero. */
1247 unsigned int delay_slot_length;
1248
584892a6
RS
1249 /* For relaxable macros, sizes[0] is the length of the first alternative
1250 in bytes and sizes[1] is the length of the second alternative.
1251 For non-relaxable macros, both elements give the length of the
1252 macro in bytes. */
1253 unsigned int sizes[2];
1254
df58fc94
RS
1255 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1256 instruction of the first alternative in bytes and first_insn_sizes[1]
1257 is the length of the first instruction of the second alternative.
1258 For non-relaxable macros, both elements give the length of the first
1259 instruction in bytes.
1260
1261 Set to zero if we haven't yet seen the first instruction. */
1262 unsigned int first_insn_sizes[2];
1263
1264 /* For relaxable macros, insns[0] is the number of instructions for the
1265 first alternative and insns[1] is the number of instructions for the
1266 second alternative.
1267
1268 For non-relaxable macros, both elements give the number of
1269 instructions for the macro. */
1270 unsigned int insns[2];
1271
584892a6
RS
1272 /* The first variant frag for this macro. */
1273 fragS *first_frag;
1274} mips_macro_warning;
1275\f
252b5132
RH
1276/* Prototypes for static functions. */
1277
252b5132
RH
1278enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1279
b34976b6 1280static void append_insn
df58fc94
RS
1281 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1282 bfd_boolean expansionp);
7d10b47d 1283static void mips_no_prev_insn (void);
c67a084a 1284static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1285static void mips16_macro_build
03ea81db 1286 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1287static void load_register (int, expressionS *, int);
584892a6
RS
1288static void macro_start (void);
1289static void macro_end (void);
833794fc 1290static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1291static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1292static void mips_ip (char *str, struct mips_cl_insn * ip);
1293static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1294static void mips16_immed
43c0598f
RS
1295 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1296 unsigned int, unsigned long *);
5e0116d5 1297static size_t my_getSmallExpression
17a2f251
TS
1298 (expressionS *, bfd_reloc_code_real_type *, char *);
1299static void my_getExpression (expressionS *, char *);
1300static void s_align (int);
1301static void s_change_sec (int);
1302static void s_change_section (int);
1303static void s_cons (int);
1304static void s_float_cons (int);
1305static void s_mips_globl (int);
1306static void s_option (int);
1307static void s_mipsset (int);
1308static void s_abicalls (int);
1309static void s_cpload (int);
1310static void s_cpsetup (int);
1311static void s_cplocal (int);
1312static void s_cprestore (int);
1313static void s_cpreturn (int);
741d6ea8
JM
1314static void s_dtprelword (int);
1315static void s_dtpreldword (int);
d0f13682
CLT
1316static void s_tprelword (int);
1317static void s_tpreldword (int);
17a2f251
TS
1318static void s_gpvalue (int);
1319static void s_gpword (int);
1320static void s_gpdword (int);
a3f278e2 1321static void s_ehword (int);
17a2f251
TS
1322static void s_cpadd (int);
1323static void s_insn (int);
1324static void md_obj_begin (void);
1325static void md_obj_end (void);
1326static void s_mips_ent (int);
1327static void s_mips_end (int);
1328static void s_mips_frame (int);
1329static void s_mips_mask (int reg_type);
1330static void s_mips_stab (int);
1331static void s_mips_weakext (int);
1332static void s_mips_file (int);
1333static void s_mips_loc (int);
1334static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1335static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1336static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1337static int validate_micromips_insn (const struct mips_opcode *);
1338static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1339static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1340
1341/* Table and functions used to map between CPU/ISA names, and
1342 ISA levels, and CPU numbers. */
1343
e972090a
NC
1344struct mips_cpu_info
1345{
e7af610e 1346 const char *name; /* CPU or ISA name. */
d16afab6
RS
1347 int flags; /* MIPS_CPU_* flags. */
1348 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1349 int isa; /* ISA level. */
1350 int cpu; /* CPU number (default CPU if ISA). */
1351};
1352
ad3fea08 1353#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1354
17a2f251
TS
1355static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1356static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1357static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1358\f
c31f3936
RS
1359/* Command-line options. */
1360const char *md_shortopts = "O::g::G:";
1361
1362enum options
1363 {
1364 OPTION_MARCH = OPTION_MD_BASE,
1365 OPTION_MTUNE,
1366 OPTION_MIPS1,
1367 OPTION_MIPS2,
1368 OPTION_MIPS3,
1369 OPTION_MIPS4,
1370 OPTION_MIPS5,
1371 OPTION_MIPS32,
1372 OPTION_MIPS64,
1373 OPTION_MIPS32R2,
1374 OPTION_MIPS64R2,
1375 OPTION_MIPS16,
1376 OPTION_NO_MIPS16,
1377 OPTION_MIPS3D,
1378 OPTION_NO_MIPS3D,
1379 OPTION_MDMX,
1380 OPTION_NO_MDMX,
1381 OPTION_DSP,
1382 OPTION_NO_DSP,
1383 OPTION_MT,
1384 OPTION_NO_MT,
1385 OPTION_VIRT,
1386 OPTION_NO_VIRT,
1387 OPTION_SMARTMIPS,
1388 OPTION_NO_SMARTMIPS,
1389 OPTION_DSPR2,
1390 OPTION_NO_DSPR2,
1391 OPTION_EVA,
1392 OPTION_NO_EVA,
1393 OPTION_MICROMIPS,
1394 OPTION_NO_MICROMIPS,
1395 OPTION_MCU,
1396 OPTION_NO_MCU,
1397 OPTION_COMPAT_ARCH_BASE,
1398 OPTION_M4650,
1399 OPTION_NO_M4650,
1400 OPTION_M4010,
1401 OPTION_NO_M4010,
1402 OPTION_M4100,
1403 OPTION_NO_M4100,
1404 OPTION_M3900,
1405 OPTION_NO_M3900,
1406 OPTION_M7000_HILO_FIX,
1407 OPTION_MNO_7000_HILO_FIX,
1408 OPTION_FIX_24K,
1409 OPTION_NO_FIX_24K,
1410 OPTION_FIX_LOONGSON2F_JUMP,
1411 OPTION_NO_FIX_LOONGSON2F_JUMP,
1412 OPTION_FIX_LOONGSON2F_NOP,
1413 OPTION_NO_FIX_LOONGSON2F_NOP,
1414 OPTION_FIX_VR4120,
1415 OPTION_NO_FIX_VR4120,
1416 OPTION_FIX_VR4130,
1417 OPTION_NO_FIX_VR4130,
1418 OPTION_FIX_CN63XXP1,
1419 OPTION_NO_FIX_CN63XXP1,
1420 OPTION_TRAP,
1421 OPTION_BREAK,
1422 OPTION_EB,
1423 OPTION_EL,
1424 OPTION_FP32,
1425 OPTION_GP32,
1426 OPTION_CONSTRUCT_FLOATS,
1427 OPTION_NO_CONSTRUCT_FLOATS,
1428 OPTION_FP64,
1429 OPTION_GP64,
1430 OPTION_RELAX_BRANCH,
1431 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1432 OPTION_INSN32,
1433 OPTION_NO_INSN32,
c31f3936
RS
1434 OPTION_MSHARED,
1435 OPTION_MNO_SHARED,
1436 OPTION_MSYM32,
1437 OPTION_MNO_SYM32,
1438 OPTION_SOFT_FLOAT,
1439 OPTION_HARD_FLOAT,
1440 OPTION_SINGLE_FLOAT,
1441 OPTION_DOUBLE_FLOAT,
1442 OPTION_32,
c31f3936
RS
1443 OPTION_CALL_SHARED,
1444 OPTION_CALL_NONPIC,
1445 OPTION_NON_SHARED,
1446 OPTION_XGOT,
1447 OPTION_MABI,
1448 OPTION_N32,
1449 OPTION_64,
1450 OPTION_MDEBUG,
1451 OPTION_NO_MDEBUG,
1452 OPTION_PDR,
1453 OPTION_NO_PDR,
1454 OPTION_MVXWORKS_PIC,
c31f3936
RS
1455 OPTION_END_OF_ENUM
1456 };
1457
1458struct option md_longopts[] =
1459{
1460 /* Options which specify architecture. */
1461 {"march", required_argument, NULL, OPTION_MARCH},
1462 {"mtune", required_argument, NULL, OPTION_MTUNE},
1463 {"mips0", no_argument, NULL, OPTION_MIPS1},
1464 {"mips1", no_argument, NULL, OPTION_MIPS1},
1465 {"mips2", no_argument, NULL, OPTION_MIPS2},
1466 {"mips3", no_argument, NULL, OPTION_MIPS3},
1467 {"mips4", no_argument, NULL, OPTION_MIPS4},
1468 {"mips5", no_argument, NULL, OPTION_MIPS5},
1469 {"mips32", no_argument, NULL, OPTION_MIPS32},
1470 {"mips64", no_argument, NULL, OPTION_MIPS64},
1471 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1472 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1473
1474 /* Options which specify Application Specific Extensions (ASEs). */
1475 {"mips16", no_argument, NULL, OPTION_MIPS16},
1476 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1477 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1478 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1479 {"mdmx", no_argument, NULL, OPTION_MDMX},
1480 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1481 {"mdsp", no_argument, NULL, OPTION_DSP},
1482 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1483 {"mmt", no_argument, NULL, OPTION_MT},
1484 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1485 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1486 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1487 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1488 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1489 {"meva", no_argument, NULL, OPTION_EVA},
1490 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1491 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1492 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1493 {"mmcu", no_argument, NULL, OPTION_MCU},
1494 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1495 {"mvirt", no_argument, NULL, OPTION_VIRT},
1496 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1497
1498 /* Old-style architecture options. Don't add more of these. */
1499 {"m4650", no_argument, NULL, OPTION_M4650},
1500 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1501 {"m4010", no_argument, NULL, OPTION_M4010},
1502 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1503 {"m4100", no_argument, NULL, OPTION_M4100},
1504 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1505 {"m3900", no_argument, NULL, OPTION_M3900},
1506 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1507
1508 /* Options which enable bug fixes. */
1509 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1510 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1511 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1512 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1513 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1514 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1515 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1516 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1517 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1518 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1519 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1520 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1521 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1522 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1523 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1524
1525 /* Miscellaneous options. */
1526 {"trap", no_argument, NULL, OPTION_TRAP},
1527 {"no-break", no_argument, NULL, OPTION_TRAP},
1528 {"break", no_argument, NULL, OPTION_BREAK},
1529 {"no-trap", no_argument, NULL, OPTION_BREAK},
1530 {"EB", no_argument, NULL, OPTION_EB},
1531 {"EL", no_argument, NULL, OPTION_EL},
1532 {"mfp32", no_argument, NULL, OPTION_FP32},
1533 {"mgp32", no_argument, NULL, OPTION_GP32},
1534 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1535 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1536 {"mfp64", no_argument, NULL, OPTION_FP64},
1537 {"mgp64", no_argument, NULL, OPTION_GP64},
1538 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1539 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1540 {"minsn32", no_argument, NULL, OPTION_INSN32},
1541 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1542 {"mshared", no_argument, NULL, OPTION_MSHARED},
1543 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1544 {"msym32", no_argument, NULL, OPTION_MSYM32},
1545 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1546 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1547 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1548 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1549 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1550
1551 /* Strictly speaking this next option is ELF specific,
1552 but we allow it for other ports as well in order to
1553 make testing easier. */
1554 {"32", no_argument, NULL, OPTION_32},
1555
1556 /* ELF-specific options. */
c31f3936
RS
1557 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1558 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1559 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1560 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1561 {"xgot", no_argument, NULL, OPTION_XGOT},
1562 {"mabi", required_argument, NULL, OPTION_MABI},
1563 {"n32", no_argument, NULL, OPTION_N32},
1564 {"64", no_argument, NULL, OPTION_64},
1565 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1566 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1567 {"mpdr", no_argument, NULL, OPTION_PDR},
1568 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1569 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
c31f3936
RS
1570
1571 {NULL, no_argument, NULL, 0}
1572};
1573size_t md_longopts_size = sizeof (md_longopts);
1574\f
c6278170
RS
1575/* Information about either an Application Specific Extension or an
1576 optional architecture feature that, for simplicity, we treat in the
1577 same way as an ASE. */
1578struct mips_ase
1579{
1580 /* The name of the ASE, used in both the command-line and .set options. */
1581 const char *name;
1582
1583 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1584 and 64-bit architectures, the flags here refer to the subset that
1585 is available on both. */
1586 unsigned int flags;
1587
1588 /* The ASE_* flag used for instructions that are available on 64-bit
1589 architectures but that are not included in FLAGS. */
1590 unsigned int flags64;
1591
1592 /* The command-line options that turn the ASE on and off. */
1593 int option_on;
1594 int option_off;
1595
1596 /* The minimum required architecture revisions for MIPS32, MIPS64,
1597 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1598 int mips32_rev;
1599 int mips64_rev;
1600 int micromips32_rev;
1601 int micromips64_rev;
1602};
1603
1604/* A table of all supported ASEs. */
1605static const struct mips_ase mips_ases[] = {
1606 { "dsp", ASE_DSP, ASE_DSP64,
1607 OPTION_DSP, OPTION_NO_DSP,
1608 2, 2, 2, 2 },
1609
1610 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1611 OPTION_DSPR2, OPTION_NO_DSPR2,
1612 2, 2, 2, 2 },
1613
1614 { "eva", ASE_EVA, 0,
1615 OPTION_EVA, OPTION_NO_EVA,
1616 2, 2, 2, 2 },
1617
1618 { "mcu", ASE_MCU, 0,
1619 OPTION_MCU, OPTION_NO_MCU,
1620 2, 2, 2, 2 },
1621
1622 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1623 { "mdmx", ASE_MDMX, 0,
1624 OPTION_MDMX, OPTION_NO_MDMX,
1625 -1, 1, -1, -1 },
1626
1627 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1628 { "mips3d", ASE_MIPS3D, 0,
1629 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1630 2, 1, -1, -1 },
1631
1632 { "mt", ASE_MT, 0,
1633 OPTION_MT, OPTION_NO_MT,
1634 2, 2, -1, -1 },
1635
1636 { "smartmips", ASE_SMARTMIPS, 0,
1637 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1638 1, -1, -1, -1 },
1639
1640 { "virt", ASE_VIRT, ASE_VIRT64,
1641 OPTION_VIRT, OPTION_NO_VIRT,
1642 2, 2, 2, 2 }
1643};
1644
1645/* The set of ASEs that require -mfp64. */
1646#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1647
1648/* Groups of ASE_* flags that represent different revisions of an ASE. */
1649static const unsigned int mips_ase_groups[] = {
1650 ASE_DSP | ASE_DSPR2
1651};
1652\f
252b5132
RH
1653/* Pseudo-op table.
1654
1655 The following pseudo-ops from the Kane and Heinrich MIPS book
1656 should be defined here, but are currently unsupported: .alias,
1657 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1658
1659 The following pseudo-ops from the Kane and Heinrich MIPS book are
1660 specific to the type of debugging information being generated, and
1661 should be defined by the object format: .aent, .begin, .bend,
1662 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1663 .vreg.
1664
1665 The following pseudo-ops from the Kane and Heinrich MIPS book are
1666 not MIPS CPU specific, but are also not specific to the object file
1667 format. This file is probably the best place to define them, but
d84bcf09 1668 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1669
e972090a
NC
1670static const pseudo_typeS mips_pseudo_table[] =
1671{
beae10d5 1672 /* MIPS specific pseudo-ops. */
252b5132
RH
1673 {"option", s_option, 0},
1674 {"set", s_mipsset, 0},
1675 {"rdata", s_change_sec, 'r'},
1676 {"sdata", s_change_sec, 's'},
1677 {"livereg", s_ignore, 0},
1678 {"abicalls", s_abicalls, 0},
1679 {"cpload", s_cpload, 0},
6478892d
TS
1680 {"cpsetup", s_cpsetup, 0},
1681 {"cplocal", s_cplocal, 0},
252b5132 1682 {"cprestore", s_cprestore, 0},
6478892d 1683 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1684 {"dtprelword", s_dtprelword, 0},
1685 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1686 {"tprelword", s_tprelword, 0},
1687 {"tpreldword", s_tpreldword, 0},
6478892d 1688 {"gpvalue", s_gpvalue, 0},
252b5132 1689 {"gpword", s_gpword, 0},
10181a0d 1690 {"gpdword", s_gpdword, 0},
a3f278e2 1691 {"ehword", s_ehword, 0},
252b5132
RH
1692 {"cpadd", s_cpadd, 0},
1693 {"insn", s_insn, 0},
1694
beae10d5 1695 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1696 chips. */
38a57ae7 1697 {"asciiz", stringer, 8 + 1},
252b5132
RH
1698 {"bss", s_change_sec, 'b'},
1699 {"err", s_err, 0},
1700 {"half", s_cons, 1},
1701 {"dword", s_cons, 3},
1702 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1703 {"origin", s_org, 0},
1704 {"repeat", s_rept, 0},
252b5132 1705
998b3c36
MR
1706 /* For MIPS this is non-standard, but we define it for consistency. */
1707 {"sbss", s_change_sec, 'B'},
1708
beae10d5 1709 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1710 here for one reason or another. */
1711 {"align", s_align, 0},
1712 {"byte", s_cons, 0},
1713 {"data", s_change_sec, 'd'},
1714 {"double", s_float_cons, 'd'},
1715 {"float", s_float_cons, 'f'},
1716 {"globl", s_mips_globl, 0},
1717 {"global", s_mips_globl, 0},
1718 {"hword", s_cons, 1},
1719 {"int", s_cons, 2},
1720 {"long", s_cons, 2},
1721 {"octa", s_cons, 4},
1722 {"quad", s_cons, 3},
cca86cc8 1723 {"section", s_change_section, 0},
252b5132
RH
1724 {"short", s_cons, 1},
1725 {"single", s_float_cons, 'f'},
754e2bb9 1726 {"stabd", s_mips_stab, 'd'},
252b5132 1727 {"stabn", s_mips_stab, 'n'},
754e2bb9 1728 {"stabs", s_mips_stab, 's'},
252b5132
RH
1729 {"text", s_change_sec, 't'},
1730 {"word", s_cons, 2},
add56521 1731
add56521 1732 { "extern", ecoff_directive_extern, 0},
add56521 1733
43841e91 1734 { NULL, NULL, 0 },
252b5132
RH
1735};
1736
e972090a
NC
1737static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1738{
beae10d5
KH
1739 /* These pseudo-ops should be defined by the object file format.
1740 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1741 {"aent", s_mips_ent, 1},
1742 {"bgnb", s_ignore, 0},
1743 {"end", s_mips_end, 0},
1744 {"endb", s_ignore, 0},
1745 {"ent", s_mips_ent, 0},
c5dd6aab 1746 {"file", s_mips_file, 0},
252b5132
RH
1747 {"fmask", s_mips_mask, 'F'},
1748 {"frame", s_mips_frame, 0},
c5dd6aab 1749 {"loc", s_mips_loc, 0},
252b5132
RH
1750 {"mask", s_mips_mask, 'R'},
1751 {"verstamp", s_ignore, 0},
43841e91 1752 { NULL, NULL, 0 },
252b5132
RH
1753};
1754
3ae8dd8d
MR
1755/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1756 purpose of the `.dc.a' internal pseudo-op. */
1757
1758int
1759mips_address_bytes (void)
1760{
1761 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1762}
1763
17a2f251 1764extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1765
1766void
17a2f251 1767mips_pop_insert (void)
252b5132
RH
1768{
1769 pop_insert (mips_pseudo_table);
1770 if (! ECOFF_DEBUGGING)
1771 pop_insert (mips_nonecoff_pseudo_table);
1772}
1773\f
1774/* Symbols labelling the current insn. */
1775
e972090a
NC
1776struct insn_label_list
1777{
252b5132
RH
1778 struct insn_label_list *next;
1779 symbolS *label;
1780};
1781
252b5132 1782static struct insn_label_list *free_insn_labels;
742a56fe 1783#define label_list tc_segment_info_data.labels
252b5132 1784
17a2f251 1785static void mips_clear_insn_labels (void);
df58fc94
RS
1786static void mips_mark_labels (void);
1787static void mips_compressed_mark_labels (void);
252b5132
RH
1788
1789static inline void
17a2f251 1790mips_clear_insn_labels (void)
252b5132
RH
1791{
1792 register struct insn_label_list **pl;
a8dbcb85 1793 segment_info_type *si;
252b5132 1794
a8dbcb85
TS
1795 if (now_seg)
1796 {
1797 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1798 ;
1799
1800 si = seg_info (now_seg);
1801 *pl = si->label_list;
1802 si->label_list = NULL;
1803 }
252b5132 1804}
a8dbcb85 1805
df58fc94
RS
1806/* Mark instruction labels in MIPS16/microMIPS mode. */
1807
1808static inline void
1809mips_mark_labels (void)
1810{
1811 if (HAVE_CODE_COMPRESSION)
1812 mips_compressed_mark_labels ();
1813}
252b5132
RH
1814\f
1815static char *expr_end;
1816
1817/* Expressions which appear in instructions. These are set by
1818 mips_ip. */
1819
1820static expressionS imm_expr;
5f74bc13 1821static expressionS imm2_expr;
252b5132
RH
1822static expressionS offset_expr;
1823
1824/* Relocs associated with imm_expr and offset_expr. */
1825
f6688943
TS
1826static bfd_reloc_code_real_type imm_reloc[3]
1827 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1828static bfd_reloc_code_real_type offset_reloc[3]
1829 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1830
df58fc94
RS
1831/* This is set to the resulting size of the instruction to be produced
1832 by mips16_ip if an explicit extension is used or by mips_ip if an
1833 explicit size is supplied. */
252b5132 1834
df58fc94 1835static unsigned int forced_insn_length;
252b5132 1836
e1b47bd5
RS
1837/* True if we are assembling an instruction. All dot symbols defined during
1838 this time should be treated as code labels. */
1839
1840static bfd_boolean mips_assembling_insn;
1841
ecb4347a
DJ
1842/* The pdr segment for per procedure frame/regmask info. Not used for
1843 ECOFF debugging. */
252b5132
RH
1844
1845static segT pdr_seg;
252b5132 1846
e013f690
TS
1847/* The default target format to use. */
1848
aeffff67
RS
1849#if defined (TE_FreeBSD)
1850#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1851#elif defined (TE_TMIPS)
1852#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1853#else
1854#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1855#endif
1856
e013f690 1857const char *
17a2f251 1858mips_target_format (void)
e013f690
TS
1859{
1860 switch (OUTPUT_FLAVOR)
1861 {
e013f690 1862 case bfd_target_elf_flavour:
0a44bf69
RS
1863#ifdef TE_VXWORKS
1864 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1865 return (target_big_endian
1866 ? "elf32-bigmips-vxworks"
1867 : "elf32-littlemips-vxworks");
1868#endif
e013f690 1869 return (target_big_endian
cfe86eaa 1870 ? (HAVE_64BIT_OBJECTS
aeffff67 1871 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1872 : (HAVE_NEWABI
aeffff67
RS
1873 ? ELF_TARGET ("elf32-n", "big")
1874 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1875 : (HAVE_64BIT_OBJECTS
aeffff67 1876 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1877 : (HAVE_NEWABI
aeffff67
RS
1878 ? ELF_TARGET ("elf32-n", "little")
1879 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1880 default:
1881 abort ();
1882 return NULL;
1883 }
1884}
1885
c6278170
RS
1886/* Return the ISA revision that is currently in use, or 0 if we are
1887 generating code for MIPS V or below. */
1888
1889static int
1890mips_isa_rev (void)
1891{
1892 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1893 return 2;
1894
1895 /* microMIPS implies revision 2 or above. */
1896 if (mips_opts.micromips)
1897 return 2;
1898
1899 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1900 return 1;
1901
1902 return 0;
1903}
1904
1905/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1906
1907static unsigned int
1908mips_ase_mask (unsigned int flags)
1909{
1910 unsigned int i;
1911
1912 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1913 if (flags & mips_ase_groups[i])
1914 flags |= mips_ase_groups[i];
1915 return flags;
1916}
1917
1918/* Check whether the current ISA supports ASE. Issue a warning if
1919 appropriate. */
1920
1921static void
1922mips_check_isa_supports_ase (const struct mips_ase *ase)
1923{
1924 const char *base;
1925 int min_rev, size;
1926 static unsigned int warned_isa;
1927 static unsigned int warned_fp32;
1928
1929 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1930 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1931 else
1932 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1933 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1934 && (warned_isa & ase->flags) != ase->flags)
1935 {
1936 warned_isa |= ase->flags;
1937 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1938 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1939 if (min_rev < 0)
1940 as_warn (_("The %d-bit %s architecture does not support the"
1941 " `%s' extension"), size, base, ase->name);
1942 else
1943 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1944 ase->name, base, size, min_rev);
1945 }
1946 if ((ase->flags & FP64_ASES)
1947 && mips_opts.fp32
1948 && (warned_fp32 & ase->flags) != ase->flags)
1949 {
1950 warned_fp32 |= ase->flags;
1951 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1952 }
1953}
1954
1955/* Check all enabled ASEs to see whether they are supported by the
1956 chosen architecture. */
1957
1958static void
1959mips_check_isa_supports_ases (void)
1960{
1961 unsigned int i, mask;
1962
1963 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1964 {
1965 mask = mips_ase_mask (mips_ases[i].flags);
1966 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1967 mips_check_isa_supports_ase (&mips_ases[i]);
1968 }
1969}
1970
1971/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1972 that were affected. */
1973
1974static unsigned int
1975mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1976{
1977 unsigned int mask;
1978
1979 mask = mips_ase_mask (ase->flags);
1980 mips_opts.ase &= ~mask;
1981 if (enabled_p)
1982 mips_opts.ase |= ase->flags;
1983 return mask;
1984}
1985
1986/* Return the ASE called NAME, or null if none. */
1987
1988static const struct mips_ase *
1989mips_lookup_ase (const char *name)
1990{
1991 unsigned int i;
1992
1993 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1994 if (strcmp (name, mips_ases[i].name) == 0)
1995 return &mips_ases[i];
1996 return NULL;
1997}
1998
df58fc94
RS
1999/* Return the length of a microMIPS instruction in bytes. If bits of
2000 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
2001 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
2002 major opcode) will require further modifications to the opcode
2003 table. */
2004
2005static inline unsigned int
2006micromips_insn_length (const struct mips_opcode *mo)
2007{
2008 return (mo->mask >> 16) == 0 ? 2 : 4;
2009}
2010
5c04167a
RS
2011/* Return the length of MIPS16 instruction OPCODE. */
2012
2013static inline unsigned int
2014mips16_opcode_length (unsigned long opcode)
2015{
2016 return (opcode >> 16) == 0 ? 2 : 4;
2017}
2018
1e915849
RS
2019/* Return the length of instruction INSN. */
2020
2021static inline unsigned int
2022insn_length (const struct mips_cl_insn *insn)
2023{
df58fc94
RS
2024 if (mips_opts.micromips)
2025 return micromips_insn_length (insn->insn_mo);
2026 else if (mips_opts.mips16)
5c04167a 2027 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2028 else
1e915849 2029 return 4;
1e915849
RS
2030}
2031
2032/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2033
2034static void
2035create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2036{
2037 size_t i;
2038
2039 insn->insn_mo = mo;
1e915849
RS
2040 insn->insn_opcode = mo->match;
2041 insn->frag = NULL;
2042 insn->where = 0;
2043 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2044 insn->fixp[i] = NULL;
2045 insn->fixed_p = (mips_opts.noreorder > 0);
2046 insn->noreorder_p = (mips_opts.noreorder > 0);
2047 insn->mips16_absolute_jump_p = 0;
15be625d 2048 insn->complete_p = 0;
e407c74b 2049 insn->cleared_p = 0;
1e915849
RS
2050}
2051
df58fc94 2052/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2053
2054static void
df58fc94 2055mips_record_compressed_mode (void)
742a56fe
RS
2056{
2057 segment_info_type *si;
2058
2059 si = seg_info (now_seg);
2060 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2061 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2062 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2063 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2064}
2065
4d68580a
RS
2066/* Read a standard MIPS instruction from BUF. */
2067
2068static unsigned long
2069read_insn (char *buf)
2070{
2071 if (target_big_endian)
2072 return bfd_getb32 ((bfd_byte *) buf);
2073 else
2074 return bfd_getl32 ((bfd_byte *) buf);
2075}
2076
2077/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2078 the next byte. */
2079
2080static char *
2081write_insn (char *buf, unsigned int insn)
2082{
2083 md_number_to_chars (buf, insn, 4);
2084 return buf + 4;
2085}
2086
2087/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2088 has length LENGTH. */
2089
2090static unsigned long
2091read_compressed_insn (char *buf, unsigned int length)
2092{
2093 unsigned long insn;
2094 unsigned int i;
2095
2096 insn = 0;
2097 for (i = 0; i < length; i += 2)
2098 {
2099 insn <<= 16;
2100 if (target_big_endian)
2101 insn |= bfd_getb16 ((char *) buf);
2102 else
2103 insn |= bfd_getl16 ((char *) buf);
2104 buf += 2;
2105 }
2106 return insn;
2107}
2108
5c04167a
RS
2109/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2110 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2111
2112static char *
2113write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2114{
2115 unsigned int i;
2116
2117 for (i = 0; i < length; i += 2)
2118 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2119 return buf + length;
2120}
2121
1e915849
RS
2122/* Install INSN at the location specified by its "frag" and "where" fields. */
2123
2124static void
2125install_insn (const struct mips_cl_insn *insn)
2126{
2127 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2128 if (HAVE_CODE_COMPRESSION)
2129 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2130 else
4d68580a 2131 write_insn (f, insn->insn_opcode);
df58fc94 2132 mips_record_compressed_mode ();
1e915849
RS
2133}
2134
2135/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2136 and install the opcode in the new location. */
2137
2138static void
2139move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2140{
2141 size_t i;
2142
2143 insn->frag = frag;
2144 insn->where = where;
2145 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2146 if (insn->fixp[i] != NULL)
2147 {
2148 insn->fixp[i]->fx_frag = frag;
2149 insn->fixp[i]->fx_where = where;
2150 }
2151 install_insn (insn);
2152}
2153
2154/* Add INSN to the end of the output. */
2155
2156static void
2157add_fixed_insn (struct mips_cl_insn *insn)
2158{
2159 char *f = frag_more (insn_length (insn));
2160 move_insn (insn, frag_now, f - frag_now->fr_literal);
2161}
2162
2163/* Start a variant frag and move INSN to the start of the variant part,
2164 marking it as fixed. The other arguments are as for frag_var. */
2165
2166static void
2167add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2168 relax_substateT subtype, symbolS *symbol, offsetT offset)
2169{
2170 frag_grow (max_chars);
2171 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2172 insn->fixed_p = 1;
2173 frag_var (rs_machine_dependent, max_chars, var,
2174 subtype, symbol, offset, NULL);
2175}
2176
2177/* Insert N copies of INSN into the history buffer, starting at
2178 position FIRST. Neither FIRST nor N need to be clipped. */
2179
2180static void
2181insert_into_history (unsigned int first, unsigned int n,
2182 const struct mips_cl_insn *insn)
2183{
2184 if (mips_relax.sequence != 2)
2185 {
2186 unsigned int i;
2187
2188 for (i = ARRAY_SIZE (history); i-- > first;)
2189 if (i >= first + n)
2190 history[i] = history[i - n];
2191 else
2192 history[i] = *insn;
2193 }
2194}
2195
71400594
RS
2196/* Initialize vr4120_conflicts. There is a bit of duplication here:
2197 the idea is to make it obvious at a glance that each errata is
2198 included. */
2199
2200static void
2201init_vr4120_conflicts (void)
2202{
2203#define CONFLICT(FIRST, SECOND) \
2204 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2205
2206 /* Errata 21 - [D]DIV[U] after [D]MACC */
2207 CONFLICT (MACC, DIV);
2208 CONFLICT (DMACC, DIV);
2209
2210 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2211 CONFLICT (DMULT, DMULT);
2212 CONFLICT (DMULT, DMACC);
2213 CONFLICT (DMACC, DMULT);
2214 CONFLICT (DMACC, DMACC);
2215
2216 /* Errata 24 - MT{LO,HI} after [D]MACC */
2217 CONFLICT (MACC, MTHILO);
2218 CONFLICT (DMACC, MTHILO);
2219
2220 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2221 instruction is executed immediately after a MACC or DMACC
2222 instruction, the result of [either instruction] is incorrect." */
2223 CONFLICT (MACC, MULT);
2224 CONFLICT (MACC, DMULT);
2225 CONFLICT (DMACC, MULT);
2226 CONFLICT (DMACC, DMULT);
2227
2228 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2229 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2230 DDIV or DDIVU instruction, the result of the MACC or
2231 DMACC instruction is incorrect.". */
2232 CONFLICT (DMULT, MACC);
2233 CONFLICT (DMULT, DMACC);
2234 CONFLICT (DIV, MACC);
2235 CONFLICT (DIV, DMACC);
2236
2237#undef CONFLICT
2238}
2239
707bfff6
TS
2240struct regname {
2241 const char *name;
2242 unsigned int num;
2243};
2244
2245#define RTYPE_MASK 0x1ff00
2246#define RTYPE_NUM 0x00100
2247#define RTYPE_FPU 0x00200
2248#define RTYPE_FCC 0x00400
2249#define RTYPE_VEC 0x00800
2250#define RTYPE_GP 0x01000
2251#define RTYPE_CP0 0x02000
2252#define RTYPE_PC 0x04000
2253#define RTYPE_ACC 0x08000
2254#define RTYPE_CCC 0x10000
2255#define RNUM_MASK 0x000ff
2256#define RWARN 0x80000
2257
2258#define GENERIC_REGISTER_NUMBERS \
2259 {"$0", RTYPE_NUM | 0}, \
2260 {"$1", RTYPE_NUM | 1}, \
2261 {"$2", RTYPE_NUM | 2}, \
2262 {"$3", RTYPE_NUM | 3}, \
2263 {"$4", RTYPE_NUM | 4}, \
2264 {"$5", RTYPE_NUM | 5}, \
2265 {"$6", RTYPE_NUM | 6}, \
2266 {"$7", RTYPE_NUM | 7}, \
2267 {"$8", RTYPE_NUM | 8}, \
2268 {"$9", RTYPE_NUM | 9}, \
2269 {"$10", RTYPE_NUM | 10}, \
2270 {"$11", RTYPE_NUM | 11}, \
2271 {"$12", RTYPE_NUM | 12}, \
2272 {"$13", RTYPE_NUM | 13}, \
2273 {"$14", RTYPE_NUM | 14}, \
2274 {"$15", RTYPE_NUM | 15}, \
2275 {"$16", RTYPE_NUM | 16}, \
2276 {"$17", RTYPE_NUM | 17}, \
2277 {"$18", RTYPE_NUM | 18}, \
2278 {"$19", RTYPE_NUM | 19}, \
2279 {"$20", RTYPE_NUM | 20}, \
2280 {"$21", RTYPE_NUM | 21}, \
2281 {"$22", RTYPE_NUM | 22}, \
2282 {"$23", RTYPE_NUM | 23}, \
2283 {"$24", RTYPE_NUM | 24}, \
2284 {"$25", RTYPE_NUM | 25}, \
2285 {"$26", RTYPE_NUM | 26}, \
2286 {"$27", RTYPE_NUM | 27}, \
2287 {"$28", RTYPE_NUM | 28}, \
2288 {"$29", RTYPE_NUM | 29}, \
2289 {"$30", RTYPE_NUM | 30}, \
2290 {"$31", RTYPE_NUM | 31}
2291
2292#define FPU_REGISTER_NAMES \
2293 {"$f0", RTYPE_FPU | 0}, \
2294 {"$f1", RTYPE_FPU | 1}, \
2295 {"$f2", RTYPE_FPU | 2}, \
2296 {"$f3", RTYPE_FPU | 3}, \
2297 {"$f4", RTYPE_FPU | 4}, \
2298 {"$f5", RTYPE_FPU | 5}, \
2299 {"$f6", RTYPE_FPU | 6}, \
2300 {"$f7", RTYPE_FPU | 7}, \
2301 {"$f8", RTYPE_FPU | 8}, \
2302 {"$f9", RTYPE_FPU | 9}, \
2303 {"$f10", RTYPE_FPU | 10}, \
2304 {"$f11", RTYPE_FPU | 11}, \
2305 {"$f12", RTYPE_FPU | 12}, \
2306 {"$f13", RTYPE_FPU | 13}, \
2307 {"$f14", RTYPE_FPU | 14}, \
2308 {"$f15", RTYPE_FPU | 15}, \
2309 {"$f16", RTYPE_FPU | 16}, \
2310 {"$f17", RTYPE_FPU | 17}, \
2311 {"$f18", RTYPE_FPU | 18}, \
2312 {"$f19", RTYPE_FPU | 19}, \
2313 {"$f20", RTYPE_FPU | 20}, \
2314 {"$f21", RTYPE_FPU | 21}, \
2315 {"$f22", RTYPE_FPU | 22}, \
2316 {"$f23", RTYPE_FPU | 23}, \
2317 {"$f24", RTYPE_FPU | 24}, \
2318 {"$f25", RTYPE_FPU | 25}, \
2319 {"$f26", RTYPE_FPU | 26}, \
2320 {"$f27", RTYPE_FPU | 27}, \
2321 {"$f28", RTYPE_FPU | 28}, \
2322 {"$f29", RTYPE_FPU | 29}, \
2323 {"$f30", RTYPE_FPU | 30}, \
2324 {"$f31", RTYPE_FPU | 31}
2325
2326#define FPU_CONDITION_CODE_NAMES \
2327 {"$fcc0", RTYPE_FCC | 0}, \
2328 {"$fcc1", RTYPE_FCC | 1}, \
2329 {"$fcc2", RTYPE_FCC | 2}, \
2330 {"$fcc3", RTYPE_FCC | 3}, \
2331 {"$fcc4", RTYPE_FCC | 4}, \
2332 {"$fcc5", RTYPE_FCC | 5}, \
2333 {"$fcc6", RTYPE_FCC | 6}, \
2334 {"$fcc7", RTYPE_FCC | 7}
2335
2336#define COPROC_CONDITION_CODE_NAMES \
2337 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2338 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2339 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2340 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2341 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2342 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2343 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2344 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2345
2346#define N32N64_SYMBOLIC_REGISTER_NAMES \
2347 {"$a4", RTYPE_GP | 8}, \
2348 {"$a5", RTYPE_GP | 9}, \
2349 {"$a6", RTYPE_GP | 10}, \
2350 {"$a7", RTYPE_GP | 11}, \
2351 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2352 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2353 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2354 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2355 {"$t0", RTYPE_GP | 12}, \
2356 {"$t1", RTYPE_GP | 13}, \
2357 {"$t2", RTYPE_GP | 14}, \
2358 {"$t3", RTYPE_GP | 15}
2359
2360#define O32_SYMBOLIC_REGISTER_NAMES \
2361 {"$t0", RTYPE_GP | 8}, \
2362 {"$t1", RTYPE_GP | 9}, \
2363 {"$t2", RTYPE_GP | 10}, \
2364 {"$t3", RTYPE_GP | 11}, \
2365 {"$t4", RTYPE_GP | 12}, \
2366 {"$t5", RTYPE_GP | 13}, \
2367 {"$t6", RTYPE_GP | 14}, \
2368 {"$t7", RTYPE_GP | 15}, \
2369 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2370 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2371 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2372 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2373
2374/* Remaining symbolic register names */
2375#define SYMBOLIC_REGISTER_NAMES \
2376 {"$zero", RTYPE_GP | 0}, \
2377 {"$at", RTYPE_GP | 1}, \
2378 {"$AT", RTYPE_GP | 1}, \
2379 {"$v0", RTYPE_GP | 2}, \
2380 {"$v1", RTYPE_GP | 3}, \
2381 {"$a0", RTYPE_GP | 4}, \
2382 {"$a1", RTYPE_GP | 5}, \
2383 {"$a2", RTYPE_GP | 6}, \
2384 {"$a3", RTYPE_GP | 7}, \
2385 {"$s0", RTYPE_GP | 16}, \
2386 {"$s1", RTYPE_GP | 17}, \
2387 {"$s2", RTYPE_GP | 18}, \
2388 {"$s3", RTYPE_GP | 19}, \
2389 {"$s4", RTYPE_GP | 20}, \
2390 {"$s5", RTYPE_GP | 21}, \
2391 {"$s6", RTYPE_GP | 22}, \
2392 {"$s7", RTYPE_GP | 23}, \
2393 {"$t8", RTYPE_GP | 24}, \
2394 {"$t9", RTYPE_GP | 25}, \
2395 {"$k0", RTYPE_GP | 26}, \
2396 {"$kt0", RTYPE_GP | 26}, \
2397 {"$k1", RTYPE_GP | 27}, \
2398 {"$kt1", RTYPE_GP | 27}, \
2399 {"$gp", RTYPE_GP | 28}, \
2400 {"$sp", RTYPE_GP | 29}, \
2401 {"$s8", RTYPE_GP | 30}, \
2402 {"$fp", RTYPE_GP | 30}, \
2403 {"$ra", RTYPE_GP | 31}
2404
2405#define MIPS16_SPECIAL_REGISTER_NAMES \
2406 {"$pc", RTYPE_PC | 0}
2407
2408#define MDMX_VECTOR_REGISTER_NAMES \
2409 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2410 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2411 {"$v2", RTYPE_VEC | 2}, \
2412 {"$v3", RTYPE_VEC | 3}, \
2413 {"$v4", RTYPE_VEC | 4}, \
2414 {"$v5", RTYPE_VEC | 5}, \
2415 {"$v6", RTYPE_VEC | 6}, \
2416 {"$v7", RTYPE_VEC | 7}, \
2417 {"$v8", RTYPE_VEC | 8}, \
2418 {"$v9", RTYPE_VEC | 9}, \
2419 {"$v10", RTYPE_VEC | 10}, \
2420 {"$v11", RTYPE_VEC | 11}, \
2421 {"$v12", RTYPE_VEC | 12}, \
2422 {"$v13", RTYPE_VEC | 13}, \
2423 {"$v14", RTYPE_VEC | 14}, \
2424 {"$v15", RTYPE_VEC | 15}, \
2425 {"$v16", RTYPE_VEC | 16}, \
2426 {"$v17", RTYPE_VEC | 17}, \
2427 {"$v18", RTYPE_VEC | 18}, \
2428 {"$v19", RTYPE_VEC | 19}, \
2429 {"$v20", RTYPE_VEC | 20}, \
2430 {"$v21", RTYPE_VEC | 21}, \
2431 {"$v22", RTYPE_VEC | 22}, \
2432 {"$v23", RTYPE_VEC | 23}, \
2433 {"$v24", RTYPE_VEC | 24}, \
2434 {"$v25", RTYPE_VEC | 25}, \
2435 {"$v26", RTYPE_VEC | 26}, \
2436 {"$v27", RTYPE_VEC | 27}, \
2437 {"$v28", RTYPE_VEC | 28}, \
2438 {"$v29", RTYPE_VEC | 29}, \
2439 {"$v30", RTYPE_VEC | 30}, \
2440 {"$v31", RTYPE_VEC | 31}
2441
2442#define MIPS_DSP_ACCUMULATOR_NAMES \
2443 {"$ac0", RTYPE_ACC | 0}, \
2444 {"$ac1", RTYPE_ACC | 1}, \
2445 {"$ac2", RTYPE_ACC | 2}, \
2446 {"$ac3", RTYPE_ACC | 3}
2447
2448static const struct regname reg_names[] = {
2449 GENERIC_REGISTER_NUMBERS,
2450 FPU_REGISTER_NAMES,
2451 FPU_CONDITION_CODE_NAMES,
2452 COPROC_CONDITION_CODE_NAMES,
2453
2454 /* The $txx registers depends on the abi,
2455 these will be added later into the symbol table from
2456 one of the tables below once mips_abi is set after
2457 parsing of arguments from the command line. */
2458 SYMBOLIC_REGISTER_NAMES,
2459
2460 MIPS16_SPECIAL_REGISTER_NAMES,
2461 MDMX_VECTOR_REGISTER_NAMES,
2462 MIPS_DSP_ACCUMULATOR_NAMES,
2463 {0, 0}
2464};
2465
2466static const struct regname reg_names_o32[] = {
2467 O32_SYMBOLIC_REGISTER_NAMES,
2468 {0, 0}
2469};
2470
2471static const struct regname reg_names_n32n64[] = {
2472 N32N64_SYMBOLIC_REGISTER_NAMES,
2473 {0, 0}
2474};
2475
df58fc94
RS
2476/* Check if S points at a valid register specifier according to TYPES.
2477 If so, then return 1, advance S to consume the specifier and store
2478 the register's number in REGNOP, otherwise return 0. */
2479
707bfff6
TS
2480static int
2481reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2482{
2483 symbolS *symbolP;
2484 char *e;
2485 char save_c;
2486 int reg = -1;
2487
2488 /* Find end of name. */
2489 e = *s;
2490 if (is_name_beginner (*e))
2491 ++e;
2492 while (is_part_of_name (*e))
2493 ++e;
2494
2495 /* Terminate name. */
2496 save_c = *e;
2497 *e = '\0';
2498
2499 /* Look for a register symbol. */
2500 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2501 {
2502 int r = S_GET_VALUE (symbolP);
2503 if (r & types)
2504 reg = r & RNUM_MASK;
2505 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2506 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2507 reg = (r & RNUM_MASK) - 2;
2508 }
2509 /* Else see if this is a register defined in an itbl entry. */
2510 else if ((types & RTYPE_GP) && itbl_have_entries)
2511 {
2512 char *n = *s;
2513 unsigned long r;
2514
2515 if (*n == '$')
2516 ++n;
2517 if (itbl_get_reg_val (n, &r))
2518 reg = r & RNUM_MASK;
2519 }
2520
2521 /* Advance to next token if a register was recognised. */
2522 if (reg >= 0)
2523 *s = e;
2524 else if (types & RWARN)
20203fb9 2525 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2526
2527 *e = save_c;
2528 if (regnop)
2529 *regnop = reg;
2530 return reg >= 0;
2531}
2532
df58fc94
RS
2533/* Check if S points at a valid register list according to TYPES.
2534 If so, then return 1, advance S to consume the list and store
2535 the registers present on the list as a bitmask of ones in REGLISTP,
2536 otherwise return 0. A valid list comprises a comma-separated
2537 enumeration of valid single registers and/or dash-separated
2538 contiguous register ranges as determined by their numbers.
2539
2540 As a special exception if one of s0-s7 registers is specified as
2541 the range's lower delimiter and s8 (fp) is its upper one, then no
2542 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2543 are selected; they have to be listed separately if needed. */
df58fc94
RS
2544
2545static int
2546reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2547{
2548 unsigned int reglist = 0;
2549 unsigned int lastregno;
2550 bfd_boolean ok = TRUE;
2551 unsigned int regmask;
2309ddf2 2552 char *s_endlist = *s;
df58fc94 2553 char *s_reset = *s;
2309ddf2 2554 unsigned int regno;
df58fc94
RS
2555
2556 while (reg_lookup (s, types, &regno))
2557 {
2558 lastregno = regno;
2559 if (**s == '-')
2560 {
2561 (*s)++;
2562 ok = reg_lookup (s, types, &lastregno);
2563 if (ok && lastregno < regno)
2564 ok = FALSE;
2565 if (!ok)
2566 break;
2567 }
2568
2569 if (lastregno == FP && regno >= S0 && regno <= S7)
2570 {
2571 lastregno = S7;
2572 reglist |= 1 << FP;
2573 }
2574 regmask = 1 << lastregno;
2575 regmask = (regmask << 1) - 1;
2576 regmask ^= (1 << regno) - 1;
2577 reglist |= regmask;
2578
2309ddf2 2579 s_endlist = *s;
df58fc94
RS
2580 if (**s != ',')
2581 break;
2582 (*s)++;
2583 }
2584
2585 if (ok)
2309ddf2 2586 *s = s_endlist;
df58fc94
RS
2587 else
2588 *s = s_reset;
2589 if (reglistp)
2590 *reglistp = reglist;
2591 return ok && reglist != 0;
2592}
2593
d301a56b
RS
2594/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2595 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2596
2597static bfd_boolean
f79e2745 2598is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2599{
2600 int isa = mips_opts.isa;
846ef2d0 2601 int ase = mips_opts.ase;
037b32b9 2602 int fp_s, fp_d;
c6278170 2603 unsigned int i;
037b32b9 2604
c6278170
RS
2605 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2606 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2607 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2608 ase |= mips_ases[i].flags64;
037b32b9 2609
d301a56b 2610 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2611 return FALSE;
2612
2613 /* Check whether the instruction or macro requires single-precision or
2614 double-precision floating-point support. Note that this information is
2615 stored differently in the opcode table for insns and macros. */
2616 if (mo->pinfo == INSN_MACRO)
2617 {
2618 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2619 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2620 }
2621 else
2622 {
2623 fp_s = mo->pinfo & FP_S;
2624 fp_d = mo->pinfo & FP_D;
2625 }
2626
2627 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2628 return FALSE;
2629
2630 if (fp_s && mips_opts.soft_float)
2631 return FALSE;
2632
2633 return TRUE;
2634}
2635
2636/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2637 selected ISA and architecture. */
2638
2639static bfd_boolean
2640is_opcode_valid_16 (const struct mips_opcode *mo)
2641{
d301a56b 2642 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2643}
2644
df58fc94
RS
2645/* Return TRUE if the size of the microMIPS opcode MO matches one
2646 explicitly requested. Always TRUE in the standard MIPS mode. */
2647
2648static bfd_boolean
2649is_size_valid (const struct mips_opcode *mo)
2650{
2651 if (!mips_opts.micromips)
2652 return TRUE;
2653
833794fc
MR
2654 if (mips_opts.insn32)
2655 {
2656 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2657 return FALSE;
2658 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2659 return FALSE;
2660 }
df58fc94
RS
2661 if (!forced_insn_length)
2662 return TRUE;
2663 if (mo->pinfo == INSN_MACRO)
2664 return FALSE;
2665 return forced_insn_length == micromips_insn_length (mo);
2666}
2667
2668/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2669 of the preceding instruction. Always TRUE in the standard MIPS mode.
2670
2671 We don't accept macros in 16-bit delay slots to avoid a case where
2672 a macro expansion fails because it relies on a preceding 32-bit real
2673 instruction to have matched and does not handle the operands correctly.
2674 The only macros that may expand to 16-bit instructions are JAL that
2675 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2676 and BGT (that likewise cannot be placed in a delay slot) that decay to
2677 a NOP. In all these cases the macros precede any corresponding real
2678 instruction definitions in the opcode table, so they will match in the
2679 second pass where the size of the delay slot is ignored and therefore
2680 produce correct code. */
df58fc94
RS
2681
2682static bfd_boolean
2683is_delay_slot_valid (const struct mips_opcode *mo)
2684{
2685 if (!mips_opts.micromips)
2686 return TRUE;
2687
2688 if (mo->pinfo == INSN_MACRO)
c06dec14 2689 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2690 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2691 && micromips_insn_length (mo) != 4)
2692 return FALSE;
2693 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2694 && micromips_insn_length (mo) != 2)
2695 return FALSE;
2696
2697 return TRUE;
2698}
2699
707bfff6
TS
2700/* This function is called once, at assembler startup time. It should set up
2701 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2702
252b5132 2703void
17a2f251 2704md_begin (void)
252b5132 2705{
3994f87e 2706 const char *retval = NULL;
156c2f8b 2707 int i = 0;
252b5132 2708 int broken = 0;
1f25f5d3 2709
0a44bf69
RS
2710 if (mips_pic != NO_PIC)
2711 {
2712 if (g_switch_seen && g_switch_value != 0)
2713 as_bad (_("-G may not be used in position-independent code"));
2714 g_switch_value = 0;
2715 }
2716
fef14a42 2717 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2718 as_warn (_("Could not set architecture and machine"));
2719
252b5132
RH
2720 op_hash = hash_new ();
2721
2722 for (i = 0; i < NUMOPCODES;)
2723 {
2724 const char *name = mips_opcodes[i].name;
2725
17a2f251 2726 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2727 if (retval != NULL)
2728 {
2729 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2730 mips_opcodes[i].name, retval);
2731 /* Probably a memory allocation problem? Give up now. */
2732 as_fatal (_("Broken assembler. No assembly attempted."));
2733 }
2734 do
2735 {
2736 if (mips_opcodes[i].pinfo != INSN_MACRO)
2737 {
2738 if (!validate_mips_insn (&mips_opcodes[i]))
2739 broken = 1;
1e915849
RS
2740 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2741 {
2742 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2743 if (mips_fix_loongson2f_nop)
2744 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2745 nop_insn.fixed_p = 1;
2746 }
252b5132
RH
2747 }
2748 ++i;
2749 }
2750 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2751 }
2752
2753 mips16_op_hash = hash_new ();
2754
2755 i = 0;
2756 while (i < bfd_mips16_num_opcodes)
2757 {
2758 const char *name = mips16_opcodes[i].name;
2759
17a2f251 2760 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2761 if (retval != NULL)
2762 as_fatal (_("internal: can't hash `%s': %s"),
2763 mips16_opcodes[i].name, retval);
2764 do
2765 {
2766 if (mips16_opcodes[i].pinfo != INSN_MACRO
2767 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2768 != mips16_opcodes[i].match))
2769 {
2770 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2771 mips16_opcodes[i].name, mips16_opcodes[i].args);
2772 broken = 1;
2773 }
1e915849
RS
2774 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2775 {
2776 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2777 mips16_nop_insn.fixed_p = 1;
2778 }
252b5132
RH
2779 ++i;
2780 }
2781 while (i < bfd_mips16_num_opcodes
2782 && strcmp (mips16_opcodes[i].name, name) == 0);
2783 }
2784
df58fc94
RS
2785 micromips_op_hash = hash_new ();
2786
2787 i = 0;
2788 while (i < bfd_micromips_num_opcodes)
2789 {
2790 const char *name = micromips_opcodes[i].name;
2791
2792 retval = hash_insert (micromips_op_hash, name,
2793 (void *) &micromips_opcodes[i]);
2794 if (retval != NULL)
2795 as_fatal (_("internal: can't hash `%s': %s"),
2796 micromips_opcodes[i].name, retval);
2797 do
2798 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2799 {
2800 struct mips_cl_insn *micromips_nop_insn;
2801
2802 if (!validate_micromips_insn (&micromips_opcodes[i]))
2803 broken = 1;
2804
2805 if (micromips_insn_length (micromips_opcodes + i) == 2)
2806 micromips_nop_insn = &micromips_nop16_insn;
2807 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2808 micromips_nop_insn = &micromips_nop32_insn;
2809 else
2810 continue;
2811
2812 if (micromips_nop_insn->insn_mo == NULL
2813 && strcmp (name, "nop") == 0)
2814 {
2815 create_insn (micromips_nop_insn, micromips_opcodes + i);
2816 micromips_nop_insn->fixed_p = 1;
2817 }
2818 }
2819 while (++i < bfd_micromips_num_opcodes
2820 && strcmp (micromips_opcodes[i].name, name) == 0);
2821 }
2822
252b5132
RH
2823 if (broken)
2824 as_fatal (_("Broken assembler. No assembly attempted."));
2825
2826 /* We add all the general register names to the symbol table. This
2827 helps us detect invalid uses of them. */
707bfff6
TS
2828 for (i = 0; reg_names[i].name; i++)
2829 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2830 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2831 &zero_address_frag));
2832 if (HAVE_NEWABI)
2833 for (i = 0; reg_names_n32n64[i].name; i++)
2834 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2835 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2836 &zero_address_frag));
707bfff6
TS
2837 else
2838 for (i = 0; reg_names_o32[i].name; i++)
2839 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2840 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2841 &zero_address_frag));
6047c971 2842
7d10b47d 2843 mips_no_prev_insn ();
252b5132
RH
2844
2845 mips_gprmask = 0;
2846 mips_cprmask[0] = 0;
2847 mips_cprmask[1] = 0;
2848 mips_cprmask[2] = 0;
2849 mips_cprmask[3] = 0;
2850
2851 /* set the default alignment for the text section (2**2) */
2852 record_alignment (text_section, 2);
2853
4d0d148d 2854 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2855
f3ded42a
RS
2856 /* On a native system other than VxWorks, sections must be aligned
2857 to 16 byte boundaries. When configured for an embedded ELF
2858 target, we don't bother. */
2859 if (strncmp (TARGET_OS, "elf", 3) != 0
2860 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 2861 {
f3ded42a
RS
2862 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2863 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2864 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2865 }
252b5132 2866
f3ded42a
RS
2867 /* Create a .reginfo section for register masks and a .mdebug
2868 section for debugging information. */
2869 {
2870 segT seg;
2871 subsegT subseg;
2872 flagword flags;
2873 segT sec;
2874
2875 seg = now_seg;
2876 subseg = now_subseg;
2877
2878 /* The ABI says this section should be loaded so that the
2879 running program can access it. However, we don't load it
2880 if we are configured for an embedded target */
2881 flags = SEC_READONLY | SEC_DATA;
2882 if (strncmp (TARGET_OS, "elf", 3) != 0)
2883 flags |= SEC_ALLOC | SEC_LOAD;
2884
2885 if (mips_abi != N64_ABI)
252b5132 2886 {
f3ded42a 2887 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 2888
f3ded42a
RS
2889 bfd_set_section_flags (stdoutput, sec, flags);
2890 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 2891
f3ded42a
RS
2892 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2893 }
2894 else
2895 {
2896 /* The 64-bit ABI uses a .MIPS.options section rather than
2897 .reginfo section. */
2898 sec = subseg_new (".MIPS.options", (subsegT) 0);
2899 bfd_set_section_flags (stdoutput, sec, flags);
2900 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2901
f3ded42a
RS
2902 /* Set up the option header. */
2903 {
2904 Elf_Internal_Options opthdr;
2905 char *f;
2906
2907 opthdr.kind = ODK_REGINFO;
2908 opthdr.size = (sizeof (Elf_External_Options)
2909 + sizeof (Elf64_External_RegInfo));
2910 opthdr.section = 0;
2911 opthdr.info = 0;
2912 f = frag_more (sizeof (Elf_External_Options));
2913 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2914 (Elf_External_Options *) f);
2915
2916 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2917 }
2918 }
252b5132 2919
f3ded42a
RS
2920 if (ECOFF_DEBUGGING)
2921 {
2922 sec = subseg_new (".mdebug", (subsegT) 0);
2923 (void) bfd_set_section_flags (stdoutput, sec,
2924 SEC_HAS_CONTENTS | SEC_READONLY);
2925 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 2926 }
f3ded42a
RS
2927 else if (mips_flag_pdr)
2928 {
2929 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2930 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2931 SEC_READONLY | SEC_RELOC
2932 | SEC_DEBUGGING);
2933 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2934 }
2935
2936 subseg_set (seg, subseg);
2937 }
252b5132
RH
2938
2939 if (! ECOFF_DEBUGGING)
2940 md_obj_begin ();
71400594
RS
2941
2942 if (mips_fix_vr4120)
2943 init_vr4120_conflicts ();
252b5132
RH
2944}
2945
2946void
17a2f251 2947md_mips_end (void)
252b5132 2948{
02b1ab82 2949 mips_emit_delays ();
252b5132
RH
2950 if (! ECOFF_DEBUGGING)
2951 md_obj_end ();
2952}
2953
2954void
17a2f251 2955md_assemble (char *str)
252b5132
RH
2956{
2957 struct mips_cl_insn insn;
f6688943
TS
2958 bfd_reloc_code_real_type unused_reloc[3]
2959 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2960
2961 imm_expr.X_op = O_absent;
5f74bc13 2962 imm2_expr.X_op = O_absent;
252b5132 2963 offset_expr.X_op = O_absent;
f6688943
TS
2964 imm_reloc[0] = BFD_RELOC_UNUSED;
2965 imm_reloc[1] = BFD_RELOC_UNUSED;
2966 imm_reloc[2] = BFD_RELOC_UNUSED;
2967 offset_reloc[0] = BFD_RELOC_UNUSED;
2968 offset_reloc[1] = BFD_RELOC_UNUSED;
2969 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2970
e1b47bd5
RS
2971 mips_mark_labels ();
2972 mips_assembling_insn = TRUE;
2973
252b5132
RH
2974 if (mips_opts.mips16)
2975 mips16_ip (str, &insn);
2976 else
2977 {
2978 mips_ip (str, &insn);
beae10d5
KH
2979 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2980 str, insn.insn_opcode));
252b5132
RH
2981 }
2982
2983 if (insn_error)
e1b47bd5
RS
2984 as_bad ("%s `%s'", insn_error, str);
2985 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2986 {
584892a6 2987 macro_start ();
252b5132
RH
2988 if (mips_opts.mips16)
2989 mips16_macro (&insn);
2990 else
833794fc 2991 macro (&insn, str);
584892a6 2992 macro_end ();
252b5132
RH
2993 }
2994 else
2995 {
2996 if (imm_expr.X_op != O_absent)
df58fc94 2997 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 2998 else if (offset_expr.X_op != O_absent)
df58fc94 2999 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3000 else
df58fc94 3001 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3002 }
e1b47bd5
RS
3003
3004 mips_assembling_insn = FALSE;
252b5132
RH
3005}
3006
738e5348
RS
3007/* Convenience functions for abstracting away the differences between
3008 MIPS16 and non-MIPS16 relocations. */
3009
3010static inline bfd_boolean
3011mips16_reloc_p (bfd_reloc_code_real_type reloc)
3012{
3013 switch (reloc)
3014 {
3015 case BFD_RELOC_MIPS16_JMP:
3016 case BFD_RELOC_MIPS16_GPREL:
3017 case BFD_RELOC_MIPS16_GOT16:
3018 case BFD_RELOC_MIPS16_CALL16:
3019 case BFD_RELOC_MIPS16_HI16_S:
3020 case BFD_RELOC_MIPS16_HI16:
3021 case BFD_RELOC_MIPS16_LO16:
3022 return TRUE;
3023
3024 default:
3025 return FALSE;
3026 }
3027}
3028
df58fc94
RS
3029static inline bfd_boolean
3030micromips_reloc_p (bfd_reloc_code_real_type reloc)
3031{
3032 switch (reloc)
3033 {
3034 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3035 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3036 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3037 case BFD_RELOC_MICROMIPS_GPREL16:
3038 case BFD_RELOC_MICROMIPS_JMP:
3039 case BFD_RELOC_MICROMIPS_HI16:
3040 case BFD_RELOC_MICROMIPS_HI16_S:
3041 case BFD_RELOC_MICROMIPS_LO16:
3042 case BFD_RELOC_MICROMIPS_LITERAL:
3043 case BFD_RELOC_MICROMIPS_GOT16:
3044 case BFD_RELOC_MICROMIPS_CALL16:
3045 case BFD_RELOC_MICROMIPS_GOT_HI16:
3046 case BFD_RELOC_MICROMIPS_GOT_LO16:
3047 case BFD_RELOC_MICROMIPS_CALL_HI16:
3048 case BFD_RELOC_MICROMIPS_CALL_LO16:
3049 case BFD_RELOC_MICROMIPS_SUB:
3050 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3051 case BFD_RELOC_MICROMIPS_GOT_OFST:
3052 case BFD_RELOC_MICROMIPS_GOT_DISP:
3053 case BFD_RELOC_MICROMIPS_HIGHEST:
3054 case BFD_RELOC_MICROMIPS_HIGHER:
3055 case BFD_RELOC_MICROMIPS_SCN_DISP:
3056 case BFD_RELOC_MICROMIPS_JALR:
3057 return TRUE;
3058
3059 default:
3060 return FALSE;
3061 }
3062}
3063
2309ddf2
MR
3064static inline bfd_boolean
3065jmp_reloc_p (bfd_reloc_code_real_type reloc)
3066{
3067 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3068}
3069
738e5348
RS
3070static inline bfd_boolean
3071got16_reloc_p (bfd_reloc_code_real_type reloc)
3072{
2309ddf2 3073 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3074 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3075}
3076
3077static inline bfd_boolean
3078hi16_reloc_p (bfd_reloc_code_real_type reloc)
3079{
2309ddf2 3080 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3081 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3082}
3083
3084static inline bfd_boolean
3085lo16_reloc_p (bfd_reloc_code_real_type reloc)
3086{
2309ddf2 3087 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3088 || reloc == BFD_RELOC_MICROMIPS_LO16);
3089}
3090
df58fc94
RS
3091static inline bfd_boolean
3092jalr_reloc_p (bfd_reloc_code_real_type reloc)
3093{
2309ddf2 3094 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3095}
3096
2de39019
CM
3097/* Return true if RELOC is a PC-relative relocation that does not have
3098 full address range. */
3099
3100static inline bfd_boolean
3101limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3102{
3103 switch (reloc)
3104 {
3105 case BFD_RELOC_16_PCREL_S2:
3106 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3107 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3108 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3109 return TRUE;
3110
b47468a6
CM
3111 case BFD_RELOC_32_PCREL:
3112 return HAVE_64BIT_ADDRESSES;
3113
2de39019
CM
3114 default:
3115 return FALSE;
3116 }
3117}
b47468a6 3118
5919d012 3119/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3120 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3121 need a matching %lo() when applied to local symbols. */
5919d012
RS
3122
3123static inline bfd_boolean
17a2f251 3124reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3125{
3b91255e 3126 return (HAVE_IN_PLACE_ADDENDS
738e5348 3127 && (hi16_reloc_p (reloc)
0a44bf69
RS
3128 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3129 all GOT16 relocations evaluate to "G". */
738e5348
RS
3130 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3131}
3132
3133/* Return the type of %lo() reloc needed by RELOC, given that
3134 reloc_needs_lo_p. */
3135
3136static inline bfd_reloc_code_real_type
3137matching_lo_reloc (bfd_reloc_code_real_type reloc)
3138{
df58fc94
RS
3139 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3140 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3141 : BFD_RELOC_LO16));
5919d012
RS
3142}
3143
3144/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3145 relocation. */
3146
3147static inline bfd_boolean
17a2f251 3148fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3149{
3150 return (fixp->fx_next != NULL
738e5348 3151 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3152 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3153 && fixp->fx_offset == fixp->fx_next->fx_offset);
3154}
3155
252b5132
RH
3156/* This function returns true if modifying a register requires a
3157 delay. */
3158
3159static int
17a2f251 3160reg_needs_delay (unsigned int reg)
252b5132
RH
3161{
3162 unsigned long prev_pinfo;
3163
47e39b9d 3164 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 3165 if (! mips_opts.noreorder
81912461
ILT
3166 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
3167 && ! gpr_interlocks)
3168 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
3169 && ! cop_interlocks)))
252b5132 3170 {
81912461
ILT
3171 /* A load from a coprocessor or from memory. All load delays
3172 delay the use of general register rt for one instruction. */
bdaaa2e1 3173 /* Itbl support may require additional care here. */
252b5132 3174 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 3175 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
3176 return 1;
3177 }
3178
3179 return 0;
3180}
3181
462427c4
RS
3182/* Move all labels in LABELS to the current insertion point. TEXT_P
3183 says whether the labels refer to text or data. */
404a8071
RS
3184
3185static void
462427c4 3186mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3187{
3188 struct insn_label_list *l;
3189 valueT val;
3190
462427c4 3191 for (l = labels; l != NULL; l = l->next)
404a8071 3192 {
9c2799c2 3193 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3194 symbol_set_frag (l->label, frag_now);
3195 val = (valueT) frag_now_fix ();
df58fc94 3196 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3197 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3198 ++val;
3199 S_SET_VALUE (l->label, val);
3200 }
3201}
3202
462427c4
RS
3203/* Move all labels in insn_labels to the current insertion point
3204 and treat them as text labels. */
3205
3206static void
3207mips_move_text_labels (void)
3208{
3209 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3210}
3211
5f0fe04b
TS
3212static bfd_boolean
3213s_is_linkonce (symbolS *sym, segT from_seg)
3214{
3215 bfd_boolean linkonce = FALSE;
3216 segT symseg = S_GET_SEGMENT (sym);
3217
3218 if (symseg != from_seg && !S_IS_LOCAL (sym))
3219 {
3220 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3221 linkonce = TRUE;
5f0fe04b
TS
3222 /* The GNU toolchain uses an extension for ELF: a section
3223 beginning with the magic string .gnu.linkonce is a
3224 linkonce section. */
3225 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3226 sizeof ".gnu.linkonce" - 1) == 0)
3227 linkonce = TRUE;
5f0fe04b
TS
3228 }
3229 return linkonce;
3230}
3231
e1b47bd5 3232/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3233 linker to handle them specially, such as generating jalx instructions
3234 when needed. We also make them odd for the duration of the assembly,
3235 in order to generate the right sort of code. We will make them even
252b5132
RH
3236 in the adjust_symtab routine, while leaving them marked. This is
3237 convenient for the debugger and the disassembler. The linker knows
3238 to make them odd again. */
3239
3240static void
e1b47bd5 3241mips_compressed_mark_label (symbolS *label)
252b5132 3242{
df58fc94 3243 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3244
f3ded42a
RS
3245 if (mips_opts.mips16)
3246 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3247 else
3248 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3249 if ((S_GET_VALUE (label) & 1) == 0
3250 /* Don't adjust the address if the label is global or weak, or
3251 in a link-once section, since we'll be emitting symbol reloc
3252 references to it which will be patched up by the linker, and
3253 the final value of the symbol may or may not be MIPS16/microMIPS. */
3254 && !S_IS_WEAK (label)
3255 && !S_IS_EXTERNAL (label)
3256 && !s_is_linkonce (label, now_seg))
3257 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3258}
3259
3260/* Mark preceding MIPS16 or microMIPS instruction labels. */
3261
3262static void
3263mips_compressed_mark_labels (void)
3264{
3265 struct insn_label_list *l;
3266
3267 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3268 mips_compressed_mark_label (l->label);
252b5132
RH
3269}
3270
4d7206a2
RS
3271/* End the current frag. Make it a variant frag and record the
3272 relaxation info. */
3273
3274static void
3275relax_close_frag (void)
3276{
584892a6 3277 mips_macro_warning.first_frag = frag_now;
4d7206a2 3278 frag_var (rs_machine_dependent, 0, 0,
584892a6 3279 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3280 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3281
3282 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3283 mips_relax.first_fixup = 0;
3284}
3285
3286/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3287 See the comment above RELAX_ENCODE for more details. */
3288
3289static void
3290relax_start (symbolS *symbol)
3291{
9c2799c2 3292 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3293 mips_relax.sequence = 1;
3294 mips_relax.symbol = symbol;
3295}
3296
3297/* Start generating the second version of a relaxable sequence.
3298 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3299
3300static void
4d7206a2
RS
3301relax_switch (void)
3302{
9c2799c2 3303 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3304 mips_relax.sequence = 2;
3305}
3306
3307/* End the current relaxable sequence. */
3308
3309static void
3310relax_end (void)
3311{
9c2799c2 3312 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3313 relax_close_frag ();
3314 mips_relax.sequence = 0;
3315}
3316
11625dd8
RS
3317/* Return true if IP is a delayed branch or jump. */
3318
3319static inline bfd_boolean
3320delayed_branch_p (const struct mips_cl_insn *ip)
3321{
3322 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3323 | INSN_COND_BRANCH_DELAY
3324 | INSN_COND_BRANCH_LIKELY)) != 0;
3325}
3326
3327/* Return true if IP is a compact branch or jump. */
3328
3329static inline bfd_boolean
3330compact_branch_p (const struct mips_cl_insn *ip)
3331{
3332 if (mips_opts.mips16)
3333 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3334 | MIPS16_INSN_COND_BRANCH)) != 0;
3335 else
3336 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3337 | INSN2_COND_BRANCH)) != 0;
3338}
3339
3340/* Return true if IP is an unconditional branch or jump. */
3341
3342static inline bfd_boolean
3343uncond_branch_p (const struct mips_cl_insn *ip)
3344{
3345 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3346 || (mips_opts.mips16
3347 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3348 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3349}
3350
3351/* Return true if IP is a branch-likely instruction. */
3352
3353static inline bfd_boolean
3354branch_likely_p (const struct mips_cl_insn *ip)
3355{
3356 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3357}
3358
14fe068b
RS
3359/* Return the type of nop that should be used to fill the delay slot
3360 of delayed branch IP. */
3361
3362static struct mips_cl_insn *
3363get_delay_slot_nop (const struct mips_cl_insn *ip)
3364{
3365 if (mips_opts.micromips
3366 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3367 return &micromips_nop32_insn;
3368 return NOP_INSN;
3369}
3370
2309ddf2 3371/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3372
3373static unsigned int
3374gpr_mod_mask (const struct mips_cl_insn *ip)
3375{
2309ddf2 3376 unsigned long pinfo2;
df58fc94
RS
3377 unsigned int mask;
3378
3379 mask = 0;
df58fc94
RS
3380 pinfo2 = ip->insn_mo->pinfo2;
3381 if (mips_opts.micromips)
3382 {
df58fc94
RS
3383 if (pinfo2 & INSN2_MOD_GPR_MD)
3384 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3385 if (pinfo2 & INSN2_MOD_GPR_MF)
3386 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3387 if (pinfo2 & INSN2_MOD_SP)
3388 mask |= 1 << SP;
3389 }
3390 return mask;
3391}
3392
4c260379
RS
3393/* Return the mask of core registers that IP reads. */
3394
3395static unsigned int
3396gpr_read_mask (const struct mips_cl_insn *ip)
3397{
3398 unsigned long pinfo, pinfo2;
3399 unsigned int mask;
3400
df58fc94 3401 mask = gpr_mod_mask (ip);
4c260379
RS
3402 pinfo = ip->insn_mo->pinfo;
3403 pinfo2 = ip->insn_mo->pinfo2;
3404 if (mips_opts.mips16)
3405 {
3406 if (pinfo & MIPS16_INSN_READ_X)
3407 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3408 if (pinfo & MIPS16_INSN_READ_Y)
3409 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3410 if (pinfo & MIPS16_INSN_READ_T)
3411 mask |= 1 << TREG;
3412 if (pinfo & MIPS16_INSN_READ_SP)
3413 mask |= 1 << SP;
3414 if (pinfo & MIPS16_INSN_READ_31)
3415 mask |= 1 << RA;
3416 if (pinfo & MIPS16_INSN_READ_Z)
3417 mask |= 1 << (mips16_to_32_reg_map
3418 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3419 if (pinfo & MIPS16_INSN_READ_GPR_X)
3420 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3421 }
3422 else
3423 {
3424 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3425 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3426 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3427 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3428 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3429 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3430 if (pinfo2 & INSN2_READ_GP)
3431 mask |= 1 << GP;
3432 if (pinfo2 & INSN2_READ_GPR_31)
3433 mask |= 1 << RA;
4c260379 3434 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3435 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3436 }
2b0c8b40
MR
3437 if (mips_opts.micromips)
3438 {
3439 if (pinfo2 & INSN2_READ_GPR_MC)
3440 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3441 if (pinfo2 & INSN2_READ_GPR_ME)
3442 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3443 if (pinfo2 & INSN2_READ_GPR_MG)
3444 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3445 if (pinfo2 & INSN2_READ_GPR_MJ)
3446 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3447 if (pinfo2 & INSN2_READ_GPR_MMN)
3448 {
3449 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3450 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3451 }
3452 if (pinfo2 & INSN2_READ_GPR_MP)
3453 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3454 if (pinfo2 & INSN2_READ_GPR_MQ)
3455 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3456 }
fe35f09f
RS
3457 /* Don't include register 0. */
3458 return mask & ~1;
4c260379
RS
3459}
3460
3461/* Return the mask of core registers that IP writes. */
3462
3463static unsigned int
3464gpr_write_mask (const struct mips_cl_insn *ip)
3465{
3466 unsigned long pinfo, pinfo2;
3467 unsigned int mask;
3468
df58fc94 3469 mask = gpr_mod_mask (ip);
4c260379
RS
3470 pinfo = ip->insn_mo->pinfo;
3471 pinfo2 = ip->insn_mo->pinfo2;
3472 if (mips_opts.mips16)
3473 {
3474 if (pinfo & MIPS16_INSN_WRITE_X)
3475 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3476 if (pinfo & MIPS16_INSN_WRITE_Y)
3477 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3478 if (pinfo & MIPS16_INSN_WRITE_Z)
3479 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3480 if (pinfo & MIPS16_INSN_WRITE_T)
3481 mask |= 1 << TREG;
3482 if (pinfo & MIPS16_INSN_WRITE_SP)
3483 mask |= 1 << SP;
3484 if (pinfo & MIPS16_INSN_WRITE_31)
3485 mask |= 1 << RA;
3486 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3487 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3488 }
3489 else
3490 {
3491 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3492 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3493 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3494 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3495 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3496 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3497 if (pinfo & INSN_WRITE_GPR_31)
3498 mask |= 1 << RA;
3499 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3500 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3501 }
2b0c8b40
MR
3502 if (mips_opts.micromips)
3503 {
3504 if (pinfo2 & INSN2_WRITE_GPR_MB)
3505 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
3506 if (pinfo2 & INSN2_WRITE_GPR_MHI)
3507 {
3508 mask |= 1 << micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
3509 mask |= 1 << micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
3510 }
3511 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3512 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3513 if (pinfo2 & INSN2_WRITE_GPR_MP)
3514 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3515 }
fe35f09f
RS
3516 /* Don't include register 0. */
3517 return mask & ~1;
4c260379
RS
3518}
3519
3520/* Return the mask of floating-point registers that IP reads. */
3521
3522static unsigned int
3523fpr_read_mask (const struct mips_cl_insn *ip)
3524{
3525 unsigned long pinfo, pinfo2;
3526 unsigned int mask;
3527
3528 mask = 0;
3529 pinfo = ip->insn_mo->pinfo;
3530 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3531 if (!mips_opts.mips16)
df58fc94
RS
3532 {
3533 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3534 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3535 if (pinfo & INSN_READ_FPR_S)
df58fc94 3536 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3537 if (pinfo & INSN_READ_FPR_T)
df58fc94 3538 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3539 if (pinfo & INSN_READ_FPR_R)
df58fc94 3540 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3541 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3542 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3543 }
3544 /* Conservatively treat all operands to an FP_D instruction are doubles.
3545 (This is overly pessimistic for things like cvt.d.s.) */
3546 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3547 mask |= mask << 1;
3548 return mask;
3549}
3550
3551/* Return the mask of floating-point registers that IP writes. */
3552
3553static unsigned int
3554fpr_write_mask (const struct mips_cl_insn *ip)
3555{
3556 unsigned long pinfo, pinfo2;
3557 unsigned int mask;
3558
3559 mask = 0;
3560 pinfo = ip->insn_mo->pinfo;
3561 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3562 if (!mips_opts.mips16)
4c260379
RS
3563 {
3564 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3565 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3566 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3567 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3568 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3569 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3570 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3571 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3572 }
3573 /* Conservatively treat all operands to an FP_D instruction are doubles.
3574 (This is overly pessimistic for things like cvt.s.d.) */
3575 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3576 mask |= mask << 1;
3577 return mask;
3578}
3579
71400594
RS
3580/* Classify an instruction according to the FIX_VR4120_* enumeration.
3581 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3582 by VR4120 errata. */
4d7206a2 3583
71400594
RS
3584static unsigned int
3585classify_vr4120_insn (const char *name)
252b5132 3586{
71400594
RS
3587 if (strncmp (name, "macc", 4) == 0)
3588 return FIX_VR4120_MACC;
3589 if (strncmp (name, "dmacc", 5) == 0)
3590 return FIX_VR4120_DMACC;
3591 if (strncmp (name, "mult", 4) == 0)
3592 return FIX_VR4120_MULT;
3593 if (strncmp (name, "dmult", 5) == 0)
3594 return FIX_VR4120_DMULT;
3595 if (strstr (name, "div"))
3596 return FIX_VR4120_DIV;
3597 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3598 return FIX_VR4120_MTHILO;
3599 return NUM_FIX_VR4120_CLASSES;
3600}
252b5132 3601
ff239038
CM
3602#define INSN_ERET 0x42000018
3603#define INSN_DERET 0x4200001f
3604
71400594
RS
3605/* Return the number of instructions that must separate INSN1 and INSN2,
3606 where INSN1 is the earlier instruction. Return the worst-case value
3607 for any INSN2 if INSN2 is null. */
252b5132 3608
71400594
RS
3609static unsigned int
3610insns_between (const struct mips_cl_insn *insn1,
3611 const struct mips_cl_insn *insn2)
3612{
3613 unsigned long pinfo1, pinfo2;
4c260379 3614 unsigned int mask;
71400594
RS
3615
3616 /* This function needs to know which pinfo flags are set for INSN2
3617 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3618 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3619 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3620 pinfo1 = insn1->insn_mo->pinfo;
3621 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3622
4c260379
RS
3623#define INSN2_USES_GPR(REG) \
3624 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3625
3626 /* For most targets, write-after-read dependencies on the HI and LO
3627 registers must be separated by at least two instructions. */
3628 if (!hilo_interlocks)
252b5132 3629 {
71400594
RS
3630 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3631 return 2;
3632 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3633 return 2;
3634 }
3635
3636 /* If we're working around r7000 errata, there must be two instructions
3637 between an mfhi or mflo and any instruction that uses the result. */
3638 if (mips_7000_hilo_fix
df58fc94 3639 && !mips_opts.micromips
71400594 3640 && MF_HILO_INSN (pinfo1)
df58fc94 3641 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3642 return 2;
3643
ff239038
CM
3644 /* If we're working around 24K errata, one instruction is required
3645 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3646 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3647 {
3648 if (insn1->insn_opcode == INSN_ERET
3649 || insn1->insn_opcode == INSN_DERET)
3650 {
3651 if (insn2 == NULL
3652 || insn2->insn_opcode == INSN_ERET
3653 || insn2->insn_opcode == INSN_DERET
11625dd8 3654 || delayed_branch_p (insn2))
ff239038
CM
3655 return 1;
3656 }
3657 }
3658
71400594
RS
3659 /* If working around VR4120 errata, check for combinations that need
3660 a single intervening instruction. */
df58fc94 3661 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3662 {
3663 unsigned int class1, class2;
252b5132 3664
71400594
RS
3665 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3666 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3667 {
71400594
RS
3668 if (insn2 == NULL)
3669 return 1;
3670 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3671 if (vr4120_conflicts[class1] & (1 << class2))
3672 return 1;
252b5132 3673 }
71400594
RS
3674 }
3675
df58fc94 3676 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3677 {
3678 /* Check for GPR or coprocessor load delays. All such delays
3679 are on the RT register. */
3680 /* Itbl support may require additional care here. */
3681 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3682 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3683 {
71400594 3684 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3685 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3686 return 1;
3687 }
3688
3689 /* Check for generic coprocessor hazards.
3690
3691 This case is not handled very well. There is no special
3692 knowledge of CP0 handling, and the coprocessors other than
3693 the floating point unit are not distinguished at all. */
3694 /* Itbl support may require additional care here. FIXME!
3695 Need to modify this to include knowledge about
3696 user specified delays! */
3697 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3698 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3699 {
3700 /* Handle cases where INSN1 writes to a known general coprocessor
3701 register. There must be a one instruction delay before INSN2
3702 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3703 mask = fpr_write_mask (insn1);
3704 if (mask != 0)
252b5132 3705 {
4c260379 3706 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3707 return 1;
252b5132
RH
3708 }
3709 else
3710 {
71400594
RS
3711 /* Read-after-write dependencies on the control registers
3712 require a two-instruction gap. */
3713 if ((pinfo1 & INSN_WRITE_COND_CODE)
3714 && (pinfo2 & INSN_READ_COND_CODE))
3715 return 2;
3716
3717 /* We don't know exactly what INSN1 does. If INSN2 is
3718 also a coprocessor instruction, assume there must be
3719 a one instruction gap. */
3720 if (pinfo2 & INSN_COP)
3721 return 1;
252b5132
RH
3722 }
3723 }
6b76fefe 3724
71400594
RS
3725 /* Check for read-after-write dependencies on the coprocessor
3726 control registers in cases where INSN1 does not need a general
3727 coprocessor delay. This means that INSN1 is a floating point
3728 comparison instruction. */
3729 /* Itbl support may require additional care here. */
3730 else if (!cop_interlocks
3731 && (pinfo1 & INSN_WRITE_COND_CODE)
3732 && (pinfo2 & INSN_READ_COND_CODE))
3733 return 1;
3734 }
6b76fefe 3735
4c260379 3736#undef INSN2_USES_GPR
6b76fefe 3737
71400594
RS
3738 return 0;
3739}
6b76fefe 3740
7d8e00cf
RS
3741/* Return the number of nops that would be needed to work around the
3742 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3743 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3744 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3745
3746static int
932d1a1b 3747nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3748 const struct mips_cl_insn *insn)
3749{
4c260379
RS
3750 int i, j;
3751 unsigned int mask;
7d8e00cf
RS
3752
3753 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3754 are not affected by the errata. */
3755 if (insn != 0
3756 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3757 || strcmp (insn->insn_mo->name, "mtlo") == 0
3758 || strcmp (insn->insn_mo->name, "mthi") == 0))
3759 return 0;
3760
3761 /* Search for the first MFLO or MFHI. */
3762 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3763 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3764 {
3765 /* Extract the destination register. */
4c260379 3766 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3767
3768 /* No nops are needed if INSN reads that register. */
4c260379 3769 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3770 return 0;
3771
3772 /* ...or if any of the intervening instructions do. */
3773 for (j = 0; j < i; j++)
4c260379 3774 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3775 return 0;
3776
932d1a1b
RS
3777 if (i >= ignore)
3778 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3779 }
3780 return 0;
3781}
3782
15be625d
CM
3783#define BASE_REG_EQ(INSN1, INSN2) \
3784 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3785 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3786
3787/* Return the minimum alignment for this store instruction. */
3788
3789static int
3790fix_24k_align_to (const struct mips_opcode *mo)
3791{
3792 if (strcmp (mo->name, "sh") == 0)
3793 return 2;
3794
3795 if (strcmp (mo->name, "swc1") == 0
3796 || strcmp (mo->name, "swc2") == 0
3797 || strcmp (mo->name, "sw") == 0
3798 || strcmp (mo->name, "sc") == 0
3799 || strcmp (mo->name, "s.s") == 0)
3800 return 4;
3801
3802 if (strcmp (mo->name, "sdc1") == 0
3803 || strcmp (mo->name, "sdc2") == 0
3804 || strcmp (mo->name, "s.d") == 0)
3805 return 8;
3806
3807 /* sb, swl, swr */
3808 return 1;
3809}
3810
3811struct fix_24k_store_info
3812 {
3813 /* Immediate offset, if any, for this store instruction. */
3814 short off;
3815 /* Alignment required by this store instruction. */
3816 int align_to;
3817 /* True for register offsets. */
3818 int register_offset;
3819 };
3820
3821/* Comparison function used by qsort. */
3822
3823static int
3824fix_24k_sort (const void *a, const void *b)
3825{
3826 const struct fix_24k_store_info *pos1 = a;
3827 const struct fix_24k_store_info *pos2 = b;
3828
3829 return (pos1->off - pos2->off);
3830}
3831
3832/* INSN is a store instruction. Try to record the store information
3833 in STINFO. Return false if the information isn't known. */
3834
3835static bfd_boolean
3836fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3837 const struct mips_cl_insn *insn)
15be625d
CM
3838{
3839 /* The instruction must have a known offset. */
3840 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3841 return FALSE;
3842
3843 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3844 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3845 return TRUE;
3846}
3847
932d1a1b
RS
3848/* Return the number of nops that would be needed to work around the 24k
3849 "lost data on stores during refill" errata if instruction INSN
3850 immediately followed the 2 instructions described by HIST.
3851 Ignore hazards that are contained within the first IGNORE
3852 instructions of HIST.
3853
3854 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3855 for the data cache refills and store data. The following describes
3856 the scenario where the store data could be lost.
3857
3858 * A data cache miss, due to either a load or a store, causing fill
3859 data to be supplied by the memory subsystem
3860 * The first three doublewords of fill data are returned and written
3861 into the cache
3862 * A sequence of four stores occurs in consecutive cycles around the
3863 final doubleword of the fill:
3864 * Store A
3865 * Store B
3866 * Store C
3867 * Zero, One or more instructions
3868 * Store D
3869
3870 The four stores A-D must be to different doublewords of the line that
3871 is being filled. The fourth instruction in the sequence above permits
3872 the fill of the final doubleword to be transferred from the FSB into
3873 the cache. In the sequence above, the stores may be either integer
3874 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3875 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3876 different doublewords on the line. If the floating point unit is
3877 running in 1:2 mode, it is not possible to create the sequence above
3878 using only floating point store instructions.
15be625d
CM
3879
3880 In this case, the cache line being filled is incorrectly marked
3881 invalid, thereby losing the data from any store to the line that
3882 occurs between the original miss and the completion of the five
3883 cycle sequence shown above.
3884
932d1a1b 3885 The workarounds are:
15be625d 3886
932d1a1b
RS
3887 * Run the data cache in write-through mode.
3888 * Insert a non-store instruction between
3889 Store A and Store B or Store B and Store C. */
15be625d
CM
3890
3891static int
932d1a1b 3892nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3893 const struct mips_cl_insn *insn)
3894{
3895 struct fix_24k_store_info pos[3];
3896 int align, i, base_offset;
3897
932d1a1b
RS
3898 if (ignore >= 2)
3899 return 0;
3900
ab9794cf
RS
3901 /* If the previous instruction wasn't a store, there's nothing to
3902 worry about. */
15be625d
CM
3903 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3904 return 0;
3905
ab9794cf
RS
3906 /* If the instructions after the previous one are unknown, we have
3907 to assume the worst. */
3908 if (!insn)
15be625d
CM
3909 return 1;
3910
ab9794cf
RS
3911 /* Check whether we are dealing with three consecutive stores. */
3912 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3913 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3914 return 0;
3915
3916 /* If we don't know the relationship between the store addresses,
3917 assume the worst. */
ab9794cf 3918 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3919 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3920 return 1;
3921
3922 if (!fix_24k_record_store_info (&pos[0], insn)
3923 || !fix_24k_record_store_info (&pos[1], &hist[0])
3924 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3925 return 1;
3926
3927 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3928
3929 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3930 X bytes and such that the base register + X is known to be aligned
3931 to align bytes. */
3932
3933 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3934 align = 8;
3935 else
3936 {
3937 align = pos[0].align_to;
3938 base_offset = pos[0].off;
3939 for (i = 1; i < 3; i++)
3940 if (align < pos[i].align_to)
3941 {
3942 align = pos[i].align_to;
3943 base_offset = pos[i].off;
3944 }
3945 for (i = 0; i < 3; i++)
3946 pos[i].off -= base_offset;
3947 }
3948
3949 pos[0].off &= ~align + 1;
3950 pos[1].off &= ~align + 1;
3951 pos[2].off &= ~align + 1;
3952
3953 /* If any two stores write to the same chunk, they also write to the
3954 same doubleword. The offsets are still sorted at this point. */
3955 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3956 return 0;
3957
3958 /* A range of at least 9 bytes is needed for the stores to be in
3959 non-overlapping doublewords. */
3960 if (pos[2].off - pos[0].off <= 8)
3961 return 0;
3962
3963 if (pos[2].off - pos[1].off >= 24
3964 || pos[1].off - pos[0].off >= 24
3965 || pos[2].off - pos[0].off >= 32)
3966 return 0;
3967
3968 return 1;
3969}
3970
71400594 3971/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3972 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3973 where HIST[0] is the most recent instruction. Ignore hazards
3974 between INSN and the first IGNORE instructions in HIST.
3975
3976 If INSN is null, return the worse-case number of nops for any
3977 instruction. */
bdaaa2e1 3978
71400594 3979static int
932d1a1b 3980nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3981 const struct mips_cl_insn *insn)
3982{
3983 int i, nops, tmp_nops;
bdaaa2e1 3984
71400594 3985 nops = 0;
932d1a1b 3986 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3987 {
91d6fa6a 3988 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3989 if (tmp_nops > nops)
3990 nops = tmp_nops;
3991 }
7d8e00cf 3992
df58fc94 3993 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3994 {
932d1a1b 3995 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3996 if (tmp_nops > nops)
3997 nops = tmp_nops;
3998 }
3999
df58fc94 4000 if (mips_fix_24k && !mips_opts.micromips)
15be625d 4001 {
932d1a1b 4002 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
4003 if (tmp_nops > nops)
4004 nops = tmp_nops;
4005 }
4006
71400594
RS
4007 return nops;
4008}
252b5132 4009
71400594 4010/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 4011 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
4012 would be needed after the extended sequence, ignoring hazards
4013 in the first IGNORE instructions. */
252b5132 4014
71400594 4015static int
932d1a1b
RS
4016nops_for_sequence (int num_insns, int ignore,
4017 const struct mips_cl_insn *hist, ...)
71400594
RS
4018{
4019 va_list args;
4020 struct mips_cl_insn buffer[MAX_NOPS];
4021 struct mips_cl_insn *cursor;
4022 int nops;
4023
91d6fa6a 4024 va_start (args, hist);
71400594 4025 cursor = buffer + num_insns;
91d6fa6a 4026 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
4027 while (cursor > buffer)
4028 *--cursor = *va_arg (args, const struct mips_cl_insn *);
4029
932d1a1b 4030 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
4031 va_end (args);
4032 return nops;
4033}
252b5132 4034
71400594
RS
4035/* Like nops_for_insn, but if INSN is a branch, take into account the
4036 worst-case delay for the branch target. */
252b5132 4037
71400594 4038static int
932d1a1b 4039nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
4040 const struct mips_cl_insn *insn)
4041{
4042 int nops, tmp_nops;
60b63b72 4043
932d1a1b 4044 nops = nops_for_insn (ignore, hist, insn);
11625dd8 4045 if (delayed_branch_p (insn))
71400594 4046 {
932d1a1b 4047 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 4048 hist, insn, get_delay_slot_nop (insn));
71400594
RS
4049 if (tmp_nops > nops)
4050 nops = tmp_nops;
4051 }
11625dd8 4052 else if (compact_branch_p (insn))
71400594 4053 {
932d1a1b 4054 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
4055 if (tmp_nops > nops)
4056 nops = tmp_nops;
4057 }
4058 return nops;
4059}
4060
c67a084a
NC
4061/* Fix NOP issue: Replace nops by "or at,at,zero". */
4062
4063static void
4064fix_loongson2f_nop (struct mips_cl_insn * ip)
4065{
df58fc94 4066 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
4067 if (strcmp (ip->insn_mo->name, "nop") == 0)
4068 ip->insn_opcode = LOONGSON2F_NOP_INSN;
4069}
4070
4071/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
4072 jr target pc &= 'hffff_ffff_cfff_ffff. */
4073
4074static void
4075fix_loongson2f_jump (struct mips_cl_insn * ip)
4076{
df58fc94 4077 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
4078 if (strcmp (ip->insn_mo->name, "j") == 0
4079 || strcmp (ip->insn_mo->name, "jr") == 0
4080 || strcmp (ip->insn_mo->name, "jalr") == 0)
4081 {
4082 int sreg;
4083 expressionS ep;
4084
4085 if (! mips_opts.at)
4086 return;
4087
df58fc94 4088 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
4089 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
4090 return;
4091
4092 ep.X_op = O_constant;
4093 ep.X_add_number = 0xcfff0000;
4094 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
4095 ep.X_add_number = 0xffff;
4096 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
4097 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
4098 }
4099}
4100
4101static void
4102fix_loongson2f (struct mips_cl_insn * ip)
4103{
4104 if (mips_fix_loongson2f_nop)
4105 fix_loongson2f_nop (ip);
4106
4107 if (mips_fix_loongson2f_jump)
4108 fix_loongson2f_jump (ip);
4109}
4110
a4e06468
RS
4111/* IP is a branch that has a delay slot, and we need to fill it
4112 automatically. Return true if we can do that by swapping IP
e407c74b
NC
4113 with the previous instruction.
4114 ADDRESS_EXPR is an operand of the instruction to be used with
4115 RELOC_TYPE. */
a4e06468
RS
4116
4117static bfd_boolean
e407c74b
NC
4118can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
4119 bfd_reloc_code_real_type *reloc_type)
a4e06468 4120{
2b0c8b40 4121 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
4122 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
4123
4124 /* -O2 and above is required for this optimization. */
4125 if (mips_optimize < 2)
4126 return FALSE;
4127
4128 /* If we have seen .set volatile or .set nomove, don't optimize. */
4129 if (mips_opts.nomove)
4130 return FALSE;
4131
4132 /* We can't swap if the previous instruction's position is fixed. */
4133 if (history[0].fixed_p)
4134 return FALSE;
4135
4136 /* If the previous previous insn was in a .set noreorder, we can't
4137 swap. Actually, the MIPS assembler will swap in this situation.
4138 However, gcc configured -with-gnu-as will generate code like
4139
4140 .set noreorder
4141 lw $4,XXX
4142 .set reorder
4143 INSN
4144 bne $4,$0,foo
4145
4146 in which we can not swap the bne and INSN. If gcc is not configured
4147 -with-gnu-as, it does not output the .set pseudo-ops. */
4148 if (history[1].noreorder_p)
4149 return FALSE;
4150
87333bb7
MR
4151 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
4152 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
4153 if (mips_opts.mips16 && history[0].fixp[0])
4154 return FALSE;
4155
4156 /* If the branch is itself the target of a branch, we can not swap.
4157 We cheat on this; all we check for is whether there is a label on
4158 this instruction. If there are any branches to anything other than
4159 a label, users must use .set noreorder. */
4160 if (seg_info (now_seg)->label_list)
4161 return FALSE;
4162
4163 /* If the previous instruction is in a variant frag other than this
2309ddf2 4164 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
4165 MIPS16 code, which uses variant frags for different purposes. */
4166 if (!mips_opts.mips16
a4e06468
RS
4167 && history[0].frag
4168 && history[0].frag->fr_type == rs_machine_dependent)
4169 return FALSE;
4170
bcd530a7
RS
4171 /* We do not swap with instructions that cannot architecturally
4172 be placed in a branch delay slot, such as SYNC or ERET. We
4173 also refrain from swapping with a trap instruction, since it
4174 complicates trap handlers to have the trap instruction be in
4175 a delay slot. */
a4e06468 4176 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 4177 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
4178 return FALSE;
4179
4180 /* Check for conflicts between the branch and the instructions
4181 before the candidate delay slot. */
4182 if (nops_for_insn (0, history + 1, ip) > 0)
4183 return FALSE;
4184
4185 /* Check for conflicts between the swapped sequence and the
4186 target of the branch. */
4187 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
4188 return FALSE;
4189
4190 /* If the branch reads a register that the previous
4191 instruction sets, we can not swap. */
4192 gpr_read = gpr_read_mask (ip);
4193 prev_gpr_write = gpr_write_mask (&history[0]);
4194 if (gpr_read & prev_gpr_write)
4195 return FALSE;
4196
4197 /* If the branch writes a register that the previous
4198 instruction sets, we can not swap. */
4199 gpr_write = gpr_write_mask (ip);
4200 if (gpr_write & prev_gpr_write)
4201 return FALSE;
4202
4203 /* If the branch writes a register that the previous
4204 instruction reads, we can not swap. */
4205 prev_gpr_read = gpr_read_mask (&history[0]);
4206 if (gpr_write & prev_gpr_read)
4207 return FALSE;
4208
4209 /* If one instruction sets a condition code and the
4210 other one uses a condition code, we can not swap. */
4211 pinfo = ip->insn_mo->pinfo;
4212 if ((pinfo & INSN_READ_COND_CODE)
4213 && (prev_pinfo & INSN_WRITE_COND_CODE))
4214 return FALSE;
4215 if ((pinfo & INSN_WRITE_COND_CODE)
4216 && (prev_pinfo & INSN_READ_COND_CODE))
4217 return FALSE;
4218
4219 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 4220 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
4221 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
4222 return FALSE;
2b0c8b40
MR
4223 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
4224 return FALSE;
a4e06468 4225
df58fc94
RS
4226 /* If the previous instruction has an incorrect size for a fixed
4227 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
4228 pinfo2 = ip->insn_mo->pinfo2;
4229 if (mips_opts.micromips
4230 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
4231 && insn_length (history) != 2)
4232 return FALSE;
4233 if (mips_opts.micromips
4234 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
4235 && insn_length (history) != 4)
4236 return FALSE;
4237
e407c74b
NC
4238 /* On R5900 short loops need to be fixed by inserting a nop in
4239 the branch delay slots.
4240 A short loop can be terminated too early. */
4241 if (mips_opts.arch == CPU_R5900
4242 /* Check if instruction has a parameter, ignore "j $31". */
4243 && (address_expr != NULL)
4244 /* Parameter must be 16 bit. */
4245 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
4246 /* Branch to same segment. */
4247 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
4248 /* Branch to same code fragment. */
4249 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
4250 /* Can only calculate branch offset if value is known. */
4251 && symbol_constant_p(address_expr->X_add_symbol)
4252 /* Check if branch is really conditional. */
4253 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
4254 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
4255 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
4256 {
4257 int distance;
4258 /* Check if loop is shorter than 6 instructions including
4259 branch and delay slot. */
4260 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
4261 if (distance <= 20)
4262 {
4263 int i;
4264 int rv;
4265
4266 rv = FALSE;
4267 /* When the loop includes branches or jumps,
4268 it is not a short loop. */
4269 for (i = 0; i < (distance / 4); i++)
4270 {
4271 if ((history[i].cleared_p)
4272 || delayed_branch_p(&history[i]))
4273 {
4274 rv = TRUE;
4275 break;
4276 }
4277 }
4278 if (rv == FALSE)
4279 {
4280 /* Insert nop after branch to fix short loop. */
4281 return FALSE;
4282 }
4283 }
4284 }
4285
a4e06468
RS
4286 return TRUE;
4287}
4288
e407c74b
NC
4289/* Decide how we should add IP to the instruction stream.
4290 ADDRESS_EXPR is an operand of the instruction to be used with
4291 RELOC_TYPE. */
a4e06468
RS
4292
4293static enum append_method
e407c74b
NC
4294get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
4295 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
4296{
4297 unsigned long pinfo;
4298
4299 /* The relaxed version of a macro sequence must be inherently
4300 hazard-free. */
4301 if (mips_relax.sequence == 2)
4302 return APPEND_ADD;
4303
4304 /* We must not dabble with instructions in a ".set norerorder" block. */
4305 if (mips_opts.noreorder)
4306 return APPEND_ADD;
4307
4308 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 4309 if (delayed_branch_p (ip))
a4e06468 4310 {
e407c74b
NC
4311 if (!branch_likely_p (ip)
4312 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
4313 return APPEND_SWAP;
4314
11625dd8 4315 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
4316 if (mips_opts.mips16
4317 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
4318 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
4319 return APPEND_ADD_COMPACT;
4320
4321 return APPEND_ADD_WITH_NOP;
4322 }
4323
a4e06468
RS
4324 return APPEND_ADD;
4325}
4326
ceb94aa5
RS
4327/* IP is a MIPS16 instruction whose opcode we have just changed.
4328 Point IP->insn_mo to the new opcode's definition. */
4329
4330static void
4331find_altered_mips16_opcode (struct mips_cl_insn *ip)
4332{
4333 const struct mips_opcode *mo, *end;
4334
4335 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4336 for (mo = ip->insn_mo; mo < end; mo++)
4337 if ((ip->insn_opcode & mo->mask) == mo->match)
4338 {
4339 ip->insn_mo = mo;
4340 return;
4341 }
4342 abort ();
4343}
4344
df58fc94
RS
4345/* For microMIPS macros, we need to generate a local number label
4346 as the target of branches. */
4347#define MICROMIPS_LABEL_CHAR '\037'
4348static unsigned long micromips_target_label;
4349static char micromips_target_name[32];
4350
4351static char *
4352micromips_label_name (void)
4353{
4354 char *p = micromips_target_name;
4355 char symbol_name_temporary[24];
4356 unsigned long l;
4357 int i;
4358
4359 if (*p)
4360 return p;
4361
4362 i = 0;
4363 l = micromips_target_label;
4364#ifdef LOCAL_LABEL_PREFIX
4365 *p++ = LOCAL_LABEL_PREFIX;
4366#endif
4367 *p++ = 'L';
4368 *p++ = MICROMIPS_LABEL_CHAR;
4369 do
4370 {
4371 symbol_name_temporary[i++] = l % 10 + '0';
4372 l /= 10;
4373 }
4374 while (l != 0);
4375 while (i > 0)
4376 *p++ = symbol_name_temporary[--i];
4377 *p = '\0';
4378
4379 return micromips_target_name;
4380}
4381
4382static void
4383micromips_label_expr (expressionS *label_expr)
4384{
4385 label_expr->X_op = O_symbol;
4386 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4387 label_expr->X_add_number = 0;
4388}
4389
4390static void
4391micromips_label_inc (void)
4392{
4393 micromips_target_label++;
4394 *micromips_target_name = '\0';
4395}
4396
4397static void
4398micromips_add_label (void)
4399{
4400 symbolS *s;
4401
4402 s = colon (micromips_label_name ());
4403 micromips_label_inc ();
f3ded42a 4404 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
4405}
4406
4407/* If assembling microMIPS code, then return the microMIPS reloc
4408 corresponding to the requested one if any. Otherwise return
4409 the reloc unchanged. */
4410
4411static bfd_reloc_code_real_type
4412micromips_map_reloc (bfd_reloc_code_real_type reloc)
4413{
4414 static const bfd_reloc_code_real_type relocs[][2] =
4415 {
4416 /* Keep sorted incrementally by the left-hand key. */
4417 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4418 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4419 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4420 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4421 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4422 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4423 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4424 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4425 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4426 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4427 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4428 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4429 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4430 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4431 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4432 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4433 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4434 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4435 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4436 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4437 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4438 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4439 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4440 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4441 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4442 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4443 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4444 };
4445 bfd_reloc_code_real_type r;
4446 size_t i;
4447
4448 if (!mips_opts.micromips)
4449 return reloc;
4450 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4451 {
4452 r = relocs[i][0];
4453 if (r > reloc)
4454 return reloc;
4455 if (r == reloc)
4456 return relocs[i][1];
4457 }
4458 return reloc;
4459}
4460
b886a2ab
RS
4461/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4462 Return true on success, storing the resolved value in RESULT. */
4463
4464static bfd_boolean
4465calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4466 offsetT *result)
4467{
4468 switch (reloc)
4469 {
4470 case BFD_RELOC_MIPS_HIGHEST:
4471 case BFD_RELOC_MICROMIPS_HIGHEST:
4472 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4473 return TRUE;
4474
4475 case BFD_RELOC_MIPS_HIGHER:
4476 case BFD_RELOC_MICROMIPS_HIGHER:
4477 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4478 return TRUE;
4479
4480 case BFD_RELOC_HI16_S:
4481 case BFD_RELOC_MICROMIPS_HI16_S:
4482 case BFD_RELOC_MIPS16_HI16_S:
4483 *result = ((operand + 0x8000) >> 16) & 0xffff;
4484 return TRUE;
4485
4486 case BFD_RELOC_HI16:
4487 case BFD_RELOC_MICROMIPS_HI16:
4488 case BFD_RELOC_MIPS16_HI16:
4489 *result = (operand >> 16) & 0xffff;
4490 return TRUE;
4491
4492 case BFD_RELOC_LO16:
4493 case BFD_RELOC_MICROMIPS_LO16:
4494 case BFD_RELOC_MIPS16_LO16:
4495 *result = operand & 0xffff;
4496 return TRUE;
4497
4498 case BFD_RELOC_UNUSED:
4499 *result = operand;
4500 return TRUE;
4501
4502 default:
4503 return FALSE;
4504 }
4505}
4506
71400594
RS
4507/* Output an instruction. IP is the instruction information.
4508 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4509 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4510 a macro expansion. */
71400594
RS
4511
4512static void
4513append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4514 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4515{
14fe068b 4516 unsigned long prev_pinfo2, pinfo;
71400594 4517 bfd_boolean relaxed_branch = FALSE;
a4e06468 4518 enum append_method method;
2309ddf2 4519 bfd_boolean relax32;
2b0c8b40 4520 int branch_disp;
71400594 4521
2309ddf2 4522 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4523 fix_loongson2f (ip);
4524
738f4d98 4525 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4526 file_ase_micromips |= mips_opts.micromips;
738f4d98 4527
df58fc94 4528 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4529 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4530
4531 if (mips_opts.micromips
4532 && !expansionp
4533 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4534 && micromips_insn_length (ip->insn_mo) != 2)
4535 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4536 && micromips_insn_length (ip->insn_mo) != 4)))
4537 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4538 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4539
15be625d
CM
4540 if (address_expr == NULL)
4541 ip->complete_p = 1;
b886a2ab
RS
4542 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4543 && reloc_type[1] == BFD_RELOC_UNUSED
4544 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4545 && address_expr->X_op == O_constant)
4546 {
15be625d
CM
4547 switch (*reloc_type)
4548 {
15be625d 4549 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4550 {
4551 int shift;
4552
4553 shift = mips_opts.micromips ? 1 : 2;
4554 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4555 as_bad (_("jump to misaligned address (0x%lx)"),
4556 (unsigned long) address_expr->X_add_number);
4557 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4558 & 0x3ffffff);
335574df 4559 ip->complete_p = 1;
df58fc94 4560 }
15be625d
CM
4561 break;
4562
4563 case BFD_RELOC_MIPS16_JMP:
4564 if ((address_expr->X_add_number & 3) != 0)
4565 as_bad (_("jump to misaligned address (0x%lx)"),
4566 (unsigned long) address_expr->X_add_number);
4567 ip->insn_opcode |=
4568 (((address_expr->X_add_number & 0x7c0000) << 3)
4569 | ((address_expr->X_add_number & 0xf800000) >> 7)
4570 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4571 ip->complete_p = 1;
15be625d
CM
4572 break;
4573
4574 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4575 {
4576 int shift;
4577
4578 shift = mips_opts.micromips ? 1 : 2;
4579 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4580 as_bad (_("branch to misaligned address (0x%lx)"),
4581 (unsigned long) address_expr->X_add_number);
4582 if (!mips_relax_branch)
4583 {
4584 if ((address_expr->X_add_number + (1 << (shift + 15)))
4585 & ~((1 << (shift + 16)) - 1))
4586 as_bad (_("branch address range overflow (0x%lx)"),
4587 (unsigned long) address_expr->X_add_number);
4588 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4589 & 0xffff);
4590 }
df58fc94 4591 }
15be625d
CM
4592 break;
4593
4594 default:
b886a2ab
RS
4595 {
4596 offsetT value;
4597
4598 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4599 &value))
4600 {
4601 ip->insn_opcode |= value & 0xffff;
4602 ip->complete_p = 1;
4603 }
4604 }
4605 break;
4606 }
15be625d
CM
4607 }
4608
71400594
RS
4609 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4610 {
4611 /* There are a lot of optimizations we could do that we don't.
4612 In particular, we do not, in general, reorder instructions.
4613 If you use gcc with optimization, it will reorder
4614 instructions and generally do much more optimization then we
4615 do here; repeating all that work in the assembler would only
4616 benefit hand written assembly code, and does not seem worth
4617 it. */
4618 int nops = (mips_optimize == 0
932d1a1b
RS
4619 ? nops_for_insn (0, history, NULL)
4620 : nops_for_insn_or_target (0, history, ip));
71400594 4621 if (nops > 0)
252b5132
RH
4622 {
4623 fragS *old_frag;
4624 unsigned long old_frag_offset;
4625 int i;
252b5132
RH
4626
4627 old_frag = frag_now;
4628 old_frag_offset = frag_now_fix ();
4629
4630 for (i = 0; i < nops; i++)
14fe068b
RS
4631 add_fixed_insn (NOP_INSN);
4632 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4633
4634 if (listing)
4635 {
4636 listing_prev_line ();
4637 /* We may be at the start of a variant frag. In case we
4638 are, make sure there is enough space for the frag
4639 after the frags created by listing_prev_line. The
4640 argument to frag_grow here must be at least as large
4641 as the argument to all other calls to frag_grow in
4642 this file. We don't have to worry about being in the
4643 middle of a variant frag, because the variants insert
4644 all needed nop instructions themselves. */
4645 frag_grow (40);
4646 }
4647
462427c4 4648 mips_move_text_labels ();
252b5132
RH
4649
4650#ifndef NO_ECOFF_DEBUGGING
4651 if (ECOFF_DEBUGGING)
4652 ecoff_fix_loc (old_frag, old_frag_offset);
4653#endif
4654 }
71400594
RS
4655 }
4656 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4657 {
932d1a1b
RS
4658 int nops;
4659
4660 /* Work out how many nops in prev_nop_frag are needed by IP,
4661 ignoring hazards generated by the first prev_nop_frag_since
4662 instructions. */
4663 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4664 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4665
71400594
RS
4666 /* Enforce NOPS as a minimum. */
4667 if (nops > prev_nop_frag_required)
4668 prev_nop_frag_required = nops;
252b5132 4669
71400594
RS
4670 if (prev_nop_frag_holds == prev_nop_frag_required)
4671 {
4672 /* Settle for the current number of nops. Update the history
4673 accordingly (for the benefit of any future .set reorder code). */
4674 prev_nop_frag = NULL;
4675 insert_into_history (prev_nop_frag_since,
4676 prev_nop_frag_holds, NOP_INSN);
4677 }
4678 else
4679 {
4680 /* Allow this instruction to replace one of the nops that was
4681 tentatively added to prev_nop_frag. */
df58fc94 4682 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4683 prev_nop_frag_holds--;
4684 prev_nop_frag_since++;
252b5132
RH
4685 }
4686 }
4687
e407c74b 4688 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4689 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4690
e410add4
RS
4691 dwarf2_emit_insn (0);
4692 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4693 so "move" the instruction address accordingly.
4694
4695 Also, it doesn't seem appropriate for the assembler to reorder .loc
4696 entries. If this instruction is a branch that we are going to swap
4697 with the previous instruction, the two instructions should be
4698 treated as a unit, and the debug information for both instructions
4699 should refer to the start of the branch sequence. Using the
4700 current position is certainly wrong when swapping a 32-bit branch
4701 and a 16-bit delay slot, since the current position would then be
4702 in the middle of a branch. */
4703 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 4704
df58fc94
RS
4705 relax32 = (mips_relax_branch
4706 /* Don't try branch relaxation within .set nomacro, or within
4707 .set noat if we use $at for PIC computations. If it turns
4708 out that the branch was out-of-range, we'll get an error. */
4709 && !mips_opts.warn_about_macros
4710 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
4711 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4712 as they have no complementing branches. */
4713 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
4714
4715 if (!HAVE_CODE_COMPRESSION
4716 && address_expr
4717 && relax32
0b25d3e6 4718 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4719 && delayed_branch_p (ip))
4a6a3df4 4720 {
895921c9 4721 relaxed_branch = TRUE;
1e915849
RS
4722 add_relaxed_insn (ip, (relaxed_branch_length
4723 (NULL, NULL,
11625dd8
RS
4724 uncond_branch_p (ip) ? -1
4725 : branch_likely_p (ip) ? 1
1e915849
RS
4726 : 0)), 4,
4727 RELAX_BRANCH_ENCODE
66b3e8da 4728 (AT,
11625dd8
RS
4729 uncond_branch_p (ip),
4730 branch_likely_p (ip),
1e915849
RS
4731 pinfo & INSN_WRITE_GPR_31,
4732 0),
4733 address_expr->X_add_symbol,
4734 address_expr->X_add_number);
4a6a3df4
AO
4735 *reloc_type = BFD_RELOC_UNUSED;
4736 }
df58fc94
RS
4737 else if (mips_opts.micromips
4738 && address_expr
4739 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4740 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4741 && (delayed_branch_p (ip) || compact_branch_p (ip))
4742 /* Don't try branch relaxation when users specify
4743 16-bit/32-bit instructions. */
4744 && !forced_insn_length)
df58fc94
RS
4745 {
4746 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4747 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4748 int uncond = uncond_branch_p (ip) ? -1 : 0;
4749 int compact = compact_branch_p (ip);
df58fc94
RS
4750 int al = pinfo & INSN_WRITE_GPR_31;
4751 int length32;
4752
4753 gas_assert (address_expr != NULL);
4754 gas_assert (!mips_relax.sequence);
4755
2b0c8b40 4756 relaxed_branch = TRUE;
df58fc94
RS
4757 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4758 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4759 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4760 relax32, 0, 0),
df58fc94
RS
4761 address_expr->X_add_symbol,
4762 address_expr->X_add_number);
4763 *reloc_type = BFD_RELOC_UNUSED;
4764 }
4765 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4766 {
4767 /* We need to set up a variant frag. */
df58fc94 4768 gas_assert (address_expr != NULL);
1e915849
RS
4769 add_relaxed_insn (ip, 4, 0,
4770 RELAX_MIPS16_ENCODE
4771 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4772 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4773 delayed_branch_p (&history[0]),
1e915849
RS
4774 history[0].mips16_absolute_jump_p),
4775 make_expr_symbol (address_expr), 0);
252b5132 4776 }
5c04167a 4777 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4778 {
11625dd8 4779 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4780 /* Make sure there is enough room to swap this instruction with
4781 a following jump instruction. */
4782 frag_grow (6);
1e915849 4783 add_fixed_insn (ip);
252b5132
RH
4784 }
4785 else
4786 {
4787 if (mips_opts.mips16
4788 && mips_opts.noreorder
11625dd8 4789 && delayed_branch_p (&history[0]))
252b5132
RH
4790 as_warn (_("extended instruction in delay slot"));
4791
4d7206a2
RS
4792 if (mips_relax.sequence)
4793 {
4794 /* If we've reached the end of this frag, turn it into a variant
4795 frag and record the information for the instructions we've
4796 written so far. */
4797 if (frag_room () < 4)
4798 relax_close_frag ();
df58fc94 4799 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4800 }
4801
584892a6 4802 if (mips_relax.sequence != 2)
df58fc94
RS
4803 {
4804 if (mips_macro_warning.first_insn_sizes[0] == 0)
4805 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4806 mips_macro_warning.sizes[0] += insn_length (ip);
4807 mips_macro_warning.insns[0]++;
4808 }
584892a6 4809 if (mips_relax.sequence != 1)
df58fc94
RS
4810 {
4811 if (mips_macro_warning.first_insn_sizes[1] == 0)
4812 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4813 mips_macro_warning.sizes[1] += insn_length (ip);
4814 mips_macro_warning.insns[1]++;
4815 }
584892a6 4816
1e915849
RS
4817 if (mips_opts.mips16)
4818 {
4819 ip->fixed_p = 1;
4820 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4821 }
4822 add_fixed_insn (ip);
252b5132
RH
4823 }
4824
9fe77896 4825 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4826 {
df58fc94 4827 bfd_reloc_code_real_type final_type[3];
2309ddf2 4828 reloc_howto_type *howto0;
9fe77896
RS
4829 reloc_howto_type *howto;
4830 int i;
34ce925e 4831
df58fc94
RS
4832 /* Perform any necessary conversion to microMIPS relocations
4833 and find out how many relocations there actually are. */
4834 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4835 final_type[i] = micromips_map_reloc (reloc_type[i]);
4836
9fe77896
RS
4837 /* In a compound relocation, it is the final (outermost)
4838 operator that determines the relocated field. */
2309ddf2 4839 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
4840 if (!howto)
4841 abort ();
2309ddf2
MR
4842
4843 if (i > 1)
4844 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4845 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4846 bfd_get_reloc_size (howto),
4847 address_expr,
2309ddf2
MR
4848 howto0 && howto0->pc_relative,
4849 final_type[0]);
9fe77896
RS
4850
4851 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4852 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4853 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4854
4855 /* These relocations can have an addend that won't fit in
4856 4 octets for 64bit assembly. */
4857 if (HAVE_64BIT_GPRS
4858 && ! howto->partial_inplace
4859 && (reloc_type[0] == BFD_RELOC_16
4860 || reloc_type[0] == BFD_RELOC_32
4861 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4862 || reloc_type[0] == BFD_RELOC_GPREL16
4863 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4864 || reloc_type[0] == BFD_RELOC_GPREL32
4865 || reloc_type[0] == BFD_RELOC_64
4866 || reloc_type[0] == BFD_RELOC_CTOR
4867 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4868 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4869 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4870 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4871 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4872 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4873 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4874 || hi16_reloc_p (reloc_type[0])
4875 || lo16_reloc_p (reloc_type[0])))
4876 ip->fixp[0]->fx_no_overflow = 1;
4877
ddaf2c41
MR
4878 /* These relocations can have an addend that won't fit in 2 octets. */
4879 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4880 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4881 ip->fixp[0]->fx_no_overflow = 1;
4882
9fe77896
RS
4883 if (mips_relax.sequence)
4884 {
4885 if (mips_relax.first_fixup == 0)
4886 mips_relax.first_fixup = ip->fixp[0];
4887 }
4888 else if (reloc_needs_lo_p (*reloc_type))
4889 {
4890 struct mips_hi_fixup *hi_fixup;
4891
4892 /* Reuse the last entry if it already has a matching %lo. */
4893 hi_fixup = mips_hi_fixup_list;
4894 if (hi_fixup == 0
4895 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4896 {
9fe77896
RS
4897 hi_fixup = ((struct mips_hi_fixup *)
4898 xmalloc (sizeof (struct mips_hi_fixup)));
4899 hi_fixup->next = mips_hi_fixup_list;
4900 mips_hi_fixup_list = hi_fixup;
4d7206a2 4901 }
9fe77896
RS
4902 hi_fixup->fixp = ip->fixp[0];
4903 hi_fixup->seg = now_seg;
4904 }
252b5132 4905
9fe77896
RS
4906 /* Add fixups for the second and third relocations, if given.
4907 Note that the ABI allows the second relocation to be
4908 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4909 moment we only use RSS_UNDEF, but we could add support
4910 for the others if it ever becomes necessary. */
4911 for (i = 1; i < 3; i++)
4912 if (reloc_type[i] != BFD_RELOC_UNUSED)
4913 {
4914 ip->fixp[i] = fix_new (ip->frag, ip->where,
4915 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4916 FALSE, final_type[i]);
f6688943 4917
9fe77896
RS
4918 /* Use fx_tcbit to mark compound relocs. */
4919 ip->fixp[0]->fx_tcbit = 1;
4920 ip->fixp[i]->fx_tcbit = 1;
4921 }
252b5132 4922 }
1e915849 4923 install_insn (ip);
252b5132
RH
4924
4925 /* Update the register mask information. */
4c260379
RS
4926 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4927 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4928
a4e06468 4929 switch (method)
252b5132 4930 {
a4e06468
RS
4931 case APPEND_ADD:
4932 insert_into_history (0, 1, ip);
4933 break;
4934
4935 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4936 {
4937 struct mips_cl_insn *nop;
4938
4939 insert_into_history (0, 1, ip);
4940 nop = get_delay_slot_nop (ip);
4941 add_fixed_insn (nop);
4942 insert_into_history (0, 1, nop);
4943 if (mips_relax.sequence)
4944 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4945 }
a4e06468
RS
4946 break;
4947
4948 case APPEND_ADD_COMPACT:
4949 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4950 gas_assert (mips_opts.mips16);
4951 ip->insn_opcode |= 0x0080;
4952 find_altered_mips16_opcode (ip);
4953 install_insn (ip);
4954 insert_into_history (0, 1, ip);
4955 break;
4956
4957 case APPEND_SWAP:
4958 {
4959 struct mips_cl_insn delay = history[0];
4960 if (mips_opts.mips16)
4961 {
4962 know (delay.frag == ip->frag);
4963 move_insn (ip, delay.frag, delay.where);
4964 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4965 }
464ab0e5 4966 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4967 {
4968 /* Add the delay slot instruction to the end of the
4969 current frag and shrink the fixed part of the
4970 original frag. If the branch occupies the tail of
4971 the latter, move it backwards to cover the gap. */
2b0c8b40 4972 delay.frag->fr_fix -= branch_disp;
a4e06468 4973 if (delay.frag == ip->frag)
2b0c8b40 4974 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4975 add_fixed_insn (&delay);
4976 }
4977 else
4978 {
2b0c8b40
MR
4979 move_insn (&delay, ip->frag,
4980 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4981 move_insn (ip, history[0].frag, history[0].where);
4982 }
4983 history[0] = *ip;
4984 delay.fixed_p = 1;
4985 insert_into_history (0, 1, &delay);
4986 }
4987 break;
252b5132
RH
4988 }
4989
13408f1e 4990 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4991 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4992 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4993 {
4994 unsigned int i;
4995
79850f26 4996 mips_no_prev_insn ();
13408f1e 4997
e407c74b 4998 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 4999 history[i].cleared_p = 1;
e407c74b
NC
5000 }
5001
df58fc94
RS
5002 /* We need to emit a label at the end of branch-likely macros. */
5003 if (emit_branch_likely_macro)
5004 {
5005 emit_branch_likely_macro = FALSE;
5006 micromips_add_label ();
5007 }
5008
252b5132
RH
5009 /* We just output an insn, so the next one doesn't have a label. */
5010 mips_clear_insn_labels ();
252b5132
RH
5011}
5012
e407c74b
NC
5013/* Forget that there was any previous instruction or label.
5014 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
5015
5016static void
7d10b47d 5017mips_no_prev_insn (void)
252b5132 5018{
7d10b47d
RS
5019 prev_nop_frag = NULL;
5020 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
5021 mips_clear_insn_labels ();
5022}
5023
7d10b47d
RS
5024/* This function must be called before we emit something other than
5025 instructions. It is like mips_no_prev_insn except that it inserts
5026 any NOPS that might be needed by previous instructions. */
252b5132 5027
7d10b47d
RS
5028void
5029mips_emit_delays (void)
252b5132
RH
5030{
5031 if (! mips_opts.noreorder)
5032 {
932d1a1b 5033 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
5034 if (nops > 0)
5035 {
7d10b47d
RS
5036 while (nops-- > 0)
5037 add_fixed_insn (NOP_INSN);
462427c4 5038 mips_move_text_labels ();
7d10b47d
RS
5039 }
5040 }
5041 mips_no_prev_insn ();
5042}
5043
5044/* Start a (possibly nested) noreorder block. */
5045
5046static void
5047start_noreorder (void)
5048{
5049 if (mips_opts.noreorder == 0)
5050 {
5051 unsigned int i;
5052 int nops;
5053
5054 /* None of the instructions before the .set noreorder can be moved. */
5055 for (i = 0; i < ARRAY_SIZE (history); i++)
5056 history[i].fixed_p = 1;
5057
5058 /* Insert any nops that might be needed between the .set noreorder
5059 block and the previous instructions. We will later remove any
5060 nops that turn out not to be needed. */
932d1a1b 5061 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
5062 if (nops > 0)
5063 {
5064 if (mips_optimize != 0)
252b5132
RH
5065 {
5066 /* Record the frag which holds the nop instructions, so
5067 that we can remove them if we don't need them. */
df58fc94 5068 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
5069 prev_nop_frag = frag_now;
5070 prev_nop_frag_holds = nops;
5071 prev_nop_frag_required = 0;
5072 prev_nop_frag_since = 0;
5073 }
5074
5075 for (; nops > 0; --nops)
1e915849 5076 add_fixed_insn (NOP_INSN);
252b5132 5077
7d10b47d
RS
5078 /* Move on to a new frag, so that it is safe to simply
5079 decrease the size of prev_nop_frag. */
5080 frag_wane (frag_now);
5081 frag_new (0);
462427c4 5082 mips_move_text_labels ();
252b5132 5083 }
df58fc94 5084 mips_mark_labels ();
7d10b47d 5085 mips_clear_insn_labels ();
252b5132 5086 }
7d10b47d
RS
5087 mips_opts.noreorder++;
5088 mips_any_noreorder = 1;
5089}
252b5132 5090
7d10b47d 5091/* End a nested noreorder block. */
252b5132 5092
7d10b47d
RS
5093static void
5094end_noreorder (void)
5095{
5096 mips_opts.noreorder--;
5097 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
5098 {
5099 /* Commit to inserting prev_nop_frag_required nops and go back to
5100 handling nop insertion the .set reorder way. */
5101 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 5102 * NOP_INSN_SIZE);
7d10b47d
RS
5103 insert_into_history (prev_nop_frag_since,
5104 prev_nop_frag_required, NOP_INSN);
5105 prev_nop_frag = NULL;
5106 }
252b5132
RH
5107}
5108
584892a6
RS
5109/* Set up global variables for the start of a new macro. */
5110
5111static void
5112macro_start (void)
5113{
5114 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
5115 memset (&mips_macro_warning.first_insn_sizes, 0,
5116 sizeof (mips_macro_warning.first_insn_sizes));
5117 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 5118 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 5119 && delayed_branch_p (&history[0]));
df58fc94
RS
5120 switch (history[0].insn_mo->pinfo2
5121 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
5122 {
5123 case INSN2_BRANCH_DELAY_32BIT:
5124 mips_macro_warning.delay_slot_length = 4;
5125 break;
5126 case INSN2_BRANCH_DELAY_16BIT:
5127 mips_macro_warning.delay_slot_length = 2;
5128 break;
5129 default:
5130 mips_macro_warning.delay_slot_length = 0;
5131 break;
5132 }
5133 mips_macro_warning.first_frag = NULL;
584892a6
RS
5134}
5135
df58fc94
RS
5136/* Given that a macro is longer than one instruction or of the wrong size,
5137 return the appropriate warning for it. Return null if no warning is
5138 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
5139 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
5140 and RELAX_NOMACRO. */
584892a6
RS
5141
5142static const char *
5143macro_warning (relax_substateT subtype)
5144{
5145 if (subtype & RELAX_DELAY_SLOT)
5146 return _("Macro instruction expanded into multiple instructions"
5147 " in a branch delay slot");
5148 else if (subtype & RELAX_NOMACRO)
5149 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
5150 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
5151 | RELAX_DELAY_SLOT_SIZE_SECOND))
5152 return ((subtype & RELAX_DELAY_SLOT_16BIT)
5153 ? _("Macro instruction expanded into a wrong size instruction"
5154 " in a 16-bit branch delay slot")
5155 : _("Macro instruction expanded into a wrong size instruction"
5156 " in a 32-bit branch delay slot"));
584892a6
RS
5157 else
5158 return 0;
5159}
5160
5161/* Finish up a macro. Emit warnings as appropriate. */
5162
5163static void
5164macro_end (void)
5165{
df58fc94
RS
5166 /* Relaxation warning flags. */
5167 relax_substateT subtype = 0;
5168
5169 /* Check delay slot size requirements. */
5170 if (mips_macro_warning.delay_slot_length == 2)
5171 subtype |= RELAX_DELAY_SLOT_16BIT;
5172 if (mips_macro_warning.delay_slot_length != 0)
584892a6 5173 {
df58fc94
RS
5174 if (mips_macro_warning.delay_slot_length
5175 != mips_macro_warning.first_insn_sizes[0])
5176 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
5177 if (mips_macro_warning.delay_slot_length
5178 != mips_macro_warning.first_insn_sizes[1])
5179 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
5180 }
584892a6 5181
df58fc94
RS
5182 /* Check instruction count requirements. */
5183 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
5184 {
5185 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
5186 subtype |= RELAX_SECOND_LONGER;
5187 if (mips_opts.warn_about_macros)
5188 subtype |= RELAX_NOMACRO;
5189 if (mips_macro_warning.delay_slot_p)
5190 subtype |= RELAX_DELAY_SLOT;
df58fc94 5191 }
584892a6 5192
df58fc94
RS
5193 /* If both alternatives fail to fill a delay slot correctly,
5194 emit the warning now. */
5195 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
5196 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
5197 {
5198 relax_substateT s;
5199 const char *msg;
5200
5201 s = subtype & (RELAX_DELAY_SLOT_16BIT
5202 | RELAX_DELAY_SLOT_SIZE_FIRST
5203 | RELAX_DELAY_SLOT_SIZE_SECOND);
5204 msg = macro_warning (s);
5205 if (msg != NULL)
5206 as_warn ("%s", msg);
5207 subtype &= ~s;
5208 }
5209
5210 /* If both implementations are longer than 1 instruction, then emit the
5211 warning now. */
5212 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
5213 {
5214 relax_substateT s;
5215 const char *msg;
5216
5217 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
5218 msg = macro_warning (s);
5219 if (msg != NULL)
5220 as_warn ("%s", msg);
5221 subtype &= ~s;
584892a6 5222 }
df58fc94
RS
5223
5224 /* If any flags still set, then one implementation might need a warning
5225 and the other either will need one of a different kind or none at all.
5226 Pass any remaining flags over to relaxation. */
5227 if (mips_macro_warning.first_frag != NULL)
5228 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
5229}
5230
df58fc94
RS
5231/* Instruction operand formats used in macros that vary between
5232 standard MIPS and microMIPS code. */
5233
833794fc 5234static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
5235static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
5236static const char * const jalr_fmt[2] = { "d,s", "t,s" };
5237static const char * const lui_fmt[2] = { "t,u", "s,u" };
5238static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 5239static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
5240static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
5241static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
5242
833794fc 5243#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
5244#define COP12_FMT (cop12_fmt[mips_opts.micromips])
5245#define JALR_FMT (jalr_fmt[mips_opts.micromips])
5246#define LUI_FMT (lui_fmt[mips_opts.micromips])
5247#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 5248#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
5249#define SHFT_FMT (shft_fmt[mips_opts.micromips])
5250#define TRAP_FMT (trap_fmt[mips_opts.micromips])
5251
6e1304d8
RS
5252/* Read a macro's relocation codes from *ARGS and store them in *R.
5253 The first argument in *ARGS will be either the code for a single
5254 relocation or -1 followed by the three codes that make up a
5255 composite relocation. */
5256
5257static void
5258macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
5259{
5260 int i, next;
5261
5262 next = va_arg (*args, int);
5263 if (next >= 0)
5264 r[0] = (bfd_reloc_code_real_type) next;
5265 else
5266 for (i = 0; i < 3; i++)
5267 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
5268}
5269
252b5132
RH
5270/* Build an instruction created by a macro expansion. This is passed
5271 a pointer to the count of instructions created so far, an
5272 expression, the name of the instruction to build, an operand format
5273 string, and corresponding arguments. */
5274
252b5132 5275static void
67c0d1eb 5276macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 5277{
df58fc94 5278 const struct mips_opcode *mo = NULL;
f6688943 5279 bfd_reloc_code_real_type r[3];
df58fc94
RS
5280 const struct mips_opcode *amo;
5281 struct hash_control *hash;
5282 struct mips_cl_insn insn;
252b5132 5283 va_list args;
252b5132 5284
252b5132 5285 va_start (args, fmt);
252b5132 5286
252b5132
RH
5287 if (mips_opts.mips16)
5288 {
03ea81db 5289 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
5290 va_end (args);
5291 return;
5292 }
5293
f6688943
TS
5294 r[0] = BFD_RELOC_UNUSED;
5295 r[1] = BFD_RELOC_UNUSED;
5296 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
5297 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
5298 amo = (struct mips_opcode *) hash_find (hash, name);
5299 gas_assert (amo);
5300 gas_assert (strcmp (name, amo->name) == 0);
1e915849 5301
df58fc94 5302 do
8b082fb1
TS
5303 {
5304 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
5305 macros will never generate MDMX, MIPS-3D, or MT instructions.
5306 We try to match an instruction that fulfils the branch delay
5307 slot instruction length requirement (if any) of the previous
5308 instruction. While doing this we record the first instruction
5309 seen that matches all the other conditions and use it anyway
5310 if the requirement cannot be met; we will issue an appropriate
5311 warning later on. */
5312 if (strcmp (fmt, amo->args) == 0
5313 && amo->pinfo != INSN_MACRO
5314 && is_opcode_valid (amo)
5315 && is_size_valid (amo))
5316 {
5317 if (is_delay_slot_valid (amo))
5318 {
5319 mo = amo;
5320 break;
5321 }
5322 else if (!mo)
5323 mo = amo;
5324 }
8b082fb1 5325
df58fc94
RS
5326 ++amo;
5327 gas_assert (amo->name);
252b5132 5328 }
df58fc94 5329 while (strcmp (name, amo->name) == 0);
252b5132 5330
df58fc94 5331 gas_assert (mo);
1e915849 5332 create_insn (&insn, mo);
252b5132
RH
5333 for (;;)
5334 {
5335 switch (*fmt++)
5336 {
5337 case '\0':
5338 break;
5339
5340 case ',':
5341 case '(':
5342 case ')':
5343 continue;
5344
5f74bc13
CD
5345 case '+':
5346 switch (*fmt++)
5347 {
5348 case 'A':
5349 case 'E':
df58fc94
RS
5350 INSERT_OPERAND (mips_opts.micromips,
5351 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5352 continue;
5353
5354 case 'B':
5355 case 'F':
5356 /* Note that in the macro case, these arguments are already
5357 in MSB form. (When handling the instruction in the
5358 non-macro case, these arguments are sizes from which
5359 MSB values must be calculated.) */
df58fc94
RS
5360 INSERT_OPERAND (mips_opts.micromips,
5361 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5362 continue;
5363
b015e599
AP
5364 case 'J':
5365 gas_assert (!mips_opts.micromips);
5366 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5367 continue;
5368
5f74bc13
CD
5369 case 'C':
5370 case 'G':
5371 case 'H':
5372 /* Note that in the macro case, these arguments are already
5373 in MSBD form. (When handling the instruction in the
5374 non-macro case, these arguments are sizes from which
5375 MSBD values must be calculated.) */
df58fc94
RS
5376 INSERT_OPERAND (mips_opts.micromips,
5377 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5378 continue;
5379
dd3cbb7e 5380 case 'Q':
df58fc94
RS
5381 gas_assert (!mips_opts.micromips);
5382 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5383 continue;
5384
7f3c4072
CM
5385 case 'j':
5386 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5387 continue;
5388
5f74bc13 5389 default:
b37df7c4 5390 abort ();
5f74bc13
CD
5391 }
5392 continue;
5393
8b082fb1 5394 case '2':
03f66e8a 5395 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5396 continue;
5397
df58fc94
RS
5398 case 'n':
5399 gas_assert (mips_opts.micromips);
252b5132
RH
5400 case 't':
5401 case 'w':
5402 case 'E':
df58fc94 5403 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5404 continue;
5405
5406 case 'c':
833794fc 5407 INSERT_OPERAND (mips_opts.micromips, CODE, insn, va_arg (args, int));
38487616
TS
5408 continue;
5409
252b5132 5410 case 'W':
df58fc94
RS
5411 gas_assert (!mips_opts.micromips);
5412 case 'T':
5413 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5414 continue;
5415
252b5132 5416 case 'G':
df58fc94
RS
5417 if (mips_opts.micromips)
5418 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5419 else
5420 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5421 continue;
5422
af7ee8bf 5423 case 'K':
df58fc94
RS
5424 gas_assert (!mips_opts.micromips);
5425 case 'd':
5426 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5427 continue;
5428
4372b673 5429 case 'U':
df58fc94 5430 gas_assert (!mips_opts.micromips);
4372b673
NC
5431 {
5432 int tmp = va_arg (args, int);
5433
df58fc94
RS
5434 INSERT_OPERAND (0, RT, insn, tmp);
5435 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5436 }
df58fc94 5437 continue;
4372b673 5438
252b5132
RH
5439 case 'V':
5440 case 'S':
df58fc94
RS
5441 gas_assert (!mips_opts.micromips);
5442 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5443 continue;
5444
5445 case 'z':
5446 continue;
5447
5448 case '<':
df58fc94
RS
5449 INSERT_OPERAND (mips_opts.micromips,
5450 SHAMT, insn, va_arg (args, int));
252b5132
RH
5451 continue;
5452
5453 case 'D':
df58fc94
RS
5454 gas_assert (!mips_opts.micromips);
5455 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5456 continue;
5457
5458 case 'B':
df58fc94
RS
5459 gas_assert (!mips_opts.micromips);
5460 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5461 continue;
5462
4372b673 5463 case 'J':
df58fc94
RS
5464 gas_assert (!mips_opts.micromips);
5465 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5466 continue;
5467
252b5132 5468 case 'q':
df58fc94
RS
5469 gas_assert (!mips_opts.micromips);
5470 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5471 continue;
5472
5473 case 'b':
5474 case 's':
5475 case 'r':
5476 case 'v':
df58fc94 5477 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5478 continue;
5479
5480 case 'i':
5481 case 'j':
6e1304d8 5482 macro_read_relocs (&args, r);
9c2799c2 5483 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5484 || *r == BFD_RELOC_MIPS_HIGHER
5485 || *r == BFD_RELOC_HI16_S
5486 || *r == BFD_RELOC_LO16
5487 || *r == BFD_RELOC_MIPS_GOT_OFST);
5488 continue;
5489
5490 case 'o':
5491 macro_read_relocs (&args, r);
252b5132
RH
5492 continue;
5493
5494 case 'u':
6e1304d8 5495 macro_read_relocs (&args, r);
9c2799c2 5496 gas_assert (ep != NULL
90ecf173
MR
5497 && (ep->X_op == O_constant
5498 || (ep->X_op == O_symbol
5499 && (*r == BFD_RELOC_MIPS_HIGHEST
5500 || *r == BFD_RELOC_HI16_S
5501 || *r == BFD_RELOC_HI16
5502 || *r == BFD_RELOC_GPREL16
5503 || *r == BFD_RELOC_MIPS_GOT_HI16
5504 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5505 continue;
5506
5507 case 'p':
9c2799c2 5508 gas_assert (ep != NULL);
bad36eac 5509
252b5132
RH
5510 /*
5511 * This allows macro() to pass an immediate expression for
5512 * creating short branches without creating a symbol.
bad36eac
DJ
5513 *
5514 * We don't allow branch relaxation for these branches, as
5515 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5516 */
5517 if (ep->X_op == O_constant)
5518 {
df58fc94
RS
5519 /* For microMIPS we always use relocations for branches.
5520 So we should not resolve immediate values. */
5521 gas_assert (!mips_opts.micromips);
5522
bad36eac
DJ
5523 if ((ep->X_add_number & 3) != 0)
5524 as_bad (_("branch to misaligned address (0x%lx)"),
5525 (unsigned long) ep->X_add_number);
5526 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5527 as_bad (_("branch address range overflow (0x%lx)"),
5528 (unsigned long) ep->X_add_number);
252b5132
RH
5529 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5530 ep = NULL;
5531 }
5532 else
0b25d3e6 5533 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5534 continue;
5535
5536 case 'a':
9c2799c2 5537 gas_assert (ep != NULL);
f6688943 5538 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5539 continue;
5540
5541 case 'C':
df58fc94
RS
5542 gas_assert (!mips_opts.micromips);
5543 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5544 continue;
5545
d43b4baf 5546 case 'k':
df58fc94
RS
5547 INSERT_OPERAND (mips_opts.micromips,
5548 CACHE, insn, va_arg (args, unsigned long));
5549 continue;
5550
5551 case '|':
5552 gas_assert (mips_opts.micromips);
5553 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5554 continue;
5555
5556 case '.':
5557 gas_assert (mips_opts.micromips);
5558 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5559 continue;
5560
dec0624d
MR
5561 case '\\':
5562 INSERT_OPERAND (mips_opts.micromips,
5563 3BITPOS, insn, va_arg (args, unsigned int));
5564 continue;
5565
df58fc94 5566 case '~':
dec0624d
MR
5567 INSERT_OPERAND (mips_opts.micromips,
5568 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5569 continue;
5570
5571 case 'N':
5572 gas_assert (mips_opts.micromips);
5573 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5574 continue;
5575
5576 case 'm': /* Opcode extension character. */
5577 gas_assert (mips_opts.micromips);
5578 switch (*fmt++)
5579 {
5580 case 'j':
5581 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5582 break;
5583
5584 case 'p':
5585 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5586 break;
5587
5588 case 'F':
5589 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5590 break;
5591
5592 default:
b37df7c4 5593 abort ();
df58fc94 5594 }
d43b4baf
TS
5595 continue;
5596
252b5132 5597 default:
b37df7c4 5598 abort ();
252b5132
RH
5599 }
5600 break;
5601 }
5602 va_end (args);
9c2799c2 5603 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5604
df58fc94 5605 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5606}
5607
5608static void
67c0d1eb 5609mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5610 va_list *args)
252b5132 5611{
1e915849 5612 struct mips_opcode *mo;
252b5132 5613 struct mips_cl_insn insn;
f6688943
TS
5614 bfd_reloc_code_real_type r[3]
5615 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5616
1e915849 5617 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5618 gas_assert (mo);
5619 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5620
1e915849 5621 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5622 {
1e915849 5623 ++mo;
9c2799c2
NC
5624 gas_assert (mo->name);
5625 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5626 }
5627
1e915849 5628 create_insn (&insn, mo);
252b5132
RH
5629 for (;;)
5630 {
5631 int c;
5632
5633 c = *fmt++;
5634 switch (c)
5635 {
5636 case '\0':
5637 break;
5638
5639 case ',':
5640 case '(':
5641 case ')':
5642 continue;
5643
5644 case 'y':
5645 case 'w':
03ea81db 5646 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5647 continue;
5648
5649 case 'x':
5650 case 'v':
03ea81db 5651 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5652 continue;
5653
5654 case 'z':
03ea81db 5655 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5656 continue;
5657
5658 case 'Z':
03ea81db 5659 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5660 continue;
5661
5662 case '0':
5663 case 'S':
5664 case 'P':
5665 case 'R':
5666 continue;
5667
5668 case 'X':
03ea81db 5669 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5670 continue;
5671
5672 case 'Y':
5673 {
5674 int regno;
5675
03ea81db 5676 regno = va_arg (*args, int);
252b5132 5677 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5678 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5679 }
5680 continue;
5681
5682 case '<':
5683 case '>':
5684 case '4':
5685 case '5':
5686 case 'H':
5687 case 'W':
5688 case 'D':
5689 case 'j':
5690 case '8':
5691 case 'V':
5692 case 'C':
5693 case 'U':
5694 case 'k':
5695 case 'K':
5696 case 'p':
5697 case 'q':
5698 {
b886a2ab
RS
5699 offsetT value;
5700
9c2799c2 5701 gas_assert (ep != NULL);
252b5132
RH
5702
5703 if (ep->X_op != O_constant)
874e8986 5704 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5705 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5706 {
b886a2ab 5707 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5708 ep = NULL;
f6688943 5709 *r = BFD_RELOC_UNUSED;
252b5132
RH
5710 }
5711 }
5712 continue;
5713
5714 case '6':
03ea81db 5715 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5716 continue;
5717 }
5718
5719 break;
5720 }
5721
9c2799c2 5722 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5723
df58fc94 5724 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5725}
5726
2051e8c4
MR
5727/*
5728 * Sign-extend 32-bit mode constants that have bit 31 set and all
5729 * higher bits unset.
5730 */
9f872bbe 5731static void
2051e8c4
MR
5732normalize_constant_expr (expressionS *ex)
5733{
9ee2a2d4 5734 if (ex->X_op == O_constant
2051e8c4
MR
5735 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5736 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5737 - 0x80000000);
5738}
5739
5740/*
5741 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5742 * all higher bits unset.
5743 */
5744static void
5745normalize_address_expr (expressionS *ex)
5746{
5747 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5748 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5749 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5750 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5751 - 0x80000000);
5752}
5753
438c16b8
TS
5754/*
5755 * Generate a "jalr" instruction with a relocation hint to the called
5756 * function. This occurs in NewABI PIC code.
5757 */
5758static void
df58fc94 5759macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5760{
df58fc94
RS
5761 static const bfd_reloc_code_real_type jalr_relocs[2]
5762 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5763 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5764 const char *jalr;
685736be 5765 char *f = NULL;
b34976b6 5766
1180b5a4 5767 if (MIPS_JALR_HINT_P (ep))
f21f8242 5768 {
cc3d92a5 5769 frag_grow (8);
f21f8242
AO
5770 f = frag_more (0);
5771 }
2906b037 5772 if (mips_opts.micromips)
df58fc94 5773 {
833794fc
MR
5774 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
5775 ? "jalr" : "jalrs");
e64af278 5776 if (MIPS_JALR_HINT_P (ep)
833794fc 5777 || mips_opts.insn32
e64af278 5778 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5779 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5780 else
5781 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5782 }
2906b037
MR
5783 else
5784 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5785 if (MIPS_JALR_HINT_P (ep))
df58fc94 5786 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5787}
5788
252b5132
RH
5789/*
5790 * Generate a "lui" instruction.
5791 */
5792static void
67c0d1eb 5793macro_build_lui (expressionS *ep, int regnum)
252b5132 5794{
9c2799c2 5795 gas_assert (! mips_opts.mips16);
252b5132 5796
df58fc94 5797 if (ep->X_op != O_constant)
252b5132 5798 {
9c2799c2 5799 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5800 /* _gp_disp is a special case, used from s_cpload.
5801 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5802 gas_assert (mips_pic == NO_PIC
78e1bb40 5803 || (! HAVE_NEWABI
aa6975fb
ILT
5804 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5805 || (! mips_in_shared
bbe506e8
TS
5806 && strcmp (S_GET_NAME (ep->X_add_symbol),
5807 "__gnu_local_gp") == 0));
252b5132
RH
5808 }
5809
df58fc94 5810 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5811}
5812
885add95
CD
5813/* Generate a sequence of instructions to do a load or store from a constant
5814 offset off of a base register (breg) into/from a target register (treg),
5815 using AT if necessary. */
5816static void
67c0d1eb
RS
5817macro_build_ldst_constoffset (expressionS *ep, const char *op,
5818 int treg, int breg, int dbl)
885add95 5819{
9c2799c2 5820 gas_assert (ep->X_op == O_constant);
885add95 5821
256ab948 5822 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5823 if (!dbl)
5824 normalize_constant_expr (ep);
256ab948 5825
67c1ffbe 5826 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5827 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5828 as_warn (_("operand overflow"));
5829
5830 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5831 {
5832 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5833 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5834 }
5835 else
5836 {
5837 /* 32-bit offset, need multiple instructions and AT, like:
5838 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5839 addu $tempreg,$tempreg,$breg
5840 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5841 to handle the complete offset. */
67c0d1eb
RS
5842 macro_build_lui (ep, AT);
5843 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5844 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5845
741fe287 5846 if (!mips_opts.at)
8fc2e39e 5847 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5848 }
5849}
5850
252b5132
RH
5851/* set_at()
5852 * Generates code to set the $at register to true (one)
5853 * if reg is less than the immediate expression.
5854 */
5855static void
67c0d1eb 5856set_at (int reg, int unsignedp)
252b5132
RH
5857{
5858 if (imm_expr.X_op == O_constant
5859 && imm_expr.X_add_number >= -0x8000
5860 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5861 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5862 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5863 else
5864 {
67c0d1eb
RS
5865 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5866 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5867 }
5868}
5869
5870/* Warn if an expression is not a constant. */
5871
5872static void
17a2f251 5873check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5874{
5875 if (ex->X_op == O_big)
5876 as_bad (_("unsupported large constant"));
5877 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5878 as_bad (_("Instruction %s requires absolute expression"),
5879 ip->insn_mo->name);
13757d0c 5880
9ee2a2d4
MR
5881 if (HAVE_32BIT_GPRS)
5882 normalize_constant_expr (ex);
252b5132
RH
5883}
5884
5885/* Count the leading zeroes by performing a binary chop. This is a
5886 bulky bit of source, but performance is a LOT better for the
5887 majority of values than a simple loop to count the bits:
5888 for (lcnt = 0; (lcnt < 32); lcnt++)
5889 if ((v) & (1 << (31 - lcnt)))
5890 break;
5891 However it is not code size friendly, and the gain will drop a bit
5892 on certain cached systems.
5893*/
5894#define COUNT_TOP_ZEROES(v) \
5895 (((v) & ~0xffff) == 0 \
5896 ? ((v) & ~0xff) == 0 \
5897 ? ((v) & ~0xf) == 0 \
5898 ? ((v) & ~0x3) == 0 \
5899 ? ((v) & ~0x1) == 0 \
5900 ? !(v) \
5901 ? 32 \
5902 : 31 \
5903 : 30 \
5904 : ((v) & ~0x7) == 0 \
5905 ? 29 \
5906 : 28 \
5907 : ((v) & ~0x3f) == 0 \
5908 ? ((v) & ~0x1f) == 0 \
5909 ? 27 \
5910 : 26 \
5911 : ((v) & ~0x7f) == 0 \
5912 ? 25 \
5913 : 24 \
5914 : ((v) & ~0xfff) == 0 \
5915 ? ((v) & ~0x3ff) == 0 \
5916 ? ((v) & ~0x1ff) == 0 \
5917 ? 23 \
5918 : 22 \
5919 : ((v) & ~0x7ff) == 0 \
5920 ? 21 \
5921 : 20 \
5922 : ((v) & ~0x3fff) == 0 \
5923 ? ((v) & ~0x1fff) == 0 \
5924 ? 19 \
5925 : 18 \
5926 : ((v) & ~0x7fff) == 0 \
5927 ? 17 \
5928 : 16 \
5929 : ((v) & ~0xffffff) == 0 \
5930 ? ((v) & ~0xfffff) == 0 \
5931 ? ((v) & ~0x3ffff) == 0 \
5932 ? ((v) & ~0x1ffff) == 0 \
5933 ? 15 \
5934 : 14 \
5935 : ((v) & ~0x7ffff) == 0 \
5936 ? 13 \
5937 : 12 \
5938 : ((v) & ~0x3fffff) == 0 \
5939 ? ((v) & ~0x1fffff) == 0 \
5940 ? 11 \
5941 : 10 \
5942 : ((v) & ~0x7fffff) == 0 \
5943 ? 9 \
5944 : 8 \
5945 : ((v) & ~0xfffffff) == 0 \
5946 ? ((v) & ~0x3ffffff) == 0 \
5947 ? ((v) & ~0x1ffffff) == 0 \
5948 ? 7 \
5949 : 6 \
5950 : ((v) & ~0x7ffffff) == 0 \
5951 ? 5 \
5952 : 4 \
5953 : ((v) & ~0x3fffffff) == 0 \
5954 ? ((v) & ~0x1fffffff) == 0 \
5955 ? 3 \
5956 : 2 \
5957 : ((v) & ~0x7fffffff) == 0 \
5958 ? 1 \
5959 : 0)
5960
5961/* load_register()
67c1ffbe 5962 * This routine generates the least number of instructions necessary to load
252b5132
RH
5963 * an absolute expression value into a register.
5964 */
5965static void
67c0d1eb 5966load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5967{
5968 int freg;
5969 expressionS hi32, lo32;
5970
5971 if (ep->X_op != O_big)
5972 {
9c2799c2 5973 gas_assert (ep->X_op == O_constant);
256ab948
TS
5974
5975 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5976 if (!dbl)
5977 normalize_constant_expr (ep);
256ab948
TS
5978
5979 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5980 {
5981 /* We can handle 16 bit signed values with an addiu to
5982 $zero. No need to ever use daddiu here, since $zero and
5983 the result are always correct in 32 bit mode. */
67c0d1eb 5984 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5985 return;
5986 }
5987 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5988 {
5989 /* We can handle 16 bit unsigned values with an ori to
5990 $zero. */
67c0d1eb 5991 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5992 return;
5993 }
256ab948 5994 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5995 {
5996 /* 32 bit values require an lui. */
df58fc94 5997 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 5998 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 5999 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
6000 return;
6001 }
6002 }
6003
6004 /* The value is larger than 32 bits. */
6005
2051e8c4 6006 if (!dbl || HAVE_32BIT_GPRS)
252b5132 6007 {
55e08f71
NC
6008 char value[32];
6009
6010 sprintf_vma (value, ep->X_add_number);
20e1fcfd 6011 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 6012 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
6013 return;
6014 }
6015
6016 if (ep->X_op != O_big)
6017 {
6018 hi32 = *ep;
6019 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6020 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6021 hi32.X_add_number &= 0xffffffff;
6022 lo32 = *ep;
6023 lo32.X_add_number &= 0xffffffff;
6024 }
6025 else
6026 {
9c2799c2 6027 gas_assert (ep->X_add_number > 2);
252b5132
RH
6028 if (ep->X_add_number == 3)
6029 generic_bignum[3] = 0;
6030 else if (ep->X_add_number > 4)
6031 as_bad (_("Number larger than 64 bits"));
6032 lo32.X_op = O_constant;
6033 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
6034 hi32.X_op = O_constant;
6035 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
6036 }
6037
6038 if (hi32.X_add_number == 0)
6039 freg = 0;
6040 else
6041 {
6042 int shift, bit;
6043 unsigned long hi, lo;
6044
956cd1d6 6045 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
6046 {
6047 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
6048 {
67c0d1eb 6049 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
6050 return;
6051 }
6052 if (lo32.X_add_number & 0x80000000)
6053 {
df58fc94 6054 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 6055 if (lo32.X_add_number & 0xffff)
67c0d1eb 6056 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
6057 return;
6058 }
6059 }
252b5132
RH
6060
6061 /* Check for 16bit shifted constant. We know that hi32 is
6062 non-zero, so start the mask on the first bit of the hi32
6063 value. */
6064 shift = 17;
6065 do
beae10d5
KH
6066 {
6067 unsigned long himask, lomask;
6068
6069 if (shift < 32)
6070 {
6071 himask = 0xffff >> (32 - shift);
6072 lomask = (0xffff << shift) & 0xffffffff;
6073 }
6074 else
6075 {
6076 himask = 0xffff << (shift - 32);
6077 lomask = 0;
6078 }
6079 if ((hi32.X_add_number & ~(offsetT) himask) == 0
6080 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
6081 {
6082 expressionS tmp;
6083
6084 tmp.X_op = O_constant;
6085 if (shift < 32)
6086 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
6087 | (lo32.X_add_number >> shift));
6088 else
6089 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 6090 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 6091 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 6092 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
6093 return;
6094 }
f9419b05 6095 ++shift;
beae10d5
KH
6096 }
6097 while (shift <= (64 - 16));
252b5132
RH
6098
6099 /* Find the bit number of the lowest one bit, and store the
6100 shifted value in hi/lo. */
6101 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
6102 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
6103 if (lo != 0)
6104 {
6105 bit = 0;
6106 while ((lo & 1) == 0)
6107 {
6108 lo >>= 1;
6109 ++bit;
6110 }
6111 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
6112 hi >>= bit;
6113 }
6114 else
6115 {
6116 bit = 32;
6117 while ((hi & 1) == 0)
6118 {
6119 hi >>= 1;
6120 ++bit;
6121 }
6122 lo = hi;
6123 hi = 0;
6124 }
6125
6126 /* Optimize if the shifted value is a (power of 2) - 1. */
6127 if ((hi == 0 && ((lo + 1) & lo) == 0)
6128 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
6129 {
6130 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 6131 if (shift != 0)
beae10d5 6132 {
252b5132
RH
6133 expressionS tmp;
6134
6135 /* This instruction will set the register to be all
6136 ones. */
beae10d5
KH
6137 tmp.X_op = O_constant;
6138 tmp.X_add_number = (offsetT) -1;
67c0d1eb 6139 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
6140 if (bit != 0)
6141 {
6142 bit += shift;
df58fc94 6143 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 6144 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 6145 }
df58fc94 6146 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 6147 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
6148 return;
6149 }
6150 }
252b5132
RH
6151
6152 /* Sign extend hi32 before calling load_register, because we can
6153 generally get better code when we load a sign extended value. */
6154 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 6155 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 6156 load_register (reg, &hi32, 0);
252b5132
RH
6157 freg = reg;
6158 }
6159 if ((lo32.X_add_number & 0xffff0000) == 0)
6160 {
6161 if (freg != 0)
6162 {
df58fc94 6163 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
6164 freg = reg;
6165 }
6166 }
6167 else
6168 {
6169 expressionS mid16;
6170
956cd1d6 6171 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 6172 {
df58fc94
RS
6173 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6174 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
6175 return;
6176 }
252b5132
RH
6177
6178 if (freg != 0)
6179 {
df58fc94 6180 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
6181 freg = reg;
6182 }
6183 mid16 = lo32;
6184 mid16.X_add_number >>= 16;
67c0d1eb 6185 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 6186 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
6187 freg = reg;
6188 }
6189 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 6190 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
6191}
6192
269137b2
TS
6193static inline void
6194load_delay_nop (void)
6195{
6196 if (!gpr_interlocks)
6197 macro_build (NULL, "nop", "");
6198}
6199
252b5132
RH
6200/* Load an address into a register. */
6201
6202static void
67c0d1eb 6203load_address (int reg, expressionS *ep, int *used_at)
252b5132 6204{
252b5132
RH
6205 if (ep->X_op != O_constant
6206 && ep->X_op != O_symbol)
6207 {
6208 as_bad (_("expression too complex"));
6209 ep->X_op = O_constant;
6210 }
6211
6212 if (ep->X_op == O_constant)
6213 {
67c0d1eb 6214 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
6215 return;
6216 }
6217
6218 if (mips_pic == NO_PIC)
6219 {
6220 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 6221 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
6222 Otherwise we want
6223 lui $reg,<sym> (BFD_RELOC_HI16_S)
6224 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 6225 If we have an addend, we always use the latter form.
76b3015f 6226
d6bc6245
TS
6227 With 64bit address space and a usable $at we want
6228 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6229 lui $at,<sym> (BFD_RELOC_HI16_S)
6230 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6231 daddiu $at,<sym> (BFD_RELOC_LO16)
6232 dsll32 $reg,0
3a482fd5 6233 daddu $reg,$reg,$at
76b3015f 6234
c03099e6 6235 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
6236 on superscalar processors.
6237 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6238 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6239 dsll $reg,16
6240 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
6241 dsll $reg,16
6242 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
6243
6244 For GP relative symbols in 64bit address space we can use
6245 the same sequence as in 32bit address space. */
aed1a261 6246 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6247 {
6caf9ef4
TS
6248 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6249 && !nopic_need_relax (ep->X_add_symbol, 1))
6250 {
6251 relax_start (ep->X_add_symbol);
6252 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6253 mips_gp_register, BFD_RELOC_GPREL16);
6254 relax_switch ();
6255 }
d6bc6245 6256
741fe287 6257 if (*used_at == 0 && mips_opts.at)
d6bc6245 6258 {
df58fc94
RS
6259 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6260 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
6261 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6262 BFD_RELOC_MIPS_HIGHER);
6263 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 6264 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 6265 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
6266 *used_at = 1;
6267 }
6268 else
6269 {
df58fc94 6270 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
6271 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6272 BFD_RELOC_MIPS_HIGHER);
df58fc94 6273 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 6274 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 6275 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 6276 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 6277 }
6caf9ef4
TS
6278
6279 if (mips_relax.sequence)
6280 relax_end ();
d6bc6245 6281 }
252b5132
RH
6282 else
6283 {
d6bc6245 6284 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6285 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 6286 {
4d7206a2 6287 relax_start (ep->X_add_symbol);
67c0d1eb 6288 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 6289 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 6290 relax_switch ();
d6bc6245 6291 }
67c0d1eb
RS
6292 macro_build_lui (ep, reg);
6293 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
6294 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
6295 if (mips_relax.sequence)
6296 relax_end ();
d6bc6245 6297 }
252b5132 6298 }
0a44bf69 6299 else if (!mips_big_got)
252b5132
RH
6300 {
6301 expressionS ex;
6302
6303 /* If this is a reference to an external symbol, we want
6304 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6305 Otherwise we want
6306 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6307 nop
6308 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
6309 If there is a constant, it must be added in after.
6310
ed6fb7bd 6311 If we have NewABI, we want
f5040a92
AO
6312 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
6313 unless we're referencing a global symbol with a non-zero
6314 offset, in which case cst must be added separately. */
ed6fb7bd
SC
6315 if (HAVE_NEWABI)
6316 {
f5040a92
AO
6317 if (ep->X_add_number)
6318 {
4d7206a2 6319 ex.X_add_number = ep->X_add_number;
f5040a92 6320 ep->X_add_number = 0;
4d7206a2 6321 relax_start (ep->X_add_symbol);
67c0d1eb
RS
6322 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6323 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
6324 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6325 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6326 ex.X_op = O_constant;
67c0d1eb 6327 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6328 reg, reg, BFD_RELOC_LO16);
f5040a92 6329 ep->X_add_number = ex.X_add_number;
4d7206a2 6330 relax_switch ();
f5040a92 6331 }
67c0d1eb 6332 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6333 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
6334 if (mips_relax.sequence)
6335 relax_end ();
ed6fb7bd
SC
6336 }
6337 else
6338 {
f5040a92
AO
6339 ex.X_add_number = ep->X_add_number;
6340 ep->X_add_number = 0;
67c0d1eb
RS
6341 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6342 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6343 load_delay_nop ();
4d7206a2
RS
6344 relax_start (ep->X_add_symbol);
6345 relax_switch ();
67c0d1eb 6346 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6347 BFD_RELOC_LO16);
4d7206a2 6348 relax_end ();
ed6fb7bd 6349
f5040a92
AO
6350 if (ex.X_add_number != 0)
6351 {
6352 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6353 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6354 ex.X_op = O_constant;
67c0d1eb 6355 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6356 reg, reg, BFD_RELOC_LO16);
f5040a92 6357 }
252b5132
RH
6358 }
6359 }
0a44bf69 6360 else if (mips_big_got)
252b5132
RH
6361 {
6362 expressionS ex;
252b5132
RH
6363
6364 /* This is the large GOT case. If this is a reference to an
6365 external symbol, we want
6366 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6367 addu $reg,$reg,$gp
6368 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6369
6370 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6371 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6372 nop
6373 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6374 If there is a constant, it must be added in after.
f5040a92
AO
6375
6376 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6377 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6378 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6379 */
438c16b8
TS
6380 if (HAVE_NEWABI)
6381 {
4d7206a2 6382 ex.X_add_number = ep->X_add_number;
f5040a92 6383 ep->X_add_number = 0;
4d7206a2 6384 relax_start (ep->X_add_symbol);
df58fc94 6385 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6386 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6387 reg, reg, mips_gp_register);
6388 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6389 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6390 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6391 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6392 else if (ex.X_add_number)
6393 {
6394 ex.X_op = O_constant;
67c0d1eb
RS
6395 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6396 BFD_RELOC_LO16);
f5040a92
AO
6397 }
6398
6399 ep->X_add_number = ex.X_add_number;
4d7206a2 6400 relax_switch ();
67c0d1eb 6401 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6402 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6403 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6404 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6405 relax_end ();
438c16b8 6406 }
252b5132 6407 else
438c16b8 6408 {
f5040a92
AO
6409 ex.X_add_number = ep->X_add_number;
6410 ep->X_add_number = 0;
4d7206a2 6411 relax_start (ep->X_add_symbol);
df58fc94 6412 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6413 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6414 reg, reg, mips_gp_register);
6415 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6416 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6417 relax_switch ();
6418 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6419 {
6420 /* We need a nop before loading from $gp. This special
6421 check is required because the lui which starts the main
6422 instruction stream does not refer to $gp, and so will not
6423 insert the nop which may be required. */
67c0d1eb 6424 macro_build (NULL, "nop", "");
438c16b8 6425 }
67c0d1eb 6426 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6427 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6428 load_delay_nop ();
67c0d1eb 6429 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6430 BFD_RELOC_LO16);
4d7206a2 6431 relax_end ();
438c16b8 6432
f5040a92
AO
6433 if (ex.X_add_number != 0)
6434 {
6435 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6436 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6437 ex.X_op = O_constant;
67c0d1eb
RS
6438 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6439 BFD_RELOC_LO16);
f5040a92 6440 }
252b5132
RH
6441 }
6442 }
252b5132
RH
6443 else
6444 abort ();
8fc2e39e 6445
741fe287 6446 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6447 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6448}
6449
ea1fb5dc
RS
6450/* Move the contents of register SOURCE into register DEST. */
6451
6452static void
67c0d1eb 6453move_register (int dest, int source)
ea1fb5dc 6454{
df58fc94
RS
6455 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6456 instruction specifically requires a 32-bit one. */
6457 if (mips_opts.micromips
833794fc 6458 && !mips_opts.insn32
df58fc94 6459 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6460 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6461 else
6462 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6463 dest, source, 0);
ea1fb5dc
RS
6464}
6465
4d7206a2 6466/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6467 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6468 The two alternatives are:
4d7206a2
RS
6469
6470 Global symbol Local sybmol
6471 ------------- ------------
6472 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6473 ... ...
6474 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6475
6476 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6477 emits the second for a 16-bit offset or add_got_offset_hilo emits
6478 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6479
6480static void
67c0d1eb 6481load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6482{
6483 expressionS global;
6484
6485 global = *local;
6486 global.X_add_number = 0;
6487
6488 relax_start (local->X_add_symbol);
67c0d1eb
RS
6489 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6490 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6491 relax_switch ();
67c0d1eb
RS
6492 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6493 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6494 relax_end ();
6495}
6496
6497static void
67c0d1eb 6498add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6499{
6500 expressionS global;
6501
6502 global.X_op = O_constant;
6503 global.X_op_symbol = NULL;
6504 global.X_add_symbol = NULL;
6505 global.X_add_number = local->X_add_number;
6506
6507 relax_start (local->X_add_symbol);
67c0d1eb 6508 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6509 dest, dest, BFD_RELOC_LO16);
6510 relax_switch ();
67c0d1eb 6511 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6512 relax_end ();
6513}
6514
f6a22291
MR
6515static void
6516add_got_offset_hilo (int dest, expressionS *local, int tmp)
6517{
6518 expressionS global;
6519 int hold_mips_optimize;
6520
6521 global.X_op = O_constant;
6522 global.X_op_symbol = NULL;
6523 global.X_add_symbol = NULL;
6524 global.X_add_number = local->X_add_number;
6525
6526 relax_start (local->X_add_symbol);
6527 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6528 relax_switch ();
6529 /* Set mips_optimize around the lui instruction to avoid
6530 inserting an unnecessary nop after the lw. */
6531 hold_mips_optimize = mips_optimize;
6532 mips_optimize = 2;
6533 macro_build_lui (&global, tmp);
6534 mips_optimize = hold_mips_optimize;
6535 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6536 relax_end ();
6537
6538 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6539}
6540
df58fc94
RS
6541/* Emit a sequence of instructions to emulate a branch likely operation.
6542 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6543 is its complementing branch with the original condition negated.
6544 CALL is set if the original branch specified the link operation.
6545 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6546
6547 Code like this is produced in the noreorder mode:
6548
6549 BRNEG <args>, 1f
6550 nop
6551 b <sym>
6552 delay slot (executed only if branch taken)
6553 1:
6554
6555 or, if CALL is set:
6556
6557 BRNEG <args>, 1f
6558 nop
6559 bal <sym>
6560 delay slot (executed only if branch taken)
6561 1:
6562
6563 In the reorder mode the delay slot would be filled with a nop anyway,
6564 so code produced is simply:
6565
6566 BR <args>, <sym>
6567 nop
6568
6569 This function is used when producing code for the microMIPS ASE that
6570 does not implement branch likely instructions in hardware. */
6571
6572static void
6573macro_build_branch_likely (const char *br, const char *brneg,
6574 int call, expressionS *ep, const char *fmt,
6575 unsigned int sreg, unsigned int treg)
6576{
6577 int noreorder = mips_opts.noreorder;
6578 expressionS expr1;
6579
6580 gas_assert (mips_opts.micromips);
6581 start_noreorder ();
6582 if (noreorder)
6583 {
6584 micromips_label_expr (&expr1);
6585 macro_build (&expr1, brneg, fmt, sreg, treg);
6586 macro_build (NULL, "nop", "");
6587 macro_build (ep, call ? "bal" : "b", "p");
6588
6589 /* Set to true so that append_insn adds a label. */
6590 emit_branch_likely_macro = TRUE;
6591 }
6592 else
6593 {
6594 macro_build (ep, br, fmt, sreg, treg);
6595 macro_build (NULL, "nop", "");
6596 }
6597 end_noreorder ();
6598}
6599
6600/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6601 the condition code tested. EP specifies the branch target. */
6602
6603static void
6604macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6605{
6606 const int call = 0;
6607 const char *brneg;
6608 const char *br;
6609
6610 switch (type)
6611 {
6612 case M_BC1FL:
6613 br = "bc1f";
6614 brneg = "bc1t";
6615 break;
6616 case M_BC1TL:
6617 br = "bc1t";
6618 brneg = "bc1f";
6619 break;
6620 case M_BC2FL:
6621 br = "bc2f";
6622 brneg = "bc2t";
6623 break;
6624 case M_BC2TL:
6625 br = "bc2t";
6626 brneg = "bc2f";
6627 break;
6628 default:
6629 abort ();
6630 }
6631 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6632}
6633
6634/* Emit a two-argument branch macro specified by TYPE, using SREG as
6635 the register tested. EP specifies the branch target. */
6636
6637static void
6638macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6639{
6640 const char *brneg = NULL;
6641 const char *br;
6642 int call = 0;
6643
6644 switch (type)
6645 {
6646 case M_BGEZ:
6647 br = "bgez";
6648 break;
6649 case M_BGEZL:
6650 br = mips_opts.micromips ? "bgez" : "bgezl";
6651 brneg = "bltz";
6652 break;
6653 case M_BGEZALL:
6654 gas_assert (mips_opts.micromips);
833794fc 6655 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
6656 brneg = "bltz";
6657 call = 1;
6658 break;
6659 case M_BGTZ:
6660 br = "bgtz";
6661 break;
6662 case M_BGTZL:
6663 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6664 brneg = "blez";
6665 break;
6666 case M_BLEZ:
6667 br = "blez";
6668 break;
6669 case M_BLEZL:
6670 br = mips_opts.micromips ? "blez" : "blezl";
6671 brneg = "bgtz";
6672 break;
6673 case M_BLTZ:
6674 br = "bltz";
6675 break;
6676 case M_BLTZL:
6677 br = mips_opts.micromips ? "bltz" : "bltzl";
6678 brneg = "bgez";
6679 break;
6680 case M_BLTZALL:
6681 gas_assert (mips_opts.micromips);
833794fc 6682 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
6683 brneg = "bgez";
6684 call = 1;
6685 break;
6686 default:
6687 abort ();
6688 }
6689 if (mips_opts.micromips && brneg)
6690 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6691 else
6692 macro_build (ep, br, "s,p", sreg);
6693}
6694
6695/* Emit a three-argument branch macro specified by TYPE, using SREG and
6696 TREG as the registers tested. EP specifies the branch target. */
6697
6698static void
6699macro_build_branch_rsrt (int type, expressionS *ep,
6700 unsigned int sreg, unsigned int treg)
6701{
6702 const char *brneg = NULL;
6703 const int call = 0;
6704 const char *br;
6705
6706 switch (type)
6707 {
6708 case M_BEQ:
6709 case M_BEQ_I:
6710 br = "beq";
6711 break;
6712 case M_BEQL:
6713 case M_BEQL_I:
6714 br = mips_opts.micromips ? "beq" : "beql";
6715 brneg = "bne";
6716 break;
6717 case M_BNE:
6718 case M_BNE_I:
6719 br = "bne";
6720 break;
6721 case M_BNEL:
6722 case M_BNEL_I:
6723 br = mips_opts.micromips ? "bne" : "bnel";
6724 brneg = "beq";
6725 break;
6726 default:
6727 abort ();
6728 }
6729 if (mips_opts.micromips && brneg)
6730 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6731 else
6732 macro_build (ep, br, "s,t,p", sreg, treg);
6733}
6734
252b5132
RH
6735/*
6736 * Build macros
6737 * This routine implements the seemingly endless macro or synthesized
6738 * instructions and addressing modes in the mips assembly language. Many
6739 * of these macros are simple and are similar to each other. These could
67c1ffbe 6740 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6741 * this verbose method. Others are not simple macros but are more like
6742 * optimizing code generation.
6743 * One interesting optimization is when several store macros appear
67c1ffbe 6744 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6745 * The ensuing load upper instructions are ommited. This implies some kind
6746 * of global optimization. We currently only optimize within a single macro.
6747 * For many of the load and store macros if the address is specified as a
6748 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6749 * first load register 'at' with zero and use it as the base register. The
6750 * mips assembler simply uses register $zero. Just one tiny optimization
6751 * we're missing.
6752 */
6753static void
833794fc 6754macro (struct mips_cl_insn *ip, char *str)
252b5132 6755{
741fe287
MR
6756 unsigned int treg, sreg, dreg, breg;
6757 unsigned int tempreg;
252b5132 6758 int mask;
43841e91 6759 int used_at = 0;
df58fc94 6760 expressionS label_expr;
252b5132 6761 expressionS expr1;
df58fc94 6762 expressionS *ep;
252b5132
RH
6763 const char *s;
6764 const char *s2;
6765 const char *fmt;
6766 int likely = 0;
252b5132 6767 int coproc = 0;
7f3c4072 6768 int offbits = 16;
1abe91b1 6769 int call = 0;
df58fc94
RS
6770 int jals = 0;
6771 int dbl = 0;
6772 int imm = 0;
6773 int ust = 0;
6774 int lp = 0;
6775 int ab = 0;
252b5132
RH
6776 int off;
6777 bfd_reloc_code_real_type r;
252b5132
RH
6778 int hold_mips_optimize;
6779
9c2799c2 6780 gas_assert (! mips_opts.mips16);
252b5132 6781
df58fc94
RS
6782 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6783 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6784 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6785 mask = ip->insn_mo->mask;
6786
df58fc94
RS
6787 label_expr.X_op = O_constant;
6788 label_expr.X_op_symbol = NULL;
6789 label_expr.X_add_symbol = NULL;
6790 label_expr.X_add_number = 0;
6791
252b5132
RH
6792 expr1.X_op = O_constant;
6793 expr1.X_op_symbol = NULL;
6794 expr1.X_add_symbol = NULL;
6795 expr1.X_add_number = 1;
6796
6797 switch (mask)
6798 {
6799 case M_DABS:
6800 dbl = 1;
6801 case M_ABS:
df58fc94
RS
6802 /* bgez $a0,1f
6803 move v0,$a0
6804 sub v0,$zero,$a0
6805 1:
6806 */
252b5132 6807
7d10b47d 6808 start_noreorder ();
252b5132 6809
df58fc94
RS
6810 if (mips_opts.micromips)
6811 micromips_label_expr (&label_expr);
6812 else
6813 label_expr.X_add_number = 8;
6814 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6815 if (dreg == sreg)
a605d2b3 6816 macro_build (NULL, "nop", "");
252b5132 6817 else
67c0d1eb
RS
6818 move_register (dreg, sreg);
6819 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6820 if (mips_opts.micromips)
6821 micromips_add_label ();
252b5132 6822
7d10b47d 6823 end_noreorder ();
8fc2e39e 6824 break;
252b5132
RH
6825
6826 case M_ADD_I:
6827 s = "addi";
6828 s2 = "add";
6829 goto do_addi;
6830 case M_ADDU_I:
6831 s = "addiu";
6832 s2 = "addu";
6833 goto do_addi;
6834 case M_DADD_I:
6835 dbl = 1;
6836 s = "daddi";
6837 s2 = "dadd";
df58fc94
RS
6838 if (!mips_opts.micromips)
6839 goto do_addi;
6840 if (imm_expr.X_op == O_constant
6841 && imm_expr.X_add_number >= -0x200
6842 && imm_expr.X_add_number < 0x200)
6843 {
6844 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6845 break;
6846 }
6847 goto do_addi_i;
252b5132
RH
6848 case M_DADDU_I:
6849 dbl = 1;
6850 s = "daddiu";
6851 s2 = "daddu";
6852 do_addi:
6853 if (imm_expr.X_op == O_constant
6854 && imm_expr.X_add_number >= -0x8000
6855 && imm_expr.X_add_number < 0x8000)
6856 {
67c0d1eb 6857 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6858 break;
252b5132 6859 }
df58fc94 6860 do_addi_i:
8fc2e39e 6861 used_at = 1;
67c0d1eb
RS
6862 load_register (AT, &imm_expr, dbl);
6863 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6864 break;
6865
6866 case M_AND_I:
6867 s = "andi";
6868 s2 = "and";
6869 goto do_bit;
6870 case M_OR_I:
6871 s = "ori";
6872 s2 = "or";
6873 goto do_bit;
6874 case M_NOR_I:
6875 s = "";
6876 s2 = "nor";
6877 goto do_bit;
6878 case M_XOR_I:
6879 s = "xori";
6880 s2 = "xor";
6881 do_bit:
6882 if (imm_expr.X_op == O_constant
6883 && imm_expr.X_add_number >= 0
6884 && imm_expr.X_add_number < 0x10000)
6885 {
6886 if (mask != M_NOR_I)
67c0d1eb 6887 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6888 else
6889 {
67c0d1eb
RS
6890 macro_build (&imm_expr, "ori", "t,r,i",
6891 treg, sreg, BFD_RELOC_LO16);
6892 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6893 }
8fc2e39e 6894 break;
252b5132
RH
6895 }
6896
8fc2e39e 6897 used_at = 1;
67c0d1eb
RS
6898 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6899 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6900 break;
6901
8b082fb1
TS
6902 case M_BALIGN:
6903 switch (imm_expr.X_add_number)
6904 {
6905 case 0:
6906 macro_build (NULL, "nop", "");
6907 break;
6908 case 2:
6909 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6910 break;
03f66e8a
MR
6911 case 1:
6912 case 3:
8b082fb1 6913 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6914 (int) imm_expr.X_add_number);
8b082fb1 6915 break;
03f66e8a
MR
6916 default:
6917 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6918 (unsigned long) imm_expr.X_add_number);
6919 break;
8b082fb1
TS
6920 }
6921 break;
6922
df58fc94
RS
6923 case M_BC1FL:
6924 case M_BC1TL:
6925 case M_BC2FL:
6926 case M_BC2TL:
6927 gas_assert (mips_opts.micromips);
6928 macro_build_branch_ccl (mask, &offset_expr,
6929 EXTRACT_OPERAND (1, BCC, *ip));
6930 break;
6931
252b5132 6932 case M_BEQ_I:
252b5132 6933 case M_BEQL_I:
252b5132 6934 case M_BNE_I:
252b5132 6935 case M_BNEL_I:
252b5132 6936 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6937 treg = 0;
6938 else
252b5132 6939 {
df58fc94
RS
6940 treg = AT;
6941 used_at = 1;
6942 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6943 }
df58fc94
RS
6944 /* Fall through. */
6945 case M_BEQL:
6946 case M_BNEL:
6947 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6948 break;
6949
6950 case M_BGEL:
6951 likely = 1;
6952 case M_BGE:
6953 if (treg == 0)
df58fc94
RS
6954 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6955 else if (sreg == 0)
6956 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6957 else
252b5132 6958 {
df58fc94
RS
6959 used_at = 1;
6960 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6961 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6962 &offset_expr, AT, ZERO);
252b5132 6963 }
df58fc94
RS
6964 break;
6965
6966 case M_BGEZL:
6967 case M_BGEZALL:
6968 case M_BGTZL:
6969 case M_BLEZL:
6970 case M_BLTZL:
6971 case M_BLTZALL:
6972 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6973 break;
6974
6975 case M_BGTL_I:
6976 likely = 1;
6977 case M_BGT_I:
90ecf173 6978 /* Check for > max integer. */
42429eac 6979 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
6980 {
6981 do_false:
90ecf173 6982 /* Result is always false. */
252b5132 6983 if (! likely)
a605d2b3 6984 macro_build (NULL, "nop", "");
252b5132 6985 else
df58fc94 6986 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6987 break;
252b5132
RH
6988 }
6989 if (imm_expr.X_op != O_constant)
6990 as_bad (_("Unsupported large constant"));
f9419b05 6991 ++imm_expr.X_add_number;
252b5132
RH
6992 /* FALLTHROUGH */
6993 case M_BGE_I:
6994 case M_BGEL_I:
6995 if (mask == M_BGEL_I)
6996 likely = 1;
6997 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
6998 {
df58fc94
RS
6999 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
7000 &offset_expr, sreg);
8fc2e39e 7001 break;
252b5132
RH
7002 }
7003 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7004 {
df58fc94
RS
7005 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
7006 &offset_expr, sreg);
8fc2e39e 7007 break;
252b5132 7008 }
42429eac 7009 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
7010 {
7011 do_true:
7012 /* result is always true */
7013 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 7014 macro_build (&offset_expr, "b", "p");
8fc2e39e 7015 break;
252b5132 7016 }
8fc2e39e 7017 used_at = 1;
67c0d1eb 7018 set_at (sreg, 0);
df58fc94
RS
7019 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7020 &offset_expr, AT, ZERO);
252b5132
RH
7021 break;
7022
7023 case M_BGEUL:
7024 likely = 1;
7025 case M_BGEU:
7026 if (treg == 0)
7027 goto do_true;
df58fc94
RS
7028 else if (sreg == 0)
7029 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7030 &offset_expr, ZERO, treg);
7031 else
252b5132 7032 {
df58fc94
RS
7033 used_at = 1;
7034 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7035 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7036 &offset_expr, AT, ZERO);
252b5132 7037 }
252b5132
RH
7038 break;
7039
7040 case M_BGTUL_I:
7041 likely = 1;
7042 case M_BGTU_I:
7043 if (sreg == 0
ca4e0257 7044 || (HAVE_32BIT_GPRS
252b5132 7045 && imm_expr.X_op == O_constant
f01dc953 7046 && imm_expr.X_add_number == -1))
252b5132
RH
7047 goto do_false;
7048 if (imm_expr.X_op != O_constant)
7049 as_bad (_("Unsupported large constant"));
f9419b05 7050 ++imm_expr.X_add_number;
252b5132
RH
7051 /* FALLTHROUGH */
7052 case M_BGEU_I:
7053 case M_BGEUL_I:
7054 if (mask == M_BGEUL_I)
7055 likely = 1;
7056 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7057 goto do_true;
df58fc94
RS
7058 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7059 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7060 &offset_expr, sreg, ZERO);
7061 else
252b5132 7062 {
df58fc94
RS
7063 used_at = 1;
7064 set_at (sreg, 1);
7065 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7066 &offset_expr, AT, ZERO);
252b5132 7067 }
252b5132
RH
7068 break;
7069
7070 case M_BGTL:
7071 likely = 1;
7072 case M_BGT:
7073 if (treg == 0)
df58fc94
RS
7074 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
7075 else if (sreg == 0)
7076 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
7077 else
252b5132 7078 {
df58fc94
RS
7079 used_at = 1;
7080 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7081 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7082 &offset_expr, AT, ZERO);
252b5132 7083 }
252b5132
RH
7084 break;
7085
7086 case M_BGTUL:
7087 likely = 1;
7088 case M_BGTU:
7089 if (treg == 0)
df58fc94
RS
7090 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7091 &offset_expr, sreg, ZERO);
7092 else if (sreg == 0)
7093 goto do_false;
7094 else
252b5132 7095 {
df58fc94
RS
7096 used_at = 1;
7097 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7098 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7099 &offset_expr, AT, ZERO);
252b5132 7100 }
252b5132
RH
7101 break;
7102
7103 case M_BLEL:
7104 likely = 1;
7105 case M_BLE:
7106 if (treg == 0)
df58fc94
RS
7107 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7108 else if (sreg == 0)
7109 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
7110 else
252b5132 7111 {
df58fc94
RS
7112 used_at = 1;
7113 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7114 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7115 &offset_expr, AT, ZERO);
252b5132 7116 }
252b5132
RH
7117 break;
7118
7119 case M_BLEL_I:
7120 likely = 1;
7121 case M_BLE_I:
42429eac 7122 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
7123 goto do_true;
7124 if (imm_expr.X_op != O_constant)
7125 as_bad (_("Unsupported large constant"));
f9419b05 7126 ++imm_expr.X_add_number;
252b5132
RH
7127 /* FALLTHROUGH */
7128 case M_BLT_I:
7129 case M_BLTL_I:
7130 if (mask == M_BLTL_I)
7131 likely = 1;
7132 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
7133 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7134 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7135 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7136 else
252b5132 7137 {
df58fc94
RS
7138 used_at = 1;
7139 set_at (sreg, 0);
7140 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7141 &offset_expr, AT, ZERO);
252b5132 7142 }
252b5132
RH
7143 break;
7144
7145 case M_BLEUL:
7146 likely = 1;
7147 case M_BLEU:
7148 if (treg == 0)
df58fc94
RS
7149 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7150 &offset_expr, sreg, ZERO);
7151 else if (sreg == 0)
7152 goto do_true;
7153 else
252b5132 7154 {
df58fc94
RS
7155 used_at = 1;
7156 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7157 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7158 &offset_expr, AT, ZERO);
252b5132 7159 }
252b5132
RH
7160 break;
7161
7162 case M_BLEUL_I:
7163 likely = 1;
7164 case M_BLEU_I:
7165 if (sreg == 0
ca4e0257 7166 || (HAVE_32BIT_GPRS
252b5132 7167 && imm_expr.X_op == O_constant
f01dc953 7168 && imm_expr.X_add_number == -1))
252b5132
RH
7169 goto do_true;
7170 if (imm_expr.X_op != O_constant)
7171 as_bad (_("Unsupported large constant"));
f9419b05 7172 ++imm_expr.X_add_number;
252b5132
RH
7173 /* FALLTHROUGH */
7174 case M_BLTU_I:
7175 case M_BLTUL_I:
7176 if (mask == M_BLTUL_I)
7177 likely = 1;
7178 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7179 goto do_false;
df58fc94
RS
7180 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7181 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7182 &offset_expr, sreg, ZERO);
7183 else
252b5132 7184 {
df58fc94
RS
7185 used_at = 1;
7186 set_at (sreg, 1);
7187 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7188 &offset_expr, AT, ZERO);
252b5132 7189 }
252b5132
RH
7190 break;
7191
7192 case M_BLTL:
7193 likely = 1;
7194 case M_BLT:
7195 if (treg == 0)
df58fc94
RS
7196 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7197 else if (sreg == 0)
7198 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
7199 else
252b5132 7200 {
df58fc94
RS
7201 used_at = 1;
7202 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
7203 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7204 &offset_expr, AT, ZERO);
252b5132 7205 }
252b5132
RH
7206 break;
7207
7208 case M_BLTUL:
7209 likely = 1;
7210 case M_BLTU:
7211 if (treg == 0)
7212 goto do_false;
df58fc94
RS
7213 else if (sreg == 0)
7214 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7215 &offset_expr, ZERO, treg);
7216 else
252b5132 7217 {
df58fc94
RS
7218 used_at = 1;
7219 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7220 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7221 &offset_expr, AT, ZERO);
252b5132 7222 }
252b5132
RH
7223 break;
7224
5f74bc13
CD
7225 case M_DEXT:
7226 {
d5818fca
MR
7227 /* Use unsigned arithmetic. */
7228 addressT pos;
7229 addressT size;
5f74bc13 7230
90ecf173 7231 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
7232 {
7233 as_bad (_("Unsupported large constant"));
7234 pos = size = 1;
7235 }
7236 else
7237 {
d5818fca
MR
7238 pos = imm_expr.X_add_number;
7239 size = imm2_expr.X_add_number;
5f74bc13
CD
7240 }
7241
7242 if (pos > 63)
7243 {
d5818fca 7244 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7245 pos = 1;
7246 }
90ecf173 7247 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7248 {
7249 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 7250 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7251 size = 1;
7252 }
7253
7254 if (size <= 32 && pos < 32)
7255 {
7256 s = "dext";
7257 fmt = "t,r,+A,+C";
7258 }
7259 else if (size <= 32)
7260 {
7261 s = "dextu";
7262 fmt = "t,r,+E,+H";
7263 }
7264 else
7265 {
7266 s = "dextm";
7267 fmt = "t,r,+A,+G";
7268 }
d5818fca
MR
7269 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7270 (int) (size - 1));
5f74bc13 7271 }
8fc2e39e 7272 break;
5f74bc13
CD
7273
7274 case M_DINS:
7275 {
d5818fca
MR
7276 /* Use unsigned arithmetic. */
7277 addressT pos;
7278 addressT size;
5f74bc13 7279
90ecf173 7280 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
7281 {
7282 as_bad (_("Unsupported large constant"));
7283 pos = size = 1;
7284 }
7285 else
7286 {
d5818fca
MR
7287 pos = imm_expr.X_add_number;
7288 size = imm2_expr.X_add_number;
5f74bc13
CD
7289 }
7290
7291 if (pos > 63)
7292 {
d5818fca 7293 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7294 pos = 1;
7295 }
90ecf173 7296 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7297 {
7298 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 7299 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7300 size = 1;
7301 }
7302
7303 if (pos < 32 && (pos + size - 1) < 32)
7304 {
7305 s = "dins";
7306 fmt = "t,r,+A,+B";
7307 }
7308 else if (pos >= 32)
7309 {
7310 s = "dinsu";
7311 fmt = "t,r,+E,+F";
7312 }
7313 else
7314 {
7315 s = "dinsm";
7316 fmt = "t,r,+A,+F";
7317 }
750bdd57
AS
7318 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7319 (int) (pos + size - 1));
5f74bc13 7320 }
8fc2e39e 7321 break;
5f74bc13 7322
252b5132
RH
7323 case M_DDIV_3:
7324 dbl = 1;
7325 case M_DIV_3:
7326 s = "mflo";
7327 goto do_div3;
7328 case M_DREM_3:
7329 dbl = 1;
7330 case M_REM_3:
7331 s = "mfhi";
7332 do_div3:
7333 if (treg == 0)
7334 {
7335 as_warn (_("Divide by zero."));
7336 if (mips_trap)
df58fc94 7337 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7338 else
df58fc94 7339 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7340 break;
252b5132
RH
7341 }
7342
7d10b47d 7343 start_noreorder ();
252b5132
RH
7344 if (mips_trap)
7345 {
df58fc94 7346 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7347 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7348 }
7349 else
7350 {
df58fc94
RS
7351 if (mips_opts.micromips)
7352 micromips_label_expr (&label_expr);
7353 else
7354 label_expr.X_add_number = 8;
7355 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7356 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7357 macro_build (NULL, "break", BRK_FMT, 7);
7358 if (mips_opts.micromips)
7359 micromips_add_label ();
252b5132
RH
7360 }
7361 expr1.X_add_number = -1;
8fc2e39e 7362 used_at = 1;
f6a22291 7363 load_register (AT, &expr1, dbl);
df58fc94
RS
7364 if (mips_opts.micromips)
7365 micromips_label_expr (&label_expr);
7366 else
7367 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7368 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7369 if (dbl)
7370 {
7371 expr1.X_add_number = 1;
f6a22291 7372 load_register (AT, &expr1, dbl);
df58fc94 7373 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7374 }
7375 else
7376 {
7377 expr1.X_add_number = 0x80000000;
df58fc94 7378 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7379 }
7380 if (mips_trap)
7381 {
df58fc94 7382 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7383 /* We want to close the noreorder block as soon as possible, so
7384 that later insns are available for delay slot filling. */
7d10b47d 7385 end_noreorder ();
252b5132
RH
7386 }
7387 else
7388 {
df58fc94
RS
7389 if (mips_opts.micromips)
7390 micromips_label_expr (&label_expr);
7391 else
7392 label_expr.X_add_number = 8;
7393 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7394 macro_build (NULL, "nop", "");
252b5132
RH
7395
7396 /* We want to close the noreorder block as soon as possible, so
7397 that later insns are available for delay slot filling. */
7d10b47d 7398 end_noreorder ();
252b5132 7399
df58fc94 7400 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7401 }
df58fc94
RS
7402 if (mips_opts.micromips)
7403 micromips_add_label ();
7404 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7405 break;
7406
7407 case M_DIV_3I:
7408 s = "div";
7409 s2 = "mflo";
7410 goto do_divi;
7411 case M_DIVU_3I:
7412 s = "divu";
7413 s2 = "mflo";
7414 goto do_divi;
7415 case M_REM_3I:
7416 s = "div";
7417 s2 = "mfhi";
7418 goto do_divi;
7419 case M_REMU_3I:
7420 s = "divu";
7421 s2 = "mfhi";
7422 goto do_divi;
7423 case M_DDIV_3I:
7424 dbl = 1;
7425 s = "ddiv";
7426 s2 = "mflo";
7427 goto do_divi;
7428 case M_DDIVU_3I:
7429 dbl = 1;
7430 s = "ddivu";
7431 s2 = "mflo";
7432 goto do_divi;
7433 case M_DREM_3I:
7434 dbl = 1;
7435 s = "ddiv";
7436 s2 = "mfhi";
7437 goto do_divi;
7438 case M_DREMU_3I:
7439 dbl = 1;
7440 s = "ddivu";
7441 s2 = "mfhi";
7442 do_divi:
7443 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7444 {
7445 as_warn (_("Divide by zero."));
7446 if (mips_trap)
df58fc94 7447 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7448 else
df58fc94 7449 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7450 break;
252b5132
RH
7451 }
7452 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7453 {
7454 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7455 move_register (dreg, sreg);
252b5132 7456 else
c80c840e 7457 move_register (dreg, ZERO);
8fc2e39e 7458 break;
252b5132
RH
7459 }
7460 if (imm_expr.X_op == O_constant
7461 && imm_expr.X_add_number == -1
7462 && s[strlen (s) - 1] != 'u')
7463 {
7464 if (strcmp (s2, "mflo") == 0)
7465 {
67c0d1eb 7466 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7467 }
7468 else
c80c840e 7469 move_register (dreg, ZERO);
8fc2e39e 7470 break;
252b5132
RH
7471 }
7472
8fc2e39e 7473 used_at = 1;
67c0d1eb
RS
7474 load_register (AT, &imm_expr, dbl);
7475 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7476 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7477 break;
7478
7479 case M_DIVU_3:
7480 s = "divu";
7481 s2 = "mflo";
7482 goto do_divu3;
7483 case M_REMU_3:
7484 s = "divu";
7485 s2 = "mfhi";
7486 goto do_divu3;
7487 case M_DDIVU_3:
7488 s = "ddivu";
7489 s2 = "mflo";
7490 goto do_divu3;
7491 case M_DREMU_3:
7492 s = "ddivu";
7493 s2 = "mfhi";
7494 do_divu3:
7d10b47d 7495 start_noreorder ();
252b5132
RH
7496 if (mips_trap)
7497 {
df58fc94 7498 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7499 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7500 /* We want to close the noreorder block as soon as possible, so
7501 that later insns are available for delay slot filling. */
7d10b47d 7502 end_noreorder ();
252b5132
RH
7503 }
7504 else
7505 {
df58fc94
RS
7506 if (mips_opts.micromips)
7507 micromips_label_expr (&label_expr);
7508 else
7509 label_expr.X_add_number = 8;
7510 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7511 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7512
7513 /* We want to close the noreorder block as soon as possible, so
7514 that later insns are available for delay slot filling. */
7d10b47d 7515 end_noreorder ();
df58fc94
RS
7516 macro_build (NULL, "break", BRK_FMT, 7);
7517 if (mips_opts.micromips)
7518 micromips_add_label ();
252b5132 7519 }
df58fc94 7520 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7521 break;
252b5132 7522
1abe91b1
MR
7523 case M_DLCA_AB:
7524 dbl = 1;
7525 case M_LCA_AB:
7526 call = 1;
7527 goto do_la;
252b5132
RH
7528 case M_DLA_AB:
7529 dbl = 1;
7530 case M_LA_AB:
1abe91b1 7531 do_la:
252b5132
RH
7532 /* Load the address of a symbol into a register. If breg is not
7533 zero, we then add a base register to it. */
7534
3bec30a8
TS
7535 if (dbl && HAVE_32BIT_GPRS)
7536 as_warn (_("dla used to load 32-bit register"));
7537
90ecf173 7538 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7539 as_warn (_("la used to load 64-bit address"));
7540
0c11417f
MR
7541 if (offset_expr.X_op == O_constant
7542 && offset_expr.X_add_number >= -0x8000
7543 && offset_expr.X_add_number < 0x8000)
7544 {
aed1a261 7545 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7546 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7547 break;
0c11417f
MR
7548 }
7549
741fe287 7550 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7551 {
7552 tempreg = AT;
7553 used_at = 1;
7554 }
7555 else
7556 {
7557 tempreg = treg;
afdbd6d0
CD
7558 }
7559
252b5132
RH
7560 if (offset_expr.X_op != O_symbol
7561 && offset_expr.X_op != O_constant)
7562 {
f71d0d44 7563 as_bad (_("Expression too complex"));
252b5132
RH
7564 offset_expr.X_op = O_constant;
7565 }
7566
252b5132 7567 if (offset_expr.X_op == O_constant)
aed1a261 7568 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7569 else if (mips_pic == NO_PIC)
7570 {
d6bc6245 7571 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7572 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7573 Otherwise we want
7574 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7575 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7576 If we have a constant, we need two instructions anyhow,
d6bc6245 7577 so we may as well always use the latter form.
76b3015f 7578
6caf9ef4
TS
7579 With 64bit address space and a usable $at we want
7580 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7581 lui $at,<sym> (BFD_RELOC_HI16_S)
7582 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7583 daddiu $at,<sym> (BFD_RELOC_LO16)
7584 dsll32 $tempreg,0
7585 daddu $tempreg,$tempreg,$at
7586
7587 If $at is already in use, we use a path which is suboptimal
7588 on superscalar processors.
7589 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7590 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7591 dsll $tempreg,16
7592 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7593 dsll $tempreg,16
7594 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7595
7596 For GP relative symbols in 64bit address space we can use
7597 the same sequence as in 32bit address space. */
aed1a261 7598 if (HAVE_64BIT_SYMBOLS)
252b5132 7599 {
6caf9ef4
TS
7600 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7601 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7602 {
7603 relax_start (offset_expr.X_add_symbol);
7604 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7605 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7606 relax_switch ();
7607 }
d6bc6245 7608
741fe287 7609 if (used_at == 0 && mips_opts.at)
98d3f06f 7610 {
df58fc94 7611 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7612 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7613 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7614 AT, BFD_RELOC_HI16_S);
67c0d1eb 7615 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7616 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7617 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7618 AT, AT, BFD_RELOC_LO16);
df58fc94 7619 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7620 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7621 used_at = 1;
7622 }
7623 else
7624 {
df58fc94 7625 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7626 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7627 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7628 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7629 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7630 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7631 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7632 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7633 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7634 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7635 }
6caf9ef4
TS
7636
7637 if (mips_relax.sequence)
7638 relax_end ();
98d3f06f
KH
7639 }
7640 else
7641 {
7642 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7643 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7644 {
4d7206a2 7645 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7646 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7647 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7648 relax_switch ();
98d3f06f 7649 }
6943caf0 7650 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7651 as_bad (_("Offset too large"));
67c0d1eb
RS
7652 macro_build_lui (&offset_expr, tempreg);
7653 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7654 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7655 if (mips_relax.sequence)
7656 relax_end ();
98d3f06f 7657 }
252b5132 7658 }
0a44bf69 7659 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7660 {
9117d219
NC
7661 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7662
252b5132
RH
7663 /* If this is a reference to an external symbol, and there
7664 is no constant, we want
7665 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7666 or for lca or if tempreg is PIC_CALL_REG
9117d219 7667 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7668 For a local symbol, we want
7669 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7670 nop
7671 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7672
7673 If we have a small constant, and this is a reference to
7674 an external symbol, we want
7675 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7676 nop
7677 addiu $tempreg,$tempreg,<constant>
7678 For a local symbol, we want the same instruction
7679 sequence, but we output a BFD_RELOC_LO16 reloc on the
7680 addiu instruction.
7681
7682 If we have a large constant, and this is a reference to
7683 an external symbol, we want
7684 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7685 lui $at,<hiconstant>
7686 addiu $at,$at,<loconstant>
7687 addu $tempreg,$tempreg,$at
7688 For a local symbol, we want the same instruction
7689 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7690 addiu instruction.
ed6fb7bd
SC
7691 */
7692
4d7206a2 7693 if (offset_expr.X_add_number == 0)
252b5132 7694 {
0a44bf69
RS
7695 if (mips_pic == SVR4_PIC
7696 && breg == 0
7697 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7698 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7699
7700 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7701 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7702 lw_reloc_type, mips_gp_register);
4d7206a2 7703 if (breg != 0)
252b5132
RH
7704 {
7705 /* We're going to put in an addu instruction using
7706 tempreg, so we may as well insert the nop right
7707 now. */
269137b2 7708 load_delay_nop ();
252b5132 7709 }
4d7206a2 7710 relax_switch ();
67c0d1eb
RS
7711 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7712 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7713 load_delay_nop ();
67c0d1eb
RS
7714 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7715 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7716 relax_end ();
252b5132
RH
7717 /* FIXME: If breg == 0, and the next instruction uses
7718 $tempreg, then if this variant case is used an extra
7719 nop will be generated. */
7720 }
4d7206a2
RS
7721 else if (offset_expr.X_add_number >= -0x8000
7722 && offset_expr.X_add_number < 0x8000)
252b5132 7723 {
67c0d1eb 7724 load_got_offset (tempreg, &offset_expr);
269137b2 7725 load_delay_nop ();
67c0d1eb 7726 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7727 }
7728 else
7729 {
4d7206a2
RS
7730 expr1.X_add_number = offset_expr.X_add_number;
7731 offset_expr.X_add_number =
43c0598f 7732 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7733 load_got_offset (tempreg, &offset_expr);
f6a22291 7734 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7735 /* If we are going to add in a base register, and the
7736 target register and the base register are the same,
7737 then we are using AT as a temporary register. Since
7738 we want to load the constant into AT, we add our
7739 current AT (from the global offset table) and the
7740 register into the register now, and pretend we were
7741 not using a base register. */
67c0d1eb 7742 if (breg == treg)
252b5132 7743 {
269137b2 7744 load_delay_nop ();
67c0d1eb 7745 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7746 treg, AT, breg);
252b5132
RH
7747 breg = 0;
7748 tempreg = treg;
252b5132 7749 }
f6a22291 7750 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7751 used_at = 1;
7752 }
7753 }
0a44bf69 7754 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7755 {
67c0d1eb 7756 int add_breg_early = 0;
f5040a92
AO
7757
7758 /* If this is a reference to an external, and there is no
7759 constant, or local symbol (*), with or without a
7760 constant, we want
7761 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7762 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7763 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7764
7765 If we have a small constant, and this is a reference to
7766 an external symbol, we want
7767 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7768 addiu $tempreg,$tempreg,<constant>
7769
7770 If we have a large constant, and this is a reference to
7771 an external symbol, we want
7772 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7773 lui $at,<hiconstant>
7774 addiu $at,$at,<loconstant>
7775 addu $tempreg,$tempreg,$at
7776
7777 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7778 local symbols, even though it introduces an additional
7779 instruction. */
7780
f5040a92
AO
7781 if (offset_expr.X_add_number)
7782 {
4d7206a2 7783 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7784 offset_expr.X_add_number = 0;
7785
4d7206a2 7786 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7787 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7788 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7789
7790 if (expr1.X_add_number >= -0x8000
7791 && expr1.X_add_number < 0x8000)
7792 {
67c0d1eb
RS
7793 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7794 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7795 }
ecd13cd3 7796 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7797 {
f5040a92
AO
7798 /* If we are going to add in a base register, and the
7799 target register and the base register are the same,
7800 then we are using AT as a temporary register. Since
7801 we want to load the constant into AT, we add our
7802 current AT (from the global offset table) and the
7803 register into the register now, and pretend we were
7804 not using a base register. */
7805 if (breg != treg)
7806 dreg = tempreg;
7807 else
7808 {
9c2799c2 7809 gas_assert (tempreg == AT);
67c0d1eb
RS
7810 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7811 treg, AT, breg);
f5040a92 7812 dreg = treg;
67c0d1eb 7813 add_breg_early = 1;
f5040a92
AO
7814 }
7815
f6a22291 7816 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7817 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7818 dreg, dreg, AT);
f5040a92 7819
f5040a92
AO
7820 used_at = 1;
7821 }
7822 else
7823 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7824
4d7206a2 7825 relax_switch ();
f5040a92
AO
7826 offset_expr.X_add_number = expr1.X_add_number;
7827
67c0d1eb
RS
7828 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7829 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7830 if (add_breg_early)
f5040a92 7831 {
67c0d1eb 7832 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7833 treg, tempreg, breg);
f5040a92
AO
7834 breg = 0;
7835 tempreg = treg;
7836 }
4d7206a2 7837 relax_end ();
f5040a92 7838 }
4d7206a2 7839 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7840 {
4d7206a2 7841 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7842 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7843 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7844 relax_switch ();
67c0d1eb
RS
7845 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7846 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7847 relax_end ();
f5040a92 7848 }
4d7206a2 7849 else
f5040a92 7850 {
67c0d1eb
RS
7851 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7852 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7853 }
7854 }
0a44bf69 7855 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7856 {
67c0d1eb 7857 int gpdelay;
9117d219
NC
7858 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7859 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7860 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7861
7862 /* This is the large GOT case. If this is a reference to an
7863 external symbol, and there is no constant, we want
7864 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7865 addu $tempreg,$tempreg,$gp
7866 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7867 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7868 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7869 addu $tempreg,$tempreg,$gp
7870 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7871 For a local symbol, we want
7872 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7873 nop
7874 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7875
7876 If we have a small constant, and this is a reference to
7877 an external symbol, we want
7878 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7879 addu $tempreg,$tempreg,$gp
7880 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7881 nop
7882 addiu $tempreg,$tempreg,<constant>
7883 For a local symbol, we want
7884 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7885 nop
7886 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7887
7888 If we have a large constant, and this is a reference to
7889 an external symbol, we want
7890 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7891 addu $tempreg,$tempreg,$gp
7892 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7893 lui $at,<hiconstant>
7894 addiu $at,$at,<loconstant>
7895 addu $tempreg,$tempreg,$at
7896 For a local symbol, we want
7897 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7898 lui $at,<hiconstant>
7899 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7900 addu $tempreg,$tempreg,$at
f5040a92 7901 */
438c16b8 7902
252b5132
RH
7903 expr1.X_add_number = offset_expr.X_add_number;
7904 offset_expr.X_add_number = 0;
4d7206a2 7905 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7906 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7907 if (expr1.X_add_number == 0 && breg == 0
7908 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7909 {
7910 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7911 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7912 }
df58fc94 7913 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7914 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7915 tempreg, tempreg, mips_gp_register);
67c0d1eb 7916 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7917 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7918 if (expr1.X_add_number == 0)
7919 {
67c0d1eb 7920 if (breg != 0)
252b5132
RH
7921 {
7922 /* We're going to put in an addu instruction using
7923 tempreg, so we may as well insert the nop right
7924 now. */
269137b2 7925 load_delay_nop ();
252b5132 7926 }
252b5132
RH
7927 }
7928 else if (expr1.X_add_number >= -0x8000
7929 && expr1.X_add_number < 0x8000)
7930 {
269137b2 7931 load_delay_nop ();
67c0d1eb 7932 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7933 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7934 }
7935 else
7936 {
252b5132
RH
7937 /* If we are going to add in a base register, and the
7938 target register and the base register are the same,
7939 then we are using AT as a temporary register. Since
7940 we want to load the constant into AT, we add our
7941 current AT (from the global offset table) and the
7942 register into the register now, and pretend we were
7943 not using a base register. */
7944 if (breg != treg)
67c0d1eb 7945 dreg = tempreg;
252b5132
RH
7946 else
7947 {
9c2799c2 7948 gas_assert (tempreg == AT);
269137b2 7949 load_delay_nop ();
67c0d1eb 7950 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7951 treg, AT, breg);
252b5132 7952 dreg = treg;
252b5132
RH
7953 }
7954
f6a22291 7955 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7956 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7957
252b5132
RH
7958 used_at = 1;
7959 }
43c0598f 7960 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7961 relax_switch ();
252b5132 7962
67c0d1eb 7963 if (gpdelay)
252b5132
RH
7964 {
7965 /* This is needed because this instruction uses $gp, but
f5040a92 7966 the first instruction on the main stream does not. */
67c0d1eb 7967 macro_build (NULL, "nop", "");
252b5132 7968 }
ed6fb7bd 7969
67c0d1eb
RS
7970 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7971 local_reloc_type, mips_gp_register);
f5040a92 7972 if (expr1.X_add_number >= -0x8000
252b5132
RH
7973 && expr1.X_add_number < 0x8000)
7974 {
269137b2 7975 load_delay_nop ();
67c0d1eb
RS
7976 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7977 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7978 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7979 register, the external symbol case ended with a load,
7980 so if the symbol turns out to not be external, and
7981 the next instruction uses tempreg, an unnecessary nop
7982 will be inserted. */
252b5132
RH
7983 }
7984 else
7985 {
7986 if (breg == treg)
7987 {
7988 /* We must add in the base register now, as in the
f5040a92 7989 external symbol case. */
9c2799c2 7990 gas_assert (tempreg == AT);
269137b2 7991 load_delay_nop ();
67c0d1eb 7992 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7993 treg, AT, breg);
252b5132
RH
7994 tempreg = treg;
7995 /* We set breg to 0 because we have arranged to add
f5040a92 7996 it in in both cases. */
252b5132
RH
7997 breg = 0;
7998 }
7999
67c0d1eb
RS
8000 macro_build_lui (&expr1, AT);
8001 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8002 AT, AT, BFD_RELOC_LO16);
67c0d1eb 8003 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8004 tempreg, tempreg, AT);
8fc2e39e 8005 used_at = 1;
252b5132 8006 }
4d7206a2 8007 relax_end ();
252b5132 8008 }
0a44bf69 8009 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8010 {
f5040a92
AO
8011 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
8012 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 8013 int add_breg_early = 0;
f5040a92
AO
8014
8015 /* This is the large GOT case. If this is a reference to an
8016 external symbol, and there is no constant, we want
8017 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8018 add $tempreg,$tempreg,$gp
8019 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 8020 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
8021 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8022 add $tempreg,$tempreg,$gp
8023 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
8024
8025 If we have a small constant, and this is a reference to
8026 an external symbol, we want
8027 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8028 add $tempreg,$tempreg,$gp
8029 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8030 addi $tempreg,$tempreg,<constant>
8031
8032 If we have a large constant, and this is a reference to
8033 an external symbol, we want
8034 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8035 addu $tempreg,$tempreg,$gp
8036 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8037 lui $at,<hiconstant>
8038 addi $at,$at,<loconstant>
8039 add $tempreg,$tempreg,$at
8040
8041 If we have NewABI, and we know it's a local symbol, we want
8042 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8043 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
8044 otherwise we have to resort to GOT_HI16/GOT_LO16. */
8045
4d7206a2 8046 relax_start (offset_expr.X_add_symbol);
f5040a92 8047
4d7206a2 8048 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8049 offset_expr.X_add_number = 0;
8050
1abe91b1
MR
8051 if (expr1.X_add_number == 0 && breg == 0
8052 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
8053 {
8054 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
8055 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
8056 }
df58fc94 8057 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 8058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8059 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
8060 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8061 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
8062
8063 if (expr1.X_add_number == 0)
4d7206a2 8064 ;
f5040a92
AO
8065 else if (expr1.X_add_number >= -0x8000
8066 && expr1.X_add_number < 0x8000)
8067 {
67c0d1eb 8068 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8069 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 8070 }
ecd13cd3 8071 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 8072 {
f5040a92
AO
8073 /* If we are going to add in a base register, and the
8074 target register and the base register are the same,
8075 then we are using AT as a temporary register. Since
8076 we want to load the constant into AT, we add our
8077 current AT (from the global offset table) and the
8078 register into the register now, and pretend we were
8079 not using a base register. */
8080 if (breg != treg)
8081 dreg = tempreg;
8082 else
8083 {
9c2799c2 8084 gas_assert (tempreg == AT);
67c0d1eb 8085 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8086 treg, AT, breg);
f5040a92 8087 dreg = treg;
67c0d1eb 8088 add_breg_early = 1;
f5040a92
AO
8089 }
8090
f6a22291 8091 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 8092 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 8093
f5040a92
AO
8094 used_at = 1;
8095 }
8096 else
8097 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
8098
4d7206a2 8099 relax_switch ();
f5040a92 8100 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8101 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8102 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8103 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8104 tempreg, BFD_RELOC_MIPS_GOT_OFST);
8105 if (add_breg_early)
f5040a92 8106 {
67c0d1eb 8107 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8108 treg, tempreg, breg);
f5040a92
AO
8109 breg = 0;
8110 tempreg = treg;
8111 }
4d7206a2 8112 relax_end ();
f5040a92 8113 }
252b5132
RH
8114 else
8115 abort ();
8116
8117 if (breg != 0)
aed1a261 8118 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
8119 break;
8120
52b6b6b9 8121 case M_MSGSND:
df58fc94 8122 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8123 {
8124 unsigned long temp = (treg << 16) | (0x01);
8125 macro_build (NULL, "c2", "C", temp);
8126 }
c7af4273 8127 break;
52b6b6b9
JM
8128
8129 case M_MSGLD:
df58fc94 8130 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8131 {
8132 unsigned long temp = (0x02);
8133 macro_build (NULL, "c2", "C", temp);
8134 }
c7af4273 8135 break;
52b6b6b9
JM
8136
8137 case M_MSGLD_T:
df58fc94 8138 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8139 {
8140 unsigned long temp = (treg << 16) | (0x02);
8141 macro_build (NULL, "c2", "C", temp);
8142 }
c7af4273 8143 break;
52b6b6b9
JM
8144
8145 case M_MSGWAIT:
df58fc94 8146 gas_assert (!mips_opts.micromips);
52b6b6b9 8147 macro_build (NULL, "c2", "C", 3);
c7af4273 8148 break;
52b6b6b9
JM
8149
8150 case M_MSGWAIT_T:
df58fc94 8151 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8152 {
8153 unsigned long temp = (treg << 16) | 0x03;
8154 macro_build (NULL, "c2", "C", temp);
8155 }
c7af4273 8156 break;
52b6b6b9 8157
252b5132
RH
8158 case M_J_A:
8159 /* The j instruction may not be used in PIC code, since it
8160 requires an absolute address. We convert it to a b
8161 instruction. */
8162 if (mips_pic == NO_PIC)
67c0d1eb 8163 macro_build (&offset_expr, "j", "a");
252b5132 8164 else
67c0d1eb 8165 macro_build (&offset_expr, "b", "p");
8fc2e39e 8166 break;
252b5132
RH
8167
8168 /* The jal instructions must be handled as macros because when
8169 generating PIC code they expand to multi-instruction
8170 sequences. Normally they are simple instructions. */
df58fc94
RS
8171 case M_JALS_1:
8172 dreg = RA;
8173 /* Fall through. */
8174 case M_JALS_2:
8175 gas_assert (mips_opts.micromips);
833794fc
MR
8176 if (mips_opts.insn32)
8177 {
8178 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8179 break;
8180 }
df58fc94
RS
8181 jals = 1;
8182 goto jal;
252b5132
RH
8183 case M_JAL_1:
8184 dreg = RA;
8185 /* Fall through. */
8186 case M_JAL_2:
df58fc94 8187 jal:
3e722fb5 8188 if (mips_pic == NO_PIC)
df58fc94
RS
8189 {
8190 s = jals ? "jalrs" : "jalr";
e64af278 8191 if (mips_opts.micromips
833794fc 8192 && !mips_opts.insn32
e64af278
MR
8193 && dreg == RA
8194 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8195 macro_build (NULL, s, "mj", sreg);
8196 else
8197 macro_build (NULL, s, JALR_FMT, dreg, sreg);
8198 }
0a44bf69 8199 else
252b5132 8200 {
df58fc94
RS
8201 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
8202 && mips_cprestore_offset >= 0);
8203
252b5132
RH
8204 if (sreg != PIC_CALL_REG)
8205 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 8206
833794fc
MR
8207 s = ((mips_opts.micromips
8208 && !mips_opts.insn32
8209 && (!mips_opts.noreorder || cprestore))
df58fc94 8210 ? "jalrs" : "jalr");
e64af278 8211 if (mips_opts.micromips
833794fc 8212 && !mips_opts.insn32
e64af278
MR
8213 && dreg == RA
8214 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8215 macro_build (NULL, s, "mj", sreg);
8216 else
8217 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 8218 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 8219 {
6478892d
TS
8220 if (mips_cprestore_offset < 0)
8221 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8222 else
8223 {
90ecf173 8224 if (!mips_frame_reg_valid)
7a621144
DJ
8225 {
8226 as_warn (_("No .frame pseudo-op used in PIC code"));
8227 /* Quiet this warning. */
8228 mips_frame_reg_valid = 1;
8229 }
90ecf173 8230 if (!mips_cprestore_valid)
7a621144
DJ
8231 {
8232 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8233 /* Quiet this warning. */
8234 mips_cprestore_valid = 1;
8235 }
d3fca0b5
MR
8236 if (mips_opts.noreorder)
8237 macro_build (NULL, "nop", "");
6478892d 8238 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8239 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8240 mips_gp_register,
256ab948
TS
8241 mips_frame_reg,
8242 HAVE_64BIT_ADDRESSES);
6478892d 8243 }
252b5132
RH
8244 }
8245 }
252b5132 8246
8fc2e39e 8247 break;
252b5132 8248
df58fc94
RS
8249 case M_JALS_A:
8250 gas_assert (mips_opts.micromips);
833794fc
MR
8251 if (mips_opts.insn32)
8252 {
8253 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8254 break;
8255 }
df58fc94
RS
8256 jals = 1;
8257 /* Fall through. */
252b5132
RH
8258 case M_JAL_A:
8259 if (mips_pic == NO_PIC)
df58fc94 8260 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
8261 else if (mips_pic == SVR4_PIC)
8262 {
8263 /* If this is a reference to an external symbol, and we are
8264 using a small GOT, we want
8265 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
8266 nop
f9419b05 8267 jalr $ra,$25
252b5132
RH
8268 nop
8269 lw $gp,cprestore($sp)
8270 The cprestore value is set using the .cprestore
8271 pseudo-op. If we are using a big GOT, we want
8272 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8273 addu $25,$25,$gp
8274 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
8275 nop
f9419b05 8276 jalr $ra,$25
252b5132
RH
8277 nop
8278 lw $gp,cprestore($sp)
8279 If the symbol is not external, we want
8280 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8281 nop
8282 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 8283 jalr $ra,$25
252b5132 8284 nop
438c16b8 8285 lw $gp,cprestore($sp)
f5040a92
AO
8286
8287 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
8288 sequences above, minus nops, unless the symbol is local,
8289 which enables us to use GOT_PAGE/GOT_OFST (big got) or
8290 GOT_DISP. */
438c16b8 8291 if (HAVE_NEWABI)
252b5132 8292 {
90ecf173 8293 if (!mips_big_got)
f5040a92 8294 {
4d7206a2 8295 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8296 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8297 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 8298 mips_gp_register);
4d7206a2 8299 relax_switch ();
67c0d1eb
RS
8300 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8301 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
8302 mips_gp_register);
8303 relax_end ();
f5040a92
AO
8304 }
8305 else
8306 {
4d7206a2 8307 relax_start (offset_expr.X_add_symbol);
df58fc94 8308 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8309 BFD_RELOC_MIPS_CALL_HI16);
8310 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8311 PIC_CALL_REG, mips_gp_register);
8312 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8313 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8314 PIC_CALL_REG);
4d7206a2 8315 relax_switch ();
67c0d1eb
RS
8316 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8317 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8318 mips_gp_register);
8319 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8320 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 8321 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8322 relax_end ();
f5040a92 8323 }
684022ea 8324
df58fc94 8325 macro_build_jalr (&offset_expr, 0);
252b5132
RH
8326 }
8327 else
8328 {
4d7206a2 8329 relax_start (offset_expr.X_add_symbol);
90ecf173 8330 if (!mips_big_got)
438c16b8 8331 {
67c0d1eb
RS
8332 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8333 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 8334 mips_gp_register);
269137b2 8335 load_delay_nop ();
4d7206a2 8336 relax_switch ();
438c16b8 8337 }
252b5132 8338 else
252b5132 8339 {
67c0d1eb
RS
8340 int gpdelay;
8341
8342 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8343 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8344 BFD_RELOC_MIPS_CALL_HI16);
8345 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8346 PIC_CALL_REG, mips_gp_register);
8347 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8348 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8349 PIC_CALL_REG);
269137b2 8350 load_delay_nop ();
4d7206a2 8351 relax_switch ();
67c0d1eb
RS
8352 if (gpdelay)
8353 macro_build (NULL, "nop", "");
252b5132 8354 }
67c0d1eb
RS
8355 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8356 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8357 mips_gp_register);
269137b2 8358 load_delay_nop ();
67c0d1eb
RS
8359 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8360 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8361 relax_end ();
df58fc94 8362 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8363
6478892d
TS
8364 if (mips_cprestore_offset < 0)
8365 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8366 else
8367 {
90ecf173 8368 if (!mips_frame_reg_valid)
7a621144
DJ
8369 {
8370 as_warn (_("No .frame pseudo-op used in PIC code"));
8371 /* Quiet this warning. */
8372 mips_frame_reg_valid = 1;
8373 }
90ecf173 8374 if (!mips_cprestore_valid)
7a621144
DJ
8375 {
8376 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8377 /* Quiet this warning. */
8378 mips_cprestore_valid = 1;
8379 }
6478892d 8380 if (mips_opts.noreorder)
67c0d1eb 8381 macro_build (NULL, "nop", "");
6478892d 8382 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8383 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8384 mips_gp_register,
256ab948
TS
8385 mips_frame_reg,
8386 HAVE_64BIT_ADDRESSES);
6478892d 8387 }
252b5132
RH
8388 }
8389 }
0a44bf69
RS
8390 else if (mips_pic == VXWORKS_PIC)
8391 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8392 else
8393 abort ();
8394
8fc2e39e 8395 break;
252b5132 8396
7f3c4072
CM
8397 case M_LBUE_AB:
8398 ab = 1;
8399 case M_LBUE_OB:
8400 s = "lbue";
8401 fmt = "t,+j(b)";
8402 offbits = 9;
8403 goto ld_st;
8404 case M_LHUE_AB:
8405 ab = 1;
8406 case M_LHUE_OB:
8407 s = "lhue";
8408 fmt = "t,+j(b)";
8409 offbits = 9;
8410 goto ld_st;
8411 case M_LBE_AB:
8412 ab = 1;
8413 case M_LBE_OB:
8414 s = "lbe";
8415 fmt = "t,+j(b)";
8416 offbits = 9;
8417 goto ld_st;
8418 case M_LHE_AB:
8419 ab = 1;
8420 case M_LHE_OB:
8421 s = "lhe";
8422 fmt = "t,+j(b)";
8423 offbits = 9;
8424 goto ld_st;
8425 case M_LLE_AB:
8426 ab = 1;
8427 case M_LLE_OB:
8428 s = "lle";
8429 fmt = "t,+j(b)";
8430 offbits = 9;
8431 goto ld_st;
8432 case M_LWE_AB:
8433 ab = 1;
8434 case M_LWE_OB:
8435 s = "lwe";
8436 fmt = "t,+j(b)";
8437 offbits = 9;
8438 goto ld_st;
8439 case M_LWLE_AB:
8440 ab = 1;
8441 case M_LWLE_OB:
8442 s = "lwle";
8443 fmt = "t,+j(b)";
8444 offbits = 9;
8445 goto ld_st;
8446 case M_LWRE_AB:
8447 ab = 1;
8448 case M_LWRE_OB:
8449 s = "lwre";
8450 fmt = "t,+j(b)";
8451 offbits = 9;
8452 goto ld_st;
8453 case M_SBE_AB:
8454 ab = 1;
8455 case M_SBE_OB:
8456 s = "sbe";
8457 fmt = "t,+j(b)";
8458 offbits = 9;
8459 goto ld_st;
8460 case M_SCE_AB:
8461 ab = 1;
8462 case M_SCE_OB:
8463 s = "sce";
8464 fmt = "t,+j(b)";
8465 offbits = 9;
8466 goto ld_st;
8467 case M_SHE_AB:
8468 ab = 1;
8469 case M_SHE_OB:
8470 s = "she";
8471 fmt = "t,+j(b)";
8472 offbits = 9;
8473 goto ld_st;
8474 case M_SWE_AB:
8475 ab = 1;
8476 case M_SWE_OB:
8477 s = "swe";
8478 fmt = "t,+j(b)";
8479 offbits = 9;
8480 goto ld_st;
8481 case M_SWLE_AB:
8482 ab = 1;
8483 case M_SWLE_OB:
8484 s = "swle";
8485 fmt = "t,+j(b)";
8486 offbits = 9;
8487 goto ld_st;
8488 case M_SWRE_AB:
8489 ab = 1;
8490 case M_SWRE_OB:
8491 s = "swre";
8492 fmt = "t,+j(b)";
8493 offbits = 9;
8494 goto ld_st;
dec0624d
MR
8495 case M_ACLR_AB:
8496 ab = 1;
8497 case M_ACLR_OB:
8498 s = "aclr";
8499 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8500 fmt = "\\,~(b)";
7f3c4072 8501 offbits = 12;
dec0624d
MR
8502 goto ld_st;
8503 case M_ASET_AB:
8504 ab = 1;
8505 case M_ASET_OB:
8506 s = "aset";
8507 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8508 fmt = "\\,~(b)";
7f3c4072 8509 offbits = 12;
dec0624d 8510 goto ld_st;
252b5132 8511 case M_LB_AB:
df58fc94 8512 ab = 1;
252b5132 8513 s = "lb";
df58fc94 8514 fmt = "t,o(b)";
252b5132
RH
8515 goto ld;
8516 case M_LBU_AB:
df58fc94 8517 ab = 1;
252b5132 8518 s = "lbu";
df58fc94 8519 fmt = "t,o(b)";
252b5132
RH
8520 goto ld;
8521 case M_LH_AB:
df58fc94 8522 ab = 1;
252b5132 8523 s = "lh";
df58fc94 8524 fmt = "t,o(b)";
252b5132
RH
8525 goto ld;
8526 case M_LHU_AB:
df58fc94 8527 ab = 1;
252b5132 8528 s = "lhu";
df58fc94 8529 fmt = "t,o(b)";
252b5132
RH
8530 goto ld;
8531 case M_LW_AB:
df58fc94 8532 ab = 1;
252b5132 8533 s = "lw";
df58fc94 8534 fmt = "t,o(b)";
252b5132
RH
8535 goto ld;
8536 case M_LWC0_AB:
df58fc94
RS
8537 ab = 1;
8538 gas_assert (!mips_opts.micromips);
252b5132 8539 s = "lwc0";
df58fc94 8540 fmt = "E,o(b)";
bdaaa2e1 8541 /* Itbl support may require additional care here. */
252b5132 8542 coproc = 1;
df58fc94 8543 goto ld_st;
252b5132 8544 case M_LWC1_AB:
df58fc94 8545 ab = 1;
252b5132 8546 s = "lwc1";
df58fc94 8547 fmt = "T,o(b)";
bdaaa2e1 8548 /* Itbl support may require additional care here. */
252b5132 8549 coproc = 1;
df58fc94 8550 goto ld_st;
252b5132 8551 case M_LWC2_AB:
df58fc94
RS
8552 ab = 1;
8553 case M_LWC2_OB:
252b5132 8554 s = "lwc2";
df58fc94 8555 fmt = COP12_FMT;
7f3c4072 8556 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8557 /* Itbl support may require additional care here. */
252b5132 8558 coproc = 1;
df58fc94 8559 goto ld_st;
252b5132 8560 case M_LWC3_AB:
df58fc94
RS
8561 ab = 1;
8562 gas_assert (!mips_opts.micromips);
252b5132 8563 s = "lwc3";
df58fc94 8564 fmt = "E,o(b)";
bdaaa2e1 8565 /* Itbl support may require additional care here. */
252b5132 8566 coproc = 1;
df58fc94 8567 goto ld_st;
252b5132 8568 case M_LWL_AB:
df58fc94
RS
8569 ab = 1;
8570 case M_LWL_OB:
252b5132 8571 s = "lwl";
df58fc94 8572 fmt = MEM12_FMT;
7f3c4072 8573 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8574 goto ld_st;
252b5132 8575 case M_LWR_AB:
df58fc94
RS
8576 ab = 1;
8577 case M_LWR_OB:
252b5132 8578 s = "lwr";
df58fc94 8579 fmt = MEM12_FMT;
7f3c4072 8580 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8581 goto ld_st;
252b5132 8582 case M_LDC1_AB:
df58fc94 8583 ab = 1;
252b5132 8584 s = "ldc1";
df58fc94 8585 fmt = "T,o(b)";
bdaaa2e1 8586 /* Itbl support may require additional care here. */
252b5132 8587 coproc = 1;
df58fc94 8588 goto ld_st;
252b5132 8589 case M_LDC2_AB:
df58fc94
RS
8590 ab = 1;
8591 case M_LDC2_OB:
252b5132 8592 s = "ldc2";
df58fc94 8593 fmt = COP12_FMT;
7f3c4072 8594 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8595 /* Itbl support may require additional care here. */
252b5132 8596 coproc = 1;
df58fc94 8597 goto ld_st;
c77c0862
RS
8598 case M_LQC2_AB:
8599 ab = 1;
8600 s = "lqc2";
8601 fmt = "E,o(b)";
8602 /* Itbl support may require additional care here. */
8603 coproc = 1;
8604 goto ld_st;
252b5132 8605 case M_LDC3_AB:
df58fc94 8606 ab = 1;
252b5132 8607 s = "ldc3";
df58fc94 8608 fmt = "E,o(b)";
bdaaa2e1 8609 /* Itbl support may require additional care here. */
252b5132 8610 coproc = 1;
df58fc94 8611 goto ld_st;
252b5132 8612 case M_LDL_AB:
df58fc94
RS
8613 ab = 1;
8614 case M_LDL_OB:
252b5132 8615 s = "ldl";
df58fc94 8616 fmt = MEM12_FMT;
7f3c4072 8617 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8618 goto ld_st;
252b5132 8619 case M_LDR_AB:
df58fc94
RS
8620 ab = 1;
8621 case M_LDR_OB:
252b5132 8622 s = "ldr";
df58fc94 8623 fmt = MEM12_FMT;
7f3c4072 8624 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8625 goto ld_st;
252b5132 8626 case M_LL_AB:
df58fc94
RS
8627 ab = 1;
8628 case M_LL_OB:
252b5132 8629 s = "ll";
df58fc94 8630 fmt = MEM12_FMT;
7f3c4072 8631 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8632 goto ld;
8633 case M_LLD_AB:
df58fc94
RS
8634 ab = 1;
8635 case M_LLD_OB:
252b5132 8636 s = "lld";
df58fc94 8637 fmt = MEM12_FMT;
7f3c4072 8638 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8639 goto ld;
8640 case M_LWU_AB:
df58fc94
RS
8641 ab = 1;
8642 case M_LWU_OB:
252b5132 8643 s = "lwu";
df58fc94 8644 fmt = MEM12_FMT;
7f3c4072 8645 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8646 goto ld;
8647 case M_LWP_AB:
8648 ab = 1;
8649 case M_LWP_OB:
8650 gas_assert (mips_opts.micromips);
8651 s = "lwp";
8652 fmt = "t,~(b)";
7f3c4072 8653 offbits = 12;
df58fc94
RS
8654 lp = 1;
8655 goto ld;
8656 case M_LDP_AB:
8657 ab = 1;
8658 case M_LDP_OB:
8659 gas_assert (mips_opts.micromips);
8660 s = "ldp";
8661 fmt = "t,~(b)";
7f3c4072 8662 offbits = 12;
df58fc94
RS
8663 lp = 1;
8664 goto ld;
8665 case M_LWM_AB:
8666 ab = 1;
8667 case M_LWM_OB:
8668 gas_assert (mips_opts.micromips);
8669 s = "lwm";
8670 fmt = "n,~(b)";
7f3c4072 8671 offbits = 12;
df58fc94
RS
8672 goto ld_st;
8673 case M_LDM_AB:
8674 ab = 1;
8675 case M_LDM_OB:
8676 gas_assert (mips_opts.micromips);
8677 s = "ldm";
8678 fmt = "n,~(b)";
7f3c4072 8679 offbits = 12;
df58fc94
RS
8680 goto ld_st;
8681
252b5132 8682 ld:
f19ccbda
MR
8683 /* We don't want to use $0 as tempreg. */
8684 if (breg == treg + lp || treg + lp == ZERO)
df58fc94 8685 goto ld_st;
252b5132 8686 else
df58fc94
RS
8687 tempreg = treg + lp;
8688 goto ld_noat;
8689
252b5132 8690 case M_SB_AB:
df58fc94 8691 ab = 1;
252b5132 8692 s = "sb";
df58fc94
RS
8693 fmt = "t,o(b)";
8694 goto ld_st;
252b5132 8695 case M_SH_AB:
df58fc94 8696 ab = 1;
252b5132 8697 s = "sh";
df58fc94
RS
8698 fmt = "t,o(b)";
8699 goto ld_st;
252b5132 8700 case M_SW_AB:
df58fc94 8701 ab = 1;
252b5132 8702 s = "sw";
df58fc94
RS
8703 fmt = "t,o(b)";
8704 goto ld_st;
252b5132 8705 case M_SWC0_AB:
df58fc94
RS
8706 ab = 1;
8707 gas_assert (!mips_opts.micromips);
252b5132 8708 s = "swc0";
df58fc94 8709 fmt = "E,o(b)";
bdaaa2e1 8710 /* Itbl support may require additional care here. */
252b5132 8711 coproc = 1;
df58fc94 8712 goto ld_st;
252b5132 8713 case M_SWC1_AB:
df58fc94 8714 ab = 1;
252b5132 8715 s = "swc1";
df58fc94 8716 fmt = "T,o(b)";
bdaaa2e1 8717 /* Itbl support may require additional care here. */
252b5132 8718 coproc = 1;
df58fc94 8719 goto ld_st;
252b5132 8720 case M_SWC2_AB:
df58fc94
RS
8721 ab = 1;
8722 case M_SWC2_OB:
252b5132 8723 s = "swc2";
df58fc94 8724 fmt = COP12_FMT;
7f3c4072 8725 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8726 /* Itbl support may require additional care here. */
252b5132 8727 coproc = 1;
df58fc94 8728 goto ld_st;
252b5132 8729 case M_SWC3_AB:
df58fc94
RS
8730 ab = 1;
8731 gas_assert (!mips_opts.micromips);
252b5132 8732 s = "swc3";
df58fc94 8733 fmt = "E,o(b)";
bdaaa2e1 8734 /* Itbl support may require additional care here. */
252b5132 8735 coproc = 1;
df58fc94 8736 goto ld_st;
252b5132 8737 case M_SWL_AB:
df58fc94
RS
8738 ab = 1;
8739 case M_SWL_OB:
252b5132 8740 s = "swl";
df58fc94 8741 fmt = MEM12_FMT;
7f3c4072 8742 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8743 goto ld_st;
252b5132 8744 case M_SWR_AB:
df58fc94
RS
8745 ab = 1;
8746 case M_SWR_OB:
252b5132 8747 s = "swr";
df58fc94 8748 fmt = MEM12_FMT;
7f3c4072 8749 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8750 goto ld_st;
252b5132 8751 case M_SC_AB:
df58fc94
RS
8752 ab = 1;
8753 case M_SC_OB:
252b5132 8754 s = "sc";
df58fc94 8755 fmt = MEM12_FMT;
7f3c4072 8756 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8757 goto ld_st;
252b5132 8758 case M_SCD_AB:
df58fc94
RS
8759 ab = 1;
8760 case M_SCD_OB:
252b5132 8761 s = "scd";
df58fc94 8762 fmt = MEM12_FMT;
7f3c4072 8763 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8764 goto ld_st;
d43b4baf 8765 case M_CACHE_AB:
df58fc94
RS
8766 ab = 1;
8767 case M_CACHE_OB:
d43b4baf 8768 s = "cache";
df58fc94 8769 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
8770 offbits = (mips_opts.micromips ? 12 : 16);
8771 goto ld_st;
8772 case M_CACHEE_AB:
8773 ab = 1;
8774 case M_CACHEE_OB:
8775 s = "cachee";
8776 fmt = "k,+j(b)";
8777 offbits = 9;
df58fc94 8778 goto ld_st;
3eebd5eb 8779 case M_PREF_AB:
df58fc94
RS
8780 ab = 1;
8781 case M_PREF_OB:
3eebd5eb 8782 s = "pref";
df58fc94 8783 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
8784 offbits = (mips_opts.micromips ? 12 : 16);
8785 goto ld_st;
8786 case M_PREFE_AB:
8787 ab = 1;
8788 case M_PREFE_OB:
8789 s = "prefe";
8790 fmt = "k,+j(b)";
8791 offbits = 9;
df58fc94 8792 goto ld_st;
252b5132 8793 case M_SDC1_AB:
df58fc94 8794 ab = 1;
252b5132 8795 s = "sdc1";
df58fc94 8796 fmt = "T,o(b)";
252b5132 8797 coproc = 1;
bdaaa2e1 8798 /* Itbl support may require additional care here. */
df58fc94 8799 goto ld_st;
252b5132 8800 case M_SDC2_AB:
df58fc94
RS
8801 ab = 1;
8802 case M_SDC2_OB:
252b5132 8803 s = "sdc2";
df58fc94 8804 fmt = COP12_FMT;
7f3c4072 8805 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
8806 /* Itbl support may require additional care here. */
8807 coproc = 1;
8808 goto ld_st;
8809 case M_SQC2_AB:
8810 ab = 1;
8811 s = "sqc2";
8812 fmt = "E,o(b)";
bdaaa2e1 8813 /* Itbl support may require additional care here. */
252b5132 8814 coproc = 1;
df58fc94 8815 goto ld_st;
252b5132 8816 case M_SDC3_AB:
df58fc94
RS
8817 ab = 1;
8818 gas_assert (!mips_opts.micromips);
252b5132 8819 s = "sdc3";
df58fc94 8820 fmt = "E,o(b)";
bdaaa2e1 8821 /* Itbl support may require additional care here. */
252b5132 8822 coproc = 1;
df58fc94 8823 goto ld_st;
252b5132 8824 case M_SDL_AB:
df58fc94
RS
8825 ab = 1;
8826 case M_SDL_OB:
252b5132 8827 s = "sdl";
df58fc94 8828 fmt = MEM12_FMT;
7f3c4072 8829 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8830 goto ld_st;
252b5132 8831 case M_SDR_AB:
df58fc94
RS
8832 ab = 1;
8833 case M_SDR_OB:
252b5132 8834 s = "sdr";
df58fc94 8835 fmt = MEM12_FMT;
7f3c4072 8836 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8837 goto ld_st;
8838 case M_SWP_AB:
8839 ab = 1;
8840 case M_SWP_OB:
8841 gas_assert (mips_opts.micromips);
8842 s = "swp";
8843 fmt = "t,~(b)";
7f3c4072 8844 offbits = 12;
df58fc94
RS
8845 goto ld_st;
8846 case M_SDP_AB:
8847 ab = 1;
8848 case M_SDP_OB:
8849 gas_assert (mips_opts.micromips);
8850 s = "sdp";
8851 fmt = "t,~(b)";
7f3c4072 8852 offbits = 12;
df58fc94
RS
8853 goto ld_st;
8854 case M_SWM_AB:
8855 ab = 1;
8856 case M_SWM_OB:
8857 gas_assert (mips_opts.micromips);
8858 s = "swm";
8859 fmt = "n,~(b)";
7f3c4072 8860 offbits = 12;
df58fc94
RS
8861 goto ld_st;
8862 case M_SDM_AB:
8863 ab = 1;
8864 case M_SDM_OB:
8865 gas_assert (mips_opts.micromips);
8866 s = "sdm";
8867 fmt = "n,~(b)";
7f3c4072 8868 offbits = 12;
df58fc94
RS
8869
8870 ld_st:
8fc2e39e
TS
8871 tempreg = AT;
8872 used_at = 1;
df58fc94 8873 ld_noat:
252b5132
RH
8874 if (offset_expr.X_op != O_constant
8875 && offset_expr.X_op != O_symbol)
8876 {
f71d0d44 8877 as_bad (_("Expression too complex"));
252b5132
RH
8878 offset_expr.X_op = O_constant;
8879 }
8880
2051e8c4
MR
8881 if (HAVE_32BIT_ADDRESSES
8882 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8883 {
8884 char value [32];
8885
8886 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8887 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8888 }
2051e8c4 8889
252b5132
RH
8890 /* A constant expression in PIC code can be handled just as it
8891 is in non PIC code. */
aed1a261
RS
8892 if (offset_expr.X_op == O_constant)
8893 {
df58fc94
RS
8894 int hipart = 0;
8895
842f8b2a 8896 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8897 normalize_address_expr (&expr1);
7f3c4072
CM
8898 if ((offbits == 0 || offbits == 16)
8899 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8900 {
8901 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8902 & ~(bfd_vma) 0xffff);
df58fc94
RS
8903 hipart = 1;
8904 }
7f3c4072 8905 else if (offbits == 12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
df58fc94
RS
8906 {
8907 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8908 & ~(bfd_vma) 0xfff);
8909 hipart = 1;
8910 }
7f3c4072
CM
8911 else if (offbits == 9 && !IS_SEXT_9BIT_NUM (expr1.X_add_number))
8912 {
8913 expr1.X_add_number = ((expr1.X_add_number + 0x100)
8914 & ~(bfd_vma) 0x1ff);
8915 hipart = 1;
8916 }
df58fc94
RS
8917 if (hipart)
8918 {
842f8b2a
MR
8919 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8920 if (breg != 0)
8921 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8922 tempreg, tempreg, breg);
8923 breg = tempreg;
8924 }
7f3c4072 8925 if (offbits == 0)
dd6a37e7
AP
8926 {
8927 if (offset_expr.X_add_number == 0)
8928 tempreg = breg;
8929 else
8930 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8931 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8932 macro_build (NULL, s, fmt, treg, tempreg);
8933 }
7f3c4072 8934 else if (offbits == 16)
df58fc94
RS
8935 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8936 else
8937 macro_build (NULL, s, fmt,
8938 treg, (unsigned long) offset_expr.X_add_number, breg);
8939 }
7f3c4072 8940 else if (offbits != 16)
df58fc94 8941 {
7f3c4072
CM
8942 /* The offset field is too narrow to be used for a low-part
8943 relocation, so load the whole address into the auxillary
8944 register. In the case of "A(b)" addresses, we first load
8945 absolute address "A" into the register and then add base
8946 register "b". In the case of "o(b)" addresses, we simply
8947 need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8948 offset_reloc already contains the relocations associated
8949 with "o". */
8950 if (ab)
8951 {
8952 load_address (tempreg, &offset_expr, &used_at);
8953 if (breg != 0)
8954 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8955 tempreg, tempreg, breg);
8956 }
8957 else
8958 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8959 tempreg, breg, -1,
8960 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8961 expr1.X_add_number = 0;
7f3c4072 8962 if (offbits == 0)
dd6a37e7
AP
8963 macro_build (NULL, s, fmt, treg, tempreg);
8964 else
8965 macro_build (NULL, s, fmt,
8966 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8967 }
8968 else if (mips_pic == NO_PIC)
252b5132
RH
8969 {
8970 /* If this is a reference to a GP relative symbol, and there
8971 is no base register, we want
cdf6fd85 8972 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8973 Otherwise, if there is no base register, we want
8974 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8975 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8976 If we have a constant, we need two instructions anyhow,
8977 so we always use the latter form.
8978
8979 If we have a base register, and this is a reference to a
8980 GP relative symbol, we want
8981 addu $tempreg,$breg,$gp
cdf6fd85 8982 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8983 Otherwise we want
8984 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8985 addu $tempreg,$tempreg,$breg
8986 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8987 With a constant we always use the latter case.
76b3015f 8988
d6bc6245
TS
8989 With 64bit address space and no base register and $at usable,
8990 we want
8991 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8992 lui $at,<sym> (BFD_RELOC_HI16_S)
8993 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8994 dsll32 $tempreg,0
8995 daddu $tempreg,$at
8996 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8997 If we have a base register, we want
8998 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8999 lui $at,<sym> (BFD_RELOC_HI16_S)
9000 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9001 daddu $at,$breg
9002 dsll32 $tempreg,0
9003 daddu $tempreg,$at
9004 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9005
9006 Without $at we can't generate the optimal path for superscalar
9007 processors here since this would require two temporary registers.
9008 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9009 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9010 dsll $tempreg,16
9011 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9012 dsll $tempreg,16
9013 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9014 If we have a base register, we want
9015 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9016 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9017 dsll $tempreg,16
9018 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9019 dsll $tempreg,16
9020 daddu $tempreg,$tempreg,$breg
9021 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 9022
6caf9ef4 9023 For GP relative symbols in 64bit address space we can use
aed1a261
RS
9024 the same sequence as in 32bit address space. */
9025 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9026 {
aed1a261 9027 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
9028 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9029 {
9030 relax_start (offset_expr.X_add_symbol);
9031 if (breg == 0)
9032 {
9033 macro_build (&offset_expr, s, fmt, treg,
9034 BFD_RELOC_GPREL16, mips_gp_register);
9035 }
9036 else
9037 {
9038 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9039 tempreg, breg, mips_gp_register);
9040 macro_build (&offset_expr, s, fmt, treg,
9041 BFD_RELOC_GPREL16, tempreg);
9042 }
9043 relax_switch ();
9044 }
d6bc6245 9045
741fe287 9046 if (used_at == 0 && mips_opts.at)
d6bc6245 9047 {
df58fc94 9048 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 9049 BFD_RELOC_MIPS_HIGHEST);
df58fc94 9050 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
9051 BFD_RELOC_HI16_S);
9052 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9053 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 9054 if (breg != 0)
67c0d1eb 9055 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 9056 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
9057 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
9058 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
9059 tempreg);
d6bc6245
TS
9060 used_at = 1;
9061 }
9062 else
9063 {
df58fc94 9064 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
9065 BFD_RELOC_MIPS_HIGHEST);
9066 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9067 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9068 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
9069 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9070 tempreg, BFD_RELOC_HI16_S);
df58fc94 9071 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 9072 if (breg != 0)
67c0d1eb 9073 macro_build (NULL, "daddu", "d,v,t",
17a2f251 9074 tempreg, tempreg, breg);
67c0d1eb 9075 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9076 BFD_RELOC_LO16, tempreg);
d6bc6245 9077 }
6caf9ef4
TS
9078
9079 if (mips_relax.sequence)
9080 relax_end ();
8fc2e39e 9081 break;
d6bc6245 9082 }
256ab948 9083
252b5132
RH
9084 if (breg == 0)
9085 {
67c0d1eb 9086 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9087 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9088 {
4d7206a2 9089 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9090 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
9091 mips_gp_register);
4d7206a2 9092 relax_switch ();
252b5132 9093 }
67c0d1eb
RS
9094 macro_build_lui (&offset_expr, tempreg);
9095 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9096 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
9097 if (mips_relax.sequence)
9098 relax_end ();
252b5132
RH
9099 }
9100 else
9101 {
67c0d1eb 9102 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9103 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9104 {
4d7206a2 9105 relax_start (offset_expr.X_add_symbol);
67c0d1eb 9106 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9107 tempreg, breg, mips_gp_register);
67c0d1eb 9108 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9109 BFD_RELOC_GPREL16, tempreg);
4d7206a2 9110 relax_switch ();
252b5132 9111 }
67c0d1eb
RS
9112 macro_build_lui (&offset_expr, tempreg);
9113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9114 tempreg, tempreg, breg);
67c0d1eb 9115 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9116 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
9117 if (mips_relax.sequence)
9118 relax_end ();
252b5132
RH
9119 }
9120 }
0a44bf69 9121 else if (!mips_big_got)
252b5132 9122 {
ed6fb7bd 9123 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 9124
252b5132
RH
9125 /* If this is a reference to an external symbol, we want
9126 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9127 nop
9128 <op> $treg,0($tempreg)
9129 Otherwise we want
9130 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9131 nop
9132 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9133 <op> $treg,0($tempreg)
f5040a92
AO
9134
9135 For NewABI, we want
9136 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9137 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
9138
252b5132
RH
9139 If there is a base register, we add it to $tempreg before
9140 the <op>. If there is a constant, we stick it in the
9141 <op> instruction. We don't handle constants larger than
9142 16 bits, because we have no way to load the upper 16 bits
9143 (actually, we could handle them for the subset of cases
9144 in which we are not using $at). */
9c2799c2 9145 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
9146 if (HAVE_NEWABI)
9147 {
67c0d1eb
RS
9148 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9149 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 9150 if (breg != 0)
67c0d1eb 9151 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9152 tempreg, tempreg, breg);
67c0d1eb 9153 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9154 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
9155 break;
9156 }
252b5132
RH
9157 expr1.X_add_number = offset_expr.X_add_number;
9158 offset_expr.X_add_number = 0;
9159 if (expr1.X_add_number < -0x8000
9160 || expr1.X_add_number >= 0x8000)
9161 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
9162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9163 lw_reloc_type, mips_gp_register);
269137b2 9164 load_delay_nop ();
4d7206a2
RS
9165 relax_start (offset_expr.X_add_symbol);
9166 relax_switch ();
67c0d1eb
RS
9167 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9168 tempreg, BFD_RELOC_LO16);
4d7206a2 9169 relax_end ();
252b5132 9170 if (breg != 0)
67c0d1eb 9171 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9172 tempreg, tempreg, breg);
67c0d1eb 9173 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 9174 }
0a44bf69 9175 else if (mips_big_got && !HAVE_NEWABI)
252b5132 9176 {
67c0d1eb 9177 int gpdelay;
252b5132
RH
9178
9179 /* If this is a reference to an external symbol, we want
9180 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9181 addu $tempreg,$tempreg,$gp
9182 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9183 <op> $treg,0($tempreg)
9184 Otherwise we want
9185 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9186 nop
9187 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9188 <op> $treg,0($tempreg)
9189 If there is a base register, we add it to $tempreg before
9190 the <op>. If there is a constant, we stick it in the
9191 <op> instruction. We don't handle constants larger than
9192 16 bits, because we have no way to load the upper 16 bits
9193 (actually, we could handle them for the subset of cases
f5040a92 9194 in which we are not using $at). */
9c2799c2 9195 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
9196 expr1.X_add_number = offset_expr.X_add_number;
9197 offset_expr.X_add_number = 0;
9198 if (expr1.X_add_number < -0x8000
9199 || expr1.X_add_number >= 0x8000)
9200 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9201 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9202 relax_start (offset_expr.X_add_symbol);
df58fc94 9203 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 9204 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9205 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9206 mips_gp_register);
9207 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9208 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 9209 relax_switch ();
67c0d1eb
RS
9210 if (gpdelay)
9211 macro_build (NULL, "nop", "");
9212 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9213 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9214 load_delay_nop ();
67c0d1eb
RS
9215 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9216 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9217 relax_end ();
9218
252b5132 9219 if (breg != 0)
67c0d1eb 9220 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9221 tempreg, tempreg, breg);
67c0d1eb 9222 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 9223 }
0a44bf69 9224 else if (mips_big_got && HAVE_NEWABI)
f5040a92 9225 {
f5040a92
AO
9226 /* If this is a reference to an external symbol, we want
9227 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9228 add $tempreg,$tempreg,$gp
9229 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9230 <op> $treg,<ofst>($tempreg)
9231 Otherwise, for local symbols, we want:
9232 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9233 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 9234 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 9235 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9236 offset_expr.X_add_number = 0;
9237 if (expr1.X_add_number < -0x8000
9238 || expr1.X_add_number >= 0x8000)
9239 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 9240 relax_start (offset_expr.X_add_symbol);
df58fc94 9241 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 9242 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9243 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9244 mips_gp_register);
9245 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9246 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 9247 if (breg != 0)
67c0d1eb 9248 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9249 tempreg, tempreg, breg);
67c0d1eb 9250 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 9251
4d7206a2 9252 relax_switch ();
f5040a92 9253 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9254 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9255 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 9256 if (breg != 0)
67c0d1eb 9257 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9258 tempreg, tempreg, breg);
67c0d1eb 9259 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9260 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 9261 relax_end ();
f5040a92 9262 }
252b5132
RH
9263 else
9264 abort ();
9265
252b5132
RH
9266 break;
9267
833794fc
MR
9268 case M_JRADDIUSP:
9269 gas_assert (mips_opts.micromips);
9270 gas_assert (mips_opts.insn32);
9271 start_noreorder ();
9272 macro_build (NULL, "jr", "s", RA);
9273 expr1.X_add_number = EXTRACT_OPERAND (1, IMMP, *ip) << 2;
9274 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
9275 end_noreorder ();
9276 break;
9277
9278 case M_JRC:
9279 gas_assert (mips_opts.micromips);
9280 gas_assert (mips_opts.insn32);
9281 macro_build (NULL, "jr", "s", sreg);
9282 if (mips_opts.noreorder)
9283 macro_build (NULL, "nop", "");
9284 break;
9285
252b5132
RH
9286 case M_LI:
9287 case M_LI_S:
67c0d1eb 9288 load_register (treg, &imm_expr, 0);
8fc2e39e 9289 break;
252b5132
RH
9290
9291 case M_DLI:
67c0d1eb 9292 load_register (treg, &imm_expr, 1);
8fc2e39e 9293 break;
252b5132
RH
9294
9295 case M_LI_SS:
9296 if (imm_expr.X_op == O_constant)
9297 {
8fc2e39e 9298 used_at = 1;
67c0d1eb
RS
9299 load_register (AT, &imm_expr, 0);
9300 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
9301 break;
9302 }
9303 else
9304 {
9c2799c2 9305 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
9306 && strcmp (segment_name (S_GET_SEGMENT
9307 (offset_expr.X_add_symbol)),
9308 ".lit4") == 0
9309 && offset_expr.X_add_number == 0);
67c0d1eb 9310 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 9311 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 9312 break;
252b5132
RH
9313 }
9314
9315 case M_LI_D:
ca4e0257
RS
9316 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
9317 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
9318 order 32 bits of the value and the low order 32 bits are either
9319 zero or in OFFSET_EXPR. */
252b5132
RH
9320 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9321 {
ca4e0257 9322 if (HAVE_64BIT_GPRS)
67c0d1eb 9323 load_register (treg, &imm_expr, 1);
252b5132
RH
9324 else
9325 {
9326 int hreg, lreg;
9327
9328 if (target_big_endian)
9329 {
9330 hreg = treg;
9331 lreg = treg + 1;
9332 }
9333 else
9334 {
9335 hreg = treg + 1;
9336 lreg = treg;
9337 }
9338
9339 if (hreg <= 31)
67c0d1eb 9340 load_register (hreg, &imm_expr, 0);
252b5132
RH
9341 if (lreg <= 31)
9342 {
9343 if (offset_expr.X_op == O_absent)
67c0d1eb 9344 move_register (lreg, 0);
252b5132
RH
9345 else
9346 {
9c2799c2 9347 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 9348 load_register (lreg, &offset_expr, 0);
252b5132
RH
9349 }
9350 }
9351 }
8fc2e39e 9352 break;
252b5132
RH
9353 }
9354
9355 /* We know that sym is in the .rdata section. First we get the
9356 upper 16 bits of the address. */
9357 if (mips_pic == NO_PIC)
9358 {
67c0d1eb 9359 macro_build_lui (&offset_expr, AT);
8fc2e39e 9360 used_at = 1;
252b5132 9361 }
0a44bf69 9362 else
252b5132 9363 {
67c0d1eb
RS
9364 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9365 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 9366 used_at = 1;
252b5132 9367 }
bdaaa2e1 9368
252b5132 9369 /* Now we load the register(s). */
ca4e0257 9370 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
9371 {
9372 used_at = 1;
9373 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9374 }
252b5132
RH
9375 else
9376 {
8fc2e39e 9377 used_at = 1;
67c0d1eb 9378 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 9379 if (treg != RA)
252b5132
RH
9380 {
9381 /* FIXME: How in the world do we deal with the possible
9382 overflow here? */
9383 offset_expr.X_add_number += 4;
67c0d1eb 9384 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 9385 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
9386 }
9387 }
252b5132
RH
9388 break;
9389
9390 case M_LI_DD:
ca4e0257
RS
9391 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
9392 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9393 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
9394 the value and the low order 32 bits are either zero or in
9395 OFFSET_EXPR. */
252b5132
RH
9396 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9397 {
8fc2e39e 9398 used_at = 1;
67c0d1eb 9399 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
9400 if (HAVE_64BIT_FPRS)
9401 {
9c2799c2 9402 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 9403 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 9404 }
252b5132
RH
9405 else
9406 {
67c0d1eb 9407 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 9408 if (offset_expr.X_op == O_absent)
67c0d1eb 9409 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
9410 else
9411 {
9c2799c2 9412 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
9413 load_register (AT, &offset_expr, 0);
9414 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
9415 }
9416 }
9417 break;
9418 }
9419
9c2799c2 9420 gas_assert (offset_expr.X_op == O_symbol
90ecf173 9421 && offset_expr.X_add_number == 0);
252b5132
RH
9422 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9423 if (strcmp (s, ".lit8") == 0)
9424 {
0aa27725 9425 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9426 {
67c0d1eb 9427 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 9428 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 9429 break;
252b5132 9430 }
c9914766 9431 breg = mips_gp_register;
252b5132
RH
9432 r = BFD_RELOC_MIPS_LITERAL;
9433 goto dob;
9434 }
9435 else
9436 {
9c2799c2 9437 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 9438 used_at = 1;
0a44bf69 9439 if (mips_pic != NO_PIC)
67c0d1eb
RS
9440 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9441 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
9442 else
9443 {
9444 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 9445 macro_build_lui (&offset_expr, AT);
252b5132 9446 }
bdaaa2e1 9447
0aa27725 9448 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9449 {
67c0d1eb
RS
9450 macro_build (&offset_expr, "ldc1", "T,o(b)",
9451 treg, BFD_RELOC_LO16, AT);
252b5132
RH
9452 break;
9453 }
9454 breg = AT;
9455 r = BFD_RELOC_LO16;
9456 goto dob;
9457 }
9458
9459 case M_L_DOB:
252b5132
RH
9460 /* Even on a big endian machine $fn comes before $fn+1. We have
9461 to adjust when loading from memory. */
9462 r = BFD_RELOC_LO16;
9463 dob:
df58fc94 9464 gas_assert (!mips_opts.micromips);
0aa27725 9465 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 9466 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9467 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
9468 /* FIXME: A possible overflow which I don't know how to deal
9469 with. */
9470 offset_expr.X_add_number += 4;
67c0d1eb 9471 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9472 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
9473 break;
9474
c4a68bea 9475 case M_S_DOB:
df58fc94 9476 gas_assert (!mips_opts.micromips);
0aa27725 9477 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
9478 /* Even on a big endian machine $fn comes before $fn+1. We have
9479 to adjust when storing to memory. */
9480 macro_build (&offset_expr, "swc1", "T,o(b)",
9481 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9482 offset_expr.X_add_number += 4;
9483 macro_build (&offset_expr, "swc1", "T,o(b)",
9484 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9485 break;
9486
252b5132 9487 case M_L_DAB:
df58fc94 9488 gas_assert (!mips_opts.micromips);
252b5132
RH
9489 /*
9490 * The MIPS assembler seems to check for X_add_number not
9491 * being double aligned and generating:
9492 * lui at,%hi(foo+1)
9493 * addu at,at,v1
9494 * addiu at,at,%lo(foo+1)
9495 * lwc1 f2,0(at)
9496 * lwc1 f3,4(at)
9497 * But, the resulting address is the same after relocation so why
9498 * generate the extra instruction?
9499 */
bdaaa2e1 9500 /* Itbl support may require additional care here. */
252b5132 9501 coproc = 1;
df58fc94 9502 fmt = "T,o(b)";
0aa27725 9503 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9504 {
9505 s = "ldc1";
df58fc94 9506 goto ld_st;
252b5132 9507 }
252b5132 9508 s = "lwc1";
252b5132
RH
9509 goto ldd_std;
9510
9511 case M_S_DAB:
df58fc94
RS
9512 gas_assert (!mips_opts.micromips);
9513 /* Itbl support may require additional care here. */
9514 coproc = 1;
9515 fmt = "T,o(b)";
0aa27725 9516 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9517 {
9518 s = "sdc1";
df58fc94 9519 goto ld_st;
252b5132 9520 }
252b5132 9521 s = "swc1";
252b5132
RH
9522 goto ldd_std;
9523
e407c74b
NC
9524 case M_LQ_AB:
9525 fmt = "t,o(b)";
9526 s = "lq";
9527 goto ld;
9528
9529 case M_SQ_AB:
9530 fmt = "t,o(b)";
9531 s = "sq";
9532 goto ld_st;
9533
252b5132 9534 case M_LD_AB:
df58fc94 9535 fmt = "t,o(b)";
ca4e0257 9536 if (HAVE_64BIT_GPRS)
252b5132
RH
9537 {
9538 s = "ld";
9539 goto ld;
9540 }
252b5132 9541 s = "lw";
252b5132
RH
9542 goto ldd_std;
9543
9544 case M_SD_AB:
df58fc94 9545 fmt = "t,o(b)";
ca4e0257 9546 if (HAVE_64BIT_GPRS)
252b5132
RH
9547 {
9548 s = "sd";
df58fc94 9549 goto ld_st;
252b5132 9550 }
252b5132 9551 s = "sw";
252b5132
RH
9552
9553 ldd_std:
9554 if (offset_expr.X_op != O_symbol
9555 && offset_expr.X_op != O_constant)
9556 {
f71d0d44 9557 as_bad (_("Expression too complex"));
252b5132
RH
9558 offset_expr.X_op = O_constant;
9559 }
9560
2051e8c4
MR
9561 if (HAVE_32BIT_ADDRESSES
9562 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9563 {
9564 char value [32];
9565
9566 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9567 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9568 }
2051e8c4 9569
252b5132
RH
9570 /* Even on a big endian machine $fn comes before $fn+1. We have
9571 to adjust when loading from memory. We set coproc if we must
9572 load $fn+1 first. */
bdaaa2e1 9573 /* Itbl support may require additional care here. */
90ecf173 9574 if (!target_big_endian)
252b5132
RH
9575 coproc = 0;
9576
90ecf173 9577 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9578 {
9579 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9580 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9581 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9582 If we have a base register, we use this
9583 addu $at,$breg,$gp
cdf6fd85
TS
9584 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9585 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9586 If this is not a GP relative symbol, we want
9587 lui $at,<sym> (BFD_RELOC_HI16_S)
9588 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9589 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9590 If there is a base register, we add it to $at after the
9591 lui instruction. If there is a constant, we always use
9592 the last case. */
39a59cf8
MR
9593 if (offset_expr.X_op == O_symbol
9594 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9595 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9596 {
4d7206a2 9597 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9598 if (breg == 0)
9599 {
c9914766 9600 tempreg = mips_gp_register;
252b5132
RH
9601 }
9602 else
9603 {
67c0d1eb 9604 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9605 AT, breg, mips_gp_register);
252b5132 9606 tempreg = AT;
252b5132
RH
9607 used_at = 1;
9608 }
9609
beae10d5 9610 /* Itbl support may require additional care here. */
67c0d1eb 9611 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9612 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9613 offset_expr.X_add_number += 4;
9614
9615 /* Set mips_optimize to 2 to avoid inserting an
9616 undesired nop. */
9617 hold_mips_optimize = mips_optimize;
9618 mips_optimize = 2;
beae10d5 9619 /* Itbl support may require additional care here. */
67c0d1eb 9620 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9621 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9622 mips_optimize = hold_mips_optimize;
9623
4d7206a2 9624 relax_switch ();
252b5132 9625
0970e49e 9626 offset_expr.X_add_number -= 4;
252b5132 9627 }
8fc2e39e 9628 used_at = 1;
67c0d1eb 9629 macro_build_lui (&offset_expr, AT);
252b5132 9630 if (breg != 0)
67c0d1eb 9631 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9632 /* Itbl support may require additional care here. */
67c0d1eb 9633 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9634 BFD_RELOC_LO16, AT);
252b5132
RH
9635 /* FIXME: How do we handle overflow here? */
9636 offset_expr.X_add_number += 4;
beae10d5 9637 /* Itbl support may require additional care here. */
67c0d1eb 9638 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9639 BFD_RELOC_LO16, AT);
4d7206a2
RS
9640 if (mips_relax.sequence)
9641 relax_end ();
bdaaa2e1 9642 }
0a44bf69 9643 else if (!mips_big_got)
252b5132 9644 {
252b5132
RH
9645 /* If this is a reference to an external symbol, we want
9646 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9647 nop
9648 <op> $treg,0($at)
9649 <op> $treg+1,4($at)
9650 Otherwise we want
9651 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9652 nop
9653 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9654 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9655 If there is a base register we add it to $at before the
9656 lwc1 instructions. If there is a constant we include it
9657 in the lwc1 instructions. */
9658 used_at = 1;
9659 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9660 if (expr1.X_add_number < -0x8000
9661 || expr1.X_add_number >= 0x8000 - 4)
9662 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9663 load_got_offset (AT, &offset_expr);
269137b2 9664 load_delay_nop ();
252b5132 9665 if (breg != 0)
67c0d1eb 9666 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9667
9668 /* Set mips_optimize to 2 to avoid inserting an undesired
9669 nop. */
9670 hold_mips_optimize = mips_optimize;
9671 mips_optimize = 2;
4d7206a2 9672
beae10d5 9673 /* Itbl support may require additional care here. */
4d7206a2 9674 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9675 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9676 BFD_RELOC_LO16, AT);
4d7206a2 9677 expr1.X_add_number += 4;
67c0d1eb
RS
9678 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9679 BFD_RELOC_LO16, AT);
4d7206a2 9680 relax_switch ();
67c0d1eb
RS
9681 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9682 BFD_RELOC_LO16, AT);
4d7206a2 9683 offset_expr.X_add_number += 4;
67c0d1eb
RS
9684 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9685 BFD_RELOC_LO16, AT);
4d7206a2 9686 relax_end ();
252b5132 9687
4d7206a2 9688 mips_optimize = hold_mips_optimize;
252b5132 9689 }
0a44bf69 9690 else if (mips_big_got)
252b5132 9691 {
67c0d1eb 9692 int gpdelay;
252b5132
RH
9693
9694 /* If this is a reference to an external symbol, we want
9695 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9696 addu $at,$at,$gp
9697 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9698 nop
9699 <op> $treg,0($at)
9700 <op> $treg+1,4($at)
9701 Otherwise we want
9702 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9703 nop
9704 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9705 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9706 If there is a base register we add it to $at before the
9707 lwc1 instructions. If there is a constant we include it
9708 in the lwc1 instructions. */
9709 used_at = 1;
9710 expr1.X_add_number = offset_expr.X_add_number;
9711 offset_expr.X_add_number = 0;
9712 if (expr1.X_add_number < -0x8000
9713 || expr1.X_add_number >= 0x8000 - 4)
9714 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9715 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9716 relax_start (offset_expr.X_add_symbol);
df58fc94 9717 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9718 AT, BFD_RELOC_MIPS_GOT_HI16);
9719 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9720 AT, AT, mips_gp_register);
67c0d1eb 9721 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9722 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9723 load_delay_nop ();
252b5132 9724 if (breg != 0)
67c0d1eb 9725 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9726 /* Itbl support may require additional care here. */
67c0d1eb 9727 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9728 BFD_RELOC_LO16, AT);
252b5132
RH
9729 expr1.X_add_number += 4;
9730
9731 /* Set mips_optimize to 2 to avoid inserting an undesired
9732 nop. */
9733 hold_mips_optimize = mips_optimize;
9734 mips_optimize = 2;
beae10d5 9735 /* Itbl support may require additional care here. */
67c0d1eb 9736 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9737 BFD_RELOC_LO16, AT);
252b5132
RH
9738 mips_optimize = hold_mips_optimize;
9739 expr1.X_add_number -= 4;
9740
4d7206a2
RS
9741 relax_switch ();
9742 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9743 if (gpdelay)
9744 macro_build (NULL, "nop", "");
9745 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9746 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9747 load_delay_nop ();
252b5132 9748 if (breg != 0)
67c0d1eb 9749 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9750 /* Itbl support may require additional care here. */
67c0d1eb
RS
9751 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9752 BFD_RELOC_LO16, AT);
4d7206a2 9753 offset_expr.X_add_number += 4;
252b5132
RH
9754
9755 /* Set mips_optimize to 2 to avoid inserting an undesired
9756 nop. */
9757 hold_mips_optimize = mips_optimize;
9758 mips_optimize = 2;
beae10d5 9759 /* Itbl support may require additional care here. */
67c0d1eb
RS
9760 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9761 BFD_RELOC_LO16, AT);
252b5132 9762 mips_optimize = hold_mips_optimize;
4d7206a2 9763 relax_end ();
252b5132 9764 }
252b5132
RH
9765 else
9766 abort ();
9767
252b5132
RH
9768 break;
9769
9770 case M_LD_OB:
704897fb 9771 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9772 goto sd_ob;
9773 case M_SD_OB:
704897fb 9774 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9775 sd_ob:
4614d845
MR
9776 macro_build (&offset_expr, s, "t,o(b)", treg,
9777 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9778 breg);
704897fb
MR
9779 if (!HAVE_64BIT_GPRS)
9780 {
9781 offset_expr.X_add_number += 4;
9782 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9783 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9784 breg);
704897fb 9785 }
8fc2e39e 9786 break;
252b5132 9787
dd6a37e7
AP
9788
9789 case M_SAA_AB:
9790 ab = 1;
9791 case M_SAA_OB:
9792 s = "saa";
7f3c4072 9793 offbits = 0;
dd6a37e7
AP
9794 fmt = "t,(b)";
9795 goto ld_st;
9796 case M_SAAD_AB:
9797 ab = 1;
9798 case M_SAAD_OB:
9799 s = "saad";
7f3c4072 9800 offbits = 0;
dd6a37e7
AP
9801 fmt = "t,(b)";
9802 goto ld_st;
9803
252b5132
RH
9804 /* New code added to support COPZ instructions.
9805 This code builds table entries out of the macros in mip_opcodes.
9806 R4000 uses interlocks to handle coproc delays.
9807 Other chips (like the R3000) require nops to be inserted for delays.
9808
f72c8c98 9809 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9810 In order to fill delay slots for non-interlocked chips,
9811 we must have a way to specify delays based on the coprocessor.
9812 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9813 What are the side-effects of the cop instruction?
9814 What cache support might we have and what are its effects?
9815 Both coprocessor & memory require delays. how long???
bdaaa2e1 9816 What registers are read/set/modified?
252b5132
RH
9817
9818 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9819 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9820
9821 case M_COP0:
9822 s = "c0";
9823 goto copz;
9824 case M_COP1:
9825 s = "c1";
9826 goto copz;
9827 case M_COP2:
9828 s = "c2";
9829 goto copz;
9830 case M_COP3:
9831 s = "c3";
9832 copz:
df58fc94 9833 gas_assert (!mips_opts.micromips);
252b5132
RH
9834 /* For now we just do C (same as Cz). The parameter will be
9835 stored in insn_opcode by mips_ip. */
67c0d1eb 9836 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9837 break;
252b5132 9838
ea1fb5dc 9839 case M_MOVE:
67c0d1eb 9840 move_register (dreg, sreg);
8fc2e39e 9841 break;
ea1fb5dc 9842
833794fc
MR
9843 case M_MOVEP:
9844 gas_assert (mips_opts.micromips);
9845 gas_assert (mips_opts.insn32);
9846 dreg = micromips_to_32_reg_h_map[EXTRACT_OPERAND (1, MH, *ip)];
9847 breg = micromips_to_32_reg_i_map[EXTRACT_OPERAND (1, MI, *ip)];
9848 sreg = micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
9849 treg = micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
9850 move_register (dreg, sreg);
9851 move_register (breg, treg);
9852 break;
9853
252b5132
RH
9854 case M_DMUL:
9855 dbl = 1;
9856 case M_MUL:
e407c74b
NC
9857 if (mips_opts.arch == CPU_R5900)
9858 {
9859 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9860 }
9861 else
9862 {
67c0d1eb 9863 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9864 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9865 }
8fc2e39e 9866 break;
252b5132
RH
9867
9868 case M_DMUL_I:
9869 dbl = 1;
9870 case M_MUL_I:
9871 /* The MIPS assembler some times generates shifts and adds. I'm
9872 not trying to be that fancy. GCC should do this for us
9873 anyway. */
8fc2e39e 9874 used_at = 1;
67c0d1eb
RS
9875 load_register (AT, &imm_expr, dbl);
9876 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9877 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9878 break;
9879
9880 case M_DMULO_I:
9881 dbl = 1;
9882 case M_MULO_I:
9883 imm = 1;
9884 goto do_mulo;
9885
9886 case M_DMULO:
9887 dbl = 1;
9888 case M_MULO:
9889 do_mulo:
7d10b47d 9890 start_noreorder ();
8fc2e39e 9891 used_at = 1;
252b5132 9892 if (imm)
67c0d1eb
RS
9893 load_register (AT, &imm_expr, dbl);
9894 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9895 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9896 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9897 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9898 if (mips_trap)
df58fc94 9899 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9900 else
9901 {
df58fc94
RS
9902 if (mips_opts.micromips)
9903 micromips_label_expr (&label_expr);
9904 else
9905 label_expr.X_add_number = 8;
9906 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9907 macro_build (NULL, "nop", "");
df58fc94
RS
9908 macro_build (NULL, "break", BRK_FMT, 6);
9909 if (mips_opts.micromips)
9910 micromips_add_label ();
252b5132 9911 }
7d10b47d 9912 end_noreorder ();
df58fc94 9913 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9914 break;
9915
9916 case M_DMULOU_I:
9917 dbl = 1;
9918 case M_MULOU_I:
9919 imm = 1;
9920 goto do_mulou;
9921
9922 case M_DMULOU:
9923 dbl = 1;
9924 case M_MULOU:
9925 do_mulou:
7d10b47d 9926 start_noreorder ();
8fc2e39e 9927 used_at = 1;
252b5132 9928 if (imm)
67c0d1eb
RS
9929 load_register (AT, &imm_expr, dbl);
9930 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9931 sreg, imm ? AT : treg);
df58fc94
RS
9932 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9933 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9934 if (mips_trap)
df58fc94 9935 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9936 else
9937 {
df58fc94
RS
9938 if (mips_opts.micromips)
9939 micromips_label_expr (&label_expr);
9940 else
9941 label_expr.X_add_number = 8;
9942 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9943 macro_build (NULL, "nop", "");
df58fc94
RS
9944 macro_build (NULL, "break", BRK_FMT, 6);
9945 if (mips_opts.micromips)
9946 micromips_add_label ();
252b5132 9947 }
7d10b47d 9948 end_noreorder ();
252b5132
RH
9949 break;
9950
771c7ce4 9951 case M_DROL:
fef14a42 9952 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9953 {
9954 if (dreg == sreg)
9955 {
9956 tempreg = AT;
9957 used_at = 1;
9958 }
9959 else
9960 {
9961 tempreg = dreg;
82dd0097 9962 }
67c0d1eb
RS
9963 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9964 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9965 break;
82dd0097 9966 }
8fc2e39e 9967 used_at = 1;
c80c840e 9968 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9969 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9970 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9971 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9972 break;
9973
252b5132 9974 case M_ROL:
fef14a42 9975 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9976 {
9977 if (dreg == sreg)
9978 {
9979 tempreg = AT;
9980 used_at = 1;
9981 }
9982 else
9983 {
9984 tempreg = dreg;
82dd0097 9985 }
67c0d1eb
RS
9986 macro_build (NULL, "negu", "d,w", tempreg, treg);
9987 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9988 break;
82dd0097 9989 }
8fc2e39e 9990 used_at = 1;
c80c840e 9991 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9992 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9993 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9994 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9995 break;
9996
771c7ce4
TS
9997 case M_DROL_I:
9998 {
9999 unsigned int rot;
91d6fa6a
NC
10000 char *l;
10001 char *rr;
771c7ce4
TS
10002
10003 if (imm_expr.X_op != O_constant)
82dd0097 10004 as_bad (_("Improper rotate count"));
771c7ce4 10005 rot = imm_expr.X_add_number & 0x3f;
fef14a42 10006 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
10007 {
10008 rot = (64 - rot) & 0x3f;
10009 if (rot >= 32)
df58fc94 10010 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 10011 else
df58fc94 10012 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10013 break;
60b63b72 10014 }
483fc7cd 10015 if (rot == 0)
483fc7cd 10016 {
df58fc94 10017 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10018 break;
483fc7cd 10019 }
82dd0097 10020 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 10021 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 10022 rot &= 0x1f;
8fc2e39e 10023 used_at = 1;
df58fc94
RS
10024 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
10025 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10026 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10027 }
10028 break;
10029
252b5132 10030 case M_ROL_I:
771c7ce4
TS
10031 {
10032 unsigned int rot;
10033
10034 if (imm_expr.X_op != O_constant)
82dd0097 10035 as_bad (_("Improper rotate count"));
771c7ce4 10036 rot = imm_expr.X_add_number & 0x1f;
fef14a42 10037 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 10038 {
df58fc94 10039 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 10040 break;
60b63b72 10041 }
483fc7cd 10042 if (rot == 0)
483fc7cd 10043 {
df58fc94 10044 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10045 break;
483fc7cd 10046 }
8fc2e39e 10047 used_at = 1;
df58fc94
RS
10048 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
10049 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10050 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10051 }
10052 break;
10053
10054 case M_DROR:
fef14a42 10055 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 10056 {
67c0d1eb 10057 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 10058 break;
82dd0097 10059 }
8fc2e39e 10060 used_at = 1;
c80c840e 10061 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
10062 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
10063 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
10064 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
10065 break;
10066
10067 case M_ROR:
fef14a42 10068 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 10069 {
67c0d1eb 10070 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 10071 break;
82dd0097 10072 }
8fc2e39e 10073 used_at = 1;
c80c840e 10074 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
10075 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
10076 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
10077 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
10078 break;
10079
771c7ce4
TS
10080 case M_DROR_I:
10081 {
10082 unsigned int rot;
91d6fa6a
NC
10083 char *l;
10084 char *rr;
771c7ce4
TS
10085
10086 if (imm_expr.X_op != O_constant)
82dd0097 10087 as_bad (_("Improper rotate count"));
771c7ce4 10088 rot = imm_expr.X_add_number & 0x3f;
fef14a42 10089 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
10090 {
10091 if (rot >= 32)
df58fc94 10092 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 10093 else
df58fc94 10094 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10095 break;
82dd0097 10096 }
483fc7cd 10097 if (rot == 0)
483fc7cd 10098 {
df58fc94 10099 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10100 break;
483fc7cd 10101 }
91d6fa6a 10102 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
10103 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
10104 rot &= 0x1f;
8fc2e39e 10105 used_at = 1;
df58fc94
RS
10106 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
10107 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10108 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10109 }
10110 break;
10111
252b5132 10112 case M_ROR_I:
771c7ce4
TS
10113 {
10114 unsigned int rot;
10115
10116 if (imm_expr.X_op != O_constant)
82dd0097 10117 as_bad (_("Improper rotate count"));
771c7ce4 10118 rot = imm_expr.X_add_number & 0x1f;
fef14a42 10119 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 10120 {
df58fc94 10121 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10122 break;
82dd0097 10123 }
483fc7cd 10124 if (rot == 0)
483fc7cd 10125 {
df58fc94 10126 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10127 break;
483fc7cd 10128 }
8fc2e39e 10129 used_at = 1;
df58fc94
RS
10130 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
10131 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10132 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 10133 }
252b5132
RH
10134 break;
10135
252b5132
RH
10136 case M_SEQ:
10137 if (sreg == 0)
67c0d1eb 10138 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 10139 else if (treg == 0)
67c0d1eb 10140 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10141 else
10142 {
67c0d1eb
RS
10143 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10144 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 10145 }
8fc2e39e 10146 break;
252b5132
RH
10147
10148 case M_SEQ_I:
10149 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10150 {
67c0d1eb 10151 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10152 break;
252b5132
RH
10153 }
10154 if (sreg == 0)
10155 {
10156 as_warn (_("Instruction %s: result is always false"),
10157 ip->insn_mo->name);
67c0d1eb 10158 move_register (dreg, 0);
8fc2e39e 10159 break;
252b5132 10160 }
dd3cbb7e
NC
10161 if (CPU_HAS_SEQ (mips_opts.arch)
10162 && -512 <= imm_expr.X_add_number
10163 && imm_expr.X_add_number < 512)
10164 {
10165 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 10166 (int) imm_expr.X_add_number);
dd3cbb7e
NC
10167 break;
10168 }
252b5132
RH
10169 if (imm_expr.X_op == O_constant
10170 && imm_expr.X_add_number >= 0
10171 && imm_expr.X_add_number < 0x10000)
10172 {
67c0d1eb 10173 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10174 }
10175 else if (imm_expr.X_op == O_constant
10176 && imm_expr.X_add_number > -0x8000
10177 && imm_expr.X_add_number < 0)
10178 {
10179 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10180 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 10181 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 10182 }
dd3cbb7e
NC
10183 else if (CPU_HAS_SEQ (mips_opts.arch))
10184 {
10185 used_at = 1;
10186 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10187 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
10188 break;
10189 }
252b5132
RH
10190 else
10191 {
67c0d1eb
RS
10192 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10193 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
10194 used_at = 1;
10195 }
67c0d1eb 10196 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10197 break;
252b5132
RH
10198
10199 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
10200 s = "slt";
10201 goto sge;
10202 case M_SGEU:
10203 s = "sltu";
10204 sge:
67c0d1eb
RS
10205 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
10206 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10207 break;
252b5132
RH
10208
10209 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
10210 case M_SGEU_I:
10211 if (imm_expr.X_op == O_constant
10212 && imm_expr.X_add_number >= -0x8000
10213 && imm_expr.X_add_number < 0x8000)
10214 {
67c0d1eb
RS
10215 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
10216 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10217 }
10218 else
10219 {
67c0d1eb
RS
10220 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10221 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
10222 dreg, sreg, AT);
252b5132
RH
10223 used_at = 1;
10224 }
67c0d1eb 10225 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10226 break;
252b5132
RH
10227
10228 case M_SGT: /* sreg > treg <==> treg < sreg */
10229 s = "slt";
10230 goto sgt;
10231 case M_SGTU:
10232 s = "sltu";
10233 sgt:
67c0d1eb 10234 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 10235 break;
252b5132
RH
10236
10237 case M_SGT_I: /* sreg > I <==> I < sreg */
10238 s = "slt";
10239 goto sgti;
10240 case M_SGTU_I:
10241 s = "sltu";
10242 sgti:
8fc2e39e 10243 used_at = 1;
67c0d1eb
RS
10244 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10245 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
10246 break;
10247
2396cfb9 10248 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
10249 s = "slt";
10250 goto sle;
10251 case M_SLEU:
10252 s = "sltu";
10253 sle:
67c0d1eb
RS
10254 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10255 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10256 break;
252b5132 10257
2396cfb9 10258 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
10259 s = "slt";
10260 goto slei;
10261 case M_SLEU_I:
10262 s = "sltu";
10263 slei:
8fc2e39e 10264 used_at = 1;
67c0d1eb
RS
10265 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10266 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10267 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
10268 break;
10269
10270 case M_SLT_I:
10271 if (imm_expr.X_op == O_constant
10272 && imm_expr.X_add_number >= -0x8000
10273 && imm_expr.X_add_number < 0x8000)
10274 {
67c0d1eb 10275 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10276 break;
252b5132 10277 }
8fc2e39e 10278 used_at = 1;
67c0d1eb
RS
10279 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10280 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
10281 break;
10282
10283 case M_SLTU_I:
10284 if (imm_expr.X_op == O_constant
10285 && imm_expr.X_add_number >= -0x8000
10286 && imm_expr.X_add_number < 0x8000)
10287 {
67c0d1eb 10288 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 10289 BFD_RELOC_LO16);
8fc2e39e 10290 break;
252b5132 10291 }
8fc2e39e 10292 used_at = 1;
67c0d1eb
RS
10293 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10294 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
10295 break;
10296
10297 case M_SNE:
10298 if (sreg == 0)
67c0d1eb 10299 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 10300 else if (treg == 0)
67c0d1eb 10301 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
10302 else
10303 {
67c0d1eb
RS
10304 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10305 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 10306 }
8fc2e39e 10307 break;
252b5132
RH
10308
10309 case M_SNE_I:
10310 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10311 {
67c0d1eb 10312 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 10313 break;
252b5132
RH
10314 }
10315 if (sreg == 0)
10316 {
10317 as_warn (_("Instruction %s: result is always true"),
10318 ip->insn_mo->name);
67c0d1eb
RS
10319 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
10320 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 10321 break;
252b5132 10322 }
dd3cbb7e
NC
10323 if (CPU_HAS_SEQ (mips_opts.arch)
10324 && -512 <= imm_expr.X_add_number
10325 && imm_expr.X_add_number < 512)
10326 {
10327 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 10328 (int) imm_expr.X_add_number);
dd3cbb7e
NC
10329 break;
10330 }
252b5132
RH
10331 if (imm_expr.X_op == O_constant
10332 && imm_expr.X_add_number >= 0
10333 && imm_expr.X_add_number < 0x10000)
10334 {
67c0d1eb 10335 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10336 }
10337 else if (imm_expr.X_op == O_constant
10338 && imm_expr.X_add_number > -0x8000
10339 && imm_expr.X_add_number < 0)
10340 {
10341 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10342 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 10343 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 10344 }
dd3cbb7e
NC
10345 else if (CPU_HAS_SEQ (mips_opts.arch))
10346 {
10347 used_at = 1;
10348 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10349 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
10350 break;
10351 }
252b5132
RH
10352 else
10353 {
67c0d1eb
RS
10354 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10355 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
10356 used_at = 1;
10357 }
67c0d1eb 10358 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 10359 break;
252b5132 10360
df58fc94
RS
10361 case M_SUB_I:
10362 s = "addi";
10363 s2 = "sub";
10364 goto do_subi;
10365 case M_SUBU_I:
10366 s = "addiu";
10367 s2 = "subu";
10368 goto do_subi;
252b5132
RH
10369 case M_DSUB_I:
10370 dbl = 1;
df58fc94
RS
10371 s = "daddi";
10372 s2 = "dsub";
10373 if (!mips_opts.micromips)
10374 goto do_subi;
252b5132 10375 if (imm_expr.X_op == O_constant
df58fc94
RS
10376 && imm_expr.X_add_number > -0x200
10377 && imm_expr.X_add_number <= 0x200)
252b5132 10378 {
df58fc94 10379 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 10380 break;
252b5132 10381 }
df58fc94 10382 goto do_subi_i;
252b5132
RH
10383 case M_DSUBU_I:
10384 dbl = 1;
df58fc94
RS
10385 s = "daddiu";
10386 s2 = "dsubu";
10387 do_subi:
252b5132
RH
10388 if (imm_expr.X_op == O_constant
10389 && imm_expr.X_add_number > -0x8000
10390 && imm_expr.X_add_number <= 0x8000)
10391 {
10392 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 10393 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10394 break;
252b5132 10395 }
df58fc94 10396 do_subi_i:
8fc2e39e 10397 used_at = 1;
67c0d1eb 10398 load_register (AT, &imm_expr, dbl);
df58fc94 10399 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
10400 break;
10401
10402 case M_TEQ_I:
10403 s = "teq";
10404 goto trap;
10405 case M_TGE_I:
10406 s = "tge";
10407 goto trap;
10408 case M_TGEU_I:
10409 s = "tgeu";
10410 goto trap;
10411 case M_TLT_I:
10412 s = "tlt";
10413 goto trap;
10414 case M_TLTU_I:
10415 s = "tltu";
10416 goto trap;
10417 case M_TNE_I:
10418 s = "tne";
10419 trap:
8fc2e39e 10420 used_at = 1;
67c0d1eb
RS
10421 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10422 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
10423 break;
10424
252b5132 10425 case M_TRUNCWS:
43841e91 10426 case M_TRUNCWD:
df58fc94 10427 gas_assert (!mips_opts.micromips);
0aa27725 10428 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 10429 used_at = 1;
252b5132
RH
10430 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
10431 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
10432
10433 /*
10434 * Is the double cfc1 instruction a bug in the mips assembler;
10435 * or is there a reason for it?
10436 */
7d10b47d 10437 start_noreorder ();
67c0d1eb
RS
10438 macro_build (NULL, "cfc1", "t,G", treg, RA);
10439 macro_build (NULL, "cfc1", "t,G", treg, RA);
10440 macro_build (NULL, "nop", "");
252b5132 10441 expr1.X_add_number = 3;
67c0d1eb 10442 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 10443 expr1.X_add_number = 2;
67c0d1eb
RS
10444 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10445 macro_build (NULL, "ctc1", "t,G", AT, RA);
10446 macro_build (NULL, "nop", "");
10447 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10448 dreg, sreg);
10449 macro_build (NULL, "ctc1", "t,G", treg, RA);
10450 macro_build (NULL, "nop", "");
7d10b47d 10451 end_noreorder ();
252b5132
RH
10452 break;
10453
df58fc94
RS
10454 case M_ULH_A:
10455 ab = 1;
252b5132
RH
10456 case M_ULH:
10457 s = "lb";
df58fc94
RS
10458 s2 = "lbu";
10459 off = 1;
10460 goto uld_st;
10461 case M_ULHU_A:
10462 ab = 1;
252b5132
RH
10463 case M_ULHU:
10464 s = "lbu";
df58fc94
RS
10465 s2 = "lbu";
10466 off = 1;
10467 goto uld_st;
10468 case M_ULW_A:
10469 ab = 1;
10470 case M_ULW:
10471 s = "lwl";
10472 s2 = "lwr";
7f3c4072 10473 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10474 off = 3;
10475 goto uld_st;
10476 case M_ULD_A:
10477 ab = 1;
252b5132
RH
10478 case M_ULD:
10479 s = "ldl";
10480 s2 = "ldr";
7f3c4072 10481 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10482 off = 7;
df58fc94
RS
10483 goto uld_st;
10484 case M_USH_A:
10485 ab = 1;
10486 case M_USH:
10487 s = "sb";
10488 s2 = "sb";
10489 off = 1;
10490 ust = 1;
10491 goto uld_st;
10492 case M_USW_A:
10493 ab = 1;
10494 case M_USW:
10495 s = "swl";
10496 s2 = "swr";
7f3c4072 10497 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10498 off = 3;
df58fc94
RS
10499 ust = 1;
10500 goto uld_st;
10501 case M_USD_A:
10502 ab = 1;
10503 case M_USD:
10504 s = "sdl";
10505 s2 = "sdr";
7f3c4072 10506 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10507 off = 7;
10508 ust = 1;
10509
10510 uld_st:
10511 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10512 as_bad (_("Operand overflow"));
df58fc94
RS
10513
10514 ep = &offset_expr;
10515 expr1.X_add_number = 0;
10516 if (ab)
10517 {
10518 used_at = 1;
10519 tempreg = AT;
10520 load_address (tempreg, ep, &used_at);
10521 if (breg != 0)
10522 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10523 tempreg, tempreg, breg);
10524 breg = tempreg;
10525 tempreg = treg;
10526 ep = &expr1;
10527 }
7f3c4072 10528 else if (offbits == 12
df58fc94
RS
10529 && (offset_expr.X_op != O_constant
10530 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10531 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10532 {
10533 used_at = 1;
10534 tempreg = AT;
10535 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10536 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10537 breg = tempreg;
10538 tempreg = treg;
10539 ep = &expr1;
10540 }
10541 else if (!ust && treg == breg)
8fc2e39e
TS
10542 {
10543 used_at = 1;
10544 tempreg = AT;
10545 }
252b5132 10546 else
df58fc94 10547 tempreg = treg;
af22f5b2 10548
df58fc94
RS
10549 if (off == 1)
10550 goto ulh_sh;
252b5132 10551
90ecf173 10552 if (!target_big_endian)
df58fc94 10553 ep->X_add_number += off;
7f3c4072 10554 if (offbits != 12)
df58fc94 10555 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10556 else
df58fc94
RS
10557 macro_build (NULL, s, "t,~(b)",
10558 tempreg, (unsigned long) ep->X_add_number, breg);
10559
90ecf173 10560 if (!target_big_endian)
df58fc94 10561 ep->X_add_number -= off;
252b5132 10562 else
df58fc94 10563 ep->X_add_number += off;
7f3c4072 10564 if (offbits != 12)
df58fc94
RS
10565 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10566 else
10567 macro_build (NULL, s2, "t,~(b)",
10568 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10569
df58fc94
RS
10570 /* If necessary, move the result in tempreg to the final destination. */
10571 if (!ust && treg != tempreg)
10572 {
10573 /* Protect second load's delay slot. */
10574 load_delay_nop ();
10575 move_register (treg, tempreg);
10576 }
8fc2e39e 10577 break;
252b5132 10578
df58fc94 10579 ulh_sh:
d6bc6245 10580 used_at = 1;
df58fc94
RS
10581 if (target_big_endian == ust)
10582 ep->X_add_number += off;
10583 tempreg = ust || ab ? treg : AT;
10584 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10585
10586 /* For halfword transfers we need a temporary register to shuffle
10587 bytes. Unfortunately for M_USH_A we have none available before
10588 the next store as AT holds the base address. We deal with this
10589 case by clobbering TREG and then restoring it as with ULH. */
10590 tempreg = ust == ab ? treg : AT;
10591 if (ust)
10592 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10593
10594 if (target_big_endian == ust)
10595 ep->X_add_number -= off;
252b5132 10596 else
df58fc94
RS
10597 ep->X_add_number += off;
10598 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10599
df58fc94
RS
10600 /* For M_USH_A re-retrieve the LSB. */
10601 if (ust && ab)
10602 {
10603 if (target_big_endian)
10604 ep->X_add_number += off;
10605 else
10606 ep->X_add_number -= off;
10607 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10608 }
10609 /* For ULH and M_USH_A OR the LSB in. */
10610 if (!ust || ab)
10611 {
10612 tempreg = !ab ? AT : treg;
10613 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10614 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10615 }
252b5132
RH
10616 break;
10617
10618 default:
10619 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10620 are added dynamically. */
252b5132
RH
10621 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10622 break;
10623 }
741fe287 10624 if (!mips_opts.at && used_at)
8fc2e39e 10625 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10626}
10627
10628/* Implement macros in mips16 mode. */
10629
10630static void
17a2f251 10631mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10632{
10633 int mask;
10634 int xreg, yreg, zreg, tmp;
252b5132
RH
10635 expressionS expr1;
10636 int dbl;
10637 const char *s, *s2, *s3;
10638
10639 mask = ip->insn_mo->mask;
10640
bf12938e
RS
10641 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10642 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10643 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10644
252b5132
RH
10645 expr1.X_op = O_constant;
10646 expr1.X_op_symbol = NULL;
10647 expr1.X_add_symbol = NULL;
10648 expr1.X_add_number = 1;
10649
10650 dbl = 0;
10651
10652 switch (mask)
10653 {
10654 default:
b37df7c4 10655 abort ();
252b5132
RH
10656
10657 case M_DDIV_3:
10658 dbl = 1;
10659 case M_DIV_3:
10660 s = "mflo";
10661 goto do_div3;
10662 case M_DREM_3:
10663 dbl = 1;
10664 case M_REM_3:
10665 s = "mfhi";
10666 do_div3:
7d10b47d 10667 start_noreorder ();
67c0d1eb 10668 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10669 expr1.X_add_number = 2;
67c0d1eb
RS
10670 macro_build (&expr1, "bnez", "x,p", yreg);
10671 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10672
252b5132
RH
10673 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10674 since that causes an overflow. We should do that as well,
10675 but I don't see how to do the comparisons without a temporary
10676 register. */
7d10b47d 10677 end_noreorder ();
67c0d1eb 10678 macro_build (NULL, s, "x", zreg);
252b5132
RH
10679 break;
10680
10681 case M_DIVU_3:
10682 s = "divu";
10683 s2 = "mflo";
10684 goto do_divu3;
10685 case M_REMU_3:
10686 s = "divu";
10687 s2 = "mfhi";
10688 goto do_divu3;
10689 case M_DDIVU_3:
10690 s = "ddivu";
10691 s2 = "mflo";
10692 goto do_divu3;
10693 case M_DREMU_3:
10694 s = "ddivu";
10695 s2 = "mfhi";
10696 do_divu3:
7d10b47d 10697 start_noreorder ();
67c0d1eb 10698 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10699 expr1.X_add_number = 2;
67c0d1eb
RS
10700 macro_build (&expr1, "bnez", "x,p", yreg);
10701 macro_build (NULL, "break", "6", 7);
7d10b47d 10702 end_noreorder ();
67c0d1eb 10703 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10704 break;
10705
10706 case M_DMUL:
10707 dbl = 1;
10708 case M_MUL:
67c0d1eb
RS
10709 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10710 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10711 break;
252b5132
RH
10712
10713 case M_DSUBU_I:
10714 dbl = 1;
10715 goto do_subu;
10716 case M_SUBU_I:
10717 do_subu:
10718 if (imm_expr.X_op != O_constant)
10719 as_bad (_("Unsupported large constant"));
10720 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10721 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10722 break;
10723
10724 case M_SUBU_I_2:
10725 if (imm_expr.X_op != O_constant)
10726 as_bad (_("Unsupported large constant"));
10727 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10728 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10729 break;
10730
10731 case M_DSUBU_I_2:
10732 if (imm_expr.X_op != O_constant)
10733 as_bad (_("Unsupported large constant"));
10734 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10735 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10736 break;
10737
10738 case M_BEQ:
10739 s = "cmp";
10740 s2 = "bteqz";
10741 goto do_branch;
10742 case M_BNE:
10743 s = "cmp";
10744 s2 = "btnez";
10745 goto do_branch;
10746 case M_BLT:
10747 s = "slt";
10748 s2 = "btnez";
10749 goto do_branch;
10750 case M_BLTU:
10751 s = "sltu";
10752 s2 = "btnez";
10753 goto do_branch;
10754 case M_BLE:
10755 s = "slt";
10756 s2 = "bteqz";
10757 goto do_reverse_branch;
10758 case M_BLEU:
10759 s = "sltu";
10760 s2 = "bteqz";
10761 goto do_reverse_branch;
10762 case M_BGE:
10763 s = "slt";
10764 s2 = "bteqz";
10765 goto do_branch;
10766 case M_BGEU:
10767 s = "sltu";
10768 s2 = "bteqz";
10769 goto do_branch;
10770 case M_BGT:
10771 s = "slt";
10772 s2 = "btnez";
10773 goto do_reverse_branch;
10774 case M_BGTU:
10775 s = "sltu";
10776 s2 = "btnez";
10777
10778 do_reverse_branch:
10779 tmp = xreg;
10780 xreg = yreg;
10781 yreg = tmp;
10782
10783 do_branch:
67c0d1eb
RS
10784 macro_build (NULL, s, "x,y", xreg, yreg);
10785 macro_build (&offset_expr, s2, "p");
252b5132
RH
10786 break;
10787
10788 case M_BEQ_I:
10789 s = "cmpi";
10790 s2 = "bteqz";
10791 s3 = "x,U";
10792 goto do_branch_i;
10793 case M_BNE_I:
10794 s = "cmpi";
10795 s2 = "btnez";
10796 s3 = "x,U";
10797 goto do_branch_i;
10798 case M_BLT_I:
10799 s = "slti";
10800 s2 = "btnez";
10801 s3 = "x,8";
10802 goto do_branch_i;
10803 case M_BLTU_I:
10804 s = "sltiu";
10805 s2 = "btnez";
10806 s3 = "x,8";
10807 goto do_branch_i;
10808 case M_BLE_I:
10809 s = "slti";
10810 s2 = "btnez";
10811 s3 = "x,8";
10812 goto do_addone_branch_i;
10813 case M_BLEU_I:
10814 s = "sltiu";
10815 s2 = "btnez";
10816 s3 = "x,8";
10817 goto do_addone_branch_i;
10818 case M_BGE_I:
10819 s = "slti";
10820 s2 = "bteqz";
10821 s3 = "x,8";
10822 goto do_branch_i;
10823 case M_BGEU_I:
10824 s = "sltiu";
10825 s2 = "bteqz";
10826 s3 = "x,8";
10827 goto do_branch_i;
10828 case M_BGT_I:
10829 s = "slti";
10830 s2 = "bteqz";
10831 s3 = "x,8";
10832 goto do_addone_branch_i;
10833 case M_BGTU_I:
10834 s = "sltiu";
10835 s2 = "bteqz";
10836 s3 = "x,8";
10837
10838 do_addone_branch_i:
10839 if (imm_expr.X_op != O_constant)
10840 as_bad (_("Unsupported large constant"));
10841 ++imm_expr.X_add_number;
10842
10843 do_branch_i:
67c0d1eb
RS
10844 macro_build (&imm_expr, s, s3, xreg);
10845 macro_build (&offset_expr, s2, "p");
252b5132
RH
10846 break;
10847
10848 case M_ABS:
10849 expr1.X_add_number = 0;
67c0d1eb 10850 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10851 if (xreg != yreg)
67c0d1eb 10852 move_register (xreg, yreg);
252b5132 10853 expr1.X_add_number = 2;
67c0d1eb
RS
10854 macro_build (&expr1, "bteqz", "p");
10855 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10856 }
10857}
10858
10859/* For consistency checking, verify that all bits are specified either
10860 by the match/mask part of the instruction definition, or by the
10861 operand list. */
10862static int
17a2f251 10863validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10864{
10865 const char *p = opc->args;
10866 char c;
10867 unsigned long used_bits = opc->mask;
10868
10869 if ((used_bits & opc->match) != opc->match)
10870 {
10871 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10872 opc->name, opc->args);
10873 return 0;
10874 }
10875#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10876 while (*p)
10877 switch (c = *p++)
10878 {
10879 case ',': break;
10880 case '(': break;
10881 case ')': break;
af7ee8bf
CD
10882 case '+':
10883 switch (c = *p++)
10884 {
9bcd4f99
TS
10885 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10886 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10887 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10888 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10889 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10890 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10891 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
bbcc0807
CD
10892 case 'D': USE_BITS (OP_MASK_RD, OP_SH_RD);
10893 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
5f74bc13
CD
10894 case 'E': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10895 case 'F': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10896 case 'G': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10897 case 'H': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); break;
10898 case 'I': break;
b015e599 10899 case 'J': USE_BITS (OP_MASK_CODE10, OP_SH_CODE10); break;
ef2e4d86
CF
10900 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10901 case 'T': USE_BITS (OP_MASK_RT, OP_SH_RT);
10902 USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
bb35fb24
NC
10903 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10904 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10905 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10906 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10907 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10908 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10909 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10910 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10911 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10912 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10913 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10914 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
7f3c4072 10915 case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
bb35fb24 10916
af7ee8bf
CD
10917 default:
10918 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10919 c, opc->name, opc->args);
10920 return 0;
10921 }
10922 break;
252b5132
RH
10923 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10924 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10925 case 'A': break;
4372b673 10926 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10927 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10928 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10929 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10930 case 'F': break;
10931 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10932 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10933 case 'I': break;
e972090a 10934 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10935 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10936 case 'L': break;
10937 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10938 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10939 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10940 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10941 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10942 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10943 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10944 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10945 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10946 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10947 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10948 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10949 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10950 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10951 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10952 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10953 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10954 case 'f': break;
10955 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10956 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10957 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10958 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10959 case 'l': break;
10960 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10961 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10962 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10963 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10964 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10965 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10966 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10967 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10968 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10969 case 'x': break;
10970 case 'z': break;
10971 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10972 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10973 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10974 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10975 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
10976 case '[': break;
10977 case ']': break;
620edafd 10978 case '1': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
8b082fb1 10979 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10980 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10981 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10982 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10983 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10984 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10985 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10986 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10987 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10988 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10989 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10990 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10991 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10992 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10993 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10994 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10995 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10996 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10997 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10998 default:
10999 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
11000 c, opc->name, opc->args);
11001 return 0;
11002 }
11003#undef USE_BITS
11004 if (used_bits != 0xffffffff)
11005 {
11006 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
11007 ~used_bits & 0xffffffff, opc->name, opc->args);
11008 return 0;
11009 }
11010 return 1;
11011}
11012
df58fc94
RS
11013/* For consistency checking, verify that the length implied matches the
11014 major opcode and that all bits are specified either by the match/mask
11015 part of the instruction definition, or by the operand list. */
11016
11017static int
11018validate_micromips_insn (const struct mips_opcode *opc)
11019{
11020 unsigned long match = opc->match;
11021 unsigned long mask = opc->mask;
11022 const char *p = opc->args;
11023 unsigned long insn_bits;
11024 unsigned long used_bits;
11025 unsigned long major;
11026 unsigned int length;
11027 char e;
11028 char c;
11029
11030 if ((mask & match) != match)
11031 {
11032 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
11033 opc->name, opc->args);
11034 return 0;
11035 }
11036 length = micromips_insn_length (opc);
11037 if (length != 2 && length != 4)
11038 {
11039 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
11040 "%s %s"), length, opc->name, opc->args);
11041 return 0;
11042 }
11043 major = match >> (10 + 8 * (length - 2));
11044 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
11045 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
11046 {
11047 as_bad (_("Internal error: bad microMIPS opcode "
11048 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
11049 return 0;
11050 }
11051
11052 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
11053 insn_bits = 1 << 4 * length;
11054 insn_bits <<= 4 * length;
11055 insn_bits -= 1;
11056 used_bits = mask;
11057#define USE_BITS(field) \
11058 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
11059 while (*p)
11060 switch (c = *p++)
11061 {
11062 case ',': break;
11063 case '(': break;
11064 case ')': break;
11065 case '+':
11066 e = c;
11067 switch (c = *p++)
11068 {
11069 case 'A': USE_BITS (EXTLSB); break;
11070 case 'B': USE_BITS (INSMSB); break;
11071 case 'C': USE_BITS (EXTMSBD); break;
11072 case 'D': USE_BITS (RS); USE_BITS (SEL); break;
11073 case 'E': USE_BITS (EXTLSB); break;
11074 case 'F': USE_BITS (INSMSB); break;
11075 case 'G': USE_BITS (EXTMSBD); break;
11076 case 'H': USE_BITS (EXTMSBD); break;
7f3c4072 11077 case 'j': USE_BITS (EVAOFFSET); break;
df58fc94
RS
11078 default:
11079 as_bad (_("Internal error: bad mips opcode "
11080 "(unknown extension operand type `%c%c'): %s %s"),
11081 e, c, opc->name, opc->args);
11082 return 0;
11083 }
11084 break;
11085 case 'm':
11086 e = c;
11087 switch (c = *p++)
11088 {
11089 case 'A': USE_BITS (IMMA); break;
11090 case 'B': USE_BITS (IMMB); break;
11091 case 'C': USE_BITS (IMMC); break;
11092 case 'D': USE_BITS (IMMD); break;
11093 case 'E': USE_BITS (IMME); break;
11094 case 'F': USE_BITS (IMMF); break;
11095 case 'G': USE_BITS (IMMG); break;
11096 case 'H': USE_BITS (IMMH); break;
11097 case 'I': USE_BITS (IMMI); break;
11098 case 'J': USE_BITS (IMMJ); break;
11099 case 'L': USE_BITS (IMML); break;
11100 case 'M': USE_BITS (IMMM); break;
11101 case 'N': USE_BITS (IMMN); break;
11102 case 'O': USE_BITS (IMMO); break;
11103 case 'P': USE_BITS (IMMP); break;
11104 case 'Q': USE_BITS (IMMQ); break;
11105 case 'U': USE_BITS (IMMU); break;
11106 case 'W': USE_BITS (IMMW); break;
11107 case 'X': USE_BITS (IMMX); break;
11108 case 'Y': USE_BITS (IMMY); break;
11109 case 'Z': break;
11110 case 'a': break;
11111 case 'b': USE_BITS (MB); break;
11112 case 'c': USE_BITS (MC); break;
11113 case 'd': USE_BITS (MD); break;
11114 case 'e': USE_BITS (ME); break;
11115 case 'f': USE_BITS (MF); break;
11116 case 'g': USE_BITS (MG); break;
11117 case 'h': USE_BITS (MH); break;
11118 case 'i': USE_BITS (MI); break;
11119 case 'j': USE_BITS (MJ); break;
11120 case 'l': USE_BITS (ML); break;
11121 case 'm': USE_BITS (MM); break;
11122 case 'n': USE_BITS (MN); break;
11123 case 'p': USE_BITS (MP); break;
11124 case 'q': USE_BITS (MQ); break;
11125 case 'r': break;
11126 case 's': break;
11127 case 't': break;
11128 case 'x': break;
11129 case 'y': break;
11130 case 'z': break;
11131 default:
11132 as_bad (_("Internal error: bad mips opcode "
11133 "(unknown extension operand type `%c%c'): %s %s"),
11134 e, c, opc->name, opc->args);
11135 return 0;
11136 }
11137 break;
11138 case '.': USE_BITS (OFFSET10); break;
11139 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
11140 case '2': USE_BITS (BP); break;
11141 case '3': USE_BITS (SA3); break;
11142 case '4': USE_BITS (SA4); break;
11143 case '5': USE_BITS (IMM8); break;
11144 case '6': USE_BITS (RS); break;
11145 case '7': USE_BITS (DSPACC); break;
11146 case '8': USE_BITS (WRDSP); break;
11147 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
11148 case '<': USE_BITS (SHAMT); break;
11149 case '>': USE_BITS (SHAMT); break;
03f66e8a 11150 case '@': USE_BITS (IMM10); break;
df58fc94
RS
11151 case 'B': USE_BITS (CODE10); break;
11152 case 'C': USE_BITS (COPZ); break;
11153 case 'D': USE_BITS (FD); break;
11154 case 'E': USE_BITS (RT); break;
11155 case 'G': USE_BITS (RS); break;
444d75be 11156 case 'H': USE_BITS (SEL); break;
df58fc94
RS
11157 case 'K': USE_BITS (RS); break;
11158 case 'M': USE_BITS (CCC); break;
11159 case 'N': USE_BITS (BCC); break;
11160 case 'R': USE_BITS (FR); break;
11161 case 'S': USE_BITS (FS); break;
11162 case 'T': USE_BITS (FT); break;
11163 case 'V': USE_BITS (FS); break;
dec0624d 11164 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 11165 case '^': USE_BITS (RD); break;
df58fc94
RS
11166 case 'a': USE_BITS (TARGET); break;
11167 case 'b': USE_BITS (RS); break;
11168 case 'c': USE_BITS (CODE); break;
11169 case 'd': USE_BITS (RD); break;
11170 case 'h': USE_BITS (PREFX); break;
11171 case 'i': USE_BITS (IMMEDIATE); break;
11172 case 'j': USE_BITS (DELTA); break;
11173 case 'k': USE_BITS (CACHE); break;
11174 case 'n': USE_BITS (RT); break;
11175 case 'o': USE_BITS (DELTA); break;
11176 case 'p': USE_BITS (DELTA); break;
11177 case 'q': USE_BITS (CODE2); break;
11178 case 'r': USE_BITS (RS); break;
11179 case 's': USE_BITS (RS); break;
11180 case 't': USE_BITS (RT); break;
11181 case 'u': USE_BITS (IMMEDIATE); break;
11182 case 'v': USE_BITS (RS); break;
11183 case 'w': USE_BITS (RT); break;
11184 case 'y': USE_BITS (RS3); break;
11185 case 'z': break;
11186 case '|': USE_BITS (TRAP); break;
11187 case '~': USE_BITS (OFFSET12); break;
11188 default:
11189 as_bad (_("Internal error: bad microMIPS opcode "
11190 "(unknown operand type `%c'): %s %s"),
11191 c, opc->name, opc->args);
11192 return 0;
11193 }
11194#undef USE_BITS
11195 if (used_bits != insn_bits)
11196 {
11197 if (~used_bits & insn_bits)
11198 as_bad (_("Internal error: bad microMIPS opcode "
11199 "(bits 0x%lx undefined): %s %s"),
11200 ~used_bits & insn_bits, opc->name, opc->args);
11201 if (used_bits & ~insn_bits)
11202 as_bad (_("Internal error: bad microMIPS opcode "
11203 "(bits 0x%lx defined): %s %s"),
11204 used_bits & ~insn_bits, opc->name, opc->args);
11205 return 0;
11206 }
11207 return 1;
11208}
11209
9bcd4f99
TS
11210/* UDI immediates. */
11211struct mips_immed {
11212 char type;
11213 unsigned int shift;
11214 unsigned long mask;
11215 const char * desc;
11216};
11217
11218static const struct mips_immed mips_immed[] = {
11219 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
11220 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
11221 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
11222 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
11223 { 0,0,0,0 }
11224};
11225
7455baf8
TS
11226/* Check whether an odd floating-point register is allowed. */
11227static int
11228mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
11229{
11230 const char *s = insn->name;
11231
11232 if (insn->pinfo == INSN_MACRO)
11233 /* Let a macro pass, we'll catch it later when it is expanded. */
11234 return 1;
11235
e407c74b 11236 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
11237 {
11238 /* Allow odd registers for single-precision ops. */
11239 switch (insn->pinfo & (FP_S | FP_D))
11240 {
11241 case FP_S:
11242 case 0:
11243 return 1; /* both single precision - ok */
11244 case FP_D:
11245 return 0; /* both double precision - fail */
11246 default:
11247 break;
11248 }
11249
11250 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
11251 s = strchr (insn->name, '.');
11252 if (argnum == 2)
11253 s = s != NULL ? strchr (s + 1, '.') : NULL;
11254 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
11255 }
11256
11257 /* Single-precision coprocessor loads and moves are OK too. */
11258 if ((insn->pinfo & FP_S)
11259 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
11260 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
11261 return 1;
11262
11263 return 0;
11264}
11265
df58fc94
RS
11266/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
11267 taking bits from BIT up. */
11268static int
11269expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
11270{
11271 return (ep->X_op == O_constant
11272 && (ep->X_add_number & ((1 << bit) - 1)) == 0
11273 && ep->X_add_number >= min << bit
11274 && ep->X_add_number < max << bit);
11275}
11276
252b5132
RH
11277/* This routine assembles an instruction into its binary format. As a
11278 side effect, it sets one of the global variables imm_reloc or
11279 offset_reloc to the type of relocation to do if one of the operands
11280 is an address expression. */
11281
11282static void
17a2f251 11283mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 11284{
df58fc94
RS
11285 bfd_boolean wrong_delay_slot_insns = FALSE;
11286 bfd_boolean need_delay_slot_ok = TRUE;
11287 struct mips_opcode *firstinsn = NULL;
11288 const struct mips_opcode *past;
11289 struct hash_control *hash;
252b5132
RH
11290 char *s;
11291 const char *args;
43841e91 11292 char c = 0;
252b5132
RH
11293 struct mips_opcode *insn;
11294 char *argsStart;
11295 unsigned int regno;
34224acf 11296 unsigned int lastregno;
df58fc94 11297 unsigned int destregno = 0;
af7ee8bf 11298 unsigned int lastpos = 0;
071742cf 11299 unsigned int limlo, limhi;
f02d8318 11300 int sizelo;
252b5132 11301 char *s_reset;
74cd071d 11302 offsetT min_range, max_range;
df58fc94 11303 long opend;
a40bc9dd 11304 char *name;
707bfff6
TS
11305 int argnum;
11306 unsigned int rtype;
df58fc94 11307 char *dot;
a40bc9dd 11308 long end;
252b5132
RH
11309
11310 insn_error = NULL;
11311
df58fc94
RS
11312 if (mips_opts.micromips)
11313 {
11314 hash = micromips_op_hash;
11315 past = &micromips_opcodes[bfd_micromips_num_opcodes];
11316 }
11317 else
11318 {
11319 hash = op_hash;
11320 past = &mips_opcodes[NUMOPCODES];
11321 }
11322 forced_insn_length = 0;
252b5132 11323 insn = NULL;
252b5132 11324
df58fc94 11325 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
11326 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
11327 continue;
bdaaa2e1 11328
a40bc9dd
RS
11329 /* Make a copy of the instruction so that we can fiddle with it. */
11330 name = alloca (end + 1);
11331 memcpy (name, str, end);
11332 name[end] = '\0';
252b5132 11333
df58fc94
RS
11334 for (;;)
11335 {
11336 insn = (struct mips_opcode *) hash_find (hash, name);
11337
11338 if (insn != NULL || !mips_opts.micromips)
11339 break;
11340 if (forced_insn_length)
11341 break;
11342
11343 /* See if there's an instruction size override suffix,
11344 either `16' or `32', at the end of the mnemonic proper,
11345 that defines the operation, i.e. before the first `.'
11346 character if any. Strip it and retry. */
11347 dot = strchr (name, '.');
11348 opend = dot != NULL ? dot - name : end;
11349 if (opend < 3)
11350 break;
11351 if (name[opend - 2] == '1' && name[opend - 1] == '6')
11352 forced_insn_length = 2;
11353 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
11354 forced_insn_length = 4;
11355 else
11356 break;
11357 memcpy (name + opend - 2, name + opend, end - opend + 1);
11358 }
252b5132
RH
11359 if (insn == NULL)
11360 {
a40bc9dd
RS
11361 insn_error = _("Unrecognized opcode");
11362 return;
252b5132
RH
11363 }
11364
df58fc94
RS
11365 /* For microMIPS instructions placed in a fixed-length branch delay slot
11366 we make up to two passes over the relevant fragment of the opcode
11367 table. First we try instructions that meet the delay slot's length
11368 requirement. If none matched, then we retry with the remaining ones
11369 and if one matches, then we use it and then issue an appropriate
11370 warning later on. */
a40bc9dd 11371 argsStart = s = str + end;
252b5132
RH
11372 for (;;)
11373 {
df58fc94
RS
11374 bfd_boolean delay_slot_ok;
11375 bfd_boolean size_ok;
b34976b6 11376 bfd_boolean ok;
252b5132 11377
a40bc9dd 11378 gas_assert (strcmp (insn->name, name) == 0);
252b5132 11379
f79e2745 11380 ok = is_opcode_valid (insn);
df58fc94
RS
11381 size_ok = is_size_valid (insn);
11382 delay_slot_ok = is_delay_slot_valid (insn);
11383 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 11384 {
df58fc94
RS
11385 firstinsn = insn;
11386 wrong_delay_slot_insns = TRUE;
11387 }
11388 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11389 {
11390 static char buf[256];
11391
11392 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
11393 {
11394 ++insn;
11395 continue;
11396 }
df58fc94 11397 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 11398 {
df58fc94
RS
11399 gas_assert (firstinsn);
11400 need_delay_slot_ok = FALSE;
11401 past = insn + 1;
11402 insn = firstinsn;
11403 continue;
252b5132 11404 }
df58fc94
RS
11405
11406 if (insn_error)
11407 return;
11408
11409 if (!ok)
7bd942df 11410 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
11411 mips_cpu_info_from_arch (mips_opts.arch)->name,
11412 mips_cpu_info_from_isa (mips_opts.isa)->name);
833794fc
MR
11413 else if (mips_opts.insn32)
11414 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
df58fc94
RS
11415 else
11416 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11417 8 * forced_insn_length);
11418 insn_error = buf;
11419
11420 return;
252b5132
RH
11421 }
11422
1e915849 11423 create_insn (ip, insn);
268f6bed 11424 insn_error = NULL;
707bfff6 11425 argnum = 1;
24864476 11426 lastregno = 0xffffffff;
252b5132
RH
11427 for (args = insn->args;; ++args)
11428 {
deec1734
CD
11429 int is_mdmx;
11430
ad8d3bb3 11431 s += strspn (s, " \t");
deec1734 11432 is_mdmx = 0;
252b5132
RH
11433 switch (*args)
11434 {
11435 case '\0': /* end of args */
11436 if (*s == '\0')
11437 return;
11438 break;
11439
03f66e8a
MR
11440 case '2':
11441 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11442 code) or 14 (for microMIPS code). */
8b082fb1
TS
11443 my_getExpression (&imm_expr, s);
11444 check_absolute_expr (ip, &imm_expr);
11445 if ((unsigned long) imm_expr.X_add_number != 1
11446 && (unsigned long) imm_expr.X_add_number != 3)
11447 {
11448 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11449 (unsigned long) imm_expr.X_add_number);
11450 }
03f66e8a
MR
11451 INSERT_OPERAND (mips_opts.micromips,
11452 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
11453 imm_expr.X_op = O_absent;
11454 s = expr_end;
11455 continue;
11456
03f66e8a 11457 case '3':
c3678916
RS
11458 /* DSP 3-bit unsigned immediate in bit 21 (for standard MIPS
11459 code) or 13 (for microMIPS code). */
03f66e8a
MR
11460 {
11461 unsigned long mask = (mips_opts.micromips
11462 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11463
11464 my_getExpression (&imm_expr, s);
11465 check_absolute_expr (ip, &imm_expr);
11466 if ((unsigned long) imm_expr.X_add_number > mask)
11467 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11468 mask, (unsigned long) imm_expr.X_add_number);
11469 INSERT_OPERAND (mips_opts.micromips,
11470 SA3, *ip, imm_expr.X_add_number);
11471 imm_expr.X_op = O_absent;
11472 s = expr_end;
11473 }
74cd071d
CF
11474 continue;
11475
03f66e8a 11476 case '4':
c3678916
RS
11477 /* DSP 4-bit unsigned immediate in bit 21 (for standard MIPS
11478 code) or 12 (for microMIPS code). */
03f66e8a
MR
11479 {
11480 unsigned long mask = (mips_opts.micromips
11481 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11482
11483 my_getExpression (&imm_expr, s);
11484 check_absolute_expr (ip, &imm_expr);
11485 if ((unsigned long) imm_expr.X_add_number > mask)
11486 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11487 mask, (unsigned long) imm_expr.X_add_number);
11488 INSERT_OPERAND (mips_opts.micromips,
11489 SA4, *ip, imm_expr.X_add_number);
11490 imm_expr.X_op = O_absent;
11491 s = expr_end;
11492 }
74cd071d
CF
11493 continue;
11494
03f66e8a 11495 case '5':
c3678916
RS
11496 /* DSP 8-bit unsigned immediate in bit 16 (for standard MIPS
11497 code) or 13 (for microMIPS code). */
03f66e8a
MR
11498 {
11499 unsigned long mask = (mips_opts.micromips
11500 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11501
11502 my_getExpression (&imm_expr, s);
11503 check_absolute_expr (ip, &imm_expr);
11504 if ((unsigned long) imm_expr.X_add_number > mask)
11505 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11506 mask, (unsigned long) imm_expr.X_add_number);
11507 INSERT_OPERAND (mips_opts.micromips,
11508 IMM8, *ip, imm_expr.X_add_number);
11509 imm_expr.X_op = O_absent;
11510 s = expr_end;
11511 }
74cd071d
CF
11512 continue;
11513
03f66e8a 11514 case '6':
c3678916
RS
11515 /* DSP 5-bit unsigned immediate in bit 21 (for standard MIPS
11516 code) or 16 (for microMIPS code). */
03f66e8a
MR
11517 {
11518 unsigned long mask = (mips_opts.micromips
11519 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11520
11521 my_getExpression (&imm_expr, s);
11522 check_absolute_expr (ip, &imm_expr);
11523 if ((unsigned long) imm_expr.X_add_number > mask)
11524 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11525 mask, (unsigned long) imm_expr.X_add_number);
11526 INSERT_OPERAND (mips_opts.micromips,
11527 RS, *ip, imm_expr.X_add_number);
11528 imm_expr.X_op = O_absent;
11529 s = expr_end;
11530 }
74cd071d
CF
11531 continue;
11532
c3678916
RS
11533 case '7':
11534 /* Four DSP accumulators in bit 11 (for standard MIPS code)
11535 or 14 (for microMIPS code). */
03f66e8a
MR
11536 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11537 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11538 {
11539 regno = s[3] - '0';
11540 s += 4;
03f66e8a 11541 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11542 continue;
11543 }
11544 else
11545 as_bad (_("Invalid dsp acc register"));
11546 break;
11547
03f66e8a
MR
11548 case '8':
11549 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11550 code) or 14 (for microMIPS code). */
11551 {
11552 unsigned long mask = (mips_opts.micromips
11553 ? MICROMIPSOP_MASK_WRDSP
11554 : OP_MASK_WRDSP);
11555
11556 my_getExpression (&imm_expr, s);
11557 check_absolute_expr (ip, &imm_expr);
11558 if ((unsigned long) imm_expr.X_add_number > mask)
11559 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11560 mask, (unsigned long) imm_expr.X_add_number);
11561 INSERT_OPERAND (mips_opts.micromips,
11562 WRDSP, *ip, imm_expr.X_add_number);
11563 imm_expr.X_op = O_absent;
11564 s = expr_end;
11565 }
74cd071d
CF
11566 continue;
11567
90ecf173 11568 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11569 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11570 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11571 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11572 {
11573 regno = s[3] - '0';
11574 s += 4;
df58fc94 11575 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11576 continue;
11577 }
11578 else
11579 as_bad (_("Invalid dsp acc register"));
11580 break;
11581
03f66e8a 11582 case '0':
c3678916
RS
11583 /* DSP 6-bit signed immediate in bit 20 (for standard MIPS
11584 code) or 16 (for microMIPS code). */
03f66e8a
MR
11585 {
11586 long mask = (mips_opts.micromips
11587 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11588
11589 my_getExpression (&imm_expr, s);
11590 check_absolute_expr (ip, &imm_expr);
11591 min_range = -((mask + 1) >> 1);
11592 max_range = ((mask + 1) >> 1) - 1;
11593 if (imm_expr.X_add_number < min_range
11594 || imm_expr.X_add_number > max_range)
a9e24354
TS
11595 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11596 (long) min_range, (long) max_range,
11597 (long) imm_expr.X_add_number);
03f66e8a
MR
11598 INSERT_OPERAND (mips_opts.micromips,
11599 DSPSFT, *ip, imm_expr.X_add_number);
11600 imm_expr.X_op = O_absent;
11601 s = expr_end;
11602 }
74cd071d
CF
11603 continue;
11604
90ecf173 11605 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11606 gas_assert (!mips_opts.micromips);
74cd071d
CF
11607 my_getExpression (&imm_expr, s);
11608 check_absolute_expr (ip, &imm_expr);
11609 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11610 {
a9e24354
TS
11611 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11612 OP_MASK_RDDSP,
11613 (unsigned long) imm_expr.X_add_number);
74cd071d 11614 }
df58fc94 11615 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11616 imm_expr.X_op = O_absent;
11617 s = expr_end;
11618 continue;
11619
90ecf173 11620 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11621 gas_assert (!mips_opts.micromips);
74cd071d
CF
11622 my_getExpression (&imm_expr, s);
11623 check_absolute_expr (ip, &imm_expr);
11624 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11625 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11626 if (imm_expr.X_add_number < min_range ||
11627 imm_expr.X_add_number > max_range)
11628 {
a9e24354
TS
11629 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11630 (long) min_range, (long) max_range,
11631 (long) imm_expr.X_add_number);
74cd071d 11632 }
df58fc94 11633 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11634 imm_expr.X_op = O_absent;
11635 s = expr_end;
11636 continue;
11637
90ecf173 11638 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11639 {
11640 long mask = (mips_opts.micromips
11641 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11642
11643 my_getExpression (&imm_expr, s);
11644 check_absolute_expr (ip, &imm_expr);
11645 min_range = -((mask + 1) >> 1);
11646 max_range = ((mask + 1) >> 1) - 1;
11647 if (imm_expr.X_add_number < min_range
11648 || imm_expr.X_add_number > max_range)
a9e24354
TS
11649 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11650 (long) min_range, (long) max_range,
11651 (long) imm_expr.X_add_number);
03f66e8a
MR
11652 INSERT_OPERAND (mips_opts.micromips,
11653 IMM10, *ip, imm_expr.X_add_number);
11654 imm_expr.X_op = O_absent;
11655 s = expr_end;
11656 }
11657 continue;
11658
11659 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11660 gas_assert (mips_opts.micromips);
11661 my_getExpression (&imm_expr, s);
11662 check_absolute_expr (ip, &imm_expr);
11663 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11664 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11665 MICROMIPSOP_MASK_RD,
11666 (unsigned long) imm_expr.X_add_number);
11667 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11668 imm_expr.X_op = O_absent;
11669 s = expr_end;
11670 continue;
11671
a9e24354 11672 case '!': /* MT usermode flag bit. */
df58fc94 11673 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11674 my_getExpression (&imm_expr, s);
11675 check_absolute_expr (ip, &imm_expr);
11676 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11677 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11678 (unsigned long) imm_expr.X_add_number);
df58fc94 11679 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11680 imm_expr.X_op = O_absent;
11681 s = expr_end;
11682 continue;
11683
a9e24354 11684 case '$': /* MT load high flag bit. */
df58fc94 11685 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11686 my_getExpression (&imm_expr, s);
11687 check_absolute_expr (ip, &imm_expr);
11688 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11689 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11690 (unsigned long) imm_expr.X_add_number);
df58fc94 11691 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11692 imm_expr.X_op = O_absent;
11693 s = expr_end;
11694 continue;
11695
90ecf173 11696 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11697 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11698 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11699 s[3] >= '0' && s[3] <= '3')
11700 {
11701 regno = s[3] - '0';
11702 s += 4;
df58fc94 11703 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11704 continue;
11705 }
11706 else
11707 as_bad (_("Invalid dsp/smartmips acc register"));
11708 break;
11709
90ecf173 11710 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11711 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11712 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11713 s[3] >= '0' && s[3] <= '3')
11714 {
11715 regno = s[3] - '0';
11716 s += 4;
df58fc94 11717 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11718 continue;
11719 }
11720 else
11721 as_bad (_("Invalid dsp/smartmips acc register"));
11722 break;
11723
dec0624d
MR
11724 case '\\': /* 3-bit bit position. */
11725 {
2906b037
MR
11726 unsigned long mask = (mips_opts.micromips
11727 ? MICROMIPSOP_MASK_3BITPOS
11728 : OP_MASK_3BITPOS);
dec0624d
MR
11729
11730 my_getExpression (&imm_expr, s);
11731 check_absolute_expr (ip, &imm_expr);
11732 if ((unsigned long) imm_expr.X_add_number > mask)
11733 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11734 ip->insn_mo->name,
11735 mask, (unsigned long) imm_expr.X_add_number);
11736 INSERT_OPERAND (mips_opts.micromips,
11737 3BITPOS, *ip, imm_expr.X_add_number);
11738 imm_expr.X_op = O_absent;
11739 s = expr_end;
11740 }
11741 continue;
11742
252b5132 11743 case ',':
a339155f 11744 ++argnum;
252b5132
RH
11745 if (*s++ == *args)
11746 continue;
11747 s--;
11748 switch (*++args)
11749 {
11750 case 'r':
11751 case 'v':
df58fc94 11752 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11753 continue;
11754
11755 case 'w':
df58fc94 11756 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11757 continue;
11758
252b5132 11759 case 'W':
df58fc94
RS
11760 gas_assert (!mips_opts.micromips);
11761 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11762 continue;
11763
11764 case 'V':
df58fc94 11765 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11766 continue;
11767 }
11768 break;
11769
11770 case '(':
11771 /* Handle optional base register.
11772 Either the base register is omitted or
bdaaa2e1 11773 we must have a left paren. */
252b5132
RH
11774 /* This is dependent on the next operand specifier
11775 is a base register specification. */
df58fc94
RS
11776 gas_assert (args[1] == 'b'
11777 || (mips_opts.micromips
11778 && args[1] == 'm'
11779 && (args[2] == 'l' || args[2] == 'n'
11780 || args[2] == 's' || args[2] == 'a')));
11781 if (*s == '\0' && args[1] == 'b')
252b5132 11782 return;
df58fc94 11783 /* Fall through. */
252b5132 11784
90ecf173 11785 case ')': /* These must match exactly. */
df58fc94
RS
11786 if (*s++ == *args)
11787 continue;
11788 break;
11789
11790 case '[': /* These must match exactly. */
60b63b72 11791 case ']':
df58fc94 11792 gas_assert (!mips_opts.micromips);
252b5132
RH
11793 if (*s++ == *args)
11794 continue;
11795 break;
11796
af7ee8bf
CD
11797 case '+': /* Opcode extension character. */
11798 switch (*++args)
11799 {
9bcd4f99
TS
11800 case '1': /* UDI immediates. */
11801 case '2':
11802 case '3':
11803 case '4':
df58fc94 11804 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11805 {
11806 const struct mips_immed *imm = mips_immed;
11807
11808 while (imm->type && imm->type != *args)
11809 ++imm;
11810 if (! imm->type)
b37df7c4 11811 abort ();
9bcd4f99
TS
11812 my_getExpression (&imm_expr, s);
11813 check_absolute_expr (ip, &imm_expr);
11814 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11815 {
11816 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11817 imm->desc ? imm->desc : ip->insn_mo->name,
11818 (unsigned long) imm_expr.X_add_number,
11819 (unsigned long) imm_expr.X_add_number);
90ecf173 11820 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11821 }
11822 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11823 << imm->shift);
11824 imm_expr.X_op = O_absent;
11825 s = expr_end;
11826 }
11827 continue;
90ecf173 11828
b015e599
AP
11829 case 'J': /* 10-bit hypcall code. */
11830 gas_assert (!mips_opts.micromips);
11831 {
11832 unsigned long mask = OP_MASK_CODE10;
11833
11834 my_getExpression (&imm_expr, s);
11835 check_absolute_expr (ip, &imm_expr);
11836 if ((unsigned long) imm_expr.X_add_number > mask)
11837 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11838 ip->insn_mo->name,
11839 mask, (unsigned long) imm_expr.X_add_number);
11840 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11841 imm_expr.X_op = O_absent;
11842 s = expr_end;
11843 }
11844 continue;
11845
071742cf
CD
11846 case 'A': /* ins/ext position, becomes LSB. */
11847 limlo = 0;
11848 limhi = 31;
5f74bc13
CD
11849 goto do_lsb;
11850 case 'E':
11851 limlo = 32;
11852 limhi = 63;
11853 goto do_lsb;
90ecf173 11854 do_lsb:
071742cf
CD
11855 my_getExpression (&imm_expr, s);
11856 check_absolute_expr (ip, &imm_expr);
11857 if ((unsigned long) imm_expr.X_add_number < limlo
11858 || (unsigned long) imm_expr.X_add_number > limhi)
11859 {
11860 as_bad (_("Improper position (%lu)"),
11861 (unsigned long) imm_expr.X_add_number);
11862 imm_expr.X_add_number = limlo;
11863 }
11864 lastpos = imm_expr.X_add_number;
df58fc94
RS
11865 INSERT_OPERAND (mips_opts.micromips,
11866 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11867 imm_expr.X_op = O_absent;
11868 s = expr_end;
11869 continue;
11870
11871 case 'B': /* ins size, becomes MSB. */
11872 limlo = 1;
11873 limhi = 32;
5f74bc13
CD
11874 goto do_msb;
11875 case 'F':
11876 limlo = 33;
11877 limhi = 64;
11878 goto do_msb;
90ecf173 11879 do_msb:
071742cf
CD
11880 my_getExpression (&imm_expr, s);
11881 check_absolute_expr (ip, &imm_expr);
11882 /* Check for negative input so that small negative numbers
11883 will not succeed incorrectly. The checks against
11884 (pos+size) transitively check "size" itself,
11885 assuming that "pos" is reasonable. */
11886 if ((long) imm_expr.X_add_number < 0
11887 || ((unsigned long) imm_expr.X_add_number
11888 + lastpos) < limlo
11889 || ((unsigned long) imm_expr.X_add_number
11890 + lastpos) > limhi)
11891 {
11892 as_bad (_("Improper insert size (%lu, position %lu)"),
11893 (unsigned long) imm_expr.X_add_number,
11894 (unsigned long) lastpos);
11895 imm_expr.X_add_number = limlo - lastpos;
11896 }
df58fc94
RS
11897 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11898 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11899 imm_expr.X_op = O_absent;
11900 s = expr_end;
11901 continue;
11902
11903 case 'C': /* ext size, becomes MSBD. */
11904 limlo = 1;
11905 limhi = 32;
f02d8318 11906 sizelo = 1;
5f74bc13
CD
11907 goto do_msbd;
11908 case 'G':
11909 limlo = 33;
11910 limhi = 64;
f02d8318 11911 sizelo = 33;
5f74bc13
CD
11912 goto do_msbd;
11913 case 'H':
11914 limlo = 33;
11915 limhi = 64;
f02d8318 11916 sizelo = 1;
5f74bc13 11917 goto do_msbd;
90ecf173 11918 do_msbd:
071742cf
CD
11919 my_getExpression (&imm_expr, s);
11920 check_absolute_expr (ip, &imm_expr);
f02d8318
CF
11921 /* The checks against (pos+size) don't transitively check
11922 "size" itself, assuming that "pos" is reasonable.
11923 We also need to check the lower bound of "size". */
11924 if ((long) imm_expr.X_add_number < sizelo
071742cf
CD
11925 || ((unsigned long) imm_expr.X_add_number
11926 + lastpos) < limlo
11927 || ((unsigned long) imm_expr.X_add_number
11928 + lastpos) > limhi)
11929 {
11930 as_bad (_("Improper extract size (%lu, position %lu)"),
11931 (unsigned long) imm_expr.X_add_number,
11932 (unsigned long) lastpos);
11933 imm_expr.X_add_number = limlo - lastpos;
11934 }
df58fc94
RS
11935 INSERT_OPERAND (mips_opts.micromips,
11936 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11937 imm_expr.X_op = O_absent;
11938 s = expr_end;
11939 continue;
af7ee8bf 11940
bbcc0807
CD
11941 case 'D':
11942 /* +D is for disassembly only; never match. */
11943 break;
11944
5f74bc13
CD
11945 case 'I':
11946 /* "+I" is like "I", except that imm2_expr is used. */
11947 my_getExpression (&imm2_expr, s);
11948 if (imm2_expr.X_op != O_big
11949 && imm2_expr.X_op != O_constant)
11950 insn_error = _("absolute expression required");
9ee2a2d4
MR
11951 if (HAVE_32BIT_GPRS)
11952 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11953 s = expr_end;
11954 continue;
11955
707bfff6 11956 case 'T': /* Coprocessor register. */
df58fc94 11957 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11958 /* +T is for disassembly only; never match. */
11959 break;
11960
707bfff6 11961 case 't': /* Coprocessor register number. */
df58fc94 11962 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11963 if (s[0] == '$' && ISDIGIT (s[1]))
11964 {
11965 ++s;
11966 regno = 0;
11967 do
11968 {
11969 regno *= 10;
11970 regno += *s - '0';
11971 ++s;
11972 }
11973 while (ISDIGIT (*s));
11974 if (regno > 31)
11975 as_bad (_("Invalid register number (%d)"), regno);
11976 else
11977 {
df58fc94 11978 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11979 continue;
11980 }
11981 }
11982 else
11983 as_bad (_("Invalid coprocessor 0 register number"));
11984 break;
11985
bb35fb24
NC
11986 case 'x':
11987 /* bbit[01] and bbit[01]32 bit index. Give error if index
11988 is not in the valid range. */
df58fc94 11989 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11990 my_getExpression (&imm_expr, s);
11991 check_absolute_expr (ip, &imm_expr);
11992 if ((unsigned) imm_expr.X_add_number > 31)
11993 {
11994 as_bad (_("Improper bit index (%lu)"),
11995 (unsigned long) imm_expr.X_add_number);
11996 imm_expr.X_add_number = 0;
11997 }
df58fc94 11998 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11999 imm_expr.X_op = O_absent;
12000 s = expr_end;
12001 continue;
12002
12003 case 'X':
12004 /* bbit[01] bit index when bbit is used but we generate
12005 bbit[01]32 because the index is over 32. Move to the
12006 next candidate if index is not in the valid range. */
df58fc94 12007 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12008 my_getExpression (&imm_expr, s);
12009 check_absolute_expr (ip, &imm_expr);
12010 if ((unsigned) imm_expr.X_add_number < 32
12011 || (unsigned) imm_expr.X_add_number > 63)
12012 break;
df58fc94 12013 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
12014 imm_expr.X_op = O_absent;
12015 s = expr_end;
12016 continue;
12017
12018 case 'p':
12019 /* cins, cins32, exts and exts32 position field. Give error
12020 if it's not in the valid range. */
df58fc94 12021 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12022 my_getExpression (&imm_expr, s);
12023 check_absolute_expr (ip, &imm_expr);
12024 if ((unsigned) imm_expr.X_add_number > 31)
12025 {
12026 as_bad (_("Improper position (%lu)"),
12027 (unsigned long) imm_expr.X_add_number);
12028 imm_expr.X_add_number = 0;
12029 }
12030 /* Make the pos explicit to simplify +S. */
12031 lastpos = imm_expr.X_add_number + 32;
df58fc94 12032 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
12033 imm_expr.X_op = O_absent;
12034 s = expr_end;
12035 continue;
12036
12037 case 'P':
12038 /* cins, cins32, exts and exts32 position field. Move to
12039 the next candidate if it's not in the valid range. */
df58fc94 12040 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12041 my_getExpression (&imm_expr, s);
12042 check_absolute_expr (ip, &imm_expr);
12043 if ((unsigned) imm_expr.X_add_number < 32
12044 || (unsigned) imm_expr.X_add_number > 63)
12045 break;
12046 lastpos = imm_expr.X_add_number;
df58fc94 12047 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
12048 imm_expr.X_op = O_absent;
12049 s = expr_end;
12050 continue;
12051
12052 case 's':
12053 /* cins and exts length-minus-one field. */
df58fc94 12054 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12055 my_getExpression (&imm_expr, s);
12056 check_absolute_expr (ip, &imm_expr);
12057 if ((unsigned long) imm_expr.X_add_number > 31)
12058 {
12059 as_bad (_("Improper size (%lu)"),
12060 (unsigned long) imm_expr.X_add_number);
12061 imm_expr.X_add_number = 0;
12062 }
df58fc94 12063 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
12064 imm_expr.X_op = O_absent;
12065 s = expr_end;
12066 continue;
12067
12068 case 'S':
12069 /* cins32/exts32 and cins/exts aliasing cint32/exts32
12070 length-minus-one field. */
df58fc94 12071 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12072 my_getExpression (&imm_expr, s);
12073 check_absolute_expr (ip, &imm_expr);
12074 if ((long) imm_expr.X_add_number < 0
12075 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
12076 {
12077 as_bad (_("Improper size (%lu)"),
12078 (unsigned long) imm_expr.X_add_number);
12079 imm_expr.X_add_number = 0;
12080 }
df58fc94 12081 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
12082 imm_expr.X_op = O_absent;
12083 s = expr_end;
12084 continue;
12085
dd3cbb7e
NC
12086 case 'Q':
12087 /* seqi/snei immediate field. */
df58fc94 12088 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
12089 my_getExpression (&imm_expr, s);
12090 check_absolute_expr (ip, &imm_expr);
12091 if ((long) imm_expr.X_add_number < -512
12092 || (long) imm_expr.X_add_number >= 512)
12093 {
12094 as_bad (_("Improper immediate (%ld)"),
12095 (long) imm_expr.X_add_number);
12096 imm_expr.X_add_number = 0;
12097 }
df58fc94 12098 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
12099 imm_expr.X_op = O_absent;
12100 s = expr_end;
12101 continue;
12102
98675402 12103 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 12104 gas_assert (!mips_opts.micromips);
98675402
RS
12105 my_getExpression (&imm_expr, s);
12106 check_absolute_expr (ip, &imm_expr);
12107 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
12108 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
12109 if (imm_expr.X_add_number < min_range
12110 || imm_expr.X_add_number > max_range)
12111 {
c95354ed 12112 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
12113 (long) min_range, (long) max_range,
12114 (long) imm_expr.X_add_number);
12115 }
df58fc94 12116 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
12117 imm_expr.X_op = O_absent;
12118 s = expr_end;
12119 continue;
12120
12121 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 12122 gas_assert (!mips_opts.micromips);
98675402
RS
12123 my_getExpression (&imm_expr, s);
12124 check_absolute_expr (ip, &imm_expr);
12125 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
12126 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
12127 if (imm_expr.X_add_number < min_range
12128 || imm_expr.X_add_number > max_range)
12129 {
c95354ed 12130 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
12131 (long) min_range, (long) max_range,
12132 (long) imm_expr.X_add_number);
12133 }
df58fc94 12134 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
12135 imm_expr.X_op = O_absent;
12136 s = expr_end;
12137 continue;
12138
12139 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 12140 gas_assert (!mips_opts.micromips);
98675402
RS
12141 my_getExpression (&imm_expr, s);
12142 check_absolute_expr (ip, &imm_expr);
12143 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
12144 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
12145 /* We check the offset range before adjusted. */
12146 min_range <<= 4;
12147 max_range <<= 4;
98675402
RS
12148 if (imm_expr.X_add_number < min_range
12149 || imm_expr.X_add_number > max_range)
12150 {
c95354ed 12151 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
12152 (long) min_range, (long) max_range,
12153 (long) imm_expr.X_add_number);
12154 }
c95354ed
MX
12155 if (imm_expr.X_add_number & 0xf)
12156 {
12157 as_bad (_("Offset not 16 bytes alignment (%ld)"),
12158 (long) imm_expr.X_add_number);
12159 }
12160 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
12161 INSERT_OPERAND (0, OFFSET_C, *ip,
12162 imm_expr.X_add_number >> 4);
98675402
RS
12163 imm_expr.X_op = O_absent;
12164 s = expr_end;
12165 continue;
12166
12167 case 'z':
df58fc94 12168 gas_assert (!mips_opts.micromips);
98675402
RS
12169 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
12170 break;
12171 if (regno == AT && mips_opts.at)
12172 {
12173 if (mips_opts.at == ATREG)
12174 as_warn (_("used $at without \".set noat\""));
12175 else
12176 as_warn (_("used $%u with \".set at=$%u\""),
12177 regno, mips_opts.at);
12178 }
df58fc94 12179 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
12180 continue;
12181
12182 case 'Z':
df58fc94 12183 gas_assert (!mips_opts.micromips);
98675402
RS
12184 if (!reg_lookup (&s, RTYPE_FPU, &regno))
12185 break;
df58fc94 12186 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
12187 continue;
12188
7f3c4072
CM
12189 case 'j':
12190 {
12191 int shift = 8;
12192 size_t i;
12193 /* Check whether there is only a single bracketed expression
12194 left. If so, it must be the base register and the
12195 constant must be zero. */
12196 if (*s == '(' && strchr (s + 1, '(') == 0)
12197 continue;
12198
12199 /* If this value won't fit into the offset, then go find
12200 a macro that will generate a 16- or 32-bit offset code
12201 pattern. */
12202 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12203 if ((i == 0 && (imm_expr.X_op != O_constant
12204 || imm_expr.X_add_number >= 1 << shift
12205 || imm_expr.X_add_number < -1 << shift))
12206 || i > 0)
12207 {
12208 imm_expr.X_op = O_absent;
12209 break;
12210 }
12211 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
12212 imm_expr.X_add_number);
12213 imm_expr.X_op = O_absent;
12214 s = expr_end;
12215 }
12216 continue;
12217
af7ee8bf 12218 default:
df58fc94 12219 as_bad (_("Internal error: bad %s opcode "
90ecf173 12220 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 12221 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 12222 *args, insn->name, insn->args);
af7ee8bf
CD
12223 /* Further processing is fruitless. */
12224 return;
12225 }
12226 break;
12227
df58fc94 12228 case '.': /* 10-bit offset. */
df58fc94 12229 gas_assert (mips_opts.micromips);
dec0624d 12230 case '~': /* 12-bit offset. */
df58fc94
RS
12231 {
12232 int shift = *args == '.' ? 9 : 11;
12233 size_t i;
12234
12235 /* Check whether there is only a single bracketed expression
12236 left. If so, it must be the base register and the
12237 constant must be zero. */
12238 if (*s == '(' && strchr (s + 1, '(') == 0)
12239 continue;
12240
12241 /* If this value won't fit into the offset, then go find
12242 a macro that will generate a 16- or 32-bit offset code
12243 pattern. */
12244 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12245 if ((i == 0 && (imm_expr.X_op != O_constant
12246 || imm_expr.X_add_number >= 1 << shift
12247 || imm_expr.X_add_number < -1 << shift))
12248 || i > 0)
12249 {
12250 imm_expr.X_op = O_absent;
12251 break;
12252 }
12253 if (shift == 9)
12254 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
12255 else
dec0624d
MR
12256 INSERT_OPERAND (mips_opts.micromips,
12257 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
12258 imm_expr.X_op = O_absent;
12259 s = expr_end;
12260 }
12261 continue;
12262
252b5132
RH
12263 case '<': /* must be at least one digit */
12264 /*
12265 * According to the manual, if the shift amount is greater
b6ff326e
KH
12266 * than 31 or less than 0, then the shift amount should be
12267 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
12268 * We issue a warning and mask out all but the low 5 bits.
12269 */
12270 my_getExpression (&imm_expr, s);
12271 check_absolute_expr (ip, &imm_expr);
12272 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
12273 as_warn (_("Improper shift amount (%lu)"),
12274 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12275 INSERT_OPERAND (mips_opts.micromips,
12276 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
12277 imm_expr.X_op = O_absent;
12278 s = expr_end;
12279 continue;
12280
12281 case '>': /* shift amount minus 32 */
12282 my_getExpression (&imm_expr, s);
12283 check_absolute_expr (ip, &imm_expr);
12284 if ((unsigned long) imm_expr.X_add_number < 32
12285 || (unsigned long) imm_expr.X_add_number > 63)
12286 break;
df58fc94
RS
12287 INSERT_OPERAND (mips_opts.micromips,
12288 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
12289 imm_expr.X_op = O_absent;
12290 s = expr_end;
12291 continue;
12292
90ecf173
MR
12293 case 'k': /* CACHE code. */
12294 case 'h': /* PREFX code. */
12295 case '1': /* SYNC type. */
252b5132
RH
12296 my_getExpression (&imm_expr, s);
12297 check_absolute_expr (ip, &imm_expr);
12298 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
12299 as_warn (_("Invalid value for `%s' (%lu)"),
12300 ip->insn_mo->name,
12301 (unsigned long) imm_expr.X_add_number);
df58fc94 12302 switch (*args)
d954098f 12303 {
df58fc94
RS
12304 case 'k':
12305 if (mips_fix_cn63xxp1
12306 && !mips_opts.micromips
12307 && strcmp ("pref", insn->name) == 0)
d954098f
DD
12308 switch (imm_expr.X_add_number)
12309 {
12310 case 5:
12311 case 25:
12312 case 26:
12313 case 27:
12314 case 28:
12315 case 29:
12316 case 30:
12317 case 31: /* These are ok. */
12318 break;
12319
12320 default: /* The rest must be changed to 28. */
12321 imm_expr.X_add_number = 28;
12322 break;
12323 }
df58fc94
RS
12324 INSERT_OPERAND (mips_opts.micromips,
12325 CACHE, *ip, imm_expr.X_add_number);
12326 break;
12327 case 'h':
12328 INSERT_OPERAND (mips_opts.micromips,
12329 PREFX, *ip, imm_expr.X_add_number);
12330 break;
12331 case '1':
12332 INSERT_OPERAND (mips_opts.micromips,
12333 STYPE, *ip, imm_expr.X_add_number);
12334 break;
d954098f 12335 }
252b5132
RH
12336 imm_expr.X_op = O_absent;
12337 s = expr_end;
12338 continue;
12339
90ecf173 12340 case 'c': /* BREAK code. */
df58fc94
RS
12341 {
12342 unsigned long mask = (mips_opts.micromips
12343 ? MICROMIPSOP_MASK_CODE
12344 : OP_MASK_CODE);
12345
12346 my_getExpression (&imm_expr, s);
12347 check_absolute_expr (ip, &imm_expr);
12348 if ((unsigned long) imm_expr.X_add_number > mask)
12349 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12350 ip->insn_mo->name,
12351 mask, (unsigned long) imm_expr.X_add_number);
12352 INSERT_OPERAND (mips_opts.micromips,
12353 CODE, *ip, imm_expr.X_add_number);
12354 imm_expr.X_op = O_absent;
12355 s = expr_end;
12356 }
252b5132
RH
12357 continue;
12358
90ecf173 12359 case 'q': /* Lower BREAK code. */
df58fc94
RS
12360 {
12361 unsigned long mask = (mips_opts.micromips
12362 ? MICROMIPSOP_MASK_CODE2
12363 : OP_MASK_CODE2);
12364
12365 my_getExpression (&imm_expr, s);
12366 check_absolute_expr (ip, &imm_expr);
12367 if ((unsigned long) imm_expr.X_add_number > mask)
12368 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12369 ip->insn_mo->name,
12370 mask, (unsigned long) imm_expr.X_add_number);
12371 INSERT_OPERAND (mips_opts.micromips,
12372 CODE2, *ip, imm_expr.X_add_number);
12373 imm_expr.X_op = O_absent;
12374 s = expr_end;
12375 }
252b5132
RH
12376 continue;
12377
df58fc94
RS
12378 case 'B': /* 20- or 10-bit syscall/break/wait code. */
12379 {
12380 unsigned long mask = (mips_opts.micromips
12381 ? MICROMIPSOP_MASK_CODE10
12382 : OP_MASK_CODE20);
12383
12384 my_getExpression (&imm_expr, s);
12385 check_absolute_expr (ip, &imm_expr);
12386 if ((unsigned long) imm_expr.X_add_number > mask)
12387 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12388 ip->insn_mo->name,
12389 mask, (unsigned long) imm_expr.X_add_number);
12390 if (mips_opts.micromips)
12391 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12392 else
12393 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12394 imm_expr.X_op = O_absent;
12395 s = expr_end;
12396 }
252b5132
RH
12397 continue;
12398
df58fc94
RS
12399 case 'C': /* 25- or 23-bit coprocessor code. */
12400 {
12401 unsigned long mask = (mips_opts.micromips
12402 ? MICROMIPSOP_MASK_COPZ
12403 : OP_MASK_COPZ);
12404
12405 my_getExpression (&imm_expr, s);
12406 check_absolute_expr (ip, &imm_expr);
12407 if ((unsigned long) imm_expr.X_add_number > mask)
12408 as_warn (_("Coproccesor code > %u bits (%lu)"),
12409 mips_opts.micromips ? 23U : 25U,
793b27f4 12410 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12411 INSERT_OPERAND (mips_opts.micromips,
12412 COPZ, *ip, imm_expr.X_add_number);
12413 imm_expr.X_op = O_absent;
12414 s = expr_end;
12415 }
beae10d5 12416 continue;
252b5132 12417
df58fc94
RS
12418 case 'J': /* 19-bit WAIT code. */
12419 gas_assert (!mips_opts.micromips);
4372b673
NC
12420 my_getExpression (&imm_expr, s);
12421 check_absolute_expr (ip, &imm_expr);
793b27f4 12422 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
12423 {
12424 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 12425 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12426 imm_expr.X_add_number &= OP_MASK_CODE19;
12427 }
12428 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
12429 imm_expr.X_op = O_absent;
12430 s = expr_end;
12431 continue;
12432
707bfff6 12433 case 'P': /* Performance register. */
df58fc94 12434 gas_assert (!mips_opts.micromips);
beae10d5 12435 my_getExpression (&imm_expr, s);
252b5132 12436 check_absolute_expr (ip, &imm_expr);
beae10d5 12437 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
12438 as_warn (_("Invalid performance register (%lu)"),
12439 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
12440 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12441 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12442 as_warn (_("Invalid performance register (%lu)"),
12443 (unsigned long) imm_expr.X_add_number);
df58fc94 12444 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
12445 imm_expr.X_op = O_absent;
12446 s = expr_end;
12447 continue;
252b5132 12448
707bfff6 12449 case 'G': /* Coprocessor destination register. */
df58fc94
RS
12450 {
12451 unsigned long opcode = ip->insn_opcode;
12452 unsigned long mask;
12453 unsigned int types;
12454 int cop0;
12455
12456 if (mips_opts.micromips)
12457 {
12458 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12459 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12460 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12461 opcode &= mask;
12462 switch (opcode)
12463 {
12464 case 0x000000fc: /* mfc0 */
12465 case 0x000002fc: /* mtc0 */
12466 case 0x580000fc: /* dmfc0 */
12467 case 0x580002fc: /* dmtc0 */
12468 cop0 = 1;
12469 break;
12470 default:
12471 cop0 = 0;
12472 break;
12473 }
12474 }
12475 else
12476 {
12477 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12478 cop0 = opcode == OP_OP_COP0;
12479 }
12480 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12481 ok = reg_lookup (&s, types, &regno);
12482 if (mips_opts.micromips)
12483 INSERT_OPERAND (1, RS, *ip, regno);
12484 else
12485 INSERT_OPERAND (0, RD, *ip, regno);
12486 if (ok)
12487 {
12488 lastregno = regno;
12489 continue;
12490 }
12491 }
12492 break;
707bfff6 12493
df58fc94
RS
12494 case 'y': /* ALNV.PS source register. */
12495 gas_assert (mips_opts.micromips);
12496 goto do_reg;
12497 case 'x': /* Ignore register name. */
12498 case 'U': /* Destination register (CLO/CLZ). */
12499 case 'g': /* Coprocessor destination register. */
12500 gas_assert (!mips_opts.micromips);
90ecf173
MR
12501 case 'b': /* Base register. */
12502 case 'd': /* Destination register. */
12503 case 's': /* Source register. */
12504 case 't': /* Target register. */
12505 case 'r': /* Both target and source. */
12506 case 'v': /* Both dest and source. */
12507 case 'w': /* Both dest and target. */
12508 case 'E': /* Coprocessor target register. */
12509 case 'K': /* RDHWR destination register. */
90ecf173 12510 case 'z': /* Must be zero register. */
df58fc94 12511 do_reg:
90ecf173 12512 s_reset = s;
707bfff6
TS
12513 if (*args == 'E' || *args == 'K')
12514 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12515 else
12516 {
12517 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
12518 if (regno == AT && mips_opts.at)
12519 {
12520 if (mips_opts.at == ATREG)
f71d0d44 12521 as_warn (_("Used $at without \".set noat\""));
741fe287 12522 else
f71d0d44 12523 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
12524 regno, mips_opts.at);
12525 }
707bfff6
TS
12526 }
12527 if (ok)
252b5132 12528 {
252b5132
RH
12529 c = *args;
12530 if (*s == ' ')
f9419b05 12531 ++s;
252b5132
RH
12532 if (args[1] != *s)
12533 {
12534 if (c == 'r' || c == 'v' || c == 'w')
12535 {
12536 regno = lastregno;
12537 s = s_reset;
f9419b05 12538 ++args;
252b5132
RH
12539 }
12540 }
12541 /* 'z' only matches $0. */
12542 if (c == 'z' && regno != 0)
12543 break;
12544
24864476 12545 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
12546 {
12547 if (regno == lastregno)
90ecf173
MR
12548 {
12549 insn_error
f71d0d44 12550 = _("Source and destination must be different");
e7c604dd 12551 continue;
90ecf173 12552 }
24864476 12553 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
12554 {
12555 insn_error
f71d0d44 12556 = _("A destination register must be supplied");
e7c604dd 12557 continue;
90ecf173 12558 }
e7c604dd 12559 }
90ecf173
MR
12560 /* Now that we have assembled one operand, we use the args
12561 string to figure out where it goes in the instruction. */
252b5132
RH
12562 switch (c)
12563 {
12564 case 'r':
12565 case 's':
12566 case 'v':
12567 case 'b':
df58fc94 12568 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12569 break;
df58fc94 12570
af7ee8bf 12571 case 'K':
df58fc94
RS
12572 if (mips_opts.micromips)
12573 INSERT_OPERAND (1, RS, *ip, regno);
12574 else
12575 INSERT_OPERAND (0, RD, *ip, regno);
12576 break;
12577
12578 case 'd':
ef2e4d86 12579 case 'g':
df58fc94 12580 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12581 break;
df58fc94 12582
4372b673 12583 case 'U':
df58fc94
RS
12584 gas_assert (!mips_opts.micromips);
12585 INSERT_OPERAND (0, RD, *ip, regno);
12586 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12587 break;
df58fc94 12588
252b5132
RH
12589 case 'w':
12590 case 't':
12591 case 'E':
df58fc94
RS
12592 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12593 break;
12594
12595 case 'y':
12596 gas_assert (mips_opts.micromips);
12597 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12598 break;
df58fc94 12599
252b5132
RH
12600 case 'x':
12601 /* This case exists because on the r3000 trunc
12602 expands into a macro which requires a gp
12603 register. On the r6000 or r4000 it is
12604 assembled into a single instruction which
12605 ignores the register. Thus the insn version
12606 is MIPS_ISA2 and uses 'x', and the macro
12607 version is MIPS_ISA1 and uses 't'. */
12608 break;
df58fc94 12609
252b5132
RH
12610 case 'z':
12611 /* This case is for the div instruction, which
12612 acts differently if the destination argument
12613 is $0. This only matches $0, and is checked
12614 outside the switch. */
12615 break;
252b5132
RH
12616 }
12617 lastregno = regno;
12618 continue;
12619 }
252b5132
RH
12620 switch (*args++)
12621 {
12622 case 'r':
12623 case 'v':
df58fc94 12624 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12625 continue;
df58fc94 12626
252b5132 12627 case 'w':
df58fc94 12628 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12629 continue;
12630 }
12631 break;
12632
deec1734 12633 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12634 gas_assert (!mips_opts.micromips);
deec1734
CD
12635 my_getExpression (&imm_expr, s);
12636 check_absolute_expr (ip, &imm_expr);
12637 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12638 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12639 (long) imm_expr.X_add_number);
df58fc94 12640 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12641 imm_expr.X_op = O_absent;
12642 s = expr_end;
12643 continue;
12644
12645 case 'Q': /* MDMX vector, element sel, or const. */
12646 if (s[0] != '$')
12647 {
12648 /* MDMX Immediate. */
df58fc94 12649 gas_assert (!mips_opts.micromips);
deec1734
CD
12650 my_getExpression (&imm_expr, s);
12651 check_absolute_expr (ip, &imm_expr);
12652 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12653 as_warn (_("Invalid MDMX Immediate (%ld)"),
12654 (long) imm_expr.X_add_number);
df58fc94 12655 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12656 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12657 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12658 else
12659 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12660 imm_expr.X_op = O_absent;
12661 s = expr_end;
12662 continue;
12663 }
12664 /* Not MDMX Immediate. Fall through. */
12665 case 'X': /* MDMX destination register. */
12666 case 'Y': /* MDMX source register. */
12667 case 'Z': /* MDMX target register. */
12668 is_mdmx = 1;
df58fc94
RS
12669 case 'W':
12670 gas_assert (!mips_opts.micromips);
90ecf173
MR
12671 case 'D': /* Floating point destination register. */
12672 case 'S': /* Floating point source register. */
12673 case 'T': /* Floating point target register. */
12674 case 'R': /* Floating point source register. */
252b5132 12675 case 'V':
707bfff6
TS
12676 rtype = RTYPE_FPU;
12677 if (is_mdmx
846ef2d0 12678 || ((mips_opts.ase & ASE_MDMX)
707bfff6
TS
12679 && (ip->insn_mo->pinfo & FP_D)
12680 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12681 | INSN_COPROC_MEMORY_DELAY
12682 | INSN_LOAD_COPROC_DELAY
12683 | INSN_LOAD_MEMORY_DELAY
12684 | INSN_STORE_MEMORY))))
12685 rtype |= RTYPE_VEC;
252b5132 12686 s_reset = s;
707bfff6 12687 if (reg_lookup (&s, rtype, &regno))
252b5132 12688 {
252b5132 12689 if ((regno & 1) != 0
ca4e0257 12690 && HAVE_32BIT_FPRS
90ecf173 12691 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12692 as_warn (_("Float register should be even, was %d"),
12693 regno);
12694
12695 c = *args;
12696 if (*s == ' ')
f9419b05 12697 ++s;
252b5132
RH
12698 if (args[1] != *s)
12699 {
12700 if (c == 'V' || c == 'W')
12701 {
12702 regno = lastregno;
12703 s = s_reset;
f9419b05 12704 ++args;
252b5132
RH
12705 }
12706 }
12707 switch (c)
12708 {
12709 case 'D':
deec1734 12710 case 'X':
df58fc94 12711 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12712 break;
df58fc94 12713
252b5132
RH
12714 case 'V':
12715 case 'S':
deec1734 12716 case 'Y':
df58fc94 12717 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12718 break;
df58fc94 12719
deec1734
CD
12720 case 'Q':
12721 /* This is like 'Z', but also needs to fix the MDMX
12722 vector/scalar select bits. Note that the
12723 scalar immediate case is handled above. */
12724 if (*s == '[')
12725 {
12726 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12727 int max_el = (is_qh ? 3 : 7);
12728 s++;
12729 my_getExpression(&imm_expr, s);
12730 check_absolute_expr (ip, &imm_expr);
12731 s = expr_end;
12732 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12733 as_bad (_("Bad element selector %ld"),
12734 (long) imm_expr.X_add_number);
deec1734
CD
12735 imm_expr.X_add_number &= max_el;
12736 ip->insn_opcode |= (imm_expr.X_add_number
12737 << (OP_SH_VSEL +
12738 (is_qh ? 2 : 1)));
01a3f561 12739 imm_expr.X_op = O_absent;
deec1734 12740 if (*s != ']')
20203fb9 12741 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12742 else
12743 s++;
12744 }
12745 else
12746 {
12747 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12748 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12749 << OP_SH_VSEL);
12750 else
12751 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12752 OP_SH_VSEL);
12753 }
90ecf173 12754 /* Fall through. */
252b5132
RH
12755 case 'W':
12756 case 'T':
deec1734 12757 case 'Z':
df58fc94 12758 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12759 break;
df58fc94 12760
252b5132 12761 case 'R':
df58fc94 12762 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12763 break;
12764 }
12765 lastregno = regno;
12766 continue;
12767 }
12768
252b5132
RH
12769 switch (*args++)
12770 {
12771 case 'V':
df58fc94 12772 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12773 continue;
df58fc94 12774
252b5132 12775 case 'W':
df58fc94 12776 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12777 continue;
12778 }
12779 break;
12780
12781 case 'I':
12782 my_getExpression (&imm_expr, s);
12783 if (imm_expr.X_op != O_big
12784 && imm_expr.X_op != O_constant)
12785 insn_error = _("absolute expression required");
9ee2a2d4
MR
12786 if (HAVE_32BIT_GPRS)
12787 normalize_constant_expr (&imm_expr);
252b5132
RH
12788 s = expr_end;
12789 continue;
12790
12791 case 'A':
12792 my_getExpression (&offset_expr, s);
2051e8c4 12793 normalize_address_expr (&offset_expr);
f6688943 12794 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12795 s = expr_end;
12796 continue;
12797
12798 case 'F':
12799 case 'L':
12800 case 'f':
12801 case 'l':
12802 {
12803 int f64;
ca4e0257 12804 int using_gprs;
252b5132
RH
12805 char *save_in;
12806 char *err;
12807 unsigned char temp[8];
12808 int len;
12809 unsigned int length;
12810 segT seg;
12811 subsegT subseg;
12812 char *p;
12813
12814 /* These only appear as the last operand in an
12815 instruction, and every instruction that accepts
12816 them in any variant accepts them in all variants.
12817 This means we don't have to worry about backing out
12818 any changes if the instruction does not match.
12819
12820 The difference between them is the size of the
12821 floating point constant and where it goes. For 'F'
12822 and 'L' the constant is 64 bits; for 'f' and 'l' it
12823 is 32 bits. Where the constant is placed is based
12824 on how the MIPS assembler does things:
12825 F -- .rdata
12826 L -- .lit8
12827 f -- immediate value
12828 l -- .lit4
12829
12830 The .lit4 and .lit8 sections are only used if
12831 permitted by the -G argument.
12832
ca4e0257
RS
12833 The code below needs to know whether the target register
12834 is 32 or 64 bits wide. It relies on the fact 'f' and
12835 'F' are used with GPR-based instructions and 'l' and
12836 'L' are used with FPR-based instructions. */
252b5132
RH
12837
12838 f64 = *args == 'F' || *args == 'L';
ca4e0257 12839 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12840
12841 save_in = input_line_pointer;
12842 input_line_pointer = s;
12843 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12844 length = len;
12845 s = input_line_pointer;
12846 input_line_pointer = save_in;
12847 if (err != NULL && *err != '\0')
12848 {
12849 as_bad (_("Bad floating point constant: %s"), err);
12850 memset (temp, '\0', sizeof temp);
12851 length = f64 ? 8 : 4;
12852 }
12853
9c2799c2 12854 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12855
12856 if (*args == 'f'
12857 || (*args == 'l'
3e722fb5 12858 && (g_switch_value < 4
252b5132
RH
12859 || (temp[0] == 0 && temp[1] == 0)
12860 || (temp[2] == 0 && temp[3] == 0))))
12861 {
12862 imm_expr.X_op = O_constant;
90ecf173 12863 if (!target_big_endian)
252b5132
RH
12864 imm_expr.X_add_number = bfd_getl32 (temp);
12865 else
12866 imm_expr.X_add_number = bfd_getb32 (temp);
12867 }
12868 else if (length > 4
90ecf173 12869 && !mips_disable_float_construction
ca4e0257
RS
12870 /* Constants can only be constructed in GPRs and
12871 copied to FPRs if the GPRs are at least as wide
12872 as the FPRs. Force the constant into memory if
12873 we are using 64-bit FPRs but the GPRs are only
12874 32 bits wide. */
12875 && (using_gprs
90ecf173 12876 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12877 && ((temp[0] == 0 && temp[1] == 0)
12878 || (temp[2] == 0 && temp[3] == 0))
12879 && ((temp[4] == 0 && temp[5] == 0)
12880 || (temp[6] == 0 && temp[7] == 0)))
12881 {
ca4e0257 12882 /* The value is simple enough to load with a couple of
90ecf173
MR
12883 instructions. If using 32-bit registers, set
12884 imm_expr to the high order 32 bits and offset_expr to
12885 the low order 32 bits. Otherwise, set imm_expr to
12886 the entire 64 bit constant. */
ca4e0257 12887 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12888 {
12889 imm_expr.X_op = O_constant;
12890 offset_expr.X_op = O_constant;
90ecf173 12891 if (!target_big_endian)
252b5132
RH
12892 {
12893 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12894 offset_expr.X_add_number = bfd_getl32 (temp);
12895 }
12896 else
12897 {
12898 imm_expr.X_add_number = bfd_getb32 (temp);
12899 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12900 }
12901 if (offset_expr.X_add_number == 0)
12902 offset_expr.X_op = O_absent;
12903 }
42429eac 12904 else
252b5132
RH
12905 {
12906 imm_expr.X_op = O_constant;
90ecf173 12907 if (!target_big_endian)
252b5132
RH
12908 imm_expr.X_add_number = bfd_getl64 (temp);
12909 else
12910 imm_expr.X_add_number = bfd_getb64 (temp);
12911 }
252b5132
RH
12912 }
12913 else
12914 {
12915 const char *newname;
12916 segT new_seg;
12917
12918 /* Switch to the right section. */
12919 seg = now_seg;
12920 subseg = now_subseg;
12921 switch (*args)
12922 {
12923 default: /* unused default case avoids warnings. */
12924 case 'L':
12925 newname = RDATA_SECTION_NAME;
3e722fb5 12926 if (g_switch_value >= 8)
252b5132
RH
12927 newname = ".lit8";
12928 break;
12929 case 'F':
3e722fb5 12930 newname = RDATA_SECTION_NAME;
252b5132
RH
12931 break;
12932 case 'l':
9c2799c2 12933 gas_assert (g_switch_value >= 4);
252b5132
RH
12934 newname = ".lit4";
12935 break;
12936 }
12937 new_seg = subseg_new (newname, (subsegT) 0);
f3ded42a
RS
12938 bfd_set_section_flags (stdoutput, new_seg,
12939 (SEC_ALLOC
12940 | SEC_LOAD
12941 | SEC_READONLY
12942 | SEC_DATA));
252b5132 12943 frag_align (*args == 'l' ? 2 : 3, 0, 0);
f3ded42a 12944 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12945 record_alignment (new_seg, 4);
12946 else
12947 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12948 if (seg == now_seg)
12949 as_bad (_("Can't use floating point insn in this section"));
12950
df58fc94
RS
12951 /* Set the argument to the current address in the
12952 section. */
12953 offset_expr.X_op = O_symbol;
12954 offset_expr.X_add_symbol = symbol_temp_new_now ();
12955 offset_expr.X_add_number = 0;
12956
12957 /* Put the floating point number into the section. */
12958 p = frag_more ((int) length);
12959 memcpy (p, temp, length);
12960
12961 /* Switch back to the original section. */
12962 subseg_set (seg, subseg);
12963 }
12964 }
12965 continue;
12966
12967 case 'i': /* 16-bit unsigned immediate. */
12968 case 'j': /* 16-bit signed immediate. */
12969 *imm_reloc = BFD_RELOC_LO16;
12970 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12971 {
12972 int more;
12973 offsetT minval, maxval;
12974
12975 more = (insn + 1 < past
12976 && strcmp (insn->name, insn[1].name) == 0);
12977
df58fc94
RS
12978 /* For compatibility with older assemblers, we accept
12979 0x8000-0xffff as signed 16-bit numbers when only
12980 signed numbers are allowed. */
12981 if (*args == 'i')
12982 minval = 0, maxval = 0xffff;
12983 else if (more)
12984 minval = -0x8000, maxval = 0x7fff;
12985 else
12986 minval = -0x8000, maxval = 0xffff;
12987
12988 if (imm_expr.X_op != O_constant
12989 || imm_expr.X_add_number < minval
12990 || imm_expr.X_add_number > maxval)
12991 {
12992 if (more)
12993 break;
12994 if (imm_expr.X_op == O_constant
12995 || imm_expr.X_op == O_big)
12996 as_bad (_("Expression out of range"));
12997 }
12998 }
12999 s = expr_end;
13000 continue;
13001
13002 case 'o': /* 16-bit offset. */
13003 offset_reloc[0] = BFD_RELOC_LO16;
13004 offset_reloc[1] = BFD_RELOC_UNUSED;
13005 offset_reloc[2] = BFD_RELOC_UNUSED;
13006
13007 /* Check whether there is only a single bracketed expression
13008 left. If so, it must be the base register and the
13009 constant must be zero. */
13010 if (*s == '(' && strchr (s + 1, '(') == 0)
13011 {
13012 offset_expr.X_op = O_constant;
13013 offset_expr.X_add_number = 0;
13014 continue;
13015 }
13016
13017 /* If this value won't fit into a 16 bit offset, then go
13018 find a macro that will generate the 32 bit offset
13019 code pattern. */
13020 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
13021 && (offset_expr.X_op != O_constant
13022 || offset_expr.X_add_number >= 0x8000
13023 || offset_expr.X_add_number < -0x8000))
13024 break;
13025
13026 s = expr_end;
13027 continue;
13028
13029 case 'p': /* PC-relative offset. */
13030 *offset_reloc = BFD_RELOC_16_PCREL_S2;
13031 my_getExpression (&offset_expr, s);
13032 s = expr_end;
13033 continue;
13034
13035 case 'u': /* Upper 16 bits. */
5821951c 13036 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
13037 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
13038 && imm_expr.X_op == O_constant
13039 && (imm_expr.X_add_number < 0
13040 || imm_expr.X_add_number >= 0x10000))
13041 as_bad (_("lui expression (%lu) not in range 0..65535"),
13042 (unsigned long) imm_expr.X_add_number);
13043 s = expr_end;
13044 continue;
13045
13046 case 'a': /* 26-bit address. */
13047 *offset_reloc = BFD_RELOC_MIPS_JMP;
13048 my_getExpression (&offset_expr, s);
13049 s = expr_end;
13050 continue;
13051
13052 case 'N': /* 3-bit branch condition code. */
13053 case 'M': /* 3-bit compare condition code. */
13054 rtype = RTYPE_CCC;
13055 if (ip->insn_mo->pinfo & (FP_D | FP_S))
13056 rtype |= RTYPE_FCC;
13057 if (!reg_lookup (&s, rtype, &regno))
13058 break;
13059 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
13060 || strcmp (str + strlen (str) - 5, "any2f") == 0
13061 || strcmp (str + strlen (str) - 5, "any2t") == 0)
13062 && (regno & 1) != 0)
13063 as_warn (_("Condition code register should be even for %s, "
13064 "was %d"),
13065 str, regno);
13066 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
13067 || strcmp (str + strlen (str) - 5, "any4t") == 0)
13068 && (regno & 3) != 0)
13069 as_warn (_("Condition code register should be 0 or 4 for %s, "
13070 "was %d"),
13071 str, regno);
13072 if (*args == 'N')
13073 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
13074 else
13075 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
13076 continue;
13077
13078 case 'H':
13079 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
13080 s += 2;
13081 if (ISDIGIT (*s))
13082 {
13083 c = 0;
13084 do
13085 {
13086 c *= 10;
13087 c += *s - '0';
13088 ++s;
13089 }
13090 while (ISDIGIT (*s));
13091 }
13092 else
13093 c = 8; /* Invalid sel value. */
13094
13095 if (c > 7)
13096 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
13097 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
13098 continue;
13099
13100 case 'e':
13101 gas_assert (!mips_opts.micromips);
13102 /* Must be at least one digit. */
13103 my_getExpression (&imm_expr, s);
13104 check_absolute_expr (ip, &imm_expr);
13105
13106 if ((unsigned long) imm_expr.X_add_number
13107 > (unsigned long) OP_MASK_VECBYTE)
13108 {
13109 as_bad (_("bad byte vector index (%ld)"),
13110 (long) imm_expr.X_add_number);
13111 imm_expr.X_add_number = 0;
13112 }
13113
13114 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
13115 imm_expr.X_op = O_absent;
13116 s = expr_end;
13117 continue;
13118
13119 case '%':
13120 gas_assert (!mips_opts.micromips);
13121 my_getExpression (&imm_expr, s);
13122 check_absolute_expr (ip, &imm_expr);
13123
13124 if ((unsigned long) imm_expr.X_add_number
13125 > (unsigned long) OP_MASK_VECALIGN)
13126 {
13127 as_bad (_("bad byte vector index (%ld)"),
13128 (long) imm_expr.X_add_number);
13129 imm_expr.X_add_number = 0;
13130 }
13131
13132 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
13133 imm_expr.X_op = O_absent;
13134 s = expr_end;
13135 continue;
13136
13137 case 'm': /* Opcode extension character. */
13138 gas_assert (mips_opts.micromips);
13139 c = *++args;
13140 switch (c)
13141 {
13142 case 'r':
13143 if (strncmp (s, "$pc", 3) == 0)
13144 {
13145 s += 3;
13146 continue;
13147 }
13148 break;
13149
13150 case 'a':
13151 case 'b':
13152 case 'c':
13153 case 'd':
13154 case 'e':
13155 case 'f':
13156 case 'g':
13157 case 'h':
13158 case 'i':
13159 case 'j':
13160 case 'l':
13161 case 'm':
13162 case 'n':
13163 case 'p':
13164 case 'q':
13165 case 's':
13166 case 't':
13167 case 'x':
13168 case 'y':
13169 case 'z':
13170 s_reset = s;
13171 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
13172 if (regno == AT && mips_opts.at)
13173 {
13174 if (mips_opts.at == ATREG)
13175 as_warn (_("Used $at without \".set noat\""));
13176 else
13177 as_warn (_("Used $%u with \".set at=$%u\""),
13178 regno, mips_opts.at);
13179 }
13180 if (!ok)
13181 {
13182 if (c == 'c')
13183 {
13184 gas_assert (args[1] == ',');
13185 regno = lastregno;
13186 ++args;
13187 }
13188 else if (c == 't')
13189 {
13190 gas_assert (args[1] == ',');
13191 ++args;
13192 continue; /* Nothing to do. */
13193 }
13194 else
13195 break;
13196 }
13197
13198 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
13199 {
13200 if (regno == lastregno)
13201 {
13202 insn_error
13203 = _("Source and destination must be different");
13204 continue;
13205 }
13206 if (regno == 31 && lastregno == 0xffffffff)
13207 {
13208 insn_error
13209 = _("A destination register must be supplied");
13210 continue;
13211 }
13212 }
13213
13214 if (*s == ' ')
13215 ++s;
13216 if (args[1] != *s)
13217 {
13218 if (c == 'e')
13219 {
13220 gas_assert (args[1] == ',');
13221 regno = lastregno;
13222 s = s_reset;
13223 ++args;
13224 }
13225 else if (c == 't')
13226 {
13227 gas_assert (args[1] == ',');
13228 s = s_reset;
13229 ++args;
13230 continue; /* Nothing to do. */
13231 }
13232 }
13233
13234 /* Make sure regno is the same as lastregno. */
13235 if (c == 't' && regno != lastregno)
13236 break;
13237
13238 /* Make sure regno is the same as destregno. */
13239 if (c == 'x' && regno != destregno)
13240 break;
13241
13242 /* We need to save regno, before regno maps to the
13243 microMIPS register encoding. */
13244 lastregno = regno;
13245
13246 if (c == 'f')
13247 destregno = regno;
13248
13249 switch (c)
13250 {
13251 case 'a':
13252 if (regno != GP)
13253 regno = ILLEGAL_REG;
13254 break;
13255
13256 case 'b':
13257 regno = mips32_to_micromips_reg_b_map[regno];
13258 break;
13259
13260 case 'c':
13261 regno = mips32_to_micromips_reg_c_map[regno];
13262 break;
13263
13264 case 'd':
13265 regno = mips32_to_micromips_reg_d_map[regno];
13266 break;
13267
13268 case 'e':
13269 regno = mips32_to_micromips_reg_e_map[regno];
13270 break;
13271
13272 case 'f':
13273 regno = mips32_to_micromips_reg_f_map[regno];
13274 break;
13275
13276 case 'g':
13277 regno = mips32_to_micromips_reg_g_map[regno];
13278 break;
13279
13280 case 'h':
13281 regno = mips32_to_micromips_reg_h_map[regno];
13282 break;
13283
13284 case 'i':
13285 switch (EXTRACT_OPERAND (1, MI, *ip))
13286 {
13287 case 4:
13288 if (regno == 21)
13289 regno = 3;
13290 else if (regno == 22)
13291 regno = 4;
13292 else if (regno == 5)
13293 regno = 5;
13294 else if (regno == 6)
13295 regno = 6;
13296 else if (regno == 7)
13297 regno = 7;
13298 else
13299 regno = ILLEGAL_REG;
13300 break;
13301
13302 case 5:
13303 if (regno == 6)
13304 regno = 0;
13305 else if (regno == 7)
13306 regno = 1;
13307 else
13308 regno = ILLEGAL_REG;
13309 break;
13310
13311 case 6:
13312 if (regno == 7)
13313 regno = 2;
13314 else
13315 regno = ILLEGAL_REG;
13316 break;
13317
13318 default:
13319 regno = ILLEGAL_REG;
13320 break;
13321 }
13322 break;
13323
13324 case 'l':
13325 regno = mips32_to_micromips_reg_l_map[regno];
13326 break;
13327
13328 case 'm':
13329 regno = mips32_to_micromips_reg_m_map[regno];
13330 break;
13331
13332 case 'n':
13333 regno = mips32_to_micromips_reg_n_map[regno];
13334 break;
13335
13336 case 'q':
13337 regno = mips32_to_micromips_reg_q_map[regno];
13338 break;
13339
13340 case 's':
13341 if (regno != SP)
13342 regno = ILLEGAL_REG;
13343 break;
13344
13345 case 'y':
13346 if (regno != 31)
13347 regno = ILLEGAL_REG;
13348 break;
13349
13350 case 'z':
13351 if (regno != ZERO)
13352 regno = ILLEGAL_REG;
13353 break;
13354
13355 case 'j': /* Do nothing. */
13356 case 'p':
13357 case 't':
13358 case 'x':
13359 break;
13360
13361 default:
b37df7c4 13362 abort ();
df58fc94
RS
13363 }
13364
13365 if (regno == ILLEGAL_REG)
13366 break;
13367
13368 switch (c)
13369 {
13370 case 'b':
13371 INSERT_OPERAND (1, MB, *ip, regno);
13372 break;
13373
13374 case 'c':
13375 INSERT_OPERAND (1, MC, *ip, regno);
13376 break;
13377
13378 case 'd':
13379 INSERT_OPERAND (1, MD, *ip, regno);
13380 break;
13381
13382 case 'e':
13383 INSERT_OPERAND (1, ME, *ip, regno);
13384 break;
13385
13386 case 'f':
13387 INSERT_OPERAND (1, MF, *ip, regno);
13388 break;
13389
13390 case 'g':
13391 INSERT_OPERAND (1, MG, *ip, regno);
13392 break;
13393
13394 case 'h':
13395 INSERT_OPERAND (1, MH, *ip, regno);
13396 break;
13397
13398 case 'i':
13399 INSERT_OPERAND (1, MI, *ip, regno);
13400 break;
13401
13402 case 'j':
13403 INSERT_OPERAND (1, MJ, *ip, regno);
13404 break;
13405
13406 case 'l':
13407 INSERT_OPERAND (1, ML, *ip, regno);
13408 break;
13409
13410 case 'm':
13411 INSERT_OPERAND (1, MM, *ip, regno);
13412 break;
13413
13414 case 'n':
13415 INSERT_OPERAND (1, MN, *ip, regno);
13416 break;
13417
13418 case 'p':
13419 INSERT_OPERAND (1, MP, *ip, regno);
13420 break;
13421
13422 case 'q':
13423 INSERT_OPERAND (1, MQ, *ip, regno);
13424 break;
13425
13426 case 'a': /* Do nothing. */
13427 case 's': /* Do nothing. */
13428 case 't': /* Do nothing. */
13429 case 'x': /* Do nothing. */
13430 case 'y': /* Do nothing. */
13431 case 'z': /* Do nothing. */
13432 break;
13433
13434 default:
b37df7c4 13435 abort ();
df58fc94
RS
13436 }
13437 continue;
13438
13439 case 'A':
13440 {
13441 bfd_reloc_code_real_type r[3];
13442 expressionS ep;
13443 int imm;
13444
13445 /* Check whether there is only a single bracketed
13446 expression left. If so, it must be the base register
13447 and the constant must be zero. */
13448 if (*s == '(' && strchr (s + 1, '(') == 0)
13449 {
13450 INSERT_OPERAND (1, IMMA, *ip, 0);
13451 continue;
13452 }
13453
13454 if (my_getSmallExpression (&ep, r, s) > 0
13455 || !expr_const_in_range (&ep, -64, 64, 2))
13456 break;
13457
13458 imm = ep.X_add_number >> 2;
13459 INSERT_OPERAND (1, IMMA, *ip, imm);
13460 }
13461 s = expr_end;
13462 continue;
13463
13464 case 'B':
13465 {
13466 bfd_reloc_code_real_type r[3];
13467 expressionS ep;
13468 int imm;
13469
13470 if (my_getSmallExpression (&ep, r, s) > 0
13471 || ep.X_op != O_constant)
13472 break;
13473
13474 for (imm = 0; imm < 8; imm++)
13475 if (micromips_imm_b_map[imm] == ep.X_add_number)
13476 break;
13477 if (imm >= 8)
13478 break;
13479
13480 INSERT_OPERAND (1, IMMB, *ip, imm);
13481 }
13482 s = expr_end;
13483 continue;
13484
13485 case 'C':
13486 {
13487 bfd_reloc_code_real_type r[3];
13488 expressionS ep;
13489 int imm;
13490
13491 if (my_getSmallExpression (&ep, r, s) > 0
13492 || ep.X_op != O_constant)
13493 break;
13494
13495 for (imm = 0; imm < 16; imm++)
13496 if (micromips_imm_c_map[imm] == ep.X_add_number)
13497 break;
13498 if (imm >= 16)
13499 break;
13500
13501 INSERT_OPERAND (1, IMMC, *ip, imm);
13502 }
13503 s = expr_end;
13504 continue;
13505
13506 case 'D': /* pc relative offset */
13507 case 'E': /* pc relative offset */
13508 my_getExpression (&offset_expr, s);
13509 if (offset_expr.X_op == O_register)
13510 break;
13511
40209cad
MR
13512 if (!forced_insn_length)
13513 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13514 else if (c == 'D')
13515 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13516 else
13517 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
13518 s = expr_end;
13519 continue;
13520
13521 case 'F':
13522 {
13523 bfd_reloc_code_real_type r[3];
13524 expressionS ep;
13525 int imm;
13526
13527 if (my_getSmallExpression (&ep, r, s) > 0
13528 || !expr_const_in_range (&ep, 0, 16, 0))
13529 break;
13530
13531 imm = ep.X_add_number;
13532 INSERT_OPERAND (1, IMMF, *ip, imm);
13533 }
13534 s = expr_end;
13535 continue;
13536
13537 case 'G':
13538 {
13539 bfd_reloc_code_real_type r[3];
13540 expressionS ep;
13541 int imm;
13542
13543 /* Check whether there is only a single bracketed
13544 expression left. If so, it must be the base register
13545 and the constant must be zero. */
13546 if (*s == '(' && strchr (s + 1, '(') == 0)
13547 {
13548 INSERT_OPERAND (1, IMMG, *ip, 0);
13549 continue;
13550 }
13551
13552 if (my_getSmallExpression (&ep, r, s) > 0
13553 || !expr_const_in_range (&ep, -1, 15, 0))
13554 break;
13555
13556 imm = ep.X_add_number & 15;
13557 INSERT_OPERAND (1, IMMG, *ip, imm);
13558 }
13559 s = expr_end;
13560 continue;
13561
13562 case 'H':
13563 {
13564 bfd_reloc_code_real_type r[3];
13565 expressionS ep;
13566 int imm;
13567
13568 /* Check whether there is only a single bracketed
13569 expression left. If so, it must be the base register
13570 and the constant must be zero. */
13571 if (*s == '(' && strchr (s + 1, '(') == 0)
13572 {
13573 INSERT_OPERAND (1, IMMH, *ip, 0);
13574 continue;
13575 }
13576
13577 if (my_getSmallExpression (&ep, r, s) > 0
13578 || !expr_const_in_range (&ep, 0, 16, 1))
13579 break;
13580
13581 imm = ep.X_add_number >> 1;
13582 INSERT_OPERAND (1, IMMH, *ip, imm);
13583 }
13584 s = expr_end;
13585 continue;
13586
13587 case 'I':
13588 {
13589 bfd_reloc_code_real_type r[3];
13590 expressionS ep;
13591 int imm;
13592
13593 if (my_getSmallExpression (&ep, r, s) > 0
13594 || !expr_const_in_range (&ep, -1, 127, 0))
13595 break;
13596
13597 imm = ep.X_add_number & 127;
13598 INSERT_OPERAND (1, IMMI, *ip, imm);
13599 }
13600 s = expr_end;
13601 continue;
13602
13603 case 'J':
13604 {
13605 bfd_reloc_code_real_type r[3];
13606 expressionS ep;
13607 int imm;
13608
13609 /* Check whether there is only a single bracketed
13610 expression left. If so, it must be the base register
13611 and the constant must be zero. */
13612 if (*s == '(' && strchr (s + 1, '(') == 0)
13613 {
13614 INSERT_OPERAND (1, IMMJ, *ip, 0);
13615 continue;
13616 }
13617
13618 if (my_getSmallExpression (&ep, r, s) > 0
13619 || !expr_const_in_range (&ep, 0, 16, 2))
13620 break;
13621
13622 imm = ep.X_add_number >> 2;
13623 INSERT_OPERAND (1, IMMJ, *ip, imm);
13624 }
13625 s = expr_end;
13626 continue;
13627
13628 case 'L':
13629 {
13630 bfd_reloc_code_real_type r[3];
13631 expressionS ep;
13632 int imm;
13633
13634 /* Check whether there is only a single bracketed
13635 expression left. If so, it must be the base register
13636 and the constant must be zero. */
13637 if (*s == '(' && strchr (s + 1, '(') == 0)
13638 {
13639 INSERT_OPERAND (1, IMML, *ip, 0);
13640 continue;
13641 }
13642
13643 if (my_getSmallExpression (&ep, r, s) > 0
13644 || !expr_const_in_range (&ep, 0, 16, 0))
13645 break;
13646
13647 imm = ep.X_add_number;
13648 INSERT_OPERAND (1, IMML, *ip, imm);
13649 }
13650 s = expr_end;
13651 continue;
13652
13653 case 'M':
13654 {
13655 bfd_reloc_code_real_type r[3];
13656 expressionS ep;
13657 int imm;
13658
13659 if (my_getSmallExpression (&ep, r, s) > 0
13660 || !expr_const_in_range (&ep, 1, 9, 0))
13661 break;
13662
13663 imm = ep.X_add_number & 7;
13664 INSERT_OPERAND (1, IMMM, *ip, imm);
13665 }
13666 s = expr_end;
13667 continue;
13668
13669 case 'N': /* Register list for lwm and swm. */
13670 {
13671 /* A comma-separated list of registers and/or
13672 dash-separated contiguous ranges including
13673 both ra and a set of one or more registers
13674 starting at s0 up to s3 which have to be
13675 consecutive, e.g.:
13676
13677 s0, ra
13678 s0, s1, ra, s2, s3
13679 s0-s2, ra
13680
13681 and any permutations of these. */
13682 unsigned int reglist;
13683 int imm;
13684
13685 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13686 break;
13687
13688 if ((reglist & 0xfff1ffff) != 0x80010000)
13689 break;
13690
13691 reglist = (reglist >> 17) & 7;
13692 reglist += 1;
13693 if ((reglist & -reglist) != reglist)
13694 break;
252b5132 13695
df58fc94
RS
13696 imm = ffs (reglist) - 1;
13697 INSERT_OPERAND (1, IMMN, *ip, imm);
13698 }
13699 continue;
252b5132 13700
df58fc94
RS
13701 case 'O': /* sdbbp 4-bit code. */
13702 {
13703 bfd_reloc_code_real_type r[3];
13704 expressionS ep;
13705 int imm;
13706
13707 if (my_getSmallExpression (&ep, r, s) > 0
13708 || !expr_const_in_range (&ep, 0, 16, 0))
13709 break;
13710
13711 imm = ep.X_add_number;
13712 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13713 }
df58fc94
RS
13714 s = expr_end;
13715 continue;
252b5132 13716
df58fc94
RS
13717 case 'P':
13718 {
13719 bfd_reloc_code_real_type r[3];
13720 expressionS ep;
13721 int imm;
5e0116d5 13722
df58fc94
RS
13723 if (my_getSmallExpression (&ep, r, s) > 0
13724 || !expr_const_in_range (&ep, 0, 32, 2))
13725 break;
5e0116d5 13726
df58fc94
RS
13727 imm = ep.X_add_number >> 2;
13728 INSERT_OPERAND (1, IMMP, *ip, imm);
13729 }
13730 s = expr_end;
13731 continue;
5e0116d5 13732
df58fc94
RS
13733 case 'Q':
13734 {
13735 bfd_reloc_code_real_type r[3];
13736 expressionS ep;
13737 int imm;
5e0116d5 13738
df58fc94
RS
13739 if (my_getSmallExpression (&ep, r, s) > 0
13740 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13741 break;
252b5132 13742
df58fc94
RS
13743 imm = ep.X_add_number >> 2;
13744 INSERT_OPERAND (1, IMMQ, *ip, imm);
13745 }
13746 s = expr_end;
13747 continue;
4614d845 13748
df58fc94
RS
13749 case 'U':
13750 {
13751 bfd_reloc_code_real_type r[3];
13752 expressionS ep;
13753 int imm;
13754
13755 /* Check whether there is only a single bracketed
13756 expression left. If so, it must be the base register
13757 and the constant must be zero. */
13758 if (*s == '(' && strchr (s + 1, '(') == 0)
13759 {
13760 INSERT_OPERAND (1, IMMU, *ip, 0);
13761 continue;
13762 }
13763
13764 if (my_getSmallExpression (&ep, r, s) > 0
13765 || !expr_const_in_range (&ep, 0, 32, 2))
13766 break;
13767
13768 imm = ep.X_add_number >> 2;
13769 INSERT_OPERAND (1, IMMU, *ip, imm);
13770 }
13771 s = expr_end;
5e0116d5 13772 continue;
252b5132 13773
df58fc94
RS
13774 case 'W':
13775 {
13776 bfd_reloc_code_real_type r[3];
13777 expressionS ep;
13778 int imm;
252b5132 13779
df58fc94
RS
13780 if (my_getSmallExpression (&ep, r, s) > 0
13781 || !expr_const_in_range (&ep, 0, 64, 2))
13782 break;
252b5132 13783
df58fc94
RS
13784 imm = ep.X_add_number >> 2;
13785 INSERT_OPERAND (1, IMMW, *ip, imm);
13786 }
13787 s = expr_end;
13788 continue;
252b5132 13789
df58fc94
RS
13790 case 'X':
13791 {
13792 bfd_reloc_code_real_type r[3];
13793 expressionS ep;
13794 int imm;
252b5132 13795
df58fc94
RS
13796 if (my_getSmallExpression (&ep, r, s) > 0
13797 || !expr_const_in_range (&ep, -8, 8, 0))
13798 break;
252b5132 13799
df58fc94
RS
13800 imm = ep.X_add_number;
13801 INSERT_OPERAND (1, IMMX, *ip, imm);
13802 }
13803 s = expr_end;
13804 continue;
252b5132 13805
df58fc94
RS
13806 case 'Y':
13807 {
13808 bfd_reloc_code_real_type r[3];
13809 expressionS ep;
13810 int imm;
156c2f8b 13811
df58fc94
RS
13812 if (my_getSmallExpression (&ep, r, s) > 0
13813 || expr_const_in_range (&ep, -2, 2, 2)
13814 || !expr_const_in_range (&ep, -258, 258, 2))
13815 break;
156c2f8b 13816
df58fc94
RS
13817 imm = ep.X_add_number >> 2;
13818 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13819 INSERT_OPERAND (1, IMMY, *ip, imm);
13820 }
13821 s = expr_end;
13822 continue;
60b63b72 13823
df58fc94
RS
13824 case 'Z':
13825 {
13826 bfd_reloc_code_real_type r[3];
13827 expressionS ep;
13828
13829 if (my_getSmallExpression (&ep, r, s) > 0
13830 || !expr_const_in_range (&ep, 0, 1, 0))
13831 break;
13832 }
13833 s = expr_end;
13834 continue;
13835
13836 default:
13837 as_bad (_("Internal error: bad microMIPS opcode "
13838 "(unknown extension operand type `m%c'): %s %s"),
13839 *args, insn->name, insn->args);
13840 /* Further processing is fruitless. */
13841 return;
60b63b72 13842 }
df58fc94 13843 break;
60b63b72 13844
df58fc94
RS
13845 case 'n': /* Register list for 32-bit lwm and swm. */
13846 gas_assert (mips_opts.micromips);
13847 {
13848 /* A comma-separated list of registers and/or
13849 dash-separated contiguous ranges including
13850 at least one of ra and a set of one or more
13851 registers starting at s0 up to s7 and then
13852 s8 which have to be consecutive, e.g.:
13853
13854 ra
13855 s0
13856 ra, s0, s1, s2
13857 s0-s8
13858 s0-s5, ra
13859
13860 and any permutations of these. */
13861 unsigned int reglist;
13862 int imm;
13863 int ra;
13864
13865 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13866 break;
13867
13868 if ((reglist & 0x3f00ffff) != 0)
13869 break;
13870
13871 ra = (reglist >> 27) & 0x10;
13872 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13873 reglist += 1;
13874 if ((reglist & -reglist) != reglist)
13875 break;
13876
13877 imm = (ffs (reglist) - 1) | ra;
13878 INSERT_OPERAND (1, RT, *ip, imm);
13879 imm_expr.X_op = O_absent;
13880 }
60b63b72
RS
13881 continue;
13882
df58fc94
RS
13883 case '|': /* 4-bit trap code. */
13884 gas_assert (mips_opts.micromips);
60b63b72
RS
13885 my_getExpression (&imm_expr, s);
13886 check_absolute_expr (ip, &imm_expr);
60b63b72 13887 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13888 > MICROMIPSOP_MASK_TRAP)
13889 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13890 (unsigned long) imm_expr.X_add_number,
13891 ip->insn_mo->name);
13892 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13893 imm_expr.X_op = O_absent;
13894 s = expr_end;
13895 continue;
13896
252b5132 13897 default:
f71d0d44 13898 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13899 abort ();
252b5132
RH
13900 }
13901 break;
13902 }
13903 /* Args don't match. */
df58fc94
RS
13904 s = argsStart;
13905 insn_error = _("Illegal operands");
13906 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13907 {
13908 ++insn;
252b5132
RH
13909 continue;
13910 }
df58fc94
RS
13911 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13912 {
13913 gas_assert (firstinsn);
13914 need_delay_slot_ok = FALSE;
13915 past = insn + 1;
13916 insn = firstinsn;
13917 continue;
13918 }
252b5132
RH
13919 return;
13920 }
13921}
13922
0499d65b
TS
13923#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13924
252b5132
RH
13925/* This routine assembles an instruction into its binary format when
13926 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13927 global variables imm_reloc or offset_reloc to the type of relocation
13928 to do if one of the operands is an address expression. It also sets
13929 forced_insn_length to the resulting instruction size in bytes if the
13930 user explicitly requested a small or extended instruction. */
252b5132
RH
13931
13932static void
17a2f251 13933mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13934{
13935 char *s;
13936 const char *args;
13937 struct mips_opcode *insn;
13938 char *argsstart;
13939 unsigned int regno;
13940 unsigned int lastregno = 0;
13941 char *s_reset;
d6f16593 13942 size_t i;
252b5132
RH
13943
13944 insn_error = NULL;
13945
df58fc94 13946 forced_insn_length = 0;
252b5132 13947
3882b010 13948 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13949 ;
13950 switch (*s)
13951 {
13952 case '\0':
13953 break;
13954
13955 case ' ':
13956 *s++ = '\0';
13957 break;
13958
13959 case '.':
13960 if (s[1] == 't' && s[2] == ' ')
13961 {
13962 *s = '\0';
df58fc94 13963 forced_insn_length = 2;
252b5132
RH
13964 s += 3;
13965 break;
13966 }
13967 else if (s[1] == 'e' && s[2] == ' ')
13968 {
13969 *s = '\0';
df58fc94 13970 forced_insn_length = 4;
252b5132
RH
13971 s += 3;
13972 break;
13973 }
13974 /* Fall through. */
13975 default:
13976 insn_error = _("unknown opcode");
13977 return;
13978 }
13979
df58fc94
RS
13980 if (mips_opts.noautoextend && !forced_insn_length)
13981 forced_insn_length = 2;
252b5132
RH
13982
13983 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13984 {
13985 insn_error = _("unrecognized opcode");
13986 return;
13987 }
13988
13989 argsstart = s;
13990 for (;;)
13991 {
9b3f89ee
TS
13992 bfd_boolean ok;
13993
9c2799c2 13994 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13995
037b32b9 13996 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13997 if (! ok)
13998 {
13999 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
14000 && strcmp (insn->name, insn[1].name) == 0)
14001 {
14002 ++insn;
14003 continue;
14004 }
14005 else
14006 {
14007 if (!insn_error)
14008 {
14009 static char buf[100];
14010 sprintf (buf,
7bd942df 14011 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
14012 mips_cpu_info_from_arch (mips_opts.arch)->name,
14013 mips_cpu_info_from_isa (mips_opts.isa)->name);
14014 insn_error = buf;
14015 }
14016 return;
14017 }
14018 }
14019
1e915849 14020 create_insn (ip, insn);
252b5132 14021 imm_expr.X_op = O_absent;
f6688943
TS
14022 imm_reloc[0] = BFD_RELOC_UNUSED;
14023 imm_reloc[1] = BFD_RELOC_UNUSED;
14024 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 14025 imm2_expr.X_op = O_absent;
252b5132 14026 offset_expr.X_op = O_absent;
f6688943
TS
14027 offset_reloc[0] = BFD_RELOC_UNUSED;
14028 offset_reloc[1] = BFD_RELOC_UNUSED;
14029 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
14030 for (args = insn->args; 1; ++args)
14031 {
14032 int c;
14033
14034 if (*s == ' ')
14035 ++s;
14036
14037 /* In this switch statement we call break if we did not find
14038 a match, continue if we did find a match, or return if we
14039 are done. */
14040
14041 c = *args;
14042 switch (c)
14043 {
14044 case '\0':
14045 if (*s == '\0')
14046 {
b886a2ab
RS
14047 offsetT value;
14048
252b5132
RH
14049 /* Stuff the immediate value in now, if we can. */
14050 if (imm_expr.X_op == O_constant
f6688943 14051 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
14052 && insn->pinfo != INSN_MACRO
14053 && calculate_reloc (*offset_reloc,
14054 imm_expr.X_add_number, &value))
252b5132 14055 {
c4e7957c 14056 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 14057 *offset_reloc, value, forced_insn_length,
43c0598f 14058 &ip->insn_opcode);
252b5132 14059 imm_expr.X_op = O_absent;
f6688943 14060 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 14061 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
14062 }
14063
14064 return;
14065 }
14066 break;
14067
14068 case ',':
14069 if (*s++ == c)
14070 continue;
14071 s--;
14072 switch (*++args)
14073 {
14074 case 'v':
bf12938e 14075 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
14076 continue;
14077 case 'w':
bf12938e 14078 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
14079 continue;
14080 }
14081 break;
14082
14083 case '(':
14084 case ')':
14085 if (*s++ == c)
14086 continue;
14087 break;
14088
14089 case 'v':
14090 case 'w':
14091 if (s[0] != '$')
14092 {
14093 if (c == 'v')
bf12938e 14094 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 14095 else
bf12938e 14096 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
14097 ++args;
14098 continue;
14099 }
14100 /* Fall through. */
14101 case 'x':
14102 case 'y':
14103 case 'z':
14104 case 'Z':
14105 case '0':
14106 case 'S':
14107 case 'R':
14108 case 'X':
14109 case 'Y':
707bfff6
TS
14110 s_reset = s;
14111 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 14112 {
707bfff6 14113 if (c == 'v' || c == 'w')
85b51719 14114 {
707bfff6 14115 if (c == 'v')
a9e24354 14116 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 14117 else
a9e24354 14118 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
14119 ++args;
14120 continue;
85b51719 14121 }
707bfff6 14122 break;
252b5132
RH
14123 }
14124
14125 if (*s == ' ')
14126 ++s;
14127 if (args[1] != *s)
14128 {
14129 if (c == 'v' || c == 'w')
14130 {
14131 regno = mips16_to_32_reg_map[lastregno];
14132 s = s_reset;
f9419b05 14133 ++args;
252b5132
RH
14134 }
14135 }
14136
14137 switch (c)
14138 {
14139 case 'x':
14140 case 'y':
14141 case 'z':
14142 case 'v':
14143 case 'w':
14144 case 'Z':
14145 regno = mips32_to_16_reg_map[regno];
14146 break;
14147
14148 case '0':
14149 if (regno != 0)
14150 regno = ILLEGAL_REG;
14151 break;
14152
14153 case 'S':
14154 if (regno != SP)
14155 regno = ILLEGAL_REG;
14156 break;
14157
14158 case 'R':
14159 if (regno != RA)
14160 regno = ILLEGAL_REG;
14161 break;
14162
14163 case 'X':
14164 case 'Y':
741fe287
MR
14165 if (regno == AT && mips_opts.at)
14166 {
14167 if (mips_opts.at == ATREG)
14168 as_warn (_("used $at without \".set noat\""));
14169 else
14170 as_warn (_("used $%u with \".set at=$%u\""),
14171 regno, mips_opts.at);
14172 }
252b5132
RH
14173 break;
14174
14175 default:
b37df7c4 14176 abort ();
252b5132
RH
14177 }
14178
14179 if (regno == ILLEGAL_REG)
14180 break;
14181
14182 switch (c)
14183 {
14184 case 'x':
14185 case 'v':
bf12938e 14186 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
14187 break;
14188 case 'y':
14189 case 'w':
bf12938e 14190 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
14191 break;
14192 case 'z':
bf12938e 14193 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
14194 break;
14195 case 'Z':
bf12938e 14196 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
14197 case '0':
14198 case 'S':
14199 case 'R':
14200 break;
14201 case 'X':
bf12938e 14202 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
14203 break;
14204 case 'Y':
14205 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 14206 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
14207 break;
14208 default:
b37df7c4 14209 abort ();
252b5132
RH
14210 }
14211
14212 lastregno = regno;
14213 continue;
14214
14215 case 'P':
14216 if (strncmp (s, "$pc", 3) == 0)
14217 {
14218 s += 3;
14219 continue;
14220 }
14221 break;
14222
252b5132
RH
14223 case '5':
14224 case 'H':
14225 case 'W':
14226 case 'D':
14227 case 'j':
252b5132
RH
14228 case 'V':
14229 case 'C':
14230 case 'U':
14231 case 'k':
14232 case 'K':
d6f16593
MR
14233 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
14234 if (i > 0)
252b5132 14235 {
d6f16593 14236 if (imm_expr.X_op != O_constant)
252b5132 14237 {
df58fc94 14238 forced_insn_length = 4;
5c04167a 14239 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 14240 }
d6f16593
MR
14241 else
14242 {
14243 /* We need to relax this instruction. */
14244 *offset_reloc = *imm_reloc;
14245 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14246 }
14247 s = expr_end;
14248 continue;
252b5132 14249 }
d6f16593
MR
14250 *imm_reloc = BFD_RELOC_UNUSED;
14251 /* Fall through. */
14252 case '<':
14253 case '>':
14254 case '[':
14255 case ']':
14256 case '4':
14257 case '8':
14258 my_getExpression (&imm_expr, s);
252b5132
RH
14259 if (imm_expr.X_op == O_register)
14260 {
14261 /* What we thought was an expression turned out to
14262 be a register. */
14263
14264 if (s[0] == '(' && args[1] == '(')
14265 {
14266 /* It looks like the expression was omitted
14267 before a register indirection, which means
14268 that the expression is implicitly zero. We
14269 still set up imm_expr, so that we handle
14270 explicit extensions correctly. */
14271 imm_expr.X_op = O_constant;
14272 imm_expr.X_add_number = 0;
f6688943 14273 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14274 continue;
14275 }
14276
14277 break;
14278 }
14279
14280 /* We need to relax this instruction. */
f6688943 14281 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14282 s = expr_end;
14283 continue;
14284
14285 case 'p':
14286 case 'q':
14287 case 'A':
14288 case 'B':
14289 case 'E':
14290 /* We use offset_reloc rather than imm_reloc for the PC
14291 relative operands. This lets macros with both
14292 immediate and address operands work correctly. */
14293 my_getExpression (&offset_expr, s);
14294
14295 if (offset_expr.X_op == O_register)
14296 break;
14297
14298 /* We need to relax this instruction. */
f6688943 14299 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14300 s = expr_end;
14301 continue;
14302
14303 case '6': /* break code */
14304 my_getExpression (&imm_expr, s);
14305 check_absolute_expr (ip, &imm_expr);
14306 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
14307 as_warn (_("Invalid value for `%s' (%lu)"),
14308 ip->insn_mo->name,
14309 (unsigned long) imm_expr.X_add_number);
14310 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
14311 imm_expr.X_op = O_absent;
14312 s = expr_end;
14313 continue;
14314
14315 case 'a': /* 26 bit address */
14316 my_getExpression (&offset_expr, s);
14317 s = expr_end;
f6688943 14318 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
14319 ip->insn_opcode <<= 16;
14320 continue;
14321
14322 case 'l': /* register list for entry macro */
14323 case 'L': /* register list for exit macro */
14324 {
14325 int mask;
14326
14327 if (c == 'l')
14328 mask = 0;
14329 else
14330 mask = 7 << 3;
14331 while (*s != '\0')
14332 {
707bfff6 14333 unsigned int freg, reg1, reg2;
252b5132
RH
14334
14335 while (*s == ' ' || *s == ',')
14336 ++s;
707bfff6 14337 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 14338 freg = 0;
707bfff6
TS
14339 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14340 freg = 1;
252b5132
RH
14341 else
14342 {
707bfff6
TS
14343 as_bad (_("can't parse register list"));
14344 break;
252b5132
RH
14345 }
14346 if (*s == ' ')
14347 ++s;
14348 if (*s != '-')
14349 reg2 = reg1;
14350 else
14351 {
14352 ++s;
707bfff6
TS
14353 if (!reg_lookup (&s, freg ? RTYPE_FPU
14354 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 14355 {
707bfff6
TS
14356 as_bad (_("invalid register list"));
14357 break;
252b5132
RH
14358 }
14359 }
14360 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14361 {
14362 mask &= ~ (7 << 3);
14363 mask |= 5 << 3;
14364 }
14365 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14366 {
14367 mask &= ~ (7 << 3);
14368 mask |= 6 << 3;
14369 }
14370 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14371 mask |= (reg2 - 3) << 3;
14372 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14373 mask |= (reg2 - 15) << 1;
f9419b05 14374 else if (reg1 == RA && reg2 == RA)
252b5132
RH
14375 mask |= 1;
14376 else
14377 {
14378 as_bad (_("invalid register list"));
14379 break;
14380 }
14381 }
14382 /* The mask is filled in in the opcode table for the
14383 benefit of the disassembler. We remove it before
14384 applying the actual mask. */
14385 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14386 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14387 }
14388 continue;
14389
0499d65b
TS
14390 case 'm': /* Register list for save insn. */
14391 case 'M': /* Register list for restore insn. */
14392 {
5c04167a 14393 int opcode = ip->insn_opcode;
0499d65b 14394 int framesz = 0, seen_framesz = 0;
91d6fa6a 14395 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
14396
14397 while (*s != '\0')
14398 {
14399 unsigned int reg1, reg2;
14400
14401 SKIP_SPACE_TABS (s);
14402 while (*s == ',')
14403 ++s;
14404 SKIP_SPACE_TABS (s);
14405
14406 my_getExpression (&imm_expr, s);
14407 if (imm_expr.X_op == O_constant)
14408 {
14409 /* Handle the frame size. */
14410 if (seen_framesz)
14411 {
14412 as_bad (_("more than one frame size in list"));
14413 break;
14414 }
14415 seen_framesz = 1;
14416 framesz = imm_expr.X_add_number;
14417 imm_expr.X_op = O_absent;
14418 s = expr_end;
14419 continue;
14420 }
14421
707bfff6 14422 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
14423 {
14424 as_bad (_("can't parse register list"));
14425 break;
14426 }
0499d65b 14427
707bfff6
TS
14428 while (*s == ' ')
14429 ++s;
14430
0499d65b
TS
14431 if (*s != '-')
14432 reg2 = reg1;
14433 else
14434 {
14435 ++s;
707bfff6
TS
14436 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14437 || reg2 < reg1)
0499d65b
TS
14438 {
14439 as_bad (_("can't parse register list"));
14440 break;
14441 }
0499d65b
TS
14442 }
14443
14444 while (reg1 <= reg2)
14445 {
14446 if (reg1 >= 4 && reg1 <= 7)
14447 {
3a93f742 14448 if (!seen_framesz)
0499d65b 14449 /* args $a0-$a3 */
91d6fa6a 14450 nargs |= 1 << (reg1 - 4);
0499d65b
TS
14451 else
14452 /* statics $a0-$a3 */
14453 statics |= 1 << (reg1 - 4);
14454 }
14455 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14456 {
14457 /* $s0-$s8 */
14458 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14459 }
14460 else if (reg1 == 31)
14461 {
14462 /* Add $ra to insn. */
14463 opcode |= 0x40;
14464 }
14465 else
14466 {
14467 as_bad (_("unexpected register in list"));
14468 break;
14469 }
14470 if (++reg1 == 24)
14471 reg1 = 30;
14472 }
14473 }
14474
14475 /* Encode args/statics combination. */
91d6fa6a 14476 if (nargs & statics)
0499d65b 14477 as_bad (_("arg/static registers overlap"));
91d6fa6a 14478 else if (nargs == 0xf)
0499d65b
TS
14479 /* All $a0-$a3 are args. */
14480 opcode |= MIPS16_ALL_ARGS << 16;
14481 else if (statics == 0xf)
14482 /* All $a0-$a3 are statics. */
14483 opcode |= MIPS16_ALL_STATICS << 16;
14484 else
14485 {
14486 int narg = 0, nstat = 0;
14487
14488 /* Count arg registers. */
91d6fa6a 14489 while (nargs & 0x1)
0499d65b 14490 {
91d6fa6a 14491 nargs >>= 1;
0499d65b
TS
14492 narg++;
14493 }
91d6fa6a 14494 if (nargs != 0)
0499d65b
TS
14495 as_bad (_("invalid arg register list"));
14496
14497 /* Count static registers. */
14498 while (statics & 0x8)
14499 {
14500 statics = (statics << 1) & 0xf;
14501 nstat++;
14502 }
14503 if (statics != 0)
14504 as_bad (_("invalid static register list"));
14505
14506 /* Encode args/statics. */
14507 opcode |= ((narg << 2) | nstat) << 16;
14508 }
14509
14510 /* Encode $s0/$s1. */
14511 if (sregs & (1 << 0)) /* $s0 */
14512 opcode |= 0x20;
14513 if (sregs & (1 << 1)) /* $s1 */
14514 opcode |= 0x10;
14515 sregs >>= 2;
14516
14517 if (sregs != 0)
14518 {
14519 /* Count regs $s2-$s8. */
14520 int nsreg = 0;
14521 while (sregs & 1)
14522 {
14523 sregs >>= 1;
14524 nsreg++;
14525 }
14526 if (sregs != 0)
14527 as_bad (_("invalid static register list"));
14528 /* Encode $s2-$s8. */
14529 opcode |= nsreg << 24;
14530 }
14531
14532 /* Encode frame size. */
14533 if (!seen_framesz)
14534 as_bad (_("missing frame size"));
14535 else if ((framesz & 7) != 0 || framesz < 0
14536 || framesz > 0xff * 8)
14537 as_bad (_("invalid frame size"));
14538 else if (framesz != 128 || (opcode >> 16) != 0)
14539 {
14540 framesz /= 8;
14541 opcode |= (((framesz & 0xf0) << 16)
14542 | (framesz & 0x0f));
14543 }
14544
14545 /* Finally build the instruction. */
14546 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14547 opcode |= MIPS16_EXTEND;
14548 ip->insn_opcode = opcode;
0499d65b
TS
14549 }
14550 continue;
14551
252b5132
RH
14552 case 'e': /* extend code */
14553 my_getExpression (&imm_expr, s);
14554 check_absolute_expr (ip, &imm_expr);
14555 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14556 {
14557 as_warn (_("Invalid value for `%s' (%lu)"),
14558 ip->insn_mo->name,
14559 (unsigned long) imm_expr.X_add_number);
14560 imm_expr.X_add_number &= 0x7ff;
14561 }
14562 ip->insn_opcode |= imm_expr.X_add_number;
14563 imm_expr.X_op = O_absent;
14564 s = expr_end;
14565 continue;
14566
14567 default:
b37df7c4 14568 abort ();
252b5132
RH
14569 }
14570 break;
14571 }
14572
14573 /* Args don't match. */
14574 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14575 strcmp (insn->name, insn[1].name) == 0)
14576 {
14577 ++insn;
14578 s = argsstart;
14579 continue;
14580 }
14581
14582 insn_error = _("illegal operands");
14583
14584 return;
14585 }
14586}
14587
14588/* This structure holds information we know about a mips16 immediate
14589 argument type. */
14590
e972090a
NC
14591struct mips16_immed_operand
14592{
252b5132
RH
14593 /* The type code used in the argument string in the opcode table. */
14594 int type;
14595 /* The number of bits in the short form of the opcode. */
14596 int nbits;
14597 /* The number of bits in the extended form of the opcode. */
14598 int extbits;
14599 /* The amount by which the short form is shifted when it is used;
14600 for example, the sw instruction has a shift count of 2. */
14601 int shift;
14602 /* The amount by which the short form is shifted when it is stored
14603 into the instruction code. */
14604 int op_shift;
14605 /* Non-zero if the short form is unsigned. */
14606 int unsp;
14607 /* Non-zero if the extended form is unsigned. */
14608 int extu;
14609 /* Non-zero if the value is PC relative. */
14610 int pcrel;
14611};
14612
14613/* The mips16 immediate operand types. */
14614
14615static const struct mips16_immed_operand mips16_immed_operands[] =
14616{
14617 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14618 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14619 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14620 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14621 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14622 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14623 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14624 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14625 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14626 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14627 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14628 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14629 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14630 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14631 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14632 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14633 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14634 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14635 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14636 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14637 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14638};
14639
14640#define MIPS16_NUM_IMMED \
14641 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14642
b886a2ab
RS
14643/* Marshal immediate value VAL for an extended MIPS16 instruction.
14644 NBITS is the number of significant bits in VAL. */
14645
14646static unsigned long
14647mips16_immed_extend (offsetT val, unsigned int nbits)
14648{
14649 int extval;
14650 if (nbits == 16)
14651 {
14652 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14653 val &= 0x1f;
14654 }
14655 else if (nbits == 15)
14656 {
14657 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14658 val &= 0xf;
14659 }
14660 else
14661 {
14662 extval = ((val & 0x1f) << 6) | (val & 0x20);
14663 val = 0;
14664 }
14665 return (extval << 16) | val;
14666}
14667
5c04167a
RS
14668/* Install immediate value VAL into MIPS16 instruction *INSN,
14669 extending it if necessary. The instruction in *INSN may
14670 already be extended.
14671
43c0598f
RS
14672 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14673 if none. In the former case, VAL is a 16-bit number with no
14674 defined signedness.
14675
14676 TYPE is the type of the immediate field. USER_INSN_LENGTH
14677 is the length that the user requested, or 0 if none. */
252b5132
RH
14678
14679static void
43c0598f
RS
14680mips16_immed (char *file, unsigned int line, int type,
14681 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14682 unsigned int user_insn_length, unsigned long *insn)
252b5132 14683{
3994f87e 14684 const struct mips16_immed_operand *op;
252b5132 14685 int mintiny, maxtiny;
252b5132
RH
14686
14687 op = mips16_immed_operands;
14688 while (op->type != type)
14689 {
14690 ++op;
9c2799c2 14691 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14692 }
14693
14694 if (op->unsp)
14695 {
14696 if (type == '<' || type == '>' || type == '[' || type == ']')
14697 {
14698 mintiny = 1;
14699 maxtiny = 1 << op->nbits;
14700 }
14701 else
14702 {
14703 mintiny = 0;
14704 maxtiny = (1 << op->nbits) - 1;
14705 }
43c0598f
RS
14706 if (reloc != BFD_RELOC_UNUSED)
14707 val &= 0xffff;
252b5132
RH
14708 }
14709 else
14710 {
14711 mintiny = - (1 << (op->nbits - 1));
14712 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14713 if (reloc != BFD_RELOC_UNUSED)
14714 val = SEXT_16BIT (val);
252b5132
RH
14715 }
14716
14717 /* Branch offsets have an implicit 0 in the lowest bit. */
14718 if (type == 'p' || type == 'q')
14719 val /= 2;
14720
14721 if ((val & ((1 << op->shift) - 1)) != 0
14722 || val < (mintiny << op->shift)
14723 || val > (maxtiny << op->shift))
5c04167a
RS
14724 {
14725 /* We need an extended instruction. */
14726 if (user_insn_length == 2)
14727 as_bad_where (file, line, _("invalid unextended operand value"));
14728 else
14729 *insn |= MIPS16_EXTEND;
14730 }
14731 else if (user_insn_length == 4)
14732 {
14733 /* The operand doesn't force an unextended instruction to be extended.
14734 Warn if the user wanted an extended instruction anyway. */
14735 *insn |= MIPS16_EXTEND;
14736 as_warn_where (file, line,
14737 _("extended operand requested but not required"));
14738 }
252b5132 14739
5c04167a 14740 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14741 {
14742 int insnval;
14743
252b5132
RH
14744 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14745 insnval <<= op->op_shift;
14746 *insn |= insnval;
14747 }
14748 else
14749 {
14750 long minext, maxext;
252b5132 14751
43c0598f 14752 if (reloc == BFD_RELOC_UNUSED)
252b5132 14753 {
43c0598f
RS
14754 if (op->extu)
14755 {
14756 minext = 0;
14757 maxext = (1 << op->extbits) - 1;
14758 }
14759 else
14760 {
14761 minext = - (1 << (op->extbits - 1));
14762 maxext = (1 << (op->extbits - 1)) - 1;
14763 }
14764 if (val < minext || val > maxext)
14765 as_bad_where (file, line,
14766 _("operand value out of range for instruction"));
252b5132 14767 }
252b5132 14768
b886a2ab 14769 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14770 }
14771}
14772\f
d6f16593 14773struct percent_op_match
ad8d3bb3 14774{
5e0116d5
RS
14775 const char *str;
14776 bfd_reloc_code_real_type reloc;
d6f16593
MR
14777};
14778
14779static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14780{
5e0116d5 14781 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14782 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14783 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14784 {"%call16", BFD_RELOC_MIPS_CALL16},
14785 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14786 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14787 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14788 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14789 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14790 {"%got", BFD_RELOC_MIPS_GOT16},
14791 {"%gp_rel", BFD_RELOC_GPREL16},
14792 {"%half", BFD_RELOC_16},
14793 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14794 {"%higher", BFD_RELOC_MIPS_HIGHER},
14795 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14796 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14797 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14798 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14799 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14800 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14801 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14802 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 14803 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14804};
14805
d6f16593
MR
14806static const struct percent_op_match mips16_percent_op[] =
14807{
14808 {"%lo", BFD_RELOC_MIPS16_LO16},
14809 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14810 {"%got", BFD_RELOC_MIPS16_GOT16},
14811 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14812 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14813 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14814 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14815 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14816 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14817 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14818 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14819 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14820};
14821
252b5132 14822
5e0116d5
RS
14823/* Return true if *STR points to a relocation operator. When returning true,
14824 move *STR over the operator and store its relocation code in *RELOC.
14825 Leave both *STR and *RELOC alone when returning false. */
14826
14827static bfd_boolean
17a2f251 14828parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14829{
d6f16593
MR
14830 const struct percent_op_match *percent_op;
14831 size_t limit, i;
14832
14833 if (mips_opts.mips16)
14834 {
14835 percent_op = mips16_percent_op;
14836 limit = ARRAY_SIZE (mips16_percent_op);
14837 }
14838 else
14839 {
14840 percent_op = mips_percent_op;
14841 limit = ARRAY_SIZE (mips_percent_op);
14842 }
76b3015f 14843
d6f16593 14844 for (i = 0; i < limit; i++)
5e0116d5 14845 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14846 {
3f98094e
DJ
14847 int len = strlen (percent_op[i].str);
14848
14849 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14850 continue;
14851
5e0116d5
RS
14852 *str += strlen (percent_op[i].str);
14853 *reloc = percent_op[i].reloc;
394f9b3a 14854
5e0116d5
RS
14855 /* Check whether the output BFD supports this relocation.
14856 If not, issue an error and fall back on something safe. */
14857 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14858 {
20203fb9 14859 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14860 percent_op[i].str);
01a3f561 14861 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14862 }
5e0116d5 14863 return TRUE;
394f9b3a 14864 }
5e0116d5 14865 return FALSE;
394f9b3a 14866}
ad8d3bb3 14867
ad8d3bb3 14868
5e0116d5
RS
14869/* Parse string STR as a 16-bit relocatable operand. Store the
14870 expression in *EP and the relocations in the array starting
14871 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14872
01a3f561 14873 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14874
5e0116d5 14875static size_t
17a2f251
TS
14876my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14877 char *str)
ad8d3bb3 14878{
5e0116d5
RS
14879 bfd_reloc_code_real_type reversed_reloc[3];
14880 size_t reloc_index, i;
09b8f35a
RS
14881 int crux_depth, str_depth;
14882 char *crux;
5e0116d5
RS
14883
14884 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14885 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14886 of the main expression and with CRUX_DEPTH containing the number
14887 of open brackets at that point. */
14888 reloc_index = -1;
14889 str_depth = 0;
14890 do
fb1b3232 14891 {
09b8f35a
RS
14892 reloc_index++;
14893 crux = str;
14894 crux_depth = str_depth;
14895
14896 /* Skip over whitespace and brackets, keeping count of the number
14897 of brackets. */
14898 while (*str == ' ' || *str == '\t' || *str == '(')
14899 if (*str++ == '(')
14900 str_depth++;
5e0116d5 14901 }
09b8f35a
RS
14902 while (*str == '%'
14903 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14904 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14905
09b8f35a 14906 my_getExpression (ep, crux);
5e0116d5 14907 str = expr_end;
394f9b3a 14908
5e0116d5 14909 /* Match every open bracket. */
09b8f35a 14910 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14911 if (*str++ == ')')
09b8f35a 14912 crux_depth--;
394f9b3a 14913
09b8f35a 14914 if (crux_depth > 0)
20203fb9 14915 as_bad (_("unclosed '('"));
394f9b3a 14916
5e0116d5 14917 expr_end = str;
252b5132 14918
01a3f561 14919 if (reloc_index != 0)
64bdfcaf
RS
14920 {
14921 prev_reloc_op_frag = frag_now;
14922 for (i = 0; i < reloc_index; i++)
14923 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14924 }
fb1b3232 14925
5e0116d5 14926 return reloc_index;
252b5132
RH
14927}
14928
14929static void
17a2f251 14930my_getExpression (expressionS *ep, char *str)
252b5132
RH
14931{
14932 char *save_in;
14933
14934 save_in = input_line_pointer;
14935 input_line_pointer = str;
14936 expression (ep);
14937 expr_end = input_line_pointer;
14938 input_line_pointer = save_in;
252b5132
RH
14939}
14940
252b5132 14941char *
17a2f251 14942md_atof (int type, char *litP, int *sizeP)
252b5132 14943{
499ac353 14944 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14945}
14946
14947void
17a2f251 14948md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14949{
14950 if (target_big_endian)
14951 number_to_chars_bigendian (buf, val, n);
14952 else
14953 number_to_chars_littleendian (buf, val, n);
14954}
14955\f
e013f690
TS
14956static int support_64bit_objects(void)
14957{
14958 const char **list, **l;
aa3d8fdf 14959 int yes;
e013f690
TS
14960
14961 list = bfd_target_list ();
14962 for (l = list; *l != NULL; l++)
aeffff67
RS
14963 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14964 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14965 break;
aa3d8fdf 14966 yes = (*l != NULL);
e013f690 14967 free (list);
aa3d8fdf 14968 return yes;
e013f690
TS
14969}
14970
316f5878
RS
14971/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14972 NEW_VALUE. Warn if another value was already specified. Note:
14973 we have to defer parsing the -march and -mtune arguments in order
14974 to handle 'from-abi' correctly, since the ABI might be specified
14975 in a later argument. */
14976
14977static void
17a2f251 14978mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14979{
14980 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14981 as_warn (_("A different %s was already specified, is now %s"),
14982 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14983 new_value);
14984
14985 *string_ptr = new_value;
14986}
14987
252b5132 14988int
17a2f251 14989md_parse_option (int c, char *arg)
252b5132 14990{
c6278170
RS
14991 unsigned int i;
14992
14993 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14994 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14995 {
14996 file_ase_explicit |= mips_set_ase (&mips_ases[i],
14997 c == mips_ases[i].option_on);
14998 return 1;
14999 }
15000
252b5132
RH
15001 switch (c)
15002 {
119d663a
NC
15003 case OPTION_CONSTRUCT_FLOATS:
15004 mips_disable_float_construction = 0;
15005 break;
bdaaa2e1 15006
119d663a
NC
15007 case OPTION_NO_CONSTRUCT_FLOATS:
15008 mips_disable_float_construction = 1;
15009 break;
bdaaa2e1 15010
252b5132
RH
15011 case OPTION_TRAP:
15012 mips_trap = 1;
15013 break;
15014
15015 case OPTION_BREAK:
15016 mips_trap = 0;
15017 break;
15018
15019 case OPTION_EB:
15020 target_big_endian = 1;
15021 break;
15022
15023 case OPTION_EL:
15024 target_big_endian = 0;
15025 break;
15026
15027 case 'O':
4ffff32f
TS
15028 if (arg == NULL)
15029 mips_optimize = 1;
15030 else if (arg[0] == '0')
15031 mips_optimize = 0;
15032 else if (arg[0] == '1')
252b5132
RH
15033 mips_optimize = 1;
15034 else
15035 mips_optimize = 2;
15036 break;
15037
15038 case 'g':
15039 if (arg == NULL)
15040 mips_debug = 2;
15041 else
15042 mips_debug = atoi (arg);
252b5132
RH
15043 break;
15044
15045 case OPTION_MIPS1:
316f5878 15046 file_mips_isa = ISA_MIPS1;
252b5132
RH
15047 break;
15048
15049 case OPTION_MIPS2:
316f5878 15050 file_mips_isa = ISA_MIPS2;
252b5132
RH
15051 break;
15052
15053 case OPTION_MIPS3:
316f5878 15054 file_mips_isa = ISA_MIPS3;
252b5132
RH
15055 break;
15056
15057 case OPTION_MIPS4:
316f5878 15058 file_mips_isa = ISA_MIPS4;
e7af610e
NC
15059 break;
15060
84ea6cf2 15061 case OPTION_MIPS5:
316f5878 15062 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
15063 break;
15064
e7af610e 15065 case OPTION_MIPS32:
316f5878 15066 file_mips_isa = ISA_MIPS32;
252b5132
RH
15067 break;
15068
af7ee8bf
CD
15069 case OPTION_MIPS32R2:
15070 file_mips_isa = ISA_MIPS32R2;
15071 break;
15072
5f74bc13
CD
15073 case OPTION_MIPS64R2:
15074 file_mips_isa = ISA_MIPS64R2;
15075 break;
15076
84ea6cf2 15077 case OPTION_MIPS64:
316f5878 15078 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
15079 break;
15080
ec68c924 15081 case OPTION_MTUNE:
316f5878
RS
15082 mips_set_option_string (&mips_tune_string, arg);
15083 break;
ec68c924 15084
316f5878
RS
15085 case OPTION_MARCH:
15086 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
15087 break;
15088
15089 case OPTION_M4650:
316f5878
RS
15090 mips_set_option_string (&mips_arch_string, "4650");
15091 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
15092 break;
15093
15094 case OPTION_NO_M4650:
15095 break;
15096
15097 case OPTION_M4010:
316f5878
RS
15098 mips_set_option_string (&mips_arch_string, "4010");
15099 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
15100 break;
15101
15102 case OPTION_NO_M4010:
15103 break;
15104
15105 case OPTION_M4100:
316f5878
RS
15106 mips_set_option_string (&mips_arch_string, "4100");
15107 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
15108 break;
15109
15110 case OPTION_NO_M4100:
15111 break;
15112
252b5132 15113 case OPTION_M3900:
316f5878
RS
15114 mips_set_option_string (&mips_arch_string, "3900");
15115 mips_set_option_string (&mips_tune_string, "3900");
252b5132 15116 break;
bdaaa2e1 15117
252b5132
RH
15118 case OPTION_NO_M3900:
15119 break;
15120
df58fc94
RS
15121 case OPTION_MICROMIPS:
15122 if (mips_opts.mips16 == 1)
15123 {
15124 as_bad (_("-mmicromips cannot be used with -mips16"));
15125 return 0;
15126 }
15127 mips_opts.micromips = 1;
15128 mips_no_prev_insn ();
15129 break;
15130
15131 case OPTION_NO_MICROMIPS:
15132 mips_opts.micromips = 0;
15133 mips_no_prev_insn ();
15134 break;
15135
252b5132 15136 case OPTION_MIPS16:
df58fc94
RS
15137 if (mips_opts.micromips == 1)
15138 {
15139 as_bad (_("-mips16 cannot be used with -micromips"));
15140 return 0;
15141 }
252b5132 15142 mips_opts.mips16 = 1;
7d10b47d 15143 mips_no_prev_insn ();
252b5132
RH
15144 break;
15145
15146 case OPTION_NO_MIPS16:
15147 mips_opts.mips16 = 0;
7d10b47d 15148 mips_no_prev_insn ();
252b5132
RH
15149 break;
15150
6a32d874
CM
15151 case OPTION_FIX_24K:
15152 mips_fix_24k = 1;
15153 break;
15154
15155 case OPTION_NO_FIX_24K:
15156 mips_fix_24k = 0;
15157 break;
15158
c67a084a
NC
15159 case OPTION_FIX_LOONGSON2F_JUMP:
15160 mips_fix_loongson2f_jump = TRUE;
15161 break;
15162
15163 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15164 mips_fix_loongson2f_jump = FALSE;
15165 break;
15166
15167 case OPTION_FIX_LOONGSON2F_NOP:
15168 mips_fix_loongson2f_nop = TRUE;
15169 break;
15170
15171 case OPTION_NO_FIX_LOONGSON2F_NOP:
15172 mips_fix_loongson2f_nop = FALSE;
15173 break;
15174
d766e8ec
RS
15175 case OPTION_FIX_VR4120:
15176 mips_fix_vr4120 = 1;
60b63b72
RS
15177 break;
15178
d766e8ec
RS
15179 case OPTION_NO_FIX_VR4120:
15180 mips_fix_vr4120 = 0;
60b63b72
RS
15181 break;
15182
7d8e00cf
RS
15183 case OPTION_FIX_VR4130:
15184 mips_fix_vr4130 = 1;
15185 break;
15186
15187 case OPTION_NO_FIX_VR4130:
15188 mips_fix_vr4130 = 0;
15189 break;
15190
d954098f
DD
15191 case OPTION_FIX_CN63XXP1:
15192 mips_fix_cn63xxp1 = TRUE;
15193 break;
15194
15195 case OPTION_NO_FIX_CN63XXP1:
15196 mips_fix_cn63xxp1 = FALSE;
15197 break;
15198
4a6a3df4
AO
15199 case OPTION_RELAX_BRANCH:
15200 mips_relax_branch = 1;
15201 break;
15202
15203 case OPTION_NO_RELAX_BRANCH:
15204 mips_relax_branch = 0;
15205 break;
15206
833794fc
MR
15207 case OPTION_INSN32:
15208 mips_opts.insn32 = TRUE;
15209 break;
15210
15211 case OPTION_NO_INSN32:
15212 mips_opts.insn32 = FALSE;
15213 break;
15214
aa6975fb
ILT
15215 case OPTION_MSHARED:
15216 mips_in_shared = TRUE;
15217 break;
15218
15219 case OPTION_MNO_SHARED:
15220 mips_in_shared = FALSE;
15221 break;
15222
aed1a261
RS
15223 case OPTION_MSYM32:
15224 mips_opts.sym32 = TRUE;
15225 break;
15226
15227 case OPTION_MNO_SYM32:
15228 mips_opts.sym32 = FALSE;
15229 break;
15230
252b5132
RH
15231 /* When generating ELF code, we permit -KPIC and -call_shared to
15232 select SVR4_PIC, and -non_shared to select no PIC. This is
15233 intended to be compatible with Irix 5. */
15234 case OPTION_CALL_SHARED:
252b5132 15235 mips_pic = SVR4_PIC;
143d77c5 15236 mips_abicalls = TRUE;
252b5132
RH
15237 break;
15238
861fb55a 15239 case OPTION_CALL_NONPIC:
861fb55a
DJ
15240 mips_pic = NO_PIC;
15241 mips_abicalls = TRUE;
15242 break;
15243
252b5132 15244 case OPTION_NON_SHARED:
252b5132 15245 mips_pic = NO_PIC;
143d77c5 15246 mips_abicalls = FALSE;
252b5132
RH
15247 break;
15248
44075ae2
TS
15249 /* The -xgot option tells the assembler to use 32 bit offsets
15250 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15251 compatibility. */
15252 case OPTION_XGOT:
15253 mips_big_got = 1;
15254 break;
15255
15256 case 'G':
6caf9ef4
TS
15257 g_switch_value = atoi (arg);
15258 g_switch_seen = 1;
252b5132
RH
15259 break;
15260
34ba82a8
TS
15261 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15262 and -mabi=64. */
252b5132 15263 case OPTION_32:
f3ded42a 15264 mips_abi = O32_ABI;
252b5132
RH
15265 break;
15266
e013f690 15267 case OPTION_N32:
316f5878 15268 mips_abi = N32_ABI;
e013f690 15269 break;
252b5132 15270
e013f690 15271 case OPTION_64:
316f5878 15272 mips_abi = N64_ABI;
f43abd2b 15273 if (!support_64bit_objects())
e013f690 15274 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
15275 break;
15276
c97ef257 15277 case OPTION_GP32:
a325df1d 15278 file_mips_gp32 = 1;
c97ef257
AH
15279 break;
15280
15281 case OPTION_GP64:
a325df1d 15282 file_mips_gp32 = 0;
c97ef257 15283 break;
252b5132 15284
ca4e0257 15285 case OPTION_FP32:
a325df1d 15286 file_mips_fp32 = 1;
316f5878
RS
15287 break;
15288
15289 case OPTION_FP64:
15290 file_mips_fp32 = 0;
ca4e0257
RS
15291 break;
15292
037b32b9
AN
15293 case OPTION_SINGLE_FLOAT:
15294 file_mips_single_float = 1;
15295 break;
15296
15297 case OPTION_DOUBLE_FLOAT:
15298 file_mips_single_float = 0;
15299 break;
15300
15301 case OPTION_SOFT_FLOAT:
15302 file_mips_soft_float = 1;
15303 break;
15304
15305 case OPTION_HARD_FLOAT:
15306 file_mips_soft_float = 0;
15307 break;
15308
252b5132 15309 case OPTION_MABI:
e013f690 15310 if (strcmp (arg, "32") == 0)
316f5878 15311 mips_abi = O32_ABI;
e013f690 15312 else if (strcmp (arg, "o64") == 0)
316f5878 15313 mips_abi = O64_ABI;
e013f690 15314 else if (strcmp (arg, "n32") == 0)
316f5878 15315 mips_abi = N32_ABI;
e013f690
TS
15316 else if (strcmp (arg, "64") == 0)
15317 {
316f5878 15318 mips_abi = N64_ABI;
e013f690
TS
15319 if (! support_64bit_objects())
15320 as_fatal (_("No compiled in support for 64 bit object file "
15321 "format"));
15322 }
15323 else if (strcmp (arg, "eabi") == 0)
316f5878 15324 mips_abi = EABI_ABI;
e013f690 15325 else
da0e507f
TS
15326 {
15327 as_fatal (_("invalid abi -mabi=%s"), arg);
15328 return 0;
15329 }
252b5132
RH
15330 break;
15331
6b76fefe 15332 case OPTION_M7000_HILO_FIX:
b34976b6 15333 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15334 break;
15335
9ee72ff1 15336 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15337 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15338 break;
15339
ecb4347a 15340 case OPTION_MDEBUG:
b34976b6 15341 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15342 break;
15343
15344 case OPTION_NO_MDEBUG:
b34976b6 15345 mips_flag_mdebug = FALSE;
ecb4347a 15346 break;
dcd410fe
RO
15347
15348 case OPTION_PDR:
15349 mips_flag_pdr = TRUE;
15350 break;
15351
15352 case OPTION_NO_PDR:
15353 mips_flag_pdr = FALSE;
15354 break;
0a44bf69
RS
15355
15356 case OPTION_MVXWORKS_PIC:
15357 mips_pic = VXWORKS_PIC;
15358 break;
ecb4347a 15359
252b5132
RH
15360 default:
15361 return 0;
15362 }
15363
c67a084a
NC
15364 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15365
252b5132
RH
15366 return 1;
15367}
316f5878
RS
15368\f
15369/* Set up globals to generate code for the ISA or processor
15370 described by INFO. */
252b5132 15371
252b5132 15372static void
17a2f251 15373mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15374{
316f5878 15375 if (info != 0)
252b5132 15376 {
fef14a42
TS
15377 file_mips_arch = info->cpu;
15378 mips_opts.arch = info->cpu;
316f5878 15379 mips_opts.isa = info->isa;
252b5132 15380 }
252b5132
RH
15381}
15382
252b5132 15383
316f5878 15384/* Likewise for tuning. */
252b5132 15385
316f5878 15386static void
17a2f251 15387mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15388{
15389 if (info != 0)
fef14a42 15390 mips_tune = info->cpu;
316f5878 15391}
80cc45a5 15392
34ba82a8 15393
252b5132 15394void
17a2f251 15395mips_after_parse_args (void)
e9670677 15396{
fef14a42
TS
15397 const struct mips_cpu_info *arch_info = 0;
15398 const struct mips_cpu_info *tune_info = 0;
15399
e9670677 15400 /* GP relative stuff not working for PE */
6caf9ef4 15401 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15402 {
6caf9ef4 15403 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15404 as_bad (_("-G not supported in this configuration."));
15405 g_switch_value = 0;
15406 }
15407
cac012d6
AO
15408 if (mips_abi == NO_ABI)
15409 mips_abi = MIPS_DEFAULT_ABI;
15410
22923709
RS
15411 /* The following code determines the architecture and register size.
15412 Similar code was added to GCC 3.3 (see override_options() in
15413 config/mips/mips.c). The GAS and GCC code should be kept in sync
15414 as much as possible. */
e9670677 15415
316f5878 15416 if (mips_arch_string != 0)
fef14a42 15417 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15418
316f5878 15419 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15420 {
316f5878 15421 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15422 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15423 the -march selection (if any). */
fef14a42 15424 if (arch_info != 0)
e9670677 15425 {
316f5878
RS
15426 /* -march takes precedence over -mipsN, since it is more descriptive.
15427 There's no harm in specifying both as long as the ISA levels
15428 are the same. */
fef14a42 15429 if (file_mips_isa != arch_info->isa)
316f5878
RS
15430 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15431 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15432 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15433 }
316f5878 15434 else
fef14a42 15435 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15436 }
15437
fef14a42 15438 if (arch_info == 0)
95bfe26e
MF
15439 {
15440 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15441 gas_assert (arch_info);
15442 }
e9670677 15443
fef14a42 15444 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15445 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15446 arch_info->name);
15447
15448 mips_set_architecture (arch_info);
15449
15450 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15451 if (mips_tune_string != 0)
15452 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15453
fef14a42
TS
15454 if (tune_info == 0)
15455 mips_set_tune (arch_info);
15456 else
15457 mips_set_tune (tune_info);
e9670677 15458
316f5878 15459 if (file_mips_gp32 >= 0)
e9670677 15460 {
316f5878
RS
15461 /* The user specified the size of the integer registers. Make sure
15462 it agrees with the ABI and ISA. */
15463 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15464 as_bad (_("-mgp64 used with a 32-bit processor"));
15465 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15466 as_bad (_("-mgp32 used with a 64-bit ABI"));
15467 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15468 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15469 }
15470 else
15471 {
316f5878
RS
15472 /* Infer the integer register size from the ABI and processor.
15473 Restrict ourselves to 32-bit registers if that's all the
15474 processor has, or if the ABI cannot handle 64-bit registers. */
15475 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15476 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15477 }
15478
ad3fea08
TS
15479 switch (file_mips_fp32)
15480 {
15481 default:
15482 case -1:
15483 /* No user specified float register size.
15484 ??? GAS treats single-float processors as though they had 64-bit
15485 float registers (although it complains when double-precision
15486 instructions are used). As things stand, saying they have 32-bit
15487 registers would lead to spurious "register must be even" messages.
15488 So here we assume float registers are never smaller than the
15489 integer ones. */
15490 if (file_mips_gp32 == 0)
15491 /* 64-bit integer registers implies 64-bit float registers. */
15492 file_mips_fp32 = 0;
c6278170 15493 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
15494 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15495 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15496 file_mips_fp32 = 0;
15497 else
15498 /* 32-bit float registers. */
15499 file_mips_fp32 = 1;
15500 break;
15501
15502 /* The user specified the size of the float registers. Check if it
15503 agrees with the ABI and ISA. */
15504 case 0:
15505 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15506 as_bad (_("-mfp64 used with a 32-bit fpu"));
15507 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15508 && !ISA_HAS_MXHC1 (mips_opts.isa))
15509 as_warn (_("-mfp64 used with a 32-bit ABI"));
15510 break;
15511 case 1:
15512 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15513 as_warn (_("-mfp32 used with a 64-bit ABI"));
15514 break;
15515 }
e9670677 15516
316f5878 15517 /* End of GCC-shared inference code. */
e9670677 15518
17a2f251
TS
15519 /* This flag is set when we have a 64-bit capable CPU but use only
15520 32-bit wide registers. Note that EABI does not use it. */
15521 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15522 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15523 || mips_abi == O32_ABI))
316f5878 15524 mips_32bitmode = 1;
e9670677
MR
15525
15526 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15527 as_bad (_("trap exception not supported at ISA 1"));
15528
e9670677
MR
15529 /* If the selected architecture includes support for ASEs, enable
15530 generation of code for them. */
a4672219 15531 if (mips_opts.mips16 == -1)
fef14a42 15532 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15533 if (mips_opts.micromips == -1)
15534 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
15535
15536 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
15537 ASEs from being selected implicitly. */
15538 if (file_mips_fp32 == 1)
15539 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
15540
15541 /* If the user didn't explicitly select or deselect a particular ASE,
15542 use the default setting for the CPU. */
15543 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
15544
e9670677 15545 file_mips_isa = mips_opts.isa;
846ef2d0 15546 file_ase = mips_opts.ase;
e9670677
MR
15547 mips_opts.gp32 = file_mips_gp32;
15548 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15549 mips_opts.soft_float = file_mips_soft_float;
15550 mips_opts.single_float = file_mips_single_float;
e9670677 15551
c6278170
RS
15552 mips_check_isa_supports_ases ();
15553
ecb4347a 15554 if (mips_flag_mdebug < 0)
e8044f35 15555 mips_flag_mdebug = 0;
e9670677
MR
15556}
15557\f
15558void
17a2f251 15559mips_init_after_args (void)
252b5132
RH
15560{
15561 /* initialize opcodes */
15562 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15563 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15564}
15565
15566long
17a2f251 15567md_pcrel_from (fixS *fixP)
252b5132 15568{
a7ebbfdf
TS
15569 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15570 switch (fixP->fx_r_type)
15571 {
df58fc94
RS
15572 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15573 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15574 /* Return the address of the delay slot. */
15575 return addr + 2;
15576
15577 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15578 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15579 case BFD_RELOC_16_PCREL_S2:
15580 case BFD_RELOC_MIPS_JMP:
15581 /* Return the address of the delay slot. */
15582 return addr + 4;
df58fc94 15583
b47468a6
CM
15584 case BFD_RELOC_32_PCREL:
15585 return addr;
15586
a7ebbfdf 15587 default:
58ea3d6a 15588 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15589 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15590 as_bad_where (fixP->fx_file, fixP->fx_line,
15591 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15592 return addr;
15593 }
252b5132
RH
15594}
15595
252b5132
RH
15596/* This is called before the symbol table is processed. In order to
15597 work with gcc when using mips-tfile, we must keep all local labels.
15598 However, in other cases, we want to discard them. If we were
15599 called with -g, but we didn't see any debugging information, it may
15600 mean that gcc is smuggling debugging information through to
15601 mips-tfile, in which case we must generate all local labels. */
15602
15603void
17a2f251 15604mips_frob_file_before_adjust (void)
252b5132
RH
15605{
15606#ifndef NO_ECOFF_DEBUGGING
15607 if (ECOFF_DEBUGGING
15608 && mips_debug != 0
15609 && ! ecoff_debugging_seen)
15610 flag_keep_locals = 1;
15611#endif
15612}
15613
3b91255e 15614/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15615 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15616 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15617 relocation operators.
15618
15619 For our purposes, a %lo() expression matches a %got() or %hi()
15620 expression if:
15621
15622 (a) it refers to the same symbol; and
15623 (b) the offset applied in the %lo() expression is no lower than
15624 the offset applied in the %got() or %hi().
15625
15626 (b) allows us to cope with code like:
15627
15628 lui $4,%hi(foo)
15629 lh $4,%lo(foo+2)($4)
15630
15631 ...which is legal on RELA targets, and has a well-defined behaviour
15632 if the user knows that adding 2 to "foo" will not induce a carry to
15633 the high 16 bits.
15634
15635 When several %lo()s match a particular %got() or %hi(), we use the
15636 following rules to distinguish them:
15637
15638 (1) %lo()s with smaller offsets are a better match than %lo()s with
15639 higher offsets.
15640
15641 (2) %lo()s with no matching %got() or %hi() are better than those
15642 that already have a matching %got() or %hi().
15643
15644 (3) later %lo()s are better than earlier %lo()s.
15645
15646 These rules are applied in order.
15647
15648 (1) means, among other things, that %lo()s with identical offsets are
15649 chosen if they exist.
15650
15651 (2) means that we won't associate several high-part relocations with
15652 the same low-part relocation unless there's no alternative. Having
15653 several high parts for the same low part is a GNU extension; this rule
15654 allows careful users to avoid it.
15655
15656 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15657 with the last high-part relocation being at the front of the list.
15658 It therefore makes sense to choose the last matching low-part
15659 relocation, all other things being equal. It's also easier
15660 to code that way. */
252b5132
RH
15661
15662void
17a2f251 15663mips_frob_file (void)
252b5132
RH
15664{
15665 struct mips_hi_fixup *l;
35903be0 15666 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15667
15668 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15669 {
15670 segment_info_type *seginfo;
3b91255e
RS
15671 bfd_boolean matched_lo_p;
15672 fixS **hi_pos, **lo_pos, **pos;
252b5132 15673
9c2799c2 15674 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15675
5919d012 15676 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15677 there isn't supposed to be a matching LO. Ignore %gots against
15678 constants; we'll report an error for those later. */
738e5348 15679 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15680 && !(l->fixp->fx_addsy
15681 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15682 continue;
15683
15684 /* Check quickly whether the next fixup happens to be a matching %lo. */
15685 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15686 continue;
15687
252b5132 15688 seginfo = seg_info (l->seg);
252b5132 15689
3b91255e
RS
15690 /* Set HI_POS to the position of this relocation in the chain.
15691 Set LO_POS to the position of the chosen low-part relocation.
15692 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15693 relocation that matches an immediately-preceding high-part
15694 relocation. */
15695 hi_pos = NULL;
15696 lo_pos = NULL;
15697 matched_lo_p = FALSE;
738e5348 15698 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15699
3b91255e
RS
15700 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15701 {
15702 if (*pos == l->fixp)
15703 hi_pos = pos;
15704
35903be0 15705 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15706 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15707 && (*pos)->fx_offset >= l->fixp->fx_offset
15708 && (lo_pos == NULL
15709 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15710 || (!matched_lo_p
15711 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15712 lo_pos = pos;
15713
15714 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15715 && fixup_has_matching_lo_p (*pos));
15716 }
15717
15718 /* If we found a match, remove the high-part relocation from its
15719 current position and insert it before the low-part relocation.
15720 Make the offsets match so that fixup_has_matching_lo_p()
15721 will return true.
15722
15723 We don't warn about unmatched high-part relocations since some
15724 versions of gcc have been known to emit dead "lui ...%hi(...)"
15725 instructions. */
15726 if (lo_pos != NULL)
15727 {
15728 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15729 if (l->fixp->fx_next != *lo_pos)
252b5132 15730 {
3b91255e
RS
15731 *hi_pos = l->fixp->fx_next;
15732 l->fixp->fx_next = *lo_pos;
15733 *lo_pos = l->fixp;
252b5132 15734 }
252b5132
RH
15735 }
15736 }
15737}
15738
252b5132 15739int
17a2f251 15740mips_force_relocation (fixS *fixp)
252b5132 15741{
ae6063d4 15742 if (generic_force_reloc (fixp))
252b5132
RH
15743 return 1;
15744
df58fc94
RS
15745 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15746 so that the linker relaxation can update targets. */
15747 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15748 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15749 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15750 return 1;
15751
3e722fb5 15752 return 0;
252b5132
RH
15753}
15754
b886a2ab
RS
15755/* Read the instruction associated with RELOC from BUF. */
15756
15757static unsigned int
15758read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15759{
15760 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15761 return read_compressed_insn (buf, 4);
15762 else
15763 return read_insn (buf);
15764}
15765
15766/* Write instruction INSN to BUF, given that it has been relocated
15767 by RELOC. */
15768
15769static void
15770write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15771 unsigned long insn)
15772{
15773 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15774 write_compressed_insn (buf, insn, 4);
15775 else
15776 write_insn (buf, insn);
15777}
15778
252b5132
RH
15779/* Apply a fixup to the object file. */
15780
94f592af 15781void
55cf6793 15782md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15783{
4d68580a 15784 char *buf;
b886a2ab 15785 unsigned long insn;
a7ebbfdf 15786 reloc_howto_type *howto;
252b5132 15787
a7ebbfdf
TS
15788 /* We ignore generic BFD relocations we don't know about. */
15789 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15790 if (! howto)
15791 return;
65551fa4 15792
df58fc94
RS
15793 gas_assert (fixP->fx_size == 2
15794 || fixP->fx_size == 4
90ecf173
MR
15795 || fixP->fx_r_type == BFD_RELOC_16
15796 || fixP->fx_r_type == BFD_RELOC_64
15797 || fixP->fx_r_type == BFD_RELOC_CTOR
15798 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15799 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15800 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15801 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15802 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15803
4d68580a 15804 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15805
df58fc94
RS
15806 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15807 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15808 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
15809 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15810 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
15811
15812 /* Don't treat parts of a composite relocation as done. There are two
15813 reasons for this:
15814
15815 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15816 should nevertheless be emitted if the first part is.
15817
15818 (2) In normal usage, composite relocations are never assembly-time
15819 constants. The easiest way of dealing with the pathological
15820 exceptions is to generate a relocation against STN_UNDEF and
15821 leave everything up to the linker. */
3994f87e 15822 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15823 fixP->fx_done = 1;
15824
15825 switch (fixP->fx_r_type)
15826 {
3f98094e
DJ
15827 case BFD_RELOC_MIPS_TLS_GD:
15828 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15829 case BFD_RELOC_MIPS_TLS_DTPREL32:
15830 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15831 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15832 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15833 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15834 case BFD_RELOC_MIPS_TLS_TPREL32:
15835 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15836 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15837 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15838 case BFD_RELOC_MICROMIPS_TLS_GD:
15839 case BFD_RELOC_MICROMIPS_TLS_LDM:
15840 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15841 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15842 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15843 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15844 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15845 case BFD_RELOC_MIPS16_TLS_GD:
15846 case BFD_RELOC_MIPS16_TLS_LDM:
15847 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15848 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15849 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15850 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15851 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15852 if (!fixP->fx_addsy)
15853 {
15854 as_bad_where (fixP->fx_file, fixP->fx_line,
15855 _("TLS relocation against a constant"));
15856 break;
15857 }
3f98094e
DJ
15858 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15859 /* fall through */
15860
252b5132 15861 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15862 case BFD_RELOC_MIPS_SHIFT5:
15863 case BFD_RELOC_MIPS_SHIFT6:
15864 case BFD_RELOC_MIPS_GOT_DISP:
15865 case BFD_RELOC_MIPS_GOT_PAGE:
15866 case BFD_RELOC_MIPS_GOT_OFST:
15867 case BFD_RELOC_MIPS_SUB:
15868 case BFD_RELOC_MIPS_INSERT_A:
15869 case BFD_RELOC_MIPS_INSERT_B:
15870 case BFD_RELOC_MIPS_DELETE:
15871 case BFD_RELOC_MIPS_HIGHEST:
15872 case BFD_RELOC_MIPS_HIGHER:
15873 case BFD_RELOC_MIPS_SCN_DISP:
15874 case BFD_RELOC_MIPS_REL16:
15875 case BFD_RELOC_MIPS_RELGOT:
15876 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15877 case BFD_RELOC_HI16:
15878 case BFD_RELOC_HI16_S:
b886a2ab 15879 case BFD_RELOC_LO16:
cdf6fd85 15880 case BFD_RELOC_GPREL16:
252b5132
RH
15881 case BFD_RELOC_MIPS_LITERAL:
15882 case BFD_RELOC_MIPS_CALL16:
15883 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15884 case BFD_RELOC_GPREL32:
252b5132
RH
15885 case BFD_RELOC_MIPS_GOT_HI16:
15886 case BFD_RELOC_MIPS_GOT_LO16:
15887 case BFD_RELOC_MIPS_CALL_HI16:
15888 case BFD_RELOC_MIPS_CALL_LO16:
15889 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15890 case BFD_RELOC_MIPS16_GOT16:
15891 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15892 case BFD_RELOC_MIPS16_HI16:
15893 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15894 case BFD_RELOC_MIPS16_LO16:
252b5132 15895 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15896 case BFD_RELOC_MICROMIPS_JMP:
15897 case BFD_RELOC_MICROMIPS_GOT_DISP:
15898 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15899 case BFD_RELOC_MICROMIPS_GOT_OFST:
15900 case BFD_RELOC_MICROMIPS_SUB:
15901 case BFD_RELOC_MICROMIPS_HIGHEST:
15902 case BFD_RELOC_MICROMIPS_HIGHER:
15903 case BFD_RELOC_MICROMIPS_SCN_DISP:
15904 case BFD_RELOC_MICROMIPS_JALR:
15905 case BFD_RELOC_MICROMIPS_HI16:
15906 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15907 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15908 case BFD_RELOC_MICROMIPS_GPREL16:
15909 case BFD_RELOC_MICROMIPS_LITERAL:
15910 case BFD_RELOC_MICROMIPS_CALL16:
15911 case BFD_RELOC_MICROMIPS_GOT16:
15912 case BFD_RELOC_MICROMIPS_GOT_HI16:
15913 case BFD_RELOC_MICROMIPS_GOT_LO16:
15914 case BFD_RELOC_MICROMIPS_CALL_HI16:
15915 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15916 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15917 if (fixP->fx_done)
15918 {
15919 offsetT value;
15920
15921 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15922 {
15923 insn = read_reloc_insn (buf, fixP->fx_r_type);
15924 if (mips16_reloc_p (fixP->fx_r_type))
15925 insn |= mips16_immed_extend (value, 16);
15926 else
15927 insn |= (value & 0xffff);
15928 write_reloc_insn (buf, fixP->fx_r_type, insn);
15929 }
15930 else
15931 as_bad_where (fixP->fx_file, fixP->fx_line,
15932 _("Unsupported constant in relocation"));
15933 }
252b5132
RH
15934 break;
15935
252b5132
RH
15936 case BFD_RELOC_64:
15937 /* This is handled like BFD_RELOC_32, but we output a sign
15938 extended value if we are only 32 bits. */
3e722fb5 15939 if (fixP->fx_done)
252b5132
RH
15940 {
15941 if (8 <= sizeof (valueT))
4d68580a 15942 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15943 else
15944 {
a7ebbfdf 15945 valueT hiv;
252b5132 15946
a7ebbfdf 15947 if ((*valP & 0x80000000) != 0)
252b5132
RH
15948 hiv = 0xffffffff;
15949 else
15950 hiv = 0;
4d68580a
RS
15951 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15952 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15953 }
15954 }
15955 break;
15956
056350c6 15957 case BFD_RELOC_RVA:
252b5132 15958 case BFD_RELOC_32:
b47468a6 15959 case BFD_RELOC_32_PCREL:
252b5132
RH
15960 case BFD_RELOC_16:
15961 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15962 value now. This can happen if we have a .word which is not
15963 resolved when it appears but is later defined. */
252b5132 15964 if (fixP->fx_done)
4d68580a 15965 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15966 break;
15967
252b5132 15968 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15969 if ((*valP & 0x3) != 0)
cb56d3d3 15970 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15971 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15972
54f4ddb3
TS
15973 /* We need to save the bits in the instruction since fixup_segment()
15974 might be deleting the relocation entry (i.e., a branch within
15975 the current segment). */
a7ebbfdf 15976 if (! fixP->fx_done)
bb2d6cd7 15977 break;
252b5132 15978
54f4ddb3 15979 /* Update old instruction data. */
4d68580a 15980 insn = read_insn (buf);
252b5132 15981
a7ebbfdf
TS
15982 if (*valP + 0x20000 <= 0x3ffff)
15983 {
15984 insn |= (*valP >> 2) & 0xffff;
4d68580a 15985 write_insn (buf, insn);
a7ebbfdf
TS
15986 }
15987 else if (mips_pic == NO_PIC
15988 && fixP->fx_done
15989 && fixP->fx_frag->fr_address >= text_section->vma
15990 && (fixP->fx_frag->fr_address
587aac4e 15991 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15992 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15993 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15994 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15995 {
15996 /* The branch offset is too large. If this is an
15997 unconditional branch, and we are not generating PIC code,
15998 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15999 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16000 insn = 0x0c000000; /* jal */
252b5132 16001 else
a7ebbfdf
TS
16002 insn = 0x08000000; /* j */
16003 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16004 fixP->fx_done = 0;
16005 fixP->fx_addsy = section_symbol (text_section);
16006 *valP += md_pcrel_from (fixP);
4d68580a 16007 write_insn (buf, insn);
a7ebbfdf
TS
16008 }
16009 else
16010 {
16011 /* If we got here, we have branch-relaxation disabled,
16012 and there's nothing we can do to fix this instruction
16013 without turning it into a longer sequence. */
16014 as_bad_where (fixP->fx_file, fixP->fx_line,
16015 _("Branch out of range"));
252b5132 16016 }
252b5132
RH
16017 break;
16018
df58fc94
RS
16019 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16020 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16021 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
16022 /* We adjust the offset back to even. */
16023 if ((*valP & 0x1) != 0)
16024 --(*valP);
16025
16026 if (! fixP->fx_done)
16027 break;
16028
16029 /* Should never visit here, because we keep the relocation. */
16030 abort ();
16031 break;
16032
252b5132
RH
16033 case BFD_RELOC_VTABLE_INHERIT:
16034 fixP->fx_done = 0;
16035 if (fixP->fx_addsy
16036 && !S_IS_DEFINED (fixP->fx_addsy)
16037 && !S_IS_WEAK (fixP->fx_addsy))
16038 S_SET_WEAK (fixP->fx_addsy);
16039 break;
16040
16041 case BFD_RELOC_VTABLE_ENTRY:
16042 fixP->fx_done = 0;
16043 break;
16044
16045 default:
b37df7c4 16046 abort ();
252b5132 16047 }
a7ebbfdf
TS
16048
16049 /* Remember value for tc_gen_reloc. */
16050 fixP->fx_addnumber = *valP;
252b5132
RH
16051}
16052
252b5132 16053static symbolS *
17a2f251 16054get_symbol (void)
252b5132
RH
16055{
16056 int c;
16057 char *name;
16058 symbolS *p;
16059
16060 name = input_line_pointer;
16061 c = get_symbol_end ();
16062 p = (symbolS *) symbol_find_or_make (name);
16063 *input_line_pointer = c;
16064 return p;
16065}
16066
742a56fe
RS
16067/* Align the current frag to a given power of two. If a particular
16068 fill byte should be used, FILL points to an integer that contains
16069 that byte, otherwise FILL is null.
16070
462427c4
RS
16071 This function used to have the comment:
16072
16073 The MIPS assembler also automatically adjusts any preceding label.
16074
16075 The implementation therefore applied the adjustment to a maximum of
16076 one label. However, other label adjustments are applied to batches
16077 of labels, and adjusting just one caused problems when new labels
16078 were added for the sake of debugging or unwind information.
16079 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16080
16081static void
462427c4 16082mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16083{
7d10b47d 16084 mips_emit_delays ();
df58fc94 16085 mips_record_compressed_mode ();
742a56fe
RS
16086 if (fill == NULL && subseg_text_p (now_seg))
16087 frag_align_code (to, 0);
16088 else
16089 frag_align (to, fill ? *fill : 0, 0);
252b5132 16090 record_alignment (now_seg, to);
462427c4 16091 mips_move_labels (labels, FALSE);
252b5132
RH
16092}
16093
16094/* Align to a given power of two. .align 0 turns off the automatic
16095 alignment used by the data creating pseudo-ops. */
16096
16097static void
17a2f251 16098s_align (int x ATTRIBUTE_UNUSED)
252b5132 16099{
742a56fe 16100 int temp, fill_value, *fill_ptr;
49954fb4 16101 long max_alignment = 28;
252b5132 16102
54f4ddb3 16103 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16104 to the aligned address.
54f4ddb3 16105 o It's not documented but auto alignment is reinstated by
252b5132 16106 a .align pseudo instruction.
54f4ddb3 16107 o Note also that after auto alignment is turned off the mips assembler
252b5132 16108 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16109 We don't. */
252b5132
RH
16110
16111 temp = get_absolute_expression ();
16112 if (temp > max_alignment)
16113 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16114 else if (temp < 0)
16115 {
16116 as_warn (_("Alignment negative: 0 assumed."));
16117 temp = 0;
16118 }
16119 if (*input_line_pointer == ',')
16120 {
f9419b05 16121 ++input_line_pointer;
742a56fe
RS
16122 fill_value = get_absolute_expression ();
16123 fill_ptr = &fill_value;
252b5132
RH
16124 }
16125 else
742a56fe 16126 fill_ptr = 0;
252b5132
RH
16127 if (temp)
16128 {
a8dbcb85
TS
16129 segment_info_type *si = seg_info (now_seg);
16130 struct insn_label_list *l = si->label_list;
54f4ddb3 16131 /* Auto alignment should be switched on by next section change. */
252b5132 16132 auto_align = 1;
462427c4 16133 mips_align (temp, fill_ptr, l);
252b5132
RH
16134 }
16135 else
16136 {
16137 auto_align = 0;
16138 }
16139
16140 demand_empty_rest_of_line ();
16141}
16142
252b5132 16143static void
17a2f251 16144s_change_sec (int sec)
252b5132
RH
16145{
16146 segT seg;
16147
252b5132
RH
16148 /* The ELF backend needs to know that we are changing sections, so
16149 that .previous works correctly. We could do something like check
b6ff326e 16150 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16151 as it would not be appropriate to use it in the section changing
16152 functions in read.c, since obj-elf.c intercepts those. FIXME:
16153 This should be cleaner, somehow. */
f3ded42a 16154 obj_elf_section_change_hook ();
252b5132 16155
7d10b47d 16156 mips_emit_delays ();
6a32d874 16157
252b5132
RH
16158 switch (sec)
16159 {
16160 case 't':
16161 s_text (0);
16162 break;
16163 case 'd':
16164 s_data (0);
16165 break;
16166 case 'b':
16167 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16168 demand_empty_rest_of_line ();
16169 break;
16170
16171 case 'r':
4d0d148d
TS
16172 seg = subseg_new (RDATA_SECTION_NAME,
16173 (subsegT) get_absolute_expression ());
f3ded42a
RS
16174 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16175 | SEC_READONLY | SEC_RELOC
16176 | SEC_DATA));
16177 if (strncmp (TARGET_OS, "elf", 3) != 0)
16178 record_alignment (seg, 4);
4d0d148d 16179 demand_empty_rest_of_line ();
252b5132
RH
16180 break;
16181
16182 case 's':
4d0d148d 16183 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16184 bfd_set_section_flags (stdoutput, seg,
16185 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16186 if (strncmp (TARGET_OS, "elf", 3) != 0)
16187 record_alignment (seg, 4);
4d0d148d
TS
16188 demand_empty_rest_of_line ();
16189 break;
998b3c36
MR
16190
16191 case 'B':
16192 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16193 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16194 if (strncmp (TARGET_OS, "elf", 3) != 0)
16195 record_alignment (seg, 4);
998b3c36
MR
16196 demand_empty_rest_of_line ();
16197 break;
252b5132
RH
16198 }
16199
16200 auto_align = 1;
16201}
b34976b6 16202
cca86cc8 16203void
17a2f251 16204s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16205{
cca86cc8
SC
16206 char *section_name;
16207 char c;
684022ea 16208 char next_c = 0;
cca86cc8
SC
16209 int section_type;
16210 int section_flag;
16211 int section_entry_size;
16212 int section_alignment;
b34976b6 16213
cca86cc8
SC
16214 section_name = input_line_pointer;
16215 c = get_symbol_end ();
a816d1ed
AO
16216 if (c)
16217 next_c = *(input_line_pointer + 1);
cca86cc8 16218
4cf0dd0d
TS
16219 /* Do we have .section Name<,"flags">? */
16220 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16221 {
4cf0dd0d
TS
16222 /* just after name is now '\0'. */
16223 *input_line_pointer = c;
cca86cc8
SC
16224 input_line_pointer = section_name;
16225 obj_elf_section (ignore);
16226 return;
16227 }
16228 input_line_pointer++;
16229
16230 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16231 if (c == ',')
16232 section_type = get_absolute_expression ();
16233 else
16234 section_type = 0;
16235 if (*input_line_pointer++ == ',')
16236 section_flag = get_absolute_expression ();
16237 else
16238 section_flag = 0;
16239 if (*input_line_pointer++ == ',')
16240 section_entry_size = get_absolute_expression ();
16241 else
16242 section_entry_size = 0;
16243 if (*input_line_pointer++ == ',')
16244 section_alignment = get_absolute_expression ();
16245 else
16246 section_alignment = 0;
87975d2a
AM
16247 /* FIXME: really ignore? */
16248 (void) section_alignment;
cca86cc8 16249
a816d1ed
AO
16250 section_name = xstrdup (section_name);
16251
8ab8a5c8
RS
16252 /* When using the generic form of .section (as implemented by obj-elf.c),
16253 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16254 traditionally had to fall back on the more common @progbits instead.
16255
16256 There's nothing really harmful in this, since bfd will correct
16257 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16258 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16259 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16260
16261 Even so, we shouldn't force users of the MIPS .section syntax to
16262 incorrectly label the sections as SHT_PROGBITS. The best compromise
16263 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16264 generic type-checking code. */
16265 if (section_type == SHT_MIPS_DWARF)
16266 section_type = SHT_PROGBITS;
16267
cca86cc8
SC
16268 obj_elf_change_section (section_name, section_type, section_flag,
16269 section_entry_size, 0, 0, 0);
a816d1ed
AO
16270
16271 if (now_seg->name != section_name)
16272 free (section_name);
cca86cc8 16273}
252b5132
RH
16274
16275void
17a2f251 16276mips_enable_auto_align (void)
252b5132
RH
16277{
16278 auto_align = 1;
16279}
16280
16281static void
17a2f251 16282s_cons (int log_size)
252b5132 16283{
a8dbcb85
TS
16284 segment_info_type *si = seg_info (now_seg);
16285 struct insn_label_list *l = si->label_list;
252b5132 16286
7d10b47d 16287 mips_emit_delays ();
252b5132 16288 if (log_size > 0 && auto_align)
462427c4 16289 mips_align (log_size, 0, l);
252b5132 16290 cons (1 << log_size);
a1facbec 16291 mips_clear_insn_labels ();
252b5132
RH
16292}
16293
16294static void
17a2f251 16295s_float_cons (int type)
252b5132 16296{
a8dbcb85
TS
16297 segment_info_type *si = seg_info (now_seg);
16298 struct insn_label_list *l = si->label_list;
252b5132 16299
7d10b47d 16300 mips_emit_delays ();
252b5132
RH
16301
16302 if (auto_align)
49309057
ILT
16303 {
16304 if (type == 'd')
462427c4 16305 mips_align (3, 0, l);
49309057 16306 else
462427c4 16307 mips_align (2, 0, l);
49309057 16308 }
252b5132 16309
252b5132 16310 float_cons (type);
a1facbec 16311 mips_clear_insn_labels ();
252b5132
RH
16312}
16313
16314/* Handle .globl. We need to override it because on Irix 5 you are
16315 permitted to say
16316 .globl foo .text
16317 where foo is an undefined symbol, to mean that foo should be
16318 considered to be the address of a function. */
16319
16320static void
17a2f251 16321s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16322{
16323 char *name;
16324 int c;
16325 symbolS *symbolP;
16326 flagword flag;
16327
8a06b769 16328 do
252b5132 16329 {
8a06b769 16330 name = input_line_pointer;
252b5132 16331 c = get_symbol_end ();
8a06b769
TS
16332 symbolP = symbol_find_or_make (name);
16333 S_SET_EXTERNAL (symbolP);
16334
252b5132 16335 *input_line_pointer = c;
8a06b769 16336 SKIP_WHITESPACE ();
252b5132 16337
8a06b769
TS
16338 /* On Irix 5, every global symbol that is not explicitly labelled as
16339 being a function is apparently labelled as being an object. */
16340 flag = BSF_OBJECT;
252b5132 16341
8a06b769
TS
16342 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16343 && (*input_line_pointer != ','))
16344 {
16345 char *secname;
16346 asection *sec;
16347
16348 secname = input_line_pointer;
16349 c = get_symbol_end ();
16350 sec = bfd_get_section_by_name (stdoutput, secname);
16351 if (sec == NULL)
16352 as_bad (_("%s: no such section"), secname);
16353 *input_line_pointer = c;
16354
16355 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16356 flag = BSF_FUNCTION;
16357 }
16358
16359 symbol_get_bfdsym (symbolP)->flags |= flag;
16360
16361 c = *input_line_pointer;
16362 if (c == ',')
16363 {
16364 input_line_pointer++;
16365 SKIP_WHITESPACE ();
16366 if (is_end_of_line[(unsigned char) *input_line_pointer])
16367 c = '\n';
16368 }
16369 }
16370 while (c == ',');
252b5132 16371
252b5132
RH
16372 demand_empty_rest_of_line ();
16373}
16374
16375static void
17a2f251 16376s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16377{
16378 char *opt;
16379 char c;
16380
16381 opt = input_line_pointer;
16382 c = get_symbol_end ();
16383
16384 if (*opt == 'O')
16385 {
16386 /* FIXME: What does this mean? */
16387 }
16388 else if (strncmp (opt, "pic", 3) == 0)
16389 {
16390 int i;
16391
16392 i = atoi (opt + 3);
16393 if (i == 0)
16394 mips_pic = NO_PIC;
16395 else if (i == 2)
143d77c5 16396 {
8b828383 16397 mips_pic = SVR4_PIC;
143d77c5
EC
16398 mips_abicalls = TRUE;
16399 }
252b5132
RH
16400 else
16401 as_bad (_(".option pic%d not supported"), i);
16402
4d0d148d 16403 if (mips_pic == SVR4_PIC)
252b5132
RH
16404 {
16405 if (g_switch_seen && g_switch_value != 0)
16406 as_warn (_("-G may not be used with SVR4 PIC code"));
16407 g_switch_value = 0;
16408 bfd_set_gp_size (stdoutput, 0);
16409 }
16410 }
16411 else
16412 as_warn (_("Unrecognized option \"%s\""), opt);
16413
16414 *input_line_pointer = c;
16415 demand_empty_rest_of_line ();
16416}
16417
16418/* This structure is used to hold a stack of .set values. */
16419
e972090a
NC
16420struct mips_option_stack
16421{
252b5132
RH
16422 struct mips_option_stack *next;
16423 struct mips_set_options options;
16424};
16425
16426static struct mips_option_stack *mips_opts_stack;
16427
16428/* Handle the .set pseudo-op. */
16429
16430static void
17a2f251 16431s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16432{
16433 char *name = input_line_pointer, ch;
c6278170 16434 const struct mips_ase *ase;
252b5132
RH
16435
16436 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16437 ++input_line_pointer;
252b5132
RH
16438 ch = *input_line_pointer;
16439 *input_line_pointer = '\0';
16440
16441 if (strcmp (name, "reorder") == 0)
16442 {
7d10b47d
RS
16443 if (mips_opts.noreorder)
16444 end_noreorder ();
252b5132
RH
16445 }
16446 else if (strcmp (name, "noreorder") == 0)
16447 {
7d10b47d
RS
16448 if (!mips_opts.noreorder)
16449 start_noreorder ();
252b5132 16450 }
741fe287
MR
16451 else if (strncmp (name, "at=", 3) == 0)
16452 {
16453 char *s = name + 3;
16454
16455 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16456 as_bad (_("Unrecognized register name `%s'"), s);
16457 }
252b5132
RH
16458 else if (strcmp (name, "at") == 0)
16459 {
741fe287 16460 mips_opts.at = ATREG;
252b5132
RH
16461 }
16462 else if (strcmp (name, "noat") == 0)
16463 {
741fe287 16464 mips_opts.at = ZERO;
252b5132
RH
16465 }
16466 else if (strcmp (name, "macro") == 0)
16467 {
16468 mips_opts.warn_about_macros = 0;
16469 }
16470 else if (strcmp (name, "nomacro") == 0)
16471 {
16472 if (mips_opts.noreorder == 0)
16473 as_bad (_("`noreorder' must be set before `nomacro'"));
16474 mips_opts.warn_about_macros = 1;
16475 }
16476 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16477 {
16478 mips_opts.nomove = 0;
16479 }
16480 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16481 {
16482 mips_opts.nomove = 1;
16483 }
16484 else if (strcmp (name, "bopt") == 0)
16485 {
16486 mips_opts.nobopt = 0;
16487 }
16488 else if (strcmp (name, "nobopt") == 0)
16489 {
16490 mips_opts.nobopt = 1;
16491 }
ad3fea08
TS
16492 else if (strcmp (name, "gp=default") == 0)
16493 mips_opts.gp32 = file_mips_gp32;
16494 else if (strcmp (name, "gp=32") == 0)
16495 mips_opts.gp32 = 1;
16496 else if (strcmp (name, "gp=64") == 0)
16497 {
16498 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16499 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16500 mips_cpu_info_from_isa (mips_opts.isa)->name);
16501 mips_opts.gp32 = 0;
16502 }
16503 else if (strcmp (name, "fp=default") == 0)
16504 mips_opts.fp32 = file_mips_fp32;
16505 else if (strcmp (name, "fp=32") == 0)
16506 mips_opts.fp32 = 1;
16507 else if (strcmp (name, "fp=64") == 0)
16508 {
16509 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16510 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16511 mips_cpu_info_from_isa (mips_opts.isa)->name);
16512 mips_opts.fp32 = 0;
16513 }
037b32b9
AN
16514 else if (strcmp (name, "softfloat") == 0)
16515 mips_opts.soft_float = 1;
16516 else if (strcmp (name, "hardfloat") == 0)
16517 mips_opts.soft_float = 0;
16518 else if (strcmp (name, "singlefloat") == 0)
16519 mips_opts.single_float = 1;
16520 else if (strcmp (name, "doublefloat") == 0)
16521 mips_opts.single_float = 0;
252b5132
RH
16522 else if (strcmp (name, "mips16") == 0
16523 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16524 {
16525 if (mips_opts.micromips == 1)
16526 as_fatal (_("`mips16' cannot be used with `micromips'"));
16527 mips_opts.mips16 = 1;
16528 }
252b5132
RH
16529 else if (strcmp (name, "nomips16") == 0
16530 || strcmp (name, "noMIPS-16") == 0)
16531 mips_opts.mips16 = 0;
df58fc94
RS
16532 else if (strcmp (name, "micromips") == 0)
16533 {
16534 if (mips_opts.mips16 == 1)
16535 as_fatal (_("`micromips' cannot be used with `mips16'"));
16536 mips_opts.micromips = 1;
16537 }
16538 else if (strcmp (name, "nomicromips") == 0)
16539 mips_opts.micromips = 0;
c6278170
RS
16540 else if (name[0] == 'n'
16541 && name[1] == 'o'
16542 && (ase = mips_lookup_ase (name + 2)))
16543 mips_set_ase (ase, FALSE);
16544 else if ((ase = mips_lookup_ase (name)))
16545 mips_set_ase (ase, TRUE);
1a2c1fad 16546 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16547 {
af7ee8bf 16548 int reset = 0;
252b5132 16549
1a2c1fad
CD
16550 /* Permit the user to change the ISA and architecture on the fly.
16551 Needless to say, misuse can cause serious problems. */
81a21e38 16552 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16553 {
16554 reset = 1;
16555 mips_opts.isa = file_mips_isa;
1a2c1fad 16556 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16557 }
16558 else if (strncmp (name, "arch=", 5) == 0)
16559 {
16560 const struct mips_cpu_info *p;
16561
16562 p = mips_parse_cpu("internal use", name + 5);
16563 if (!p)
16564 as_bad (_("unknown architecture %s"), name + 5);
16565 else
16566 {
16567 mips_opts.arch = p->cpu;
16568 mips_opts.isa = p->isa;
16569 }
16570 }
81a21e38
TS
16571 else if (strncmp (name, "mips", 4) == 0)
16572 {
16573 const struct mips_cpu_info *p;
16574
16575 p = mips_parse_cpu("internal use", name);
16576 if (!p)
16577 as_bad (_("unknown ISA level %s"), name + 4);
16578 else
16579 {
16580 mips_opts.arch = p->cpu;
16581 mips_opts.isa = p->isa;
16582 }
16583 }
af7ee8bf 16584 else
81a21e38 16585 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16586
16587 switch (mips_opts.isa)
98d3f06f
KH
16588 {
16589 case 0:
98d3f06f 16590 break;
af7ee8bf
CD
16591 case ISA_MIPS1:
16592 case ISA_MIPS2:
16593 case ISA_MIPS32:
16594 case ISA_MIPS32R2:
98d3f06f
KH
16595 mips_opts.gp32 = 1;
16596 mips_opts.fp32 = 1;
16597 break;
af7ee8bf
CD
16598 case ISA_MIPS3:
16599 case ISA_MIPS4:
16600 case ISA_MIPS5:
16601 case ISA_MIPS64:
5f74bc13 16602 case ISA_MIPS64R2:
98d3f06f 16603 mips_opts.gp32 = 0;
e407c74b
NC
16604 if (mips_opts.arch == CPU_R5900)
16605 {
16606 mips_opts.fp32 = 1;
16607 }
16608 else
16609 {
98d3f06f 16610 mips_opts.fp32 = 0;
e407c74b 16611 }
98d3f06f
KH
16612 break;
16613 default:
16614 as_bad (_("unknown ISA level %s"), name + 4);
16615 break;
16616 }
af7ee8bf 16617 if (reset)
98d3f06f 16618 {
af7ee8bf
CD
16619 mips_opts.gp32 = file_mips_gp32;
16620 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16621 }
252b5132
RH
16622 }
16623 else if (strcmp (name, "autoextend") == 0)
16624 mips_opts.noautoextend = 0;
16625 else if (strcmp (name, "noautoextend") == 0)
16626 mips_opts.noautoextend = 1;
833794fc
MR
16627 else if (strcmp (name, "insn32") == 0)
16628 mips_opts.insn32 = TRUE;
16629 else if (strcmp (name, "noinsn32") == 0)
16630 mips_opts.insn32 = FALSE;
252b5132
RH
16631 else if (strcmp (name, "push") == 0)
16632 {
16633 struct mips_option_stack *s;
16634
16635 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16636 s->next = mips_opts_stack;
16637 s->options = mips_opts;
16638 mips_opts_stack = s;
16639 }
16640 else if (strcmp (name, "pop") == 0)
16641 {
16642 struct mips_option_stack *s;
16643
16644 s = mips_opts_stack;
16645 if (s == NULL)
16646 as_bad (_(".set pop with no .set push"));
16647 else
16648 {
16649 /* If we're changing the reorder mode we need to handle
16650 delay slots correctly. */
16651 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16652 start_noreorder ();
252b5132 16653 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16654 end_noreorder ();
252b5132
RH
16655
16656 mips_opts = s->options;
16657 mips_opts_stack = s->next;
16658 free (s);
16659 }
16660 }
aed1a261
RS
16661 else if (strcmp (name, "sym32") == 0)
16662 mips_opts.sym32 = TRUE;
16663 else if (strcmp (name, "nosym32") == 0)
16664 mips_opts.sym32 = FALSE;
e6559e01
JM
16665 else if (strchr (name, ','))
16666 {
16667 /* Generic ".set" directive; use the generic handler. */
16668 *input_line_pointer = ch;
16669 input_line_pointer = name;
16670 s_set (0);
16671 return;
16672 }
252b5132
RH
16673 else
16674 {
16675 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16676 }
c6278170 16677 mips_check_isa_supports_ases ();
252b5132
RH
16678 *input_line_pointer = ch;
16679 demand_empty_rest_of_line ();
16680}
16681
16682/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16683 .option pic2. It means to generate SVR4 PIC calls. */
16684
16685static void
17a2f251 16686s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16687{
16688 mips_pic = SVR4_PIC;
143d77c5 16689 mips_abicalls = TRUE;
4d0d148d
TS
16690
16691 if (g_switch_seen && g_switch_value != 0)
16692 as_warn (_("-G may not be used with SVR4 PIC code"));
16693 g_switch_value = 0;
16694
252b5132
RH
16695 bfd_set_gp_size (stdoutput, 0);
16696 demand_empty_rest_of_line ();
16697}
16698
16699/* Handle the .cpload pseudo-op. This is used when generating SVR4
16700 PIC code. It sets the $gp register for the function based on the
16701 function address, which is in the register named in the argument.
16702 This uses a relocation against _gp_disp, which is handled specially
16703 by the linker. The result is:
16704 lui $gp,%hi(_gp_disp)
16705 addiu $gp,$gp,%lo(_gp_disp)
16706 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16707 The .cpload argument is normally $25 == $t9.
16708
16709 The -mno-shared option changes this to:
bbe506e8
TS
16710 lui $gp,%hi(__gnu_local_gp)
16711 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16712 and the argument is ignored. This saves an instruction, but the
16713 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16714 address for __gnu_local_gp. Thus code assembled with -mno-shared
16715 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16716
16717static void
17a2f251 16718s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16719{
16720 expressionS ex;
aa6975fb
ILT
16721 int reg;
16722 int in_shared;
252b5132 16723
6478892d
TS
16724 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16725 .cpload is ignored. */
16726 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16727 {
16728 s_ignore (0);
16729 return;
16730 }
16731
a276b80c
MR
16732 if (mips_opts.mips16)
16733 {
16734 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16735 ignore_rest_of_line ();
16736 return;
16737 }
16738
d3ecfc59 16739 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16740 if (mips_opts.noreorder == 0)
16741 as_warn (_(".cpload not in noreorder section"));
16742
aa6975fb
ILT
16743 reg = tc_get_register (0);
16744
16745 /* If we need to produce a 64-bit address, we are better off using
16746 the default instruction sequence. */
aed1a261 16747 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16748
252b5132 16749 ex.X_op = O_symbol;
bbe506e8
TS
16750 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16751 "__gnu_local_gp");
252b5132
RH
16752 ex.X_op_symbol = NULL;
16753 ex.X_add_number = 0;
16754
16755 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16756 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16757
8a75745d
MR
16758 mips_mark_labels ();
16759 mips_assembling_insn = TRUE;
16760
584892a6 16761 macro_start ();
67c0d1eb
RS
16762 macro_build_lui (&ex, mips_gp_register);
16763 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16764 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16765 if (in_shared)
16766 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16767 mips_gp_register, reg);
584892a6 16768 macro_end ();
252b5132 16769
8a75745d 16770 mips_assembling_insn = FALSE;
252b5132
RH
16771 demand_empty_rest_of_line ();
16772}
16773
6478892d
TS
16774/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16775 .cpsetup $reg1, offset|$reg2, label
16776
16777 If offset is given, this results in:
16778 sd $gp, offset($sp)
956cd1d6 16779 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16780 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16781 daddu $gp, $gp, $reg1
6478892d
TS
16782
16783 If $reg2 is given, this results in:
16784 daddu $reg2, $gp, $0
956cd1d6 16785 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16786 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16787 daddu $gp, $gp, $reg1
aa6975fb
ILT
16788 $reg1 is normally $25 == $t9.
16789
16790 The -mno-shared option replaces the last three instructions with
16791 lui $gp,%hi(_gp)
54f4ddb3 16792 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16793
6478892d 16794static void
17a2f251 16795s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16796{
16797 expressionS ex_off;
16798 expressionS ex_sym;
16799 int reg1;
6478892d 16800
8586fc66 16801 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16802 We also need NewABI support. */
16803 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16804 {
16805 s_ignore (0);
16806 return;
16807 }
16808
a276b80c
MR
16809 if (mips_opts.mips16)
16810 {
16811 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16812 ignore_rest_of_line ();
16813 return;
16814 }
16815
6478892d
TS
16816 reg1 = tc_get_register (0);
16817 SKIP_WHITESPACE ();
16818 if (*input_line_pointer != ',')
16819 {
16820 as_bad (_("missing argument separator ',' for .cpsetup"));
16821 return;
16822 }
16823 else
80245285 16824 ++input_line_pointer;
6478892d
TS
16825 SKIP_WHITESPACE ();
16826 if (*input_line_pointer == '$')
80245285
TS
16827 {
16828 mips_cpreturn_register = tc_get_register (0);
16829 mips_cpreturn_offset = -1;
16830 }
6478892d 16831 else
80245285
TS
16832 {
16833 mips_cpreturn_offset = get_absolute_expression ();
16834 mips_cpreturn_register = -1;
16835 }
6478892d
TS
16836 SKIP_WHITESPACE ();
16837 if (*input_line_pointer != ',')
16838 {
16839 as_bad (_("missing argument separator ',' for .cpsetup"));
16840 return;
16841 }
16842 else
f9419b05 16843 ++input_line_pointer;
6478892d 16844 SKIP_WHITESPACE ();
f21f8242 16845 expression (&ex_sym);
6478892d 16846
8a75745d
MR
16847 mips_mark_labels ();
16848 mips_assembling_insn = TRUE;
16849
584892a6 16850 macro_start ();
6478892d
TS
16851 if (mips_cpreturn_register == -1)
16852 {
16853 ex_off.X_op = O_constant;
16854 ex_off.X_add_symbol = NULL;
16855 ex_off.X_op_symbol = NULL;
16856 ex_off.X_add_number = mips_cpreturn_offset;
16857
67c0d1eb 16858 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16859 BFD_RELOC_LO16, SP);
6478892d
TS
16860 }
16861 else
67c0d1eb 16862 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16863 mips_gp_register, 0);
6478892d 16864
aed1a261 16865 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16866 {
df58fc94 16867 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16868 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16869 BFD_RELOC_HI16_S);
16870
16871 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16872 mips_gp_register, -1, BFD_RELOC_GPREL16,
16873 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16874
16875 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16876 mips_gp_register, reg1);
16877 }
16878 else
16879 {
16880 expressionS ex;
16881
16882 ex.X_op = O_symbol;
4184909a 16883 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16884 ex.X_op_symbol = NULL;
16885 ex.X_add_number = 0;
6e1304d8 16886
aa6975fb
ILT
16887 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16888 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16889
16890 macro_build_lui (&ex, mips_gp_register);
16891 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16892 mips_gp_register, BFD_RELOC_LO16);
16893 }
f21f8242 16894
584892a6 16895 macro_end ();
6478892d 16896
8a75745d 16897 mips_assembling_insn = FALSE;
6478892d
TS
16898 demand_empty_rest_of_line ();
16899}
16900
16901static void
17a2f251 16902s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16903{
16904 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16905 .cplocal is ignored. */
6478892d
TS
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"), ".cplocal");
16915 ignore_rest_of_line ();
16916 return;
16917 }
16918
6478892d 16919 mips_gp_register = tc_get_register (0);
85b51719 16920 demand_empty_rest_of_line ();
6478892d
TS
16921}
16922
252b5132
RH
16923/* Handle the .cprestore pseudo-op. This stores $gp into a given
16924 offset from $sp. The offset is remembered, and after making a PIC
16925 call $gp is restored from that location. */
16926
16927static void
17a2f251 16928s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16929{
16930 expressionS ex;
252b5132 16931
6478892d 16932 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16933 .cprestore is ignored. */
6478892d 16934 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16935 {
16936 s_ignore (0);
16937 return;
16938 }
16939
a276b80c
MR
16940 if (mips_opts.mips16)
16941 {
16942 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16943 ignore_rest_of_line ();
16944 return;
16945 }
16946
252b5132 16947 mips_cprestore_offset = get_absolute_expression ();
7a621144 16948 mips_cprestore_valid = 1;
252b5132
RH
16949
16950 ex.X_op = O_constant;
16951 ex.X_add_symbol = NULL;
16952 ex.X_op_symbol = NULL;
16953 ex.X_add_number = mips_cprestore_offset;
16954
8a75745d
MR
16955 mips_mark_labels ();
16956 mips_assembling_insn = TRUE;
16957
584892a6 16958 macro_start ();
67c0d1eb
RS
16959 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16960 SP, HAVE_64BIT_ADDRESSES);
584892a6 16961 macro_end ();
252b5132 16962
8a75745d 16963 mips_assembling_insn = FALSE;
252b5132
RH
16964 demand_empty_rest_of_line ();
16965}
16966
6478892d 16967/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16968 was given in the preceding .cpsetup, it results in:
6478892d 16969 ld $gp, offset($sp)
76b3015f 16970
6478892d 16971 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16972 daddu $gp, $reg2, $0 */
16973
6478892d 16974static void
17a2f251 16975s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16976{
16977 expressionS ex;
6478892d
TS
16978
16979 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16980 We also need NewABI support. */
16981 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16982 {
16983 s_ignore (0);
16984 return;
16985 }
16986
a276b80c
MR
16987 if (mips_opts.mips16)
16988 {
16989 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16990 ignore_rest_of_line ();
16991 return;
16992 }
16993
8a75745d
MR
16994 mips_mark_labels ();
16995 mips_assembling_insn = TRUE;
16996
584892a6 16997 macro_start ();
6478892d
TS
16998 if (mips_cpreturn_register == -1)
16999 {
17000 ex.X_op = O_constant;
17001 ex.X_add_symbol = NULL;
17002 ex.X_op_symbol = NULL;
17003 ex.X_add_number = mips_cpreturn_offset;
17004
67c0d1eb 17005 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17006 }
17007 else
67c0d1eb 17008 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 17009 mips_cpreturn_register, 0);
584892a6 17010 macro_end ();
6478892d 17011
8a75745d 17012 mips_assembling_insn = FALSE;
6478892d
TS
17013 demand_empty_rest_of_line ();
17014}
17015
d0f13682
CLT
17016/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17017 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17018 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17019 debug information or MIPS16 TLS. */
741d6ea8
JM
17020
17021static void
d0f13682
CLT
17022s_tls_rel_directive (const size_t bytes, const char *dirstr,
17023 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17024{
17025 expressionS ex;
17026 char *p;
17027
17028 expression (&ex);
17029
17030 if (ex.X_op != O_symbol)
17031 {
d0f13682 17032 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
17033 ignore_rest_of_line ();
17034 }
17035
17036 p = frag_more (bytes);
17037 md_number_to_chars (p, 0, bytes);
d0f13682 17038 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17039 demand_empty_rest_of_line ();
de64cffd 17040 mips_clear_insn_labels ();
741d6ea8
JM
17041}
17042
17043/* Handle .dtprelword. */
17044
17045static void
17046s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17047{
d0f13682 17048 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17049}
17050
17051/* Handle .dtpreldword. */
17052
17053static void
17054s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17055{
d0f13682
CLT
17056 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17057}
17058
17059/* Handle .tprelword. */
17060
17061static void
17062s_tprelword (int ignore ATTRIBUTE_UNUSED)
17063{
17064 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17065}
17066
17067/* Handle .tpreldword. */
17068
17069static void
17070s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17071{
17072 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17073}
17074
6478892d
TS
17075/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17076 code. It sets the offset to use in gp_rel relocations. */
17077
17078static void
17a2f251 17079s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17080{
17081 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17082 We also need NewABI support. */
17083 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17084 {
17085 s_ignore (0);
17086 return;
17087 }
17088
def2e0dd 17089 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17090
17091 demand_empty_rest_of_line ();
17092}
17093
252b5132
RH
17094/* Handle the .gpword pseudo-op. This is used when generating PIC
17095 code. It generates a 32 bit GP relative reloc. */
17096
17097static void
17a2f251 17098s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17099{
a8dbcb85
TS
17100 segment_info_type *si;
17101 struct insn_label_list *l;
252b5132
RH
17102 expressionS ex;
17103 char *p;
17104
17105 /* When not generating PIC code, this is treated as .word. */
17106 if (mips_pic != SVR4_PIC)
17107 {
17108 s_cons (2);
17109 return;
17110 }
17111
a8dbcb85
TS
17112 si = seg_info (now_seg);
17113 l = si->label_list;
7d10b47d 17114 mips_emit_delays ();
252b5132 17115 if (auto_align)
462427c4 17116 mips_align (2, 0, l);
252b5132
RH
17117
17118 expression (&ex);
a1facbec 17119 mips_clear_insn_labels ();
252b5132
RH
17120
17121 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17122 {
17123 as_bad (_("Unsupported use of .gpword"));
17124 ignore_rest_of_line ();
17125 }
17126
17127 p = frag_more (4);
17a2f251 17128 md_number_to_chars (p, 0, 4);
b34976b6 17129 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17130 BFD_RELOC_GPREL32);
252b5132
RH
17131
17132 demand_empty_rest_of_line ();
17133}
17134
10181a0d 17135static void
17a2f251 17136s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17137{
a8dbcb85
TS
17138 segment_info_type *si;
17139 struct insn_label_list *l;
10181a0d
AO
17140 expressionS ex;
17141 char *p;
17142
17143 /* When not generating PIC code, this is treated as .dword. */
17144 if (mips_pic != SVR4_PIC)
17145 {
17146 s_cons (3);
17147 return;
17148 }
17149
a8dbcb85
TS
17150 si = seg_info (now_seg);
17151 l = si->label_list;
7d10b47d 17152 mips_emit_delays ();
10181a0d 17153 if (auto_align)
462427c4 17154 mips_align (3, 0, l);
10181a0d
AO
17155
17156 expression (&ex);
a1facbec 17157 mips_clear_insn_labels ();
10181a0d
AO
17158
17159 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17160 {
17161 as_bad (_("Unsupported use of .gpdword"));
17162 ignore_rest_of_line ();
17163 }
17164
17165 p = frag_more (8);
17a2f251 17166 md_number_to_chars (p, 0, 8);
a105a300 17167 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17168 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17169
17170 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17171 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17172 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17173
17174 demand_empty_rest_of_line ();
17175}
17176
a3f278e2
CM
17177/* Handle the .ehword pseudo-op. This is used when generating unwinding
17178 tables. It generates a R_MIPS_EH reloc. */
17179
17180static void
17181s_ehword (int ignore ATTRIBUTE_UNUSED)
17182{
17183 expressionS ex;
17184 char *p;
17185
17186 mips_emit_delays ();
17187
17188 expression (&ex);
17189 mips_clear_insn_labels ();
17190
17191 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17192 {
17193 as_bad (_("Unsupported use of .ehword"));
17194 ignore_rest_of_line ();
17195 }
17196
17197 p = frag_more (4);
17198 md_number_to_chars (p, 0, 4);
17199 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17200 BFD_RELOC_MIPS_EH);
17201
17202 demand_empty_rest_of_line ();
17203}
17204
252b5132
RH
17205/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17206 tables in SVR4 PIC code. */
17207
17208static void
17a2f251 17209s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17210{
252b5132
RH
17211 int reg;
17212
10181a0d
AO
17213 /* This is ignored when not generating SVR4 PIC code. */
17214 if (mips_pic != SVR4_PIC)
252b5132
RH
17215 {
17216 s_ignore (0);
17217 return;
17218 }
17219
8a75745d
MR
17220 mips_mark_labels ();
17221 mips_assembling_insn = TRUE;
17222
252b5132 17223 /* Add $gp to the register named as an argument. */
584892a6 17224 macro_start ();
252b5132 17225 reg = tc_get_register (0);
67c0d1eb 17226 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17227 macro_end ();
252b5132 17228
8a75745d 17229 mips_assembling_insn = FALSE;
bdaaa2e1 17230 demand_empty_rest_of_line ();
252b5132
RH
17231}
17232
17233/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17234 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17235 such as generating jalx instructions when needed. We also make
17236 them odd for the duration of the assembly, in order to generate the
17237 right sort of code. We will make them even in the adjust_symtab
17238 routine, while leaving them marked. This is convenient for the
17239 debugger and the disassembler. The linker knows to make them odd
17240 again. */
17241
17242static void
17a2f251 17243s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17244{
df58fc94 17245 mips_mark_labels ();
252b5132
RH
17246
17247 demand_empty_rest_of_line ();
17248}
17249
754e2bb9
RS
17250/* Handle a .stab[snd] directive. Ideally these directives would be
17251 implemented in a transparent way, so that removing them would not
17252 have any effect on the generated instructions. However, s_stab
17253 internally changes the section, so in practice we need to decide
17254 now whether the preceding label marks compressed code. We do not
17255 support changing the compression mode of a label after a .stab*
17256 directive, such as in:
17257
17258 foo:
17259 .stabs ...
17260 .set mips16
17261
17262 so the current mode wins. */
252b5132
RH
17263
17264static void
17a2f251 17265s_mips_stab (int type)
252b5132 17266{
754e2bb9 17267 mips_mark_labels ();
252b5132
RH
17268 s_stab (type);
17269}
17270
54f4ddb3 17271/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17272
17273static void
17a2f251 17274s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17275{
17276 char *name;
17277 int c;
17278 symbolS *symbolP;
17279 expressionS exp;
17280
17281 name = input_line_pointer;
17282 c = get_symbol_end ();
17283 symbolP = symbol_find_or_make (name);
17284 S_SET_WEAK (symbolP);
17285 *input_line_pointer = c;
17286
17287 SKIP_WHITESPACE ();
17288
17289 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17290 {
17291 if (S_IS_DEFINED (symbolP))
17292 {
20203fb9 17293 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17294 S_GET_NAME (symbolP));
17295 ignore_rest_of_line ();
17296 return;
17297 }
bdaaa2e1 17298
252b5132
RH
17299 if (*input_line_pointer == ',')
17300 {
17301 ++input_line_pointer;
17302 SKIP_WHITESPACE ();
17303 }
bdaaa2e1 17304
252b5132
RH
17305 expression (&exp);
17306 if (exp.X_op != O_symbol)
17307 {
20203fb9 17308 as_bad (_("bad .weakext directive"));
98d3f06f 17309 ignore_rest_of_line ();
252b5132
RH
17310 return;
17311 }
49309057 17312 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17313 }
17314
17315 demand_empty_rest_of_line ();
17316}
17317
17318/* Parse a register string into a number. Called from the ECOFF code
17319 to parse .frame. The argument is non-zero if this is the frame
17320 register, so that we can record it in mips_frame_reg. */
17321
17322int
17a2f251 17323tc_get_register (int frame)
252b5132 17324{
707bfff6 17325 unsigned int reg;
252b5132
RH
17326
17327 SKIP_WHITESPACE ();
707bfff6
TS
17328 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17329 reg = 0;
252b5132 17330 if (frame)
7a621144
DJ
17331 {
17332 mips_frame_reg = reg != 0 ? reg : SP;
17333 mips_frame_reg_valid = 1;
17334 mips_cprestore_valid = 0;
17335 }
252b5132
RH
17336 return reg;
17337}
17338
17339valueT
17a2f251 17340md_section_align (asection *seg, valueT addr)
252b5132
RH
17341{
17342 int align = bfd_get_section_alignment (stdoutput, seg);
17343
f3ded42a
RS
17344 /* We don't need to align ELF sections to the full alignment.
17345 However, Irix 5 may prefer that we align them at least to a 16
17346 byte boundary. We don't bother to align the sections if we
17347 are targeted for an embedded system. */
17348 if (strncmp (TARGET_OS, "elf", 3) == 0)
17349 return addr;
17350 if (align > 4)
17351 align = 4;
252b5132
RH
17352
17353 return ((addr + (1 << align) - 1) & (-1 << align));
17354}
17355
17356/* Utility routine, called from above as well. If called while the
17357 input file is still being read, it's only an approximation. (For
17358 example, a symbol may later become defined which appeared to be
17359 undefined earlier.) */
17360
17361static int
17a2f251 17362nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17363{
17364 if (sym == 0)
17365 return 0;
17366
4d0d148d 17367 if (g_switch_value > 0)
252b5132
RH
17368 {
17369 const char *symname;
17370 int change;
17371
c9914766 17372 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17373 register. It can be if it is smaller than the -G size or if
17374 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17375 not be referenced off the $gp, although it appears as though
252b5132
RH
17376 they can. */
17377 symname = S_GET_NAME (sym);
17378 if (symname != (const char *) NULL
17379 && (strcmp (symname, "eprol") == 0
17380 || strcmp (symname, "etext") == 0
17381 || strcmp (symname, "_gp") == 0
17382 || strcmp (symname, "edata") == 0
17383 || strcmp (symname, "_fbss") == 0
17384 || strcmp (symname, "_fdata") == 0
17385 || strcmp (symname, "_ftext") == 0
17386 || strcmp (symname, "end") == 0
17387 || strcmp (symname, "_gp_disp") == 0))
17388 change = 1;
17389 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17390 && (0
17391#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17392 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17393 && (symbol_get_obj (sym)->ecoff_extern_size
17394 <= g_switch_value))
252b5132
RH
17395#endif
17396 /* We must defer this decision until after the whole
17397 file has been read, since there might be a .extern
17398 after the first use of this symbol. */
17399 || (before_relaxing
17400#ifndef NO_ECOFF_DEBUGGING
49309057 17401 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17402#endif
17403 && S_GET_VALUE (sym) == 0)
17404 || (S_GET_VALUE (sym) != 0
17405 && S_GET_VALUE (sym) <= g_switch_value)))
17406 change = 0;
17407 else
17408 {
17409 const char *segname;
17410
17411 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17412 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17413 && strcmp (segname, ".lit4") != 0);
17414 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17415 && strcmp (segname, ".sbss") != 0
17416 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17417 && strncmp (segname, ".sbss.", 6) != 0
17418 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17419 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17420 }
17421 return change;
17422 }
17423 else
c9914766 17424 /* We are not optimizing for the $gp register. */
252b5132
RH
17425 return 1;
17426}
17427
5919d012
RS
17428
17429/* Return true if the given symbol should be considered local for SVR4 PIC. */
17430
17431static bfd_boolean
17a2f251 17432pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17433{
17434 asection *symsec;
5919d012
RS
17435
17436 /* Handle the case of a symbol equated to another symbol. */
17437 while (symbol_equated_reloc_p (sym))
17438 {
17439 symbolS *n;
17440
5f0fe04b 17441 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17442 n = symbol_get_value_expression (sym)->X_add_symbol;
17443 if (n == sym)
17444 break;
17445 sym = n;
17446 }
17447
df1f3cda
DD
17448 if (symbol_section_p (sym))
17449 return TRUE;
17450
5919d012
RS
17451 symsec = S_GET_SEGMENT (sym);
17452
5919d012 17453 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17454 return (!bfd_is_und_section (symsec)
17455 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17456 && !bfd_is_com_section (symsec)
17457 && !s_is_linkonce (sym, segtype)
5919d012 17458 /* A global or weak symbol is treated as external. */
f3ded42a 17459 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
17460}
17461
17462
252b5132
RH
17463/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17464 extended opcode. SEC is the section the frag is in. */
17465
17466static int
17a2f251 17467mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17468{
17469 int type;
3994f87e 17470 const struct mips16_immed_operand *op;
252b5132
RH
17471 offsetT val;
17472 int mintiny, maxtiny;
17473 segT symsec;
98aa84af 17474 fragS *sym_frag;
252b5132
RH
17475
17476 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17477 return 0;
17478 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17479 return 1;
17480
17481 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17482 op = mips16_immed_operands;
17483 while (op->type != type)
17484 {
17485 ++op;
9c2799c2 17486 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17487 }
17488
17489 if (op->unsp)
17490 {
17491 if (type == '<' || type == '>' || type == '[' || type == ']')
17492 {
17493 mintiny = 1;
17494 maxtiny = 1 << op->nbits;
17495 }
17496 else
17497 {
17498 mintiny = 0;
17499 maxtiny = (1 << op->nbits) - 1;
17500 }
17501 }
17502 else
17503 {
17504 mintiny = - (1 << (op->nbits - 1));
17505 maxtiny = (1 << (op->nbits - 1)) - 1;
17506 }
17507
98aa84af 17508 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17509 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17510 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17511
17512 if (op->pcrel)
17513 {
17514 addressT addr;
17515
17516 /* We won't have the section when we are called from
17517 mips_relax_frag. However, we will always have been called
17518 from md_estimate_size_before_relax first. If this is a
17519 branch to a different section, we mark it as such. If SEC is
17520 NULL, and the frag is not marked, then it must be a branch to
17521 the same section. */
17522 if (sec == NULL)
17523 {
17524 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17525 return 1;
17526 }
17527 else
17528 {
98aa84af 17529 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17530 if (symsec != sec)
17531 {
17532 fragp->fr_subtype =
17533 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17534
17535 /* FIXME: We should support this, and let the linker
17536 catch branches and loads that are out of range. */
17537 as_bad_where (fragp->fr_file, fragp->fr_line,
17538 _("unsupported PC relative reference to different section"));
17539
17540 return 1;
17541 }
98aa84af
AM
17542 if (fragp != sym_frag && sym_frag->fr_address == 0)
17543 /* Assume non-extended on the first relaxation pass.
17544 The address we have calculated will be bogus if this is
17545 a forward branch to another frag, as the forward frag
17546 will have fr_address == 0. */
17547 return 0;
252b5132
RH
17548 }
17549
17550 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17551 the same section. If the relax_marker of the symbol fragment
17552 differs from the relax_marker of this fragment, we have not
17553 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17554 in STRETCH in order to get a better estimate of the address.
17555 This particularly matters because of the shift bits. */
17556 if (stretch != 0
98aa84af 17557 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17558 {
17559 fragS *f;
17560
17561 /* Adjust stretch for any alignment frag. Note that if have
17562 been expanding the earlier code, the symbol may be
17563 defined in what appears to be an earlier frag. FIXME:
17564 This doesn't handle the fr_subtype field, which specifies
17565 a maximum number of bytes to skip when doing an
17566 alignment. */
98aa84af 17567 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17568 {
17569 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17570 {
17571 if (stretch < 0)
17572 stretch = - ((- stretch)
17573 & ~ ((1 << (int) f->fr_offset) - 1));
17574 else
17575 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17576 if (stretch == 0)
17577 break;
17578 }
17579 }
17580 if (f != NULL)
17581 val += stretch;
17582 }
17583
17584 addr = fragp->fr_address + fragp->fr_fix;
17585
17586 /* The base address rules are complicated. The base address of
17587 a branch is the following instruction. The base address of a
17588 PC relative load or add is the instruction itself, but if it
17589 is in a delay slot (in which case it can not be extended) use
17590 the address of the instruction whose delay slot it is in. */
17591 if (type == 'p' || type == 'q')
17592 {
17593 addr += 2;
17594
17595 /* If we are currently assuming that this frag should be
17596 extended, then, the current address is two bytes
bdaaa2e1 17597 higher. */
252b5132
RH
17598 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17599 addr += 2;
17600
17601 /* Ignore the low bit in the target, since it will be set
17602 for a text label. */
17603 if ((val & 1) != 0)
17604 --val;
17605 }
17606 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17607 addr -= 4;
17608 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17609 addr -= 2;
17610
17611 val -= addr & ~ ((1 << op->shift) - 1);
17612
17613 /* Branch offsets have an implicit 0 in the lowest bit. */
17614 if (type == 'p' || type == 'q')
17615 val /= 2;
17616
17617 /* If any of the shifted bits are set, we must use an extended
17618 opcode. If the address depends on the size of this
17619 instruction, this can lead to a loop, so we arrange to always
17620 use an extended opcode. We only check this when we are in
17621 the main relaxation loop, when SEC is NULL. */
17622 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17623 {
17624 fragp->fr_subtype =
17625 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17626 return 1;
17627 }
17628
17629 /* If we are about to mark a frag as extended because the value
17630 is precisely maxtiny + 1, then there is a chance of an
17631 infinite loop as in the following code:
17632 la $4,foo
17633 .skip 1020
17634 .align 2
17635 foo:
17636 In this case when the la is extended, foo is 0x3fc bytes
17637 away, so the la can be shrunk, but then foo is 0x400 away, so
17638 the la must be extended. To avoid this loop, we mark the
17639 frag as extended if it was small, and is about to become
17640 extended with a value of maxtiny + 1. */
17641 if (val == ((maxtiny + 1) << op->shift)
17642 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17643 && sec == NULL)
17644 {
17645 fragp->fr_subtype =
17646 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17647 return 1;
17648 }
17649 }
17650 else if (symsec != absolute_section && sec != NULL)
17651 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17652
17653 if ((val & ((1 << op->shift) - 1)) != 0
17654 || val < (mintiny << op->shift)
17655 || val > (maxtiny << op->shift))
17656 return 1;
17657 else
17658 return 0;
17659}
17660
4a6a3df4
AO
17661/* Compute the length of a branch sequence, and adjust the
17662 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17663 worst-case length is computed, with UPDATE being used to indicate
17664 whether an unconditional (-1), branch-likely (+1) or regular (0)
17665 branch is to be computed. */
17666static int
17a2f251 17667relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17668{
b34976b6 17669 bfd_boolean toofar;
4a6a3df4
AO
17670 int length;
17671
17672 if (fragp
17673 && S_IS_DEFINED (fragp->fr_symbol)
17674 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17675 {
17676 addressT addr;
17677 offsetT val;
17678
17679 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17680
17681 addr = fragp->fr_address + fragp->fr_fix + 4;
17682
17683 val -= addr;
17684
17685 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17686 }
17687 else if (fragp)
17688 /* If the symbol is not defined or it's in a different segment,
17689 assume the user knows what's going on and emit a short
17690 branch. */
b34976b6 17691 toofar = FALSE;
4a6a3df4 17692 else
b34976b6 17693 toofar = TRUE;
4a6a3df4
AO
17694
17695 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17696 fragp->fr_subtype
66b3e8da
MR
17697 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17698 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17699 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17700 RELAX_BRANCH_LINK (fragp->fr_subtype),
17701 toofar);
17702
17703 length = 4;
17704 if (toofar)
17705 {
17706 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17707 length += 8;
17708
17709 if (mips_pic != NO_PIC)
17710 {
17711 /* Additional space for PIC loading of target address. */
17712 length += 8;
17713 if (mips_opts.isa == ISA_MIPS1)
17714 /* Additional space for $at-stabilizing nop. */
17715 length += 4;
17716 }
17717
17718 /* If branch is conditional. */
17719 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17720 length += 8;
17721 }
b34976b6 17722
4a6a3df4
AO
17723 return length;
17724}
17725
df58fc94
RS
17726/* Compute the length of a branch sequence, and adjust the
17727 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17728 worst-case length is computed, with UPDATE being used to indicate
17729 whether an unconditional (-1), or regular (0) branch is to be
17730 computed. */
17731
17732static int
17733relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17734{
17735 bfd_boolean toofar;
17736 int length;
17737
17738 if (fragp
17739 && S_IS_DEFINED (fragp->fr_symbol)
17740 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17741 {
17742 addressT addr;
17743 offsetT val;
17744
17745 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17746 /* Ignore the low bit in the target, since it will be set
17747 for a text label. */
17748 if ((val & 1) != 0)
17749 --val;
17750
17751 addr = fragp->fr_address + fragp->fr_fix + 4;
17752
17753 val -= addr;
17754
17755 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17756 }
17757 else if (fragp)
17758 /* If the symbol is not defined or it's in a different segment,
17759 assume the user knows what's going on and emit a short
17760 branch. */
17761 toofar = FALSE;
17762 else
17763 toofar = TRUE;
17764
17765 if (fragp && update
17766 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17767 fragp->fr_subtype = (toofar
17768 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17769 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17770
17771 length = 4;
17772 if (toofar)
17773 {
17774 bfd_boolean compact_known = fragp != NULL;
17775 bfd_boolean compact = FALSE;
17776 bfd_boolean uncond;
17777
17778 if (compact_known)
17779 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17780 if (fragp)
17781 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17782 else
17783 uncond = update < 0;
17784
17785 /* If label is out of range, we turn branch <br>:
17786
17787 <br> label # 4 bytes
17788 0:
17789
17790 into:
17791
17792 j label # 4 bytes
17793 nop # 2 bytes if compact && !PIC
17794 0:
17795 */
17796 if (mips_pic == NO_PIC && (!compact_known || compact))
17797 length += 2;
17798
17799 /* If assembling PIC code, we further turn:
17800
17801 j label # 4 bytes
17802
17803 into:
17804
17805 lw/ld at, %got(label)(gp) # 4 bytes
17806 d/addiu at, %lo(label) # 4 bytes
17807 jr/c at # 2 bytes
17808 */
17809 if (mips_pic != NO_PIC)
17810 length += 6;
17811
17812 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17813
17814 <brneg> 0f # 4 bytes
17815 nop # 2 bytes if !compact
17816 */
17817 if (!uncond)
17818 length += (compact_known && compact) ? 4 : 6;
17819 }
17820
17821 return length;
17822}
17823
17824/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17825 bit accordingly. */
17826
17827static int
17828relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17829{
17830 bfd_boolean toofar;
17831
df58fc94
RS
17832 if (fragp
17833 && S_IS_DEFINED (fragp->fr_symbol)
17834 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17835 {
17836 addressT addr;
17837 offsetT val;
17838 int type;
17839
17840 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17841 /* Ignore the low bit in the target, since it will be set
17842 for a text label. */
17843 if ((val & 1) != 0)
17844 --val;
17845
17846 /* Assume this is a 2-byte branch. */
17847 addr = fragp->fr_address + fragp->fr_fix + 2;
17848
17849 /* We try to avoid the infinite loop by not adding 2 more bytes for
17850 long branches. */
17851
17852 val -= addr;
17853
17854 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17855 if (type == 'D')
17856 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17857 else if (type == 'E')
17858 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17859 else
17860 abort ();
17861 }
17862 else
17863 /* If the symbol is not defined or it's in a different segment,
17864 we emit a normal 32-bit branch. */
17865 toofar = TRUE;
17866
17867 if (fragp && update
17868 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17869 fragp->fr_subtype
17870 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17871 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17872
17873 if (toofar)
17874 return 4;
17875
17876 return 2;
17877}
17878
252b5132
RH
17879/* Estimate the size of a frag before relaxing. Unless this is the
17880 mips16, we are not really relaxing here, and the final size is
17881 encoded in the subtype information. For the mips16, we have to
17882 decide whether we are using an extended opcode or not. */
17883
252b5132 17884int
17a2f251 17885md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17886{
5919d012 17887 int change;
252b5132 17888
4a6a3df4
AO
17889 if (RELAX_BRANCH_P (fragp->fr_subtype))
17890 {
17891
b34976b6
AM
17892 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17893
4a6a3df4
AO
17894 return fragp->fr_var;
17895 }
17896
252b5132 17897 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17898 /* We don't want to modify the EXTENDED bit here; it might get us
17899 into infinite loops. We change it only in mips_relax_frag(). */
17900 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17901
df58fc94
RS
17902 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17903 {
17904 int length = 4;
17905
17906 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17907 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17908 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17909 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17910 fragp->fr_var = length;
17911
17912 return length;
17913 }
17914
252b5132 17915 if (mips_pic == NO_PIC)
5919d012 17916 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17917 else if (mips_pic == SVR4_PIC)
5919d012 17918 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17919 else if (mips_pic == VXWORKS_PIC)
17920 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17921 change = 0;
252b5132
RH
17922 else
17923 abort ();
17924
17925 if (change)
17926 {
4d7206a2 17927 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17928 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17929 }
4d7206a2
RS
17930 else
17931 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17932}
17933
17934/* This is called to see whether a reloc against a defined symbol
de7e6852 17935 should be converted into a reloc against a section. */
252b5132
RH
17936
17937int
17a2f251 17938mips_fix_adjustable (fixS *fixp)
252b5132 17939{
252b5132
RH
17940 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17941 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17942 return 0;
a161fe53 17943
252b5132
RH
17944 if (fixp->fx_addsy == NULL)
17945 return 1;
a161fe53 17946
de7e6852
RS
17947 /* If symbol SYM is in a mergeable section, relocations of the form
17948 SYM + 0 can usually be made section-relative. The mergeable data
17949 is then identified by the section offset rather than by the symbol.
17950
17951 However, if we're generating REL LO16 relocations, the offset is split
17952 between the LO16 and parterning high part relocation. The linker will
17953 need to recalculate the complete offset in order to correctly identify
17954 the merge data.
17955
17956 The linker has traditionally not looked for the parterning high part
17957 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17958 placed anywhere. Rather than break backwards compatibility by changing
17959 this, it seems better not to force the issue, and instead keep the
17960 original symbol. This will work with either linker behavior. */
738e5348 17961 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17962 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17963 && HAVE_IN_PLACE_ADDENDS
17964 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17965 return 0;
17966
ce70d90a 17967 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
17968 Likewise an in-range offset of limited PC-relative relocations may
17969 overflow the in-place relocatable field if recalculated against the
17970 start address of the symbol's containing section. */
ce70d90a 17971 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
17972 && (limited_pcrel_reloc_p (fixp->fx_r_type)
17973 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17974 return 0;
17975
b314ec0e
RS
17976 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17977 to a floating-point stub. The same is true for non-R_MIPS16_26
17978 relocations against MIPS16 functions; in this case, the stub becomes
17979 the function's canonical address.
17980
17981 Floating-point stubs are stored in unique .mips16.call.* or
17982 .mips16.fn.* sections. If a stub T for function F is in section S,
17983 the first relocation in section S must be against F; this is how the
17984 linker determines the target function. All relocations that might
17985 resolve to T must also be against F. We therefore have the following
17986 restrictions, which are given in an intentionally-redundant way:
17987
17988 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17989 symbols.
17990
17991 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17992 if that stub might be used.
17993
17994 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17995 symbols.
17996
17997 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17998 that stub might be used.
17999
18000 There is a further restriction:
18001
df58fc94
RS
18002 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
18003 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
18004 targets with in-place addends; the relocation field cannot
b314ec0e
RS
18005 encode the low bit.
18006
df58fc94
RS
18007 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
18008 against a MIPS16 symbol. We deal with (5) by by not reducing any
18009 such relocations on REL targets.
b314ec0e
RS
18010
18011 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18012 relocation against some symbol R, no relocation against R may be
18013 reduced. (Note that this deals with (2) as well as (1) because
18014 relocations against global symbols will never be reduced on ELF
18015 targets.) This approach is a little simpler than trying to detect
18016 stub sections, and gives the "all or nothing" per-symbol consistency
18017 that we have for MIPS16 symbols. */
f3ded42a 18018 if (fixp->fx_subsy == NULL
30c09090 18019 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
18020 || *symbol_get_tc (fixp->fx_addsy)
18021 || (HAVE_IN_PLACE_ADDENDS
18022 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
18023 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 18024 return 0;
a161fe53 18025
252b5132
RH
18026 return 1;
18027}
18028
18029/* Translate internal representation of relocation info to BFD target
18030 format. */
18031
18032arelent **
17a2f251 18033tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18034{
18035 static arelent *retval[4];
18036 arelent *reloc;
18037 bfd_reloc_code_real_type code;
18038
4b0cff4e
TS
18039 memset (retval, 0, sizeof(retval));
18040 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
18041 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18042 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18043 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18044
bad36eac
DJ
18045 if (fixp->fx_pcrel)
18046 {
df58fc94
RS
18047 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18048 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18049 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
18050 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18051 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
18052
18053 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18054 Relocations want only the symbol offset. */
18055 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
18056 }
18057 else
18058 reloc->addend = fixp->fx_addnumber;
252b5132 18059
438c16b8
TS
18060 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18061 entry to be used in the relocation's section offset. */
18062 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18063 {
18064 reloc->address = reloc->addend;
18065 reloc->addend = 0;
18066 }
18067
252b5132 18068 code = fixp->fx_r_type;
252b5132 18069
bad36eac 18070 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18071 if (reloc->howto == NULL)
18072 {
18073 as_bad_where (fixp->fx_file, fixp->fx_line,
18074 _("Can not represent %s relocation in this object file format"),
18075 bfd_get_reloc_code_name (code));
18076 retval[0] = NULL;
18077 }
18078
18079 return retval;
18080}
18081
18082/* Relax a machine dependent frag. This returns the amount by which
18083 the current size of the frag should change. */
18084
18085int
17a2f251 18086mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18087{
4a6a3df4
AO
18088 if (RELAX_BRANCH_P (fragp->fr_subtype))
18089 {
18090 offsetT old_var = fragp->fr_var;
b34976b6
AM
18091
18092 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18093
18094 return fragp->fr_var - old_var;
18095 }
18096
df58fc94
RS
18097 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18098 {
18099 offsetT old_var = fragp->fr_var;
18100 offsetT new_var = 4;
18101
18102 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18103 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18104 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18105 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18106 fragp->fr_var = new_var;
18107
18108 return new_var - old_var;
18109 }
18110
252b5132
RH
18111 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18112 return 0;
18113
c4e7957c 18114 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
18115 {
18116 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18117 return 0;
18118 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18119 return 2;
18120 }
18121 else
18122 {
18123 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18124 return 0;
18125 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18126 return -2;
18127 }
18128
18129 return 0;
18130}
18131
18132/* Convert a machine dependent frag. */
18133
18134void
17a2f251 18135md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18136{
4a6a3df4
AO
18137 if (RELAX_BRANCH_P (fragp->fr_subtype))
18138 {
4d68580a 18139 char *buf;
4a6a3df4
AO
18140 unsigned long insn;
18141 expressionS exp;
18142 fixS *fixp;
b34976b6 18143
4d68580a
RS
18144 buf = fragp->fr_literal + fragp->fr_fix;
18145 insn = read_insn (buf);
b34976b6 18146
4a6a3df4
AO
18147 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18148 {
18149 /* We generate a fixup instead of applying it right now
18150 because, if there are linker relaxations, we're going to
18151 need the relocations. */
18152 exp.X_op = O_symbol;
18153 exp.X_add_symbol = fragp->fr_symbol;
18154 exp.X_add_number = fragp->fr_offset;
18155
4d68580a
RS
18156 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18157 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18158 fixp->fx_file = fragp->fr_file;
18159 fixp->fx_line = fragp->fr_line;
b34976b6 18160
4d68580a 18161 buf = write_insn (buf, insn);
4a6a3df4
AO
18162 }
18163 else
18164 {
18165 int i;
18166
18167 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 18168 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18169
18170 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18171 goto uncond;
18172
18173 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18174 {
18175 /* Reverse the branch. */
18176 switch ((insn >> 28) & 0xf)
18177 {
18178 case 4:
3bf0dbfb
MR
18179 /* bc[0-3][tf]l? instructions can have the condition
18180 reversed by tweaking a single TF bit, and their
18181 opcodes all have 0x4???????. */
18182 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
18183 insn ^= 0x00010000;
18184 break;
18185
18186 case 0:
18187 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18188 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18189 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18190 insn ^= 0x00010000;
18191 break;
b34976b6 18192
4a6a3df4
AO
18193 case 1:
18194 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18195 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18196 insn ^= 0x04000000;
18197 break;
18198
18199 default:
18200 abort ();
18201 }
18202 }
18203
18204 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18205 {
18206 /* Clear the and-link bit. */
9c2799c2 18207 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18208
54f4ddb3
TS
18209 /* bltzal 0x04100000 bgezal 0x04110000
18210 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18211 insn &= ~0x00100000;
18212 }
18213
18214 /* Branch over the branch (if the branch was likely) or the
18215 full jump (not likely case). Compute the offset from the
18216 current instruction to branch to. */
18217 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18218 i = 16;
18219 else
18220 {
18221 /* How many bytes in instructions we've already emitted? */
4d68580a 18222 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18223 /* How many bytes in instructions from here to the end? */
18224 i = fragp->fr_var - i;
18225 }
18226 /* Convert to instruction count. */
18227 i >>= 2;
18228 /* Branch counts from the next instruction. */
b34976b6 18229 i--;
4a6a3df4
AO
18230 insn |= i;
18231 /* Branch over the jump. */
4d68580a 18232 buf = write_insn (buf, insn);
4a6a3df4 18233
54f4ddb3 18234 /* nop */
4d68580a 18235 buf = write_insn (buf, 0);
4a6a3df4
AO
18236
18237 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18238 {
18239 /* beql $0, $0, 2f */
18240 insn = 0x50000000;
18241 /* Compute the PC offset from the current instruction to
18242 the end of the variable frag. */
18243 /* How many bytes in instructions we've already emitted? */
4d68580a 18244 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18245 /* How many bytes in instructions from here to the end? */
18246 i = fragp->fr_var - i;
18247 /* Convert to instruction count. */
18248 i >>= 2;
18249 /* Don't decrement i, because we want to branch over the
18250 delay slot. */
4a6a3df4 18251 insn |= i;
4a6a3df4 18252
4d68580a
RS
18253 buf = write_insn (buf, insn);
18254 buf = write_insn (buf, 0);
4a6a3df4
AO
18255 }
18256
18257 uncond:
18258 if (mips_pic == NO_PIC)
18259 {
18260 /* j or jal. */
18261 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18262 ? 0x0c000000 : 0x08000000);
18263 exp.X_op = O_symbol;
18264 exp.X_add_symbol = fragp->fr_symbol;
18265 exp.X_add_number = fragp->fr_offset;
18266
4d68580a
RS
18267 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18268 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18269 fixp->fx_file = fragp->fr_file;
18270 fixp->fx_line = fragp->fr_line;
18271
4d68580a 18272 buf = write_insn (buf, insn);
4a6a3df4
AO
18273 }
18274 else
18275 {
66b3e8da
MR
18276 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18277
4a6a3df4 18278 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18279 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18280 insn |= at << OP_SH_RT;
4a6a3df4
AO
18281 exp.X_op = O_symbol;
18282 exp.X_add_symbol = fragp->fr_symbol;
18283 exp.X_add_number = fragp->fr_offset;
18284
18285 if (fragp->fr_offset)
18286 {
18287 exp.X_add_symbol = make_expr_symbol (&exp);
18288 exp.X_add_number = 0;
18289 }
18290
4d68580a
RS
18291 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18292 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18293 fixp->fx_file = fragp->fr_file;
18294 fixp->fx_line = fragp->fr_line;
18295
4d68580a 18296 buf = write_insn (buf, insn);
b34976b6 18297
4a6a3df4 18298 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18299 /* nop */
18300 buf = write_insn (buf, 0);
4a6a3df4
AO
18301
18302 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18303 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18304 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18305
4d68580a
RS
18306 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18307 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18308 fixp->fx_file = fragp->fr_file;
18309 fixp->fx_line = fragp->fr_line;
b34976b6 18310
4d68580a 18311 buf = write_insn (buf, insn);
4a6a3df4
AO
18312
18313 /* j(al)r $at. */
18314 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18315 insn = 0x0000f809;
4a6a3df4 18316 else
66b3e8da
MR
18317 insn = 0x00000008;
18318 insn |= at << OP_SH_RS;
4a6a3df4 18319
4d68580a 18320 buf = write_insn (buf, insn);
4a6a3df4
AO
18321 }
18322 }
18323
4a6a3df4 18324 fragp->fr_fix += fragp->fr_var;
4d68580a 18325 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18326 return;
18327 }
18328
df58fc94
RS
18329 /* Relax microMIPS branches. */
18330 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18331 {
4d68580a 18332 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18333 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18334 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18335 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18336 bfd_boolean short_ds;
df58fc94
RS
18337 unsigned long insn;
18338 expressionS exp;
18339 fixS *fixp;
18340
18341 exp.X_op = O_symbol;
18342 exp.X_add_symbol = fragp->fr_symbol;
18343 exp.X_add_number = fragp->fr_offset;
18344
18345 fragp->fr_fix += fragp->fr_var;
18346
18347 /* Handle 16-bit branches that fit or are forced to fit. */
18348 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18349 {
18350 /* We generate a fixup instead of applying it right now,
18351 because if there is linker relaxation, we're going to
18352 need the relocations. */
18353 if (type == 'D')
4d68580a 18354 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18355 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18356 else if (type == 'E')
4d68580a 18357 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18358 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18359 else
18360 abort ();
18361
18362 fixp->fx_file = fragp->fr_file;
18363 fixp->fx_line = fragp->fr_line;
18364
18365 /* These relocations can have an addend that won't fit in
18366 2 octets. */
18367 fixp->fx_no_overflow = 1;
18368
18369 return;
18370 }
18371
2309ddf2 18372 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18373 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18374 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18375 {
18376 /* We generate a fixup instead of applying it right now,
18377 because if there is linker relaxation, we're going to
18378 need the relocations. */
4d68580a
RS
18379 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18380 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18381 fixp->fx_file = fragp->fr_file;
18382 fixp->fx_line = fragp->fr_line;
18383
18384 if (type == 0)
18385 return;
18386 }
18387
18388 /* Relax 16-bit branches to 32-bit branches. */
18389 if (type != 0)
18390 {
4d68580a 18391 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18392
18393 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18394 insn = 0x94000000; /* beq */
18395 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18396 {
18397 unsigned long regno;
18398
18399 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18400 regno = micromips_to_32_reg_d_map [regno];
18401 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18402 insn |= regno << MICROMIPSOP_SH_RS;
18403 }
18404 else
18405 abort ();
18406
18407 /* Nothing else to do, just write it out. */
18408 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18409 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18410 {
4d68580a
RS
18411 buf = write_compressed_insn (buf, insn, 4);
18412 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18413 return;
18414 }
18415 }
18416 else
4d68580a 18417 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18418
18419 /* Relax 32-bit branches to a sequence of instructions. */
18420 as_warn_where (fragp->fr_file, fragp->fr_line,
18421 _("Relaxed out-of-range branch into a jump"));
18422
2309ddf2
MR
18423 /* Set the short-delay-slot bit. */
18424 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18425
18426 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18427 {
18428 symbolS *l;
18429
18430 /* Reverse the branch. */
18431 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18432 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18433 insn ^= 0x20000000;
18434 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18435 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18436 || (insn & 0xffe00000) == 0x40800000 /* blez */
18437 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18438 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18439 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18440 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18441 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18442 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18443 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18444 insn ^= 0x00400000;
18445 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18446 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18447 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18448 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18449 insn ^= 0x00200000;
18450 else
18451 abort ();
18452
18453 if (al)
18454 {
18455 /* Clear the and-link and short-delay-slot bits. */
18456 gas_assert ((insn & 0xfda00000) == 0x40200000);
18457
18458 /* bltzal 0x40200000 bgezal 0x40600000 */
18459 /* bltzals 0x42200000 bgezals 0x42600000 */
18460 insn &= ~0x02200000;
18461 }
18462
18463 /* Make a label at the end for use with the branch. */
18464 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18465 micromips_label_inc ();
f3ded42a 18466 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18467
18468 /* Refer to it. */
4d68580a
RS
18469 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18470 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18471 fixp->fx_file = fragp->fr_file;
18472 fixp->fx_line = fragp->fr_line;
18473
18474 /* Branch over the jump. */
4d68580a 18475 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18476 if (!compact)
4d68580a
RS
18477 /* nop */
18478 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18479 }
18480
18481 if (mips_pic == NO_PIC)
18482 {
2309ddf2
MR
18483 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18484
df58fc94
RS
18485 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18486 insn = al ? jal : 0xd4000000;
18487
4d68580a
RS
18488 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18489 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18490 fixp->fx_file = fragp->fr_file;
18491 fixp->fx_line = fragp->fr_line;
18492
4d68580a 18493 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18494 if (compact)
4d68580a
RS
18495 /* nop */
18496 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18497 }
18498 else
18499 {
18500 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18501 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18502 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18503
18504 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18505 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18506 insn |= at << MICROMIPSOP_SH_RT;
18507
18508 if (exp.X_add_number)
18509 {
18510 exp.X_add_symbol = make_expr_symbol (&exp);
18511 exp.X_add_number = 0;
18512 }
18513
4d68580a
RS
18514 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18515 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18516 fixp->fx_file = fragp->fr_file;
18517 fixp->fx_line = fragp->fr_line;
18518
4d68580a 18519 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18520
18521 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18522 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18523 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18524
4d68580a
RS
18525 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18526 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18527 fixp->fx_file = fragp->fr_file;
18528 fixp->fx_line = fragp->fr_line;
18529
4d68580a 18530 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18531
18532 /* jr/jrc/jalr/jalrs $at */
18533 insn = al ? jalr : jr;
18534 insn |= at << MICROMIPSOP_SH_MJ;
18535
4d68580a 18536 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18537 }
18538
4d68580a 18539 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18540 return;
18541 }
18542
252b5132
RH
18543 if (RELAX_MIPS16_P (fragp->fr_subtype))
18544 {
18545 int type;
3994f87e 18546 const struct mips16_immed_operand *op;
252b5132 18547 offsetT val;
5c04167a
RS
18548 char *buf;
18549 unsigned int user_length, length;
252b5132 18550 unsigned long insn;
5c04167a 18551 bfd_boolean ext;
252b5132
RH
18552
18553 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18554 op = mips16_immed_operands;
18555 while (op->type != type)
18556 ++op;
18557
5c04167a 18558 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18559 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18560 if (op->pcrel)
18561 {
18562 addressT addr;
18563
18564 addr = fragp->fr_address + fragp->fr_fix;
18565
18566 /* The rules for the base address of a PC relative reloc are
18567 complicated; see mips16_extended_frag. */
18568 if (type == 'p' || type == 'q')
18569 {
18570 addr += 2;
18571 if (ext)
18572 addr += 2;
18573 /* Ignore the low bit in the target, since it will be
18574 set for a text label. */
18575 if ((val & 1) != 0)
18576 --val;
18577 }
18578 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18579 addr -= 4;
18580 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18581 addr -= 2;
18582
18583 addr &= ~ (addressT) ((1 << op->shift) - 1);
18584 val -= addr;
18585
18586 /* Make sure the section winds up with the alignment we have
18587 assumed. */
18588 if (op->shift > 0)
18589 record_alignment (asec, op->shift);
18590 }
18591
18592 if (ext
18593 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18594 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18595 as_warn_where (fragp->fr_file, fragp->fr_line,
18596 _("extended instruction in delay slot"));
18597
5c04167a 18598 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18599
4d68580a 18600 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18601 if (ext)
18602 insn |= MIPS16_EXTEND;
252b5132 18603
5c04167a
RS
18604 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18605 user_length = 4;
18606 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18607 user_length = 2;
18608 else
18609 user_length = 0;
18610
43c0598f 18611 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18612 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18613
5c04167a
RS
18614 length = (ext ? 4 : 2);
18615 gas_assert (mips16_opcode_length (insn) == length);
18616 write_compressed_insn (buf, insn, length);
18617 fragp->fr_fix += length;
252b5132
RH
18618 }
18619 else
18620 {
df58fc94
RS
18621 relax_substateT subtype = fragp->fr_subtype;
18622 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18623 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18624 int first, second;
18625 fixS *fixp;
252b5132 18626
df58fc94
RS
18627 first = RELAX_FIRST (subtype);
18628 second = RELAX_SECOND (subtype);
4d7206a2 18629 fixp = (fixS *) fragp->fr_opcode;
252b5132 18630
df58fc94
RS
18631 /* If the delay slot chosen does not match the size of the instruction,
18632 then emit a warning. */
18633 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18634 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18635 {
18636 relax_substateT s;
18637 const char *msg;
18638
18639 s = subtype & (RELAX_DELAY_SLOT_16BIT
18640 | RELAX_DELAY_SLOT_SIZE_FIRST
18641 | RELAX_DELAY_SLOT_SIZE_SECOND);
18642 msg = macro_warning (s);
18643 if (msg != NULL)
db9b2be4 18644 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18645 subtype &= ~s;
18646 }
18647
584892a6 18648 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18649 if (use_second == second_longer)
584892a6 18650 {
df58fc94
RS
18651 relax_substateT s;
18652 const char *msg;
18653
18654 s = (subtype
18655 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18656 msg = macro_warning (s);
18657 if (msg != NULL)
db9b2be4 18658 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18659 subtype &= ~s;
584892a6
RS
18660 }
18661
4d7206a2
RS
18662 /* Go through all the fixups for the first sequence. Disable them
18663 (by marking them as done) if we're going to use the second
18664 sequence instead. */
18665 while (fixp
18666 && fixp->fx_frag == fragp
18667 && fixp->fx_where < fragp->fr_fix - second)
18668 {
df58fc94 18669 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18670 fixp->fx_done = 1;
18671 fixp = fixp->fx_next;
18672 }
252b5132 18673
4d7206a2
RS
18674 /* Go through the fixups for the second sequence. Disable them if
18675 we're going to use the first sequence, otherwise adjust their
18676 addresses to account for the relaxation. */
18677 while (fixp && fixp->fx_frag == fragp)
18678 {
df58fc94 18679 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18680 fixp->fx_where -= first;
18681 else
18682 fixp->fx_done = 1;
18683 fixp = fixp->fx_next;
18684 }
18685
18686 /* Now modify the frag contents. */
df58fc94 18687 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18688 {
18689 char *start;
18690
18691 start = fragp->fr_literal + fragp->fr_fix - first - second;
18692 memmove (start, start + first, second);
18693 fragp->fr_fix -= first;
18694 }
18695 else
18696 fragp->fr_fix -= second;
252b5132
RH
18697 }
18698}
18699
252b5132
RH
18700/* This function is called after the relocs have been generated.
18701 We've been storing mips16 text labels as odd. Here we convert them
18702 back to even for the convenience of the debugger. */
18703
18704void
17a2f251 18705mips_frob_file_after_relocs (void)
252b5132
RH
18706{
18707 asymbol **syms;
18708 unsigned int count, i;
18709
252b5132
RH
18710 syms = bfd_get_outsymbols (stdoutput);
18711 count = bfd_get_symcount (stdoutput);
18712 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18713 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18714 && ((*syms)->value & 1) != 0)
18715 {
18716 (*syms)->value &= ~1;
18717 /* If the symbol has an odd size, it was probably computed
18718 incorrectly, so adjust that as well. */
18719 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18720 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18721 }
252b5132
RH
18722}
18723
a1facbec
MR
18724/* This function is called whenever a label is defined, including fake
18725 labels instantiated off the dot special symbol. It is used when
18726 handling branch delays; if a branch has a label, we assume we cannot
18727 move it. This also bumps the value of the symbol by 1 in compressed
18728 code. */
252b5132 18729
e1b47bd5 18730static void
a1facbec 18731mips_record_label (symbolS *sym)
252b5132 18732{
a8dbcb85 18733 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18734 struct insn_label_list *l;
18735
18736 if (free_insn_labels == NULL)
18737 l = (struct insn_label_list *) xmalloc (sizeof *l);
18738 else
18739 {
18740 l = free_insn_labels;
18741 free_insn_labels = l->next;
18742 }
18743
18744 l->label = sym;
a8dbcb85
TS
18745 l->next = si->label_list;
18746 si->label_list = l;
a1facbec 18747}
07a53e5c 18748
a1facbec
MR
18749/* This function is called as tc_frob_label() whenever a label is defined
18750 and adds a DWARF-2 record we only want for true labels. */
18751
18752void
18753mips_define_label (symbolS *sym)
18754{
18755 mips_record_label (sym);
07a53e5c 18756 dwarf2_emit_label (sym);
252b5132 18757}
e1b47bd5
RS
18758
18759/* This function is called by tc_new_dot_label whenever a new dot symbol
18760 is defined. */
18761
18762void
18763mips_add_dot_label (symbolS *sym)
18764{
18765 mips_record_label (sym);
18766 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18767 mips_compressed_mark_label (sym);
18768}
252b5132 18769\f
252b5132
RH
18770/* Some special processing for a MIPS ELF file. */
18771
18772void
17a2f251 18773mips_elf_final_processing (void)
252b5132
RH
18774{
18775 /* Write out the register information. */
316f5878 18776 if (mips_abi != N64_ABI)
252b5132
RH
18777 {
18778 Elf32_RegInfo s;
18779
18780 s.ri_gprmask = mips_gprmask;
18781 s.ri_cprmask[0] = mips_cprmask[0];
18782 s.ri_cprmask[1] = mips_cprmask[1];
18783 s.ri_cprmask[2] = mips_cprmask[2];
18784 s.ri_cprmask[3] = mips_cprmask[3];
18785 /* The gp_value field is set by the MIPS ELF backend. */
18786
18787 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18788 ((Elf32_External_RegInfo *)
18789 mips_regmask_frag));
18790 }
18791 else
18792 {
18793 Elf64_Internal_RegInfo s;
18794
18795 s.ri_gprmask = mips_gprmask;
18796 s.ri_pad = 0;
18797 s.ri_cprmask[0] = mips_cprmask[0];
18798 s.ri_cprmask[1] = mips_cprmask[1];
18799 s.ri_cprmask[2] = mips_cprmask[2];
18800 s.ri_cprmask[3] = mips_cprmask[3];
18801 /* The gp_value field is set by the MIPS ELF backend. */
18802
18803 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18804 ((Elf64_External_RegInfo *)
18805 mips_regmask_frag));
18806 }
18807
18808 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18809 sort of BFD interface for this. */
18810 if (mips_any_noreorder)
18811 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18812 if (mips_pic != NO_PIC)
143d77c5 18813 {
8b828383 18814 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18815 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18816 }
18817 if (mips_abicalls)
18818 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18819
b015e599
AP
18820 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18821 defined at present; this might need to change in future. */
a4672219
TS
18822 if (file_ase_mips16)
18823 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18824 if (file_ase_micromips)
18825 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 18826 if (file_ase & ASE_MDMX)
deec1734 18827 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18828
bdaaa2e1 18829 /* Set the MIPS ELF ABI flags. */
316f5878 18830 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18831 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18832 else if (mips_abi == O64_ABI)
252b5132 18833 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18834 else if (mips_abi == EABI_ABI)
252b5132 18835 {
316f5878 18836 if (!file_mips_gp32)
252b5132
RH
18837 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18838 else
18839 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18840 }
316f5878 18841 else if (mips_abi == N32_ABI)
be00bddd
TS
18842 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18843
c9914766 18844 /* Nothing to do for N64_ABI. */
252b5132
RH
18845
18846 if (mips_32bitmode)
18847 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18848
18849#if 0 /* XXX FIXME */
18850 /* 32 bit code with 64 bit FP registers. */
18851 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18852 elf_elfheader (stdoutput)->e_flags |= ???;
18853#endif
252b5132 18854}
252b5132 18855\f
beae10d5 18856typedef struct proc {
9b2f1d35
EC
18857 symbolS *func_sym;
18858 symbolS *func_end_sym;
beae10d5
KH
18859 unsigned long reg_mask;
18860 unsigned long reg_offset;
18861 unsigned long fpreg_mask;
18862 unsigned long fpreg_offset;
18863 unsigned long frame_offset;
18864 unsigned long frame_reg;
18865 unsigned long pc_reg;
18866} procS;
252b5132
RH
18867
18868static procS cur_proc;
18869static procS *cur_proc_ptr;
18870static int numprocs;
18871
df58fc94
RS
18872/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18873 as "2", and a normal nop as "0". */
18874
18875#define NOP_OPCODE_MIPS 0
18876#define NOP_OPCODE_MIPS16 1
18877#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18878
18879char
18880mips_nop_opcode (void)
18881{
df58fc94
RS
18882 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18883 return NOP_OPCODE_MICROMIPS;
18884 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18885 return NOP_OPCODE_MIPS16;
18886 else
18887 return NOP_OPCODE_MIPS;
742a56fe
RS
18888}
18889
df58fc94
RS
18890/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18891 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18892
0a9ef439 18893void
17a2f251 18894mips_handle_align (fragS *fragp)
a19d8eb0 18895{
df58fc94 18896 char nop_opcode;
742a56fe 18897 char *p;
c67a084a
NC
18898 int bytes, size, excess;
18899 valueT opcode;
742a56fe 18900
0a9ef439
RH
18901 if (fragp->fr_type != rs_align_code)
18902 return;
18903
742a56fe 18904 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18905 nop_opcode = *p;
18906 switch (nop_opcode)
a19d8eb0 18907 {
df58fc94
RS
18908 case NOP_OPCODE_MICROMIPS:
18909 opcode = micromips_nop32_insn.insn_opcode;
18910 size = 4;
18911 break;
18912 case NOP_OPCODE_MIPS16:
c67a084a
NC
18913 opcode = mips16_nop_insn.insn_opcode;
18914 size = 2;
df58fc94
RS
18915 break;
18916 case NOP_OPCODE_MIPS:
18917 default:
c67a084a
NC
18918 opcode = nop_insn.insn_opcode;
18919 size = 4;
df58fc94 18920 break;
c67a084a 18921 }
a19d8eb0 18922
c67a084a
NC
18923 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18924 excess = bytes % size;
df58fc94
RS
18925
18926 /* Handle the leading part if we're not inserting a whole number of
18927 instructions, and make it the end of the fixed part of the frag.
18928 Try to fit in a short microMIPS NOP if applicable and possible,
18929 and use zeroes otherwise. */
18930 gas_assert (excess < 4);
18931 fragp->fr_fix += excess;
18932 switch (excess)
c67a084a 18933 {
df58fc94
RS
18934 case 3:
18935 *p++ = '\0';
18936 /* Fall through. */
18937 case 2:
833794fc 18938 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 18939 {
4d68580a 18940 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18941 break;
18942 }
18943 *p++ = '\0';
18944 /* Fall through. */
18945 case 1:
18946 *p++ = '\0';
18947 /* Fall through. */
18948 case 0:
18949 break;
a19d8eb0 18950 }
c67a084a
NC
18951
18952 md_number_to_chars (p, opcode, size);
18953 fragp->fr_var = size;
a19d8eb0
CP
18954}
18955
252b5132 18956static void
17a2f251 18957md_obj_begin (void)
252b5132
RH
18958{
18959}
18960
18961static void
17a2f251 18962md_obj_end (void)
252b5132 18963{
54f4ddb3 18964 /* Check for premature end, nesting errors, etc. */
252b5132 18965 if (cur_proc_ptr)
9a41af64 18966 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18967}
18968
18969static long
17a2f251 18970get_number (void)
252b5132
RH
18971{
18972 int negative = 0;
18973 long val = 0;
18974
18975 if (*input_line_pointer == '-')
18976 {
18977 ++input_line_pointer;
18978 negative = 1;
18979 }
3882b010 18980 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18981 as_bad (_("expected simple number"));
252b5132
RH
18982 if (input_line_pointer[0] == '0')
18983 {
18984 if (input_line_pointer[1] == 'x')
18985 {
18986 input_line_pointer += 2;
3882b010 18987 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18988 {
18989 val <<= 4;
18990 val |= hex_value (*input_line_pointer++);
18991 }
18992 return negative ? -val : val;
18993 }
18994 else
18995 {
18996 ++input_line_pointer;
3882b010 18997 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18998 {
18999 val <<= 3;
19000 val |= *input_line_pointer++ - '0';
19001 }
19002 return negative ? -val : val;
19003 }
19004 }
3882b010 19005 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19006 {
19007 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19008 *input_line_pointer, *input_line_pointer);
956cd1d6 19009 as_warn (_("invalid number"));
252b5132
RH
19010 return -1;
19011 }
3882b010 19012 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19013 {
19014 val *= 10;
19015 val += *input_line_pointer++ - '0';
19016 }
19017 return negative ? -val : val;
19018}
19019
19020/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19021 is an initial number which is the ECOFF file index. In the non-ECOFF
19022 case .file implies DWARF-2. */
19023
19024static void
17a2f251 19025s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19026{
ecb4347a
DJ
19027 static int first_file_directive = 0;
19028
c5dd6aab
DJ
19029 if (ECOFF_DEBUGGING)
19030 {
19031 get_number ();
19032 s_app_file (0);
19033 }
19034 else
ecb4347a
DJ
19035 {
19036 char *filename;
19037
19038 filename = dwarf2_directive_file (0);
19039
19040 /* Versions of GCC up to 3.1 start files with a ".file"
19041 directive even for stabs output. Make sure that this
19042 ".file" is handled. Note that you need a version of GCC
19043 after 3.1 in order to support DWARF-2 on MIPS. */
19044 if (filename != NULL && ! first_file_directive)
19045 {
19046 (void) new_logical_line (filename, -1);
c04f5787 19047 s_app_file_string (filename, 0);
ecb4347a
DJ
19048 }
19049 first_file_directive = 1;
19050 }
c5dd6aab
DJ
19051}
19052
19053/* The .loc directive, implying DWARF-2. */
252b5132
RH
19054
19055static void
17a2f251 19056s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19057{
c5dd6aab
DJ
19058 if (!ECOFF_DEBUGGING)
19059 dwarf2_directive_loc (0);
252b5132
RH
19060}
19061
252b5132
RH
19062/* The .end directive. */
19063
19064static void
17a2f251 19065s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19066{
19067 symbolS *p;
252b5132 19068
7a621144
DJ
19069 /* Following functions need their own .frame and .cprestore directives. */
19070 mips_frame_reg_valid = 0;
19071 mips_cprestore_valid = 0;
19072
252b5132
RH
19073 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19074 {
19075 p = get_symbol ();
19076 demand_empty_rest_of_line ();
19077 }
19078 else
19079 p = NULL;
19080
14949570 19081 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19082 as_warn (_(".end not in text section"));
19083
19084 if (!cur_proc_ptr)
19085 {
19086 as_warn (_(".end directive without a preceding .ent directive."));
19087 demand_empty_rest_of_line ();
19088 return;
19089 }
19090
19091 if (p != NULL)
19092 {
9c2799c2 19093 gas_assert (S_GET_NAME (p));
9b2f1d35 19094 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 19095 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
19096
19097 if (debug_type == DEBUG_STABS)
19098 stabs_generate_asm_endfunc (S_GET_NAME (p),
19099 S_GET_NAME (p));
252b5132
RH
19100 }
19101 else
19102 as_warn (_(".end directive missing or unknown symbol"));
19103
9b2f1d35
EC
19104 /* Create an expression to calculate the size of the function. */
19105 if (p && cur_proc_ptr)
19106 {
19107 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19108 expressionS *exp = xmalloc (sizeof (expressionS));
19109
19110 obj->size = exp;
19111 exp->X_op = O_subtract;
19112 exp->X_add_symbol = symbol_temp_new_now ();
19113 exp->X_op_symbol = p;
19114 exp->X_add_number = 0;
19115
19116 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19117 }
19118
ecb4347a 19119 /* Generate a .pdr section. */
f3ded42a 19120 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19121 {
19122 segT saved_seg = now_seg;
19123 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19124 expressionS exp;
19125 char *fragp;
252b5132 19126
252b5132 19127#ifdef md_flush_pending_output
ecb4347a 19128 md_flush_pending_output ();
252b5132
RH
19129#endif
19130
9c2799c2 19131 gas_assert (pdr_seg);
ecb4347a 19132 subseg_set (pdr_seg, 0);
252b5132 19133
ecb4347a
DJ
19134 /* Write the symbol. */
19135 exp.X_op = O_symbol;
19136 exp.X_add_symbol = p;
19137 exp.X_add_number = 0;
19138 emit_expr (&exp, 4);
252b5132 19139
ecb4347a 19140 fragp = frag_more (7 * 4);
252b5132 19141
17a2f251
TS
19142 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19143 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19144 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19145 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19146 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19147 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19148 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19149
ecb4347a
DJ
19150 subseg_set (saved_seg, saved_subseg);
19151 }
252b5132
RH
19152
19153 cur_proc_ptr = NULL;
19154}
19155
19156/* The .aent and .ent directives. */
19157
19158static void
17a2f251 19159s_mips_ent (int aent)
252b5132 19160{
252b5132 19161 symbolS *symbolP;
252b5132
RH
19162
19163 symbolP = get_symbol ();
19164 if (*input_line_pointer == ',')
f9419b05 19165 ++input_line_pointer;
252b5132 19166 SKIP_WHITESPACE ();
3882b010 19167 if (ISDIGIT (*input_line_pointer)
d9a62219 19168 || *input_line_pointer == '-')
874e8986 19169 get_number ();
252b5132 19170
14949570 19171 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19172 as_warn (_(".ent or .aent not in text section."));
19173
19174 if (!aent && cur_proc_ptr)
9a41af64 19175 as_warn (_("missing .end"));
252b5132
RH
19176
19177 if (!aent)
19178 {
7a621144
DJ
19179 /* This function needs its own .frame and .cprestore directives. */
19180 mips_frame_reg_valid = 0;
19181 mips_cprestore_valid = 0;
19182
252b5132
RH
19183 cur_proc_ptr = &cur_proc;
19184 memset (cur_proc_ptr, '\0', sizeof (procS));
19185
9b2f1d35 19186 cur_proc_ptr->func_sym = symbolP;
252b5132 19187
f9419b05 19188 ++numprocs;
ecb4347a
DJ
19189
19190 if (debug_type == DEBUG_STABS)
19191 stabs_generate_asm_func (S_GET_NAME (symbolP),
19192 S_GET_NAME (symbolP));
252b5132
RH
19193 }
19194
7c0fc524
MR
19195 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19196
252b5132
RH
19197 demand_empty_rest_of_line ();
19198}
19199
19200/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19201 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19202 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19203 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19204 symbol table (in the mdebug section). */
19205
19206static void
17a2f251 19207s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19208{
f3ded42a
RS
19209 if (ECOFF_DEBUGGING)
19210 s_ignore (ignore);
19211 else
ecb4347a
DJ
19212 {
19213 long val;
252b5132 19214
ecb4347a
DJ
19215 if (cur_proc_ptr == (procS *) NULL)
19216 {
19217 as_warn (_(".frame outside of .ent"));
19218 demand_empty_rest_of_line ();
19219 return;
19220 }
252b5132 19221
ecb4347a
DJ
19222 cur_proc_ptr->frame_reg = tc_get_register (1);
19223
19224 SKIP_WHITESPACE ();
19225 if (*input_line_pointer++ != ','
19226 || get_absolute_expression_and_terminator (&val) != ',')
19227 {
19228 as_warn (_("Bad .frame directive"));
19229 --input_line_pointer;
19230 demand_empty_rest_of_line ();
19231 return;
19232 }
252b5132 19233
ecb4347a
DJ
19234 cur_proc_ptr->frame_offset = val;
19235 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19236
252b5132 19237 demand_empty_rest_of_line ();
252b5132 19238 }
252b5132
RH
19239}
19240
bdaaa2e1
KH
19241/* The .fmask and .mask directives. If the mdebug section is present
19242 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19243 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19244 information correctly. We can't use the ecoff routines because they
252b5132
RH
19245 make reference to the ecoff symbol table (in the mdebug section). */
19246
19247static void
17a2f251 19248s_mips_mask (int reg_type)
252b5132 19249{
f3ded42a
RS
19250 if (ECOFF_DEBUGGING)
19251 s_ignore (reg_type);
19252 else
252b5132 19253 {
ecb4347a 19254 long mask, off;
252b5132 19255
ecb4347a
DJ
19256 if (cur_proc_ptr == (procS *) NULL)
19257 {
19258 as_warn (_(".mask/.fmask outside of .ent"));
19259 demand_empty_rest_of_line ();
19260 return;
19261 }
252b5132 19262
ecb4347a
DJ
19263 if (get_absolute_expression_and_terminator (&mask) != ',')
19264 {
19265 as_warn (_("Bad .mask/.fmask directive"));
19266 --input_line_pointer;
19267 demand_empty_rest_of_line ();
19268 return;
19269 }
252b5132 19270
ecb4347a
DJ
19271 off = get_absolute_expression ();
19272
19273 if (reg_type == 'F')
19274 {
19275 cur_proc_ptr->fpreg_mask = mask;
19276 cur_proc_ptr->fpreg_offset = off;
19277 }
19278 else
19279 {
19280 cur_proc_ptr->reg_mask = mask;
19281 cur_proc_ptr->reg_offset = off;
19282 }
19283
19284 demand_empty_rest_of_line ();
252b5132 19285 }
252b5132
RH
19286}
19287
316f5878
RS
19288/* A table describing all the processors gas knows about. Names are
19289 matched in the order listed.
e7af610e 19290
316f5878
RS
19291 To ease comparison, please keep this table in the same order as
19292 gcc's mips_cpu_info_table[]. */
e972090a
NC
19293static const struct mips_cpu_info mips_cpu_info_table[] =
19294{
316f5878 19295 /* Entries for generic ISAs */
d16afab6
RS
19296 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19297 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19298 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19299 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19300 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19301 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19302 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19303 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19304 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19305
19306 /* MIPS I */
d16afab6
RS
19307 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19308 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19309 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19310
19311 /* MIPS II */
d16afab6 19312 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19313
19314 /* MIPS III */
d16afab6
RS
19315 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19316 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19317 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19318 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19319 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19320 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19321 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19322 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19323 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19324 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19325 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19326 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19327 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19328 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19329 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19330 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19331
19332 /* MIPS IV */
d16afab6
RS
19333 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19334 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19335 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19336 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19337 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19338 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19339 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19340 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19341 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19342 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19343 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19344 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19345 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19346 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19347 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19348
19349 /* MIPS 32 */
d16afab6
RS
19350 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19351 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19352 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19353 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19354
19355 /* MIPS 32 Release 2 */
d16afab6
RS
19356 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19357 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19358 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19359 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19360 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19361 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19362 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19363 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19364 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19365 ISA_MIPS32R2, CPU_MIPS32R2 },
19366 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19367 ISA_MIPS32R2, CPU_MIPS32R2 },
19368 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19369 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19370 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19371 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19372 /* Deprecated forms of the above. */
d16afab6
RS
19373 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19374 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19375 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19376 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19377 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19378 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19379 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19380 /* Deprecated forms of the above. */
d16afab6
RS
19381 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19382 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19383 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19384 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19385 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19386 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19387 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19388 /* Deprecated forms of the above. */
d16afab6
RS
19389 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19390 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19391 /* 34Kn is a 34kc without DSP. */
d16afab6 19392 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19393 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19394 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19395 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19396 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19397 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19398 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19399 /* Deprecated forms of the above. */
d16afab6
RS
19400 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19401 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19402 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19403 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19404 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19405 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19406 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19407
316f5878 19408 /* MIPS 64 */
d16afab6
RS
19409 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19410 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19411 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19412 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19413
c7a23324 19414 /* Broadcom SB-1 CPU core */
d16afab6 19415 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19416 /* Broadcom SB-1A CPU core */
d16afab6 19417 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 19418
d16afab6 19419 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19420
ed163775
MR
19421 /* MIPS 64 Release 2 */
19422
967344c6 19423 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19424 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19425 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19426 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19427
52b6b6b9 19428 /* RMI Xlr */
d16afab6 19429 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19430
55a36193
MK
19431 /* Broadcom XLP.
19432 XLP is mostly like XLR, with the prominent exception that it is
19433 MIPS64R2 rather than MIPS64. */
d16afab6 19434 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19435
316f5878 19436 /* End marker */
d16afab6 19437 { NULL, 0, 0, 0, 0 }
316f5878 19438};
e7af610e 19439
84ea6cf2 19440
316f5878
RS
19441/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19442 with a final "000" replaced by "k". Ignore case.
e7af610e 19443
316f5878 19444 Note: this function is shared between GCC and GAS. */
c6c98b38 19445
b34976b6 19446static bfd_boolean
17a2f251 19447mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19448{
19449 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19450 given++, canonical++;
19451
19452 return ((*given == 0 && *canonical == 0)
19453 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19454}
19455
19456
19457/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19458 CPU name. We've traditionally allowed a lot of variation here.
19459
19460 Note: this function is shared between GCC and GAS. */
19461
b34976b6 19462static bfd_boolean
17a2f251 19463mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19464{
19465 /* First see if the name matches exactly, or with a final "000"
19466 turned into "k". */
19467 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19468 return TRUE;
316f5878
RS
19469
19470 /* If not, try comparing based on numerical designation alone.
19471 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19472 if (TOLOWER (*given) == 'r')
19473 given++;
19474 if (!ISDIGIT (*given))
b34976b6 19475 return FALSE;
316f5878
RS
19476
19477 /* Skip over some well-known prefixes in the canonical name,
19478 hoping to find a number there too. */
19479 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19480 canonical += 2;
19481 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19482 canonical += 2;
19483 else if (TOLOWER (canonical[0]) == 'r')
19484 canonical += 1;
19485
19486 return mips_strict_matching_cpu_name_p (canonical, given);
19487}
19488
19489
19490/* Parse an option that takes the name of a processor as its argument.
19491 OPTION is the name of the option and CPU_STRING is the argument.
19492 Return the corresponding processor enumeration if the CPU_STRING is
19493 recognized, otherwise report an error and return null.
19494
19495 A similar function exists in GCC. */
e7af610e
NC
19496
19497static const struct mips_cpu_info *
17a2f251 19498mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19499{
316f5878 19500 const struct mips_cpu_info *p;
e7af610e 19501
316f5878
RS
19502 /* 'from-abi' selects the most compatible architecture for the given
19503 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19504 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19505 version. Look first at the -mgp options, if given, otherwise base
19506 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19507
316f5878
RS
19508 Treat NO_ABI like the EABIs. One reason to do this is that the
19509 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19510 architecture. This code picks MIPS I for 'mips' and MIPS III for
19511 'mips64', just as we did in the days before 'from-abi'. */
19512 if (strcasecmp (cpu_string, "from-abi") == 0)
19513 {
19514 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19515 return mips_cpu_info_from_isa (ISA_MIPS1);
19516
19517 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19518 return mips_cpu_info_from_isa (ISA_MIPS3);
19519
19520 if (file_mips_gp32 >= 0)
19521 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19522
19523 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19524 ? ISA_MIPS3
19525 : ISA_MIPS1);
19526 }
19527
19528 /* 'default' has traditionally been a no-op. Probably not very useful. */
19529 if (strcasecmp (cpu_string, "default") == 0)
19530 return 0;
19531
19532 for (p = mips_cpu_info_table; p->name != 0; p++)
19533 if (mips_matching_cpu_name_p (p->name, cpu_string))
19534 return p;
19535
20203fb9 19536 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19537 return 0;
e7af610e
NC
19538}
19539
316f5878
RS
19540/* Return the canonical processor information for ISA (a member of the
19541 ISA_MIPS* enumeration). */
19542
e7af610e 19543static const struct mips_cpu_info *
17a2f251 19544mips_cpu_info_from_isa (int isa)
e7af610e
NC
19545{
19546 int i;
19547
19548 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19549 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19550 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19551 return (&mips_cpu_info_table[i]);
19552
e972090a 19553 return NULL;
e7af610e 19554}
fef14a42
TS
19555
19556static const struct mips_cpu_info *
17a2f251 19557mips_cpu_info_from_arch (int arch)
fef14a42
TS
19558{
19559 int i;
19560
19561 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19562 if (arch == mips_cpu_info_table[i].cpu)
19563 return (&mips_cpu_info_table[i]);
19564
19565 return NULL;
19566}
316f5878
RS
19567\f
19568static void
17a2f251 19569show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19570{
19571 if (*first_p)
19572 {
19573 fprintf (stream, "%24s", "");
19574 *col_p = 24;
19575 }
19576 else
19577 {
19578 fprintf (stream, ", ");
19579 *col_p += 2;
19580 }
e7af610e 19581
316f5878
RS
19582 if (*col_p + strlen (string) > 72)
19583 {
19584 fprintf (stream, "\n%24s", "");
19585 *col_p = 24;
19586 }
19587
19588 fprintf (stream, "%s", string);
19589 *col_p += strlen (string);
19590
19591 *first_p = 0;
19592}
19593
19594void
17a2f251 19595md_show_usage (FILE *stream)
e7af610e 19596{
316f5878
RS
19597 int column, first;
19598 size_t i;
19599
19600 fprintf (stream, _("\
19601MIPS options:\n\
316f5878
RS
19602-EB generate big endian output\n\
19603-EL generate little endian output\n\
19604-g, -g2 do not remove unneeded NOPs or swap branches\n\
19605-G NUM allow referencing objects up to NUM bytes\n\
19606 implicitly with the gp register [default 8]\n"));
19607 fprintf (stream, _("\
19608-mips1 generate MIPS ISA I instructions\n\
19609-mips2 generate MIPS ISA II instructions\n\
19610-mips3 generate MIPS ISA III instructions\n\
19611-mips4 generate MIPS ISA IV instructions\n\
19612-mips5 generate MIPS ISA V instructions\n\
19613-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19614-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19615-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19616-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19617-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19618
19619 first = 1;
e7af610e
NC
19620
19621 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19622 show (stream, mips_cpu_info_table[i].name, &column, &first);
19623 show (stream, "from-abi", &column, &first);
19624 fputc ('\n', stream);
e7af610e 19625
316f5878
RS
19626 fprintf (stream, _("\
19627-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19628-no-mCPU don't generate code specific to CPU.\n\
19629 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19630
19631 first = 1;
19632
19633 show (stream, "3900", &column, &first);
19634 show (stream, "4010", &column, &first);
19635 show (stream, "4100", &column, &first);
19636 show (stream, "4650", &column, &first);
19637 fputc ('\n', stream);
19638
19639 fprintf (stream, _("\
19640-mips16 generate mips16 instructions\n\
19641-no-mips16 do not generate mips16 instructions\n"));
19642 fprintf (stream, _("\
df58fc94
RS
19643-mmicromips generate microMIPS instructions\n\
19644-mno-micromips do not generate microMIPS instructions\n"));
19645 fprintf (stream, _("\
e16bfa71
TS
19646-msmartmips generate smartmips instructions\n\
19647-mno-smartmips do not generate smartmips instructions\n"));
19648 fprintf (stream, _("\
74cd071d
CF
19649-mdsp generate DSP instructions\n\
19650-mno-dsp do not generate DSP instructions\n"));
19651 fprintf (stream, _("\
8b082fb1
TS
19652-mdspr2 generate DSP R2 instructions\n\
19653-mno-dspr2 do not generate DSP R2 instructions\n"));
19654 fprintf (stream, _("\
ef2e4d86
CF
19655-mmt generate MT instructions\n\
19656-mno-mt do not generate MT instructions\n"));
19657 fprintf (stream, _("\
dec0624d
MR
19658-mmcu generate MCU instructions\n\
19659-mno-mcu do not generate MCU instructions\n"));
19660 fprintf (stream, _("\
b015e599
AP
19661-mvirt generate Virtualization instructions\n\
19662-mno-virt do not generate Virtualization instructions\n"));
19663 fprintf (stream, _("\
833794fc
MR
19664-minsn32 only generate 32-bit microMIPS instructions\n\
19665-mno-insn32 generate all microMIPS instructions\n"));
19666 fprintf (stream, _("\
c67a084a
NC
19667-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19668-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19669-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19670-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19671-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19672-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19673-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19674-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19675-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19676-O0 remove unneeded NOPs, do not swap branches\n\
19677-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19678--trap, --no-break trap exception on div by 0 and mult overflow\n\
19679--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19680 fprintf (stream, _("\
19681-mhard-float allow floating-point instructions\n\
19682-msoft-float do not allow floating-point instructions\n\
19683-msingle-float only allow 32-bit floating-point operations\n\
19684-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb
MR
19685--[no-]construct-floats [dis]allow floating point values to be constructed\n\
19686--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n"
037b32b9 19687 ));
316f5878
RS
19688 fprintf (stream, _("\
19689-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19690-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19691-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19692-non_shared do not generate code that can operate with DSOs\n\
316f5878 19693-xgot assume a 32 bit GOT\n\
dcd410fe 19694-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19695-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19696 position dependent (non shared) code\n\
316f5878
RS
19697-mabi=ABI create ABI conformant object file for:\n"));
19698
19699 first = 1;
19700
19701 show (stream, "32", &column, &first);
19702 show (stream, "o64", &column, &first);
19703 show (stream, "n32", &column, &first);
19704 show (stream, "64", &column, &first);
19705 show (stream, "eabi", &column, &first);
19706
19707 fputc ('\n', stream);
19708
19709 fprintf (stream, _("\
19710-32 create o32 ABI object file (default)\n\
19711-n32 create n32 ABI object file\n\
19712-64 create 64 ABI object file\n"));
e7af610e 19713}
14e777e0 19714
1575952e 19715#ifdef TE_IRIX
14e777e0 19716enum dwarf2_format
413a266c 19717mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19718{
369943fe 19719 if (HAVE_64BIT_SYMBOLS)
1575952e 19720 return dwarf2_format_64bit_irix;
14e777e0
KB
19721 else
19722 return dwarf2_format_32bit;
19723}
1575952e 19724#endif
73369e65
EC
19725
19726int
19727mips_dwarf2_addr_size (void)
19728{
6b6b3450 19729 if (HAVE_64BIT_OBJECTS)
73369e65 19730 return 8;
73369e65
EC
19731 else
19732 return 4;
19733}
5862107c
EC
19734
19735/* Standard calling conventions leave the CFA at SP on entry. */
19736void
19737mips_cfi_frame_initial_instructions (void)
19738{
19739 cfi_add_CFA_def_cfa_register (SP);
19740}
19741
707bfff6
TS
19742int
19743tc_mips_regname_to_dw2regnum (char *regname)
19744{
19745 unsigned int regnum = -1;
19746 unsigned int reg;
19747
19748 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19749 regnum = reg;
19750
19751 return regnum;
19752}
This page took 2.456115 seconds and 4 git commands to generate.