gas/testsuite/
[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
df58fc94
RS
782/* reg type m: 0, 17, 2, 3, 16, 18, 19, 20. */
783static const int mips32_to_micromips_reg_m_map[] =
784{
785 0, X, 2, 3, X, X, X, X,
786 X, X, X, X, X, X, X, X,
787 4, 1, 5, 6, 7, X, X, X,
788 X, X, X, X, X, X, X, X
789};
790
791/* reg type q: 0, 2-7. 17. */
792static const int mips32_to_micromips_reg_q_map[] =
793{
794 0, X, 2, 3, 4, 5, 6, 7,
795 X, X, X, X, X, X, X, X,
796 X, 1, X, X, X, X, X, X,
797 X, X, X, X, X, X, X, X
798};
799
800#define mips32_to_micromips_reg_n_map mips32_to_micromips_reg_m_map
801#undef X
802
803/* Map microMIPS register numbers to normal MIPS register numbers. */
804
805#define micromips_to_32_reg_b_map mips16_to_32_reg_map
806#define micromips_to_32_reg_c_map mips16_to_32_reg_map
807#define micromips_to_32_reg_d_map mips16_to_32_reg_map
808#define micromips_to_32_reg_e_map mips16_to_32_reg_map
809#define micromips_to_32_reg_f_map mips16_to_32_reg_map
810#define micromips_to_32_reg_g_map mips16_to_32_reg_map
811
812/* The microMIPS registers with type h. */
e76ff5ab 813static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
814{
815 5, 5, 6, 4, 4, 4, 4, 4
816};
e76ff5ab 817static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
818{
819 6, 7, 7, 21, 22, 5, 6, 7
820};
821
822#define micromips_to_32_reg_l_map mips16_to_32_reg_map
823
824/* The microMIPS registers with type m. */
825static const unsigned int micromips_to_32_reg_m_map[] =
826{
827 0, 17, 2, 3, 16, 18, 19, 20
828};
829
830#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
831
832/* The microMIPS registers with type q. */
833static const unsigned int micromips_to_32_reg_q_map[] =
834{
835 0, 17, 2, 3, 4, 5, 6, 7
836};
837
838/* microMIPS imm type B. */
839static const int micromips_imm_b_map[] =
840{
841 1, 4, 8, 12, 16, 20, 24, -1
842};
843
844/* microMIPS imm type C. */
845static const int micromips_imm_c_map[] =
846{
847 128, 1, 2, 3, 4, 7, 8, 15, 16, 31, 32, 63, 64, 255, 32768, 65535
848};
849
71400594
RS
850/* Classifies the kind of instructions we're interested in when
851 implementing -mfix-vr4120. */
c67a084a
NC
852enum fix_vr4120_class
853{
71400594
RS
854 FIX_VR4120_MACC,
855 FIX_VR4120_DMACC,
856 FIX_VR4120_MULT,
857 FIX_VR4120_DMULT,
858 FIX_VR4120_DIV,
859 FIX_VR4120_MTHILO,
860 NUM_FIX_VR4120_CLASSES
861};
862
c67a084a
NC
863/* ...likewise -mfix-loongson2f-jump. */
864static bfd_boolean mips_fix_loongson2f_jump;
865
866/* ...likewise -mfix-loongson2f-nop. */
867static bfd_boolean mips_fix_loongson2f_nop;
868
869/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
870static bfd_boolean mips_fix_loongson2f;
871
71400594
RS
872/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
873 there must be at least one other instruction between an instruction
874 of type X and an instruction of type Y. */
875static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
876
877/* True if -mfix-vr4120 is in force. */
d766e8ec 878static int mips_fix_vr4120;
4a6a3df4 879
7d8e00cf
RS
880/* ...likewise -mfix-vr4130. */
881static int mips_fix_vr4130;
882
6a32d874
CM
883/* ...likewise -mfix-24k. */
884static int mips_fix_24k;
885
d954098f
DD
886/* ...likewise -mfix-cn63xxp1 */
887static bfd_boolean mips_fix_cn63xxp1;
888
4a6a3df4
AO
889/* We don't relax branches by default, since this causes us to expand
890 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
891 fail to compute the offset before expanding the macro to the most
892 efficient expansion. */
893
894static int mips_relax_branch;
252b5132 895\f
4d7206a2
RS
896/* The expansion of many macros depends on the type of symbol that
897 they refer to. For example, when generating position-dependent code,
898 a macro that refers to a symbol may have two different expansions,
899 one which uses GP-relative addresses and one which uses absolute
900 addresses. When generating SVR4-style PIC, a macro may have
901 different expansions for local and global symbols.
902
903 We handle these situations by generating both sequences and putting
904 them in variant frags. In position-dependent code, the first sequence
905 will be the GP-relative one and the second sequence will be the
906 absolute one. In SVR4 PIC, the first sequence will be for global
907 symbols and the second will be for local symbols.
908
584892a6
RS
909 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
910 SECOND are the lengths of the two sequences in bytes. These fields
911 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
912 the subtype has the following flags:
4d7206a2 913
584892a6
RS
914 RELAX_USE_SECOND
915 Set if it has been decided that we should use the second
916 sequence instead of the first.
917
918 RELAX_SECOND_LONGER
919 Set in the first variant frag if the macro's second implementation
920 is longer than its first. This refers to the macro as a whole,
921 not an individual relaxation.
922
923 RELAX_NOMACRO
924 Set in the first variant frag if the macro appeared in a .set nomacro
925 block and if one alternative requires a warning but the other does not.
926
927 RELAX_DELAY_SLOT
928 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
929 delay slot.
4d7206a2 930
df58fc94
RS
931 RELAX_DELAY_SLOT_16BIT
932 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
933 16-bit instruction.
934
935 RELAX_DELAY_SLOT_SIZE_FIRST
936 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
937 the macro is of the wrong size for the branch delay slot.
938
939 RELAX_DELAY_SLOT_SIZE_SECOND
940 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
941 the macro is of the wrong size for the branch delay slot.
942
4d7206a2
RS
943 The frag's "opcode" points to the first fixup for relaxable code.
944
945 Relaxable macros are generated using a sequence such as:
946
947 relax_start (SYMBOL);
948 ... generate first expansion ...
949 relax_switch ();
950 ... generate second expansion ...
951 relax_end ();
952
953 The code and fixups for the unwanted alternative are discarded
954 by md_convert_frag. */
584892a6 955#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 956
584892a6
RS
957#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
958#define RELAX_SECOND(X) ((X) & 0xff)
959#define RELAX_USE_SECOND 0x10000
960#define RELAX_SECOND_LONGER 0x20000
961#define RELAX_NOMACRO 0x40000
962#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
963#define RELAX_DELAY_SLOT_16BIT 0x100000
964#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
965#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 966
4a6a3df4
AO
967/* Branch without likely bit. If label is out of range, we turn:
968
969 beq reg1, reg2, label
970 delay slot
971
972 into
973
974 bne reg1, reg2, 0f
975 nop
976 j label
977 0: delay slot
978
979 with the following opcode replacements:
980
981 beq <-> bne
982 blez <-> bgtz
983 bltz <-> bgez
984 bc1f <-> bc1t
985
986 bltzal <-> bgezal (with jal label instead of j label)
987
988 Even though keeping the delay slot instruction in the delay slot of
989 the branch would be more efficient, it would be very tricky to do
990 correctly, because we'd have to introduce a variable frag *after*
991 the delay slot instruction, and expand that instead. Let's do it
992 the easy way for now, even if the branch-not-taken case now costs
993 one additional instruction. Out-of-range branches are not supposed
994 to be common, anyway.
995
996 Branch likely. If label is out of range, we turn:
997
998 beql reg1, reg2, label
999 delay slot (annulled if branch not taken)
1000
1001 into
1002
1003 beql reg1, reg2, 1f
1004 nop
1005 beql $0, $0, 2f
1006 nop
1007 1: j[al] label
1008 delay slot (executed only if branch taken)
1009 2:
1010
1011 It would be possible to generate a shorter sequence by losing the
1012 likely bit, generating something like:
b34976b6 1013
4a6a3df4
AO
1014 bne reg1, reg2, 0f
1015 nop
1016 j[al] label
1017 delay slot (executed only if branch taken)
1018 0:
1019
1020 beql -> bne
1021 bnel -> beq
1022 blezl -> bgtz
1023 bgtzl -> blez
1024 bltzl -> bgez
1025 bgezl -> bltz
1026 bc1fl -> bc1t
1027 bc1tl -> bc1f
1028
1029 bltzall -> bgezal (with jal label instead of j label)
1030 bgezall -> bltzal (ditto)
1031
1032
1033 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1034#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1035 ((relax_substateT) \
1036 (0xc0000000 \
1037 | ((at) & 0x1f) \
1038 | ((toofar) ? 0x20 : 0) \
1039 | ((link) ? 0x40 : 0) \
1040 | ((likely) ? 0x80 : 0) \
1041 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1042#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1043#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1044#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1045#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1046#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1047#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1048
252b5132
RH
1049/* For mips16 code, we use an entirely different form of relaxation.
1050 mips16 supports two versions of most instructions which take
1051 immediate values: a small one which takes some small value, and a
1052 larger one which takes a 16 bit value. Since branches also follow
1053 this pattern, relaxing these values is required.
1054
1055 We can assemble both mips16 and normal MIPS code in a single
1056 object. Therefore, we need to support this type of relaxation at
1057 the same time that we support the relaxation described above. We
1058 use the high bit of the subtype field to distinguish these cases.
1059
1060 The information we store for this type of relaxation is the
1061 argument code found in the opcode file for this relocation, whether
1062 the user explicitly requested a small or extended form, and whether
1063 the relocation is in a jump or jal delay slot. That tells us the
1064 size of the value, and how it should be stored. We also store
1065 whether the fragment is considered to be extended or not. We also
1066 store whether this is known to be a branch to a different section,
1067 whether we have tried to relax this frag yet, and whether we have
1068 ever extended a PC relative fragment because of a shift count. */
1069#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1070 (0x80000000 \
1071 | ((type) & 0xff) \
1072 | ((small) ? 0x100 : 0) \
1073 | ((ext) ? 0x200 : 0) \
1074 | ((dslot) ? 0x400 : 0) \
1075 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1076#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1077#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1078#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1079#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1080#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1081#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1082#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1083#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1084#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1085#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1086#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1087#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1088
df58fc94
RS
1089/* For microMIPS code, we use relaxation similar to one we use for
1090 MIPS16 code. Some instructions that take immediate values support
1091 two encodings: a small one which takes some small value, and a
1092 larger one which takes a 16 bit value. As some branches also follow
1093 this pattern, relaxing these values is required.
1094
1095 We can assemble both microMIPS and normal MIPS code in a single
1096 object. Therefore, we need to support this type of relaxation at
1097 the same time that we support the relaxation described above. We
1098 use one of the high bits of the subtype field to distinguish these
1099 cases.
1100
1101 The information we store for this type of relaxation is the argument
1102 code found in the opcode file for this relocation, the register
40209cad
MR
1103 selected as the assembler temporary, whether the branch is
1104 unconditional, whether it is compact, whether it stores the link
1105 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1106 branches to a sequence of instructions is enabled, and whether the
1107 displacement of a branch is too large to fit as an immediate argument
1108 of a 16-bit and a 32-bit branch, respectively. */
1109#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1110 relax32, toofar16, toofar32) \
1111 (0x40000000 \
1112 | ((type) & 0xff) \
1113 | (((at) & 0x1f) << 8) \
1114 | ((uncond) ? 0x2000 : 0) \
1115 | ((compact) ? 0x4000 : 0) \
1116 | ((link) ? 0x8000 : 0) \
1117 | ((relax32) ? 0x10000 : 0) \
1118 | ((toofar16) ? 0x20000 : 0) \
1119 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1120#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1121#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1122#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1123#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1124#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1125#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1126#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1127
1128#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1129#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1130#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1131#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1132#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1133#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1134
43c0598f
RS
1135/* Sign-extend 16-bit value X. */
1136#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1137
885add95
CD
1138/* Is the given value a sign-extended 32-bit value? */
1139#define IS_SEXT_32BIT_NUM(x) \
1140 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1141 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1142
1143/* Is the given value a sign-extended 16-bit value? */
1144#define IS_SEXT_16BIT_NUM(x) \
1145 (((x) &~ (offsetT) 0x7fff) == 0 \
1146 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1147
df58fc94
RS
1148/* Is the given value a sign-extended 12-bit value? */
1149#define IS_SEXT_12BIT_NUM(x) \
1150 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1151
7f3c4072
CM
1152/* Is the given value a sign-extended 9-bit value? */
1153#define IS_SEXT_9BIT_NUM(x) \
1154 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1155
2051e8c4
MR
1156/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1157#define IS_ZEXT_32BIT_NUM(x) \
1158 (((x) &~ (offsetT) 0xffffffff) == 0 \
1159 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1160
bf12938e
RS
1161/* Replace bits MASK << SHIFT of STRUCT with the equivalent bits in
1162 VALUE << SHIFT. VALUE is evaluated exactly once. */
1163#define INSERT_BITS(STRUCT, VALUE, MASK, SHIFT) \
1164 (STRUCT) = (((STRUCT) & ~((MASK) << (SHIFT))) \
1165 | (((VALUE) & (MASK)) << (SHIFT)))
1166
1167/* Extract bits MASK << SHIFT from STRUCT and shift them right
1168 SHIFT places. */
1169#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1170 (((STRUCT) >> (SHIFT)) & (MASK))
1171
1172/* Change INSN's opcode so that the operand given by FIELD has value VALUE.
1173 INSN is a mips_cl_insn structure and VALUE is evaluated exactly once.
1174
1175 include/opcode/mips.h specifies operand fields using the macros
1176 OP_MASK_<FIELD> and OP_SH_<FIELD>. The MIPS16 equivalents start
1177 with "MIPS16OP" instead of "OP". */
df58fc94
RS
1178#define INSERT_OPERAND(MICROMIPS, FIELD, INSN, VALUE) \
1179 do \
1180 if (!(MICROMIPS)) \
1181 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1182 OP_MASK_##FIELD, OP_SH_##FIELD); \
1183 else \
1184 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1185 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD); \
1186 while (0)
bf12938e
RS
1187#define MIPS16_INSERT_OPERAND(FIELD, INSN, VALUE) \
1188 INSERT_BITS ((INSN).insn_opcode, VALUE, \
1189 MIPS16OP_MASK_##FIELD, MIPS16OP_SH_##FIELD)
1190
1191/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1192#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1193 (!(MICROMIPS) \
1194 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1195 : EXTRACT_BITS ((INSN).insn_opcode, \
1196 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1197#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1198 EXTRACT_BITS ((INSN).insn_opcode, \
1199 MIPS16OP_MASK_##FIELD, \
1200 MIPS16OP_SH_##FIELD)
5c04167a
RS
1201
1202/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1203#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1204\f
df58fc94
RS
1205/* Whether or not we are emitting a branch-likely macro. */
1206static bfd_boolean emit_branch_likely_macro = FALSE;
1207
4d7206a2
RS
1208/* Global variables used when generating relaxable macros. See the
1209 comment above RELAX_ENCODE for more details about how relaxation
1210 is used. */
1211static struct {
1212 /* 0 if we're not emitting a relaxable macro.
1213 1 if we're emitting the first of the two relaxation alternatives.
1214 2 if we're emitting the second alternative. */
1215 int sequence;
1216
1217 /* The first relaxable fixup in the current frag. (In other words,
1218 the first fixup that refers to relaxable code.) */
1219 fixS *first_fixup;
1220
1221 /* sizes[0] says how many bytes of the first alternative are stored in
1222 the current frag. Likewise sizes[1] for the second alternative. */
1223 unsigned int sizes[2];
1224
1225 /* The symbol on which the choice of sequence depends. */
1226 symbolS *symbol;
1227} mips_relax;
252b5132 1228\f
584892a6
RS
1229/* Global variables used to decide whether a macro needs a warning. */
1230static struct {
1231 /* True if the macro is in a branch delay slot. */
1232 bfd_boolean delay_slot_p;
1233
df58fc94
RS
1234 /* Set to the length in bytes required if the macro is in a delay slot
1235 that requires a specific length of instruction, otherwise zero. */
1236 unsigned int delay_slot_length;
1237
584892a6
RS
1238 /* For relaxable macros, sizes[0] is the length of the first alternative
1239 in bytes and sizes[1] is the length of the second alternative.
1240 For non-relaxable macros, both elements give the length of the
1241 macro in bytes. */
1242 unsigned int sizes[2];
1243
df58fc94
RS
1244 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1245 instruction of the first alternative in bytes and first_insn_sizes[1]
1246 is the length of the first instruction of the second alternative.
1247 For non-relaxable macros, both elements give the length of the first
1248 instruction in bytes.
1249
1250 Set to zero if we haven't yet seen the first instruction. */
1251 unsigned int first_insn_sizes[2];
1252
1253 /* For relaxable macros, insns[0] is the number of instructions for the
1254 first alternative and insns[1] is the number of instructions for the
1255 second alternative.
1256
1257 For non-relaxable macros, both elements give the number of
1258 instructions for the macro. */
1259 unsigned int insns[2];
1260
584892a6
RS
1261 /* The first variant frag for this macro. */
1262 fragS *first_frag;
1263} mips_macro_warning;
1264\f
252b5132
RH
1265/* Prototypes for static functions. */
1266
252b5132
RH
1267enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1268
b34976b6 1269static void append_insn
df58fc94
RS
1270 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1271 bfd_boolean expansionp);
7d10b47d 1272static void mips_no_prev_insn (void);
c67a084a 1273static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1274static void mips16_macro_build
03ea81db 1275 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1276static void load_register (int, expressionS *, int);
584892a6
RS
1277static void macro_start (void);
1278static void macro_end (void);
833794fc 1279static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1280static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1281static void mips_ip (char *str, struct mips_cl_insn * ip);
1282static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1283static void mips16_immed
43c0598f
RS
1284 (char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
1285 unsigned int, unsigned long *);
5e0116d5 1286static size_t my_getSmallExpression
17a2f251
TS
1287 (expressionS *, bfd_reloc_code_real_type *, char *);
1288static void my_getExpression (expressionS *, char *);
1289static void s_align (int);
1290static void s_change_sec (int);
1291static void s_change_section (int);
1292static void s_cons (int);
1293static void s_float_cons (int);
1294static void s_mips_globl (int);
1295static void s_option (int);
1296static void s_mipsset (int);
1297static void s_abicalls (int);
1298static void s_cpload (int);
1299static void s_cpsetup (int);
1300static void s_cplocal (int);
1301static void s_cprestore (int);
1302static void s_cpreturn (int);
741d6ea8
JM
1303static void s_dtprelword (int);
1304static void s_dtpreldword (int);
d0f13682
CLT
1305static void s_tprelword (int);
1306static void s_tpreldword (int);
17a2f251
TS
1307static void s_gpvalue (int);
1308static void s_gpword (int);
1309static void s_gpdword (int);
a3f278e2 1310static void s_ehword (int);
17a2f251
TS
1311static void s_cpadd (int);
1312static void s_insn (int);
1313static void md_obj_begin (void);
1314static void md_obj_end (void);
1315static void s_mips_ent (int);
1316static void s_mips_end (int);
1317static void s_mips_frame (int);
1318static void s_mips_mask (int reg_type);
1319static void s_mips_stab (int);
1320static void s_mips_weakext (int);
1321static void s_mips_file (int);
1322static void s_mips_loc (int);
1323static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1324static int relaxed_branch_length (fragS *, asection *, int);
17a2f251 1325static int validate_mips_insn (const struct mips_opcode *);
df58fc94
RS
1326static int validate_micromips_insn (const struct mips_opcode *);
1327static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1328static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
e7af610e
NC
1329
1330/* Table and functions used to map between CPU/ISA names, and
1331 ISA levels, and CPU numbers. */
1332
e972090a
NC
1333struct mips_cpu_info
1334{
e7af610e 1335 const char *name; /* CPU or ISA name. */
d16afab6
RS
1336 int flags; /* MIPS_CPU_* flags. */
1337 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1338 int isa; /* ISA level. */
1339 int cpu; /* CPU number (default CPU if ISA). */
1340};
1341
ad3fea08 1342#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1343
17a2f251
TS
1344static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1345static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1346static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1347\f
c31f3936
RS
1348/* Command-line options. */
1349const char *md_shortopts = "O::g::G:";
1350
1351enum options
1352 {
1353 OPTION_MARCH = OPTION_MD_BASE,
1354 OPTION_MTUNE,
1355 OPTION_MIPS1,
1356 OPTION_MIPS2,
1357 OPTION_MIPS3,
1358 OPTION_MIPS4,
1359 OPTION_MIPS5,
1360 OPTION_MIPS32,
1361 OPTION_MIPS64,
1362 OPTION_MIPS32R2,
1363 OPTION_MIPS64R2,
1364 OPTION_MIPS16,
1365 OPTION_NO_MIPS16,
1366 OPTION_MIPS3D,
1367 OPTION_NO_MIPS3D,
1368 OPTION_MDMX,
1369 OPTION_NO_MDMX,
1370 OPTION_DSP,
1371 OPTION_NO_DSP,
1372 OPTION_MT,
1373 OPTION_NO_MT,
1374 OPTION_VIRT,
1375 OPTION_NO_VIRT,
1376 OPTION_SMARTMIPS,
1377 OPTION_NO_SMARTMIPS,
1378 OPTION_DSPR2,
1379 OPTION_NO_DSPR2,
1380 OPTION_EVA,
1381 OPTION_NO_EVA,
1382 OPTION_MICROMIPS,
1383 OPTION_NO_MICROMIPS,
1384 OPTION_MCU,
1385 OPTION_NO_MCU,
1386 OPTION_COMPAT_ARCH_BASE,
1387 OPTION_M4650,
1388 OPTION_NO_M4650,
1389 OPTION_M4010,
1390 OPTION_NO_M4010,
1391 OPTION_M4100,
1392 OPTION_NO_M4100,
1393 OPTION_M3900,
1394 OPTION_NO_M3900,
1395 OPTION_M7000_HILO_FIX,
1396 OPTION_MNO_7000_HILO_FIX,
1397 OPTION_FIX_24K,
1398 OPTION_NO_FIX_24K,
1399 OPTION_FIX_LOONGSON2F_JUMP,
1400 OPTION_NO_FIX_LOONGSON2F_JUMP,
1401 OPTION_FIX_LOONGSON2F_NOP,
1402 OPTION_NO_FIX_LOONGSON2F_NOP,
1403 OPTION_FIX_VR4120,
1404 OPTION_NO_FIX_VR4120,
1405 OPTION_FIX_VR4130,
1406 OPTION_NO_FIX_VR4130,
1407 OPTION_FIX_CN63XXP1,
1408 OPTION_NO_FIX_CN63XXP1,
1409 OPTION_TRAP,
1410 OPTION_BREAK,
1411 OPTION_EB,
1412 OPTION_EL,
1413 OPTION_FP32,
1414 OPTION_GP32,
1415 OPTION_CONSTRUCT_FLOATS,
1416 OPTION_NO_CONSTRUCT_FLOATS,
1417 OPTION_FP64,
1418 OPTION_GP64,
1419 OPTION_RELAX_BRANCH,
1420 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1421 OPTION_INSN32,
1422 OPTION_NO_INSN32,
c31f3936
RS
1423 OPTION_MSHARED,
1424 OPTION_MNO_SHARED,
1425 OPTION_MSYM32,
1426 OPTION_MNO_SYM32,
1427 OPTION_SOFT_FLOAT,
1428 OPTION_HARD_FLOAT,
1429 OPTION_SINGLE_FLOAT,
1430 OPTION_DOUBLE_FLOAT,
1431 OPTION_32,
c31f3936
RS
1432 OPTION_CALL_SHARED,
1433 OPTION_CALL_NONPIC,
1434 OPTION_NON_SHARED,
1435 OPTION_XGOT,
1436 OPTION_MABI,
1437 OPTION_N32,
1438 OPTION_64,
1439 OPTION_MDEBUG,
1440 OPTION_NO_MDEBUG,
1441 OPTION_PDR,
1442 OPTION_NO_PDR,
1443 OPTION_MVXWORKS_PIC,
c31f3936
RS
1444 OPTION_END_OF_ENUM
1445 };
1446
1447struct option md_longopts[] =
1448{
1449 /* Options which specify architecture. */
1450 {"march", required_argument, NULL, OPTION_MARCH},
1451 {"mtune", required_argument, NULL, OPTION_MTUNE},
1452 {"mips0", no_argument, NULL, OPTION_MIPS1},
1453 {"mips1", no_argument, NULL, OPTION_MIPS1},
1454 {"mips2", no_argument, NULL, OPTION_MIPS2},
1455 {"mips3", no_argument, NULL, OPTION_MIPS3},
1456 {"mips4", no_argument, NULL, OPTION_MIPS4},
1457 {"mips5", no_argument, NULL, OPTION_MIPS5},
1458 {"mips32", no_argument, NULL, OPTION_MIPS32},
1459 {"mips64", no_argument, NULL, OPTION_MIPS64},
1460 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
1461 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
1462
1463 /* Options which specify Application Specific Extensions (ASEs). */
1464 {"mips16", no_argument, NULL, OPTION_MIPS16},
1465 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1466 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1467 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1468 {"mdmx", no_argument, NULL, OPTION_MDMX},
1469 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1470 {"mdsp", no_argument, NULL, OPTION_DSP},
1471 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1472 {"mmt", no_argument, NULL, OPTION_MT},
1473 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1474 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1475 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1476 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1477 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
1478 {"meva", no_argument, NULL, OPTION_EVA},
1479 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1480 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1481 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1482 {"mmcu", no_argument, NULL, OPTION_MCU},
1483 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1484 {"mvirt", no_argument, NULL, OPTION_VIRT},
1485 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
1486
1487 /* Old-style architecture options. Don't add more of these. */
1488 {"m4650", no_argument, NULL, OPTION_M4650},
1489 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1490 {"m4010", no_argument, NULL, OPTION_M4010},
1491 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1492 {"m4100", no_argument, NULL, OPTION_M4100},
1493 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1494 {"m3900", no_argument, NULL, OPTION_M3900},
1495 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1496
1497 /* Options which enable bug fixes. */
1498 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1499 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1500 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1501 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1502 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1503 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1504 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1505 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1506 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1507 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1508 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1509 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1510 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
1511 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1512 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1513
1514 /* Miscellaneous options. */
1515 {"trap", no_argument, NULL, OPTION_TRAP},
1516 {"no-break", no_argument, NULL, OPTION_TRAP},
1517 {"break", no_argument, NULL, OPTION_BREAK},
1518 {"no-trap", no_argument, NULL, OPTION_BREAK},
1519 {"EB", no_argument, NULL, OPTION_EB},
1520 {"EL", no_argument, NULL, OPTION_EL},
1521 {"mfp32", no_argument, NULL, OPTION_FP32},
1522 {"mgp32", no_argument, NULL, OPTION_GP32},
1523 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1524 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1525 {"mfp64", no_argument, NULL, OPTION_FP64},
1526 {"mgp64", no_argument, NULL, OPTION_GP64},
1527 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1528 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1529 {"minsn32", no_argument, NULL, OPTION_INSN32},
1530 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1531 {"mshared", no_argument, NULL, OPTION_MSHARED},
1532 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1533 {"msym32", no_argument, NULL, OPTION_MSYM32},
1534 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1535 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1536 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1537 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1538 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
1539
1540 /* Strictly speaking this next option is ELF specific,
1541 but we allow it for other ports as well in order to
1542 make testing easier. */
1543 {"32", no_argument, NULL, OPTION_32},
1544
1545 /* ELF-specific options. */
c31f3936
RS
1546 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1547 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1548 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1549 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1550 {"xgot", no_argument, NULL, OPTION_XGOT},
1551 {"mabi", required_argument, NULL, OPTION_MABI},
1552 {"n32", no_argument, NULL, OPTION_N32},
1553 {"64", no_argument, NULL, OPTION_64},
1554 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1555 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1556 {"mpdr", no_argument, NULL, OPTION_PDR},
1557 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1558 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
c31f3936
RS
1559
1560 {NULL, no_argument, NULL, 0}
1561};
1562size_t md_longopts_size = sizeof (md_longopts);
1563\f
c6278170
RS
1564/* Information about either an Application Specific Extension or an
1565 optional architecture feature that, for simplicity, we treat in the
1566 same way as an ASE. */
1567struct mips_ase
1568{
1569 /* The name of the ASE, used in both the command-line and .set options. */
1570 const char *name;
1571
1572 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1573 and 64-bit architectures, the flags here refer to the subset that
1574 is available on both. */
1575 unsigned int flags;
1576
1577 /* The ASE_* flag used for instructions that are available on 64-bit
1578 architectures but that are not included in FLAGS. */
1579 unsigned int flags64;
1580
1581 /* The command-line options that turn the ASE on and off. */
1582 int option_on;
1583 int option_off;
1584
1585 /* The minimum required architecture revisions for MIPS32, MIPS64,
1586 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1587 int mips32_rev;
1588 int mips64_rev;
1589 int micromips32_rev;
1590 int micromips64_rev;
1591};
1592
1593/* A table of all supported ASEs. */
1594static const struct mips_ase mips_ases[] = {
1595 { "dsp", ASE_DSP, ASE_DSP64,
1596 OPTION_DSP, OPTION_NO_DSP,
1597 2, 2, 2, 2 },
1598
1599 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1600 OPTION_DSPR2, OPTION_NO_DSPR2,
1601 2, 2, 2, 2 },
1602
1603 { "eva", ASE_EVA, 0,
1604 OPTION_EVA, OPTION_NO_EVA,
1605 2, 2, 2, 2 },
1606
1607 { "mcu", ASE_MCU, 0,
1608 OPTION_MCU, OPTION_NO_MCU,
1609 2, 2, 2, 2 },
1610
1611 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1612 { "mdmx", ASE_MDMX, 0,
1613 OPTION_MDMX, OPTION_NO_MDMX,
1614 -1, 1, -1, -1 },
1615
1616 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1617 { "mips3d", ASE_MIPS3D, 0,
1618 OPTION_MIPS3D, OPTION_NO_MIPS3D,
1619 2, 1, -1, -1 },
1620
1621 { "mt", ASE_MT, 0,
1622 OPTION_MT, OPTION_NO_MT,
1623 2, 2, -1, -1 },
1624
1625 { "smartmips", ASE_SMARTMIPS, 0,
1626 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
1627 1, -1, -1, -1 },
1628
1629 { "virt", ASE_VIRT, ASE_VIRT64,
1630 OPTION_VIRT, OPTION_NO_VIRT,
1631 2, 2, 2, 2 }
1632};
1633
1634/* The set of ASEs that require -mfp64. */
1635#define FP64_ASES (ASE_MIPS3D | ASE_MDMX)
1636
1637/* Groups of ASE_* flags that represent different revisions of an ASE. */
1638static const unsigned int mips_ase_groups[] = {
1639 ASE_DSP | ASE_DSPR2
1640};
1641\f
252b5132
RH
1642/* Pseudo-op table.
1643
1644 The following pseudo-ops from the Kane and Heinrich MIPS book
1645 should be defined here, but are currently unsupported: .alias,
1646 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1647
1648 The following pseudo-ops from the Kane and Heinrich MIPS book are
1649 specific to the type of debugging information being generated, and
1650 should be defined by the object format: .aent, .begin, .bend,
1651 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1652 .vreg.
1653
1654 The following pseudo-ops from the Kane and Heinrich MIPS book are
1655 not MIPS CPU specific, but are also not specific to the object file
1656 format. This file is probably the best place to define them, but
d84bcf09 1657 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1658
e972090a
NC
1659static const pseudo_typeS mips_pseudo_table[] =
1660{
beae10d5 1661 /* MIPS specific pseudo-ops. */
252b5132
RH
1662 {"option", s_option, 0},
1663 {"set", s_mipsset, 0},
1664 {"rdata", s_change_sec, 'r'},
1665 {"sdata", s_change_sec, 's'},
1666 {"livereg", s_ignore, 0},
1667 {"abicalls", s_abicalls, 0},
1668 {"cpload", s_cpload, 0},
6478892d
TS
1669 {"cpsetup", s_cpsetup, 0},
1670 {"cplocal", s_cplocal, 0},
252b5132 1671 {"cprestore", s_cprestore, 0},
6478892d 1672 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1673 {"dtprelword", s_dtprelword, 0},
1674 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1675 {"tprelword", s_tprelword, 0},
1676 {"tpreldword", s_tpreldword, 0},
6478892d 1677 {"gpvalue", s_gpvalue, 0},
252b5132 1678 {"gpword", s_gpword, 0},
10181a0d 1679 {"gpdword", s_gpdword, 0},
a3f278e2 1680 {"ehword", s_ehword, 0},
252b5132
RH
1681 {"cpadd", s_cpadd, 0},
1682 {"insn", s_insn, 0},
1683
beae10d5 1684 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1685 chips. */
38a57ae7 1686 {"asciiz", stringer, 8 + 1},
252b5132
RH
1687 {"bss", s_change_sec, 'b'},
1688 {"err", s_err, 0},
1689 {"half", s_cons, 1},
1690 {"dword", s_cons, 3},
1691 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1692 {"origin", s_org, 0},
1693 {"repeat", s_rept, 0},
252b5132 1694
998b3c36
MR
1695 /* For MIPS this is non-standard, but we define it for consistency. */
1696 {"sbss", s_change_sec, 'B'},
1697
beae10d5 1698 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1699 here for one reason or another. */
1700 {"align", s_align, 0},
1701 {"byte", s_cons, 0},
1702 {"data", s_change_sec, 'd'},
1703 {"double", s_float_cons, 'd'},
1704 {"float", s_float_cons, 'f'},
1705 {"globl", s_mips_globl, 0},
1706 {"global", s_mips_globl, 0},
1707 {"hword", s_cons, 1},
1708 {"int", s_cons, 2},
1709 {"long", s_cons, 2},
1710 {"octa", s_cons, 4},
1711 {"quad", s_cons, 3},
cca86cc8 1712 {"section", s_change_section, 0},
252b5132
RH
1713 {"short", s_cons, 1},
1714 {"single", s_float_cons, 'f'},
754e2bb9 1715 {"stabd", s_mips_stab, 'd'},
252b5132 1716 {"stabn", s_mips_stab, 'n'},
754e2bb9 1717 {"stabs", s_mips_stab, 's'},
252b5132
RH
1718 {"text", s_change_sec, 't'},
1719 {"word", s_cons, 2},
add56521 1720
add56521 1721 { "extern", ecoff_directive_extern, 0},
add56521 1722
43841e91 1723 { NULL, NULL, 0 },
252b5132
RH
1724};
1725
e972090a
NC
1726static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1727{
beae10d5
KH
1728 /* These pseudo-ops should be defined by the object file format.
1729 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1730 {"aent", s_mips_ent, 1},
1731 {"bgnb", s_ignore, 0},
1732 {"end", s_mips_end, 0},
1733 {"endb", s_ignore, 0},
1734 {"ent", s_mips_ent, 0},
c5dd6aab 1735 {"file", s_mips_file, 0},
252b5132
RH
1736 {"fmask", s_mips_mask, 'F'},
1737 {"frame", s_mips_frame, 0},
c5dd6aab 1738 {"loc", s_mips_loc, 0},
252b5132
RH
1739 {"mask", s_mips_mask, 'R'},
1740 {"verstamp", s_ignore, 0},
43841e91 1741 { NULL, NULL, 0 },
252b5132
RH
1742};
1743
3ae8dd8d
MR
1744/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1745 purpose of the `.dc.a' internal pseudo-op. */
1746
1747int
1748mips_address_bytes (void)
1749{
1750 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1751}
1752
17a2f251 1753extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1754
1755void
17a2f251 1756mips_pop_insert (void)
252b5132
RH
1757{
1758 pop_insert (mips_pseudo_table);
1759 if (! ECOFF_DEBUGGING)
1760 pop_insert (mips_nonecoff_pseudo_table);
1761}
1762\f
1763/* Symbols labelling the current insn. */
1764
e972090a
NC
1765struct insn_label_list
1766{
252b5132
RH
1767 struct insn_label_list *next;
1768 symbolS *label;
1769};
1770
252b5132 1771static struct insn_label_list *free_insn_labels;
742a56fe 1772#define label_list tc_segment_info_data.labels
252b5132 1773
17a2f251 1774static void mips_clear_insn_labels (void);
df58fc94
RS
1775static void mips_mark_labels (void);
1776static void mips_compressed_mark_labels (void);
252b5132
RH
1777
1778static inline void
17a2f251 1779mips_clear_insn_labels (void)
252b5132
RH
1780{
1781 register struct insn_label_list **pl;
a8dbcb85 1782 segment_info_type *si;
252b5132 1783
a8dbcb85
TS
1784 if (now_seg)
1785 {
1786 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1787 ;
1788
1789 si = seg_info (now_seg);
1790 *pl = si->label_list;
1791 si->label_list = NULL;
1792 }
252b5132 1793}
a8dbcb85 1794
df58fc94
RS
1795/* Mark instruction labels in MIPS16/microMIPS mode. */
1796
1797static inline void
1798mips_mark_labels (void)
1799{
1800 if (HAVE_CODE_COMPRESSION)
1801 mips_compressed_mark_labels ();
1802}
252b5132
RH
1803\f
1804static char *expr_end;
1805
1806/* Expressions which appear in instructions. These are set by
1807 mips_ip. */
1808
1809static expressionS imm_expr;
5f74bc13 1810static expressionS imm2_expr;
252b5132
RH
1811static expressionS offset_expr;
1812
1813/* Relocs associated with imm_expr and offset_expr. */
1814
f6688943
TS
1815static bfd_reloc_code_real_type imm_reloc[3]
1816 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
1817static bfd_reloc_code_real_type offset_reloc[3]
1818 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1819
df58fc94
RS
1820/* This is set to the resulting size of the instruction to be produced
1821 by mips16_ip if an explicit extension is used or by mips_ip if an
1822 explicit size is supplied. */
252b5132 1823
df58fc94 1824static unsigned int forced_insn_length;
252b5132 1825
e1b47bd5
RS
1826/* True if we are assembling an instruction. All dot symbols defined during
1827 this time should be treated as code labels. */
1828
1829static bfd_boolean mips_assembling_insn;
1830
ecb4347a
DJ
1831/* The pdr segment for per procedure frame/regmask info. Not used for
1832 ECOFF debugging. */
252b5132
RH
1833
1834static segT pdr_seg;
252b5132 1835
e013f690
TS
1836/* The default target format to use. */
1837
aeffff67
RS
1838#if defined (TE_FreeBSD)
1839#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1840#elif defined (TE_TMIPS)
1841#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1842#else
1843#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1844#endif
1845
e013f690 1846const char *
17a2f251 1847mips_target_format (void)
e013f690
TS
1848{
1849 switch (OUTPUT_FLAVOR)
1850 {
e013f690 1851 case bfd_target_elf_flavour:
0a44bf69
RS
1852#ifdef TE_VXWORKS
1853 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1854 return (target_big_endian
1855 ? "elf32-bigmips-vxworks"
1856 : "elf32-littlemips-vxworks");
1857#endif
e013f690 1858 return (target_big_endian
cfe86eaa 1859 ? (HAVE_64BIT_OBJECTS
aeffff67 1860 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1861 : (HAVE_NEWABI
aeffff67
RS
1862 ? ELF_TARGET ("elf32-n", "big")
1863 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1864 : (HAVE_64BIT_OBJECTS
aeffff67 1865 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1866 : (HAVE_NEWABI
aeffff67
RS
1867 ? ELF_TARGET ("elf32-n", "little")
1868 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1869 default:
1870 abort ();
1871 return NULL;
1872 }
1873}
1874
c6278170
RS
1875/* Return the ISA revision that is currently in use, or 0 if we are
1876 generating code for MIPS V or below. */
1877
1878static int
1879mips_isa_rev (void)
1880{
1881 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1882 return 2;
1883
1884 /* microMIPS implies revision 2 or above. */
1885 if (mips_opts.micromips)
1886 return 2;
1887
1888 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1889 return 1;
1890
1891 return 0;
1892}
1893
1894/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1895
1896static unsigned int
1897mips_ase_mask (unsigned int flags)
1898{
1899 unsigned int i;
1900
1901 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
1902 if (flags & mips_ase_groups[i])
1903 flags |= mips_ase_groups[i];
1904 return flags;
1905}
1906
1907/* Check whether the current ISA supports ASE. Issue a warning if
1908 appropriate. */
1909
1910static void
1911mips_check_isa_supports_ase (const struct mips_ase *ase)
1912{
1913 const char *base;
1914 int min_rev, size;
1915 static unsigned int warned_isa;
1916 static unsigned int warned_fp32;
1917
1918 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
1919 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
1920 else
1921 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
1922 if ((min_rev < 0 || mips_isa_rev () < min_rev)
1923 && (warned_isa & ase->flags) != ase->flags)
1924 {
1925 warned_isa |= ase->flags;
1926 base = mips_opts.micromips ? "microMIPS" : "MIPS";
1927 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
1928 if (min_rev < 0)
1929 as_warn (_("The %d-bit %s architecture does not support the"
1930 " `%s' extension"), size, base, ase->name);
1931 else
1932 as_warn (_("The `%s' extension requires %s%d revision %d or greater"),
1933 ase->name, base, size, min_rev);
1934 }
1935 if ((ase->flags & FP64_ASES)
1936 && mips_opts.fp32
1937 && (warned_fp32 & ase->flags) != ase->flags)
1938 {
1939 warned_fp32 |= ase->flags;
1940 as_warn (_("The `%s' extension requires 64-bit FPRs"), ase->name);
1941 }
1942}
1943
1944/* Check all enabled ASEs to see whether they are supported by the
1945 chosen architecture. */
1946
1947static void
1948mips_check_isa_supports_ases (void)
1949{
1950 unsigned int i, mask;
1951
1952 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1953 {
1954 mask = mips_ase_mask (mips_ases[i].flags);
1955 if ((mips_opts.ase & mask) == mips_ases[i].flags)
1956 mips_check_isa_supports_ase (&mips_ases[i]);
1957 }
1958}
1959
1960/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
1961 that were affected. */
1962
1963static unsigned int
1964mips_set_ase (const struct mips_ase *ase, bfd_boolean enabled_p)
1965{
1966 unsigned int mask;
1967
1968 mask = mips_ase_mask (ase->flags);
1969 mips_opts.ase &= ~mask;
1970 if (enabled_p)
1971 mips_opts.ase |= ase->flags;
1972 return mask;
1973}
1974
1975/* Return the ASE called NAME, or null if none. */
1976
1977static const struct mips_ase *
1978mips_lookup_ase (const char *name)
1979{
1980 unsigned int i;
1981
1982 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
1983 if (strcmp (name, mips_ases[i].name) == 0)
1984 return &mips_ases[i];
1985 return NULL;
1986}
1987
df58fc94
RS
1988/* Return the length of a microMIPS instruction in bytes. If bits of
1989 the mask beyond the low 16 are 0, then it is a 16-bit instruction.
1990 Otherwise assume a 32-bit instruction; 48-bit instructions (0x1f
1991 major opcode) will require further modifications to the opcode
1992 table. */
1993
1994static inline unsigned int
1995micromips_insn_length (const struct mips_opcode *mo)
1996{
1997 return (mo->mask >> 16) == 0 ? 2 : 4;
1998}
1999
5c04167a
RS
2000/* Return the length of MIPS16 instruction OPCODE. */
2001
2002static inline unsigned int
2003mips16_opcode_length (unsigned long opcode)
2004{
2005 return (opcode >> 16) == 0 ? 2 : 4;
2006}
2007
1e915849
RS
2008/* Return the length of instruction INSN. */
2009
2010static inline unsigned int
2011insn_length (const struct mips_cl_insn *insn)
2012{
df58fc94
RS
2013 if (mips_opts.micromips)
2014 return micromips_insn_length (insn->insn_mo);
2015 else if (mips_opts.mips16)
5c04167a 2016 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2017 else
1e915849 2018 return 4;
1e915849
RS
2019}
2020
2021/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2022
2023static void
2024create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2025{
2026 size_t i;
2027
2028 insn->insn_mo = mo;
1e915849
RS
2029 insn->insn_opcode = mo->match;
2030 insn->frag = NULL;
2031 insn->where = 0;
2032 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2033 insn->fixp[i] = NULL;
2034 insn->fixed_p = (mips_opts.noreorder > 0);
2035 insn->noreorder_p = (mips_opts.noreorder > 0);
2036 insn->mips16_absolute_jump_p = 0;
15be625d 2037 insn->complete_p = 0;
e407c74b 2038 insn->cleared_p = 0;
1e915849
RS
2039}
2040
df58fc94 2041/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2042
2043static void
df58fc94 2044mips_record_compressed_mode (void)
742a56fe
RS
2045{
2046 segment_info_type *si;
2047
2048 si = seg_info (now_seg);
2049 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2050 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2051 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2052 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2053}
2054
4d68580a
RS
2055/* Read a standard MIPS instruction from BUF. */
2056
2057static unsigned long
2058read_insn (char *buf)
2059{
2060 if (target_big_endian)
2061 return bfd_getb32 ((bfd_byte *) buf);
2062 else
2063 return bfd_getl32 ((bfd_byte *) buf);
2064}
2065
2066/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2067 the next byte. */
2068
2069static char *
2070write_insn (char *buf, unsigned int insn)
2071{
2072 md_number_to_chars (buf, insn, 4);
2073 return buf + 4;
2074}
2075
2076/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2077 has length LENGTH. */
2078
2079static unsigned long
2080read_compressed_insn (char *buf, unsigned int length)
2081{
2082 unsigned long insn;
2083 unsigned int i;
2084
2085 insn = 0;
2086 for (i = 0; i < length; i += 2)
2087 {
2088 insn <<= 16;
2089 if (target_big_endian)
2090 insn |= bfd_getb16 ((char *) buf);
2091 else
2092 insn |= bfd_getl16 ((char *) buf);
2093 buf += 2;
2094 }
2095 return insn;
2096}
2097
5c04167a
RS
2098/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2099 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2100
2101static char *
2102write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2103{
2104 unsigned int i;
2105
2106 for (i = 0; i < length; i += 2)
2107 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2108 return buf + length;
2109}
2110
1e915849
RS
2111/* Install INSN at the location specified by its "frag" and "where" fields. */
2112
2113static void
2114install_insn (const struct mips_cl_insn *insn)
2115{
2116 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2117 if (HAVE_CODE_COMPRESSION)
2118 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2119 else
4d68580a 2120 write_insn (f, insn->insn_opcode);
df58fc94 2121 mips_record_compressed_mode ();
1e915849
RS
2122}
2123
2124/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2125 and install the opcode in the new location. */
2126
2127static void
2128move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2129{
2130 size_t i;
2131
2132 insn->frag = frag;
2133 insn->where = where;
2134 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2135 if (insn->fixp[i] != NULL)
2136 {
2137 insn->fixp[i]->fx_frag = frag;
2138 insn->fixp[i]->fx_where = where;
2139 }
2140 install_insn (insn);
2141}
2142
2143/* Add INSN to the end of the output. */
2144
2145static void
2146add_fixed_insn (struct mips_cl_insn *insn)
2147{
2148 char *f = frag_more (insn_length (insn));
2149 move_insn (insn, frag_now, f - frag_now->fr_literal);
2150}
2151
2152/* Start a variant frag and move INSN to the start of the variant part,
2153 marking it as fixed. The other arguments are as for frag_var. */
2154
2155static void
2156add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2157 relax_substateT subtype, symbolS *symbol, offsetT offset)
2158{
2159 frag_grow (max_chars);
2160 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2161 insn->fixed_p = 1;
2162 frag_var (rs_machine_dependent, max_chars, var,
2163 subtype, symbol, offset, NULL);
2164}
2165
2166/* Insert N copies of INSN into the history buffer, starting at
2167 position FIRST. Neither FIRST nor N need to be clipped. */
2168
2169static void
2170insert_into_history (unsigned int first, unsigned int n,
2171 const struct mips_cl_insn *insn)
2172{
2173 if (mips_relax.sequence != 2)
2174 {
2175 unsigned int i;
2176
2177 for (i = ARRAY_SIZE (history); i-- > first;)
2178 if (i >= first + n)
2179 history[i] = history[i - n];
2180 else
2181 history[i] = *insn;
2182 }
2183}
2184
71400594
RS
2185/* Initialize vr4120_conflicts. There is a bit of duplication here:
2186 the idea is to make it obvious at a glance that each errata is
2187 included. */
2188
2189static void
2190init_vr4120_conflicts (void)
2191{
2192#define CONFLICT(FIRST, SECOND) \
2193 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2194
2195 /* Errata 21 - [D]DIV[U] after [D]MACC */
2196 CONFLICT (MACC, DIV);
2197 CONFLICT (DMACC, DIV);
2198
2199 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2200 CONFLICT (DMULT, DMULT);
2201 CONFLICT (DMULT, DMACC);
2202 CONFLICT (DMACC, DMULT);
2203 CONFLICT (DMACC, DMACC);
2204
2205 /* Errata 24 - MT{LO,HI} after [D]MACC */
2206 CONFLICT (MACC, MTHILO);
2207 CONFLICT (DMACC, MTHILO);
2208
2209 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2210 instruction is executed immediately after a MACC or DMACC
2211 instruction, the result of [either instruction] is incorrect." */
2212 CONFLICT (MACC, MULT);
2213 CONFLICT (MACC, DMULT);
2214 CONFLICT (DMACC, MULT);
2215 CONFLICT (DMACC, DMULT);
2216
2217 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2218 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2219 DDIV or DDIVU instruction, the result of the MACC or
2220 DMACC instruction is incorrect.". */
2221 CONFLICT (DMULT, MACC);
2222 CONFLICT (DMULT, DMACC);
2223 CONFLICT (DIV, MACC);
2224 CONFLICT (DIV, DMACC);
2225
2226#undef CONFLICT
2227}
2228
707bfff6
TS
2229struct regname {
2230 const char *name;
2231 unsigned int num;
2232};
2233
2234#define RTYPE_MASK 0x1ff00
2235#define RTYPE_NUM 0x00100
2236#define RTYPE_FPU 0x00200
2237#define RTYPE_FCC 0x00400
2238#define RTYPE_VEC 0x00800
2239#define RTYPE_GP 0x01000
2240#define RTYPE_CP0 0x02000
2241#define RTYPE_PC 0x04000
2242#define RTYPE_ACC 0x08000
2243#define RTYPE_CCC 0x10000
2244#define RNUM_MASK 0x000ff
2245#define RWARN 0x80000
2246
2247#define GENERIC_REGISTER_NUMBERS \
2248 {"$0", RTYPE_NUM | 0}, \
2249 {"$1", RTYPE_NUM | 1}, \
2250 {"$2", RTYPE_NUM | 2}, \
2251 {"$3", RTYPE_NUM | 3}, \
2252 {"$4", RTYPE_NUM | 4}, \
2253 {"$5", RTYPE_NUM | 5}, \
2254 {"$6", RTYPE_NUM | 6}, \
2255 {"$7", RTYPE_NUM | 7}, \
2256 {"$8", RTYPE_NUM | 8}, \
2257 {"$9", RTYPE_NUM | 9}, \
2258 {"$10", RTYPE_NUM | 10}, \
2259 {"$11", RTYPE_NUM | 11}, \
2260 {"$12", RTYPE_NUM | 12}, \
2261 {"$13", RTYPE_NUM | 13}, \
2262 {"$14", RTYPE_NUM | 14}, \
2263 {"$15", RTYPE_NUM | 15}, \
2264 {"$16", RTYPE_NUM | 16}, \
2265 {"$17", RTYPE_NUM | 17}, \
2266 {"$18", RTYPE_NUM | 18}, \
2267 {"$19", RTYPE_NUM | 19}, \
2268 {"$20", RTYPE_NUM | 20}, \
2269 {"$21", RTYPE_NUM | 21}, \
2270 {"$22", RTYPE_NUM | 22}, \
2271 {"$23", RTYPE_NUM | 23}, \
2272 {"$24", RTYPE_NUM | 24}, \
2273 {"$25", RTYPE_NUM | 25}, \
2274 {"$26", RTYPE_NUM | 26}, \
2275 {"$27", RTYPE_NUM | 27}, \
2276 {"$28", RTYPE_NUM | 28}, \
2277 {"$29", RTYPE_NUM | 29}, \
2278 {"$30", RTYPE_NUM | 30}, \
2279 {"$31", RTYPE_NUM | 31}
2280
2281#define FPU_REGISTER_NAMES \
2282 {"$f0", RTYPE_FPU | 0}, \
2283 {"$f1", RTYPE_FPU | 1}, \
2284 {"$f2", RTYPE_FPU | 2}, \
2285 {"$f3", RTYPE_FPU | 3}, \
2286 {"$f4", RTYPE_FPU | 4}, \
2287 {"$f5", RTYPE_FPU | 5}, \
2288 {"$f6", RTYPE_FPU | 6}, \
2289 {"$f7", RTYPE_FPU | 7}, \
2290 {"$f8", RTYPE_FPU | 8}, \
2291 {"$f9", RTYPE_FPU | 9}, \
2292 {"$f10", RTYPE_FPU | 10}, \
2293 {"$f11", RTYPE_FPU | 11}, \
2294 {"$f12", RTYPE_FPU | 12}, \
2295 {"$f13", RTYPE_FPU | 13}, \
2296 {"$f14", RTYPE_FPU | 14}, \
2297 {"$f15", RTYPE_FPU | 15}, \
2298 {"$f16", RTYPE_FPU | 16}, \
2299 {"$f17", RTYPE_FPU | 17}, \
2300 {"$f18", RTYPE_FPU | 18}, \
2301 {"$f19", RTYPE_FPU | 19}, \
2302 {"$f20", RTYPE_FPU | 20}, \
2303 {"$f21", RTYPE_FPU | 21}, \
2304 {"$f22", RTYPE_FPU | 22}, \
2305 {"$f23", RTYPE_FPU | 23}, \
2306 {"$f24", RTYPE_FPU | 24}, \
2307 {"$f25", RTYPE_FPU | 25}, \
2308 {"$f26", RTYPE_FPU | 26}, \
2309 {"$f27", RTYPE_FPU | 27}, \
2310 {"$f28", RTYPE_FPU | 28}, \
2311 {"$f29", RTYPE_FPU | 29}, \
2312 {"$f30", RTYPE_FPU | 30}, \
2313 {"$f31", RTYPE_FPU | 31}
2314
2315#define FPU_CONDITION_CODE_NAMES \
2316 {"$fcc0", RTYPE_FCC | 0}, \
2317 {"$fcc1", RTYPE_FCC | 1}, \
2318 {"$fcc2", RTYPE_FCC | 2}, \
2319 {"$fcc3", RTYPE_FCC | 3}, \
2320 {"$fcc4", RTYPE_FCC | 4}, \
2321 {"$fcc5", RTYPE_FCC | 5}, \
2322 {"$fcc6", RTYPE_FCC | 6}, \
2323 {"$fcc7", RTYPE_FCC | 7}
2324
2325#define COPROC_CONDITION_CODE_NAMES \
2326 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2327 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2328 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2329 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2330 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2331 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2332 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2333 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2334
2335#define N32N64_SYMBOLIC_REGISTER_NAMES \
2336 {"$a4", RTYPE_GP | 8}, \
2337 {"$a5", RTYPE_GP | 9}, \
2338 {"$a6", RTYPE_GP | 10}, \
2339 {"$a7", RTYPE_GP | 11}, \
2340 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2341 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2342 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2343 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2344 {"$t0", RTYPE_GP | 12}, \
2345 {"$t1", RTYPE_GP | 13}, \
2346 {"$t2", RTYPE_GP | 14}, \
2347 {"$t3", RTYPE_GP | 15}
2348
2349#define O32_SYMBOLIC_REGISTER_NAMES \
2350 {"$t0", RTYPE_GP | 8}, \
2351 {"$t1", RTYPE_GP | 9}, \
2352 {"$t2", RTYPE_GP | 10}, \
2353 {"$t3", RTYPE_GP | 11}, \
2354 {"$t4", RTYPE_GP | 12}, \
2355 {"$t5", RTYPE_GP | 13}, \
2356 {"$t6", RTYPE_GP | 14}, \
2357 {"$t7", RTYPE_GP | 15}, \
2358 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2359 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2360 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
2361 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
2362
2363/* Remaining symbolic register names */
2364#define SYMBOLIC_REGISTER_NAMES \
2365 {"$zero", RTYPE_GP | 0}, \
2366 {"$at", RTYPE_GP | 1}, \
2367 {"$AT", RTYPE_GP | 1}, \
2368 {"$v0", RTYPE_GP | 2}, \
2369 {"$v1", RTYPE_GP | 3}, \
2370 {"$a0", RTYPE_GP | 4}, \
2371 {"$a1", RTYPE_GP | 5}, \
2372 {"$a2", RTYPE_GP | 6}, \
2373 {"$a3", RTYPE_GP | 7}, \
2374 {"$s0", RTYPE_GP | 16}, \
2375 {"$s1", RTYPE_GP | 17}, \
2376 {"$s2", RTYPE_GP | 18}, \
2377 {"$s3", RTYPE_GP | 19}, \
2378 {"$s4", RTYPE_GP | 20}, \
2379 {"$s5", RTYPE_GP | 21}, \
2380 {"$s6", RTYPE_GP | 22}, \
2381 {"$s7", RTYPE_GP | 23}, \
2382 {"$t8", RTYPE_GP | 24}, \
2383 {"$t9", RTYPE_GP | 25}, \
2384 {"$k0", RTYPE_GP | 26}, \
2385 {"$kt0", RTYPE_GP | 26}, \
2386 {"$k1", RTYPE_GP | 27}, \
2387 {"$kt1", RTYPE_GP | 27}, \
2388 {"$gp", RTYPE_GP | 28}, \
2389 {"$sp", RTYPE_GP | 29}, \
2390 {"$s8", RTYPE_GP | 30}, \
2391 {"$fp", RTYPE_GP | 30}, \
2392 {"$ra", RTYPE_GP | 31}
2393
2394#define MIPS16_SPECIAL_REGISTER_NAMES \
2395 {"$pc", RTYPE_PC | 0}
2396
2397#define MDMX_VECTOR_REGISTER_NAMES \
2398 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2399 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2400 {"$v2", RTYPE_VEC | 2}, \
2401 {"$v3", RTYPE_VEC | 3}, \
2402 {"$v4", RTYPE_VEC | 4}, \
2403 {"$v5", RTYPE_VEC | 5}, \
2404 {"$v6", RTYPE_VEC | 6}, \
2405 {"$v7", RTYPE_VEC | 7}, \
2406 {"$v8", RTYPE_VEC | 8}, \
2407 {"$v9", RTYPE_VEC | 9}, \
2408 {"$v10", RTYPE_VEC | 10}, \
2409 {"$v11", RTYPE_VEC | 11}, \
2410 {"$v12", RTYPE_VEC | 12}, \
2411 {"$v13", RTYPE_VEC | 13}, \
2412 {"$v14", RTYPE_VEC | 14}, \
2413 {"$v15", RTYPE_VEC | 15}, \
2414 {"$v16", RTYPE_VEC | 16}, \
2415 {"$v17", RTYPE_VEC | 17}, \
2416 {"$v18", RTYPE_VEC | 18}, \
2417 {"$v19", RTYPE_VEC | 19}, \
2418 {"$v20", RTYPE_VEC | 20}, \
2419 {"$v21", RTYPE_VEC | 21}, \
2420 {"$v22", RTYPE_VEC | 22}, \
2421 {"$v23", RTYPE_VEC | 23}, \
2422 {"$v24", RTYPE_VEC | 24}, \
2423 {"$v25", RTYPE_VEC | 25}, \
2424 {"$v26", RTYPE_VEC | 26}, \
2425 {"$v27", RTYPE_VEC | 27}, \
2426 {"$v28", RTYPE_VEC | 28}, \
2427 {"$v29", RTYPE_VEC | 29}, \
2428 {"$v30", RTYPE_VEC | 30}, \
2429 {"$v31", RTYPE_VEC | 31}
2430
2431#define MIPS_DSP_ACCUMULATOR_NAMES \
2432 {"$ac0", RTYPE_ACC | 0}, \
2433 {"$ac1", RTYPE_ACC | 1}, \
2434 {"$ac2", RTYPE_ACC | 2}, \
2435 {"$ac3", RTYPE_ACC | 3}
2436
2437static const struct regname reg_names[] = {
2438 GENERIC_REGISTER_NUMBERS,
2439 FPU_REGISTER_NAMES,
2440 FPU_CONDITION_CODE_NAMES,
2441 COPROC_CONDITION_CODE_NAMES,
2442
2443 /* The $txx registers depends on the abi,
2444 these will be added later into the symbol table from
2445 one of the tables below once mips_abi is set after
2446 parsing of arguments from the command line. */
2447 SYMBOLIC_REGISTER_NAMES,
2448
2449 MIPS16_SPECIAL_REGISTER_NAMES,
2450 MDMX_VECTOR_REGISTER_NAMES,
2451 MIPS_DSP_ACCUMULATOR_NAMES,
2452 {0, 0}
2453};
2454
2455static const struct regname reg_names_o32[] = {
2456 O32_SYMBOLIC_REGISTER_NAMES,
2457 {0, 0}
2458};
2459
2460static const struct regname reg_names_n32n64[] = {
2461 N32N64_SYMBOLIC_REGISTER_NAMES,
2462 {0, 0}
2463};
2464
df58fc94
RS
2465/* Check if S points at a valid register specifier according to TYPES.
2466 If so, then return 1, advance S to consume the specifier and store
2467 the register's number in REGNOP, otherwise return 0. */
2468
707bfff6
TS
2469static int
2470reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2471{
2472 symbolS *symbolP;
2473 char *e;
2474 char save_c;
2475 int reg = -1;
2476
2477 /* Find end of name. */
2478 e = *s;
2479 if (is_name_beginner (*e))
2480 ++e;
2481 while (is_part_of_name (*e))
2482 ++e;
2483
2484 /* Terminate name. */
2485 save_c = *e;
2486 *e = '\0';
2487
2488 /* Look for a register symbol. */
2489 if ((symbolP = symbol_find (*s)) && S_GET_SEGMENT (symbolP) == reg_section)
2490 {
2491 int r = S_GET_VALUE (symbolP);
2492 if (r & types)
2493 reg = r & RNUM_MASK;
2494 else if ((types & RTYPE_VEC) && (r & ~1) == (RTYPE_GP | 2))
2495 /* Convert GP reg $v0/1 to MDMX reg $v0/1! */
2496 reg = (r & RNUM_MASK) - 2;
2497 }
2498 /* Else see if this is a register defined in an itbl entry. */
2499 else if ((types & RTYPE_GP) && itbl_have_entries)
2500 {
2501 char *n = *s;
2502 unsigned long r;
2503
2504 if (*n == '$')
2505 ++n;
2506 if (itbl_get_reg_val (n, &r))
2507 reg = r & RNUM_MASK;
2508 }
2509
2510 /* Advance to next token if a register was recognised. */
2511 if (reg >= 0)
2512 *s = e;
2513 else if (types & RWARN)
20203fb9 2514 as_warn (_("Unrecognized register name `%s'"), *s);
707bfff6
TS
2515
2516 *e = save_c;
2517 if (regnop)
2518 *regnop = reg;
2519 return reg >= 0;
2520}
2521
df58fc94
RS
2522/* Check if S points at a valid register list according to TYPES.
2523 If so, then return 1, advance S to consume the list and store
2524 the registers present on the list as a bitmask of ones in REGLISTP,
2525 otherwise return 0. A valid list comprises a comma-separated
2526 enumeration of valid single registers and/or dash-separated
2527 contiguous register ranges as determined by their numbers.
2528
2529 As a special exception if one of s0-s7 registers is specified as
2530 the range's lower delimiter and s8 (fp) is its upper one, then no
2531 registers whose numbers place them between s7 and s8 (i.e. $24-$29)
2309ddf2 2532 are selected; they have to be listed separately if needed. */
df58fc94
RS
2533
2534static int
2535reglist_lookup (char **s, unsigned int types, unsigned int *reglistp)
2536{
2537 unsigned int reglist = 0;
2538 unsigned int lastregno;
2539 bfd_boolean ok = TRUE;
2540 unsigned int regmask;
2309ddf2 2541 char *s_endlist = *s;
df58fc94 2542 char *s_reset = *s;
2309ddf2 2543 unsigned int regno;
df58fc94
RS
2544
2545 while (reg_lookup (s, types, &regno))
2546 {
2547 lastregno = regno;
2548 if (**s == '-')
2549 {
2550 (*s)++;
2551 ok = reg_lookup (s, types, &lastregno);
2552 if (ok && lastregno < regno)
2553 ok = FALSE;
2554 if (!ok)
2555 break;
2556 }
2557
2558 if (lastregno == FP && regno >= S0 && regno <= S7)
2559 {
2560 lastregno = S7;
2561 reglist |= 1 << FP;
2562 }
2563 regmask = 1 << lastregno;
2564 regmask = (regmask << 1) - 1;
2565 regmask ^= (1 << regno) - 1;
2566 reglist |= regmask;
2567
2309ddf2 2568 s_endlist = *s;
df58fc94
RS
2569 if (**s != ',')
2570 break;
2571 (*s)++;
2572 }
2573
2574 if (ok)
2309ddf2 2575 *s = s_endlist;
df58fc94
RS
2576 else
2577 *s = s_reset;
2578 if (reglistp)
2579 *reglistp = reglist;
2580 return ok && reglist != 0;
2581}
2582
e76ff5ab
RS
2583static unsigned int
2584mips_lookup_reg_pair (unsigned int regno1, unsigned int regno2,
2585 const unsigned int *map1, const unsigned int *map2,
2586 unsigned int count)
2587{
2588 unsigned int i;
2589
2590 for (i = 0; i < count; i++)
2591 if (map1[i] == regno1 && map2[i] == regno2)
2592 return i;
2593 return ILLEGAL_REG;
2594}
2595
d301a56b
RS
2596/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
2597 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
2598
2599static bfd_boolean
f79e2745 2600is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
2601{
2602 int isa = mips_opts.isa;
846ef2d0 2603 int ase = mips_opts.ase;
037b32b9 2604 int fp_s, fp_d;
c6278170 2605 unsigned int i;
037b32b9 2606
c6278170
RS
2607 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2608 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2609 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
2610 ase |= mips_ases[i].flags64;
037b32b9 2611
d301a56b 2612 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
2613 return FALSE;
2614
2615 /* Check whether the instruction or macro requires single-precision or
2616 double-precision floating-point support. Note that this information is
2617 stored differently in the opcode table for insns and macros. */
2618 if (mo->pinfo == INSN_MACRO)
2619 {
2620 fp_s = mo->pinfo2 & INSN2_M_FP_S;
2621 fp_d = mo->pinfo2 & INSN2_M_FP_D;
2622 }
2623 else
2624 {
2625 fp_s = mo->pinfo & FP_S;
2626 fp_d = mo->pinfo & FP_D;
2627 }
2628
2629 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
2630 return FALSE;
2631
2632 if (fp_s && mips_opts.soft_float)
2633 return FALSE;
2634
2635 return TRUE;
2636}
2637
2638/* Return TRUE if the MIPS16 opcode MO is valid on the currently
2639 selected ISA and architecture. */
2640
2641static bfd_boolean
2642is_opcode_valid_16 (const struct mips_opcode *mo)
2643{
d301a56b 2644 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
2645}
2646
df58fc94
RS
2647/* Return TRUE if the size of the microMIPS opcode MO matches one
2648 explicitly requested. Always TRUE in the standard MIPS mode. */
2649
2650static bfd_boolean
2651is_size_valid (const struct mips_opcode *mo)
2652{
2653 if (!mips_opts.micromips)
2654 return TRUE;
2655
833794fc
MR
2656 if (mips_opts.insn32)
2657 {
2658 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
2659 return FALSE;
2660 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
2661 return FALSE;
2662 }
df58fc94
RS
2663 if (!forced_insn_length)
2664 return TRUE;
2665 if (mo->pinfo == INSN_MACRO)
2666 return FALSE;
2667 return forced_insn_length == micromips_insn_length (mo);
2668}
2669
2670/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
2671 of the preceding instruction. Always TRUE in the standard MIPS mode.
2672
2673 We don't accept macros in 16-bit delay slots to avoid a case where
2674 a macro expansion fails because it relies on a preceding 32-bit real
2675 instruction to have matched and does not handle the operands correctly.
2676 The only macros that may expand to 16-bit instructions are JAL that
2677 cannot be placed in a delay slot anyway, and corner cases of BALIGN
2678 and BGT (that likewise cannot be placed in a delay slot) that decay to
2679 a NOP. In all these cases the macros precede any corresponding real
2680 instruction definitions in the opcode table, so they will match in the
2681 second pass where the size of the delay slot is ignored and therefore
2682 produce correct code. */
df58fc94
RS
2683
2684static bfd_boolean
2685is_delay_slot_valid (const struct mips_opcode *mo)
2686{
2687 if (!mips_opts.micromips)
2688 return TRUE;
2689
2690 if (mo->pinfo == INSN_MACRO)
c06dec14 2691 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
2692 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
2693 && micromips_insn_length (mo) != 4)
2694 return FALSE;
2695 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
2696 && micromips_insn_length (mo) != 2)
2697 return FALSE;
2698
2699 return TRUE;
2700}
2701
707bfff6
TS
2702/* This function is called once, at assembler startup time. It should set up
2703 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 2704
252b5132 2705void
17a2f251 2706md_begin (void)
252b5132 2707{
3994f87e 2708 const char *retval = NULL;
156c2f8b 2709 int i = 0;
252b5132 2710 int broken = 0;
1f25f5d3 2711
0a44bf69
RS
2712 if (mips_pic != NO_PIC)
2713 {
2714 if (g_switch_seen && g_switch_value != 0)
2715 as_bad (_("-G may not be used in position-independent code"));
2716 g_switch_value = 0;
2717 }
2718
fef14a42 2719 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_arch))
252b5132
RH
2720 as_warn (_("Could not set architecture and machine"));
2721
252b5132
RH
2722 op_hash = hash_new ();
2723
2724 for (i = 0; i < NUMOPCODES;)
2725 {
2726 const char *name = mips_opcodes[i].name;
2727
17a2f251 2728 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
2729 if (retval != NULL)
2730 {
2731 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
2732 mips_opcodes[i].name, retval);
2733 /* Probably a memory allocation problem? Give up now. */
2734 as_fatal (_("Broken assembler. No assembly attempted."));
2735 }
2736 do
2737 {
2738 if (mips_opcodes[i].pinfo != INSN_MACRO)
2739 {
2740 if (!validate_mips_insn (&mips_opcodes[i]))
2741 broken = 1;
1e915849
RS
2742 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2743 {
2744 create_insn (&nop_insn, mips_opcodes + i);
c67a084a
NC
2745 if (mips_fix_loongson2f_nop)
2746 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
1e915849
RS
2747 nop_insn.fixed_p = 1;
2748 }
252b5132
RH
2749 }
2750 ++i;
2751 }
2752 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
2753 }
2754
2755 mips16_op_hash = hash_new ();
2756
2757 i = 0;
2758 while (i < bfd_mips16_num_opcodes)
2759 {
2760 const char *name = mips16_opcodes[i].name;
2761
17a2f251 2762 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
2763 if (retval != NULL)
2764 as_fatal (_("internal: can't hash `%s': %s"),
2765 mips16_opcodes[i].name, retval);
2766 do
2767 {
2768 if (mips16_opcodes[i].pinfo != INSN_MACRO
2769 && ((mips16_opcodes[i].match & mips16_opcodes[i].mask)
2770 != mips16_opcodes[i].match))
2771 {
2772 fprintf (stderr, _("internal error: bad mips16 opcode: %s %s\n"),
2773 mips16_opcodes[i].name, mips16_opcodes[i].args);
2774 broken = 1;
2775 }
1e915849
RS
2776 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
2777 {
2778 create_insn (&mips16_nop_insn, mips16_opcodes + i);
2779 mips16_nop_insn.fixed_p = 1;
2780 }
252b5132
RH
2781 ++i;
2782 }
2783 while (i < bfd_mips16_num_opcodes
2784 && strcmp (mips16_opcodes[i].name, name) == 0);
2785 }
2786
df58fc94
RS
2787 micromips_op_hash = hash_new ();
2788
2789 i = 0;
2790 while (i < bfd_micromips_num_opcodes)
2791 {
2792 const char *name = micromips_opcodes[i].name;
2793
2794 retval = hash_insert (micromips_op_hash, name,
2795 (void *) &micromips_opcodes[i]);
2796 if (retval != NULL)
2797 as_fatal (_("internal: can't hash `%s': %s"),
2798 micromips_opcodes[i].name, retval);
2799 do
2800 if (micromips_opcodes[i].pinfo != INSN_MACRO)
2801 {
2802 struct mips_cl_insn *micromips_nop_insn;
2803
2804 if (!validate_micromips_insn (&micromips_opcodes[i]))
2805 broken = 1;
2806
2807 if (micromips_insn_length (micromips_opcodes + i) == 2)
2808 micromips_nop_insn = &micromips_nop16_insn;
2809 else if (micromips_insn_length (micromips_opcodes + i) == 4)
2810 micromips_nop_insn = &micromips_nop32_insn;
2811 else
2812 continue;
2813
2814 if (micromips_nop_insn->insn_mo == NULL
2815 && strcmp (name, "nop") == 0)
2816 {
2817 create_insn (micromips_nop_insn, micromips_opcodes + i);
2818 micromips_nop_insn->fixed_p = 1;
2819 }
2820 }
2821 while (++i < bfd_micromips_num_opcodes
2822 && strcmp (micromips_opcodes[i].name, name) == 0);
2823 }
2824
252b5132
RH
2825 if (broken)
2826 as_fatal (_("Broken assembler. No assembly attempted."));
2827
2828 /* We add all the general register names to the symbol table. This
2829 helps us detect invalid uses of them. */
707bfff6
TS
2830 for (i = 0; reg_names[i].name; i++)
2831 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 2832 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
2833 &zero_address_frag));
2834 if (HAVE_NEWABI)
2835 for (i = 0; reg_names_n32n64[i].name; i++)
2836 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 2837 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 2838 &zero_address_frag));
707bfff6
TS
2839 else
2840 for (i = 0; reg_names_o32[i].name; i++)
2841 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 2842 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 2843 &zero_address_frag));
6047c971 2844
7d10b47d 2845 mips_no_prev_insn ();
252b5132
RH
2846
2847 mips_gprmask = 0;
2848 mips_cprmask[0] = 0;
2849 mips_cprmask[1] = 0;
2850 mips_cprmask[2] = 0;
2851 mips_cprmask[3] = 0;
2852
2853 /* set the default alignment for the text section (2**2) */
2854 record_alignment (text_section, 2);
2855
4d0d148d 2856 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 2857
f3ded42a
RS
2858 /* On a native system other than VxWorks, sections must be aligned
2859 to 16 byte boundaries. When configured for an embedded ELF
2860 target, we don't bother. */
2861 if (strncmp (TARGET_OS, "elf", 3) != 0
2862 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 2863 {
f3ded42a
RS
2864 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
2865 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
2866 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
2867 }
252b5132 2868
f3ded42a
RS
2869 /* Create a .reginfo section for register masks and a .mdebug
2870 section for debugging information. */
2871 {
2872 segT seg;
2873 subsegT subseg;
2874 flagword flags;
2875 segT sec;
2876
2877 seg = now_seg;
2878 subseg = now_subseg;
2879
2880 /* The ABI says this section should be loaded so that the
2881 running program can access it. However, we don't load it
2882 if we are configured for an embedded target */
2883 flags = SEC_READONLY | SEC_DATA;
2884 if (strncmp (TARGET_OS, "elf", 3) != 0)
2885 flags |= SEC_ALLOC | SEC_LOAD;
2886
2887 if (mips_abi != N64_ABI)
252b5132 2888 {
f3ded42a 2889 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 2890
f3ded42a
RS
2891 bfd_set_section_flags (stdoutput, sec, flags);
2892 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 2893
f3ded42a
RS
2894 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
2895 }
2896 else
2897 {
2898 /* The 64-bit ABI uses a .MIPS.options section rather than
2899 .reginfo section. */
2900 sec = subseg_new (".MIPS.options", (subsegT) 0);
2901 bfd_set_section_flags (stdoutput, sec, flags);
2902 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 2903
f3ded42a
RS
2904 /* Set up the option header. */
2905 {
2906 Elf_Internal_Options opthdr;
2907 char *f;
2908
2909 opthdr.kind = ODK_REGINFO;
2910 opthdr.size = (sizeof (Elf_External_Options)
2911 + sizeof (Elf64_External_RegInfo));
2912 opthdr.section = 0;
2913 opthdr.info = 0;
2914 f = frag_more (sizeof (Elf_External_Options));
2915 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
2916 (Elf_External_Options *) f);
2917
2918 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
2919 }
2920 }
252b5132 2921
f3ded42a
RS
2922 if (ECOFF_DEBUGGING)
2923 {
2924 sec = subseg_new (".mdebug", (subsegT) 0);
2925 (void) bfd_set_section_flags (stdoutput, sec,
2926 SEC_HAS_CONTENTS | SEC_READONLY);
2927 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 2928 }
f3ded42a
RS
2929 else if (mips_flag_pdr)
2930 {
2931 pdr_seg = subseg_new (".pdr", (subsegT) 0);
2932 (void) bfd_set_section_flags (stdoutput, pdr_seg,
2933 SEC_READONLY | SEC_RELOC
2934 | SEC_DEBUGGING);
2935 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
2936 }
2937
2938 subseg_set (seg, subseg);
2939 }
252b5132
RH
2940
2941 if (! ECOFF_DEBUGGING)
2942 md_obj_begin ();
71400594
RS
2943
2944 if (mips_fix_vr4120)
2945 init_vr4120_conflicts ();
252b5132
RH
2946}
2947
2948void
17a2f251 2949md_mips_end (void)
252b5132 2950{
02b1ab82 2951 mips_emit_delays ();
252b5132
RH
2952 if (! ECOFF_DEBUGGING)
2953 md_obj_end ();
2954}
2955
2956void
17a2f251 2957md_assemble (char *str)
252b5132
RH
2958{
2959 struct mips_cl_insn insn;
f6688943
TS
2960 bfd_reloc_code_real_type unused_reloc[3]
2961 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132
RH
2962
2963 imm_expr.X_op = O_absent;
5f74bc13 2964 imm2_expr.X_op = O_absent;
252b5132 2965 offset_expr.X_op = O_absent;
f6688943
TS
2966 imm_reloc[0] = BFD_RELOC_UNUSED;
2967 imm_reloc[1] = BFD_RELOC_UNUSED;
2968 imm_reloc[2] = BFD_RELOC_UNUSED;
2969 offset_reloc[0] = BFD_RELOC_UNUSED;
2970 offset_reloc[1] = BFD_RELOC_UNUSED;
2971 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 2972
e1b47bd5
RS
2973 mips_mark_labels ();
2974 mips_assembling_insn = TRUE;
2975
252b5132
RH
2976 if (mips_opts.mips16)
2977 mips16_ip (str, &insn);
2978 else
2979 {
2980 mips_ip (str, &insn);
beae10d5
KH
2981 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
2982 str, insn.insn_opcode));
252b5132
RH
2983 }
2984
2985 if (insn_error)
e1b47bd5
RS
2986 as_bad ("%s `%s'", insn_error, str);
2987 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 2988 {
584892a6 2989 macro_start ();
252b5132
RH
2990 if (mips_opts.mips16)
2991 mips16_macro (&insn);
2992 else
833794fc 2993 macro (&insn, str);
584892a6 2994 macro_end ();
252b5132
RH
2995 }
2996 else
2997 {
2998 if (imm_expr.X_op != O_absent)
df58fc94 2999 append_insn (&insn, &imm_expr, imm_reloc, FALSE);
252b5132 3000 else if (offset_expr.X_op != O_absent)
df58fc94 3001 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 3002 else
df58fc94 3003 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 3004 }
e1b47bd5
RS
3005
3006 mips_assembling_insn = FALSE;
252b5132
RH
3007}
3008
738e5348
RS
3009/* Convenience functions for abstracting away the differences between
3010 MIPS16 and non-MIPS16 relocations. */
3011
3012static inline bfd_boolean
3013mips16_reloc_p (bfd_reloc_code_real_type reloc)
3014{
3015 switch (reloc)
3016 {
3017 case BFD_RELOC_MIPS16_JMP:
3018 case BFD_RELOC_MIPS16_GPREL:
3019 case BFD_RELOC_MIPS16_GOT16:
3020 case BFD_RELOC_MIPS16_CALL16:
3021 case BFD_RELOC_MIPS16_HI16_S:
3022 case BFD_RELOC_MIPS16_HI16:
3023 case BFD_RELOC_MIPS16_LO16:
3024 return TRUE;
3025
3026 default:
3027 return FALSE;
3028 }
3029}
3030
df58fc94
RS
3031static inline bfd_boolean
3032micromips_reloc_p (bfd_reloc_code_real_type reloc)
3033{
3034 switch (reloc)
3035 {
3036 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3037 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3038 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3039 case BFD_RELOC_MICROMIPS_GPREL16:
3040 case BFD_RELOC_MICROMIPS_JMP:
3041 case BFD_RELOC_MICROMIPS_HI16:
3042 case BFD_RELOC_MICROMIPS_HI16_S:
3043 case BFD_RELOC_MICROMIPS_LO16:
3044 case BFD_RELOC_MICROMIPS_LITERAL:
3045 case BFD_RELOC_MICROMIPS_GOT16:
3046 case BFD_RELOC_MICROMIPS_CALL16:
3047 case BFD_RELOC_MICROMIPS_GOT_HI16:
3048 case BFD_RELOC_MICROMIPS_GOT_LO16:
3049 case BFD_RELOC_MICROMIPS_CALL_HI16:
3050 case BFD_RELOC_MICROMIPS_CALL_LO16:
3051 case BFD_RELOC_MICROMIPS_SUB:
3052 case BFD_RELOC_MICROMIPS_GOT_PAGE:
3053 case BFD_RELOC_MICROMIPS_GOT_OFST:
3054 case BFD_RELOC_MICROMIPS_GOT_DISP:
3055 case BFD_RELOC_MICROMIPS_HIGHEST:
3056 case BFD_RELOC_MICROMIPS_HIGHER:
3057 case BFD_RELOC_MICROMIPS_SCN_DISP:
3058 case BFD_RELOC_MICROMIPS_JALR:
3059 return TRUE;
3060
3061 default:
3062 return FALSE;
3063 }
3064}
3065
2309ddf2
MR
3066static inline bfd_boolean
3067jmp_reloc_p (bfd_reloc_code_real_type reloc)
3068{
3069 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
3070}
3071
738e5348
RS
3072static inline bfd_boolean
3073got16_reloc_p (bfd_reloc_code_real_type reloc)
3074{
2309ddf2 3075 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 3076 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
3077}
3078
3079static inline bfd_boolean
3080hi16_reloc_p (bfd_reloc_code_real_type reloc)
3081{
2309ddf2 3082 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 3083 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
3084}
3085
3086static inline bfd_boolean
3087lo16_reloc_p (bfd_reloc_code_real_type reloc)
3088{
2309ddf2 3089 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
3090 || reloc == BFD_RELOC_MICROMIPS_LO16);
3091}
3092
df58fc94
RS
3093static inline bfd_boolean
3094jalr_reloc_p (bfd_reloc_code_real_type reloc)
3095{
2309ddf2 3096 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
3097}
3098
2de39019
CM
3099/* Return true if RELOC is a PC-relative relocation that does not have
3100 full address range. */
3101
3102static inline bfd_boolean
3103limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
3104{
3105 switch (reloc)
3106 {
3107 case BFD_RELOC_16_PCREL_S2:
3108 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
3109 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
3110 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
3111 return TRUE;
3112
b47468a6
CM
3113 case BFD_RELOC_32_PCREL:
3114 return HAVE_64BIT_ADDRESSES;
3115
2de39019
CM
3116 default:
3117 return FALSE;
3118 }
3119}
b47468a6 3120
5919d012 3121/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
3122 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
3123 need a matching %lo() when applied to local symbols. */
5919d012
RS
3124
3125static inline bfd_boolean
17a2f251 3126reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 3127{
3b91255e 3128 return (HAVE_IN_PLACE_ADDENDS
738e5348 3129 && (hi16_reloc_p (reloc)
0a44bf69
RS
3130 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
3131 all GOT16 relocations evaluate to "G". */
738e5348
RS
3132 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
3133}
3134
3135/* Return the type of %lo() reloc needed by RELOC, given that
3136 reloc_needs_lo_p. */
3137
3138static inline bfd_reloc_code_real_type
3139matching_lo_reloc (bfd_reloc_code_real_type reloc)
3140{
df58fc94
RS
3141 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
3142 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
3143 : BFD_RELOC_LO16));
5919d012
RS
3144}
3145
3146/* Return true if the given fixup is followed by a matching R_MIPS_LO16
3147 relocation. */
3148
3149static inline bfd_boolean
17a2f251 3150fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
3151{
3152 return (fixp->fx_next != NULL
738e5348 3153 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
3154 && fixp->fx_addsy == fixp->fx_next->fx_addsy
3155 && fixp->fx_offset == fixp->fx_next->fx_offset);
3156}
3157
252b5132
RH
3158/* This function returns true if modifying a register requires a
3159 delay. */
3160
3161static int
17a2f251 3162reg_needs_delay (unsigned int reg)
252b5132
RH
3163{
3164 unsigned long prev_pinfo;
3165
47e39b9d 3166 prev_pinfo = history[0].insn_mo->pinfo;
252b5132 3167 if (! mips_opts.noreorder
81912461
ILT
3168 && (((prev_pinfo & INSN_LOAD_MEMORY_DELAY)
3169 && ! gpr_interlocks)
3170 || ((prev_pinfo & INSN_LOAD_COPROC_DELAY)
3171 && ! cop_interlocks)))
252b5132 3172 {
81912461
ILT
3173 /* A load from a coprocessor or from memory. All load delays
3174 delay the use of general register rt for one instruction. */
bdaaa2e1 3175 /* Itbl support may require additional care here. */
252b5132 3176 know (prev_pinfo & INSN_WRITE_GPR_T);
df58fc94 3177 if (reg == EXTRACT_OPERAND (mips_opts.micromips, RT, history[0]))
252b5132
RH
3178 return 1;
3179 }
3180
3181 return 0;
3182}
3183
462427c4
RS
3184/* Move all labels in LABELS to the current insertion point. TEXT_P
3185 says whether the labels refer to text or data. */
404a8071
RS
3186
3187static void
462427c4 3188mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
3189{
3190 struct insn_label_list *l;
3191 valueT val;
3192
462427c4 3193 for (l = labels; l != NULL; l = l->next)
404a8071 3194 {
9c2799c2 3195 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
3196 symbol_set_frag (l->label, frag_now);
3197 val = (valueT) frag_now_fix ();
df58fc94 3198 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 3199 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
3200 ++val;
3201 S_SET_VALUE (l->label, val);
3202 }
3203}
3204
462427c4
RS
3205/* Move all labels in insn_labels to the current insertion point
3206 and treat them as text labels. */
3207
3208static void
3209mips_move_text_labels (void)
3210{
3211 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
3212}
3213
5f0fe04b
TS
3214static bfd_boolean
3215s_is_linkonce (symbolS *sym, segT from_seg)
3216{
3217 bfd_boolean linkonce = FALSE;
3218 segT symseg = S_GET_SEGMENT (sym);
3219
3220 if (symseg != from_seg && !S_IS_LOCAL (sym))
3221 {
3222 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
3223 linkonce = TRUE;
5f0fe04b
TS
3224 /* The GNU toolchain uses an extension for ELF: a section
3225 beginning with the magic string .gnu.linkonce is a
3226 linkonce section. */
3227 if (strncmp (segment_name (symseg), ".gnu.linkonce",
3228 sizeof ".gnu.linkonce" - 1) == 0)
3229 linkonce = TRUE;
5f0fe04b
TS
3230 }
3231 return linkonce;
3232}
3233
e1b47bd5 3234/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
3235 linker to handle them specially, such as generating jalx instructions
3236 when needed. We also make them odd for the duration of the assembly,
3237 in order to generate the right sort of code. We will make them even
252b5132
RH
3238 in the adjust_symtab routine, while leaving them marked. This is
3239 convenient for the debugger and the disassembler. The linker knows
3240 to make them odd again. */
3241
3242static void
e1b47bd5 3243mips_compressed_mark_label (symbolS *label)
252b5132 3244{
df58fc94 3245 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 3246
f3ded42a
RS
3247 if (mips_opts.mips16)
3248 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
3249 else
3250 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
3251 if ((S_GET_VALUE (label) & 1) == 0
3252 /* Don't adjust the address if the label is global or weak, or
3253 in a link-once section, since we'll be emitting symbol reloc
3254 references to it which will be patched up by the linker, and
3255 the final value of the symbol may or may not be MIPS16/microMIPS. */
3256 && !S_IS_WEAK (label)
3257 && !S_IS_EXTERNAL (label)
3258 && !s_is_linkonce (label, now_seg))
3259 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
3260}
3261
3262/* Mark preceding MIPS16 or microMIPS instruction labels. */
3263
3264static void
3265mips_compressed_mark_labels (void)
3266{
3267 struct insn_label_list *l;
3268
3269 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
3270 mips_compressed_mark_label (l->label);
252b5132
RH
3271}
3272
4d7206a2
RS
3273/* End the current frag. Make it a variant frag and record the
3274 relaxation info. */
3275
3276static void
3277relax_close_frag (void)
3278{
584892a6 3279 mips_macro_warning.first_frag = frag_now;
4d7206a2 3280 frag_var (rs_machine_dependent, 0, 0,
584892a6 3281 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
3282 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
3283
3284 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
3285 mips_relax.first_fixup = 0;
3286}
3287
3288/* Start a new relaxation sequence whose expansion depends on SYMBOL.
3289 See the comment above RELAX_ENCODE for more details. */
3290
3291static void
3292relax_start (symbolS *symbol)
3293{
9c2799c2 3294 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
3295 mips_relax.sequence = 1;
3296 mips_relax.symbol = symbol;
3297}
3298
3299/* Start generating the second version of a relaxable sequence.
3300 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
3301
3302static void
4d7206a2
RS
3303relax_switch (void)
3304{
9c2799c2 3305 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
3306 mips_relax.sequence = 2;
3307}
3308
3309/* End the current relaxable sequence. */
3310
3311static void
3312relax_end (void)
3313{
9c2799c2 3314 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
3315 relax_close_frag ();
3316 mips_relax.sequence = 0;
3317}
3318
11625dd8
RS
3319/* Return true if IP is a delayed branch or jump. */
3320
3321static inline bfd_boolean
3322delayed_branch_p (const struct mips_cl_insn *ip)
3323{
3324 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
3325 | INSN_COND_BRANCH_DELAY
3326 | INSN_COND_BRANCH_LIKELY)) != 0;
3327}
3328
3329/* Return true if IP is a compact branch or jump. */
3330
3331static inline bfd_boolean
3332compact_branch_p (const struct mips_cl_insn *ip)
3333{
3334 if (mips_opts.mips16)
3335 return (ip->insn_mo->pinfo & (MIPS16_INSN_UNCOND_BRANCH
3336 | MIPS16_INSN_COND_BRANCH)) != 0;
3337 else
3338 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
3339 | INSN2_COND_BRANCH)) != 0;
3340}
3341
3342/* Return true if IP is an unconditional branch or jump. */
3343
3344static inline bfd_boolean
3345uncond_branch_p (const struct mips_cl_insn *ip)
3346{
3347 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
3348 || (mips_opts.mips16
3349 ? (ip->insn_mo->pinfo & MIPS16_INSN_UNCOND_BRANCH) != 0
3350 : (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0));
3351}
3352
3353/* Return true if IP is a branch-likely instruction. */
3354
3355static inline bfd_boolean
3356branch_likely_p (const struct mips_cl_insn *ip)
3357{
3358 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
3359}
3360
14fe068b
RS
3361/* Return the type of nop that should be used to fill the delay slot
3362 of delayed branch IP. */
3363
3364static struct mips_cl_insn *
3365get_delay_slot_nop (const struct mips_cl_insn *ip)
3366{
3367 if (mips_opts.micromips
3368 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
3369 return &micromips_nop32_insn;
3370 return NOP_INSN;
3371}
3372
2309ddf2 3373/* Return the mask of core registers that IP reads or writes. */
df58fc94
RS
3374
3375static unsigned int
3376gpr_mod_mask (const struct mips_cl_insn *ip)
3377{
2309ddf2 3378 unsigned long pinfo2;
df58fc94
RS
3379 unsigned int mask;
3380
3381 mask = 0;
df58fc94
RS
3382 pinfo2 = ip->insn_mo->pinfo2;
3383 if (mips_opts.micromips)
3384 {
df58fc94
RS
3385 if (pinfo2 & INSN2_MOD_GPR_MD)
3386 mask |= 1 << micromips_to_32_reg_d_map[EXTRACT_OPERAND (1, MD, *ip)];
df58fc94
RS
3387 if (pinfo2 & INSN2_MOD_GPR_MF)
3388 mask |= 1 << micromips_to_32_reg_f_map[EXTRACT_OPERAND (1, MF, *ip)];
df58fc94
RS
3389 if (pinfo2 & INSN2_MOD_SP)
3390 mask |= 1 << SP;
3391 }
3392 return mask;
3393}
3394
4c260379
RS
3395/* Return the mask of core registers that IP reads. */
3396
3397static unsigned int
3398gpr_read_mask (const struct mips_cl_insn *ip)
3399{
3400 unsigned long pinfo, pinfo2;
3401 unsigned int mask;
3402
df58fc94 3403 mask = gpr_mod_mask (ip);
4c260379
RS
3404 pinfo = ip->insn_mo->pinfo;
3405 pinfo2 = ip->insn_mo->pinfo2;
3406 if (mips_opts.mips16)
3407 {
3408 if (pinfo & MIPS16_INSN_READ_X)
3409 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3410 if (pinfo & MIPS16_INSN_READ_Y)
3411 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3412 if (pinfo & MIPS16_INSN_READ_T)
3413 mask |= 1 << TREG;
3414 if (pinfo & MIPS16_INSN_READ_SP)
3415 mask |= 1 << SP;
3416 if (pinfo & MIPS16_INSN_READ_31)
3417 mask |= 1 << RA;
3418 if (pinfo & MIPS16_INSN_READ_Z)
3419 mask |= 1 << (mips16_to_32_reg_map
3420 [MIPS16_EXTRACT_OPERAND (MOVE32Z, *ip)]);
3421 if (pinfo & MIPS16_INSN_READ_GPR_X)
3422 mask |= 1 << MIPS16_EXTRACT_OPERAND (REGR32, *ip);
3423 }
3424 else
3425 {
3426 if (pinfo2 & INSN2_READ_GPR_D)
2309ddf2 3427 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3428 if (pinfo & INSN_READ_GPR_T)
2309ddf2 3429 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4c260379 3430 if (pinfo & INSN_READ_GPR_S)
2309ddf2
MR
3431 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
3432 if (pinfo2 & INSN2_READ_GP)
3433 mask |= 1 << GP;
3434 if (pinfo2 & INSN2_READ_GPR_31)
3435 mask |= 1 << RA;
4c260379 3436 if (pinfo2 & INSN2_READ_GPR_Z)
2309ddf2 3437 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3438 }
2b0c8b40
MR
3439 if (mips_opts.micromips)
3440 {
3441 if (pinfo2 & INSN2_READ_GPR_MC)
3442 mask |= 1 << micromips_to_32_reg_c_map[EXTRACT_OPERAND (1, MC, *ip)];
3443 if (pinfo2 & INSN2_READ_GPR_ME)
3444 mask |= 1 << micromips_to_32_reg_e_map[EXTRACT_OPERAND (1, ME, *ip)];
3445 if (pinfo2 & INSN2_READ_GPR_MG)
3446 mask |= 1 << micromips_to_32_reg_g_map[EXTRACT_OPERAND (1, MG, *ip)];
3447 if (pinfo2 & INSN2_READ_GPR_MJ)
3448 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3449 if (pinfo2 & INSN2_READ_GPR_MMN)
3450 {
3451 mask |= 1 << micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
3452 mask |= 1 << micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
3453 }
3454 if (pinfo2 & INSN2_READ_GPR_MP)
3455 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3456 if (pinfo2 & INSN2_READ_GPR_MQ)
3457 mask |= 1 << micromips_to_32_reg_q_map[EXTRACT_OPERAND (1, MQ, *ip)];
3458 }
fe35f09f
RS
3459 /* Don't include register 0. */
3460 return mask & ~1;
4c260379
RS
3461}
3462
3463/* Return the mask of core registers that IP writes. */
3464
3465static unsigned int
3466gpr_write_mask (const struct mips_cl_insn *ip)
3467{
3468 unsigned long pinfo, pinfo2;
3469 unsigned int mask;
3470
df58fc94 3471 mask = gpr_mod_mask (ip);
4c260379
RS
3472 pinfo = ip->insn_mo->pinfo;
3473 pinfo2 = ip->insn_mo->pinfo2;
3474 if (mips_opts.mips16)
3475 {
3476 if (pinfo & MIPS16_INSN_WRITE_X)
3477 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RX, *ip)];
3478 if (pinfo & MIPS16_INSN_WRITE_Y)
3479 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RY, *ip)];
3480 if (pinfo & MIPS16_INSN_WRITE_Z)
3481 mask |= 1 << mips16_to_32_reg_map[MIPS16_EXTRACT_OPERAND (RZ, *ip)];
3482 if (pinfo & MIPS16_INSN_WRITE_T)
3483 mask |= 1 << TREG;
3484 if (pinfo & MIPS16_INSN_WRITE_SP)
3485 mask |= 1 << SP;
3486 if (pinfo & MIPS16_INSN_WRITE_31)
3487 mask |= 1 << RA;
3488 if (pinfo & MIPS16_INSN_WRITE_GPR_Y)
3489 mask |= 1 << MIPS16OP_EXTRACT_REG32R (ip->insn_opcode);
3490 }
3491 else
3492 {
3493 if (pinfo & INSN_WRITE_GPR_D)
df58fc94 3494 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4c260379 3495 if (pinfo & INSN_WRITE_GPR_T)
df58fc94 3496 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
2b0c8b40 3497 if (pinfo & INSN_WRITE_GPR_S)
2309ddf2 3498 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379
RS
3499 if (pinfo & INSN_WRITE_GPR_31)
3500 mask |= 1 << RA;
3501 if (pinfo2 & INSN2_WRITE_GPR_Z)
df58fc94 3502 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RZ, *ip);
4c260379 3503 }
2b0c8b40
MR
3504 if (mips_opts.micromips)
3505 {
3506 if (pinfo2 & INSN2_WRITE_GPR_MB)
3507 mask |= 1 << micromips_to_32_reg_b_map[EXTRACT_OPERAND (1, MB, *ip)];
e76ff5ab 3508 if (pinfo2 & INSN2_WRITE_GPR_MH)
2b0c8b40 3509 {
e76ff5ab
RS
3510 mask |= 1 << micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
3511 mask |= 1 << micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
2b0c8b40
MR
3512 }
3513 if (pinfo2 & INSN2_WRITE_GPR_MJ)
3514 mask |= 1 << EXTRACT_OPERAND (1, MJ, *ip);
3515 if (pinfo2 & INSN2_WRITE_GPR_MP)
3516 mask |= 1 << EXTRACT_OPERAND (1, MP, *ip);
3517 }
fe35f09f
RS
3518 /* Don't include register 0. */
3519 return mask & ~1;
4c260379
RS
3520}
3521
3522/* Return the mask of floating-point registers that IP reads. */
3523
3524static unsigned int
3525fpr_read_mask (const struct mips_cl_insn *ip)
3526{
3527 unsigned long pinfo, pinfo2;
3528 unsigned int mask;
3529
3530 mask = 0;
3531 pinfo = ip->insn_mo->pinfo;
3532 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3533 if (!mips_opts.mips16)
df58fc94
RS
3534 {
3535 if (pinfo2 & INSN2_READ_FPR_D)
2309ddf2 3536 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3537 if (pinfo & INSN_READ_FPR_S)
df58fc94 3538 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3539 if (pinfo & INSN_READ_FPR_T)
df58fc94 3540 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3541 if (pinfo & INSN_READ_FPR_R)
df58fc94 3542 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FR, *ip);
4c260379 3543 if (pinfo2 & INSN2_READ_FPR_Z)
df58fc94 3544 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3545 }
3546 /* Conservatively treat all operands to an FP_D instruction are doubles.
3547 (This is overly pessimistic for things like cvt.d.s.) */
3548 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3549 mask |= mask << 1;
3550 return mask;
3551}
3552
3553/* Return the mask of floating-point registers that IP writes. */
3554
3555static unsigned int
3556fpr_write_mask (const struct mips_cl_insn *ip)
3557{
3558 unsigned long pinfo, pinfo2;
3559 unsigned int mask;
3560
3561 mask = 0;
3562 pinfo = ip->insn_mo->pinfo;
3563 pinfo2 = ip->insn_mo->pinfo2;
2309ddf2 3564 if (!mips_opts.mips16)
4c260379
RS
3565 {
3566 if (pinfo & INSN_WRITE_FPR_D)
df58fc94 3567 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FD, *ip);
4c260379 3568 if (pinfo & INSN_WRITE_FPR_S)
df58fc94 3569 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FS, *ip);
4c260379 3570 if (pinfo & INSN_WRITE_FPR_T)
df58fc94 3571 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FT, *ip);
4c260379 3572 if (pinfo2 & INSN2_WRITE_FPR_Z)
df58fc94 3573 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, FZ, *ip);
4c260379
RS
3574 }
3575 /* Conservatively treat all operands to an FP_D instruction are doubles.
3576 (This is overly pessimistic for things like cvt.s.d.) */
3577 if (HAVE_32BIT_FPRS && (pinfo & FP_D))
3578 mask |= mask << 1;
3579 return mask;
3580}
3581
71400594
RS
3582/* Classify an instruction according to the FIX_VR4120_* enumeration.
3583 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
3584 by VR4120 errata. */
4d7206a2 3585
71400594
RS
3586static unsigned int
3587classify_vr4120_insn (const char *name)
252b5132 3588{
71400594
RS
3589 if (strncmp (name, "macc", 4) == 0)
3590 return FIX_VR4120_MACC;
3591 if (strncmp (name, "dmacc", 5) == 0)
3592 return FIX_VR4120_DMACC;
3593 if (strncmp (name, "mult", 4) == 0)
3594 return FIX_VR4120_MULT;
3595 if (strncmp (name, "dmult", 5) == 0)
3596 return FIX_VR4120_DMULT;
3597 if (strstr (name, "div"))
3598 return FIX_VR4120_DIV;
3599 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
3600 return FIX_VR4120_MTHILO;
3601 return NUM_FIX_VR4120_CLASSES;
3602}
252b5132 3603
ff239038
CM
3604#define INSN_ERET 0x42000018
3605#define INSN_DERET 0x4200001f
3606
71400594
RS
3607/* Return the number of instructions that must separate INSN1 and INSN2,
3608 where INSN1 is the earlier instruction. Return the worst-case value
3609 for any INSN2 if INSN2 is null. */
252b5132 3610
71400594
RS
3611static unsigned int
3612insns_between (const struct mips_cl_insn *insn1,
3613 const struct mips_cl_insn *insn2)
3614{
3615 unsigned long pinfo1, pinfo2;
4c260379 3616 unsigned int mask;
71400594
RS
3617
3618 /* This function needs to know which pinfo flags are set for INSN2
3619 and which registers INSN2 uses. The former is stored in PINFO2 and
4c260379
RS
3620 the latter is tested via INSN2_USES_GPR. If INSN2 is null, PINFO2
3621 will have every flag set and INSN2_USES_GPR will always return true. */
71400594
RS
3622 pinfo1 = insn1->insn_mo->pinfo;
3623 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 3624
4c260379
RS
3625#define INSN2_USES_GPR(REG) \
3626 (insn2 == NULL || (gpr_read_mask (insn2) & (1U << (REG))) != 0)
71400594
RS
3627
3628 /* For most targets, write-after-read dependencies on the HI and LO
3629 registers must be separated by at least two instructions. */
3630 if (!hilo_interlocks)
252b5132 3631 {
71400594
RS
3632 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
3633 return 2;
3634 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
3635 return 2;
3636 }
3637
3638 /* If we're working around r7000 errata, there must be two instructions
3639 between an mfhi or mflo and any instruction that uses the result. */
3640 if (mips_7000_hilo_fix
df58fc94 3641 && !mips_opts.micromips
71400594 3642 && MF_HILO_INSN (pinfo1)
df58fc94 3643 && INSN2_USES_GPR (EXTRACT_OPERAND (0, RD, *insn1)))
71400594
RS
3644 return 2;
3645
ff239038
CM
3646 /* If we're working around 24K errata, one instruction is required
3647 if an ERET or DERET is followed by a branch instruction. */
df58fc94 3648 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
3649 {
3650 if (insn1->insn_opcode == INSN_ERET
3651 || insn1->insn_opcode == INSN_DERET)
3652 {
3653 if (insn2 == NULL
3654 || insn2->insn_opcode == INSN_ERET
3655 || insn2->insn_opcode == INSN_DERET
11625dd8 3656 || delayed_branch_p (insn2))
ff239038
CM
3657 return 1;
3658 }
3659 }
3660
71400594
RS
3661 /* If working around VR4120 errata, check for combinations that need
3662 a single intervening instruction. */
df58fc94 3663 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
3664 {
3665 unsigned int class1, class2;
252b5132 3666
71400594
RS
3667 class1 = classify_vr4120_insn (insn1->insn_mo->name);
3668 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 3669 {
71400594
RS
3670 if (insn2 == NULL)
3671 return 1;
3672 class2 = classify_vr4120_insn (insn2->insn_mo->name);
3673 if (vr4120_conflicts[class1] & (1 << class2))
3674 return 1;
252b5132 3675 }
71400594
RS
3676 }
3677
df58fc94 3678 if (!HAVE_CODE_COMPRESSION)
71400594
RS
3679 {
3680 /* Check for GPR or coprocessor load delays. All such delays
3681 are on the RT register. */
3682 /* Itbl support may require additional care here. */
3683 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY_DELAY))
3684 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC_DELAY)))
252b5132 3685 {
71400594 3686 know (pinfo1 & INSN_WRITE_GPR_T);
df58fc94 3687 if (INSN2_USES_GPR (EXTRACT_OPERAND (0, RT, *insn1)))
71400594
RS
3688 return 1;
3689 }
3690
3691 /* Check for generic coprocessor hazards.
3692
3693 This case is not handled very well. There is no special
3694 knowledge of CP0 handling, and the coprocessors other than
3695 the floating point unit are not distinguished at all. */
3696 /* Itbl support may require additional care here. FIXME!
3697 Need to modify this to include knowledge about
3698 user specified delays! */
3699 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE_DELAY))
3700 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
3701 {
3702 /* Handle cases where INSN1 writes to a known general coprocessor
3703 register. There must be a one instruction delay before INSN2
3704 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
3705 mask = fpr_write_mask (insn1);
3706 if (mask != 0)
252b5132 3707 {
4c260379 3708 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 3709 return 1;
252b5132
RH
3710 }
3711 else
3712 {
71400594
RS
3713 /* Read-after-write dependencies on the control registers
3714 require a two-instruction gap. */
3715 if ((pinfo1 & INSN_WRITE_COND_CODE)
3716 && (pinfo2 & INSN_READ_COND_CODE))
3717 return 2;
3718
3719 /* We don't know exactly what INSN1 does. If INSN2 is
3720 also a coprocessor instruction, assume there must be
3721 a one instruction gap. */
3722 if (pinfo2 & INSN_COP)
3723 return 1;
252b5132
RH
3724 }
3725 }
6b76fefe 3726
71400594
RS
3727 /* Check for read-after-write dependencies on the coprocessor
3728 control registers in cases where INSN1 does not need a general
3729 coprocessor delay. This means that INSN1 is a floating point
3730 comparison instruction. */
3731 /* Itbl support may require additional care here. */
3732 else if (!cop_interlocks
3733 && (pinfo1 & INSN_WRITE_COND_CODE)
3734 && (pinfo2 & INSN_READ_COND_CODE))
3735 return 1;
3736 }
6b76fefe 3737
4c260379 3738#undef INSN2_USES_GPR
6b76fefe 3739
71400594
RS
3740 return 0;
3741}
6b76fefe 3742
7d8e00cf
RS
3743/* Return the number of nops that would be needed to work around the
3744 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
3745 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
3746 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
3747
3748static int
932d1a1b 3749nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
3750 const struct mips_cl_insn *insn)
3751{
4c260379
RS
3752 int i, j;
3753 unsigned int mask;
7d8e00cf
RS
3754
3755 /* Check if the instruction writes to HI or LO. MTHI and MTLO
3756 are not affected by the errata. */
3757 if (insn != 0
3758 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
3759 || strcmp (insn->insn_mo->name, "mtlo") == 0
3760 || strcmp (insn->insn_mo->name, "mthi") == 0))
3761 return 0;
3762
3763 /* Search for the first MFLO or MFHI. */
3764 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 3765 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
3766 {
3767 /* Extract the destination register. */
4c260379 3768 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
3769
3770 /* No nops are needed if INSN reads that register. */
4c260379 3771 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
3772 return 0;
3773
3774 /* ...or if any of the intervening instructions do. */
3775 for (j = 0; j < i; j++)
4c260379 3776 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
3777 return 0;
3778
932d1a1b
RS
3779 if (i >= ignore)
3780 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
3781 }
3782 return 0;
3783}
3784
15be625d
CM
3785#define BASE_REG_EQ(INSN1, INSN2) \
3786 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
3787 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
3788
3789/* Return the minimum alignment for this store instruction. */
3790
3791static int
3792fix_24k_align_to (const struct mips_opcode *mo)
3793{
3794 if (strcmp (mo->name, "sh") == 0)
3795 return 2;
3796
3797 if (strcmp (mo->name, "swc1") == 0
3798 || strcmp (mo->name, "swc2") == 0
3799 || strcmp (mo->name, "sw") == 0
3800 || strcmp (mo->name, "sc") == 0
3801 || strcmp (mo->name, "s.s") == 0)
3802 return 4;
3803
3804 if (strcmp (mo->name, "sdc1") == 0
3805 || strcmp (mo->name, "sdc2") == 0
3806 || strcmp (mo->name, "s.d") == 0)
3807 return 8;
3808
3809 /* sb, swl, swr */
3810 return 1;
3811}
3812
3813struct fix_24k_store_info
3814 {
3815 /* Immediate offset, if any, for this store instruction. */
3816 short off;
3817 /* Alignment required by this store instruction. */
3818 int align_to;
3819 /* True for register offsets. */
3820 int register_offset;
3821 };
3822
3823/* Comparison function used by qsort. */
3824
3825static int
3826fix_24k_sort (const void *a, const void *b)
3827{
3828 const struct fix_24k_store_info *pos1 = a;
3829 const struct fix_24k_store_info *pos2 = b;
3830
3831 return (pos1->off - pos2->off);
3832}
3833
3834/* INSN is a store instruction. Try to record the store information
3835 in STINFO. Return false if the information isn't known. */
3836
3837static bfd_boolean
3838fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 3839 const struct mips_cl_insn *insn)
15be625d
CM
3840{
3841 /* The instruction must have a known offset. */
3842 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
3843 return FALSE;
3844
3845 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
3846 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
3847 return TRUE;
3848}
3849
932d1a1b
RS
3850/* Return the number of nops that would be needed to work around the 24k
3851 "lost data on stores during refill" errata if instruction INSN
3852 immediately followed the 2 instructions described by HIST.
3853 Ignore hazards that are contained within the first IGNORE
3854 instructions of HIST.
3855
3856 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
3857 for the data cache refills and store data. The following describes
3858 the scenario where the store data could be lost.
3859
3860 * A data cache miss, due to either a load or a store, causing fill
3861 data to be supplied by the memory subsystem
3862 * The first three doublewords of fill data are returned and written
3863 into the cache
3864 * A sequence of four stores occurs in consecutive cycles around the
3865 final doubleword of the fill:
3866 * Store A
3867 * Store B
3868 * Store C
3869 * Zero, One or more instructions
3870 * Store D
3871
3872 The four stores A-D must be to different doublewords of the line that
3873 is being filled. The fourth instruction in the sequence above permits
3874 the fill of the final doubleword to be transferred from the FSB into
3875 the cache. In the sequence above, the stores may be either integer
3876 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
3877 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
3878 different doublewords on the line. If the floating point unit is
3879 running in 1:2 mode, it is not possible to create the sequence above
3880 using only floating point store instructions.
15be625d
CM
3881
3882 In this case, the cache line being filled is incorrectly marked
3883 invalid, thereby losing the data from any store to the line that
3884 occurs between the original miss and the completion of the five
3885 cycle sequence shown above.
3886
932d1a1b 3887 The workarounds are:
15be625d 3888
932d1a1b
RS
3889 * Run the data cache in write-through mode.
3890 * Insert a non-store instruction between
3891 Store A and Store B or Store B and Store C. */
15be625d
CM
3892
3893static int
932d1a1b 3894nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
3895 const struct mips_cl_insn *insn)
3896{
3897 struct fix_24k_store_info pos[3];
3898 int align, i, base_offset;
3899
932d1a1b
RS
3900 if (ignore >= 2)
3901 return 0;
3902
ab9794cf
RS
3903 /* If the previous instruction wasn't a store, there's nothing to
3904 worry about. */
15be625d
CM
3905 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
3906 return 0;
3907
ab9794cf
RS
3908 /* If the instructions after the previous one are unknown, we have
3909 to assume the worst. */
3910 if (!insn)
15be625d
CM
3911 return 1;
3912
ab9794cf
RS
3913 /* Check whether we are dealing with three consecutive stores. */
3914 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
3915 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
3916 return 0;
3917
3918 /* If we don't know the relationship between the store addresses,
3919 assume the worst. */
ab9794cf 3920 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
3921 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
3922 return 1;
3923
3924 if (!fix_24k_record_store_info (&pos[0], insn)
3925 || !fix_24k_record_store_info (&pos[1], &hist[0])
3926 || !fix_24k_record_store_info (&pos[2], &hist[1]))
3927 return 1;
3928
3929 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
3930
3931 /* Pick a value of ALIGN and X such that all offsets are adjusted by
3932 X bytes and such that the base register + X is known to be aligned
3933 to align bytes. */
3934
3935 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
3936 align = 8;
3937 else
3938 {
3939 align = pos[0].align_to;
3940 base_offset = pos[0].off;
3941 for (i = 1; i < 3; i++)
3942 if (align < pos[i].align_to)
3943 {
3944 align = pos[i].align_to;
3945 base_offset = pos[i].off;
3946 }
3947 for (i = 0; i < 3; i++)
3948 pos[i].off -= base_offset;
3949 }
3950
3951 pos[0].off &= ~align + 1;
3952 pos[1].off &= ~align + 1;
3953 pos[2].off &= ~align + 1;
3954
3955 /* If any two stores write to the same chunk, they also write to the
3956 same doubleword. The offsets are still sorted at this point. */
3957 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
3958 return 0;
3959
3960 /* A range of at least 9 bytes is needed for the stores to be in
3961 non-overlapping doublewords. */
3962 if (pos[2].off - pos[0].off <= 8)
3963 return 0;
3964
3965 if (pos[2].off - pos[1].off >= 24
3966 || pos[1].off - pos[0].off >= 24
3967 || pos[2].off - pos[0].off >= 32)
3968 return 0;
3969
3970 return 1;
3971}
3972
71400594 3973/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 3974 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
3975 where HIST[0] is the most recent instruction. Ignore hazards
3976 between INSN and the first IGNORE instructions in HIST.
3977
3978 If INSN is null, return the worse-case number of nops for any
3979 instruction. */
bdaaa2e1 3980
71400594 3981static int
932d1a1b 3982nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
3983 const struct mips_cl_insn *insn)
3984{
3985 int i, nops, tmp_nops;
bdaaa2e1 3986
71400594 3987 nops = 0;
932d1a1b 3988 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 3989 {
91d6fa6a 3990 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
3991 if (tmp_nops > nops)
3992 nops = tmp_nops;
3993 }
7d8e00cf 3994
df58fc94 3995 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 3996 {
932d1a1b 3997 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
3998 if (tmp_nops > nops)
3999 nops = tmp_nops;
4000 }
4001
df58fc94 4002 if (mips_fix_24k && !mips_opts.micromips)
15be625d 4003 {
932d1a1b 4004 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
4005 if (tmp_nops > nops)
4006 nops = tmp_nops;
4007 }
4008
71400594
RS
4009 return nops;
4010}
252b5132 4011
71400594 4012/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 4013 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
4014 would be needed after the extended sequence, ignoring hazards
4015 in the first IGNORE instructions. */
252b5132 4016
71400594 4017static int
932d1a1b
RS
4018nops_for_sequence (int num_insns, int ignore,
4019 const struct mips_cl_insn *hist, ...)
71400594
RS
4020{
4021 va_list args;
4022 struct mips_cl_insn buffer[MAX_NOPS];
4023 struct mips_cl_insn *cursor;
4024 int nops;
4025
91d6fa6a 4026 va_start (args, hist);
71400594 4027 cursor = buffer + num_insns;
91d6fa6a 4028 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
4029 while (cursor > buffer)
4030 *--cursor = *va_arg (args, const struct mips_cl_insn *);
4031
932d1a1b 4032 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
4033 va_end (args);
4034 return nops;
4035}
252b5132 4036
71400594
RS
4037/* Like nops_for_insn, but if INSN is a branch, take into account the
4038 worst-case delay for the branch target. */
252b5132 4039
71400594 4040static int
932d1a1b 4041nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
4042 const struct mips_cl_insn *insn)
4043{
4044 int nops, tmp_nops;
60b63b72 4045
932d1a1b 4046 nops = nops_for_insn (ignore, hist, insn);
11625dd8 4047 if (delayed_branch_p (insn))
71400594 4048 {
932d1a1b 4049 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 4050 hist, insn, get_delay_slot_nop (insn));
71400594
RS
4051 if (tmp_nops > nops)
4052 nops = tmp_nops;
4053 }
11625dd8 4054 else if (compact_branch_p (insn))
71400594 4055 {
932d1a1b 4056 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
4057 if (tmp_nops > nops)
4058 nops = tmp_nops;
4059 }
4060 return nops;
4061}
4062
c67a084a
NC
4063/* Fix NOP issue: Replace nops by "or at,at,zero". */
4064
4065static void
4066fix_loongson2f_nop (struct mips_cl_insn * ip)
4067{
df58fc94 4068 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
4069 if (strcmp (ip->insn_mo->name, "nop") == 0)
4070 ip->insn_opcode = LOONGSON2F_NOP_INSN;
4071}
4072
4073/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
4074 jr target pc &= 'hffff_ffff_cfff_ffff. */
4075
4076static void
4077fix_loongson2f_jump (struct mips_cl_insn * ip)
4078{
df58fc94 4079 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
4080 if (strcmp (ip->insn_mo->name, "j") == 0
4081 || strcmp (ip->insn_mo->name, "jr") == 0
4082 || strcmp (ip->insn_mo->name, "jalr") == 0)
4083 {
4084 int sreg;
4085 expressionS ep;
4086
4087 if (! mips_opts.at)
4088 return;
4089
df58fc94 4090 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
4091 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
4092 return;
4093
4094 ep.X_op = O_constant;
4095 ep.X_add_number = 0xcfff0000;
4096 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
4097 ep.X_add_number = 0xffff;
4098 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
4099 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
4100 }
4101}
4102
4103static void
4104fix_loongson2f (struct mips_cl_insn * ip)
4105{
4106 if (mips_fix_loongson2f_nop)
4107 fix_loongson2f_nop (ip);
4108
4109 if (mips_fix_loongson2f_jump)
4110 fix_loongson2f_jump (ip);
4111}
4112
a4e06468
RS
4113/* IP is a branch that has a delay slot, and we need to fill it
4114 automatically. Return true if we can do that by swapping IP
e407c74b
NC
4115 with the previous instruction.
4116 ADDRESS_EXPR is an operand of the instruction to be used with
4117 RELOC_TYPE. */
a4e06468
RS
4118
4119static bfd_boolean
e407c74b
NC
4120can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
4121 bfd_reloc_code_real_type *reloc_type)
a4e06468 4122{
2b0c8b40 4123 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468
RS
4124 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
4125
4126 /* -O2 and above is required for this optimization. */
4127 if (mips_optimize < 2)
4128 return FALSE;
4129
4130 /* If we have seen .set volatile or .set nomove, don't optimize. */
4131 if (mips_opts.nomove)
4132 return FALSE;
4133
4134 /* We can't swap if the previous instruction's position is fixed. */
4135 if (history[0].fixed_p)
4136 return FALSE;
4137
4138 /* If the previous previous insn was in a .set noreorder, we can't
4139 swap. Actually, the MIPS assembler will swap in this situation.
4140 However, gcc configured -with-gnu-as will generate code like
4141
4142 .set noreorder
4143 lw $4,XXX
4144 .set reorder
4145 INSN
4146 bne $4,$0,foo
4147
4148 in which we can not swap the bne and INSN. If gcc is not configured
4149 -with-gnu-as, it does not output the .set pseudo-ops. */
4150 if (history[1].noreorder_p)
4151 return FALSE;
4152
87333bb7
MR
4153 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
4154 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
4155 if (mips_opts.mips16 && history[0].fixp[0])
4156 return FALSE;
4157
4158 /* If the branch is itself the target of a branch, we can not swap.
4159 We cheat on this; all we check for is whether there is a label on
4160 this instruction. If there are any branches to anything other than
4161 a label, users must use .set noreorder. */
4162 if (seg_info (now_seg)->label_list)
4163 return FALSE;
4164
4165 /* If the previous instruction is in a variant frag other than this
2309ddf2 4166 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
4167 MIPS16 code, which uses variant frags for different purposes. */
4168 if (!mips_opts.mips16
a4e06468
RS
4169 && history[0].frag
4170 && history[0].frag->fr_type == rs_machine_dependent)
4171 return FALSE;
4172
bcd530a7
RS
4173 /* We do not swap with instructions that cannot architecturally
4174 be placed in a branch delay slot, such as SYNC or ERET. We
4175 also refrain from swapping with a trap instruction, since it
4176 complicates trap handlers to have the trap instruction be in
4177 a delay slot. */
a4e06468 4178 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 4179 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
4180 return FALSE;
4181
4182 /* Check for conflicts between the branch and the instructions
4183 before the candidate delay slot. */
4184 if (nops_for_insn (0, history + 1, ip) > 0)
4185 return FALSE;
4186
4187 /* Check for conflicts between the swapped sequence and the
4188 target of the branch. */
4189 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
4190 return FALSE;
4191
4192 /* If the branch reads a register that the previous
4193 instruction sets, we can not swap. */
4194 gpr_read = gpr_read_mask (ip);
4195 prev_gpr_write = gpr_write_mask (&history[0]);
4196 if (gpr_read & prev_gpr_write)
4197 return FALSE;
4198
4199 /* If the branch writes a register that the previous
4200 instruction sets, we can not swap. */
4201 gpr_write = gpr_write_mask (ip);
4202 if (gpr_write & prev_gpr_write)
4203 return FALSE;
4204
4205 /* If the branch writes a register that the previous
4206 instruction reads, we can not swap. */
4207 prev_gpr_read = gpr_read_mask (&history[0]);
4208 if (gpr_write & prev_gpr_read)
4209 return FALSE;
4210
4211 /* If one instruction sets a condition code and the
4212 other one uses a condition code, we can not swap. */
4213 pinfo = ip->insn_mo->pinfo;
4214 if ((pinfo & INSN_READ_COND_CODE)
4215 && (prev_pinfo & INSN_WRITE_COND_CODE))
4216 return FALSE;
4217 if ((pinfo & INSN_WRITE_COND_CODE)
4218 && (prev_pinfo & INSN_READ_COND_CODE))
4219 return FALSE;
4220
4221 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 4222 prev_pinfo2 = history[0].insn_mo->pinfo2;
a4e06468
RS
4223 if (mips_opts.mips16 && (prev_pinfo & MIPS16_INSN_READ_PC))
4224 return FALSE;
2b0c8b40
MR
4225 if (mips_opts.micromips && (prev_pinfo2 & INSN2_READ_PC))
4226 return FALSE;
a4e06468 4227
df58fc94
RS
4228 /* If the previous instruction has an incorrect size for a fixed
4229 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
4230 pinfo2 = ip->insn_mo->pinfo2;
4231 if (mips_opts.micromips
4232 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
4233 && insn_length (history) != 2)
4234 return FALSE;
4235 if (mips_opts.micromips
4236 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
4237 && insn_length (history) != 4)
4238 return FALSE;
4239
e407c74b
NC
4240 /* On R5900 short loops need to be fixed by inserting a nop in
4241 the branch delay slots.
4242 A short loop can be terminated too early. */
4243 if (mips_opts.arch == CPU_R5900
4244 /* Check if instruction has a parameter, ignore "j $31". */
4245 && (address_expr != NULL)
4246 /* Parameter must be 16 bit. */
4247 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
4248 /* Branch to same segment. */
4249 && (S_GET_SEGMENT(address_expr->X_add_symbol) == now_seg)
4250 /* Branch to same code fragment. */
4251 && (symbol_get_frag(address_expr->X_add_symbol) == frag_now)
4252 /* Can only calculate branch offset if value is known. */
4253 && symbol_constant_p(address_expr->X_add_symbol)
4254 /* Check if branch is really conditional. */
4255 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
4256 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
4257 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
4258 {
4259 int distance;
4260 /* Check if loop is shorter than 6 instructions including
4261 branch and delay slot. */
4262 distance = frag_now_fix() - S_GET_VALUE(address_expr->X_add_symbol);
4263 if (distance <= 20)
4264 {
4265 int i;
4266 int rv;
4267
4268 rv = FALSE;
4269 /* When the loop includes branches or jumps,
4270 it is not a short loop. */
4271 for (i = 0; i < (distance / 4); i++)
4272 {
4273 if ((history[i].cleared_p)
4274 || delayed_branch_p(&history[i]))
4275 {
4276 rv = TRUE;
4277 break;
4278 }
4279 }
4280 if (rv == FALSE)
4281 {
4282 /* Insert nop after branch to fix short loop. */
4283 return FALSE;
4284 }
4285 }
4286 }
4287
a4e06468
RS
4288 return TRUE;
4289}
4290
e407c74b
NC
4291/* Decide how we should add IP to the instruction stream.
4292 ADDRESS_EXPR is an operand of the instruction to be used with
4293 RELOC_TYPE. */
a4e06468
RS
4294
4295static enum append_method
e407c74b
NC
4296get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
4297 bfd_reloc_code_real_type *reloc_type)
a4e06468
RS
4298{
4299 unsigned long pinfo;
4300
4301 /* The relaxed version of a macro sequence must be inherently
4302 hazard-free. */
4303 if (mips_relax.sequence == 2)
4304 return APPEND_ADD;
4305
4306 /* We must not dabble with instructions in a ".set norerorder" block. */
4307 if (mips_opts.noreorder)
4308 return APPEND_ADD;
4309
4310 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 4311 if (delayed_branch_p (ip))
a4e06468 4312 {
e407c74b
NC
4313 if (!branch_likely_p (ip)
4314 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
4315 return APPEND_SWAP;
4316
11625dd8 4317 pinfo = ip->insn_mo->pinfo;
a4e06468
RS
4318 if (mips_opts.mips16
4319 && ISA_SUPPORTS_MIPS16E
a4e06468
RS
4320 && (pinfo & (MIPS16_INSN_READ_X | MIPS16_INSN_READ_31)))
4321 return APPEND_ADD_COMPACT;
4322
4323 return APPEND_ADD_WITH_NOP;
4324 }
4325
a4e06468
RS
4326 return APPEND_ADD;
4327}
4328
ceb94aa5
RS
4329/* IP is a MIPS16 instruction whose opcode we have just changed.
4330 Point IP->insn_mo to the new opcode's definition. */
4331
4332static void
4333find_altered_mips16_opcode (struct mips_cl_insn *ip)
4334{
4335 const struct mips_opcode *mo, *end;
4336
4337 end = &mips16_opcodes[bfd_mips16_num_opcodes];
4338 for (mo = ip->insn_mo; mo < end; mo++)
4339 if ((ip->insn_opcode & mo->mask) == mo->match)
4340 {
4341 ip->insn_mo = mo;
4342 return;
4343 }
4344 abort ();
4345}
4346
df58fc94
RS
4347/* For microMIPS macros, we need to generate a local number label
4348 as the target of branches. */
4349#define MICROMIPS_LABEL_CHAR '\037'
4350static unsigned long micromips_target_label;
4351static char micromips_target_name[32];
4352
4353static char *
4354micromips_label_name (void)
4355{
4356 char *p = micromips_target_name;
4357 char symbol_name_temporary[24];
4358 unsigned long l;
4359 int i;
4360
4361 if (*p)
4362 return p;
4363
4364 i = 0;
4365 l = micromips_target_label;
4366#ifdef LOCAL_LABEL_PREFIX
4367 *p++ = LOCAL_LABEL_PREFIX;
4368#endif
4369 *p++ = 'L';
4370 *p++ = MICROMIPS_LABEL_CHAR;
4371 do
4372 {
4373 symbol_name_temporary[i++] = l % 10 + '0';
4374 l /= 10;
4375 }
4376 while (l != 0);
4377 while (i > 0)
4378 *p++ = symbol_name_temporary[--i];
4379 *p = '\0';
4380
4381 return micromips_target_name;
4382}
4383
4384static void
4385micromips_label_expr (expressionS *label_expr)
4386{
4387 label_expr->X_op = O_symbol;
4388 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
4389 label_expr->X_add_number = 0;
4390}
4391
4392static void
4393micromips_label_inc (void)
4394{
4395 micromips_target_label++;
4396 *micromips_target_name = '\0';
4397}
4398
4399static void
4400micromips_add_label (void)
4401{
4402 symbolS *s;
4403
4404 s = colon (micromips_label_name ());
4405 micromips_label_inc ();
f3ded42a 4406 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
4407}
4408
4409/* If assembling microMIPS code, then return the microMIPS reloc
4410 corresponding to the requested one if any. Otherwise return
4411 the reloc unchanged. */
4412
4413static bfd_reloc_code_real_type
4414micromips_map_reloc (bfd_reloc_code_real_type reloc)
4415{
4416 static const bfd_reloc_code_real_type relocs[][2] =
4417 {
4418 /* Keep sorted incrementally by the left-hand key. */
4419 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
4420 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
4421 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
4422 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
4423 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
4424 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
4425 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
4426 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
4427 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
4428 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
4429 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
4430 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
4431 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
4432 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
4433 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
4434 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
4435 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
4436 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
4437 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
4438 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
4439 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
4440 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
4441 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
4442 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
4443 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
4444 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
4445 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
4446 };
4447 bfd_reloc_code_real_type r;
4448 size_t i;
4449
4450 if (!mips_opts.micromips)
4451 return reloc;
4452 for (i = 0; i < ARRAY_SIZE (relocs); i++)
4453 {
4454 r = relocs[i][0];
4455 if (r > reloc)
4456 return reloc;
4457 if (r == reloc)
4458 return relocs[i][1];
4459 }
4460 return reloc;
4461}
4462
b886a2ab
RS
4463/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
4464 Return true on success, storing the resolved value in RESULT. */
4465
4466static bfd_boolean
4467calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
4468 offsetT *result)
4469{
4470 switch (reloc)
4471 {
4472 case BFD_RELOC_MIPS_HIGHEST:
4473 case BFD_RELOC_MICROMIPS_HIGHEST:
4474 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
4475 return TRUE;
4476
4477 case BFD_RELOC_MIPS_HIGHER:
4478 case BFD_RELOC_MICROMIPS_HIGHER:
4479 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
4480 return TRUE;
4481
4482 case BFD_RELOC_HI16_S:
4483 case BFD_RELOC_MICROMIPS_HI16_S:
4484 case BFD_RELOC_MIPS16_HI16_S:
4485 *result = ((operand + 0x8000) >> 16) & 0xffff;
4486 return TRUE;
4487
4488 case BFD_RELOC_HI16:
4489 case BFD_RELOC_MICROMIPS_HI16:
4490 case BFD_RELOC_MIPS16_HI16:
4491 *result = (operand >> 16) & 0xffff;
4492 return TRUE;
4493
4494 case BFD_RELOC_LO16:
4495 case BFD_RELOC_MICROMIPS_LO16:
4496 case BFD_RELOC_MIPS16_LO16:
4497 *result = operand & 0xffff;
4498 return TRUE;
4499
4500 case BFD_RELOC_UNUSED:
4501 *result = operand;
4502 return TRUE;
4503
4504 default:
4505 return FALSE;
4506 }
4507}
4508
71400594
RS
4509/* Output an instruction. IP is the instruction information.
4510 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
4511 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
4512 a macro expansion. */
71400594
RS
4513
4514static void
4515append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 4516 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 4517{
14fe068b 4518 unsigned long prev_pinfo2, pinfo;
71400594 4519 bfd_boolean relaxed_branch = FALSE;
a4e06468 4520 enum append_method method;
2309ddf2 4521 bfd_boolean relax32;
2b0c8b40 4522 int branch_disp;
71400594 4523
2309ddf2 4524 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
4525 fix_loongson2f (ip);
4526
738f4d98 4527 file_ase_mips16 |= mips_opts.mips16;
df58fc94 4528 file_ase_micromips |= mips_opts.micromips;
738f4d98 4529
df58fc94 4530 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 4531 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
4532
4533 if (mips_opts.micromips
4534 && !expansionp
4535 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
4536 && micromips_insn_length (ip->insn_mo) != 2)
4537 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
4538 && micromips_insn_length (ip->insn_mo) != 4)))
4539 as_warn (_("Wrong size instruction in a %u-bit branch delay slot"),
4540 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 4541
15be625d
CM
4542 if (address_expr == NULL)
4543 ip->complete_p = 1;
b886a2ab
RS
4544 else if (reloc_type[0] <= BFD_RELOC_UNUSED
4545 && reloc_type[1] == BFD_RELOC_UNUSED
4546 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
4547 && address_expr->X_op == O_constant)
4548 {
15be625d
CM
4549 switch (*reloc_type)
4550 {
15be625d 4551 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
4552 {
4553 int shift;
4554
4555 shift = mips_opts.micromips ? 1 : 2;
4556 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4557 as_bad (_("jump to misaligned address (0x%lx)"),
4558 (unsigned long) address_expr->X_add_number);
4559 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4560 & 0x3ffffff);
335574df 4561 ip->complete_p = 1;
df58fc94 4562 }
15be625d
CM
4563 break;
4564
4565 case BFD_RELOC_MIPS16_JMP:
4566 if ((address_expr->X_add_number & 3) != 0)
4567 as_bad (_("jump to misaligned address (0x%lx)"),
4568 (unsigned long) address_expr->X_add_number);
4569 ip->insn_opcode |=
4570 (((address_expr->X_add_number & 0x7c0000) << 3)
4571 | ((address_expr->X_add_number & 0xf800000) >> 7)
4572 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 4573 ip->complete_p = 1;
15be625d
CM
4574 break;
4575
4576 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
4577 {
4578 int shift;
4579
4580 shift = mips_opts.micromips ? 1 : 2;
4581 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
4582 as_bad (_("branch to misaligned address (0x%lx)"),
4583 (unsigned long) address_expr->X_add_number);
4584 if (!mips_relax_branch)
4585 {
4586 if ((address_expr->X_add_number + (1 << (shift + 15)))
4587 & ~((1 << (shift + 16)) - 1))
4588 as_bad (_("branch address range overflow (0x%lx)"),
4589 (unsigned long) address_expr->X_add_number);
4590 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
4591 & 0xffff);
4592 }
df58fc94 4593 }
15be625d
CM
4594 break;
4595
4596 default:
b886a2ab
RS
4597 {
4598 offsetT value;
4599
4600 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
4601 &value))
4602 {
4603 ip->insn_opcode |= value & 0xffff;
4604 ip->complete_p = 1;
4605 }
4606 }
4607 break;
4608 }
15be625d
CM
4609 }
4610
71400594
RS
4611 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
4612 {
4613 /* There are a lot of optimizations we could do that we don't.
4614 In particular, we do not, in general, reorder instructions.
4615 If you use gcc with optimization, it will reorder
4616 instructions and generally do much more optimization then we
4617 do here; repeating all that work in the assembler would only
4618 benefit hand written assembly code, and does not seem worth
4619 it. */
4620 int nops = (mips_optimize == 0
932d1a1b
RS
4621 ? nops_for_insn (0, history, NULL)
4622 : nops_for_insn_or_target (0, history, ip));
71400594 4623 if (nops > 0)
252b5132
RH
4624 {
4625 fragS *old_frag;
4626 unsigned long old_frag_offset;
4627 int i;
252b5132
RH
4628
4629 old_frag = frag_now;
4630 old_frag_offset = frag_now_fix ();
4631
4632 for (i = 0; i < nops; i++)
14fe068b
RS
4633 add_fixed_insn (NOP_INSN);
4634 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
4635
4636 if (listing)
4637 {
4638 listing_prev_line ();
4639 /* We may be at the start of a variant frag. In case we
4640 are, make sure there is enough space for the frag
4641 after the frags created by listing_prev_line. The
4642 argument to frag_grow here must be at least as large
4643 as the argument to all other calls to frag_grow in
4644 this file. We don't have to worry about being in the
4645 middle of a variant frag, because the variants insert
4646 all needed nop instructions themselves. */
4647 frag_grow (40);
4648 }
4649
462427c4 4650 mips_move_text_labels ();
252b5132
RH
4651
4652#ifndef NO_ECOFF_DEBUGGING
4653 if (ECOFF_DEBUGGING)
4654 ecoff_fix_loc (old_frag, old_frag_offset);
4655#endif
4656 }
71400594
RS
4657 }
4658 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
4659 {
932d1a1b
RS
4660 int nops;
4661
4662 /* Work out how many nops in prev_nop_frag are needed by IP,
4663 ignoring hazards generated by the first prev_nop_frag_since
4664 instructions. */
4665 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 4666 gas_assert (nops <= prev_nop_frag_holds);
252b5132 4667
71400594
RS
4668 /* Enforce NOPS as a minimum. */
4669 if (nops > prev_nop_frag_required)
4670 prev_nop_frag_required = nops;
252b5132 4671
71400594
RS
4672 if (prev_nop_frag_holds == prev_nop_frag_required)
4673 {
4674 /* Settle for the current number of nops. Update the history
4675 accordingly (for the benefit of any future .set reorder code). */
4676 prev_nop_frag = NULL;
4677 insert_into_history (prev_nop_frag_since,
4678 prev_nop_frag_holds, NOP_INSN);
4679 }
4680 else
4681 {
4682 /* Allow this instruction to replace one of the nops that was
4683 tentatively added to prev_nop_frag. */
df58fc94 4684 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
4685 prev_nop_frag_holds--;
4686 prev_nop_frag_since++;
252b5132
RH
4687 }
4688 }
4689
e407c74b 4690 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 4691 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 4692
e410add4
RS
4693 dwarf2_emit_insn (0);
4694 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
4695 so "move" the instruction address accordingly.
4696
4697 Also, it doesn't seem appropriate for the assembler to reorder .loc
4698 entries. If this instruction is a branch that we are going to swap
4699 with the previous instruction, the two instructions should be
4700 treated as a unit, and the debug information for both instructions
4701 should refer to the start of the branch sequence. Using the
4702 current position is certainly wrong when swapping a 32-bit branch
4703 and a 16-bit delay slot, since the current position would then be
4704 in the middle of a branch. */
4705 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 4706
df58fc94
RS
4707 relax32 = (mips_relax_branch
4708 /* Don't try branch relaxation within .set nomacro, or within
4709 .set noat if we use $at for PIC computations. If it turns
4710 out that the branch was out-of-range, we'll get an error. */
4711 && !mips_opts.warn_about_macros
4712 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
4713 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
4714 as they have no complementing branches. */
4715 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
4716
4717 if (!HAVE_CODE_COMPRESSION
4718 && address_expr
4719 && relax32
0b25d3e6 4720 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 4721 && delayed_branch_p (ip))
4a6a3df4 4722 {
895921c9 4723 relaxed_branch = TRUE;
1e915849
RS
4724 add_relaxed_insn (ip, (relaxed_branch_length
4725 (NULL, NULL,
11625dd8
RS
4726 uncond_branch_p (ip) ? -1
4727 : branch_likely_p (ip) ? 1
1e915849
RS
4728 : 0)), 4,
4729 RELAX_BRANCH_ENCODE
66b3e8da 4730 (AT,
11625dd8
RS
4731 uncond_branch_p (ip),
4732 branch_likely_p (ip),
1e915849
RS
4733 pinfo & INSN_WRITE_GPR_31,
4734 0),
4735 address_expr->X_add_symbol,
4736 address_expr->X_add_number);
4a6a3df4
AO
4737 *reloc_type = BFD_RELOC_UNUSED;
4738 }
df58fc94
RS
4739 else if (mips_opts.micromips
4740 && address_expr
4741 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
4742 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
4743 && (delayed_branch_p (ip) || compact_branch_p (ip))
4744 /* Don't try branch relaxation when users specify
4745 16-bit/32-bit instructions. */
4746 && !forced_insn_length)
df58fc94
RS
4747 {
4748 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
4749 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
4750 int uncond = uncond_branch_p (ip) ? -1 : 0;
4751 int compact = compact_branch_p (ip);
df58fc94
RS
4752 int al = pinfo & INSN_WRITE_GPR_31;
4753 int length32;
4754
4755 gas_assert (address_expr != NULL);
4756 gas_assert (!mips_relax.sequence);
4757
2b0c8b40 4758 relaxed_branch = TRUE;
df58fc94
RS
4759 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
4760 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
4761 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
4762 relax32, 0, 0),
df58fc94
RS
4763 address_expr->X_add_symbol,
4764 address_expr->X_add_number);
4765 *reloc_type = BFD_RELOC_UNUSED;
4766 }
4767 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132
RH
4768 {
4769 /* We need to set up a variant frag. */
df58fc94 4770 gas_assert (address_expr != NULL);
1e915849
RS
4771 add_relaxed_insn (ip, 4, 0,
4772 RELAX_MIPS16_ENCODE
4773 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 4774 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 4775 delayed_branch_p (&history[0]),
1e915849
RS
4776 history[0].mips16_absolute_jump_p),
4777 make_expr_symbol (address_expr), 0);
252b5132 4778 }
5c04167a 4779 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 4780 {
11625dd8 4781 if (!delayed_branch_p (ip))
b8ee1a6e
DU
4782 /* Make sure there is enough room to swap this instruction with
4783 a following jump instruction. */
4784 frag_grow (6);
1e915849 4785 add_fixed_insn (ip);
252b5132
RH
4786 }
4787 else
4788 {
4789 if (mips_opts.mips16
4790 && mips_opts.noreorder
11625dd8 4791 && delayed_branch_p (&history[0]))
252b5132
RH
4792 as_warn (_("extended instruction in delay slot"));
4793
4d7206a2
RS
4794 if (mips_relax.sequence)
4795 {
4796 /* If we've reached the end of this frag, turn it into a variant
4797 frag and record the information for the instructions we've
4798 written so far. */
4799 if (frag_room () < 4)
4800 relax_close_frag ();
df58fc94 4801 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
4802 }
4803
584892a6 4804 if (mips_relax.sequence != 2)
df58fc94
RS
4805 {
4806 if (mips_macro_warning.first_insn_sizes[0] == 0)
4807 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
4808 mips_macro_warning.sizes[0] += insn_length (ip);
4809 mips_macro_warning.insns[0]++;
4810 }
584892a6 4811 if (mips_relax.sequence != 1)
df58fc94
RS
4812 {
4813 if (mips_macro_warning.first_insn_sizes[1] == 0)
4814 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
4815 mips_macro_warning.sizes[1] += insn_length (ip);
4816 mips_macro_warning.insns[1]++;
4817 }
584892a6 4818
1e915849
RS
4819 if (mips_opts.mips16)
4820 {
4821 ip->fixed_p = 1;
4822 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
4823 }
4824 add_fixed_insn (ip);
252b5132
RH
4825 }
4826
9fe77896 4827 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 4828 {
df58fc94 4829 bfd_reloc_code_real_type final_type[3];
2309ddf2 4830 reloc_howto_type *howto0;
9fe77896
RS
4831 reloc_howto_type *howto;
4832 int i;
34ce925e 4833
df58fc94
RS
4834 /* Perform any necessary conversion to microMIPS relocations
4835 and find out how many relocations there actually are. */
4836 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
4837 final_type[i] = micromips_map_reloc (reloc_type[i]);
4838
9fe77896
RS
4839 /* In a compound relocation, it is the final (outermost)
4840 operator that determines the relocated field. */
2309ddf2 4841 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
4842 if (!howto)
4843 abort ();
2309ddf2
MR
4844
4845 if (i > 1)
4846 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
4847 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
4848 bfd_get_reloc_size (howto),
4849 address_expr,
2309ddf2
MR
4850 howto0 && howto0->pc_relative,
4851 final_type[0]);
9fe77896
RS
4852
4853 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 4854 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
4855 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
4856
4857 /* These relocations can have an addend that won't fit in
4858 4 octets for 64bit assembly. */
4859 if (HAVE_64BIT_GPRS
4860 && ! howto->partial_inplace
4861 && (reloc_type[0] == BFD_RELOC_16
4862 || reloc_type[0] == BFD_RELOC_32
4863 || reloc_type[0] == BFD_RELOC_MIPS_JMP
4864 || reloc_type[0] == BFD_RELOC_GPREL16
4865 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
4866 || reloc_type[0] == BFD_RELOC_GPREL32
4867 || reloc_type[0] == BFD_RELOC_64
4868 || reloc_type[0] == BFD_RELOC_CTOR
4869 || reloc_type[0] == BFD_RELOC_MIPS_SUB
4870 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
4871 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
4872 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
4873 || reloc_type[0] == BFD_RELOC_MIPS_REL16
4874 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
4875 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
4876 || hi16_reloc_p (reloc_type[0])
4877 || lo16_reloc_p (reloc_type[0])))
4878 ip->fixp[0]->fx_no_overflow = 1;
4879
ddaf2c41
MR
4880 /* These relocations can have an addend that won't fit in 2 octets. */
4881 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
4882 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
4883 ip->fixp[0]->fx_no_overflow = 1;
4884
9fe77896
RS
4885 if (mips_relax.sequence)
4886 {
4887 if (mips_relax.first_fixup == 0)
4888 mips_relax.first_fixup = ip->fixp[0];
4889 }
4890 else if (reloc_needs_lo_p (*reloc_type))
4891 {
4892 struct mips_hi_fixup *hi_fixup;
4893
4894 /* Reuse the last entry if it already has a matching %lo. */
4895 hi_fixup = mips_hi_fixup_list;
4896 if (hi_fixup == 0
4897 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 4898 {
9fe77896
RS
4899 hi_fixup = ((struct mips_hi_fixup *)
4900 xmalloc (sizeof (struct mips_hi_fixup)));
4901 hi_fixup->next = mips_hi_fixup_list;
4902 mips_hi_fixup_list = hi_fixup;
4d7206a2 4903 }
9fe77896
RS
4904 hi_fixup->fixp = ip->fixp[0];
4905 hi_fixup->seg = now_seg;
4906 }
252b5132 4907
9fe77896
RS
4908 /* Add fixups for the second and third relocations, if given.
4909 Note that the ABI allows the second relocation to be
4910 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
4911 moment we only use RSS_UNDEF, but we could add support
4912 for the others if it ever becomes necessary. */
4913 for (i = 1; i < 3; i++)
4914 if (reloc_type[i] != BFD_RELOC_UNUSED)
4915 {
4916 ip->fixp[i] = fix_new (ip->frag, ip->where,
4917 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 4918 FALSE, final_type[i]);
f6688943 4919
9fe77896
RS
4920 /* Use fx_tcbit to mark compound relocs. */
4921 ip->fixp[0]->fx_tcbit = 1;
4922 ip->fixp[i]->fx_tcbit = 1;
4923 }
252b5132 4924 }
1e915849 4925 install_insn (ip);
252b5132
RH
4926
4927 /* Update the register mask information. */
4c260379
RS
4928 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
4929 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 4930
a4e06468 4931 switch (method)
252b5132 4932 {
a4e06468
RS
4933 case APPEND_ADD:
4934 insert_into_history (0, 1, ip);
4935 break;
4936
4937 case APPEND_ADD_WITH_NOP:
14fe068b
RS
4938 {
4939 struct mips_cl_insn *nop;
4940
4941 insert_into_history (0, 1, ip);
4942 nop = get_delay_slot_nop (ip);
4943 add_fixed_insn (nop);
4944 insert_into_history (0, 1, nop);
4945 if (mips_relax.sequence)
4946 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
4947 }
a4e06468
RS
4948 break;
4949
4950 case APPEND_ADD_COMPACT:
4951 /* Convert MIPS16 jr/jalr into a "compact" jump. */
4952 gas_assert (mips_opts.mips16);
4953 ip->insn_opcode |= 0x0080;
4954 find_altered_mips16_opcode (ip);
4955 install_insn (ip);
4956 insert_into_history (0, 1, ip);
4957 break;
4958
4959 case APPEND_SWAP:
4960 {
4961 struct mips_cl_insn delay = history[0];
4962 if (mips_opts.mips16)
4963 {
4964 know (delay.frag == ip->frag);
4965 move_insn (ip, delay.frag, delay.where);
4966 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
4967 }
464ab0e5 4968 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
4969 {
4970 /* Add the delay slot instruction to the end of the
4971 current frag and shrink the fixed part of the
4972 original frag. If the branch occupies the tail of
4973 the latter, move it backwards to cover the gap. */
2b0c8b40 4974 delay.frag->fr_fix -= branch_disp;
a4e06468 4975 if (delay.frag == ip->frag)
2b0c8b40 4976 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
4977 add_fixed_insn (&delay);
4978 }
4979 else
4980 {
2b0c8b40
MR
4981 move_insn (&delay, ip->frag,
4982 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
4983 move_insn (ip, history[0].frag, history[0].where);
4984 }
4985 history[0] = *ip;
4986 delay.fixed_p = 1;
4987 insert_into_history (0, 1, &delay);
4988 }
4989 break;
252b5132
RH
4990 }
4991
13408f1e 4992 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
4993 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
4994 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
4995 {
4996 unsigned int i;
4997
79850f26 4998 mips_no_prev_insn ();
13408f1e 4999
e407c74b 5000 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 5001 history[i].cleared_p = 1;
e407c74b
NC
5002 }
5003
df58fc94
RS
5004 /* We need to emit a label at the end of branch-likely macros. */
5005 if (emit_branch_likely_macro)
5006 {
5007 emit_branch_likely_macro = FALSE;
5008 micromips_add_label ();
5009 }
5010
252b5132
RH
5011 /* We just output an insn, so the next one doesn't have a label. */
5012 mips_clear_insn_labels ();
252b5132
RH
5013}
5014
e407c74b
NC
5015/* Forget that there was any previous instruction or label.
5016 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
5017
5018static void
7d10b47d 5019mips_no_prev_insn (void)
252b5132 5020{
7d10b47d
RS
5021 prev_nop_frag = NULL;
5022 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
5023 mips_clear_insn_labels ();
5024}
5025
7d10b47d
RS
5026/* This function must be called before we emit something other than
5027 instructions. It is like mips_no_prev_insn except that it inserts
5028 any NOPS that might be needed by previous instructions. */
252b5132 5029
7d10b47d
RS
5030void
5031mips_emit_delays (void)
252b5132
RH
5032{
5033 if (! mips_opts.noreorder)
5034 {
932d1a1b 5035 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
5036 if (nops > 0)
5037 {
7d10b47d
RS
5038 while (nops-- > 0)
5039 add_fixed_insn (NOP_INSN);
462427c4 5040 mips_move_text_labels ();
7d10b47d
RS
5041 }
5042 }
5043 mips_no_prev_insn ();
5044}
5045
5046/* Start a (possibly nested) noreorder block. */
5047
5048static void
5049start_noreorder (void)
5050{
5051 if (mips_opts.noreorder == 0)
5052 {
5053 unsigned int i;
5054 int nops;
5055
5056 /* None of the instructions before the .set noreorder can be moved. */
5057 for (i = 0; i < ARRAY_SIZE (history); i++)
5058 history[i].fixed_p = 1;
5059
5060 /* Insert any nops that might be needed between the .set noreorder
5061 block and the previous instructions. We will later remove any
5062 nops that turn out not to be needed. */
932d1a1b 5063 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
5064 if (nops > 0)
5065 {
5066 if (mips_optimize != 0)
252b5132
RH
5067 {
5068 /* Record the frag which holds the nop instructions, so
5069 that we can remove them if we don't need them. */
df58fc94 5070 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
5071 prev_nop_frag = frag_now;
5072 prev_nop_frag_holds = nops;
5073 prev_nop_frag_required = 0;
5074 prev_nop_frag_since = 0;
5075 }
5076
5077 for (; nops > 0; --nops)
1e915849 5078 add_fixed_insn (NOP_INSN);
252b5132 5079
7d10b47d
RS
5080 /* Move on to a new frag, so that it is safe to simply
5081 decrease the size of prev_nop_frag. */
5082 frag_wane (frag_now);
5083 frag_new (0);
462427c4 5084 mips_move_text_labels ();
252b5132 5085 }
df58fc94 5086 mips_mark_labels ();
7d10b47d 5087 mips_clear_insn_labels ();
252b5132 5088 }
7d10b47d
RS
5089 mips_opts.noreorder++;
5090 mips_any_noreorder = 1;
5091}
252b5132 5092
7d10b47d 5093/* End a nested noreorder block. */
252b5132 5094
7d10b47d
RS
5095static void
5096end_noreorder (void)
5097{
5098 mips_opts.noreorder--;
5099 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
5100 {
5101 /* Commit to inserting prev_nop_frag_required nops and go back to
5102 handling nop insertion the .set reorder way. */
5103 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 5104 * NOP_INSN_SIZE);
7d10b47d
RS
5105 insert_into_history (prev_nop_frag_since,
5106 prev_nop_frag_required, NOP_INSN);
5107 prev_nop_frag = NULL;
5108 }
252b5132
RH
5109}
5110
584892a6
RS
5111/* Set up global variables for the start of a new macro. */
5112
5113static void
5114macro_start (void)
5115{
5116 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
5117 memset (&mips_macro_warning.first_insn_sizes, 0,
5118 sizeof (mips_macro_warning.first_insn_sizes));
5119 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 5120 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 5121 && delayed_branch_p (&history[0]));
df58fc94
RS
5122 switch (history[0].insn_mo->pinfo2
5123 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
5124 {
5125 case INSN2_BRANCH_DELAY_32BIT:
5126 mips_macro_warning.delay_slot_length = 4;
5127 break;
5128 case INSN2_BRANCH_DELAY_16BIT:
5129 mips_macro_warning.delay_slot_length = 2;
5130 break;
5131 default:
5132 mips_macro_warning.delay_slot_length = 0;
5133 break;
5134 }
5135 mips_macro_warning.first_frag = NULL;
584892a6
RS
5136}
5137
df58fc94
RS
5138/* Given that a macro is longer than one instruction or of the wrong size,
5139 return the appropriate warning for it. Return null if no warning is
5140 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
5141 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
5142 and RELAX_NOMACRO. */
584892a6
RS
5143
5144static const char *
5145macro_warning (relax_substateT subtype)
5146{
5147 if (subtype & RELAX_DELAY_SLOT)
5148 return _("Macro instruction expanded into multiple instructions"
5149 " in a branch delay slot");
5150 else if (subtype & RELAX_NOMACRO)
5151 return _("Macro instruction expanded into multiple instructions");
df58fc94
RS
5152 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
5153 | RELAX_DELAY_SLOT_SIZE_SECOND))
5154 return ((subtype & RELAX_DELAY_SLOT_16BIT)
5155 ? _("Macro instruction expanded into a wrong size instruction"
5156 " in a 16-bit branch delay slot")
5157 : _("Macro instruction expanded into a wrong size instruction"
5158 " in a 32-bit branch delay slot"));
584892a6
RS
5159 else
5160 return 0;
5161}
5162
5163/* Finish up a macro. Emit warnings as appropriate. */
5164
5165static void
5166macro_end (void)
5167{
df58fc94
RS
5168 /* Relaxation warning flags. */
5169 relax_substateT subtype = 0;
5170
5171 /* Check delay slot size requirements. */
5172 if (mips_macro_warning.delay_slot_length == 2)
5173 subtype |= RELAX_DELAY_SLOT_16BIT;
5174 if (mips_macro_warning.delay_slot_length != 0)
584892a6 5175 {
df58fc94
RS
5176 if (mips_macro_warning.delay_slot_length
5177 != mips_macro_warning.first_insn_sizes[0])
5178 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
5179 if (mips_macro_warning.delay_slot_length
5180 != mips_macro_warning.first_insn_sizes[1])
5181 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
5182 }
584892a6 5183
df58fc94
RS
5184 /* Check instruction count requirements. */
5185 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
5186 {
5187 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
5188 subtype |= RELAX_SECOND_LONGER;
5189 if (mips_opts.warn_about_macros)
5190 subtype |= RELAX_NOMACRO;
5191 if (mips_macro_warning.delay_slot_p)
5192 subtype |= RELAX_DELAY_SLOT;
df58fc94 5193 }
584892a6 5194
df58fc94
RS
5195 /* If both alternatives fail to fill a delay slot correctly,
5196 emit the warning now. */
5197 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
5198 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
5199 {
5200 relax_substateT s;
5201 const char *msg;
5202
5203 s = subtype & (RELAX_DELAY_SLOT_16BIT
5204 | RELAX_DELAY_SLOT_SIZE_FIRST
5205 | RELAX_DELAY_SLOT_SIZE_SECOND);
5206 msg = macro_warning (s);
5207 if (msg != NULL)
5208 as_warn ("%s", msg);
5209 subtype &= ~s;
5210 }
5211
5212 /* If both implementations are longer than 1 instruction, then emit the
5213 warning now. */
5214 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
5215 {
5216 relax_substateT s;
5217 const char *msg;
5218
5219 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
5220 msg = macro_warning (s);
5221 if (msg != NULL)
5222 as_warn ("%s", msg);
5223 subtype &= ~s;
584892a6 5224 }
df58fc94
RS
5225
5226 /* If any flags still set, then one implementation might need a warning
5227 and the other either will need one of a different kind or none at all.
5228 Pass any remaining flags over to relaxation. */
5229 if (mips_macro_warning.first_frag != NULL)
5230 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
5231}
5232
df58fc94
RS
5233/* Instruction operand formats used in macros that vary between
5234 standard MIPS and microMIPS code. */
5235
833794fc 5236static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
5237static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
5238static const char * const jalr_fmt[2] = { "d,s", "t,s" };
5239static const char * const lui_fmt[2] = { "t,u", "s,u" };
5240static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 5241static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
5242static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
5243static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
5244
833794fc 5245#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
5246#define COP12_FMT (cop12_fmt[mips_opts.micromips])
5247#define JALR_FMT (jalr_fmt[mips_opts.micromips])
5248#define LUI_FMT (lui_fmt[mips_opts.micromips])
5249#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
833794fc 5250#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
5251#define SHFT_FMT (shft_fmt[mips_opts.micromips])
5252#define TRAP_FMT (trap_fmt[mips_opts.micromips])
5253
6e1304d8
RS
5254/* Read a macro's relocation codes from *ARGS and store them in *R.
5255 The first argument in *ARGS will be either the code for a single
5256 relocation or -1 followed by the three codes that make up a
5257 composite relocation. */
5258
5259static void
5260macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
5261{
5262 int i, next;
5263
5264 next = va_arg (*args, int);
5265 if (next >= 0)
5266 r[0] = (bfd_reloc_code_real_type) next;
5267 else
5268 for (i = 0; i < 3; i++)
5269 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
5270}
5271
252b5132
RH
5272/* Build an instruction created by a macro expansion. This is passed
5273 a pointer to the count of instructions created so far, an
5274 expression, the name of the instruction to build, an operand format
5275 string, and corresponding arguments. */
5276
252b5132 5277static void
67c0d1eb 5278macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 5279{
df58fc94 5280 const struct mips_opcode *mo = NULL;
f6688943 5281 bfd_reloc_code_real_type r[3];
df58fc94
RS
5282 const struct mips_opcode *amo;
5283 struct hash_control *hash;
5284 struct mips_cl_insn insn;
252b5132 5285 va_list args;
252b5132 5286
252b5132 5287 va_start (args, fmt);
252b5132 5288
252b5132
RH
5289 if (mips_opts.mips16)
5290 {
03ea81db 5291 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
5292 va_end (args);
5293 return;
5294 }
5295
f6688943
TS
5296 r[0] = BFD_RELOC_UNUSED;
5297 r[1] = BFD_RELOC_UNUSED;
5298 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
5299 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
5300 amo = (struct mips_opcode *) hash_find (hash, name);
5301 gas_assert (amo);
5302 gas_assert (strcmp (name, amo->name) == 0);
1e915849 5303
df58fc94 5304 do
8b082fb1
TS
5305 {
5306 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
5307 macros will never generate MDMX, MIPS-3D, or MT instructions.
5308 We try to match an instruction that fulfils the branch delay
5309 slot instruction length requirement (if any) of the previous
5310 instruction. While doing this we record the first instruction
5311 seen that matches all the other conditions and use it anyway
5312 if the requirement cannot be met; we will issue an appropriate
5313 warning later on. */
5314 if (strcmp (fmt, amo->args) == 0
5315 && amo->pinfo != INSN_MACRO
5316 && is_opcode_valid (amo)
5317 && is_size_valid (amo))
5318 {
5319 if (is_delay_slot_valid (amo))
5320 {
5321 mo = amo;
5322 break;
5323 }
5324 else if (!mo)
5325 mo = amo;
5326 }
8b082fb1 5327
df58fc94
RS
5328 ++amo;
5329 gas_assert (amo->name);
252b5132 5330 }
df58fc94 5331 while (strcmp (name, amo->name) == 0);
252b5132 5332
df58fc94 5333 gas_assert (mo);
1e915849 5334 create_insn (&insn, mo);
252b5132
RH
5335 for (;;)
5336 {
5337 switch (*fmt++)
5338 {
5339 case '\0':
5340 break;
5341
5342 case ',':
5343 case '(':
5344 case ')':
5345 continue;
5346
5f74bc13
CD
5347 case '+':
5348 switch (*fmt++)
5349 {
5350 case 'A':
5351 case 'E':
df58fc94
RS
5352 INSERT_OPERAND (mips_opts.micromips,
5353 EXTLSB, insn, va_arg (args, int));
5f74bc13
CD
5354 continue;
5355
5356 case 'B':
5357 case 'F':
5358 /* Note that in the macro case, these arguments are already
5359 in MSB form. (When handling the instruction in the
5360 non-macro case, these arguments are sizes from which
5361 MSB values must be calculated.) */
df58fc94
RS
5362 INSERT_OPERAND (mips_opts.micromips,
5363 INSMSB, insn, va_arg (args, int));
5f74bc13
CD
5364 continue;
5365
b015e599
AP
5366 case 'J':
5367 gas_assert (!mips_opts.micromips);
5368 INSERT_OPERAND (0, CODE10, insn, va_arg (args, int));
5369 continue;
5370
5f74bc13
CD
5371 case 'C':
5372 case 'G':
5373 case 'H':
5374 /* Note that in the macro case, these arguments are already
5375 in MSBD form. (When handling the instruction in the
5376 non-macro case, these arguments are sizes from which
5377 MSBD values must be calculated.) */
df58fc94
RS
5378 INSERT_OPERAND (mips_opts.micromips,
5379 EXTMSBD, insn, va_arg (args, int));
5f74bc13
CD
5380 continue;
5381
dd3cbb7e 5382 case 'Q':
df58fc94
RS
5383 gas_assert (!mips_opts.micromips);
5384 INSERT_OPERAND (0, SEQI, insn, va_arg (args, int));
dd3cbb7e
NC
5385 continue;
5386
7f3c4072
CM
5387 case 'j':
5388 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, insn, va_arg (args, int));
5389 continue;
5390
5f74bc13 5391 default:
b37df7c4 5392 abort ();
5f74bc13
CD
5393 }
5394 continue;
5395
8b082fb1 5396 case '2':
03f66e8a 5397 INSERT_OPERAND (mips_opts.micromips, BP, insn, va_arg (args, int));
8b082fb1
TS
5398 continue;
5399
df58fc94
RS
5400 case 'n':
5401 gas_assert (mips_opts.micromips);
252b5132
RH
5402 case 't':
5403 case 'w':
5404 case 'E':
df58fc94 5405 INSERT_OPERAND (mips_opts.micromips, RT, insn, va_arg (args, int));
252b5132
RH
5406 continue;
5407
5408 case 'c':
833794fc 5409 INSERT_OPERAND (mips_opts.micromips, CODE, insn, va_arg (args, int));
38487616
TS
5410 continue;
5411
252b5132 5412 case 'W':
df58fc94
RS
5413 gas_assert (!mips_opts.micromips);
5414 case 'T':
5415 INSERT_OPERAND (mips_opts.micromips, FT, insn, va_arg (args, int));
252b5132
RH
5416 continue;
5417
252b5132 5418 case 'G':
df58fc94
RS
5419 if (mips_opts.micromips)
5420 INSERT_OPERAND (1, RS, insn, va_arg (args, int));
5421 else
5422 INSERT_OPERAND (0, RD, insn, va_arg (args, int));
5423 continue;
5424
af7ee8bf 5425 case 'K':
df58fc94
RS
5426 gas_assert (!mips_opts.micromips);
5427 case 'd':
5428 INSERT_OPERAND (mips_opts.micromips, RD, insn, va_arg (args, int));
252b5132
RH
5429 continue;
5430
4372b673 5431 case 'U':
df58fc94 5432 gas_assert (!mips_opts.micromips);
4372b673
NC
5433 {
5434 int tmp = va_arg (args, int);
5435
df58fc94
RS
5436 INSERT_OPERAND (0, RT, insn, tmp);
5437 INSERT_OPERAND (0, RD, insn, tmp);
4372b673 5438 }
df58fc94 5439 continue;
4372b673 5440
252b5132
RH
5441 case 'V':
5442 case 'S':
df58fc94
RS
5443 gas_assert (!mips_opts.micromips);
5444 INSERT_OPERAND (0, FS, insn, va_arg (args, int));
252b5132
RH
5445 continue;
5446
5447 case 'z':
5448 continue;
5449
5450 case '<':
df58fc94
RS
5451 INSERT_OPERAND (mips_opts.micromips,
5452 SHAMT, insn, va_arg (args, int));
252b5132
RH
5453 continue;
5454
5455 case 'D':
df58fc94
RS
5456 gas_assert (!mips_opts.micromips);
5457 INSERT_OPERAND (0, FD, insn, va_arg (args, int));
252b5132
RH
5458 continue;
5459
5460 case 'B':
df58fc94
RS
5461 gas_assert (!mips_opts.micromips);
5462 INSERT_OPERAND (0, CODE20, insn, va_arg (args, int));
252b5132
RH
5463 continue;
5464
4372b673 5465 case 'J':
df58fc94
RS
5466 gas_assert (!mips_opts.micromips);
5467 INSERT_OPERAND (0, CODE19, insn, va_arg (args, int));
4372b673
NC
5468 continue;
5469
252b5132 5470 case 'q':
df58fc94
RS
5471 gas_assert (!mips_opts.micromips);
5472 INSERT_OPERAND (0, CODE2, insn, va_arg (args, int));
252b5132
RH
5473 continue;
5474
5475 case 'b':
5476 case 's':
5477 case 'r':
5478 case 'v':
df58fc94 5479 INSERT_OPERAND (mips_opts.micromips, RS, insn, va_arg (args, int));
252b5132
RH
5480 continue;
5481
5482 case 'i':
5483 case 'j':
6e1304d8 5484 macro_read_relocs (&args, r);
9c2799c2 5485 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
5486 || *r == BFD_RELOC_MIPS_HIGHER
5487 || *r == BFD_RELOC_HI16_S
5488 || *r == BFD_RELOC_LO16
5489 || *r == BFD_RELOC_MIPS_GOT_OFST);
5490 continue;
5491
5492 case 'o':
5493 macro_read_relocs (&args, r);
252b5132
RH
5494 continue;
5495
5496 case 'u':
6e1304d8 5497 macro_read_relocs (&args, r);
9c2799c2 5498 gas_assert (ep != NULL
90ecf173
MR
5499 && (ep->X_op == O_constant
5500 || (ep->X_op == O_symbol
5501 && (*r == BFD_RELOC_MIPS_HIGHEST
5502 || *r == BFD_RELOC_HI16_S
5503 || *r == BFD_RELOC_HI16
5504 || *r == BFD_RELOC_GPREL16
5505 || *r == BFD_RELOC_MIPS_GOT_HI16
5506 || *r == BFD_RELOC_MIPS_CALL_HI16))));
252b5132
RH
5507 continue;
5508
5509 case 'p':
9c2799c2 5510 gas_assert (ep != NULL);
bad36eac 5511
252b5132
RH
5512 /*
5513 * This allows macro() to pass an immediate expression for
5514 * creating short branches without creating a symbol.
bad36eac
DJ
5515 *
5516 * We don't allow branch relaxation for these branches, as
5517 * they should only appear in ".set nomacro" anyway.
252b5132
RH
5518 */
5519 if (ep->X_op == O_constant)
5520 {
df58fc94
RS
5521 /* For microMIPS we always use relocations for branches.
5522 So we should not resolve immediate values. */
5523 gas_assert (!mips_opts.micromips);
5524
bad36eac
DJ
5525 if ((ep->X_add_number & 3) != 0)
5526 as_bad (_("branch to misaligned address (0x%lx)"),
5527 (unsigned long) ep->X_add_number);
5528 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
5529 as_bad (_("branch address range overflow (0x%lx)"),
5530 (unsigned long) ep->X_add_number);
252b5132
RH
5531 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
5532 ep = NULL;
5533 }
5534 else
0b25d3e6 5535 *r = BFD_RELOC_16_PCREL_S2;
252b5132
RH
5536 continue;
5537
5538 case 'a':
9c2799c2 5539 gas_assert (ep != NULL);
f6688943 5540 *r = BFD_RELOC_MIPS_JMP;
252b5132
RH
5541 continue;
5542
5543 case 'C':
df58fc94
RS
5544 gas_assert (!mips_opts.micromips);
5545 INSERT_OPERAND (0, COPZ, insn, va_arg (args, unsigned long));
252b5132
RH
5546 continue;
5547
d43b4baf 5548 case 'k':
df58fc94
RS
5549 INSERT_OPERAND (mips_opts.micromips,
5550 CACHE, insn, va_arg (args, unsigned long));
5551 continue;
5552
5553 case '|':
5554 gas_assert (mips_opts.micromips);
5555 INSERT_OPERAND (1, TRAP, insn, va_arg (args, int));
5556 continue;
5557
5558 case '.':
5559 gas_assert (mips_opts.micromips);
5560 INSERT_OPERAND (1, OFFSET10, insn, va_arg (args, int));
5561 continue;
5562
dec0624d
MR
5563 case '\\':
5564 INSERT_OPERAND (mips_opts.micromips,
5565 3BITPOS, insn, va_arg (args, unsigned int));
5566 continue;
5567
df58fc94 5568 case '~':
dec0624d
MR
5569 INSERT_OPERAND (mips_opts.micromips,
5570 OFFSET12, insn, va_arg (args, unsigned long));
df58fc94
RS
5571 continue;
5572
5573 case 'N':
5574 gas_assert (mips_opts.micromips);
5575 INSERT_OPERAND (1, BCC, insn, va_arg (args, int));
5576 continue;
5577
5578 case 'm': /* Opcode extension character. */
5579 gas_assert (mips_opts.micromips);
5580 switch (*fmt++)
5581 {
5582 case 'j':
5583 INSERT_OPERAND (1, MJ, insn, va_arg (args, int));
5584 break;
5585
5586 case 'p':
5587 INSERT_OPERAND (1, MP, insn, va_arg (args, int));
5588 break;
5589
5590 case 'F':
5591 INSERT_OPERAND (1, IMMF, insn, va_arg (args, int));
5592 break;
5593
5594 default:
b37df7c4 5595 abort ();
df58fc94 5596 }
d43b4baf
TS
5597 continue;
5598
252b5132 5599 default:
b37df7c4 5600 abort ();
252b5132
RH
5601 }
5602 break;
5603 }
5604 va_end (args);
9c2799c2 5605 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5606
df58fc94 5607 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5608}
5609
5610static void
67c0d1eb 5611mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 5612 va_list *args)
252b5132 5613{
1e915849 5614 struct mips_opcode *mo;
252b5132 5615 struct mips_cl_insn insn;
f6688943
TS
5616 bfd_reloc_code_real_type r[3]
5617 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 5618
1e915849 5619 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
5620 gas_assert (mo);
5621 gas_assert (strcmp (name, mo->name) == 0);
252b5132 5622
1e915849 5623 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 5624 {
1e915849 5625 ++mo;
9c2799c2
NC
5626 gas_assert (mo->name);
5627 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
5628 }
5629
1e915849 5630 create_insn (&insn, mo);
252b5132
RH
5631 for (;;)
5632 {
5633 int c;
5634
5635 c = *fmt++;
5636 switch (c)
5637 {
5638 case '\0':
5639 break;
5640
5641 case ',':
5642 case '(':
5643 case ')':
5644 continue;
5645
5646 case 'y':
5647 case 'w':
03ea81db 5648 MIPS16_INSERT_OPERAND (RY, insn, va_arg (*args, int));
252b5132
RH
5649 continue;
5650
5651 case 'x':
5652 case 'v':
03ea81db 5653 MIPS16_INSERT_OPERAND (RX, insn, va_arg (*args, int));
252b5132
RH
5654 continue;
5655
5656 case 'z':
03ea81db 5657 MIPS16_INSERT_OPERAND (RZ, insn, va_arg (*args, int));
252b5132
RH
5658 continue;
5659
5660 case 'Z':
03ea81db 5661 MIPS16_INSERT_OPERAND (MOVE32Z, insn, va_arg (*args, int));
252b5132
RH
5662 continue;
5663
5664 case '0':
5665 case 'S':
5666 case 'P':
5667 case 'R':
5668 continue;
5669
5670 case 'X':
03ea81db 5671 MIPS16_INSERT_OPERAND (REGR32, insn, va_arg (*args, int));
252b5132
RH
5672 continue;
5673
5674 case 'Y':
5675 {
5676 int regno;
5677
03ea81db 5678 regno = va_arg (*args, int);
252b5132 5679 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
a9e24354 5680 MIPS16_INSERT_OPERAND (REG32R, insn, regno);
252b5132
RH
5681 }
5682 continue;
5683
5684 case '<':
5685 case '>':
5686 case '4':
5687 case '5':
5688 case 'H':
5689 case 'W':
5690 case 'D':
5691 case 'j':
5692 case '8':
5693 case 'V':
5694 case 'C':
5695 case 'U':
5696 case 'k':
5697 case 'K':
5698 case 'p':
5699 case 'q':
5700 {
b886a2ab
RS
5701 offsetT value;
5702
9c2799c2 5703 gas_assert (ep != NULL);
252b5132
RH
5704
5705 if (ep->X_op != O_constant)
874e8986 5706 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 5707 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 5708 {
b886a2ab 5709 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 5710 ep = NULL;
f6688943 5711 *r = BFD_RELOC_UNUSED;
252b5132
RH
5712 }
5713 }
5714 continue;
5715
5716 case '6':
03ea81db 5717 MIPS16_INSERT_OPERAND (IMM6, insn, va_arg (*args, int));
252b5132
RH
5718 continue;
5719 }
5720
5721 break;
5722 }
5723
9c2799c2 5724 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 5725
df58fc94 5726 append_insn (&insn, ep, r, TRUE);
252b5132
RH
5727}
5728
2051e8c4
MR
5729/*
5730 * Sign-extend 32-bit mode constants that have bit 31 set and all
5731 * higher bits unset.
5732 */
9f872bbe 5733static void
2051e8c4
MR
5734normalize_constant_expr (expressionS *ex)
5735{
9ee2a2d4 5736 if (ex->X_op == O_constant
2051e8c4
MR
5737 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5738 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5739 - 0x80000000);
5740}
5741
5742/*
5743 * Sign-extend 32-bit mode address offsets that have bit 31 set and
5744 * all higher bits unset.
5745 */
5746static void
5747normalize_address_expr (expressionS *ex)
5748{
5749 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
5750 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
5751 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
5752 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
5753 - 0x80000000);
5754}
5755
438c16b8
TS
5756/*
5757 * Generate a "jalr" instruction with a relocation hint to the called
5758 * function. This occurs in NewABI PIC code.
5759 */
5760static void
df58fc94 5761macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 5762{
df58fc94
RS
5763 static const bfd_reloc_code_real_type jalr_relocs[2]
5764 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
5765 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
5766 const char *jalr;
685736be 5767 char *f = NULL;
b34976b6 5768
1180b5a4 5769 if (MIPS_JALR_HINT_P (ep))
f21f8242 5770 {
cc3d92a5 5771 frag_grow (8);
f21f8242
AO
5772 f = frag_more (0);
5773 }
2906b037 5774 if (mips_opts.micromips)
df58fc94 5775 {
833794fc
MR
5776 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
5777 ? "jalr" : "jalrs");
e64af278 5778 if (MIPS_JALR_HINT_P (ep)
833794fc 5779 || mips_opts.insn32
e64af278 5780 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
5781 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
5782 else
5783 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
5784 }
2906b037
MR
5785 else
5786 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 5787 if (MIPS_JALR_HINT_P (ep))
df58fc94 5788 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
5789}
5790
252b5132
RH
5791/*
5792 * Generate a "lui" instruction.
5793 */
5794static void
67c0d1eb 5795macro_build_lui (expressionS *ep, int regnum)
252b5132 5796{
9c2799c2 5797 gas_assert (! mips_opts.mips16);
252b5132 5798
df58fc94 5799 if (ep->X_op != O_constant)
252b5132 5800 {
9c2799c2 5801 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
5802 /* _gp_disp is a special case, used from s_cpload.
5803 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 5804 gas_assert (mips_pic == NO_PIC
78e1bb40 5805 || (! HAVE_NEWABI
aa6975fb
ILT
5806 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
5807 || (! mips_in_shared
bbe506e8
TS
5808 && strcmp (S_GET_NAME (ep->X_add_symbol),
5809 "__gnu_local_gp") == 0));
252b5132
RH
5810 }
5811
df58fc94 5812 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
5813}
5814
885add95
CD
5815/* Generate a sequence of instructions to do a load or store from a constant
5816 offset off of a base register (breg) into/from a target register (treg),
5817 using AT if necessary. */
5818static void
67c0d1eb
RS
5819macro_build_ldst_constoffset (expressionS *ep, const char *op,
5820 int treg, int breg, int dbl)
885add95 5821{
9c2799c2 5822 gas_assert (ep->X_op == O_constant);
885add95 5823
256ab948 5824 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5825 if (!dbl)
5826 normalize_constant_expr (ep);
256ab948 5827
67c1ffbe 5828 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 5829 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
5830 as_warn (_("operand overflow"));
5831
5832 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
5833 {
5834 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 5835 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
5836 }
5837 else
5838 {
5839 /* 32-bit offset, need multiple instructions and AT, like:
5840 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
5841 addu $tempreg,$tempreg,$breg
5842 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
5843 to handle the complete offset. */
67c0d1eb
RS
5844 macro_build_lui (ep, AT);
5845 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
5846 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 5847
741fe287 5848 if (!mips_opts.at)
8fc2e39e 5849 as_bad (_("Macro used $at after \".set noat\""));
885add95
CD
5850 }
5851}
5852
252b5132
RH
5853/* set_at()
5854 * Generates code to set the $at register to true (one)
5855 * if reg is less than the immediate expression.
5856 */
5857static void
67c0d1eb 5858set_at (int reg, int unsignedp)
252b5132
RH
5859{
5860 if (imm_expr.X_op == O_constant
5861 && imm_expr.X_add_number >= -0x8000
5862 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
5863 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
5864 AT, reg, BFD_RELOC_LO16);
252b5132
RH
5865 else
5866 {
67c0d1eb
RS
5867 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
5868 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
5869 }
5870}
5871
5872/* Warn if an expression is not a constant. */
5873
5874static void
17a2f251 5875check_absolute_expr (struct mips_cl_insn *ip, expressionS *ex)
252b5132
RH
5876{
5877 if (ex->X_op == O_big)
5878 as_bad (_("unsupported large constant"));
5879 else if (ex->X_op != O_constant)
9ee2a2d4
MR
5880 as_bad (_("Instruction %s requires absolute expression"),
5881 ip->insn_mo->name);
13757d0c 5882
9ee2a2d4
MR
5883 if (HAVE_32BIT_GPRS)
5884 normalize_constant_expr (ex);
252b5132
RH
5885}
5886
5887/* Count the leading zeroes by performing a binary chop. This is a
5888 bulky bit of source, but performance is a LOT better for the
5889 majority of values than a simple loop to count the bits:
5890 for (lcnt = 0; (lcnt < 32); lcnt++)
5891 if ((v) & (1 << (31 - lcnt)))
5892 break;
5893 However it is not code size friendly, and the gain will drop a bit
5894 on certain cached systems.
5895*/
5896#define COUNT_TOP_ZEROES(v) \
5897 (((v) & ~0xffff) == 0 \
5898 ? ((v) & ~0xff) == 0 \
5899 ? ((v) & ~0xf) == 0 \
5900 ? ((v) & ~0x3) == 0 \
5901 ? ((v) & ~0x1) == 0 \
5902 ? !(v) \
5903 ? 32 \
5904 : 31 \
5905 : 30 \
5906 : ((v) & ~0x7) == 0 \
5907 ? 29 \
5908 : 28 \
5909 : ((v) & ~0x3f) == 0 \
5910 ? ((v) & ~0x1f) == 0 \
5911 ? 27 \
5912 : 26 \
5913 : ((v) & ~0x7f) == 0 \
5914 ? 25 \
5915 : 24 \
5916 : ((v) & ~0xfff) == 0 \
5917 ? ((v) & ~0x3ff) == 0 \
5918 ? ((v) & ~0x1ff) == 0 \
5919 ? 23 \
5920 : 22 \
5921 : ((v) & ~0x7ff) == 0 \
5922 ? 21 \
5923 : 20 \
5924 : ((v) & ~0x3fff) == 0 \
5925 ? ((v) & ~0x1fff) == 0 \
5926 ? 19 \
5927 : 18 \
5928 : ((v) & ~0x7fff) == 0 \
5929 ? 17 \
5930 : 16 \
5931 : ((v) & ~0xffffff) == 0 \
5932 ? ((v) & ~0xfffff) == 0 \
5933 ? ((v) & ~0x3ffff) == 0 \
5934 ? ((v) & ~0x1ffff) == 0 \
5935 ? 15 \
5936 : 14 \
5937 : ((v) & ~0x7ffff) == 0 \
5938 ? 13 \
5939 : 12 \
5940 : ((v) & ~0x3fffff) == 0 \
5941 ? ((v) & ~0x1fffff) == 0 \
5942 ? 11 \
5943 : 10 \
5944 : ((v) & ~0x7fffff) == 0 \
5945 ? 9 \
5946 : 8 \
5947 : ((v) & ~0xfffffff) == 0 \
5948 ? ((v) & ~0x3ffffff) == 0 \
5949 ? ((v) & ~0x1ffffff) == 0 \
5950 ? 7 \
5951 : 6 \
5952 : ((v) & ~0x7ffffff) == 0 \
5953 ? 5 \
5954 : 4 \
5955 : ((v) & ~0x3fffffff) == 0 \
5956 ? ((v) & ~0x1fffffff) == 0 \
5957 ? 3 \
5958 : 2 \
5959 : ((v) & ~0x7fffffff) == 0 \
5960 ? 1 \
5961 : 0)
5962
5963/* load_register()
67c1ffbe 5964 * This routine generates the least number of instructions necessary to load
252b5132
RH
5965 * an absolute expression value into a register.
5966 */
5967static void
67c0d1eb 5968load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
5969{
5970 int freg;
5971 expressionS hi32, lo32;
5972
5973 if (ep->X_op != O_big)
5974 {
9c2799c2 5975 gas_assert (ep->X_op == O_constant);
256ab948
TS
5976
5977 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
5978 if (!dbl)
5979 normalize_constant_expr (ep);
256ab948
TS
5980
5981 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
5982 {
5983 /* We can handle 16 bit signed values with an addiu to
5984 $zero. No need to ever use daddiu here, since $zero and
5985 the result are always correct in 32 bit mode. */
67c0d1eb 5986 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5987 return;
5988 }
5989 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
5990 {
5991 /* We can handle 16 bit unsigned values with an ori to
5992 $zero. */
67c0d1eb 5993 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
5994 return;
5995 }
256ab948 5996 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
5997 {
5998 /* 32 bit values require an lui. */
df58fc94 5999 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 6000 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 6001 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
6002 return;
6003 }
6004 }
6005
6006 /* The value is larger than 32 bits. */
6007
2051e8c4 6008 if (!dbl || HAVE_32BIT_GPRS)
252b5132 6009 {
55e08f71
NC
6010 char value[32];
6011
6012 sprintf_vma (value, ep->X_add_number);
20e1fcfd 6013 as_bad (_("Number (0x%s) larger than 32 bits"), value);
67c0d1eb 6014 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
6015 return;
6016 }
6017
6018 if (ep->X_op != O_big)
6019 {
6020 hi32 = *ep;
6021 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6022 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
6023 hi32.X_add_number &= 0xffffffff;
6024 lo32 = *ep;
6025 lo32.X_add_number &= 0xffffffff;
6026 }
6027 else
6028 {
9c2799c2 6029 gas_assert (ep->X_add_number > 2);
252b5132
RH
6030 if (ep->X_add_number == 3)
6031 generic_bignum[3] = 0;
6032 else if (ep->X_add_number > 4)
6033 as_bad (_("Number larger than 64 bits"));
6034 lo32.X_op = O_constant;
6035 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
6036 hi32.X_op = O_constant;
6037 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
6038 }
6039
6040 if (hi32.X_add_number == 0)
6041 freg = 0;
6042 else
6043 {
6044 int shift, bit;
6045 unsigned long hi, lo;
6046
956cd1d6 6047 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
6048 {
6049 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
6050 {
67c0d1eb 6051 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
6052 return;
6053 }
6054 if (lo32.X_add_number & 0x80000000)
6055 {
df58fc94 6056 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 6057 if (lo32.X_add_number & 0xffff)
67c0d1eb 6058 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
6059 return;
6060 }
6061 }
252b5132
RH
6062
6063 /* Check for 16bit shifted constant. We know that hi32 is
6064 non-zero, so start the mask on the first bit of the hi32
6065 value. */
6066 shift = 17;
6067 do
beae10d5
KH
6068 {
6069 unsigned long himask, lomask;
6070
6071 if (shift < 32)
6072 {
6073 himask = 0xffff >> (32 - shift);
6074 lomask = (0xffff << shift) & 0xffffffff;
6075 }
6076 else
6077 {
6078 himask = 0xffff << (shift - 32);
6079 lomask = 0;
6080 }
6081 if ((hi32.X_add_number & ~(offsetT) himask) == 0
6082 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
6083 {
6084 expressionS tmp;
6085
6086 tmp.X_op = O_constant;
6087 if (shift < 32)
6088 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
6089 | (lo32.X_add_number >> shift));
6090 else
6091 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 6092 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 6093 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 6094 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
6095 return;
6096 }
f9419b05 6097 ++shift;
beae10d5
KH
6098 }
6099 while (shift <= (64 - 16));
252b5132
RH
6100
6101 /* Find the bit number of the lowest one bit, and store the
6102 shifted value in hi/lo. */
6103 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
6104 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
6105 if (lo != 0)
6106 {
6107 bit = 0;
6108 while ((lo & 1) == 0)
6109 {
6110 lo >>= 1;
6111 ++bit;
6112 }
6113 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
6114 hi >>= bit;
6115 }
6116 else
6117 {
6118 bit = 32;
6119 while ((hi & 1) == 0)
6120 {
6121 hi >>= 1;
6122 ++bit;
6123 }
6124 lo = hi;
6125 hi = 0;
6126 }
6127
6128 /* Optimize if the shifted value is a (power of 2) - 1. */
6129 if ((hi == 0 && ((lo + 1) & lo) == 0)
6130 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
6131 {
6132 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 6133 if (shift != 0)
beae10d5 6134 {
252b5132
RH
6135 expressionS tmp;
6136
6137 /* This instruction will set the register to be all
6138 ones. */
beae10d5
KH
6139 tmp.X_op = O_constant;
6140 tmp.X_add_number = (offsetT) -1;
67c0d1eb 6141 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
6142 if (bit != 0)
6143 {
6144 bit += shift;
df58fc94 6145 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 6146 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 6147 }
df58fc94 6148 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 6149 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
6150 return;
6151 }
6152 }
252b5132
RH
6153
6154 /* Sign extend hi32 before calling load_register, because we can
6155 generally get better code when we load a sign extended value. */
6156 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 6157 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 6158 load_register (reg, &hi32, 0);
252b5132
RH
6159 freg = reg;
6160 }
6161 if ((lo32.X_add_number & 0xffff0000) == 0)
6162 {
6163 if (freg != 0)
6164 {
df58fc94 6165 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
6166 freg = reg;
6167 }
6168 }
6169 else
6170 {
6171 expressionS mid16;
6172
956cd1d6 6173 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 6174 {
df58fc94
RS
6175 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
6176 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
6177 return;
6178 }
252b5132
RH
6179
6180 if (freg != 0)
6181 {
df58fc94 6182 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
6183 freg = reg;
6184 }
6185 mid16 = lo32;
6186 mid16.X_add_number >>= 16;
67c0d1eb 6187 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 6188 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
6189 freg = reg;
6190 }
6191 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 6192 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
6193}
6194
269137b2
TS
6195static inline void
6196load_delay_nop (void)
6197{
6198 if (!gpr_interlocks)
6199 macro_build (NULL, "nop", "");
6200}
6201
252b5132
RH
6202/* Load an address into a register. */
6203
6204static void
67c0d1eb 6205load_address (int reg, expressionS *ep, int *used_at)
252b5132 6206{
252b5132
RH
6207 if (ep->X_op != O_constant
6208 && ep->X_op != O_symbol)
6209 {
6210 as_bad (_("expression too complex"));
6211 ep->X_op = O_constant;
6212 }
6213
6214 if (ep->X_op == O_constant)
6215 {
67c0d1eb 6216 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
6217 return;
6218 }
6219
6220 if (mips_pic == NO_PIC)
6221 {
6222 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 6223 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
6224 Otherwise we want
6225 lui $reg,<sym> (BFD_RELOC_HI16_S)
6226 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 6227 If we have an addend, we always use the latter form.
76b3015f 6228
d6bc6245
TS
6229 With 64bit address space and a usable $at we want
6230 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6231 lui $at,<sym> (BFD_RELOC_HI16_S)
6232 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6233 daddiu $at,<sym> (BFD_RELOC_LO16)
6234 dsll32 $reg,0
3a482fd5 6235 daddu $reg,$reg,$at
76b3015f 6236
c03099e6 6237 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
6238 on superscalar processors.
6239 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
6240 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
6241 dsll $reg,16
6242 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
6243 dsll $reg,16
6244 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
6245
6246 For GP relative symbols in 64bit address space we can use
6247 the same sequence as in 32bit address space. */
aed1a261 6248 if (HAVE_64BIT_SYMBOLS)
d6bc6245 6249 {
6caf9ef4
TS
6250 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6251 && !nopic_need_relax (ep->X_add_symbol, 1))
6252 {
6253 relax_start (ep->X_add_symbol);
6254 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
6255 mips_gp_register, BFD_RELOC_GPREL16);
6256 relax_switch ();
6257 }
d6bc6245 6258
741fe287 6259 if (*used_at == 0 && mips_opts.at)
d6bc6245 6260 {
df58fc94
RS
6261 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
6262 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
6263 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6264 BFD_RELOC_MIPS_HIGHER);
6265 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 6266 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 6267 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
6268 *used_at = 1;
6269 }
6270 else
6271 {
df58fc94 6272 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
6273 macro_build (ep, "daddiu", "t,r,j", reg, reg,
6274 BFD_RELOC_MIPS_HIGHER);
df58fc94 6275 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 6276 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 6277 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 6278 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 6279 }
6caf9ef4
TS
6280
6281 if (mips_relax.sequence)
6282 relax_end ();
d6bc6245 6283 }
252b5132
RH
6284 else
6285 {
d6bc6245 6286 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 6287 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 6288 {
4d7206a2 6289 relax_start (ep->X_add_symbol);
67c0d1eb 6290 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 6291 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 6292 relax_switch ();
d6bc6245 6293 }
67c0d1eb
RS
6294 macro_build_lui (ep, reg);
6295 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
6296 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
6297 if (mips_relax.sequence)
6298 relax_end ();
d6bc6245 6299 }
252b5132 6300 }
0a44bf69 6301 else if (!mips_big_got)
252b5132
RH
6302 {
6303 expressionS ex;
6304
6305 /* If this is a reference to an external symbol, we want
6306 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6307 Otherwise we want
6308 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6309 nop
6310 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
6311 If there is a constant, it must be added in after.
6312
ed6fb7bd 6313 If we have NewABI, we want
f5040a92
AO
6314 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
6315 unless we're referencing a global symbol with a non-zero
6316 offset, in which case cst must be added separately. */
ed6fb7bd
SC
6317 if (HAVE_NEWABI)
6318 {
f5040a92
AO
6319 if (ep->X_add_number)
6320 {
4d7206a2 6321 ex.X_add_number = ep->X_add_number;
f5040a92 6322 ep->X_add_number = 0;
4d7206a2 6323 relax_start (ep->X_add_symbol);
67c0d1eb
RS
6324 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6325 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
6326 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6327 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6328 ex.X_op = O_constant;
67c0d1eb 6329 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6330 reg, reg, BFD_RELOC_LO16);
f5040a92 6331 ep->X_add_number = ex.X_add_number;
4d7206a2 6332 relax_switch ();
f5040a92 6333 }
67c0d1eb 6334 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6335 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
6336 if (mips_relax.sequence)
6337 relax_end ();
ed6fb7bd
SC
6338 }
6339 else
6340 {
f5040a92
AO
6341 ex.X_add_number = ep->X_add_number;
6342 ep->X_add_number = 0;
67c0d1eb
RS
6343 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
6344 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6345 load_delay_nop ();
4d7206a2
RS
6346 relax_start (ep->X_add_symbol);
6347 relax_switch ();
67c0d1eb 6348 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6349 BFD_RELOC_LO16);
4d7206a2 6350 relax_end ();
ed6fb7bd 6351
f5040a92
AO
6352 if (ex.X_add_number != 0)
6353 {
6354 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6355 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6356 ex.X_op = O_constant;
67c0d1eb 6357 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 6358 reg, reg, BFD_RELOC_LO16);
f5040a92 6359 }
252b5132
RH
6360 }
6361 }
0a44bf69 6362 else if (mips_big_got)
252b5132
RH
6363 {
6364 expressionS ex;
252b5132
RH
6365
6366 /* This is the large GOT case. If this is a reference to an
6367 external symbol, we want
6368 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
6369 addu $reg,$reg,$gp
6370 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
6371
6372 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
6373 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
6374 nop
6375 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 6376 If there is a constant, it must be added in after.
f5040a92
AO
6377
6378 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
6379 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
6380 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 6381 */
438c16b8
TS
6382 if (HAVE_NEWABI)
6383 {
4d7206a2 6384 ex.X_add_number = ep->X_add_number;
f5040a92 6385 ep->X_add_number = 0;
4d7206a2 6386 relax_start (ep->X_add_symbol);
df58fc94 6387 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6388 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6389 reg, reg, mips_gp_register);
6390 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6391 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
6392 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6393 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6394 else if (ex.X_add_number)
6395 {
6396 ex.X_op = O_constant;
67c0d1eb
RS
6397 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6398 BFD_RELOC_LO16);
f5040a92
AO
6399 }
6400
6401 ep->X_add_number = ex.X_add_number;
4d7206a2 6402 relax_switch ();
67c0d1eb 6403 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6404 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
6405 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6406 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 6407 relax_end ();
438c16b8 6408 }
252b5132 6409 else
438c16b8 6410 {
f5040a92
AO
6411 ex.X_add_number = ep->X_add_number;
6412 ep->X_add_number = 0;
4d7206a2 6413 relax_start (ep->X_add_symbol);
df58fc94 6414 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
6415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
6416 reg, reg, mips_gp_register);
6417 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
6418 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
6419 relax_switch ();
6420 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
6421 {
6422 /* We need a nop before loading from $gp. This special
6423 check is required because the lui which starts the main
6424 instruction stream does not refer to $gp, and so will not
6425 insert the nop which may be required. */
67c0d1eb 6426 macro_build (NULL, "nop", "");
438c16b8 6427 }
67c0d1eb 6428 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 6429 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 6430 load_delay_nop ();
67c0d1eb 6431 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 6432 BFD_RELOC_LO16);
4d7206a2 6433 relax_end ();
438c16b8 6434
f5040a92
AO
6435 if (ex.X_add_number != 0)
6436 {
6437 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
6438 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
6439 ex.X_op = O_constant;
67c0d1eb
RS
6440 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
6441 BFD_RELOC_LO16);
f5040a92 6442 }
252b5132
RH
6443 }
6444 }
252b5132
RH
6445 else
6446 abort ();
8fc2e39e 6447
741fe287 6448 if (!mips_opts.at && *used_at == 1)
8fc2e39e 6449 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
6450}
6451
ea1fb5dc
RS
6452/* Move the contents of register SOURCE into register DEST. */
6453
6454static void
67c0d1eb 6455move_register (int dest, int source)
ea1fb5dc 6456{
df58fc94
RS
6457 /* Prefer to use a 16-bit microMIPS instruction unless the previous
6458 instruction specifically requires a 32-bit one. */
6459 if (mips_opts.micromips
833794fc 6460 && !mips_opts.insn32
df58fc94 6461 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 6462 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94
RS
6463 else
6464 macro_build (NULL, HAVE_32BIT_GPRS ? "addu" : "daddu", "d,v,t",
6465 dest, source, 0);
ea1fb5dc
RS
6466}
6467
4d7206a2 6468/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
6469 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
6470 The two alternatives are:
4d7206a2
RS
6471
6472 Global symbol Local sybmol
6473 ------------- ------------
6474 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
6475 ... ...
6476 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
6477
6478 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
6479 emits the second for a 16-bit offset or add_got_offset_hilo emits
6480 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
6481
6482static void
67c0d1eb 6483load_got_offset (int dest, expressionS *local)
4d7206a2
RS
6484{
6485 expressionS global;
6486
6487 global = *local;
6488 global.X_add_number = 0;
6489
6490 relax_start (local->X_add_symbol);
67c0d1eb
RS
6491 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6492 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 6493 relax_switch ();
67c0d1eb
RS
6494 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
6495 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
6496 relax_end ();
6497}
6498
6499static void
67c0d1eb 6500add_got_offset (int dest, expressionS *local)
4d7206a2
RS
6501{
6502 expressionS global;
6503
6504 global.X_op = O_constant;
6505 global.X_op_symbol = NULL;
6506 global.X_add_symbol = NULL;
6507 global.X_add_number = local->X_add_number;
6508
6509 relax_start (local->X_add_symbol);
67c0d1eb 6510 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
6511 dest, dest, BFD_RELOC_LO16);
6512 relax_switch ();
67c0d1eb 6513 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
6514 relax_end ();
6515}
6516
f6a22291
MR
6517static void
6518add_got_offset_hilo (int dest, expressionS *local, int tmp)
6519{
6520 expressionS global;
6521 int hold_mips_optimize;
6522
6523 global.X_op = O_constant;
6524 global.X_op_symbol = NULL;
6525 global.X_add_symbol = NULL;
6526 global.X_add_number = local->X_add_number;
6527
6528 relax_start (local->X_add_symbol);
6529 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
6530 relax_switch ();
6531 /* Set mips_optimize around the lui instruction to avoid
6532 inserting an unnecessary nop after the lw. */
6533 hold_mips_optimize = mips_optimize;
6534 mips_optimize = 2;
6535 macro_build_lui (&global, tmp);
6536 mips_optimize = hold_mips_optimize;
6537 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
6538 relax_end ();
6539
6540 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
6541}
6542
df58fc94
RS
6543/* Emit a sequence of instructions to emulate a branch likely operation.
6544 BR is an ordinary branch corresponding to one to be emulated. BRNEG
6545 is its complementing branch with the original condition negated.
6546 CALL is set if the original branch specified the link operation.
6547 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
6548
6549 Code like this is produced in the noreorder mode:
6550
6551 BRNEG <args>, 1f
6552 nop
6553 b <sym>
6554 delay slot (executed only if branch taken)
6555 1:
6556
6557 or, if CALL is set:
6558
6559 BRNEG <args>, 1f
6560 nop
6561 bal <sym>
6562 delay slot (executed only if branch taken)
6563 1:
6564
6565 In the reorder mode the delay slot would be filled with a nop anyway,
6566 so code produced is simply:
6567
6568 BR <args>, <sym>
6569 nop
6570
6571 This function is used when producing code for the microMIPS ASE that
6572 does not implement branch likely instructions in hardware. */
6573
6574static void
6575macro_build_branch_likely (const char *br, const char *brneg,
6576 int call, expressionS *ep, const char *fmt,
6577 unsigned int sreg, unsigned int treg)
6578{
6579 int noreorder = mips_opts.noreorder;
6580 expressionS expr1;
6581
6582 gas_assert (mips_opts.micromips);
6583 start_noreorder ();
6584 if (noreorder)
6585 {
6586 micromips_label_expr (&expr1);
6587 macro_build (&expr1, brneg, fmt, sreg, treg);
6588 macro_build (NULL, "nop", "");
6589 macro_build (ep, call ? "bal" : "b", "p");
6590
6591 /* Set to true so that append_insn adds a label. */
6592 emit_branch_likely_macro = TRUE;
6593 }
6594 else
6595 {
6596 macro_build (ep, br, fmt, sreg, treg);
6597 macro_build (NULL, "nop", "");
6598 }
6599 end_noreorder ();
6600}
6601
6602/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
6603 the condition code tested. EP specifies the branch target. */
6604
6605static void
6606macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
6607{
6608 const int call = 0;
6609 const char *brneg;
6610 const char *br;
6611
6612 switch (type)
6613 {
6614 case M_BC1FL:
6615 br = "bc1f";
6616 brneg = "bc1t";
6617 break;
6618 case M_BC1TL:
6619 br = "bc1t";
6620 brneg = "bc1f";
6621 break;
6622 case M_BC2FL:
6623 br = "bc2f";
6624 brneg = "bc2t";
6625 break;
6626 case M_BC2TL:
6627 br = "bc2t";
6628 brneg = "bc2f";
6629 break;
6630 default:
6631 abort ();
6632 }
6633 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
6634}
6635
6636/* Emit a two-argument branch macro specified by TYPE, using SREG as
6637 the register tested. EP specifies the branch target. */
6638
6639static void
6640macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
6641{
6642 const char *brneg = NULL;
6643 const char *br;
6644 int call = 0;
6645
6646 switch (type)
6647 {
6648 case M_BGEZ:
6649 br = "bgez";
6650 break;
6651 case M_BGEZL:
6652 br = mips_opts.micromips ? "bgez" : "bgezl";
6653 brneg = "bltz";
6654 break;
6655 case M_BGEZALL:
6656 gas_assert (mips_opts.micromips);
833794fc 6657 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
6658 brneg = "bltz";
6659 call = 1;
6660 break;
6661 case M_BGTZ:
6662 br = "bgtz";
6663 break;
6664 case M_BGTZL:
6665 br = mips_opts.micromips ? "bgtz" : "bgtzl";
6666 brneg = "blez";
6667 break;
6668 case M_BLEZ:
6669 br = "blez";
6670 break;
6671 case M_BLEZL:
6672 br = mips_opts.micromips ? "blez" : "blezl";
6673 brneg = "bgtz";
6674 break;
6675 case M_BLTZ:
6676 br = "bltz";
6677 break;
6678 case M_BLTZL:
6679 br = mips_opts.micromips ? "bltz" : "bltzl";
6680 brneg = "bgez";
6681 break;
6682 case M_BLTZALL:
6683 gas_assert (mips_opts.micromips);
833794fc 6684 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
6685 brneg = "bgez";
6686 call = 1;
6687 break;
6688 default:
6689 abort ();
6690 }
6691 if (mips_opts.micromips && brneg)
6692 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
6693 else
6694 macro_build (ep, br, "s,p", sreg);
6695}
6696
6697/* Emit a three-argument branch macro specified by TYPE, using SREG and
6698 TREG as the registers tested. EP specifies the branch target. */
6699
6700static void
6701macro_build_branch_rsrt (int type, expressionS *ep,
6702 unsigned int sreg, unsigned int treg)
6703{
6704 const char *brneg = NULL;
6705 const int call = 0;
6706 const char *br;
6707
6708 switch (type)
6709 {
6710 case M_BEQ:
6711 case M_BEQ_I:
6712 br = "beq";
6713 break;
6714 case M_BEQL:
6715 case M_BEQL_I:
6716 br = mips_opts.micromips ? "beq" : "beql";
6717 brneg = "bne";
6718 break;
6719 case M_BNE:
6720 case M_BNE_I:
6721 br = "bne";
6722 break;
6723 case M_BNEL:
6724 case M_BNEL_I:
6725 br = mips_opts.micromips ? "bne" : "bnel";
6726 brneg = "beq";
6727 break;
6728 default:
6729 abort ();
6730 }
6731 if (mips_opts.micromips && brneg)
6732 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
6733 else
6734 macro_build (ep, br, "s,t,p", sreg, treg);
6735}
6736
252b5132
RH
6737/*
6738 * Build macros
6739 * This routine implements the seemingly endless macro or synthesized
6740 * instructions and addressing modes in the mips assembly language. Many
6741 * of these macros are simple and are similar to each other. These could
67c1ffbe 6742 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
6743 * this verbose method. Others are not simple macros but are more like
6744 * optimizing code generation.
6745 * One interesting optimization is when several store macros appear
67c1ffbe 6746 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
6747 * The ensuing load upper instructions are ommited. This implies some kind
6748 * of global optimization. We currently only optimize within a single macro.
6749 * For many of the load and store macros if the address is specified as a
6750 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
6751 * first load register 'at' with zero and use it as the base register. The
6752 * mips assembler simply uses register $zero. Just one tiny optimization
6753 * we're missing.
6754 */
6755static void
833794fc 6756macro (struct mips_cl_insn *ip, char *str)
252b5132 6757{
741fe287
MR
6758 unsigned int treg, sreg, dreg, breg;
6759 unsigned int tempreg;
252b5132 6760 int mask;
43841e91 6761 int used_at = 0;
df58fc94 6762 expressionS label_expr;
252b5132 6763 expressionS expr1;
df58fc94 6764 expressionS *ep;
252b5132
RH
6765 const char *s;
6766 const char *s2;
6767 const char *fmt;
6768 int likely = 0;
252b5132 6769 int coproc = 0;
7f3c4072 6770 int offbits = 16;
1abe91b1 6771 int call = 0;
df58fc94
RS
6772 int jals = 0;
6773 int dbl = 0;
6774 int imm = 0;
6775 int ust = 0;
6776 int lp = 0;
6777 int ab = 0;
252b5132
RH
6778 int off;
6779 bfd_reloc_code_real_type r;
252b5132
RH
6780 int hold_mips_optimize;
6781
9c2799c2 6782 gas_assert (! mips_opts.mips16);
252b5132 6783
df58fc94
RS
6784 treg = EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
6785 dreg = EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
6786 sreg = breg = EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
252b5132
RH
6787 mask = ip->insn_mo->mask;
6788
df58fc94
RS
6789 label_expr.X_op = O_constant;
6790 label_expr.X_op_symbol = NULL;
6791 label_expr.X_add_symbol = NULL;
6792 label_expr.X_add_number = 0;
6793
252b5132
RH
6794 expr1.X_op = O_constant;
6795 expr1.X_op_symbol = NULL;
6796 expr1.X_add_symbol = NULL;
6797 expr1.X_add_number = 1;
6798
6799 switch (mask)
6800 {
6801 case M_DABS:
6802 dbl = 1;
6803 case M_ABS:
df58fc94
RS
6804 /* bgez $a0,1f
6805 move v0,$a0
6806 sub v0,$zero,$a0
6807 1:
6808 */
252b5132 6809
7d10b47d 6810 start_noreorder ();
252b5132 6811
df58fc94
RS
6812 if (mips_opts.micromips)
6813 micromips_label_expr (&label_expr);
6814 else
6815 label_expr.X_add_number = 8;
6816 macro_build (&label_expr, "bgez", "s,p", sreg);
252b5132 6817 if (dreg == sreg)
a605d2b3 6818 macro_build (NULL, "nop", "");
252b5132 6819 else
67c0d1eb
RS
6820 move_register (dreg, sreg);
6821 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", dreg, 0, sreg);
df58fc94
RS
6822 if (mips_opts.micromips)
6823 micromips_add_label ();
252b5132 6824
7d10b47d 6825 end_noreorder ();
8fc2e39e 6826 break;
252b5132
RH
6827
6828 case M_ADD_I:
6829 s = "addi";
6830 s2 = "add";
6831 goto do_addi;
6832 case M_ADDU_I:
6833 s = "addiu";
6834 s2 = "addu";
6835 goto do_addi;
6836 case M_DADD_I:
6837 dbl = 1;
6838 s = "daddi";
6839 s2 = "dadd";
df58fc94
RS
6840 if (!mips_opts.micromips)
6841 goto do_addi;
6842 if (imm_expr.X_op == O_constant
6843 && imm_expr.X_add_number >= -0x200
6844 && imm_expr.X_add_number < 0x200)
6845 {
6846 macro_build (NULL, s, "t,r,.", treg, sreg, imm_expr.X_add_number);
6847 break;
6848 }
6849 goto do_addi_i;
252b5132
RH
6850 case M_DADDU_I:
6851 dbl = 1;
6852 s = "daddiu";
6853 s2 = "daddu";
6854 do_addi:
6855 if (imm_expr.X_op == O_constant
6856 && imm_expr.X_add_number >= -0x8000
6857 && imm_expr.X_add_number < 0x8000)
6858 {
67c0d1eb 6859 macro_build (&imm_expr, s, "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 6860 break;
252b5132 6861 }
df58fc94 6862 do_addi_i:
8fc2e39e 6863 used_at = 1;
67c0d1eb
RS
6864 load_register (AT, &imm_expr, dbl);
6865 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6866 break;
6867
6868 case M_AND_I:
6869 s = "andi";
6870 s2 = "and";
6871 goto do_bit;
6872 case M_OR_I:
6873 s = "ori";
6874 s2 = "or";
6875 goto do_bit;
6876 case M_NOR_I:
6877 s = "";
6878 s2 = "nor";
6879 goto do_bit;
6880 case M_XOR_I:
6881 s = "xori";
6882 s2 = "xor";
6883 do_bit:
6884 if (imm_expr.X_op == O_constant
6885 && imm_expr.X_add_number >= 0
6886 && imm_expr.X_add_number < 0x10000)
6887 {
6888 if (mask != M_NOR_I)
67c0d1eb 6889 macro_build (&imm_expr, s, "t,r,i", treg, sreg, BFD_RELOC_LO16);
252b5132
RH
6890 else
6891 {
67c0d1eb
RS
6892 macro_build (&imm_expr, "ori", "t,r,i",
6893 treg, sreg, BFD_RELOC_LO16);
6894 macro_build (NULL, "nor", "d,v,t", treg, treg, 0);
252b5132 6895 }
8fc2e39e 6896 break;
252b5132
RH
6897 }
6898
8fc2e39e 6899 used_at = 1;
67c0d1eb
RS
6900 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
6901 macro_build (NULL, s2, "d,v,t", treg, sreg, AT);
252b5132
RH
6902 break;
6903
8b082fb1
TS
6904 case M_BALIGN:
6905 switch (imm_expr.X_add_number)
6906 {
6907 case 0:
6908 macro_build (NULL, "nop", "");
6909 break;
6910 case 2:
6911 macro_build (NULL, "packrl.ph", "d,s,t", treg, treg, sreg);
6912 break;
03f66e8a
MR
6913 case 1:
6914 case 3:
8b082fb1 6915 macro_build (NULL, "balign", "t,s,2", treg, sreg,
90ecf173 6916 (int) imm_expr.X_add_number);
8b082fb1 6917 break;
03f66e8a
MR
6918 default:
6919 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
6920 (unsigned long) imm_expr.X_add_number);
6921 break;
8b082fb1
TS
6922 }
6923 break;
6924
df58fc94
RS
6925 case M_BC1FL:
6926 case M_BC1TL:
6927 case M_BC2FL:
6928 case M_BC2TL:
6929 gas_assert (mips_opts.micromips);
6930 macro_build_branch_ccl (mask, &offset_expr,
6931 EXTRACT_OPERAND (1, BCC, *ip));
6932 break;
6933
252b5132 6934 case M_BEQ_I:
252b5132 6935 case M_BEQL_I:
252b5132 6936 case M_BNE_I:
252b5132 6937 case M_BNEL_I:
252b5132 6938 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
6939 treg = 0;
6940 else
252b5132 6941 {
df58fc94
RS
6942 treg = AT;
6943 used_at = 1;
6944 load_register (treg, &imm_expr, HAVE_64BIT_GPRS);
252b5132 6945 }
df58fc94
RS
6946 /* Fall through. */
6947 case M_BEQL:
6948 case M_BNEL:
6949 macro_build_branch_rsrt (mask, &offset_expr, sreg, treg);
252b5132
RH
6950 break;
6951
6952 case M_BGEL:
6953 likely = 1;
6954 case M_BGE:
6955 if (treg == 0)
df58fc94
RS
6956 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, sreg);
6957 else if (sreg == 0)
6958 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, treg);
6959 else
252b5132 6960 {
df58fc94
RS
6961 used_at = 1;
6962 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
6963 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
6964 &offset_expr, AT, ZERO);
252b5132 6965 }
df58fc94
RS
6966 break;
6967
6968 case M_BGEZL:
6969 case M_BGEZALL:
6970 case M_BGTZL:
6971 case M_BLEZL:
6972 case M_BLTZL:
6973 case M_BLTZALL:
6974 macro_build_branch_rs (mask, &offset_expr, sreg);
252b5132
RH
6975 break;
6976
6977 case M_BGTL_I:
6978 likely = 1;
6979 case M_BGT_I:
90ecf173 6980 /* Check for > max integer. */
42429eac 6981 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
6982 {
6983 do_false:
90ecf173 6984 /* Result is always false. */
252b5132 6985 if (! likely)
a605d2b3 6986 macro_build (NULL, "nop", "");
252b5132 6987 else
df58fc94 6988 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 6989 break;
252b5132
RH
6990 }
6991 if (imm_expr.X_op != O_constant)
6992 as_bad (_("Unsupported large constant"));
f9419b05 6993 ++imm_expr.X_add_number;
252b5132
RH
6994 /* FALLTHROUGH */
6995 case M_BGE_I:
6996 case M_BGEL_I:
6997 if (mask == M_BGEL_I)
6998 likely = 1;
6999 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7000 {
df58fc94
RS
7001 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
7002 &offset_expr, sreg);
8fc2e39e 7003 break;
252b5132
RH
7004 }
7005 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7006 {
df58fc94
RS
7007 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
7008 &offset_expr, sreg);
8fc2e39e 7009 break;
252b5132 7010 }
42429eac 7011 if (imm_expr.X_op == O_constant && imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
7012 {
7013 do_true:
7014 /* result is always true */
7015 as_warn (_("Branch %s is always true"), ip->insn_mo->name);
67c0d1eb 7016 macro_build (&offset_expr, "b", "p");
8fc2e39e 7017 break;
252b5132 7018 }
8fc2e39e 7019 used_at = 1;
67c0d1eb 7020 set_at (sreg, 0);
df58fc94
RS
7021 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7022 &offset_expr, AT, ZERO);
252b5132
RH
7023 break;
7024
7025 case M_BGEUL:
7026 likely = 1;
7027 case M_BGEU:
7028 if (treg == 0)
7029 goto do_true;
df58fc94
RS
7030 else if (sreg == 0)
7031 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7032 &offset_expr, ZERO, treg);
7033 else
252b5132 7034 {
df58fc94
RS
7035 used_at = 1;
7036 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7037 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7038 &offset_expr, AT, ZERO);
252b5132 7039 }
252b5132
RH
7040 break;
7041
7042 case M_BGTUL_I:
7043 likely = 1;
7044 case M_BGTU_I:
7045 if (sreg == 0
ca4e0257 7046 || (HAVE_32BIT_GPRS
252b5132 7047 && imm_expr.X_op == O_constant
f01dc953 7048 && imm_expr.X_add_number == -1))
252b5132
RH
7049 goto do_false;
7050 if (imm_expr.X_op != O_constant)
7051 as_bad (_("Unsupported large constant"));
f9419b05 7052 ++imm_expr.X_add_number;
252b5132
RH
7053 /* FALLTHROUGH */
7054 case M_BGEU_I:
7055 case M_BGEUL_I:
7056 if (mask == M_BGEUL_I)
7057 likely = 1;
7058 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7059 goto do_true;
df58fc94
RS
7060 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7061 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7062 &offset_expr, sreg, ZERO);
7063 else
252b5132 7064 {
df58fc94
RS
7065 used_at = 1;
7066 set_at (sreg, 1);
7067 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7068 &offset_expr, AT, ZERO);
252b5132 7069 }
252b5132
RH
7070 break;
7071
7072 case M_BGTL:
7073 likely = 1;
7074 case M_BGT:
7075 if (treg == 0)
df58fc94
RS
7076 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, sreg);
7077 else if (sreg == 0)
7078 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, treg);
7079 else
252b5132 7080 {
df58fc94
RS
7081 used_at = 1;
7082 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7083 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7084 &offset_expr, AT, ZERO);
252b5132 7085 }
252b5132
RH
7086 break;
7087
7088 case M_BGTUL:
7089 likely = 1;
7090 case M_BGTU:
7091 if (treg == 0)
df58fc94
RS
7092 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7093 &offset_expr, sreg, ZERO);
7094 else if (sreg == 0)
7095 goto do_false;
7096 else
252b5132 7097 {
df58fc94
RS
7098 used_at = 1;
7099 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7100 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7101 &offset_expr, AT, ZERO);
252b5132 7102 }
252b5132
RH
7103 break;
7104
7105 case M_BLEL:
7106 likely = 1;
7107 case M_BLE:
7108 if (treg == 0)
df58fc94
RS
7109 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7110 else if (sreg == 0)
7111 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, treg);
7112 else
252b5132 7113 {
df58fc94
RS
7114 used_at = 1;
7115 macro_build (NULL, "slt", "d,v,t", AT, treg, sreg);
7116 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7117 &offset_expr, AT, ZERO);
252b5132 7118 }
252b5132
RH
7119 break;
7120
7121 case M_BLEL_I:
7122 likely = 1;
7123 case M_BLE_I:
42429eac 7124 if (imm_expr.X_op == O_constant && imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
7125 goto do_true;
7126 if (imm_expr.X_op != O_constant)
7127 as_bad (_("Unsupported large constant"));
f9419b05 7128 ++imm_expr.X_add_number;
252b5132
RH
7129 /* FALLTHROUGH */
7130 case M_BLT_I:
7131 case M_BLTL_I:
7132 if (mask == M_BLTL_I)
7133 likely = 1;
7134 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
df58fc94
RS
7135 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7136 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7137 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, sreg);
7138 else
252b5132 7139 {
df58fc94
RS
7140 used_at = 1;
7141 set_at (sreg, 0);
7142 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7143 &offset_expr, AT, ZERO);
252b5132 7144 }
252b5132
RH
7145 break;
7146
7147 case M_BLEUL:
7148 likely = 1;
7149 case M_BLEU:
7150 if (treg == 0)
df58fc94
RS
7151 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7152 &offset_expr, sreg, ZERO);
7153 else if (sreg == 0)
7154 goto do_true;
7155 else
252b5132 7156 {
df58fc94
RS
7157 used_at = 1;
7158 macro_build (NULL, "sltu", "d,v,t", AT, treg, sreg);
7159 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7160 &offset_expr, AT, ZERO);
252b5132 7161 }
252b5132
RH
7162 break;
7163
7164 case M_BLEUL_I:
7165 likely = 1;
7166 case M_BLEU_I:
7167 if (sreg == 0
ca4e0257 7168 || (HAVE_32BIT_GPRS
252b5132 7169 && imm_expr.X_op == O_constant
f01dc953 7170 && imm_expr.X_add_number == -1))
252b5132
RH
7171 goto do_true;
7172 if (imm_expr.X_op != O_constant)
7173 as_bad (_("Unsupported large constant"));
f9419b05 7174 ++imm_expr.X_add_number;
252b5132
RH
7175 /* FALLTHROUGH */
7176 case M_BLTU_I:
7177 case M_BLTUL_I:
7178 if (mask == M_BLTUL_I)
7179 likely = 1;
7180 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7181 goto do_false;
df58fc94
RS
7182 else if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7183 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
7184 &offset_expr, sreg, ZERO);
7185 else
252b5132 7186 {
df58fc94
RS
7187 used_at = 1;
7188 set_at (sreg, 1);
7189 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7190 &offset_expr, AT, ZERO);
252b5132 7191 }
252b5132
RH
7192 break;
7193
7194 case M_BLTL:
7195 likely = 1;
7196 case M_BLT:
7197 if (treg == 0)
df58fc94
RS
7198 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, sreg);
7199 else if (sreg == 0)
7200 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, treg);
7201 else
252b5132 7202 {
df58fc94
RS
7203 used_at = 1;
7204 macro_build (NULL, "slt", "d,v,t", AT, sreg, treg);
7205 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7206 &offset_expr, AT, ZERO);
252b5132 7207 }
252b5132
RH
7208 break;
7209
7210 case M_BLTUL:
7211 likely = 1;
7212 case M_BLTU:
7213 if (treg == 0)
7214 goto do_false;
df58fc94
RS
7215 else if (sreg == 0)
7216 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7217 &offset_expr, ZERO, treg);
7218 else
252b5132 7219 {
df58fc94
RS
7220 used_at = 1;
7221 macro_build (NULL, "sltu", "d,v,t", AT, sreg, treg);
7222 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
7223 &offset_expr, AT, ZERO);
252b5132 7224 }
252b5132
RH
7225 break;
7226
5f74bc13
CD
7227 case M_DEXT:
7228 {
d5818fca
MR
7229 /* Use unsigned arithmetic. */
7230 addressT pos;
7231 addressT size;
5f74bc13 7232
90ecf173 7233 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
7234 {
7235 as_bad (_("Unsupported large constant"));
7236 pos = size = 1;
7237 }
7238 else
7239 {
d5818fca
MR
7240 pos = imm_expr.X_add_number;
7241 size = imm2_expr.X_add_number;
5f74bc13
CD
7242 }
7243
7244 if (pos > 63)
7245 {
d5818fca 7246 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7247 pos = 1;
7248 }
90ecf173 7249 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7250 {
7251 as_bad (_("Improper extract size (%lu, position %lu)"),
d5818fca 7252 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7253 size = 1;
7254 }
7255
7256 if (size <= 32 && pos < 32)
7257 {
7258 s = "dext";
7259 fmt = "t,r,+A,+C";
7260 }
7261 else if (size <= 32)
7262 {
7263 s = "dextu";
7264 fmt = "t,r,+E,+H";
7265 }
7266 else
7267 {
7268 s = "dextm";
7269 fmt = "t,r,+A,+G";
7270 }
d5818fca
MR
7271 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7272 (int) (size - 1));
5f74bc13 7273 }
8fc2e39e 7274 break;
5f74bc13
CD
7275
7276 case M_DINS:
7277 {
d5818fca
MR
7278 /* Use unsigned arithmetic. */
7279 addressT pos;
7280 addressT size;
5f74bc13 7281
90ecf173 7282 if (imm_expr.X_op != O_constant || imm2_expr.X_op != O_constant)
5f74bc13
CD
7283 {
7284 as_bad (_("Unsupported large constant"));
7285 pos = size = 1;
7286 }
7287 else
7288 {
d5818fca
MR
7289 pos = imm_expr.X_add_number;
7290 size = imm2_expr.X_add_number;
5f74bc13
CD
7291 }
7292
7293 if (pos > 63)
7294 {
d5818fca 7295 as_bad (_("Improper position (%lu)"), (unsigned long) pos);
5f74bc13
CD
7296 pos = 1;
7297 }
90ecf173 7298 if (size == 0 || size > 64 || (pos + size - 1) > 63)
5f74bc13
CD
7299 {
7300 as_bad (_("Improper insert size (%lu, position %lu)"),
d5818fca 7301 (unsigned long) size, (unsigned long) pos);
5f74bc13
CD
7302 size = 1;
7303 }
7304
7305 if (pos < 32 && (pos + size - 1) < 32)
7306 {
7307 s = "dins";
7308 fmt = "t,r,+A,+B";
7309 }
7310 else if (pos >= 32)
7311 {
7312 s = "dinsu";
7313 fmt = "t,r,+E,+F";
7314 }
7315 else
7316 {
7317 s = "dinsm";
7318 fmt = "t,r,+A,+F";
7319 }
750bdd57
AS
7320 macro_build ((expressionS *) NULL, s, fmt, treg, sreg, (int) pos,
7321 (int) (pos + size - 1));
5f74bc13 7322 }
8fc2e39e 7323 break;
5f74bc13 7324
252b5132
RH
7325 case M_DDIV_3:
7326 dbl = 1;
7327 case M_DIV_3:
7328 s = "mflo";
7329 goto do_div3;
7330 case M_DREM_3:
7331 dbl = 1;
7332 case M_REM_3:
7333 s = "mfhi";
7334 do_div3:
7335 if (treg == 0)
7336 {
7337 as_warn (_("Divide by zero."));
7338 if (mips_trap)
df58fc94 7339 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7340 else
df58fc94 7341 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7342 break;
252b5132
RH
7343 }
7344
7d10b47d 7345 start_noreorder ();
252b5132
RH
7346 if (mips_trap)
7347 {
df58fc94 7348 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7349 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
252b5132
RH
7350 }
7351 else
7352 {
df58fc94
RS
7353 if (mips_opts.micromips)
7354 micromips_label_expr (&label_expr);
7355 else
7356 label_expr.X_add_number = 8;
7357 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7358 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", sreg, treg);
df58fc94
RS
7359 macro_build (NULL, "break", BRK_FMT, 7);
7360 if (mips_opts.micromips)
7361 micromips_add_label ();
252b5132
RH
7362 }
7363 expr1.X_add_number = -1;
8fc2e39e 7364 used_at = 1;
f6a22291 7365 load_register (AT, &expr1, dbl);
df58fc94
RS
7366 if (mips_opts.micromips)
7367 micromips_label_expr (&label_expr);
7368 else
7369 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
7370 macro_build (&label_expr, "bne", "s,t,p", treg, AT);
252b5132
RH
7371 if (dbl)
7372 {
7373 expr1.X_add_number = 1;
f6a22291 7374 load_register (AT, &expr1, dbl);
df58fc94 7375 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
7376 }
7377 else
7378 {
7379 expr1.X_add_number = 0x80000000;
df58fc94 7380 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
7381 }
7382 if (mips_trap)
7383 {
df58fc94 7384 macro_build (NULL, "teq", TRAP_FMT, sreg, AT, 6);
252b5132
RH
7385 /* We want to close the noreorder block as soon as possible, so
7386 that later insns are available for delay slot filling. */
7d10b47d 7387 end_noreorder ();
252b5132
RH
7388 }
7389 else
7390 {
df58fc94
RS
7391 if (mips_opts.micromips)
7392 micromips_label_expr (&label_expr);
7393 else
7394 label_expr.X_add_number = 8;
7395 macro_build (&label_expr, "bne", "s,t,p", sreg, AT);
a605d2b3 7396 macro_build (NULL, "nop", "");
252b5132
RH
7397
7398 /* We want to close the noreorder block as soon as possible, so
7399 that later insns are available for delay slot filling. */
7d10b47d 7400 end_noreorder ();
252b5132 7401
df58fc94 7402 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 7403 }
df58fc94
RS
7404 if (mips_opts.micromips)
7405 micromips_add_label ();
7406 macro_build (NULL, s, MFHL_FMT, dreg);
252b5132
RH
7407 break;
7408
7409 case M_DIV_3I:
7410 s = "div";
7411 s2 = "mflo";
7412 goto do_divi;
7413 case M_DIVU_3I:
7414 s = "divu";
7415 s2 = "mflo";
7416 goto do_divi;
7417 case M_REM_3I:
7418 s = "div";
7419 s2 = "mfhi";
7420 goto do_divi;
7421 case M_REMU_3I:
7422 s = "divu";
7423 s2 = "mfhi";
7424 goto do_divi;
7425 case M_DDIV_3I:
7426 dbl = 1;
7427 s = "ddiv";
7428 s2 = "mflo";
7429 goto do_divi;
7430 case M_DDIVU_3I:
7431 dbl = 1;
7432 s = "ddivu";
7433 s2 = "mflo";
7434 goto do_divi;
7435 case M_DREM_3I:
7436 dbl = 1;
7437 s = "ddiv";
7438 s2 = "mfhi";
7439 goto do_divi;
7440 case M_DREMU_3I:
7441 dbl = 1;
7442 s = "ddivu";
7443 s2 = "mfhi";
7444 do_divi:
7445 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
7446 {
7447 as_warn (_("Divide by zero."));
7448 if (mips_trap)
df58fc94 7449 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 7450 else
df58fc94 7451 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 7452 break;
252b5132
RH
7453 }
7454 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 1)
7455 {
7456 if (strcmp (s2, "mflo") == 0)
67c0d1eb 7457 move_register (dreg, sreg);
252b5132 7458 else
c80c840e 7459 move_register (dreg, ZERO);
8fc2e39e 7460 break;
252b5132
RH
7461 }
7462 if (imm_expr.X_op == O_constant
7463 && imm_expr.X_add_number == -1
7464 && s[strlen (s) - 1] != 'u')
7465 {
7466 if (strcmp (s2, "mflo") == 0)
7467 {
67c0d1eb 7468 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", dreg, sreg);
252b5132
RH
7469 }
7470 else
c80c840e 7471 move_register (dreg, ZERO);
8fc2e39e 7472 break;
252b5132
RH
7473 }
7474
8fc2e39e 7475 used_at = 1;
67c0d1eb
RS
7476 load_register (AT, &imm_expr, dbl);
7477 macro_build (NULL, s, "z,s,t", sreg, AT);
df58fc94 7478 macro_build (NULL, s2, MFHL_FMT, dreg);
252b5132
RH
7479 break;
7480
7481 case M_DIVU_3:
7482 s = "divu";
7483 s2 = "mflo";
7484 goto do_divu3;
7485 case M_REMU_3:
7486 s = "divu";
7487 s2 = "mfhi";
7488 goto do_divu3;
7489 case M_DDIVU_3:
7490 s = "ddivu";
7491 s2 = "mflo";
7492 goto do_divu3;
7493 case M_DREMU_3:
7494 s = "ddivu";
7495 s2 = "mfhi";
7496 do_divu3:
7d10b47d 7497 start_noreorder ();
252b5132
RH
7498 if (mips_trap)
7499 {
df58fc94 7500 macro_build (NULL, "teq", TRAP_FMT, treg, ZERO, 7);
67c0d1eb 7501 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7502 /* We want to close the noreorder block as soon as possible, so
7503 that later insns are available for delay slot filling. */
7d10b47d 7504 end_noreorder ();
252b5132
RH
7505 }
7506 else
7507 {
df58fc94
RS
7508 if (mips_opts.micromips)
7509 micromips_label_expr (&label_expr);
7510 else
7511 label_expr.X_add_number = 8;
7512 macro_build (&label_expr, "bne", "s,t,p", treg, ZERO);
67c0d1eb 7513 macro_build (NULL, s, "z,s,t", sreg, treg);
252b5132
RH
7514
7515 /* We want to close the noreorder block as soon as possible, so
7516 that later insns are available for delay slot filling. */
7d10b47d 7517 end_noreorder ();
df58fc94
RS
7518 macro_build (NULL, "break", BRK_FMT, 7);
7519 if (mips_opts.micromips)
7520 micromips_add_label ();
252b5132 7521 }
df58fc94 7522 macro_build (NULL, s2, MFHL_FMT, dreg);
8fc2e39e 7523 break;
252b5132 7524
1abe91b1
MR
7525 case M_DLCA_AB:
7526 dbl = 1;
7527 case M_LCA_AB:
7528 call = 1;
7529 goto do_la;
252b5132
RH
7530 case M_DLA_AB:
7531 dbl = 1;
7532 case M_LA_AB:
1abe91b1 7533 do_la:
252b5132
RH
7534 /* Load the address of a symbol into a register. If breg is not
7535 zero, we then add a base register to it. */
7536
3bec30a8
TS
7537 if (dbl && HAVE_32BIT_GPRS)
7538 as_warn (_("dla used to load 32-bit register"));
7539
90ecf173 7540 if (!dbl && HAVE_64BIT_OBJECTS)
3bec30a8
TS
7541 as_warn (_("la used to load 64-bit address"));
7542
0c11417f
MR
7543 if (offset_expr.X_op == O_constant
7544 && offset_expr.X_add_number >= -0x8000
7545 && offset_expr.X_add_number < 0x8000)
7546 {
aed1a261 7547 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
17a2f251 7548 "t,r,j", treg, sreg, BFD_RELOC_LO16);
8fc2e39e 7549 break;
0c11417f
MR
7550 }
7551
741fe287 7552 if (mips_opts.at && (treg == breg))
afdbd6d0
CD
7553 {
7554 tempreg = AT;
7555 used_at = 1;
7556 }
7557 else
7558 {
7559 tempreg = treg;
afdbd6d0
CD
7560 }
7561
252b5132
RH
7562 if (offset_expr.X_op != O_symbol
7563 && offset_expr.X_op != O_constant)
7564 {
f71d0d44 7565 as_bad (_("Expression too complex"));
252b5132
RH
7566 offset_expr.X_op = O_constant;
7567 }
7568
252b5132 7569 if (offset_expr.X_op == O_constant)
aed1a261 7570 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
7571 else if (mips_pic == NO_PIC)
7572 {
d6bc6245 7573 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 7574 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
7575 Otherwise we want
7576 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
7577 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7578 If we have a constant, we need two instructions anyhow,
d6bc6245 7579 so we may as well always use the latter form.
76b3015f 7580
6caf9ef4
TS
7581 With 64bit address space and a usable $at we want
7582 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7583 lui $at,<sym> (BFD_RELOC_HI16_S)
7584 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7585 daddiu $at,<sym> (BFD_RELOC_LO16)
7586 dsll32 $tempreg,0
7587 daddu $tempreg,$tempreg,$at
7588
7589 If $at is already in use, we use a path which is suboptimal
7590 on superscalar processors.
7591 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
7592 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
7593 dsll $tempreg,16
7594 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
7595 dsll $tempreg,16
7596 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
7597
7598 For GP relative symbols in 64bit address space we can use
7599 the same sequence as in 32bit address space. */
aed1a261 7600 if (HAVE_64BIT_SYMBOLS)
252b5132 7601 {
6caf9ef4
TS
7602 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
7603 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
7604 {
7605 relax_start (offset_expr.X_add_symbol);
7606 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7607 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
7608 relax_switch ();
7609 }
d6bc6245 7610
741fe287 7611 if (used_at == 0 && mips_opts.at)
98d3f06f 7612 {
df58fc94 7613 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7614 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 7615 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7616 AT, BFD_RELOC_HI16_S);
67c0d1eb 7617 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7618 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 7619 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7620 AT, AT, BFD_RELOC_LO16);
df58fc94 7621 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 7622 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
7623 used_at = 1;
7624 }
7625 else
7626 {
df58fc94 7627 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 7628 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 7629 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7630 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 7631 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7632 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7633 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 7634 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 7635 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 7636 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 7637 }
6caf9ef4
TS
7638
7639 if (mips_relax.sequence)
7640 relax_end ();
98d3f06f
KH
7641 }
7642 else
7643 {
7644 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 7645 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 7646 {
4d7206a2 7647 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7648 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7649 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 7650 relax_switch ();
98d3f06f 7651 }
6943caf0 7652 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
f71d0d44 7653 as_bad (_("Offset too large"));
67c0d1eb
RS
7654 macro_build_lui (&offset_expr, tempreg);
7655 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7656 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
7657 if (mips_relax.sequence)
7658 relax_end ();
98d3f06f 7659 }
252b5132 7660 }
0a44bf69 7661 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 7662 {
9117d219
NC
7663 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
7664
252b5132
RH
7665 /* If this is a reference to an external symbol, and there
7666 is no constant, we want
7667 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 7668 or for lca or if tempreg is PIC_CALL_REG
9117d219 7669 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
7670 For a local symbol, we want
7671 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7672 nop
7673 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7674
7675 If we have a small constant, and this is a reference to
7676 an external symbol, we want
7677 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7678 nop
7679 addiu $tempreg,$tempreg,<constant>
7680 For a local symbol, we want the same instruction
7681 sequence, but we output a BFD_RELOC_LO16 reloc on the
7682 addiu instruction.
7683
7684 If we have a large constant, and this is a reference to
7685 an external symbol, we want
7686 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7687 lui $at,<hiconstant>
7688 addiu $at,$at,<loconstant>
7689 addu $tempreg,$tempreg,$at
7690 For a local symbol, we want the same instruction
7691 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 7692 addiu instruction.
ed6fb7bd
SC
7693 */
7694
4d7206a2 7695 if (offset_expr.X_add_number == 0)
252b5132 7696 {
0a44bf69
RS
7697 if (mips_pic == SVR4_PIC
7698 && breg == 0
7699 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
7700 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
7701
7702 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7703 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7704 lw_reloc_type, mips_gp_register);
4d7206a2 7705 if (breg != 0)
252b5132
RH
7706 {
7707 /* We're going to put in an addu instruction using
7708 tempreg, so we may as well insert the nop right
7709 now. */
269137b2 7710 load_delay_nop ();
252b5132 7711 }
4d7206a2 7712 relax_switch ();
67c0d1eb
RS
7713 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
7714 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 7715 load_delay_nop ();
67c0d1eb
RS
7716 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7717 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 7718 relax_end ();
252b5132
RH
7719 /* FIXME: If breg == 0, and the next instruction uses
7720 $tempreg, then if this variant case is used an extra
7721 nop will be generated. */
7722 }
4d7206a2
RS
7723 else if (offset_expr.X_add_number >= -0x8000
7724 && offset_expr.X_add_number < 0x8000)
252b5132 7725 {
67c0d1eb 7726 load_got_offset (tempreg, &offset_expr);
269137b2 7727 load_delay_nop ();
67c0d1eb 7728 add_got_offset (tempreg, &offset_expr);
252b5132
RH
7729 }
7730 else
7731 {
4d7206a2
RS
7732 expr1.X_add_number = offset_expr.X_add_number;
7733 offset_expr.X_add_number =
43c0598f 7734 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 7735 load_got_offset (tempreg, &offset_expr);
f6a22291 7736 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
7737 /* If we are going to add in a base register, and the
7738 target register and the base register are the same,
7739 then we are using AT as a temporary register. Since
7740 we want to load the constant into AT, we add our
7741 current AT (from the global offset table) and the
7742 register into the register now, and pretend we were
7743 not using a base register. */
67c0d1eb 7744 if (breg == treg)
252b5132 7745 {
269137b2 7746 load_delay_nop ();
67c0d1eb 7747 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7748 treg, AT, breg);
252b5132
RH
7749 breg = 0;
7750 tempreg = treg;
252b5132 7751 }
f6a22291 7752 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
7753 used_at = 1;
7754 }
7755 }
0a44bf69 7756 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 7757 {
67c0d1eb 7758 int add_breg_early = 0;
f5040a92
AO
7759
7760 /* If this is a reference to an external, and there is no
7761 constant, or local symbol (*), with or without a
7762 constant, we want
7763 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 7764 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
7765 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
7766
7767 If we have a small constant, and this is a reference to
7768 an external symbol, we want
7769 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7770 addiu $tempreg,$tempreg,<constant>
7771
7772 If we have a large constant, and this is a reference to
7773 an external symbol, we want
7774 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
7775 lui $at,<hiconstant>
7776 addiu $at,$at,<loconstant>
7777 addu $tempreg,$tempreg,$at
7778
7779 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
7780 local symbols, even though it introduces an additional
7781 instruction. */
7782
f5040a92
AO
7783 if (offset_expr.X_add_number)
7784 {
4d7206a2 7785 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
7786 offset_expr.X_add_number = 0;
7787
4d7206a2 7788 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7789 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7790 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7791
7792 if (expr1.X_add_number >= -0x8000
7793 && expr1.X_add_number < 0x8000)
7794 {
67c0d1eb
RS
7795 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
7796 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 7797 }
ecd13cd3 7798 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 7799 {
f5040a92
AO
7800 /* If we are going to add in a base register, and the
7801 target register and the base register are the same,
7802 then we are using AT as a temporary register. Since
7803 we want to load the constant into AT, we add our
7804 current AT (from the global offset table) and the
7805 register into the register now, and pretend we were
7806 not using a base register. */
7807 if (breg != treg)
7808 dreg = tempreg;
7809 else
7810 {
9c2799c2 7811 gas_assert (tempreg == AT);
67c0d1eb
RS
7812 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
7813 treg, AT, breg);
f5040a92 7814 dreg = treg;
67c0d1eb 7815 add_breg_early = 1;
f5040a92
AO
7816 }
7817
f6a22291 7818 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7819 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7820 dreg, dreg, AT);
f5040a92 7821
f5040a92
AO
7822 used_at = 1;
7823 }
7824 else
7825 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
7826
4d7206a2 7827 relax_switch ();
f5040a92
AO
7828 offset_expr.X_add_number = expr1.X_add_number;
7829
67c0d1eb
RS
7830 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7831 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
7832 if (add_breg_early)
f5040a92 7833 {
67c0d1eb 7834 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
f899b4b8 7835 treg, tempreg, breg);
f5040a92
AO
7836 breg = 0;
7837 tempreg = treg;
7838 }
4d7206a2 7839 relax_end ();
f5040a92 7840 }
4d7206a2 7841 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 7842 {
4d7206a2 7843 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
7844 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7845 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 7846 relax_switch ();
67c0d1eb
RS
7847 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7848 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 7849 relax_end ();
f5040a92 7850 }
4d7206a2 7851 else
f5040a92 7852 {
67c0d1eb
RS
7853 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7854 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
7855 }
7856 }
0a44bf69 7857 else if (mips_big_got && !HAVE_NEWABI)
252b5132 7858 {
67c0d1eb 7859 int gpdelay;
9117d219
NC
7860 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
7861 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 7862 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
7863
7864 /* This is the large GOT case. If this is a reference to an
7865 external symbol, and there is no constant, we want
7866 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7867 addu $tempreg,$tempreg,$gp
7868 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 7869 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
7870 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
7871 addu $tempreg,$tempreg,$gp
7872 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
7873 For a local symbol, we want
7874 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7875 nop
7876 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
7877
7878 If we have a small constant, and this is a reference to
7879 an external symbol, we want
7880 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7881 addu $tempreg,$tempreg,$gp
7882 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7883 nop
7884 addiu $tempreg,$tempreg,<constant>
7885 For a local symbol, we want
7886 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7887 nop
7888 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
7889
7890 If we have a large constant, and this is a reference to
7891 an external symbol, we want
7892 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
7893 addu $tempreg,$tempreg,$gp
7894 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
7895 lui $at,<hiconstant>
7896 addiu $at,$at,<loconstant>
7897 addu $tempreg,$tempreg,$at
7898 For a local symbol, we want
7899 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
7900 lui $at,<hiconstant>
7901 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
7902 addu $tempreg,$tempreg,$at
f5040a92 7903 */
438c16b8 7904
252b5132
RH
7905 expr1.X_add_number = offset_expr.X_add_number;
7906 offset_expr.X_add_number = 0;
4d7206a2 7907 relax_start (offset_expr.X_add_symbol);
67c0d1eb 7908 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
7909 if (expr1.X_add_number == 0 && breg == 0
7910 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
7911 {
7912 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
7913 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
7914 }
df58fc94 7915 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 7916 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7917 tempreg, tempreg, mips_gp_register);
67c0d1eb 7918 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 7919 tempreg, lw_reloc_type, tempreg);
252b5132
RH
7920 if (expr1.X_add_number == 0)
7921 {
67c0d1eb 7922 if (breg != 0)
252b5132
RH
7923 {
7924 /* We're going to put in an addu instruction using
7925 tempreg, so we may as well insert the nop right
7926 now. */
269137b2 7927 load_delay_nop ();
252b5132 7928 }
252b5132
RH
7929 }
7930 else if (expr1.X_add_number >= -0x8000
7931 && expr1.X_add_number < 0x8000)
7932 {
269137b2 7933 load_delay_nop ();
67c0d1eb 7934 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 7935 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
7936 }
7937 else
7938 {
252b5132
RH
7939 /* If we are going to add in a base register, and the
7940 target register and the base register are the same,
7941 then we are using AT as a temporary register. Since
7942 we want to load the constant into AT, we add our
7943 current AT (from the global offset table) and the
7944 register into the register now, and pretend we were
7945 not using a base register. */
7946 if (breg != treg)
67c0d1eb 7947 dreg = tempreg;
252b5132
RH
7948 else
7949 {
9c2799c2 7950 gas_assert (tempreg == AT);
269137b2 7951 load_delay_nop ();
67c0d1eb 7952 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7953 treg, AT, breg);
252b5132 7954 dreg = treg;
252b5132
RH
7955 }
7956
f6a22291 7957 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 7958 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 7959
252b5132
RH
7960 used_at = 1;
7961 }
43c0598f 7962 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 7963 relax_switch ();
252b5132 7964
67c0d1eb 7965 if (gpdelay)
252b5132
RH
7966 {
7967 /* This is needed because this instruction uses $gp, but
f5040a92 7968 the first instruction on the main stream does not. */
67c0d1eb 7969 macro_build (NULL, "nop", "");
252b5132 7970 }
ed6fb7bd 7971
67c0d1eb
RS
7972 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
7973 local_reloc_type, mips_gp_register);
f5040a92 7974 if (expr1.X_add_number >= -0x8000
252b5132
RH
7975 && expr1.X_add_number < 0x8000)
7976 {
269137b2 7977 load_delay_nop ();
67c0d1eb
RS
7978 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
7979 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 7980 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
7981 register, the external symbol case ended with a load,
7982 so if the symbol turns out to not be external, and
7983 the next instruction uses tempreg, an unnecessary nop
7984 will be inserted. */
252b5132
RH
7985 }
7986 else
7987 {
7988 if (breg == treg)
7989 {
7990 /* We must add in the base register now, as in the
f5040a92 7991 external symbol case. */
9c2799c2 7992 gas_assert (tempreg == AT);
269137b2 7993 load_delay_nop ();
67c0d1eb 7994 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 7995 treg, AT, breg);
252b5132
RH
7996 tempreg = treg;
7997 /* We set breg to 0 because we have arranged to add
f5040a92 7998 it in in both cases. */
252b5132
RH
7999 breg = 0;
8000 }
8001
67c0d1eb
RS
8002 macro_build_lui (&expr1, AT);
8003 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8004 AT, AT, BFD_RELOC_LO16);
67c0d1eb 8005 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8006 tempreg, tempreg, AT);
8fc2e39e 8007 used_at = 1;
252b5132 8008 }
4d7206a2 8009 relax_end ();
252b5132 8010 }
0a44bf69 8011 else if (mips_big_got && HAVE_NEWABI)
f5040a92 8012 {
f5040a92
AO
8013 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
8014 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 8015 int add_breg_early = 0;
f5040a92
AO
8016
8017 /* This is the large GOT case. If this is a reference to an
8018 external symbol, and there is no constant, we want
8019 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8020 add $tempreg,$tempreg,$gp
8021 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 8022 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
8023 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8024 add $tempreg,$tempreg,$gp
8025 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
8026
8027 If we have a small constant, and this is a reference to
8028 an external symbol, we want
8029 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8030 add $tempreg,$tempreg,$gp
8031 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8032 addi $tempreg,$tempreg,<constant>
8033
8034 If we have a large constant, and this is a reference to
8035 an external symbol, we want
8036 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
8037 addu $tempreg,$tempreg,$gp
8038 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
8039 lui $at,<hiconstant>
8040 addi $at,$at,<loconstant>
8041 add $tempreg,$tempreg,$at
8042
8043 If we have NewABI, and we know it's a local symbol, we want
8044 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
8045 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
8046 otherwise we have to resort to GOT_HI16/GOT_LO16. */
8047
4d7206a2 8048 relax_start (offset_expr.X_add_symbol);
f5040a92 8049
4d7206a2 8050 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
8051 offset_expr.X_add_number = 0;
8052
1abe91b1
MR
8053 if (expr1.X_add_number == 0 && breg == 0
8054 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
8055 {
8056 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
8057 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
8058 }
df58fc94 8059 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 8060 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8061 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
8062 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8063 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
8064
8065 if (expr1.X_add_number == 0)
4d7206a2 8066 ;
f5040a92
AO
8067 else if (expr1.X_add_number >= -0x8000
8068 && expr1.X_add_number < 0x8000)
8069 {
67c0d1eb 8070 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 8071 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 8072 }
ecd13cd3 8073 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 8074 {
f5040a92
AO
8075 /* If we are going to add in a base register, and the
8076 target register and the base register are the same,
8077 then we are using AT as a temporary register. Since
8078 we want to load the constant into AT, we add our
8079 current AT (from the global offset table) and the
8080 register into the register now, and pretend we were
8081 not using a base register. */
8082 if (breg != treg)
8083 dreg = tempreg;
8084 else
8085 {
9c2799c2 8086 gas_assert (tempreg == AT);
67c0d1eb 8087 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8088 treg, AT, breg);
f5040a92 8089 dreg = treg;
67c0d1eb 8090 add_breg_early = 1;
f5040a92
AO
8091 }
8092
f6a22291 8093 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 8094 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 8095
f5040a92
AO
8096 used_at = 1;
8097 }
8098 else
8099 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
8100
4d7206a2 8101 relax_switch ();
f5040a92 8102 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
8103 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
8104 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
8105 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
8106 tempreg, BFD_RELOC_MIPS_GOT_OFST);
8107 if (add_breg_early)
f5040a92 8108 {
67c0d1eb 8109 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 8110 treg, tempreg, breg);
f5040a92
AO
8111 breg = 0;
8112 tempreg = treg;
8113 }
4d7206a2 8114 relax_end ();
f5040a92 8115 }
252b5132
RH
8116 else
8117 abort ();
8118
8119 if (breg != 0)
aed1a261 8120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", treg, tempreg, breg);
252b5132
RH
8121 break;
8122
52b6b6b9 8123 case M_MSGSND:
df58fc94 8124 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8125 {
8126 unsigned long temp = (treg << 16) | (0x01);
8127 macro_build (NULL, "c2", "C", temp);
8128 }
c7af4273 8129 break;
52b6b6b9
JM
8130
8131 case M_MSGLD:
df58fc94 8132 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8133 {
8134 unsigned long temp = (0x02);
8135 macro_build (NULL, "c2", "C", temp);
8136 }
c7af4273 8137 break;
52b6b6b9
JM
8138
8139 case M_MSGLD_T:
df58fc94 8140 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8141 {
8142 unsigned long temp = (treg << 16) | (0x02);
8143 macro_build (NULL, "c2", "C", temp);
8144 }
c7af4273 8145 break;
52b6b6b9
JM
8146
8147 case M_MSGWAIT:
df58fc94 8148 gas_assert (!mips_opts.micromips);
52b6b6b9 8149 macro_build (NULL, "c2", "C", 3);
c7af4273 8150 break;
52b6b6b9
JM
8151
8152 case M_MSGWAIT_T:
df58fc94 8153 gas_assert (!mips_opts.micromips);
52b6b6b9
JM
8154 {
8155 unsigned long temp = (treg << 16) | 0x03;
8156 macro_build (NULL, "c2", "C", temp);
8157 }
c7af4273 8158 break;
52b6b6b9 8159
252b5132
RH
8160 case M_J_A:
8161 /* The j instruction may not be used in PIC code, since it
8162 requires an absolute address. We convert it to a b
8163 instruction. */
8164 if (mips_pic == NO_PIC)
67c0d1eb 8165 macro_build (&offset_expr, "j", "a");
252b5132 8166 else
67c0d1eb 8167 macro_build (&offset_expr, "b", "p");
8fc2e39e 8168 break;
252b5132
RH
8169
8170 /* The jal instructions must be handled as macros because when
8171 generating PIC code they expand to multi-instruction
8172 sequences. Normally they are simple instructions. */
df58fc94
RS
8173 case M_JALS_1:
8174 dreg = RA;
8175 /* Fall through. */
8176 case M_JALS_2:
8177 gas_assert (mips_opts.micromips);
833794fc
MR
8178 if (mips_opts.insn32)
8179 {
8180 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8181 break;
8182 }
df58fc94
RS
8183 jals = 1;
8184 goto jal;
252b5132
RH
8185 case M_JAL_1:
8186 dreg = RA;
8187 /* Fall through. */
8188 case M_JAL_2:
df58fc94 8189 jal:
3e722fb5 8190 if (mips_pic == NO_PIC)
df58fc94
RS
8191 {
8192 s = jals ? "jalrs" : "jalr";
e64af278 8193 if (mips_opts.micromips
833794fc 8194 && !mips_opts.insn32
e64af278
MR
8195 && dreg == RA
8196 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8197 macro_build (NULL, s, "mj", sreg);
8198 else
8199 macro_build (NULL, s, JALR_FMT, dreg, sreg);
8200 }
0a44bf69 8201 else
252b5132 8202 {
df58fc94
RS
8203 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
8204 && mips_cprestore_offset >= 0);
8205
252b5132
RH
8206 if (sreg != PIC_CALL_REG)
8207 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 8208
833794fc
MR
8209 s = ((mips_opts.micromips
8210 && !mips_opts.insn32
8211 && (!mips_opts.noreorder || cprestore))
df58fc94 8212 ? "jalrs" : "jalr");
e64af278 8213 if (mips_opts.micromips
833794fc 8214 && !mips_opts.insn32
e64af278
MR
8215 && dreg == RA
8216 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8217 macro_build (NULL, s, "mj", sreg);
8218 else
8219 macro_build (NULL, s, JALR_FMT, dreg, sreg);
0a44bf69 8220 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 8221 {
6478892d
TS
8222 if (mips_cprestore_offset < 0)
8223 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8224 else
8225 {
90ecf173 8226 if (!mips_frame_reg_valid)
7a621144
DJ
8227 {
8228 as_warn (_("No .frame pseudo-op used in PIC code"));
8229 /* Quiet this warning. */
8230 mips_frame_reg_valid = 1;
8231 }
90ecf173 8232 if (!mips_cprestore_valid)
7a621144
DJ
8233 {
8234 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8235 /* Quiet this warning. */
8236 mips_cprestore_valid = 1;
8237 }
d3fca0b5
MR
8238 if (mips_opts.noreorder)
8239 macro_build (NULL, "nop", "");
6478892d 8240 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8241 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8242 mips_gp_register,
256ab948
TS
8243 mips_frame_reg,
8244 HAVE_64BIT_ADDRESSES);
6478892d 8245 }
252b5132
RH
8246 }
8247 }
252b5132 8248
8fc2e39e 8249 break;
252b5132 8250
df58fc94
RS
8251 case M_JALS_A:
8252 gas_assert (mips_opts.micromips);
833794fc
MR
8253 if (mips_opts.insn32)
8254 {
8255 as_bad (_("Opcode not supported in the `insn32' mode `%s'"), str);
8256 break;
8257 }
df58fc94
RS
8258 jals = 1;
8259 /* Fall through. */
252b5132
RH
8260 case M_JAL_A:
8261 if (mips_pic == NO_PIC)
df58fc94 8262 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
8263 else if (mips_pic == SVR4_PIC)
8264 {
8265 /* If this is a reference to an external symbol, and we are
8266 using a small GOT, we want
8267 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
8268 nop
f9419b05 8269 jalr $ra,$25
252b5132
RH
8270 nop
8271 lw $gp,cprestore($sp)
8272 The cprestore value is set using the .cprestore
8273 pseudo-op. If we are using a big GOT, we want
8274 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
8275 addu $25,$25,$gp
8276 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
8277 nop
f9419b05 8278 jalr $ra,$25
252b5132
RH
8279 nop
8280 lw $gp,cprestore($sp)
8281 If the symbol is not external, we want
8282 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
8283 nop
8284 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 8285 jalr $ra,$25
252b5132 8286 nop
438c16b8 8287 lw $gp,cprestore($sp)
f5040a92
AO
8288
8289 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
8290 sequences above, minus nops, unless the symbol is local,
8291 which enables us to use GOT_PAGE/GOT_OFST (big got) or
8292 GOT_DISP. */
438c16b8 8293 if (HAVE_NEWABI)
252b5132 8294 {
90ecf173 8295 if (!mips_big_got)
f5040a92 8296 {
4d7206a2 8297 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
8298 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8299 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 8300 mips_gp_register);
4d7206a2 8301 relax_switch ();
67c0d1eb
RS
8302 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8303 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
8304 mips_gp_register);
8305 relax_end ();
f5040a92
AO
8306 }
8307 else
8308 {
4d7206a2 8309 relax_start (offset_expr.X_add_symbol);
df58fc94 8310 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8311 BFD_RELOC_MIPS_CALL_HI16);
8312 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8313 PIC_CALL_REG, mips_gp_register);
8314 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8315 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8316 PIC_CALL_REG);
4d7206a2 8317 relax_switch ();
67c0d1eb
RS
8318 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8319 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
8320 mips_gp_register);
8321 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8322 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 8323 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 8324 relax_end ();
f5040a92 8325 }
684022ea 8326
df58fc94 8327 macro_build_jalr (&offset_expr, 0);
252b5132
RH
8328 }
8329 else
8330 {
4d7206a2 8331 relax_start (offset_expr.X_add_symbol);
90ecf173 8332 if (!mips_big_got)
438c16b8 8333 {
67c0d1eb
RS
8334 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8335 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 8336 mips_gp_register);
269137b2 8337 load_delay_nop ();
4d7206a2 8338 relax_switch ();
438c16b8 8339 }
252b5132 8340 else
252b5132 8341 {
67c0d1eb
RS
8342 int gpdelay;
8343
8344 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 8345 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
8346 BFD_RELOC_MIPS_CALL_HI16);
8347 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
8348 PIC_CALL_REG, mips_gp_register);
8349 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8350 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
8351 PIC_CALL_REG);
269137b2 8352 load_delay_nop ();
4d7206a2 8353 relax_switch ();
67c0d1eb
RS
8354 if (gpdelay)
8355 macro_build (NULL, "nop", "");
252b5132 8356 }
67c0d1eb
RS
8357 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
8358 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 8359 mips_gp_register);
269137b2 8360 load_delay_nop ();
67c0d1eb
RS
8361 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8362 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 8363 relax_end ();
df58fc94 8364 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 8365
6478892d
TS
8366 if (mips_cprestore_offset < 0)
8367 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8368 else
8369 {
90ecf173 8370 if (!mips_frame_reg_valid)
7a621144
DJ
8371 {
8372 as_warn (_("No .frame pseudo-op used in PIC code"));
8373 /* Quiet this warning. */
8374 mips_frame_reg_valid = 1;
8375 }
90ecf173 8376 if (!mips_cprestore_valid)
7a621144
DJ
8377 {
8378 as_warn (_("No .cprestore pseudo-op used in PIC code"));
8379 /* Quiet this warning. */
8380 mips_cprestore_valid = 1;
8381 }
6478892d 8382 if (mips_opts.noreorder)
67c0d1eb 8383 macro_build (NULL, "nop", "");
6478892d 8384 expr1.X_add_number = mips_cprestore_offset;
67c0d1eb 8385 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 8386 mips_gp_register,
256ab948
TS
8387 mips_frame_reg,
8388 HAVE_64BIT_ADDRESSES);
6478892d 8389 }
252b5132
RH
8390 }
8391 }
0a44bf69
RS
8392 else if (mips_pic == VXWORKS_PIC)
8393 as_bad (_("Non-PIC jump used in PIC library"));
252b5132
RH
8394 else
8395 abort ();
8396
8fc2e39e 8397 break;
252b5132 8398
7f3c4072
CM
8399 case M_LBUE_AB:
8400 ab = 1;
8401 case M_LBUE_OB:
8402 s = "lbue";
8403 fmt = "t,+j(b)";
8404 offbits = 9;
8405 goto ld_st;
8406 case M_LHUE_AB:
8407 ab = 1;
8408 case M_LHUE_OB:
8409 s = "lhue";
8410 fmt = "t,+j(b)";
8411 offbits = 9;
8412 goto ld_st;
8413 case M_LBE_AB:
8414 ab = 1;
8415 case M_LBE_OB:
8416 s = "lbe";
8417 fmt = "t,+j(b)";
8418 offbits = 9;
8419 goto ld_st;
8420 case M_LHE_AB:
8421 ab = 1;
8422 case M_LHE_OB:
8423 s = "lhe";
8424 fmt = "t,+j(b)";
8425 offbits = 9;
8426 goto ld_st;
8427 case M_LLE_AB:
8428 ab = 1;
8429 case M_LLE_OB:
8430 s = "lle";
8431 fmt = "t,+j(b)";
8432 offbits = 9;
8433 goto ld_st;
8434 case M_LWE_AB:
8435 ab = 1;
8436 case M_LWE_OB:
8437 s = "lwe";
8438 fmt = "t,+j(b)";
8439 offbits = 9;
8440 goto ld_st;
8441 case M_LWLE_AB:
8442 ab = 1;
8443 case M_LWLE_OB:
8444 s = "lwle";
8445 fmt = "t,+j(b)";
8446 offbits = 9;
8447 goto ld_st;
8448 case M_LWRE_AB:
8449 ab = 1;
8450 case M_LWRE_OB:
8451 s = "lwre";
8452 fmt = "t,+j(b)";
8453 offbits = 9;
8454 goto ld_st;
8455 case M_SBE_AB:
8456 ab = 1;
8457 case M_SBE_OB:
8458 s = "sbe";
8459 fmt = "t,+j(b)";
8460 offbits = 9;
8461 goto ld_st;
8462 case M_SCE_AB:
8463 ab = 1;
8464 case M_SCE_OB:
8465 s = "sce";
8466 fmt = "t,+j(b)";
8467 offbits = 9;
8468 goto ld_st;
8469 case M_SHE_AB:
8470 ab = 1;
8471 case M_SHE_OB:
8472 s = "she";
8473 fmt = "t,+j(b)";
8474 offbits = 9;
8475 goto ld_st;
8476 case M_SWE_AB:
8477 ab = 1;
8478 case M_SWE_OB:
8479 s = "swe";
8480 fmt = "t,+j(b)";
8481 offbits = 9;
8482 goto ld_st;
8483 case M_SWLE_AB:
8484 ab = 1;
8485 case M_SWLE_OB:
8486 s = "swle";
8487 fmt = "t,+j(b)";
8488 offbits = 9;
8489 goto ld_st;
8490 case M_SWRE_AB:
8491 ab = 1;
8492 case M_SWRE_OB:
8493 s = "swre";
8494 fmt = "t,+j(b)";
8495 offbits = 9;
8496 goto ld_st;
dec0624d
MR
8497 case M_ACLR_AB:
8498 ab = 1;
8499 case M_ACLR_OB:
8500 s = "aclr";
8501 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8502 fmt = "\\,~(b)";
7f3c4072 8503 offbits = 12;
dec0624d
MR
8504 goto ld_st;
8505 case M_ASET_AB:
8506 ab = 1;
8507 case M_ASET_OB:
8508 s = "aset";
8509 treg = EXTRACT_OPERAND (mips_opts.micromips, 3BITPOS, *ip);
8510 fmt = "\\,~(b)";
7f3c4072 8511 offbits = 12;
dec0624d 8512 goto ld_st;
252b5132 8513 case M_LB_AB:
df58fc94 8514 ab = 1;
252b5132 8515 s = "lb";
df58fc94 8516 fmt = "t,o(b)";
252b5132
RH
8517 goto ld;
8518 case M_LBU_AB:
df58fc94 8519 ab = 1;
252b5132 8520 s = "lbu";
df58fc94 8521 fmt = "t,o(b)";
252b5132
RH
8522 goto ld;
8523 case M_LH_AB:
df58fc94 8524 ab = 1;
252b5132 8525 s = "lh";
df58fc94 8526 fmt = "t,o(b)";
252b5132
RH
8527 goto ld;
8528 case M_LHU_AB:
df58fc94 8529 ab = 1;
252b5132 8530 s = "lhu";
df58fc94 8531 fmt = "t,o(b)";
252b5132
RH
8532 goto ld;
8533 case M_LW_AB:
df58fc94 8534 ab = 1;
252b5132 8535 s = "lw";
df58fc94 8536 fmt = "t,o(b)";
252b5132
RH
8537 goto ld;
8538 case M_LWC0_AB:
df58fc94
RS
8539 ab = 1;
8540 gas_assert (!mips_opts.micromips);
252b5132 8541 s = "lwc0";
df58fc94 8542 fmt = "E,o(b)";
bdaaa2e1 8543 /* Itbl support may require additional care here. */
252b5132 8544 coproc = 1;
df58fc94 8545 goto ld_st;
252b5132 8546 case M_LWC1_AB:
df58fc94 8547 ab = 1;
252b5132 8548 s = "lwc1";
df58fc94 8549 fmt = "T,o(b)";
bdaaa2e1 8550 /* Itbl support may require additional care here. */
252b5132 8551 coproc = 1;
df58fc94 8552 goto ld_st;
252b5132 8553 case M_LWC2_AB:
df58fc94
RS
8554 ab = 1;
8555 case M_LWC2_OB:
252b5132 8556 s = "lwc2";
df58fc94 8557 fmt = COP12_FMT;
7f3c4072 8558 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8559 /* Itbl support may require additional care here. */
252b5132 8560 coproc = 1;
df58fc94 8561 goto ld_st;
252b5132 8562 case M_LWC3_AB:
df58fc94
RS
8563 ab = 1;
8564 gas_assert (!mips_opts.micromips);
252b5132 8565 s = "lwc3";
df58fc94 8566 fmt = "E,o(b)";
bdaaa2e1 8567 /* Itbl support may require additional care here. */
252b5132 8568 coproc = 1;
df58fc94 8569 goto ld_st;
252b5132 8570 case M_LWL_AB:
df58fc94
RS
8571 ab = 1;
8572 case M_LWL_OB:
252b5132 8573 s = "lwl";
df58fc94 8574 fmt = MEM12_FMT;
7f3c4072 8575 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8576 goto ld_st;
252b5132 8577 case M_LWR_AB:
df58fc94
RS
8578 ab = 1;
8579 case M_LWR_OB:
252b5132 8580 s = "lwr";
df58fc94 8581 fmt = MEM12_FMT;
7f3c4072 8582 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8583 goto ld_st;
252b5132 8584 case M_LDC1_AB:
df58fc94 8585 ab = 1;
252b5132 8586 s = "ldc1";
df58fc94 8587 fmt = "T,o(b)";
bdaaa2e1 8588 /* Itbl support may require additional care here. */
252b5132 8589 coproc = 1;
df58fc94 8590 goto ld_st;
252b5132 8591 case M_LDC2_AB:
df58fc94
RS
8592 ab = 1;
8593 case M_LDC2_OB:
252b5132 8594 s = "ldc2";
df58fc94 8595 fmt = COP12_FMT;
7f3c4072 8596 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8597 /* Itbl support may require additional care here. */
252b5132 8598 coproc = 1;
df58fc94 8599 goto ld_st;
c77c0862
RS
8600 case M_LQC2_AB:
8601 ab = 1;
8602 s = "lqc2";
8603 fmt = "E,o(b)";
8604 /* Itbl support may require additional care here. */
8605 coproc = 1;
8606 goto ld_st;
252b5132 8607 case M_LDC3_AB:
df58fc94 8608 ab = 1;
252b5132 8609 s = "ldc3";
df58fc94 8610 fmt = "E,o(b)";
bdaaa2e1 8611 /* Itbl support may require additional care here. */
252b5132 8612 coproc = 1;
df58fc94 8613 goto ld_st;
252b5132 8614 case M_LDL_AB:
df58fc94
RS
8615 ab = 1;
8616 case M_LDL_OB:
252b5132 8617 s = "ldl";
df58fc94 8618 fmt = MEM12_FMT;
7f3c4072 8619 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8620 goto ld_st;
252b5132 8621 case M_LDR_AB:
df58fc94
RS
8622 ab = 1;
8623 case M_LDR_OB:
252b5132 8624 s = "ldr";
df58fc94 8625 fmt = MEM12_FMT;
7f3c4072 8626 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8627 goto ld_st;
252b5132 8628 case M_LL_AB:
df58fc94
RS
8629 ab = 1;
8630 case M_LL_OB:
252b5132 8631 s = "ll";
df58fc94 8632 fmt = MEM12_FMT;
7f3c4072 8633 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8634 goto ld;
8635 case M_LLD_AB:
df58fc94
RS
8636 ab = 1;
8637 case M_LLD_OB:
252b5132 8638 s = "lld";
df58fc94 8639 fmt = MEM12_FMT;
7f3c4072 8640 offbits = (mips_opts.micromips ? 12 : 16);
252b5132
RH
8641 goto ld;
8642 case M_LWU_AB:
df58fc94
RS
8643 ab = 1;
8644 case M_LWU_OB:
252b5132 8645 s = "lwu";
df58fc94 8646 fmt = MEM12_FMT;
7f3c4072 8647 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8648 goto ld;
8649 case M_LWP_AB:
8650 ab = 1;
8651 case M_LWP_OB:
8652 gas_assert (mips_opts.micromips);
8653 s = "lwp";
8654 fmt = "t,~(b)";
7f3c4072 8655 offbits = 12;
df58fc94
RS
8656 lp = 1;
8657 goto ld;
8658 case M_LDP_AB:
8659 ab = 1;
8660 case M_LDP_OB:
8661 gas_assert (mips_opts.micromips);
8662 s = "ldp";
8663 fmt = "t,~(b)";
7f3c4072 8664 offbits = 12;
df58fc94
RS
8665 lp = 1;
8666 goto ld;
8667 case M_LWM_AB:
8668 ab = 1;
8669 case M_LWM_OB:
8670 gas_assert (mips_opts.micromips);
8671 s = "lwm";
8672 fmt = "n,~(b)";
7f3c4072 8673 offbits = 12;
df58fc94
RS
8674 goto ld_st;
8675 case M_LDM_AB:
8676 ab = 1;
8677 case M_LDM_OB:
8678 gas_assert (mips_opts.micromips);
8679 s = "ldm";
8680 fmt = "n,~(b)";
7f3c4072 8681 offbits = 12;
df58fc94
RS
8682 goto ld_st;
8683
252b5132 8684 ld:
f19ccbda
MR
8685 /* We don't want to use $0 as tempreg. */
8686 if (breg == treg + lp || treg + lp == ZERO)
df58fc94 8687 goto ld_st;
252b5132 8688 else
df58fc94
RS
8689 tempreg = treg + lp;
8690 goto ld_noat;
8691
252b5132 8692 case M_SB_AB:
df58fc94 8693 ab = 1;
252b5132 8694 s = "sb";
df58fc94
RS
8695 fmt = "t,o(b)";
8696 goto ld_st;
252b5132 8697 case M_SH_AB:
df58fc94 8698 ab = 1;
252b5132 8699 s = "sh";
df58fc94
RS
8700 fmt = "t,o(b)";
8701 goto ld_st;
252b5132 8702 case M_SW_AB:
df58fc94 8703 ab = 1;
252b5132 8704 s = "sw";
df58fc94
RS
8705 fmt = "t,o(b)";
8706 goto ld_st;
252b5132 8707 case M_SWC0_AB:
df58fc94
RS
8708 ab = 1;
8709 gas_assert (!mips_opts.micromips);
252b5132 8710 s = "swc0";
df58fc94 8711 fmt = "E,o(b)";
bdaaa2e1 8712 /* Itbl support may require additional care here. */
252b5132 8713 coproc = 1;
df58fc94 8714 goto ld_st;
252b5132 8715 case M_SWC1_AB:
df58fc94 8716 ab = 1;
252b5132 8717 s = "swc1";
df58fc94 8718 fmt = "T,o(b)";
bdaaa2e1 8719 /* Itbl support may require additional care here. */
252b5132 8720 coproc = 1;
df58fc94 8721 goto ld_st;
252b5132 8722 case M_SWC2_AB:
df58fc94
RS
8723 ab = 1;
8724 case M_SWC2_OB:
252b5132 8725 s = "swc2";
df58fc94 8726 fmt = COP12_FMT;
7f3c4072 8727 offbits = (mips_opts.micromips ? 12 : 16);
bdaaa2e1 8728 /* Itbl support may require additional care here. */
252b5132 8729 coproc = 1;
df58fc94 8730 goto ld_st;
252b5132 8731 case M_SWC3_AB:
df58fc94
RS
8732 ab = 1;
8733 gas_assert (!mips_opts.micromips);
252b5132 8734 s = "swc3";
df58fc94 8735 fmt = "E,o(b)";
bdaaa2e1 8736 /* Itbl support may require additional care here. */
252b5132 8737 coproc = 1;
df58fc94 8738 goto ld_st;
252b5132 8739 case M_SWL_AB:
df58fc94
RS
8740 ab = 1;
8741 case M_SWL_OB:
252b5132 8742 s = "swl";
df58fc94 8743 fmt = MEM12_FMT;
7f3c4072 8744 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8745 goto ld_st;
252b5132 8746 case M_SWR_AB:
df58fc94
RS
8747 ab = 1;
8748 case M_SWR_OB:
252b5132 8749 s = "swr";
df58fc94 8750 fmt = MEM12_FMT;
7f3c4072 8751 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8752 goto ld_st;
252b5132 8753 case M_SC_AB:
df58fc94
RS
8754 ab = 1;
8755 case M_SC_OB:
252b5132 8756 s = "sc";
df58fc94 8757 fmt = MEM12_FMT;
7f3c4072 8758 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8759 goto ld_st;
252b5132 8760 case M_SCD_AB:
df58fc94
RS
8761 ab = 1;
8762 case M_SCD_OB:
252b5132 8763 s = "scd";
df58fc94 8764 fmt = MEM12_FMT;
7f3c4072 8765 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8766 goto ld_st;
d43b4baf 8767 case M_CACHE_AB:
df58fc94
RS
8768 ab = 1;
8769 case M_CACHE_OB:
d43b4baf 8770 s = "cache";
df58fc94 8771 fmt = mips_opts.micromips ? "k,~(b)" : "k,o(b)";
7f3c4072
CM
8772 offbits = (mips_opts.micromips ? 12 : 16);
8773 goto ld_st;
8774 case M_CACHEE_AB:
8775 ab = 1;
8776 case M_CACHEE_OB:
8777 s = "cachee";
8778 fmt = "k,+j(b)";
8779 offbits = 9;
df58fc94 8780 goto ld_st;
3eebd5eb 8781 case M_PREF_AB:
df58fc94
RS
8782 ab = 1;
8783 case M_PREF_OB:
3eebd5eb 8784 s = "pref";
df58fc94 8785 fmt = !mips_opts.micromips ? "k,o(b)" : "k,~(b)";
7f3c4072
CM
8786 offbits = (mips_opts.micromips ? 12 : 16);
8787 goto ld_st;
8788 case M_PREFE_AB:
8789 ab = 1;
8790 case M_PREFE_OB:
8791 s = "prefe";
8792 fmt = "k,+j(b)";
8793 offbits = 9;
df58fc94 8794 goto ld_st;
252b5132 8795 case M_SDC1_AB:
df58fc94 8796 ab = 1;
252b5132 8797 s = "sdc1";
df58fc94 8798 fmt = "T,o(b)";
252b5132 8799 coproc = 1;
bdaaa2e1 8800 /* Itbl support may require additional care here. */
df58fc94 8801 goto ld_st;
252b5132 8802 case M_SDC2_AB:
df58fc94
RS
8803 ab = 1;
8804 case M_SDC2_OB:
252b5132 8805 s = "sdc2";
df58fc94 8806 fmt = COP12_FMT;
7f3c4072 8807 offbits = (mips_opts.micromips ? 12 : 16);
c77c0862
RS
8808 /* Itbl support may require additional care here. */
8809 coproc = 1;
8810 goto ld_st;
8811 case M_SQC2_AB:
8812 ab = 1;
8813 s = "sqc2";
8814 fmt = "E,o(b)";
bdaaa2e1 8815 /* Itbl support may require additional care here. */
252b5132 8816 coproc = 1;
df58fc94 8817 goto ld_st;
252b5132 8818 case M_SDC3_AB:
df58fc94
RS
8819 ab = 1;
8820 gas_assert (!mips_opts.micromips);
252b5132 8821 s = "sdc3";
df58fc94 8822 fmt = "E,o(b)";
bdaaa2e1 8823 /* Itbl support may require additional care here. */
252b5132 8824 coproc = 1;
df58fc94 8825 goto ld_st;
252b5132 8826 case M_SDL_AB:
df58fc94
RS
8827 ab = 1;
8828 case M_SDL_OB:
252b5132 8829 s = "sdl";
df58fc94 8830 fmt = MEM12_FMT;
7f3c4072 8831 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 8832 goto ld_st;
252b5132 8833 case M_SDR_AB:
df58fc94
RS
8834 ab = 1;
8835 case M_SDR_OB:
252b5132 8836 s = "sdr";
df58fc94 8837 fmt = MEM12_FMT;
7f3c4072 8838 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
8839 goto ld_st;
8840 case M_SWP_AB:
8841 ab = 1;
8842 case M_SWP_OB:
8843 gas_assert (mips_opts.micromips);
8844 s = "swp";
8845 fmt = "t,~(b)";
7f3c4072 8846 offbits = 12;
df58fc94
RS
8847 goto ld_st;
8848 case M_SDP_AB:
8849 ab = 1;
8850 case M_SDP_OB:
8851 gas_assert (mips_opts.micromips);
8852 s = "sdp";
8853 fmt = "t,~(b)";
7f3c4072 8854 offbits = 12;
df58fc94
RS
8855 goto ld_st;
8856 case M_SWM_AB:
8857 ab = 1;
8858 case M_SWM_OB:
8859 gas_assert (mips_opts.micromips);
8860 s = "swm";
8861 fmt = "n,~(b)";
7f3c4072 8862 offbits = 12;
df58fc94
RS
8863 goto ld_st;
8864 case M_SDM_AB:
8865 ab = 1;
8866 case M_SDM_OB:
8867 gas_assert (mips_opts.micromips);
8868 s = "sdm";
8869 fmt = "n,~(b)";
7f3c4072 8870 offbits = 12;
df58fc94
RS
8871
8872 ld_st:
8fc2e39e
TS
8873 tempreg = AT;
8874 used_at = 1;
df58fc94 8875 ld_noat:
252b5132
RH
8876 if (offset_expr.X_op != O_constant
8877 && offset_expr.X_op != O_symbol)
8878 {
f71d0d44 8879 as_bad (_("Expression too complex"));
252b5132
RH
8880 offset_expr.X_op = O_constant;
8881 }
8882
2051e8c4
MR
8883 if (HAVE_32BIT_ADDRESSES
8884 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
8885 {
8886 char value [32];
8887
8888 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 8889 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 8890 }
2051e8c4 8891
252b5132
RH
8892 /* A constant expression in PIC code can be handled just as it
8893 is in non PIC code. */
aed1a261
RS
8894 if (offset_expr.X_op == O_constant)
8895 {
df58fc94
RS
8896 int hipart = 0;
8897
842f8b2a 8898 expr1.X_add_number = offset_expr.X_add_number;
2051e8c4 8899 normalize_address_expr (&expr1);
7f3c4072
CM
8900 if ((offbits == 0 || offbits == 16)
8901 && !IS_SEXT_16BIT_NUM (expr1.X_add_number))
842f8b2a
MR
8902 {
8903 expr1.X_add_number = ((expr1.X_add_number + 0x8000)
8904 & ~(bfd_vma) 0xffff);
df58fc94
RS
8905 hipart = 1;
8906 }
7f3c4072 8907 else if (offbits == 12 && !IS_SEXT_12BIT_NUM (expr1.X_add_number))
df58fc94
RS
8908 {
8909 expr1.X_add_number = ((expr1.X_add_number + 0x800)
8910 & ~(bfd_vma) 0xfff);
8911 hipart = 1;
8912 }
7f3c4072
CM
8913 else if (offbits == 9 && !IS_SEXT_9BIT_NUM (expr1.X_add_number))
8914 {
8915 expr1.X_add_number = ((expr1.X_add_number + 0x100)
8916 & ~(bfd_vma) 0x1ff);
8917 hipart = 1;
8918 }
df58fc94
RS
8919 if (hipart)
8920 {
842f8b2a
MR
8921 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
8922 if (breg != 0)
8923 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8924 tempreg, tempreg, breg);
8925 breg = tempreg;
8926 }
7f3c4072 8927 if (offbits == 0)
dd6a37e7
AP
8928 {
8929 if (offset_expr.X_add_number == 0)
8930 tempreg = breg;
8931 else
8932 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
8933 "t,r,j", tempreg, breg, BFD_RELOC_LO16);
8934 macro_build (NULL, s, fmt, treg, tempreg);
8935 }
7f3c4072 8936 else if (offbits == 16)
df58fc94
RS
8937 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16, breg);
8938 else
8939 macro_build (NULL, s, fmt,
8940 treg, (unsigned long) offset_expr.X_add_number, breg);
8941 }
7f3c4072 8942 else if (offbits != 16)
df58fc94 8943 {
7f3c4072
CM
8944 /* The offset field is too narrow to be used for a low-part
8945 relocation, so load the whole address into the auxillary
8946 register. In the case of "A(b)" addresses, we first load
8947 absolute address "A" into the register and then add base
8948 register "b". In the case of "o(b)" addresses, we simply
8949 need to add 16-bit offset "o" to base register "b", and
df58fc94
RS
8950 offset_reloc already contains the relocations associated
8951 with "o". */
8952 if (ab)
8953 {
8954 load_address (tempreg, &offset_expr, &used_at);
8955 if (breg != 0)
8956 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
8957 tempreg, tempreg, breg);
8958 }
8959 else
8960 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
8961 tempreg, breg, -1,
8962 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8963 expr1.X_add_number = 0;
7f3c4072 8964 if (offbits == 0)
dd6a37e7
AP
8965 macro_build (NULL, s, fmt, treg, tempreg);
8966 else
8967 macro_build (NULL, s, fmt,
8968 treg, (unsigned long) expr1.X_add_number, tempreg);
aed1a261
RS
8969 }
8970 else if (mips_pic == NO_PIC)
252b5132
RH
8971 {
8972 /* If this is a reference to a GP relative symbol, and there
8973 is no base register, we want
cdf6fd85 8974 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
8975 Otherwise, if there is no base register, we want
8976 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8977 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8978 If we have a constant, we need two instructions anyhow,
8979 so we always use the latter form.
8980
8981 If we have a base register, and this is a reference to a
8982 GP relative symbol, we want
8983 addu $tempreg,$breg,$gp
cdf6fd85 8984 <op> $treg,<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
8985 Otherwise we want
8986 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
8987 addu $tempreg,$tempreg,$breg
8988 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 8989 With a constant we always use the latter case.
76b3015f 8990
d6bc6245
TS
8991 With 64bit address space and no base register and $at usable,
8992 we want
8993 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
8994 lui $at,<sym> (BFD_RELOC_HI16_S)
8995 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
8996 dsll32 $tempreg,0
8997 daddu $tempreg,$at
8998 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
8999 If we have a base register, we want
9000 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9001 lui $at,<sym> (BFD_RELOC_HI16_S)
9002 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9003 daddu $at,$breg
9004 dsll32 $tempreg,0
9005 daddu $tempreg,$at
9006 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9007
9008 Without $at we can't generate the optimal path for superscalar
9009 processors here since this would require two temporary registers.
9010 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9011 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9012 dsll $tempreg,16
9013 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9014 dsll $tempreg,16
9015 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
9016 If we have a base register, we want
9017 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9018 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
9019 dsll $tempreg,16
9020 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
9021 dsll $tempreg,16
9022 daddu $tempreg,$tempreg,$breg
9023 <op> $treg,<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 9024
6caf9ef4 9025 For GP relative symbols in 64bit address space we can use
aed1a261
RS
9026 the same sequence as in 32bit address space. */
9027 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9028 {
aed1a261 9029 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
9030 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
9031 {
9032 relax_start (offset_expr.X_add_symbol);
9033 if (breg == 0)
9034 {
9035 macro_build (&offset_expr, s, fmt, treg,
9036 BFD_RELOC_GPREL16, mips_gp_register);
9037 }
9038 else
9039 {
9040 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9041 tempreg, breg, mips_gp_register);
9042 macro_build (&offset_expr, s, fmt, treg,
9043 BFD_RELOC_GPREL16, tempreg);
9044 }
9045 relax_switch ();
9046 }
d6bc6245 9047
741fe287 9048 if (used_at == 0 && mips_opts.at)
d6bc6245 9049 {
df58fc94 9050 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 9051 BFD_RELOC_MIPS_HIGHEST);
df58fc94 9052 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
9053 BFD_RELOC_HI16_S);
9054 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9055 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 9056 if (breg != 0)
67c0d1eb 9057 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 9058 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb
RS
9059 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
9060 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_LO16,
9061 tempreg);
d6bc6245
TS
9062 used_at = 1;
9063 }
9064 else
9065 {
df58fc94 9066 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
9067 BFD_RELOC_MIPS_HIGHEST);
9068 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9069 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 9070 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
9071 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
9072 tempreg, BFD_RELOC_HI16_S);
df58fc94 9073 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 9074 if (breg != 0)
67c0d1eb 9075 macro_build (NULL, "daddu", "d,v,t",
17a2f251 9076 tempreg, tempreg, breg);
67c0d1eb 9077 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9078 BFD_RELOC_LO16, tempreg);
d6bc6245 9079 }
6caf9ef4
TS
9080
9081 if (mips_relax.sequence)
9082 relax_end ();
8fc2e39e 9083 break;
d6bc6245 9084 }
256ab948 9085
252b5132
RH
9086 if (breg == 0)
9087 {
67c0d1eb 9088 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9089 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9090 {
4d7206a2 9091 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9092 macro_build (&offset_expr, s, fmt, treg, BFD_RELOC_GPREL16,
9093 mips_gp_register);
4d7206a2 9094 relax_switch ();
252b5132 9095 }
67c0d1eb
RS
9096 macro_build_lui (&offset_expr, tempreg);
9097 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9098 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
9099 if (mips_relax.sequence)
9100 relax_end ();
252b5132
RH
9101 }
9102 else
9103 {
67c0d1eb 9104 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9105 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9106 {
4d7206a2 9107 relax_start (offset_expr.X_add_symbol);
67c0d1eb 9108 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9109 tempreg, breg, mips_gp_register);
67c0d1eb 9110 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9111 BFD_RELOC_GPREL16, tempreg);
4d7206a2 9112 relax_switch ();
252b5132 9113 }
67c0d1eb
RS
9114 macro_build_lui (&offset_expr, tempreg);
9115 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9116 tempreg, tempreg, breg);
67c0d1eb 9117 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9118 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
9119 if (mips_relax.sequence)
9120 relax_end ();
252b5132
RH
9121 }
9122 }
0a44bf69 9123 else if (!mips_big_got)
252b5132 9124 {
ed6fb7bd 9125 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 9126
252b5132
RH
9127 /* If this is a reference to an external symbol, we want
9128 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9129 nop
9130 <op> $treg,0($tempreg)
9131 Otherwise we want
9132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9133 nop
9134 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9135 <op> $treg,0($tempreg)
f5040a92
AO
9136
9137 For NewABI, we want
9138 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9139 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
9140
252b5132
RH
9141 If there is a base register, we add it to $tempreg before
9142 the <op>. If there is a constant, we stick it in the
9143 <op> instruction. We don't handle constants larger than
9144 16 bits, because we have no way to load the upper 16 bits
9145 (actually, we could handle them for the subset of cases
9146 in which we are not using $at). */
9c2799c2 9147 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
9148 if (HAVE_NEWABI)
9149 {
67c0d1eb
RS
9150 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9151 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 9152 if (breg != 0)
67c0d1eb 9153 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9154 tempreg, tempreg, breg);
67c0d1eb 9155 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9156 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
9157 break;
9158 }
252b5132
RH
9159 expr1.X_add_number = offset_expr.X_add_number;
9160 offset_expr.X_add_number = 0;
9161 if (expr1.X_add_number < -0x8000
9162 || expr1.X_add_number >= 0x8000)
9163 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
9164 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9165 lw_reloc_type, mips_gp_register);
269137b2 9166 load_delay_nop ();
4d7206a2
RS
9167 relax_start (offset_expr.X_add_symbol);
9168 relax_switch ();
67c0d1eb
RS
9169 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9170 tempreg, BFD_RELOC_LO16);
4d7206a2 9171 relax_end ();
252b5132 9172 if (breg != 0)
67c0d1eb 9173 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9174 tempreg, tempreg, breg);
67c0d1eb 9175 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 9176 }
0a44bf69 9177 else if (mips_big_got && !HAVE_NEWABI)
252b5132 9178 {
67c0d1eb 9179 int gpdelay;
252b5132
RH
9180
9181 /* If this is a reference to an external symbol, we want
9182 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9183 addu $tempreg,$tempreg,$gp
9184 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9185 <op> $treg,0($tempreg)
9186 Otherwise we want
9187 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9188 nop
9189 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
9190 <op> $treg,0($tempreg)
9191 If there is a base register, we add it to $tempreg before
9192 the <op>. If there is a constant, we stick it in the
9193 <op> instruction. We don't handle constants larger than
9194 16 bits, because we have no way to load the upper 16 bits
9195 (actually, we could handle them for the subset of cases
f5040a92 9196 in which we are not using $at). */
9c2799c2 9197 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
9198 expr1.X_add_number = offset_expr.X_add_number;
9199 offset_expr.X_add_number = 0;
9200 if (expr1.X_add_number < -0x8000
9201 || expr1.X_add_number >= 0x8000)
9202 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9203 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9204 relax_start (offset_expr.X_add_symbol);
df58fc94 9205 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 9206 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9207 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9208 mips_gp_register);
9209 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9210 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 9211 relax_switch ();
67c0d1eb
RS
9212 if (gpdelay)
9213 macro_build (NULL, "nop", "");
9214 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9215 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9216 load_delay_nop ();
67c0d1eb
RS
9217 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
9218 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
9219 relax_end ();
9220
252b5132 9221 if (breg != 0)
67c0d1eb 9222 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9223 tempreg, tempreg, breg);
67c0d1eb 9224 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
252b5132 9225 }
0a44bf69 9226 else if (mips_big_got && HAVE_NEWABI)
f5040a92 9227 {
f5040a92
AO
9228 /* If this is a reference to an external symbol, we want
9229 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9230 add $tempreg,$tempreg,$gp
9231 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
9232 <op> $treg,<ofst>($tempreg)
9233 Otherwise, for local symbols, we want:
9234 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9235 <op> $treg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 9236 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 9237 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
9238 offset_expr.X_add_number = 0;
9239 if (expr1.X_add_number < -0x8000
9240 || expr1.X_add_number >= 0x8000)
9241 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 9242 relax_start (offset_expr.X_add_symbol);
df58fc94 9243 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 9244 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9245 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
9246 mips_gp_register);
9247 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9248 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 9249 if (breg != 0)
67c0d1eb 9250 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9251 tempreg, tempreg, breg);
67c0d1eb 9252 macro_build (&expr1, s, fmt, treg, BFD_RELOC_LO16, tempreg);
684022ea 9253
4d7206a2 9254 relax_switch ();
f5040a92 9255 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9256 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
9257 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 9258 if (breg != 0)
67c0d1eb 9259 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9260 tempreg, tempreg, breg);
67c0d1eb 9261 macro_build (&offset_expr, s, fmt, treg,
17a2f251 9262 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 9263 relax_end ();
f5040a92 9264 }
252b5132
RH
9265 else
9266 abort ();
9267
252b5132
RH
9268 break;
9269
833794fc
MR
9270 case M_JRADDIUSP:
9271 gas_assert (mips_opts.micromips);
9272 gas_assert (mips_opts.insn32);
9273 start_noreorder ();
9274 macro_build (NULL, "jr", "s", RA);
9275 expr1.X_add_number = EXTRACT_OPERAND (1, IMMP, *ip) << 2;
9276 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
9277 end_noreorder ();
9278 break;
9279
9280 case M_JRC:
9281 gas_assert (mips_opts.micromips);
9282 gas_assert (mips_opts.insn32);
9283 macro_build (NULL, "jr", "s", sreg);
9284 if (mips_opts.noreorder)
9285 macro_build (NULL, "nop", "");
9286 break;
9287
252b5132
RH
9288 case M_LI:
9289 case M_LI_S:
67c0d1eb 9290 load_register (treg, &imm_expr, 0);
8fc2e39e 9291 break;
252b5132
RH
9292
9293 case M_DLI:
67c0d1eb 9294 load_register (treg, &imm_expr, 1);
8fc2e39e 9295 break;
252b5132
RH
9296
9297 case M_LI_SS:
9298 if (imm_expr.X_op == O_constant)
9299 {
8fc2e39e 9300 used_at = 1;
67c0d1eb
RS
9301 load_register (AT, &imm_expr, 0);
9302 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
9303 break;
9304 }
9305 else
9306 {
9c2799c2 9307 gas_assert (offset_expr.X_op == O_symbol
90ecf173
MR
9308 && strcmp (segment_name (S_GET_SEGMENT
9309 (offset_expr.X_add_symbol)),
9310 ".lit4") == 0
9311 && offset_expr.X_add_number == 0);
67c0d1eb 9312 macro_build (&offset_expr, "lwc1", "T,o(b)", treg,
17a2f251 9313 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 9314 break;
252b5132
RH
9315 }
9316
9317 case M_LI_D:
ca4e0257
RS
9318 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
9319 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
9320 order 32 bits of the value and the low order 32 bits are either
9321 zero or in OFFSET_EXPR. */
252b5132
RH
9322 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9323 {
ca4e0257 9324 if (HAVE_64BIT_GPRS)
67c0d1eb 9325 load_register (treg, &imm_expr, 1);
252b5132
RH
9326 else
9327 {
9328 int hreg, lreg;
9329
9330 if (target_big_endian)
9331 {
9332 hreg = treg;
9333 lreg = treg + 1;
9334 }
9335 else
9336 {
9337 hreg = treg + 1;
9338 lreg = treg;
9339 }
9340
9341 if (hreg <= 31)
67c0d1eb 9342 load_register (hreg, &imm_expr, 0);
252b5132
RH
9343 if (lreg <= 31)
9344 {
9345 if (offset_expr.X_op == O_absent)
67c0d1eb 9346 move_register (lreg, 0);
252b5132
RH
9347 else
9348 {
9c2799c2 9349 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 9350 load_register (lreg, &offset_expr, 0);
252b5132
RH
9351 }
9352 }
9353 }
8fc2e39e 9354 break;
252b5132
RH
9355 }
9356
9357 /* We know that sym is in the .rdata section. First we get the
9358 upper 16 bits of the address. */
9359 if (mips_pic == NO_PIC)
9360 {
67c0d1eb 9361 macro_build_lui (&offset_expr, AT);
8fc2e39e 9362 used_at = 1;
252b5132 9363 }
0a44bf69 9364 else
252b5132 9365 {
67c0d1eb
RS
9366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9367 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 9368 used_at = 1;
252b5132 9369 }
bdaaa2e1 9370
252b5132 9371 /* Now we load the register(s). */
ca4e0257 9372 if (HAVE_64BIT_GPRS)
8fc2e39e
TS
9373 {
9374 used_at = 1;
9375 macro_build (&offset_expr, "ld", "t,o(b)", treg, BFD_RELOC_LO16, AT);
9376 }
252b5132
RH
9377 else
9378 {
8fc2e39e 9379 used_at = 1;
67c0d1eb 9380 macro_build (&offset_expr, "lw", "t,o(b)", treg, BFD_RELOC_LO16, AT);
f9419b05 9381 if (treg != RA)
252b5132
RH
9382 {
9383 /* FIXME: How in the world do we deal with the possible
9384 overflow here? */
9385 offset_expr.X_add_number += 4;
67c0d1eb 9386 macro_build (&offset_expr, "lw", "t,o(b)",
17a2f251 9387 treg + 1, BFD_RELOC_LO16, AT);
252b5132
RH
9388 }
9389 }
252b5132
RH
9390 break;
9391
9392 case M_LI_DD:
ca4e0257
RS
9393 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
9394 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
9395 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
9396 the value and the low order 32 bits are either zero or in
9397 OFFSET_EXPR. */
252b5132
RH
9398 if (imm_expr.X_op == O_constant || imm_expr.X_op == O_big)
9399 {
8fc2e39e 9400 used_at = 1;
67c0d1eb 9401 load_register (AT, &imm_expr, HAVE_64BIT_FPRS);
ca4e0257
RS
9402 if (HAVE_64BIT_FPRS)
9403 {
9c2799c2 9404 gas_assert (HAVE_64BIT_GPRS);
67c0d1eb 9405 macro_build (NULL, "dmtc1", "t,S", AT, treg);
ca4e0257 9406 }
252b5132
RH
9407 else
9408 {
67c0d1eb 9409 macro_build (NULL, "mtc1", "t,G", AT, treg + 1);
252b5132 9410 if (offset_expr.X_op == O_absent)
67c0d1eb 9411 macro_build (NULL, "mtc1", "t,G", 0, treg);
252b5132
RH
9412 else
9413 {
9c2799c2 9414 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb
RS
9415 load_register (AT, &offset_expr, 0);
9416 macro_build (NULL, "mtc1", "t,G", AT, treg);
252b5132
RH
9417 }
9418 }
9419 break;
9420 }
9421
9c2799c2 9422 gas_assert (offset_expr.X_op == O_symbol
90ecf173 9423 && offset_expr.X_add_number == 0);
252b5132
RH
9424 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
9425 if (strcmp (s, ".lit8") == 0)
9426 {
0aa27725 9427 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9428 {
67c0d1eb 9429 macro_build (&offset_expr, "ldc1", "T,o(b)", treg,
17a2f251 9430 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 9431 break;
252b5132 9432 }
c9914766 9433 breg = mips_gp_register;
252b5132
RH
9434 r = BFD_RELOC_MIPS_LITERAL;
9435 goto dob;
9436 }
9437 else
9438 {
9c2799c2 9439 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 9440 used_at = 1;
0a44bf69 9441 if (mips_pic != NO_PIC)
67c0d1eb
RS
9442 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9443 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
9444 else
9445 {
9446 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 9447 macro_build_lui (&offset_expr, AT);
252b5132 9448 }
bdaaa2e1 9449
0aa27725 9450 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch) || mips_opts.micromips)
252b5132 9451 {
67c0d1eb
RS
9452 macro_build (&offset_expr, "ldc1", "T,o(b)",
9453 treg, BFD_RELOC_LO16, AT);
252b5132
RH
9454 break;
9455 }
9456 breg = AT;
9457 r = BFD_RELOC_LO16;
9458 goto dob;
9459 }
9460
9461 case M_L_DOB:
252b5132
RH
9462 /* Even on a big endian machine $fn comes before $fn+1. We have
9463 to adjust when loading from memory. */
9464 r = BFD_RELOC_LO16;
9465 dob:
df58fc94 9466 gas_assert (!mips_opts.micromips);
0aa27725 9467 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
67c0d1eb 9468 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9469 target_big_endian ? treg + 1 : treg, r, breg);
252b5132
RH
9470 /* FIXME: A possible overflow which I don't know how to deal
9471 with. */
9472 offset_expr.X_add_number += 4;
67c0d1eb 9473 macro_build (&offset_expr, "lwc1", "T,o(b)",
17a2f251 9474 target_big_endian ? treg : treg + 1, r, breg);
252b5132
RH
9475 break;
9476
c4a68bea 9477 case M_S_DOB:
df58fc94 9478 gas_assert (!mips_opts.micromips);
0aa27725 9479 gas_assert (!CPU_HAS_LDC1_SDC1 (mips_opts.arch));
c4a68bea
MR
9480 /* Even on a big endian machine $fn comes before $fn+1. We have
9481 to adjust when storing to memory. */
9482 macro_build (&offset_expr, "swc1", "T,o(b)",
9483 target_big_endian ? treg + 1 : treg, BFD_RELOC_LO16, breg);
9484 offset_expr.X_add_number += 4;
9485 macro_build (&offset_expr, "swc1", "T,o(b)",
9486 target_big_endian ? treg : treg + 1, BFD_RELOC_LO16, breg);
9487 break;
9488
252b5132 9489 case M_L_DAB:
df58fc94 9490 gas_assert (!mips_opts.micromips);
252b5132
RH
9491 /*
9492 * The MIPS assembler seems to check for X_add_number not
9493 * being double aligned and generating:
9494 * lui at,%hi(foo+1)
9495 * addu at,at,v1
9496 * addiu at,at,%lo(foo+1)
9497 * lwc1 f2,0(at)
9498 * lwc1 f3,4(at)
9499 * But, the resulting address is the same after relocation so why
9500 * generate the extra instruction?
9501 */
bdaaa2e1 9502 /* Itbl support may require additional care here. */
252b5132 9503 coproc = 1;
df58fc94 9504 fmt = "T,o(b)";
0aa27725 9505 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9506 {
9507 s = "ldc1";
df58fc94 9508 goto ld_st;
252b5132 9509 }
252b5132 9510 s = "lwc1";
252b5132
RH
9511 goto ldd_std;
9512
9513 case M_S_DAB:
df58fc94
RS
9514 gas_assert (!mips_opts.micromips);
9515 /* Itbl support may require additional care here. */
9516 coproc = 1;
9517 fmt = "T,o(b)";
0aa27725 9518 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
9519 {
9520 s = "sdc1";
df58fc94 9521 goto ld_st;
252b5132 9522 }
252b5132 9523 s = "swc1";
252b5132
RH
9524 goto ldd_std;
9525
e407c74b
NC
9526 case M_LQ_AB:
9527 fmt = "t,o(b)";
9528 s = "lq";
9529 goto ld;
9530
9531 case M_SQ_AB:
9532 fmt = "t,o(b)";
9533 s = "sq";
9534 goto ld_st;
9535
252b5132 9536 case M_LD_AB:
df58fc94 9537 fmt = "t,o(b)";
ca4e0257 9538 if (HAVE_64BIT_GPRS)
252b5132
RH
9539 {
9540 s = "ld";
9541 goto ld;
9542 }
252b5132 9543 s = "lw";
252b5132
RH
9544 goto ldd_std;
9545
9546 case M_SD_AB:
df58fc94 9547 fmt = "t,o(b)";
ca4e0257 9548 if (HAVE_64BIT_GPRS)
252b5132
RH
9549 {
9550 s = "sd";
df58fc94 9551 goto ld_st;
252b5132 9552 }
252b5132 9553 s = "sw";
252b5132
RH
9554
9555 ldd_std:
9556 if (offset_expr.X_op != O_symbol
9557 && offset_expr.X_op != O_constant)
9558 {
f71d0d44 9559 as_bad (_("Expression too complex"));
252b5132
RH
9560 offset_expr.X_op = O_constant;
9561 }
9562
2051e8c4
MR
9563 if (HAVE_32BIT_ADDRESSES
9564 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
9565 {
9566 char value [32];
9567
9568 sprintf_vma (value, offset_expr.X_add_number);
20e1fcfd 9569 as_bad (_("Number (0x%s) larger than 32 bits"), value);
55e08f71 9570 }
2051e8c4 9571
252b5132
RH
9572 /* Even on a big endian machine $fn comes before $fn+1. We have
9573 to adjust when loading from memory. We set coproc if we must
9574 load $fn+1 first. */
bdaaa2e1 9575 /* Itbl support may require additional care here. */
90ecf173 9576 if (!target_big_endian)
252b5132
RH
9577 coproc = 0;
9578
90ecf173 9579 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
9580 {
9581 /* If this is a reference to a GP relative symbol, we want
cdf6fd85
TS
9582 <op> $treg,<sym>($gp) (BFD_RELOC_GPREL16)
9583 <op> $treg+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
9584 If we have a base register, we use this
9585 addu $at,$breg,$gp
cdf6fd85
TS
9586 <op> $treg,<sym>($at) (BFD_RELOC_GPREL16)
9587 <op> $treg+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
9588 If this is not a GP relative symbol, we want
9589 lui $at,<sym> (BFD_RELOC_HI16_S)
9590 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9591 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9592 If there is a base register, we add it to $at after the
9593 lui instruction. If there is a constant, we always use
9594 the last case. */
39a59cf8
MR
9595 if (offset_expr.X_op == O_symbol
9596 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9597 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 9598 {
4d7206a2 9599 relax_start (offset_expr.X_add_symbol);
252b5132
RH
9600 if (breg == 0)
9601 {
c9914766 9602 tempreg = mips_gp_register;
252b5132
RH
9603 }
9604 else
9605 {
67c0d1eb 9606 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9607 AT, breg, mips_gp_register);
252b5132 9608 tempreg = AT;
252b5132
RH
9609 used_at = 1;
9610 }
9611
beae10d5 9612 /* Itbl support may require additional care here. */
67c0d1eb 9613 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9614 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9615 offset_expr.X_add_number += 4;
9616
9617 /* Set mips_optimize to 2 to avoid inserting an
9618 undesired nop. */
9619 hold_mips_optimize = mips_optimize;
9620 mips_optimize = 2;
beae10d5 9621 /* Itbl support may require additional care here. */
67c0d1eb 9622 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9623 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
9624 mips_optimize = hold_mips_optimize;
9625
4d7206a2 9626 relax_switch ();
252b5132 9627
0970e49e 9628 offset_expr.X_add_number -= 4;
252b5132 9629 }
8fc2e39e 9630 used_at = 1;
67c0d1eb 9631 macro_build_lui (&offset_expr, AT);
252b5132 9632 if (breg != 0)
67c0d1eb 9633 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9634 /* Itbl support may require additional care here. */
67c0d1eb 9635 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9636 BFD_RELOC_LO16, AT);
252b5132
RH
9637 /* FIXME: How do we handle overflow here? */
9638 offset_expr.X_add_number += 4;
beae10d5 9639 /* Itbl support may require additional care here. */
67c0d1eb 9640 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
17a2f251 9641 BFD_RELOC_LO16, AT);
4d7206a2
RS
9642 if (mips_relax.sequence)
9643 relax_end ();
bdaaa2e1 9644 }
0a44bf69 9645 else if (!mips_big_got)
252b5132 9646 {
252b5132
RH
9647 /* If this is a reference to an external symbol, we want
9648 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9649 nop
9650 <op> $treg,0($at)
9651 <op> $treg+1,4($at)
9652 Otherwise we want
9653 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9654 nop
9655 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9656 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9657 If there is a base register we add it to $at before the
9658 lwc1 instructions. If there is a constant we include it
9659 in the lwc1 instructions. */
9660 used_at = 1;
9661 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
9662 if (expr1.X_add_number < -0x8000
9663 || expr1.X_add_number >= 0x8000 - 4)
9664 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9665 load_got_offset (AT, &offset_expr);
269137b2 9666 load_delay_nop ();
252b5132 9667 if (breg != 0)
67c0d1eb 9668 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
9669
9670 /* Set mips_optimize to 2 to avoid inserting an undesired
9671 nop. */
9672 hold_mips_optimize = mips_optimize;
9673 mips_optimize = 2;
4d7206a2 9674
beae10d5 9675 /* Itbl support may require additional care here. */
4d7206a2 9676 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
9677 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
9678 BFD_RELOC_LO16, AT);
4d7206a2 9679 expr1.X_add_number += 4;
67c0d1eb
RS
9680 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
9681 BFD_RELOC_LO16, AT);
4d7206a2 9682 relax_switch ();
67c0d1eb
RS
9683 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9684 BFD_RELOC_LO16, AT);
4d7206a2 9685 offset_expr.X_add_number += 4;
67c0d1eb
RS
9686 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9687 BFD_RELOC_LO16, AT);
4d7206a2 9688 relax_end ();
252b5132 9689
4d7206a2 9690 mips_optimize = hold_mips_optimize;
252b5132 9691 }
0a44bf69 9692 else if (mips_big_got)
252b5132 9693 {
67c0d1eb 9694 int gpdelay;
252b5132
RH
9695
9696 /* If this is a reference to an external symbol, we want
9697 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9698 addu $at,$at,$gp
9699 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
9700 nop
9701 <op> $treg,0($at)
9702 <op> $treg+1,4($at)
9703 Otherwise we want
9704 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9705 nop
9706 <op> $treg,<sym>($at) (BFD_RELOC_LO16)
9707 <op> $treg+1,<sym>+4($at) (BFD_RELOC_LO16)
9708 If there is a base register we add it to $at before the
9709 lwc1 instructions. If there is a constant we include it
9710 in the lwc1 instructions. */
9711 used_at = 1;
9712 expr1.X_add_number = offset_expr.X_add_number;
9713 offset_expr.X_add_number = 0;
9714 if (expr1.X_add_number < -0x8000
9715 || expr1.X_add_number >= 0x8000 - 4)
9716 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 9717 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 9718 relax_start (offset_expr.X_add_symbol);
df58fc94 9719 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
9720 AT, BFD_RELOC_MIPS_GOT_HI16);
9721 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 9722 AT, AT, mips_gp_register);
67c0d1eb 9723 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 9724 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 9725 load_delay_nop ();
252b5132 9726 if (breg != 0)
67c0d1eb 9727 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9728 /* Itbl support may require additional care here. */
67c0d1eb 9729 macro_build (&expr1, s, fmt, coproc ? treg + 1 : treg,
17a2f251 9730 BFD_RELOC_LO16, AT);
252b5132
RH
9731 expr1.X_add_number += 4;
9732
9733 /* Set mips_optimize to 2 to avoid inserting an undesired
9734 nop. */
9735 hold_mips_optimize = mips_optimize;
9736 mips_optimize = 2;
beae10d5 9737 /* Itbl support may require additional care here. */
67c0d1eb 9738 macro_build (&expr1, s, fmt, coproc ? treg : treg + 1,
17a2f251 9739 BFD_RELOC_LO16, AT);
252b5132
RH
9740 mips_optimize = hold_mips_optimize;
9741 expr1.X_add_number -= 4;
9742
4d7206a2
RS
9743 relax_switch ();
9744 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
9745 if (gpdelay)
9746 macro_build (NULL, "nop", "");
9747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
9748 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9749 load_delay_nop ();
252b5132 9750 if (breg != 0)
67c0d1eb 9751 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 9752 /* Itbl support may require additional care here. */
67c0d1eb
RS
9753 macro_build (&offset_expr, s, fmt, coproc ? treg + 1 : treg,
9754 BFD_RELOC_LO16, AT);
4d7206a2 9755 offset_expr.X_add_number += 4;
252b5132
RH
9756
9757 /* Set mips_optimize to 2 to avoid inserting an undesired
9758 nop. */
9759 hold_mips_optimize = mips_optimize;
9760 mips_optimize = 2;
beae10d5 9761 /* Itbl support may require additional care here. */
67c0d1eb
RS
9762 macro_build (&offset_expr, s, fmt, coproc ? treg : treg + 1,
9763 BFD_RELOC_LO16, AT);
252b5132 9764 mips_optimize = hold_mips_optimize;
4d7206a2 9765 relax_end ();
252b5132 9766 }
252b5132
RH
9767 else
9768 abort ();
9769
252b5132
RH
9770 break;
9771
9772 case M_LD_OB:
704897fb 9773 s = HAVE_64BIT_GPRS ? "ld" : "lw";
252b5132
RH
9774 goto sd_ob;
9775 case M_SD_OB:
704897fb 9776 s = HAVE_64BIT_GPRS ? "sd" : "sw";
252b5132 9777 sd_ob:
4614d845
MR
9778 macro_build (&offset_expr, s, "t,o(b)", treg,
9779 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9780 breg);
704897fb
MR
9781 if (!HAVE_64BIT_GPRS)
9782 {
9783 offset_expr.X_add_number += 4;
9784 macro_build (&offset_expr, s, "t,o(b)", treg + 1,
4614d845
MR
9785 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2],
9786 breg);
704897fb 9787 }
8fc2e39e 9788 break;
252b5132 9789
dd6a37e7
AP
9790
9791 case M_SAA_AB:
9792 ab = 1;
9793 case M_SAA_OB:
9794 s = "saa";
7f3c4072 9795 offbits = 0;
dd6a37e7
AP
9796 fmt = "t,(b)";
9797 goto ld_st;
9798 case M_SAAD_AB:
9799 ab = 1;
9800 case M_SAAD_OB:
9801 s = "saad";
7f3c4072 9802 offbits = 0;
dd6a37e7
AP
9803 fmt = "t,(b)";
9804 goto ld_st;
9805
252b5132
RH
9806 /* New code added to support COPZ instructions.
9807 This code builds table entries out of the macros in mip_opcodes.
9808 R4000 uses interlocks to handle coproc delays.
9809 Other chips (like the R3000) require nops to be inserted for delays.
9810
f72c8c98 9811 FIXME: Currently, we require that the user handle delays.
252b5132
RH
9812 In order to fill delay slots for non-interlocked chips,
9813 we must have a way to specify delays based on the coprocessor.
9814 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
9815 What are the side-effects of the cop instruction?
9816 What cache support might we have and what are its effects?
9817 Both coprocessor & memory require delays. how long???
bdaaa2e1 9818 What registers are read/set/modified?
252b5132
RH
9819
9820 If an itbl is provided to interpret cop instructions,
bdaaa2e1 9821 this knowledge can be encoded in the itbl spec. */
252b5132
RH
9822
9823 case M_COP0:
9824 s = "c0";
9825 goto copz;
9826 case M_COP1:
9827 s = "c1";
9828 goto copz;
9829 case M_COP2:
9830 s = "c2";
9831 goto copz;
9832 case M_COP3:
9833 s = "c3";
9834 copz:
df58fc94 9835 gas_assert (!mips_opts.micromips);
252b5132
RH
9836 /* For now we just do C (same as Cz). The parameter will be
9837 stored in insn_opcode by mips_ip. */
67c0d1eb 9838 macro_build (NULL, s, "C", ip->insn_opcode);
8fc2e39e 9839 break;
252b5132 9840
ea1fb5dc 9841 case M_MOVE:
67c0d1eb 9842 move_register (dreg, sreg);
8fc2e39e 9843 break;
ea1fb5dc 9844
833794fc
MR
9845 case M_MOVEP:
9846 gas_assert (mips_opts.micromips);
9847 gas_assert (mips_opts.insn32);
e76ff5ab
RS
9848 dreg = micromips_to_32_reg_h_map1[EXTRACT_OPERAND (1, MH, *ip)];
9849 breg = micromips_to_32_reg_h_map2[EXTRACT_OPERAND (1, MH, *ip)];
833794fc
MR
9850 sreg = micromips_to_32_reg_m_map[EXTRACT_OPERAND (1, MM, *ip)];
9851 treg = micromips_to_32_reg_n_map[EXTRACT_OPERAND (1, MN, *ip)];
9852 move_register (dreg, sreg);
9853 move_register (breg, treg);
9854 break;
9855
252b5132
RH
9856 case M_DMUL:
9857 dbl = 1;
9858 case M_MUL:
e407c74b
NC
9859 if (mips_opts.arch == CPU_R5900)
9860 {
9861 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", dreg, sreg, treg);
9862 }
9863 else
9864 {
67c0d1eb 9865 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", sreg, treg);
df58fc94 9866 macro_build (NULL, "mflo", MFHL_FMT, dreg);
e407c74b 9867 }
8fc2e39e 9868 break;
252b5132
RH
9869
9870 case M_DMUL_I:
9871 dbl = 1;
9872 case M_MUL_I:
9873 /* The MIPS assembler some times generates shifts and adds. I'm
9874 not trying to be that fancy. GCC should do this for us
9875 anyway. */
8fc2e39e 9876 used_at = 1;
67c0d1eb
RS
9877 load_register (AT, &imm_expr, dbl);
9878 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, AT);
df58fc94 9879 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9880 break;
9881
9882 case M_DMULO_I:
9883 dbl = 1;
9884 case M_MULO_I:
9885 imm = 1;
9886 goto do_mulo;
9887
9888 case M_DMULO:
9889 dbl = 1;
9890 case M_MULO:
9891 do_mulo:
7d10b47d 9892 start_noreorder ();
8fc2e39e 9893 used_at = 1;
252b5132 9894 if (imm)
67c0d1eb
RS
9895 load_register (AT, &imm_expr, dbl);
9896 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", sreg, imm ? AT : treg);
df58fc94
RS
9897 macro_build (NULL, "mflo", MFHL_FMT, dreg);
9898 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, dreg, dreg, RA);
9899 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 9900 if (mips_trap)
df58fc94 9901 macro_build (NULL, "tne", TRAP_FMT, dreg, AT, 6);
252b5132
RH
9902 else
9903 {
df58fc94
RS
9904 if (mips_opts.micromips)
9905 micromips_label_expr (&label_expr);
9906 else
9907 label_expr.X_add_number = 8;
9908 macro_build (&label_expr, "beq", "s,t,p", dreg, AT);
a605d2b3 9909 macro_build (NULL, "nop", "");
df58fc94
RS
9910 macro_build (NULL, "break", BRK_FMT, 6);
9911 if (mips_opts.micromips)
9912 micromips_add_label ();
252b5132 9913 }
7d10b47d 9914 end_noreorder ();
df58fc94 9915 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132
RH
9916 break;
9917
9918 case M_DMULOU_I:
9919 dbl = 1;
9920 case M_MULOU_I:
9921 imm = 1;
9922 goto do_mulou;
9923
9924 case M_DMULOU:
9925 dbl = 1;
9926 case M_MULOU:
9927 do_mulou:
7d10b47d 9928 start_noreorder ();
8fc2e39e 9929 used_at = 1;
252b5132 9930 if (imm)
67c0d1eb
RS
9931 load_register (AT, &imm_expr, dbl);
9932 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
17a2f251 9933 sreg, imm ? AT : treg);
df58fc94
RS
9934 macro_build (NULL, "mfhi", MFHL_FMT, AT);
9935 macro_build (NULL, "mflo", MFHL_FMT, dreg);
252b5132 9936 if (mips_trap)
df58fc94 9937 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
9938 else
9939 {
df58fc94
RS
9940 if (mips_opts.micromips)
9941 micromips_label_expr (&label_expr);
9942 else
9943 label_expr.X_add_number = 8;
9944 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 9945 macro_build (NULL, "nop", "");
df58fc94
RS
9946 macro_build (NULL, "break", BRK_FMT, 6);
9947 if (mips_opts.micromips)
9948 micromips_add_label ();
252b5132 9949 }
7d10b47d 9950 end_noreorder ();
252b5132
RH
9951 break;
9952
771c7ce4 9953 case M_DROL:
fef14a42 9954 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
9955 {
9956 if (dreg == sreg)
9957 {
9958 tempreg = AT;
9959 used_at = 1;
9960 }
9961 else
9962 {
9963 tempreg = dreg;
82dd0097 9964 }
67c0d1eb
RS
9965 macro_build (NULL, "dnegu", "d,w", tempreg, treg);
9966 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9967 break;
82dd0097 9968 }
8fc2e39e 9969 used_at = 1;
c80c840e 9970 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9971 macro_build (NULL, "dsrlv", "d,t,s", AT, sreg, AT);
9972 macro_build (NULL, "dsllv", "d,t,s", dreg, sreg, treg);
9973 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
9974 break;
9975
252b5132 9976 case M_ROL:
fef14a42 9977 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097
CD
9978 {
9979 if (dreg == sreg)
9980 {
9981 tempreg = AT;
9982 used_at = 1;
9983 }
9984 else
9985 {
9986 tempreg = dreg;
82dd0097 9987 }
67c0d1eb
RS
9988 macro_build (NULL, "negu", "d,w", tempreg, treg);
9989 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, tempreg);
8fc2e39e 9990 break;
82dd0097 9991 }
8fc2e39e 9992 used_at = 1;
c80c840e 9993 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
9994 macro_build (NULL, "srlv", "d,t,s", AT, sreg, AT);
9995 macro_build (NULL, "sllv", "d,t,s", dreg, sreg, treg);
9996 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
9997 break;
9998
771c7ce4
TS
9999 case M_DROL_I:
10000 {
10001 unsigned int rot;
91d6fa6a
NC
10002 char *l;
10003 char *rr;
771c7ce4
TS
10004
10005 if (imm_expr.X_op != O_constant)
82dd0097 10006 as_bad (_("Improper rotate count"));
771c7ce4 10007 rot = imm_expr.X_add_number & 0x3f;
fef14a42 10008 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
10009 {
10010 rot = (64 - rot) & 0x3f;
10011 if (rot >= 32)
df58fc94 10012 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
60b63b72 10013 else
df58fc94 10014 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10015 break;
60b63b72 10016 }
483fc7cd 10017 if (rot == 0)
483fc7cd 10018 {
df58fc94 10019 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10020 break;
483fc7cd 10021 }
82dd0097 10022 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 10023 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 10024 rot &= 0x1f;
8fc2e39e 10025 used_at = 1;
df58fc94
RS
10026 macro_build (NULL, l, SHFT_FMT, AT, sreg, rot);
10027 macro_build (NULL, rr, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10028 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10029 }
10030 break;
10031
252b5132 10032 case M_ROL_I:
771c7ce4
TS
10033 {
10034 unsigned int rot;
10035
10036 if (imm_expr.X_op != O_constant)
82dd0097 10037 as_bad (_("Improper rotate count"));
771c7ce4 10038 rot = imm_expr.X_add_number & 0x1f;
fef14a42 10039 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 10040 {
df58fc94 10041 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, (32 - rot) & 0x1f);
8fc2e39e 10042 break;
60b63b72 10043 }
483fc7cd 10044 if (rot == 0)
483fc7cd 10045 {
df58fc94 10046 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10047 break;
483fc7cd 10048 }
8fc2e39e 10049 used_at = 1;
df58fc94
RS
10050 macro_build (NULL, "sll", SHFT_FMT, AT, sreg, rot);
10051 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10052 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10053 }
10054 break;
10055
10056 case M_DROR:
fef14a42 10057 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 10058 {
67c0d1eb 10059 macro_build (NULL, "drorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 10060 break;
82dd0097 10061 }
8fc2e39e 10062 used_at = 1;
c80c840e 10063 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
10064 macro_build (NULL, "dsllv", "d,t,s", AT, sreg, AT);
10065 macro_build (NULL, "dsrlv", "d,t,s", dreg, sreg, treg);
10066 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
10067 break;
10068
10069 case M_ROR:
fef14a42 10070 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 10071 {
67c0d1eb 10072 macro_build (NULL, "rorv", "d,t,s", dreg, sreg, treg);
8fc2e39e 10073 break;
82dd0097 10074 }
8fc2e39e 10075 used_at = 1;
c80c840e 10076 macro_build (NULL, "subu", "d,v,t", AT, ZERO, treg);
67c0d1eb
RS
10077 macro_build (NULL, "sllv", "d,t,s", AT, sreg, AT);
10078 macro_build (NULL, "srlv", "d,t,s", dreg, sreg, treg);
10079 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
252b5132
RH
10080 break;
10081
771c7ce4
TS
10082 case M_DROR_I:
10083 {
10084 unsigned int rot;
91d6fa6a
NC
10085 char *l;
10086 char *rr;
771c7ce4
TS
10087
10088 if (imm_expr.X_op != O_constant)
82dd0097 10089 as_bad (_("Improper rotate count"));
771c7ce4 10090 rot = imm_expr.X_add_number & 0x3f;
fef14a42 10091 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
10092 {
10093 if (rot >= 32)
df58fc94 10094 macro_build (NULL, "dror32", SHFT_FMT, dreg, sreg, rot - 32);
82dd0097 10095 else
df58fc94 10096 macro_build (NULL, "dror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10097 break;
82dd0097 10098 }
483fc7cd 10099 if (rot == 0)
483fc7cd 10100 {
df58fc94 10101 macro_build (NULL, "dsrl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10102 break;
483fc7cd 10103 }
91d6fa6a 10104 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
10105 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
10106 rot &= 0x1f;
8fc2e39e 10107 used_at = 1;
df58fc94
RS
10108 macro_build (NULL, rr, SHFT_FMT, AT, sreg, rot);
10109 macro_build (NULL, l, SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10110 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4
TS
10111 }
10112 break;
10113
252b5132 10114 case M_ROR_I:
771c7ce4
TS
10115 {
10116 unsigned int rot;
10117
10118 if (imm_expr.X_op != O_constant)
82dd0097 10119 as_bad (_("Improper rotate count"));
771c7ce4 10120 rot = imm_expr.X_add_number & 0x1f;
fef14a42 10121 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 10122 {
df58fc94 10123 macro_build (NULL, "ror", SHFT_FMT, dreg, sreg, rot);
8fc2e39e 10124 break;
82dd0097 10125 }
483fc7cd 10126 if (rot == 0)
483fc7cd 10127 {
df58fc94 10128 macro_build (NULL, "srl", SHFT_FMT, dreg, sreg, 0);
8fc2e39e 10129 break;
483fc7cd 10130 }
8fc2e39e 10131 used_at = 1;
df58fc94
RS
10132 macro_build (NULL, "srl", SHFT_FMT, AT, sreg, rot);
10133 macro_build (NULL, "sll", SHFT_FMT, dreg, sreg, (0x20 - rot) & 0x1f);
67c0d1eb 10134 macro_build (NULL, "or", "d,v,t", dreg, dreg, AT);
771c7ce4 10135 }
252b5132
RH
10136 break;
10137
252b5132
RH
10138 case M_SEQ:
10139 if (sreg == 0)
67c0d1eb 10140 macro_build (&expr1, "sltiu", "t,r,j", dreg, treg, BFD_RELOC_LO16);
252b5132 10141 else if (treg == 0)
67c0d1eb 10142 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10143 else
10144 {
67c0d1eb
RS
10145 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10146 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
252b5132 10147 }
8fc2e39e 10148 break;
252b5132
RH
10149
10150 case M_SEQ_I:
10151 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10152 {
67c0d1eb 10153 macro_build (&expr1, "sltiu", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10154 break;
252b5132
RH
10155 }
10156 if (sreg == 0)
10157 {
10158 as_warn (_("Instruction %s: result is always false"),
10159 ip->insn_mo->name);
67c0d1eb 10160 move_register (dreg, 0);
8fc2e39e 10161 break;
252b5132 10162 }
dd3cbb7e
NC
10163 if (CPU_HAS_SEQ (mips_opts.arch)
10164 && -512 <= imm_expr.X_add_number
10165 && imm_expr.X_add_number < 512)
10166 {
10167 macro_build (NULL, "seqi", "t,r,+Q", dreg, sreg,
750bdd57 10168 (int) imm_expr.X_add_number);
dd3cbb7e
NC
10169 break;
10170 }
252b5132
RH
10171 if (imm_expr.X_op == O_constant
10172 && imm_expr.X_add_number >= 0
10173 && imm_expr.X_add_number < 0x10000)
10174 {
67c0d1eb 10175 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10176 }
10177 else if (imm_expr.X_op == O_constant
10178 && imm_expr.X_add_number > -0x8000
10179 && imm_expr.X_add_number < 0)
10180 {
10181 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10182 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 10183 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 10184 }
dd3cbb7e
NC
10185 else if (CPU_HAS_SEQ (mips_opts.arch))
10186 {
10187 used_at = 1;
10188 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10189 macro_build (NULL, "seq", "d,v,t", dreg, sreg, AT);
10190 break;
10191 }
252b5132
RH
10192 else
10193 {
67c0d1eb
RS
10194 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10195 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
10196 used_at = 1;
10197 }
67c0d1eb 10198 macro_build (&expr1, "sltiu", "t,r,j", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10199 break;
252b5132
RH
10200
10201 case M_SGE: /* sreg >= treg <==> not (sreg < treg) */
10202 s = "slt";
10203 goto sge;
10204 case M_SGEU:
10205 s = "sltu";
10206 sge:
67c0d1eb
RS
10207 macro_build (NULL, s, "d,v,t", dreg, sreg, treg);
10208 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10209 break;
252b5132
RH
10210
10211 case M_SGE_I: /* sreg >= I <==> not (sreg < I) */
10212 case M_SGEU_I:
10213 if (imm_expr.X_op == O_constant
10214 && imm_expr.X_add_number >= -0x8000
10215 && imm_expr.X_add_number < 0x8000)
10216 {
67c0d1eb
RS
10217 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
10218 dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10219 }
10220 else
10221 {
67c0d1eb
RS
10222 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10223 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
10224 dreg, sreg, AT);
252b5132
RH
10225 used_at = 1;
10226 }
67c0d1eb 10227 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10228 break;
252b5132
RH
10229
10230 case M_SGT: /* sreg > treg <==> treg < sreg */
10231 s = "slt";
10232 goto sgt;
10233 case M_SGTU:
10234 s = "sltu";
10235 sgt:
67c0d1eb 10236 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
8fc2e39e 10237 break;
252b5132
RH
10238
10239 case M_SGT_I: /* sreg > I <==> I < sreg */
10240 s = "slt";
10241 goto sgti;
10242 case M_SGTU_I:
10243 s = "sltu";
10244 sgti:
8fc2e39e 10245 used_at = 1;
67c0d1eb
RS
10246 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10247 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
252b5132
RH
10248 break;
10249
2396cfb9 10250 case M_SLE: /* sreg <= treg <==> treg >= sreg <==> not (treg < sreg) */
252b5132
RH
10251 s = "slt";
10252 goto sle;
10253 case M_SLEU:
10254 s = "sltu";
10255 sle:
67c0d1eb
RS
10256 macro_build (NULL, s, "d,v,t", dreg, treg, sreg);
10257 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
8fc2e39e 10258 break;
252b5132 10259
2396cfb9 10260 case M_SLE_I: /* sreg <= I <==> I >= sreg <==> not (I < sreg) */
252b5132
RH
10261 s = "slt";
10262 goto slei;
10263 case M_SLEU_I:
10264 s = "sltu";
10265 slei:
8fc2e39e 10266 used_at = 1;
67c0d1eb
RS
10267 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10268 macro_build (NULL, s, "d,v,t", dreg, AT, sreg);
10269 macro_build (&expr1, "xori", "t,r,i", dreg, dreg, BFD_RELOC_LO16);
252b5132
RH
10270 break;
10271
10272 case M_SLT_I:
10273 if (imm_expr.X_op == O_constant
10274 && imm_expr.X_add_number >= -0x8000
10275 && imm_expr.X_add_number < 0x8000)
10276 {
67c0d1eb 10277 macro_build (&imm_expr, "slti", "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10278 break;
252b5132 10279 }
8fc2e39e 10280 used_at = 1;
67c0d1eb
RS
10281 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10282 macro_build (NULL, "slt", "d,v,t", dreg, sreg, AT);
252b5132
RH
10283 break;
10284
10285 case M_SLTU_I:
10286 if (imm_expr.X_op == O_constant
10287 && imm_expr.X_add_number >= -0x8000
10288 && imm_expr.X_add_number < 0x8000)
10289 {
67c0d1eb 10290 macro_build (&imm_expr, "sltiu", "t,r,j", dreg, sreg,
17a2f251 10291 BFD_RELOC_LO16);
8fc2e39e 10292 break;
252b5132 10293 }
8fc2e39e 10294 used_at = 1;
67c0d1eb
RS
10295 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10296 macro_build (NULL, "sltu", "d,v,t", dreg, sreg, AT);
252b5132
RH
10297 break;
10298
10299 case M_SNE:
10300 if (sreg == 0)
67c0d1eb 10301 macro_build (NULL, "sltu", "d,v,t", dreg, 0, treg);
252b5132 10302 else if (treg == 0)
67c0d1eb 10303 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
252b5132
RH
10304 else
10305 {
67c0d1eb
RS
10306 macro_build (NULL, "xor", "d,v,t", dreg, sreg, treg);
10307 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
252b5132 10308 }
8fc2e39e 10309 break;
252b5132
RH
10310
10311 case M_SNE_I:
10312 if (imm_expr.X_op == O_constant && imm_expr.X_add_number == 0)
10313 {
67c0d1eb 10314 macro_build (NULL, "sltu", "d,v,t", dreg, 0, sreg);
8fc2e39e 10315 break;
252b5132
RH
10316 }
10317 if (sreg == 0)
10318 {
10319 as_warn (_("Instruction %s: result is always true"),
10320 ip->insn_mo->name);
67c0d1eb
RS
10321 macro_build (&expr1, HAVE_32BIT_GPRS ? "addiu" : "daddiu", "t,r,j",
10322 dreg, 0, BFD_RELOC_LO16);
8fc2e39e 10323 break;
252b5132 10324 }
dd3cbb7e
NC
10325 if (CPU_HAS_SEQ (mips_opts.arch)
10326 && -512 <= imm_expr.X_add_number
10327 && imm_expr.X_add_number < 512)
10328 {
10329 macro_build (NULL, "snei", "t,r,+Q", dreg, sreg,
750bdd57 10330 (int) imm_expr.X_add_number);
dd3cbb7e
NC
10331 break;
10332 }
252b5132
RH
10333 if (imm_expr.X_op == O_constant
10334 && imm_expr.X_add_number >= 0
10335 && imm_expr.X_add_number < 0x10000)
10336 {
67c0d1eb 10337 macro_build (&imm_expr, "xori", "t,r,i", dreg, sreg, BFD_RELOC_LO16);
252b5132
RH
10338 }
10339 else if (imm_expr.X_op == O_constant
10340 && imm_expr.X_add_number > -0x8000
10341 && imm_expr.X_add_number < 0)
10342 {
10343 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10344 macro_build (&imm_expr, HAVE_32BIT_GPRS ? "addiu" : "daddiu",
17a2f251 10345 "t,r,j", dreg, sreg, BFD_RELOC_LO16);
252b5132 10346 }
dd3cbb7e
NC
10347 else if (CPU_HAS_SEQ (mips_opts.arch))
10348 {
10349 used_at = 1;
10350 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10351 macro_build (NULL, "sne", "d,v,t", dreg, sreg, AT);
10352 break;
10353 }
252b5132
RH
10354 else
10355 {
67c0d1eb
RS
10356 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10357 macro_build (NULL, "xor", "d,v,t", dreg, sreg, AT);
252b5132
RH
10358 used_at = 1;
10359 }
67c0d1eb 10360 macro_build (NULL, "sltu", "d,v,t", dreg, 0, dreg);
8fc2e39e 10361 break;
252b5132 10362
df58fc94
RS
10363 case M_SUB_I:
10364 s = "addi";
10365 s2 = "sub";
10366 goto do_subi;
10367 case M_SUBU_I:
10368 s = "addiu";
10369 s2 = "subu";
10370 goto do_subi;
252b5132
RH
10371 case M_DSUB_I:
10372 dbl = 1;
df58fc94
RS
10373 s = "daddi";
10374 s2 = "dsub";
10375 if (!mips_opts.micromips)
10376 goto do_subi;
252b5132 10377 if (imm_expr.X_op == O_constant
df58fc94
RS
10378 && imm_expr.X_add_number > -0x200
10379 && imm_expr.X_add_number <= 0x200)
252b5132 10380 {
df58fc94 10381 macro_build (NULL, s, "t,r,.", dreg, sreg, -imm_expr.X_add_number);
8fc2e39e 10382 break;
252b5132 10383 }
df58fc94 10384 goto do_subi_i;
252b5132
RH
10385 case M_DSUBU_I:
10386 dbl = 1;
df58fc94
RS
10387 s = "daddiu";
10388 s2 = "dsubu";
10389 do_subi:
252b5132
RH
10390 if (imm_expr.X_op == O_constant
10391 && imm_expr.X_add_number > -0x8000
10392 && imm_expr.X_add_number <= 0x8000)
10393 {
10394 imm_expr.X_add_number = -imm_expr.X_add_number;
df58fc94 10395 macro_build (&imm_expr, s, "t,r,j", dreg, sreg, BFD_RELOC_LO16);
8fc2e39e 10396 break;
252b5132 10397 }
df58fc94 10398 do_subi_i:
8fc2e39e 10399 used_at = 1;
67c0d1eb 10400 load_register (AT, &imm_expr, dbl);
df58fc94 10401 macro_build (NULL, s2, "d,v,t", dreg, sreg, AT);
252b5132
RH
10402 break;
10403
10404 case M_TEQ_I:
10405 s = "teq";
10406 goto trap;
10407 case M_TGE_I:
10408 s = "tge";
10409 goto trap;
10410 case M_TGEU_I:
10411 s = "tgeu";
10412 goto trap;
10413 case M_TLT_I:
10414 s = "tlt";
10415 goto trap;
10416 case M_TLTU_I:
10417 s = "tltu";
10418 goto trap;
10419 case M_TNE_I:
10420 s = "tne";
10421 trap:
8fc2e39e 10422 used_at = 1;
67c0d1eb
RS
10423 load_register (AT, &imm_expr, HAVE_64BIT_GPRS);
10424 macro_build (NULL, s, "s,t", sreg, AT);
252b5132
RH
10425 break;
10426
252b5132 10427 case M_TRUNCWS:
43841e91 10428 case M_TRUNCWD:
df58fc94 10429 gas_assert (!mips_opts.micromips);
0aa27725 10430 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 10431 used_at = 1;
252b5132
RH
10432 sreg = (ip->insn_opcode >> 11) & 0x1f; /* floating reg */
10433 dreg = (ip->insn_opcode >> 06) & 0x1f; /* floating reg */
10434
10435 /*
10436 * Is the double cfc1 instruction a bug in the mips assembler;
10437 * or is there a reason for it?
10438 */
7d10b47d 10439 start_noreorder ();
67c0d1eb
RS
10440 macro_build (NULL, "cfc1", "t,G", treg, RA);
10441 macro_build (NULL, "cfc1", "t,G", treg, RA);
10442 macro_build (NULL, "nop", "");
252b5132 10443 expr1.X_add_number = 3;
67c0d1eb 10444 macro_build (&expr1, "ori", "t,r,i", AT, treg, BFD_RELOC_LO16);
252b5132 10445 expr1.X_add_number = 2;
67c0d1eb
RS
10446 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
10447 macro_build (NULL, "ctc1", "t,G", AT, RA);
10448 macro_build (NULL, "nop", "");
10449 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
10450 dreg, sreg);
10451 macro_build (NULL, "ctc1", "t,G", treg, RA);
10452 macro_build (NULL, "nop", "");
7d10b47d 10453 end_noreorder ();
252b5132
RH
10454 break;
10455
df58fc94
RS
10456 case M_ULH_A:
10457 ab = 1;
252b5132
RH
10458 case M_ULH:
10459 s = "lb";
df58fc94
RS
10460 s2 = "lbu";
10461 off = 1;
10462 goto uld_st;
10463 case M_ULHU_A:
10464 ab = 1;
252b5132
RH
10465 case M_ULHU:
10466 s = "lbu";
df58fc94
RS
10467 s2 = "lbu";
10468 off = 1;
10469 goto uld_st;
10470 case M_ULW_A:
10471 ab = 1;
10472 case M_ULW:
10473 s = "lwl";
10474 s2 = "lwr";
7f3c4072 10475 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10476 off = 3;
10477 goto uld_st;
10478 case M_ULD_A:
10479 ab = 1;
252b5132
RH
10480 case M_ULD:
10481 s = "ldl";
10482 s2 = "ldr";
7f3c4072 10483 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10484 off = 7;
df58fc94
RS
10485 goto uld_st;
10486 case M_USH_A:
10487 ab = 1;
10488 case M_USH:
10489 s = "sb";
10490 s2 = "sb";
10491 off = 1;
10492 ust = 1;
10493 goto uld_st;
10494 case M_USW_A:
10495 ab = 1;
10496 case M_USW:
10497 s = "swl";
10498 s2 = "swr";
7f3c4072 10499 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 10500 off = 3;
df58fc94
RS
10501 ust = 1;
10502 goto uld_st;
10503 case M_USD_A:
10504 ab = 1;
10505 case M_USD:
10506 s = "sdl";
10507 s2 = "sdr";
7f3c4072 10508 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
10509 off = 7;
10510 ust = 1;
10511
10512 uld_st:
10513 if (!ab && offset_expr.X_add_number >= 0x8000 - off)
f71d0d44 10514 as_bad (_("Operand overflow"));
df58fc94
RS
10515
10516 ep = &offset_expr;
10517 expr1.X_add_number = 0;
10518 if (ab)
10519 {
10520 used_at = 1;
10521 tempreg = AT;
10522 load_address (tempreg, ep, &used_at);
10523 if (breg != 0)
10524 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
10525 tempreg, tempreg, breg);
10526 breg = tempreg;
10527 tempreg = treg;
10528 ep = &expr1;
10529 }
7f3c4072 10530 else if (offbits == 12
df58fc94
RS
10531 && (offset_expr.X_op != O_constant
10532 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number)
10533 || !IS_SEXT_12BIT_NUM (offset_expr.X_add_number + off)))
10534 {
10535 used_at = 1;
10536 tempreg = AT;
10537 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg,
10538 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
10539 breg = tempreg;
10540 tempreg = treg;
10541 ep = &expr1;
10542 }
10543 else if (!ust && treg == breg)
8fc2e39e
TS
10544 {
10545 used_at = 1;
10546 tempreg = AT;
10547 }
252b5132 10548 else
df58fc94 10549 tempreg = treg;
af22f5b2 10550
df58fc94
RS
10551 if (off == 1)
10552 goto ulh_sh;
252b5132 10553
90ecf173 10554 if (!target_big_endian)
df58fc94 10555 ep->X_add_number += off;
7f3c4072 10556 if (offbits != 12)
df58fc94 10557 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10558 else
df58fc94
RS
10559 macro_build (NULL, s, "t,~(b)",
10560 tempreg, (unsigned long) ep->X_add_number, breg);
10561
90ecf173 10562 if (!target_big_endian)
df58fc94 10563 ep->X_add_number -= off;
252b5132 10564 else
df58fc94 10565 ep->X_add_number += off;
7f3c4072 10566 if (offbits != 12)
df58fc94
RS
10567 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10568 else
10569 macro_build (NULL, s2, "t,~(b)",
10570 tempreg, (unsigned long) ep->X_add_number, breg);
252b5132 10571
df58fc94
RS
10572 /* If necessary, move the result in tempreg to the final destination. */
10573 if (!ust && treg != tempreg)
10574 {
10575 /* Protect second load's delay slot. */
10576 load_delay_nop ();
10577 move_register (treg, tempreg);
10578 }
8fc2e39e 10579 break;
252b5132 10580
df58fc94 10581 ulh_sh:
d6bc6245 10582 used_at = 1;
df58fc94
RS
10583 if (target_big_endian == ust)
10584 ep->X_add_number += off;
10585 tempreg = ust || ab ? treg : AT;
10586 macro_build (ep, s, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
10587
10588 /* For halfword transfers we need a temporary register to shuffle
10589 bytes. Unfortunately for M_USH_A we have none available before
10590 the next store as AT holds the base address. We deal with this
10591 case by clobbering TREG and then restoring it as with ULH. */
10592 tempreg = ust == ab ? treg : AT;
10593 if (ust)
10594 macro_build (NULL, "srl", SHFT_FMT, tempreg, treg, 8);
10595
10596 if (target_big_endian == ust)
10597 ep->X_add_number -= off;
252b5132 10598 else
df58fc94
RS
10599 ep->X_add_number += off;
10600 macro_build (ep, s2, "t,o(b)", tempreg, BFD_RELOC_LO16, breg);
252b5132 10601
df58fc94
RS
10602 /* For M_USH_A re-retrieve the LSB. */
10603 if (ust && ab)
10604 {
10605 if (target_big_endian)
10606 ep->X_add_number += off;
10607 else
10608 ep->X_add_number -= off;
10609 macro_build (&expr1, "lbu", "t,o(b)", AT, BFD_RELOC_LO16, AT);
10610 }
10611 /* For ULH and M_USH_A OR the LSB in. */
10612 if (!ust || ab)
10613 {
10614 tempreg = !ab ? AT : treg;
10615 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
10616 macro_build (NULL, "or", "d,v,t", treg, treg, AT);
10617 }
252b5132
RH
10618 break;
10619
10620 default:
10621 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 10622 are added dynamically. */
252b5132
RH
10623 as_bad (_("Macro %s not implemented yet"), ip->insn_mo->name);
10624 break;
10625 }
741fe287 10626 if (!mips_opts.at && used_at)
8fc2e39e 10627 as_bad (_("Macro used $at after \".set noat\""));
252b5132
RH
10628}
10629
10630/* Implement macros in mips16 mode. */
10631
10632static void
17a2f251 10633mips16_macro (struct mips_cl_insn *ip)
252b5132
RH
10634{
10635 int mask;
10636 int xreg, yreg, zreg, tmp;
252b5132
RH
10637 expressionS expr1;
10638 int dbl;
10639 const char *s, *s2, *s3;
10640
10641 mask = ip->insn_mo->mask;
10642
bf12938e
RS
10643 xreg = MIPS16_EXTRACT_OPERAND (RX, *ip);
10644 yreg = MIPS16_EXTRACT_OPERAND (RY, *ip);
10645 zreg = MIPS16_EXTRACT_OPERAND (RZ, *ip);
252b5132 10646
252b5132
RH
10647 expr1.X_op = O_constant;
10648 expr1.X_op_symbol = NULL;
10649 expr1.X_add_symbol = NULL;
10650 expr1.X_add_number = 1;
10651
10652 dbl = 0;
10653
10654 switch (mask)
10655 {
10656 default:
b37df7c4 10657 abort ();
252b5132
RH
10658
10659 case M_DDIV_3:
10660 dbl = 1;
10661 case M_DIV_3:
10662 s = "mflo";
10663 goto do_div3;
10664 case M_DREM_3:
10665 dbl = 1;
10666 case M_REM_3:
10667 s = "mfhi";
10668 do_div3:
7d10b47d 10669 start_noreorder ();
67c0d1eb 10670 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", xreg, yreg);
252b5132 10671 expr1.X_add_number = 2;
67c0d1eb
RS
10672 macro_build (&expr1, "bnez", "x,p", yreg);
10673 macro_build (NULL, "break", "6", 7);
bdaaa2e1 10674
252b5132
RH
10675 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
10676 since that causes an overflow. We should do that as well,
10677 but I don't see how to do the comparisons without a temporary
10678 register. */
7d10b47d 10679 end_noreorder ();
67c0d1eb 10680 macro_build (NULL, s, "x", zreg);
252b5132
RH
10681 break;
10682
10683 case M_DIVU_3:
10684 s = "divu";
10685 s2 = "mflo";
10686 goto do_divu3;
10687 case M_REMU_3:
10688 s = "divu";
10689 s2 = "mfhi";
10690 goto do_divu3;
10691 case M_DDIVU_3:
10692 s = "ddivu";
10693 s2 = "mflo";
10694 goto do_divu3;
10695 case M_DREMU_3:
10696 s = "ddivu";
10697 s2 = "mfhi";
10698 do_divu3:
7d10b47d 10699 start_noreorder ();
67c0d1eb 10700 macro_build (NULL, s, "0,x,y", xreg, yreg);
252b5132 10701 expr1.X_add_number = 2;
67c0d1eb
RS
10702 macro_build (&expr1, "bnez", "x,p", yreg);
10703 macro_build (NULL, "break", "6", 7);
7d10b47d 10704 end_noreorder ();
67c0d1eb 10705 macro_build (NULL, s2, "x", zreg);
252b5132
RH
10706 break;
10707
10708 case M_DMUL:
10709 dbl = 1;
10710 case M_MUL:
67c0d1eb
RS
10711 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", xreg, yreg);
10712 macro_build (NULL, "mflo", "x", zreg);
8fc2e39e 10713 break;
252b5132
RH
10714
10715 case M_DSUBU_I:
10716 dbl = 1;
10717 goto do_subu;
10718 case M_SUBU_I:
10719 do_subu:
10720 if (imm_expr.X_op != O_constant)
10721 as_bad (_("Unsupported large constant"));
10722 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10723 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", yreg, xreg);
252b5132
RH
10724 break;
10725
10726 case M_SUBU_I_2:
10727 if (imm_expr.X_op != O_constant)
10728 as_bad (_("Unsupported large constant"));
10729 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10730 macro_build (&imm_expr, "addiu", "x,k", xreg);
252b5132
RH
10731 break;
10732
10733 case M_DSUBU_I_2:
10734 if (imm_expr.X_op != O_constant)
10735 as_bad (_("Unsupported large constant"));
10736 imm_expr.X_add_number = -imm_expr.X_add_number;
67c0d1eb 10737 macro_build (&imm_expr, "daddiu", "y,j", yreg);
252b5132
RH
10738 break;
10739
10740 case M_BEQ:
10741 s = "cmp";
10742 s2 = "bteqz";
10743 goto do_branch;
10744 case M_BNE:
10745 s = "cmp";
10746 s2 = "btnez";
10747 goto do_branch;
10748 case M_BLT:
10749 s = "slt";
10750 s2 = "btnez";
10751 goto do_branch;
10752 case M_BLTU:
10753 s = "sltu";
10754 s2 = "btnez";
10755 goto do_branch;
10756 case M_BLE:
10757 s = "slt";
10758 s2 = "bteqz";
10759 goto do_reverse_branch;
10760 case M_BLEU:
10761 s = "sltu";
10762 s2 = "bteqz";
10763 goto do_reverse_branch;
10764 case M_BGE:
10765 s = "slt";
10766 s2 = "bteqz";
10767 goto do_branch;
10768 case M_BGEU:
10769 s = "sltu";
10770 s2 = "bteqz";
10771 goto do_branch;
10772 case M_BGT:
10773 s = "slt";
10774 s2 = "btnez";
10775 goto do_reverse_branch;
10776 case M_BGTU:
10777 s = "sltu";
10778 s2 = "btnez";
10779
10780 do_reverse_branch:
10781 tmp = xreg;
10782 xreg = yreg;
10783 yreg = tmp;
10784
10785 do_branch:
67c0d1eb
RS
10786 macro_build (NULL, s, "x,y", xreg, yreg);
10787 macro_build (&offset_expr, s2, "p");
252b5132
RH
10788 break;
10789
10790 case M_BEQ_I:
10791 s = "cmpi";
10792 s2 = "bteqz";
10793 s3 = "x,U";
10794 goto do_branch_i;
10795 case M_BNE_I:
10796 s = "cmpi";
10797 s2 = "btnez";
10798 s3 = "x,U";
10799 goto do_branch_i;
10800 case M_BLT_I:
10801 s = "slti";
10802 s2 = "btnez";
10803 s3 = "x,8";
10804 goto do_branch_i;
10805 case M_BLTU_I:
10806 s = "sltiu";
10807 s2 = "btnez";
10808 s3 = "x,8";
10809 goto do_branch_i;
10810 case M_BLE_I:
10811 s = "slti";
10812 s2 = "btnez";
10813 s3 = "x,8";
10814 goto do_addone_branch_i;
10815 case M_BLEU_I:
10816 s = "sltiu";
10817 s2 = "btnez";
10818 s3 = "x,8";
10819 goto do_addone_branch_i;
10820 case M_BGE_I:
10821 s = "slti";
10822 s2 = "bteqz";
10823 s3 = "x,8";
10824 goto do_branch_i;
10825 case M_BGEU_I:
10826 s = "sltiu";
10827 s2 = "bteqz";
10828 s3 = "x,8";
10829 goto do_branch_i;
10830 case M_BGT_I:
10831 s = "slti";
10832 s2 = "bteqz";
10833 s3 = "x,8";
10834 goto do_addone_branch_i;
10835 case M_BGTU_I:
10836 s = "sltiu";
10837 s2 = "bteqz";
10838 s3 = "x,8";
10839
10840 do_addone_branch_i:
10841 if (imm_expr.X_op != O_constant)
10842 as_bad (_("Unsupported large constant"));
10843 ++imm_expr.X_add_number;
10844
10845 do_branch_i:
67c0d1eb
RS
10846 macro_build (&imm_expr, s, s3, xreg);
10847 macro_build (&offset_expr, s2, "p");
252b5132
RH
10848 break;
10849
10850 case M_ABS:
10851 expr1.X_add_number = 0;
67c0d1eb 10852 macro_build (&expr1, "slti", "x,8", yreg);
252b5132 10853 if (xreg != yreg)
67c0d1eb 10854 move_register (xreg, yreg);
252b5132 10855 expr1.X_add_number = 2;
67c0d1eb
RS
10856 macro_build (&expr1, "bteqz", "p");
10857 macro_build (NULL, "neg", "x,w", xreg, xreg);
252b5132
RH
10858 }
10859}
10860
10861/* For consistency checking, verify that all bits are specified either
10862 by the match/mask part of the instruction definition, or by the
10863 operand list. */
10864static int
17a2f251 10865validate_mips_insn (const struct mips_opcode *opc)
252b5132
RH
10866{
10867 const char *p = opc->args;
10868 char c;
10869 unsigned long used_bits = opc->mask;
10870
10871 if ((used_bits & opc->match) != opc->match)
10872 {
10873 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
10874 opc->name, opc->args);
10875 return 0;
10876 }
10877#define USE_BITS(mask,shift) (used_bits |= ((mask) << (shift)))
10878 while (*p)
10879 switch (c = *p++)
10880 {
10881 case ',': break;
10882 case '(': break;
10883 case ')': break;
af7ee8bf
CD
10884 case '+':
10885 switch (c = *p++)
10886 {
9bcd4f99
TS
10887 case '1': USE_BITS (OP_MASK_UDI1, OP_SH_UDI1); break;
10888 case '2': USE_BITS (OP_MASK_UDI2, OP_SH_UDI2); break;
10889 case '3': USE_BITS (OP_MASK_UDI3, OP_SH_UDI3); break;
10890 case '4': USE_BITS (OP_MASK_UDI4, OP_SH_UDI4); break;
af7ee8bf
CD
10891 case 'A': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10892 case 'B': USE_BITS (OP_MASK_INSMSB, OP_SH_INSMSB); break;
10893 case 'C': USE_BITS (OP_MASK_EXTMSBD, OP_SH_EXTMSBD); 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 10900 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
bb35fb24
NC
10901 case 'x': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10902 case 'X': USE_BITS (OP_MASK_BBITIND, OP_SH_BBITIND); break;
10903 case 'p': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
10904 case 'P': USE_BITS (OP_MASK_CINSPOS, OP_SH_CINSPOS); break;
dd3cbb7e 10905 case 'Q': USE_BITS (OP_MASK_SEQI, OP_SH_SEQI); break;
bb35fb24
NC
10906 case 's': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
10907 case 'S': USE_BITS (OP_MASK_CINSLM1, OP_SH_CINSLM1); break;
98675402
RS
10908 case 'z': USE_BITS (OP_MASK_RZ, OP_SH_RZ); break;
10909 case 'Z': USE_BITS (OP_MASK_FZ, OP_SH_FZ); break;
10910 case 'a': USE_BITS (OP_MASK_OFFSET_A, OP_SH_OFFSET_A); break;
10911 case 'b': USE_BITS (OP_MASK_OFFSET_B, OP_SH_OFFSET_B); break;
10912 case 'c': USE_BITS (OP_MASK_OFFSET_C, OP_SH_OFFSET_C); break;
27c5c572 10913 case 'i': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
7f3c4072 10914 case 'j': USE_BITS (OP_MASK_EVAOFFSET, OP_SH_EVAOFFSET); break;
bb35fb24 10915
af7ee8bf
CD
10916 default:
10917 as_bad (_("internal: bad mips opcode (unknown extension operand type `+%c'): %s %s"),
10918 c, opc->name, opc->args);
10919 return 0;
10920 }
10921 break;
252b5132
RH
10922 case '<': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10923 case '>': USE_BITS (OP_MASK_SHAMT, OP_SH_SHAMT); break;
10924 case 'A': break;
4372b673 10925 case 'B': USE_BITS (OP_MASK_CODE20, OP_SH_CODE20); break;
252b5132
RH
10926 case 'C': USE_BITS (OP_MASK_COPZ, OP_SH_COPZ); break;
10927 case 'D': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10928 case 'E': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10929 case 'F': break;
10930 case 'G': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
156c2f8b 10931 case 'H': USE_BITS (OP_MASK_SEL, OP_SH_SEL); break;
252b5132 10932 case 'I': break;
e972090a 10933 case 'J': USE_BITS (OP_MASK_CODE19, OP_SH_CODE19); break;
af7ee8bf 10934 case 'K': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10935 case 'L': break;
10936 case 'M': USE_BITS (OP_MASK_CCC, OP_SH_CCC); break;
10937 case 'N': USE_BITS (OP_MASK_BCC, OP_SH_BCC); break;
deec1734
CD
10938 case 'O': USE_BITS (OP_MASK_ALN, OP_SH_ALN); break;
10939 case 'Q': USE_BITS (OP_MASK_VSEL, OP_SH_VSEL);
10940 USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10941 case 'R': USE_BITS (OP_MASK_FR, OP_SH_FR); break;
10942 case 'S': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10943 case 'T': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
10944 case 'V': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10945 case 'W': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
deec1734
CD
10946 case 'X': USE_BITS (OP_MASK_FD, OP_SH_FD); break;
10947 case 'Y': USE_BITS (OP_MASK_FS, OP_SH_FS); break;
10948 case 'Z': USE_BITS (OP_MASK_FT, OP_SH_FT); break;
252b5132
RH
10949 case 'a': USE_BITS (OP_MASK_TARGET, OP_SH_TARGET); break;
10950 case 'b': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10951 case 'c': USE_BITS (OP_MASK_CODE, OP_SH_CODE); break;
10952 case 'd': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
10953 case 'f': break;
10954 case 'h': USE_BITS (OP_MASK_PREFX, OP_SH_PREFX); break;
10955 case 'i': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10956 case 'j': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10957 case 'k': USE_BITS (OP_MASK_CACHE, OP_SH_CACHE); break;
10958 case 'l': break;
10959 case 'o': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10960 case 'p': USE_BITS (OP_MASK_DELTA, OP_SH_DELTA); break;
10961 case 'q': USE_BITS (OP_MASK_CODE2, OP_SH_CODE2); break;
10962 case 'r': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10963 case 's': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10964 case 't': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10965 case 'u': USE_BITS (OP_MASK_IMMEDIATE, OP_SH_IMMEDIATE); break;
10966 case 'v': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10967 case 'w': USE_BITS (OP_MASK_RT, OP_SH_RT); break;
10968 case 'x': break;
10969 case 'z': break;
10970 case 'P': USE_BITS (OP_MASK_PERFREG, OP_SH_PERFREG); break;
4372b673
NC
10971 case 'U': USE_BITS (OP_MASK_RD, OP_SH_RD);
10972 USE_BITS (OP_MASK_RT, OP_SH_RT); break;
60b63b72
RS
10973 case 'e': USE_BITS (OP_MASK_VECBYTE, OP_SH_VECBYTE); break;
10974 case '%': USE_BITS (OP_MASK_VECALIGN, OP_SH_VECALIGN); break;
18870af7 10975 case '1': USE_BITS (OP_MASK_STYPE, OP_SH_STYPE); break;
8b082fb1 10976 case '2': USE_BITS (OP_MASK_BP, OP_SH_BP); break;
74cd071d
CF
10977 case '3': USE_BITS (OP_MASK_SA3, OP_SH_SA3); break;
10978 case '4': USE_BITS (OP_MASK_SA4, OP_SH_SA4); break;
10979 case '5': USE_BITS (OP_MASK_IMM8, OP_SH_IMM8); break;
10980 case '6': USE_BITS (OP_MASK_RS, OP_SH_RS); break;
10981 case '7': USE_BITS (OP_MASK_DSPACC, OP_SH_DSPACC); break;
10982 case '8': USE_BITS (OP_MASK_WRDSP, OP_SH_WRDSP); break;
10983 case '9': USE_BITS (OP_MASK_DSPACC_S, OP_SH_DSPACC_S);break;
10984 case '0': USE_BITS (OP_MASK_DSPSFT, OP_SH_DSPSFT); break;
10985 case '\'': USE_BITS (OP_MASK_RDDSP, OP_SH_RDDSP); break;
10986 case ':': USE_BITS (OP_MASK_DSPSFT_7, OP_SH_DSPSFT_7);break;
10987 case '@': USE_BITS (OP_MASK_IMM10, OP_SH_IMM10); break;
ef2e4d86
CF
10988 case '!': USE_BITS (OP_MASK_MT_U, OP_SH_MT_U); break;
10989 case '$': USE_BITS (OP_MASK_MT_H, OP_SH_MT_H); break;
10990 case '*': USE_BITS (OP_MASK_MTACC_T, OP_SH_MTACC_T); break;
10991 case '&': USE_BITS (OP_MASK_MTACC_D, OP_SH_MTACC_D); break;
dec0624d
MR
10992 case '\\': USE_BITS (OP_MASK_3BITPOS, OP_SH_3BITPOS); break;
10993 case '~': USE_BITS (OP_MASK_OFFSET12, OP_SH_OFFSET12); break;
ef2e4d86 10994 case 'g': USE_BITS (OP_MASK_RD, OP_SH_RD); break;
252b5132
RH
10995 default:
10996 as_bad (_("internal: bad mips opcode (unknown operand type `%c'): %s %s"),
10997 c, opc->name, opc->args);
10998 return 0;
10999 }
11000#undef USE_BITS
11001 if (used_bits != 0xffffffff)
11002 {
11003 as_bad (_("internal: bad mips opcode (bits 0x%lx undefined): %s %s"),
11004 ~used_bits & 0xffffffff, opc->name, opc->args);
11005 return 0;
11006 }
11007 return 1;
11008}
11009
df58fc94
RS
11010/* For consistency checking, verify that the length implied matches the
11011 major opcode and that all bits are specified either by the match/mask
11012 part of the instruction definition, or by the operand list. */
11013
11014static int
11015validate_micromips_insn (const struct mips_opcode *opc)
11016{
11017 unsigned long match = opc->match;
11018 unsigned long mask = opc->mask;
11019 const char *p = opc->args;
11020 unsigned long insn_bits;
11021 unsigned long used_bits;
11022 unsigned long major;
11023 unsigned int length;
11024 char e;
11025 char c;
11026
11027 if ((mask & match) != match)
11028 {
11029 as_bad (_("Internal error: bad microMIPS opcode (mask error): %s %s"),
11030 opc->name, opc->args);
11031 return 0;
11032 }
11033 length = micromips_insn_length (opc);
11034 if (length != 2 && length != 4)
11035 {
11036 as_bad (_("Internal error: bad microMIPS opcode (incorrect length: %u): "
11037 "%s %s"), length, opc->name, opc->args);
11038 return 0;
11039 }
11040 major = match >> (10 + 8 * (length - 2));
11041 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
11042 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
11043 {
11044 as_bad (_("Internal error: bad microMIPS opcode "
11045 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
11046 return 0;
11047 }
11048
11049 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
11050 insn_bits = 1 << 4 * length;
11051 insn_bits <<= 4 * length;
11052 insn_bits -= 1;
11053 used_bits = mask;
11054#define USE_BITS(field) \
11055 (used_bits |= MICROMIPSOP_MASK_##field << MICROMIPSOP_SH_##field)
11056 while (*p)
11057 switch (c = *p++)
11058 {
11059 case ',': break;
11060 case '(': break;
11061 case ')': break;
11062 case '+':
11063 e = c;
11064 switch (c = *p++)
11065 {
11066 case 'A': USE_BITS (EXTLSB); break;
11067 case 'B': USE_BITS (INSMSB); break;
11068 case 'C': USE_BITS (EXTMSBD); break;
df58fc94
RS
11069 case 'E': USE_BITS (EXTLSB); break;
11070 case 'F': USE_BITS (INSMSB); break;
11071 case 'G': USE_BITS (EXTMSBD); break;
11072 case 'H': USE_BITS (EXTMSBD); break;
27c5c572 11073 case 'i': USE_BITS (TARGET); break;
7f3c4072 11074 case 'j': USE_BITS (EVAOFFSET); break;
df58fc94
RS
11075 default:
11076 as_bad (_("Internal error: bad mips opcode "
11077 "(unknown extension operand type `%c%c'): %s %s"),
11078 e, c, opc->name, opc->args);
11079 return 0;
11080 }
11081 break;
11082 case 'm':
11083 e = c;
11084 switch (c = *p++)
11085 {
11086 case 'A': USE_BITS (IMMA); break;
11087 case 'B': USE_BITS (IMMB); break;
11088 case 'C': USE_BITS (IMMC); break;
11089 case 'D': USE_BITS (IMMD); break;
11090 case 'E': USE_BITS (IMME); break;
11091 case 'F': USE_BITS (IMMF); break;
11092 case 'G': USE_BITS (IMMG); break;
11093 case 'H': USE_BITS (IMMH); break;
11094 case 'I': USE_BITS (IMMI); break;
11095 case 'J': USE_BITS (IMMJ); break;
11096 case 'L': USE_BITS (IMML); break;
11097 case 'M': USE_BITS (IMMM); break;
11098 case 'N': USE_BITS (IMMN); break;
11099 case 'O': USE_BITS (IMMO); break;
11100 case 'P': USE_BITS (IMMP); break;
11101 case 'Q': USE_BITS (IMMQ); break;
11102 case 'U': USE_BITS (IMMU); break;
11103 case 'W': USE_BITS (IMMW); break;
11104 case 'X': USE_BITS (IMMX); break;
11105 case 'Y': USE_BITS (IMMY); break;
11106 case 'Z': break;
11107 case 'a': break;
11108 case 'b': USE_BITS (MB); break;
11109 case 'c': USE_BITS (MC); break;
11110 case 'd': USE_BITS (MD); break;
11111 case 'e': USE_BITS (ME); break;
11112 case 'f': USE_BITS (MF); break;
11113 case 'g': USE_BITS (MG); break;
11114 case 'h': USE_BITS (MH); break;
df58fc94
RS
11115 case 'j': USE_BITS (MJ); break;
11116 case 'l': USE_BITS (ML); break;
11117 case 'm': USE_BITS (MM); break;
11118 case 'n': USE_BITS (MN); break;
11119 case 'p': USE_BITS (MP); break;
11120 case 'q': USE_BITS (MQ); break;
11121 case 'r': break;
11122 case 's': break;
11123 case 't': break;
11124 case 'x': break;
11125 case 'y': break;
11126 case 'z': break;
11127 default:
11128 as_bad (_("Internal error: bad mips opcode "
11129 "(unknown extension operand type `%c%c'): %s %s"),
11130 e, c, opc->name, opc->args);
11131 return 0;
11132 }
11133 break;
11134 case '.': USE_BITS (OFFSET10); break;
11135 case '1': USE_BITS (STYPE); break;
03f66e8a
MR
11136 case '2': USE_BITS (BP); break;
11137 case '3': USE_BITS (SA3); break;
11138 case '4': USE_BITS (SA4); break;
11139 case '5': USE_BITS (IMM8); break;
11140 case '6': USE_BITS (RS); break;
11141 case '7': USE_BITS (DSPACC); break;
11142 case '8': USE_BITS (WRDSP); break;
11143 case '0': USE_BITS (DSPSFT); break;
df58fc94
RS
11144 case '<': USE_BITS (SHAMT); break;
11145 case '>': USE_BITS (SHAMT); break;
03f66e8a 11146 case '@': USE_BITS (IMM10); break;
df58fc94
RS
11147 case 'B': USE_BITS (CODE10); break;
11148 case 'C': USE_BITS (COPZ); break;
11149 case 'D': USE_BITS (FD); break;
11150 case 'E': USE_BITS (RT); break;
11151 case 'G': USE_BITS (RS); break;
444d75be 11152 case 'H': USE_BITS (SEL); break;
df58fc94
RS
11153 case 'K': USE_BITS (RS); break;
11154 case 'M': USE_BITS (CCC); break;
11155 case 'N': USE_BITS (BCC); break;
11156 case 'R': USE_BITS (FR); break;
11157 case 'S': USE_BITS (FS); break;
11158 case 'T': USE_BITS (FT); break;
11159 case 'V': USE_BITS (FS); break;
dec0624d 11160 case '\\': USE_BITS (3BITPOS); break;
03f66e8a 11161 case '^': USE_BITS (RD); break;
df58fc94
RS
11162 case 'a': USE_BITS (TARGET); break;
11163 case 'b': USE_BITS (RS); break;
11164 case 'c': USE_BITS (CODE); break;
11165 case 'd': USE_BITS (RD); break;
11166 case 'h': USE_BITS (PREFX); break;
11167 case 'i': USE_BITS (IMMEDIATE); break;
11168 case 'j': USE_BITS (DELTA); break;
11169 case 'k': USE_BITS (CACHE); break;
11170 case 'n': USE_BITS (RT); break;
11171 case 'o': USE_BITS (DELTA); break;
11172 case 'p': USE_BITS (DELTA); break;
11173 case 'q': USE_BITS (CODE2); break;
11174 case 'r': USE_BITS (RS); break;
11175 case 's': USE_BITS (RS); break;
11176 case 't': USE_BITS (RT); break;
11177 case 'u': USE_BITS (IMMEDIATE); break;
11178 case 'v': USE_BITS (RS); break;
11179 case 'w': USE_BITS (RT); break;
11180 case 'y': USE_BITS (RS3); break;
11181 case 'z': break;
11182 case '|': USE_BITS (TRAP); break;
11183 case '~': USE_BITS (OFFSET12); break;
11184 default:
11185 as_bad (_("Internal error: bad microMIPS opcode "
11186 "(unknown operand type `%c'): %s %s"),
11187 c, opc->name, opc->args);
11188 return 0;
11189 }
11190#undef USE_BITS
11191 if (used_bits != insn_bits)
11192 {
11193 if (~used_bits & insn_bits)
11194 as_bad (_("Internal error: bad microMIPS opcode "
11195 "(bits 0x%lx undefined): %s %s"),
11196 ~used_bits & insn_bits, opc->name, opc->args);
11197 if (used_bits & ~insn_bits)
11198 as_bad (_("Internal error: bad microMIPS opcode "
11199 "(bits 0x%lx defined): %s %s"),
11200 used_bits & ~insn_bits, opc->name, opc->args);
11201 return 0;
11202 }
11203 return 1;
11204}
11205
9bcd4f99
TS
11206/* UDI immediates. */
11207struct mips_immed {
11208 char type;
11209 unsigned int shift;
11210 unsigned long mask;
11211 const char * desc;
11212};
11213
11214static const struct mips_immed mips_immed[] = {
11215 { '1', OP_SH_UDI1, OP_MASK_UDI1, 0},
11216 { '2', OP_SH_UDI2, OP_MASK_UDI2, 0},
11217 { '3', OP_SH_UDI3, OP_MASK_UDI3, 0},
11218 { '4', OP_SH_UDI4, OP_MASK_UDI4, 0},
11219 { 0,0,0,0 }
11220};
11221
7455baf8
TS
11222/* Check whether an odd floating-point register is allowed. */
11223static int
11224mips_oddfpreg_ok (const struct mips_opcode *insn, int argnum)
11225{
11226 const char *s = insn->name;
11227
11228 if (insn->pinfo == INSN_MACRO)
11229 /* Let a macro pass, we'll catch it later when it is expanded. */
11230 return 1;
11231
e407c74b 11232 if (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa) || (mips_opts.arch == CPU_R5900))
7455baf8
TS
11233 {
11234 /* Allow odd registers for single-precision ops. */
11235 switch (insn->pinfo & (FP_S | FP_D))
11236 {
11237 case FP_S:
11238 case 0:
11239 return 1; /* both single precision - ok */
11240 case FP_D:
11241 return 0; /* both double precision - fail */
11242 default:
11243 break;
11244 }
11245
11246 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
11247 s = strchr (insn->name, '.');
11248 if (argnum == 2)
11249 s = s != NULL ? strchr (s + 1, '.') : NULL;
11250 return (s != NULL && (s[1] == 'w' || s[1] == 's'));
11251 }
11252
11253 /* Single-precision coprocessor loads and moves are OK too. */
11254 if ((insn->pinfo & FP_S)
11255 && (insn->pinfo & (INSN_COPROC_MEMORY_DELAY | INSN_STORE_MEMORY
11256 | INSN_LOAD_COPROC_DELAY | INSN_COPROC_MOVE_DELAY)))
11257 return 1;
11258
11259 return 0;
11260}
11261
df58fc94
RS
11262/* Check if EXPR is a constant between MIN (inclusive) and MAX (exclusive)
11263 taking bits from BIT up. */
11264static int
11265expr_const_in_range (expressionS *ep, offsetT min, offsetT max, int bit)
11266{
11267 return (ep->X_op == O_constant
11268 && (ep->X_add_number & ((1 << bit) - 1)) == 0
11269 && ep->X_add_number >= min << bit
11270 && ep->X_add_number < max << bit);
11271}
11272
252b5132
RH
11273/* This routine assembles an instruction into its binary format. As a
11274 side effect, it sets one of the global variables imm_reloc or
11275 offset_reloc to the type of relocation to do if one of the operands
11276 is an address expression. */
11277
11278static void
17a2f251 11279mips_ip (char *str, struct mips_cl_insn *ip)
252b5132 11280{
df58fc94
RS
11281 bfd_boolean wrong_delay_slot_insns = FALSE;
11282 bfd_boolean need_delay_slot_ok = TRUE;
11283 struct mips_opcode *firstinsn = NULL;
11284 const struct mips_opcode *past;
11285 struct hash_control *hash;
252b5132
RH
11286 char *s;
11287 const char *args;
43841e91 11288 char c = 0;
252b5132
RH
11289 struct mips_opcode *insn;
11290 char *argsStart;
e76ff5ab 11291 unsigned int regno, regno2;
34224acf 11292 unsigned int lastregno;
df58fc94 11293 unsigned int destregno = 0;
af7ee8bf 11294 unsigned int lastpos = 0;
071742cf 11295 unsigned int limlo, limhi;
f02d8318 11296 int sizelo;
252b5132 11297 char *s_reset;
74cd071d 11298 offsetT min_range, max_range;
df58fc94 11299 long opend;
a40bc9dd 11300 char *name;
707bfff6
TS
11301 int argnum;
11302 unsigned int rtype;
df58fc94 11303 char *dot;
a40bc9dd 11304 long end;
252b5132
RH
11305
11306 insn_error = NULL;
11307
df58fc94
RS
11308 if (mips_opts.micromips)
11309 {
11310 hash = micromips_op_hash;
11311 past = &micromips_opcodes[bfd_micromips_num_opcodes];
11312 }
11313 else
11314 {
11315 hash = op_hash;
11316 past = &mips_opcodes[NUMOPCODES];
11317 }
11318 forced_insn_length = 0;
252b5132 11319 insn = NULL;
252b5132 11320
df58fc94 11321 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
11322 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
11323 continue;
bdaaa2e1 11324
a40bc9dd
RS
11325 /* Make a copy of the instruction so that we can fiddle with it. */
11326 name = alloca (end + 1);
11327 memcpy (name, str, end);
11328 name[end] = '\0';
252b5132 11329
df58fc94
RS
11330 for (;;)
11331 {
11332 insn = (struct mips_opcode *) hash_find (hash, name);
11333
11334 if (insn != NULL || !mips_opts.micromips)
11335 break;
11336 if (forced_insn_length)
11337 break;
11338
11339 /* See if there's an instruction size override suffix,
11340 either `16' or `32', at the end of the mnemonic proper,
11341 that defines the operation, i.e. before the first `.'
11342 character if any. Strip it and retry. */
11343 dot = strchr (name, '.');
11344 opend = dot != NULL ? dot - name : end;
11345 if (opend < 3)
11346 break;
11347 if (name[opend - 2] == '1' && name[opend - 1] == '6')
11348 forced_insn_length = 2;
11349 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
11350 forced_insn_length = 4;
11351 else
11352 break;
11353 memcpy (name + opend - 2, name + opend, end - opend + 1);
11354 }
252b5132
RH
11355 if (insn == NULL)
11356 {
a40bc9dd
RS
11357 insn_error = _("Unrecognized opcode");
11358 return;
252b5132
RH
11359 }
11360
df58fc94
RS
11361 /* For microMIPS instructions placed in a fixed-length branch delay slot
11362 we make up to two passes over the relevant fragment of the opcode
11363 table. First we try instructions that meet the delay slot's length
11364 requirement. If none matched, then we retry with the remaining ones
11365 and if one matches, then we use it and then issue an appropriate
11366 warning later on. */
a40bc9dd 11367 argsStart = s = str + end;
252b5132
RH
11368 for (;;)
11369 {
df58fc94
RS
11370 bfd_boolean delay_slot_ok;
11371 bfd_boolean size_ok;
b34976b6 11372 bfd_boolean ok;
252b5132 11373
a40bc9dd 11374 gas_assert (strcmp (insn->name, name) == 0);
252b5132 11375
f79e2745 11376 ok = is_opcode_valid (insn);
df58fc94
RS
11377 size_ok = is_size_valid (insn);
11378 delay_slot_ok = is_delay_slot_valid (insn);
11379 if (!delay_slot_ok && !wrong_delay_slot_insns)
252b5132 11380 {
df58fc94
RS
11381 firstinsn = insn;
11382 wrong_delay_slot_insns = TRUE;
11383 }
11384 if (!ok || !size_ok || delay_slot_ok != need_delay_slot_ok)
11385 {
11386 static char buf[256];
11387
11388 if (insn + 1 < past && strcmp (insn->name, insn[1].name) == 0)
252b5132
RH
11389 {
11390 ++insn;
11391 continue;
11392 }
df58fc94 11393 if (wrong_delay_slot_insns && need_delay_slot_ok)
beae10d5 11394 {
df58fc94
RS
11395 gas_assert (firstinsn);
11396 need_delay_slot_ok = FALSE;
11397 past = insn + 1;
11398 insn = firstinsn;
11399 continue;
252b5132 11400 }
df58fc94
RS
11401
11402 if (insn_error)
11403 return;
11404
11405 if (!ok)
7bd942df 11406 sprintf (buf, _("Opcode not supported on this processor: %s (%s)"),
df58fc94
RS
11407 mips_cpu_info_from_arch (mips_opts.arch)->name,
11408 mips_cpu_info_from_isa (mips_opts.isa)->name);
833794fc
MR
11409 else if (mips_opts.insn32)
11410 sprintf (buf, _("Opcode not supported in the `insn32' mode"));
df58fc94
RS
11411 else
11412 sprintf (buf, _("Unrecognized %u-bit version of microMIPS opcode"),
11413 8 * forced_insn_length);
11414 insn_error = buf;
11415
11416 return;
252b5132
RH
11417 }
11418
1e915849 11419 create_insn (ip, insn);
268f6bed 11420 insn_error = NULL;
707bfff6 11421 argnum = 1;
24864476 11422 lastregno = 0xffffffff;
252b5132
RH
11423 for (args = insn->args;; ++args)
11424 {
deec1734
CD
11425 int is_mdmx;
11426
ad8d3bb3 11427 s += strspn (s, " \t");
deec1734 11428 is_mdmx = 0;
252b5132
RH
11429 switch (*args)
11430 {
11431 case '\0': /* end of args */
11432 if (*s == '\0')
11433 return;
11434 break;
11435
03f66e8a
MR
11436 case '2':
11437 /* DSP 2-bit unsigned immediate in bit 11 (for standard MIPS
11438 code) or 14 (for microMIPS code). */
8b082fb1
TS
11439 my_getExpression (&imm_expr, s);
11440 check_absolute_expr (ip, &imm_expr);
11441 if ((unsigned long) imm_expr.X_add_number != 1
11442 && (unsigned long) imm_expr.X_add_number != 3)
11443 {
11444 as_bad (_("BALIGN immediate not 1 or 3 (%lu)"),
11445 (unsigned long) imm_expr.X_add_number);
11446 }
03f66e8a
MR
11447 INSERT_OPERAND (mips_opts.micromips,
11448 BP, *ip, imm_expr.X_add_number);
8b082fb1
TS
11449 imm_expr.X_op = O_absent;
11450 s = expr_end;
11451 continue;
11452
03f66e8a 11453 case '3':
c3678916
RS
11454 /* DSP 3-bit unsigned immediate in bit 21 (for standard MIPS
11455 code) or 13 (for microMIPS code). */
03f66e8a
MR
11456 {
11457 unsigned long mask = (mips_opts.micromips
11458 ? MICROMIPSOP_MASK_SA3 : OP_MASK_SA3);
11459
11460 my_getExpression (&imm_expr, s);
11461 check_absolute_expr (ip, &imm_expr);
11462 if ((unsigned long) imm_expr.X_add_number > mask)
11463 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11464 mask, (unsigned long) imm_expr.X_add_number);
11465 INSERT_OPERAND (mips_opts.micromips,
11466 SA3, *ip, imm_expr.X_add_number);
11467 imm_expr.X_op = O_absent;
11468 s = expr_end;
11469 }
74cd071d
CF
11470 continue;
11471
03f66e8a 11472 case '4':
c3678916
RS
11473 /* DSP 4-bit unsigned immediate in bit 21 (for standard MIPS
11474 code) or 12 (for microMIPS code). */
03f66e8a
MR
11475 {
11476 unsigned long mask = (mips_opts.micromips
11477 ? MICROMIPSOP_MASK_SA4 : OP_MASK_SA4);
11478
11479 my_getExpression (&imm_expr, s);
11480 check_absolute_expr (ip, &imm_expr);
11481 if ((unsigned long) imm_expr.X_add_number > mask)
11482 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11483 mask, (unsigned long) imm_expr.X_add_number);
11484 INSERT_OPERAND (mips_opts.micromips,
11485 SA4, *ip, imm_expr.X_add_number);
11486 imm_expr.X_op = O_absent;
11487 s = expr_end;
11488 }
74cd071d
CF
11489 continue;
11490
03f66e8a 11491 case '5':
c3678916
RS
11492 /* DSP 8-bit unsigned immediate in bit 16 (for standard MIPS
11493 code) or 13 (for microMIPS code). */
03f66e8a
MR
11494 {
11495 unsigned long mask = (mips_opts.micromips
11496 ? MICROMIPSOP_MASK_IMM8 : OP_MASK_IMM8);
11497
11498 my_getExpression (&imm_expr, s);
11499 check_absolute_expr (ip, &imm_expr);
11500 if ((unsigned long) imm_expr.X_add_number > mask)
11501 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11502 mask, (unsigned long) imm_expr.X_add_number);
11503 INSERT_OPERAND (mips_opts.micromips,
11504 IMM8, *ip, imm_expr.X_add_number);
11505 imm_expr.X_op = O_absent;
11506 s = expr_end;
11507 }
74cd071d
CF
11508 continue;
11509
03f66e8a 11510 case '6':
c3678916
RS
11511 /* DSP 5-bit unsigned immediate in bit 21 (for standard MIPS
11512 code) or 16 (for microMIPS code). */
03f66e8a
MR
11513 {
11514 unsigned long mask = (mips_opts.micromips
11515 ? MICROMIPSOP_MASK_RS : OP_MASK_RS);
11516
11517 my_getExpression (&imm_expr, s);
11518 check_absolute_expr (ip, &imm_expr);
11519 if ((unsigned long) imm_expr.X_add_number > mask)
11520 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11521 mask, (unsigned long) imm_expr.X_add_number);
11522 INSERT_OPERAND (mips_opts.micromips,
11523 RS, *ip, imm_expr.X_add_number);
11524 imm_expr.X_op = O_absent;
11525 s = expr_end;
11526 }
74cd071d
CF
11527 continue;
11528
c3678916
RS
11529 case '7':
11530 /* Four DSP accumulators in bit 11 (for standard MIPS code)
11531 or 14 (for microMIPS code). */
03f66e8a
MR
11532 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11533 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11534 {
11535 regno = s[3] - '0';
11536 s += 4;
03f66e8a 11537 INSERT_OPERAND (mips_opts.micromips, DSPACC, *ip, regno);
74cd071d
CF
11538 continue;
11539 }
11540 else
11541 as_bad (_("Invalid dsp acc register"));
11542 break;
11543
03f66e8a
MR
11544 case '8':
11545 /* DSP 6-bit unsigned immediate in bit 11 (for standard MIPS
11546 code) or 14 (for microMIPS code). */
11547 {
11548 unsigned long mask = (mips_opts.micromips
11549 ? MICROMIPSOP_MASK_WRDSP
11550 : OP_MASK_WRDSP);
11551
11552 my_getExpression (&imm_expr, s);
11553 check_absolute_expr (ip, &imm_expr);
11554 if ((unsigned long) imm_expr.X_add_number > mask)
11555 as_bad (_("DSP immediate not in range 0..%lu (%lu)"),
11556 mask, (unsigned long) imm_expr.X_add_number);
11557 INSERT_OPERAND (mips_opts.micromips,
11558 WRDSP, *ip, imm_expr.X_add_number);
11559 imm_expr.X_op = O_absent;
11560 s = expr_end;
11561 }
74cd071d
CF
11562 continue;
11563
90ecf173 11564 case '9': /* Four DSP accumulators in bits 21,22. */
df58fc94 11565 gas_assert (!mips_opts.micromips);
03f66e8a
MR
11566 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c'
11567 && s[3] >= '0' && s[3] <= '3')
74cd071d
CF
11568 {
11569 regno = s[3] - '0';
11570 s += 4;
df58fc94 11571 INSERT_OPERAND (0, DSPACC_S, *ip, regno);
74cd071d
CF
11572 continue;
11573 }
11574 else
11575 as_bad (_("Invalid dsp acc register"));
11576 break;
11577
03f66e8a 11578 case '0':
c3678916
RS
11579 /* DSP 6-bit signed immediate in bit 20 (for standard MIPS
11580 code) or 16 (for microMIPS code). */
03f66e8a
MR
11581 {
11582 long mask = (mips_opts.micromips
11583 ? MICROMIPSOP_MASK_DSPSFT : OP_MASK_DSPSFT);
11584
11585 my_getExpression (&imm_expr, s);
11586 check_absolute_expr (ip, &imm_expr);
11587 min_range = -((mask + 1) >> 1);
11588 max_range = ((mask + 1) >> 1) - 1;
11589 if (imm_expr.X_add_number < min_range
11590 || imm_expr.X_add_number > max_range)
a9e24354
TS
11591 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11592 (long) min_range, (long) max_range,
11593 (long) imm_expr.X_add_number);
03f66e8a
MR
11594 INSERT_OPERAND (mips_opts.micromips,
11595 DSPSFT, *ip, imm_expr.X_add_number);
11596 imm_expr.X_op = O_absent;
11597 s = expr_end;
11598 }
74cd071d
CF
11599 continue;
11600
90ecf173 11601 case '\'': /* DSP 6-bit unsigned immediate in bit 16. */
df58fc94 11602 gas_assert (!mips_opts.micromips);
74cd071d
CF
11603 my_getExpression (&imm_expr, s);
11604 check_absolute_expr (ip, &imm_expr);
11605 if (imm_expr.X_add_number & ~OP_MASK_RDDSP)
11606 {
a9e24354
TS
11607 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11608 OP_MASK_RDDSP,
11609 (unsigned long) imm_expr.X_add_number);
74cd071d 11610 }
df58fc94 11611 INSERT_OPERAND (0, RDDSP, *ip, imm_expr.X_add_number);
74cd071d
CF
11612 imm_expr.X_op = O_absent;
11613 s = expr_end;
11614 continue;
11615
90ecf173 11616 case ':': /* DSP 7-bit signed immediate in bit 19. */
df58fc94 11617 gas_assert (!mips_opts.micromips);
74cd071d
CF
11618 my_getExpression (&imm_expr, s);
11619 check_absolute_expr (ip, &imm_expr);
11620 min_range = -((OP_MASK_DSPSFT_7 + 1) >> 1);
11621 max_range = ((OP_MASK_DSPSFT_7 + 1) >> 1) - 1;
11622 if (imm_expr.X_add_number < min_range ||
11623 imm_expr.X_add_number > max_range)
11624 {
a9e24354
TS
11625 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11626 (long) min_range, (long) max_range,
11627 (long) imm_expr.X_add_number);
74cd071d 11628 }
df58fc94 11629 INSERT_OPERAND (0, DSPSFT_7, *ip, imm_expr.X_add_number);
74cd071d
CF
11630 imm_expr.X_op = O_absent;
11631 s = expr_end;
11632 continue;
11633
90ecf173 11634 case '@': /* DSP 10-bit signed immediate in bit 16. */
03f66e8a
MR
11635 {
11636 long mask = (mips_opts.micromips
11637 ? MICROMIPSOP_MASK_IMM10 : OP_MASK_IMM10);
11638
11639 my_getExpression (&imm_expr, s);
11640 check_absolute_expr (ip, &imm_expr);
11641 min_range = -((mask + 1) >> 1);
11642 max_range = ((mask + 1) >> 1) - 1;
11643 if (imm_expr.X_add_number < min_range
11644 || imm_expr.X_add_number > max_range)
a9e24354
TS
11645 as_bad (_("DSP immediate not in range %ld..%ld (%ld)"),
11646 (long) min_range, (long) max_range,
11647 (long) imm_expr.X_add_number);
03f66e8a
MR
11648 INSERT_OPERAND (mips_opts.micromips,
11649 IMM10, *ip, imm_expr.X_add_number);
11650 imm_expr.X_op = O_absent;
11651 s = expr_end;
11652 }
11653 continue;
11654
11655 case '^': /* DSP 5-bit unsigned immediate in bit 11. */
11656 gas_assert (mips_opts.micromips);
11657 my_getExpression (&imm_expr, s);
11658 check_absolute_expr (ip, &imm_expr);
11659 if (imm_expr.X_add_number & ~MICROMIPSOP_MASK_RD)
11660 as_bad (_("DSP immediate not in range 0..%d (%lu)"),
11661 MICROMIPSOP_MASK_RD,
11662 (unsigned long) imm_expr.X_add_number);
11663 INSERT_OPERAND (1, RD, *ip, imm_expr.X_add_number);
74cd071d
CF
11664 imm_expr.X_op = O_absent;
11665 s = expr_end;
11666 continue;
11667
a9e24354 11668 case '!': /* MT usermode flag bit. */
df58fc94 11669 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11670 my_getExpression (&imm_expr, s);
11671 check_absolute_expr (ip, &imm_expr);
11672 if (imm_expr.X_add_number & ~OP_MASK_MT_U)
a9e24354
TS
11673 as_bad (_("MT usermode bit not 0 or 1 (%lu)"),
11674 (unsigned long) imm_expr.X_add_number);
df58fc94 11675 INSERT_OPERAND (0, MT_U, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11676 imm_expr.X_op = O_absent;
11677 s = expr_end;
11678 continue;
11679
a9e24354 11680 case '$': /* MT load high flag bit. */
df58fc94 11681 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11682 my_getExpression (&imm_expr, s);
11683 check_absolute_expr (ip, &imm_expr);
11684 if (imm_expr.X_add_number & ~OP_MASK_MT_H)
a9e24354
TS
11685 as_bad (_("MT load high bit not 0 or 1 (%lu)"),
11686 (unsigned long) imm_expr.X_add_number);
df58fc94 11687 INSERT_OPERAND (0, MT_H, *ip, imm_expr.X_add_number);
ef2e4d86
CF
11688 imm_expr.X_op = O_absent;
11689 s = expr_end;
11690 continue;
11691
90ecf173 11692 case '*': /* Four DSP accumulators in bits 18,19. */
df58fc94 11693 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11694 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11695 s[3] >= '0' && s[3] <= '3')
11696 {
11697 regno = s[3] - '0';
11698 s += 4;
df58fc94 11699 INSERT_OPERAND (0, MTACC_T, *ip, regno);
ef2e4d86
CF
11700 continue;
11701 }
11702 else
11703 as_bad (_("Invalid dsp/smartmips acc register"));
11704 break;
11705
90ecf173 11706 case '&': /* Four DSP accumulators in bits 13,14. */
df58fc94 11707 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11708 if (s[0] == '$' && s[1] == 'a' && s[2] == 'c' &&
11709 s[3] >= '0' && s[3] <= '3')
11710 {
11711 regno = s[3] - '0';
11712 s += 4;
df58fc94 11713 INSERT_OPERAND (0, MTACC_D, *ip, regno);
ef2e4d86
CF
11714 continue;
11715 }
11716 else
11717 as_bad (_("Invalid dsp/smartmips acc register"));
11718 break;
11719
dec0624d
MR
11720 case '\\': /* 3-bit bit position. */
11721 {
2906b037
MR
11722 unsigned long mask = (mips_opts.micromips
11723 ? MICROMIPSOP_MASK_3BITPOS
11724 : OP_MASK_3BITPOS);
dec0624d
MR
11725
11726 my_getExpression (&imm_expr, s);
11727 check_absolute_expr (ip, &imm_expr);
11728 if ((unsigned long) imm_expr.X_add_number > mask)
11729 as_warn (_("Bit position for %s not in range 0..%lu (%lu)"),
11730 ip->insn_mo->name,
11731 mask, (unsigned long) imm_expr.X_add_number);
11732 INSERT_OPERAND (mips_opts.micromips,
11733 3BITPOS, *ip, imm_expr.X_add_number);
11734 imm_expr.X_op = O_absent;
11735 s = expr_end;
11736 }
11737 continue;
11738
252b5132 11739 case ',':
a339155f 11740 ++argnum;
252b5132
RH
11741 if (*s++ == *args)
11742 continue;
11743 s--;
11744 switch (*++args)
11745 {
11746 case 'r':
11747 case 'v':
df58fc94 11748 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132
RH
11749 continue;
11750
11751 case 'w':
df58fc94 11752 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
38487616
TS
11753 continue;
11754
252b5132 11755 case 'W':
df58fc94
RS
11756 gas_assert (!mips_opts.micromips);
11757 INSERT_OPERAND (0, FT, *ip, lastregno);
252b5132
RH
11758 continue;
11759
11760 case 'V':
df58fc94 11761 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132
RH
11762 continue;
11763 }
11764 break;
11765
11766 case '(':
11767 /* Handle optional base register.
11768 Either the base register is omitted or
bdaaa2e1 11769 we must have a left paren. */
252b5132
RH
11770 /* This is dependent on the next operand specifier
11771 is a base register specification. */
df58fc94
RS
11772 gas_assert (args[1] == 'b'
11773 || (mips_opts.micromips
11774 && args[1] == 'm'
11775 && (args[2] == 'l' || args[2] == 'n'
11776 || args[2] == 's' || args[2] == 'a')));
11777 if (*s == '\0' && args[1] == 'b')
252b5132 11778 return;
df58fc94 11779 /* Fall through. */
252b5132 11780
90ecf173 11781 case ')': /* These must match exactly. */
df58fc94
RS
11782 if (*s++ == *args)
11783 continue;
11784 break;
11785
af7ee8bf
CD
11786 case '+': /* Opcode extension character. */
11787 switch (*++args)
11788 {
9bcd4f99
TS
11789 case '1': /* UDI immediates. */
11790 case '2':
11791 case '3':
11792 case '4':
df58fc94 11793 gas_assert (!mips_opts.micromips);
9bcd4f99
TS
11794 {
11795 const struct mips_immed *imm = mips_immed;
11796
11797 while (imm->type && imm->type != *args)
11798 ++imm;
11799 if (! imm->type)
b37df7c4 11800 abort ();
9bcd4f99
TS
11801 my_getExpression (&imm_expr, s);
11802 check_absolute_expr (ip, &imm_expr);
11803 if ((unsigned long) imm_expr.X_add_number & ~imm->mask)
11804 {
11805 as_warn (_("Illegal %s number (%lu, 0x%lx)"),
11806 imm->desc ? imm->desc : ip->insn_mo->name,
11807 (unsigned long) imm_expr.X_add_number,
11808 (unsigned long) imm_expr.X_add_number);
90ecf173 11809 imm_expr.X_add_number &= imm->mask;
9bcd4f99
TS
11810 }
11811 ip->insn_opcode |= ((unsigned long) imm_expr.X_add_number
11812 << imm->shift);
11813 imm_expr.X_op = O_absent;
11814 s = expr_end;
11815 }
11816 continue;
90ecf173 11817
b015e599
AP
11818 case 'J': /* 10-bit hypcall code. */
11819 gas_assert (!mips_opts.micromips);
11820 {
11821 unsigned long mask = OP_MASK_CODE10;
11822
11823 my_getExpression (&imm_expr, s);
11824 check_absolute_expr (ip, &imm_expr);
11825 if ((unsigned long) imm_expr.X_add_number > mask)
11826 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
11827 ip->insn_mo->name,
11828 mask, (unsigned long) imm_expr.X_add_number);
11829 INSERT_OPERAND (0, CODE10, *ip, imm_expr.X_add_number);
11830 imm_expr.X_op = O_absent;
11831 s = expr_end;
11832 }
11833 continue;
11834
071742cf
CD
11835 case 'A': /* ins/ext position, becomes LSB. */
11836 limlo = 0;
11837 limhi = 31;
5f74bc13
CD
11838 goto do_lsb;
11839 case 'E':
11840 limlo = 32;
11841 limhi = 63;
11842 goto do_lsb;
90ecf173 11843 do_lsb:
071742cf
CD
11844 my_getExpression (&imm_expr, s);
11845 check_absolute_expr (ip, &imm_expr);
11846 if ((unsigned long) imm_expr.X_add_number < limlo
11847 || (unsigned long) imm_expr.X_add_number > limhi)
11848 {
11849 as_bad (_("Improper position (%lu)"),
11850 (unsigned long) imm_expr.X_add_number);
11851 imm_expr.X_add_number = limlo;
11852 }
11853 lastpos = imm_expr.X_add_number;
df58fc94
RS
11854 INSERT_OPERAND (mips_opts.micromips,
11855 EXTLSB, *ip, imm_expr.X_add_number);
071742cf
CD
11856 imm_expr.X_op = O_absent;
11857 s = expr_end;
11858 continue;
11859
11860 case 'B': /* ins size, becomes MSB. */
11861 limlo = 1;
11862 limhi = 32;
5f74bc13
CD
11863 goto do_msb;
11864 case 'F':
11865 limlo = 33;
11866 limhi = 64;
11867 goto do_msb;
90ecf173 11868 do_msb:
071742cf
CD
11869 my_getExpression (&imm_expr, s);
11870 check_absolute_expr (ip, &imm_expr);
11871 /* Check for negative input so that small negative numbers
11872 will not succeed incorrectly. The checks against
11873 (pos+size) transitively check "size" itself,
11874 assuming that "pos" is reasonable. */
11875 if ((long) imm_expr.X_add_number < 0
11876 || ((unsigned long) imm_expr.X_add_number
11877 + lastpos) < limlo
11878 || ((unsigned long) imm_expr.X_add_number
11879 + lastpos) > limhi)
11880 {
11881 as_bad (_("Improper insert size (%lu, position %lu)"),
11882 (unsigned long) imm_expr.X_add_number,
11883 (unsigned long) lastpos);
11884 imm_expr.X_add_number = limlo - lastpos;
11885 }
df58fc94
RS
11886 INSERT_OPERAND (mips_opts.micromips, INSMSB, *ip,
11887 lastpos + imm_expr.X_add_number - 1);
071742cf
CD
11888 imm_expr.X_op = O_absent;
11889 s = expr_end;
11890 continue;
11891
11892 case 'C': /* ext size, becomes MSBD. */
11893 limlo = 1;
11894 limhi = 32;
f02d8318 11895 sizelo = 1;
5f74bc13
CD
11896 goto do_msbd;
11897 case 'G':
11898 limlo = 33;
11899 limhi = 64;
f02d8318 11900 sizelo = 33;
5f74bc13
CD
11901 goto do_msbd;
11902 case 'H':
11903 limlo = 33;
11904 limhi = 64;
f02d8318 11905 sizelo = 1;
5f74bc13 11906 goto do_msbd;
90ecf173 11907 do_msbd:
071742cf
CD
11908 my_getExpression (&imm_expr, s);
11909 check_absolute_expr (ip, &imm_expr);
f02d8318
CF
11910 /* The checks against (pos+size) don't transitively check
11911 "size" itself, assuming that "pos" is reasonable.
11912 We also need to check the lower bound of "size". */
11913 if ((long) imm_expr.X_add_number < sizelo
071742cf
CD
11914 || ((unsigned long) imm_expr.X_add_number
11915 + lastpos) < limlo
11916 || ((unsigned long) imm_expr.X_add_number
11917 + lastpos) > limhi)
11918 {
11919 as_bad (_("Improper extract size (%lu, position %lu)"),
11920 (unsigned long) imm_expr.X_add_number,
11921 (unsigned long) lastpos);
11922 imm_expr.X_add_number = limlo - lastpos;
11923 }
df58fc94
RS
11924 INSERT_OPERAND (mips_opts.micromips,
11925 EXTMSBD, *ip, imm_expr.X_add_number - 1);
071742cf
CD
11926 imm_expr.X_op = O_absent;
11927 s = expr_end;
11928 continue;
af7ee8bf 11929
5f74bc13
CD
11930 case 'I':
11931 /* "+I" is like "I", except that imm2_expr is used. */
11932 my_getExpression (&imm2_expr, s);
11933 if (imm2_expr.X_op != O_big
11934 && imm2_expr.X_op != O_constant)
11935 insn_error = _("absolute expression required");
9ee2a2d4
MR
11936 if (HAVE_32BIT_GPRS)
11937 normalize_constant_expr (&imm2_expr);
5f74bc13
CD
11938 s = expr_end;
11939 continue;
11940
707bfff6 11941 case 't': /* Coprocessor register number. */
df58fc94 11942 gas_assert (!mips_opts.micromips);
ef2e4d86
CF
11943 if (s[0] == '$' && ISDIGIT (s[1]))
11944 {
11945 ++s;
11946 regno = 0;
11947 do
11948 {
11949 regno *= 10;
11950 regno += *s - '0';
11951 ++s;
11952 }
11953 while (ISDIGIT (*s));
11954 if (regno > 31)
11955 as_bad (_("Invalid register number (%d)"), regno);
11956 else
11957 {
df58fc94 11958 INSERT_OPERAND (0, RT, *ip, regno);
ef2e4d86
CF
11959 continue;
11960 }
11961 }
11962 else
11963 as_bad (_("Invalid coprocessor 0 register number"));
11964 break;
11965
bb35fb24
NC
11966 case 'x':
11967 /* bbit[01] and bbit[01]32 bit index. Give error if index
11968 is not in the valid range. */
df58fc94 11969 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11970 my_getExpression (&imm_expr, s);
11971 check_absolute_expr (ip, &imm_expr);
11972 if ((unsigned) imm_expr.X_add_number > 31)
11973 {
11974 as_bad (_("Improper bit index (%lu)"),
11975 (unsigned long) imm_expr.X_add_number);
11976 imm_expr.X_add_number = 0;
11977 }
df58fc94 11978 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number);
bb35fb24
NC
11979 imm_expr.X_op = O_absent;
11980 s = expr_end;
11981 continue;
11982
11983 case 'X':
11984 /* bbit[01] bit index when bbit is used but we generate
11985 bbit[01]32 because the index is over 32. Move to the
11986 next candidate if index is not in the valid range. */
df58fc94 11987 gas_assert (!mips_opts.micromips);
bb35fb24
NC
11988 my_getExpression (&imm_expr, s);
11989 check_absolute_expr (ip, &imm_expr);
11990 if ((unsigned) imm_expr.X_add_number < 32
11991 || (unsigned) imm_expr.X_add_number > 63)
11992 break;
df58fc94 11993 INSERT_OPERAND (0, BBITIND, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
11994 imm_expr.X_op = O_absent;
11995 s = expr_end;
11996 continue;
11997
11998 case 'p':
11999 /* cins, cins32, exts and exts32 position field. Give error
12000 if it's not in the valid range. */
df58fc94 12001 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12002 my_getExpression (&imm_expr, s);
12003 check_absolute_expr (ip, &imm_expr);
12004 if ((unsigned) imm_expr.X_add_number > 31)
12005 {
12006 as_bad (_("Improper position (%lu)"),
12007 (unsigned long) imm_expr.X_add_number);
12008 imm_expr.X_add_number = 0;
12009 }
23e69e47 12010 lastpos = imm_expr.X_add_number;
df58fc94 12011 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number);
bb35fb24
NC
12012 imm_expr.X_op = O_absent;
12013 s = expr_end;
12014 continue;
12015
12016 case 'P':
12017 /* cins, cins32, exts and exts32 position field. Move to
12018 the next candidate if it's not in the valid range. */
df58fc94 12019 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12020 my_getExpression (&imm_expr, s);
12021 check_absolute_expr (ip, &imm_expr);
12022 if ((unsigned) imm_expr.X_add_number < 32
12023 || (unsigned) imm_expr.X_add_number > 63)
12024 break;
12025 lastpos = imm_expr.X_add_number;
df58fc94 12026 INSERT_OPERAND (0, CINSPOS, *ip, imm_expr.X_add_number - 32);
bb35fb24
NC
12027 imm_expr.X_op = O_absent;
12028 s = expr_end;
12029 continue;
12030
12031 case 's':
23e69e47 12032 /* cins32 and exts32 length-minus-one field. */
df58fc94 12033 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12034 my_getExpression (&imm_expr, s);
12035 check_absolute_expr (ip, &imm_expr);
23e69e47
RS
12036 if ((unsigned long) imm_expr.X_add_number > 31
12037 || (unsigned long) imm_expr.X_add_number + lastpos > 31)
bb35fb24
NC
12038 {
12039 as_bad (_("Improper size (%lu)"),
12040 (unsigned long) imm_expr.X_add_number);
12041 imm_expr.X_add_number = 0;
12042 }
df58fc94 12043 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
12044 imm_expr.X_op = O_absent;
12045 s = expr_end;
12046 continue;
12047
12048 case 'S':
23e69e47 12049 /* cins/exts length-minus-one field. */
df58fc94 12050 gas_assert (!mips_opts.micromips);
bb35fb24
NC
12051 my_getExpression (&imm_expr, s);
12052 check_absolute_expr (ip, &imm_expr);
23e69e47 12053 if ((unsigned long) imm_expr.X_add_number > 31
bb35fb24
NC
12054 || (unsigned long) imm_expr.X_add_number + lastpos > 63)
12055 {
12056 as_bad (_("Improper size (%lu)"),
12057 (unsigned long) imm_expr.X_add_number);
12058 imm_expr.X_add_number = 0;
12059 }
df58fc94 12060 INSERT_OPERAND (0, CINSLM1, *ip, imm_expr.X_add_number);
bb35fb24
NC
12061 imm_expr.X_op = O_absent;
12062 s = expr_end;
12063 continue;
12064
dd3cbb7e
NC
12065 case 'Q':
12066 /* seqi/snei immediate field. */
df58fc94 12067 gas_assert (!mips_opts.micromips);
dd3cbb7e
NC
12068 my_getExpression (&imm_expr, s);
12069 check_absolute_expr (ip, &imm_expr);
12070 if ((long) imm_expr.X_add_number < -512
12071 || (long) imm_expr.X_add_number >= 512)
12072 {
12073 as_bad (_("Improper immediate (%ld)"),
12074 (long) imm_expr.X_add_number);
12075 imm_expr.X_add_number = 0;
12076 }
df58fc94 12077 INSERT_OPERAND (0, SEQI, *ip, imm_expr.X_add_number);
dd3cbb7e
NC
12078 imm_expr.X_op = O_absent;
12079 s = expr_end;
12080 continue;
12081
98675402 12082 case 'a': /* 8-bit signed offset in bit 6 */
df58fc94 12083 gas_assert (!mips_opts.micromips);
98675402
RS
12084 my_getExpression (&imm_expr, s);
12085 check_absolute_expr (ip, &imm_expr);
12086 min_range = -((OP_MASK_OFFSET_A + 1) >> 1);
12087 max_range = ((OP_MASK_OFFSET_A + 1) >> 1) - 1;
12088 if (imm_expr.X_add_number < min_range
12089 || imm_expr.X_add_number > max_range)
12090 {
c95354ed 12091 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
12092 (long) min_range, (long) max_range,
12093 (long) imm_expr.X_add_number);
12094 }
df58fc94 12095 INSERT_OPERAND (0, OFFSET_A, *ip, imm_expr.X_add_number);
98675402
RS
12096 imm_expr.X_op = O_absent;
12097 s = expr_end;
12098 continue;
12099
12100 case 'b': /* 8-bit signed offset in bit 3 */
df58fc94 12101 gas_assert (!mips_opts.micromips);
98675402
RS
12102 my_getExpression (&imm_expr, s);
12103 check_absolute_expr (ip, &imm_expr);
12104 min_range = -((OP_MASK_OFFSET_B + 1) >> 1);
12105 max_range = ((OP_MASK_OFFSET_B + 1) >> 1) - 1;
12106 if (imm_expr.X_add_number < min_range
12107 || imm_expr.X_add_number > max_range)
12108 {
c95354ed 12109 as_bad (_("Offset not in range %ld..%ld (%ld)"),
98675402
RS
12110 (long) min_range, (long) max_range,
12111 (long) imm_expr.X_add_number);
12112 }
df58fc94 12113 INSERT_OPERAND (0, OFFSET_B, *ip, imm_expr.X_add_number);
98675402
RS
12114 imm_expr.X_op = O_absent;
12115 s = expr_end;
12116 continue;
12117
12118 case 'c': /* 9-bit signed offset in bit 6 */
df58fc94 12119 gas_assert (!mips_opts.micromips);
98675402
RS
12120 my_getExpression (&imm_expr, s);
12121 check_absolute_expr (ip, &imm_expr);
12122 min_range = -((OP_MASK_OFFSET_C + 1) >> 1);
12123 max_range = ((OP_MASK_OFFSET_C + 1) >> 1) - 1;
c95354ed
MX
12124 /* We check the offset range before adjusted. */
12125 min_range <<= 4;
12126 max_range <<= 4;
98675402
RS
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 }
c95354ed
MX
12134 if (imm_expr.X_add_number & 0xf)
12135 {
12136 as_bad (_("Offset not 16 bytes alignment (%ld)"),
12137 (long) imm_expr.X_add_number);
12138 }
12139 /* Right shift 4 bits to adjust the offset operand. */
df58fc94
RS
12140 INSERT_OPERAND (0, OFFSET_C, *ip,
12141 imm_expr.X_add_number >> 4);
98675402
RS
12142 imm_expr.X_op = O_absent;
12143 s = expr_end;
12144 continue;
12145
12146 case 'z':
df58fc94 12147 gas_assert (!mips_opts.micromips);
98675402
RS
12148 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
12149 break;
12150 if (regno == AT && mips_opts.at)
12151 {
12152 if (mips_opts.at == ATREG)
12153 as_warn (_("used $at without \".set noat\""));
12154 else
12155 as_warn (_("used $%u with \".set at=$%u\""),
12156 regno, mips_opts.at);
12157 }
df58fc94 12158 INSERT_OPERAND (0, RZ, *ip, regno);
98675402
RS
12159 continue;
12160
12161 case 'Z':
df58fc94 12162 gas_assert (!mips_opts.micromips);
98675402
RS
12163 if (!reg_lookup (&s, RTYPE_FPU, &regno))
12164 break;
df58fc94 12165 INSERT_OPERAND (0, FZ, *ip, regno);
98675402
RS
12166 continue;
12167
27c5c572
RS
12168 case 'i':
12169 goto jump;
12170
7f3c4072
CM
12171 case 'j':
12172 {
12173 int shift = 8;
12174 size_t i;
12175 /* Check whether there is only a single bracketed expression
12176 left. If so, it must be the base register and the
12177 constant must be zero. */
12178 if (*s == '(' && strchr (s + 1, '(') == 0)
12179 continue;
12180
12181 /* If this value won't fit into the offset, then go find
12182 a macro that will generate a 16- or 32-bit offset code
12183 pattern. */
12184 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12185 if ((i == 0 && (imm_expr.X_op != O_constant
12186 || imm_expr.X_add_number >= 1 << shift
12187 || imm_expr.X_add_number < -1 << shift))
12188 || i > 0)
12189 {
12190 imm_expr.X_op = O_absent;
12191 break;
12192 }
12193 INSERT_OPERAND (mips_opts.micromips, EVAOFFSET, *ip,
12194 imm_expr.X_add_number);
12195 imm_expr.X_op = O_absent;
12196 s = expr_end;
12197 }
12198 continue;
12199
af7ee8bf 12200 default:
df58fc94 12201 as_bad (_("Internal error: bad %s opcode "
90ecf173 12202 "(unknown extension operand type `+%c'): %s %s"),
df58fc94 12203 mips_opts.micromips ? "microMIPS" : "MIPS",
90ecf173 12204 *args, insn->name, insn->args);
af7ee8bf
CD
12205 /* Further processing is fruitless. */
12206 return;
12207 }
12208 break;
12209
df58fc94 12210 case '.': /* 10-bit offset. */
df58fc94 12211 gas_assert (mips_opts.micromips);
dec0624d 12212 case '~': /* 12-bit offset. */
df58fc94
RS
12213 {
12214 int shift = *args == '.' ? 9 : 11;
12215 size_t i;
12216
12217 /* Check whether there is only a single bracketed expression
12218 left. If so, it must be the base register and the
12219 constant must be zero. */
12220 if (*s == '(' && strchr (s + 1, '(') == 0)
12221 continue;
12222
12223 /* If this value won't fit into the offset, then go find
12224 a macro that will generate a 16- or 32-bit offset code
12225 pattern. */
12226 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
12227 if ((i == 0 && (imm_expr.X_op != O_constant
12228 || imm_expr.X_add_number >= 1 << shift
12229 || imm_expr.X_add_number < -1 << shift))
12230 || i > 0)
12231 {
12232 imm_expr.X_op = O_absent;
12233 break;
12234 }
12235 if (shift == 9)
12236 INSERT_OPERAND (1, OFFSET10, *ip, imm_expr.X_add_number);
12237 else
dec0624d
MR
12238 INSERT_OPERAND (mips_opts.micromips,
12239 OFFSET12, *ip, imm_expr.X_add_number);
df58fc94
RS
12240 imm_expr.X_op = O_absent;
12241 s = expr_end;
12242 }
12243 continue;
12244
252b5132
RH
12245 case '<': /* must be at least one digit */
12246 /*
12247 * According to the manual, if the shift amount is greater
b6ff326e
KH
12248 * than 31 or less than 0, then the shift amount should be
12249 * mod 32. In reality the mips assembler issues an error.
252b5132
RH
12250 * We issue a warning and mask out all but the low 5 bits.
12251 */
12252 my_getExpression (&imm_expr, s);
12253 check_absolute_expr (ip, &imm_expr);
12254 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
12255 as_warn (_("Improper shift amount (%lu)"),
12256 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12257 INSERT_OPERAND (mips_opts.micromips,
12258 SHAMT, *ip, imm_expr.X_add_number);
252b5132
RH
12259 imm_expr.X_op = O_absent;
12260 s = expr_end;
12261 continue;
12262
12263 case '>': /* shift amount minus 32 */
12264 my_getExpression (&imm_expr, s);
12265 check_absolute_expr (ip, &imm_expr);
12266 if ((unsigned long) imm_expr.X_add_number < 32
12267 || (unsigned long) imm_expr.X_add_number > 63)
12268 break;
df58fc94
RS
12269 INSERT_OPERAND (mips_opts.micromips,
12270 SHAMT, *ip, imm_expr.X_add_number - 32);
252b5132
RH
12271 imm_expr.X_op = O_absent;
12272 s = expr_end;
12273 continue;
12274
90ecf173
MR
12275 case 'k': /* CACHE code. */
12276 case 'h': /* PREFX code. */
12277 case '1': /* SYNC type. */
252b5132
RH
12278 my_getExpression (&imm_expr, s);
12279 check_absolute_expr (ip, &imm_expr);
12280 if ((unsigned long) imm_expr.X_add_number > 31)
bf12938e
RS
12281 as_warn (_("Invalid value for `%s' (%lu)"),
12282 ip->insn_mo->name,
12283 (unsigned long) imm_expr.X_add_number);
df58fc94 12284 switch (*args)
d954098f 12285 {
df58fc94
RS
12286 case 'k':
12287 if (mips_fix_cn63xxp1
12288 && !mips_opts.micromips
12289 && strcmp ("pref", insn->name) == 0)
d954098f
DD
12290 switch (imm_expr.X_add_number)
12291 {
12292 case 5:
12293 case 25:
12294 case 26:
12295 case 27:
12296 case 28:
12297 case 29:
12298 case 30:
12299 case 31: /* These are ok. */
12300 break;
12301
12302 default: /* The rest must be changed to 28. */
12303 imm_expr.X_add_number = 28;
12304 break;
12305 }
df58fc94
RS
12306 INSERT_OPERAND (mips_opts.micromips,
12307 CACHE, *ip, imm_expr.X_add_number);
12308 break;
12309 case 'h':
12310 INSERT_OPERAND (mips_opts.micromips,
12311 PREFX, *ip, imm_expr.X_add_number);
12312 break;
12313 case '1':
12314 INSERT_OPERAND (mips_opts.micromips,
12315 STYPE, *ip, imm_expr.X_add_number);
12316 break;
d954098f 12317 }
252b5132
RH
12318 imm_expr.X_op = O_absent;
12319 s = expr_end;
12320 continue;
12321
90ecf173 12322 case 'c': /* BREAK code. */
df58fc94
RS
12323 {
12324 unsigned long mask = (mips_opts.micromips
12325 ? MICROMIPSOP_MASK_CODE
12326 : OP_MASK_CODE);
12327
12328 my_getExpression (&imm_expr, s);
12329 check_absolute_expr (ip, &imm_expr);
12330 if ((unsigned long) imm_expr.X_add_number > mask)
12331 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12332 ip->insn_mo->name,
12333 mask, (unsigned long) imm_expr.X_add_number);
12334 INSERT_OPERAND (mips_opts.micromips,
12335 CODE, *ip, imm_expr.X_add_number);
12336 imm_expr.X_op = O_absent;
12337 s = expr_end;
12338 }
252b5132
RH
12339 continue;
12340
90ecf173 12341 case 'q': /* Lower BREAK code. */
df58fc94
RS
12342 {
12343 unsigned long mask = (mips_opts.micromips
12344 ? MICROMIPSOP_MASK_CODE2
12345 : OP_MASK_CODE2);
12346
12347 my_getExpression (&imm_expr, s);
12348 check_absolute_expr (ip, &imm_expr);
12349 if ((unsigned long) imm_expr.X_add_number > mask)
12350 as_warn (_("Lower code for %s not in range 0..%lu (%lu)"),
12351 ip->insn_mo->name,
12352 mask, (unsigned long) imm_expr.X_add_number);
12353 INSERT_OPERAND (mips_opts.micromips,
12354 CODE2, *ip, imm_expr.X_add_number);
12355 imm_expr.X_op = O_absent;
12356 s = expr_end;
12357 }
252b5132
RH
12358 continue;
12359
df58fc94
RS
12360 case 'B': /* 20- or 10-bit syscall/break/wait code. */
12361 {
12362 unsigned long mask = (mips_opts.micromips
12363 ? MICROMIPSOP_MASK_CODE10
12364 : OP_MASK_CODE20);
12365
12366 my_getExpression (&imm_expr, s);
12367 check_absolute_expr (ip, &imm_expr);
12368 if ((unsigned long) imm_expr.X_add_number > mask)
12369 as_warn (_("Code for %s not in range 0..%lu (%lu)"),
12370 ip->insn_mo->name,
12371 mask, (unsigned long) imm_expr.X_add_number);
12372 if (mips_opts.micromips)
12373 INSERT_OPERAND (1, CODE10, *ip, imm_expr.X_add_number);
12374 else
12375 INSERT_OPERAND (0, CODE20, *ip, imm_expr.X_add_number);
12376 imm_expr.X_op = O_absent;
12377 s = expr_end;
12378 }
252b5132
RH
12379 continue;
12380
df58fc94
RS
12381 case 'C': /* 25- or 23-bit coprocessor code. */
12382 {
12383 unsigned long mask = (mips_opts.micromips
12384 ? MICROMIPSOP_MASK_COPZ
12385 : OP_MASK_COPZ);
12386
12387 my_getExpression (&imm_expr, s);
12388 check_absolute_expr (ip, &imm_expr);
12389 if ((unsigned long) imm_expr.X_add_number > mask)
12390 as_warn (_("Coproccesor code > %u bits (%lu)"),
12391 mips_opts.micromips ? 23U : 25U,
793b27f4 12392 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12393 INSERT_OPERAND (mips_opts.micromips,
12394 COPZ, *ip, imm_expr.X_add_number);
12395 imm_expr.X_op = O_absent;
12396 s = expr_end;
12397 }
beae10d5 12398 continue;
252b5132 12399
df58fc94
RS
12400 case 'J': /* 19-bit WAIT code. */
12401 gas_assert (!mips_opts.micromips);
4372b673
NC
12402 my_getExpression (&imm_expr, s);
12403 check_absolute_expr (ip, &imm_expr);
793b27f4 12404 if ((unsigned long) imm_expr.X_add_number > OP_MASK_CODE19)
df58fc94
RS
12405 {
12406 as_warn (_("Illegal 19-bit code (%lu)"),
a9e24354 12407 (unsigned long) imm_expr.X_add_number);
df58fc94
RS
12408 imm_expr.X_add_number &= OP_MASK_CODE19;
12409 }
12410 INSERT_OPERAND (0, CODE19, *ip, imm_expr.X_add_number);
4372b673
NC
12411 imm_expr.X_op = O_absent;
12412 s = expr_end;
12413 continue;
12414
707bfff6 12415 case 'P': /* Performance register. */
df58fc94 12416 gas_assert (!mips_opts.micromips);
beae10d5 12417 my_getExpression (&imm_expr, s);
252b5132 12418 check_absolute_expr (ip, &imm_expr);
beae10d5 12419 if (imm_expr.X_add_number != 0 && imm_expr.X_add_number != 1)
bf12938e
RS
12420 as_warn (_("Invalid performance register (%lu)"),
12421 (unsigned long) imm_expr.X_add_number);
e407c74b
NC
12422 if (imm_expr.X_add_number != 0 && mips_opts.arch == CPU_R5900
12423 && (!strcmp(insn->name,"mfps") || !strcmp(insn->name,"mtps")))
12424 as_warn (_("Invalid performance register (%lu)"),
12425 (unsigned long) imm_expr.X_add_number);
df58fc94 12426 INSERT_OPERAND (0, PERFREG, *ip, imm_expr.X_add_number);
beae10d5
KH
12427 imm_expr.X_op = O_absent;
12428 s = expr_end;
12429 continue;
252b5132 12430
707bfff6 12431 case 'G': /* Coprocessor destination register. */
df58fc94
RS
12432 {
12433 unsigned long opcode = ip->insn_opcode;
12434 unsigned long mask;
12435 unsigned int types;
12436 int cop0;
12437
12438 if (mips_opts.micromips)
12439 {
12440 mask = ~((MICROMIPSOP_MASK_RT << MICROMIPSOP_SH_RT)
12441 | (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS)
12442 | (MICROMIPSOP_MASK_SEL << MICROMIPSOP_SH_SEL));
12443 opcode &= mask;
12444 switch (opcode)
12445 {
12446 case 0x000000fc: /* mfc0 */
12447 case 0x000002fc: /* mtc0 */
12448 case 0x580000fc: /* dmfc0 */
12449 case 0x580002fc: /* dmtc0 */
12450 cop0 = 1;
12451 break;
12452 default:
12453 cop0 = 0;
12454 break;
12455 }
12456 }
12457 else
12458 {
12459 opcode = (opcode >> OP_SH_OP) & OP_MASK_OP;
12460 cop0 = opcode == OP_OP_COP0;
12461 }
12462 types = RTYPE_NUM | (cop0 ? RTYPE_CP0 : RTYPE_GP);
12463 ok = reg_lookup (&s, types, &regno);
12464 if (mips_opts.micromips)
12465 INSERT_OPERAND (1, RS, *ip, regno);
12466 else
12467 INSERT_OPERAND (0, RD, *ip, regno);
12468 if (ok)
12469 {
12470 lastregno = regno;
12471 continue;
12472 }
12473 }
12474 break;
707bfff6 12475
df58fc94
RS
12476 case 'y': /* ALNV.PS source register. */
12477 gas_assert (mips_opts.micromips);
12478 goto do_reg;
12479 case 'x': /* Ignore register name. */
12480 case 'U': /* Destination register (CLO/CLZ). */
12481 case 'g': /* Coprocessor destination register. */
12482 gas_assert (!mips_opts.micromips);
90ecf173
MR
12483 case 'b': /* Base register. */
12484 case 'd': /* Destination register. */
12485 case 's': /* Source register. */
12486 case 't': /* Target register. */
12487 case 'r': /* Both target and source. */
12488 case 'v': /* Both dest and source. */
12489 case 'w': /* Both dest and target. */
12490 case 'E': /* Coprocessor target register. */
12491 case 'K': /* RDHWR destination register. */
90ecf173 12492 case 'z': /* Must be zero register. */
df58fc94 12493 do_reg:
90ecf173 12494 s_reset = s;
707bfff6
TS
12495 if (*args == 'E' || *args == 'K')
12496 ok = reg_lookup (&s, RTYPE_NUM, &regno);
12497 else
12498 {
12499 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
741fe287
MR
12500 if (regno == AT && mips_opts.at)
12501 {
12502 if (mips_opts.at == ATREG)
f71d0d44 12503 as_warn (_("Used $at without \".set noat\""));
741fe287 12504 else
f71d0d44 12505 as_warn (_("Used $%u with \".set at=$%u\""),
741fe287
MR
12506 regno, mips_opts.at);
12507 }
707bfff6
TS
12508 }
12509 if (ok)
252b5132 12510 {
252b5132
RH
12511 c = *args;
12512 if (*s == ' ')
f9419b05 12513 ++s;
252b5132
RH
12514 if (args[1] != *s)
12515 {
12516 if (c == 'r' || c == 'v' || c == 'w')
12517 {
12518 regno = lastregno;
12519 s = s_reset;
f9419b05 12520 ++args;
252b5132
RH
12521 }
12522 }
12523 /* 'z' only matches $0. */
12524 if (c == 'z' && regno != 0)
12525 break;
12526
24864476 12527 if (c == 's' && !strncmp (ip->insn_mo->name, "jalr", 4))
e7c604dd
CM
12528 {
12529 if (regno == lastregno)
90ecf173
MR
12530 {
12531 insn_error
f71d0d44 12532 = _("Source and destination must be different");
e7c604dd 12533 continue;
90ecf173 12534 }
24864476 12535 if (regno == 31 && lastregno == 0xffffffff)
90ecf173
MR
12536 {
12537 insn_error
f71d0d44 12538 = _("A destination register must be supplied");
e7c604dd 12539 continue;
90ecf173 12540 }
e7c604dd 12541 }
90ecf173
MR
12542 /* Now that we have assembled one operand, we use the args
12543 string to figure out where it goes in the instruction. */
252b5132
RH
12544 switch (c)
12545 {
12546 case 'r':
12547 case 's':
12548 case 'v':
12549 case 'b':
df58fc94 12550 INSERT_OPERAND (mips_opts.micromips, RS, *ip, regno);
252b5132 12551 break;
df58fc94 12552
af7ee8bf 12553 case 'K':
df58fc94
RS
12554 if (mips_opts.micromips)
12555 INSERT_OPERAND (1, RS, *ip, regno);
12556 else
12557 INSERT_OPERAND (0, RD, *ip, regno);
12558 break;
12559
12560 case 'd':
ef2e4d86 12561 case 'g':
df58fc94 12562 INSERT_OPERAND (mips_opts.micromips, RD, *ip, regno);
252b5132 12563 break;
df58fc94 12564
4372b673 12565 case 'U':
df58fc94
RS
12566 gas_assert (!mips_opts.micromips);
12567 INSERT_OPERAND (0, RD, *ip, regno);
12568 INSERT_OPERAND (0, RT, *ip, regno);
4372b673 12569 break;
df58fc94 12570
252b5132
RH
12571 case 'w':
12572 case 't':
12573 case 'E':
df58fc94
RS
12574 INSERT_OPERAND (mips_opts.micromips, RT, *ip, regno);
12575 break;
12576
12577 case 'y':
12578 gas_assert (mips_opts.micromips);
12579 INSERT_OPERAND (1, RS3, *ip, regno);
252b5132 12580 break;
df58fc94 12581
252b5132
RH
12582 case 'x':
12583 /* This case exists because on the r3000 trunc
12584 expands into a macro which requires a gp
12585 register. On the r6000 or r4000 it is
12586 assembled into a single instruction which
12587 ignores the register. Thus the insn version
12588 is MIPS_ISA2 and uses 'x', and the macro
12589 version is MIPS_ISA1 and uses 't'. */
12590 break;
df58fc94 12591
252b5132
RH
12592 case 'z':
12593 /* This case is for the div instruction, which
12594 acts differently if the destination argument
12595 is $0. This only matches $0, and is checked
12596 outside the switch. */
12597 break;
252b5132
RH
12598 }
12599 lastregno = regno;
12600 continue;
12601 }
252b5132
RH
12602 switch (*args++)
12603 {
12604 case 'r':
12605 case 'v':
df58fc94 12606 INSERT_OPERAND (mips_opts.micromips, RS, *ip, lastregno);
252b5132 12607 continue;
df58fc94 12608
252b5132 12609 case 'w':
df58fc94 12610 INSERT_OPERAND (mips_opts.micromips, RT, *ip, lastregno);
252b5132
RH
12611 continue;
12612 }
12613 break;
12614
deec1734 12615 case 'O': /* MDMX alignment immediate constant. */
df58fc94 12616 gas_assert (!mips_opts.micromips);
deec1734
CD
12617 my_getExpression (&imm_expr, s);
12618 check_absolute_expr (ip, &imm_expr);
12619 if ((unsigned long) imm_expr.X_add_number > OP_MASK_ALN)
20203fb9 12620 as_warn (_("Improper align amount (%ld), using low bits"),
bf12938e 12621 (long) imm_expr.X_add_number);
df58fc94 12622 INSERT_OPERAND (0, ALN, *ip, imm_expr.X_add_number);
deec1734
CD
12623 imm_expr.X_op = O_absent;
12624 s = expr_end;
12625 continue;
12626
12627 case 'Q': /* MDMX vector, element sel, or const. */
12628 if (s[0] != '$')
12629 {
12630 /* MDMX Immediate. */
df58fc94 12631 gas_assert (!mips_opts.micromips);
deec1734
CD
12632 my_getExpression (&imm_expr, s);
12633 check_absolute_expr (ip, &imm_expr);
12634 if ((unsigned long) imm_expr.X_add_number > OP_MASK_FT)
bf12938e
RS
12635 as_warn (_("Invalid MDMX Immediate (%ld)"),
12636 (long) imm_expr.X_add_number);
df58fc94 12637 INSERT_OPERAND (0, FT, *ip, imm_expr.X_add_number);
deec1734
CD
12638 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12639 ip->insn_opcode |= MDMX_FMTSEL_IMM_QH << OP_SH_VSEL;
12640 else
12641 ip->insn_opcode |= MDMX_FMTSEL_IMM_OB << OP_SH_VSEL;
deec1734
CD
12642 imm_expr.X_op = O_absent;
12643 s = expr_end;
12644 continue;
12645 }
12646 /* Not MDMX Immediate. Fall through. */
12647 case 'X': /* MDMX destination register. */
12648 case 'Y': /* MDMX source register. */
12649 case 'Z': /* MDMX target register. */
5c324c16 12650 is_mdmx = !(insn->membership & INSN_5400);
df58fc94
RS
12651 case 'W':
12652 gas_assert (!mips_opts.micromips);
90ecf173
MR
12653 case 'D': /* Floating point destination register. */
12654 case 'S': /* Floating point source register. */
12655 case 'T': /* Floating point target register. */
12656 case 'R': /* Floating point source register. */
252b5132 12657 case 'V':
707bfff6
TS
12658 rtype = RTYPE_FPU;
12659 if (is_mdmx
846ef2d0 12660 || ((mips_opts.ase & ASE_MDMX)
707bfff6
TS
12661 && (ip->insn_mo->pinfo & FP_D)
12662 && (ip->insn_mo->pinfo & (INSN_COPROC_MOVE_DELAY
12663 | INSN_COPROC_MEMORY_DELAY
12664 | INSN_LOAD_COPROC_DELAY
12665 | INSN_LOAD_MEMORY_DELAY
12666 | INSN_STORE_MEMORY))))
12667 rtype |= RTYPE_VEC;
252b5132 12668 s_reset = s;
707bfff6 12669 if (reg_lookup (&s, rtype, &regno))
252b5132 12670 {
252b5132 12671 if ((regno & 1) != 0
ca4e0257 12672 && HAVE_32BIT_FPRS
90ecf173 12673 && !mips_oddfpreg_ok (ip->insn_mo, argnum))
252b5132
RH
12674 as_warn (_("Float register should be even, was %d"),
12675 regno);
12676
12677 c = *args;
12678 if (*s == ' ')
f9419b05 12679 ++s;
252b5132
RH
12680 if (args[1] != *s)
12681 {
12682 if (c == 'V' || c == 'W')
12683 {
12684 regno = lastregno;
12685 s = s_reset;
f9419b05 12686 ++args;
252b5132
RH
12687 }
12688 }
12689 switch (c)
12690 {
12691 case 'D':
deec1734 12692 case 'X':
df58fc94 12693 INSERT_OPERAND (mips_opts.micromips, FD, *ip, regno);
252b5132 12694 break;
df58fc94 12695
252b5132
RH
12696 case 'V':
12697 case 'S':
deec1734 12698 case 'Y':
df58fc94 12699 INSERT_OPERAND (mips_opts.micromips, FS, *ip, regno);
252b5132 12700 break;
df58fc94 12701
deec1734
CD
12702 case 'Q':
12703 /* This is like 'Z', but also needs to fix the MDMX
12704 vector/scalar select bits. Note that the
12705 scalar immediate case is handled above. */
5c324c16
RS
12706 if ((ip->insn_mo->membership & INSN_5400)
12707 && strcmp (insn->name, "rzu.ob") == 0)
12708 as_bad (_("Operand %d of `%s' must be an immediate"),
12709 argnum, ip->insn_mo->name);
12710
deec1734
CD
12711 if (*s == '[')
12712 {
12713 int is_qh = (ip->insn_opcode & (1 << OP_SH_VSEL));
12714 int max_el = (is_qh ? 3 : 7);
12715 s++;
12716 my_getExpression(&imm_expr, s);
12717 check_absolute_expr (ip, &imm_expr);
12718 s = expr_end;
12719 if (imm_expr.X_add_number > max_el)
20203fb9
NC
12720 as_bad (_("Bad element selector %ld"),
12721 (long) imm_expr.X_add_number);
deec1734
CD
12722 imm_expr.X_add_number &= max_el;
12723 ip->insn_opcode |= (imm_expr.X_add_number
12724 << (OP_SH_VSEL +
12725 (is_qh ? 2 : 1)));
01a3f561 12726 imm_expr.X_op = O_absent;
deec1734 12727 if (*s != ']')
20203fb9 12728 as_warn (_("Expecting ']' found '%s'"), s);
deec1734
CD
12729 else
12730 s++;
12731 }
12732 else
5c324c16
RS
12733 {
12734 if ((ip->insn_mo->membership & INSN_5400)
12735 && (strcmp (insn->name, "sll.ob") == 0
12736 || strcmp (insn->name, "srl.ob") == 0))
12737 as_bad (_("Operand %d of `%s' must be scalar"),
12738 argnum, ip->insn_mo->name);
12739
deec1734
CD
12740 if (ip->insn_opcode & (OP_MASK_VSEL << OP_SH_VSEL))
12741 ip->insn_opcode |= (MDMX_FMTSEL_VEC_QH
12742 << OP_SH_VSEL);
12743 else
12744 ip->insn_opcode |= (MDMX_FMTSEL_VEC_OB <<
12745 OP_SH_VSEL);
12746 }
90ecf173 12747 /* Fall through. */
252b5132
RH
12748 case 'W':
12749 case 'T':
deec1734 12750 case 'Z':
df58fc94 12751 INSERT_OPERAND (mips_opts.micromips, FT, *ip, regno);
252b5132 12752 break;
df58fc94 12753
252b5132 12754 case 'R':
df58fc94 12755 INSERT_OPERAND (mips_opts.micromips, FR, *ip, regno);
252b5132
RH
12756 break;
12757 }
12758 lastregno = regno;
12759 continue;
12760 }
12761
252b5132
RH
12762 switch (*args++)
12763 {
12764 case 'V':
df58fc94 12765 INSERT_OPERAND (mips_opts.micromips, FS, *ip, lastregno);
252b5132 12766 continue;
df58fc94 12767
252b5132 12768 case 'W':
df58fc94 12769 INSERT_OPERAND (mips_opts.micromips, FT, *ip, lastregno);
252b5132
RH
12770 continue;
12771 }
12772 break;
12773
12774 case 'I':
12775 my_getExpression (&imm_expr, s);
12776 if (imm_expr.X_op != O_big
12777 && imm_expr.X_op != O_constant)
12778 insn_error = _("absolute expression required");
9ee2a2d4
MR
12779 if (HAVE_32BIT_GPRS)
12780 normalize_constant_expr (&imm_expr);
252b5132
RH
12781 s = expr_end;
12782 continue;
12783
12784 case 'A':
12785 my_getExpression (&offset_expr, s);
2051e8c4 12786 normalize_address_expr (&offset_expr);
f6688943 12787 *imm_reloc = BFD_RELOC_32;
252b5132
RH
12788 s = expr_end;
12789 continue;
12790
12791 case 'F':
12792 case 'L':
12793 case 'f':
12794 case 'l':
12795 {
12796 int f64;
ca4e0257 12797 int using_gprs;
252b5132
RH
12798 char *save_in;
12799 char *err;
12800 unsigned char temp[8];
12801 int len;
12802 unsigned int length;
12803 segT seg;
12804 subsegT subseg;
12805 char *p;
12806
12807 /* These only appear as the last operand in an
12808 instruction, and every instruction that accepts
12809 them in any variant accepts them in all variants.
12810 This means we don't have to worry about backing out
12811 any changes if the instruction does not match.
12812
12813 The difference between them is the size of the
12814 floating point constant and where it goes. For 'F'
12815 and 'L' the constant is 64 bits; for 'f' and 'l' it
12816 is 32 bits. Where the constant is placed is based
12817 on how the MIPS assembler does things:
12818 F -- .rdata
12819 L -- .lit8
12820 f -- immediate value
12821 l -- .lit4
12822
12823 The .lit4 and .lit8 sections are only used if
12824 permitted by the -G argument.
12825
ca4e0257
RS
12826 The code below needs to know whether the target register
12827 is 32 or 64 bits wide. It relies on the fact 'f' and
12828 'F' are used with GPR-based instructions and 'l' and
12829 'L' are used with FPR-based instructions. */
252b5132
RH
12830
12831 f64 = *args == 'F' || *args == 'L';
ca4e0257 12832 using_gprs = *args == 'F' || *args == 'f';
252b5132
RH
12833
12834 save_in = input_line_pointer;
12835 input_line_pointer = s;
12836 err = md_atof (f64 ? 'd' : 'f', (char *) temp, &len);
12837 length = len;
12838 s = input_line_pointer;
12839 input_line_pointer = save_in;
12840 if (err != NULL && *err != '\0')
12841 {
12842 as_bad (_("Bad floating point constant: %s"), err);
12843 memset (temp, '\0', sizeof temp);
12844 length = f64 ? 8 : 4;
12845 }
12846
9c2799c2 12847 gas_assert (length == (unsigned) (f64 ? 8 : 4));
252b5132
RH
12848
12849 if (*args == 'f'
12850 || (*args == 'l'
3e722fb5 12851 && (g_switch_value < 4
252b5132
RH
12852 || (temp[0] == 0 && temp[1] == 0)
12853 || (temp[2] == 0 && temp[3] == 0))))
12854 {
12855 imm_expr.X_op = O_constant;
90ecf173 12856 if (!target_big_endian)
252b5132
RH
12857 imm_expr.X_add_number = bfd_getl32 (temp);
12858 else
12859 imm_expr.X_add_number = bfd_getb32 (temp);
12860 }
12861 else if (length > 4
90ecf173 12862 && !mips_disable_float_construction
ca4e0257
RS
12863 /* Constants can only be constructed in GPRs and
12864 copied to FPRs if the GPRs are at least as wide
12865 as the FPRs. Force the constant into memory if
12866 we are using 64-bit FPRs but the GPRs are only
12867 32 bits wide. */
12868 && (using_gprs
90ecf173 12869 || !(HAVE_64BIT_FPRS && HAVE_32BIT_GPRS))
252b5132
RH
12870 && ((temp[0] == 0 && temp[1] == 0)
12871 || (temp[2] == 0 && temp[3] == 0))
12872 && ((temp[4] == 0 && temp[5] == 0)
12873 || (temp[6] == 0 && temp[7] == 0)))
12874 {
ca4e0257 12875 /* The value is simple enough to load with a couple of
90ecf173
MR
12876 instructions. If using 32-bit registers, set
12877 imm_expr to the high order 32 bits and offset_expr to
12878 the low order 32 bits. Otherwise, set imm_expr to
12879 the entire 64 bit constant. */
ca4e0257 12880 if (using_gprs ? HAVE_32BIT_GPRS : HAVE_32BIT_FPRS)
252b5132
RH
12881 {
12882 imm_expr.X_op = O_constant;
12883 offset_expr.X_op = O_constant;
90ecf173 12884 if (!target_big_endian)
252b5132
RH
12885 {
12886 imm_expr.X_add_number = bfd_getl32 (temp + 4);
12887 offset_expr.X_add_number = bfd_getl32 (temp);
12888 }
12889 else
12890 {
12891 imm_expr.X_add_number = bfd_getb32 (temp);
12892 offset_expr.X_add_number = bfd_getb32 (temp + 4);
12893 }
12894 if (offset_expr.X_add_number == 0)
12895 offset_expr.X_op = O_absent;
12896 }
42429eac 12897 else
252b5132
RH
12898 {
12899 imm_expr.X_op = O_constant;
90ecf173 12900 if (!target_big_endian)
252b5132
RH
12901 imm_expr.X_add_number = bfd_getl64 (temp);
12902 else
12903 imm_expr.X_add_number = bfd_getb64 (temp);
12904 }
252b5132
RH
12905 }
12906 else
12907 {
12908 const char *newname;
12909 segT new_seg;
12910
12911 /* Switch to the right section. */
12912 seg = now_seg;
12913 subseg = now_subseg;
12914 switch (*args)
12915 {
12916 default: /* unused default case avoids warnings. */
12917 case 'L':
12918 newname = RDATA_SECTION_NAME;
3e722fb5 12919 if (g_switch_value >= 8)
252b5132
RH
12920 newname = ".lit8";
12921 break;
12922 case 'F':
3e722fb5 12923 newname = RDATA_SECTION_NAME;
252b5132
RH
12924 break;
12925 case 'l':
9c2799c2 12926 gas_assert (g_switch_value >= 4);
252b5132
RH
12927 newname = ".lit4";
12928 break;
12929 }
12930 new_seg = subseg_new (newname, (subsegT) 0);
f3ded42a
RS
12931 bfd_set_section_flags (stdoutput, new_seg,
12932 (SEC_ALLOC
12933 | SEC_LOAD
12934 | SEC_READONLY
12935 | SEC_DATA));
252b5132 12936 frag_align (*args == 'l' ? 2 : 3, 0, 0);
f3ded42a 12937 if (strncmp (TARGET_OS, "elf", 3) != 0)
252b5132
RH
12938 record_alignment (new_seg, 4);
12939 else
12940 record_alignment (new_seg, *args == 'l' ? 2 : 3);
12941 if (seg == now_seg)
12942 as_bad (_("Can't use floating point insn in this section"));
12943
df58fc94
RS
12944 /* Set the argument to the current address in the
12945 section. */
12946 offset_expr.X_op = O_symbol;
12947 offset_expr.X_add_symbol = symbol_temp_new_now ();
12948 offset_expr.X_add_number = 0;
12949
12950 /* Put the floating point number into the section. */
12951 p = frag_more ((int) length);
12952 memcpy (p, temp, length);
12953
12954 /* Switch back to the original section. */
12955 subseg_set (seg, subseg);
12956 }
12957 }
12958 continue;
12959
12960 case 'i': /* 16-bit unsigned immediate. */
12961 case 'j': /* 16-bit signed immediate. */
12962 *imm_reloc = BFD_RELOC_LO16;
12963 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0)
12964 {
12965 int more;
12966 offsetT minval, maxval;
12967
12968 more = (insn + 1 < past
12969 && strcmp (insn->name, insn[1].name) == 0);
12970
df58fc94
RS
12971 /* For compatibility with older assemblers, we accept
12972 0x8000-0xffff as signed 16-bit numbers when only
12973 signed numbers are allowed. */
12974 if (*args == 'i')
12975 minval = 0, maxval = 0xffff;
12976 else if (more)
12977 minval = -0x8000, maxval = 0x7fff;
12978 else
12979 minval = -0x8000, maxval = 0xffff;
12980
12981 if (imm_expr.X_op != O_constant
12982 || imm_expr.X_add_number < minval
12983 || imm_expr.X_add_number > maxval)
12984 {
12985 if (more)
12986 break;
12987 if (imm_expr.X_op == O_constant
12988 || imm_expr.X_op == O_big)
12989 as_bad (_("Expression out of range"));
12990 }
12991 }
12992 s = expr_end;
12993 continue;
12994
12995 case 'o': /* 16-bit offset. */
12996 offset_reloc[0] = BFD_RELOC_LO16;
12997 offset_reloc[1] = BFD_RELOC_UNUSED;
12998 offset_reloc[2] = BFD_RELOC_UNUSED;
12999
13000 /* Check whether there is only a single bracketed expression
13001 left. If so, it must be the base register and the
13002 constant must be zero. */
13003 if (*s == '(' && strchr (s + 1, '(') == 0)
13004 {
13005 offset_expr.X_op = O_constant;
13006 offset_expr.X_add_number = 0;
13007 continue;
13008 }
13009
13010 /* If this value won't fit into a 16 bit offset, then go
13011 find a macro that will generate the 32 bit offset
13012 code pattern. */
13013 if (my_getSmallExpression (&offset_expr, offset_reloc, s) == 0
13014 && (offset_expr.X_op != O_constant
13015 || offset_expr.X_add_number >= 0x8000
13016 || offset_expr.X_add_number < -0x8000))
13017 break;
13018
13019 s = expr_end;
13020 continue;
13021
13022 case 'p': /* PC-relative offset. */
13023 *offset_reloc = BFD_RELOC_16_PCREL_S2;
13024 my_getExpression (&offset_expr, s);
13025 s = expr_end;
13026 continue;
13027
13028 case 'u': /* Upper 16 bits. */
5821951c 13029 *imm_reloc = BFD_RELOC_LO16;
df58fc94
RS
13030 if (my_getSmallExpression (&imm_expr, imm_reloc, s) == 0
13031 && imm_expr.X_op == O_constant
13032 && (imm_expr.X_add_number < 0
13033 || imm_expr.X_add_number >= 0x10000))
13034 as_bad (_("lui expression (%lu) not in range 0..65535"),
13035 (unsigned long) imm_expr.X_add_number);
13036 s = expr_end;
13037 continue;
13038
13039 case 'a': /* 26-bit address. */
27c5c572 13040 jump:
df58fc94
RS
13041 *offset_reloc = BFD_RELOC_MIPS_JMP;
13042 my_getExpression (&offset_expr, s);
13043 s = expr_end;
13044 continue;
13045
13046 case 'N': /* 3-bit branch condition code. */
13047 case 'M': /* 3-bit compare condition code. */
13048 rtype = RTYPE_CCC;
13049 if (ip->insn_mo->pinfo & (FP_D | FP_S))
13050 rtype |= RTYPE_FCC;
13051 if (!reg_lookup (&s, rtype, &regno))
13052 break;
13053 if ((strcmp (str + strlen (str) - 3, ".ps") == 0
13054 || strcmp (str + strlen (str) - 5, "any2f") == 0
13055 || strcmp (str + strlen (str) - 5, "any2t") == 0)
13056 && (regno & 1) != 0)
13057 as_warn (_("Condition code register should be even for %s, "
13058 "was %d"),
13059 str, regno);
13060 if ((strcmp (str + strlen (str) - 5, "any4f") == 0
13061 || strcmp (str + strlen (str) - 5, "any4t") == 0)
13062 && (regno & 3) != 0)
13063 as_warn (_("Condition code register should be 0 or 4 for %s, "
13064 "was %d"),
13065 str, regno);
13066 if (*args == 'N')
13067 INSERT_OPERAND (mips_opts.micromips, BCC, *ip, regno);
13068 else
13069 INSERT_OPERAND (mips_opts.micromips, CCC, *ip, regno);
13070 continue;
13071
13072 case 'H':
13073 if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
13074 s += 2;
13075 if (ISDIGIT (*s))
13076 {
13077 c = 0;
13078 do
13079 {
13080 c *= 10;
13081 c += *s - '0';
13082 ++s;
13083 }
13084 while (ISDIGIT (*s));
13085 }
13086 else
13087 c = 8; /* Invalid sel value. */
13088
13089 if (c > 7)
13090 as_bad (_("Invalid coprocessor sub-selection value (0-7)"));
13091 INSERT_OPERAND (mips_opts.micromips, SEL, *ip, c);
13092 continue;
13093
13094 case 'e':
13095 gas_assert (!mips_opts.micromips);
13096 /* Must be at least one digit. */
13097 my_getExpression (&imm_expr, s);
13098 check_absolute_expr (ip, &imm_expr);
13099
13100 if ((unsigned long) imm_expr.X_add_number
13101 > (unsigned long) OP_MASK_VECBYTE)
13102 {
13103 as_bad (_("bad byte vector index (%ld)"),
13104 (long) imm_expr.X_add_number);
13105 imm_expr.X_add_number = 0;
13106 }
13107
13108 INSERT_OPERAND (0, VECBYTE, *ip, imm_expr.X_add_number);
13109 imm_expr.X_op = O_absent;
13110 s = expr_end;
13111 continue;
13112
13113 case '%':
13114 gas_assert (!mips_opts.micromips);
13115 my_getExpression (&imm_expr, s);
13116 check_absolute_expr (ip, &imm_expr);
13117
13118 if ((unsigned long) imm_expr.X_add_number
13119 > (unsigned long) OP_MASK_VECALIGN)
13120 {
13121 as_bad (_("bad byte vector index (%ld)"),
13122 (long) imm_expr.X_add_number);
13123 imm_expr.X_add_number = 0;
13124 }
13125
13126 INSERT_OPERAND (0, VECALIGN, *ip, imm_expr.X_add_number);
13127 imm_expr.X_op = O_absent;
13128 s = expr_end;
13129 continue;
13130
13131 case 'm': /* Opcode extension character. */
13132 gas_assert (mips_opts.micromips);
13133 c = *++args;
13134 switch (c)
13135 {
13136 case 'r':
13137 if (strncmp (s, "$pc", 3) == 0)
13138 {
13139 s += 3;
13140 continue;
13141 }
13142 break;
13143
13144 case 'a':
13145 case 'b':
13146 case 'c':
13147 case 'd':
13148 case 'e':
13149 case 'f':
13150 case 'g':
13151 case 'h':
df58fc94
RS
13152 case 'j':
13153 case 'l':
13154 case 'm':
13155 case 'n':
13156 case 'p':
13157 case 'q':
13158 case 's':
13159 case 't':
13160 case 'x':
13161 case 'y':
13162 case 'z':
13163 s_reset = s;
13164 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno);
13165 if (regno == AT && mips_opts.at)
13166 {
13167 if (mips_opts.at == ATREG)
13168 as_warn (_("Used $at without \".set noat\""));
13169 else
13170 as_warn (_("Used $%u with \".set at=$%u\""),
13171 regno, mips_opts.at);
13172 }
13173 if (!ok)
13174 {
13175 if (c == 'c')
13176 {
13177 gas_assert (args[1] == ',');
13178 regno = lastregno;
13179 ++args;
13180 }
13181 else if (c == 't')
13182 {
13183 gas_assert (args[1] == ',');
13184 ++args;
13185 continue; /* Nothing to do. */
13186 }
13187 else
13188 break;
13189 }
13190
13191 if (c == 'j' && !strncmp (ip->insn_mo->name, "jalr", 4))
13192 {
13193 if (regno == lastregno)
13194 {
13195 insn_error
13196 = _("Source and destination must be different");
13197 continue;
13198 }
13199 if (regno == 31 && lastregno == 0xffffffff)
13200 {
13201 insn_error
13202 = _("A destination register must be supplied");
13203 continue;
13204 }
13205 }
13206
13207 if (*s == ' ')
13208 ++s;
13209 if (args[1] != *s)
13210 {
13211 if (c == 'e')
13212 {
13213 gas_assert (args[1] == ',');
13214 regno = lastregno;
13215 s = s_reset;
13216 ++args;
13217 }
13218 else if (c == 't')
13219 {
13220 gas_assert (args[1] == ',');
13221 s = s_reset;
13222 ++args;
13223 continue; /* Nothing to do. */
13224 }
13225 }
13226
13227 /* Make sure regno is the same as lastregno. */
13228 if (c == 't' && regno != lastregno)
13229 break;
13230
13231 /* Make sure regno is the same as destregno. */
13232 if (c == 'x' && regno != destregno)
13233 break;
13234
13235 /* We need to save regno, before regno maps to the
13236 microMIPS register encoding. */
13237 lastregno = regno;
13238
13239 if (c == 'f')
13240 destregno = regno;
13241
13242 switch (c)
13243 {
13244 case 'a':
13245 if (regno != GP)
13246 regno = ILLEGAL_REG;
13247 break;
13248
13249 case 'b':
13250 regno = mips32_to_micromips_reg_b_map[regno];
13251 break;
13252
13253 case 'c':
13254 regno = mips32_to_micromips_reg_c_map[regno];
13255 break;
13256
13257 case 'd':
13258 regno = mips32_to_micromips_reg_d_map[regno];
13259 break;
13260
13261 case 'e':
13262 regno = mips32_to_micromips_reg_e_map[regno];
13263 break;
13264
13265 case 'f':
13266 regno = mips32_to_micromips_reg_f_map[regno];
13267 break;
13268
13269 case 'g':
13270 regno = mips32_to_micromips_reg_g_map[regno];
13271 break;
13272
13273 case 'h':
e76ff5ab
RS
13274 s += strspn (s, " \t");
13275 if (*s != ',')
df58fc94 13276 {
e76ff5ab
RS
13277 regno = ILLEGAL_REG;
13278 break;
df58fc94 13279 }
e76ff5ab
RS
13280 ++s;
13281 s += strspn (s, " \t");
13282 ok = reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno2);
13283 if (!ok)
13284 {
13285 regno = ILLEGAL_REG;
13286 break;
13287 }
13288 if (regno2 == AT && mips_opts.at)
13289 {
13290 if (mips_opts.at == ATREG)
13291 as_warn (_("Used $at without \".set noat\""));
13292 else
13293 as_warn (_("Used $%u with \".set at=$%u\""),
13294 regno2, mips_opts.at);
13295 }
13296 regno = (mips_lookup_reg_pair
13297 (regno, regno2,
13298 micromips_to_32_reg_h_map1,
13299 micromips_to_32_reg_h_map2, 8));
df58fc94
RS
13300 break;
13301
13302 case 'l':
13303 regno = mips32_to_micromips_reg_l_map[regno];
13304 break;
13305
13306 case 'm':
13307 regno = mips32_to_micromips_reg_m_map[regno];
13308 break;
13309
13310 case 'n':
13311 regno = mips32_to_micromips_reg_n_map[regno];
13312 break;
13313
13314 case 'q':
13315 regno = mips32_to_micromips_reg_q_map[regno];
13316 break;
13317
13318 case 's':
13319 if (regno != SP)
13320 regno = ILLEGAL_REG;
13321 break;
13322
13323 case 'y':
13324 if (regno != 31)
13325 regno = ILLEGAL_REG;
13326 break;
13327
13328 case 'z':
13329 if (regno != ZERO)
13330 regno = ILLEGAL_REG;
13331 break;
13332
13333 case 'j': /* Do nothing. */
13334 case 'p':
13335 case 't':
13336 case 'x':
13337 break;
13338
13339 default:
b37df7c4 13340 abort ();
df58fc94
RS
13341 }
13342
13343 if (regno == ILLEGAL_REG)
13344 break;
13345
13346 switch (c)
13347 {
13348 case 'b':
13349 INSERT_OPERAND (1, MB, *ip, regno);
13350 break;
13351
13352 case 'c':
13353 INSERT_OPERAND (1, MC, *ip, regno);
13354 break;
13355
13356 case 'd':
13357 INSERT_OPERAND (1, MD, *ip, regno);
13358 break;
13359
13360 case 'e':
13361 INSERT_OPERAND (1, ME, *ip, regno);
13362 break;
13363
13364 case 'f':
13365 INSERT_OPERAND (1, MF, *ip, regno);
13366 break;
13367
13368 case 'g':
13369 INSERT_OPERAND (1, MG, *ip, regno);
13370 break;
13371
13372 case 'h':
13373 INSERT_OPERAND (1, MH, *ip, regno);
13374 break;
13375
df58fc94
RS
13376 case 'j':
13377 INSERT_OPERAND (1, MJ, *ip, regno);
13378 break;
13379
13380 case 'l':
13381 INSERT_OPERAND (1, ML, *ip, regno);
13382 break;
13383
13384 case 'm':
13385 INSERT_OPERAND (1, MM, *ip, regno);
13386 break;
13387
13388 case 'n':
13389 INSERT_OPERAND (1, MN, *ip, regno);
13390 break;
13391
13392 case 'p':
13393 INSERT_OPERAND (1, MP, *ip, regno);
13394 break;
13395
13396 case 'q':
13397 INSERT_OPERAND (1, MQ, *ip, regno);
13398 break;
13399
13400 case 'a': /* Do nothing. */
13401 case 's': /* Do nothing. */
13402 case 't': /* Do nothing. */
13403 case 'x': /* Do nothing. */
13404 case 'y': /* Do nothing. */
13405 case 'z': /* Do nothing. */
13406 break;
13407
13408 default:
b37df7c4 13409 abort ();
df58fc94
RS
13410 }
13411 continue;
13412
13413 case 'A':
13414 {
13415 bfd_reloc_code_real_type r[3];
13416 expressionS ep;
13417 int imm;
13418
13419 /* Check whether there is only a single bracketed
13420 expression left. If so, it must be the base register
13421 and the constant must be zero. */
13422 if (*s == '(' && strchr (s + 1, '(') == 0)
13423 {
13424 INSERT_OPERAND (1, IMMA, *ip, 0);
13425 continue;
13426 }
13427
13428 if (my_getSmallExpression (&ep, r, s) > 0
13429 || !expr_const_in_range (&ep, -64, 64, 2))
13430 break;
13431
13432 imm = ep.X_add_number >> 2;
13433 INSERT_OPERAND (1, IMMA, *ip, imm);
13434 }
13435 s = expr_end;
13436 continue;
13437
13438 case 'B':
13439 {
13440 bfd_reloc_code_real_type r[3];
13441 expressionS ep;
13442 int imm;
13443
13444 if (my_getSmallExpression (&ep, r, s) > 0
13445 || ep.X_op != O_constant)
13446 break;
13447
13448 for (imm = 0; imm < 8; imm++)
13449 if (micromips_imm_b_map[imm] == ep.X_add_number)
13450 break;
13451 if (imm >= 8)
13452 break;
13453
13454 INSERT_OPERAND (1, IMMB, *ip, imm);
13455 }
13456 s = expr_end;
13457 continue;
13458
13459 case 'C':
13460 {
13461 bfd_reloc_code_real_type r[3];
13462 expressionS ep;
13463 int imm;
13464
13465 if (my_getSmallExpression (&ep, r, s) > 0
13466 || ep.X_op != O_constant)
13467 break;
13468
13469 for (imm = 0; imm < 16; imm++)
13470 if (micromips_imm_c_map[imm] == ep.X_add_number)
13471 break;
13472 if (imm >= 16)
13473 break;
13474
13475 INSERT_OPERAND (1, IMMC, *ip, imm);
13476 }
13477 s = expr_end;
13478 continue;
13479
13480 case 'D': /* pc relative offset */
13481 case 'E': /* pc relative offset */
13482 my_getExpression (&offset_expr, s);
13483 if (offset_expr.X_op == O_register)
13484 break;
13485
40209cad
MR
13486 if (!forced_insn_length)
13487 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
13488 else if (c == 'D')
13489 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
13490 else
13491 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
df58fc94
RS
13492 s = expr_end;
13493 continue;
13494
13495 case 'F':
13496 {
13497 bfd_reloc_code_real_type r[3];
13498 expressionS ep;
13499 int imm;
13500
13501 if (my_getSmallExpression (&ep, r, s) > 0
13502 || !expr_const_in_range (&ep, 0, 16, 0))
13503 break;
13504
13505 imm = ep.X_add_number;
13506 INSERT_OPERAND (1, IMMF, *ip, imm);
13507 }
13508 s = expr_end;
13509 continue;
13510
13511 case 'G':
13512 {
13513 bfd_reloc_code_real_type r[3];
13514 expressionS ep;
13515 int imm;
13516
13517 /* Check whether there is only a single bracketed
13518 expression left. If so, it must be the base register
13519 and the constant must be zero. */
13520 if (*s == '(' && strchr (s + 1, '(') == 0)
13521 {
13522 INSERT_OPERAND (1, IMMG, *ip, 0);
13523 continue;
13524 }
13525
13526 if (my_getSmallExpression (&ep, r, s) > 0
13527 || !expr_const_in_range (&ep, -1, 15, 0))
13528 break;
13529
13530 imm = ep.X_add_number & 15;
13531 INSERT_OPERAND (1, IMMG, *ip, imm);
13532 }
13533 s = expr_end;
13534 continue;
13535
13536 case 'H':
13537 {
13538 bfd_reloc_code_real_type r[3];
13539 expressionS ep;
13540 int imm;
13541
13542 /* Check whether there is only a single bracketed
13543 expression left. If so, it must be the base register
13544 and the constant must be zero. */
13545 if (*s == '(' && strchr (s + 1, '(') == 0)
13546 {
13547 INSERT_OPERAND (1, IMMH, *ip, 0);
13548 continue;
13549 }
13550
13551 if (my_getSmallExpression (&ep, r, s) > 0
13552 || !expr_const_in_range (&ep, 0, 16, 1))
13553 break;
13554
13555 imm = ep.X_add_number >> 1;
13556 INSERT_OPERAND (1, IMMH, *ip, imm);
13557 }
13558 s = expr_end;
13559 continue;
13560
13561 case 'I':
13562 {
13563 bfd_reloc_code_real_type r[3];
13564 expressionS ep;
13565 int imm;
13566
13567 if (my_getSmallExpression (&ep, r, s) > 0
13568 || !expr_const_in_range (&ep, -1, 127, 0))
13569 break;
13570
13571 imm = ep.X_add_number & 127;
13572 INSERT_OPERAND (1, IMMI, *ip, imm);
13573 }
13574 s = expr_end;
13575 continue;
13576
13577 case 'J':
13578 {
13579 bfd_reloc_code_real_type r[3];
13580 expressionS ep;
13581 int imm;
13582
13583 /* Check whether there is only a single bracketed
13584 expression left. If so, it must be the base register
13585 and the constant must be zero. */
13586 if (*s == '(' && strchr (s + 1, '(') == 0)
13587 {
13588 INSERT_OPERAND (1, IMMJ, *ip, 0);
13589 continue;
13590 }
13591
13592 if (my_getSmallExpression (&ep, r, s) > 0
13593 || !expr_const_in_range (&ep, 0, 16, 2))
13594 break;
13595
13596 imm = ep.X_add_number >> 2;
13597 INSERT_OPERAND (1, IMMJ, *ip, imm);
13598 }
13599 s = expr_end;
13600 continue;
13601
13602 case 'L':
13603 {
13604 bfd_reloc_code_real_type r[3];
13605 expressionS ep;
13606 int imm;
13607
13608 /* Check whether there is only a single bracketed
13609 expression left. If so, it must be the base register
13610 and the constant must be zero. */
13611 if (*s == '(' && strchr (s + 1, '(') == 0)
13612 {
13613 INSERT_OPERAND (1, IMML, *ip, 0);
13614 continue;
13615 }
13616
13617 if (my_getSmallExpression (&ep, r, s) > 0
13618 || !expr_const_in_range (&ep, 0, 16, 0))
13619 break;
13620
13621 imm = ep.X_add_number;
13622 INSERT_OPERAND (1, IMML, *ip, imm);
13623 }
13624 s = expr_end;
13625 continue;
13626
13627 case 'M':
13628 {
13629 bfd_reloc_code_real_type r[3];
13630 expressionS ep;
13631 int imm;
13632
13633 if (my_getSmallExpression (&ep, r, s) > 0
13634 || !expr_const_in_range (&ep, 1, 9, 0))
13635 break;
13636
13637 imm = ep.X_add_number & 7;
13638 INSERT_OPERAND (1, IMMM, *ip, imm);
13639 }
13640 s = expr_end;
13641 continue;
13642
13643 case 'N': /* Register list for lwm and swm. */
13644 {
13645 /* A comma-separated list of registers and/or
13646 dash-separated contiguous ranges including
13647 both ra and a set of one or more registers
13648 starting at s0 up to s3 which have to be
13649 consecutive, e.g.:
13650
13651 s0, ra
13652 s0, s1, ra, s2, s3
13653 s0-s2, ra
13654
13655 and any permutations of these. */
13656 unsigned int reglist;
13657 int imm;
13658
13659 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13660 break;
13661
13662 if ((reglist & 0xfff1ffff) != 0x80010000)
13663 break;
13664
13665 reglist = (reglist >> 17) & 7;
13666 reglist += 1;
13667 if ((reglist & -reglist) != reglist)
13668 break;
252b5132 13669
df58fc94
RS
13670 imm = ffs (reglist) - 1;
13671 INSERT_OPERAND (1, IMMN, *ip, imm);
13672 }
13673 continue;
252b5132 13674
df58fc94
RS
13675 case 'O': /* sdbbp 4-bit code. */
13676 {
13677 bfd_reloc_code_real_type r[3];
13678 expressionS ep;
13679 int imm;
13680
13681 if (my_getSmallExpression (&ep, r, s) > 0
13682 || !expr_const_in_range (&ep, 0, 16, 0))
13683 break;
13684
13685 imm = ep.X_add_number;
13686 INSERT_OPERAND (1, IMMO, *ip, imm);
252b5132 13687 }
df58fc94
RS
13688 s = expr_end;
13689 continue;
252b5132 13690
df58fc94
RS
13691 case 'P':
13692 {
13693 bfd_reloc_code_real_type r[3];
13694 expressionS ep;
13695 int imm;
5e0116d5 13696
df58fc94
RS
13697 if (my_getSmallExpression (&ep, r, s) > 0
13698 || !expr_const_in_range (&ep, 0, 32, 2))
13699 break;
5e0116d5 13700
df58fc94
RS
13701 imm = ep.X_add_number >> 2;
13702 INSERT_OPERAND (1, IMMP, *ip, imm);
13703 }
13704 s = expr_end;
13705 continue;
5e0116d5 13706
df58fc94
RS
13707 case 'Q':
13708 {
13709 bfd_reloc_code_real_type r[3];
13710 expressionS ep;
13711 int imm;
5e0116d5 13712
df58fc94
RS
13713 if (my_getSmallExpression (&ep, r, s) > 0
13714 || !expr_const_in_range (&ep, -0x400000, 0x400000, 2))
13715 break;
252b5132 13716
df58fc94
RS
13717 imm = ep.X_add_number >> 2;
13718 INSERT_OPERAND (1, IMMQ, *ip, imm);
13719 }
13720 s = expr_end;
13721 continue;
4614d845 13722
df58fc94
RS
13723 case 'U':
13724 {
13725 bfd_reloc_code_real_type r[3];
13726 expressionS ep;
13727 int imm;
13728
13729 /* Check whether there is only a single bracketed
13730 expression left. If so, it must be the base register
13731 and the constant must be zero. */
13732 if (*s == '(' && strchr (s + 1, '(') == 0)
13733 {
13734 INSERT_OPERAND (1, IMMU, *ip, 0);
13735 continue;
13736 }
13737
13738 if (my_getSmallExpression (&ep, r, s) > 0
13739 || !expr_const_in_range (&ep, 0, 32, 2))
13740 break;
13741
13742 imm = ep.X_add_number >> 2;
13743 INSERT_OPERAND (1, IMMU, *ip, imm);
13744 }
13745 s = expr_end;
5e0116d5 13746 continue;
252b5132 13747
df58fc94
RS
13748 case 'W':
13749 {
13750 bfd_reloc_code_real_type r[3];
13751 expressionS ep;
13752 int imm;
252b5132 13753
df58fc94
RS
13754 if (my_getSmallExpression (&ep, r, s) > 0
13755 || !expr_const_in_range (&ep, 0, 64, 2))
13756 break;
252b5132 13757
df58fc94
RS
13758 imm = ep.X_add_number >> 2;
13759 INSERT_OPERAND (1, IMMW, *ip, imm);
13760 }
13761 s = expr_end;
13762 continue;
252b5132 13763
df58fc94
RS
13764 case 'X':
13765 {
13766 bfd_reloc_code_real_type r[3];
13767 expressionS ep;
13768 int imm;
252b5132 13769
df58fc94
RS
13770 if (my_getSmallExpression (&ep, r, s) > 0
13771 || !expr_const_in_range (&ep, -8, 8, 0))
13772 break;
252b5132 13773
df58fc94
RS
13774 imm = ep.X_add_number;
13775 INSERT_OPERAND (1, IMMX, *ip, imm);
13776 }
13777 s = expr_end;
13778 continue;
252b5132 13779
df58fc94
RS
13780 case 'Y':
13781 {
13782 bfd_reloc_code_real_type r[3];
13783 expressionS ep;
13784 int imm;
156c2f8b 13785
df58fc94
RS
13786 if (my_getSmallExpression (&ep, r, s) > 0
13787 || expr_const_in_range (&ep, -2, 2, 2)
13788 || !expr_const_in_range (&ep, -258, 258, 2))
13789 break;
156c2f8b 13790
df58fc94
RS
13791 imm = ep.X_add_number >> 2;
13792 imm = ((imm >> 1) & ~0xff) | (imm & 0xff);
13793 INSERT_OPERAND (1, IMMY, *ip, imm);
13794 }
13795 s = expr_end;
13796 continue;
60b63b72 13797
df58fc94
RS
13798 case 'Z':
13799 {
13800 bfd_reloc_code_real_type r[3];
13801 expressionS ep;
13802
13803 if (my_getSmallExpression (&ep, r, s) > 0
13804 || !expr_const_in_range (&ep, 0, 1, 0))
13805 break;
13806 }
13807 s = expr_end;
13808 continue;
13809
13810 default:
13811 as_bad (_("Internal error: bad microMIPS opcode "
13812 "(unknown extension operand type `m%c'): %s %s"),
13813 *args, insn->name, insn->args);
13814 /* Further processing is fruitless. */
13815 return;
60b63b72 13816 }
df58fc94 13817 break;
60b63b72 13818
df58fc94
RS
13819 case 'n': /* Register list for 32-bit lwm and swm. */
13820 gas_assert (mips_opts.micromips);
13821 {
13822 /* A comma-separated list of registers and/or
13823 dash-separated contiguous ranges including
13824 at least one of ra and a set of one or more
13825 registers starting at s0 up to s7 and then
13826 s8 which have to be consecutive, e.g.:
13827
13828 ra
13829 s0
13830 ra, s0, s1, s2
13831 s0-s8
13832 s0-s5, ra
13833
13834 and any permutations of these. */
13835 unsigned int reglist;
13836 int imm;
13837 int ra;
13838
13839 if (!reglist_lookup (&s, RTYPE_NUM | RTYPE_GP, &reglist))
13840 break;
13841
13842 if ((reglist & 0x3f00ffff) != 0)
13843 break;
13844
13845 ra = (reglist >> 27) & 0x10;
13846 reglist = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
13847 reglist += 1;
13848 if ((reglist & -reglist) != reglist)
13849 break;
13850
13851 imm = (ffs (reglist) - 1) | ra;
13852 INSERT_OPERAND (1, RT, *ip, imm);
13853 imm_expr.X_op = O_absent;
13854 }
60b63b72
RS
13855 continue;
13856
df58fc94
RS
13857 case '|': /* 4-bit trap code. */
13858 gas_assert (mips_opts.micromips);
60b63b72
RS
13859 my_getExpression (&imm_expr, s);
13860 check_absolute_expr (ip, &imm_expr);
60b63b72 13861 if ((unsigned long) imm_expr.X_add_number
df58fc94
RS
13862 > MICROMIPSOP_MASK_TRAP)
13863 as_bad (_("Trap code (%lu) for %s not in 0..15 range"),
13864 (unsigned long) imm_expr.X_add_number,
13865 ip->insn_mo->name);
13866 INSERT_OPERAND (1, TRAP, *ip, imm_expr.X_add_number);
60b63b72
RS
13867 imm_expr.X_op = O_absent;
13868 s = expr_end;
13869 continue;
13870
252b5132 13871 default:
f71d0d44 13872 as_bad (_("Bad char = '%c'\n"), *args);
b37df7c4 13873 abort ();
252b5132
RH
13874 }
13875 break;
13876 }
13877 /* Args don't match. */
df58fc94
RS
13878 s = argsStart;
13879 insn_error = _("Illegal operands");
13880 if (insn + 1 < past && !strcmp (insn->name, insn[1].name))
252b5132
RH
13881 {
13882 ++insn;
252b5132
RH
13883 continue;
13884 }
df58fc94
RS
13885 else if (wrong_delay_slot_insns && need_delay_slot_ok)
13886 {
13887 gas_assert (firstinsn);
13888 need_delay_slot_ok = FALSE;
13889 past = insn + 1;
13890 insn = firstinsn;
13891 continue;
13892 }
252b5132
RH
13893 return;
13894 }
13895}
13896
0499d65b
TS
13897#define SKIP_SPACE_TABS(S) { while (*(S) == ' ' || *(S) == '\t') ++(S); }
13898
252b5132
RH
13899/* This routine assembles an instruction into its binary format when
13900 assembling for the mips16. As a side effect, it sets one of the
df58fc94
RS
13901 global variables imm_reloc or offset_reloc to the type of relocation
13902 to do if one of the operands is an address expression. It also sets
13903 forced_insn_length to the resulting instruction size in bytes if the
13904 user explicitly requested a small or extended instruction. */
252b5132
RH
13905
13906static void
17a2f251 13907mips16_ip (char *str, struct mips_cl_insn *ip)
252b5132
RH
13908{
13909 char *s;
13910 const char *args;
13911 struct mips_opcode *insn;
13912 char *argsstart;
13913 unsigned int regno;
13914 unsigned int lastregno = 0;
13915 char *s_reset;
d6f16593 13916 size_t i;
252b5132
RH
13917
13918 insn_error = NULL;
13919
df58fc94 13920 forced_insn_length = 0;
252b5132 13921
3882b010 13922 for (s = str; ISLOWER (*s); ++s)
252b5132
RH
13923 ;
13924 switch (*s)
13925 {
13926 case '\0':
13927 break;
13928
13929 case ' ':
13930 *s++ = '\0';
13931 break;
13932
13933 case '.':
13934 if (s[1] == 't' && s[2] == ' ')
13935 {
13936 *s = '\0';
df58fc94 13937 forced_insn_length = 2;
252b5132
RH
13938 s += 3;
13939 break;
13940 }
13941 else if (s[1] == 'e' && s[2] == ' ')
13942 {
13943 *s = '\0';
df58fc94 13944 forced_insn_length = 4;
252b5132
RH
13945 s += 3;
13946 break;
13947 }
13948 /* Fall through. */
13949 default:
13950 insn_error = _("unknown opcode");
13951 return;
13952 }
13953
df58fc94
RS
13954 if (mips_opts.noautoextend && !forced_insn_length)
13955 forced_insn_length = 2;
252b5132
RH
13956
13957 if ((insn = (struct mips_opcode *) hash_find (mips16_op_hash, str)) == NULL)
13958 {
13959 insn_error = _("unrecognized opcode");
13960 return;
13961 }
13962
13963 argsstart = s;
13964 for (;;)
13965 {
9b3f89ee
TS
13966 bfd_boolean ok;
13967
9c2799c2 13968 gas_assert (strcmp (insn->name, str) == 0);
252b5132 13969
037b32b9 13970 ok = is_opcode_valid_16 (insn);
9b3f89ee
TS
13971 if (! ok)
13972 {
13973 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes]
13974 && strcmp (insn->name, insn[1].name) == 0)
13975 {
13976 ++insn;
13977 continue;
13978 }
13979 else
13980 {
13981 if (!insn_error)
13982 {
13983 static char buf[100];
13984 sprintf (buf,
7bd942df 13985 _("Opcode not supported on this processor: %s (%s)"),
9b3f89ee
TS
13986 mips_cpu_info_from_arch (mips_opts.arch)->name,
13987 mips_cpu_info_from_isa (mips_opts.isa)->name);
13988 insn_error = buf;
13989 }
13990 return;
13991 }
13992 }
13993
1e915849 13994 create_insn (ip, insn);
252b5132 13995 imm_expr.X_op = O_absent;
f6688943
TS
13996 imm_reloc[0] = BFD_RELOC_UNUSED;
13997 imm_reloc[1] = BFD_RELOC_UNUSED;
13998 imm_reloc[2] = BFD_RELOC_UNUSED;
5f74bc13 13999 imm2_expr.X_op = O_absent;
252b5132 14000 offset_expr.X_op = O_absent;
f6688943
TS
14001 offset_reloc[0] = BFD_RELOC_UNUSED;
14002 offset_reloc[1] = BFD_RELOC_UNUSED;
14003 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
14004 for (args = insn->args; 1; ++args)
14005 {
14006 int c;
14007
14008 if (*s == ' ')
14009 ++s;
14010
14011 /* In this switch statement we call break if we did not find
14012 a match, continue if we did find a match, or return if we
14013 are done. */
14014
14015 c = *args;
14016 switch (c)
14017 {
14018 case '\0':
14019 if (*s == '\0')
14020 {
b886a2ab
RS
14021 offsetT value;
14022
252b5132
RH
14023 /* Stuff the immediate value in now, if we can. */
14024 if (imm_expr.X_op == O_constant
f6688943 14025 && *imm_reloc > BFD_RELOC_UNUSED
b886a2ab
RS
14026 && insn->pinfo != INSN_MACRO
14027 && calculate_reloc (*offset_reloc,
14028 imm_expr.X_add_number, &value))
252b5132 14029 {
c4e7957c 14030 mips16_immed (NULL, 0, *imm_reloc - BFD_RELOC_UNUSED,
b886a2ab 14031 *offset_reloc, value, forced_insn_length,
43c0598f 14032 &ip->insn_opcode);
252b5132 14033 imm_expr.X_op = O_absent;
f6688943 14034 *imm_reloc = BFD_RELOC_UNUSED;
43c0598f 14035 *offset_reloc = BFD_RELOC_UNUSED;
252b5132
RH
14036 }
14037
14038 return;
14039 }
14040 break;
14041
14042 case ',':
14043 if (*s++ == c)
14044 continue;
14045 s--;
14046 switch (*++args)
14047 {
14048 case 'v':
bf12938e 14049 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132
RH
14050 continue;
14051 case 'w':
bf12938e 14052 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
14053 continue;
14054 }
14055 break;
14056
14057 case '(':
14058 case ')':
14059 if (*s++ == c)
14060 continue;
14061 break;
14062
14063 case 'v':
14064 case 'w':
14065 if (s[0] != '$')
14066 {
14067 if (c == 'v')
bf12938e 14068 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
252b5132 14069 else
bf12938e 14070 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
252b5132
RH
14071 ++args;
14072 continue;
14073 }
14074 /* Fall through. */
14075 case 'x':
14076 case 'y':
14077 case 'z':
14078 case 'Z':
14079 case '0':
14080 case 'S':
14081 case 'R':
14082 case 'X':
14083 case 'Y':
707bfff6
TS
14084 s_reset = s;
14085 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &regno))
252b5132 14086 {
707bfff6 14087 if (c == 'v' || c == 'w')
85b51719 14088 {
707bfff6 14089 if (c == 'v')
a9e24354 14090 MIPS16_INSERT_OPERAND (RX, *ip, lastregno);
707bfff6 14091 else
a9e24354 14092 MIPS16_INSERT_OPERAND (RY, *ip, lastregno);
707bfff6
TS
14093 ++args;
14094 continue;
85b51719 14095 }
707bfff6 14096 break;
252b5132
RH
14097 }
14098
14099 if (*s == ' ')
14100 ++s;
14101 if (args[1] != *s)
14102 {
14103 if (c == 'v' || c == 'w')
14104 {
14105 regno = mips16_to_32_reg_map[lastregno];
14106 s = s_reset;
f9419b05 14107 ++args;
252b5132
RH
14108 }
14109 }
14110
14111 switch (c)
14112 {
14113 case 'x':
14114 case 'y':
14115 case 'z':
14116 case 'v':
14117 case 'w':
14118 case 'Z':
14119 regno = mips32_to_16_reg_map[regno];
14120 break;
14121
14122 case '0':
14123 if (regno != 0)
14124 regno = ILLEGAL_REG;
14125 break;
14126
14127 case 'S':
14128 if (regno != SP)
14129 regno = ILLEGAL_REG;
14130 break;
14131
14132 case 'R':
14133 if (regno != RA)
14134 regno = ILLEGAL_REG;
14135 break;
14136
14137 case 'X':
14138 case 'Y':
741fe287
MR
14139 if (regno == AT && mips_opts.at)
14140 {
14141 if (mips_opts.at == ATREG)
14142 as_warn (_("used $at without \".set noat\""));
14143 else
14144 as_warn (_("used $%u with \".set at=$%u\""),
14145 regno, mips_opts.at);
14146 }
252b5132
RH
14147 break;
14148
14149 default:
b37df7c4 14150 abort ();
252b5132
RH
14151 }
14152
14153 if (regno == ILLEGAL_REG)
14154 break;
14155
14156 switch (c)
14157 {
14158 case 'x':
14159 case 'v':
bf12938e 14160 MIPS16_INSERT_OPERAND (RX, *ip, regno);
252b5132
RH
14161 break;
14162 case 'y':
14163 case 'w':
bf12938e 14164 MIPS16_INSERT_OPERAND (RY, *ip, regno);
252b5132
RH
14165 break;
14166 case 'z':
bf12938e 14167 MIPS16_INSERT_OPERAND (RZ, *ip, regno);
252b5132
RH
14168 break;
14169 case 'Z':
bf12938e 14170 MIPS16_INSERT_OPERAND (MOVE32Z, *ip, regno);
252b5132
RH
14171 case '0':
14172 case 'S':
14173 case 'R':
14174 break;
14175 case 'X':
bf12938e 14176 MIPS16_INSERT_OPERAND (REGR32, *ip, regno);
252b5132
RH
14177 break;
14178 case 'Y':
14179 regno = ((regno & 7) << 2) | ((regno & 0x18) >> 3);
bf12938e 14180 MIPS16_INSERT_OPERAND (REG32R, *ip, regno);
252b5132
RH
14181 break;
14182 default:
b37df7c4 14183 abort ();
252b5132
RH
14184 }
14185
14186 lastregno = regno;
14187 continue;
14188
14189 case 'P':
14190 if (strncmp (s, "$pc", 3) == 0)
14191 {
14192 s += 3;
14193 continue;
14194 }
14195 break;
14196
252b5132
RH
14197 case '5':
14198 case 'H':
14199 case 'W':
14200 case 'D':
14201 case 'j':
252b5132
RH
14202 case 'V':
14203 case 'C':
14204 case 'U':
14205 case 'k':
14206 case 'K':
d6f16593
MR
14207 i = my_getSmallExpression (&imm_expr, imm_reloc, s);
14208 if (i > 0)
252b5132 14209 {
d6f16593 14210 if (imm_expr.X_op != O_constant)
252b5132 14211 {
df58fc94 14212 forced_insn_length = 4;
5c04167a 14213 ip->insn_opcode |= MIPS16_EXTEND;
252b5132 14214 }
d6f16593
MR
14215 else
14216 {
14217 /* We need to relax this instruction. */
14218 *offset_reloc = *imm_reloc;
14219 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
14220 }
14221 s = expr_end;
14222 continue;
252b5132 14223 }
d6f16593
MR
14224 *imm_reloc = BFD_RELOC_UNUSED;
14225 /* Fall through. */
14226 case '<':
14227 case '>':
14228 case '[':
14229 case ']':
14230 case '4':
14231 case '8':
14232 my_getExpression (&imm_expr, s);
252b5132
RH
14233 if (imm_expr.X_op == O_register)
14234 {
14235 /* What we thought was an expression turned out to
14236 be a register. */
14237
14238 if (s[0] == '(' && args[1] == '(')
14239 {
14240 /* It looks like the expression was omitted
14241 before a register indirection, which means
14242 that the expression is implicitly zero. We
14243 still set up imm_expr, so that we handle
14244 explicit extensions correctly. */
14245 imm_expr.X_op = O_constant;
14246 imm_expr.X_add_number = 0;
f6688943 14247 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14248 continue;
14249 }
14250
14251 break;
14252 }
14253
14254 /* We need to relax this instruction. */
f6688943 14255 *imm_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14256 s = expr_end;
14257 continue;
14258
14259 case 'p':
14260 case 'q':
14261 case 'A':
14262 case 'B':
14263 case 'E':
14264 /* We use offset_reloc rather than imm_reloc for the PC
14265 relative operands. This lets macros with both
14266 immediate and address operands work correctly. */
14267 my_getExpression (&offset_expr, s);
14268
14269 if (offset_expr.X_op == O_register)
14270 break;
14271
14272 /* We need to relax this instruction. */
f6688943 14273 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
252b5132
RH
14274 s = expr_end;
14275 continue;
14276
14277 case '6': /* break code */
14278 my_getExpression (&imm_expr, s);
14279 check_absolute_expr (ip, &imm_expr);
14280 if ((unsigned long) imm_expr.X_add_number > 63)
bf12938e
RS
14281 as_warn (_("Invalid value for `%s' (%lu)"),
14282 ip->insn_mo->name,
14283 (unsigned long) imm_expr.X_add_number);
14284 MIPS16_INSERT_OPERAND (IMM6, *ip, imm_expr.X_add_number);
252b5132
RH
14285 imm_expr.X_op = O_absent;
14286 s = expr_end;
14287 continue;
14288
14289 case 'a': /* 26 bit address */
27c5c572 14290 case 'i':
252b5132
RH
14291 my_getExpression (&offset_expr, s);
14292 s = expr_end;
f6688943 14293 *offset_reloc = BFD_RELOC_MIPS16_JMP;
252b5132
RH
14294 ip->insn_opcode <<= 16;
14295 continue;
14296
14297 case 'l': /* register list for entry macro */
14298 case 'L': /* register list for exit macro */
14299 {
14300 int mask;
14301
14302 if (c == 'l')
14303 mask = 0;
14304 else
14305 mask = 7 << 3;
14306 while (*s != '\0')
14307 {
707bfff6 14308 unsigned int freg, reg1, reg2;
252b5132
RH
14309
14310 while (*s == ' ' || *s == ',')
14311 ++s;
707bfff6 14312 if (reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
252b5132 14313 freg = 0;
707bfff6
TS
14314 else if (reg_lookup (&s, RTYPE_FPU, &reg1))
14315 freg = 1;
252b5132
RH
14316 else
14317 {
707bfff6
TS
14318 as_bad (_("can't parse register list"));
14319 break;
252b5132
RH
14320 }
14321 if (*s == ' ')
14322 ++s;
14323 if (*s != '-')
14324 reg2 = reg1;
14325 else
14326 {
14327 ++s;
707bfff6
TS
14328 if (!reg_lookup (&s, freg ? RTYPE_FPU
14329 : (RTYPE_GP | RTYPE_NUM), &reg2))
252b5132 14330 {
707bfff6
TS
14331 as_bad (_("invalid register list"));
14332 break;
252b5132
RH
14333 }
14334 }
14335 if (freg && reg1 == 0 && reg2 == 0 && c == 'L')
14336 {
14337 mask &= ~ (7 << 3);
14338 mask |= 5 << 3;
14339 }
14340 else if (freg && reg1 == 0 && reg2 == 1 && c == 'L')
14341 {
14342 mask &= ~ (7 << 3);
14343 mask |= 6 << 3;
14344 }
14345 else if (reg1 == 4 && reg2 >= 4 && reg2 <= 7 && c != 'L')
14346 mask |= (reg2 - 3) << 3;
14347 else if (reg1 == 16 && reg2 >= 16 && reg2 <= 17)
14348 mask |= (reg2 - 15) << 1;
f9419b05 14349 else if (reg1 == RA && reg2 == RA)
252b5132
RH
14350 mask |= 1;
14351 else
14352 {
14353 as_bad (_("invalid register list"));
14354 break;
14355 }
14356 }
14357 /* The mask is filled in in the opcode table for the
14358 benefit of the disassembler. We remove it before
14359 applying the actual mask. */
14360 ip->insn_opcode &= ~ ((7 << 3) << MIPS16OP_SH_IMM6);
14361 ip->insn_opcode |= mask << MIPS16OP_SH_IMM6;
14362 }
14363 continue;
14364
0499d65b
TS
14365 case 'm': /* Register list for save insn. */
14366 case 'M': /* Register list for restore insn. */
14367 {
5c04167a 14368 int opcode = ip->insn_opcode;
0499d65b 14369 int framesz = 0, seen_framesz = 0;
91d6fa6a 14370 int nargs = 0, statics = 0, sregs = 0;
0499d65b
TS
14371
14372 while (*s != '\0')
14373 {
14374 unsigned int reg1, reg2;
14375
14376 SKIP_SPACE_TABS (s);
14377 while (*s == ',')
14378 ++s;
14379 SKIP_SPACE_TABS (s);
14380
14381 my_getExpression (&imm_expr, s);
14382 if (imm_expr.X_op == O_constant)
14383 {
14384 /* Handle the frame size. */
14385 if (seen_framesz)
14386 {
14387 as_bad (_("more than one frame size in list"));
14388 break;
14389 }
14390 seen_framesz = 1;
14391 framesz = imm_expr.X_add_number;
14392 imm_expr.X_op = O_absent;
14393 s = expr_end;
14394 continue;
14395 }
14396
707bfff6 14397 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg1))
0499d65b
TS
14398 {
14399 as_bad (_("can't parse register list"));
14400 break;
14401 }
0499d65b 14402
707bfff6
TS
14403 while (*s == ' ')
14404 ++s;
14405
0499d65b
TS
14406 if (*s != '-')
14407 reg2 = reg1;
14408 else
14409 {
14410 ++s;
707bfff6
TS
14411 if (! reg_lookup (&s, RTYPE_GP | RTYPE_NUM, &reg2)
14412 || reg2 < reg1)
0499d65b
TS
14413 {
14414 as_bad (_("can't parse register list"));
14415 break;
14416 }
0499d65b
TS
14417 }
14418
14419 while (reg1 <= reg2)
14420 {
14421 if (reg1 >= 4 && reg1 <= 7)
14422 {
3a93f742 14423 if (!seen_framesz)
0499d65b 14424 /* args $a0-$a3 */
91d6fa6a 14425 nargs |= 1 << (reg1 - 4);
0499d65b
TS
14426 else
14427 /* statics $a0-$a3 */
14428 statics |= 1 << (reg1 - 4);
14429 }
14430 else if ((reg1 >= 16 && reg1 <= 23) || reg1 == 30)
14431 {
14432 /* $s0-$s8 */
14433 sregs |= 1 << ((reg1 == 30) ? 8 : (reg1 - 16));
14434 }
14435 else if (reg1 == 31)
14436 {
14437 /* Add $ra to insn. */
14438 opcode |= 0x40;
14439 }
14440 else
14441 {
14442 as_bad (_("unexpected register in list"));
14443 break;
14444 }
14445 if (++reg1 == 24)
14446 reg1 = 30;
14447 }
14448 }
14449
14450 /* Encode args/statics combination. */
91d6fa6a 14451 if (nargs & statics)
0499d65b 14452 as_bad (_("arg/static registers overlap"));
91d6fa6a 14453 else if (nargs == 0xf)
0499d65b
TS
14454 /* All $a0-$a3 are args. */
14455 opcode |= MIPS16_ALL_ARGS << 16;
14456 else if (statics == 0xf)
14457 /* All $a0-$a3 are statics. */
14458 opcode |= MIPS16_ALL_STATICS << 16;
14459 else
14460 {
14461 int narg = 0, nstat = 0;
14462
14463 /* Count arg registers. */
91d6fa6a 14464 while (nargs & 0x1)
0499d65b 14465 {
91d6fa6a 14466 nargs >>= 1;
0499d65b
TS
14467 narg++;
14468 }
91d6fa6a 14469 if (nargs != 0)
0499d65b
TS
14470 as_bad (_("invalid arg register list"));
14471
14472 /* Count static registers. */
14473 while (statics & 0x8)
14474 {
14475 statics = (statics << 1) & 0xf;
14476 nstat++;
14477 }
14478 if (statics != 0)
14479 as_bad (_("invalid static register list"));
14480
14481 /* Encode args/statics. */
14482 opcode |= ((narg << 2) | nstat) << 16;
14483 }
14484
14485 /* Encode $s0/$s1. */
14486 if (sregs & (1 << 0)) /* $s0 */
14487 opcode |= 0x20;
14488 if (sregs & (1 << 1)) /* $s1 */
14489 opcode |= 0x10;
14490 sregs >>= 2;
14491
14492 if (sregs != 0)
14493 {
14494 /* Count regs $s2-$s8. */
14495 int nsreg = 0;
14496 while (sregs & 1)
14497 {
14498 sregs >>= 1;
14499 nsreg++;
14500 }
14501 if (sregs != 0)
14502 as_bad (_("invalid static register list"));
14503 /* Encode $s2-$s8. */
14504 opcode |= nsreg << 24;
14505 }
14506
14507 /* Encode frame size. */
14508 if (!seen_framesz)
14509 as_bad (_("missing frame size"));
14510 else if ((framesz & 7) != 0 || framesz < 0
14511 || framesz > 0xff * 8)
14512 as_bad (_("invalid frame size"));
14513 else if (framesz != 128 || (opcode >> 16) != 0)
14514 {
14515 framesz /= 8;
14516 opcode |= (((framesz & 0xf0) << 16)
14517 | (framesz & 0x0f));
14518 }
14519
14520 /* Finally build the instruction. */
14521 if ((opcode >> 16) != 0 || framesz == 0)
5c04167a
RS
14522 opcode |= MIPS16_EXTEND;
14523 ip->insn_opcode = opcode;
0499d65b
TS
14524 }
14525 continue;
14526
252b5132
RH
14527 case 'e': /* extend code */
14528 my_getExpression (&imm_expr, s);
14529 check_absolute_expr (ip, &imm_expr);
14530 if ((unsigned long) imm_expr.X_add_number > 0x7ff)
14531 {
14532 as_warn (_("Invalid value for `%s' (%lu)"),
14533 ip->insn_mo->name,
14534 (unsigned long) imm_expr.X_add_number);
14535 imm_expr.X_add_number &= 0x7ff;
14536 }
14537 ip->insn_opcode |= imm_expr.X_add_number;
14538 imm_expr.X_op = O_absent;
14539 s = expr_end;
14540 continue;
14541
14542 default:
b37df7c4 14543 abort ();
252b5132
RH
14544 }
14545 break;
14546 }
14547
14548 /* Args don't match. */
14549 if (insn + 1 < &mips16_opcodes[bfd_mips16_num_opcodes] &&
14550 strcmp (insn->name, insn[1].name) == 0)
14551 {
14552 ++insn;
14553 s = argsstart;
14554 continue;
14555 }
14556
14557 insn_error = _("illegal operands");
14558
14559 return;
14560 }
14561}
14562
14563/* This structure holds information we know about a mips16 immediate
14564 argument type. */
14565
e972090a
NC
14566struct mips16_immed_operand
14567{
252b5132
RH
14568 /* The type code used in the argument string in the opcode table. */
14569 int type;
14570 /* The number of bits in the short form of the opcode. */
14571 int nbits;
14572 /* The number of bits in the extended form of the opcode. */
14573 int extbits;
14574 /* The amount by which the short form is shifted when it is used;
14575 for example, the sw instruction has a shift count of 2. */
14576 int shift;
14577 /* The amount by which the short form is shifted when it is stored
14578 into the instruction code. */
14579 int op_shift;
14580 /* Non-zero if the short form is unsigned. */
14581 int unsp;
14582 /* Non-zero if the extended form is unsigned. */
14583 int extu;
14584 /* Non-zero if the value is PC relative. */
14585 int pcrel;
14586};
14587
14588/* The mips16 immediate operand types. */
14589
14590static const struct mips16_immed_operand mips16_immed_operands[] =
14591{
14592 { '<', 3, 5, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14593 { '>', 3, 5, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14594 { '[', 3, 6, 0, MIPS16OP_SH_RZ, 1, 1, 0 },
14595 { ']', 3, 6, 0, MIPS16OP_SH_RX, 1, 1, 0 },
14596 { '4', 4, 15, 0, MIPS16OP_SH_IMM4, 0, 0, 0 },
14597 { '5', 5, 16, 0, MIPS16OP_SH_IMM5, 1, 0, 0 },
14598 { 'H', 5, 16, 1, MIPS16OP_SH_IMM5, 1, 0, 0 },
14599 { 'W', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 0 },
14600 { 'D', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 0 },
14601 { 'j', 5, 16, 0, MIPS16OP_SH_IMM5, 0, 0, 0 },
14602 { '8', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 0, 0 },
14603 { 'V', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 0 },
14604 { 'C', 8, 16, 3, MIPS16OP_SH_IMM8, 1, 0, 0 },
14605 { 'U', 8, 16, 0, MIPS16OP_SH_IMM8, 1, 1, 0 },
14606 { 'k', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 0 },
14607 { 'K', 8, 16, 3, MIPS16OP_SH_IMM8, 0, 0, 0 },
14608 { 'p', 8, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14609 { 'q', 11, 16, 0, MIPS16OP_SH_IMM8, 0, 0, 1 },
14610 { 'A', 8, 16, 2, MIPS16OP_SH_IMM8, 1, 0, 1 },
14611 { 'B', 5, 16, 3, MIPS16OP_SH_IMM5, 1, 0, 1 },
14612 { 'E', 5, 16, 2, MIPS16OP_SH_IMM5, 1, 0, 1 }
14613};
14614
14615#define MIPS16_NUM_IMMED \
14616 (sizeof mips16_immed_operands / sizeof mips16_immed_operands[0])
14617
b886a2ab
RS
14618/* Marshal immediate value VAL for an extended MIPS16 instruction.
14619 NBITS is the number of significant bits in VAL. */
14620
14621static unsigned long
14622mips16_immed_extend (offsetT val, unsigned int nbits)
14623{
14624 int extval;
14625 if (nbits == 16)
14626 {
14627 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14628 val &= 0x1f;
14629 }
14630 else if (nbits == 15)
14631 {
14632 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14633 val &= 0xf;
14634 }
14635 else
14636 {
14637 extval = ((val & 0x1f) << 6) | (val & 0x20);
14638 val = 0;
14639 }
14640 return (extval << 16) | val;
14641}
14642
5c04167a
RS
14643/* Install immediate value VAL into MIPS16 instruction *INSN,
14644 extending it if necessary. The instruction in *INSN may
14645 already be extended.
14646
43c0598f
RS
14647 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14648 if none. In the former case, VAL is a 16-bit number with no
14649 defined signedness.
14650
14651 TYPE is the type of the immediate field. USER_INSN_LENGTH
14652 is the length that the user requested, or 0 if none. */
252b5132
RH
14653
14654static void
43c0598f
RS
14655mips16_immed (char *file, unsigned int line, int type,
14656 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14657 unsigned int user_insn_length, unsigned long *insn)
252b5132 14658{
3994f87e 14659 const struct mips16_immed_operand *op;
252b5132 14660 int mintiny, maxtiny;
252b5132
RH
14661
14662 op = mips16_immed_operands;
14663 while (op->type != type)
14664 {
14665 ++op;
9c2799c2 14666 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
14667 }
14668
14669 if (op->unsp)
14670 {
14671 if (type == '<' || type == '>' || type == '[' || type == ']')
14672 {
14673 mintiny = 1;
14674 maxtiny = 1 << op->nbits;
14675 }
14676 else
14677 {
14678 mintiny = 0;
14679 maxtiny = (1 << op->nbits) - 1;
14680 }
43c0598f
RS
14681 if (reloc != BFD_RELOC_UNUSED)
14682 val &= 0xffff;
252b5132
RH
14683 }
14684 else
14685 {
14686 mintiny = - (1 << (op->nbits - 1));
14687 maxtiny = (1 << (op->nbits - 1)) - 1;
43c0598f
RS
14688 if (reloc != BFD_RELOC_UNUSED)
14689 val = SEXT_16BIT (val);
252b5132
RH
14690 }
14691
14692 /* Branch offsets have an implicit 0 in the lowest bit. */
14693 if (type == 'p' || type == 'q')
14694 val /= 2;
14695
14696 if ((val & ((1 << op->shift) - 1)) != 0
14697 || val < (mintiny << op->shift)
14698 || val > (maxtiny << op->shift))
5c04167a
RS
14699 {
14700 /* We need an extended instruction. */
14701 if (user_insn_length == 2)
14702 as_bad_where (file, line, _("invalid unextended operand value"));
14703 else
14704 *insn |= MIPS16_EXTEND;
14705 }
14706 else if (user_insn_length == 4)
14707 {
14708 /* The operand doesn't force an unextended instruction to be extended.
14709 Warn if the user wanted an extended instruction anyway. */
14710 *insn |= MIPS16_EXTEND;
14711 as_warn_where (file, line,
14712 _("extended operand requested but not required"));
14713 }
252b5132 14714
5c04167a 14715 if (mips16_opcode_length (*insn) == 2)
252b5132
RH
14716 {
14717 int insnval;
14718
252b5132
RH
14719 insnval = ((val >> op->shift) & ((1 << op->nbits) - 1));
14720 insnval <<= op->op_shift;
14721 *insn |= insnval;
14722 }
14723 else
14724 {
14725 long minext, maxext;
252b5132 14726
43c0598f 14727 if (reloc == BFD_RELOC_UNUSED)
252b5132 14728 {
43c0598f
RS
14729 if (op->extu)
14730 {
14731 minext = 0;
14732 maxext = (1 << op->extbits) - 1;
14733 }
14734 else
14735 {
14736 minext = - (1 << (op->extbits - 1));
14737 maxext = (1 << (op->extbits - 1)) - 1;
14738 }
14739 if (val < minext || val > maxext)
14740 as_bad_where (file, line,
14741 _("operand value out of range for instruction"));
252b5132 14742 }
252b5132 14743
b886a2ab 14744 *insn |= mips16_immed_extend (val, op->extbits);
252b5132
RH
14745 }
14746}
14747\f
d6f16593 14748struct percent_op_match
ad8d3bb3 14749{
5e0116d5
RS
14750 const char *str;
14751 bfd_reloc_code_real_type reloc;
d6f16593
MR
14752};
14753
14754static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14755{
5e0116d5 14756 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14757 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14758 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14759 {"%call16", BFD_RELOC_MIPS_CALL16},
14760 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14761 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14762 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14763 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14764 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14765 {"%got", BFD_RELOC_MIPS_GOT16},
14766 {"%gp_rel", BFD_RELOC_GPREL16},
14767 {"%half", BFD_RELOC_16},
14768 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14769 {"%higher", BFD_RELOC_MIPS_HIGHER},
14770 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14771 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14772 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14773 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14774 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14775 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14776 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14777 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
5e0116d5 14778 {"%hi", BFD_RELOC_HI16_S}
ad8d3bb3
TS
14779};
14780
d6f16593
MR
14781static const struct percent_op_match mips16_percent_op[] =
14782{
14783 {"%lo", BFD_RELOC_MIPS16_LO16},
14784 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14785 {"%got", BFD_RELOC_MIPS16_GOT16},
14786 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14787 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14788 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14789 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14790 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14791 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14792 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14793 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14794 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14795};
14796
252b5132 14797
5e0116d5
RS
14798/* Return true if *STR points to a relocation operator. When returning true,
14799 move *STR over the operator and store its relocation code in *RELOC.
14800 Leave both *STR and *RELOC alone when returning false. */
14801
14802static bfd_boolean
17a2f251 14803parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14804{
d6f16593
MR
14805 const struct percent_op_match *percent_op;
14806 size_t limit, i;
14807
14808 if (mips_opts.mips16)
14809 {
14810 percent_op = mips16_percent_op;
14811 limit = ARRAY_SIZE (mips16_percent_op);
14812 }
14813 else
14814 {
14815 percent_op = mips_percent_op;
14816 limit = ARRAY_SIZE (mips_percent_op);
14817 }
76b3015f 14818
d6f16593 14819 for (i = 0; i < limit; i++)
5e0116d5 14820 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14821 {
3f98094e
DJ
14822 int len = strlen (percent_op[i].str);
14823
14824 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14825 continue;
14826
5e0116d5
RS
14827 *str += strlen (percent_op[i].str);
14828 *reloc = percent_op[i].reloc;
394f9b3a 14829
5e0116d5
RS
14830 /* Check whether the output BFD supports this relocation.
14831 If not, issue an error and fall back on something safe. */
14832 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14833 {
20203fb9 14834 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14835 percent_op[i].str);
01a3f561 14836 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14837 }
5e0116d5 14838 return TRUE;
394f9b3a 14839 }
5e0116d5 14840 return FALSE;
394f9b3a 14841}
ad8d3bb3 14842
ad8d3bb3 14843
5e0116d5
RS
14844/* Parse string STR as a 16-bit relocatable operand. Store the
14845 expression in *EP and the relocations in the array starting
14846 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14847
01a3f561 14848 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14849
5e0116d5 14850static size_t
17a2f251
TS
14851my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14852 char *str)
ad8d3bb3 14853{
5e0116d5
RS
14854 bfd_reloc_code_real_type reversed_reloc[3];
14855 size_t reloc_index, i;
09b8f35a
RS
14856 int crux_depth, str_depth;
14857 char *crux;
5e0116d5
RS
14858
14859 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14860 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14861 of the main expression and with CRUX_DEPTH containing the number
14862 of open brackets at that point. */
14863 reloc_index = -1;
14864 str_depth = 0;
14865 do
fb1b3232 14866 {
09b8f35a
RS
14867 reloc_index++;
14868 crux = str;
14869 crux_depth = str_depth;
14870
14871 /* Skip over whitespace and brackets, keeping count of the number
14872 of brackets. */
14873 while (*str == ' ' || *str == '\t' || *str == '(')
14874 if (*str++ == '(')
14875 str_depth++;
5e0116d5 14876 }
09b8f35a
RS
14877 while (*str == '%'
14878 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14879 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14880
09b8f35a 14881 my_getExpression (ep, crux);
5e0116d5 14882 str = expr_end;
394f9b3a 14883
5e0116d5 14884 /* Match every open bracket. */
09b8f35a 14885 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14886 if (*str++ == ')')
09b8f35a 14887 crux_depth--;
394f9b3a 14888
09b8f35a 14889 if (crux_depth > 0)
20203fb9 14890 as_bad (_("unclosed '('"));
394f9b3a 14891
5e0116d5 14892 expr_end = str;
252b5132 14893
01a3f561 14894 if (reloc_index != 0)
64bdfcaf
RS
14895 {
14896 prev_reloc_op_frag = frag_now;
14897 for (i = 0; i < reloc_index; i++)
14898 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14899 }
fb1b3232 14900
5e0116d5 14901 return reloc_index;
252b5132
RH
14902}
14903
14904static void
17a2f251 14905my_getExpression (expressionS *ep, char *str)
252b5132
RH
14906{
14907 char *save_in;
14908
14909 save_in = input_line_pointer;
14910 input_line_pointer = str;
14911 expression (ep);
14912 expr_end = input_line_pointer;
14913 input_line_pointer = save_in;
252b5132
RH
14914}
14915
252b5132 14916char *
17a2f251 14917md_atof (int type, char *litP, int *sizeP)
252b5132 14918{
499ac353 14919 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14920}
14921
14922void
17a2f251 14923md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14924{
14925 if (target_big_endian)
14926 number_to_chars_bigendian (buf, val, n);
14927 else
14928 number_to_chars_littleendian (buf, val, n);
14929}
14930\f
e013f690
TS
14931static int support_64bit_objects(void)
14932{
14933 const char **list, **l;
aa3d8fdf 14934 int yes;
e013f690
TS
14935
14936 list = bfd_target_list ();
14937 for (l = list; *l != NULL; l++)
aeffff67
RS
14938 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14939 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14940 break;
aa3d8fdf 14941 yes = (*l != NULL);
e013f690 14942 free (list);
aa3d8fdf 14943 return yes;
e013f690
TS
14944}
14945
316f5878
RS
14946/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14947 NEW_VALUE. Warn if another value was already specified. Note:
14948 we have to defer parsing the -march and -mtune arguments in order
14949 to handle 'from-abi' correctly, since the ABI might be specified
14950 in a later argument. */
14951
14952static void
17a2f251 14953mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14954{
14955 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
14956 as_warn (_("A different %s was already specified, is now %s"),
14957 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14958 new_value);
14959
14960 *string_ptr = new_value;
14961}
14962
252b5132 14963int
17a2f251 14964md_parse_option (int c, char *arg)
252b5132 14965{
c6278170
RS
14966 unsigned int i;
14967
14968 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14969 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14970 {
14971 file_ase_explicit |= mips_set_ase (&mips_ases[i],
14972 c == mips_ases[i].option_on);
14973 return 1;
14974 }
14975
252b5132
RH
14976 switch (c)
14977 {
119d663a
NC
14978 case OPTION_CONSTRUCT_FLOATS:
14979 mips_disable_float_construction = 0;
14980 break;
bdaaa2e1 14981
119d663a
NC
14982 case OPTION_NO_CONSTRUCT_FLOATS:
14983 mips_disable_float_construction = 1;
14984 break;
bdaaa2e1 14985
252b5132
RH
14986 case OPTION_TRAP:
14987 mips_trap = 1;
14988 break;
14989
14990 case OPTION_BREAK:
14991 mips_trap = 0;
14992 break;
14993
14994 case OPTION_EB:
14995 target_big_endian = 1;
14996 break;
14997
14998 case OPTION_EL:
14999 target_big_endian = 0;
15000 break;
15001
15002 case 'O':
4ffff32f
TS
15003 if (arg == NULL)
15004 mips_optimize = 1;
15005 else if (arg[0] == '0')
15006 mips_optimize = 0;
15007 else if (arg[0] == '1')
252b5132
RH
15008 mips_optimize = 1;
15009 else
15010 mips_optimize = 2;
15011 break;
15012
15013 case 'g':
15014 if (arg == NULL)
15015 mips_debug = 2;
15016 else
15017 mips_debug = atoi (arg);
252b5132
RH
15018 break;
15019
15020 case OPTION_MIPS1:
316f5878 15021 file_mips_isa = ISA_MIPS1;
252b5132
RH
15022 break;
15023
15024 case OPTION_MIPS2:
316f5878 15025 file_mips_isa = ISA_MIPS2;
252b5132
RH
15026 break;
15027
15028 case OPTION_MIPS3:
316f5878 15029 file_mips_isa = ISA_MIPS3;
252b5132
RH
15030 break;
15031
15032 case OPTION_MIPS4:
316f5878 15033 file_mips_isa = ISA_MIPS4;
e7af610e
NC
15034 break;
15035
84ea6cf2 15036 case OPTION_MIPS5:
316f5878 15037 file_mips_isa = ISA_MIPS5;
84ea6cf2
NC
15038 break;
15039
e7af610e 15040 case OPTION_MIPS32:
316f5878 15041 file_mips_isa = ISA_MIPS32;
252b5132
RH
15042 break;
15043
af7ee8bf
CD
15044 case OPTION_MIPS32R2:
15045 file_mips_isa = ISA_MIPS32R2;
15046 break;
15047
5f74bc13
CD
15048 case OPTION_MIPS64R2:
15049 file_mips_isa = ISA_MIPS64R2;
15050 break;
15051
84ea6cf2 15052 case OPTION_MIPS64:
316f5878 15053 file_mips_isa = ISA_MIPS64;
84ea6cf2
NC
15054 break;
15055
ec68c924 15056 case OPTION_MTUNE:
316f5878
RS
15057 mips_set_option_string (&mips_tune_string, arg);
15058 break;
ec68c924 15059
316f5878
RS
15060 case OPTION_MARCH:
15061 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
15062 break;
15063
15064 case OPTION_M4650:
316f5878
RS
15065 mips_set_option_string (&mips_arch_string, "4650");
15066 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
15067 break;
15068
15069 case OPTION_NO_M4650:
15070 break;
15071
15072 case OPTION_M4010:
316f5878
RS
15073 mips_set_option_string (&mips_arch_string, "4010");
15074 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
15075 break;
15076
15077 case OPTION_NO_M4010:
15078 break;
15079
15080 case OPTION_M4100:
316f5878
RS
15081 mips_set_option_string (&mips_arch_string, "4100");
15082 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
15083 break;
15084
15085 case OPTION_NO_M4100:
15086 break;
15087
252b5132 15088 case OPTION_M3900:
316f5878
RS
15089 mips_set_option_string (&mips_arch_string, "3900");
15090 mips_set_option_string (&mips_tune_string, "3900");
252b5132 15091 break;
bdaaa2e1 15092
252b5132
RH
15093 case OPTION_NO_M3900:
15094 break;
15095
df58fc94
RS
15096 case OPTION_MICROMIPS:
15097 if (mips_opts.mips16 == 1)
15098 {
15099 as_bad (_("-mmicromips cannot be used with -mips16"));
15100 return 0;
15101 }
15102 mips_opts.micromips = 1;
15103 mips_no_prev_insn ();
15104 break;
15105
15106 case OPTION_NO_MICROMIPS:
15107 mips_opts.micromips = 0;
15108 mips_no_prev_insn ();
15109 break;
15110
252b5132 15111 case OPTION_MIPS16:
df58fc94
RS
15112 if (mips_opts.micromips == 1)
15113 {
15114 as_bad (_("-mips16 cannot be used with -micromips"));
15115 return 0;
15116 }
252b5132 15117 mips_opts.mips16 = 1;
7d10b47d 15118 mips_no_prev_insn ();
252b5132
RH
15119 break;
15120
15121 case OPTION_NO_MIPS16:
15122 mips_opts.mips16 = 0;
7d10b47d 15123 mips_no_prev_insn ();
252b5132
RH
15124 break;
15125
6a32d874
CM
15126 case OPTION_FIX_24K:
15127 mips_fix_24k = 1;
15128 break;
15129
15130 case OPTION_NO_FIX_24K:
15131 mips_fix_24k = 0;
15132 break;
15133
c67a084a
NC
15134 case OPTION_FIX_LOONGSON2F_JUMP:
15135 mips_fix_loongson2f_jump = TRUE;
15136 break;
15137
15138 case OPTION_NO_FIX_LOONGSON2F_JUMP:
15139 mips_fix_loongson2f_jump = FALSE;
15140 break;
15141
15142 case OPTION_FIX_LOONGSON2F_NOP:
15143 mips_fix_loongson2f_nop = TRUE;
15144 break;
15145
15146 case OPTION_NO_FIX_LOONGSON2F_NOP:
15147 mips_fix_loongson2f_nop = FALSE;
15148 break;
15149
d766e8ec
RS
15150 case OPTION_FIX_VR4120:
15151 mips_fix_vr4120 = 1;
60b63b72
RS
15152 break;
15153
d766e8ec
RS
15154 case OPTION_NO_FIX_VR4120:
15155 mips_fix_vr4120 = 0;
60b63b72
RS
15156 break;
15157
7d8e00cf
RS
15158 case OPTION_FIX_VR4130:
15159 mips_fix_vr4130 = 1;
15160 break;
15161
15162 case OPTION_NO_FIX_VR4130:
15163 mips_fix_vr4130 = 0;
15164 break;
15165
d954098f
DD
15166 case OPTION_FIX_CN63XXP1:
15167 mips_fix_cn63xxp1 = TRUE;
15168 break;
15169
15170 case OPTION_NO_FIX_CN63XXP1:
15171 mips_fix_cn63xxp1 = FALSE;
15172 break;
15173
4a6a3df4
AO
15174 case OPTION_RELAX_BRANCH:
15175 mips_relax_branch = 1;
15176 break;
15177
15178 case OPTION_NO_RELAX_BRANCH:
15179 mips_relax_branch = 0;
15180 break;
15181
833794fc
MR
15182 case OPTION_INSN32:
15183 mips_opts.insn32 = TRUE;
15184 break;
15185
15186 case OPTION_NO_INSN32:
15187 mips_opts.insn32 = FALSE;
15188 break;
15189
aa6975fb
ILT
15190 case OPTION_MSHARED:
15191 mips_in_shared = TRUE;
15192 break;
15193
15194 case OPTION_MNO_SHARED:
15195 mips_in_shared = FALSE;
15196 break;
15197
aed1a261
RS
15198 case OPTION_MSYM32:
15199 mips_opts.sym32 = TRUE;
15200 break;
15201
15202 case OPTION_MNO_SYM32:
15203 mips_opts.sym32 = FALSE;
15204 break;
15205
252b5132
RH
15206 /* When generating ELF code, we permit -KPIC and -call_shared to
15207 select SVR4_PIC, and -non_shared to select no PIC. This is
15208 intended to be compatible with Irix 5. */
15209 case OPTION_CALL_SHARED:
252b5132 15210 mips_pic = SVR4_PIC;
143d77c5 15211 mips_abicalls = TRUE;
252b5132
RH
15212 break;
15213
861fb55a 15214 case OPTION_CALL_NONPIC:
861fb55a
DJ
15215 mips_pic = NO_PIC;
15216 mips_abicalls = TRUE;
15217 break;
15218
252b5132 15219 case OPTION_NON_SHARED:
252b5132 15220 mips_pic = NO_PIC;
143d77c5 15221 mips_abicalls = FALSE;
252b5132
RH
15222 break;
15223
44075ae2
TS
15224 /* The -xgot option tells the assembler to use 32 bit offsets
15225 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15226 compatibility. */
15227 case OPTION_XGOT:
15228 mips_big_got = 1;
15229 break;
15230
15231 case 'G':
6caf9ef4
TS
15232 g_switch_value = atoi (arg);
15233 g_switch_seen = 1;
252b5132
RH
15234 break;
15235
34ba82a8
TS
15236 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15237 and -mabi=64. */
252b5132 15238 case OPTION_32:
f3ded42a 15239 mips_abi = O32_ABI;
252b5132
RH
15240 break;
15241
e013f690 15242 case OPTION_N32:
316f5878 15243 mips_abi = N32_ABI;
e013f690 15244 break;
252b5132 15245
e013f690 15246 case OPTION_64:
316f5878 15247 mips_abi = N64_ABI;
f43abd2b 15248 if (!support_64bit_objects())
e013f690 15249 as_fatal (_("No compiled in support for 64 bit object file format"));
252b5132
RH
15250 break;
15251
c97ef257 15252 case OPTION_GP32:
a325df1d 15253 file_mips_gp32 = 1;
c97ef257
AH
15254 break;
15255
15256 case OPTION_GP64:
a325df1d 15257 file_mips_gp32 = 0;
c97ef257 15258 break;
252b5132 15259
ca4e0257 15260 case OPTION_FP32:
a325df1d 15261 file_mips_fp32 = 1;
316f5878
RS
15262 break;
15263
15264 case OPTION_FP64:
15265 file_mips_fp32 = 0;
ca4e0257
RS
15266 break;
15267
037b32b9
AN
15268 case OPTION_SINGLE_FLOAT:
15269 file_mips_single_float = 1;
15270 break;
15271
15272 case OPTION_DOUBLE_FLOAT:
15273 file_mips_single_float = 0;
15274 break;
15275
15276 case OPTION_SOFT_FLOAT:
15277 file_mips_soft_float = 1;
15278 break;
15279
15280 case OPTION_HARD_FLOAT:
15281 file_mips_soft_float = 0;
15282 break;
15283
252b5132 15284 case OPTION_MABI:
e013f690 15285 if (strcmp (arg, "32") == 0)
316f5878 15286 mips_abi = O32_ABI;
e013f690 15287 else if (strcmp (arg, "o64") == 0)
316f5878 15288 mips_abi = O64_ABI;
e013f690 15289 else if (strcmp (arg, "n32") == 0)
316f5878 15290 mips_abi = N32_ABI;
e013f690
TS
15291 else if (strcmp (arg, "64") == 0)
15292 {
316f5878 15293 mips_abi = N64_ABI;
e013f690
TS
15294 if (! support_64bit_objects())
15295 as_fatal (_("No compiled in support for 64 bit object file "
15296 "format"));
15297 }
15298 else if (strcmp (arg, "eabi") == 0)
316f5878 15299 mips_abi = EABI_ABI;
e013f690 15300 else
da0e507f
TS
15301 {
15302 as_fatal (_("invalid abi -mabi=%s"), arg);
15303 return 0;
15304 }
252b5132
RH
15305 break;
15306
6b76fefe 15307 case OPTION_M7000_HILO_FIX:
b34976b6 15308 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
15309 break;
15310
9ee72ff1 15311 case OPTION_MNO_7000_HILO_FIX:
b34976b6 15312 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
15313 break;
15314
ecb4347a 15315 case OPTION_MDEBUG:
b34976b6 15316 mips_flag_mdebug = TRUE;
ecb4347a
DJ
15317 break;
15318
15319 case OPTION_NO_MDEBUG:
b34976b6 15320 mips_flag_mdebug = FALSE;
ecb4347a 15321 break;
dcd410fe
RO
15322
15323 case OPTION_PDR:
15324 mips_flag_pdr = TRUE;
15325 break;
15326
15327 case OPTION_NO_PDR:
15328 mips_flag_pdr = FALSE;
15329 break;
0a44bf69
RS
15330
15331 case OPTION_MVXWORKS_PIC:
15332 mips_pic = VXWORKS_PIC;
15333 break;
ecb4347a 15334
252b5132
RH
15335 default:
15336 return 0;
15337 }
15338
c67a084a
NC
15339 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15340
252b5132
RH
15341 return 1;
15342}
316f5878
RS
15343\f
15344/* Set up globals to generate code for the ISA or processor
15345 described by INFO. */
252b5132 15346
252b5132 15347static void
17a2f251 15348mips_set_architecture (const struct mips_cpu_info *info)
252b5132 15349{
316f5878 15350 if (info != 0)
252b5132 15351 {
fef14a42
TS
15352 file_mips_arch = info->cpu;
15353 mips_opts.arch = info->cpu;
316f5878 15354 mips_opts.isa = info->isa;
252b5132 15355 }
252b5132
RH
15356}
15357
252b5132 15358
316f5878 15359/* Likewise for tuning. */
252b5132 15360
316f5878 15361static void
17a2f251 15362mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15363{
15364 if (info != 0)
fef14a42 15365 mips_tune = info->cpu;
316f5878 15366}
80cc45a5 15367
34ba82a8 15368
252b5132 15369void
17a2f251 15370mips_after_parse_args (void)
e9670677 15371{
fef14a42
TS
15372 const struct mips_cpu_info *arch_info = 0;
15373 const struct mips_cpu_info *tune_info = 0;
15374
e9670677 15375 /* GP relative stuff not working for PE */
6caf9ef4 15376 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 15377 {
6caf9ef4 15378 if (g_switch_seen && g_switch_value != 0)
e9670677
MR
15379 as_bad (_("-G not supported in this configuration."));
15380 g_switch_value = 0;
15381 }
15382
cac012d6
AO
15383 if (mips_abi == NO_ABI)
15384 mips_abi = MIPS_DEFAULT_ABI;
15385
22923709
RS
15386 /* The following code determines the architecture and register size.
15387 Similar code was added to GCC 3.3 (see override_options() in
15388 config/mips/mips.c). The GAS and GCC code should be kept in sync
15389 as much as possible. */
e9670677 15390
316f5878 15391 if (mips_arch_string != 0)
fef14a42 15392 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15393
316f5878 15394 if (file_mips_isa != ISA_UNKNOWN)
e9670677 15395 {
316f5878 15396 /* Handle -mipsN. At this point, file_mips_isa contains the
fef14a42 15397 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15398 the -march selection (if any). */
fef14a42 15399 if (arch_info != 0)
e9670677 15400 {
316f5878
RS
15401 /* -march takes precedence over -mipsN, since it is more descriptive.
15402 There's no harm in specifying both as long as the ISA levels
15403 are the same. */
fef14a42 15404 if (file_mips_isa != arch_info->isa)
316f5878
RS
15405 as_bad (_("-%s conflicts with the other architecture options, which imply -%s"),
15406 mips_cpu_info_from_isa (file_mips_isa)->name,
fef14a42 15407 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15408 }
316f5878 15409 else
fef14a42 15410 arch_info = mips_cpu_info_from_isa (file_mips_isa);
e9670677
MR
15411 }
15412
fef14a42 15413 if (arch_info == 0)
95bfe26e
MF
15414 {
15415 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15416 gas_assert (arch_info);
15417 }
e9670677 15418
fef14a42 15419 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15420 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15421 arch_info->name);
15422
15423 mips_set_architecture (arch_info);
15424
15425 /* Optimize for file_mips_arch, unless -mtune selects a different processor. */
15426 if (mips_tune_string != 0)
15427 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15428
fef14a42
TS
15429 if (tune_info == 0)
15430 mips_set_tune (arch_info);
15431 else
15432 mips_set_tune (tune_info);
e9670677 15433
316f5878 15434 if (file_mips_gp32 >= 0)
e9670677 15435 {
316f5878
RS
15436 /* The user specified the size of the integer registers. Make sure
15437 it agrees with the ABI and ISA. */
15438 if (file_mips_gp32 == 0 && !ISA_HAS_64BIT_REGS (mips_opts.isa))
15439 as_bad (_("-mgp64 used with a 32-bit processor"));
15440 else if (file_mips_gp32 == 1 && ABI_NEEDS_64BIT_REGS (mips_abi))
15441 as_bad (_("-mgp32 used with a 64-bit ABI"));
15442 else if (file_mips_gp32 == 0 && ABI_NEEDS_32BIT_REGS (mips_abi))
15443 as_bad (_("-mgp64 used with a 32-bit ABI"));
e9670677
MR
15444 }
15445 else
15446 {
316f5878
RS
15447 /* Infer the integer register size from the ABI and processor.
15448 Restrict ourselves to 32-bit registers if that's all the
15449 processor has, or if the ABI cannot handle 64-bit registers. */
15450 file_mips_gp32 = (ABI_NEEDS_32BIT_REGS (mips_abi)
15451 || !ISA_HAS_64BIT_REGS (mips_opts.isa));
e9670677
MR
15452 }
15453
ad3fea08
TS
15454 switch (file_mips_fp32)
15455 {
15456 default:
15457 case -1:
15458 /* No user specified float register size.
15459 ??? GAS treats single-float processors as though they had 64-bit
15460 float registers (although it complains when double-precision
15461 instructions are used). As things stand, saying they have 32-bit
15462 registers would lead to spurious "register must be even" messages.
15463 So here we assume float registers are never smaller than the
15464 integer ones. */
15465 if (file_mips_gp32 == 0)
15466 /* 64-bit integer registers implies 64-bit float registers. */
15467 file_mips_fp32 = 0;
c6278170 15468 else if ((mips_opts.ase & FP64_ASES)
ad3fea08
TS
15469 && ISA_HAS_64BIT_FPRS (mips_opts.isa))
15470 /* -mips3d and -mdmx imply 64-bit float registers, if possible. */
15471 file_mips_fp32 = 0;
15472 else
15473 /* 32-bit float registers. */
15474 file_mips_fp32 = 1;
15475 break;
15476
15477 /* The user specified the size of the float registers. Check if it
15478 agrees with the ABI and ISA. */
15479 case 0:
15480 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
15481 as_bad (_("-mfp64 used with a 32-bit fpu"));
15482 else if (ABI_NEEDS_32BIT_REGS (mips_abi)
15483 && !ISA_HAS_MXHC1 (mips_opts.isa))
15484 as_warn (_("-mfp64 used with a 32-bit ABI"));
15485 break;
15486 case 1:
15487 if (ABI_NEEDS_64BIT_REGS (mips_abi))
15488 as_warn (_("-mfp32 used with a 64-bit ABI"));
15489 break;
15490 }
e9670677 15491
316f5878 15492 /* End of GCC-shared inference code. */
e9670677 15493
17a2f251
TS
15494 /* This flag is set when we have a 64-bit capable CPU but use only
15495 32-bit wide registers. Note that EABI does not use it. */
15496 if (ISA_HAS_64BIT_REGS (mips_opts.isa)
15497 && ((mips_abi == NO_ABI && file_mips_gp32 == 1)
15498 || mips_abi == O32_ABI))
316f5878 15499 mips_32bitmode = 1;
e9670677
MR
15500
15501 if (mips_opts.isa == ISA_MIPS1 && mips_trap)
15502 as_bad (_("trap exception not supported at ISA 1"));
15503
e9670677
MR
15504 /* If the selected architecture includes support for ASEs, enable
15505 generation of code for them. */
a4672219 15506 if (mips_opts.mips16 == -1)
fef14a42 15507 mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_arch)) ? 1 : 0;
df58fc94
RS
15508 if (mips_opts.micromips == -1)
15509 mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_arch)) ? 1 : 0;
846ef2d0
RS
15510
15511 /* MIPS3D and MDMX require 64-bit FPRs, so -mfp32 should stop those
15512 ASEs from being selected implicitly. */
15513 if (file_mips_fp32 == 1)
15514 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX;
15515
15516 /* If the user didn't explicitly select or deselect a particular ASE,
15517 use the default setting for the CPU. */
15518 mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
15519
e9670677 15520 file_mips_isa = mips_opts.isa;
846ef2d0 15521 file_ase = mips_opts.ase;
e9670677
MR
15522 mips_opts.gp32 = file_mips_gp32;
15523 mips_opts.fp32 = file_mips_fp32;
037b32b9
AN
15524 mips_opts.soft_float = file_mips_soft_float;
15525 mips_opts.single_float = file_mips_single_float;
e9670677 15526
c6278170
RS
15527 mips_check_isa_supports_ases ();
15528
ecb4347a 15529 if (mips_flag_mdebug < 0)
e8044f35 15530 mips_flag_mdebug = 0;
e9670677
MR
15531}
15532\f
15533void
17a2f251 15534mips_init_after_args (void)
252b5132
RH
15535{
15536 /* initialize opcodes */
15537 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15538 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15539}
15540
15541long
17a2f251 15542md_pcrel_from (fixS *fixP)
252b5132 15543{
a7ebbfdf
TS
15544 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15545 switch (fixP->fx_r_type)
15546 {
df58fc94
RS
15547 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15548 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15549 /* Return the address of the delay slot. */
15550 return addr + 2;
15551
15552 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15553 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf
TS
15554 case BFD_RELOC_16_PCREL_S2:
15555 case BFD_RELOC_MIPS_JMP:
15556 /* Return the address of the delay slot. */
15557 return addr + 4;
df58fc94 15558
b47468a6
CM
15559 case BFD_RELOC_32_PCREL:
15560 return addr;
15561
a7ebbfdf 15562 default:
58ea3d6a 15563 /* We have no relocation type for PC relative MIPS16 instructions. */
64817874
TS
15564 if (fixP->fx_addsy && S_GET_SEGMENT (fixP->fx_addsy) != now_seg)
15565 as_bad_where (fixP->fx_file, fixP->fx_line,
15566 _("PC relative MIPS16 instruction references a different section"));
a7ebbfdf
TS
15567 return addr;
15568 }
252b5132
RH
15569}
15570
252b5132
RH
15571/* This is called before the symbol table is processed. In order to
15572 work with gcc when using mips-tfile, we must keep all local labels.
15573 However, in other cases, we want to discard them. If we were
15574 called with -g, but we didn't see any debugging information, it may
15575 mean that gcc is smuggling debugging information through to
15576 mips-tfile, in which case we must generate all local labels. */
15577
15578void
17a2f251 15579mips_frob_file_before_adjust (void)
252b5132
RH
15580{
15581#ifndef NO_ECOFF_DEBUGGING
15582 if (ECOFF_DEBUGGING
15583 && mips_debug != 0
15584 && ! ecoff_debugging_seen)
15585 flag_keep_locals = 1;
15586#endif
15587}
15588
3b91255e 15589/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15590 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15591 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15592 relocation operators.
15593
15594 For our purposes, a %lo() expression matches a %got() or %hi()
15595 expression if:
15596
15597 (a) it refers to the same symbol; and
15598 (b) the offset applied in the %lo() expression is no lower than
15599 the offset applied in the %got() or %hi().
15600
15601 (b) allows us to cope with code like:
15602
15603 lui $4,%hi(foo)
15604 lh $4,%lo(foo+2)($4)
15605
15606 ...which is legal on RELA targets, and has a well-defined behaviour
15607 if the user knows that adding 2 to "foo" will not induce a carry to
15608 the high 16 bits.
15609
15610 When several %lo()s match a particular %got() or %hi(), we use the
15611 following rules to distinguish them:
15612
15613 (1) %lo()s with smaller offsets are a better match than %lo()s with
15614 higher offsets.
15615
15616 (2) %lo()s with no matching %got() or %hi() are better than those
15617 that already have a matching %got() or %hi().
15618
15619 (3) later %lo()s are better than earlier %lo()s.
15620
15621 These rules are applied in order.
15622
15623 (1) means, among other things, that %lo()s with identical offsets are
15624 chosen if they exist.
15625
15626 (2) means that we won't associate several high-part relocations with
15627 the same low-part relocation unless there's no alternative. Having
15628 several high parts for the same low part is a GNU extension; this rule
15629 allows careful users to avoid it.
15630
15631 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15632 with the last high-part relocation being at the front of the list.
15633 It therefore makes sense to choose the last matching low-part
15634 relocation, all other things being equal. It's also easier
15635 to code that way. */
252b5132
RH
15636
15637void
17a2f251 15638mips_frob_file (void)
252b5132
RH
15639{
15640 struct mips_hi_fixup *l;
35903be0 15641 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15642
15643 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15644 {
15645 segment_info_type *seginfo;
3b91255e
RS
15646 bfd_boolean matched_lo_p;
15647 fixS **hi_pos, **lo_pos, **pos;
252b5132 15648
9c2799c2 15649 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15650
5919d012 15651 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15652 there isn't supposed to be a matching LO. Ignore %gots against
15653 constants; we'll report an error for those later. */
738e5348 15654 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
15655 && !(l->fixp->fx_addsy
15656 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
15657 continue;
15658
15659 /* Check quickly whether the next fixup happens to be a matching %lo. */
15660 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15661 continue;
15662
252b5132 15663 seginfo = seg_info (l->seg);
252b5132 15664
3b91255e
RS
15665 /* Set HI_POS to the position of this relocation in the chain.
15666 Set LO_POS to the position of the chosen low-part relocation.
15667 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15668 relocation that matches an immediately-preceding high-part
15669 relocation. */
15670 hi_pos = NULL;
15671 lo_pos = NULL;
15672 matched_lo_p = FALSE;
738e5348 15673 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15674
3b91255e
RS
15675 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15676 {
15677 if (*pos == l->fixp)
15678 hi_pos = pos;
15679
35903be0 15680 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15681 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15682 && (*pos)->fx_offset >= l->fixp->fx_offset
15683 && (lo_pos == NULL
15684 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15685 || (!matched_lo_p
15686 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15687 lo_pos = pos;
15688
15689 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15690 && fixup_has_matching_lo_p (*pos));
15691 }
15692
15693 /* If we found a match, remove the high-part relocation from its
15694 current position and insert it before the low-part relocation.
15695 Make the offsets match so that fixup_has_matching_lo_p()
15696 will return true.
15697
15698 We don't warn about unmatched high-part relocations since some
15699 versions of gcc have been known to emit dead "lui ...%hi(...)"
15700 instructions. */
15701 if (lo_pos != NULL)
15702 {
15703 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15704 if (l->fixp->fx_next != *lo_pos)
252b5132 15705 {
3b91255e
RS
15706 *hi_pos = l->fixp->fx_next;
15707 l->fixp->fx_next = *lo_pos;
15708 *lo_pos = l->fixp;
252b5132 15709 }
252b5132
RH
15710 }
15711 }
15712}
15713
252b5132 15714int
17a2f251 15715mips_force_relocation (fixS *fixp)
252b5132 15716{
ae6063d4 15717 if (generic_force_reloc (fixp))
252b5132
RH
15718 return 1;
15719
df58fc94
RS
15720 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15721 so that the linker relaxation can update targets. */
15722 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15723 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15724 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15725 return 1;
15726
3e722fb5 15727 return 0;
252b5132
RH
15728}
15729
b886a2ab
RS
15730/* Read the instruction associated with RELOC from BUF. */
15731
15732static unsigned int
15733read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15734{
15735 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15736 return read_compressed_insn (buf, 4);
15737 else
15738 return read_insn (buf);
15739}
15740
15741/* Write instruction INSN to BUF, given that it has been relocated
15742 by RELOC. */
15743
15744static void
15745write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15746 unsigned long insn)
15747{
15748 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15749 write_compressed_insn (buf, insn, 4);
15750 else
15751 write_insn (buf, insn);
15752}
15753
252b5132
RH
15754/* Apply a fixup to the object file. */
15755
94f592af 15756void
55cf6793 15757md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15758{
4d68580a 15759 char *buf;
b886a2ab 15760 unsigned long insn;
a7ebbfdf 15761 reloc_howto_type *howto;
252b5132 15762
a7ebbfdf
TS
15763 /* We ignore generic BFD relocations we don't know about. */
15764 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15765 if (! howto)
15766 return;
65551fa4 15767
df58fc94
RS
15768 gas_assert (fixP->fx_size == 2
15769 || fixP->fx_size == 4
90ecf173
MR
15770 || fixP->fx_r_type == BFD_RELOC_16
15771 || fixP->fx_r_type == BFD_RELOC_64
15772 || fixP->fx_r_type == BFD_RELOC_CTOR
15773 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15774 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15775 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15776 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
15777 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64);
252b5132 15778
4d68580a 15779 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15780
df58fc94
RS
15781 gas_assert (!fixP->fx_pcrel || fixP->fx_r_type == BFD_RELOC_16_PCREL_S2
15782 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15783 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
15784 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
15785 || fixP->fx_r_type == BFD_RELOC_32_PCREL);
b1dca8ee
RS
15786
15787 /* Don't treat parts of a composite relocation as done. There are two
15788 reasons for this:
15789
15790 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15791 should nevertheless be emitted if the first part is.
15792
15793 (2) In normal usage, composite relocations are never assembly-time
15794 constants. The easiest way of dealing with the pathological
15795 exceptions is to generate a relocation against STN_UNDEF and
15796 leave everything up to the linker. */
3994f87e 15797 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15798 fixP->fx_done = 1;
15799
15800 switch (fixP->fx_r_type)
15801 {
3f98094e
DJ
15802 case BFD_RELOC_MIPS_TLS_GD:
15803 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15804 case BFD_RELOC_MIPS_TLS_DTPREL32:
15805 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15806 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15807 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15808 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15809 case BFD_RELOC_MIPS_TLS_TPREL32:
15810 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15811 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15812 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15813 case BFD_RELOC_MICROMIPS_TLS_GD:
15814 case BFD_RELOC_MICROMIPS_TLS_LDM:
15815 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15816 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15817 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15818 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15819 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15820 case BFD_RELOC_MIPS16_TLS_GD:
15821 case BFD_RELOC_MIPS16_TLS_LDM:
15822 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15823 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15824 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15825 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15826 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
b886a2ab
RS
15827 if (!fixP->fx_addsy)
15828 {
15829 as_bad_where (fixP->fx_file, fixP->fx_line,
15830 _("TLS relocation against a constant"));
15831 break;
15832 }
3f98094e
DJ
15833 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15834 /* fall through */
15835
252b5132 15836 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
15837 case BFD_RELOC_MIPS_SHIFT5:
15838 case BFD_RELOC_MIPS_SHIFT6:
15839 case BFD_RELOC_MIPS_GOT_DISP:
15840 case BFD_RELOC_MIPS_GOT_PAGE:
15841 case BFD_RELOC_MIPS_GOT_OFST:
15842 case BFD_RELOC_MIPS_SUB:
15843 case BFD_RELOC_MIPS_INSERT_A:
15844 case BFD_RELOC_MIPS_INSERT_B:
15845 case BFD_RELOC_MIPS_DELETE:
15846 case BFD_RELOC_MIPS_HIGHEST:
15847 case BFD_RELOC_MIPS_HIGHER:
15848 case BFD_RELOC_MIPS_SCN_DISP:
15849 case BFD_RELOC_MIPS_REL16:
15850 case BFD_RELOC_MIPS_RELGOT:
15851 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15852 case BFD_RELOC_HI16:
15853 case BFD_RELOC_HI16_S:
b886a2ab 15854 case BFD_RELOC_LO16:
cdf6fd85 15855 case BFD_RELOC_GPREL16:
252b5132
RH
15856 case BFD_RELOC_MIPS_LITERAL:
15857 case BFD_RELOC_MIPS_CALL16:
15858 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15859 case BFD_RELOC_GPREL32:
252b5132
RH
15860 case BFD_RELOC_MIPS_GOT_HI16:
15861 case BFD_RELOC_MIPS_GOT_LO16:
15862 case BFD_RELOC_MIPS_CALL_HI16:
15863 case BFD_RELOC_MIPS_CALL_LO16:
15864 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15865 case BFD_RELOC_MIPS16_GOT16:
15866 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15867 case BFD_RELOC_MIPS16_HI16:
15868 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15869 case BFD_RELOC_MIPS16_LO16:
252b5132 15870 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
15871 case BFD_RELOC_MICROMIPS_JMP:
15872 case BFD_RELOC_MICROMIPS_GOT_DISP:
15873 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15874 case BFD_RELOC_MICROMIPS_GOT_OFST:
15875 case BFD_RELOC_MICROMIPS_SUB:
15876 case BFD_RELOC_MICROMIPS_HIGHEST:
15877 case BFD_RELOC_MICROMIPS_HIGHER:
15878 case BFD_RELOC_MICROMIPS_SCN_DISP:
15879 case BFD_RELOC_MICROMIPS_JALR:
15880 case BFD_RELOC_MICROMIPS_HI16:
15881 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15882 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15883 case BFD_RELOC_MICROMIPS_GPREL16:
15884 case BFD_RELOC_MICROMIPS_LITERAL:
15885 case BFD_RELOC_MICROMIPS_CALL16:
15886 case BFD_RELOC_MICROMIPS_GOT16:
15887 case BFD_RELOC_MICROMIPS_GOT_HI16:
15888 case BFD_RELOC_MICROMIPS_GOT_LO16:
15889 case BFD_RELOC_MICROMIPS_CALL_HI16:
15890 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15891 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15892 if (fixP->fx_done)
15893 {
15894 offsetT value;
15895
15896 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15897 {
15898 insn = read_reloc_insn (buf, fixP->fx_r_type);
15899 if (mips16_reloc_p (fixP->fx_r_type))
15900 insn |= mips16_immed_extend (value, 16);
15901 else
15902 insn |= (value & 0xffff);
15903 write_reloc_insn (buf, fixP->fx_r_type, insn);
15904 }
15905 else
15906 as_bad_where (fixP->fx_file, fixP->fx_line,
15907 _("Unsupported constant in relocation"));
15908 }
252b5132
RH
15909 break;
15910
252b5132
RH
15911 case BFD_RELOC_64:
15912 /* This is handled like BFD_RELOC_32, but we output a sign
15913 extended value if we are only 32 bits. */
3e722fb5 15914 if (fixP->fx_done)
252b5132
RH
15915 {
15916 if (8 <= sizeof (valueT))
4d68580a 15917 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15918 else
15919 {
a7ebbfdf 15920 valueT hiv;
252b5132 15921
a7ebbfdf 15922 if ((*valP & 0x80000000) != 0)
252b5132
RH
15923 hiv = 0xffffffff;
15924 else
15925 hiv = 0;
4d68580a
RS
15926 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15927 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15928 }
15929 }
15930 break;
15931
056350c6 15932 case BFD_RELOC_RVA:
252b5132 15933 case BFD_RELOC_32:
b47468a6 15934 case BFD_RELOC_32_PCREL:
252b5132
RH
15935 case BFD_RELOC_16:
15936 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15937 value now. This can happen if we have a .word which is not
15938 resolved when it appears but is later defined. */
252b5132 15939 if (fixP->fx_done)
4d68580a 15940 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15941 break;
15942
252b5132 15943 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15944 if ((*valP & 0x3) != 0)
cb56d3d3 15945 as_bad_where (fixP->fx_file, fixP->fx_line,
bad36eac 15946 _("Branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15947
54f4ddb3
TS
15948 /* We need to save the bits in the instruction since fixup_segment()
15949 might be deleting the relocation entry (i.e., a branch within
15950 the current segment). */
a7ebbfdf 15951 if (! fixP->fx_done)
bb2d6cd7 15952 break;
252b5132 15953
54f4ddb3 15954 /* Update old instruction data. */
4d68580a 15955 insn = read_insn (buf);
252b5132 15956
a7ebbfdf
TS
15957 if (*valP + 0x20000 <= 0x3ffff)
15958 {
15959 insn |= (*valP >> 2) & 0xffff;
4d68580a 15960 write_insn (buf, insn);
a7ebbfdf
TS
15961 }
15962 else if (mips_pic == NO_PIC
15963 && fixP->fx_done
15964 && fixP->fx_frag->fr_address >= text_section->vma
15965 && (fixP->fx_frag->fr_address
587aac4e 15966 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15967 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15968 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15969 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15970 {
15971 /* The branch offset is too large. If this is an
15972 unconditional branch, and we are not generating PIC code,
15973 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15974 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15975 insn = 0x0c000000; /* jal */
252b5132 15976 else
a7ebbfdf
TS
15977 insn = 0x08000000; /* j */
15978 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15979 fixP->fx_done = 0;
15980 fixP->fx_addsy = section_symbol (text_section);
15981 *valP += md_pcrel_from (fixP);
4d68580a 15982 write_insn (buf, insn);
a7ebbfdf
TS
15983 }
15984 else
15985 {
15986 /* If we got here, we have branch-relaxation disabled,
15987 and there's nothing we can do to fix this instruction
15988 without turning it into a longer sequence. */
15989 as_bad_where (fixP->fx_file, fixP->fx_line,
15990 _("Branch out of range"));
252b5132 15991 }
252b5132
RH
15992 break;
15993
df58fc94
RS
15994 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15995 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15996 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15997 /* We adjust the offset back to even. */
15998 if ((*valP & 0x1) != 0)
15999 --(*valP);
16000
16001 if (! fixP->fx_done)
16002 break;
16003
16004 /* Should never visit here, because we keep the relocation. */
16005 abort ();
16006 break;
16007
252b5132
RH
16008 case BFD_RELOC_VTABLE_INHERIT:
16009 fixP->fx_done = 0;
16010 if (fixP->fx_addsy
16011 && !S_IS_DEFINED (fixP->fx_addsy)
16012 && !S_IS_WEAK (fixP->fx_addsy))
16013 S_SET_WEAK (fixP->fx_addsy);
16014 break;
16015
16016 case BFD_RELOC_VTABLE_ENTRY:
16017 fixP->fx_done = 0;
16018 break;
16019
16020 default:
b37df7c4 16021 abort ();
252b5132 16022 }
a7ebbfdf
TS
16023
16024 /* Remember value for tc_gen_reloc. */
16025 fixP->fx_addnumber = *valP;
252b5132
RH
16026}
16027
252b5132 16028static symbolS *
17a2f251 16029get_symbol (void)
252b5132
RH
16030{
16031 int c;
16032 char *name;
16033 symbolS *p;
16034
16035 name = input_line_pointer;
16036 c = get_symbol_end ();
16037 p = (symbolS *) symbol_find_or_make (name);
16038 *input_line_pointer = c;
16039 return p;
16040}
16041
742a56fe
RS
16042/* Align the current frag to a given power of two. If a particular
16043 fill byte should be used, FILL points to an integer that contains
16044 that byte, otherwise FILL is null.
16045
462427c4
RS
16046 This function used to have the comment:
16047
16048 The MIPS assembler also automatically adjusts any preceding label.
16049
16050 The implementation therefore applied the adjustment to a maximum of
16051 one label. However, other label adjustments are applied to batches
16052 of labels, and adjusting just one caused problems when new labels
16053 were added for the sake of debugging or unwind information.
16054 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16055
16056static void
462427c4 16057mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16058{
7d10b47d 16059 mips_emit_delays ();
df58fc94 16060 mips_record_compressed_mode ();
742a56fe
RS
16061 if (fill == NULL && subseg_text_p (now_seg))
16062 frag_align_code (to, 0);
16063 else
16064 frag_align (to, fill ? *fill : 0, 0);
252b5132 16065 record_alignment (now_seg, to);
462427c4 16066 mips_move_labels (labels, FALSE);
252b5132
RH
16067}
16068
16069/* Align to a given power of two. .align 0 turns off the automatic
16070 alignment used by the data creating pseudo-ops. */
16071
16072static void
17a2f251 16073s_align (int x ATTRIBUTE_UNUSED)
252b5132 16074{
742a56fe 16075 int temp, fill_value, *fill_ptr;
49954fb4 16076 long max_alignment = 28;
252b5132 16077
54f4ddb3 16078 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16079 to the aligned address.
54f4ddb3 16080 o It's not documented but auto alignment is reinstated by
252b5132 16081 a .align pseudo instruction.
54f4ddb3 16082 o Note also that after auto alignment is turned off the mips assembler
252b5132 16083 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16084 We don't. */
252b5132
RH
16085
16086 temp = get_absolute_expression ();
16087 if (temp > max_alignment)
16088 as_bad (_("Alignment too large: %d. assumed."), temp = max_alignment);
16089 else if (temp < 0)
16090 {
16091 as_warn (_("Alignment negative: 0 assumed."));
16092 temp = 0;
16093 }
16094 if (*input_line_pointer == ',')
16095 {
f9419b05 16096 ++input_line_pointer;
742a56fe
RS
16097 fill_value = get_absolute_expression ();
16098 fill_ptr = &fill_value;
252b5132
RH
16099 }
16100 else
742a56fe 16101 fill_ptr = 0;
252b5132
RH
16102 if (temp)
16103 {
a8dbcb85
TS
16104 segment_info_type *si = seg_info (now_seg);
16105 struct insn_label_list *l = si->label_list;
54f4ddb3 16106 /* Auto alignment should be switched on by next section change. */
252b5132 16107 auto_align = 1;
462427c4 16108 mips_align (temp, fill_ptr, l);
252b5132
RH
16109 }
16110 else
16111 {
16112 auto_align = 0;
16113 }
16114
16115 demand_empty_rest_of_line ();
16116}
16117
252b5132 16118static void
17a2f251 16119s_change_sec (int sec)
252b5132
RH
16120{
16121 segT seg;
16122
252b5132
RH
16123 /* The ELF backend needs to know that we are changing sections, so
16124 that .previous works correctly. We could do something like check
b6ff326e 16125 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16126 as it would not be appropriate to use it in the section changing
16127 functions in read.c, since obj-elf.c intercepts those. FIXME:
16128 This should be cleaner, somehow. */
f3ded42a 16129 obj_elf_section_change_hook ();
252b5132 16130
7d10b47d 16131 mips_emit_delays ();
6a32d874 16132
252b5132
RH
16133 switch (sec)
16134 {
16135 case 't':
16136 s_text (0);
16137 break;
16138 case 'd':
16139 s_data (0);
16140 break;
16141 case 'b':
16142 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16143 demand_empty_rest_of_line ();
16144 break;
16145
16146 case 'r':
4d0d148d
TS
16147 seg = subseg_new (RDATA_SECTION_NAME,
16148 (subsegT) get_absolute_expression ());
f3ded42a
RS
16149 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16150 | SEC_READONLY | SEC_RELOC
16151 | SEC_DATA));
16152 if (strncmp (TARGET_OS, "elf", 3) != 0)
16153 record_alignment (seg, 4);
4d0d148d 16154 demand_empty_rest_of_line ();
252b5132
RH
16155 break;
16156
16157 case 's':
4d0d148d 16158 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16159 bfd_set_section_flags (stdoutput, seg,
16160 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16161 if (strncmp (TARGET_OS, "elf", 3) != 0)
16162 record_alignment (seg, 4);
4d0d148d
TS
16163 demand_empty_rest_of_line ();
16164 break;
998b3c36
MR
16165
16166 case 'B':
16167 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16168 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16169 if (strncmp (TARGET_OS, "elf", 3) != 0)
16170 record_alignment (seg, 4);
998b3c36
MR
16171 demand_empty_rest_of_line ();
16172 break;
252b5132
RH
16173 }
16174
16175 auto_align = 1;
16176}
b34976b6 16177
cca86cc8 16178void
17a2f251 16179s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16180{
cca86cc8
SC
16181 char *section_name;
16182 char c;
684022ea 16183 char next_c = 0;
cca86cc8
SC
16184 int section_type;
16185 int section_flag;
16186 int section_entry_size;
16187 int section_alignment;
b34976b6 16188
cca86cc8
SC
16189 section_name = input_line_pointer;
16190 c = get_symbol_end ();
a816d1ed
AO
16191 if (c)
16192 next_c = *(input_line_pointer + 1);
cca86cc8 16193
4cf0dd0d
TS
16194 /* Do we have .section Name<,"flags">? */
16195 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16196 {
4cf0dd0d
TS
16197 /* just after name is now '\0'. */
16198 *input_line_pointer = c;
cca86cc8
SC
16199 input_line_pointer = section_name;
16200 obj_elf_section (ignore);
16201 return;
16202 }
16203 input_line_pointer++;
16204
16205 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16206 if (c == ',')
16207 section_type = get_absolute_expression ();
16208 else
16209 section_type = 0;
16210 if (*input_line_pointer++ == ',')
16211 section_flag = get_absolute_expression ();
16212 else
16213 section_flag = 0;
16214 if (*input_line_pointer++ == ',')
16215 section_entry_size = get_absolute_expression ();
16216 else
16217 section_entry_size = 0;
16218 if (*input_line_pointer++ == ',')
16219 section_alignment = get_absolute_expression ();
16220 else
16221 section_alignment = 0;
87975d2a
AM
16222 /* FIXME: really ignore? */
16223 (void) section_alignment;
cca86cc8 16224
a816d1ed
AO
16225 section_name = xstrdup (section_name);
16226
8ab8a5c8
RS
16227 /* When using the generic form of .section (as implemented by obj-elf.c),
16228 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16229 traditionally had to fall back on the more common @progbits instead.
16230
16231 There's nothing really harmful in this, since bfd will correct
16232 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16233 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16234 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16235
16236 Even so, we shouldn't force users of the MIPS .section syntax to
16237 incorrectly label the sections as SHT_PROGBITS. The best compromise
16238 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16239 generic type-checking code. */
16240 if (section_type == SHT_MIPS_DWARF)
16241 section_type = SHT_PROGBITS;
16242
cca86cc8
SC
16243 obj_elf_change_section (section_name, section_type, section_flag,
16244 section_entry_size, 0, 0, 0);
a816d1ed
AO
16245
16246 if (now_seg->name != section_name)
16247 free (section_name);
cca86cc8 16248}
252b5132
RH
16249
16250void
17a2f251 16251mips_enable_auto_align (void)
252b5132
RH
16252{
16253 auto_align = 1;
16254}
16255
16256static void
17a2f251 16257s_cons (int log_size)
252b5132 16258{
a8dbcb85
TS
16259 segment_info_type *si = seg_info (now_seg);
16260 struct insn_label_list *l = si->label_list;
252b5132 16261
7d10b47d 16262 mips_emit_delays ();
252b5132 16263 if (log_size > 0 && auto_align)
462427c4 16264 mips_align (log_size, 0, l);
252b5132 16265 cons (1 << log_size);
a1facbec 16266 mips_clear_insn_labels ();
252b5132
RH
16267}
16268
16269static void
17a2f251 16270s_float_cons (int type)
252b5132 16271{
a8dbcb85
TS
16272 segment_info_type *si = seg_info (now_seg);
16273 struct insn_label_list *l = si->label_list;
252b5132 16274
7d10b47d 16275 mips_emit_delays ();
252b5132
RH
16276
16277 if (auto_align)
49309057
ILT
16278 {
16279 if (type == 'd')
462427c4 16280 mips_align (3, 0, l);
49309057 16281 else
462427c4 16282 mips_align (2, 0, l);
49309057 16283 }
252b5132 16284
252b5132 16285 float_cons (type);
a1facbec 16286 mips_clear_insn_labels ();
252b5132
RH
16287}
16288
16289/* Handle .globl. We need to override it because on Irix 5 you are
16290 permitted to say
16291 .globl foo .text
16292 where foo is an undefined symbol, to mean that foo should be
16293 considered to be the address of a function. */
16294
16295static void
17a2f251 16296s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16297{
16298 char *name;
16299 int c;
16300 symbolS *symbolP;
16301 flagword flag;
16302
8a06b769 16303 do
252b5132 16304 {
8a06b769 16305 name = input_line_pointer;
252b5132 16306 c = get_symbol_end ();
8a06b769
TS
16307 symbolP = symbol_find_or_make (name);
16308 S_SET_EXTERNAL (symbolP);
16309
252b5132 16310 *input_line_pointer = c;
8a06b769 16311 SKIP_WHITESPACE ();
252b5132 16312
8a06b769
TS
16313 /* On Irix 5, every global symbol that is not explicitly labelled as
16314 being a function is apparently labelled as being an object. */
16315 flag = BSF_OBJECT;
252b5132 16316
8a06b769
TS
16317 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16318 && (*input_line_pointer != ','))
16319 {
16320 char *secname;
16321 asection *sec;
16322
16323 secname = input_line_pointer;
16324 c = get_symbol_end ();
16325 sec = bfd_get_section_by_name (stdoutput, secname);
16326 if (sec == NULL)
16327 as_bad (_("%s: no such section"), secname);
16328 *input_line_pointer = c;
16329
16330 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16331 flag = BSF_FUNCTION;
16332 }
16333
16334 symbol_get_bfdsym (symbolP)->flags |= flag;
16335
16336 c = *input_line_pointer;
16337 if (c == ',')
16338 {
16339 input_line_pointer++;
16340 SKIP_WHITESPACE ();
16341 if (is_end_of_line[(unsigned char) *input_line_pointer])
16342 c = '\n';
16343 }
16344 }
16345 while (c == ',');
252b5132 16346
252b5132
RH
16347 demand_empty_rest_of_line ();
16348}
16349
16350static void
17a2f251 16351s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16352{
16353 char *opt;
16354 char c;
16355
16356 opt = input_line_pointer;
16357 c = get_symbol_end ();
16358
16359 if (*opt == 'O')
16360 {
16361 /* FIXME: What does this mean? */
16362 }
16363 else if (strncmp (opt, "pic", 3) == 0)
16364 {
16365 int i;
16366
16367 i = atoi (opt + 3);
16368 if (i == 0)
16369 mips_pic = NO_PIC;
16370 else if (i == 2)
143d77c5 16371 {
8b828383 16372 mips_pic = SVR4_PIC;
143d77c5
EC
16373 mips_abicalls = TRUE;
16374 }
252b5132
RH
16375 else
16376 as_bad (_(".option pic%d not supported"), i);
16377
4d0d148d 16378 if (mips_pic == SVR4_PIC)
252b5132
RH
16379 {
16380 if (g_switch_seen && g_switch_value != 0)
16381 as_warn (_("-G may not be used with SVR4 PIC code"));
16382 g_switch_value = 0;
16383 bfd_set_gp_size (stdoutput, 0);
16384 }
16385 }
16386 else
16387 as_warn (_("Unrecognized option \"%s\""), opt);
16388
16389 *input_line_pointer = c;
16390 demand_empty_rest_of_line ();
16391}
16392
16393/* This structure is used to hold a stack of .set values. */
16394
e972090a
NC
16395struct mips_option_stack
16396{
252b5132
RH
16397 struct mips_option_stack *next;
16398 struct mips_set_options options;
16399};
16400
16401static struct mips_option_stack *mips_opts_stack;
16402
16403/* Handle the .set pseudo-op. */
16404
16405static void
17a2f251 16406s_mipsset (int x ATTRIBUTE_UNUSED)
252b5132
RH
16407{
16408 char *name = input_line_pointer, ch;
c6278170 16409 const struct mips_ase *ase;
252b5132
RH
16410
16411 while (!is_end_of_line[(unsigned char) *input_line_pointer])
f9419b05 16412 ++input_line_pointer;
252b5132
RH
16413 ch = *input_line_pointer;
16414 *input_line_pointer = '\0';
16415
16416 if (strcmp (name, "reorder") == 0)
16417 {
7d10b47d
RS
16418 if (mips_opts.noreorder)
16419 end_noreorder ();
252b5132
RH
16420 }
16421 else if (strcmp (name, "noreorder") == 0)
16422 {
7d10b47d
RS
16423 if (!mips_opts.noreorder)
16424 start_noreorder ();
252b5132 16425 }
741fe287
MR
16426 else if (strncmp (name, "at=", 3) == 0)
16427 {
16428 char *s = name + 3;
16429
16430 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
16431 as_bad (_("Unrecognized register name `%s'"), s);
16432 }
252b5132
RH
16433 else if (strcmp (name, "at") == 0)
16434 {
741fe287 16435 mips_opts.at = ATREG;
252b5132
RH
16436 }
16437 else if (strcmp (name, "noat") == 0)
16438 {
741fe287 16439 mips_opts.at = ZERO;
252b5132
RH
16440 }
16441 else if (strcmp (name, "macro") == 0)
16442 {
16443 mips_opts.warn_about_macros = 0;
16444 }
16445 else if (strcmp (name, "nomacro") == 0)
16446 {
16447 if (mips_opts.noreorder == 0)
16448 as_bad (_("`noreorder' must be set before `nomacro'"));
16449 mips_opts.warn_about_macros = 1;
16450 }
16451 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
16452 {
16453 mips_opts.nomove = 0;
16454 }
16455 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
16456 {
16457 mips_opts.nomove = 1;
16458 }
16459 else if (strcmp (name, "bopt") == 0)
16460 {
16461 mips_opts.nobopt = 0;
16462 }
16463 else if (strcmp (name, "nobopt") == 0)
16464 {
16465 mips_opts.nobopt = 1;
16466 }
ad3fea08
TS
16467 else if (strcmp (name, "gp=default") == 0)
16468 mips_opts.gp32 = file_mips_gp32;
16469 else if (strcmp (name, "gp=32") == 0)
16470 mips_opts.gp32 = 1;
16471 else if (strcmp (name, "gp=64") == 0)
16472 {
16473 if (!ISA_HAS_64BIT_REGS (mips_opts.isa))
20203fb9 16474 as_warn (_("%s isa does not support 64-bit registers"),
ad3fea08
TS
16475 mips_cpu_info_from_isa (mips_opts.isa)->name);
16476 mips_opts.gp32 = 0;
16477 }
16478 else if (strcmp (name, "fp=default") == 0)
16479 mips_opts.fp32 = file_mips_fp32;
16480 else if (strcmp (name, "fp=32") == 0)
16481 mips_opts.fp32 = 1;
16482 else if (strcmp (name, "fp=64") == 0)
16483 {
16484 if (!ISA_HAS_64BIT_FPRS (mips_opts.isa))
20203fb9 16485 as_warn (_("%s isa does not support 64-bit floating point registers"),
ad3fea08
TS
16486 mips_cpu_info_from_isa (mips_opts.isa)->name);
16487 mips_opts.fp32 = 0;
16488 }
037b32b9
AN
16489 else if (strcmp (name, "softfloat") == 0)
16490 mips_opts.soft_float = 1;
16491 else if (strcmp (name, "hardfloat") == 0)
16492 mips_opts.soft_float = 0;
16493 else if (strcmp (name, "singlefloat") == 0)
16494 mips_opts.single_float = 1;
16495 else if (strcmp (name, "doublefloat") == 0)
16496 mips_opts.single_float = 0;
252b5132
RH
16497 else if (strcmp (name, "mips16") == 0
16498 || strcmp (name, "MIPS-16") == 0)
df58fc94
RS
16499 {
16500 if (mips_opts.micromips == 1)
16501 as_fatal (_("`mips16' cannot be used with `micromips'"));
16502 mips_opts.mips16 = 1;
16503 }
252b5132
RH
16504 else if (strcmp (name, "nomips16") == 0
16505 || strcmp (name, "noMIPS-16") == 0)
16506 mips_opts.mips16 = 0;
df58fc94
RS
16507 else if (strcmp (name, "micromips") == 0)
16508 {
16509 if (mips_opts.mips16 == 1)
16510 as_fatal (_("`micromips' cannot be used with `mips16'"));
16511 mips_opts.micromips = 1;
16512 }
16513 else if (strcmp (name, "nomicromips") == 0)
16514 mips_opts.micromips = 0;
c6278170
RS
16515 else if (name[0] == 'n'
16516 && name[1] == 'o'
16517 && (ase = mips_lookup_ase (name + 2)))
16518 mips_set_ase (ase, FALSE);
16519 else if ((ase = mips_lookup_ase (name)))
16520 mips_set_ase (ase, TRUE);
1a2c1fad 16521 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16522 {
af7ee8bf 16523 int reset = 0;
252b5132 16524
1a2c1fad
CD
16525 /* Permit the user to change the ISA and architecture on the fly.
16526 Needless to say, misuse can cause serious problems. */
81a21e38 16527 if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
af7ee8bf
CD
16528 {
16529 reset = 1;
16530 mips_opts.isa = file_mips_isa;
1a2c1fad 16531 mips_opts.arch = file_mips_arch;
1a2c1fad
CD
16532 }
16533 else if (strncmp (name, "arch=", 5) == 0)
16534 {
16535 const struct mips_cpu_info *p;
16536
16537 p = mips_parse_cpu("internal use", name + 5);
16538 if (!p)
16539 as_bad (_("unknown architecture %s"), name + 5);
16540 else
16541 {
16542 mips_opts.arch = p->cpu;
16543 mips_opts.isa = p->isa;
16544 }
16545 }
81a21e38
TS
16546 else if (strncmp (name, "mips", 4) == 0)
16547 {
16548 const struct mips_cpu_info *p;
16549
16550 p = mips_parse_cpu("internal use", name);
16551 if (!p)
16552 as_bad (_("unknown ISA level %s"), name + 4);
16553 else
16554 {
16555 mips_opts.arch = p->cpu;
16556 mips_opts.isa = p->isa;
16557 }
16558 }
af7ee8bf 16559 else
81a21e38 16560 as_bad (_("unknown ISA or architecture %s"), name);
af7ee8bf
CD
16561
16562 switch (mips_opts.isa)
98d3f06f
KH
16563 {
16564 case 0:
98d3f06f 16565 break;
af7ee8bf
CD
16566 case ISA_MIPS1:
16567 case ISA_MIPS2:
16568 case ISA_MIPS32:
16569 case ISA_MIPS32R2:
98d3f06f
KH
16570 mips_opts.gp32 = 1;
16571 mips_opts.fp32 = 1;
16572 break;
af7ee8bf
CD
16573 case ISA_MIPS3:
16574 case ISA_MIPS4:
16575 case ISA_MIPS5:
16576 case ISA_MIPS64:
5f74bc13 16577 case ISA_MIPS64R2:
98d3f06f 16578 mips_opts.gp32 = 0;
e407c74b
NC
16579 if (mips_opts.arch == CPU_R5900)
16580 {
16581 mips_opts.fp32 = 1;
16582 }
16583 else
16584 {
98d3f06f 16585 mips_opts.fp32 = 0;
e407c74b 16586 }
98d3f06f
KH
16587 break;
16588 default:
16589 as_bad (_("unknown ISA level %s"), name + 4);
16590 break;
16591 }
af7ee8bf 16592 if (reset)
98d3f06f 16593 {
af7ee8bf
CD
16594 mips_opts.gp32 = file_mips_gp32;
16595 mips_opts.fp32 = file_mips_fp32;
98d3f06f 16596 }
252b5132
RH
16597 }
16598 else if (strcmp (name, "autoextend") == 0)
16599 mips_opts.noautoextend = 0;
16600 else if (strcmp (name, "noautoextend") == 0)
16601 mips_opts.noautoextend = 1;
833794fc
MR
16602 else if (strcmp (name, "insn32") == 0)
16603 mips_opts.insn32 = TRUE;
16604 else if (strcmp (name, "noinsn32") == 0)
16605 mips_opts.insn32 = FALSE;
252b5132
RH
16606 else if (strcmp (name, "push") == 0)
16607 {
16608 struct mips_option_stack *s;
16609
16610 s = (struct mips_option_stack *) xmalloc (sizeof *s);
16611 s->next = mips_opts_stack;
16612 s->options = mips_opts;
16613 mips_opts_stack = s;
16614 }
16615 else if (strcmp (name, "pop") == 0)
16616 {
16617 struct mips_option_stack *s;
16618
16619 s = mips_opts_stack;
16620 if (s == NULL)
16621 as_bad (_(".set pop with no .set push"));
16622 else
16623 {
16624 /* If we're changing the reorder mode we need to handle
16625 delay slots correctly. */
16626 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16627 start_noreorder ();
252b5132 16628 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16629 end_noreorder ();
252b5132
RH
16630
16631 mips_opts = s->options;
16632 mips_opts_stack = s->next;
16633 free (s);
16634 }
16635 }
aed1a261
RS
16636 else if (strcmp (name, "sym32") == 0)
16637 mips_opts.sym32 = TRUE;
16638 else if (strcmp (name, "nosym32") == 0)
16639 mips_opts.sym32 = FALSE;
e6559e01
JM
16640 else if (strchr (name, ','))
16641 {
16642 /* Generic ".set" directive; use the generic handler. */
16643 *input_line_pointer = ch;
16644 input_line_pointer = name;
16645 s_set (0);
16646 return;
16647 }
252b5132
RH
16648 else
16649 {
16650 as_warn (_("Tried to set unrecognized symbol: %s\n"), name);
16651 }
c6278170 16652 mips_check_isa_supports_ases ();
252b5132
RH
16653 *input_line_pointer = ch;
16654 demand_empty_rest_of_line ();
16655}
16656
16657/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16658 .option pic2. It means to generate SVR4 PIC calls. */
16659
16660static void
17a2f251 16661s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16662{
16663 mips_pic = SVR4_PIC;
143d77c5 16664 mips_abicalls = TRUE;
4d0d148d
TS
16665
16666 if (g_switch_seen && g_switch_value != 0)
16667 as_warn (_("-G may not be used with SVR4 PIC code"));
16668 g_switch_value = 0;
16669
252b5132
RH
16670 bfd_set_gp_size (stdoutput, 0);
16671 demand_empty_rest_of_line ();
16672}
16673
16674/* Handle the .cpload pseudo-op. This is used when generating SVR4
16675 PIC code. It sets the $gp register for the function based on the
16676 function address, which is in the register named in the argument.
16677 This uses a relocation against _gp_disp, which is handled specially
16678 by the linker. The result is:
16679 lui $gp,%hi(_gp_disp)
16680 addiu $gp,$gp,%lo(_gp_disp)
16681 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16682 The .cpload argument is normally $25 == $t9.
16683
16684 The -mno-shared option changes this to:
bbe506e8
TS
16685 lui $gp,%hi(__gnu_local_gp)
16686 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16687 and the argument is ignored. This saves an instruction, but the
16688 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16689 address for __gnu_local_gp. Thus code assembled with -mno-shared
16690 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16691
16692static void
17a2f251 16693s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16694{
16695 expressionS ex;
aa6975fb
ILT
16696 int reg;
16697 int in_shared;
252b5132 16698
6478892d
TS
16699 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16700 .cpload is ignored. */
16701 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16702 {
16703 s_ignore (0);
16704 return;
16705 }
16706
a276b80c
MR
16707 if (mips_opts.mips16)
16708 {
16709 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16710 ignore_rest_of_line ();
16711 return;
16712 }
16713
d3ecfc59 16714 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16715 if (mips_opts.noreorder == 0)
16716 as_warn (_(".cpload not in noreorder section"));
16717
aa6975fb
ILT
16718 reg = tc_get_register (0);
16719
16720 /* If we need to produce a 64-bit address, we are better off using
16721 the default instruction sequence. */
aed1a261 16722 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16723
252b5132 16724 ex.X_op = O_symbol;
bbe506e8
TS
16725 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16726 "__gnu_local_gp");
252b5132
RH
16727 ex.X_op_symbol = NULL;
16728 ex.X_add_number = 0;
16729
16730 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16731 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16732
8a75745d
MR
16733 mips_mark_labels ();
16734 mips_assembling_insn = TRUE;
16735
584892a6 16736 macro_start ();
67c0d1eb
RS
16737 macro_build_lui (&ex, mips_gp_register);
16738 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16739 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16740 if (in_shared)
16741 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16742 mips_gp_register, reg);
584892a6 16743 macro_end ();
252b5132 16744
8a75745d 16745 mips_assembling_insn = FALSE;
252b5132
RH
16746 demand_empty_rest_of_line ();
16747}
16748
6478892d
TS
16749/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16750 .cpsetup $reg1, offset|$reg2, label
16751
16752 If offset is given, this results in:
16753 sd $gp, offset($sp)
956cd1d6 16754 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16755 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16756 daddu $gp, $gp, $reg1
6478892d
TS
16757
16758 If $reg2 is given, this results in:
16759 daddu $reg2, $gp, $0
956cd1d6 16760 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16761 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16762 daddu $gp, $gp, $reg1
aa6975fb
ILT
16763 $reg1 is normally $25 == $t9.
16764
16765 The -mno-shared option replaces the last three instructions with
16766 lui $gp,%hi(_gp)
54f4ddb3 16767 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16768
6478892d 16769static void
17a2f251 16770s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16771{
16772 expressionS ex_off;
16773 expressionS ex_sym;
16774 int reg1;
6478892d 16775
8586fc66 16776 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16777 We also need NewABI support. */
16778 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16779 {
16780 s_ignore (0);
16781 return;
16782 }
16783
a276b80c
MR
16784 if (mips_opts.mips16)
16785 {
16786 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16787 ignore_rest_of_line ();
16788 return;
16789 }
16790
6478892d
TS
16791 reg1 = tc_get_register (0);
16792 SKIP_WHITESPACE ();
16793 if (*input_line_pointer != ',')
16794 {
16795 as_bad (_("missing argument separator ',' for .cpsetup"));
16796 return;
16797 }
16798 else
80245285 16799 ++input_line_pointer;
6478892d
TS
16800 SKIP_WHITESPACE ();
16801 if (*input_line_pointer == '$')
80245285
TS
16802 {
16803 mips_cpreturn_register = tc_get_register (0);
16804 mips_cpreturn_offset = -1;
16805 }
6478892d 16806 else
80245285
TS
16807 {
16808 mips_cpreturn_offset = get_absolute_expression ();
16809 mips_cpreturn_register = -1;
16810 }
6478892d
TS
16811 SKIP_WHITESPACE ();
16812 if (*input_line_pointer != ',')
16813 {
16814 as_bad (_("missing argument separator ',' for .cpsetup"));
16815 return;
16816 }
16817 else
f9419b05 16818 ++input_line_pointer;
6478892d 16819 SKIP_WHITESPACE ();
f21f8242 16820 expression (&ex_sym);
6478892d 16821
8a75745d
MR
16822 mips_mark_labels ();
16823 mips_assembling_insn = TRUE;
16824
584892a6 16825 macro_start ();
6478892d
TS
16826 if (mips_cpreturn_register == -1)
16827 {
16828 ex_off.X_op = O_constant;
16829 ex_off.X_add_symbol = NULL;
16830 ex_off.X_op_symbol = NULL;
16831 ex_off.X_add_number = mips_cpreturn_offset;
16832
67c0d1eb 16833 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16834 BFD_RELOC_LO16, SP);
6478892d
TS
16835 }
16836 else
67c0d1eb 16837 macro_build (NULL, "daddu", "d,v,t", mips_cpreturn_register,
17a2f251 16838 mips_gp_register, 0);
6478892d 16839
aed1a261 16840 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16841 {
df58fc94 16842 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16843 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16844 BFD_RELOC_HI16_S);
16845
16846 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16847 mips_gp_register, -1, BFD_RELOC_GPREL16,
16848 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16849
16850 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16851 mips_gp_register, reg1);
16852 }
16853 else
16854 {
16855 expressionS ex;
16856
16857 ex.X_op = O_symbol;
4184909a 16858 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16859 ex.X_op_symbol = NULL;
16860 ex.X_add_number = 0;
6e1304d8 16861
aa6975fb
ILT
16862 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16863 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16864
16865 macro_build_lui (&ex, mips_gp_register);
16866 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16867 mips_gp_register, BFD_RELOC_LO16);
16868 }
f21f8242 16869
584892a6 16870 macro_end ();
6478892d 16871
8a75745d 16872 mips_assembling_insn = FALSE;
6478892d
TS
16873 demand_empty_rest_of_line ();
16874}
16875
16876static void
17a2f251 16877s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16878{
16879 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16880 .cplocal is ignored. */
6478892d
TS
16881 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16882 {
16883 s_ignore (0);
16884 return;
16885 }
16886
a276b80c
MR
16887 if (mips_opts.mips16)
16888 {
16889 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16890 ignore_rest_of_line ();
16891 return;
16892 }
16893
6478892d 16894 mips_gp_register = tc_get_register (0);
85b51719 16895 demand_empty_rest_of_line ();
6478892d
TS
16896}
16897
252b5132
RH
16898/* Handle the .cprestore pseudo-op. This stores $gp into a given
16899 offset from $sp. The offset is remembered, and after making a PIC
16900 call $gp is restored from that location. */
16901
16902static void
17a2f251 16903s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16904{
16905 expressionS ex;
252b5132 16906
6478892d 16907 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16908 .cprestore is ignored. */
6478892d 16909 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16910 {
16911 s_ignore (0);
16912 return;
16913 }
16914
a276b80c
MR
16915 if (mips_opts.mips16)
16916 {
16917 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16918 ignore_rest_of_line ();
16919 return;
16920 }
16921
252b5132 16922 mips_cprestore_offset = get_absolute_expression ();
7a621144 16923 mips_cprestore_valid = 1;
252b5132
RH
16924
16925 ex.X_op = O_constant;
16926 ex.X_add_symbol = NULL;
16927 ex.X_op_symbol = NULL;
16928 ex.X_add_number = mips_cprestore_offset;
16929
8a75745d
MR
16930 mips_mark_labels ();
16931 mips_assembling_insn = TRUE;
16932
584892a6 16933 macro_start ();
67c0d1eb
RS
16934 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16935 SP, HAVE_64BIT_ADDRESSES);
584892a6 16936 macro_end ();
252b5132 16937
8a75745d 16938 mips_assembling_insn = FALSE;
252b5132
RH
16939 demand_empty_rest_of_line ();
16940}
16941
6478892d 16942/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16943 was given in the preceding .cpsetup, it results in:
6478892d 16944 ld $gp, offset($sp)
76b3015f 16945
6478892d 16946 If a register $reg2 was given there, it results in:
54f4ddb3
TS
16947 daddu $gp, $reg2, $0 */
16948
6478892d 16949static void
17a2f251 16950s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16951{
16952 expressionS ex;
6478892d
TS
16953
16954 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16955 We also need NewABI support. */
16956 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16957 {
16958 s_ignore (0);
16959 return;
16960 }
16961
a276b80c
MR
16962 if (mips_opts.mips16)
16963 {
16964 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16965 ignore_rest_of_line ();
16966 return;
16967 }
16968
8a75745d
MR
16969 mips_mark_labels ();
16970 mips_assembling_insn = TRUE;
16971
584892a6 16972 macro_start ();
6478892d
TS
16973 if (mips_cpreturn_register == -1)
16974 {
16975 ex.X_op = O_constant;
16976 ex.X_add_symbol = NULL;
16977 ex.X_op_symbol = NULL;
16978 ex.X_add_number = mips_cpreturn_offset;
16979
67c0d1eb 16980 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16981 }
16982 else
67c0d1eb 16983 macro_build (NULL, "daddu", "d,v,t", mips_gp_register,
17a2f251 16984 mips_cpreturn_register, 0);
584892a6 16985 macro_end ();
6478892d 16986
8a75745d 16987 mips_assembling_insn = FALSE;
6478892d
TS
16988 demand_empty_rest_of_line ();
16989}
16990
d0f13682
CLT
16991/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16992 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16993 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16994 debug information or MIPS16 TLS. */
741d6ea8
JM
16995
16996static void
d0f13682
CLT
16997s_tls_rel_directive (const size_t bytes, const char *dirstr,
16998 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16999{
17000 expressionS ex;
17001 char *p;
17002
17003 expression (&ex);
17004
17005 if (ex.X_op != O_symbol)
17006 {
d0f13682 17007 as_bad (_("Unsupported use of %s"), dirstr);
741d6ea8
JM
17008 ignore_rest_of_line ();
17009 }
17010
17011 p = frag_more (bytes);
17012 md_number_to_chars (p, 0, bytes);
d0f13682 17013 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 17014 demand_empty_rest_of_line ();
de64cffd 17015 mips_clear_insn_labels ();
741d6ea8
JM
17016}
17017
17018/* Handle .dtprelword. */
17019
17020static void
17021s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17022{
d0f13682 17023 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17024}
17025
17026/* Handle .dtpreldword. */
17027
17028static void
17029s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17030{
d0f13682
CLT
17031 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17032}
17033
17034/* Handle .tprelword. */
17035
17036static void
17037s_tprelword (int ignore ATTRIBUTE_UNUSED)
17038{
17039 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17040}
17041
17042/* Handle .tpreldword. */
17043
17044static void
17045s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17046{
17047 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17048}
17049
6478892d
TS
17050/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17051 code. It sets the offset to use in gp_rel relocations. */
17052
17053static void
17a2f251 17054s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17055{
17056 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17057 We also need NewABI support. */
17058 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17059 {
17060 s_ignore (0);
17061 return;
17062 }
17063
def2e0dd 17064 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17065
17066 demand_empty_rest_of_line ();
17067}
17068
252b5132
RH
17069/* Handle the .gpword pseudo-op. This is used when generating PIC
17070 code. It generates a 32 bit GP relative reloc. */
17071
17072static void
17a2f251 17073s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17074{
a8dbcb85
TS
17075 segment_info_type *si;
17076 struct insn_label_list *l;
252b5132
RH
17077 expressionS ex;
17078 char *p;
17079
17080 /* When not generating PIC code, this is treated as .word. */
17081 if (mips_pic != SVR4_PIC)
17082 {
17083 s_cons (2);
17084 return;
17085 }
17086
a8dbcb85
TS
17087 si = seg_info (now_seg);
17088 l = si->label_list;
7d10b47d 17089 mips_emit_delays ();
252b5132 17090 if (auto_align)
462427c4 17091 mips_align (2, 0, l);
252b5132
RH
17092
17093 expression (&ex);
a1facbec 17094 mips_clear_insn_labels ();
252b5132
RH
17095
17096 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17097 {
17098 as_bad (_("Unsupported use of .gpword"));
17099 ignore_rest_of_line ();
17100 }
17101
17102 p = frag_more (4);
17a2f251 17103 md_number_to_chars (p, 0, 4);
b34976b6 17104 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17105 BFD_RELOC_GPREL32);
252b5132
RH
17106
17107 demand_empty_rest_of_line ();
17108}
17109
10181a0d 17110static void
17a2f251 17111s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17112{
a8dbcb85
TS
17113 segment_info_type *si;
17114 struct insn_label_list *l;
10181a0d
AO
17115 expressionS ex;
17116 char *p;
17117
17118 /* When not generating PIC code, this is treated as .dword. */
17119 if (mips_pic != SVR4_PIC)
17120 {
17121 s_cons (3);
17122 return;
17123 }
17124
a8dbcb85
TS
17125 si = seg_info (now_seg);
17126 l = si->label_list;
7d10b47d 17127 mips_emit_delays ();
10181a0d 17128 if (auto_align)
462427c4 17129 mips_align (3, 0, l);
10181a0d
AO
17130
17131 expression (&ex);
a1facbec 17132 mips_clear_insn_labels ();
10181a0d
AO
17133
17134 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17135 {
17136 as_bad (_("Unsupported use of .gpdword"));
17137 ignore_rest_of_line ();
17138 }
17139
17140 p = frag_more (8);
17a2f251 17141 md_number_to_chars (p, 0, 8);
a105a300 17142 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17143 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17144
17145 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17146 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17147 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17148
17149 demand_empty_rest_of_line ();
17150}
17151
a3f278e2
CM
17152/* Handle the .ehword pseudo-op. This is used when generating unwinding
17153 tables. It generates a R_MIPS_EH reloc. */
17154
17155static void
17156s_ehword (int ignore ATTRIBUTE_UNUSED)
17157{
17158 expressionS ex;
17159 char *p;
17160
17161 mips_emit_delays ();
17162
17163 expression (&ex);
17164 mips_clear_insn_labels ();
17165
17166 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17167 {
17168 as_bad (_("Unsupported use of .ehword"));
17169 ignore_rest_of_line ();
17170 }
17171
17172 p = frag_more (4);
17173 md_number_to_chars (p, 0, 4);
17174 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
17175 BFD_RELOC_MIPS_EH);
17176
17177 demand_empty_rest_of_line ();
17178}
17179
252b5132
RH
17180/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17181 tables in SVR4 PIC code. */
17182
17183static void
17a2f251 17184s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17185{
252b5132
RH
17186 int reg;
17187
10181a0d
AO
17188 /* This is ignored when not generating SVR4 PIC code. */
17189 if (mips_pic != SVR4_PIC)
252b5132
RH
17190 {
17191 s_ignore (0);
17192 return;
17193 }
17194
8a75745d
MR
17195 mips_mark_labels ();
17196 mips_assembling_insn = TRUE;
17197
252b5132 17198 /* Add $gp to the register named as an argument. */
584892a6 17199 macro_start ();
252b5132 17200 reg = tc_get_register (0);
67c0d1eb 17201 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17202 macro_end ();
252b5132 17203
8a75745d 17204 mips_assembling_insn = FALSE;
bdaaa2e1 17205 demand_empty_rest_of_line ();
252b5132
RH
17206}
17207
17208/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17209 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17210 such as generating jalx instructions when needed. We also make
17211 them odd for the duration of the assembly, in order to generate the
17212 right sort of code. We will make them even in the adjust_symtab
17213 routine, while leaving them marked. This is convenient for the
17214 debugger and the disassembler. The linker knows to make them odd
17215 again. */
17216
17217static void
17a2f251 17218s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17219{
df58fc94 17220 mips_mark_labels ();
252b5132
RH
17221
17222 demand_empty_rest_of_line ();
17223}
17224
754e2bb9
RS
17225/* Handle a .stab[snd] directive. Ideally these directives would be
17226 implemented in a transparent way, so that removing them would not
17227 have any effect on the generated instructions. However, s_stab
17228 internally changes the section, so in practice we need to decide
17229 now whether the preceding label marks compressed code. We do not
17230 support changing the compression mode of a label after a .stab*
17231 directive, such as in:
17232
17233 foo:
17234 .stabs ...
17235 .set mips16
17236
17237 so the current mode wins. */
252b5132
RH
17238
17239static void
17a2f251 17240s_mips_stab (int type)
252b5132 17241{
754e2bb9 17242 mips_mark_labels ();
252b5132
RH
17243 s_stab (type);
17244}
17245
54f4ddb3 17246/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17247
17248static void
17a2f251 17249s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17250{
17251 char *name;
17252 int c;
17253 symbolS *symbolP;
17254 expressionS exp;
17255
17256 name = input_line_pointer;
17257 c = get_symbol_end ();
17258 symbolP = symbol_find_or_make (name);
17259 S_SET_WEAK (symbolP);
17260 *input_line_pointer = c;
17261
17262 SKIP_WHITESPACE ();
17263
17264 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17265 {
17266 if (S_IS_DEFINED (symbolP))
17267 {
20203fb9 17268 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17269 S_GET_NAME (symbolP));
17270 ignore_rest_of_line ();
17271 return;
17272 }
bdaaa2e1 17273
252b5132
RH
17274 if (*input_line_pointer == ',')
17275 {
17276 ++input_line_pointer;
17277 SKIP_WHITESPACE ();
17278 }
bdaaa2e1 17279
252b5132
RH
17280 expression (&exp);
17281 if (exp.X_op != O_symbol)
17282 {
20203fb9 17283 as_bad (_("bad .weakext directive"));
98d3f06f 17284 ignore_rest_of_line ();
252b5132
RH
17285 return;
17286 }
49309057 17287 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17288 }
17289
17290 demand_empty_rest_of_line ();
17291}
17292
17293/* Parse a register string into a number. Called from the ECOFF code
17294 to parse .frame. The argument is non-zero if this is the frame
17295 register, so that we can record it in mips_frame_reg. */
17296
17297int
17a2f251 17298tc_get_register (int frame)
252b5132 17299{
707bfff6 17300 unsigned int reg;
252b5132
RH
17301
17302 SKIP_WHITESPACE ();
707bfff6
TS
17303 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17304 reg = 0;
252b5132 17305 if (frame)
7a621144
DJ
17306 {
17307 mips_frame_reg = reg != 0 ? reg : SP;
17308 mips_frame_reg_valid = 1;
17309 mips_cprestore_valid = 0;
17310 }
252b5132
RH
17311 return reg;
17312}
17313
17314valueT
17a2f251 17315md_section_align (asection *seg, valueT addr)
252b5132
RH
17316{
17317 int align = bfd_get_section_alignment (stdoutput, seg);
17318
f3ded42a
RS
17319 /* We don't need to align ELF sections to the full alignment.
17320 However, Irix 5 may prefer that we align them at least to a 16
17321 byte boundary. We don't bother to align the sections if we
17322 are targeted for an embedded system. */
17323 if (strncmp (TARGET_OS, "elf", 3) == 0)
17324 return addr;
17325 if (align > 4)
17326 align = 4;
252b5132
RH
17327
17328 return ((addr + (1 << align) - 1) & (-1 << align));
17329}
17330
17331/* Utility routine, called from above as well. If called while the
17332 input file is still being read, it's only an approximation. (For
17333 example, a symbol may later become defined which appeared to be
17334 undefined earlier.) */
17335
17336static int
17a2f251 17337nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17338{
17339 if (sym == 0)
17340 return 0;
17341
4d0d148d 17342 if (g_switch_value > 0)
252b5132
RH
17343 {
17344 const char *symname;
17345 int change;
17346
c9914766 17347 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17348 register. It can be if it is smaller than the -G size or if
17349 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17350 not be referenced off the $gp, although it appears as though
252b5132
RH
17351 they can. */
17352 symname = S_GET_NAME (sym);
17353 if (symname != (const char *) NULL
17354 && (strcmp (symname, "eprol") == 0
17355 || strcmp (symname, "etext") == 0
17356 || strcmp (symname, "_gp") == 0
17357 || strcmp (symname, "edata") == 0
17358 || strcmp (symname, "_fbss") == 0
17359 || strcmp (symname, "_fdata") == 0
17360 || strcmp (symname, "_ftext") == 0
17361 || strcmp (symname, "end") == 0
17362 || strcmp (symname, "_gp_disp") == 0))
17363 change = 1;
17364 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17365 && (0
17366#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17367 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17368 && (symbol_get_obj (sym)->ecoff_extern_size
17369 <= g_switch_value))
252b5132
RH
17370#endif
17371 /* We must defer this decision until after the whole
17372 file has been read, since there might be a .extern
17373 after the first use of this symbol. */
17374 || (before_relaxing
17375#ifndef NO_ECOFF_DEBUGGING
49309057 17376 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17377#endif
17378 && S_GET_VALUE (sym) == 0)
17379 || (S_GET_VALUE (sym) != 0
17380 && S_GET_VALUE (sym) <= g_switch_value)))
17381 change = 0;
17382 else
17383 {
17384 const char *segname;
17385
17386 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17387 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17388 && strcmp (segname, ".lit4") != 0);
17389 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17390 && strcmp (segname, ".sbss") != 0
17391 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17392 && strncmp (segname, ".sbss.", 6) != 0
17393 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17394 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17395 }
17396 return change;
17397 }
17398 else
c9914766 17399 /* We are not optimizing for the $gp register. */
252b5132
RH
17400 return 1;
17401}
17402
5919d012
RS
17403
17404/* Return true if the given symbol should be considered local for SVR4 PIC. */
17405
17406static bfd_boolean
17a2f251 17407pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
17408{
17409 asection *symsec;
5919d012
RS
17410
17411 /* Handle the case of a symbol equated to another symbol. */
17412 while (symbol_equated_reloc_p (sym))
17413 {
17414 symbolS *n;
17415
5f0fe04b 17416 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17417 n = symbol_get_value_expression (sym)->X_add_symbol;
17418 if (n == sym)
17419 break;
17420 sym = n;
17421 }
17422
df1f3cda
DD
17423 if (symbol_section_p (sym))
17424 return TRUE;
17425
5919d012
RS
17426 symsec = S_GET_SEGMENT (sym);
17427
5919d012 17428 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17429 return (!bfd_is_und_section (symsec)
17430 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
17431 && !bfd_is_com_section (symsec)
17432 && !s_is_linkonce (sym, segtype)
5919d012 17433 /* A global or weak symbol is treated as external. */
f3ded42a 17434 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
17435}
17436
17437
252b5132
RH
17438/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17439 extended opcode. SEC is the section the frag is in. */
17440
17441static int
17a2f251 17442mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
17443{
17444 int type;
3994f87e 17445 const struct mips16_immed_operand *op;
252b5132
RH
17446 offsetT val;
17447 int mintiny, maxtiny;
17448 segT symsec;
98aa84af 17449 fragS *sym_frag;
252b5132
RH
17450
17451 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17452 return 0;
17453 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17454 return 1;
17455
17456 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17457 op = mips16_immed_operands;
17458 while (op->type != type)
17459 {
17460 ++op;
9c2799c2 17461 gas_assert (op < mips16_immed_operands + MIPS16_NUM_IMMED);
252b5132
RH
17462 }
17463
17464 if (op->unsp)
17465 {
17466 if (type == '<' || type == '>' || type == '[' || type == ']')
17467 {
17468 mintiny = 1;
17469 maxtiny = 1 << op->nbits;
17470 }
17471 else
17472 {
17473 mintiny = 0;
17474 maxtiny = (1 << op->nbits) - 1;
17475 }
17476 }
17477 else
17478 {
17479 mintiny = - (1 << (op->nbits - 1));
17480 maxtiny = (1 << (op->nbits - 1)) - 1;
17481 }
17482
98aa84af 17483 sym_frag = symbol_get_frag (fragp->fr_symbol);
ac62c346 17484 val = S_GET_VALUE (fragp->fr_symbol);
98aa84af 17485 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132
RH
17486
17487 if (op->pcrel)
17488 {
17489 addressT addr;
17490
17491 /* We won't have the section when we are called from
17492 mips_relax_frag. However, we will always have been called
17493 from md_estimate_size_before_relax first. If this is a
17494 branch to a different section, we mark it as such. If SEC is
17495 NULL, and the frag is not marked, then it must be a branch to
17496 the same section. */
17497 if (sec == NULL)
17498 {
17499 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
17500 return 1;
17501 }
17502 else
17503 {
98aa84af 17504 /* Must have been called from md_estimate_size_before_relax. */
252b5132
RH
17505 if (symsec != sec)
17506 {
17507 fragp->fr_subtype =
17508 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17509
17510 /* FIXME: We should support this, and let the linker
17511 catch branches and loads that are out of range. */
17512 as_bad_where (fragp->fr_file, fragp->fr_line,
17513 _("unsupported PC relative reference to different section"));
17514
17515 return 1;
17516 }
98aa84af
AM
17517 if (fragp != sym_frag && sym_frag->fr_address == 0)
17518 /* Assume non-extended on the first relaxation pass.
17519 The address we have calculated will be bogus if this is
17520 a forward branch to another frag, as the forward frag
17521 will have fr_address == 0. */
17522 return 0;
252b5132
RH
17523 }
17524
17525 /* In this case, we know for sure that the symbol fragment is in
98aa84af
AM
17526 the same section. If the relax_marker of the symbol fragment
17527 differs from the relax_marker of this fragment, we have not
17528 yet adjusted the symbol fragment fr_address. We want to add
252b5132
RH
17529 in STRETCH in order to get a better estimate of the address.
17530 This particularly matters because of the shift bits. */
17531 if (stretch != 0
98aa84af 17532 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
17533 {
17534 fragS *f;
17535
17536 /* Adjust stretch for any alignment frag. Note that if have
17537 been expanding the earlier code, the symbol may be
17538 defined in what appears to be an earlier frag. FIXME:
17539 This doesn't handle the fr_subtype field, which specifies
17540 a maximum number of bytes to skip when doing an
17541 alignment. */
98aa84af 17542 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
17543 {
17544 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17545 {
17546 if (stretch < 0)
17547 stretch = - ((- stretch)
17548 & ~ ((1 << (int) f->fr_offset) - 1));
17549 else
17550 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
17551 if (stretch == 0)
17552 break;
17553 }
17554 }
17555 if (f != NULL)
17556 val += stretch;
17557 }
17558
17559 addr = fragp->fr_address + fragp->fr_fix;
17560
17561 /* The base address rules are complicated. The base address of
17562 a branch is the following instruction. The base address of a
17563 PC relative load or add is the instruction itself, but if it
17564 is in a delay slot (in which case it can not be extended) use
17565 the address of the instruction whose delay slot it is in. */
17566 if (type == 'p' || type == 'q')
17567 {
17568 addr += 2;
17569
17570 /* If we are currently assuming that this frag should be
17571 extended, then, the current address is two bytes
bdaaa2e1 17572 higher. */
252b5132
RH
17573 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17574 addr += 2;
17575
17576 /* Ignore the low bit in the target, since it will be set
17577 for a text label. */
17578 if ((val & 1) != 0)
17579 --val;
17580 }
17581 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17582 addr -= 4;
17583 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17584 addr -= 2;
17585
17586 val -= addr & ~ ((1 << op->shift) - 1);
17587
17588 /* Branch offsets have an implicit 0 in the lowest bit. */
17589 if (type == 'p' || type == 'q')
17590 val /= 2;
17591
17592 /* If any of the shifted bits are set, we must use an extended
17593 opcode. If the address depends on the size of this
17594 instruction, this can lead to a loop, so we arrange to always
17595 use an extended opcode. We only check this when we are in
17596 the main relaxation loop, when SEC is NULL. */
17597 if ((val & ((1 << op->shift) - 1)) != 0 && sec == NULL)
17598 {
17599 fragp->fr_subtype =
17600 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17601 return 1;
17602 }
17603
17604 /* If we are about to mark a frag as extended because the value
17605 is precisely maxtiny + 1, then there is a chance of an
17606 infinite loop as in the following code:
17607 la $4,foo
17608 .skip 1020
17609 .align 2
17610 foo:
17611 In this case when the la is extended, foo is 0x3fc bytes
17612 away, so the la can be shrunk, but then foo is 0x400 away, so
17613 the la must be extended. To avoid this loop, we mark the
17614 frag as extended if it was small, and is about to become
17615 extended with a value of maxtiny + 1. */
17616 if (val == ((maxtiny + 1) << op->shift)
17617 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype)
17618 && sec == NULL)
17619 {
17620 fragp->fr_subtype =
17621 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
17622 return 1;
17623 }
17624 }
17625 else if (symsec != absolute_section && sec != NULL)
17626 as_bad_where (fragp->fr_file, fragp->fr_line, _("unsupported relocation"));
17627
17628 if ((val & ((1 << op->shift) - 1)) != 0
17629 || val < (mintiny << op->shift)
17630 || val > (maxtiny << op->shift))
17631 return 1;
17632 else
17633 return 0;
17634}
17635
4a6a3df4
AO
17636/* Compute the length of a branch sequence, and adjust the
17637 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17638 worst-case length is computed, with UPDATE being used to indicate
17639 whether an unconditional (-1), branch-likely (+1) or regular (0)
17640 branch is to be computed. */
17641static int
17a2f251 17642relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17643{
b34976b6 17644 bfd_boolean toofar;
4a6a3df4
AO
17645 int length;
17646
17647 if (fragp
17648 && S_IS_DEFINED (fragp->fr_symbol)
17649 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17650 {
17651 addressT addr;
17652 offsetT val;
17653
17654 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17655
17656 addr = fragp->fr_address + fragp->fr_fix + 4;
17657
17658 val -= addr;
17659
17660 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17661 }
17662 else if (fragp)
17663 /* If the symbol is not defined or it's in a different segment,
17664 assume the user knows what's going on and emit a short
17665 branch. */
b34976b6 17666 toofar = FALSE;
4a6a3df4 17667 else
b34976b6 17668 toofar = TRUE;
4a6a3df4
AO
17669
17670 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17671 fragp->fr_subtype
66b3e8da
MR
17672 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
17673 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17674 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17675 RELAX_BRANCH_LINK (fragp->fr_subtype),
17676 toofar);
17677
17678 length = 4;
17679 if (toofar)
17680 {
17681 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17682 length += 8;
17683
17684 if (mips_pic != NO_PIC)
17685 {
17686 /* Additional space for PIC loading of target address. */
17687 length += 8;
17688 if (mips_opts.isa == ISA_MIPS1)
17689 /* Additional space for $at-stabilizing nop. */
17690 length += 4;
17691 }
17692
17693 /* If branch is conditional. */
17694 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17695 length += 8;
17696 }
b34976b6 17697
4a6a3df4
AO
17698 return length;
17699}
17700
df58fc94
RS
17701/* Compute the length of a branch sequence, and adjust the
17702 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17703 worst-case length is computed, with UPDATE being used to indicate
17704 whether an unconditional (-1), or regular (0) branch is to be
17705 computed. */
17706
17707static int
17708relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17709{
17710 bfd_boolean toofar;
17711 int length;
17712
17713 if (fragp
17714 && S_IS_DEFINED (fragp->fr_symbol)
17715 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17716 {
17717 addressT addr;
17718 offsetT val;
17719
17720 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17721 /* Ignore the low bit in the target, since it will be set
17722 for a text label. */
17723 if ((val & 1) != 0)
17724 --val;
17725
17726 addr = fragp->fr_address + fragp->fr_fix + 4;
17727
17728 val -= addr;
17729
17730 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17731 }
17732 else if (fragp)
17733 /* If the symbol is not defined or it's in a different segment,
17734 assume the user knows what's going on and emit a short
17735 branch. */
17736 toofar = FALSE;
17737 else
17738 toofar = TRUE;
17739
17740 if (fragp && update
17741 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17742 fragp->fr_subtype = (toofar
17743 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17744 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17745
17746 length = 4;
17747 if (toofar)
17748 {
17749 bfd_boolean compact_known = fragp != NULL;
17750 bfd_boolean compact = FALSE;
17751 bfd_boolean uncond;
17752
17753 if (compact_known)
17754 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17755 if (fragp)
17756 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
17757 else
17758 uncond = update < 0;
17759
17760 /* If label is out of range, we turn branch <br>:
17761
17762 <br> label # 4 bytes
17763 0:
17764
17765 into:
17766
17767 j label # 4 bytes
17768 nop # 2 bytes if compact && !PIC
17769 0:
17770 */
17771 if (mips_pic == NO_PIC && (!compact_known || compact))
17772 length += 2;
17773
17774 /* If assembling PIC code, we further turn:
17775
17776 j label # 4 bytes
17777
17778 into:
17779
17780 lw/ld at, %got(label)(gp) # 4 bytes
17781 d/addiu at, %lo(label) # 4 bytes
17782 jr/c at # 2 bytes
17783 */
17784 if (mips_pic != NO_PIC)
17785 length += 6;
17786
17787 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17788
17789 <brneg> 0f # 4 bytes
17790 nop # 2 bytes if !compact
17791 */
17792 if (!uncond)
17793 length += (compact_known && compact) ? 4 : 6;
17794 }
17795
17796 return length;
17797}
17798
17799/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17800 bit accordingly. */
17801
17802static int
17803relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17804{
17805 bfd_boolean toofar;
17806
df58fc94
RS
17807 if (fragp
17808 && S_IS_DEFINED (fragp->fr_symbol)
17809 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17810 {
17811 addressT addr;
17812 offsetT val;
17813 int type;
17814
17815 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17816 /* Ignore the low bit in the target, since it will be set
17817 for a text label. */
17818 if ((val & 1) != 0)
17819 --val;
17820
17821 /* Assume this is a 2-byte branch. */
17822 addr = fragp->fr_address + fragp->fr_fix + 2;
17823
17824 /* We try to avoid the infinite loop by not adding 2 more bytes for
17825 long branches. */
17826
17827 val -= addr;
17828
17829 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17830 if (type == 'D')
17831 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17832 else if (type == 'E')
17833 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17834 else
17835 abort ();
17836 }
17837 else
17838 /* If the symbol is not defined or it's in a different segment,
17839 we emit a normal 32-bit branch. */
17840 toofar = TRUE;
17841
17842 if (fragp && update
17843 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17844 fragp->fr_subtype
17845 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17846 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17847
17848 if (toofar)
17849 return 4;
17850
17851 return 2;
17852}
17853
252b5132
RH
17854/* Estimate the size of a frag before relaxing. Unless this is the
17855 mips16, we are not really relaxing here, and the final size is
17856 encoded in the subtype information. For the mips16, we have to
17857 decide whether we are using an extended opcode or not. */
17858
252b5132 17859int
17a2f251 17860md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17861{
5919d012 17862 int change;
252b5132 17863
4a6a3df4
AO
17864 if (RELAX_BRANCH_P (fragp->fr_subtype))
17865 {
17866
b34976b6
AM
17867 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17868
4a6a3df4
AO
17869 return fragp->fr_var;
17870 }
17871
252b5132 17872 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17873 /* We don't want to modify the EXTENDED bit here; it might get us
17874 into infinite loops. We change it only in mips_relax_frag(). */
17875 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17876
df58fc94
RS
17877 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17878 {
17879 int length = 4;
17880
17881 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17882 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17883 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17884 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17885 fragp->fr_var = length;
17886
17887 return length;
17888 }
17889
252b5132 17890 if (mips_pic == NO_PIC)
5919d012 17891 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17892 else if (mips_pic == SVR4_PIC)
5919d012 17893 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17894 else if (mips_pic == VXWORKS_PIC)
17895 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17896 change = 0;
252b5132
RH
17897 else
17898 abort ();
17899
17900 if (change)
17901 {
4d7206a2 17902 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17903 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17904 }
4d7206a2
RS
17905 else
17906 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17907}
17908
17909/* This is called to see whether a reloc against a defined symbol
de7e6852 17910 should be converted into a reloc against a section. */
252b5132
RH
17911
17912int
17a2f251 17913mips_fix_adjustable (fixS *fixp)
252b5132 17914{
252b5132
RH
17915 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17916 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17917 return 0;
a161fe53 17918
252b5132
RH
17919 if (fixp->fx_addsy == NULL)
17920 return 1;
a161fe53 17921
de7e6852
RS
17922 /* If symbol SYM is in a mergeable section, relocations of the form
17923 SYM + 0 can usually be made section-relative. The mergeable data
17924 is then identified by the section offset rather than by the symbol.
17925
17926 However, if we're generating REL LO16 relocations, the offset is split
17927 between the LO16 and parterning high part relocation. The linker will
17928 need to recalculate the complete offset in order to correctly identify
17929 the merge data.
17930
17931 The linker has traditionally not looked for the parterning high part
17932 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17933 placed anywhere. Rather than break backwards compatibility by changing
17934 this, it seems better not to force the issue, and instead keep the
17935 original symbol. This will work with either linker behavior. */
738e5348 17936 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17937 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17938 && HAVE_IN_PLACE_ADDENDS
17939 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17940 return 0;
17941
ce70d90a 17942 /* There is no place to store an in-place offset for JALR relocations.
2de39019
CM
17943 Likewise an in-range offset of limited PC-relative relocations may
17944 overflow the in-place relocatable field if recalculated against the
17945 start address of the symbol's containing section. */
ce70d90a 17946 if (HAVE_IN_PLACE_ADDENDS
2de39019
CM
17947 && (limited_pcrel_reloc_p (fixp->fx_r_type)
17948 || jalr_reloc_p (fixp->fx_r_type)))
1180b5a4
RS
17949 return 0;
17950
b314ec0e
RS
17951 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17952 to a floating-point stub. The same is true for non-R_MIPS16_26
17953 relocations against MIPS16 functions; in this case, the stub becomes
17954 the function's canonical address.
17955
17956 Floating-point stubs are stored in unique .mips16.call.* or
17957 .mips16.fn.* sections. If a stub T for function F is in section S,
17958 the first relocation in section S must be against F; this is how the
17959 linker determines the target function. All relocations that might
17960 resolve to T must also be against F. We therefore have the following
17961 restrictions, which are given in an intentionally-redundant way:
17962
17963 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17964 symbols.
17965
17966 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17967 if that stub might be used.
17968
17969 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17970 symbols.
17971
17972 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17973 that stub might be used.
17974
17975 There is a further restriction:
17976
df58fc94
RS
17977 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
17978 R_MICROMIPS_26_S1) against MIPS16 or microMIPS symbols on
17979 targets with in-place addends; the relocation field cannot
b314ec0e
RS
17980 encode the low bit.
17981
df58fc94
RS
17982 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
17983 against a MIPS16 symbol. We deal with (5) by by not reducing any
17984 such relocations on REL targets.
b314ec0e
RS
17985
17986 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17987 relocation against some symbol R, no relocation against R may be
17988 reduced. (Note that this deals with (2) as well as (1) because
17989 relocations against global symbols will never be reduced on ELF
17990 targets.) This approach is a little simpler than trying to detect
17991 stub sections, and gives the "all or nothing" per-symbol consistency
17992 that we have for MIPS16 symbols. */
f3ded42a 17993 if (fixp->fx_subsy == NULL
30c09090 17994 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
df58fc94
RS
17995 || *symbol_get_tc (fixp->fx_addsy)
17996 || (HAVE_IN_PLACE_ADDENDS
17997 && ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
17998 && jmp_reloc_p (fixp->fx_r_type))))
252b5132 17999 return 0;
a161fe53 18000
252b5132
RH
18001 return 1;
18002}
18003
18004/* Translate internal representation of relocation info to BFD target
18005 format. */
18006
18007arelent **
17a2f251 18008tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18009{
18010 static arelent *retval[4];
18011 arelent *reloc;
18012 bfd_reloc_code_real_type code;
18013
4b0cff4e
TS
18014 memset (retval, 0, sizeof(retval));
18015 reloc = retval[0] = (arelent *) xcalloc (1, sizeof (arelent));
49309057
ILT
18016 reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
18017 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18018 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18019
bad36eac
DJ
18020 if (fixp->fx_pcrel)
18021 {
df58fc94
RS
18022 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
18023 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18024 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6
CM
18025 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
18026 || fixp->fx_r_type == BFD_RELOC_32_PCREL);
bad36eac
DJ
18027
18028 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18029 Relocations want only the symbol offset. */
18030 reloc->addend = fixp->fx_addnumber + reloc->address;
bad36eac
DJ
18031 }
18032 else
18033 reloc->addend = fixp->fx_addnumber;
252b5132 18034
438c16b8
TS
18035 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18036 entry to be used in the relocation's section offset. */
18037 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18038 {
18039 reloc->address = reloc->addend;
18040 reloc->addend = 0;
18041 }
18042
252b5132 18043 code = fixp->fx_r_type;
252b5132 18044
bad36eac 18045 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18046 if (reloc->howto == NULL)
18047 {
18048 as_bad_where (fixp->fx_file, fixp->fx_line,
18049 _("Can not represent %s relocation in this object file format"),
18050 bfd_get_reloc_code_name (code));
18051 retval[0] = NULL;
18052 }
18053
18054 return retval;
18055}
18056
18057/* Relax a machine dependent frag. This returns the amount by which
18058 the current size of the frag should change. */
18059
18060int
17a2f251 18061mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18062{
4a6a3df4
AO
18063 if (RELAX_BRANCH_P (fragp->fr_subtype))
18064 {
18065 offsetT old_var = fragp->fr_var;
b34976b6
AM
18066
18067 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18068
18069 return fragp->fr_var - old_var;
18070 }
18071
df58fc94
RS
18072 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18073 {
18074 offsetT old_var = fragp->fr_var;
18075 offsetT new_var = 4;
18076
18077 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18078 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18079 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18080 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18081 fragp->fr_var = new_var;
18082
18083 return new_var - old_var;
18084 }
18085
252b5132
RH
18086 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18087 return 0;
18088
c4e7957c 18089 if (mips16_extended_frag (fragp, NULL, stretch))
252b5132
RH
18090 {
18091 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18092 return 0;
18093 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18094 return 2;
18095 }
18096 else
18097 {
18098 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18099 return 0;
18100 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18101 return -2;
18102 }
18103
18104 return 0;
18105}
18106
18107/* Convert a machine dependent frag. */
18108
18109void
17a2f251 18110md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18111{
4a6a3df4
AO
18112 if (RELAX_BRANCH_P (fragp->fr_subtype))
18113 {
4d68580a 18114 char *buf;
4a6a3df4
AO
18115 unsigned long insn;
18116 expressionS exp;
18117 fixS *fixp;
b34976b6 18118
4d68580a
RS
18119 buf = fragp->fr_literal + fragp->fr_fix;
18120 insn = read_insn (buf);
b34976b6 18121
4a6a3df4
AO
18122 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18123 {
18124 /* We generate a fixup instead of applying it right now
18125 because, if there are linker relaxations, we're going to
18126 need the relocations. */
18127 exp.X_op = O_symbol;
18128 exp.X_add_symbol = fragp->fr_symbol;
18129 exp.X_add_number = fragp->fr_offset;
18130
4d68580a
RS
18131 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18132 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18133 fixp->fx_file = fragp->fr_file;
18134 fixp->fx_line = fragp->fr_line;
b34976b6 18135
4d68580a 18136 buf = write_insn (buf, insn);
4a6a3df4
AO
18137 }
18138 else
18139 {
18140 int i;
18141
18142 as_warn_where (fragp->fr_file, fragp->fr_line,
5c4f07ba 18143 _("Relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18144
18145 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18146 goto uncond;
18147
18148 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18149 {
18150 /* Reverse the branch. */
18151 switch ((insn >> 28) & 0xf)
18152 {
18153 case 4:
3bf0dbfb
MR
18154 /* bc[0-3][tf]l? instructions can have the condition
18155 reversed by tweaking a single TF bit, and their
18156 opcodes all have 0x4???????. */
18157 gas_assert ((insn & 0xf3e00000) == 0x41000000);
4a6a3df4
AO
18158 insn ^= 0x00010000;
18159 break;
18160
18161 case 0:
18162 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18163 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18164 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18165 insn ^= 0x00010000;
18166 break;
b34976b6 18167
4a6a3df4
AO
18168 case 1:
18169 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18170 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18171 insn ^= 0x04000000;
18172 break;
18173
18174 default:
18175 abort ();
18176 }
18177 }
18178
18179 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18180 {
18181 /* Clear the and-link bit. */
9c2799c2 18182 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18183
54f4ddb3
TS
18184 /* bltzal 0x04100000 bgezal 0x04110000
18185 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18186 insn &= ~0x00100000;
18187 }
18188
18189 /* Branch over the branch (if the branch was likely) or the
18190 full jump (not likely case). Compute the offset from the
18191 current instruction to branch to. */
18192 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18193 i = 16;
18194 else
18195 {
18196 /* How many bytes in instructions we've already emitted? */
4d68580a 18197 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18198 /* How many bytes in instructions from here to the end? */
18199 i = fragp->fr_var - i;
18200 }
18201 /* Convert to instruction count. */
18202 i >>= 2;
18203 /* Branch counts from the next instruction. */
b34976b6 18204 i--;
4a6a3df4
AO
18205 insn |= i;
18206 /* Branch over the jump. */
4d68580a 18207 buf = write_insn (buf, insn);
4a6a3df4 18208
54f4ddb3 18209 /* nop */
4d68580a 18210 buf = write_insn (buf, 0);
4a6a3df4
AO
18211
18212 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18213 {
18214 /* beql $0, $0, 2f */
18215 insn = 0x50000000;
18216 /* Compute the PC offset from the current instruction to
18217 the end of the variable frag. */
18218 /* How many bytes in instructions we've already emitted? */
4d68580a 18219 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18220 /* How many bytes in instructions from here to the end? */
18221 i = fragp->fr_var - i;
18222 /* Convert to instruction count. */
18223 i >>= 2;
18224 /* Don't decrement i, because we want to branch over the
18225 delay slot. */
4a6a3df4 18226 insn |= i;
4a6a3df4 18227
4d68580a
RS
18228 buf = write_insn (buf, insn);
18229 buf = write_insn (buf, 0);
4a6a3df4
AO
18230 }
18231
18232 uncond:
18233 if (mips_pic == NO_PIC)
18234 {
18235 /* j or jal. */
18236 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18237 ? 0x0c000000 : 0x08000000);
18238 exp.X_op = O_symbol;
18239 exp.X_add_symbol = fragp->fr_symbol;
18240 exp.X_add_number = fragp->fr_offset;
18241
4d68580a
RS
18242 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18243 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18244 fixp->fx_file = fragp->fr_file;
18245 fixp->fx_line = fragp->fr_line;
18246
4d68580a 18247 buf = write_insn (buf, insn);
4a6a3df4
AO
18248 }
18249 else
18250 {
66b3e8da
MR
18251 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18252
4a6a3df4 18253 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18254 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18255 insn |= at << OP_SH_RT;
4a6a3df4
AO
18256 exp.X_op = O_symbol;
18257 exp.X_add_symbol = fragp->fr_symbol;
18258 exp.X_add_number = fragp->fr_offset;
18259
18260 if (fragp->fr_offset)
18261 {
18262 exp.X_add_symbol = make_expr_symbol (&exp);
18263 exp.X_add_number = 0;
18264 }
18265
4d68580a
RS
18266 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18267 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18268 fixp->fx_file = fragp->fr_file;
18269 fixp->fx_line = fragp->fr_line;
18270
4d68580a 18271 buf = write_insn (buf, insn);
b34976b6 18272
4a6a3df4 18273 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18274 /* nop */
18275 buf = write_insn (buf, 0);
4a6a3df4
AO
18276
18277 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18278 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18279 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18280
4d68580a
RS
18281 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18282 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18283 fixp->fx_file = fragp->fr_file;
18284 fixp->fx_line = fragp->fr_line;
b34976b6 18285
4d68580a 18286 buf = write_insn (buf, insn);
4a6a3df4
AO
18287
18288 /* j(al)r $at. */
18289 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18290 insn = 0x0000f809;
4a6a3df4 18291 else
66b3e8da
MR
18292 insn = 0x00000008;
18293 insn |= at << OP_SH_RS;
4a6a3df4 18294
4d68580a 18295 buf = write_insn (buf, insn);
4a6a3df4
AO
18296 }
18297 }
18298
4a6a3df4 18299 fragp->fr_fix += fragp->fr_var;
4d68580a 18300 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18301 return;
18302 }
18303
df58fc94
RS
18304 /* Relax microMIPS branches. */
18305 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18306 {
4d68580a 18307 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18308 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18309 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18310 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18311 bfd_boolean short_ds;
df58fc94
RS
18312 unsigned long insn;
18313 expressionS exp;
18314 fixS *fixp;
18315
18316 exp.X_op = O_symbol;
18317 exp.X_add_symbol = fragp->fr_symbol;
18318 exp.X_add_number = fragp->fr_offset;
18319
18320 fragp->fr_fix += fragp->fr_var;
18321
18322 /* Handle 16-bit branches that fit or are forced to fit. */
18323 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18324 {
18325 /* We generate a fixup instead of applying it right now,
18326 because if there is linker relaxation, we're going to
18327 need the relocations. */
18328 if (type == 'D')
4d68580a 18329 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18330 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18331 else if (type == 'E')
4d68580a 18332 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18333 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18334 else
18335 abort ();
18336
18337 fixp->fx_file = fragp->fr_file;
18338 fixp->fx_line = fragp->fr_line;
18339
18340 /* These relocations can have an addend that won't fit in
18341 2 octets. */
18342 fixp->fx_no_overflow = 1;
18343
18344 return;
18345 }
18346
2309ddf2 18347 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18348 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18349 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18350 {
18351 /* We generate a fixup instead of applying it right now,
18352 because if there is linker relaxation, we're going to
18353 need the relocations. */
4d68580a
RS
18354 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18355 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18356 fixp->fx_file = fragp->fr_file;
18357 fixp->fx_line = fragp->fr_line;
18358
18359 if (type == 0)
18360 return;
18361 }
18362
18363 /* Relax 16-bit branches to 32-bit branches. */
18364 if (type != 0)
18365 {
4d68580a 18366 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18367
18368 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18369 insn = 0x94000000; /* beq */
18370 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18371 {
18372 unsigned long regno;
18373
18374 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18375 regno = micromips_to_32_reg_d_map [regno];
18376 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18377 insn |= regno << MICROMIPSOP_SH_RS;
18378 }
18379 else
18380 abort ();
18381
18382 /* Nothing else to do, just write it out. */
18383 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18384 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18385 {
4d68580a
RS
18386 buf = write_compressed_insn (buf, insn, 4);
18387 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18388 return;
18389 }
18390 }
18391 else
4d68580a 18392 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18393
18394 /* Relax 32-bit branches to a sequence of instructions. */
18395 as_warn_where (fragp->fr_file, fragp->fr_line,
18396 _("Relaxed out-of-range branch into a jump"));
18397
2309ddf2
MR
18398 /* Set the short-delay-slot bit. */
18399 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
18400
18401 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18402 {
18403 symbolS *l;
18404
18405 /* Reverse the branch. */
18406 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18407 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18408 insn ^= 0x20000000;
18409 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18410 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18411 || (insn & 0xffe00000) == 0x40800000 /* blez */
18412 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18413 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18414 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18415 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18416 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18417 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18418 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18419 insn ^= 0x00400000;
18420 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18421 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18422 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18423 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18424 insn ^= 0x00200000;
18425 else
18426 abort ();
18427
18428 if (al)
18429 {
18430 /* Clear the and-link and short-delay-slot bits. */
18431 gas_assert ((insn & 0xfda00000) == 0x40200000);
18432
18433 /* bltzal 0x40200000 bgezal 0x40600000 */
18434 /* bltzals 0x42200000 bgezals 0x42600000 */
18435 insn &= ~0x02200000;
18436 }
18437
18438 /* Make a label at the end for use with the branch. */
18439 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18440 micromips_label_inc ();
f3ded42a 18441 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18442
18443 /* Refer to it. */
4d68580a
RS
18444 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18445 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18446 fixp->fx_file = fragp->fr_file;
18447 fixp->fx_line = fragp->fr_line;
18448
18449 /* Branch over the jump. */
4d68580a 18450 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18451 if (!compact)
4d68580a
RS
18452 /* nop */
18453 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18454 }
18455
18456 if (mips_pic == NO_PIC)
18457 {
2309ddf2
MR
18458 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
18459
df58fc94
RS
18460 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18461 insn = al ? jal : 0xd4000000;
18462
4d68580a
RS
18463 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18464 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18465 fixp->fx_file = fragp->fr_file;
18466 fixp->fx_line = fragp->fr_line;
18467
4d68580a 18468 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18469 if (compact)
4d68580a
RS
18470 /* nop */
18471 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
18472 }
18473 else
18474 {
18475 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
18476 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
18477 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
18478
18479 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18480 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18481 insn |= at << MICROMIPSOP_SH_RT;
18482
18483 if (exp.X_add_number)
18484 {
18485 exp.X_add_symbol = make_expr_symbol (&exp);
18486 exp.X_add_number = 0;
18487 }
18488
4d68580a
RS
18489 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18490 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18491 fixp->fx_file = fragp->fr_file;
18492 fixp->fx_line = fragp->fr_line;
18493
4d68580a 18494 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18495
18496 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18497 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18498 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18499
4d68580a
RS
18500 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18501 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18502 fixp->fx_file = fragp->fr_file;
18503 fixp->fx_line = fragp->fr_line;
18504
4d68580a 18505 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18506
18507 /* jr/jrc/jalr/jalrs $at */
18508 insn = al ? jalr : jr;
18509 insn |= at << MICROMIPSOP_SH_MJ;
18510
4d68580a 18511 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
18512 }
18513
4d68580a 18514 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18515 return;
18516 }
18517
252b5132
RH
18518 if (RELAX_MIPS16_P (fragp->fr_subtype))
18519 {
18520 int type;
3994f87e 18521 const struct mips16_immed_operand *op;
252b5132 18522 offsetT val;
5c04167a
RS
18523 char *buf;
18524 unsigned int user_length, length;
252b5132 18525 unsigned long insn;
5c04167a 18526 bfd_boolean ext;
252b5132
RH
18527
18528 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
18529 op = mips16_immed_operands;
18530 while (op->type != type)
18531 ++op;
18532
5c04167a 18533 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
5f5f22c0 18534 val = resolve_symbol_value (fragp->fr_symbol);
252b5132
RH
18535 if (op->pcrel)
18536 {
18537 addressT addr;
18538
18539 addr = fragp->fr_address + fragp->fr_fix;
18540
18541 /* The rules for the base address of a PC relative reloc are
18542 complicated; see mips16_extended_frag. */
18543 if (type == 'p' || type == 'q')
18544 {
18545 addr += 2;
18546 if (ext)
18547 addr += 2;
18548 /* Ignore the low bit in the target, since it will be
18549 set for a text label. */
18550 if ((val & 1) != 0)
18551 --val;
18552 }
18553 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
18554 addr -= 4;
18555 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18556 addr -= 2;
18557
18558 addr &= ~ (addressT) ((1 << op->shift) - 1);
18559 val -= addr;
18560
18561 /* Make sure the section winds up with the alignment we have
18562 assumed. */
18563 if (op->shift > 0)
18564 record_alignment (asec, op->shift);
18565 }
18566
18567 if (ext
18568 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18569 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18570 as_warn_where (fragp->fr_file, fragp->fr_line,
18571 _("extended instruction in delay slot"));
18572
5c04167a 18573 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18574
4d68580a 18575 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18576 if (ext)
18577 insn |= MIPS16_EXTEND;
252b5132 18578
5c04167a
RS
18579 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18580 user_length = 4;
18581 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18582 user_length = 2;
18583 else
18584 user_length = 0;
18585
43c0598f 18586 mips16_immed (fragp->fr_file, fragp->fr_line, type,
c150d1d2 18587 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18588
5c04167a
RS
18589 length = (ext ? 4 : 2);
18590 gas_assert (mips16_opcode_length (insn) == length);
18591 write_compressed_insn (buf, insn, length);
18592 fragp->fr_fix += length;
252b5132
RH
18593 }
18594 else
18595 {
df58fc94
RS
18596 relax_substateT subtype = fragp->fr_subtype;
18597 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18598 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18599 int first, second;
18600 fixS *fixp;
252b5132 18601
df58fc94
RS
18602 first = RELAX_FIRST (subtype);
18603 second = RELAX_SECOND (subtype);
4d7206a2 18604 fixp = (fixS *) fragp->fr_opcode;
252b5132 18605
df58fc94
RS
18606 /* If the delay slot chosen does not match the size of the instruction,
18607 then emit a warning. */
18608 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18609 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18610 {
18611 relax_substateT s;
18612 const char *msg;
18613
18614 s = subtype & (RELAX_DELAY_SLOT_16BIT
18615 | RELAX_DELAY_SLOT_SIZE_FIRST
18616 | RELAX_DELAY_SLOT_SIZE_SECOND);
18617 msg = macro_warning (s);
18618 if (msg != NULL)
db9b2be4 18619 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18620 subtype &= ~s;
18621 }
18622
584892a6 18623 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18624 if (use_second == second_longer)
584892a6 18625 {
df58fc94
RS
18626 relax_substateT s;
18627 const char *msg;
18628
18629 s = (subtype
18630 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18631 msg = macro_warning (s);
18632 if (msg != NULL)
db9b2be4 18633 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18634 subtype &= ~s;
584892a6
RS
18635 }
18636
4d7206a2
RS
18637 /* Go through all the fixups for the first sequence. Disable them
18638 (by marking them as done) if we're going to use the second
18639 sequence instead. */
18640 while (fixp
18641 && fixp->fx_frag == fragp
18642 && fixp->fx_where < fragp->fr_fix - second)
18643 {
df58fc94 18644 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18645 fixp->fx_done = 1;
18646 fixp = fixp->fx_next;
18647 }
252b5132 18648
4d7206a2
RS
18649 /* Go through the fixups for the second sequence. Disable them if
18650 we're going to use the first sequence, otherwise adjust their
18651 addresses to account for the relaxation. */
18652 while (fixp && fixp->fx_frag == fragp)
18653 {
df58fc94 18654 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18655 fixp->fx_where -= first;
18656 else
18657 fixp->fx_done = 1;
18658 fixp = fixp->fx_next;
18659 }
18660
18661 /* Now modify the frag contents. */
df58fc94 18662 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18663 {
18664 char *start;
18665
18666 start = fragp->fr_literal + fragp->fr_fix - first - second;
18667 memmove (start, start + first, second);
18668 fragp->fr_fix -= first;
18669 }
18670 else
18671 fragp->fr_fix -= second;
252b5132
RH
18672 }
18673}
18674
252b5132
RH
18675/* This function is called after the relocs have been generated.
18676 We've been storing mips16 text labels as odd. Here we convert them
18677 back to even for the convenience of the debugger. */
18678
18679void
17a2f251 18680mips_frob_file_after_relocs (void)
252b5132
RH
18681{
18682 asymbol **syms;
18683 unsigned int count, i;
18684
252b5132
RH
18685 syms = bfd_get_outsymbols (stdoutput);
18686 count = bfd_get_symcount (stdoutput);
18687 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18688 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18689 && ((*syms)->value & 1) != 0)
18690 {
18691 (*syms)->value &= ~1;
18692 /* If the symbol has an odd size, it was probably computed
18693 incorrectly, so adjust that as well. */
18694 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18695 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18696 }
252b5132
RH
18697}
18698
a1facbec
MR
18699/* This function is called whenever a label is defined, including fake
18700 labels instantiated off the dot special symbol. It is used when
18701 handling branch delays; if a branch has a label, we assume we cannot
18702 move it. This also bumps the value of the symbol by 1 in compressed
18703 code. */
252b5132 18704
e1b47bd5 18705static void
a1facbec 18706mips_record_label (symbolS *sym)
252b5132 18707{
a8dbcb85 18708 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18709 struct insn_label_list *l;
18710
18711 if (free_insn_labels == NULL)
18712 l = (struct insn_label_list *) xmalloc (sizeof *l);
18713 else
18714 {
18715 l = free_insn_labels;
18716 free_insn_labels = l->next;
18717 }
18718
18719 l->label = sym;
a8dbcb85
TS
18720 l->next = si->label_list;
18721 si->label_list = l;
a1facbec 18722}
07a53e5c 18723
a1facbec
MR
18724/* This function is called as tc_frob_label() whenever a label is defined
18725 and adds a DWARF-2 record we only want for true labels. */
18726
18727void
18728mips_define_label (symbolS *sym)
18729{
18730 mips_record_label (sym);
07a53e5c 18731 dwarf2_emit_label (sym);
252b5132 18732}
e1b47bd5
RS
18733
18734/* This function is called by tc_new_dot_label whenever a new dot symbol
18735 is defined. */
18736
18737void
18738mips_add_dot_label (symbolS *sym)
18739{
18740 mips_record_label (sym);
18741 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18742 mips_compressed_mark_label (sym);
18743}
252b5132 18744\f
252b5132
RH
18745/* Some special processing for a MIPS ELF file. */
18746
18747void
17a2f251 18748mips_elf_final_processing (void)
252b5132
RH
18749{
18750 /* Write out the register information. */
316f5878 18751 if (mips_abi != N64_ABI)
252b5132
RH
18752 {
18753 Elf32_RegInfo s;
18754
18755 s.ri_gprmask = mips_gprmask;
18756 s.ri_cprmask[0] = mips_cprmask[0];
18757 s.ri_cprmask[1] = mips_cprmask[1];
18758 s.ri_cprmask[2] = mips_cprmask[2];
18759 s.ri_cprmask[3] = mips_cprmask[3];
18760 /* The gp_value field is set by the MIPS ELF backend. */
18761
18762 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18763 ((Elf32_External_RegInfo *)
18764 mips_regmask_frag));
18765 }
18766 else
18767 {
18768 Elf64_Internal_RegInfo s;
18769
18770 s.ri_gprmask = mips_gprmask;
18771 s.ri_pad = 0;
18772 s.ri_cprmask[0] = mips_cprmask[0];
18773 s.ri_cprmask[1] = mips_cprmask[1];
18774 s.ri_cprmask[2] = mips_cprmask[2];
18775 s.ri_cprmask[3] = mips_cprmask[3];
18776 /* The gp_value field is set by the MIPS ELF backend. */
18777
18778 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18779 ((Elf64_External_RegInfo *)
18780 mips_regmask_frag));
18781 }
18782
18783 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18784 sort of BFD interface for this. */
18785 if (mips_any_noreorder)
18786 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18787 if (mips_pic != NO_PIC)
143d77c5 18788 {
8b828383 18789 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18790 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18791 }
18792 if (mips_abicalls)
18793 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18794
b015e599
AP
18795 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18796 defined at present; this might need to change in future. */
a4672219
TS
18797 if (file_ase_mips16)
18798 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18799 if (file_ase_micromips)
18800 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
846ef2d0 18801 if (file_ase & ASE_MDMX)
deec1734 18802 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18803
bdaaa2e1 18804 /* Set the MIPS ELF ABI flags. */
316f5878 18805 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18806 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18807 else if (mips_abi == O64_ABI)
252b5132 18808 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18809 else if (mips_abi == EABI_ABI)
252b5132 18810 {
316f5878 18811 if (!file_mips_gp32)
252b5132
RH
18812 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18813 else
18814 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18815 }
316f5878 18816 else if (mips_abi == N32_ABI)
be00bddd
TS
18817 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18818
c9914766 18819 /* Nothing to do for N64_ABI. */
252b5132
RH
18820
18821 if (mips_32bitmode)
18822 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08
TS
18823
18824#if 0 /* XXX FIXME */
18825 /* 32 bit code with 64 bit FP registers. */
18826 if (!file_mips_fp32 && ABI_NEEDS_32BIT_REGS (mips_abi))
18827 elf_elfheader (stdoutput)->e_flags |= ???;
18828#endif
252b5132 18829}
252b5132 18830\f
beae10d5 18831typedef struct proc {
9b2f1d35
EC
18832 symbolS *func_sym;
18833 symbolS *func_end_sym;
beae10d5
KH
18834 unsigned long reg_mask;
18835 unsigned long reg_offset;
18836 unsigned long fpreg_mask;
18837 unsigned long fpreg_offset;
18838 unsigned long frame_offset;
18839 unsigned long frame_reg;
18840 unsigned long pc_reg;
18841} procS;
252b5132
RH
18842
18843static procS cur_proc;
18844static procS *cur_proc_ptr;
18845static int numprocs;
18846
df58fc94
RS
18847/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18848 as "2", and a normal nop as "0". */
18849
18850#define NOP_OPCODE_MIPS 0
18851#define NOP_OPCODE_MIPS16 1
18852#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18853
18854char
18855mips_nop_opcode (void)
18856{
df58fc94
RS
18857 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18858 return NOP_OPCODE_MICROMIPS;
18859 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18860 return NOP_OPCODE_MIPS16;
18861 else
18862 return NOP_OPCODE_MIPS;
742a56fe
RS
18863}
18864
df58fc94
RS
18865/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18866 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18867
0a9ef439 18868void
17a2f251 18869mips_handle_align (fragS *fragp)
a19d8eb0 18870{
df58fc94 18871 char nop_opcode;
742a56fe 18872 char *p;
c67a084a
NC
18873 int bytes, size, excess;
18874 valueT opcode;
742a56fe 18875
0a9ef439
RH
18876 if (fragp->fr_type != rs_align_code)
18877 return;
18878
742a56fe 18879 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18880 nop_opcode = *p;
18881 switch (nop_opcode)
a19d8eb0 18882 {
df58fc94
RS
18883 case NOP_OPCODE_MICROMIPS:
18884 opcode = micromips_nop32_insn.insn_opcode;
18885 size = 4;
18886 break;
18887 case NOP_OPCODE_MIPS16:
c67a084a
NC
18888 opcode = mips16_nop_insn.insn_opcode;
18889 size = 2;
df58fc94
RS
18890 break;
18891 case NOP_OPCODE_MIPS:
18892 default:
c67a084a
NC
18893 opcode = nop_insn.insn_opcode;
18894 size = 4;
df58fc94 18895 break;
c67a084a 18896 }
a19d8eb0 18897
c67a084a
NC
18898 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18899 excess = bytes % size;
df58fc94
RS
18900
18901 /* Handle the leading part if we're not inserting a whole number of
18902 instructions, and make it the end of the fixed part of the frag.
18903 Try to fit in a short microMIPS NOP if applicable and possible,
18904 and use zeroes otherwise. */
18905 gas_assert (excess < 4);
18906 fragp->fr_fix += excess;
18907 switch (excess)
c67a084a 18908 {
df58fc94
RS
18909 case 3:
18910 *p++ = '\0';
18911 /* Fall through. */
18912 case 2:
833794fc 18913 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 18914 {
4d68580a 18915 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18916 break;
18917 }
18918 *p++ = '\0';
18919 /* Fall through. */
18920 case 1:
18921 *p++ = '\0';
18922 /* Fall through. */
18923 case 0:
18924 break;
a19d8eb0 18925 }
c67a084a
NC
18926
18927 md_number_to_chars (p, opcode, size);
18928 fragp->fr_var = size;
a19d8eb0
CP
18929}
18930
252b5132 18931static void
17a2f251 18932md_obj_begin (void)
252b5132
RH
18933{
18934}
18935
18936static void
17a2f251 18937md_obj_end (void)
252b5132 18938{
54f4ddb3 18939 /* Check for premature end, nesting errors, etc. */
252b5132 18940 if (cur_proc_ptr)
9a41af64 18941 as_warn (_("missing .end at end of assembly"));
252b5132
RH
18942}
18943
18944static long
17a2f251 18945get_number (void)
252b5132
RH
18946{
18947 int negative = 0;
18948 long val = 0;
18949
18950 if (*input_line_pointer == '-')
18951 {
18952 ++input_line_pointer;
18953 negative = 1;
18954 }
3882b010 18955 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18956 as_bad (_("expected simple number"));
252b5132
RH
18957 if (input_line_pointer[0] == '0')
18958 {
18959 if (input_line_pointer[1] == 'x')
18960 {
18961 input_line_pointer += 2;
3882b010 18962 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18963 {
18964 val <<= 4;
18965 val |= hex_value (*input_line_pointer++);
18966 }
18967 return negative ? -val : val;
18968 }
18969 else
18970 {
18971 ++input_line_pointer;
3882b010 18972 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18973 {
18974 val <<= 3;
18975 val |= *input_line_pointer++ - '0';
18976 }
18977 return negative ? -val : val;
18978 }
18979 }
3882b010 18980 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18981 {
18982 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18983 *input_line_pointer, *input_line_pointer);
956cd1d6 18984 as_warn (_("invalid number"));
252b5132
RH
18985 return -1;
18986 }
3882b010 18987 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18988 {
18989 val *= 10;
18990 val += *input_line_pointer++ - '0';
18991 }
18992 return negative ? -val : val;
18993}
18994
18995/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18996 is an initial number which is the ECOFF file index. In the non-ECOFF
18997 case .file implies DWARF-2. */
18998
18999static void
17a2f251 19000s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19001{
ecb4347a
DJ
19002 static int first_file_directive = 0;
19003
c5dd6aab
DJ
19004 if (ECOFF_DEBUGGING)
19005 {
19006 get_number ();
19007 s_app_file (0);
19008 }
19009 else
ecb4347a
DJ
19010 {
19011 char *filename;
19012
19013 filename = dwarf2_directive_file (0);
19014
19015 /* Versions of GCC up to 3.1 start files with a ".file"
19016 directive even for stabs output. Make sure that this
19017 ".file" is handled. Note that you need a version of GCC
19018 after 3.1 in order to support DWARF-2 on MIPS. */
19019 if (filename != NULL && ! first_file_directive)
19020 {
19021 (void) new_logical_line (filename, -1);
c04f5787 19022 s_app_file_string (filename, 0);
ecb4347a
DJ
19023 }
19024 first_file_directive = 1;
19025 }
c5dd6aab
DJ
19026}
19027
19028/* The .loc directive, implying DWARF-2. */
252b5132
RH
19029
19030static void
17a2f251 19031s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19032{
c5dd6aab
DJ
19033 if (!ECOFF_DEBUGGING)
19034 dwarf2_directive_loc (0);
252b5132
RH
19035}
19036
252b5132
RH
19037/* The .end directive. */
19038
19039static void
17a2f251 19040s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19041{
19042 symbolS *p;
252b5132 19043
7a621144
DJ
19044 /* Following functions need their own .frame and .cprestore directives. */
19045 mips_frame_reg_valid = 0;
19046 mips_cprestore_valid = 0;
19047
252b5132
RH
19048 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19049 {
19050 p = get_symbol ();
19051 demand_empty_rest_of_line ();
19052 }
19053 else
19054 p = NULL;
19055
14949570 19056 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19057 as_warn (_(".end not in text section"));
19058
19059 if (!cur_proc_ptr)
19060 {
19061 as_warn (_(".end directive without a preceding .ent directive."));
19062 demand_empty_rest_of_line ();
19063 return;
19064 }
19065
19066 if (p != NULL)
19067 {
9c2799c2 19068 gas_assert (S_GET_NAME (p));
9b2f1d35 19069 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
252b5132 19070 as_warn (_(".end symbol does not match .ent symbol."));
ecb4347a
DJ
19071
19072 if (debug_type == DEBUG_STABS)
19073 stabs_generate_asm_endfunc (S_GET_NAME (p),
19074 S_GET_NAME (p));
252b5132
RH
19075 }
19076 else
19077 as_warn (_(".end directive missing or unknown symbol"));
19078
9b2f1d35
EC
19079 /* Create an expression to calculate the size of the function. */
19080 if (p && cur_proc_ptr)
19081 {
19082 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
19083 expressionS *exp = xmalloc (sizeof (expressionS));
19084
19085 obj->size = exp;
19086 exp->X_op = O_subtract;
19087 exp->X_add_symbol = symbol_temp_new_now ();
19088 exp->X_op_symbol = p;
19089 exp->X_add_number = 0;
19090
19091 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19092 }
19093
ecb4347a 19094 /* Generate a .pdr section. */
f3ded42a 19095 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19096 {
19097 segT saved_seg = now_seg;
19098 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19099 expressionS exp;
19100 char *fragp;
252b5132 19101
252b5132 19102#ifdef md_flush_pending_output
ecb4347a 19103 md_flush_pending_output ();
252b5132
RH
19104#endif
19105
9c2799c2 19106 gas_assert (pdr_seg);
ecb4347a 19107 subseg_set (pdr_seg, 0);
252b5132 19108
ecb4347a
DJ
19109 /* Write the symbol. */
19110 exp.X_op = O_symbol;
19111 exp.X_add_symbol = p;
19112 exp.X_add_number = 0;
19113 emit_expr (&exp, 4);
252b5132 19114
ecb4347a 19115 fragp = frag_more (7 * 4);
252b5132 19116
17a2f251
TS
19117 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19118 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19119 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19120 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19121 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19122 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19123 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19124
ecb4347a
DJ
19125 subseg_set (saved_seg, saved_subseg);
19126 }
252b5132
RH
19127
19128 cur_proc_ptr = NULL;
19129}
19130
19131/* The .aent and .ent directives. */
19132
19133static void
17a2f251 19134s_mips_ent (int aent)
252b5132 19135{
252b5132 19136 symbolS *symbolP;
252b5132
RH
19137
19138 symbolP = get_symbol ();
19139 if (*input_line_pointer == ',')
f9419b05 19140 ++input_line_pointer;
252b5132 19141 SKIP_WHITESPACE ();
3882b010 19142 if (ISDIGIT (*input_line_pointer)
d9a62219 19143 || *input_line_pointer == '-')
874e8986 19144 get_number ();
252b5132 19145
14949570 19146 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19147 as_warn (_(".ent or .aent not in text section."));
19148
19149 if (!aent && cur_proc_ptr)
9a41af64 19150 as_warn (_("missing .end"));
252b5132
RH
19151
19152 if (!aent)
19153 {
7a621144
DJ
19154 /* This function needs its own .frame and .cprestore directives. */
19155 mips_frame_reg_valid = 0;
19156 mips_cprestore_valid = 0;
19157
252b5132
RH
19158 cur_proc_ptr = &cur_proc;
19159 memset (cur_proc_ptr, '\0', sizeof (procS));
19160
9b2f1d35 19161 cur_proc_ptr->func_sym = symbolP;
252b5132 19162
f9419b05 19163 ++numprocs;
ecb4347a
DJ
19164
19165 if (debug_type == DEBUG_STABS)
19166 stabs_generate_asm_func (S_GET_NAME (symbolP),
19167 S_GET_NAME (symbolP));
252b5132
RH
19168 }
19169
7c0fc524
MR
19170 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19171
252b5132
RH
19172 demand_empty_rest_of_line ();
19173}
19174
19175/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19176 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19177 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19178 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19179 symbol table (in the mdebug section). */
19180
19181static void
17a2f251 19182s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19183{
f3ded42a
RS
19184 if (ECOFF_DEBUGGING)
19185 s_ignore (ignore);
19186 else
ecb4347a
DJ
19187 {
19188 long val;
252b5132 19189
ecb4347a
DJ
19190 if (cur_proc_ptr == (procS *) NULL)
19191 {
19192 as_warn (_(".frame outside of .ent"));
19193 demand_empty_rest_of_line ();
19194 return;
19195 }
252b5132 19196
ecb4347a
DJ
19197 cur_proc_ptr->frame_reg = tc_get_register (1);
19198
19199 SKIP_WHITESPACE ();
19200 if (*input_line_pointer++ != ','
19201 || get_absolute_expression_and_terminator (&val) != ',')
19202 {
19203 as_warn (_("Bad .frame directive"));
19204 --input_line_pointer;
19205 demand_empty_rest_of_line ();
19206 return;
19207 }
252b5132 19208
ecb4347a
DJ
19209 cur_proc_ptr->frame_offset = val;
19210 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19211
252b5132 19212 demand_empty_rest_of_line ();
252b5132 19213 }
252b5132
RH
19214}
19215
bdaaa2e1
KH
19216/* The .fmask and .mask directives. If the mdebug section is present
19217 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19218 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19219 information correctly. We can't use the ecoff routines because they
252b5132
RH
19220 make reference to the ecoff symbol table (in the mdebug section). */
19221
19222static void
17a2f251 19223s_mips_mask (int reg_type)
252b5132 19224{
f3ded42a
RS
19225 if (ECOFF_DEBUGGING)
19226 s_ignore (reg_type);
19227 else
252b5132 19228 {
ecb4347a 19229 long mask, off;
252b5132 19230
ecb4347a
DJ
19231 if (cur_proc_ptr == (procS *) NULL)
19232 {
19233 as_warn (_(".mask/.fmask outside of .ent"));
19234 demand_empty_rest_of_line ();
19235 return;
19236 }
252b5132 19237
ecb4347a
DJ
19238 if (get_absolute_expression_and_terminator (&mask) != ',')
19239 {
19240 as_warn (_("Bad .mask/.fmask directive"));
19241 --input_line_pointer;
19242 demand_empty_rest_of_line ();
19243 return;
19244 }
252b5132 19245
ecb4347a
DJ
19246 off = get_absolute_expression ();
19247
19248 if (reg_type == 'F')
19249 {
19250 cur_proc_ptr->fpreg_mask = mask;
19251 cur_proc_ptr->fpreg_offset = off;
19252 }
19253 else
19254 {
19255 cur_proc_ptr->reg_mask = mask;
19256 cur_proc_ptr->reg_offset = off;
19257 }
19258
19259 demand_empty_rest_of_line ();
252b5132 19260 }
252b5132
RH
19261}
19262
316f5878
RS
19263/* A table describing all the processors gas knows about. Names are
19264 matched in the order listed.
e7af610e 19265
316f5878
RS
19266 To ease comparison, please keep this table in the same order as
19267 gcc's mips_cpu_info_table[]. */
e972090a
NC
19268static const struct mips_cpu_info mips_cpu_info_table[] =
19269{
316f5878 19270 /* Entries for generic ISAs */
d16afab6
RS
19271 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19272 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19273 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19274 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19275 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19276 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19277 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19278 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19279 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
316f5878
RS
19280
19281 /* MIPS I */
d16afab6
RS
19282 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19283 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19284 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19285
19286 /* MIPS II */
d16afab6 19287 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19288
19289 /* MIPS III */
d16afab6
RS
19290 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19291 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19292 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19293 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19294 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19295 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19296 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19297 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19298 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19299 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19300 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19301 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19302 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19303 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19304 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19305 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19306
19307 /* MIPS IV */
d16afab6
RS
19308 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19309 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19310 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19311 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19312 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19313 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19314 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19315 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19316 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19317 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19318 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19319 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19320 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19321 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19322 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19323
19324 /* MIPS 32 */
d16afab6
RS
19325 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19326 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19327 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19328 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19329
19330 /* MIPS 32 Release 2 */
d16afab6
RS
19331 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19332 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19333 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19334 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19335 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19336 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19337 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19338 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19339 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19340 ISA_MIPS32R2, CPU_MIPS32R2 },
19341 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19342 ISA_MIPS32R2, CPU_MIPS32R2 },
19343 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19344 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19345 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19346 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19347 /* Deprecated forms of the above. */
d16afab6
RS
19348 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19349 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19350 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19351 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19352 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19353 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19354 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19355 /* Deprecated forms of the above. */
d16afab6
RS
19356 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19357 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19358 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19359 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19360 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19361 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19362 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19363 /* Deprecated forms of the above. */
d16afab6
RS
19364 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19365 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19366 /* 34Kn is a 34kc without DSP. */
d16afab6 19367 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19368 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19369 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19370 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19371 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19372 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19373 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19374 /* Deprecated forms of the above. */
d16afab6
RS
19375 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19376 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19377 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19378 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19379 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19380 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19381 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
32b26a03 19382
316f5878 19383 /* MIPS 64 */
d16afab6
RS
19384 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19385 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19386 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19387 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19388
c7a23324 19389 /* Broadcom SB-1 CPU core */
d16afab6 19390 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19391 /* Broadcom SB-1A CPU core */
d16afab6 19392 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
d051516a 19393
d16afab6 19394 { "loongson3a", 0, 0, ISA_MIPS64, CPU_LOONGSON_3A },
e7af610e 19395
ed163775
MR
19396 /* MIPS 64 Release 2 */
19397
967344c6 19398 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19399 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19400 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19401 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
967344c6 19402
52b6b6b9 19403 /* RMI Xlr */
d16afab6 19404 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19405
55a36193
MK
19406 /* Broadcom XLP.
19407 XLP is mostly like XLR, with the prominent exception that it is
19408 MIPS64R2 rather than MIPS64. */
d16afab6 19409 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19410
316f5878 19411 /* End marker */
d16afab6 19412 { NULL, 0, 0, 0, 0 }
316f5878 19413};
e7af610e 19414
84ea6cf2 19415
316f5878
RS
19416/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19417 with a final "000" replaced by "k". Ignore case.
e7af610e 19418
316f5878 19419 Note: this function is shared between GCC and GAS. */
c6c98b38 19420
b34976b6 19421static bfd_boolean
17a2f251 19422mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19423{
19424 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19425 given++, canonical++;
19426
19427 return ((*given == 0 && *canonical == 0)
19428 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19429}
19430
19431
19432/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19433 CPU name. We've traditionally allowed a lot of variation here.
19434
19435 Note: this function is shared between GCC and GAS. */
19436
b34976b6 19437static bfd_boolean
17a2f251 19438mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19439{
19440 /* First see if the name matches exactly, or with a final "000"
19441 turned into "k". */
19442 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19443 return TRUE;
316f5878
RS
19444
19445 /* If not, try comparing based on numerical designation alone.
19446 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19447 if (TOLOWER (*given) == 'r')
19448 given++;
19449 if (!ISDIGIT (*given))
b34976b6 19450 return FALSE;
316f5878
RS
19451
19452 /* Skip over some well-known prefixes in the canonical name,
19453 hoping to find a number there too. */
19454 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19455 canonical += 2;
19456 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19457 canonical += 2;
19458 else if (TOLOWER (canonical[0]) == 'r')
19459 canonical += 1;
19460
19461 return mips_strict_matching_cpu_name_p (canonical, given);
19462}
19463
19464
19465/* Parse an option that takes the name of a processor as its argument.
19466 OPTION is the name of the option and CPU_STRING is the argument.
19467 Return the corresponding processor enumeration if the CPU_STRING is
19468 recognized, otherwise report an error and return null.
19469
19470 A similar function exists in GCC. */
e7af610e
NC
19471
19472static const struct mips_cpu_info *
17a2f251 19473mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19474{
316f5878 19475 const struct mips_cpu_info *p;
e7af610e 19476
316f5878
RS
19477 /* 'from-abi' selects the most compatible architecture for the given
19478 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19479 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19480 version. Look first at the -mgp options, if given, otherwise base
19481 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19482
316f5878
RS
19483 Treat NO_ABI like the EABIs. One reason to do this is that the
19484 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19485 architecture. This code picks MIPS I for 'mips' and MIPS III for
19486 'mips64', just as we did in the days before 'from-abi'. */
19487 if (strcasecmp (cpu_string, "from-abi") == 0)
19488 {
19489 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19490 return mips_cpu_info_from_isa (ISA_MIPS1);
19491
19492 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19493 return mips_cpu_info_from_isa (ISA_MIPS3);
19494
19495 if (file_mips_gp32 >= 0)
19496 return mips_cpu_info_from_isa (file_mips_gp32 ? ISA_MIPS1 : ISA_MIPS3);
19497
19498 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19499 ? ISA_MIPS3
19500 : ISA_MIPS1);
19501 }
19502
19503 /* 'default' has traditionally been a no-op. Probably not very useful. */
19504 if (strcasecmp (cpu_string, "default") == 0)
19505 return 0;
19506
19507 for (p = mips_cpu_info_table; p->name != 0; p++)
19508 if (mips_matching_cpu_name_p (p->name, cpu_string))
19509 return p;
19510
20203fb9 19511 as_bad (_("Bad value (%s) for %s"), cpu_string, option);
316f5878 19512 return 0;
e7af610e
NC
19513}
19514
316f5878
RS
19515/* Return the canonical processor information for ISA (a member of the
19516 ISA_MIPS* enumeration). */
19517
e7af610e 19518static const struct mips_cpu_info *
17a2f251 19519mips_cpu_info_from_isa (int isa)
e7af610e
NC
19520{
19521 int i;
19522
19523 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19524 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19525 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19526 return (&mips_cpu_info_table[i]);
19527
e972090a 19528 return NULL;
e7af610e 19529}
fef14a42
TS
19530
19531static const struct mips_cpu_info *
17a2f251 19532mips_cpu_info_from_arch (int arch)
fef14a42
TS
19533{
19534 int i;
19535
19536 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19537 if (arch == mips_cpu_info_table[i].cpu)
19538 return (&mips_cpu_info_table[i]);
19539
19540 return NULL;
19541}
316f5878
RS
19542\f
19543static void
17a2f251 19544show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19545{
19546 if (*first_p)
19547 {
19548 fprintf (stream, "%24s", "");
19549 *col_p = 24;
19550 }
19551 else
19552 {
19553 fprintf (stream, ", ");
19554 *col_p += 2;
19555 }
e7af610e 19556
316f5878
RS
19557 if (*col_p + strlen (string) > 72)
19558 {
19559 fprintf (stream, "\n%24s", "");
19560 *col_p = 24;
19561 }
19562
19563 fprintf (stream, "%s", string);
19564 *col_p += strlen (string);
19565
19566 *first_p = 0;
19567}
19568
19569void
17a2f251 19570md_show_usage (FILE *stream)
e7af610e 19571{
316f5878
RS
19572 int column, first;
19573 size_t i;
19574
19575 fprintf (stream, _("\
19576MIPS options:\n\
316f5878
RS
19577-EB generate big endian output\n\
19578-EL generate little endian output\n\
19579-g, -g2 do not remove unneeded NOPs or swap branches\n\
19580-G NUM allow referencing objects up to NUM bytes\n\
19581 implicitly with the gp register [default 8]\n"));
19582 fprintf (stream, _("\
19583-mips1 generate MIPS ISA I instructions\n\
19584-mips2 generate MIPS ISA II instructions\n\
19585-mips3 generate MIPS ISA III instructions\n\
19586-mips4 generate MIPS ISA IV instructions\n\
19587-mips5 generate MIPS ISA V instructions\n\
19588-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19589-mips32r2 generate MIPS32 release 2 ISA instructions\n\
316f5878 19590-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19591-mips64r2 generate MIPS64 release 2 ISA instructions\n\
316f5878
RS
19592-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19593
19594 first = 1;
e7af610e
NC
19595
19596 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19597 show (stream, mips_cpu_info_table[i].name, &column, &first);
19598 show (stream, "from-abi", &column, &first);
19599 fputc ('\n', stream);
e7af610e 19600
316f5878
RS
19601 fprintf (stream, _("\
19602-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19603-no-mCPU don't generate code specific to CPU.\n\
19604 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19605
19606 first = 1;
19607
19608 show (stream, "3900", &column, &first);
19609 show (stream, "4010", &column, &first);
19610 show (stream, "4100", &column, &first);
19611 show (stream, "4650", &column, &first);
19612 fputc ('\n', stream);
19613
19614 fprintf (stream, _("\
19615-mips16 generate mips16 instructions\n\
19616-no-mips16 do not generate mips16 instructions\n"));
19617 fprintf (stream, _("\
df58fc94
RS
19618-mmicromips generate microMIPS instructions\n\
19619-mno-micromips do not generate microMIPS instructions\n"));
19620 fprintf (stream, _("\
e16bfa71
TS
19621-msmartmips generate smartmips instructions\n\
19622-mno-smartmips do not generate smartmips instructions\n"));
19623 fprintf (stream, _("\
74cd071d
CF
19624-mdsp generate DSP instructions\n\
19625-mno-dsp do not generate DSP instructions\n"));
19626 fprintf (stream, _("\
8b082fb1
TS
19627-mdspr2 generate DSP R2 instructions\n\
19628-mno-dspr2 do not generate DSP R2 instructions\n"));
19629 fprintf (stream, _("\
ef2e4d86
CF
19630-mmt generate MT instructions\n\
19631-mno-mt do not generate MT instructions\n"));
19632 fprintf (stream, _("\
dec0624d
MR
19633-mmcu generate MCU instructions\n\
19634-mno-mcu do not generate MCU instructions\n"));
19635 fprintf (stream, _("\
b015e599
AP
19636-mvirt generate Virtualization instructions\n\
19637-mno-virt do not generate Virtualization instructions\n"));
19638 fprintf (stream, _("\
833794fc
MR
19639-minsn32 only generate 32-bit microMIPS instructions\n\
19640-mno-insn32 generate all microMIPS instructions\n"));
19641 fprintf (stream, _("\
c67a084a
NC
19642-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19643-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19644-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19645-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19646-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19647-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19648-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19649-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19650-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19651-O0 remove unneeded NOPs, do not swap branches\n\
19652-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19653--trap, --no-break trap exception on div by 0 and mult overflow\n\
19654--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19655 fprintf (stream, _("\
19656-mhard-float allow floating-point instructions\n\
19657-msoft-float do not allow floating-point instructions\n\
19658-msingle-float only allow 32-bit floating-point operations\n\
19659-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb
MR
19660--[no-]construct-floats [dis]allow floating point values to be constructed\n\
19661--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n"
037b32b9 19662 ));
316f5878
RS
19663 fprintf (stream, _("\
19664-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19665-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19666-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19667-non_shared do not generate code that can operate with DSOs\n\
316f5878 19668-xgot assume a 32 bit GOT\n\
dcd410fe 19669-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19670-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19671 position dependent (non shared) code\n\
316f5878
RS
19672-mabi=ABI create ABI conformant object file for:\n"));
19673
19674 first = 1;
19675
19676 show (stream, "32", &column, &first);
19677 show (stream, "o64", &column, &first);
19678 show (stream, "n32", &column, &first);
19679 show (stream, "64", &column, &first);
19680 show (stream, "eabi", &column, &first);
19681
19682 fputc ('\n', stream);
19683
19684 fprintf (stream, _("\
19685-32 create o32 ABI object file (default)\n\
19686-n32 create n32 ABI object file\n\
19687-64 create 64 ABI object file\n"));
e7af610e 19688}
14e777e0 19689
1575952e 19690#ifdef TE_IRIX
14e777e0 19691enum dwarf2_format
413a266c 19692mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19693{
369943fe 19694 if (HAVE_64BIT_SYMBOLS)
1575952e 19695 return dwarf2_format_64bit_irix;
14e777e0
KB
19696 else
19697 return dwarf2_format_32bit;
19698}
1575952e 19699#endif
73369e65
EC
19700
19701int
19702mips_dwarf2_addr_size (void)
19703{
6b6b3450 19704 if (HAVE_64BIT_OBJECTS)
73369e65 19705 return 8;
73369e65
EC
19706 else
19707 return 4;
19708}
5862107c
EC
19709
19710/* Standard calling conventions leave the CFA at SP on entry. */
19711void
19712mips_cfi_frame_initial_instructions (void)
19713{
19714 cfi_add_CFA_def_cfa_register (SP);
19715}
19716
707bfff6
TS
19717int
19718tc_mips_regname_to_dw2regnum (char *regname)
19719{
19720 unsigned int regnum = -1;
19721 unsigned int reg;
19722
19723 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19724 regnum = reg;
19725
19726 return regnum;
19727}
This page took 2.615838 seconds and 4 git commands to generate.