]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
Revert "2.41 Release sources"
[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 \
df18f71b 440 || (CPU) == CPU_ALLEGREX \
351cdf24 441 || (CPU) == CPU_R5900) \
bd782c07 442 && ((CPU) != CPU_GS464 \
9108bc33
CX
443 || (CPU) != CPU_GS464E \
444 || (CPU) != CPU_GS264E))
af7ee8bf 445
ad3fea08
TS
446/* Return true if ISA supports move to/from high part of a 64-bit
447 floating-point register. */
448#define ISA_HAS_MXHC1(ISA) \
449 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
450 || (ISA) == ISA_MIPS32R3 \
451 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
452 || (ISA) == ISA_MIPS32R6 \
453 || (ISA) == ISA_MIPS64R2 \
454 || (ISA) == ISA_MIPS64R3 \
455 || (ISA) == ISA_MIPS64R5 \
456 || (ISA) == ISA_MIPS64R6)
457
458/* Return true if ISA supports legacy NAN. */
459#define ISA_HAS_LEGACY_NAN(ISA) \
460 ((ISA) == ISA_MIPS1 \
461 || (ISA) == ISA_MIPS2 \
462 || (ISA) == ISA_MIPS3 \
463 || (ISA) == ISA_MIPS4 \
464 || (ISA) == ISA_MIPS5 \
465 || (ISA) == ISA_MIPS32 \
466 || (ISA) == ISA_MIPS32R2 \
467 || (ISA) == ISA_MIPS32R3 \
468 || (ISA) == ISA_MIPS32R5 \
469 || (ISA) == ISA_MIPS64 \
ae52f483
AB
470 || (ISA) == ISA_MIPS64R2 \
471 || (ISA) == ISA_MIPS64R3 \
472 || (ISA) == ISA_MIPS64R5)
ad3fea08 473
bad1aba3 474#define GPR_SIZE \
475 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
476 ? 32 \
477 : mips_opts.gp)
ca4e0257 478
bad1aba3 479#define FPR_SIZE \
480 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
481 ? 32 \
482 : mips_opts.fp)
ca4e0257 483
316f5878 484#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 485
316f5878 486#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 487
3b91255e
RS
488/* True if relocations are stored in-place. */
489#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
490
aed1a261
RS
491/* The ABI-derived address size. */
492#define HAVE_64BIT_ADDRESSES \
bad1aba3 493 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 494#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 495
aed1a261
RS
496/* The size of symbolic constants (i.e., expressions of the form
497 "SYMBOL" or "SYMBOL + OFFSET"). */
498#define HAVE_32BIT_SYMBOLS \
499 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
500#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 501
b7c7d6c1
TS
502/* Addresses are loaded in different ways, depending on the address size
503 in use. The n32 ABI Documentation also mandates the use of additions
504 with overflow checking, but existing implementations don't follow it. */
f899b4b8 505#define ADDRESS_ADD_INSN \
b7c7d6c1 506 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
507
508#define ADDRESS_ADDI_INSN \
b7c7d6c1 509 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
510
511#define ADDRESS_LOAD_INSN \
512 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
513
514#define ADDRESS_STORE_INSN \
515 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
516
a4672219 517/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36 518#define CPU_HAS_MIPS16(cpu) \
d34049e8
ML
519 (startswith (TARGET_CPU, "mips16") \
520 || startswith (TARGET_CANONICAL, "mips-lsi-elf"))
a4672219 521
2309ddf2 522/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
523#define CPU_HAS_MICROMIPS(cpu) 0
524
60b63b72
RS
525/* True if CPU has a dror instruction. */
526#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
527
528/* True if CPU has a ror instruction. */
d29b94fc 529#define CPU_HAS_ROR(CPU) (CPU_HAS_DROR (CPU) || (CPU) == CPU_ALLEGREX)
60b63b72 530
6f2117ba 531/* True if CPU is in the Octeon family. */
2c629856
N
532#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
533 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 534
dd3cbb7e 535/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 536#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 537
0aa27725 538/* True, if CPU has support for ldc1 and sdc1. */
df18f71b
DGF
539#define CPU_HAS_LDC1_SDC1(CPU) (mips_opts.isa != ISA_MIPS1 \
540 && (CPU) != CPU_ALLEGREX \
541 && (CPU) != CPU_R5900)
0aa27725 542
c8978940
CD
543/* True if mflo and mfhi can be immediately followed by instructions
544 which write to the HI and LO registers.
545
546 According to MIPS specifications, MIPS ISAs I, II, and III need
547 (at least) two instructions between the reads of HI/LO and
548 instructions which write them, and later ISAs do not. Contradicting
549 the MIPS specifications, some MIPS IV processor user manuals (e.g.
550 the UM for the NEC Vr5000) document needing the instructions between
551 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
552 MIPS64 and later ISAs to have the interlocks, plus any specific
553 earlier-ISA CPUs for which CPU documentation declares that the
554 instructions are really interlocked. */
555#define hilo_interlocks \
556 (mips_opts.isa == ISA_MIPS32 \
557 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
558 || mips_opts.isa == ISA_MIPS32R3 \
559 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 560 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
561 || mips_opts.isa == ISA_MIPS64 \
562 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
563 || mips_opts.isa == ISA_MIPS64R3 \
564 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 565 || mips_opts.isa == ISA_MIPS64R6 \
df18f71b 566 || mips_opts.arch == CPU_ALLEGREX \
c8978940 567 || mips_opts.arch == CPU_R4010 \
e407c74b 568 || mips_opts.arch == CPU_R5900 \
c8978940
CD
569 || mips_opts.arch == CPU_R10000 \
570 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
571 || mips_opts.arch == CPU_R14000 \
572 || mips_opts.arch == CPU_R16000 \
c8978940 573 || mips_opts.arch == CPU_RM7000 \
c8978940 574 || mips_opts.arch == CPU_VR5500 \
df58fc94 575 || mips_opts.micromips \
c8978940 576 )
252b5132
RH
577
578/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
579 from the GPRs after they are loaded from memory, and thus does not
580 require nops to be inserted. This applies to instructions marked
67dc82bc 581 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
582 level I and microMIPS mode instructions are always interlocked. */
583#define gpr_interlocks \
584 (mips_opts.isa != ISA_MIPS1 \
585 || mips_opts.arch == CPU_R3900 \
e407c74b 586 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
587 || mips_opts.micromips \
588 )
252b5132 589
81912461
ILT
590/* Whether the processor uses hardware interlocks to avoid delays
591 required by coprocessor instructions, and thus does not require
592 nops to be inserted. This applies to instructions marked
43885403
MF
593 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
594 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 595 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
596 levels I, II, and III and microMIPS mode instructions are always
597 interlocked. */
bdaaa2e1 598/* Itbl support may require additional care here. */
81912461
ILT
599#define cop_interlocks \
600 ((mips_opts.isa != ISA_MIPS1 \
601 && mips_opts.isa != ISA_MIPS2 \
602 && mips_opts.isa != ISA_MIPS3) \
603 || mips_opts.arch == CPU_R4300 \
df58fc94 604 || mips_opts.micromips \
81912461
ILT
605 )
606
607/* Whether the processor uses hardware interlocks to protect reads
608 from coprocessor registers after they are loaded from memory, and
609 thus does not require nops to be inserted. This applies to
610 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
611 requires at MIPS ISA level I and microMIPS mode instructions are
612 always interlocked. */
613#define cop_mem_interlocks \
614 (mips_opts.isa != ISA_MIPS1 \
615 || mips_opts.micromips \
616 )
252b5132 617
6b76fefe
CM
618/* Is this a mfhi or mflo instruction? */
619#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
620 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
621
df58fc94
RS
622/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
623 has been selected. This implies, in particular, that addresses of text
624 labels have their LSB set. */
625#define HAVE_CODE_COMPRESSION \
626 ((mips_opts.mips16 | mips_opts.micromips) != 0)
627
42429eac 628/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 629#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
630#define GPR_SMIN (-GPR_SMAX - 1)
631
252b5132
RH
632/* MIPS PIC level. */
633
a161fe53 634enum mips_pic_level mips_pic;
252b5132 635
c9914766 636/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 637 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 638static int mips_big_got = 0;
252b5132
RH
639
640/* 1 if trap instructions should used for overflow rather than break
641 instructions. */
c9914766 642static int mips_trap = 0;
252b5132 643
119d663a 644/* 1 if double width floating point constants should not be constructed
b6ff326e 645 by assembling two single width halves into two single width floating
119d663a
NC
646 point registers which just happen to alias the double width destination
647 register. On some architectures this aliasing can be disabled by a bit
d547a75e 648 in the status register, and the setting of this bit cannot be determined
119d663a
NC
649 automatically at assemble time. */
650static int mips_disable_float_construction;
651
252b5132
RH
652/* Non-zero if any .set noreorder directives were used. */
653
654static int mips_any_noreorder;
655
6b76fefe
CM
656/* Non-zero if nops should be inserted when the register referenced in
657 an mfhi/mflo instruction is read in the next two instructions. */
658static int mips_7000_hilo_fix;
659
02ffd3e4 660/* The size of objects in the small data section. */
156c2f8b 661static unsigned int g_switch_value = 8;
252b5132
RH
662/* Whether the -G option was used. */
663static int g_switch_seen = 0;
664
665#define N_RMASK 0xc4
666#define N_VFP 0xd4
667
668/* If we can determine in advance that GP optimization won't be
669 possible, we can skip the relaxation stuff that tries to produce
670 GP-relative references. This makes delay slot optimization work
671 better.
672
673 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
674 gcc output. It needs to guess right for gcc, otherwise gcc
675 will put what it thinks is a GP-relative instruction in a branch
676 delay slot.
252b5132
RH
677
678 I don't know if a fix is needed for the SVR4_PIC mode. I've only
679 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 680static int nopic_need_relax (symbolS *, int);
252b5132 681
6f2117ba 682/* Handle of the OPCODE hash table. */
629310ab 683static htab_t op_hash = NULL;
252b5132
RH
684
685/* The opcode hash table we use for the mips16. */
629310ab 686static htab_t mips16_op_hash = NULL;
252b5132 687
df58fc94 688/* The opcode hash table we use for the microMIPS ASE. */
629310ab 689static htab_t micromips_op_hash = NULL;
df58fc94 690
252b5132 691/* This array holds the chars that always start a comment. If the
6f2117ba 692 pre-processor is disabled, these aren't very useful. */
252b5132
RH
693const char comment_chars[] = "#";
694
695/* This array holds the chars that only start a comment at the beginning of
696 a line. If the line seems to have the form '# 123 filename'
6f2117ba 697 .line and .file directives will appear in the pre-processed output. */
252b5132
RH
698/* Note that input_file.c hand checks for '#' at the beginning of the
699 first line of the input file. This is because the compiler outputs
bdaaa2e1 700 #NO_APP at the beginning of its output. */
252b5132
RH
701/* Also note that C style comments are always supported. */
702const char line_comment_chars[] = "#";
703
bdaaa2e1 704/* This array holds machine specific line separator characters. */
63a0b638 705const char line_separator_chars[] = ";";
252b5132 706
6f2117ba 707/* Chars that can be used to separate mant from exp in floating point nums. */
252b5132
RH
708const char EXP_CHARS[] = "eE";
709
6f2117ba
PH
710/* Chars that mean this number is a floating point constant.
711 As in 0f12.456
712 or 0d1.2345e12. */
252b5132
RH
713const char FLT_CHARS[] = "rRsSfFdDxXpP";
714
715/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
716 changed in read.c . Ideally it shouldn't have to know about it at all,
6f2117ba 717 but nothing is ideal around here. */
252b5132 718
e3de51ce 719/* Types of printf format used for instruction-related error messages.
6f2117ba
PH
720 "I" means int ("%d") and "S" means string ("%s"). */
721enum mips_insn_error_format
722{
e3de51ce
RS
723 ERR_FMT_PLAIN,
724 ERR_FMT_I,
725 ERR_FMT_SS,
726};
727
728/* Information about an error that was found while assembling the current
729 instruction. */
6f2117ba
PH
730struct mips_insn_error
731{
e3de51ce
RS
732 /* We sometimes need to match an instruction against more than one
733 opcode table entry. Errors found during this matching are reported
734 against a particular syntactic argument rather than against the
735 instruction as a whole. We grade these messages so that errors
736 against argument N have a greater priority than an error against
737 any argument < N, since the former implies that arguments up to N
738 were acceptable and that the opcode entry was therefore a closer match.
739 If several matches report an error against the same argument,
740 we only use that error if it is the same in all cases.
741
742 min_argnum is the minimum argument number for which an error message
743 should be accepted. It is 0 if MSG is against the instruction as
744 a whole. */
745 int min_argnum;
746
747 /* The printf()-style message, including its format and arguments. */
748 enum mips_insn_error_format format;
749 const char *msg;
6f2117ba
PH
750 union
751 {
e3de51ce
RS
752 int i;
753 const char *ss[2];
754 } u;
755};
756
757/* The error that should be reported for the current instruction. */
758static struct mips_insn_error insn_error;
252b5132
RH
759
760static int auto_align = 1;
761
762/* When outputting SVR4 PIC code, the assembler needs to know the
763 offset in the stack frame from which to restore the $gp register.
764 This is set by the .cprestore pseudo-op, and saved in this
765 variable. */
766static offsetT mips_cprestore_offset = -1;
767
67c1ffbe 768/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 769 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 770 offset and even an other register than $gp as global pointer. */
6478892d
TS
771static offsetT mips_cpreturn_offset = -1;
772static int mips_cpreturn_register = -1;
773static int mips_gp_register = GP;
def2e0dd 774static int mips_gprel_offset = 0;
6478892d 775
7a621144
DJ
776/* Whether mips_cprestore_offset has been set in the current function
777 (or whether it has already been warned about, if not). */
778static int mips_cprestore_valid = 0;
779
252b5132
RH
780/* This is the register which holds the stack frame, as set by the
781 .frame pseudo-op. This is needed to implement .cprestore. */
782static int mips_frame_reg = SP;
783
7a621144
DJ
784/* Whether mips_frame_reg has been set in the current function
785 (or whether it has already been warned about, if not). */
786static int mips_frame_reg_valid = 0;
787
252b5132
RH
788/* To output NOP instructions correctly, we need to keep information
789 about the previous two instructions. */
790
791/* Whether we are optimizing. The default value of 2 means to remove
792 unneeded NOPs and swap branch instructions when possible. A value
793 of 1 means to not swap branches. A value of 0 means to always
794 insert NOPs. */
795static int mips_optimize = 2;
796
797/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
798 equivalent to seeing no -g option at all. */
799static int mips_debug = 0;
800
7d8e00cf
RS
801/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
802#define MAX_VR4130_NOPS 4
803
804/* The maximum number of NOPs needed to fill delay slots. */
805#define MAX_DELAY_NOPS 2
806
807/* The maximum number of NOPs needed for any purpose. */
808#define MAX_NOPS 4
71400594 809
6f2117ba
PH
810/* The maximum range of context length of ll/sc. */
811#define MAX_LLSC_RANGE 20
812
71400594
RS
813/* A list of previous instructions, with index 0 being the most recent.
814 We need to look back MAX_NOPS instructions when filling delay slots
815 or working around processor errata. We need to look back one
816 instruction further if we're thinking about using history[0] to
817 fill a branch delay slot. */
6f2117ba 818static struct mips_cl_insn history[1 + MAX_NOPS + MAX_LLSC_RANGE];
252b5132 819
dec7b24b
YS
820/* The maximum number of LABELS detect for the same address. */
821#define MAX_LABELS_SAME 10
822
fc76e730 823/* Arrays of operands for each instruction. */
14daeee3 824#define MAX_OPERANDS 6
6f2117ba
PH
825struct mips_operand_array
826{
fc76e730
RS
827 const struct mips_operand *operand[MAX_OPERANDS];
828};
829static struct mips_operand_array *mips_operands;
830static struct mips_operand_array *mips16_operands;
831static struct mips_operand_array *micromips_operands;
832
1e915849 833/* Nop instructions used by emit_nop. */
df58fc94
RS
834static struct mips_cl_insn nop_insn;
835static struct mips_cl_insn mips16_nop_insn;
836static struct mips_cl_insn micromips_nop16_insn;
837static struct mips_cl_insn micromips_nop32_insn;
1e915849 838
6f2117ba
PH
839/* Sync instructions used by insert sync. */
840static struct mips_cl_insn sync_insn;
841
1e915849 842/* The appropriate nop for the current mode. */
833794fc
MR
843#define NOP_INSN (mips_opts.mips16 \
844 ? &mips16_nop_insn \
845 : (mips_opts.micromips \
846 ? (mips_opts.insn32 \
847 ? &micromips_nop32_insn \
848 : &micromips_nop16_insn) \
849 : &nop_insn))
df58fc94
RS
850
851/* The size of NOP_INSN in bytes. */
833794fc
MR
852#define NOP_INSN_SIZE ((mips_opts.mips16 \
853 || (mips_opts.micromips && !mips_opts.insn32)) \
854 ? 2 : 4)
252b5132 855
252b5132
RH
856/* If this is set, it points to a frag holding nop instructions which
857 were inserted before the start of a noreorder section. If those
858 nops turn out to be unnecessary, the size of the frag can be
859 decreased. */
860static fragS *prev_nop_frag;
861
862/* The number of nop instructions we created in prev_nop_frag. */
863static int prev_nop_frag_holds;
864
865/* The number of nop instructions that we know we need in
bdaaa2e1 866 prev_nop_frag. */
252b5132
RH
867static int prev_nop_frag_required;
868
869/* The number of instructions we've seen since prev_nop_frag. */
870static int prev_nop_frag_since;
871
e8044f35
RS
872/* Relocations against symbols are sometimes done in two parts, with a HI
873 relocation and a LO relocation. Each relocation has only 16 bits of
874 space to store an addend. This means that in order for the linker to
875 handle carries correctly, it must be able to locate both the HI and
876 the LO relocation. This means that the relocations must appear in
877 order in the relocation table.
252b5132
RH
878
879 In order to implement this, we keep track of each unmatched HI
880 relocation. We then sort them so that they immediately precede the
bdaaa2e1 881 corresponding LO relocation. */
252b5132 882
e972090a
NC
883struct mips_hi_fixup
884{
252b5132
RH
885 /* Next HI fixup. */
886 struct mips_hi_fixup *next;
887 /* This fixup. */
888 fixS *fixp;
889 /* The section this fixup is in. */
890 segT seg;
891};
892
893/* The list of unmatched HI relocs. */
894
895static struct mips_hi_fixup *mips_hi_fixup_list;
896
252b5132
RH
897/* Map mips16 register numbers to normal MIPS register numbers. */
898
e972090a
NC
899static const unsigned int mips16_to_32_reg_map[] =
900{
252b5132
RH
901 16, 17, 2, 3, 4, 5, 6, 7
902};
60b63b72 903
df58fc94
RS
904/* Map microMIPS register numbers to normal MIPS register numbers. */
905
df58fc94 906#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
907
908/* The microMIPS registers with type h. */
e76ff5ab 909static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
910{
911 5, 5, 6, 4, 4, 4, 4, 4
912};
e76ff5ab 913static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
914{
915 6, 7, 7, 21, 22, 5, 6, 7
916};
917
df58fc94
RS
918/* The microMIPS registers with type m. */
919static const unsigned int micromips_to_32_reg_m_map[] =
920{
921 0, 17, 2, 3, 16, 18, 19, 20
922};
923
924#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
925
71400594
RS
926/* Classifies the kind of instructions we're interested in when
927 implementing -mfix-vr4120. */
c67a084a
NC
928enum fix_vr4120_class
929{
71400594
RS
930 FIX_VR4120_MACC,
931 FIX_VR4120_DMACC,
932 FIX_VR4120_MULT,
933 FIX_VR4120_DMULT,
934 FIX_VR4120_DIV,
935 FIX_VR4120_MTHILO,
936 NUM_FIX_VR4120_CLASSES
937};
938
c67a084a 939/* ...likewise -mfix-loongson2f-jump. */
5b7c81bd 940static bool mips_fix_loongson2f_jump;
c67a084a
NC
941
942/* ...likewise -mfix-loongson2f-nop. */
5b7c81bd 943static bool mips_fix_loongson2f_nop;
c67a084a
NC
944
945/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
5b7c81bd 946static bool mips_fix_loongson2f;
c67a084a 947
71400594
RS
948/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
949 there must be at least one other instruction between an instruction
950 of type X and an instruction of type Y. */
951static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
952
953/* True if -mfix-vr4120 is in force. */
d766e8ec 954static int mips_fix_vr4120;
4a6a3df4 955
7d8e00cf
RS
956/* ...likewise -mfix-vr4130. */
957static int mips_fix_vr4130;
958
6a32d874
CM
959/* ...likewise -mfix-24k. */
960static int mips_fix_24k;
961
a8d14a88
CM
962/* ...likewise -mfix-rm7000 */
963static int mips_fix_rm7000;
964
d954098f 965/* ...likewise -mfix-cn63xxp1 */
5b7c81bd 966static bool mips_fix_cn63xxp1;
d954098f 967
27c634e0 968/* ...likewise -mfix-r5900 */
5b7c81bd
AM
969static bool mips_fix_r5900;
970static bool mips_fix_r5900_explicit;
27c634e0 971
6f2117ba 972/* ...likewise -mfix-loongson3-llsc. */
5b7c81bd 973static bool mips_fix_loongson3_llsc = DEFAULT_MIPS_FIX_LOONGSON3_LLSC;
6f2117ba 974
4a6a3df4
AO
975/* We don't relax branches by default, since this causes us to expand
976 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
977 fail to compute the offset before expanding the macro to the most
978 efficient expansion. */
979
980static int mips_relax_branch;
8b10b0b3
MR
981
982/* TRUE if checks are suppressed for invalid branches between ISA modes.
983 Needed for broken assembly produced by some GCC versions and some
984 sloppy code out there, where branches to data labels are present. */
5b7c81bd 985static bool mips_ignore_branch_isa;
252b5132 986\f
4d7206a2
RS
987/* The expansion of many macros depends on the type of symbol that
988 they refer to. For example, when generating position-dependent code,
989 a macro that refers to a symbol may have two different expansions,
990 one which uses GP-relative addresses and one which uses absolute
991 addresses. When generating SVR4-style PIC, a macro may have
992 different expansions for local and global symbols.
993
994 We handle these situations by generating both sequences and putting
995 them in variant frags. In position-dependent code, the first sequence
996 will be the GP-relative one and the second sequence will be the
997 absolute one. In SVR4 PIC, the first sequence will be for global
998 symbols and the second will be for local symbols.
999
584892a6
RS
1000 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
1001 SECOND are the lengths of the two sequences in bytes. These fields
1002 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
1003 the subtype has the following flags:
4d7206a2 1004
ce8ad872
MR
1005 RELAX_PIC
1006 Set if generating PIC code.
1007
584892a6
RS
1008 RELAX_USE_SECOND
1009 Set if it has been decided that we should use the second
1010 sequence instead of the first.
1011
1012 RELAX_SECOND_LONGER
1013 Set in the first variant frag if the macro's second implementation
1014 is longer than its first. This refers to the macro as a whole,
1015 not an individual relaxation.
1016
1017 RELAX_NOMACRO
1018 Set in the first variant frag if the macro appeared in a .set nomacro
1019 block and if one alternative requires a warning but the other does not.
1020
1021 RELAX_DELAY_SLOT
1022 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
1023 delay slot.
4d7206a2 1024
df58fc94
RS
1025 RELAX_DELAY_SLOT_16BIT
1026 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
1027 16-bit instruction.
1028
1029 RELAX_DELAY_SLOT_SIZE_FIRST
1030 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
1031 the macro is of the wrong size for the branch delay slot.
1032
1033 RELAX_DELAY_SLOT_SIZE_SECOND
1034 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1035 the macro is of the wrong size for the branch delay slot.
1036
4d7206a2
RS
1037 The frag's "opcode" points to the first fixup for relaxable code.
1038
1039 Relaxable macros are generated using a sequence such as:
1040
1041 relax_start (SYMBOL);
1042 ... generate first expansion ...
1043 relax_switch ();
1044 ... generate second expansion ...
1045 relax_end ();
1046
1047 The code and fixups for the unwanted alternative are discarded
1048 by md_convert_frag. */
ce8ad872
MR
1049#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1050 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1051
584892a6
RS
1052#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1053#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1054#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1055#define RELAX_USE_SECOND 0x20000
1056#define RELAX_SECOND_LONGER 0x40000
1057#define RELAX_NOMACRO 0x80000
1058#define RELAX_DELAY_SLOT 0x100000
1059#define RELAX_DELAY_SLOT_16BIT 0x200000
1060#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1061#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1062
4a6a3df4
AO
1063/* Branch without likely bit. If label is out of range, we turn:
1064
134c0c8b 1065 beq reg1, reg2, label
4a6a3df4
AO
1066 delay slot
1067
1068 into
1069
1070 bne reg1, reg2, 0f
1071 nop
1072 j label
1073 0: delay slot
1074
1075 with the following opcode replacements:
1076
1077 beq <-> bne
1078 blez <-> bgtz
1079 bltz <-> bgez
1080 bc1f <-> bc1t
1081
1082 bltzal <-> bgezal (with jal label instead of j label)
1083
1084 Even though keeping the delay slot instruction in the delay slot of
1085 the branch would be more efficient, it would be very tricky to do
1086 correctly, because we'd have to introduce a variable frag *after*
1087 the delay slot instruction, and expand that instead. Let's do it
1088 the easy way for now, even if the branch-not-taken case now costs
1089 one additional instruction. Out-of-range branches are not supposed
1090 to be common, anyway.
1091
1092 Branch likely. If label is out of range, we turn:
1093
1094 beql reg1, reg2, label
1095 delay slot (annulled if branch not taken)
1096
1097 into
1098
1099 beql reg1, reg2, 1f
1100 nop
1101 beql $0, $0, 2f
1102 nop
1103 1: j[al] label
1104 delay slot (executed only if branch taken)
1105 2:
1106
1107 It would be possible to generate a shorter sequence by losing the
1108 likely bit, generating something like:
b34976b6 1109
4a6a3df4
AO
1110 bne reg1, reg2, 0f
1111 nop
1112 j[al] label
1113 delay slot (executed only if branch taken)
1114 0:
1115
1116 beql -> bne
1117 bnel -> beq
1118 blezl -> bgtz
1119 bgtzl -> blez
1120 bltzl -> bgez
1121 bgezl -> bltz
1122 bc1fl -> bc1t
1123 bc1tl -> bc1f
1124
1125 bltzall -> bgezal (with jal label instead of j label)
1126 bgezall -> bltzal (ditto)
1127
1128
1129 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1130#define RELAX_BRANCH_ENCODE(at, pic, \
1131 uncond, likely, link, toofar) \
66b3e8da
MR
1132 ((relax_substateT) \
1133 (0xc0000000 \
1134 | ((at) & 0x1f) \
ce8ad872
MR
1135 | ((pic) ? 0x20 : 0) \
1136 | ((toofar) ? 0x40 : 0) \
1137 | ((link) ? 0x80 : 0) \
1138 | ((likely) ? 0x100 : 0) \
1139 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1140#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1141#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1142#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1143#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1144#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1145#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1146#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1147
252b5132
RH
1148/* For mips16 code, we use an entirely different form of relaxation.
1149 mips16 supports two versions of most instructions which take
1150 immediate values: a small one which takes some small value, and a
1151 larger one which takes a 16 bit value. Since branches also follow
1152 this pattern, relaxing these values is required.
1153
1154 We can assemble both mips16 and normal MIPS code in a single
1155 object. Therefore, we need to support this type of relaxation at
1156 the same time that we support the relaxation described above. We
1157 use the high bit of the subtype field to distinguish these cases.
1158
1159 The information we store for this type of relaxation is the
1160 argument code found in the opcode file for this relocation, whether
1161 the user explicitly requested a small or extended form, and whether
1162 the relocation is in a jump or jal delay slot. That tells us the
1163 size of the value, and how it should be stored. We also store
1164 whether the fragment is considered to be extended or not. We also
1165 store whether this is known to be a branch to a different section,
1166 whether we have tried to relax this frag yet, and whether we have
1167 ever extended a PC relative fragment because of a shift count. */
25499ac7 1168#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1169 small, ext, \
1170 dslot, jal_dslot) \
252b5132
RH
1171 (0x80000000 \
1172 | ((type) & 0xff) \
25499ac7
MR
1173 | ((e2) ? 0x100 : 0) \
1174 | ((pic) ? 0x200 : 0) \
1175 | ((sym32) ? 0x400 : 0) \
1176 | ((nomacro) ? 0x800 : 0) \
1177 | ((small) ? 0x1000 : 0) \
1178 | ((ext) ? 0x2000 : 0) \
1179 | ((dslot) ? 0x4000 : 0) \
1180 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1181
4a6a3df4 1182#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1183#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1184#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1185#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1186#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1187#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1188#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1189#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1190#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1191#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1192
1193#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1194#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1195#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1196#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1197#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1198#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1199#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1200#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1201#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1202
df58fc94
RS
1203/* For microMIPS code, we use relaxation similar to one we use for
1204 MIPS16 code. Some instructions that take immediate values support
1205 two encodings: a small one which takes some small value, and a
1206 larger one which takes a 16 bit value. As some branches also follow
1207 this pattern, relaxing these values is required.
1208
1209 We can assemble both microMIPS and normal MIPS code in a single
1210 object. Therefore, we need to support this type of relaxation at
1211 the same time that we support the relaxation described above. We
1212 use one of the high bits of the subtype field to distinguish these
1213 cases.
1214
1215 The information we store for this type of relaxation is the argument
1216 code found in the opcode file for this relocation, the register
8484fb75
MR
1217 selected as the assembler temporary, whether in the 32-bit
1218 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1219 compact, whether there is no delay-slot instruction available to fill
1220 in, whether it stores the link address implicitly in $ra, whether
1221 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1222 instructions is enabled, and whether the displacement of a branch is
1223 too large to fit as an immediate argument of a 16-bit and a 32-bit
1224 branch, respectively. */
ce8ad872 1225#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1226 uncond, compact, link, nods, \
40209cad
MR
1227 relax32, toofar16, toofar32) \
1228 (0x40000000 \
1229 | ((type) & 0xff) \
1230 | (((at) & 0x1f) << 8) \
8484fb75 1231 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1232 | ((pic) ? 0x4000 : 0) \
1233 | ((uncond) ? 0x8000 : 0) \
1234 | ((compact) ? 0x10000 : 0) \
1235 | ((link) ? 0x20000 : 0) \
1236 | ((nods) ? 0x40000 : 0) \
1237 | ((relax32) ? 0x80000 : 0) \
1238 | ((toofar16) ? 0x100000 : 0) \
1239 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1240#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1241#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1242#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1243#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1244#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1245#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1246#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1247#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1248#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1249#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1250
1251#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1252#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1253#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1254#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1255#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1256#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1257
43c0598f
RS
1258/* Sign-extend 16-bit value X. */
1259#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1260
885add95
CD
1261/* Is the given value a sign-extended 32-bit value? */
1262#define IS_SEXT_32BIT_NUM(x) \
1263 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1264 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1265
1266/* Is the given value a sign-extended 16-bit value? */
1267#define IS_SEXT_16BIT_NUM(x) \
1268 (((x) &~ (offsetT) 0x7fff) == 0 \
1269 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1270
df58fc94
RS
1271/* Is the given value a sign-extended 12-bit value? */
1272#define IS_SEXT_12BIT_NUM(x) \
1273 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1274
7f3c4072
CM
1275/* Is the given value a sign-extended 9-bit value? */
1276#define IS_SEXT_9BIT_NUM(x) \
1277 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1278
2051e8c4
MR
1279/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1280#define IS_ZEXT_32BIT_NUM(x) \
1281 (((x) &~ (offsetT) 0xffffffff) == 0 \
1282 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1283
bf12938e
RS
1284/* Extract bits MASK << SHIFT from STRUCT and shift them right
1285 SHIFT places. */
1286#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1287 (((STRUCT) >> (SHIFT)) & (MASK))
1288
bf12938e 1289/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1290#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1291 (!(MICROMIPS) \
1292 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1293 : EXTRACT_BITS ((INSN).insn_opcode, \
1294 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1295#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1296 EXTRACT_BITS ((INSN).insn_opcode, \
1297 MIPS16OP_MASK_##FIELD, \
1298 MIPS16OP_SH_##FIELD)
5c04167a
RS
1299
1300/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1301#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1302\f
df58fc94 1303/* Whether or not we are emitting a branch-likely macro. */
5b7c81bd 1304static bool emit_branch_likely_macro = false;
df58fc94 1305
4d7206a2
RS
1306/* Global variables used when generating relaxable macros. See the
1307 comment above RELAX_ENCODE for more details about how relaxation
1308 is used. */
1309static struct {
1310 /* 0 if we're not emitting a relaxable macro.
1311 1 if we're emitting the first of the two relaxation alternatives.
1312 2 if we're emitting the second alternative. */
1313 int sequence;
1314
1315 /* The first relaxable fixup in the current frag. (In other words,
1316 the first fixup that refers to relaxable code.) */
1317 fixS *first_fixup;
1318
1319 /* sizes[0] says how many bytes of the first alternative are stored in
1320 the current frag. Likewise sizes[1] for the second alternative. */
1321 unsigned int sizes[2];
1322
1323 /* The symbol on which the choice of sequence depends. */
1324 symbolS *symbol;
1325} mips_relax;
252b5132 1326\f
584892a6
RS
1327/* Global variables used to decide whether a macro needs a warning. */
1328static struct {
1329 /* True if the macro is in a branch delay slot. */
5b7c81bd 1330 bool delay_slot_p;
584892a6 1331
df58fc94
RS
1332 /* Set to the length in bytes required if the macro is in a delay slot
1333 that requires a specific length of instruction, otherwise zero. */
1334 unsigned int delay_slot_length;
1335
584892a6
RS
1336 /* For relaxable macros, sizes[0] is the length of the first alternative
1337 in bytes and sizes[1] is the length of the second alternative.
1338 For non-relaxable macros, both elements give the length of the
1339 macro in bytes. */
1340 unsigned int sizes[2];
1341
df58fc94
RS
1342 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1343 instruction of the first alternative in bytes and first_insn_sizes[1]
1344 is the length of the first instruction of the second alternative.
1345 For non-relaxable macros, both elements give the length of the first
1346 instruction in bytes.
1347
1348 Set to zero if we haven't yet seen the first instruction. */
1349 unsigned int first_insn_sizes[2];
1350
1351 /* For relaxable macros, insns[0] is the number of instructions for the
1352 first alternative and insns[1] is the number of instructions for the
1353 second alternative.
1354
1355 For non-relaxable macros, both elements give the number of
1356 instructions for the macro. */
1357 unsigned int insns[2];
1358
584892a6
RS
1359 /* The first variant frag for this macro. */
1360 fragS *first_frag;
1361} mips_macro_warning;
1362\f
252b5132
RH
1363/* Prototypes for static functions. */
1364
252b5132
RH
1365enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1366
b34976b6 1367static void append_insn
df58fc94 1368 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
5b7c81bd 1369 bool expansionp);
7d10b47d 1370static void mips_no_prev_insn (void);
c67a084a 1371static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1372static void mips16_macro_build
03ea81db 1373 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1374static void load_register (int, expressionS *, int);
584892a6
RS
1375static void macro_start (void);
1376static void macro_end (void);
833794fc 1377static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1378static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1379static void mips_ip (char *str, struct mips_cl_insn * ip);
1380static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1381static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1382static void mips16_immed
3b4dbbbf 1383 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1384 unsigned int, unsigned long *);
5e0116d5 1385static size_t my_getSmallExpression
17a2f251
TS
1386 (expressionS *, bfd_reloc_code_real_type *, char *);
1387static void my_getExpression (expressionS *, char *);
1388static void s_align (int);
1389static void s_change_sec (int);
1390static void s_change_section (int);
1391static void s_cons (int);
1392static void s_float_cons (int);
1393static void s_mips_globl (int);
1394static void s_option (int);
1395static void s_mipsset (int);
1396static void s_abicalls (int);
1397static void s_cpload (int);
1398static void s_cpsetup (int);
1399static void s_cplocal (int);
1400static void s_cprestore (int);
1401static void s_cpreturn (int);
741d6ea8
JM
1402static void s_dtprelword (int);
1403static void s_dtpreldword (int);
d0f13682
CLT
1404static void s_tprelword (int);
1405static void s_tpreldword (int);
17a2f251
TS
1406static void s_gpvalue (int);
1407static void s_gpword (int);
1408static void s_gpdword (int);
a3f278e2 1409static void s_ehword (int);
17a2f251
TS
1410static void s_cpadd (int);
1411static void s_insn (int);
ba92f887 1412static void s_nan (int);
919731af 1413static void s_module (int);
17a2f251
TS
1414static void s_mips_ent (int);
1415static void s_mips_end (int);
1416static void s_mips_frame (int);
1417static void s_mips_mask (int reg_type);
1418static void s_mips_stab (int);
1419static void s_mips_weakext (int);
1420static void s_mips_file (int);
1421static void s_mips_loc (int);
5b7c81bd 1422static bool pic_need_relax (symbolS *);
4a6a3df4 1423static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1424static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1425static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1426static void file_mips_check_options (void);
e7af610e
NC
1427
1428/* Table and functions used to map between CPU/ISA names, and
1429 ISA levels, and CPU numbers. */
1430
e972090a
NC
1431struct mips_cpu_info
1432{
e7af610e 1433 const char *name; /* CPU or ISA name. */
d16afab6
RS
1434 int flags; /* MIPS_CPU_* flags. */
1435 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1436 int isa; /* ISA level. */
1437 int cpu; /* CPU number (default CPU if ISA). */
1438};
1439
ad3fea08 1440#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1441
17a2f251
TS
1442static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1443static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1444static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1445\f
c31f3936
RS
1446/* Command-line options. */
1447const char *md_shortopts = "O::g::G:";
1448
1449enum options
1450 {
1451 OPTION_MARCH = OPTION_MD_BASE,
1452 OPTION_MTUNE,
1453 OPTION_MIPS1,
1454 OPTION_MIPS2,
1455 OPTION_MIPS3,
1456 OPTION_MIPS4,
1457 OPTION_MIPS5,
1458 OPTION_MIPS32,
1459 OPTION_MIPS64,
1460 OPTION_MIPS32R2,
ae52f483
AB
1461 OPTION_MIPS32R3,
1462 OPTION_MIPS32R5,
7361da2c 1463 OPTION_MIPS32R6,
c31f3936 1464 OPTION_MIPS64R2,
ae52f483
AB
1465 OPTION_MIPS64R3,
1466 OPTION_MIPS64R5,
7361da2c 1467 OPTION_MIPS64R6,
c31f3936
RS
1468 OPTION_MIPS16,
1469 OPTION_NO_MIPS16,
1470 OPTION_MIPS3D,
1471 OPTION_NO_MIPS3D,
1472 OPTION_MDMX,
1473 OPTION_NO_MDMX,
1474 OPTION_DSP,
1475 OPTION_NO_DSP,
1476 OPTION_MT,
1477 OPTION_NO_MT,
1478 OPTION_VIRT,
1479 OPTION_NO_VIRT,
56d438b1
CF
1480 OPTION_MSA,
1481 OPTION_NO_MSA,
c31f3936
RS
1482 OPTION_SMARTMIPS,
1483 OPTION_NO_SMARTMIPS,
1484 OPTION_DSPR2,
1485 OPTION_NO_DSPR2,
8f4f9071
MF
1486 OPTION_DSPR3,
1487 OPTION_NO_DSPR3,
c31f3936
RS
1488 OPTION_EVA,
1489 OPTION_NO_EVA,
7d64c587
AB
1490 OPTION_XPA,
1491 OPTION_NO_XPA,
c31f3936
RS
1492 OPTION_MICROMIPS,
1493 OPTION_NO_MICROMIPS,
1494 OPTION_MCU,
1495 OPTION_NO_MCU,
25499ac7
MR
1496 OPTION_MIPS16E2,
1497 OPTION_NO_MIPS16E2,
730c3174
SE
1498 OPTION_CRC,
1499 OPTION_NO_CRC,
c31f3936
RS
1500 OPTION_M4650,
1501 OPTION_NO_M4650,
1502 OPTION_M4010,
1503 OPTION_NO_M4010,
1504 OPTION_M4100,
1505 OPTION_NO_M4100,
1506 OPTION_M3900,
1507 OPTION_NO_M3900,
1508 OPTION_M7000_HILO_FIX,
1509 OPTION_MNO_7000_HILO_FIX,
1510 OPTION_FIX_24K,
1511 OPTION_NO_FIX_24K,
a8d14a88
CM
1512 OPTION_FIX_RM7000,
1513 OPTION_NO_FIX_RM7000,
6f2117ba
PH
1514 OPTION_FIX_LOONGSON3_LLSC,
1515 OPTION_NO_FIX_LOONGSON3_LLSC,
c31f3936
RS
1516 OPTION_FIX_LOONGSON2F_JUMP,
1517 OPTION_NO_FIX_LOONGSON2F_JUMP,
1518 OPTION_FIX_LOONGSON2F_NOP,
1519 OPTION_NO_FIX_LOONGSON2F_NOP,
1520 OPTION_FIX_VR4120,
1521 OPTION_NO_FIX_VR4120,
1522 OPTION_FIX_VR4130,
1523 OPTION_NO_FIX_VR4130,
1524 OPTION_FIX_CN63XXP1,
1525 OPTION_NO_FIX_CN63XXP1,
27c634e0
FN
1526 OPTION_FIX_R5900,
1527 OPTION_NO_FIX_R5900,
c31f3936
RS
1528 OPTION_TRAP,
1529 OPTION_BREAK,
1530 OPTION_EB,
1531 OPTION_EL,
1532 OPTION_FP32,
1533 OPTION_GP32,
1534 OPTION_CONSTRUCT_FLOATS,
1535 OPTION_NO_CONSTRUCT_FLOATS,
1536 OPTION_FP64,
351cdf24 1537 OPTION_FPXX,
c31f3936
RS
1538 OPTION_GP64,
1539 OPTION_RELAX_BRANCH,
1540 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1541 OPTION_IGNORE_BRANCH_ISA,
1542 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1543 OPTION_INSN32,
1544 OPTION_NO_INSN32,
c31f3936
RS
1545 OPTION_MSHARED,
1546 OPTION_MNO_SHARED,
1547 OPTION_MSYM32,
1548 OPTION_MNO_SYM32,
1549 OPTION_SOFT_FLOAT,
1550 OPTION_HARD_FLOAT,
1551 OPTION_SINGLE_FLOAT,
1552 OPTION_DOUBLE_FLOAT,
1553 OPTION_32,
c31f3936
RS
1554 OPTION_CALL_SHARED,
1555 OPTION_CALL_NONPIC,
1556 OPTION_NON_SHARED,
1557 OPTION_XGOT,
1558 OPTION_MABI,
1559 OPTION_N32,
1560 OPTION_64,
1561 OPTION_MDEBUG,
1562 OPTION_NO_MDEBUG,
1563 OPTION_PDR,
1564 OPTION_NO_PDR,
1565 OPTION_MVXWORKS_PIC,
ba92f887 1566 OPTION_NAN,
351cdf24
MF
1567 OPTION_ODD_SPREG,
1568 OPTION_NO_ODD_SPREG,
6f20c942
FS
1569 OPTION_GINV,
1570 OPTION_NO_GINV,
8095d2f7
CX
1571 OPTION_LOONGSON_MMI,
1572 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1573 OPTION_LOONGSON_CAM,
1574 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1575 OPTION_LOONGSON_EXT,
1576 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1577 OPTION_LOONGSON_EXT2,
1578 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1579 OPTION_END_OF_ENUM
1580 };
1581
1582struct option md_longopts[] =
1583{
1584 /* Options which specify architecture. */
1585 {"march", required_argument, NULL, OPTION_MARCH},
1586 {"mtune", required_argument, NULL, OPTION_MTUNE},
1587 {"mips0", no_argument, NULL, OPTION_MIPS1},
1588 {"mips1", no_argument, NULL, OPTION_MIPS1},
1589 {"mips2", no_argument, NULL, OPTION_MIPS2},
1590 {"mips3", no_argument, NULL, OPTION_MIPS3},
1591 {"mips4", no_argument, NULL, OPTION_MIPS4},
1592 {"mips5", no_argument, NULL, OPTION_MIPS5},
1593 {"mips32", no_argument, NULL, OPTION_MIPS32},
1594 {"mips64", no_argument, NULL, OPTION_MIPS64},
1595 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1596 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1597 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1598 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1599 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1600 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1601 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1602 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1603
1604 /* Options which specify Application Specific Extensions (ASEs). */
1605 {"mips16", no_argument, NULL, OPTION_MIPS16},
1606 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1607 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1608 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1609 {"mdmx", no_argument, NULL, OPTION_MDMX},
1610 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1611 {"mdsp", no_argument, NULL, OPTION_DSP},
1612 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1613 {"mmt", no_argument, NULL, OPTION_MT},
1614 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1615 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1616 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1617 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1618 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1619 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1620 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1621 {"meva", no_argument, NULL, OPTION_EVA},
1622 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1623 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1624 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1625 {"mmcu", no_argument, NULL, OPTION_MCU},
1626 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1627 {"mvirt", no_argument, NULL, OPTION_VIRT},
1628 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1629 {"mmsa", no_argument, NULL, OPTION_MSA},
1630 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1631 {"mxpa", no_argument, NULL, OPTION_XPA},
1632 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1633 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1634 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1635 {"mcrc", no_argument, NULL, OPTION_CRC},
1636 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1637 {"mginv", no_argument, NULL, OPTION_GINV},
1638 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1639 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1640 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1641 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1642 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1643 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1644 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1645 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1646 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1647
1648 /* Old-style architecture options. Don't add more of these. */
1649 {"m4650", no_argument, NULL, OPTION_M4650},
1650 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1651 {"m4010", no_argument, NULL, OPTION_M4010},
1652 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1653 {"m4100", no_argument, NULL, OPTION_M4100},
1654 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1655 {"m3900", no_argument, NULL, OPTION_M3900},
1656 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1657
1658 /* Options which enable bug fixes. */
1659 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1660 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1661 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
6f2117ba
PH
1662 {"mfix-loongson3-llsc", no_argument, NULL, OPTION_FIX_LOONGSON3_LLSC},
1663 {"mno-fix-loongson3-llsc", no_argument, NULL, OPTION_NO_FIX_LOONGSON3_LLSC},
c31f3936
RS
1664 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1665 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1666 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1667 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1668 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1669 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1670 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1671 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1672 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1673 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1674 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1675 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1676 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1677 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
27c634e0
FN
1678 {"mfix-r5900", no_argument, NULL, OPTION_FIX_R5900},
1679 {"mno-fix-r5900", no_argument, NULL, OPTION_NO_FIX_R5900},
c31f3936
RS
1680
1681 /* Miscellaneous options. */
1682 {"trap", no_argument, NULL, OPTION_TRAP},
1683 {"no-break", no_argument, NULL, OPTION_TRAP},
1684 {"break", no_argument, NULL, OPTION_BREAK},
1685 {"no-trap", no_argument, NULL, OPTION_BREAK},
1686 {"EB", no_argument, NULL, OPTION_EB},
1687 {"EL", no_argument, NULL, OPTION_EL},
1688 {"mfp32", no_argument, NULL, OPTION_FP32},
1689 {"mgp32", no_argument, NULL, OPTION_GP32},
1690 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1691 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1692 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1693 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1694 {"mgp64", no_argument, NULL, OPTION_GP64},
1695 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1696 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1697 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1698 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1699 {"minsn32", no_argument, NULL, OPTION_INSN32},
1700 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1701 {"mshared", no_argument, NULL, OPTION_MSHARED},
1702 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1703 {"msym32", no_argument, NULL, OPTION_MSYM32},
1704 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1705 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1706 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1707 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1708 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1709 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1710 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1711
1712 /* Strictly speaking this next option is ELF specific,
1713 but we allow it for other ports as well in order to
1714 make testing easier. */
1715 {"32", no_argument, NULL, OPTION_32},
1716
1717 /* ELF-specific options. */
c31f3936
RS
1718 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1719 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1720 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1721 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1722 {"xgot", no_argument, NULL, OPTION_XGOT},
1723 {"mabi", required_argument, NULL, OPTION_MABI},
1724 {"n32", no_argument, NULL, OPTION_N32},
1725 {"64", no_argument, NULL, OPTION_64},
1726 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1727 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1728 {"mpdr", no_argument, NULL, OPTION_PDR},
1729 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1730 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1731 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1732
1733 {NULL, no_argument, NULL, 0}
1734};
1735size_t md_longopts_size = sizeof (md_longopts);
1736\f
c6278170
RS
1737/* Information about either an Application Specific Extension or an
1738 optional architecture feature that, for simplicity, we treat in the
1739 same way as an ASE. */
1740struct mips_ase
1741{
1742 /* The name of the ASE, used in both the command-line and .set options. */
1743 const char *name;
1744
1745 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1746 and 64-bit architectures, the flags here refer to the subset that
1747 is available on both. */
1748 unsigned int flags;
1749
1750 /* The ASE_* flag used for instructions that are available on 64-bit
1751 architectures but that are not included in FLAGS. */
1752 unsigned int flags64;
1753
1754 /* The command-line options that turn the ASE on and off. */
1755 int option_on;
1756 int option_off;
1757
1758 /* The minimum required architecture revisions for MIPS32, MIPS64,
1759 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1760 int mips32_rev;
1761 int mips64_rev;
1762 int micromips32_rev;
1763 int micromips64_rev;
7361da2c
AB
1764
1765 /* The architecture where the ASE was removed or -1 if the extension has not
1766 been removed. */
1767 int rem_rev;
c6278170
RS
1768};
1769
1770/* A table of all supported ASEs. */
1771static const struct mips_ase mips_ases[] = {
1772 { "dsp", ASE_DSP, ASE_DSP64,
1773 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1774 2, 2, 2, 2,
1775 -1 },
c6278170
RS
1776
1777 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1778 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1779 2, 2, 2, 2,
1780 -1 },
c6278170 1781
8f4f9071
MF
1782 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1783 OPTION_DSPR3, OPTION_NO_DSPR3,
1784 6, 6, -1, -1,
1785 -1 },
1786
c6278170
RS
1787 { "eva", ASE_EVA, 0,
1788 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1789 2, 2, 2, 2,
1790 -1 },
c6278170
RS
1791
1792 { "mcu", ASE_MCU, 0,
1793 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1794 2, 2, 2, 2,
1795 -1 },
c6278170
RS
1796
1797 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1798 { "mdmx", ASE_MDMX, 0,
1799 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1800 -1, 1, -1, -1,
1801 6 },
c6278170
RS
1802
1803 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1804 { "mips3d", ASE_MIPS3D, 0,
1805 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1806 2, 1, -1, -1,
1807 6 },
c6278170
RS
1808
1809 { "mt", ASE_MT, 0,
1810 OPTION_MT, OPTION_NO_MT,
0c5c669c 1811 2, 2, -1, -1,
7361da2c 1812 -1 },
c6278170
RS
1813
1814 { "smartmips", ASE_SMARTMIPS, 0,
1815 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1816 1, -1, -1, -1,
1817 6 },
c6278170
RS
1818
1819 { "virt", ASE_VIRT, ASE_VIRT64,
1820 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1821 2, 2, 2, 2,
1822 -1 },
56d438b1
CF
1823
1824 { "msa", ASE_MSA, ASE_MSA64,
1825 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1826 2, 2, 2, 2,
1827 -1 },
7d64c587
AB
1828
1829 { "xpa", ASE_XPA, 0,
1830 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1831 2, 2, 2, 2,
7361da2c 1832 -1 },
25499ac7
MR
1833
1834 { "mips16e2", ASE_MIPS16E2, 0,
1835 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1836 2, 2, -1, -1,
1837 6 },
730c3174
SE
1838
1839 { "crc", ASE_CRC, ASE_CRC64,
1840 OPTION_CRC, OPTION_NO_CRC,
1841 6, 6, -1, -1,
1842 -1 },
6f20c942
FS
1843
1844 { "ginv", ASE_GINV, 0,
1845 OPTION_GINV, OPTION_NO_GINV,
1846 6, 6, 6, 6,
1847 -1 },
8095d2f7
CX
1848
1849 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1850 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1851 0, 0, -1, -1,
1852 -1 },
716c08de
CX
1853
1854 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1855 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1856 0, 0, -1, -1,
1857 -1 },
bdc6c06e
CX
1858
1859 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1860 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1861 0, 0, -1, -1,
1862 -1 },
a693765e
CX
1863
1864 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1865 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1866 0, 0, -1, -1,
1867 -1 },
c6278170
RS
1868};
1869
1870/* The set of ASEs that require -mfp64. */
82bda27b 1871#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1872
1873/* Groups of ASE_* flags that represent different revisions of an ASE. */
1874static const unsigned int mips_ase_groups[] = {
a693765e
CX
1875 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1876 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1877};
1878\f
252b5132
RH
1879/* Pseudo-op table.
1880
1881 The following pseudo-ops from the Kane and Heinrich MIPS book
1882 should be defined here, but are currently unsupported: .alias,
1883 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1884
1885 The following pseudo-ops from the Kane and Heinrich MIPS book are
1886 specific to the type of debugging information being generated, and
1887 should be defined by the object format: .aent, .begin, .bend,
1888 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1889 .vreg.
1890
1891 The following pseudo-ops from the Kane and Heinrich MIPS book are
1892 not MIPS CPU specific, but are also not specific to the object file
1893 format. This file is probably the best place to define them, but
d84bcf09 1894 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1895
e972090a
NC
1896static const pseudo_typeS mips_pseudo_table[] =
1897{
beae10d5 1898 /* MIPS specific pseudo-ops. */
252b5132
RH
1899 {"option", s_option, 0},
1900 {"set", s_mipsset, 0},
1901 {"rdata", s_change_sec, 'r'},
1902 {"sdata", s_change_sec, 's'},
1903 {"livereg", s_ignore, 0},
1904 {"abicalls", s_abicalls, 0},
1905 {"cpload", s_cpload, 0},
6478892d
TS
1906 {"cpsetup", s_cpsetup, 0},
1907 {"cplocal", s_cplocal, 0},
252b5132 1908 {"cprestore", s_cprestore, 0},
6478892d 1909 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1910 {"dtprelword", s_dtprelword, 0},
1911 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1912 {"tprelword", s_tprelword, 0},
1913 {"tpreldword", s_tpreldword, 0},
6478892d 1914 {"gpvalue", s_gpvalue, 0},
252b5132 1915 {"gpword", s_gpword, 0},
10181a0d 1916 {"gpdword", s_gpdword, 0},
a3f278e2 1917 {"ehword", s_ehword, 0},
252b5132
RH
1918 {"cpadd", s_cpadd, 0},
1919 {"insn", s_insn, 0},
ba92f887 1920 {"nan", s_nan, 0},
919731af 1921 {"module", s_module, 0},
252b5132 1922
beae10d5 1923 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1924 chips. */
38a57ae7 1925 {"asciiz", stringer, 8 + 1},
252b5132
RH
1926 {"bss", s_change_sec, 'b'},
1927 {"err", s_err, 0},
1928 {"half", s_cons, 1},
1929 {"dword", s_cons, 3},
1930 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1931 {"origin", s_org, 0},
1932 {"repeat", s_rept, 0},
252b5132 1933
998b3c36
MR
1934 /* For MIPS this is non-standard, but we define it for consistency. */
1935 {"sbss", s_change_sec, 'B'},
1936
beae10d5 1937 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1938 here for one reason or another. */
1939 {"align", s_align, 0},
1940 {"byte", s_cons, 0},
1941 {"data", s_change_sec, 'd'},
1942 {"double", s_float_cons, 'd'},
1943 {"float", s_float_cons, 'f'},
1944 {"globl", s_mips_globl, 0},
1945 {"global", s_mips_globl, 0},
1946 {"hword", s_cons, 1},
1947 {"int", s_cons, 2},
1948 {"long", s_cons, 2},
1949 {"octa", s_cons, 4},
1950 {"quad", s_cons, 3},
cca86cc8 1951 {"section", s_change_section, 0},
252b5132
RH
1952 {"short", s_cons, 1},
1953 {"single", s_float_cons, 'f'},
754e2bb9 1954 {"stabd", s_mips_stab, 'd'},
252b5132 1955 {"stabn", s_mips_stab, 'n'},
754e2bb9 1956 {"stabs", s_mips_stab, 's'},
252b5132
RH
1957 {"text", s_change_sec, 't'},
1958 {"word", s_cons, 2},
add56521 1959
add56521 1960 { "extern", ecoff_directive_extern, 0},
add56521 1961
43841e91 1962 { NULL, NULL, 0 },
252b5132
RH
1963};
1964
e972090a
NC
1965static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1966{
beae10d5
KH
1967 /* These pseudo-ops should be defined by the object file format.
1968 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1969 {"aent", s_mips_ent, 1},
1970 {"bgnb", s_ignore, 0},
1971 {"end", s_mips_end, 0},
1972 {"endb", s_ignore, 0},
1973 {"ent", s_mips_ent, 0},
c5dd6aab 1974 {"file", s_mips_file, 0},
252b5132
RH
1975 {"fmask", s_mips_mask, 'F'},
1976 {"frame", s_mips_frame, 0},
c5dd6aab 1977 {"loc", s_mips_loc, 0},
252b5132
RH
1978 {"mask", s_mips_mask, 'R'},
1979 {"verstamp", s_ignore, 0},
43841e91 1980 { NULL, NULL, 0 },
252b5132
RH
1981};
1982
3ae8dd8d
MR
1983/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1984 purpose of the `.dc.a' internal pseudo-op. */
1985
1986int
1987mips_address_bytes (void)
1988{
919731af 1989 file_mips_check_options ();
3ae8dd8d
MR
1990 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1991}
1992
17a2f251 1993extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1994
1995void
17a2f251 1996mips_pop_insert (void)
252b5132
RH
1997{
1998 pop_insert (mips_pseudo_table);
1999 if (! ECOFF_DEBUGGING)
2000 pop_insert (mips_nonecoff_pseudo_table);
2001}
2002\f
2003/* Symbols labelling the current insn. */
2004
e972090a
NC
2005struct insn_label_list
2006{
252b5132
RH
2007 struct insn_label_list *next;
2008 symbolS *label;
2009};
2010
252b5132 2011static struct insn_label_list *free_insn_labels;
742a56fe 2012#define label_list tc_segment_info_data.labels
252b5132 2013
17a2f251 2014static void mips_clear_insn_labels (void);
df58fc94
RS
2015static void mips_mark_labels (void);
2016static void mips_compressed_mark_labels (void);
252b5132
RH
2017
2018static inline void
17a2f251 2019mips_clear_insn_labels (void)
252b5132 2020{
ed9e98c2 2021 struct insn_label_list **pl;
a8dbcb85 2022 segment_info_type *si;
252b5132 2023
a8dbcb85
TS
2024 if (now_seg)
2025 {
2026 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
2027 ;
3739860c 2028
a8dbcb85
TS
2029 si = seg_info (now_seg);
2030 *pl = si->label_list;
2031 si->label_list = NULL;
2032 }
252b5132 2033}
a8dbcb85 2034
df58fc94
RS
2035/* Mark instruction labels in MIPS16/microMIPS mode. */
2036
2037static inline void
2038mips_mark_labels (void)
2039{
2040 if (HAVE_CODE_COMPRESSION)
2041 mips_compressed_mark_labels ();
2042}
252b5132 2043\f
6eb099ae 2044static char *expr_parse_end;
252b5132 2045
e423441d 2046/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2047 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2048
2049static expressionS imm_expr;
252b5132 2050
77bd4346
RS
2051/* The relocatable field in an instruction and the relocs associated
2052 with it. These variables are used for instructions like LUI and
2053 JAL as well as true offsets. They are also used for address
2054 operands in macros. */
252b5132 2055
77bd4346 2056static expressionS offset_expr;
f6688943
TS
2057static bfd_reloc_code_real_type offset_reloc[3]
2058 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2059
df58fc94
RS
2060/* This is set to the resulting size of the instruction to be produced
2061 by mips16_ip if an explicit extension is used or by mips_ip if an
2062 explicit size is supplied. */
252b5132 2063
df58fc94 2064static unsigned int forced_insn_length;
252b5132 2065
e1b47bd5
RS
2066/* True if we are assembling an instruction. All dot symbols defined during
2067 this time should be treated as code labels. */
2068
5b7c81bd 2069static bool mips_assembling_insn;
e1b47bd5 2070
ecb4347a
DJ
2071/* The pdr segment for per procedure frame/regmask info. Not used for
2072 ECOFF debugging. */
252b5132
RH
2073
2074static segT pdr_seg;
252b5132 2075
e013f690
TS
2076/* The default target format to use. */
2077
aeffff67
RS
2078#if defined (TE_FreeBSD)
2079#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2080#elif defined (TE_TMIPS)
2081#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2082#else
2083#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2084#endif
2085
e013f690 2086const char *
17a2f251 2087mips_target_format (void)
e013f690
TS
2088{
2089 switch (OUTPUT_FLAVOR)
2090 {
e013f690 2091 case bfd_target_elf_flavour:
0a44bf69
RS
2092#ifdef TE_VXWORKS
2093 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2094 return (target_big_endian
2095 ? "elf32-bigmips-vxworks"
2096 : "elf32-littlemips-vxworks");
2097#endif
e013f690 2098 return (target_big_endian
cfe86eaa 2099 ? (HAVE_64BIT_OBJECTS
aeffff67 2100 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2101 : (HAVE_NEWABI
aeffff67
RS
2102 ? ELF_TARGET ("elf32-n", "big")
2103 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2104 : (HAVE_64BIT_OBJECTS
aeffff67 2105 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2106 : (HAVE_NEWABI
aeffff67
RS
2107 ? ELF_TARGET ("elf32-n", "little")
2108 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2109 default:
2110 abort ();
2111 return NULL;
2112 }
2113}
2114
c6278170
RS
2115/* Return the ISA revision that is currently in use, or 0 if we are
2116 generating code for MIPS V or below. */
2117
2118static int
2119mips_isa_rev (void)
2120{
2121 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2122 return 2;
2123
ae52f483
AB
2124 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2125 return 3;
2126
2127 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2128 return 5;
2129
7361da2c
AB
2130 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2131 return 6;
2132
c6278170
RS
2133 /* microMIPS implies revision 2 or above. */
2134 if (mips_opts.micromips)
2135 return 2;
2136
2137 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2138 return 1;
2139
2140 return 0;
2141}
2142
2143/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2144
2145static unsigned int
2146mips_ase_mask (unsigned int flags)
2147{
2148 unsigned int i;
2149
2150 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2151 if (flags & mips_ase_groups[i])
2152 flags |= mips_ase_groups[i];
2153 return flags;
2154}
2155
2156/* Check whether the current ISA supports ASE. Issue a warning if
2157 appropriate. */
2158
2159static void
2160mips_check_isa_supports_ase (const struct mips_ase *ase)
2161{
2162 const char *base;
2163 int min_rev, size;
2164 static unsigned int warned_isa;
2165 static unsigned int warned_fp32;
2166
2167 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2168 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2169 else
2170 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2171 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2172 && (warned_isa & ase->flags) != ase->flags)
2173 {
2174 warned_isa |= ase->flags;
2175 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2176 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2177 if (min_rev < 0)
1661c76c 2178 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2179 " `%s' extension"), size, base, ase->name);
2180 else
1661c76c 2181 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2182 ase->name, base, size, min_rev);
2183 }
7361da2c
AB
2184 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2185 && (warned_isa & ase->flags) != ase->flags)
2186 {
2187 warned_isa |= ase->flags;
2188 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2189 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2190 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2191 ase->name, base, size, ase->rem_rev);
2192 }
2193
c6278170 2194 if ((ase->flags & FP64_ASES)
0b35dfee 2195 && mips_opts.fp != 64
c6278170
RS
2196 && (warned_fp32 & ase->flags) != ase->flags)
2197 {
2198 warned_fp32 |= ase->flags;
1661c76c 2199 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2200 }
2201}
2202
2203/* Check all enabled ASEs to see whether they are supported by the
2204 chosen architecture. */
2205
2206static void
2207mips_check_isa_supports_ases (void)
2208{
2209 unsigned int i, mask;
2210
2211 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2212 {
2213 mask = mips_ase_mask (mips_ases[i].flags);
2214 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2215 mips_check_isa_supports_ase (&mips_ases[i]);
2216 }
2217}
2218
2219/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2220 that were affected. */
2221
2222static unsigned int
919731af 2223mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
5b7c81bd 2224 bool enabled_p)
c6278170
RS
2225{
2226 unsigned int mask;
2227
2228 mask = mips_ase_mask (ase->flags);
919731af 2229 opts->ase &= ~mask;
92cebb3d
MR
2230
2231 /* Clear combination ASE flags, which need to be recalculated based on
2232 updated regular ASE settings. */
41cee089 2233 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT | ASE_EVA_R6);
92cebb3d 2234
c6278170 2235 if (enabled_p)
919731af 2236 opts->ase |= ase->flags;
25499ac7 2237
9785fc2a
MR
2238 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2239 instructions which are only valid when both ASEs are enabled.
2240 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2241 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2242 {
2243 opts->ase |= ASE_XPA_VIRT;
2244 mask |= ASE_XPA_VIRT;
2245 }
25499ac7
MR
2246 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2247 {
2248 opts->ase |= ASE_MIPS16E2_MT;
2249 mask |= ASE_MIPS16E2_MT;
2250 }
2251
41cee089
FS
2252 /* The EVA Extension has instructions which are only valid when the R6 ISA
2253 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
2254 present. */
2255 if (((opts->ase & ASE_EVA) != 0) && ISA_IS_R6 (opts->isa))
2256 {
2257 opts->ase |= ASE_EVA_R6;
2258 mask |= ASE_EVA_R6;
2259 }
2260
c6278170
RS
2261 return mask;
2262}
2263
2264/* Return the ASE called NAME, or null if none. */
2265
2266static const struct mips_ase *
2267mips_lookup_ase (const char *name)
2268{
2269 unsigned int i;
2270
2271 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2272 if (strcmp (name, mips_ases[i].name) == 0)
2273 return &mips_ases[i];
2274 return NULL;
2275}
2276
df58fc94 2277/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2278 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2279 otherwise it is a 32-bit instruction. */
df58fc94
RS
2280
2281static inline unsigned int
2282micromips_insn_length (const struct mips_opcode *mo)
2283{
7fd53920 2284 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2285}
2286
5c04167a
RS
2287/* Return the length of MIPS16 instruction OPCODE. */
2288
2289static inline unsigned int
2290mips16_opcode_length (unsigned long opcode)
2291{
2292 return (opcode >> 16) == 0 ? 2 : 4;
2293}
2294
1e915849
RS
2295/* Return the length of instruction INSN. */
2296
2297static inline unsigned int
2298insn_length (const struct mips_cl_insn *insn)
2299{
df58fc94
RS
2300 if (mips_opts.micromips)
2301 return micromips_insn_length (insn->insn_mo);
2302 else if (mips_opts.mips16)
5c04167a 2303 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2304 else
1e915849 2305 return 4;
1e915849
RS
2306}
2307
2308/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2309
2310static void
2311create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2312{
2313 size_t i;
2314
2315 insn->insn_mo = mo;
1e915849
RS
2316 insn->insn_opcode = mo->match;
2317 insn->frag = NULL;
2318 insn->where = 0;
2319 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2320 insn->fixp[i] = NULL;
2321 insn->fixed_p = (mips_opts.noreorder > 0);
2322 insn->noreorder_p = (mips_opts.noreorder > 0);
2323 insn->mips16_absolute_jump_p = 0;
15be625d 2324 insn->complete_p = 0;
e407c74b 2325 insn->cleared_p = 0;
1e915849
RS
2326}
2327
fc76e730
RS
2328/* Get a list of all the operands in INSN. */
2329
2330static const struct mips_operand_array *
2331insn_operands (const struct mips_cl_insn *insn)
2332{
2333 if (insn->insn_mo >= &mips_opcodes[0]
2334 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2335 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2336
2337 if (insn->insn_mo >= &mips16_opcodes[0]
2338 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2339 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2340
2341 if (insn->insn_mo >= &micromips_opcodes[0]
2342 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2343 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2344
2345 abort ();
2346}
2347
2348/* Get a description of operand OPNO of INSN. */
2349
2350static const struct mips_operand *
2351insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2352{
2353 const struct mips_operand_array *operands;
2354
2355 operands = insn_operands (insn);
2356 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2357 abort ();
2358 return operands->operand[opno];
2359}
2360
e077a1c8
RS
2361/* Install UVAL as the value of OPERAND in INSN. */
2362
2363static inline void
2364insn_insert_operand (struct mips_cl_insn *insn,
2365 const struct mips_operand *operand, unsigned int uval)
2366{
25499ac7
MR
2367 if (mips_opts.mips16
2368 && operand->type == OP_INT && operand->lsb == 0
2369 && mips_opcode_32bit_p (insn->insn_mo))
2370 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2371 else
2372 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2373}
2374
fc76e730
RS
2375/* Extract the value of OPERAND from INSN. */
2376
2377static inline unsigned
2378insn_extract_operand (const struct mips_cl_insn *insn,
2379 const struct mips_operand *operand)
2380{
2381 return mips_extract_operand (operand, insn->insn_opcode);
2382}
2383
df58fc94 2384/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2385
2386static void
df58fc94 2387mips_record_compressed_mode (void)
742a56fe
RS
2388{
2389 segment_info_type *si;
2390
2391 si = seg_info (now_seg);
2392 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2393 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2394 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2395 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2396}
2397
4d68580a
RS
2398/* Read a standard MIPS instruction from BUF. */
2399
2400static unsigned long
2401read_insn (char *buf)
2402{
2403 if (target_big_endian)
2404 return bfd_getb32 ((bfd_byte *) buf);
2405 else
2406 return bfd_getl32 ((bfd_byte *) buf);
2407}
2408
2409/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2410 the next byte. */
2411
2412static char *
2413write_insn (char *buf, unsigned int insn)
2414{
2415 md_number_to_chars (buf, insn, 4);
2416 return buf + 4;
2417}
2418
2419/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2420 has length LENGTH. */
2421
2422static unsigned long
2423read_compressed_insn (char *buf, unsigned int length)
2424{
2425 unsigned long insn;
2426 unsigned int i;
2427
2428 insn = 0;
2429 for (i = 0; i < length; i += 2)
2430 {
2431 insn <<= 16;
2432 if (target_big_endian)
2433 insn |= bfd_getb16 ((char *) buf);
2434 else
2435 insn |= bfd_getl16 ((char *) buf);
2436 buf += 2;
2437 }
2438 return insn;
2439}
2440
5c04167a
RS
2441/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2442 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2443
2444static char *
2445write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2446{
2447 unsigned int i;
2448
2449 for (i = 0; i < length; i += 2)
2450 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2451 return buf + length;
2452}
2453
1e915849
RS
2454/* Install INSN at the location specified by its "frag" and "where" fields. */
2455
2456static void
2457install_insn (const struct mips_cl_insn *insn)
2458{
2459 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2460 if (HAVE_CODE_COMPRESSION)
2461 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2462 else
4d68580a 2463 write_insn (f, insn->insn_opcode);
df58fc94 2464 mips_record_compressed_mode ();
1e915849
RS
2465}
2466
2467/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2468 and install the opcode in the new location. */
2469
2470static void
2471move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2472{
2473 size_t i;
2474
2475 insn->frag = frag;
2476 insn->where = where;
2477 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2478 if (insn->fixp[i] != NULL)
2479 {
2480 insn->fixp[i]->fx_frag = frag;
2481 insn->fixp[i]->fx_where = where;
2482 }
2483 install_insn (insn);
2484}
2485
2486/* Add INSN to the end of the output. */
2487
2488static void
2489add_fixed_insn (struct mips_cl_insn *insn)
2490{
2491 char *f = frag_more (insn_length (insn));
2492 move_insn (insn, frag_now, f - frag_now->fr_literal);
2493}
2494
2495/* Start a variant frag and move INSN to the start of the variant part,
2496 marking it as fixed. The other arguments are as for frag_var. */
2497
2498static void
2499add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2500 relax_substateT subtype, symbolS *symbol, offsetT offset)
2501{
2502 frag_grow (max_chars);
2503 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2504 insn->fixed_p = 1;
2505 frag_var (rs_machine_dependent, max_chars, var,
2506 subtype, symbol, offset, NULL);
2507}
2508
2509/* Insert N copies of INSN into the history buffer, starting at
2510 position FIRST. Neither FIRST nor N need to be clipped. */
2511
2512static void
2513insert_into_history (unsigned int first, unsigned int n,
2514 const struct mips_cl_insn *insn)
2515{
2516 if (mips_relax.sequence != 2)
2517 {
2518 unsigned int i;
2519
2520 for (i = ARRAY_SIZE (history); i-- > first;)
2521 if (i >= first + n)
2522 history[i] = history[i - n];
2523 else
2524 history[i] = *insn;
2525 }
2526}
2527
e3de51ce
RS
2528/* Clear the error in insn_error. */
2529
2530static void
2531clear_insn_error (void)
2532{
2533 memset (&insn_error, 0, sizeof (insn_error));
2534}
2535
2536/* Possibly record error message MSG for the current instruction.
2537 If the error is about a particular argument, ARGNUM is the 1-based
2538 number of that argument, otherwise it is 0. FORMAT is the format
2539 of MSG. Return true if MSG was used, false if the current message
2540 was kept. */
2541
5b7c81bd 2542static bool
e3de51ce
RS
2543set_insn_error_format (int argnum, enum mips_insn_error_format format,
2544 const char *msg)
2545{
2546 if (argnum == 0)
2547 {
2548 /* Give priority to errors against specific arguments, and to
2549 the first whole-instruction message. */
2550 if (insn_error.msg)
5b7c81bd 2551 return false;
e3de51ce
RS
2552 }
2553 else
2554 {
2555 /* Keep insn_error if it is against a later argument. */
2556 if (argnum < insn_error.min_argnum)
5b7c81bd 2557 return false;
e3de51ce
RS
2558
2559 /* If both errors are against the same argument but are different,
2560 give up on reporting a specific error for this argument.
2561 See the comment about mips_insn_error for details. */
2562 if (argnum == insn_error.min_argnum
2563 && insn_error.msg
2564 && strcmp (insn_error.msg, msg) != 0)
2565 {
2566 insn_error.msg = 0;
2567 insn_error.min_argnum += 1;
5b7c81bd 2568 return false;
e3de51ce
RS
2569 }
2570 }
2571 insn_error.min_argnum = argnum;
2572 insn_error.format = format;
2573 insn_error.msg = msg;
5b7c81bd 2574 return true;
e3de51ce
RS
2575}
2576
2577/* Record an instruction error with no % format fields. ARGNUM and MSG are
2578 as for set_insn_error_format. */
2579
2580static void
2581set_insn_error (int argnum, const char *msg)
2582{
2583 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2584}
2585
2586/* Record an instruction error with one %d field I. ARGNUM and MSG are
2587 as for set_insn_error_format. */
2588
2589static void
2590set_insn_error_i (int argnum, const char *msg, int i)
2591{
2592 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2593 insn_error.u.i = i;
2594}
2595
2596/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2597 are as for set_insn_error_format. */
2598
2599static void
2600set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2601{
2602 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2603 {
2604 insn_error.u.ss[0] = s1;
2605 insn_error.u.ss[1] = s2;
2606 }
2607}
2608
2609/* Report the error in insn_error, which is against assembly code STR. */
2610
2611static void
2612report_insn_error (const char *str)
2613{
e1fa0163 2614 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2615
e3de51ce
RS
2616 switch (insn_error.format)
2617 {
2618 case ERR_FMT_PLAIN:
2619 as_bad (msg, str);
2620 break;
2621
2622 case ERR_FMT_I:
2623 as_bad (msg, insn_error.u.i, str);
2624 break;
2625
2626 case ERR_FMT_SS:
2627 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2628 break;
2629 }
e1fa0163
NC
2630
2631 free ((char *) msg);
e3de51ce
RS
2632}
2633
71400594
RS
2634/* Initialize vr4120_conflicts. There is a bit of duplication here:
2635 the idea is to make it obvious at a glance that each errata is
2636 included. */
2637
2638static void
2639init_vr4120_conflicts (void)
2640{
2641#define CONFLICT(FIRST, SECOND) \
2642 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2643
2644 /* Errata 21 - [D]DIV[U] after [D]MACC */
2645 CONFLICT (MACC, DIV);
2646 CONFLICT (DMACC, DIV);
2647
2648 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2649 CONFLICT (DMULT, DMULT);
2650 CONFLICT (DMULT, DMACC);
2651 CONFLICT (DMACC, DMULT);
2652 CONFLICT (DMACC, DMACC);
2653
2654 /* Errata 24 - MT{LO,HI} after [D]MACC */
2655 CONFLICT (MACC, MTHILO);
2656 CONFLICT (DMACC, MTHILO);
2657
2658 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2659 instruction is executed immediately after a MACC or DMACC
2660 instruction, the result of [either instruction] is incorrect." */
2661 CONFLICT (MACC, MULT);
2662 CONFLICT (MACC, DMULT);
2663 CONFLICT (DMACC, MULT);
2664 CONFLICT (DMACC, DMULT);
2665
2666 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2667 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2668 DDIV or DDIVU instruction, the result of the MACC or
2669 DMACC instruction is incorrect.". */
2670 CONFLICT (DMULT, MACC);
2671 CONFLICT (DMULT, DMACC);
2672 CONFLICT (DIV, MACC);
2673 CONFLICT (DIV, DMACC);
2674
2675#undef CONFLICT
2676}
2677
707bfff6
TS
2678struct regname {
2679 const char *name;
2680 unsigned int num;
2681};
2682
14daeee3 2683#define RNUM_MASK 0x00000ff
56d438b1 2684#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2685#define RTYPE_NUM 0x0000100
2686#define RTYPE_FPU 0x0000200
2687#define RTYPE_FCC 0x0000400
2688#define RTYPE_VEC 0x0000800
2689#define RTYPE_GP 0x0001000
2690#define RTYPE_CP0 0x0002000
2691#define RTYPE_PC 0x0004000
2692#define RTYPE_ACC 0x0008000
2693#define RTYPE_CCC 0x0010000
2694#define RTYPE_VI 0x0020000
2695#define RTYPE_VF 0x0040000
2696#define RTYPE_R5900_I 0x0080000
2697#define RTYPE_R5900_Q 0x0100000
2698#define RTYPE_R5900_R 0x0200000
2699#define RTYPE_R5900_ACC 0x0400000
56d438b1 2700#define RTYPE_MSA 0x0800000
14daeee3 2701#define RWARN 0x8000000
707bfff6
TS
2702
2703#define GENERIC_REGISTER_NUMBERS \
2704 {"$0", RTYPE_NUM | 0}, \
2705 {"$1", RTYPE_NUM | 1}, \
2706 {"$2", RTYPE_NUM | 2}, \
2707 {"$3", RTYPE_NUM | 3}, \
2708 {"$4", RTYPE_NUM | 4}, \
2709 {"$5", RTYPE_NUM | 5}, \
2710 {"$6", RTYPE_NUM | 6}, \
2711 {"$7", RTYPE_NUM | 7}, \
2712 {"$8", RTYPE_NUM | 8}, \
2713 {"$9", RTYPE_NUM | 9}, \
2714 {"$10", RTYPE_NUM | 10}, \
2715 {"$11", RTYPE_NUM | 11}, \
2716 {"$12", RTYPE_NUM | 12}, \
2717 {"$13", RTYPE_NUM | 13}, \
2718 {"$14", RTYPE_NUM | 14}, \
2719 {"$15", RTYPE_NUM | 15}, \
2720 {"$16", RTYPE_NUM | 16}, \
2721 {"$17", RTYPE_NUM | 17}, \
2722 {"$18", RTYPE_NUM | 18}, \
2723 {"$19", RTYPE_NUM | 19}, \
2724 {"$20", RTYPE_NUM | 20}, \
2725 {"$21", RTYPE_NUM | 21}, \
2726 {"$22", RTYPE_NUM | 22}, \
2727 {"$23", RTYPE_NUM | 23}, \
2728 {"$24", RTYPE_NUM | 24}, \
2729 {"$25", RTYPE_NUM | 25}, \
2730 {"$26", RTYPE_NUM | 26}, \
2731 {"$27", RTYPE_NUM | 27}, \
2732 {"$28", RTYPE_NUM | 28}, \
2733 {"$29", RTYPE_NUM | 29}, \
2734 {"$30", RTYPE_NUM | 30}, \
3739860c 2735 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2736
2737#define FPU_REGISTER_NAMES \
2738 {"$f0", RTYPE_FPU | 0}, \
2739 {"$f1", RTYPE_FPU | 1}, \
2740 {"$f2", RTYPE_FPU | 2}, \
2741 {"$f3", RTYPE_FPU | 3}, \
2742 {"$f4", RTYPE_FPU | 4}, \
2743 {"$f5", RTYPE_FPU | 5}, \
2744 {"$f6", RTYPE_FPU | 6}, \
2745 {"$f7", RTYPE_FPU | 7}, \
2746 {"$f8", RTYPE_FPU | 8}, \
2747 {"$f9", RTYPE_FPU | 9}, \
2748 {"$f10", RTYPE_FPU | 10}, \
2749 {"$f11", RTYPE_FPU | 11}, \
2750 {"$f12", RTYPE_FPU | 12}, \
2751 {"$f13", RTYPE_FPU | 13}, \
2752 {"$f14", RTYPE_FPU | 14}, \
2753 {"$f15", RTYPE_FPU | 15}, \
2754 {"$f16", RTYPE_FPU | 16}, \
2755 {"$f17", RTYPE_FPU | 17}, \
2756 {"$f18", RTYPE_FPU | 18}, \
2757 {"$f19", RTYPE_FPU | 19}, \
2758 {"$f20", RTYPE_FPU | 20}, \
2759 {"$f21", RTYPE_FPU | 21}, \
2760 {"$f22", RTYPE_FPU | 22}, \
2761 {"$f23", RTYPE_FPU | 23}, \
2762 {"$f24", RTYPE_FPU | 24}, \
2763 {"$f25", RTYPE_FPU | 25}, \
2764 {"$f26", RTYPE_FPU | 26}, \
2765 {"$f27", RTYPE_FPU | 27}, \
2766 {"$f28", RTYPE_FPU | 28}, \
2767 {"$f29", RTYPE_FPU | 29}, \
2768 {"$f30", RTYPE_FPU | 30}, \
2769 {"$f31", RTYPE_FPU | 31}
2770
2771#define FPU_CONDITION_CODE_NAMES \
2772 {"$fcc0", RTYPE_FCC | 0}, \
2773 {"$fcc1", RTYPE_FCC | 1}, \
2774 {"$fcc2", RTYPE_FCC | 2}, \
2775 {"$fcc3", RTYPE_FCC | 3}, \
2776 {"$fcc4", RTYPE_FCC | 4}, \
2777 {"$fcc5", RTYPE_FCC | 5}, \
2778 {"$fcc6", RTYPE_FCC | 6}, \
2779 {"$fcc7", RTYPE_FCC | 7}
2780
2781#define COPROC_CONDITION_CODE_NAMES \
2782 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2783 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2784 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2785 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2786 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2787 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2788 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2789 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2790
2791#define N32N64_SYMBOLIC_REGISTER_NAMES \
2792 {"$a4", RTYPE_GP | 8}, \
2793 {"$a5", RTYPE_GP | 9}, \
2794 {"$a6", RTYPE_GP | 10}, \
2795 {"$a7", RTYPE_GP | 11}, \
2796 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2797 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2798 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2799 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2800 {"$t0", RTYPE_GP | 12}, \
2801 {"$t1", RTYPE_GP | 13}, \
2802 {"$t2", RTYPE_GP | 14}, \
2803 {"$t3", RTYPE_GP | 15}
2804
2805#define O32_SYMBOLIC_REGISTER_NAMES \
2806 {"$t0", RTYPE_GP | 8}, \
2807 {"$t1", RTYPE_GP | 9}, \
2808 {"$t2", RTYPE_GP | 10}, \
2809 {"$t3", RTYPE_GP | 11}, \
2810 {"$t4", RTYPE_GP | 12}, \
2811 {"$t5", RTYPE_GP | 13}, \
2812 {"$t6", RTYPE_GP | 14}, \
2813 {"$t7", RTYPE_GP | 15}, \
2814 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2815 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2816 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2817 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6 2818
6f2117ba 2819/* Remaining symbolic register names. */
707bfff6
TS
2820#define SYMBOLIC_REGISTER_NAMES \
2821 {"$zero", RTYPE_GP | 0}, \
2822 {"$at", RTYPE_GP | 1}, \
2823 {"$AT", RTYPE_GP | 1}, \
2824 {"$v0", RTYPE_GP | 2}, \
2825 {"$v1", RTYPE_GP | 3}, \
2826 {"$a0", RTYPE_GP | 4}, \
2827 {"$a1", RTYPE_GP | 5}, \
2828 {"$a2", RTYPE_GP | 6}, \
2829 {"$a3", RTYPE_GP | 7}, \
2830 {"$s0", RTYPE_GP | 16}, \
2831 {"$s1", RTYPE_GP | 17}, \
2832 {"$s2", RTYPE_GP | 18}, \
2833 {"$s3", RTYPE_GP | 19}, \
2834 {"$s4", RTYPE_GP | 20}, \
2835 {"$s5", RTYPE_GP | 21}, \
2836 {"$s6", RTYPE_GP | 22}, \
2837 {"$s7", RTYPE_GP | 23}, \
2838 {"$t8", RTYPE_GP | 24}, \
2839 {"$t9", RTYPE_GP | 25}, \
2840 {"$k0", RTYPE_GP | 26}, \
2841 {"$kt0", RTYPE_GP | 26}, \
2842 {"$k1", RTYPE_GP | 27}, \
2843 {"$kt1", RTYPE_GP | 27}, \
2844 {"$gp", RTYPE_GP | 28}, \
2845 {"$sp", RTYPE_GP | 29}, \
2846 {"$s8", RTYPE_GP | 30}, \
2847 {"$fp", RTYPE_GP | 30}, \
2848 {"$ra", RTYPE_GP | 31}
2849
2850#define MIPS16_SPECIAL_REGISTER_NAMES \
2851 {"$pc", RTYPE_PC | 0}
2852
2853#define MDMX_VECTOR_REGISTER_NAMES \
6f2117ba
PH
2854 /* {"$v0", RTYPE_VEC | 0}, Clash with REG 2 above. */ \
2855 /* {"$v1", RTYPE_VEC | 1}, Clash with REG 3 above. */ \
707bfff6
TS
2856 {"$v2", RTYPE_VEC | 2}, \
2857 {"$v3", RTYPE_VEC | 3}, \
2858 {"$v4", RTYPE_VEC | 4}, \
2859 {"$v5", RTYPE_VEC | 5}, \
2860 {"$v6", RTYPE_VEC | 6}, \
2861 {"$v7", RTYPE_VEC | 7}, \
2862 {"$v8", RTYPE_VEC | 8}, \
2863 {"$v9", RTYPE_VEC | 9}, \
2864 {"$v10", RTYPE_VEC | 10}, \
2865 {"$v11", RTYPE_VEC | 11}, \
2866 {"$v12", RTYPE_VEC | 12}, \
2867 {"$v13", RTYPE_VEC | 13}, \
2868 {"$v14", RTYPE_VEC | 14}, \
2869 {"$v15", RTYPE_VEC | 15}, \
2870 {"$v16", RTYPE_VEC | 16}, \
2871 {"$v17", RTYPE_VEC | 17}, \
2872 {"$v18", RTYPE_VEC | 18}, \
2873 {"$v19", RTYPE_VEC | 19}, \
2874 {"$v20", RTYPE_VEC | 20}, \
2875 {"$v21", RTYPE_VEC | 21}, \
2876 {"$v22", RTYPE_VEC | 22}, \
2877 {"$v23", RTYPE_VEC | 23}, \
2878 {"$v24", RTYPE_VEC | 24}, \
2879 {"$v25", RTYPE_VEC | 25}, \
2880 {"$v26", RTYPE_VEC | 26}, \
2881 {"$v27", RTYPE_VEC | 27}, \
2882 {"$v28", RTYPE_VEC | 28}, \
2883 {"$v29", RTYPE_VEC | 29}, \
2884 {"$v30", RTYPE_VEC | 30}, \
2885 {"$v31", RTYPE_VEC | 31}
2886
14daeee3
RS
2887#define R5900_I_NAMES \
2888 {"$I", RTYPE_R5900_I | 0}
2889
2890#define R5900_Q_NAMES \
2891 {"$Q", RTYPE_R5900_Q | 0}
2892
2893#define R5900_R_NAMES \
2894 {"$R", RTYPE_R5900_R | 0}
2895
2896#define R5900_ACC_NAMES \
2897 {"$ACC", RTYPE_R5900_ACC | 0 }
2898
707bfff6
TS
2899#define MIPS_DSP_ACCUMULATOR_NAMES \
2900 {"$ac0", RTYPE_ACC | 0}, \
2901 {"$ac1", RTYPE_ACC | 1}, \
2902 {"$ac2", RTYPE_ACC | 2}, \
2903 {"$ac3", RTYPE_ACC | 3}
2904
2905static const struct regname reg_names[] = {
2906 GENERIC_REGISTER_NUMBERS,
2907 FPU_REGISTER_NAMES,
2908 FPU_CONDITION_CODE_NAMES,
2909 COPROC_CONDITION_CODE_NAMES,
2910
2911 /* The $txx registers depends on the abi,
2912 these will be added later into the symbol table from
3739860c 2913 one of the tables below once mips_abi is set after
707bfff6
TS
2914 parsing of arguments from the command line. */
2915 SYMBOLIC_REGISTER_NAMES,
2916
2917 MIPS16_SPECIAL_REGISTER_NAMES,
2918 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2919 R5900_I_NAMES,
2920 R5900_Q_NAMES,
2921 R5900_R_NAMES,
2922 R5900_ACC_NAMES,
707bfff6
TS
2923 MIPS_DSP_ACCUMULATOR_NAMES,
2924 {0, 0}
2925};
2926
2927static const struct regname reg_names_o32[] = {
2928 O32_SYMBOLIC_REGISTER_NAMES,
2929 {0, 0}
2930};
2931
2932static const struct regname reg_names_n32n64[] = {
2933 N32N64_SYMBOLIC_REGISTER_NAMES,
2934 {0, 0}
2935};
2936
a92713e6
RS
2937/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2938 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2939 of these register symbols, return the associated vector register,
2940 otherwise return SYMVAL itself. */
df58fc94 2941
a92713e6
RS
2942static unsigned int
2943mips_prefer_vec_regno (unsigned int symval)
707bfff6 2944{
a92713e6
RS
2945 if ((symval & -2) == (RTYPE_GP | 2))
2946 return RTYPE_VEC | (symval & 1);
2947 return symval;
2948}
2949
14daeee3
RS
2950/* Return true if string [S, E) is a valid register name, storing its
2951 symbol value in *SYMVAL_PTR if so. */
a92713e6 2952
5b7c81bd 2953static bool
14daeee3 2954mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2955{
707bfff6 2956 char save_c;
14daeee3 2957 symbolS *symbol;
707bfff6
TS
2958
2959 /* Terminate name. */
2960 save_c = *e;
2961 *e = '\0';
2962
a92713e6
RS
2963 /* Look up the name. */
2964 symbol = symbol_find (s);
2965 *e = save_c;
2966
2967 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
5b7c81bd 2968 return false;
a92713e6 2969
14daeee3 2970 *symval_ptr = S_GET_VALUE (symbol);
5b7c81bd 2971 return true;
14daeee3
RS
2972}
2973
2974/* Return true if the string at *SPTR is a valid register name. Allow it
2975 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2976 is nonnull.
2977
2978 When returning true, move *SPTR past the register, store the
2979 register's symbol value in *SYMVAL_PTR and the channel mask in
2980 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2981 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2982 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2983
5b7c81bd 2984static bool
14daeee3
RS
2985mips_parse_register (char **sptr, unsigned int *symval_ptr,
2986 unsigned int *channels_ptr)
2987{
2988 char *s, *e, *m;
2989 const char *q;
2990 unsigned int channels, symval, bit;
2991
2992 /* Find end of name. */
2993 s = e = *sptr;
2994 if (is_name_beginner (*e))
2995 ++e;
2996 while (is_part_of_name (*e))
2997 ++e;
2998
2999 channels = 0;
3000 if (!mips_parse_register_1 (s, e, &symval))
3001 {
3002 if (!channels_ptr)
5b7c81bd 3003 return false;
14daeee3
RS
3004
3005 /* Eat characters from the end of the string that are valid
3006 channel suffixes. The preceding register must be $ACC or
3007 end with a digit, so there is no ambiguity. */
3008 bit = 1;
3009 m = e;
3010 for (q = "wzyx"; *q; q++, bit <<= 1)
3011 if (m > s && m[-1] == *q)
3012 {
3013 --m;
3014 channels |= bit;
3015 }
3016
3017 if (channels == 0
3018 || !mips_parse_register_1 (s, m, &symval)
3019 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
5b7c81bd 3020 return false;
14daeee3
RS
3021 }
3022
a92713e6 3023 *sptr = e;
14daeee3
RS
3024 *symval_ptr = symval;
3025 if (channels_ptr)
3026 *channels_ptr = channels;
5b7c81bd 3027 return true;
a92713e6
RS
3028}
3029
3030/* Check if SPTR points at a valid register specifier according to TYPES.
3031 If so, then return 1, advance S to consume the specifier and store
3032 the register's number in REGNOP, otherwise return 0. */
3033
3034static int
3035reg_lookup (char **s, unsigned int types, unsigned int *regnop)
3036{
3037 unsigned int regno;
3038
14daeee3 3039 if (mips_parse_register (s, &regno, NULL))
707bfff6 3040 {
a92713e6
RS
3041 if (types & RTYPE_VEC)
3042 regno = mips_prefer_vec_regno (regno);
3043 if (regno & types)
3044 regno &= RNUM_MASK;
3045 else
3046 regno = ~0;
707bfff6 3047 }
a92713e6 3048 else
707bfff6 3049 {
a92713e6 3050 if (types & RWARN)
1661c76c 3051 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3052 regno = ~0;
707bfff6 3053 }
707bfff6 3054 if (regnop)
a92713e6
RS
3055 *regnop = regno;
3056 return regno <= RNUM_MASK;
707bfff6
TS
3057}
3058
14daeee3
RS
3059/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3060 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3061
3062static char *
3063mips_parse_vu0_channels (char *s, unsigned int *channels)
3064{
3065 unsigned int i;
3066
3067 *channels = 0;
3068 for (i = 0; i < 4; i++)
3069 if (*s == "xyzw"[i])
3070 {
3071 *channels |= 1 << (3 - i);
3072 ++s;
3073 }
3074 return s;
3075}
3076
a92713e6
RS
3077/* Token types for parsed operand lists. */
3078enum mips_operand_token_type {
3079 /* A plain register, e.g. $f2. */
3080 OT_REG,
df58fc94 3081
14daeee3
RS
3082 /* A 4-bit XYZW channel mask. */
3083 OT_CHANNELS,
3084
56d438b1
CF
3085 /* A constant vector index, e.g. [1]. */
3086 OT_INTEGER_INDEX,
3087
3088 /* A register vector index, e.g. [$2]. */
3089 OT_REG_INDEX,
df58fc94 3090
a92713e6
RS
3091 /* A continuous range of registers, e.g. $s0-$s4. */
3092 OT_REG_RANGE,
3093
3094 /* A (possibly relocated) expression. */
3095 OT_INTEGER,
3096
3097 /* A floating-point value. */
3098 OT_FLOAT,
3099
3100 /* A single character. This can be '(', ')' or ',', but '(' only appears
3101 before OT_REGs. */
3102 OT_CHAR,
3103
14daeee3
RS
3104 /* A doubled character, either "--" or "++". */
3105 OT_DOUBLE_CHAR,
3106
a92713e6
RS
3107 /* The end of the operand list. */
3108 OT_END
3109};
3110
3111/* A parsed operand token. */
3112struct mips_operand_token
3113{
3114 /* The type of token. */
3115 enum mips_operand_token_type type;
3116 union
3117 {
56d438b1 3118 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3119 unsigned int regno;
3120
14daeee3
RS
3121 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3122 unsigned int channels;
3123
56d438b1
CF
3124 /* The integer value of an OT_INTEGER_INDEX. */
3125 addressT index;
a92713e6
RS
3126
3127 /* The two register symbol values involved in an OT_REG_RANGE. */
3128 struct {
3129 unsigned int regno1;
3130 unsigned int regno2;
3131 } reg_range;
3132
3133 /* The value of an OT_INTEGER. The value is represented as an
3134 expression and the relocation operators that were applied to
3135 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3136 relocation operators were used. */
3137 struct {
3138 expressionS value;
3139 bfd_reloc_code_real_type relocs[3];
3140 } integer;
3141
3142 /* The binary data for an OT_FLOAT constant, and the number of bytes
3143 in the constant. */
3144 struct {
3145 unsigned char data[8];
3146 int length;
3147 } flt;
3148
14daeee3 3149 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3150 char ch;
3151 } u;
3152};
3153
3154/* An obstack used to construct lists of mips_operand_tokens. */
3155static struct obstack mips_operand_tokens;
3156
3157/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3158
3159static void
3160mips_add_token (struct mips_operand_token *token,
3161 enum mips_operand_token_type type)
3162{
3163 token->type = type;
3164 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3165}
3166
3167/* Check whether S is '(' followed by a register name. Add OT_CHAR
3168 and OT_REG tokens for them if so, and return a pointer to the first
3169 unconsumed character. Return null otherwise. */
3170
3171static char *
3172mips_parse_base_start (char *s)
3173{
3174 struct mips_operand_token token;
14daeee3 3175 unsigned int regno, channels;
5b7c81bd 3176 bool decrement_p;
df58fc94 3177
a92713e6
RS
3178 if (*s != '(')
3179 return 0;
3180
3181 ++s;
3182 SKIP_SPACE_TABS (s);
14daeee3
RS
3183
3184 /* Only match "--" as part of a base expression. In other contexts "--X"
3185 is a double negative. */
3186 decrement_p = (s[0] == '-' && s[1] == '-');
3187 if (decrement_p)
3188 {
3189 s += 2;
3190 SKIP_SPACE_TABS (s);
3191 }
3192
3193 /* Allow a channel specifier because that leads to better error messages
3194 than treating something like "$vf0x++" as an expression. */
3195 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3196 return 0;
3197
3198 token.u.ch = '(';
3199 mips_add_token (&token, OT_CHAR);
3200
14daeee3
RS
3201 if (decrement_p)
3202 {
3203 token.u.ch = '-';
3204 mips_add_token (&token, OT_DOUBLE_CHAR);
3205 }
3206
a92713e6
RS
3207 token.u.regno = regno;
3208 mips_add_token (&token, OT_REG);
3209
14daeee3
RS
3210 if (channels)
3211 {
3212 token.u.channels = channels;
3213 mips_add_token (&token, OT_CHANNELS);
3214 }
3215
3216 /* For consistency, only match "++" as part of base expressions too. */
3217 SKIP_SPACE_TABS (s);
3218 if (s[0] == '+' && s[1] == '+')
3219 {
3220 s += 2;
3221 token.u.ch = '+';
3222 mips_add_token (&token, OT_DOUBLE_CHAR);
3223 }
3224
a92713e6
RS
3225 return s;
3226}
3227
3228/* Parse one or more tokens from S. Return a pointer to the first
3229 unconsumed character on success. Return null if an error was found
3230 and store the error text in insn_error. FLOAT_FORMAT is as for
3231 mips_parse_arguments. */
3232
3233static char *
3234mips_parse_argument_token (char *s, char float_format)
3235{
6d4af3c2
AM
3236 char *end, *save_in;
3237 const char *err;
14daeee3 3238 unsigned int regno1, regno2, channels;
a92713e6
RS
3239 struct mips_operand_token token;
3240
3241 /* First look for "($reg", since we want to treat that as an
3242 OT_CHAR and OT_REG rather than an expression. */
3243 end = mips_parse_base_start (s);
3244 if (end)
3245 return end;
3246
3247 /* Handle other characters that end up as OT_CHARs. */
3248 if (*s == ')' || *s == ',')
3249 {
3250 token.u.ch = *s;
3251 mips_add_token (&token, OT_CHAR);
3252 ++s;
3253 return s;
3254 }
3255
3256 /* Handle tokens that start with a register. */
14daeee3 3257 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3258 {
14daeee3
RS
3259 if (channels)
3260 {
3261 /* A register and a VU0 channel suffix. */
3262 token.u.regno = regno1;
3263 mips_add_token (&token, OT_REG);
3264
3265 token.u.channels = channels;
3266 mips_add_token (&token, OT_CHANNELS);
3267 return s;
3268 }
3269
a92713e6
RS
3270 SKIP_SPACE_TABS (s);
3271 if (*s == '-')
df58fc94 3272 {
a92713e6
RS
3273 /* A register range. */
3274 ++s;
3275 SKIP_SPACE_TABS (s);
14daeee3 3276 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3277 {
1661c76c 3278 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3279 return 0;
3280 }
df58fc94 3281
a92713e6
RS
3282 token.u.reg_range.regno1 = regno1;
3283 token.u.reg_range.regno2 = regno2;
3284 mips_add_token (&token, OT_REG_RANGE);
3285 return s;
3286 }
a92713e6 3287
56d438b1
CF
3288 /* Add the register itself. */
3289 token.u.regno = regno1;
3290 mips_add_token (&token, OT_REG);
3291
3292 /* Check for a vector index. */
3293 if (*s == '[')
3294 {
a92713e6
RS
3295 ++s;
3296 SKIP_SPACE_TABS (s);
56d438b1
CF
3297 if (mips_parse_register (&s, &token.u.regno, NULL))
3298 mips_add_token (&token, OT_REG_INDEX);
3299 else
a92713e6 3300 {
56d438b1
CF
3301 expressionS element;
3302
3303 my_getExpression (&element, s);
3304 if (element.X_op != O_constant)
3305 {
3306 set_insn_error (0, _("vector element must be constant"));
3307 return 0;
3308 }
6eb099ae 3309 s = expr_parse_end;
56d438b1
CF
3310 token.u.index = element.X_add_number;
3311 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3312 }
a92713e6
RS
3313 SKIP_SPACE_TABS (s);
3314 if (*s != ']')
3315 {
1661c76c 3316 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3317 return 0;
3318 }
3319 ++s;
df58fc94 3320 }
a92713e6 3321 return s;
df58fc94
RS
3322 }
3323
a92713e6
RS
3324 if (float_format)
3325 {
3326 /* First try to treat expressions as floats. */
3327 save_in = input_line_pointer;
3328 input_line_pointer = s;
3329 err = md_atof (float_format, (char *) token.u.flt.data,
3330 &token.u.flt.length);
3331 end = input_line_pointer;
3332 input_line_pointer = save_in;
3333 if (err && *err)
3334 {
e3de51ce 3335 set_insn_error (0, err);
a92713e6
RS
3336 return 0;
3337 }
3338 if (s != end)
3339 {
3340 mips_add_token (&token, OT_FLOAT);
3341 return end;
3342 }
3343 }
3344
3345 /* Treat everything else as an integer expression. */
3346 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3347 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3348 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3349 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
6eb099ae 3350 s = expr_parse_end;
a92713e6
RS
3351 mips_add_token (&token, OT_INTEGER);
3352 return s;
3353}
3354
3355/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3356 if expressions should be treated as 32-bit floating-point constants,
3357 'd' if they should be treated as 64-bit floating-point constants,
3358 or 0 if they should be treated as integer expressions (the usual case).
3359
3360 Return a list of tokens on success, otherwise return 0. The caller
3361 must obstack_free the list after use. */
3362
3363static struct mips_operand_token *
3364mips_parse_arguments (char *s, char float_format)
3365{
3366 struct mips_operand_token token;
3367
3368 SKIP_SPACE_TABS (s);
3369 while (*s)
3370 {
3371 s = mips_parse_argument_token (s, float_format);
3372 if (!s)
3373 {
3374 obstack_free (&mips_operand_tokens,
3375 obstack_finish (&mips_operand_tokens));
3376 return 0;
3377 }
3378 SKIP_SPACE_TABS (s);
3379 }
3380 mips_add_token (&token, OT_END);
3381 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3382}
3383
d301a56b
RS
3384/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3385 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9 3386
5b7c81bd 3387static bool
f79e2745 3388is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3389{
3390 int isa = mips_opts.isa;
846ef2d0 3391 int ase = mips_opts.ase;
037b32b9 3392 int fp_s, fp_d;
c6278170 3393 unsigned int i;
037b32b9 3394
be0fcbee 3395 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3396 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3397 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3398 ase |= mips_ases[i].flags64;
037b32b9 3399
d301a56b 3400 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
5b7c81bd 3401 return false;
037b32b9
AN
3402
3403 /* Check whether the instruction or macro requires single-precision or
3404 double-precision floating-point support. Note that this information is
3405 stored differently in the opcode table for insns and macros. */
3406 if (mo->pinfo == INSN_MACRO)
3407 {
3408 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3409 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3410 }
3411 else
3412 {
3413 fp_s = mo->pinfo & FP_S;
3414 fp_d = mo->pinfo & FP_D;
3415 }
3416
3417 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
5b7c81bd 3418 return false;
037b32b9
AN
3419
3420 if (fp_s && mips_opts.soft_float)
5b7c81bd 3421 return false;
037b32b9 3422
5b7c81bd 3423 return true;
037b32b9
AN
3424}
3425
3426/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3427 selected ISA and architecture. */
3428
5b7c81bd 3429static bool
037b32b9
AN
3430is_opcode_valid_16 (const struct mips_opcode *mo)
3431{
25499ac7
MR
3432 int isa = mips_opts.isa;
3433 int ase = mips_opts.ase;
3434 unsigned int i;
3435
3436 if (ISA_HAS_64BIT_REGS (isa))
3437 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3438 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3439 ase |= mips_ases[i].flags64;
3440
3441 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3442}
3443
df58fc94 3444/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3445 explicitly requested. Always TRUE in the standard MIPS mode.
3446 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94 3447
5b7c81bd 3448static bool
df58fc94
RS
3449is_size_valid (const struct mips_opcode *mo)
3450{
3451 if (!mips_opts.micromips)
5b7c81bd 3452 return true;
df58fc94 3453
833794fc
MR
3454 if (mips_opts.insn32)
3455 {
3456 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
5b7c81bd 3457 return false;
833794fc 3458 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
5b7c81bd 3459 return false;
833794fc 3460 }
df58fc94 3461 if (!forced_insn_length)
5b7c81bd 3462 return true;
df58fc94 3463 if (mo->pinfo == INSN_MACRO)
5b7c81bd 3464 return false;
df58fc94
RS
3465 return forced_insn_length == micromips_insn_length (mo);
3466}
3467
7fd53920
MR
3468/* Return TRUE if the size of the MIPS16 opcode MO matches one
3469 explicitly requested. */
3470
5b7c81bd 3471static bool
7fd53920
MR
3472is_size_valid_16 (const struct mips_opcode *mo)
3473{
3474 if (!forced_insn_length)
5b7c81bd 3475 return true;
7fd53920 3476 if (mo->pinfo == INSN_MACRO)
5b7c81bd 3477 return false;
7fd53920 3478 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
5b7c81bd 3479 return false;
0674ee5d 3480 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
5b7c81bd
AM
3481 return false;
3482 return true;
7fd53920
MR
3483}
3484
df58fc94 3485/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3486 of the preceding instruction. Always TRUE in the standard MIPS mode.
3487
3488 We don't accept macros in 16-bit delay slots to avoid a case where
3489 a macro expansion fails because it relies on a preceding 32-bit real
3490 instruction to have matched and does not handle the operands correctly.
3491 The only macros that may expand to 16-bit instructions are JAL that
3492 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3493 and BGT (that likewise cannot be placed in a delay slot) that decay to
3494 a NOP. In all these cases the macros precede any corresponding real
3495 instruction definitions in the opcode table, so they will match in the
3496 second pass where the size of the delay slot is ignored and therefore
3497 produce correct code. */
df58fc94 3498
5b7c81bd 3499static bool
df58fc94
RS
3500is_delay_slot_valid (const struct mips_opcode *mo)
3501{
3502 if (!mips_opts.micromips)
5b7c81bd 3503 return true;
df58fc94
RS
3504
3505 if (mo->pinfo == INSN_MACRO)
c06dec14 3506 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3507 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3508 && micromips_insn_length (mo) != 4)
5b7c81bd 3509 return false;
df58fc94
RS
3510 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3511 && micromips_insn_length (mo) != 2)
5b7c81bd 3512 return false;
df58fc94 3513
5b7c81bd 3514 return true;
df58fc94
RS
3515}
3516
fc76e730
RS
3517/* For consistency checking, verify that all bits of OPCODE are specified
3518 either by the match/mask part of the instruction definition, or by the
3519 operand list. Also build up a list of operands in OPERANDS.
3520
3521 INSN_BITS says which bits of the instruction are significant.
3522 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3523 provides the mips_operand description of each operand. DECODE_OPERAND
3524 is null for MIPS16 instructions. */
ab902481
RS
3525
3526static int
3527validate_mips_insn (const struct mips_opcode *opcode,
3528 unsigned long insn_bits,
fc76e730
RS
3529 const struct mips_operand *(*decode_operand) (const char *),
3530 struct mips_operand_array *operands)
ab902481
RS
3531{
3532 const char *s;
fc76e730 3533 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3534 const struct mips_operand *operand;
3535
fc76e730
RS
3536 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3537 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3538 {
3539 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3540 opcode->name, opcode->args);
3541 return 0;
3542 }
3543 used_bits = 0;
fc76e730 3544 opno = 0;
14daeee3
RS
3545 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3546 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3547 for (s = opcode->args; *s; ++s)
3548 switch (*s)
3549 {
3550 case ',':
3551 case '(':
3552 case ')':
3553 break;
3554
14daeee3
RS
3555 case '#':
3556 s++;
3557 break;
3558
ab902481 3559 default:
fc76e730 3560 if (!decode_operand)
7fd53920 3561 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3562 else
3563 operand = decode_operand (s);
3564 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3565 {
3566 as_bad (_("internal: unknown operand type: %s %s"),
3567 opcode->name, opcode->args);
3568 return 0;
3569 }
fc76e730
RS
3570 gas_assert (opno < MAX_OPERANDS);
3571 operands->operand[opno] = operand;
25499ac7
MR
3572 if (!decode_operand && operand
3573 && operand->type == OP_INT && operand->lsb == 0
3574 && mips_opcode_32bit_p (opcode))
3575 used_bits |= mips16_immed_extend (-1, operand->size);
3576 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3577 {
14daeee3 3578 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3579 if (operand->type == OP_MDMX_IMM_REG)
3580 /* Bit 5 is the format selector (OB vs QH). The opcode table
3581 has separate entries for each format. */
3582 used_bits &= ~(1 << (operand->lsb + 5));
3583 if (operand->type == OP_ENTRY_EXIT_LIST)
3584 used_bits &= ~(mask & 0x700);
38bf472a
MR
3585 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3586 operand field that cannot be fully described with LSB/SIZE. */
3587 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3588 used_bits &= ~0x6000;
fc76e730 3589 }
ab902481 3590 /* Skip prefix characters. */
7361da2c 3591 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3592 ++s;
fc76e730 3593 opno += 1;
ab902481
RS
3594 break;
3595 }
fc76e730 3596 doubled = used_bits & mask & insn_bits;
ab902481
RS
3597 if (doubled)
3598 {
3599 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3600 " %s %s"), doubled, opcode->name, opcode->args);
3601 return 0;
3602 }
fc76e730 3603 used_bits |= mask;
ab902481 3604 undefined = ~used_bits & insn_bits;
fc76e730 3605 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3606 {
3607 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3608 undefined, opcode->name, opcode->args);
3609 return 0;
3610 }
3611 used_bits &= ~insn_bits;
3612 if (used_bits)
3613 {
3614 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3615 used_bits, opcode->name, opcode->args);
3616 return 0;
3617 }
3618 return 1;
3619}
3620
fc76e730
RS
3621/* The MIPS16 version of validate_mips_insn. */
3622
3623static int
3624validate_mips16_insn (const struct mips_opcode *opcode,
3625 struct mips_operand_array *operands)
3626{
7fd53920 3627 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3628
7fd53920 3629 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3630}
3631
ab902481
RS
3632/* The microMIPS version of validate_mips_insn. */
3633
3634static int
fc76e730
RS
3635validate_micromips_insn (const struct mips_opcode *opc,
3636 struct mips_operand_array *operands)
ab902481
RS
3637{
3638 unsigned long insn_bits;
3639 unsigned long major;
3640 unsigned int length;
3641
fc76e730
RS
3642 if (opc->pinfo == INSN_MACRO)
3643 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3644 operands);
3645
ab902481
RS
3646 length = micromips_insn_length (opc);
3647 if (length != 2 && length != 4)
3648 {
1661c76c 3649 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3650 "%s %s"), length, opc->name, opc->args);
3651 return 0;
3652 }
3653 major = opc->match >> (10 + 8 * (length - 2));
3654 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3655 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3656 {
1661c76c 3657 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3658 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3659 return 0;
3660 }
3661
3662 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3663 insn_bits = 1 << 4 * length;
3664 insn_bits <<= 4 * length;
3665 insn_bits -= 1;
fc76e730
RS
3666 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3667 operands);
ab902481
RS
3668}
3669
707bfff6
TS
3670/* This function is called once, at assembler startup time. It should set up
3671 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3672
252b5132 3673void
17a2f251 3674md_begin (void)
252b5132 3675{
156c2f8b 3676 int i = 0;
252b5132 3677 int broken = 0;
1f25f5d3 3678
0a44bf69
RS
3679 if (mips_pic != NO_PIC)
3680 {
3681 if (g_switch_seen && g_switch_value != 0)
3682 as_bad (_("-G may not be used in position-independent code"));
3683 g_switch_value = 0;
3684 }
00acd688
CM
3685 else if (mips_abicalls)
3686 {
3687 if (g_switch_seen && g_switch_value != 0)
3688 as_bad (_("-G may not be used with abicalls"));
3689 g_switch_value = 0;
3690 }
0a44bf69 3691
0b35dfee 3692 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3693 as_warn (_("could not set architecture and machine"));
252b5132 3694
629310ab 3695 op_hash = str_htab_create ();
252b5132 3696
fc76e730 3697 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3698 for (i = 0; i < NUMOPCODES;)
3699 {
3700 const char *name = mips_opcodes[i].name;
3701
fe0e921f
AM
3702 if (str_hash_insert (op_hash, name, &mips_opcodes[i], 0) != NULL)
3703 as_fatal (_("duplicate %s"), name);
252b5132
RH
3704 do
3705 {
fc76e730
RS
3706 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3707 decode_mips_operand, &mips_operands[i]))
3708 broken = 1;
6f2117ba 3709
fc76e730 3710 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3711 {
fc76e730
RS
3712 create_insn (&nop_insn, mips_opcodes + i);
3713 if (mips_fix_loongson2f_nop)
3714 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3715 nop_insn.fixed_p = 1;
252b5132 3716 }
6f2117ba
PH
3717
3718 if (sync_insn.insn_mo == NULL && strcmp (name, "sync") == 0)
3719 create_insn (&sync_insn, mips_opcodes + i);
3720
252b5132
RH
3721 ++i;
3722 }
3723 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3724 }
3725
629310ab 3726 mips16_op_hash = str_htab_create ();
fc76e730
RS
3727 mips16_operands = XCNEWVEC (struct mips_operand_array,
3728 bfd_mips16_num_opcodes);
252b5132
RH
3729
3730 i = 0;
3731 while (i < bfd_mips16_num_opcodes)
3732 {
3733 const char *name = mips16_opcodes[i].name;
3734
fe0e921f
AM
3735 if (str_hash_insert (mips16_op_hash, name, &mips16_opcodes[i], 0))
3736 as_fatal (_("duplicate %s"), name);
252b5132
RH
3737 do
3738 {
fc76e730
RS
3739 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3740 broken = 1;
1e915849
RS
3741 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3742 {
3743 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3744 mips16_nop_insn.fixed_p = 1;
3745 }
252b5132
RH
3746 ++i;
3747 }
3748 while (i < bfd_mips16_num_opcodes
3749 && strcmp (mips16_opcodes[i].name, name) == 0);
3750 }
3751
629310ab 3752 micromips_op_hash = str_htab_create ();
fc76e730
RS
3753 micromips_operands = XCNEWVEC (struct mips_operand_array,
3754 bfd_micromips_num_opcodes);
df58fc94
RS
3755
3756 i = 0;
3757 while (i < bfd_micromips_num_opcodes)
3758 {
3759 const char *name = micromips_opcodes[i].name;
3760
fe0e921f
AM
3761 if (str_hash_insert (micromips_op_hash, name, &micromips_opcodes[i], 0))
3762 as_fatal (_("duplicate %s"), name);
df58fc94 3763 do
fc76e730
RS
3764 {
3765 struct mips_cl_insn *micromips_nop_insn;
3766
3767 if (!validate_micromips_insn (&micromips_opcodes[i],
3768 &micromips_operands[i]))
3769 broken = 1;
3770
3771 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3772 {
3773 if (micromips_insn_length (micromips_opcodes + i) == 2)
3774 micromips_nop_insn = &micromips_nop16_insn;
3775 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3776 micromips_nop_insn = &micromips_nop32_insn;
3777 else
3778 continue;
3779
3780 if (micromips_nop_insn->insn_mo == NULL
3781 && strcmp (name, "nop") == 0)
3782 {
3783 create_insn (micromips_nop_insn, micromips_opcodes + i);
3784 micromips_nop_insn->fixed_p = 1;
3785 }
3786 }
3787 }
df58fc94
RS
3788 while (++i < bfd_micromips_num_opcodes
3789 && strcmp (micromips_opcodes[i].name, name) == 0);
3790 }
3791
252b5132 3792 if (broken)
1661c76c 3793 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3794
3795 /* We add all the general register names to the symbol table. This
3796 helps us detect invalid uses of them. */
3739860c 3797 for (i = 0; reg_names[i].name; i++)
707bfff6 3798 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
e01e1cee
AM
3799 &zero_address_frag,
3800 reg_names[i].num));
707bfff6 3801 if (HAVE_NEWABI)
3739860c 3802 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3803 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
e01e1cee
AM
3804 &zero_address_frag,
3805 reg_names_n32n64[i].num));
707bfff6 3806 else
3739860c 3807 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3808 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
e01e1cee
AM
3809 &zero_address_frag,
3810 reg_names_o32[i].num));
6047c971 3811
14daeee3
RS
3812 for (i = 0; i < 32; i++)
3813 {
ca159256 3814 char regname[16];
14daeee3
RS
3815
3816 /* R5900 VU0 floating-point register. */
92fce9bd 3817 sprintf (regname, "$vf%d", i);
14daeee3 3818 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3819 &zero_address_frag, RTYPE_VF | i));
14daeee3
RS
3820
3821 /* R5900 VU0 integer register. */
92fce9bd 3822 sprintf (regname, "$vi%d", i);
14daeee3 3823 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3824 &zero_address_frag, RTYPE_VI | i));
14daeee3 3825
56d438b1 3826 /* MSA register. */
92fce9bd 3827 sprintf (regname, "$w%d", i);
56d438b1 3828 symbol_table_insert (symbol_new (regname, reg_section,
e01e1cee 3829 &zero_address_frag, RTYPE_MSA | i));
14daeee3
RS
3830 }
3831
a92713e6
RS
3832 obstack_init (&mips_operand_tokens);
3833
7d10b47d 3834 mips_no_prev_insn ();
252b5132
RH
3835
3836 mips_gprmask = 0;
3837 mips_cprmask[0] = 0;
3838 mips_cprmask[1] = 0;
3839 mips_cprmask[2] = 0;
3840 mips_cprmask[3] = 0;
3841
3842 /* set the default alignment for the text section (2**2) */
3843 record_alignment (text_section, 2);
3844
4d0d148d 3845 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3846
f3ded42a
RS
3847 /* On a native system other than VxWorks, sections must be aligned
3848 to 16 byte boundaries. When configured for an embedded ELF
3849 target, we don't bother. */
d34049e8
ML
3850 if (!startswith (TARGET_OS, "elf")
3851 && !startswith (TARGET_OS, "vxworks"))
252b5132 3852 {
fd361982
AM
3853 bfd_set_section_alignment (text_section, 4);
3854 bfd_set_section_alignment (data_section, 4);
3855 bfd_set_section_alignment (bss_section, 4);
f3ded42a 3856 }
252b5132 3857
f3ded42a
RS
3858 /* Create a .reginfo section for register masks and a .mdebug
3859 section for debugging information. */
3860 {
3861 segT seg;
3862 subsegT subseg;
3863 flagword flags;
3864 segT sec;
3865
3866 seg = now_seg;
3867 subseg = now_subseg;
3868
3869 /* The ABI says this section should be loaded so that the
3870 running program can access it. However, we don't load it
6f2117ba 3871 if we are configured for an embedded target. */
f3ded42a 3872 flags = SEC_READONLY | SEC_DATA;
d34049e8 3873 if (!startswith (TARGET_OS, "elf"))
f3ded42a
RS
3874 flags |= SEC_ALLOC | SEC_LOAD;
3875
3876 if (mips_abi != N64_ABI)
252b5132 3877 {
f3ded42a 3878 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3879
fd361982
AM
3880 bfd_set_section_flags (sec, flags);
3881 bfd_set_section_alignment (sec, HAVE_NEWABI ? 3 : 2);
252b5132 3882
f3ded42a
RS
3883 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3884 }
3885 else
3886 {
3887 /* The 64-bit ABI uses a .MIPS.options section rather than
3888 .reginfo section. */
3889 sec = subseg_new (".MIPS.options", (subsegT) 0);
fd361982
AM
3890 bfd_set_section_flags (sec, flags);
3891 bfd_set_section_alignment (sec, 3);
252b5132 3892
f3ded42a
RS
3893 /* Set up the option header. */
3894 {
3895 Elf_Internal_Options opthdr;
3896 char *f;
3897
3898 opthdr.kind = ODK_REGINFO;
3899 opthdr.size = (sizeof (Elf_External_Options)
3900 + sizeof (Elf64_External_RegInfo));
3901 opthdr.section = 0;
3902 opthdr.info = 0;
3903 f = frag_more (sizeof (Elf_External_Options));
3904 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3905 (Elf_External_Options *) f);
3906
3907 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3908 }
3909 }
252b5132 3910
351cdf24 3911 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
fd361982 3912 bfd_set_section_flags (sec,
351cdf24 3913 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
fd361982 3914 bfd_set_section_alignment (sec, 3);
351cdf24
MF
3915 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3916
f3ded42a
RS
3917 if (ECOFF_DEBUGGING)
3918 {
3919 sec = subseg_new (".mdebug", (subsegT) 0);
fd361982
AM
3920 bfd_set_section_flags (sec, SEC_HAS_CONTENTS | SEC_READONLY);
3921 bfd_set_section_alignment (sec, 2);
252b5132 3922 }
f3ded42a
RS
3923 else if (mips_flag_pdr)
3924 {
3925 pdr_seg = subseg_new (".pdr", (subsegT) 0);
fd361982
AM
3926 bfd_set_section_flags (pdr_seg,
3927 SEC_READONLY | SEC_RELOC | SEC_DEBUGGING);
3928 bfd_set_section_alignment (pdr_seg, 2);
f3ded42a
RS
3929 }
3930
3931 subseg_set (seg, subseg);
3932 }
252b5132 3933
71400594
RS
3934 if (mips_fix_vr4120)
3935 init_vr4120_conflicts ();
252b5132
RH
3936}
3937
351cdf24
MF
3938static inline void
3939fpabi_incompatible_with (int fpabi, const char *what)
3940{
3941 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3942 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3943}
3944
3945static inline void
3946fpabi_requires (int fpabi, const char *what)
3947{
3948 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3949 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3950}
3951
3952/* Check -mabi and register sizes against the specified FP ABI. */
3953static void
3954check_fpabi (int fpabi)
3955{
351cdf24
MF
3956 switch (fpabi)
3957 {
3958 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3959 if (file_mips_opts.soft_float)
3960 fpabi_incompatible_with (fpabi, "softfloat");
3961 else if (file_mips_opts.single_float)
3962 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3963 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3964 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3965 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3966 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3967 break;
3968
3969 case Val_GNU_MIPS_ABI_FP_XX:
3970 if (mips_abi != O32_ABI)
3971 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3972 else if (file_mips_opts.soft_float)
3973 fpabi_incompatible_with (fpabi, "softfloat");
3974 else if (file_mips_opts.single_float)
3975 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3976 else if (file_mips_opts.fp != 0)
3977 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3978 break;
3979
3980 case Val_GNU_MIPS_ABI_FP_64A:
3981 case Val_GNU_MIPS_ABI_FP_64:
3982 if (mips_abi != O32_ABI)
3983 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3984 else if (file_mips_opts.soft_float)
3985 fpabi_incompatible_with (fpabi, "softfloat");
3986 else if (file_mips_opts.single_float)
3987 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3988 else if (file_mips_opts.fp != 64)
3989 fpabi_requires (fpabi, "fp=64");
3990 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3991 fpabi_incompatible_with (fpabi, "nooddspreg");
3992 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3993 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3994 break;
3995
3996 case Val_GNU_MIPS_ABI_FP_SINGLE:
3997 if (file_mips_opts.soft_float)
3998 fpabi_incompatible_with (fpabi, "softfloat");
3999 else if (!file_mips_opts.single_float)
4000 fpabi_requires (fpabi, "singlefloat");
4001 break;
4002
4003 case Val_GNU_MIPS_ABI_FP_SOFT:
4004 if (!file_mips_opts.soft_float)
4005 fpabi_requires (fpabi, "softfloat");
4006 break;
4007
4008 case Val_GNU_MIPS_ABI_FP_OLD_64:
4009 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
4010 Tag_GNU_MIPS_ABI_FP, fpabi);
4011 break;
4012
3350cc01
CM
4013 case Val_GNU_MIPS_ABI_FP_NAN2008:
4014 /* Silently ignore compatibility value. */
4015 break;
4016
351cdf24
MF
4017 default:
4018 as_warn (_(".gnu_attribute %d,%d is not a recognized"
4019 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
4020 break;
4021 }
351cdf24
MF
4022}
4023
919731af 4024/* Perform consistency checks on the current options. */
4025
4026static void
5b7c81bd 4027mips_check_options (struct mips_set_options *opts, bool abi_checks)
919731af 4028{
4029 /* Check the size of integer registers agrees with the ABI and ISA. */
4030 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
4031 as_bad (_("`gp=64' used with a 32-bit processor"));
4032 else if (abi_checks
4033 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
4034 as_bad (_("`gp=32' used with a 64-bit ABI"));
4035 else if (abi_checks
4036 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
4037 as_bad (_("`gp=64' used with a 32-bit ABI"));
4038
4039 /* Check the size of the float registers agrees with the ABI and ISA. */
4040 switch (opts->fp)
4041 {
351cdf24
MF
4042 case 0:
4043 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4044 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4045 else if (opts->single_float == 1)
4046 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4047 break;
919731af 4048 case 64:
4049 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4050 as_bad (_("`fp=64' used with a 32-bit fpu"));
4051 else if (abi_checks
4052 && ABI_NEEDS_32BIT_REGS (mips_abi)
4053 && !ISA_HAS_MXHC1 (opts->isa))
4054 as_warn (_("`fp=64' used with a 32-bit ABI"));
4055 break;
4056 case 32:
4057 if (abi_checks
4058 && ABI_NEEDS_64BIT_REGS (mips_abi))
4059 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4060 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4061 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4062 break;
4063 default:
4064 as_bad (_("Unknown size of floating point registers"));
4065 break;
4066 }
4067
351cdf24
MF
4068 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4069 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4070
919731af 4071 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4072 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4073 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4074 && (opts->micromips == 1
4075 || opts->mips16 == 1))
1357373c 4076 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4077 opts->micromips ? "micromips" : "mips16",
5f4678bb 4078 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4079
4080 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4081 as_fatal (_("branch relaxation is not supported in `%s'"),
4082 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4083}
4084
4085/* Perform consistency checks on the module level options exactly once.
4086 This is a deferred check that happens:
4087 at the first .set directive
4088 or, at the first pseudo op that generates code (inc .dc.a)
4089 or, at the first instruction
4090 or, at the end. */
4091
4092static void
4093file_mips_check_options (void)
4094{
919731af 4095 if (file_mips_opts_checked)
4096 return;
4097
4098 /* The following code determines the register size.
4099 Similar code was added to GCC 3.3 (see override_options() in
4100 config/mips/mips.c). The GAS and GCC code should be kept in sync
4101 as much as possible. */
4102
4103 if (file_mips_opts.gp < 0)
4104 {
4105 /* Infer the integer register size from the ABI and processor.
4106 Restrict ourselves to 32-bit registers if that's all the
4107 processor has, or if the ABI cannot handle 64-bit registers. */
4108 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4109 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4110 ? 32 : 64;
4111 }
4112
4113 if (file_mips_opts.fp < 0)
4114 {
4115 /* No user specified float register size.
4116 ??? GAS treats single-float processors as though they had 64-bit
4117 float registers (although it complains when double-precision
4118 instructions are used). As things stand, saying they have 32-bit
4119 registers would lead to spurious "register must be even" messages.
4120 So here we assume float registers are never smaller than the
4121 integer ones. */
4122 if (file_mips_opts.gp == 64)
4123 /* 64-bit integer registers implies 64-bit float registers. */
4124 file_mips_opts.fp = 64;
4125 else if ((file_mips_opts.ase & FP64_ASES)
4126 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4127 /* Handle ASEs that require 64-bit float registers, if possible. */
4128 file_mips_opts.fp = 64;
7361da2c
AB
4129 else if (ISA_IS_R6 (mips_opts.isa))
4130 /* R6 implies 64-bit float registers. */
4131 file_mips_opts.fp = 64;
919731af 4132 else
4133 /* 32-bit float registers. */
4134 file_mips_opts.fp = 32;
4135 }
4136
351cdf24
MF
4137 /* Disable operations on odd-numbered floating-point registers by default
4138 when using the FPXX ABI. */
4139 if (file_mips_opts.oddspreg < 0)
4140 {
4141 if (file_mips_opts.fp == 0)
4142 file_mips_opts.oddspreg = 0;
4143 else
4144 file_mips_opts.oddspreg = 1;
4145 }
4146
919731af 4147 /* End of GCC-shared inference code. */
4148
4149 /* This flag is set when we have a 64-bit capable CPU but use only
4150 32-bit wide registers. Note that EABI does not use it. */
4151 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4152 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4153 || mips_abi == O32_ABI))
4154 mips_32bitmode = 1;
4155
4156 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4157 as_bad (_("trap exception not supported at ISA 1"));
4158
4159 /* If the selected architecture includes support for ASEs, enable
4160 generation of code for them. */
4161 if (file_mips_opts.mips16 == -1)
4162 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4163 if (file_mips_opts.micromips == -1)
4164 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4165 ? 1 : 0;
4166
7361da2c
AB
4167 if (mips_nan2008 == -1)
4168 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4169 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4170 as_fatal (_("`%s' does not support legacy NaN"),
4171 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4172
919731af 4173 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4174 being selected implicitly. */
4175 if (file_mips_opts.fp != 64)
4176 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4177
4178 /* If the user didn't explicitly select or deselect a particular ASE,
4179 use the default setting for the CPU. */
3315614d 4180 file_mips_opts.ase |= (file_mips_opts.init_ase & ~file_ase_explicit);
919731af 4181
4182 /* Set up the current options. These may change throughout assembly. */
4183 mips_opts = file_mips_opts;
4184
4185 mips_check_isa_supports_ases ();
5b7c81bd
AM
4186 mips_check_options (&file_mips_opts, true);
4187 file_mips_opts_checked = true;
919731af 4188
4189 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4190 as_warn (_("could not set architecture and machine"));
4191}
4192
252b5132 4193void
17a2f251 4194md_assemble (char *str)
252b5132
RH
4195{
4196 struct mips_cl_insn insn;
f6688943
TS
4197 bfd_reloc_code_real_type unused_reloc[3]
4198 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4199
919731af 4200 file_mips_check_options ();
4201
252b5132 4202 imm_expr.X_op = O_absent;
252b5132 4203 offset_expr.X_op = O_absent;
f6688943
TS
4204 offset_reloc[0] = BFD_RELOC_UNUSED;
4205 offset_reloc[1] = BFD_RELOC_UNUSED;
4206 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4207
e1b47bd5 4208 mips_mark_labels ();
5b7c81bd 4209 mips_assembling_insn = true;
e3de51ce 4210 clear_insn_error ();
e1b47bd5 4211
252b5132
RH
4212 if (mips_opts.mips16)
4213 mips16_ip (str, &insn);
4214 else
4215 {
4216 mips_ip (str, &insn);
beae10d5
KH
4217 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4218 str, insn.insn_opcode));
252b5132
RH
4219 }
4220
e3de51ce
RS
4221 if (insn_error.msg)
4222 report_insn_error (str);
e1b47bd5 4223 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4224 {
584892a6 4225 macro_start ();
252b5132
RH
4226 if (mips_opts.mips16)
4227 mips16_macro (&insn);
4228 else
833794fc 4229 macro (&insn, str);
584892a6 4230 macro_end ();
252b5132
RH
4231 }
4232 else
4233 {
77bd4346 4234 if (offset_expr.X_op != O_absent)
5b7c81bd 4235 append_insn (&insn, &offset_expr, offset_reloc, false);
252b5132 4236 else
5b7c81bd 4237 append_insn (&insn, NULL, unused_reloc, false);
252b5132 4238 }
e1b47bd5 4239
5b7c81bd 4240 mips_assembling_insn = false;
252b5132
RH
4241}
4242
738e5348
RS
4243/* Convenience functions for abstracting away the differences between
4244 MIPS16 and non-MIPS16 relocations. */
4245
5b7c81bd 4246static inline bool
738e5348
RS
4247mips16_reloc_p (bfd_reloc_code_real_type reloc)
4248{
4249 switch (reloc)
4250 {
4251 case BFD_RELOC_MIPS16_JMP:
4252 case BFD_RELOC_MIPS16_GPREL:
4253 case BFD_RELOC_MIPS16_GOT16:
4254 case BFD_RELOC_MIPS16_CALL16:
4255 case BFD_RELOC_MIPS16_HI16_S:
4256 case BFD_RELOC_MIPS16_HI16:
4257 case BFD_RELOC_MIPS16_LO16:
c9775dde 4258 case BFD_RELOC_MIPS16_16_PCREL_S1:
5b7c81bd 4259 return true;
738e5348
RS
4260
4261 default:
5b7c81bd 4262 return false;
738e5348
RS
4263 }
4264}
4265
5b7c81bd 4266static inline bool
df58fc94
RS
4267micromips_reloc_p (bfd_reloc_code_real_type reloc)
4268{
4269 switch (reloc)
4270 {
4271 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4272 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4273 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4274 case BFD_RELOC_MICROMIPS_GPREL16:
4275 case BFD_RELOC_MICROMIPS_JMP:
4276 case BFD_RELOC_MICROMIPS_HI16:
4277 case BFD_RELOC_MICROMIPS_HI16_S:
4278 case BFD_RELOC_MICROMIPS_LO16:
4279 case BFD_RELOC_MICROMIPS_LITERAL:
4280 case BFD_RELOC_MICROMIPS_GOT16:
4281 case BFD_RELOC_MICROMIPS_CALL16:
4282 case BFD_RELOC_MICROMIPS_GOT_HI16:
4283 case BFD_RELOC_MICROMIPS_GOT_LO16:
4284 case BFD_RELOC_MICROMIPS_CALL_HI16:
4285 case BFD_RELOC_MICROMIPS_CALL_LO16:
4286 case BFD_RELOC_MICROMIPS_SUB:
4287 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4288 case BFD_RELOC_MICROMIPS_GOT_OFST:
4289 case BFD_RELOC_MICROMIPS_GOT_DISP:
4290 case BFD_RELOC_MICROMIPS_HIGHEST:
4291 case BFD_RELOC_MICROMIPS_HIGHER:
4292 case BFD_RELOC_MICROMIPS_SCN_DISP:
4293 case BFD_RELOC_MICROMIPS_JALR:
5b7c81bd 4294 return true;
df58fc94
RS
4295
4296 default:
5b7c81bd 4297 return false;
df58fc94
RS
4298 }
4299}
4300
5b7c81bd 4301static inline bool
2309ddf2
MR
4302jmp_reloc_p (bfd_reloc_code_real_type reloc)
4303{
4304 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4305}
4306
5b7c81bd 4307static inline bool
0e9c5a5c
MR
4308b_reloc_p (bfd_reloc_code_real_type reloc)
4309{
4310 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4311 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4312 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4313 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4314 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4315 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4316 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4317}
4318
5b7c81bd 4319static inline bool
738e5348
RS
4320got16_reloc_p (bfd_reloc_code_real_type reloc)
4321{
2309ddf2 4322 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4323 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4324}
4325
5b7c81bd 4326static inline bool
738e5348
RS
4327hi16_reloc_p (bfd_reloc_code_real_type reloc)
4328{
2309ddf2 4329 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4330 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4331}
4332
5b7c81bd 4333static inline bool
738e5348
RS
4334lo16_reloc_p (bfd_reloc_code_real_type reloc)
4335{
2309ddf2 4336 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4337 || reloc == BFD_RELOC_MICROMIPS_LO16);
4338}
4339
5b7c81bd 4340static inline bool
df58fc94
RS
4341jalr_reloc_p (bfd_reloc_code_real_type reloc)
4342{
2309ddf2 4343 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4344}
4345
5b7c81bd 4346static inline bool
f2ae14a1
RS
4347gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4348{
4349 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4350 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4351}
4352
2de39019
CM
4353/* Return true if RELOC is a PC-relative relocation that does not have
4354 full address range. */
4355
5b7c81bd 4356static inline bool
2de39019
CM
4357limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4358{
4359 switch (reloc)
4360 {
4361 case BFD_RELOC_16_PCREL_S2:
c9775dde 4362 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4363 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4364 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4365 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4366 case BFD_RELOC_MIPS_21_PCREL_S2:
4367 case BFD_RELOC_MIPS_26_PCREL_S2:
4368 case BFD_RELOC_MIPS_18_PCREL_S3:
4369 case BFD_RELOC_MIPS_19_PCREL_S2:
5b7c81bd 4370 return true;
2de39019 4371
b47468a6 4372 case BFD_RELOC_32_PCREL:
7361da2c
AB
4373 case BFD_RELOC_HI16_S_PCREL:
4374 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4375 return HAVE_64BIT_ADDRESSES;
4376
2de39019 4377 default:
5b7c81bd 4378 return false;
2de39019
CM
4379 }
4380}
b47468a6 4381
5919d012 4382/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4383 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4384 need a matching %lo() when applied to local symbols. */
5919d012 4385
5b7c81bd 4386static inline bool
17a2f251 4387reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4388{
3b91255e 4389 return (HAVE_IN_PLACE_ADDENDS
738e5348 4390 && (hi16_reloc_p (reloc)
0a44bf69
RS
4391 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4392 all GOT16 relocations evaluate to "G". */
738e5348
RS
4393 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4394}
4395
4396/* Return the type of %lo() reloc needed by RELOC, given that
4397 reloc_needs_lo_p. */
4398
4399static inline bfd_reloc_code_real_type
4400matching_lo_reloc (bfd_reloc_code_real_type reloc)
4401{
df58fc94
RS
4402 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4403 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4404 : BFD_RELOC_LO16));
5919d012
RS
4405}
4406
4407/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4408 relocation. */
4409
5b7c81bd 4410static inline bool
17a2f251 4411fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4412{
4413 return (fixp->fx_next != NULL
738e5348 4414 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4415 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4416 && fixp->fx_offset == fixp->fx_next->fx_offset);
4417}
4418
462427c4
RS
4419/* Move all labels in LABELS to the current insertion point. TEXT_P
4420 says whether the labels refer to text or data. */
404a8071
RS
4421
4422static void
5b7c81bd 4423mips_move_labels (struct insn_label_list *labels, bool text_p)
404a8071
RS
4424{
4425 struct insn_label_list *l;
4426 valueT val;
4427
462427c4 4428 for (l = labels; l != NULL; l = l->next)
404a8071 4429 {
9c2799c2 4430 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4431 symbol_set_frag (l->label, frag_now);
4432 val = (valueT) frag_now_fix ();
770c0151
FS
4433 /* MIPS16/microMIPS text labels are stored as odd.
4434 We just carry the ISA mode bit forward. */
462427c4 4435 if (text_p && HAVE_CODE_COMPRESSION)
770c0151 4436 val |= (S_GET_VALUE (l->label) & 0x1);
404a8071
RS
4437 S_SET_VALUE (l->label, val);
4438 }
4439}
4440
462427c4
RS
4441/* Move all labels in insn_labels to the current insertion point
4442 and treat them as text labels. */
4443
4444static void
4445mips_move_text_labels (void)
4446{
5b7c81bd 4447 mips_move_labels (seg_info (now_seg)->label_list, true);
462427c4
RS
4448}
4449
9e009953
MR
4450/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4451
5b7c81bd 4452static bool
5f0fe04b
TS
4453s_is_linkonce (symbolS *sym, segT from_seg)
4454{
5b7c81bd 4455 bool linkonce = false;
5f0fe04b
TS
4456 segT symseg = S_GET_SEGMENT (sym);
4457
4458 if (symseg != from_seg && !S_IS_LOCAL (sym))
4459 {
fd361982 4460 if ((bfd_section_flags (symseg) & SEC_LINK_ONCE))
5b7c81bd 4461 linkonce = true;
5f0fe04b
TS
4462 /* The GNU toolchain uses an extension for ELF: a section
4463 beginning with the magic string .gnu.linkonce is a
4464 linkonce section. */
d34049e8 4465 if (startswith (segment_name (symseg), ".gnu.linkonce"))
5b7c81bd 4466 linkonce = true;
5f0fe04b
TS
4467 }
4468 return linkonce;
4469}
4470
e1b47bd5 4471/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4472 linker to handle them specially, such as generating jalx instructions
4473 when needed. We also make them odd for the duration of the assembly,
4474 in order to generate the right sort of code. We will make them even
252b5132
RH
4475 in the adjust_symtab routine, while leaving them marked. This is
4476 convenient for the debugger and the disassembler. The linker knows
4477 to make them odd again. */
4478
4479static void
e1b47bd5 4480mips_compressed_mark_label (symbolS *label)
252b5132 4481{
df58fc94 4482 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4483
f3ded42a
RS
4484 if (mips_opts.mips16)
4485 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4486 else
4487 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4488 if ((S_GET_VALUE (label) & 1) == 0
4489 /* Don't adjust the address if the label is global or weak, or
4490 in a link-once section, since we'll be emitting symbol reloc
4491 references to it which will be patched up by the linker, and
4492 the final value of the symbol may or may not be MIPS16/microMIPS. */
4493 && !S_IS_WEAK (label)
4494 && !S_IS_EXTERNAL (label)
4495 && !s_is_linkonce (label, now_seg))
4496 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4497}
4498
4499/* Mark preceding MIPS16 or microMIPS instruction labels. */
4500
4501static void
4502mips_compressed_mark_labels (void)
4503{
4504 struct insn_label_list *l;
4505
4506 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4507 mips_compressed_mark_label (l->label);
252b5132
RH
4508}
4509
4d7206a2
RS
4510/* End the current frag. Make it a variant frag and record the
4511 relaxation info. */
4512
4513static void
4514relax_close_frag (void)
4515{
584892a6 4516 mips_macro_warning.first_frag = frag_now;
4d7206a2 4517 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4518 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4519 mips_pic != NO_PIC),
4d7206a2
RS
4520 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4521
4522 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4523 mips_relax.first_fixup = 0;
4524}
4525
4526/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4527 See the comment above RELAX_ENCODE for more details. */
4528
4529static void
4530relax_start (symbolS *symbol)
4531{
9c2799c2 4532 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4533 mips_relax.sequence = 1;
4534 mips_relax.symbol = symbol;
4535}
4536
4537/* Start generating the second version of a relaxable sequence.
4538 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4539
4540static void
4d7206a2
RS
4541relax_switch (void)
4542{
9c2799c2 4543 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4544 mips_relax.sequence = 2;
4545}
4546
4547/* End the current relaxable sequence. */
4548
4549static void
4550relax_end (void)
4551{
9c2799c2 4552 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4553 relax_close_frag ();
4554 mips_relax.sequence = 0;
4555}
4556
11625dd8
RS
4557/* Return true if IP is a delayed branch or jump. */
4558
5b7c81bd 4559static inline bool
11625dd8
RS
4560delayed_branch_p (const struct mips_cl_insn *ip)
4561{
4562 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4563 | INSN_COND_BRANCH_DELAY
4564 | INSN_COND_BRANCH_LIKELY)) != 0;
4565}
4566
4567/* Return true if IP is a compact branch or jump. */
4568
5b7c81bd 4569static inline bool
11625dd8
RS
4570compact_branch_p (const struct mips_cl_insn *ip)
4571{
26545944
RS
4572 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4573 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4574}
4575
4576/* Return true if IP is an unconditional branch or jump. */
4577
5b7c81bd 4578static inline bool
11625dd8
RS
4579uncond_branch_p (const struct mips_cl_insn *ip)
4580{
4581 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4582 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4583}
4584
4585/* Return true if IP is a branch-likely instruction. */
4586
5b7c81bd 4587static inline bool
11625dd8
RS
4588branch_likely_p (const struct mips_cl_insn *ip)
4589{
4590 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4591}
4592
14fe068b
RS
4593/* Return the type of nop that should be used to fill the delay slot
4594 of delayed branch IP. */
4595
4596static struct mips_cl_insn *
4597get_delay_slot_nop (const struct mips_cl_insn *ip)
4598{
4599 if (mips_opts.micromips
4600 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4601 return &micromips_nop32_insn;
4602 return NOP_INSN;
4603}
4604
fc76e730
RS
4605/* Return a mask that has bit N set if OPCODE reads the register(s)
4606 in operand N. */
df58fc94
RS
4607
4608static unsigned int
fc76e730 4609insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4610{
fc76e730
RS
4611 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4612}
df58fc94 4613
fc76e730
RS
4614/* Return a mask that has bit N set if OPCODE writes to the register(s)
4615 in operand N. */
4616
4617static unsigned int
4618insn_write_mask (const struct mips_opcode *opcode)
4619{
4620 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4621}
4622
4623/* Return a mask of the registers specified by operand OPERAND of INSN.
4624 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4625 is set. */
4626
4627static unsigned int
4628operand_reg_mask (const struct mips_cl_insn *insn,
4629 const struct mips_operand *operand,
4630 unsigned int type_mask)
4631{
4632 unsigned int uval, vsel;
4633
4634 switch (operand->type)
df58fc94 4635 {
fc76e730
RS
4636 case OP_INT:
4637 case OP_MAPPED_INT:
4638 case OP_MSB:
4639 case OP_PCREL:
4640 case OP_PERF_REG:
4641 case OP_ADDIUSP_INT:
4642 case OP_ENTRY_EXIT_LIST:
4643 case OP_REPEAT_DEST_REG:
4644 case OP_REPEAT_PREV_REG:
4645 case OP_PC:
14daeee3
RS
4646 case OP_VU0_SUFFIX:
4647 case OP_VU0_MATCH_SUFFIX:
56d438b1 4648 case OP_IMM_INDEX:
fc76e730
RS
4649 abort ();
4650
25499ac7
MR
4651 case OP_REG28:
4652 return 1 << 28;
4653
fc76e730 4654 case OP_REG:
0f35dbc4 4655 case OP_OPTIONAL_REG:
fc76e730
RS
4656 {
4657 const struct mips_reg_operand *reg_op;
4658
4659 reg_op = (const struct mips_reg_operand *) operand;
4660 if (!(type_mask & (1 << reg_op->reg_type)))
4661 return 0;
4662 uval = insn_extract_operand (insn, operand);
e0fd91ef 4663 return 1u << mips_decode_reg_operand (reg_op, uval);
fc76e730
RS
4664 }
4665
4666 case OP_REG_PAIR:
4667 {
4668 const struct mips_reg_pair_operand *pair_op;
4669
4670 pair_op = (const struct mips_reg_pair_operand *) operand;
4671 if (!(type_mask & (1 << pair_op->reg_type)))
4672 return 0;
4673 uval = insn_extract_operand (insn, operand);
e0fd91ef 4674 return (1u << pair_op->reg1_map[uval]) | (1u << pair_op->reg2_map[uval]);
fc76e730
RS
4675 }
4676
4677 case OP_CLO_CLZ_DEST:
4678 if (!(type_mask & (1 << OP_REG_GP)))
4679 return 0;
4680 uval = insn_extract_operand (insn, operand);
e0fd91ef 4681 return (1u << (uval & 31)) | (1u << (uval >> 5));
fc76e730 4682
7361da2c
AB
4683 case OP_SAME_RS_RT:
4684 if (!(type_mask & (1 << OP_REG_GP)))
4685 return 0;
4686 uval = insn_extract_operand (insn, operand);
4687 gas_assert ((uval & 31) == (uval >> 5));
e0fd91ef 4688 return 1u << (uval & 31);
7361da2c
AB
4689
4690 case OP_CHECK_PREV:
4691 case OP_NON_ZERO_REG:
4692 if (!(type_mask & (1 << OP_REG_GP)))
4693 return 0;
4694 uval = insn_extract_operand (insn, operand);
e0fd91ef 4695 return 1u << (uval & 31);
7361da2c 4696
fc76e730
RS
4697 case OP_LWM_SWM_LIST:
4698 abort ();
4699
4700 case OP_SAVE_RESTORE_LIST:
4701 abort ();
4702
4703 case OP_MDMX_IMM_REG:
4704 if (!(type_mask & (1 << OP_REG_VEC)))
4705 return 0;
4706 uval = insn_extract_operand (insn, operand);
4707 vsel = uval >> 5;
4708 if ((vsel & 0x18) == 0x18)
4709 return 0;
e0fd91ef 4710 return 1u << (uval & 31);
56d438b1
CF
4711
4712 case OP_REG_INDEX:
4713 if (!(type_mask & (1 << OP_REG_GP)))
4714 return 0;
e0fd91ef 4715 return 1u << insn_extract_operand (insn, operand);
df58fc94 4716 }
fc76e730
RS
4717 abort ();
4718}
4719
4720/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4721 where bit N of OPNO_MASK is set if operand N should be included.
4722 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4723 is set. */
4724
4725static unsigned int
4726insn_reg_mask (const struct mips_cl_insn *insn,
4727 unsigned int type_mask, unsigned int opno_mask)
4728{
4729 unsigned int opno, reg_mask;
4730
4731 opno = 0;
4732 reg_mask = 0;
4733 while (opno_mask != 0)
4734 {
4735 if (opno_mask & 1)
4736 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4737 opno_mask >>= 1;
4738 opno += 1;
4739 }
4740 return reg_mask;
df58fc94
RS
4741}
4742
4c260379
RS
4743/* Return the mask of core registers that IP reads. */
4744
4745static unsigned int
4746gpr_read_mask (const struct mips_cl_insn *ip)
4747{
4748 unsigned long pinfo, pinfo2;
4749 unsigned int mask;
4750
fc76e730 4751 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4752 pinfo = ip->insn_mo->pinfo;
4753 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4754 if (pinfo & INSN_UDI)
4c260379 4755 {
fc76e730
RS
4756 /* UDI instructions have traditionally been assumed to read RS
4757 and RT. */
4758 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4759 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4760 }
fc76e730
RS
4761 if (pinfo & INSN_READ_GPR_24)
4762 mask |= 1 << 24;
4763 if (pinfo2 & INSN2_READ_GPR_16)
4764 mask |= 1 << 16;
4765 if (pinfo2 & INSN2_READ_SP)
4766 mask |= 1 << SP;
26545944 4767 if (pinfo2 & INSN2_READ_GPR_31)
a6a1f5e0 4768 mask |= 1u << 31;
fe35f09f
RS
4769 /* Don't include register 0. */
4770 return mask & ~1;
4c260379
RS
4771}
4772
4773/* Return the mask of core registers that IP writes. */
4774
4775static unsigned int
4776gpr_write_mask (const struct mips_cl_insn *ip)
4777{
4778 unsigned long pinfo, pinfo2;
4779 unsigned int mask;
4780
fc76e730 4781 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4782 pinfo = ip->insn_mo->pinfo;
4783 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4784 if (pinfo & INSN_WRITE_GPR_24)
4785 mask |= 1 << 24;
4786 if (pinfo & INSN_WRITE_GPR_31)
a6a1f5e0 4787 mask |= 1u << 31;
fc76e730
RS
4788 if (pinfo & INSN_UDI)
4789 /* UDI instructions have traditionally been assumed to write to RD. */
4790 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4791 if (pinfo2 & INSN2_WRITE_SP)
4792 mask |= 1 << SP;
fe35f09f
RS
4793 /* Don't include register 0. */
4794 return mask & ~1;
4c260379
RS
4795}
4796
4797/* Return the mask of floating-point registers that IP reads. */
4798
4799static unsigned int
4800fpr_read_mask (const struct mips_cl_insn *ip)
4801{
fc76e730 4802 unsigned long pinfo;
4c260379
RS
4803 unsigned int mask;
4804
9d5de888
CF
4805 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4806 | (1 << OP_REG_MSA)),
fc76e730 4807 insn_read_mask (ip->insn_mo));
4c260379 4808 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4809 /* Conservatively treat all operands to an FP_D instruction are doubles.
4810 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4811 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4812 mask |= mask << 1;
4813 return mask;
4814}
4815
4816/* Return the mask of floating-point registers that IP writes. */
4817
4818static unsigned int
4819fpr_write_mask (const struct mips_cl_insn *ip)
4820{
fc76e730 4821 unsigned long pinfo;
4c260379
RS
4822 unsigned int mask;
4823
9d5de888
CF
4824 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4825 | (1 << OP_REG_MSA)),
fc76e730 4826 insn_write_mask (ip->insn_mo));
4c260379 4827 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4828 /* Conservatively treat all operands to an FP_D instruction are doubles.
4829 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4830 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4831 mask |= mask << 1;
4832 return mask;
4833}
4834
a1d78564
RS
4835/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4836 Check whether that is allowed. */
4837
5b7c81bd 4838static bool
a1d78564
RS
4839mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4840{
4841 const char *s = insn->name;
5b7c81bd
AM
4842 bool oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4843 || FPR_SIZE == 64) && mips_opts.oddspreg;
a1d78564
RS
4844
4845 if (insn->pinfo == INSN_MACRO)
4846 /* Let a macro pass, we'll catch it later when it is expanded. */
5b7c81bd 4847 return true;
a1d78564 4848
351cdf24
MF
4849 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4850 otherwise it depends on oddspreg. */
4851 if ((insn->pinfo & FP_S)
4852 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4853 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4854 return FPR_SIZE == 32 || oddspreg;
a1d78564 4855
351cdf24
MF
4856 /* Allow odd registers for single-precision ops and double-precision if the
4857 floating-point registers are 64-bit wide. */
4858 switch (insn->pinfo & (FP_S | FP_D))
4859 {
4860 case FP_S:
4861 case 0:
4862 return oddspreg;
4863 case FP_D:
4864 return FPR_SIZE == 64;
4865 default:
4866 break;
a1d78564
RS
4867 }
4868
351cdf24
MF
4869 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4870 s = strchr (insn->name, '.');
4871 if (s != NULL && opnum == 2)
4872 s = strchr (s + 1, '.');
4873 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4874 return oddspreg;
a1d78564 4875
351cdf24 4876 return FPR_SIZE == 64;
a1d78564
RS
4877}
4878
a1d78564
RS
4879/* Information about an instruction argument that we're trying to match. */
4880struct mips_arg_info
4881{
4882 /* The instruction so far. */
4883 struct mips_cl_insn *insn;
4884
a92713e6
RS
4885 /* The first unconsumed operand token. */
4886 struct mips_operand_token *token;
4887
a1d78564
RS
4888 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4889 int opnum;
4890
4891 /* The 1-based argument number, for error reporting. This does not
4892 count elided optional registers, etc.. */
4893 int argnum;
4894
4895 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4896 unsigned int last_regno;
4897
4898 /* If the first operand was an OP_REG, this is the register that it
4899 specified, otherwise it is ILLEGAL_REG. */
4900 unsigned int dest_regno;
4901
4902 /* The value of the last OP_INT operand. Only used for OP_MSB,
4903 where it gives the lsb position. */
4904 unsigned int last_op_int;
4905
60f20e8b 4906 /* If true, match routines should assume that no later instruction
2b0f3761 4907 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4908 possible. Match routines should not report errors if something
4909 is only invalid for !LAX_MATCH. */
5b7c81bd 4910 bool lax_match;
a1d78564 4911
a1d78564 4912 /* True if a reference to the current AT register was seen. */
5b7c81bd 4913 bool seen_at;
a1d78564
RS
4914};
4915
1a00e612
RS
4916/* Record that the argument is out of range. */
4917
4918static void
4919match_out_of_range (struct mips_arg_info *arg)
4920{
4921 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4922}
4923
4924/* Record that the argument isn't constant but needs to be. */
4925
4926static void
4927match_not_constant (struct mips_arg_info *arg)
4928{
4929 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4930 arg->argnum);
4931}
4932
a92713e6
RS
4933/* Try to match an OT_CHAR token for character CH. Consume the token
4934 and return true on success, otherwise return false. */
a1d78564 4935
5b7c81bd 4936static bool
a92713e6 4937match_char (struct mips_arg_info *arg, char ch)
a1d78564 4938{
a92713e6
RS
4939 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4940 {
4941 ++arg->token;
4942 if (ch == ',')
4943 arg->argnum += 1;
5b7c81bd 4944 return true;
a92713e6 4945 }
5b7c81bd 4946 return false;
a92713e6 4947}
a1d78564 4948
a92713e6
RS
4949/* Try to get an expression from the next tokens in ARG. Consume the
4950 tokens and return true on success, storing the expression value in
4951 VALUE and relocation types in R. */
4952
5b7c81bd 4953static bool
a92713e6
RS
4954match_expression (struct mips_arg_info *arg, expressionS *value,
4955 bfd_reloc_code_real_type *r)
4956{
d436c1c2
RS
4957 /* If the next token is a '(' that was parsed as being part of a base
4958 expression, assume we have an elided offset. The later match will fail
4959 if this turns out to be wrong. */
4960 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4961 {
d436c1c2
RS
4962 value->X_op = O_constant;
4963 value->X_add_number = 0;
4964 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
5b7c81bd 4965 return true;
a92713e6
RS
4966 }
4967
d436c1c2
RS
4968 /* Reject register-based expressions such as "0+$2" and "(($2))".
4969 For plain registers the default error seems more appropriate. */
4970 if (arg->token->type == OT_INTEGER
4971 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4972 {
d436c1c2 4973 set_insn_error (arg->argnum, _("register value used as expression"));
5b7c81bd 4974 return false;
a1d78564 4975 }
d436c1c2
RS
4976
4977 if (arg->token->type == OT_INTEGER)
a92713e6 4978 {
d436c1c2
RS
4979 *value = arg->token->u.integer.value;
4980 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4981 ++arg->token;
5b7c81bd 4982 return true;
a92713e6 4983 }
a92713e6 4984
d436c1c2
RS
4985 set_insn_error_i
4986 (arg->argnum, _("operand %d must be an immediate expression"),
4987 arg->argnum);
5b7c81bd 4988 return false;
a92713e6
RS
4989}
4990
4991/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4992 the tokens and return true on success, storing the constant value
a54d5f8b 4993 in *VALUE. */
a92713e6 4994
5b7c81bd 4995static bool
1a00e612 4996match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4997{
4998 expressionS ex;
4999 bfd_reloc_code_real_type r[3];
a1d78564 5000
a92713e6 5001 if (!match_expression (arg, &ex, r))
5b7c81bd 5002 return false;
a92713e6
RS
5003
5004 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
5005 *value = ex.X_add_number;
5006 else
5007 {
c96425c5
MR
5008 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
5009 match_out_of_range (arg);
5010 else
5011 match_not_constant (arg);
5b7c81bd 5012 return false;
a1d78564 5013 }
5b7c81bd 5014 return true;
a1d78564
RS
5015}
5016
5017/* Return the RTYPE_* flags for a register operand of type TYPE that
5018 appears in instruction OPCODE. */
5019
5020static unsigned int
5021convert_reg_type (const struct mips_opcode *opcode,
5022 enum mips_reg_operand_type type)
5023{
5024 switch (type)
5025 {
5026 case OP_REG_GP:
5027 return RTYPE_NUM | RTYPE_GP;
5028
5029 case OP_REG_FP:
5030 /* Allow vector register names for MDMX if the instruction is a 64-bit
5031 FPR load, store or move (including moves to and from GPRs). */
5032 if ((mips_opts.ase & ASE_MDMX)
5033 && (opcode->pinfo & FP_D)
43885403 5034 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 5035 | INSN_COPROC_MEMORY_DELAY
43885403 5036 | INSN_LOAD_COPROC
67dc82bc 5037 | INSN_LOAD_MEMORY
a1d78564
RS
5038 | INSN_STORE_MEMORY)))
5039 return RTYPE_FPU | RTYPE_VEC;
5040 return RTYPE_FPU;
5041
5042 case OP_REG_CCC:
5043 if (opcode->pinfo & (FP_D | FP_S))
5044 return RTYPE_CCC | RTYPE_FCC;
5045 return RTYPE_CCC;
5046
5047 case OP_REG_VEC:
5048 if (opcode->membership & INSN_5400)
5049 return RTYPE_FPU;
5050 return RTYPE_FPU | RTYPE_VEC;
5051
5052 case OP_REG_ACC:
5053 return RTYPE_ACC;
5054
5055 case OP_REG_COPRO:
9204ccd4 5056 case OP_REG_CONTROL:
a1d78564
RS
5057 if (opcode->name[strlen (opcode->name) - 1] == '0')
5058 return RTYPE_NUM | RTYPE_CP0;
5059 return RTYPE_NUM;
5060
5061 case OP_REG_HW:
5062 return RTYPE_NUM;
14daeee3
RS
5063
5064 case OP_REG_VI:
5065 return RTYPE_NUM | RTYPE_VI;
5066
5067 case OP_REG_VF:
5068 return RTYPE_NUM | RTYPE_VF;
5069
5070 case OP_REG_R5900_I:
5071 return RTYPE_R5900_I;
5072
5073 case OP_REG_R5900_Q:
5074 return RTYPE_R5900_Q;
5075
5076 case OP_REG_R5900_R:
5077 return RTYPE_R5900_R;
5078
5079 case OP_REG_R5900_ACC:
5080 return RTYPE_R5900_ACC;
56d438b1
CF
5081
5082 case OP_REG_MSA:
5083 return RTYPE_MSA;
5084
5085 case OP_REG_MSA_CTRL:
5086 return RTYPE_NUM;
a1d78564
RS
5087 }
5088 abort ();
5089}
5090
5091/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5092
5093static void
5094check_regno (struct mips_arg_info *arg,
5095 enum mips_reg_operand_type type, unsigned int regno)
5096{
5097 if (AT && type == OP_REG_GP && regno == AT)
5b7c81bd 5098 arg->seen_at = true;
a1d78564
RS
5099
5100 if (type == OP_REG_FP
5101 && (regno & 1) != 0
a1d78564 5102 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5103 {
5104 /* This was a warning prior to introducing O32 FPXX and FP64 support
5105 so maintain a warning for FP32 but raise an error for the new
5106 cases. */
5107 if (FPR_SIZE == 32)
5108 as_warn (_("float register should be even, was %d"), regno);
5109 else
5110 as_bad (_("float register should be even, was %d"), regno);
5111 }
a1d78564
RS
5112
5113 if (type == OP_REG_CCC)
5114 {
5115 const char *name;
5116 size_t length;
5117
5118 name = arg->insn->insn_mo->name;
5119 length = strlen (name);
5120 if ((regno & 1) != 0
5121 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
d34049e8 5122 || (length >= 5 && startswith (name + length - 5, "any2"))))
1661c76c 5123 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5124 name, regno);
5125
5126 if ((regno & 3) != 0
d34049e8 5127 && (length >= 5 && startswith (name + length - 5, "any4")))
1661c76c 5128 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5129 name, regno);
5130 }
5131}
5132
a92713e6
RS
5133/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5134 a register of type TYPE. Return true on success, storing the register
5135 number in *REGNO and warning about any dubious uses. */
5136
5b7c81bd 5137static bool
a92713e6
RS
5138match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5139 unsigned int symval, unsigned int *regno)
5140{
5141 if (type == OP_REG_VEC)
5142 symval = mips_prefer_vec_regno (symval);
5143 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5b7c81bd 5144 return false;
a92713e6
RS
5145
5146 *regno = symval & RNUM_MASK;
5147 check_regno (arg, type, *regno);
5b7c81bd 5148 return true;
a92713e6
RS
5149}
5150
5151/* Try to interpret the next token in ARG as a register of type TYPE.
5152 Consume the token and return true on success, storing the register
5153 number in *REGNO. Return false on failure. */
5154
5b7c81bd 5155static bool
a92713e6
RS
5156match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5157 unsigned int *regno)
5158{
5159 if (arg->token->type == OT_REG
5160 && match_regno (arg, type, arg->token->u.regno, regno))
5161 {
5162 ++arg->token;
5b7c81bd 5163 return true;
a92713e6 5164 }
5b7c81bd 5165 return false;
a92713e6
RS
5166}
5167
5168/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5169 Consume the token and return true on success, storing the register numbers
5170 in *REGNO1 and *REGNO2. Return false on failure. */
5171
5b7c81bd 5172static bool
a92713e6
RS
5173match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5174 unsigned int *regno1, unsigned int *regno2)
5175{
5176 if (match_reg (arg, type, regno1))
5177 {
5178 *regno2 = *regno1;
5b7c81bd 5179 return true;
a92713e6
RS
5180 }
5181 if (arg->token->type == OT_REG_RANGE
5182 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5183 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5184 && *regno1 <= *regno2)
5185 {
5186 ++arg->token;
5b7c81bd 5187 return true;
a92713e6 5188 }
5b7c81bd 5189 return false;
a92713e6
RS
5190}
5191
a1d78564
RS
5192/* OP_INT matcher. */
5193
5b7c81bd 5194static bool
a1d78564 5195match_int_operand (struct mips_arg_info *arg,
a92713e6 5196 const struct mips_operand *operand_base)
a1d78564
RS
5197{
5198 const struct mips_int_operand *operand;
3ccad066 5199 unsigned int uval;
a1d78564
RS
5200 int min_val, max_val, factor;
5201 offsetT sval;
a1d78564
RS
5202
5203 operand = (const struct mips_int_operand *) operand_base;
5204 factor = 1 << operand->shift;
3ccad066
RS
5205 min_val = mips_int_operand_min (operand);
5206 max_val = mips_int_operand_max (operand);
a1d78564 5207
d436c1c2
RS
5208 if (operand_base->lsb == 0
5209 && operand_base->size == 16
5210 && operand->shift == 0
5211 && operand->bias == 0
5212 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5213 {
5214 /* The operand can be relocated. */
a92713e6 5215 if (!match_expression (arg, &offset_expr, offset_reloc))
5b7c81bd 5216 return false;
a92713e6 5217
c96425c5
MR
5218 if (offset_expr.X_op == O_big)
5219 {
5220 match_out_of_range (arg);
5b7c81bd 5221 return false;
c96425c5
MR
5222 }
5223
a92713e6 5224 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5225 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5226 leave the relocation value in offset_expr and offset_relocs
5227 for the caller to process. */
5b7c81bd 5228 return true;
a92713e6
RS
5229
5230 if (offset_expr.X_op != O_constant)
a1d78564 5231 {
60f20e8b
RS
5232 /* Accept non-constant operands if no later alternative matches,
5233 leaving it for the caller to process. */
5234 if (!arg->lax_match)
602b88e3
MR
5235 {
5236 match_not_constant (arg);
5b7c81bd 5237 return false;
602b88e3 5238 }
a92713e6 5239 offset_reloc[0] = BFD_RELOC_LO16;
5b7c81bd 5240 return true;
a1d78564 5241 }
a92713e6 5242
a1d78564
RS
5243 /* Clear the global state; we're going to install the operand
5244 ourselves. */
a92713e6 5245 sval = offset_expr.X_add_number;
a1d78564 5246 offset_expr.X_op = O_absent;
60f20e8b
RS
5247
5248 /* For compatibility with older assemblers, we accept
5249 0x8000-0xffff as signed 16-bit numbers when only
5250 signed numbers are allowed. */
5251 if (sval > max_val)
5252 {
5253 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5254 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5255 {
5256 match_out_of_range (arg);
5b7c81bd 5257 return false;
20c59b84 5258 }
60f20e8b 5259 }
a1d78564
RS
5260 }
5261 else
5262 {
1a00e612 5263 if (!match_const_int (arg, &sval))
5b7c81bd 5264 return false;
a1d78564
RS
5265 }
5266
5267 arg->last_op_int = sval;
5268
1a00e612 5269 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5270 {
1a00e612 5271 match_out_of_range (arg);
5b7c81bd 5272 return false;
a1d78564
RS
5273 }
5274
5275 uval = (unsigned int) sval >> operand->shift;
5276 uval -= operand->bias;
5277
5278 /* Handle -mfix-cn63xxp1. */
5279 if (arg->opnum == 1
5280 && mips_fix_cn63xxp1
5281 && !mips_opts.micromips
5282 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5283 switch (uval)
5284 {
5285 case 5:
5286 case 25:
5287 case 26:
5288 case 27:
5289 case 28:
5290 case 29:
5291 case 30:
5292 case 31:
5293 /* These are ok. */
5294 break;
5295
5296 default:
5297 /* The rest must be changed to 28. */
5298 uval = 28;
5299 break;
5300 }
5301
5302 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5303 return true;
a1d78564
RS
5304}
5305
5306/* OP_MAPPED_INT matcher. */
5307
5b7c81bd 5308static bool
a1d78564 5309match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5310 const struct mips_operand *operand_base)
a1d78564
RS
5311{
5312 const struct mips_mapped_int_operand *operand;
5313 unsigned int uval, num_vals;
5314 offsetT sval;
5315
5316 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5317 if (!match_const_int (arg, &sval))
5b7c81bd 5318 return false;
a1d78564
RS
5319
5320 num_vals = 1 << operand_base->size;
5321 for (uval = 0; uval < num_vals; uval++)
5322 if (operand->int_map[uval] == sval)
5323 break;
5324 if (uval == num_vals)
1a00e612
RS
5325 {
5326 match_out_of_range (arg);
5b7c81bd 5327 return false;
1a00e612 5328 }
a1d78564
RS
5329
5330 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5331 return true;
a1d78564
RS
5332}
5333
5334/* OP_MSB matcher. */
5335
5b7c81bd 5336static bool
a1d78564 5337match_msb_operand (struct mips_arg_info *arg,
a92713e6 5338 const struct mips_operand *operand_base)
a1d78564
RS
5339{
5340 const struct mips_msb_operand *operand;
5341 int min_val, max_val, max_high;
5342 offsetT size, sval, high;
5343
5344 operand = (const struct mips_msb_operand *) operand_base;
5345 min_val = operand->bias;
5346 max_val = min_val + (1 << operand_base->size) - 1;
5347 max_high = operand->opsize;
5348
1a00e612 5349 if (!match_const_int (arg, &size))
5b7c81bd 5350 return false;
a1d78564
RS
5351
5352 high = size + arg->last_op_int;
5353 sval = operand->add_lsb ? high : size;
5354
5355 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5356 {
1a00e612 5357 match_out_of_range (arg);
5b7c81bd 5358 return false;
a1d78564
RS
5359 }
5360 insn_insert_operand (arg->insn, operand_base, sval - min_val);
5b7c81bd 5361 return true;
a1d78564
RS
5362}
5363
5364/* OP_REG matcher. */
5365
5b7c81bd 5366static bool
a1d78564 5367match_reg_operand (struct mips_arg_info *arg,
a92713e6 5368 const struct mips_operand *operand_base)
a1d78564
RS
5369{
5370 const struct mips_reg_operand *operand;
a92713e6 5371 unsigned int regno, uval, num_vals;
a1d78564
RS
5372
5373 operand = (const struct mips_reg_operand *) operand_base;
a92713e6 5374 if (!match_reg (arg, operand->reg_type, &regno))
5b7c81bd 5375 return false;
a1d78564
RS
5376
5377 if (operand->reg_map)
5378 {
5379 num_vals = 1 << operand->root.size;
5380 for (uval = 0; uval < num_vals; uval++)
5381 if (operand->reg_map[uval] == regno)
5382 break;
5383 if (num_vals == uval)
5b7c81bd 5384 return false;
a1d78564
RS
5385 }
5386 else
5387 uval = regno;
5388
a1d78564
RS
5389 arg->last_regno = regno;
5390 if (arg->opnum == 1)
5391 arg->dest_regno = regno;
5392 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5393 return true;
a1d78564
RS
5394}
5395
5396/* OP_REG_PAIR matcher. */
5397
5b7c81bd 5398static bool
a1d78564 5399match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5400 const struct mips_operand *operand_base)
a1d78564
RS
5401{
5402 const struct mips_reg_pair_operand *operand;
a92713e6 5403 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5404
5405 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5406 if (!match_reg (arg, operand->reg_type, &regno1)
5407 || !match_char (arg, ',')
5408 || !match_reg (arg, operand->reg_type, &regno2))
5b7c81bd 5409 return false;
a1d78564
RS
5410
5411 num_vals = 1 << operand_base->size;
5412 for (uval = 0; uval < num_vals; uval++)
5413 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5414 break;
5415 if (uval == num_vals)
5b7c81bd 5416 return false;
a1d78564 5417
a1d78564 5418 insn_insert_operand (arg->insn, operand_base, uval);
5b7c81bd 5419 return true;
a1d78564
RS
5420}
5421
5422/* OP_PCREL matcher. The caller chooses the relocation type. */
5423
5b7c81bd 5424static bool
a92713e6 5425match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5426{
a92713e6
RS
5427 bfd_reloc_code_real_type r[3];
5428
5429 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5430}
5431
5432/* OP_PERF_REG matcher. */
5433
5b7c81bd 5434static bool
a1d78564 5435match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5436 const struct mips_operand *operand)
a1d78564
RS
5437{
5438 offsetT sval;
5439
1a00e612 5440 if (!match_const_int (arg, &sval))
5b7c81bd 5441 return false;
a1d78564
RS
5442
5443 if (sval != 0
5444 && (sval != 1
5445 || (mips_opts.arch == CPU_R5900
5446 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5447 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5448 {
1a00e612 5449 set_insn_error (arg->argnum, _("invalid performance register"));
5b7c81bd 5450 return false;
a1d78564
RS
5451 }
5452
5453 insn_insert_operand (arg->insn, operand, sval);
5b7c81bd 5454 return true;
a1d78564
RS
5455}
5456
5457/* OP_ADDIUSP matcher. */
5458
5b7c81bd 5459static bool
a1d78564 5460match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5461 const struct mips_operand *operand)
a1d78564
RS
5462{
5463 offsetT sval;
5464 unsigned int uval;
5465
1a00e612 5466 if (!match_const_int (arg, &sval))
5b7c81bd 5467 return false;
a1d78564
RS
5468
5469 if (sval % 4)
1a00e612
RS
5470 {
5471 match_out_of_range (arg);
5b7c81bd 5472 return false;
1a00e612 5473 }
a1d78564
RS
5474
5475 sval /= 4;
5476 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5477 {
5478 match_out_of_range (arg);
5b7c81bd 5479 return false;
1a00e612 5480 }
a1d78564
RS
5481
5482 uval = (unsigned int) sval;
5483 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5484 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 5485 return true;
a1d78564
RS
5486}
5487
5488/* OP_CLO_CLZ_DEST matcher. */
5489
5b7c81bd 5490static bool
a1d78564 5491match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5492 const struct mips_operand *operand)
a1d78564
RS
5493{
5494 unsigned int regno;
5495
a92713e6 5496 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5497 return false;
a1d78564 5498
a1d78564 5499 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5b7c81bd 5500 return true;
a1d78564
RS
5501}
5502
7361da2c
AB
5503/* OP_CHECK_PREV matcher. */
5504
5b7c81bd 5505static bool
7361da2c
AB
5506match_check_prev_operand (struct mips_arg_info *arg,
5507 const struct mips_operand *operand_base)
5508{
5509 const struct mips_check_prev_operand *operand;
5510 unsigned int regno;
5511
5512 operand = (const struct mips_check_prev_operand *) operand_base;
5513
5514 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5515 return false;
7361da2c
AB
5516
5517 if (!operand->zero_ok && regno == 0)
5b7c81bd 5518 return false;
7361da2c
AB
5519
5520 if ((operand->less_than_ok && regno < arg->last_regno)
5521 || (operand->greater_than_ok && regno > arg->last_regno)
5522 || (operand->equal_ok && regno == arg->last_regno))
5523 {
5524 arg->last_regno = regno;
5525 insn_insert_operand (arg->insn, operand_base, regno);
5b7c81bd 5526 return true;
7361da2c
AB
5527 }
5528
5b7c81bd 5529 return false;
7361da2c
AB
5530}
5531
5532/* OP_SAME_RS_RT matcher. */
5533
5b7c81bd 5534static bool
7361da2c
AB
5535match_same_rs_rt_operand (struct mips_arg_info *arg,
5536 const struct mips_operand *operand)
5537{
5538 unsigned int regno;
5539
5540 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 5541 return false;
7361da2c
AB
5542
5543 if (regno == 0)
5544 {
5545 set_insn_error (arg->argnum, _("the source register must not be $0"));
5b7c81bd 5546 return false;
7361da2c
AB
5547 }
5548
5549 arg->last_regno = regno;
5550
5551 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5b7c81bd 5552 return true;
7361da2c
AB
5553}
5554
a1d78564
RS
5555/* OP_LWM_SWM_LIST matcher. */
5556
5b7c81bd 5557static bool
a1d78564 5558match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5559 const struct mips_operand *operand)
a1d78564 5560{
a92713e6
RS
5561 unsigned int reglist, sregs, ra, regno1, regno2;
5562 struct mips_arg_info reset;
a1d78564 5563
a92713e6
RS
5564 reglist = 0;
5565 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5566 return false;
a92713e6
RS
5567 do
5568 {
5569 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5570 {
5571 reglist |= 1 << FP;
5572 regno2 = S7;
5573 }
5574 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5575 reset = *arg;
5576 }
5577 while (match_char (arg, ',')
5578 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5579 *arg = reset;
a1d78564
RS
5580
5581 if (operand->size == 2)
5582 {
5583 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5584
5585 s0, ra
5586 s0, s1, ra, s2, s3
5587 s0-s2, ra
5588
5589 and any permutations of these. */
5590 if ((reglist & 0xfff1ffff) != 0x80010000)
5b7c81bd 5591 return false;
a1d78564
RS
5592
5593 sregs = (reglist >> 17) & 7;
5594 ra = 0;
5595 }
5596 else
5597 {
5598 /* The list must include at least one of ra and s0-sN,
5599 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5600 which are $23 and $30 respectively.) E.g.:
5601
5602 ra
5603 s0
5604 ra, s0, s1, s2
5605 s0-s8
5606 s0-s5, ra
5607
5608 and any permutations of these. */
5609 if ((reglist & 0x3f00ffff) != 0)
5b7c81bd 5610 return false;
a1d78564
RS
5611
5612 ra = (reglist >> 27) & 0x10;
5613 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5614 }
5615 sregs += 1;
5616 if ((sregs & -sregs) != sregs)
5b7c81bd 5617 return false;
a1d78564
RS
5618
5619 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
5b7c81bd 5620 return true;
a1d78564
RS
5621}
5622
364215c8
RS
5623/* OP_ENTRY_EXIT_LIST matcher. */
5624
a92713e6 5625static unsigned int
364215c8 5626match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5627 const struct mips_operand *operand)
364215c8
RS
5628{
5629 unsigned int mask;
5b7c81bd 5630 bool is_exit;
364215c8
RS
5631
5632 /* The format is the same for both ENTRY and EXIT, but the constraints
5633 are different. */
5634 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5635 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5636 do
364215c8
RS
5637 {
5638 unsigned int regno1, regno2;
5b7c81bd 5639 bool is_freg;
364215c8 5640
a92713e6 5641 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5642 is_freg = false;
a92713e6 5643 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
5b7c81bd 5644 is_freg = true;
364215c8 5645 else
5b7c81bd 5646 return false;
364215c8
RS
5647
5648 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5649 {
5650 mask &= ~(7 << 3);
5651 mask |= (5 + regno2) << 3;
5652 }
5653 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5654 mask |= (regno2 - 3) << 3;
5655 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5656 mask |= (regno2 - 15) << 1;
5657 else if (regno1 == RA && regno2 == RA)
5658 mask |= 1;
5659 else
5b7c81bd 5660 return false;
364215c8 5661 }
a92713e6
RS
5662 while (match_char (arg, ','));
5663
364215c8 5664 insn_insert_operand (arg->insn, operand, mask);
5b7c81bd 5665 return true;
364215c8
RS
5666}
5667
38bf472a
MR
5668/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5669 the argument register mask AMASK, the number of static registers
5670 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5671 respectively, and the frame size FRAME_SIZE. */
5672
5673static unsigned int
5674mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5675 unsigned int ra, unsigned int s0, unsigned int s1,
5676 unsigned int frame_size)
5677{
5678 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5679 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5680}
5681
5682/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5683 argument register mask AMASK, the number of static registers saved
5684 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5685 respectively, and the frame size FRAME_SIZE. */
5686
5687static unsigned int
5688mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5689 unsigned int ra, unsigned int s0, unsigned int s1,
5690 unsigned int frame_size)
5691{
5692 unsigned int args;
5693
5694 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5695 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5696 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5697 | ((frame_size & 0xf0) << 16));
5698 return args;
5699}
5700
364215c8
RS
5701/* OP_SAVE_RESTORE_LIST matcher. */
5702
5b7c81bd 5703static bool
a92713e6 5704match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5705{
5706 unsigned int opcode, args, statics, sregs;
5707 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5708 unsigned int arg_mask, ra, s0, s1;
364215c8 5709 offsetT frame_size;
364215c8 5710
364215c8
RS
5711 opcode = arg->insn->insn_opcode;
5712 frame_size = 0;
5713 num_frame_sizes = 0;
5714 args = 0;
5715 statics = 0;
5716 sregs = 0;
38bf472a
MR
5717 ra = 0;
5718 s0 = 0;
5719 s1 = 0;
a92713e6 5720 do
364215c8
RS
5721 {
5722 unsigned int regno1, regno2;
5723
a92713e6 5724 if (arg->token->type == OT_INTEGER)
364215c8
RS
5725 {
5726 /* Handle the frame size. */
1a00e612 5727 if (!match_const_int (arg, &frame_size))
5b7c81bd 5728 return false;
364215c8 5729 num_frame_sizes += 1;
364215c8
RS
5730 }
5731 else
5732 {
a92713e6 5733 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5b7c81bd 5734 return false;
364215c8
RS
5735
5736 while (regno1 <= regno2)
5737 {
5738 if (regno1 >= 4 && regno1 <= 7)
5739 {
5740 if (num_frame_sizes == 0)
5741 /* args $a0-$a3 */
5742 args |= 1 << (regno1 - 4);
5743 else
5744 /* statics $a0-$a3 */
5745 statics |= 1 << (regno1 - 4);
5746 }
5747 else if (regno1 >= 16 && regno1 <= 23)
5748 /* $s0-$s7 */
5749 sregs |= 1 << (regno1 - 16);
5750 else if (regno1 == 30)
5751 /* $s8 */
5752 sregs |= 1 << 8;
5753 else if (regno1 == 31)
5754 /* Add $ra to insn. */
38bf472a 5755 ra = 1;
364215c8 5756 else
5b7c81bd 5757 return false;
364215c8
RS
5758 regno1 += 1;
5759 if (regno1 == 24)
5760 regno1 = 30;
5761 }
5762 }
364215c8 5763 }
a92713e6 5764 while (match_char (arg, ','));
364215c8
RS
5765
5766 /* Encode args/statics combination. */
5767 if (args & statics)
5b7c81bd 5768 return false;
364215c8
RS
5769 else if (args == 0xf)
5770 /* All $a0-$a3 are args. */
38bf472a 5771 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5772 else if (statics == 0xf)
5773 /* All $a0-$a3 are statics. */
38bf472a 5774 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5775 else
5776 {
5777 /* Count arg registers. */
5778 num_args = 0;
5779 while (args & 0x1)
5780 {
5781 args >>= 1;
5782 num_args += 1;
5783 }
5784 if (args != 0)
5b7c81bd 5785 return false;
364215c8
RS
5786
5787 /* Count static registers. */
5788 num_statics = 0;
5789 while (statics & 0x8)
5790 {
5791 statics = (statics << 1) & 0xf;
5792 num_statics += 1;
5793 }
5794 if (statics != 0)
5b7c81bd 5795 return false;
364215c8
RS
5796
5797 /* Encode args/statics. */
38bf472a 5798 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5799 }
5800
5801 /* Encode $s0/$s1. */
5802 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5803 s0 = 1;
364215c8 5804 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5805 s1 = 1;
364215c8
RS
5806 sregs >>= 2;
5807
5808 /* Encode $s2-$s8. */
5809 num_sregs = 0;
5810 while (sregs & 1)
5811 {
5812 sregs >>= 1;
5813 num_sregs += 1;
5814 }
5815 if (sregs != 0)
5b7c81bd 5816 return false;
364215c8
RS
5817
5818 /* Encode frame size. */
5819 if (num_frame_sizes == 0)
1a00e612
RS
5820 {
5821 set_insn_error (arg->argnum, _("missing frame size"));
5b7c81bd 5822 return false;
1a00e612
RS
5823 }
5824 if (num_frame_sizes > 1)
5825 {
5826 set_insn_error (arg->argnum, _("frame size specified twice"));
5b7c81bd 5827 return false;
1a00e612
RS
5828 }
5829 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5830 {
5831 set_insn_error (arg->argnum, _("invalid frame size"));
5b7c81bd 5832 return false;
1a00e612 5833 }
38bf472a 5834 frame_size /= 8;
364215c8 5835
364215c8 5836 /* Finally build the instruction. */
38bf472a
MR
5837 if (mips_opts.mips16)
5838 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5839 frame_size);
5840 else if (!mips_opts.micromips)
5841 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5842 frame_size);
5843 else
5844 abort ();
5845
364215c8 5846 arg->insn->insn_opcode = opcode;
5b7c81bd 5847 return true;
364215c8
RS
5848}
5849
a1d78564
RS
5850/* OP_MDMX_IMM_REG matcher. */
5851
5b7c81bd 5852static bool
a1d78564 5853match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5854 const struct mips_operand *operand)
a1d78564 5855{
a92713e6 5856 unsigned int regno, uval;
5b7c81bd 5857 bool is_qh;
a1d78564
RS
5858 const struct mips_opcode *opcode;
5859
5860 /* The mips_opcode records whether this is an octobyte or quadhalf
5861 instruction. Start out with that bit in place. */
5862 opcode = arg->insn->insn_mo;
5863 uval = mips_extract_operand (operand, opcode->match);
5864 is_qh = (uval != 0);
5865
56d438b1 5866 if (arg->token->type == OT_REG)
a1d78564
RS
5867 {
5868 if ((opcode->membership & INSN_5400)
5869 && strcmp (opcode->name, "rzu.ob") == 0)
5870 {
1a00e612
RS
5871 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5872 arg->argnum);
5b7c81bd 5873 return false;
a1d78564
RS
5874 }
5875
56d438b1 5876 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5b7c81bd 5877 return false;
56d438b1
CF
5878 ++arg->token;
5879
a1d78564
RS
5880 /* Check whether this is a vector register or a broadcast of
5881 a single element. */
56d438b1 5882 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5883 {
56d438b1 5884 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5885 {
1a00e612 5886 set_insn_error (arg->argnum, _("invalid element selector"));
5b7c81bd 5887 return false;
a1d78564 5888 }
56d438b1
CF
5889 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5890 ++arg->token;
a1d78564
RS
5891 }
5892 else
5893 {
5894 /* A full vector. */
5895 if ((opcode->membership & INSN_5400)
5896 && (strcmp (opcode->name, "sll.ob") == 0
5897 || strcmp (opcode->name, "srl.ob") == 0))
5898 {
1a00e612
RS
5899 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5900 arg->argnum);
5b7c81bd 5901 return false;
a1d78564
RS
5902 }
5903
5904 if (is_qh)
5905 uval |= MDMX_FMTSEL_VEC_QH << 5;
5906 else
5907 uval |= MDMX_FMTSEL_VEC_OB << 5;
5908 }
a1d78564
RS
5909 uval |= regno;
5910 }
5911 else
5912 {
5913 offsetT sval;
5914
1a00e612 5915 if (!match_const_int (arg, &sval))
5b7c81bd 5916 return false;
a1d78564
RS
5917 if (sval < 0 || sval > 31)
5918 {
1a00e612 5919 match_out_of_range (arg);
5b7c81bd 5920 return false;
a1d78564
RS
5921 }
5922 uval |= (sval & 31);
5923 if (is_qh)
5924 uval |= MDMX_FMTSEL_IMM_QH << 5;
5925 else
5926 uval |= MDMX_FMTSEL_IMM_OB << 5;
5927 }
5928 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 5929 return true;
a1d78564
RS
5930}
5931
56d438b1
CF
5932/* OP_IMM_INDEX matcher. */
5933
5b7c81bd 5934static bool
56d438b1
CF
5935match_imm_index_operand (struct mips_arg_info *arg,
5936 const struct mips_operand *operand)
5937{
5938 unsigned int max_val;
5939
5940 if (arg->token->type != OT_INTEGER_INDEX)
5b7c81bd 5941 return false;
56d438b1
CF
5942
5943 max_val = (1 << operand->size) - 1;
5944 if (arg->token->u.index > max_val)
5945 {
5946 match_out_of_range (arg);
5b7c81bd 5947 return false;
56d438b1
CF
5948 }
5949 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5950 ++arg->token;
5b7c81bd 5951 return true;
56d438b1
CF
5952}
5953
5954/* OP_REG_INDEX matcher. */
5955
5b7c81bd 5956static bool
56d438b1
CF
5957match_reg_index_operand (struct mips_arg_info *arg,
5958 const struct mips_operand *operand)
5959{
5960 unsigned int regno;
5961
5962 if (arg->token->type != OT_REG_INDEX)
5b7c81bd 5963 return false;
56d438b1
CF
5964
5965 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5b7c81bd 5966 return false;
56d438b1
CF
5967
5968 insn_insert_operand (arg->insn, operand, regno);
5969 ++arg->token;
5b7c81bd 5970 return true;
56d438b1
CF
5971}
5972
a1d78564
RS
5973/* OP_PC matcher. */
5974
5b7c81bd 5975static bool
a92713e6 5976match_pc_operand (struct mips_arg_info *arg)
a1d78564 5977{
a92713e6
RS
5978 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5979 {
5980 ++arg->token;
5b7c81bd 5981 return true;
a92713e6 5982 }
5b7c81bd 5983 return false;
a1d78564
RS
5984}
5985
25499ac7
MR
5986/* OP_REG28 matcher. */
5987
5b7c81bd 5988static bool
25499ac7
MR
5989match_reg28_operand (struct mips_arg_info *arg)
5990{
5991 unsigned int regno;
5992
5993 if (arg->token->type == OT_REG
5994 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5995 && regno == GP)
5996 {
5997 ++arg->token;
5b7c81bd 5998 return true;
25499ac7 5999 }
5b7c81bd 6000 return false;
25499ac7
MR
6001}
6002
7361da2c
AB
6003/* OP_NON_ZERO_REG matcher. */
6004
5b7c81bd 6005static bool
7361da2c
AB
6006match_non_zero_reg_operand (struct mips_arg_info *arg,
6007 const struct mips_operand *operand)
6008{
6009 unsigned int regno;
6010
6011 if (!match_reg (arg, OP_REG_GP, &regno))
5b7c81bd 6012 return false;
7361da2c
AB
6013
6014 if (regno == 0)
85bec12d
MF
6015 {
6016 set_insn_error (arg->argnum, _("the source register must not be $0"));
5b7c81bd 6017 return false;
85bec12d 6018 }
7361da2c
AB
6019
6020 arg->last_regno = regno;
6021 insn_insert_operand (arg->insn, operand, regno);
5b7c81bd 6022 return true;
7361da2c
AB
6023}
6024
a1d78564
RS
6025/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
6026 register that we need to match. */
6027
5b7c81bd 6028static bool
a92713e6 6029match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
6030{
6031 unsigned int regno;
6032
a92713e6 6033 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
6034}
6035
33f46696
MR
6036/* Try to match a floating-point constant from ARG for LI.S or LI.D.
6037 LENGTH is the length of the value in bytes (4 for float, 8 for double)
6038 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
6039
6040 Return the constant in IMM and OFFSET as follows:
6041
6042 - If the constant should be loaded via memory, set IMM to O_absent and
6043 OFFSET to the memory address.
6044
6045 - Otherwise, if the constant should be loaded into two 32-bit registers,
6046 set IMM to the O_constant to load into the high register and OFFSET
6047 to the corresponding value for the low register.
6048
6049 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6050
6051 These constants only appear as the last operand in an instruction,
6052 and every instruction that accepts them in any variant accepts them
6053 in all variants. This means we don't have to worry about backing out
6054 any changes if the instruction does not match. We just match
6055 unconditionally and report an error if the constant is invalid. */
6056
5b7c81bd 6057static bool
a92713e6 6058match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5b7c81bd 6059 expressionS *offset, int length, bool using_gprs)
89565f1b 6060{
a92713e6 6061 char *p;
89565f1b
RS
6062 segT seg, new_seg;
6063 subsegT subseg;
6064 const char *newname;
a92713e6 6065 unsigned char *data;
89565f1b
RS
6066
6067 /* Where the constant is placed is based on how the MIPS assembler
6068 does things:
6069
6070 length == 4 && using_gprs -- immediate value only
6071 length == 8 && using_gprs -- .rdata or immediate value
6072 length == 4 && !using_gprs -- .lit4 or immediate value
6073 length == 8 && !using_gprs -- .lit8 or immediate value
6074
6075 The .lit4 and .lit8 sections are only used if permitted by the
6076 -G argument. */
a92713e6 6077 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6078 {
6079 set_insn_error (arg->argnum, _("floating-point expression required"));
5b7c81bd 6080 return false;
1a00e612 6081 }
a92713e6
RS
6082
6083 gas_assert (arg->token->u.flt.length == length);
6084 data = arg->token->u.flt.data;
6085 ++arg->token;
89565f1b
RS
6086
6087 /* Handle 32-bit constants for which an immediate value is best. */
6088 if (length == 4
6089 && (using_gprs
6090 || g_switch_value < 4
6091 || (data[0] == 0 && data[1] == 0)
6092 || (data[2] == 0 && data[3] == 0)))
6093 {
6094 imm->X_op = O_constant;
6095 if (!target_big_endian)
6096 imm->X_add_number = bfd_getl32 (data);
6097 else
6098 imm->X_add_number = bfd_getb32 (data);
6099 offset->X_op = O_absent;
5b7c81bd 6100 return true;
89565f1b
RS
6101 }
6102
6103 /* Handle 64-bit constants for which an immediate value is best. */
6104 if (length == 8
6105 && !mips_disable_float_construction
351cdf24
MF
6106 /* Constants can only be constructed in GPRs and copied to FPRs if the
6107 GPRs are at least as wide as the FPRs or MTHC1 is available.
6108 Unlike most tests for 32-bit floating-point registers this check
6109 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6110 permit 64-bit moves without MXHC1.
6111 Force the constant into memory otherwise. */
6112 && (using_gprs
6113 || GPR_SIZE == 64
6114 || ISA_HAS_MXHC1 (mips_opts.isa)
6115 || FPR_SIZE == 32)
89565f1b
RS
6116 && ((data[0] == 0 && data[1] == 0)
6117 || (data[2] == 0 && data[3] == 0))
6118 && ((data[4] == 0 && data[5] == 0)
6119 || (data[6] == 0 && data[7] == 0)))
6120 {
6121 /* The value is simple enough to load with a couple of instructions.
6122 If using 32-bit registers, set IMM to the high order 32 bits and
6123 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6124 64 bit constant. */
351cdf24 6125 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6126 {
6127 imm->X_op = O_constant;
6128 offset->X_op = O_constant;
6129 if (!target_big_endian)
6130 {
6131 imm->X_add_number = bfd_getl32 (data + 4);
6132 offset->X_add_number = bfd_getl32 (data);
6133 }
6134 else
6135 {
6136 imm->X_add_number = bfd_getb32 (data);
6137 offset->X_add_number = bfd_getb32 (data + 4);
6138 }
6139 if (offset->X_add_number == 0)
6140 offset->X_op = O_absent;
6141 }
6142 else
6143 {
6144 imm->X_op = O_constant;
6145 if (!target_big_endian)
6146 imm->X_add_number = bfd_getl64 (data);
6147 else
6148 imm->X_add_number = bfd_getb64 (data);
6149 offset->X_op = O_absent;
6150 }
5b7c81bd 6151 return true;
89565f1b
RS
6152 }
6153
6154 /* Switch to the right section. */
6155 seg = now_seg;
6156 subseg = now_subseg;
6157 if (length == 4)
6158 {
6159 gas_assert (!using_gprs && g_switch_value >= 4);
6160 newname = ".lit4";
6161 }
6162 else
6163 {
6164 if (using_gprs || g_switch_value < 8)
6165 newname = RDATA_SECTION_NAME;
6166 else
6167 newname = ".lit8";
6168 }
6169
6170 new_seg = subseg_new (newname, (subsegT) 0);
fd361982 6171 bfd_set_section_flags (new_seg,
89565f1b
RS
6172 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6173 frag_align (length == 4 ? 2 : 3, 0, 0);
d34049e8 6174 if (!startswith (TARGET_OS, "elf"))
89565f1b
RS
6175 record_alignment (new_seg, 4);
6176 else
6177 record_alignment (new_seg, length == 4 ? 2 : 3);
6178 if (seg == now_seg)
1661c76c 6179 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6180
6181 /* Set the argument to the current address in the section. */
6182 imm->X_op = O_absent;
6183 offset->X_op = O_symbol;
6184 offset->X_add_symbol = symbol_temp_new_now ();
6185 offset->X_add_number = 0;
6186
6187 /* Put the floating point number into the section. */
6188 p = frag_more (length);
6189 memcpy (p, data, length);
6190
6191 /* Switch back to the original section. */
6192 subseg_set (seg, subseg);
5b7c81bd 6193 return true;
89565f1b
RS
6194}
6195
14daeee3
RS
6196/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6197 them. */
6198
5b7c81bd 6199static bool
14daeee3
RS
6200match_vu0_suffix_operand (struct mips_arg_info *arg,
6201 const struct mips_operand *operand,
5b7c81bd 6202 bool match_p)
14daeee3
RS
6203{
6204 unsigned int uval;
6205
6206 /* The operand can be an XYZW mask or a single 2-bit channel index
6207 (with X being 0). */
6208 gas_assert (operand->size == 2 || operand->size == 4);
6209
ee5734f0 6210 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6211 if (arg->token->type != OT_CHANNELS)
ee5734f0 6212 return match_p;
14daeee3
RS
6213
6214 uval = arg->token->u.channels;
6215 if (operand->size == 2)
6216 {
6217 /* Check that a single bit is set and convert it into a 2-bit index. */
6218 if ((uval & -uval) != uval)
5b7c81bd 6219 return false;
14daeee3
RS
6220 uval = 4 - ffs (uval);
6221 }
6222
6223 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5b7c81bd 6224 return false;
14daeee3
RS
6225
6226 ++arg->token;
6227 if (!match_p)
6228 insn_insert_operand (arg->insn, operand, uval);
5b7c81bd 6229 return true;
14daeee3
RS
6230}
6231
33f46696
MR
6232/* Try to match a token from ARG against OPERAND. Consume the token
6233 and return true on success, otherwise return false. */
a1d78564 6234
5b7c81bd 6235static bool
a1d78564 6236match_operand (struct mips_arg_info *arg,
a92713e6 6237 const struct mips_operand *operand)
a1d78564
RS
6238{
6239 switch (operand->type)
6240 {
6241 case OP_INT:
a92713e6 6242 return match_int_operand (arg, operand);
a1d78564
RS
6243
6244 case OP_MAPPED_INT:
a92713e6 6245 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6246
6247 case OP_MSB:
a92713e6 6248 return match_msb_operand (arg, operand);
a1d78564
RS
6249
6250 case OP_REG:
0f35dbc4 6251 case OP_OPTIONAL_REG:
a92713e6 6252 return match_reg_operand (arg, operand);
a1d78564
RS
6253
6254 case OP_REG_PAIR:
a92713e6 6255 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6256
6257 case OP_PCREL:
a92713e6 6258 return match_pcrel_operand (arg);
a1d78564
RS
6259
6260 case OP_PERF_REG:
a92713e6 6261 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6262
6263 case OP_ADDIUSP_INT:
a92713e6 6264 return match_addiusp_operand (arg, operand);
a1d78564
RS
6265
6266 case OP_CLO_CLZ_DEST:
a92713e6 6267 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6268
6269 case OP_LWM_SWM_LIST:
a92713e6 6270 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6271
6272 case OP_ENTRY_EXIT_LIST:
a92713e6 6273 return match_entry_exit_operand (arg, operand);
364215c8 6274
a1d78564 6275 case OP_SAVE_RESTORE_LIST:
a92713e6 6276 return match_save_restore_list_operand (arg);
a1d78564
RS
6277
6278 case OP_MDMX_IMM_REG:
a92713e6 6279 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6280
6281 case OP_REPEAT_DEST_REG:
a92713e6 6282 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6283
6284 case OP_REPEAT_PREV_REG:
a92713e6 6285 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6286
6287 case OP_PC:
a92713e6 6288 return match_pc_operand (arg);
14daeee3 6289
25499ac7
MR
6290 case OP_REG28:
6291 return match_reg28_operand (arg);
6292
14daeee3 6293 case OP_VU0_SUFFIX:
5b7c81bd 6294 return match_vu0_suffix_operand (arg, operand, false);
14daeee3
RS
6295
6296 case OP_VU0_MATCH_SUFFIX:
5b7c81bd 6297 return match_vu0_suffix_operand (arg, operand, true);
56d438b1
CF
6298
6299 case OP_IMM_INDEX:
6300 return match_imm_index_operand (arg, operand);
6301
6302 case OP_REG_INDEX:
6303 return match_reg_index_operand (arg, operand);
7361da2c
AB
6304
6305 case OP_SAME_RS_RT:
6306 return match_same_rs_rt_operand (arg, operand);
6307
6308 case OP_CHECK_PREV:
6309 return match_check_prev_operand (arg, operand);
6310
6311 case OP_NON_ZERO_REG:
6312 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6313 }
6314 abort ();
6315}
6316
6317/* ARG is the state after successfully matching an instruction.
6318 Issue any queued-up warnings. */
6319
6320static void
6321check_completed_insn (struct mips_arg_info *arg)
6322{
6323 if (arg->seen_at)
6324 {
6325 if (AT == ATREG)
1661c76c 6326 as_warn (_("used $at without \".set noat\""));
a1d78564 6327 else
1661c76c 6328 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6329 }
6330}
a1d78564 6331
85fcb30f
RS
6332/* Return true if modifying general-purpose register REG needs a delay. */
6333
5b7c81bd 6334static bool
85fcb30f
RS
6335reg_needs_delay (unsigned int reg)
6336{
6337 unsigned long prev_pinfo;
6338
6339 prev_pinfo = history[0].insn_mo->pinfo;
6340 if (!mips_opts.noreorder
67dc82bc 6341 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6342 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f 6343 && (gpr_write_mask (&history[0]) & (1 << reg)))
5b7c81bd 6344 return true;
85fcb30f 6345
5b7c81bd 6346 return false;
85fcb30f
RS
6347}
6348
71400594
RS
6349/* Classify an instruction according to the FIX_VR4120_* enumeration.
6350 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6351 by VR4120 errata. */
4d7206a2 6352
71400594
RS
6353static unsigned int
6354classify_vr4120_insn (const char *name)
252b5132 6355{
d34049e8 6356 if (startswith (name, "macc"))
71400594 6357 return FIX_VR4120_MACC;
d34049e8 6358 if (startswith (name, "dmacc"))
71400594 6359 return FIX_VR4120_DMACC;
d34049e8 6360 if (startswith (name, "mult"))
71400594 6361 return FIX_VR4120_MULT;
d34049e8 6362 if (startswith (name, "dmult"))
71400594
RS
6363 return FIX_VR4120_DMULT;
6364 if (strstr (name, "div"))
6365 return FIX_VR4120_DIV;
6366 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6367 return FIX_VR4120_MTHILO;
6368 return NUM_FIX_VR4120_CLASSES;
6369}
252b5132 6370
a8d14a88
CM
6371#define INSN_ERET 0x42000018
6372#define INSN_DERET 0x4200001f
6373#define INSN_DMULT 0x1c
6374#define INSN_DMULTU 0x1d
ff239038 6375
71400594
RS
6376/* Return the number of instructions that must separate INSN1 and INSN2,
6377 where INSN1 is the earlier instruction. Return the worst-case value
6378 for any INSN2 if INSN2 is null. */
252b5132 6379
71400594
RS
6380static unsigned int
6381insns_between (const struct mips_cl_insn *insn1,
6382 const struct mips_cl_insn *insn2)
6383{
6384 unsigned long pinfo1, pinfo2;
4c260379 6385 unsigned int mask;
71400594 6386
85fcb30f
RS
6387 /* If INFO2 is null, pessimistically assume that all flags are set for
6388 the second instruction. */
71400594
RS
6389 pinfo1 = insn1->insn_mo->pinfo;
6390 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6391
71400594
RS
6392 /* For most targets, write-after-read dependencies on the HI and LO
6393 registers must be separated by at least two instructions. */
6394 if (!hilo_interlocks)
252b5132 6395 {
71400594
RS
6396 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6397 return 2;
6398 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6399 return 2;
6400 }
6401
6402 /* If we're working around r7000 errata, there must be two instructions
6403 between an mfhi or mflo and any instruction that uses the result. */
6404 if (mips_7000_hilo_fix
df58fc94 6405 && !mips_opts.micromips
71400594 6406 && MF_HILO_INSN (pinfo1)
85fcb30f 6407 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6408 return 2;
6409
ff239038
CM
6410 /* If we're working around 24K errata, one instruction is required
6411 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6412 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6413 {
6414 if (insn1->insn_opcode == INSN_ERET
6415 || insn1->insn_opcode == INSN_DERET)
6416 {
6417 if (insn2 == NULL
6418 || insn2->insn_opcode == INSN_ERET
6419 || insn2->insn_opcode == INSN_DERET
11625dd8 6420 || delayed_branch_p (insn2))
ff239038
CM
6421 return 1;
6422 }
6423 }
6424
a8d14a88
CM
6425 /* If we're working around PMC RM7000 errata, there must be three
6426 nops between a dmult and a load instruction. */
6427 if (mips_fix_rm7000 && !mips_opts.micromips)
6428 {
6429 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6430 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6431 {
6432 if (pinfo2 & INSN_LOAD_MEMORY)
6433 return 3;
6434 }
6435 }
6436
71400594
RS
6437 /* If working around VR4120 errata, check for combinations that need
6438 a single intervening instruction. */
df58fc94 6439 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6440 {
6441 unsigned int class1, class2;
252b5132 6442
71400594
RS
6443 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6444 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6445 {
71400594
RS
6446 if (insn2 == NULL)
6447 return 1;
6448 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6449 if (vr4120_conflicts[class1] & (1 << class2))
6450 return 1;
252b5132 6451 }
71400594
RS
6452 }
6453
df58fc94 6454 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6455 {
6456 /* Check for GPR or coprocessor load delays. All such delays
6457 are on the RT register. */
6458 /* Itbl support may require additional care here. */
67dc82bc 6459 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6460 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6461 {
85fcb30f 6462 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6463 return 1;
6464 }
6465
6466 /* Check for generic coprocessor hazards.
6467
6468 This case is not handled very well. There is no special
6469 knowledge of CP0 handling, and the coprocessors other than
6470 the floating point unit are not distinguished at all. */
6471 /* Itbl support may require additional care here. FIXME!
6472 Need to modify this to include knowledge about
6473 user specified delays! */
43885403 6474 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6475 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6476 {
6477 /* Handle cases where INSN1 writes to a known general coprocessor
6478 register. There must be a one instruction delay before INSN2
6479 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6480 mask = fpr_write_mask (insn1);
6481 if (mask != 0)
252b5132 6482 {
4c260379 6483 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6484 return 1;
252b5132
RH
6485 }
6486 else
6487 {
71400594
RS
6488 /* Read-after-write dependencies on the control registers
6489 require a two-instruction gap. */
6490 if ((pinfo1 & INSN_WRITE_COND_CODE)
6491 && (pinfo2 & INSN_READ_COND_CODE))
6492 return 2;
6493
6494 /* We don't know exactly what INSN1 does. If INSN2 is
6495 also a coprocessor instruction, assume there must be
6496 a one instruction gap. */
6497 if (pinfo2 & INSN_COP)
6498 return 1;
252b5132
RH
6499 }
6500 }
6b76fefe 6501
71400594
RS
6502 /* Check for read-after-write dependencies on the coprocessor
6503 control registers in cases where INSN1 does not need a general
6504 coprocessor delay. This means that INSN1 is a floating point
6505 comparison instruction. */
6506 /* Itbl support may require additional care here. */
6507 else if (!cop_interlocks
6508 && (pinfo1 & INSN_WRITE_COND_CODE)
6509 && (pinfo2 & INSN_READ_COND_CODE))
6510 return 1;
6511 }
6b76fefe 6512
7361da2c
AB
6513 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6514 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6515 and pause. */
6516 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6517 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6518 || (insn2 && delayed_branch_p (insn2))))
6519 return 1;
6520
71400594
RS
6521 return 0;
6522}
6b76fefe 6523
7d8e00cf
RS
6524/* Return the number of nops that would be needed to work around the
6525 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6526 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6527 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6528
6529static int
932d1a1b 6530nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6531 const struct mips_cl_insn *insn)
6532{
4c260379
RS
6533 int i, j;
6534 unsigned int mask;
7d8e00cf
RS
6535
6536 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6537 are not affected by the errata. */
6538 if (insn != 0
6539 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6540 || strcmp (insn->insn_mo->name, "mtlo") == 0
6541 || strcmp (insn->insn_mo->name, "mthi") == 0))
6542 return 0;
6543
6544 /* Search for the first MFLO or MFHI. */
6545 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6546 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6547 {
6548 /* Extract the destination register. */
4c260379 6549 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6550
6551 /* No nops are needed if INSN reads that register. */
4c260379 6552 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6553 return 0;
6554
6555 /* ...or if any of the intervening instructions do. */
6556 for (j = 0; j < i; j++)
4c260379 6557 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6558 return 0;
6559
932d1a1b
RS
6560 if (i >= ignore)
6561 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6562 }
6563 return 0;
6564}
6565
134c0c8b
MR
6566#define BASE_REG_EQ(INSN1, INSN2) \
6567 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6568 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6569
6570/* Return the minimum alignment for this store instruction. */
6571
6572static int
6573fix_24k_align_to (const struct mips_opcode *mo)
6574{
6575 if (strcmp (mo->name, "sh") == 0)
6576 return 2;
6577
6578 if (strcmp (mo->name, "swc1") == 0
6579 || strcmp (mo->name, "swc2") == 0
6580 || strcmp (mo->name, "sw") == 0
6581 || strcmp (mo->name, "sc") == 0
6582 || strcmp (mo->name, "s.s") == 0)
6583 return 4;
6584
6585 if (strcmp (mo->name, "sdc1") == 0
6586 || strcmp (mo->name, "sdc2") == 0
6587 || strcmp (mo->name, "s.d") == 0)
6588 return 8;
6589
6590 /* sb, swl, swr */
6591 return 1;
6592}
6593
6594struct fix_24k_store_info
6595 {
6596 /* Immediate offset, if any, for this store instruction. */
6597 short off;
6598 /* Alignment required by this store instruction. */
6599 int align_to;
6600 /* True for register offsets. */
6601 int register_offset;
6602 };
6603
6604/* Comparison function used by qsort. */
6605
6606static int
6607fix_24k_sort (const void *a, const void *b)
6608{
6609 const struct fix_24k_store_info *pos1 = a;
6610 const struct fix_24k_store_info *pos2 = b;
6611
6612 return (pos1->off - pos2->off);
6613}
6614
6615/* INSN is a store instruction. Try to record the store information
6616 in STINFO. Return false if the information isn't known. */
6617
5b7c81bd 6618static bool
15be625d 6619fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6620 const struct mips_cl_insn *insn)
15be625d
CM
6621{
6622 /* The instruction must have a known offset. */
6623 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
5b7c81bd 6624 return false;
15be625d
CM
6625
6626 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6627 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
5b7c81bd 6628 return true;
15be625d
CM
6629}
6630
932d1a1b
RS
6631/* Return the number of nops that would be needed to work around the 24k
6632 "lost data on stores during refill" errata if instruction INSN
6633 immediately followed the 2 instructions described by HIST.
6634 Ignore hazards that are contained within the first IGNORE
6635 instructions of HIST.
6636
6637 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6638 for the data cache refills and store data. The following describes
6639 the scenario where the store data could be lost.
6640
6641 * A data cache miss, due to either a load or a store, causing fill
6642 data to be supplied by the memory subsystem
6643 * The first three doublewords of fill data are returned and written
6644 into the cache
6645 * A sequence of four stores occurs in consecutive cycles around the
6646 final doubleword of the fill:
6647 * Store A
6648 * Store B
6649 * Store C
6650 * Zero, One or more instructions
6651 * Store D
6652
6653 The four stores A-D must be to different doublewords of the line that
6654 is being filled. The fourth instruction in the sequence above permits
6655 the fill of the final doubleword to be transferred from the FSB into
6656 the cache. In the sequence above, the stores may be either integer
6657 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6658 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6659 different doublewords on the line. If the floating point unit is
6660 running in 1:2 mode, it is not possible to create the sequence above
6661 using only floating point store instructions.
15be625d
CM
6662
6663 In this case, the cache line being filled is incorrectly marked
6664 invalid, thereby losing the data from any store to the line that
6665 occurs between the original miss and the completion of the five
6666 cycle sequence shown above.
6667
932d1a1b 6668 The workarounds are:
15be625d 6669
932d1a1b
RS
6670 * Run the data cache in write-through mode.
6671 * Insert a non-store instruction between
6672 Store A and Store B or Store B and Store C. */
3739860c 6673
15be625d 6674static int
932d1a1b 6675nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6676 const struct mips_cl_insn *insn)
6677{
6678 struct fix_24k_store_info pos[3];
6679 int align, i, base_offset;
6680
932d1a1b
RS
6681 if (ignore >= 2)
6682 return 0;
6683
ab9794cf
RS
6684 /* If the previous instruction wasn't a store, there's nothing to
6685 worry about. */
15be625d
CM
6686 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6687 return 0;
6688
ab9794cf
RS
6689 /* If the instructions after the previous one are unknown, we have
6690 to assume the worst. */
6691 if (!insn)
15be625d
CM
6692 return 1;
6693
ab9794cf
RS
6694 /* Check whether we are dealing with three consecutive stores. */
6695 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6696 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6697 return 0;
6698
6699 /* If we don't know the relationship between the store addresses,
6700 assume the worst. */
ab9794cf 6701 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6702 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6703 return 1;
6704
6705 if (!fix_24k_record_store_info (&pos[0], insn)
6706 || !fix_24k_record_store_info (&pos[1], &hist[0])
6707 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6708 return 1;
6709
6710 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6711
6712 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6713 X bytes and such that the base register + X is known to be aligned
6714 to align bytes. */
6715
6716 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6717 align = 8;
6718 else
6719 {
6720 align = pos[0].align_to;
6721 base_offset = pos[0].off;
6722 for (i = 1; i < 3; i++)
6723 if (align < pos[i].align_to)
6724 {
6725 align = pos[i].align_to;
6726 base_offset = pos[i].off;
6727 }
6728 for (i = 0; i < 3; i++)
6729 pos[i].off -= base_offset;
6730 }
6731
6732 pos[0].off &= ~align + 1;
6733 pos[1].off &= ~align + 1;
6734 pos[2].off &= ~align + 1;
6735
6736 /* If any two stores write to the same chunk, they also write to the
6737 same doubleword. The offsets are still sorted at this point. */
6738 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6739 return 0;
6740
6741 /* A range of at least 9 bytes is needed for the stores to be in
6742 non-overlapping doublewords. */
6743 if (pos[2].off - pos[0].off <= 8)
6744 return 0;
6745
6746 if (pos[2].off - pos[1].off >= 24
6747 || pos[1].off - pos[0].off >= 24
6748 || pos[2].off - pos[0].off >= 32)
6749 return 0;
6750
6751 return 1;
6752}
6753
71400594 6754/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6755 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6756 where HIST[0] is the most recent instruction. Ignore hazards
6757 between INSN and the first IGNORE instructions in HIST.
6758
6759 If INSN is null, return the worse-case number of nops for any
6760 instruction. */
bdaaa2e1 6761
71400594 6762static int
932d1a1b 6763nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6764 const struct mips_cl_insn *insn)
6765{
6766 int i, nops, tmp_nops;
bdaaa2e1 6767
71400594 6768 nops = 0;
932d1a1b 6769 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6770 {
91d6fa6a 6771 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6772 if (tmp_nops > nops)
6773 nops = tmp_nops;
6774 }
7d8e00cf 6775
df58fc94 6776 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6777 {
932d1a1b 6778 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6779 if (tmp_nops > nops)
6780 nops = tmp_nops;
6781 }
6782
df58fc94 6783 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6784 {
932d1a1b 6785 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6786 if (tmp_nops > nops)
6787 nops = tmp_nops;
6788 }
6789
71400594
RS
6790 return nops;
6791}
252b5132 6792
71400594 6793/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6794 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6795 would be needed after the extended sequence, ignoring hazards
6796 in the first IGNORE instructions. */
252b5132 6797
71400594 6798static int
932d1a1b
RS
6799nops_for_sequence (int num_insns, int ignore,
6800 const struct mips_cl_insn *hist, ...)
71400594
RS
6801{
6802 va_list args;
6803 struct mips_cl_insn buffer[MAX_NOPS];
6804 struct mips_cl_insn *cursor;
6805 int nops;
6806
91d6fa6a 6807 va_start (args, hist);
71400594 6808 cursor = buffer + num_insns;
91d6fa6a 6809 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6810 while (cursor > buffer)
6811 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6812
932d1a1b 6813 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6814 va_end (args);
6815 return nops;
6816}
252b5132 6817
71400594
RS
6818/* Like nops_for_insn, but if INSN is a branch, take into account the
6819 worst-case delay for the branch target. */
252b5132 6820
71400594 6821static int
932d1a1b 6822nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6823 const struct mips_cl_insn *insn)
6824{
6825 int nops, tmp_nops;
60b63b72 6826
932d1a1b 6827 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6828 if (delayed_branch_p (insn))
71400594 6829 {
932d1a1b 6830 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6831 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6832 if (tmp_nops > nops)
6833 nops = tmp_nops;
6834 }
11625dd8 6835 else if (compact_branch_p (insn))
71400594 6836 {
932d1a1b 6837 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6838 if (tmp_nops > nops)
6839 nops = tmp_nops;
6840 }
6841 return nops;
6842}
6843
c67a084a
NC
6844/* Fix NOP issue: Replace nops by "or at,at,zero". */
6845
6846static void
6847fix_loongson2f_nop (struct mips_cl_insn * ip)
6848{
df58fc94 6849 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6850 if (strcmp (ip->insn_mo->name, "nop") == 0)
6851 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6852}
6853
6854/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6855 jr target pc &= 'hffff_ffff_cfff_ffff. */
6856
6857static void
6858fix_loongson2f_jump (struct mips_cl_insn * ip)
6859{
df58fc94 6860 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6861 if (strcmp (ip->insn_mo->name, "j") == 0
6862 || strcmp (ip->insn_mo->name, "jr") == 0
6863 || strcmp (ip->insn_mo->name, "jalr") == 0)
6864 {
6865 int sreg;
6866 expressionS ep;
6867
6868 if (! mips_opts.at)
6869 return;
6870
df58fc94 6871 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6872 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6873 return;
6874
6875 ep.X_op = O_constant;
6876 ep.X_add_number = 0xcfff0000;
6877 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6878 ep.X_add_number = 0xffff;
6879 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6880 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6881 }
6882}
6883
6884static void
6885fix_loongson2f (struct mips_cl_insn * ip)
6886{
6887 if (mips_fix_loongson2f_nop)
6888 fix_loongson2f_nop (ip);
6889
6890 if (mips_fix_loongson2f_jump)
6891 fix_loongson2f_jump (ip);
6892}
6893
5b7c81bd 6894static bool
dec7b24b
YS
6895has_label_name (const char *arr[], size_t len ,const char *s)
6896{
6897 unsigned long i;
6898 for (i = 0; i < len; i++)
6899 {
6900 if (!arr[i])
5b7c81bd 6901 return false;
dec7b24b 6902 if (streq (arr[i], s))
5b7c81bd 6903 return true;
dec7b24b 6904 }
5b7c81bd 6905 return false;
dec7b24b
YS
6906}
6907
6908/* Fix loongson3 llsc errata: Insert sync before ll/lld. */
6f2117ba
PH
6909
6910static void
6911fix_loongson3_llsc (struct mips_cl_insn * ip)
6912{
6913 gas_assert (!HAVE_CODE_COMPRESSION);
6914
6915 /* If is an local label and the insn is not sync,
6916 look forward that whether an branch between ll/sc jump to here
6917 if so, insert a sync. */
6918 if (seg_info (now_seg)->label_list
6919 && S_IS_LOCAL (seg_info (now_seg)->label_list->label)
6920 && (strcmp (ip->insn_mo->name, "sync") != 0))
6921 {
6f2117ba 6922 unsigned long i;
dec7b24b
YS
6923 valueT label_value;
6924 const char *label_names[MAX_LABELS_SAME];
6925 const char *label_name;
6926
6927 label_name = S_GET_NAME (seg_info (now_seg)->label_list->label);
6928 label_names[0] = label_name;
6929 struct insn_label_list *llist = seg_info (now_seg)->label_list;
6930 label_value = S_GET_VALUE (llist->label);
6f2117ba 6931
dec7b24b
YS
6932 for (i = 1; i < MAX_LABELS_SAME; i++)
6933 {
6934 llist = llist->next;
6935 if (!llist)
6936 break;
6937 if (S_GET_VALUE (llist->label) == label_value)
6938 label_names[i] = S_GET_NAME (llist->label);
6939 else
6940 break;
6941 }
6942 for (; i < MAX_LABELS_SAME; i++)
6943 label_names[i] = NULL;
6944
6945 unsigned long lookback = ARRAY_SIZE (history);
6f2117ba
PH
6946 for (i = 0; i < lookback; i++)
6947 {
6f2117ba
PH
6948 if (streq (history[i].insn_mo->name, "sc")
6949 || streq (history[i].insn_mo->name, "scd"))
6950 {
6951 unsigned long j;
6952
6953 for (j = i + 1; j < lookback; j++)
6954 {
af989297
YS
6955 if (streq (history[j].insn_mo->name, "ll")
6956 || streq (history[j].insn_mo->name, "lld"))
6f2117ba
PH
6957 break;
6958
6959 if (delayed_branch_p (&history[j]))
6960 {
dec7b24b
YS
6961 if (has_label_name (label_names,
6962 MAX_LABELS_SAME,
6963 history[j].target))
6f2117ba
PH
6964 {
6965 add_fixed_insn (&sync_insn);
6966 insert_into_history (0, 1, &sync_insn);
6967 i = lookback;
6968 break;
6969 }
6970 }
6971 }
6972 }
6973 }
6974 }
6975 /* If we find a sc, we look forward to look for an branch insn,
6976 and see whether it jump back and out of ll/sc. */
dec7b24b 6977 else if (streq (ip->insn_mo->name, "sc") || streq (ip->insn_mo->name, "scd"))
6f2117ba
PH
6978 {
6979 unsigned long lookback = ARRAY_SIZE (history) - 1;
6980 unsigned long i;
6981
6982 for (i = 0; i < lookback; i++)
6983 {
6984 if (streq (history[i].insn_mo->name, "ll")
6985 || streq (history[i].insn_mo->name, "lld"))
6986 break;
6987
6988 if (delayed_branch_p (&history[i]))
6989 {
6990 unsigned long j;
6991
6992 for (j = i + 1; j < lookback; j++)
6993 {
6994 if (streq (history[j].insn_mo->name, "ll")
af989297 6995 || streq (history[j].insn_mo->name, "lld"))
6f2117ba
PH
6996 break;
6997 }
6998
6999 for (; j < lookback; j++)
7000 {
7001 if (history[j].label[0] != '\0'
7002 && streq (history[j].label, history[i].target)
7003 && strcmp (history[j+1].insn_mo->name, "sync") != 0)
7004 {
7005 add_fixed_insn (&sync_insn);
7006 insert_into_history (++j, 1, &sync_insn);
7007 }
7008 }
7009 }
7010 }
7011 }
7012
7013 /* Skip if there is a sync before ll/lld. */
7014 if ((strcmp (ip->insn_mo->name, "ll") == 0
7015 || strcmp (ip->insn_mo->name, "lld") == 0)
7016 && (strcmp (history[0].insn_mo->name, "sync") != 0))
7017 {
7018 add_fixed_insn (&sync_insn);
7019 insert_into_history (0, 1, &sync_insn);
7020 }
7021}
7022
a4e06468
RS
7023/* IP is a branch that has a delay slot, and we need to fill it
7024 automatically. Return true if we can do that by swapping IP
e407c74b
NC
7025 with the previous instruction.
7026 ADDRESS_EXPR is an operand of the instruction to be used with
7027 RELOC_TYPE. */
a4e06468 7028
5b7c81bd 7029static bool
e407c74b 7030can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7031 bfd_reloc_code_real_type *reloc_type)
a4e06468 7032{
2b0c8b40 7033 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 7034 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 7035 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
7036
7037 /* -O2 and above is required for this optimization. */
7038 if (mips_optimize < 2)
5b7c81bd 7039 return false;
a4e06468
RS
7040
7041 /* If we have seen .set volatile or .set nomove, don't optimize. */
7042 if (mips_opts.nomove)
5b7c81bd 7043 return false;
a4e06468
RS
7044
7045 /* We can't swap if the previous instruction's position is fixed. */
7046 if (history[0].fixed_p)
5b7c81bd 7047 return false;
a4e06468
RS
7048
7049 /* If the previous previous insn was in a .set noreorder, we can't
7050 swap. Actually, the MIPS assembler will swap in this situation.
7051 However, gcc configured -with-gnu-as will generate code like
7052
7053 .set noreorder
7054 lw $4,XXX
7055 .set reorder
7056 INSN
7057 bne $4,$0,foo
7058
7059 in which we can not swap the bne and INSN. If gcc is not configured
7060 -with-gnu-as, it does not output the .set pseudo-ops. */
7061 if (history[1].noreorder_p)
5b7c81bd 7062 return false;
a4e06468 7063
87333bb7
MR
7064 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
7065 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468 7066 if (mips_opts.mips16 && history[0].fixp[0])
5b7c81bd 7067 return false;
a4e06468
RS
7068
7069 /* If the branch is itself the target of a branch, we can not swap.
7070 We cheat on this; all we check for is whether there is a label on
7071 this instruction. If there are any branches to anything other than
7072 a label, users must use .set noreorder. */
7073 if (seg_info (now_seg)->label_list)
5b7c81bd 7074 return false;
a4e06468
RS
7075
7076 /* If the previous instruction is in a variant frag other than this
2309ddf2 7077 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
7078 MIPS16 code, which uses variant frags for different purposes. */
7079 if (!mips_opts.mips16
a4e06468
RS
7080 && history[0].frag
7081 && history[0].frag->fr_type == rs_machine_dependent)
5b7c81bd 7082 return false;
a4e06468 7083
bcd530a7
RS
7084 /* We do not swap with instructions that cannot architecturally
7085 be placed in a branch delay slot, such as SYNC or ERET. We
7086 also refrain from swapping with a trap instruction, since it
7087 complicates trap handlers to have the trap instruction be in
7088 a delay slot. */
a4e06468 7089 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 7090 if (prev_pinfo & INSN_NO_DELAY_SLOT)
5b7c81bd 7091 return false;
a4e06468
RS
7092
7093 /* Check for conflicts between the branch and the instructions
7094 before the candidate delay slot. */
7095 if (nops_for_insn (0, history + 1, ip) > 0)
5b7c81bd 7096 return false;
a4e06468
RS
7097
7098 /* Check for conflicts between the swapped sequence and the
7099 target of the branch. */
7100 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
5b7c81bd 7101 return false;
a4e06468
RS
7102
7103 /* If the branch reads a register that the previous
7104 instruction sets, we can not swap. */
7105 gpr_read = gpr_read_mask (ip);
7106 prev_gpr_write = gpr_write_mask (&history[0]);
7107 if (gpr_read & prev_gpr_write)
5b7c81bd 7108 return false;
a4e06468 7109
9d5de888
CF
7110 fpr_read = fpr_read_mask (ip);
7111 prev_fpr_write = fpr_write_mask (&history[0]);
7112 if (fpr_read & prev_fpr_write)
5b7c81bd 7113 return false;
9d5de888 7114
a4e06468
RS
7115 /* If the branch writes a register that the previous
7116 instruction sets, we can not swap. */
7117 gpr_write = gpr_write_mask (ip);
7118 if (gpr_write & prev_gpr_write)
5b7c81bd 7119 return false;
a4e06468
RS
7120
7121 /* If the branch writes a register that the previous
7122 instruction reads, we can not swap. */
7123 prev_gpr_read = gpr_read_mask (&history[0]);
7124 if (gpr_write & prev_gpr_read)
5b7c81bd 7125 return false;
a4e06468
RS
7126
7127 /* If one instruction sets a condition code and the
7128 other one uses a condition code, we can not swap. */
7129 pinfo = ip->insn_mo->pinfo;
7130 if ((pinfo & INSN_READ_COND_CODE)
7131 && (prev_pinfo & INSN_WRITE_COND_CODE))
5b7c81bd 7132 return false;
a4e06468
RS
7133 if ((pinfo & INSN_WRITE_COND_CODE)
7134 && (prev_pinfo & INSN_READ_COND_CODE))
5b7c81bd 7135 return false;
a4e06468
RS
7136
7137 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 7138 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 7139 if (prev_pinfo2 & INSN2_READ_PC)
5b7c81bd 7140 return false;
a4e06468 7141
df58fc94
RS
7142 /* If the previous instruction has an incorrect size for a fixed
7143 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
7144 pinfo2 = ip->insn_mo->pinfo2;
7145 if (mips_opts.micromips
7146 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
7147 && insn_length (history) != 2)
5b7c81bd 7148 return false;
2309ddf2
MR
7149 if (mips_opts.micromips
7150 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
7151 && insn_length (history) != 4)
5b7c81bd 7152 return false;
2309ddf2 7153
33d64ca5
FN
7154 /* On the R5900 short loops need to be fixed by inserting a NOP in the
7155 branch delay slot.
7156
7157 The short loop bug under certain conditions causes loops to execute
7158 only once or twice. We must ensure that the assembler never
7159 generates loops that satisfy all of the following conditions:
7160
7161 - a loop consists of less than or equal to six instructions
7162 (including the branch delay slot);
7163 - a loop contains only one conditional branch instruction at the end
7164 of the loop;
7165 - a loop does not contain any other branch or jump instructions;
7166 - a branch delay slot of the loop is not NOP (EE 2.9 or later).
7167
7168 We need to do this because of a hardware bug in the R5900 chip. */
27c634e0 7169 if (mips_fix_r5900
e407c74b
NC
7170 /* Check if instruction has a parameter, ignore "j $31". */
7171 && (address_expr != NULL)
7172 /* Parameter must be 16 bit. */
7173 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
7174 /* Branch to same segment. */
41065f5e 7175 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 7176 /* Branch to same code fragment. */
41065f5e 7177 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 7178 /* Can only calculate branch offset if value is known. */
41065f5e 7179 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
7180 /* Check if branch is really conditional. */
7181 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7182 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7183 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7184 {
7185 int distance;
33d64ca5
FN
7186 /* Check if loop is shorter than or equal to 6 instructions
7187 including branch and delay slot. */
41065f5e 7188 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7189 if (distance <= 20)
7190 {
7191 int i;
7192 int rv;
7193
5b7c81bd 7194 rv = false;
e407c74b
NC
7195 /* When the loop includes branches or jumps,
7196 it is not a short loop. */
7197 for (i = 0; i < (distance / 4); i++)
7198 {
7199 if ((history[i].cleared_p)
41065f5e 7200 || delayed_branch_p (&history[i]))
e407c74b 7201 {
5b7c81bd 7202 rv = true;
e407c74b
NC
7203 break;
7204 }
7205 }
535b785f 7206 if (!rv)
e407c74b
NC
7207 {
7208 /* Insert nop after branch to fix short loop. */
5b7c81bd 7209 return false;
e407c74b
NC
7210 }
7211 }
7212 }
7213
5b7c81bd 7214 return true;
a4e06468
RS
7215}
7216
e407c74b
NC
7217/* Decide how we should add IP to the instruction stream.
7218 ADDRESS_EXPR is an operand of the instruction to be used with
7219 RELOC_TYPE. */
a4e06468
RS
7220
7221static enum append_method
e407c74b 7222get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7223 bfd_reloc_code_real_type *reloc_type)
a4e06468 7224{
a4e06468
RS
7225 /* The relaxed version of a macro sequence must be inherently
7226 hazard-free. */
7227 if (mips_relax.sequence == 2)
7228 return APPEND_ADD;
7229
3b821a28 7230 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7231 if (mips_opts.noreorder)
7232 return APPEND_ADD;
7233
7234 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7235 if (delayed_branch_p (ip))
a4e06468 7236 {
e407c74b
NC
7237 if (!branch_likely_p (ip)
7238 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7239 return APPEND_SWAP;
7240
7241 if (mips_opts.mips16
7242 && ISA_SUPPORTS_MIPS16E
fc76e730 7243 && gpr_read_mask (ip) != 0)
a4e06468
RS
7244 return APPEND_ADD_COMPACT;
7245
7bd374a4
MR
7246 if (mips_opts.micromips
7247 && ((ip->insn_opcode & 0xffe0) == 0x4580
7248 || (!forced_insn_length
7249 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7250 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7251 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7252 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7253 return APPEND_ADD_COMPACT;
7254
a4e06468
RS
7255 return APPEND_ADD_WITH_NOP;
7256 }
7257
a4e06468
RS
7258 return APPEND_ADD;
7259}
7260
7bd374a4
MR
7261/* IP is an instruction whose opcode we have just changed, END points
7262 to the end of the opcode table processed. Point IP->insn_mo to the
7263 new opcode's definition. */
ceb94aa5
RS
7264
7265static void
7bd374a4 7266find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7267{
7bd374a4 7268 const struct mips_opcode *mo;
ceb94aa5 7269
ceb94aa5 7270 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7271 if (mo->pinfo != INSN_MACRO
7272 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7273 {
7274 ip->insn_mo = mo;
7275 return;
7276 }
7277 abort ();
7278}
7279
7bd374a4
MR
7280/* IP is a MIPS16 instruction whose opcode we have just changed.
7281 Point IP->insn_mo to the new opcode's definition. */
7282
7283static void
7284find_altered_mips16_opcode (struct mips_cl_insn *ip)
7285{
7286 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7287}
7288
7289/* IP is a microMIPS instruction whose opcode we have just changed.
7290 Point IP->insn_mo to the new opcode's definition. */
7291
7292static void
7293find_altered_micromips_opcode (struct mips_cl_insn *ip)
7294{
7295 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7296}
7297
df58fc94
RS
7298/* For microMIPS macros, we need to generate a local number label
7299 as the target of branches. */
7300#define MICROMIPS_LABEL_CHAR '\037'
7301static unsigned long micromips_target_label;
7302static char micromips_target_name[32];
7303
7304static char *
7305micromips_label_name (void)
7306{
7307 char *p = micromips_target_name;
7308 char symbol_name_temporary[24];
7309 unsigned long l;
7310 int i;
7311
7312 if (*p)
7313 return p;
7314
7315 i = 0;
7316 l = micromips_target_label;
7317#ifdef LOCAL_LABEL_PREFIX
7318 *p++ = LOCAL_LABEL_PREFIX;
7319#endif
7320 *p++ = 'L';
7321 *p++ = MICROMIPS_LABEL_CHAR;
7322 do
7323 {
7324 symbol_name_temporary[i++] = l % 10 + '0';
7325 l /= 10;
7326 }
7327 while (l != 0);
7328 while (i > 0)
7329 *p++ = symbol_name_temporary[--i];
7330 *p = '\0';
7331
7332 return micromips_target_name;
7333}
7334
7335static void
7336micromips_label_expr (expressionS *label_expr)
7337{
7338 label_expr->X_op = O_symbol;
7339 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7340 label_expr->X_add_number = 0;
7341}
7342
7343static void
7344micromips_label_inc (void)
7345{
7346 micromips_target_label++;
7347 *micromips_target_name = '\0';
7348}
7349
7350static void
7351micromips_add_label (void)
7352{
7353 symbolS *s;
7354
7355 s = colon (micromips_label_name ());
7356 micromips_label_inc ();
f3ded42a 7357 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7358}
7359
7360/* If assembling microMIPS code, then return the microMIPS reloc
7361 corresponding to the requested one if any. Otherwise return
7362 the reloc unchanged. */
7363
7364static bfd_reloc_code_real_type
7365micromips_map_reloc (bfd_reloc_code_real_type reloc)
7366{
7367 static const bfd_reloc_code_real_type relocs[][2] =
7368 {
7369 /* Keep sorted incrementally by the left-hand key. */
7370 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7371 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7372 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7373 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7374 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7375 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7376 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7377 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7378 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7379 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7380 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7381 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7382 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7383 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7384 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7385 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7386 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7387 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7388 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7389 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7390 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7391 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7392 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7393 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7394 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7395 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7396 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7397 };
7398 bfd_reloc_code_real_type r;
7399 size_t i;
7400
7401 if (!mips_opts.micromips)
7402 return reloc;
7403 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7404 {
7405 r = relocs[i][0];
7406 if (r > reloc)
7407 return reloc;
7408 if (r == reloc)
7409 return relocs[i][1];
7410 }
7411 return reloc;
7412}
7413
b886a2ab
RS
7414/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7415 Return true on success, storing the resolved value in RESULT. */
7416
5b7c81bd 7417static bool
b886a2ab
RS
7418calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7419 offsetT *result)
7420{
7421 switch (reloc)
7422 {
7423 case BFD_RELOC_MIPS_HIGHEST:
7424 case BFD_RELOC_MICROMIPS_HIGHEST:
7425 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
5b7c81bd 7426 return true;
b886a2ab
RS
7427
7428 case BFD_RELOC_MIPS_HIGHER:
7429 case BFD_RELOC_MICROMIPS_HIGHER:
7430 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
5b7c81bd 7431 return true;
b886a2ab
RS
7432
7433 case BFD_RELOC_HI16_S:
41947d9e 7434 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7435 case BFD_RELOC_MICROMIPS_HI16_S:
7436 case BFD_RELOC_MIPS16_HI16_S:
7437 *result = ((operand + 0x8000) >> 16) & 0xffff;
5b7c81bd 7438 return true;
b886a2ab
RS
7439
7440 case BFD_RELOC_HI16:
7441 case BFD_RELOC_MICROMIPS_HI16:
7442 case BFD_RELOC_MIPS16_HI16:
7443 *result = (operand >> 16) & 0xffff;
5b7c81bd 7444 return true;
b886a2ab
RS
7445
7446 case BFD_RELOC_LO16:
41947d9e 7447 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7448 case BFD_RELOC_MICROMIPS_LO16:
7449 case BFD_RELOC_MIPS16_LO16:
7450 *result = operand & 0xffff;
5b7c81bd 7451 return true;
b886a2ab
RS
7452
7453 case BFD_RELOC_UNUSED:
7454 *result = operand;
5b7c81bd 7455 return true;
b886a2ab
RS
7456
7457 default:
5b7c81bd 7458 return false;
b886a2ab
RS
7459 }
7460}
7461
71400594
RS
7462/* Output an instruction. IP is the instruction information.
7463 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7464 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7465 a macro expansion. */
71400594
RS
7466
7467static void
7468append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
5b7c81bd 7469 bfd_reloc_code_real_type *reloc_type, bool expansionp)
71400594 7470{
14fe068b 7471 unsigned long prev_pinfo2, pinfo;
5b7c81bd 7472 bool relaxed_branch = false;
a4e06468 7473 enum append_method method;
5b7c81bd 7474 bool relax32;
2b0c8b40 7475 int branch_disp;
71400594 7476
2309ddf2 7477 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7478 fix_loongson2f (ip);
7479
6f2117ba
PH
7480 ip->target[0] = '\0';
7481 if (offset_expr.X_op == O_symbol)
7482 strncpy (ip->target, S_GET_NAME (offset_expr.X_add_symbol), 15);
7483 ip->label[0] = '\0';
7484 if (seg_info (now_seg)->label_list)
7485 strncpy (ip->label, S_GET_NAME (seg_info (now_seg)->label_list->label), 15);
7486 if (mips_fix_loongson3_llsc && !HAVE_CODE_COMPRESSION)
7487 fix_loongson3_llsc (ip);
7488
738f4d98 7489 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7490 file_ase_micromips |= mips_opts.micromips;
738f4d98 7491
df58fc94 7492 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7493 pinfo = ip->insn_mo->pinfo;
df58fc94 7494
7bd374a4
MR
7495 /* Don't raise alarm about `nods' frags as they'll fill in the right
7496 kind of nop in relaxation if required. */
df58fc94
RS
7497 if (mips_opts.micromips
7498 && !expansionp
7bd374a4
MR
7499 && !(history[0].frag
7500 && history[0].frag->fr_type == rs_machine_dependent
7501 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7502 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7503 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7504 && micromips_insn_length (ip->insn_mo) != 2)
7505 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7506 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7507 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7508 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7509
15be625d
CM
7510 if (address_expr == NULL)
7511 ip->complete_p = 1;
b886a2ab
RS
7512 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7513 && reloc_type[1] == BFD_RELOC_UNUSED
7514 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7515 && address_expr->X_op == O_constant)
7516 {
15be625d
CM
7517 switch (*reloc_type)
7518 {
15be625d 7519 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7520 {
7521 int shift;
7522
17c6c9d9
MR
7523 /* Shift is 2, unusually, for microMIPS JALX. */
7524 shift = (mips_opts.micromips
7525 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7526 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7527 as_bad (_("jump to misaligned address (0x%lx)"),
7528 (unsigned long) address_expr->X_add_number);
7529 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7530 & 0x3ffffff);
335574df 7531 ip->complete_p = 1;
df58fc94 7532 }
15be625d
CM
7533 break;
7534
7535 case BFD_RELOC_MIPS16_JMP:
7536 if ((address_expr->X_add_number & 3) != 0)
7537 as_bad (_("jump to misaligned address (0x%lx)"),
7538 (unsigned long) address_expr->X_add_number);
7539 ip->insn_opcode |=
7540 (((address_expr->X_add_number & 0x7c0000) << 3)
7541 | ((address_expr->X_add_number & 0xf800000) >> 7)
7542 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7543 ip->complete_p = 1;
15be625d
CM
7544 break;
7545
7546 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7547 {
7548 int shift;
7549
7550 shift = mips_opts.micromips ? 1 : 2;
7551 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7552 as_bad (_("branch to misaligned address (0x%lx)"),
7553 (unsigned long) address_expr->X_add_number);
7554 if (!mips_relax_branch)
7555 {
7556 if ((address_expr->X_add_number + (1 << (shift + 15)))
7557 & ~((1 << (shift + 16)) - 1))
7558 as_bad (_("branch address range overflow (0x%lx)"),
7559 (unsigned long) address_expr->X_add_number);
7560 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7561 & 0xffff);
7562 }
df58fc94 7563 }
15be625d
CM
7564 break;
7565
7361da2c
AB
7566 case BFD_RELOC_MIPS_21_PCREL_S2:
7567 {
7568 int shift;
7569
7570 shift = 2;
7571 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7572 as_bad (_("branch to misaligned address (0x%lx)"),
7573 (unsigned long) address_expr->X_add_number);
7574 if ((address_expr->X_add_number + (1 << (shift + 20)))
7575 & ~((1 << (shift + 21)) - 1))
7576 as_bad (_("branch address range overflow (0x%lx)"),
7577 (unsigned long) address_expr->X_add_number);
7578 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7579 & 0x1fffff);
7580 }
7581 break;
7582
7583 case BFD_RELOC_MIPS_26_PCREL_S2:
7584 {
7585 int shift;
7586
7587 shift = 2;
7588 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7589 as_bad (_("branch to misaligned address (0x%lx)"),
7590 (unsigned long) address_expr->X_add_number);
7591 if ((address_expr->X_add_number + (1 << (shift + 25)))
7592 & ~((1 << (shift + 26)) - 1))
7593 as_bad (_("branch address range overflow (0x%lx)"),
7594 (unsigned long) address_expr->X_add_number);
7595 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7596 & 0x3ffffff);
7597 }
7598 break;
7599
15be625d 7600 default:
b886a2ab
RS
7601 {
7602 offsetT value;
7603
7604 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7605 &value))
7606 {
7607 ip->insn_opcode |= value & 0xffff;
7608 ip->complete_p = 1;
7609 }
7610 }
7611 break;
7612 }
15be625d
CM
7613 }
7614
71400594
RS
7615 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7616 {
7617 /* There are a lot of optimizations we could do that we don't.
7618 In particular, we do not, in general, reorder instructions.
7619 If you use gcc with optimization, it will reorder
7620 instructions and generally do much more optimization then we
7621 do here; repeating all that work in the assembler would only
7622 benefit hand written assembly code, and does not seem worth
7623 it. */
7624 int nops = (mips_optimize == 0
932d1a1b
RS
7625 ? nops_for_insn (0, history, NULL)
7626 : nops_for_insn_or_target (0, history, ip));
71400594 7627 if (nops > 0)
252b5132
RH
7628 {
7629 fragS *old_frag;
7630 unsigned long old_frag_offset;
7631 int i;
252b5132
RH
7632
7633 old_frag = frag_now;
7634 old_frag_offset = frag_now_fix ();
7635
7636 for (i = 0; i < nops; i++)
14fe068b
RS
7637 add_fixed_insn (NOP_INSN);
7638 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7639
7640 if (listing)
7641 {
7642 listing_prev_line ();
7643 /* We may be at the start of a variant frag. In case we
7644 are, make sure there is enough space for the frag
7645 after the frags created by listing_prev_line. The
7646 argument to frag_grow here must be at least as large
7647 as the argument to all other calls to frag_grow in
7648 this file. We don't have to worry about being in the
7649 middle of a variant frag, because the variants insert
7650 all needed nop instructions themselves. */
7651 frag_grow (40);
7652 }
7653
462427c4 7654 mips_move_text_labels ();
252b5132
RH
7655
7656#ifndef NO_ECOFF_DEBUGGING
7657 if (ECOFF_DEBUGGING)
7658 ecoff_fix_loc (old_frag, old_frag_offset);
7659#endif
7660 }
71400594
RS
7661 }
7662 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7663 {
932d1a1b
RS
7664 int nops;
7665
7666 /* Work out how many nops in prev_nop_frag are needed by IP,
7667 ignoring hazards generated by the first prev_nop_frag_since
7668 instructions. */
7669 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7670 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7671
71400594
RS
7672 /* Enforce NOPS as a minimum. */
7673 if (nops > prev_nop_frag_required)
7674 prev_nop_frag_required = nops;
252b5132 7675
71400594
RS
7676 if (prev_nop_frag_holds == prev_nop_frag_required)
7677 {
7678 /* Settle for the current number of nops. Update the history
7679 accordingly (for the benefit of any future .set reorder code). */
7680 prev_nop_frag = NULL;
7681 insert_into_history (prev_nop_frag_since,
7682 prev_nop_frag_holds, NOP_INSN);
7683 }
7684 else
7685 {
7686 /* Allow this instruction to replace one of the nops that was
7687 tentatively added to prev_nop_frag. */
df58fc94 7688 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7689 prev_nop_frag_holds--;
7690 prev_nop_frag_since++;
252b5132
RH
7691 }
7692 }
7693
e407c74b 7694 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7695 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7696
e410add4
RS
7697 dwarf2_emit_insn (0);
7698 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7699 so "move" the instruction address accordingly.
7700
7701 Also, it doesn't seem appropriate for the assembler to reorder .loc
7702 entries. If this instruction is a branch that we are going to swap
7703 with the previous instruction, the two instructions should be
7704 treated as a unit, and the debug information for both instructions
7705 should refer to the start of the branch sequence. Using the
7706 current position is certainly wrong when swapping a 32-bit branch
7707 and a 16-bit delay slot, since the current position would then be
7708 in the middle of a branch. */
7709 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7710
df58fc94
RS
7711 relax32 = (mips_relax_branch
7712 /* Don't try branch relaxation within .set nomacro, or within
7713 .set noat if we use $at for PIC computations. If it turns
7714 out that the branch was out-of-range, we'll get an error. */
7715 && !mips_opts.warn_about_macros
7716 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7717 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7718 as they have no complementing branches. */
7719 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7720
7721 if (!HAVE_CODE_COMPRESSION
7722 && address_expr
7723 && relax32
0b25d3e6 7724 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7725 && delayed_branch_p (ip))
4a6a3df4 7726 {
5b7c81bd 7727 relaxed_branch = true;
1e915849
RS
7728 add_relaxed_insn (ip, (relaxed_branch_length
7729 (NULL, NULL,
11625dd8
RS
7730 uncond_branch_p (ip) ? -1
7731 : branch_likely_p (ip) ? 1
1e915849
RS
7732 : 0)), 4,
7733 RELAX_BRANCH_ENCODE
ce8ad872 7734 (AT, mips_pic != NO_PIC,
11625dd8
RS
7735 uncond_branch_p (ip),
7736 branch_likely_p (ip),
1e915849
RS
7737 pinfo & INSN_WRITE_GPR_31,
7738 0),
7739 address_expr->X_add_symbol,
7740 address_expr->X_add_number);
4a6a3df4
AO
7741 *reloc_type = BFD_RELOC_UNUSED;
7742 }
df58fc94
RS
7743 else if (mips_opts.micromips
7744 && address_expr
7745 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7746 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7747 && (delayed_branch_p (ip) || compact_branch_p (ip))
7748 /* Don't try branch relaxation when users specify
7749 16-bit/32-bit instructions. */
7750 && !forced_insn_length)
df58fc94 7751 {
5b7c81bd
AM
7752 bool relax16 = (method != APPEND_ADD_COMPACT
7753 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7754 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7755 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7756 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7757 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7758 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7759 int length32 = nods ? 8 : 4;
df58fc94
RS
7760
7761 gas_assert (address_expr != NULL);
7762 gas_assert (!mips_relax.sequence);
7763
5b7c81bd 7764 relaxed_branch = true;
7bd374a4
MR
7765 if (nods)
7766 method = APPEND_ADD;
7767 if (relax32)
7768 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7769 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7770 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7771 mips_pic != NO_PIC,
7bd374a4 7772 uncond, compact, al, nods,
40209cad 7773 relax32, 0, 0),
df58fc94
RS
7774 address_expr->X_add_symbol,
7775 address_expr->X_add_number);
7776 *reloc_type = BFD_RELOC_UNUSED;
7777 }
7778 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7779 {
5b7c81bd
AM
7780 bool require_unextended;
7781 bool require_extended;
88a7ef16
MR
7782 symbolS *symbol;
7783 offsetT offset;
7784
7fd53920
MR
7785 if (forced_insn_length != 0)
7786 {
7787 require_unextended = forced_insn_length == 2;
7788 require_extended = forced_insn_length == 4;
7789 }
7790 else
7791 {
7792 require_unextended = (mips_opts.noautoextend
7793 && !mips_opcode_32bit_p (ip->insn_mo));
7794 require_extended = 0;
7795 }
7796
252b5132 7797 /* We need to set up a variant frag. */
df58fc94 7798 gas_assert (address_expr != NULL);
88a7ef16
MR
7799 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7800 symbol created by `make_expr_symbol' may not get a necessary
7801 external relocation produced. */
7802 if (address_expr->X_op == O_symbol)
7803 {
7804 symbol = address_expr->X_add_symbol;
7805 offset = address_expr->X_add_number;
7806 }
7807 else
7808 {
7809 symbol = make_expr_symbol (address_expr);
82d808ed 7810 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7811 offset = 0;
7812 }
8507b6e7 7813 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7814 RELAX_MIPS16_ENCODE
7815 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7816 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7817 mips_pic != NO_PIC,
7818 HAVE_32BIT_SYMBOLS,
7819 mips_opts.warn_about_macros,
7fd53920 7820 require_unextended, require_extended,
11625dd8 7821 delayed_branch_p (&history[0]),
1e915849 7822 history[0].mips16_absolute_jump_p),
88a7ef16 7823 symbol, offset);
252b5132 7824 }
5c04167a 7825 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7826 {
11625dd8 7827 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7828 /* Make sure there is enough room to swap this instruction with
7829 a following jump instruction. */
7830 frag_grow (6);
1e915849 7831 add_fixed_insn (ip);
252b5132
RH
7832 }
7833 else
7834 {
7835 if (mips_opts.mips16
7836 && mips_opts.noreorder
11625dd8 7837 && delayed_branch_p (&history[0]))
252b5132
RH
7838 as_warn (_("extended instruction in delay slot"));
7839
4d7206a2
RS
7840 if (mips_relax.sequence)
7841 {
7842 /* If we've reached the end of this frag, turn it into a variant
7843 frag and record the information for the instructions we've
7844 written so far. */
7845 if (frag_room () < 4)
7846 relax_close_frag ();
df58fc94 7847 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7848 }
7849
584892a6 7850 if (mips_relax.sequence != 2)
df58fc94
RS
7851 {
7852 if (mips_macro_warning.first_insn_sizes[0] == 0)
7853 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7854 mips_macro_warning.sizes[0] += insn_length (ip);
7855 mips_macro_warning.insns[0]++;
7856 }
584892a6 7857 if (mips_relax.sequence != 1)
df58fc94
RS
7858 {
7859 if (mips_macro_warning.first_insn_sizes[1] == 0)
7860 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7861 mips_macro_warning.sizes[1] += insn_length (ip);
7862 mips_macro_warning.insns[1]++;
7863 }
584892a6 7864
1e915849
RS
7865 if (mips_opts.mips16)
7866 {
7867 ip->fixed_p = 1;
7868 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7869 }
7870 add_fixed_insn (ip);
252b5132
RH
7871 }
7872
9fe77896 7873 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7874 {
df58fc94 7875 bfd_reloc_code_real_type final_type[3];
2309ddf2 7876 reloc_howto_type *howto0;
9fe77896
RS
7877 reloc_howto_type *howto;
7878 int i;
34ce925e 7879
df58fc94
RS
7880 /* Perform any necessary conversion to microMIPS relocations
7881 and find out how many relocations there actually are. */
7882 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7883 final_type[i] = micromips_map_reloc (reloc_type[i]);
7884
9fe77896
RS
7885 /* In a compound relocation, it is the final (outermost)
7886 operator that determines the relocated field. */
2309ddf2 7887 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7888 if (!howto)
7889 abort ();
2309ddf2
MR
7890
7891 if (i > 1)
7892 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7893 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7894 bfd_get_reloc_size (howto),
7895 address_expr,
2309ddf2
MR
7896 howto0 && howto0->pc_relative,
7897 final_type[0]);
ce8ad872
MR
7898 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7899 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7900
7901 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7902 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7903 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7904
7905 /* These relocations can have an addend that won't fit in
7906 4 octets for 64bit assembly. */
bad1aba3 7907 if (GPR_SIZE == 64
9fe77896
RS
7908 && ! howto->partial_inplace
7909 && (reloc_type[0] == BFD_RELOC_16
7910 || reloc_type[0] == BFD_RELOC_32
7911 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7912 || reloc_type[0] == BFD_RELOC_GPREL16
7913 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7914 || reloc_type[0] == BFD_RELOC_GPREL32
7915 || reloc_type[0] == BFD_RELOC_64
7916 || reloc_type[0] == BFD_RELOC_CTOR
7917 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7918 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7919 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7920 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
d712f276 7921 || reloc_type[0] == BFD_RELOC_MIPS_16
9fe77896
RS
7922 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7923 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7924 || hi16_reloc_p (reloc_type[0])
7925 || lo16_reloc_p (reloc_type[0])))
7926 ip->fixp[0]->fx_no_overflow = 1;
7927
ddaf2c41
MR
7928 /* These relocations can have an addend that won't fit in 2 octets. */
7929 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7930 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7931 ip->fixp[0]->fx_no_overflow = 1;
7932
9fe77896
RS
7933 if (mips_relax.sequence)
7934 {
7935 if (mips_relax.first_fixup == 0)
7936 mips_relax.first_fixup = ip->fixp[0];
7937 }
7938 else if (reloc_needs_lo_p (*reloc_type))
7939 {
7940 struct mips_hi_fixup *hi_fixup;
7941
7942 /* Reuse the last entry if it already has a matching %lo. */
7943 hi_fixup = mips_hi_fixup_list;
7944 if (hi_fixup == 0
7945 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7946 {
325801bd 7947 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7948 hi_fixup->next = mips_hi_fixup_list;
7949 mips_hi_fixup_list = hi_fixup;
4d7206a2 7950 }
9fe77896
RS
7951 hi_fixup->fixp = ip->fixp[0];
7952 hi_fixup->seg = now_seg;
7953 }
252b5132 7954
9fe77896
RS
7955 /* Add fixups for the second and third relocations, if given.
7956 Note that the ABI allows the second relocation to be
7957 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7958 moment we only use RSS_UNDEF, but we could add support
7959 for the others if it ever becomes necessary. */
7960 for (i = 1; i < 3; i++)
7961 if (reloc_type[i] != BFD_RELOC_UNUSED)
7962 {
7963 ip->fixp[i] = fix_new (ip->frag, ip->where,
7964 ip->fixp[0]->fx_size, NULL, 0,
5b7c81bd 7965 false, final_type[i]);
f6688943 7966
9fe77896
RS
7967 /* Use fx_tcbit to mark compound relocs. */
7968 ip->fixp[0]->fx_tcbit = 1;
7969 ip->fixp[i]->fx_tcbit = 1;
7970 }
252b5132 7971 }
252b5132
RH
7972
7973 /* Update the register mask information. */
4c260379
RS
7974 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7975 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7976
a4e06468 7977 switch (method)
252b5132 7978 {
a4e06468
RS
7979 case APPEND_ADD:
7980 insert_into_history (0, 1, ip);
7981 break;
7982
7983 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7984 {
7985 struct mips_cl_insn *nop;
7986
7987 insert_into_history (0, 1, ip);
7988 nop = get_delay_slot_nop (ip);
7989 add_fixed_insn (nop);
7990 insert_into_history (0, 1, nop);
7991 if (mips_relax.sequence)
7992 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7993 }
a4e06468
RS
7994 break;
7995
7996 case APPEND_ADD_COMPACT:
7997 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7998 if (mips_opts.mips16)
7999 {
8000 ip->insn_opcode |= 0x0080;
8001 find_altered_mips16_opcode (ip);
8002 }
8003 /* Convert microMIPS instructions. */
8004 else if (mips_opts.micromips)
8005 {
8006 /* jr16->jrc */
8007 if ((ip->insn_opcode & 0xffe0) == 0x4580)
8008 ip->insn_opcode |= 0x0020;
8009 /* b16->bc */
8010 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
8011 ip->insn_opcode = 0x40e00000;
8012 /* beqz16->beqzc, bnez16->bnezc */
8013 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
8014 {
8015 unsigned long regno;
8016
8017 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
8018 regno &= MICROMIPSOP_MASK_MD;
8019 regno = micromips_to_32_reg_d_map[regno];
8020 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
8021 | (regno << MICROMIPSOP_SH_RS)
8022 | 0x40a00000) ^ 0x00400000;
8023 }
8024 /* beqz->beqzc, bnez->bnezc */
8025 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
8026 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
8027 | ((ip->insn_opcode >> 7) & 0x00400000)
8028 | 0x40a00000) ^ 0x00400000;
8029 /* beq $0->beqzc, bne $0->bnezc */
8030 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
8031 ip->insn_opcode = (((ip->insn_opcode >>
8032 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
8033 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
8034 | ((ip->insn_opcode >> 7) & 0x00400000)
8035 | 0x40a00000) ^ 0x00400000;
8036 else
8037 abort ();
8038 find_altered_micromips_opcode (ip);
8039 }
8040 else
8041 abort ();
a4e06468
RS
8042 install_insn (ip);
8043 insert_into_history (0, 1, ip);
8044 break;
8045
8046 case APPEND_SWAP:
8047 {
8048 struct mips_cl_insn delay = history[0];
99e7978b
MF
8049
8050 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
8051 {
8052 /* Add the delay slot instruction to the end of the
8053 current frag and shrink the fixed part of the
8054 original frag. If the branch occupies the tail of
8055 the latter, move it backwards to cover the gap. */
2b0c8b40 8056 delay.frag->fr_fix -= branch_disp;
a4e06468 8057 if (delay.frag == ip->frag)
2b0c8b40 8058 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
8059 add_fixed_insn (&delay);
8060 }
8061 else
8062 {
5e35670b
MR
8063 /* If this is not a relaxed branch and we are in the
8064 same frag, then just swap the instructions. */
8065 move_insn (ip, delay.frag, delay.where);
8066 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
8067 }
8068 history[0] = *ip;
8069 delay.fixed_p = 1;
8070 insert_into_history (0, 1, &delay);
8071 }
8072 break;
252b5132
RH
8073 }
8074
13408f1e 8075 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
8076 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
8077 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
8078 {
8079 unsigned int i;
8080
79850f26 8081 mips_no_prev_insn ();
13408f1e 8082
e407c74b 8083 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 8084 history[i].cleared_p = 1;
e407c74b
NC
8085 }
8086
df58fc94
RS
8087 /* We need to emit a label at the end of branch-likely macros. */
8088 if (emit_branch_likely_macro)
8089 {
5b7c81bd 8090 emit_branch_likely_macro = false;
df58fc94
RS
8091 micromips_add_label ();
8092 }
8093
252b5132
RH
8094 /* We just output an insn, so the next one doesn't have a label. */
8095 mips_clear_insn_labels ();
252b5132
RH
8096}
8097
e407c74b
NC
8098/* Forget that there was any previous instruction or label.
8099 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
8100
8101static void
7d10b47d 8102mips_no_prev_insn (void)
252b5132 8103{
7d10b47d
RS
8104 prev_nop_frag = NULL;
8105 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
8106 mips_clear_insn_labels ();
8107}
8108
7d10b47d
RS
8109/* This function must be called before we emit something other than
8110 instructions. It is like mips_no_prev_insn except that it inserts
8111 any NOPS that might be needed by previous instructions. */
252b5132 8112
7d10b47d
RS
8113void
8114mips_emit_delays (void)
252b5132
RH
8115{
8116 if (! mips_opts.noreorder)
8117 {
932d1a1b 8118 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
8119 if (nops > 0)
8120 {
7d10b47d
RS
8121 while (nops-- > 0)
8122 add_fixed_insn (NOP_INSN);
462427c4 8123 mips_move_text_labels ();
7d10b47d
RS
8124 }
8125 }
8126 mips_no_prev_insn ();
8127}
8128
8129/* Start a (possibly nested) noreorder block. */
8130
8131static void
8132start_noreorder (void)
8133{
8134 if (mips_opts.noreorder == 0)
8135 {
8136 unsigned int i;
8137 int nops;
8138
8139 /* None of the instructions before the .set noreorder can be moved. */
8140 for (i = 0; i < ARRAY_SIZE (history); i++)
8141 history[i].fixed_p = 1;
8142
8143 /* Insert any nops that might be needed between the .set noreorder
8144 block and the previous instructions. We will later remove any
8145 nops that turn out not to be needed. */
932d1a1b 8146 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
8147 if (nops > 0)
8148 {
8149 if (mips_optimize != 0)
252b5132
RH
8150 {
8151 /* Record the frag which holds the nop instructions, so
8152 that we can remove them if we don't need them. */
df58fc94 8153 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
8154 prev_nop_frag = frag_now;
8155 prev_nop_frag_holds = nops;
8156 prev_nop_frag_required = 0;
8157 prev_nop_frag_since = 0;
8158 }
8159
8160 for (; nops > 0; --nops)
1e915849 8161 add_fixed_insn (NOP_INSN);
252b5132 8162
7d10b47d
RS
8163 /* Move on to a new frag, so that it is safe to simply
8164 decrease the size of prev_nop_frag. */
8165 frag_wane (frag_now);
8166 frag_new (0);
462427c4 8167 mips_move_text_labels ();
252b5132 8168 }
df58fc94 8169 mips_mark_labels ();
7d10b47d 8170 mips_clear_insn_labels ();
252b5132 8171 }
7d10b47d
RS
8172 mips_opts.noreorder++;
8173 mips_any_noreorder = 1;
8174}
252b5132 8175
7d10b47d 8176/* End a nested noreorder block. */
252b5132 8177
7d10b47d
RS
8178static void
8179end_noreorder (void)
8180{
8181 mips_opts.noreorder--;
8182 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
8183 {
8184 /* Commit to inserting prev_nop_frag_required nops and go back to
8185 handling nop insertion the .set reorder way. */
8186 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 8187 * NOP_INSN_SIZE);
7d10b47d
RS
8188 insert_into_history (prev_nop_frag_since,
8189 prev_nop_frag_required, NOP_INSN);
8190 prev_nop_frag = NULL;
8191 }
252b5132
RH
8192}
8193
97d87491
RS
8194/* Sign-extend 32-bit mode constants that have bit 31 set and all
8195 higher bits unset. */
8196
8197static void
8198normalize_constant_expr (expressionS *ex)
8199{
8200 if (ex->X_op == O_constant
8201 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8202 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8203 - 0x80000000);
8204}
8205
8206/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8207 all higher bits unset. */
8208
8209static void
8210normalize_address_expr (expressionS *ex)
8211{
8212 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8213 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8214 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8215 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8216 - 0x80000000);
8217}
8218
8219/* Try to match TOKENS against OPCODE, storing the result in INSN.
8220 Return true if the match was successful.
8221
8222 OPCODE_EXTRA is a value that should be ORed into the opcode
8223 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8224 there are more alternatives after OPCODE and SOFT_MATCH is
8225 as for mips_arg_info. */
8226
5b7c81bd 8227static bool
97d87491
RS
8228match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8229 struct mips_operand_token *tokens, unsigned int opcode_extra,
5b7c81bd 8230 bool lax_match, bool complete_p)
97d87491
RS
8231{
8232 const char *args;
8233 struct mips_arg_info arg;
8234 const struct mips_operand *operand;
8235 char c;
8236
8237 imm_expr.X_op = O_absent;
97d87491
RS
8238 offset_expr.X_op = O_absent;
8239 offset_reloc[0] = BFD_RELOC_UNUSED;
8240 offset_reloc[1] = BFD_RELOC_UNUSED;
8241 offset_reloc[2] = BFD_RELOC_UNUSED;
8242
8243 create_insn (insn, opcode);
60f20e8b
RS
8244 /* When no opcode suffix is specified, assume ".xyzw". */
8245 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8246 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8247 else
8248 insn->insn_opcode |= opcode_extra;
97d87491
RS
8249 memset (&arg, 0, sizeof (arg));
8250 arg.insn = insn;
8251 arg.token = tokens;
8252 arg.argnum = 1;
8253 arg.last_regno = ILLEGAL_REG;
8254 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8255 arg.lax_match = lax_match;
97d87491
RS
8256 for (args = opcode->args;; ++args)
8257 {
8258 if (arg.token->type == OT_END)
8259 {
8260 /* Handle unary instructions in which only one operand is given.
8261 The source is then the same as the destination. */
8262 if (arg.opnum == 1 && *args == ',')
8263 {
8264 operand = (mips_opts.micromips
8265 ? decode_micromips_operand (args + 1)
8266 : decode_mips_operand (args + 1));
8267 if (operand && mips_optional_operand_p (operand))
8268 {
8269 arg.token = tokens;
8270 arg.argnum = 1;
8271 continue;
8272 }
8273 }
8274
8275 /* Treat elided base registers as $0. */
8276 if (strcmp (args, "(b)") == 0)
8277 args += 3;
8278
8279 if (args[0] == '+')
8280 switch (args[1])
8281 {
8282 case 'K':
8283 case 'N':
8284 /* The register suffix is optional. */
8285 args += 2;
8286 break;
8287 }
8288
8289 /* Fail the match if there were too few operands. */
8290 if (*args)
5b7c81bd 8291 return false;
97d87491
RS
8292
8293 /* Successful match. */
60f20e8b 8294 if (!complete_p)
5b7c81bd 8295 return true;
e3de51ce 8296 clear_insn_error ();
97d87491 8297 if (arg.dest_regno == arg.last_regno
d34049e8 8298 && startswith (insn->insn_mo->name, "jalr"))
97d87491
RS
8299 {
8300 if (arg.opnum == 2)
e3de51ce 8301 set_insn_error
1661c76c 8302 (0, _("source and destination must be different"));
97d87491 8303 else if (arg.last_regno == 31)
e3de51ce 8304 set_insn_error
1661c76c 8305 (0, _("a destination register must be supplied"));
97d87491 8306 }
173d3447 8307 else if (arg.last_regno == 31
d34049e8
ML
8308 && (startswith (insn->insn_mo->name, "bltzal")
8309 || startswith (insn->insn_mo->name, "bgezal")))
173d3447 8310 set_insn_error (0, _("the source register must not be $31"));
97d87491 8311 check_completed_insn (&arg);
5b7c81bd 8312 return true;
97d87491
RS
8313 }
8314
8315 /* Fail the match if the line has too many operands. */
8316 if (*args == 0)
5b7c81bd 8317 return false;
97d87491
RS
8318
8319 /* Handle characters that need to match exactly. */
8320 if (*args == '(' || *args == ')' || *args == ',')
8321 {
8322 if (match_char (&arg, *args))
8323 continue;
5b7c81bd 8324 return false;
97d87491
RS
8325 }
8326 if (*args == '#')
8327 {
8328 ++args;
8329 if (arg.token->type == OT_DOUBLE_CHAR
8330 && arg.token->u.ch == *args)
8331 {
8332 ++arg.token;
8333 continue;
8334 }
5b7c81bd 8335 return false;
97d87491
RS
8336 }
8337
8338 /* Handle special macro operands. Work out the properties of
8339 other operands. */
8340 arg.opnum += 1;
97d87491
RS
8341 switch (*args)
8342 {
7361da2c
AB
8343 case '-':
8344 switch (args[1])
8345 {
8346 case 'A':
8347 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8348 break;
8349
8350 case 'B':
8351 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8352 break;
8353 }
8354 break;
8355
97d87491
RS
8356 case '+':
8357 switch (args[1])
8358 {
97d87491
RS
8359 case 'i':
8360 *offset_reloc = BFD_RELOC_MIPS_JMP;
8361 break;
7361da2c
AB
8362
8363 case '\'':
8364 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8365 break;
8366
8367 case '\"':
8368 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8369 break;
97d87491
RS
8370 }
8371 break;
8372
97d87491 8373 case 'I':
1a00e612 8374 if (!match_const_int (&arg, &imm_expr.X_add_number))
5b7c81bd 8375 return false;
1a00e612 8376 imm_expr.X_op = O_constant;
bad1aba3 8377 if (GPR_SIZE == 32)
97d87491
RS
8378 normalize_constant_expr (&imm_expr);
8379 continue;
8380
8381 case 'A':
8382 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8383 {
8384 /* Assume that the offset has been elided and that what
8385 we saw was a base register. The match will fail later
8386 if that assumption turns out to be wrong. */
8387 offset_expr.X_op = O_constant;
8388 offset_expr.X_add_number = 0;
8389 }
97d87491 8390 else
1a00e612
RS
8391 {
8392 if (!match_expression (&arg, &offset_expr, offset_reloc))
5b7c81bd 8393 return false;
1a00e612
RS
8394 normalize_address_expr (&offset_expr);
8395 }
97d87491
RS
8396 continue;
8397
8398 case 'F':
8399 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8400 8, true))
8401 return false;
97d87491
RS
8402 continue;
8403
8404 case 'L':
8405 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8406 8, false))
8407 return false;
97d87491
RS
8408 continue;
8409
8410 case 'f':
8411 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8412 4, true))
8413 return false;
97d87491
RS
8414 continue;
8415
8416 case 'l':
8417 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
5b7c81bd
AM
8418 4, false))
8419 return false;
97d87491
RS
8420 continue;
8421
97d87491
RS
8422 case 'p':
8423 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8424 break;
8425
8426 case 'a':
8427 *offset_reloc = BFD_RELOC_MIPS_JMP;
8428 break;
8429
8430 case 'm':
8431 gas_assert (mips_opts.micromips);
8432 c = args[1];
8433 switch (c)
8434 {
8435 case 'D':
8436 case 'E':
8437 if (!forced_insn_length)
8438 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8439 else if (c == 'D')
8440 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8441 else
8442 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8443 break;
8444 }
8445 break;
8446 }
8447
8448 operand = (mips_opts.micromips
8449 ? decode_micromips_operand (args)
8450 : decode_mips_operand (args));
8451 if (!operand)
8452 abort ();
8453
8454 /* Skip prefixes. */
7361da2c 8455 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8456 args++;
8457
8458 if (mips_optional_operand_p (operand)
8459 && args[1] == ','
8460 && (arg.token[0].type != OT_REG
8461 || arg.token[1].type == OT_END))
8462 {
8463 /* Assume that the register has been elided and is the
8464 same as the first operand. */
8465 arg.token = tokens;
8466 arg.argnum = 1;
8467 }
8468
8469 if (!match_operand (&arg, operand))
5b7c81bd 8470 return false;
97d87491
RS
8471 }
8472}
8473
8474/* Like match_insn, but for MIPS16. */
8475
5b7c81bd 8476static bool
97d87491 8477match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8478 struct mips_operand_token *tokens)
97d87491
RS
8479{
8480 const char *args;
8481 const struct mips_operand *operand;
8482 const struct mips_operand *ext_operand;
5b7c81bd 8483 bool pcrel = false;
7fd53920 8484 int required_insn_length;
97d87491
RS
8485 struct mips_arg_info arg;
8486 int relax_char;
8487
7fd53920
MR
8488 if (forced_insn_length)
8489 required_insn_length = forced_insn_length;
8490 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8491 required_insn_length = 2;
8492 else
8493 required_insn_length = 0;
8494
97d87491
RS
8495 create_insn (insn, opcode);
8496 imm_expr.X_op = O_absent;
97d87491
RS
8497 offset_expr.X_op = O_absent;
8498 offset_reloc[0] = BFD_RELOC_UNUSED;
8499 offset_reloc[1] = BFD_RELOC_UNUSED;
8500 offset_reloc[2] = BFD_RELOC_UNUSED;
8501 relax_char = 0;
8502
8503 memset (&arg, 0, sizeof (arg));
8504 arg.insn = insn;
8505 arg.token = tokens;
8506 arg.argnum = 1;
8507 arg.last_regno = ILLEGAL_REG;
8508 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8509 relax_char = 0;
8510 for (args = opcode->args;; ++args)
8511 {
8512 int c;
8513
8514 if (arg.token->type == OT_END)
8515 {
8516 offsetT value;
8517
8518 /* Handle unary instructions in which only one operand is given.
8519 The source is then the same as the destination. */
8520 if (arg.opnum == 1 && *args == ',')
8521 {
5b7c81bd 8522 operand = decode_mips16_operand (args[1], false);
97d87491
RS
8523 if (operand && mips_optional_operand_p (operand))
8524 {
8525 arg.token = tokens;
8526 arg.argnum = 1;
8527 continue;
8528 }
8529 }
8530
8531 /* Fail the match if there were too few operands. */
8532 if (*args)
5b7c81bd 8533 return false;
97d87491
RS
8534
8535 /* Successful match. Stuff the immediate value in now, if
8536 we can. */
e3de51ce 8537 clear_insn_error ();
97d87491
RS
8538 if (opcode->pinfo == INSN_MACRO)
8539 {
8540 gas_assert (relax_char == 0 || relax_char == 'p');
8541 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8542 }
8543 else if (relax_char
8544 && offset_expr.X_op == O_constant
82d808ed 8545 && !pcrel
97d87491
RS
8546 && calculate_reloc (*offset_reloc,
8547 offset_expr.X_add_number,
8548 &value))
8549 {
8550 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8551 required_insn_length, &insn->insn_opcode);
97d87491
RS
8552 offset_expr.X_op = O_absent;
8553 *offset_reloc = BFD_RELOC_UNUSED;
8554 }
8555 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8556 {
7fd53920 8557 if (required_insn_length == 2)
e3de51ce 8558 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8559 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8560 {
8561 forced_insn_length = 4;
8562 insn->insn_opcode |= MIPS16_EXTEND;
8563 }
97d87491
RS
8564 }
8565 else if (relax_char)
8566 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8567
8568 check_completed_insn (&arg);
5b7c81bd 8569 return true;
97d87491
RS
8570 }
8571
8572 /* Fail the match if the line has too many operands. */
8573 if (*args == 0)
5b7c81bd 8574 return false;
97d87491
RS
8575
8576 /* Handle characters that need to match exactly. */
8577 if (*args == '(' || *args == ')' || *args == ',')
8578 {
8579 if (match_char (&arg, *args))
8580 continue;
5b7c81bd 8581 return false;
97d87491
RS
8582 }
8583
8584 arg.opnum += 1;
8585 c = *args;
8586 switch (c)
8587 {
8588 case 'p':
8589 case 'q':
8590 case 'A':
8591 case 'B':
8592 case 'E':
25499ac7
MR
8593 case 'V':
8594 case 'u':
97d87491
RS
8595 relax_char = c;
8596 break;
8597
8598 case 'I':
1a00e612 8599 if (!match_const_int (&arg, &imm_expr.X_add_number))
5b7c81bd 8600 return false;
1a00e612 8601 imm_expr.X_op = O_constant;
bad1aba3 8602 if (GPR_SIZE == 32)
97d87491
RS
8603 normalize_constant_expr (&imm_expr);
8604 continue;
8605
8606 case 'a':
8607 case 'i':
8608 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8609 break;
8610 }
8611
7fd53920 8612 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8613 if (!operand)
8614 abort ();
8615
82d808ed 8616 if (operand->type == OP_PCREL)
5b7c81bd 8617 pcrel = true;
82d808ed 8618 else
97d87491 8619 {
5b7c81bd 8620 ext_operand = decode_mips16_operand (c, true);
97d87491
RS
8621 if (operand != ext_operand)
8622 {
8623 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8624 {
8625 offset_expr.X_op = O_constant;
8626 offset_expr.X_add_number = 0;
8627 relax_char = c;
8628 continue;
8629 }
8630
1a7bf198 8631 if (!match_expression (&arg, &offset_expr, offset_reloc))
5b7c81bd 8632 return false;
97d87491
RS
8633
8634 /* '8' is used for SLTI(U) and has traditionally not
8635 been allowed to take relocation operators. */
8636 if (offset_reloc[0] != BFD_RELOC_UNUSED
8637 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8638 {
8639 match_not_constant (&arg);
5b7c81bd 8640 return false;
e295202f 8641 }
97d87491 8642
c96425c5
MR
8643 if (offset_expr.X_op == O_big)
8644 {
8645 match_out_of_range (&arg);
5b7c81bd 8646 return false;
c96425c5
MR
8647 }
8648
97d87491
RS
8649 relax_char = c;
8650 continue;
8651 }
8652 }
8653
8654 if (mips_optional_operand_p (operand)
8655 && args[1] == ','
8656 && (arg.token[0].type != OT_REG
8657 || arg.token[1].type == OT_END))
8658 {
8659 /* Assume that the register has been elided and is the
8660 same as the first operand. */
8661 arg.token = tokens;
8662 arg.argnum = 1;
8663 }
8664
8665 if (!match_operand (&arg, operand))
5b7c81bd 8666 return false;
97d87491
RS
8667 }
8668}
8669
60f20e8b
RS
8670/* Record that the current instruction is invalid for the current ISA. */
8671
8672static void
8673match_invalid_for_isa (void)
8674{
8675 set_insn_error_ss
1661c76c 8676 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8677 mips_cpu_info_from_arch (mips_opts.arch)->name,
8678 mips_cpu_info_from_isa (mips_opts.isa)->name);
8679}
8680
8681/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8682 Return true if a definite match or failure was found, storing any match
8683 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8684 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8685 tried and failed to match under normal conditions and now want to try a
8686 more relaxed match. */
8687
5b7c81bd 8688static bool
60f20e8b
RS
8689match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8690 const struct mips_opcode *past, struct mips_operand_token *tokens,
5b7c81bd 8691 int opcode_extra, bool lax_match)
60f20e8b
RS
8692{
8693 const struct mips_opcode *opcode;
8694 const struct mips_opcode *invalid_delay_slot;
5b7c81bd 8695 bool seen_valid_for_isa, seen_valid_for_size;
60f20e8b
RS
8696
8697 /* Search for a match, ignoring alternatives that don't satisfy the
8698 current ISA or forced_length. */
8699 invalid_delay_slot = 0;
5b7c81bd
AM
8700 seen_valid_for_isa = false;
8701 seen_valid_for_size = false;
60f20e8b
RS
8702 opcode = first;
8703 do
8704 {
8705 gas_assert (strcmp (opcode->name, first->name) == 0);
8706 if (is_opcode_valid (opcode))
8707 {
5b7c81bd 8708 seen_valid_for_isa = true;
60f20e8b
RS
8709 if (is_size_valid (opcode))
8710 {
5b7c81bd 8711 bool delay_slot_ok;
60f20e8b 8712
5b7c81bd 8713 seen_valid_for_size = true;
60f20e8b
RS
8714 delay_slot_ok = is_delay_slot_valid (opcode);
8715 if (match_insn (insn, opcode, tokens, opcode_extra,
8716 lax_match, delay_slot_ok))
8717 {
8718 if (!delay_slot_ok)
8719 {
8720 if (!invalid_delay_slot)
8721 invalid_delay_slot = opcode;
8722 }
8723 else
5b7c81bd 8724 return true;
60f20e8b
RS
8725 }
8726 }
8727 }
8728 ++opcode;
8729 }
8730 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8731
8732 /* If the only matches we found had the wrong length for the delay slot,
8733 pick the first such match. We'll issue an appropriate warning later. */
8734 if (invalid_delay_slot)
8735 {
8736 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
5b7c81bd
AM
8737 lax_match, true))
8738 return true;
60f20e8b
RS
8739 abort ();
8740 }
8741
8742 /* Handle the case where we didn't try to match an instruction because
8743 all the alternatives were incompatible with the current ISA. */
8744 if (!seen_valid_for_isa)
8745 {
8746 match_invalid_for_isa ();
5b7c81bd 8747 return true;
60f20e8b
RS
8748 }
8749
8750 /* Handle the case where we didn't try to match an instruction because
8751 all the alternatives were of the wrong size. */
8752 if (!seen_valid_for_size)
8753 {
8754 if (mips_opts.insn32)
1661c76c 8755 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8756 else
8757 set_insn_error_i
1661c76c 8758 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b 8759 8 * forced_insn_length);
5b7c81bd 8760 return true;
60f20e8b
RS
8761 }
8762
5b7c81bd 8763 return false;
60f20e8b
RS
8764}
8765
8766/* Like match_insns, but for MIPS16. */
8767
5b7c81bd 8768static bool
60f20e8b
RS
8769match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8770 struct mips_operand_token *tokens)
8771{
8772 const struct mips_opcode *opcode;
5b7c81bd
AM
8773 bool seen_valid_for_isa;
8774 bool seen_valid_for_size;
60f20e8b
RS
8775
8776 /* Search for a match, ignoring alternatives that don't satisfy the
8777 current ISA. There are no separate entries for extended forms so
8778 we deal with forced_length later. */
5b7c81bd
AM
8779 seen_valid_for_isa = false;
8780 seen_valid_for_size = false;
60f20e8b
RS
8781 opcode = first;
8782 do
8783 {
8784 gas_assert (strcmp (opcode->name, first->name) == 0);
8785 if (is_opcode_valid_16 (opcode))
8786 {
5b7c81bd 8787 seen_valid_for_isa = true;
7fd53920
MR
8788 if (is_size_valid_16 (opcode))
8789 {
5b7c81bd 8790 seen_valid_for_size = true;
7fd53920 8791 if (match_mips16_insn (insn, opcode, tokens))
5b7c81bd 8792 return true;
7fd53920 8793 }
60f20e8b
RS
8794 }
8795 ++opcode;
8796 }
8797 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8798 && strcmp (opcode->name, first->name) == 0);
8799
8800 /* Handle the case where we didn't try to match an instruction because
8801 all the alternatives were incompatible with the current ISA. */
8802 if (!seen_valid_for_isa)
8803 {
8804 match_invalid_for_isa ();
5b7c81bd 8805 return true;
60f20e8b
RS
8806 }
8807
7fd53920
MR
8808 /* Handle the case where we didn't try to match an instruction because
8809 all the alternatives were of the wrong size. */
8810 if (!seen_valid_for_size)
8811 {
8812 if (forced_insn_length == 2)
8813 set_insn_error
8814 (0, _("unrecognized unextended version of MIPS16 opcode"));
8815 else
8816 set_insn_error
8817 (0, _("unrecognized extended version of MIPS16 opcode"));
5b7c81bd 8818 return true;
7fd53920
MR
8819 }
8820
5b7c81bd 8821 return false;
60f20e8b
RS
8822}
8823
584892a6
RS
8824/* Set up global variables for the start of a new macro. */
8825
8826static void
8827macro_start (void)
8828{
8829 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8830 memset (&mips_macro_warning.first_insn_sizes, 0,
8831 sizeof (mips_macro_warning.first_insn_sizes));
8832 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8833 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8834 && delayed_branch_p (&history[0]));
7bd374a4
MR
8835 if (history[0].frag
8836 && history[0].frag->fr_type == rs_machine_dependent
8837 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8838 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8839 mips_macro_warning.delay_slot_length = 0;
8840 else
8841 switch (history[0].insn_mo->pinfo2
8842 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8843 {
8844 case INSN2_BRANCH_DELAY_32BIT:
8845 mips_macro_warning.delay_slot_length = 4;
8846 break;
8847 case INSN2_BRANCH_DELAY_16BIT:
8848 mips_macro_warning.delay_slot_length = 2;
8849 break;
8850 default:
8851 mips_macro_warning.delay_slot_length = 0;
8852 break;
8853 }
df58fc94 8854 mips_macro_warning.first_frag = NULL;
584892a6
RS
8855}
8856
df58fc94
RS
8857/* Given that a macro is longer than one instruction or of the wrong size,
8858 return the appropriate warning for it. Return null if no warning is
8859 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8860 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8861 and RELAX_NOMACRO. */
584892a6
RS
8862
8863static const char *
8864macro_warning (relax_substateT subtype)
8865{
8866 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8867 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8868 " in a branch delay slot");
8869 else if (subtype & RELAX_NOMACRO)
1661c76c 8870 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8871 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8872 | RELAX_DELAY_SLOT_SIZE_SECOND))
8873 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8874 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8875 " in a 16-bit branch delay slot")
1661c76c 8876 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8877 " in a 32-bit branch delay slot"));
584892a6
RS
8878 else
8879 return 0;
8880}
8881
8882/* Finish up a macro. Emit warnings as appropriate. */
8883
8884static void
8885macro_end (void)
8886{
df58fc94
RS
8887 /* Relaxation warning flags. */
8888 relax_substateT subtype = 0;
8889
8890 /* Check delay slot size requirements. */
8891 if (mips_macro_warning.delay_slot_length == 2)
8892 subtype |= RELAX_DELAY_SLOT_16BIT;
8893 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8894 {
df58fc94
RS
8895 if (mips_macro_warning.delay_slot_length
8896 != mips_macro_warning.first_insn_sizes[0])
8897 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8898 if (mips_macro_warning.delay_slot_length
8899 != mips_macro_warning.first_insn_sizes[1])
8900 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8901 }
584892a6 8902
df58fc94
RS
8903 /* Check instruction count requirements. */
8904 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8905 {
8906 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8907 subtype |= RELAX_SECOND_LONGER;
8908 if (mips_opts.warn_about_macros)
8909 subtype |= RELAX_NOMACRO;
8910 if (mips_macro_warning.delay_slot_p)
8911 subtype |= RELAX_DELAY_SLOT;
df58fc94 8912 }
584892a6 8913
df58fc94
RS
8914 /* If both alternatives fail to fill a delay slot correctly,
8915 emit the warning now. */
8916 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8917 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8918 {
8919 relax_substateT s;
8920 const char *msg;
8921
8922 s = subtype & (RELAX_DELAY_SLOT_16BIT
8923 | RELAX_DELAY_SLOT_SIZE_FIRST
8924 | RELAX_DELAY_SLOT_SIZE_SECOND);
8925 msg = macro_warning (s);
8926 if (msg != NULL)
8927 as_warn ("%s", msg);
8928 subtype &= ~s;
8929 }
8930
8931 /* If both implementations are longer than 1 instruction, then emit the
8932 warning now. */
8933 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8934 {
8935 relax_substateT s;
8936 const char *msg;
8937
8938 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8939 msg = macro_warning (s);
8940 if (msg != NULL)
8941 as_warn ("%s", msg);
8942 subtype &= ~s;
584892a6 8943 }
df58fc94
RS
8944
8945 /* If any flags still set, then one implementation might need a warning
8946 and the other either will need one of a different kind or none at all.
8947 Pass any remaining flags over to relaxation. */
8948 if (mips_macro_warning.first_frag != NULL)
8949 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8950}
8951
df58fc94
RS
8952/* Instruction operand formats used in macros that vary between
8953 standard MIPS and microMIPS code. */
8954
833794fc 8955static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8956static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8957static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8958static const char * const lui_fmt[2] = { "t,u", "s,u" };
8959static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8960static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8961static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8962static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8963
833794fc 8964#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8965#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8966 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8967#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8968#define LUI_FMT (lui_fmt[mips_opts.micromips])
8969#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8970#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8971 : mem12_fmt[mips_opts.micromips])
833794fc 8972#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8973#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8974#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8975
6e1304d8
RS
8976/* Read a macro's relocation codes from *ARGS and store them in *R.
8977 The first argument in *ARGS will be either the code for a single
8978 relocation or -1 followed by the three codes that make up a
8979 composite relocation. */
8980
8981static void
8982macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8983{
8984 int i, next;
8985
8986 next = va_arg (*args, int);
8987 if (next >= 0)
8988 r[0] = (bfd_reloc_code_real_type) next;
8989 else
f2ae14a1
RS
8990 {
8991 for (i = 0; i < 3; i++)
8992 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8993 /* This function is only used for 16-bit relocation fields.
8994 To make the macro code simpler, treat an unrelocated value
8995 in the same way as BFD_RELOC_LO16. */
8996 if (r[0] == BFD_RELOC_UNUSED)
8997 r[0] = BFD_RELOC_LO16;
8998 }
6e1304d8
RS
8999}
9000
252b5132
RH
9001/* Build an instruction created by a macro expansion. This is passed
9002 a pointer to the count of instructions created so far, an
9003 expression, the name of the instruction to build, an operand format
9004 string, and corresponding arguments. */
9005
252b5132 9006static void
67c0d1eb 9007macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 9008{
df58fc94 9009 const struct mips_opcode *mo = NULL;
f6688943 9010 bfd_reloc_code_real_type r[3];
df58fc94 9011 const struct mips_opcode *amo;
e077a1c8 9012 const struct mips_operand *operand;
629310ab 9013 htab_t hash;
df58fc94 9014 struct mips_cl_insn insn;
252b5132 9015 va_list args;
e077a1c8 9016 unsigned int uval;
252b5132 9017
252b5132 9018 va_start (args, fmt);
252b5132 9019
252b5132
RH
9020 if (mips_opts.mips16)
9021 {
03ea81db 9022 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
9023 va_end (args);
9024 return;
9025 }
9026
f6688943
TS
9027 r[0] = BFD_RELOC_UNUSED;
9028 r[1] = BFD_RELOC_UNUSED;
9029 r[2] = BFD_RELOC_UNUSED;
df58fc94 9030 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
629310ab 9031 amo = (struct mips_opcode *) str_hash_find (hash, name);
df58fc94
RS
9032 gas_assert (amo);
9033 gas_assert (strcmp (name, amo->name) == 0);
1e915849 9034
df58fc94 9035 do
8b082fb1
TS
9036 {
9037 /* Search until we get a match for NAME. It is assumed here that
df58fc94 9038 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 9039 We try to match an instruction that fulfills the branch delay
df58fc94
RS
9040 slot instruction length requirement (if any) of the previous
9041 instruction. While doing this we record the first instruction
9042 seen that matches all the other conditions and use it anyway
9043 if the requirement cannot be met; we will issue an appropriate
9044 warning later on. */
9045 if (strcmp (fmt, amo->args) == 0
9046 && amo->pinfo != INSN_MACRO
9047 && is_opcode_valid (amo)
9048 && is_size_valid (amo))
9049 {
9050 if (is_delay_slot_valid (amo))
9051 {
9052 mo = amo;
9053 break;
9054 }
9055 else if (!mo)
9056 mo = amo;
9057 }
8b082fb1 9058
df58fc94
RS
9059 ++amo;
9060 gas_assert (amo->name);
252b5132 9061 }
df58fc94 9062 while (strcmp (name, amo->name) == 0);
252b5132 9063
df58fc94 9064 gas_assert (mo);
1e915849 9065 create_insn (&insn, mo);
e077a1c8 9066 for (; *fmt; ++fmt)
252b5132 9067 {
e077a1c8 9068 switch (*fmt)
252b5132 9069 {
252b5132
RH
9070 case ',':
9071 case '(':
9072 case ')':
252b5132 9073 case 'z':
e077a1c8 9074 break;
252b5132
RH
9075
9076 case 'i':
9077 case 'j':
6e1304d8 9078 macro_read_relocs (&args, r);
9c2799c2 9079 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
9080 || *r == BFD_RELOC_MIPS_HIGHER
9081 || *r == BFD_RELOC_HI16_S
9082 || *r == BFD_RELOC_LO16
14c80123
MR
9083 || *r == BFD_RELOC_MIPS_GOT_OFST
9084 || (mips_opts.micromips
d712f276 9085 && (*r == BFD_RELOC_MIPS_16
14c80123
MR
9086 || *r == BFD_RELOC_MIPS_GOT16
9087 || *r == BFD_RELOC_MIPS_CALL16
9088 || *r == BFD_RELOC_MIPS_GOT_HI16
9089 || *r == BFD_RELOC_MIPS_GOT_LO16
9090 || *r == BFD_RELOC_MIPS_CALL_HI16
9091 || *r == BFD_RELOC_MIPS_CALL_LO16
9092 || *r == BFD_RELOC_MIPS_SUB
9093 || *r == BFD_RELOC_MIPS_GOT_PAGE
9094 || *r == BFD_RELOC_MIPS_HIGHEST
9095 || *r == BFD_RELOC_MIPS_GOT_DISP
9096 || *r == BFD_RELOC_MIPS_TLS_GD
9097 || *r == BFD_RELOC_MIPS_TLS_LDM
9098 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
9099 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
9100 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
9101 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
9102 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 9103 break;
e391c024
RS
9104
9105 case 'o':
9106 macro_read_relocs (&args, r);
e077a1c8 9107 break;
252b5132
RH
9108
9109 case 'u':
6e1304d8 9110 macro_read_relocs (&args, r);
9c2799c2 9111 gas_assert (ep != NULL
90ecf173
MR
9112 && (ep->X_op == O_constant
9113 || (ep->X_op == O_symbol
9114 && (*r == BFD_RELOC_MIPS_HIGHEST
9115 || *r == BFD_RELOC_HI16_S
9116 || *r == BFD_RELOC_HI16
9117 || *r == BFD_RELOC_GPREL16
9118 || *r == BFD_RELOC_MIPS_GOT_HI16
9119 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 9120 break;
252b5132
RH
9121
9122 case 'p':
9c2799c2 9123 gas_assert (ep != NULL);
bad36eac 9124
252b5132
RH
9125 /*
9126 * This allows macro() to pass an immediate expression for
9127 * creating short branches without creating a symbol.
bad36eac
DJ
9128 *
9129 * We don't allow branch relaxation for these branches, as
9130 * they should only appear in ".set nomacro" anyway.
252b5132
RH
9131 */
9132 if (ep->X_op == O_constant)
9133 {
df58fc94
RS
9134 /* For microMIPS we always use relocations for branches.
9135 So we should not resolve immediate values. */
9136 gas_assert (!mips_opts.micromips);
9137
bad36eac
DJ
9138 if ((ep->X_add_number & 3) != 0)
9139 as_bad (_("branch to misaligned address (0x%lx)"),
9140 (unsigned long) ep->X_add_number);
9141 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
9142 as_bad (_("branch address range overflow (0x%lx)"),
9143 (unsigned long) ep->X_add_number);
252b5132
RH
9144 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
9145 ep = NULL;
9146 }
9147 else
0b25d3e6 9148 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 9149 break;
252b5132
RH
9150
9151 case 'a':
9c2799c2 9152 gas_assert (ep != NULL);
f6688943 9153 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 9154 break;
d43b4baf 9155
252b5132 9156 default:
e077a1c8
RS
9157 operand = (mips_opts.micromips
9158 ? decode_micromips_operand (fmt)
9159 : decode_mips_operand (fmt));
9160 if (!operand)
9161 abort ();
9162
9163 uval = va_arg (args, int);
9164 if (operand->type == OP_CLO_CLZ_DEST)
9165 uval |= (uval << 5);
9166 insn_insert_operand (&insn, operand, uval);
9167
7361da2c 9168 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
9169 ++fmt;
9170 break;
252b5132 9171 }
252b5132
RH
9172 }
9173 va_end (args);
9c2799c2 9174 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9175
5b7c81bd 9176 append_insn (&insn, ep, r, true);
252b5132
RH
9177}
9178
9179static void
67c0d1eb 9180mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 9181 va_list *args)
252b5132 9182{
1e915849 9183 struct mips_opcode *mo;
252b5132 9184 struct mips_cl_insn insn;
e077a1c8 9185 const struct mips_operand *operand;
f6688943
TS
9186 bfd_reloc_code_real_type r[3]
9187 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 9188
629310ab 9189 mo = (struct mips_opcode *) str_hash_find (mips16_op_hash, name);
9c2799c2
NC
9190 gas_assert (mo);
9191 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9192
1e915849 9193 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9194 {
1e915849 9195 ++mo;
9c2799c2
NC
9196 gas_assert (mo->name);
9197 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9198 }
9199
1e915849 9200 create_insn (&insn, mo);
e077a1c8 9201 for (; *fmt; ++fmt)
252b5132
RH
9202 {
9203 int c;
9204
e077a1c8 9205 c = *fmt;
252b5132
RH
9206 switch (c)
9207 {
252b5132
RH
9208 case ',':
9209 case '(':
9210 case ')':
e077a1c8 9211 break;
252b5132 9212
d8722d76 9213 case '.':
252b5132
RH
9214 case 'S':
9215 case 'P':
9216 case 'R':
e077a1c8 9217 break;
252b5132
RH
9218
9219 case '<':
252b5132 9220 case '5':
d8722d76 9221 case 'F':
252b5132
RH
9222 case 'H':
9223 case 'W':
9224 case 'D':
9225 case 'j':
9226 case '8':
9227 case 'V':
9228 case 'C':
9229 case 'U':
9230 case 'k':
9231 case 'K':
9232 case 'p':
9233 case 'q':
9234 {
b886a2ab
RS
9235 offsetT value;
9236
9c2799c2 9237 gas_assert (ep != NULL);
252b5132
RH
9238
9239 if (ep->X_op != O_constant)
874e8986 9240 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9241 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9242 {
b886a2ab 9243 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9244 ep = NULL;
f6688943 9245 *r = BFD_RELOC_UNUSED;
252b5132
RH
9246 }
9247 }
e077a1c8 9248 break;
252b5132 9249
e077a1c8 9250 default:
5b7c81bd 9251 operand = decode_mips16_operand (c, false);
e077a1c8
RS
9252 if (!operand)
9253 abort ();
252b5132 9254
4a06e5a2 9255 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9256 break;
9257 }
252b5132
RH
9258 }
9259
9c2799c2 9260 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9261
5b7c81bd 9262 append_insn (&insn, ep, r, true);
252b5132
RH
9263}
9264
438c16b8
TS
9265/*
9266 * Generate a "jalr" instruction with a relocation hint to the called
9267 * function. This occurs in NewABI PIC code.
9268 */
9269static void
df58fc94 9270macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9271{
df58fc94
RS
9272 static const bfd_reloc_code_real_type jalr_relocs[2]
9273 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9274 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9275 const char *jalr;
685736be 9276 char *f = NULL;
b34976b6 9277
1180b5a4 9278 if (MIPS_JALR_HINT_P (ep))
f21f8242 9279 {
cc3d92a5 9280 frag_grow (8);
f21f8242
AO
9281 f = frag_more (0);
9282 }
2906b037 9283 if (mips_opts.micromips)
df58fc94 9284 {
833794fc
MR
9285 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9286 ? "jalr" : "jalrs");
e64af278 9287 if (MIPS_JALR_HINT_P (ep)
833794fc 9288 || mips_opts.insn32
e64af278 9289 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9290 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9291 else
9292 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9293 }
2906b037
MR
9294 else
9295 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9296 if (MIPS_JALR_HINT_P (ep))
5b7c81bd 9297 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, false, jalr_reloc);
438c16b8
TS
9298}
9299
252b5132
RH
9300/*
9301 * Generate a "lui" instruction.
9302 */
9303static void
67c0d1eb 9304macro_build_lui (expressionS *ep, int regnum)
252b5132 9305{
9c2799c2 9306 gas_assert (! mips_opts.mips16);
252b5132 9307
df58fc94 9308 if (ep->X_op != O_constant)
252b5132 9309 {
9c2799c2 9310 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9311 /* _gp_disp is a special case, used from s_cpload.
9312 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9313 gas_assert (mips_pic == NO_PIC
78e1bb40 9314 || (! HAVE_NEWABI
aa6975fb
ILT
9315 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9316 || (! mips_in_shared
bbe506e8
TS
9317 && strcmp (S_GET_NAME (ep->X_add_symbol),
9318 "__gnu_local_gp") == 0));
252b5132
RH
9319 }
9320
df58fc94 9321 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9322}
9323
885add95
CD
9324/* Generate a sequence of instructions to do a load or store from a constant
9325 offset off of a base register (breg) into/from a target register (treg),
9326 using AT if necessary. */
9327static void
67c0d1eb
RS
9328macro_build_ldst_constoffset (expressionS *ep, const char *op,
9329 int treg, int breg, int dbl)
885add95 9330{
9c2799c2 9331 gas_assert (ep->X_op == O_constant);
885add95 9332
256ab948 9333 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9334 if (!dbl)
9335 normalize_constant_expr (ep);
256ab948 9336
67c1ffbe 9337 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9338 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9339 as_warn (_("operand overflow"));
9340
9341 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9342 {
9343 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9344 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9345 }
9346 else
9347 {
9348 /* 32-bit offset, need multiple instructions and AT, like:
9349 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9350 addu $tempreg,$tempreg,$breg
9351 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9352 to handle the complete offset. */
67c0d1eb
RS
9353 macro_build_lui (ep, AT);
9354 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9355 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9356
741fe287 9357 if (!mips_opts.at)
1661c76c 9358 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9359 }
9360}
9361
252b5132
RH
9362/* set_at()
9363 * Generates code to set the $at register to true (one)
9364 * if reg is less than the immediate expression.
9365 */
9366static void
67c0d1eb 9367set_at (int reg, int unsignedp)
252b5132 9368{
b0e6f033 9369 if (imm_expr.X_add_number >= -0x8000
252b5132 9370 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9371 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9372 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9373 else
9374 {
bad1aba3 9375 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9376 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9377 }
9378}
9379
252b5132
RH
9380/* Count the leading zeroes by performing a binary chop. This is a
9381 bulky bit of source, but performance is a LOT better for the
9382 majority of values than a simple loop to count the bits:
9383 for (lcnt = 0; (lcnt < 32); lcnt++)
9384 if ((v) & (1 << (31 - lcnt)))
9385 break;
9386 However it is not code size friendly, and the gain will drop a bit
9387 on certain cached systems.
9388*/
9389#define COUNT_TOP_ZEROES(v) \
9390 (((v) & ~0xffff) == 0 \
9391 ? ((v) & ~0xff) == 0 \
9392 ? ((v) & ~0xf) == 0 \
9393 ? ((v) & ~0x3) == 0 \
9394 ? ((v) & ~0x1) == 0 \
9395 ? !(v) \
9396 ? 32 \
9397 : 31 \
9398 : 30 \
9399 : ((v) & ~0x7) == 0 \
9400 ? 29 \
9401 : 28 \
9402 : ((v) & ~0x3f) == 0 \
9403 ? ((v) & ~0x1f) == 0 \
9404 ? 27 \
9405 : 26 \
9406 : ((v) & ~0x7f) == 0 \
9407 ? 25 \
9408 : 24 \
9409 : ((v) & ~0xfff) == 0 \
9410 ? ((v) & ~0x3ff) == 0 \
9411 ? ((v) & ~0x1ff) == 0 \
9412 ? 23 \
9413 : 22 \
9414 : ((v) & ~0x7ff) == 0 \
9415 ? 21 \
9416 : 20 \
9417 : ((v) & ~0x3fff) == 0 \
9418 ? ((v) & ~0x1fff) == 0 \
9419 ? 19 \
9420 : 18 \
9421 : ((v) & ~0x7fff) == 0 \
9422 ? 17 \
9423 : 16 \
9424 : ((v) & ~0xffffff) == 0 \
9425 ? ((v) & ~0xfffff) == 0 \
9426 ? ((v) & ~0x3ffff) == 0 \
9427 ? ((v) & ~0x1ffff) == 0 \
9428 ? 15 \
9429 : 14 \
9430 : ((v) & ~0x7ffff) == 0 \
9431 ? 13 \
9432 : 12 \
9433 : ((v) & ~0x3fffff) == 0 \
9434 ? ((v) & ~0x1fffff) == 0 \
9435 ? 11 \
9436 : 10 \
9437 : ((v) & ~0x7fffff) == 0 \
9438 ? 9 \
9439 : 8 \
9440 : ((v) & ~0xfffffff) == 0 \
9441 ? ((v) & ~0x3ffffff) == 0 \
9442 ? ((v) & ~0x1ffffff) == 0 \
9443 ? 7 \
9444 : 6 \
9445 : ((v) & ~0x7ffffff) == 0 \
9446 ? 5 \
9447 : 4 \
9448 : ((v) & ~0x3fffffff) == 0 \
9449 ? ((v) & ~0x1fffffff) == 0 \
9450 ? 3 \
9451 : 2 \
9452 : ((v) & ~0x7fffffff) == 0 \
9453 ? 1 \
9454 : 0)
9455
9456/* load_register()
67c1ffbe 9457 * This routine generates the least number of instructions necessary to load
252b5132
RH
9458 * an absolute expression value into a register.
9459 */
9460static void
67c0d1eb 9461load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9462{
9463 int freg;
9464 expressionS hi32, lo32;
9465
9466 if (ep->X_op != O_big)
9467 {
9c2799c2 9468 gas_assert (ep->X_op == O_constant);
256ab948
TS
9469
9470 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9471 if (!dbl)
9472 normalize_constant_expr (ep);
256ab948
TS
9473
9474 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9475 {
9476 /* We can handle 16 bit signed values with an addiu to
9477 $zero. No need to ever use daddiu here, since $zero and
9478 the result are always correct in 32 bit mode. */
67c0d1eb 9479 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9480 return;
9481 }
9482 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9483 {
9484 /* We can handle 16 bit unsigned values with an ori to
9485 $zero. */
67c0d1eb 9486 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9487 return;
9488 }
256ab948 9489 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9490 {
9491 /* 32 bit values require an lui. */
df58fc94 9492 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9493 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9494 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9495 return;
9496 }
9497 }
9498
9499 /* The value is larger than 32 bits. */
9500
bad1aba3 9501 if (!dbl || GPR_SIZE == 32)
252b5132 9502 {
f493c217
AM
9503 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
9504 ep->X_add_number);
67c0d1eb 9505 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9506 return;
9507 }
9508
9509 if (ep->X_op != O_big)
9510 {
9511 hi32 = *ep;
9512 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9513 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9514 hi32.X_add_number &= 0xffffffff;
9515 lo32 = *ep;
9516 lo32.X_add_number &= 0xffffffff;
9517 }
9518 else
9519 {
9c2799c2 9520 gas_assert (ep->X_add_number > 2);
252b5132
RH
9521 if (ep->X_add_number == 3)
9522 generic_bignum[3] = 0;
9523 else if (ep->X_add_number > 4)
1661c76c 9524 as_bad (_("number larger than 64 bits"));
252b5132
RH
9525 lo32.X_op = O_constant;
9526 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9527 hi32.X_op = O_constant;
9528 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9529 }
9530
9531 if (hi32.X_add_number == 0)
9532 freg = 0;
9533 else
9534 {
9535 int shift, bit;
9536 unsigned long hi, lo;
9537
956cd1d6 9538 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9539 {
9540 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9541 {
67c0d1eb 9542 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9543 return;
9544 }
9545 if (lo32.X_add_number & 0x80000000)
9546 {
df58fc94 9547 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9548 if (lo32.X_add_number & 0xffff)
67c0d1eb 9549 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9550 return;
9551 }
9552 }
252b5132
RH
9553
9554 /* Check for 16bit shifted constant. We know that hi32 is
9555 non-zero, so start the mask on the first bit of the hi32
9556 value. */
9557 shift = 17;
9558 do
beae10d5
KH
9559 {
9560 unsigned long himask, lomask;
9561
9562 if (shift < 32)
9563 {
9564 himask = 0xffff >> (32 - shift);
e0fd91ef 9565 lomask = (0xffffU << shift) & 0xffffffff;
beae10d5
KH
9566 }
9567 else
9568 {
e0fd91ef 9569 himask = 0xffffU << (shift - 32);
beae10d5
KH
9570 lomask = 0;
9571 }
9572 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9573 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9574 {
9575 expressionS tmp;
9576
9577 tmp.X_op = O_constant;
9578 if (shift < 32)
9579 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9580 | (lo32.X_add_number >> shift));
9581 else
9582 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9583 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9584 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9585 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9586 return;
9587 }
f9419b05 9588 ++shift;
beae10d5
KH
9589 }
9590 while (shift <= (64 - 16));
252b5132
RH
9591
9592 /* Find the bit number of the lowest one bit, and store the
9593 shifted value in hi/lo. */
9594 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9595 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9596 if (lo != 0)
9597 {
9598 bit = 0;
9599 while ((lo & 1) == 0)
9600 {
9601 lo >>= 1;
9602 ++bit;
9603 }
7697028a
AM
9604 if (bit != 0)
9605 {
9606 lo |= (hi & ((2UL << (bit - 1)) - 1)) << (32 - bit);
9607 hi >>= bit;
9608 }
252b5132
RH
9609 }
9610 else
9611 {
9612 bit = 32;
9613 while ((hi & 1) == 0)
9614 {
9615 hi >>= 1;
9616 ++bit;
9617 }
9618 lo = hi;
9619 hi = 0;
9620 }
9621
9622 /* Optimize if the shifted value is a (power of 2) - 1. */
9623 if ((hi == 0 && ((lo + 1) & lo) == 0)
9624 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9625 {
9626 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9627 if (shift != 0)
beae10d5 9628 {
252b5132
RH
9629 expressionS tmp;
9630
9631 /* This instruction will set the register to be all
9632 ones. */
beae10d5
KH
9633 tmp.X_op = O_constant;
9634 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9635 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9636 if (bit != 0)
9637 {
9638 bit += shift;
df58fc94 9639 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9640 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9641 }
df58fc94 9642 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9643 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9644 return;
9645 }
9646 }
252b5132
RH
9647
9648 /* Sign extend hi32 before calling load_register, because we can
9649 generally get better code when we load a sign extended value. */
9650 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9651 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9652 load_register (reg, &hi32, 0);
252b5132
RH
9653 freg = reg;
9654 }
9655 if ((lo32.X_add_number & 0xffff0000) == 0)
9656 {
9657 if (freg != 0)
9658 {
df58fc94 9659 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9660 freg = reg;
9661 }
9662 }
9663 else
9664 {
9665 expressionS mid16;
9666
956cd1d6 9667 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9668 {
df58fc94
RS
9669 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9670 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9671 return;
9672 }
252b5132
RH
9673
9674 if (freg != 0)
9675 {
df58fc94 9676 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9677 freg = reg;
9678 }
9679 mid16 = lo32;
9680 mid16.X_add_number >>= 16;
67c0d1eb 9681 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9682 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9683 freg = reg;
9684 }
9685 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9686 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9687}
9688
269137b2
TS
9689static inline void
9690load_delay_nop (void)
9691{
9692 if (!gpr_interlocks)
9693 macro_build (NULL, "nop", "");
9694}
9695
252b5132
RH
9696/* Load an address into a register. */
9697
9698static void
67c0d1eb 9699load_address (int reg, expressionS *ep, int *used_at)
252b5132 9700{
252b5132
RH
9701 if (ep->X_op != O_constant
9702 && ep->X_op != O_symbol)
9703 {
9704 as_bad (_("expression too complex"));
9705 ep->X_op = O_constant;
9706 }
9707
9708 if (ep->X_op == O_constant)
9709 {
67c0d1eb 9710 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9711 return;
9712 }
9713
9714 if (mips_pic == NO_PIC)
9715 {
9716 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9717 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9718 Otherwise we want
9719 lui $reg,<sym> (BFD_RELOC_HI16_S)
9720 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9721 If we have an addend, we always use the latter form.
76b3015f 9722
d6bc6245
TS
9723 With 64bit address space and a usable $at we want
9724 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9725 lui $at,<sym> (BFD_RELOC_HI16_S)
9726 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9727 daddiu $at,<sym> (BFD_RELOC_LO16)
9728 dsll32 $reg,0
3a482fd5 9729 daddu $reg,$reg,$at
76b3015f 9730
c03099e6 9731 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9732 on superscalar processors.
9733 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9734 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9735 dsll $reg,16
9736 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9737 dsll $reg,16
9738 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9739
9740 For GP relative symbols in 64bit address space we can use
9741 the same sequence as in 32bit address space. */
aed1a261 9742 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9743 {
6caf9ef4
TS
9744 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9745 && !nopic_need_relax (ep->X_add_symbol, 1))
9746 {
9747 relax_start (ep->X_add_symbol);
9748 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9749 mips_gp_register, BFD_RELOC_GPREL16);
9750 relax_switch ();
9751 }
d6bc6245 9752
741fe287 9753 if (*used_at == 0 && mips_opts.at)
d6bc6245 9754 {
df58fc94
RS
9755 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9756 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9757 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9758 BFD_RELOC_MIPS_HIGHER);
9759 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9760 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9761 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9762 *used_at = 1;
9763 }
9764 else
9765 {
df58fc94 9766 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9767 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9768 BFD_RELOC_MIPS_HIGHER);
df58fc94 9769 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9770 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9771 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9772 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9773 }
6caf9ef4
TS
9774
9775 if (mips_relax.sequence)
9776 relax_end ();
d6bc6245 9777 }
252b5132
RH
9778 else
9779 {
d6bc6245 9780 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9781 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9782 {
4d7206a2 9783 relax_start (ep->X_add_symbol);
67c0d1eb 9784 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9785 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9786 relax_switch ();
d6bc6245 9787 }
67c0d1eb
RS
9788 macro_build_lui (ep, reg);
9789 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9790 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9791 if (mips_relax.sequence)
9792 relax_end ();
d6bc6245 9793 }
252b5132 9794 }
0a44bf69 9795 else if (!mips_big_got)
252b5132
RH
9796 {
9797 expressionS ex;
9798
9799 /* If this is a reference to an external symbol, we want
9800 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9801 Otherwise we want
9802 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9803 nop
9804 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9805 If there is a constant, it must be added in after.
9806
ed6fb7bd 9807 If we have NewABI, we want
f5040a92
AO
9808 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9809 unless we're referencing a global symbol with a non-zero
9810 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9811 if (HAVE_NEWABI)
9812 {
f5040a92
AO
9813 if (ep->X_add_number)
9814 {
4d7206a2 9815 ex.X_add_number = ep->X_add_number;
f5040a92 9816 ep->X_add_number = 0;
4d7206a2 9817 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9818 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9819 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9820 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9821 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9822 ex.X_op = O_constant;
67c0d1eb 9823 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9824 reg, reg, BFD_RELOC_LO16);
f5040a92 9825 ep->X_add_number = ex.X_add_number;
4d7206a2 9826 relax_switch ();
f5040a92 9827 }
67c0d1eb 9828 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9829 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9830 if (mips_relax.sequence)
9831 relax_end ();
ed6fb7bd
SC
9832 }
9833 else
9834 {
f5040a92
AO
9835 ex.X_add_number = ep->X_add_number;
9836 ep->X_add_number = 0;
67c0d1eb
RS
9837 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9838 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9839 load_delay_nop ();
4d7206a2
RS
9840 relax_start (ep->X_add_symbol);
9841 relax_switch ();
67c0d1eb 9842 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9843 BFD_RELOC_LO16);
4d7206a2 9844 relax_end ();
ed6fb7bd 9845
f5040a92
AO
9846 if (ex.X_add_number != 0)
9847 {
9848 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9849 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9850 ex.X_op = O_constant;
67c0d1eb 9851 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9852 reg, reg, BFD_RELOC_LO16);
f5040a92 9853 }
252b5132
RH
9854 }
9855 }
0a44bf69 9856 else if (mips_big_got)
252b5132
RH
9857 {
9858 expressionS ex;
252b5132
RH
9859
9860 /* This is the large GOT case. If this is a reference to an
9861 external symbol, we want
9862 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9863 addu $reg,$reg,$gp
9864 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9865
9866 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9867 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9868 nop
9869 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9870 If there is a constant, it must be added in after.
f5040a92
AO
9871
9872 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9873 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9874 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9875 */
438c16b8
TS
9876 if (HAVE_NEWABI)
9877 {
4d7206a2 9878 ex.X_add_number = ep->X_add_number;
f5040a92 9879 ep->X_add_number = 0;
4d7206a2 9880 relax_start (ep->X_add_symbol);
df58fc94 9881 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9882 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9883 reg, reg, mips_gp_register);
9884 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9885 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9886 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9887 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9888 else if (ex.X_add_number)
9889 {
9890 ex.X_op = O_constant;
67c0d1eb
RS
9891 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9892 BFD_RELOC_LO16);
f5040a92
AO
9893 }
9894
9895 ep->X_add_number = ex.X_add_number;
4d7206a2 9896 relax_switch ();
67c0d1eb 9897 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9898 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9899 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9900 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9901 relax_end ();
438c16b8 9902 }
252b5132 9903 else
438c16b8 9904 {
f5040a92
AO
9905 ex.X_add_number = ep->X_add_number;
9906 ep->X_add_number = 0;
4d7206a2 9907 relax_start (ep->X_add_symbol);
df58fc94 9908 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9909 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9910 reg, reg, mips_gp_register);
9911 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9912 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9913 relax_switch ();
9914 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9915 {
9916 /* We need a nop before loading from $gp. This special
9917 check is required because the lui which starts the main
9918 instruction stream does not refer to $gp, and so will not
9919 insert the nop which may be required. */
67c0d1eb 9920 macro_build (NULL, "nop", "");
438c16b8 9921 }
67c0d1eb 9922 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9923 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9924 load_delay_nop ();
67c0d1eb 9925 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9926 BFD_RELOC_LO16);
4d7206a2 9927 relax_end ();
438c16b8 9928
f5040a92
AO
9929 if (ex.X_add_number != 0)
9930 {
9931 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9932 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9933 ex.X_op = O_constant;
67c0d1eb
RS
9934 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9935 BFD_RELOC_LO16);
f5040a92 9936 }
252b5132
RH
9937 }
9938 }
252b5132
RH
9939 else
9940 abort ();
8fc2e39e 9941
741fe287 9942 if (!mips_opts.at && *used_at == 1)
1661c76c 9943 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9944}
9945
ea1fb5dc
RS
9946/* Move the contents of register SOURCE into register DEST. */
9947
9948static void
67c0d1eb 9949move_register (int dest, int source)
ea1fb5dc 9950{
df58fc94
RS
9951 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9952 instruction specifically requires a 32-bit one. */
9953 if (mips_opts.micromips
833794fc 9954 && !mips_opts.insn32
df58fc94 9955 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9956 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9957 else
40fc1451 9958 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9959}
9960
4d7206a2 9961/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9962 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9963 The two alternatives are:
4d7206a2 9964
33eaf5de 9965 Global symbol Local symbol
4d7206a2
RS
9966 ------------- ------------
9967 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9968 ... ...
9969 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9970
9971 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9972 emits the second for a 16-bit offset or add_got_offset_hilo emits
9973 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9974
9975static void
67c0d1eb 9976load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9977{
9978 expressionS global;
9979
9980 global = *local;
9981 global.X_add_number = 0;
9982
9983 relax_start (local->X_add_symbol);
67c0d1eb
RS
9984 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9985 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9986 relax_switch ();
67c0d1eb
RS
9987 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9988 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9989 relax_end ();
9990}
9991
9992static void
67c0d1eb 9993add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9994{
9995 expressionS global;
9996
9997 global.X_op = O_constant;
9998 global.X_op_symbol = NULL;
9999 global.X_add_symbol = NULL;
10000 global.X_add_number = local->X_add_number;
10001
10002 relax_start (local->X_add_symbol);
67c0d1eb 10003 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
10004 dest, dest, BFD_RELOC_LO16);
10005 relax_switch ();
67c0d1eb 10006 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
10007 relax_end ();
10008}
10009
f6a22291
MR
10010static void
10011add_got_offset_hilo (int dest, expressionS *local, int tmp)
10012{
10013 expressionS global;
10014 int hold_mips_optimize;
10015
10016 global.X_op = O_constant;
10017 global.X_op_symbol = NULL;
10018 global.X_add_symbol = NULL;
10019 global.X_add_number = local->X_add_number;
10020
10021 relax_start (local->X_add_symbol);
10022 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
10023 relax_switch ();
10024 /* Set mips_optimize around the lui instruction to avoid
10025 inserting an unnecessary nop after the lw. */
10026 hold_mips_optimize = mips_optimize;
10027 mips_optimize = 2;
10028 macro_build_lui (&global, tmp);
10029 mips_optimize = hold_mips_optimize;
10030 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
10031 relax_end ();
10032
10033 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
10034}
10035
df58fc94
RS
10036/* Emit a sequence of instructions to emulate a branch likely operation.
10037 BR is an ordinary branch corresponding to one to be emulated. BRNEG
10038 is its complementing branch with the original condition negated.
10039 CALL is set if the original branch specified the link operation.
10040 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
10041
10042 Code like this is produced in the noreorder mode:
10043
10044 BRNEG <args>, 1f
10045 nop
10046 b <sym>
10047 delay slot (executed only if branch taken)
10048 1:
10049
10050 or, if CALL is set:
10051
10052 BRNEG <args>, 1f
10053 nop
10054 bal <sym>
10055 delay slot (executed only if branch taken)
10056 1:
10057
10058 In the reorder mode the delay slot would be filled with a nop anyway,
10059 so code produced is simply:
10060
10061 BR <args>, <sym>
10062 nop
10063
10064 This function is used when producing code for the microMIPS ASE that
10065 does not implement branch likely instructions in hardware. */
10066
10067static void
10068macro_build_branch_likely (const char *br, const char *brneg,
10069 int call, expressionS *ep, const char *fmt,
10070 unsigned int sreg, unsigned int treg)
10071{
10072 int noreorder = mips_opts.noreorder;
10073 expressionS expr1;
10074
10075 gas_assert (mips_opts.micromips);
10076 start_noreorder ();
10077 if (noreorder)
10078 {
10079 micromips_label_expr (&expr1);
10080 macro_build (&expr1, brneg, fmt, sreg, treg);
10081 macro_build (NULL, "nop", "");
10082 macro_build (ep, call ? "bal" : "b", "p");
10083
10084 /* Set to true so that append_insn adds a label. */
5b7c81bd 10085 emit_branch_likely_macro = true;
df58fc94
RS
10086 }
10087 else
10088 {
10089 macro_build (ep, br, fmt, sreg, treg);
10090 macro_build (NULL, "nop", "");
10091 }
10092 end_noreorder ();
10093}
10094
10095/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
10096 the condition code tested. EP specifies the branch target. */
10097
10098static void
10099macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
10100{
10101 const int call = 0;
10102 const char *brneg;
10103 const char *br;
10104
10105 switch (type)
10106 {
10107 case M_BC1FL:
10108 br = "bc1f";
10109 brneg = "bc1t";
10110 break;
10111 case M_BC1TL:
10112 br = "bc1t";
10113 brneg = "bc1f";
10114 break;
10115 case M_BC2FL:
10116 br = "bc2f";
10117 brneg = "bc2t";
10118 break;
10119 case M_BC2TL:
10120 br = "bc2t";
10121 brneg = "bc2f";
10122 break;
10123 default:
10124 abort ();
10125 }
10126 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
10127}
10128
10129/* Emit a two-argument branch macro specified by TYPE, using SREG as
10130 the register tested. EP specifies the branch target. */
10131
10132static void
10133macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
10134{
10135 const char *brneg = NULL;
10136 const char *br;
10137 int call = 0;
10138
10139 switch (type)
10140 {
10141 case M_BGEZ:
10142 br = "bgez";
10143 break;
10144 case M_BGEZL:
10145 br = mips_opts.micromips ? "bgez" : "bgezl";
10146 brneg = "bltz";
10147 break;
10148 case M_BGEZALL:
10149 gas_assert (mips_opts.micromips);
833794fc 10150 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
10151 brneg = "bltz";
10152 call = 1;
10153 break;
10154 case M_BGTZ:
10155 br = "bgtz";
10156 break;
10157 case M_BGTZL:
10158 br = mips_opts.micromips ? "bgtz" : "bgtzl";
10159 brneg = "blez";
10160 break;
10161 case M_BLEZ:
10162 br = "blez";
10163 break;
10164 case M_BLEZL:
10165 br = mips_opts.micromips ? "blez" : "blezl";
10166 brneg = "bgtz";
10167 break;
10168 case M_BLTZ:
10169 br = "bltz";
10170 break;
10171 case M_BLTZL:
10172 br = mips_opts.micromips ? "bltz" : "bltzl";
10173 brneg = "bgez";
10174 break;
10175 case M_BLTZALL:
10176 gas_assert (mips_opts.micromips);
833794fc 10177 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
10178 brneg = "bgez";
10179 call = 1;
10180 break;
10181 default:
10182 abort ();
10183 }
10184 if (mips_opts.micromips && brneg)
10185 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
10186 else
10187 macro_build (ep, br, "s,p", sreg);
10188}
10189
10190/* Emit a three-argument branch macro specified by TYPE, using SREG and
10191 TREG as the registers tested. EP specifies the branch target. */
10192
10193static void
10194macro_build_branch_rsrt (int type, expressionS *ep,
10195 unsigned int sreg, unsigned int treg)
10196{
10197 const char *brneg = NULL;
10198 const int call = 0;
10199 const char *br;
10200
10201 switch (type)
10202 {
10203 case M_BEQ:
10204 case M_BEQ_I:
10205 br = "beq";
10206 break;
10207 case M_BEQL:
10208 case M_BEQL_I:
10209 br = mips_opts.micromips ? "beq" : "beql";
10210 brneg = "bne";
10211 break;
10212 case M_BNE:
10213 case M_BNE_I:
10214 br = "bne";
10215 break;
10216 case M_BNEL:
10217 case M_BNEL_I:
10218 br = mips_opts.micromips ? "bne" : "bnel";
10219 brneg = "beq";
10220 break;
10221 default:
10222 abort ();
10223 }
10224 if (mips_opts.micromips && brneg)
10225 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10226 else
10227 macro_build (ep, br, "s,t,p", sreg, treg);
10228}
10229
f2ae14a1
RS
10230/* Return the high part that should be loaded in order to make the low
10231 part of VALUE accessible using an offset of OFFBITS bits. */
10232
10233static offsetT
10234offset_high_part (offsetT value, unsigned int offbits)
10235{
10236 offsetT bias;
10237 addressT low_mask;
10238
10239 if (offbits == 0)
10240 return value;
10241 bias = 1 << (offbits - 1);
10242 low_mask = bias * 2 - 1;
10243 return (value + bias) & ~low_mask;
10244}
10245
10246/* Return true if the value stored in offset_expr and offset_reloc
10247 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10248 amount that the caller wants to add without inducing overflow
10249 and ALIGN is the known alignment of the value in bytes. */
10250
5b7c81bd 10251static bool
f2ae14a1
RS
10252small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10253{
10254 if (offbits == 16)
10255 {
10256 /* Accept any relocation operator if overflow isn't a concern. */
10257 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
5b7c81bd 10258 return true;
f2ae14a1
RS
10259
10260 /* These relocations are guaranteed not to overflow in correct links. */
10261 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10262 || gprel16_reloc_p (*offset_reloc))
5b7c81bd 10263 return true;
f2ae14a1
RS
10264 }
10265 if (offset_expr.X_op == O_constant
10266 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10267 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
5b7c81bd
AM
10268 return true;
10269 return false;
f2ae14a1
RS
10270}
10271
252b5132
RH
10272/*
10273 * Build macros
10274 * This routine implements the seemingly endless macro or synthesized
10275 * instructions and addressing modes in the mips assembly language. Many
10276 * of these macros are simple and are similar to each other. These could
67c1ffbe 10277 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10278 * this verbose method. Others are not simple macros but are more like
10279 * optimizing code generation.
10280 * One interesting optimization is when several store macros appear
67c1ffbe 10281 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10282 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10283 * of global optimization. We currently only optimize within a single macro.
10284 * For many of the load and store macros if the address is specified as a
10285 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10286 * first load register 'at' with zero and use it as the base register. The
10287 * mips assembler simply uses register $zero. Just one tiny optimization
10288 * we're missing.
10289 */
10290static void
833794fc 10291macro (struct mips_cl_insn *ip, char *str)
252b5132 10292{
c0ebe874
RS
10293 const struct mips_operand_array *operands;
10294 unsigned int breg, i;
741fe287 10295 unsigned int tempreg;
252b5132 10296 int mask;
43841e91 10297 int used_at = 0;
df58fc94 10298 expressionS label_expr;
252b5132 10299 expressionS expr1;
df58fc94 10300 expressionS *ep;
252b5132
RH
10301 const char *s;
10302 const char *s2;
10303 const char *fmt;
10304 int likely = 0;
252b5132 10305 int coproc = 0;
7f3c4072 10306 int offbits = 16;
1abe91b1 10307 int call = 0;
df58fc94
RS
10308 int jals = 0;
10309 int dbl = 0;
10310 int imm = 0;
10311 int ust = 0;
10312 int lp = 0;
a45328b9 10313 int ll_sc_paired = 0;
5b7c81bd 10314 bool large_offset;
252b5132 10315 int off;
252b5132 10316 int hold_mips_optimize;
f2ae14a1 10317 unsigned int align;
c0ebe874 10318 unsigned int op[MAX_OPERANDS];
252b5132 10319
9c2799c2 10320 gas_assert (! mips_opts.mips16);
252b5132 10321
c0ebe874
RS
10322 operands = insn_operands (ip);
10323 for (i = 0; i < MAX_OPERANDS; i++)
10324 if (operands->operand[i])
10325 op[i] = insn_extract_operand (ip, operands->operand[i]);
10326 else
10327 op[i] = -1;
10328
252b5132
RH
10329 mask = ip->insn_mo->mask;
10330
df58fc94
RS
10331 label_expr.X_op = O_constant;
10332 label_expr.X_op_symbol = NULL;
10333 label_expr.X_add_symbol = NULL;
10334 label_expr.X_add_number = 0;
10335
252b5132
RH
10336 expr1.X_op = O_constant;
10337 expr1.X_op_symbol = NULL;
10338 expr1.X_add_symbol = NULL;
10339 expr1.X_add_number = 1;
f2ae14a1 10340 align = 1;
252b5132
RH
10341
10342 switch (mask)
10343 {
10344 case M_DABS:
10345 dbl = 1;
1a0670f3 10346 /* Fall through. */
252b5132 10347 case M_ABS:
df58fc94
RS
10348 /* bgez $a0,1f
10349 move v0,$a0
10350 sub v0,$zero,$a0
10351 1:
10352 */
252b5132 10353
7d10b47d 10354 start_noreorder ();
252b5132 10355
df58fc94
RS
10356 if (mips_opts.micromips)
10357 micromips_label_expr (&label_expr);
10358 else
10359 label_expr.X_add_number = 8;
c0ebe874
RS
10360 macro_build (&label_expr, "bgez", "s,p", op[1]);
10361 if (op[0] == op[1])
a605d2b3 10362 macro_build (NULL, "nop", "");
252b5132 10363 else
c0ebe874
RS
10364 move_register (op[0], op[1]);
10365 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10366 if (mips_opts.micromips)
10367 micromips_add_label ();
252b5132 10368
7d10b47d 10369 end_noreorder ();
8fc2e39e 10370 break;
252b5132
RH
10371
10372 case M_ADD_I:
10373 s = "addi";
10374 s2 = "add";
387e7624
FS
10375 if (ISA_IS_R6 (mips_opts.isa))
10376 goto do_addi_i;
10377 else
10378 goto do_addi;
252b5132
RH
10379 case M_ADDU_I:
10380 s = "addiu";
10381 s2 = "addu";
10382 goto do_addi;
10383 case M_DADD_I:
10384 dbl = 1;
10385 s = "daddi";
10386 s2 = "dadd";
387e7624 10387 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10388 goto do_addi;
b0e6f033 10389 if (imm_expr.X_add_number >= -0x200
387e7624
FS
10390 && imm_expr.X_add_number < 0x200
10391 && !ISA_IS_R6 (mips_opts.isa))
df58fc94 10392 {
b0e6f033
RS
10393 macro_build (NULL, s, "t,r,.", op[0], op[1],
10394 (int) imm_expr.X_add_number);
df58fc94
RS
10395 break;
10396 }
10397 goto do_addi_i;
252b5132
RH
10398 case M_DADDU_I:
10399 dbl = 1;
10400 s = "daddiu";
10401 s2 = "daddu";
10402 do_addi:
b0e6f033 10403 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10404 && imm_expr.X_add_number < 0x8000)
10405 {
c0ebe874 10406 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10407 break;
252b5132 10408 }
df58fc94 10409 do_addi_i:
8fc2e39e 10410 used_at = 1;
67c0d1eb 10411 load_register (AT, &imm_expr, dbl);
c0ebe874 10412 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10413 break;
10414
10415 case M_AND_I:
10416 s = "andi";
10417 s2 = "and";
10418 goto do_bit;
10419 case M_OR_I:
10420 s = "ori";
10421 s2 = "or";
10422 goto do_bit;
10423 case M_NOR_I:
10424 s = "";
10425 s2 = "nor";
10426 goto do_bit;
10427 case M_XOR_I:
10428 s = "xori";
10429 s2 = "xor";
10430 do_bit:
b0e6f033 10431 if (imm_expr.X_add_number >= 0
252b5132
RH
10432 && imm_expr.X_add_number < 0x10000)
10433 {
10434 if (mask != M_NOR_I)
c0ebe874 10435 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10436 else
10437 {
67c0d1eb 10438 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10439 op[0], op[1], BFD_RELOC_LO16);
10440 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10441 }
8fc2e39e 10442 break;
252b5132
RH
10443 }
10444
8fc2e39e 10445 used_at = 1;
bad1aba3 10446 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10447 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10448 break;
10449
8b082fb1
TS
10450 case M_BALIGN:
10451 switch (imm_expr.X_add_number)
10452 {
10453 case 0:
10454 macro_build (NULL, "nop", "");
10455 break;
10456 case 2:
c0ebe874 10457 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10458 break;
03f66e8a
MR
10459 case 1:
10460 case 3:
c0ebe874 10461 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10462 (int) imm_expr.X_add_number);
8b082fb1 10463 break;
03f66e8a
MR
10464 default:
10465 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10466 (unsigned long) imm_expr.X_add_number);
10467 break;
8b082fb1
TS
10468 }
10469 break;
10470
df58fc94
RS
10471 case M_BC1FL:
10472 case M_BC1TL:
10473 case M_BC2FL:
10474 case M_BC2TL:
10475 gas_assert (mips_opts.micromips);
10476 macro_build_branch_ccl (mask, &offset_expr,
10477 EXTRACT_OPERAND (1, BCC, *ip));
10478 break;
10479
252b5132 10480 case M_BEQ_I:
252b5132 10481 case M_BEQL_I:
252b5132 10482 case M_BNE_I:
252b5132 10483 case M_BNEL_I:
b0e6f033 10484 if (imm_expr.X_add_number == 0)
c0ebe874 10485 op[1] = 0;
df58fc94 10486 else
252b5132 10487 {
c0ebe874 10488 op[1] = AT;
df58fc94 10489 used_at = 1;
bad1aba3 10490 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10491 }
df58fc94
RS
10492 /* Fall through. */
10493 case M_BEQL:
10494 case M_BNEL:
c0ebe874 10495 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10496 break;
10497
10498 case M_BGEL:
10499 likely = 1;
1a0670f3 10500 /* Fall through. */
252b5132 10501 case M_BGE:
c0ebe874
RS
10502 if (op[1] == 0)
10503 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10504 else if (op[0] == 0)
10505 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10506 else
252b5132 10507 {
df58fc94 10508 used_at = 1;
c0ebe874 10509 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10510 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10511 &offset_expr, AT, ZERO);
252b5132 10512 }
df58fc94
RS
10513 break;
10514
10515 case M_BGEZL:
10516 case M_BGEZALL:
10517 case M_BGTZL:
10518 case M_BLEZL:
10519 case M_BLTZL:
10520 case M_BLTZALL:
c0ebe874 10521 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10522 break;
10523
10524 case M_BGTL_I:
10525 likely = 1;
1a0670f3 10526 /* Fall through. */
252b5132 10527 case M_BGT_I:
90ecf173 10528 /* Check for > max integer. */
b0e6f033 10529 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10530 {
10531 do_false:
90ecf173 10532 /* Result is always false. */
252b5132 10533 if (! likely)
a605d2b3 10534 macro_build (NULL, "nop", "");
252b5132 10535 else
df58fc94 10536 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10537 break;
252b5132 10538 }
f9419b05 10539 ++imm_expr.X_add_number;
6f2117ba 10540 /* Fall through. */
252b5132
RH
10541 case M_BGE_I:
10542 case M_BGEL_I:
10543 if (mask == M_BGEL_I)
10544 likely = 1;
b0e6f033 10545 if (imm_expr.X_add_number == 0)
252b5132 10546 {
df58fc94 10547 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10548 &offset_expr, op[0]);
8fc2e39e 10549 break;
252b5132 10550 }
b0e6f033 10551 if (imm_expr.X_add_number == 1)
252b5132 10552 {
df58fc94 10553 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10554 &offset_expr, op[0]);
8fc2e39e 10555 break;
252b5132 10556 }
b0e6f033 10557 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10558 {
10559 do_true:
6f2117ba 10560 /* Result is always true. */
1661c76c 10561 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10562 macro_build (&offset_expr, "b", "p");
8fc2e39e 10563 break;
252b5132 10564 }
8fc2e39e 10565 used_at = 1;
c0ebe874 10566 set_at (op[0], 0);
df58fc94
RS
10567 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10568 &offset_expr, AT, ZERO);
252b5132
RH
10569 break;
10570
10571 case M_BGEUL:
10572 likely = 1;
1a0670f3 10573 /* Fall through. */
252b5132 10574 case M_BGEU:
c0ebe874 10575 if (op[1] == 0)
252b5132 10576 goto do_true;
c0ebe874 10577 else if (op[0] == 0)
df58fc94 10578 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10579 &offset_expr, ZERO, op[1]);
df58fc94 10580 else
252b5132 10581 {
df58fc94 10582 used_at = 1;
c0ebe874 10583 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10584 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10585 &offset_expr, AT, ZERO);
252b5132 10586 }
252b5132
RH
10587 break;
10588
10589 case M_BGTUL_I:
10590 likely = 1;
1a0670f3 10591 /* Fall through. */
252b5132 10592 case M_BGTU_I:
c0ebe874 10593 if (op[0] == 0
bad1aba3 10594 || (GPR_SIZE == 32
f01dc953 10595 && imm_expr.X_add_number == -1))
252b5132 10596 goto do_false;
f9419b05 10597 ++imm_expr.X_add_number;
6f2117ba 10598 /* Fall through. */
252b5132
RH
10599 case M_BGEU_I:
10600 case M_BGEUL_I:
10601 if (mask == M_BGEUL_I)
10602 likely = 1;
b0e6f033 10603 if (imm_expr.X_add_number == 0)
252b5132 10604 goto do_true;
b0e6f033 10605 else if (imm_expr.X_add_number == 1)
df58fc94 10606 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10607 &offset_expr, op[0], ZERO);
df58fc94 10608 else
252b5132 10609 {
df58fc94 10610 used_at = 1;
c0ebe874 10611 set_at (op[0], 1);
df58fc94
RS
10612 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10613 &offset_expr, AT, ZERO);
252b5132 10614 }
252b5132
RH
10615 break;
10616
10617 case M_BGTL:
10618 likely = 1;
1a0670f3 10619 /* Fall through. */
252b5132 10620 case M_BGT:
c0ebe874
RS
10621 if (op[1] == 0)
10622 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10623 else if (op[0] == 0)
10624 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10625 else
252b5132 10626 {
df58fc94 10627 used_at = 1;
c0ebe874 10628 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10629 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10630 &offset_expr, AT, ZERO);
252b5132 10631 }
252b5132
RH
10632 break;
10633
10634 case M_BGTUL:
10635 likely = 1;
1a0670f3 10636 /* Fall through. */
252b5132 10637 case M_BGTU:
c0ebe874 10638 if (op[1] == 0)
df58fc94 10639 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10640 &offset_expr, op[0], ZERO);
10641 else if (op[0] == 0)
df58fc94
RS
10642 goto do_false;
10643 else
252b5132 10644 {
df58fc94 10645 used_at = 1;
c0ebe874 10646 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10647 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10648 &offset_expr, AT, ZERO);
252b5132 10649 }
252b5132
RH
10650 break;
10651
10652 case M_BLEL:
10653 likely = 1;
1a0670f3 10654 /* Fall through. */
252b5132 10655 case M_BLE:
c0ebe874
RS
10656 if (op[1] == 0)
10657 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10658 else if (op[0] == 0)
10659 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10660 else
252b5132 10661 {
df58fc94 10662 used_at = 1;
c0ebe874 10663 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10664 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10665 &offset_expr, AT, ZERO);
252b5132 10666 }
252b5132
RH
10667 break;
10668
10669 case M_BLEL_I:
10670 likely = 1;
1a0670f3 10671 /* Fall through. */
252b5132 10672 case M_BLE_I:
b0e6f033 10673 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10674 goto do_true;
f9419b05 10675 ++imm_expr.X_add_number;
6f2117ba 10676 /* Fall through. */
252b5132
RH
10677 case M_BLT_I:
10678 case M_BLTL_I:
10679 if (mask == M_BLTL_I)
10680 likely = 1;
b0e6f033 10681 if (imm_expr.X_add_number == 0)
c0ebe874 10682 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10683 else if (imm_expr.X_add_number == 1)
c0ebe874 10684 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10685 else
252b5132 10686 {
df58fc94 10687 used_at = 1;
c0ebe874 10688 set_at (op[0], 0);
df58fc94
RS
10689 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10690 &offset_expr, AT, ZERO);
252b5132 10691 }
252b5132
RH
10692 break;
10693
10694 case M_BLEUL:
10695 likely = 1;
1a0670f3 10696 /* Fall through. */
252b5132 10697 case M_BLEU:
c0ebe874 10698 if (op[1] == 0)
df58fc94 10699 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10700 &offset_expr, op[0], ZERO);
10701 else if (op[0] == 0)
df58fc94
RS
10702 goto do_true;
10703 else
252b5132 10704 {
df58fc94 10705 used_at = 1;
c0ebe874 10706 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10707 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10708 &offset_expr, AT, ZERO);
252b5132 10709 }
252b5132
RH
10710 break;
10711
10712 case M_BLEUL_I:
10713 likely = 1;
1a0670f3 10714 /* Fall through. */
252b5132 10715 case M_BLEU_I:
c0ebe874 10716 if (op[0] == 0
bad1aba3 10717 || (GPR_SIZE == 32
f01dc953 10718 && imm_expr.X_add_number == -1))
252b5132 10719 goto do_true;
f9419b05 10720 ++imm_expr.X_add_number;
6f2117ba 10721 /* Fall through. */
252b5132
RH
10722 case M_BLTU_I:
10723 case M_BLTUL_I:
10724 if (mask == M_BLTUL_I)
10725 likely = 1;
b0e6f033 10726 if (imm_expr.X_add_number == 0)
252b5132 10727 goto do_false;
b0e6f033 10728 else if (imm_expr.X_add_number == 1)
df58fc94 10729 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10730 &offset_expr, op[0], ZERO);
df58fc94 10731 else
252b5132 10732 {
df58fc94 10733 used_at = 1;
c0ebe874 10734 set_at (op[0], 1);
df58fc94
RS
10735 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10736 &offset_expr, AT, ZERO);
252b5132 10737 }
252b5132
RH
10738 break;
10739
10740 case M_BLTL:
10741 likely = 1;
1a0670f3 10742 /* Fall through. */
252b5132 10743 case M_BLT:
c0ebe874
RS
10744 if (op[1] == 0)
10745 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10746 else if (op[0] == 0)
10747 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10748 else
252b5132 10749 {
df58fc94 10750 used_at = 1;
c0ebe874 10751 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10752 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10753 &offset_expr, AT, ZERO);
252b5132 10754 }
252b5132
RH
10755 break;
10756
10757 case M_BLTUL:
10758 likely = 1;
1a0670f3 10759 /* Fall through. */
252b5132 10760 case M_BLTU:
c0ebe874 10761 if (op[1] == 0)
252b5132 10762 goto do_false;
c0ebe874 10763 else if (op[0] == 0)
df58fc94 10764 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10765 &offset_expr, ZERO, op[1]);
df58fc94 10766 else
252b5132 10767 {
df58fc94 10768 used_at = 1;
c0ebe874 10769 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10770 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10771 &offset_expr, AT, ZERO);
252b5132 10772 }
252b5132
RH
10773 break;
10774
10775 case M_DDIV_3:
10776 dbl = 1;
1a0670f3 10777 /* Fall through. */
252b5132
RH
10778 case M_DIV_3:
10779 s = "mflo";
10780 goto do_div3;
10781 case M_DREM_3:
10782 dbl = 1;
1a0670f3 10783 /* Fall through. */
252b5132
RH
10784 case M_REM_3:
10785 s = "mfhi";
10786 do_div3:
c0ebe874 10787 if (op[2] == 0)
252b5132 10788 {
1661c76c 10789 as_warn (_("divide by zero"));
252b5132 10790 if (mips_trap)
df58fc94 10791 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10792 else
df58fc94 10793 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10794 break;
252b5132
RH
10795 }
10796
7d10b47d 10797 start_noreorder ();
252b5132
RH
10798 if (mips_trap)
10799 {
c0ebe874
RS
10800 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10801 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10802 }
10803 else
10804 {
df58fc94
RS
10805 if (mips_opts.micromips)
10806 micromips_label_expr (&label_expr);
10807 else
10808 label_expr.X_add_number = 8;
c0ebe874
RS
10809 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10810 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10811 macro_build (NULL, "break", BRK_FMT, 7);
10812 if (mips_opts.micromips)
10813 micromips_add_label ();
252b5132
RH
10814 }
10815 expr1.X_add_number = -1;
8fc2e39e 10816 used_at = 1;
f6a22291 10817 load_register (AT, &expr1, dbl);
df58fc94
RS
10818 if (mips_opts.micromips)
10819 micromips_label_expr (&label_expr);
10820 else
10821 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10822 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10823 if (dbl)
10824 {
10825 expr1.X_add_number = 1;
f6a22291 10826 load_register (AT, &expr1, dbl);
df58fc94 10827 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10828 }
10829 else
10830 {
10831 expr1.X_add_number = 0x80000000;
df58fc94 10832 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10833 }
10834 if (mips_trap)
10835 {
c0ebe874 10836 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10837 /* We want to close the noreorder block as soon as possible, so
10838 that later insns are available for delay slot filling. */
7d10b47d 10839 end_noreorder ();
252b5132
RH
10840 }
10841 else
10842 {
df58fc94
RS
10843 if (mips_opts.micromips)
10844 micromips_label_expr (&label_expr);
10845 else
10846 label_expr.X_add_number = 8;
c0ebe874 10847 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10848 macro_build (NULL, "nop", "");
252b5132
RH
10849
10850 /* We want to close the noreorder block as soon as possible, so
10851 that later insns are available for delay slot filling. */
7d10b47d 10852 end_noreorder ();
252b5132 10853
df58fc94 10854 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10855 }
df58fc94
RS
10856 if (mips_opts.micromips)
10857 micromips_add_label ();
c0ebe874 10858 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10859 break;
10860
10861 case M_DIV_3I:
10862 s = "div";
10863 s2 = "mflo";
10864 goto do_divi;
10865 case M_DIVU_3I:
10866 s = "divu";
10867 s2 = "mflo";
10868 goto do_divi;
10869 case M_REM_3I:
10870 s = "div";
10871 s2 = "mfhi";
10872 goto do_divi;
10873 case M_REMU_3I:
10874 s = "divu";
10875 s2 = "mfhi";
10876 goto do_divi;
10877 case M_DDIV_3I:
10878 dbl = 1;
10879 s = "ddiv";
10880 s2 = "mflo";
10881 goto do_divi;
10882 case M_DDIVU_3I:
10883 dbl = 1;
10884 s = "ddivu";
10885 s2 = "mflo";
10886 goto do_divi;
10887 case M_DREM_3I:
10888 dbl = 1;
10889 s = "ddiv";
10890 s2 = "mfhi";
10891 goto do_divi;
10892 case M_DREMU_3I:
10893 dbl = 1;
10894 s = "ddivu";
10895 s2 = "mfhi";
10896 do_divi:
b0e6f033 10897 if (imm_expr.X_add_number == 0)
252b5132 10898 {
1661c76c 10899 as_warn (_("divide by zero"));
252b5132 10900 if (mips_trap)
df58fc94 10901 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10902 else
df58fc94 10903 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10904 break;
252b5132 10905 }
b0e6f033 10906 if (imm_expr.X_add_number == 1)
252b5132
RH
10907 {
10908 if (strcmp (s2, "mflo") == 0)
c0ebe874 10909 move_register (op[0], op[1]);
252b5132 10910 else
c0ebe874 10911 move_register (op[0], ZERO);
8fc2e39e 10912 break;
252b5132 10913 }
b0e6f033 10914 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10915 {
10916 if (strcmp (s2, "mflo") == 0)
c0ebe874 10917 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10918 else
c0ebe874 10919 move_register (op[0], ZERO);
8fc2e39e 10920 break;
252b5132
RH
10921 }
10922
8fc2e39e 10923 used_at = 1;
67c0d1eb 10924 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10925 macro_build (NULL, s, "z,s,t", op[1], AT);
10926 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10927 break;
10928
10929 case M_DIVU_3:
10930 s = "divu";
10931 s2 = "mflo";
10932 goto do_divu3;
10933 case M_REMU_3:
10934 s = "divu";
10935 s2 = "mfhi";
10936 goto do_divu3;
10937 case M_DDIVU_3:
10938 s = "ddivu";
10939 s2 = "mflo";
10940 goto do_divu3;
10941 case M_DREMU_3:
10942 s = "ddivu";
10943 s2 = "mfhi";
10944 do_divu3:
7d10b47d 10945 start_noreorder ();
252b5132
RH
10946 if (mips_trap)
10947 {
c0ebe874
RS
10948 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10949 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10950 /* We want to close the noreorder block as soon as possible, so
10951 that later insns are available for delay slot filling. */
7d10b47d 10952 end_noreorder ();
252b5132
RH
10953 }
10954 else
10955 {
df58fc94
RS
10956 if (mips_opts.micromips)
10957 micromips_label_expr (&label_expr);
10958 else
10959 label_expr.X_add_number = 8;
c0ebe874
RS
10960 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10961 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10962
10963 /* We want to close the noreorder block as soon as possible, so
10964 that later insns are available for delay slot filling. */
7d10b47d 10965 end_noreorder ();
df58fc94
RS
10966 macro_build (NULL, "break", BRK_FMT, 7);
10967 if (mips_opts.micromips)
10968 micromips_add_label ();
252b5132 10969 }
c0ebe874 10970 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10971 break;
252b5132 10972
1abe91b1
MR
10973 case M_DLCA_AB:
10974 dbl = 1;
1a0670f3 10975 /* Fall through. */
1abe91b1
MR
10976 case M_LCA_AB:
10977 call = 1;
10978 goto do_la;
252b5132
RH
10979 case M_DLA_AB:
10980 dbl = 1;
1a0670f3 10981 /* Fall through. */
252b5132 10982 case M_LA_AB:
1abe91b1 10983 do_la:
252b5132
RH
10984 /* Load the address of a symbol into a register. If breg is not
10985 zero, we then add a base register to it. */
10986
c0ebe874 10987 breg = op[2];
bad1aba3 10988 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10989 as_warn (_("dla used to load 32-bit register; recommend using la "
10990 "instead"));
3bec30a8 10991
90ecf173 10992 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10993 as_warn (_("la used to load 64-bit address; recommend using dla "
10994 "instead"));
3bec30a8 10995
f2ae14a1 10996 if (small_offset_p (0, align, 16))
0c11417f 10997 {
c0ebe874 10998 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10999 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 11000 break;
0c11417f
MR
11001 }
11002
c0ebe874 11003 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
11004 {
11005 tempreg = AT;
11006 used_at = 1;
11007 }
11008 else
c0ebe874 11009 tempreg = op[0];
afdbd6d0 11010
252b5132
RH
11011 if (offset_expr.X_op != O_symbol
11012 && offset_expr.X_op != O_constant)
11013 {
1661c76c 11014 as_bad (_("expression too complex"));
252b5132
RH
11015 offset_expr.X_op = O_constant;
11016 }
11017
252b5132 11018 if (offset_expr.X_op == O_constant)
aed1a261 11019 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
11020 else if (mips_pic == NO_PIC)
11021 {
d6bc6245 11022 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 11023 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
11024 Otherwise we want
11025 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11026 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11027 If we have a constant, we need two instructions anyhow,
d6bc6245 11028 so we may as well always use the latter form.
76b3015f 11029
6caf9ef4
TS
11030 With 64bit address space and a usable $at we want
11031 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11032 lui $at,<sym> (BFD_RELOC_HI16_S)
11033 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11034 daddiu $at,<sym> (BFD_RELOC_LO16)
11035 dsll32 $tempreg,0
11036 daddu $tempreg,$tempreg,$at
11037
11038 If $at is already in use, we use a path which is suboptimal
11039 on superscalar processors.
11040 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11041 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11042 dsll $tempreg,16
11043 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11044 dsll $tempreg,16
11045 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
11046
11047 For GP relative symbols in 64bit address space we can use
11048 the same sequence as in 32bit address space. */
aed1a261 11049 if (HAVE_64BIT_SYMBOLS)
252b5132 11050 {
6caf9ef4
TS
11051 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
11052 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11053 {
11054 relax_start (offset_expr.X_add_symbol);
11055 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11056 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
11057 relax_switch ();
11058 }
d6bc6245 11059
741fe287 11060 if (used_at == 0 && mips_opts.at)
98d3f06f 11061 {
df58fc94 11062 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11063 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 11064 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11065 AT, BFD_RELOC_HI16_S);
67c0d1eb 11066 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11067 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 11068 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11069 AT, AT, BFD_RELOC_LO16);
df58fc94 11070 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11071 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
11072 used_at = 1;
11073 }
11074 else
11075 {
df58fc94 11076 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 11077 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 11078 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11079 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
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_HI16_S);
df58fc94 11083 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 11084 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 11085 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 11086 }
6caf9ef4
TS
11087
11088 if (mips_relax.sequence)
11089 relax_end ();
98d3f06f
KH
11090 }
11091 else
11092 {
11093 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11094 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 11095 {
4d7206a2 11096 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11097 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11098 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 11099 relax_switch ();
98d3f06f 11100 }
6943caf0 11101 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 11102 as_bad (_("offset too large"));
67c0d1eb
RS
11103 macro_build_lui (&offset_expr, tempreg);
11104 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11105 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11106 if (mips_relax.sequence)
11107 relax_end ();
98d3f06f 11108 }
252b5132 11109 }
0a44bf69 11110 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 11111 {
9117d219
NC
11112 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
11113
252b5132
RH
11114 /* If this is a reference to an external symbol, and there
11115 is no constant, we want
11116 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 11117 or for lca or if tempreg is PIC_CALL_REG
9117d219 11118 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
11119 For a local symbol, we want
11120 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11121 nop
11122 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11123
11124 If we have a small constant, and this is a reference to
11125 an external symbol, we want
11126 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11127 nop
11128 addiu $tempreg,$tempreg,<constant>
11129 For a local symbol, we want the same instruction
11130 sequence, but we output a BFD_RELOC_LO16 reloc on the
11131 addiu instruction.
11132
11133 If we have a large constant, and this is a reference to
11134 an external symbol, we want
11135 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11136 lui $at,<hiconstant>
11137 addiu $at,$at,<loconstant>
11138 addu $tempreg,$tempreg,$at
11139 For a local symbol, we want the same instruction
11140 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 11141 addiu instruction.
ed6fb7bd
SC
11142 */
11143
4d7206a2 11144 if (offset_expr.X_add_number == 0)
252b5132 11145 {
0a44bf69
RS
11146 if (mips_pic == SVR4_PIC
11147 && breg == 0
11148 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
11149 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
11150
11151 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11152 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11153 lw_reloc_type, mips_gp_register);
4d7206a2 11154 if (breg != 0)
252b5132
RH
11155 {
11156 /* We're going to put in an addu instruction using
11157 tempreg, so we may as well insert the nop right
11158 now. */
269137b2 11159 load_delay_nop ();
252b5132 11160 }
4d7206a2 11161 relax_switch ();
67c0d1eb
RS
11162 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11163 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11164 load_delay_nop ();
67c0d1eb
RS
11165 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11166 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 11167 relax_end ();
252b5132
RH
11168 /* FIXME: If breg == 0, and the next instruction uses
11169 $tempreg, then if this variant case is used an extra
11170 nop will be generated. */
11171 }
4d7206a2
RS
11172 else if (offset_expr.X_add_number >= -0x8000
11173 && offset_expr.X_add_number < 0x8000)
252b5132 11174 {
67c0d1eb 11175 load_got_offset (tempreg, &offset_expr);
269137b2 11176 load_delay_nop ();
67c0d1eb 11177 add_got_offset (tempreg, &offset_expr);
252b5132
RH
11178 }
11179 else
11180 {
4d7206a2
RS
11181 expr1.X_add_number = offset_expr.X_add_number;
11182 offset_expr.X_add_number =
43c0598f 11183 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 11184 load_got_offset (tempreg, &offset_expr);
f6a22291 11185 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
11186 /* If we are going to add in a base register, and the
11187 target register and the base register are the same,
11188 then we are using AT as a temporary register. Since
11189 we want to load the constant into AT, we add our
11190 current AT (from the global offset table) and the
11191 register into the register now, and pretend we were
11192 not using a base register. */
c0ebe874 11193 if (breg == op[0])
252b5132 11194 {
269137b2 11195 load_delay_nop ();
67c0d1eb 11196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11197 op[0], AT, breg);
252b5132 11198 breg = 0;
c0ebe874 11199 tempreg = op[0];
252b5132 11200 }
f6a22291 11201 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11202 used_at = 1;
11203 }
11204 }
0a44bf69 11205 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11206 {
67c0d1eb 11207 int add_breg_early = 0;
f5040a92
AO
11208
11209 /* If this is a reference to an external, and there is no
11210 constant, or local symbol (*), with or without a
11211 constant, we want
11212 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11213 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11214 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11215
11216 If we have a small constant, and this is a reference to
11217 an external symbol, we want
11218 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11219 addiu $tempreg,$tempreg,<constant>
11220
11221 If we have a large constant, and this is a reference to
11222 an external symbol, we want
11223 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11224 lui $at,<hiconstant>
11225 addiu $at,$at,<loconstant>
11226 addu $tempreg,$tempreg,$at
11227
11228 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11229 local symbols, even though it introduces an additional
11230 instruction. */
11231
f5040a92
AO
11232 if (offset_expr.X_add_number)
11233 {
4d7206a2 11234 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11235 offset_expr.X_add_number = 0;
11236
4d7206a2 11237 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11238 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11239 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11240
11241 if (expr1.X_add_number >= -0x8000
11242 && expr1.X_add_number < 0x8000)
11243 {
67c0d1eb
RS
11244 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11245 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11246 }
ecd13cd3 11247 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11248 {
c0ebe874
RS
11249 unsigned int dreg;
11250
f5040a92
AO
11251 /* If we are going to add in a base register, and the
11252 target register and the base register are the same,
11253 then we are using AT as a temporary register. Since
11254 we want to load the constant into AT, we add our
11255 current AT (from the global offset table) and the
11256 register into the register now, and pretend we were
11257 not using a base register. */
c0ebe874 11258 if (breg != op[0])
f5040a92
AO
11259 dreg = tempreg;
11260 else
11261 {
9c2799c2 11262 gas_assert (tempreg == AT);
67c0d1eb 11263 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11264 op[0], AT, breg);
11265 dreg = op[0];
67c0d1eb 11266 add_breg_early = 1;
f5040a92
AO
11267 }
11268
f6a22291 11269 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11270 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11271 dreg, dreg, AT);
f5040a92 11272
f5040a92
AO
11273 used_at = 1;
11274 }
11275 else
11276 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11277
4d7206a2 11278 relax_switch ();
f5040a92
AO
11279 offset_expr.X_add_number = expr1.X_add_number;
11280
67c0d1eb
RS
11281 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11282 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11283 if (add_breg_early)
f5040a92 11284 {
67c0d1eb 11285 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11286 op[0], tempreg, breg);
f5040a92 11287 breg = 0;
c0ebe874 11288 tempreg = op[0];
f5040a92 11289 }
4d7206a2 11290 relax_end ();
f5040a92 11291 }
4d7206a2 11292 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11293 {
4d7206a2 11294 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11295 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11296 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11297 relax_switch ();
67c0d1eb
RS
11298 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11299 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11300 relax_end ();
f5040a92 11301 }
4d7206a2 11302 else
f5040a92 11303 {
67c0d1eb
RS
11304 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11305 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11306 }
11307 }
0a44bf69 11308 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11309 {
67c0d1eb 11310 int gpdelay;
9117d219
NC
11311 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11312 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11313 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11314
11315 /* This is the large GOT case. If this is a reference to an
11316 external symbol, and there is no constant, we want
11317 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11318 addu $tempreg,$tempreg,$gp
11319 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11320 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11321 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11322 addu $tempreg,$tempreg,$gp
11323 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11324 For a local symbol, we want
11325 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11326 nop
11327 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11328
11329 If we have a small constant, and this is a reference to
11330 an external symbol, we want
11331 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11332 addu $tempreg,$tempreg,$gp
11333 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11334 nop
11335 addiu $tempreg,$tempreg,<constant>
11336 For a local symbol, we want
11337 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11338 nop
11339 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11340
11341 If we have a large constant, and this is a reference to
11342 an external symbol, we want
11343 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11344 addu $tempreg,$tempreg,$gp
11345 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11346 lui $at,<hiconstant>
11347 addiu $at,$at,<loconstant>
11348 addu $tempreg,$tempreg,$at
11349 For a local symbol, we want
11350 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11351 lui $at,<hiconstant>
11352 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11353 addu $tempreg,$tempreg,$at
f5040a92 11354 */
438c16b8 11355
252b5132
RH
11356 expr1.X_add_number = offset_expr.X_add_number;
11357 offset_expr.X_add_number = 0;
4d7206a2 11358 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11359 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11360 if (expr1.X_add_number == 0 && breg == 0
11361 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11362 {
11363 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11364 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11365 }
df58fc94 11366 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11367 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11368 tempreg, tempreg, mips_gp_register);
67c0d1eb 11369 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11370 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11371 if (expr1.X_add_number == 0)
11372 {
67c0d1eb 11373 if (breg != 0)
252b5132
RH
11374 {
11375 /* We're going to put in an addu instruction using
11376 tempreg, so we may as well insert the nop right
11377 now. */
269137b2 11378 load_delay_nop ();
252b5132 11379 }
252b5132
RH
11380 }
11381 else if (expr1.X_add_number >= -0x8000
11382 && expr1.X_add_number < 0x8000)
11383 {
269137b2 11384 load_delay_nop ();
67c0d1eb 11385 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11386 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11387 }
11388 else
11389 {
c0ebe874
RS
11390 unsigned int dreg;
11391
252b5132
RH
11392 /* If we are going to add in a base register, and the
11393 target register and the base register are the same,
11394 then we are using AT as a temporary register. Since
11395 we want to load the constant into AT, we add our
11396 current AT (from the global offset table) and the
11397 register into the register now, and pretend we were
11398 not using a base register. */
c0ebe874 11399 if (breg != op[0])
67c0d1eb 11400 dreg = tempreg;
252b5132
RH
11401 else
11402 {
9c2799c2 11403 gas_assert (tempreg == AT);
269137b2 11404 load_delay_nop ();
67c0d1eb 11405 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11406 op[0], AT, breg);
11407 dreg = op[0];
252b5132
RH
11408 }
11409
f6a22291 11410 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11411 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11412
252b5132
RH
11413 used_at = 1;
11414 }
43c0598f 11415 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11416 relax_switch ();
252b5132 11417
67c0d1eb 11418 if (gpdelay)
252b5132
RH
11419 {
11420 /* This is needed because this instruction uses $gp, but
f5040a92 11421 the first instruction on the main stream does not. */
67c0d1eb 11422 macro_build (NULL, "nop", "");
252b5132 11423 }
ed6fb7bd 11424
67c0d1eb
RS
11425 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11426 local_reloc_type, mips_gp_register);
f5040a92 11427 if (expr1.X_add_number >= -0x8000
252b5132
RH
11428 && expr1.X_add_number < 0x8000)
11429 {
269137b2 11430 load_delay_nop ();
67c0d1eb
RS
11431 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11432 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11433 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11434 register, the external symbol case ended with a load,
11435 so if the symbol turns out to not be external, and
11436 the next instruction uses tempreg, an unnecessary nop
11437 will be inserted. */
252b5132
RH
11438 }
11439 else
11440 {
c0ebe874 11441 if (breg == op[0])
252b5132
RH
11442 {
11443 /* We must add in the base register now, as in the
f5040a92 11444 external symbol case. */
9c2799c2 11445 gas_assert (tempreg == AT);
269137b2 11446 load_delay_nop ();
67c0d1eb 11447 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11448 op[0], AT, breg);
11449 tempreg = op[0];
252b5132 11450 /* We set breg to 0 because we have arranged to add
f5040a92 11451 it in in both cases. */
252b5132
RH
11452 breg = 0;
11453 }
11454
67c0d1eb
RS
11455 macro_build_lui (&expr1, AT);
11456 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11457 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11458 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11459 tempreg, tempreg, AT);
8fc2e39e 11460 used_at = 1;
252b5132 11461 }
4d7206a2 11462 relax_end ();
252b5132 11463 }
0a44bf69 11464 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11465 {
f5040a92
AO
11466 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11467 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11468 int add_breg_early = 0;
f5040a92
AO
11469
11470 /* This is the large GOT case. If this is a reference to an
11471 external symbol, and there is no constant, we want
11472 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11473 add $tempreg,$tempreg,$gp
11474 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11475 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11476 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11477 add $tempreg,$tempreg,$gp
11478 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11479
11480 If we have a small constant, and this is a reference to
11481 an external symbol, we want
11482 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11483 add $tempreg,$tempreg,$gp
11484 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11485 addi $tempreg,$tempreg,<constant>
11486
11487 If we have a large constant, and this is a reference to
11488 an external symbol, we want
11489 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11490 addu $tempreg,$tempreg,$gp
11491 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11492 lui $at,<hiconstant>
11493 addi $at,$at,<loconstant>
11494 add $tempreg,$tempreg,$at
11495
11496 If we have NewABI, and we know it's a local symbol, we want
11497 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11498 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11499 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11500
4d7206a2 11501 relax_start (offset_expr.X_add_symbol);
f5040a92 11502
4d7206a2 11503 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11504 offset_expr.X_add_number = 0;
11505
1abe91b1
MR
11506 if (expr1.X_add_number == 0 && breg == 0
11507 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11508 {
11509 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11510 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11511 }
df58fc94 11512 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11513 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11514 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11515 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11516 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11517
11518 if (expr1.X_add_number == 0)
4d7206a2 11519 ;
f5040a92
AO
11520 else if (expr1.X_add_number >= -0x8000
11521 && expr1.X_add_number < 0x8000)
11522 {
67c0d1eb 11523 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11524 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11525 }
ecd13cd3 11526 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11527 {
c0ebe874
RS
11528 unsigned int dreg;
11529
f5040a92
AO
11530 /* If we are going to add in a base register, and the
11531 target register and the base register are the same,
11532 then we are using AT as a temporary register. Since
11533 we want to load the constant into AT, we add our
11534 current AT (from the global offset table) and the
11535 register into the register now, and pretend we were
11536 not using a base register. */
c0ebe874 11537 if (breg != op[0])
f5040a92
AO
11538 dreg = tempreg;
11539 else
11540 {
9c2799c2 11541 gas_assert (tempreg == AT);
67c0d1eb 11542 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11543 op[0], AT, breg);
11544 dreg = op[0];
67c0d1eb 11545 add_breg_early = 1;
f5040a92
AO
11546 }
11547
f6a22291 11548 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11549 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11550
f5040a92
AO
11551 used_at = 1;
11552 }
11553 else
11554 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11555
4d7206a2 11556 relax_switch ();
f5040a92 11557 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11558 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11559 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11560 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11561 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11562 if (add_breg_early)
f5040a92 11563 {
67c0d1eb 11564 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11565 op[0], tempreg, breg);
f5040a92 11566 breg = 0;
c0ebe874 11567 tempreg = op[0];
f5040a92 11568 }
4d7206a2 11569 relax_end ();
f5040a92 11570 }
252b5132
RH
11571 else
11572 abort ();
11573
11574 if (breg != 0)
c0ebe874 11575 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11576 break;
11577
52b6b6b9 11578 case M_MSGSND:
df58fc94 11579 gas_assert (!mips_opts.micromips);
c0ebe874 11580 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11581 break;
52b6b6b9
JM
11582
11583 case M_MSGLD:
df58fc94 11584 gas_assert (!mips_opts.micromips);
c8276761 11585 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11586 break;
52b6b6b9
JM
11587
11588 case M_MSGLD_T:
df58fc94 11589 gas_assert (!mips_opts.micromips);
c0ebe874 11590 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11591 break;
52b6b6b9
JM
11592
11593 case M_MSGWAIT:
df58fc94 11594 gas_assert (!mips_opts.micromips);
52b6b6b9 11595 macro_build (NULL, "c2", "C", 3);
c7af4273 11596 break;
52b6b6b9
JM
11597
11598 case M_MSGWAIT_T:
df58fc94 11599 gas_assert (!mips_opts.micromips);
c0ebe874 11600 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11601 break;
52b6b6b9 11602
252b5132
RH
11603 case M_J_A:
11604 /* The j instruction may not be used in PIC code, since it
11605 requires an absolute address. We convert it to a b
11606 instruction. */
11607 if (mips_pic == NO_PIC)
67c0d1eb 11608 macro_build (&offset_expr, "j", "a");
252b5132 11609 else
67c0d1eb 11610 macro_build (&offset_expr, "b", "p");
8fc2e39e 11611 break;
252b5132
RH
11612
11613 /* The jal instructions must be handled as macros because when
11614 generating PIC code they expand to multi-instruction
11615 sequences. Normally they are simple instructions. */
df58fc94 11616 case M_JALS_1:
c0ebe874
RS
11617 op[1] = op[0];
11618 op[0] = RA;
df58fc94
RS
11619 /* Fall through. */
11620 case M_JALS_2:
11621 gas_assert (mips_opts.micromips);
833794fc
MR
11622 if (mips_opts.insn32)
11623 {
1661c76c 11624 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11625 break;
11626 }
df58fc94
RS
11627 jals = 1;
11628 goto jal;
252b5132 11629 case M_JAL_1:
c0ebe874
RS
11630 op[1] = op[0];
11631 op[0] = RA;
252b5132
RH
11632 /* Fall through. */
11633 case M_JAL_2:
df58fc94 11634 jal:
3e722fb5 11635 if (mips_pic == NO_PIC)
df58fc94
RS
11636 {
11637 s = jals ? "jalrs" : "jalr";
e64af278 11638 if (mips_opts.micromips
833794fc 11639 && !mips_opts.insn32
c0ebe874 11640 && op[0] == RA
e64af278 11641 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11642 macro_build (NULL, s, "mj", op[1]);
df58fc94 11643 else
c0ebe874 11644 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11645 }
0a44bf69 11646 else
252b5132 11647 {
df58fc94
RS
11648 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11649 && mips_cprestore_offset >= 0);
11650
c0ebe874 11651 if (op[1] != PIC_CALL_REG)
252b5132 11652 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11653
833794fc
MR
11654 s = ((mips_opts.micromips
11655 && !mips_opts.insn32
11656 && (!mips_opts.noreorder || cprestore))
df58fc94 11657 ? "jalrs" : "jalr");
e64af278 11658 if (mips_opts.micromips
833794fc 11659 && !mips_opts.insn32
c0ebe874 11660 && op[0] == RA
e64af278 11661 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11662 macro_build (NULL, s, "mj", op[1]);
df58fc94 11663 else
c0ebe874 11664 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11665 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11666 {
6478892d 11667 if (mips_cprestore_offset < 0)
1661c76c 11668 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11669 else
11670 {
90ecf173 11671 if (!mips_frame_reg_valid)
7a621144 11672 {
1661c76c 11673 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11674 /* Quiet this warning. */
11675 mips_frame_reg_valid = 1;
11676 }
90ecf173 11677 if (!mips_cprestore_valid)
7a621144 11678 {
1661c76c 11679 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11680 /* Quiet this warning. */
11681 mips_cprestore_valid = 1;
11682 }
d3fca0b5
MR
11683 if (mips_opts.noreorder)
11684 macro_build (NULL, "nop", "");
6478892d 11685 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11686 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11687 mips_gp_register,
256ab948
TS
11688 mips_frame_reg,
11689 HAVE_64BIT_ADDRESSES);
6478892d 11690 }
252b5132
RH
11691 }
11692 }
252b5132 11693
8fc2e39e 11694 break;
252b5132 11695
df58fc94
RS
11696 case M_JALS_A:
11697 gas_assert (mips_opts.micromips);
833794fc
MR
11698 if (mips_opts.insn32)
11699 {
1661c76c 11700 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11701 break;
11702 }
df58fc94
RS
11703 jals = 1;
11704 /* Fall through. */
252b5132
RH
11705 case M_JAL_A:
11706 if (mips_pic == NO_PIC)
df58fc94 11707 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11708 else if (mips_pic == SVR4_PIC)
11709 {
11710 /* If this is a reference to an external symbol, and we are
11711 using a small GOT, we want
11712 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11713 nop
f9419b05 11714 jalr $ra,$25
252b5132
RH
11715 nop
11716 lw $gp,cprestore($sp)
11717 The cprestore value is set using the .cprestore
11718 pseudo-op. If we are using a big GOT, we want
11719 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11720 addu $25,$25,$gp
11721 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11722 nop
f9419b05 11723 jalr $ra,$25
252b5132
RH
11724 nop
11725 lw $gp,cprestore($sp)
11726 If the symbol is not external, we want
11727 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11728 nop
11729 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11730 jalr $ra,$25
252b5132 11731 nop
438c16b8 11732 lw $gp,cprestore($sp)
f5040a92
AO
11733
11734 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11735 sequences above, minus nops, unless the symbol is local,
11736 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11737 GOT_DISP. */
438c16b8 11738 if (HAVE_NEWABI)
252b5132 11739 {
90ecf173 11740 if (!mips_big_got)
f5040a92 11741 {
4d7206a2 11742 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11743 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11744 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11745 mips_gp_register);
4d7206a2 11746 relax_switch ();
67c0d1eb
RS
11747 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11748 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11749 mips_gp_register);
11750 relax_end ();
f5040a92
AO
11751 }
11752 else
11753 {
4d7206a2 11754 relax_start (offset_expr.X_add_symbol);
df58fc94 11755 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11756 BFD_RELOC_MIPS_CALL_HI16);
11757 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11758 PIC_CALL_REG, mips_gp_register);
11759 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11760 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11761 PIC_CALL_REG);
4d7206a2 11762 relax_switch ();
67c0d1eb
RS
11763 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11764 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11765 mips_gp_register);
11766 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11767 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11768 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11769 relax_end ();
f5040a92 11770 }
684022ea 11771
df58fc94 11772 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11773 }
11774 else
11775 {
4d7206a2 11776 relax_start (offset_expr.X_add_symbol);
90ecf173 11777 if (!mips_big_got)
438c16b8 11778 {
67c0d1eb
RS
11779 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11780 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11781 mips_gp_register);
269137b2 11782 load_delay_nop ();
4d7206a2 11783 relax_switch ();
438c16b8 11784 }
252b5132 11785 else
252b5132 11786 {
67c0d1eb
RS
11787 int gpdelay;
11788
11789 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11790 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11791 BFD_RELOC_MIPS_CALL_HI16);
11792 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11793 PIC_CALL_REG, mips_gp_register);
11794 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11795 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11796 PIC_CALL_REG);
269137b2 11797 load_delay_nop ();
4d7206a2 11798 relax_switch ();
67c0d1eb
RS
11799 if (gpdelay)
11800 macro_build (NULL, "nop", "");
252b5132 11801 }
67c0d1eb
RS
11802 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11803 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11804 mips_gp_register);
269137b2 11805 load_delay_nop ();
67c0d1eb
RS
11806 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11807 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11808 relax_end ();
df58fc94 11809 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11810
6478892d 11811 if (mips_cprestore_offset < 0)
1661c76c 11812 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11813 else
11814 {
90ecf173 11815 if (!mips_frame_reg_valid)
7a621144 11816 {
1661c76c 11817 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11818 /* Quiet this warning. */
11819 mips_frame_reg_valid = 1;
11820 }
90ecf173 11821 if (!mips_cprestore_valid)
7a621144 11822 {
1661c76c 11823 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11824 /* Quiet this warning. */
11825 mips_cprestore_valid = 1;
11826 }
6478892d 11827 if (mips_opts.noreorder)
67c0d1eb 11828 macro_build (NULL, "nop", "");
6478892d 11829 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11830 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11831 mips_gp_register,
256ab948
TS
11832 mips_frame_reg,
11833 HAVE_64BIT_ADDRESSES);
6478892d 11834 }
252b5132
RH
11835 }
11836 }
0a44bf69 11837 else if (mips_pic == VXWORKS_PIC)
1661c76c 11838 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11839 else
11840 abort ();
11841
8fc2e39e 11842 break;
252b5132 11843
7f3c4072 11844 case M_LBUE_AB:
7f3c4072
CM
11845 s = "lbue";
11846 fmt = "t,+j(b)";
11847 offbits = 9;
11848 goto ld_st;
11849 case M_LHUE_AB:
7f3c4072
CM
11850 s = "lhue";
11851 fmt = "t,+j(b)";
11852 offbits = 9;
11853 goto ld_st;
11854 case M_LBE_AB:
7f3c4072
CM
11855 s = "lbe";
11856 fmt = "t,+j(b)";
11857 offbits = 9;
11858 goto ld_st;
11859 case M_LHE_AB:
7f3c4072
CM
11860 s = "lhe";
11861 fmt = "t,+j(b)";
11862 offbits = 9;
11863 goto ld_st;
11864 case M_LLE_AB:
7f3c4072
CM
11865 s = "lle";
11866 fmt = "t,+j(b)";
11867 offbits = 9;
11868 goto ld_st;
11869 case M_LWE_AB:
7f3c4072
CM
11870 s = "lwe";
11871 fmt = "t,+j(b)";
11872 offbits = 9;
11873 goto ld_st;
11874 case M_LWLE_AB:
7f3c4072
CM
11875 s = "lwle";
11876 fmt = "t,+j(b)";
11877 offbits = 9;
11878 goto ld_st;
11879 case M_LWRE_AB:
7f3c4072
CM
11880 s = "lwre";
11881 fmt = "t,+j(b)";
11882 offbits = 9;
11883 goto ld_st;
11884 case M_SBE_AB:
7f3c4072
CM
11885 s = "sbe";
11886 fmt = "t,+j(b)";
11887 offbits = 9;
11888 goto ld_st;
11889 case M_SCE_AB:
7f3c4072
CM
11890 s = "sce";
11891 fmt = "t,+j(b)";
11892 offbits = 9;
11893 goto ld_st;
11894 case M_SHE_AB:
7f3c4072
CM
11895 s = "she";
11896 fmt = "t,+j(b)";
11897 offbits = 9;
11898 goto ld_st;
11899 case M_SWE_AB:
7f3c4072
CM
11900 s = "swe";
11901 fmt = "t,+j(b)";
11902 offbits = 9;
11903 goto ld_st;
11904 case M_SWLE_AB:
7f3c4072
CM
11905 s = "swle";
11906 fmt = "t,+j(b)";
11907 offbits = 9;
11908 goto ld_st;
11909 case M_SWRE_AB:
7f3c4072
CM
11910 s = "swre";
11911 fmt = "t,+j(b)";
11912 offbits = 9;
11913 goto ld_st;
dec0624d 11914 case M_ACLR_AB:
dec0624d 11915 s = "aclr";
dec0624d 11916 fmt = "\\,~(b)";
7f3c4072 11917 offbits = 12;
dec0624d
MR
11918 goto ld_st;
11919 case M_ASET_AB:
dec0624d 11920 s = "aset";
dec0624d 11921 fmt = "\\,~(b)";
7f3c4072 11922 offbits = 12;
dec0624d 11923 goto ld_st;
252b5132
RH
11924 case M_LB_AB:
11925 s = "lb";
df58fc94 11926 fmt = "t,o(b)";
252b5132
RH
11927 goto ld;
11928 case M_LBU_AB:
11929 s = "lbu";
df58fc94 11930 fmt = "t,o(b)";
252b5132
RH
11931 goto ld;
11932 case M_LH_AB:
11933 s = "lh";
df58fc94 11934 fmt = "t,o(b)";
252b5132
RH
11935 goto ld;
11936 case M_LHU_AB:
11937 s = "lhu";
df58fc94 11938 fmt = "t,o(b)";
252b5132
RH
11939 goto ld;
11940 case M_LW_AB:
11941 s = "lw";
df58fc94 11942 fmt = "t,o(b)";
252b5132
RH
11943 goto ld;
11944 case M_LWC0_AB:
df58fc94 11945 gas_assert (!mips_opts.micromips);
252b5132 11946 s = "lwc0";
df58fc94 11947 fmt = "E,o(b)";
bdaaa2e1 11948 /* Itbl support may require additional care here. */
252b5132 11949 coproc = 1;
df58fc94 11950 goto ld_st;
252b5132
RH
11951 case M_LWC1_AB:
11952 s = "lwc1";
df58fc94 11953 fmt = "T,o(b)";
bdaaa2e1 11954 /* Itbl support may require additional care here. */
252b5132 11955 coproc = 1;
df58fc94 11956 goto ld_st;
252b5132
RH
11957 case M_LWC2_AB:
11958 s = "lwc2";
df58fc94 11959 fmt = COP12_FMT;
7361da2c
AB
11960 offbits = (mips_opts.micromips ? 12
11961 : ISA_IS_R6 (mips_opts.isa) ? 11
11962 : 16);
bdaaa2e1 11963 /* Itbl support may require additional care here. */
252b5132 11964 coproc = 1;
df58fc94 11965 goto ld_st;
252b5132 11966 case M_LWC3_AB:
df58fc94 11967 gas_assert (!mips_opts.micromips);
252b5132 11968 s = "lwc3";
df58fc94 11969 fmt = "E,o(b)";
bdaaa2e1 11970 /* Itbl support may require additional care here. */
252b5132 11971 coproc = 1;
df58fc94 11972 goto ld_st;
252b5132
RH
11973 case M_LWL_AB:
11974 s = "lwl";
df58fc94 11975 fmt = MEM12_FMT;
7f3c4072 11976 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11977 goto ld_st;
252b5132
RH
11978 case M_LWR_AB:
11979 s = "lwr";
df58fc94 11980 fmt = MEM12_FMT;
7f3c4072 11981 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11982 goto ld_st;
252b5132 11983 case M_LDC1_AB:
252b5132 11984 s = "ldc1";
df58fc94 11985 fmt = "T,o(b)";
bdaaa2e1 11986 /* Itbl support may require additional care here. */
252b5132 11987 coproc = 1;
df58fc94 11988 goto ld_st;
252b5132
RH
11989 case M_LDC2_AB:
11990 s = "ldc2";
df58fc94 11991 fmt = COP12_FMT;
7361da2c
AB
11992 offbits = (mips_opts.micromips ? 12
11993 : ISA_IS_R6 (mips_opts.isa) ? 11
11994 : 16);
bdaaa2e1 11995 /* Itbl support may require additional care here. */
252b5132 11996 coproc = 1;
df58fc94 11997 goto ld_st;
c77c0862 11998 case M_LQC2_AB:
c77c0862 11999 s = "lqc2";
14daeee3 12000 fmt = "+7,o(b)";
c77c0862
RS
12001 /* Itbl support may require additional care here. */
12002 coproc = 1;
12003 goto ld_st;
252b5132
RH
12004 case M_LDC3_AB:
12005 s = "ldc3";
df58fc94 12006 fmt = "E,o(b)";
bdaaa2e1 12007 /* Itbl support may require additional care here. */
252b5132 12008 coproc = 1;
df58fc94 12009 goto ld_st;
252b5132
RH
12010 case M_LDL_AB:
12011 s = "ldl";
df58fc94 12012 fmt = MEM12_FMT;
7f3c4072 12013 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12014 goto ld_st;
252b5132
RH
12015 case M_LDR_AB:
12016 s = "ldr";
df58fc94 12017 fmt = MEM12_FMT;
7f3c4072 12018 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12019 goto ld_st;
252b5132
RH
12020 case M_LL_AB:
12021 s = "ll";
7361da2c
AB
12022 fmt = LL_SC_FMT;
12023 offbits = (mips_opts.micromips ? 12
12024 : ISA_IS_R6 (mips_opts.isa) ? 9
12025 : 16);
252b5132
RH
12026 goto ld;
12027 case M_LLD_AB:
12028 s = "lld";
7361da2c
AB
12029 fmt = LL_SC_FMT;
12030 offbits = (mips_opts.micromips ? 12
12031 : ISA_IS_R6 (mips_opts.isa) ? 9
12032 : 16);
252b5132
RH
12033 goto ld;
12034 case M_LWU_AB:
12035 s = "lwu";
df58fc94 12036 fmt = MEM12_FMT;
7f3c4072 12037 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12038 goto ld;
12039 case M_LWP_AB:
df58fc94
RS
12040 gas_assert (mips_opts.micromips);
12041 s = "lwp";
12042 fmt = "t,~(b)";
7f3c4072 12043 offbits = 12;
df58fc94
RS
12044 lp = 1;
12045 goto ld;
12046 case M_LDP_AB:
df58fc94
RS
12047 gas_assert (mips_opts.micromips);
12048 s = "ldp";
12049 fmt = "t,~(b)";
7f3c4072 12050 offbits = 12;
df58fc94
RS
12051 lp = 1;
12052 goto ld;
a45328b9
AB
12053 case M_LLDP_AB:
12054 case M_LLWP_AB:
41cee089 12055 case M_LLWPE_AB:
a45328b9
AB
12056 s = ip->insn_mo->name;
12057 fmt = "t,d,s";
12058 ll_sc_paired = 1;
12059 offbits = 0;
12060 goto ld;
df58fc94 12061 case M_LWM_AB:
df58fc94
RS
12062 gas_assert (mips_opts.micromips);
12063 s = "lwm";
12064 fmt = "n,~(b)";
7f3c4072 12065 offbits = 12;
df58fc94
RS
12066 goto ld_st;
12067 case M_LDM_AB:
df58fc94
RS
12068 gas_assert (mips_opts.micromips);
12069 s = "ldm";
12070 fmt = "n,~(b)";
7f3c4072 12071 offbits = 12;
df58fc94
RS
12072 goto ld_st;
12073
252b5132 12074 ld:
a45328b9
AB
12075 /* Try to use one the the load registers to compute the base address.
12076 We don't want to use $0 as tempreg. */
12077 if (ll_sc_paired)
12078 {
12079 if ((op[0] == ZERO && op[3] == op[1])
12080 || (op[1] == ZERO && op[3] == op[0])
12081 || (op[0] == ZERO && op[1] == ZERO))
12082 goto ld_st;
12083 else if (op[0] != op[3] && op[0] != ZERO)
12084 tempreg = op[0];
12085 else
12086 tempreg = op[1];
12087 }
252b5132 12088 else
a45328b9
AB
12089 {
12090 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
12091 goto ld_st;
12092 else
12093 tempreg = op[0] + lp;
12094 }
df58fc94
RS
12095 goto ld_noat;
12096
252b5132
RH
12097 case M_SB_AB:
12098 s = "sb";
df58fc94
RS
12099 fmt = "t,o(b)";
12100 goto ld_st;
252b5132
RH
12101 case M_SH_AB:
12102 s = "sh";
df58fc94
RS
12103 fmt = "t,o(b)";
12104 goto ld_st;
252b5132
RH
12105 case M_SW_AB:
12106 s = "sw";
df58fc94
RS
12107 fmt = "t,o(b)";
12108 goto ld_st;
252b5132 12109 case M_SWC0_AB:
df58fc94 12110 gas_assert (!mips_opts.micromips);
252b5132 12111 s = "swc0";
df58fc94 12112 fmt = "E,o(b)";
bdaaa2e1 12113 /* Itbl support may require additional care here. */
252b5132 12114 coproc = 1;
df58fc94 12115 goto ld_st;
252b5132
RH
12116 case M_SWC1_AB:
12117 s = "swc1";
df58fc94 12118 fmt = "T,o(b)";
bdaaa2e1 12119 /* Itbl support may require additional care here. */
252b5132 12120 coproc = 1;
df58fc94 12121 goto ld_st;
252b5132
RH
12122 case M_SWC2_AB:
12123 s = "swc2";
df58fc94 12124 fmt = COP12_FMT;
7361da2c
AB
12125 offbits = (mips_opts.micromips ? 12
12126 : ISA_IS_R6 (mips_opts.isa) ? 11
12127 : 16);
bdaaa2e1 12128 /* Itbl support may require additional care here. */
252b5132 12129 coproc = 1;
df58fc94 12130 goto ld_st;
252b5132 12131 case M_SWC3_AB:
df58fc94 12132 gas_assert (!mips_opts.micromips);
252b5132 12133 s = "swc3";
df58fc94 12134 fmt = "E,o(b)";
bdaaa2e1 12135 /* Itbl support may require additional care here. */
252b5132 12136 coproc = 1;
df58fc94 12137 goto ld_st;
252b5132
RH
12138 case M_SWL_AB:
12139 s = "swl";
df58fc94 12140 fmt = MEM12_FMT;
7f3c4072 12141 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12142 goto ld_st;
252b5132
RH
12143 case M_SWR_AB:
12144 s = "swr";
df58fc94 12145 fmt = MEM12_FMT;
7f3c4072 12146 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12147 goto ld_st;
252b5132
RH
12148 case M_SC_AB:
12149 s = "sc";
7361da2c
AB
12150 fmt = LL_SC_FMT;
12151 offbits = (mips_opts.micromips ? 12
12152 : ISA_IS_R6 (mips_opts.isa) ? 9
12153 : 16);
df58fc94 12154 goto ld_st;
252b5132
RH
12155 case M_SCD_AB:
12156 s = "scd";
7361da2c
AB
12157 fmt = LL_SC_FMT;
12158 offbits = (mips_opts.micromips ? 12
12159 : ISA_IS_R6 (mips_opts.isa) ? 9
12160 : 16);
df58fc94 12161 goto ld_st;
a45328b9
AB
12162 case M_SCDP_AB:
12163 case M_SCWP_AB:
41cee089 12164 case M_SCWPE_AB:
a45328b9
AB
12165 s = ip->insn_mo->name;
12166 fmt = "t,d,s";
12167 ll_sc_paired = 1;
12168 offbits = 0;
12169 goto ld_st;
d43b4baf
TS
12170 case M_CACHE_AB:
12171 s = "cache";
7361da2c
AB
12172 fmt = (mips_opts.micromips ? "k,~(b)"
12173 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12174 : "k,o(b)");
12175 offbits = (mips_opts.micromips ? 12
12176 : ISA_IS_R6 (mips_opts.isa) ? 9
12177 : 16);
7f3c4072
CM
12178 goto ld_st;
12179 case M_CACHEE_AB:
7f3c4072
CM
12180 s = "cachee";
12181 fmt = "k,+j(b)";
12182 offbits = 9;
df58fc94 12183 goto ld_st;
3eebd5eb
MR
12184 case M_PREF_AB:
12185 s = "pref";
7361da2c
AB
12186 fmt = (mips_opts.micromips ? "k,~(b)"
12187 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
12188 : "k,o(b)");
12189 offbits = (mips_opts.micromips ? 12
12190 : ISA_IS_R6 (mips_opts.isa) ? 9
12191 : 16);
7f3c4072
CM
12192 goto ld_st;
12193 case M_PREFE_AB:
7f3c4072
CM
12194 s = "prefe";
12195 fmt = "k,+j(b)";
12196 offbits = 9;
df58fc94 12197 goto ld_st;
252b5132 12198 case M_SDC1_AB:
252b5132 12199 s = "sdc1";
df58fc94 12200 fmt = "T,o(b)";
252b5132 12201 coproc = 1;
bdaaa2e1 12202 /* Itbl support may require additional care here. */
df58fc94 12203 goto ld_st;
252b5132
RH
12204 case M_SDC2_AB:
12205 s = "sdc2";
df58fc94 12206 fmt = COP12_FMT;
7361da2c
AB
12207 offbits = (mips_opts.micromips ? 12
12208 : ISA_IS_R6 (mips_opts.isa) ? 11
12209 : 16);
c77c0862
RS
12210 /* Itbl support may require additional care here. */
12211 coproc = 1;
12212 goto ld_st;
12213 case M_SQC2_AB:
c77c0862 12214 s = "sqc2";
14daeee3 12215 fmt = "+7,o(b)";
bdaaa2e1 12216 /* Itbl support may require additional care here. */
252b5132 12217 coproc = 1;
df58fc94 12218 goto ld_st;
252b5132 12219 case M_SDC3_AB:
df58fc94 12220 gas_assert (!mips_opts.micromips);
252b5132 12221 s = "sdc3";
df58fc94 12222 fmt = "E,o(b)";
bdaaa2e1 12223 /* Itbl support may require additional care here. */
252b5132 12224 coproc = 1;
df58fc94 12225 goto ld_st;
252b5132
RH
12226 case M_SDL_AB:
12227 s = "sdl";
df58fc94 12228 fmt = MEM12_FMT;
7f3c4072 12229 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12230 goto ld_st;
252b5132
RH
12231 case M_SDR_AB:
12232 s = "sdr";
df58fc94 12233 fmt = MEM12_FMT;
7f3c4072 12234 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12235 goto ld_st;
12236 case M_SWP_AB:
df58fc94
RS
12237 gas_assert (mips_opts.micromips);
12238 s = "swp";
12239 fmt = "t,~(b)";
7f3c4072 12240 offbits = 12;
df58fc94
RS
12241 goto ld_st;
12242 case M_SDP_AB:
df58fc94
RS
12243 gas_assert (mips_opts.micromips);
12244 s = "sdp";
12245 fmt = "t,~(b)";
7f3c4072 12246 offbits = 12;
df58fc94
RS
12247 goto ld_st;
12248 case M_SWM_AB:
df58fc94
RS
12249 gas_assert (mips_opts.micromips);
12250 s = "swm";
12251 fmt = "n,~(b)";
7f3c4072 12252 offbits = 12;
df58fc94
RS
12253 goto ld_st;
12254 case M_SDM_AB:
df58fc94
RS
12255 gas_assert (mips_opts.micromips);
12256 s = "sdm";
12257 fmt = "n,~(b)";
7f3c4072 12258 offbits = 12;
df58fc94
RS
12259
12260 ld_st:
8fc2e39e 12261 tempreg = AT;
df58fc94 12262 ld_noat:
a45328b9 12263 breg = ll_sc_paired ? op[3] : op[2];
f2ae14a1
RS
12264 if (small_offset_p (0, align, 16))
12265 {
12266 /* The first case exists for M_LD_AB and M_SD_AB, which are
12267 macros for o32 but which should act like normal instructions
12268 otherwise. */
12269 if (offbits == 16)
c0ebe874 12270 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12271 offset_reloc[1], offset_reloc[2], breg);
12272 else if (small_offset_p (0, align, offbits))
12273 {
12274 if (offbits == 0)
a45328b9
AB
12275 {
12276 if (ll_sc_paired)
12277 macro_build (NULL, s, fmt, op[0], op[1], breg);
12278 else
12279 macro_build (NULL, s, fmt, op[0], breg);
12280 }
f2ae14a1 12281 else
c0ebe874 12282 macro_build (NULL, s, fmt, op[0],
c8276761 12283 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12284 }
12285 else
12286 {
12287 if (tempreg == AT)
12288 used_at = 1;
12289 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12290 tempreg, breg, -1, offset_reloc[0],
12291 offset_reloc[1], offset_reloc[2]);
12292 if (offbits == 0)
a45328b9
AB
12293 {
12294 if (ll_sc_paired)
12295 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12296 else
12297 macro_build (NULL, s, fmt, op[0], tempreg);
12298 }
f2ae14a1 12299 else
c0ebe874 12300 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12301 }
12302 break;
12303 }
12304
12305 if (tempreg == AT)
12306 used_at = 1;
12307
252b5132
RH
12308 if (offset_expr.X_op != O_constant
12309 && offset_expr.X_op != O_symbol)
12310 {
1661c76c 12311 as_bad (_("expression too complex"));
252b5132
RH
12312 offset_expr.X_op = O_constant;
12313 }
12314
2051e8c4
MR
12315 if (HAVE_32BIT_ADDRESSES
12316 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71 12317 {
f493c217
AM
12318 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
12319 offset_expr.X_add_number);
55e08f71 12320 }
2051e8c4 12321
252b5132
RH
12322 /* A constant expression in PIC code can be handled just as it
12323 is in non PIC code. */
aed1a261
RS
12324 if (offset_expr.X_op == O_constant)
12325 {
f2ae14a1
RS
12326 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12327 offbits == 0 ? 16 : offbits);
12328 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12329
f2ae14a1
RS
12330 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12331 if (breg != 0)
12332 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12333 tempreg, tempreg, breg);
7f3c4072 12334 if (offbits == 0)
dd6a37e7 12335 {
f2ae14a1 12336 if (offset_expr.X_add_number != 0)
dd6a37e7 12337 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12338 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
a45328b9
AB
12339 if (ll_sc_paired)
12340 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12341 else
12342 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12343 }
7f3c4072 12344 else if (offbits == 16)
c0ebe874 12345 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12346 else
c0ebe874 12347 macro_build (NULL, s, fmt, op[0],
c8276761 12348 (int) offset_expr.X_add_number, tempreg);
df58fc94 12349 }
7f3c4072 12350 else if (offbits != 16)
df58fc94 12351 {
7f3c4072 12352 /* The offset field is too narrow to be used for a low-part
2b0f3761 12353 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12354 register. */
12355 load_address (tempreg, &offset_expr, &used_at);
12356 if (breg != 0)
12357 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12358 tempreg, tempreg, breg);
7f3c4072 12359 if (offbits == 0)
a45328b9
AB
12360 {
12361 if (ll_sc_paired)
12362 macro_build (NULL, s, fmt, op[0], op[1], tempreg);
12363 else
12364 macro_build (NULL, s, fmt, op[0], tempreg);
12365 }
dd6a37e7 12366 else
c0ebe874 12367 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12368 }
12369 else if (mips_pic == NO_PIC)
252b5132
RH
12370 {
12371 /* If this is a reference to a GP relative symbol, and there
12372 is no base register, we want
c0ebe874 12373 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12374 Otherwise, if there is no base register, we want
12375 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12376 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12377 If we have a constant, we need two instructions anyhow,
12378 so we always use the latter form.
12379
12380 If we have a base register, and this is a reference to a
12381 GP relative symbol, we want
12382 addu $tempreg,$breg,$gp
c0ebe874 12383 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12384 Otherwise we want
12385 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12386 addu $tempreg,$tempreg,$breg
c0ebe874 12387 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12388 With a constant we always use the latter case.
76b3015f 12389
d6bc6245
TS
12390 With 64bit address space and no base register and $at usable,
12391 we want
12392 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12393 lui $at,<sym> (BFD_RELOC_HI16_S)
12394 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12395 dsll32 $tempreg,0
12396 daddu $tempreg,$at
c0ebe874 12397 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12398 If we have a base register, we want
12399 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12400 lui $at,<sym> (BFD_RELOC_HI16_S)
12401 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12402 daddu $at,$breg
12403 dsll32 $tempreg,0
12404 daddu $tempreg,$at
c0ebe874 12405 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12406
12407 Without $at we can't generate the optimal path for superscalar
12408 processors here since this would require two temporary registers.
12409 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12410 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12411 dsll $tempreg,16
12412 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12413 dsll $tempreg,16
c0ebe874 12414 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12415 If we have a base register, we want
12416 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12417 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12418 dsll $tempreg,16
12419 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12420 dsll $tempreg,16
12421 daddu $tempreg,$tempreg,$breg
c0ebe874 12422 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12423
6caf9ef4 12424 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12425 the same sequence as in 32bit address space. */
12426 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12427 {
aed1a261 12428 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12429 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12430 {
12431 relax_start (offset_expr.X_add_symbol);
12432 if (breg == 0)
12433 {
c0ebe874 12434 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12435 BFD_RELOC_GPREL16, mips_gp_register);
12436 }
12437 else
12438 {
12439 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12440 tempreg, breg, mips_gp_register);
c0ebe874 12441 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12442 BFD_RELOC_GPREL16, tempreg);
12443 }
12444 relax_switch ();
12445 }
d6bc6245 12446
741fe287 12447 if (used_at == 0 && mips_opts.at)
d6bc6245 12448 {
df58fc94 12449 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12450 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12451 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12452 BFD_RELOC_HI16_S);
12453 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12454 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12455 if (breg != 0)
67c0d1eb 12456 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12457 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12458 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12459 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12460 tempreg);
d6bc6245
TS
12461 used_at = 1;
12462 }
12463 else
12464 {
df58fc94 12465 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12466 BFD_RELOC_MIPS_HIGHEST);
12467 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12468 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12469 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12470 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12471 tempreg, BFD_RELOC_HI16_S);
df58fc94 12472 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12473 if (breg != 0)
67c0d1eb 12474 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12475 tempreg, tempreg, breg);
c0ebe874 12476 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12477 BFD_RELOC_LO16, tempreg);
d6bc6245 12478 }
6caf9ef4
TS
12479
12480 if (mips_relax.sequence)
12481 relax_end ();
8fc2e39e 12482 break;
d6bc6245 12483 }
256ab948 12484
252b5132
RH
12485 if (breg == 0)
12486 {
67c0d1eb 12487 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12488 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12489 {
4d7206a2 12490 relax_start (offset_expr.X_add_symbol);
c0ebe874 12491 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12492 mips_gp_register);
4d7206a2 12493 relax_switch ();
252b5132 12494 }
67c0d1eb 12495 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12496 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12497 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12498 if (mips_relax.sequence)
12499 relax_end ();
252b5132
RH
12500 }
12501 else
12502 {
67c0d1eb 12503 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12504 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12505 {
4d7206a2 12506 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12508 tempreg, breg, mips_gp_register);
c0ebe874 12509 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12510 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12511 relax_switch ();
252b5132 12512 }
67c0d1eb
RS
12513 macro_build_lui (&offset_expr, tempreg);
12514 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12515 tempreg, tempreg, breg);
c0ebe874 12516 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12517 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12518 if (mips_relax.sequence)
12519 relax_end ();
252b5132
RH
12520 }
12521 }
0a44bf69 12522 else if (!mips_big_got)
252b5132 12523 {
ed6fb7bd 12524 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12525
252b5132
RH
12526 /* If this is a reference to an external symbol, we want
12527 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12528 nop
c0ebe874 12529 <op> op[0],0($tempreg)
252b5132
RH
12530 Otherwise we want
12531 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12532 nop
12533 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12534 <op> op[0],0($tempreg)
f5040a92
AO
12535
12536 For NewABI, we want
12537 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12538 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12539
252b5132
RH
12540 If there is a base register, we add it to $tempreg before
12541 the <op>. If there is a constant, we stick it in the
12542 <op> instruction. We don't handle constants larger than
12543 16 bits, because we have no way to load the upper 16 bits
12544 (actually, we could handle them for the subset of cases
12545 in which we are not using $at). */
9c2799c2 12546 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12547 if (HAVE_NEWABI)
12548 {
67c0d1eb
RS
12549 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12550 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12551 if (breg != 0)
67c0d1eb 12552 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12553 tempreg, tempreg, breg);
c0ebe874 12554 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12555 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12556 break;
12557 }
252b5132
RH
12558 expr1.X_add_number = offset_expr.X_add_number;
12559 offset_expr.X_add_number = 0;
12560 if (expr1.X_add_number < -0x8000
12561 || expr1.X_add_number >= 0x8000)
12562 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12564 lw_reloc_type, mips_gp_register);
269137b2 12565 load_delay_nop ();
4d7206a2
RS
12566 relax_start (offset_expr.X_add_symbol);
12567 relax_switch ();
67c0d1eb
RS
12568 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12569 tempreg, BFD_RELOC_LO16);
4d7206a2 12570 relax_end ();
252b5132 12571 if (breg != 0)
67c0d1eb 12572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12573 tempreg, tempreg, breg);
c0ebe874 12574 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12575 }
0a44bf69 12576 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12577 {
67c0d1eb 12578 int gpdelay;
252b5132
RH
12579
12580 /* If this is a reference to an external symbol, we want
12581 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12582 addu $tempreg,$tempreg,$gp
12583 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12584 <op> op[0],0($tempreg)
252b5132
RH
12585 Otherwise we want
12586 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12587 nop
12588 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12589 <op> op[0],0($tempreg)
252b5132
RH
12590 If there is a base register, we add it to $tempreg before
12591 the <op>. If there is a constant, we stick it in the
12592 <op> instruction. We don't handle constants larger than
12593 16 bits, because we have no way to load the upper 16 bits
12594 (actually, we could handle them for the subset of cases
f5040a92 12595 in which we are not using $at). */
9c2799c2 12596 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12597 expr1.X_add_number = offset_expr.X_add_number;
12598 offset_expr.X_add_number = 0;
12599 if (expr1.X_add_number < -0x8000
12600 || expr1.X_add_number >= 0x8000)
12601 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12602 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12603 relax_start (offset_expr.X_add_symbol);
df58fc94 12604 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12605 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12606 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12607 mips_gp_register);
12608 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12609 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12610 relax_switch ();
67c0d1eb
RS
12611 if (gpdelay)
12612 macro_build (NULL, "nop", "");
12613 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12614 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12615 load_delay_nop ();
67c0d1eb
RS
12616 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12617 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12618 relax_end ();
12619
252b5132 12620 if (breg != 0)
67c0d1eb 12621 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12622 tempreg, tempreg, breg);
c0ebe874 12623 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12624 }
0a44bf69 12625 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12626 {
f5040a92
AO
12627 /* If this is a reference to an external symbol, we want
12628 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12629 add $tempreg,$tempreg,$gp
12630 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12631 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12632 Otherwise, for local symbols, we want:
12633 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12634 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12635 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12636 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12637 offset_expr.X_add_number = 0;
12638 if (expr1.X_add_number < -0x8000
12639 || expr1.X_add_number >= 0x8000)
12640 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12641 relax_start (offset_expr.X_add_symbol);
df58fc94 12642 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12643 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12644 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12645 mips_gp_register);
12646 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12647 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12648 if (breg != 0)
67c0d1eb 12649 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12650 tempreg, tempreg, breg);
c0ebe874 12651 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12652
4d7206a2 12653 relax_switch ();
f5040a92 12654 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12655 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12656 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12657 if (breg != 0)
67c0d1eb 12658 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12659 tempreg, tempreg, breg);
c0ebe874 12660 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12661 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12662 relax_end ();
f5040a92 12663 }
252b5132
RH
12664 else
12665 abort ();
12666
252b5132
RH
12667 break;
12668
833794fc
MR
12669 case M_JRADDIUSP:
12670 gas_assert (mips_opts.micromips);
12671 gas_assert (mips_opts.insn32);
12672 start_noreorder ();
12673 macro_build (NULL, "jr", "s", RA);
c0ebe874 12674 expr1.X_add_number = op[0] << 2;
833794fc
MR
12675 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12676 end_noreorder ();
12677 break;
12678
12679 case M_JRC:
12680 gas_assert (mips_opts.micromips);
12681 gas_assert (mips_opts.insn32);
c0ebe874 12682 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12683 if (mips_opts.noreorder)
12684 macro_build (NULL, "nop", "");
12685 break;
12686
252b5132
RH
12687 case M_LI:
12688 case M_LI_S:
c0ebe874 12689 load_register (op[0], &imm_expr, 0);
8fc2e39e 12690 break;
252b5132
RH
12691
12692 case M_DLI:
c0ebe874 12693 load_register (op[0], &imm_expr, 1);
8fc2e39e 12694 break;
252b5132
RH
12695
12696 case M_LI_SS:
12697 if (imm_expr.X_op == O_constant)
12698 {
8fc2e39e 12699 used_at = 1;
67c0d1eb 12700 load_register (AT, &imm_expr, 0);
c0ebe874 12701 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12702 break;
12703 }
12704 else
12705 {
b0e6f033
RS
12706 gas_assert (imm_expr.X_op == O_absent
12707 && offset_expr.X_op == O_symbol
90ecf173
MR
12708 && strcmp (segment_name (S_GET_SEGMENT
12709 (offset_expr.X_add_symbol)),
12710 ".lit4") == 0
12711 && offset_expr.X_add_number == 0);
c0ebe874 12712 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12713 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12714 break;
252b5132
RH
12715 }
12716
12717 case M_LI_D:
ca4e0257
RS
12718 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12719 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12720 order 32 bits of the value and the low order 32 bits are either
12721 zero or in OFFSET_EXPR. */
b0e6f033 12722 if (imm_expr.X_op == O_constant)
252b5132 12723 {
bad1aba3 12724 if (GPR_SIZE == 64)
c0ebe874 12725 load_register (op[0], &imm_expr, 1);
252b5132
RH
12726 else
12727 {
12728 int hreg, lreg;
12729
12730 if (target_big_endian)
12731 {
c0ebe874
RS
12732 hreg = op[0];
12733 lreg = op[0] + 1;
252b5132
RH
12734 }
12735 else
12736 {
c0ebe874
RS
12737 hreg = op[0] + 1;
12738 lreg = op[0];
252b5132
RH
12739 }
12740
12741 if (hreg <= 31)
67c0d1eb 12742 load_register (hreg, &imm_expr, 0);
252b5132
RH
12743 if (lreg <= 31)
12744 {
12745 if (offset_expr.X_op == O_absent)
67c0d1eb 12746 move_register (lreg, 0);
252b5132
RH
12747 else
12748 {
9c2799c2 12749 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12750 load_register (lreg, &offset_expr, 0);
252b5132
RH
12751 }
12752 }
12753 }
8fc2e39e 12754 break;
252b5132 12755 }
b0e6f033 12756 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12757
12758 /* We know that sym is in the .rdata section. First we get the
12759 upper 16 bits of the address. */
12760 if (mips_pic == NO_PIC)
12761 {
67c0d1eb 12762 macro_build_lui (&offset_expr, AT);
8fc2e39e 12763 used_at = 1;
252b5132 12764 }
0a44bf69 12765 else
252b5132 12766 {
67c0d1eb
RS
12767 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12768 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12769 used_at = 1;
252b5132 12770 }
bdaaa2e1 12771
252b5132 12772 /* Now we load the register(s). */
bad1aba3 12773 if (GPR_SIZE == 64)
8fc2e39e
TS
12774 {
12775 used_at = 1;
c0ebe874
RS
12776 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12777 BFD_RELOC_LO16, AT);
8fc2e39e 12778 }
252b5132
RH
12779 else
12780 {
8fc2e39e 12781 used_at = 1;
c0ebe874
RS
12782 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12783 BFD_RELOC_LO16, AT);
12784 if (op[0] != RA)
252b5132
RH
12785 {
12786 /* FIXME: How in the world do we deal with the possible
12787 overflow here? */
12788 offset_expr.X_add_number += 4;
67c0d1eb 12789 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12790 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12791 }
12792 }
252b5132
RH
12793 break;
12794
12795 case M_LI_DD:
ca4e0257
RS
12796 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12797 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12798 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12799 the value and the low order 32 bits are either zero or in
12800 OFFSET_EXPR. */
b0e6f033 12801 if (imm_expr.X_op == O_constant)
252b5132 12802 {
9b444f95
FS
12803 tempreg = ZERO;
12804 if (((FPR_SIZE == 64 && GPR_SIZE == 64)
12805 || !ISA_HAS_MXHC1 (mips_opts.isa))
12806 && imm_expr.X_add_number != 0)
12807 {
12808 used_at = 1;
12809 tempreg = AT;
12810 load_register (AT, &imm_expr, FPR_SIZE == 64);
12811 }
351cdf24 12812 if (FPR_SIZE == 64 && GPR_SIZE == 64)
9b444f95 12813 macro_build (NULL, "dmtc1", "t,S", tempreg, op[0]);
252b5132
RH
12814 else
12815 {
9b444f95
FS
12816 if (!ISA_HAS_MXHC1 (mips_opts.isa))
12817 {
12818 if (FPR_SIZE != 32)
12819 as_bad (_("Unable to generate `%s' compliant code "
12820 "without mthc1"),
12821 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12822 else
12823 macro_build (NULL, "mtc1", "t,G", tempreg, op[0] + 1);
12824 }
252b5132 12825 if (offset_expr.X_op == O_absent)
c0ebe874 12826 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12827 else
12828 {
9c2799c2 12829 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12830 load_register (AT, &offset_expr, 0);
c0ebe874 12831 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132 12832 }
9b444f95
FS
12833 if (ISA_HAS_MXHC1 (mips_opts.isa))
12834 {
12835 if (imm_expr.X_add_number != 0)
12836 {
12837 used_at = 1;
12838 tempreg = AT;
12839 load_register (AT, &imm_expr, 0);
12840 }
12841 macro_build (NULL, "mthc1", "t,G", tempreg, op[0]);
12842 }
252b5132
RH
12843 }
12844 break;
12845 }
12846
b0e6f033
RS
12847 gas_assert (imm_expr.X_op == O_absent
12848 && offset_expr.X_op == O_symbol
90ecf173 12849 && offset_expr.X_add_number == 0);
252b5132
RH
12850 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12851 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12852 {
12853 op[2] = mips_gp_register;
f2ae14a1
RS
12854 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12855 offset_reloc[1] = BFD_RELOC_UNUSED;
12856 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12857 }
12858 else
12859 {
9c2799c2 12860 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12861 used_at = 1;
0a44bf69 12862 if (mips_pic != NO_PIC)
67c0d1eb
RS
12863 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12864 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12865 else
12866 {
12867 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12868 macro_build_lui (&offset_expr, AT);
252b5132 12869 }
bdaaa2e1 12870
c0ebe874 12871 op[2] = AT;
f2ae14a1
RS
12872 offset_reloc[0] = BFD_RELOC_LO16;
12873 offset_reloc[1] = BFD_RELOC_UNUSED;
12874 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12875 }
f2ae14a1 12876 align = 8;
6f2117ba 12877 /* Fall through. */
c4a68bea 12878
252b5132 12879 case M_L_DAB:
6f2117ba
PH
12880 /* The MIPS assembler seems to check for X_add_number not
12881 being double aligned and generating:
12882 lui at,%hi(foo+1)
12883 addu at,at,v1
12884 addiu at,at,%lo(foo+1)
12885 lwc1 f2,0(at)
12886 lwc1 f3,4(at)
12887 But, the resulting address is the same after relocation so why
12888 generate the extra instruction? */
bdaaa2e1 12889 /* Itbl support may require additional care here. */
252b5132 12890 coproc = 1;
df58fc94 12891 fmt = "T,o(b)";
0aa27725 12892 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12893 {
12894 s = "ldc1";
df58fc94 12895 goto ld_st;
252b5132 12896 }
252b5132 12897 s = "lwc1";
252b5132
RH
12898 goto ldd_std;
12899
12900 case M_S_DAB:
df58fc94
RS
12901 gas_assert (!mips_opts.micromips);
12902 /* Itbl support may require additional care here. */
12903 coproc = 1;
12904 fmt = "T,o(b)";
0aa27725 12905 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12906 {
12907 s = "sdc1";
df58fc94 12908 goto ld_st;
252b5132 12909 }
252b5132 12910 s = "swc1";
252b5132
RH
12911 goto ldd_std;
12912
e407c74b
NC
12913 case M_LQ_AB:
12914 fmt = "t,o(b)";
12915 s = "lq";
12916 goto ld;
12917
12918 case M_SQ_AB:
12919 fmt = "t,o(b)";
12920 s = "sq";
12921 goto ld_st;
12922
252b5132 12923 case M_LD_AB:
df58fc94 12924 fmt = "t,o(b)";
bad1aba3 12925 if (GPR_SIZE == 64)
252b5132
RH
12926 {
12927 s = "ld";
12928 goto ld;
12929 }
252b5132 12930 s = "lw";
252b5132
RH
12931 goto ldd_std;
12932
12933 case M_SD_AB:
df58fc94 12934 fmt = "t,o(b)";
bad1aba3 12935 if (GPR_SIZE == 64)
252b5132
RH
12936 {
12937 s = "sd";
df58fc94 12938 goto ld_st;
252b5132 12939 }
252b5132 12940 s = "sw";
252b5132
RH
12941
12942 ldd_std:
f2ae14a1
RS
12943 /* Even on a big endian machine $fn comes before $fn+1. We have
12944 to adjust when loading from memory. We set coproc if we must
12945 load $fn+1 first. */
12946 /* Itbl support may require additional care here. */
12947 if (!target_big_endian)
12948 coproc = 0;
12949
c0ebe874 12950 breg = op[2];
f2ae14a1
RS
12951 if (small_offset_p (0, align, 16))
12952 {
12953 ep = &offset_expr;
12954 if (!small_offset_p (4, align, 16))
12955 {
12956 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12957 -1, offset_reloc[0], offset_reloc[1],
12958 offset_reloc[2]);
12959 expr1.X_add_number = 0;
12960 ep = &expr1;
12961 breg = AT;
12962 used_at = 1;
12963 offset_reloc[0] = BFD_RELOC_LO16;
12964 offset_reloc[1] = BFD_RELOC_UNUSED;
12965 offset_reloc[2] = BFD_RELOC_UNUSED;
12966 }
c0ebe874 12967 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12968 {
12969 ep->X_add_number += 4;
c0ebe874 12970 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12971 offset_reloc[1], offset_reloc[2], breg);
12972 ep->X_add_number -= 4;
c0ebe874 12973 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12974 offset_reloc[1], offset_reloc[2], breg);
12975 }
12976 else
12977 {
c0ebe874 12978 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12979 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12980 breg);
12981 ep->X_add_number += 4;
c0ebe874 12982 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12983 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12984 breg);
12985 }
12986 break;
12987 }
12988
252b5132
RH
12989 if (offset_expr.X_op != O_symbol
12990 && offset_expr.X_op != O_constant)
12991 {
1661c76c 12992 as_bad (_("expression too complex"));
252b5132
RH
12993 offset_expr.X_op = O_constant;
12994 }
12995
2051e8c4
MR
12996 if (HAVE_32BIT_ADDRESSES
12997 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71 12998 {
f493c217
AM
12999 as_bad (_("number (0x%" PRIx64 ") larger than 32 bits"),
13000 offset_expr.X_add_number);
55e08f71 13001 }
2051e8c4 13002
90ecf173 13003 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
13004 {
13005 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
13006 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
13007 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
13008 If we have a base register, we use this
13009 addu $at,$breg,$gp
c0ebe874
RS
13010 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
13011 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
13012 If this is not a GP relative symbol, we want
13013 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
13014 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13015 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13016 If there is a base register, we add it to $at after the
13017 lui instruction. If there is a constant, we always use
13018 the last case. */
39a59cf8
MR
13019 if (offset_expr.X_op == O_symbol
13020 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 13021 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 13022 {
4d7206a2 13023 relax_start (offset_expr.X_add_symbol);
252b5132
RH
13024 if (breg == 0)
13025 {
c9914766 13026 tempreg = mips_gp_register;
252b5132
RH
13027 }
13028 else
13029 {
67c0d1eb 13030 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13031 AT, breg, mips_gp_register);
252b5132 13032 tempreg = AT;
252b5132
RH
13033 used_at = 1;
13034 }
13035
beae10d5 13036 /* Itbl support may require additional care here. */
c0ebe874 13037 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13038 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13039 offset_expr.X_add_number += 4;
13040
13041 /* Set mips_optimize to 2 to avoid inserting an
13042 undesired nop. */
13043 hold_mips_optimize = mips_optimize;
13044 mips_optimize = 2;
beae10d5 13045 /* Itbl support may require additional care here. */
c0ebe874 13046 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13047 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
13048 mips_optimize = hold_mips_optimize;
13049
4d7206a2 13050 relax_switch ();
252b5132 13051
0970e49e 13052 offset_expr.X_add_number -= 4;
252b5132 13053 }
8fc2e39e 13054 used_at = 1;
f2ae14a1
RS
13055 if (offset_high_part (offset_expr.X_add_number, 16)
13056 != offset_high_part (offset_expr.X_add_number + 4, 16))
13057 {
13058 load_address (AT, &offset_expr, &used_at);
13059 offset_expr.X_op = O_constant;
13060 offset_expr.X_add_number = 0;
13061 }
13062 else
13063 macro_build_lui (&offset_expr, AT);
252b5132 13064 if (breg != 0)
67c0d1eb 13065 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13066 /* Itbl support may require additional care here. */
c0ebe874 13067 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13068 BFD_RELOC_LO16, AT);
252b5132
RH
13069 /* FIXME: How do we handle overflow here? */
13070 offset_expr.X_add_number += 4;
beae10d5 13071 /* Itbl support may require additional care here. */
c0ebe874 13072 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13073 BFD_RELOC_LO16, AT);
4d7206a2
RS
13074 if (mips_relax.sequence)
13075 relax_end ();
bdaaa2e1 13076 }
0a44bf69 13077 else if (!mips_big_got)
252b5132 13078 {
252b5132
RH
13079 /* If this is a reference to an external symbol, we want
13080 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13081 nop
c0ebe874
RS
13082 <op> op[0],0($at)
13083 <op> op[0]+1,4($at)
252b5132
RH
13084 Otherwise we want
13085 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13086 nop
c0ebe874
RS
13087 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13088 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13089 If there is a base register we add it to $at before the
13090 lwc1 instructions. If there is a constant we include it
13091 in the lwc1 instructions. */
13092 used_at = 1;
13093 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
13094 if (expr1.X_add_number < -0x8000
13095 || expr1.X_add_number >= 0x8000 - 4)
13096 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13097 load_got_offset (AT, &offset_expr);
269137b2 13098 load_delay_nop ();
252b5132 13099 if (breg != 0)
67c0d1eb 13100 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
13101
13102 /* Set mips_optimize to 2 to avoid inserting an undesired
13103 nop. */
13104 hold_mips_optimize = mips_optimize;
13105 mips_optimize = 2;
4d7206a2 13106
beae10d5 13107 /* Itbl support may require additional care here. */
4d7206a2 13108 relax_start (offset_expr.X_add_symbol);
c0ebe874 13109 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13110 BFD_RELOC_LO16, AT);
4d7206a2 13111 expr1.X_add_number += 4;
c0ebe874 13112 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13113 BFD_RELOC_LO16, AT);
4d7206a2 13114 relax_switch ();
c0ebe874 13115 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13116 BFD_RELOC_LO16, AT);
4d7206a2 13117 offset_expr.X_add_number += 4;
c0ebe874 13118 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13119 BFD_RELOC_LO16, AT);
4d7206a2 13120 relax_end ();
252b5132 13121
4d7206a2 13122 mips_optimize = hold_mips_optimize;
252b5132 13123 }
0a44bf69 13124 else if (mips_big_got)
252b5132 13125 {
67c0d1eb 13126 int gpdelay;
252b5132
RH
13127
13128 /* If this is a reference to an external symbol, we want
13129 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
13130 addu $at,$at,$gp
13131 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
13132 nop
c0ebe874
RS
13133 <op> op[0],0($at)
13134 <op> op[0]+1,4($at)
252b5132
RH
13135 Otherwise we want
13136 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
13137 nop
c0ebe874
RS
13138 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
13139 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
13140 If there is a base register we add it to $at before the
13141 lwc1 instructions. If there is a constant we include it
13142 in the lwc1 instructions. */
13143 used_at = 1;
13144 expr1.X_add_number = offset_expr.X_add_number;
13145 offset_expr.X_add_number = 0;
13146 if (expr1.X_add_number < -0x8000
13147 || expr1.X_add_number >= 0x8000 - 4)
13148 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 13149 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 13150 relax_start (offset_expr.X_add_symbol);
df58fc94 13151 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
13152 AT, BFD_RELOC_MIPS_GOT_HI16);
13153 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 13154 AT, AT, mips_gp_register);
67c0d1eb 13155 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 13156 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 13157 load_delay_nop ();
252b5132 13158 if (breg != 0)
67c0d1eb 13159 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13160 /* Itbl support may require additional care here. */
c0ebe874 13161 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 13162 BFD_RELOC_LO16, AT);
252b5132
RH
13163 expr1.X_add_number += 4;
13164
13165 /* Set mips_optimize to 2 to avoid inserting an undesired
13166 nop. */
13167 hold_mips_optimize = mips_optimize;
13168 mips_optimize = 2;
beae10d5 13169 /* Itbl support may require additional care here. */
c0ebe874 13170 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 13171 BFD_RELOC_LO16, AT);
252b5132
RH
13172 mips_optimize = hold_mips_optimize;
13173 expr1.X_add_number -= 4;
13174
4d7206a2
RS
13175 relax_switch ();
13176 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
13177 if (gpdelay)
13178 macro_build (NULL, "nop", "");
13179 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
13180 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 13181 load_delay_nop ();
252b5132 13182 if (breg != 0)
67c0d1eb 13183 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 13184 /* Itbl support may require additional care here. */
c0ebe874 13185 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 13186 BFD_RELOC_LO16, AT);
4d7206a2 13187 offset_expr.X_add_number += 4;
252b5132
RH
13188
13189 /* Set mips_optimize to 2 to avoid inserting an undesired
13190 nop. */
13191 hold_mips_optimize = mips_optimize;
13192 mips_optimize = 2;
beae10d5 13193 /* Itbl support may require additional care here. */
c0ebe874 13194 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 13195 BFD_RELOC_LO16, AT);
252b5132 13196 mips_optimize = hold_mips_optimize;
4d7206a2 13197 relax_end ();
252b5132 13198 }
252b5132
RH
13199 else
13200 abort ();
13201
252b5132 13202 break;
3739860c 13203
dd6a37e7 13204 case M_SAA_AB:
dd6a37e7 13205 s = "saa";
0db377d0 13206 goto saa_saad;
dd6a37e7 13207 case M_SAAD_AB:
dd6a37e7 13208 s = "saad";
0db377d0
MR
13209 saa_saad:
13210 gas_assert (!mips_opts.micromips);
7f3c4072 13211 offbits = 0;
dd6a37e7
AP
13212 fmt = "t,(b)";
13213 goto ld_st;
13214
252b5132
RH
13215 /* New code added to support COPZ instructions.
13216 This code builds table entries out of the macros in mip_opcodes.
13217 R4000 uses interlocks to handle coproc delays.
13218 Other chips (like the R3000) require nops to be inserted for delays.
13219
f72c8c98 13220 FIXME: Currently, we require that the user handle delays.
252b5132
RH
13221 In order to fill delay slots for non-interlocked chips,
13222 we must have a way to specify delays based on the coprocessor.
13223 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
13224 What are the side-effects of the cop instruction?
13225 What cache support might we have and what are its effects?
13226 Both coprocessor & memory require delays. how long???
bdaaa2e1 13227 What registers are read/set/modified?
252b5132
RH
13228
13229 If an itbl is provided to interpret cop instructions,
bdaaa2e1 13230 this knowledge can be encoded in the itbl spec. */
252b5132
RH
13231
13232 case M_COP0:
13233 s = "c0";
13234 goto copz;
13235 case M_COP1:
13236 s = "c1";
13237 goto copz;
13238 case M_COP2:
13239 s = "c2";
13240 goto copz;
13241 case M_COP3:
13242 s = "c3";
13243 copz:
df58fc94 13244 gas_assert (!mips_opts.micromips);
252b5132
RH
13245 /* For now we just do C (same as Cz). The parameter will be
13246 stored in insn_opcode by mips_ip. */
c8276761 13247 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 13248 break;
252b5132 13249
ea1fb5dc 13250 case M_MOVE:
c0ebe874 13251 move_register (op[0], op[1]);
8fc2e39e 13252 break;
ea1fb5dc 13253
833794fc
MR
13254 case M_MOVEP:
13255 gas_assert (mips_opts.micromips);
13256 gas_assert (mips_opts.insn32);
c0ebe874
RS
13257 move_register (micromips_to_32_reg_h_map1[op[0]],
13258 micromips_to_32_reg_m_map[op[1]]);
13259 move_register (micromips_to_32_reg_h_map2[op[0]],
13260 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13261 break;
13262
252b5132
RH
13263 case M_DMUL:
13264 dbl = 1;
1a0670f3 13265 /* Fall through. */
252b5132 13266 case M_MUL:
e407c74b 13267 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13268 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13269 op[2]);
e407c74b
NC
13270 else
13271 {
c0ebe874
RS
13272 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13273 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13274 }
8fc2e39e 13275 break;
252b5132
RH
13276
13277 case M_DMUL_I:
13278 dbl = 1;
1a0670f3 13279 /* Fall through. */
252b5132
RH
13280 case M_MUL_I:
13281 /* The MIPS assembler some times generates shifts and adds. I'm
13282 not trying to be that fancy. GCC should do this for us
13283 anyway. */
8fc2e39e 13284 used_at = 1;
67c0d1eb 13285 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13286 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13287 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13288 break;
13289
13290 case M_DMULO_I:
13291 dbl = 1;
1a0670f3 13292 /* Fall through. */
252b5132
RH
13293 case M_MULO_I:
13294 imm = 1;
13295 goto do_mulo;
13296
13297 case M_DMULO:
13298 dbl = 1;
1a0670f3 13299 /* Fall through. */
252b5132
RH
13300 case M_MULO:
13301 do_mulo:
7d10b47d 13302 start_noreorder ();
8fc2e39e 13303 used_at = 1;
252b5132 13304 if (imm)
67c0d1eb 13305 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13306 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13307 op[1], imm ? AT : op[2]);
13308 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13309 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13310 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13311 if (mips_trap)
c0ebe874 13312 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13313 else
13314 {
df58fc94
RS
13315 if (mips_opts.micromips)
13316 micromips_label_expr (&label_expr);
13317 else
13318 label_expr.X_add_number = 8;
c0ebe874 13319 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13320 macro_build (NULL, "nop", "");
df58fc94
RS
13321 macro_build (NULL, "break", BRK_FMT, 6);
13322 if (mips_opts.micromips)
13323 micromips_add_label ();
252b5132 13324 }
7d10b47d 13325 end_noreorder ();
c0ebe874 13326 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13327 break;
13328
13329 case M_DMULOU_I:
13330 dbl = 1;
1a0670f3 13331 /* Fall through. */
252b5132
RH
13332 case M_MULOU_I:
13333 imm = 1;
13334 goto do_mulou;
13335
13336 case M_DMULOU:
13337 dbl = 1;
1a0670f3 13338 /* Fall through. */
252b5132
RH
13339 case M_MULOU:
13340 do_mulou:
7d10b47d 13341 start_noreorder ();
8fc2e39e 13342 used_at = 1;
252b5132 13343 if (imm)
67c0d1eb
RS
13344 load_register (AT, &imm_expr, dbl);
13345 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13346 op[1], imm ? AT : op[2]);
df58fc94 13347 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13348 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13349 if (mips_trap)
df58fc94 13350 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13351 else
13352 {
df58fc94
RS
13353 if (mips_opts.micromips)
13354 micromips_label_expr (&label_expr);
13355 else
13356 label_expr.X_add_number = 8;
13357 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13358 macro_build (NULL, "nop", "");
df58fc94
RS
13359 macro_build (NULL, "break", BRK_FMT, 6);
13360 if (mips_opts.micromips)
13361 micromips_add_label ();
252b5132 13362 }
7d10b47d 13363 end_noreorder ();
252b5132
RH
13364 break;
13365
771c7ce4 13366 case M_DROL:
fef14a42 13367 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13368 {
c0ebe874 13369 if (op[0] == op[1])
82dd0097
CD
13370 {
13371 tempreg = AT;
13372 used_at = 1;
13373 }
13374 else
c0ebe874
RS
13375 tempreg = op[0];
13376 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13377 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13378 break;
82dd0097 13379 }
8fc2e39e 13380 used_at = 1;
c0ebe874
RS
13381 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13382 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13383 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13384 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13385 break;
13386
252b5132 13387 case M_ROL:
fef14a42 13388 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13389 {
c0ebe874 13390 if (op[0] == op[1])
82dd0097
CD
13391 {
13392 tempreg = AT;
13393 used_at = 1;
13394 }
13395 else
c0ebe874
RS
13396 tempreg = op[0];
13397 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13398 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13399 break;
82dd0097 13400 }
8fc2e39e 13401 used_at = 1;
c0ebe874
RS
13402 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13403 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13404 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13405 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13406 break;
13407
771c7ce4
TS
13408 case M_DROL_I:
13409 {
13410 unsigned int rot;
e0471c16
TS
13411 const char *l;
13412 const char *rr;
771c7ce4 13413
771c7ce4 13414 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13415 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13416 {
13417 rot = (64 - rot) & 0x3f;
13418 if (rot >= 32)
c0ebe874 13419 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13420 else
c0ebe874 13421 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13422 break;
60b63b72 13423 }
483fc7cd 13424 if (rot == 0)
483fc7cd 13425 {
c0ebe874 13426 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13427 break;
483fc7cd 13428 }
82dd0097 13429 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13430 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13431 rot &= 0x1f;
8fc2e39e 13432 used_at = 1;
c0ebe874
RS
13433 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13434 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13435 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13436 }
13437 break;
13438
252b5132 13439 case M_ROL_I:
771c7ce4
TS
13440 {
13441 unsigned int rot;
13442
771c7ce4 13443 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13444 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13445 {
c0ebe874
RS
13446 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13447 (32 - rot) & 0x1f);
8fc2e39e 13448 break;
60b63b72 13449 }
483fc7cd 13450 if (rot == 0)
483fc7cd 13451 {
c0ebe874 13452 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13453 break;
483fc7cd 13454 }
8fc2e39e 13455 used_at = 1;
c0ebe874
RS
13456 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13457 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13458 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13459 }
13460 break;
13461
13462 case M_DROR:
fef14a42 13463 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13464 {
c0ebe874 13465 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13466 break;
82dd0097 13467 }
8fc2e39e 13468 used_at = 1;
c0ebe874
RS
13469 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13470 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13471 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13472 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13473 break;
13474
13475 case M_ROR:
fef14a42 13476 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13477 {
c0ebe874 13478 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13479 break;
82dd0097 13480 }
8fc2e39e 13481 used_at = 1;
c0ebe874
RS
13482 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13483 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13484 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13485 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13486 break;
13487
771c7ce4
TS
13488 case M_DROR_I:
13489 {
13490 unsigned int rot;
e0471c16
TS
13491 const char *l;
13492 const char *rr;
771c7ce4 13493
771c7ce4 13494 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13495 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13496 {
13497 if (rot >= 32)
c0ebe874 13498 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13499 else
c0ebe874 13500 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13501 break;
82dd0097 13502 }
483fc7cd 13503 if (rot == 0)
483fc7cd 13504 {
c0ebe874 13505 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13506 break;
483fc7cd 13507 }
91d6fa6a 13508 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13509 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13510 rot &= 0x1f;
8fc2e39e 13511 used_at = 1;
c0ebe874
RS
13512 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13513 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13514 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13515 }
13516 break;
13517
252b5132 13518 case M_ROR_I:
771c7ce4
TS
13519 {
13520 unsigned int rot;
13521
771c7ce4 13522 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13523 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13524 {
c0ebe874 13525 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13526 break;
82dd0097 13527 }
483fc7cd 13528 if (rot == 0)
483fc7cd 13529 {
c0ebe874 13530 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13531 break;
483fc7cd 13532 }
8fc2e39e 13533 used_at = 1;
c0ebe874
RS
13534 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13535 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13536 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13537 }
252b5132
RH
13538 break;
13539
252b5132 13540 case M_SEQ:
c0ebe874
RS
13541 if (op[1] == 0)
13542 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13543 else if (op[2] == 0)
13544 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13545 else
13546 {
c0ebe874
RS
13547 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13548 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13549 }
8fc2e39e 13550 break;
252b5132
RH
13551
13552 case M_SEQ_I:
b0e6f033 13553 if (imm_expr.X_add_number == 0)
252b5132 13554 {
c0ebe874 13555 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13556 break;
252b5132 13557 }
c0ebe874 13558 if (op[1] == 0)
252b5132 13559 {
1661c76c 13560 as_warn (_("instruction %s: result is always false"),
252b5132 13561 ip->insn_mo->name);
c0ebe874 13562 move_register (op[0], 0);
8fc2e39e 13563 break;
252b5132 13564 }
dd3cbb7e
NC
13565 if (CPU_HAS_SEQ (mips_opts.arch)
13566 && -512 <= imm_expr.X_add_number
13567 && imm_expr.X_add_number < 512)
13568 {
c0ebe874 13569 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13570 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13571 break;
13572 }
b0e6f033 13573 if (imm_expr.X_add_number >= 0
252b5132 13574 && imm_expr.X_add_number < 0x10000)
c0ebe874 13575 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13576 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13577 && imm_expr.X_add_number < 0)
13578 {
13579 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13580 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13581 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13582 }
dd3cbb7e
NC
13583 else if (CPU_HAS_SEQ (mips_opts.arch))
13584 {
13585 used_at = 1;
bad1aba3 13586 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13587 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13588 break;
13589 }
252b5132
RH
13590 else
13591 {
bad1aba3 13592 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13593 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13594 used_at = 1;
13595 }
c0ebe874 13596 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13597 break;
252b5132 13598
c0ebe874 13599 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13600 s = "slt";
13601 goto sge;
13602 case M_SGEU:
13603 s = "sltu";
13604 sge:
c0ebe874
RS
13605 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13606 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13607 break;
252b5132 13608
6f2117ba 13609 case M_SGE_I: /* X >= I <==> not (X < I). */
252b5132 13610 case M_SGEU_I:
b0e6f033 13611 if (imm_expr.X_add_number >= -0x8000
252b5132 13612 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13613 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13614 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13615 else
13616 {
bad1aba3 13617 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13618 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13619 op[0], op[1], AT);
252b5132
RH
13620 used_at = 1;
13621 }
c0ebe874 13622 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13623 break;
252b5132 13624
6f2117ba 13625 case M_SGT: /* X > Y <==> Y < X. */
252b5132
RH
13626 s = "slt";
13627 goto sgt;
13628 case M_SGTU:
13629 s = "sltu";
13630 sgt:
c0ebe874 13631 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13632 break;
252b5132 13633
6f2117ba 13634 case M_SGT_I: /* X > I <==> I < X. */
252b5132
RH
13635 s = "slt";
13636 goto sgti;
13637 case M_SGTU_I:
13638 s = "sltu";
13639 sgti:
8fc2e39e 13640 used_at = 1;
bad1aba3 13641 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13642 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13643 break;
13644
6f2117ba 13645 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X). */
252b5132
RH
13646 s = "slt";
13647 goto sle;
13648 case M_SLEU:
13649 s = "sltu";
13650 sle:
c0ebe874
RS
13651 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13652 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13653 break;
252b5132 13654
c0ebe874 13655 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13656 s = "slt";
13657 goto slei;
13658 case M_SLEU_I:
13659 s = "sltu";
13660 slei:
8fc2e39e 13661 used_at = 1;
bad1aba3 13662 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13663 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13664 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13665 break;
13666
13667 case M_SLT_I:
b0e6f033 13668 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13669 && imm_expr.X_add_number < 0x8000)
13670 {
c0ebe874
RS
13671 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13672 BFD_RELOC_LO16);
8fc2e39e 13673 break;
252b5132 13674 }
8fc2e39e 13675 used_at = 1;
bad1aba3 13676 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13677 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13678 break;
13679
13680 case M_SLTU_I:
b0e6f033 13681 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13682 && imm_expr.X_add_number < 0x8000)
13683 {
c0ebe874 13684 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13685 BFD_RELOC_LO16);
8fc2e39e 13686 break;
252b5132 13687 }
8fc2e39e 13688 used_at = 1;
bad1aba3 13689 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13690 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13691 break;
13692
13693 case M_SNE:
c0ebe874
RS
13694 if (op[1] == 0)
13695 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13696 else if (op[2] == 0)
13697 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13698 else
13699 {
c0ebe874
RS
13700 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13701 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13702 }
8fc2e39e 13703 break;
252b5132
RH
13704
13705 case M_SNE_I:
b0e6f033 13706 if (imm_expr.X_add_number == 0)
252b5132 13707 {
c0ebe874 13708 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13709 break;
252b5132 13710 }
c0ebe874 13711 if (op[1] == 0)
252b5132 13712 {
1661c76c 13713 as_warn (_("instruction %s: result is always true"),
252b5132 13714 ip->insn_mo->name);
bad1aba3 13715 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13716 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13717 break;
252b5132 13718 }
dd3cbb7e
NC
13719 if (CPU_HAS_SEQ (mips_opts.arch)
13720 && -512 <= imm_expr.X_add_number
13721 && imm_expr.X_add_number < 512)
13722 {
c0ebe874 13723 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13724 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13725 break;
13726 }
b0e6f033 13727 if (imm_expr.X_add_number >= 0
252b5132
RH
13728 && imm_expr.X_add_number < 0x10000)
13729 {
c0ebe874
RS
13730 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13731 BFD_RELOC_LO16);
252b5132 13732 }
b0e6f033 13733 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13734 && imm_expr.X_add_number < 0)
13735 {
13736 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13737 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13738 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13739 }
dd3cbb7e
NC
13740 else if (CPU_HAS_SEQ (mips_opts.arch))
13741 {
13742 used_at = 1;
bad1aba3 13743 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13744 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13745 break;
13746 }
252b5132
RH
13747 else
13748 {
bad1aba3 13749 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13750 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13751 used_at = 1;
13752 }
c0ebe874 13753 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13754 break;
252b5132 13755
df58fc94
RS
13756 case M_SUB_I:
13757 s = "addi";
13758 s2 = "sub";
387e7624
FS
13759 if (ISA_IS_R6 (mips_opts.isa))
13760 goto do_subi_i;
13761 else
13762 goto do_subi;
df58fc94
RS
13763 case M_SUBU_I:
13764 s = "addiu";
13765 s2 = "subu";
13766 goto do_subi;
252b5132
RH
13767 case M_DSUB_I:
13768 dbl = 1;
df58fc94
RS
13769 s = "daddi";
13770 s2 = "dsub";
387e7624 13771 if (!mips_opts.micromips && !ISA_IS_R6 (mips_opts.isa))
df58fc94 13772 goto do_subi;
b0e6f033 13773 if (imm_expr.X_add_number > -0x200
387e7624
FS
13774 && imm_expr.X_add_number <= 0x200
13775 && !ISA_IS_R6 (mips_opts.isa))
252b5132 13776 {
b0e6f033
RS
13777 macro_build (NULL, s, "t,r,.", op[0], op[1],
13778 (int) -imm_expr.X_add_number);
8fc2e39e 13779 break;
252b5132 13780 }
df58fc94 13781 goto do_subi_i;
252b5132
RH
13782 case M_DSUBU_I:
13783 dbl = 1;
df58fc94
RS
13784 s = "daddiu";
13785 s2 = "dsubu";
13786 do_subi:
b0e6f033 13787 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13788 && imm_expr.X_add_number <= 0x8000)
13789 {
13790 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13791 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13792 break;
252b5132 13793 }
df58fc94 13794 do_subi_i:
8fc2e39e 13795 used_at = 1;
67c0d1eb 13796 load_register (AT, &imm_expr, dbl);
c0ebe874 13797 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13798 break;
13799
13800 case M_TEQ_I:
13801 s = "teq";
13802 goto trap;
13803 case M_TGE_I:
13804 s = "tge";
13805 goto trap;
13806 case M_TGEU_I:
13807 s = "tgeu";
13808 goto trap;
13809 case M_TLT_I:
13810 s = "tlt";
13811 goto trap;
13812 case M_TLTU_I:
13813 s = "tltu";
13814 goto trap;
13815 case M_TNE_I:
13816 s = "tne";
13817 trap:
8fc2e39e 13818 used_at = 1;
bad1aba3 13819 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13820 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13821 break;
13822
252b5132 13823 case M_TRUNCWS:
43841e91 13824 case M_TRUNCWD:
df58fc94 13825 gas_assert (!mips_opts.micromips);
0aa27725 13826 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13827 used_at = 1;
252b5132
RH
13828
13829 /*
13830 * Is the double cfc1 instruction a bug in the mips assembler;
13831 * or is there a reason for it?
13832 */
7d10b47d 13833 start_noreorder ();
9204ccd4
MR
13834 macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
13835 macro_build (NULL, "cfc1", "t,g", op[2], FCSR);
67c0d1eb 13836 macro_build (NULL, "nop", "");
252b5132 13837 expr1.X_add_number = 3;
c0ebe874 13838 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13839 expr1.X_add_number = 2;
67c0d1eb 13840 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
9204ccd4 13841 macro_build (NULL, "ctc1", "t,g", AT, FCSR);
67c0d1eb
RS
13842 macro_build (NULL, "nop", "");
13843 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874 13844 op[0], op[1]);
9204ccd4 13845 macro_build (NULL, "ctc1", "t,g", op[2], FCSR);
67c0d1eb 13846 macro_build (NULL, "nop", "");
7d10b47d 13847 end_noreorder ();
252b5132
RH
13848 break;
13849
f2ae14a1 13850 case M_ULH_AB:
252b5132 13851 s = "lb";
df58fc94
RS
13852 s2 = "lbu";
13853 off = 1;
13854 goto uld_st;
f2ae14a1 13855 case M_ULHU_AB:
252b5132 13856 s = "lbu";
df58fc94
RS
13857 s2 = "lbu";
13858 off = 1;
13859 goto uld_st;
f2ae14a1 13860 case M_ULW_AB:
df58fc94
RS
13861 s = "lwl";
13862 s2 = "lwr";
7f3c4072 13863 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13864 off = 3;
13865 goto uld_st;
f2ae14a1 13866 case M_ULD_AB:
252b5132
RH
13867 s = "ldl";
13868 s2 = "ldr";
7f3c4072 13869 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13870 off = 7;
df58fc94 13871 goto uld_st;
f2ae14a1 13872 case M_USH_AB:
df58fc94
RS
13873 s = "sb";
13874 s2 = "sb";
13875 off = 1;
13876 ust = 1;
13877 goto uld_st;
f2ae14a1 13878 case M_USW_AB:
df58fc94
RS
13879 s = "swl";
13880 s2 = "swr";
7f3c4072 13881 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13882 off = 3;
df58fc94
RS
13883 ust = 1;
13884 goto uld_st;
f2ae14a1 13885 case M_USD_AB:
df58fc94
RS
13886 s = "sdl";
13887 s2 = "sdr";
7f3c4072 13888 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13889 off = 7;
13890 ust = 1;
13891
13892 uld_st:
c0ebe874 13893 breg = op[2];
f2ae14a1 13894 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13895 ep = &offset_expr;
13896 expr1.X_add_number = 0;
f2ae14a1 13897 if (large_offset)
df58fc94
RS
13898 {
13899 used_at = 1;
13900 tempreg = AT;
f2ae14a1
RS
13901 if (small_offset_p (0, align, 16))
13902 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13903 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13904 else
13905 {
13906 load_address (tempreg, ep, &used_at);
13907 if (breg != 0)
13908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13909 tempreg, tempreg, breg);
13910 }
13911 offset_reloc[0] = BFD_RELOC_LO16;
13912 offset_reloc[1] = BFD_RELOC_UNUSED;
13913 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13914 breg = tempreg;
c0ebe874 13915 tempreg = op[0];
df58fc94
RS
13916 ep = &expr1;
13917 }
c0ebe874 13918 else if (!ust && op[0] == breg)
8fc2e39e
TS
13919 {
13920 used_at = 1;
13921 tempreg = AT;
13922 }
252b5132 13923 else
c0ebe874 13924 tempreg = op[0];
af22f5b2 13925
df58fc94
RS
13926 if (off == 1)
13927 goto ulh_sh;
252b5132 13928
90ecf173 13929 if (!target_big_endian)
df58fc94 13930 ep->X_add_number += off;
f2ae14a1 13931 if (offbits == 12)
c8276761 13932 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13933 else
13934 macro_build (ep, s, "t,o(b)", tempreg, -1,
13935 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13936
90ecf173 13937 if (!target_big_endian)
df58fc94 13938 ep->X_add_number -= off;
252b5132 13939 else
df58fc94 13940 ep->X_add_number += off;
f2ae14a1 13941 if (offbits == 12)
df58fc94 13942 macro_build (NULL, s2, "t,~(b)",
c8276761 13943 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13944 else
13945 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13946 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13947
df58fc94 13948 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13949 if (!ust && op[0] != tempreg)
df58fc94
RS
13950 {
13951 /* Protect second load's delay slot. */
13952 load_delay_nop ();
c0ebe874 13953 move_register (op[0], tempreg);
df58fc94 13954 }
8fc2e39e 13955 break;
252b5132 13956
df58fc94 13957 ulh_sh:
d6bc6245 13958 used_at = 1;
df58fc94
RS
13959 if (target_big_endian == ust)
13960 ep->X_add_number += off;
c0ebe874 13961 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13962 macro_build (ep, s, "t,o(b)", tempreg, -1,
13963 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13964
13965 /* For halfword transfers we need a temporary register to shuffle
13966 bytes. Unfortunately for M_USH_A we have none available before
13967 the next store as AT holds the base address. We deal with this
13968 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13969 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13970 if (ust)
c0ebe874 13971 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13972
13973 if (target_big_endian == ust)
13974 ep->X_add_number -= off;
252b5132 13975 else
df58fc94 13976 ep->X_add_number += off;
f2ae14a1
RS
13977 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13978 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13979
df58fc94 13980 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13981 if (ust && large_offset)
df58fc94
RS
13982 {
13983 if (target_big_endian)
13984 ep->X_add_number += off;
13985 else
13986 ep->X_add_number -= off;
f2ae14a1
RS
13987 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13988 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13989 }
13990 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13991 if (!ust || large_offset)
df58fc94 13992 {
c0ebe874 13993 tempreg = !large_offset ? AT : op[0];
df58fc94 13994 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13995 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13996 }
252b5132
RH
13997 break;
13998
13999 default:
14000 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 14001 are added dynamically. */
1661c76c 14002 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
14003 break;
14004 }
741fe287 14005 if (!mips_opts.at && used_at)
1661c76c 14006 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
14007}
14008
14009/* Implement macros in mips16 mode. */
14010
14011static void
17a2f251 14012mips16_macro (struct mips_cl_insn *ip)
252b5132 14013{
c0ebe874 14014 const struct mips_operand_array *operands;
252b5132 14015 int mask;
c0ebe874 14016 int tmp;
252b5132
RH
14017 expressionS expr1;
14018 int dbl;
14019 const char *s, *s2, *s3;
c0ebe874
RS
14020 unsigned int op[MAX_OPERANDS];
14021 unsigned int i;
252b5132
RH
14022
14023 mask = ip->insn_mo->mask;
14024
c0ebe874
RS
14025 operands = insn_operands (ip);
14026 for (i = 0; i < MAX_OPERANDS; i++)
14027 if (operands->operand[i])
14028 op[i] = insn_extract_operand (ip, operands->operand[i]);
14029 else
14030 op[i] = -1;
252b5132 14031
252b5132
RH
14032 expr1.X_op = O_constant;
14033 expr1.X_op_symbol = NULL;
14034 expr1.X_add_symbol = NULL;
14035 expr1.X_add_number = 1;
14036
14037 dbl = 0;
14038
14039 switch (mask)
14040 {
14041 default:
b37df7c4 14042 abort ();
252b5132
RH
14043
14044 case M_DDIV_3:
14045 dbl = 1;
1a0670f3 14046 /* Fall through. */
252b5132
RH
14047 case M_DIV_3:
14048 s = "mflo";
14049 goto do_div3;
14050 case M_DREM_3:
14051 dbl = 1;
1a0670f3 14052 /* Fall through. */
252b5132
RH
14053 case M_REM_3:
14054 s = "mfhi";
14055 do_div3:
7d10b47d 14056 start_noreorder ();
d8722d76 14057 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 14058 expr1.X_add_number = 2;
c0ebe874 14059 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14060 macro_build (NULL, "break", "6", 7);
bdaaa2e1 14061
252b5132
RH
14062 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
14063 since that causes an overflow. We should do that as well,
14064 but I don't see how to do the comparisons without a temporary
14065 register. */
7d10b47d 14066 end_noreorder ();
c0ebe874 14067 macro_build (NULL, s, "x", op[0]);
252b5132
RH
14068 break;
14069
14070 case M_DIVU_3:
14071 s = "divu";
14072 s2 = "mflo";
14073 goto do_divu3;
14074 case M_REMU_3:
14075 s = "divu";
14076 s2 = "mfhi";
14077 goto do_divu3;
14078 case M_DDIVU_3:
14079 s = "ddivu";
14080 s2 = "mflo";
14081 goto do_divu3;
14082 case M_DREMU_3:
14083 s = "ddivu";
14084 s2 = "mfhi";
14085 do_divu3:
7d10b47d 14086 start_noreorder ();
d8722d76 14087 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 14088 expr1.X_add_number = 2;
c0ebe874 14089 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 14090 macro_build (NULL, "break", "6", 7);
7d10b47d 14091 end_noreorder ();
c0ebe874 14092 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
14093 break;
14094
14095 case M_DMUL:
14096 dbl = 1;
1a0670f3 14097 /* Fall through. */
252b5132 14098 case M_MUL:
c0ebe874
RS
14099 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
14100 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 14101 break;
252b5132
RH
14102
14103 case M_DSUBU_I:
14104 dbl = 1;
14105 goto do_subu;
14106 case M_SUBU_I:
14107 do_subu:
252b5132 14108 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 14109 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
14110 break;
14111
14112 case M_SUBU_I_2:
252b5132 14113 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14114 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
14115 break;
14116
14117 case M_DSUBU_I_2:
252b5132 14118 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 14119 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
14120 break;
14121
14122 case M_BEQ:
14123 s = "cmp";
14124 s2 = "bteqz";
14125 goto do_branch;
14126 case M_BNE:
14127 s = "cmp";
14128 s2 = "btnez";
14129 goto do_branch;
14130 case M_BLT:
14131 s = "slt";
14132 s2 = "btnez";
14133 goto do_branch;
14134 case M_BLTU:
14135 s = "sltu";
14136 s2 = "btnez";
14137 goto do_branch;
14138 case M_BLE:
14139 s = "slt";
14140 s2 = "bteqz";
14141 goto do_reverse_branch;
14142 case M_BLEU:
14143 s = "sltu";
14144 s2 = "bteqz";
14145 goto do_reverse_branch;
14146 case M_BGE:
14147 s = "slt";
14148 s2 = "bteqz";
14149 goto do_branch;
14150 case M_BGEU:
14151 s = "sltu";
14152 s2 = "bteqz";
14153 goto do_branch;
14154 case M_BGT:
14155 s = "slt";
14156 s2 = "btnez";
14157 goto do_reverse_branch;
14158 case M_BGTU:
14159 s = "sltu";
14160 s2 = "btnez";
14161
14162 do_reverse_branch:
c0ebe874
RS
14163 tmp = op[1];
14164 op[1] = op[0];
14165 op[0] = tmp;
252b5132
RH
14166
14167 do_branch:
c0ebe874 14168 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 14169 macro_build (&offset_expr, s2, "p");
252b5132
RH
14170 break;
14171
14172 case M_BEQ_I:
14173 s = "cmpi";
14174 s2 = "bteqz";
14175 s3 = "x,U";
14176 goto do_branch_i;
14177 case M_BNE_I:
14178 s = "cmpi";
14179 s2 = "btnez";
14180 s3 = "x,U";
14181 goto do_branch_i;
14182 case M_BLT_I:
14183 s = "slti";
14184 s2 = "btnez";
14185 s3 = "x,8";
14186 goto do_branch_i;
14187 case M_BLTU_I:
14188 s = "sltiu";
14189 s2 = "btnez";
14190 s3 = "x,8";
14191 goto do_branch_i;
14192 case M_BLE_I:
14193 s = "slti";
14194 s2 = "btnez";
14195 s3 = "x,8";
14196 goto do_addone_branch_i;
14197 case M_BLEU_I:
14198 s = "sltiu";
14199 s2 = "btnez";
14200 s3 = "x,8";
14201 goto do_addone_branch_i;
14202 case M_BGE_I:
14203 s = "slti";
14204 s2 = "bteqz";
14205 s3 = "x,8";
14206 goto do_branch_i;
14207 case M_BGEU_I:
14208 s = "sltiu";
14209 s2 = "bteqz";
14210 s3 = "x,8";
14211 goto do_branch_i;
14212 case M_BGT_I:
14213 s = "slti";
14214 s2 = "bteqz";
14215 s3 = "x,8";
14216 goto do_addone_branch_i;
14217 case M_BGTU_I:
14218 s = "sltiu";
14219 s2 = "bteqz";
14220 s3 = "x,8";
14221
14222 do_addone_branch_i:
252b5132
RH
14223 ++imm_expr.X_add_number;
14224
14225 do_branch_i:
c0ebe874 14226 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 14227 macro_build (&offset_expr, s2, "p");
252b5132
RH
14228 break;
14229
14230 case M_ABS:
14231 expr1.X_add_number = 0;
c0ebe874
RS
14232 macro_build (&expr1, "slti", "x,8", op[1]);
14233 if (op[0] != op[1])
14234 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 14235 expr1.X_add_number = 2;
67c0d1eb 14236 macro_build (&expr1, "bteqz", "p");
c0ebe874 14237 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 14238 break;
252b5132
RH
14239 }
14240}
14241
14daeee3
RS
14242/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
14243 opcode bits in *OPCODE_EXTRA. */
14244
14245static struct mips_opcode *
629310ab 14246mips_lookup_insn (htab_t hash, const char *start,
da8bca91 14247 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
14248{
14249 char *name, *dot, *p;
14250 unsigned int mask, suffix;
da8bca91 14251 ssize_t opend;
14daeee3
RS
14252 struct mips_opcode *insn;
14253
14254 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 14255 name = xstrndup (start, length);
14daeee3
RS
14256
14257 /* Look up the instruction as-is. */
629310ab 14258 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14259 if (insn)
e1fa0163 14260 goto end;
14daeee3
RS
14261
14262 dot = strchr (name, '.');
14263 if (dot && dot[1])
14264 {
14265 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14266 p = mips_parse_vu0_channels (dot + 1, &mask);
14267 if (*p == 0 && mask != 0)
14268 {
14269 *dot = 0;
629310ab 14270 insn = (struct mips_opcode *) str_hash_find (hash, name);
14daeee3
RS
14271 *dot = '.';
14272 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14273 {
14274 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14275 goto end;
14daeee3
RS
14276 }
14277 }
14278 }
14279
14280 if (mips_opts.micromips)
14281 {
14282 /* See if there's an instruction size override suffix,
14283 either `16' or `32', at the end of the mnemonic proper,
14284 that defines the operation, i.e. before the first `.'
14285 character if any. Strip it and retry. */
14286 opend = dot != NULL ? dot - name : length;
14287 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14288 suffix = 2;
3076e594 14289 else if (opend >= 2 && name[opend - 2] == '3' && name[opend - 1] == '2')
14daeee3
RS
14290 suffix = 4;
14291 else
14292 suffix = 0;
14293 if (suffix)
14294 {
39334a61 14295 memmove (name + opend - 2, name + opend, length - opend + 1);
629310ab 14296 insn = (struct mips_opcode *) str_hash_find (hash, name);
ee5734f0 14297 if (insn)
14daeee3
RS
14298 {
14299 forced_insn_length = suffix;
e1fa0163 14300 goto end;
14daeee3
RS
14301 }
14302 }
14303 }
14304
e1fa0163
NC
14305 insn = NULL;
14306 end:
14307 free (name);
14308 return insn;
14daeee3
RS
14309}
14310
77bd4346 14311/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14312 is a macro, set imm_expr and offset_expr to the values associated
14313 with "I" and "A" operands respectively. Otherwise store the value
14314 of the relocatable field (if any) in offset_expr. In both cases
14315 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14316
14317static void
60f20e8b 14318mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14319{
60f20e8b 14320 const struct mips_opcode *first, *past;
629310ab 14321 htab_t hash;
a92713e6 14322 char format;
14daeee3 14323 size_t end;
a92713e6 14324 struct mips_operand_token *tokens;
14daeee3 14325 unsigned int opcode_extra;
252b5132 14326
df58fc94
RS
14327 if (mips_opts.micromips)
14328 {
14329 hash = micromips_op_hash;
14330 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14331 }
14332 else
14333 {
14334 hash = op_hash;
14335 past = &mips_opcodes[NUMOPCODES];
14336 }
14337 forced_insn_length = 0;
14daeee3 14338 opcode_extra = 0;
252b5132 14339
df58fc94 14340 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14341 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14342 continue;
bdaaa2e1 14343
60f20e8b
RS
14344 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14345 if (first == NULL)
252b5132 14346 {
1661c76c 14347 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14348 return;
252b5132
RH
14349 }
14350
60f20e8b 14351 if (strcmp (first->name, "li.s") == 0)
a92713e6 14352 format = 'f';
60f20e8b 14353 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14354 format = 'd';
14355 else
14356 format = 0;
14357 tokens = mips_parse_arguments (str + end, format);
14358 if (!tokens)
14359 return;
14360
5b7c81bd
AM
14361 if (!match_insns (insn, first, past, tokens, opcode_extra, false)
14362 && !match_insns (insn, first, past, tokens, opcode_extra, true))
1661c76c 14363 set_insn_error (0, _("invalid operands"));
df58fc94 14364
e3de51ce 14365 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14366}
14367
77bd4346
RS
14368/* As for mips_ip, but used when assembling MIPS16 code.
14369 Also set forced_insn_length to the resulting instruction size in
14370 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14371
14372static void
60f20e8b 14373mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14374{
1a00e612 14375 char *end, *s, c;
60f20e8b 14376 struct mips_opcode *first;
a92713e6 14377 struct mips_operand_token *tokens;
3fb49709 14378 unsigned int l;
252b5132 14379
25499ac7 14380 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14381 ;
1a00e612
RS
14382 end = s;
14383 c = *end;
3fb49709
MR
14384
14385 l = 0;
1a00e612 14386 switch (c)
252b5132
RH
14387 {
14388 case '\0':
14389 break;
14390
14391 case ' ':
1a00e612 14392 s++;
252b5132
RH
14393 break;
14394
14395 case '.':
3fb49709
MR
14396 s++;
14397 if (*s == 't')
252b5132 14398 {
3fb49709
MR
14399 l = 2;
14400 s++;
252b5132 14401 }
3fb49709 14402 else if (*s == 'e')
252b5132 14403 {
3fb49709
MR
14404 l = 4;
14405 s++;
252b5132 14406 }
3fb49709
MR
14407 if (*s == '\0')
14408 break;
14409 else if (*s++ == ' ')
14410 break;
1661c76c 14411 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14412 return;
14413 }
3fb49709 14414 forced_insn_length = l;
252b5132 14415
1a00e612 14416 *end = 0;
629310ab 14417 first = (struct mips_opcode *) str_hash_find (mips16_op_hash, str);
1a00e612
RS
14418 *end = c;
14419
60f20e8b 14420 if (!first)
252b5132 14421 {
1661c76c 14422 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14423 return;
14424 }
14425
a92713e6
RS
14426 tokens = mips_parse_arguments (s, 0);
14427 if (!tokens)
14428 return;
14429
60f20e8b 14430 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14431 set_insn_error (0, _("invalid operands"));
252b5132 14432
e3de51ce 14433 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14434}
14435
b886a2ab
RS
14436/* Marshal immediate value VAL for an extended MIPS16 instruction.
14437 NBITS is the number of significant bits in VAL. */
14438
14439static unsigned long
14440mips16_immed_extend (offsetT val, unsigned int nbits)
14441{
14442 int extval;
25499ac7
MR
14443
14444 extval = 0;
14445 val &= (1U << nbits) - 1;
14446 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14447 {
14448 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14449 val &= 0x1f;
14450 }
14451 else if (nbits == 15)
14452 {
14453 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14454 val &= 0xf;
14455 }
25499ac7 14456 else if (nbits == 6)
b886a2ab
RS
14457 {
14458 extval = ((val & 0x1f) << 6) | (val & 0x20);
14459 val = 0;
14460 }
14461 return (extval << 16) | val;
14462}
14463
3ccad066
RS
14464/* Like decode_mips16_operand, but require the operand to be defined and
14465 require it to be an integer. */
14466
14467static const struct mips_int_operand *
5b7c81bd 14468mips16_immed_operand (int type, bool extended_p)
3ccad066
RS
14469{
14470 const struct mips_operand *operand;
14471
14472 operand = decode_mips16_operand (type, extended_p);
14473 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14474 abort ();
14475 return (const struct mips_int_operand *) operand;
14476}
14477
14478/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14479
5b7c81bd 14480static bool
3ccad066
RS
14481mips16_immed_in_range_p (const struct mips_int_operand *operand,
14482 bfd_reloc_code_real_type reloc, offsetT sval)
14483{
14484 int min_val, max_val;
14485
14486 min_val = mips_int_operand_min (operand);
14487 max_val = mips_int_operand_max (operand);
14488 if (reloc != BFD_RELOC_UNUSED)
14489 {
14490 if (min_val < 0)
14491 sval = SEXT_16BIT (sval);
14492 else
14493 sval &= 0xffff;
14494 }
14495
14496 return (sval >= min_val
14497 && sval <= max_val
14498 && (sval & ((1 << operand->shift) - 1)) == 0);
14499}
14500
5c04167a
RS
14501/* Install immediate value VAL into MIPS16 instruction *INSN,
14502 extending it if necessary. The instruction in *INSN may
14503 already be extended.
14504
43c0598f
RS
14505 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14506 if none. In the former case, VAL is a 16-bit number with no
14507 defined signedness.
14508
14509 TYPE is the type of the immediate field. USER_INSN_LENGTH
14510 is the length that the user requested, or 0 if none. */
252b5132
RH
14511
14512static void
3b4dbbbf 14513mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14514 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14515 unsigned int user_insn_length, unsigned long *insn)
252b5132 14516{
3ccad066
RS
14517 const struct mips_int_operand *operand;
14518 unsigned int uval, length;
252b5132 14519
5b7c81bd 14520 operand = mips16_immed_operand (type, false);
3ccad066 14521 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14522 {
14523 /* We need an extended instruction. */
14524 if (user_insn_length == 2)
14525 as_bad_where (file, line, _("invalid unextended operand value"));
14526 else
14527 *insn |= MIPS16_EXTEND;
14528 }
14529 else if (user_insn_length == 4)
14530 {
14531 /* The operand doesn't force an unextended instruction to be extended.
14532 Warn if the user wanted an extended instruction anyway. */
14533 *insn |= MIPS16_EXTEND;
14534 as_warn_where (file, line,
14535 _("extended operand requested but not required"));
14536 }
252b5132 14537
3ccad066
RS
14538 length = mips16_opcode_length (*insn);
14539 if (length == 4)
252b5132 14540 {
5b7c81bd 14541 operand = mips16_immed_operand (type, true);
3ccad066
RS
14542 if (!mips16_immed_in_range_p (operand, reloc, val))
14543 as_bad_where (file, line,
14544 _("operand value out of range for instruction"));
252b5132 14545 }
3ccad066 14546 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14547 if (length == 2 || operand->root.lsb != 0)
3ccad066 14548 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14549 else
3ccad066 14550 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14551}
14552\f
d6f16593 14553struct percent_op_match
ad8d3bb3 14554{
5e0116d5
RS
14555 const char *str;
14556 bfd_reloc_code_real_type reloc;
d6f16593
MR
14557};
14558
14559static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14560{
5e0116d5 14561 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14562 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14563 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14564 {"%call16", BFD_RELOC_MIPS_CALL16},
14565 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14566 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14567 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14568 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14569 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14570 {"%got", BFD_RELOC_MIPS_GOT16},
14571 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14572 {"%gprel", BFD_RELOC_GPREL16},
d712f276 14573 {"%half", BFD_RELOC_MIPS_16},
5e0116d5
RS
14574 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14575 {"%higher", BFD_RELOC_MIPS_HIGHER},
14576 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14577 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14578 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14579 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14580 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14581 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14582 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14583 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14584 {"%hi", BFD_RELOC_HI16_S},
14585 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14586 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14587};
14588
d6f16593
MR
14589static const struct percent_op_match mips16_percent_op[] =
14590{
14591 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14592 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14593 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14594 {"%got", BFD_RELOC_MIPS16_GOT16},
14595 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14596 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14597 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14598 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14599 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14600 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14601 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14602 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14603 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14604};
14605
252b5132 14606
5e0116d5
RS
14607/* Return true if *STR points to a relocation operator. When returning true,
14608 move *STR over the operator and store its relocation code in *RELOC.
14609 Leave both *STR and *RELOC alone when returning false. */
14610
5b7c81bd 14611static bool
17a2f251 14612parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14613{
d6f16593
MR
14614 const struct percent_op_match *percent_op;
14615 size_t limit, i;
14616
14617 if (mips_opts.mips16)
14618 {
14619 percent_op = mips16_percent_op;
14620 limit = ARRAY_SIZE (mips16_percent_op);
14621 }
14622 else
14623 {
14624 percent_op = mips_percent_op;
14625 limit = ARRAY_SIZE (mips_percent_op);
14626 }
76b3015f 14627
d6f16593 14628 for (i = 0; i < limit; i++)
5e0116d5 14629 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14630 {
3f98094e
DJ
14631 int len = strlen (percent_op[i].str);
14632
14633 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14634 continue;
14635
5e0116d5
RS
14636 *str += strlen (percent_op[i].str);
14637 *reloc = percent_op[i].reloc;
394f9b3a 14638
5e0116d5
RS
14639 /* Check whether the output BFD supports this relocation.
14640 If not, issue an error and fall back on something safe. */
14641 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14642 {
20203fb9 14643 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14644 percent_op[i].str);
01a3f561 14645 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14646 }
5b7c81bd 14647 return true;
394f9b3a 14648 }
5b7c81bd 14649 return false;
394f9b3a 14650}
ad8d3bb3 14651
ad8d3bb3 14652
5e0116d5
RS
14653/* Parse string STR as a 16-bit relocatable operand. Store the
14654 expression in *EP and the relocations in the array starting
14655 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14656
6eb099ae
AM
14657 On exit, EXPR_PARSE_END points to the first character after the
14658 expression. */
ad8d3bb3 14659
5e0116d5 14660static size_t
17a2f251
TS
14661my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14662 char *str)
ad8d3bb3 14663{
5e0116d5
RS
14664 bfd_reloc_code_real_type reversed_reloc[3];
14665 size_t reloc_index, i;
09b8f35a
RS
14666 int crux_depth, str_depth;
14667 char *crux;
5e0116d5
RS
14668
14669 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14670 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14671 of the main expression and with CRUX_DEPTH containing the number
14672 of open brackets at that point. */
14673 reloc_index = -1;
14674 str_depth = 0;
14675 do
fb1b3232 14676 {
09b8f35a
RS
14677 reloc_index++;
14678 crux = str;
14679 crux_depth = str_depth;
14680
14681 /* Skip over whitespace and brackets, keeping count of the number
14682 of brackets. */
14683 while (*str == ' ' || *str == '\t' || *str == '(')
14684 if (*str++ == '(')
14685 str_depth++;
5e0116d5 14686 }
09b8f35a
RS
14687 while (*str == '%'
14688 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14689 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14690
09b8f35a 14691 my_getExpression (ep, crux);
6eb099ae 14692 str = expr_parse_end;
394f9b3a 14693
5e0116d5 14694 /* Match every open bracket. */
09b8f35a 14695 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14696 if (*str++ == ')')
09b8f35a 14697 crux_depth--;
394f9b3a 14698
09b8f35a 14699 if (crux_depth > 0)
20203fb9 14700 as_bad (_("unclosed '('"));
394f9b3a 14701
6eb099ae 14702 expr_parse_end = str;
252b5132 14703
ec4fcab0
MR
14704 for (i = 0; i < reloc_index; i++)
14705 reloc[i] = reversed_reloc[reloc_index - 1 - i];
fb1b3232 14706
5e0116d5 14707 return reloc_index;
252b5132
RH
14708}
14709
14710static void
17a2f251 14711my_getExpression (expressionS *ep, char *str)
252b5132
RH
14712{
14713 char *save_in;
14714
14715 save_in = input_line_pointer;
14716 input_line_pointer = str;
14717 expression (ep);
6eb099ae 14718 expr_parse_end = input_line_pointer;
252b5132 14719 input_line_pointer = save_in;
252b5132
RH
14720}
14721
6d4af3c2 14722const char *
17a2f251 14723md_atof (int type, char *litP, int *sizeP)
252b5132 14724{
499ac353 14725 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14726}
14727
14728void
17a2f251 14729md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14730{
14731 if (target_big_endian)
14732 number_to_chars_bigendian (buf, val, n);
14733 else
14734 number_to_chars_littleendian (buf, val, n);
14735}
14736\f
e013f690
TS
14737static int support_64bit_objects(void)
14738{
14739 const char **list, **l;
aa3d8fdf 14740 int yes;
e013f690
TS
14741
14742 list = bfd_target_list ();
14743 for (l = list; *l != NULL; l++)
aeffff67
RS
14744 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14745 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14746 break;
aa3d8fdf 14747 yes = (*l != NULL);
e013f690 14748 free (list);
aa3d8fdf 14749 return yes;
e013f690
TS
14750}
14751
316f5878
RS
14752/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14753 NEW_VALUE. Warn if another value was already specified. Note:
14754 we have to defer parsing the -march and -mtune arguments in order
14755 to handle 'from-abi' correctly, since the ABI might be specified
14756 in a later argument. */
14757
14758static void
17a2f251 14759mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14760{
14761 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14762 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14763 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14764 new_value);
14765
14766 *string_ptr = new_value;
14767}
14768
252b5132 14769int
17b9d67d 14770md_parse_option (int c, const char *arg)
252b5132 14771{
c6278170
RS
14772 unsigned int i;
14773
14774 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14775 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14776 {
919731af 14777 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14778 c == mips_ases[i].option_on);
14779 return 1;
14780 }
14781
252b5132
RH
14782 switch (c)
14783 {
119d663a
NC
14784 case OPTION_CONSTRUCT_FLOATS:
14785 mips_disable_float_construction = 0;
14786 break;
bdaaa2e1 14787
119d663a
NC
14788 case OPTION_NO_CONSTRUCT_FLOATS:
14789 mips_disable_float_construction = 1;
14790 break;
bdaaa2e1 14791
252b5132
RH
14792 case OPTION_TRAP:
14793 mips_trap = 1;
14794 break;
14795
14796 case OPTION_BREAK:
14797 mips_trap = 0;
14798 break;
14799
14800 case OPTION_EB:
14801 target_big_endian = 1;
14802 break;
14803
14804 case OPTION_EL:
14805 target_big_endian = 0;
14806 break;
14807
14808 case 'O':
4ffff32f
TS
14809 if (arg == NULL)
14810 mips_optimize = 1;
14811 else if (arg[0] == '0')
14812 mips_optimize = 0;
14813 else if (arg[0] == '1')
252b5132
RH
14814 mips_optimize = 1;
14815 else
14816 mips_optimize = 2;
14817 break;
14818
14819 case 'g':
14820 if (arg == NULL)
14821 mips_debug = 2;
14822 else
14823 mips_debug = atoi (arg);
252b5132
RH
14824 break;
14825
14826 case OPTION_MIPS1:
0b35dfee 14827 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14828 break;
14829
14830 case OPTION_MIPS2:
0b35dfee 14831 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14832 break;
14833
14834 case OPTION_MIPS3:
0b35dfee 14835 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14836 break;
14837
14838 case OPTION_MIPS4:
0b35dfee 14839 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14840 break;
14841
84ea6cf2 14842 case OPTION_MIPS5:
0b35dfee 14843 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14844 break;
14845
e7af610e 14846 case OPTION_MIPS32:
0b35dfee 14847 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14848 break;
14849
af7ee8bf 14850 case OPTION_MIPS32R2:
0b35dfee 14851 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14852 break;
14853
ae52f483 14854 case OPTION_MIPS32R3:
0ae19f05 14855 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14856 break;
14857
14858 case OPTION_MIPS32R5:
0ae19f05 14859 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14860 break;
14861
7361da2c
AB
14862 case OPTION_MIPS32R6:
14863 file_mips_opts.isa = ISA_MIPS32R6;
14864 break;
14865
5f74bc13 14866 case OPTION_MIPS64R2:
0b35dfee 14867 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14868 break;
14869
ae52f483 14870 case OPTION_MIPS64R3:
0ae19f05 14871 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14872 break;
14873
14874 case OPTION_MIPS64R5:
0ae19f05 14875 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14876 break;
14877
7361da2c
AB
14878 case OPTION_MIPS64R6:
14879 file_mips_opts.isa = ISA_MIPS64R6;
14880 break;
14881
84ea6cf2 14882 case OPTION_MIPS64:
0b35dfee 14883 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14884 break;
14885
ec68c924 14886 case OPTION_MTUNE:
316f5878
RS
14887 mips_set_option_string (&mips_tune_string, arg);
14888 break;
ec68c924 14889
316f5878
RS
14890 case OPTION_MARCH:
14891 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14892 break;
14893
14894 case OPTION_M4650:
316f5878
RS
14895 mips_set_option_string (&mips_arch_string, "4650");
14896 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14897 break;
14898
14899 case OPTION_NO_M4650:
14900 break;
14901
14902 case OPTION_M4010:
316f5878
RS
14903 mips_set_option_string (&mips_arch_string, "4010");
14904 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14905 break;
14906
14907 case OPTION_NO_M4010:
14908 break;
14909
14910 case OPTION_M4100:
316f5878
RS
14911 mips_set_option_string (&mips_arch_string, "4100");
14912 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14913 break;
14914
14915 case OPTION_NO_M4100:
14916 break;
14917
252b5132 14918 case OPTION_M3900:
316f5878
RS
14919 mips_set_option_string (&mips_arch_string, "3900");
14920 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14921 break;
bdaaa2e1 14922
252b5132
RH
14923 case OPTION_NO_M3900:
14924 break;
14925
df58fc94 14926 case OPTION_MICROMIPS:
919731af 14927 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14928 {
14929 as_bad (_("-mmicromips cannot be used with -mips16"));
14930 return 0;
14931 }
919731af 14932 file_mips_opts.micromips = 1;
df58fc94
RS
14933 mips_no_prev_insn ();
14934 break;
14935
14936 case OPTION_NO_MICROMIPS:
919731af 14937 file_mips_opts.micromips = 0;
df58fc94
RS
14938 mips_no_prev_insn ();
14939 break;
14940
252b5132 14941 case OPTION_MIPS16:
919731af 14942 if (file_mips_opts.micromips == 1)
df58fc94
RS
14943 {
14944 as_bad (_("-mips16 cannot be used with -micromips"));
14945 return 0;
14946 }
919731af 14947 file_mips_opts.mips16 = 1;
7d10b47d 14948 mips_no_prev_insn ();
252b5132
RH
14949 break;
14950
14951 case OPTION_NO_MIPS16:
919731af 14952 file_mips_opts.mips16 = 0;
7d10b47d 14953 mips_no_prev_insn ();
252b5132
RH
14954 break;
14955
6a32d874
CM
14956 case OPTION_FIX_24K:
14957 mips_fix_24k = 1;
14958 break;
14959
14960 case OPTION_NO_FIX_24K:
14961 mips_fix_24k = 0;
14962 break;
14963
a8d14a88
CM
14964 case OPTION_FIX_RM7000:
14965 mips_fix_rm7000 = 1;
14966 break;
14967
14968 case OPTION_NO_FIX_RM7000:
14969 mips_fix_rm7000 = 0;
14970 break;
14971
6f2117ba 14972 case OPTION_FIX_LOONGSON3_LLSC:
5b7c81bd 14973 mips_fix_loongson3_llsc = true;
6f2117ba
PH
14974 break;
14975
14976 case OPTION_NO_FIX_LOONGSON3_LLSC:
5b7c81bd 14977 mips_fix_loongson3_llsc = false;
6f2117ba
PH
14978 break;
14979
c67a084a 14980 case OPTION_FIX_LOONGSON2F_JUMP:
5b7c81bd 14981 mips_fix_loongson2f_jump = true;
c67a084a
NC
14982 break;
14983
14984 case OPTION_NO_FIX_LOONGSON2F_JUMP:
5b7c81bd 14985 mips_fix_loongson2f_jump = false;
c67a084a
NC
14986 break;
14987
14988 case OPTION_FIX_LOONGSON2F_NOP:
5b7c81bd 14989 mips_fix_loongson2f_nop = true;
c67a084a
NC
14990 break;
14991
14992 case OPTION_NO_FIX_LOONGSON2F_NOP:
5b7c81bd 14993 mips_fix_loongson2f_nop = false;
c67a084a
NC
14994 break;
14995
d766e8ec
RS
14996 case OPTION_FIX_VR4120:
14997 mips_fix_vr4120 = 1;
60b63b72
RS
14998 break;
14999
d766e8ec
RS
15000 case OPTION_NO_FIX_VR4120:
15001 mips_fix_vr4120 = 0;
60b63b72
RS
15002 break;
15003
7d8e00cf
RS
15004 case OPTION_FIX_VR4130:
15005 mips_fix_vr4130 = 1;
15006 break;
15007
15008 case OPTION_NO_FIX_VR4130:
15009 mips_fix_vr4130 = 0;
15010 break;
15011
d954098f 15012 case OPTION_FIX_CN63XXP1:
5b7c81bd 15013 mips_fix_cn63xxp1 = true;
d954098f
DD
15014 break;
15015
15016 case OPTION_NO_FIX_CN63XXP1:
5b7c81bd 15017 mips_fix_cn63xxp1 = false;
d954098f
DD
15018 break;
15019
27c634e0 15020 case OPTION_FIX_R5900:
5b7c81bd
AM
15021 mips_fix_r5900 = true;
15022 mips_fix_r5900_explicit = true;
27c634e0
FN
15023 break;
15024
15025 case OPTION_NO_FIX_R5900:
5b7c81bd
AM
15026 mips_fix_r5900 = false;
15027 mips_fix_r5900_explicit = true;
27c634e0
FN
15028 break;
15029
4a6a3df4
AO
15030 case OPTION_RELAX_BRANCH:
15031 mips_relax_branch = 1;
15032 break;
15033
15034 case OPTION_NO_RELAX_BRANCH:
15035 mips_relax_branch = 0;
15036 break;
15037
8b10b0b3 15038 case OPTION_IGNORE_BRANCH_ISA:
5b7c81bd 15039 mips_ignore_branch_isa = true;
8b10b0b3
MR
15040 break;
15041
15042 case OPTION_NO_IGNORE_BRANCH_ISA:
5b7c81bd 15043 mips_ignore_branch_isa = false;
8b10b0b3
MR
15044 break;
15045
833794fc 15046 case OPTION_INSN32:
5b7c81bd 15047 file_mips_opts.insn32 = true;
833794fc
MR
15048 break;
15049
15050 case OPTION_NO_INSN32:
5b7c81bd 15051 file_mips_opts.insn32 = false;
833794fc
MR
15052 break;
15053
aa6975fb 15054 case OPTION_MSHARED:
5b7c81bd 15055 mips_in_shared = true;
aa6975fb
ILT
15056 break;
15057
15058 case OPTION_MNO_SHARED:
5b7c81bd 15059 mips_in_shared = false;
aa6975fb
ILT
15060 break;
15061
aed1a261 15062 case OPTION_MSYM32:
5b7c81bd 15063 file_mips_opts.sym32 = true;
aed1a261
RS
15064 break;
15065
15066 case OPTION_MNO_SYM32:
5b7c81bd 15067 file_mips_opts.sym32 = false;
aed1a261
RS
15068 break;
15069
252b5132
RH
15070 /* When generating ELF code, we permit -KPIC and -call_shared to
15071 select SVR4_PIC, and -non_shared to select no PIC. This is
15072 intended to be compatible with Irix 5. */
15073 case OPTION_CALL_SHARED:
252b5132 15074 mips_pic = SVR4_PIC;
5b7c81bd 15075 mips_abicalls = true;
252b5132
RH
15076 break;
15077
861fb55a 15078 case OPTION_CALL_NONPIC:
861fb55a 15079 mips_pic = NO_PIC;
5b7c81bd 15080 mips_abicalls = true;
861fb55a
DJ
15081 break;
15082
252b5132 15083 case OPTION_NON_SHARED:
252b5132 15084 mips_pic = NO_PIC;
5b7c81bd 15085 mips_abicalls = false;
252b5132
RH
15086 break;
15087
44075ae2
TS
15088 /* The -xgot option tells the assembler to use 32 bit offsets
15089 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
15090 compatibility. */
15091 case OPTION_XGOT:
15092 mips_big_got = 1;
15093 break;
15094
15095 case 'G':
6caf9ef4
TS
15096 g_switch_value = atoi (arg);
15097 g_switch_seen = 1;
252b5132
RH
15098 break;
15099
34ba82a8
TS
15100 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
15101 and -mabi=64. */
252b5132 15102 case OPTION_32:
f3ded42a 15103 mips_abi = O32_ABI;
252b5132
RH
15104 break;
15105
e013f690 15106 case OPTION_N32:
316f5878 15107 mips_abi = N32_ABI;
e013f690 15108 break;
252b5132 15109
e013f690 15110 case OPTION_64:
316f5878 15111 mips_abi = N64_ABI;
f43abd2b 15112 if (!support_64bit_objects())
1661c76c 15113 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
15114 break;
15115
c97ef257 15116 case OPTION_GP32:
bad1aba3 15117 file_mips_opts.gp = 32;
c97ef257
AH
15118 break;
15119
15120 case OPTION_GP64:
bad1aba3 15121 file_mips_opts.gp = 64;
c97ef257 15122 break;
252b5132 15123
ca4e0257 15124 case OPTION_FP32:
0b35dfee 15125 file_mips_opts.fp = 32;
316f5878
RS
15126 break;
15127
351cdf24
MF
15128 case OPTION_FPXX:
15129 file_mips_opts.fp = 0;
15130 break;
15131
316f5878 15132 case OPTION_FP64:
0b35dfee 15133 file_mips_opts.fp = 64;
ca4e0257
RS
15134 break;
15135
351cdf24
MF
15136 case OPTION_ODD_SPREG:
15137 file_mips_opts.oddspreg = 1;
15138 break;
15139
15140 case OPTION_NO_ODD_SPREG:
15141 file_mips_opts.oddspreg = 0;
15142 break;
15143
037b32b9 15144 case OPTION_SINGLE_FLOAT:
0b35dfee 15145 file_mips_opts.single_float = 1;
037b32b9
AN
15146 break;
15147
15148 case OPTION_DOUBLE_FLOAT:
0b35dfee 15149 file_mips_opts.single_float = 0;
037b32b9
AN
15150 break;
15151
15152 case OPTION_SOFT_FLOAT:
0b35dfee 15153 file_mips_opts.soft_float = 1;
037b32b9
AN
15154 break;
15155
15156 case OPTION_HARD_FLOAT:
0b35dfee 15157 file_mips_opts.soft_float = 0;
037b32b9
AN
15158 break;
15159
252b5132 15160 case OPTION_MABI:
e013f690 15161 if (strcmp (arg, "32") == 0)
316f5878 15162 mips_abi = O32_ABI;
e013f690 15163 else if (strcmp (arg, "o64") == 0)
316f5878 15164 mips_abi = O64_ABI;
e013f690 15165 else if (strcmp (arg, "n32") == 0)
316f5878 15166 mips_abi = N32_ABI;
e013f690
TS
15167 else if (strcmp (arg, "64") == 0)
15168 {
316f5878 15169 mips_abi = N64_ABI;
e013f690 15170 if (! support_64bit_objects())
1661c76c 15171 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
15172 "format"));
15173 }
15174 else if (strcmp (arg, "eabi") == 0)
316f5878 15175 mips_abi = EABI_ABI;
e013f690 15176 else
da0e507f
TS
15177 {
15178 as_fatal (_("invalid abi -mabi=%s"), arg);
15179 return 0;
15180 }
252b5132
RH
15181 break;
15182
6b76fefe 15183 case OPTION_M7000_HILO_FIX:
5b7c81bd 15184 mips_7000_hilo_fix = true;
6b76fefe
CM
15185 break;
15186
9ee72ff1 15187 case OPTION_MNO_7000_HILO_FIX:
5b7c81bd 15188 mips_7000_hilo_fix = false;
6b76fefe
CM
15189 break;
15190
ecb4347a 15191 case OPTION_MDEBUG:
5b7c81bd 15192 mips_flag_mdebug = true;
ecb4347a
DJ
15193 break;
15194
15195 case OPTION_NO_MDEBUG:
5b7c81bd 15196 mips_flag_mdebug = false;
ecb4347a 15197 break;
dcd410fe
RO
15198
15199 case OPTION_PDR:
5b7c81bd 15200 mips_flag_pdr = true;
dcd410fe
RO
15201 break;
15202
15203 case OPTION_NO_PDR:
5b7c81bd 15204 mips_flag_pdr = false;
dcd410fe 15205 break;
0a44bf69
RS
15206
15207 case OPTION_MVXWORKS_PIC:
15208 mips_pic = VXWORKS_PIC;
15209 break;
ecb4347a 15210
ba92f887
MR
15211 case OPTION_NAN:
15212 if (strcmp (arg, "2008") == 0)
7361da2c 15213 mips_nan2008 = 1;
ba92f887 15214 else if (strcmp (arg, "legacy") == 0)
7361da2c 15215 mips_nan2008 = 0;
ba92f887
MR
15216 else
15217 {
1661c76c 15218 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
15219 return 0;
15220 }
15221 break;
15222
252b5132
RH
15223 default:
15224 return 0;
15225 }
15226
c67a084a
NC
15227 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
15228
252b5132
RH
15229 return 1;
15230}
316f5878 15231\f
919731af 15232/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 15233
316f5878 15234static void
17a2f251 15235mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
15236{
15237 if (info != 0)
fef14a42 15238 mips_tune = info->cpu;
316f5878 15239}
80cc45a5 15240
34ba82a8 15241
252b5132 15242void
17a2f251 15243mips_after_parse_args (void)
e9670677 15244{
fef14a42
TS
15245 const struct mips_cpu_info *arch_info = 0;
15246 const struct mips_cpu_info *tune_info = 0;
15247
6f2117ba 15248 /* GP relative stuff not working for PE. */
d34049e8 15249 if (startswith (TARGET_OS, "pe"))
e9670677 15250 {
6caf9ef4 15251 if (g_switch_seen && g_switch_value != 0)
1661c76c 15252 as_bad (_("-G not supported in this configuration"));
e9670677
MR
15253 g_switch_value = 0;
15254 }
15255
cac012d6
AO
15256 if (mips_abi == NO_ABI)
15257 mips_abi = MIPS_DEFAULT_ABI;
15258
919731af 15259 /* The following code determines the architecture.
22923709
RS
15260 Similar code was added to GCC 3.3 (see override_options() in
15261 config/mips/mips.c). The GAS and GCC code should be kept in sync
15262 as much as possible. */
e9670677 15263
316f5878 15264 if (mips_arch_string != 0)
fef14a42 15265 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 15266
0b35dfee 15267 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 15268 {
0b35dfee 15269 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 15270 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 15271 the -march selection (if any). */
fef14a42 15272 if (arch_info != 0)
e9670677 15273 {
316f5878
RS
15274 /* -march takes precedence over -mipsN, since it is more descriptive.
15275 There's no harm in specifying both as long as the ISA levels
15276 are the same. */
0b35dfee 15277 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15278 as_bad (_("-%s conflicts with the other architecture options,"
15279 " which imply -%s"),
0b35dfee 15280 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15281 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15282 }
316f5878 15283 else
0b35dfee 15284 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15285 }
15286
fef14a42 15287 if (arch_info == 0)
95bfe26e
MF
15288 {
15289 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15290 gas_assert (arch_info);
15291 }
e9670677 15292
fef14a42 15293 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15294 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15295 arch_info->name);
15296
919731af 15297 file_mips_opts.arch = arch_info->cpu;
15298 file_mips_opts.isa = arch_info->isa;
3315614d 15299 file_mips_opts.init_ase = arch_info->ase;
919731af 15300
41cee089
FS
15301 /* The EVA Extension has instructions which are only valid when the R6 ISA
15302 is enabled. This sets the ASE_EVA_R6 flag when both EVA and R6 ISA are
15303 present. */
15304 if (((file_mips_opts.ase & ASE_EVA) != 0) && ISA_IS_R6 (file_mips_opts.isa))
15305 file_mips_opts.ase |= ASE_EVA_R6;
15306
919731af 15307 /* Set up initial mips_opts state. */
15308 mips_opts = file_mips_opts;
15309
27c634e0
FN
15310 /* For the R5900 default to `-mfix-r5900' unless the user told otherwise. */
15311 if (!mips_fix_r5900_explicit)
15312 mips_fix_r5900 = file_mips_opts.arch == CPU_R5900;
15313
919731af 15314 /* The register size inference code is now placed in
15315 file_mips_check_options. */
fef14a42 15316
0b35dfee 15317 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15318 processor. */
fef14a42
TS
15319 if (mips_tune_string != 0)
15320 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15321
fef14a42
TS
15322 if (tune_info == 0)
15323 mips_set_tune (arch_info);
15324 else
15325 mips_set_tune (tune_info);
e9670677 15326
ecb4347a 15327 if (mips_flag_mdebug < 0)
e8044f35 15328 mips_flag_mdebug = 0;
e9670677
MR
15329}
15330\f
15331void
17a2f251 15332mips_init_after_args (void)
252b5132 15333{
6f2117ba 15334 /* Initialize opcodes. */
252b5132 15335 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15336 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15337}
15338
15339long
17a2f251 15340md_pcrel_from (fixS *fixP)
252b5132 15341{
a7ebbfdf 15342 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
6f2117ba 15343
a7ebbfdf
TS
15344 switch (fixP->fx_r_type)
15345 {
df58fc94
RS
15346 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15347 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15348 /* Return the address of the delay slot. */
15349 return addr + 2;
15350
15351 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15352 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15353 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15354 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15355 case BFD_RELOC_MIPS_21_PCREL_S2:
15356 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15357 case BFD_RELOC_MIPS_JMP:
15358 /* Return the address of the delay slot. */
15359 return addr + 4;
df58fc94 15360
51f6035b
MR
15361 case BFD_RELOC_MIPS_18_PCREL_S3:
15362 /* Return the aligned address of the doubleword containing
15363 the instruction. */
15364 return addr & ~7;
15365
a7ebbfdf
TS
15366 default:
15367 return addr;
15368 }
252b5132
RH
15369}
15370
252b5132
RH
15371/* This is called before the symbol table is processed. In order to
15372 work with gcc when using mips-tfile, we must keep all local labels.
15373 However, in other cases, we want to discard them. If we were
15374 called with -g, but we didn't see any debugging information, it may
15375 mean that gcc is smuggling debugging information through to
15376 mips-tfile, in which case we must generate all local labels. */
15377
15378void
17a2f251 15379mips_frob_file_before_adjust (void)
252b5132
RH
15380{
15381#ifndef NO_ECOFF_DEBUGGING
15382 if (ECOFF_DEBUGGING
15383 && mips_debug != 0
15384 && ! ecoff_debugging_seen)
15385 flag_keep_locals = 1;
15386#endif
15387}
15388
3b91255e 15389/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15390 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15391 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15392 relocation operators.
15393
15394 For our purposes, a %lo() expression matches a %got() or %hi()
15395 expression if:
15396
15397 (a) it refers to the same symbol; and
15398 (b) the offset applied in the %lo() expression is no lower than
15399 the offset applied in the %got() or %hi().
15400
15401 (b) allows us to cope with code like:
15402
15403 lui $4,%hi(foo)
15404 lh $4,%lo(foo+2)($4)
15405
15406 ...which is legal on RELA targets, and has a well-defined behaviour
15407 if the user knows that adding 2 to "foo" will not induce a carry to
15408 the high 16 bits.
15409
15410 When several %lo()s match a particular %got() or %hi(), we use the
15411 following rules to distinguish them:
15412
15413 (1) %lo()s with smaller offsets are a better match than %lo()s with
15414 higher offsets.
15415
15416 (2) %lo()s with no matching %got() or %hi() are better than those
15417 that already have a matching %got() or %hi().
15418
15419 (3) later %lo()s are better than earlier %lo()s.
15420
15421 These rules are applied in order.
15422
15423 (1) means, among other things, that %lo()s with identical offsets are
15424 chosen if they exist.
15425
15426 (2) means that we won't associate several high-part relocations with
15427 the same low-part relocation unless there's no alternative. Having
15428 several high parts for the same low part is a GNU extension; this rule
15429 allows careful users to avoid it.
15430
15431 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15432 with the last high-part relocation being at the front of the list.
15433 It therefore makes sense to choose the last matching low-part
15434 relocation, all other things being equal. It's also easier
15435 to code that way. */
252b5132
RH
15436
15437void
17a2f251 15438mips_frob_file (void)
252b5132
RH
15439{
15440 struct mips_hi_fixup *l;
35903be0 15441 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15442
15443 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15444 {
15445 segment_info_type *seginfo;
5b7c81bd 15446 bool matched_lo_p;
3b91255e 15447 fixS **hi_pos, **lo_pos, **pos;
252b5132 15448
9c2799c2 15449 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15450
5919d012 15451 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15452 there isn't supposed to be a matching LO. Ignore %gots against
15453 constants; we'll report an error for those later. */
738e5348 15454 if (got16_reloc_p (l->fixp->fx_r_type)
249d1bad 15455 && !pic_need_relax (l->fixp->fx_addsy))
5919d012
RS
15456 continue;
15457
15458 /* Check quickly whether the next fixup happens to be a matching %lo. */
15459 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15460 continue;
15461
252b5132 15462 seginfo = seg_info (l->seg);
252b5132 15463
3b91255e
RS
15464 /* Set HI_POS to the position of this relocation in the chain.
15465 Set LO_POS to the position of the chosen low-part relocation.
15466 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15467 relocation that matches an immediately-preceding high-part
15468 relocation. */
15469 hi_pos = NULL;
15470 lo_pos = NULL;
5b7c81bd 15471 matched_lo_p = false;
738e5348 15472 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15473
3b91255e
RS
15474 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15475 {
15476 if (*pos == l->fixp)
15477 hi_pos = pos;
15478
35903be0 15479 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15480 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15481 && (*pos)->fx_offset >= l->fixp->fx_offset
15482 && (lo_pos == NULL
15483 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15484 || (!matched_lo_p
15485 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15486 lo_pos = pos;
15487
15488 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15489 && fixup_has_matching_lo_p (*pos));
15490 }
15491
15492 /* If we found a match, remove the high-part relocation from its
15493 current position and insert it before the low-part relocation.
15494 Make the offsets match so that fixup_has_matching_lo_p()
15495 will return true.
15496
15497 We don't warn about unmatched high-part relocations since some
15498 versions of gcc have been known to emit dead "lui ...%hi(...)"
15499 instructions. */
15500 if (lo_pos != NULL)
15501 {
15502 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15503 if (l->fixp->fx_next != *lo_pos)
252b5132 15504 {
3b91255e
RS
15505 *hi_pos = l->fixp->fx_next;
15506 l->fixp->fx_next = *lo_pos;
15507 *lo_pos = l->fixp;
252b5132 15508 }
252b5132
RH
15509 }
15510 }
15511}
15512
252b5132 15513int
17a2f251 15514mips_force_relocation (fixS *fixp)
252b5132 15515{
ae6063d4 15516 if (generic_force_reloc (fixp))
252b5132
RH
15517 return 1;
15518
df58fc94
RS
15519 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15520 so that the linker relaxation can update targets. */
15521 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15522 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15523 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15524 return 1;
15525
5caa2b07
MR
15526 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15527 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15528 microMIPS symbols so that we can do cross-mode branch diagnostics
15529 and BAL to JALX conversion by the linker. */
15530 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15531 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15532 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15533 && fixp->fx_addsy
15534 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15535 return 1;
15536
7361da2c 15537 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15538 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15539 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15540 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15541 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15542 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15543 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15544 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15545 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15546 return 1;
15547
3e722fb5 15548 return 0;
252b5132
RH
15549}
15550
b416ba9b
MR
15551/* Implement TC_FORCE_RELOCATION_ABS. */
15552
5b7c81bd 15553bool
b416ba9b
MR
15554mips_force_relocation_abs (fixS *fixp)
15555{
15556 if (generic_force_reloc (fixp))
5b7c81bd 15557 return true;
b416ba9b
MR
15558
15559 /* These relocations do not have enough bits in the in-place addend
15560 to hold an arbitrary absolute section's offset. */
15561 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
5b7c81bd 15562 return true;
b416ba9b 15563
5b7c81bd 15564 return false;
b416ba9b
MR
15565}
15566
b886a2ab
RS
15567/* Read the instruction associated with RELOC from BUF. */
15568
15569static unsigned int
15570read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15571{
15572 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15573 return read_compressed_insn (buf, 4);
15574 else
15575 return read_insn (buf);
15576}
15577
15578/* Write instruction INSN to BUF, given that it has been relocated
15579 by RELOC. */
15580
15581static void
15582write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15583 unsigned long insn)
15584{
15585 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15586 write_compressed_insn (buf, insn, 4);
15587 else
15588 write_insn (buf, insn);
15589}
15590
9d862524
MR
15591/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15592 to a symbol in another ISA mode, which cannot be converted to JALX. */
15593
5b7c81bd 15594static bool
9d862524
MR
15595fix_bad_cross_mode_jump_p (fixS *fixP)
15596{
15597 unsigned long opcode;
15598 int other;
15599 char *buf;
15600
5b7c81bd
AM
15601 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15602 return false;
9d862524
MR
15603
15604 other = S_GET_OTHER (fixP->fx_addsy);
15605 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15606 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15607 switch (fixP->fx_r_type)
15608 {
15609 case BFD_RELOC_MIPS_JMP:
15610 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15611 case BFD_RELOC_MICROMIPS_JMP:
15612 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15613 default:
5b7c81bd 15614 return false;
9d862524
MR
15615 }
15616}
15617
15618/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15619 jump to a symbol in the same ISA mode. */
15620
5b7c81bd 15621static bool
9d862524
MR
15622fix_bad_same_mode_jalx_p (fixS *fixP)
15623{
15624 unsigned long opcode;
15625 int other;
15626 char *buf;
15627
5b7c81bd
AM
15628 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15629 return false;
9d862524
MR
15630
15631 other = S_GET_OTHER (fixP->fx_addsy);
15632 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15633 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15634 switch (fixP->fx_r_type)
15635 {
15636 case BFD_RELOC_MIPS_JMP:
15637 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15638 case BFD_RELOC_MIPS16_JMP:
15639 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15640 case BFD_RELOC_MICROMIPS_JMP:
15641 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15642 default:
5b7c81bd 15643 return false;
9d862524
MR
15644 }
15645}
15646
15647/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15648 to a symbol whose value plus addend is not aligned according to the
15649 ultimate (after linker relaxation) jump instruction's immediate field
15650 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15651 regular MIPS code, to (1 << 2). */
15652
5b7c81bd 15653static bool
9d862524
MR
15654fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15655{
5b7c81bd 15656 bool micro_to_mips_p;
9d862524
MR
15657 valueT val;
15658 int other;
15659
5b7c81bd
AM
15660 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15661 return false;
9d862524
MR
15662
15663 other = S_GET_OTHER (fixP->fx_addsy);
15664 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15665 val += fixP->fx_offset;
15666 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15667 && !ELF_ST_IS_MICROMIPS (other));
15668 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15669 != ELF_ST_IS_COMPRESSED (other));
15670}
15671
15672/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15673 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15674 symbols check the ISA bit instead.
15675
15676 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15677 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15678 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15679 may be converted to JALX by the linker. */
9d862524 15680
5b7c81bd 15681static bool
9d862524
MR
15682fix_bad_cross_mode_branch_p (fixS *fixP)
15683{
5b7c81bd 15684 bool absolute_p;
9d862524
MR
15685 unsigned long opcode;
15686 asection *symsec;
15687 valueT val;
15688 int other;
15689 char *buf;
15690
8b10b0b3 15691 if (mips_ignore_branch_isa)
5b7c81bd 15692 return false;
8b10b0b3 15693
5b7c81bd
AM
15694 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15695 return false;
9d862524
MR
15696
15697 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15698 absolute_p = bfd_is_abs_section (symsec);
15699
15700 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15701 other = S_GET_OTHER (fixP->fx_addsy);
15702
15703 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15704 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15705 switch (fixP->fx_r_type)
15706 {
15707 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15708 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15709 && opcode != 0x0411);
15710 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15711 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15712 && opcode != 0x4060);
9d862524
MR
15713 case BFD_RELOC_MIPS_21_PCREL_S2:
15714 case BFD_RELOC_MIPS_26_PCREL_S2:
15715 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15716 case BFD_RELOC_MIPS16_16_PCREL_S1:
15717 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15718 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15719 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15720 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15721 default:
15722 abort ();
15723 }
15724}
15725
15726/* Return TRUE if the symbol plus addend associated with a regular MIPS
15727 branch instruction pointed to by FIXP is not aligned according to the
15728 branch instruction's immediate field requirement. We need the addend
15729 to preserve the ISA bit and also the sum must not have bit 2 set. We
15730 must explicitly OR in the ISA bit from symbol annotation as the bit
15731 won't be set in the symbol's value then. */
15732
5b7c81bd 15733static bool
9d862524
MR
15734fix_bad_misaligned_branch_p (fixS *fixP)
15735{
5b7c81bd 15736 bool absolute_p;
9d862524
MR
15737 asection *symsec;
15738 valueT isa_bit;
15739 valueT val;
15740 valueT off;
15741 int other;
15742
5b7c81bd
AM
15743 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, true))
15744 return false;
9d862524
MR
15745
15746 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15747 absolute_p = bfd_is_abs_section (symsec);
15748
15749 val = S_GET_VALUE (fixP->fx_addsy);
15750 other = S_GET_OTHER (fixP->fx_addsy);
15751 off = fixP->fx_offset;
15752
15753 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15754 val |= ELF_ST_IS_COMPRESSED (other);
15755 val += off;
15756 return (val & 0x3) != isa_bit;
15757}
15758
52031738
FS
15759/* Calculate the relocation target by masking off ISA mode bit before
15760 combining symbol and addend. */
15761
15762static valueT
15763fix_bad_misaligned_address (fixS *fixP)
15764{
15765 valueT val;
15766 valueT off;
15767 unsigned isa_mode;
15768 gas_assert (fixP != NULL && fixP->fx_addsy != NULL);
15769 val = S_GET_VALUE (fixP->fx_addsy);
15770 off = fixP->fx_offset;
15771 isa_mode = (ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixP->fx_addsy))
15772 ? 1 : 0);
15773
15774 return ((val & ~isa_mode) + off);
15775}
15776
9d862524
MR
15777/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15778 and its calculated value VAL. */
15779
15780static void
15781fix_validate_branch (fixS *fixP, valueT val)
15782{
15783 if (fixP->fx_done && (val & 0x3) != 0)
15784 as_bad_where (fixP->fx_file, fixP->fx_line,
15785 _("branch to misaligned address (0x%lx)"),
15786 (long) (val + md_pcrel_from (fixP)));
15787 else if (fix_bad_cross_mode_branch_p (fixP))
15788 as_bad_where (fixP->fx_file, fixP->fx_line,
15789 _("branch to a symbol in another ISA mode"));
15790 else if (fix_bad_misaligned_branch_p (fixP))
15791 as_bad_where (fixP->fx_file, fixP->fx_line,
15792 _("branch to misaligned address (0x%lx)"),
52031738 15793 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15794 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15795 as_bad_where (fixP->fx_file, fixP->fx_line,
15796 _("cannot encode misaligned addend "
15797 "in the relocatable field (0x%lx)"),
15798 (long) fixP->fx_offset);
15799}
15800
252b5132
RH
15801/* Apply a fixup to the object file. */
15802
94f592af 15803void
55cf6793 15804md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15805{
4d68580a 15806 char *buf;
b886a2ab 15807 unsigned long insn;
a7ebbfdf 15808 reloc_howto_type *howto;
252b5132 15809
d56a8dda
RS
15810 if (fixP->fx_pcrel)
15811 switch (fixP->fx_r_type)
15812 {
15813 case BFD_RELOC_16_PCREL_S2:
c9775dde 15814 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15815 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15816 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15817 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15818 case BFD_RELOC_32_PCREL:
7361da2c
AB
15819 case BFD_RELOC_MIPS_21_PCREL_S2:
15820 case BFD_RELOC_MIPS_26_PCREL_S2:
15821 case BFD_RELOC_MIPS_18_PCREL_S3:
15822 case BFD_RELOC_MIPS_19_PCREL_S2:
15823 case BFD_RELOC_HI16_S_PCREL:
15824 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15825 break;
15826
15827 case BFD_RELOC_32:
15828 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15829 break;
15830
15831 default:
15832 as_bad_where (fixP->fx_file, fixP->fx_line,
15833 _("PC-relative reference to a different section"));
15834 break;
15835 }
15836
d712f276
AM
15837 /* Handle BFD_RELOC_8 and BFD_RELOC_16. Punt on other bfd
15838 relocations that have no MIPS ELF equivalent. */
15839 if (fixP->fx_r_type != BFD_RELOC_8
15840 && fixP->fx_r_type != BFD_RELOC_16)
d56a8dda
RS
15841 {
15842 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15843 if (!howto)
15844 return;
15845 }
65551fa4 15846
df58fc94
RS
15847 gas_assert (fixP->fx_size == 2
15848 || fixP->fx_size == 4
d56a8dda 15849 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15850 || fixP->fx_r_type == BFD_RELOC_64
15851 || fixP->fx_r_type == BFD_RELOC_CTOR
15852 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15853 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15854 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15855 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15856 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15857 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15858
4d68580a 15859 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15860
b1dca8ee
RS
15861 /* Don't treat parts of a composite relocation as done. There are two
15862 reasons for this:
15863
15864 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15865 should nevertheless be emitted if the first part is.
15866
15867 (2) In normal usage, composite relocations are never assembly-time
15868 constants. The easiest way of dealing with the pathological
15869 exceptions is to generate a relocation against STN_UNDEF and
15870 leave everything up to the linker. */
3994f87e 15871 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15872 fixP->fx_done = 1;
15873
15874 switch (fixP->fx_r_type)
15875 {
3f98094e
DJ
15876 case BFD_RELOC_MIPS_TLS_GD:
15877 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15878 case BFD_RELOC_MIPS_TLS_DTPREL32:
15879 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15880 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15881 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15882 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15883 case BFD_RELOC_MIPS_TLS_TPREL32:
15884 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15885 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15886 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15887 case BFD_RELOC_MICROMIPS_TLS_GD:
15888 case BFD_RELOC_MICROMIPS_TLS_LDM:
15889 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15890 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15891 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15892 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15893 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15894 case BFD_RELOC_MIPS16_TLS_GD:
15895 case BFD_RELOC_MIPS16_TLS_LDM:
15896 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15897 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15898 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15899 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15900 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15901 if (fixP->fx_addsy)
15902 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15903 else
15904 as_bad_where (fixP->fx_file, fixP->fx_line,
15905 _("TLS relocation against a constant"));
15906 break;
3f98094e 15907
252b5132 15908 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15909 case BFD_RELOC_MIPS16_JMP:
15910 case BFD_RELOC_MICROMIPS_JMP:
15911 {
15912 int shift;
15913
15914 gas_assert (!fixP->fx_done);
15915
15916 /* Shift is 2, unusually, for microMIPS JALX. */
15917 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15918 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15919 shift = 1;
15920 else
15921 shift = 2;
15922
15923 if (fix_bad_cross_mode_jump_p (fixP))
15924 as_bad_where (fixP->fx_file, fixP->fx_line,
15925 _("jump to a symbol in another ISA mode"));
15926 else if (fix_bad_same_mode_jalx_p (fixP))
15927 as_bad_where (fixP->fx_file, fixP->fx_line,
15928 _("JALX to a symbol in the same ISA mode"));
15929 else if (fix_bad_misaligned_jump_p (fixP, shift))
15930 as_bad_where (fixP->fx_file, fixP->fx_line,
15931 _("jump to misaligned address (0x%lx)"),
52031738 15932 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
15933 else if (HAVE_IN_PLACE_ADDENDS
15934 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15935 as_bad_where (fixP->fx_file, fixP->fx_line,
15936 _("cannot encode misaligned addend "
15937 "in the relocatable field (0x%lx)"),
15938 (long) fixP->fx_offset);
15939 }
15940 /* Fall through. */
15941
e369bcce
TS
15942 case BFD_RELOC_MIPS_SHIFT5:
15943 case BFD_RELOC_MIPS_SHIFT6:
15944 case BFD_RELOC_MIPS_GOT_DISP:
15945 case BFD_RELOC_MIPS_GOT_PAGE:
15946 case BFD_RELOC_MIPS_GOT_OFST:
15947 case BFD_RELOC_MIPS_SUB:
15948 case BFD_RELOC_MIPS_INSERT_A:
15949 case BFD_RELOC_MIPS_INSERT_B:
15950 case BFD_RELOC_MIPS_DELETE:
15951 case BFD_RELOC_MIPS_HIGHEST:
15952 case BFD_RELOC_MIPS_HIGHER:
15953 case BFD_RELOC_MIPS_SCN_DISP:
e369bcce
TS
15954 case BFD_RELOC_MIPS_RELGOT:
15955 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15956 case BFD_RELOC_HI16:
15957 case BFD_RELOC_HI16_S:
b886a2ab 15958 case BFD_RELOC_LO16:
cdf6fd85 15959 case BFD_RELOC_GPREL16:
252b5132
RH
15960 case BFD_RELOC_MIPS_LITERAL:
15961 case BFD_RELOC_MIPS_CALL16:
15962 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15963 case BFD_RELOC_GPREL32:
252b5132
RH
15964 case BFD_RELOC_MIPS_GOT_HI16:
15965 case BFD_RELOC_MIPS_GOT_LO16:
15966 case BFD_RELOC_MIPS_CALL_HI16:
15967 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15968 case BFD_RELOC_HI16_S_PCREL:
15969 case BFD_RELOC_LO16_PCREL:
252b5132 15970 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15971 case BFD_RELOC_MIPS16_GOT16:
15972 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15973 case BFD_RELOC_MIPS16_HI16:
15974 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15975 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15976 case BFD_RELOC_MICROMIPS_GOT_DISP:
15977 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15978 case BFD_RELOC_MICROMIPS_GOT_OFST:
15979 case BFD_RELOC_MICROMIPS_SUB:
15980 case BFD_RELOC_MICROMIPS_HIGHEST:
15981 case BFD_RELOC_MICROMIPS_HIGHER:
15982 case BFD_RELOC_MICROMIPS_SCN_DISP:
15983 case BFD_RELOC_MICROMIPS_JALR:
15984 case BFD_RELOC_MICROMIPS_HI16:
15985 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15986 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15987 case BFD_RELOC_MICROMIPS_GPREL16:
15988 case BFD_RELOC_MICROMIPS_LITERAL:
15989 case BFD_RELOC_MICROMIPS_CALL16:
15990 case BFD_RELOC_MICROMIPS_GOT16:
15991 case BFD_RELOC_MICROMIPS_GOT_HI16:
15992 case BFD_RELOC_MICROMIPS_GOT_LO16:
15993 case BFD_RELOC_MICROMIPS_CALL_HI16:
15994 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15995 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15996 if (fixP->fx_done)
15997 {
15998 offsetT value;
15999
16000 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
16001 {
16002 insn = read_reloc_insn (buf, fixP->fx_r_type);
16003 if (mips16_reloc_p (fixP->fx_r_type))
16004 insn |= mips16_immed_extend (value, 16);
16005 else
16006 insn |= (value & 0xffff);
16007 write_reloc_insn (buf, fixP->fx_r_type, insn);
16008 }
16009 else
16010 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16011 _("unsupported constant in relocation"));
b886a2ab 16012 }
252b5132
RH
16013 break;
16014
252b5132
RH
16015 case BFD_RELOC_64:
16016 /* This is handled like BFD_RELOC_32, but we output a sign
16017 extended value if we are only 32 bits. */
3e722fb5 16018 if (fixP->fx_done)
252b5132
RH
16019 {
16020 if (8 <= sizeof (valueT))
4d68580a 16021 md_number_to_chars (buf, *valP, 8);
252b5132
RH
16022 else
16023 {
a7ebbfdf 16024 valueT hiv;
252b5132 16025
a7ebbfdf 16026 if ((*valP & 0x80000000) != 0)
252b5132
RH
16027 hiv = 0xffffffff;
16028 else
16029 hiv = 0;
4d68580a
RS
16030 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
16031 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
16032 }
16033 }
16034 break;
16035
056350c6 16036 case BFD_RELOC_RVA:
252b5132 16037 case BFD_RELOC_32:
b47468a6 16038 case BFD_RELOC_32_PCREL:
d712f276 16039 case BFD_RELOC_MIPS_16:
252b5132 16040 case BFD_RELOC_16:
d56a8dda 16041 case BFD_RELOC_8:
252b5132 16042 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
16043 value now. This can happen if we have a .word which is not
16044 resolved when it appears but is later defined. */
252b5132 16045 if (fixP->fx_done)
4d68580a 16046 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
16047 break;
16048
7361da2c 16049 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 16050 fix_validate_branch (fixP, *valP);
41947d9e
MR
16051 if (!fixP->fx_done)
16052 break;
16053
16054 if (*valP + 0x400000 <= 0x7fffff)
16055 {
16056 insn = read_insn (buf);
16057 insn |= (*valP >> 2) & 0x1fffff;
16058 write_insn (buf, insn);
16059 }
16060 else
16061 as_bad_where (fixP->fx_file, fixP->fx_line,
16062 _("branch out of range"));
16063 break;
16064
7361da2c 16065 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 16066 fix_validate_branch (fixP, *valP);
41947d9e
MR
16067 if (!fixP->fx_done)
16068 break;
7361da2c 16069
41947d9e
MR
16070 if (*valP + 0x8000000 <= 0xfffffff)
16071 {
16072 insn = read_insn (buf);
16073 insn |= (*valP >> 2) & 0x3ffffff;
16074 write_insn (buf, insn);
16075 }
16076 else
16077 as_bad_where (fixP->fx_file, fixP->fx_line,
16078 _("branch out of range"));
7361da2c
AB
16079 break;
16080
16081 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 16082 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 16083 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
16084 _("PC-relative access using misaligned symbol (%lx)"),
16085 (long) S_GET_VALUE (fixP->fx_addsy));
16086 if ((fixP->fx_offset & 0x7) != 0)
16087 as_bad_where (fixP->fx_file, fixP->fx_line,
16088 _("PC-relative access using misaligned offset (%lx)"),
16089 (long) fixP->fx_offset);
41947d9e
MR
16090 if (!fixP->fx_done)
16091 break;
7361da2c 16092
41947d9e
MR
16093 if (*valP + 0x100000 <= 0x1fffff)
16094 {
16095 insn = read_insn (buf);
16096 insn |= (*valP >> 3) & 0x3ffff;
16097 write_insn (buf, insn);
16098 }
16099 else
16100 as_bad_where (fixP->fx_file, fixP->fx_line,
16101 _("PC-relative access out of range"));
7361da2c
AB
16102 break;
16103
16104 case BFD_RELOC_MIPS_19_PCREL_S2:
16105 if ((*valP & 0x3) != 0)
16106 as_bad_where (fixP->fx_file, fixP->fx_line,
16107 _("PC-relative access to misaligned address (%lx)"),
717ba204 16108 (long) *valP);
41947d9e
MR
16109 if (!fixP->fx_done)
16110 break;
7361da2c 16111
41947d9e
MR
16112 if (*valP + 0x100000 <= 0x1fffff)
16113 {
16114 insn = read_insn (buf);
16115 insn |= (*valP >> 2) & 0x7ffff;
16116 write_insn (buf, insn);
16117 }
16118 else
16119 as_bad_where (fixP->fx_file, fixP->fx_line,
16120 _("PC-relative access out of range"));
7361da2c
AB
16121 break;
16122
252b5132 16123 case BFD_RELOC_16_PCREL_S2:
9d862524 16124 fix_validate_branch (fixP, *valP);
cb56d3d3 16125
54f4ddb3
TS
16126 /* We need to save the bits in the instruction since fixup_segment()
16127 might be deleting the relocation entry (i.e., a branch within
16128 the current segment). */
a7ebbfdf 16129 if (! fixP->fx_done)
bb2d6cd7 16130 break;
252b5132 16131
54f4ddb3 16132 /* Update old instruction data. */
4d68580a 16133 insn = read_insn (buf);
252b5132 16134
a7ebbfdf
TS
16135 if (*valP + 0x20000 <= 0x3ffff)
16136 {
16137 insn |= (*valP >> 2) & 0xffff;
4d68580a 16138 write_insn (buf, insn);
a7ebbfdf 16139 }
ce8ad872 16140 else if (fixP->fx_tcbit2
a7ebbfdf
TS
16141 && fixP->fx_done
16142 && fixP->fx_frag->fr_address >= text_section->vma
16143 && (fixP->fx_frag->fr_address
fd361982 16144 < text_section->vma + bfd_section_size (text_section))
a7ebbfdf
TS
16145 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
16146 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
16147 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
16148 {
16149 /* The branch offset is too large. If this is an
16150 unconditional branch, and we are not generating PIC code,
16151 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
16152 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
16153 insn = 0x0c000000; /* jal */
252b5132 16154 else
a7ebbfdf
TS
16155 insn = 0x08000000; /* j */
16156 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
16157 fixP->fx_done = 0;
16158 fixP->fx_addsy = section_symbol (text_section);
16159 *valP += md_pcrel_from (fixP);
4d68580a 16160 write_insn (buf, insn);
a7ebbfdf
TS
16161 }
16162 else
16163 {
16164 /* If we got here, we have branch-relaxation disabled,
16165 and there's nothing we can do to fix this instruction
16166 without turning it into a longer sequence. */
16167 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 16168 _("branch out of range"));
252b5132 16169 }
252b5132
RH
16170 break;
16171
c9775dde 16172 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
16173 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
16174 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
16175 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 16176 gas_assert (!fixP->fx_done);
9d862524
MR
16177 if (fix_bad_cross_mode_branch_p (fixP))
16178 as_bad_where (fixP->fx_file, fixP->fx_line,
16179 _("branch to a symbol in another ISA mode"));
16180 else if (fixP->fx_addsy
5b7c81bd 16181 && !S_FORCE_RELOC (fixP->fx_addsy, true)
9d862524
MR
16182 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
16183 && (fixP->fx_offset & 0x1) != 0)
16184 as_bad_where (fixP->fx_file, fixP->fx_line,
16185 _("branch to misaligned address (0x%lx)"),
52031738 16186 (long) fix_bad_misaligned_address (fixP));
9d862524
MR
16187 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
16188 as_bad_where (fixP->fx_file, fixP->fx_line,
16189 _("cannot encode misaligned addend "
16190 "in the relocatable field (0x%lx)"),
16191 (long) fixP->fx_offset);
df58fc94
RS
16192 break;
16193
252b5132
RH
16194 case BFD_RELOC_VTABLE_INHERIT:
16195 fixP->fx_done = 0;
16196 if (fixP->fx_addsy
16197 && !S_IS_DEFINED (fixP->fx_addsy)
16198 && !S_IS_WEAK (fixP->fx_addsy))
16199 S_SET_WEAK (fixP->fx_addsy);
16200 break;
16201
2f0c68f2 16202 case BFD_RELOC_NONE:
252b5132
RH
16203 case BFD_RELOC_VTABLE_ENTRY:
16204 fixP->fx_done = 0;
16205 break;
16206
16207 default:
b37df7c4 16208 abort ();
252b5132 16209 }
a7ebbfdf
TS
16210
16211 /* Remember value for tc_gen_reloc. */
16212 fixP->fx_addnumber = *valP;
252b5132
RH
16213}
16214
252b5132 16215static symbolS *
17a2f251 16216get_symbol (void)
252b5132
RH
16217{
16218 int c;
16219 char *name;
16220 symbolS *p;
16221
d02603dc 16222 c = get_symbol_name (&name);
252b5132 16223 p = (symbolS *) symbol_find_or_make (name);
d02603dc 16224 (void) restore_line_pointer (c);
252b5132
RH
16225 return p;
16226}
16227
742a56fe
RS
16228/* Align the current frag to a given power of two. If a particular
16229 fill byte should be used, FILL points to an integer that contains
16230 that byte, otherwise FILL is null.
16231
462427c4
RS
16232 This function used to have the comment:
16233
16234 The MIPS assembler also automatically adjusts any preceding label.
16235
16236 The implementation therefore applied the adjustment to a maximum of
16237 one label. However, other label adjustments are applied to batches
16238 of labels, and adjusting just one caused problems when new labels
16239 were added for the sake of debugging or unwind information.
16240 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
16241
16242static void
462427c4 16243mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 16244{
7d10b47d 16245 mips_emit_delays ();
df58fc94 16246 mips_record_compressed_mode ();
742a56fe
RS
16247 if (fill == NULL && subseg_text_p (now_seg))
16248 frag_align_code (to, 0);
16249 else
16250 frag_align (to, fill ? *fill : 0, 0);
252b5132 16251 record_alignment (now_seg, to);
770c0151 16252 mips_move_labels (labels, subseg_text_p (now_seg));
252b5132
RH
16253}
16254
16255/* Align to a given power of two. .align 0 turns off the automatic
16256 alignment used by the data creating pseudo-ops. */
16257
16258static void
17a2f251 16259s_align (int x ATTRIBUTE_UNUSED)
252b5132 16260{
742a56fe 16261 int temp, fill_value, *fill_ptr;
49954fb4 16262 long max_alignment = 28;
252b5132 16263
5999477d
AM
16264 file_mips_check_options ();
16265
54f4ddb3 16266 /* o Note that the assembler pulls down any immediately preceding label
252b5132 16267 to the aligned address.
54f4ddb3 16268 o It's not documented but auto alignment is reinstated by
252b5132 16269 a .align pseudo instruction.
54f4ddb3 16270 o Note also that after auto alignment is turned off the mips assembler
252b5132 16271 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 16272 We don't. */
252b5132
RH
16273
16274 temp = get_absolute_expression ();
16275 if (temp > max_alignment)
1661c76c 16276 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
16277 else if (temp < 0)
16278 {
1661c76c 16279 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
16280 temp = 0;
16281 }
16282 if (*input_line_pointer == ',')
16283 {
f9419b05 16284 ++input_line_pointer;
742a56fe
RS
16285 fill_value = get_absolute_expression ();
16286 fill_ptr = &fill_value;
252b5132
RH
16287 }
16288 else
742a56fe 16289 fill_ptr = 0;
5999477d
AM
16290
16291 mips_mark_labels ();
16292
252b5132
RH
16293 if (temp)
16294 {
a8dbcb85
TS
16295 segment_info_type *si = seg_info (now_seg);
16296 struct insn_label_list *l = si->label_list;
54f4ddb3 16297 /* Auto alignment should be switched on by next section change. */
252b5132 16298 auto_align = 1;
462427c4 16299 mips_align (temp, fill_ptr, l);
252b5132
RH
16300 }
16301 else
16302 {
16303 auto_align = 0;
16304 }
16305
16306 demand_empty_rest_of_line ();
16307}
16308
252b5132 16309static void
17a2f251 16310s_change_sec (int sec)
252b5132
RH
16311{
16312 segT seg;
16313
252b5132
RH
16314 /* The ELF backend needs to know that we are changing sections, so
16315 that .previous works correctly. We could do something like check
b6ff326e 16316 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16317 as it would not be appropriate to use it in the section changing
16318 functions in read.c, since obj-elf.c intercepts those. FIXME:
16319 This should be cleaner, somehow. */
f3ded42a 16320 obj_elf_section_change_hook ();
252b5132 16321
7d10b47d 16322 mips_emit_delays ();
6a32d874 16323
252b5132
RH
16324 switch (sec)
16325 {
16326 case 't':
16327 s_text (0);
16328 break;
16329 case 'd':
16330 s_data (0);
16331 break;
16332 case 'b':
16333 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16334 demand_empty_rest_of_line ();
16335 break;
16336
16337 case 'r':
4d0d148d
TS
16338 seg = subseg_new (RDATA_SECTION_NAME,
16339 (subsegT) get_absolute_expression ());
fd361982
AM
16340 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_READONLY
16341 | SEC_RELOC | SEC_DATA));
d34049e8 16342 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16343 record_alignment (seg, 4);
4d0d148d 16344 demand_empty_rest_of_line ();
252b5132
RH
16345 break;
16346
16347 case 's':
4d0d148d 16348 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
a4dd6c97
AM
16349 bfd_set_section_flags (seg, (SEC_ALLOC | SEC_LOAD | SEC_RELOC
16350 | SEC_DATA | SEC_SMALL_DATA));
d34049e8 16351 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16352 record_alignment (seg, 4);
4d0d148d
TS
16353 demand_empty_rest_of_line ();
16354 break;
998b3c36
MR
16355
16356 case 'B':
16357 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
a4dd6c97 16358 bfd_set_section_flags (seg, SEC_ALLOC | SEC_SMALL_DATA);
d34049e8 16359 if (!startswith (TARGET_OS, "elf"))
f3ded42a 16360 record_alignment (seg, 4);
998b3c36
MR
16361 demand_empty_rest_of_line ();
16362 break;
252b5132
RH
16363 }
16364
16365 auto_align = 1;
16366}
b34976b6 16367
cca86cc8 16368void
17a2f251 16369s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16370{
d02603dc 16371 char *saved_ilp;
16849591
JB
16372 const char *section_name;
16373 char c, next_c = 0;
cca86cc8
SC
16374 int section_type;
16375 int section_flag;
16376 int section_entry_size;
16377 int section_alignment;
b34976b6 16378
d02603dc 16379 saved_ilp = input_line_pointer;
16849591
JB
16380 section_name = obj_elf_section_name ();
16381 if (section_name == NULL)
16382 return;
16383 c = input_line_pointer[0];
a816d1ed 16384 if (c)
16849591 16385 next_c = input_line_pointer[1];
cca86cc8 16386
4cf0dd0d
TS
16387 /* Do we have .section Name<,"flags">? */
16388 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16389 {
d02603dc 16390 input_line_pointer = saved_ilp;
cca86cc8
SC
16391 obj_elf_section (ignore);
16392 return;
16393 }
d02603dc 16394
cca86cc8
SC
16395 input_line_pointer++;
16396
16397 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16398 if (c == ',')
16399 section_type = get_absolute_expression ();
16400 else
16401 section_type = 0;
d02603dc 16402
cca86cc8
SC
16403 if (*input_line_pointer++ == ',')
16404 section_flag = get_absolute_expression ();
16405 else
16406 section_flag = 0;
d02603dc 16407
cca86cc8
SC
16408 if (*input_line_pointer++ == ',')
16409 section_entry_size = get_absolute_expression ();
16410 else
16411 section_entry_size = 0;
d02603dc 16412
cca86cc8
SC
16413 if (*input_line_pointer++ == ',')
16414 section_alignment = get_absolute_expression ();
16415 else
16416 section_alignment = 0;
d02603dc 16417
87975d2a
AM
16418 /* FIXME: really ignore? */
16419 (void) section_alignment;
cca86cc8 16420
8ab8a5c8
RS
16421 /* When using the generic form of .section (as implemented by obj-elf.c),
16422 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16423 traditionally had to fall back on the more common @progbits instead.
16424
16425 There's nothing really harmful in this, since bfd will correct
16426 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16427 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16428 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16429
16430 Even so, we shouldn't force users of the MIPS .section syntax to
16431 incorrectly label the sections as SHT_PROGBITS. The best compromise
16432 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16433 generic type-checking code. */
16434 if (section_type == SHT_MIPS_DWARF)
16435 section_type = SHT_PROGBITS;
16436
a8c4d40b 16437 obj_elf_change_section (section_name, section_type, section_flag,
cca86cc8
SC
16438 section_entry_size, 0, 0, 0);
16439}
252b5132
RH
16440
16441void
17a2f251 16442mips_enable_auto_align (void)
252b5132
RH
16443{
16444 auto_align = 1;
16445}
16446
16447static void
17a2f251 16448s_cons (int log_size)
252b5132 16449{
a8dbcb85
TS
16450 segment_info_type *si = seg_info (now_seg);
16451 struct insn_label_list *l = si->label_list;
252b5132 16452
7d10b47d 16453 mips_emit_delays ();
252b5132 16454 if (log_size > 0 && auto_align)
462427c4 16455 mips_align (log_size, 0, l);
252b5132 16456 cons (1 << log_size);
a1facbec 16457 mips_clear_insn_labels ();
252b5132
RH
16458}
16459
16460static void
17a2f251 16461s_float_cons (int type)
252b5132 16462{
a8dbcb85
TS
16463 segment_info_type *si = seg_info (now_seg);
16464 struct insn_label_list *l = si->label_list;
252b5132 16465
7d10b47d 16466 mips_emit_delays ();
252b5132
RH
16467
16468 if (auto_align)
49309057
ILT
16469 {
16470 if (type == 'd')
462427c4 16471 mips_align (3, 0, l);
49309057 16472 else
462427c4 16473 mips_align (2, 0, l);
49309057 16474 }
252b5132 16475
252b5132 16476 float_cons (type);
a1facbec 16477 mips_clear_insn_labels ();
252b5132
RH
16478}
16479
16480/* Handle .globl. We need to override it because on Irix 5 you are
16481 permitted to say
16482 .globl foo .text
16483 where foo is an undefined symbol, to mean that foo should be
16484 considered to be the address of a function. */
16485
16486static void
17a2f251 16487s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16488{
16489 char *name;
16490 int c;
16491 symbolS *symbolP;
252b5132 16492
8a06b769 16493 do
252b5132 16494 {
d02603dc 16495 c = get_symbol_name (&name);
8a06b769
TS
16496 symbolP = symbol_find_or_make (name);
16497 S_SET_EXTERNAL (symbolP);
16498
252b5132 16499 *input_line_pointer = c;
d02603dc 16500 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16501
8a06b769
TS
16502 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16503 && (*input_line_pointer != ','))
16504 {
16505 char *secname;
16506 asection *sec;
16507
d02603dc 16508 c = get_symbol_name (&secname);
8a06b769
TS
16509 sec = bfd_get_section_by_name (stdoutput, secname);
16510 if (sec == NULL)
16511 as_bad (_("%s: no such section"), secname);
d02603dc 16512 (void) restore_line_pointer (c);
8a06b769
TS
16513
16514 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
d69cd47e 16515 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
8a06b769
TS
16516 }
16517
8a06b769
TS
16518 c = *input_line_pointer;
16519 if (c == ',')
16520 {
16521 input_line_pointer++;
16522 SKIP_WHITESPACE ();
16523 if (is_end_of_line[(unsigned char) *input_line_pointer])
16524 c = '\n';
16525 }
16526 }
16527 while (c == ',');
252b5132 16528
252b5132
RH
16529 demand_empty_rest_of_line ();
16530}
16531
d69cd47e
AM
16532#ifdef TE_IRIX
16533/* The Irix 5 and 6 assemblers set the type of any common symbol and
16534 any undefined non-function symbol to STT_OBJECT. We try to be
16535 compatible, since newer Irix 5 and 6 linkers care. */
16536
16537void
16538mips_frob_symbol (symbolS *symp ATTRIBUTE_UNUSED)
16539{
16540 /* This late in assembly we can set BSF_OBJECT indiscriminately
16541 and let elf.c:swap_out_syms sort out the symbol type. */
16542 flagword *flags = &symbol_get_bfdsym (symp)->flags;
16543 if ((*flags & (BSF_GLOBAL | BSF_WEAK)) != 0
16544 || !S_IS_DEFINED (symp))
16545 *flags |= BSF_OBJECT;
16546}
16547#endif
16548
252b5132 16549static void
17a2f251 16550s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16551{
16552 char *opt;
16553 char c;
16554
d02603dc 16555 c = get_symbol_name (&opt);
252b5132
RH
16556
16557 if (*opt == 'O')
16558 {
16559 /* FIXME: What does this mean? */
16560 }
d34049e8 16561 else if (startswith (opt, "pic") && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16562 {
16563 int i;
16564
16565 i = atoi (opt + 3);
668c5ebc
MR
16566 if (i != 0 && i != 2)
16567 as_bad (_(".option pic%d not supported"), i);
16568 else if (mips_pic == VXWORKS_PIC)
16569 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16570 else if (i == 0)
252b5132
RH
16571 mips_pic = NO_PIC;
16572 else if (i == 2)
143d77c5 16573 {
8b828383 16574 mips_pic = SVR4_PIC;
5b7c81bd 16575 mips_abicalls = true;
143d77c5 16576 }
252b5132 16577
4d0d148d 16578 if (mips_pic == SVR4_PIC)
252b5132
RH
16579 {
16580 if (g_switch_seen && g_switch_value != 0)
16581 as_warn (_("-G may not be used with SVR4 PIC code"));
16582 g_switch_value = 0;
16583 bfd_set_gp_size (stdoutput, 0);
16584 }
16585 }
16586 else
1661c76c 16587 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16588
d02603dc 16589 (void) restore_line_pointer (c);
252b5132
RH
16590 demand_empty_rest_of_line ();
16591}
16592
16593/* This structure is used to hold a stack of .set values. */
16594
e972090a
NC
16595struct mips_option_stack
16596{
252b5132
RH
16597 struct mips_option_stack *next;
16598 struct mips_set_options options;
16599};
16600
16601static struct mips_option_stack *mips_opts_stack;
16602
22522f88
MR
16603/* Return status for .set/.module option handling. */
16604
16605enum code_option_type
16606{
16607 /* Unrecognized option. */
16608 OPTION_TYPE_BAD = -1,
16609
16610 /* Ordinary option. */
16611 OPTION_TYPE_NORMAL,
16612
16613 /* ISA changing option. */
16614 OPTION_TYPE_ISA
16615};
16616
16617/* Handle common .set/.module options. Return status indicating option
16618 type. */
16619
16620static enum code_option_type
919731af 16621parse_code_option (char * name)
252b5132 16622{
5b7c81bd 16623 bool isa_set = false;
c6278170 16624 const struct mips_ase *ase;
22522f88 16625
d34049e8 16626 if (startswith (name, "at="))
741fe287
MR
16627 {
16628 char *s = name + 3;
16629
16630 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16631 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16632 }
252b5132 16633 else if (strcmp (name, "at") == 0)
919731af 16634 mips_opts.at = ATREG;
252b5132 16635 else if (strcmp (name, "noat") == 0)
919731af 16636 mips_opts.at = ZERO;
252b5132 16637 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16638 mips_opts.nomove = 0;
252b5132 16639 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16640 mips_opts.nomove = 1;
252b5132 16641 else if (strcmp (name, "bopt") == 0)
919731af 16642 mips_opts.nobopt = 0;
252b5132 16643 else if (strcmp (name, "nobopt") == 0)
919731af 16644 mips_opts.nobopt = 1;
ad3fea08 16645 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16646 mips_opts.gp = 32;
ad3fea08 16647 else if (strcmp (name, "gp=64") == 0)
919731af 16648 mips_opts.gp = 64;
ad3fea08 16649 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16650 mips_opts.fp = 32;
351cdf24
MF
16651 else if (strcmp (name, "fp=xx") == 0)
16652 mips_opts.fp = 0;
ad3fea08 16653 else if (strcmp (name, "fp=64") == 0)
919731af 16654 mips_opts.fp = 64;
037b32b9
AN
16655 else if (strcmp (name, "softfloat") == 0)
16656 mips_opts.soft_float = 1;
16657 else if (strcmp (name, "hardfloat") == 0)
16658 mips_opts.soft_float = 0;
16659 else if (strcmp (name, "singlefloat") == 0)
16660 mips_opts.single_float = 1;
16661 else if (strcmp (name, "doublefloat") == 0)
16662 mips_opts.single_float = 0;
351cdf24
MF
16663 else if (strcmp (name, "nooddspreg") == 0)
16664 mips_opts.oddspreg = 0;
16665 else if (strcmp (name, "oddspreg") == 0)
16666 mips_opts.oddspreg = 1;
252b5132
RH
16667 else if (strcmp (name, "mips16") == 0
16668 || strcmp (name, "MIPS-16") == 0)
919731af 16669 mips_opts.mips16 = 1;
252b5132
RH
16670 else if (strcmp (name, "nomips16") == 0
16671 || strcmp (name, "noMIPS-16") == 0)
16672 mips_opts.mips16 = 0;
df58fc94 16673 else if (strcmp (name, "micromips") == 0)
919731af 16674 mips_opts.micromips = 1;
df58fc94
RS
16675 else if (strcmp (name, "nomicromips") == 0)
16676 mips_opts.micromips = 0;
c6278170
RS
16677 else if (name[0] == 'n'
16678 && name[1] == 'o'
16679 && (ase = mips_lookup_ase (name + 2)))
5b7c81bd 16680 mips_set_ase (ase, &mips_opts, false);
c6278170 16681 else if ((ase = mips_lookup_ase (name)))
5b7c81bd 16682 mips_set_ase (ase, &mips_opts, true);
d34049e8 16683 else if (startswith (name, "mips") || startswith (name, "arch="))
252b5132 16684 {
1a2c1fad
CD
16685 /* Permit the user to change the ISA and architecture on the fly.
16686 Needless to say, misuse can cause serious problems. */
d34049e8 16687 if (startswith (name, "arch="))
1a2c1fad
CD
16688 {
16689 const struct mips_cpu_info *p;
16690
919731af 16691 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16692 if (!p)
16693 as_bad (_("unknown architecture %s"), name + 5);
16694 else
16695 {
16696 mips_opts.arch = p->cpu;
16697 mips_opts.isa = p->isa;
5b7c81bd 16698 isa_set = true;
3315614d 16699 mips_opts.init_ase = p->ase;
1a2c1fad
CD
16700 }
16701 }
d34049e8 16702 else if (startswith (name, "mips"))
81a21e38
TS
16703 {
16704 const struct mips_cpu_info *p;
16705
919731af 16706 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16707 if (!p)
16708 as_bad (_("unknown ISA level %s"), name + 4);
16709 else
16710 {
16711 mips_opts.arch = p->cpu;
16712 mips_opts.isa = p->isa;
5b7c81bd 16713 isa_set = true;
3315614d 16714 mips_opts.init_ase = p->ase;
81a21e38
TS
16715 }
16716 }
af7ee8bf 16717 else
81a21e38 16718 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16719 }
16720 else if (strcmp (name, "autoextend") == 0)
16721 mips_opts.noautoextend = 0;
16722 else if (strcmp (name, "noautoextend") == 0)
16723 mips_opts.noautoextend = 1;
833794fc 16724 else if (strcmp (name, "insn32") == 0)
5b7c81bd 16725 mips_opts.insn32 = true;
833794fc 16726 else if (strcmp (name, "noinsn32") == 0)
5b7c81bd 16727 mips_opts.insn32 = false;
919731af 16728 else if (strcmp (name, "sym32") == 0)
5b7c81bd 16729 mips_opts.sym32 = true;
919731af 16730 else if (strcmp (name, "nosym32") == 0)
5b7c81bd 16731 mips_opts.sym32 = false;
919731af 16732 else
22522f88
MR
16733 return OPTION_TYPE_BAD;
16734
16735 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16736}
16737
16738/* Handle the .set pseudo-op. */
16739
16740static void
16741s_mipsset (int x ATTRIBUTE_UNUSED)
16742{
22522f88 16743 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16744 char *name = input_line_pointer, ch;
919731af 16745
16746 file_mips_check_options ();
16747
16748 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16749 ++input_line_pointer;
16750 ch = *input_line_pointer;
16751 *input_line_pointer = '\0';
16752
16753 if (strchr (name, ','))
16754 {
16755 /* Generic ".set" directive; use the generic handler. */
16756 *input_line_pointer = ch;
16757 input_line_pointer = name;
16758 s_set (0);
16759 return;
16760 }
16761
16762 if (strcmp (name, "reorder") == 0)
16763 {
16764 if (mips_opts.noreorder)
16765 end_noreorder ();
16766 }
16767 else if (strcmp (name, "noreorder") == 0)
16768 {
16769 if (!mips_opts.noreorder)
16770 start_noreorder ();
16771 }
16772 else if (strcmp (name, "macro") == 0)
16773 mips_opts.warn_about_macros = 0;
16774 else if (strcmp (name, "nomacro") == 0)
16775 {
16776 if (mips_opts.noreorder == 0)
16777 as_bad (_("`noreorder' must be set before `nomacro'"));
16778 mips_opts.warn_about_macros = 1;
16779 }
16780 else if (strcmp (name, "gp=default") == 0)
16781 mips_opts.gp = file_mips_opts.gp;
16782 else if (strcmp (name, "fp=default") == 0)
16783 mips_opts.fp = file_mips_opts.fp;
16784 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16785 {
16786 mips_opts.isa = file_mips_opts.isa;
16787 mips_opts.arch = file_mips_opts.arch;
3315614d 16788 mips_opts.init_ase = file_mips_opts.init_ase;
919731af 16789 mips_opts.gp = file_mips_opts.gp;
16790 mips_opts.fp = file_mips_opts.fp;
16791 }
252b5132
RH
16792 else if (strcmp (name, "push") == 0)
16793 {
16794 struct mips_option_stack *s;
16795
325801bd 16796 s = XNEW (struct mips_option_stack);
252b5132
RH
16797 s->next = mips_opts_stack;
16798 s->options = mips_opts;
16799 mips_opts_stack = s;
16800 }
16801 else if (strcmp (name, "pop") == 0)
16802 {
16803 struct mips_option_stack *s;
16804
16805 s = mips_opts_stack;
16806 if (s == NULL)
16807 as_bad (_(".set pop with no .set push"));
16808 else
16809 {
16810 /* If we're changing the reorder mode we need to handle
16811 delay slots correctly. */
16812 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16813 start_noreorder ();
252b5132 16814 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16815 end_noreorder ();
252b5132
RH
16816
16817 mips_opts = s->options;
16818 mips_opts_stack = s->next;
16819 free (s);
16820 }
16821 }
22522f88
MR
16822 else
16823 {
16824 type = parse_code_option (name);
16825 if (type == OPTION_TYPE_BAD)
16826 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16827 }
919731af 16828
16829 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16830 registers based on what is supported by the arch/cpu. */
22522f88 16831 if (type == OPTION_TYPE_ISA)
e6559e01 16832 {
919731af 16833 switch (mips_opts.isa)
16834 {
16835 case 0:
16836 break;
16837 case ISA_MIPS1:
351cdf24
MF
16838 /* MIPS I cannot support FPXX. */
16839 mips_opts.fp = 32;
16840 /* fall-through. */
919731af 16841 case ISA_MIPS2:
16842 case ISA_MIPS32:
16843 case ISA_MIPS32R2:
16844 case ISA_MIPS32R3:
16845 case ISA_MIPS32R5:
16846 mips_opts.gp = 32;
351cdf24
MF
16847 if (mips_opts.fp != 0)
16848 mips_opts.fp = 32;
919731af 16849 break;
7361da2c
AB
16850 case ISA_MIPS32R6:
16851 mips_opts.gp = 32;
16852 mips_opts.fp = 64;
16853 break;
919731af 16854 case ISA_MIPS3:
16855 case ISA_MIPS4:
16856 case ISA_MIPS5:
16857 case ISA_MIPS64:
16858 case ISA_MIPS64R2:
16859 case ISA_MIPS64R3:
16860 case ISA_MIPS64R5:
7361da2c 16861 case ISA_MIPS64R6:
919731af 16862 mips_opts.gp = 64;
351cdf24
MF
16863 if (mips_opts.fp != 0)
16864 {
16865 if (mips_opts.arch == CPU_R5900)
16866 mips_opts.fp = 32;
16867 else
16868 mips_opts.fp = 64;
16869 }
919731af 16870 break;
16871 default:
16872 as_bad (_("unknown ISA level %s"), name + 4);
16873 break;
16874 }
e6559e01 16875 }
919731af 16876
5b7c81bd 16877 mips_check_options (&mips_opts, false);
919731af 16878
16879 mips_check_isa_supports_ases ();
16880 *input_line_pointer = ch;
16881 demand_empty_rest_of_line ();
16882}
16883
16884/* Handle the .module pseudo-op. */
16885
16886static void
16887s_module (int ignore ATTRIBUTE_UNUSED)
16888{
16889 char *name = input_line_pointer, ch;
16890
16891 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16892 ++input_line_pointer;
16893 ch = *input_line_pointer;
16894 *input_line_pointer = '\0';
16895
16896 if (!file_mips_opts_checked)
252b5132 16897 {
22522f88 16898 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16899 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16900
16901 /* Update module level settings from mips_opts. */
16902 file_mips_opts = mips_opts;
252b5132 16903 }
919731af 16904 else
16905 as_bad (_(".module is not permitted after generating code"));
16906
252b5132
RH
16907 *input_line_pointer = ch;
16908 demand_empty_rest_of_line ();
16909}
16910
16911/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16912 .option pic2. It means to generate SVR4 PIC calls. */
16913
16914static void
17a2f251 16915s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16916{
16917 mips_pic = SVR4_PIC;
5b7c81bd 16918 mips_abicalls = true;
4d0d148d
TS
16919
16920 if (g_switch_seen && g_switch_value != 0)
16921 as_warn (_("-G may not be used with SVR4 PIC code"));
16922 g_switch_value = 0;
16923
252b5132
RH
16924 bfd_set_gp_size (stdoutput, 0);
16925 demand_empty_rest_of_line ();
16926}
16927
16928/* Handle the .cpload pseudo-op. This is used when generating SVR4
16929 PIC code. It sets the $gp register for the function based on the
16930 function address, which is in the register named in the argument.
16931 This uses a relocation against _gp_disp, which is handled specially
16932 by the linker. The result is:
16933 lui $gp,%hi(_gp_disp)
16934 addiu $gp,$gp,%lo(_gp_disp)
16935 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16936 The .cpload argument is normally $25 == $t9.
16937
16938 The -mno-shared option changes this to:
bbe506e8
TS
16939 lui $gp,%hi(__gnu_local_gp)
16940 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16941 and the argument is ignored. This saves an instruction, but the
16942 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16943 address for __gnu_local_gp. Thus code assembled with -mno-shared
16944 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16945
16946static void
17a2f251 16947s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16948{
16949 expressionS ex;
aa6975fb
ILT
16950 int reg;
16951 int in_shared;
252b5132 16952
919731af 16953 file_mips_check_options ();
16954
6478892d
TS
16955 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16956 .cpload is ignored. */
16957 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16958 {
16959 s_ignore (0);
16960 return;
16961 }
16962
a276b80c
MR
16963 if (mips_opts.mips16)
16964 {
16965 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16966 ignore_rest_of_line ();
16967 return;
16968 }
16969
d3ecfc59 16970 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16971 if (mips_opts.noreorder == 0)
16972 as_warn (_(".cpload not in noreorder section"));
16973
aa6975fb
ILT
16974 reg = tc_get_register (0);
16975
16976 /* If we need to produce a 64-bit address, we are better off using
16977 the default instruction sequence. */
aed1a261 16978 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16979
252b5132 16980 ex.X_op = O_symbol;
bbe506e8
TS
16981 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16982 "__gnu_local_gp");
252b5132
RH
16983 ex.X_op_symbol = NULL;
16984 ex.X_add_number = 0;
16985
16986 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16987 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16988
8a75745d 16989 mips_mark_labels ();
5b7c81bd 16990 mips_assembling_insn = true;
8a75745d 16991
584892a6 16992 macro_start ();
67c0d1eb
RS
16993 macro_build_lui (&ex, mips_gp_register);
16994 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16995 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16996 if (in_shared)
16997 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16998 mips_gp_register, reg);
584892a6 16999 macro_end ();
252b5132 17000
5b7c81bd 17001 mips_assembling_insn = false;
252b5132
RH
17002 demand_empty_rest_of_line ();
17003}
17004
6478892d
TS
17005/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
17006 .cpsetup $reg1, offset|$reg2, label
17007
17008 If offset is given, this results in:
17009 sd $gp, offset($sp)
956cd1d6 17010 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17011 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17012 daddu $gp, $gp, $reg1
6478892d
TS
17013
17014 If $reg2 is given, this results in:
40fc1451 17015 or $reg2, $gp, $0
956cd1d6 17016 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
17017 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
17018 daddu $gp, $gp, $reg1
aa6975fb
ILT
17019 $reg1 is normally $25 == $t9.
17020
17021 The -mno-shared option replaces the last three instructions with
17022 lui $gp,%hi(_gp)
54f4ddb3 17023 addiu $gp,$gp,%lo(_gp) */
aa6975fb 17024
6478892d 17025static void
17a2f251 17026s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17027{
17028 expressionS ex_off;
17029 expressionS ex_sym;
17030 int reg1;
6478892d 17031
919731af 17032 file_mips_check_options ();
17033
8586fc66 17034 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
17035 We also need NewABI support. */
17036 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17037 {
17038 s_ignore (0);
17039 return;
17040 }
17041
a276b80c
MR
17042 if (mips_opts.mips16)
17043 {
17044 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
17045 ignore_rest_of_line ();
17046 return;
17047 }
17048
6478892d
TS
17049 reg1 = tc_get_register (0);
17050 SKIP_WHITESPACE ();
17051 if (*input_line_pointer != ',')
17052 {
17053 as_bad (_("missing argument separator ',' for .cpsetup"));
17054 return;
17055 }
17056 else
80245285 17057 ++input_line_pointer;
6478892d
TS
17058 SKIP_WHITESPACE ();
17059 if (*input_line_pointer == '$')
80245285
TS
17060 {
17061 mips_cpreturn_register = tc_get_register (0);
17062 mips_cpreturn_offset = -1;
17063 }
6478892d 17064 else
80245285
TS
17065 {
17066 mips_cpreturn_offset = get_absolute_expression ();
17067 mips_cpreturn_register = -1;
17068 }
6478892d
TS
17069 SKIP_WHITESPACE ();
17070 if (*input_line_pointer != ',')
17071 {
17072 as_bad (_("missing argument separator ',' for .cpsetup"));
17073 return;
17074 }
17075 else
f9419b05 17076 ++input_line_pointer;
6478892d 17077 SKIP_WHITESPACE ();
f21f8242 17078 expression (&ex_sym);
6478892d 17079
8a75745d 17080 mips_mark_labels ();
5b7c81bd 17081 mips_assembling_insn = true;
8a75745d 17082
584892a6 17083 macro_start ();
6478892d
TS
17084 if (mips_cpreturn_register == -1)
17085 {
17086 ex_off.X_op = O_constant;
17087 ex_off.X_add_symbol = NULL;
17088 ex_off.X_op_symbol = NULL;
17089 ex_off.X_add_number = mips_cpreturn_offset;
17090
67c0d1eb 17091 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 17092 BFD_RELOC_LO16, SP);
6478892d
TS
17093 }
17094 else
40fc1451 17095 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 17096
aed1a261 17097 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 17098 {
df58fc94 17099 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
17100 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
17101 BFD_RELOC_HI16_S);
17102
17103 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
17104 mips_gp_register, -1, BFD_RELOC_GPREL16,
17105 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
17106
17107 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
17108 mips_gp_register, reg1);
17109 }
17110 else
17111 {
17112 expressionS ex;
17113
17114 ex.X_op = O_symbol;
4184909a 17115 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
17116 ex.X_op_symbol = NULL;
17117 ex.X_add_number = 0;
6e1304d8 17118
aa6975fb
ILT
17119 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
17120 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
17121
17122 macro_build_lui (&ex, mips_gp_register);
17123 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17124 mips_gp_register, BFD_RELOC_LO16);
17125 }
f21f8242 17126
584892a6 17127 macro_end ();
6478892d 17128
5b7c81bd 17129 mips_assembling_insn = false;
6478892d
TS
17130 demand_empty_rest_of_line ();
17131}
17132
17133static void
17a2f251 17134s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 17135{
919731af 17136 file_mips_check_options ();
17137
6478892d 17138 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 17139 .cplocal is ignored. */
6478892d
TS
17140 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17141 {
17142 s_ignore (0);
17143 return;
17144 }
17145
a276b80c
MR
17146 if (mips_opts.mips16)
17147 {
17148 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
17149 ignore_rest_of_line ();
17150 return;
17151 }
17152
6478892d 17153 mips_gp_register = tc_get_register (0);
85b51719 17154 demand_empty_rest_of_line ();
6478892d
TS
17155}
17156
252b5132
RH
17157/* Handle the .cprestore pseudo-op. This stores $gp into a given
17158 offset from $sp. The offset is remembered, and after making a PIC
17159 call $gp is restored from that location. */
17160
17161static void
17a2f251 17162s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17163{
17164 expressionS ex;
252b5132 17165
919731af 17166 file_mips_check_options ();
17167
6478892d 17168 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 17169 .cprestore is ignored. */
6478892d 17170 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
17171 {
17172 s_ignore (0);
17173 return;
17174 }
17175
a276b80c
MR
17176 if (mips_opts.mips16)
17177 {
17178 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
17179 ignore_rest_of_line ();
17180 return;
17181 }
17182
252b5132 17183 mips_cprestore_offset = get_absolute_expression ();
7a621144 17184 mips_cprestore_valid = 1;
252b5132
RH
17185
17186 ex.X_op = O_constant;
17187 ex.X_add_symbol = NULL;
17188 ex.X_op_symbol = NULL;
17189 ex.X_add_number = mips_cprestore_offset;
17190
8a75745d 17191 mips_mark_labels ();
5b7c81bd 17192 mips_assembling_insn = true;
8a75745d 17193
584892a6 17194 macro_start ();
67c0d1eb
RS
17195 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
17196 SP, HAVE_64BIT_ADDRESSES);
584892a6 17197 macro_end ();
252b5132 17198
5b7c81bd 17199 mips_assembling_insn = false;
252b5132
RH
17200 demand_empty_rest_of_line ();
17201}
17202
6478892d 17203/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 17204 was given in the preceding .cpsetup, it results in:
6478892d 17205 ld $gp, offset($sp)
76b3015f 17206
6478892d 17207 If a register $reg2 was given there, it results in:
40fc1451 17208 or $gp, $reg2, $0 */
54f4ddb3 17209
6478892d 17210static void
17a2f251 17211s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17212{
17213 expressionS ex;
6478892d 17214
919731af 17215 file_mips_check_options ();
17216
6478892d
TS
17217 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
17218 We also need NewABI support. */
17219 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17220 {
17221 s_ignore (0);
17222 return;
17223 }
17224
a276b80c
MR
17225 if (mips_opts.mips16)
17226 {
17227 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
17228 ignore_rest_of_line ();
17229 return;
17230 }
17231
8a75745d 17232 mips_mark_labels ();
5b7c81bd 17233 mips_assembling_insn = true;
8a75745d 17234
584892a6 17235 macro_start ();
6478892d
TS
17236 if (mips_cpreturn_register == -1)
17237 {
17238 ex.X_op = O_constant;
17239 ex.X_add_symbol = NULL;
17240 ex.X_op_symbol = NULL;
17241 ex.X_add_number = mips_cpreturn_offset;
17242
67c0d1eb 17243 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
17244 }
17245 else
40fc1451
SD
17246 move_register (mips_gp_register, mips_cpreturn_register);
17247
584892a6 17248 macro_end ();
6478892d 17249
5b7c81bd 17250 mips_assembling_insn = false;
6478892d
TS
17251 demand_empty_rest_of_line ();
17252}
17253
d0f13682
CLT
17254/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
17255 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
17256 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
17257 debug information or MIPS16 TLS. */
741d6ea8
JM
17258
17259static void
d0f13682
CLT
17260s_tls_rel_directive (const size_t bytes, const char *dirstr,
17261 bfd_reloc_code_real_type rtype)
741d6ea8
JM
17262{
17263 expressionS ex;
17264 char *p;
17265
17266 expression (&ex);
17267
17268 if (ex.X_op != O_symbol)
17269 {
1661c76c 17270 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
17271 ignore_rest_of_line ();
17272 }
17273
17274 p = frag_more (bytes);
17275 md_number_to_chars (p, 0, bytes);
5b7c81bd 17276 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, false, rtype);
741d6ea8 17277 demand_empty_rest_of_line ();
de64cffd 17278 mips_clear_insn_labels ();
741d6ea8
JM
17279}
17280
17281/* Handle .dtprelword. */
17282
17283static void
17284s_dtprelword (int ignore ATTRIBUTE_UNUSED)
17285{
d0f13682 17286 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
17287}
17288
17289/* Handle .dtpreldword. */
17290
17291static void
17292s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
17293{
d0f13682
CLT
17294 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
17295}
17296
17297/* Handle .tprelword. */
17298
17299static void
17300s_tprelword (int ignore ATTRIBUTE_UNUSED)
17301{
17302 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
17303}
17304
17305/* Handle .tpreldword. */
17306
17307static void
17308s_tpreldword (int ignore ATTRIBUTE_UNUSED)
17309{
17310 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
17311}
17312
6478892d
TS
17313/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17314 code. It sets the offset to use in gp_rel relocations. */
17315
17316static void
17a2f251 17317s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17318{
17319 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17320 We also need NewABI support. */
17321 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17322 {
17323 s_ignore (0);
17324 return;
17325 }
17326
def2e0dd 17327 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17328
17329 demand_empty_rest_of_line ();
17330}
17331
252b5132
RH
17332/* Handle the .gpword pseudo-op. This is used when generating PIC
17333 code. It generates a 32 bit GP relative reloc. */
17334
17335static void
17a2f251 17336s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17337{
a8dbcb85
TS
17338 segment_info_type *si;
17339 struct insn_label_list *l;
252b5132
RH
17340 expressionS ex;
17341 char *p;
17342
17343 /* When not generating PIC code, this is treated as .word. */
17344 if (mips_pic != SVR4_PIC)
17345 {
17346 s_cons (2);
17347 return;
17348 }
17349
a8dbcb85
TS
17350 si = seg_info (now_seg);
17351 l = si->label_list;
7d10b47d 17352 mips_emit_delays ();
252b5132 17353 if (auto_align)
462427c4 17354 mips_align (2, 0, l);
252b5132
RH
17355
17356 expression (&ex);
a1facbec 17357 mips_clear_insn_labels ();
252b5132
RH
17358
17359 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17360 {
1661c76c 17361 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17362 ignore_rest_of_line ();
17363 }
17364
17365 p = frag_more (4);
17a2f251 17366 md_number_to_chars (p, 0, 4);
5b7c81bd 17367 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
cdf6fd85 17368 BFD_RELOC_GPREL32);
252b5132
RH
17369
17370 demand_empty_rest_of_line ();
17371}
17372
10181a0d 17373static void
17a2f251 17374s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17375{
a8dbcb85
TS
17376 segment_info_type *si;
17377 struct insn_label_list *l;
10181a0d
AO
17378 expressionS ex;
17379 char *p;
17380
17381 /* When not generating PIC code, this is treated as .dword. */
17382 if (mips_pic != SVR4_PIC)
17383 {
17384 s_cons (3);
17385 return;
17386 }
17387
a8dbcb85
TS
17388 si = seg_info (now_seg);
17389 l = si->label_list;
7d10b47d 17390 mips_emit_delays ();
10181a0d 17391 if (auto_align)
462427c4 17392 mips_align (3, 0, l);
10181a0d
AO
17393
17394 expression (&ex);
a1facbec 17395 mips_clear_insn_labels ();
10181a0d
AO
17396
17397 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17398 {
1661c76c 17399 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17400 ignore_rest_of_line ();
17401 }
17402
17403 p = frag_more (8);
17a2f251 17404 md_number_to_chars (p, 0, 8);
5b7c81bd 17405 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
6e1304d8 17406 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17407
17408 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8 17409 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
5b7c81bd 17410 false, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17411
17412 demand_empty_rest_of_line ();
17413}
17414
a3f278e2
CM
17415/* Handle the .ehword pseudo-op. This is used when generating unwinding
17416 tables. It generates a R_MIPS_EH reloc. */
17417
17418static void
17419s_ehword (int ignore ATTRIBUTE_UNUSED)
17420{
17421 expressionS ex;
17422 char *p;
17423
17424 mips_emit_delays ();
17425
17426 expression (&ex);
17427 mips_clear_insn_labels ();
17428
17429 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17430 {
1661c76c 17431 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17432 ignore_rest_of_line ();
17433 }
17434
17435 p = frag_more (4);
17436 md_number_to_chars (p, 0, 4);
5b7c81bd 17437 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, false,
2f0c68f2 17438 BFD_RELOC_32_PCREL);
a3f278e2
CM
17439
17440 demand_empty_rest_of_line ();
17441}
17442
252b5132
RH
17443/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17444 tables in SVR4 PIC code. */
17445
17446static void
17a2f251 17447s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17448{
252b5132
RH
17449 int reg;
17450
919731af 17451 file_mips_check_options ();
17452
10181a0d
AO
17453 /* This is ignored when not generating SVR4 PIC code. */
17454 if (mips_pic != SVR4_PIC)
252b5132
RH
17455 {
17456 s_ignore (0);
17457 return;
17458 }
17459
8a75745d 17460 mips_mark_labels ();
5b7c81bd 17461 mips_assembling_insn = true;
8a75745d 17462
252b5132 17463 /* Add $gp to the register named as an argument. */
584892a6 17464 macro_start ();
252b5132 17465 reg = tc_get_register (0);
67c0d1eb 17466 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17467 macro_end ();
252b5132 17468
5b7c81bd 17469 mips_assembling_insn = false;
bdaaa2e1 17470 demand_empty_rest_of_line ();
252b5132
RH
17471}
17472
17473/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17474 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17475 such as generating jalx instructions when needed. We also make
17476 them odd for the duration of the assembly, in order to generate the
17477 right sort of code. We will make them even in the adjust_symtab
17478 routine, while leaving them marked. This is convenient for the
17479 debugger and the disassembler. The linker knows to make them odd
17480 again. */
17481
17482static void
17a2f251 17483s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17484{
7bb01e2d
MR
17485 file_mips_check_options ();
17486 file_ase_mips16 |= mips_opts.mips16;
17487 file_ase_micromips |= mips_opts.micromips;
17488
df58fc94 17489 mips_mark_labels ();
252b5132
RH
17490
17491 demand_empty_rest_of_line ();
17492}
17493
ba92f887
MR
17494/* Handle the .nan pseudo-op. */
17495
17496static void
17497s_nan (int ignore ATTRIBUTE_UNUSED)
17498{
17499 static const char str_legacy[] = "legacy";
17500 static const char str_2008[] = "2008";
17501 size_t i;
17502
17503 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17504
17505 if (i == sizeof (str_2008) - 1
17506 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17507 mips_nan2008 = 1;
ba92f887
MR
17508 else if (i == sizeof (str_legacy) - 1
17509 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17510 {
17511 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17512 mips_nan2008 = 0;
17513 else
17514 as_bad (_("`%s' does not support legacy NaN"),
17515 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17516 }
ba92f887 17517 else
1661c76c 17518 as_bad (_("bad .nan directive"));
ba92f887
MR
17519
17520 input_line_pointer += i;
17521 demand_empty_rest_of_line ();
17522}
17523
754e2bb9
RS
17524/* Handle a .stab[snd] directive. Ideally these directives would be
17525 implemented in a transparent way, so that removing them would not
17526 have any effect on the generated instructions. However, s_stab
17527 internally changes the section, so in practice we need to decide
17528 now whether the preceding label marks compressed code. We do not
17529 support changing the compression mode of a label after a .stab*
17530 directive, such as in:
17531
17532 foo:
134c0c8b 17533 .stabs ...
754e2bb9
RS
17534 .set mips16
17535
17536 so the current mode wins. */
252b5132
RH
17537
17538static void
17a2f251 17539s_mips_stab (int type)
252b5132 17540{
42c0794e 17541 file_mips_check_options ();
754e2bb9 17542 mips_mark_labels ();
252b5132
RH
17543 s_stab (type);
17544}
17545
54f4ddb3 17546/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17547
17548static void
17a2f251 17549s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17550{
17551 char *name;
17552 int c;
17553 symbolS *symbolP;
17554 expressionS exp;
17555
d02603dc 17556 c = get_symbol_name (&name);
252b5132
RH
17557 symbolP = symbol_find_or_make (name);
17558 S_SET_WEAK (symbolP);
17559 *input_line_pointer = c;
17560
d02603dc 17561 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17562
17563 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17564 {
17565 if (S_IS_DEFINED (symbolP))
17566 {
20203fb9 17567 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17568 S_GET_NAME (symbolP));
17569 ignore_rest_of_line ();
17570 return;
17571 }
bdaaa2e1 17572
252b5132
RH
17573 if (*input_line_pointer == ',')
17574 {
17575 ++input_line_pointer;
17576 SKIP_WHITESPACE ();
17577 }
bdaaa2e1 17578
252b5132
RH
17579 expression (&exp);
17580 if (exp.X_op != O_symbol)
17581 {
20203fb9 17582 as_bad (_("bad .weakext directive"));
98d3f06f 17583 ignore_rest_of_line ();
252b5132
RH
17584 return;
17585 }
49309057 17586 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17587 }
17588
17589 demand_empty_rest_of_line ();
17590}
17591
17592/* Parse a register string into a number. Called from the ECOFF code
17593 to parse .frame. The argument is non-zero if this is the frame
17594 register, so that we can record it in mips_frame_reg. */
17595
17596int
17a2f251 17597tc_get_register (int frame)
252b5132 17598{
707bfff6 17599 unsigned int reg;
252b5132
RH
17600
17601 SKIP_WHITESPACE ();
707bfff6
TS
17602 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17603 reg = 0;
252b5132 17604 if (frame)
7a621144
DJ
17605 {
17606 mips_frame_reg = reg != 0 ? reg : SP;
17607 mips_frame_reg_valid = 1;
17608 mips_cprestore_valid = 0;
17609 }
252b5132
RH
17610 return reg;
17611}
17612
17613valueT
17a2f251 17614md_section_align (asection *seg, valueT addr)
252b5132 17615{
fd361982 17616 int align = bfd_section_alignment (seg);
252b5132 17617
f3ded42a
RS
17618 /* We don't need to align ELF sections to the full alignment.
17619 However, Irix 5 may prefer that we align them at least to a 16
17620 byte boundary. We don't bother to align the sections if we
17621 are targeted for an embedded system. */
d34049e8 17622 if (startswith (TARGET_OS, "elf"))
f3ded42a
RS
17623 return addr;
17624 if (align > 4)
17625 align = 4;
252b5132 17626
8d3842cd 17627 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17628}
17629
17630/* Utility routine, called from above as well. If called while the
17631 input file is still being read, it's only an approximation. (For
17632 example, a symbol may later become defined which appeared to be
17633 undefined earlier.) */
17634
17635static int
17a2f251 17636nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17637{
17638 if (sym == 0)
17639 return 0;
17640
4d0d148d 17641 if (g_switch_value > 0)
252b5132
RH
17642 {
17643 const char *symname;
17644 int change;
17645
c9914766 17646 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17647 register. It can be if it is smaller than the -G size or if
17648 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17649 not be referenced off the $gp, although it appears as though
252b5132
RH
17650 they can. */
17651 symname = S_GET_NAME (sym);
17652 if (symname != (const char *) NULL
17653 && (strcmp (symname, "eprol") == 0
17654 || strcmp (symname, "etext") == 0
17655 || strcmp (symname, "_gp") == 0
17656 || strcmp (symname, "edata") == 0
17657 || strcmp (symname, "_fbss") == 0
17658 || strcmp (symname, "_fdata") == 0
17659 || strcmp (symname, "_ftext") == 0
17660 || strcmp (symname, "end") == 0
17661 || strcmp (symname, "_gp_disp") == 0))
17662 change = 1;
17663 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17664 && (0
17665#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17666 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17667 && (symbol_get_obj (sym)->ecoff_extern_size
17668 <= g_switch_value))
252b5132
RH
17669#endif
17670 /* We must defer this decision until after the whole
17671 file has been read, since there might be a .extern
17672 after the first use of this symbol. */
17673 || (before_relaxing
17674#ifndef NO_ECOFF_DEBUGGING
49309057 17675 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17676#endif
17677 && S_GET_VALUE (sym) == 0)
17678 || (S_GET_VALUE (sym) != 0
17679 && S_GET_VALUE (sym) <= g_switch_value)))
17680 change = 0;
17681 else
17682 {
17683 const char *segname;
17684
17685 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17686 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17687 && strcmp (segname, ".lit4") != 0);
17688 change = (strcmp (segname, ".sdata") != 0
fba2b7f9 17689 && strcmp (segname, ".sbss") != 0
d34049e8
ML
17690 && !startswith (segname, ".sdata.")
17691 && !startswith (segname, ".sbss.")
17692 && !startswith (segname, ".gnu.linkonce.sb.")
17693 && !startswith (segname, ".gnu.linkonce.s."));
252b5132
RH
17694 }
17695 return change;
17696 }
17697 else
c9914766 17698 /* We are not optimizing for the $gp register. */
252b5132
RH
17699 return 1;
17700}
17701
5919d012
RS
17702
17703/* Return true if the given symbol should be considered local for SVR4 PIC. */
17704
5b7c81bd 17705static bool
9e009953 17706pic_need_relax (symbolS *sym)
5919d012
RS
17707{
17708 asection *symsec;
5919d012 17709
249d1bad
AM
17710 if (!sym)
17711 return false;
17712
5919d012
RS
17713 /* Handle the case of a symbol equated to another symbol. */
17714 while (symbol_equated_reloc_p (sym))
17715 {
17716 symbolS *n;
17717
5f0fe04b 17718 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17719 n = symbol_get_value_expression (sym)->X_add_symbol;
17720 if (n == sym)
17721 break;
17722 sym = n;
17723 }
17724
df1f3cda 17725 if (symbol_section_p (sym))
5b7c81bd 17726 return true;
df1f3cda 17727
5919d012
RS
17728 symsec = S_GET_SEGMENT (sym);
17729
5919d012 17730 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17731 return (!bfd_is_und_section (symsec)
17732 && !bfd_is_abs_section (symsec)
5f0fe04b 17733 && !bfd_is_com_section (symsec)
5919d012 17734 /* A global or weak symbol is treated as external. */
f3ded42a 17735 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17736}
14f72d45
MR
17737\f
17738/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17739 convert a section-relative value VAL to the equivalent PC-relative
17740 value. */
17741
17742static offsetT
17743mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17744 offsetT val, long stretch)
17745{
17746 fragS *sym_frag;
17747 addressT addr;
17748
17749 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17750
17751 sym_frag = symbol_get_frag (fragp->fr_symbol);
17752
17753 /* If the relax_marker of the symbol fragment differs from the
17754 relax_marker of this fragment, we have not yet adjusted the
17755 symbol fragment fr_address. We want to add in STRETCH in
17756 order to get a better estimate of the address. This
17757 particularly matters because of the shift bits. */
17758 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17759 {
17760 fragS *f;
17761
17762 /* Adjust stretch for any alignment frag. Note that if have
17763 been expanding the earlier code, the symbol may be
17764 defined in what appears to be an earlier frag. FIXME:
17765 This doesn't handle the fr_subtype field, which specifies
17766 a maximum number of bytes to skip when doing an
17767 alignment. */
17768 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17769 {
17770 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17771 {
17772 if (stretch < 0)
17773 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17774 else
17775 stretch &= ~((1 << (int) f->fr_offset) - 1);
17776 if (stretch == 0)
17777 break;
17778 }
17779 }
17780 if (f != NULL)
17781 val += stretch;
17782 }
17783
17784 addr = fragp->fr_address + fragp->fr_fix;
17785
17786 /* The base address rules are complicated. The base address of
17787 a branch is the following instruction. The base address of a
17788 PC relative load or add is the instruction itself, but if it
17789 is in a delay slot (in which case it can not be extended) use
17790 the address of the instruction whose delay slot it is in. */
17791 if (pcrel_op->include_isa_bit)
17792 {
17793 addr += 2;
17794
17795 /* If we are currently assuming that this frag should be
17796 extended, then the current address is two bytes higher. */
17797 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17798 addr += 2;
17799
17800 /* Ignore the low bit in the target, since it will be set
17801 for a text label. */
17802 val &= -2;
17803 }
17804 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17805 addr -= 4;
17806 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17807 addr -= 2;
5919d012 17808
14f72d45
MR
17809 val -= addr & -(1 << pcrel_op->align_log2);
17810
17811 return val;
17812}
5919d012 17813
252b5132
RH
17814/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17815 extended opcode. SEC is the section the frag is in. */
17816
17817static int
17a2f251 17818mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17819{
3ccad066 17820 const struct mips_int_operand *operand;
252b5132 17821 offsetT val;
252b5132 17822 segT symsec;
14f72d45 17823 int type;
252b5132
RH
17824
17825 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17826 return 0;
17827 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17828 return 1;
17829
88a7ef16 17830 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17831 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
5b7c81bd
AM
17832 operand = mips16_immed_operand (type, false);
17833 if (S_FORCE_RELOC (fragp->fr_symbol, true)
88a7ef16
MR
17834 || (operand->root.type == OP_PCREL
17835 ? sec != symsec
17836 : !bfd_is_abs_section (symsec)))
17837 return 1;
252b5132 17838
88a7ef16 17839 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17840
3ccad066 17841 if (operand->root.type == OP_PCREL)
252b5132 17842 {
3ccad066 17843 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17844 offsetT maxtiny;
252b5132 17845
1425c41d 17846 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17847 return 1;
252b5132 17848
88a7ef16 17849 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17850 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17851
17852 /* If any of the shifted bits are set, we must use an extended
17853 opcode. If the address depends on the size of this
17854 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17855 use an extended opcode. */
17856 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17857 {
17858 fragp->fr_subtype =
1425c41d 17859 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17860 return 1;
17861 }
17862
17863 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17864 is precisely the next value above maxtiny, then there is a
17865 chance of an infinite loop as in the following code:
252b5132
RH
17866 la $4,foo
17867 .skip 1020
17868 .align 2
17869 foo:
17870 In this case when the la is extended, foo is 0x3fc bytes
17871 away, so the la can be shrunk, but then foo is 0x400 away, so
17872 the la must be extended. To avoid this loop, we mark the
17873 frag as extended if it was small, and is about to become
3ccad066
RS
17874 extended with the next value above maxtiny. */
17875 maxtiny = mips_int_operand_max (operand);
17876 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17877 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17878 {
17879 fragp->fr_subtype =
1425c41d 17880 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17881 return 1;
17882 }
17883 }
252b5132 17884
3ccad066 17885 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17886}
17887
8507b6e7
MR
17888/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17889 macro expansion. SEC is the section the frag is in. We only
17890 support PC-relative instructions (LA, DLA, LW, LD) here, in
17891 non-PIC code using 32-bit addressing. */
17892
17893static int
17894mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17895{
17896 const struct mips_pcrel_operand *pcrel_op;
17897 const struct mips_int_operand *operand;
17898 offsetT val;
17899 segT symsec;
17900 int type;
17901
17902 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17903
17904 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17905 return 0;
17906 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17907 return 0;
17908
17909 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17910 switch (type)
17911 {
17912 case 'A':
17913 case 'B':
17914 case 'E':
17915 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17916 if (bfd_is_abs_section (symsec))
17917 return 1;
17918 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17919 return 0;
5b7c81bd 17920 if (S_FORCE_RELOC (fragp->fr_symbol, true) || sec != symsec)
8507b6e7
MR
17921 return 1;
17922
5b7c81bd 17923 operand = mips16_immed_operand (type, true);
8507b6e7
MR
17924 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17925 pcrel_op = (const struct mips_pcrel_operand *) operand;
17926 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17927
17928 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17929
17930 default:
17931 return 0;
17932 }
17933}
17934
4a6a3df4
AO
17935/* Compute the length of a branch sequence, and adjust the
17936 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17937 worst-case length is computed, with UPDATE being used to indicate
17938 whether an unconditional (-1), branch-likely (+1) or regular (0)
17939 branch is to be computed. */
17940static int
17a2f251 17941relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17942{
5b7c81bd 17943 bool toofar;
4a6a3df4
AO
17944 int length;
17945
17946 if (fragp
17947 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17948 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17949 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17950 {
17951 addressT addr;
17952 offsetT val;
17953
17954 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17955
17956 addr = fragp->fr_address + fragp->fr_fix + 4;
17957
17958 val -= addr;
17959
17960 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17961 }
4a6a3df4 17962 else
c1f61bd2
MR
17963 /* If the symbol is not defined or it's in a different segment,
17964 we emit the long sequence. */
5b7c81bd 17965 toofar = true;
4a6a3df4
AO
17966
17967 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17968 fragp->fr_subtype
66b3e8da 17969 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17970 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17971 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17972 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17973 RELAX_BRANCH_LINK (fragp->fr_subtype),
17974 toofar);
17975
17976 length = 4;
17977 if (toofar)
17978 {
17979 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17980 length += 8;
17981
ce8ad872 17982 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17983 {
17984 /* Additional space for PIC loading of target address. */
17985 length += 8;
17986 if (mips_opts.isa == ISA_MIPS1)
17987 /* Additional space for $at-stabilizing nop. */
17988 length += 4;
17989 }
17990
17991 /* If branch is conditional. */
17992 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17993 length += 8;
17994 }
b34976b6 17995
4a6a3df4
AO
17996 return length;
17997}
17998
7bd374a4
MR
17999/* Get a FRAG's branch instruction delay slot size, either from the
18000 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
18001 or SHORT_INSN_SIZE otherwise. */
18002
18003static int
5b7c81bd 18004frag_branch_delay_slot_size (fragS *fragp, bool al, int short_insn_size)
7bd374a4
MR
18005{
18006 char *buf = fragp->fr_literal + fragp->fr_fix;
18007
18008 if (al)
18009 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
18010 else
18011 return short_insn_size;
18012}
18013
df58fc94
RS
18014/* Compute the length of a branch sequence, and adjust the
18015 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
18016 worst-case length is computed, with UPDATE being used to indicate
18017 whether an unconditional (-1), or regular (0) branch is to be
18018 computed. */
18019
18020static int
18021relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
18022{
5b7c81bd
AM
18023 bool insn32 = true;
18024 bool nods = true;
18025 bool pic = true;
18026 bool al = true;
7bd374a4 18027 int short_insn_size;
5b7c81bd 18028 bool toofar;
df58fc94
RS
18029 int length;
18030
7bd374a4
MR
18031 if (fragp)
18032 {
18033 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18034 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18035 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
18036 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18037 }
18038 short_insn_size = insn32 ? 4 : 2;
18039
df58fc94
RS
18040 if (fragp
18041 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18042 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18043 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18044 {
18045 addressT addr;
18046 offsetT val;
18047
18048 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18049 /* Ignore the low bit in the target, since it will be set
18050 for a text label. */
18051 if ((val & 1) != 0)
18052 --val;
18053
18054 addr = fragp->fr_address + fragp->fr_fix + 4;
18055
18056 val -= addr;
18057
18058 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
18059 }
df58fc94 18060 else
c1f61bd2
MR
18061 /* If the symbol is not defined or it's in a different segment,
18062 we emit the long sequence. */
5b7c81bd 18063 toofar = true;
df58fc94
RS
18064
18065 if (fragp && update
18066 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18067 fragp->fr_subtype = (toofar
18068 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
18069 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
18070
18071 length = 4;
18072 if (toofar)
18073 {
5b7c81bd
AM
18074 bool compact_known = fragp != NULL;
18075 bool compact = false;
18076 bool uncond;
df58fc94 18077
df58fc94 18078 if (fragp)
8484fb75
MR
18079 {
18080 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18081 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 18082 }
df58fc94
RS
18083 else
18084 uncond = update < 0;
18085
18086 /* If label is out of range, we turn branch <br>:
18087
18088 <br> label # 4 bytes
18089 0:
18090
18091 into:
18092
18093 j label # 4 bytes
8484fb75
MR
18094 nop # 2/4 bytes if
18095 # compact && (!PIC || insn32)
df58fc94
RS
18096 0:
18097 */
ce8ad872 18098 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 18099 length += short_insn_size;
df58fc94
RS
18100
18101 /* If assembling PIC code, we further turn:
18102
18103 j label # 4 bytes
18104
18105 into:
18106
18107 lw/ld at, %got(label)(gp) # 4 bytes
18108 d/addiu at, %lo(label) # 4 bytes
8484fb75 18109 jr/c at # 2/4 bytes
df58fc94 18110 */
ce8ad872 18111 if (pic)
8484fb75 18112 length += 4 + short_insn_size;
df58fc94 18113
7bd374a4
MR
18114 /* Add an extra nop if the jump has no compact form and we need
18115 to fill the delay slot. */
ce8ad872 18116 if ((!pic || al) && nods)
7bd374a4
MR
18117 length += (fragp
18118 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
18119 : short_insn_size);
18120
df58fc94
RS
18121 /* If branch <br> is conditional, we prepend negated branch <brneg>:
18122
18123 <brneg> 0f # 4 bytes
8484fb75 18124 nop # 2/4 bytes if !compact
df58fc94
RS
18125 */
18126 if (!uncond)
8484fb75 18127 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 18128 }
7bd374a4
MR
18129 else if (nods)
18130 {
18131 /* Add an extra nop to fill the delay slot. */
18132 gas_assert (fragp);
18133 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
18134 }
df58fc94
RS
18135
18136 return length;
18137}
18138
18139/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
18140 bit accordingly. */
18141
18142static int
18143relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
18144{
5b7c81bd 18145 bool toofar;
df58fc94 18146
df58fc94
RS
18147 if (fragp
18148 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 18149 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
18150 && sec == S_GET_SEGMENT (fragp->fr_symbol))
18151 {
18152 addressT addr;
18153 offsetT val;
18154 int type;
18155
18156 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
18157 /* Ignore the low bit in the target, since it will be set
18158 for a text label. */
18159 if ((val & 1) != 0)
18160 --val;
18161
18162 /* Assume this is a 2-byte branch. */
18163 addr = fragp->fr_address + fragp->fr_fix + 2;
18164
18165 /* We try to avoid the infinite loop by not adding 2 more bytes for
18166 long branches. */
18167
18168 val -= addr;
18169
18170 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
18171 if (type == 'D')
18172 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
18173 else if (type == 'E')
18174 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
18175 else
18176 abort ();
18177 }
18178 else
18179 /* If the symbol is not defined or it's in a different segment,
18180 we emit a normal 32-bit branch. */
5b7c81bd 18181 toofar = true;
df58fc94
RS
18182
18183 if (fragp && update
18184 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18185 fragp->fr_subtype
18186 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
18187 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
18188
18189 if (toofar)
18190 return 4;
18191
18192 return 2;
18193}
18194
252b5132
RH
18195/* Estimate the size of a frag before relaxing. Unless this is the
18196 mips16, we are not really relaxing here, and the final size is
18197 encoded in the subtype information. For the mips16, we have to
18198 decide whether we are using an extended opcode or not. */
18199
252b5132 18200int
17a2f251 18201md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 18202{
5919d012 18203 int change;
252b5132 18204
4a6a3df4
AO
18205 if (RELAX_BRANCH_P (fragp->fr_subtype))
18206 {
18207
5b7c81bd 18208 fragp->fr_var = relaxed_branch_length (fragp, segtype, false);
b34976b6 18209
4a6a3df4
AO
18210 return fragp->fr_var;
18211 }
18212
252b5132 18213 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
18214 {
18215 /* We don't want to modify the EXTENDED bit here; it might get us
18216 into infinite loops. We change it only in mips_relax_frag(). */
18217 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 18218 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
18219 else
18220 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
18221 }
252b5132 18222
df58fc94
RS
18223 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18224 {
18225 int length = 4;
18226
18227 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
5b7c81bd 18228 length = relaxed_micromips_16bit_branch_length (fragp, segtype, false);
df58fc94 18229 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
5b7c81bd 18230 length = relaxed_micromips_32bit_branch_length (fragp, segtype, false);
df58fc94
RS
18231 fragp->fr_var = length;
18232
18233 return length;
18234 }
18235
ce8ad872 18236 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
18237 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
18238 change = 0;
ce8ad872
MR
18239 else if (RELAX_PIC (fragp->fr_subtype))
18240 change = pic_need_relax (fragp->fr_symbol);
252b5132 18241 else
ce8ad872 18242 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
18243
18244 if (change)
18245 {
4d7206a2 18246 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 18247 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 18248 }
4d7206a2
RS
18249 else
18250 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
18251}
18252
18253/* This is called to see whether a reloc against a defined symbol
de7e6852 18254 should be converted into a reloc against a section. */
252b5132
RH
18255
18256int
17a2f251 18257mips_fix_adjustable (fixS *fixp)
252b5132 18258{
252b5132
RH
18259 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
18260 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18261 return 0;
a161fe53 18262
252b5132
RH
18263 if (fixp->fx_addsy == NULL)
18264 return 1;
a161fe53 18265
2f0c68f2
CM
18266 /* Allow relocs used for EH tables. */
18267 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18268 return 1;
18269
de7e6852
RS
18270 /* If symbol SYM is in a mergeable section, relocations of the form
18271 SYM + 0 can usually be made section-relative. The mergeable data
18272 is then identified by the section offset rather than by the symbol.
18273
18274 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 18275 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
18276 need to recalculate the complete offset in order to correctly identify
18277 the merge data.
18278
33eaf5de 18279 The linker has traditionally not looked for the partnering high part
de7e6852
RS
18280 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
18281 placed anywhere. Rather than break backwards compatibility by changing
18282 this, it seems better not to force the issue, and instead keep the
18283 original symbol. This will work with either linker behavior. */
738e5348 18284 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 18285 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
18286 && HAVE_IN_PLACE_ADDENDS
18287 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
18288 return 0;
18289
97f50151
MR
18290 /* There is no place to store an in-place offset for JALR relocations. */
18291 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
18292 return 0;
18293
18294 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 18295 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
18296 start address of the symbol's containing section.
18297
18298 Also, PC relative relocations for MIPS R6 need to be symbol rather than
18299 section relative to allow linker relaxations to be performed later on. */
97f50151 18300 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 18301 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
18302 return 0;
18303
b314ec0e
RS
18304 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
18305 to a floating-point stub. The same is true for non-R_MIPS16_26
18306 relocations against MIPS16 functions; in this case, the stub becomes
18307 the function's canonical address.
18308
18309 Floating-point stubs are stored in unique .mips16.call.* or
18310 .mips16.fn.* sections. If a stub T for function F is in section S,
18311 the first relocation in section S must be against F; this is how the
18312 linker determines the target function. All relocations that might
18313 resolve to T must also be against F. We therefore have the following
18314 restrictions, which are given in an intentionally-redundant way:
18315
18316 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18317 symbols.
18318
18319 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18320 if that stub might be used.
18321
18322 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18323 symbols.
18324
18325 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18326 that stub might be used.
18327
18328 There is a further restriction:
18329
df58fc94 18330 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18331 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18332 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18333 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18334 against MIPS16 or microMIPS symbols because we need to keep the
18335 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18336 detection and JAL or BAL to JALX instruction conversion in the
18337 linker.
b314ec0e 18338
df58fc94 18339 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18340 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18341 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18342
18343 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18344 relocation against some symbol R, no relocation against R may be
18345 reduced. (Note that this deals with (2) as well as (1) because
18346 relocations against global symbols will never be reduced on ELF
18347 targets.) This approach is a little simpler than trying to detect
18348 stub sections, and gives the "all or nothing" per-symbol consistency
18349 that we have for MIPS16 symbols. */
f3ded42a 18350 if (fixp->fx_subsy == NULL
30c09090 18351 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18352 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18353 && (jmp_reloc_p (fixp->fx_r_type)
18354 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18355 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18356 return 0;
a161fe53 18357
252b5132
RH
18358 return 1;
18359}
18360
18361/* Translate internal representation of relocation info to BFD target
18362 format. */
18363
18364arelent **
17a2f251 18365tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18366{
18367 static arelent *retval[4];
18368 arelent *reloc;
18369 bfd_reloc_code_real_type code;
18370
4b0cff4e 18371 memset (retval, 0, sizeof(retval));
325801bd
TS
18372 reloc = retval[0] = XCNEW (arelent);
18373 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18374 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18375 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18376
bad36eac
DJ
18377 if (fixp->fx_pcrel)
18378 {
df58fc94 18379 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18380 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18381 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18382 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18383 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18384 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18385 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18386 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18387 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18388 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18389 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18390 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18391
18392 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18393 Relocations want only the symbol offset. */
51f6035b
MR
18394 switch (fixp->fx_r_type)
18395 {
18396 case BFD_RELOC_MIPS_18_PCREL_S3:
18397 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18398 break;
18399 default:
18400 reloc->addend = fixp->fx_addnumber + reloc->address;
18401 break;
18402 }
bad36eac 18403 }
17c6c9d9
MR
18404 else if (HAVE_IN_PLACE_ADDENDS
18405 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18406 && (read_compressed_insn (fixp->fx_frag->fr_literal
18407 + fixp->fx_where, 4) >> 26) == 0x3c)
18408 {
18409 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18410 addend accordingly. */
18411 reloc->addend = fixp->fx_addnumber >> 1;
18412 }
bad36eac
DJ
18413 else
18414 reloc->addend = fixp->fx_addnumber;
252b5132 18415
438c16b8
TS
18416 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18417 entry to be used in the relocation's section offset. */
18418 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18419 {
18420 reloc->address = reloc->addend;
18421 reloc->addend = 0;
18422 }
18423
252b5132 18424 code = fixp->fx_r_type;
252b5132 18425
bad36eac 18426 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18427 if (reloc->howto == NULL)
18428 {
18429 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18430 _("cannot represent %s relocation in this object file"
18431 " format"),
252b5132
RH
18432 bfd_get_reloc_code_name (code));
18433 retval[0] = NULL;
18434 }
18435
18436 return retval;
18437}
18438
18439/* Relax a machine dependent frag. This returns the amount by which
18440 the current size of the frag should change. */
18441
18442int
17a2f251 18443mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18444{
4a6a3df4
AO
18445 if (RELAX_BRANCH_P (fragp->fr_subtype))
18446 {
18447 offsetT old_var = fragp->fr_var;
b34976b6 18448
5b7c81bd 18449 fragp->fr_var = relaxed_branch_length (fragp, sec, true);
4a6a3df4
AO
18450
18451 return fragp->fr_var - old_var;
18452 }
18453
df58fc94
RS
18454 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18455 {
18456 offsetT old_var = fragp->fr_var;
18457 offsetT new_var = 4;
18458
18459 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
5b7c81bd 18460 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, true);
df58fc94 18461 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
5b7c81bd 18462 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, true);
df58fc94
RS
18463 fragp->fr_var = new_var;
18464
18465 return new_var - old_var;
18466 }
18467
252b5132
RH
18468 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18469 return 0;
18470
8507b6e7 18471 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18472 {
8507b6e7
MR
18473 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18474 {
18475 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18476 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18477 }
18478 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18479 {
18480 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18481 return -2;
18482 }
18483 else
18484 return 0;
18485 }
18486 else if (!mips16_macro_frag (fragp, sec, stretch))
18487 {
18488 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18489 {
18490 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18491 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18492 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18493 }
18494 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18495 {
18496 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18497 return 2;
18498 }
18499 else
252b5132 18500 return 0;
252b5132
RH
18501 }
18502 else
18503 {
8507b6e7 18504 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18505 return 0;
8507b6e7
MR
18506 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18507 {
18508 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18509 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18510 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18511 }
18512 else
18513 {
18514 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18515 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18516 }
252b5132
RH
18517 }
18518
18519 return 0;
18520}
18521
18522/* Convert a machine dependent frag. */
18523
18524void
17a2f251 18525md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18526{
4a6a3df4
AO
18527 if (RELAX_BRANCH_P (fragp->fr_subtype))
18528 {
4d68580a 18529 char *buf;
4a6a3df4 18530 unsigned long insn;
4a6a3df4 18531 fixS *fixp;
b34976b6 18532
4d68580a
RS
18533 buf = fragp->fr_literal + fragp->fr_fix;
18534 insn = read_insn (buf);
b34976b6 18535
4a6a3df4
AO
18536 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18537 {
18538 /* We generate a fixup instead of applying it right now
18539 because, if there are linker relaxations, we're going to
18540 need the relocations. */
bbd27b76
MR
18541 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18542 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18543 true, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18544 fixp->fx_file = fragp->fr_file;
18545 fixp->fx_line = fragp->fr_line;
b34976b6 18546
4d68580a 18547 buf = write_insn (buf, insn);
4a6a3df4
AO
18548 }
18549 else
18550 {
18551 int i;
18552
18553 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18554 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18555
18556 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18557 goto uncond;
18558
18559 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18560 {
18561 /* Reverse the branch. */
18562 switch ((insn >> 28) & 0xf)
18563 {
18564 case 4:
56d438b1
CF
18565 if ((insn & 0xff000000) == 0x47000000
18566 || (insn & 0xff600000) == 0x45600000)
18567 {
18568 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18569 reversed by tweaking bit 23. */
18570 insn ^= 0x00800000;
18571 }
18572 else
18573 {
18574 /* bc[0-3][tf]l? instructions can have the condition
18575 reversed by tweaking a single TF bit, and their
18576 opcodes all have 0x4???????. */
18577 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18578 insn ^= 0x00010000;
18579 }
4a6a3df4
AO
18580 break;
18581
18582 case 0:
18583 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18584 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18585 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18586 insn ^= 0x00010000;
18587 break;
b34976b6 18588
4a6a3df4
AO
18589 case 1:
18590 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18591 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18592 insn ^= 0x04000000;
18593 break;
18594
18595 default:
18596 abort ();
18597 }
18598 }
18599
18600 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18601 {
18602 /* Clear the and-link bit. */
9c2799c2 18603 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18604
54f4ddb3
TS
18605 /* bltzal 0x04100000 bgezal 0x04110000
18606 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18607 insn &= ~0x00100000;
18608 }
18609
18610 /* Branch over the branch (if the branch was likely) or the
18611 full jump (not likely case). Compute the offset from the
18612 current instruction to branch to. */
18613 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18614 i = 16;
18615 else
18616 {
18617 /* How many bytes in instructions we've already emitted? */
4d68580a 18618 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18619 /* How many bytes in instructions from here to the end? */
18620 i = fragp->fr_var - i;
18621 }
18622 /* Convert to instruction count. */
18623 i >>= 2;
18624 /* Branch counts from the next instruction. */
b34976b6 18625 i--;
4a6a3df4
AO
18626 insn |= i;
18627 /* Branch over the jump. */
4d68580a 18628 buf = write_insn (buf, insn);
4a6a3df4 18629
54f4ddb3 18630 /* nop */
4d68580a 18631 buf = write_insn (buf, 0);
4a6a3df4
AO
18632
18633 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18634 {
18635 /* beql $0, $0, 2f */
18636 insn = 0x50000000;
18637 /* Compute the PC offset from the current instruction to
18638 the end of the variable frag. */
18639 /* How many bytes in instructions we've already emitted? */
4d68580a 18640 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18641 /* How many bytes in instructions from here to the end? */
18642 i = fragp->fr_var - i;
18643 /* Convert to instruction count. */
18644 i >>= 2;
18645 /* Don't decrement i, because we want to branch over the
18646 delay slot. */
4a6a3df4 18647 insn |= i;
4a6a3df4 18648
4d68580a
RS
18649 buf = write_insn (buf, insn);
18650 buf = write_insn (buf, 0);
4a6a3df4
AO
18651 }
18652
18653 uncond:
ce8ad872 18654 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18655 {
18656 /* j or jal. */
18657 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18658 ? 0x0c000000 : 0x08000000);
4a6a3df4 18659
bbd27b76
MR
18660 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18661 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18662 false, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18663 fixp->fx_file = fragp->fr_file;
18664 fixp->fx_line = fragp->fr_line;
18665
4d68580a 18666 buf = write_insn (buf, insn);
4a6a3df4
AO
18667 }
18668 else
18669 {
66b3e8da
MR
18670 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18671
4a6a3df4 18672 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18673 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18674 insn |= at << OP_SH_RT;
4a6a3df4 18675
bbd27b76
MR
18676 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18677 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18678 false, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18679 fixp->fx_file = fragp->fr_file;
18680 fixp->fx_line = fragp->fr_line;
18681
4d68580a 18682 buf = write_insn (buf, insn);
b34976b6 18683
4a6a3df4 18684 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18685 /* nop */
18686 buf = write_insn (buf, 0);
4a6a3df4
AO
18687
18688 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18689 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18690 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18691
bbd27b76
MR
18692 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18693 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18694 false, BFD_RELOC_LO16);
4a6a3df4
AO
18695 fixp->fx_file = fragp->fr_file;
18696 fixp->fx_line = fragp->fr_line;
b34976b6 18697
4d68580a 18698 buf = write_insn (buf, insn);
4a6a3df4
AO
18699
18700 /* j(al)r $at. */
18701 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18702 insn = 0x0000f809;
4a6a3df4 18703 else
66b3e8da
MR
18704 insn = 0x00000008;
18705 insn |= at << OP_SH_RS;
4a6a3df4 18706
4d68580a 18707 buf = write_insn (buf, insn);
4a6a3df4
AO
18708 }
18709 }
18710
4a6a3df4 18711 fragp->fr_fix += fragp->fr_var;
4d68580a 18712 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18713 return;
18714 }
18715
df58fc94
RS
18716 /* Relax microMIPS branches. */
18717 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18718 {
4d68580a 18719 char *buf = fragp->fr_literal + fragp->fr_fix;
5b7c81bd
AM
18720 bool compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
18721 bool insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
18722 bool nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
18723 bool pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
18724 bool al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
df58fc94 18725 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
5b7c81bd 18726 bool short_ds;
df58fc94 18727 unsigned long insn;
df58fc94
RS
18728 fixS *fixp;
18729
df58fc94
RS
18730 fragp->fr_fix += fragp->fr_var;
18731
18732 /* Handle 16-bit branches that fit or are forced to fit. */
18733 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18734 {
18735 /* We generate a fixup instead of applying it right now,
18736 because if there is linker relaxation, we're going to
18737 need the relocations. */
834a65aa
MR
18738 switch (type)
18739 {
18740 case 'D':
18741 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18742 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18743 true, BFD_RELOC_MICROMIPS_10_PCREL_S1);
834a65aa
MR
18744 break;
18745 case 'E':
18746 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18747 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18748 true, BFD_RELOC_MICROMIPS_7_PCREL_S1);
834a65aa
MR
18749 break;
18750 default:
18751 abort ();
18752 }
df58fc94
RS
18753
18754 fixp->fx_file = fragp->fr_file;
18755 fixp->fx_line = fragp->fr_line;
18756
18757 /* These relocations can have an addend that won't fit in
18758 2 octets. */
18759 fixp->fx_no_overflow = 1;
18760
18761 return;
18762 }
18763
2309ddf2 18764 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18765 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18766 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18767 {
18768 /* We generate a fixup instead of applying it right now,
18769 because if there is linker relaxation, we're going to
18770 need the relocations. */
bbd27b76
MR
18771 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18772 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18773 true, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18774 fixp->fx_file = fragp->fr_file;
18775 fixp->fx_line = fragp->fr_line;
18776
18777 if (type == 0)
7bd374a4
MR
18778 {
18779 insn = read_compressed_insn (buf, 4);
18780 buf += 4;
18781
18782 if (nods)
18783 {
18784 /* Check the short-delay-slot bit. */
18785 if (!al || (insn & 0x02000000) != 0)
18786 buf = write_compressed_insn (buf, 0x0c00, 2);
18787 else
18788 buf = write_compressed_insn (buf, 0x00000000, 4);
18789 }
18790
18791 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18792 return;
18793 }
df58fc94
RS
18794 }
18795
18796 /* Relax 16-bit branches to 32-bit branches. */
18797 if (type != 0)
18798 {
4d68580a 18799 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18800
18801 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18802 insn = 0x94000000; /* beq */
18803 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18804 {
18805 unsigned long regno;
18806
18807 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18808 regno = micromips_to_32_reg_d_map [regno];
18809 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18810 insn |= regno << MICROMIPSOP_SH_RS;
18811 }
18812 else
18813 abort ();
18814
18815 /* Nothing else to do, just write it out. */
18816 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18817 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18818 {
4d68580a 18819 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18820 if (nods)
18821 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18822 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18823 return;
18824 }
18825 }
18826 else
4d68580a 18827 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18828
18829 /* Relax 32-bit branches to a sequence of instructions. */
18830 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18831 _("relaxed out-of-range branch into a jump"));
df58fc94 18832
2309ddf2 18833 /* Set the short-delay-slot bit. */
7bd374a4 18834 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18835
18836 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18837 {
18838 symbolS *l;
18839
18840 /* Reverse the branch. */
18841 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18842 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18843 insn ^= 0x20000000;
18844 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18845 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18846 || (insn & 0xffe00000) == 0x40800000 /* blez */
18847 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18848 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18849 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18850 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18851 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18852 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18853 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18854 insn ^= 0x00400000;
18855 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18856 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18857 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18858 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18859 insn ^= 0x00200000;
56d438b1
CF
18860 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18861 BNZ.df */
18862 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18863 BNZ.V */
18864 insn ^= 0x00800000;
df58fc94
RS
18865 else
18866 abort ();
18867
18868 if (al)
18869 {
18870 /* Clear the and-link and short-delay-slot bits. */
18871 gas_assert ((insn & 0xfda00000) == 0x40200000);
18872
18873 /* bltzal 0x40200000 bgezal 0x40600000 */
18874 /* bltzals 0x42200000 bgezals 0x42600000 */
18875 insn &= ~0x02200000;
18876 }
18877
18878 /* Make a label at the end for use with the branch. */
e01e1cee 18879 l = symbol_new (micromips_label_name (), asec, fragp, fragp->fr_fix);
df58fc94 18880 micromips_label_inc ();
f3ded42a 18881 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18882
18883 /* Refer to it. */
5b7c81bd 18884 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, true,
4d68580a 18885 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18886 fixp->fx_file = fragp->fr_file;
18887 fixp->fx_line = fragp->fr_line;
18888
18889 /* Branch over the jump. */
4d68580a 18890 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18891
df58fc94 18892 if (!compact)
8484fb75
MR
18893 {
18894 /* nop */
18895 if (insn32)
18896 buf = write_compressed_insn (buf, 0x00000000, 4);
18897 else
18898 buf = write_compressed_insn (buf, 0x0c00, 2);
18899 }
df58fc94
RS
18900 }
18901
ce8ad872 18902 if (!pic)
df58fc94 18903 {
7bd374a4
MR
18904 unsigned long jal = (short_ds || nods
18905 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18906
df58fc94
RS
18907 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18908 insn = al ? jal : 0xd4000000;
18909
bbd27b76
MR
18910 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18911 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18912 false, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18913 fixp->fx_file = fragp->fr_file;
18914 fixp->fx_line = fragp->fr_line;
18915
4d68580a 18916 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18917
7bd374a4 18918 if (compact || nods)
8484fb75
MR
18919 {
18920 /* nop */
18921 if (insn32)
18922 buf = write_compressed_insn (buf, 0x00000000, 4);
18923 else
18924 buf = write_compressed_insn (buf, 0x0c00, 2);
18925 }
df58fc94
RS
18926 }
18927 else
18928 {
18929 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18930
18931 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18932 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18933 insn |= at << MICROMIPSOP_SH_RT;
18934
bbd27b76
MR
18935 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18936 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18937 false, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18938 fixp->fx_file = fragp->fr_file;
18939 fixp->fx_line = fragp->fr_line;
18940
4d68580a 18941 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18942
18943 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18944 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18945 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18946
bbd27b76
MR
18947 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18948 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 18949 false, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18950 fixp->fx_file = fragp->fr_file;
18951 fixp->fx_line = fragp->fr_line;
18952
4d68580a 18953 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18954
8484fb75
MR
18955 if (insn32)
18956 {
18957 /* jr/jalr $at */
18958 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18959 insn |= at << MICROMIPSOP_SH_RS;
18960
18961 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18962
7bd374a4 18963 if (compact || nods)
8484fb75
MR
18964 /* nop */
18965 buf = write_compressed_insn (buf, 0x00000000, 4);
18966 }
18967 else
18968 {
18969 /* jr/jrc/jalr/jalrs $at */
18970 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18971 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18972
18973 insn = al ? jalr : jr;
18974 insn |= at << MICROMIPSOP_SH_MJ;
18975
18976 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18977 if (al && nods)
18978 {
18979 /* nop */
18980 if (short_ds)
18981 buf = write_compressed_insn (buf, 0x0c00, 2);
18982 else
18983 buf = write_compressed_insn (buf, 0x00000000, 4);
18984 }
8484fb75 18985 }
df58fc94
RS
18986 }
18987
4d68580a 18988 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18989 return;
18990 }
18991
252b5132
RH
18992 if (RELAX_MIPS16_P (fragp->fr_subtype))
18993 {
18994 int type;
3ccad066 18995 const struct mips_int_operand *operand;
252b5132 18996 offsetT val;
5c04167a 18997 char *buf;
8507b6e7 18998 unsigned int user_length;
5b7c81bd 18999 bool need_reloc;
252b5132 19000 unsigned long insn;
5b7c81bd
AM
19001 bool mac;
19002 bool ext;
88a7ef16 19003 segT symsec;
252b5132
RH
19004
19005 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
5b7c81bd 19006 operand = mips16_immed_operand (type, false);
252b5132 19007
8507b6e7 19008 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 19009 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 19010 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
19011
19012 symsec = S_GET_SEGMENT (fragp->fr_symbol);
5b7c81bd 19013 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, true)
8507b6e7 19014 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
19015 ? asec != symsec
19016 : !bfd_is_abs_section (symsec)));
19017
8507b6e7 19018 if (operand->root.type == OP_PCREL && !mac)
252b5132 19019 {
3ccad066 19020 const struct mips_pcrel_operand *pcrel_op;
252b5132 19021
3ccad066 19022 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 19023
14f72d45 19024 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 19025 {
37b2d327
MR
19026 if (!mips_ignore_branch_isa
19027 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
19028 as_bad_where (fragp->fr_file, fragp->fr_line,
19029 _("branch to a symbol in another ISA mode"));
19030 else if ((fragp->fr_offset & 0x1) != 0)
19031 as_bad_where (fragp->fr_file, fragp->fr_line,
19032 _("branch to misaligned address (0x%lx)"),
52031738
FS
19033 (long) (resolve_symbol_value (fragp->fr_symbol)
19034 + (fragp->fr_offset & ~1)));
252b5132 19035 }
252b5132 19036
14f72d45 19037 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
19038
19039 /* Make sure the section winds up with the alignment we have
19040 assumed. */
3ccad066
RS
19041 if (operand->shift > 0)
19042 record_alignment (asec, operand->shift);
252b5132
RH
19043 }
19044
8507b6e7
MR
19045 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
19046 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
19047 {
19048 if (mac)
19049 as_warn_where (fragp->fr_file, fragp->fr_line,
19050 _("macro instruction expanded into multiple "
19051 "instructions in a branch delay slot"));
19052 else if (ext)
19053 as_warn_where (fragp->fr_file, fragp->fr_line,
19054 _("extended instruction in a branch delay slot"));
19055 }
19056 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 19057 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
19058 _("macro instruction expanded into multiple "
19059 "instructions"));
252b5132 19060
5c04167a 19061 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 19062
4d68580a 19063 insn = read_compressed_insn (buf, 2);
5c04167a
RS
19064 if (ext)
19065 insn |= MIPS16_EXTEND;
252b5132 19066
5c04167a
RS
19067 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
19068 user_length = 4;
19069 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
19070 user_length = 2;
19071 else
19072 user_length = 0;
19073
8507b6e7 19074 if (mac)
c9775dde 19075 {
8507b6e7
MR
19076 unsigned long reg;
19077 unsigned long new;
19078 unsigned long op;
5b7c81bd 19079 bool e2;
8507b6e7
MR
19080
19081 gas_assert (type == 'A' || type == 'B' || type == 'E');
19082 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 19083
25499ac7
MR
19084 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
19085
8507b6e7 19086 if (need_reloc)
c9775dde 19087 {
8507b6e7
MR
19088 fixS *fixp;
19089
19090 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
19091
19092 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19093 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19094 false, BFD_RELOC_MIPS16_HI16_S);
8507b6e7
MR
19095 fixp->fx_file = fragp->fr_file;
19096 fixp->fx_line = fragp->fr_line;
19097
25499ac7 19098 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7 19099 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19100 false, BFD_RELOC_MIPS16_LO16);
8507b6e7
MR
19101 fixp->fx_file = fragp->fr_file;
19102 fixp->fx_line = fragp->fr_line;
19103
19104 val = 0;
19105 }
19106
19107 switch (insn & 0xf800)
19108 {
19109 case 0x0800: /* ADDIU */
19110 reg = (insn >> 8) & 0x7;
19111 op = 0xf0004800 | (reg << 8);
c9775dde 19112 break;
8507b6e7
MR
19113 case 0xb000: /* LW */
19114 reg = (insn >> 8) & 0x7;
19115 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 19116 break;
8507b6e7
MR
19117 case 0xf800: /* I64 */
19118 reg = (insn >> 5) & 0x7;
19119 switch (insn & 0x0700)
19120 {
19121 case 0x0400: /* LD */
19122 op = 0xf0003800 | (reg << 8) | (reg << 5);
19123 break;
19124 case 0x0600: /* DADDIU */
19125 op = 0xf000fd00 | (reg << 5);
19126 break;
19127 default:
19128 abort ();
19129 }
19130 break;
19131 default:
19132 abort ();
c9775dde 19133 }
8507b6e7 19134
25499ac7 19135 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
19136 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
19137 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
19138 if (!e2)
19139 {
19140 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
19141 buf = write_compressed_insn (buf, new, 4);
19142 }
8507b6e7
MR
19143 op |= mips16_immed_extend (val, 16);
19144 buf = write_compressed_insn (buf, op, 4);
19145
25499ac7 19146 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
19147 }
19148 else
19149 {
19150 unsigned int length = ext ? 4 : 2;
19151
19152 if (need_reloc)
c9775dde 19153 {
8507b6e7 19154 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 19155 fixS *fixp;
c9775dde 19156
8507b6e7
MR
19157 switch (type)
19158 {
19159 case 'p':
19160 case 'q':
19161 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
19162 break;
19163 default:
19164 break;
19165 }
19166 if (mac || reloc == BFD_RELOC_NONE)
19167 as_bad_where (fragp->fr_file, fragp->fr_line,
19168 _("unsupported relocation"));
19169 else if (ext)
19170 {
bbd27b76
MR
19171 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
19172 fragp->fr_symbol, fragp->fr_offset,
5b7c81bd 19173 true, reloc);
8507b6e7
MR
19174 fixp->fx_file = fragp->fr_file;
19175 fixp->fx_line = fragp->fr_line;
19176 }
19177 else
19178 as_bad_where (fragp->fr_file, fragp->fr_line,
19179 _("invalid unextended operand value"));
c9775dde 19180 }
eefc3365 19181 else
8507b6e7
MR
19182 mips16_immed (fragp->fr_file, fragp->fr_line, type,
19183 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 19184
8507b6e7
MR
19185 gas_assert (mips16_opcode_length (insn) == length);
19186 write_compressed_insn (buf, insn, length);
19187 fragp->fr_fix += length;
19188 }
252b5132
RH
19189 }
19190 else
19191 {
df58fc94 19192 relax_substateT subtype = fragp->fr_subtype;
5b7c81bd
AM
19193 bool second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
19194 bool use_second = (subtype & RELAX_USE_SECOND) != 0;
871a6bd2 19195 unsigned int first, second;
4d7206a2 19196 fixS *fixp;
252b5132 19197
df58fc94
RS
19198 first = RELAX_FIRST (subtype);
19199 second = RELAX_SECOND (subtype);
4d7206a2 19200 fixp = (fixS *) fragp->fr_opcode;
252b5132 19201
df58fc94
RS
19202 /* If the delay slot chosen does not match the size of the instruction,
19203 then emit a warning. */
19204 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
19205 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
19206 {
19207 relax_substateT s;
19208 const char *msg;
19209
19210 s = subtype & (RELAX_DELAY_SLOT_16BIT
19211 | RELAX_DELAY_SLOT_SIZE_FIRST
19212 | RELAX_DELAY_SLOT_SIZE_SECOND);
19213 msg = macro_warning (s);
19214 if (msg != NULL)
db9b2be4 19215 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
19216 subtype &= ~s;
19217 }
19218
584892a6 19219 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 19220 if (use_second == second_longer)
584892a6 19221 {
df58fc94
RS
19222 relax_substateT s;
19223 const char *msg;
19224
19225 s = (subtype
19226 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
19227 msg = macro_warning (s);
19228 if (msg != NULL)
db9b2be4 19229 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 19230 subtype &= ~s;
584892a6
RS
19231 }
19232
4d7206a2
RS
19233 /* Go through all the fixups for the first sequence. Disable them
19234 (by marking them as done) if we're going to use the second
19235 sequence instead. */
19236 while (fixp
19237 && fixp->fx_frag == fragp
90bd3c90 19238 && fixp->fx_where + second < fragp->fr_fix)
4d7206a2 19239 {
df58fc94 19240 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19241 fixp->fx_done = 1;
19242 fixp = fixp->fx_next;
19243 }
252b5132 19244
4d7206a2
RS
19245 /* Go through the fixups for the second sequence. Disable them if
19246 we're going to use the first sequence, otherwise adjust their
19247 addresses to account for the relaxation. */
19248 while (fixp && fixp->fx_frag == fragp)
19249 {
df58fc94 19250 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19251 fixp->fx_where -= first;
19252 else
19253 fixp->fx_done = 1;
19254 fixp = fixp->fx_next;
19255 }
19256
19257 /* Now modify the frag contents. */
df58fc94 19258 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
19259 {
19260 char *start;
19261
19262 start = fragp->fr_literal + fragp->fr_fix - first - second;
19263 memmove (start, start + first, second);
19264 fragp->fr_fix -= first;
19265 }
19266 else
19267 fragp->fr_fix -= second;
252b5132
RH
19268 }
19269}
19270
252b5132
RH
19271/* This function is called after the relocs have been generated.
19272 We've been storing mips16 text labels as odd. Here we convert them
19273 back to even for the convenience of the debugger. */
19274
19275void
17a2f251 19276mips_frob_file_after_relocs (void)
252b5132
RH
19277{
19278 asymbol **syms;
19279 unsigned int count, i;
19280
252b5132
RH
19281 syms = bfd_get_outsymbols (stdoutput);
19282 count = bfd_get_symcount (stdoutput);
19283 for (i = 0; i < count; i++, syms++)
df58fc94
RS
19284 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
19285 && ((*syms)->value & 1) != 0)
19286 {
19287 (*syms)->value &= ~1;
19288 /* If the symbol has an odd size, it was probably computed
19289 incorrectly, so adjust that as well. */
19290 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
19291 ++elf_symbol (*syms)->internal_elf_sym.st_size;
19292 }
252b5132
RH
19293}
19294
a1facbec
MR
19295/* This function is called whenever a label is defined, including fake
19296 labels instantiated off the dot special symbol. It is used when
19297 handling branch delays; if a branch has a label, we assume we cannot
19298 move it. This also bumps the value of the symbol by 1 in compressed
19299 code. */
252b5132 19300
e1b47bd5 19301static void
a1facbec 19302mips_record_label (symbolS *sym)
252b5132 19303{
a8dbcb85 19304 segment_info_type *si = seg_info (now_seg);
252b5132
RH
19305 struct insn_label_list *l;
19306
19307 if (free_insn_labels == NULL)
325801bd 19308 l = XNEW (struct insn_label_list);
252b5132
RH
19309 else
19310 {
19311 l = free_insn_labels;
19312 free_insn_labels = l->next;
19313 }
19314
19315 l->label = sym;
a8dbcb85
TS
19316 l->next = si->label_list;
19317 si->label_list = l;
a1facbec 19318}
07a53e5c 19319
a1facbec
MR
19320/* This function is called as tc_frob_label() whenever a label is defined
19321 and adds a DWARF-2 record we only want for true labels. */
19322
19323void
19324mips_define_label (symbolS *sym)
19325{
19326 mips_record_label (sym);
07a53e5c 19327 dwarf2_emit_label (sym);
252b5132 19328}
e1b47bd5
RS
19329
19330/* This function is called by tc_new_dot_label whenever a new dot symbol
19331 is defined. */
19332
19333void
19334mips_add_dot_label (symbolS *sym)
19335{
19336 mips_record_label (sym);
19337 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19338 mips_compressed_mark_label (sym);
19339}
252b5132 19340\f
351cdf24
MF
19341/* Converting ASE flags from internal to .MIPS.abiflags values. */
19342static unsigned int
19343mips_convert_ase_flags (int ase)
19344{
19345 unsigned int ext_ases = 0;
19346
19347 if (ase & ASE_DSP)
19348 ext_ases |= AFL_ASE_DSP;
19349 if (ase & ASE_DSPR2)
19350 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19351 if (ase & ASE_DSPR3)
19352 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19353 if (ase & ASE_EVA)
19354 ext_ases |= AFL_ASE_EVA;
19355 if (ase & ASE_MCU)
19356 ext_ases |= AFL_ASE_MCU;
19357 if (ase & ASE_MDMX)
19358 ext_ases |= AFL_ASE_MDMX;
19359 if (ase & ASE_MIPS3D)
19360 ext_ases |= AFL_ASE_MIPS3D;
19361 if (ase & ASE_MT)
19362 ext_ases |= AFL_ASE_MT;
19363 if (ase & ASE_SMARTMIPS)
19364 ext_ases |= AFL_ASE_SMARTMIPS;
19365 if (ase & ASE_VIRT)
19366 ext_ases |= AFL_ASE_VIRT;
19367 if (ase & ASE_MSA)
19368 ext_ases |= AFL_ASE_MSA;
19369 if (ase & ASE_XPA)
19370 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19371 if (ase & ASE_MIPS16E2)
19372 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19373 if (ase & ASE_CRC)
19374 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19375 if (ase & ASE_GINV)
19376 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19377 if (ase & ASE_LOONGSON_MMI)
19378 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19379 if (ase & ASE_LOONGSON_CAM)
19380 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19381 if (ase & ASE_LOONGSON_EXT)
19382 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19383 if (ase & ASE_LOONGSON_EXT2)
19384 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19385
19386 return ext_ases;
19387}
252b5132
RH
19388/* Some special processing for a MIPS ELF file. */
19389
19390void
17a2f251 19391mips_elf_final_processing (void)
252b5132 19392{
351cdf24
MF
19393 int fpabi;
19394 Elf_Internal_ABIFlags_v0 flags;
19395
19396 flags.version = 0;
19397 flags.isa_rev = 0;
19398 switch (file_mips_opts.isa)
19399 {
19400 case INSN_ISA1:
19401 flags.isa_level = 1;
19402 break;
19403 case INSN_ISA2:
19404 flags.isa_level = 2;
19405 break;
19406 case INSN_ISA3:
19407 flags.isa_level = 3;
19408 break;
19409 case INSN_ISA4:
19410 flags.isa_level = 4;
19411 break;
19412 case INSN_ISA5:
19413 flags.isa_level = 5;
19414 break;
19415 case INSN_ISA32:
19416 flags.isa_level = 32;
19417 flags.isa_rev = 1;
19418 break;
19419 case INSN_ISA32R2:
19420 flags.isa_level = 32;
19421 flags.isa_rev = 2;
19422 break;
19423 case INSN_ISA32R3:
19424 flags.isa_level = 32;
19425 flags.isa_rev = 3;
19426 break;
19427 case INSN_ISA32R5:
19428 flags.isa_level = 32;
19429 flags.isa_rev = 5;
19430 break;
09c14161
MF
19431 case INSN_ISA32R6:
19432 flags.isa_level = 32;
19433 flags.isa_rev = 6;
19434 break;
351cdf24
MF
19435 case INSN_ISA64:
19436 flags.isa_level = 64;
19437 flags.isa_rev = 1;
19438 break;
19439 case INSN_ISA64R2:
19440 flags.isa_level = 64;
19441 flags.isa_rev = 2;
19442 break;
19443 case INSN_ISA64R3:
19444 flags.isa_level = 64;
19445 flags.isa_rev = 3;
19446 break;
19447 case INSN_ISA64R5:
19448 flags.isa_level = 64;
19449 flags.isa_rev = 5;
19450 break;
09c14161
MF
19451 case INSN_ISA64R6:
19452 flags.isa_level = 64;
19453 flags.isa_rev = 6;
19454 break;
351cdf24
MF
19455 }
19456
19457 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19458 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19459 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19460 : (file_mips_opts.fp == 64) ? AFL_REG_64
19461 : AFL_REG_32;
19462 flags.cpr2_size = AFL_REG_NONE;
19463 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19464 Tag_GNU_MIPS_ABI_FP);
19465 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19466 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19467 if (file_ase_mips16)
19468 flags.ases |= AFL_ASE_MIPS16;
19469 if (file_ase_micromips)
19470 flags.ases |= AFL_ASE_MICROMIPS;
19471 flags.flags1 = 0;
19472 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19473 || file_mips_opts.fp == 64)
19474 && file_mips_opts.oddspreg)
19475 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19476 flags.flags2 = 0;
19477
19478 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19479 ((Elf_External_ABIFlags_v0 *)
19480 mips_flags_frag));
19481
252b5132 19482 /* Write out the register information. */
316f5878 19483 if (mips_abi != N64_ABI)
252b5132
RH
19484 {
19485 Elf32_RegInfo s;
19486
19487 s.ri_gprmask = mips_gprmask;
19488 s.ri_cprmask[0] = mips_cprmask[0];
19489 s.ri_cprmask[1] = mips_cprmask[1];
19490 s.ri_cprmask[2] = mips_cprmask[2];
19491 s.ri_cprmask[3] = mips_cprmask[3];
19492 /* The gp_value field is set by the MIPS ELF backend. */
19493
19494 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19495 ((Elf32_External_RegInfo *)
19496 mips_regmask_frag));
19497 }
19498 else
19499 {
19500 Elf64_Internal_RegInfo s;
19501
19502 s.ri_gprmask = mips_gprmask;
19503 s.ri_pad = 0;
19504 s.ri_cprmask[0] = mips_cprmask[0];
19505 s.ri_cprmask[1] = mips_cprmask[1];
19506 s.ri_cprmask[2] = mips_cprmask[2];
19507 s.ri_cprmask[3] = mips_cprmask[3];
19508 /* The gp_value field is set by the MIPS ELF backend. */
19509
19510 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19511 ((Elf64_External_RegInfo *)
19512 mips_regmask_frag));
19513 }
19514
19515 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19516 sort of BFD interface for this. */
19517 if (mips_any_noreorder)
19518 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19519 if (mips_pic != NO_PIC)
143d77c5 19520 {
8b828383 19521 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19522 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19523 }
19524 if (mips_abicalls)
19525 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19526
b015e599
AP
19527 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19528 defined at present; this might need to change in future. */
a4672219
TS
19529 if (file_ase_mips16)
19530 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19531 if (file_ase_micromips)
19532 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19533 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19534 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19535
bdaaa2e1 19536 /* Set the MIPS ELF ABI flags. */
316f5878 19537 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19538 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19539 else if (mips_abi == O64_ABI)
252b5132 19540 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19541 else if (mips_abi == EABI_ABI)
252b5132 19542 {
bad1aba3 19543 if (file_mips_opts.gp == 64)
252b5132
RH
19544 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19545 else
19546 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19547 }
be00bddd 19548
defc8e2b 19549 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19550
19551 if (mips_32bitmode)
19552 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19553
7361da2c 19554 if (mips_nan2008 == 1)
ba92f887
MR
19555 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19556
ad3fea08 19557 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19558 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19559 Tag_GNU_MIPS_ABI_FP);
19560 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19561 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19562}
252b5132 19563\f
beae10d5 19564typedef struct proc {
9b2f1d35
EC
19565 symbolS *func_sym;
19566 symbolS *func_end_sym;
beae10d5
KH
19567 unsigned long reg_mask;
19568 unsigned long reg_offset;
19569 unsigned long fpreg_mask;
19570 unsigned long fpreg_offset;
19571 unsigned long frame_offset;
19572 unsigned long frame_reg;
19573 unsigned long pc_reg;
19574} procS;
252b5132
RH
19575
19576static procS cur_proc;
19577static procS *cur_proc_ptr;
19578static int numprocs;
19579
df58fc94
RS
19580/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19581 as "2", and a normal nop as "0". */
19582
19583#define NOP_OPCODE_MIPS 0
19584#define NOP_OPCODE_MIPS16 1
19585#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19586
19587char
19588mips_nop_opcode (void)
19589{
df58fc94
RS
19590 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19591 return NOP_OPCODE_MICROMIPS;
19592 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19593 return NOP_OPCODE_MIPS16;
19594 else
19595 return NOP_OPCODE_MIPS;
742a56fe
RS
19596}
19597
df58fc94
RS
19598/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19599 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19600
0a9ef439 19601void
17a2f251 19602mips_handle_align (fragS *fragp)
a19d8eb0 19603{
df58fc94 19604 char nop_opcode;
742a56fe 19605 char *p;
c67a084a
NC
19606 int bytes, size, excess;
19607 valueT opcode;
742a56fe 19608
0a9ef439
RH
19609 if (fragp->fr_type != rs_align_code)
19610 return;
19611
742a56fe 19612 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19613 nop_opcode = *p;
19614 switch (nop_opcode)
a19d8eb0 19615 {
df58fc94
RS
19616 case NOP_OPCODE_MICROMIPS:
19617 opcode = micromips_nop32_insn.insn_opcode;
19618 size = 4;
19619 break;
19620 case NOP_OPCODE_MIPS16:
c67a084a
NC
19621 opcode = mips16_nop_insn.insn_opcode;
19622 size = 2;
df58fc94
RS
19623 break;
19624 case NOP_OPCODE_MIPS:
19625 default:
c67a084a
NC
19626 opcode = nop_insn.insn_opcode;
19627 size = 4;
df58fc94 19628 break;
c67a084a 19629 }
a19d8eb0 19630
c67a084a
NC
19631 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19632 excess = bytes % size;
df58fc94
RS
19633
19634 /* Handle the leading part if we're not inserting a whole number of
19635 instructions, and make it the end of the fixed part of the frag.
19636 Try to fit in a short microMIPS NOP if applicable and possible,
19637 and use zeroes otherwise. */
19638 gas_assert (excess < 4);
19639 fragp->fr_fix += excess;
19640 switch (excess)
c67a084a 19641 {
df58fc94
RS
19642 case 3:
19643 *p++ = '\0';
19644 /* Fall through. */
19645 case 2:
833794fc 19646 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19647 {
4d68580a 19648 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19649 break;
19650 }
19651 *p++ = '\0';
19652 /* Fall through. */
19653 case 1:
19654 *p++ = '\0';
19655 /* Fall through. */
19656 case 0:
19657 break;
a19d8eb0 19658 }
c67a084a
NC
19659
19660 md_number_to_chars (p, opcode, size);
19661 fragp->fr_var = size;
a19d8eb0
CP
19662}
19663
252b5132 19664static long
17a2f251 19665get_number (void)
252b5132
RH
19666{
19667 int negative = 0;
19668 long val = 0;
19669
19670 if (*input_line_pointer == '-')
19671 {
19672 ++input_line_pointer;
19673 negative = 1;
19674 }
3882b010 19675 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19676 as_bad (_("expected simple number"));
252b5132
RH
19677 if (input_line_pointer[0] == '0')
19678 {
19679 if (input_line_pointer[1] == 'x')
19680 {
19681 input_line_pointer += 2;
3882b010 19682 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19683 {
19684 val <<= 4;
19685 val |= hex_value (*input_line_pointer++);
19686 }
19687 return negative ? -val : val;
19688 }
19689 else
19690 {
19691 ++input_line_pointer;
3882b010 19692 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19693 {
19694 val <<= 3;
19695 val |= *input_line_pointer++ - '0';
19696 }
19697 return negative ? -val : val;
19698 }
19699 }
3882b010 19700 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19701 {
19702 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19703 *input_line_pointer, *input_line_pointer);
956cd1d6 19704 as_warn (_("invalid number"));
252b5132
RH
19705 return -1;
19706 }
3882b010 19707 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19708 {
19709 val *= 10;
19710 val += *input_line_pointer++ - '0';
19711 }
19712 return negative ? -val : val;
19713}
19714
19715/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19716 is an initial number which is the ECOFF file index. In the non-ECOFF
19717 case .file implies DWARF-2. */
19718
19719static void
17a2f251 19720s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19721{
ecb4347a
DJ
19722 static int first_file_directive = 0;
19723
c5dd6aab
DJ
19724 if (ECOFF_DEBUGGING)
19725 {
19726 get_number ();
c39e89c3 19727 s_file (0);
c5dd6aab
DJ
19728 }
19729 else
ecb4347a
DJ
19730 {
19731 char *filename;
19732
68d20676 19733 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19734
19735 /* Versions of GCC up to 3.1 start files with a ".file"
19736 directive even for stabs output. Make sure that this
19737 ".file" is handled. Note that you need a version of GCC
19738 after 3.1 in order to support DWARF-2 on MIPS. */
19739 if (filename != NULL && ! first_file_directive)
19740 {
66b39b8b 19741 new_logical_line (filename, -1);
c39e89c3 19742 s_file_string (filename);
ecb4347a
DJ
19743 }
19744 first_file_directive = 1;
19745 }
c5dd6aab
DJ
19746}
19747
19748/* The .loc directive, implying DWARF-2. */
252b5132
RH
19749
19750static void
17a2f251 19751s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19752{
c5dd6aab
DJ
19753 if (!ECOFF_DEBUGGING)
19754 dwarf2_directive_loc (0);
252b5132
RH
19755}
19756
252b5132
RH
19757/* The .end directive. */
19758
19759static void
17a2f251 19760s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19761{
19762 symbolS *p;
252b5132 19763
7a621144
DJ
19764 /* Following functions need their own .frame and .cprestore directives. */
19765 mips_frame_reg_valid = 0;
19766 mips_cprestore_valid = 0;
19767
252b5132
RH
19768 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19769 {
19770 p = get_symbol ();
19771 demand_empty_rest_of_line ();
19772 }
19773 else
19774 p = NULL;
19775
fd361982 19776 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
252b5132
RH
19777 as_warn (_(".end not in text section"));
19778
19779 if (!cur_proc_ptr)
19780 {
1661c76c 19781 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19782 demand_empty_rest_of_line ();
19783 return;
19784 }
19785
19786 if (p != NULL)
19787 {
9c2799c2 19788 gas_assert (S_GET_NAME (p));
9b2f1d35 19789 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19790 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19791
19792 if (debug_type == DEBUG_STABS)
19793 stabs_generate_asm_endfunc (S_GET_NAME (p),
19794 S_GET_NAME (p));
252b5132
RH
19795 }
19796 else
19797 as_warn (_(".end directive missing or unknown symbol"));
19798
9b2f1d35
EC
19799 /* Create an expression to calculate the size of the function. */
19800 if (p && cur_proc_ptr)
19801 {
19802 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19803 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19804
19805 obj->size = exp;
19806 exp->X_op = O_subtract;
19807 exp->X_add_symbol = symbol_temp_new_now ();
19808 exp->X_op_symbol = p;
19809 exp->X_add_number = 0;
19810
19811 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19812 }
19813
5ff6a06c
MR
19814#ifdef md_flush_pending_output
19815 md_flush_pending_output ();
19816#endif
19817
ecb4347a 19818 /* Generate a .pdr section. */
f3ded42a 19819 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19820 {
19821 segT saved_seg = now_seg;
19822 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19823 expressionS exp;
19824 char *fragp;
252b5132 19825
9c2799c2 19826 gas_assert (pdr_seg);
ecb4347a 19827 subseg_set (pdr_seg, 0);
252b5132 19828
ecb4347a
DJ
19829 /* Write the symbol. */
19830 exp.X_op = O_symbol;
19831 exp.X_add_symbol = p;
19832 exp.X_add_number = 0;
19833 emit_expr (&exp, 4);
252b5132 19834
ecb4347a 19835 fragp = frag_more (7 * 4);
252b5132 19836
17a2f251
TS
19837 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19838 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19839 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19840 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19841 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19842 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19843 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19844
ecb4347a
DJ
19845 subseg_set (saved_seg, saved_subseg);
19846 }
252b5132
RH
19847
19848 cur_proc_ptr = NULL;
19849}
19850
19851/* The .aent and .ent directives. */
19852
19853static void
17a2f251 19854s_mips_ent (int aent)
252b5132 19855{
252b5132 19856 symbolS *symbolP;
252b5132
RH
19857
19858 symbolP = get_symbol ();
19859 if (*input_line_pointer == ',')
f9419b05 19860 ++input_line_pointer;
252b5132 19861 SKIP_WHITESPACE ();
3882b010 19862 if (ISDIGIT (*input_line_pointer)
d9a62219 19863 || *input_line_pointer == '-')
874e8986 19864 get_number ();
252b5132 19865
fd361982 19866 if ((bfd_section_flags (now_seg) & SEC_CODE) == 0)
1661c76c 19867 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19868
19869 if (!aent && cur_proc_ptr)
9a41af64 19870 as_warn (_("missing .end"));
252b5132
RH
19871
19872 if (!aent)
19873 {
7a621144
DJ
19874 /* This function needs its own .frame and .cprestore directives. */
19875 mips_frame_reg_valid = 0;
19876 mips_cprestore_valid = 0;
19877
252b5132
RH
19878 cur_proc_ptr = &cur_proc;
19879 memset (cur_proc_ptr, '\0', sizeof (procS));
19880
9b2f1d35 19881 cur_proc_ptr->func_sym = symbolP;
252b5132 19882
f9419b05 19883 ++numprocs;
ecb4347a
DJ
19884
19885 if (debug_type == DEBUG_STABS)
19886 stabs_generate_asm_func (S_GET_NAME (symbolP),
19887 S_GET_NAME (symbolP));
252b5132
RH
19888 }
19889
7c0fc524
MR
19890 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19891
252b5132
RH
19892 demand_empty_rest_of_line ();
19893}
19894
19895/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19896 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19897 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19898 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19899 symbol table (in the mdebug section). */
19900
19901static void
17a2f251 19902s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19903{
f3ded42a
RS
19904 if (ECOFF_DEBUGGING)
19905 s_ignore (ignore);
19906 else
ecb4347a
DJ
19907 {
19908 long val;
252b5132 19909
ecb4347a
DJ
19910 if (cur_proc_ptr == (procS *) NULL)
19911 {
19912 as_warn (_(".frame outside of .ent"));
19913 demand_empty_rest_of_line ();
19914 return;
19915 }
252b5132 19916
ecb4347a
DJ
19917 cur_proc_ptr->frame_reg = tc_get_register (1);
19918
19919 SKIP_WHITESPACE ();
19920 if (*input_line_pointer++ != ','
19921 || get_absolute_expression_and_terminator (&val) != ',')
19922 {
1661c76c 19923 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19924 --input_line_pointer;
19925 demand_empty_rest_of_line ();
19926 return;
19927 }
252b5132 19928
ecb4347a
DJ
19929 cur_proc_ptr->frame_offset = val;
19930 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19931
252b5132 19932 demand_empty_rest_of_line ();
252b5132 19933 }
252b5132
RH
19934}
19935
bdaaa2e1
KH
19936/* The .fmask and .mask directives. If the mdebug section is present
19937 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19938 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19939 information correctly. We can't use the ecoff routines because they
252b5132
RH
19940 make reference to the ecoff symbol table (in the mdebug section). */
19941
19942static void
17a2f251 19943s_mips_mask (int reg_type)
252b5132 19944{
f3ded42a
RS
19945 if (ECOFF_DEBUGGING)
19946 s_ignore (reg_type);
19947 else
252b5132 19948 {
ecb4347a 19949 long mask, off;
252b5132 19950
ecb4347a
DJ
19951 if (cur_proc_ptr == (procS *) NULL)
19952 {
19953 as_warn (_(".mask/.fmask outside of .ent"));
19954 demand_empty_rest_of_line ();
19955 return;
19956 }
252b5132 19957
ecb4347a
DJ
19958 if (get_absolute_expression_and_terminator (&mask) != ',')
19959 {
1661c76c 19960 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19961 --input_line_pointer;
19962 demand_empty_rest_of_line ();
19963 return;
19964 }
252b5132 19965
ecb4347a
DJ
19966 off = get_absolute_expression ();
19967
19968 if (reg_type == 'F')
19969 {
19970 cur_proc_ptr->fpreg_mask = mask;
19971 cur_proc_ptr->fpreg_offset = off;
19972 }
19973 else
19974 {
19975 cur_proc_ptr->reg_mask = mask;
19976 cur_proc_ptr->reg_offset = off;
19977 }
19978
19979 demand_empty_rest_of_line ();
252b5132 19980 }
252b5132
RH
19981}
19982
316f5878
RS
19983/* A table describing all the processors gas knows about. Names are
19984 matched in the order listed.
e7af610e 19985
316f5878
RS
19986 To ease comparison, please keep this table in the same order as
19987 gcc's mips_cpu_info_table[]. */
e972090a
NC
19988static const struct mips_cpu_info mips_cpu_info_table[] =
19989{
6f2117ba 19990 /* Entries for generic ISAs. */
dbec9420
MR
19991 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19992 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19993 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19994 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19995 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19996 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19997 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19998 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19999 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
20000 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
20001 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
20002 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
20003 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
20004 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
20005 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
20006
20007 /* MIPS I */
dbec9420
MR
20008 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
20009 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
20010 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
20011
20012 /* MIPS II */
dbec9420 20013 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
df18f71b 20014 { "allegrex", 0, 0, ISA_MIPS2, CPU_ALLEGREX },
316f5878
RS
20015
20016 /* MIPS III */
dbec9420
MR
20017 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
20018 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
20019 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
20020 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
20021 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
20022 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
20023 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
20024 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
20025 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
20026 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
20027 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
20028 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
20029 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
6f2117ba 20030 /* ST Microelectronics Loongson 2E and 2F cores. */
dbec9420
MR
20031 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
20032 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
20033
20034 /* MIPS IV */
dbec9420
MR
20035 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
20036 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
20037 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
20038 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
20039 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
20040 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
20041 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
20042 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
20043 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
20044 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
20045 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
20046 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
20047 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
20048 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
20049 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
20050
20051 /* MIPS 32 */
dbec9420
MR
20052 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20053 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20054 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
20055 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
20056
20057 /* MIPS 32 Release 2 */
dbec9420
MR
20058 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20059 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20060 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20061 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
20062 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20063 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20064 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
20065 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
d16afab6 20066 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
dbec9420 20067 ISA_MIPS32R2, CPU_MIPS32R2 },
d16afab6 20068 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
dbec9420
MR
20069 ISA_MIPS32R2, CPU_MIPS32R2 },
20070 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20071 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20072 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20073 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20074 /* Deprecated forms of the above. */
dbec9420
MR
20075 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
20076 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20077 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
dbec9420
MR
20078 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20079 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20080 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20081 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20082 /* Deprecated forms of the above. */
dbec9420
MR
20083 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
20084 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20085 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
dbec9420
MR
20086 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20087 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20088 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20089 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20090 /* Deprecated forms of the above. */
dbec9420
MR
20091 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20092 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 20093 /* 34Kn is a 34kc without DSP. */
dbec9420 20094 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 20095 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
dbec9420
MR
20096 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20097 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20098 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20099 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20100 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 20101 /* Deprecated forms of the above. */
dbec9420
MR
20102 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
20103 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 20104 /* 1004K cores are multiprocessor versions of the 34K. */
dbec9420
MR
20105 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20106 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20107 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
20108 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
6f2117ba 20109 /* interaptiv is the new name for 1004kf. */
dbec9420 20110 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
20111 { "interaptiv-mr2", 0,
20112 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
dbec9420 20113 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
6f2117ba 20114 /* M5100 family. */
dbec9420
MR
20115 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
20116 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 20117 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
dbec9420 20118 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 20119
316f5878 20120 /* MIPS 64 */
dbec9420
MR
20121 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20122 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
20123 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
20124 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 20125
6f2117ba 20126 /* Broadcom SB-1 CPU core. */
dbec9420 20127 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
6f2117ba 20128 /* Broadcom SB-1A CPU core. */
dbec9420 20129 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 20130
6f2117ba
PH
20131 /* MIPS 64 Release 2. */
20132 /* Loongson CPU core. */
20133 /* -march=loongson3a is an alias of -march=gs464 for compatibility. */
bdc6c06e 20134 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
dbec9420 20135 ISA_MIPS64R2, CPU_GS464 },
ac8cb70f 20136 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
dbec9420
MR
20137 ISA_MIPS64R2, CPU_GS464 },
20138 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20139 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
20140 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
20141 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 20142
6f2117ba 20143 /* Cavium Networks Octeon CPU core. */
dbec9420
MR
20144 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
20145 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
20146 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
20147 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 20148
52b6b6b9 20149 /* RMI Xlr */
dbec9420 20150 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 20151
55a36193
MK
20152 /* Broadcom XLP.
20153 XLP is mostly like XLR, with the prominent exception that it is
20154 MIPS64R2 rather than MIPS64. */
dbec9420 20155 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 20156
6f2117ba 20157 /* MIPS 64 Release 6. */
dbec9420 20158 { "i6400", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
bdc8beb4 20159 { "i6500", 0, ASE_VIRT | ASE_MSA | ASE_CRC | ASE_GINV,
dbec9420
MR
20160 ISA_MIPS64R6, CPU_MIPS64R6},
20161 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 20162
6f2117ba 20163 /* End marker. */
dbec9420 20164 { NULL, 0, 0, 0, 0 }
316f5878 20165};
e7af610e 20166
84ea6cf2 20167
316f5878
RS
20168/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
20169 with a final "000" replaced by "k". Ignore case.
e7af610e 20170
316f5878 20171 Note: this function is shared between GCC and GAS. */
c6c98b38 20172
5b7c81bd 20173static bool
17a2f251 20174mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20175{
20176 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
20177 given++, canonical++;
20178
20179 return ((*given == 0 && *canonical == 0)
20180 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
20181}
20182
20183
20184/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
20185 CPU name. We've traditionally allowed a lot of variation here.
20186
20187 Note: this function is shared between GCC and GAS. */
20188
5b7c81bd 20189static bool
17a2f251 20190mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
20191{
20192 /* First see if the name matches exactly, or with a final "000"
20193 turned into "k". */
20194 if (mips_strict_matching_cpu_name_p (canonical, given))
5b7c81bd 20195 return true;
316f5878
RS
20196
20197 /* If not, try comparing based on numerical designation alone.
20198 See if GIVEN is an unadorned number, or 'r' followed by a number. */
20199 if (TOLOWER (*given) == 'r')
20200 given++;
20201 if (!ISDIGIT (*given))
5b7c81bd 20202 return false;
316f5878
RS
20203
20204 /* Skip over some well-known prefixes in the canonical name,
20205 hoping to find a number there too. */
20206 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
20207 canonical += 2;
20208 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
20209 canonical += 2;
20210 else if (TOLOWER (canonical[0]) == 'r')
20211 canonical += 1;
20212
20213 return mips_strict_matching_cpu_name_p (canonical, given);
20214}
20215
20216
20217/* Parse an option that takes the name of a processor as its argument.
20218 OPTION is the name of the option and CPU_STRING is the argument.
20219 Return the corresponding processor enumeration if the CPU_STRING is
20220 recognized, otherwise report an error and return null.
20221
20222 A similar function exists in GCC. */
e7af610e
NC
20223
20224static const struct mips_cpu_info *
17a2f251 20225mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 20226{
316f5878 20227 const struct mips_cpu_info *p;
e7af610e 20228
316f5878
RS
20229 /* 'from-abi' selects the most compatible architecture for the given
20230 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
20231 EABIs, we have to decide whether we're using the 32-bit or 64-bit
20232 version. Look first at the -mgp options, if given, otherwise base
20233 the choice on MIPS_DEFAULT_64BIT.
e7af610e 20234
316f5878
RS
20235 Treat NO_ABI like the EABIs. One reason to do this is that the
20236 plain 'mips' and 'mips64' configs have 'from-abi' as their default
20237 architecture. This code picks MIPS I for 'mips' and MIPS III for
20238 'mips64', just as we did in the days before 'from-abi'. */
20239 if (strcasecmp (cpu_string, "from-abi") == 0)
20240 {
20241 if (ABI_NEEDS_32BIT_REGS (mips_abi))
20242 return mips_cpu_info_from_isa (ISA_MIPS1);
20243
20244 if (ABI_NEEDS_64BIT_REGS (mips_abi))
20245 return mips_cpu_info_from_isa (ISA_MIPS3);
20246
bad1aba3 20247 if (file_mips_opts.gp >= 0)
20248 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 20249 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
20250
20251 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
20252 ? ISA_MIPS3
20253 : ISA_MIPS1);
20254 }
20255
20256 /* 'default' has traditionally been a no-op. Probably not very useful. */
20257 if (strcasecmp (cpu_string, "default") == 0)
20258 return 0;
20259
20260 for (p = mips_cpu_info_table; p->name != 0; p++)
20261 if (mips_matching_cpu_name_p (p->name, cpu_string))
20262 return p;
20263
1661c76c 20264 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 20265 return 0;
e7af610e
NC
20266}
20267
316f5878
RS
20268/* Return the canonical processor information for ISA (a member of the
20269 ISA_MIPS* enumeration). */
20270
e7af610e 20271static const struct mips_cpu_info *
17a2f251 20272mips_cpu_info_from_isa (int isa)
e7af610e
NC
20273{
20274 int i;
20275
20276 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 20277 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 20278 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
20279 return (&mips_cpu_info_table[i]);
20280
e972090a 20281 return NULL;
e7af610e 20282}
fef14a42
TS
20283
20284static const struct mips_cpu_info *
17a2f251 20285mips_cpu_info_from_arch (int arch)
fef14a42
TS
20286{
20287 int i;
20288
20289 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
20290 if (arch == mips_cpu_info_table[i].cpu)
20291 return (&mips_cpu_info_table[i]);
20292
20293 return NULL;
20294}
316f5878
RS
20295\f
20296static void
17a2f251 20297show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
20298{
20299 if (*first_p)
20300 {
20301 fprintf (stream, "%24s", "");
20302 *col_p = 24;
20303 }
20304 else
20305 {
20306 fprintf (stream, ", ");
20307 *col_p += 2;
20308 }
e7af610e 20309
316f5878
RS
20310 if (*col_p + strlen (string) > 72)
20311 {
20312 fprintf (stream, "\n%24s", "");
20313 *col_p = 24;
20314 }
20315
20316 fprintf (stream, "%s", string);
20317 *col_p += strlen (string);
20318
20319 *first_p = 0;
20320}
20321
20322void
17a2f251 20323md_show_usage (FILE *stream)
e7af610e 20324{
316f5878
RS
20325 int column, first;
20326 size_t i;
20327
20328 fprintf (stream, _("\
20329MIPS options:\n\
316f5878
RS
20330-EB generate big endian output\n\
20331-EL generate little endian output\n\
20332-g, -g2 do not remove unneeded NOPs or swap branches\n\
20333-G NUM allow referencing objects up to NUM bytes\n\
20334 implicitly with the gp register [default 8]\n"));
20335 fprintf (stream, _("\
20336-mips1 generate MIPS ISA I instructions\n\
20337-mips2 generate MIPS ISA II instructions\n\
20338-mips3 generate MIPS ISA III instructions\n\
20339-mips4 generate MIPS ISA IV instructions\n\
20340-mips5 generate MIPS ISA V instructions\n\
20341-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20342-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20343-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20344-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20345-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20346-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20347-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20348-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20349-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20350-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20351-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20352
20353 first = 1;
e7af610e
NC
20354
20355 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20356 show (stream, mips_cpu_info_table[i].name, &column, &first);
20357 show (stream, "from-abi", &column, &first);
20358 fputc ('\n', stream);
e7af610e 20359
316f5878
RS
20360 fprintf (stream, _("\
20361-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20362-no-mCPU don't generate code specific to CPU.\n\
20363 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20364
20365 first = 1;
20366
20367 show (stream, "3900", &column, &first);
20368 show (stream, "4010", &column, &first);
20369 show (stream, "4100", &column, &first);
20370 show (stream, "4650", &column, &first);
20371 fputc ('\n', stream);
20372
20373 fprintf (stream, _("\
20374-mips16 generate mips16 instructions\n\
20375-no-mips16 do not generate mips16 instructions\n"));
20376 fprintf (stream, _("\
f866b262
MR
20377-mmips16e2 generate MIPS16e2 instructions\n\
20378-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20379 fprintf (stream, _("\
df58fc94
RS
20380-mmicromips generate microMIPS instructions\n\
20381-mno-micromips do not generate microMIPS instructions\n"));
20382 fprintf (stream, _("\
e16bfa71 20383-msmartmips generate smartmips instructions\n\
3739860c 20384-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20385 fprintf (stream, _("\
74cd071d
CF
20386-mdsp generate DSP instructions\n\
20387-mno-dsp do not generate DSP instructions\n"));
20388 fprintf (stream, _("\
8b082fb1
TS
20389-mdspr2 generate DSP R2 instructions\n\
20390-mno-dspr2 do not generate DSP R2 instructions\n"));
20391 fprintf (stream, _("\
8f4f9071
MF
20392-mdspr3 generate DSP R3 instructions\n\
20393-mno-dspr3 do not generate DSP R3 instructions\n"));
20394 fprintf (stream, _("\
ef2e4d86
CF
20395-mmt generate MT instructions\n\
20396-mno-mt do not generate MT instructions\n"));
20397 fprintf (stream, _("\
dec0624d
MR
20398-mmcu generate MCU instructions\n\
20399-mno-mcu do not generate MCU instructions\n"));
20400 fprintf (stream, _("\
56d438b1
CF
20401-mmsa generate MSA instructions\n\
20402-mno-msa do not generate MSA instructions\n"));
20403 fprintf (stream, _("\
7d64c587
AB
20404-mxpa generate eXtended Physical Address (XPA) instructions\n\
20405-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20406 fprintf (stream, _("\
b015e599
AP
20407-mvirt generate Virtualization instructions\n\
20408-mno-virt do not generate Virtualization instructions\n"));
20409 fprintf (stream, _("\
730c3174
SE
20410-mcrc generate CRC instructions\n\
20411-mno-crc do not generate CRC instructions\n"));
20412 fprintf (stream, _("\
6f20c942
FS
20413-mginv generate Global INValidate (GINV) instructions\n\
20414-mno-ginv do not generate Global INValidate instructions\n"));
20415 fprintf (stream, _("\
8095d2f7
CX
20416-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20417-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20418 fprintf (stream, _("\
716c08de
CX
20419-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20420-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20421 fprintf (stream, _("\
bdc6c06e
CX
20422-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20423-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20424 fprintf (stream, _("\
a693765e
CX
20425-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20426-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20427 fprintf (stream, _("\
833794fc
MR
20428-minsn32 only generate 32-bit microMIPS instructions\n\
20429-mno-insn32 generate all microMIPS instructions\n"));
6f2117ba
PH
20430#if DEFAULT_MIPS_FIX_LOONGSON3_LLSC
20431 fprintf (stream, _("\
20432-mfix-loongson3-llsc work around Loongson3 LL/SC errata, default\n\
20433-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n"));
20434#else
20435 fprintf (stream, _("\
20436-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20437-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata, default\n"));
20438#endif
833794fc 20439 fprintf (stream, _("\
c67a084a
NC
20440-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20441-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
6f2117ba
PH
20442-mfix-loongson3-llsc work around Loongson3 LL/SC errata\n\
20443-mno-fix-loongson3-llsc disable work around Loongson3 LL/SC errata\n\
d766e8ec 20444-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20445-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20446-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20447-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
27c634e0 20448-mfix-r5900 work around R5900 short loop errata\n\
316f5878
RS
20449-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20450-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20451-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20452-O0 do not remove unneeded NOPs, do not swap branches\n\
20453-O, -O1 remove unneeded NOPs, do not swap branches\n\
20454-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20455--trap, --no-break trap exception on div by 0 and mult overflow\n\
20456--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20457 fprintf (stream, _("\
20458-mhard-float allow floating-point instructions\n\
20459-msoft-float do not allow floating-point instructions\n\
20460-msingle-float only allow 32-bit floating-point operations\n\
20461-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20462--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20463--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20464-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20465-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20466-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20467
20468 first = 1;
20469
20470 show (stream, "legacy", &column, &first);
20471 show (stream, "2008", &column, &first);
20472
20473 fputc ('\n', stream);
20474
316f5878
RS
20475 fprintf (stream, _("\
20476-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20477-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20478-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20479-non_shared do not generate code that can operate with DSOs\n\
316f5878 20480-xgot assume a 32 bit GOT\n\
dcd410fe 20481-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20482-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20483 position dependent (non shared) code\n\
316f5878
RS
20484-mabi=ABI create ABI conformant object file for:\n"));
20485
20486 first = 1;
20487
20488 show (stream, "32", &column, &first);
20489 show (stream, "o64", &column, &first);
20490 show (stream, "n32", &column, &first);
20491 show (stream, "64", &column, &first);
20492 show (stream, "eabi", &column, &first);
20493
20494 fputc ('\n', stream);
20495
20496 fprintf (stream, _("\
b4f6242e
MR
20497-32 create o32 ABI object file%s\n"),
20498 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20499 fprintf (stream, _("\
20500-n32 create n32 ABI object file%s\n"),
20501 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20502 fprintf (stream, _("\
20503-64 create 64 ABI object file%s\n"),
20504 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20505}
14e777e0 20506
1575952e 20507#ifdef TE_IRIX
14e777e0 20508enum dwarf2_format
413a266c 20509mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20510{
369943fe 20511 if (HAVE_64BIT_SYMBOLS)
1575952e 20512 return dwarf2_format_64bit_irix;
14e777e0
KB
20513 else
20514 return dwarf2_format_32bit;
20515}
1575952e 20516#endif
73369e65
EC
20517
20518int
20519mips_dwarf2_addr_size (void)
20520{
6b6b3450 20521 if (HAVE_64BIT_OBJECTS)
73369e65 20522 return 8;
73369e65
EC
20523 else
20524 return 4;
20525}
5862107c
EC
20526
20527/* Standard calling conventions leave the CFA at SP on entry. */
20528void
20529mips_cfi_frame_initial_instructions (void)
20530{
20531 cfi_add_CFA_def_cfa_register (SP);
20532}
20533
707bfff6
TS
20534int
20535tc_mips_regname_to_dw2regnum (char *regname)
20536{
20537 unsigned int regnum = -1;
20538 unsigned int reg;
20539
20540 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20541 regnum = reg;
20542
20543 return regnum;
20544}
263b2574 20545
20546/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20547 Given a symbolic attribute NAME, return the proper integer value.
20548 Returns -1 if the attribute is not known. */
20549
20550int
20551mips_convert_symbolic_attribute (const char *name)
20552{
20553 static const struct
20554 {
20555 const char * name;
20556 const int tag;
20557 }
20558 attribute_table[] =
20559 {
20560#define T(tag) {#tag, tag}
20561 T (Tag_GNU_MIPS_ABI_FP),
20562 T (Tag_GNU_MIPS_ABI_MSA),
20563#undef T
20564 };
20565 unsigned int i;
20566
20567 if (name == NULL)
20568 return -1;
20569
20570 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20571 if (streq (name, attribute_table[i].name))
20572 return attribute_table[i].tag;
20573
20574 return -1;
20575}
fd5c94ab
RS
20576
20577void
ed2917de 20578mips_md_finish (void)
fd5c94ab 20579{
351cdf24
MF
20580 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20581
fd5c94ab
RS
20582 mips_emit_delays ();
20583 if (cur_proc_ptr)
20584 as_warn (_("missing .end at end of assembly"));
919731af 20585
20586 /* Just in case no code was emitted, do the consistency check. */
20587 file_mips_check_options ();
351cdf24
MF
20588
20589 /* Set a floating-point ABI if the user did not. */
20590 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20591 {
20592 /* Perform consistency checks on the floating-point ABI. */
20593 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20594 Tag_GNU_MIPS_ABI_FP);
20595 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20596 check_fpabi (fpabi);
20597 }
20598 else
20599 {
20600 /* Soft-float gets precedence over single-float, the two options should
20601 not be used together so this should not matter. */
20602 if (file_mips_opts.soft_float == 1)
20603 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20604 /* Single-float gets precedence over all double_float cases. */
20605 else if (file_mips_opts.single_float == 1)
20606 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20607 else
20608 {
20609 switch (file_mips_opts.fp)
20610 {
20611 case 32:
20612 if (file_mips_opts.gp == 32)
20613 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20614 break;
20615 case 0:
20616 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20617 break;
20618 case 64:
20619 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20620 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20621 else if (file_mips_opts.gp == 32)
20622 fpabi = Val_GNU_MIPS_ABI_FP_64;
20623 else
20624 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20625 break;
20626 }
20627 }
20628
20629 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20630 Tag_GNU_MIPS_ABI_FP, fpabi);
20631 }
fd5c94ab 20632}
2f0c68f2
CM
20633
20634/* Returns the relocation type required for a particular CFI encoding. */
20635
20636bfd_reloc_code_real_type
20637mips_cfi_reloc_for_encoding (int encoding)
20638{
20639 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20640 return BFD_RELOC_32_PCREL;
20641 else return BFD_RELOC_NONE;
20642}