]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
Revert "MIPS: gas: alter 64 or 32 for mipsisa triples if march is implicit"
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
d87bef3a 2 Copyright (C) 1993-2023 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
5b7c81bd 84int mips_flag_pdr = false;
dcd410fe 85#else
5b7c81bd 86int mips_flag_pdr = true;
dcd410fe
RO
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
25663db4
MR
107#define FCSR 31
108
252b5132
RH
109#define ILLEGAL_REG (32)
110
741fe287
MR
111#define AT mips_opts.at
112
252b5132
RH
113extern int target_big_endian;
114
252b5132 115/* The name of the readonly data section. */
e8044f35 116#define RDATA_SECTION_NAME ".rodata"
252b5132 117
a4e06468
RS
118/* Ways in which an instruction can be "appended" to the output. */
119enum append_method {
120 /* Just add it normally. */
121 APPEND_ADD,
122
123 /* Add it normally and then add a nop. */
124 APPEND_ADD_WITH_NOP,
125
126 /* Turn an instruction with a delay slot into a "compact" version. */
127 APPEND_ADD_COMPACT,
128
129 /* Insert the instruction before the last one. */
130 APPEND_SWAP
131};
132
47e39b9d
RS
133/* Information about an instruction, including its format, operands
134 and fixups. */
135struct mips_cl_insn
136{
137 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
138 const struct mips_opcode *insn_mo;
139
47e39b9d 140 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
141 a copy of INSN_MO->match with the operands filled in. If we have
142 decided to use an extended MIPS16 instruction, this includes the
143 extension. */
47e39b9d
RS
144 unsigned long insn_opcode;
145
6f2117ba
PH
146 /* The name if this is an label. */
147 char label[16];
148
149 /* The target label name if this is an branch. */
150 char target[16];
151
47e39b9d
RS
152 /* The frag that contains the instruction. */
153 struct frag *frag;
154
155 /* The offset into FRAG of the first instruction byte. */
156 long where;
157
158 /* The relocs associated with the instruction, if any. */
159 fixS *fixp[3];
160
a38419a5
RS
161 /* True if this entry cannot be moved from its current position. */
162 unsigned int fixed_p : 1;
47e39b9d 163
708587a4 164 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
165 unsigned int noreorder_p : 1;
166
2fa15973
RS
167 /* True for mips16 instructions that jump to an absolute address. */
168 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
169
170 /* True if this instruction is complete. */
171 unsigned int complete_p : 1;
e407c74b
NC
172
173 /* True if this instruction is cleared from history by unconditional
174 branch. */
175 unsigned int cleared_p : 1;
47e39b9d
RS
176};
177
a325df1d
TS
178/* The ABI to use. */
179enum mips_abi_level
180{
181 NO_ABI = 0,
182 O32_ABI,
183 O64_ABI,
184 N32_ABI,
185 N64_ABI,
186 EABI_ABI
187};
188
189/* MIPS ABI we are using for this output file. */
316f5878 190static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 191
143d77c5 192/* Whether or not we have code that can call pic code. */
5b7c81bd 193int mips_abicalls = false;
143d77c5 194
aa6975fb
ILT
195/* Whether or not we have code which can be put into a shared
196 library. */
5b7c81bd 197static bool mips_in_shared = true;
aa6975fb 198
252b5132
RH
199/* This is the set of options which may be modified by the .set
200 pseudo-op. We use a struct so that .set push and .set pop are more
201 reliable. */
202
e972090a
NC
203struct mips_set_options
204{
252b5132
RH
205 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
206 if it has not been initialized. Changed by `.set mipsN', and the
207 -mipsN command line option, and the default CPU. */
208 int isa;
846ef2d0
RS
209 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
210 <asename>', by command line options, and based on the default
211 architecture. */
212 int ase;
252b5132
RH
213 /* Whether we are assembling for the mips16 processor. 0 if we are
214 not, 1 if we are, and -1 if the value has not been initialized.
215 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
216 -nomips16 command line options, and the default CPU. */
217 int mips16;
df58fc94
RS
218 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
219 1 if we are, and -1 if the value has not been initialized. Changed
220 by `.set micromips' and `.set nomicromips', and the -mmicromips
221 and -mno-micromips command line options, and the default CPU. */
222 int micromips;
252b5132
RH
223 /* Non-zero if we should not reorder instructions. Changed by `.set
224 reorder' and `.set noreorder'. */
225 int noreorder;
741fe287
MR
226 /* Non-zero if we should not permit the register designated "assembler
227 temporary" to be used in instructions. The value is the register
228 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
229 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
230 unsigned int at;
252b5132
RH
231 /* Non-zero if we should warn when a macro instruction expands into
232 more than one machine instruction. Changed by `.set nomacro' and
233 `.set macro'. */
234 int warn_about_macros;
235 /* Non-zero if we should not move instructions. Changed by `.set
236 move', `.set volatile', `.set nomove', and `.set novolatile'. */
237 int nomove;
238 /* Non-zero if we should not optimize branches by moving the target
239 of the branch into the delay slot. Actually, we don't perform
240 this optimization anyhow. Changed by `.set bopt' and `.set
241 nobopt'. */
242 int nobopt;
243 /* Non-zero if we should not autoextend mips16 instructions.
244 Changed by `.set autoextend' and `.set noautoextend'. */
245 int noautoextend;
833794fc
MR
246 /* True if we should only emit 32-bit microMIPS instructions.
247 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
248 and -mno-insn32 command line options. */
5b7c81bd 249 bool insn32;
a325df1d
TS
250 /* Restrict general purpose registers and floating point registers
251 to 32 bit. This is initially determined when -mgp32 or -mfp32
252 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 253 int gp;
0b35dfee 254 int fp;
fef14a42
TS
255 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
256 command line option, and the default CPU. */
257 int arch;
aed1a261 258 /* True if ".set sym32" is in effect. */
5b7c81bd 259 bool sym32;
037b32b9
AN
260 /* True if floating-point operations are not allowed. Changed by .set
261 softfloat or .set hardfloat, by command line options -msoft-float or
262 -mhard-float. The default is false. */
5b7c81bd 263 bool soft_float;
037b32b9
AN
264
265 /* True if only single-precision floating-point operations are allowed.
266 Changed by .set singlefloat or .set doublefloat, command-line options
267 -msingle-float or -mdouble-float. The default is false. */
5b7c81bd 268 bool single_float;
351cdf24
MF
269
270 /* 1 if single-precision operations on odd-numbered registers are
271 allowed. */
272 int oddspreg;
3315614d
MF
273
274 /* The set of ASEs that should be enabled for the user specified
275 architecture. This cannot be inferred from 'arch' for all cores
276 as processors only have a unique 'arch' if they add architecture
277 specific instructions (UDI). */
278 int init_ase;
252b5132
RH
279};
280
919731af 281/* Specifies whether module level options have been checked yet. */
5b7c81bd 282static bool file_mips_opts_checked = false;
919731af 283
7361da2c
AB
284/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
285 value has not been initialized. Changed by `.nan legacy' and
286 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
287 options, and the default CPU. */
288static int mips_nan2008 = -1;
a325df1d 289
0b35dfee 290/* This is the struct we use to hold the module level set of options.
bad1aba3 291 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 292 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 293
0b35dfee 294static struct mips_set_options file_mips_opts =
295{
296 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
297 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
5b7c81bd
AM
298 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
299 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
300 /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
3315614d 301 /* init_ase */ 0
0b35dfee 302};
252b5132 303
0b35dfee 304/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 305
e972090a
NC
306static struct mips_set_options mips_opts =
307{
846ef2d0 308 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 309 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
5b7c81bd
AM
310 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ false,
311 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ false,
312 /* soft_float */ false, /* single_float */ false, /* oddspreg */ -1,
3315614d 313 /* init_ase */ 0
e7af610e 314};
252b5132 315
846ef2d0
RS
316/* Which bits of file_ase were explicitly set or cleared by ASE options. */
317static unsigned int file_ase_explicit;
318
252b5132
RH
319/* These variables are filled in with the masks of registers used.
320 The object format code reads them and puts them in the appropriate
321 place. */
322unsigned long mips_gprmask;
323unsigned long mips_cprmask[4];
324
738f4d98 325/* True if any MIPS16 code was produced. */
a4672219
TS
326static int file_ase_mips16;
327
3994f87e
TS
328#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
329 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
330 || mips_opts.isa == ISA_MIPS32R3 \
331 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 332 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
333 || mips_opts.isa == ISA_MIPS64R2 \
334 || mips_opts.isa == ISA_MIPS64R3 \
335 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 336
df58fc94
RS
337/* True if any microMIPS code was produced. */
338static int file_ase_micromips;
339
b12dd2e4
CF
340/* True if we want to create R_MIPS_JALR for jalr $25. */
341#ifdef TE_IRIX
1180b5a4 342#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 343#else
1180b5a4
RS
344/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
345 because there's no place for any addend, the only acceptable
346 expression is a bare symbol. */
347#define MIPS_JALR_HINT_P(EXPR) \
348 (!HAVE_IN_PLACE_ADDENDS \
349 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
350#endif
351
ec68c924 352/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 353static const char *mips_arch_string;
ec68c924
EC
354
355/* The argument of the -mtune= flag. The architecture for which we
356 are optimizing. */
357static int mips_tune = CPU_UNKNOWN;
316f5878 358static const char *mips_tune_string;
ec68c924 359
316f5878 360/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
361static int mips_32bitmode = 0;
362
316f5878
RS
363/* True if the given ABI requires 32-bit registers. */
364#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
365
366/* Likewise 64-bit registers. */
707bfff6 367#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 368 ((ABI) == N32_ABI \
707bfff6 369 || (ABI) == N64_ABI \
316f5878
RS
370 || (ABI) == O64_ABI)
371
7361da2c
AB
372#define ISA_IS_R6(ISA) \
373 ((ISA) == ISA_MIPS32R6 \
374 || (ISA) == ISA_MIPS64R6)
375
ad3fea08 376/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
377#define ISA_HAS_64BIT_REGS(ISA) \
378 ((ISA) == ISA_MIPS3 \
379 || (ISA) == ISA_MIPS4 \
380 || (ISA) == ISA_MIPS5 \
381 || (ISA) == ISA_MIPS64 \
ae52f483
AB
382 || (ISA) == ISA_MIPS64R2 \
383 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
384 || (ISA) == ISA_MIPS64R5 \
385 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 386
ad3fea08
TS
387/* Return true if ISA supports 64 bit wide float registers. */
388#define ISA_HAS_64BIT_FPRS(ISA) \
389 ((ISA) == ISA_MIPS3 \
390 || (ISA) == ISA_MIPS4 \
391 || (ISA) == ISA_MIPS5 \
392 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
393 || (ISA) == ISA_MIPS32R3 \
394 || (ISA) == ISA_MIPS32R5 \
7361da2c 395 || (ISA) == ISA_MIPS32R6 \
ad3fea08 396 || (ISA) == ISA_MIPS64 \
ae52f483
AB
397 || (ISA) == ISA_MIPS64R2 \
398 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
399 || (ISA) == ISA_MIPS64R5 \
400 || (ISA) == ISA_MIPS64R6)
ad3fea08 401
af7ee8bf
CD
402/* Return true if ISA supports 64-bit right rotate (dror et al.)
403 instructions. */
707bfff6 404#define ISA_HAS_DROR(ISA) \
df58fc94 405 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
406 || (ISA) == ISA_MIPS64R3 \
407 || (ISA) == ISA_MIPS64R5 \
7361da2c 408 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
409 || (mips_opts.micromips \
410 && ISA_HAS_64BIT_REGS (ISA)) \
411 )
af7ee8bf
CD
412
413/* Return true if ISA supports 32-bit right rotate (ror et al.)
414 instructions. */
707bfff6
TS
415#define ISA_HAS_ROR(ISA) \
416 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
417 || (ISA) == ISA_MIPS32R3 \
418 || (ISA) == ISA_MIPS32R5 \
7361da2c 419 || (ISA) == ISA_MIPS32R6 \
707bfff6 420 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
846ef2d0 424 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
425 || mips_opts.micromips \
426 )
707bfff6 427
7455baf8 428/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
429#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
430 (((ISA) == ISA_MIPS32 \
431 || (ISA) == ISA_MIPS32R2 \
432 || (ISA) == ISA_MIPS32R3 \
433 || (ISA) == ISA_MIPS32R5 \
7361da2c 434 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
435 || (ISA) == ISA_MIPS64 \
436 || (ISA) == ISA_MIPS64R2 \
437 || (ISA) == ISA_MIPS64R3 \
438 || (ISA) == ISA_MIPS64R5 \
7361da2c 439 || (ISA) == ISA_MIPS64R6 \
351cdf24 440 || (CPU) == CPU_R5900) \
bd782c07 441 && ((CPU) != CPU_GS464 \
9108bc33
CX
442 || (CPU) != CPU_GS464E \
443 || (CPU) != CPU_GS264E))
af7ee8bf 444
ad3fea08
TS
445/* Return true if ISA supports move to/from high part of a 64-bit
446 floating-point register. */
447#define ISA_HAS_MXHC1(ISA) \
448 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
449 || (ISA) == ISA_MIPS32R3 \
450 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
451 || (ISA) == ISA_MIPS32R6 \
452 || (ISA) == ISA_MIPS64R2 \
453 || (ISA) == ISA_MIPS64R3 \
454 || (ISA) == ISA_MIPS64R5 \
455 || (ISA) == ISA_MIPS64R6)
456
457/* Return true if ISA supports legacy NAN. */
458#define ISA_HAS_LEGACY_NAN(ISA) \
459 ((ISA) == ISA_MIPS1 \
460 || (ISA) == ISA_MIPS2 \
461 || (ISA) == ISA_MIPS3 \
462 || (ISA) == ISA_MIPS4 \
463 || (ISA) == ISA_MIPS5 \
464 || (ISA) == ISA_MIPS32 \
465 || (ISA) == ISA_MIPS32R2 \
466 || (ISA) == ISA_MIPS32R3 \
467 || (ISA) == ISA_MIPS32R5 \
468 || (ISA) == ISA_MIPS64 \
ae52f483
AB
469 || (ISA) == ISA_MIPS64R2 \
470 || (ISA) == ISA_MIPS64R3 \
471 || (ISA) == ISA_MIPS64R5)
ad3fea08 472
bad1aba3 473#define GPR_SIZE \
474 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
475 ? 32 \
476 : mips_opts.gp)
ca4e0257 477
bad1aba3 478#define FPR_SIZE \
479 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
480 ? 32 \
481 : mips_opts.fp)
ca4e0257 482
316f5878 483#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 484
316f5878 485#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 486
3b91255e
RS
487/* True if relocations are stored in-place. */
488#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
489
aed1a261
RS
490/* The ABI-derived address size. */
491#define HAVE_64BIT_ADDRESSES \
bad1aba3 492 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 493#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 494
aed1a261
RS
495/* The size of symbolic constants (i.e., expressions of the form
496 "SYMBOL" or "SYMBOL + OFFSET"). */
497#define HAVE_32BIT_SYMBOLS \
498 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
499#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 500
b7c7d6c1
TS
501/* Addresses are loaded in different ways, depending on the address size
502 in use. The n32 ABI Documentation also mandates the use of additions
503 with overflow checking, but existing implementations don't follow it. */
f899b4b8 504#define ADDRESS_ADD_INSN \
b7c7d6c1 505 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
506
507#define ADDRESS_ADDI_INSN \
b7c7d6c1 508 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
509
510#define ADDRESS_LOAD_INSN \
511 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
512
513#define ADDRESS_STORE_INSN \
514 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
515
a4672219 516/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36 517#define CPU_HAS_MIPS16(cpu) \
d34049e8
ML
518 (startswith (TARGET_CPU, "mips16") \
519 || startswith (TARGET_CANONICAL, "mips-lsi-elf"))
a4672219 520
2309ddf2 521/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
522#define CPU_HAS_MICROMIPS(cpu) 0
523
60b63b72
RS
524/* True if CPU has a dror instruction. */
525#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
526
527/* True if CPU has a ror instruction. */
528#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
529
6f2117ba 530/* True if CPU is in the Octeon family. */
2c629856
N
531#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
532 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 533
dd3cbb7e 534/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 535#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 536
0aa27725
RS
537/* True, if CPU has support for ldc1 and sdc1. */
538#define CPU_HAS_LDC1_SDC1(CPU) \
539 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
540
c8978940
CD
541/* True if mflo and mfhi can be immediately followed by instructions
542 which write to the HI and LO registers.
543
544 According to MIPS specifications, MIPS ISAs I, II, and III need
545 (at least) two instructions between the reads of HI/LO and
546 instructions which write them, and later ISAs do not. Contradicting
547 the MIPS specifications, some MIPS IV processor user manuals (e.g.
548 the UM for the NEC Vr5000) document needing the instructions between
549 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
550 MIPS64 and later ISAs to have the interlocks, plus any specific
551 earlier-ISA CPUs for which CPU documentation declares that the
552 instructions are really interlocked. */
553#define hilo_interlocks \
554 (mips_opts.isa == ISA_MIPS32 \
555 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
556 || mips_opts.isa == ISA_MIPS32R3 \
557 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 558 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
559 || mips_opts.isa == ISA_MIPS64 \
560 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
561 || mips_opts.isa == ISA_MIPS64R3 \
562 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 563 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 564 || mips_opts.arch == CPU_R4010 \
e407c74b 565 || mips_opts.arch == CPU_R5900 \
c8978940
CD
566 || mips_opts.arch == CPU_R10000 \
567 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
568 || mips_opts.arch == CPU_R14000 \
569 || mips_opts.arch == CPU_R16000 \
c8978940 570 || mips_opts.arch == CPU_RM7000 \
c8978940 571 || mips_opts.arch == CPU_VR5500 \
df58fc94 572 || mips_opts.micromips \
c8978940 573 )
252b5132
RH
574
575/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
576 from the GPRs after they are loaded from memory, and thus does not
577 require nops to be inserted. This applies to instructions marked
67dc82bc 578 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
579 level I and microMIPS mode instructions are always interlocked. */
580#define gpr_interlocks \
581 (mips_opts.isa != ISA_MIPS1 \
582 || mips_opts.arch == CPU_R3900 \
e407c74b 583 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
584 || mips_opts.micromips \
585 )
252b5132 586
81912461
ILT
587/* Whether the processor uses hardware interlocks to avoid delays
588 required by coprocessor instructions, and thus does not require
589 nops to be inserted. This applies to instructions marked
43885403
MF
590 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
591 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 592 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
593 levels I, II, and III and microMIPS mode instructions are always
594 interlocked. */
bdaaa2e1 595/* Itbl support may require additional care here. */
81912461
ILT
596#define cop_interlocks \
597 ((mips_opts.isa != ISA_MIPS1 \
598 && mips_opts.isa != ISA_MIPS2 \
599 && mips_opts.isa != ISA_MIPS3) \
600 || mips_opts.arch == CPU_R4300 \
df58fc94 601 || mips_opts.micromips \
81912461
ILT
602 )
603
604/* Whether the processor uses hardware interlocks to protect reads
605 from coprocessor registers after they are loaded from memory, and
606 thus does not require nops to be inserted. This applies to
607 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
608 requires at MIPS ISA level I and microMIPS mode instructions are
609 always interlocked. */
610#define cop_mem_interlocks \
611 (mips_opts.isa != ISA_MIPS1 \
612 || mips_opts.micromips \
613 )
252b5132 614
6b76fefe
CM
615/* Is this a mfhi or mflo instruction? */
616#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
617 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
618
df58fc94
RS
619/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
620 has been selected. This implies, in particular, that addresses of text
621 labels have their LSB set. */
622#define HAVE_CODE_COMPRESSION \
623 ((mips_opts.mips16 | mips_opts.micromips) != 0)
624
42429eac 625/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 626#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
627#define GPR_SMIN (-GPR_SMAX - 1)
628
252b5132
RH
629/* MIPS PIC level. */
630
a161fe53 631enum mips_pic_level mips_pic;
252b5132 632
c9914766 633/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 634 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 635static int mips_big_got = 0;
252b5132
RH
636
637/* 1 if trap instructions should used for overflow rather than break
638 instructions. */
c9914766 639static int mips_trap = 0;
252b5132 640
119d663a 641/* 1 if double width floating point constants should not be constructed
b6ff326e 642 by assembling two single width halves into two single width floating
119d663a
NC
643 point registers which just happen to alias the double width destination
644 register. On some architectures this aliasing can be disabled by a bit
d547a75e 645 in the status register, and the setting of this bit cannot be determined
119d663a
NC
646 automatically at assemble time. */
647static int mips_disable_float_construction;
648
252b5132
RH
649/* Non-zero if any .set noreorder directives were used. */
650
651static int mips_any_noreorder;
652
6b76fefe
CM
653/* Non-zero if nops should be inserted when the register referenced in
654 an mfhi/mflo instruction is read in the next two instructions. */
655static int mips_7000_hilo_fix;
656
02ffd3e4 657/* The size of objects in the small data section. */
156c2f8b 658static unsigned int g_switch_value = 8;
252b5132
RH
659/* Whether the -G option was used. */
660static int g_switch_seen = 0;
661
662#define N_RMASK 0xc4
663#define N_VFP 0xd4
664
665/* If we can determine in advance that GP optimization won't be
666 possible, we can skip the relaxation stuff that tries to produce
667 GP-relative references. This makes delay slot optimization work
668 better.
669
670 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
671 gcc output. It needs to guess right for gcc, otherwise gcc
672 will put what it thinks is a GP-relative instruction in a branch
673 delay slot.
252b5132
RH
674
675 I don't know if a fix is needed for the SVR4_PIC mode. I've only
676 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 677static int nopic_need_relax (symbolS *, int);
252b5132 678
6f2117ba 679/* Handle of the OPCODE hash table. */
629310ab 680static htab_t op_hash = NULL;
252b5132
RH
681
682/* The opcode hash table we use for the mips16. */
629310ab 683static htab_t mips16_op_hash = NULL;
252b5132 684
df58fc94 685/* The opcode hash table we use for the microMIPS ASE. */
629310ab 686static htab_t micromips_op_hash = NULL;
df58fc94 687
252b5132 688/* This array holds the chars that always start a comment. If the
6f2117ba 689 pre-processor is disabled, these aren't very useful. */
252b5132
RH
690const char comment_chars[] = "#";
691
692/* This array holds the chars that only start a comment at the beginning of
693 a line. If the line seems to have the form '# 123 filename'
6f2117ba 694 .line and .file directives will appear in the pre-processed output. */
252b5132
RH
695/* Note that input_file.c hand checks for '#' at the beginning of the
696 first line of the input file. This is because the compiler outputs
bdaaa2e1 697 #NO_APP at the beginning of its output. */
252b5132
RH
698/* Also note that C style comments are always supported. */
699const char line_comment_chars[] = "#";
700
bdaaa2e1 701/* This array holds machine specific line separator characters. */
63a0b638 702const char line_separator_chars[] = ";";
252b5132 703
6f2117ba 704/* Chars that can be used to separate mant from exp in floating point nums. */
252b5132
RH
705const char EXP_CHARS[] = "eE";
706
6f2117ba
PH
707/* Chars that mean this number is a floating point constant.
708 As in 0f12.456
709 or 0d1.2345e12. */
252b5132
RH
710const char FLT_CHARS[] = "rRsSfFdDxXpP";
711
712/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
713 changed in read.c . Ideally it shouldn't have to know about it at all,
6f2117ba 714 but nothing is ideal around here. */
252b5132 715
e3de51ce 716/* Types of printf format used for instruction-related error messages.
6f2117ba
PH
717 "I" means int ("%d") and "S" means string ("%s"). */
718enum mips_insn_error_format
719{
e3de51ce
RS
720 ERR_FMT_PLAIN,
721 ERR_FMT_I,
722 ERR_FMT_SS,
723};
724
725/* Information about an error that was found while assembling the current
726 instruction. */
6f2117ba
PH
727struct mips_insn_error
728{
e3de51ce
RS
729 /* We sometimes need to match an instruction against more than one
730 opcode table entry. Errors found during this matching are reported
731 against a particular syntactic argument rather than against the
732 instruction as a whole. We grade these messages so that errors
733 against argument N have a greater priority than an error against
734 any argument < N, since the former implies that arguments up to N
735 were acceptable and that the opcode entry was therefore a closer match.
736 If several matches report an error against the same argument,
737 we only use that error if it is the same in all cases.
738
739 min_argnum is the minimum argument number for which an error message
740 should be accepted. It is 0 if MSG is against the instruction as
741 a whole. */
742 int min_argnum;
743
744 /* The printf()-style message, including its format and arguments. */
745 enum mips_insn_error_format format;
746 const char *msg;
6f2117ba
PH
747 union
748 {
e3de51ce
RS
749 int i;
750 const char *ss[2];
751 } u;
752};
753
754/* The error that should be reported for the current instruction. */
755static struct mips_insn_error insn_error;
252b5132
RH
756
757static int auto_align = 1;
758
759/* When outputting SVR4 PIC code, the assembler needs to know the
760 offset in the stack frame from which to restore the $gp register.
761 This is set by the .cprestore pseudo-op, and saved in this
762 variable. */
763static offsetT mips_cprestore_offset = -1;
764
67c1ffbe 765/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 766 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 767 offset and even an other register than $gp as global pointer. */
6478892d
TS
768static offsetT mips_cpreturn_offset = -1;
769static int mips_cpreturn_register = -1;
770static int mips_gp_register = GP;
def2e0dd 771static int mips_gprel_offset = 0;
6478892d 772
7a621144
DJ
773/* Whether mips_cprestore_offset has been set in the current function
774 (or whether it has already been warned about, if not). */
775static int mips_cprestore_valid = 0;
776
252b5132
RH
777/* This is the register which holds the stack frame, as set by the
778 .frame pseudo-op. This is needed to implement .cprestore. */
779static int mips_frame_reg = SP;
780
7a621144
DJ
781/* Whether mips_frame_reg has been set in the current function
782 (or whether it has already been warned about, if not). */
783static int mips_frame_reg_valid = 0;
784
252b5132
RH
785/* To output NOP instructions correctly, we need to keep information
786 about the previous two instructions. */
787
788/* Whether we are optimizing. The default value of 2 means to remove
789 unneeded NOPs and swap branch instructions when possible. A value
790 of 1 means to not swap branches. A value of 0 means to always
791 insert NOPs. */
792static int mips_optimize = 2;
793
794/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
795 equivalent to seeing no -g option at all. */
796static int mips_debug = 0;
797
7d8e00cf
RS
798/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
799#define MAX_VR4130_NOPS 4
800
801/* The maximum number of NOPs needed to fill delay slots. */
802#define MAX_DELAY_NOPS 2
803
804/* The maximum number of NOPs needed for any purpose. */
805#define MAX_NOPS 4
71400594 806
6f2117ba
PH
807/* The maximum range of context length of ll/sc. */
808#define MAX_LLSC_RANGE 20
809
71400594
RS
810/* A list of previous instructions, with index 0 being the most recent.
811 We need to look back MAX_NOPS instructions when filling delay slots
812 or working around processor errata. We need to look back one
813 instruction further if we're thinking about using history[0] to
814 fill a branch delay slot. */
6f2117ba 815static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
252b5132 816
dec7b24b
YS
817/* The maximum number of LABELS detect for the same address. */
818#define MAX_LABELS_SAME 10
819
fc76e730 820/* Arrays of operands for each instruction. */
14daeee3 821#define MAX_OPERANDS 6
6f2117ba
PH
822struct mips_operand_array
823{
fc76e730
RS
824 const struct mips_operand *operand[MAX_OPERANDS];
825};
826static struct mips_operand_array *mips_operands;
827static struct mips_operand_array *mips16_operands;
828static struct mips_operand_array *micromips_operands;
829
1e915849 830/* Nop instructions used by emit_nop. */
df58fc94
RS
831static struct mips_cl_insn nop_insn;
832static struct mips_cl_insn mips16_nop_insn;
833static struct mips_cl_insn micromips_nop16_insn;
834static struct mips_cl_insn micromips_nop32_insn;
1e915849 835
6f2117ba
PH
836/* Sync instructions used by insert sync. */
837static struct mips_cl_insn sync_insn;
838
1e915849 839/* The appropriate nop for the current mode. */
833794fc
MR
840#define NOP_INSN (mips_opts.mips16 \
841 ? &mips16_nop_insn \
842 : (mips_opts.micromips \
843 ? (mips_opts.insn32 \
844 ? &micromips_nop32_insn \
845 : &micromips_nop16_insn) \
846 : &nop_insn))
df58fc94
RS
847
848/* The size of NOP_INSN in bytes. */
833794fc
MR
849#define NOP_INSN_SIZE ((mips_opts.mips16 \
850 || (mips_opts.micromips && !mips_opts.insn32)) \
851 ? 2 : 4)
252b5132 852
252b5132
RH
853/* If this is set, it points to a frag holding nop instructions which
854 were inserted before the start of a noreorder section. If those
855 nops turn out to be unnecessary, the size of the frag can be
856 decreased. */
857static fragS *prev_nop_frag;
858
859/* The number of nop instructions we created in prev_nop_frag. */
860static int prev_nop_frag_holds;
861
862/* The number of nop instructions that we know we need in
bdaaa2e1 863 prev_nop_frag. */
252b5132
RH
864static int prev_nop_frag_required;
865
866/* The number of instructions we've seen since prev_nop_frag. */
867static int prev_nop_frag_since;
868
e8044f35
RS
869/* Relocations against symbols are sometimes done in two parts, with a HI
870 relocation and a LO relocation. Each relocation has only 16 bits of
871 space to store an addend. This means that in order for the linker to
872 handle carries correctly, it must be able to locate both the HI and
873 the LO relocation. This means that the relocations must appear in
874 order in the relocation table.
252b5132
RH
875
876 In order to implement this, we keep track of each unmatched HI
877 relocation. We then sort them so that they immediately precede the
bdaaa2e1 878 corresponding LO relocation. */
252b5132 879
e972090a
NC
880struct mips_hi_fixup
881{
252b5132
RH
882 /* Next HI fixup. */
883 struct mips_hi_fixup *next;
884 /* This fixup. */
885 fixS *fixp;
886 /* The section this fixup is in. */
887 segT seg;
888};
889
890/* The list of unmatched HI relocs. */
891
892static struct mips_hi_fixup *mips_hi_fixup_list;
893
252b5132
RH
894/* Map mips16 register numbers to normal MIPS register numbers. */
895
e972090a
NC
896static const unsigned int mips16_to_32_reg_map[] =
897{
252b5132
RH
898 16, 17, 2, 3, 4, 5, 6, 7
899};
60b63b72 900
df58fc94
RS
901/* Map microMIPS register numbers to normal MIPS register numbers. */
902
df58fc94 903#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
904
905/* The microMIPS registers with type h. */
e76ff5ab 906static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
907{
908 5, 5, 6, 4, 4, 4, 4, 4
909};
e76ff5ab 910static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
911{
912 6, 7, 7, 21, 22, 5, 6, 7
913};
914
df58fc94
RS
915/* The microMIPS registers with type m. */
916static const unsigned int micromips_to_32_reg_m_map[] =
917{
918 0, 17, 2, 3, 16, 18, 19, 20
919};
920
921#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
922
71400594
RS
923/* Classifies the kind of instructions we're interested in when
924 implementing -mfix-vr4120. */
c67a084a
NC
925enum fix_vr4120_class
926{
71400594
RS
927 FIX_VR4120_MACC,
928 FIX_VR4120_DMACC,
929 FIX_VR4120_MULT,
930 FIX_VR4120_DMULT,
931 FIX_VR4120_DIV,
932 FIX_VR4120_MTHILO,
933 NUM_FIX_VR4120_CLASSES
934};
935
c67a084a 936/* ...likewise -mfix-loongson2f-jump. */
5b7c81bd 937static bool mips_fix_loongson2f_jump;
c67a084a
NC
938
939/* ...likewise -mfix-loongson2f-nop. */
5b7c81bd 940static bool mips_fix_loongson2f_nop;
c67a084a
NC
941
942/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
5b7c81bd 943static bool mips_fix_loongson2f;
c67a084a 944
71400594
RS
945/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
946 there must be at least one other instruction between an instruction
947 of type X and an instruction of type Y. */
948static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
949
950/* True if -mfix-vr4120 is in force. */
d766e8ec 951static int mips_fix_vr4120;
4a6a3df4 952
7d8e00cf
RS
953/* ...likewise -mfix-vr4130. */
954static int mips_fix_vr4130;
955
6a32d874
CM
956/* ...likewise -mfix-24k. */
957static int mips_fix_24k;
958
a8d14a88
CM
959/* ...likewise -mfix-rm7000 */
960static int mips_fix_rm7000;
961
d954098f 962/* ...likewise -mfix-cn63xxp1 */
5b7c81bd 963static bool mips_fix_cn63xxp1;
d954098f 964
27c634e0 965/* ...likewise -mfix-r5900 */
5b7c81bd
AM
966static bool mips_fix_r5900;
967static bool mips_fix_r5900_explicit;
27c634e0 968
6f2117ba 969/* ...likewise -mfix-loongson3-llsc. */
5b7c81bd 970static bool mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
6f2117ba 971
4a6a3df4
AO
972/* We don't relax branches by default, since this causes us to expand
973 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
974 fail to compute the offset before expanding the macro to the most
975 efficient expansion. */
976
977static int mips_relax_branch;
8b10b0b3
MR
978
979/* TRUE if checks are suppressed for invalid branches between ISA modes.
980 Needed for broken assembly produced by some GCC versions and some
981 sloppy code out there, where branches to data labels are present. */
5b7c81bd 982static bool mips_ignore_branch_isa;
252b5132 983\f
4d7206a2
RS
984/* The expansion of many macros depends on the type of symbol that
985 they refer to. For example, when generating position-dependent code,
986 a macro that refers to a symbol may have two different expansions,
987 one which uses GP-relative addresses and one which uses absolute
988 addresses. When generating SVR4-style PIC, a macro may have
989 different expansions for local and global symbols.
990
991 We handle these situations by generating both sequences and putting
992 them in variant frags. In position-dependent code, the first sequence
993 will be the GP-relative one and the second sequence will be the
994 absolute one. In SVR4 PIC, the first sequence will be for global
995 symbols and the second will be for local symbols.
996
584892a6
RS
997 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
998 SECOND are the lengths of the two sequences in bytes. These fields
999 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1000 the subtype has the following flags:
4d7206a2 1001
ce8ad872
MR
1002 RELAX_PIC
1003 Set if generating PIC code.
1004
584892a6
RS
1005 RELAX_USE_SECOND
1006 Set if it has been decided that we should use the second
1007 sequence instead of the first.
1008
1009 RELAX_SECOND_LONGER
1010 Set in the first variant frag if the macro's second implementation
1011 is longer than its first. This refers to the macro as a whole,
1012 not an individual relaxation.
1013
1014 RELAX_NOMACRO
1015 Set in the first variant frag if the macro appeared in a .set nomacro
1016 block and if one alternative requires a warning but the other does not.
1017
1018 RELAX_DELAY_SLOT
1019 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1020 delay slot.
4d7206a2 1021
df58fc94
RS
1022 RELAX_DELAY_SLOT_16BIT
1023 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1024 16-bit instruction.
1025
1026 RELAX_DELAY_SLOT_SIZE_FIRST
1027 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1028 the macro is of the wrong size for the branch delay slot.
1029
1030 RELAX_DELAY_SLOT_SIZE_SECOND
1031 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1032 the macro is of the wrong size for the branch delay slot.
1033
4d7206a2
RS
1034 The frag's "opcode" points to the first fixup for relaxable code.
1035
1036 Relaxable macros are generated using a sequence such as:
1037
1038 relax_start (SYMBOL);
1039 ... generate first expansion ...
1040 relax_switch ();
1041 ... generate second expansion ...
1042 relax_end ();
1043
1044 The code and fixups for the unwanted alternative are discarded
1045 by md_convert_frag. */
ce8ad872
MR
1046#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1047 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1048
584892a6
RS
1049#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1050#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1051#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1052#define RELAX_USE_SECOND 0x20000
1053#define RELAX_SECOND_LONGER 0x40000
1054#define RELAX_NOMACRO 0x80000
1055#define RELAX_DELAY_SLOT 0x100000
1056#define RELAX_DELAY_SLOT_16BIT 0x200000
1057#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1058#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1059
4a6a3df4
AO
1060/* Branch without likely bit. If label is out of range, we turn:
1061
134c0c8b 1062 beq reg1, reg2, label
4a6a3df4
AO
1063 delay slot
1064
1065 into
1066
1067 bne reg1, reg2, 0f
1068 nop
1069 j label
1070 0: delay slot
1071
1072 with the following opcode replacements:
1073
1074 beq <-> bne
1075 blez <-> bgtz
1076 bltz <-> bgez
1077 bc1f <-> bc1t
1078
1079 bltzal <-> bgezal (with jal label instead of j label)
1080
1081 Even though keeping the delay slot instruction in the delay slot of
1082 the branch would be more efficient, it would be very tricky to do
1083 correctly, because we'd have to introduce a variable frag *after*
1084 the delay slot instruction, and expand that instead. Let's do it
1085 the easy way for now, even if the branch-not-taken case now costs
1086 one additional instruction. Out-of-range branches are not supposed
1087 to be common, anyway.
1088
1089 Branch likely. If label is out of range, we turn:
1090
1091 beql reg1, reg2, label
1092 delay slot (annulled if branch not taken)
1093
1094 into
1095
1096 beql reg1, reg2, 1f
1097 nop
1098 beql $0, $0, 2f
1099 nop
1100 1: j[al] label
1101 delay slot (executed only if branch taken)
1102 2:
1103
1104 It would be possible to generate a shorter sequence by losing the
1105 likely bit, generating something like:
b34976b6 1106
4a6a3df4
AO
1107 bne reg1, reg2, 0f
1108 nop
1109 j[al] label
1110 delay slot (executed only if branch taken)
1111 0:
1112
1113 beql -> bne
1114 bnel -> beq
1115 blezl -> bgtz
1116 bgtzl -> blez
1117 bltzl -> bgez
1118 bgezl -> bltz
1119 bc1fl -> bc1t
1120 bc1tl -> bc1f
1121
1122 bltzall -> bgezal (with jal label instead of j label)
1123 bgezall -> bltzal (ditto)
1124
1125
1126 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1127#define RELAX_BRANCH_ENCODE(at, pic, \
1128 uncond, likely, link, toofar) \
66b3e8da
MR
1129 ((relax_substateT) \
1130 (0xc0000000 \
1131 | ((at) & 0x1f) \
ce8ad872
MR
1132 | ((pic) ? 0x20 : 0) \
1133 | ((toofar) ? 0x40 : 0) \
1134 | ((link) ? 0x80 : 0) \
1135 | ((likely) ? 0x100 : 0) \
1136 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1137#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1138#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1139#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1140#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1141#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1142#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1143#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1144
252b5132
RH
1145/* For mips16 code, we use an entirely different form of relaxation.
1146 mips16 supports two versions of most instructions which take
1147 immediate values: a small one which takes some small value, and a
1148 larger one which takes a 16 bit value. Since branches also follow
1149 this pattern, relaxing these values is required.
1150
1151 We can assemble both mips16 and normal MIPS code in a single
1152 object. Therefore, we need to support this type of relaxation at
1153 the same time that we support the relaxation described above. We
1154 use the high bit of the subtype field to distinguish these cases.
1155
1156 The information we store for this type of relaxation is the
1157 argument code found in the opcode file for this relocation, whether
1158 the user explicitly requested a small or extended form, and whether
1159 the relocation is in a jump or jal delay slot. That tells us the
1160 size of the value, and how it should be stored. We also store
1161 whether the fragment is considered to be extended or not. We also
1162 store whether this is known to be a branch to a different section,
1163 whether we have tried to relax this frag yet, and whether we have
1164 ever extended a PC relative fragment because of a shift count. */
25499ac7 1165#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1166 small, ext, \
1167 dslot, jal_dslot) \
252b5132
RH
1168 (0x80000000 \
1169 | ((type) & 0xff) \
25499ac7
MR
1170 | ((e2) ? 0x100 : 0) \
1171 | ((pic) ? 0x200 : 0) \
1172 | ((sym32) ? 0x400 : 0) \
1173 | ((nomacro) ? 0x800 : 0) \
1174 | ((small) ? 0x1000 : 0) \
1175 | ((ext) ? 0x2000 : 0) \
1176 | ((dslot) ? 0x4000 : 0) \
1177 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1178
4a6a3df4 1179#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1180#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1181#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1182#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1183#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1184#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1185#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1186#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1187#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1188#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1189
1190#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1191#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1192#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1193#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1194#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1195#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1196#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1197#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1198#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1199
df58fc94
RS
1200/* For microMIPS code, we use relaxation similar to one we use for
1201 MIPS16 code. Some instructions that take immediate values support
1202 two encodings: a small one which takes some small value, and a
1203 larger one which takes a 16 bit value. As some branches also follow
1204 this pattern, relaxing these values is required.
1205
1206 We can assemble both microMIPS and normal MIPS code in a single
1207 object. Therefore, we need to support this type of relaxation at
1208 the same time that we support the relaxation described above. We
1209 use one of the high bits of the subtype field to distinguish these
1210 cases.
1211
1212 The information we store for this type of relaxation is the argument
1213 code found in the opcode file for this relocation, the register
8484fb75
MR
1214 selected as the assembler temporary, whether in the 32-bit
1215 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1216 compact, whether there is no delay-slot instruction available to fill
1217 in, whether it stores the link address implicitly in $ra, whether
1218 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1219 instructions is enabled, and whether the displacement of a branch is
1220 too large to fit as an immediate argument of a 16-bit and a 32-bit
1221 branch, respectively. */
ce8ad872 1222#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1223 uncond, compact, link, nods, \
40209cad
MR
1224 relax32, toofar16, toofar32) \
1225 (0x40000000 \
1226 | ((type) & 0xff) \
1227 | (((at) & 0x1f) << 8) \
8484fb75 1228 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1229 | ((pic) ? 0x4000 : 0) \
1230 | ((uncond) ? 0x8000 : 0) \
1231 | ((compact) ? 0x10000 : 0) \
1232 | ((link) ? 0x20000 : 0) \
1233 | ((nods) ? 0x40000 : 0) \
1234 | ((relax32) ? 0x80000 : 0) \
1235 | ((toofar16) ? 0x100000 : 0) \
1236 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1237#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1238#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1239#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1240#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1241#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1242#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1243#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1244#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1245#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1246#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1247
1248#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1249#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1250#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1251#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1252#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1253#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1254
43c0598f
RS
1255/* Sign-extend 16-bit value X. */
1256#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1257
885add95
CD
1258/* Is the given value a sign-extended 32-bit value? */
1259#define IS_SEXT_32BIT_NUM(x) \
1260 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1261 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1262
1263/* Is the given value a sign-extended 16-bit value? */
1264#define IS_SEXT_16BIT_NUM(x) \
1265 (((x) &~ (offsetT) 0x7fff) == 0 \
1266 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1267
df58fc94
RS
1268/* Is the given value a sign-extended 12-bit value? */
1269#define IS_SEXT_12BIT_NUM(x) \
1270 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1271
7f3c4072
CM
1272/* Is the given value a sign-extended 9-bit value? */
1273#define IS_SEXT_9BIT_NUM(x) \
1274 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1275
2051e8c4
MR
1276/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1277#define IS_ZEXT_32BIT_NUM(x) \
1278 (((x) &~ (offsetT) 0xffffffff) == 0 \
1279 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1280
bf12938e
RS
1281/* Extract bits MASK << SHIFT from STRUCT and shift them right
1282 SHIFT places. */
1283#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1284 (((STRUCT) >> (SHIFT)) & (MASK))
1285
bf12938e 1286/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1287#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1288 (!(MICROMIPS) \
1289 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1290 : EXTRACT_BITS ((INSN).insn_opcode, \
1291 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1292#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1293 EXTRACT_BITS ((INSN).insn_opcode, \
1294 MIPS16OP_MASK_##FIELD, \
1295 MIPS16OP_SH_##FIELD)
5c04167a
RS
1296
1297/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1298#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1299\f
df58fc94 1300/* Whether or not we are emitting a branch-likely macro. */
5b7c81bd 1301static bool emit_branch_likely_macro = false;
df58fc94 1302
4d7206a2
RS
1303/* Global variables used when generating relaxable macros. See the
1304 comment above RELAX_ENCODE for more details about how relaxation
1305 is used. */
1306static struct {
1307 /* 0 if we're not emitting a relaxable macro.
1308 1 if we're emitting the first of the two relaxation alternatives.
1309 2 if we're emitting the second alternative. */
1310 int sequence;
1311
1312 /* The first relaxable fixup in the current frag. (In other words,
1313 the first fixup that refers to relaxable code.) */
1314 fixS *first_fixup;
1315
1316 /* sizes[0] says how many bytes of the first alternative are stored in
1317 the current frag. Likewise sizes[1] for the second alternative. */
1318 unsigned int sizes[2];
1319
1320 /* The symbol on which the choice of sequence depends. */
1321 symbolS *symbol;
1322} mips_relax;
252b5132 1323\f
584892a6
RS
1324/* Global variables used to decide whether a macro needs a warning. */
1325static struct {
1326 /* True if the macro is in a branch delay slot. */
5b7c81bd 1327 bool delay_slot_p;
584892a6 1328
df58fc94
RS
1329 /* Set to the length in bytes required if the macro is in a delay slot
1330 that requires a specific length of instruction, otherwise zero. */
1331 unsigned int delay_slot_length;
1332
584892a6
RS
1333 /* For relaxable macros, sizes[0] is the length of the first alternative
1334 in bytes and sizes[1] is the length of the second alternative.
1335 For non-relaxable macros, both elements give the length of the
1336 macro in bytes. */
1337 unsigned int sizes[2];
1338
df58fc94
RS
1339 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1340 instruction of the first alternative in bytes and first_insn_sizes[1]
1341 is the length of the first instruction of the second alternative.
1342 For non-relaxable macros, both elements give the length of the first
1343 instruction in bytes.
1344
1345 Set to zero if we haven't yet seen the first instruction. */
1346 unsigned int first_insn_sizes[2];
1347
1348 /* For relaxable macros, insns[0] is the number of instructions for the
1349 first alternative and insns[1] is the number of instructions for the
1350 second alternative.
1351
1352 For non-relaxable macros, both elements give the number of
1353 instructions for the macro. */
1354 unsigned int insns[2];
1355
584892a6
RS
1356 /* The first variant frag for this macro. */
1357 fragS *first_frag;
1358} mips_macro_warning;
1359\f
252b5132
RH
1360/* Prototypes for static functions. */
1361
252b5132
RH
1362enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1363
b34976b6 1364static void append_insn
df58fc94 1365 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
5b7c81bd 1366 bool expansionp);
7d10b47d 1367static void mips_no_prev_insn (void);
c67a084a 1368static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1369static void mips16_macro_build
03ea81db 1370 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1371static void load_register (int, expressionS *, int);
584892a6
RS
1372static void macro_start (void);
1373static void macro_end (void);
833794fc 1374static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1375static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1376static void mips_ip (char *str, struct mips_cl_insn * ip);
1377static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1378static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1379static void mips16_immed
3b4dbbbf 1380 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1381 unsigned int, unsigned long *);
5e0116d5 1382static size_t my_getSmallExpression
17a2f251
TS
1383 (expressionS *, bfd_reloc_code_real_type *, char *);
1384static void my_getExpression (expressionS *, char *);
1385static void s_align (int);
1386static void s_change_sec (int);
1387static void s_change_section (int);
1388static void s_cons (int);
1389static void s_float_cons (int);
1390static void s_mips_globl (int);
1391static void s_option (int);
1392static void s_mipsset (int);
1393static void s_abicalls (int);
1394static void s_cpload (int);
1395static void s_cpsetup (int);
1396static void s_cplocal (int);
1397static void s_cprestore (int);
1398static void s_cpreturn (int);
741d6ea8
JM
1399static void s_dtprelword (int);
1400static void s_dtpreldword (int);
d0f13682
CLT
1401static void s_tprelword (int);
1402static void s_tpreldword (int);
17a2f251
TS
1403static void s_gpvalue (int);
1404static void s_gpword (int);
1405static void s_gpdword (int);
a3f278e2 1406static void s_ehword (int);
17a2f251
TS
1407static void s_cpadd (int);
1408static void s_insn (int);
ba92f887 1409static void s_nan (int);
919731af 1410static void s_module (int);
17a2f251
TS
1411static void s_mips_ent (int);
1412static void s_mips_end (int);
1413static void s_mips_frame (int);
1414static void s_mips_mask (int reg_type);
1415static void s_mips_stab (int);
1416static void s_mips_weakext (int);
1417static void s_mips_file (int);
1418static void s_mips_loc (int);
5b7c81bd 1419static bool pic_need_relax (symbolS *);
4a6a3df4 1420static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1421static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1422static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1423static void file_mips_check_options (void);
e7af610e
NC
1424
1425/* Table and functions used to map between CPU/ISA names, and
1426 ISA levels, and CPU numbers. */
1427
e972090a
NC
1428struct mips_cpu_info
1429{
e7af610e 1430 const char *name; /* CPU or ISA name. */
d16afab6
RS
1431 int flags; /* MIPS_CPU_* flags. */
1432 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1433 int isa; /* ISA level. */
1434 int cpu; /* CPU number (default CPU if ISA). */
1435};
1436
ad3fea08 1437#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1438
17a2f251
TS
1439static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1440static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1441static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1442\f
c31f3936
RS
1443/* Command-line options. */
1444const char *md_shortopts = "O::g::G:";
1445
1446enum options
1447 {
1448 OPTION_MARCH = OPTION_MD_BASE,
1449 OPTION_MTUNE,
1450 OPTION_MIPS1,
1451 OPTION_MIPS2,
1452 OPTION_MIPS3,
1453 OPTION_MIPS4,
1454 OPTION_MIPS5,
1455 OPTION_MIPS32,
1456 OPTION_MIPS64,
1457 OPTION_MIPS32R2,
ae52f483
AB
1458 OPTION_MIPS32R3,
1459 OPTION_MIPS32R5,
7361da2c 1460 OPTION_MIPS32R6,
c31f3936 1461 OPTION_MIPS64R2,
ae52f483
AB
1462 OPTION_MIPS64R3,
1463 OPTION_MIPS64R5,
7361da2c 1464 OPTION_MIPS64R6,
c31f3936
RS
1465 OPTION_MIPS16,
1466 OPTION_NO_MIPS16,
1467 OPTION_MIPS3D,
1468 OPTION_NO_MIPS3D,
1469 OPTION_MDMX,
1470 OPTION_NO_MDMX,
1471 OPTION_DSP,
1472 OPTION_NO_DSP,
1473 OPTION_MT,
1474 OPTION_NO_MT,
1475 OPTION_VIRT,
1476 OPTION_NO_VIRT,
56d438b1
CF
1477 OPTION_MSA,
1478 OPTION_NO_MSA,
c31f3936
RS
1479 OPTION_SMARTMIPS,
1480 OPTION_NO_SMARTMIPS,
1481 OPTION_DSPR2,
1482 OPTION_NO_DSPR2,
8f4f9071
MF
1483 OPTION_DSPR3,
1484 OPTION_NO_DSPR3,
c31f3936
RS
1485 OPTION_EVA,
1486 OPTION_NO_EVA,
7d64c587
AB
1487 OPTION_XPA,
1488 OPTION_NO_XPA,
c31f3936
RS
1489 OPTION_MICROMIPS,
1490 OPTION_NO_MICROMIPS,
1491 OPTION_MCU,
1492 OPTION_NO_MCU,
25499ac7
MR
1493 OPTION_MIPS16E2,
1494 OPTION_NO_MIPS16E2,
730c3174
SE
1495 OPTION_CRC,
1496 OPTION_NO_CRC,
c31f3936
RS
1497 OPTION_M4650,
1498 OPTION_NO_M4650,
1499 OPTION_M4010,
1500 OPTION_NO_M4010,
1501 OPTION_M4100,
1502 OPTION_NO_M4100,
1503 OPTION_M3900,
1504 OPTION_NO_M3900,
1505 OPTION_M7000_HILO_FIX,
1506 OPTION_MNO_7000_HILO_FIX,
1507 OPTION_FIX_24K,
1508 OPTION_NO_FIX_24K,
a8d14a88
CM
1509 OPTION_FIX_RM7000,
1510 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1511 OPTION_FIX_LOONGSON3_LLSC,
1512 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1513 OPTION_FIX_LOONGSON2F_JUMP,
1514 OPTION_NO_FIX_LOONGSON2F_JUMP,
1515 OPTION_FIX_LOONGSON2F_NOP,
1516 OPTION_NO_FIX_LOONGSON2F_NOP,
1517 OPTION_FIX_VR4120,
1518 OPTION_NO_FIX_VR4120,
1519 OPTION_FIX_VR4130,
1520 OPTION_NO_FIX_VR4130,
1521 OPTION_FIX_CN63XXP1,
1522 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1523 OPTION_FIX_R5900,
1524 OPTION_NO_FIX_R5900,
c31f3936
RS
1525 OPTION_TRAP,
1526 OPTION_BREAK,
1527 OPTION_EB,
1528 OPTION_EL,
1529 OPTION_FP32,
1530 OPTION_GP32,
1531 OPTION_CONSTRUCT_FLOATS,
1532 OPTION_NO_CONSTRUCT_FLOATS,
1533 OPTION_FP64,
351cdf24 1534 OPTION_FPXX,
c31f3936
RS
1535 OPTION_GP64,
1536 OPTION_RELAX_BRANCH,
1537 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1538 OPTION_IGNORE_BRANCH_ISA,
1539 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1540 OPTION_INSN32,
1541 OPTION_NO_INSN32,
c31f3936
RS
1542 OPTION_MSHARED,
1543 OPTION_MNO_SHARED,
1544 OPTION_MSYM32,
1545 OPTION_MNO_SYM32,
1546 OPTION_SOFT_FLOAT,
1547 OPTION_HARD_FLOAT,
1548 OPTION_SINGLE_FLOAT,
1549 OPTION_DOUBLE_FLOAT,
1550 OPTION_32,
c31f3936
RS
1551 OPTION_CALL_SHARED,
1552 OPTION_CALL_NONPIC,
1553 OPTION_NON_SHARED,
1554 OPTION_XGOT,
1555 OPTION_MABI,
1556 OPTION_N32,
1557 OPTION_64,
1558 OPTION_MDEBUG,
1559 OPTION_NO_MDEBUG,
1560 OPTION_PDR,
1561 OPTION_NO_PDR,
1562 OPTION_MVXWORKS_PIC,
ba92f887 1563 OPTION_NAN,
351cdf24
MF
1564 OPTION_ODD_SPREG,
1565 OPTION_NO_ODD_SPREG,
6f20c942
FS
1566 OPTION_GINV,
1567 OPTION_NO_GINV,
8095d2f7
CX
1568 OPTION_LOONGSON_MMI,
1569 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1570 OPTION_LOONGSON_CAM,
1571 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1572 OPTION_LOONGSON_EXT,
1573 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1574 OPTION_LOONGSON_EXT2,
1575 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1576 OPTION_END_OF_ENUM
1577 };
1578
1579struct option md_longopts[] =
1580{
1581 /* Options which specify architecture. */
1582 {"march", required_argument, NULL, OPTION_MARCH},
1583 {"mtune", required_argument, NULL, OPTION_MTUNE},
1584 {"mips0", no_argument, NULL, OPTION_MIPS1},
1585 {"mips1", no_argument, NULL, OPTION_MIPS1},
1586 {"mips2", no_argument, NULL, OPTION_MIPS2},
1587 {"mips3", no_argument, NULL, OPTION_MIPS3},
1588 {"mips4", no_argument, NULL, OPTION_MIPS4},
1589 {"mips5", no_argument, NULL, OPTION_MIPS5},
1590 {"mips32", no_argument, NULL, OPTION_MIPS32},
1591 {"mips64", no_argument, NULL, OPTION_MIPS64},
1592 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1593 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1594 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1595 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1596 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1597 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1598 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1599 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1600
1601 /* Options which specify Application Specific Extensions (ASEs). */
1602 {"mips16", no_argument, NULL, OPTION_MIPS16},
1603 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1604 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1605 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1606 {"mdmx", no_argument, NULL, OPTION_MDMX},
1607 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1608 {"mdsp", no_argument, NULL, OPTION_DSP},
1609 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1610 {"mmt", no_argument, NULL, OPTION_MT},
1611 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1612 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1613 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1614 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1615 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1616 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1617 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1618 {"meva", no_argument, NULL, OPTION_EVA},
1619 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1620 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1621 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1622 {"mmcu", no_argument, NULL, OPTION_MCU},
1623 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1624 {"mvirt", no_argument, NULL, OPTION_VIRT},
1625 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1626 {"mmsa", no_argument, NULL, OPTION_MSA},
1627 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1628 {"mxpa", no_argument, NULL, OPTION_XPA},
1629 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1630 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1631 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1632 {"mcrc", no_argument, NULL, OPTION_CRC},
1633 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1634 {"mginv", no_argument, NULL, OPTION_GINV},
1635 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1636 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1637 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1638 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1639 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1640 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1641 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1642 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1643 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1644
1645 /* Old-style architecture options. Don't add more of these. */
1646 {"m4650", no_argument, NULL, OPTION_M4650},
1647 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1648 {"m4010", no_argument, NULL, OPTION_M4010},
1649 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1650 {"m4100", no_argument, NULL, OPTION_M4100},
1651 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1652 {"m3900", no_argument, NULL, OPTION_M3900},
1653 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1654
1655 /* Options which enable bug fixes. */
1656 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1657 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1658 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1659 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1660 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1661 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1662 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1663 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1664 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1665 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1666 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1667 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1668 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1669 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1670 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1671 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1672 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1673 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1674 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1675 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1676 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1677
1678 /* Miscellaneous options. */
1679 {"trap", no_argument, NULL, OPTION_TRAP},
1680 {"no-break", no_argument, NULL, OPTION_TRAP},
1681 {"break", no_argument, NULL, OPTION_BREAK},
1682 {"no-trap", no_argument, NULL, OPTION_BREAK},
1683 {"EB", no_argument, NULL, OPTION_EB},
1684 {"EL", no_argument, NULL, OPTION_EL},
1685 {"mfp32", no_argument, NULL, OPTION_FP32},
1686 {"mgp32", no_argument, NULL, OPTION_GP32},
1687 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1688 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1689 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1690 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1691 {"mgp64", no_argument, NULL, OPTION_GP64},
1692 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1693 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1694 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1695 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1696 {"minsn32", no_argument, NULL, OPTION_INSN32},
1697 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1698 {"mshared", no_argument, NULL, OPTION_MSHARED},
1699 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1700 {"msym32", no_argument, NULL, OPTION_MSYM32},
1701 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1702 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1703 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1704 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1705 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1706 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1707 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1708
1709 /* Strictly speaking this next option is ELF specific,
1710 but we allow it for other ports as well in order to
1711 make testing easier. */
1712 {"32", no_argument, NULL, OPTION_32},
1713
1714 /* ELF-specific options. */
c31f3936
RS
1715 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1716 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1717 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1718 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1719 {"xgot", no_argument, NULL, OPTION_XGOT},
1720 {"mabi", required_argument, NULL, OPTION_MABI},
1721 {"n32", no_argument, NULL, OPTION_N32},
1722 {"64", no_argument, NULL, OPTION_64},
1723 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1724 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1725 {"mpdr", no_argument, NULL, OPTION_PDR},
1726 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1727 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1728 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1729
1730 {NULL, no_argument, NULL, 0}
1731};
1732size_t md_longopts_size = sizeof (md_longopts);
1733\f
c6278170
RS
1734/* Information about either an Application Specific Extension or an
1735 optional architecture feature that, for simplicity, we treat in the
1736 same way as an ASE. */
1737struct mips_ase
1738{
1739 /* The name of the ASE, used in both the command-line and .set options. */
1740 const char *name;
1741
1742 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1743 and 64-bit architectures, the flags here refer to the subset that
1744 is available on both. */
1745 unsigned int flags;
1746
1747 /* The ASE_* flag used for instructions that are available on 64-bit
1748 architectures but that are not included in FLAGS. */
1749 unsigned int flags64;
1750
1751 /* The command-line options that turn the ASE on and off. */
1752 int option_on;
1753 int option_off;
1754
1755 /* The minimum required architecture revisions for MIPS32, MIPS64,
1756 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1757 int mips32_rev;
1758 int mips64_rev;
1759 int micromips32_rev;
1760 int micromips64_rev;
7361da2c
AB
1761
1762 /* The architecture where the ASE was removed or -1 if the extension has not
1763 been removed. */
1764 int rem_rev;
c6278170
RS
1765};
1766
1767/* A table of all supported ASEs. */
1768static const struct mips_ase mips_ases[] = {
1769 { "dsp", ASE_DSP, ASE_DSP64,
1770 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1771 2, 2, 2, 2,
1772 -1 },
c6278170
RS
1773
1774 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1775 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1776 2, 2, 2, 2,
1777 -1 },
c6278170 1778
8f4f9071
MF
1779 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1780 OPTION_DSPR3, OPTION_NO_DSPR3,
1781 6, 6, -1, -1,
1782 -1 },
1783
c6278170
RS
1784 { "eva", ASE_EVA, 0,
1785 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1786 2, 2, 2, 2,
1787 -1 },
c6278170
RS
1788
1789 { "mcu", ASE_MCU, 0,
1790 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1791 2, 2, 2, 2,
1792 -1 },
c6278170
RS
1793
1794 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1795 { "mdmx", ASE_MDMX, 0,
1796 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1797 -1, 1, -1, -1,
1798 6 },
c6278170
RS
1799
1800 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1801 { "mips3d", ASE_MIPS3D, 0,
1802 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1803 2, 1, -1, -1,
1804 6 },
c6278170
RS
1805
1806 { "mt", ASE_MT, 0,
1807 OPTION_MT, OPTION_NO_MT,
0c5c669c 1808 2, 2, -1, -1,
7361da2c 1809 -1 },
c6278170
RS
1810
1811 { "smartmips", ASE_SMARTMIPS, 0,
1812 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1813 1, -1, -1, -1,
1814 6 },
c6278170
RS
1815
1816 { "virt", ASE_VIRT, ASE_VIRT64,
1817 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1818 2, 2, 2, 2,
1819 -1 },
56d438b1
CF
1820
1821 { "msa", ASE_MSA, ASE_MSA64,
1822 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1823 2, 2, 2, 2,
1824 -1 },
7d64c587
AB
1825
1826 { "xpa", ASE_XPA, 0,
1827 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1828 2, 2, 2, 2,
7361da2c 1829 -1 },
25499ac7
MR
1830
1831 { "mips16e2", ASE_MIPS16E2, 0,
1832 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1833 2, 2, -1, -1,
1834 6 },
730c3174
SE
1835
1836 { "crc", ASE_CRC, ASE_CRC64,
1837 OPTION_CRC, OPTION_NO_CRC,
1838 6, 6, -1, -1,
1839 -1 },
6f20c942
FS
1840
1841 { "ginv", ASE_GINV, 0,
1842 OPTION_GINV, OPTION_NO_GINV,
1843 6, 6, 6, 6,
1844 -1 },
8095d2f7
CX
1845
1846 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1847 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1848 0, 0, -1, -1,
1849 -1 },
716c08de
CX
1850
1851 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1852 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1853 0, 0, -1, -1,
1854 -1 },
bdc6c06e
CX
1855
1856 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1857 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1858 0, 0, -1, -1,
1859 -1 },
a693765e
CX
1860
1861 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1862 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1863 0, 0, -1, -1,
1864 -1 },
c6278170
RS
1865};
1866
1867/* The set of ASEs that require -mfp64. */
82bda27b 1868#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1869
1870/* Groups of ASE_* flags that represent different revisions of an ASE. */
1871static const unsigned int mips_ase_groups[] = {
a693765e
CX
1872 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1873 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1874};
1875\f
252b5132
RH
1876/* Pseudo-op table.
1877
1878 The following pseudo-ops from the Kane and Heinrich MIPS book
1879 should be defined here, but are currently unsupported: .alias,
1880 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1881
1882 The following pseudo-ops from the Kane and Heinrich MIPS book are
1883 specific to the type of debugging information being generated, and
1884 should be defined by the object format: .aent, .begin, .bend,
1885 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1886 .vreg.
1887
1888 The following pseudo-ops from the Kane and Heinrich MIPS book are
1889 not MIPS CPU specific, but are also not specific to the object file
1890 format. This file is probably the best place to define them, but
d84bcf09 1891 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1892
e972090a
NC
1893static const pseudo_typeS mips_pseudo_table[] =
1894{
beae10d5 1895 /* MIPS specific pseudo-ops. */
252b5132
RH
1896 {"option", s_option, 0},
1897 {"set", s_mipsset, 0},
1898 {"rdata", s_change_sec, 'r'},
1899 {"sdata", s_change_sec, 's'},
1900 {"livereg", s_ignore, 0},
1901 {"abicalls", s_abicalls, 0},
1902 {"cpload", s_cpload, 0},
6478892d
TS
1903 {"cpsetup", s_cpsetup, 0},
1904 {"cplocal", s_cplocal, 0},
252b5132 1905 {"cprestore", s_cprestore, 0},
6478892d 1906 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1907 {"dtprelword", s_dtprelword, 0},
1908 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1909 {"tprelword", s_tprelword, 0},
1910 {"tpreldword", s_tpreldword, 0},
6478892d 1911 {"gpvalue", s_gpvalue, 0},
252b5132 1912 {"gpword", s_gpword, 0},
10181a0d 1913 {"gpdword", s_gpdword, 0},
a3f278e2 1914 {"ehword", s_ehword, 0},
252b5132
RH
1915 {"cpadd", s_cpadd, 0},
1916 {"insn", s_insn, 0},
ba92f887 1917 {"nan", s_nan, 0},
919731af 1918 {"module", s_module, 0},
252b5132 1919
beae10d5 1920 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1921 chips. */
38a57ae7 1922 {"asciiz", stringer, 8 + 1},
252b5132
RH
1923 {"bss", s_change_sec, 'b'},
1924 {"err", s_err, 0},
1925 {"half", s_cons, 1},
1926 {"dword", s_cons, 3},
1927 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1928 {"origin", s_org, 0},
1929 {"repeat", s_rept, 0},
252b5132 1930
998b3c36
MR
1931 /* For MIPS this is non-standard, but we define it for consistency. */
1932 {"sbss", s_change_sec, 'B'},
1933
beae10d5 1934 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1935 here for one reason or another. */
1936 {"align", s_align, 0},
1937 {"byte", s_cons, 0},
1938 {"data", s_change_sec, 'd'},
1939 {"double", s_float_cons, 'd'},
1940 {"float", s_float_cons, 'f'},
1941 {"globl", s_mips_globl, 0},
1942 {"global", s_mips_globl, 0},
1943 {"hword", s_cons, 1},
1944 {"int", s_cons, 2},
1945 {"long", s_cons, 2},
1946 {"octa", s_cons, 4},
1947 {"quad", s_cons, 3},
cca86cc8 1948 {"section", s_change_section, 0},
252b5132
RH
1949 {"short", s_cons, 1},
1950 {"single", s_float_cons, 'f'},
754e2bb9 1951 {"stabd", s_mips_stab, 'd'},
252b5132 1952 {"stabn", s_mips_stab, 'n'},
754e2bb9 1953 {"stabs", s_mips_stab, 's'},
252b5132
RH
1954 {"text", s_change_sec, 't'},
1955 {"word", s_cons, 2},
add56521 1956
add56521 1957 { "extern", ecoff_directive_extern, 0},
add56521 1958
43841e91 1959 { NULL, NULL, 0 },
252b5132
RH
1960};
1961
e972090a
NC
1962static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1963{
beae10d5
KH
1964 /* These pseudo-ops should be defined by the object file format.
1965 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1966 {"aent", s_mips_ent, 1},
1967 {"bgnb", s_ignore, 0},
1968 {"end", s_mips_end, 0},
1969 {"endb", s_ignore, 0},
1970 {"ent", s_mips_ent, 0},
c5dd6aab 1971 {"file", s_mips_file, 0},
252b5132
RH
1972 {"fmask", s_mips_mask, 'F'},
1973 {"frame", s_mips_frame, 0},
c5dd6aab 1974 {"loc", s_mips_loc, 0},
252b5132
RH
1975 {"mask", s_mips_mask, 'R'},
1976 {"verstamp", s_ignore, 0},
43841e91 1977 { NULL, NULL, 0 },
252b5132
RH
1978};
1979
3ae8dd8d
MR
1980/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1981 purpose of the `.dc.a' internal pseudo-op. */
1982
1983int
1984mips_address_bytes (void)
1985{
919731af 1986 file_mips_check_options ();
3ae8dd8d
MR
1987 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1988}
1989
17a2f251 1990extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1991
1992void
17a2f251 1993mips_pop_insert (void)
252b5132
RH
1994{
1995 pop_insert (mips_pseudo_table);
1996 if (! ECOFF_DEBUGGING)
1997 pop_insert (mips_nonecoff_pseudo_table);
1998}
1999\f
2000/* Symbols labelling the current insn. */
2001
e972090a
NC
2002struct insn_label_list
2003{
252b5132
RH
2004 struct insn_label_list *next;
2005 symbolS *label;
2006};
2007
252b5132 2008static struct insn_label_list *free_insn_labels;
742a56fe 2009#define label_list tc_segment_info_data.labels
252b5132 2010
17a2f251 2011static void mips_clear_insn_labels (void);
df58fc94
RS
2012static void mips_mark_labels (void);
2013static void mips_compressed_mark_labels (void);
252b5132
RH
2014
2015static inline void
17a2f251 2016mips_clear_insn_labels (void)
252b5132 2017{
ed9e98c2 2018 struct insn_label_list **pl;
a8dbcb85 2019 segment_info_type *si;
252b5132 2020
a8dbcb85
TS
2021 if (now_seg)
2022 {
2023 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2024 ;
3739860c 2025
a8dbcb85
TS
2026 si = seg_info (now_seg);
2027 *pl = si->label_list;
2028 si->label_list = NULL;
2029 }
252b5132 2030}
a8dbcb85 2031
df58fc94
RS
2032/* Mark instruction labels in MIPS16/microMIPS mode. */
2033
2034static inline void
2035mips_mark_labels (void)
2036{
2037 if (HAVE_CODE_COMPRESSION)
2038 mips_compressed_mark_labels ();
2039}
252b5132 2040\f
6eb099ae 2041static char *expr_parse_end;
252b5132 2042
e423441d 2043/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2044 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2045
2046static expressionS imm_expr;
252b5132 2047
77bd4346
RS
2048/* The relocatable field in an instruction and the relocs associated
2049 with it. These variables are used for instructions like LUI and
2050 JAL as well as true offsets. They are also used for address
2051 operands in macros. */
252b5132 2052
77bd4346 2053static expressionS offset_expr;
f6688943
TS
2054static bfd_reloc_code_real_type offset_reloc[3]
2055 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2056
df58fc94
RS
2057/* This is set to the resulting size of the instruction to be produced
2058 by mips16_ip if an explicit extension is used or by mips_ip if an
2059 explicit size is supplied. */
252b5132 2060
df58fc94 2061static unsigned int forced_insn_length;
252b5132 2062
e1b47bd5
RS
2063/* True if we are assembling an instruction. All dot symbols defined during
2064 this time should be treated as code labels. */
2065
5b7c81bd 2066static bool mips_assembling_insn;
e1b47bd5 2067
ecb4347a
DJ
2068/* The pdr segment for per procedure frame/regmask info. Not used for
2069 ECOFF debugging. */
252b5132
RH
2070
2071static segT pdr_seg;
252b5132 2072
e013f690
TS
2073/* The default target format to use. */
2074
aeffff67
RS
2075#if defined (TE_FreeBSD)
2076#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2077#elif defined (TE_TMIPS)
2078#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2079#else
2080#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2081#endif
2082
e013f690 2083const char *
17a2f251 2084mips_target_format (void)
e013f690
TS
2085{
2086 switch (OUTPUT_FLAVOR)
2087 {
e013f690 2088 case bfd_target_elf_flavour:
0a44bf69
RS
2089#ifdef TE_VXWORKS
2090 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2091 return (target_big_endian
2092 ? "elf32-bigmips-vxworks"
2093 : "elf32-littlemips-vxworks");
2094#endif
e013f690 2095 return (target_big_endian
cfe86eaa 2096 ? (HAVE_64BIT_OBJECTS
aeffff67 2097 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2098 : (HAVE_NEWABI
aeffff67
RS
2099 ? ELF_TARGET ("elf32-n", "big")
2100 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2101 : (HAVE_64BIT_OBJECTS
aeffff67 2102 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2103 : (HAVE_NEWABI
aeffff67
RS
2104 ? ELF_TARGET ("elf32-n", "little")
2105 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2106 default:
2107 abort ();
2108 return NULL;
2109 }
2110}
2111
c6278170
RS
2112/* Return the ISA revision that is currently in use, or 0 if we are
2113 generating code for MIPS V or below. */
2114
2115static int
2116mips_isa_rev (void)
2117{
2118 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2119 return 2;
2120
ae52f483
AB
2121 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2122 return 3;
2123
2124 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2125 return 5;
2126
7361da2c
AB
2127 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2128 return 6;
2129
c6278170
RS
2130 /* microMIPS implies revision 2 or above. */
2131 if (mips_opts.micromips)
2132 return 2;
2133
2134 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2135 return 1;
2136
2137 return 0;
2138}
2139
2140/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2141
2142static unsigned int
2143mips_ase_mask (unsigned int flags)
2144{
2145 unsigned int i;
2146
2147 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2148 if (flags & mips_ase_groups[i])
2149 flags |= mips_ase_groups[i];
2150 return flags;
2151}
2152
2153/* Check whether the current ISA supports ASE. Issue a warning if
2154 appropriate. */
2155
2156static void
2157mips_check_isa_supports_ase (const struct mips_ase *ase)
2158{
2159 const char *base;
2160 int min_rev, size;
2161 static unsigned int warned_isa;
2162 static unsigned int warned_fp32;
2163
2164 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2165 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2166 else
2167 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2168 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2169 && (warned_isa & ase->flags) != ase->flags)
2170 {
2171 warned_isa |= ase->flags;
2172 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2173 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2174 if (min_rev < 0)
1661c76c 2175 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2176 " `%s' extension"), size, base, ase->name);
2177 else
1661c76c 2178 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2179 ase->name, base, size, min_rev);
2180 }
7361da2c
AB
2181 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2182 && (warned_isa & ase->flags) != ase->flags)
2183 {
2184 warned_isa |= ase->flags;
2185 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2186 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2187 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2188 ase->name, base, size, ase->rem_rev);
2189 }
2190
c6278170 2191 if ((ase->flags & FP64_ASES)
0b35dfee 2192 && mips_opts.fp != 64
c6278170
RS
2193 && (warned_fp32 & ase->flags) != ase->flags)
2194 {
2195 warned_fp32 |= ase->flags;
1661c76c 2196 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2197 }
2198}
2199
2200/* Check all enabled ASEs to see whether they are supported by the
2201 chosen architecture. */
2202
2203static void
2204mips_check_isa_supports_ases (void)
2205{
2206 unsigned int i, mask;
2207
2208 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2209 {
2210 mask = mips_ase_mask (mips_ases[i].flags);
2211 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2212 mips_check_isa_supports_ase (&mips_ases[i]);
2213 }
2214}
2215
2216/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2217 that were affected. */
2218
2219static unsigned int
919731af 2220mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
5b7c81bd 2221 bool enabled_p)
c6278170
RS
2222{
2223 unsigned int mask;
2224
2225 mask = mips_ase_mask (ase->flags);
919731af 2226 opts->ase &= ~mask;
92cebb3d
MR
2227
2228 /* Clear combination ASE flags, which need to be recalculated based on
2229 updated regular ASE settings. */
41cee089 2230 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
92cebb3d 2231
c6278170 2232 if (enabled_p)
919731af 2233 opts->ase |= ase->flags;
25499ac7 2234
9785fc2a
MR
2235 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2236 instructions which are only valid when both ASEs are enabled.
2237 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2238 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2239 {
2240 opts->ase |= ASE_XPA_VIRT;
2241 mask |= ASE_XPA_VIRT;
2242 }
25499ac7
MR
2243 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2244 {
2245 opts->ase |= ASE_MIPS16E2_MT;
2246 mask |= ASE_MIPS16E2_MT;
2247 }
2248
41cee089
FS
2249 /* The EVA Extension has instructions which are only valid when the R6 ISA
2250 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2251 present. */
2252 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2253 {
2254 opts->ase |= ASE_EVA_R6;
2255 mask |= ASE_EVA_R6;
2256 }
2257
c6278170
RS
2258 return mask;
2259}
2260
2261/* Return the ASE called NAME, or null if none. */
2262
2263static const struct mips_ase *
2264mips_lookup_ase (const char *name)
2265{
2266 unsigned int i;
2267
2268 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2269 if (strcmp (name, mips_ases[i].name) == 0)
2270 return &mips_ases[i];
2271 return NULL;
2272}
2273
df58fc94 2274/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2275 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2276 otherwise it is a 32-bit instruction. */
df58fc94
RS
2277
2278static inline unsigned int
2279micromips_insn_length (const struct mips_opcode *mo)
2280{
7fd53920 2281 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2282}
2283
5c04167a
RS
2284/* Return the length of MIPS16 instruction OPCODE. */
2285
2286static inline unsigned int
2287mips16_opcode_length (unsigned long opcode)
2288{
2289 return (opcode >> 16) == 0 ? 2 : 4;
2290}
2291
1e915849
RS
2292/* Return the length of instruction INSN. */
2293
2294static inline unsigned int
2295insn_length (const struct mips_cl_insn *insn)
2296{
df58fc94
RS
2297 if (mips_opts.micromips)
2298 return micromips_insn_length (insn->insn_mo);
2299 else if (mips_opts.mips16)
5c04167a 2300 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2301 else
1e915849 2302 return 4;
1e915849
RS
2303}
2304
2305/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2306
2307static void
2308create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2309{
2310 size_t i;
2311
2312 insn->insn_mo = mo;
1e915849
RS
2313 insn->insn_opcode = mo->match;
2314 insn->frag = NULL;
2315 insn->where = 0;
2316 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2317 insn->fixp[i] = NULL;
2318 insn->fixed_p = (mips_opts.noreorder > 0);
2319 insn->noreorder_p = (mips_opts.noreorder > 0);
2320 insn->mips16_absolute_jump_p = 0;
15be625d 2321 insn->complete_p = 0;
e407c74b 2322 insn->cleared_p = 0;
1e915849
RS
2323}
2324
fc76e730
RS
2325/* Get a list of all the operands in INSN. */
2326
2327static const struct mips_operand_array *
2328insn_operands (const struct mips_cl_insn *insn)
2329{
2330 if (insn->insn_mo >= &mips_opcodes[0]
2331 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2332 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2333
2334 if (insn->insn_mo >= &mips16_opcodes[0]
2335 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2336 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2337
2338 if (insn->insn_mo >= &micromips_opcodes[0]
2339 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2340 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2341
2342 abort ();
2343}
2344
2345/* Get a description of operand OPNO of INSN. */
2346
2347static const struct mips_operand *
2348insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2349{
2350 const struct mips_operand_array *operands;
2351
2352 operands = insn_operands (insn);
2353 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2354 abort ();
2355 return operands->operand[opno];
2356}
2357
e077a1c8
RS
2358/* Install UVAL as the value of OPERAND in INSN. */
2359
2360static inline void
2361insn_insert_operand (struct mips_cl_insn *insn,
2362 const struct mips_operand *operand, unsigned int uval)
2363{
25499ac7
MR
2364 if (mips_opts.mips16
2365 && operand->type == OP_INT && operand->lsb == 0
2366 && mips_opcode_32bit_p (insn->insn_mo))
2367 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2368 else
2369 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2370}
2371
fc76e730
RS
2372/* Extract the value of OPERAND from INSN. */
2373
2374static inline unsigned
2375insn_extract_operand (const struct mips_cl_insn *insn,
2376 const struct mips_operand *operand)
2377{
2378 return mips_extract_operand (operand, insn->insn_opcode);
2379}
2380
df58fc94 2381/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2382
2383static void
df58fc94 2384mips_record_compressed_mode (void)
742a56fe
RS
2385{
2386 segment_info_type *si;
2387
2388 si = seg_info (now_seg);
2389 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2390 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2391 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2392 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2393}
2394
4d68580a
RS
2395/* Read a standard MIPS instruction from BUF. */
2396
2397static unsigned long
2398read_insn (char *buf)
2399{
2400 if (target_big_endian)
2401 return bfd_getb32 ((bfd_byte *) buf);
2402 else
2403 return bfd_getl32 ((bfd_byte *) buf);
2404}
2405
2406/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2407 the next byte. */
2408
2409static char *
2410write_insn (char *buf, unsigned int insn)
2411{
2412 md_number_to_chars (buf, insn, 4);
2413 return buf + 4;
2414}
2415
2416/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2417 has length LENGTH. */
2418
2419static unsigned long
2420read_compressed_insn (char *buf, unsigned int length)
2421{
2422 unsigned long insn;
2423 unsigned int i;
2424
2425 insn = 0;
2426 for (i = 0; i < length; i += 2)
2427 {
2428 insn <<= 16;
2429 if (target_big_endian)
2430 insn |= bfd_getb16 ((char *) buf);
2431 else
2432 insn |= bfd_getl16 ((char *) buf);
2433 buf += 2;
2434 }
2435 return insn;
2436}
2437
5c04167a
RS
2438/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2439 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2440
2441static char *
2442write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2443{
2444 unsigned int i;
2445
2446 for (i = 0; i < length; i += 2)
2447 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2448 return buf + length;
2449}
2450
1e915849
RS
2451/* Install INSN at the location specified by its "frag" and "where" fields. */
2452
2453static void
2454install_insn (const struct mips_cl_insn *insn)
2455{
2456 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2457 if (HAVE_CODE_COMPRESSION)
2458 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2459 else
4d68580a 2460 write_insn (f, insn->insn_opcode);
df58fc94 2461 mips_record_compressed_mode ();
1e915849
RS
2462}
2463
2464/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2465 and install the opcode in the new location. */
2466
2467static void
2468move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2469{
2470 size_t i;
2471
2472 insn->frag = frag;
2473 insn->where = where;
2474 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2475 if (insn->fixp[i] != NULL)
2476 {
2477 insn->fixp[i]->fx_frag = frag;
2478 insn->fixp[i]->fx_where = where;
2479 }
2480 install_insn (insn);
2481}
2482
2483/* Add INSN to the end of the output. */
2484
2485static void
2486add_fixed_insn (struct mips_cl_insn *insn)
2487{
2488 char *f = frag_more (insn_length (insn));
2489 move_insn (insn, frag_now, f - frag_now->fr_literal);
2490}
2491
2492/* Start a variant frag and move INSN to the start of the variant part,
2493 marking it as fixed. The other arguments are as for frag_var. */
2494
2495static void
2496add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2497 relax_substateT subtype, symbolS *symbol, offsetT offset)
2498{
2499 frag_grow (max_chars);
2500 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2501 insn->fixed_p = 1;
2502 frag_var (rs_machine_dependent, max_chars, var,
2503 subtype, symbol, offset, NULL);
2504}
2505
2506/* Insert N copies of INSN into the history buffer, starting at
2507 position FIRST. Neither FIRST nor N need to be clipped. */
2508
2509static void
2510insert_into_history (unsigned int first, unsigned int n,
2511 const struct mips_cl_insn *insn)
2512{
2513 if (mips_relax.sequence != 2)
2514 {
2515 unsigned int i;
2516
2517 for (i = ARRAY_SIZE (history); i-- > first;)
2518 if (i >= first + n)
2519 history[i] = history[i - n];
2520 else
2521 history[i] = *insn;
2522 }
2523}
2524
e3de51ce
RS
2525/* Clear the error in insn_error. */
2526
2527static void
2528clear_insn_error (void)
2529{
2530 memset (&insn_error, 0, sizeof (insn_error));
2531}
2532
2533/* Possibly record error message MSG for the current instruction.
2534 If the error is about a particular argument, ARGNUM is the 1-based
2535 number of that argument, otherwise it is 0. FORMAT is the format
2536 of MSG. Return true if MSG was used, false if the current message
2537 was kept. */
2538
5b7c81bd 2539static bool
e3de51ce
RS
2540set_insn_error_format (int argnum, enum mips_insn_error_format format,
2541 const char *msg)
2542{
2543 if (argnum == 0)
2544 {
2545 /* Give priority to errors against specific arguments, and to
2546 the first whole-instruction message. */
2547 if (insn_error.msg)
5b7c81bd 2548 return false;
e3de51ce
RS
2549 }
2550 else
2551 {
2552 /* Keep insn_error if it is against a later argument. */
2553 if (argnum < insn_error.min_argnum)
5b7c81bd 2554 return false;
e3de51ce
RS
2555
2556 /* If both errors are against the same argument but are different,
2557 give up on reporting a specific error for this argument.
2558 See the comment about mips_insn_error for details. */
2559 if (argnum == insn_error.min_argnum
2560 && insn_error.msg
2561 && strcmp (insn_error.msg, msg) != 0)
2562 {
2563 insn_error.msg = 0;
2564 insn_error.min_argnum += 1;
5b7c81bd 2565 return false;
e3de51ce
RS
2566 }
2567 }
2568 insn_error.min_argnum = argnum;
2569 insn_error.format = format;
2570 insn_error.msg = msg;
5b7c81bd 2571 return true;
e3de51ce
RS
2572}
2573
2574/* Record an instruction error with no % format fields. ARGNUM and MSG are
2575 as for set_insn_error_format. */
2576
2577static void
2578set_insn_error (int argnum, const char *msg)
2579{
2580 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2581}
2582
2583/* Record an instruction error with one %d field I. ARGNUM and MSG are
2584 as for set_insn_error_format. */
2585
2586static void
2587set_insn_error_i (int argnum, const char *msg, int i)
2588{
2589 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2590 insn_error.u.i = i;
2591}
2592
2593/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2594 are as for set_insn_error_format. */
2595
2596static void
2597set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2598{
2599 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2600 {
2601 insn_error.u.ss[0] = s1;
2602 insn_error.u.ss[1] = s2;
2603 }
2604}
2605
2606/* Report the error in insn_error, which is against assembly code STR. */
2607
2608static void
2609report_insn_error (const char *str)
2610{
e1fa0163 2611 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2612
e3de51ce
RS
2613 switch (insn_error.format)
2614 {
2615 case ERR_FMT_PLAIN:
2616 as_bad (msg, str);
2617 break;
2618
2619 case ERR_FMT_I:
2620 as_bad (msg, insn_error.u.i, str);
2621 break;
2622
2623 case ERR_FMT_SS:
2624 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2625 break;
2626 }
e1fa0163
NC
2627
2628 free ((char *) msg);
e3de51ce
RS
2629}
2630
71400594
RS
2631/* Initialize vr4120_conflicts. There is a bit of duplication here:
2632 the idea is to make it obvious at a glance that each errata is
2633 included. */
2634
2635static void
2636init_vr4120_conflicts (void)
2637{
2638#define CONFLICT(FIRST, SECOND) \
2639 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2640
2641 /* Errata 21 - [D]DIV[U] after [D]MACC */
2642 CONFLICT (MACC, DIV);
2643 CONFLICT (DMACC, DIV);
2644
2645 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2646 CONFLICT (DMULT, DMULT);
2647 CONFLICT (DMULT, DMACC);
2648 CONFLICT (DMACC, DMULT);
2649 CONFLICT (DMACC, DMACC);
2650
2651 /* Errata 24 - MT{LO,HI} after [D]MACC */
2652 CONFLICT (MACC, MTHILO);
2653 CONFLICT (DMACC, MTHILO);
2654
2655 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2656 instruction is executed immediately after a MACC or DMACC
2657 instruction, the result of [either instruction] is incorrect." */
2658 CONFLICT (MACC, MULT);
2659 CONFLICT (MACC, DMULT);
2660 CONFLICT (DMACC, MULT);
2661 CONFLICT (DMACC, DMULT);
2662
2663 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2664 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2665 DDIV or DDIVU instruction, the result of the MACC or
2666 DMACC instruction is incorrect.". */
2667 CONFLICT (DMULT, MACC);
2668 CONFLICT (DMULT, DMACC);
2669 CONFLICT (DIV, MACC);
2670 CONFLICT (DIV, DMACC);
2671
2672#undef CONFLICT
2673}
2674
707bfff6
TS
2675struct regname {
2676 const char *name;
2677 unsigned int num;
2678};
2679
14daeee3 2680#define RNUM_MASK 0x00000ff
56d438b1 2681#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2682#define RTYPE_NUM 0x0000100
2683#define RTYPE_FPU 0x0000200
2684#define RTYPE_FCC 0x0000400
2685#define RTYPE_VEC 0x0000800
2686#define RTYPE_GP 0x0001000
2687#define RTYPE_CP0 0x0002000
2688#define RTYPE_PC 0x0004000
2689#define RTYPE_ACC 0x0008000
2690#define RTYPE_CCC 0x0010000
2691#define RTYPE_VI 0x0020000
2692#define RTYPE_VF 0x0040000
2693#define RTYPE_R5900_I 0x0080000
2694#define RTYPE_R5900_Q 0x0100000
2695#define RTYPE_R5900_R 0x0200000
2696#define RTYPE_R5900_ACC 0x0400000
56d438b1 2697#define RTYPE_MSA 0x0800000
14daeee3 2698#define RWARN 0x8000000
707bfff6
TS
2699
2700#define GENERIC_REGISTER_NUMBERS \
2701 {"$0", RTYPE_NUM | 0}, \
2702 {"$1", RTYPE_NUM | 1}, \
2703 {"$2", RTYPE_NUM | 2}, \
2704 {"$3", RTYPE_NUM | 3}, \
2705 {"$4", RTYPE_NUM | 4}, \
2706 {"$5", RTYPE_NUM | 5}, \
2707 {"$6", RTYPE_NUM | 6}, \
2708 {"$7", RTYPE_NUM | 7}, \
2709 {"$8", RTYPE_NUM | 8}, \
2710 {"$9", RTYPE_NUM | 9}, \
2711 {"$10", RTYPE_NUM | 10}, \
2712 {"$11", RTYPE_NUM | 11}, \
2713 {"$12", RTYPE_NUM | 12}, \
2714 {"$13", RTYPE_NUM | 13}, \
2715 {"$14", RTYPE_NUM | 14}, \
2716 {"$15", RTYPE_NUM | 15}, \
2717 {"$16", RTYPE_NUM | 16}, \
2718 {"$17", RTYPE_NUM | 17}, \
2719 {"$18", RTYPE_NUM | 18}, \
2720 {"$19", RTYPE_NUM | 19}, \
2721 {"$20", RTYPE_NUM | 20}, \
2722 {"$21", RTYPE_NUM | 21}, \
2723 {"$22", RTYPE_NUM | 22}, \
2724 {"$23", RTYPE_NUM | 23}, \
2725 {"$24", RTYPE_NUM | 24}, \
2726 {"$25", RTYPE_NUM | 25}, \
2727 {"$26", RTYPE_NUM | 26}, \
2728 {"$27", RTYPE_NUM | 27}, \
2729 {"$28", RTYPE_NUM | 28}, \
2730 {"$29", RTYPE_NUM | 29}, \
2731 {"$30", RTYPE_NUM | 30}, \
3739860c 2732 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2733
2734#define FPU_REGISTER_NAMES \
2735 {"$f0", RTYPE_FPU | 0}, \
2736 {"$f1", RTYPE_FPU | 1}, \
2737 {"$f2", RTYPE_FPU | 2}, \
2738 {"$f3", RTYPE_FPU | 3}, \
2739 {"$f4", RTYPE_FPU | 4}, \
2740 {"$f5", RTYPE_FPU | 5}, \
2741 {"$f6", RTYPE_FPU | 6}, \
2742 {"$f7", RTYPE_FPU | 7}, \
2743 {"$f8", RTYPE_FPU | 8}, \
2744 {"$f9", RTYPE_FPU | 9}, \
2745 {"$f10", RTYPE_FPU | 10}, \
2746 {"$f11", RTYPE_FPU | 11}, \
2747 {"$f12", RTYPE_FPU | 12}, \
2748 {"$f13", RTYPE_FPU | 13}, \
2749 {"$f14", RTYPE_FPU | 14}, \
2750 {"$f15", RTYPE_FPU | 15}, \
2751 {"$f16", RTYPE_FPU | 16}, \
2752 {"$f17", RTYPE_FPU | 17}, \
2753 {"$f18", RTYPE_FPU | 18}, \
2754 {"$f19", RTYPE_FPU | 19}, \
2755 {"$f20", RTYPE_FPU | 20}, \
2756 {"$f21", RTYPE_FPU | 21}, \
2757 {"$f22", RTYPE_FPU | 22}, \
2758 {"$f23", RTYPE_FPU | 23}, \
2759 {"$f24", RTYPE_FPU | 24}, \
2760 {"$f25", RTYPE_FPU | 25}, \
2761 {"$f26", RTYPE_FPU | 26}, \
2762 {"$f27", RTYPE_FPU | 27}, \
2763 {"$f28", RTYPE_FPU | 28}, \
2764 {"$f29", RTYPE_FPU | 29}, \
2765 {"$f30", RTYPE_FPU | 30}, \
2766 {"$f31", RTYPE_FPU | 31}
2767
2768#define FPU_CONDITION_CODE_NAMES \
2769 {"$fcc0", RTYPE_FCC | 0}, \
2770 {"$fcc1", RTYPE_FCC | 1}, \
2771 {"$fcc2", RTYPE_FCC | 2}, \
2772 {"$fcc3", RTYPE_FCC | 3}, \
2773 {"$fcc4", RTYPE_FCC | 4}, \
2774 {"$fcc5", RTYPE_FCC | 5}, \
2775 {"$fcc6", RTYPE_FCC | 6}, \
2776 {"$fcc7", RTYPE_FCC | 7}
2777
2778#define COPROC_CONDITION_CODE_NAMES \
2779 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2780 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2781 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2782 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2783 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2784 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2785 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2786 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2787
2788#define N32N64_SYMBOLIC_REGISTER_NAMES \
2789 {"$a4", RTYPE_GP | 8}, \
2790 {"$a5", RTYPE_GP | 9}, \
2791 {"$a6", RTYPE_GP | 10}, \
2792 {"$a7", RTYPE_GP | 11}, \
2793 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2794 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2795 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2796 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2797 {"$t0", RTYPE_GP | 12}, \
2798 {"$t1", RTYPE_GP | 13}, \
2799 {"$t2", RTYPE_GP | 14}, \
2800 {"$t3", RTYPE_GP | 15}
2801
2802#define O32_SYMBOLIC_REGISTER_NAMES \
2803 {"$t0", RTYPE_GP | 8}, \
2804 {"$t1", RTYPE_GP | 9}, \
2805 {"$t2", RTYPE_GP | 10}, \
2806 {"$t3", RTYPE_GP | 11}, \
2807 {"$t4", RTYPE_GP | 12}, \
2808 {"$t5", RTYPE_GP | 13}, \
2809 {"$t6", RTYPE_GP | 14}, \
2810 {"$t7", RTYPE_GP | 15}, \
2811 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2812 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2813 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2814 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2815
6f2117ba 2816/* Remaining symbolic register names. */
707bfff6
TS
2817#define SYMBOLIC_REGISTER_NAMES \
2818 {"$zero", RTYPE_GP | 0}, \
2819 {"$at", RTYPE_GP | 1}, \
2820 {"$AT", RTYPE_GP | 1}, \
2821 {"$v0", RTYPE_GP | 2}, \
2822 {"$v1", RTYPE_GP | 3}, \
2823 {"$a0", RTYPE_GP | 4}, \
2824 {"$a1", RTYPE_GP | 5}, \
2825 {"$a2", RTYPE_GP | 6}, \
2826 {"$a3", RTYPE_GP | 7}, \
2827 {"$s0", RTYPE_GP | 16}, \
2828 {"$s1", RTYPE_GP | 17}, \
2829 {"$s2", RTYPE_GP | 18}, \
2830 {"$s3", RTYPE_GP | 19}, \
2831 {"$s4", RTYPE_GP | 20}, \
2832 {"$s5", RTYPE_GP | 21}, \
2833 {"$s6", RTYPE_GP | 22}, \
2834 {"$s7", RTYPE_GP | 23}, \
2835 {"$t8", RTYPE_GP | 24}, \
2836 {"$t9", RTYPE_GP | 25}, \
2837 {"$k0", RTYPE_GP | 26}, \
2838 {"$kt0", RTYPE_GP | 26}, \
2839 {"$k1", RTYPE_GP | 27}, \
2840 {"$kt1", RTYPE_GP | 27}, \
2841 {"$gp", RTYPE_GP | 28}, \
2842 {"$sp", RTYPE_GP | 29}, \
2843 {"$s8", RTYPE_GP | 30}, \
2844 {"$fp", RTYPE_GP | 30}, \
2845 {"$ra", RTYPE_GP | 31}
2846
2847#define MIPS16_SPECIAL_REGISTER_NAMES \
2848 {"$pc", RTYPE_PC | 0}
2849
2850#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2851 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2852 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2853 {"$v2", RTYPE_VEC | 2}, \
2854 {"$v3", RTYPE_VEC | 3}, \
2855 {"$v4", RTYPE_VEC | 4}, \
2856 {"$v5", RTYPE_VEC | 5}, \
2857 {"$v6", RTYPE_VEC | 6}, \
2858 {"$v7", RTYPE_VEC | 7}, \
2859 {"$v8", RTYPE_VEC | 8}, \
2860 {"$v9", RTYPE_VEC | 9}, \
2861 {"$v10", RTYPE_VEC | 10}, \
2862 {"$v11", RTYPE_VEC | 11}, \
2863 {"$v12", RTYPE_VEC | 12}, \
2864 {"$v13", RTYPE_VEC | 13}, \
2865 {"$v14", RTYPE_VEC | 14}, \
2866 {"$v15", RTYPE_VEC | 15}, \
2867 {"$v16", RTYPE_VEC | 16}, \
2868 {"$v17", RTYPE_VEC | 17}, \
2869 {"$v18", RTYPE_VEC | 18}, \
2870 {"$v19", RTYPE_VEC | 19}, \
2871 {"$v20", RTYPE_VEC | 20}, \
2872 {"$v21", RTYPE_VEC | 21}, \
2873 {"$v22", RTYPE_VEC | 22}, \
2874 {"$v23", RTYPE_VEC | 23}, \
2875 {"$v24", RTYPE_VEC | 24}, \
2876 {"$v25", RTYPE_VEC | 25}, \
2877 {"$v26", RTYPE_VEC | 26}, \
2878 {"$v27", RTYPE_VEC | 27}, \
2879 {"$v28", RTYPE_VEC | 28}, \
2880 {"$v29", RTYPE_VEC | 29}, \
2881 {"$v30", RTYPE_VEC | 30}, \
2882 {"$v31", RTYPE_VEC | 31}
2883
14daeee3
RS
2884#define R5900_I_NAMES \
2885 {"$I", RTYPE_R5900_I | 0}
2886
2887#define R5900_Q_NAMES \
2888 {"$Q", RTYPE_R5900_Q | 0}
2889
2890#define R5900_R_NAMES \
2891 {"$R", RTYPE_R5900_R | 0}
2892
2893#define R5900_ACC_NAMES \
2894 {"$ACC", RTYPE_R5900_ACC | 0 }
2895
707bfff6
TS
2896#define MIPS_DSP_ACCUMULATOR_NAMES \
2897 {"$ac0", RTYPE_ACC | 0}, \
2898 {"$ac1", RTYPE_ACC | 1}, \
2899 {"$ac2", RTYPE_ACC | 2}, \
2900 {"$ac3", RTYPE_ACC | 3}
2901
2902static const struct regname reg_names[] = {
2903 GENERIC_REGISTER_NUMBERS,
2904 FPU_REGISTER_NAMES,
2905 FPU_CONDITION_CODE_NAMES,
2906 COPROC_CONDITION_CODE_NAMES,
2907
2908 /* The $txx registers depends on the abi,
2909 these will be added later into the symbol table from
3739860c 2910 one of the tables below once mips_abi is set after
707bfff6
TS
2911 parsing of arguments from the command line. */
2912 SYMBOLIC_REGISTER_NAMES,
2913
2914 MIPS16_SPECIAL_REGISTER_NAMES,
2915 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2916 R5900_I_NAMES,
2917 R5900_Q_NAMES,
2918 R5900_R_NAMES,
2919 R5900_ACC_NAMES,
707bfff6
TS
2920 MIPS_DSP_ACCUMULATOR_NAMES,
2921 {0, 0}
2922};
2923
2924static const struct regname reg_names_o32[] = {
2925 O32_SYMBOLIC_REGISTER_NAMES,
2926 {0, 0}
2927};
2928
2929static const struct regname reg_names_n32n64[] = {
2930 N32N64_SYMBOLIC_REGISTER_NAMES,
2931 {0, 0}
2932};
2933
a92713e6
RS
2934/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2935 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2936 of these register symbols, return the associated vector register,
2937 otherwise return SYMVAL itself. */
df58fc94 2938
a92713e6
RS
2939static unsigned int
2940mips_prefer_vec_regno (unsigned int symval)
707bfff6 2941{
a92713e6
RS
2942 if ((symval & -2) == (RTYPE_GP | 2))
2943 return RTYPE_VEC | (symval & 1);
2944 return symval;
2945}
2946
14daeee3
RS
2947/* Return true if string [S, E) is a valid register name, storing its
2948 symbol value in *SYMVAL_PTR if so. */
a92713e6 2949
5b7c81bd 2950static bool
14daeee3 2951mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2952{
707bfff6 2953 char save_c;
14daeee3 2954 symbolS *symbol;
707bfff6
TS
2955
2956 /* Terminate name. */
2957 save_c = *e;
2958 *e = '\0';
2959
a92713e6
RS
2960 /* Look up the name. */
2961 symbol = symbol_find (s);
2962 *e = save_c;
2963
2964 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
5b7c81bd 2965 return false;
a92713e6 2966
14daeee3 2967 *symval_ptr = S_GET_VALUE (symbol);
5b7c81bd 2968 return true;
14daeee3
RS
2969}
2970
2971/* Return true if the string at *SPTR is a valid register name. Allow it
2972 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2973 is nonnull.
2974
2975 When returning true, move *SPTR past the register, store the
2976 register's symbol value in *SYMVAL_PTR and the channel mask in
2977 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2978 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2979 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2980
5b7c81bd 2981static bool
14daeee3
RS
2982mips_parse_register (char **sptr, unsigned int *symval_ptr,
2983 unsigned int *channels_ptr)
2984{
2985 char *s, *e, *m;
2986 const char *q;
2987 unsigned int channels, symval, bit;
2988
2989 /* Find end of name. */
2990 s = e = *sptr;
2991 if (is_name_beginner (*e))
2992 ++e;
2993 while (is_part_of_name (*e))
2994 ++e;
2995
2996 channels = 0;
2997 if (!mips_parse_register_1 (s, e, &symval))
2998 {
2999 if (!channels_ptr)
5b7c81bd 3000 return false;
14daeee3
RS
3001
3002 /* Eat characters from the end of the string that are valid
3003 channel suffixes. The preceding register must be $ACC or
3004 end with a digit, so there is no ambiguity. */
3005 bit = 1;
3006 m = e;
3007 for (q = "wzyx"; *q; q++, bit <<= 1)
3008 if (m > s && m[-1] == *q)
3009 {
3010 --m;
3011 channels |= bit;
3012 }
3013
3014 if (channels == 0
3015 || !mips_parse_register_1 (s, m, &symval)
3016 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
5b7c81bd 3017 return false;
14daeee3
RS
3018 }
3019
a92713e6 3020 *sptr = e;
14daeee3
RS
3021 *symval_ptr = symval;
3022 if (channels_ptr)
3023 *channels_ptr = channels;
5b7c81bd 3024 return true;
a92713e6
RS
3025}
3026
3027/* Check if SPTR points at a valid register specifier according to TYPES.
3028 If so, then return 1, advance S to consume the specifier and store
3029 the register's number in REGNOP, otherwise return 0. */
3030
3031static int
3032reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3033{
3034 unsigned int regno;
3035
14daeee3 3036 if (mips_parse_register (s, &regno, NULL))
707bfff6 3037 {
a92713e6
RS
3038 if (types & RTYPE_VEC)
3039 regno = mips_prefer_vec_regno (regno);
3040 if (regno & types)
3041 regno &= RNUM_MASK;
3042 else
3043 regno = ~0;
707bfff6 3044 }
a92713e6 3045 else
707bfff6 3046 {
a92713e6 3047 if (types & RWARN)
1661c76c 3048 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3049 regno = ~0;
707bfff6 3050 }
707bfff6 3051 if (regnop)
a92713e6
RS
3052 *regnop = regno;
3053 return regno <= RNUM_MASK;
707bfff6
TS
3054}
3055
14daeee3
RS
3056/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3057 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3058
3059static char *
3060mips_parse_vu0_channels (char *s, unsigned int *channels)
3061{
3062 unsigned int i;
3063
3064 *channels = 0;
3065 for (i = 0; i < 4; i++)
3066 if (*s == "xyzw"[i])
3067 {
3068 *channels |= 1 << (3 - i);
3069 ++s;
3070 }
3071 return s;
3072}
3073
a92713e6
RS
3074/* Token types for parsed operand lists. */
3075enum mips_operand_token_type {
3076 /* A plain register, e.g. $f2. */
3077 OT_REG,
df58fc94 3078
14daeee3
RS
3079 /* A 4-bit XYZW channel mask. */
3080 OT_CHANNELS,
3081
56d438b1
CF
3082 /* A constant vector index, e.g. [1]. */
3083 OT_INTEGER_INDEX,
3084
3085 /* A register vector index, e.g. [$2]. */
3086 OT_REG_INDEX,
df58fc94 3087
a92713e6
RS
3088 /* A continuous range of registers, e.g. $s0-$s4. */
3089 OT_REG_RANGE,
3090
3091 /* A (possibly relocated) expression. */
3092 OT_INTEGER,
3093
3094 /* A floating-point value. */
3095 OT_FLOAT,
3096
3097 /* A single character. This can be '(', ')' or ',', but '(' only appears
3098 before OT_REGs. */
3099 OT_CHAR,
3100
14daeee3
RS
3101 /* A doubled character, either "--" or "++". */
3102 OT_DOUBLE_CHAR,
3103
a92713e6
RS
3104 /* The end of the operand list. */
3105 OT_END
3106};
3107
3108/* A parsed operand token. */
3109struct mips_operand_token
3110{
3111 /* The type of token. */
3112 enum mips_operand_token_type type;
3113 union
3114 {
56d438b1 3115 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3116 unsigned int regno;
3117
14daeee3
RS
3118 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3119 unsigned int channels;
3120
56d438b1
CF
3121 /* The integer value of an OT_INTEGER_INDEX. */
3122 addressT index;
a92713e6
RS
3123
3124 /* The two register symbol values involved in an OT_REG_RANGE. */
3125 struct {
3126 unsigned int regno1;
3127 unsigned int regno2;
3128 } reg_range;
3129
3130 /* The value of an OT_INTEGER. The value is represented as an
3131 expression and the relocation operators that were applied to
3132 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3133 relocation operators were used. */
3134 struct {
3135 expressionS value;
3136 bfd_reloc_code_real_type relocs[3];
3137 } integer;
3138
3139 /* The binary data for an OT_FLOAT constant, and the number of bytes
3140 in the constant. */
3141 struct {
3142 unsigned char data[8];
3143 int length;
3144 } flt;
3145
14daeee3 3146 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3147 char ch;
3148 } u;
3149};
3150
3151/* An obstack used to construct lists of mips_operand_tokens. */
3152static struct obstack mips_operand_tokens;
3153
3154/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3155
3156static void
3157mips_add_token (struct mips_operand_token *token,
3158 enum mips_operand_token_type type)
3159{
3160 token->type = type;
3161 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3162}
3163
3164/* Check whether S is '(' followed by a register name. Add OT_CHAR
3165 and OT_REG tokens for them if so, and return a pointer to the first
3166 unconsumed character. Return null otherwise. */
3167
3168static char *
3169mips_parse_base_start (char *s)
3170{
3171 struct mips_operand_token token;
14daeee3 3172 unsigned int regno, channels;
5b7c81bd 3173 bool decrement_p;
df58fc94 3174
a92713e6
RS
3175 if (*s != '(')
3176 return 0;
3177
3178 ++s;
3179 SKIP_SPACE_TABS (s);
14daeee3
RS
3180
3181 /* Only match "--" as part of a base expression. In other contexts "--X"
3182 is a double negative. */
3183 decrement_p = (s[0] == '-' && s[1] == '-');
3184 if (decrement_p)
3185 {
3186 s += 2;
3187 SKIP_SPACE_TABS (s);
3188 }
3189
3190 /* Allow a channel specifier because that leads to better error messages
3191 than treating something like "$vf0x++" as an expression. */
3192 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3193 return 0;
3194
3195 token.u.ch = '(';
3196 mips_add_token (&token, OT_CHAR);
3197
14daeee3
RS
3198 if (decrement_p)
3199 {
3200 token.u.ch = '-';
3201 mips_add_token (&token, OT_DOUBLE_CHAR);
3202 }
3203
a92713e6
RS
3204 token.u.regno = regno;
3205 mips_add_token (&token, OT_REG);
3206
14daeee3
RS
3207 if (channels)
3208 {
3209 token.u.channels = channels;
3210 mips_add_token (&token, OT_CHANNELS);
3211 }
3212
3213 /* For consistency, only match "++" as part of base expressions too. */
3214 SKIP_SPACE_TABS (s);
3215 if (s[0] == '+' && s[1] == '+')
3216 {
3217 s += 2;
3218 token.u.ch = '+';
3219 mips_add_token (&token, OT_DOUBLE_CHAR);
3220 }
3221
a92713e6
RS
3222 return s;
3223}
3224
3225/* Parse one or more tokens from S. Return a pointer to the first
3226 unconsumed character on success. Return null if an error was found
3227 and store the error text in insn_error. FLOAT_FORMAT is as for
3228 mips_parse_arguments. */
3229
3230static char *
3231mips_parse_argument_token (char *s, char float_format)
3232{
6d4af3c2
AM
3233 char *end, *save_in;
3234 const char *err;
14daeee3 3235 unsigned int regno1, regno2, channels;
a92713e6
RS
3236 struct mips_operand_token token;
3237
3238 /* First look for "($reg", since we want to treat that as an
3239 OT_CHAR and OT_REG rather than an expression. */
3240 end = mips_parse_base_start (s);
3241 if (end)
3242 return end;
3243
3244 /* Handle other characters that end up as OT_CHARs. */
3245 if (*s == ')' || *s == ',')
3246 {
3247 token.u.ch = *s;
3248 mips_add_token (&token, OT_CHAR);
3249 ++s;
3250 return s;
3251 }
3252
3253 /* Handle tokens that start with a register. */
14daeee3 3254 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3255 {
14daeee3
RS
3256 if (channels)
3257 {
3258 /* A register and a VU0 channel suffix. */
3259 token.u.regno = regno1;
3260 mips_add_token (&token, OT_REG);
3261
3262 token.u.channels = channels;
3263 mips_add_token (&token, OT_CHANNELS);
3264 return s;
3265 }
3266
a92713e6
RS
3267 SKIP_SPACE_TABS (s);
3268 if (*s == '-')
df58fc94 3269 {
a92713e6
RS
3270 /* A register range. */
3271 ++s;
3272 SKIP_SPACE_TABS (s);
14daeee3 3273 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3274 {
1661c76c 3275 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3276 return 0;
3277 }
df58fc94 3278
a92713e6
RS
3279 token.u.reg_range.regno1 = regno1;
3280 token.u.reg_range.regno2 = regno2;
3281 mips_add_token (&token, OT_REG_RANGE);
3282 return s;
3283 }
a92713e6 3284
56d438b1
CF
3285 /* Add the register itself. */
3286 token.u.regno = regno1;
3287 mips_add_token (&token, OT_REG);
3288
3289 /* Check for a vector index. */
3290 if (*s == '[')
3291 {
a92713e6
RS
3292 ++s;
3293 SKIP_SPACE_TABS (s);
56d438b1
CF
3294 if (mips_parse_register (&s, &token.u.regno, NULL))
3295 mips_add_token (&token, OT_REG_INDEX);
3296 else
a92713e6 3297 {
56d438b1
CF
3298 expressionS element;
3299
3300 my_getExpression (&element, s);
3301 if (element.X_op != O_constant)
3302 {
3303 set_insn_error (0, _("vector element must be constant"));
3304 return 0;
3305 }
6eb099ae 3306 s = expr_parse_end;
56d438b1
CF
3307 token.u.index = element.X_add_number;
3308 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3309 }
a92713e6
RS
3310 SKIP_SPACE_TABS (s);
3311 if (*s != ']')
3312 {
1661c76c 3313 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3314 return 0;
3315 }
3316 ++s;
df58fc94 3317 }
a92713e6 3318 return s;
df58fc94
RS
3319 }
3320
a92713e6
RS
3321 if (float_format)
3322 {
3323 /* First try to treat expressions as floats. */
3324 save_in = input_line_pointer;
3325 input_line_pointer = s;
3326 err = md_atof (float_format, (char *) token.u.flt.data,
3327 &token.u.flt.length);
3328 end = input_line_pointer;
3329 input_line_pointer = save_in;
3330 if (err && *err)
3331 {
e3de51ce 3332 set_insn_error (0, err);
a92713e6
RS
3333 return 0;
3334 }
3335 if (s != end)
3336 {
3337 mips_add_token (&token, OT_FLOAT);
3338 return end;
3339 }
3340 }
3341
3342 /* Treat everything else as an integer expression. */
3343 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3344 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3345 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3346 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
6eb099ae 3347 s = expr_parse_end;
a92713e6
RS
3348 mips_add_token (&token, OT_INTEGER);
3349 return s;
3350}
3351
3352/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3353 if expressions should be treated as 32-bit floating-point constants,
3354 'd' if they should be treated as 64-bit floating-point constants,
3355 or 0 if they should be treated as integer expressions (the usual case).
3356
3357 Return a list of tokens on success, otherwise return 0. The caller
3358 must obstack_free the list after use. */
3359
3360static struct mips_operand_token *
3361mips_parse_arguments (char *s, char float_format)
3362{
3363 struct mips_operand_token token;
3364
3365 SKIP_SPACE_TABS (s);
3366 while (*s)
3367 {
3368 s = mips_parse_argument_token (s, float_format);
3369 if (!s)
3370 {
3371 obstack_free (&mips_operand_tokens,
3372 obstack_finish (&mips_operand_tokens));
3373 return 0;
3374 }
3375 SKIP_SPACE_TABS (s);
3376 }
3377 mips_add_token (&token, OT_END);
3378 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3379}
3380
d301a56b
RS
3381/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3382 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9 3383
5b7c81bd 3384static bool
f79e2745 3385is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3386{
3387 int isa = mips_opts.isa;
846ef2d0 3388 int ase = mips_opts.ase;
037b32b9 3389 int fp_s, fp_d;
c6278170 3390 unsigned int i;
037b32b9 3391
be0fcbee 3392 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3393 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3394 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3395 ase |= mips_ases[i].flags64;
037b32b9 3396
d301a56b 3397 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
5b7c81bd 3398 return false;
037b32b9
AN
3399
3400 /* Check whether the instruction or macro requires single-precision or
3401 double-precision floating-point support. Note that this information is
3402 stored differently in the opcode table for insns and macros. */
3403 if (mo->pinfo == INSN_MACRO)
3404 {
3405 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3406 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3407 }
3408 else
3409 {
3410 fp_s = mo->pinfo & FP_S;
3411 fp_d = mo->pinfo & FP_D;
3412 }
3413
3414 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
5b7c81bd 3415 return false;
037b32b9
AN
3416
3417 if (fp_s && mips_opts.soft_float)
5b7c81bd 3418 return false;
037b32b9 3419
5b7c81bd 3420 return true;
037b32b9
AN
3421}
3422
3423/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3424 selected ISA and architecture. */
3425
5b7c81bd 3426static bool
037b32b9
AN
3427is_opcode_valid_16 (const struct mips_opcode *mo)
3428{
25499ac7
MR
3429 int isa = mips_opts.isa;
3430 int ase = mips_opts.ase;
3431 unsigned int i;
3432
3433 if (ISA_HAS_64BIT_REGS (isa))
3434 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3435 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3436 ase |= mips_ases[i].flags64;
3437
3438 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3439}
3440
df58fc94 3441/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3442 explicitly requested. Always TRUE in the standard MIPS mode.
3443 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94 3444
5b7c81bd 3445static bool
df58fc94
RS
3446is_size_valid (const struct mips_opcode *mo)
3447{
3448 if (!mips_opts.micromips)
5b7c81bd 3449 return true;
df58fc94 3450
833794fc
MR
3451 if (mips_opts.insn32)
3452 {
3453 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
5b7c81bd 3454 return false;
833794fc 3455 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
5b7c81bd 3456 return false;
833794fc 3457 }
df58fc94 3458 if (!forced_insn_length)
5b7c81bd 3459 return true;
df58fc94 3460 if (mo->pinfo == INSN_MACRO)
5b7c81bd 3461 return false;
df58fc94
RS
3462 return forced_insn_length == micromips_insn_length (mo);
3463}
3464
7fd53920
MR
3465/* Return TRUE if the size of the MIPS16 opcode MO matches one
3466 explicitly requested. */
3467
5b7c81bd 3468static bool
7fd53920
MR
3469is_size_valid_16 (const struct mips_opcode *mo)
3470{
3471 if (!forced_insn_length)
5b7c81bd 3472 return true;
7fd53920 3473 if (mo->pinfo == INSN_MACRO)
5b7c81bd 3474 return false;
7fd53920 3475 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
5b7c81bd 3476 return false;
0674ee5d 3477 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
5b7c81bd
AM
3478 return false;
3479 return true;
7fd53920
MR
3480}
3481
df58fc94 3482/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3483 of the preceding instruction. Always TRUE in the standard MIPS mode.
3484
3485 We don't accept macros in 16-bit delay slots to avoid a case where
3486 a macro expansion fails because it relies on a preceding 32-bit real
3487 instruction to have matched and does not handle the operands correctly.
3488 The only macros that may expand to 16-bit instructions are JAL that
3489 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3490 and BGT (that likewise cannot be placed in a delay slot) that decay to
3491 a NOP. In all these cases the macros precede any corresponding real
3492 instruction definitions in the opcode table, so they will match in the
3493 second pass where the size of the delay slot is ignored and therefore
3494 produce correct code. */
df58fc94 3495
5b7c81bd 3496static bool
df58fc94
RS
3497is_delay_slot_valid (const struct mips_opcode *mo)
3498{
3499 if (!mips_opts.micromips)
5b7c81bd 3500 return true;
df58fc94
RS
3501
3502 if (mo->pinfo == INSN_MACRO)
c06dec14 3503 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3504 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3505 && micromips_insn_length (mo) != 4)
5b7c81bd 3506 return false;
df58fc94
RS
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3508 && micromips_insn_length (mo) != 2)
5b7c81bd 3509 return false;
df58fc94 3510
5b7c81bd 3511 return true;
df58fc94
RS
3512}
3513
fc76e730
RS
3514/* For consistency checking, verify that all bits of OPCODE are specified
3515 either by the match/mask part of the instruction definition, or by the
3516 operand list. Also build up a list of operands in OPERANDS.
3517
3518 INSN_BITS says which bits of the instruction are significant.
3519 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3520 provides the mips_operand description of each operand. DECODE_OPERAND
3521 is null for MIPS16 instructions. */
ab902481
RS
3522
3523static int
3524validate_mips_insn (const struct mips_opcode *opcode,
3525 unsigned long insn_bits,
fc76e730
RS
3526 const struct mips_operand *(*decode_operand) (const char *),
3527 struct mips_operand_array *operands)
ab902481
RS
3528{
3529 const char *s;
fc76e730 3530 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3531 const struct mips_operand *operand;
3532
fc76e730
RS
3533 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3534 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3535 {
3536 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3537 opcode->name, opcode->args);
3538 return 0;
3539 }
3540 used_bits = 0;
fc76e730 3541 opno = 0;
14daeee3
RS
3542 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3543 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3544 for (s = opcode->args; *s; ++s)
3545 switch (*s)
3546 {
3547 case ',':
3548 case '(':
3549 case ')':
3550 break;
3551
14daeee3
RS
3552 case '#':
3553 s++;
3554 break;
3555
ab902481 3556 default:
fc76e730 3557 if (!decode_operand)
7fd53920 3558 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3559 else
3560 operand = decode_operand (s);
3561 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3562 {
3563 as_bad (_("internal: unknown operand type: %s %s"),
3564 opcode->name, opcode->args);
3565 return 0;
3566 }
fc76e730
RS
3567 gas_assert (opno < MAX_OPERANDS);
3568 operands->operand[opno] = operand;
25499ac7
MR
3569 if (!decode_operand && operand
3570 && operand->type == OP_INT && operand->lsb == 0
3571 && mips_opcode_32bit_p (opcode))
3572 used_bits |= mips16_immed_extend (-1, operand->size);
3573 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3574 {
14daeee3 3575 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3576 if (operand->type == OP_MDMX_IMM_REG)
3577 /* Bit 5 is the format selector (OB vs QH). The opcode table
3578 has separate entries for each format. */
3579 used_bits &= ~(1 << (operand->lsb + 5));
3580 if (operand->type == OP_ENTRY_EXIT_LIST)
3581 used_bits &= ~(mask & 0x700);
38bf472a
MR
3582 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3583 operand field that cannot be fully described with LSB/SIZE. */
3584 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3585 used_bits &= ~0x6000;
fc76e730 3586 }
ab902481 3587 /* Skip prefix characters. */
7361da2c 3588 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3589 ++s;
fc76e730 3590 opno += 1;
ab902481
RS
3591 break;
3592 }
fc76e730 3593 doubled = used_bits & mask & insn_bits;
ab902481
RS
3594 if (doubled)
3595 {
3596 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3597 " %s %s"), doubled, opcode->name, opcode->args);
3598 return 0;
3599 }
fc76e730 3600 used_bits |= mask;
ab902481 3601 undefined = ~used_bits & insn_bits;
fc76e730 3602 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3603 {
3604 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3605 undefined, opcode->name, opcode->args);
3606 return 0;
3607 }
3608 used_bits &= ~insn_bits;
3609 if (used_bits)
3610 {
3611 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3612 used_bits, opcode->name, opcode->args);
3613 return 0;
3614 }
3615 return 1;
3616}
3617
fc76e730
RS
3618/* The MIPS16 version of validate_mips_insn. */
3619
3620static int
3621validate_mips16_insn (const struct mips_opcode *opcode,
3622 struct mips_operand_array *operands)
3623{
7fd53920 3624 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3625
7fd53920 3626 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3627}
3628
ab902481
RS
3629/* The microMIPS version of validate_mips_insn. */
3630
3631static int
fc76e730
RS
3632validate_micromips_insn (const struct mips_opcode *opc,
3633 struct mips_operand_array *operands)
ab902481
RS
3634{
3635 unsigned long insn_bits;
3636 unsigned long major;
3637 unsigned int length;
3638
fc76e730
RS
3639 if (opc->pinfo == INSN_MACRO)
3640 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3641 operands);
3642
ab902481
RS
3643 length = micromips_insn_length (opc);
3644 if (length != 2 && length != 4)
3645 {
1661c76c 3646 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3647 "%s %s"), length, opc->name, opc->args);
3648 return 0;
3649 }
3650 major = opc->match >> (10 + 8 * (length - 2));
3651 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3652 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3653 {
1661c76c 3654 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3655 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3656 return 0;
3657 }
3658
3659 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3660 insn_bits = 1 << 4 * length;
3661 insn_bits <<= 4 * length;
3662 insn_bits -= 1;
fc76e730
RS
3663 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3664 operands);
ab902481
RS
3665}
3666
707bfff6
TS
3667/* This function is called once, at assembler startup time. It should set up
3668 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3669
252b5132 3670void
17a2f251 3671md_begin (void)
252b5132 3672{
156c2f8b 3673 int i = 0;
252b5132 3674 int broken = 0;
1f25f5d3 3675
0a44bf69
RS
3676 if (mips_pic != NO_PIC)
3677 {
3678 if (g_switch_seen && g_switch_value != 0)
3679 as_bad (_("-G may not be used in position-independent code"));
3680 g_switch_value = 0;
3681 }
00acd688
CM
3682 else if (mips_abicalls)
3683 {
3684 if (g_switch_seen && g_switch_value != 0)
3685 as_bad (_("-G may not be used with abicalls"));
3686 g_switch_value = 0;
3687 }
0a44bf69 3688
0b35dfee 3689 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3690 as_warn (_("could not set architecture and machine"));
252b5132 3691
629310ab 3692 op_hash = str_htab_create ();
252b5132 3693
fc76e730 3694 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3695 for (i = 0; i < NUMOPCODES;)
3696 {
3697 const char *name = mips_opcodes[i].name;
3698
fe0e921f
AM
3699 if (str_hash_insert (op_hash, name, &mips_opcodes[i], 0) != NULL)
3700 as_fatal (_("duplicate %s"), name);
252b5132
RH
3701 do
3702 {
fc76e730
RS
3703 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3704 decode_mips_operand, &mips_operands[i]))
3705 broken = 1;
6f2117ba 3706
fc76e730 3707 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3708 {
fc76e730
RS
3709 create_insn (&nop_insn, mips_opcodes + i);
3710 if (mips_fix_loongson2f_nop)
3711 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3712 nop_insn.fixed_p = 1;
252b5132 3713 }
6f2117ba
PH
3714
3715 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3716 create_insn (&sync_insn, mips_opcodes + i);
3717
252b5132
RH
3718 ++i;
3719 }
3720 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3721 }
3722
629310ab 3723 mips16_op_hash = str_htab_create ();
fc76e730
RS
3724 mips16_operands = XCNEWVEC (struct mips_operand_array,
3725 bfd_mips16_num_opcodes);
252b5132
RH
3726
3727 i = 0;
3728 while (i < bfd_mips16_num_opcodes)
3729 {
3730 const char *name = mips16_opcodes[i].name;
3731
fe0e921f
AM
3732 if (str_hash_insert (mips16_op_hash, name, &mips16_opcodes[i], 0))
3733 as_fatal (_("duplicate %s"), name);
252b5132
RH
3734 do
3735 {
fc76e730
RS
3736 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3737 broken = 1;
1e915849
RS
3738 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3739 {
3740 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3741 mips16_nop_insn.fixed_p = 1;
3742 }
252b5132
RH
3743 ++i;
3744 }
3745 while (i < bfd_mips16_num_opcodes
3746 && strcmp (mips16_opcodes[i].name, name) == 0);
3747 }
3748
629310ab 3749 micromips_op_hash = str_htab_create ();
fc76e730
RS
3750 micromips_operands = XCNEWVEC (struct mips_operand_array,
3751 bfd_micromips_num_opcodes);
df58fc94
RS
3752
3753 i = 0;
3754 while (i < bfd_micromips_num_opcodes)
3755 {
3756 const char *name = micromips_opcodes[i].name;
3757
fe0e921f
AM
3758 if (str_hash_insert (micromips_op_hash, name, &micromips_opcodes[i], 0))
3759 as_fatal (_("duplicate %s"), name);
df58fc94 3760 do
fc76e730
RS
3761 {
3762 struct mips_cl_insn *micromips_nop_insn;
3763
3764 if (!validate_micromips_insn (&micromips_opcodes[i],
3765 &micromips_operands[i]))
3766 broken = 1;
3767
3768 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3769 {
3770 if (micromips_insn_length (micromips_opcodes + i) == 2)
3771 micromips_nop_insn = &micromips_nop16_insn;
3772 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3773 micromips_nop_insn = &micromips_nop32_insn;
3774 else
3775 continue;
3776
3777 if (micromips_nop_insn->insn_mo == NULL
3778 && strcmp (name, "nop") == 0)
3779 {
3780 create_insn (micromips_nop_insn, micromips_opcodes + i);
3781 micromips_nop_insn->fixed_p = 1;
3782 }
3783 }
3784 }
df58fc94
RS
3785 while (++i < bfd_micromips_num_opcodes
3786 && strcmp (micromips_opcodes[i].name, name) == 0);
3787 }
3788
252b5132 3789 if (broken)
1661c76c 3790 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3791
3792 /* We add all the general register names to the symbol table. This
3793 helps us detect invalid uses of them. */
3739860c 3794 for (i = 0; reg_names[i].name; i++)
707bfff6 3795 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
e01e1cee
AM
3796 &zero_address_frag,
3797 reg_names[i].num));
707bfff6 3798 if (HAVE_NEWABI)
3739860c 3799 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3800 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
e01e1cee
AM
3801 &zero_address_frag,
3802 reg_names_n32n64[i].num));
707bfff6 3803 else
3739860c 3804 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3805 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
e01e1cee
AM
3806 &zero_address_frag,
3807 reg_names_o32[i].num));
6047c971 3808
14daeee3
RS
3809 for (i = 0; i < 32; i++)
3810 {
ca159256 3811 char regname[16];
14daeee3
RS
3812
3813 /* R5900 VU0 floating-point register. */
92fce9bd 3814 sprintf (regname, "$vf%d", i);
14daeee3 3815 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3816 &zero_address_frag, RTYPE_VF | i));
14daeee3
RS
3817
3818 /* R5900 VU0 integer register. */
92fce9bd 3819 sprintf (regname, "$vi%d", i);
14daeee3 3820 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3821 &zero_address_frag, RTYPE_VI | i));
14daeee3 3822
56d438b1 3823 /* MSA register. */
92fce9bd 3824 sprintf (regname, "$w%d", i);
56d438b1 3825 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3826 &zero_address_frag, RTYPE_MSA | i));
14daeee3
RS
3827 }
3828
a92713e6
RS
3829 obstack_init (&mips_operand_tokens);
3830
7d10b47d 3831 mips_no_prev_insn ();
252b5132
RH
3832
3833 mips_gprmask = 0;
3834 mips_cprmask[0] = 0;
3835 mips_cprmask[1] = 0;
3836 mips_cprmask[2] = 0;
3837 mips_cprmask[3] = 0;
3838
3839 /* set the default alignment for the text section (2**2) */
3840 record_alignment (text_section, 2);
3841
4d0d148d 3842 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3843
f3ded42a
RS
3844 /* On a native system other than VxWorks, sections must be aligned
3845 to 16 byte boundaries. When configured for an embedded ELF
3846 target, we don't bother. */
d34049e8
ML
3847 if (!startswith (TARGET_OS, "elf")
3848 && !startswith (TARGET_OS, "vxworks"))
252b5132 3849 {
fd361982
AM
3850 bfd_set_section_alignment (text_section, 4);
3851 bfd_set_section_alignment (data_section, 4);
3852 bfd_set_section_alignment (bss_section, 4);
f3ded42a 3853 }
252b5132 3854
f3ded42a
RS
3855 /* Create a .reginfo section for register masks and a .mdebug
3856 section for debugging information. */
3857 {
3858 segT seg;
3859 subsegT subseg;
3860 flagword flags;
3861 segT sec;
3862
3863 seg = now_seg;
3864 subseg = now_subseg;
3865
3866 /* The ABI says this section should be loaded so that the
3867 running program can access it. However, we don't load it
6f2117ba 3868 if we are configured for an embedded target. */
f3ded42a 3869 flags = SEC_READONLY | SEC_DATA;
d34049e8 3870 if (!startswith (TARGET_OS, "elf"))
f3ded42a
RS
3871 flags |= SEC_ALLOC | SEC_LOAD;
3872
3873 if (mips_abi != N64_ABI)
252b5132 3874 {
f3ded42a 3875 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3876
fd361982
AM
3877 bfd_set_section_flags (sec, flags);
3878 bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
252b5132 3879
f3ded42a
RS
3880 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3881 }
3882 else
3883 {
3884 /* The 64-bit ABI uses a .MIPS.options section rather than
3885 .reginfo section. */
3886 sec = subseg_new (".MIPS.options", (subsegT) 0);
fd361982
AM
3887 bfd_set_section_flags (sec, flags);
3888 bfd_set_section_alignment (sec, 3);
252b5132 3889
f3ded42a
RS
3890 /* Set up the option header. */
3891 {
3892 Elf_Internal_Options opthdr;
3893 char *f;
3894
3895 opthdr.kind = ODK_REGINFO;
3896 opthdr.size = (sizeof (Elf_External_Options)
3897 + sizeof (Elf64_External_RegInfo));
3898 opthdr.section = 0;
3899 opthdr.info = 0;
3900 f = frag_more (sizeof (Elf_External_Options));
3901 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3902 (Elf_External_Options *) f);
3903
3904 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3905 }
3906 }
252b5132 3907
351cdf24 3908 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
fd361982 3909 bfd_set_section_flags (sec,
351cdf24 3910 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
fd361982 3911 bfd_set_section_alignment (sec, 3);
351cdf24
MF
3912 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3913
f3ded42a
RS
3914 if (ECOFF_DEBUGGING)
3915 {
3916 sec = subseg_new (".mdebug", (subsegT) 0);
fd361982
AM
3917 bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3918 bfd_set_section_alignment (sec, 2);
252b5132 3919 }
f3ded42a
RS
3920 else if (mips_flag_pdr)
3921 {
3922 pdr_seg = subseg_new (".pdr", (subsegT) 0);
fd361982
AM
3923 bfd_set_section_flags (pdr_seg,
3924 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3925 bfd_set_section_alignment (pdr_seg, 2);
f3ded42a
RS
3926 }
3927
3928 subseg_set (seg, subseg);
3929 }
252b5132 3930
71400594
RS
3931 if (mips_fix_vr4120)
3932 init_vr4120_conflicts ();
252b5132
RH
3933}
3934
351cdf24
MF
3935static inline void
3936fpabi_incompatible_with (int fpabi, const char *what)
3937{
3938 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3939 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3940}
3941
3942static inline void
3943fpabi_requires (int fpabi, const char *what)
3944{
3945 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3946 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3947}
3948
3949/* Check -mabi and register sizes against the specified FP ABI. */
3950static void
3951check_fpabi (int fpabi)
3952{
351cdf24
MF
3953 switch (fpabi)
3954 {
3955 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3956 if (file_mips_opts.soft_float)
3957 fpabi_incompatible_with (fpabi, "softfloat");
3958 else if (file_mips_opts.single_float)
3959 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3960 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3961 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3962 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3963 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3964 break;
3965
3966 case Val_GNU_MIPS_ABI_FP_XX:
3967 if (mips_abi != O32_ABI)
3968 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3969 else if (file_mips_opts.soft_float)
3970 fpabi_incompatible_with (fpabi, "softfloat");
3971 else if (file_mips_opts.single_float)
3972 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3973 else if (file_mips_opts.fp != 0)
3974 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3975 break;
3976
3977 case Val_GNU_MIPS_ABI_FP_64A:
3978 case Val_GNU_MIPS_ABI_FP_64:
3979 if (mips_abi != O32_ABI)
3980 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3981 else if (file_mips_opts.soft_float)
3982 fpabi_incompatible_with (fpabi, "softfloat");
3983 else if (file_mips_opts.single_float)
3984 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3985 else if (file_mips_opts.fp != 64)
3986 fpabi_requires (fpabi, "fp=64");
3987 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3988 fpabi_incompatible_with (fpabi, "nooddspreg");
3989 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3990 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3991 break;
3992
3993 case Val_GNU_MIPS_ABI_FP_SINGLE:
3994 if (file_mips_opts.soft_float)
3995 fpabi_incompatible_with (fpabi, "softfloat");
3996 else if (!file_mips_opts.single_float)
3997 fpabi_requires (fpabi, "singlefloat");
3998 break;
3999
4000 case Val_GNU_MIPS_ABI_FP_SOFT:
4001 if (!file_mips_opts.soft_float)
4002 fpabi_requires (fpabi, "softfloat");
4003 break;
4004
4005 case Val_GNU_MIPS_ABI_FP_OLD_64:
4006 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4007 Tag_GNU_MIPS_ABI_FP, fpabi);
4008 break;
4009
3350cc01
CM
4010 case Val_GNU_MIPS_ABI_FP_NAN2008:
4011 /* Silently ignore compatibility value. */
4012 break;
4013
351cdf24
MF
4014 default:
4015 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4016 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4017 break;
4018 }
351cdf24
MF
4019}
4020
919731af 4021/* Perform consistency checks on the current options. */
4022
4023static void
5b7c81bd 4024mips_check_options (struct mips_set_options *opts, bool abi_checks)
919731af 4025{
4026 /* Check the size of integer registers agrees with the ABI and ISA. */
4027 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4028 as_bad (_("`gp=64' used with a 32-bit processor"));
4029 else if (abi_checks
4030 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4031 as_bad (_("`gp=32' used with a 64-bit ABI"));
4032 else if (abi_checks
4033 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4034 as_bad (_("`gp=64' used with a 32-bit ABI"));
4035
4036 /* Check the size of the float registers agrees with the ABI and ISA. */
4037 switch (opts->fp)
4038 {
351cdf24
MF
4039 case 0:
4040 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4041 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4042 else if (opts->single_float == 1)
4043 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4044 break;
919731af 4045 case 64:
4046 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4047 as_bad (_("`fp=64' used with a 32-bit fpu"));
4048 else if (abi_checks
4049 && ABI_NEEDS_32BIT_REGS (mips_abi)
4050 && !ISA_HAS_MXHC1 (opts->isa))
4051 as_warn (_("`fp=64' used with a 32-bit ABI"));
4052 break;
4053 case 32:
4054 if (abi_checks
4055 && ABI_NEEDS_64BIT_REGS (mips_abi))
4056 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4057 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4058 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4059 break;
4060 default:
4061 as_bad (_("Unknown size of floating point registers"));
4062 break;
4063 }
4064
351cdf24
MF
4065 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4066 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4067
919731af 4068 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4069 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4070 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4071 && (opts->micromips == 1
4072 || opts->mips16 == 1))
1357373c 4073 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4074 opts->micromips ? "micromips" : "mips16",
5f4678bb 4075 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4076
4077 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4078 as_fatal (_("branch relaxation is not supported in `%s'"),
4079 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4080}
4081
4082/* Perform consistency checks on the module level options exactly once.
4083 This is a deferred check that happens:
4084 at the first .set directive
4085 or, at the first pseudo op that generates code (inc .dc.a)
4086 or, at the first instruction
4087 or, at the end. */
4088
4089static void
4090file_mips_check_options (void)
4091{
919731af 4092 if (file_mips_opts_checked)
4093 return;
4094
4095 /* The following code determines the register size.
4096 Similar code was added to GCC 3.3 (see override_options() in
4097 config/mips/mips.c). The GAS and GCC code should be kept in sync
4098 as much as possible. */
4099
4100 if (file_mips_opts.gp < 0)
4101 {
4102 /* Infer the integer register size from the ABI and processor.
4103 Restrict ourselves to 32-bit registers if that's all the
4104 processor has, or if the ABI cannot handle 64-bit registers. */
4105 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4106 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4107 ? 32 : 64;
4108 }
4109
4110 if (file_mips_opts.fp < 0)
4111 {
4112 /* No user specified float register size.
4113 ??? GAS treats single-float processors as though they had 64-bit
4114 float registers (although it complains when double-precision
4115 instructions are used). As things stand, saying they have 32-bit
4116 registers would lead to spurious "register must be even" messages.
4117 So here we assume float registers are never smaller than the
4118 integer ones. */
4119 if (file_mips_opts.gp == 64)
4120 /* 64-bit integer registers implies 64-bit float registers. */
4121 file_mips_opts.fp = 64;
4122 else if ((file_mips_opts.ase & FP64_ASES)
4123 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4124 /* Handle ASEs that require 64-bit float registers, if possible. */
4125 file_mips_opts.fp = 64;
7361da2c
AB
4126 else if (ISA_IS_R6 (mips_opts.isa))
4127 /* R6 implies 64-bit float registers. */
4128 file_mips_opts.fp = 64;
919731af 4129 else
4130 /* 32-bit float registers. */
4131 file_mips_opts.fp = 32;
4132 }
4133
351cdf24
MF
4134 /* Disable operations on odd-numbered floating-point registers by default
4135 when using the FPXX ABI. */
4136 if (file_mips_opts.oddspreg < 0)
4137 {
4138 if (file_mips_opts.fp == 0)
4139 file_mips_opts.oddspreg = 0;
4140 else
4141 file_mips_opts.oddspreg = 1;
4142 }
4143
919731af 4144 /* End of GCC-shared inference code. */
4145
4146 /* This flag is set when we have a 64-bit capable CPU but use only
4147 32-bit wide registers. Note that EABI does not use it. */
4148 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4149 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4150 || mips_abi == O32_ABI))
4151 mips_32bitmode = 1;
4152
4153 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4154 as_bad (_("trap exception not supported at ISA 1"));
4155
4156 /* If the selected architecture includes support for ASEs, enable
4157 generation of code for them. */
4158 if (file_mips_opts.mips16 == -1)
4159 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4160 if (file_mips_opts.micromips == -1)
4161 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4162 ? 1 : 0;
4163
7361da2c
AB
4164 if (mips_nan2008 == -1)
4165 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4166 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4167 as_fatal (_("`%s' does not support legacy NaN"),
4168 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4169
919731af 4170 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4171 being selected implicitly. */
4172 if (file_mips_opts.fp != 64)
4173 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4174
4175 /* If the user didn't explicitly select or deselect a particular ASE,
4176 use the default setting for the CPU. */
3315614d 4177 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4178
4179 /* Set up the current options. These may change throughout assembly. */
4180 mips_opts = file_mips_opts;
4181
4182 mips_check_isa_supports_ases ();
5b7c81bd
AM
4183 mips_check_options (&file_mips_opts, true);
4184 file_mips_opts_checked = true;
919731af 4185
4186 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4187 as_warn (_("could not set architecture and machine"));
4188}
4189
252b5132 4190void
17a2f251 4191md_assemble (char *str)
252b5132
RH
4192{
4193 struct mips_cl_insn insn;
f6688943
TS
4194 bfd_reloc_code_real_type unused_reloc[3]
4195 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4196
919731af 4197 file_mips_check_options ();
4198
252b5132 4199 imm_expr.X_op = O_absent;
252b5132 4200 offset_expr.X_op = O_absent;
f6688943
TS
4201 offset_reloc[0] = BFD_RELOC_UNUSED;
4202 offset_reloc[1] = BFD_RELOC_UNUSED;
4203 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4204
e1b47bd5 4205 mips_mark_labels ();
5b7c81bd 4206 mips_assembling_insn = true;
e3de51ce 4207 clear_insn_error ();
e1b47bd5 4208
252b5132
RH
4209 if (mips_opts.mips16)
4210 mips16_ip (str, &insn);
4211 else
4212 {
4213 mips_ip (str, &insn);
beae10d5
KH
4214 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4215 str, insn.insn_opcode));
252b5132
RH
4216 }
4217
e3de51ce
RS
4218 if (insn_error.msg)
4219 report_insn_error (str);
e1b47bd5 4220 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4221 {
584892a6 4222 macro_start ();
252b5132
RH
4223 if (mips_opts.mips16)
4224 mips16_macro (&insn);
4225 else
833794fc 4226 macro (&insn, str);
584892a6 4227 macro_end ();
252b5132
RH
4228 }
4229 else
4230 {
77bd4346 4231 if (offset_expr.X_op != O_absent)
5b7c81bd 4232 append_insn (&insn, &offset_expr, offset_reloc, false);
252b5132 4233 else
5b7c81bd 4234 append_insn (&insn, NULL, unused_reloc, false);
252b5132 4235 }
e1b47bd5 4236
5b7c81bd 4237 mips_assembling_insn = false;
252b5132
RH
4238}
4239
738e5348
RS
4240/* Convenience functions for abstracting away the differences between
4241 MIPS16 and non-MIPS16 relocations. */
4242
5b7c81bd 4243static inline bool
738e5348
RS
4244mips16_reloc_p (bfd_reloc_code_real_type reloc)
4245{
4246 switch (reloc)
4247 {
4248 case BFD_RELOC_MIPS16_JMP:
4249 case BFD_RELOC_MIPS16_GPREL:
4250 case BFD_RELOC_MIPS16_GOT16:
4251 case BFD_RELOC_MIPS16_CALL16:
4252 case BFD_RELOC_MIPS16_HI16_S:
4253 case BFD_RELOC_MIPS16_HI16:
4254 case BFD_RELOC_MIPS16_LO16:
c9775dde 4255 case BFD_RELOC_MIPS16_16_PCREL_S1:
5b7c81bd 4256 return true;
738e5348
RS
4257
4258 default:
5b7c81bd 4259 return false;
738e5348
RS
4260 }
4261}
4262
5b7c81bd 4263static inline bool
df58fc94
RS
4264micromips_reloc_p (bfd_reloc_code_real_type reloc)
4265{
4266 switch (reloc)
4267 {
4268 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4269 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4270 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4271 case BFD_RELOC_MICROMIPS_GPREL16:
4272 case BFD_RELOC_MICROMIPS_JMP:
4273 case BFD_RELOC_MICROMIPS_HI16:
4274 case BFD_RELOC_MICROMIPS_HI16_S:
4275 case BFD_RELOC_MICROMIPS_LO16:
4276 case BFD_RELOC_MICROMIPS_LITERAL:
4277 case BFD_RELOC_MICROMIPS_GOT16:
4278 case BFD_RELOC_MICROMIPS_CALL16:
4279 case BFD_RELOC_MICROMIPS_GOT_HI16:
4280 case BFD_RELOC_MICROMIPS_GOT_LO16:
4281 case BFD_RELOC_MICROMIPS_CALL_HI16:
4282 case BFD_RELOC_MICROMIPS_CALL_LO16:
4283 case BFD_RELOC_MICROMIPS_SUB:
4284 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4285 case BFD_RELOC_MICROMIPS_GOT_OFST:
4286 case BFD_RELOC_MICROMIPS_GOT_DISP:
4287 case BFD_RELOC_MICROMIPS_HIGHEST:
4288 case BFD_RELOC_MICROMIPS_HIGHER:
4289 case BFD_RELOC_MICROMIPS_SCN_DISP:
4290 case BFD_RELOC_MICROMIPS_JALR:
5b7c81bd 4291 return true;
df58fc94
RS
4292
4293 default:
5b7c81bd 4294 return false;
df58fc94
RS
4295 }
4296}
4297
5b7c81bd 4298static inline bool
2309ddf2
MR
4299jmp_reloc_p (bfd_reloc_code_real_type reloc)
4300{
4301 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4302}
4303
5b7c81bd 4304static inline bool
0e9c5a5c
MR
4305b_reloc_p (bfd_reloc_code_real_type reloc)
4306{
4307 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4308 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4309 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4310 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4311 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4312 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4313 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4314}
4315
5b7c81bd 4316static inline bool
738e5348
RS
4317got16_reloc_p (bfd_reloc_code_real_type reloc)
4318{
2309ddf2 4319 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4320 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4321}
4322
5b7c81bd 4323static inline bool
738e5348
RS
4324hi16_reloc_p (bfd_reloc_code_real_type reloc)
4325{
2309ddf2 4326 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4327 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4328}
4329
5b7c81bd 4330static inline bool
738e5348
RS
4331lo16_reloc_p (bfd_reloc_code_real_type reloc)
4332{
2309ddf2 4333 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4334 || reloc == BFD_RELOC_MICROMIPS_LO16);
4335}
4336
5b7c81bd 4337static inline bool
df58fc94
RS
4338jalr_reloc_p (bfd_reloc_code_real_type reloc)
4339{
2309ddf2 4340 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4341}
4342
5b7c81bd 4343static inline bool
f2ae14a1
RS
4344gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4345{
4346 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4347 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4348}
4349
2de39019
CM
4350/* Return true if RELOC is a PC-relative relocation that does not have
4351 full address range. */
4352
5b7c81bd 4353static inline bool
2de39019
CM
4354limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4355{
4356 switch (reloc)
4357 {
4358 case BFD_RELOC_16_PCREL_S2:
c9775dde 4359 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4360 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4361 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4362 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4363 case BFD_RELOC_MIPS_21_PCREL_S2:
4364 case BFD_RELOC_MIPS_26_PCREL_S2:
4365 case BFD_RELOC_MIPS_18_PCREL_S3:
4366 case BFD_RELOC_MIPS_19_PCREL_S2:
5b7c81bd 4367 return true;
2de39019 4368
b47468a6 4369 case BFD_RELOC_32_PCREL:
7361da2c
AB
4370 case BFD_RELOC_HI16_S_PCREL:
4371 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4372 return HAVE_64BIT_ADDRESSES;
4373
2de39019 4374 default:
5b7c81bd 4375 return false;
2de39019
CM
4376 }
4377}
b47468a6 4378
5919d012 4379/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4380 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4381 need a matching %lo() when applied to local symbols. */
5919d012 4382
5b7c81bd 4383static inline bool
17a2f251 4384reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4385{
3b91255e 4386 return (HAVE_IN_PLACE_ADDENDS
738e5348 4387 && (hi16_reloc_p (reloc)
0a44bf69
RS
4388 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4389 all GOT16 relocations evaluate to "G". */
738e5348
RS
4390 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4391}
4392
4393/* Return the type of %lo() reloc needed by RELOC, given that
4394 reloc_needs_lo_p. */
4395
4396static inline bfd_reloc_code_real_type
4397matching_lo_reloc (bfd_reloc_code_real_type reloc)
4398{
df58fc94
RS
4399 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4400 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4401 : BFD_RELOC_LO16));
5919d012
RS
4402}
4403
4404/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4405 relocation. */
4406
5b7c81bd 4407static inline bool
17a2f251 4408fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4409{
4410 return (fixp->fx_next != NULL
738e5348 4411 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4412 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4413 && fixp->fx_offset == fixp->fx_next->fx_offset);
4414}
4415
462427c4
RS
4416/* Move all labels in LABELS to the current insertion point. TEXT_P
4417 says whether the labels refer to text or data. */
404a8071
RS
4418
4419static void
5b7c81bd 4420mips_move_labels (struct insn_label_list *labels, bool text_p)
404a8071
RS
4421{
4422 struct insn_label_list *l;
4423 valueT val;
4424
462427c4 4425 for (l = labels; l != NULL; l = l->next)
404a8071 4426 {
9c2799c2 4427 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4428 symbol_set_frag (l->label, frag_now);
4429 val = (valueT) frag_now_fix ();
770c0151
FS
4430 /* MIPS16/microMIPS text labels are stored as odd.
4431 We just carry the ISA mode bit forward. */
462427c4 4432 if (text_p && HAVE_CODE_COMPRESSION)
770c0151 4433 val |= (S_GET_VALUE (l->label) & 0x1);
404a8071
RS
4434 S_SET_VALUE (l->label, val);
4435 }
4436}
4437
462427c4
RS
4438/* Move all labels in insn_labels to the current insertion point
4439 and treat them as text labels. */
4440
4441static void
4442mips_move_text_labels (void)
4443{
5b7c81bd 4444 mips_move_labels (seg_info (now_seg)->label_list, true);
462427c4
RS
4445}
4446
9e009953
MR
4447/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4448
5b7c81bd 4449static bool
5f0fe04b
TS
4450s_is_linkonce (symbolS *sym, segT from_seg)
4451{
5b7c81bd 4452 bool linkonce = false;
5f0fe04b
TS
4453 segT symseg = S_GET_SEGMENT (sym);
4454
4455 if (symseg != from_seg && !S_IS_LOCAL (sym))
4456 {
fd361982 4457 if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
5b7c81bd 4458 linkonce = true;
5f0fe04b
TS
4459 /* The GNU toolchain uses an extension for ELF: a section
4460 beginning with the magic string .gnu.linkonce is a
4461 linkonce section. */
d34049e8 4462 if (startswith (segment_name (symseg), ".gnu.linkonce"))
5b7c81bd 4463 linkonce = true;
5f0fe04b
TS
4464 }
4465 return linkonce;
4466}
4467
e1b47bd5 4468/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4469 linker to handle them specially, such as generating jalx instructions
4470 when needed. We also make them odd for the duration of the assembly,
4471 in order to generate the right sort of code. We will make them even
252b5132
RH
4472 in the adjust_symtab routine, while leaving them marked. This is
4473 convenient for the debugger and the disassembler. The linker knows
4474 to make them odd again. */
4475
4476static void
e1b47bd5 4477mips_compressed_mark_label (symbolS *label)
252b5132 4478{
df58fc94 4479 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4480
f3ded42a
RS
4481 if (mips_opts.mips16)
4482 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4483 else
4484 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4485 if ((S_GET_VALUE (label) & 1) == 0
4486 /* Don't adjust the address if the label is global or weak, or
4487 in a link-once section, since we'll be emitting symbol reloc
4488 references to it which will be patched up by the linker, and
4489 the final value of the symbol may or may not be MIPS16/microMIPS. */
4490 && !S_IS_WEAK (label)
4491 && !S_IS_EXTERNAL (label)
4492 && !s_is_linkonce (label, now_seg))
4493 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4494}
4495
4496/* Mark preceding MIPS16 or microMIPS instruction labels. */
4497
4498static void
4499mips_compressed_mark_labels (void)
4500{
4501 struct insn_label_list *l;
4502
4503 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4504 mips_compressed_mark_label (l->label);
252b5132
RH
4505}
4506
4d7206a2
RS
4507/* End the current frag. Make it a variant frag and record the
4508 relaxation info. */
4509
4510static void
4511relax_close_frag (void)
4512{
584892a6 4513 mips_macro_warning.first_frag = frag_now;
4d7206a2 4514 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4515 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4516 mips_pic != NO_PIC),
4d7206a2
RS
4517 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4518
4519 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4520 mips_relax.first_fixup = 0;
4521}
4522
4523/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4524 See the comment above RELAX_ENCODE for more details. */
4525
4526static void
4527relax_start (symbolS *symbol)
4528{
9c2799c2 4529 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4530 mips_relax.sequence = 1;
4531 mips_relax.symbol = symbol;
4532}
4533
4534/* Start generating the second version of a relaxable sequence.
4535 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4536
4537static void
4d7206a2
RS
4538relax_switch (void)
4539{
9c2799c2 4540 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4541 mips_relax.sequence = 2;
4542}
4543
4544/* End the current relaxable sequence. */
4545
4546static void
4547relax_end (void)
4548{
9c2799c2 4549 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4550 relax_close_frag ();
4551 mips_relax.sequence = 0;
4552}
4553
11625dd8
RS
4554/* Return true if IP is a delayed branch or jump. */
4555
5b7c81bd 4556static inline bool
11625dd8
RS
4557delayed_branch_p (const struct mips_cl_insn *ip)
4558{
4559 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4560 | INSN_COND_BRANCH_DELAY
4561 | INSN_COND_BRANCH_LIKELY)) != 0;
4562}
4563
4564/* Return true if IP is a compact branch or jump. */
4565
5b7c81bd 4566static inline bool
11625dd8
RS
4567compact_branch_p (const struct mips_cl_insn *ip)
4568{
26545944
RS
4569 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4570 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4571}
4572
4573/* Return true if IP is an unconditional branch or jump. */
4574
5b7c81bd 4575static inline bool
11625dd8
RS
4576uncond_branch_p (const struct mips_cl_insn *ip)
4577{
4578 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4579 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4580}
4581
4582/* Return true if IP is a branch-likely instruction. */
4583
5b7c81bd 4584static inline bool
11625dd8
RS
4585branch_likely_p (const struct mips_cl_insn *ip)
4586{
4587 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4588}
4589
14fe068b
RS
4590/* Return the type of nop that should be used to fill the delay slot
4591 of delayed branch IP. */
4592
4593static struct mips_cl_insn *
4594get_delay_slot_nop (const struct mips_cl_insn *ip)
4595{
4596 if (mips_opts.micromips
4597 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4598 return &micromips_nop32_insn;
4599 return NOP_INSN;
4600}
4601
fc76e730
RS
4602/* Return a mask that has bit N set if OPCODE reads the register(s)
4603 in operand N. */
df58fc94
RS
4604
4605static unsigned int
fc76e730 4606insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4607{
fc76e730
RS
4608 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4609}
df58fc94 4610
fc76e730
RS
4611/* Return a mask that has bit N set if OPCODE writes to the register(s)
4612 in operand N. */
4613
4614static unsigned int
4615insn_write_mask (const struct mips_opcode *opcode)
4616{
4617 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4618}
4619
4620/* Return a mask of the registers specified by operand OPERAND of INSN.
4621 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4622 is set. */
4623
4624static unsigned int
4625operand_reg_mask (const struct mips_cl_insn *insn,
4626 const struct mips_operand *operand,
4627 unsigned int type_mask)
4628{
4629 unsigned int uval, vsel;
4630
4631 switch (operand->type)
df58fc94 4632 {
fc76e730
RS
4633 case OP_INT:
4634 case OP_MAPPED_INT:
4635 case OP_MSB:
4636 case OP_PCREL:
4637 case OP_PERF_REG:
4638 case OP_ADDIUSP_INT:
4639 case OP_ENTRY_EXIT_LIST:
4640 case OP_REPEAT_DEST_REG:
4641 case OP_REPEAT_PREV_REG:
4642 case OP_PC:
14daeee3
RS
4643 case OP_VU0_SUFFIX:
4644 case OP_VU0_MATCH_SUFFIX:
56d438b1 4645 case OP_IMM_INDEX:
fc76e730
RS
4646 abort ();
4647
25499ac7
MR
4648 case OP_REG28:
4649 return 1 << 28;
4650
fc76e730 4651 case OP_REG:
0f35dbc4 4652 case OP_OPTIONAL_REG:
fc76e730
RS
4653 {
4654 const struct mips_reg_operand *reg_op;
4655
4656 reg_op = (const struct mips_reg_operand *) operand;
4657 if (!(type_mask & (1 << reg_op->reg_type)))
4658 return 0;
4659 uval = insn_extract_operand (insn, operand);
e0fd91ef 4660 return 1u << mips_decode_reg_operand (reg_op, uval);
fc76e730
RS
4661 }
4662
4663 case OP_REG_PAIR:
4664 {
4665 const struct mips_reg_pair_operand *pair_op;
4666
4667 pair_op = (const struct mips_reg_pair_operand *) operand;
4668 if (!(type_mask & (1 << pair_op->reg_type)))
4669 return 0;
4670 uval = insn_extract_operand (insn, operand);
e0fd91ef 4671 return (1u << pair_op->reg1_map[uval]) | (1u << pair_op->reg2_map[uval]);
fc76e730
RS
4672 }
4673
4674 case OP_CLO_CLZ_DEST:
4675 if (!(type_mask & (1 << OP_REG_GP)))
4676 return 0;
4677 uval = insn_extract_operand (insn, operand);
e0fd91ef 4678 return (1u << (uval & 31)) | (1u << (uval >> 5));
fc76e730 4679
7361da2c
AB
4680 case OP_SAME_RS_RT:
4681 if (!(type_mask & (1 << OP_REG_GP)))
4682 return 0;
4683 uval = insn_extract_operand (insn, operand);
4684 gas_assert ((uval & 31) == (uval >> 5));
e0fd91ef 4685 return 1u << (uval & 31);
7361da2c
AB
4686
4687 case OP_CHECK_PREV:
4688 case OP_NON_ZERO_REG:
4689 if (!(type_mask & (1 << OP_REG_GP)))
4690 return 0;
4691 uval = insn_extract_operand (insn, operand);
e0fd91ef 4692 return 1u << (uval & 31);
7361da2c 4693
fc76e730
RS
4694 case OP_LWM_SWM_LIST:
4695 abort ();
4696
4697 case OP_SAVE_RESTORE_LIST:
4698 abort ();
4699
4700 case OP_MDMX_IMM_REG:
4701 if (!(type_mask & (1 << OP_REG_VEC)))
4702 return 0;
4703 uval = insn_extract_operand (insn, operand);
4704 vsel = uval >> 5;
4705 if ((vsel & 0x18) == 0x18)
4706 return 0;
e0fd91ef 4707 return 1u << (uval & 31);
56d438b1
CF
4708
4709 case OP_REG_INDEX:
4710 if (!(type_mask & (1 << OP_REG_GP)))
4711 return 0;
e0fd91ef 4712 return 1u << insn_extract_operand (insn, operand);
df58fc94 4713 }
fc76e730
RS
4714 abort ();
4715}
4716
4717/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4718 where bit N of OPNO_MASK is set if operand N should be included.
4719 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4720 is set. */
4721
4722static unsigned int
4723insn_reg_mask (const struct mips_cl_insn *insn,
4724 unsigned int type_mask, unsigned int opno_mask)
4725{
4726 unsigned int opno, reg_mask;
4727
4728 opno = 0;
4729 reg_mask = 0;
4730 while (opno_mask != 0)
4731 {
4732 if (opno_mask & 1)
4733 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4734 opno_mask >>= 1;
4735 opno += 1;
4736 }
4737 return reg_mask;
df58fc94
RS
4738}
4739
4c260379
RS
4740/* Return the mask of core registers that IP reads. */
4741
4742static unsigned int
4743gpr_read_mask (const struct mips_cl_insn *ip)
4744{
4745 unsigned long pinfo, pinfo2;
4746 unsigned int mask;
4747
fc76e730 4748 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4749 pinfo = ip->insn_mo->pinfo;
4750 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4751 if (pinfo & INSN_UDI)
4c260379 4752 {
fc76e730
RS
4753 /* UDI instructions have traditionally been assumed to read RS
4754 and RT. */
4755 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4756 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4757 }
fc76e730
RS
4758 if (pinfo & INSN_READ_GPR_24)
4759 mask |= 1 << 24;
4760 if (pinfo2 & INSN2_READ_GPR_16)
4761 mask |= 1 << 16;
4762 if (pinfo2 & INSN2_READ_SP)
4763 mask |= 1 << SP;
26545944 4764 if (pinfo2 & INSN2_READ_GPR_31)
a6a1f5e0 4765 mask |= 1u << 31;
fe35f09f
RS
4766 /* Don't include register 0. */
4767 return mask & ~1;
4c260379
RS
4768}
4769
4770/* Return the mask of core registers that IP writes. */
4771
4772static unsigned int
4773gpr_write_mask (const struct mips_cl_insn *ip)
4774{
4775 unsigned long pinfo, pinfo2;
4776 unsigned int mask;
4777
fc76e730 4778 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4779 pinfo = ip->insn_mo->pinfo;
4780 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4781 if (pinfo & INSN_WRITE_GPR_24)
4782 mask |= 1 << 24;
4783 if (pinfo & INSN_WRITE_GPR_31)
a6a1f5e0 4784 mask |= 1u << 31;
fc76e730
RS
4785 if (pinfo & INSN_UDI)
4786 /* UDI instructions have traditionally been assumed to write to RD. */
4787 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4788 if (pinfo2 & INSN2_WRITE_SP)
4789 mask |= 1 << SP;
fe35f09f
RS
4790 /* Don't include register 0. */
4791 return mask & ~1;
4c260379
RS
4792}
4793
4794/* Return the mask of floating-point registers that IP reads. */
4795
4796static unsigned int
4797fpr_read_mask (const struct mips_cl_insn *ip)
4798{
fc76e730 4799 unsigned long pinfo;
4c260379
RS
4800 unsigned int mask;
4801
9d5de888
CF
4802 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4803 | (1 << OP_REG_MSA)),
fc76e730 4804 insn_read_mask (ip->insn_mo));
4c260379 4805 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4806 /* Conservatively treat all operands to an FP_D instruction are doubles.
4807 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4808 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4809 mask |= mask << 1;
4810 return mask;
4811}
4812
4813/* Return the mask of floating-point registers that IP writes. */
4814
4815static unsigned int
4816fpr_write_mask (const struct mips_cl_insn *ip)
4817{
fc76e730 4818 unsigned long pinfo;
4c260379
RS
4819 unsigned int mask;
4820
9d5de888
CF
4821 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4822 | (1 << OP_REG_MSA)),
fc76e730 4823 insn_write_mask (ip->insn_mo));
4c260379 4824 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4825 /* Conservatively treat all operands to an FP_D instruction are doubles.
4826 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4827 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4828 mask |= mask << 1;
4829 return mask;
4830}
4831
a1d78564
RS
4832/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4833 Check whether that is allowed. */
4834
5b7c81bd 4835static bool
a1d78564
RS
4836mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4837{
4838 const char *s = insn->name;
5b7c81bd
AM
4839 bool oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4840 || FPR_SIZE == 64) && mips_opts.oddspreg;
a1d78564
RS
4841
4842 if (insn->pinfo == INSN_MACRO)
4843 /* Let a macro pass, we'll catch it later when it is expanded. */
5b7c81bd 4844 return true;
a1d78564 4845
351cdf24
MF
4846 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4847 otherwise it depends on oddspreg. */
4848 if ((insn->pinfo & FP_S)
4849 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4850 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4851 return FPR_SIZE == 32 || oddspreg;
a1d78564 4852
351cdf24
MF
4853 /* Allow odd registers for single-precision ops and double-precision if the
4854 floating-point registers are 64-bit wide. */
4855 switch (insn->pinfo & (FP_S | FP_D))
4856 {
4857 case FP_S:
4858 case 0:
4859 return oddspreg;
4860 case FP_D:
4861 return FPR_SIZE == 64;
4862 default:
4863 break;
a1d78564
RS
4864 }
4865
351cdf24
MF
4866 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4867 s = strchr (insn->name, '.');
4868 if (s != NULL && opnum == 2)
4869 s = strchr (s + 1, '.');
4870 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4871 return oddspreg;
a1d78564 4872
351cdf24 4873 return FPR_SIZE == 64;
a1d78564
RS
4874}
4875
a1d78564
RS
4876/* Information about an instruction argument that we're trying to match. */
4877struct mips_arg_info
4878{
4879 /* The instruction so far. */
4880 struct mips_cl_insn *insn;
4881
a92713e6
RS
4882 /* The first unconsumed operand token. */
4883 struct mips_operand_token *token;
4884
a1d78564
RS
4885 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4886 int opnum;
4887
4888 /* The 1-based argument number, for error reporting. This does not
4889 count elided optional registers, etc.. */
4890 int argnum;
4891
4892 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4893 unsigned int last_regno;
4894
4895 /* If the first operand was an OP_REG, this is the register that it
4896 specified, otherwise it is ILLEGAL_REG. */
4897 unsigned int dest_regno;
4898
4899 /* The value of the last OP_INT operand. Only used for OP_MSB,
4900 where it gives the lsb position. */
4901 unsigned int last_op_int;
4902
60f20e8b 4903 /* If true, match routines should assume that no later instruction
2b0f3761 4904 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4905 possible. Match routines should not report errors if something
4906 is only invalid for !LAX_MATCH. */
5b7c81bd 4907 bool lax_match;
a1d78564 4908
a1d78564 4909 /* True if a reference to the current AT register was seen. */
5b7c81bd 4910 bool seen_at;
a1d78564
RS
4911};
4912
1a00e612
RS
4913/* Record that the argument is out of range. */
4914
4915static void
4916match_out_of_range (struct mips_arg_info *arg)
4917{
4918 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4919}
4920
4921/* Record that the argument isn't constant but needs to be. */
4922
4923static void
4924match_not_constant (struct mips_arg_info *arg)
4925{
4926 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4927 arg->argnum);
4928}
4929
a92713e6
RS
4930/* Try to match an OT_CHAR token for character CH. Consume the token
4931 and return true on success, otherwise return false. */
a1d78564 4932
5b7c81bd 4933static bool
a92713e6 4934match_char (struct mips_arg_info *arg, char ch)
a1d78564 4935{
a92713e6
RS
4936 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4937 {
4938 ++arg->token;
4939 if (ch == ',')
4940 arg->argnum += 1;
5b7c81bd 4941 return true;
a92713e6 4942 }
5b7c81bd 4943 return false;
a92713e6 4944}
a1d78564 4945
a92713e6
RS
4946/* Try to get an expression from the next tokens in ARG. Consume the
4947 tokens and return true on success, storing the expression value in
4948 VALUE and relocation types in R. */
4949
5b7c81bd 4950static bool
a92713e6
RS
4951match_expression (struct mips_arg_info *arg, expressionS *value,
4952 bfd_reloc_code_real_type *r)
4953{
d436c1c2
RS
4954 /* If the next token is a '(' that was parsed as being part of a base
4955 expression, assume we have an elided offset. The later match will fail
4956 if this turns out to be wrong. */
4957 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4958 {
d436c1c2
RS
4959 value->X_op = O_constant;
4960 value->X_add_number = 0;
4961 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
5b7c81bd 4962 return true;
a92713e6
RS
4963 }
4964
d436c1c2
RS
4965 /* Reject register-based expressions such as "0+$2" and "(($2))".
4966 For plain registers the default error seems more appropriate. */
4967 if (arg->token->type == OT_INTEGER
4968 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4969 {
d436c1c2 4970 set_insn_error (arg->argnum, _("register value used as expression"));
5b7c81bd 4971 return false;
a1d78564 4972 }
d436c1c2
RS
4973
4974 if (arg->token->type == OT_INTEGER)
a92713e6 4975 {
d436c1c2
RS
4976 *value = arg->token->u.integer.value;
4977 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4978 ++arg->token;
5b7c81bd 4979 return true;
a92713e6 4980 }
a92713e6 4981
d436c1c2
RS
4982 set_insn_error_i
4983 (arg->argnum, _("operand %d must be an immediate expression"),
4984 arg->argnum);
5b7c81bd 4985 return false;
a92713e6
RS
4986}
4987
4988/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4989 the tokens and return true on success, storing the constant value
a54d5f8b 4990 in *VALUE. */
a92713e6 4991
5b7c81bd 4992static bool
1a00e612 4993match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4994{
4995 expressionS ex;
4996 bfd_reloc_code_real_type r[3];
a1d78564 4997
a92713e6 4998 if (!match_expression (arg, &ex, r))
5b7c81bd 4999 return false;
a92713e6
RS
5000
5001 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5002 *value = ex.X_add_number;
5003 else
5004 {
c96425c5
MR
5005 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5006 match_out_of_range (arg);
5007 else
5008 match_not_constant (arg);
5b7c81bd 5009 return false;
a1d78564 5010 }
5b7c81bd 5011 return true;
a1d78564
RS
5012}
5013
5014/* Return the RTYPE_* flags for a register operand of type TYPE that
5015 appears in instruction OPCODE. */
5016
5017static unsigned int
5018convert_reg_type (const struct mips_opcode *opcode,
5019 enum mips_reg_operand_type type)
5020{
5021 switch (type)
5022 {
5023 case OP_REG_GP:
5024 return RTYPE_NUM | RTYPE_GP;
5025
5026 case OP_REG_FP:
5027 /* Allow vector register names for MDMX if the instruction is a 64-bit
5028 FPR load, store or move (including moves to and from GPRs). */
5029 if ((mips_opts.ase & ASE_MDMX)
5030 && (opcode->pinfo & FP_D)
43885403 5031 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5032 | INSN_COPROC_MEMORY_DELAY
43885403 5033 | INSN_LOAD_COPROC
67dc82bc 5034 | INSN_LOAD_MEMORY
a1d78564
RS
5035 | INSN_STORE_MEMORY)))
5036 return RTYPE_FPU | RTYPE_VEC;
5037 return RTYPE_FPU;
5038
5039 case OP_REG_CCC:
5040 if (opcode->pinfo & (FP_D | FP_S))
5041 return RTYPE_CCC | RTYPE_FCC;
5042 return RTYPE_CCC;
5043
5044 case OP_REG_VEC:
5045 if (opcode->membership & INSN_5400)
5046 return RTYPE_FPU;
5047 return RTYPE_FPU | RTYPE_VEC;
5048
5049 case OP_REG_ACC:
5050 return RTYPE_ACC;
5051
5052 case OP_REG_COPRO:
9204ccd4 5053 case OP_REG_CONTROL:
a1d78564
RS
5054 if (opcode->name[strlen (opcode->name) - 1] == '0')
5055 return RTYPE_NUM | RTYPE_CP0;
5056 return RTYPE_NUM;
5057
5058 case OP_REG_HW:
5059 return RTYPE_NUM;
14daeee3
RS
5060
5061 case OP_REG_VI:
5062 return RTYPE_NUM | RTYPE_VI;
5063
5064 case OP_REG_VF:
5065 return RTYPE_NUM | RTYPE_VF;
5066
5067 case OP_REG_R5900_I:
5068 return RTYPE_R5900_I;
5069
5070 case OP_REG_R5900_Q:
5071 return RTYPE_R5900_Q;
5072
5073 case OP_REG_R5900_R:
5074 return RTYPE_R5900_R;
5075
5076 case OP_REG_R5900_ACC:
5077 return RTYPE_R5900_ACC;
56d438b1
CF
5078
5079 case OP_REG_MSA:
5080 return RTYPE_MSA;
5081
5082 case OP_REG_MSA_CTRL:
5083 return RTYPE_NUM;
a1d78564
RS
5084 }
5085 abort ();
5086}
5087
5088/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5089
5090static void
5091check_regno (struct mips_arg_info *arg,
5092 enum mips_reg_operand_type type, unsigned int regno)
5093{
5094 if (AT && type == OP_REG_GP && regno == AT)
5b7c81bd 5095 arg->seen_at = true;
a1d78564
RS
5096
5097 if (type == OP_REG_FP
5098 && (regno & 1) != 0
a1d78564 5099 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5100 {
5101 /* This was a warning prior to introducing O32 FPXX and FP64 support
5102 so maintain a warning for FP32 but raise an error for the new
5103 cases. */
5104 if (FPR_SIZE == 32)
5105 as_warn (_("float register should be even, was %d"), regno);
5106 else
5107 as_bad (_("float register should be even, was %d"), regno);
5108 }
a1d78564
RS
5109
5110 if (type == OP_REG_CCC)
5111 {
5112 const char *name;
5113 size_t length;
5114
5115 name = arg->insn->insn_mo->name;
5116 length = strlen (name);
5117 if ((regno & 1) != 0
5118 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
d34049e8 5119 || (length >= 5 && startswith (name + length - 5, "any2"))))
1661c76c 5120 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5121 name, regno);
5122
5123 if ((regno & 3) != 0
d34049e8 5124 && (length >= 5 && startswith (name + length - 5, "any4")))
1661c76c 5125 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5126 name, regno);
5127 }
5128}
5129
a92713e6
RS
5130/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5131 a register of type TYPE. Return true on success, storing the register
5132 number in *REGNO and warning about any dubious uses. */
5133
5b7c81bd 5134static bool
a92713e6
RS
5135match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5136 unsigned int symval, unsigned int *regno)
5137{
5138 if (type == OP_REG_VEC)
5139 symval = mips_prefer_vec_regno (symval);
5140 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5b7c81bd 5141 return false;
a92713e6
RS
5142
5143 *regno = symval & RNUM_MASK;
5144 check_regno (arg, type, *regno);
5b7c81bd 5145 return true;
a92713e6
RS
5146}
5147
5148/* Try to interpret the next token in ARG as a register of type TYPE.
5149 Consume the token and return true on success, storing the register
5150 number in *REGNO. Return false on failure. */
5151
5b7c81bd 5152static bool
a92713e6
RS
5153match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5154 unsigned int *regno)
5155{
5156 if (arg->token->type == OT_REG
5157 && match_regno (arg, type, arg->token->u.regno, regno))
5158 {
5159 ++arg->token;
5b7c81bd 5160 return true;
a92713e6 5161 }
5b7c81bd 5162 return false;
a92713e6
RS
5163}
5164
5165/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5166 Consume the token and return true on success, storing the register numbers
5167 in *REGNO1 and *REGNO2. Return false on failure. */
5168
5b7c81bd 5169static bool
a92713e6
RS
5170match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5171 unsigned int *regno1, unsigned int *regno2)
5172{
5173 if (match_reg (arg, type, regno1))
5174 {
5175 *regno2 = *regno1;
5b7c81bd 5176 return true;
a92713e6
RS
5177 }
5178 if (arg->token->type == OT_REG_RANGE
5179 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5180 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5181 && *regno1 <= *regno2)
5182 {
5183 ++arg->token;
5b7c81bd 5184 return true;
a92713e6 5185 }
5b7c81bd 5186 return false;
a92713e6
RS
5187}
5188
a1d78564
RS
5189/* OP_INT matcher. */
5190
5b7c81bd 5191static bool
a1d78564 5192match_int_operand (struct mips_arg_info *arg,
a92713e6 5193 const struct mips_operand *operand_base)
a1d78564
RS
5194{
5195 const struct mips_int_operand *operand;
3ccad066 5196 unsigned int uval;
a1d78564
RS
5197 int min_val, max_val, factor;
5198 offsetT sval;
a1d78564
RS
5199
5200 operand = (const struct mips_int_operand *) operand_base;
5201 factor = 1 << operand->shift;
3ccad066
RS
5202 min_val = mips_int_operand_min (operand);
5203 max_val = mips_int_operand_max (operand);
a1d78564 5204
d436c1c2
RS
5205 if (operand_base->lsb == 0
5206 && operand_base->size == 16
5207 && operand->shift == 0
5208 && operand->bias == 0
5209 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5210 {
5211 /* The operand can be relocated. */
a92713e6 5212 if (!match_expression (arg, &offset_expr, offset_reloc))
5b7c81bd 5213 return false;
a92713e6 5214
c96425c5
MR
5215 if (offset_expr.X_op == O_big)
5216 {
5217 match_out_of_range (arg);
5b7c81bd 5218 return false;
c96425c5
MR
5219 }
5220
a92713e6 5221 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5222 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5223 leave the relocation value in offset_expr and offset_relocs
5224 for the caller to process. */
5b7c81bd 5225 return true;
a92713e6
RS
5226
5227 if (offset_expr.X_op != O_constant)
a1d78564 5228 {
60f20e8b
RS
5229 /* Accept non-constant operands if no later alternative matches,
5230 leaving it for the caller to process. */
5231 if (!arg->lax_match)
602b88e3
MR
5232 {
5233 match_not_constant (arg);
5b7c81bd 5234 return false;
602b88e3 5235 }
a92713e6 5236 offset_reloc[0] = BFD_RELOC_LO16;
5b7c81bd 5237 return true;
a1d78564 5238 }
a92713e6 5239
a1d78564
RS
5240 /* Clear the global state; we're going to install the operand
5241 ourselves. */
a92713e6 5242 sval = offset_expr.X_add_number;
a1d78564 5243 offset_expr.X_op = O_absent;
60f20e8b
RS
5244
5245 /* For compatibility with older assemblers, we accept
5246 0x8000-0xffff as signed 16-bit numbers when only
5247 signed numbers are allowed. */
5248 if (sval > max_val)
5249 {
5250 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5251 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5252 {
5253 match_out_of_range (arg);
5b7c81bd 5254 return false;
20c59b84 5255 }
60f20e8b 5256 }
a1d78564
RS
5257 }
5258 else
5259 {
1a00e612 5260 if (!match_const_int (arg, &sval))
5b7c81bd 5261 return false;
a1d78564
RS
5262 }
5263
5264 arg->last_op_int = sval;
5265
1a00e612 5266 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5267 {
1a00e612 5268 match_out_of_range (arg);
5b7c81bd 5269 return false;
a1d78564
RS
5270 }
5271
5272 uval = (unsigned int) sval >> operand->shift;
5273 uval -= operand->bias;
5274
5275 /* Handle -mfix-cn63xxp1. */
5276 if (arg->opnum == 1
5277 && mips_fix_cn63xxp1
5278 && !mips_opts.micromips
5279 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5280 switch (uval)
5281 {
5282 case 5:
5283 case 25:
5284 case 26:
5285 case 27:
5286 case 28:
5287 case 29:
5288 case 30:
5289 case 31:
5290 /* These are ok. */
5291 break;
5292
5293 default:
5294 /* The rest must be changed to 28. */
5295 uval = 28;
5296 break;
5297 }
5298
5299 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5300 return true;
a1d78564
RS
5301}
5302
5303/* OP_MAPPED_INT matcher. */
5304
5b7c81bd 5305static bool
a1d78564 5306match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5307 const struct mips_operand *operand_base)
a1d78564
RS
5308{
5309 const struct mips_mapped_int_operand *operand;
5310 unsigned int uval, num_vals;
5311 offsetT sval;
5312
5313 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5314 if (!match_const_int (arg, &sval))
5b7c81bd 5315 return false;
a1d78564
RS
5316
5317 num_vals = 1 << operand_base->size;
5318 for (uval = 0; uval < num_vals; uval++)
5319 if (operand->int_map[uval] == sval)
5320 break;
5321 if (uval == num_vals)
1a00e612
RS
5322 {
5323 match_out_of_range (arg);
5b7c81bd 5324 return false;
1a00e612 5325 }
a1d78564
RS
5326
5327 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5328 return true;
a1d78564
RS
5329}
5330
5331/* OP_MSB matcher. */
5332
5b7c81bd 5333static bool
a1d78564 5334match_msb_operand (struct mips_arg_info *arg,
a92713e6 5335 const struct mips_operand *operand_base)
a1d78564
RS
5336{
5337 const struct mips_msb_operand *operand;
5338 int min_val, max_val, max_high;
5339 offsetT size, sval, high;
5340
5341 operand = (const struct mips_msb_operand *) operand_base;
5342 min_val = operand->bias;
5343 max_val = min_val + (1 << operand_base->size) - 1;
5344 max_high = operand->opsize;
5345
1a00e612 5346 if (!match_const_int (arg, &size))
5b7c81bd 5347 return false;
a1d78564
RS
5348
5349 high = size + arg->last_op_int;
5350 sval = operand->add_lsb ? high : size;
5351
5352 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5353 {
1a00e612 5354 match_out_of_range (arg);
5b7c81bd 5355 return false;
a1d78564
RS
5356 }
5357 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5b7c81bd 5358 return true;
a1d78564
RS
5359}
5360
5361/* OP_REG matcher. */
5362
5b7c81bd 5363static bool
a1d78564 5364match_reg_operand (struct mips_arg_info *arg,
a92713e6 5365 const struct mips_operand *operand_base)
a1d78564
RS
5366{
5367 const struct mips_reg_operand *operand;
a92713e6 5368 unsigned int regno, uval, num_vals;
a1d78564
RS
5369
5370 operand = (const struct mips_reg_operand *) operand_base;
a92713e6 5371 if (!match_reg (arg, operand->reg_type, &regno))
5b7c81bd 5372 return false;
a1d78564
RS
5373
5374 if (operand->reg_map)
5375 {
5376 num_vals = 1 << operand->root.size;
5377 for (uval = 0; uval < num_vals; uval++)
5378 if (operand->reg_map[uval] == regno)
5379 break;
5380 if (num_vals == uval)
5b7c81bd 5381 return false;
a1d78564
RS
5382 }
5383 else
5384 uval = regno;
5385
a1d78564
RS
5386 arg->last_regno = regno;
5387 if (arg->opnum == 1)
5388 arg->dest_regno = regno;
5389 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5390 return true;
a1d78564
RS
5391}
5392
5393/* OP_REG_PAIR matcher. */
5394
5b7c81bd 5395static bool
a1d78564 5396match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5397 const struct mips_operand *operand_base)
a1d78564
RS
5398{
5399 const struct mips_reg_pair_operand *operand;
a92713e6 5400 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5401
5402 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5403 if (!match_reg (arg, operand->reg_type, &regno1)
5404 || !match_char (arg, ',')
5405 || !match_reg (arg, operand->reg_type, &regno2))
5b7c81bd 5406 return false;
a1d78564
RS
5407
5408 num_vals = 1 << operand_base->size;
5409 for (uval = 0; uval < num_vals; uval++)
5410 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5411 break;
5412 if (uval == num_vals)
5b7c81bd 5413 return false;
a1d78564 5414
a1d78564 5415 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5416 return true;
a1d78564
RS
5417}
5418
5419/* OP_PCREL matcher. The caller chooses the relocation type. */
5420
5b7c81bd 5421static bool
a92713e6 5422match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5423{
a92713e6
RS
5424 bfd_reloc_code_real_type r[3];
5425
5426 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5427}
5428
5429/* OP_PERF_REG matcher. */
5430
5b7c81bd 5431static bool
a1d78564 5432match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5433 const struct mips_operand *operand)
a1d78564
RS
5434{
5435 offsetT sval;
5436
1a00e612 5437 if (!match_const_int (arg, &sval))
5b7c81bd 5438 return false;
a1d78564
RS
5439
5440 if (sval != 0
5441 && (sval != 1
5442 || (mips_opts.arch == CPU_R5900
5443 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5444 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5445 {
1a00e612 5446 set_insn_error (arg->argnum, _("invalid performance register"));
5b7c81bd 5447 return false;
a1d78564
RS
5448 }
5449
5450 insn_insert_operand (arg->insn, operand, sval);
5b7c81bd 5451 return true;
a1d78564
RS
5452}
5453
5454/* OP_ADDIUSP matcher. */
5455
5b7c81bd 5456static bool
a1d78564 5457match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5458 const struct mips_operand *operand)
a1d78564
RS
5459{
5460 offsetT sval;
5461 unsigned int uval;
5462
1a00e612 5463 if (!match_const_int (arg, &sval))
5b7c81bd 5464 return false;
a1d78564
RS
5465
5466 if (sval % 4)
1a00e612
RS
5467 {
5468 match_out_of_range (arg);
5b7c81bd 5469 return false;
1a00e612 5470 }
a1d78564
RS
5471
5472 sval /= 4;
5473 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5474 {
5475 match_out_of_range (arg);
5b7c81bd 5476 return false;
1a00e612 5477 }
a1d78564
RS
5478
5479 uval = (unsigned int) sval;
5480 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5481 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 5482 return true;
a1d78564
RS
5483}
5484
5485/* OP_CLO_CLZ_DEST matcher. */
5486
5b7c81bd 5487static bool
a1d78564 5488match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5489 const struct mips_operand *operand)
a1d78564
RS
5490{
5491 unsigned int regno;
5492
a92713e6 5493 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5494 return false;
a1d78564 5495
a1d78564 5496 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5b7c81bd 5497 return true;
a1d78564
RS
5498}
5499
7361da2c
AB
5500/* OP_CHECK_PREV matcher. */
5501
5b7c81bd 5502static bool
7361da2c
AB
5503match_check_prev_operand (struct mips_arg_info *arg,
5504 const struct mips_operand *operand_base)
5505{
5506 const struct mips_check_prev_operand *operand;
5507 unsigned int regno;
5508
5509 operand = (const struct mips_check_prev_operand *) operand_base;
5510
5511 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5512 return false;
7361da2c
AB
5513
5514 if (!operand->zero_ok && regno == 0)
5b7c81bd 5515 return false;
7361da2c
AB
5516
5517 if ((operand->less_than_ok && regno < arg->last_regno)
5518 || (operand->greater_than_ok && regno > arg->last_regno)
5519 || (operand->equal_ok && regno == arg->last_regno))
5520 {
5521 arg->last_regno = regno;
5522 insn_insert_operand (arg->insn, operand_base, regno);
5b7c81bd 5523 return true;
7361da2c
AB
5524 }
5525
5b7c81bd 5526 return false;
7361da2c
AB
5527}
5528
5529/* OP_SAME_RS_RT matcher. */
5530
5b7c81bd 5531static bool
7361da2c
AB
5532match_same_rs_rt_operand (struct mips_arg_info *arg,
5533 const struct mips_operand *operand)
5534{
5535 unsigned int regno;
5536
5537 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5538 return false;
7361da2c
AB
5539
5540 if (regno == 0)
5541 {
5542 set_insn_error (arg->argnum, _("the source register must not be $0"));
5b7c81bd 5543 return false;
7361da2c
AB
5544 }
5545
5546 arg->last_regno = regno;
5547
5548 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5b7c81bd 5549 return true;
7361da2c
AB
5550}
5551
a1d78564
RS
5552/* OP_LWM_SWM_LIST matcher. */
5553
5b7c81bd 5554static bool
a1d78564 5555match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5556 const struct mips_operand *operand)
a1d78564 5557{
a92713e6
RS
5558 unsigned int reglist, sregs, ra, regno1, regno2;
5559 struct mips_arg_info reset;
a1d78564 5560
a92713e6
RS
5561 reglist = 0;
5562 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5563 return false;
a92713e6
RS
5564 do
5565 {
5566 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5567 {
5568 reglist |= 1 << FP;
5569 regno2 = S7;
5570 }
5571 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5572 reset = *arg;
5573 }
5574 while (match_char (arg, ',')
5575 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5576 *arg = reset;
a1d78564
RS
5577
5578 if (operand->size == 2)
5579 {
5580 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5581
5582 s0, ra
5583 s0, s1, ra, s2, s3
5584 s0-s2, ra
5585
5586 and any permutations of these. */
5587 if ((reglist & 0xfff1ffff) != 0x80010000)
5b7c81bd 5588 return false;
a1d78564
RS
5589
5590 sregs = (reglist >> 17) & 7;
5591 ra = 0;
5592 }
5593 else
5594 {
5595 /* The list must include at least one of ra and s0-sN,
5596 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5597 which are $23 and $30 respectively.) E.g.:
5598
5599 ra
5600 s0
5601 ra, s0, s1, s2
5602 s0-s8
5603 s0-s5, ra
5604
5605 and any permutations of these. */
5606 if ((reglist & 0x3f00ffff) != 0)
5b7c81bd 5607 return false;
a1d78564
RS
5608
5609 ra = (reglist >> 27) & 0x10;
5610 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5611 }
5612 sregs += 1;
5613 if ((sregs & -sregs) != sregs)
5b7c81bd 5614 return false;
a1d78564
RS
5615
5616 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5b7c81bd 5617 return true;
a1d78564
RS
5618}
5619
364215c8
RS
5620/* OP_ENTRY_EXIT_LIST matcher. */
5621
a92713e6 5622static unsigned int
364215c8 5623match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5624 const struct mips_operand *operand)
364215c8
RS
5625{
5626 unsigned int mask;
5b7c81bd 5627 bool is_exit;
364215c8
RS
5628
5629 /* The format is the same for both ENTRY and EXIT, but the constraints
5630 are different. */
5631 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5632 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5633 do
364215c8
RS
5634 {
5635 unsigned int regno1, regno2;
5b7c81bd 5636 bool is_freg;
364215c8 5637
a92713e6 5638 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5639 is_freg = false;
a92713e6 5640 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5b7c81bd 5641 is_freg = true;
364215c8 5642 else
5b7c81bd 5643 return false;
364215c8
RS
5644
5645 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5646 {
5647 mask &= ~(7 << 3);
5648 mask |= (5 + regno2) << 3;
5649 }
5650 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5651 mask |= (regno2 - 3) << 3;
5652 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5653 mask |= (regno2 - 15) << 1;
5654 else if (regno1 == RA && regno2 == RA)
5655 mask |= 1;
5656 else
5b7c81bd 5657 return false;
364215c8 5658 }
a92713e6
RS
5659 while (match_char (arg, ','));
5660
364215c8 5661 insn_insert_operand (arg->insn, operand, mask);
5b7c81bd 5662 return true;
364215c8
RS
5663}
5664
38bf472a
MR
5665/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5666 the argument register mask AMASK, the number of static registers
5667 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5668 respectively, and the frame size FRAME_SIZE. */
5669
5670static unsigned int
5671mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5672 unsigned int ra, unsigned int s0, unsigned int s1,
5673 unsigned int frame_size)
5674{
5675 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5676 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5677}
5678
5679/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5680 argument register mask AMASK, the number of static registers saved
5681 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5682 respectively, and the frame size FRAME_SIZE. */
5683
5684static unsigned int
5685mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5686 unsigned int ra, unsigned int s0, unsigned int s1,
5687 unsigned int frame_size)
5688{
5689 unsigned int args;
5690
5691 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5692 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5693 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5694 | ((frame_size & 0xf0) << 16));
5695 return args;
5696}
5697
364215c8
RS
5698/* OP_SAVE_RESTORE_LIST matcher. */
5699
5b7c81bd 5700static bool
a92713e6 5701match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5702{
5703 unsigned int opcode, args, statics, sregs;
5704 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5705 unsigned int arg_mask, ra, s0, s1;
364215c8 5706 offsetT frame_size;
364215c8 5707
364215c8
RS
5708 opcode = arg->insn->insn_opcode;
5709 frame_size = 0;
5710 num_frame_sizes = 0;
5711 args = 0;
5712 statics = 0;
5713 sregs = 0;
38bf472a
MR
5714 ra = 0;
5715 s0 = 0;
5716 s1 = 0;
a92713e6 5717 do
364215c8
RS
5718 {
5719 unsigned int regno1, regno2;
5720
a92713e6 5721 if (arg->token->type == OT_INTEGER)
364215c8
RS
5722 {
5723 /* Handle the frame size. */
1a00e612 5724 if (!match_const_int (arg, &frame_size))
5b7c81bd 5725 return false;
364215c8 5726 num_frame_sizes += 1;
364215c8
RS
5727 }
5728 else
5729 {
a92713e6 5730 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5731 return false;
364215c8
RS
5732
5733 while (regno1 <= regno2)
5734 {
5735 if (regno1 >= 4 && regno1 <= 7)
5736 {
5737 if (num_frame_sizes == 0)
5738 /* args $a0-$a3 */
5739 args |= 1 << (regno1 - 4);
5740 else
5741 /* statics $a0-$a3 */
5742 statics |= 1 << (regno1 - 4);
5743 }
5744 else if (regno1 >= 16 && regno1 <= 23)
5745 /* $s0-$s7 */
5746 sregs |= 1 << (regno1 - 16);
5747 else if (regno1 == 30)
5748 /* $s8 */
5749 sregs |= 1 << 8;
5750 else if (regno1 == 31)
5751 /* Add $ra to insn. */
38bf472a 5752 ra = 1;
364215c8 5753 else
5b7c81bd 5754 return false;
364215c8
RS
5755 regno1 += 1;
5756 if (regno1 == 24)
5757 regno1 = 30;
5758 }
5759 }
364215c8 5760 }
a92713e6 5761 while (match_char (arg, ','));
364215c8
RS
5762
5763 /* Encode args/statics combination. */
5764 if (args & statics)
5b7c81bd 5765 return false;
364215c8
RS
5766 else if (args == 0xf)
5767 /* All $a0-$a3 are args. */
38bf472a 5768 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5769 else if (statics == 0xf)
5770 /* All $a0-$a3 are statics. */
38bf472a 5771 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5772 else
5773 {
5774 /* Count arg registers. */
5775 num_args = 0;
5776 while (args & 0x1)
5777 {
5778 args >>= 1;
5779 num_args += 1;
5780 }
5781 if (args != 0)
5b7c81bd 5782 return false;
364215c8
RS
5783
5784 /* Count static registers. */
5785 num_statics = 0;
5786 while (statics & 0x8)
5787 {
5788 statics = (statics << 1) & 0xf;
5789 num_statics += 1;
5790 }
5791 if (statics != 0)
5b7c81bd 5792 return false;
364215c8
RS
5793
5794 /* Encode args/statics. */
38bf472a 5795 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5796 }
5797
5798 /* Encode $s0/$s1. */
5799 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5800 s0 = 1;
364215c8 5801 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5802 s1 = 1;
364215c8
RS
5803 sregs >>= 2;
5804
5805 /* Encode $s2-$s8. */
5806 num_sregs = 0;
5807 while (sregs & 1)
5808 {
5809 sregs >>= 1;
5810 num_sregs += 1;
5811 }
5812 if (sregs != 0)
5b7c81bd 5813 return false;
364215c8
RS
5814
5815 /* Encode frame size. */
5816 if (num_frame_sizes == 0)
1a00e612
RS
5817 {
5818 set_insn_error (arg->argnum, _("missing frame size"));
5b7c81bd 5819 return false;
1a00e612
RS
5820 }
5821 if (num_frame_sizes > 1)
5822 {
5823 set_insn_error (arg->argnum, _("frame size specified twice"));
5b7c81bd 5824 return false;
1a00e612
RS
5825 }
5826 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5827 {
5828 set_insn_error (arg->argnum, _("invalid frame size"));
5b7c81bd 5829 return false;
1a00e612 5830 }
38bf472a 5831 frame_size /= 8;
364215c8 5832
364215c8 5833 /* Finally build the instruction. */
38bf472a
MR
5834 if (mips_opts.mips16)
5835 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5836 frame_size);
5837 else if (!mips_opts.micromips)
5838 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5839 frame_size);
5840 else
5841 abort ();
5842
364215c8 5843 arg->insn->insn_opcode = opcode;
5b7c81bd 5844 return true;
364215c8
RS
5845}
5846
a1d78564
RS
5847/* OP_MDMX_IMM_REG matcher. */
5848
5b7c81bd 5849static bool
a1d78564 5850match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5851 const struct mips_operand *operand)
a1d78564 5852{
a92713e6 5853 unsigned int regno, uval;
5b7c81bd 5854 bool is_qh;
a1d78564
RS
5855 const struct mips_opcode *opcode;
5856
5857 /* The mips_opcode records whether this is an octobyte or quadhalf
5858 instruction. Start out with that bit in place. */
5859 opcode = arg->insn->insn_mo;
5860 uval = mips_extract_operand (operand, opcode->match);
5861 is_qh = (uval != 0);
5862
56d438b1 5863 if (arg->token->type == OT_REG)
a1d78564
RS
5864 {
5865 if ((opcode->membership & INSN_5400)
5866 && strcmp (opcode->name, "rzu.ob") == 0)
5867 {
1a00e612
RS
5868 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5869 arg->argnum);
5b7c81bd 5870 return false;
a1d78564
RS
5871 }
5872
56d438b1 5873 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5b7c81bd 5874 return false;
56d438b1
CF
5875 ++arg->token;
5876
a1d78564
RS
5877 /* Check whether this is a vector register or a broadcast of
5878 a single element. */
56d438b1 5879 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5880 {
56d438b1 5881 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5882 {
1a00e612 5883 set_insn_error (arg->argnum, _("invalid element selector"));
5b7c81bd 5884 return false;
a1d78564 5885 }
56d438b1
CF
5886 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5887 ++arg->token;
a1d78564
RS
5888 }
5889 else
5890 {
5891 /* A full vector. */
5892 if ((opcode->membership & INSN_5400)
5893 && (strcmp (opcode->name, "sll.ob") == 0
5894 || strcmp (opcode->name, "srl.ob") == 0))
5895 {
1a00e612
RS
5896 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5897 arg->argnum);
5b7c81bd 5898 return false;
a1d78564
RS
5899 }
5900
5901 if (is_qh)
5902 uval |= MDMX_FMTSEL_VEC_QH << 5;
5903 else
5904 uval |= MDMX_FMTSEL_VEC_OB << 5;
5905 }
a1d78564
RS
5906 uval |= regno;
5907 }
5908 else
5909 {
5910 offsetT sval;
5911
1a00e612 5912 if (!match_const_int (arg, &sval))
5b7c81bd 5913 return false;
a1d78564
RS
5914 if (sval < 0 || sval > 31)
5915 {
1a00e612 5916 match_out_of_range (arg);
5b7c81bd 5917 return false;
a1d78564
RS
5918 }
5919 uval |= (sval & 31);
5920 if (is_qh)
5921 uval |= MDMX_FMTSEL_IMM_QH << 5;
5922 else
5923 uval |= MDMX_FMTSEL_IMM_OB << 5;
5924 }
5925 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 5926 return true;
a1d78564
RS
5927}
5928
56d438b1
CF
5929/* OP_IMM_INDEX matcher. */
5930
5b7c81bd 5931static bool
56d438b1
CF
5932match_imm_index_operand (struct mips_arg_info *arg,
5933 const struct mips_operand *operand)
5934{
5935 unsigned int max_val;
5936
5937 if (arg->token->type != OT_INTEGER_INDEX)
5b7c81bd 5938 return false;
56d438b1
CF
5939
5940 max_val = (1 << operand->size) - 1;
5941 if (arg->token->u.index > max_val)
5942 {
5943 match_out_of_range (arg);
5b7c81bd 5944 return false;
56d438b1
CF
5945 }
5946 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5947 ++arg->token;
5b7c81bd 5948 return true;
56d438b1
CF
5949}
5950
5951/* OP_REG_INDEX matcher. */
5952
5b7c81bd 5953static bool
56d438b1
CF
5954match_reg_index_operand (struct mips_arg_info *arg,
5955 const struct mips_operand *operand)
5956{
5957 unsigned int regno;
5958
5959 if (arg->token->type != OT_REG_INDEX)
5b7c81bd 5960 return false;
56d438b1
CF
5961
5962 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5b7c81bd 5963 return false;
56d438b1
CF
5964
5965 insn_insert_operand (arg->insn, operand, regno);
5966 ++arg->token;
5b7c81bd 5967 return true;
56d438b1
CF
5968}
5969
a1d78564
RS
5970/* OP_PC matcher. */
5971
5b7c81bd 5972static bool
a92713e6 5973match_pc_operand (struct mips_arg_info *arg)
a1d78564 5974{
a92713e6
RS
5975 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5976 {
5977 ++arg->token;
5b7c81bd 5978 return true;
a92713e6 5979 }
5b7c81bd 5980 return false;
a1d78564
RS
5981}
5982
25499ac7
MR
5983/* OP_REG28 matcher. */
5984
5b7c81bd 5985static bool
25499ac7
MR
5986match_reg28_operand (struct mips_arg_info *arg)
5987{
5988 unsigned int regno;
5989
5990 if (arg->token->type == OT_REG
5991 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5992 && regno == GP)
5993 {
5994 ++arg->token;
5b7c81bd 5995 return true;
25499ac7 5996 }
5b7c81bd 5997 return false;
25499ac7
MR
5998}
5999
7361da2c
AB
6000/* OP_NON_ZERO_REG matcher. */
6001
5b7c81bd 6002static bool
7361da2c
AB
6003match_non_zero_reg_operand (struct mips_arg_info *arg,
6004 const struct mips_operand *operand)
6005{
6006 unsigned int regno;
6007
6008 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 6009 return false;
7361da2c
AB
6010
6011 if (regno == 0)
85bec12d
MF
6012 {
6013 set_insn_error (arg->argnum, _("the source register must not be $0"));
5b7c81bd 6014 return false;
85bec12d 6015 }
7361da2c
AB
6016
6017 arg->last_regno = regno;
6018 insn_insert_operand (arg->insn, operand, regno);
5b7c81bd 6019 return true;
7361da2c
AB
6020}
6021
a1d78564
RS
6022/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6023 register that we need to match. */
6024
5b7c81bd 6025static bool
a92713e6 6026match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6027{
6028 unsigned int regno;
6029
a92713e6 6030 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6031}
6032
33f46696
MR
6033/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6034 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6035 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6036
6037 Return the constant in IMM and OFFSET as follows:
6038
6039 - If the constant should be loaded via memory, set IMM to O_absent and
6040 OFFSET to the memory address.
6041
6042 - Otherwise, if the constant should be loaded into two 32-bit registers,
6043 set IMM to the O_constant to load into the high register and OFFSET
6044 to the corresponding value for the low register.
6045
6046 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6047
6048 These constants only appear as the last operand in an instruction,
6049 and every instruction that accepts them in any variant accepts them
6050 in all variants. This means we don't have to worry about backing out
6051 any changes if the instruction does not match. We just match
6052 unconditionally and report an error if the constant is invalid. */
6053
5b7c81bd 6054static bool
a92713e6 6055match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5b7c81bd 6056 expressionS *offset, int length, bool using_gprs)
89565f1b 6057{
a92713e6 6058 char *p;
89565f1b
RS
6059 segT seg, new_seg;
6060 subsegT subseg;
6061 const char *newname;
a92713e6 6062 unsigned char *data;
89565f1b
RS
6063
6064 /* Where the constant is placed is based on how the MIPS assembler
6065 does things:
6066
6067 length == 4 && using_gprs -- immediate value only
6068 length == 8 && using_gprs -- .rdata or immediate value
6069 length == 4 && !using_gprs -- .lit4 or immediate value
6070 length == 8 && !using_gprs -- .lit8 or immediate value
6071
6072 The .lit4 and .lit8 sections are only used if permitted by the
6073 -G argument. */
a92713e6 6074 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6075 {
6076 set_insn_error (arg->argnum, _("floating-point expression required"));
5b7c81bd 6077 return false;
1a00e612 6078 }
a92713e6
RS
6079
6080 gas_assert (arg->token->u.flt.length == length);
6081 data = arg->token->u.flt.data;
6082 ++arg->token;
89565f1b
RS
6083
6084 /* Handle 32-bit constants for which an immediate value is best. */
6085 if (length == 4
6086 && (using_gprs
6087 || g_switch_value < 4
6088 || (data[0] == 0 && data[1] == 0)
6089 || (data[2] == 0 && data[3] == 0)))
6090 {
6091 imm->X_op = O_constant;
6092 if (!target_big_endian)
6093 imm->X_add_number = bfd_getl32 (data);
6094 else
6095 imm->X_add_number = bfd_getb32 (data);
6096 offset->X_op = O_absent;
5b7c81bd 6097 return true;
89565f1b
RS
6098 }
6099
6100 /* Handle 64-bit constants for which an immediate value is best. */
6101 if (length == 8
6102 && !mips_disable_float_construction
351cdf24
MF
6103 /* Constants can only be constructed in GPRs and copied to FPRs if the
6104 GPRs are at least as wide as the FPRs or MTHC1 is available.
6105 Unlike most tests for 32-bit floating-point registers this check
6106 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6107 permit 64-bit moves without MXHC1.
6108 Force the constant into memory otherwise. */
6109 && (using_gprs
6110 || GPR_SIZE == 64
6111 || ISA_HAS_MXHC1 (mips_opts.isa)
6112 || FPR_SIZE == 32)
89565f1b
RS
6113 && ((data[0] == 0 && data[1] == 0)
6114 || (data[2] == 0 && data[3] == 0))
6115 && ((data[4] == 0 && data[5] == 0)
6116 || (data[6] == 0 && data[7] == 0)))
6117 {
6118 /* The value is simple enough to load with a couple of instructions.
6119 If using 32-bit registers, set IMM to the high order 32 bits and
6120 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6121 64 bit constant. */
351cdf24 6122 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6123 {
6124 imm->X_op = O_constant;
6125 offset->X_op = O_constant;
6126 if (!target_big_endian)
6127 {
6128 imm->X_add_number = bfd_getl32 (data + 4);
6129 offset->X_add_number = bfd_getl32 (data);
6130 }
6131 else
6132 {
6133 imm->X_add_number = bfd_getb32 (data);
6134 offset->X_add_number = bfd_getb32 (data + 4);
6135 }
6136 if (offset->X_add_number == 0)
6137 offset->X_op = O_absent;
6138 }
6139 else
6140 {
6141 imm->X_op = O_constant;
6142 if (!target_big_endian)
6143 imm->X_add_number = bfd_getl64 (data);
6144 else
6145 imm->X_add_number = bfd_getb64 (data);
6146 offset->X_op = O_absent;
6147 }
5b7c81bd 6148 return true;
89565f1b
RS
6149 }
6150
6151 /* Switch to the right section. */
6152 seg = now_seg;
6153 subseg = now_subseg;
6154 if (length == 4)
6155 {
6156 gas_assert (!using_gprs && g_switch_value >= 4);
6157 newname = ".lit4";
6158 }
6159 else
6160 {
6161 if (using_gprs || g_switch_value < 8)
6162 newname = RDATA_SECTION_NAME;
6163 else
6164 newname = ".lit8";
6165 }
6166
6167 new_seg = subseg_new (newname, (subsegT) 0);
fd361982 6168 bfd_set_section_flags (new_seg,
89565f1b
RS
6169 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6170 frag_align (length == 4 ? 2 : 3, 0, 0);
d34049e8 6171 if (!startswith (TARGET_OS, "elf"))
89565f1b
RS
6172 record_alignment (new_seg, 4);
6173 else
6174 record_alignment (new_seg, length == 4 ? 2 : 3);
6175 if (seg == now_seg)
1661c76c 6176 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6177
6178 /* Set the argument to the current address in the section. */
6179 imm->X_op = O_absent;
6180 offset->X_op = O_symbol;
6181 offset->X_add_symbol = symbol_temp_new_now ();
6182 offset->X_add_number = 0;
6183
6184 /* Put the floating point number into the section. */
6185 p = frag_more (length);
6186 memcpy (p, data, length);
6187
6188 /* Switch back to the original section. */
6189 subseg_set (seg, subseg);
5b7c81bd 6190 return true;
89565f1b
RS
6191}
6192
14daeee3
RS
6193/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6194 them. */
6195
5b7c81bd 6196static bool
14daeee3
RS
6197match_vu0_suffix_operand (struct mips_arg_info *arg,
6198 const struct mips_operand *operand,
5b7c81bd 6199 bool match_p)
14daeee3
RS
6200{
6201 unsigned int uval;
6202
6203 /* The operand can be an XYZW mask or a single 2-bit channel index
6204 (with X being 0). */
6205 gas_assert (operand->size == 2 || operand->size == 4);
6206
ee5734f0 6207 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6208 if (arg->token->type != OT_CHANNELS)
ee5734f0 6209 return match_p;
14daeee3
RS
6210
6211 uval = arg->token->u.channels;
6212 if (operand->size == 2)
6213 {
6214 /* Check that a single bit is set and convert it into a 2-bit index. */
6215 if ((uval & -uval) != uval)
5b7c81bd 6216 return false;
14daeee3
RS
6217 uval = 4 - ffs (uval);
6218 }
6219
6220 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5b7c81bd 6221 return false;
14daeee3
RS
6222
6223 ++arg->token;
6224 if (!match_p)
6225 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 6226 return true;
14daeee3
RS
6227}
6228
33f46696
MR
6229/* Try to match a token from ARG against OPERAND. Consume the token
6230 and return true on success, otherwise return false. */
a1d78564 6231
5b7c81bd 6232static bool
a1d78564 6233match_operand (struct mips_arg_info *arg,
a92713e6 6234 const struct mips_operand *operand)
a1d78564
RS
6235{
6236 switch (operand->type)
6237 {
6238 case OP_INT:
a92713e6 6239 return match_int_operand (arg, operand);
a1d78564
RS
6240
6241 case OP_MAPPED_INT:
a92713e6 6242 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6243
6244 case OP_MSB:
a92713e6 6245 return match_msb_operand (arg, operand);
a1d78564
RS
6246
6247 case OP_REG:
0f35dbc4 6248 case OP_OPTIONAL_REG:
a92713e6 6249 return match_reg_operand (arg, operand);
a1d78564
RS
6250
6251 case OP_REG_PAIR:
a92713e6 6252 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6253
6254 case OP_PCREL:
a92713e6 6255 return match_pcrel_operand (arg);
a1d78564
RS
6256
6257 case OP_PERF_REG:
a92713e6 6258 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6259
6260 case OP_ADDIUSP_INT:
a92713e6 6261 return match_addiusp_operand (arg, operand);
a1d78564
RS
6262
6263 case OP_CLO_CLZ_DEST:
a92713e6 6264 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6265
6266 case OP_LWM_SWM_LIST:
a92713e6 6267 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6268
6269 case OP_ENTRY_EXIT_LIST:
a92713e6 6270 return match_entry_exit_operand (arg, operand);
364215c8 6271
a1d78564 6272 case OP_SAVE_RESTORE_LIST:
a92713e6 6273 return match_save_restore_list_operand (arg);
a1d78564
RS
6274
6275 case OP_MDMX_IMM_REG:
a92713e6 6276 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6277
6278 case OP_REPEAT_DEST_REG:
a92713e6 6279 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6280
6281 case OP_REPEAT_PREV_REG:
a92713e6 6282 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6283
6284 case OP_PC:
a92713e6 6285 return match_pc_operand (arg);
14daeee3 6286
25499ac7
MR
6287 case OP_REG28:
6288 return match_reg28_operand (arg);
6289
14daeee3 6290 case OP_VU0_SUFFIX:
5b7c81bd 6291 return match_vu0_suffix_operand (arg, operand, false);
14daeee3
RS
6292
6293 case OP_VU0_MATCH_SUFFIX:
5b7c81bd 6294 return match_vu0_suffix_operand (arg, operand, true);
56d438b1
CF
6295
6296 case OP_IMM_INDEX:
6297 return match_imm_index_operand (arg, operand);
6298
6299 case OP_REG_INDEX:
6300 return match_reg_index_operand (arg, operand);
7361da2c
AB
6301
6302 case OP_SAME_RS_RT:
6303 return match_same_rs_rt_operand (arg, operand);
6304
6305 case OP_CHECK_PREV:
6306 return match_check_prev_operand (arg, operand);
6307
6308 case OP_NON_ZERO_REG:
6309 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6310 }
6311 abort ();
6312}
6313
6314/* ARG is the state after successfully matching an instruction.
6315 Issue any queued-up warnings. */
6316
6317static void
6318check_completed_insn (struct mips_arg_info *arg)
6319{
6320 if (arg->seen_at)
6321 {
6322 if (AT == ATREG)
1661c76c 6323 as_warn (_("used $at without \".set noat\""));
a1d78564 6324 else
1661c76c 6325 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6326 }
6327}
a1d78564 6328
85fcb30f
RS
6329/* Return true if modifying general-purpose register REG needs a delay. */
6330
5b7c81bd 6331static bool
85fcb30f
RS
6332reg_needs_delay (unsigned int reg)
6333{
6334 unsigned long prev_pinfo;
6335
6336 prev_pinfo = history[0].insn_mo->pinfo;
6337 if (!mips_opts.noreorder
67dc82bc 6338 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6339 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f 6340 && (gpr_write_mask (&history[0]) & (1 << reg)))
5b7c81bd 6341 return true;
85fcb30f 6342
5b7c81bd 6343 return false;
85fcb30f
RS
6344}
6345
71400594
RS
6346/* Classify an instruction according to the FIX_VR4120_* enumeration.
6347 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6348 by VR4120 errata. */
4d7206a2 6349
71400594
RS
6350static unsigned int
6351classify_vr4120_insn (const char *name)
252b5132 6352{
d34049e8 6353 if (startswith (name, "macc"))
71400594 6354 return FIX_VR4120_MACC;
d34049e8 6355 if (startswith (name, "dmacc"))
71400594 6356 return FIX_VR4120_DMACC;
d34049e8 6357 if (startswith (name, "mult"))
71400594 6358 return FIX_VR4120_MULT;
d34049e8 6359 if (startswith (name, "dmult"))
71400594
RS
6360 return FIX_VR4120_DMULT;
6361 if (strstr (name, "div"))
6362 return FIX_VR4120_DIV;
6363 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6364 return FIX_VR4120_MTHILO;
6365 return NUM_FIX_VR4120_CLASSES;
6366}
252b5132 6367
a8d14a88
CM
6368#define INSN_ERET 0x42000018
6369#define INSN_DERET 0x4200001f
6370#define INSN_DMULT 0x1c
6371#define INSN_DMULTU 0x1d
ff239038 6372
71400594
RS
6373/* Return the number of instructions that must separate INSN1 and INSN2,
6374 where INSN1 is the earlier instruction. Return the worst-case value
6375 for any INSN2 if INSN2 is null. */
252b5132 6376
71400594
RS
6377static unsigned int
6378insns_between (const struct mips_cl_insn *insn1,
6379 const struct mips_cl_insn *insn2)
6380{
6381 unsigned long pinfo1, pinfo2;
4c260379 6382 unsigned int mask;
71400594 6383
85fcb30f
RS
6384 /* If INFO2 is null, pessimistically assume that all flags are set for
6385 the second instruction. */
71400594
RS
6386 pinfo1 = insn1->insn_mo->pinfo;
6387 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6388
71400594
RS
6389 /* For most targets, write-after-read dependencies on the HI and LO
6390 registers must be separated by at least two instructions. */
6391 if (!hilo_interlocks)
252b5132 6392 {
71400594
RS
6393 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6394 return 2;
6395 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6396 return 2;
6397 }
6398
6399 /* If we're working around r7000 errata, there must be two instructions
6400 between an mfhi or mflo and any instruction that uses the result. */
6401 if (mips_7000_hilo_fix
df58fc94 6402 && !mips_opts.micromips
71400594 6403 && MF_HILO_INSN (pinfo1)
85fcb30f 6404 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6405 return 2;
6406
ff239038
CM
6407 /* If we're working around 24K errata, one instruction is required
6408 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6409 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6410 {
6411 if (insn1->insn_opcode == INSN_ERET
6412 || insn1->insn_opcode == INSN_DERET)
6413 {
6414 if (insn2 == NULL
6415 || insn2->insn_opcode == INSN_ERET
6416 || insn2->insn_opcode == INSN_DERET
11625dd8 6417 || delayed_branch_p (insn2))
ff239038
CM
6418 return 1;
6419 }
6420 }
6421
a8d14a88
CM
6422 /* If we're working around PMC RM7000 errata, there must be three
6423 nops between a dmult and a load instruction. */
6424 if (mips_fix_rm7000 && !mips_opts.micromips)
6425 {
6426 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6427 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6428 {
6429 if (pinfo2 & INSN_LOAD_MEMORY)
6430 return 3;
6431 }
6432 }
6433
71400594
RS
6434 /* If working around VR4120 errata, check for combinations that need
6435 a single intervening instruction. */
df58fc94 6436 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6437 {
6438 unsigned int class1, class2;
252b5132 6439
71400594
RS
6440 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6441 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6442 {
71400594
RS
6443 if (insn2 == NULL)
6444 return 1;
6445 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6446 if (vr4120_conflicts[class1] & (1 << class2))
6447 return 1;
252b5132 6448 }
71400594
RS
6449 }
6450
df58fc94 6451 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6452 {
6453 /* Check for GPR or coprocessor load delays. All such delays
6454 are on the RT register. */
6455 /* Itbl support may require additional care here. */
67dc82bc 6456 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6457 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6458 {
85fcb30f 6459 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6460 return 1;
6461 }
6462
6463 /* Check for generic coprocessor hazards.
6464
6465 This case is not handled very well. There is no special
6466 knowledge of CP0 handling, and the coprocessors other than
6467 the floating point unit are not distinguished at all. */
6468 /* Itbl support may require additional care here. FIXME!
6469 Need to modify this to include knowledge about
6470 user specified delays! */
43885403 6471 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6472 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6473 {
6474 /* Handle cases where INSN1 writes to a known general coprocessor
6475 register. There must be a one instruction delay before INSN2
6476 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6477 mask = fpr_write_mask (insn1);
6478 if (mask != 0)
252b5132 6479 {
4c260379 6480 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6481 return 1;
252b5132
RH
6482 }
6483 else
6484 {
71400594
RS
6485 /* Read-after-write dependencies on the control registers
6486 require a two-instruction gap. */
6487 if ((pinfo1 & INSN_WRITE_COND_CODE)
6488 && (pinfo2 & INSN_READ_COND_CODE))
6489 return 2;
6490
6491 /* We don't know exactly what INSN1 does. If INSN2 is
6492 also a coprocessor instruction, assume there must be
6493 a one instruction gap. */
6494 if (pinfo2 & INSN_COP)
6495 return 1;
252b5132
RH
6496 }
6497 }
6b76fefe 6498
71400594
RS
6499 /* Check for read-after-write dependencies on the coprocessor
6500 control registers in cases where INSN1 does not need a general
6501 coprocessor delay. This means that INSN1 is a floating point
6502 comparison instruction. */
6503 /* Itbl support may require additional care here. */
6504 else if (!cop_interlocks
6505 && (pinfo1 & INSN_WRITE_COND_CODE)
6506 && (pinfo2 & INSN_READ_COND_CODE))
6507 return 1;
6508 }
6b76fefe 6509
7361da2c
AB
6510 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6511 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6512 and pause. */
6513 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6514 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6515 || (insn2 && delayed_branch_p (insn2))))
6516 return 1;
6517
71400594
RS
6518 return 0;
6519}
6b76fefe 6520
7d8e00cf
RS
6521/* Return the number of nops that would be needed to work around the
6522 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6523 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6524 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6525
6526static int
932d1a1b 6527nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6528 const struct mips_cl_insn *insn)
6529{
4c260379
RS
6530 int i, j;
6531 unsigned int mask;
7d8e00cf
RS
6532
6533 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6534 are not affected by the errata. */
6535 if (insn != 0
6536 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6537 || strcmp (insn->insn_mo->name, "mtlo") == 0
6538 || strcmp (insn->insn_mo->name, "mthi") == 0))
6539 return 0;
6540
6541 /* Search for the first MFLO or MFHI. */
6542 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6543 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6544 {
6545 /* Extract the destination register. */
4c260379 6546 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6547
6548 /* No nops are needed if INSN reads that register. */
4c260379 6549 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6550 return 0;
6551
6552 /* ...or if any of the intervening instructions do. */
6553 for (j = 0; j < i; j++)
4c260379 6554 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6555 return 0;
6556
932d1a1b
RS
6557 if (i >= ignore)
6558 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6559 }
6560 return 0;
6561}
6562
134c0c8b
MR
6563#define BASE_REG_EQ(INSN1, INSN2) \
6564 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6565 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6566
6567/* Return the minimum alignment for this store instruction. */
6568
6569static int
6570fix_24k_align_to (const struct mips_opcode *mo)
6571{
6572 if (strcmp (mo->name, "sh") == 0)
6573 return 2;
6574
6575 if (strcmp (mo->name, "swc1") == 0
6576 || strcmp (mo->name, "swc2") == 0
6577 || strcmp (mo->name, "sw") == 0
6578 || strcmp (mo->name, "sc") == 0
6579 || strcmp (mo->name, "s.s") == 0)
6580 return 4;
6581
6582 if (strcmp (mo->name, "sdc1") == 0
6583 || strcmp (mo->name, "sdc2") == 0
6584 || strcmp (mo->name, "s.d") == 0)
6585 return 8;
6586
6587 /* sb, swl, swr */
6588 return 1;
6589}
6590
6591struct fix_24k_store_info
6592 {
6593 /* Immediate offset, if any, for this store instruction. */
6594 short off;
6595 /* Alignment required by this store instruction. */
6596 int align_to;
6597 /* True for register offsets. */
6598 int register_offset;
6599 };
6600
6601/* Comparison function used by qsort. */
6602
6603static int
6604fix_24k_sort (const void *a, const void *b)
6605{
6606 const struct fix_24k_store_info *pos1 = a;
6607 const struct fix_24k_store_info *pos2 = b;
6608
6609 return (pos1->off - pos2->off);
6610}
6611
6612/* INSN is a store instruction. Try to record the store information
6613 in STINFO. Return false if the information isn't known. */
6614
5b7c81bd 6615static bool
15be625d 6616fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6617 const struct mips_cl_insn *insn)
15be625d
CM
6618{
6619 /* The instruction must have a known offset. */
6620 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5b7c81bd 6621 return false;
15be625d
CM
6622
6623 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6624 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5b7c81bd 6625 return true;
15be625d
CM
6626}
6627
932d1a1b
RS
6628/* Return the number of nops that would be needed to work around the 24k
6629 "lost data on stores during refill" errata if instruction INSN
6630 immediately followed the 2 instructions described by HIST.
6631 Ignore hazards that are contained within the first IGNORE
6632 instructions of HIST.
6633
6634 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6635 for the data cache refills and store data. The following describes
6636 the scenario where the store data could be lost.
6637
6638 * A data cache miss, due to either a load or a store, causing fill
6639 data to be supplied by the memory subsystem
6640 * The first three doublewords of fill data are returned and written
6641 into the cache
6642 * A sequence of four stores occurs in consecutive cycles around the
6643 final doubleword of the fill:
6644 * Store A
6645 * Store B
6646 * Store C
6647 * Zero, One or more instructions
6648 * Store D
6649
6650 The four stores A-D must be to different doublewords of the line that
6651 is being filled. The fourth instruction in the sequence above permits
6652 the fill of the final doubleword to be transferred from the FSB into
6653 the cache. In the sequence above, the stores may be either integer
6654 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6655 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6656 different doublewords on the line. If the floating point unit is
6657 running in 1:2 mode, it is not possible to create the sequence above
6658 using only floating point store instructions.
15be625d
CM
6659
6660 In this case, the cache line being filled is incorrectly marked
6661 invalid, thereby losing the data from any store to the line that
6662 occurs between the original miss and the completion of the five
6663 cycle sequence shown above.
6664
932d1a1b 6665 The workarounds are:
15be625d 6666
932d1a1b
RS
6667 * Run the data cache in write-through mode.
6668 * Insert a non-store instruction between
6669 Store A and Store B or Store B and Store C. */
3739860c 6670
15be625d 6671static int
932d1a1b 6672nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6673 const struct mips_cl_insn *insn)
6674{
6675 struct fix_24k_store_info pos[3];
6676 int align, i, base_offset;
6677
932d1a1b
RS
6678 if (ignore >= 2)
6679 return 0;
6680
ab9794cf
RS
6681 /* If the previous instruction wasn't a store, there's nothing to
6682 worry about. */
15be625d
CM
6683 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6684 return 0;
6685
ab9794cf
RS
6686 /* If the instructions after the previous one are unknown, we have
6687 to assume the worst. */
6688 if (!insn)
15be625d
CM
6689 return 1;
6690
ab9794cf
RS
6691 /* Check whether we are dealing with three consecutive stores. */
6692 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6693 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6694 return 0;
6695
6696 /* If we don't know the relationship between the store addresses,
6697 assume the worst. */
ab9794cf 6698 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6699 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6700 return 1;
6701
6702 if (!fix_24k_record_store_info (&pos[0], insn)
6703 || !fix_24k_record_store_info (&pos[1], &hist[0])
6704 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6705 return 1;
6706
6707 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6708
6709 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6710 X bytes and such that the base register + X is known to be aligned
6711 to align bytes. */
6712
6713 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6714 align = 8;
6715 else
6716 {
6717 align = pos[0].align_to;
6718 base_offset = pos[0].off;
6719 for (i = 1; i < 3; i++)
6720 if (align < pos[i].align_to)
6721 {
6722 align = pos[i].align_to;
6723 base_offset = pos[i].off;
6724 }
6725 for (i = 0; i < 3; i++)
6726 pos[i].off -= base_offset;
6727 }
6728
6729 pos[0].off &= ~align + 1;
6730 pos[1].off &= ~align + 1;
6731 pos[2].off &= ~align + 1;
6732
6733 /* If any two stores write to the same chunk, they also write to the
6734 same doubleword. The offsets are still sorted at this point. */
6735 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6736 return 0;
6737
6738 /* A range of at least 9 bytes is needed for the stores to be in
6739 non-overlapping doublewords. */
6740 if (pos[2].off - pos[0].off <= 8)
6741 return 0;
6742
6743 if (pos[2].off - pos[1].off >= 24
6744 || pos[1].off - pos[0].off >= 24
6745 || pos[2].off - pos[0].off >= 32)
6746 return 0;
6747
6748 return 1;
6749}
6750
71400594 6751/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6752 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6753 where HIST[0] is the most recent instruction. Ignore hazards
6754 between INSN and the first IGNORE instructions in HIST.
6755
6756 If INSN is null, return the worse-case number of nops for any
6757 instruction. */
bdaaa2e1 6758
71400594 6759static int
932d1a1b 6760nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6761 const struct mips_cl_insn *insn)
6762{
6763 int i, nops, tmp_nops;
bdaaa2e1 6764
71400594 6765 nops = 0;
932d1a1b 6766 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6767 {
91d6fa6a 6768 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6769 if (tmp_nops > nops)
6770 nops = tmp_nops;
6771 }
7d8e00cf 6772
df58fc94 6773 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6774 {
932d1a1b 6775 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6776 if (tmp_nops > nops)
6777 nops = tmp_nops;
6778 }
6779
df58fc94 6780 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6781 {
932d1a1b 6782 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6783 if (tmp_nops > nops)
6784 nops = tmp_nops;
6785 }
6786
71400594
RS
6787 return nops;
6788}
252b5132 6789
71400594 6790/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6791 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6792 would be needed after the extended sequence, ignoring hazards
6793 in the first IGNORE instructions. */
252b5132 6794
71400594 6795static int
932d1a1b
RS
6796nops_for_sequence (int num_insns, int ignore,
6797 const struct mips_cl_insn *hist, ...)
71400594
RS
6798{
6799 va_list args;
6800 struct mips_cl_insn buffer[MAX_NOPS];
6801 struct mips_cl_insn *cursor;
6802 int nops;
6803
91d6fa6a 6804 va_start (args, hist);
71400594 6805 cursor = buffer + num_insns;
91d6fa6a 6806 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6807 while (cursor > buffer)
6808 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6809
932d1a1b 6810 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6811 va_end (args);
6812 return nops;
6813}
252b5132 6814
71400594
RS
6815/* Like nops_for_insn, but if INSN is a branch, take into account the
6816 worst-case delay for the branch target. */
252b5132 6817
71400594 6818static int
932d1a1b 6819nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6820 const struct mips_cl_insn *insn)
6821{
6822 int nops, tmp_nops;
60b63b72 6823
932d1a1b 6824 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6825 if (delayed_branch_p (insn))
71400594 6826 {
932d1a1b 6827 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6828 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6829 if (tmp_nops > nops)
6830 nops = tmp_nops;
6831 }
11625dd8 6832 else if (compact_branch_p (insn))
71400594 6833 {
932d1a1b 6834 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6835 if (tmp_nops > nops)
6836 nops = tmp_nops;
6837 }
6838 return nops;
6839}
6840
c67a084a
NC
6841/* Fix NOP issue: Replace nops by "or at,at,zero". */
6842
6843static void
6844fix_loongson2f_nop (struct mips_cl_insn * ip)
6845{
df58fc94 6846 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6847 if (strcmp (ip->insn_mo->name, "nop") == 0)
6848 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6849}
6850
6851/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6852 jr target pc &= 'hffff_ffff_cfff_ffff. */
6853
6854static void
6855fix_loongson2f_jump (struct mips_cl_insn * ip)
6856{
df58fc94 6857 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6858 if (strcmp (ip->insn_mo->name, "j") == 0
6859 || strcmp (ip->insn_mo->name, "jr") == 0
6860 || strcmp (ip->insn_mo->name, "jalr") == 0)
6861 {
6862 int sreg;
6863 expressionS ep;
6864
6865 if (! mips_opts.at)
6866 return;
6867
df58fc94 6868 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6869 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6870 return;
6871
6872 ep.X_op = O_constant;
6873 ep.X_add_number = 0xcfff0000;
6874 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6875 ep.X_add_number = 0xffff;
6876 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6877 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6878 }
6879}
6880
6881static void
6882fix_loongson2f (struct mips_cl_insn * ip)
6883{
6884 if (mips_fix_loongson2f_nop)
6885 fix_loongson2f_nop (ip);
6886
6887 if (mips_fix_loongson2f_jump)
6888 fix_loongson2f_jump (ip);
6889}
6890
5b7c81bd 6891static bool
dec7b24b
YS
6892has_label_name (const char *arr[], size_t len ,const char *s)
6893{
6894 unsigned long i;
6895 for (i = 0; i < len; i++)
6896 {
6897 if (!arr[i])
5b7c81bd 6898 return false;
dec7b24b 6899 if (streq (arr[i], s))
5b7c81bd 6900 return true;
dec7b24b 6901 }
5b7c81bd 6902 return false;
dec7b24b
YS
6903}
6904
6905/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6f2117ba
PH
6906
6907static void
6908fix_loongson3_llsc (struct mips_cl_insn * ip)
6909{
6910 gas_assert (!HAVE_CODE_COMPRESSION);
6911
6912 /* If is an local label and the insn is not sync,
6913 look forward that whether an branch between ll/sc jump to here
6914 if so, insert a sync. */
6915 if (seg_info (now_seg)->label_list
6916 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6917 && (strcmp (ip->insn_mo->name, "sync") != 0))
6918 {
6f2117ba 6919 unsigned long i;
dec7b24b
YS
6920 valueT label_value;
6921 const char *label_names[MAX_LABELS_SAME];
6922 const char *label_name;
6923
6924 label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6925 label_names[0] = label_name;
6926 struct insn_label_list *llist = seg_info (now_seg)->label_list;
6927 label_value = S_GET_VALUE (llist->label);
6f2117ba 6928
dec7b24b
YS
6929 for (i = 1; i < MAX_LABELS_SAME; i++)
6930 {
6931 llist = llist->next;
6932 if (!llist)
6933 break;
6934 if (S_GET_VALUE (llist->label) == label_value)
6935 label_names[i] = S_GET_NAME (llist->label);
6936 else
6937 break;
6938 }
6939 for (; i < MAX_LABELS_SAME; i++)
6940 label_names[i] = NULL;
6941
6942 unsigned long lookback = ARRAY_SIZE (history);
6f2117ba
PH
6943 for (i = 0; i < lookback; i++)
6944 {
6f2117ba
PH
6945 if (streq (history[i].insn_mo->name, "sc")
6946 || streq (history[i].insn_mo->name, "scd"))
6947 {
6948 unsigned long j;
6949
6950 for (j = i + 1; j < lookback; j++)
6951 {
af989297
YS
6952 if (streq (history[j].insn_mo->name, "ll")
6953 || streq (history[j].insn_mo->name, "lld"))
6f2117ba
PH
6954 break;
6955
6956 if (delayed_branch_p (&history[j]))
6957 {
dec7b24b
YS
6958 if (has_label_name (label_names,
6959 MAX_LABELS_SAME,
6960 history[j].target))
6f2117ba
PH
6961 {
6962 add_fixed_insn (&sync_insn);
6963 insert_into_history (0, 1, &sync_insn);
6964 i = lookback;
6965 break;
6966 }
6967 }
6968 }
6969 }
6970 }
6971 }
6972 /* If we find a sc, we look forward to look for an branch insn,
6973 and see whether it jump back and out of ll/sc. */
dec7b24b 6974 else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6f2117ba
PH
6975 {
6976 unsigned long lookback = ARRAY_SIZE (history) - 1;
6977 unsigned long i;
6978
6979 for (i = 0; i < lookback; i++)
6980 {
6981 if (streq (history[i].insn_mo->name, "ll")
6982 || streq (history[i].insn_mo->name, "lld"))
6983 break;
6984
6985 if (delayed_branch_p (&history[i]))
6986 {
6987 unsigned long j;
6988
6989 for (j = i + 1; j < lookback; j++)
6990 {
6991 if (streq (history[j].insn_mo->name, "ll")
af989297 6992 || streq (history[j].insn_mo->name, "lld"))
6f2117ba
PH
6993 break;
6994 }
6995
6996 for (; j < lookback; j++)
6997 {
6998 if (history[j].label[0] != '\0'
6999 && streq (history[j].label, history[i].target)
7000 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7001 {
7002 add_fixed_insn (&sync_insn);
7003 insert_into_history (++j, 1, &sync_insn);
7004 }
7005 }
7006 }
7007 }
7008 }
7009
7010 /* Skip if there is a sync before ll/lld. */
7011 if ((strcmp (ip->insn_mo->name, "ll") == 0
7012 || strcmp (ip->insn_mo->name, "lld") == 0)
7013 && (strcmp (history[0].insn_mo->name, "sync") != 0))
7014 {
7015 add_fixed_insn (&sync_insn);
7016 insert_into_history (0, 1, &sync_insn);
7017 }
7018}
7019
a4e06468
RS
7020/* IP is a branch that has a delay slot, and we need to fill it
7021 automatically. Return true if we can do that by swapping IP
e407c74b
NC
7022 with the previous instruction.
7023 ADDRESS_EXPR is an operand of the instruction to be used with
7024 RELOC_TYPE. */
a4e06468 7025
5b7c81bd 7026static bool
e407c74b 7027can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7028 bfd_reloc_code_real_type *reloc_type)
a4e06468 7029{
2b0c8b40 7030 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7031 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7032 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7033
7034 /* -O2 and above is required for this optimization. */
7035 if (mips_optimize < 2)
5b7c81bd 7036 return false;
a4e06468
RS
7037
7038 /* If we have seen .set volatile or .set nomove, don't optimize. */
7039 if (mips_opts.nomove)
5b7c81bd 7040 return false;
a4e06468
RS
7041
7042 /* We can't swap if the previous instruction's position is fixed. */
7043 if (history[0].fixed_p)
5b7c81bd 7044 return false;
a4e06468
RS
7045
7046 /* If the previous previous insn was in a .set noreorder, we can't
7047 swap. Actually, the MIPS assembler will swap in this situation.
7048 However, gcc configured -with-gnu-as will generate code like
7049
7050 .set noreorder
7051 lw $4,XXX
7052 .set reorder
7053 INSN
7054 bne $4,$0,foo
7055
7056 in which we can not swap the bne and INSN. If gcc is not configured
7057 -with-gnu-as, it does not output the .set pseudo-ops. */
7058 if (history[1].noreorder_p)
5b7c81bd 7059 return false;
a4e06468 7060
87333bb7
MR
7061 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7062 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468 7063 if (mips_opts.mips16 && history[0].fixp[0])
5b7c81bd 7064 return false;
a4e06468
RS
7065
7066 /* If the branch is itself the target of a branch, we can not swap.
7067 We cheat on this; all we check for is whether there is a label on
7068 this instruction. If there are any branches to anything other than
7069 a label, users must use .set noreorder. */
7070 if (seg_info (now_seg)->label_list)
5b7c81bd 7071 return false;
a4e06468
RS
7072
7073 /* If the previous instruction is in a variant frag other than this
2309ddf2 7074 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7075 MIPS16 code, which uses variant frags for different purposes. */
7076 if (!mips_opts.mips16
a4e06468
RS
7077 && history[0].frag
7078 && history[0].frag->fr_type == rs_machine_dependent)
5b7c81bd 7079 return false;
a4e06468 7080
bcd530a7
RS
7081 /* We do not swap with instructions that cannot architecturally
7082 be placed in a branch delay slot, such as SYNC or ERET. We
7083 also refrain from swapping with a trap instruction, since it
7084 complicates trap handlers to have the trap instruction be in
7085 a delay slot. */
a4e06468 7086 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7087 if (prev_pinfo & INSN_NO_DELAY_SLOT)
5b7c81bd 7088 return false;
a4e06468
RS
7089
7090 /* Check for conflicts between the branch and the instructions
7091 before the candidate delay slot. */
7092 if (nops_for_insn (0, history + 1, ip) > 0)
5b7c81bd 7093 return false;
a4e06468
RS
7094
7095 /* Check for conflicts between the swapped sequence and the
7096 target of the branch. */
7097 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5b7c81bd 7098 return false;
a4e06468
RS
7099
7100 /* If the branch reads a register that the previous
7101 instruction sets, we can not swap. */
7102 gpr_read = gpr_read_mask (ip);
7103 prev_gpr_write = gpr_write_mask (&history[0]);
7104 if (gpr_read & prev_gpr_write)
5b7c81bd 7105 return false;
a4e06468 7106
9d5de888
CF
7107 fpr_read = fpr_read_mask (ip);
7108 prev_fpr_write = fpr_write_mask (&history[0]);
7109 if (fpr_read & prev_fpr_write)
5b7c81bd 7110 return false;
9d5de888 7111
a4e06468
RS
7112 /* If the branch writes a register that the previous
7113 instruction sets, we can not swap. */
7114 gpr_write = gpr_write_mask (ip);
7115 if (gpr_write & prev_gpr_write)
5b7c81bd 7116 return false;
a4e06468
RS
7117
7118 /* If the branch writes a register that the previous
7119 instruction reads, we can not swap. */
7120 prev_gpr_read = gpr_read_mask (&history[0]);
7121 if (gpr_write & prev_gpr_read)
5b7c81bd 7122 return false;
a4e06468
RS
7123
7124 /* If one instruction sets a condition code and the
7125 other one uses a condition code, we can not swap. */
7126 pinfo = ip->insn_mo->pinfo;
7127 if ((pinfo & INSN_READ_COND_CODE)
7128 && (prev_pinfo & INSN_WRITE_COND_CODE))
5b7c81bd 7129 return false;
a4e06468
RS
7130 if ((pinfo & INSN_WRITE_COND_CODE)
7131 && (prev_pinfo & INSN_READ_COND_CODE))
5b7c81bd 7132 return false;
a4e06468
RS
7133
7134 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7135 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7136 if (prev_pinfo2 & INSN2_READ_PC)
5b7c81bd 7137 return false;
a4e06468 7138
df58fc94
RS
7139 /* If the previous instruction has an incorrect size for a fixed
7140 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7141 pinfo2 = ip->insn_mo->pinfo2;
7142 if (mips_opts.micromips
7143 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7144 && insn_length (history) != 2)
5b7c81bd 7145 return false;
2309ddf2
MR
7146 if (mips_opts.micromips
7147 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7148 && insn_length (history) != 4)
5b7c81bd 7149 return false;
2309ddf2 7150
33d64ca5
FN
7151 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7152 branch delay slot.
7153
7154 The short loop bug under certain conditions causes loops to execute
7155 only once or twice. We must ensure that the assembler never
7156 generates loops that satisfy all of the following conditions:
7157
7158 - a loop consists of less than or equal to six instructions
7159 (including the branch delay slot);
7160 - a loop contains only one conditional branch instruction at the end
7161 of the loop;
7162 - a loop does not contain any other branch or jump instructions;
7163 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7164
7165 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7166 if (mips_fix_r5900
e407c74b
NC
7167 /* Check if instruction has a parameter, ignore "j $31". */
7168 && (address_expr != NULL)
7169 /* Parameter must be 16 bit. */
7170 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7171 /* Branch to same segment. */
41065f5e 7172 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7173 /* Branch to same code fragment. */
41065f5e 7174 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7175 /* Can only calculate branch offset if value is known. */
41065f5e 7176 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7177 /* Check if branch is really conditional. */
7178 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7179 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7180 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7181 {
7182 int distance;
33d64ca5
FN
7183 /* Check if loop is shorter than or equal to 6 instructions
7184 including branch and delay slot. */
41065f5e 7185 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7186 if (distance <= 20)
7187 {
7188 int i;
7189 int rv;
7190
5b7c81bd 7191 rv = false;
e407c74b
NC
7192 /* When the loop includes branches or jumps,
7193 it is not a short loop. */
7194 for (i = 0; i < (distance / 4); i++)
7195 {
7196 if ((history[i].cleared_p)
41065f5e 7197 || delayed_branch_p (&history[i]))
e407c74b 7198 {
5b7c81bd 7199 rv = true;
e407c74b
NC
7200 break;
7201 }
7202 }
535b785f 7203 if (!rv)
e407c74b
NC
7204 {
7205 /* Insert nop after branch to fix short loop. */
5b7c81bd 7206 return false;
e407c74b
NC
7207 }
7208 }
7209 }
7210
5b7c81bd 7211 return true;
a4e06468
RS
7212}
7213
e407c74b
NC
7214/* Decide how we should add IP to the instruction stream.
7215 ADDRESS_EXPR is an operand of the instruction to be used with
7216 RELOC_TYPE. */
a4e06468
RS
7217
7218static enum append_method
e407c74b 7219get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7220 bfd_reloc_code_real_type *reloc_type)
a4e06468 7221{
a4e06468
RS
7222 /* The relaxed version of a macro sequence must be inherently
7223 hazard-free. */
7224 if (mips_relax.sequence == 2)
7225 return APPEND_ADD;
7226
3b821a28 7227 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7228 if (mips_opts.noreorder)
7229 return APPEND_ADD;
7230
7231 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7232 if (delayed_branch_p (ip))
a4e06468 7233 {
e407c74b
NC
7234 if (!branch_likely_p (ip)
7235 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7236 return APPEND_SWAP;
7237
7238 if (mips_opts.mips16
7239 && ISA_SUPPORTS_MIPS16E
fc76e730 7240 && gpr_read_mask (ip) != 0)
a4e06468
RS
7241 return APPEND_ADD_COMPACT;
7242
7bd374a4
MR
7243 if (mips_opts.micromips
7244 && ((ip->insn_opcode & 0xffe0) == 0x4580
7245 || (!forced_insn_length
7246 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7247 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7248 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7249 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7250 return APPEND_ADD_COMPACT;
7251
a4e06468
RS
7252 return APPEND_ADD_WITH_NOP;
7253 }
7254
a4e06468
RS
7255 return APPEND_ADD;
7256}
7257
7bd374a4
MR
7258/* IP is an instruction whose opcode we have just changed, END points
7259 to the end of the opcode table processed. Point IP->insn_mo to the
7260 new opcode's definition. */
ceb94aa5
RS
7261
7262static void
7bd374a4 7263find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7264{
7bd374a4 7265 const struct mips_opcode *mo;
ceb94aa5 7266
ceb94aa5 7267 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7268 if (mo->pinfo != INSN_MACRO
7269 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7270 {
7271 ip->insn_mo = mo;
7272 return;
7273 }
7274 abort ();
7275}
7276
7bd374a4
MR
7277/* IP is a MIPS16 instruction whose opcode we have just changed.
7278 Point IP->insn_mo to the new opcode's definition. */
7279
7280static void
7281find_altered_mips16_opcode (struct mips_cl_insn *ip)
7282{
7283 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7284}
7285
7286/* IP is a microMIPS instruction whose opcode we have just changed.
7287 Point IP->insn_mo to the new opcode's definition. */
7288
7289static void
7290find_altered_micromips_opcode (struct mips_cl_insn *ip)
7291{
7292 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7293}
7294
df58fc94
RS
7295/* For microMIPS macros, we need to generate a local number label
7296 as the target of branches. */
7297#define MICROMIPS_LABEL_CHAR '\037'
7298static unsigned long micromips_target_label;
7299static char micromips_target_name[32];
7300
7301static char *
7302micromips_label_name (void)
7303{
7304 char *p = micromips_target_name;
7305 char symbol_name_temporary[24];
7306 unsigned long l;
7307 int i;
7308
7309 if (*p)
7310 return p;
7311
7312 i = 0;
7313 l = micromips_target_label;
7314#ifdef LOCAL_LABEL_PREFIX
7315 *p++ = LOCAL_LABEL_PREFIX;
7316#endif
7317 *p++ = 'L';
7318 *p++ = MICROMIPS_LABEL_CHAR;
7319 do
7320 {
7321 symbol_name_temporary[i++] = l % 10 + '0';
7322 l /= 10;
7323 }
7324 while (l != 0);
7325 while (i > 0)
7326 *p++ = symbol_name_temporary[--i];
7327 *p = '\0';
7328
7329 return micromips_target_name;
7330}
7331
7332static void
7333micromips_label_expr (expressionS *label_expr)
7334{
7335 label_expr->X_op = O_symbol;
7336 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7337 label_expr->X_add_number = 0;
7338}
7339
7340static void
7341micromips_label_inc (void)
7342{
7343 micromips_target_label++;
7344 *micromips_target_name = '\0';
7345}
7346
7347static void
7348micromips_add_label (void)
7349{
7350 symbolS *s;
7351
7352 s = colon (micromips_label_name ());
7353 micromips_label_inc ();
f3ded42a 7354 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7355}
7356
7357/* If assembling microMIPS code, then return the microMIPS reloc
7358 corresponding to the requested one if any. Otherwise return
7359 the reloc unchanged. */
7360
7361static bfd_reloc_code_real_type
7362micromips_map_reloc (bfd_reloc_code_real_type reloc)
7363{
7364 static const bfd_reloc_code_real_type relocs[][2] =
7365 {
7366 /* Keep sorted incrementally by the left-hand key. */
7367 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7368 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7369 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7370 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7371 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7372 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7373 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7374 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7375 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7376 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7377 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7378 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7379 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7380 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7381 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7382 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7383 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7384 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7385 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7386 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7387 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7388 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7389 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7390 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7391 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7392 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7393 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7394 };
7395 bfd_reloc_code_real_type r;
7396 size_t i;
7397
7398 if (!mips_opts.micromips)
7399 return reloc;
7400 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7401 {
7402 r = relocs[i][0];
7403 if (r > reloc)
7404 return reloc;
7405 if (r == reloc)
7406 return relocs[i][1];
7407 }
7408 return reloc;
7409}
7410
b886a2ab
RS
7411/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7412 Return true on success, storing the resolved value in RESULT. */
7413
5b7c81bd 7414static bool
b886a2ab
RS
7415calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7416 offsetT *result)
7417{
7418 switch (reloc)
7419 {
7420 case BFD_RELOC_MIPS_HIGHEST:
7421 case BFD_RELOC_MICROMIPS_HIGHEST:
7422 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
5b7c81bd 7423 return true;
b886a2ab
RS
7424
7425 case BFD_RELOC_MIPS_HIGHER:
7426 case BFD_RELOC_MICROMIPS_HIGHER:
7427 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
5b7c81bd 7428 return true;
b886a2ab
RS
7429
7430 case BFD_RELOC_HI16_S:
41947d9e 7431 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7432 case BFD_RELOC_MICROMIPS_HI16_S:
7433 case BFD_RELOC_MIPS16_HI16_S:
7434 *result = ((operand + 0x8000) >> 16) & 0xffff;
5b7c81bd 7435 return true;
b886a2ab
RS
7436
7437 case BFD_RELOC_HI16:
7438 case BFD_RELOC_MICROMIPS_HI16:
7439 case BFD_RELOC_MIPS16_HI16:
7440 *result = (operand >> 16) & 0xffff;
5b7c81bd 7441 return true;
b886a2ab
RS
7442
7443 case BFD_RELOC_LO16:
41947d9e 7444 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7445 case BFD_RELOC_MICROMIPS_LO16:
7446 case BFD_RELOC_MIPS16_LO16:
7447 *result = operand & 0xffff;
5b7c81bd 7448 return true;
b886a2ab
RS
7449
7450 case BFD_RELOC_UNUSED:
7451 *result = operand;
5b7c81bd 7452 return true;
b886a2ab
RS
7453
7454 default:
5b7c81bd 7455 return false;
b886a2ab
RS
7456 }
7457}
7458
71400594
RS
7459/* Output an instruction. IP is the instruction information.
7460 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7461 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7462 a macro expansion. */
71400594
RS
7463
7464static void
7465append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
5b7c81bd 7466 bfd_reloc_code_real_type *reloc_type, bool expansionp)
71400594 7467{
14fe068b 7468 unsigned long prev_pinfo2, pinfo;
5b7c81bd 7469 bool relaxed_branch = false;
a4e06468 7470 enum append_method method;
5b7c81bd 7471 bool relax32;
2b0c8b40 7472 int branch_disp;
71400594 7473
2309ddf2 7474 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7475 fix_loongson2f (ip);
7476
6f2117ba
PH
7477 ip->target[0] = '\0';
7478 if (offset_expr.X_op == O_symbol)
7479 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7480 ip->label[0] = '\0';
7481 if (seg_info (now_seg)->label_list)
7482 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7483 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7484 fix_loongson3_llsc (ip);
7485
738f4d98 7486 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7487 file_ase_micromips |= mips_opts.micromips;
738f4d98 7488
df58fc94 7489 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7490 pinfo = ip->insn_mo->pinfo;
df58fc94 7491
7bd374a4
MR
7492 /* Don't raise alarm about `nods' frags as they'll fill in the right
7493 kind of nop in relaxation if required. */
df58fc94
RS
7494 if (mips_opts.micromips
7495 && !expansionp
7bd374a4
MR
7496 && !(history[0].frag
7497 && history[0].frag->fr_type == rs_machine_dependent
7498 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7499 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7500 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7501 && micromips_insn_length (ip->insn_mo) != 2)
7502 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7503 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7504 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7505 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7506
15be625d
CM
7507 if (address_expr == NULL)
7508 ip->complete_p = 1;
b886a2ab
RS
7509 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7510 && reloc_type[1] == BFD_RELOC_UNUSED
7511 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7512 && address_expr->X_op == O_constant)
7513 {
15be625d
CM
7514 switch (*reloc_type)
7515 {
15be625d 7516 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7517 {
7518 int shift;
7519
17c6c9d9
MR
7520 /* Shift is 2, unusually, for microMIPS JALX. */
7521 shift = (mips_opts.micromips
7522 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7523 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7524 as_bad (_("jump to misaligned address (0x%lx)"),
7525 (unsigned long) address_expr->X_add_number);
7526 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7527 & 0x3ffffff);
335574df 7528 ip->complete_p = 1;
df58fc94 7529 }
15be625d
CM
7530 break;
7531
7532 case BFD_RELOC_MIPS16_JMP:
7533 if ((address_expr->X_add_number & 3) != 0)
7534 as_bad (_("jump to misaligned address (0x%lx)"),
7535 (unsigned long) address_expr->X_add_number);
7536 ip->insn_opcode |=
7537 (((address_expr->X_add_number & 0x7c0000) << 3)
7538 | ((address_expr->X_add_number & 0xf800000) >> 7)
7539 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7540 ip->complete_p = 1;
15be625d
CM
7541 break;
7542
7543 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7544 {
7545 int shift;
7546
7547 shift = mips_opts.micromips ? 1 : 2;
7548 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7549 as_bad (_("branch to misaligned address (0x%lx)"),
7550 (unsigned long) address_expr->X_add_number);
7551 if (!mips_relax_branch)
7552 {
7553 if ((address_expr->X_add_number + (1 << (shift + 15)))
7554 & ~((1 << (shift + 16)) - 1))
7555 as_bad (_("branch address range overflow (0x%lx)"),
7556 (unsigned long) address_expr->X_add_number);
7557 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7558 & 0xffff);
7559 }
df58fc94 7560 }
15be625d
CM
7561 break;
7562
7361da2c
AB
7563 case BFD_RELOC_MIPS_21_PCREL_S2:
7564 {
7565 int shift;
7566
7567 shift = 2;
7568 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7569 as_bad (_("branch to misaligned address (0x%lx)"),
7570 (unsigned long) address_expr->X_add_number);
7571 if ((address_expr->X_add_number + (1 << (shift + 20)))
7572 & ~((1 << (shift + 21)) - 1))
7573 as_bad (_("branch address range overflow (0x%lx)"),
7574 (unsigned long) address_expr->X_add_number);
7575 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7576 & 0x1fffff);
7577 }
7578 break;
7579
7580 case BFD_RELOC_MIPS_26_PCREL_S2:
7581 {
7582 int shift;
7583
7584 shift = 2;
7585 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7586 as_bad (_("branch to misaligned address (0x%lx)"),
7587 (unsigned long) address_expr->X_add_number);
7588 if ((address_expr->X_add_number + (1 << (shift + 25)))
7589 & ~((1 << (shift + 26)) - 1))
7590 as_bad (_("branch address range overflow (0x%lx)"),
7591 (unsigned long) address_expr->X_add_number);
7592 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7593 & 0x3ffffff);
7594 }
7595 break;
7596
15be625d 7597 default:
b886a2ab
RS
7598 {
7599 offsetT value;
7600
7601 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7602 &value))
7603 {
7604 ip->insn_opcode |= value & 0xffff;
7605 ip->complete_p = 1;
7606 }
7607 }
7608 break;
7609 }
15be625d
CM
7610 }
7611
71400594
RS
7612 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7613 {
7614 /* There are a lot of optimizations we could do that we don't.
7615 In particular, we do not, in general, reorder instructions.
7616 If you use gcc with optimization, it will reorder
7617 instructions and generally do much more optimization then we
7618 do here; repeating all that work in the assembler would only
7619 benefit hand written assembly code, and does not seem worth
7620 it. */
7621 int nops = (mips_optimize == 0
932d1a1b
RS
7622 ? nops_for_insn (0, history, NULL)
7623 : nops_for_insn_or_target (0, history, ip));
71400594 7624 if (nops > 0)
252b5132
RH
7625 {
7626 fragS *old_frag;
7627 unsigned long old_frag_offset;
7628 int i;
252b5132
RH
7629
7630 old_frag = frag_now;
7631 old_frag_offset = frag_now_fix ();
7632
7633 for (i = 0; i < nops; i++)
14fe068b
RS
7634 add_fixed_insn (NOP_INSN);
7635 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7636
7637 if (listing)
7638 {
7639 listing_prev_line ();
7640 /* We may be at the start of a variant frag. In case we
7641 are, make sure there is enough space for the frag
7642 after the frags created by listing_prev_line. The
7643 argument to frag_grow here must be at least as large
7644 as the argument to all other calls to frag_grow in
7645 this file. We don't have to worry about being in the
7646 middle of a variant frag, because the variants insert
7647 all needed nop instructions themselves. */
7648 frag_grow (40);
7649 }
7650
462427c4 7651 mips_move_text_labels ();
252b5132
RH
7652
7653#ifndef NO_ECOFF_DEBUGGING
7654 if (ECOFF_DEBUGGING)
7655 ecoff_fix_loc (old_frag, old_frag_offset);
7656#endif
7657 }
71400594
RS
7658 }
7659 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7660 {
932d1a1b
RS
7661 int nops;
7662
7663 /* Work out how many nops in prev_nop_frag are needed by IP,
7664 ignoring hazards generated by the first prev_nop_frag_since
7665 instructions. */
7666 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7667 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7668
71400594
RS
7669 /* Enforce NOPS as a minimum. */
7670 if (nops > prev_nop_frag_required)
7671 prev_nop_frag_required = nops;
252b5132 7672
71400594
RS
7673 if (prev_nop_frag_holds == prev_nop_frag_required)
7674 {
7675 /* Settle for the current number of nops. Update the history
7676 accordingly (for the benefit of any future .set reorder code). */
7677 prev_nop_frag = NULL;
7678 insert_into_history (prev_nop_frag_since,
7679 prev_nop_frag_holds, NOP_INSN);
7680 }
7681 else
7682 {
7683 /* Allow this instruction to replace one of the nops that was
7684 tentatively added to prev_nop_frag. */
df58fc94 7685 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7686 prev_nop_frag_holds--;
7687 prev_nop_frag_since++;
252b5132
RH
7688 }
7689 }
7690
e407c74b 7691 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7692 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7693
e410add4
RS
7694 dwarf2_emit_insn (0);
7695 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7696 so "move" the instruction address accordingly.
7697
7698 Also, it doesn't seem appropriate for the assembler to reorder .loc
7699 entries. If this instruction is a branch that we are going to swap
7700 with the previous instruction, the two instructions should be
7701 treated as a unit, and the debug information for both instructions
7702 should refer to the start of the branch sequence. Using the
7703 current position is certainly wrong when swapping a 32-bit branch
7704 and a 16-bit delay slot, since the current position would then be
7705 in the middle of a branch. */
7706 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7707
df58fc94
RS
7708 relax32 = (mips_relax_branch
7709 /* Don't try branch relaxation within .set nomacro, or within
7710 .set noat if we use $at for PIC computations. If it turns
7711 out that the branch was out-of-range, we'll get an error. */
7712 && !mips_opts.warn_about_macros
7713 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7714 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7715 as they have no complementing branches. */
7716 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7717
7718 if (!HAVE_CODE_COMPRESSION
7719 && address_expr
7720 && relax32
0b25d3e6 7721 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7722 && delayed_branch_p (ip))
4a6a3df4 7723 {
5b7c81bd 7724 relaxed_branch = true;
1e915849
RS
7725 add_relaxed_insn (ip, (relaxed_branch_length
7726 (NULL, NULL,
11625dd8
RS
7727 uncond_branch_p (ip) ? -1
7728 : branch_likely_p (ip) ? 1
1e915849
RS
7729 : 0)), 4,
7730 RELAX_BRANCH_ENCODE
ce8ad872 7731 (AT, mips_pic != NO_PIC,
11625dd8
RS
7732 uncond_branch_p (ip),
7733 branch_likely_p (ip),
1e915849
RS
7734 pinfo & INSN_WRITE_GPR_31,
7735 0),
7736 address_expr->X_add_symbol,
7737 address_expr->X_add_number);
4a6a3df4
AO
7738 *reloc_type = BFD_RELOC_UNUSED;
7739 }
df58fc94
RS
7740 else if (mips_opts.micromips
7741 && address_expr
7742 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7743 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7744 && (delayed_branch_p (ip) || compact_branch_p (ip))
7745 /* Don't try branch relaxation when users specify
7746 16-bit/32-bit instructions. */
7747 && !forced_insn_length)
df58fc94 7748 {
5b7c81bd
AM
7749 bool relax16 = (method != APPEND_ADD_COMPACT
7750 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7751 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7752 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7753 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7754 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7755 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7756 int length32 = nods ? 8 : 4;
df58fc94
RS
7757
7758 gas_assert (address_expr != NULL);
7759 gas_assert (!mips_relax.sequence);
7760
5b7c81bd 7761 relaxed_branch = true;
7bd374a4
MR
7762 if (nods)
7763 method = APPEND_ADD;
7764 if (relax32)
7765 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7766 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7767 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7768 mips_pic != NO_PIC,
7bd374a4 7769 uncond, compact, al, nods,
40209cad 7770 relax32, 0, 0),
df58fc94
RS
7771 address_expr->X_add_symbol,
7772 address_expr->X_add_number);
7773 *reloc_type = BFD_RELOC_UNUSED;
7774 }
7775 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7776 {
5b7c81bd
AM
7777 bool require_unextended;
7778 bool require_extended;
88a7ef16
MR
7779 symbolS *symbol;
7780 offsetT offset;
7781
7fd53920
MR
7782 if (forced_insn_length != 0)
7783 {
7784 require_unextended = forced_insn_length == 2;
7785 require_extended = forced_insn_length == 4;
7786 }
7787 else
7788 {
7789 require_unextended = (mips_opts.noautoextend
7790 && !mips_opcode_32bit_p (ip->insn_mo));
7791 require_extended = 0;
7792 }
7793
252b5132 7794 /* We need to set up a variant frag. */
df58fc94 7795 gas_assert (address_expr != NULL);
88a7ef16
MR
7796 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7797 symbol created by `make_expr_symbol' may not get a necessary
7798 external relocation produced. */
7799 if (address_expr->X_op == O_symbol)
7800 {
7801 symbol = address_expr->X_add_symbol;
7802 offset = address_expr->X_add_number;
7803 }
7804 else
7805 {
7806 symbol = make_expr_symbol (address_expr);
82d808ed 7807 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7808 offset = 0;
7809 }
8507b6e7 7810 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7811 RELAX_MIPS16_ENCODE
7812 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7813 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7814 mips_pic != NO_PIC,
7815 HAVE_32BIT_SYMBOLS,
7816 mips_opts.warn_about_macros,
7fd53920 7817 require_unextended, require_extended,
11625dd8 7818 delayed_branch_p (&history[0]),
1e915849 7819 history[0].mips16_absolute_jump_p),
88a7ef16 7820 symbol, offset);
252b5132 7821 }
5c04167a 7822 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7823 {
11625dd8 7824 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7825 /* Make sure there is enough room to swap this instruction with
7826 a following jump instruction. */
7827 frag_grow (6);
1e915849 7828 add_fixed_insn (ip);
252b5132
RH
7829 }
7830 else
7831 {
7832 if (mips_opts.mips16
7833 && mips_opts.noreorder
11625dd8 7834 && delayed_branch_p (&history[0]))
252b5132
RH
7835 as_warn (_("extended instruction in delay slot"));
7836
4d7206a2
RS
7837 if (mips_relax.sequence)
7838 {
7839 /* If we've reached the end of this frag, turn it into a variant
7840 frag and record the information for the instructions we've
7841 written so far. */
7842 if (frag_room () < 4)
7843 relax_close_frag ();
df58fc94 7844 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7845 }
7846
584892a6 7847 if (mips_relax.sequence != 2)
df58fc94
RS
7848 {
7849 if (mips_macro_warning.first_insn_sizes[0] == 0)
7850 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7851 mips_macro_warning.sizes[0] += insn_length (ip);
7852 mips_macro_warning.insns[0]++;
7853 }
584892a6 7854 if (mips_relax.sequence != 1)
df58fc94
RS
7855 {
7856 if (mips_macro_warning.first_insn_sizes[1] == 0)
7857 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7858 mips_macro_warning.sizes[1] += insn_length (ip);
7859 mips_macro_warning.insns[1]++;
7860 }
584892a6 7861
1e915849
RS
7862 if (mips_opts.mips16)
7863 {
7864 ip->fixed_p = 1;
7865 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7866 }
7867 add_fixed_insn (ip);
252b5132
RH
7868 }
7869
9fe77896 7870 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7871 {
df58fc94 7872 bfd_reloc_code_real_type final_type[3];
2309ddf2 7873 reloc_howto_type *howto0;
9fe77896
RS
7874 reloc_howto_type *howto;
7875 int i;
34ce925e 7876
df58fc94
RS
7877 /* Perform any necessary conversion to microMIPS relocations
7878 and find out how many relocations there actually are. */
7879 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7880 final_type[i] = micromips_map_reloc (reloc_type[i]);
7881
9fe77896
RS
7882 /* In a compound relocation, it is the final (outermost)
7883 operator that determines the relocated field. */
2309ddf2 7884 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7885 if (!howto)
7886 abort ();
2309ddf2
MR
7887
7888 if (i > 1)
7889 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7890 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7891 bfd_get_reloc_size (howto),
7892 address_expr,
2309ddf2
MR
7893 howto0 && howto0->pc_relative,
7894 final_type[0]);
ce8ad872
MR
7895 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7896 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7897
7898 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7899 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7900 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7901
7902 /* These relocations can have an addend that won't fit in
7903 4 octets for 64bit assembly. */
bad1aba3 7904 if (GPR_SIZE == 64
9fe77896
RS
7905 && ! howto->partial_inplace
7906 && (reloc_type[0] == BFD_RELOC_16
7907 || reloc_type[0] == BFD_RELOC_32
7908 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7909 || reloc_type[0] == BFD_RELOC_GPREL16
7910 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7911 || reloc_type[0] == BFD_RELOC_GPREL32
7912 || reloc_type[0] == BFD_RELOC_64
7913 || reloc_type[0] == BFD_RELOC_CTOR
7914 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7915 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7916 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7917 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
d712f276 7918 || reloc_type[0] == BFD_RELOC_MIPS_16
9fe77896
RS
7919 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7920 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7921 || hi16_reloc_p (reloc_type[0])
7922 || lo16_reloc_p (reloc_type[0])))
7923 ip->fixp[0]->fx_no_overflow = 1;
7924
ddaf2c41
MR
7925 /* These relocations can have an addend that won't fit in 2 octets. */
7926 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7927 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7928 ip->fixp[0]->fx_no_overflow = 1;
7929
9fe77896
RS
7930 if (mips_relax.sequence)
7931 {
7932 if (mips_relax.first_fixup == 0)
7933 mips_relax.first_fixup = ip->fixp[0];
7934 }
7935 else if (reloc_needs_lo_p (*reloc_type))
7936 {
7937 struct mips_hi_fixup *hi_fixup;
7938
7939 /* Reuse the last entry if it already has a matching %lo. */
7940 hi_fixup = mips_hi_fixup_list;
7941 if (hi_fixup == 0
7942 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7943 {
325801bd 7944 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7945 hi_fixup->next = mips_hi_fixup_list;
7946 mips_hi_fixup_list = hi_fixup;
4d7206a2 7947 }
9fe77896
RS
7948 hi_fixup->fixp = ip->fixp[0];
7949 hi_fixup->seg = now_seg;
7950 }
252b5132 7951
9fe77896
RS
7952 /* Add fixups for the second and third relocations, if given.
7953 Note that the ABI allows the second relocation to be
7954 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7955 moment we only use RSS_UNDEF, but we could add support
7956 for the others if it ever becomes necessary. */
7957 for (i = 1; i < 3; i++)
7958 if (reloc_type[i] != BFD_RELOC_UNUSED)
7959 {
7960 ip->fixp[i] = fix_new (ip->frag, ip->where,
7961 ip->fixp[0]->fx_size, NULL, 0,
5b7c81bd 7962 false, final_type[i]);
f6688943 7963
9fe77896
RS
7964 /* Use fx_tcbit to mark compound relocs. */
7965 ip->fixp[0]->fx_tcbit = 1;
7966 ip->fixp[i]->fx_tcbit = 1;
7967 }
252b5132 7968 }
252b5132
RH
7969
7970 /* Update the register mask information. */
4c260379
RS
7971 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7972 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7973
a4e06468 7974 switch (method)
252b5132 7975 {
a4e06468
RS
7976 case APPEND_ADD:
7977 insert_into_history (0, 1, ip);
7978 break;
7979
7980 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7981 {
7982 struct mips_cl_insn *nop;
7983
7984 insert_into_history (0, 1, ip);
7985 nop = get_delay_slot_nop (ip);
7986 add_fixed_insn (nop);
7987 insert_into_history (0, 1, nop);
7988 if (mips_relax.sequence)
7989 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7990 }
a4e06468
RS
7991 break;
7992
7993 case APPEND_ADD_COMPACT:
7994 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7995 if (mips_opts.mips16)
7996 {
7997 ip->insn_opcode |= 0x0080;
7998 find_altered_mips16_opcode (ip);
7999 }
8000 /* Convert microMIPS instructions. */
8001 else if (mips_opts.micromips)
8002 {
8003 /* jr16->jrc */
8004 if ((ip->insn_opcode & 0xffe0) == 0x4580)
8005 ip->insn_opcode |= 0x0020;
8006 /* b16->bc */
8007 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8008 ip->insn_opcode = 0x40e00000;
8009 /* beqz16->beqzc, bnez16->bnezc */
8010 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8011 {
8012 unsigned long regno;
8013
8014 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8015 regno &= MICROMIPSOP_MASK_MD;
8016 regno = micromips_to_32_reg_d_map[regno];
8017 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8018 | (regno << MICROMIPSOP_SH_RS)
8019 | 0x40a00000) ^ 0x00400000;
8020 }
8021 /* beqz->beqzc, bnez->bnezc */
8022 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8023 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8024 | ((ip->insn_opcode >> 7) & 0x00400000)
8025 | 0x40a00000) ^ 0x00400000;
8026 /* beq $0->beqzc, bne $0->bnezc */
8027 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8028 ip->insn_opcode = (((ip->insn_opcode >>
8029 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8030 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8031 | ((ip->insn_opcode >> 7) & 0x00400000)
8032 | 0x40a00000) ^ 0x00400000;
8033 else
8034 abort ();
8035 find_altered_micromips_opcode (ip);
8036 }
8037 else
8038 abort ();
a4e06468
RS
8039 install_insn (ip);
8040 insert_into_history (0, 1, ip);
8041 break;
8042
8043 case APPEND_SWAP:
8044 {
8045 struct mips_cl_insn delay = history[0];
99e7978b
MF
8046
8047 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8048 {
8049 /* Add the delay slot instruction to the end of the
8050 current frag and shrink the fixed part of the
8051 original frag. If the branch occupies the tail of
8052 the latter, move it backwards to cover the gap. */
2b0c8b40 8053 delay.frag->fr_fix -= branch_disp;
a4e06468 8054 if (delay.frag == ip->frag)
2b0c8b40 8055 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8056 add_fixed_insn (&delay);
8057 }
8058 else
8059 {
5e35670b
MR
8060 /* If this is not a relaxed branch and we are in the
8061 same frag, then just swap the instructions. */
8062 move_insn (ip, delay.frag, delay.where);
8063 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8064 }
8065 history[0] = *ip;
8066 delay.fixed_p = 1;
8067 insert_into_history (0, 1, &delay);
8068 }
8069 break;
252b5132
RH
8070 }
8071
13408f1e 8072 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8073 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8074 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8075 {
8076 unsigned int i;
8077
79850f26 8078 mips_no_prev_insn ();
13408f1e 8079
e407c74b 8080 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8081 history[i].cleared_p = 1;
e407c74b
NC
8082 }
8083
df58fc94
RS
8084 /* We need to emit a label at the end of branch-likely macros. */
8085 if (emit_branch_likely_macro)
8086 {
5b7c81bd 8087 emit_branch_likely_macro = false;
df58fc94
RS
8088 micromips_add_label ();
8089 }
8090
252b5132
RH
8091 /* We just output an insn, so the next one doesn't have a label. */
8092 mips_clear_insn_labels ();
252b5132
RH
8093}
8094
e407c74b
NC
8095/* Forget that there was any previous instruction or label.
8096 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8097
8098static void
7d10b47d 8099mips_no_prev_insn (void)
252b5132 8100{
7d10b47d
RS
8101 prev_nop_frag = NULL;
8102 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8103 mips_clear_insn_labels ();
8104}
8105
7d10b47d
RS
8106/* This function must be called before we emit something other than
8107 instructions. It is like mips_no_prev_insn except that it inserts
8108 any NOPS that might be needed by previous instructions. */
252b5132 8109
7d10b47d
RS
8110void
8111mips_emit_delays (void)
252b5132
RH
8112{
8113 if (! mips_opts.noreorder)
8114 {
932d1a1b 8115 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8116 if (nops > 0)
8117 {
7d10b47d
RS
8118 while (nops-- > 0)
8119 add_fixed_insn (NOP_INSN);
462427c4 8120 mips_move_text_labels ();
7d10b47d
RS
8121 }
8122 }
8123 mips_no_prev_insn ();
8124}
8125
8126/* Start a (possibly nested) noreorder block. */
8127
8128static void
8129start_noreorder (void)
8130{
8131 if (mips_opts.noreorder == 0)
8132 {
8133 unsigned int i;
8134 int nops;
8135
8136 /* None of the instructions before the .set noreorder can be moved. */
8137 for (i = 0; i < ARRAY_SIZE (history); i++)
8138 history[i].fixed_p = 1;
8139
8140 /* Insert any nops that might be needed between the .set noreorder
8141 block and the previous instructions. We will later remove any
8142 nops that turn out not to be needed. */
932d1a1b 8143 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8144 if (nops > 0)
8145 {
8146 if (mips_optimize != 0)
252b5132
RH
8147 {
8148 /* Record the frag which holds the nop instructions, so
8149 that we can remove them if we don't need them. */
df58fc94 8150 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8151 prev_nop_frag = frag_now;
8152 prev_nop_frag_holds = nops;
8153 prev_nop_frag_required = 0;
8154 prev_nop_frag_since = 0;
8155 }
8156
8157 for (; nops > 0; --nops)
1e915849 8158 add_fixed_insn (NOP_INSN);
252b5132 8159
7d10b47d
RS
8160 /* Move on to a new frag, so that it is safe to simply
8161 decrease the size of prev_nop_frag. */
8162 frag_wane (frag_now);
8163 frag_new (0);
462427c4 8164 mips_move_text_labels ();
252b5132 8165 }
df58fc94 8166 mips_mark_labels ();
7d10b47d 8167 mips_clear_insn_labels ();
252b5132 8168 }
7d10b47d
RS
8169 mips_opts.noreorder++;
8170 mips_any_noreorder = 1;
8171}
252b5132 8172
7d10b47d 8173/* End a nested noreorder block. */
252b5132 8174
7d10b47d
RS
8175static void
8176end_noreorder (void)
8177{
8178 mips_opts.noreorder--;
8179 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8180 {
8181 /* Commit to inserting prev_nop_frag_required nops and go back to
8182 handling nop insertion the .set reorder way. */
8183 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8184 * NOP_INSN_SIZE);
7d10b47d
RS
8185 insert_into_history (prev_nop_frag_since,
8186 prev_nop_frag_required, NOP_INSN);
8187 prev_nop_frag = NULL;
8188 }
252b5132
RH
8189}
8190
97d87491
RS
8191/* Sign-extend 32-bit mode constants that have bit 31 set and all
8192 higher bits unset. */
8193
8194static void
8195normalize_constant_expr (expressionS *ex)
8196{
8197 if (ex->X_op == O_constant
8198 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8199 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8200 - 0x80000000);
8201}
8202
8203/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8204 all higher bits unset. */
8205
8206static void
8207normalize_address_expr (expressionS *ex)
8208{
8209 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8210 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8211 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8212 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8213 - 0x80000000);
8214}
8215
8216/* Try to match TOKENS against OPCODE, storing the result in INSN.
8217 Return true if the match was successful.
8218
8219 OPCODE_EXTRA is a value that should be ORed into the opcode
8220 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8221 there are more alternatives after OPCODE and SOFT_MATCH is
8222 as for mips_arg_info. */
8223
5b7c81bd 8224static bool
97d87491
RS
8225match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8226 struct mips_operand_token *tokens, unsigned int opcode_extra,
5b7c81bd 8227 bool lax_match, bool complete_p)
97d87491
RS
8228{
8229 const char *args;
8230 struct mips_arg_info arg;
8231 const struct mips_operand *operand;
8232 char c;
8233
8234 imm_expr.X_op = O_absent;
97d87491
RS
8235 offset_expr.X_op = O_absent;
8236 offset_reloc[0] = BFD_RELOC_UNUSED;
8237 offset_reloc[1] = BFD_RELOC_UNUSED;
8238 offset_reloc[2] = BFD_RELOC_UNUSED;
8239
8240 create_insn (insn, opcode);
60f20e8b
RS
8241 /* When no opcode suffix is specified, assume ".xyzw". */
8242 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8243 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8244 else
8245 insn->insn_opcode |= opcode_extra;
97d87491
RS
8246 memset (&arg, 0, sizeof (arg));
8247 arg.insn = insn;
8248 arg.token = tokens;
8249 arg.argnum = 1;
8250 arg.last_regno = ILLEGAL_REG;
8251 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8252 arg.lax_match = lax_match;
97d87491
RS
8253 for (args = opcode->args;; ++args)
8254 {
8255 if (arg.token->type == OT_END)
8256 {
8257 /* Handle unary instructions in which only one operand is given.
8258 The source is then the same as the destination. */
8259 if (arg.opnum == 1 && *args == ',')
8260 {
8261 operand = (mips_opts.micromips
8262 ? decode_micromips_operand (args + 1)
8263 : decode_mips_operand (args + 1));
8264 if (operand && mips_optional_operand_p (operand))
8265 {
8266 arg.token = tokens;
8267 arg.argnum = 1;
8268 continue;
8269 }
8270 }
8271
8272 /* Treat elided base registers as $0. */
8273 if (strcmp (args, "(b)") == 0)
8274 args += 3;
8275
8276 if (args[0] == '+')
8277 switch (args[1])
8278 {
8279 case 'K':
8280 case 'N':
8281 /* The register suffix is optional. */
8282 args += 2;
8283 break;
8284 }
8285
8286 /* Fail the match if there were too few operands. */
8287 if (*args)
5b7c81bd 8288 return false;
97d87491
RS
8289
8290 /* Successful match. */
60f20e8b 8291 if (!complete_p)
5b7c81bd 8292 return true;
e3de51ce 8293 clear_insn_error ();
97d87491 8294 if (arg.dest_regno == arg.last_regno
d34049e8 8295 && startswith (insn->insn_mo->name, "jalr"))
97d87491
RS
8296 {
8297 if (arg.opnum == 2)
e3de51ce 8298 set_insn_error
1661c76c 8299 (0, _("source and destination must be different"));
97d87491 8300 else if (arg.last_regno == 31)
e3de51ce 8301 set_insn_error
1661c76c 8302 (0, _("a destination register must be supplied"));
97d87491 8303 }
173d3447 8304 else if (arg.last_regno == 31
d34049e8
ML
8305 && (startswith (insn->insn_mo->name, "bltzal")
8306 || startswith (insn->insn_mo->name, "bgezal")))
173d3447 8307 set_insn_error (0, _("the source register must not be $31"));
97d87491 8308 check_completed_insn (&arg);
5b7c81bd 8309 return true;
97d87491
RS
8310 }
8311
8312 /* Fail the match if the line has too many operands. */
8313 if (*args == 0)
5b7c81bd 8314 return false;
97d87491
RS
8315
8316 /* Handle characters that need to match exactly. */
8317 if (*args == '(' || *args == ')' || *args == ',')
8318 {
8319 if (match_char (&arg, *args))
8320 continue;
5b7c81bd 8321 return false;
97d87491
RS
8322 }
8323 if (*args == '#')
8324 {
8325 ++args;
8326 if (arg.token->type == OT_DOUBLE_CHAR
8327 && arg.token->u.ch == *args)
8328 {
8329 ++arg.token;
8330 continue;
8331 }
5b7c81bd 8332 return false;
97d87491
RS
8333 }
8334
8335 /* Handle special macro operands. Work out the properties of
8336 other operands. */
8337 arg.opnum += 1;
97d87491
RS
8338 switch (*args)
8339 {
7361da2c
AB
8340 case '-':
8341 switch (args[1])
8342 {
8343 case 'A':
8344 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8345 break;
8346
8347 case 'B':
8348 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8349 break;
8350 }
8351 break;
8352
97d87491
RS
8353 case '+':
8354 switch (args[1])
8355 {
97d87491
RS
8356 case 'i':
8357 *offset_reloc = BFD_RELOC_MIPS_JMP;
8358 break;
7361da2c
AB
8359
8360 case '\'':
8361 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8362 break;
8363
8364 case '\"':
8365 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8366 break;
97d87491
RS
8367 }
8368 break;
8369
97d87491 8370 case 'I':
1a00e612 8371 if (!match_const_int (&arg, &imm_expr.X_add_number))
5b7c81bd 8372 return false;
1a00e612 8373 imm_expr.X_op = O_constant;
bad1aba3 8374 if (GPR_SIZE == 32)
97d87491
RS
8375 normalize_constant_expr (&imm_expr);
8376 continue;
8377
8378 case 'A':
8379 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8380 {
8381 /* Assume that the offset has been elided and that what
8382 we saw was a base register. The match will fail later
8383 if that assumption turns out to be wrong. */
8384 offset_expr.X_op = O_constant;
8385 offset_expr.X_add_number = 0;
8386 }
97d87491 8387 else
1a00e612
RS
8388 {
8389 if (!match_expression (&arg, &offset_expr, offset_reloc))
5b7c81bd 8390 return false;
1a00e612
RS
8391 normalize_address_expr (&offset_expr);
8392 }
97d87491
RS
8393 continue;
8394
8395 case 'F':
8396 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8397 8, true))
8398 return false;
97d87491
RS
8399 continue;
8400
8401 case 'L':
8402 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8403 8, false))
8404 return false;
97d87491
RS
8405 continue;
8406
8407 case 'f':
8408 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8409 4, true))
8410 return false;
97d87491
RS
8411 continue;
8412
8413 case 'l':
8414 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8415 4, false))
8416 return false;
97d87491
RS
8417 continue;
8418
97d87491
RS
8419 case 'p':
8420 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8421 break;
8422
8423 case 'a':
8424 *offset_reloc = BFD_RELOC_MIPS_JMP;
8425 break;
8426
8427 case 'm':
8428 gas_assert (mips_opts.micromips);
8429 c = args[1];
8430 switch (c)
8431 {
8432 case 'D':
8433 case 'E':
8434 if (!forced_insn_length)
8435 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8436 else if (c == 'D')
8437 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8438 else
8439 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8440 break;
8441 }
8442 break;
8443 }
8444
8445 operand = (mips_opts.micromips
8446 ? decode_micromips_operand (args)
8447 : decode_mips_operand (args));
8448 if (!operand)
8449 abort ();
8450
8451 /* Skip prefixes. */
7361da2c 8452 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8453 args++;
8454
8455 if (mips_optional_operand_p (operand)
8456 && args[1] == ','
8457 && (arg.token[0].type != OT_REG
8458 || arg.token[1].type == OT_END))
8459 {
8460 /* Assume that the register has been elided and is the
8461 same as the first operand. */
8462 arg.token = tokens;
8463 arg.argnum = 1;
8464 }
8465
8466 if (!match_operand (&arg, operand))
5b7c81bd 8467 return false;
97d87491
RS
8468 }
8469}
8470
8471/* Like match_insn, but for MIPS16. */
8472
5b7c81bd 8473static bool
97d87491 8474match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8475 struct mips_operand_token *tokens)
97d87491
RS
8476{
8477 const char *args;
8478 const struct mips_operand *operand;
8479 const struct mips_operand *ext_operand;
5b7c81bd 8480 bool pcrel = false;
7fd53920 8481 int required_insn_length;
97d87491
RS
8482 struct mips_arg_info arg;
8483 int relax_char;
8484
7fd53920
MR
8485 if (forced_insn_length)
8486 required_insn_length = forced_insn_length;
8487 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8488 required_insn_length = 2;
8489 else
8490 required_insn_length = 0;
8491
97d87491
RS
8492 create_insn (insn, opcode);
8493 imm_expr.X_op = O_absent;
97d87491
RS
8494 offset_expr.X_op = O_absent;
8495 offset_reloc[0] = BFD_RELOC_UNUSED;
8496 offset_reloc[1] = BFD_RELOC_UNUSED;
8497 offset_reloc[2] = BFD_RELOC_UNUSED;
8498 relax_char = 0;
8499
8500 memset (&arg, 0, sizeof (arg));
8501 arg.insn = insn;
8502 arg.token = tokens;
8503 arg.argnum = 1;
8504 arg.last_regno = ILLEGAL_REG;
8505 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8506 relax_char = 0;
8507 for (args = opcode->args;; ++args)
8508 {
8509 int c;
8510
8511 if (arg.token->type == OT_END)
8512 {
8513 offsetT value;
8514
8515 /* Handle unary instructions in which only one operand is given.
8516 The source is then the same as the destination. */
8517 if (arg.opnum == 1 && *args == ',')
8518 {
5b7c81bd 8519 operand = decode_mips16_operand (args[1], false);
97d87491
RS
8520 if (operand && mips_optional_operand_p (operand))
8521 {
8522 arg.token = tokens;
8523 arg.argnum = 1;
8524 continue;
8525 }
8526 }
8527
8528 /* Fail the match if there were too few operands. */
8529 if (*args)
5b7c81bd 8530 return false;
97d87491
RS
8531
8532 /* Successful match. Stuff the immediate value in now, if
8533 we can. */
e3de51ce 8534 clear_insn_error ();
97d87491
RS
8535 if (opcode->pinfo == INSN_MACRO)
8536 {
8537 gas_assert (relax_char == 0 || relax_char == 'p');
8538 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8539 }
8540 else if (relax_char
8541 && offset_expr.X_op == O_constant
82d808ed 8542 && !pcrel
97d87491
RS
8543 && calculate_reloc (*offset_reloc,
8544 offset_expr.X_add_number,
8545 &value))
8546 {
8547 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8548 required_insn_length, &insn->insn_opcode);
97d87491
RS
8549 offset_expr.X_op = O_absent;
8550 *offset_reloc = BFD_RELOC_UNUSED;
8551 }
8552 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8553 {
7fd53920 8554 if (required_insn_length == 2)
e3de51ce 8555 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8556 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8557 {
8558 forced_insn_length = 4;
8559 insn->insn_opcode |= MIPS16_EXTEND;
8560 }
97d87491
RS
8561 }
8562 else if (relax_char)
8563 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8564
8565 check_completed_insn (&arg);
5b7c81bd 8566 return true;
97d87491
RS
8567 }
8568
8569 /* Fail the match if the line has too many operands. */
8570 if (*args == 0)
5b7c81bd 8571 return false;
97d87491
RS
8572
8573 /* Handle characters that need to match exactly. */
8574 if (*args == '(' || *args == ')' || *args == ',')
8575 {
8576 if (match_char (&arg, *args))
8577 continue;
5b7c81bd 8578 return false;
97d87491
RS
8579 }
8580
8581 arg.opnum += 1;
8582 c = *args;
8583 switch (c)
8584 {
8585 case 'p':
8586 case 'q':
8587 case 'A':
8588 case 'B':
8589 case 'E':
25499ac7
MR
8590 case 'V':
8591 case 'u':
97d87491
RS
8592 relax_char = c;
8593 break;
8594
8595 case 'I':
1a00e612 8596 if (!match_const_int (&arg, &imm_expr.X_add_number))
5b7c81bd 8597 return false;
1a00e612 8598 imm_expr.X_op = O_constant;
bad1aba3 8599 if (GPR_SIZE == 32)
97d87491
RS
8600 normalize_constant_expr (&imm_expr);
8601 continue;
8602
8603 case 'a':
8604 case 'i':
8605 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8606 break;
8607 }
8608
7fd53920 8609 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8610 if (!operand)
8611 abort ();
8612
82d808ed 8613 if (operand->type == OP_PCREL)
5b7c81bd 8614 pcrel = true;
82d808ed 8615 else
97d87491 8616 {
5b7c81bd 8617 ext_operand = decode_mips16_operand (c, true);
97d87491
RS
8618 if (operand != ext_operand)
8619 {
8620 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8621 {
8622 offset_expr.X_op = O_constant;
8623 offset_expr.X_add_number = 0;
8624 relax_char = c;
8625 continue;
8626 }
8627
1a7bf198 8628 if (!match_expression (&arg, &offset_expr, offset_reloc))
5b7c81bd 8629 return false;
97d87491
RS
8630
8631 /* '8' is used for SLTI(U) and has traditionally not
8632 been allowed to take relocation operators. */
8633 if (offset_reloc[0] != BFD_RELOC_UNUSED
8634 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8635 {
8636 match_not_constant (&arg);
5b7c81bd 8637 return false;
e295202f 8638 }
97d87491 8639
c96425c5
MR
8640 if (offset_expr.X_op == O_big)
8641 {
8642 match_out_of_range (&arg);
5b7c81bd 8643 return false;
c96425c5
MR
8644 }
8645
97d87491
RS
8646 relax_char = c;
8647 continue;
8648 }
8649 }
8650
8651 if (mips_optional_operand_p (operand)
8652 && args[1] == ','
8653 && (arg.token[0].type != OT_REG
8654 || arg.token[1].type == OT_END))
8655 {
8656 /* Assume that the register has been elided and is the
8657 same as the first operand. */
8658 arg.token = tokens;
8659 arg.argnum = 1;
8660 }
8661
8662 if (!match_operand (&arg, operand))
5b7c81bd 8663 return false;
97d87491
RS
8664 }
8665}
8666
60f20e8b
RS
8667/* Record that the current instruction is invalid for the current ISA. */
8668
8669static void
8670match_invalid_for_isa (void)
8671{
8672 set_insn_error_ss
1661c76c 8673 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8674 mips_cpu_info_from_arch (mips_opts.arch)->name,
8675 mips_cpu_info_from_isa (mips_opts.isa)->name);
8676}
8677
8678/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8679 Return true if a definite match or failure was found, storing any match
8680 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8681 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8682 tried and failed to match under normal conditions and now want to try a
8683 more relaxed match. */
8684
5b7c81bd 8685static bool
60f20e8b
RS
8686match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8687 const struct mips_opcode *past, struct mips_operand_token *tokens,
5b7c81bd 8688 int opcode_extra, bool lax_match)
60f20e8b
RS
8689{
8690 const struct mips_opcode *opcode;
8691 const struct mips_opcode *invalid_delay_slot;
5b7c81bd 8692 bool seen_valid_for_isa, seen_valid_for_size;
60f20e8b
RS
8693
8694 /* Search for a match, ignoring alternatives that don't satisfy the
8695 current ISA or forced_length. */
8696 invalid_delay_slot = 0;
5b7c81bd
AM
8697 seen_valid_for_isa = false;
8698 seen_valid_for_size = false;
60f20e8b
RS
8699 opcode = first;
8700 do
8701 {
8702 gas_assert (strcmp (opcode->name, first->name) == 0);
8703 if (is_opcode_valid (opcode))
8704 {
5b7c81bd 8705 seen_valid_for_isa = true;
60f20e8b
RS
8706 if (is_size_valid (opcode))
8707 {
5b7c81bd 8708 bool delay_slot_ok;
60f20e8b 8709
5b7c81bd 8710 seen_valid_for_size = true;
60f20e8b
RS
8711 delay_slot_ok = is_delay_slot_valid (opcode);
8712 if (match_insn (insn, opcode, tokens, opcode_extra,
8713 lax_match, delay_slot_ok))
8714 {
8715 if (!delay_slot_ok)
8716 {
8717 if (!invalid_delay_slot)
8718 invalid_delay_slot = opcode;
8719 }
8720 else
5b7c81bd 8721 return true;
60f20e8b
RS
8722 }
8723 }
8724 }
8725 ++opcode;
8726 }
8727 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8728
8729 /* If the only matches we found had the wrong length for the delay slot,
8730 pick the first such match. We'll issue an appropriate warning later. */
8731 if (invalid_delay_slot)
8732 {
8733 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
5b7c81bd
AM
8734 lax_match, true))
8735 return true;
60f20e8b
RS
8736 abort ();
8737 }
8738
8739 /* Handle the case where we didn't try to match an instruction because
8740 all the alternatives were incompatible with the current ISA. */
8741 if (!seen_valid_for_isa)
8742 {
8743 match_invalid_for_isa ();
5b7c81bd 8744 return true;
60f20e8b
RS
8745 }
8746
8747 /* Handle the case where we didn't try to match an instruction because
8748 all the alternatives were of the wrong size. */
8749 if (!seen_valid_for_size)
8750 {
8751 if (mips_opts.insn32)
1661c76c 8752 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8753 else
8754 set_insn_error_i
1661c76c 8755 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b 8756 8 * forced_insn_length);
5b7c81bd 8757 return true;
60f20e8b
RS
8758 }
8759
5b7c81bd 8760 return false;
60f20e8b
RS
8761}
8762
8763/* Like match_insns, but for MIPS16. */
8764
5b7c81bd 8765static bool
60f20e8b
RS
8766match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8767 struct mips_operand_token *tokens)
8768{
8769 const struct mips_opcode *opcode;
5b7c81bd
AM
8770 bool seen_valid_for_isa;
8771 bool seen_valid_for_size;
60f20e8b
RS
8772
8773 /* Search for a match, ignoring alternatives that don't satisfy the
8774 current ISA. There are no separate entries for extended forms so
8775 we deal with forced_length later. */
5b7c81bd
AM
8776 seen_valid_for_isa = false;
8777 seen_valid_for_size = false;
60f20e8b
RS
8778 opcode = first;
8779 do
8780 {
8781 gas_assert (strcmp (opcode->name, first->name) == 0);
8782 if (is_opcode_valid_16 (opcode))
8783 {
5b7c81bd 8784 seen_valid_for_isa = true;
7fd53920
MR
8785 if (is_size_valid_16 (opcode))
8786 {
5b7c81bd 8787 seen_valid_for_size = true;
7fd53920 8788 if (match_mips16_insn (insn, opcode, tokens))
5b7c81bd 8789 return true;
7fd53920 8790 }
60f20e8b
RS
8791 }
8792 ++opcode;
8793 }
8794 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8795 && strcmp (opcode->name, first->name) == 0);
8796
8797 /* Handle the case where we didn't try to match an instruction because
8798 all the alternatives were incompatible with the current ISA. */
8799 if (!seen_valid_for_isa)
8800 {
8801 match_invalid_for_isa ();
5b7c81bd 8802 return true;
60f20e8b
RS
8803 }
8804
7fd53920
MR
8805 /* Handle the case where we didn't try to match an instruction because
8806 all the alternatives were of the wrong size. */
8807 if (!seen_valid_for_size)
8808 {
8809 if (forced_insn_length == 2)
8810 set_insn_error
8811 (0, _("unrecognized unextended version of MIPS16 opcode"));
8812 else
8813 set_insn_error
8814 (0, _("unrecognized extended version of MIPS16 opcode"));
5b7c81bd 8815 return true;
7fd53920
MR
8816 }
8817
5b7c81bd 8818 return false;
60f20e8b
RS
8819}
8820
584892a6
RS
8821/* Set up global variables for the start of a new macro. */
8822
8823static void
8824macro_start (void)
8825{
8826 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8827 memset (&mips_macro_warning.first_insn_sizes, 0,
8828 sizeof (mips_macro_warning.first_insn_sizes));
8829 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8830 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8831 && delayed_branch_p (&history[0]));
7bd374a4
MR
8832 if (history[0].frag
8833 && history[0].frag->fr_type == rs_machine_dependent
8834 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8835 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8836 mips_macro_warning.delay_slot_length = 0;
8837 else
8838 switch (history[0].insn_mo->pinfo2
8839 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8840 {
8841 case INSN2_BRANCH_DELAY_32BIT:
8842 mips_macro_warning.delay_slot_length = 4;
8843 break;
8844 case INSN2_BRANCH_DELAY_16BIT:
8845 mips_macro_warning.delay_slot_length = 2;
8846 break;
8847 default:
8848 mips_macro_warning.delay_slot_length = 0;
8849 break;
8850 }
df58fc94 8851 mips_macro_warning.first_frag = NULL;
584892a6
RS
8852}
8853
df58fc94
RS
8854/* Given that a macro is longer than one instruction or of the wrong size,
8855 return the appropriate warning for it. Return null if no warning is
8856 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8857 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8858 and RELAX_NOMACRO. */
584892a6
RS
8859
8860static const char *
8861macro_warning (relax_substateT subtype)
8862{
8863 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8864 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8865 " in a branch delay slot");
8866 else if (subtype & RELAX_NOMACRO)
1661c76c 8867 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8868 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8869 | RELAX_DELAY_SLOT_SIZE_SECOND))
8870 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8871 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8872 " in a 16-bit branch delay slot")
1661c76c 8873 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8874 " in a 32-bit branch delay slot"));
584892a6
RS
8875 else
8876 return 0;
8877}
8878
8879/* Finish up a macro. Emit warnings as appropriate. */
8880
8881static void
8882macro_end (void)
8883{
df58fc94
RS
8884 /* Relaxation warning flags. */
8885 relax_substateT subtype = 0;
8886
8887 /* Check delay slot size requirements. */
8888 if (mips_macro_warning.delay_slot_length == 2)
8889 subtype |= RELAX_DELAY_SLOT_16BIT;
8890 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8891 {
df58fc94
RS
8892 if (mips_macro_warning.delay_slot_length
8893 != mips_macro_warning.first_insn_sizes[0])
8894 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8895 if (mips_macro_warning.delay_slot_length
8896 != mips_macro_warning.first_insn_sizes[1])
8897 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8898 }
584892a6 8899
df58fc94
RS
8900 /* Check instruction count requirements. */
8901 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8902 {
8903 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8904 subtype |= RELAX_SECOND_LONGER;
8905 if (mips_opts.warn_about_macros)
8906 subtype |= RELAX_NOMACRO;
8907 if (mips_macro_warning.delay_slot_p)
8908 subtype |= RELAX_DELAY_SLOT;
df58fc94 8909 }
584892a6 8910
df58fc94
RS
8911 /* If both alternatives fail to fill a delay slot correctly,
8912 emit the warning now. */
8913 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8914 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8915 {
8916 relax_substateT s;
8917 const char *msg;
8918
8919 s = subtype & (RELAX_DELAY_SLOT_16BIT
8920 | RELAX_DELAY_SLOT_SIZE_FIRST
8921 | RELAX_DELAY_SLOT_SIZE_SECOND);
8922 msg = macro_warning (s);
8923 if (msg != NULL)
8924 as_warn ("%s", msg);
8925 subtype &= ~s;
8926 }
8927
8928 /* If both implementations are longer than 1 instruction, then emit the
8929 warning now. */
8930 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8931 {
8932 relax_substateT s;
8933 const char *msg;
8934
8935 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8936 msg = macro_warning (s);
8937 if (msg != NULL)
8938 as_warn ("%s", msg);
8939 subtype &= ~s;
584892a6 8940 }
df58fc94
RS
8941
8942 /* If any flags still set, then one implementation might need a warning
8943 and the other either will need one of a different kind or none at all.
8944 Pass any remaining flags over to relaxation. */
8945 if (mips_macro_warning.first_frag != NULL)
8946 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8947}
8948
df58fc94
RS
8949/* Instruction operand formats used in macros that vary between
8950 standard MIPS and microMIPS code. */
8951
833794fc 8952static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8953static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8954static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8955static const char * const lui_fmt[2] = { "t,u", "s,u" };
8956static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8957static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8958static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8959static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8960
833794fc 8961#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8962#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8963 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8964#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8965#define LUI_FMT (lui_fmt[mips_opts.micromips])
8966#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8967#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8968 : mem12_fmt[mips_opts.micromips])
833794fc 8969#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8970#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8971#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8972
6e1304d8
RS
8973/* Read a macro's relocation codes from *ARGS and store them in *R.
8974 The first argument in *ARGS will be either the code for a single
8975 relocation or -1 followed by the three codes that make up a
8976 composite relocation. */
8977
8978static void
8979macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8980{
8981 int i, next;
8982
8983 next = va_arg (*args, int);
8984 if (next >= 0)
8985 r[0] = (bfd_reloc_code_real_type) next;
8986 else
f2ae14a1
RS
8987 {
8988 for (i = 0; i < 3; i++)
8989 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8990 /* This function is only used for 16-bit relocation fields.
8991 To make the macro code simpler, treat an unrelocated value
8992 in the same way as BFD_RELOC_LO16. */
8993 if (r[0] == BFD_RELOC_UNUSED)
8994 r[0] = BFD_RELOC_LO16;
8995 }
6e1304d8
RS
8996}
8997
252b5132
RH
8998/* Build an instruction created by a macro expansion. This is passed
8999 a pointer to the count of instructions created so far, an
9000 expression, the name of the instruction to build, an operand format
9001 string, and corresponding arguments. */
9002
252b5132 9003static void
67c0d1eb 9004macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 9005{
df58fc94 9006 const struct mips_opcode *mo = NULL;
f6688943 9007 bfd_reloc_code_real_type r[3];
df58fc94 9008 const struct mips_opcode *amo;
e077a1c8 9009 const struct mips_operand *operand;
629310ab 9010 htab_t hash;
df58fc94 9011 struct mips_cl_insn insn;
252b5132 9012 va_list args;
e077a1c8 9013 unsigned int uval;
252b5132 9014
252b5132 9015 va_start (args, fmt);
252b5132 9016
252b5132
RH
9017 if (mips_opts.mips16)
9018 {
03ea81db 9019 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
9020 va_end (args);
9021 return;
9022 }
9023
f6688943
TS
9024 r[0] = BFD_RELOC_UNUSED;
9025 r[1] = BFD_RELOC_UNUSED;
9026 r[2] = BFD_RELOC_UNUSED;
df58fc94 9027 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
629310ab 9028 amo = (struct mips_opcode *) str_hash_find (hash, name);
df58fc94
RS
9029 gas_assert (amo);
9030 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9031
df58fc94 9032 do
8b082fb1
TS
9033 {
9034 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9035 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9036 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9037 slot instruction length requirement (if any) of the previous
9038 instruction. While doing this we record the first instruction
9039 seen that matches all the other conditions and use it anyway
9040 if the requirement cannot be met; we will issue an appropriate
9041 warning later on. */
9042 if (strcmp (fmt, amo->args) == 0
9043 && amo->pinfo != INSN_MACRO
9044 && is_opcode_valid (amo)
9045 && is_size_valid (amo))
9046 {
9047 if (is_delay_slot_valid (amo))
9048 {
9049 mo = amo;
9050 break;
9051 }
9052 else if (!mo)
9053 mo = amo;
9054 }
8b082fb1 9055
df58fc94
RS
9056 ++amo;
9057 gas_assert (amo->name);
252b5132 9058 }
df58fc94 9059 while (strcmp (name, amo->name) == 0);
252b5132 9060
df58fc94 9061 gas_assert (mo);
1e915849 9062 create_insn (&insn, mo);
e077a1c8 9063 for (; *fmt; ++fmt)
252b5132 9064 {
e077a1c8 9065 switch (*fmt)
252b5132 9066 {
252b5132
RH
9067 case ',':
9068 case '(':
9069 case ')':
252b5132 9070 case 'z':
e077a1c8 9071 break;
252b5132
RH
9072
9073 case 'i':
9074 case 'j':
6e1304d8 9075 macro_read_relocs (&args, r);
9c2799c2 9076 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9077 || *r == BFD_RELOC_MIPS_HIGHER
9078 || *r == BFD_RELOC_HI16_S
9079 || *r == BFD_RELOC_LO16
14c80123
MR
9080 || *r == BFD_RELOC_MIPS_GOT_OFST
9081 || (mips_opts.micromips
d712f276 9082 && (*r == BFD_RELOC_MIPS_16
14c80123
MR
9083 || *r == BFD_RELOC_MIPS_GOT16
9084 || *r == BFD_RELOC_MIPS_CALL16
9085 || *r == BFD_RELOC_MIPS_GOT_HI16
9086 || *r == BFD_RELOC_MIPS_GOT_LO16
9087 || *r == BFD_RELOC_MIPS_CALL_HI16
9088 || *r == BFD_RELOC_MIPS_CALL_LO16
9089 || *r == BFD_RELOC_MIPS_SUB
9090 || *r == BFD_RELOC_MIPS_GOT_PAGE
9091 || *r == BFD_RELOC_MIPS_HIGHEST
9092 || *r == BFD_RELOC_MIPS_GOT_DISP
9093 || *r == BFD_RELOC_MIPS_TLS_GD
9094 || *r == BFD_RELOC_MIPS_TLS_LDM
9095 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9096 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9097 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9098 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9099 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9100 break;
e391c024
RS
9101
9102 case 'o':
9103 macro_read_relocs (&args, r);
e077a1c8 9104 break;
252b5132
RH
9105
9106 case 'u':
6e1304d8 9107 macro_read_relocs (&args, r);
9c2799c2 9108 gas_assert (ep != NULL
90ecf173
MR
9109 && (ep->X_op == O_constant
9110 || (ep->X_op == O_symbol
9111 && (*r == BFD_RELOC_MIPS_HIGHEST
9112 || *r == BFD_RELOC_HI16_S
9113 || *r == BFD_RELOC_HI16
9114 || *r == BFD_RELOC_GPREL16
9115 || *r == BFD_RELOC_MIPS_GOT_HI16
9116 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9117 break;
252b5132
RH
9118
9119 case 'p':
9c2799c2 9120 gas_assert (ep != NULL);
bad36eac 9121
252b5132
RH
9122 /*
9123 * This allows macro() to pass an immediate expression for
9124 * creating short branches without creating a symbol.
bad36eac
DJ
9125 *
9126 * We don't allow branch relaxation for these branches, as
9127 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9128 */
9129 if (ep->X_op == O_constant)
9130 {
df58fc94
RS
9131 /* For microMIPS we always use relocations for branches.
9132 So we should not resolve immediate values. */
9133 gas_assert (!mips_opts.micromips);
9134
bad36eac
DJ
9135 if ((ep->X_add_number & 3) != 0)
9136 as_bad (_("branch to misaligned address (0x%lx)"),
9137 (unsigned long) ep->X_add_number);
9138 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9139 as_bad (_("branch address range overflow (0x%lx)"),
9140 (unsigned long) ep->X_add_number);
252b5132
RH
9141 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9142 ep = NULL;
9143 }
9144 else
0b25d3e6 9145 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9146 break;
252b5132
RH
9147
9148 case 'a':
9c2799c2 9149 gas_assert (ep != NULL);
f6688943 9150 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9151 break;
d43b4baf 9152
252b5132 9153 default:
e077a1c8
RS
9154 operand = (mips_opts.micromips
9155 ? decode_micromips_operand (fmt)
9156 : decode_mips_operand (fmt));
9157 if (!operand)
9158 abort ();
9159
9160 uval = va_arg (args, int);
9161 if (operand->type == OP_CLO_CLZ_DEST)
9162 uval |= (uval << 5);
9163 insn_insert_operand (&insn, operand, uval);
9164
7361da2c 9165 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9166 ++fmt;
9167 break;
252b5132 9168 }
252b5132
RH
9169 }
9170 va_end (args);
9c2799c2 9171 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9172
5b7c81bd 9173 append_insn (&insn, ep, r, true);
252b5132
RH
9174}
9175
9176static void
67c0d1eb 9177mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9178 va_list *args)
252b5132 9179{
1e915849 9180 struct mips_opcode *mo;
252b5132 9181 struct mips_cl_insn insn;
e077a1c8 9182 const struct mips_operand *operand;
f6688943
TS
9183 bfd_reloc_code_real_type r[3]
9184 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9185
629310ab 9186 mo = (struct mips_opcode *) str_hash_find (mips16_op_hash, name);
9c2799c2
NC
9187 gas_assert (mo);
9188 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9189
1e915849 9190 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9191 {
1e915849 9192 ++mo;
9c2799c2
NC
9193 gas_assert (mo->name);
9194 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9195 }
9196
1e915849 9197 create_insn (&insn, mo);
e077a1c8 9198 for (; *fmt; ++fmt)
252b5132
RH
9199 {
9200 int c;
9201
e077a1c8 9202 c = *fmt;
252b5132
RH
9203 switch (c)
9204 {
252b5132
RH
9205 case ',':
9206 case '(':
9207 case ')':
e077a1c8 9208 break;
252b5132 9209
d8722d76 9210 case '.':
252b5132
RH
9211 case 'S':
9212 case 'P':
9213 case 'R':
e077a1c8 9214 break;
252b5132
RH
9215
9216 case '<':
252b5132 9217 case '5':
d8722d76 9218 case 'F':
252b5132
RH
9219 case 'H':
9220 case 'W':
9221 case 'D':
9222 case 'j':
9223 case '8':
9224 case 'V':
9225 case 'C':
9226 case 'U':
9227 case 'k':
9228 case 'K':
9229 case 'p':
9230 case 'q':
9231 {
b886a2ab
RS
9232 offsetT value;
9233
9c2799c2 9234 gas_assert (ep != NULL);
252b5132
RH
9235
9236 if (ep->X_op != O_constant)
874e8986 9237 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9238 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9239 {
b886a2ab 9240 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9241 ep = NULL;
f6688943 9242 *r = BFD_RELOC_UNUSED;
252b5132
RH
9243 }
9244 }
e077a1c8 9245 break;
252b5132 9246
e077a1c8 9247 default:
5b7c81bd 9248 operand = decode_mips16_operand (c, false);
e077a1c8
RS
9249 if (!operand)
9250 abort ();
252b5132 9251
4a06e5a2 9252 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9253 break;
9254 }
252b5132
RH
9255 }
9256
9c2799c2 9257 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9258
5b7c81bd 9259 append_insn (&insn, ep, r, true);
252b5132
RH
9260}
9261
438c16b8
TS
9262/*
9263 * Generate a "jalr" instruction with a relocation hint to the called
9264 * function. This occurs in NewABI PIC code.
9265 */
9266static void
df58fc94 9267macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9268{
df58fc94
RS
9269 static const bfd_reloc_code_real_type jalr_relocs[2]
9270 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9271 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9272 const char *jalr;
685736be 9273 char *f = NULL;
b34976b6 9274
1180b5a4 9275 if (MIPS_JALR_HINT_P (ep))
f21f8242 9276 {
cc3d92a5 9277 frag_grow (8);
f21f8242
AO
9278 f = frag_more (0);
9279 }
2906b037 9280 if (mips_opts.micromips)
df58fc94 9281 {
833794fc
MR
9282 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9283 ? "jalr" : "jalrs");
e64af278 9284 if (MIPS_JALR_HINT_P (ep)
833794fc 9285 || mips_opts.insn32
e64af278 9286 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9287 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9288 else
9289 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9290 }
2906b037
MR
9291 else
9292 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9293 if (MIPS_JALR_HINT_P (ep))
5b7c81bd 9294 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, false, jalr_reloc);
438c16b8
TS
9295}
9296
252b5132
RH
9297/*
9298 * Generate a "lui" instruction.
9299 */
9300static void
67c0d1eb 9301macro_build_lui (expressionS *ep, int regnum)
252b5132 9302{
9c2799c2 9303 gas_assert (! mips_opts.mips16);
252b5132 9304
df58fc94 9305 if (ep->X_op != O_constant)
252b5132 9306 {
9c2799c2 9307 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9308 /* _gp_disp is a special case, used from s_cpload.
9309 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9310 gas_assert (mips_pic == NO_PIC
78e1bb40 9311 || (! HAVE_NEWABI
aa6975fb
ILT
9312 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9313 || (! mips_in_shared
bbe506e8
TS
9314 && strcmp (S_GET_NAME (ep->X_add_symbol),
9315 "__gnu_local_gp") == 0));
252b5132
RH
9316 }
9317
df58fc94 9318 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9319}
9320
885add95
CD
9321/* Generate a sequence of instructions to do a load or store from a constant
9322 offset off of a base register (breg) into/from a target register (treg),
9323 using AT if necessary. */
9324static void
67c0d1eb
RS
9325macro_build_ldst_constoffset (expressionS *ep, const char *op,
9326 int treg, int breg, int dbl)
885add95 9327{
9c2799c2 9328 gas_assert (ep->X_op == O_constant);
885add95 9329
256ab948 9330 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9331 if (!dbl)
9332 normalize_constant_expr (ep);
256ab948 9333
67c1ffbe 9334 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9335 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9336 as_warn (_("operand overflow"));
9337
9338 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9339 {
9340 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9341 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9342 }
9343 else
9344 {
9345 /* 32-bit offset, need multiple instructions and AT, like:
9346 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9347 addu $tempreg,$tempreg,$breg
9348 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9349 to handle the complete offset. */
67c0d1eb
RS
9350 macro_build_lui (ep, AT);
9351 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9352 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9353
741fe287 9354 if (!mips_opts.at)
1661c76c 9355 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9356 }
9357}
9358
252b5132
RH
9359/* set_at()
9360 * Generates code to set the $at register to true (one)
9361 * if reg is less than the immediate expression.
9362 */
9363static void
67c0d1eb 9364set_at (int reg, int unsignedp)
252b5132 9365{
b0e6f033 9366 if (imm_expr.X_add_number >= -0x8000
252b5132 9367 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9368 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9369 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9370 else
9371 {
bad1aba3 9372 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9373 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9374 }
9375}
9376
252b5132
RH
9377/* Count the leading zeroes by performing a binary chop. This is a
9378 bulky bit of source, but performance is a LOT better for the
9379 majority of values than a simple loop to count the bits:
9380 for (lcnt = 0; (lcnt < 32); lcnt++)
9381 if ((v) & (1 << (31 - lcnt)))
9382 break;
9383 However it is not code size friendly, and the gain will drop a bit
9384 on certain cached systems.
9385*/
9386#define COUNT_TOP_ZEROES(v) \
9387 (((v) & ~0xffff) == 0 \
9388 ? ((v) & ~0xff) == 0 \
9389 ? ((v) & ~0xf) == 0 \
9390 ? ((v) & ~0x3) == 0 \
9391 ? ((v) & ~0x1) == 0 \
9392 ? !(v) \
9393 ? 32 \
9394 : 31 \
9395 : 30 \
9396 : ((v) & ~0x7) == 0 \
9397 ? 29 \
9398 : 28 \
9399 : ((v) & ~0x3f) == 0 \
9400 ? ((v) & ~0x1f) == 0 \
9401 ? 27 \
9402 : 26 \
9403 : ((v) & ~0x7f) == 0 \
9404 ? 25 \
9405 : 24 \
9406 : ((v) & ~0xfff) == 0 \
9407 ? ((v) & ~0x3ff) == 0 \
9408 ? ((v) & ~0x1ff) == 0 \
9409 ? 23 \
9410 : 22 \
9411 : ((v) & ~0x7ff) == 0 \
9412 ? 21 \
9413 : 20 \
9414 : ((v) & ~0x3fff) == 0 \
9415 ? ((v) & ~0x1fff) == 0 \
9416 ? 19 \
9417 : 18 \
9418 : ((v) & ~0x7fff) == 0 \
9419 ? 17 \
9420 : 16 \
9421 : ((v) & ~0xffffff) == 0 \
9422 ? ((v) & ~0xfffff) == 0 \
9423 ? ((v) & ~0x3ffff) == 0 \
9424 ? ((v) & ~0x1ffff) == 0 \
9425 ? 15 \
9426 : 14 \
9427 : ((v) & ~0x7ffff) == 0 \
9428 ? 13 \
9429 : 12 \
9430 : ((v) & ~0x3fffff) == 0 \
9431 ? ((v) & ~0x1fffff) == 0 \
9432 ? 11 \
9433 : 10 \
9434 : ((v) & ~0x7fffff) == 0 \
9435 ? 9 \
9436 : 8 \
9437 : ((v) & ~0xfffffff) == 0 \
9438 ? ((v) & ~0x3ffffff) == 0 \
9439 ? ((v) & ~0x1ffffff) == 0 \
9440 ? 7 \
9441 : 6 \
9442 : ((v) & ~0x7ffffff) == 0 \
9443 ? 5 \
9444 : 4 \
9445 : ((v) & ~0x3fffffff) == 0 \
9446 ? ((v) & ~0x1fffffff) == 0 \
9447 ? 3 \
9448 : 2 \
9449 : ((v) & ~0x7fffffff) == 0 \
9450 ? 1 \
9451 : 0)
9452
9453/* load_register()
67c1ffbe 9454 * This routine generates the least number of instructions necessary to load
252b5132
RH
9455 * an absolute expression value into a register.
9456 */
9457static void
67c0d1eb 9458load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9459{
9460 int freg;
9461 expressionS hi32, lo32;
9462
9463 if (ep->X_op != O_big)
9464 {
9c2799c2 9465 gas_assert (ep->X_op == O_constant);
256ab948
TS
9466
9467 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9468 if (!dbl)
9469 normalize_constant_expr (ep);
256ab948
TS
9470
9471 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9472 {
9473 /* We can handle 16 bit signed values with an addiu to
9474 $zero. No need to ever use daddiu here, since $zero and
9475 the result are always correct in 32 bit mode. */
67c0d1eb 9476 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9477 return;
9478 }
9479 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9480 {
9481 /* We can handle 16 bit unsigned values with an ori to
9482 $zero. */
67c0d1eb 9483 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9484 return;
9485 }
256ab948 9486 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9487 {
9488 /* 32 bit values require an lui. */
df58fc94 9489 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9490 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9491 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9492 return;
9493 }
9494 }
9495
9496 /* The value is larger than 32 bits. */
9497
bad1aba3 9498 if (!dbl || GPR_SIZE == 32)
252b5132 9499 {
f493c217
AM
9500 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
9501 ep->X_add_number);
67c0d1eb 9502 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9503 return;
9504 }
9505
9506 if (ep->X_op != O_big)
9507 {
9508 hi32 = *ep;
9509 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9510 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9511 hi32.X_add_number &= 0xffffffff;
9512 lo32 = *ep;
9513 lo32.X_add_number &= 0xffffffff;
9514 }
9515 else
9516 {
9c2799c2 9517 gas_assert (ep->X_add_number > 2);
252b5132
RH
9518 if (ep->X_add_number == 3)
9519 generic_bignum[3] = 0;
9520 else if (ep->X_add_number > 4)
1661c76c 9521 as_bad (_("number larger than 64 bits"));
252b5132
RH
9522 lo32.X_op = O_constant;
9523 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9524 hi32.X_op = O_constant;
9525 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9526 }
9527
9528 if (hi32.X_add_number == 0)
9529 freg = 0;
9530 else
9531 {
9532 int shift, bit;
9533 unsigned long hi, lo;
9534
956cd1d6 9535 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9536 {
9537 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9538 {
67c0d1eb 9539 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9540 return;
9541 }
9542 if (lo32.X_add_number & 0x80000000)
9543 {
df58fc94 9544 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9545 if (lo32.X_add_number & 0xffff)
67c0d1eb 9546 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9547 return;
9548 }
9549 }
252b5132
RH
9550
9551 /* Check for 16bit shifted constant. We know that hi32 is
9552 non-zero, so start the mask on the first bit of the hi32
9553 value. */
9554 shift = 17;
9555 do
beae10d5
KH
9556 {
9557 unsigned long himask, lomask;
9558
9559 if (shift < 32)
9560 {
9561 himask = 0xffff >> (32 - shift);
e0fd91ef 9562 lomask = (0xffffU << shift) & 0xffffffff;
beae10d5
KH
9563 }
9564 else
9565 {
e0fd91ef 9566 himask = 0xffffU << (shift - 32);
beae10d5
KH
9567 lomask = 0;
9568 }
9569 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9570 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9571 {
9572 expressionS tmp;
9573
9574 tmp.X_op = O_constant;
9575 if (shift < 32)
9576 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9577 | (lo32.X_add_number >> shift));
9578 else
9579 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9580 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9581 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9582 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9583 return;
9584 }
f9419b05 9585 ++shift;
beae10d5
KH
9586 }
9587 while (shift <= (64 - 16));
252b5132
RH
9588
9589 /* Find the bit number of the lowest one bit, and store the
9590 shifted value in hi/lo. */
9591 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9592 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9593 if (lo != 0)
9594 {
9595 bit = 0;
9596 while ((lo & 1) == 0)
9597 {
9598 lo >>= 1;
9599 ++bit;
9600 }
7697028a
AM
9601 if (bit != 0)
9602 {
9603 lo |= (hi & ((2UL << (bit - 1)) - 1)) << (32 - bit);
9604 hi >>= bit;
9605 }
252b5132
RH
9606 }
9607 else
9608 {
9609 bit = 32;
9610 while ((hi & 1) == 0)
9611 {
9612 hi >>= 1;
9613 ++bit;
9614 }
9615 lo = hi;
9616 hi = 0;
9617 }
9618
9619 /* Optimize if the shifted value is a (power of 2) - 1. */
9620 if ((hi == 0 && ((lo + 1) & lo) == 0)
9621 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9622 {
9623 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9624 if (shift != 0)
beae10d5 9625 {
252b5132
RH
9626 expressionS tmp;
9627
9628 /* This instruction will set the register to be all
9629 ones. */
beae10d5
KH
9630 tmp.X_op = O_constant;
9631 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9632 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9633 if (bit != 0)
9634 {
9635 bit += shift;
df58fc94 9636 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9637 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9638 }
df58fc94 9639 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9640 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9641 return;
9642 }
9643 }
252b5132
RH
9644
9645 /* Sign extend hi32 before calling load_register, because we can
9646 generally get better code when we load a sign extended value. */
9647 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9648 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9649 load_register (reg, &hi32, 0);
252b5132
RH
9650 freg = reg;
9651 }
9652 if ((lo32.X_add_number & 0xffff0000) == 0)
9653 {
9654 if (freg != 0)
9655 {
df58fc94 9656 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9657 freg = reg;
9658 }
9659 }
9660 else
9661 {
9662 expressionS mid16;
9663
956cd1d6 9664 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9665 {
df58fc94
RS
9666 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9667 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9668 return;
9669 }
252b5132
RH
9670
9671 if (freg != 0)
9672 {
df58fc94 9673 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9674 freg = reg;
9675 }
9676 mid16 = lo32;
9677 mid16.X_add_number >>= 16;
67c0d1eb 9678 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9679 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9680 freg = reg;
9681 }
9682 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9683 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9684}
9685
269137b2
TS
9686static inline void
9687load_delay_nop (void)
9688{
9689 if (!gpr_interlocks)
9690 macro_build (NULL, "nop", "");
9691}
9692
252b5132
RH
9693/* Load an address into a register. */
9694
9695static void
67c0d1eb 9696load_address (int reg, expressionS *ep, int *used_at)
252b5132 9697{
252b5132
RH
9698 if (ep->X_op != O_constant
9699 && ep->X_op != O_symbol)
9700 {
9701 as_bad (_("expression too complex"));
9702 ep->X_op = O_constant;
9703 }
9704
9705 if (ep->X_op == O_constant)
9706 {
67c0d1eb 9707 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9708 return;
9709 }
9710
9711 if (mips_pic == NO_PIC)
9712 {
9713 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9714 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9715 Otherwise we want
9716 lui $reg,<sym> (BFD_RELOC_HI16_S)
9717 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9718 If we have an addend, we always use the latter form.
76b3015f 9719
d6bc6245
TS
9720 With 64bit address space and a usable $at we want
9721 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9722 lui $at,<sym> (BFD_RELOC_HI16_S)
9723 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9724 daddiu $at,<sym> (BFD_RELOC_LO16)
9725 dsll32 $reg,0
3a482fd5 9726 daddu $reg,$reg,$at
76b3015f 9727
c03099e6 9728 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9729 on superscalar processors.
9730 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9731 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9732 dsll $reg,16
9733 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9734 dsll $reg,16
9735 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9736
9737 For GP relative symbols in 64bit address space we can use
9738 the same sequence as in 32bit address space. */
aed1a261 9739 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9740 {
6caf9ef4
TS
9741 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9742 && !nopic_need_relax (ep->X_add_symbol, 1))
9743 {
9744 relax_start (ep->X_add_symbol);
9745 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9746 mips_gp_register, BFD_RELOC_GPREL16);
9747 relax_switch ();
9748 }
d6bc6245 9749
741fe287 9750 if (*used_at == 0 && mips_opts.at)
d6bc6245 9751 {
df58fc94
RS
9752 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9753 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9754 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9755 BFD_RELOC_MIPS_HIGHER);
9756 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9757 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9758 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9759 *used_at = 1;
9760 }
9761 else
9762 {
df58fc94 9763 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9764 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9765 BFD_RELOC_MIPS_HIGHER);
df58fc94 9766 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9767 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9768 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9769 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9770 }
6caf9ef4
TS
9771
9772 if (mips_relax.sequence)
9773 relax_end ();
d6bc6245 9774 }
252b5132
RH
9775 else
9776 {
d6bc6245 9777 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9778 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9779 {
4d7206a2 9780 relax_start (ep->X_add_symbol);
67c0d1eb 9781 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9782 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9783 relax_switch ();
d6bc6245 9784 }
67c0d1eb
RS
9785 macro_build_lui (ep, reg);
9786 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9787 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9788 if (mips_relax.sequence)
9789 relax_end ();
d6bc6245 9790 }
252b5132 9791 }
0a44bf69 9792 else if (!mips_big_got)
252b5132
RH
9793 {
9794 expressionS ex;
9795
9796 /* If this is a reference to an external symbol, we want
9797 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9798 Otherwise we want
9799 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9800 nop
9801 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9802 If there is a constant, it must be added in after.
9803
ed6fb7bd 9804 If we have NewABI, we want
f5040a92
AO
9805 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9806 unless we're referencing a global symbol with a non-zero
9807 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9808 if (HAVE_NEWABI)
9809 {
f5040a92
AO
9810 if (ep->X_add_number)
9811 {
4d7206a2 9812 ex.X_add_number = ep->X_add_number;
f5040a92 9813 ep->X_add_number = 0;
4d7206a2 9814 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9815 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9816 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9817 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9818 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9819 ex.X_op = O_constant;
67c0d1eb 9820 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9821 reg, reg, BFD_RELOC_LO16);
f5040a92 9822 ep->X_add_number = ex.X_add_number;
4d7206a2 9823 relax_switch ();
f5040a92 9824 }
67c0d1eb 9825 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9826 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9827 if (mips_relax.sequence)
9828 relax_end ();
ed6fb7bd
SC
9829 }
9830 else
9831 {
f5040a92
AO
9832 ex.X_add_number = ep->X_add_number;
9833 ep->X_add_number = 0;
67c0d1eb
RS
9834 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9835 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9836 load_delay_nop ();
4d7206a2
RS
9837 relax_start (ep->X_add_symbol);
9838 relax_switch ();
67c0d1eb 9839 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9840 BFD_RELOC_LO16);
4d7206a2 9841 relax_end ();
ed6fb7bd 9842
f5040a92
AO
9843 if (ex.X_add_number != 0)
9844 {
9845 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9846 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9847 ex.X_op = O_constant;
67c0d1eb 9848 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9849 reg, reg, BFD_RELOC_LO16);
f5040a92 9850 }
252b5132
RH
9851 }
9852 }
0a44bf69 9853 else if (mips_big_got)
252b5132
RH
9854 {
9855 expressionS ex;
252b5132
RH
9856
9857 /* This is the large GOT case. If this is a reference to an
9858 external symbol, we want
9859 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9860 addu $reg,$reg,$gp
9861 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9862
9863 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9864 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9865 nop
9866 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9867 If there is a constant, it must be added in after.
f5040a92
AO
9868
9869 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9870 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9871 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9872 */
438c16b8
TS
9873 if (HAVE_NEWABI)
9874 {
4d7206a2 9875 ex.X_add_number = ep->X_add_number;
f5040a92 9876 ep->X_add_number = 0;
4d7206a2 9877 relax_start (ep->X_add_symbol);
df58fc94 9878 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9879 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9880 reg, reg, mips_gp_register);
9881 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9882 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9883 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9884 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9885 else if (ex.X_add_number)
9886 {
9887 ex.X_op = O_constant;
67c0d1eb
RS
9888 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9889 BFD_RELOC_LO16);
f5040a92
AO
9890 }
9891
9892 ep->X_add_number = ex.X_add_number;
4d7206a2 9893 relax_switch ();
67c0d1eb 9894 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9895 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9896 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9897 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9898 relax_end ();
438c16b8 9899 }
252b5132 9900 else
438c16b8 9901 {
f5040a92
AO
9902 ex.X_add_number = ep->X_add_number;
9903 ep->X_add_number = 0;
4d7206a2 9904 relax_start (ep->X_add_symbol);
df58fc94 9905 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9906 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9907 reg, reg, mips_gp_register);
9908 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9909 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9910 relax_switch ();
9911 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9912 {
9913 /* We need a nop before loading from $gp. This special
9914 check is required because the lui which starts the main
9915 instruction stream does not refer to $gp, and so will not
9916 insert the nop which may be required. */
67c0d1eb 9917 macro_build (NULL, "nop", "");
438c16b8 9918 }
67c0d1eb 9919 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9920 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9921 load_delay_nop ();
67c0d1eb 9922 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9923 BFD_RELOC_LO16);
4d7206a2 9924 relax_end ();
438c16b8 9925
f5040a92
AO
9926 if (ex.X_add_number != 0)
9927 {
9928 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9929 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9930 ex.X_op = O_constant;
67c0d1eb
RS
9931 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9932 BFD_RELOC_LO16);
f5040a92 9933 }
252b5132
RH
9934 }
9935 }
252b5132
RH
9936 else
9937 abort ();
8fc2e39e 9938
741fe287 9939 if (!mips_opts.at && *used_at == 1)
1661c76c 9940 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9941}
9942
ea1fb5dc
RS
9943/* Move the contents of register SOURCE into register DEST. */
9944
9945static void
67c0d1eb 9946move_register (int dest, int source)
ea1fb5dc 9947{
df58fc94
RS
9948 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9949 instruction specifically requires a 32-bit one. */
9950 if (mips_opts.micromips
833794fc 9951 && !mips_opts.insn32
df58fc94 9952 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9953 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9954 else
40fc1451 9955 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9956}
9957
4d7206a2 9958/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9959 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9960 The two alternatives are:
4d7206a2 9961
33eaf5de 9962 Global symbol Local symbol
4d7206a2
RS
9963 ------------- ------------
9964 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9965 ... ...
9966 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9967
9968 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9969 emits the second for a 16-bit offset or add_got_offset_hilo emits
9970 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9971
9972static void
67c0d1eb 9973load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9974{
9975 expressionS global;
9976
9977 global = *local;
9978 global.X_add_number = 0;
9979
9980 relax_start (local->X_add_symbol);
67c0d1eb
RS
9981 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9982 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9983 relax_switch ();
67c0d1eb
RS
9984 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9985 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9986 relax_end ();
9987}
9988
9989static void
67c0d1eb 9990add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9991{
9992 expressionS global;
9993
9994 global.X_op = O_constant;
9995 global.X_op_symbol = NULL;
9996 global.X_add_symbol = NULL;
9997 global.X_add_number = local->X_add_number;
9998
9999 relax_start (local->X_add_symbol);
67c0d1eb 10000 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
10001 dest, dest, BFD_RELOC_LO16);
10002 relax_switch ();
67c0d1eb 10003 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
10004 relax_end ();
10005}
10006
f6a22291
MR
10007static void
10008add_got_offset_hilo (int dest, expressionS *local, int tmp)
10009{
10010 expressionS global;
10011 int hold_mips_optimize;
10012
10013 global.X_op = O_constant;
10014 global.X_op_symbol = NULL;
10015 global.X_add_symbol = NULL;
10016 global.X_add_number = local->X_add_number;
10017
10018 relax_start (local->X_add_symbol);
10019 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10020 relax_switch ();
10021 /* Set mips_optimize around the lui instruction to avoid
10022 inserting an unnecessary nop after the lw. */
10023 hold_mips_optimize = mips_optimize;
10024 mips_optimize = 2;
10025 macro_build_lui (&global, tmp);
10026 mips_optimize = hold_mips_optimize;
10027 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10028 relax_end ();
10029
10030 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10031}
10032
df58fc94
RS
10033/* Emit a sequence of instructions to emulate a branch likely operation.
10034 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10035 is its complementing branch with the original condition negated.
10036 CALL is set if the original branch specified the link operation.
10037 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10038
10039 Code like this is produced in the noreorder mode:
10040
10041 BRNEG <args>, 1f
10042 nop
10043 b <sym>
10044 delay slot (executed only if branch taken)
10045 1:
10046
10047 or, if CALL is set:
10048
10049 BRNEG <args>, 1f
10050 nop
10051 bal <sym>
10052 delay slot (executed only if branch taken)
10053 1:
10054
10055 In the reorder mode the delay slot would be filled with a nop anyway,
10056 so code produced is simply:
10057
10058 BR <args>, <sym>
10059 nop
10060
10061 This function is used when producing code for the microMIPS ASE that
10062 does not implement branch likely instructions in hardware. */
10063
10064static void
10065macro_build_branch_likely (const char *br, const char *brneg,
10066 int call, expressionS *ep, const char *fmt,
10067 unsigned int sreg, unsigned int treg)
10068{
10069 int noreorder = mips_opts.noreorder;
10070 expressionS expr1;
10071
10072 gas_assert (mips_opts.micromips);
10073 start_noreorder ();
10074 if (noreorder)
10075 {
10076 micromips_label_expr (&expr1);
10077 macro_build (&expr1, brneg, fmt, sreg, treg);
10078 macro_build (NULL, "nop", "");
10079 macro_build (ep, call ? "bal" : "b", "p");
10080
10081 /* Set to true so that append_insn adds a label. */
5b7c81bd 10082 emit_branch_likely_macro = true;
df58fc94
RS
10083 }
10084 else
10085 {
10086 macro_build (ep, br, fmt, sreg, treg);
10087 macro_build (NULL, "nop", "");
10088 }
10089 end_noreorder ();
10090}
10091
10092/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10093 the condition code tested. EP specifies the branch target. */
10094
10095static void
10096macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10097{
10098 const int call = 0;
10099 const char *brneg;
10100 const char *br;
10101
10102 switch (type)
10103 {
10104 case M_BC1FL:
10105 br = "bc1f";
10106 brneg = "bc1t";
10107 break;
10108 case M_BC1TL:
10109 br = "bc1t";
10110 brneg = "bc1f";
10111 break;
10112 case M_BC2FL:
10113 br = "bc2f";
10114 brneg = "bc2t";
10115 break;
10116 case M_BC2TL:
10117 br = "bc2t";
10118 brneg = "bc2f";
10119 break;
10120 default:
10121 abort ();
10122 }
10123 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10124}
10125
10126/* Emit a two-argument branch macro specified by TYPE, using SREG as
10127 the register tested. EP specifies the branch target. */
10128
10129static void
10130macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10131{
10132 const char *brneg = NULL;
10133 const char *br;
10134 int call = 0;
10135
10136 switch (type)
10137 {
10138 case M_BGEZ:
10139 br = "bgez";
10140 break;
10141 case M_BGEZL:
10142 br = mips_opts.micromips ? "bgez" : "bgezl";
10143 brneg = "bltz";
10144 break;
10145 case M_BGEZALL:
10146 gas_assert (mips_opts.micromips);
833794fc 10147 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10148 brneg = "bltz";
10149 call = 1;
10150 break;
10151 case M_BGTZ:
10152 br = "bgtz";
10153 break;
10154 case M_BGTZL:
10155 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10156 brneg = "blez";
10157 break;
10158 case M_BLEZ:
10159 br = "blez";
10160 break;
10161 case M_BLEZL:
10162 br = mips_opts.micromips ? "blez" : "blezl";
10163 brneg = "bgtz";
10164 break;
10165 case M_BLTZ:
10166 br = "bltz";
10167 break;
10168 case M_BLTZL:
10169 br = mips_opts.micromips ? "bltz" : "bltzl";
10170 brneg = "bgez";
10171 break;
10172 case M_BLTZALL:
10173 gas_assert (mips_opts.micromips);
833794fc 10174 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10175 brneg = "bgez";
10176 call = 1;
10177 break;
10178 default:
10179 abort ();
10180 }
10181 if (mips_opts.micromips && brneg)
10182 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10183 else
10184 macro_build (ep, br, "s,p", sreg);
10185}
10186
10187/* Emit a three-argument branch macro specified by TYPE, using SREG and
10188 TREG as the registers tested. EP specifies the branch target. */
10189
10190static void
10191macro_build_branch_rsrt (int type, expressionS *ep,
10192 unsigned int sreg, unsigned int treg)
10193{
10194 const char *brneg = NULL;
10195 const int call = 0;
10196 const char *br;
10197
10198 switch (type)
10199 {
10200 case M_BEQ:
10201 case M_BEQ_I:
10202 br = "beq";
10203 break;
10204 case M_BEQL:
10205 case M_BEQL_I:
10206 br = mips_opts.micromips ? "beq" : "beql";
10207 brneg = "bne";
10208 break;
10209 case M_BNE:
10210 case M_BNE_I:
10211 br = "bne";
10212 break;
10213 case M_BNEL:
10214 case M_BNEL_I:
10215 br = mips_opts.micromips ? "bne" : "bnel";
10216 brneg = "beq";
10217 break;
10218 default:
10219 abort ();
10220 }
10221 if (mips_opts.micromips && brneg)
10222 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10223 else
10224 macro_build (ep, br, "s,t,p", sreg, treg);
10225}
10226
f2ae14a1
RS
10227/* Return the high part that should be loaded in order to make the low
10228 part of VALUE accessible using an offset of OFFBITS bits. */
10229
10230static offsetT
10231offset_high_part (offsetT value, unsigned int offbits)
10232{
10233 offsetT bias;
10234 addressT low_mask;
10235
10236 if (offbits == 0)
10237 return value;
10238 bias = 1 << (offbits - 1);
10239 low_mask = bias * 2 - 1;
10240 return (value + bias) & ~low_mask;
10241}
10242
10243/* Return true if the value stored in offset_expr and offset_reloc
10244 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10245 amount that the caller wants to add without inducing overflow
10246 and ALIGN is the known alignment of the value in bytes. */
10247
5b7c81bd 10248static bool
f2ae14a1
RS
10249small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10250{
10251 if (offbits == 16)
10252 {
10253 /* Accept any relocation operator if overflow isn't a concern. */
10254 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
5b7c81bd 10255 return true;
f2ae14a1
RS
10256
10257 /* These relocations are guaranteed not to overflow in correct links. */
10258 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10259 || gprel16_reloc_p (*offset_reloc))
5b7c81bd 10260 return true;
f2ae14a1
RS
10261 }
10262 if (offset_expr.X_op == O_constant
10263 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10264 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
5b7c81bd
AM
10265 return true;
10266 return false;
f2ae14a1
RS
10267}
10268
252b5132
RH
10269/*
10270 * Build macros
10271 * This routine implements the seemingly endless macro or synthesized
10272 * instructions and addressing modes in the mips assembly language. Many
10273 * of these macros are simple and are similar to each other. These could
67c1ffbe 10274 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10275 * this verbose method. Others are not simple macros but are more like
10276 * optimizing code generation.
10277 * One interesting optimization is when several store macros appear
67c1ffbe 10278 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10279 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10280 * of global optimization. We currently only optimize within a single macro.
10281 * For many of the load and store macros if the address is specified as a
10282 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10283 * first load register 'at' with zero and use it as the base register. The
10284 * mips assembler simply uses register $zero. Just one tiny optimization
10285 * we're missing.
10286 */
10287static void
833794fc 10288macro (struct mips_cl_insn *ip, char *str)
252b5132 10289{
c0ebe874
RS
10290 const struct mips_operand_array *operands;
10291 unsigned int breg, i;
741fe287 10292 unsigned int tempreg;
252b5132 10293 int mask;
43841e91 10294 int used_at = 0;
df58fc94 10295 expressionS label_expr;
252b5132 10296 expressionS expr1;
df58fc94 10297 expressionS *ep;
252b5132
RH
10298 const char *s;
10299 const char *s2;
10300 const char *fmt;
10301 int likely = 0;
252b5132 10302 int coproc = 0;
7f3c4072 10303 int offbits = 16;
1abe91b1 10304 int call = 0;
df58fc94
RS
10305 int jals = 0;
10306 int dbl = 0;
10307 int imm = 0;
10308 int ust = 0;
10309 int lp = 0;
a45328b9 10310 int ll_sc_paired = 0;
5b7c81bd 10311 bool large_offset;
252b5132 10312 int off;
252b5132 10313 int hold_mips_optimize;
f2ae14a1 10314 unsigned int align;
c0ebe874 10315 unsigned int op[MAX_OPERANDS];
252b5132 10316
9c2799c2 10317 gas_assert (! mips_opts.mips16);
252b5132 10318
c0ebe874
RS
10319 operands = insn_operands (ip);
10320 for (i = 0; i < MAX_OPERANDS; i++)
10321 if (operands->operand[i])
10322 op[i] = insn_extract_operand (ip, operands->operand[i]);
10323 else
10324 op[i] = -1;
10325
252b5132
RH
10326 mask = ip->insn_mo->mask;
10327
df58fc94
RS
10328 label_expr.X_op = O_constant;
10329 label_expr.X_op_symbol = NULL;
10330 label_expr.X_add_symbol = NULL;
10331 label_expr.X_add_number = 0;
10332
252b5132
RH
10333 expr1.X_op = O_constant;
10334 expr1.X_op_symbol = NULL;
10335 expr1.X_add_symbol = NULL;
10336 expr1.X_add_number = 1;
f2ae14a1 10337 align = 1;
252b5132
RH
10338
10339 switch (mask)
10340 {
10341 case M_DABS:
10342 dbl = 1;
1a0670f3 10343 /* Fall through. */
252b5132 10344 case M_ABS:
df58fc94
RS
10345 /* bgez $a0,1f
10346 move v0,$a0
10347 sub v0,$zero,$a0
10348 1:
10349 */
252b5132 10350
7d10b47d 10351 start_noreorder ();
252b5132 10352
df58fc94
RS
10353 if (mips_opts.micromips)
10354 micromips_label_expr (&label_expr);
10355 else
10356 label_expr.X_add_number = 8;
c0ebe874
RS
10357 macro_build (&label_expr, "bgez", "s,p", op[1]);
10358 if (op[0] == op[1])
a605d2b3 10359 macro_build (NULL, "nop", "");
252b5132 10360 else
c0ebe874
RS
10361 move_register (op[0], op[1]);
10362 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10363 if (mips_opts.micromips)
10364 micromips_add_label ();
252b5132 10365
7d10b47d 10366 end_noreorder ();
8fc2e39e 10367 break;
252b5132
RH
10368
10369 case M_ADD_I:
10370 s = "addi";
10371 s2 = "add";
387e7624
FS
10372 if (ISA_IS_R6 (mips_opts.isa))
10373 goto do_addi_i;
10374 else
10375 goto do_addi;
252b5132
RH
10376 case M_ADDU_I:
10377 s = "addiu";
10378 s2 = "addu";
10379 goto do_addi;
10380 case M_DADD_I:
10381 dbl = 1;
10382 s = "daddi";
10383 s2 = "dadd";
387e7624 10384 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10385 goto do_addi;
b0e6f033 10386 if (imm_expr.X_add_number >= -0x200
387e7624
FS
10387 && imm_expr.X_add_number < 0x200
10388 && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10389 {
b0e6f033
RS
10390 macro_build (NULL, s, "t,r,.", op[0], op[1],
10391 (int) imm_expr.X_add_number);
df58fc94
RS
10392 break;
10393 }
10394 goto do_addi_i;
252b5132
RH
10395 case M_DADDU_I:
10396 dbl = 1;
10397 s = "daddiu";
10398 s2 = "daddu";
10399 do_addi:
b0e6f033 10400 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10401 && imm_expr.X_add_number < 0x8000)
10402 {
c0ebe874 10403 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10404 break;
252b5132 10405 }
df58fc94 10406 do_addi_i:
8fc2e39e 10407 used_at = 1;
67c0d1eb 10408 load_register (AT, &imm_expr, dbl);
c0ebe874 10409 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10410 break;
10411
10412 case M_AND_I:
10413 s = "andi";
10414 s2 = "and";
10415 goto do_bit;
10416 case M_OR_I:
10417 s = "ori";
10418 s2 = "or";
10419 goto do_bit;
10420 case M_NOR_I:
10421 s = "";
10422 s2 = "nor";
10423 goto do_bit;
10424 case M_XOR_I:
10425 s = "xori";
10426 s2 = "xor";
10427 do_bit:
b0e6f033 10428 if (imm_expr.X_add_number >= 0
252b5132
RH
10429 && imm_expr.X_add_number < 0x10000)
10430 {
10431 if (mask != M_NOR_I)
c0ebe874 10432 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10433 else
10434 {
67c0d1eb 10435 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10436 op[0], op[1], BFD_RELOC_LO16);
10437 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10438 }
8fc2e39e 10439 break;
252b5132
RH
10440 }
10441
8fc2e39e 10442 used_at = 1;
bad1aba3 10443 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10444 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10445 break;
10446
8b082fb1
TS
10447 case M_BALIGN:
10448 switch (imm_expr.X_add_number)
10449 {
10450 case 0:
10451 macro_build (NULL, "nop", "");
10452 break;
10453 case 2:
c0ebe874 10454 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10455 break;
03f66e8a
MR
10456 case 1:
10457 case 3:
c0ebe874 10458 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10459 (int) imm_expr.X_add_number);
8b082fb1 10460 break;
03f66e8a
MR
10461 default:
10462 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10463 (unsigned long) imm_expr.X_add_number);
10464 break;
8b082fb1
TS
10465 }
10466 break;
10467
df58fc94
RS
10468 case M_BC1FL:
10469 case M_BC1TL:
10470 case M_BC2FL:
10471 case M_BC2TL:
10472 gas_assert (mips_opts.micromips);
10473 macro_build_branch_ccl (mask, &offset_expr,
10474 EXTRACT_OPERAND (1, BCC, *ip));
10475 break;
10476
252b5132 10477 case M_BEQ_I:
252b5132 10478 case M_BEQL_I:
252b5132 10479 case M_BNE_I:
252b5132 10480 case M_BNEL_I:
b0e6f033 10481 if (imm_expr.X_add_number == 0)
c0ebe874 10482 op[1] = 0;
df58fc94 10483 else
252b5132 10484 {
c0ebe874 10485 op[1] = AT;
df58fc94 10486 used_at = 1;
bad1aba3 10487 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10488 }
df58fc94
RS
10489 /* Fall through. */
10490 case M_BEQL:
10491 case M_BNEL:
c0ebe874 10492 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10493 break;
10494
10495 case M_BGEL:
10496 likely = 1;
1a0670f3 10497 /* Fall through. */
252b5132 10498 case M_BGE:
c0ebe874
RS
10499 if (op[1] == 0)
10500 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10501 else if (op[0] == 0)
10502 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10503 else
252b5132 10504 {
df58fc94 10505 used_at = 1;
c0ebe874 10506 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10507 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10508 &offset_expr, AT, ZERO);
252b5132 10509 }
df58fc94
RS
10510 break;
10511
10512 case M_BGEZL:
10513 case M_BGEZALL:
10514 case M_BGTZL:
10515 case M_BLEZL:
10516 case M_BLTZL:
10517 case M_BLTZALL:
c0ebe874 10518 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10519 break;
10520
10521 case M_BGTL_I:
10522 likely = 1;
1a0670f3 10523 /* Fall through. */
252b5132 10524 case M_BGT_I:
90ecf173 10525 /* Check for > max integer. */
b0e6f033 10526 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10527 {
10528 do_false:
90ecf173 10529 /* Result is always false. */
252b5132 10530 if (! likely)
a605d2b3 10531 macro_build (NULL, "nop", "");
252b5132 10532 else
df58fc94 10533 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10534 break;
252b5132 10535 }
f9419b05 10536 ++imm_expr.X_add_number;
6f2117ba 10537 /* Fall through. */
252b5132
RH
10538 case M_BGE_I:
10539 case M_BGEL_I:
10540 if (mask == M_BGEL_I)
10541 likely = 1;
b0e6f033 10542 if (imm_expr.X_add_number == 0)
252b5132 10543 {
df58fc94 10544 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10545 &offset_expr, op[0]);
8fc2e39e 10546 break;
252b5132 10547 }
b0e6f033 10548 if (imm_expr.X_add_number == 1)
252b5132 10549 {
df58fc94 10550 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10551 &offset_expr, op[0]);
8fc2e39e 10552 break;
252b5132 10553 }
b0e6f033 10554 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10555 {
10556 do_true:
6f2117ba 10557 /* Result is always true. */
1661c76c 10558 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10559 macro_build (&offset_expr, "b", "p");
8fc2e39e 10560 break;
252b5132 10561 }
8fc2e39e 10562 used_at = 1;
c0ebe874 10563 set_at (op[0], 0);
df58fc94
RS
10564 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10565 &offset_expr, AT, ZERO);
252b5132
RH
10566 break;
10567
10568 case M_BGEUL:
10569 likely = 1;
1a0670f3 10570 /* Fall through. */
252b5132 10571 case M_BGEU:
c0ebe874 10572 if (op[1] == 0)
252b5132 10573 goto do_true;
c0ebe874 10574 else if (op[0] == 0)
df58fc94 10575 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10576 &offset_expr, ZERO, op[1]);
df58fc94 10577 else
252b5132 10578 {
df58fc94 10579 used_at = 1;
c0ebe874 10580 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10581 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10582 &offset_expr, AT, ZERO);
252b5132 10583 }
252b5132
RH
10584 break;
10585
10586 case M_BGTUL_I:
10587 likely = 1;
1a0670f3 10588 /* Fall through. */
252b5132 10589 case M_BGTU_I:
c0ebe874 10590 if (op[0] == 0
bad1aba3 10591 || (GPR_SIZE == 32
f01dc953 10592 && imm_expr.X_add_number == -1))
252b5132 10593 goto do_false;
f9419b05 10594 ++imm_expr.X_add_number;
6f2117ba 10595 /* Fall through. */
252b5132
RH
10596 case M_BGEU_I:
10597 case M_BGEUL_I:
10598 if (mask == M_BGEUL_I)
10599 likely = 1;
b0e6f033 10600 if (imm_expr.X_add_number == 0)
252b5132 10601 goto do_true;
b0e6f033 10602 else if (imm_expr.X_add_number == 1)
df58fc94 10603 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10604 &offset_expr, op[0], ZERO);
df58fc94 10605 else
252b5132 10606 {
df58fc94 10607 used_at = 1;
c0ebe874 10608 set_at (op[0], 1);
df58fc94
RS
10609 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10610 &offset_expr, AT, ZERO);
252b5132 10611 }
252b5132
RH
10612 break;
10613
10614 case M_BGTL:
10615 likely = 1;
1a0670f3 10616 /* Fall through. */
252b5132 10617 case M_BGT:
c0ebe874
RS
10618 if (op[1] == 0)
10619 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10620 else if (op[0] == 0)
10621 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10622 else
252b5132 10623 {
df58fc94 10624 used_at = 1;
c0ebe874 10625 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10626 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10627 &offset_expr, AT, ZERO);
252b5132 10628 }
252b5132
RH
10629 break;
10630
10631 case M_BGTUL:
10632 likely = 1;
1a0670f3 10633 /* Fall through. */
252b5132 10634 case M_BGTU:
c0ebe874 10635 if (op[1] == 0)
df58fc94 10636 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10637 &offset_expr, op[0], ZERO);
10638 else if (op[0] == 0)
df58fc94
RS
10639 goto do_false;
10640 else
252b5132 10641 {
df58fc94 10642 used_at = 1;
c0ebe874 10643 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10644 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10645 &offset_expr, AT, ZERO);
252b5132 10646 }
252b5132
RH
10647 break;
10648
10649 case M_BLEL:
10650 likely = 1;
1a0670f3 10651 /* Fall through. */
252b5132 10652 case M_BLE:
c0ebe874
RS
10653 if (op[1] == 0)
10654 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10655 else if (op[0] == 0)
10656 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10657 else
252b5132 10658 {
df58fc94 10659 used_at = 1;
c0ebe874 10660 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10661 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10662 &offset_expr, AT, ZERO);
252b5132 10663 }
252b5132
RH
10664 break;
10665
10666 case M_BLEL_I:
10667 likely = 1;
1a0670f3 10668 /* Fall through. */
252b5132 10669 case M_BLE_I:
b0e6f033 10670 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10671 goto do_true;
f9419b05 10672 ++imm_expr.X_add_number;
6f2117ba 10673 /* Fall through. */
252b5132
RH
10674 case M_BLT_I:
10675 case M_BLTL_I:
10676 if (mask == M_BLTL_I)
10677 likely = 1;
b0e6f033 10678 if (imm_expr.X_add_number == 0)
c0ebe874 10679 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10680 else if (imm_expr.X_add_number == 1)
c0ebe874 10681 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10682 else
252b5132 10683 {
df58fc94 10684 used_at = 1;
c0ebe874 10685 set_at (op[0], 0);
df58fc94
RS
10686 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10687 &offset_expr, AT, ZERO);
252b5132 10688 }
252b5132
RH
10689 break;
10690
10691 case M_BLEUL:
10692 likely = 1;
1a0670f3 10693 /* Fall through. */
252b5132 10694 case M_BLEU:
c0ebe874 10695 if (op[1] == 0)
df58fc94 10696 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10697 &offset_expr, op[0], ZERO);
10698 else if (op[0] == 0)
df58fc94
RS
10699 goto do_true;
10700 else
252b5132 10701 {
df58fc94 10702 used_at = 1;
c0ebe874 10703 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10704 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10705 &offset_expr, AT, ZERO);
252b5132 10706 }
252b5132
RH
10707 break;
10708
10709 case M_BLEUL_I:
10710 likely = 1;
1a0670f3 10711 /* Fall through. */
252b5132 10712 case M_BLEU_I:
c0ebe874 10713 if (op[0] == 0
bad1aba3 10714 || (GPR_SIZE == 32
f01dc953 10715 && imm_expr.X_add_number == -1))
252b5132 10716 goto do_true;
f9419b05 10717 ++imm_expr.X_add_number;
6f2117ba 10718 /* Fall through. */
252b5132
RH
10719 case M_BLTU_I:
10720 case M_BLTUL_I:
10721 if (mask == M_BLTUL_I)
10722 likely = 1;
b0e6f033 10723 if (imm_expr.X_add_number == 0)
252b5132 10724 goto do_false;
b0e6f033 10725 else if (imm_expr.X_add_number == 1)
df58fc94 10726 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10727 &offset_expr, op[0], ZERO);
df58fc94 10728 else
252b5132 10729 {
df58fc94 10730 used_at = 1;
c0ebe874 10731 set_at (op[0], 1);
df58fc94
RS
10732 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10733 &offset_expr, AT, ZERO);
252b5132 10734 }
252b5132
RH
10735 break;
10736
10737 case M_BLTL:
10738 likely = 1;
1a0670f3 10739 /* Fall through. */
252b5132 10740 case M_BLT:
c0ebe874
RS
10741 if (op[1] == 0)
10742 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10743 else if (op[0] == 0)
10744 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10745 else
252b5132 10746 {
df58fc94 10747 used_at = 1;
c0ebe874 10748 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10749 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10750 &offset_expr, AT, ZERO);
252b5132 10751 }
252b5132
RH
10752 break;
10753
10754 case M_BLTUL:
10755 likely = 1;
1a0670f3 10756 /* Fall through. */
252b5132 10757 case M_BLTU:
c0ebe874 10758 if (op[1] == 0)
252b5132 10759 goto do_false;
c0ebe874 10760 else if (op[0] == 0)
df58fc94 10761 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10762 &offset_expr, ZERO, op[1]);
df58fc94 10763 else
252b5132 10764 {
df58fc94 10765 used_at = 1;
c0ebe874 10766 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10767 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10768 &offset_expr, AT, ZERO);
252b5132 10769 }
252b5132
RH
10770 break;
10771
10772 case M_DDIV_3:
10773 dbl = 1;
1a0670f3 10774 /* Fall through. */
252b5132
RH
10775 case M_DIV_3:
10776 s = "mflo";
10777 goto do_div3;
10778 case M_DREM_3:
10779 dbl = 1;
1a0670f3 10780 /* Fall through. */
252b5132
RH
10781 case M_REM_3:
10782 s = "mfhi";
10783 do_div3:
c0ebe874 10784 if (op[2] == 0)
252b5132 10785 {
1661c76c 10786 as_warn (_("divide by zero"));
252b5132 10787 if (mips_trap)
df58fc94 10788 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10789 else
df58fc94 10790 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10791 break;
252b5132
RH
10792 }
10793
7d10b47d 10794 start_noreorder ();
252b5132
RH
10795 if (mips_trap)
10796 {
c0ebe874
RS
10797 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10798 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10799 }
10800 else
10801 {
df58fc94
RS
10802 if (mips_opts.micromips)
10803 micromips_label_expr (&label_expr);
10804 else
10805 label_expr.X_add_number = 8;
c0ebe874
RS
10806 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10807 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10808 macro_build (NULL, "break", BRK_FMT, 7);
10809 if (mips_opts.micromips)
10810 micromips_add_label ();
252b5132
RH
10811 }
10812 expr1.X_add_number = -1;
8fc2e39e 10813 used_at = 1;
f6a22291 10814 load_register (AT, &expr1, dbl);
df58fc94
RS
10815 if (mips_opts.micromips)
10816 micromips_label_expr (&label_expr);
10817 else
10818 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10819 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10820 if (dbl)
10821 {
10822 expr1.X_add_number = 1;
f6a22291 10823 load_register (AT, &expr1, dbl);
df58fc94 10824 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10825 }
10826 else
10827 {
10828 expr1.X_add_number = 0x80000000;
df58fc94 10829 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10830 }
10831 if (mips_trap)
10832 {
c0ebe874 10833 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10834 /* We want to close the noreorder block as soon as possible, so
10835 that later insns are available for delay slot filling. */
7d10b47d 10836 end_noreorder ();
252b5132
RH
10837 }
10838 else
10839 {
df58fc94
RS
10840 if (mips_opts.micromips)
10841 micromips_label_expr (&label_expr);
10842 else
10843 label_expr.X_add_number = 8;
c0ebe874 10844 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10845 macro_build (NULL, "nop", "");
252b5132
RH
10846
10847 /* We want to close the noreorder block as soon as possible, so
10848 that later insns are available for delay slot filling. */
7d10b47d 10849 end_noreorder ();
252b5132 10850
df58fc94 10851 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10852 }
df58fc94
RS
10853 if (mips_opts.micromips)
10854 micromips_add_label ();
c0ebe874 10855 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10856 break;
10857
10858 case M_DIV_3I:
10859 s = "div";
10860 s2 = "mflo";
10861 goto do_divi;
10862 case M_DIVU_3I:
10863 s = "divu";
10864 s2 = "mflo";
10865 goto do_divi;
10866 case M_REM_3I:
10867 s = "div";
10868 s2 = "mfhi";
10869 goto do_divi;
10870 case M_REMU_3I:
10871 s = "divu";
10872 s2 = "mfhi";
10873 goto do_divi;
10874 case M_DDIV_3I:
10875 dbl = 1;
10876 s = "ddiv";
10877 s2 = "mflo";
10878 goto do_divi;
10879 case M_DDIVU_3I:
10880 dbl = 1;
10881 s = "ddivu";
10882 s2 = "mflo";
10883 goto do_divi;
10884 case M_DREM_3I:
10885 dbl = 1;
10886 s = "ddiv";
10887 s2 = "mfhi";
10888 goto do_divi;
10889 case M_DREMU_3I:
10890 dbl = 1;
10891 s = "ddivu";
10892 s2 = "mfhi";
10893 do_divi:
b0e6f033 10894 if (imm_expr.X_add_number == 0)
252b5132 10895 {
1661c76c 10896 as_warn (_("divide by zero"));
252b5132 10897 if (mips_trap)
df58fc94 10898 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10899 else
df58fc94 10900 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10901 break;
252b5132 10902 }
b0e6f033 10903 if (imm_expr.X_add_number == 1)
252b5132
RH
10904 {
10905 if (strcmp (s2, "mflo") == 0)
c0ebe874 10906 move_register (op[0], op[1]);
252b5132 10907 else
c0ebe874 10908 move_register (op[0], ZERO);
8fc2e39e 10909 break;
252b5132 10910 }
b0e6f033 10911 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10912 {
10913 if (strcmp (s2, "mflo") == 0)
c0ebe874 10914 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10915 else
c0ebe874 10916 move_register (op[0], ZERO);
8fc2e39e 10917 break;
252b5132
RH
10918 }
10919
8fc2e39e 10920 used_at = 1;
67c0d1eb 10921 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10922 macro_build (NULL, s, "z,s,t", op[1], AT);
10923 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10924 break;
10925
10926 case M_DIVU_3:
10927 s = "divu";
10928 s2 = "mflo";
10929 goto do_divu3;
10930 case M_REMU_3:
10931 s = "divu";
10932 s2 = "mfhi";
10933 goto do_divu3;
10934 case M_DDIVU_3:
10935 s = "ddivu";
10936 s2 = "mflo";
10937 goto do_divu3;
10938 case M_DREMU_3:
10939 s = "ddivu";
10940 s2 = "mfhi";
10941 do_divu3:
7d10b47d 10942 start_noreorder ();
252b5132
RH
10943 if (mips_trap)
10944 {
c0ebe874
RS
10945 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10946 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10947 /* We want to close the noreorder block as soon as possible, so
10948 that later insns are available for delay slot filling. */
7d10b47d 10949 end_noreorder ();
252b5132
RH
10950 }
10951 else
10952 {
df58fc94
RS
10953 if (mips_opts.micromips)
10954 micromips_label_expr (&label_expr);
10955 else
10956 label_expr.X_add_number = 8;
c0ebe874
RS
10957 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10958 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10959
10960 /* We want to close the noreorder block as soon as possible, so
10961 that later insns are available for delay slot filling. */
7d10b47d 10962 end_noreorder ();
df58fc94
RS
10963 macro_build (NULL, "break", BRK_FMT, 7);
10964 if (mips_opts.micromips)
10965 micromips_add_label ();
252b5132 10966 }
c0ebe874 10967 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10968 break;
252b5132 10969
1abe91b1
MR
10970 case M_DLCA_AB:
10971 dbl = 1;
1a0670f3 10972 /* Fall through. */
1abe91b1
MR
10973 case M_LCA_AB:
10974 call = 1;
10975 goto do_la;
252b5132
RH
10976 case M_DLA_AB:
10977 dbl = 1;
1a0670f3 10978 /* Fall through. */
252b5132 10979 case M_LA_AB:
1abe91b1 10980 do_la:
252b5132
RH
10981 /* Load the address of a symbol into a register. If breg is not
10982 zero, we then add a base register to it. */
10983
c0ebe874 10984 breg = op[2];
bad1aba3 10985 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10986 as_warn (_("dla used to load 32-bit register; recommend using la "
10987 "instead"));
3bec30a8 10988
90ecf173 10989 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10990 as_warn (_("la used to load 64-bit address; recommend using dla "
10991 "instead"));
3bec30a8 10992
f2ae14a1 10993 if (small_offset_p (0, align, 16))
0c11417f 10994 {
c0ebe874 10995 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10996 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10997 break;
0c11417f
MR
10998 }
10999
c0ebe874 11000 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
11001 {
11002 tempreg = AT;
11003 used_at = 1;
11004 }
11005 else
c0ebe874 11006 tempreg = op[0];
afdbd6d0 11007
252b5132
RH
11008 if (offset_expr.X_op != O_symbol
11009 && offset_expr.X_op != O_constant)
11010 {
1661c76c 11011 as_bad (_("expression too complex"));
252b5132
RH
11012 offset_expr.X_op = O_constant;
11013 }
11014
252b5132 11015 if (offset_expr.X_op == O_constant)
aed1a261 11016 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
11017 else if (mips_pic == NO_PIC)
11018 {
d6bc6245 11019 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 11020 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
11021 Otherwise we want
11022 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11023 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11024 If we have a constant, we need two instructions anyhow,
d6bc6245 11025 so we may as well always use the latter form.
76b3015f 11026
6caf9ef4
TS
11027 With 64bit address space and a usable $at we want
11028 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11029 lui $at,<sym> (BFD_RELOC_HI16_S)
11030 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11031 daddiu $at,<sym> (BFD_RELOC_LO16)
11032 dsll32 $tempreg,0
11033 daddu $tempreg,$tempreg,$at
11034
11035 If $at is already in use, we use a path which is suboptimal
11036 on superscalar processors.
11037 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11038 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11039 dsll $tempreg,16
11040 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11041 dsll $tempreg,16
11042 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11043
11044 For GP relative symbols in 64bit address space we can use
11045 the same sequence as in 32bit address space. */
aed1a261 11046 if (HAVE_64BIT_SYMBOLS)
252b5132 11047 {
6caf9ef4
TS
11048 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11049 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11050 {
11051 relax_start (offset_expr.X_add_symbol);
11052 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11053 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11054 relax_switch ();
11055 }
d6bc6245 11056
741fe287 11057 if (used_at == 0 && mips_opts.at)
98d3f06f 11058 {
df58fc94 11059 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11060 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11061 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11062 AT, BFD_RELOC_HI16_S);
67c0d1eb 11063 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11064 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11065 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11066 AT, AT, BFD_RELOC_LO16);
df58fc94 11067 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11068 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11069 used_at = 1;
11070 }
11071 else
11072 {
df58fc94 11073 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11074 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11075 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11076 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11077 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11078 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11079 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 11080 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11081 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11082 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11083 }
6caf9ef4
TS
11084
11085 if (mips_relax.sequence)
11086 relax_end ();
98d3f06f
KH
11087 }
11088 else
11089 {
11090 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11091 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11092 {
4d7206a2 11093 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11094 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11095 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11096 relax_switch ();
98d3f06f 11097 }
6943caf0 11098 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11099 as_bad (_("offset too large"));
67c0d1eb
RS
11100 macro_build_lui (&offset_expr, tempreg);
11101 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11102 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11103 if (mips_relax.sequence)
11104 relax_end ();
98d3f06f 11105 }
252b5132 11106 }
0a44bf69 11107 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11108 {
9117d219
NC
11109 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11110
252b5132
RH
11111 /* If this is a reference to an external symbol, and there
11112 is no constant, we want
11113 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11114 or for lca or if tempreg is PIC_CALL_REG
9117d219 11115 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11116 For a local symbol, we want
11117 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11118 nop
11119 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11120
11121 If we have a small constant, and this is a reference to
11122 an external symbol, we want
11123 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11124 nop
11125 addiu $tempreg,$tempreg,<constant>
11126 For a local symbol, we want the same instruction
11127 sequence, but we output a BFD_RELOC_LO16 reloc on the
11128 addiu instruction.
11129
11130 If we have a large constant, and this is a reference to
11131 an external symbol, we want
11132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11133 lui $at,<hiconstant>
11134 addiu $at,$at,<loconstant>
11135 addu $tempreg,$tempreg,$at
11136 For a local symbol, we want the same instruction
11137 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11138 addiu instruction.
ed6fb7bd
SC
11139 */
11140
4d7206a2 11141 if (offset_expr.X_add_number == 0)
252b5132 11142 {
0a44bf69
RS
11143 if (mips_pic == SVR4_PIC
11144 && breg == 0
11145 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11146 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11147
11148 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11149 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11150 lw_reloc_type, mips_gp_register);
4d7206a2 11151 if (breg != 0)
252b5132
RH
11152 {
11153 /* We're going to put in an addu instruction using
11154 tempreg, so we may as well insert the nop right
11155 now. */
269137b2 11156 load_delay_nop ();
252b5132 11157 }
4d7206a2 11158 relax_switch ();
67c0d1eb
RS
11159 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11160 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11161 load_delay_nop ();
67c0d1eb
RS
11162 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11163 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11164 relax_end ();
252b5132
RH
11165 /* FIXME: If breg == 0, and the next instruction uses
11166 $tempreg, then if this variant case is used an extra
11167 nop will be generated. */
11168 }
4d7206a2
RS
11169 else if (offset_expr.X_add_number >= -0x8000
11170 && offset_expr.X_add_number < 0x8000)
252b5132 11171 {
67c0d1eb 11172 load_got_offset (tempreg, &offset_expr);
269137b2 11173 load_delay_nop ();
67c0d1eb 11174 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11175 }
11176 else
11177 {
4d7206a2
RS
11178 expr1.X_add_number = offset_expr.X_add_number;
11179 offset_expr.X_add_number =
43c0598f 11180 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11181 load_got_offset (tempreg, &offset_expr);
f6a22291 11182 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11183 /* If we are going to add in a base register, and the
11184 target register and the base register are the same,
11185 then we are using AT as a temporary register. Since
11186 we want to load the constant into AT, we add our
11187 current AT (from the global offset table) and the
11188 register into the register now, and pretend we were
11189 not using a base register. */
c0ebe874 11190 if (breg == op[0])
252b5132 11191 {
269137b2 11192 load_delay_nop ();
67c0d1eb 11193 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11194 op[0], AT, breg);
252b5132 11195 breg = 0;
c0ebe874 11196 tempreg = op[0];
252b5132 11197 }
f6a22291 11198 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11199 used_at = 1;
11200 }
11201 }
0a44bf69 11202 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11203 {
67c0d1eb 11204 int add_breg_early = 0;
f5040a92
AO
11205
11206 /* If this is a reference to an external, and there is no
11207 constant, or local symbol (*), with or without a
11208 constant, we want
11209 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11210 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11211 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11212
11213 If we have a small constant, and this is a reference to
11214 an external symbol, we want
11215 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11216 addiu $tempreg,$tempreg,<constant>
11217
11218 If we have a large constant, and this is a reference to
11219 an external symbol, we want
11220 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11221 lui $at,<hiconstant>
11222 addiu $at,$at,<loconstant>
11223 addu $tempreg,$tempreg,$at
11224
11225 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11226 local symbols, even though it introduces an additional
11227 instruction. */
11228
f5040a92
AO
11229 if (offset_expr.X_add_number)
11230 {
4d7206a2 11231 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11232 offset_expr.X_add_number = 0;
11233
4d7206a2 11234 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11235 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11236 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11237
11238 if (expr1.X_add_number >= -0x8000
11239 && expr1.X_add_number < 0x8000)
11240 {
67c0d1eb
RS
11241 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11242 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11243 }
ecd13cd3 11244 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11245 {
c0ebe874
RS
11246 unsigned int dreg;
11247
f5040a92
AO
11248 /* If we are going to add in a base register, and the
11249 target register and the base register are the same,
11250 then we are using AT as a temporary register. Since
11251 we want to load the constant into AT, we add our
11252 current AT (from the global offset table) and the
11253 register into the register now, and pretend we were
11254 not using a base register. */
c0ebe874 11255 if (breg != op[0])
f5040a92
AO
11256 dreg = tempreg;
11257 else
11258 {
9c2799c2 11259 gas_assert (tempreg == AT);
67c0d1eb 11260 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11261 op[0], AT, breg);
11262 dreg = op[0];
67c0d1eb 11263 add_breg_early = 1;
f5040a92
AO
11264 }
11265
f6a22291 11266 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11267 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11268 dreg, dreg, AT);
f5040a92 11269
f5040a92
AO
11270 used_at = 1;
11271 }
11272 else
11273 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11274
4d7206a2 11275 relax_switch ();
f5040a92
AO
11276 offset_expr.X_add_number = expr1.X_add_number;
11277
67c0d1eb
RS
11278 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11279 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11280 if (add_breg_early)
f5040a92 11281 {
67c0d1eb 11282 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11283 op[0], tempreg, breg);
f5040a92 11284 breg = 0;
c0ebe874 11285 tempreg = op[0];
f5040a92 11286 }
4d7206a2 11287 relax_end ();
f5040a92 11288 }
4d7206a2 11289 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11290 {
4d7206a2 11291 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11292 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11293 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11294 relax_switch ();
67c0d1eb
RS
11295 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11296 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11297 relax_end ();
f5040a92 11298 }
4d7206a2 11299 else
f5040a92 11300 {
67c0d1eb
RS
11301 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11302 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11303 }
11304 }
0a44bf69 11305 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11306 {
67c0d1eb 11307 int gpdelay;
9117d219
NC
11308 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11309 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11310 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11311
11312 /* This is the large GOT case. If this is a reference to an
11313 external symbol, and there is no constant, we want
11314 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11315 addu $tempreg,$tempreg,$gp
11316 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11317 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11318 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11319 addu $tempreg,$tempreg,$gp
11320 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11321 For a local symbol, we want
11322 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11323 nop
11324 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11325
11326 If we have a small constant, and this is a reference to
11327 an external symbol, we want
11328 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11329 addu $tempreg,$tempreg,$gp
11330 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11331 nop
11332 addiu $tempreg,$tempreg,<constant>
11333 For a local symbol, we want
11334 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11335 nop
11336 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11337
11338 If we have a large constant, and this is a reference to
11339 an external symbol, we want
11340 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11341 addu $tempreg,$tempreg,$gp
11342 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11343 lui $at,<hiconstant>
11344 addiu $at,$at,<loconstant>
11345 addu $tempreg,$tempreg,$at
11346 For a local symbol, we want
11347 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11348 lui $at,<hiconstant>
11349 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11350 addu $tempreg,$tempreg,$at
f5040a92 11351 */
438c16b8 11352
252b5132
RH
11353 expr1.X_add_number = offset_expr.X_add_number;
11354 offset_expr.X_add_number = 0;
4d7206a2 11355 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11356 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11357 if (expr1.X_add_number == 0 && breg == 0
11358 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11359 {
11360 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11361 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11362 }
df58fc94 11363 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11364 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11365 tempreg, tempreg, mips_gp_register);
67c0d1eb 11366 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11367 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11368 if (expr1.X_add_number == 0)
11369 {
67c0d1eb 11370 if (breg != 0)
252b5132
RH
11371 {
11372 /* We're going to put in an addu instruction using
11373 tempreg, so we may as well insert the nop right
11374 now. */
269137b2 11375 load_delay_nop ();
252b5132 11376 }
252b5132
RH
11377 }
11378 else if (expr1.X_add_number >= -0x8000
11379 && expr1.X_add_number < 0x8000)
11380 {
269137b2 11381 load_delay_nop ();
67c0d1eb 11382 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11383 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11384 }
11385 else
11386 {
c0ebe874
RS
11387 unsigned int dreg;
11388
252b5132
RH
11389 /* If we are going to add in a base register, and the
11390 target register and the base register are the same,
11391 then we are using AT as a temporary register. Since
11392 we want to load the constant into AT, we add our
11393 current AT (from the global offset table) and the
11394 register into the register now, and pretend we were
11395 not using a base register. */
c0ebe874 11396 if (breg != op[0])
67c0d1eb 11397 dreg = tempreg;
252b5132
RH
11398 else
11399 {
9c2799c2 11400 gas_assert (tempreg == AT);
269137b2 11401 load_delay_nop ();
67c0d1eb 11402 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11403 op[0], AT, breg);
11404 dreg = op[0];
252b5132
RH
11405 }
11406
f6a22291 11407 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11408 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11409
252b5132
RH
11410 used_at = 1;
11411 }
43c0598f 11412 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11413 relax_switch ();
252b5132 11414
67c0d1eb 11415 if (gpdelay)
252b5132
RH
11416 {
11417 /* This is needed because this instruction uses $gp, but
f5040a92 11418 the first instruction on the main stream does not. */
67c0d1eb 11419 macro_build (NULL, "nop", "");
252b5132 11420 }
ed6fb7bd 11421
67c0d1eb
RS
11422 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11423 local_reloc_type, mips_gp_register);
f5040a92 11424 if (expr1.X_add_number >= -0x8000
252b5132
RH
11425 && expr1.X_add_number < 0x8000)
11426 {
269137b2 11427 load_delay_nop ();
67c0d1eb
RS
11428 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11429 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11430 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11431 register, the external symbol case ended with a load,
11432 so if the symbol turns out to not be external, and
11433 the next instruction uses tempreg, an unnecessary nop
11434 will be inserted. */
252b5132
RH
11435 }
11436 else
11437 {
c0ebe874 11438 if (breg == op[0])
252b5132
RH
11439 {
11440 /* We must add in the base register now, as in the
f5040a92 11441 external symbol case. */
9c2799c2 11442 gas_assert (tempreg == AT);
269137b2 11443 load_delay_nop ();
67c0d1eb 11444 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11445 op[0], AT, breg);
11446 tempreg = op[0];
252b5132 11447 /* We set breg to 0 because we have arranged to add
f5040a92 11448 it in in both cases. */
252b5132
RH
11449 breg = 0;
11450 }
11451
67c0d1eb
RS
11452 macro_build_lui (&expr1, AT);
11453 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11454 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11455 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11456 tempreg, tempreg, AT);
8fc2e39e 11457 used_at = 1;
252b5132 11458 }
4d7206a2 11459 relax_end ();
252b5132 11460 }
0a44bf69 11461 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11462 {
f5040a92
AO
11463 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11464 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11465 int add_breg_early = 0;
f5040a92
AO
11466
11467 /* This is the large GOT case. If this is a reference to an
11468 external symbol, and there is no constant, we want
11469 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11470 add $tempreg,$tempreg,$gp
11471 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11472 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11473 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11474 add $tempreg,$tempreg,$gp
11475 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11476
11477 If we have a small constant, and this is a reference to
11478 an external symbol, we want
11479 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11480 add $tempreg,$tempreg,$gp
11481 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11482 addi $tempreg,$tempreg,<constant>
11483
11484 If we have a large constant, and this is a reference to
11485 an external symbol, we want
11486 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11487 addu $tempreg,$tempreg,$gp
11488 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11489 lui $at,<hiconstant>
11490 addi $at,$at,<loconstant>
11491 add $tempreg,$tempreg,$at
11492
11493 If we have NewABI, and we know it's a local symbol, we want
11494 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11495 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11496 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11497
4d7206a2 11498 relax_start (offset_expr.X_add_symbol);
f5040a92 11499
4d7206a2 11500 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11501 offset_expr.X_add_number = 0;
11502
1abe91b1
MR
11503 if (expr1.X_add_number == 0 && breg == 0
11504 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11505 {
11506 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11507 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11508 }
df58fc94 11509 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11510 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11511 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11512 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11513 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11514
11515 if (expr1.X_add_number == 0)
4d7206a2 11516 ;
f5040a92
AO
11517 else if (expr1.X_add_number >= -0x8000
11518 && expr1.X_add_number < 0x8000)
11519 {
67c0d1eb 11520 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11521 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11522 }
ecd13cd3 11523 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11524 {
c0ebe874
RS
11525 unsigned int dreg;
11526
f5040a92
AO
11527 /* If we are going to add in a base register, and the
11528 target register and the base register are the same,
11529 then we are using AT as a temporary register. Since
11530 we want to load the constant into AT, we add our
11531 current AT (from the global offset table) and the
11532 register into the register now, and pretend we were
11533 not using a base register. */
c0ebe874 11534 if (breg != op[0])
f5040a92
AO
11535 dreg = tempreg;
11536 else
11537 {
9c2799c2 11538 gas_assert (tempreg == AT);
67c0d1eb 11539 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11540 op[0], AT, breg);
11541 dreg = op[0];
67c0d1eb 11542 add_breg_early = 1;
f5040a92
AO
11543 }
11544
f6a22291 11545 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11546 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11547
f5040a92
AO
11548 used_at = 1;
11549 }
11550 else
11551 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11552
4d7206a2 11553 relax_switch ();
f5040a92 11554 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11555 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11556 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11557 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11558 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11559 if (add_breg_early)
f5040a92 11560 {
67c0d1eb 11561 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11562 op[0], tempreg, breg);
f5040a92 11563 breg = 0;
c0ebe874 11564 tempreg = op[0];
f5040a92 11565 }
4d7206a2 11566 relax_end ();
f5040a92 11567 }
252b5132
RH
11568 else
11569 abort ();
11570
11571 if (breg != 0)
c0ebe874 11572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11573 break;
11574
52b6b6b9 11575 case M_MSGSND:
df58fc94 11576 gas_assert (!mips_opts.micromips);
c0ebe874 11577 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11578 break;
52b6b6b9
JM
11579
11580 case M_MSGLD:
df58fc94 11581 gas_assert (!mips_opts.micromips);
c8276761 11582 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11583 break;
52b6b6b9
JM
11584
11585 case M_MSGLD_T:
df58fc94 11586 gas_assert (!mips_opts.micromips);
c0ebe874 11587 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11588 break;
52b6b6b9
JM
11589
11590 case M_MSGWAIT:
df58fc94 11591 gas_assert (!mips_opts.micromips);
52b6b6b9 11592 macro_build (NULL, "c2", "C", 3);
c7af4273 11593 break;
52b6b6b9
JM
11594
11595 case M_MSGWAIT_T:
df58fc94 11596 gas_assert (!mips_opts.micromips);
c0ebe874 11597 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11598 break;
52b6b6b9 11599
252b5132
RH
11600 case M_J_A:
11601 /* The j instruction may not be used in PIC code, since it
11602 requires an absolute address. We convert it to a b
11603 instruction. */
11604 if (mips_pic == NO_PIC)
67c0d1eb 11605 macro_build (&offset_expr, "j", "a");
252b5132 11606 else
67c0d1eb 11607 macro_build (&offset_expr, "b", "p");
8fc2e39e 11608 break;
252b5132
RH
11609
11610 /* The jal instructions must be handled as macros because when
11611 generating PIC code they expand to multi-instruction
11612 sequences. Normally they are simple instructions. */
df58fc94 11613 case M_JALS_1:
c0ebe874
RS
11614 op[1] = op[0];
11615 op[0] = RA;
df58fc94
RS
11616 /* Fall through. */
11617 case M_JALS_2:
11618 gas_assert (mips_opts.micromips);
833794fc
MR
11619 if (mips_opts.insn32)
11620 {
1661c76c 11621 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11622 break;
11623 }
df58fc94
RS
11624 jals = 1;
11625 goto jal;
252b5132 11626 case M_JAL_1:
c0ebe874
RS
11627 op[1] = op[0];
11628 op[0] = RA;
252b5132
RH
11629 /* Fall through. */
11630 case M_JAL_2:
df58fc94 11631 jal:
3e722fb5 11632 if (mips_pic == NO_PIC)
df58fc94
RS
11633 {
11634 s = jals ? "jalrs" : "jalr";
e64af278 11635 if (mips_opts.micromips
833794fc 11636 && !mips_opts.insn32
c0ebe874 11637 && op[0] == RA
e64af278 11638 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11639 macro_build (NULL, s, "mj", op[1]);
df58fc94 11640 else
c0ebe874 11641 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11642 }
0a44bf69 11643 else
252b5132 11644 {
df58fc94
RS
11645 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11646 && mips_cprestore_offset >= 0);
11647
c0ebe874 11648 if (op[1] != PIC_CALL_REG)
252b5132 11649 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11650
833794fc
MR
11651 s = ((mips_opts.micromips
11652 && !mips_opts.insn32
11653 && (!mips_opts.noreorder || cprestore))
df58fc94 11654 ? "jalrs" : "jalr");
e64af278 11655 if (mips_opts.micromips
833794fc 11656 && !mips_opts.insn32
c0ebe874 11657 && op[0] == RA
e64af278 11658 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11659 macro_build (NULL, s, "mj", op[1]);
df58fc94 11660 else
c0ebe874 11661 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11662 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11663 {
6478892d 11664 if (mips_cprestore_offset < 0)
1661c76c 11665 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11666 else
11667 {
90ecf173 11668 if (!mips_frame_reg_valid)
7a621144 11669 {
1661c76c 11670 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11671 /* Quiet this warning. */
11672 mips_frame_reg_valid = 1;
11673 }
90ecf173 11674 if (!mips_cprestore_valid)
7a621144 11675 {
1661c76c 11676 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11677 /* Quiet this warning. */
11678 mips_cprestore_valid = 1;
11679 }
d3fca0b5
MR
11680 if (mips_opts.noreorder)
11681 macro_build (NULL, "nop", "");
6478892d 11682 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11683 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11684 mips_gp_register,
256ab948
TS
11685 mips_frame_reg,
11686 HAVE_64BIT_ADDRESSES);
6478892d 11687 }
252b5132
RH
11688 }
11689 }
252b5132 11690
8fc2e39e 11691 break;
252b5132 11692
df58fc94
RS
11693 case M_JALS_A:
11694 gas_assert (mips_opts.micromips);
833794fc
MR
11695 if (mips_opts.insn32)
11696 {
1661c76c 11697 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11698 break;
11699 }
df58fc94
RS
11700 jals = 1;
11701 /* Fall through. */
252b5132
RH
11702 case M_JAL_A:
11703 if (mips_pic == NO_PIC)
df58fc94 11704 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11705 else if (mips_pic == SVR4_PIC)
11706 {
11707 /* If this is a reference to an external symbol, and we are
11708 using a small GOT, we want
11709 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11710 nop
f9419b05 11711 jalr $ra,$25
252b5132
RH
11712 nop
11713 lw $gp,cprestore($sp)
11714 The cprestore value is set using the .cprestore
11715 pseudo-op. If we are using a big GOT, we want
11716 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11717 addu $25,$25,$gp
11718 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11719 nop
f9419b05 11720 jalr $ra,$25
252b5132
RH
11721 nop
11722 lw $gp,cprestore($sp)
11723 If the symbol is not external, we want
11724 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11725 nop
11726 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11727 jalr $ra,$25
252b5132 11728 nop
438c16b8 11729 lw $gp,cprestore($sp)
f5040a92
AO
11730
11731 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11732 sequences above, minus nops, unless the symbol is local,
11733 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11734 GOT_DISP. */
438c16b8 11735 if (HAVE_NEWABI)
252b5132 11736 {
90ecf173 11737 if (!mips_big_got)
f5040a92 11738 {
4d7206a2 11739 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11740 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11741 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11742 mips_gp_register);
4d7206a2 11743 relax_switch ();
67c0d1eb
RS
11744 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11745 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11746 mips_gp_register);
11747 relax_end ();
f5040a92
AO
11748 }
11749 else
11750 {
4d7206a2 11751 relax_start (offset_expr.X_add_symbol);
df58fc94 11752 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11753 BFD_RELOC_MIPS_CALL_HI16);
11754 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11755 PIC_CALL_REG, mips_gp_register);
11756 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11757 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11758 PIC_CALL_REG);
4d7206a2 11759 relax_switch ();
67c0d1eb
RS
11760 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11761 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11762 mips_gp_register);
11763 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11764 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11765 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11766 relax_end ();
f5040a92 11767 }
684022ea 11768
df58fc94 11769 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11770 }
11771 else
11772 {
4d7206a2 11773 relax_start (offset_expr.X_add_symbol);
90ecf173 11774 if (!mips_big_got)
438c16b8 11775 {
67c0d1eb
RS
11776 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11777 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11778 mips_gp_register);
269137b2 11779 load_delay_nop ();
4d7206a2 11780 relax_switch ();
438c16b8 11781 }
252b5132 11782 else
252b5132 11783 {
67c0d1eb
RS
11784 int gpdelay;
11785
11786 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11787 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11788 BFD_RELOC_MIPS_CALL_HI16);
11789 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11790 PIC_CALL_REG, mips_gp_register);
11791 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11792 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11793 PIC_CALL_REG);
269137b2 11794 load_delay_nop ();
4d7206a2 11795 relax_switch ();
67c0d1eb
RS
11796 if (gpdelay)
11797 macro_build (NULL, "nop", "");
252b5132 11798 }
67c0d1eb
RS
11799 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11800 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11801 mips_gp_register);
269137b2 11802 load_delay_nop ();
67c0d1eb
RS
11803 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11804 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11805 relax_end ();
df58fc94 11806 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11807
6478892d 11808 if (mips_cprestore_offset < 0)
1661c76c 11809 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11810 else
11811 {
90ecf173 11812 if (!mips_frame_reg_valid)
7a621144 11813 {
1661c76c 11814 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11815 /* Quiet this warning. */
11816 mips_frame_reg_valid = 1;
11817 }
90ecf173 11818 if (!mips_cprestore_valid)
7a621144 11819 {
1661c76c 11820 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11821 /* Quiet this warning. */
11822 mips_cprestore_valid = 1;
11823 }
6478892d 11824 if (mips_opts.noreorder)
67c0d1eb 11825 macro_build (NULL, "nop", "");
6478892d 11826 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11827 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11828 mips_gp_register,
256ab948
TS
11829 mips_frame_reg,
11830 HAVE_64BIT_ADDRESSES);
6478892d 11831 }
252b5132
RH
11832 }
11833 }
0a44bf69 11834 else if (mips_pic == VXWORKS_PIC)
1661c76c 11835 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11836 else
11837 abort ();
11838
8fc2e39e 11839 break;
252b5132 11840
7f3c4072 11841 case M_LBUE_AB:
7f3c4072
CM
11842 s = "lbue";
11843 fmt = "t,+j(b)";
11844 offbits = 9;
11845 goto ld_st;
11846 case M_LHUE_AB:
7f3c4072
CM
11847 s = "lhue";
11848 fmt = "t,+j(b)";
11849 offbits = 9;
11850 goto ld_st;
11851 case M_LBE_AB:
7f3c4072
CM
11852 s = "lbe";
11853 fmt = "t,+j(b)";
11854 offbits = 9;
11855 goto ld_st;
11856 case M_LHE_AB:
7f3c4072
CM
11857 s = "lhe";
11858 fmt = "t,+j(b)";
11859 offbits = 9;
11860 goto ld_st;
11861 case M_LLE_AB:
7f3c4072
CM
11862 s = "lle";
11863 fmt = "t,+j(b)";
11864 offbits = 9;
11865 goto ld_st;
11866 case M_LWE_AB:
7f3c4072
CM
11867 s = "lwe";
11868 fmt = "t,+j(b)";
11869 offbits = 9;
11870 goto ld_st;
11871 case M_LWLE_AB:
7f3c4072
CM
11872 s = "lwle";
11873 fmt = "t,+j(b)";
11874 offbits = 9;
11875 goto ld_st;
11876 case M_LWRE_AB:
7f3c4072
CM
11877 s = "lwre";
11878 fmt = "t,+j(b)";
11879 offbits = 9;
11880 goto ld_st;
11881 case M_SBE_AB:
7f3c4072
CM
11882 s = "sbe";
11883 fmt = "t,+j(b)";
11884 offbits = 9;
11885 goto ld_st;
11886 case M_SCE_AB:
7f3c4072
CM
11887 s = "sce";
11888 fmt = "t,+j(b)";
11889 offbits = 9;
11890 goto ld_st;
11891 case M_SHE_AB:
7f3c4072
CM
11892 s = "she";
11893 fmt = "t,+j(b)";
11894 offbits = 9;
11895 goto ld_st;
11896 case M_SWE_AB:
7f3c4072
CM
11897 s = "swe";
11898 fmt = "t,+j(b)";
11899 offbits = 9;
11900 goto ld_st;
11901 case M_SWLE_AB:
7f3c4072
CM
11902 s = "swle";
11903 fmt = "t,+j(b)";
11904 offbits = 9;
11905 goto ld_st;
11906 case M_SWRE_AB:
7f3c4072
CM
11907 s = "swre";
11908 fmt = "t,+j(b)";
11909 offbits = 9;
11910 goto ld_st;
dec0624d 11911 case M_ACLR_AB:
dec0624d 11912 s = "aclr";
dec0624d 11913 fmt = "\\,~(b)";
7f3c4072 11914 offbits = 12;
dec0624d
MR
11915 goto ld_st;
11916 case M_ASET_AB:
dec0624d 11917 s = "aset";
dec0624d 11918 fmt = "\\,~(b)";
7f3c4072 11919 offbits = 12;
dec0624d 11920 goto ld_st;
252b5132
RH
11921 case M_LB_AB:
11922 s = "lb";
df58fc94 11923 fmt = "t,o(b)";
252b5132
RH
11924 goto ld;
11925 case M_LBU_AB:
11926 s = "lbu";
df58fc94 11927 fmt = "t,o(b)";
252b5132
RH
11928 goto ld;
11929 case M_LH_AB:
11930 s = "lh";
df58fc94 11931 fmt = "t,o(b)";
252b5132
RH
11932 goto ld;
11933 case M_LHU_AB:
11934 s = "lhu";
df58fc94 11935 fmt = "t,o(b)";
252b5132
RH
11936 goto ld;
11937 case M_LW_AB:
11938 s = "lw";
df58fc94 11939 fmt = "t,o(b)";
252b5132
RH
11940 goto ld;
11941 case M_LWC0_AB:
df58fc94 11942 gas_assert (!mips_opts.micromips);
252b5132 11943 s = "lwc0";
df58fc94 11944 fmt = "E,o(b)";
bdaaa2e1 11945 /* Itbl support may require additional care here. */
252b5132 11946 coproc = 1;
df58fc94 11947 goto ld_st;
252b5132
RH
11948 case M_LWC1_AB:
11949 s = "lwc1";
df58fc94 11950 fmt = "T,o(b)";
bdaaa2e1 11951 /* Itbl support may require additional care here. */
252b5132 11952 coproc = 1;
df58fc94 11953 goto ld_st;
252b5132
RH
11954 case M_LWC2_AB:
11955 s = "lwc2";
df58fc94 11956 fmt = COP12_FMT;
7361da2c
AB
11957 offbits = (mips_opts.micromips ? 12
11958 : ISA_IS_R6 (mips_opts.isa) ? 11
11959 : 16);
bdaaa2e1 11960 /* Itbl support may require additional care here. */
252b5132 11961 coproc = 1;
df58fc94 11962 goto ld_st;
252b5132 11963 case M_LWC3_AB:
df58fc94 11964 gas_assert (!mips_opts.micromips);
252b5132 11965 s = "lwc3";
df58fc94 11966 fmt = "E,o(b)";
bdaaa2e1 11967 /* Itbl support may require additional care here. */
252b5132 11968 coproc = 1;
df58fc94 11969 goto ld_st;
252b5132
RH
11970 case M_LWL_AB:
11971 s = "lwl";
df58fc94 11972 fmt = MEM12_FMT;
7f3c4072 11973 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11974 goto ld_st;
252b5132
RH
11975 case M_LWR_AB:
11976 s = "lwr";
df58fc94 11977 fmt = MEM12_FMT;
7f3c4072 11978 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11979 goto ld_st;
252b5132 11980 case M_LDC1_AB:
252b5132 11981 s = "ldc1";
df58fc94 11982 fmt = "T,o(b)";
bdaaa2e1 11983 /* Itbl support may require additional care here. */
252b5132 11984 coproc = 1;
df58fc94 11985 goto ld_st;
252b5132
RH
11986 case M_LDC2_AB:
11987 s = "ldc2";
df58fc94 11988 fmt = COP12_FMT;
7361da2c
AB
11989 offbits = (mips_opts.micromips ? 12
11990 : ISA_IS_R6 (mips_opts.isa) ? 11
11991 : 16);
bdaaa2e1 11992 /* Itbl support may require additional care here. */
252b5132 11993 coproc = 1;
df58fc94 11994 goto ld_st;
c77c0862 11995 case M_LQC2_AB:
c77c0862 11996 s = "lqc2";
14daeee3 11997 fmt = "+7,o(b)";
c77c0862
RS
11998 /* Itbl support may require additional care here. */
11999 coproc = 1;
12000 goto ld_st;
252b5132
RH
12001 case M_LDC3_AB:
12002 s = "ldc3";
df58fc94 12003 fmt = "E,o(b)";
bdaaa2e1 12004 /* Itbl support may require additional care here. */
252b5132 12005 coproc = 1;
df58fc94 12006 goto ld_st;
252b5132
RH
12007 case M_LDL_AB:
12008 s = "ldl";
df58fc94 12009 fmt = MEM12_FMT;
7f3c4072 12010 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12011 goto ld_st;
252b5132
RH
12012 case M_LDR_AB:
12013 s = "ldr";
df58fc94 12014 fmt = MEM12_FMT;
7f3c4072 12015 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12016 goto ld_st;
252b5132
RH
12017 case M_LL_AB:
12018 s = "ll";
7361da2c
AB
12019 fmt = LL_SC_FMT;
12020 offbits = (mips_opts.micromips ? 12
12021 : ISA_IS_R6 (mips_opts.isa) ? 9
12022 : 16);
252b5132
RH
12023 goto ld;
12024 case M_LLD_AB:
12025 s = "lld";
7361da2c
AB
12026 fmt = LL_SC_FMT;
12027 offbits = (mips_opts.micromips ? 12
12028 : ISA_IS_R6 (mips_opts.isa) ? 9
12029 : 16);
252b5132
RH
12030 goto ld;
12031 case M_LWU_AB:
12032 s = "lwu";
df58fc94 12033 fmt = MEM12_FMT;
7f3c4072 12034 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12035 goto ld;
12036 case M_LWP_AB:
df58fc94
RS
12037 gas_assert (mips_opts.micromips);
12038 s = "lwp";
12039 fmt = "t,~(b)";
7f3c4072 12040 offbits = 12;
df58fc94
RS
12041 lp = 1;
12042 goto ld;
12043 case M_LDP_AB:
df58fc94
RS
12044 gas_assert (mips_opts.micromips);
12045 s = "ldp";
12046 fmt = "t,~(b)";
7f3c4072 12047 offbits = 12;
df58fc94
RS
12048 lp = 1;
12049 goto ld;
a45328b9
AB
12050 case M_LLDP_AB:
12051 case M_LLWP_AB:
41cee089 12052 case M_LLWPE_AB:
a45328b9
AB
12053 s = ip->insn_mo->name;
12054 fmt = "t,d,s";
12055 ll_sc_paired = 1;
12056 offbits = 0;
12057 goto ld;
df58fc94 12058 case M_LWM_AB:
df58fc94
RS
12059 gas_assert (mips_opts.micromips);
12060 s = "lwm";
12061 fmt = "n,~(b)";
7f3c4072 12062 offbits = 12;
df58fc94
RS
12063 goto ld_st;
12064 case M_LDM_AB:
df58fc94
RS
12065 gas_assert (mips_opts.micromips);
12066 s = "ldm";
12067 fmt = "n,~(b)";
7f3c4072 12068 offbits = 12;
df58fc94
RS
12069 goto ld_st;
12070
252b5132 12071 ld:
a45328b9
AB
12072 /* Try to use one the the load registers to compute the base address.
12073 We don't want to use $0 as tempreg. */
12074 if (ll_sc_paired)
12075 {
12076 if ((op[0] == ZERO && op[3] == op[1])
12077 || (op[1] == ZERO && op[3] == op[0])
12078 || (op[0] == ZERO && op[1] == ZERO))
12079 goto ld_st;
12080 else if (op[0] != op[3] && op[0] != ZERO)
12081 tempreg = op[0];
12082 else
12083 tempreg = op[1];
12084 }
252b5132 12085 else
a45328b9
AB
12086 {
12087 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12088 goto ld_st;
12089 else
12090 tempreg = op[0] + lp;
12091 }
df58fc94
RS
12092 goto ld_noat;
12093
252b5132
RH
12094 case M_SB_AB:
12095 s = "sb";
df58fc94
RS
12096 fmt = "t,o(b)";
12097 goto ld_st;
252b5132
RH
12098 case M_SH_AB:
12099 s = "sh";
df58fc94
RS
12100 fmt = "t,o(b)";
12101 goto ld_st;
252b5132
RH
12102 case M_SW_AB:
12103 s = "sw";
df58fc94
RS
12104 fmt = "t,o(b)";
12105 goto ld_st;
252b5132 12106 case M_SWC0_AB:
df58fc94 12107 gas_assert (!mips_opts.micromips);
252b5132 12108 s = "swc0";
df58fc94 12109 fmt = "E,o(b)";
bdaaa2e1 12110 /* Itbl support may require additional care here. */
252b5132 12111 coproc = 1;
df58fc94 12112 goto ld_st;
252b5132
RH
12113 case M_SWC1_AB:
12114 s = "swc1";
df58fc94 12115 fmt = "T,o(b)";
bdaaa2e1 12116 /* Itbl support may require additional care here. */
252b5132 12117 coproc = 1;
df58fc94 12118 goto ld_st;
252b5132
RH
12119 case M_SWC2_AB:
12120 s = "swc2";
df58fc94 12121 fmt = COP12_FMT;
7361da2c
AB
12122 offbits = (mips_opts.micromips ? 12
12123 : ISA_IS_R6 (mips_opts.isa) ? 11
12124 : 16);
bdaaa2e1 12125 /* Itbl support may require additional care here. */
252b5132 12126 coproc = 1;
df58fc94 12127 goto ld_st;
252b5132 12128 case M_SWC3_AB:
df58fc94 12129 gas_assert (!mips_opts.micromips);
252b5132 12130 s = "swc3";
df58fc94 12131 fmt = "E,o(b)";
bdaaa2e1 12132 /* Itbl support may require additional care here. */
252b5132 12133 coproc = 1;
df58fc94 12134 goto ld_st;
252b5132
RH
12135 case M_SWL_AB:
12136 s = "swl";
df58fc94 12137 fmt = MEM12_FMT;
7f3c4072 12138 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12139 goto ld_st;
252b5132
RH
12140 case M_SWR_AB:
12141 s = "swr";
df58fc94 12142 fmt = MEM12_FMT;
7f3c4072 12143 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12144 goto ld_st;
252b5132
RH
12145 case M_SC_AB:
12146 s = "sc";
7361da2c
AB
12147 fmt = LL_SC_FMT;
12148 offbits = (mips_opts.micromips ? 12
12149 : ISA_IS_R6 (mips_opts.isa) ? 9
12150 : 16);
df58fc94 12151 goto ld_st;
252b5132
RH
12152 case M_SCD_AB:
12153 s = "scd";
7361da2c
AB
12154 fmt = LL_SC_FMT;
12155 offbits = (mips_opts.micromips ? 12
12156 : ISA_IS_R6 (mips_opts.isa) ? 9
12157 : 16);
df58fc94 12158 goto ld_st;
a45328b9
AB
12159 case M_SCDP_AB:
12160 case M_SCWP_AB:
41cee089 12161 case M_SCWPE_AB:
a45328b9
AB
12162 s = ip->insn_mo->name;
12163 fmt = "t,d,s";
12164 ll_sc_paired = 1;
12165 offbits = 0;
12166 goto ld_st;
d43b4baf
TS
12167 case M_CACHE_AB:
12168 s = "cache";
7361da2c
AB
12169 fmt = (mips_opts.micromips ? "k,~(b)"
12170 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12171 : "k,o(b)");
12172 offbits = (mips_opts.micromips ? 12
12173 : ISA_IS_R6 (mips_opts.isa) ? 9
12174 : 16);
7f3c4072
CM
12175 goto ld_st;
12176 case M_CACHEE_AB:
7f3c4072
CM
12177 s = "cachee";
12178 fmt = "k,+j(b)";
12179 offbits = 9;
df58fc94 12180 goto ld_st;
3eebd5eb
MR
12181 case M_PREF_AB:
12182 s = "pref";
7361da2c
AB
12183 fmt = (mips_opts.micromips ? "k,~(b)"
12184 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12185 : "k,o(b)");
12186 offbits = (mips_opts.micromips ? 12
12187 : ISA_IS_R6 (mips_opts.isa) ? 9
12188 : 16);
7f3c4072
CM
12189 goto ld_st;
12190 case M_PREFE_AB:
7f3c4072
CM
12191 s = "prefe";
12192 fmt = "k,+j(b)";
12193 offbits = 9;
df58fc94 12194 goto ld_st;
252b5132 12195 case M_SDC1_AB:
252b5132 12196 s = "sdc1";
df58fc94 12197 fmt = "T,o(b)";
252b5132 12198 coproc = 1;
bdaaa2e1 12199 /* Itbl support may require additional care here. */
df58fc94 12200 goto ld_st;
252b5132
RH
12201 case M_SDC2_AB:
12202 s = "sdc2";
df58fc94 12203 fmt = COP12_FMT;
7361da2c
AB
12204 offbits = (mips_opts.micromips ? 12
12205 : ISA_IS_R6 (mips_opts.isa) ? 11
12206 : 16);
c77c0862
RS
12207 /* Itbl support may require additional care here. */
12208 coproc = 1;
12209 goto ld_st;
12210 case M_SQC2_AB:
c77c0862 12211 s = "sqc2";
14daeee3 12212 fmt = "+7,o(b)";
bdaaa2e1 12213 /* Itbl support may require additional care here. */
252b5132 12214 coproc = 1;
df58fc94 12215 goto ld_st;
252b5132 12216 case M_SDC3_AB:
df58fc94 12217 gas_assert (!mips_opts.micromips);
252b5132 12218 s = "sdc3";
df58fc94 12219 fmt = "E,o(b)";
bdaaa2e1 12220 /* Itbl support may require additional care here. */
252b5132 12221 coproc = 1;
df58fc94 12222 goto ld_st;
252b5132
RH
12223 case M_SDL_AB:
12224 s = "sdl";
df58fc94 12225 fmt = MEM12_FMT;
7f3c4072 12226 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12227 goto ld_st;
252b5132
RH
12228 case M_SDR_AB:
12229 s = "sdr";
df58fc94 12230 fmt = MEM12_FMT;
7f3c4072 12231 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12232 goto ld_st;
12233 case M_SWP_AB:
df58fc94
RS
12234 gas_assert (mips_opts.micromips);
12235 s = "swp";
12236 fmt = "t,~(b)";
7f3c4072 12237 offbits = 12;
df58fc94
RS
12238 goto ld_st;
12239 case M_SDP_AB:
df58fc94
RS
12240 gas_assert (mips_opts.micromips);
12241 s = "sdp";
12242 fmt = "t,~(b)";
7f3c4072 12243 offbits = 12;
df58fc94
RS
12244 goto ld_st;
12245 case M_SWM_AB:
df58fc94
RS
12246 gas_assert (mips_opts.micromips);
12247 s = "swm";
12248 fmt = "n,~(b)";
7f3c4072 12249 offbits = 12;
df58fc94
RS
12250 goto ld_st;
12251 case M_SDM_AB:
df58fc94
RS
12252 gas_assert (mips_opts.micromips);
12253 s = "sdm";
12254 fmt = "n,~(b)";
7f3c4072 12255 offbits = 12;
df58fc94
RS
12256
12257 ld_st:
8fc2e39e 12258 tempreg = AT;
df58fc94 12259 ld_noat:
a45328b9 12260 breg = ll_sc_paired ? op[3] : op[2];
f2ae14a1
RS
12261 if (small_offset_p (0, align, 16))
12262 {
12263 /* The first case exists for M_LD_AB and M_SD_AB, which are
12264 macros for o32 but which should act like normal instructions
12265 otherwise. */
12266 if (offbits == 16)
c0ebe874 12267 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12268 offset_reloc[1], offset_reloc[2], breg);
12269 else if (small_offset_p (0, align, offbits))
12270 {
12271 if (offbits == 0)
a45328b9
AB
12272 {
12273 if (ll_sc_paired)
12274 macro_build (NULL, s, fmt, op[0], op[1], breg);
12275 else
12276 macro_build (NULL, s, fmt, op[0], breg);
12277 }
f2ae14a1 12278 else
c0ebe874 12279 macro_build (NULL, s, fmt, op[0],
c8276761 12280 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12281 }
12282 else
12283 {
12284 if (tempreg == AT)
12285 used_at = 1;
12286 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12287 tempreg, breg, -1, offset_reloc[0],
12288 offset_reloc[1], offset_reloc[2]);
12289 if (offbits == 0)
a45328b9
AB
12290 {
12291 if (ll_sc_paired)
12292 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12293 else
12294 macro_build (NULL, s, fmt, op[0], tempreg);
12295 }
f2ae14a1 12296 else
c0ebe874 12297 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12298 }
12299 break;
12300 }
12301
12302 if (tempreg == AT)
12303 used_at = 1;
12304
252b5132
RH
12305 if (offset_expr.X_op != O_constant
12306 && offset_expr.X_op != O_symbol)
12307 {
1661c76c 12308 as_bad (_("expression too complex"));
252b5132
RH
12309 offset_expr.X_op = O_constant;
12310 }
12311
2051e8c4
MR
12312 if (HAVE_32BIT_ADDRESSES
12313 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71 12314 {
f493c217
AM
12315 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
12316 offset_expr.X_add_number);
55e08f71 12317 }
2051e8c4 12318
252b5132
RH
12319 /* A constant expression in PIC code can be handled just as it
12320 is in non PIC code. */
aed1a261
RS
12321 if (offset_expr.X_op == O_constant)
12322 {
f2ae14a1
RS
12323 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12324 offbits == 0 ? 16 : offbits);
12325 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12326
f2ae14a1
RS
12327 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12328 if (breg != 0)
12329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12330 tempreg, tempreg, breg);
7f3c4072 12331 if (offbits == 0)
dd6a37e7 12332 {
f2ae14a1 12333 if (offset_expr.X_add_number != 0)
dd6a37e7 12334 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12335 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
a45328b9
AB
12336 if (ll_sc_paired)
12337 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12338 else
12339 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12340 }
7f3c4072 12341 else if (offbits == 16)
c0ebe874 12342 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12343 else
c0ebe874 12344 macro_build (NULL, s, fmt, op[0],
c8276761 12345 (int) offset_expr.X_add_number, tempreg);
df58fc94 12346 }
7f3c4072 12347 else if (offbits != 16)
df58fc94 12348 {
7f3c4072 12349 /* The offset field is too narrow to be used for a low-part
2b0f3761 12350 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12351 register. */
12352 load_address (tempreg, &offset_expr, &used_at);
12353 if (breg != 0)
12354 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12355 tempreg, tempreg, breg);
7f3c4072 12356 if (offbits == 0)
a45328b9
AB
12357 {
12358 if (ll_sc_paired)
12359 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12360 else
12361 macro_build (NULL, s, fmt, op[0], tempreg);
12362 }
dd6a37e7 12363 else
c0ebe874 12364 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12365 }
12366 else if (mips_pic == NO_PIC)
252b5132
RH
12367 {
12368 /* If this is a reference to a GP relative symbol, and there
12369 is no base register, we want
c0ebe874 12370 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12371 Otherwise, if there is no base register, we want
12372 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12373 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12374 If we have a constant, we need two instructions anyhow,
12375 so we always use the latter form.
12376
12377 If we have a base register, and this is a reference to a
12378 GP relative symbol, we want
12379 addu $tempreg,$breg,$gp
c0ebe874 12380 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12381 Otherwise we want
12382 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12383 addu $tempreg,$tempreg,$breg
c0ebe874 12384 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12385 With a constant we always use the latter case.
76b3015f 12386
d6bc6245
TS
12387 With 64bit address space and no base register and $at usable,
12388 we want
12389 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12390 lui $at,<sym> (BFD_RELOC_HI16_S)
12391 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12392 dsll32 $tempreg,0
12393 daddu $tempreg,$at
c0ebe874 12394 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12395 If we have a base register, we want
12396 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12397 lui $at,<sym> (BFD_RELOC_HI16_S)
12398 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12399 daddu $at,$breg
12400 dsll32 $tempreg,0
12401 daddu $tempreg,$at
c0ebe874 12402 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12403
12404 Without $at we can't generate the optimal path for superscalar
12405 processors here since this would require two temporary registers.
12406 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12407 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12408 dsll $tempreg,16
12409 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12410 dsll $tempreg,16
c0ebe874 12411 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12412 If we have a base register, we want
12413 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12414 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12415 dsll $tempreg,16
12416 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12417 dsll $tempreg,16
12418 daddu $tempreg,$tempreg,$breg
c0ebe874 12419 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12420
6caf9ef4 12421 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12422 the same sequence as in 32bit address space. */
12423 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12424 {
aed1a261 12425 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12426 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12427 {
12428 relax_start (offset_expr.X_add_symbol);
12429 if (breg == 0)
12430 {
c0ebe874 12431 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12432 BFD_RELOC_GPREL16, mips_gp_register);
12433 }
12434 else
12435 {
12436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12437 tempreg, breg, mips_gp_register);
c0ebe874 12438 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12439 BFD_RELOC_GPREL16, tempreg);
12440 }
12441 relax_switch ();
12442 }
d6bc6245 12443
741fe287 12444 if (used_at == 0 && mips_opts.at)
d6bc6245 12445 {
df58fc94 12446 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12447 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12448 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12449 BFD_RELOC_HI16_S);
12450 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12451 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12452 if (breg != 0)
67c0d1eb 12453 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12454 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12455 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12456 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12457 tempreg);
d6bc6245
TS
12458 used_at = 1;
12459 }
12460 else
12461 {
df58fc94 12462 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12463 BFD_RELOC_MIPS_HIGHEST);
12464 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12465 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12466 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12467 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12468 tempreg, BFD_RELOC_HI16_S);
df58fc94 12469 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12470 if (breg != 0)
67c0d1eb 12471 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12472 tempreg, tempreg, breg);
c0ebe874 12473 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12474 BFD_RELOC_LO16, tempreg);
d6bc6245 12475 }
6caf9ef4
TS
12476
12477 if (mips_relax.sequence)
12478 relax_end ();
8fc2e39e 12479 break;
d6bc6245 12480 }
256ab948 12481
252b5132
RH
12482 if (breg == 0)
12483 {
67c0d1eb 12484 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12485 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12486 {
4d7206a2 12487 relax_start (offset_expr.X_add_symbol);
c0ebe874 12488 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12489 mips_gp_register);
4d7206a2 12490 relax_switch ();
252b5132 12491 }
67c0d1eb 12492 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12493 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12494 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12495 if (mips_relax.sequence)
12496 relax_end ();
252b5132
RH
12497 }
12498 else
12499 {
67c0d1eb 12500 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12501 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12502 {
4d7206a2 12503 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12504 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12505 tempreg, breg, mips_gp_register);
c0ebe874 12506 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12507 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12508 relax_switch ();
252b5132 12509 }
67c0d1eb
RS
12510 macro_build_lui (&offset_expr, tempreg);
12511 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12512 tempreg, tempreg, breg);
c0ebe874 12513 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12514 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12515 if (mips_relax.sequence)
12516 relax_end ();
252b5132
RH
12517 }
12518 }
0a44bf69 12519 else if (!mips_big_got)
252b5132 12520 {
ed6fb7bd 12521 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12522
252b5132
RH
12523 /* If this is a reference to an external symbol, we want
12524 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12525 nop
c0ebe874 12526 <op> op[0],0($tempreg)
252b5132
RH
12527 Otherwise we want
12528 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12529 nop
12530 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12531 <op> op[0],0($tempreg)
f5040a92
AO
12532
12533 For NewABI, we want
12534 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12535 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12536
252b5132
RH
12537 If there is a base register, we add it to $tempreg before
12538 the <op>. If there is a constant, we stick it in the
12539 <op> instruction. We don't handle constants larger than
12540 16 bits, because we have no way to load the upper 16 bits
12541 (actually, we could handle them for the subset of cases
12542 in which we are not using $at). */
9c2799c2 12543 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12544 if (HAVE_NEWABI)
12545 {
67c0d1eb
RS
12546 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12547 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12548 if (breg != 0)
67c0d1eb 12549 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12550 tempreg, tempreg, breg);
c0ebe874 12551 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12552 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12553 break;
12554 }
252b5132
RH
12555 expr1.X_add_number = offset_expr.X_add_number;
12556 offset_expr.X_add_number = 0;
12557 if (expr1.X_add_number < -0x8000
12558 || expr1.X_add_number >= 0x8000)
12559 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12560 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12561 lw_reloc_type, mips_gp_register);
269137b2 12562 load_delay_nop ();
4d7206a2
RS
12563 relax_start (offset_expr.X_add_symbol);
12564 relax_switch ();
67c0d1eb
RS
12565 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12566 tempreg, BFD_RELOC_LO16);
4d7206a2 12567 relax_end ();
252b5132 12568 if (breg != 0)
67c0d1eb 12569 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12570 tempreg, tempreg, breg);
c0ebe874 12571 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12572 }
0a44bf69 12573 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12574 {
67c0d1eb 12575 int gpdelay;
252b5132
RH
12576
12577 /* If this is a reference to an external symbol, we want
12578 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12579 addu $tempreg,$tempreg,$gp
12580 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12581 <op> op[0],0($tempreg)
252b5132
RH
12582 Otherwise we want
12583 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12584 nop
12585 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12586 <op> op[0],0($tempreg)
252b5132
RH
12587 If there is a base register, we add it to $tempreg before
12588 the <op>. If there is a constant, we stick it in the
12589 <op> instruction. We don't handle constants larger than
12590 16 bits, because we have no way to load the upper 16 bits
12591 (actually, we could handle them for the subset of cases
f5040a92 12592 in which we are not using $at). */
9c2799c2 12593 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12594 expr1.X_add_number = offset_expr.X_add_number;
12595 offset_expr.X_add_number = 0;
12596 if (expr1.X_add_number < -0x8000
12597 || expr1.X_add_number >= 0x8000)
12598 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12599 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12600 relax_start (offset_expr.X_add_symbol);
df58fc94 12601 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12602 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12603 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12604 mips_gp_register);
12605 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12606 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12607 relax_switch ();
67c0d1eb
RS
12608 if (gpdelay)
12609 macro_build (NULL, "nop", "");
12610 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12611 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12612 load_delay_nop ();
67c0d1eb
RS
12613 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12614 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12615 relax_end ();
12616
252b5132 12617 if (breg != 0)
67c0d1eb 12618 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12619 tempreg, tempreg, breg);
c0ebe874 12620 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12621 }
0a44bf69 12622 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12623 {
f5040a92
AO
12624 /* If this is a reference to an external symbol, we want
12625 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12626 add $tempreg,$tempreg,$gp
12627 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12628 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12629 Otherwise, for local symbols, we want:
12630 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12631 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12632 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12633 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12634 offset_expr.X_add_number = 0;
12635 if (expr1.X_add_number < -0x8000
12636 || expr1.X_add_number >= 0x8000)
12637 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12638 relax_start (offset_expr.X_add_symbol);
df58fc94 12639 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12640 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12641 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12642 mips_gp_register);
12643 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12644 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12645 if (breg != 0)
67c0d1eb 12646 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12647 tempreg, tempreg, breg);
c0ebe874 12648 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12649
4d7206a2 12650 relax_switch ();
f5040a92 12651 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12652 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12653 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12654 if (breg != 0)
67c0d1eb 12655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12656 tempreg, tempreg, breg);
c0ebe874 12657 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12658 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12659 relax_end ();
f5040a92 12660 }
252b5132
RH
12661 else
12662 abort ();
12663
252b5132
RH
12664 break;
12665
833794fc
MR
12666 case M_JRADDIUSP:
12667 gas_assert (mips_opts.micromips);
12668 gas_assert (mips_opts.insn32);
12669 start_noreorder ();
12670 macro_build (NULL, "jr", "s", RA);
c0ebe874 12671 expr1.X_add_number = op[0] << 2;
833794fc
MR
12672 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12673 end_noreorder ();
12674 break;
12675
12676 case M_JRC:
12677 gas_assert (mips_opts.micromips);
12678 gas_assert (mips_opts.insn32);
c0ebe874 12679 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12680 if (mips_opts.noreorder)
12681 macro_build (NULL, "nop", "");
12682 break;
12683
252b5132
RH
12684 case M_LI:
12685 case M_LI_S:
c0ebe874 12686 load_register (op[0], &imm_expr, 0);
8fc2e39e 12687 break;
252b5132
RH
12688
12689 case M_DLI:
c0ebe874 12690 load_register (op[0], &imm_expr, 1);
8fc2e39e 12691 break;
252b5132
RH
12692
12693 case M_LI_SS:
12694 if (imm_expr.X_op == O_constant)
12695 {
8fc2e39e 12696 used_at = 1;
67c0d1eb 12697 load_register (AT, &imm_expr, 0);
c0ebe874 12698 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12699 break;
12700 }
12701 else
12702 {
b0e6f033
RS
12703 gas_assert (imm_expr.X_op == O_absent
12704 && offset_expr.X_op == O_symbol
90ecf173
MR
12705 && strcmp (segment_name (S_GET_SEGMENT
12706 (offset_expr.X_add_symbol)),
12707 ".lit4") == 0
12708 && offset_expr.X_add_number == 0);
c0ebe874 12709 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12710 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12711 break;
252b5132
RH
12712 }
12713
12714 case M_LI_D:
ca4e0257
RS
12715 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12716 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12717 order 32 bits of the value and the low order 32 bits are either
12718 zero or in OFFSET_EXPR. */
b0e6f033 12719 if (imm_expr.X_op == O_constant)
252b5132 12720 {
bad1aba3 12721 if (GPR_SIZE == 64)
c0ebe874 12722 load_register (op[0], &imm_expr, 1);
252b5132
RH
12723 else
12724 {
12725 int hreg, lreg;
12726
12727 if (target_big_endian)
12728 {
c0ebe874
RS
12729 hreg = op[0];
12730 lreg = op[0] + 1;
252b5132
RH
12731 }
12732 else
12733 {
c0ebe874
RS
12734 hreg = op[0] + 1;
12735 lreg = op[0];
252b5132
RH
12736 }
12737
12738 if (hreg <= 31)
67c0d1eb 12739 load_register (hreg, &imm_expr, 0);
252b5132
RH
12740 if (lreg <= 31)
12741 {
12742 if (offset_expr.X_op == O_absent)
67c0d1eb 12743 move_register (lreg, 0);
252b5132
RH
12744 else
12745 {
9c2799c2 12746 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12747 load_register (lreg, &offset_expr, 0);
252b5132
RH
12748 }
12749 }
12750 }
8fc2e39e 12751 break;
252b5132 12752 }
b0e6f033 12753 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12754
12755 /* We know that sym is in the .rdata section. First we get the
12756 upper 16 bits of the address. */
12757 if (mips_pic == NO_PIC)
12758 {
67c0d1eb 12759 macro_build_lui (&offset_expr, AT);
8fc2e39e 12760 used_at = 1;
252b5132 12761 }
0a44bf69 12762 else
252b5132 12763 {
67c0d1eb
RS
12764 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12765 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12766 used_at = 1;
252b5132 12767 }
bdaaa2e1 12768
252b5132 12769 /* Now we load the register(s). */
bad1aba3 12770 if (GPR_SIZE == 64)
8fc2e39e
TS
12771 {
12772 used_at = 1;
c0ebe874
RS
12773 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12774 BFD_RELOC_LO16, AT);
8fc2e39e 12775 }
252b5132
RH
12776 else
12777 {
8fc2e39e 12778 used_at = 1;
c0ebe874
RS
12779 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12780 BFD_RELOC_LO16, AT);
12781 if (op[0] != RA)
252b5132
RH
12782 {
12783 /* FIXME: How in the world do we deal with the possible
12784 overflow here? */
12785 offset_expr.X_add_number += 4;
67c0d1eb 12786 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12787 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12788 }
12789 }
252b5132
RH
12790 break;
12791
12792 case M_LI_DD:
ca4e0257
RS
12793 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12794 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12795 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12796 the value and the low order 32 bits are either zero or in
12797 OFFSET_EXPR. */
b0e6f033 12798 if (imm_expr.X_op == O_constant)
252b5132 12799 {
9b444f95
FS
12800 tempreg = ZERO;
12801 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12802 || !ISA_HAS_MXHC1 (mips_opts.isa))
12803 && imm_expr.X_add_number != 0)
12804 {
12805 used_at = 1;
12806 tempreg = AT;
12807 load_register (AT, &imm_expr, FPR_SIZE == 64);
12808 }
351cdf24 12809 if (FPR_SIZE == 64 && GPR_SIZE == 64)
9b444f95 12810 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
252b5132
RH
12811 else
12812 {
9b444f95
FS
12813 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12814 {
12815 if (FPR_SIZE != 32)
12816 as_bad (_("Unable to generate `%s' compliant code "
12817 "without mthc1"),
12818 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12819 else
12820 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12821 }
252b5132 12822 if (offset_expr.X_op == O_absent)
c0ebe874 12823 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12824 else
12825 {
9c2799c2 12826 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12827 load_register (AT, &offset_expr, 0);
c0ebe874 12828 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132 12829 }
9b444f95
FS
12830 if (ISA_HAS_MXHC1 (mips_opts.isa))
12831 {
12832 if (imm_expr.X_add_number != 0)
12833 {
12834 used_at = 1;
12835 tempreg = AT;
12836 load_register (AT, &imm_expr, 0);
12837 }
12838 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12839 }
252b5132
RH
12840 }
12841 break;
12842 }
12843
b0e6f033
RS
12844 gas_assert (imm_expr.X_op == O_absent
12845 && offset_expr.X_op == O_symbol
90ecf173 12846 && offset_expr.X_add_number == 0);
252b5132
RH
12847 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12848 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12849 {
12850 op[2] = mips_gp_register;
f2ae14a1
RS
12851 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12852 offset_reloc[1] = BFD_RELOC_UNUSED;
12853 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12854 }
12855 else
12856 {
9c2799c2 12857 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12858 used_at = 1;
0a44bf69 12859 if (mips_pic != NO_PIC)
67c0d1eb
RS
12860 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12861 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12862 else
12863 {
12864 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12865 macro_build_lui (&offset_expr, AT);
252b5132 12866 }
bdaaa2e1 12867
c0ebe874 12868 op[2] = AT;
f2ae14a1
RS
12869 offset_reloc[0] = BFD_RELOC_LO16;
12870 offset_reloc[1] = BFD_RELOC_UNUSED;
12871 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12872 }
f2ae14a1 12873 align = 8;
6f2117ba 12874 /* Fall through. */
c4a68bea 12875
252b5132 12876 case M_L_DAB:
6f2117ba
PH
12877 /* The MIPS assembler seems to check for X_add_number not
12878 being double aligned and generating:
12879 lui at,%hi(foo+1)
12880 addu at,at,v1
12881 addiu at,at,%lo(foo+1)
12882 lwc1 f2,0(at)
12883 lwc1 f3,4(at)
12884 But, the resulting address is the same after relocation so why
12885 generate the extra instruction? */
bdaaa2e1 12886 /* Itbl support may require additional care here. */
252b5132 12887 coproc = 1;
df58fc94 12888 fmt = "T,o(b)";
0aa27725 12889 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12890 {
12891 s = "ldc1";
df58fc94 12892 goto ld_st;
252b5132 12893 }
252b5132 12894 s = "lwc1";
252b5132
RH
12895 goto ldd_std;
12896
12897 case M_S_DAB:
df58fc94
RS
12898 gas_assert (!mips_opts.micromips);
12899 /* Itbl support may require additional care here. */
12900 coproc = 1;
12901 fmt = "T,o(b)";
0aa27725 12902 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12903 {
12904 s = "sdc1";
df58fc94 12905 goto ld_st;
252b5132 12906 }
252b5132 12907 s = "swc1";
252b5132
RH
12908 goto ldd_std;
12909
e407c74b
NC
12910 case M_LQ_AB:
12911 fmt = "t,o(b)";
12912 s = "lq";
12913 goto ld;
12914
12915 case M_SQ_AB:
12916 fmt = "t,o(b)";
12917 s = "sq";
12918 goto ld_st;
12919
252b5132 12920 case M_LD_AB:
df58fc94 12921 fmt = "t,o(b)";
bad1aba3 12922 if (GPR_SIZE == 64)
252b5132
RH
12923 {
12924 s = "ld";
12925 goto ld;
12926 }
252b5132 12927 s = "lw";
252b5132
RH
12928 goto ldd_std;
12929
12930 case M_SD_AB:
df58fc94 12931 fmt = "t,o(b)";
bad1aba3 12932 if (GPR_SIZE == 64)
252b5132
RH
12933 {
12934 s = "sd";
df58fc94 12935 goto ld_st;
252b5132 12936 }
252b5132 12937 s = "sw";
252b5132
RH
12938
12939 ldd_std:
f2ae14a1
RS
12940 /* Even on a big endian machine $fn comes before $fn+1. We have
12941 to adjust when loading from memory. We set coproc if we must
12942 load $fn+1 first. */
12943 /* Itbl support may require additional care here. */
12944 if (!target_big_endian)
12945 coproc = 0;
12946
c0ebe874 12947 breg = op[2];
f2ae14a1
RS
12948 if (small_offset_p (0, align, 16))
12949 {
12950 ep = &offset_expr;
12951 if (!small_offset_p (4, align, 16))
12952 {
12953 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12954 -1, offset_reloc[0], offset_reloc[1],
12955 offset_reloc[2]);
12956 expr1.X_add_number = 0;
12957 ep = &expr1;
12958 breg = AT;
12959 used_at = 1;
12960 offset_reloc[0] = BFD_RELOC_LO16;
12961 offset_reloc[1] = BFD_RELOC_UNUSED;
12962 offset_reloc[2] = BFD_RELOC_UNUSED;
12963 }
c0ebe874 12964 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12965 {
12966 ep->X_add_number += 4;
c0ebe874 12967 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12968 offset_reloc[1], offset_reloc[2], breg);
12969 ep->X_add_number -= 4;
c0ebe874 12970 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12971 offset_reloc[1], offset_reloc[2], breg);
12972 }
12973 else
12974 {
c0ebe874 12975 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12976 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12977 breg);
12978 ep->X_add_number += 4;
c0ebe874 12979 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12980 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12981 breg);
12982 }
12983 break;
12984 }
12985
252b5132
RH
12986 if (offset_expr.X_op != O_symbol
12987 && offset_expr.X_op != O_constant)
12988 {
1661c76c 12989 as_bad (_("expression too complex"));
252b5132
RH
12990 offset_expr.X_op = O_constant;
12991 }
12992
2051e8c4
MR
12993 if (HAVE_32BIT_ADDRESSES
12994 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71 12995 {
f493c217
AM
12996 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
12997 offset_expr.X_add_number);
55e08f71 12998 }
2051e8c4 12999
90ecf173 13000 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
13001 {
13002 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
13003 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
13004 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
13005 If we have a base register, we use this
13006 addu $at,$breg,$gp
c0ebe874
RS
13007 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
13008 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
13009 If this is not a GP relative symbol, we want
13010 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
13011 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13012 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13013 If there is a base register, we add it to $at after the
13014 lui instruction. If there is a constant, we always use
13015 the last case. */
39a59cf8
MR
13016 if (offset_expr.X_op == O_symbol
13017 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 13018 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 13019 {
4d7206a2 13020 relax_start (offset_expr.X_add_symbol);
252b5132
RH
13021 if (breg == 0)
13022 {
c9914766 13023 tempreg = mips_gp_register;
252b5132
RH
13024 }
13025 else
13026 {
67c0d1eb 13027 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13028 AT, breg, mips_gp_register);
252b5132 13029 tempreg = AT;
252b5132
RH
13030 used_at = 1;
13031 }
13032
beae10d5 13033 /* Itbl support may require additional care here. */
c0ebe874 13034 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13035 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13036 offset_expr.X_add_number += 4;
13037
13038 /* Set mips_optimize to 2 to avoid inserting an
13039 undesired nop. */
13040 hold_mips_optimize = mips_optimize;
13041 mips_optimize = 2;
beae10d5 13042 /* Itbl support may require additional care here. */
c0ebe874 13043 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13044 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13045 mips_optimize = hold_mips_optimize;
13046
4d7206a2 13047 relax_switch ();
252b5132 13048
0970e49e 13049 offset_expr.X_add_number -= 4;
252b5132 13050 }
8fc2e39e 13051 used_at = 1;
f2ae14a1
RS
13052 if (offset_high_part (offset_expr.X_add_number, 16)
13053 != offset_high_part (offset_expr.X_add_number + 4, 16))
13054 {
13055 load_address (AT, &offset_expr, &used_at);
13056 offset_expr.X_op = O_constant;
13057 offset_expr.X_add_number = 0;
13058 }
13059 else
13060 macro_build_lui (&offset_expr, AT);
252b5132 13061 if (breg != 0)
67c0d1eb 13062 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13063 /* Itbl support may require additional care here. */
c0ebe874 13064 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13065 BFD_RELOC_LO16, AT);
252b5132
RH
13066 /* FIXME: How do we handle overflow here? */
13067 offset_expr.X_add_number += 4;
beae10d5 13068 /* Itbl support may require additional care here. */
c0ebe874 13069 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13070 BFD_RELOC_LO16, AT);
4d7206a2
RS
13071 if (mips_relax.sequence)
13072 relax_end ();
bdaaa2e1 13073 }
0a44bf69 13074 else if (!mips_big_got)
252b5132 13075 {
252b5132
RH
13076 /* If this is a reference to an external symbol, we want
13077 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13078 nop
c0ebe874
RS
13079 <op> op[0],0($at)
13080 <op> op[0]+1,4($at)
252b5132
RH
13081 Otherwise we want
13082 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13083 nop
c0ebe874
RS
13084 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13085 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13086 If there is a base register we add it to $at before the
13087 lwc1 instructions. If there is a constant we include it
13088 in the lwc1 instructions. */
13089 used_at = 1;
13090 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
13091 if (expr1.X_add_number < -0x8000
13092 || expr1.X_add_number >= 0x8000 - 4)
13093 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13094 load_got_offset (AT, &offset_expr);
269137b2 13095 load_delay_nop ();
252b5132 13096 if (breg != 0)
67c0d1eb 13097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
13098
13099 /* Set mips_optimize to 2 to avoid inserting an undesired
13100 nop. */
13101 hold_mips_optimize = mips_optimize;
13102 mips_optimize = 2;
4d7206a2 13103
beae10d5 13104 /* Itbl support may require additional care here. */
4d7206a2 13105 relax_start (offset_expr.X_add_symbol);
c0ebe874 13106 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13107 BFD_RELOC_LO16, AT);
4d7206a2 13108 expr1.X_add_number += 4;
c0ebe874 13109 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13110 BFD_RELOC_LO16, AT);
4d7206a2 13111 relax_switch ();
c0ebe874 13112 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13113 BFD_RELOC_LO16, AT);
4d7206a2 13114 offset_expr.X_add_number += 4;
c0ebe874 13115 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13116 BFD_RELOC_LO16, AT);
4d7206a2 13117 relax_end ();
252b5132 13118
4d7206a2 13119 mips_optimize = hold_mips_optimize;
252b5132 13120 }
0a44bf69 13121 else if (mips_big_got)
252b5132 13122 {
67c0d1eb 13123 int gpdelay;
252b5132
RH
13124
13125 /* If this is a reference to an external symbol, we want
13126 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13127 addu $at,$at,$gp
13128 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13129 nop
c0ebe874
RS
13130 <op> op[0],0($at)
13131 <op> op[0]+1,4($at)
252b5132
RH
13132 Otherwise we want
13133 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13134 nop
c0ebe874
RS
13135 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13136 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13137 If there is a base register we add it to $at before the
13138 lwc1 instructions. If there is a constant we include it
13139 in the lwc1 instructions. */
13140 used_at = 1;
13141 expr1.X_add_number = offset_expr.X_add_number;
13142 offset_expr.X_add_number = 0;
13143 if (expr1.X_add_number < -0x8000
13144 || expr1.X_add_number >= 0x8000 - 4)
13145 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13146 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13147 relax_start (offset_expr.X_add_symbol);
df58fc94 13148 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13149 AT, BFD_RELOC_MIPS_GOT_HI16);
13150 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13151 AT, AT, mips_gp_register);
67c0d1eb 13152 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13153 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13154 load_delay_nop ();
252b5132 13155 if (breg != 0)
67c0d1eb 13156 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13157 /* Itbl support may require additional care here. */
c0ebe874 13158 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13159 BFD_RELOC_LO16, AT);
252b5132
RH
13160 expr1.X_add_number += 4;
13161
13162 /* Set mips_optimize to 2 to avoid inserting an undesired
13163 nop. */
13164 hold_mips_optimize = mips_optimize;
13165 mips_optimize = 2;
beae10d5 13166 /* Itbl support may require additional care here. */
c0ebe874 13167 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13168 BFD_RELOC_LO16, AT);
252b5132
RH
13169 mips_optimize = hold_mips_optimize;
13170 expr1.X_add_number -= 4;
13171
4d7206a2
RS
13172 relax_switch ();
13173 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13174 if (gpdelay)
13175 macro_build (NULL, "nop", "");
13176 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13177 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13178 load_delay_nop ();
252b5132 13179 if (breg != 0)
67c0d1eb 13180 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13181 /* Itbl support may require additional care here. */
c0ebe874 13182 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13183 BFD_RELOC_LO16, AT);
4d7206a2 13184 offset_expr.X_add_number += 4;
252b5132
RH
13185
13186 /* Set mips_optimize to 2 to avoid inserting an undesired
13187 nop. */
13188 hold_mips_optimize = mips_optimize;
13189 mips_optimize = 2;
beae10d5 13190 /* Itbl support may require additional care here. */
c0ebe874 13191 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13192 BFD_RELOC_LO16, AT);
252b5132 13193 mips_optimize = hold_mips_optimize;
4d7206a2 13194 relax_end ();
252b5132 13195 }
252b5132
RH
13196 else
13197 abort ();
13198
252b5132 13199 break;
3739860c 13200
dd6a37e7 13201 case M_SAA_AB:
dd6a37e7 13202 s = "saa";
0db377d0 13203 goto saa_saad;
dd6a37e7 13204 case M_SAAD_AB:
dd6a37e7 13205 s = "saad";
0db377d0
MR
13206 saa_saad:
13207 gas_assert (!mips_opts.micromips);
7f3c4072 13208 offbits = 0;
dd6a37e7
AP
13209 fmt = "t,(b)";
13210 goto ld_st;
13211
252b5132
RH
13212 /* New code added to support COPZ instructions.
13213 This code builds table entries out of the macros in mip_opcodes.
13214 R4000 uses interlocks to handle coproc delays.
13215 Other chips (like the R3000) require nops to be inserted for delays.
13216
f72c8c98 13217 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13218 In order to fill delay slots for non-interlocked chips,
13219 we must have a way to specify delays based on the coprocessor.
13220 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13221 What are the side-effects of the cop instruction?
13222 What cache support might we have and what are its effects?
13223 Both coprocessor & memory require delays. how long???
bdaaa2e1 13224 What registers are read/set/modified?
252b5132
RH
13225
13226 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13227 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13228
13229 case M_COP0:
13230 s = "c0";
13231 goto copz;
13232 case M_COP1:
13233 s = "c1";
13234 goto copz;
13235 case M_COP2:
13236 s = "c2";
13237 goto copz;
13238 case M_COP3:
13239 s = "c3";
13240 copz:
df58fc94 13241 gas_assert (!mips_opts.micromips);
252b5132
RH
13242 /* For now we just do C (same as Cz). The parameter will be
13243 stored in insn_opcode by mips_ip. */
c8276761 13244 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13245 break;
252b5132 13246
ea1fb5dc 13247 case M_MOVE:
c0ebe874 13248 move_register (op[0], op[1]);
8fc2e39e 13249 break;
ea1fb5dc 13250
833794fc
MR
13251 case M_MOVEP:
13252 gas_assert (mips_opts.micromips);
13253 gas_assert (mips_opts.insn32);
c0ebe874
RS
13254 move_register (micromips_to_32_reg_h_map1[op[0]],
13255 micromips_to_32_reg_m_map[op[1]]);
13256 move_register (micromips_to_32_reg_h_map2[op[0]],
13257 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13258 break;
13259
252b5132
RH
13260 case M_DMUL:
13261 dbl = 1;
1a0670f3 13262 /* Fall through. */
252b5132 13263 case M_MUL:
e407c74b 13264 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13265 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13266 op[2]);
e407c74b
NC
13267 else
13268 {
c0ebe874
RS
13269 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13270 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13271 }
8fc2e39e 13272 break;
252b5132
RH
13273
13274 case M_DMUL_I:
13275 dbl = 1;
1a0670f3 13276 /* Fall through. */
252b5132
RH
13277 case M_MUL_I:
13278 /* The MIPS assembler some times generates shifts and adds. I'm
13279 not trying to be that fancy. GCC should do this for us
13280 anyway. */
8fc2e39e 13281 used_at = 1;
67c0d1eb 13282 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13283 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13284 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13285 break;
13286
13287 case M_DMULO_I:
13288 dbl = 1;
1a0670f3 13289 /* Fall through. */
252b5132
RH
13290 case M_MULO_I:
13291 imm = 1;
13292 goto do_mulo;
13293
13294 case M_DMULO:
13295 dbl = 1;
1a0670f3 13296 /* Fall through. */
252b5132
RH
13297 case M_MULO:
13298 do_mulo:
7d10b47d 13299 start_noreorder ();
8fc2e39e 13300 used_at = 1;
252b5132 13301 if (imm)
67c0d1eb 13302 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13303 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13304 op[1], imm ? AT : op[2]);
13305 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13306 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13307 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13308 if (mips_trap)
c0ebe874 13309 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13310 else
13311 {
df58fc94
RS
13312 if (mips_opts.micromips)
13313 micromips_label_expr (&label_expr);
13314 else
13315 label_expr.X_add_number = 8;
c0ebe874 13316 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13317 macro_build (NULL, "nop", "");
df58fc94
RS
13318 macro_build (NULL, "break", BRK_FMT, 6);
13319 if (mips_opts.micromips)
13320 micromips_add_label ();
252b5132 13321 }
7d10b47d 13322 end_noreorder ();
c0ebe874 13323 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13324 break;
13325
13326 case M_DMULOU_I:
13327 dbl = 1;
1a0670f3 13328 /* Fall through. */
252b5132
RH
13329 case M_MULOU_I:
13330 imm = 1;
13331 goto do_mulou;
13332
13333 case M_DMULOU:
13334 dbl = 1;
1a0670f3 13335 /* Fall through. */
252b5132
RH
13336 case M_MULOU:
13337 do_mulou:
7d10b47d 13338 start_noreorder ();
8fc2e39e 13339 used_at = 1;
252b5132 13340 if (imm)
67c0d1eb
RS
13341 load_register (AT, &imm_expr, dbl);
13342 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13343 op[1], imm ? AT : op[2]);
df58fc94 13344 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13345 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13346 if (mips_trap)
df58fc94 13347 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13348 else
13349 {
df58fc94
RS
13350 if (mips_opts.micromips)
13351 micromips_label_expr (&label_expr);
13352 else
13353 label_expr.X_add_number = 8;
13354 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13355 macro_build (NULL, "nop", "");
df58fc94
RS
13356 macro_build (NULL, "break", BRK_FMT, 6);
13357 if (mips_opts.micromips)
13358 micromips_add_label ();
252b5132 13359 }
7d10b47d 13360 end_noreorder ();
252b5132
RH
13361 break;
13362
771c7ce4 13363 case M_DROL:
fef14a42 13364 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13365 {
c0ebe874 13366 if (op[0] == op[1])
82dd0097
CD
13367 {
13368 tempreg = AT;
13369 used_at = 1;
13370 }
13371 else
c0ebe874
RS
13372 tempreg = op[0];
13373 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13374 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13375 break;
82dd0097 13376 }
8fc2e39e 13377 used_at = 1;
c0ebe874
RS
13378 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13379 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13380 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13381 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13382 break;
13383
252b5132 13384 case M_ROL:
fef14a42 13385 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13386 {
c0ebe874 13387 if (op[0] == op[1])
82dd0097
CD
13388 {
13389 tempreg = AT;
13390 used_at = 1;
13391 }
13392 else
c0ebe874
RS
13393 tempreg = op[0];
13394 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13395 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13396 break;
82dd0097 13397 }
8fc2e39e 13398 used_at = 1;
c0ebe874
RS
13399 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13400 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13401 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13402 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13403 break;
13404
771c7ce4
TS
13405 case M_DROL_I:
13406 {
13407 unsigned int rot;
e0471c16
TS
13408 const char *l;
13409 const char *rr;
771c7ce4 13410
771c7ce4 13411 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13412 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13413 {
13414 rot = (64 - rot) & 0x3f;
13415 if (rot >= 32)
c0ebe874 13416 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13417 else
c0ebe874 13418 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13419 break;
60b63b72 13420 }
483fc7cd 13421 if (rot == 0)
483fc7cd 13422 {
c0ebe874 13423 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13424 break;
483fc7cd 13425 }
82dd0097 13426 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13427 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13428 rot &= 0x1f;
8fc2e39e 13429 used_at = 1;
c0ebe874
RS
13430 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13431 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13432 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13433 }
13434 break;
13435
252b5132 13436 case M_ROL_I:
771c7ce4
TS
13437 {
13438 unsigned int rot;
13439
771c7ce4 13440 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13441 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13442 {
c0ebe874
RS
13443 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13444 (32 - rot) & 0x1f);
8fc2e39e 13445 break;
60b63b72 13446 }
483fc7cd 13447 if (rot == 0)
483fc7cd 13448 {
c0ebe874 13449 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13450 break;
483fc7cd 13451 }
8fc2e39e 13452 used_at = 1;
c0ebe874
RS
13453 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13454 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13455 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13456 }
13457 break;
13458
13459 case M_DROR:
fef14a42 13460 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13461 {
c0ebe874 13462 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13463 break;
82dd0097 13464 }
8fc2e39e 13465 used_at = 1;
c0ebe874
RS
13466 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13467 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13468 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13469 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13470 break;
13471
13472 case M_ROR:
fef14a42 13473 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13474 {
c0ebe874 13475 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13476 break;
82dd0097 13477 }
8fc2e39e 13478 used_at = 1;
c0ebe874
RS
13479 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13480 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13481 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13482 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13483 break;
13484
771c7ce4
TS
13485 case M_DROR_I:
13486 {
13487 unsigned int rot;
e0471c16
TS
13488 const char *l;
13489 const char *rr;
771c7ce4 13490
771c7ce4 13491 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13492 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13493 {
13494 if (rot >= 32)
c0ebe874 13495 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13496 else
c0ebe874 13497 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13498 break;
82dd0097 13499 }
483fc7cd 13500 if (rot == 0)
483fc7cd 13501 {
c0ebe874 13502 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13503 break;
483fc7cd 13504 }
91d6fa6a 13505 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13506 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13507 rot &= 0x1f;
8fc2e39e 13508 used_at = 1;
c0ebe874
RS
13509 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13510 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13511 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13512 }
13513 break;
13514
252b5132 13515 case M_ROR_I:
771c7ce4
TS
13516 {
13517 unsigned int rot;
13518
771c7ce4 13519 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13520 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13521 {
c0ebe874 13522 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13523 break;
82dd0097 13524 }
483fc7cd 13525 if (rot == 0)
483fc7cd 13526 {
c0ebe874 13527 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13528 break;
483fc7cd 13529 }
8fc2e39e 13530 used_at = 1;
c0ebe874
RS
13531 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13532 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13533 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13534 }
252b5132
RH
13535 break;
13536
252b5132 13537 case M_SEQ:
c0ebe874
RS
13538 if (op[1] == 0)
13539 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13540 else if (op[2] == 0)
13541 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13542 else
13543 {
c0ebe874
RS
13544 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13545 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13546 }
8fc2e39e 13547 break;
252b5132
RH
13548
13549 case M_SEQ_I:
b0e6f033 13550 if (imm_expr.X_add_number == 0)
252b5132 13551 {
c0ebe874 13552 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13553 break;
252b5132 13554 }
c0ebe874 13555 if (op[1] == 0)
252b5132 13556 {
1661c76c 13557 as_warn (_("instruction %s: result is always false"),
252b5132 13558 ip->insn_mo->name);
c0ebe874 13559 move_register (op[0], 0);
8fc2e39e 13560 break;
252b5132 13561 }
dd3cbb7e
NC
13562 if (CPU_HAS_SEQ (mips_opts.arch)
13563 && -512 <= imm_expr.X_add_number
13564 && imm_expr.X_add_number < 512)
13565 {
c0ebe874 13566 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13567 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13568 break;
13569 }
b0e6f033 13570 if (imm_expr.X_add_number >= 0
252b5132 13571 && imm_expr.X_add_number < 0x10000)
c0ebe874 13572 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13573 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13574 && imm_expr.X_add_number < 0)
13575 {
13576 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13577 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13578 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13579 }
dd3cbb7e
NC
13580 else if (CPU_HAS_SEQ (mips_opts.arch))
13581 {
13582 used_at = 1;
bad1aba3 13583 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13584 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13585 break;
13586 }
252b5132
RH
13587 else
13588 {
bad1aba3 13589 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13590 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13591 used_at = 1;
13592 }
c0ebe874 13593 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13594 break;
252b5132 13595
c0ebe874 13596 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13597 s = "slt";
13598 goto sge;
13599 case M_SGEU:
13600 s = "sltu";
13601 sge:
c0ebe874
RS
13602 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13603 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13604 break;
252b5132 13605
6f2117ba 13606 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13607 case M_SGEU_I:
b0e6f033 13608 if (imm_expr.X_add_number >= -0x8000
252b5132 13609 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13610 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13611 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13612 else
13613 {
bad1aba3 13614 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13615 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13616 op[0], op[1], AT);
252b5132
RH
13617 used_at = 1;
13618 }
c0ebe874 13619 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13620 break;
252b5132 13621
6f2117ba 13622 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13623 s = "slt";
13624 goto sgt;
13625 case M_SGTU:
13626 s = "sltu";
13627 sgt:
c0ebe874 13628 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13629 break;
252b5132 13630
6f2117ba 13631 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13632 s = "slt";
13633 goto sgti;
13634 case M_SGTU_I:
13635 s = "sltu";
13636 sgti:
8fc2e39e 13637 used_at = 1;
bad1aba3 13638 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13639 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13640 break;
13641
6f2117ba 13642 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13643 s = "slt";
13644 goto sle;
13645 case M_SLEU:
13646 s = "sltu";
13647 sle:
c0ebe874
RS
13648 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13649 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13650 break;
252b5132 13651
c0ebe874 13652 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13653 s = "slt";
13654 goto slei;
13655 case M_SLEU_I:
13656 s = "sltu";
13657 slei:
8fc2e39e 13658 used_at = 1;
bad1aba3 13659 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13660 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13661 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13662 break;
13663
13664 case M_SLT_I:
b0e6f033 13665 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13666 && imm_expr.X_add_number < 0x8000)
13667 {
c0ebe874
RS
13668 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13669 BFD_RELOC_LO16);
8fc2e39e 13670 break;
252b5132 13671 }
8fc2e39e 13672 used_at = 1;
bad1aba3 13673 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13674 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13675 break;
13676
13677 case M_SLTU_I:
b0e6f033 13678 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13679 && imm_expr.X_add_number < 0x8000)
13680 {
c0ebe874 13681 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13682 BFD_RELOC_LO16);
8fc2e39e 13683 break;
252b5132 13684 }
8fc2e39e 13685 used_at = 1;
bad1aba3 13686 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13687 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13688 break;
13689
13690 case M_SNE:
c0ebe874
RS
13691 if (op[1] == 0)
13692 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13693 else if (op[2] == 0)
13694 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13695 else
13696 {
c0ebe874
RS
13697 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13698 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13699 }
8fc2e39e 13700 break;
252b5132
RH
13701
13702 case M_SNE_I:
b0e6f033 13703 if (imm_expr.X_add_number == 0)
252b5132 13704 {
c0ebe874 13705 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13706 break;
252b5132 13707 }
c0ebe874 13708 if (op[1] == 0)
252b5132 13709 {
1661c76c 13710 as_warn (_("instruction %s: result is always true"),
252b5132 13711 ip->insn_mo->name);
bad1aba3 13712 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13713 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13714 break;
252b5132 13715 }
dd3cbb7e
NC
13716 if (CPU_HAS_SEQ (mips_opts.arch)
13717 && -512 <= imm_expr.X_add_number
13718 && imm_expr.X_add_number < 512)
13719 {
c0ebe874 13720 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13721 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13722 break;
13723 }
b0e6f033 13724 if (imm_expr.X_add_number >= 0
252b5132
RH
13725 && imm_expr.X_add_number < 0x10000)
13726 {
c0ebe874
RS
13727 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13728 BFD_RELOC_LO16);
252b5132 13729 }
b0e6f033 13730 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13731 && imm_expr.X_add_number < 0)
13732 {
13733 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13734 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13735 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13736 }
dd3cbb7e
NC
13737 else if (CPU_HAS_SEQ (mips_opts.arch))
13738 {
13739 used_at = 1;
bad1aba3 13740 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13741 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13742 break;
13743 }
252b5132
RH
13744 else
13745 {
bad1aba3 13746 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13747 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13748 used_at = 1;
13749 }
c0ebe874 13750 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13751 break;
252b5132 13752
df58fc94
RS
13753 case M_SUB_I:
13754 s = "addi";
13755 s2 = "sub";
387e7624
FS
13756 if (ISA_IS_R6 (mips_opts.isa))
13757 goto do_subi_i;
13758 else
13759 goto do_subi;
df58fc94
RS
13760 case M_SUBU_I:
13761 s = "addiu";
13762 s2 = "subu";
13763 goto do_subi;
252b5132
RH
13764 case M_DSUB_I:
13765 dbl = 1;
df58fc94
RS
13766 s = "daddi";
13767 s2 = "dsub";
387e7624 13768 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 13769 goto do_subi;
b0e6f033 13770 if (imm_expr.X_add_number > -0x200
387e7624
FS
13771 && imm_expr.X_add_number <= 0x200
13772 && !ISA_IS_R6 (mips_opts.isa))
252b5132 13773 {
b0e6f033
RS
13774 macro_build (NULL, s, "t,r,.", op[0], op[1],
13775 (int) -imm_expr.X_add_number);
8fc2e39e 13776 break;
252b5132 13777 }
df58fc94 13778 goto do_subi_i;
252b5132
RH
13779 case M_DSUBU_I:
13780 dbl = 1;
df58fc94
RS
13781 s = "daddiu";
13782 s2 = "dsubu";
13783 do_subi:
b0e6f033 13784 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13785 && imm_expr.X_add_number <= 0x8000)
13786 {
13787 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13788 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13789 break;
252b5132 13790 }
df58fc94 13791 do_subi_i:
8fc2e39e 13792 used_at = 1;
67c0d1eb 13793 load_register (AT, &imm_expr, dbl);
c0ebe874 13794 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13795 break;
13796
13797 case M_TEQ_I:
13798 s = "teq";
13799 goto trap;
13800 case M_TGE_I:
13801 s = "tge";
13802 goto trap;
13803 case M_TGEU_I:
13804 s = "tgeu";
13805 goto trap;
13806 case M_TLT_I:
13807 s = "tlt";
13808 goto trap;
13809 case M_TLTU_I:
13810 s = "tltu";
13811 goto trap;
13812 case M_TNE_I:
13813 s = "tne";
13814 trap:
8fc2e39e 13815 used_at = 1;
bad1aba3 13816 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13817 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13818 break;
13819
252b5132 13820 case M_TRUNCWS:
43841e91 13821 case M_TRUNCWD:
df58fc94 13822 gas_assert (!mips_opts.micromips);
0aa27725 13823 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13824 used_at = 1;
252b5132
RH
13825
13826 /*
13827 * Is the double cfc1 instruction a bug in the mips assembler;
13828 * or is there a reason for it?
13829 */
7d10b47d 13830 start_noreorder ();
9204ccd4
MR
13831 macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13832 macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
67c0d1eb 13833 macro_build (NULL, "nop", "");
252b5132 13834 expr1.X_add_number = 3;
c0ebe874 13835 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13836 expr1.X_add_number = 2;
67c0d1eb 13837 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9204ccd4 13838 macro_build (NULL, "ctc1", "t,g", AT, FCSR);
67c0d1eb
RS
13839 macro_build (NULL, "nop", "");
13840 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874 13841 op[0], op[1]);
9204ccd4 13842 macro_build (NULL, "ctc1", "t,g", op[2], FCSR);
67c0d1eb 13843 macro_build (NULL, "nop", "");
7d10b47d 13844 end_noreorder ();
252b5132
RH
13845 break;
13846
f2ae14a1 13847 case M_ULH_AB:
252b5132 13848 s = "lb";
df58fc94
RS
13849 s2 = "lbu";
13850 off = 1;
13851 goto uld_st;
f2ae14a1 13852 case M_ULHU_AB:
252b5132 13853 s = "lbu";
df58fc94
RS
13854 s2 = "lbu";
13855 off = 1;
13856 goto uld_st;
f2ae14a1 13857 case M_ULW_AB:
df58fc94
RS
13858 s = "lwl";
13859 s2 = "lwr";
7f3c4072 13860 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13861 off = 3;
13862 goto uld_st;
f2ae14a1 13863 case M_ULD_AB:
252b5132
RH
13864 s = "ldl";
13865 s2 = "ldr";
7f3c4072 13866 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13867 off = 7;
df58fc94 13868 goto uld_st;
f2ae14a1 13869 case M_USH_AB:
df58fc94
RS
13870 s = "sb";
13871 s2 = "sb";
13872 off = 1;
13873 ust = 1;
13874 goto uld_st;
f2ae14a1 13875 case M_USW_AB:
df58fc94
RS
13876 s = "swl";
13877 s2 = "swr";
7f3c4072 13878 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13879 off = 3;
df58fc94
RS
13880 ust = 1;
13881 goto uld_st;
f2ae14a1 13882 case M_USD_AB:
df58fc94
RS
13883 s = "sdl";
13884 s2 = "sdr";
7f3c4072 13885 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13886 off = 7;
13887 ust = 1;
13888
13889 uld_st:
c0ebe874 13890 breg = op[2];
f2ae14a1 13891 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13892 ep = &offset_expr;
13893 expr1.X_add_number = 0;
f2ae14a1 13894 if (large_offset)
df58fc94
RS
13895 {
13896 used_at = 1;
13897 tempreg = AT;
f2ae14a1
RS
13898 if (small_offset_p (0, align, 16))
13899 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13900 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13901 else
13902 {
13903 load_address (tempreg, ep, &used_at);
13904 if (breg != 0)
13905 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13906 tempreg, tempreg, breg);
13907 }
13908 offset_reloc[0] = BFD_RELOC_LO16;
13909 offset_reloc[1] = BFD_RELOC_UNUSED;
13910 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13911 breg = tempreg;
c0ebe874 13912 tempreg = op[0];
df58fc94
RS
13913 ep = &expr1;
13914 }
c0ebe874 13915 else if (!ust && op[0] == breg)
8fc2e39e
TS
13916 {
13917 used_at = 1;
13918 tempreg = AT;
13919 }
252b5132 13920 else
c0ebe874 13921 tempreg = op[0];
af22f5b2 13922
df58fc94
RS
13923 if (off == 1)
13924 goto ulh_sh;
252b5132 13925
90ecf173 13926 if (!target_big_endian)
df58fc94 13927 ep->X_add_number += off;
f2ae14a1 13928 if (offbits == 12)
c8276761 13929 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13930 else
13931 macro_build (ep, s, "t,o(b)", tempreg, -1,
13932 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13933
90ecf173 13934 if (!target_big_endian)
df58fc94 13935 ep->X_add_number -= off;
252b5132 13936 else
df58fc94 13937 ep->X_add_number += off;
f2ae14a1 13938 if (offbits == 12)
df58fc94 13939 macro_build (NULL, s2, "t,~(b)",
c8276761 13940 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13941 else
13942 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13943 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13944
df58fc94 13945 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13946 if (!ust && op[0] != tempreg)
df58fc94
RS
13947 {
13948 /* Protect second load's delay slot. */
13949 load_delay_nop ();
c0ebe874 13950 move_register (op[0], tempreg);
df58fc94 13951 }
8fc2e39e 13952 break;
252b5132 13953
df58fc94 13954 ulh_sh:
d6bc6245 13955 used_at = 1;
df58fc94
RS
13956 if (target_big_endian == ust)
13957 ep->X_add_number += off;
c0ebe874 13958 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13959 macro_build (ep, s, "t,o(b)", tempreg, -1,
13960 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13961
13962 /* For halfword transfers we need a temporary register to shuffle
13963 bytes. Unfortunately for M_USH_A we have none available before
13964 the next store as AT holds the base address. We deal with this
13965 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13966 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13967 if (ust)
c0ebe874 13968 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13969
13970 if (target_big_endian == ust)
13971 ep->X_add_number -= off;
252b5132 13972 else
df58fc94 13973 ep->X_add_number += off;
f2ae14a1
RS
13974 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13975 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13976
df58fc94 13977 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13978 if (ust && large_offset)
df58fc94
RS
13979 {
13980 if (target_big_endian)
13981 ep->X_add_number += off;
13982 else
13983 ep->X_add_number -= off;
f2ae14a1
RS
13984 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13985 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13986 }
13987 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13988 if (!ust || large_offset)
df58fc94 13989 {
c0ebe874 13990 tempreg = !large_offset ? AT : op[0];
df58fc94 13991 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13992 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13993 }
252b5132
RH
13994 break;
13995
13996 default:
13997 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13998 are added dynamically. */
1661c76c 13999 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
14000 break;
14001 }
741fe287 14002 if (!mips_opts.at && used_at)
1661c76c 14003 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
14004}
14005
14006/* Implement macros in mips16 mode. */
14007
14008static void
17a2f251 14009mips16_macro (struct mips_cl_insn *ip)
252b5132 14010{
c0ebe874 14011 const struct mips_operand_array *operands;
252b5132 14012 int mask;
c0ebe874 14013 int tmp;
252b5132
RH
14014 expressionS expr1;
14015 int dbl;
14016 const char *s, *s2, *s3;
c0ebe874
RS
14017 unsigned int op[MAX_OPERANDS];
14018 unsigned int i;
252b5132
RH
14019
14020 mask = ip->insn_mo->mask;
14021
c0ebe874
RS
14022 operands = insn_operands (ip);
14023 for (i = 0; i < MAX_OPERANDS; i++)
14024 if (operands->operand[i])
14025 op[i] = insn_extract_operand (ip, operands->operand[i]);
14026 else
14027 op[i] = -1;
252b5132 14028
252b5132
RH
14029 expr1.X_op = O_constant;
14030 expr1.X_op_symbol = NULL;
14031 expr1.X_add_symbol = NULL;
14032 expr1.X_add_number = 1;
14033
14034 dbl = 0;
14035
14036 switch (mask)
14037 {
14038 default:
b37df7c4 14039 abort ();
252b5132
RH
14040
14041 case M_DDIV_3:
14042 dbl = 1;
1a0670f3 14043 /* Fall through. */
252b5132
RH
14044 case M_DIV_3:
14045 s = "mflo";
14046 goto do_div3;
14047 case M_DREM_3:
14048 dbl = 1;
1a0670f3 14049 /* Fall through. */
252b5132
RH
14050 case M_REM_3:
14051 s = "mfhi";
14052 do_div3:
7d10b47d 14053 start_noreorder ();
d8722d76 14054 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 14055 expr1.X_add_number = 2;
c0ebe874 14056 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14057 macro_build (NULL, "break", "6", 7);
bdaaa2e1 14058
252b5132
RH
14059 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14060 since that causes an overflow. We should do that as well,
14061 but I don't see how to do the comparisons without a temporary
14062 register. */
7d10b47d 14063 end_noreorder ();
c0ebe874 14064 macro_build (NULL, s, "x", op[0]);
252b5132
RH
14065 break;
14066
14067 case M_DIVU_3:
14068 s = "divu";
14069 s2 = "mflo";
14070 goto do_divu3;
14071 case M_REMU_3:
14072 s = "divu";
14073 s2 = "mfhi";
14074 goto do_divu3;
14075 case M_DDIVU_3:
14076 s = "ddivu";
14077 s2 = "mflo";
14078 goto do_divu3;
14079 case M_DREMU_3:
14080 s = "ddivu";
14081 s2 = "mfhi";
14082 do_divu3:
7d10b47d 14083 start_noreorder ();
d8722d76 14084 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 14085 expr1.X_add_number = 2;
c0ebe874 14086 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14087 macro_build (NULL, "break", "6", 7);
7d10b47d 14088 end_noreorder ();
c0ebe874 14089 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
14090 break;
14091
14092 case M_DMUL:
14093 dbl = 1;
1a0670f3 14094 /* Fall through. */
252b5132 14095 case M_MUL:
c0ebe874
RS
14096 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14097 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 14098 break;
252b5132
RH
14099
14100 case M_DSUBU_I:
14101 dbl = 1;
14102 goto do_subu;
14103 case M_SUBU_I:
14104 do_subu:
252b5132 14105 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14106 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14107 break;
14108
14109 case M_SUBU_I_2:
252b5132 14110 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14111 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14112 break;
14113
14114 case M_DSUBU_I_2:
252b5132 14115 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14116 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14117 break;
14118
14119 case M_BEQ:
14120 s = "cmp";
14121 s2 = "bteqz";
14122 goto do_branch;
14123 case M_BNE:
14124 s = "cmp";
14125 s2 = "btnez";
14126 goto do_branch;
14127 case M_BLT:
14128 s = "slt";
14129 s2 = "btnez";
14130 goto do_branch;
14131 case M_BLTU:
14132 s = "sltu";
14133 s2 = "btnez";
14134 goto do_branch;
14135 case M_BLE:
14136 s = "slt";
14137 s2 = "bteqz";
14138 goto do_reverse_branch;
14139 case M_BLEU:
14140 s = "sltu";
14141 s2 = "bteqz";
14142 goto do_reverse_branch;
14143 case M_BGE:
14144 s = "slt";
14145 s2 = "bteqz";
14146 goto do_branch;
14147 case M_BGEU:
14148 s = "sltu";
14149 s2 = "bteqz";
14150 goto do_branch;
14151 case M_BGT:
14152 s = "slt";
14153 s2 = "btnez";
14154 goto do_reverse_branch;
14155 case M_BGTU:
14156 s = "sltu";
14157 s2 = "btnez";
14158
14159 do_reverse_branch:
c0ebe874
RS
14160 tmp = op[1];
14161 op[1] = op[0];
14162 op[0] = tmp;
252b5132
RH
14163
14164 do_branch:
c0ebe874 14165 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14166 macro_build (&offset_expr, s2, "p");
252b5132
RH
14167 break;
14168
14169 case M_BEQ_I:
14170 s = "cmpi";
14171 s2 = "bteqz";
14172 s3 = "x,U";
14173 goto do_branch_i;
14174 case M_BNE_I:
14175 s = "cmpi";
14176 s2 = "btnez";
14177 s3 = "x,U";
14178 goto do_branch_i;
14179 case M_BLT_I:
14180 s = "slti";
14181 s2 = "btnez";
14182 s3 = "x,8";
14183 goto do_branch_i;
14184 case M_BLTU_I:
14185 s = "sltiu";
14186 s2 = "btnez";
14187 s3 = "x,8";
14188 goto do_branch_i;
14189 case M_BLE_I:
14190 s = "slti";
14191 s2 = "btnez";
14192 s3 = "x,8";
14193 goto do_addone_branch_i;
14194 case M_BLEU_I:
14195 s = "sltiu";
14196 s2 = "btnez";
14197 s3 = "x,8";
14198 goto do_addone_branch_i;
14199 case M_BGE_I:
14200 s = "slti";
14201 s2 = "bteqz";
14202 s3 = "x,8";
14203 goto do_branch_i;
14204 case M_BGEU_I:
14205 s = "sltiu";
14206 s2 = "bteqz";
14207 s3 = "x,8";
14208 goto do_branch_i;
14209 case M_BGT_I:
14210 s = "slti";
14211 s2 = "bteqz";
14212 s3 = "x,8";
14213 goto do_addone_branch_i;
14214 case M_BGTU_I:
14215 s = "sltiu";
14216 s2 = "bteqz";
14217 s3 = "x,8";
14218
14219 do_addone_branch_i:
252b5132
RH
14220 ++imm_expr.X_add_number;
14221
14222 do_branch_i:
c0ebe874 14223 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14224 macro_build (&offset_expr, s2, "p");
252b5132
RH
14225 break;
14226
14227 case M_ABS:
14228 expr1.X_add_number = 0;
c0ebe874
RS
14229 macro_build (&expr1, "slti", "x,8", op[1]);
14230 if (op[0] != op[1])
14231 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14232 expr1.X_add_number = 2;
67c0d1eb 14233 macro_build (&expr1, "bteqz", "p");
c0ebe874 14234 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14235 break;
252b5132
RH
14236 }
14237}
14238
14daeee3
RS
14239/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14240 opcode bits in *OPCODE_EXTRA. */
14241
14242static struct mips_opcode *
629310ab 14243mips_lookup_insn (htab_t hash, const char *start,
da8bca91 14244 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14245{
14246 char *name, *dot, *p;
14247 unsigned int mask, suffix;
da8bca91 14248 ssize_t opend;
14daeee3
RS
14249 struct mips_opcode *insn;
14250
14251 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14252 name = xstrndup (start, length);
14daeee3
RS
14253
14254 /* Look up the instruction as-is. */
629310ab 14255 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14256 if (insn)
e1fa0163 14257 goto end;
14daeee3
RS
14258
14259 dot = strchr (name, '.');
14260 if (dot && dot[1])
14261 {
14262 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14263 p = mips_parse_vu0_channels (dot + 1, &mask);
14264 if (*p == 0 && mask != 0)
14265 {
14266 *dot = 0;
629310ab 14267 insn = (struct mips_opcode *) str_hash_find (hash, name);
14daeee3
RS
14268 *dot = '.';
14269 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14270 {
14271 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14272 goto end;
14daeee3
RS
14273 }
14274 }
14275 }
14276
14277 if (mips_opts.micromips)
14278 {
14279 /* See if there's an instruction size override suffix,
14280 either `16' or `32', at the end of the mnemonic proper,
14281 that defines the operation, i.e. before the first `.'
14282 character if any. Strip it and retry. */
14283 opend = dot != NULL ? dot - name : length;
14284 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14285 suffix = 2;
3076e594 14286 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14daeee3
RS
14287 suffix = 4;
14288 else
14289 suffix = 0;
14290 if (suffix)
14291 {
39334a61 14292 memmove (name + opend - 2, name + opend, length - opend + 1);
629310ab 14293 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14294 if (insn)
14daeee3
RS
14295 {
14296 forced_insn_length = suffix;
e1fa0163 14297 goto end;
14daeee3
RS
14298 }
14299 }
14300 }
14301
e1fa0163
NC
14302 insn = NULL;
14303 end:
14304 free (name);
14305 return insn;
14daeee3
RS
14306}
14307
77bd4346 14308/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14309 is a macro, set imm_expr and offset_expr to the values associated
14310 with "I" and "A" operands respectively. Otherwise store the value
14311 of the relocatable field (if any) in offset_expr. In both cases
14312 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14313
14314static void
60f20e8b 14315mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14316{
60f20e8b 14317 const struct mips_opcode *first, *past;
629310ab 14318 htab_t hash;
a92713e6 14319 char format;
14daeee3 14320 size_t end;
a92713e6 14321 struct mips_operand_token *tokens;
14daeee3 14322 unsigned int opcode_extra;
252b5132 14323
df58fc94
RS
14324 if (mips_opts.micromips)
14325 {
14326 hash = micromips_op_hash;
14327 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14328 }
14329 else
14330 {
14331 hash = op_hash;
14332 past = &mips_opcodes[NUMOPCODES];
14333 }
14334 forced_insn_length = 0;
14daeee3 14335 opcode_extra = 0;
252b5132 14336
df58fc94 14337 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14338 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14339 continue;
bdaaa2e1 14340
60f20e8b
RS
14341 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14342 if (first == NULL)
252b5132 14343 {
1661c76c 14344 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14345 return;
252b5132
RH
14346 }
14347
60f20e8b 14348 if (strcmp (first->name, "li.s") == 0)
a92713e6 14349 format = 'f';
60f20e8b 14350 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14351 format = 'd';
14352 else
14353 format = 0;
14354 tokens = mips_parse_arguments (str + end, format);
14355 if (!tokens)
14356 return;
14357
5b7c81bd
AM
14358 if (!match_insns (insn, first, past, tokens, opcode_extra, false)
14359 && !match_insns (insn, first, past, tokens, opcode_extra, true))
1661c76c 14360 set_insn_error (0, _("invalid operands"));
df58fc94 14361
e3de51ce 14362 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14363}
14364
77bd4346
RS
14365/* As for mips_ip, but used when assembling MIPS16 code.
14366 Also set forced_insn_length to the resulting instruction size in
14367 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14368
14369static void
60f20e8b 14370mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14371{
1a00e612 14372 char *end, *s, c;
60f20e8b 14373 struct mips_opcode *first;
a92713e6 14374 struct mips_operand_token *tokens;
3fb49709 14375 unsigned int l;
252b5132 14376
25499ac7 14377 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14378 ;
1a00e612
RS
14379 end = s;
14380 c = *end;
3fb49709
MR
14381
14382 l = 0;
1a00e612 14383 switch (c)
252b5132
RH
14384 {
14385 case '\0':
14386 break;
14387
14388 case ' ':
1a00e612 14389 s++;
252b5132
RH
14390 break;
14391
14392 case '.':
3fb49709
MR
14393 s++;
14394 if (*s == 't')
252b5132 14395 {
3fb49709
MR
14396 l = 2;
14397 s++;
252b5132 14398 }
3fb49709 14399 else if (*s == 'e')
252b5132 14400 {
3fb49709
MR
14401 l = 4;
14402 s++;
252b5132 14403 }
3fb49709
MR
14404 if (*s == '\0')
14405 break;
14406 else if (*s++ == ' ')
14407 break;
1661c76c 14408 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14409 return;
14410 }
3fb49709 14411 forced_insn_length = l;
252b5132 14412
1a00e612 14413 *end = 0;
629310ab 14414 first = (struct mips_opcode *) str_hash_find (mips16_op_hash, str);
1a00e612
RS
14415 *end = c;
14416
60f20e8b 14417 if (!first)
252b5132 14418 {
1661c76c 14419 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14420 return;
14421 }
14422
a92713e6
RS
14423 tokens = mips_parse_arguments (s, 0);
14424 if (!tokens)
14425 return;
14426
60f20e8b 14427 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14428 set_insn_error (0, _("invalid operands"));
252b5132 14429
e3de51ce 14430 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14431}
14432
b886a2ab
RS
14433/* Marshal immediate value VAL for an extended MIPS16 instruction.
14434 NBITS is the number of significant bits in VAL. */
14435
14436static unsigned long
14437mips16_immed_extend (offsetT val, unsigned int nbits)
14438{
14439 int extval;
25499ac7
MR
14440
14441 extval = 0;
14442 val &= (1U << nbits) - 1;
14443 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14444 {
14445 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14446 val &= 0x1f;
14447 }
14448 else if (nbits == 15)
14449 {
14450 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14451 val &= 0xf;
14452 }
25499ac7 14453 else if (nbits == 6)
b886a2ab
RS
14454 {
14455 extval = ((val & 0x1f) << 6) | (val & 0x20);
14456 val = 0;
14457 }
14458 return (extval << 16) | val;
14459}
14460
3ccad066
RS
14461/* Like decode_mips16_operand, but require the operand to be defined and
14462 require it to be an integer. */
14463
14464static const struct mips_int_operand *
5b7c81bd 14465mips16_immed_operand (int type, bool extended_p)
3ccad066
RS
14466{
14467 const struct mips_operand *operand;
14468
14469 operand = decode_mips16_operand (type, extended_p);
14470 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14471 abort ();
14472 return (const struct mips_int_operand *) operand;
14473}
14474
14475/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14476
5b7c81bd 14477static bool
3ccad066
RS
14478mips16_immed_in_range_p (const struct mips_int_operand *operand,
14479 bfd_reloc_code_real_type reloc, offsetT sval)
14480{
14481 int min_val, max_val;
14482
14483 min_val = mips_int_operand_min (operand);
14484 max_val = mips_int_operand_max (operand);
14485 if (reloc != BFD_RELOC_UNUSED)
14486 {
14487 if (min_val < 0)
14488 sval = SEXT_16BIT (sval);
14489 else
14490 sval &= 0xffff;
14491 }
14492
14493 return (sval >= min_val
14494 && sval <= max_val
14495 && (sval & ((1 << operand->shift) - 1)) == 0);
14496}
14497
5c04167a
RS
14498/* Install immediate value VAL into MIPS16 instruction *INSN,
14499 extending it if necessary. The instruction in *INSN may
14500 already be extended.
14501
43c0598f
RS
14502 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14503 if none. In the former case, VAL is a 16-bit number with no
14504 defined signedness.
14505
14506 TYPE is the type of the immediate field. USER_INSN_LENGTH
14507 is the length that the user requested, or 0 if none. */
252b5132
RH
14508
14509static void
3b4dbbbf 14510mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14511 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14512 unsigned int user_insn_length, unsigned long *insn)
252b5132 14513{
3ccad066
RS
14514 const struct mips_int_operand *operand;
14515 unsigned int uval, length;
252b5132 14516
5b7c81bd 14517 operand = mips16_immed_operand (type, false);
3ccad066 14518 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14519 {
14520 /* We need an extended instruction. */
14521 if (user_insn_length == 2)
14522 as_bad_where (file, line, _("invalid unextended operand value"));
14523 else
14524 *insn |= MIPS16_EXTEND;
14525 }
14526 else if (user_insn_length == 4)
14527 {
14528 /* The operand doesn't force an unextended instruction to be extended.
14529 Warn if the user wanted an extended instruction anyway. */
14530 *insn |= MIPS16_EXTEND;
14531 as_warn_where (file, line,
14532 _("extended operand requested but not required"));
14533 }
252b5132 14534
3ccad066
RS
14535 length = mips16_opcode_length (*insn);
14536 if (length == 4)
252b5132 14537 {
5b7c81bd 14538 operand = mips16_immed_operand (type, true);
3ccad066
RS
14539 if (!mips16_immed_in_range_p (operand, reloc, val))
14540 as_bad_where (file, line,
14541 _("operand value out of range for instruction"));
252b5132 14542 }
3ccad066 14543 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14544 if (length == 2 || operand->root.lsb != 0)
3ccad066 14545 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14546 else
3ccad066 14547 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14548}
14549\f
d6f16593 14550struct percent_op_match
ad8d3bb3 14551{
5e0116d5
RS
14552 const char *str;
14553 bfd_reloc_code_real_type reloc;
d6f16593
MR
14554};
14555
14556static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14557{
5e0116d5 14558 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14559 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14560 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14561 {"%call16", BFD_RELOC_MIPS_CALL16},
14562 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14563 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14564 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14565 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14566 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14567 {"%got", BFD_RELOC_MIPS_GOT16},
14568 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14569 {"%gprel", BFD_RELOC_GPREL16},
d712f276 14570 {"%half", BFD_RELOC_MIPS_16},
5e0116d5
RS
14571 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14572 {"%higher", BFD_RELOC_MIPS_HIGHER},
14573 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14574 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14575 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14576 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14577 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14578 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14579 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14580 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14581 {"%hi", BFD_RELOC_HI16_S},
14582 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14583 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14584};
14585
d6f16593
MR
14586static const struct percent_op_match mips16_percent_op[] =
14587{
14588 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14589 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14590 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14591 {"%got", BFD_RELOC_MIPS16_GOT16},
14592 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14593 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14594 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14595 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14596 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14597 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14598 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14599 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14600 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14601};
14602
252b5132 14603
5e0116d5
RS
14604/* Return true if *STR points to a relocation operator. When returning true,
14605 move *STR over the operator and store its relocation code in *RELOC.
14606 Leave both *STR and *RELOC alone when returning false. */
14607
5b7c81bd 14608static bool
17a2f251 14609parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14610{
d6f16593
MR
14611 const struct percent_op_match *percent_op;
14612 size_t limit, i;
14613
14614 if (mips_opts.mips16)
14615 {
14616 percent_op = mips16_percent_op;
14617 limit = ARRAY_SIZE (mips16_percent_op);
14618 }
14619 else
14620 {
14621 percent_op = mips_percent_op;
14622 limit = ARRAY_SIZE (mips_percent_op);
14623 }
76b3015f 14624
d6f16593 14625 for (i = 0; i < limit; i++)
5e0116d5 14626 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14627 {
3f98094e
DJ
14628 int len = strlen (percent_op[i].str);
14629
14630 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14631 continue;
14632
5e0116d5
RS
14633 *str += strlen (percent_op[i].str);
14634 *reloc = percent_op[i].reloc;
394f9b3a 14635
5e0116d5
RS
14636 /* Check whether the output BFD supports this relocation.
14637 If not, issue an error and fall back on something safe. */
14638 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14639 {
20203fb9 14640 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14641 percent_op[i].str);
01a3f561 14642 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14643 }
5b7c81bd 14644 return true;
394f9b3a 14645 }
5b7c81bd 14646 return false;
394f9b3a 14647}
ad8d3bb3 14648
ad8d3bb3 14649
5e0116d5
RS
14650/* Parse string STR as a 16-bit relocatable operand. Store the
14651 expression in *EP and the relocations in the array starting
14652 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14653
6eb099ae
AM
14654 On exit, EXPR_PARSE_END points to the first character after the
14655 expression. */
ad8d3bb3 14656
5e0116d5 14657static size_t
17a2f251
TS
14658my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14659 char *str)
ad8d3bb3 14660{
5e0116d5
RS
14661 bfd_reloc_code_real_type reversed_reloc[3];
14662 size_t reloc_index, i;
09b8f35a
RS
14663 int crux_depth, str_depth;
14664 char *crux;
5e0116d5
RS
14665
14666 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14667 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14668 of the main expression and with CRUX_DEPTH containing the number
14669 of open brackets at that point. */
14670 reloc_index = -1;
14671 str_depth = 0;
14672 do
fb1b3232 14673 {
09b8f35a
RS
14674 reloc_index++;
14675 crux = str;
14676 crux_depth = str_depth;
14677
14678 /* Skip over whitespace and brackets, keeping count of the number
14679 of brackets. */
14680 while (*str == ' ' || *str == '\t' || *str == '(')
14681 if (*str++ == '(')
14682 str_depth++;
5e0116d5 14683 }
09b8f35a
RS
14684 while (*str == '%'
14685 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14686 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14687
09b8f35a 14688 my_getExpression (ep, crux);
6eb099ae 14689 str = expr_parse_end;
394f9b3a 14690
5e0116d5 14691 /* Match every open bracket. */
09b8f35a 14692 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14693 if (*str++ == ')')
09b8f35a 14694 crux_depth--;
394f9b3a 14695
09b8f35a 14696 if (crux_depth > 0)
20203fb9 14697 as_bad (_("unclosed '('"));
394f9b3a 14698
6eb099ae 14699 expr_parse_end = str;
252b5132 14700
ec4fcab0
MR
14701 for (i = 0; i < reloc_index; i++)
14702 reloc[i] = reversed_reloc[reloc_index - 1 - i];
fb1b3232 14703
5e0116d5 14704 return reloc_index;
252b5132
RH
14705}
14706
14707static void
17a2f251 14708my_getExpression (expressionS *ep, char *str)
252b5132
RH
14709{
14710 char *save_in;
14711
14712 save_in = input_line_pointer;
14713 input_line_pointer = str;
14714 expression (ep);
6eb099ae 14715 expr_parse_end = input_line_pointer;
252b5132 14716 input_line_pointer = save_in;
252b5132
RH
14717}
14718
6d4af3c2 14719const char *
17a2f251 14720md_atof (int type, char *litP, int *sizeP)
252b5132 14721{
499ac353 14722 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14723}
14724
14725void
17a2f251 14726md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14727{
14728 if (target_big_endian)
14729 number_to_chars_bigendian (buf, val, n);
14730 else
14731 number_to_chars_littleendian (buf, val, n);
14732}
14733\f
e013f690
TS
14734static int support_64bit_objects(void)
14735{
14736 const char **list, **l;
aa3d8fdf 14737 int yes;
e013f690
TS
14738
14739 list = bfd_target_list ();
14740 for (l = list; *l != NULL; l++)
aeffff67
RS
14741 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14742 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14743 break;
aa3d8fdf 14744 yes = (*l != NULL);
e013f690 14745 free (list);
aa3d8fdf 14746 return yes;
e013f690
TS
14747}
14748
316f5878
RS
14749/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14750 NEW_VALUE. Warn if another value was already specified. Note:
14751 we have to defer parsing the -march and -mtune arguments in order
14752 to handle 'from-abi' correctly, since the ABI might be specified
14753 in a later argument. */
14754
14755static void
17a2f251 14756mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14757{
14758 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14759 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14760 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14761 new_value);
14762
14763 *string_ptr = new_value;
14764}
14765
252b5132 14766int
17b9d67d 14767md_parse_option (int c, const char *arg)
252b5132 14768{
c6278170
RS
14769 unsigned int i;
14770
14771 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14772 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14773 {
919731af 14774 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14775 c == mips_ases[i].option_on);
14776 return 1;
14777 }
14778
252b5132
RH
14779 switch (c)
14780 {
119d663a
NC
14781 case OPTION_CONSTRUCT_FLOATS:
14782 mips_disable_float_construction = 0;
14783 break;
bdaaa2e1 14784
119d663a
NC
14785 case OPTION_NO_CONSTRUCT_FLOATS:
14786 mips_disable_float_construction = 1;
14787 break;
bdaaa2e1 14788
252b5132
RH
14789 case OPTION_TRAP:
14790 mips_trap = 1;
14791 break;
14792
14793 case OPTION_BREAK:
14794 mips_trap = 0;
14795 break;
14796
14797 case OPTION_EB:
14798 target_big_endian = 1;
14799 break;
14800
14801 case OPTION_EL:
14802 target_big_endian = 0;
14803 break;
14804
14805 case 'O':
4ffff32f
TS
14806 if (arg == NULL)
14807 mips_optimize = 1;
14808 else if (arg[0] == '0')
14809 mips_optimize = 0;
14810 else if (arg[0] == '1')
252b5132
RH
14811 mips_optimize = 1;
14812 else
14813 mips_optimize = 2;
14814 break;
14815
14816 case 'g':
14817 if (arg == NULL)
14818 mips_debug = 2;
14819 else
14820 mips_debug = atoi (arg);
252b5132
RH
14821 break;
14822
14823 case OPTION_MIPS1:
0b35dfee 14824 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14825 break;
14826
14827 case OPTION_MIPS2:
0b35dfee 14828 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14829 break;
14830
14831 case OPTION_MIPS3:
0b35dfee 14832 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14833 break;
14834
14835 case OPTION_MIPS4:
0b35dfee 14836 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14837 break;
14838
84ea6cf2 14839 case OPTION_MIPS5:
0b35dfee 14840 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14841 break;
14842
e7af610e 14843 case OPTION_MIPS32:
0b35dfee 14844 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14845 break;
14846
af7ee8bf 14847 case OPTION_MIPS32R2:
0b35dfee 14848 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14849 break;
14850
ae52f483 14851 case OPTION_MIPS32R3:
0ae19f05 14852 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14853 break;
14854
14855 case OPTION_MIPS32R5:
0ae19f05 14856 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14857 break;
14858
7361da2c
AB
14859 case OPTION_MIPS32R6:
14860 file_mips_opts.isa = ISA_MIPS32R6;
14861 break;
14862
5f74bc13 14863 case OPTION_MIPS64R2:
0b35dfee 14864 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14865 break;
14866
ae52f483 14867 case OPTION_MIPS64R3:
0ae19f05 14868 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14869 break;
14870
14871 case OPTION_MIPS64R5:
0ae19f05 14872 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14873 break;
14874
7361da2c
AB
14875 case OPTION_MIPS64R6:
14876 file_mips_opts.isa = ISA_MIPS64R6;
14877 break;
14878
84ea6cf2 14879 case OPTION_MIPS64:
0b35dfee 14880 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14881 break;
14882
ec68c924 14883 case OPTION_MTUNE:
316f5878
RS
14884 mips_set_option_string (&mips_tune_string, arg);
14885 break;
ec68c924 14886
316f5878
RS
14887 case OPTION_MARCH:
14888 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14889 break;
14890
14891 case OPTION_M4650:
316f5878
RS
14892 mips_set_option_string (&mips_arch_string, "4650");
14893 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14894 break;
14895
14896 case OPTION_NO_M4650:
14897 break;
14898
14899 case OPTION_M4010:
316f5878
RS
14900 mips_set_option_string (&mips_arch_string, "4010");
14901 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14902 break;
14903
14904 case OPTION_NO_M4010:
14905 break;
14906
14907 case OPTION_M4100:
316f5878
RS
14908 mips_set_option_string (&mips_arch_string, "4100");
14909 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14910 break;
14911
14912 case OPTION_NO_M4100:
14913 break;
14914
252b5132 14915 case OPTION_M3900:
316f5878
RS
14916 mips_set_option_string (&mips_arch_string, "3900");
14917 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14918 break;
bdaaa2e1 14919
252b5132
RH
14920 case OPTION_NO_M3900:
14921 break;
14922
df58fc94 14923 case OPTION_MICROMIPS:
919731af 14924 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14925 {
14926 as_bad (_("-mmicromips cannot be used with -mips16"));
14927 return 0;
14928 }
919731af 14929 file_mips_opts.micromips = 1;
df58fc94
RS
14930 mips_no_prev_insn ();
14931 break;
14932
14933 case OPTION_NO_MICROMIPS:
919731af 14934 file_mips_opts.micromips = 0;
df58fc94
RS
14935 mips_no_prev_insn ();
14936 break;
14937
252b5132 14938 case OPTION_MIPS16:
919731af 14939 if (file_mips_opts.micromips == 1)
df58fc94
RS
14940 {
14941 as_bad (_("-mips16 cannot be used with -micromips"));
14942 return 0;
14943 }
919731af 14944 file_mips_opts.mips16 = 1;
7d10b47d 14945 mips_no_prev_insn ();
252b5132
RH
14946 break;
14947
14948 case OPTION_NO_MIPS16:
919731af 14949 file_mips_opts.mips16 = 0;
7d10b47d 14950 mips_no_prev_insn ();
252b5132
RH
14951 break;
14952
6a32d874
CM
14953 case OPTION_FIX_24K:
14954 mips_fix_24k = 1;
14955 break;
14956
14957 case OPTION_NO_FIX_24K:
14958 mips_fix_24k = 0;
14959 break;
14960
a8d14a88
CM
14961 case OPTION_FIX_RM7000:
14962 mips_fix_rm7000 = 1;
14963 break;
14964
14965 case OPTION_NO_FIX_RM7000:
14966 mips_fix_rm7000 = 0;
14967 break;
14968
6f2117ba 14969 case OPTION_FIX_LOONGSON3_LLSC:
5b7c81bd 14970 mips_fix_loongson3_llsc = true;
6f2117ba
PH
14971 break;
14972
14973 case OPTION_NO_FIX_LOONGSON3_LLSC:
5b7c81bd 14974 mips_fix_loongson3_llsc = false;
6f2117ba
PH
14975 break;
14976
c67a084a 14977 case OPTION_FIX_LOONGSON2F_JUMP:
5b7c81bd 14978 mips_fix_loongson2f_jump = true;
c67a084a
NC
14979 break;
14980
14981 case OPTION_NO_FIX_LOONGSON2F_JUMP:
5b7c81bd 14982 mips_fix_loongson2f_jump = false;
c67a084a
NC
14983 break;
14984
14985 case OPTION_FIX_LOONGSON2F_NOP:
5b7c81bd 14986 mips_fix_loongson2f_nop = true;
c67a084a
NC
14987 break;
14988
14989 case OPTION_NO_FIX_LOONGSON2F_NOP:
5b7c81bd 14990 mips_fix_loongson2f_nop = false;
c67a084a
NC
14991 break;
14992
d766e8ec
RS
14993 case OPTION_FIX_VR4120:
14994 mips_fix_vr4120 = 1;
60b63b72
RS
14995 break;
14996
d766e8ec
RS
14997 case OPTION_NO_FIX_VR4120:
14998 mips_fix_vr4120 = 0;
60b63b72
RS
14999 break;
15000
7d8e00cf
RS
15001 case OPTION_FIX_VR4130:
15002 mips_fix_vr4130 = 1;
15003 break;
15004
15005 case OPTION_NO_FIX_VR4130:
15006 mips_fix_vr4130 = 0;
15007 break;
15008
d954098f 15009 case OPTION_FIX_CN63XXP1:
5b7c81bd 15010 mips_fix_cn63xxp1 = true;
d954098f
DD
15011 break;
15012
15013 case OPTION_NO_FIX_CN63XXP1:
5b7c81bd 15014 mips_fix_cn63xxp1 = false;
d954098f
DD
15015 break;
15016
27c634e0 15017 case OPTION_FIX_R5900:
5b7c81bd
AM
15018 mips_fix_r5900 = true;
15019 mips_fix_r5900_explicit = true;
27c634e0
FN
15020 break;
15021
15022 case OPTION_NO_FIX_R5900:
5b7c81bd
AM
15023 mips_fix_r5900 = false;
15024 mips_fix_r5900_explicit = true;
27c634e0
FN
15025 break;
15026
4a6a3df4
AO
15027 case OPTION_RELAX_BRANCH:
15028 mips_relax_branch = 1;
15029 break;
15030
15031 case OPTION_NO_RELAX_BRANCH:
15032 mips_relax_branch = 0;
15033 break;
15034
8b10b0b3 15035 case OPTION_IGNORE_BRANCH_ISA:
5b7c81bd 15036 mips_ignore_branch_isa = true;
8b10b0b3
MR
15037 break;
15038
15039 case OPTION_NO_IGNORE_BRANCH_ISA:
5b7c81bd 15040 mips_ignore_branch_isa = false;
8b10b0b3
MR
15041 break;
15042
833794fc 15043 case OPTION_INSN32:
5b7c81bd 15044 file_mips_opts.insn32 = true;
833794fc
MR
15045 break;
15046
15047 case OPTION_NO_INSN32:
5b7c81bd 15048 file_mips_opts.insn32 = false;
833794fc
MR
15049 break;
15050
aa6975fb 15051 case OPTION_MSHARED:
5b7c81bd 15052 mips_in_shared = true;
aa6975fb
ILT
15053 break;
15054
15055 case OPTION_MNO_SHARED:
5b7c81bd 15056 mips_in_shared = false;
aa6975fb
ILT
15057 break;
15058
aed1a261 15059 case OPTION_MSYM32:
5b7c81bd 15060 file_mips_opts.sym32 = true;
aed1a261
RS
15061 break;
15062
15063 case OPTION_MNO_SYM32:
5b7c81bd 15064 file_mips_opts.sym32 = false;
aed1a261
RS
15065 break;
15066
252b5132
RH
15067 /* When generating ELF code, we permit -KPIC and -call_shared to
15068 select SVR4_PIC, and -non_shared to select no PIC. This is
15069 intended to be compatible with Irix 5. */
15070 case OPTION_CALL_SHARED:
252b5132 15071 mips_pic = SVR4_PIC;
5b7c81bd 15072 mips_abicalls = true;
252b5132
RH
15073 break;
15074
861fb55a 15075 case OPTION_CALL_NONPIC:
861fb55a 15076 mips_pic = NO_PIC;
5b7c81bd 15077 mips_abicalls = true;
861fb55a
DJ
15078 break;
15079
252b5132 15080 case OPTION_NON_SHARED:
252b5132 15081 mips_pic = NO_PIC;
5b7c81bd 15082 mips_abicalls = false;
252b5132
RH
15083 break;
15084
44075ae2
TS
15085 /* The -xgot option tells the assembler to use 32 bit offsets
15086 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15087 compatibility. */
15088 case OPTION_XGOT:
15089 mips_big_got = 1;
15090 break;
15091
15092 case 'G':
6caf9ef4
TS
15093 g_switch_value = atoi (arg);
15094 g_switch_seen = 1;
252b5132
RH
15095 break;
15096
34ba82a8
TS
15097 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15098 and -mabi=64. */
252b5132 15099 case OPTION_32:
f3ded42a 15100 mips_abi = O32_ABI;
252b5132
RH
15101 break;
15102
e013f690 15103 case OPTION_N32:
316f5878 15104 mips_abi = N32_ABI;
e013f690 15105 break;
252b5132 15106
e013f690 15107 case OPTION_64:
316f5878 15108 mips_abi = N64_ABI;
f43abd2b 15109 if (!support_64bit_objects())
1661c76c 15110 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15111 break;
15112
c97ef257 15113 case OPTION_GP32:
bad1aba3 15114 file_mips_opts.gp = 32;
c97ef257
AH
15115 break;
15116
15117 case OPTION_GP64:
bad1aba3 15118 file_mips_opts.gp = 64;
c97ef257 15119 break;
252b5132 15120
ca4e0257 15121 case OPTION_FP32:
0b35dfee 15122 file_mips_opts.fp = 32;
316f5878
RS
15123 break;
15124
351cdf24
MF
15125 case OPTION_FPXX:
15126 file_mips_opts.fp = 0;
15127 break;
15128
316f5878 15129 case OPTION_FP64:
0b35dfee 15130 file_mips_opts.fp = 64;
ca4e0257
RS
15131 break;
15132
351cdf24
MF
15133 case OPTION_ODD_SPREG:
15134 file_mips_opts.oddspreg = 1;
15135 break;
15136
15137 case OPTION_NO_ODD_SPREG:
15138 file_mips_opts.oddspreg = 0;
15139 break;
15140
037b32b9 15141 case OPTION_SINGLE_FLOAT:
0b35dfee 15142 file_mips_opts.single_float = 1;
037b32b9
AN
15143 break;
15144
15145 case OPTION_DOUBLE_FLOAT:
0b35dfee 15146 file_mips_opts.single_float = 0;
037b32b9
AN
15147 break;
15148
15149 case OPTION_SOFT_FLOAT:
0b35dfee 15150 file_mips_opts.soft_float = 1;
037b32b9
AN
15151 break;
15152
15153 case OPTION_HARD_FLOAT:
0b35dfee 15154 file_mips_opts.soft_float = 0;
037b32b9
AN
15155 break;
15156
252b5132 15157 case OPTION_MABI:
e013f690 15158 if (strcmp (arg, "32") == 0)
316f5878 15159 mips_abi = O32_ABI;
e013f690 15160 else if (strcmp (arg, "o64") == 0)
316f5878 15161 mips_abi = O64_ABI;
e013f690 15162 else if (strcmp (arg, "n32") == 0)
316f5878 15163 mips_abi = N32_ABI;
e013f690
TS
15164 else if (strcmp (arg, "64") == 0)
15165 {
316f5878 15166 mips_abi = N64_ABI;
e013f690 15167 if (! support_64bit_objects())
1661c76c 15168 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15169 "format"));
15170 }
15171 else if (strcmp (arg, "eabi") == 0)
316f5878 15172 mips_abi = EABI_ABI;
e013f690 15173 else
da0e507f
TS
15174 {
15175 as_fatal (_("invalid abi -mabi=%s"), arg);
15176 return 0;
15177 }
252b5132
RH
15178 break;
15179
6b76fefe 15180 case OPTION_M7000_HILO_FIX:
5b7c81bd 15181 mips_7000_hilo_fix = true;
6b76fefe
CM
15182 break;
15183
9ee72ff1 15184 case OPTION_MNO_7000_HILO_FIX:
5b7c81bd 15185 mips_7000_hilo_fix = false;
6b76fefe
CM
15186 break;
15187
ecb4347a 15188 case OPTION_MDEBUG:
5b7c81bd 15189 mips_flag_mdebug = true;
ecb4347a
DJ
15190 break;
15191
15192 case OPTION_NO_MDEBUG:
5b7c81bd 15193 mips_flag_mdebug = false;
ecb4347a 15194 break;
dcd410fe
RO
15195
15196 case OPTION_PDR:
5b7c81bd 15197 mips_flag_pdr = true;
dcd410fe
RO
15198 break;
15199
15200 case OPTION_NO_PDR:
5b7c81bd 15201 mips_flag_pdr = false;
dcd410fe 15202 break;
0a44bf69
RS
15203
15204 case OPTION_MVXWORKS_PIC:
15205 mips_pic = VXWORKS_PIC;
15206 break;
ecb4347a 15207
ba92f887
MR
15208 case OPTION_NAN:
15209 if (strcmp (arg, "2008") == 0)
7361da2c 15210 mips_nan2008 = 1;
ba92f887 15211 else if (strcmp (arg, "legacy") == 0)
7361da2c 15212 mips_nan2008 = 0;
ba92f887
MR
15213 else
15214 {
1661c76c 15215 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15216 return 0;
15217 }
15218 break;
15219
252b5132
RH
15220 default:
15221 return 0;
15222 }
15223
c67a084a
NC
15224 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15225
252b5132
RH
15226 return 1;
15227}
316f5878 15228\f
919731af 15229/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15230
316f5878 15231static void
17a2f251 15232mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15233{
15234 if (info != 0)
fef14a42 15235 mips_tune = info->cpu;
316f5878 15236}
80cc45a5 15237
34ba82a8 15238
252b5132 15239void
17a2f251 15240mips_after_parse_args (void)
e9670677 15241{
fef14a42
TS
15242 const struct mips_cpu_info *arch_info = 0;
15243 const struct mips_cpu_info *tune_info = 0;
15244
6f2117ba 15245 /* GP relative stuff not working for PE. */
d34049e8 15246 if (startswith (TARGET_OS, "pe"))
e9670677 15247 {
6caf9ef4 15248 if (g_switch_seen && g_switch_value != 0)
1661c76c 15249 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15250 g_switch_value = 0;
15251 }
15252
cac012d6
AO
15253 if (mips_abi == NO_ABI)
15254 mips_abi = MIPS_DEFAULT_ABI;
15255
919731af 15256 /* The following code determines the architecture.
22923709
RS
15257 Similar code was added to GCC 3.3 (see override_options() in
15258 config/mips/mips.c). The GAS and GCC code should be kept in sync
15259 as much as possible. */
e9670677 15260
316f5878 15261 if (mips_arch_string != 0)
fef14a42 15262 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15263
0b35dfee 15264 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15265 {
0b35dfee 15266 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15267 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15268 the -march selection (if any). */
fef14a42 15269 if (arch_info != 0)
e9670677 15270 {
316f5878
RS
15271 /* -march takes precedence over -mipsN, since it is more descriptive.
15272 There's no harm in specifying both as long as the ISA levels
15273 are the same. */
0b35dfee 15274 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15275 as_bad (_("-%s conflicts with the other architecture options,"
15276 " which imply -%s"),
0b35dfee 15277 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15278 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15279 }
316f5878 15280 else
0b35dfee 15281 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15282 }
15283
fef14a42 15284 if (arch_info == 0)
95bfe26e
MF
15285 {
15286 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15287 gas_assert (arch_info);
15288 }
e9670677 15289
fef14a42 15290 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15291 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15292 arch_info->name);
15293
919731af 15294 file_mips_opts.arch = arch_info->cpu;
15295 file_mips_opts.isa = arch_info->isa;
3315614d 15296 file_mips_opts.init_ase = arch_info->ase;
919731af 15297
41cee089
FS
15298 /* The EVA Extension has instructions which are only valid when the R6 ISA
15299 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15300 present. */
15301 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15302 file_mips_opts.ase |= ASE_EVA_R6;
15303
919731af 15304 /* Set up initial mips_opts state. */
15305 mips_opts = file_mips_opts;
15306
27c634e0
FN
15307 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15308 if (!mips_fix_r5900_explicit)
15309 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15310
919731af 15311 /* The register size inference code is now placed in
15312 file_mips_check_options. */
fef14a42 15313
0b35dfee 15314 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15315 processor. */
fef14a42
TS
15316 if (mips_tune_string != 0)
15317 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15318
fef14a42
TS
15319 if (tune_info == 0)
15320 mips_set_tune (arch_info);
15321 else
15322 mips_set_tune (tune_info);
e9670677 15323
ecb4347a 15324 if (mips_flag_mdebug < 0)
e8044f35 15325 mips_flag_mdebug = 0;
e9670677
MR
15326}
15327\f
15328void
17a2f251 15329mips_init_after_args (void)
252b5132 15330{
6f2117ba 15331 /* Initialize opcodes. */
252b5132 15332 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15333 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15334}
15335
15336long
17a2f251 15337md_pcrel_from (fixS *fixP)
252b5132 15338{
a7ebbfdf 15339 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15340
a7ebbfdf
TS
15341 switch (fixP->fx_r_type)
15342 {
df58fc94
RS
15343 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15344 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15345 /* Return the address of the delay slot. */
15346 return addr + 2;
15347
15348 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15349 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15350 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15351 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15352 case BFD_RELOC_MIPS_21_PCREL_S2:
15353 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15354 case BFD_RELOC_MIPS_JMP:
15355 /* Return the address of the delay slot. */
15356 return addr + 4;
df58fc94 15357
51f6035b
MR
15358 case BFD_RELOC_MIPS_18_PCREL_S3:
15359 /* Return the aligned address of the doubleword containing
15360 the instruction. */
15361 return addr & ~7;
15362
a7ebbfdf
TS
15363 default:
15364 return addr;
15365 }
252b5132
RH
15366}
15367
252b5132
RH
15368/* This is called before the symbol table is processed. In order to
15369 work with gcc when using mips-tfile, we must keep all local labels.
15370 However, in other cases, we want to discard them. If we were
15371 called with -g, but we didn't see any debugging information, it may
15372 mean that gcc is smuggling debugging information through to
15373 mips-tfile, in which case we must generate all local labels. */
15374
15375void
17a2f251 15376mips_frob_file_before_adjust (void)
252b5132
RH
15377{
15378#ifndef NO_ECOFF_DEBUGGING
15379 if (ECOFF_DEBUGGING
15380 && mips_debug != 0
15381 && ! ecoff_debugging_seen)
15382 flag_keep_locals = 1;
15383#endif
15384}
15385
3b91255e 15386/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15387 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15388 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15389 relocation operators.
15390
15391 For our purposes, a %lo() expression matches a %got() or %hi()
15392 expression if:
15393
15394 (a) it refers to the same symbol; and
15395 (b) the offset applied in the %lo() expression is no lower than
15396 the offset applied in the %got() or %hi().
15397
15398 (b) allows us to cope with code like:
15399
15400 lui $4,%hi(foo)
15401 lh $4,%lo(foo+2)($4)
15402
15403 ...which is legal on RELA targets, and has a well-defined behaviour
15404 if the user knows that adding 2 to "foo" will not induce a carry to
15405 the high 16 bits.
15406
15407 When several %lo()s match a particular %got() or %hi(), we use the
15408 following rules to distinguish them:
15409
15410 (1) %lo()s with smaller offsets are a better match than %lo()s with
15411 higher offsets.
15412
15413 (2) %lo()s with no matching %got() or %hi() are better than those
15414 that already have a matching %got() or %hi().
15415
15416 (3) later %lo()s are better than earlier %lo()s.
15417
15418 These rules are applied in order.
15419
15420 (1) means, among other things, that %lo()s with identical offsets are
15421 chosen if they exist.
15422
15423 (2) means that we won't associate several high-part relocations with
15424 the same low-part relocation unless there's no alternative. Having
15425 several high parts for the same low part is a GNU extension; this rule
15426 allows careful users to avoid it.
15427
15428 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15429 with the last high-part relocation being at the front of the list.
15430 It therefore makes sense to choose the last matching low-part
15431 relocation, all other things being equal. It's also easier
15432 to code that way. */
252b5132
RH
15433
15434void
17a2f251 15435mips_frob_file (void)
252b5132
RH
15436{
15437 struct mips_hi_fixup *l;
35903be0 15438 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15439
15440 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15441 {
15442 segment_info_type *seginfo;
5b7c81bd 15443 bool matched_lo_p;
3b91255e 15444 fixS **hi_pos, **lo_pos, **pos;
252b5132 15445
9c2799c2 15446 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15447
5919d012 15448 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15449 there isn't supposed to be a matching LO. Ignore %gots against
15450 constants; we'll report an error for those later. */
738e5348 15451 if (got16_reloc_p (l->fixp->fx_r_type)
249d1bad 15452 && !pic_need_relax (l->fixp->fx_addsy))
5919d012
RS
15453 continue;
15454
15455 /* Check quickly whether the next fixup happens to be a matching %lo. */
15456 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15457 continue;
15458
252b5132 15459 seginfo = seg_info (l->seg);
252b5132 15460
3b91255e
RS
15461 /* Set HI_POS to the position of this relocation in the chain.
15462 Set LO_POS to the position of the chosen low-part relocation.
15463 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15464 relocation that matches an immediately-preceding high-part
15465 relocation. */
15466 hi_pos = NULL;
15467 lo_pos = NULL;
5b7c81bd 15468 matched_lo_p = false;
738e5348 15469 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15470
3b91255e
RS
15471 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15472 {
15473 if (*pos == l->fixp)
15474 hi_pos = pos;
15475
35903be0 15476 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15477 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15478 && (*pos)->fx_offset >= l->fixp->fx_offset
15479 && (lo_pos == NULL
15480 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15481 || (!matched_lo_p
15482 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15483 lo_pos = pos;
15484
15485 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15486 && fixup_has_matching_lo_p (*pos));
15487 }
15488
15489 /* If we found a match, remove the high-part relocation from its
15490 current position and insert it before the low-part relocation.
15491 Make the offsets match so that fixup_has_matching_lo_p()
15492 will return true.
15493
15494 We don't warn about unmatched high-part relocations since some
15495 versions of gcc have been known to emit dead "lui ...%hi(...)"
15496 instructions. */
15497 if (lo_pos != NULL)
15498 {
15499 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15500 if (l->fixp->fx_next != *lo_pos)
252b5132 15501 {
3b91255e
RS
15502 *hi_pos = l->fixp->fx_next;
15503 l->fixp->fx_next = *lo_pos;
15504 *lo_pos = l->fixp;
252b5132 15505 }
252b5132
RH
15506 }
15507 }
15508}
15509
252b5132 15510int
17a2f251 15511mips_force_relocation (fixS *fixp)
252b5132 15512{
ae6063d4 15513 if (generic_force_reloc (fixp))
252b5132
RH
15514 return 1;
15515
df58fc94
RS
15516 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15517 so that the linker relaxation can update targets. */
15518 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15519 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15520 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15521 return 1;
15522
5caa2b07
MR
15523 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15524 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15525 microMIPS symbols so that we can do cross-mode branch diagnostics
15526 and BAL to JALX conversion by the linker. */
15527 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15528 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15529 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15530 && fixp->fx_addsy
15531 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15532 return 1;
15533
7361da2c 15534 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15535 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15536 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15537 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15538 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15539 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15540 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15541 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15542 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15543 return 1;
15544
3e722fb5 15545 return 0;
252b5132
RH
15546}
15547
b416ba9b
MR
15548/* Implement TC_FORCE_RELOCATION_ABS. */
15549
5b7c81bd 15550bool
b416ba9b
MR
15551mips_force_relocation_abs (fixS *fixp)
15552{
15553 if (generic_force_reloc (fixp))
5b7c81bd 15554 return true;
b416ba9b
MR
15555
15556 /* These relocations do not have enough bits in the in-place addend
15557 to hold an arbitrary absolute section's offset. */
15558 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
5b7c81bd 15559 return true;
b416ba9b 15560
5b7c81bd 15561 return false;
b416ba9b
MR
15562}
15563
b886a2ab
RS
15564/* Read the instruction associated with RELOC from BUF. */
15565
15566static unsigned int
15567read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15568{
15569 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15570 return read_compressed_insn (buf, 4);
15571 else
15572 return read_insn (buf);
15573}
15574
15575/* Write instruction INSN to BUF, given that it has been relocated
15576 by RELOC. */
15577
15578static void
15579write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15580 unsigned long insn)
15581{
15582 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15583 write_compressed_insn (buf, insn, 4);
15584 else
15585 write_insn (buf, insn);
15586}
15587
9d862524
MR
15588/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15589 to a symbol in another ISA mode, which cannot be converted to JALX. */
15590
5b7c81bd 15591static bool
9d862524
MR
15592fix_bad_cross_mode_jump_p (fixS *fixP)
15593{
15594 unsigned long opcode;
15595 int other;
15596 char *buf;
15597
5b7c81bd
AM
15598 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15599 return false;
9d862524
MR
15600
15601 other = S_GET_OTHER (fixP->fx_addsy);
15602 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15603 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15604 switch (fixP->fx_r_type)
15605 {
15606 case BFD_RELOC_MIPS_JMP:
15607 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15608 case BFD_RELOC_MICROMIPS_JMP:
15609 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15610 default:
5b7c81bd 15611 return false;
9d862524
MR
15612 }
15613}
15614
15615/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15616 jump to a symbol in the same ISA mode. */
15617
5b7c81bd 15618static bool
9d862524
MR
15619fix_bad_same_mode_jalx_p (fixS *fixP)
15620{
15621 unsigned long opcode;
15622 int other;
15623 char *buf;
15624
5b7c81bd
AM
15625 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15626 return false;
9d862524
MR
15627
15628 other = S_GET_OTHER (fixP->fx_addsy);
15629 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15630 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15631 switch (fixP->fx_r_type)
15632 {
15633 case BFD_RELOC_MIPS_JMP:
15634 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15635 case BFD_RELOC_MIPS16_JMP:
15636 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15637 case BFD_RELOC_MICROMIPS_JMP:
15638 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15639 default:
5b7c81bd 15640 return false;
9d862524
MR
15641 }
15642}
15643
15644/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15645 to a symbol whose value plus addend is not aligned according to the
15646 ultimate (after linker relaxation) jump instruction's immediate field
15647 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15648 regular MIPS code, to (1 << 2). */
15649
5b7c81bd 15650static bool
9d862524
MR
15651fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15652{
5b7c81bd 15653 bool micro_to_mips_p;
9d862524
MR
15654 valueT val;
15655 int other;
15656
5b7c81bd
AM
15657 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15658 return false;
9d862524
MR
15659
15660 other = S_GET_OTHER (fixP->fx_addsy);
15661 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15662 val += fixP->fx_offset;
15663 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15664 && !ELF_ST_IS_MICROMIPS (other));
15665 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15666 != ELF_ST_IS_COMPRESSED (other));
15667}
15668
15669/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15670 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15671 symbols check the ISA bit instead.
15672
15673 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15674 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15675 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15676 may be converted to JALX by the linker. */
9d862524 15677
5b7c81bd 15678static bool
9d862524
MR
15679fix_bad_cross_mode_branch_p (fixS *fixP)
15680{
5b7c81bd 15681 bool absolute_p;
9d862524
MR
15682 unsigned long opcode;
15683 asection *symsec;
15684 valueT val;
15685 int other;
15686 char *buf;
15687
8b10b0b3 15688 if (mips_ignore_branch_isa)
5b7c81bd 15689 return false;
8b10b0b3 15690
5b7c81bd
AM
15691 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15692 return false;
9d862524
MR
15693
15694 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15695 absolute_p = bfd_is_abs_section (symsec);
15696
15697 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15698 other = S_GET_OTHER (fixP->fx_addsy);
15699
15700 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15701 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15702 switch (fixP->fx_r_type)
15703 {
15704 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15705 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15706 && opcode != 0x0411);
15707 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15708 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15709 && opcode != 0x4060);
9d862524
MR
15710 case BFD_RELOC_MIPS_21_PCREL_S2:
15711 case BFD_RELOC_MIPS_26_PCREL_S2:
15712 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15713 case BFD_RELOC_MIPS16_16_PCREL_S1:
15714 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15715 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15716 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15717 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15718 default:
15719 abort ();
15720 }
15721}
15722
15723/* Return TRUE if the symbol plus addend associated with a regular MIPS
15724 branch instruction pointed to by FIXP is not aligned according to the
15725 branch instruction's immediate field requirement. We need the addend
15726 to preserve the ISA bit and also the sum must not have bit 2 set. We
15727 must explicitly OR in the ISA bit from symbol annotation as the bit
15728 won't be set in the symbol's value then. */
15729
5b7c81bd 15730static bool
9d862524
MR
15731fix_bad_misaligned_branch_p (fixS *fixP)
15732{
5b7c81bd 15733 bool absolute_p;
9d862524
MR
15734 asection *symsec;
15735 valueT isa_bit;
15736 valueT val;
15737 valueT off;
15738 int other;
15739
5b7c81bd
AM
15740 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15741 return false;
9d862524
MR
15742
15743 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15744 absolute_p = bfd_is_abs_section (symsec);
15745
15746 val = S_GET_VALUE (fixP->fx_addsy);
15747 other = S_GET_OTHER (fixP->fx_addsy);
15748 off = fixP->fx_offset;
15749
15750 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15751 val |= ELF_ST_IS_COMPRESSED (other);
15752 val += off;
15753 return (val & 0x3) != isa_bit;
15754}
15755
52031738
FS
15756/* Calculate the relocation target by masking off ISA mode bit before
15757 combining symbol and addend. */
15758
15759static valueT
15760fix_bad_misaligned_address (fixS *fixP)
15761{
15762 valueT val;
15763 valueT off;
15764 unsigned isa_mode;
15765 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15766 val = S_GET_VALUE (fixP->fx_addsy);
15767 off = fixP->fx_offset;
15768 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15769 ? 1 : 0);
15770
15771 return ((val & ~isa_mode) + off);
15772}
15773
9d862524
MR
15774/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15775 and its calculated value VAL. */
15776
15777static void
15778fix_validate_branch (fixS *fixP, valueT val)
15779{
15780 if (fixP->fx_done && (val & 0x3) != 0)
15781 as_bad_where (fixP->fx_file, fixP->fx_line,
15782 _("branch to misaligned address (0x%lx)"),
15783 (long) (val + md_pcrel_from (fixP)));
15784 else if (fix_bad_cross_mode_branch_p (fixP))
15785 as_bad_where (fixP->fx_file, fixP->fx_line,
15786 _("branch to a symbol in another ISA mode"));
15787 else if (fix_bad_misaligned_branch_p (fixP))
15788 as_bad_where (fixP->fx_file, fixP->fx_line,
15789 _("branch to misaligned address (0x%lx)"),
52031738 15790 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15791 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15792 as_bad_where (fixP->fx_file, fixP->fx_line,
15793 _("cannot encode misaligned addend "
15794 "in the relocatable field (0x%lx)"),
15795 (long) fixP->fx_offset);
15796}
15797
252b5132
RH
15798/* Apply a fixup to the object file. */
15799
94f592af 15800void
55cf6793 15801md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15802{
4d68580a 15803 char *buf;
b886a2ab 15804 unsigned long insn;
a7ebbfdf 15805 reloc_howto_type *howto;
252b5132 15806
d56a8dda
RS
15807 if (fixP->fx_pcrel)
15808 switch (fixP->fx_r_type)
15809 {
15810 case BFD_RELOC_16_PCREL_S2:
c9775dde 15811 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15812 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15813 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15814 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15815 case BFD_RELOC_32_PCREL:
7361da2c
AB
15816 case BFD_RELOC_MIPS_21_PCREL_S2:
15817 case BFD_RELOC_MIPS_26_PCREL_S2:
15818 case BFD_RELOC_MIPS_18_PCREL_S3:
15819 case BFD_RELOC_MIPS_19_PCREL_S2:
15820 case BFD_RELOC_HI16_S_PCREL:
15821 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15822 break;
15823
15824 case BFD_RELOC_32:
15825 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15826 break;
15827
15828 default:
15829 as_bad_where (fixP->fx_file, fixP->fx_line,
15830 _("PC-relative reference to a different section"));
15831 break;
15832 }
15833
d712f276
AM
15834 /* Handle BFD_RELOC_8 and BFD_RELOC_16. Punt on other bfd
15835 relocations that have no MIPS ELF equivalent. */
15836 if (fixP->fx_r_type != BFD_RELOC_8
15837 && fixP->fx_r_type != BFD_RELOC_16)
d56a8dda
RS
15838 {
15839 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15840 if (!howto)
15841 return;
15842 }
65551fa4 15843
df58fc94
RS
15844 gas_assert (fixP->fx_size == 2
15845 || fixP->fx_size == 4
d56a8dda 15846 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15847 || fixP->fx_r_type == BFD_RELOC_64
15848 || fixP->fx_r_type == BFD_RELOC_CTOR
15849 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15850 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15851 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15852 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15853 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15854 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15855
4d68580a 15856 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15857
b1dca8ee
RS
15858 /* Don't treat parts of a composite relocation as done. There are two
15859 reasons for this:
15860
15861 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15862 should nevertheless be emitted if the first part is.
15863
15864 (2) In normal usage, composite relocations are never assembly-time
15865 constants. The easiest way of dealing with the pathological
15866 exceptions is to generate a relocation against STN_UNDEF and
15867 leave everything up to the linker. */
3994f87e 15868 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15869 fixP->fx_done = 1;
15870
15871 switch (fixP->fx_r_type)
15872 {
3f98094e
DJ
15873 case BFD_RELOC_MIPS_TLS_GD:
15874 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15875 case BFD_RELOC_MIPS_TLS_DTPREL32:
15876 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15877 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15878 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15879 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15880 case BFD_RELOC_MIPS_TLS_TPREL32:
15881 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15882 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15883 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15884 case BFD_RELOC_MICROMIPS_TLS_GD:
15885 case BFD_RELOC_MICROMIPS_TLS_LDM:
15886 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15887 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15888 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15889 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15890 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15891 case BFD_RELOC_MIPS16_TLS_GD:
15892 case BFD_RELOC_MIPS16_TLS_LDM:
15893 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15894 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15895 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15896 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15897 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15898 if (fixP->fx_addsy)
15899 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15900 else
15901 as_bad_where (fixP->fx_file, fixP->fx_line,
15902 _("TLS relocation against a constant"));
15903 break;
3f98094e 15904
252b5132 15905 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15906 case BFD_RELOC_MIPS16_JMP:
15907 case BFD_RELOC_MICROMIPS_JMP:
15908 {
15909 int shift;
15910
15911 gas_assert (!fixP->fx_done);
15912
15913 /* Shift is 2, unusually, for microMIPS JALX. */
15914 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15915 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15916 shift = 1;
15917 else
15918 shift = 2;
15919
15920 if (fix_bad_cross_mode_jump_p (fixP))
15921 as_bad_where (fixP->fx_file, fixP->fx_line,
15922 _("jump to a symbol in another ISA mode"));
15923 else if (fix_bad_same_mode_jalx_p (fixP))
15924 as_bad_where (fixP->fx_file, fixP->fx_line,
15925 _("JALX to a symbol in the same ISA mode"));
15926 else if (fix_bad_misaligned_jump_p (fixP, shift))
15927 as_bad_where (fixP->fx_file, fixP->fx_line,
15928 _("jump to misaligned address (0x%lx)"),
52031738 15929 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15930 else if (HAVE_IN_PLACE_ADDENDS
15931 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15932 as_bad_where (fixP->fx_file, fixP->fx_line,
15933 _("cannot encode misaligned addend "
15934 "in the relocatable field (0x%lx)"),
15935 (long) fixP->fx_offset);
15936 }
15937 /* Fall through. */
15938
e369bcce
TS
15939 case BFD_RELOC_MIPS_SHIFT5:
15940 case BFD_RELOC_MIPS_SHIFT6:
15941 case BFD_RELOC_MIPS_GOT_DISP:
15942 case BFD_RELOC_MIPS_GOT_PAGE:
15943 case BFD_RELOC_MIPS_GOT_OFST:
15944 case BFD_RELOC_MIPS_SUB:
15945 case BFD_RELOC_MIPS_INSERT_A:
15946 case BFD_RELOC_MIPS_INSERT_B:
15947 case BFD_RELOC_MIPS_DELETE:
15948 case BFD_RELOC_MIPS_HIGHEST:
15949 case BFD_RELOC_MIPS_HIGHER:
15950 case BFD_RELOC_MIPS_SCN_DISP:
e369bcce
TS
15951 case BFD_RELOC_MIPS_RELGOT:
15952 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15953 case BFD_RELOC_HI16:
15954 case BFD_RELOC_HI16_S:
b886a2ab 15955 case BFD_RELOC_LO16:
cdf6fd85 15956 case BFD_RELOC_GPREL16:
252b5132
RH
15957 case BFD_RELOC_MIPS_LITERAL:
15958 case BFD_RELOC_MIPS_CALL16:
15959 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15960 case BFD_RELOC_GPREL32:
252b5132
RH
15961 case BFD_RELOC_MIPS_GOT_HI16:
15962 case BFD_RELOC_MIPS_GOT_LO16:
15963 case BFD_RELOC_MIPS_CALL_HI16:
15964 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15965 case BFD_RELOC_HI16_S_PCREL:
15966 case BFD_RELOC_LO16_PCREL:
252b5132 15967 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15968 case BFD_RELOC_MIPS16_GOT16:
15969 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15970 case BFD_RELOC_MIPS16_HI16:
15971 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15972 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15973 case BFD_RELOC_MICROMIPS_GOT_DISP:
15974 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15975 case BFD_RELOC_MICROMIPS_GOT_OFST:
15976 case BFD_RELOC_MICROMIPS_SUB:
15977 case BFD_RELOC_MICROMIPS_HIGHEST:
15978 case BFD_RELOC_MICROMIPS_HIGHER:
15979 case BFD_RELOC_MICROMIPS_SCN_DISP:
15980 case BFD_RELOC_MICROMIPS_JALR:
15981 case BFD_RELOC_MICROMIPS_HI16:
15982 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15983 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15984 case BFD_RELOC_MICROMIPS_GPREL16:
15985 case BFD_RELOC_MICROMIPS_LITERAL:
15986 case BFD_RELOC_MICROMIPS_CALL16:
15987 case BFD_RELOC_MICROMIPS_GOT16:
15988 case BFD_RELOC_MICROMIPS_GOT_HI16:
15989 case BFD_RELOC_MICROMIPS_GOT_LO16:
15990 case BFD_RELOC_MICROMIPS_CALL_HI16:
15991 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15992 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15993 if (fixP->fx_done)
15994 {
15995 offsetT value;
15996
15997 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15998 {
15999 insn = read_reloc_insn (buf, fixP->fx_r_type);
16000 if (mips16_reloc_p (fixP->fx_r_type))
16001 insn |= mips16_immed_extend (value, 16);
16002 else
16003 insn |= (value & 0xffff);
16004 write_reloc_insn (buf, fixP->fx_r_type, insn);
16005 }
16006 else
16007 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16008 _("unsupported constant in relocation"));
b886a2ab 16009 }
252b5132
RH
16010 break;
16011
252b5132
RH
16012 case BFD_RELOC_64:
16013 /* This is handled like BFD_RELOC_32, but we output a sign
16014 extended value if we are only 32 bits. */
3e722fb5 16015 if (fixP->fx_done)
252b5132
RH
16016 {
16017 if (8 <= sizeof (valueT))
4d68580a 16018 md_number_to_chars (buf, *valP, 8);
252b5132
RH
16019 else
16020 {
a7ebbfdf 16021 valueT hiv;
252b5132 16022
a7ebbfdf 16023 if ((*valP & 0x80000000) != 0)
252b5132
RH
16024 hiv = 0xffffffff;
16025 else
16026 hiv = 0;
4d68580a
RS
16027 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16028 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
16029 }
16030 }
16031 break;
16032
056350c6 16033 case BFD_RELOC_RVA:
252b5132 16034 case BFD_RELOC_32:
b47468a6 16035 case BFD_RELOC_32_PCREL:
d712f276 16036 case BFD_RELOC_MIPS_16:
252b5132 16037 case BFD_RELOC_16:
d56a8dda 16038 case BFD_RELOC_8:
252b5132 16039 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
16040 value now. This can happen if we have a .word which is not
16041 resolved when it appears but is later defined. */
252b5132 16042 if (fixP->fx_done)
4d68580a 16043 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
16044 break;
16045
7361da2c 16046 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 16047 fix_validate_branch (fixP, *valP);
41947d9e
MR
16048 if (!fixP->fx_done)
16049 break;
16050
16051 if (*valP + 0x400000 <= 0x7fffff)
16052 {
16053 insn = read_insn (buf);
16054 insn |= (*valP >> 2) & 0x1fffff;
16055 write_insn (buf, insn);
16056 }
16057 else
16058 as_bad_where (fixP->fx_file, fixP->fx_line,
16059 _("branch out of range"));
16060 break;
16061
7361da2c 16062 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 16063 fix_validate_branch (fixP, *valP);
41947d9e
MR
16064 if (!fixP->fx_done)
16065 break;
7361da2c 16066
41947d9e
MR
16067 if (*valP + 0x8000000 <= 0xfffffff)
16068 {
16069 insn = read_insn (buf);
16070 insn |= (*valP >> 2) & 0x3ffffff;
16071 write_insn (buf, insn);
16072 }
16073 else
16074 as_bad_where (fixP->fx_file, fixP->fx_line,
16075 _("branch out of range"));
7361da2c
AB
16076 break;
16077
16078 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 16079 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 16080 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
16081 _("PC-relative access using misaligned symbol (%lx)"),
16082 (long) S_GET_VALUE (fixP->fx_addsy));
16083 if ((fixP->fx_offset & 0x7) != 0)
16084 as_bad_where (fixP->fx_file, fixP->fx_line,
16085 _("PC-relative access using misaligned offset (%lx)"),
16086 (long) fixP->fx_offset);
41947d9e
MR
16087 if (!fixP->fx_done)
16088 break;
7361da2c 16089
41947d9e
MR
16090 if (*valP + 0x100000 <= 0x1fffff)
16091 {
16092 insn = read_insn (buf);
16093 insn |= (*valP >> 3) & 0x3ffff;
16094 write_insn (buf, insn);
16095 }
16096 else
16097 as_bad_where (fixP->fx_file, fixP->fx_line,
16098 _("PC-relative access out of range"));
7361da2c
AB
16099 break;
16100
16101 case BFD_RELOC_MIPS_19_PCREL_S2:
16102 if ((*valP & 0x3) != 0)
16103 as_bad_where (fixP->fx_file, fixP->fx_line,
16104 _("PC-relative access to misaligned address (%lx)"),
717ba204 16105 (long) *valP);
41947d9e
MR
16106 if (!fixP->fx_done)
16107 break;
7361da2c 16108
41947d9e
MR
16109 if (*valP + 0x100000 <= 0x1fffff)
16110 {
16111 insn = read_insn (buf);
16112 insn |= (*valP >> 2) & 0x7ffff;
16113 write_insn (buf, insn);
16114 }
16115 else
16116 as_bad_where (fixP->fx_file, fixP->fx_line,
16117 _("PC-relative access out of range"));
7361da2c
AB
16118 break;
16119
252b5132 16120 case BFD_RELOC_16_PCREL_S2:
9d862524 16121 fix_validate_branch (fixP, *valP);
cb56d3d3 16122
54f4ddb3
TS
16123 /* We need to save the bits in the instruction since fixup_segment()
16124 might be deleting the relocation entry (i.e., a branch within
16125 the current segment). */
a7ebbfdf 16126 if (! fixP->fx_done)
bb2d6cd7 16127 break;
252b5132 16128
54f4ddb3 16129 /* Update old instruction data. */
4d68580a 16130 insn = read_insn (buf);
252b5132 16131
a7ebbfdf
TS
16132 if (*valP + 0x20000 <= 0x3ffff)
16133 {
16134 insn |= (*valP >> 2) & 0xffff;
4d68580a 16135 write_insn (buf, insn);
a7ebbfdf 16136 }
ce8ad872 16137 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16138 && fixP->fx_done
16139 && fixP->fx_frag->fr_address >= text_section->vma
16140 && (fixP->fx_frag->fr_address
fd361982 16141 < text_section->vma + bfd_section_size (text_section))
a7ebbfdf
TS
16142 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16143 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16144 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16145 {
16146 /* The branch offset is too large. If this is an
16147 unconditional branch, and we are not generating PIC code,
16148 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16149 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16150 insn = 0x0c000000; /* jal */
252b5132 16151 else
a7ebbfdf
TS
16152 insn = 0x08000000; /* j */
16153 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16154 fixP->fx_done = 0;
16155 fixP->fx_addsy = section_symbol (text_section);
16156 *valP += md_pcrel_from (fixP);
4d68580a 16157 write_insn (buf, insn);
a7ebbfdf
TS
16158 }
16159 else
16160 {
16161 /* If we got here, we have branch-relaxation disabled,
16162 and there's nothing we can do to fix this instruction
16163 without turning it into a longer sequence. */
16164 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16165 _("branch out of range"));
252b5132 16166 }
252b5132
RH
16167 break;
16168
c9775dde 16169 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16170 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16171 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16172 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16173 gas_assert (!fixP->fx_done);
9d862524
MR
16174 if (fix_bad_cross_mode_branch_p (fixP))
16175 as_bad_where (fixP->fx_file, fixP->fx_line,
16176 _("branch to a symbol in another ISA mode"));
16177 else if (fixP->fx_addsy
5b7c81bd 16178 && !S_FORCE_RELOC (fixP->fx_addsy, true)
9d862524
MR
16179 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16180 && (fixP->fx_offset & 0x1) != 0)
16181 as_bad_where (fixP->fx_file, fixP->fx_line,
16182 _("branch to misaligned address (0x%lx)"),
52031738 16183 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
16184 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16185 as_bad_where (fixP->fx_file, fixP->fx_line,
16186 _("cannot encode misaligned addend "
16187 "in the relocatable field (0x%lx)"),
16188 (long) fixP->fx_offset);
df58fc94
RS
16189 break;
16190
252b5132
RH
16191 case BFD_RELOC_VTABLE_INHERIT:
16192 fixP->fx_done = 0;
16193 if (fixP->fx_addsy
16194 && !S_IS_DEFINED (fixP->fx_addsy)
16195 && !S_IS_WEAK (fixP->fx_addsy))
16196 S_SET_WEAK (fixP->fx_addsy);
16197 break;
16198
2f0c68f2 16199 case BFD_RELOC_NONE:
252b5132
RH
16200 case BFD_RELOC_VTABLE_ENTRY:
16201 fixP->fx_done = 0;
16202 break;
16203
16204 default:
b37df7c4 16205 abort ();
252b5132 16206 }
a7ebbfdf
TS
16207
16208 /* Remember value for tc_gen_reloc. */
16209 fixP->fx_addnumber = *valP;
252b5132
RH
16210}
16211
252b5132 16212static symbolS *
17a2f251 16213get_symbol (void)
252b5132
RH
16214{
16215 int c;
16216 char *name;
16217 symbolS *p;
16218
d02603dc 16219 c = get_symbol_name (&name);
252b5132 16220 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16221 (void) restore_line_pointer (c);
252b5132
RH
16222 return p;
16223}
16224
742a56fe
RS
16225/* Align the current frag to a given power of two. If a particular
16226 fill byte should be used, FILL points to an integer that contains
16227 that byte, otherwise FILL is null.
16228
462427c4
RS
16229 This function used to have the comment:
16230
16231 The MIPS assembler also automatically adjusts any preceding label.
16232
16233 The implementation therefore applied the adjustment to a maximum of
16234 one label. However, other label adjustments are applied to batches
16235 of labels, and adjusting just one caused problems when new labels
16236 were added for the sake of debugging or unwind information.
16237 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16238
16239static void
462427c4 16240mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16241{
7d10b47d 16242 mips_emit_delays ();
df58fc94 16243 mips_record_compressed_mode ();
742a56fe
RS
16244 if (fill == NULL && subseg_text_p (now_seg))
16245 frag_align_code (to, 0);
16246 else
16247 frag_align (to, fill ? *fill : 0, 0);
252b5132 16248 record_alignment (now_seg, to);
770c0151 16249 mips_move_labels (labels, subseg_text_p (now_seg));
252b5132
RH
16250}
16251
16252/* Align to a given power of two. .align 0 turns off the automatic
16253 alignment used by the data creating pseudo-ops. */
16254
16255static void
17a2f251 16256s_align (int x ATTRIBUTE_UNUSED)
252b5132 16257{
742a56fe 16258 int temp, fill_value, *fill_ptr;
49954fb4 16259 long max_alignment = 28;
252b5132 16260
5999477d
AM
16261 file_mips_check_options ();
16262
54f4ddb3 16263 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16264 to the aligned address.
54f4ddb3 16265 o It's not documented but auto alignment is reinstated by
252b5132 16266 a .align pseudo instruction.
54f4ddb3 16267 o Note also that after auto alignment is turned off the mips assembler
252b5132 16268 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16269 We don't. */
252b5132
RH
16270
16271 temp = get_absolute_expression ();
16272 if (temp > max_alignment)
1661c76c 16273 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16274 else if (temp < 0)
16275 {
1661c76c 16276 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16277 temp = 0;
16278 }
16279 if (*input_line_pointer == ',')
16280 {
f9419b05 16281 ++input_line_pointer;
742a56fe
RS
16282 fill_value = get_absolute_expression ();
16283 fill_ptr = &fill_value;
252b5132
RH
16284 }
16285 else
742a56fe 16286 fill_ptr = 0;
5999477d
AM
16287
16288 mips_mark_labels ();
16289
252b5132
RH
16290 if (temp)
16291 {
a8dbcb85
TS
16292 segment_info_type *si = seg_info (now_seg);
16293 struct insn_label_list *l = si->label_list;
54f4ddb3 16294 /* Auto alignment should be switched on by next section change. */
252b5132 16295 auto_align = 1;
462427c4 16296 mips_align (temp, fill_ptr, l);
252b5132
RH
16297 }
16298 else
16299 {
16300 auto_align = 0;
16301 }
16302
16303 demand_empty_rest_of_line ();
16304}
16305
252b5132 16306static void
17a2f251 16307s_change_sec (int sec)
252b5132
RH
16308{
16309 segT seg;
16310
252b5132
RH
16311 /* The ELF backend needs to know that we are changing sections, so
16312 that .previous works correctly. We could do something like check
b6ff326e 16313 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16314 as it would not be appropriate to use it in the section changing
16315 functions in read.c, since obj-elf.c intercepts those. FIXME:
16316 This should be cleaner, somehow. */
f3ded42a 16317 obj_elf_section_change_hook ();
252b5132 16318
7d10b47d 16319 mips_emit_delays ();
6a32d874 16320
252b5132
RH
16321 switch (sec)
16322 {
16323 case 't':
16324 s_text (0);
16325 break;
16326 case 'd':
16327 s_data (0);
16328 break;
16329 case 'b':
16330 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16331 demand_empty_rest_of_line ();
16332 break;
16333
16334 case 'r':
4d0d148d
TS
16335 seg = subseg_new (RDATA_SECTION_NAME,
16336 (subsegT) get_absolute_expression ());
fd361982
AM
16337 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16338 | SEC_RELOC | SEC_DATA));
d34049e8 16339 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16340 record_alignment (seg, 4);
4d0d148d 16341 demand_empty_rest_of_line ();
252b5132
RH
16342 break;
16343
16344 case 's':
4d0d148d 16345 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
a4dd6c97
AM
16346 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16347 | SEC_DATA | SEC_SMALL_DATA));
d34049e8 16348 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16349 record_alignment (seg, 4);
4d0d148d
TS
16350 demand_empty_rest_of_line ();
16351 break;
998b3c36
MR
16352
16353 case 'B':
16354 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
a4dd6c97 16355 bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
d34049e8 16356 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16357 record_alignment (seg, 4);
998b3c36
MR
16358 demand_empty_rest_of_line ();
16359 break;
252b5132
RH
16360 }
16361
16362 auto_align = 1;
16363}
b34976b6 16364
cca86cc8 16365void
17a2f251 16366s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16367{
d02603dc 16368 char *saved_ilp;
16849591
JB
16369 const char *section_name;
16370 char c, next_c = 0;
cca86cc8
SC
16371 int section_type;
16372 int section_flag;
16373 int section_entry_size;
16374 int section_alignment;
b34976b6 16375
d02603dc 16376 saved_ilp = input_line_pointer;
16849591
JB
16377 section_name = obj_elf_section_name ();
16378 if (section_name == NULL)
16379 return;
16380 c = input_line_pointer[0];
a816d1ed 16381 if (c)
16849591 16382 next_c = input_line_pointer[1];
cca86cc8 16383
4cf0dd0d
TS
16384 /* Do we have .section Name<,"flags">? */
16385 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16386 {
d02603dc 16387 input_line_pointer = saved_ilp;
cca86cc8
SC
16388 obj_elf_section (ignore);
16389 return;
16390 }
d02603dc 16391
cca86cc8
SC
16392 input_line_pointer++;
16393
16394 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16395 if (c == ',')
16396 section_type = get_absolute_expression ();
16397 else
16398 section_type = 0;
d02603dc 16399
cca86cc8
SC
16400 if (*input_line_pointer++ == ',')
16401 section_flag = get_absolute_expression ();
16402 else
16403 section_flag = 0;
d02603dc 16404
cca86cc8
SC
16405 if (*input_line_pointer++ == ',')
16406 section_entry_size = get_absolute_expression ();
16407 else
16408 section_entry_size = 0;
d02603dc 16409
cca86cc8
SC
16410 if (*input_line_pointer++ == ',')
16411 section_alignment = get_absolute_expression ();
16412 else
16413 section_alignment = 0;
d02603dc 16414
87975d2a
AM
16415 /* FIXME: really ignore? */
16416 (void) section_alignment;
cca86cc8 16417
8ab8a5c8
RS
16418 /* When using the generic form of .section (as implemented by obj-elf.c),
16419 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16420 traditionally had to fall back on the more common @progbits instead.
16421
16422 There's nothing really harmful in this, since bfd will correct
16423 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16424 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16425 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16426
16427 Even so, we shouldn't force users of the MIPS .section syntax to
16428 incorrectly label the sections as SHT_PROGBITS. The best compromise
16429 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16430 generic type-checking code. */
16431 if (section_type == SHT_MIPS_DWARF)
16432 section_type = SHT_PROGBITS;
16433
a8c4d40b 16434 obj_elf_change_section (section_name, section_type, section_flag,
cca86cc8
SC
16435 section_entry_size, 0, 0, 0);
16436}
252b5132
RH
16437
16438void
17a2f251 16439mips_enable_auto_align (void)
252b5132
RH
16440{
16441 auto_align = 1;
16442}
16443
16444static void
17a2f251 16445s_cons (int log_size)
252b5132 16446{
a8dbcb85
TS
16447 segment_info_type *si = seg_info (now_seg);
16448 struct insn_label_list *l = si->label_list;
252b5132 16449
7d10b47d 16450 mips_emit_delays ();
252b5132 16451 if (log_size > 0 && auto_align)
462427c4 16452 mips_align (log_size, 0, l);
252b5132 16453 cons (1 << log_size);
a1facbec 16454 mips_clear_insn_labels ();
252b5132
RH
16455}
16456
16457static void
17a2f251 16458s_float_cons (int type)
252b5132 16459{
a8dbcb85
TS
16460 segment_info_type *si = seg_info (now_seg);
16461 struct insn_label_list *l = si->label_list;
252b5132 16462
7d10b47d 16463 mips_emit_delays ();
252b5132
RH
16464
16465 if (auto_align)
49309057
ILT
16466 {
16467 if (type == 'd')
462427c4 16468 mips_align (3, 0, l);
49309057 16469 else
462427c4 16470 mips_align (2, 0, l);
49309057 16471 }
252b5132 16472
252b5132 16473 float_cons (type);
a1facbec 16474 mips_clear_insn_labels ();
252b5132
RH
16475}
16476
16477/* Handle .globl. We need to override it because on Irix 5 you are
16478 permitted to say
16479 .globl foo .text
16480 where foo is an undefined symbol, to mean that foo should be
16481 considered to be the address of a function. */
16482
16483static void
17a2f251 16484s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16485{
16486 char *name;
16487 int c;
16488 symbolS *symbolP;
252b5132 16489
8a06b769 16490 do
252b5132 16491 {
d02603dc 16492 c = get_symbol_name (&name);
8a06b769
TS
16493 symbolP = symbol_find_or_make (name);
16494 S_SET_EXTERNAL (symbolP);
16495
252b5132 16496 *input_line_pointer = c;
d02603dc 16497 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16498
8a06b769
TS
16499 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16500 && (*input_line_pointer != ','))
16501 {
16502 char *secname;
16503 asection *sec;
16504
d02603dc 16505 c = get_symbol_name (&secname);
8a06b769
TS
16506 sec = bfd_get_section_by_name (stdoutput, secname);
16507 if (sec == NULL)
16508 as_bad (_("%s: no such section"), secname);
d02603dc 16509 (void) restore_line_pointer (c);
8a06b769
TS
16510
16511 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
d69cd47e 16512 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
8a06b769
TS
16513 }
16514
8a06b769
TS
16515 c = *input_line_pointer;
16516 if (c == ',')
16517 {
16518 input_line_pointer++;
16519 SKIP_WHITESPACE ();
16520 if (is_end_of_line[(unsigned char) *input_line_pointer])
16521 c = '\n';
16522 }
16523 }
16524 while (c == ',');
252b5132 16525
252b5132
RH
16526 demand_empty_rest_of_line ();
16527}
16528
d69cd47e
AM
16529#ifdef TE_IRIX
16530/* The Irix 5 and 6 assemblers set the type of any common symbol and
16531 any undefined non-function symbol to STT_OBJECT. We try to be
16532 compatible, since newer Irix 5 and 6 linkers care. */
16533
16534void
16535mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16536{
16537 /* This late in assembly we can set BSF_OBJECT indiscriminately
16538 and let elf.c:swap_out_syms sort out the symbol type. */
16539 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16540 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16541 || !S_IS_DEFINED (symp))
16542 *flags |= BSF_OBJECT;
16543}
16544#endif
16545
252b5132 16546static void
17a2f251 16547s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16548{
16549 char *opt;
16550 char c;
16551
d02603dc 16552 c = get_symbol_name (&opt);
252b5132
RH
16553
16554 if (*opt == 'O')
16555 {
16556 /* FIXME: What does this mean? */
16557 }
d34049e8 16558 else if (startswith (opt, "pic") && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16559 {
16560 int i;
16561
16562 i = atoi (opt + 3);
668c5ebc
MR
16563 if (i != 0 && i != 2)
16564 as_bad (_(".option pic%d not supported"), i);
16565 else if (mips_pic == VXWORKS_PIC)
16566 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16567 else if (i == 0)
252b5132
RH
16568 mips_pic = NO_PIC;
16569 else if (i == 2)
143d77c5 16570 {
8b828383 16571 mips_pic = SVR4_PIC;
5b7c81bd 16572 mips_abicalls = true;
143d77c5 16573 }
252b5132 16574
4d0d148d 16575 if (mips_pic == SVR4_PIC)
252b5132
RH
16576 {
16577 if (g_switch_seen && g_switch_value != 0)
16578 as_warn (_("-G may not be used with SVR4 PIC code"));
16579 g_switch_value = 0;
16580 bfd_set_gp_size (stdoutput, 0);
16581 }
16582 }
16583 else
1661c76c 16584 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16585
d02603dc 16586 (void) restore_line_pointer (c);
252b5132
RH
16587 demand_empty_rest_of_line ();
16588}
16589
16590/* This structure is used to hold a stack of .set values. */
16591
e972090a
NC
16592struct mips_option_stack
16593{
252b5132
RH
16594 struct mips_option_stack *next;
16595 struct mips_set_options options;
16596};
16597
16598static struct mips_option_stack *mips_opts_stack;
16599
22522f88
MR
16600/* Return status for .set/.module option handling. */
16601
16602enum code_option_type
16603{
16604 /* Unrecognized option. */
16605 OPTION_TYPE_BAD = -1,
16606
16607 /* Ordinary option. */
16608 OPTION_TYPE_NORMAL,
16609
16610 /* ISA changing option. */
16611 OPTION_TYPE_ISA
16612};
16613
16614/* Handle common .set/.module options. Return status indicating option
16615 type. */
16616
16617static enum code_option_type
919731af 16618parse_code_option (char * name)
252b5132 16619{
5b7c81bd 16620 bool isa_set = false;
c6278170 16621 const struct mips_ase *ase;
22522f88 16622
d34049e8 16623 if (startswith (name, "at="))
741fe287
MR
16624 {
16625 char *s = name + 3;
16626
16627 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16628 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16629 }
252b5132 16630 else if (strcmp (name, "at") == 0)
919731af 16631 mips_opts.at = ATREG;
252b5132 16632 else if (strcmp (name, "noat") == 0)
919731af 16633 mips_opts.at = ZERO;
252b5132 16634 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16635 mips_opts.nomove = 0;
252b5132 16636 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16637 mips_opts.nomove = 1;
252b5132 16638 else if (strcmp (name, "bopt") == 0)
919731af 16639 mips_opts.nobopt = 0;
252b5132 16640 else if (strcmp (name, "nobopt") == 0)
919731af 16641 mips_opts.nobopt = 1;
ad3fea08 16642 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16643 mips_opts.gp = 32;
ad3fea08 16644 else if (strcmp (name, "gp=64") == 0)
919731af 16645 mips_opts.gp = 64;
ad3fea08 16646 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16647 mips_opts.fp = 32;
351cdf24
MF
16648 else if (strcmp (name, "fp=xx") == 0)
16649 mips_opts.fp = 0;
ad3fea08 16650 else if (strcmp (name, "fp=64") == 0)
919731af 16651 mips_opts.fp = 64;
037b32b9
AN
16652 else if (strcmp (name, "softfloat") == 0)
16653 mips_opts.soft_float = 1;
16654 else if (strcmp (name, "hardfloat") == 0)
16655 mips_opts.soft_float = 0;
16656 else if (strcmp (name, "singlefloat") == 0)
16657 mips_opts.single_float = 1;
16658 else if (strcmp (name, "doublefloat") == 0)
16659 mips_opts.single_float = 0;
351cdf24
MF
16660 else if (strcmp (name, "nooddspreg") == 0)
16661 mips_opts.oddspreg = 0;
16662 else if (strcmp (name, "oddspreg") == 0)
16663 mips_opts.oddspreg = 1;
252b5132
RH
16664 else if (strcmp (name, "mips16") == 0
16665 || strcmp (name, "MIPS-16") == 0)
919731af 16666 mips_opts.mips16 = 1;
252b5132
RH
16667 else if (strcmp (name, "nomips16") == 0
16668 || strcmp (name, "noMIPS-16") == 0)
16669 mips_opts.mips16 = 0;
df58fc94 16670 else if (strcmp (name, "micromips") == 0)
919731af 16671 mips_opts.micromips = 1;
df58fc94
RS
16672 else if (strcmp (name, "nomicromips") == 0)
16673 mips_opts.micromips = 0;
c6278170
RS
16674 else if (name[0] == 'n'
16675 && name[1] == 'o'
16676 && (ase = mips_lookup_ase (name + 2)))
5b7c81bd 16677 mips_set_ase (ase, &mips_opts, false);
c6278170 16678 else if ((ase = mips_lookup_ase (name)))
5b7c81bd 16679 mips_set_ase (ase, &mips_opts, true);
d34049e8 16680 else if (startswith (name, "mips") || startswith (name, "arch="))
252b5132 16681 {
1a2c1fad
CD
16682 /* Permit the user to change the ISA and architecture on the fly.
16683 Needless to say, misuse can cause serious problems. */
d34049e8 16684 if (startswith (name, "arch="))
1a2c1fad
CD
16685 {
16686 const struct mips_cpu_info *p;
16687
919731af 16688 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16689 if (!p)
16690 as_bad (_("unknown architecture %s"), name + 5);
16691 else
16692 {
16693 mips_opts.arch = p->cpu;
16694 mips_opts.isa = p->isa;
5b7c81bd 16695 isa_set = true;
3315614d 16696 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16697 }
16698 }
d34049e8 16699 else if (startswith (name, "mips"))
81a21e38
TS
16700 {
16701 const struct mips_cpu_info *p;
16702
919731af 16703 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16704 if (!p)
16705 as_bad (_("unknown ISA level %s"), name + 4);
16706 else
16707 {
16708 mips_opts.arch = p->cpu;
16709 mips_opts.isa = p->isa;
5b7c81bd 16710 isa_set = true;
3315614d 16711 mips_opts.init_ase = p->ase;
81a21e38
TS
16712 }
16713 }
af7ee8bf 16714 else
81a21e38 16715 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16716 }
16717 else if (strcmp (name, "autoextend") == 0)
16718 mips_opts.noautoextend = 0;
16719 else if (strcmp (name, "noautoextend") == 0)
16720 mips_opts.noautoextend = 1;
833794fc 16721 else if (strcmp (name, "insn32") == 0)
5b7c81bd 16722 mips_opts.insn32 = true;
833794fc 16723 else if (strcmp (name, "noinsn32") == 0)
5b7c81bd 16724 mips_opts.insn32 = false;
919731af 16725 else if (strcmp (name, "sym32") == 0)
5b7c81bd 16726 mips_opts.sym32 = true;
919731af 16727 else if (strcmp (name, "nosym32") == 0)
5b7c81bd 16728 mips_opts.sym32 = false;
919731af 16729 else
22522f88
MR
16730 return OPTION_TYPE_BAD;
16731
16732 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16733}
16734
16735/* Handle the .set pseudo-op. */
16736
16737static void
16738s_mipsset (int x ATTRIBUTE_UNUSED)
16739{
22522f88 16740 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16741 char *name = input_line_pointer, ch;
919731af 16742
16743 file_mips_check_options ();
16744
16745 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16746 ++input_line_pointer;
16747 ch = *input_line_pointer;
16748 *input_line_pointer = '\0';
16749
16750 if (strchr (name, ','))
16751 {
16752 /* Generic ".set" directive; use the generic handler. */
16753 *input_line_pointer = ch;
16754 input_line_pointer = name;
16755 s_set (0);
16756 return;
16757 }
16758
16759 if (strcmp (name, "reorder") == 0)
16760 {
16761 if (mips_opts.noreorder)
16762 end_noreorder ();
16763 }
16764 else if (strcmp (name, "noreorder") == 0)
16765 {
16766 if (!mips_opts.noreorder)
16767 start_noreorder ();
16768 }
16769 else if (strcmp (name, "macro") == 0)
16770 mips_opts.warn_about_macros = 0;
16771 else if (strcmp (name, "nomacro") == 0)
16772 {
16773 if (mips_opts.noreorder == 0)
16774 as_bad (_("`noreorder' must be set before `nomacro'"));
16775 mips_opts.warn_about_macros = 1;
16776 }
16777 else if (strcmp (name, "gp=default") == 0)
16778 mips_opts.gp = file_mips_opts.gp;
16779 else if (strcmp (name, "fp=default") == 0)
16780 mips_opts.fp = file_mips_opts.fp;
16781 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16782 {
16783 mips_opts.isa = file_mips_opts.isa;
16784 mips_opts.arch = file_mips_opts.arch;
3315614d 16785 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16786 mips_opts.gp = file_mips_opts.gp;
16787 mips_opts.fp = file_mips_opts.fp;
16788 }
252b5132
RH
16789 else if (strcmp (name, "push") == 0)
16790 {
16791 struct mips_option_stack *s;
16792
325801bd 16793 s = XNEW (struct mips_option_stack);
252b5132
RH
16794 s->next = mips_opts_stack;
16795 s->options = mips_opts;
16796 mips_opts_stack = s;
16797 }
16798 else if (strcmp (name, "pop") == 0)
16799 {
16800 struct mips_option_stack *s;
16801
16802 s = mips_opts_stack;
16803 if (s == NULL)
16804 as_bad (_(".set pop with no .set push"));
16805 else
16806 {
16807 /* If we're changing the reorder mode we need to handle
16808 delay slots correctly. */
16809 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16810 start_noreorder ();
252b5132 16811 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16812 end_noreorder ();
252b5132
RH
16813
16814 mips_opts = s->options;
16815 mips_opts_stack = s->next;
16816 free (s);
16817 }
16818 }
22522f88
MR
16819 else
16820 {
16821 type = parse_code_option (name);
16822 if (type == OPTION_TYPE_BAD)
16823 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16824 }
919731af 16825
16826 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16827 registers based on what is supported by the arch/cpu. */
22522f88 16828 if (type == OPTION_TYPE_ISA)
e6559e01 16829 {
919731af 16830 switch (mips_opts.isa)
16831 {
16832 case 0:
16833 break;
16834 case ISA_MIPS1:
351cdf24
MF
16835 /* MIPS I cannot support FPXX. */
16836 mips_opts.fp = 32;
16837 /* fall-through. */
919731af 16838 case ISA_MIPS2:
16839 case ISA_MIPS32:
16840 case ISA_MIPS32R2:
16841 case ISA_MIPS32R3:
16842 case ISA_MIPS32R5:
16843 mips_opts.gp = 32;
351cdf24
MF
16844 if (mips_opts.fp != 0)
16845 mips_opts.fp = 32;
919731af 16846 break;
7361da2c
AB
16847 case ISA_MIPS32R6:
16848 mips_opts.gp = 32;
16849 mips_opts.fp = 64;
16850 break;
919731af 16851 case ISA_MIPS3:
16852 case ISA_MIPS4:
16853 case ISA_MIPS5:
16854 case ISA_MIPS64:
16855 case ISA_MIPS64R2:
16856 case ISA_MIPS64R3:
16857 case ISA_MIPS64R5:
7361da2c 16858 case ISA_MIPS64R6:
919731af 16859 mips_opts.gp = 64;
351cdf24
MF
16860 if (mips_opts.fp != 0)
16861 {
16862 if (mips_opts.arch == CPU_R5900)
16863 mips_opts.fp = 32;
16864 else
16865 mips_opts.fp = 64;
16866 }
919731af 16867 break;
16868 default:
16869 as_bad (_("unknown ISA level %s"), name + 4);
16870 break;
16871 }
e6559e01 16872 }
919731af 16873
5b7c81bd 16874 mips_check_options (&mips_opts, false);
919731af 16875
16876 mips_check_isa_supports_ases ();
16877 *input_line_pointer = ch;
16878 demand_empty_rest_of_line ();
16879}
16880
16881/* Handle the .module pseudo-op. */
16882
16883static void
16884s_module (int ignore ATTRIBUTE_UNUSED)
16885{
16886 char *name = input_line_pointer, ch;
16887
16888 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16889 ++input_line_pointer;
16890 ch = *input_line_pointer;
16891 *input_line_pointer = '\0';
16892
16893 if (!file_mips_opts_checked)
252b5132 16894 {
22522f88 16895 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16896 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16897
16898 /* Update module level settings from mips_opts. */
16899 file_mips_opts = mips_opts;
252b5132 16900 }
919731af 16901 else
16902 as_bad (_(".module is not permitted after generating code"));
16903
252b5132
RH
16904 *input_line_pointer = ch;
16905 demand_empty_rest_of_line ();
16906}
16907
16908/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16909 .option pic2. It means to generate SVR4 PIC calls. */
16910
16911static void
17a2f251 16912s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16913{
16914 mips_pic = SVR4_PIC;
5b7c81bd 16915 mips_abicalls = true;
4d0d148d
TS
16916
16917 if (g_switch_seen && g_switch_value != 0)
16918 as_warn (_("-G may not be used with SVR4 PIC code"));
16919 g_switch_value = 0;
16920
252b5132
RH
16921 bfd_set_gp_size (stdoutput, 0);
16922 demand_empty_rest_of_line ();
16923}
16924
16925/* Handle the .cpload pseudo-op. This is used when generating SVR4
16926 PIC code. It sets the $gp register for the function based on the
16927 function address, which is in the register named in the argument.
16928 This uses a relocation against _gp_disp, which is handled specially
16929 by the linker. The result is:
16930 lui $gp,%hi(_gp_disp)
16931 addiu $gp,$gp,%lo(_gp_disp)
16932 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16933 The .cpload argument is normally $25 == $t9.
16934
16935 The -mno-shared option changes this to:
bbe506e8
TS
16936 lui $gp,%hi(__gnu_local_gp)
16937 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16938 and the argument is ignored. This saves an instruction, but the
16939 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16940 address for __gnu_local_gp. Thus code assembled with -mno-shared
16941 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16942
16943static void
17a2f251 16944s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16945{
16946 expressionS ex;
aa6975fb
ILT
16947 int reg;
16948 int in_shared;
252b5132 16949
919731af 16950 file_mips_check_options ();
16951
6478892d
TS
16952 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16953 .cpload is ignored. */
16954 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16955 {
16956 s_ignore (0);
16957 return;
16958 }
16959
a276b80c
MR
16960 if (mips_opts.mips16)
16961 {
16962 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16963 ignore_rest_of_line ();
16964 return;
16965 }
16966
d3ecfc59 16967 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16968 if (mips_opts.noreorder == 0)
16969 as_warn (_(".cpload not in noreorder section"));
16970
aa6975fb
ILT
16971 reg = tc_get_register (0);
16972
16973 /* If we need to produce a 64-bit address, we are better off using
16974 the default instruction sequence. */
aed1a261 16975 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16976
252b5132 16977 ex.X_op = O_symbol;
bbe506e8
TS
16978 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16979 "__gnu_local_gp");
252b5132
RH
16980 ex.X_op_symbol = NULL;
16981 ex.X_add_number = 0;
16982
16983 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16984 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16985
8a75745d 16986 mips_mark_labels ();
5b7c81bd 16987 mips_assembling_insn = true;
8a75745d 16988
584892a6 16989 macro_start ();
67c0d1eb
RS
16990 macro_build_lui (&ex, mips_gp_register);
16991 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16992 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16993 if (in_shared)
16994 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16995 mips_gp_register, reg);
584892a6 16996 macro_end ();
252b5132 16997
5b7c81bd 16998 mips_assembling_insn = false;
252b5132
RH
16999 demand_empty_rest_of_line ();
17000}
17001
6478892d
TS
17002/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
17003 .cpsetup $reg1, offset|$reg2, label
17004
17005 If offset is given, this results in:
17006 sd $gp, offset($sp)
956cd1d6 17007 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17008 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17009 daddu $gp, $gp, $reg1
6478892d
TS
17010
17011 If $reg2 is given, this results in:
40fc1451 17012 or $reg2, $gp, $0
956cd1d6 17013 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17014 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17015 daddu $gp, $gp, $reg1
aa6975fb
ILT
17016 $reg1 is normally $25 == $t9.
17017
17018 The -mno-shared option replaces the last three instructions with
17019 lui $gp,%hi(_gp)
54f4ddb3 17020 addiu $gp,$gp,%lo(_gp) */
aa6975fb 17021
6478892d 17022static void
17a2f251 17023s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17024{
17025 expressionS ex_off;
17026 expressionS ex_sym;
17027 int reg1;
6478892d 17028
919731af 17029 file_mips_check_options ();
17030
8586fc66 17031 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
17032 We also need NewABI support. */
17033 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17034 {
17035 s_ignore (0);
17036 return;
17037 }
17038
a276b80c
MR
17039 if (mips_opts.mips16)
17040 {
17041 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17042 ignore_rest_of_line ();
17043 return;
17044 }
17045
6478892d
TS
17046 reg1 = tc_get_register (0);
17047 SKIP_WHITESPACE ();
17048 if (*input_line_pointer != ',')
17049 {
17050 as_bad (_("missing argument separator ',' for .cpsetup"));
17051 return;
17052 }
17053 else
80245285 17054 ++input_line_pointer;
6478892d
TS
17055 SKIP_WHITESPACE ();
17056 if (*input_line_pointer == '$')
80245285
TS
17057 {
17058 mips_cpreturn_register = tc_get_register (0);
17059 mips_cpreturn_offset = -1;
17060 }
6478892d 17061 else
80245285
TS
17062 {
17063 mips_cpreturn_offset = get_absolute_expression ();
17064 mips_cpreturn_register = -1;
17065 }
6478892d
TS
17066 SKIP_WHITESPACE ();
17067 if (*input_line_pointer != ',')
17068 {
17069 as_bad (_("missing argument separator ',' for .cpsetup"));
17070 return;
17071 }
17072 else
f9419b05 17073 ++input_line_pointer;
6478892d 17074 SKIP_WHITESPACE ();
f21f8242 17075 expression (&ex_sym);
6478892d 17076
8a75745d 17077 mips_mark_labels ();
5b7c81bd 17078 mips_assembling_insn = true;
8a75745d 17079
584892a6 17080 macro_start ();
6478892d
TS
17081 if (mips_cpreturn_register == -1)
17082 {
17083 ex_off.X_op = O_constant;
17084 ex_off.X_add_symbol = NULL;
17085 ex_off.X_op_symbol = NULL;
17086 ex_off.X_add_number = mips_cpreturn_offset;
17087
67c0d1eb 17088 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 17089 BFD_RELOC_LO16, SP);
6478892d
TS
17090 }
17091 else
40fc1451 17092 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 17093
aed1a261 17094 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 17095 {
df58fc94 17096 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
17097 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17098 BFD_RELOC_HI16_S);
17099
17100 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17101 mips_gp_register, -1, BFD_RELOC_GPREL16,
17102 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17103
17104 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17105 mips_gp_register, reg1);
17106 }
17107 else
17108 {
17109 expressionS ex;
17110
17111 ex.X_op = O_symbol;
4184909a 17112 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
17113 ex.X_op_symbol = NULL;
17114 ex.X_add_number = 0;
6e1304d8 17115
aa6975fb
ILT
17116 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17117 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17118
17119 macro_build_lui (&ex, mips_gp_register);
17120 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17121 mips_gp_register, BFD_RELOC_LO16);
17122 }
f21f8242 17123
584892a6 17124 macro_end ();
6478892d 17125
5b7c81bd 17126 mips_assembling_insn = false;
6478892d
TS
17127 demand_empty_rest_of_line ();
17128}
17129
17130static void
17a2f251 17131s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17132{
919731af 17133 file_mips_check_options ();
17134
6478892d 17135 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17136 .cplocal is ignored. */
6478892d
TS
17137 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17138 {
17139 s_ignore (0);
17140 return;
17141 }
17142
a276b80c
MR
17143 if (mips_opts.mips16)
17144 {
17145 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17146 ignore_rest_of_line ();
17147 return;
17148 }
17149
6478892d 17150 mips_gp_register = tc_get_register (0);
85b51719 17151 demand_empty_rest_of_line ();
6478892d
TS
17152}
17153
252b5132
RH
17154/* Handle the .cprestore pseudo-op. This stores $gp into a given
17155 offset from $sp. The offset is remembered, and after making a PIC
17156 call $gp is restored from that location. */
17157
17158static void
17a2f251 17159s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17160{
17161 expressionS ex;
252b5132 17162
919731af 17163 file_mips_check_options ();
17164
6478892d 17165 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17166 .cprestore is ignored. */
6478892d 17167 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17168 {
17169 s_ignore (0);
17170 return;
17171 }
17172
a276b80c
MR
17173 if (mips_opts.mips16)
17174 {
17175 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17176 ignore_rest_of_line ();
17177 return;
17178 }
17179
252b5132 17180 mips_cprestore_offset = get_absolute_expression ();
7a621144 17181 mips_cprestore_valid = 1;
252b5132
RH
17182
17183 ex.X_op = O_constant;
17184 ex.X_add_symbol = NULL;
17185 ex.X_op_symbol = NULL;
17186 ex.X_add_number = mips_cprestore_offset;
17187
8a75745d 17188 mips_mark_labels ();
5b7c81bd 17189 mips_assembling_insn = true;
8a75745d 17190
584892a6 17191 macro_start ();
67c0d1eb
RS
17192 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17193 SP, HAVE_64BIT_ADDRESSES);
584892a6 17194 macro_end ();
252b5132 17195
5b7c81bd 17196 mips_assembling_insn = false;
252b5132
RH
17197 demand_empty_rest_of_line ();
17198}
17199
6478892d 17200/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17201 was given in the preceding .cpsetup, it results in:
6478892d 17202 ld $gp, offset($sp)
76b3015f 17203
6478892d 17204 If a register $reg2 was given there, it results in:
40fc1451 17205 or $gp, $reg2, $0 */
54f4ddb3 17206
6478892d 17207static void
17a2f251 17208s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17209{
17210 expressionS ex;
6478892d 17211
919731af 17212 file_mips_check_options ();
17213
6478892d
TS
17214 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17215 We also need NewABI support. */
17216 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17217 {
17218 s_ignore (0);
17219 return;
17220 }
17221
a276b80c
MR
17222 if (mips_opts.mips16)
17223 {
17224 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17225 ignore_rest_of_line ();
17226 return;
17227 }
17228
8a75745d 17229 mips_mark_labels ();
5b7c81bd 17230 mips_assembling_insn = true;
8a75745d 17231
584892a6 17232 macro_start ();
6478892d
TS
17233 if (mips_cpreturn_register == -1)
17234 {
17235 ex.X_op = O_constant;
17236 ex.X_add_symbol = NULL;
17237 ex.X_op_symbol = NULL;
17238 ex.X_add_number = mips_cpreturn_offset;
17239
67c0d1eb 17240 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17241 }
17242 else
40fc1451
SD
17243 move_register (mips_gp_register, mips_cpreturn_register);
17244
584892a6 17245 macro_end ();
6478892d 17246
5b7c81bd 17247 mips_assembling_insn = false;
6478892d
TS
17248 demand_empty_rest_of_line ();
17249}
17250
d0f13682
CLT
17251/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17252 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17253 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17254 debug information or MIPS16 TLS. */
741d6ea8
JM
17255
17256static void
d0f13682
CLT
17257s_tls_rel_directive (const size_t bytes, const char *dirstr,
17258 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17259{
17260 expressionS ex;
17261 char *p;
17262
17263 expression (&ex);
17264
17265 if (ex.X_op != O_symbol)
17266 {
1661c76c 17267 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17268 ignore_rest_of_line ();
17269 }
17270
17271 p = frag_more (bytes);
17272 md_number_to_chars (p, 0, bytes);
5b7c81bd 17273 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false, rtype);
741d6ea8 17274 demand_empty_rest_of_line ();
de64cffd 17275 mips_clear_insn_labels ();
741d6ea8
JM
17276}
17277
17278/* Handle .dtprelword. */
17279
17280static void
17281s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17282{
d0f13682 17283 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17284}
17285
17286/* Handle .dtpreldword. */
17287
17288static void
17289s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17290{
d0f13682
CLT
17291 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17292}
17293
17294/* Handle .tprelword. */
17295
17296static void
17297s_tprelword (int ignore ATTRIBUTE_UNUSED)
17298{
17299 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17300}
17301
17302/* Handle .tpreldword. */
17303
17304static void
17305s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17306{
17307 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17308}
17309
6478892d
TS
17310/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17311 code. It sets the offset to use in gp_rel relocations. */
17312
17313static void
17a2f251 17314s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17315{
17316 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17317 We also need NewABI support. */
17318 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17319 {
17320 s_ignore (0);
17321 return;
17322 }
17323
def2e0dd 17324 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17325
17326 demand_empty_rest_of_line ();
17327}
17328
252b5132
RH
17329/* Handle the .gpword pseudo-op. This is used when generating PIC
17330 code. It generates a 32 bit GP relative reloc. */
17331
17332static void
17a2f251 17333s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17334{
a8dbcb85
TS
17335 segment_info_type *si;
17336 struct insn_label_list *l;
252b5132
RH
17337 expressionS ex;
17338 char *p;
17339
17340 /* When not generating PIC code, this is treated as .word. */
17341 if (mips_pic != SVR4_PIC)
17342 {
17343 s_cons (2);
17344 return;
17345 }
17346
a8dbcb85
TS
17347 si = seg_info (now_seg);
17348 l = si->label_list;
7d10b47d 17349 mips_emit_delays ();
252b5132 17350 if (auto_align)
462427c4 17351 mips_align (2, 0, l);
252b5132
RH
17352
17353 expression (&ex);
a1facbec 17354 mips_clear_insn_labels ();
252b5132
RH
17355
17356 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17357 {
1661c76c 17358 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17359 ignore_rest_of_line ();
17360 }
17361
17362 p = frag_more (4);
17a2f251 17363 md_number_to_chars (p, 0, 4);
5b7c81bd 17364 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 17365 BFD_RELOC_GPREL32);
252b5132
RH
17366
17367 demand_empty_rest_of_line ();
17368}
17369
10181a0d 17370static void
17a2f251 17371s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17372{
a8dbcb85
TS
17373 segment_info_type *si;
17374 struct insn_label_list *l;
10181a0d
AO
17375 expressionS ex;
17376 char *p;
17377
17378 /* When not generating PIC code, this is treated as .dword. */
17379 if (mips_pic != SVR4_PIC)
17380 {
17381 s_cons (3);
17382 return;
17383 }
17384
a8dbcb85
TS
17385 si = seg_info (now_seg);
17386 l = si->label_list;
7d10b47d 17387 mips_emit_delays ();
10181a0d 17388 if (auto_align)
462427c4 17389 mips_align (3, 0, l);
10181a0d
AO
17390
17391 expression (&ex);
a1facbec 17392 mips_clear_insn_labels ();
10181a0d
AO
17393
17394 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17395 {
1661c76c 17396 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17397 ignore_rest_of_line ();
17398 }
17399
17400 p = frag_more (8);
17a2f251 17401 md_number_to_chars (p, 0, 8);
5b7c81bd 17402 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
6e1304d8 17403 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17404
17405 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8 17406 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
5b7c81bd 17407 false, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17408
17409 demand_empty_rest_of_line ();
17410}
17411
a3f278e2
CM
17412/* Handle the .ehword pseudo-op. This is used when generating unwinding
17413 tables. It generates a R_MIPS_EH reloc. */
17414
17415static void
17416s_ehword (int ignore ATTRIBUTE_UNUSED)
17417{
17418 expressionS ex;
17419 char *p;
17420
17421 mips_emit_delays ();
17422
17423 expression (&ex);
17424 mips_clear_insn_labels ();
17425
17426 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17427 {
1661c76c 17428 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17429 ignore_rest_of_line ();
17430 }
17431
17432 p = frag_more (4);
17433 md_number_to_chars (p, 0, 4);
5b7c81bd 17434 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
2f0c68f2 17435 BFD_RELOC_32_PCREL);
a3f278e2
CM
17436
17437 demand_empty_rest_of_line ();
17438}
17439
252b5132
RH
17440/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17441 tables in SVR4 PIC code. */
17442
17443static void
17a2f251 17444s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17445{
252b5132
RH
17446 int reg;
17447
919731af 17448 file_mips_check_options ();
17449
10181a0d
AO
17450 /* This is ignored when not generating SVR4 PIC code. */
17451 if (mips_pic != SVR4_PIC)
252b5132
RH
17452 {
17453 s_ignore (0);
17454 return;
17455 }
17456
8a75745d 17457 mips_mark_labels ();
5b7c81bd 17458 mips_assembling_insn = true;
8a75745d 17459
252b5132 17460 /* Add $gp to the register named as an argument. */
584892a6 17461 macro_start ();
252b5132 17462 reg = tc_get_register (0);
67c0d1eb 17463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17464 macro_end ();
252b5132 17465
5b7c81bd 17466 mips_assembling_insn = false;
bdaaa2e1 17467 demand_empty_rest_of_line ();
252b5132
RH
17468}
17469
17470/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17471 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17472 such as generating jalx instructions when needed. We also make
17473 them odd for the duration of the assembly, in order to generate the
17474 right sort of code. We will make them even in the adjust_symtab
17475 routine, while leaving them marked. This is convenient for the
17476 debugger and the disassembler. The linker knows to make them odd
17477 again. */
17478
17479static void
17a2f251 17480s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17481{
7bb01e2d
MR
17482 file_mips_check_options ();
17483 file_ase_mips16 |= mips_opts.mips16;
17484 file_ase_micromips |= mips_opts.micromips;
17485
df58fc94 17486 mips_mark_labels ();
252b5132
RH
17487
17488 demand_empty_rest_of_line ();
17489}
17490
ba92f887
MR
17491/* Handle the .nan pseudo-op. */
17492
17493static void
17494s_nan (int ignore ATTRIBUTE_UNUSED)
17495{
17496 static const char str_legacy[] = "legacy";
17497 static const char str_2008[] = "2008";
17498 size_t i;
17499
17500 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17501
17502 if (i == sizeof (str_2008) - 1
17503 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17504 mips_nan2008 = 1;
ba92f887
MR
17505 else if (i == sizeof (str_legacy) - 1
17506 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17507 {
17508 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17509 mips_nan2008 = 0;
17510 else
17511 as_bad (_("`%s' does not support legacy NaN"),
17512 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17513 }
ba92f887 17514 else
1661c76c 17515 as_bad (_("bad .nan directive"));
ba92f887
MR
17516
17517 input_line_pointer += i;
17518 demand_empty_rest_of_line ();
17519}
17520
754e2bb9
RS
17521/* Handle a .stab[snd] directive. Ideally these directives would be
17522 implemented in a transparent way, so that removing them would not
17523 have any effect on the generated instructions. However, s_stab
17524 internally changes the section, so in practice we need to decide
17525 now whether the preceding label marks compressed code. We do not
17526 support changing the compression mode of a label after a .stab*
17527 directive, such as in:
17528
17529 foo:
134c0c8b 17530 .stabs ...
754e2bb9
RS
17531 .set mips16
17532
17533 so the current mode wins. */
252b5132
RH
17534
17535static void
17a2f251 17536s_mips_stab (int type)
252b5132 17537{
42c0794e 17538 file_mips_check_options ();
754e2bb9 17539 mips_mark_labels ();
252b5132
RH
17540 s_stab (type);
17541}
17542
54f4ddb3 17543/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17544
17545static void
17a2f251 17546s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17547{
17548 char *name;
17549 int c;
17550 symbolS *symbolP;
17551 expressionS exp;
17552
d02603dc 17553 c = get_symbol_name (&name);
252b5132
RH
17554 symbolP = symbol_find_or_make (name);
17555 S_SET_WEAK (symbolP);
17556 *input_line_pointer = c;
17557
d02603dc 17558 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17559
17560 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17561 {
17562 if (S_IS_DEFINED (symbolP))
17563 {
20203fb9 17564 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17565 S_GET_NAME (symbolP));
17566 ignore_rest_of_line ();
17567 return;
17568 }
bdaaa2e1 17569
252b5132
RH
17570 if (*input_line_pointer == ',')
17571 {
17572 ++input_line_pointer;
17573 SKIP_WHITESPACE ();
17574 }
bdaaa2e1 17575
252b5132
RH
17576 expression (&exp);
17577 if (exp.X_op != O_symbol)
17578 {
20203fb9 17579 as_bad (_("bad .weakext directive"));
98d3f06f 17580 ignore_rest_of_line ();
252b5132
RH
17581 return;
17582 }
49309057 17583 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17584 }
17585
17586 demand_empty_rest_of_line ();
17587}
17588
17589/* Parse a register string into a number. Called from the ECOFF code
17590 to parse .frame. The argument is non-zero if this is the frame
17591 register, so that we can record it in mips_frame_reg. */
17592
17593int
17a2f251 17594tc_get_register (int frame)
252b5132 17595{
707bfff6 17596 unsigned int reg;
252b5132
RH
17597
17598 SKIP_WHITESPACE ();
707bfff6
TS
17599 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17600 reg = 0;
252b5132 17601 if (frame)
7a621144
DJ
17602 {
17603 mips_frame_reg = reg != 0 ? reg : SP;
17604 mips_frame_reg_valid = 1;
17605 mips_cprestore_valid = 0;
17606 }
252b5132
RH
17607 return reg;
17608}
17609
17610valueT
17a2f251 17611md_section_align (asection *seg, valueT addr)
252b5132 17612{
fd361982 17613 int align = bfd_section_alignment (seg);
252b5132 17614
f3ded42a
RS
17615 /* We don't need to align ELF sections to the full alignment.
17616 However, Irix 5 may prefer that we align them at least to a 16
17617 byte boundary. We don't bother to align the sections if we
17618 are targeted for an embedded system. */
d34049e8 17619 if (startswith (TARGET_OS, "elf"))
f3ded42a
RS
17620 return addr;
17621 if (align > 4)
17622 align = 4;
252b5132 17623
8d3842cd 17624 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17625}
17626
17627/* Utility routine, called from above as well. If called while the
17628 input file is still being read, it's only an approximation. (For
17629 example, a symbol may later become defined which appeared to be
17630 undefined earlier.) */
17631
17632static int
17a2f251 17633nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17634{
17635 if (sym == 0)
17636 return 0;
17637
4d0d148d 17638 if (g_switch_value > 0)
252b5132
RH
17639 {
17640 const char *symname;
17641 int change;
17642
c9914766 17643 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17644 register. It can be if it is smaller than the -G size or if
17645 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17646 not be referenced off the $gp, although it appears as though
252b5132
RH
17647 they can. */
17648 symname = S_GET_NAME (sym);
17649 if (symname != (const char *) NULL
17650 && (strcmp (symname, "eprol") == 0
17651 || strcmp (symname, "etext") == 0
17652 || strcmp (symname, "_gp") == 0
17653 || strcmp (symname, "edata") == 0
17654 || strcmp (symname, "_fbss") == 0
17655 || strcmp (symname, "_fdata") == 0
17656 || strcmp (symname, "_ftext") == 0
17657 || strcmp (symname, "end") == 0
17658 || strcmp (symname, "_gp_disp") == 0))
17659 change = 1;
17660 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17661 && (0
17662#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17663 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17664 && (symbol_get_obj (sym)->ecoff_extern_size
17665 <= g_switch_value))
252b5132
RH
17666#endif
17667 /* We must defer this decision until after the whole
17668 file has been read, since there might be a .extern
17669 after the first use of this symbol. */
17670 || (before_relaxing
17671#ifndef NO_ECOFF_DEBUGGING
49309057 17672 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17673#endif
17674 && S_GET_VALUE (sym) == 0)
17675 || (S_GET_VALUE (sym) != 0
17676 && S_GET_VALUE (sym) <= g_switch_value)))
17677 change = 0;
17678 else
17679 {
17680 const char *segname;
17681
17682 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17683 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17684 && strcmp (segname, ".lit4") != 0);
17685 change = (strcmp (segname, ".sdata") != 0
fba2b7f9 17686 && strcmp (segname, ".sbss") != 0
d34049e8
ML
17687 && !startswith (segname, ".sdata.")
17688 && !startswith (segname, ".sbss.")
17689 && !startswith (segname, ".gnu.linkonce.sb.")
17690 && !startswith (segname, ".gnu.linkonce.s."));
252b5132
RH
17691 }
17692 return change;
17693 }
17694 else
c9914766 17695 /* We are not optimizing for the $gp register. */
252b5132
RH
17696 return 1;
17697}
17698
5919d012
RS
17699
17700/* Return true if the given symbol should be considered local for SVR4 PIC. */
17701
5b7c81bd 17702static bool
9e009953 17703pic_need_relax (symbolS *sym)
5919d012
RS
17704{
17705 asection *symsec;
5919d012 17706
249d1bad
AM
17707 if (!sym)
17708 return false;
17709
5919d012
RS
17710 /* Handle the case of a symbol equated to another symbol. */
17711 while (symbol_equated_reloc_p (sym))
17712 {
17713 symbolS *n;
17714
5f0fe04b 17715 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17716 n = symbol_get_value_expression (sym)->X_add_symbol;
17717 if (n == sym)
17718 break;
17719 sym = n;
17720 }
17721
df1f3cda 17722 if (symbol_section_p (sym))
5b7c81bd 17723 return true;
df1f3cda 17724
5919d012
RS
17725 symsec = S_GET_SEGMENT (sym);
17726
5919d012 17727 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17728 return (!bfd_is_und_section (symsec)
17729 && !bfd_is_abs_section (symsec)
5f0fe04b 17730 && !bfd_is_com_section (symsec)
5919d012 17731 /* A global or weak symbol is treated as external. */
f3ded42a 17732 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17733}
14f72d45
MR
17734\f
17735/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17736 convert a section-relative value VAL to the equivalent PC-relative
17737 value. */
17738
17739static offsetT
17740mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17741 offsetT val, long stretch)
17742{
17743 fragS *sym_frag;
17744 addressT addr;
17745
17746 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17747
17748 sym_frag = symbol_get_frag (fragp->fr_symbol);
17749
17750 /* If the relax_marker of the symbol fragment differs from the
17751 relax_marker of this fragment, we have not yet adjusted the
17752 symbol fragment fr_address. We want to add in STRETCH in
17753 order to get a better estimate of the address. This
17754 particularly matters because of the shift bits. */
17755 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17756 {
17757 fragS *f;
17758
17759 /* Adjust stretch for any alignment frag. Note that if have
17760 been expanding the earlier code, the symbol may be
17761 defined in what appears to be an earlier frag. FIXME:
17762 This doesn't handle the fr_subtype field, which specifies
17763 a maximum number of bytes to skip when doing an
17764 alignment. */
17765 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17766 {
17767 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17768 {
17769 if (stretch < 0)
17770 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17771 else
17772 stretch &= ~((1 << (int) f->fr_offset) - 1);
17773 if (stretch == 0)
17774 break;
17775 }
17776 }
17777 if (f != NULL)
17778 val += stretch;
17779 }
17780
17781 addr = fragp->fr_address + fragp->fr_fix;
17782
17783 /* The base address rules are complicated. The base address of
17784 a branch is the following instruction. The base address of a
17785 PC relative load or add is the instruction itself, but if it
17786 is in a delay slot (in which case it can not be extended) use
17787 the address of the instruction whose delay slot it is in. */
17788 if (pcrel_op->include_isa_bit)
17789 {
17790 addr += 2;
17791
17792 /* If we are currently assuming that this frag should be
17793 extended, then the current address is two bytes higher. */
17794 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17795 addr += 2;
17796
17797 /* Ignore the low bit in the target, since it will be set
17798 for a text label. */
17799 val &= -2;
17800 }
17801 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17802 addr -= 4;
17803 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17804 addr -= 2;
5919d012 17805
14f72d45
MR
17806 val -= addr & -(1 << pcrel_op->align_log2);
17807
17808 return val;
17809}
5919d012 17810
252b5132
RH
17811/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17812 extended opcode. SEC is the section the frag is in. */
17813
17814static int
17a2f251 17815mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17816{
3ccad066 17817 const struct mips_int_operand *operand;
252b5132 17818 offsetT val;
252b5132 17819 segT symsec;
14f72d45 17820 int type;
252b5132
RH
17821
17822 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17823 return 0;
17824 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17825 return 1;
17826
88a7ef16 17827 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17828 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
5b7c81bd
AM
17829 operand = mips16_immed_operand (type, false);
17830 if (S_FORCE_RELOC (fragp->fr_symbol, true)
88a7ef16
MR
17831 || (operand->root.type == OP_PCREL
17832 ? sec != symsec
17833 : !bfd_is_abs_section (symsec)))
17834 return 1;
252b5132 17835
88a7ef16 17836 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17837
3ccad066 17838 if (operand->root.type == OP_PCREL)
252b5132 17839 {
3ccad066 17840 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17841 offsetT maxtiny;
252b5132 17842
1425c41d 17843 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17844 return 1;
252b5132 17845
88a7ef16 17846 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17847 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17848
17849 /* If any of the shifted bits are set, we must use an extended
17850 opcode. If the address depends on the size of this
17851 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17852 use an extended opcode. */
17853 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17854 {
17855 fragp->fr_subtype =
1425c41d 17856 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17857 return 1;
17858 }
17859
17860 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17861 is precisely the next value above maxtiny, then there is a
17862 chance of an infinite loop as in the following code:
252b5132
RH
17863 la $4,foo
17864 .skip 1020
17865 .align 2
17866 foo:
17867 In this case when the la is extended, foo is 0x3fc bytes
17868 away, so the la can be shrunk, but then foo is 0x400 away, so
17869 the la must be extended. To avoid this loop, we mark the
17870 frag as extended if it was small, and is about to become
3ccad066
RS
17871 extended with the next value above maxtiny. */
17872 maxtiny = mips_int_operand_max (operand);
17873 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17874 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17875 {
17876 fragp->fr_subtype =
1425c41d 17877 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17878 return 1;
17879 }
17880 }
252b5132 17881
3ccad066 17882 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17883}
17884
8507b6e7
MR
17885/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17886 macro expansion. SEC is the section the frag is in. We only
17887 support PC-relative instructions (LA, DLA, LW, LD) here, in
17888 non-PIC code using 32-bit addressing. */
17889
17890static int
17891mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17892{
17893 const struct mips_pcrel_operand *pcrel_op;
17894 const struct mips_int_operand *operand;
17895 offsetT val;
17896 segT symsec;
17897 int type;
17898
17899 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17900
17901 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17902 return 0;
17903 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17904 return 0;
17905
17906 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17907 switch (type)
17908 {
17909 case 'A':
17910 case 'B':
17911 case 'E':
17912 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17913 if (bfd_is_abs_section (symsec))
17914 return 1;
17915 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17916 return 0;
5b7c81bd 17917 if (S_FORCE_RELOC (fragp->fr_symbol, true) || sec != symsec)
8507b6e7
MR
17918 return 1;
17919
5b7c81bd 17920 operand = mips16_immed_operand (type, true);
8507b6e7
MR
17921 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17922 pcrel_op = (const struct mips_pcrel_operand *) operand;
17923 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17924
17925 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17926
17927 default:
17928 return 0;
17929 }
17930}
17931
4a6a3df4
AO
17932/* Compute the length of a branch sequence, and adjust the
17933 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17934 worst-case length is computed, with UPDATE being used to indicate
17935 whether an unconditional (-1), branch-likely (+1) or regular (0)
17936 branch is to be computed. */
17937static int
17a2f251 17938relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17939{
5b7c81bd 17940 bool toofar;
4a6a3df4
AO
17941 int length;
17942
17943 if (fragp
17944 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17945 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17946 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17947 {
17948 addressT addr;
17949 offsetT val;
17950
17951 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17952
17953 addr = fragp->fr_address + fragp->fr_fix + 4;
17954
17955 val -= addr;
17956
17957 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17958 }
4a6a3df4 17959 else
c1f61bd2
MR
17960 /* If the symbol is not defined or it's in a different segment,
17961 we emit the long sequence. */
5b7c81bd 17962 toofar = true;
4a6a3df4
AO
17963
17964 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17965 fragp->fr_subtype
66b3e8da 17966 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17967 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17968 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17969 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17970 RELAX_BRANCH_LINK (fragp->fr_subtype),
17971 toofar);
17972
17973 length = 4;
17974 if (toofar)
17975 {
17976 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17977 length += 8;
17978
ce8ad872 17979 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17980 {
17981 /* Additional space for PIC loading of target address. */
17982 length += 8;
17983 if (mips_opts.isa == ISA_MIPS1)
17984 /* Additional space for $at-stabilizing nop. */
17985 length += 4;
17986 }
17987
17988 /* If branch is conditional. */
17989 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17990 length += 8;
17991 }
b34976b6 17992
4a6a3df4
AO
17993 return length;
17994}
17995
7bd374a4
MR
17996/* Get a FRAG's branch instruction delay slot size, either from the
17997 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17998 or SHORT_INSN_SIZE otherwise. */
17999
18000static int
5b7c81bd 18001frag_branch_delay_slot_size (fragS *fragp, bool al, int short_insn_size)
7bd374a4
MR
18002{
18003 char *buf = fragp->fr_literal + fragp->fr_fix;
18004
18005 if (al)
18006 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18007 else
18008 return short_insn_size;
18009}
18010
df58fc94
RS
18011/* Compute the length of a branch sequence, and adjust the
18012 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18013 worst-case length is computed, with UPDATE being used to indicate
18014 whether an unconditional (-1), or regular (0) branch is to be
18015 computed. */
18016
18017static int
18018relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18019{
5b7c81bd
AM
18020 bool insn32 = true;
18021 bool nods = true;
18022 bool pic = true;
18023 bool al = true;
7bd374a4 18024 int short_insn_size;
5b7c81bd 18025 bool toofar;
df58fc94
RS
18026 int length;
18027
7bd374a4
MR
18028 if (fragp)
18029 {
18030 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18031 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18032 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
18033 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18034 }
18035 short_insn_size = insn32 ? 4 : 2;
18036
df58fc94
RS
18037 if (fragp
18038 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18039 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18040 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18041 {
18042 addressT addr;
18043 offsetT val;
18044
18045 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18046 /* Ignore the low bit in the target, since it will be set
18047 for a text label. */
18048 if ((val & 1) != 0)
18049 --val;
18050
18051 addr = fragp->fr_address + fragp->fr_fix + 4;
18052
18053 val -= addr;
18054
18055 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18056 }
df58fc94 18057 else
c1f61bd2
MR
18058 /* If the symbol is not defined or it's in a different segment,
18059 we emit the long sequence. */
5b7c81bd 18060 toofar = true;
df58fc94
RS
18061
18062 if (fragp && update
18063 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18064 fragp->fr_subtype = (toofar
18065 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18066 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18067
18068 length = 4;
18069 if (toofar)
18070 {
5b7c81bd
AM
18071 bool compact_known = fragp != NULL;
18072 bool compact = false;
18073 bool uncond;
df58fc94 18074
df58fc94 18075 if (fragp)
8484fb75
MR
18076 {
18077 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18078 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 18079 }
df58fc94
RS
18080 else
18081 uncond = update < 0;
18082
18083 /* If label is out of range, we turn branch <br>:
18084
18085 <br> label # 4 bytes
18086 0:
18087
18088 into:
18089
18090 j label # 4 bytes
8484fb75
MR
18091 nop # 2/4 bytes if
18092 # compact && (!PIC || insn32)
df58fc94
RS
18093 0:
18094 */
ce8ad872 18095 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 18096 length += short_insn_size;
df58fc94
RS
18097
18098 /* If assembling PIC code, we further turn:
18099
18100 j label # 4 bytes
18101
18102 into:
18103
18104 lw/ld at, %got(label)(gp) # 4 bytes
18105 d/addiu at, %lo(label) # 4 bytes
8484fb75 18106 jr/c at # 2/4 bytes
df58fc94 18107 */
ce8ad872 18108 if (pic)
8484fb75 18109 length += 4 + short_insn_size;
df58fc94 18110
7bd374a4
MR
18111 /* Add an extra nop if the jump has no compact form and we need
18112 to fill the delay slot. */
ce8ad872 18113 if ((!pic || al) && nods)
7bd374a4
MR
18114 length += (fragp
18115 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18116 : short_insn_size);
18117
df58fc94
RS
18118 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18119
18120 <brneg> 0f # 4 bytes
8484fb75 18121 nop # 2/4 bytes if !compact
df58fc94
RS
18122 */
18123 if (!uncond)
8484fb75 18124 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 18125 }
7bd374a4
MR
18126 else if (nods)
18127 {
18128 /* Add an extra nop to fill the delay slot. */
18129 gas_assert (fragp);
18130 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18131 }
df58fc94
RS
18132
18133 return length;
18134}
18135
18136/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18137 bit accordingly. */
18138
18139static int
18140relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18141{
5b7c81bd 18142 bool toofar;
df58fc94 18143
df58fc94
RS
18144 if (fragp
18145 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18146 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18147 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18148 {
18149 addressT addr;
18150 offsetT val;
18151 int type;
18152
18153 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18154 /* Ignore the low bit in the target, since it will be set
18155 for a text label. */
18156 if ((val & 1) != 0)
18157 --val;
18158
18159 /* Assume this is a 2-byte branch. */
18160 addr = fragp->fr_address + fragp->fr_fix + 2;
18161
18162 /* We try to avoid the infinite loop by not adding 2 more bytes for
18163 long branches. */
18164
18165 val -= addr;
18166
18167 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18168 if (type == 'D')
18169 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18170 else if (type == 'E')
18171 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18172 else
18173 abort ();
18174 }
18175 else
18176 /* If the symbol is not defined or it's in a different segment,
18177 we emit a normal 32-bit branch. */
5b7c81bd 18178 toofar = true;
df58fc94
RS
18179
18180 if (fragp && update
18181 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18182 fragp->fr_subtype
18183 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18184 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18185
18186 if (toofar)
18187 return 4;
18188
18189 return 2;
18190}
18191
252b5132
RH
18192/* Estimate the size of a frag before relaxing. Unless this is the
18193 mips16, we are not really relaxing here, and the final size is
18194 encoded in the subtype information. For the mips16, we have to
18195 decide whether we are using an extended opcode or not. */
18196
252b5132 18197int
17a2f251 18198md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18199{
5919d012 18200 int change;
252b5132 18201
4a6a3df4
AO
18202 if (RELAX_BRANCH_P (fragp->fr_subtype))
18203 {
18204
5b7c81bd 18205 fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
b34976b6 18206
4a6a3df4
AO
18207 return fragp->fr_var;
18208 }
18209
252b5132 18210 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18211 {
18212 /* We don't want to modify the EXTENDED bit here; it might get us
18213 into infinite loops. We change it only in mips_relax_frag(). */
18214 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18215 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18216 else
18217 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18218 }
252b5132 18219
df58fc94
RS
18220 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18221 {
18222 int length = 4;
18223
18224 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
5b7c81bd 18225 length = relaxed_micromips_16bit_branch_length (fragp, segtype, false);
df58fc94 18226 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
5b7c81bd 18227 length = relaxed_micromips_32bit_branch_length (fragp, segtype, false);
df58fc94
RS
18228 fragp->fr_var = length;
18229
18230 return length;
18231 }
18232
ce8ad872 18233 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18234 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18235 change = 0;
ce8ad872
MR
18236 else if (RELAX_PIC (fragp->fr_subtype))
18237 change = pic_need_relax (fragp->fr_symbol);
252b5132 18238 else
ce8ad872 18239 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18240
18241 if (change)
18242 {
4d7206a2 18243 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18244 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18245 }
4d7206a2
RS
18246 else
18247 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18248}
18249
18250/* This is called to see whether a reloc against a defined symbol
de7e6852 18251 should be converted into a reloc against a section. */
252b5132
RH
18252
18253int
17a2f251 18254mips_fix_adjustable (fixS *fixp)
252b5132 18255{
252b5132
RH
18256 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18257 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18258 return 0;
a161fe53 18259
252b5132
RH
18260 if (fixp->fx_addsy == NULL)
18261 return 1;
a161fe53 18262
2f0c68f2
CM
18263 /* Allow relocs used for EH tables. */
18264 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18265 return 1;
18266
de7e6852
RS
18267 /* If symbol SYM is in a mergeable section, relocations of the form
18268 SYM + 0 can usually be made section-relative. The mergeable data
18269 is then identified by the section offset rather than by the symbol.
18270
18271 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18272 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18273 need to recalculate the complete offset in order to correctly identify
18274 the merge data.
18275
33eaf5de 18276 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18277 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18278 placed anywhere. Rather than break backwards compatibility by changing
18279 this, it seems better not to force the issue, and instead keep the
18280 original symbol. This will work with either linker behavior. */
738e5348 18281 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18282 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18283 && HAVE_IN_PLACE_ADDENDS
18284 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18285 return 0;
18286
97f50151
MR
18287 /* There is no place to store an in-place offset for JALR relocations. */
18288 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18289 return 0;
18290
18291 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18292 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18293 start address of the symbol's containing section.
18294
18295 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18296 section relative to allow linker relaxations to be performed later on. */
97f50151 18297 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18298 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18299 return 0;
18300
b314ec0e
RS
18301 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18302 to a floating-point stub. The same is true for non-R_MIPS16_26
18303 relocations against MIPS16 functions; in this case, the stub becomes
18304 the function's canonical address.
18305
18306 Floating-point stubs are stored in unique .mips16.call.* or
18307 .mips16.fn.* sections. If a stub T for function F is in section S,
18308 the first relocation in section S must be against F; this is how the
18309 linker determines the target function. All relocations that might
18310 resolve to T must also be against F. We therefore have the following
18311 restrictions, which are given in an intentionally-redundant way:
18312
18313 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18314 symbols.
18315
18316 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18317 if that stub might be used.
18318
18319 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18320 symbols.
18321
18322 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18323 that stub might be used.
18324
18325 There is a further restriction:
18326
df58fc94 18327 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18328 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18329 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18330 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18331 against MIPS16 or microMIPS symbols because we need to keep the
18332 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18333 detection and JAL or BAL to JALX instruction conversion in the
18334 linker.
b314ec0e 18335
df58fc94 18336 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18337 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18338 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18339
18340 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18341 relocation against some symbol R, no relocation against R may be
18342 reduced. (Note that this deals with (2) as well as (1) because
18343 relocations against global symbols will never be reduced on ELF
18344 targets.) This approach is a little simpler than trying to detect
18345 stub sections, and gives the "all or nothing" per-symbol consistency
18346 that we have for MIPS16 symbols. */
f3ded42a 18347 if (fixp->fx_subsy == NULL
30c09090 18348 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18349 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18350 && (jmp_reloc_p (fixp->fx_r_type)
18351 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18352 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18353 return 0;
a161fe53 18354
252b5132
RH
18355 return 1;
18356}
18357
18358/* Translate internal representation of relocation info to BFD target
18359 format. */
18360
18361arelent **
17a2f251 18362tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18363{
18364 static arelent *retval[4];
18365 arelent *reloc;
18366 bfd_reloc_code_real_type code;
18367
4b0cff4e 18368 memset (retval, 0, sizeof(retval));
325801bd
TS
18369 reloc = retval[0] = XCNEW (arelent);
18370 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18371 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18372 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18373
bad36eac
DJ
18374 if (fixp->fx_pcrel)
18375 {
df58fc94 18376 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18377 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18378 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18379 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18380 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18381 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18382 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18383 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18384 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18385 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18386 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18387 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18388
18389 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18390 Relocations want only the symbol offset. */
51f6035b
MR
18391 switch (fixp->fx_r_type)
18392 {
18393 case BFD_RELOC_MIPS_18_PCREL_S3:
18394 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18395 break;
18396 default:
18397 reloc->addend = fixp->fx_addnumber + reloc->address;
18398 break;
18399 }
bad36eac 18400 }
17c6c9d9
MR
18401 else if (HAVE_IN_PLACE_ADDENDS
18402 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18403 && (read_compressed_insn (fixp->fx_frag->fr_literal
18404 + fixp->fx_where, 4) >> 26) == 0x3c)
18405 {
18406 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18407 addend accordingly. */
18408 reloc->addend = fixp->fx_addnumber >> 1;
18409 }
bad36eac
DJ
18410 else
18411 reloc->addend = fixp->fx_addnumber;
252b5132 18412
438c16b8
TS
18413 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18414 entry to be used in the relocation's section offset. */
18415 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18416 {
18417 reloc->address = reloc->addend;
18418 reloc->addend = 0;
18419 }
18420
252b5132 18421 code = fixp->fx_r_type;
252b5132 18422
bad36eac 18423 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18424 if (reloc->howto == NULL)
18425 {
18426 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18427 _("cannot represent %s relocation in this object file"
18428 " format"),
252b5132
RH
18429 bfd_get_reloc_code_name (code));
18430 retval[0] = NULL;
18431 }
18432
18433 return retval;
18434}
18435
18436/* Relax a machine dependent frag. This returns the amount by which
18437 the current size of the frag should change. */
18438
18439int
17a2f251 18440mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18441{
4a6a3df4
AO
18442 if (RELAX_BRANCH_P (fragp->fr_subtype))
18443 {
18444 offsetT old_var = fragp->fr_var;
b34976b6 18445
5b7c81bd 18446 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
4a6a3df4
AO
18447
18448 return fragp->fr_var - old_var;
18449 }
18450
df58fc94
RS
18451 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18452 {
18453 offsetT old_var = fragp->fr_var;
18454 offsetT new_var = 4;
18455
18456 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
5b7c81bd 18457 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, true);
df58fc94 18458 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
5b7c81bd 18459 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, true);
df58fc94
RS
18460 fragp->fr_var = new_var;
18461
18462 return new_var - old_var;
18463 }
18464
252b5132
RH
18465 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18466 return 0;
18467
8507b6e7 18468 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18469 {
8507b6e7
MR
18470 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18471 {
18472 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18473 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18474 }
18475 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18476 {
18477 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18478 return -2;
18479 }
18480 else
18481 return 0;
18482 }
18483 else if (!mips16_macro_frag (fragp, sec, stretch))
18484 {
18485 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18486 {
18487 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18488 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18489 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18490 }
18491 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18492 {
18493 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18494 return 2;
18495 }
18496 else
252b5132 18497 return 0;
252b5132
RH
18498 }
18499 else
18500 {
8507b6e7 18501 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18502 return 0;
8507b6e7
MR
18503 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18504 {
18505 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18506 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18507 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18508 }
18509 else
18510 {
18511 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18512 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18513 }
252b5132
RH
18514 }
18515
18516 return 0;
18517}
18518
18519/* Convert a machine dependent frag. */
18520
18521void
17a2f251 18522md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18523{
4a6a3df4
AO
18524 if (RELAX_BRANCH_P (fragp->fr_subtype))
18525 {
4d68580a 18526 char *buf;
4a6a3df4 18527 unsigned long insn;
4a6a3df4 18528 fixS *fixp;
b34976b6 18529
4d68580a
RS
18530 buf = fragp->fr_literal + fragp->fr_fix;
18531 insn = read_insn (buf);
b34976b6 18532
4a6a3df4
AO
18533 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18534 {
18535 /* We generate a fixup instead of applying it right now
18536 because, if there are linker relaxations, we're going to
18537 need the relocations. */
bbd27b76
MR
18538 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18539 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18540 true, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18541 fixp->fx_file = fragp->fr_file;
18542 fixp->fx_line = fragp->fr_line;
b34976b6 18543
4d68580a 18544 buf = write_insn (buf, insn);
4a6a3df4
AO
18545 }
18546 else
18547 {
18548 int i;
18549
18550 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18551 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18552
18553 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18554 goto uncond;
18555
18556 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18557 {
18558 /* Reverse the branch. */
18559 switch ((insn >> 28) & 0xf)
18560 {
18561 case 4:
56d438b1
CF
18562 if ((insn & 0xff000000) == 0x47000000
18563 || (insn & 0xff600000) == 0x45600000)
18564 {
18565 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18566 reversed by tweaking bit 23. */
18567 insn ^= 0x00800000;
18568 }
18569 else
18570 {
18571 /* bc[0-3][tf]l? instructions can have the condition
18572 reversed by tweaking a single TF bit, and their
18573 opcodes all have 0x4???????. */
18574 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18575 insn ^= 0x00010000;
18576 }
4a6a3df4
AO
18577 break;
18578
18579 case 0:
18580 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18581 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18582 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18583 insn ^= 0x00010000;
18584 break;
b34976b6 18585
4a6a3df4
AO
18586 case 1:
18587 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18588 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18589 insn ^= 0x04000000;
18590 break;
18591
18592 default:
18593 abort ();
18594 }
18595 }
18596
18597 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18598 {
18599 /* Clear the and-link bit. */
9c2799c2 18600 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18601
54f4ddb3
TS
18602 /* bltzal 0x04100000 bgezal 0x04110000
18603 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18604 insn &= ~0x00100000;
18605 }
18606
18607 /* Branch over the branch (if the branch was likely) or the
18608 full jump (not likely case). Compute the offset from the
18609 current instruction to branch to. */
18610 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18611 i = 16;
18612 else
18613 {
18614 /* How many bytes in instructions we've already emitted? */
4d68580a 18615 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18616 /* How many bytes in instructions from here to the end? */
18617 i = fragp->fr_var - i;
18618 }
18619 /* Convert to instruction count. */
18620 i >>= 2;
18621 /* Branch counts from the next instruction. */
b34976b6 18622 i--;
4a6a3df4
AO
18623 insn |= i;
18624 /* Branch over the jump. */
4d68580a 18625 buf = write_insn (buf, insn);
4a6a3df4 18626
54f4ddb3 18627 /* nop */
4d68580a 18628 buf = write_insn (buf, 0);
4a6a3df4
AO
18629
18630 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18631 {
18632 /* beql $0, $0, 2f */
18633 insn = 0x50000000;
18634 /* Compute the PC offset from the current instruction to
18635 the end of the variable frag. */
18636 /* How many bytes in instructions we've already emitted? */
4d68580a 18637 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18638 /* How many bytes in instructions from here to the end? */
18639 i = fragp->fr_var - i;
18640 /* Convert to instruction count. */
18641 i >>= 2;
18642 /* Don't decrement i, because we want to branch over the
18643 delay slot. */
4a6a3df4 18644 insn |= i;
4a6a3df4 18645
4d68580a
RS
18646 buf = write_insn (buf, insn);
18647 buf = write_insn (buf, 0);
4a6a3df4
AO
18648 }
18649
18650 uncond:
ce8ad872 18651 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18652 {
18653 /* j or jal. */
18654 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18655 ? 0x0c000000 : 0x08000000);
4a6a3df4 18656
bbd27b76
MR
18657 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18658 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18659 false, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18660 fixp->fx_file = fragp->fr_file;
18661 fixp->fx_line = fragp->fr_line;
18662
4d68580a 18663 buf = write_insn (buf, insn);
4a6a3df4
AO
18664 }
18665 else
18666 {
66b3e8da
MR
18667 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18668
4a6a3df4 18669 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18670 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18671 insn |= at << OP_SH_RT;
4a6a3df4 18672
bbd27b76
MR
18673 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18674 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18675 false, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18676 fixp->fx_file = fragp->fr_file;
18677 fixp->fx_line = fragp->fr_line;
18678
4d68580a 18679 buf = write_insn (buf, insn);
b34976b6 18680
4a6a3df4 18681 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18682 /* nop */
18683 buf = write_insn (buf, 0);
4a6a3df4
AO
18684
18685 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18686 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18687 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18688
bbd27b76
MR
18689 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18690 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18691 false, BFD_RELOC_LO16);
4a6a3df4
AO
18692 fixp->fx_file = fragp->fr_file;
18693 fixp->fx_line = fragp->fr_line;
b34976b6 18694
4d68580a 18695 buf = write_insn (buf, insn);
4a6a3df4
AO
18696
18697 /* j(al)r $at. */
18698 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18699 insn = 0x0000f809;
4a6a3df4 18700 else
66b3e8da
MR
18701 insn = 0x00000008;
18702 insn |= at << OP_SH_RS;
4a6a3df4 18703
4d68580a 18704 buf = write_insn (buf, insn);
4a6a3df4
AO
18705 }
18706 }
18707
4a6a3df4 18708 fragp->fr_fix += fragp->fr_var;
4d68580a 18709 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18710 return;
18711 }
18712
df58fc94
RS
18713 /* Relax microMIPS branches. */
18714 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18715 {
4d68580a 18716 char *buf = fragp->fr_literal + fragp->fr_fix;
5b7c81bd
AM
18717 bool compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18718 bool insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18719 bool nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18720 bool pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18721 bool al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
df58fc94 18722 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
5b7c81bd 18723 bool short_ds;
df58fc94 18724 unsigned long insn;
df58fc94
RS
18725 fixS *fixp;
18726
df58fc94
RS
18727 fragp->fr_fix += fragp->fr_var;
18728
18729 /* Handle 16-bit branches that fit or are forced to fit. */
18730 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18731 {
18732 /* We generate a fixup instead of applying it right now,
18733 because if there is linker relaxation, we're going to
18734 need the relocations. */
834a65aa
MR
18735 switch (type)
18736 {
18737 case 'D':
18738 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18739 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18740 true, BFD_RELOC_MICROMIPS_10_PCREL_S1);
834a65aa
MR
18741 break;
18742 case 'E':
18743 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18744 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18745 true, BFD_RELOC_MICROMIPS_7_PCREL_S1);
834a65aa
MR
18746 break;
18747 default:
18748 abort ();
18749 }
df58fc94
RS
18750
18751 fixp->fx_file = fragp->fr_file;
18752 fixp->fx_line = fragp->fr_line;
18753
18754 /* These relocations can have an addend that won't fit in
18755 2 octets. */
18756 fixp->fx_no_overflow = 1;
18757
18758 return;
18759 }
18760
2309ddf2 18761 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18762 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18763 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18764 {
18765 /* We generate a fixup instead of applying it right now,
18766 because if there is linker relaxation, we're going to
18767 need the relocations. */
bbd27b76
MR
18768 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18769 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18770 true, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18771 fixp->fx_file = fragp->fr_file;
18772 fixp->fx_line = fragp->fr_line;
18773
18774 if (type == 0)
7bd374a4
MR
18775 {
18776 insn = read_compressed_insn (buf, 4);
18777 buf += 4;
18778
18779 if (nods)
18780 {
18781 /* Check the short-delay-slot bit. */
18782 if (!al || (insn & 0x02000000) != 0)
18783 buf = write_compressed_insn (buf, 0x0c00, 2);
18784 else
18785 buf = write_compressed_insn (buf, 0x00000000, 4);
18786 }
18787
18788 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18789 return;
18790 }
df58fc94
RS
18791 }
18792
18793 /* Relax 16-bit branches to 32-bit branches. */
18794 if (type != 0)
18795 {
4d68580a 18796 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18797
18798 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18799 insn = 0x94000000; /* beq */
18800 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18801 {
18802 unsigned long regno;
18803
18804 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18805 regno = micromips_to_32_reg_d_map [regno];
18806 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18807 insn |= regno << MICROMIPSOP_SH_RS;
18808 }
18809 else
18810 abort ();
18811
18812 /* Nothing else to do, just write it out. */
18813 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18814 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18815 {
4d68580a 18816 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18817 if (nods)
18818 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18819 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18820 return;
18821 }
18822 }
18823 else
4d68580a 18824 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18825
18826 /* Relax 32-bit branches to a sequence of instructions. */
18827 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18828 _("relaxed out-of-range branch into a jump"));
df58fc94 18829
2309ddf2 18830 /* Set the short-delay-slot bit. */
7bd374a4 18831 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18832
18833 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18834 {
18835 symbolS *l;
18836
18837 /* Reverse the branch. */
18838 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18839 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18840 insn ^= 0x20000000;
18841 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18842 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18843 || (insn & 0xffe00000) == 0x40800000 /* blez */
18844 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18845 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18846 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18847 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18848 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18849 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18850 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18851 insn ^= 0x00400000;
18852 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18853 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18854 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18855 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18856 insn ^= 0x00200000;
56d438b1
CF
18857 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18858 BNZ.df */
18859 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18860 BNZ.V */
18861 insn ^= 0x00800000;
df58fc94
RS
18862 else
18863 abort ();
18864
18865 if (al)
18866 {
18867 /* Clear the and-link and short-delay-slot bits. */
18868 gas_assert ((insn & 0xfda00000) == 0x40200000);
18869
18870 /* bltzal 0x40200000 bgezal 0x40600000 */
18871 /* bltzals 0x42200000 bgezals 0x42600000 */
18872 insn &= ~0x02200000;
18873 }
18874
18875 /* Make a label at the end for use with the branch. */
e01e1cee 18876 l = symbol_new (micromips_label_name (), asec, fragp, fragp->fr_fix);
df58fc94 18877 micromips_label_inc ();
f3ded42a 18878 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18879
18880 /* Refer to it. */
5b7c81bd 18881 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, true,
4d68580a 18882 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18883 fixp->fx_file = fragp->fr_file;
18884 fixp->fx_line = fragp->fr_line;
18885
18886 /* Branch over the jump. */
4d68580a 18887 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18888
df58fc94 18889 if (!compact)
8484fb75
MR
18890 {
18891 /* nop */
18892 if (insn32)
18893 buf = write_compressed_insn (buf, 0x00000000, 4);
18894 else
18895 buf = write_compressed_insn (buf, 0x0c00, 2);
18896 }
df58fc94
RS
18897 }
18898
ce8ad872 18899 if (!pic)
df58fc94 18900 {
7bd374a4
MR
18901 unsigned long jal = (short_ds || nods
18902 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18903
df58fc94
RS
18904 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18905 insn = al ? jal : 0xd4000000;
18906
bbd27b76
MR
18907 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18908 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18909 false, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18910 fixp->fx_file = fragp->fr_file;
18911 fixp->fx_line = fragp->fr_line;
18912
4d68580a 18913 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18914
7bd374a4 18915 if (compact || nods)
8484fb75
MR
18916 {
18917 /* nop */
18918 if (insn32)
18919 buf = write_compressed_insn (buf, 0x00000000, 4);
18920 else
18921 buf = write_compressed_insn (buf, 0x0c00, 2);
18922 }
df58fc94
RS
18923 }
18924 else
18925 {
18926 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18927
18928 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18929 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18930 insn |= at << MICROMIPSOP_SH_RT;
18931
bbd27b76
MR
18932 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18933 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18934 false, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18935 fixp->fx_file = fragp->fr_file;
18936 fixp->fx_line = fragp->fr_line;
18937
4d68580a 18938 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18939
18940 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18941 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18942 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18943
bbd27b76
MR
18944 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18945 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18946 false, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18947 fixp->fx_file = fragp->fr_file;
18948 fixp->fx_line = fragp->fr_line;
18949
4d68580a 18950 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18951
8484fb75
MR
18952 if (insn32)
18953 {
18954 /* jr/jalr $at */
18955 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18956 insn |= at << MICROMIPSOP_SH_RS;
18957
18958 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18959
7bd374a4 18960 if (compact || nods)
8484fb75
MR
18961 /* nop */
18962 buf = write_compressed_insn (buf, 0x00000000, 4);
18963 }
18964 else
18965 {
18966 /* jr/jrc/jalr/jalrs $at */
18967 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18968 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18969
18970 insn = al ? jalr : jr;
18971 insn |= at << MICROMIPSOP_SH_MJ;
18972
18973 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18974 if (al && nods)
18975 {
18976 /* nop */
18977 if (short_ds)
18978 buf = write_compressed_insn (buf, 0x0c00, 2);
18979 else
18980 buf = write_compressed_insn (buf, 0x00000000, 4);
18981 }
8484fb75 18982 }
df58fc94
RS
18983 }
18984
4d68580a 18985 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18986 return;
18987 }
18988
252b5132
RH
18989 if (RELAX_MIPS16_P (fragp->fr_subtype))
18990 {
18991 int type;
3ccad066 18992 const struct mips_int_operand *operand;
252b5132 18993 offsetT val;
5c04167a 18994 char *buf;
8507b6e7 18995 unsigned int user_length;
5b7c81bd 18996 bool need_reloc;
252b5132 18997 unsigned long insn;
5b7c81bd
AM
18998 bool mac;
18999 bool ext;
88a7ef16 19000 segT symsec;
252b5132
RH
19001
19002 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
5b7c81bd 19003 operand = mips16_immed_operand (type, false);
252b5132 19004
8507b6e7 19005 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 19006 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 19007 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
19008
19009 symsec = S_GET_SEGMENT (fragp->fr_symbol);
5b7c81bd 19010 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, true)
8507b6e7 19011 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
19012 ? asec != symsec
19013 : !bfd_is_abs_section (symsec)));
19014
8507b6e7 19015 if (operand->root.type == OP_PCREL && !mac)
252b5132 19016 {
3ccad066 19017 const struct mips_pcrel_operand *pcrel_op;
252b5132 19018
3ccad066 19019 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 19020
14f72d45 19021 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 19022 {
37b2d327
MR
19023 if (!mips_ignore_branch_isa
19024 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
19025 as_bad_where (fragp->fr_file, fragp->fr_line,
19026 _("branch to a symbol in another ISA mode"));
19027 else if ((fragp->fr_offset & 0x1) != 0)
19028 as_bad_where (fragp->fr_file, fragp->fr_line,
19029 _("branch to misaligned address (0x%lx)"),
52031738
FS
19030 (long) (resolve_symbol_value (fragp->fr_symbol)
19031 + (fragp->fr_offset & ~1)));
252b5132 19032 }
252b5132 19033
14f72d45 19034 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
19035
19036 /* Make sure the section winds up with the alignment we have
19037 assumed. */
3ccad066
RS
19038 if (operand->shift > 0)
19039 record_alignment (asec, operand->shift);
252b5132
RH
19040 }
19041
8507b6e7
MR
19042 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19043 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19044 {
19045 if (mac)
19046 as_warn_where (fragp->fr_file, fragp->fr_line,
19047 _("macro instruction expanded into multiple "
19048 "instructions in a branch delay slot"));
19049 else if (ext)
19050 as_warn_where (fragp->fr_file, fragp->fr_line,
19051 _("extended instruction in a branch delay slot"));
19052 }
19053 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 19054 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
19055 _("macro instruction expanded into multiple "
19056 "instructions"));
252b5132 19057
5c04167a 19058 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 19059
4d68580a 19060 insn = read_compressed_insn (buf, 2);
5c04167a
RS
19061 if (ext)
19062 insn |= MIPS16_EXTEND;
252b5132 19063
5c04167a
RS
19064 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19065 user_length = 4;
19066 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19067 user_length = 2;
19068 else
19069 user_length = 0;
19070
8507b6e7 19071 if (mac)
c9775dde 19072 {
8507b6e7
MR
19073 unsigned long reg;
19074 unsigned long new;
19075 unsigned long op;
5b7c81bd 19076 bool e2;
8507b6e7
MR
19077
19078 gas_assert (type == 'A' || type == 'B' || type == 'E');
19079 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 19080
25499ac7
MR
19081 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19082
8507b6e7 19083 if (need_reloc)
c9775dde 19084 {
8507b6e7
MR
19085 fixS *fixp;
19086
19087 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19088
19089 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19090 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19091 false, BFD_RELOC_MIPS16_HI16_S);
8507b6e7
MR
19092 fixp->fx_file = fragp->fr_file;
19093 fixp->fx_line = fragp->fr_line;
19094
25499ac7 19095 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7 19096 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19097 false, BFD_RELOC_MIPS16_LO16);
8507b6e7
MR
19098 fixp->fx_file = fragp->fr_file;
19099 fixp->fx_line = fragp->fr_line;
19100
19101 val = 0;
19102 }
19103
19104 switch (insn & 0xf800)
19105 {
19106 case 0x0800: /* ADDIU */
19107 reg = (insn >> 8) & 0x7;
19108 op = 0xf0004800 | (reg << 8);
c9775dde 19109 break;
8507b6e7
MR
19110 case 0xb000: /* LW */
19111 reg = (insn >> 8) & 0x7;
19112 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 19113 break;
8507b6e7
MR
19114 case 0xf800: /* I64 */
19115 reg = (insn >> 5) & 0x7;
19116 switch (insn & 0x0700)
19117 {
19118 case 0x0400: /* LD */
19119 op = 0xf0003800 | (reg << 8) | (reg << 5);
19120 break;
19121 case 0x0600: /* DADDIU */
19122 op = 0xf000fd00 | (reg << 5);
19123 break;
19124 default:
19125 abort ();
19126 }
19127 break;
19128 default:
19129 abort ();
c9775dde 19130 }
8507b6e7 19131
25499ac7 19132 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19133 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19134 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19135 if (!e2)
19136 {
19137 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19138 buf = write_compressed_insn (buf, new, 4);
19139 }
8507b6e7
MR
19140 op |= mips16_immed_extend (val, 16);
19141 buf = write_compressed_insn (buf, op, 4);
19142
25499ac7 19143 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19144 }
19145 else
19146 {
19147 unsigned int length = ext ? 4 : 2;
19148
19149 if (need_reloc)
c9775dde 19150 {
8507b6e7 19151 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19152 fixS *fixp;
c9775dde 19153
8507b6e7
MR
19154 switch (type)
19155 {
19156 case 'p':
19157 case 'q':
19158 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19159 break;
19160 default:
19161 break;
19162 }
19163 if (mac || reloc == BFD_RELOC_NONE)
19164 as_bad_where (fragp->fr_file, fragp->fr_line,
19165 _("unsupported relocation"));
19166 else if (ext)
19167 {
bbd27b76
MR
19168 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19169 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19170 true, reloc);
8507b6e7
MR
19171 fixp->fx_file = fragp->fr_file;
19172 fixp->fx_line = fragp->fr_line;
19173 }
19174 else
19175 as_bad_where (fragp->fr_file, fragp->fr_line,
19176 _("invalid unextended operand value"));
c9775dde 19177 }
eefc3365 19178 else
8507b6e7
MR
19179 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19180 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19181
8507b6e7
MR
19182 gas_assert (mips16_opcode_length (insn) == length);
19183 write_compressed_insn (buf, insn, length);
19184 fragp->fr_fix += length;
19185 }
252b5132
RH
19186 }
19187 else
19188 {
df58fc94 19189 relax_substateT subtype = fragp->fr_subtype;
5b7c81bd
AM
19190 bool second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19191 bool use_second = (subtype & RELAX_USE_SECOND) != 0;
871a6bd2 19192 unsigned int first, second;
4d7206a2 19193 fixS *fixp;
252b5132 19194
df58fc94
RS
19195 first = RELAX_FIRST (subtype);
19196 second = RELAX_SECOND (subtype);
4d7206a2 19197 fixp = (fixS *) fragp->fr_opcode;
252b5132 19198
df58fc94
RS
19199 /* If the delay slot chosen does not match the size of the instruction,
19200 then emit a warning. */
19201 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19202 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19203 {
19204 relax_substateT s;
19205 const char *msg;
19206
19207 s = subtype & (RELAX_DELAY_SLOT_16BIT
19208 | RELAX_DELAY_SLOT_SIZE_FIRST
19209 | RELAX_DELAY_SLOT_SIZE_SECOND);
19210 msg = macro_warning (s);
19211 if (msg != NULL)
db9b2be4 19212 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19213 subtype &= ~s;
19214 }
19215
584892a6 19216 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19217 if (use_second == second_longer)
584892a6 19218 {
df58fc94
RS
19219 relax_substateT s;
19220 const char *msg;
19221
19222 s = (subtype
19223 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19224 msg = macro_warning (s);
19225 if (msg != NULL)
db9b2be4 19226 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19227 subtype &= ~s;
584892a6
RS
19228 }
19229
4d7206a2
RS
19230 /* Go through all the fixups for the first sequence. Disable them
19231 (by marking them as done) if we're going to use the second
19232 sequence instead. */
19233 while (fixp
19234 && fixp->fx_frag == fragp
90bd3c90 19235 && fixp->fx_where + second < fragp->fr_fix)
4d7206a2 19236 {
df58fc94 19237 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19238 fixp->fx_done = 1;
19239 fixp = fixp->fx_next;
19240 }
252b5132 19241
4d7206a2
RS
19242 /* Go through the fixups for the second sequence. Disable them if
19243 we're going to use the first sequence, otherwise adjust their
19244 addresses to account for the relaxation. */
19245 while (fixp && fixp->fx_frag == fragp)
19246 {
df58fc94 19247 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19248 fixp->fx_where -= first;
19249 else
19250 fixp->fx_done = 1;
19251 fixp = fixp->fx_next;
19252 }
19253
19254 /* Now modify the frag contents. */
df58fc94 19255 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19256 {
19257 char *start;
19258
19259 start = fragp->fr_literal + fragp->fr_fix - first - second;
19260 memmove (start, start + first, second);
19261 fragp->fr_fix -= first;
19262 }
19263 else
19264 fragp->fr_fix -= second;
252b5132
RH
19265 }
19266}
19267
252b5132
RH
19268/* This function is called after the relocs have been generated.
19269 We've been storing mips16 text labels as odd. Here we convert them
19270 back to even for the convenience of the debugger. */
19271
19272void
17a2f251 19273mips_frob_file_after_relocs (void)
252b5132
RH
19274{
19275 asymbol **syms;
19276 unsigned int count, i;
19277
252b5132
RH
19278 syms = bfd_get_outsymbols (stdoutput);
19279 count = bfd_get_symcount (stdoutput);
19280 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19281 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19282 && ((*syms)->value & 1) != 0)
19283 {
19284 (*syms)->value &= ~1;
19285 /* If the symbol has an odd size, it was probably computed
19286 incorrectly, so adjust that as well. */
19287 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19288 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19289 }
252b5132
RH
19290}
19291
a1facbec
MR
19292/* This function is called whenever a label is defined, including fake
19293 labels instantiated off the dot special symbol. It is used when
19294 handling branch delays; if a branch has a label, we assume we cannot
19295 move it. This also bumps the value of the symbol by 1 in compressed
19296 code. */
252b5132 19297
e1b47bd5 19298static void
a1facbec 19299mips_record_label (symbolS *sym)
252b5132 19300{
a8dbcb85 19301 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19302 struct insn_label_list *l;
19303
19304 if (free_insn_labels == NULL)
325801bd 19305 l = XNEW (struct insn_label_list);
252b5132
RH
19306 else
19307 {
19308 l = free_insn_labels;
19309 free_insn_labels = l->next;
19310 }
19311
19312 l->label = sym;
a8dbcb85
TS
19313 l->next = si->label_list;
19314 si->label_list = l;
a1facbec 19315}
07a53e5c 19316
a1facbec
MR
19317/* This function is called as tc_frob_label() whenever a label is defined
19318 and adds a DWARF-2 record we only want for true labels. */
19319
19320void
19321mips_define_label (symbolS *sym)
19322{
19323 mips_record_label (sym);
07a53e5c 19324 dwarf2_emit_label (sym);
252b5132 19325}
e1b47bd5
RS
19326
19327/* This function is called by tc_new_dot_label whenever a new dot symbol
19328 is defined. */
19329
19330void
19331mips_add_dot_label (symbolS *sym)
19332{
19333 mips_record_label (sym);
19334 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19335 mips_compressed_mark_label (sym);
19336}
252b5132 19337\f
351cdf24
MF
19338/* Converting ASE flags from internal to .MIPS.abiflags values. */
19339static unsigned int
19340mips_convert_ase_flags (int ase)
19341{
19342 unsigned int ext_ases = 0;
19343
19344 if (ase & ASE_DSP)
19345 ext_ases |= AFL_ASE_DSP;
19346 if (ase & ASE_DSPR2)
19347 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19348 if (ase & ASE_DSPR3)
19349 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19350 if (ase & ASE_EVA)
19351 ext_ases |= AFL_ASE_EVA;
19352 if (ase & ASE_MCU)
19353 ext_ases |= AFL_ASE_MCU;
19354 if (ase & ASE_MDMX)
19355 ext_ases |= AFL_ASE_MDMX;
19356 if (ase & ASE_MIPS3D)
19357 ext_ases |= AFL_ASE_MIPS3D;
19358 if (ase & ASE_MT)
19359 ext_ases |= AFL_ASE_MT;
19360 if (ase & ASE_SMARTMIPS)
19361 ext_ases |= AFL_ASE_SMARTMIPS;
19362 if (ase & ASE_VIRT)
19363 ext_ases |= AFL_ASE_VIRT;
19364 if (ase & ASE_MSA)
19365 ext_ases |= AFL_ASE_MSA;
19366 if (ase & ASE_XPA)
19367 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19368 if (ase & ASE_MIPS16E2)
19369 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19370 if (ase & ASE_CRC)
19371 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19372 if (ase & ASE_GINV)
19373 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19374 if (ase & ASE_LOONGSON_MMI)
19375 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19376 if (ase & ASE_LOONGSON_CAM)
19377 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19378 if (ase & ASE_LOONGSON_EXT)
19379 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19380 if (ase & ASE_LOONGSON_EXT2)
19381 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19382
19383 return ext_ases;
19384}
252b5132
RH
19385/* Some special processing for a MIPS ELF file. */
19386
19387void
17a2f251 19388mips_elf_final_processing (void)
252b5132 19389{
351cdf24
MF
19390 int fpabi;
19391 Elf_Internal_ABIFlags_v0 flags;
19392
19393 flags.version = 0;
19394 flags.isa_rev = 0;
19395 switch (file_mips_opts.isa)
19396 {
19397 case INSN_ISA1:
19398 flags.isa_level = 1;
19399 break;
19400 case INSN_ISA2:
19401 flags.isa_level = 2;
19402 break;
19403 case INSN_ISA3:
19404 flags.isa_level = 3;
19405 break;
19406 case INSN_ISA4:
19407 flags.isa_level = 4;
19408 break;
19409 case INSN_ISA5:
19410 flags.isa_level = 5;
19411 break;
19412 case INSN_ISA32:
19413 flags.isa_level = 32;
19414 flags.isa_rev = 1;
19415 break;
19416 case INSN_ISA32R2:
19417 flags.isa_level = 32;
19418 flags.isa_rev = 2;
19419 break;
19420 case INSN_ISA32R3:
19421 flags.isa_level = 32;
19422 flags.isa_rev = 3;
19423 break;
19424 case INSN_ISA32R5:
19425 flags.isa_level = 32;
19426 flags.isa_rev = 5;
19427 break;
09c14161
MF
19428 case INSN_ISA32R6:
19429 flags.isa_level = 32;
19430 flags.isa_rev = 6;
19431 break;
351cdf24
MF
19432 case INSN_ISA64:
19433 flags.isa_level = 64;
19434 flags.isa_rev = 1;
19435 break;
19436 case INSN_ISA64R2:
19437 flags.isa_level = 64;
19438 flags.isa_rev = 2;
19439 break;
19440 case INSN_ISA64R3:
19441 flags.isa_level = 64;
19442 flags.isa_rev = 3;
19443 break;
19444 case INSN_ISA64R5:
19445 flags.isa_level = 64;
19446 flags.isa_rev = 5;
19447 break;
09c14161
MF
19448 case INSN_ISA64R6:
19449 flags.isa_level = 64;
19450 flags.isa_rev = 6;
19451 break;
351cdf24
MF
19452 }
19453
19454 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19455 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19456 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19457 : (file_mips_opts.fp == 64) ? AFL_REG_64
19458 : AFL_REG_32;
19459 flags.cpr2_size = AFL_REG_NONE;
19460 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19461 Tag_GNU_MIPS_ABI_FP);
19462 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19463 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19464 if (file_ase_mips16)
19465 flags.ases |= AFL_ASE_MIPS16;
19466 if (file_ase_micromips)
19467 flags.ases |= AFL_ASE_MICROMIPS;
19468 flags.flags1 = 0;
19469 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19470 || file_mips_opts.fp == 64)
19471 && file_mips_opts.oddspreg)
19472 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19473 flags.flags2 = 0;
19474
19475 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19476 ((Elf_External_ABIFlags_v0 *)
19477 mips_flags_frag));
19478
252b5132 19479 /* Write out the register information. */
316f5878 19480 if (mips_abi != N64_ABI)
252b5132
RH
19481 {
19482 Elf32_RegInfo s;
19483
19484 s.ri_gprmask = mips_gprmask;
19485 s.ri_cprmask[0] = mips_cprmask[0];
19486 s.ri_cprmask[1] = mips_cprmask[1];
19487 s.ri_cprmask[2] = mips_cprmask[2];
19488 s.ri_cprmask[3] = mips_cprmask[3];
19489 /* The gp_value field is set by the MIPS ELF backend. */
19490
19491 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19492 ((Elf32_External_RegInfo *)
19493 mips_regmask_frag));
19494 }
19495 else
19496 {
19497 Elf64_Internal_RegInfo s;
19498
19499 s.ri_gprmask = mips_gprmask;
19500 s.ri_pad = 0;
19501 s.ri_cprmask[0] = mips_cprmask[0];
19502 s.ri_cprmask[1] = mips_cprmask[1];
19503 s.ri_cprmask[2] = mips_cprmask[2];
19504 s.ri_cprmask[3] = mips_cprmask[3];
19505 /* The gp_value field is set by the MIPS ELF backend. */
19506
19507 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19508 ((Elf64_External_RegInfo *)
19509 mips_regmask_frag));
19510 }
19511
19512 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19513 sort of BFD interface for this. */
19514 if (mips_any_noreorder)
19515 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19516 if (mips_pic != NO_PIC)
143d77c5 19517 {
8b828383 19518 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19519 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19520 }
19521 if (mips_abicalls)
19522 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19523
b015e599
AP
19524 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19525 defined at present; this might need to change in future. */
a4672219
TS
19526 if (file_ase_mips16)
19527 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19528 if (file_ase_micromips)
19529 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19530 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19531 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19532
bdaaa2e1 19533 /* Set the MIPS ELF ABI flags. */
316f5878 19534 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19535 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19536 else if (mips_abi == O64_ABI)
252b5132 19537 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19538 else if (mips_abi == EABI_ABI)
252b5132 19539 {
bad1aba3 19540 if (file_mips_opts.gp == 64)
252b5132
RH
19541 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19542 else
19543 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19544 }
be00bddd 19545
defc8e2b 19546 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19547
19548 if (mips_32bitmode)
19549 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19550
7361da2c 19551 if (mips_nan2008 == 1)
ba92f887
MR
19552 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19553
ad3fea08 19554 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19555 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19556 Tag_GNU_MIPS_ABI_FP);
19557 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19558 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19559}
252b5132 19560\f
beae10d5 19561typedef struct proc {
9b2f1d35
EC
19562 symbolS *func_sym;
19563 symbolS *func_end_sym;
beae10d5
KH
19564 unsigned long reg_mask;
19565 unsigned long reg_offset;
19566 unsigned long fpreg_mask;
19567 unsigned long fpreg_offset;
19568 unsigned long frame_offset;
19569 unsigned long frame_reg;
19570 unsigned long pc_reg;
19571} procS;
252b5132
RH
19572
19573static procS cur_proc;
19574static procS *cur_proc_ptr;
19575static int numprocs;
19576
df58fc94
RS
19577/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19578 as "2", and a normal nop as "0". */
19579
19580#define NOP_OPCODE_MIPS 0
19581#define NOP_OPCODE_MIPS16 1
19582#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19583
19584char
19585mips_nop_opcode (void)
19586{
df58fc94
RS
19587 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19588 return NOP_OPCODE_MICROMIPS;
19589 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19590 return NOP_OPCODE_MIPS16;
19591 else
19592 return NOP_OPCODE_MIPS;
742a56fe
RS
19593}
19594
df58fc94
RS
19595/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19596 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19597
0a9ef439 19598void
17a2f251 19599mips_handle_align (fragS *fragp)
a19d8eb0 19600{
df58fc94 19601 char nop_opcode;
742a56fe 19602 char *p;
c67a084a
NC
19603 int bytes, size, excess;
19604 valueT opcode;
742a56fe 19605
0a9ef439
RH
19606 if (fragp->fr_type != rs_align_code)
19607 return;
19608
742a56fe 19609 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19610 nop_opcode = *p;
19611 switch (nop_opcode)
a19d8eb0 19612 {
df58fc94
RS
19613 case NOP_OPCODE_MICROMIPS:
19614 opcode = micromips_nop32_insn.insn_opcode;
19615 size = 4;
19616 break;
19617 case NOP_OPCODE_MIPS16:
c67a084a
NC
19618 opcode = mips16_nop_insn.insn_opcode;
19619 size = 2;
df58fc94
RS
19620 break;
19621 case NOP_OPCODE_MIPS:
19622 default:
c67a084a
NC
19623 opcode = nop_insn.insn_opcode;
19624 size = 4;
df58fc94 19625 break;
c67a084a 19626 }
a19d8eb0 19627
c67a084a
NC
19628 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19629 excess = bytes % size;
df58fc94
RS
19630
19631 /* Handle the leading part if we're not inserting a whole number of
19632 instructions, and make it the end of the fixed part of the frag.
19633 Try to fit in a short microMIPS NOP if applicable and possible,
19634 and use zeroes otherwise. */
19635 gas_assert (excess < 4);
19636 fragp->fr_fix += excess;
19637 switch (excess)
c67a084a 19638 {
df58fc94
RS
19639 case 3:
19640 *p++ = '\0';
19641 /* Fall through. */
19642 case 2:
833794fc 19643 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19644 {
4d68580a 19645 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19646 break;
19647 }
19648 *p++ = '\0';
19649 /* Fall through. */
19650 case 1:
19651 *p++ = '\0';
19652 /* Fall through. */
19653 case 0:
19654 break;
a19d8eb0 19655 }
c67a084a
NC
19656
19657 md_number_to_chars (p, opcode, size);
19658 fragp->fr_var = size;
a19d8eb0
CP
19659}
19660
252b5132 19661static long
17a2f251 19662get_number (void)
252b5132
RH
19663{
19664 int negative = 0;
19665 long val = 0;
19666
19667 if (*input_line_pointer == '-')
19668 {
19669 ++input_line_pointer;
19670 negative = 1;
19671 }
3882b010 19672 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19673 as_bad (_("expected simple number"));
252b5132
RH
19674 if (input_line_pointer[0] == '0')
19675 {
19676 if (input_line_pointer[1] == 'x')
19677 {
19678 input_line_pointer += 2;
3882b010 19679 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19680 {
19681 val <<= 4;
19682 val |= hex_value (*input_line_pointer++);
19683 }
19684 return negative ? -val : val;
19685 }
19686 else
19687 {
19688 ++input_line_pointer;
3882b010 19689 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19690 {
19691 val <<= 3;
19692 val |= *input_line_pointer++ - '0';
19693 }
19694 return negative ? -val : val;
19695 }
19696 }
3882b010 19697 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19698 {
19699 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19700 *input_line_pointer, *input_line_pointer);
956cd1d6 19701 as_warn (_("invalid number"));
252b5132
RH
19702 return -1;
19703 }
3882b010 19704 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19705 {
19706 val *= 10;
19707 val += *input_line_pointer++ - '0';
19708 }
19709 return negative ? -val : val;
19710}
19711
19712/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19713 is an initial number which is the ECOFF file index. In the non-ECOFF
19714 case .file implies DWARF-2. */
19715
19716static void
17a2f251 19717s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19718{
ecb4347a
DJ
19719 static int first_file_directive = 0;
19720
c5dd6aab
DJ
19721 if (ECOFF_DEBUGGING)
19722 {
19723 get_number ();
c39e89c3 19724 s_file (0);
c5dd6aab
DJ
19725 }
19726 else
ecb4347a
DJ
19727 {
19728 char *filename;
19729
68d20676 19730 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19731
19732 /* Versions of GCC up to 3.1 start files with a ".file"
19733 directive even for stabs output. Make sure that this
19734 ".file" is handled. Note that you need a version of GCC
19735 after 3.1 in order to support DWARF-2 on MIPS. */
19736 if (filename != NULL && ! first_file_directive)
19737 {
66b39b8b 19738 new_logical_line (filename, -1);
c39e89c3 19739 s_file_string (filename);
ecb4347a
DJ
19740 }
19741 first_file_directive = 1;
19742 }
c5dd6aab
DJ
19743}
19744
19745/* The .loc directive, implying DWARF-2. */
252b5132
RH
19746
19747static void
17a2f251 19748s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19749{
c5dd6aab
DJ
19750 if (!ECOFF_DEBUGGING)
19751 dwarf2_directive_loc (0);
252b5132
RH
19752}
19753
252b5132
RH
19754/* The .end directive. */
19755
19756static void
17a2f251 19757s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19758{
19759 symbolS *p;
252b5132 19760
7a621144
DJ
19761 /* Following functions need their own .frame and .cprestore directives. */
19762 mips_frame_reg_valid = 0;
19763 mips_cprestore_valid = 0;
19764
252b5132
RH
19765 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19766 {
19767 p = get_symbol ();
19768 demand_empty_rest_of_line ();
19769 }
19770 else
19771 p = NULL;
19772
fd361982 19773 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
252b5132
RH
19774 as_warn (_(".end not in text section"));
19775
19776 if (!cur_proc_ptr)
19777 {
1661c76c 19778 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19779 demand_empty_rest_of_line ();
19780 return;
19781 }
19782
19783 if (p != NULL)
19784 {
9c2799c2 19785 gas_assert (S_GET_NAME (p));
9b2f1d35 19786 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19787 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19788
19789 if (debug_type == DEBUG_STABS)
19790 stabs_generate_asm_endfunc (S_GET_NAME (p),
19791 S_GET_NAME (p));
252b5132
RH
19792 }
19793 else
19794 as_warn (_(".end directive missing or unknown symbol"));
19795
9b2f1d35
EC
19796 /* Create an expression to calculate the size of the function. */
19797 if (p && cur_proc_ptr)
19798 {
19799 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19800 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19801
19802 obj->size = exp;
19803 exp->X_op = O_subtract;
19804 exp->X_add_symbol = symbol_temp_new_now ();
19805 exp->X_op_symbol = p;
19806 exp->X_add_number = 0;
19807
19808 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19809 }
19810
5ff6a06c
MR
19811#ifdef md_flush_pending_output
19812 md_flush_pending_output ();
19813#endif
19814
ecb4347a 19815 /* Generate a .pdr section. */
f3ded42a 19816 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19817 {
19818 segT saved_seg = now_seg;
19819 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19820 expressionS exp;
19821 char *fragp;
252b5132 19822
9c2799c2 19823 gas_assert (pdr_seg);
ecb4347a 19824 subseg_set (pdr_seg, 0);
252b5132 19825
ecb4347a
DJ
19826 /* Write the symbol. */
19827 exp.X_op = O_symbol;
19828 exp.X_add_symbol = p;
19829 exp.X_add_number = 0;
19830 emit_expr (&exp, 4);
252b5132 19831
ecb4347a 19832 fragp = frag_more (7 * 4);
252b5132 19833
17a2f251
TS
19834 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19835 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19836 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19837 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19838 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19839 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19840 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19841
ecb4347a
DJ
19842 subseg_set (saved_seg, saved_subseg);
19843 }
252b5132
RH
19844
19845 cur_proc_ptr = NULL;
19846}
19847
19848/* The .aent and .ent directives. */
19849
19850static void
17a2f251 19851s_mips_ent (int aent)
252b5132 19852{
252b5132 19853 symbolS *symbolP;
252b5132
RH
19854
19855 symbolP = get_symbol ();
19856 if (*input_line_pointer == ',')
f9419b05 19857 ++input_line_pointer;
252b5132 19858 SKIP_WHITESPACE ();
3882b010 19859 if (ISDIGIT (*input_line_pointer)
d9a62219 19860 || *input_line_pointer == '-')
874e8986 19861 get_number ();
252b5132 19862
fd361982 19863 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
1661c76c 19864 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19865
19866 if (!aent && cur_proc_ptr)
9a41af64 19867 as_warn (_("missing .end"));
252b5132
RH
19868
19869 if (!aent)
19870 {
7a621144
DJ
19871 /* This function needs its own .frame and .cprestore directives. */
19872 mips_frame_reg_valid = 0;
19873 mips_cprestore_valid = 0;
19874
252b5132
RH
19875 cur_proc_ptr = &cur_proc;
19876 memset (cur_proc_ptr, '\0', sizeof (procS));
19877
9b2f1d35 19878 cur_proc_ptr->func_sym = symbolP;
252b5132 19879
f9419b05 19880 ++numprocs;
ecb4347a
DJ
19881
19882 if (debug_type == DEBUG_STABS)
19883 stabs_generate_asm_func (S_GET_NAME (symbolP),
19884 S_GET_NAME (symbolP));
252b5132
RH
19885 }
19886
7c0fc524
MR
19887 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19888
252b5132
RH
19889 demand_empty_rest_of_line ();
19890}
19891
19892/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19893 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19894 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19895 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19896 symbol table (in the mdebug section). */
19897
19898static void
17a2f251 19899s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19900{
f3ded42a
RS
19901 if (ECOFF_DEBUGGING)
19902 s_ignore (ignore);
19903 else
ecb4347a
DJ
19904 {
19905 long val;
252b5132 19906
ecb4347a
DJ
19907 if (cur_proc_ptr == (procS *) NULL)
19908 {
19909 as_warn (_(".frame outside of .ent"));
19910 demand_empty_rest_of_line ();
19911 return;
19912 }
252b5132 19913
ecb4347a
DJ
19914 cur_proc_ptr->frame_reg = tc_get_register (1);
19915
19916 SKIP_WHITESPACE ();
19917 if (*input_line_pointer++ != ','
19918 || get_absolute_expression_and_terminator (&val) != ',')
19919 {
1661c76c 19920 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19921 --input_line_pointer;
19922 demand_empty_rest_of_line ();
19923 return;
19924 }
252b5132 19925
ecb4347a
DJ
19926 cur_proc_ptr->frame_offset = val;
19927 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19928
252b5132 19929 demand_empty_rest_of_line ();
252b5132 19930 }
252b5132
RH
19931}
19932
bdaaa2e1
KH
19933/* The .fmask and .mask directives. If the mdebug section is present
19934 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19935 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19936 information correctly. We can't use the ecoff routines because they
252b5132
RH
19937 make reference to the ecoff symbol table (in the mdebug section). */
19938
19939static void
17a2f251 19940s_mips_mask (int reg_type)
252b5132 19941{
f3ded42a
RS
19942 if (ECOFF_DEBUGGING)
19943 s_ignore (reg_type);
19944 else
252b5132 19945 {
ecb4347a 19946 long mask, off;
252b5132 19947
ecb4347a
DJ
19948 if (cur_proc_ptr == (procS *) NULL)
19949 {
19950 as_warn (_(".mask/.fmask outside of .ent"));
19951 demand_empty_rest_of_line ();
19952 return;
19953 }
252b5132 19954
ecb4347a
DJ
19955 if (get_absolute_expression_and_terminator (&mask) != ',')
19956 {
1661c76c 19957 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19958 --input_line_pointer;
19959 demand_empty_rest_of_line ();
19960 return;
19961 }
252b5132 19962
ecb4347a
DJ
19963 off = get_absolute_expression ();
19964
19965 if (reg_type == 'F')
19966 {
19967 cur_proc_ptr->fpreg_mask = mask;
19968 cur_proc_ptr->fpreg_offset = off;
19969 }
19970 else
19971 {
19972 cur_proc_ptr->reg_mask = mask;
19973 cur_proc_ptr->reg_offset = off;
19974 }
19975
19976 demand_empty_rest_of_line ();
252b5132 19977 }
252b5132
RH
19978}
19979
316f5878
RS
19980/* A table describing all the processors gas knows about. Names are
19981 matched in the order listed.
e7af610e 19982
316f5878
RS
19983 To ease comparison, please keep this table in the same order as
19984 gcc's mips_cpu_info_table[]. */
e972090a
NC
19985static const struct mips_cpu_info mips_cpu_info_table[] =
19986{
6f2117ba 19987 /* Entries for generic ISAs. */
dbec9420
MR
19988 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19989 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19990 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19991 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19992 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19993 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19994 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19995 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19996 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
19997 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
19998 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19999 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
20000 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
20001 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
20002 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
20003
20004 /* MIPS I */
dbec9420
MR
20005 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
20006 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
20007 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
20008
20009 /* MIPS II */
dbec9420 20010 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
20011
20012 /* MIPS III */
dbec9420
MR
20013 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20014 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20015 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20016 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20017 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20018 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20019 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20020 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20021 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20022 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20023 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20024 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20025 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 20026 /* ST Microelectronics Loongson 2E and 2F cores. */
dbec9420
MR
20027 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
20028 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
20029
20030 /* MIPS IV */
dbec9420
MR
20031 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20032 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20033 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20034 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20035 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20036 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20037 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20038 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20039 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20040 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20041 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20042 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20043 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20044 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20045 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
20046
20047 /* MIPS 32 */
dbec9420
MR
20048 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20049 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20050 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20051 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
20052
20053 /* MIPS 32 Release 2 */
dbec9420
MR
20054 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20055 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20056 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20057 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20058 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20059 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20060 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20061 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
d16afab6 20062 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
dbec9420 20063 ISA_MIPS32R2, CPU_MIPS32R2 },
d16afab6 20064 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
dbec9420
MR
20065 ISA_MIPS32R2, CPU_MIPS32R2 },
20066 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20067 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20068 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20069 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20070 /* Deprecated forms of the above. */
dbec9420
MR
20071 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20072 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20073 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
dbec9420
MR
20074 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20075 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20076 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20077 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20078 /* Deprecated forms of the above. */
dbec9420
MR
20079 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20080 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20081 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
dbec9420
MR
20082 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20083 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20085 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20086 /* Deprecated forms of the above. */
dbec9420
MR
20087 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20088 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 20089 /* 34Kn is a 34kc without DSP. */
dbec9420 20090 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20091 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
dbec9420
MR
20092 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20093 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20094 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20095 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20096 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20097 /* Deprecated forms of the above. */
dbec9420
MR
20098 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20099 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 20100 /* 1004K cores are multiprocessor versions of the 34K. */
dbec9420
MR
20101 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20102 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20103 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20104 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 20105 /* interaptiv is the new name for 1004kf. */
dbec9420 20106 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
20107 { "interaptiv-mr2", 0,
20108 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
dbec9420 20109 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 20110 /* M5100 family. */
dbec9420
MR
20111 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20112 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 20113 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
dbec9420 20114 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 20115
316f5878 20116 /* MIPS 64 */
dbec9420
MR
20117 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20118 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20119 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20120 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 20121
6f2117ba 20122 /* Broadcom SB-1 CPU core. */
dbec9420 20123 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 20124 /* Broadcom SB-1A CPU core. */
dbec9420 20125 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 20126
6f2117ba
PH
20127 /* MIPS 64 Release 2. */
20128 /* Loongson CPU core. */
20129 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 20130 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
dbec9420 20131 ISA_MIPS64R2, CPU_GS464 },
ac8cb70f 20132 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
dbec9420
MR
20133 ISA_MIPS64R2, CPU_GS464 },
20134 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20135 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
20136 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20137 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20138
6f2117ba 20139 /* Cavium Networks Octeon CPU core. */
dbec9420
MR
20140 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20141 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20142 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
20143 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20144
52b6b6b9 20145 /* RMI Xlr */
dbec9420 20146 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20147
55a36193
MK
20148 /* Broadcom XLP.
20149 XLP is mostly like XLR, with the prominent exception that it is
20150 MIPS64R2 rather than MIPS64. */
dbec9420 20151 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20152
6f2117ba 20153 /* MIPS 64 Release 6. */
dbec9420 20154 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
bdc8beb4 20155 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
dbec9420
MR
20156 ISA_MIPS64R6, CPU_MIPS64R6},
20157 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20158
6f2117ba 20159 /* End marker. */
dbec9420 20160 { NULL, 0, 0, 0, 0 }
316f5878 20161};
e7af610e 20162
84ea6cf2 20163
316f5878
RS
20164/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20165 with a final "000" replaced by "k". Ignore case.
e7af610e 20166
316f5878 20167 Note: this function is shared between GCC and GAS. */
c6c98b38 20168
5b7c81bd 20169static bool
17a2f251 20170mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20171{
20172 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20173 given++, canonical++;
20174
20175 return ((*given == 0 && *canonical == 0)
20176 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20177}
20178
20179
20180/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20181 CPU name. We've traditionally allowed a lot of variation here.
20182
20183 Note: this function is shared between GCC and GAS. */
20184
5b7c81bd 20185static bool
17a2f251 20186mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20187{
20188 /* First see if the name matches exactly, or with a final "000"
20189 turned into "k". */
20190 if (mips_strict_matching_cpu_name_p (canonical, given))
5b7c81bd 20191 return true;
316f5878
RS
20192
20193 /* If not, try comparing based on numerical designation alone.
20194 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20195 if (TOLOWER (*given) == 'r')
20196 given++;
20197 if (!ISDIGIT (*given))
5b7c81bd 20198 return false;
316f5878
RS
20199
20200 /* Skip over some well-known prefixes in the canonical name,
20201 hoping to find a number there too. */
20202 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20203 canonical += 2;
20204 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20205 canonical += 2;
20206 else if (TOLOWER (canonical[0]) == 'r')
20207 canonical += 1;
20208
20209 return mips_strict_matching_cpu_name_p (canonical, given);
20210}
20211
20212
20213/* Parse an option that takes the name of a processor as its argument.
20214 OPTION is the name of the option and CPU_STRING is the argument.
20215 Return the corresponding processor enumeration if the CPU_STRING is
20216 recognized, otherwise report an error and return null.
20217
20218 A similar function exists in GCC. */
e7af610e
NC
20219
20220static const struct mips_cpu_info *
17a2f251 20221mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20222{
316f5878 20223 const struct mips_cpu_info *p;
e7af610e 20224
316f5878
RS
20225 /* 'from-abi' selects the most compatible architecture for the given
20226 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20227 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20228 version. Look first at the -mgp options, if given, otherwise base
20229 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20230
316f5878
RS
20231 Treat NO_ABI like the EABIs. One reason to do this is that the
20232 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20233 architecture. This code picks MIPS I for 'mips' and MIPS III for
20234 'mips64', just as we did in the days before 'from-abi'. */
20235 if (strcasecmp (cpu_string, "from-abi") == 0)
20236 {
20237 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20238 return mips_cpu_info_from_isa (ISA_MIPS1);
20239
20240 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20241 return mips_cpu_info_from_isa (ISA_MIPS3);
20242
bad1aba3 20243 if (file_mips_opts.gp >= 0)
20244 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20245 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20246
20247 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20248 ? ISA_MIPS3
20249 : ISA_MIPS1);
20250 }
20251
20252 /* 'default' has traditionally been a no-op. Probably not very useful. */
20253 if (strcasecmp (cpu_string, "default") == 0)
20254 return 0;
20255
20256 for (p = mips_cpu_info_table; p->name != 0; p++)
20257 if (mips_matching_cpu_name_p (p->name, cpu_string))
20258 return p;
20259
1661c76c 20260 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20261 return 0;
e7af610e
NC
20262}
20263
316f5878
RS
20264/* Return the canonical processor information for ISA (a member of the
20265 ISA_MIPS* enumeration). */
20266
e7af610e 20267static const struct mips_cpu_info *
17a2f251 20268mips_cpu_info_from_isa (int isa)
e7af610e
NC
20269{
20270 int i;
20271
20272 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20273 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20274 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20275 return (&mips_cpu_info_table[i]);
20276
e972090a 20277 return NULL;
e7af610e 20278}
fef14a42
TS
20279
20280static const struct mips_cpu_info *
17a2f251 20281mips_cpu_info_from_arch (int arch)
fef14a42
TS
20282{
20283 int i;
20284
20285 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20286 if (arch == mips_cpu_info_table[i].cpu)
20287 return (&mips_cpu_info_table[i]);
20288
20289 return NULL;
20290}
316f5878
RS
20291\f
20292static void
17a2f251 20293show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20294{
20295 if (*first_p)
20296 {
20297 fprintf (stream, "%24s", "");
20298 *col_p = 24;
20299 }
20300 else
20301 {
20302 fprintf (stream, ", ");
20303 *col_p += 2;
20304 }
e7af610e 20305
316f5878
RS
20306 if (*col_p + strlen (string) > 72)
20307 {
20308 fprintf (stream, "\n%24s", "");
20309 *col_p = 24;
20310 }
20311
20312 fprintf (stream, "%s", string);
20313 *col_p += strlen (string);
20314
20315 *first_p = 0;
20316}
20317
20318void
17a2f251 20319md_show_usage (FILE *stream)
e7af610e 20320{
316f5878
RS
20321 int column, first;
20322 size_t i;
20323
20324 fprintf (stream, _("\
20325MIPS options:\n\
316f5878
RS
20326-EB generate big endian output\n\
20327-EL generate little endian output\n\
20328-g, -g2 do not remove unneeded NOPs or swap branches\n\
20329-G NUM allow referencing objects up to NUM bytes\n\
20330 implicitly with the gp register [default 8]\n"));
20331 fprintf (stream, _("\
20332-mips1 generate MIPS ISA I instructions\n\
20333-mips2 generate MIPS ISA II instructions\n\
20334-mips3 generate MIPS ISA III instructions\n\
20335-mips4 generate MIPS ISA IV instructions\n\
20336-mips5 generate MIPS ISA V instructions\n\
20337-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20338-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20339-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20340-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20341-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20342-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20343-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20344-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20345-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20346-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20347-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20348
20349 first = 1;
e7af610e
NC
20350
20351 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20352 show (stream, mips_cpu_info_table[i].name, &column, &first);
20353 show (stream, "from-abi", &column, &first);
20354 fputc ('\n', stream);
e7af610e 20355
316f5878
RS
20356 fprintf (stream, _("\
20357-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20358-no-mCPU don't generate code specific to CPU.\n\
20359 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20360
20361 first = 1;
20362
20363 show (stream, "3900", &column, &first);
20364 show (stream, "4010", &column, &first);
20365 show (stream, "4100", &column, &first);
20366 show (stream, "4650", &column, &first);
20367 fputc ('\n', stream);
20368
20369 fprintf (stream, _("\
20370-mips16 generate mips16 instructions\n\
20371-no-mips16 do not generate mips16 instructions\n"));
20372 fprintf (stream, _("\
f866b262
MR
20373-mmips16e2 generate MIPS16e2 instructions\n\
20374-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20375 fprintf (stream, _("\
df58fc94
RS
20376-mmicromips generate microMIPS instructions\n\
20377-mno-micromips do not generate microMIPS instructions\n"));
20378 fprintf (stream, _("\
e16bfa71 20379-msmartmips generate smartmips instructions\n\
3739860c 20380-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20381 fprintf (stream, _("\
74cd071d
CF
20382-mdsp generate DSP instructions\n\
20383-mno-dsp do not generate DSP instructions\n"));
20384 fprintf (stream, _("\
8b082fb1
TS
20385-mdspr2 generate DSP R2 instructions\n\
20386-mno-dspr2 do not generate DSP R2 instructions\n"));
20387 fprintf (stream, _("\
8f4f9071
MF
20388-mdspr3 generate DSP R3 instructions\n\
20389-mno-dspr3 do not generate DSP R3 instructions\n"));
20390 fprintf (stream, _("\
ef2e4d86
CF
20391-mmt generate MT instructions\n\
20392-mno-mt do not generate MT instructions\n"));
20393 fprintf (stream, _("\
dec0624d
MR
20394-mmcu generate MCU instructions\n\
20395-mno-mcu do not generate MCU instructions\n"));
20396 fprintf (stream, _("\
56d438b1
CF
20397-mmsa generate MSA instructions\n\
20398-mno-msa do not generate MSA instructions\n"));
20399 fprintf (stream, _("\
7d64c587
AB
20400-mxpa generate eXtended Physical Address (XPA) instructions\n\
20401-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20402 fprintf (stream, _("\
b015e599
AP
20403-mvirt generate Virtualization instructions\n\
20404-mno-virt do not generate Virtualization instructions\n"));
20405 fprintf (stream, _("\
730c3174
SE
20406-mcrc generate CRC instructions\n\
20407-mno-crc do not generate CRC instructions\n"));
20408 fprintf (stream, _("\
6f20c942
FS
20409-mginv generate Global INValidate (GINV) instructions\n\
20410-mno-ginv do not generate Global INValidate instructions\n"));
20411 fprintf (stream, _("\
8095d2f7
CX
20412-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20413-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20414 fprintf (stream, _("\
716c08de
CX
20415-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20416-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20417 fprintf (stream, _("\
bdc6c06e
CX
20418-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20419-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20420 fprintf (stream, _("\
a693765e
CX
20421-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20422-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20423 fprintf (stream, _("\
833794fc
MR
20424-minsn32 only generate 32-bit microMIPS instructions\n\
20425-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20426#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20427 fprintf (stream, _("\
20428-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20429-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20430#else
20431 fprintf (stream, _("\
20432-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20433-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20434#endif
833794fc 20435 fprintf (stream, _("\
c67a084a
NC
20436-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20437-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20438-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20439-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20440-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20441-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20442-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20443-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20444-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20445-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20446-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20447-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20448-O0 do not remove unneeded NOPs, do not swap branches\n\
20449-O, -O1 remove unneeded NOPs, do not swap branches\n\
20450-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20451--trap, --no-break trap exception on div by 0 and mult overflow\n\
20452--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20453 fprintf (stream, _("\
20454-mhard-float allow floating-point instructions\n\
20455-msoft-float do not allow floating-point instructions\n\
20456-msingle-float only allow 32-bit floating-point operations\n\
20457-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20458--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20459--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20460-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20461-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20462-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20463
20464 first = 1;
20465
20466 show (stream, "legacy", &column, &first);
20467 show (stream, "2008", &column, &first);
20468
20469 fputc ('\n', stream);
20470
316f5878
RS
20471 fprintf (stream, _("\
20472-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20473-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20474-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20475-non_shared do not generate code that can operate with DSOs\n\
316f5878 20476-xgot assume a 32 bit GOT\n\
dcd410fe 20477-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20478-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20479 position dependent (non shared) code\n\
316f5878
RS
20480-mabi=ABI create ABI conformant object file for:\n"));
20481
20482 first = 1;
20483
20484 show (stream, "32", &column, &first);
20485 show (stream, "o64", &column, &first);
20486 show (stream, "n32", &column, &first);
20487 show (stream, "64", &column, &first);
20488 show (stream, "eabi", &column, &first);
20489
20490 fputc ('\n', stream);
20491
20492 fprintf (stream, _("\
b4f6242e
MR
20493-32 create o32 ABI object file%s\n"),
20494 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20495 fprintf (stream, _("\
20496-n32 create n32 ABI object file%s\n"),
20497 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20498 fprintf (stream, _("\
20499-64 create 64 ABI object file%s\n"),
20500 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20501}
14e777e0 20502
1575952e 20503#ifdef TE_IRIX
14e777e0 20504enum dwarf2_format
413a266c 20505mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20506{
369943fe 20507 if (HAVE_64BIT_SYMBOLS)
1575952e 20508 return dwarf2_format_64bit_irix;
14e777e0
KB
20509 else
20510 return dwarf2_format_32bit;
20511}
1575952e 20512#endif
73369e65
EC
20513
20514int
20515mips_dwarf2_addr_size (void)
20516{
6b6b3450 20517 if (HAVE_64BIT_OBJECTS)
73369e65 20518 return 8;
73369e65
EC
20519 else
20520 return 4;
20521}
5862107c
EC
20522
20523/* Standard calling conventions leave the CFA at SP on entry. */
20524void
20525mips_cfi_frame_initial_instructions (void)
20526{
20527 cfi_add_CFA_def_cfa_register (SP);
20528}
20529
707bfff6
TS
20530int
20531tc_mips_regname_to_dw2regnum (char *regname)
20532{
20533 unsigned int regnum = -1;
20534 unsigned int reg;
20535
20536 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20537 regnum = reg;
20538
20539 return regnum;
20540}
263b2574 20541
20542/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20543 Given a symbolic attribute NAME, return the proper integer value.
20544 Returns -1 if the attribute is not known. */
20545
20546int
20547mips_convert_symbolic_attribute (const char *name)
20548{
20549 static const struct
20550 {
20551 const char * name;
20552 const int tag;
20553 }
20554 attribute_table[] =
20555 {
20556#define T(tag) {#tag, tag}
20557 T (Tag_GNU_MIPS_ABI_FP),
20558 T (Tag_GNU_MIPS_ABI_MSA),
20559#undef T
20560 };
20561 unsigned int i;
20562
20563 if (name == NULL)
20564 return -1;
20565
20566 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20567 if (streq (name, attribute_table[i].name))
20568 return attribute_table[i].tag;
20569
20570 return -1;
20571}
fd5c94ab
RS
20572
20573void
ed2917de 20574mips_md_finish (void)
fd5c94ab 20575{
351cdf24
MF
20576 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20577
fd5c94ab
RS
20578 mips_emit_delays ();
20579 if (cur_proc_ptr)
20580 as_warn (_("missing .end at end of assembly"));
919731af 20581
20582 /* Just in case no code was emitted, do the consistency check. */
20583 file_mips_check_options ();
351cdf24
MF
20584
20585 /* Set a floating-point ABI if the user did not. */
20586 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20587 {
20588 /* Perform consistency checks on the floating-point ABI. */
20589 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20590 Tag_GNU_MIPS_ABI_FP);
20591 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20592 check_fpabi (fpabi);
20593 }
20594 else
20595 {
20596 /* Soft-float gets precedence over single-float, the two options should
20597 not be used together so this should not matter. */
20598 if (file_mips_opts.soft_float == 1)
20599 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20600 /* Single-float gets precedence over all double_float cases. */
20601 else if (file_mips_opts.single_float == 1)
20602 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20603 else
20604 {
20605 switch (file_mips_opts.fp)
20606 {
20607 case 32:
20608 if (file_mips_opts.gp == 32)
20609 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20610 break;
20611 case 0:
20612 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20613 break;
20614 case 64:
20615 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20616 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20617 else if (file_mips_opts.gp == 32)
20618 fpabi = Val_GNU_MIPS_ABI_FP_64;
20619 else
20620 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20621 break;
20622 }
20623 }
20624
20625 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20626 Tag_GNU_MIPS_ABI_FP, fpabi);
20627 }
fd5c94ab 20628}
2f0c68f2
CM
20629
20630/* Returns the relocation type required for a particular CFI encoding. */
20631
20632bfd_reloc_code_real_type
20633mips_cfi_reloc_for_encoding (int encoding)
20634{
20635 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20636 return BFD_RELOC_32_PCREL;
20637 else return BFD_RELOC_NONE;
20638}