]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-mips.c
MIPS/GAS: Also respect `-mignore-branch-isa' with MIPS16 code
[thirdparty/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
2571583a 2 Copyright (C) 1993-2017 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
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
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
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24
MF
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
af7ee8bf 426
ad3fea08
TS
427/* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429#define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439/* Return true if ISA supports legacy NAN. */
440#define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
ae52f483
AB
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
ad3fea08 454
bad1aba3 455#define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
ca4e0257 459
bad1aba3 460#define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
ca4e0257 464
316f5878 465#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 466
316f5878 467#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 468
3b91255e
RS
469/* True if relocations are stored in-place. */
470#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
aed1a261
RS
472/* The ABI-derived address size. */
473#define HAVE_64BIT_ADDRESSES \
bad1aba3 474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 475#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 476
aed1a261
RS
477/* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479#define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 482
b7c7d6c1
TS
483/* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
f899b4b8 486#define ADDRESS_ADD_INSN \
b7c7d6c1 487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
488
489#define ADDRESS_ADDI_INSN \
b7c7d6c1 490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
491
492#define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495#define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
a4672219 498/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
499#define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 502
2309ddf2 503/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
504#define CPU_HAS_MICROMIPS(cpu) 0
505
60b63b72
RS
506/* True if CPU has a dror instruction. */
507#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509/* True if CPU has a ror instruction. */
510#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
dd6a37e7 512/* True if CPU is in the Octeon family */
2c629856
N
513#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 515
dd3cbb7e 516/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 517#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 518
0aa27725
RS
519/* True, if CPU has support for ldc1 and sdc1. */
520#define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
c8978940
CD
523/* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535#define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 540 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 545 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 546 || mips_opts.arch == CPU_R4010 \
e407c74b 547 || mips_opts.arch == CPU_R5900 \
c8978940
CD
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
c8978940 552 || mips_opts.arch == CPU_RM7000 \
c8978940 553 || mips_opts.arch == CPU_VR5500 \
df58fc94 554 || mips_opts.micromips \
c8978940 555 )
252b5132
RH
556
557/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
67dc82bc 560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
561 level I and microMIPS mode instructions are always interlocked. */
562#define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
e407c74b 565 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
566 || mips_opts.micromips \
567 )
252b5132 568
81912461
ILT
569/* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
43885403
MF
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
bdaaa2e1 577/* Itbl support may require additional care here. */
81912461
ILT
578#define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
df58fc94 583 || mips_opts.micromips \
81912461
ILT
584 )
585
586/* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592#define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
252b5132 596
6b76fefe
CM
597/* Is this a mfhi or mflo instruction? */
598#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
df58fc94
RS
601/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604#define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
42429eac 607/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 608#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
609#define GPR_SMIN (-GPR_SMAX - 1)
610
252b5132
RH
611/* MIPS PIC level. */
612
a161fe53 613enum mips_pic_level mips_pic;
252b5132 614
c9914766 615/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 616 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 617static int mips_big_got = 0;
252b5132
RH
618
619/* 1 if trap instructions should used for overflow rather than break
620 instructions. */
c9914766 621static int mips_trap = 0;
252b5132 622
119d663a 623/* 1 if double width floating point constants should not be constructed
b6ff326e 624 by assembling two single width halves into two single width floating
119d663a
NC
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
d547a75e 627 in the status register, and the setting of this bit cannot be determined
119d663a
NC
628 automatically at assemble time. */
629static int mips_disable_float_construction;
630
252b5132
RH
631/* Non-zero if any .set noreorder directives were used. */
632
633static int mips_any_noreorder;
634
6b76fefe
CM
635/* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637static int mips_7000_hilo_fix;
638
02ffd3e4 639/* The size of objects in the small data section. */
156c2f8b 640static unsigned int g_switch_value = 8;
252b5132
RH
641/* Whether the -G option was used. */
642static int g_switch_seen = 0;
643
644#define N_RMASK 0xc4
645#define N_VFP 0xd4
646
647/* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
252b5132
RH
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 659static int nopic_need_relax (symbolS *, int);
252b5132
RH
660
661/* handle of the OPCODE hash table */
662static struct hash_control *op_hash = NULL;
663
664/* The opcode hash table we use for the mips16. */
665static struct hash_control *mips16_op_hash = NULL;
666
df58fc94
RS
667/* The opcode hash table we use for the microMIPS ASE. */
668static struct hash_control *micromips_op_hash = NULL;
669
252b5132
RH
670/* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672const char comment_chars[] = "#";
673
674/* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677/* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
bdaaa2e1 679 #NO_APP at the beginning of its output. */
252b5132
RH
680/* Also note that C style comments are always supported. */
681const char line_comment_chars[] = "#";
682
bdaaa2e1 683/* This array holds machine specific line separator characters. */
63a0b638 684const char line_separator_chars[] = ";";
252b5132
RH
685
686/* Chars that can be used to separate mant from exp in floating point nums */
687const char EXP_CHARS[] = "eE";
688
689/* Chars that mean this number is a floating point constant */
690/* As in 0f12.456 */
691/* or 0d1.2345e12 */
692const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
e3de51ce
RS
699/* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705};
706
707/* Information about an error that was found while assembling the current
708 instruction. */
709struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732};
733
734/* The error that should be reported for the current instruction. */
735static struct mips_insn_error insn_error;
252b5132
RH
736
737static int auto_align = 1;
738
739/* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743static offsetT mips_cprestore_offset = -1;
744
67c1ffbe 745/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 746 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 747 offset and even an other register than $gp as global pointer. */
6478892d
TS
748static offsetT mips_cpreturn_offset = -1;
749static int mips_cpreturn_register = -1;
750static int mips_gp_register = GP;
def2e0dd 751static int mips_gprel_offset = 0;
6478892d 752
7a621144
DJ
753/* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755static int mips_cprestore_valid = 0;
756
252b5132
RH
757/* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759static int mips_frame_reg = SP;
760
7a621144
DJ
761/* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763static int mips_frame_reg_valid = 0;
764
252b5132
RH
765/* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768/* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772static int mips_optimize = 2;
773
774/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776static int mips_debug = 0;
777
7d8e00cf
RS
778/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779#define MAX_VR4130_NOPS 4
780
781/* The maximum number of NOPs needed to fill delay slots. */
782#define MAX_DELAY_NOPS 2
783
784/* The maximum number of NOPs needed for any purpose. */
785#define MAX_NOPS 4
71400594
RS
786
787/* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 793
fc76e730 794/* Arrays of operands for each instruction. */
14daeee3 795#define MAX_OPERANDS 6
fc76e730
RS
796struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798};
799static struct mips_operand_array *mips_operands;
800static struct mips_operand_array *mips16_operands;
801static struct mips_operand_array *micromips_operands;
802
1e915849 803/* Nop instructions used by emit_nop. */
df58fc94
RS
804static struct mips_cl_insn nop_insn;
805static struct mips_cl_insn mips16_nop_insn;
806static struct mips_cl_insn micromips_nop16_insn;
807static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
808
809/* The appropriate nop for the current mode. */
833794fc
MR
810#define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
df58fc94
RS
817
818/* The size of NOP_INSN in bytes. */
833794fc
MR
819#define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
252b5132 822
252b5132
RH
823/* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827static fragS *prev_nop_frag;
828
829/* The number of nop instructions we created in prev_nop_frag. */
830static int prev_nop_frag_holds;
831
832/* The number of nop instructions that we know we need in
bdaaa2e1 833 prev_nop_frag. */
252b5132
RH
834static int prev_nop_frag_required;
835
836/* The number of instructions we've seen since prev_nop_frag. */
837static int prev_nop_frag_since;
838
e8044f35
RS
839/* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
252b5132
RH
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
bdaaa2e1 848 corresponding LO relocation. */
252b5132 849
e972090a
NC
850struct mips_hi_fixup
851{
252b5132
RH
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858};
859
860/* The list of unmatched HI relocs. */
861
862static struct mips_hi_fixup *mips_hi_fixup_list;
863
64bdfcaf
RS
864/* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867static fragS *prev_reloc_op_frag;
868
252b5132
RH
869/* Map mips16 register numbers to normal MIPS register numbers. */
870
e972090a
NC
871static const unsigned int mips16_to_32_reg_map[] =
872{
252b5132
RH
873 16, 17, 2, 3, 4, 5, 6, 7
874};
60b63b72 875
df58fc94
RS
876/* Map microMIPS register numbers to normal MIPS register numbers. */
877
df58fc94 878#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
879
880/* The microMIPS registers with type h. */
e76ff5ab 881static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
882{
883 5, 5, 6, 4, 4, 4, 4, 4
884};
e76ff5ab 885static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
886{
887 6, 7, 7, 21, 22, 5, 6, 7
888};
889
df58fc94
RS
890/* The microMIPS registers with type m. */
891static const unsigned int micromips_to_32_reg_m_map[] =
892{
893 0, 17, 2, 3, 16, 18, 19, 20
894};
895
896#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
71400594
RS
898/* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
c67a084a
NC
900enum fix_vr4120_class
901{
71400594
RS
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909};
910
c67a084a
NC
911/* ...likewise -mfix-loongson2f-jump. */
912static bfd_boolean mips_fix_loongson2f_jump;
913
914/* ...likewise -mfix-loongson2f-nop. */
915static bfd_boolean mips_fix_loongson2f_nop;
916
917/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918static bfd_boolean mips_fix_loongson2f;
919
71400594
RS
920/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925/* True if -mfix-vr4120 is in force. */
d766e8ec 926static int mips_fix_vr4120;
4a6a3df4 927
7d8e00cf
RS
928/* ...likewise -mfix-vr4130. */
929static int mips_fix_vr4130;
930
6a32d874
CM
931/* ...likewise -mfix-24k. */
932static int mips_fix_24k;
933
a8d14a88
CM
934/* ...likewise -mfix-rm7000 */
935static int mips_fix_rm7000;
936
d954098f
DD
937/* ...likewise -mfix-cn63xxp1 */
938static bfd_boolean mips_fix_cn63xxp1;
939
4a6a3df4
AO
940/* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945static int mips_relax_branch;
8b10b0b3
MR
946
947/* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950static bfd_boolean mips_ignore_branch_isa;
252b5132 951\f
4d7206a2
RS
952/* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
584892a6
RS
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
4d7206a2 969
ce8ad872
MR
970 RELAX_PIC
971 Set if generating PIC code.
972
584892a6
RS
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
4d7206a2 989
df58fc94
RS
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
4d7206a2
RS
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
ce8ad872
MR
1014#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1016
584892a6
RS
1017#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1019#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020#define RELAX_USE_SECOND 0x20000
1021#define RELAX_SECOND_LONGER 0x40000
1022#define RELAX_NOMACRO 0x80000
1023#define RELAX_DELAY_SLOT 0x100000
1024#define RELAX_DELAY_SLOT_16BIT 0x200000
1025#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1027
4a6a3df4
AO
1028/* Branch without likely bit. If label is out of range, we turn:
1029
134c0c8b 1030 beq reg1, reg2, label
4a6a3df4
AO
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
b34976b6 1074
4a6a3df4
AO
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1095#define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
66b3e8da
MR
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
ce8ad872
MR
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1105#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1106#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1111#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1112
252b5132
RH
1113/* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
25499ac7 1133#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1134 small, ext, \
1135 dslot, jal_dslot) \
252b5132
RH
1136 (0x80000000 \
1137 | ((type) & 0xff) \
25499ac7
MR
1138 | ((e2) ? 0x100 : 0) \
1139 | ((pic) ? 0x200 : 0) \
1140 | ((sym32) ? 0x400 : 0) \
1141 | ((nomacro) ? 0x800 : 0) \
1142 | ((small) ? 0x1000 : 0) \
1143 | ((ext) ? 0x2000 : 0) \
1144 | ((dslot) ? 0x4000 : 0) \
1145 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1146
4a6a3df4 1147#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1148#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1149#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1150#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1151#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1152#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1153#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1154#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1155#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1156#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1157
1158#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1159#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1160#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1161#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1162#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1163#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1164#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1165#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1166#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1167
df58fc94
RS
1168/* For microMIPS code, we use relaxation similar to one we use for
1169 MIPS16 code. Some instructions that take immediate values support
1170 two encodings: a small one which takes some small value, and a
1171 larger one which takes a 16 bit value. As some branches also follow
1172 this pattern, relaxing these values is required.
1173
1174 We can assemble both microMIPS and normal MIPS code in a single
1175 object. Therefore, we need to support this type of relaxation at
1176 the same time that we support the relaxation described above. We
1177 use one of the high bits of the subtype field to distinguish these
1178 cases.
1179
1180 The information we store for this type of relaxation is the argument
1181 code found in the opcode file for this relocation, the register
8484fb75
MR
1182 selected as the assembler temporary, whether in the 32-bit
1183 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1184 compact, whether there is no delay-slot instruction available to fill
1185 in, whether it stores the link address implicitly in $ra, whether
1186 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1187 instructions is enabled, and whether the displacement of a branch is
1188 too large to fit as an immediate argument of a 16-bit and a 32-bit
1189 branch, respectively. */
ce8ad872 1190#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1191 uncond, compact, link, nods, \
40209cad
MR
1192 relax32, toofar16, toofar32) \
1193 (0x40000000 \
1194 | ((type) & 0xff) \
1195 | (((at) & 0x1f) << 8) \
8484fb75 1196 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1197 | ((pic) ? 0x4000 : 0) \
1198 | ((uncond) ? 0x8000 : 0) \
1199 | ((compact) ? 0x10000 : 0) \
1200 | ((link) ? 0x20000 : 0) \
1201 | ((nods) ? 0x40000 : 0) \
1202 | ((relax32) ? 0x80000 : 0) \
1203 | ((toofar16) ? 0x100000 : 0) \
1204 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1205#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1206#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1207#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1208#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1209#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1210#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1211#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1212#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1213#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1214#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1215
1216#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1217#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1218#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1219#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1220#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1221#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1222
43c0598f
RS
1223/* Sign-extend 16-bit value X. */
1224#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1225
885add95
CD
1226/* Is the given value a sign-extended 32-bit value? */
1227#define IS_SEXT_32BIT_NUM(x) \
1228 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1229 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1230
1231/* Is the given value a sign-extended 16-bit value? */
1232#define IS_SEXT_16BIT_NUM(x) \
1233 (((x) &~ (offsetT) 0x7fff) == 0 \
1234 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1235
df58fc94
RS
1236/* Is the given value a sign-extended 12-bit value? */
1237#define IS_SEXT_12BIT_NUM(x) \
1238 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1239
7f3c4072
CM
1240/* Is the given value a sign-extended 9-bit value? */
1241#define IS_SEXT_9BIT_NUM(x) \
1242 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1243
2051e8c4
MR
1244/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1245#define IS_ZEXT_32BIT_NUM(x) \
1246 (((x) &~ (offsetT) 0xffffffff) == 0 \
1247 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1248
bf12938e
RS
1249/* Extract bits MASK << SHIFT from STRUCT and shift them right
1250 SHIFT places. */
1251#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1252 (((STRUCT) >> (SHIFT)) & (MASK))
1253
bf12938e 1254/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1255#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1256 (!(MICROMIPS) \
1257 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1258 : EXTRACT_BITS ((INSN).insn_opcode, \
1259 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1260#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1261 EXTRACT_BITS ((INSN).insn_opcode, \
1262 MIPS16OP_MASK_##FIELD, \
1263 MIPS16OP_SH_##FIELD)
5c04167a
RS
1264
1265/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1266#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1267\f
df58fc94
RS
1268/* Whether or not we are emitting a branch-likely macro. */
1269static bfd_boolean emit_branch_likely_macro = FALSE;
1270
4d7206a2
RS
1271/* Global variables used when generating relaxable macros. See the
1272 comment above RELAX_ENCODE for more details about how relaxation
1273 is used. */
1274static struct {
1275 /* 0 if we're not emitting a relaxable macro.
1276 1 if we're emitting the first of the two relaxation alternatives.
1277 2 if we're emitting the second alternative. */
1278 int sequence;
1279
1280 /* The first relaxable fixup in the current frag. (In other words,
1281 the first fixup that refers to relaxable code.) */
1282 fixS *first_fixup;
1283
1284 /* sizes[0] says how many bytes of the first alternative are stored in
1285 the current frag. Likewise sizes[1] for the second alternative. */
1286 unsigned int sizes[2];
1287
1288 /* The symbol on which the choice of sequence depends. */
1289 symbolS *symbol;
1290} mips_relax;
252b5132 1291\f
584892a6
RS
1292/* Global variables used to decide whether a macro needs a warning. */
1293static struct {
1294 /* True if the macro is in a branch delay slot. */
1295 bfd_boolean delay_slot_p;
1296
df58fc94
RS
1297 /* Set to the length in bytes required if the macro is in a delay slot
1298 that requires a specific length of instruction, otherwise zero. */
1299 unsigned int delay_slot_length;
1300
584892a6
RS
1301 /* For relaxable macros, sizes[0] is the length of the first alternative
1302 in bytes and sizes[1] is the length of the second alternative.
1303 For non-relaxable macros, both elements give the length of the
1304 macro in bytes. */
1305 unsigned int sizes[2];
1306
df58fc94
RS
1307 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1308 instruction of the first alternative in bytes and first_insn_sizes[1]
1309 is the length of the first instruction of the second alternative.
1310 For non-relaxable macros, both elements give the length of the first
1311 instruction in bytes.
1312
1313 Set to zero if we haven't yet seen the first instruction. */
1314 unsigned int first_insn_sizes[2];
1315
1316 /* For relaxable macros, insns[0] is the number of instructions for the
1317 first alternative and insns[1] is the number of instructions for the
1318 second alternative.
1319
1320 For non-relaxable macros, both elements give the number of
1321 instructions for the macro. */
1322 unsigned int insns[2];
1323
584892a6
RS
1324 /* The first variant frag for this macro. */
1325 fragS *first_frag;
1326} mips_macro_warning;
1327\f
252b5132
RH
1328/* Prototypes for static functions. */
1329
252b5132
RH
1330enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1331
b34976b6 1332static void append_insn
df58fc94
RS
1333 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1334 bfd_boolean expansionp);
7d10b47d 1335static void mips_no_prev_insn (void);
c67a084a 1336static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1337static void mips16_macro_build
03ea81db 1338 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1339static void load_register (int, expressionS *, int);
584892a6
RS
1340static void macro_start (void);
1341static void macro_end (void);
833794fc 1342static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1343static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1344static void mips_ip (char *str, struct mips_cl_insn * ip);
1345static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1346static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1347static void mips16_immed
3b4dbbbf 1348 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1349 unsigned int, unsigned long *);
5e0116d5 1350static size_t my_getSmallExpression
17a2f251
TS
1351 (expressionS *, bfd_reloc_code_real_type *, char *);
1352static void my_getExpression (expressionS *, char *);
1353static void s_align (int);
1354static void s_change_sec (int);
1355static void s_change_section (int);
1356static void s_cons (int);
1357static void s_float_cons (int);
1358static void s_mips_globl (int);
1359static void s_option (int);
1360static void s_mipsset (int);
1361static void s_abicalls (int);
1362static void s_cpload (int);
1363static void s_cpsetup (int);
1364static void s_cplocal (int);
1365static void s_cprestore (int);
1366static void s_cpreturn (int);
741d6ea8
JM
1367static void s_dtprelword (int);
1368static void s_dtpreldword (int);
d0f13682
CLT
1369static void s_tprelword (int);
1370static void s_tpreldword (int);
17a2f251
TS
1371static void s_gpvalue (int);
1372static void s_gpword (int);
1373static void s_gpdword (int);
a3f278e2 1374static void s_ehword (int);
17a2f251
TS
1375static void s_cpadd (int);
1376static void s_insn (int);
ba92f887 1377static void s_nan (int);
919731af 1378static void s_module (int);
17a2f251
TS
1379static void s_mips_ent (int);
1380static void s_mips_end (int);
1381static void s_mips_frame (int);
1382static void s_mips_mask (int reg_type);
1383static void s_mips_stab (int);
1384static void s_mips_weakext (int);
1385static void s_mips_file (int);
1386static void s_mips_loc (int);
9e009953 1387static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1388static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1389static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1390static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1391static void file_mips_check_options (void);
e7af610e
NC
1392
1393/* Table and functions used to map between CPU/ISA names, and
1394 ISA levels, and CPU numbers. */
1395
e972090a
NC
1396struct mips_cpu_info
1397{
e7af610e 1398 const char *name; /* CPU or ISA name. */
d16afab6
RS
1399 int flags; /* MIPS_CPU_* flags. */
1400 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1401 int isa; /* ISA level. */
1402 int cpu; /* CPU number (default CPU if ISA). */
1403};
1404
ad3fea08 1405#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1406
17a2f251
TS
1407static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1408static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1409static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1410\f
c31f3936
RS
1411/* Command-line options. */
1412const char *md_shortopts = "O::g::G:";
1413
1414enum options
1415 {
1416 OPTION_MARCH = OPTION_MD_BASE,
1417 OPTION_MTUNE,
1418 OPTION_MIPS1,
1419 OPTION_MIPS2,
1420 OPTION_MIPS3,
1421 OPTION_MIPS4,
1422 OPTION_MIPS5,
1423 OPTION_MIPS32,
1424 OPTION_MIPS64,
1425 OPTION_MIPS32R2,
ae52f483
AB
1426 OPTION_MIPS32R3,
1427 OPTION_MIPS32R5,
7361da2c 1428 OPTION_MIPS32R6,
c31f3936 1429 OPTION_MIPS64R2,
ae52f483
AB
1430 OPTION_MIPS64R3,
1431 OPTION_MIPS64R5,
7361da2c 1432 OPTION_MIPS64R6,
c31f3936
RS
1433 OPTION_MIPS16,
1434 OPTION_NO_MIPS16,
1435 OPTION_MIPS3D,
1436 OPTION_NO_MIPS3D,
1437 OPTION_MDMX,
1438 OPTION_NO_MDMX,
1439 OPTION_DSP,
1440 OPTION_NO_DSP,
1441 OPTION_MT,
1442 OPTION_NO_MT,
1443 OPTION_VIRT,
1444 OPTION_NO_VIRT,
56d438b1
CF
1445 OPTION_MSA,
1446 OPTION_NO_MSA,
c31f3936
RS
1447 OPTION_SMARTMIPS,
1448 OPTION_NO_SMARTMIPS,
1449 OPTION_DSPR2,
1450 OPTION_NO_DSPR2,
8f4f9071
MF
1451 OPTION_DSPR3,
1452 OPTION_NO_DSPR3,
c31f3936
RS
1453 OPTION_EVA,
1454 OPTION_NO_EVA,
7d64c587
AB
1455 OPTION_XPA,
1456 OPTION_NO_XPA,
c31f3936
RS
1457 OPTION_MICROMIPS,
1458 OPTION_NO_MICROMIPS,
1459 OPTION_MCU,
1460 OPTION_NO_MCU,
25499ac7
MR
1461 OPTION_MIPS16E2,
1462 OPTION_NO_MIPS16E2,
c31f3936
RS
1463 OPTION_COMPAT_ARCH_BASE,
1464 OPTION_M4650,
1465 OPTION_NO_M4650,
1466 OPTION_M4010,
1467 OPTION_NO_M4010,
1468 OPTION_M4100,
1469 OPTION_NO_M4100,
1470 OPTION_M3900,
1471 OPTION_NO_M3900,
1472 OPTION_M7000_HILO_FIX,
1473 OPTION_MNO_7000_HILO_FIX,
1474 OPTION_FIX_24K,
1475 OPTION_NO_FIX_24K,
a8d14a88
CM
1476 OPTION_FIX_RM7000,
1477 OPTION_NO_FIX_RM7000,
c31f3936
RS
1478 OPTION_FIX_LOONGSON2F_JUMP,
1479 OPTION_NO_FIX_LOONGSON2F_JUMP,
1480 OPTION_FIX_LOONGSON2F_NOP,
1481 OPTION_NO_FIX_LOONGSON2F_NOP,
1482 OPTION_FIX_VR4120,
1483 OPTION_NO_FIX_VR4120,
1484 OPTION_FIX_VR4130,
1485 OPTION_NO_FIX_VR4130,
1486 OPTION_FIX_CN63XXP1,
1487 OPTION_NO_FIX_CN63XXP1,
1488 OPTION_TRAP,
1489 OPTION_BREAK,
1490 OPTION_EB,
1491 OPTION_EL,
1492 OPTION_FP32,
1493 OPTION_GP32,
1494 OPTION_CONSTRUCT_FLOATS,
1495 OPTION_NO_CONSTRUCT_FLOATS,
1496 OPTION_FP64,
351cdf24 1497 OPTION_FPXX,
c31f3936
RS
1498 OPTION_GP64,
1499 OPTION_RELAX_BRANCH,
1500 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1501 OPTION_IGNORE_BRANCH_ISA,
1502 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1503 OPTION_INSN32,
1504 OPTION_NO_INSN32,
c31f3936
RS
1505 OPTION_MSHARED,
1506 OPTION_MNO_SHARED,
1507 OPTION_MSYM32,
1508 OPTION_MNO_SYM32,
1509 OPTION_SOFT_FLOAT,
1510 OPTION_HARD_FLOAT,
1511 OPTION_SINGLE_FLOAT,
1512 OPTION_DOUBLE_FLOAT,
1513 OPTION_32,
c31f3936
RS
1514 OPTION_CALL_SHARED,
1515 OPTION_CALL_NONPIC,
1516 OPTION_NON_SHARED,
1517 OPTION_XGOT,
1518 OPTION_MABI,
1519 OPTION_N32,
1520 OPTION_64,
1521 OPTION_MDEBUG,
1522 OPTION_NO_MDEBUG,
1523 OPTION_PDR,
1524 OPTION_NO_PDR,
1525 OPTION_MVXWORKS_PIC,
ba92f887 1526 OPTION_NAN,
351cdf24
MF
1527 OPTION_ODD_SPREG,
1528 OPTION_NO_ODD_SPREG,
c31f3936
RS
1529 OPTION_END_OF_ENUM
1530 };
1531
1532struct option md_longopts[] =
1533{
1534 /* Options which specify architecture. */
1535 {"march", required_argument, NULL, OPTION_MARCH},
1536 {"mtune", required_argument, NULL, OPTION_MTUNE},
1537 {"mips0", no_argument, NULL, OPTION_MIPS1},
1538 {"mips1", no_argument, NULL, OPTION_MIPS1},
1539 {"mips2", no_argument, NULL, OPTION_MIPS2},
1540 {"mips3", no_argument, NULL, OPTION_MIPS3},
1541 {"mips4", no_argument, NULL, OPTION_MIPS4},
1542 {"mips5", no_argument, NULL, OPTION_MIPS5},
1543 {"mips32", no_argument, NULL, OPTION_MIPS32},
1544 {"mips64", no_argument, NULL, OPTION_MIPS64},
1545 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1546 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1547 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1548 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1549 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1550 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1551 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1552 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1553
1554 /* Options which specify Application Specific Extensions (ASEs). */
1555 {"mips16", no_argument, NULL, OPTION_MIPS16},
1556 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1557 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1558 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1559 {"mdmx", no_argument, NULL, OPTION_MDMX},
1560 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1561 {"mdsp", no_argument, NULL, OPTION_DSP},
1562 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1563 {"mmt", no_argument, NULL, OPTION_MT},
1564 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1565 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1566 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1567 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1568 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1569 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1570 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1571 {"meva", no_argument, NULL, OPTION_EVA},
1572 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1573 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1574 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1575 {"mmcu", no_argument, NULL, OPTION_MCU},
1576 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1577 {"mvirt", no_argument, NULL, OPTION_VIRT},
1578 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1579 {"mmsa", no_argument, NULL, OPTION_MSA},
1580 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1581 {"mxpa", no_argument, NULL, OPTION_XPA},
1582 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1583 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1584 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
c31f3936
RS
1585
1586 /* Old-style architecture options. Don't add more of these. */
1587 {"m4650", no_argument, NULL, OPTION_M4650},
1588 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1589 {"m4010", no_argument, NULL, OPTION_M4010},
1590 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1591 {"m4100", no_argument, NULL, OPTION_M4100},
1592 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1593 {"m3900", no_argument, NULL, OPTION_M3900},
1594 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1595
1596 /* Options which enable bug fixes. */
1597 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1598 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1599 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1600 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1601 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1602 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1603 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1604 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1605 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1606 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1607 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1608 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1609 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1610 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1611 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1612 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1613 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1614
1615 /* Miscellaneous options. */
1616 {"trap", no_argument, NULL, OPTION_TRAP},
1617 {"no-break", no_argument, NULL, OPTION_TRAP},
1618 {"break", no_argument, NULL, OPTION_BREAK},
1619 {"no-trap", no_argument, NULL, OPTION_BREAK},
1620 {"EB", no_argument, NULL, OPTION_EB},
1621 {"EL", no_argument, NULL, OPTION_EL},
1622 {"mfp32", no_argument, NULL, OPTION_FP32},
1623 {"mgp32", no_argument, NULL, OPTION_GP32},
1624 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1625 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1626 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1627 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1628 {"mgp64", no_argument, NULL, OPTION_GP64},
1629 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1630 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1631 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1632 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1633 {"minsn32", no_argument, NULL, OPTION_INSN32},
1634 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1635 {"mshared", no_argument, NULL, OPTION_MSHARED},
1636 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1637 {"msym32", no_argument, NULL, OPTION_MSYM32},
1638 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1639 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1640 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1641 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1642 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1643 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1644 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1645
1646 /* Strictly speaking this next option is ELF specific,
1647 but we allow it for other ports as well in order to
1648 make testing easier. */
1649 {"32", no_argument, NULL, OPTION_32},
1650
1651 /* ELF-specific options. */
c31f3936
RS
1652 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1653 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1654 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1655 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1656 {"xgot", no_argument, NULL, OPTION_XGOT},
1657 {"mabi", required_argument, NULL, OPTION_MABI},
1658 {"n32", no_argument, NULL, OPTION_N32},
1659 {"64", no_argument, NULL, OPTION_64},
1660 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1661 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1662 {"mpdr", no_argument, NULL, OPTION_PDR},
1663 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1664 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1665 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1666
1667 {NULL, no_argument, NULL, 0}
1668};
1669size_t md_longopts_size = sizeof (md_longopts);
1670\f
c6278170
RS
1671/* Information about either an Application Specific Extension or an
1672 optional architecture feature that, for simplicity, we treat in the
1673 same way as an ASE. */
1674struct mips_ase
1675{
1676 /* The name of the ASE, used in both the command-line and .set options. */
1677 const char *name;
1678
1679 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1680 and 64-bit architectures, the flags here refer to the subset that
1681 is available on both. */
1682 unsigned int flags;
1683
1684 /* The ASE_* flag used for instructions that are available on 64-bit
1685 architectures but that are not included in FLAGS. */
1686 unsigned int flags64;
1687
1688 /* The command-line options that turn the ASE on and off. */
1689 int option_on;
1690 int option_off;
1691
1692 /* The minimum required architecture revisions for MIPS32, MIPS64,
1693 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1694 int mips32_rev;
1695 int mips64_rev;
1696 int micromips32_rev;
1697 int micromips64_rev;
7361da2c
AB
1698
1699 /* The architecture where the ASE was removed or -1 if the extension has not
1700 been removed. */
1701 int rem_rev;
c6278170
RS
1702};
1703
1704/* A table of all supported ASEs. */
1705static const struct mips_ase mips_ases[] = {
1706 { "dsp", ASE_DSP, ASE_DSP64,
1707 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1708 2, 2, 2, 2,
1709 -1 },
c6278170
RS
1710
1711 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1712 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1713 2, 2, 2, 2,
1714 -1 },
c6278170 1715
8f4f9071
MF
1716 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1717 OPTION_DSPR3, OPTION_NO_DSPR3,
1718 6, 6, -1, -1,
1719 -1 },
1720
c6278170
RS
1721 { "eva", ASE_EVA, 0,
1722 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1723 2, 2, 2, 2,
1724 -1 },
c6278170
RS
1725
1726 { "mcu", ASE_MCU, 0,
1727 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1728 2, 2, 2, 2,
1729 -1 },
c6278170
RS
1730
1731 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1732 { "mdmx", ASE_MDMX, 0,
1733 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1734 -1, 1, -1, -1,
1735 6 },
c6278170
RS
1736
1737 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1738 { "mips3d", ASE_MIPS3D, 0,
1739 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1740 2, 1, -1, -1,
1741 6 },
c6278170
RS
1742
1743 { "mt", ASE_MT, 0,
1744 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1745 2, 2, -1, -1,
1746 -1 },
c6278170
RS
1747
1748 { "smartmips", ASE_SMARTMIPS, 0,
1749 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1750 1, -1, -1, -1,
1751 6 },
c6278170
RS
1752
1753 { "virt", ASE_VIRT, ASE_VIRT64,
1754 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1755 2, 2, 2, 2,
1756 -1 },
56d438b1
CF
1757
1758 { "msa", ASE_MSA, ASE_MSA64,
1759 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1760 2, 2, 2, 2,
1761 -1 },
7d64c587
AB
1762
1763 { "xpa", ASE_XPA, 0,
1764 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1765 2, 2, 2, 2,
7361da2c 1766 -1 },
25499ac7
MR
1767
1768 { "mips16e2", ASE_MIPS16E2, 0,
1769 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1770 2, 2, -1, -1,
1771 6 },
c6278170
RS
1772};
1773
1774/* The set of ASEs that require -mfp64. */
82bda27b 1775#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1776
1777/* Groups of ASE_* flags that represent different revisions of an ASE. */
1778static const unsigned int mips_ase_groups[] = {
8f4f9071 1779 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1780};
1781\f
252b5132
RH
1782/* Pseudo-op table.
1783
1784 The following pseudo-ops from the Kane and Heinrich MIPS book
1785 should be defined here, but are currently unsupported: .alias,
1786 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1787
1788 The following pseudo-ops from the Kane and Heinrich MIPS book are
1789 specific to the type of debugging information being generated, and
1790 should be defined by the object format: .aent, .begin, .bend,
1791 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1792 .vreg.
1793
1794 The following pseudo-ops from the Kane and Heinrich MIPS book are
1795 not MIPS CPU specific, but are also not specific to the object file
1796 format. This file is probably the best place to define them, but
d84bcf09 1797 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1798
e972090a
NC
1799static const pseudo_typeS mips_pseudo_table[] =
1800{
beae10d5 1801 /* MIPS specific pseudo-ops. */
252b5132
RH
1802 {"option", s_option, 0},
1803 {"set", s_mipsset, 0},
1804 {"rdata", s_change_sec, 'r'},
1805 {"sdata", s_change_sec, 's'},
1806 {"livereg", s_ignore, 0},
1807 {"abicalls", s_abicalls, 0},
1808 {"cpload", s_cpload, 0},
6478892d
TS
1809 {"cpsetup", s_cpsetup, 0},
1810 {"cplocal", s_cplocal, 0},
252b5132 1811 {"cprestore", s_cprestore, 0},
6478892d 1812 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1813 {"dtprelword", s_dtprelword, 0},
1814 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1815 {"tprelword", s_tprelword, 0},
1816 {"tpreldword", s_tpreldword, 0},
6478892d 1817 {"gpvalue", s_gpvalue, 0},
252b5132 1818 {"gpword", s_gpword, 0},
10181a0d 1819 {"gpdword", s_gpdword, 0},
a3f278e2 1820 {"ehword", s_ehword, 0},
252b5132
RH
1821 {"cpadd", s_cpadd, 0},
1822 {"insn", s_insn, 0},
ba92f887 1823 {"nan", s_nan, 0},
919731af 1824 {"module", s_module, 0},
252b5132 1825
beae10d5 1826 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1827 chips. */
38a57ae7 1828 {"asciiz", stringer, 8 + 1},
252b5132
RH
1829 {"bss", s_change_sec, 'b'},
1830 {"err", s_err, 0},
1831 {"half", s_cons, 1},
1832 {"dword", s_cons, 3},
1833 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1834 {"origin", s_org, 0},
1835 {"repeat", s_rept, 0},
252b5132 1836
998b3c36
MR
1837 /* For MIPS this is non-standard, but we define it for consistency. */
1838 {"sbss", s_change_sec, 'B'},
1839
beae10d5 1840 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1841 here for one reason or another. */
1842 {"align", s_align, 0},
1843 {"byte", s_cons, 0},
1844 {"data", s_change_sec, 'd'},
1845 {"double", s_float_cons, 'd'},
1846 {"float", s_float_cons, 'f'},
1847 {"globl", s_mips_globl, 0},
1848 {"global", s_mips_globl, 0},
1849 {"hword", s_cons, 1},
1850 {"int", s_cons, 2},
1851 {"long", s_cons, 2},
1852 {"octa", s_cons, 4},
1853 {"quad", s_cons, 3},
cca86cc8 1854 {"section", s_change_section, 0},
252b5132
RH
1855 {"short", s_cons, 1},
1856 {"single", s_float_cons, 'f'},
754e2bb9 1857 {"stabd", s_mips_stab, 'd'},
252b5132 1858 {"stabn", s_mips_stab, 'n'},
754e2bb9 1859 {"stabs", s_mips_stab, 's'},
252b5132
RH
1860 {"text", s_change_sec, 't'},
1861 {"word", s_cons, 2},
add56521 1862
add56521 1863 { "extern", ecoff_directive_extern, 0},
add56521 1864
43841e91 1865 { NULL, NULL, 0 },
252b5132
RH
1866};
1867
e972090a
NC
1868static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1869{
beae10d5
KH
1870 /* These pseudo-ops should be defined by the object file format.
1871 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1872 {"aent", s_mips_ent, 1},
1873 {"bgnb", s_ignore, 0},
1874 {"end", s_mips_end, 0},
1875 {"endb", s_ignore, 0},
1876 {"ent", s_mips_ent, 0},
c5dd6aab 1877 {"file", s_mips_file, 0},
252b5132
RH
1878 {"fmask", s_mips_mask, 'F'},
1879 {"frame", s_mips_frame, 0},
c5dd6aab 1880 {"loc", s_mips_loc, 0},
252b5132
RH
1881 {"mask", s_mips_mask, 'R'},
1882 {"verstamp", s_ignore, 0},
43841e91 1883 { NULL, NULL, 0 },
252b5132
RH
1884};
1885
3ae8dd8d
MR
1886/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1887 purpose of the `.dc.a' internal pseudo-op. */
1888
1889int
1890mips_address_bytes (void)
1891{
919731af 1892 file_mips_check_options ();
3ae8dd8d
MR
1893 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1894}
1895
17a2f251 1896extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1897
1898void
17a2f251 1899mips_pop_insert (void)
252b5132
RH
1900{
1901 pop_insert (mips_pseudo_table);
1902 if (! ECOFF_DEBUGGING)
1903 pop_insert (mips_nonecoff_pseudo_table);
1904}
1905\f
1906/* Symbols labelling the current insn. */
1907
e972090a
NC
1908struct insn_label_list
1909{
252b5132
RH
1910 struct insn_label_list *next;
1911 symbolS *label;
1912};
1913
252b5132 1914static struct insn_label_list *free_insn_labels;
742a56fe 1915#define label_list tc_segment_info_data.labels
252b5132 1916
17a2f251 1917static void mips_clear_insn_labels (void);
df58fc94
RS
1918static void mips_mark_labels (void);
1919static void mips_compressed_mark_labels (void);
252b5132
RH
1920
1921static inline void
17a2f251 1922mips_clear_insn_labels (void)
252b5132 1923{
ed9e98c2 1924 struct insn_label_list **pl;
a8dbcb85 1925 segment_info_type *si;
252b5132 1926
a8dbcb85
TS
1927 if (now_seg)
1928 {
1929 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1930 ;
3739860c 1931
a8dbcb85
TS
1932 si = seg_info (now_seg);
1933 *pl = si->label_list;
1934 si->label_list = NULL;
1935 }
252b5132 1936}
a8dbcb85 1937
df58fc94
RS
1938/* Mark instruction labels in MIPS16/microMIPS mode. */
1939
1940static inline void
1941mips_mark_labels (void)
1942{
1943 if (HAVE_CODE_COMPRESSION)
1944 mips_compressed_mark_labels ();
1945}
252b5132
RH
1946\f
1947static char *expr_end;
1948
e423441d 1949/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1950 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1951
1952static expressionS imm_expr;
252b5132 1953
77bd4346
RS
1954/* The relocatable field in an instruction and the relocs associated
1955 with it. These variables are used for instructions like LUI and
1956 JAL as well as true offsets. They are also used for address
1957 operands in macros. */
252b5132 1958
77bd4346 1959static expressionS offset_expr;
f6688943
TS
1960static bfd_reloc_code_real_type offset_reloc[3]
1961 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1962
df58fc94
RS
1963/* This is set to the resulting size of the instruction to be produced
1964 by mips16_ip if an explicit extension is used or by mips_ip if an
1965 explicit size is supplied. */
252b5132 1966
df58fc94 1967static unsigned int forced_insn_length;
252b5132 1968
e1b47bd5
RS
1969/* True if we are assembling an instruction. All dot symbols defined during
1970 this time should be treated as code labels. */
1971
1972static bfd_boolean mips_assembling_insn;
1973
ecb4347a
DJ
1974/* The pdr segment for per procedure frame/regmask info. Not used for
1975 ECOFF debugging. */
252b5132
RH
1976
1977static segT pdr_seg;
252b5132 1978
e013f690
TS
1979/* The default target format to use. */
1980
aeffff67
RS
1981#if defined (TE_FreeBSD)
1982#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1983#elif defined (TE_TMIPS)
1984#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1985#else
1986#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1987#endif
1988
e013f690 1989const char *
17a2f251 1990mips_target_format (void)
e013f690
TS
1991{
1992 switch (OUTPUT_FLAVOR)
1993 {
e013f690 1994 case bfd_target_elf_flavour:
0a44bf69
RS
1995#ifdef TE_VXWORKS
1996 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1997 return (target_big_endian
1998 ? "elf32-bigmips-vxworks"
1999 : "elf32-littlemips-vxworks");
2000#endif
e013f690 2001 return (target_big_endian
cfe86eaa 2002 ? (HAVE_64BIT_OBJECTS
aeffff67 2003 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2004 : (HAVE_NEWABI
aeffff67
RS
2005 ? ELF_TARGET ("elf32-n", "big")
2006 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2007 : (HAVE_64BIT_OBJECTS
aeffff67 2008 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2009 : (HAVE_NEWABI
aeffff67
RS
2010 ? ELF_TARGET ("elf32-n", "little")
2011 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2012 default:
2013 abort ();
2014 return NULL;
2015 }
2016}
2017
c6278170
RS
2018/* Return the ISA revision that is currently in use, or 0 if we are
2019 generating code for MIPS V or below. */
2020
2021static int
2022mips_isa_rev (void)
2023{
2024 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2025 return 2;
2026
ae52f483
AB
2027 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2028 return 3;
2029
2030 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2031 return 5;
2032
7361da2c
AB
2033 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2034 return 6;
2035
c6278170
RS
2036 /* microMIPS implies revision 2 or above. */
2037 if (mips_opts.micromips)
2038 return 2;
2039
2040 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2041 return 1;
2042
2043 return 0;
2044}
2045
2046/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2047
2048static unsigned int
2049mips_ase_mask (unsigned int flags)
2050{
2051 unsigned int i;
2052
2053 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2054 if (flags & mips_ase_groups[i])
2055 flags |= mips_ase_groups[i];
2056 return flags;
2057}
2058
2059/* Check whether the current ISA supports ASE. Issue a warning if
2060 appropriate. */
2061
2062static void
2063mips_check_isa_supports_ase (const struct mips_ase *ase)
2064{
2065 const char *base;
2066 int min_rev, size;
2067 static unsigned int warned_isa;
2068 static unsigned int warned_fp32;
2069
2070 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2071 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2072 else
2073 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2074 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2075 && (warned_isa & ase->flags) != ase->flags)
2076 {
2077 warned_isa |= ase->flags;
2078 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2079 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2080 if (min_rev < 0)
1661c76c 2081 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2082 " `%s' extension"), size, base, ase->name);
2083 else
1661c76c 2084 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2085 ase->name, base, size, min_rev);
2086 }
7361da2c
AB
2087 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2088 && (warned_isa & ase->flags) != ase->flags)
2089 {
2090 warned_isa |= ase->flags;
2091 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2092 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2093 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2094 ase->name, base, size, ase->rem_rev);
2095 }
2096
c6278170 2097 if ((ase->flags & FP64_ASES)
0b35dfee 2098 && mips_opts.fp != 64
c6278170
RS
2099 && (warned_fp32 & ase->flags) != ase->flags)
2100 {
2101 warned_fp32 |= ase->flags;
1661c76c 2102 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2103 }
2104}
2105
2106/* Check all enabled ASEs to see whether they are supported by the
2107 chosen architecture. */
2108
2109static void
2110mips_check_isa_supports_ases (void)
2111{
2112 unsigned int i, mask;
2113
2114 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2115 {
2116 mask = mips_ase_mask (mips_ases[i].flags);
2117 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2118 mips_check_isa_supports_ase (&mips_ases[i]);
2119 }
2120}
2121
2122/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2123 that were affected. */
2124
2125static unsigned int
919731af 2126mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2127 bfd_boolean enabled_p)
c6278170
RS
2128{
2129 unsigned int mask;
2130
2131 mask = mips_ase_mask (ase->flags);
919731af 2132 opts->ase &= ~mask;
92cebb3d
MR
2133
2134 /* Clear combination ASE flags, which need to be recalculated based on
2135 updated regular ASE settings. */
9785fc2a 2136 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2137
c6278170 2138 if (enabled_p)
919731af 2139 opts->ase |= ase->flags;
25499ac7 2140
9785fc2a
MR
2141 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2142 instructions which are only valid when both ASEs are enabled.
2143 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2144 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2145 {
2146 opts->ase |= ASE_XPA_VIRT;
2147 mask |= ASE_XPA_VIRT;
2148 }
25499ac7
MR
2149 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2150 {
2151 opts->ase |= ASE_MIPS16E2_MT;
2152 mask |= ASE_MIPS16E2_MT;
2153 }
2154
c6278170
RS
2155 return mask;
2156}
2157
2158/* Return the ASE called NAME, or null if none. */
2159
2160static const struct mips_ase *
2161mips_lookup_ase (const char *name)
2162{
2163 unsigned int i;
2164
2165 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2166 if (strcmp (name, mips_ases[i].name) == 0)
2167 return &mips_ases[i];
2168 return NULL;
2169}
2170
df58fc94 2171/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2172 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2173 otherwise it is a 32-bit instruction. */
df58fc94
RS
2174
2175static inline unsigned int
2176micromips_insn_length (const struct mips_opcode *mo)
2177{
7fd53920 2178 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2179}
2180
5c04167a
RS
2181/* Return the length of MIPS16 instruction OPCODE. */
2182
2183static inline unsigned int
2184mips16_opcode_length (unsigned long opcode)
2185{
2186 return (opcode >> 16) == 0 ? 2 : 4;
2187}
2188
1e915849
RS
2189/* Return the length of instruction INSN. */
2190
2191static inline unsigned int
2192insn_length (const struct mips_cl_insn *insn)
2193{
df58fc94
RS
2194 if (mips_opts.micromips)
2195 return micromips_insn_length (insn->insn_mo);
2196 else if (mips_opts.mips16)
5c04167a 2197 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2198 else
1e915849 2199 return 4;
1e915849
RS
2200}
2201
2202/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2203
2204static void
2205create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2206{
2207 size_t i;
2208
2209 insn->insn_mo = mo;
1e915849
RS
2210 insn->insn_opcode = mo->match;
2211 insn->frag = NULL;
2212 insn->where = 0;
2213 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2214 insn->fixp[i] = NULL;
2215 insn->fixed_p = (mips_opts.noreorder > 0);
2216 insn->noreorder_p = (mips_opts.noreorder > 0);
2217 insn->mips16_absolute_jump_p = 0;
15be625d 2218 insn->complete_p = 0;
e407c74b 2219 insn->cleared_p = 0;
1e915849
RS
2220}
2221
fc76e730
RS
2222/* Get a list of all the operands in INSN. */
2223
2224static const struct mips_operand_array *
2225insn_operands (const struct mips_cl_insn *insn)
2226{
2227 if (insn->insn_mo >= &mips_opcodes[0]
2228 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2229 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2230
2231 if (insn->insn_mo >= &mips16_opcodes[0]
2232 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2233 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2234
2235 if (insn->insn_mo >= &micromips_opcodes[0]
2236 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2237 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2238
2239 abort ();
2240}
2241
2242/* Get a description of operand OPNO of INSN. */
2243
2244static const struct mips_operand *
2245insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2246{
2247 const struct mips_operand_array *operands;
2248
2249 operands = insn_operands (insn);
2250 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2251 abort ();
2252 return operands->operand[opno];
2253}
2254
e077a1c8
RS
2255/* Install UVAL as the value of OPERAND in INSN. */
2256
2257static inline void
2258insn_insert_operand (struct mips_cl_insn *insn,
2259 const struct mips_operand *operand, unsigned int uval)
2260{
25499ac7
MR
2261 if (mips_opts.mips16
2262 && operand->type == OP_INT && operand->lsb == 0
2263 && mips_opcode_32bit_p (insn->insn_mo))
2264 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2265 else
2266 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2267}
2268
fc76e730
RS
2269/* Extract the value of OPERAND from INSN. */
2270
2271static inline unsigned
2272insn_extract_operand (const struct mips_cl_insn *insn,
2273 const struct mips_operand *operand)
2274{
2275 return mips_extract_operand (operand, insn->insn_opcode);
2276}
2277
df58fc94 2278/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2279
2280static void
df58fc94 2281mips_record_compressed_mode (void)
742a56fe
RS
2282{
2283 segment_info_type *si;
2284
2285 si = seg_info (now_seg);
2286 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2287 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2288 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2289 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2290}
2291
4d68580a
RS
2292/* Read a standard MIPS instruction from BUF. */
2293
2294static unsigned long
2295read_insn (char *buf)
2296{
2297 if (target_big_endian)
2298 return bfd_getb32 ((bfd_byte *) buf);
2299 else
2300 return bfd_getl32 ((bfd_byte *) buf);
2301}
2302
2303/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2304 the next byte. */
2305
2306static char *
2307write_insn (char *buf, unsigned int insn)
2308{
2309 md_number_to_chars (buf, insn, 4);
2310 return buf + 4;
2311}
2312
2313/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2314 has length LENGTH. */
2315
2316static unsigned long
2317read_compressed_insn (char *buf, unsigned int length)
2318{
2319 unsigned long insn;
2320 unsigned int i;
2321
2322 insn = 0;
2323 for (i = 0; i < length; i += 2)
2324 {
2325 insn <<= 16;
2326 if (target_big_endian)
2327 insn |= bfd_getb16 ((char *) buf);
2328 else
2329 insn |= bfd_getl16 ((char *) buf);
2330 buf += 2;
2331 }
2332 return insn;
2333}
2334
5c04167a
RS
2335/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2336 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2337
2338static char *
2339write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2340{
2341 unsigned int i;
2342
2343 for (i = 0; i < length; i += 2)
2344 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2345 return buf + length;
2346}
2347
1e915849
RS
2348/* Install INSN at the location specified by its "frag" and "where" fields. */
2349
2350static void
2351install_insn (const struct mips_cl_insn *insn)
2352{
2353 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2354 if (HAVE_CODE_COMPRESSION)
2355 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2356 else
4d68580a 2357 write_insn (f, insn->insn_opcode);
df58fc94 2358 mips_record_compressed_mode ();
1e915849
RS
2359}
2360
2361/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2362 and install the opcode in the new location. */
2363
2364static void
2365move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2366{
2367 size_t i;
2368
2369 insn->frag = frag;
2370 insn->where = where;
2371 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2372 if (insn->fixp[i] != NULL)
2373 {
2374 insn->fixp[i]->fx_frag = frag;
2375 insn->fixp[i]->fx_where = where;
2376 }
2377 install_insn (insn);
2378}
2379
2380/* Add INSN to the end of the output. */
2381
2382static void
2383add_fixed_insn (struct mips_cl_insn *insn)
2384{
2385 char *f = frag_more (insn_length (insn));
2386 move_insn (insn, frag_now, f - frag_now->fr_literal);
2387}
2388
2389/* Start a variant frag and move INSN to the start of the variant part,
2390 marking it as fixed. The other arguments are as for frag_var. */
2391
2392static void
2393add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2394 relax_substateT subtype, symbolS *symbol, offsetT offset)
2395{
2396 frag_grow (max_chars);
2397 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2398 insn->fixed_p = 1;
2399 frag_var (rs_machine_dependent, max_chars, var,
2400 subtype, symbol, offset, NULL);
2401}
2402
2403/* Insert N copies of INSN into the history buffer, starting at
2404 position FIRST. Neither FIRST nor N need to be clipped. */
2405
2406static void
2407insert_into_history (unsigned int first, unsigned int n,
2408 const struct mips_cl_insn *insn)
2409{
2410 if (mips_relax.sequence != 2)
2411 {
2412 unsigned int i;
2413
2414 for (i = ARRAY_SIZE (history); i-- > first;)
2415 if (i >= first + n)
2416 history[i] = history[i - n];
2417 else
2418 history[i] = *insn;
2419 }
2420}
2421
e3de51ce
RS
2422/* Clear the error in insn_error. */
2423
2424static void
2425clear_insn_error (void)
2426{
2427 memset (&insn_error, 0, sizeof (insn_error));
2428}
2429
2430/* Possibly record error message MSG for the current instruction.
2431 If the error is about a particular argument, ARGNUM is the 1-based
2432 number of that argument, otherwise it is 0. FORMAT is the format
2433 of MSG. Return true if MSG was used, false if the current message
2434 was kept. */
2435
2436static bfd_boolean
2437set_insn_error_format (int argnum, enum mips_insn_error_format format,
2438 const char *msg)
2439{
2440 if (argnum == 0)
2441 {
2442 /* Give priority to errors against specific arguments, and to
2443 the first whole-instruction message. */
2444 if (insn_error.msg)
2445 return FALSE;
2446 }
2447 else
2448 {
2449 /* Keep insn_error if it is against a later argument. */
2450 if (argnum < insn_error.min_argnum)
2451 return FALSE;
2452
2453 /* If both errors are against the same argument but are different,
2454 give up on reporting a specific error for this argument.
2455 See the comment about mips_insn_error for details. */
2456 if (argnum == insn_error.min_argnum
2457 && insn_error.msg
2458 && strcmp (insn_error.msg, msg) != 0)
2459 {
2460 insn_error.msg = 0;
2461 insn_error.min_argnum += 1;
2462 return FALSE;
2463 }
2464 }
2465 insn_error.min_argnum = argnum;
2466 insn_error.format = format;
2467 insn_error.msg = msg;
2468 return TRUE;
2469}
2470
2471/* Record an instruction error with no % format fields. ARGNUM and MSG are
2472 as for set_insn_error_format. */
2473
2474static void
2475set_insn_error (int argnum, const char *msg)
2476{
2477 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2478}
2479
2480/* Record an instruction error with one %d field I. ARGNUM and MSG are
2481 as for set_insn_error_format. */
2482
2483static void
2484set_insn_error_i (int argnum, const char *msg, int i)
2485{
2486 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2487 insn_error.u.i = i;
2488}
2489
2490/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2491 are as for set_insn_error_format. */
2492
2493static void
2494set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2495{
2496 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2497 {
2498 insn_error.u.ss[0] = s1;
2499 insn_error.u.ss[1] = s2;
2500 }
2501}
2502
2503/* Report the error in insn_error, which is against assembly code STR. */
2504
2505static void
2506report_insn_error (const char *str)
2507{
e1fa0163 2508 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2509
e3de51ce
RS
2510 switch (insn_error.format)
2511 {
2512 case ERR_FMT_PLAIN:
2513 as_bad (msg, str);
2514 break;
2515
2516 case ERR_FMT_I:
2517 as_bad (msg, insn_error.u.i, str);
2518 break;
2519
2520 case ERR_FMT_SS:
2521 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2522 break;
2523 }
e1fa0163
NC
2524
2525 free ((char *) msg);
e3de51ce
RS
2526}
2527
71400594
RS
2528/* Initialize vr4120_conflicts. There is a bit of duplication here:
2529 the idea is to make it obvious at a glance that each errata is
2530 included. */
2531
2532static void
2533init_vr4120_conflicts (void)
2534{
2535#define CONFLICT(FIRST, SECOND) \
2536 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2537
2538 /* Errata 21 - [D]DIV[U] after [D]MACC */
2539 CONFLICT (MACC, DIV);
2540 CONFLICT (DMACC, DIV);
2541
2542 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2543 CONFLICT (DMULT, DMULT);
2544 CONFLICT (DMULT, DMACC);
2545 CONFLICT (DMACC, DMULT);
2546 CONFLICT (DMACC, DMACC);
2547
2548 /* Errata 24 - MT{LO,HI} after [D]MACC */
2549 CONFLICT (MACC, MTHILO);
2550 CONFLICT (DMACC, MTHILO);
2551
2552 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2553 instruction is executed immediately after a MACC or DMACC
2554 instruction, the result of [either instruction] is incorrect." */
2555 CONFLICT (MACC, MULT);
2556 CONFLICT (MACC, DMULT);
2557 CONFLICT (DMACC, MULT);
2558 CONFLICT (DMACC, DMULT);
2559
2560 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2561 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2562 DDIV or DDIVU instruction, the result of the MACC or
2563 DMACC instruction is incorrect.". */
2564 CONFLICT (DMULT, MACC);
2565 CONFLICT (DMULT, DMACC);
2566 CONFLICT (DIV, MACC);
2567 CONFLICT (DIV, DMACC);
2568
2569#undef CONFLICT
2570}
2571
707bfff6
TS
2572struct regname {
2573 const char *name;
2574 unsigned int num;
2575};
2576
14daeee3 2577#define RNUM_MASK 0x00000ff
56d438b1 2578#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2579#define RTYPE_NUM 0x0000100
2580#define RTYPE_FPU 0x0000200
2581#define RTYPE_FCC 0x0000400
2582#define RTYPE_VEC 0x0000800
2583#define RTYPE_GP 0x0001000
2584#define RTYPE_CP0 0x0002000
2585#define RTYPE_PC 0x0004000
2586#define RTYPE_ACC 0x0008000
2587#define RTYPE_CCC 0x0010000
2588#define RTYPE_VI 0x0020000
2589#define RTYPE_VF 0x0040000
2590#define RTYPE_R5900_I 0x0080000
2591#define RTYPE_R5900_Q 0x0100000
2592#define RTYPE_R5900_R 0x0200000
2593#define RTYPE_R5900_ACC 0x0400000
56d438b1 2594#define RTYPE_MSA 0x0800000
14daeee3 2595#define RWARN 0x8000000
707bfff6
TS
2596
2597#define GENERIC_REGISTER_NUMBERS \
2598 {"$0", RTYPE_NUM | 0}, \
2599 {"$1", RTYPE_NUM | 1}, \
2600 {"$2", RTYPE_NUM | 2}, \
2601 {"$3", RTYPE_NUM | 3}, \
2602 {"$4", RTYPE_NUM | 4}, \
2603 {"$5", RTYPE_NUM | 5}, \
2604 {"$6", RTYPE_NUM | 6}, \
2605 {"$7", RTYPE_NUM | 7}, \
2606 {"$8", RTYPE_NUM | 8}, \
2607 {"$9", RTYPE_NUM | 9}, \
2608 {"$10", RTYPE_NUM | 10}, \
2609 {"$11", RTYPE_NUM | 11}, \
2610 {"$12", RTYPE_NUM | 12}, \
2611 {"$13", RTYPE_NUM | 13}, \
2612 {"$14", RTYPE_NUM | 14}, \
2613 {"$15", RTYPE_NUM | 15}, \
2614 {"$16", RTYPE_NUM | 16}, \
2615 {"$17", RTYPE_NUM | 17}, \
2616 {"$18", RTYPE_NUM | 18}, \
2617 {"$19", RTYPE_NUM | 19}, \
2618 {"$20", RTYPE_NUM | 20}, \
2619 {"$21", RTYPE_NUM | 21}, \
2620 {"$22", RTYPE_NUM | 22}, \
2621 {"$23", RTYPE_NUM | 23}, \
2622 {"$24", RTYPE_NUM | 24}, \
2623 {"$25", RTYPE_NUM | 25}, \
2624 {"$26", RTYPE_NUM | 26}, \
2625 {"$27", RTYPE_NUM | 27}, \
2626 {"$28", RTYPE_NUM | 28}, \
2627 {"$29", RTYPE_NUM | 29}, \
2628 {"$30", RTYPE_NUM | 30}, \
3739860c 2629 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2630
2631#define FPU_REGISTER_NAMES \
2632 {"$f0", RTYPE_FPU | 0}, \
2633 {"$f1", RTYPE_FPU | 1}, \
2634 {"$f2", RTYPE_FPU | 2}, \
2635 {"$f3", RTYPE_FPU | 3}, \
2636 {"$f4", RTYPE_FPU | 4}, \
2637 {"$f5", RTYPE_FPU | 5}, \
2638 {"$f6", RTYPE_FPU | 6}, \
2639 {"$f7", RTYPE_FPU | 7}, \
2640 {"$f8", RTYPE_FPU | 8}, \
2641 {"$f9", RTYPE_FPU | 9}, \
2642 {"$f10", RTYPE_FPU | 10}, \
2643 {"$f11", RTYPE_FPU | 11}, \
2644 {"$f12", RTYPE_FPU | 12}, \
2645 {"$f13", RTYPE_FPU | 13}, \
2646 {"$f14", RTYPE_FPU | 14}, \
2647 {"$f15", RTYPE_FPU | 15}, \
2648 {"$f16", RTYPE_FPU | 16}, \
2649 {"$f17", RTYPE_FPU | 17}, \
2650 {"$f18", RTYPE_FPU | 18}, \
2651 {"$f19", RTYPE_FPU | 19}, \
2652 {"$f20", RTYPE_FPU | 20}, \
2653 {"$f21", RTYPE_FPU | 21}, \
2654 {"$f22", RTYPE_FPU | 22}, \
2655 {"$f23", RTYPE_FPU | 23}, \
2656 {"$f24", RTYPE_FPU | 24}, \
2657 {"$f25", RTYPE_FPU | 25}, \
2658 {"$f26", RTYPE_FPU | 26}, \
2659 {"$f27", RTYPE_FPU | 27}, \
2660 {"$f28", RTYPE_FPU | 28}, \
2661 {"$f29", RTYPE_FPU | 29}, \
2662 {"$f30", RTYPE_FPU | 30}, \
2663 {"$f31", RTYPE_FPU | 31}
2664
2665#define FPU_CONDITION_CODE_NAMES \
2666 {"$fcc0", RTYPE_FCC | 0}, \
2667 {"$fcc1", RTYPE_FCC | 1}, \
2668 {"$fcc2", RTYPE_FCC | 2}, \
2669 {"$fcc3", RTYPE_FCC | 3}, \
2670 {"$fcc4", RTYPE_FCC | 4}, \
2671 {"$fcc5", RTYPE_FCC | 5}, \
2672 {"$fcc6", RTYPE_FCC | 6}, \
2673 {"$fcc7", RTYPE_FCC | 7}
2674
2675#define COPROC_CONDITION_CODE_NAMES \
2676 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2677 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2678 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2679 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2680 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2681 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2682 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2683 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2684
2685#define N32N64_SYMBOLIC_REGISTER_NAMES \
2686 {"$a4", RTYPE_GP | 8}, \
2687 {"$a5", RTYPE_GP | 9}, \
2688 {"$a6", RTYPE_GP | 10}, \
2689 {"$a7", RTYPE_GP | 11}, \
2690 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2691 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2692 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2693 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2694 {"$t0", RTYPE_GP | 12}, \
2695 {"$t1", RTYPE_GP | 13}, \
2696 {"$t2", RTYPE_GP | 14}, \
2697 {"$t3", RTYPE_GP | 15}
2698
2699#define O32_SYMBOLIC_REGISTER_NAMES \
2700 {"$t0", RTYPE_GP | 8}, \
2701 {"$t1", RTYPE_GP | 9}, \
2702 {"$t2", RTYPE_GP | 10}, \
2703 {"$t3", RTYPE_GP | 11}, \
2704 {"$t4", RTYPE_GP | 12}, \
2705 {"$t5", RTYPE_GP | 13}, \
2706 {"$t6", RTYPE_GP | 14}, \
2707 {"$t7", RTYPE_GP | 15}, \
2708 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2709 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2710 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2711 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2712
2713/* Remaining symbolic register names */
2714#define SYMBOLIC_REGISTER_NAMES \
2715 {"$zero", RTYPE_GP | 0}, \
2716 {"$at", RTYPE_GP | 1}, \
2717 {"$AT", RTYPE_GP | 1}, \
2718 {"$v0", RTYPE_GP | 2}, \
2719 {"$v1", RTYPE_GP | 3}, \
2720 {"$a0", RTYPE_GP | 4}, \
2721 {"$a1", RTYPE_GP | 5}, \
2722 {"$a2", RTYPE_GP | 6}, \
2723 {"$a3", RTYPE_GP | 7}, \
2724 {"$s0", RTYPE_GP | 16}, \
2725 {"$s1", RTYPE_GP | 17}, \
2726 {"$s2", RTYPE_GP | 18}, \
2727 {"$s3", RTYPE_GP | 19}, \
2728 {"$s4", RTYPE_GP | 20}, \
2729 {"$s5", RTYPE_GP | 21}, \
2730 {"$s6", RTYPE_GP | 22}, \
2731 {"$s7", RTYPE_GP | 23}, \
2732 {"$t8", RTYPE_GP | 24}, \
2733 {"$t9", RTYPE_GP | 25}, \
2734 {"$k0", RTYPE_GP | 26}, \
2735 {"$kt0", RTYPE_GP | 26}, \
2736 {"$k1", RTYPE_GP | 27}, \
2737 {"$kt1", RTYPE_GP | 27}, \
2738 {"$gp", RTYPE_GP | 28}, \
2739 {"$sp", RTYPE_GP | 29}, \
2740 {"$s8", RTYPE_GP | 30}, \
2741 {"$fp", RTYPE_GP | 30}, \
2742 {"$ra", RTYPE_GP | 31}
2743
2744#define MIPS16_SPECIAL_REGISTER_NAMES \
2745 {"$pc", RTYPE_PC | 0}
2746
2747#define MDMX_VECTOR_REGISTER_NAMES \
2748 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2749 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2750 {"$v2", RTYPE_VEC | 2}, \
2751 {"$v3", RTYPE_VEC | 3}, \
2752 {"$v4", RTYPE_VEC | 4}, \
2753 {"$v5", RTYPE_VEC | 5}, \
2754 {"$v6", RTYPE_VEC | 6}, \
2755 {"$v7", RTYPE_VEC | 7}, \
2756 {"$v8", RTYPE_VEC | 8}, \
2757 {"$v9", RTYPE_VEC | 9}, \
2758 {"$v10", RTYPE_VEC | 10}, \
2759 {"$v11", RTYPE_VEC | 11}, \
2760 {"$v12", RTYPE_VEC | 12}, \
2761 {"$v13", RTYPE_VEC | 13}, \
2762 {"$v14", RTYPE_VEC | 14}, \
2763 {"$v15", RTYPE_VEC | 15}, \
2764 {"$v16", RTYPE_VEC | 16}, \
2765 {"$v17", RTYPE_VEC | 17}, \
2766 {"$v18", RTYPE_VEC | 18}, \
2767 {"$v19", RTYPE_VEC | 19}, \
2768 {"$v20", RTYPE_VEC | 20}, \
2769 {"$v21", RTYPE_VEC | 21}, \
2770 {"$v22", RTYPE_VEC | 22}, \
2771 {"$v23", RTYPE_VEC | 23}, \
2772 {"$v24", RTYPE_VEC | 24}, \
2773 {"$v25", RTYPE_VEC | 25}, \
2774 {"$v26", RTYPE_VEC | 26}, \
2775 {"$v27", RTYPE_VEC | 27}, \
2776 {"$v28", RTYPE_VEC | 28}, \
2777 {"$v29", RTYPE_VEC | 29}, \
2778 {"$v30", RTYPE_VEC | 30}, \
2779 {"$v31", RTYPE_VEC | 31}
2780
14daeee3
RS
2781#define R5900_I_NAMES \
2782 {"$I", RTYPE_R5900_I | 0}
2783
2784#define R5900_Q_NAMES \
2785 {"$Q", RTYPE_R5900_Q | 0}
2786
2787#define R5900_R_NAMES \
2788 {"$R", RTYPE_R5900_R | 0}
2789
2790#define R5900_ACC_NAMES \
2791 {"$ACC", RTYPE_R5900_ACC | 0 }
2792
707bfff6
TS
2793#define MIPS_DSP_ACCUMULATOR_NAMES \
2794 {"$ac0", RTYPE_ACC | 0}, \
2795 {"$ac1", RTYPE_ACC | 1}, \
2796 {"$ac2", RTYPE_ACC | 2}, \
2797 {"$ac3", RTYPE_ACC | 3}
2798
2799static const struct regname reg_names[] = {
2800 GENERIC_REGISTER_NUMBERS,
2801 FPU_REGISTER_NAMES,
2802 FPU_CONDITION_CODE_NAMES,
2803 COPROC_CONDITION_CODE_NAMES,
2804
2805 /* The $txx registers depends on the abi,
2806 these will be added later into the symbol table from
3739860c 2807 one of the tables below once mips_abi is set after
707bfff6
TS
2808 parsing of arguments from the command line. */
2809 SYMBOLIC_REGISTER_NAMES,
2810
2811 MIPS16_SPECIAL_REGISTER_NAMES,
2812 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2813 R5900_I_NAMES,
2814 R5900_Q_NAMES,
2815 R5900_R_NAMES,
2816 R5900_ACC_NAMES,
707bfff6
TS
2817 MIPS_DSP_ACCUMULATOR_NAMES,
2818 {0, 0}
2819};
2820
2821static const struct regname reg_names_o32[] = {
2822 O32_SYMBOLIC_REGISTER_NAMES,
2823 {0, 0}
2824};
2825
2826static const struct regname reg_names_n32n64[] = {
2827 N32N64_SYMBOLIC_REGISTER_NAMES,
2828 {0, 0}
2829};
2830
a92713e6
RS
2831/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2832 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2833 of these register symbols, return the associated vector register,
2834 otherwise return SYMVAL itself. */
df58fc94 2835
a92713e6
RS
2836static unsigned int
2837mips_prefer_vec_regno (unsigned int symval)
707bfff6 2838{
a92713e6
RS
2839 if ((symval & -2) == (RTYPE_GP | 2))
2840 return RTYPE_VEC | (symval & 1);
2841 return symval;
2842}
2843
14daeee3
RS
2844/* Return true if string [S, E) is a valid register name, storing its
2845 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2846
2847static bfd_boolean
14daeee3 2848mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2849{
707bfff6 2850 char save_c;
14daeee3 2851 symbolS *symbol;
707bfff6
TS
2852
2853 /* Terminate name. */
2854 save_c = *e;
2855 *e = '\0';
2856
a92713e6
RS
2857 /* Look up the name. */
2858 symbol = symbol_find (s);
2859 *e = save_c;
2860
2861 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2862 return FALSE;
2863
14daeee3
RS
2864 *symval_ptr = S_GET_VALUE (symbol);
2865 return TRUE;
2866}
2867
2868/* Return true if the string at *SPTR is a valid register name. Allow it
2869 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2870 is nonnull.
2871
2872 When returning true, move *SPTR past the register, store the
2873 register's symbol value in *SYMVAL_PTR and the channel mask in
2874 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2875 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2876 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2877
2878static bfd_boolean
2879mips_parse_register (char **sptr, unsigned int *symval_ptr,
2880 unsigned int *channels_ptr)
2881{
2882 char *s, *e, *m;
2883 const char *q;
2884 unsigned int channels, symval, bit;
2885
2886 /* Find end of name. */
2887 s = e = *sptr;
2888 if (is_name_beginner (*e))
2889 ++e;
2890 while (is_part_of_name (*e))
2891 ++e;
2892
2893 channels = 0;
2894 if (!mips_parse_register_1 (s, e, &symval))
2895 {
2896 if (!channels_ptr)
2897 return FALSE;
2898
2899 /* Eat characters from the end of the string that are valid
2900 channel suffixes. The preceding register must be $ACC or
2901 end with a digit, so there is no ambiguity. */
2902 bit = 1;
2903 m = e;
2904 for (q = "wzyx"; *q; q++, bit <<= 1)
2905 if (m > s && m[-1] == *q)
2906 {
2907 --m;
2908 channels |= bit;
2909 }
2910
2911 if (channels == 0
2912 || !mips_parse_register_1 (s, m, &symval)
2913 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2914 return FALSE;
2915 }
2916
a92713e6 2917 *sptr = e;
14daeee3
RS
2918 *symval_ptr = symval;
2919 if (channels_ptr)
2920 *channels_ptr = channels;
a92713e6
RS
2921 return TRUE;
2922}
2923
2924/* Check if SPTR points at a valid register specifier according to TYPES.
2925 If so, then return 1, advance S to consume the specifier and store
2926 the register's number in REGNOP, otherwise return 0. */
2927
2928static int
2929reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2930{
2931 unsigned int regno;
2932
14daeee3 2933 if (mips_parse_register (s, &regno, NULL))
707bfff6 2934 {
a92713e6
RS
2935 if (types & RTYPE_VEC)
2936 regno = mips_prefer_vec_regno (regno);
2937 if (regno & types)
2938 regno &= RNUM_MASK;
2939 else
2940 regno = ~0;
707bfff6 2941 }
a92713e6 2942 else
707bfff6 2943 {
a92713e6 2944 if (types & RWARN)
1661c76c 2945 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2946 regno = ~0;
707bfff6 2947 }
707bfff6 2948 if (regnop)
a92713e6
RS
2949 *regnop = regno;
2950 return regno <= RNUM_MASK;
707bfff6
TS
2951}
2952
14daeee3
RS
2953/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2954 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2955
2956static char *
2957mips_parse_vu0_channels (char *s, unsigned int *channels)
2958{
2959 unsigned int i;
2960
2961 *channels = 0;
2962 for (i = 0; i < 4; i++)
2963 if (*s == "xyzw"[i])
2964 {
2965 *channels |= 1 << (3 - i);
2966 ++s;
2967 }
2968 return s;
2969}
2970
a92713e6
RS
2971/* Token types for parsed operand lists. */
2972enum mips_operand_token_type {
2973 /* A plain register, e.g. $f2. */
2974 OT_REG,
df58fc94 2975
14daeee3
RS
2976 /* A 4-bit XYZW channel mask. */
2977 OT_CHANNELS,
2978
56d438b1
CF
2979 /* A constant vector index, e.g. [1]. */
2980 OT_INTEGER_INDEX,
2981
2982 /* A register vector index, e.g. [$2]. */
2983 OT_REG_INDEX,
df58fc94 2984
a92713e6
RS
2985 /* A continuous range of registers, e.g. $s0-$s4. */
2986 OT_REG_RANGE,
2987
2988 /* A (possibly relocated) expression. */
2989 OT_INTEGER,
2990
2991 /* A floating-point value. */
2992 OT_FLOAT,
2993
2994 /* A single character. This can be '(', ')' or ',', but '(' only appears
2995 before OT_REGs. */
2996 OT_CHAR,
2997
14daeee3
RS
2998 /* A doubled character, either "--" or "++". */
2999 OT_DOUBLE_CHAR,
3000
a92713e6
RS
3001 /* The end of the operand list. */
3002 OT_END
3003};
3004
3005/* A parsed operand token. */
3006struct mips_operand_token
3007{
3008 /* The type of token. */
3009 enum mips_operand_token_type type;
3010 union
3011 {
56d438b1 3012 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3013 unsigned int regno;
3014
14daeee3
RS
3015 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3016 unsigned int channels;
3017
56d438b1
CF
3018 /* The integer value of an OT_INTEGER_INDEX. */
3019 addressT index;
a92713e6
RS
3020
3021 /* The two register symbol values involved in an OT_REG_RANGE. */
3022 struct {
3023 unsigned int regno1;
3024 unsigned int regno2;
3025 } reg_range;
3026
3027 /* The value of an OT_INTEGER. The value is represented as an
3028 expression and the relocation operators that were applied to
3029 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3030 relocation operators were used. */
3031 struct {
3032 expressionS value;
3033 bfd_reloc_code_real_type relocs[3];
3034 } integer;
3035
3036 /* The binary data for an OT_FLOAT constant, and the number of bytes
3037 in the constant. */
3038 struct {
3039 unsigned char data[8];
3040 int length;
3041 } flt;
3042
14daeee3 3043 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3044 char ch;
3045 } u;
3046};
3047
3048/* An obstack used to construct lists of mips_operand_tokens. */
3049static struct obstack mips_operand_tokens;
3050
3051/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3052
3053static void
3054mips_add_token (struct mips_operand_token *token,
3055 enum mips_operand_token_type type)
3056{
3057 token->type = type;
3058 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3059}
3060
3061/* Check whether S is '(' followed by a register name. Add OT_CHAR
3062 and OT_REG tokens for them if so, and return a pointer to the first
3063 unconsumed character. Return null otherwise. */
3064
3065static char *
3066mips_parse_base_start (char *s)
3067{
3068 struct mips_operand_token token;
14daeee3
RS
3069 unsigned int regno, channels;
3070 bfd_boolean decrement_p;
df58fc94 3071
a92713e6
RS
3072 if (*s != '(')
3073 return 0;
3074
3075 ++s;
3076 SKIP_SPACE_TABS (s);
14daeee3
RS
3077
3078 /* Only match "--" as part of a base expression. In other contexts "--X"
3079 is a double negative. */
3080 decrement_p = (s[0] == '-' && s[1] == '-');
3081 if (decrement_p)
3082 {
3083 s += 2;
3084 SKIP_SPACE_TABS (s);
3085 }
3086
3087 /* Allow a channel specifier because that leads to better error messages
3088 than treating something like "$vf0x++" as an expression. */
3089 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3090 return 0;
3091
3092 token.u.ch = '(';
3093 mips_add_token (&token, OT_CHAR);
3094
14daeee3
RS
3095 if (decrement_p)
3096 {
3097 token.u.ch = '-';
3098 mips_add_token (&token, OT_DOUBLE_CHAR);
3099 }
3100
a92713e6
RS
3101 token.u.regno = regno;
3102 mips_add_token (&token, OT_REG);
3103
14daeee3
RS
3104 if (channels)
3105 {
3106 token.u.channels = channels;
3107 mips_add_token (&token, OT_CHANNELS);
3108 }
3109
3110 /* For consistency, only match "++" as part of base expressions too. */
3111 SKIP_SPACE_TABS (s);
3112 if (s[0] == '+' && s[1] == '+')
3113 {
3114 s += 2;
3115 token.u.ch = '+';
3116 mips_add_token (&token, OT_DOUBLE_CHAR);
3117 }
3118
a92713e6
RS
3119 return s;
3120}
3121
3122/* Parse one or more tokens from S. Return a pointer to the first
3123 unconsumed character on success. Return null if an error was found
3124 and store the error text in insn_error. FLOAT_FORMAT is as for
3125 mips_parse_arguments. */
3126
3127static char *
3128mips_parse_argument_token (char *s, char float_format)
3129{
6d4af3c2
AM
3130 char *end, *save_in;
3131 const char *err;
14daeee3 3132 unsigned int regno1, regno2, channels;
a92713e6
RS
3133 struct mips_operand_token token;
3134
3135 /* First look for "($reg", since we want to treat that as an
3136 OT_CHAR and OT_REG rather than an expression. */
3137 end = mips_parse_base_start (s);
3138 if (end)
3139 return end;
3140
3141 /* Handle other characters that end up as OT_CHARs. */
3142 if (*s == ')' || *s == ',')
3143 {
3144 token.u.ch = *s;
3145 mips_add_token (&token, OT_CHAR);
3146 ++s;
3147 return s;
3148 }
3149
3150 /* Handle tokens that start with a register. */
14daeee3 3151 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3152 {
14daeee3
RS
3153 if (channels)
3154 {
3155 /* A register and a VU0 channel suffix. */
3156 token.u.regno = regno1;
3157 mips_add_token (&token, OT_REG);
3158
3159 token.u.channels = channels;
3160 mips_add_token (&token, OT_CHANNELS);
3161 return s;
3162 }
3163
a92713e6
RS
3164 SKIP_SPACE_TABS (s);
3165 if (*s == '-')
df58fc94 3166 {
a92713e6
RS
3167 /* A register range. */
3168 ++s;
3169 SKIP_SPACE_TABS (s);
14daeee3 3170 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3171 {
1661c76c 3172 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3173 return 0;
3174 }
df58fc94 3175
a92713e6
RS
3176 token.u.reg_range.regno1 = regno1;
3177 token.u.reg_range.regno2 = regno2;
3178 mips_add_token (&token, OT_REG_RANGE);
3179 return s;
3180 }
a92713e6 3181
56d438b1
CF
3182 /* Add the register itself. */
3183 token.u.regno = regno1;
3184 mips_add_token (&token, OT_REG);
3185
3186 /* Check for a vector index. */
3187 if (*s == '[')
3188 {
a92713e6
RS
3189 ++s;
3190 SKIP_SPACE_TABS (s);
56d438b1
CF
3191 if (mips_parse_register (&s, &token.u.regno, NULL))
3192 mips_add_token (&token, OT_REG_INDEX);
3193 else
a92713e6 3194 {
56d438b1
CF
3195 expressionS element;
3196
3197 my_getExpression (&element, s);
3198 if (element.X_op != O_constant)
3199 {
3200 set_insn_error (0, _("vector element must be constant"));
3201 return 0;
3202 }
3203 s = expr_end;
3204 token.u.index = element.X_add_number;
3205 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3206 }
a92713e6
RS
3207 SKIP_SPACE_TABS (s);
3208 if (*s != ']')
3209 {
1661c76c 3210 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3211 return 0;
3212 }
3213 ++s;
df58fc94 3214 }
a92713e6 3215 return s;
df58fc94
RS
3216 }
3217
a92713e6
RS
3218 if (float_format)
3219 {
3220 /* First try to treat expressions as floats. */
3221 save_in = input_line_pointer;
3222 input_line_pointer = s;
3223 err = md_atof (float_format, (char *) token.u.flt.data,
3224 &token.u.flt.length);
3225 end = input_line_pointer;
3226 input_line_pointer = save_in;
3227 if (err && *err)
3228 {
e3de51ce 3229 set_insn_error (0, err);
a92713e6
RS
3230 return 0;
3231 }
3232 if (s != end)
3233 {
3234 mips_add_token (&token, OT_FLOAT);
3235 return end;
3236 }
3237 }
3238
3239 /* Treat everything else as an integer expression. */
3240 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3241 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3242 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3243 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3244 s = expr_end;
3245 mips_add_token (&token, OT_INTEGER);
3246 return s;
3247}
3248
3249/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3250 if expressions should be treated as 32-bit floating-point constants,
3251 'd' if they should be treated as 64-bit floating-point constants,
3252 or 0 if they should be treated as integer expressions (the usual case).
3253
3254 Return a list of tokens on success, otherwise return 0. The caller
3255 must obstack_free the list after use. */
3256
3257static struct mips_operand_token *
3258mips_parse_arguments (char *s, char float_format)
3259{
3260 struct mips_operand_token token;
3261
3262 SKIP_SPACE_TABS (s);
3263 while (*s)
3264 {
3265 s = mips_parse_argument_token (s, float_format);
3266 if (!s)
3267 {
3268 obstack_free (&mips_operand_tokens,
3269 obstack_finish (&mips_operand_tokens));
3270 return 0;
3271 }
3272 SKIP_SPACE_TABS (s);
3273 }
3274 mips_add_token (&token, OT_END);
3275 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3276}
3277
d301a56b
RS
3278/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3279 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3280
3281static bfd_boolean
f79e2745 3282is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3283{
3284 int isa = mips_opts.isa;
846ef2d0 3285 int ase = mips_opts.ase;
037b32b9 3286 int fp_s, fp_d;
c6278170 3287 unsigned int i;
037b32b9 3288
be0fcbee 3289 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3290 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3291 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3292 ase |= mips_ases[i].flags64;
037b32b9 3293
d301a56b 3294 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3295 return FALSE;
3296
3297 /* Check whether the instruction or macro requires single-precision or
3298 double-precision floating-point support. Note that this information is
3299 stored differently in the opcode table for insns and macros. */
3300 if (mo->pinfo == INSN_MACRO)
3301 {
3302 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3303 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3304 }
3305 else
3306 {
3307 fp_s = mo->pinfo & FP_S;
3308 fp_d = mo->pinfo & FP_D;
3309 }
3310
3311 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3312 return FALSE;
3313
3314 if (fp_s && mips_opts.soft_float)
3315 return FALSE;
3316
3317 return TRUE;
3318}
3319
3320/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3321 selected ISA and architecture. */
3322
3323static bfd_boolean
3324is_opcode_valid_16 (const struct mips_opcode *mo)
3325{
25499ac7
MR
3326 int isa = mips_opts.isa;
3327 int ase = mips_opts.ase;
3328 unsigned int i;
3329
3330 if (ISA_HAS_64BIT_REGS (isa))
3331 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3332 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3333 ase |= mips_ases[i].flags64;
3334
3335 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3336}
3337
df58fc94 3338/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3339 explicitly requested. Always TRUE in the standard MIPS mode.
3340 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3341
3342static bfd_boolean
3343is_size_valid (const struct mips_opcode *mo)
3344{
3345 if (!mips_opts.micromips)
3346 return TRUE;
3347
833794fc
MR
3348 if (mips_opts.insn32)
3349 {
3350 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3351 return FALSE;
3352 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3353 return FALSE;
3354 }
df58fc94
RS
3355 if (!forced_insn_length)
3356 return TRUE;
3357 if (mo->pinfo == INSN_MACRO)
3358 return FALSE;
3359 return forced_insn_length == micromips_insn_length (mo);
3360}
3361
7fd53920
MR
3362/* Return TRUE if the size of the MIPS16 opcode MO matches one
3363 explicitly requested. */
3364
3365static bfd_boolean
3366is_size_valid_16 (const struct mips_opcode *mo)
3367{
3368 if (!forced_insn_length)
3369 return TRUE;
3370 if (mo->pinfo == INSN_MACRO)
3371 return FALSE;
3372 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3373 return FALSE;
0674ee5d
MR
3374 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3375 return FALSE;
7fd53920
MR
3376 return TRUE;
3377}
3378
df58fc94 3379/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3380 of the preceding instruction. Always TRUE in the standard MIPS mode.
3381
3382 We don't accept macros in 16-bit delay slots to avoid a case where
3383 a macro expansion fails because it relies on a preceding 32-bit real
3384 instruction to have matched and does not handle the operands correctly.
3385 The only macros that may expand to 16-bit instructions are JAL that
3386 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3387 and BGT (that likewise cannot be placed in a delay slot) that decay to
3388 a NOP. In all these cases the macros precede any corresponding real
3389 instruction definitions in the opcode table, so they will match in the
3390 second pass where the size of the delay slot is ignored and therefore
3391 produce correct code. */
df58fc94
RS
3392
3393static bfd_boolean
3394is_delay_slot_valid (const struct mips_opcode *mo)
3395{
3396 if (!mips_opts.micromips)
3397 return TRUE;
3398
3399 if (mo->pinfo == INSN_MACRO)
c06dec14 3400 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3401 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3402 && micromips_insn_length (mo) != 4)
3403 return FALSE;
3404 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3405 && micromips_insn_length (mo) != 2)
3406 return FALSE;
3407
3408 return TRUE;
3409}
3410
fc76e730
RS
3411/* For consistency checking, verify that all bits of OPCODE are specified
3412 either by the match/mask part of the instruction definition, or by the
3413 operand list. Also build up a list of operands in OPERANDS.
3414
3415 INSN_BITS says which bits of the instruction are significant.
3416 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3417 provides the mips_operand description of each operand. DECODE_OPERAND
3418 is null for MIPS16 instructions. */
ab902481
RS
3419
3420static int
3421validate_mips_insn (const struct mips_opcode *opcode,
3422 unsigned long insn_bits,
fc76e730
RS
3423 const struct mips_operand *(*decode_operand) (const char *),
3424 struct mips_operand_array *operands)
ab902481
RS
3425{
3426 const char *s;
fc76e730 3427 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3428 const struct mips_operand *operand;
3429
fc76e730
RS
3430 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3431 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3432 {
3433 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3434 opcode->name, opcode->args);
3435 return 0;
3436 }
3437 used_bits = 0;
fc76e730 3438 opno = 0;
14daeee3
RS
3439 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3440 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3441 for (s = opcode->args; *s; ++s)
3442 switch (*s)
3443 {
3444 case ',':
3445 case '(':
3446 case ')':
3447 break;
3448
14daeee3
RS
3449 case '#':
3450 s++;
3451 break;
3452
ab902481 3453 default:
fc76e730 3454 if (!decode_operand)
7fd53920 3455 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3456 else
3457 operand = decode_operand (s);
3458 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3459 {
3460 as_bad (_("internal: unknown operand type: %s %s"),
3461 opcode->name, opcode->args);
3462 return 0;
3463 }
fc76e730
RS
3464 gas_assert (opno < MAX_OPERANDS);
3465 operands->operand[opno] = operand;
25499ac7
MR
3466 if (!decode_operand && operand
3467 && operand->type == OP_INT && operand->lsb == 0
3468 && mips_opcode_32bit_p (opcode))
3469 used_bits |= mips16_immed_extend (-1, operand->size);
3470 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3471 {
14daeee3 3472 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3473 if (operand->type == OP_MDMX_IMM_REG)
3474 /* Bit 5 is the format selector (OB vs QH). The opcode table
3475 has separate entries for each format. */
3476 used_bits &= ~(1 << (operand->lsb + 5));
3477 if (operand->type == OP_ENTRY_EXIT_LIST)
3478 used_bits &= ~(mask & 0x700);
38bf472a
MR
3479 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3480 operand field that cannot be fully described with LSB/SIZE. */
3481 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3482 used_bits &= ~0x6000;
fc76e730 3483 }
ab902481 3484 /* Skip prefix characters. */
7361da2c 3485 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3486 ++s;
fc76e730 3487 opno += 1;
ab902481
RS
3488 break;
3489 }
fc76e730 3490 doubled = used_bits & mask & insn_bits;
ab902481
RS
3491 if (doubled)
3492 {
3493 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3494 " %s %s"), doubled, opcode->name, opcode->args);
3495 return 0;
3496 }
fc76e730 3497 used_bits |= mask;
ab902481 3498 undefined = ~used_bits & insn_bits;
fc76e730 3499 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3500 {
3501 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3502 undefined, opcode->name, opcode->args);
3503 return 0;
3504 }
3505 used_bits &= ~insn_bits;
3506 if (used_bits)
3507 {
3508 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3509 used_bits, opcode->name, opcode->args);
3510 return 0;
3511 }
3512 return 1;
3513}
3514
fc76e730
RS
3515/* The MIPS16 version of validate_mips_insn. */
3516
3517static int
3518validate_mips16_insn (const struct mips_opcode *opcode,
3519 struct mips_operand_array *operands)
3520{
7fd53920 3521 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3522
7fd53920 3523 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3524}
3525
ab902481
RS
3526/* The microMIPS version of validate_mips_insn. */
3527
3528static int
fc76e730
RS
3529validate_micromips_insn (const struct mips_opcode *opc,
3530 struct mips_operand_array *operands)
ab902481
RS
3531{
3532 unsigned long insn_bits;
3533 unsigned long major;
3534 unsigned int length;
3535
fc76e730
RS
3536 if (opc->pinfo == INSN_MACRO)
3537 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3538 operands);
3539
ab902481
RS
3540 length = micromips_insn_length (opc);
3541 if (length != 2 && length != 4)
3542 {
1661c76c 3543 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3544 "%s %s"), length, opc->name, opc->args);
3545 return 0;
3546 }
3547 major = opc->match >> (10 + 8 * (length - 2));
3548 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3549 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3550 {
1661c76c 3551 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3552 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3553 return 0;
3554 }
3555
3556 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3557 insn_bits = 1 << 4 * length;
3558 insn_bits <<= 4 * length;
3559 insn_bits -= 1;
fc76e730
RS
3560 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3561 operands);
ab902481
RS
3562}
3563
707bfff6
TS
3564/* This function is called once, at assembler startup time. It should set up
3565 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3566
252b5132 3567void
17a2f251 3568md_begin (void)
252b5132 3569{
3994f87e 3570 const char *retval = NULL;
156c2f8b 3571 int i = 0;
252b5132 3572 int broken = 0;
1f25f5d3 3573
0a44bf69
RS
3574 if (mips_pic != NO_PIC)
3575 {
3576 if (g_switch_seen && g_switch_value != 0)
3577 as_bad (_("-G may not be used in position-independent code"));
3578 g_switch_value = 0;
3579 }
00acd688
CM
3580 else if (mips_abicalls)
3581 {
3582 if (g_switch_seen && g_switch_value != 0)
3583 as_bad (_("-G may not be used with abicalls"));
3584 g_switch_value = 0;
3585 }
0a44bf69 3586
0b35dfee 3587 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3588 as_warn (_("could not set architecture and machine"));
252b5132 3589
252b5132
RH
3590 op_hash = hash_new ();
3591
fc76e730 3592 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3593 for (i = 0; i < NUMOPCODES;)
3594 {
3595 const char *name = mips_opcodes[i].name;
3596
17a2f251 3597 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3598 if (retval != NULL)
3599 {
3600 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3601 mips_opcodes[i].name, retval);
3602 /* Probably a memory allocation problem? Give up now. */
1661c76c 3603 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3604 }
3605 do
3606 {
fc76e730
RS
3607 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3608 decode_mips_operand, &mips_operands[i]))
3609 broken = 1;
3610 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3611 {
fc76e730
RS
3612 create_insn (&nop_insn, mips_opcodes + i);
3613 if (mips_fix_loongson2f_nop)
3614 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3615 nop_insn.fixed_p = 1;
252b5132
RH
3616 }
3617 ++i;
3618 }
3619 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3620 }
3621
3622 mips16_op_hash = hash_new ();
fc76e730
RS
3623 mips16_operands = XCNEWVEC (struct mips_operand_array,
3624 bfd_mips16_num_opcodes);
252b5132
RH
3625
3626 i = 0;
3627 while (i < bfd_mips16_num_opcodes)
3628 {
3629 const char *name = mips16_opcodes[i].name;
3630
17a2f251 3631 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3632 if (retval != NULL)
3633 as_fatal (_("internal: can't hash `%s': %s"),
3634 mips16_opcodes[i].name, retval);
3635 do
3636 {
fc76e730
RS
3637 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3638 broken = 1;
1e915849
RS
3639 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3640 {
3641 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3642 mips16_nop_insn.fixed_p = 1;
3643 }
252b5132
RH
3644 ++i;
3645 }
3646 while (i < bfd_mips16_num_opcodes
3647 && strcmp (mips16_opcodes[i].name, name) == 0);
3648 }
3649
df58fc94 3650 micromips_op_hash = hash_new ();
fc76e730
RS
3651 micromips_operands = XCNEWVEC (struct mips_operand_array,
3652 bfd_micromips_num_opcodes);
df58fc94
RS
3653
3654 i = 0;
3655 while (i < bfd_micromips_num_opcodes)
3656 {
3657 const char *name = micromips_opcodes[i].name;
3658
3659 retval = hash_insert (micromips_op_hash, name,
3660 (void *) &micromips_opcodes[i]);
3661 if (retval != NULL)
3662 as_fatal (_("internal: can't hash `%s': %s"),
3663 micromips_opcodes[i].name, retval);
3664 do
fc76e730
RS
3665 {
3666 struct mips_cl_insn *micromips_nop_insn;
3667
3668 if (!validate_micromips_insn (&micromips_opcodes[i],
3669 &micromips_operands[i]))
3670 broken = 1;
3671
3672 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3673 {
3674 if (micromips_insn_length (micromips_opcodes + i) == 2)
3675 micromips_nop_insn = &micromips_nop16_insn;
3676 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3677 micromips_nop_insn = &micromips_nop32_insn;
3678 else
3679 continue;
3680
3681 if (micromips_nop_insn->insn_mo == NULL
3682 && strcmp (name, "nop") == 0)
3683 {
3684 create_insn (micromips_nop_insn, micromips_opcodes + i);
3685 micromips_nop_insn->fixed_p = 1;
3686 }
3687 }
3688 }
df58fc94
RS
3689 while (++i < bfd_micromips_num_opcodes
3690 && strcmp (micromips_opcodes[i].name, name) == 0);
3691 }
3692
252b5132 3693 if (broken)
1661c76c 3694 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3695
3696 /* We add all the general register names to the symbol table. This
3697 helps us detect invalid uses of them. */
3739860c 3698 for (i = 0; reg_names[i].name; i++)
707bfff6 3699 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3700 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3701 &zero_address_frag));
3702 if (HAVE_NEWABI)
3739860c 3703 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3704 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3705 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3706 &zero_address_frag));
707bfff6 3707 else
3739860c 3708 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3709 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3710 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3711 &zero_address_frag));
6047c971 3712
14daeee3
RS
3713 for (i = 0; i < 32; i++)
3714 {
92fce9bd 3715 char regname[6];
14daeee3
RS
3716
3717 /* R5900 VU0 floating-point register. */
92fce9bd 3718 sprintf (regname, "$vf%d", i);
14daeee3
RS
3719 symbol_table_insert (symbol_new (regname, reg_section,
3720 RTYPE_VF | i, &zero_address_frag));
3721
3722 /* R5900 VU0 integer register. */
92fce9bd 3723 sprintf (regname, "$vi%d", i);
14daeee3
RS
3724 symbol_table_insert (symbol_new (regname, reg_section,
3725 RTYPE_VI | i, &zero_address_frag));
3726
56d438b1 3727 /* MSA register. */
92fce9bd 3728 sprintf (regname, "$w%d", i);
56d438b1
CF
3729 symbol_table_insert (symbol_new (regname, reg_section,
3730 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3731 }
3732
a92713e6
RS
3733 obstack_init (&mips_operand_tokens);
3734
7d10b47d 3735 mips_no_prev_insn ();
252b5132
RH
3736
3737 mips_gprmask = 0;
3738 mips_cprmask[0] = 0;
3739 mips_cprmask[1] = 0;
3740 mips_cprmask[2] = 0;
3741 mips_cprmask[3] = 0;
3742
3743 /* set the default alignment for the text section (2**2) */
3744 record_alignment (text_section, 2);
3745
4d0d148d 3746 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3747
f3ded42a
RS
3748 /* On a native system other than VxWorks, sections must be aligned
3749 to 16 byte boundaries. When configured for an embedded ELF
3750 target, we don't bother. */
3751 if (strncmp (TARGET_OS, "elf", 3) != 0
3752 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3753 {
f3ded42a
RS
3754 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3755 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3756 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3757 }
252b5132 3758
f3ded42a
RS
3759 /* Create a .reginfo section for register masks and a .mdebug
3760 section for debugging information. */
3761 {
3762 segT seg;
3763 subsegT subseg;
3764 flagword flags;
3765 segT sec;
3766
3767 seg = now_seg;
3768 subseg = now_subseg;
3769
3770 /* The ABI says this section should be loaded so that the
3771 running program can access it. However, we don't load it
3772 if we are configured for an embedded target */
3773 flags = SEC_READONLY | SEC_DATA;
3774 if (strncmp (TARGET_OS, "elf", 3) != 0)
3775 flags |= SEC_ALLOC | SEC_LOAD;
3776
3777 if (mips_abi != N64_ABI)
252b5132 3778 {
f3ded42a 3779 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3780
f3ded42a
RS
3781 bfd_set_section_flags (stdoutput, sec, flags);
3782 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3783
f3ded42a
RS
3784 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3785 }
3786 else
3787 {
3788 /* The 64-bit ABI uses a .MIPS.options section rather than
3789 .reginfo section. */
3790 sec = subseg_new (".MIPS.options", (subsegT) 0);
3791 bfd_set_section_flags (stdoutput, sec, flags);
3792 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3793
f3ded42a
RS
3794 /* Set up the option header. */
3795 {
3796 Elf_Internal_Options opthdr;
3797 char *f;
3798
3799 opthdr.kind = ODK_REGINFO;
3800 opthdr.size = (sizeof (Elf_External_Options)
3801 + sizeof (Elf64_External_RegInfo));
3802 opthdr.section = 0;
3803 opthdr.info = 0;
3804 f = frag_more (sizeof (Elf_External_Options));
3805 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3806 (Elf_External_Options *) f);
3807
3808 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3809 }
3810 }
252b5132 3811
351cdf24
MF
3812 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3813 bfd_set_section_flags (stdoutput, sec,
3814 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3815 bfd_set_section_alignment (stdoutput, sec, 3);
3816 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3817
f3ded42a
RS
3818 if (ECOFF_DEBUGGING)
3819 {
3820 sec = subseg_new (".mdebug", (subsegT) 0);
3821 (void) bfd_set_section_flags (stdoutput, sec,
3822 SEC_HAS_CONTENTS | SEC_READONLY);
3823 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3824 }
f3ded42a
RS
3825 else if (mips_flag_pdr)
3826 {
3827 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3828 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3829 SEC_READONLY | SEC_RELOC
3830 | SEC_DEBUGGING);
3831 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3832 }
3833
3834 subseg_set (seg, subseg);
3835 }
252b5132 3836
71400594
RS
3837 if (mips_fix_vr4120)
3838 init_vr4120_conflicts ();
252b5132
RH
3839}
3840
351cdf24
MF
3841static inline void
3842fpabi_incompatible_with (int fpabi, const char *what)
3843{
3844 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3845 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3846}
3847
3848static inline void
3849fpabi_requires (int fpabi, const char *what)
3850{
3851 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3852 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3853}
3854
3855/* Check -mabi and register sizes against the specified FP ABI. */
3856static void
3857check_fpabi (int fpabi)
3858{
351cdf24
MF
3859 switch (fpabi)
3860 {
3861 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3862 if (file_mips_opts.soft_float)
3863 fpabi_incompatible_with (fpabi, "softfloat");
3864 else if (file_mips_opts.single_float)
3865 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3866 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3867 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3868 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3869 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3870 break;
3871
3872 case Val_GNU_MIPS_ABI_FP_XX:
3873 if (mips_abi != O32_ABI)
3874 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3875 else if (file_mips_opts.soft_float)
3876 fpabi_incompatible_with (fpabi, "softfloat");
3877 else if (file_mips_opts.single_float)
3878 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3879 else if (file_mips_opts.fp != 0)
3880 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3881 break;
3882
3883 case Val_GNU_MIPS_ABI_FP_64A:
3884 case Val_GNU_MIPS_ABI_FP_64:
3885 if (mips_abi != O32_ABI)
3886 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3887 else if (file_mips_opts.soft_float)
3888 fpabi_incompatible_with (fpabi, "softfloat");
3889 else if (file_mips_opts.single_float)
3890 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3891 else if (file_mips_opts.fp != 64)
3892 fpabi_requires (fpabi, "fp=64");
3893 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3894 fpabi_incompatible_with (fpabi, "nooddspreg");
3895 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3896 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3897 break;
3898
3899 case Val_GNU_MIPS_ABI_FP_SINGLE:
3900 if (file_mips_opts.soft_float)
3901 fpabi_incompatible_with (fpabi, "softfloat");
3902 else if (!file_mips_opts.single_float)
3903 fpabi_requires (fpabi, "singlefloat");
3904 break;
3905
3906 case Val_GNU_MIPS_ABI_FP_SOFT:
3907 if (!file_mips_opts.soft_float)
3908 fpabi_requires (fpabi, "softfloat");
3909 break;
3910
3911 case Val_GNU_MIPS_ABI_FP_OLD_64:
3912 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3913 Tag_GNU_MIPS_ABI_FP, fpabi);
3914 break;
3915
3350cc01
CM
3916 case Val_GNU_MIPS_ABI_FP_NAN2008:
3917 /* Silently ignore compatibility value. */
3918 break;
3919
351cdf24
MF
3920 default:
3921 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3922 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3923 break;
3924 }
351cdf24
MF
3925}
3926
919731af 3927/* Perform consistency checks on the current options. */
3928
3929static void
3930mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3931{
3932 /* Check the size of integer registers agrees with the ABI and ISA. */
3933 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3934 as_bad (_("`gp=64' used with a 32-bit processor"));
3935 else if (abi_checks
3936 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3937 as_bad (_("`gp=32' used with a 64-bit ABI"));
3938 else if (abi_checks
3939 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3940 as_bad (_("`gp=64' used with a 32-bit ABI"));
3941
3942 /* Check the size of the float registers agrees with the ABI and ISA. */
3943 switch (opts->fp)
3944 {
351cdf24
MF
3945 case 0:
3946 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3947 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3948 else if (opts->single_float == 1)
3949 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3950 break;
919731af 3951 case 64:
3952 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3953 as_bad (_("`fp=64' used with a 32-bit fpu"));
3954 else if (abi_checks
3955 && ABI_NEEDS_32BIT_REGS (mips_abi)
3956 && !ISA_HAS_MXHC1 (opts->isa))
3957 as_warn (_("`fp=64' used with a 32-bit ABI"));
3958 break;
3959 case 32:
3960 if (abi_checks
3961 && ABI_NEEDS_64BIT_REGS (mips_abi))
3962 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3963 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3964 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3965 break;
3966 default:
3967 as_bad (_("Unknown size of floating point registers"));
3968 break;
3969 }
3970
351cdf24
MF
3971 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3972 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3973
919731af 3974 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3975 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3976 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3977 && (opts->micromips == 1
3978 || opts->mips16 == 1))
1357373c 3979 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3980 opts->micromips ? "micromips" : "mips16",
5f4678bb 3981 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3982
3983 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3984 as_fatal (_("branch relaxation is not supported in `%s'"),
3985 mips_cpu_info_from_isa (opts->isa)->name);
919731af 3986}
3987
3988/* Perform consistency checks on the module level options exactly once.
3989 This is a deferred check that happens:
3990 at the first .set directive
3991 or, at the first pseudo op that generates code (inc .dc.a)
3992 or, at the first instruction
3993 or, at the end. */
3994
3995static void
3996file_mips_check_options (void)
3997{
3998 const struct mips_cpu_info *arch_info = 0;
3999
4000 if (file_mips_opts_checked)
4001 return;
4002
4003 /* The following code determines the register size.
4004 Similar code was added to GCC 3.3 (see override_options() in
4005 config/mips/mips.c). The GAS and GCC code should be kept in sync
4006 as much as possible. */
4007
4008 if (file_mips_opts.gp < 0)
4009 {
4010 /* Infer the integer register size from the ABI and processor.
4011 Restrict ourselves to 32-bit registers if that's all the
4012 processor has, or if the ABI cannot handle 64-bit registers. */
4013 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4014 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4015 ? 32 : 64;
4016 }
4017
4018 if (file_mips_opts.fp < 0)
4019 {
4020 /* No user specified float register size.
4021 ??? GAS treats single-float processors as though they had 64-bit
4022 float registers (although it complains when double-precision
4023 instructions are used). As things stand, saying they have 32-bit
4024 registers would lead to spurious "register must be even" messages.
4025 So here we assume float registers are never smaller than the
4026 integer ones. */
4027 if (file_mips_opts.gp == 64)
4028 /* 64-bit integer registers implies 64-bit float registers. */
4029 file_mips_opts.fp = 64;
4030 else if ((file_mips_opts.ase & FP64_ASES)
4031 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4032 /* Handle ASEs that require 64-bit float registers, if possible. */
4033 file_mips_opts.fp = 64;
7361da2c
AB
4034 else if (ISA_IS_R6 (mips_opts.isa))
4035 /* R6 implies 64-bit float registers. */
4036 file_mips_opts.fp = 64;
919731af 4037 else
4038 /* 32-bit float registers. */
4039 file_mips_opts.fp = 32;
4040 }
4041
4042 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4043
351cdf24
MF
4044 /* Disable operations on odd-numbered floating-point registers by default
4045 when using the FPXX ABI. */
4046 if (file_mips_opts.oddspreg < 0)
4047 {
4048 if (file_mips_opts.fp == 0)
4049 file_mips_opts.oddspreg = 0;
4050 else
4051 file_mips_opts.oddspreg = 1;
4052 }
4053
919731af 4054 /* End of GCC-shared inference code. */
4055
4056 /* This flag is set when we have a 64-bit capable CPU but use only
4057 32-bit wide registers. Note that EABI does not use it. */
4058 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4059 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4060 || mips_abi == O32_ABI))
4061 mips_32bitmode = 1;
4062
4063 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4064 as_bad (_("trap exception not supported at ISA 1"));
4065
4066 /* If the selected architecture includes support for ASEs, enable
4067 generation of code for them. */
4068 if (file_mips_opts.mips16 == -1)
4069 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4070 if (file_mips_opts.micromips == -1)
4071 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4072 ? 1 : 0;
4073
7361da2c
AB
4074 if (mips_nan2008 == -1)
4075 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4076 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4077 as_fatal (_("`%s' does not support legacy NaN"),
4078 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4079
919731af 4080 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4081 being selected implicitly. */
4082 if (file_mips_opts.fp != 64)
4083 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4084
4085 /* If the user didn't explicitly select or deselect a particular ASE,
4086 use the default setting for the CPU. */
4087 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4088
4089 /* Set up the current options. These may change throughout assembly. */
4090 mips_opts = file_mips_opts;
4091
4092 mips_check_isa_supports_ases ();
4093 mips_check_options (&file_mips_opts, TRUE);
4094 file_mips_opts_checked = TRUE;
4095
4096 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4097 as_warn (_("could not set architecture and machine"));
4098}
4099
252b5132 4100void
17a2f251 4101md_assemble (char *str)
252b5132
RH
4102{
4103 struct mips_cl_insn insn;
f6688943
TS
4104 bfd_reloc_code_real_type unused_reloc[3]
4105 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4106
919731af 4107 file_mips_check_options ();
4108
252b5132 4109 imm_expr.X_op = O_absent;
252b5132 4110 offset_expr.X_op = O_absent;
f6688943
TS
4111 offset_reloc[0] = BFD_RELOC_UNUSED;
4112 offset_reloc[1] = BFD_RELOC_UNUSED;
4113 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4114
e1b47bd5
RS
4115 mips_mark_labels ();
4116 mips_assembling_insn = TRUE;
e3de51ce 4117 clear_insn_error ();
e1b47bd5 4118
252b5132
RH
4119 if (mips_opts.mips16)
4120 mips16_ip (str, &insn);
4121 else
4122 {
4123 mips_ip (str, &insn);
beae10d5
KH
4124 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4125 str, insn.insn_opcode));
252b5132
RH
4126 }
4127
e3de51ce
RS
4128 if (insn_error.msg)
4129 report_insn_error (str);
e1b47bd5 4130 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4131 {
584892a6 4132 macro_start ();
252b5132
RH
4133 if (mips_opts.mips16)
4134 mips16_macro (&insn);
4135 else
833794fc 4136 macro (&insn, str);
584892a6 4137 macro_end ();
252b5132
RH
4138 }
4139 else
4140 {
77bd4346 4141 if (offset_expr.X_op != O_absent)
df58fc94 4142 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4143 else
df58fc94 4144 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4145 }
e1b47bd5
RS
4146
4147 mips_assembling_insn = FALSE;
252b5132
RH
4148}
4149
738e5348
RS
4150/* Convenience functions for abstracting away the differences between
4151 MIPS16 and non-MIPS16 relocations. */
4152
4153static inline bfd_boolean
4154mips16_reloc_p (bfd_reloc_code_real_type reloc)
4155{
4156 switch (reloc)
4157 {
4158 case BFD_RELOC_MIPS16_JMP:
4159 case BFD_RELOC_MIPS16_GPREL:
4160 case BFD_RELOC_MIPS16_GOT16:
4161 case BFD_RELOC_MIPS16_CALL16:
4162 case BFD_RELOC_MIPS16_HI16_S:
4163 case BFD_RELOC_MIPS16_HI16:
4164 case BFD_RELOC_MIPS16_LO16:
c9775dde 4165 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4166 return TRUE;
4167
4168 default:
4169 return FALSE;
4170 }
4171}
4172
df58fc94
RS
4173static inline bfd_boolean
4174micromips_reloc_p (bfd_reloc_code_real_type reloc)
4175{
4176 switch (reloc)
4177 {
4178 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4179 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4180 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4181 case BFD_RELOC_MICROMIPS_GPREL16:
4182 case BFD_RELOC_MICROMIPS_JMP:
4183 case BFD_RELOC_MICROMIPS_HI16:
4184 case BFD_RELOC_MICROMIPS_HI16_S:
4185 case BFD_RELOC_MICROMIPS_LO16:
4186 case BFD_RELOC_MICROMIPS_LITERAL:
4187 case BFD_RELOC_MICROMIPS_GOT16:
4188 case BFD_RELOC_MICROMIPS_CALL16:
4189 case BFD_RELOC_MICROMIPS_GOT_HI16:
4190 case BFD_RELOC_MICROMIPS_GOT_LO16:
4191 case BFD_RELOC_MICROMIPS_CALL_HI16:
4192 case BFD_RELOC_MICROMIPS_CALL_LO16:
4193 case BFD_RELOC_MICROMIPS_SUB:
4194 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4195 case BFD_RELOC_MICROMIPS_GOT_OFST:
4196 case BFD_RELOC_MICROMIPS_GOT_DISP:
4197 case BFD_RELOC_MICROMIPS_HIGHEST:
4198 case BFD_RELOC_MICROMIPS_HIGHER:
4199 case BFD_RELOC_MICROMIPS_SCN_DISP:
4200 case BFD_RELOC_MICROMIPS_JALR:
4201 return TRUE;
4202
4203 default:
4204 return FALSE;
4205 }
4206}
4207
2309ddf2
MR
4208static inline bfd_boolean
4209jmp_reloc_p (bfd_reloc_code_real_type reloc)
4210{
4211 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4212}
4213
0e9c5a5c
MR
4214static inline bfd_boolean
4215b_reloc_p (bfd_reloc_code_real_type reloc)
4216{
4217 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4218 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4219 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4220 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4221 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4222 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4223 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4224}
4225
738e5348
RS
4226static inline bfd_boolean
4227got16_reloc_p (bfd_reloc_code_real_type reloc)
4228{
2309ddf2 4229 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4230 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4231}
4232
4233static inline bfd_boolean
4234hi16_reloc_p (bfd_reloc_code_real_type reloc)
4235{
2309ddf2 4236 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4237 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4238}
4239
4240static inline bfd_boolean
4241lo16_reloc_p (bfd_reloc_code_real_type reloc)
4242{
2309ddf2 4243 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4244 || reloc == BFD_RELOC_MICROMIPS_LO16);
4245}
4246
df58fc94
RS
4247static inline bfd_boolean
4248jalr_reloc_p (bfd_reloc_code_real_type reloc)
4249{
2309ddf2 4250 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4251}
4252
f2ae14a1
RS
4253static inline bfd_boolean
4254gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4255{
4256 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4257 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4258}
4259
2de39019
CM
4260/* Return true if RELOC is a PC-relative relocation that does not have
4261 full address range. */
4262
4263static inline bfd_boolean
4264limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4265{
4266 switch (reloc)
4267 {
4268 case BFD_RELOC_16_PCREL_S2:
c9775dde 4269 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4270 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4271 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4272 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4273 case BFD_RELOC_MIPS_21_PCREL_S2:
4274 case BFD_RELOC_MIPS_26_PCREL_S2:
4275 case BFD_RELOC_MIPS_18_PCREL_S3:
4276 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4277 return TRUE;
4278
b47468a6 4279 case BFD_RELOC_32_PCREL:
7361da2c
AB
4280 case BFD_RELOC_HI16_S_PCREL:
4281 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4282 return HAVE_64BIT_ADDRESSES;
4283
2de39019
CM
4284 default:
4285 return FALSE;
4286 }
4287}
b47468a6 4288
5919d012 4289/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4290 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4291 need a matching %lo() when applied to local symbols. */
5919d012
RS
4292
4293static inline bfd_boolean
17a2f251 4294reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4295{
3b91255e 4296 return (HAVE_IN_PLACE_ADDENDS
738e5348 4297 && (hi16_reloc_p (reloc)
0a44bf69
RS
4298 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4299 all GOT16 relocations evaluate to "G". */
738e5348
RS
4300 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4301}
4302
4303/* Return the type of %lo() reloc needed by RELOC, given that
4304 reloc_needs_lo_p. */
4305
4306static inline bfd_reloc_code_real_type
4307matching_lo_reloc (bfd_reloc_code_real_type reloc)
4308{
df58fc94
RS
4309 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4310 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4311 : BFD_RELOC_LO16));
5919d012
RS
4312}
4313
4314/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4315 relocation. */
4316
4317static inline bfd_boolean
17a2f251 4318fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4319{
4320 return (fixp->fx_next != NULL
738e5348 4321 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4322 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4323 && fixp->fx_offset == fixp->fx_next->fx_offset);
4324}
4325
462427c4
RS
4326/* Move all labels in LABELS to the current insertion point. TEXT_P
4327 says whether the labels refer to text or data. */
404a8071
RS
4328
4329static void
462427c4 4330mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4331{
4332 struct insn_label_list *l;
4333 valueT val;
4334
462427c4 4335 for (l = labels; l != NULL; l = l->next)
404a8071 4336 {
9c2799c2 4337 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4338 symbol_set_frag (l->label, frag_now);
4339 val = (valueT) frag_now_fix ();
df58fc94 4340 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4341 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4342 ++val;
4343 S_SET_VALUE (l->label, val);
4344 }
4345}
4346
462427c4
RS
4347/* Move all labels in insn_labels to the current insertion point
4348 and treat them as text labels. */
4349
4350static void
4351mips_move_text_labels (void)
4352{
4353 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4354}
4355
9e009953
MR
4356/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4357
5f0fe04b
TS
4358static bfd_boolean
4359s_is_linkonce (symbolS *sym, segT from_seg)
4360{
4361 bfd_boolean linkonce = FALSE;
4362 segT symseg = S_GET_SEGMENT (sym);
4363
4364 if (symseg != from_seg && !S_IS_LOCAL (sym))
4365 {
4366 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4367 linkonce = TRUE;
5f0fe04b
TS
4368 /* The GNU toolchain uses an extension for ELF: a section
4369 beginning with the magic string .gnu.linkonce is a
4370 linkonce section. */
4371 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4372 sizeof ".gnu.linkonce" - 1) == 0)
4373 linkonce = TRUE;
5f0fe04b
TS
4374 }
4375 return linkonce;
4376}
4377
e1b47bd5 4378/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4379 linker to handle them specially, such as generating jalx instructions
4380 when needed. We also make them odd for the duration of the assembly,
4381 in order to generate the right sort of code. We will make them even
252b5132
RH
4382 in the adjust_symtab routine, while leaving them marked. This is
4383 convenient for the debugger and the disassembler. The linker knows
4384 to make them odd again. */
4385
4386static void
e1b47bd5 4387mips_compressed_mark_label (symbolS *label)
252b5132 4388{
df58fc94 4389 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4390
f3ded42a
RS
4391 if (mips_opts.mips16)
4392 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4393 else
4394 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4395 if ((S_GET_VALUE (label) & 1) == 0
4396 /* Don't adjust the address if the label is global or weak, or
4397 in a link-once section, since we'll be emitting symbol reloc
4398 references to it which will be patched up by the linker, and
4399 the final value of the symbol may or may not be MIPS16/microMIPS. */
4400 && !S_IS_WEAK (label)
4401 && !S_IS_EXTERNAL (label)
4402 && !s_is_linkonce (label, now_seg))
4403 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4404}
4405
4406/* Mark preceding MIPS16 or microMIPS instruction labels. */
4407
4408static void
4409mips_compressed_mark_labels (void)
4410{
4411 struct insn_label_list *l;
4412
4413 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4414 mips_compressed_mark_label (l->label);
252b5132
RH
4415}
4416
4d7206a2
RS
4417/* End the current frag. Make it a variant frag and record the
4418 relaxation info. */
4419
4420static void
4421relax_close_frag (void)
4422{
584892a6 4423 mips_macro_warning.first_frag = frag_now;
4d7206a2 4424 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4425 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4426 mips_pic != NO_PIC),
4d7206a2
RS
4427 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4428
4429 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4430 mips_relax.first_fixup = 0;
4431}
4432
4433/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4434 See the comment above RELAX_ENCODE for more details. */
4435
4436static void
4437relax_start (symbolS *symbol)
4438{
9c2799c2 4439 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4440 mips_relax.sequence = 1;
4441 mips_relax.symbol = symbol;
4442}
4443
4444/* Start generating the second version of a relaxable sequence.
4445 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4446
4447static void
4d7206a2
RS
4448relax_switch (void)
4449{
9c2799c2 4450 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4451 mips_relax.sequence = 2;
4452}
4453
4454/* End the current relaxable sequence. */
4455
4456static void
4457relax_end (void)
4458{
9c2799c2 4459 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4460 relax_close_frag ();
4461 mips_relax.sequence = 0;
4462}
4463
11625dd8
RS
4464/* Return true if IP is a delayed branch or jump. */
4465
4466static inline bfd_boolean
4467delayed_branch_p (const struct mips_cl_insn *ip)
4468{
4469 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4470 | INSN_COND_BRANCH_DELAY
4471 | INSN_COND_BRANCH_LIKELY)) != 0;
4472}
4473
4474/* Return true if IP is a compact branch or jump. */
4475
4476static inline bfd_boolean
4477compact_branch_p (const struct mips_cl_insn *ip)
4478{
26545944
RS
4479 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4480 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4481}
4482
4483/* Return true if IP is an unconditional branch or jump. */
4484
4485static inline bfd_boolean
4486uncond_branch_p (const struct mips_cl_insn *ip)
4487{
4488 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4489 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4490}
4491
4492/* Return true if IP is a branch-likely instruction. */
4493
4494static inline bfd_boolean
4495branch_likely_p (const struct mips_cl_insn *ip)
4496{
4497 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4498}
4499
14fe068b
RS
4500/* Return the type of nop that should be used to fill the delay slot
4501 of delayed branch IP. */
4502
4503static struct mips_cl_insn *
4504get_delay_slot_nop (const struct mips_cl_insn *ip)
4505{
4506 if (mips_opts.micromips
4507 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4508 return &micromips_nop32_insn;
4509 return NOP_INSN;
4510}
4511
fc76e730
RS
4512/* Return a mask that has bit N set if OPCODE reads the register(s)
4513 in operand N. */
df58fc94
RS
4514
4515static unsigned int
fc76e730 4516insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4517{
fc76e730
RS
4518 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4519}
df58fc94 4520
fc76e730
RS
4521/* Return a mask that has bit N set if OPCODE writes to the register(s)
4522 in operand N. */
4523
4524static unsigned int
4525insn_write_mask (const struct mips_opcode *opcode)
4526{
4527 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4528}
4529
4530/* Return a mask of the registers specified by operand OPERAND of INSN.
4531 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4532 is set. */
4533
4534static unsigned int
4535operand_reg_mask (const struct mips_cl_insn *insn,
4536 const struct mips_operand *operand,
4537 unsigned int type_mask)
4538{
4539 unsigned int uval, vsel;
4540
4541 switch (operand->type)
df58fc94 4542 {
fc76e730
RS
4543 case OP_INT:
4544 case OP_MAPPED_INT:
4545 case OP_MSB:
4546 case OP_PCREL:
4547 case OP_PERF_REG:
4548 case OP_ADDIUSP_INT:
4549 case OP_ENTRY_EXIT_LIST:
4550 case OP_REPEAT_DEST_REG:
4551 case OP_REPEAT_PREV_REG:
4552 case OP_PC:
14daeee3
RS
4553 case OP_VU0_SUFFIX:
4554 case OP_VU0_MATCH_SUFFIX:
56d438b1 4555 case OP_IMM_INDEX:
fc76e730
RS
4556 abort ();
4557
25499ac7
MR
4558 case OP_REG28:
4559 return 1 << 28;
4560
fc76e730 4561 case OP_REG:
0f35dbc4 4562 case OP_OPTIONAL_REG:
fc76e730
RS
4563 {
4564 const struct mips_reg_operand *reg_op;
4565
4566 reg_op = (const struct mips_reg_operand *) operand;
4567 if (!(type_mask & (1 << reg_op->reg_type)))
4568 return 0;
4569 uval = insn_extract_operand (insn, operand);
4570 return 1 << mips_decode_reg_operand (reg_op, uval);
4571 }
4572
4573 case OP_REG_PAIR:
4574 {
4575 const struct mips_reg_pair_operand *pair_op;
4576
4577 pair_op = (const struct mips_reg_pair_operand *) operand;
4578 if (!(type_mask & (1 << pair_op->reg_type)))
4579 return 0;
4580 uval = insn_extract_operand (insn, operand);
4581 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4582 }
4583
4584 case OP_CLO_CLZ_DEST:
4585 if (!(type_mask & (1 << OP_REG_GP)))
4586 return 0;
4587 uval = insn_extract_operand (insn, operand);
4588 return (1 << (uval & 31)) | (1 << (uval >> 5));
4589
7361da2c
AB
4590 case OP_SAME_RS_RT:
4591 if (!(type_mask & (1 << OP_REG_GP)))
4592 return 0;
4593 uval = insn_extract_operand (insn, operand);
4594 gas_assert ((uval & 31) == (uval >> 5));
4595 return 1 << (uval & 31);
4596
4597 case OP_CHECK_PREV:
4598 case OP_NON_ZERO_REG:
4599 if (!(type_mask & (1 << OP_REG_GP)))
4600 return 0;
4601 uval = insn_extract_operand (insn, operand);
4602 return 1 << (uval & 31);
4603
fc76e730
RS
4604 case OP_LWM_SWM_LIST:
4605 abort ();
4606
4607 case OP_SAVE_RESTORE_LIST:
4608 abort ();
4609
4610 case OP_MDMX_IMM_REG:
4611 if (!(type_mask & (1 << OP_REG_VEC)))
4612 return 0;
4613 uval = insn_extract_operand (insn, operand);
4614 vsel = uval >> 5;
4615 if ((vsel & 0x18) == 0x18)
4616 return 0;
4617 return 1 << (uval & 31);
56d438b1
CF
4618
4619 case OP_REG_INDEX:
4620 if (!(type_mask & (1 << OP_REG_GP)))
4621 return 0;
4622 return 1 << insn_extract_operand (insn, operand);
df58fc94 4623 }
fc76e730
RS
4624 abort ();
4625}
4626
4627/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4628 where bit N of OPNO_MASK is set if operand N should be included.
4629 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4630 is set. */
4631
4632static unsigned int
4633insn_reg_mask (const struct mips_cl_insn *insn,
4634 unsigned int type_mask, unsigned int opno_mask)
4635{
4636 unsigned int opno, reg_mask;
4637
4638 opno = 0;
4639 reg_mask = 0;
4640 while (opno_mask != 0)
4641 {
4642 if (opno_mask & 1)
4643 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4644 opno_mask >>= 1;
4645 opno += 1;
4646 }
4647 return reg_mask;
df58fc94
RS
4648}
4649
4c260379
RS
4650/* Return the mask of core registers that IP reads. */
4651
4652static unsigned int
4653gpr_read_mask (const struct mips_cl_insn *ip)
4654{
4655 unsigned long pinfo, pinfo2;
4656 unsigned int mask;
4657
fc76e730 4658 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4659 pinfo = ip->insn_mo->pinfo;
4660 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4661 if (pinfo & INSN_UDI)
4c260379 4662 {
fc76e730
RS
4663 /* UDI instructions have traditionally been assumed to read RS
4664 and RT. */
4665 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4666 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4667 }
fc76e730
RS
4668 if (pinfo & INSN_READ_GPR_24)
4669 mask |= 1 << 24;
4670 if (pinfo2 & INSN2_READ_GPR_16)
4671 mask |= 1 << 16;
4672 if (pinfo2 & INSN2_READ_SP)
4673 mask |= 1 << SP;
26545944 4674 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4675 mask |= 1 << 31;
fe35f09f
RS
4676 /* Don't include register 0. */
4677 return mask & ~1;
4c260379
RS
4678}
4679
4680/* Return the mask of core registers that IP writes. */
4681
4682static unsigned int
4683gpr_write_mask (const struct mips_cl_insn *ip)
4684{
4685 unsigned long pinfo, pinfo2;
4686 unsigned int mask;
4687
fc76e730 4688 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4689 pinfo = ip->insn_mo->pinfo;
4690 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4691 if (pinfo & INSN_WRITE_GPR_24)
4692 mask |= 1 << 24;
4693 if (pinfo & INSN_WRITE_GPR_31)
4694 mask |= 1 << 31;
4695 if (pinfo & INSN_UDI)
4696 /* UDI instructions have traditionally been assumed to write to RD. */
4697 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4698 if (pinfo2 & INSN2_WRITE_SP)
4699 mask |= 1 << SP;
fe35f09f
RS
4700 /* Don't include register 0. */
4701 return mask & ~1;
4c260379
RS
4702}
4703
4704/* Return the mask of floating-point registers that IP reads. */
4705
4706static unsigned int
4707fpr_read_mask (const struct mips_cl_insn *ip)
4708{
fc76e730 4709 unsigned long pinfo;
4c260379
RS
4710 unsigned int mask;
4711
9d5de888
CF
4712 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4713 | (1 << OP_REG_MSA)),
fc76e730 4714 insn_read_mask (ip->insn_mo));
4c260379 4715 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4716 /* Conservatively treat all operands to an FP_D instruction are doubles.
4717 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4718 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4719 mask |= mask << 1;
4720 return mask;
4721}
4722
4723/* Return the mask of floating-point registers that IP writes. */
4724
4725static unsigned int
4726fpr_write_mask (const struct mips_cl_insn *ip)
4727{
fc76e730 4728 unsigned long pinfo;
4c260379
RS
4729 unsigned int mask;
4730
9d5de888
CF
4731 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4732 | (1 << OP_REG_MSA)),
fc76e730 4733 insn_write_mask (ip->insn_mo));
4c260379 4734 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4735 /* Conservatively treat all operands to an FP_D instruction are doubles.
4736 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4737 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4738 mask |= mask << 1;
4739 return mask;
4740}
4741
a1d78564
RS
4742/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4743 Check whether that is allowed. */
4744
4745static bfd_boolean
4746mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4747{
4748 const char *s = insn->name;
351cdf24
MF
4749 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4750 || FPR_SIZE == 64)
4751 && mips_opts.oddspreg;
a1d78564
RS
4752
4753 if (insn->pinfo == INSN_MACRO)
4754 /* Let a macro pass, we'll catch it later when it is expanded. */
4755 return TRUE;
4756
351cdf24
MF
4757 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4758 otherwise it depends on oddspreg. */
4759 if ((insn->pinfo & FP_S)
4760 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4761 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4762 return FPR_SIZE == 32 || oddspreg;
a1d78564 4763
351cdf24
MF
4764 /* Allow odd registers for single-precision ops and double-precision if the
4765 floating-point registers are 64-bit wide. */
4766 switch (insn->pinfo & (FP_S | FP_D))
4767 {
4768 case FP_S:
4769 case 0:
4770 return oddspreg;
4771 case FP_D:
4772 return FPR_SIZE == 64;
4773 default:
4774 break;
a1d78564
RS
4775 }
4776
351cdf24
MF
4777 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4778 s = strchr (insn->name, '.');
4779 if (s != NULL && opnum == 2)
4780 s = strchr (s + 1, '.');
4781 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4782 return oddspreg;
a1d78564 4783
351cdf24 4784 return FPR_SIZE == 64;
a1d78564
RS
4785}
4786
a1d78564
RS
4787/* Information about an instruction argument that we're trying to match. */
4788struct mips_arg_info
4789{
4790 /* The instruction so far. */
4791 struct mips_cl_insn *insn;
4792
a92713e6
RS
4793 /* The first unconsumed operand token. */
4794 struct mips_operand_token *token;
4795
a1d78564
RS
4796 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4797 int opnum;
4798
4799 /* The 1-based argument number, for error reporting. This does not
4800 count elided optional registers, etc.. */
4801 int argnum;
4802
4803 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4804 unsigned int last_regno;
4805
4806 /* If the first operand was an OP_REG, this is the register that it
4807 specified, otherwise it is ILLEGAL_REG. */
4808 unsigned int dest_regno;
4809
4810 /* The value of the last OP_INT operand. Only used for OP_MSB,
4811 where it gives the lsb position. */
4812 unsigned int last_op_int;
4813
60f20e8b 4814 /* If true, match routines should assume that no later instruction
2b0f3761 4815 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4816 possible. Match routines should not report errors if something
4817 is only invalid for !LAX_MATCH. */
4818 bfd_boolean lax_match;
a1d78564 4819
a1d78564
RS
4820 /* True if a reference to the current AT register was seen. */
4821 bfd_boolean seen_at;
4822};
4823
1a00e612
RS
4824/* Record that the argument is out of range. */
4825
4826static void
4827match_out_of_range (struct mips_arg_info *arg)
4828{
4829 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4830}
4831
4832/* Record that the argument isn't constant but needs to be. */
4833
4834static void
4835match_not_constant (struct mips_arg_info *arg)
4836{
4837 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4838 arg->argnum);
4839}
4840
a92713e6
RS
4841/* Try to match an OT_CHAR token for character CH. Consume the token
4842 and return true on success, otherwise return false. */
a1d78564 4843
a92713e6
RS
4844static bfd_boolean
4845match_char (struct mips_arg_info *arg, char ch)
a1d78564 4846{
a92713e6
RS
4847 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4848 {
4849 ++arg->token;
4850 if (ch == ',')
4851 arg->argnum += 1;
4852 return TRUE;
4853 }
4854 return FALSE;
4855}
a1d78564 4856
a92713e6
RS
4857/* Try to get an expression from the next tokens in ARG. Consume the
4858 tokens and return true on success, storing the expression value in
4859 VALUE and relocation types in R. */
4860
4861static bfd_boolean
4862match_expression (struct mips_arg_info *arg, expressionS *value,
4863 bfd_reloc_code_real_type *r)
4864{
d436c1c2
RS
4865 /* If the next token is a '(' that was parsed as being part of a base
4866 expression, assume we have an elided offset. The later match will fail
4867 if this turns out to be wrong. */
4868 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4869 {
d436c1c2
RS
4870 value->X_op = O_constant;
4871 value->X_add_number = 0;
4872 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4873 return TRUE;
4874 }
4875
d436c1c2
RS
4876 /* Reject register-based expressions such as "0+$2" and "(($2))".
4877 For plain registers the default error seems more appropriate. */
4878 if (arg->token->type == OT_INTEGER
4879 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4880 {
d436c1c2
RS
4881 set_insn_error (arg->argnum, _("register value used as expression"));
4882 return FALSE;
a1d78564 4883 }
d436c1c2
RS
4884
4885 if (arg->token->type == OT_INTEGER)
a92713e6 4886 {
d436c1c2
RS
4887 *value = arg->token->u.integer.value;
4888 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4889 ++arg->token;
4890 return TRUE;
a92713e6 4891 }
a92713e6 4892
d436c1c2
RS
4893 set_insn_error_i
4894 (arg->argnum, _("operand %d must be an immediate expression"),
4895 arg->argnum);
4896 return FALSE;
a92713e6
RS
4897}
4898
4899/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4900 the tokens and return true on success, storing the constant value
a54d5f8b 4901 in *VALUE. */
a92713e6
RS
4902
4903static bfd_boolean
1a00e612 4904match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4905{
4906 expressionS ex;
4907 bfd_reloc_code_real_type r[3];
a1d78564 4908
a92713e6
RS
4909 if (!match_expression (arg, &ex, r))
4910 return FALSE;
4911
4912 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4913 *value = ex.X_add_number;
4914 else
4915 {
c96425c5
MR
4916 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4917 match_out_of_range (arg);
4918 else
4919 match_not_constant (arg);
1a00e612 4920 return FALSE;
a1d78564 4921 }
a92713e6 4922 return TRUE;
a1d78564
RS
4923}
4924
4925/* Return the RTYPE_* flags for a register operand of type TYPE that
4926 appears in instruction OPCODE. */
4927
4928static unsigned int
4929convert_reg_type (const struct mips_opcode *opcode,
4930 enum mips_reg_operand_type type)
4931{
4932 switch (type)
4933 {
4934 case OP_REG_GP:
4935 return RTYPE_NUM | RTYPE_GP;
4936
4937 case OP_REG_FP:
4938 /* Allow vector register names for MDMX if the instruction is a 64-bit
4939 FPR load, store or move (including moves to and from GPRs). */
4940 if ((mips_opts.ase & ASE_MDMX)
4941 && (opcode->pinfo & FP_D)
43885403 4942 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4943 | INSN_COPROC_MEMORY_DELAY
43885403 4944 | INSN_LOAD_COPROC
67dc82bc 4945 | INSN_LOAD_MEMORY
a1d78564
RS
4946 | INSN_STORE_MEMORY)))
4947 return RTYPE_FPU | RTYPE_VEC;
4948 return RTYPE_FPU;
4949
4950 case OP_REG_CCC:
4951 if (opcode->pinfo & (FP_D | FP_S))
4952 return RTYPE_CCC | RTYPE_FCC;
4953 return RTYPE_CCC;
4954
4955 case OP_REG_VEC:
4956 if (opcode->membership & INSN_5400)
4957 return RTYPE_FPU;
4958 return RTYPE_FPU | RTYPE_VEC;
4959
4960 case OP_REG_ACC:
4961 return RTYPE_ACC;
4962
4963 case OP_REG_COPRO:
4964 if (opcode->name[strlen (opcode->name) - 1] == '0')
4965 return RTYPE_NUM | RTYPE_CP0;
4966 return RTYPE_NUM;
4967
4968 case OP_REG_HW:
4969 return RTYPE_NUM;
14daeee3
RS
4970
4971 case OP_REG_VI:
4972 return RTYPE_NUM | RTYPE_VI;
4973
4974 case OP_REG_VF:
4975 return RTYPE_NUM | RTYPE_VF;
4976
4977 case OP_REG_R5900_I:
4978 return RTYPE_R5900_I;
4979
4980 case OP_REG_R5900_Q:
4981 return RTYPE_R5900_Q;
4982
4983 case OP_REG_R5900_R:
4984 return RTYPE_R5900_R;
4985
4986 case OP_REG_R5900_ACC:
4987 return RTYPE_R5900_ACC;
56d438b1
CF
4988
4989 case OP_REG_MSA:
4990 return RTYPE_MSA;
4991
4992 case OP_REG_MSA_CTRL:
4993 return RTYPE_NUM;
a1d78564
RS
4994 }
4995 abort ();
4996}
4997
4998/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4999
5000static void
5001check_regno (struct mips_arg_info *arg,
5002 enum mips_reg_operand_type type, unsigned int regno)
5003{
5004 if (AT && type == OP_REG_GP && regno == AT)
5005 arg->seen_at = TRUE;
5006
5007 if (type == OP_REG_FP
5008 && (regno & 1) != 0
a1d78564 5009 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5010 {
5011 /* This was a warning prior to introducing O32 FPXX and FP64 support
5012 so maintain a warning for FP32 but raise an error for the new
5013 cases. */
5014 if (FPR_SIZE == 32)
5015 as_warn (_("float register should be even, was %d"), regno);
5016 else
5017 as_bad (_("float register should be even, was %d"), regno);
5018 }
a1d78564
RS
5019
5020 if (type == OP_REG_CCC)
5021 {
5022 const char *name;
5023 size_t length;
5024
5025 name = arg->insn->insn_mo->name;
5026 length = strlen (name);
5027 if ((regno & 1) != 0
5028 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5029 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5030 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5031 name, regno);
5032
5033 if ((regno & 3) != 0
5034 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5035 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5036 name, regno);
5037 }
5038}
5039
a92713e6
RS
5040/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5041 a register of type TYPE. Return true on success, storing the register
5042 number in *REGNO and warning about any dubious uses. */
5043
5044static bfd_boolean
5045match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5046 unsigned int symval, unsigned int *regno)
5047{
5048 if (type == OP_REG_VEC)
5049 symval = mips_prefer_vec_regno (symval);
5050 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5051 return FALSE;
5052
5053 *regno = symval & RNUM_MASK;
5054 check_regno (arg, type, *regno);
5055 return TRUE;
5056}
5057
5058/* Try to interpret the next token in ARG as a register of type TYPE.
5059 Consume the token and return true on success, storing the register
5060 number in *REGNO. Return false on failure. */
5061
5062static bfd_boolean
5063match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5064 unsigned int *regno)
5065{
5066 if (arg->token->type == OT_REG
5067 && match_regno (arg, type, arg->token->u.regno, regno))
5068 {
5069 ++arg->token;
5070 return TRUE;
5071 }
5072 return FALSE;
5073}
5074
5075/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5076 Consume the token and return true on success, storing the register numbers
5077 in *REGNO1 and *REGNO2. Return false on failure. */
5078
5079static bfd_boolean
5080match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5081 unsigned int *regno1, unsigned int *regno2)
5082{
5083 if (match_reg (arg, type, regno1))
5084 {
5085 *regno2 = *regno1;
5086 return TRUE;
5087 }
5088 if (arg->token->type == OT_REG_RANGE
5089 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5090 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5091 && *regno1 <= *regno2)
5092 {
5093 ++arg->token;
5094 return TRUE;
5095 }
5096 return FALSE;
5097}
5098
a1d78564
RS
5099/* OP_INT matcher. */
5100
a92713e6 5101static bfd_boolean
a1d78564 5102match_int_operand (struct mips_arg_info *arg,
a92713e6 5103 const struct mips_operand *operand_base)
a1d78564
RS
5104{
5105 const struct mips_int_operand *operand;
3ccad066 5106 unsigned int uval;
a1d78564
RS
5107 int min_val, max_val, factor;
5108 offsetT sval;
a1d78564
RS
5109
5110 operand = (const struct mips_int_operand *) operand_base;
5111 factor = 1 << operand->shift;
3ccad066
RS
5112 min_val = mips_int_operand_min (operand);
5113 max_val = mips_int_operand_max (operand);
a1d78564 5114
d436c1c2
RS
5115 if (operand_base->lsb == 0
5116 && operand_base->size == 16
5117 && operand->shift == 0
5118 && operand->bias == 0
5119 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5120 {
5121 /* The operand can be relocated. */
a92713e6
RS
5122 if (!match_expression (arg, &offset_expr, offset_reloc))
5123 return FALSE;
5124
c96425c5
MR
5125 if (offset_expr.X_op == O_big)
5126 {
5127 match_out_of_range (arg);
5128 return FALSE;
5129 }
5130
a92713e6 5131 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5132 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5133 leave the relocation value in offset_expr and offset_relocs
5134 for the caller to process. */
a92713e6
RS
5135 return TRUE;
5136
5137 if (offset_expr.X_op != O_constant)
a1d78564 5138 {
60f20e8b
RS
5139 /* Accept non-constant operands if no later alternative matches,
5140 leaving it for the caller to process. */
5141 if (!arg->lax_match)
602b88e3
MR
5142 {
5143 match_not_constant (arg);
5144 return FALSE;
5145 }
a92713e6
RS
5146 offset_reloc[0] = BFD_RELOC_LO16;
5147 return TRUE;
a1d78564 5148 }
a92713e6 5149
a1d78564
RS
5150 /* Clear the global state; we're going to install the operand
5151 ourselves. */
a92713e6 5152 sval = offset_expr.X_add_number;
a1d78564 5153 offset_expr.X_op = O_absent;
60f20e8b
RS
5154
5155 /* For compatibility with older assemblers, we accept
5156 0x8000-0xffff as signed 16-bit numbers when only
5157 signed numbers are allowed. */
5158 if (sval > max_val)
5159 {
5160 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5161 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5162 {
5163 match_out_of_range (arg);
5164 return FALSE;
5165 }
60f20e8b 5166 }
a1d78564
RS
5167 }
5168 else
5169 {
1a00e612 5170 if (!match_const_int (arg, &sval))
a92713e6 5171 return FALSE;
a1d78564
RS
5172 }
5173
5174 arg->last_op_int = sval;
5175
1a00e612 5176 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5177 {
1a00e612
RS
5178 match_out_of_range (arg);
5179 return FALSE;
a1d78564
RS
5180 }
5181
5182 uval = (unsigned int) sval >> operand->shift;
5183 uval -= operand->bias;
5184
5185 /* Handle -mfix-cn63xxp1. */
5186 if (arg->opnum == 1
5187 && mips_fix_cn63xxp1
5188 && !mips_opts.micromips
5189 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5190 switch (uval)
5191 {
5192 case 5:
5193 case 25:
5194 case 26:
5195 case 27:
5196 case 28:
5197 case 29:
5198 case 30:
5199 case 31:
5200 /* These are ok. */
5201 break;
5202
5203 default:
5204 /* The rest must be changed to 28. */
5205 uval = 28;
5206 break;
5207 }
5208
5209 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5210 return TRUE;
a1d78564
RS
5211}
5212
5213/* OP_MAPPED_INT matcher. */
5214
a92713e6 5215static bfd_boolean
a1d78564 5216match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5217 const struct mips_operand *operand_base)
a1d78564
RS
5218{
5219 const struct mips_mapped_int_operand *operand;
5220 unsigned int uval, num_vals;
5221 offsetT sval;
5222
5223 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5224 if (!match_const_int (arg, &sval))
a92713e6 5225 return FALSE;
a1d78564
RS
5226
5227 num_vals = 1 << operand_base->size;
5228 for (uval = 0; uval < num_vals; uval++)
5229 if (operand->int_map[uval] == sval)
5230 break;
5231 if (uval == num_vals)
1a00e612
RS
5232 {
5233 match_out_of_range (arg);
5234 return FALSE;
5235 }
a1d78564
RS
5236
5237 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5238 return TRUE;
a1d78564
RS
5239}
5240
5241/* OP_MSB matcher. */
5242
a92713e6 5243static bfd_boolean
a1d78564 5244match_msb_operand (struct mips_arg_info *arg,
a92713e6 5245 const struct mips_operand *operand_base)
a1d78564
RS
5246{
5247 const struct mips_msb_operand *operand;
5248 int min_val, max_val, max_high;
5249 offsetT size, sval, high;
5250
5251 operand = (const struct mips_msb_operand *) operand_base;
5252 min_val = operand->bias;
5253 max_val = min_val + (1 << operand_base->size) - 1;
5254 max_high = operand->opsize;
5255
1a00e612 5256 if (!match_const_int (arg, &size))
a92713e6 5257 return FALSE;
a1d78564
RS
5258
5259 high = size + arg->last_op_int;
5260 sval = operand->add_lsb ? high : size;
5261
5262 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5263 {
1a00e612
RS
5264 match_out_of_range (arg);
5265 return FALSE;
a1d78564
RS
5266 }
5267 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5268 return TRUE;
a1d78564
RS
5269}
5270
5271/* OP_REG matcher. */
5272
a92713e6 5273static bfd_boolean
a1d78564 5274match_reg_operand (struct mips_arg_info *arg,
a92713e6 5275 const struct mips_operand *operand_base)
a1d78564
RS
5276{
5277 const struct mips_reg_operand *operand;
a92713e6 5278 unsigned int regno, uval, num_vals;
a1d78564
RS
5279
5280 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5281 if (!match_reg (arg, operand->reg_type, &regno))
5282 return FALSE;
a1d78564
RS
5283
5284 if (operand->reg_map)
5285 {
5286 num_vals = 1 << operand->root.size;
5287 for (uval = 0; uval < num_vals; uval++)
5288 if (operand->reg_map[uval] == regno)
5289 break;
5290 if (num_vals == uval)
a92713e6 5291 return FALSE;
a1d78564
RS
5292 }
5293 else
5294 uval = regno;
5295
a1d78564
RS
5296 arg->last_regno = regno;
5297 if (arg->opnum == 1)
5298 arg->dest_regno = regno;
5299 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5300 return TRUE;
a1d78564
RS
5301}
5302
5303/* OP_REG_PAIR matcher. */
5304
a92713e6 5305static bfd_boolean
a1d78564 5306match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5307 const struct mips_operand *operand_base)
a1d78564
RS
5308{
5309 const struct mips_reg_pair_operand *operand;
a92713e6 5310 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5311
5312 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5313 if (!match_reg (arg, operand->reg_type, &regno1)
5314 || !match_char (arg, ',')
5315 || !match_reg (arg, operand->reg_type, &regno2))
5316 return FALSE;
a1d78564
RS
5317
5318 num_vals = 1 << operand_base->size;
5319 for (uval = 0; uval < num_vals; uval++)
5320 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5321 break;
5322 if (uval == num_vals)
a92713e6 5323 return FALSE;
a1d78564 5324
a1d78564 5325 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5326 return TRUE;
a1d78564
RS
5327}
5328
5329/* OP_PCREL matcher. The caller chooses the relocation type. */
5330
a92713e6
RS
5331static bfd_boolean
5332match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5333{
a92713e6
RS
5334 bfd_reloc_code_real_type r[3];
5335
5336 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5337}
5338
5339/* OP_PERF_REG matcher. */
5340
a92713e6 5341static bfd_boolean
a1d78564 5342match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5343 const struct mips_operand *operand)
a1d78564
RS
5344{
5345 offsetT sval;
5346
1a00e612 5347 if (!match_const_int (arg, &sval))
a92713e6 5348 return FALSE;
a1d78564
RS
5349
5350 if (sval != 0
5351 && (sval != 1
5352 || (mips_opts.arch == CPU_R5900
5353 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5354 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5355 {
1a00e612
RS
5356 set_insn_error (arg->argnum, _("invalid performance register"));
5357 return FALSE;
a1d78564
RS
5358 }
5359
5360 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5361 return TRUE;
a1d78564
RS
5362}
5363
5364/* OP_ADDIUSP matcher. */
5365
a92713e6 5366static bfd_boolean
a1d78564 5367match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5368 const struct mips_operand *operand)
a1d78564
RS
5369{
5370 offsetT sval;
5371 unsigned int uval;
5372
1a00e612 5373 if (!match_const_int (arg, &sval))
a92713e6 5374 return FALSE;
a1d78564
RS
5375
5376 if (sval % 4)
1a00e612
RS
5377 {
5378 match_out_of_range (arg);
5379 return FALSE;
5380 }
a1d78564
RS
5381
5382 sval /= 4;
5383 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5384 {
5385 match_out_of_range (arg);
5386 return FALSE;
5387 }
a1d78564
RS
5388
5389 uval = (unsigned int) sval;
5390 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5391 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5392 return TRUE;
a1d78564
RS
5393}
5394
5395/* OP_CLO_CLZ_DEST matcher. */
5396
a92713e6 5397static bfd_boolean
a1d78564 5398match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5399 const struct mips_operand *operand)
a1d78564
RS
5400{
5401 unsigned int regno;
5402
a92713e6
RS
5403 if (!match_reg (arg, OP_REG_GP, &regno))
5404 return FALSE;
a1d78564 5405
a1d78564 5406 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5407 return TRUE;
a1d78564
RS
5408}
5409
7361da2c
AB
5410/* OP_CHECK_PREV matcher. */
5411
5412static bfd_boolean
5413match_check_prev_operand (struct mips_arg_info *arg,
5414 const struct mips_operand *operand_base)
5415{
5416 const struct mips_check_prev_operand *operand;
5417 unsigned int regno;
5418
5419 operand = (const struct mips_check_prev_operand *) operand_base;
5420
5421 if (!match_reg (arg, OP_REG_GP, &regno))
5422 return FALSE;
5423
5424 if (!operand->zero_ok && regno == 0)
5425 return FALSE;
5426
5427 if ((operand->less_than_ok && regno < arg->last_regno)
5428 || (operand->greater_than_ok && regno > arg->last_regno)
5429 || (operand->equal_ok && regno == arg->last_regno))
5430 {
5431 arg->last_regno = regno;
5432 insn_insert_operand (arg->insn, operand_base, regno);
5433 return TRUE;
5434 }
5435
5436 return FALSE;
5437}
5438
5439/* OP_SAME_RS_RT matcher. */
5440
5441static bfd_boolean
5442match_same_rs_rt_operand (struct mips_arg_info *arg,
5443 const struct mips_operand *operand)
5444{
5445 unsigned int regno;
5446
5447 if (!match_reg (arg, OP_REG_GP, &regno))
5448 return FALSE;
5449
5450 if (regno == 0)
5451 {
5452 set_insn_error (arg->argnum, _("the source register must not be $0"));
5453 return FALSE;
5454 }
5455
5456 arg->last_regno = regno;
5457
5458 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5459 return TRUE;
5460}
5461
a1d78564
RS
5462/* OP_LWM_SWM_LIST matcher. */
5463
a92713e6 5464static bfd_boolean
a1d78564 5465match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5466 const struct mips_operand *operand)
a1d78564 5467{
a92713e6
RS
5468 unsigned int reglist, sregs, ra, regno1, regno2;
5469 struct mips_arg_info reset;
a1d78564 5470
a92713e6
RS
5471 reglist = 0;
5472 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5473 return FALSE;
5474 do
5475 {
5476 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5477 {
5478 reglist |= 1 << FP;
5479 regno2 = S7;
5480 }
5481 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5482 reset = *arg;
5483 }
5484 while (match_char (arg, ',')
5485 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5486 *arg = reset;
a1d78564
RS
5487
5488 if (operand->size == 2)
5489 {
5490 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5491
5492 s0, ra
5493 s0, s1, ra, s2, s3
5494 s0-s2, ra
5495
5496 and any permutations of these. */
5497 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5498 return FALSE;
a1d78564
RS
5499
5500 sregs = (reglist >> 17) & 7;
5501 ra = 0;
5502 }
5503 else
5504 {
5505 /* The list must include at least one of ra and s0-sN,
5506 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5507 which are $23 and $30 respectively.) E.g.:
5508
5509 ra
5510 s0
5511 ra, s0, s1, s2
5512 s0-s8
5513 s0-s5, ra
5514
5515 and any permutations of these. */
5516 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5517 return FALSE;
a1d78564
RS
5518
5519 ra = (reglist >> 27) & 0x10;
5520 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5521 }
5522 sregs += 1;
5523 if ((sregs & -sregs) != sregs)
a92713e6 5524 return FALSE;
a1d78564
RS
5525
5526 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5527 return TRUE;
a1d78564
RS
5528}
5529
364215c8
RS
5530/* OP_ENTRY_EXIT_LIST matcher. */
5531
a92713e6 5532static unsigned int
364215c8 5533match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5534 const struct mips_operand *operand)
364215c8
RS
5535{
5536 unsigned int mask;
5537 bfd_boolean is_exit;
5538
5539 /* The format is the same for both ENTRY and EXIT, but the constraints
5540 are different. */
5541 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5542 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5543 do
364215c8
RS
5544 {
5545 unsigned int regno1, regno2;
5546 bfd_boolean is_freg;
5547
a92713e6 5548 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5549 is_freg = FALSE;
a92713e6 5550 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5551 is_freg = TRUE;
5552 else
a92713e6 5553 return FALSE;
364215c8
RS
5554
5555 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5556 {
5557 mask &= ~(7 << 3);
5558 mask |= (5 + regno2) << 3;
5559 }
5560 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5561 mask |= (regno2 - 3) << 3;
5562 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5563 mask |= (regno2 - 15) << 1;
5564 else if (regno1 == RA && regno2 == RA)
5565 mask |= 1;
5566 else
a92713e6 5567 return FALSE;
364215c8 5568 }
a92713e6
RS
5569 while (match_char (arg, ','));
5570
364215c8 5571 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5572 return TRUE;
364215c8
RS
5573}
5574
38bf472a
MR
5575/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5576 the argument register mask AMASK, the number of static registers
5577 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5578 respectively, and the frame size FRAME_SIZE. */
5579
5580static unsigned int
5581mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5582 unsigned int ra, unsigned int s0, unsigned int s1,
5583 unsigned int frame_size)
5584{
5585 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5586 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5587}
5588
5589/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5590 argument register mask AMASK, the number of static registers saved
5591 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5592 respectively, and the frame size FRAME_SIZE. */
5593
5594static unsigned int
5595mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5596 unsigned int ra, unsigned int s0, unsigned int s1,
5597 unsigned int frame_size)
5598{
5599 unsigned int args;
5600
5601 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5602 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5603 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5604 | ((frame_size & 0xf0) << 16));
5605 return args;
5606}
5607
364215c8
RS
5608/* OP_SAVE_RESTORE_LIST matcher. */
5609
a92713e6
RS
5610static bfd_boolean
5611match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5612{
5613 unsigned int opcode, args, statics, sregs;
5614 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5615 unsigned int arg_mask, ra, s0, s1;
364215c8 5616 offsetT frame_size;
364215c8 5617
364215c8
RS
5618 opcode = arg->insn->insn_opcode;
5619 frame_size = 0;
5620 num_frame_sizes = 0;
5621 args = 0;
5622 statics = 0;
5623 sregs = 0;
38bf472a
MR
5624 ra = 0;
5625 s0 = 0;
5626 s1 = 0;
a92713e6 5627 do
364215c8
RS
5628 {
5629 unsigned int regno1, regno2;
5630
a92713e6 5631 if (arg->token->type == OT_INTEGER)
364215c8
RS
5632 {
5633 /* Handle the frame size. */
1a00e612 5634 if (!match_const_int (arg, &frame_size))
a92713e6 5635 return FALSE;
364215c8 5636 num_frame_sizes += 1;
364215c8
RS
5637 }
5638 else
5639 {
a92713e6
RS
5640 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5641 return FALSE;
364215c8
RS
5642
5643 while (regno1 <= regno2)
5644 {
5645 if (regno1 >= 4 && regno1 <= 7)
5646 {
5647 if (num_frame_sizes == 0)
5648 /* args $a0-$a3 */
5649 args |= 1 << (regno1 - 4);
5650 else
5651 /* statics $a0-$a3 */
5652 statics |= 1 << (regno1 - 4);
5653 }
5654 else if (regno1 >= 16 && regno1 <= 23)
5655 /* $s0-$s7 */
5656 sregs |= 1 << (regno1 - 16);
5657 else if (regno1 == 30)
5658 /* $s8 */
5659 sregs |= 1 << 8;
5660 else if (regno1 == 31)
5661 /* Add $ra to insn. */
38bf472a 5662 ra = 1;
364215c8 5663 else
a92713e6 5664 return FALSE;
364215c8
RS
5665 regno1 += 1;
5666 if (regno1 == 24)
5667 regno1 = 30;
5668 }
5669 }
364215c8 5670 }
a92713e6 5671 while (match_char (arg, ','));
364215c8
RS
5672
5673 /* Encode args/statics combination. */
5674 if (args & statics)
a92713e6 5675 return FALSE;
364215c8
RS
5676 else if (args == 0xf)
5677 /* All $a0-$a3 are args. */
38bf472a 5678 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5679 else if (statics == 0xf)
5680 /* All $a0-$a3 are statics. */
38bf472a 5681 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5682 else
5683 {
5684 /* Count arg registers. */
5685 num_args = 0;
5686 while (args & 0x1)
5687 {
5688 args >>= 1;
5689 num_args += 1;
5690 }
5691 if (args != 0)
a92713e6 5692 return FALSE;
364215c8
RS
5693
5694 /* Count static registers. */
5695 num_statics = 0;
5696 while (statics & 0x8)
5697 {
5698 statics = (statics << 1) & 0xf;
5699 num_statics += 1;
5700 }
5701 if (statics != 0)
a92713e6 5702 return FALSE;
364215c8
RS
5703
5704 /* Encode args/statics. */
38bf472a 5705 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5706 }
5707
5708 /* Encode $s0/$s1. */
5709 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5710 s0 = 1;
364215c8 5711 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5712 s1 = 1;
364215c8
RS
5713 sregs >>= 2;
5714
5715 /* Encode $s2-$s8. */
5716 num_sregs = 0;
5717 while (sregs & 1)
5718 {
5719 sregs >>= 1;
5720 num_sregs += 1;
5721 }
5722 if (sregs != 0)
a92713e6 5723 return FALSE;
364215c8
RS
5724
5725 /* Encode frame size. */
5726 if (num_frame_sizes == 0)
1a00e612
RS
5727 {
5728 set_insn_error (arg->argnum, _("missing frame size"));
5729 return FALSE;
5730 }
5731 if (num_frame_sizes > 1)
5732 {
5733 set_insn_error (arg->argnum, _("frame size specified twice"));
5734 return FALSE;
5735 }
5736 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5737 {
5738 set_insn_error (arg->argnum, _("invalid frame size"));
5739 return FALSE;
5740 }
38bf472a 5741 frame_size /= 8;
364215c8 5742
364215c8 5743 /* Finally build the instruction. */
38bf472a
MR
5744 if (mips_opts.mips16)
5745 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5746 frame_size);
5747 else if (!mips_opts.micromips)
5748 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5749 frame_size);
5750 else
5751 abort ();
5752
364215c8 5753 arg->insn->insn_opcode = opcode;
a92713e6 5754 return TRUE;
364215c8
RS
5755}
5756
a1d78564
RS
5757/* OP_MDMX_IMM_REG matcher. */
5758
a92713e6 5759static bfd_boolean
a1d78564 5760match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5761 const struct mips_operand *operand)
a1d78564 5762{
a92713e6 5763 unsigned int regno, uval;
a1d78564
RS
5764 bfd_boolean is_qh;
5765 const struct mips_opcode *opcode;
5766
5767 /* The mips_opcode records whether this is an octobyte or quadhalf
5768 instruction. Start out with that bit in place. */
5769 opcode = arg->insn->insn_mo;
5770 uval = mips_extract_operand (operand, opcode->match);
5771 is_qh = (uval != 0);
5772
56d438b1 5773 if (arg->token->type == OT_REG)
a1d78564
RS
5774 {
5775 if ((opcode->membership & INSN_5400)
5776 && strcmp (opcode->name, "rzu.ob") == 0)
5777 {
1a00e612
RS
5778 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5779 arg->argnum);
5780 return FALSE;
a1d78564
RS
5781 }
5782
56d438b1
CF
5783 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5784 return FALSE;
5785 ++arg->token;
5786
a1d78564
RS
5787 /* Check whether this is a vector register or a broadcast of
5788 a single element. */
56d438b1 5789 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5790 {
56d438b1 5791 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5792 {
1a00e612
RS
5793 set_insn_error (arg->argnum, _("invalid element selector"));
5794 return FALSE;
a1d78564 5795 }
56d438b1
CF
5796 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5797 ++arg->token;
a1d78564
RS
5798 }
5799 else
5800 {
5801 /* A full vector. */
5802 if ((opcode->membership & INSN_5400)
5803 && (strcmp (opcode->name, "sll.ob") == 0
5804 || strcmp (opcode->name, "srl.ob") == 0))
5805 {
1a00e612
RS
5806 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5807 arg->argnum);
5808 return FALSE;
a1d78564
RS
5809 }
5810
5811 if (is_qh)
5812 uval |= MDMX_FMTSEL_VEC_QH << 5;
5813 else
5814 uval |= MDMX_FMTSEL_VEC_OB << 5;
5815 }
a1d78564
RS
5816 uval |= regno;
5817 }
5818 else
5819 {
5820 offsetT sval;
5821
1a00e612 5822 if (!match_const_int (arg, &sval))
a92713e6 5823 return FALSE;
a1d78564
RS
5824 if (sval < 0 || sval > 31)
5825 {
1a00e612
RS
5826 match_out_of_range (arg);
5827 return FALSE;
a1d78564
RS
5828 }
5829 uval |= (sval & 31);
5830 if (is_qh)
5831 uval |= MDMX_FMTSEL_IMM_QH << 5;
5832 else
5833 uval |= MDMX_FMTSEL_IMM_OB << 5;
5834 }
5835 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5836 return TRUE;
a1d78564
RS
5837}
5838
56d438b1
CF
5839/* OP_IMM_INDEX matcher. */
5840
5841static bfd_boolean
5842match_imm_index_operand (struct mips_arg_info *arg,
5843 const struct mips_operand *operand)
5844{
5845 unsigned int max_val;
5846
5847 if (arg->token->type != OT_INTEGER_INDEX)
5848 return FALSE;
5849
5850 max_val = (1 << operand->size) - 1;
5851 if (arg->token->u.index > max_val)
5852 {
5853 match_out_of_range (arg);
5854 return FALSE;
5855 }
5856 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5857 ++arg->token;
5858 return TRUE;
5859}
5860
5861/* OP_REG_INDEX matcher. */
5862
5863static bfd_boolean
5864match_reg_index_operand (struct mips_arg_info *arg,
5865 const struct mips_operand *operand)
5866{
5867 unsigned int regno;
5868
5869 if (arg->token->type != OT_REG_INDEX)
5870 return FALSE;
5871
5872 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5873 return FALSE;
5874
5875 insn_insert_operand (arg->insn, operand, regno);
5876 ++arg->token;
5877 return TRUE;
5878}
5879
a1d78564
RS
5880/* OP_PC matcher. */
5881
a92713e6
RS
5882static bfd_boolean
5883match_pc_operand (struct mips_arg_info *arg)
a1d78564 5884{
a92713e6
RS
5885 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5886 {
5887 ++arg->token;
5888 return TRUE;
5889 }
5890 return FALSE;
a1d78564
RS
5891}
5892
25499ac7
MR
5893/* OP_REG28 matcher. */
5894
5895static bfd_boolean
5896match_reg28_operand (struct mips_arg_info *arg)
5897{
5898 unsigned int regno;
5899
5900 if (arg->token->type == OT_REG
5901 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5902 && regno == GP)
5903 {
5904 ++arg->token;
5905 return TRUE;
5906 }
5907 return FALSE;
5908}
5909
7361da2c
AB
5910/* OP_NON_ZERO_REG matcher. */
5911
5912static bfd_boolean
5913match_non_zero_reg_operand (struct mips_arg_info *arg,
5914 const struct mips_operand *operand)
5915{
5916 unsigned int regno;
5917
5918 if (!match_reg (arg, OP_REG_GP, &regno))
5919 return FALSE;
5920
5921 if (regno == 0)
5922 return FALSE;
5923
5924 arg->last_regno = regno;
5925 insn_insert_operand (arg->insn, operand, regno);
5926 return TRUE;
5927}
5928
a1d78564
RS
5929/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5930 register that we need to match. */
5931
a92713e6
RS
5932static bfd_boolean
5933match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5934{
5935 unsigned int regno;
5936
a92713e6 5937 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5938}
5939
33f46696
MR
5940/* Try to match a floating-point constant from ARG for LI.S or LI.D.
5941 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5942 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
5943
5944 Return the constant in IMM and OFFSET as follows:
5945
5946 - If the constant should be loaded via memory, set IMM to O_absent and
5947 OFFSET to the memory address.
5948
5949 - Otherwise, if the constant should be loaded into two 32-bit registers,
5950 set IMM to the O_constant to load into the high register and OFFSET
5951 to the corresponding value for the low register.
5952
5953 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5954
5955 These constants only appear as the last operand in an instruction,
5956 and every instruction that accepts them in any variant accepts them
5957 in all variants. This means we don't have to worry about backing out
5958 any changes if the instruction does not match. We just match
5959 unconditionally and report an error if the constant is invalid. */
5960
a92713e6
RS
5961static bfd_boolean
5962match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5963 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5964{
a92713e6 5965 char *p;
89565f1b
RS
5966 segT seg, new_seg;
5967 subsegT subseg;
5968 const char *newname;
a92713e6 5969 unsigned char *data;
89565f1b
RS
5970
5971 /* Where the constant is placed is based on how the MIPS assembler
5972 does things:
5973
5974 length == 4 && using_gprs -- immediate value only
5975 length == 8 && using_gprs -- .rdata or immediate value
5976 length == 4 && !using_gprs -- .lit4 or immediate value
5977 length == 8 && !using_gprs -- .lit8 or immediate value
5978
5979 The .lit4 and .lit8 sections are only used if permitted by the
5980 -G argument. */
a92713e6 5981 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5982 {
5983 set_insn_error (arg->argnum, _("floating-point expression required"));
5984 return FALSE;
5985 }
a92713e6
RS
5986
5987 gas_assert (arg->token->u.flt.length == length);
5988 data = arg->token->u.flt.data;
5989 ++arg->token;
89565f1b
RS
5990
5991 /* Handle 32-bit constants for which an immediate value is best. */
5992 if (length == 4
5993 && (using_gprs
5994 || g_switch_value < 4
5995 || (data[0] == 0 && data[1] == 0)
5996 || (data[2] == 0 && data[3] == 0)))
5997 {
5998 imm->X_op = O_constant;
5999 if (!target_big_endian)
6000 imm->X_add_number = bfd_getl32 (data);
6001 else
6002 imm->X_add_number = bfd_getb32 (data);
6003 offset->X_op = O_absent;
a92713e6 6004 return TRUE;
89565f1b
RS
6005 }
6006
6007 /* Handle 64-bit constants for which an immediate value is best. */
6008 if (length == 8
6009 && !mips_disable_float_construction
351cdf24
MF
6010 /* Constants can only be constructed in GPRs and copied to FPRs if the
6011 GPRs are at least as wide as the FPRs or MTHC1 is available.
6012 Unlike most tests for 32-bit floating-point registers this check
6013 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6014 permit 64-bit moves without MXHC1.
6015 Force the constant into memory otherwise. */
6016 && (using_gprs
6017 || GPR_SIZE == 64
6018 || ISA_HAS_MXHC1 (mips_opts.isa)
6019 || FPR_SIZE == 32)
89565f1b
RS
6020 && ((data[0] == 0 && data[1] == 0)
6021 || (data[2] == 0 && data[3] == 0))
6022 && ((data[4] == 0 && data[5] == 0)
6023 || (data[6] == 0 && data[7] == 0)))
6024 {
6025 /* The value is simple enough to load with a couple of instructions.
6026 If using 32-bit registers, set IMM to the high order 32 bits and
6027 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6028 64 bit constant. */
351cdf24 6029 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6030 {
6031 imm->X_op = O_constant;
6032 offset->X_op = O_constant;
6033 if (!target_big_endian)
6034 {
6035 imm->X_add_number = bfd_getl32 (data + 4);
6036 offset->X_add_number = bfd_getl32 (data);
6037 }
6038 else
6039 {
6040 imm->X_add_number = bfd_getb32 (data);
6041 offset->X_add_number = bfd_getb32 (data + 4);
6042 }
6043 if (offset->X_add_number == 0)
6044 offset->X_op = O_absent;
6045 }
6046 else
6047 {
6048 imm->X_op = O_constant;
6049 if (!target_big_endian)
6050 imm->X_add_number = bfd_getl64 (data);
6051 else
6052 imm->X_add_number = bfd_getb64 (data);
6053 offset->X_op = O_absent;
6054 }
a92713e6 6055 return TRUE;
89565f1b
RS
6056 }
6057
6058 /* Switch to the right section. */
6059 seg = now_seg;
6060 subseg = now_subseg;
6061 if (length == 4)
6062 {
6063 gas_assert (!using_gprs && g_switch_value >= 4);
6064 newname = ".lit4";
6065 }
6066 else
6067 {
6068 if (using_gprs || g_switch_value < 8)
6069 newname = RDATA_SECTION_NAME;
6070 else
6071 newname = ".lit8";
6072 }
6073
6074 new_seg = subseg_new (newname, (subsegT) 0);
6075 bfd_set_section_flags (stdoutput, new_seg,
6076 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6077 frag_align (length == 4 ? 2 : 3, 0, 0);
6078 if (strncmp (TARGET_OS, "elf", 3) != 0)
6079 record_alignment (new_seg, 4);
6080 else
6081 record_alignment (new_seg, length == 4 ? 2 : 3);
6082 if (seg == now_seg)
1661c76c 6083 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6084
6085 /* Set the argument to the current address in the section. */
6086 imm->X_op = O_absent;
6087 offset->X_op = O_symbol;
6088 offset->X_add_symbol = symbol_temp_new_now ();
6089 offset->X_add_number = 0;
6090
6091 /* Put the floating point number into the section. */
6092 p = frag_more (length);
6093 memcpy (p, data, length);
6094
6095 /* Switch back to the original section. */
6096 subseg_set (seg, subseg);
a92713e6 6097 return TRUE;
89565f1b
RS
6098}
6099
14daeee3
RS
6100/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6101 them. */
6102
6103static bfd_boolean
6104match_vu0_suffix_operand (struct mips_arg_info *arg,
6105 const struct mips_operand *operand,
6106 bfd_boolean match_p)
6107{
6108 unsigned int uval;
6109
6110 /* The operand can be an XYZW mask or a single 2-bit channel index
6111 (with X being 0). */
6112 gas_assert (operand->size == 2 || operand->size == 4);
6113
ee5734f0 6114 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6115 if (arg->token->type != OT_CHANNELS)
ee5734f0 6116 return match_p;
14daeee3
RS
6117
6118 uval = arg->token->u.channels;
6119 if (operand->size == 2)
6120 {
6121 /* Check that a single bit is set and convert it into a 2-bit index. */
6122 if ((uval & -uval) != uval)
6123 return FALSE;
6124 uval = 4 - ffs (uval);
6125 }
6126
6127 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6128 return FALSE;
6129
6130 ++arg->token;
6131 if (!match_p)
6132 insn_insert_operand (arg->insn, operand, uval);
6133 return TRUE;
6134}
6135
33f46696
MR
6136/* Try to match a token from ARG against OPERAND. Consume the token
6137 and return true on success, otherwise return false. */
a1d78564 6138
a92713e6 6139static bfd_boolean
a1d78564 6140match_operand (struct mips_arg_info *arg,
a92713e6 6141 const struct mips_operand *operand)
a1d78564
RS
6142{
6143 switch (operand->type)
6144 {
6145 case OP_INT:
a92713e6 6146 return match_int_operand (arg, operand);
a1d78564
RS
6147
6148 case OP_MAPPED_INT:
a92713e6 6149 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6150
6151 case OP_MSB:
a92713e6 6152 return match_msb_operand (arg, operand);
a1d78564
RS
6153
6154 case OP_REG:
0f35dbc4 6155 case OP_OPTIONAL_REG:
a92713e6 6156 return match_reg_operand (arg, operand);
a1d78564
RS
6157
6158 case OP_REG_PAIR:
a92713e6 6159 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6160
6161 case OP_PCREL:
a92713e6 6162 return match_pcrel_operand (arg);
a1d78564
RS
6163
6164 case OP_PERF_REG:
a92713e6 6165 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6166
6167 case OP_ADDIUSP_INT:
a92713e6 6168 return match_addiusp_operand (arg, operand);
a1d78564
RS
6169
6170 case OP_CLO_CLZ_DEST:
a92713e6 6171 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6172
6173 case OP_LWM_SWM_LIST:
a92713e6 6174 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6175
6176 case OP_ENTRY_EXIT_LIST:
a92713e6 6177 return match_entry_exit_operand (arg, operand);
364215c8 6178
a1d78564 6179 case OP_SAVE_RESTORE_LIST:
a92713e6 6180 return match_save_restore_list_operand (arg);
a1d78564
RS
6181
6182 case OP_MDMX_IMM_REG:
a92713e6 6183 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6184
6185 case OP_REPEAT_DEST_REG:
a92713e6 6186 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6187
6188 case OP_REPEAT_PREV_REG:
a92713e6 6189 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6190
6191 case OP_PC:
a92713e6 6192 return match_pc_operand (arg);
14daeee3 6193
25499ac7
MR
6194 case OP_REG28:
6195 return match_reg28_operand (arg);
6196
14daeee3
RS
6197 case OP_VU0_SUFFIX:
6198 return match_vu0_suffix_operand (arg, operand, FALSE);
6199
6200 case OP_VU0_MATCH_SUFFIX:
6201 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6202
6203 case OP_IMM_INDEX:
6204 return match_imm_index_operand (arg, operand);
6205
6206 case OP_REG_INDEX:
6207 return match_reg_index_operand (arg, operand);
7361da2c
AB
6208
6209 case OP_SAME_RS_RT:
6210 return match_same_rs_rt_operand (arg, operand);
6211
6212 case OP_CHECK_PREV:
6213 return match_check_prev_operand (arg, operand);
6214
6215 case OP_NON_ZERO_REG:
6216 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6217 }
6218 abort ();
6219}
6220
6221/* ARG is the state after successfully matching an instruction.
6222 Issue any queued-up warnings. */
6223
6224static void
6225check_completed_insn (struct mips_arg_info *arg)
6226{
6227 if (arg->seen_at)
6228 {
6229 if (AT == ATREG)
1661c76c 6230 as_warn (_("used $at without \".set noat\""));
a1d78564 6231 else
1661c76c 6232 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6233 }
6234}
a1d78564 6235
85fcb30f
RS
6236/* Return true if modifying general-purpose register REG needs a delay. */
6237
6238static bfd_boolean
6239reg_needs_delay (unsigned int reg)
6240{
6241 unsigned long prev_pinfo;
6242
6243 prev_pinfo = history[0].insn_mo->pinfo;
6244 if (!mips_opts.noreorder
67dc82bc 6245 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6246 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6247 && (gpr_write_mask (&history[0]) & (1 << reg)))
6248 return TRUE;
6249
6250 return FALSE;
6251}
6252
71400594
RS
6253/* Classify an instruction according to the FIX_VR4120_* enumeration.
6254 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6255 by VR4120 errata. */
4d7206a2 6256
71400594
RS
6257static unsigned int
6258classify_vr4120_insn (const char *name)
252b5132 6259{
71400594
RS
6260 if (strncmp (name, "macc", 4) == 0)
6261 return FIX_VR4120_MACC;
6262 if (strncmp (name, "dmacc", 5) == 0)
6263 return FIX_VR4120_DMACC;
6264 if (strncmp (name, "mult", 4) == 0)
6265 return FIX_VR4120_MULT;
6266 if (strncmp (name, "dmult", 5) == 0)
6267 return FIX_VR4120_DMULT;
6268 if (strstr (name, "div"))
6269 return FIX_VR4120_DIV;
6270 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6271 return FIX_VR4120_MTHILO;
6272 return NUM_FIX_VR4120_CLASSES;
6273}
252b5132 6274
a8d14a88
CM
6275#define INSN_ERET 0x42000018
6276#define INSN_DERET 0x4200001f
6277#define INSN_DMULT 0x1c
6278#define INSN_DMULTU 0x1d
ff239038 6279
71400594
RS
6280/* Return the number of instructions that must separate INSN1 and INSN2,
6281 where INSN1 is the earlier instruction. Return the worst-case value
6282 for any INSN2 if INSN2 is null. */
252b5132 6283
71400594
RS
6284static unsigned int
6285insns_between (const struct mips_cl_insn *insn1,
6286 const struct mips_cl_insn *insn2)
6287{
6288 unsigned long pinfo1, pinfo2;
4c260379 6289 unsigned int mask;
71400594 6290
85fcb30f
RS
6291 /* If INFO2 is null, pessimistically assume that all flags are set for
6292 the second instruction. */
71400594
RS
6293 pinfo1 = insn1->insn_mo->pinfo;
6294 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6295
71400594
RS
6296 /* For most targets, write-after-read dependencies on the HI and LO
6297 registers must be separated by at least two instructions. */
6298 if (!hilo_interlocks)
252b5132 6299 {
71400594
RS
6300 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6301 return 2;
6302 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6303 return 2;
6304 }
6305
6306 /* If we're working around r7000 errata, there must be two instructions
6307 between an mfhi or mflo and any instruction that uses the result. */
6308 if (mips_7000_hilo_fix
df58fc94 6309 && !mips_opts.micromips
71400594 6310 && MF_HILO_INSN (pinfo1)
85fcb30f 6311 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6312 return 2;
6313
ff239038
CM
6314 /* If we're working around 24K errata, one instruction is required
6315 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6316 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6317 {
6318 if (insn1->insn_opcode == INSN_ERET
6319 || insn1->insn_opcode == INSN_DERET)
6320 {
6321 if (insn2 == NULL
6322 || insn2->insn_opcode == INSN_ERET
6323 || insn2->insn_opcode == INSN_DERET
11625dd8 6324 || delayed_branch_p (insn2))
ff239038
CM
6325 return 1;
6326 }
6327 }
6328
a8d14a88
CM
6329 /* If we're working around PMC RM7000 errata, there must be three
6330 nops between a dmult and a load instruction. */
6331 if (mips_fix_rm7000 && !mips_opts.micromips)
6332 {
6333 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6334 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6335 {
6336 if (pinfo2 & INSN_LOAD_MEMORY)
6337 return 3;
6338 }
6339 }
6340
71400594
RS
6341 /* If working around VR4120 errata, check for combinations that need
6342 a single intervening instruction. */
df58fc94 6343 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6344 {
6345 unsigned int class1, class2;
252b5132 6346
71400594
RS
6347 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6348 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6349 {
71400594
RS
6350 if (insn2 == NULL)
6351 return 1;
6352 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6353 if (vr4120_conflicts[class1] & (1 << class2))
6354 return 1;
252b5132 6355 }
71400594
RS
6356 }
6357
df58fc94 6358 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6359 {
6360 /* Check for GPR or coprocessor load delays. All such delays
6361 are on the RT register. */
6362 /* Itbl support may require additional care here. */
67dc82bc 6363 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6364 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6365 {
85fcb30f 6366 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6367 return 1;
6368 }
6369
6370 /* Check for generic coprocessor hazards.
6371
6372 This case is not handled very well. There is no special
6373 knowledge of CP0 handling, and the coprocessors other than
6374 the floating point unit are not distinguished at all. */
6375 /* Itbl support may require additional care here. FIXME!
6376 Need to modify this to include knowledge about
6377 user specified delays! */
43885403 6378 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6379 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6380 {
6381 /* Handle cases where INSN1 writes to a known general coprocessor
6382 register. There must be a one instruction delay before INSN2
6383 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6384 mask = fpr_write_mask (insn1);
6385 if (mask != 0)
252b5132 6386 {
4c260379 6387 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6388 return 1;
252b5132
RH
6389 }
6390 else
6391 {
71400594
RS
6392 /* Read-after-write dependencies on the control registers
6393 require a two-instruction gap. */
6394 if ((pinfo1 & INSN_WRITE_COND_CODE)
6395 && (pinfo2 & INSN_READ_COND_CODE))
6396 return 2;
6397
6398 /* We don't know exactly what INSN1 does. If INSN2 is
6399 also a coprocessor instruction, assume there must be
6400 a one instruction gap. */
6401 if (pinfo2 & INSN_COP)
6402 return 1;
252b5132
RH
6403 }
6404 }
6b76fefe 6405
71400594
RS
6406 /* Check for read-after-write dependencies on the coprocessor
6407 control registers in cases where INSN1 does not need a general
6408 coprocessor delay. This means that INSN1 is a floating point
6409 comparison instruction. */
6410 /* Itbl support may require additional care here. */
6411 else if (!cop_interlocks
6412 && (pinfo1 & INSN_WRITE_COND_CODE)
6413 && (pinfo2 & INSN_READ_COND_CODE))
6414 return 1;
6415 }
6b76fefe 6416
7361da2c
AB
6417 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6418 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6419 and pause. */
6420 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6421 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6422 || (insn2 && delayed_branch_p (insn2))))
6423 return 1;
6424
71400594
RS
6425 return 0;
6426}
6b76fefe 6427
7d8e00cf
RS
6428/* Return the number of nops that would be needed to work around the
6429 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6430 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6431 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6432
6433static int
932d1a1b 6434nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6435 const struct mips_cl_insn *insn)
6436{
4c260379
RS
6437 int i, j;
6438 unsigned int mask;
7d8e00cf
RS
6439
6440 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6441 are not affected by the errata. */
6442 if (insn != 0
6443 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6444 || strcmp (insn->insn_mo->name, "mtlo") == 0
6445 || strcmp (insn->insn_mo->name, "mthi") == 0))
6446 return 0;
6447
6448 /* Search for the first MFLO or MFHI. */
6449 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6450 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6451 {
6452 /* Extract the destination register. */
4c260379 6453 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6454
6455 /* No nops are needed if INSN reads that register. */
4c260379 6456 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6457 return 0;
6458
6459 /* ...or if any of the intervening instructions do. */
6460 for (j = 0; j < i; j++)
4c260379 6461 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6462 return 0;
6463
932d1a1b
RS
6464 if (i >= ignore)
6465 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6466 }
6467 return 0;
6468}
6469
134c0c8b
MR
6470#define BASE_REG_EQ(INSN1, INSN2) \
6471 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6472 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6473
6474/* Return the minimum alignment for this store instruction. */
6475
6476static int
6477fix_24k_align_to (const struct mips_opcode *mo)
6478{
6479 if (strcmp (mo->name, "sh") == 0)
6480 return 2;
6481
6482 if (strcmp (mo->name, "swc1") == 0
6483 || strcmp (mo->name, "swc2") == 0
6484 || strcmp (mo->name, "sw") == 0
6485 || strcmp (mo->name, "sc") == 0
6486 || strcmp (mo->name, "s.s") == 0)
6487 return 4;
6488
6489 if (strcmp (mo->name, "sdc1") == 0
6490 || strcmp (mo->name, "sdc2") == 0
6491 || strcmp (mo->name, "s.d") == 0)
6492 return 8;
6493
6494 /* sb, swl, swr */
6495 return 1;
6496}
6497
6498struct fix_24k_store_info
6499 {
6500 /* Immediate offset, if any, for this store instruction. */
6501 short off;
6502 /* Alignment required by this store instruction. */
6503 int align_to;
6504 /* True for register offsets. */
6505 int register_offset;
6506 };
6507
6508/* Comparison function used by qsort. */
6509
6510static int
6511fix_24k_sort (const void *a, const void *b)
6512{
6513 const struct fix_24k_store_info *pos1 = a;
6514 const struct fix_24k_store_info *pos2 = b;
6515
6516 return (pos1->off - pos2->off);
6517}
6518
6519/* INSN is a store instruction. Try to record the store information
6520 in STINFO. Return false if the information isn't known. */
6521
6522static bfd_boolean
6523fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6524 const struct mips_cl_insn *insn)
15be625d
CM
6525{
6526 /* The instruction must have a known offset. */
6527 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6528 return FALSE;
6529
6530 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6531 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6532 return TRUE;
6533}
6534
932d1a1b
RS
6535/* Return the number of nops that would be needed to work around the 24k
6536 "lost data on stores during refill" errata if instruction INSN
6537 immediately followed the 2 instructions described by HIST.
6538 Ignore hazards that are contained within the first IGNORE
6539 instructions of HIST.
6540
6541 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6542 for the data cache refills and store data. The following describes
6543 the scenario where the store data could be lost.
6544
6545 * A data cache miss, due to either a load or a store, causing fill
6546 data to be supplied by the memory subsystem
6547 * The first three doublewords of fill data are returned and written
6548 into the cache
6549 * A sequence of four stores occurs in consecutive cycles around the
6550 final doubleword of the fill:
6551 * Store A
6552 * Store B
6553 * Store C
6554 * Zero, One or more instructions
6555 * Store D
6556
6557 The four stores A-D must be to different doublewords of the line that
6558 is being filled. The fourth instruction in the sequence above permits
6559 the fill of the final doubleword to be transferred from the FSB into
6560 the cache. In the sequence above, the stores may be either integer
6561 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6562 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6563 different doublewords on the line. If the floating point unit is
6564 running in 1:2 mode, it is not possible to create the sequence above
6565 using only floating point store instructions.
15be625d
CM
6566
6567 In this case, the cache line being filled is incorrectly marked
6568 invalid, thereby losing the data from any store to the line that
6569 occurs between the original miss and the completion of the five
6570 cycle sequence shown above.
6571
932d1a1b 6572 The workarounds are:
15be625d 6573
932d1a1b
RS
6574 * Run the data cache in write-through mode.
6575 * Insert a non-store instruction between
6576 Store A and Store B or Store B and Store C. */
3739860c 6577
15be625d 6578static int
932d1a1b 6579nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6580 const struct mips_cl_insn *insn)
6581{
6582 struct fix_24k_store_info pos[3];
6583 int align, i, base_offset;
6584
932d1a1b
RS
6585 if (ignore >= 2)
6586 return 0;
6587
ab9794cf
RS
6588 /* If the previous instruction wasn't a store, there's nothing to
6589 worry about. */
15be625d
CM
6590 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6591 return 0;
6592
ab9794cf
RS
6593 /* If the instructions after the previous one are unknown, we have
6594 to assume the worst. */
6595 if (!insn)
15be625d
CM
6596 return 1;
6597
ab9794cf
RS
6598 /* Check whether we are dealing with three consecutive stores. */
6599 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6600 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6601 return 0;
6602
6603 /* If we don't know the relationship between the store addresses,
6604 assume the worst. */
ab9794cf 6605 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6606 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6607 return 1;
6608
6609 if (!fix_24k_record_store_info (&pos[0], insn)
6610 || !fix_24k_record_store_info (&pos[1], &hist[0])
6611 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6612 return 1;
6613
6614 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6615
6616 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6617 X bytes and such that the base register + X is known to be aligned
6618 to align bytes. */
6619
6620 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6621 align = 8;
6622 else
6623 {
6624 align = pos[0].align_to;
6625 base_offset = pos[0].off;
6626 for (i = 1; i < 3; i++)
6627 if (align < pos[i].align_to)
6628 {
6629 align = pos[i].align_to;
6630 base_offset = pos[i].off;
6631 }
6632 for (i = 0; i < 3; i++)
6633 pos[i].off -= base_offset;
6634 }
6635
6636 pos[0].off &= ~align + 1;
6637 pos[1].off &= ~align + 1;
6638 pos[2].off &= ~align + 1;
6639
6640 /* If any two stores write to the same chunk, they also write to the
6641 same doubleword. The offsets are still sorted at this point. */
6642 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6643 return 0;
6644
6645 /* A range of at least 9 bytes is needed for the stores to be in
6646 non-overlapping doublewords. */
6647 if (pos[2].off - pos[0].off <= 8)
6648 return 0;
6649
6650 if (pos[2].off - pos[1].off >= 24
6651 || pos[1].off - pos[0].off >= 24
6652 || pos[2].off - pos[0].off >= 32)
6653 return 0;
6654
6655 return 1;
6656}
6657
71400594 6658/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6659 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6660 where HIST[0] is the most recent instruction. Ignore hazards
6661 between INSN and the first IGNORE instructions in HIST.
6662
6663 If INSN is null, return the worse-case number of nops for any
6664 instruction. */
bdaaa2e1 6665
71400594 6666static int
932d1a1b 6667nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6668 const struct mips_cl_insn *insn)
6669{
6670 int i, nops, tmp_nops;
bdaaa2e1 6671
71400594 6672 nops = 0;
932d1a1b 6673 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6674 {
91d6fa6a 6675 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6676 if (tmp_nops > nops)
6677 nops = tmp_nops;
6678 }
7d8e00cf 6679
df58fc94 6680 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6681 {
932d1a1b 6682 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6683 if (tmp_nops > nops)
6684 nops = tmp_nops;
6685 }
6686
df58fc94 6687 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6688 {
932d1a1b 6689 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6690 if (tmp_nops > nops)
6691 nops = tmp_nops;
6692 }
6693
71400594
RS
6694 return nops;
6695}
252b5132 6696
71400594 6697/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6698 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6699 would be needed after the extended sequence, ignoring hazards
6700 in the first IGNORE instructions. */
252b5132 6701
71400594 6702static int
932d1a1b
RS
6703nops_for_sequence (int num_insns, int ignore,
6704 const struct mips_cl_insn *hist, ...)
71400594
RS
6705{
6706 va_list args;
6707 struct mips_cl_insn buffer[MAX_NOPS];
6708 struct mips_cl_insn *cursor;
6709 int nops;
6710
91d6fa6a 6711 va_start (args, hist);
71400594 6712 cursor = buffer + num_insns;
91d6fa6a 6713 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6714 while (cursor > buffer)
6715 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6716
932d1a1b 6717 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6718 va_end (args);
6719 return nops;
6720}
252b5132 6721
71400594
RS
6722/* Like nops_for_insn, but if INSN is a branch, take into account the
6723 worst-case delay for the branch target. */
252b5132 6724
71400594 6725static int
932d1a1b 6726nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6727 const struct mips_cl_insn *insn)
6728{
6729 int nops, tmp_nops;
60b63b72 6730
932d1a1b 6731 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6732 if (delayed_branch_p (insn))
71400594 6733 {
932d1a1b 6734 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6735 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6736 if (tmp_nops > nops)
6737 nops = tmp_nops;
6738 }
11625dd8 6739 else if (compact_branch_p (insn))
71400594 6740 {
932d1a1b 6741 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6742 if (tmp_nops > nops)
6743 nops = tmp_nops;
6744 }
6745 return nops;
6746}
6747
c67a084a
NC
6748/* Fix NOP issue: Replace nops by "or at,at,zero". */
6749
6750static void
6751fix_loongson2f_nop (struct mips_cl_insn * ip)
6752{
df58fc94 6753 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6754 if (strcmp (ip->insn_mo->name, "nop") == 0)
6755 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6756}
6757
6758/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6759 jr target pc &= 'hffff_ffff_cfff_ffff. */
6760
6761static void
6762fix_loongson2f_jump (struct mips_cl_insn * ip)
6763{
df58fc94 6764 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6765 if (strcmp (ip->insn_mo->name, "j") == 0
6766 || strcmp (ip->insn_mo->name, "jr") == 0
6767 || strcmp (ip->insn_mo->name, "jalr") == 0)
6768 {
6769 int sreg;
6770 expressionS ep;
6771
6772 if (! mips_opts.at)
6773 return;
6774
df58fc94 6775 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6776 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6777 return;
6778
6779 ep.X_op = O_constant;
6780 ep.X_add_number = 0xcfff0000;
6781 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6782 ep.X_add_number = 0xffff;
6783 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6784 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6785 }
6786}
6787
6788static void
6789fix_loongson2f (struct mips_cl_insn * ip)
6790{
6791 if (mips_fix_loongson2f_nop)
6792 fix_loongson2f_nop (ip);
6793
6794 if (mips_fix_loongson2f_jump)
6795 fix_loongson2f_jump (ip);
6796}
6797
a4e06468
RS
6798/* IP is a branch that has a delay slot, and we need to fill it
6799 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6800 with the previous instruction.
6801 ADDRESS_EXPR is an operand of the instruction to be used with
6802 RELOC_TYPE. */
a4e06468
RS
6803
6804static bfd_boolean
e407c74b 6805can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6806 bfd_reloc_code_real_type *reloc_type)
a4e06468 6807{
2b0c8b40 6808 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6809 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6810 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6811
6812 /* -O2 and above is required for this optimization. */
6813 if (mips_optimize < 2)
6814 return FALSE;
6815
6816 /* If we have seen .set volatile or .set nomove, don't optimize. */
6817 if (mips_opts.nomove)
6818 return FALSE;
6819
6820 /* We can't swap if the previous instruction's position is fixed. */
6821 if (history[0].fixed_p)
6822 return FALSE;
6823
6824 /* If the previous previous insn was in a .set noreorder, we can't
6825 swap. Actually, the MIPS assembler will swap in this situation.
6826 However, gcc configured -with-gnu-as will generate code like
6827
6828 .set noreorder
6829 lw $4,XXX
6830 .set reorder
6831 INSN
6832 bne $4,$0,foo
6833
6834 in which we can not swap the bne and INSN. If gcc is not configured
6835 -with-gnu-as, it does not output the .set pseudo-ops. */
6836 if (history[1].noreorder_p)
6837 return FALSE;
6838
87333bb7
MR
6839 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6840 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6841 if (mips_opts.mips16 && history[0].fixp[0])
6842 return FALSE;
6843
6844 /* If the branch is itself the target of a branch, we can not swap.
6845 We cheat on this; all we check for is whether there is a label on
6846 this instruction. If there are any branches to anything other than
6847 a label, users must use .set noreorder. */
6848 if (seg_info (now_seg)->label_list)
6849 return FALSE;
6850
6851 /* If the previous instruction is in a variant frag other than this
2309ddf2 6852 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6853 MIPS16 code, which uses variant frags for different purposes. */
6854 if (!mips_opts.mips16
a4e06468
RS
6855 && history[0].frag
6856 && history[0].frag->fr_type == rs_machine_dependent)
6857 return FALSE;
6858
bcd530a7
RS
6859 /* We do not swap with instructions that cannot architecturally
6860 be placed in a branch delay slot, such as SYNC or ERET. We
6861 also refrain from swapping with a trap instruction, since it
6862 complicates trap handlers to have the trap instruction be in
6863 a delay slot. */
a4e06468 6864 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6865 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6866 return FALSE;
6867
6868 /* Check for conflicts between the branch and the instructions
6869 before the candidate delay slot. */
6870 if (nops_for_insn (0, history + 1, ip) > 0)
6871 return FALSE;
6872
6873 /* Check for conflicts between the swapped sequence and the
6874 target of the branch. */
6875 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6876 return FALSE;
6877
6878 /* If the branch reads a register that the previous
6879 instruction sets, we can not swap. */
6880 gpr_read = gpr_read_mask (ip);
6881 prev_gpr_write = gpr_write_mask (&history[0]);
6882 if (gpr_read & prev_gpr_write)
6883 return FALSE;
6884
9d5de888
CF
6885 fpr_read = fpr_read_mask (ip);
6886 prev_fpr_write = fpr_write_mask (&history[0]);
6887 if (fpr_read & prev_fpr_write)
6888 return FALSE;
6889
a4e06468
RS
6890 /* If the branch writes a register that the previous
6891 instruction sets, we can not swap. */
6892 gpr_write = gpr_write_mask (ip);
6893 if (gpr_write & prev_gpr_write)
6894 return FALSE;
6895
6896 /* If the branch writes a register that the previous
6897 instruction reads, we can not swap. */
6898 prev_gpr_read = gpr_read_mask (&history[0]);
6899 if (gpr_write & prev_gpr_read)
6900 return FALSE;
6901
6902 /* If one instruction sets a condition code and the
6903 other one uses a condition code, we can not swap. */
6904 pinfo = ip->insn_mo->pinfo;
6905 if ((pinfo & INSN_READ_COND_CODE)
6906 && (prev_pinfo & INSN_WRITE_COND_CODE))
6907 return FALSE;
6908 if ((pinfo & INSN_WRITE_COND_CODE)
6909 && (prev_pinfo & INSN_READ_COND_CODE))
6910 return FALSE;
6911
6912 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6913 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6914 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6915 return FALSE;
a4e06468 6916
df58fc94
RS
6917 /* If the previous instruction has an incorrect size for a fixed
6918 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6919 pinfo2 = ip->insn_mo->pinfo2;
6920 if (mips_opts.micromips
6921 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6922 && insn_length (history) != 2)
6923 return FALSE;
6924 if (mips_opts.micromips
6925 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6926 && insn_length (history) != 4)
6927 return FALSE;
6928
e407c74b
NC
6929 /* On R5900 short loops need to be fixed by inserting a nop in
6930 the branch delay slots.
6931 A short loop can be terminated too early. */
6932 if (mips_opts.arch == CPU_R5900
6933 /* Check if instruction has a parameter, ignore "j $31". */
6934 && (address_expr != NULL)
6935 /* Parameter must be 16 bit. */
6936 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6937 /* Branch to same segment. */
41065f5e 6938 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6939 /* Branch to same code fragment. */
41065f5e 6940 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6941 /* Can only calculate branch offset if value is known. */
41065f5e 6942 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6943 /* Check if branch is really conditional. */
6944 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6945 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6946 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6947 {
6948 int distance;
6949 /* Check if loop is shorter than 6 instructions including
6950 branch and delay slot. */
41065f5e 6951 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6952 if (distance <= 20)
6953 {
6954 int i;
6955 int rv;
6956
6957 rv = FALSE;
6958 /* When the loop includes branches or jumps,
6959 it is not a short loop. */
6960 for (i = 0; i < (distance / 4); i++)
6961 {
6962 if ((history[i].cleared_p)
41065f5e 6963 || delayed_branch_p (&history[i]))
e407c74b
NC
6964 {
6965 rv = TRUE;
6966 break;
6967 }
6968 }
535b785f 6969 if (!rv)
e407c74b
NC
6970 {
6971 /* Insert nop after branch to fix short loop. */
6972 return FALSE;
6973 }
6974 }
6975 }
6976
a4e06468
RS
6977 return TRUE;
6978}
6979
e407c74b
NC
6980/* Decide how we should add IP to the instruction stream.
6981 ADDRESS_EXPR is an operand of the instruction to be used with
6982 RELOC_TYPE. */
a4e06468
RS
6983
6984static enum append_method
e407c74b 6985get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6986 bfd_reloc_code_real_type *reloc_type)
a4e06468 6987{
a4e06468
RS
6988 /* The relaxed version of a macro sequence must be inherently
6989 hazard-free. */
6990 if (mips_relax.sequence == 2)
6991 return APPEND_ADD;
6992
3b821a28 6993 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
6994 if (mips_opts.noreorder)
6995 return APPEND_ADD;
6996
6997 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6998 if (delayed_branch_p (ip))
a4e06468 6999 {
e407c74b
NC
7000 if (!branch_likely_p (ip)
7001 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7002 return APPEND_SWAP;
7003
7004 if (mips_opts.mips16
7005 && ISA_SUPPORTS_MIPS16E
fc76e730 7006 && gpr_read_mask (ip) != 0)
a4e06468
RS
7007 return APPEND_ADD_COMPACT;
7008
7bd374a4
MR
7009 if (mips_opts.micromips
7010 && ((ip->insn_opcode & 0xffe0) == 0x4580
7011 || (!forced_insn_length
7012 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7013 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7014 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7015 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7016 return APPEND_ADD_COMPACT;
7017
a4e06468
RS
7018 return APPEND_ADD_WITH_NOP;
7019 }
7020
a4e06468
RS
7021 return APPEND_ADD;
7022}
7023
7bd374a4
MR
7024/* IP is an instruction whose opcode we have just changed, END points
7025 to the end of the opcode table processed. Point IP->insn_mo to the
7026 new opcode's definition. */
ceb94aa5
RS
7027
7028static void
7bd374a4 7029find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7030{
7bd374a4 7031 const struct mips_opcode *mo;
ceb94aa5 7032
ceb94aa5 7033 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7034 if (mo->pinfo != INSN_MACRO
7035 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7036 {
7037 ip->insn_mo = mo;
7038 return;
7039 }
7040 abort ();
7041}
7042
7bd374a4
MR
7043/* IP is a MIPS16 instruction whose opcode we have just changed.
7044 Point IP->insn_mo to the new opcode's definition. */
7045
7046static void
7047find_altered_mips16_opcode (struct mips_cl_insn *ip)
7048{
7049 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7050}
7051
7052/* IP is a microMIPS instruction whose opcode we have just changed.
7053 Point IP->insn_mo to the new opcode's definition. */
7054
7055static void
7056find_altered_micromips_opcode (struct mips_cl_insn *ip)
7057{
7058 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7059}
7060
df58fc94
RS
7061/* For microMIPS macros, we need to generate a local number label
7062 as the target of branches. */
7063#define MICROMIPS_LABEL_CHAR '\037'
7064static unsigned long micromips_target_label;
7065static char micromips_target_name[32];
7066
7067static char *
7068micromips_label_name (void)
7069{
7070 char *p = micromips_target_name;
7071 char symbol_name_temporary[24];
7072 unsigned long l;
7073 int i;
7074
7075 if (*p)
7076 return p;
7077
7078 i = 0;
7079 l = micromips_target_label;
7080#ifdef LOCAL_LABEL_PREFIX
7081 *p++ = LOCAL_LABEL_PREFIX;
7082#endif
7083 *p++ = 'L';
7084 *p++ = MICROMIPS_LABEL_CHAR;
7085 do
7086 {
7087 symbol_name_temporary[i++] = l % 10 + '0';
7088 l /= 10;
7089 }
7090 while (l != 0);
7091 while (i > 0)
7092 *p++ = symbol_name_temporary[--i];
7093 *p = '\0';
7094
7095 return micromips_target_name;
7096}
7097
7098static void
7099micromips_label_expr (expressionS *label_expr)
7100{
7101 label_expr->X_op = O_symbol;
7102 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7103 label_expr->X_add_number = 0;
7104}
7105
7106static void
7107micromips_label_inc (void)
7108{
7109 micromips_target_label++;
7110 *micromips_target_name = '\0';
7111}
7112
7113static void
7114micromips_add_label (void)
7115{
7116 symbolS *s;
7117
7118 s = colon (micromips_label_name ());
7119 micromips_label_inc ();
f3ded42a 7120 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7121}
7122
7123/* If assembling microMIPS code, then return the microMIPS reloc
7124 corresponding to the requested one if any. Otherwise return
7125 the reloc unchanged. */
7126
7127static bfd_reloc_code_real_type
7128micromips_map_reloc (bfd_reloc_code_real_type reloc)
7129{
7130 static const bfd_reloc_code_real_type relocs[][2] =
7131 {
7132 /* Keep sorted incrementally by the left-hand key. */
7133 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7134 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7135 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7136 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7137 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7138 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7139 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7140 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7141 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7142 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7143 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7144 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7145 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7146 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7147 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7148 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7149 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7150 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7151 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7152 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7153 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7154 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7155 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7156 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7157 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7158 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7159 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7160 };
7161 bfd_reloc_code_real_type r;
7162 size_t i;
7163
7164 if (!mips_opts.micromips)
7165 return reloc;
7166 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7167 {
7168 r = relocs[i][0];
7169 if (r > reloc)
7170 return reloc;
7171 if (r == reloc)
7172 return relocs[i][1];
7173 }
7174 return reloc;
7175}
7176
b886a2ab
RS
7177/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7178 Return true on success, storing the resolved value in RESULT. */
7179
7180static bfd_boolean
7181calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7182 offsetT *result)
7183{
7184 switch (reloc)
7185 {
7186 case BFD_RELOC_MIPS_HIGHEST:
7187 case BFD_RELOC_MICROMIPS_HIGHEST:
7188 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7189 return TRUE;
7190
7191 case BFD_RELOC_MIPS_HIGHER:
7192 case BFD_RELOC_MICROMIPS_HIGHER:
7193 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7194 return TRUE;
7195
7196 case BFD_RELOC_HI16_S:
41947d9e 7197 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7198 case BFD_RELOC_MICROMIPS_HI16_S:
7199 case BFD_RELOC_MIPS16_HI16_S:
7200 *result = ((operand + 0x8000) >> 16) & 0xffff;
7201 return TRUE;
7202
7203 case BFD_RELOC_HI16:
7204 case BFD_RELOC_MICROMIPS_HI16:
7205 case BFD_RELOC_MIPS16_HI16:
7206 *result = (operand >> 16) & 0xffff;
7207 return TRUE;
7208
7209 case BFD_RELOC_LO16:
41947d9e 7210 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7211 case BFD_RELOC_MICROMIPS_LO16:
7212 case BFD_RELOC_MIPS16_LO16:
7213 *result = operand & 0xffff;
7214 return TRUE;
7215
7216 case BFD_RELOC_UNUSED:
7217 *result = operand;
7218 return TRUE;
7219
7220 default:
7221 return FALSE;
7222 }
7223}
7224
71400594
RS
7225/* Output an instruction. IP is the instruction information.
7226 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7227 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7228 a macro expansion. */
71400594
RS
7229
7230static void
7231append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7232 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7233{
14fe068b 7234 unsigned long prev_pinfo2, pinfo;
71400594 7235 bfd_boolean relaxed_branch = FALSE;
a4e06468 7236 enum append_method method;
2309ddf2 7237 bfd_boolean relax32;
2b0c8b40 7238 int branch_disp;
71400594 7239
2309ddf2 7240 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7241 fix_loongson2f (ip);
7242
738f4d98 7243 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7244 file_ase_micromips |= mips_opts.micromips;
738f4d98 7245
df58fc94 7246 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7247 pinfo = ip->insn_mo->pinfo;
df58fc94 7248
7bd374a4
MR
7249 /* Don't raise alarm about `nods' frags as they'll fill in the right
7250 kind of nop in relaxation if required. */
df58fc94
RS
7251 if (mips_opts.micromips
7252 && !expansionp
7bd374a4
MR
7253 && !(history[0].frag
7254 && history[0].frag->fr_type == rs_machine_dependent
7255 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7256 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7257 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7258 && micromips_insn_length (ip->insn_mo) != 2)
7259 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7260 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7261 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7262 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7263
15be625d
CM
7264 if (address_expr == NULL)
7265 ip->complete_p = 1;
b886a2ab
RS
7266 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7267 && reloc_type[1] == BFD_RELOC_UNUSED
7268 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7269 && address_expr->X_op == O_constant)
7270 {
15be625d
CM
7271 switch (*reloc_type)
7272 {
15be625d 7273 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7274 {
7275 int shift;
7276
17c6c9d9
MR
7277 /* Shift is 2, unusually, for microMIPS JALX. */
7278 shift = (mips_opts.micromips
7279 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7280 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7281 as_bad (_("jump to misaligned address (0x%lx)"),
7282 (unsigned long) address_expr->X_add_number);
7283 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7284 & 0x3ffffff);
335574df 7285 ip->complete_p = 1;
df58fc94 7286 }
15be625d
CM
7287 break;
7288
7289 case BFD_RELOC_MIPS16_JMP:
7290 if ((address_expr->X_add_number & 3) != 0)
7291 as_bad (_("jump to misaligned address (0x%lx)"),
7292 (unsigned long) address_expr->X_add_number);
7293 ip->insn_opcode |=
7294 (((address_expr->X_add_number & 0x7c0000) << 3)
7295 | ((address_expr->X_add_number & 0xf800000) >> 7)
7296 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7297 ip->complete_p = 1;
15be625d
CM
7298 break;
7299
7300 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7301 {
7302 int shift;
7303
7304 shift = mips_opts.micromips ? 1 : 2;
7305 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7306 as_bad (_("branch to misaligned address (0x%lx)"),
7307 (unsigned long) address_expr->X_add_number);
7308 if (!mips_relax_branch)
7309 {
7310 if ((address_expr->X_add_number + (1 << (shift + 15)))
7311 & ~((1 << (shift + 16)) - 1))
7312 as_bad (_("branch address range overflow (0x%lx)"),
7313 (unsigned long) address_expr->X_add_number);
7314 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7315 & 0xffff);
7316 }
df58fc94 7317 }
15be625d
CM
7318 break;
7319
7361da2c
AB
7320 case BFD_RELOC_MIPS_21_PCREL_S2:
7321 {
7322 int shift;
7323
7324 shift = 2;
7325 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7326 as_bad (_("branch to misaligned address (0x%lx)"),
7327 (unsigned long) address_expr->X_add_number);
7328 if ((address_expr->X_add_number + (1 << (shift + 20)))
7329 & ~((1 << (shift + 21)) - 1))
7330 as_bad (_("branch address range overflow (0x%lx)"),
7331 (unsigned long) address_expr->X_add_number);
7332 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7333 & 0x1fffff);
7334 }
7335 break;
7336
7337 case BFD_RELOC_MIPS_26_PCREL_S2:
7338 {
7339 int shift;
7340
7341 shift = 2;
7342 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7343 as_bad (_("branch to misaligned address (0x%lx)"),
7344 (unsigned long) address_expr->X_add_number);
7345 if ((address_expr->X_add_number + (1 << (shift + 25)))
7346 & ~((1 << (shift + 26)) - 1))
7347 as_bad (_("branch address range overflow (0x%lx)"),
7348 (unsigned long) address_expr->X_add_number);
7349 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7350 & 0x3ffffff);
7351 }
7352 break;
7353
15be625d 7354 default:
b886a2ab
RS
7355 {
7356 offsetT value;
7357
7358 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7359 &value))
7360 {
7361 ip->insn_opcode |= value & 0xffff;
7362 ip->complete_p = 1;
7363 }
7364 }
7365 break;
7366 }
15be625d
CM
7367 }
7368
71400594
RS
7369 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7370 {
7371 /* There are a lot of optimizations we could do that we don't.
7372 In particular, we do not, in general, reorder instructions.
7373 If you use gcc with optimization, it will reorder
7374 instructions and generally do much more optimization then we
7375 do here; repeating all that work in the assembler would only
7376 benefit hand written assembly code, and does not seem worth
7377 it. */
7378 int nops = (mips_optimize == 0
932d1a1b
RS
7379 ? nops_for_insn (0, history, NULL)
7380 : nops_for_insn_or_target (0, history, ip));
71400594 7381 if (nops > 0)
252b5132
RH
7382 {
7383 fragS *old_frag;
7384 unsigned long old_frag_offset;
7385 int i;
252b5132
RH
7386
7387 old_frag = frag_now;
7388 old_frag_offset = frag_now_fix ();
7389
7390 for (i = 0; i < nops; i++)
14fe068b
RS
7391 add_fixed_insn (NOP_INSN);
7392 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7393
7394 if (listing)
7395 {
7396 listing_prev_line ();
7397 /* We may be at the start of a variant frag. In case we
7398 are, make sure there is enough space for the frag
7399 after the frags created by listing_prev_line. The
7400 argument to frag_grow here must be at least as large
7401 as the argument to all other calls to frag_grow in
7402 this file. We don't have to worry about being in the
7403 middle of a variant frag, because the variants insert
7404 all needed nop instructions themselves. */
7405 frag_grow (40);
7406 }
7407
462427c4 7408 mips_move_text_labels ();
252b5132
RH
7409
7410#ifndef NO_ECOFF_DEBUGGING
7411 if (ECOFF_DEBUGGING)
7412 ecoff_fix_loc (old_frag, old_frag_offset);
7413#endif
7414 }
71400594
RS
7415 }
7416 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7417 {
932d1a1b
RS
7418 int nops;
7419
7420 /* Work out how many nops in prev_nop_frag are needed by IP,
7421 ignoring hazards generated by the first prev_nop_frag_since
7422 instructions. */
7423 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7424 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7425
71400594
RS
7426 /* Enforce NOPS as a minimum. */
7427 if (nops > prev_nop_frag_required)
7428 prev_nop_frag_required = nops;
252b5132 7429
71400594
RS
7430 if (prev_nop_frag_holds == prev_nop_frag_required)
7431 {
7432 /* Settle for the current number of nops. Update the history
7433 accordingly (for the benefit of any future .set reorder code). */
7434 prev_nop_frag = NULL;
7435 insert_into_history (prev_nop_frag_since,
7436 prev_nop_frag_holds, NOP_INSN);
7437 }
7438 else
7439 {
7440 /* Allow this instruction to replace one of the nops that was
7441 tentatively added to prev_nop_frag. */
df58fc94 7442 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7443 prev_nop_frag_holds--;
7444 prev_nop_frag_since++;
252b5132
RH
7445 }
7446 }
7447
e407c74b 7448 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7449 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7450
e410add4
RS
7451 dwarf2_emit_insn (0);
7452 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7453 so "move" the instruction address accordingly.
7454
7455 Also, it doesn't seem appropriate for the assembler to reorder .loc
7456 entries. If this instruction is a branch that we are going to swap
7457 with the previous instruction, the two instructions should be
7458 treated as a unit, and the debug information for both instructions
7459 should refer to the start of the branch sequence. Using the
7460 current position is certainly wrong when swapping a 32-bit branch
7461 and a 16-bit delay slot, since the current position would then be
7462 in the middle of a branch. */
7463 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7464
df58fc94
RS
7465 relax32 = (mips_relax_branch
7466 /* Don't try branch relaxation within .set nomacro, or within
7467 .set noat if we use $at for PIC computations. If it turns
7468 out that the branch was out-of-range, we'll get an error. */
7469 && !mips_opts.warn_about_macros
7470 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7471 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7472 as they have no complementing branches. */
7473 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7474
7475 if (!HAVE_CODE_COMPRESSION
7476 && address_expr
7477 && relax32
0b25d3e6 7478 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7479 && delayed_branch_p (ip))
4a6a3df4 7480 {
895921c9 7481 relaxed_branch = TRUE;
1e915849
RS
7482 add_relaxed_insn (ip, (relaxed_branch_length
7483 (NULL, NULL,
11625dd8
RS
7484 uncond_branch_p (ip) ? -1
7485 : branch_likely_p (ip) ? 1
1e915849
RS
7486 : 0)), 4,
7487 RELAX_BRANCH_ENCODE
ce8ad872 7488 (AT, mips_pic != NO_PIC,
11625dd8
RS
7489 uncond_branch_p (ip),
7490 branch_likely_p (ip),
1e915849
RS
7491 pinfo & INSN_WRITE_GPR_31,
7492 0),
7493 address_expr->X_add_symbol,
7494 address_expr->X_add_number);
4a6a3df4
AO
7495 *reloc_type = BFD_RELOC_UNUSED;
7496 }
df58fc94
RS
7497 else if (mips_opts.micromips
7498 && address_expr
7499 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7500 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7501 && (delayed_branch_p (ip) || compact_branch_p (ip))
7502 /* Don't try branch relaxation when users specify
7503 16-bit/32-bit instructions. */
7504 && !forced_insn_length)
df58fc94 7505 {
7bd374a4
MR
7506 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7507 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7508 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7509 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7510 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7511 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7512 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7513 int length32 = nods ? 8 : 4;
df58fc94
RS
7514
7515 gas_assert (address_expr != NULL);
7516 gas_assert (!mips_relax.sequence);
7517
2b0c8b40 7518 relaxed_branch = TRUE;
7bd374a4
MR
7519 if (nods)
7520 method = APPEND_ADD;
7521 if (relax32)
7522 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7523 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7524 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7525 mips_pic != NO_PIC,
7bd374a4 7526 uncond, compact, al, nods,
40209cad 7527 relax32, 0, 0),
df58fc94
RS
7528 address_expr->X_add_symbol,
7529 address_expr->X_add_number);
7530 *reloc_type = BFD_RELOC_UNUSED;
7531 }
7532 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7533 {
7fd53920
MR
7534 bfd_boolean require_unextended;
7535 bfd_boolean require_extended;
88a7ef16
MR
7536 symbolS *symbol;
7537 offsetT offset;
7538
7fd53920
MR
7539 if (forced_insn_length != 0)
7540 {
7541 require_unextended = forced_insn_length == 2;
7542 require_extended = forced_insn_length == 4;
7543 }
7544 else
7545 {
7546 require_unextended = (mips_opts.noautoextend
7547 && !mips_opcode_32bit_p (ip->insn_mo));
7548 require_extended = 0;
7549 }
7550
252b5132 7551 /* We need to set up a variant frag. */
df58fc94 7552 gas_assert (address_expr != NULL);
88a7ef16
MR
7553 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7554 symbol created by `make_expr_symbol' may not get a necessary
7555 external relocation produced. */
7556 if (address_expr->X_op == O_symbol)
7557 {
7558 symbol = address_expr->X_add_symbol;
7559 offset = address_expr->X_add_number;
7560 }
7561 else
7562 {
7563 symbol = make_expr_symbol (address_expr);
82d808ed 7564 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7565 offset = 0;
7566 }
8507b6e7 7567 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7568 RELAX_MIPS16_ENCODE
7569 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7570 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7571 mips_pic != NO_PIC,
7572 HAVE_32BIT_SYMBOLS,
7573 mips_opts.warn_about_macros,
7fd53920 7574 require_unextended, require_extended,
11625dd8 7575 delayed_branch_p (&history[0]),
1e915849 7576 history[0].mips16_absolute_jump_p),
88a7ef16 7577 symbol, offset);
252b5132 7578 }
5c04167a 7579 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7580 {
11625dd8 7581 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7582 /* Make sure there is enough room to swap this instruction with
7583 a following jump instruction. */
7584 frag_grow (6);
1e915849 7585 add_fixed_insn (ip);
252b5132
RH
7586 }
7587 else
7588 {
7589 if (mips_opts.mips16
7590 && mips_opts.noreorder
11625dd8 7591 && delayed_branch_p (&history[0]))
252b5132
RH
7592 as_warn (_("extended instruction in delay slot"));
7593
4d7206a2
RS
7594 if (mips_relax.sequence)
7595 {
7596 /* If we've reached the end of this frag, turn it into a variant
7597 frag and record the information for the instructions we've
7598 written so far. */
7599 if (frag_room () < 4)
7600 relax_close_frag ();
df58fc94 7601 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7602 }
7603
584892a6 7604 if (mips_relax.sequence != 2)
df58fc94
RS
7605 {
7606 if (mips_macro_warning.first_insn_sizes[0] == 0)
7607 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7608 mips_macro_warning.sizes[0] += insn_length (ip);
7609 mips_macro_warning.insns[0]++;
7610 }
584892a6 7611 if (mips_relax.sequence != 1)
df58fc94
RS
7612 {
7613 if (mips_macro_warning.first_insn_sizes[1] == 0)
7614 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7615 mips_macro_warning.sizes[1] += insn_length (ip);
7616 mips_macro_warning.insns[1]++;
7617 }
584892a6 7618
1e915849
RS
7619 if (mips_opts.mips16)
7620 {
7621 ip->fixed_p = 1;
7622 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7623 }
7624 add_fixed_insn (ip);
252b5132
RH
7625 }
7626
9fe77896 7627 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7628 {
df58fc94 7629 bfd_reloc_code_real_type final_type[3];
2309ddf2 7630 reloc_howto_type *howto0;
9fe77896
RS
7631 reloc_howto_type *howto;
7632 int i;
34ce925e 7633
df58fc94
RS
7634 /* Perform any necessary conversion to microMIPS relocations
7635 and find out how many relocations there actually are. */
7636 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7637 final_type[i] = micromips_map_reloc (reloc_type[i]);
7638
9fe77896
RS
7639 /* In a compound relocation, it is the final (outermost)
7640 operator that determines the relocated field. */
2309ddf2 7641 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7642 if (!howto)
7643 abort ();
2309ddf2
MR
7644
7645 if (i > 1)
7646 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7647 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7648 bfd_get_reloc_size (howto),
7649 address_expr,
2309ddf2
MR
7650 howto0 && howto0->pc_relative,
7651 final_type[0]);
ce8ad872
MR
7652 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7653 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7654
7655 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7656 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7657 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7658
7659 /* These relocations can have an addend that won't fit in
7660 4 octets for 64bit assembly. */
bad1aba3 7661 if (GPR_SIZE == 64
9fe77896
RS
7662 && ! howto->partial_inplace
7663 && (reloc_type[0] == BFD_RELOC_16
7664 || reloc_type[0] == BFD_RELOC_32
7665 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7666 || reloc_type[0] == BFD_RELOC_GPREL16
7667 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7668 || reloc_type[0] == BFD_RELOC_GPREL32
7669 || reloc_type[0] == BFD_RELOC_64
7670 || reloc_type[0] == BFD_RELOC_CTOR
7671 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7672 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7673 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7674 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7675 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7676 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7677 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7678 || hi16_reloc_p (reloc_type[0])
7679 || lo16_reloc_p (reloc_type[0])))
7680 ip->fixp[0]->fx_no_overflow = 1;
7681
ddaf2c41
MR
7682 /* These relocations can have an addend that won't fit in 2 octets. */
7683 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7684 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7685 ip->fixp[0]->fx_no_overflow = 1;
7686
9fe77896
RS
7687 if (mips_relax.sequence)
7688 {
7689 if (mips_relax.first_fixup == 0)
7690 mips_relax.first_fixup = ip->fixp[0];
7691 }
7692 else if (reloc_needs_lo_p (*reloc_type))
7693 {
7694 struct mips_hi_fixup *hi_fixup;
7695
7696 /* Reuse the last entry if it already has a matching %lo. */
7697 hi_fixup = mips_hi_fixup_list;
7698 if (hi_fixup == 0
7699 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7700 {
325801bd 7701 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7702 hi_fixup->next = mips_hi_fixup_list;
7703 mips_hi_fixup_list = hi_fixup;
4d7206a2 7704 }
9fe77896
RS
7705 hi_fixup->fixp = ip->fixp[0];
7706 hi_fixup->seg = now_seg;
7707 }
252b5132 7708
9fe77896
RS
7709 /* Add fixups for the second and third relocations, if given.
7710 Note that the ABI allows the second relocation to be
7711 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7712 moment we only use RSS_UNDEF, but we could add support
7713 for the others if it ever becomes necessary. */
7714 for (i = 1; i < 3; i++)
7715 if (reloc_type[i] != BFD_RELOC_UNUSED)
7716 {
7717 ip->fixp[i] = fix_new (ip->frag, ip->where,
7718 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7719 FALSE, final_type[i]);
f6688943 7720
9fe77896
RS
7721 /* Use fx_tcbit to mark compound relocs. */
7722 ip->fixp[0]->fx_tcbit = 1;
7723 ip->fixp[i]->fx_tcbit = 1;
7724 }
252b5132 7725 }
252b5132
RH
7726
7727 /* Update the register mask information. */
4c260379
RS
7728 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7729 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7730
a4e06468 7731 switch (method)
252b5132 7732 {
a4e06468
RS
7733 case APPEND_ADD:
7734 insert_into_history (0, 1, ip);
7735 break;
7736
7737 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7738 {
7739 struct mips_cl_insn *nop;
7740
7741 insert_into_history (0, 1, ip);
7742 nop = get_delay_slot_nop (ip);
7743 add_fixed_insn (nop);
7744 insert_into_history (0, 1, nop);
7745 if (mips_relax.sequence)
7746 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7747 }
a4e06468
RS
7748 break;
7749
7750 case APPEND_ADD_COMPACT:
7751 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7752 if (mips_opts.mips16)
7753 {
7754 ip->insn_opcode |= 0x0080;
7755 find_altered_mips16_opcode (ip);
7756 }
7757 /* Convert microMIPS instructions. */
7758 else if (mips_opts.micromips)
7759 {
7760 /* jr16->jrc */
7761 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7762 ip->insn_opcode |= 0x0020;
7763 /* b16->bc */
7764 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7765 ip->insn_opcode = 0x40e00000;
7766 /* beqz16->beqzc, bnez16->bnezc */
7767 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7768 {
7769 unsigned long regno;
7770
7771 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7772 regno &= MICROMIPSOP_MASK_MD;
7773 regno = micromips_to_32_reg_d_map[regno];
7774 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7775 | (regno << MICROMIPSOP_SH_RS)
7776 | 0x40a00000) ^ 0x00400000;
7777 }
7778 /* beqz->beqzc, bnez->bnezc */
7779 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7780 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7781 | ((ip->insn_opcode >> 7) & 0x00400000)
7782 | 0x40a00000) ^ 0x00400000;
7783 /* beq $0->beqzc, bne $0->bnezc */
7784 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7785 ip->insn_opcode = (((ip->insn_opcode >>
7786 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7787 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7788 | ((ip->insn_opcode >> 7) & 0x00400000)
7789 | 0x40a00000) ^ 0x00400000;
7790 else
7791 abort ();
7792 find_altered_micromips_opcode (ip);
7793 }
7794 else
7795 abort ();
a4e06468
RS
7796 install_insn (ip);
7797 insert_into_history (0, 1, ip);
7798 break;
7799
7800 case APPEND_SWAP:
7801 {
7802 struct mips_cl_insn delay = history[0];
99e7978b
MF
7803
7804 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7805 {
7806 /* Add the delay slot instruction to the end of the
7807 current frag and shrink the fixed part of the
7808 original frag. If the branch occupies the tail of
7809 the latter, move it backwards to cover the gap. */
2b0c8b40 7810 delay.frag->fr_fix -= branch_disp;
a4e06468 7811 if (delay.frag == ip->frag)
2b0c8b40 7812 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7813 add_fixed_insn (&delay);
7814 }
7815 else
7816 {
5e35670b
MR
7817 /* If this is not a relaxed branch and we are in the
7818 same frag, then just swap the instructions. */
7819 move_insn (ip, delay.frag, delay.where);
7820 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7821 }
7822 history[0] = *ip;
7823 delay.fixed_p = 1;
7824 insert_into_history (0, 1, &delay);
7825 }
7826 break;
252b5132
RH
7827 }
7828
13408f1e 7829 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7830 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7831 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7832 {
7833 unsigned int i;
7834
79850f26 7835 mips_no_prev_insn ();
13408f1e 7836
e407c74b 7837 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7838 history[i].cleared_p = 1;
e407c74b
NC
7839 }
7840
df58fc94
RS
7841 /* We need to emit a label at the end of branch-likely macros. */
7842 if (emit_branch_likely_macro)
7843 {
7844 emit_branch_likely_macro = FALSE;
7845 micromips_add_label ();
7846 }
7847
252b5132
RH
7848 /* We just output an insn, so the next one doesn't have a label. */
7849 mips_clear_insn_labels ();
252b5132
RH
7850}
7851
e407c74b
NC
7852/* Forget that there was any previous instruction or label.
7853 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7854
7855static void
7d10b47d 7856mips_no_prev_insn (void)
252b5132 7857{
7d10b47d
RS
7858 prev_nop_frag = NULL;
7859 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7860 mips_clear_insn_labels ();
7861}
7862
7d10b47d
RS
7863/* This function must be called before we emit something other than
7864 instructions. It is like mips_no_prev_insn except that it inserts
7865 any NOPS that might be needed by previous instructions. */
252b5132 7866
7d10b47d
RS
7867void
7868mips_emit_delays (void)
252b5132
RH
7869{
7870 if (! mips_opts.noreorder)
7871 {
932d1a1b 7872 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7873 if (nops > 0)
7874 {
7d10b47d
RS
7875 while (nops-- > 0)
7876 add_fixed_insn (NOP_INSN);
462427c4 7877 mips_move_text_labels ();
7d10b47d
RS
7878 }
7879 }
7880 mips_no_prev_insn ();
7881}
7882
7883/* Start a (possibly nested) noreorder block. */
7884
7885static void
7886start_noreorder (void)
7887{
7888 if (mips_opts.noreorder == 0)
7889 {
7890 unsigned int i;
7891 int nops;
7892
7893 /* None of the instructions before the .set noreorder can be moved. */
7894 for (i = 0; i < ARRAY_SIZE (history); i++)
7895 history[i].fixed_p = 1;
7896
7897 /* Insert any nops that might be needed between the .set noreorder
7898 block and the previous instructions. We will later remove any
7899 nops that turn out not to be needed. */
932d1a1b 7900 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7901 if (nops > 0)
7902 {
7903 if (mips_optimize != 0)
252b5132
RH
7904 {
7905 /* Record the frag which holds the nop instructions, so
7906 that we can remove them if we don't need them. */
df58fc94 7907 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7908 prev_nop_frag = frag_now;
7909 prev_nop_frag_holds = nops;
7910 prev_nop_frag_required = 0;
7911 prev_nop_frag_since = 0;
7912 }
7913
7914 for (; nops > 0; --nops)
1e915849 7915 add_fixed_insn (NOP_INSN);
252b5132 7916
7d10b47d
RS
7917 /* Move on to a new frag, so that it is safe to simply
7918 decrease the size of prev_nop_frag. */
7919 frag_wane (frag_now);
7920 frag_new (0);
462427c4 7921 mips_move_text_labels ();
252b5132 7922 }
df58fc94 7923 mips_mark_labels ();
7d10b47d 7924 mips_clear_insn_labels ();
252b5132 7925 }
7d10b47d
RS
7926 mips_opts.noreorder++;
7927 mips_any_noreorder = 1;
7928}
252b5132 7929
7d10b47d 7930/* End a nested noreorder block. */
252b5132 7931
7d10b47d
RS
7932static void
7933end_noreorder (void)
7934{
7935 mips_opts.noreorder--;
7936 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7937 {
7938 /* Commit to inserting prev_nop_frag_required nops and go back to
7939 handling nop insertion the .set reorder way. */
7940 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7941 * NOP_INSN_SIZE);
7d10b47d
RS
7942 insert_into_history (prev_nop_frag_since,
7943 prev_nop_frag_required, NOP_INSN);
7944 prev_nop_frag = NULL;
7945 }
252b5132
RH
7946}
7947
97d87491
RS
7948/* Sign-extend 32-bit mode constants that have bit 31 set and all
7949 higher bits unset. */
7950
7951static void
7952normalize_constant_expr (expressionS *ex)
7953{
7954 if (ex->X_op == O_constant
7955 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7956 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7957 - 0x80000000);
7958}
7959
7960/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7961 all higher bits unset. */
7962
7963static void
7964normalize_address_expr (expressionS *ex)
7965{
7966 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7967 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7968 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7969 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7970 - 0x80000000);
7971}
7972
7973/* Try to match TOKENS against OPCODE, storing the result in INSN.
7974 Return true if the match was successful.
7975
7976 OPCODE_EXTRA is a value that should be ORed into the opcode
7977 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7978 there are more alternatives after OPCODE and SOFT_MATCH is
7979 as for mips_arg_info. */
7980
7981static bfd_boolean
7982match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7983 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7984 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7985{
7986 const char *args;
7987 struct mips_arg_info arg;
7988 const struct mips_operand *operand;
7989 char c;
7990
7991 imm_expr.X_op = O_absent;
97d87491
RS
7992 offset_expr.X_op = O_absent;
7993 offset_reloc[0] = BFD_RELOC_UNUSED;
7994 offset_reloc[1] = BFD_RELOC_UNUSED;
7995 offset_reloc[2] = BFD_RELOC_UNUSED;
7996
7997 create_insn (insn, opcode);
60f20e8b
RS
7998 /* When no opcode suffix is specified, assume ".xyzw". */
7999 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8000 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8001 else
8002 insn->insn_opcode |= opcode_extra;
97d87491
RS
8003 memset (&arg, 0, sizeof (arg));
8004 arg.insn = insn;
8005 arg.token = tokens;
8006 arg.argnum = 1;
8007 arg.last_regno = ILLEGAL_REG;
8008 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8009 arg.lax_match = lax_match;
97d87491
RS
8010 for (args = opcode->args;; ++args)
8011 {
8012 if (arg.token->type == OT_END)
8013 {
8014 /* Handle unary instructions in which only one operand is given.
8015 The source is then the same as the destination. */
8016 if (arg.opnum == 1 && *args == ',')
8017 {
8018 operand = (mips_opts.micromips
8019 ? decode_micromips_operand (args + 1)
8020 : decode_mips_operand (args + 1));
8021 if (operand && mips_optional_operand_p (operand))
8022 {
8023 arg.token = tokens;
8024 arg.argnum = 1;
8025 continue;
8026 }
8027 }
8028
8029 /* Treat elided base registers as $0. */
8030 if (strcmp (args, "(b)") == 0)
8031 args += 3;
8032
8033 if (args[0] == '+')
8034 switch (args[1])
8035 {
8036 case 'K':
8037 case 'N':
8038 /* The register suffix is optional. */
8039 args += 2;
8040 break;
8041 }
8042
8043 /* Fail the match if there were too few operands. */
8044 if (*args)
8045 return FALSE;
8046
8047 /* Successful match. */
60f20e8b
RS
8048 if (!complete_p)
8049 return TRUE;
e3de51ce 8050 clear_insn_error ();
97d87491
RS
8051 if (arg.dest_regno == arg.last_regno
8052 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8053 {
8054 if (arg.opnum == 2)
e3de51ce 8055 set_insn_error
1661c76c 8056 (0, _("source and destination must be different"));
97d87491 8057 else if (arg.last_regno == 31)
e3de51ce 8058 set_insn_error
1661c76c 8059 (0, _("a destination register must be supplied"));
97d87491 8060 }
173d3447
CF
8061 else if (arg.last_regno == 31
8062 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8063 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8064 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8065 check_completed_insn (&arg);
8066 return TRUE;
8067 }
8068
8069 /* Fail the match if the line has too many operands. */
8070 if (*args == 0)
8071 return FALSE;
8072
8073 /* Handle characters that need to match exactly. */
8074 if (*args == '(' || *args == ')' || *args == ',')
8075 {
8076 if (match_char (&arg, *args))
8077 continue;
8078 return FALSE;
8079 }
8080 if (*args == '#')
8081 {
8082 ++args;
8083 if (arg.token->type == OT_DOUBLE_CHAR
8084 && arg.token->u.ch == *args)
8085 {
8086 ++arg.token;
8087 continue;
8088 }
8089 return FALSE;
8090 }
8091
8092 /* Handle special macro operands. Work out the properties of
8093 other operands. */
8094 arg.opnum += 1;
97d87491
RS
8095 switch (*args)
8096 {
7361da2c
AB
8097 case '-':
8098 switch (args[1])
8099 {
8100 case 'A':
8101 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8102 break;
8103
8104 case 'B':
8105 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8106 break;
8107 }
8108 break;
8109
97d87491
RS
8110 case '+':
8111 switch (args[1])
8112 {
97d87491
RS
8113 case 'i':
8114 *offset_reloc = BFD_RELOC_MIPS_JMP;
8115 break;
7361da2c
AB
8116
8117 case '\'':
8118 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8119 break;
8120
8121 case '\"':
8122 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8123 break;
97d87491
RS
8124 }
8125 break;
8126
97d87491 8127 case 'I':
1a00e612
RS
8128 if (!match_const_int (&arg, &imm_expr.X_add_number))
8129 return FALSE;
8130 imm_expr.X_op = O_constant;
bad1aba3 8131 if (GPR_SIZE == 32)
97d87491
RS
8132 normalize_constant_expr (&imm_expr);
8133 continue;
8134
8135 case 'A':
8136 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8137 {
8138 /* Assume that the offset has been elided and that what
8139 we saw was a base register. The match will fail later
8140 if that assumption turns out to be wrong. */
8141 offset_expr.X_op = O_constant;
8142 offset_expr.X_add_number = 0;
8143 }
97d87491 8144 else
1a00e612
RS
8145 {
8146 if (!match_expression (&arg, &offset_expr, offset_reloc))
8147 return FALSE;
8148 normalize_address_expr (&offset_expr);
8149 }
97d87491
RS
8150 continue;
8151
8152 case 'F':
8153 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8154 8, TRUE))
1a00e612 8155 return FALSE;
97d87491
RS
8156 continue;
8157
8158 case 'L':
8159 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8160 8, FALSE))
1a00e612 8161 return FALSE;
97d87491
RS
8162 continue;
8163
8164 case 'f':
8165 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8166 4, TRUE))
1a00e612 8167 return FALSE;
97d87491
RS
8168 continue;
8169
8170 case 'l':
8171 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8172 4, FALSE))
1a00e612 8173 return FALSE;
97d87491
RS
8174 continue;
8175
97d87491
RS
8176 case 'p':
8177 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8178 break;
8179
8180 case 'a':
8181 *offset_reloc = BFD_RELOC_MIPS_JMP;
8182 break;
8183
8184 case 'm':
8185 gas_assert (mips_opts.micromips);
8186 c = args[1];
8187 switch (c)
8188 {
8189 case 'D':
8190 case 'E':
8191 if (!forced_insn_length)
8192 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8193 else if (c == 'D')
8194 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8195 else
8196 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8197 break;
8198 }
8199 break;
8200 }
8201
8202 operand = (mips_opts.micromips
8203 ? decode_micromips_operand (args)
8204 : decode_mips_operand (args));
8205 if (!operand)
8206 abort ();
8207
8208 /* Skip prefixes. */
7361da2c 8209 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8210 args++;
8211
8212 if (mips_optional_operand_p (operand)
8213 && args[1] == ','
8214 && (arg.token[0].type != OT_REG
8215 || arg.token[1].type == OT_END))
8216 {
8217 /* Assume that the register has been elided and is the
8218 same as the first operand. */
8219 arg.token = tokens;
8220 arg.argnum = 1;
8221 }
8222
8223 if (!match_operand (&arg, operand))
8224 return FALSE;
8225 }
8226}
8227
8228/* Like match_insn, but for MIPS16. */
8229
8230static bfd_boolean
8231match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8232 struct mips_operand_token *tokens)
97d87491
RS
8233{
8234 const char *args;
8235 const struct mips_operand *operand;
8236 const struct mips_operand *ext_operand;
82d808ed 8237 bfd_boolean pcrel = FALSE;
7fd53920 8238 int required_insn_length;
97d87491
RS
8239 struct mips_arg_info arg;
8240 int relax_char;
8241
7fd53920
MR
8242 if (forced_insn_length)
8243 required_insn_length = forced_insn_length;
8244 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8245 required_insn_length = 2;
8246 else
8247 required_insn_length = 0;
8248
97d87491
RS
8249 create_insn (insn, opcode);
8250 imm_expr.X_op = O_absent;
97d87491
RS
8251 offset_expr.X_op = O_absent;
8252 offset_reloc[0] = BFD_RELOC_UNUSED;
8253 offset_reloc[1] = BFD_RELOC_UNUSED;
8254 offset_reloc[2] = BFD_RELOC_UNUSED;
8255 relax_char = 0;
8256
8257 memset (&arg, 0, sizeof (arg));
8258 arg.insn = insn;
8259 arg.token = tokens;
8260 arg.argnum = 1;
8261 arg.last_regno = ILLEGAL_REG;
8262 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8263 relax_char = 0;
8264 for (args = opcode->args;; ++args)
8265 {
8266 int c;
8267
8268 if (arg.token->type == OT_END)
8269 {
8270 offsetT value;
8271
8272 /* Handle unary instructions in which only one operand is given.
8273 The source is then the same as the destination. */
8274 if (arg.opnum == 1 && *args == ',')
8275 {
8276 operand = decode_mips16_operand (args[1], FALSE);
8277 if (operand && mips_optional_operand_p (operand))
8278 {
8279 arg.token = tokens;
8280 arg.argnum = 1;
8281 continue;
8282 }
8283 }
8284
8285 /* Fail the match if there were too few operands. */
8286 if (*args)
8287 return FALSE;
8288
8289 /* Successful match. Stuff the immediate value in now, if
8290 we can. */
e3de51ce 8291 clear_insn_error ();
97d87491
RS
8292 if (opcode->pinfo == INSN_MACRO)
8293 {
8294 gas_assert (relax_char == 0 || relax_char == 'p');
8295 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8296 }
8297 else if (relax_char
8298 && offset_expr.X_op == O_constant
82d808ed 8299 && !pcrel
97d87491
RS
8300 && calculate_reloc (*offset_reloc,
8301 offset_expr.X_add_number,
8302 &value))
8303 {
8304 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8305 required_insn_length, &insn->insn_opcode);
97d87491
RS
8306 offset_expr.X_op = O_absent;
8307 *offset_reloc = BFD_RELOC_UNUSED;
8308 }
8309 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8310 {
7fd53920 8311 if (required_insn_length == 2)
e3de51ce 8312 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8313 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8314 {
8315 forced_insn_length = 4;
8316 insn->insn_opcode |= MIPS16_EXTEND;
8317 }
97d87491
RS
8318 }
8319 else if (relax_char)
8320 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8321
8322 check_completed_insn (&arg);
8323 return TRUE;
8324 }
8325
8326 /* Fail the match if the line has too many operands. */
8327 if (*args == 0)
8328 return FALSE;
8329
8330 /* Handle characters that need to match exactly. */
8331 if (*args == '(' || *args == ')' || *args == ',')
8332 {
8333 if (match_char (&arg, *args))
8334 continue;
8335 return FALSE;
8336 }
8337
8338 arg.opnum += 1;
8339 c = *args;
8340 switch (c)
8341 {
8342 case 'p':
8343 case 'q':
8344 case 'A':
8345 case 'B':
8346 case 'E':
25499ac7
MR
8347 case 'V':
8348 case 'u':
97d87491
RS
8349 relax_char = c;
8350 break;
8351
8352 case 'I':
1a00e612
RS
8353 if (!match_const_int (&arg, &imm_expr.X_add_number))
8354 return FALSE;
8355 imm_expr.X_op = O_constant;
bad1aba3 8356 if (GPR_SIZE == 32)
97d87491
RS
8357 normalize_constant_expr (&imm_expr);
8358 continue;
8359
8360 case 'a':
8361 case 'i':
8362 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8363 break;
8364 }
8365
7fd53920 8366 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8367 if (!operand)
8368 abort ();
8369
82d808ed
MR
8370 if (operand->type == OP_PCREL)
8371 pcrel = TRUE;
8372 else
97d87491
RS
8373 {
8374 ext_operand = decode_mips16_operand (c, TRUE);
8375 if (operand != ext_operand)
8376 {
8377 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8378 {
8379 offset_expr.X_op = O_constant;
8380 offset_expr.X_add_number = 0;
8381 relax_char = c;
8382 continue;
8383 }
8384
1a7bf198 8385 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8386 return FALSE;
8387
8388 /* '8' is used for SLTI(U) and has traditionally not
8389 been allowed to take relocation operators. */
8390 if (offset_reloc[0] != BFD_RELOC_UNUSED
8391 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8392 {
8393 match_not_constant (&arg);
8394 return FALSE;
8395 }
97d87491 8396
c96425c5
MR
8397 if (offset_expr.X_op == O_big)
8398 {
8399 match_out_of_range (&arg);
8400 return FALSE;
8401 }
8402
97d87491
RS
8403 relax_char = c;
8404 continue;
8405 }
8406 }
8407
8408 if (mips_optional_operand_p (operand)
8409 && args[1] == ','
8410 && (arg.token[0].type != OT_REG
8411 || arg.token[1].type == OT_END))
8412 {
8413 /* Assume that the register has been elided and is the
8414 same as the first operand. */
8415 arg.token = tokens;
8416 arg.argnum = 1;
8417 }
8418
8419 if (!match_operand (&arg, operand))
8420 return FALSE;
8421 }
8422}
8423
60f20e8b
RS
8424/* Record that the current instruction is invalid for the current ISA. */
8425
8426static void
8427match_invalid_for_isa (void)
8428{
8429 set_insn_error_ss
1661c76c 8430 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8431 mips_cpu_info_from_arch (mips_opts.arch)->name,
8432 mips_cpu_info_from_isa (mips_opts.isa)->name);
8433}
8434
8435/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8436 Return true if a definite match or failure was found, storing any match
8437 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8438 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8439 tried and failed to match under normal conditions and now want to try a
8440 more relaxed match. */
8441
8442static bfd_boolean
8443match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8444 const struct mips_opcode *past, struct mips_operand_token *tokens,
8445 int opcode_extra, bfd_boolean lax_match)
8446{
8447 const struct mips_opcode *opcode;
8448 const struct mips_opcode *invalid_delay_slot;
8449 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8450
8451 /* Search for a match, ignoring alternatives that don't satisfy the
8452 current ISA or forced_length. */
8453 invalid_delay_slot = 0;
8454 seen_valid_for_isa = FALSE;
8455 seen_valid_for_size = FALSE;
8456 opcode = first;
8457 do
8458 {
8459 gas_assert (strcmp (opcode->name, first->name) == 0);
8460 if (is_opcode_valid (opcode))
8461 {
8462 seen_valid_for_isa = TRUE;
8463 if (is_size_valid (opcode))
8464 {
8465 bfd_boolean delay_slot_ok;
8466
8467 seen_valid_for_size = TRUE;
8468 delay_slot_ok = is_delay_slot_valid (opcode);
8469 if (match_insn (insn, opcode, tokens, opcode_extra,
8470 lax_match, delay_slot_ok))
8471 {
8472 if (!delay_slot_ok)
8473 {
8474 if (!invalid_delay_slot)
8475 invalid_delay_slot = opcode;
8476 }
8477 else
8478 return TRUE;
8479 }
8480 }
8481 }
8482 ++opcode;
8483 }
8484 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8485
8486 /* If the only matches we found had the wrong length for the delay slot,
8487 pick the first such match. We'll issue an appropriate warning later. */
8488 if (invalid_delay_slot)
8489 {
8490 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8491 lax_match, TRUE))
8492 return TRUE;
8493 abort ();
8494 }
8495
8496 /* Handle the case where we didn't try to match an instruction because
8497 all the alternatives were incompatible with the current ISA. */
8498 if (!seen_valid_for_isa)
8499 {
8500 match_invalid_for_isa ();
8501 return TRUE;
8502 }
8503
8504 /* Handle the case where we didn't try to match an instruction because
8505 all the alternatives were of the wrong size. */
8506 if (!seen_valid_for_size)
8507 {
8508 if (mips_opts.insn32)
1661c76c 8509 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8510 else
8511 set_insn_error_i
1661c76c 8512 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8513 8 * forced_insn_length);
8514 return TRUE;
8515 }
8516
8517 return FALSE;
8518}
8519
8520/* Like match_insns, but for MIPS16. */
8521
8522static bfd_boolean
8523match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8524 struct mips_operand_token *tokens)
8525{
8526 const struct mips_opcode *opcode;
8527 bfd_boolean seen_valid_for_isa;
7fd53920 8528 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8529
8530 /* Search for a match, ignoring alternatives that don't satisfy the
8531 current ISA. There are no separate entries for extended forms so
8532 we deal with forced_length later. */
8533 seen_valid_for_isa = FALSE;
7fd53920 8534 seen_valid_for_size = FALSE;
60f20e8b
RS
8535 opcode = first;
8536 do
8537 {
8538 gas_assert (strcmp (opcode->name, first->name) == 0);
8539 if (is_opcode_valid_16 (opcode))
8540 {
8541 seen_valid_for_isa = TRUE;
7fd53920
MR
8542 if (is_size_valid_16 (opcode))
8543 {
8544 seen_valid_for_size = TRUE;
8545 if (match_mips16_insn (insn, opcode, tokens))
8546 return TRUE;
8547 }
60f20e8b
RS
8548 }
8549 ++opcode;
8550 }
8551 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8552 && strcmp (opcode->name, first->name) == 0);
8553
8554 /* Handle the case where we didn't try to match an instruction because
8555 all the alternatives were incompatible with the current ISA. */
8556 if (!seen_valid_for_isa)
8557 {
8558 match_invalid_for_isa ();
8559 return TRUE;
8560 }
8561
7fd53920
MR
8562 /* Handle the case where we didn't try to match an instruction because
8563 all the alternatives were of the wrong size. */
8564 if (!seen_valid_for_size)
8565 {
8566 if (forced_insn_length == 2)
8567 set_insn_error
8568 (0, _("unrecognized unextended version of MIPS16 opcode"));
8569 else
8570 set_insn_error
8571 (0, _("unrecognized extended version of MIPS16 opcode"));
8572 return TRUE;
8573 }
8574
60f20e8b
RS
8575 return FALSE;
8576}
8577
584892a6
RS
8578/* Set up global variables for the start of a new macro. */
8579
8580static void
8581macro_start (void)
8582{
8583 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8584 memset (&mips_macro_warning.first_insn_sizes, 0,
8585 sizeof (mips_macro_warning.first_insn_sizes));
8586 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8587 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8588 && delayed_branch_p (&history[0]));
7bd374a4
MR
8589 if (history[0].frag
8590 && history[0].frag->fr_type == rs_machine_dependent
8591 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8592 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8593 mips_macro_warning.delay_slot_length = 0;
8594 else
8595 switch (history[0].insn_mo->pinfo2
8596 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8597 {
8598 case INSN2_BRANCH_DELAY_32BIT:
8599 mips_macro_warning.delay_slot_length = 4;
8600 break;
8601 case INSN2_BRANCH_DELAY_16BIT:
8602 mips_macro_warning.delay_slot_length = 2;
8603 break;
8604 default:
8605 mips_macro_warning.delay_slot_length = 0;
8606 break;
8607 }
df58fc94 8608 mips_macro_warning.first_frag = NULL;
584892a6
RS
8609}
8610
df58fc94
RS
8611/* Given that a macro is longer than one instruction or of the wrong size,
8612 return the appropriate warning for it. Return null if no warning is
8613 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8614 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8615 and RELAX_NOMACRO. */
584892a6
RS
8616
8617static const char *
8618macro_warning (relax_substateT subtype)
8619{
8620 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8621 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8622 " in a branch delay slot");
8623 else if (subtype & RELAX_NOMACRO)
1661c76c 8624 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8625 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8626 | RELAX_DELAY_SLOT_SIZE_SECOND))
8627 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8628 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8629 " in a 16-bit branch delay slot")
1661c76c 8630 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8631 " in a 32-bit branch delay slot"));
584892a6
RS
8632 else
8633 return 0;
8634}
8635
8636/* Finish up a macro. Emit warnings as appropriate. */
8637
8638static void
8639macro_end (void)
8640{
df58fc94
RS
8641 /* Relaxation warning flags. */
8642 relax_substateT subtype = 0;
8643
8644 /* Check delay slot size requirements. */
8645 if (mips_macro_warning.delay_slot_length == 2)
8646 subtype |= RELAX_DELAY_SLOT_16BIT;
8647 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8648 {
df58fc94
RS
8649 if (mips_macro_warning.delay_slot_length
8650 != mips_macro_warning.first_insn_sizes[0])
8651 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8652 if (mips_macro_warning.delay_slot_length
8653 != mips_macro_warning.first_insn_sizes[1])
8654 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8655 }
584892a6 8656
df58fc94
RS
8657 /* Check instruction count requirements. */
8658 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8659 {
8660 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8661 subtype |= RELAX_SECOND_LONGER;
8662 if (mips_opts.warn_about_macros)
8663 subtype |= RELAX_NOMACRO;
8664 if (mips_macro_warning.delay_slot_p)
8665 subtype |= RELAX_DELAY_SLOT;
df58fc94 8666 }
584892a6 8667
df58fc94
RS
8668 /* If both alternatives fail to fill a delay slot correctly,
8669 emit the warning now. */
8670 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8671 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8672 {
8673 relax_substateT s;
8674 const char *msg;
8675
8676 s = subtype & (RELAX_DELAY_SLOT_16BIT
8677 | RELAX_DELAY_SLOT_SIZE_FIRST
8678 | RELAX_DELAY_SLOT_SIZE_SECOND);
8679 msg = macro_warning (s);
8680 if (msg != NULL)
8681 as_warn ("%s", msg);
8682 subtype &= ~s;
8683 }
8684
8685 /* If both implementations are longer than 1 instruction, then emit the
8686 warning now. */
8687 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8688 {
8689 relax_substateT s;
8690 const char *msg;
8691
8692 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8693 msg = macro_warning (s);
8694 if (msg != NULL)
8695 as_warn ("%s", msg);
8696 subtype &= ~s;
584892a6 8697 }
df58fc94
RS
8698
8699 /* If any flags still set, then one implementation might need a warning
8700 and the other either will need one of a different kind or none at all.
8701 Pass any remaining flags over to relaxation. */
8702 if (mips_macro_warning.first_frag != NULL)
8703 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8704}
8705
df58fc94
RS
8706/* Instruction operand formats used in macros that vary between
8707 standard MIPS and microMIPS code. */
8708
833794fc 8709static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8710static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8711static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8712static const char * const lui_fmt[2] = { "t,u", "s,u" };
8713static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8714static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8715static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8716static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8717
833794fc 8718#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8719#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8720 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8721#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8722#define LUI_FMT (lui_fmt[mips_opts.micromips])
8723#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8724#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8725 : mem12_fmt[mips_opts.micromips])
833794fc 8726#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8727#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8728#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8729
6e1304d8
RS
8730/* Read a macro's relocation codes from *ARGS and store them in *R.
8731 The first argument in *ARGS will be either the code for a single
8732 relocation or -1 followed by the three codes that make up a
8733 composite relocation. */
8734
8735static void
8736macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8737{
8738 int i, next;
8739
8740 next = va_arg (*args, int);
8741 if (next >= 0)
8742 r[0] = (bfd_reloc_code_real_type) next;
8743 else
f2ae14a1
RS
8744 {
8745 for (i = 0; i < 3; i++)
8746 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8747 /* This function is only used for 16-bit relocation fields.
8748 To make the macro code simpler, treat an unrelocated value
8749 in the same way as BFD_RELOC_LO16. */
8750 if (r[0] == BFD_RELOC_UNUSED)
8751 r[0] = BFD_RELOC_LO16;
8752 }
6e1304d8
RS
8753}
8754
252b5132
RH
8755/* Build an instruction created by a macro expansion. This is passed
8756 a pointer to the count of instructions created so far, an
8757 expression, the name of the instruction to build, an operand format
8758 string, and corresponding arguments. */
8759
252b5132 8760static void
67c0d1eb 8761macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8762{
df58fc94 8763 const struct mips_opcode *mo = NULL;
f6688943 8764 bfd_reloc_code_real_type r[3];
df58fc94 8765 const struct mips_opcode *amo;
e077a1c8 8766 const struct mips_operand *operand;
df58fc94
RS
8767 struct hash_control *hash;
8768 struct mips_cl_insn insn;
252b5132 8769 va_list args;
e077a1c8 8770 unsigned int uval;
252b5132 8771
252b5132 8772 va_start (args, fmt);
252b5132 8773
252b5132
RH
8774 if (mips_opts.mips16)
8775 {
03ea81db 8776 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8777 va_end (args);
8778 return;
8779 }
8780
f6688943
TS
8781 r[0] = BFD_RELOC_UNUSED;
8782 r[1] = BFD_RELOC_UNUSED;
8783 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8784 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8785 amo = (struct mips_opcode *) hash_find (hash, name);
8786 gas_assert (amo);
8787 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8788
df58fc94 8789 do
8b082fb1
TS
8790 {
8791 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8792 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8793 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8794 slot instruction length requirement (if any) of the previous
8795 instruction. While doing this we record the first instruction
8796 seen that matches all the other conditions and use it anyway
8797 if the requirement cannot be met; we will issue an appropriate
8798 warning later on. */
8799 if (strcmp (fmt, amo->args) == 0
8800 && amo->pinfo != INSN_MACRO
8801 && is_opcode_valid (amo)
8802 && is_size_valid (amo))
8803 {
8804 if (is_delay_slot_valid (amo))
8805 {
8806 mo = amo;
8807 break;
8808 }
8809 else if (!mo)
8810 mo = amo;
8811 }
8b082fb1 8812
df58fc94
RS
8813 ++amo;
8814 gas_assert (amo->name);
252b5132 8815 }
df58fc94 8816 while (strcmp (name, amo->name) == 0);
252b5132 8817
df58fc94 8818 gas_assert (mo);
1e915849 8819 create_insn (&insn, mo);
e077a1c8 8820 for (; *fmt; ++fmt)
252b5132 8821 {
e077a1c8 8822 switch (*fmt)
252b5132 8823 {
252b5132
RH
8824 case ',':
8825 case '(':
8826 case ')':
252b5132 8827 case 'z':
e077a1c8 8828 break;
252b5132
RH
8829
8830 case 'i':
8831 case 'j':
6e1304d8 8832 macro_read_relocs (&args, r);
9c2799c2 8833 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8834 || *r == BFD_RELOC_MIPS_HIGHER
8835 || *r == BFD_RELOC_HI16_S
8836 || *r == BFD_RELOC_LO16
8837 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 8838 break;
e391c024
RS
8839
8840 case 'o':
8841 macro_read_relocs (&args, r);
e077a1c8 8842 break;
252b5132
RH
8843
8844 case 'u':
6e1304d8 8845 macro_read_relocs (&args, r);
9c2799c2 8846 gas_assert (ep != NULL
90ecf173
MR
8847 && (ep->X_op == O_constant
8848 || (ep->X_op == O_symbol
8849 && (*r == BFD_RELOC_MIPS_HIGHEST
8850 || *r == BFD_RELOC_HI16_S
8851 || *r == BFD_RELOC_HI16
8852 || *r == BFD_RELOC_GPREL16
8853 || *r == BFD_RELOC_MIPS_GOT_HI16
8854 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8855 break;
252b5132
RH
8856
8857 case 'p':
9c2799c2 8858 gas_assert (ep != NULL);
bad36eac 8859
252b5132
RH
8860 /*
8861 * This allows macro() to pass an immediate expression for
8862 * creating short branches without creating a symbol.
bad36eac
DJ
8863 *
8864 * We don't allow branch relaxation for these branches, as
8865 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8866 */
8867 if (ep->X_op == O_constant)
8868 {
df58fc94
RS
8869 /* For microMIPS we always use relocations for branches.
8870 So we should not resolve immediate values. */
8871 gas_assert (!mips_opts.micromips);
8872
bad36eac
DJ
8873 if ((ep->X_add_number & 3) != 0)
8874 as_bad (_("branch to misaligned address (0x%lx)"),
8875 (unsigned long) ep->X_add_number);
8876 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8877 as_bad (_("branch address range overflow (0x%lx)"),
8878 (unsigned long) ep->X_add_number);
252b5132
RH
8879 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8880 ep = NULL;
8881 }
8882 else
0b25d3e6 8883 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8884 break;
252b5132
RH
8885
8886 case 'a':
9c2799c2 8887 gas_assert (ep != NULL);
f6688943 8888 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8889 break;
d43b4baf 8890
252b5132 8891 default:
e077a1c8
RS
8892 operand = (mips_opts.micromips
8893 ? decode_micromips_operand (fmt)
8894 : decode_mips_operand (fmt));
8895 if (!operand)
8896 abort ();
8897
8898 uval = va_arg (args, int);
8899 if (operand->type == OP_CLO_CLZ_DEST)
8900 uval |= (uval << 5);
8901 insn_insert_operand (&insn, operand, uval);
8902
7361da2c 8903 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8904 ++fmt;
8905 break;
252b5132 8906 }
252b5132
RH
8907 }
8908 va_end (args);
9c2799c2 8909 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8910
df58fc94 8911 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8912}
8913
8914static void
67c0d1eb 8915mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8916 va_list *args)
252b5132 8917{
1e915849 8918 struct mips_opcode *mo;
252b5132 8919 struct mips_cl_insn insn;
e077a1c8 8920 const struct mips_operand *operand;
f6688943
TS
8921 bfd_reloc_code_real_type r[3]
8922 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8923
1e915849 8924 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8925 gas_assert (mo);
8926 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8927
1e915849 8928 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8929 {
1e915849 8930 ++mo;
9c2799c2
NC
8931 gas_assert (mo->name);
8932 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8933 }
8934
1e915849 8935 create_insn (&insn, mo);
e077a1c8 8936 for (; *fmt; ++fmt)
252b5132
RH
8937 {
8938 int c;
8939
e077a1c8 8940 c = *fmt;
252b5132
RH
8941 switch (c)
8942 {
252b5132
RH
8943 case ',':
8944 case '(':
8945 case ')':
e077a1c8 8946 break;
252b5132 8947
d8722d76 8948 case '.':
252b5132
RH
8949 case 'S':
8950 case 'P':
8951 case 'R':
e077a1c8 8952 break;
252b5132
RH
8953
8954 case '<':
252b5132 8955 case '5':
d8722d76 8956 case 'F':
252b5132
RH
8957 case 'H':
8958 case 'W':
8959 case 'D':
8960 case 'j':
8961 case '8':
8962 case 'V':
8963 case 'C':
8964 case 'U':
8965 case 'k':
8966 case 'K':
8967 case 'p':
8968 case 'q':
8969 {
b886a2ab
RS
8970 offsetT value;
8971
9c2799c2 8972 gas_assert (ep != NULL);
252b5132
RH
8973
8974 if (ep->X_op != O_constant)
874e8986 8975 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8976 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8977 {
b886a2ab 8978 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8979 ep = NULL;
f6688943 8980 *r = BFD_RELOC_UNUSED;
252b5132
RH
8981 }
8982 }
e077a1c8 8983 break;
252b5132 8984
e077a1c8
RS
8985 default:
8986 operand = decode_mips16_operand (c, FALSE);
8987 if (!operand)
8988 abort ();
252b5132 8989
4a06e5a2 8990 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8991 break;
8992 }
252b5132
RH
8993 }
8994
9c2799c2 8995 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8996
df58fc94 8997 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8998}
8999
438c16b8
TS
9000/*
9001 * Generate a "jalr" instruction with a relocation hint to the called
9002 * function. This occurs in NewABI PIC code.
9003 */
9004static void
df58fc94 9005macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9006{
df58fc94
RS
9007 static const bfd_reloc_code_real_type jalr_relocs[2]
9008 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9009 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9010 const char *jalr;
685736be 9011 char *f = NULL;
b34976b6 9012
1180b5a4 9013 if (MIPS_JALR_HINT_P (ep))
f21f8242 9014 {
cc3d92a5 9015 frag_grow (8);
f21f8242
AO
9016 f = frag_more (0);
9017 }
2906b037 9018 if (mips_opts.micromips)
df58fc94 9019 {
833794fc
MR
9020 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9021 ? "jalr" : "jalrs");
e64af278 9022 if (MIPS_JALR_HINT_P (ep)
833794fc 9023 || mips_opts.insn32
e64af278 9024 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9025 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9026 else
9027 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9028 }
2906b037
MR
9029 else
9030 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9031 if (MIPS_JALR_HINT_P (ep))
df58fc94 9032 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9033}
9034
252b5132
RH
9035/*
9036 * Generate a "lui" instruction.
9037 */
9038static void
67c0d1eb 9039macro_build_lui (expressionS *ep, int regnum)
252b5132 9040{
9c2799c2 9041 gas_assert (! mips_opts.mips16);
252b5132 9042
df58fc94 9043 if (ep->X_op != O_constant)
252b5132 9044 {
9c2799c2 9045 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9046 /* _gp_disp is a special case, used from s_cpload.
9047 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9048 gas_assert (mips_pic == NO_PIC
78e1bb40 9049 || (! HAVE_NEWABI
aa6975fb
ILT
9050 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9051 || (! mips_in_shared
bbe506e8
TS
9052 && strcmp (S_GET_NAME (ep->X_add_symbol),
9053 "__gnu_local_gp") == 0));
252b5132
RH
9054 }
9055
df58fc94 9056 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9057}
9058
885add95
CD
9059/* Generate a sequence of instructions to do a load or store from a constant
9060 offset off of a base register (breg) into/from a target register (treg),
9061 using AT if necessary. */
9062static void
67c0d1eb
RS
9063macro_build_ldst_constoffset (expressionS *ep, const char *op,
9064 int treg, int breg, int dbl)
885add95 9065{
9c2799c2 9066 gas_assert (ep->X_op == O_constant);
885add95 9067
256ab948 9068 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9069 if (!dbl)
9070 normalize_constant_expr (ep);
256ab948 9071
67c1ffbe 9072 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9073 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9074 as_warn (_("operand overflow"));
9075
9076 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9077 {
9078 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9079 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9080 }
9081 else
9082 {
9083 /* 32-bit offset, need multiple instructions and AT, like:
9084 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9085 addu $tempreg,$tempreg,$breg
9086 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9087 to handle the complete offset. */
67c0d1eb
RS
9088 macro_build_lui (ep, AT);
9089 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9090 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9091
741fe287 9092 if (!mips_opts.at)
1661c76c 9093 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9094 }
9095}
9096
252b5132
RH
9097/* set_at()
9098 * Generates code to set the $at register to true (one)
9099 * if reg is less than the immediate expression.
9100 */
9101static void
67c0d1eb 9102set_at (int reg, int unsignedp)
252b5132 9103{
b0e6f033 9104 if (imm_expr.X_add_number >= -0x8000
252b5132 9105 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9106 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9107 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9108 else
9109 {
bad1aba3 9110 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9111 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9112 }
9113}
9114
252b5132
RH
9115/* Count the leading zeroes by performing a binary chop. This is a
9116 bulky bit of source, but performance is a LOT better for the
9117 majority of values than a simple loop to count the bits:
9118 for (lcnt = 0; (lcnt < 32); lcnt++)
9119 if ((v) & (1 << (31 - lcnt)))
9120 break;
9121 However it is not code size friendly, and the gain will drop a bit
9122 on certain cached systems.
9123*/
9124#define COUNT_TOP_ZEROES(v) \
9125 (((v) & ~0xffff) == 0 \
9126 ? ((v) & ~0xff) == 0 \
9127 ? ((v) & ~0xf) == 0 \
9128 ? ((v) & ~0x3) == 0 \
9129 ? ((v) & ~0x1) == 0 \
9130 ? !(v) \
9131 ? 32 \
9132 : 31 \
9133 : 30 \
9134 : ((v) & ~0x7) == 0 \
9135 ? 29 \
9136 : 28 \
9137 : ((v) & ~0x3f) == 0 \
9138 ? ((v) & ~0x1f) == 0 \
9139 ? 27 \
9140 : 26 \
9141 : ((v) & ~0x7f) == 0 \
9142 ? 25 \
9143 : 24 \
9144 : ((v) & ~0xfff) == 0 \
9145 ? ((v) & ~0x3ff) == 0 \
9146 ? ((v) & ~0x1ff) == 0 \
9147 ? 23 \
9148 : 22 \
9149 : ((v) & ~0x7ff) == 0 \
9150 ? 21 \
9151 : 20 \
9152 : ((v) & ~0x3fff) == 0 \
9153 ? ((v) & ~0x1fff) == 0 \
9154 ? 19 \
9155 : 18 \
9156 : ((v) & ~0x7fff) == 0 \
9157 ? 17 \
9158 : 16 \
9159 : ((v) & ~0xffffff) == 0 \
9160 ? ((v) & ~0xfffff) == 0 \
9161 ? ((v) & ~0x3ffff) == 0 \
9162 ? ((v) & ~0x1ffff) == 0 \
9163 ? 15 \
9164 : 14 \
9165 : ((v) & ~0x7ffff) == 0 \
9166 ? 13 \
9167 : 12 \
9168 : ((v) & ~0x3fffff) == 0 \
9169 ? ((v) & ~0x1fffff) == 0 \
9170 ? 11 \
9171 : 10 \
9172 : ((v) & ~0x7fffff) == 0 \
9173 ? 9 \
9174 : 8 \
9175 : ((v) & ~0xfffffff) == 0 \
9176 ? ((v) & ~0x3ffffff) == 0 \
9177 ? ((v) & ~0x1ffffff) == 0 \
9178 ? 7 \
9179 : 6 \
9180 : ((v) & ~0x7ffffff) == 0 \
9181 ? 5 \
9182 : 4 \
9183 : ((v) & ~0x3fffffff) == 0 \
9184 ? ((v) & ~0x1fffffff) == 0 \
9185 ? 3 \
9186 : 2 \
9187 : ((v) & ~0x7fffffff) == 0 \
9188 ? 1 \
9189 : 0)
9190
9191/* load_register()
67c1ffbe 9192 * This routine generates the least number of instructions necessary to load
252b5132
RH
9193 * an absolute expression value into a register.
9194 */
9195static void
67c0d1eb 9196load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9197{
9198 int freg;
9199 expressionS hi32, lo32;
9200
9201 if (ep->X_op != O_big)
9202 {
9c2799c2 9203 gas_assert (ep->X_op == O_constant);
256ab948
TS
9204
9205 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9206 if (!dbl)
9207 normalize_constant_expr (ep);
256ab948
TS
9208
9209 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9210 {
9211 /* We can handle 16 bit signed values with an addiu to
9212 $zero. No need to ever use daddiu here, since $zero and
9213 the result are always correct in 32 bit mode. */
67c0d1eb 9214 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9215 return;
9216 }
9217 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9218 {
9219 /* We can handle 16 bit unsigned values with an ori to
9220 $zero. */
67c0d1eb 9221 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9222 return;
9223 }
256ab948 9224 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9225 {
9226 /* 32 bit values require an lui. */
df58fc94 9227 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9228 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9229 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9230 return;
9231 }
9232 }
9233
9234 /* The value is larger than 32 bits. */
9235
bad1aba3 9236 if (!dbl || GPR_SIZE == 32)
252b5132 9237 {
55e08f71
NC
9238 char value[32];
9239
9240 sprintf_vma (value, ep->X_add_number);
1661c76c 9241 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9242 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9243 return;
9244 }
9245
9246 if (ep->X_op != O_big)
9247 {
9248 hi32 = *ep;
9249 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9250 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9251 hi32.X_add_number &= 0xffffffff;
9252 lo32 = *ep;
9253 lo32.X_add_number &= 0xffffffff;
9254 }
9255 else
9256 {
9c2799c2 9257 gas_assert (ep->X_add_number > 2);
252b5132
RH
9258 if (ep->X_add_number == 3)
9259 generic_bignum[3] = 0;
9260 else if (ep->X_add_number > 4)
1661c76c 9261 as_bad (_("number larger than 64 bits"));
252b5132
RH
9262 lo32.X_op = O_constant;
9263 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9264 hi32.X_op = O_constant;
9265 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9266 }
9267
9268 if (hi32.X_add_number == 0)
9269 freg = 0;
9270 else
9271 {
9272 int shift, bit;
9273 unsigned long hi, lo;
9274
956cd1d6 9275 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9276 {
9277 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9278 {
67c0d1eb 9279 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9280 return;
9281 }
9282 if (lo32.X_add_number & 0x80000000)
9283 {
df58fc94 9284 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9285 if (lo32.X_add_number & 0xffff)
67c0d1eb 9286 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9287 return;
9288 }
9289 }
252b5132
RH
9290
9291 /* Check for 16bit shifted constant. We know that hi32 is
9292 non-zero, so start the mask on the first bit of the hi32
9293 value. */
9294 shift = 17;
9295 do
beae10d5
KH
9296 {
9297 unsigned long himask, lomask;
9298
9299 if (shift < 32)
9300 {
9301 himask = 0xffff >> (32 - shift);
9302 lomask = (0xffff << shift) & 0xffffffff;
9303 }
9304 else
9305 {
9306 himask = 0xffff << (shift - 32);
9307 lomask = 0;
9308 }
9309 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9310 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9311 {
9312 expressionS tmp;
9313
9314 tmp.X_op = O_constant;
9315 if (shift < 32)
9316 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9317 | (lo32.X_add_number >> shift));
9318 else
9319 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9320 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9321 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9322 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9323 return;
9324 }
f9419b05 9325 ++shift;
beae10d5
KH
9326 }
9327 while (shift <= (64 - 16));
252b5132
RH
9328
9329 /* Find the bit number of the lowest one bit, and store the
9330 shifted value in hi/lo. */
9331 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9332 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9333 if (lo != 0)
9334 {
9335 bit = 0;
9336 while ((lo & 1) == 0)
9337 {
9338 lo >>= 1;
9339 ++bit;
9340 }
9341 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9342 hi >>= bit;
9343 }
9344 else
9345 {
9346 bit = 32;
9347 while ((hi & 1) == 0)
9348 {
9349 hi >>= 1;
9350 ++bit;
9351 }
9352 lo = hi;
9353 hi = 0;
9354 }
9355
9356 /* Optimize if the shifted value is a (power of 2) - 1. */
9357 if ((hi == 0 && ((lo + 1) & lo) == 0)
9358 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9359 {
9360 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9361 if (shift != 0)
beae10d5 9362 {
252b5132
RH
9363 expressionS tmp;
9364
9365 /* This instruction will set the register to be all
9366 ones. */
beae10d5
KH
9367 tmp.X_op = O_constant;
9368 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9369 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9370 if (bit != 0)
9371 {
9372 bit += shift;
df58fc94 9373 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9374 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9375 }
df58fc94 9376 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9377 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9378 return;
9379 }
9380 }
252b5132
RH
9381
9382 /* Sign extend hi32 before calling load_register, because we can
9383 generally get better code when we load a sign extended value. */
9384 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9385 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9386 load_register (reg, &hi32, 0);
252b5132
RH
9387 freg = reg;
9388 }
9389 if ((lo32.X_add_number & 0xffff0000) == 0)
9390 {
9391 if (freg != 0)
9392 {
df58fc94 9393 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9394 freg = reg;
9395 }
9396 }
9397 else
9398 {
9399 expressionS mid16;
9400
956cd1d6 9401 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9402 {
df58fc94
RS
9403 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9404 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9405 return;
9406 }
252b5132
RH
9407
9408 if (freg != 0)
9409 {
df58fc94 9410 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9411 freg = reg;
9412 }
9413 mid16 = lo32;
9414 mid16.X_add_number >>= 16;
67c0d1eb 9415 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9416 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9417 freg = reg;
9418 }
9419 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9420 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9421}
9422
269137b2
TS
9423static inline void
9424load_delay_nop (void)
9425{
9426 if (!gpr_interlocks)
9427 macro_build (NULL, "nop", "");
9428}
9429
252b5132
RH
9430/* Load an address into a register. */
9431
9432static void
67c0d1eb 9433load_address (int reg, expressionS *ep, int *used_at)
252b5132 9434{
252b5132
RH
9435 if (ep->X_op != O_constant
9436 && ep->X_op != O_symbol)
9437 {
9438 as_bad (_("expression too complex"));
9439 ep->X_op = O_constant;
9440 }
9441
9442 if (ep->X_op == O_constant)
9443 {
67c0d1eb 9444 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9445 return;
9446 }
9447
9448 if (mips_pic == NO_PIC)
9449 {
9450 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9451 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9452 Otherwise we want
9453 lui $reg,<sym> (BFD_RELOC_HI16_S)
9454 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9455 If we have an addend, we always use the latter form.
76b3015f 9456
d6bc6245
TS
9457 With 64bit address space and a usable $at we want
9458 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9459 lui $at,<sym> (BFD_RELOC_HI16_S)
9460 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9461 daddiu $at,<sym> (BFD_RELOC_LO16)
9462 dsll32 $reg,0
3a482fd5 9463 daddu $reg,$reg,$at
76b3015f 9464
c03099e6 9465 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9466 on superscalar processors.
9467 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9468 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9469 dsll $reg,16
9470 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9471 dsll $reg,16
9472 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9473
9474 For GP relative symbols in 64bit address space we can use
9475 the same sequence as in 32bit address space. */
aed1a261 9476 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9477 {
6caf9ef4
TS
9478 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9479 && !nopic_need_relax (ep->X_add_symbol, 1))
9480 {
9481 relax_start (ep->X_add_symbol);
9482 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9483 mips_gp_register, BFD_RELOC_GPREL16);
9484 relax_switch ();
9485 }
d6bc6245 9486
741fe287 9487 if (*used_at == 0 && mips_opts.at)
d6bc6245 9488 {
df58fc94
RS
9489 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9490 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9491 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9492 BFD_RELOC_MIPS_HIGHER);
9493 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9494 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9495 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9496 *used_at = 1;
9497 }
9498 else
9499 {
df58fc94 9500 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9501 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9502 BFD_RELOC_MIPS_HIGHER);
df58fc94 9503 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9504 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9505 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9506 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9507 }
6caf9ef4
TS
9508
9509 if (mips_relax.sequence)
9510 relax_end ();
d6bc6245 9511 }
252b5132
RH
9512 else
9513 {
d6bc6245 9514 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9515 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9516 {
4d7206a2 9517 relax_start (ep->X_add_symbol);
67c0d1eb 9518 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9519 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9520 relax_switch ();
d6bc6245 9521 }
67c0d1eb
RS
9522 macro_build_lui (ep, reg);
9523 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9524 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9525 if (mips_relax.sequence)
9526 relax_end ();
d6bc6245 9527 }
252b5132 9528 }
0a44bf69 9529 else if (!mips_big_got)
252b5132
RH
9530 {
9531 expressionS ex;
9532
9533 /* If this is a reference to an external symbol, we want
9534 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9535 Otherwise we want
9536 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9537 nop
9538 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9539 If there is a constant, it must be added in after.
9540
ed6fb7bd 9541 If we have NewABI, we want
f5040a92
AO
9542 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9543 unless we're referencing a global symbol with a non-zero
9544 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9545 if (HAVE_NEWABI)
9546 {
f5040a92
AO
9547 if (ep->X_add_number)
9548 {
4d7206a2 9549 ex.X_add_number = ep->X_add_number;
f5040a92 9550 ep->X_add_number = 0;
4d7206a2 9551 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9552 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9553 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9554 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9555 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9556 ex.X_op = O_constant;
67c0d1eb 9557 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9558 reg, reg, BFD_RELOC_LO16);
f5040a92 9559 ep->X_add_number = ex.X_add_number;
4d7206a2 9560 relax_switch ();
f5040a92 9561 }
67c0d1eb 9562 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9563 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9564 if (mips_relax.sequence)
9565 relax_end ();
ed6fb7bd
SC
9566 }
9567 else
9568 {
f5040a92
AO
9569 ex.X_add_number = ep->X_add_number;
9570 ep->X_add_number = 0;
67c0d1eb
RS
9571 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9572 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9573 load_delay_nop ();
4d7206a2
RS
9574 relax_start (ep->X_add_symbol);
9575 relax_switch ();
67c0d1eb 9576 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9577 BFD_RELOC_LO16);
4d7206a2 9578 relax_end ();
ed6fb7bd 9579
f5040a92
AO
9580 if (ex.X_add_number != 0)
9581 {
9582 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9583 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9584 ex.X_op = O_constant;
67c0d1eb 9585 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9586 reg, reg, BFD_RELOC_LO16);
f5040a92 9587 }
252b5132
RH
9588 }
9589 }
0a44bf69 9590 else if (mips_big_got)
252b5132
RH
9591 {
9592 expressionS ex;
252b5132
RH
9593
9594 /* This is the large GOT case. If this is a reference to an
9595 external symbol, we want
9596 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9597 addu $reg,$reg,$gp
9598 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9599
9600 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9601 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9602 nop
9603 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9604 If there is a constant, it must be added in after.
f5040a92
AO
9605
9606 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9607 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9608 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9609 */
438c16b8
TS
9610 if (HAVE_NEWABI)
9611 {
4d7206a2 9612 ex.X_add_number = ep->X_add_number;
f5040a92 9613 ep->X_add_number = 0;
4d7206a2 9614 relax_start (ep->X_add_symbol);
df58fc94 9615 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9616 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9617 reg, reg, mips_gp_register);
9618 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9619 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9620 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9621 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9622 else if (ex.X_add_number)
9623 {
9624 ex.X_op = O_constant;
67c0d1eb
RS
9625 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9626 BFD_RELOC_LO16);
f5040a92
AO
9627 }
9628
9629 ep->X_add_number = ex.X_add_number;
4d7206a2 9630 relax_switch ();
67c0d1eb 9631 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9632 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9633 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9634 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9635 relax_end ();
438c16b8 9636 }
252b5132 9637 else
438c16b8 9638 {
f5040a92
AO
9639 ex.X_add_number = ep->X_add_number;
9640 ep->X_add_number = 0;
4d7206a2 9641 relax_start (ep->X_add_symbol);
df58fc94 9642 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9643 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9644 reg, reg, mips_gp_register);
9645 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9646 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9647 relax_switch ();
9648 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9649 {
9650 /* We need a nop before loading from $gp. This special
9651 check is required because the lui which starts the main
9652 instruction stream does not refer to $gp, and so will not
9653 insert the nop which may be required. */
67c0d1eb 9654 macro_build (NULL, "nop", "");
438c16b8 9655 }
67c0d1eb 9656 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9657 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9658 load_delay_nop ();
67c0d1eb 9659 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9660 BFD_RELOC_LO16);
4d7206a2 9661 relax_end ();
438c16b8 9662
f5040a92
AO
9663 if (ex.X_add_number != 0)
9664 {
9665 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9666 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9667 ex.X_op = O_constant;
67c0d1eb
RS
9668 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9669 BFD_RELOC_LO16);
f5040a92 9670 }
252b5132
RH
9671 }
9672 }
252b5132
RH
9673 else
9674 abort ();
8fc2e39e 9675
741fe287 9676 if (!mips_opts.at && *used_at == 1)
1661c76c 9677 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9678}
9679
ea1fb5dc
RS
9680/* Move the contents of register SOURCE into register DEST. */
9681
9682static void
67c0d1eb 9683move_register (int dest, int source)
ea1fb5dc 9684{
df58fc94
RS
9685 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9686 instruction specifically requires a 32-bit one. */
9687 if (mips_opts.micromips
833794fc 9688 && !mips_opts.insn32
df58fc94 9689 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9690 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9691 else
40fc1451 9692 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9693}
9694
4d7206a2 9695/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9696 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9697 The two alternatives are:
4d7206a2 9698
33eaf5de 9699 Global symbol Local symbol
4d7206a2
RS
9700 ------------- ------------
9701 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9702 ... ...
9703 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9704
9705 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9706 emits the second for a 16-bit offset or add_got_offset_hilo emits
9707 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9708
9709static void
67c0d1eb 9710load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9711{
9712 expressionS global;
9713
9714 global = *local;
9715 global.X_add_number = 0;
9716
9717 relax_start (local->X_add_symbol);
67c0d1eb
RS
9718 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9719 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9720 relax_switch ();
67c0d1eb
RS
9721 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9722 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9723 relax_end ();
9724}
9725
9726static void
67c0d1eb 9727add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9728{
9729 expressionS global;
9730
9731 global.X_op = O_constant;
9732 global.X_op_symbol = NULL;
9733 global.X_add_symbol = NULL;
9734 global.X_add_number = local->X_add_number;
9735
9736 relax_start (local->X_add_symbol);
67c0d1eb 9737 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9738 dest, dest, BFD_RELOC_LO16);
9739 relax_switch ();
67c0d1eb 9740 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9741 relax_end ();
9742}
9743
f6a22291
MR
9744static void
9745add_got_offset_hilo (int dest, expressionS *local, int tmp)
9746{
9747 expressionS global;
9748 int hold_mips_optimize;
9749
9750 global.X_op = O_constant;
9751 global.X_op_symbol = NULL;
9752 global.X_add_symbol = NULL;
9753 global.X_add_number = local->X_add_number;
9754
9755 relax_start (local->X_add_symbol);
9756 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9757 relax_switch ();
9758 /* Set mips_optimize around the lui instruction to avoid
9759 inserting an unnecessary nop after the lw. */
9760 hold_mips_optimize = mips_optimize;
9761 mips_optimize = 2;
9762 macro_build_lui (&global, tmp);
9763 mips_optimize = hold_mips_optimize;
9764 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9765 relax_end ();
9766
9767 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9768}
9769
df58fc94
RS
9770/* Emit a sequence of instructions to emulate a branch likely operation.
9771 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9772 is its complementing branch with the original condition negated.
9773 CALL is set if the original branch specified the link operation.
9774 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9775
9776 Code like this is produced in the noreorder mode:
9777
9778 BRNEG <args>, 1f
9779 nop
9780 b <sym>
9781 delay slot (executed only if branch taken)
9782 1:
9783
9784 or, if CALL is set:
9785
9786 BRNEG <args>, 1f
9787 nop
9788 bal <sym>
9789 delay slot (executed only if branch taken)
9790 1:
9791
9792 In the reorder mode the delay slot would be filled with a nop anyway,
9793 so code produced is simply:
9794
9795 BR <args>, <sym>
9796 nop
9797
9798 This function is used when producing code for the microMIPS ASE that
9799 does not implement branch likely instructions in hardware. */
9800
9801static void
9802macro_build_branch_likely (const char *br, const char *brneg,
9803 int call, expressionS *ep, const char *fmt,
9804 unsigned int sreg, unsigned int treg)
9805{
9806 int noreorder = mips_opts.noreorder;
9807 expressionS expr1;
9808
9809 gas_assert (mips_opts.micromips);
9810 start_noreorder ();
9811 if (noreorder)
9812 {
9813 micromips_label_expr (&expr1);
9814 macro_build (&expr1, brneg, fmt, sreg, treg);
9815 macro_build (NULL, "nop", "");
9816 macro_build (ep, call ? "bal" : "b", "p");
9817
9818 /* Set to true so that append_insn adds a label. */
9819 emit_branch_likely_macro = TRUE;
9820 }
9821 else
9822 {
9823 macro_build (ep, br, fmt, sreg, treg);
9824 macro_build (NULL, "nop", "");
9825 }
9826 end_noreorder ();
9827}
9828
9829/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9830 the condition code tested. EP specifies the branch target. */
9831
9832static void
9833macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9834{
9835 const int call = 0;
9836 const char *brneg;
9837 const char *br;
9838
9839 switch (type)
9840 {
9841 case M_BC1FL:
9842 br = "bc1f";
9843 brneg = "bc1t";
9844 break;
9845 case M_BC1TL:
9846 br = "bc1t";
9847 brneg = "bc1f";
9848 break;
9849 case M_BC2FL:
9850 br = "bc2f";
9851 brneg = "bc2t";
9852 break;
9853 case M_BC2TL:
9854 br = "bc2t";
9855 brneg = "bc2f";
9856 break;
9857 default:
9858 abort ();
9859 }
9860 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9861}
9862
9863/* Emit a two-argument branch macro specified by TYPE, using SREG as
9864 the register tested. EP specifies the branch target. */
9865
9866static void
9867macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9868{
9869 const char *brneg = NULL;
9870 const char *br;
9871 int call = 0;
9872
9873 switch (type)
9874 {
9875 case M_BGEZ:
9876 br = "bgez";
9877 break;
9878 case M_BGEZL:
9879 br = mips_opts.micromips ? "bgez" : "bgezl";
9880 brneg = "bltz";
9881 break;
9882 case M_BGEZALL:
9883 gas_assert (mips_opts.micromips);
833794fc 9884 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9885 brneg = "bltz";
9886 call = 1;
9887 break;
9888 case M_BGTZ:
9889 br = "bgtz";
9890 break;
9891 case M_BGTZL:
9892 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9893 brneg = "blez";
9894 break;
9895 case M_BLEZ:
9896 br = "blez";
9897 break;
9898 case M_BLEZL:
9899 br = mips_opts.micromips ? "blez" : "blezl";
9900 brneg = "bgtz";
9901 break;
9902 case M_BLTZ:
9903 br = "bltz";
9904 break;
9905 case M_BLTZL:
9906 br = mips_opts.micromips ? "bltz" : "bltzl";
9907 brneg = "bgez";
9908 break;
9909 case M_BLTZALL:
9910 gas_assert (mips_opts.micromips);
833794fc 9911 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9912 brneg = "bgez";
9913 call = 1;
9914 break;
9915 default:
9916 abort ();
9917 }
9918 if (mips_opts.micromips && brneg)
9919 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9920 else
9921 macro_build (ep, br, "s,p", sreg);
9922}
9923
9924/* Emit a three-argument branch macro specified by TYPE, using SREG and
9925 TREG as the registers tested. EP specifies the branch target. */
9926
9927static void
9928macro_build_branch_rsrt (int type, expressionS *ep,
9929 unsigned int sreg, unsigned int treg)
9930{
9931 const char *brneg = NULL;
9932 const int call = 0;
9933 const char *br;
9934
9935 switch (type)
9936 {
9937 case M_BEQ:
9938 case M_BEQ_I:
9939 br = "beq";
9940 break;
9941 case M_BEQL:
9942 case M_BEQL_I:
9943 br = mips_opts.micromips ? "beq" : "beql";
9944 brneg = "bne";
9945 break;
9946 case M_BNE:
9947 case M_BNE_I:
9948 br = "bne";
9949 break;
9950 case M_BNEL:
9951 case M_BNEL_I:
9952 br = mips_opts.micromips ? "bne" : "bnel";
9953 brneg = "beq";
9954 break;
9955 default:
9956 abort ();
9957 }
9958 if (mips_opts.micromips && brneg)
9959 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9960 else
9961 macro_build (ep, br, "s,t,p", sreg, treg);
9962}
9963
f2ae14a1
RS
9964/* Return the high part that should be loaded in order to make the low
9965 part of VALUE accessible using an offset of OFFBITS bits. */
9966
9967static offsetT
9968offset_high_part (offsetT value, unsigned int offbits)
9969{
9970 offsetT bias;
9971 addressT low_mask;
9972
9973 if (offbits == 0)
9974 return value;
9975 bias = 1 << (offbits - 1);
9976 low_mask = bias * 2 - 1;
9977 return (value + bias) & ~low_mask;
9978}
9979
9980/* Return true if the value stored in offset_expr and offset_reloc
9981 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9982 amount that the caller wants to add without inducing overflow
9983 and ALIGN is the known alignment of the value in bytes. */
9984
9985static bfd_boolean
9986small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9987{
9988 if (offbits == 16)
9989 {
9990 /* Accept any relocation operator if overflow isn't a concern. */
9991 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9992 return TRUE;
9993
9994 /* These relocations are guaranteed not to overflow in correct links. */
9995 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9996 || gprel16_reloc_p (*offset_reloc))
9997 return TRUE;
9998 }
9999 if (offset_expr.X_op == O_constant
10000 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10001 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10002 return TRUE;
10003 return FALSE;
10004}
10005
252b5132
RH
10006/*
10007 * Build macros
10008 * This routine implements the seemingly endless macro or synthesized
10009 * instructions and addressing modes in the mips assembly language. Many
10010 * of these macros are simple and are similar to each other. These could
67c1ffbe 10011 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10012 * this verbose method. Others are not simple macros but are more like
10013 * optimizing code generation.
10014 * One interesting optimization is when several store macros appear
67c1ffbe 10015 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10016 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10017 * of global optimization. We currently only optimize within a single macro.
10018 * For many of the load and store macros if the address is specified as a
10019 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10020 * first load register 'at' with zero and use it as the base register. The
10021 * mips assembler simply uses register $zero. Just one tiny optimization
10022 * we're missing.
10023 */
10024static void
833794fc 10025macro (struct mips_cl_insn *ip, char *str)
252b5132 10026{
c0ebe874
RS
10027 const struct mips_operand_array *operands;
10028 unsigned int breg, i;
741fe287 10029 unsigned int tempreg;
252b5132 10030 int mask;
43841e91 10031 int used_at = 0;
df58fc94 10032 expressionS label_expr;
252b5132 10033 expressionS expr1;
df58fc94 10034 expressionS *ep;
252b5132
RH
10035 const char *s;
10036 const char *s2;
10037 const char *fmt;
10038 int likely = 0;
252b5132 10039 int coproc = 0;
7f3c4072 10040 int offbits = 16;
1abe91b1 10041 int call = 0;
df58fc94
RS
10042 int jals = 0;
10043 int dbl = 0;
10044 int imm = 0;
10045 int ust = 0;
10046 int lp = 0;
f2ae14a1 10047 bfd_boolean large_offset;
252b5132 10048 int off;
252b5132 10049 int hold_mips_optimize;
f2ae14a1 10050 unsigned int align;
c0ebe874 10051 unsigned int op[MAX_OPERANDS];
252b5132 10052
9c2799c2 10053 gas_assert (! mips_opts.mips16);
252b5132 10054
c0ebe874
RS
10055 operands = insn_operands (ip);
10056 for (i = 0; i < MAX_OPERANDS; i++)
10057 if (operands->operand[i])
10058 op[i] = insn_extract_operand (ip, operands->operand[i]);
10059 else
10060 op[i] = -1;
10061
252b5132
RH
10062 mask = ip->insn_mo->mask;
10063
df58fc94
RS
10064 label_expr.X_op = O_constant;
10065 label_expr.X_op_symbol = NULL;
10066 label_expr.X_add_symbol = NULL;
10067 label_expr.X_add_number = 0;
10068
252b5132
RH
10069 expr1.X_op = O_constant;
10070 expr1.X_op_symbol = NULL;
10071 expr1.X_add_symbol = NULL;
10072 expr1.X_add_number = 1;
f2ae14a1 10073 align = 1;
252b5132
RH
10074
10075 switch (mask)
10076 {
10077 case M_DABS:
10078 dbl = 1;
1a0670f3 10079 /* Fall through. */
252b5132 10080 case M_ABS:
df58fc94
RS
10081 /* bgez $a0,1f
10082 move v0,$a0
10083 sub v0,$zero,$a0
10084 1:
10085 */
252b5132 10086
7d10b47d 10087 start_noreorder ();
252b5132 10088
df58fc94
RS
10089 if (mips_opts.micromips)
10090 micromips_label_expr (&label_expr);
10091 else
10092 label_expr.X_add_number = 8;
c0ebe874
RS
10093 macro_build (&label_expr, "bgez", "s,p", op[1]);
10094 if (op[0] == op[1])
a605d2b3 10095 macro_build (NULL, "nop", "");
252b5132 10096 else
c0ebe874
RS
10097 move_register (op[0], op[1]);
10098 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10099 if (mips_opts.micromips)
10100 micromips_add_label ();
252b5132 10101
7d10b47d 10102 end_noreorder ();
8fc2e39e 10103 break;
252b5132
RH
10104
10105 case M_ADD_I:
10106 s = "addi";
10107 s2 = "add";
10108 goto do_addi;
10109 case M_ADDU_I:
10110 s = "addiu";
10111 s2 = "addu";
10112 goto do_addi;
10113 case M_DADD_I:
10114 dbl = 1;
10115 s = "daddi";
10116 s2 = "dadd";
df58fc94
RS
10117 if (!mips_opts.micromips)
10118 goto do_addi;
b0e6f033 10119 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10120 && imm_expr.X_add_number < 0x200)
10121 {
b0e6f033
RS
10122 macro_build (NULL, s, "t,r,.", op[0], op[1],
10123 (int) imm_expr.X_add_number);
df58fc94
RS
10124 break;
10125 }
10126 goto do_addi_i;
252b5132
RH
10127 case M_DADDU_I:
10128 dbl = 1;
10129 s = "daddiu";
10130 s2 = "daddu";
10131 do_addi:
b0e6f033 10132 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10133 && imm_expr.X_add_number < 0x8000)
10134 {
c0ebe874 10135 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10136 break;
252b5132 10137 }
df58fc94 10138 do_addi_i:
8fc2e39e 10139 used_at = 1;
67c0d1eb 10140 load_register (AT, &imm_expr, dbl);
c0ebe874 10141 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10142 break;
10143
10144 case M_AND_I:
10145 s = "andi";
10146 s2 = "and";
10147 goto do_bit;
10148 case M_OR_I:
10149 s = "ori";
10150 s2 = "or";
10151 goto do_bit;
10152 case M_NOR_I:
10153 s = "";
10154 s2 = "nor";
10155 goto do_bit;
10156 case M_XOR_I:
10157 s = "xori";
10158 s2 = "xor";
10159 do_bit:
b0e6f033 10160 if (imm_expr.X_add_number >= 0
252b5132
RH
10161 && imm_expr.X_add_number < 0x10000)
10162 {
10163 if (mask != M_NOR_I)
c0ebe874 10164 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10165 else
10166 {
67c0d1eb 10167 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10168 op[0], op[1], BFD_RELOC_LO16);
10169 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10170 }
8fc2e39e 10171 break;
252b5132
RH
10172 }
10173
8fc2e39e 10174 used_at = 1;
bad1aba3 10175 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10176 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10177 break;
10178
8b082fb1
TS
10179 case M_BALIGN:
10180 switch (imm_expr.X_add_number)
10181 {
10182 case 0:
10183 macro_build (NULL, "nop", "");
10184 break;
10185 case 2:
c0ebe874 10186 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10187 break;
03f66e8a
MR
10188 case 1:
10189 case 3:
c0ebe874 10190 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10191 (int) imm_expr.X_add_number);
8b082fb1 10192 break;
03f66e8a
MR
10193 default:
10194 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10195 (unsigned long) imm_expr.X_add_number);
10196 break;
8b082fb1
TS
10197 }
10198 break;
10199
df58fc94
RS
10200 case M_BC1FL:
10201 case M_BC1TL:
10202 case M_BC2FL:
10203 case M_BC2TL:
10204 gas_assert (mips_opts.micromips);
10205 macro_build_branch_ccl (mask, &offset_expr,
10206 EXTRACT_OPERAND (1, BCC, *ip));
10207 break;
10208
252b5132 10209 case M_BEQ_I:
252b5132 10210 case M_BEQL_I:
252b5132 10211 case M_BNE_I:
252b5132 10212 case M_BNEL_I:
b0e6f033 10213 if (imm_expr.X_add_number == 0)
c0ebe874 10214 op[1] = 0;
df58fc94 10215 else
252b5132 10216 {
c0ebe874 10217 op[1] = AT;
df58fc94 10218 used_at = 1;
bad1aba3 10219 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10220 }
df58fc94
RS
10221 /* Fall through. */
10222 case M_BEQL:
10223 case M_BNEL:
c0ebe874 10224 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10225 break;
10226
10227 case M_BGEL:
10228 likely = 1;
1a0670f3 10229 /* Fall through. */
252b5132 10230 case M_BGE:
c0ebe874
RS
10231 if (op[1] == 0)
10232 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10233 else if (op[0] == 0)
10234 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10235 else
252b5132 10236 {
df58fc94 10237 used_at = 1;
c0ebe874 10238 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10239 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10240 &offset_expr, AT, ZERO);
252b5132 10241 }
df58fc94
RS
10242 break;
10243
10244 case M_BGEZL:
10245 case M_BGEZALL:
10246 case M_BGTZL:
10247 case M_BLEZL:
10248 case M_BLTZL:
10249 case M_BLTZALL:
c0ebe874 10250 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10251 break;
10252
10253 case M_BGTL_I:
10254 likely = 1;
1a0670f3 10255 /* Fall through. */
252b5132 10256 case M_BGT_I:
90ecf173 10257 /* Check for > max integer. */
b0e6f033 10258 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10259 {
10260 do_false:
90ecf173 10261 /* Result is always false. */
252b5132 10262 if (! likely)
a605d2b3 10263 macro_build (NULL, "nop", "");
252b5132 10264 else
df58fc94 10265 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10266 break;
252b5132 10267 }
f9419b05 10268 ++imm_expr.X_add_number;
252b5132
RH
10269 /* FALLTHROUGH */
10270 case M_BGE_I:
10271 case M_BGEL_I:
10272 if (mask == M_BGEL_I)
10273 likely = 1;
b0e6f033 10274 if (imm_expr.X_add_number == 0)
252b5132 10275 {
df58fc94 10276 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10277 &offset_expr, op[0]);
8fc2e39e 10278 break;
252b5132 10279 }
b0e6f033 10280 if (imm_expr.X_add_number == 1)
252b5132 10281 {
df58fc94 10282 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10283 &offset_expr, op[0]);
8fc2e39e 10284 break;
252b5132 10285 }
b0e6f033 10286 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10287 {
10288 do_true:
10289 /* result is always true */
1661c76c 10290 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10291 macro_build (&offset_expr, "b", "p");
8fc2e39e 10292 break;
252b5132 10293 }
8fc2e39e 10294 used_at = 1;
c0ebe874 10295 set_at (op[0], 0);
df58fc94
RS
10296 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10297 &offset_expr, AT, ZERO);
252b5132
RH
10298 break;
10299
10300 case M_BGEUL:
10301 likely = 1;
1a0670f3 10302 /* Fall through. */
252b5132 10303 case M_BGEU:
c0ebe874 10304 if (op[1] == 0)
252b5132 10305 goto do_true;
c0ebe874 10306 else if (op[0] == 0)
df58fc94 10307 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10308 &offset_expr, ZERO, op[1]);
df58fc94 10309 else
252b5132 10310 {
df58fc94 10311 used_at = 1;
c0ebe874 10312 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10313 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10314 &offset_expr, AT, ZERO);
252b5132 10315 }
252b5132
RH
10316 break;
10317
10318 case M_BGTUL_I:
10319 likely = 1;
1a0670f3 10320 /* Fall through. */
252b5132 10321 case M_BGTU_I:
c0ebe874 10322 if (op[0] == 0
bad1aba3 10323 || (GPR_SIZE == 32
f01dc953 10324 && imm_expr.X_add_number == -1))
252b5132 10325 goto do_false;
f9419b05 10326 ++imm_expr.X_add_number;
252b5132
RH
10327 /* FALLTHROUGH */
10328 case M_BGEU_I:
10329 case M_BGEUL_I:
10330 if (mask == M_BGEUL_I)
10331 likely = 1;
b0e6f033 10332 if (imm_expr.X_add_number == 0)
252b5132 10333 goto do_true;
b0e6f033 10334 else if (imm_expr.X_add_number == 1)
df58fc94 10335 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10336 &offset_expr, op[0], ZERO);
df58fc94 10337 else
252b5132 10338 {
df58fc94 10339 used_at = 1;
c0ebe874 10340 set_at (op[0], 1);
df58fc94
RS
10341 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10342 &offset_expr, AT, ZERO);
252b5132 10343 }
252b5132
RH
10344 break;
10345
10346 case M_BGTL:
10347 likely = 1;
1a0670f3 10348 /* Fall through. */
252b5132 10349 case M_BGT:
c0ebe874
RS
10350 if (op[1] == 0)
10351 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10352 else if (op[0] == 0)
10353 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10354 else
252b5132 10355 {
df58fc94 10356 used_at = 1;
c0ebe874 10357 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10358 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10359 &offset_expr, AT, ZERO);
252b5132 10360 }
252b5132
RH
10361 break;
10362
10363 case M_BGTUL:
10364 likely = 1;
1a0670f3 10365 /* Fall through. */
252b5132 10366 case M_BGTU:
c0ebe874 10367 if (op[1] == 0)
df58fc94 10368 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10369 &offset_expr, op[0], ZERO);
10370 else if (op[0] == 0)
df58fc94
RS
10371 goto do_false;
10372 else
252b5132 10373 {
df58fc94 10374 used_at = 1;
c0ebe874 10375 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10376 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10377 &offset_expr, AT, ZERO);
252b5132 10378 }
252b5132
RH
10379 break;
10380
10381 case M_BLEL:
10382 likely = 1;
1a0670f3 10383 /* Fall through. */
252b5132 10384 case M_BLE:
c0ebe874
RS
10385 if (op[1] == 0)
10386 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10387 else if (op[0] == 0)
10388 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10389 else
252b5132 10390 {
df58fc94 10391 used_at = 1;
c0ebe874 10392 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10393 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10394 &offset_expr, AT, ZERO);
252b5132 10395 }
252b5132
RH
10396 break;
10397
10398 case M_BLEL_I:
10399 likely = 1;
1a0670f3 10400 /* Fall through. */
252b5132 10401 case M_BLE_I:
b0e6f033 10402 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10403 goto do_true;
f9419b05 10404 ++imm_expr.X_add_number;
252b5132
RH
10405 /* FALLTHROUGH */
10406 case M_BLT_I:
10407 case M_BLTL_I:
10408 if (mask == M_BLTL_I)
10409 likely = 1;
b0e6f033 10410 if (imm_expr.X_add_number == 0)
c0ebe874 10411 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10412 else if (imm_expr.X_add_number == 1)
c0ebe874 10413 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10414 else
252b5132 10415 {
df58fc94 10416 used_at = 1;
c0ebe874 10417 set_at (op[0], 0);
df58fc94
RS
10418 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10419 &offset_expr, AT, ZERO);
252b5132 10420 }
252b5132
RH
10421 break;
10422
10423 case M_BLEUL:
10424 likely = 1;
1a0670f3 10425 /* Fall through. */
252b5132 10426 case M_BLEU:
c0ebe874 10427 if (op[1] == 0)
df58fc94 10428 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10429 &offset_expr, op[0], ZERO);
10430 else if (op[0] == 0)
df58fc94
RS
10431 goto do_true;
10432 else
252b5132 10433 {
df58fc94 10434 used_at = 1;
c0ebe874 10435 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10436 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10437 &offset_expr, AT, ZERO);
252b5132 10438 }
252b5132
RH
10439 break;
10440
10441 case M_BLEUL_I:
10442 likely = 1;
1a0670f3 10443 /* Fall through. */
252b5132 10444 case M_BLEU_I:
c0ebe874 10445 if (op[0] == 0
bad1aba3 10446 || (GPR_SIZE == 32
f01dc953 10447 && imm_expr.X_add_number == -1))
252b5132 10448 goto do_true;
f9419b05 10449 ++imm_expr.X_add_number;
252b5132
RH
10450 /* FALLTHROUGH */
10451 case M_BLTU_I:
10452 case M_BLTUL_I:
10453 if (mask == M_BLTUL_I)
10454 likely = 1;
b0e6f033 10455 if (imm_expr.X_add_number == 0)
252b5132 10456 goto do_false;
b0e6f033 10457 else if (imm_expr.X_add_number == 1)
df58fc94 10458 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10459 &offset_expr, op[0], ZERO);
df58fc94 10460 else
252b5132 10461 {
df58fc94 10462 used_at = 1;
c0ebe874 10463 set_at (op[0], 1);
df58fc94
RS
10464 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10465 &offset_expr, AT, ZERO);
252b5132 10466 }
252b5132
RH
10467 break;
10468
10469 case M_BLTL:
10470 likely = 1;
1a0670f3 10471 /* Fall through. */
252b5132 10472 case M_BLT:
c0ebe874
RS
10473 if (op[1] == 0)
10474 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10475 else if (op[0] == 0)
10476 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10477 else
252b5132 10478 {
df58fc94 10479 used_at = 1;
c0ebe874 10480 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10481 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10482 &offset_expr, AT, ZERO);
252b5132 10483 }
252b5132
RH
10484 break;
10485
10486 case M_BLTUL:
10487 likely = 1;
1a0670f3 10488 /* Fall through. */
252b5132 10489 case M_BLTU:
c0ebe874 10490 if (op[1] == 0)
252b5132 10491 goto do_false;
c0ebe874 10492 else if (op[0] == 0)
df58fc94 10493 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10494 &offset_expr, ZERO, op[1]);
df58fc94 10495 else
252b5132 10496 {
df58fc94 10497 used_at = 1;
c0ebe874 10498 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10499 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10500 &offset_expr, AT, ZERO);
252b5132 10501 }
252b5132
RH
10502 break;
10503
10504 case M_DDIV_3:
10505 dbl = 1;
1a0670f3 10506 /* Fall through. */
252b5132
RH
10507 case M_DIV_3:
10508 s = "mflo";
10509 goto do_div3;
10510 case M_DREM_3:
10511 dbl = 1;
1a0670f3 10512 /* Fall through. */
252b5132
RH
10513 case M_REM_3:
10514 s = "mfhi";
10515 do_div3:
c0ebe874 10516 if (op[2] == 0)
252b5132 10517 {
1661c76c 10518 as_warn (_("divide by zero"));
252b5132 10519 if (mips_trap)
df58fc94 10520 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10521 else
df58fc94 10522 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10523 break;
252b5132
RH
10524 }
10525
7d10b47d 10526 start_noreorder ();
252b5132
RH
10527 if (mips_trap)
10528 {
c0ebe874
RS
10529 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10530 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10531 }
10532 else
10533 {
df58fc94
RS
10534 if (mips_opts.micromips)
10535 micromips_label_expr (&label_expr);
10536 else
10537 label_expr.X_add_number = 8;
c0ebe874
RS
10538 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10539 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10540 macro_build (NULL, "break", BRK_FMT, 7);
10541 if (mips_opts.micromips)
10542 micromips_add_label ();
252b5132
RH
10543 }
10544 expr1.X_add_number = -1;
8fc2e39e 10545 used_at = 1;
f6a22291 10546 load_register (AT, &expr1, dbl);
df58fc94
RS
10547 if (mips_opts.micromips)
10548 micromips_label_expr (&label_expr);
10549 else
10550 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10551 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10552 if (dbl)
10553 {
10554 expr1.X_add_number = 1;
f6a22291 10555 load_register (AT, &expr1, dbl);
df58fc94 10556 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10557 }
10558 else
10559 {
10560 expr1.X_add_number = 0x80000000;
df58fc94 10561 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10562 }
10563 if (mips_trap)
10564 {
c0ebe874 10565 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10566 /* We want to close the noreorder block as soon as possible, so
10567 that later insns are available for delay slot filling. */
7d10b47d 10568 end_noreorder ();
252b5132
RH
10569 }
10570 else
10571 {
df58fc94
RS
10572 if (mips_opts.micromips)
10573 micromips_label_expr (&label_expr);
10574 else
10575 label_expr.X_add_number = 8;
c0ebe874 10576 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10577 macro_build (NULL, "nop", "");
252b5132
RH
10578
10579 /* We want to close the noreorder block as soon as possible, so
10580 that later insns are available for delay slot filling. */
7d10b47d 10581 end_noreorder ();
252b5132 10582
df58fc94 10583 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10584 }
df58fc94
RS
10585 if (mips_opts.micromips)
10586 micromips_add_label ();
c0ebe874 10587 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10588 break;
10589
10590 case M_DIV_3I:
10591 s = "div";
10592 s2 = "mflo";
10593 goto do_divi;
10594 case M_DIVU_3I:
10595 s = "divu";
10596 s2 = "mflo";
10597 goto do_divi;
10598 case M_REM_3I:
10599 s = "div";
10600 s2 = "mfhi";
10601 goto do_divi;
10602 case M_REMU_3I:
10603 s = "divu";
10604 s2 = "mfhi";
10605 goto do_divi;
10606 case M_DDIV_3I:
10607 dbl = 1;
10608 s = "ddiv";
10609 s2 = "mflo";
10610 goto do_divi;
10611 case M_DDIVU_3I:
10612 dbl = 1;
10613 s = "ddivu";
10614 s2 = "mflo";
10615 goto do_divi;
10616 case M_DREM_3I:
10617 dbl = 1;
10618 s = "ddiv";
10619 s2 = "mfhi";
10620 goto do_divi;
10621 case M_DREMU_3I:
10622 dbl = 1;
10623 s = "ddivu";
10624 s2 = "mfhi";
10625 do_divi:
b0e6f033 10626 if (imm_expr.X_add_number == 0)
252b5132 10627 {
1661c76c 10628 as_warn (_("divide by zero"));
252b5132 10629 if (mips_trap)
df58fc94 10630 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10631 else
df58fc94 10632 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10633 break;
252b5132 10634 }
b0e6f033 10635 if (imm_expr.X_add_number == 1)
252b5132
RH
10636 {
10637 if (strcmp (s2, "mflo") == 0)
c0ebe874 10638 move_register (op[0], op[1]);
252b5132 10639 else
c0ebe874 10640 move_register (op[0], ZERO);
8fc2e39e 10641 break;
252b5132 10642 }
b0e6f033 10643 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10644 {
10645 if (strcmp (s2, "mflo") == 0)
c0ebe874 10646 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10647 else
c0ebe874 10648 move_register (op[0], ZERO);
8fc2e39e 10649 break;
252b5132
RH
10650 }
10651
8fc2e39e 10652 used_at = 1;
67c0d1eb 10653 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10654 macro_build (NULL, s, "z,s,t", op[1], AT);
10655 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10656 break;
10657
10658 case M_DIVU_3:
10659 s = "divu";
10660 s2 = "mflo";
10661 goto do_divu3;
10662 case M_REMU_3:
10663 s = "divu";
10664 s2 = "mfhi";
10665 goto do_divu3;
10666 case M_DDIVU_3:
10667 s = "ddivu";
10668 s2 = "mflo";
10669 goto do_divu3;
10670 case M_DREMU_3:
10671 s = "ddivu";
10672 s2 = "mfhi";
10673 do_divu3:
7d10b47d 10674 start_noreorder ();
252b5132
RH
10675 if (mips_trap)
10676 {
c0ebe874
RS
10677 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10678 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10679 /* We want to close the noreorder block as soon as possible, so
10680 that later insns are available for delay slot filling. */
7d10b47d 10681 end_noreorder ();
252b5132
RH
10682 }
10683 else
10684 {
df58fc94
RS
10685 if (mips_opts.micromips)
10686 micromips_label_expr (&label_expr);
10687 else
10688 label_expr.X_add_number = 8;
c0ebe874
RS
10689 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10690 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10691
10692 /* We want to close the noreorder block as soon as possible, so
10693 that later insns are available for delay slot filling. */
7d10b47d 10694 end_noreorder ();
df58fc94
RS
10695 macro_build (NULL, "break", BRK_FMT, 7);
10696 if (mips_opts.micromips)
10697 micromips_add_label ();
252b5132 10698 }
c0ebe874 10699 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10700 break;
252b5132 10701
1abe91b1
MR
10702 case M_DLCA_AB:
10703 dbl = 1;
1a0670f3 10704 /* Fall through. */
1abe91b1
MR
10705 case M_LCA_AB:
10706 call = 1;
10707 goto do_la;
252b5132
RH
10708 case M_DLA_AB:
10709 dbl = 1;
1a0670f3 10710 /* Fall through. */
252b5132 10711 case M_LA_AB:
1abe91b1 10712 do_la:
252b5132
RH
10713 /* Load the address of a symbol into a register. If breg is not
10714 zero, we then add a base register to it. */
10715
c0ebe874 10716 breg = op[2];
bad1aba3 10717 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10718 as_warn (_("dla used to load 32-bit register; recommend using la "
10719 "instead"));
3bec30a8 10720
90ecf173 10721 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10722 as_warn (_("la used to load 64-bit address; recommend using dla "
10723 "instead"));
3bec30a8 10724
f2ae14a1 10725 if (small_offset_p (0, align, 16))
0c11417f 10726 {
c0ebe874 10727 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10728 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10729 break;
0c11417f
MR
10730 }
10731
c0ebe874 10732 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10733 {
10734 tempreg = AT;
10735 used_at = 1;
10736 }
10737 else
c0ebe874 10738 tempreg = op[0];
afdbd6d0 10739
252b5132
RH
10740 if (offset_expr.X_op != O_symbol
10741 && offset_expr.X_op != O_constant)
10742 {
1661c76c 10743 as_bad (_("expression too complex"));
252b5132
RH
10744 offset_expr.X_op = O_constant;
10745 }
10746
252b5132 10747 if (offset_expr.X_op == O_constant)
aed1a261 10748 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10749 else if (mips_pic == NO_PIC)
10750 {
d6bc6245 10751 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10752 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10753 Otherwise we want
10754 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10755 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10756 If we have a constant, we need two instructions anyhow,
d6bc6245 10757 so we may as well always use the latter form.
76b3015f 10758
6caf9ef4
TS
10759 With 64bit address space and a usable $at we want
10760 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10761 lui $at,<sym> (BFD_RELOC_HI16_S)
10762 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10763 daddiu $at,<sym> (BFD_RELOC_LO16)
10764 dsll32 $tempreg,0
10765 daddu $tempreg,$tempreg,$at
10766
10767 If $at is already in use, we use a path which is suboptimal
10768 on superscalar processors.
10769 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10770 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10771 dsll $tempreg,16
10772 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10773 dsll $tempreg,16
10774 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10775
10776 For GP relative symbols in 64bit address space we can use
10777 the same sequence as in 32bit address space. */
aed1a261 10778 if (HAVE_64BIT_SYMBOLS)
252b5132 10779 {
6caf9ef4
TS
10780 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10781 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10782 {
10783 relax_start (offset_expr.X_add_symbol);
10784 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10785 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10786 relax_switch ();
10787 }
d6bc6245 10788
741fe287 10789 if (used_at == 0 && mips_opts.at)
98d3f06f 10790 {
df58fc94 10791 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10792 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10793 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10794 AT, BFD_RELOC_HI16_S);
67c0d1eb 10795 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10796 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10797 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10798 AT, AT, BFD_RELOC_LO16);
df58fc94 10799 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10800 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10801 used_at = 1;
10802 }
10803 else
10804 {
df58fc94 10805 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10806 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10807 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10808 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10809 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10810 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10811 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10812 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10813 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10814 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10815 }
6caf9ef4
TS
10816
10817 if (mips_relax.sequence)
10818 relax_end ();
98d3f06f
KH
10819 }
10820 else
10821 {
10822 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10823 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10824 {
4d7206a2 10825 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10826 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10827 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10828 relax_switch ();
98d3f06f 10829 }
6943caf0 10830 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10831 as_bad (_("offset too large"));
67c0d1eb
RS
10832 macro_build_lui (&offset_expr, tempreg);
10833 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10834 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10835 if (mips_relax.sequence)
10836 relax_end ();
98d3f06f 10837 }
252b5132 10838 }
0a44bf69 10839 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10840 {
9117d219
NC
10841 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10842
252b5132
RH
10843 /* If this is a reference to an external symbol, and there
10844 is no constant, we want
10845 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10846 or for lca or if tempreg is PIC_CALL_REG
9117d219 10847 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10848 For a local symbol, we want
10849 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10850 nop
10851 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10852
10853 If we have a small constant, and this is a reference to
10854 an external symbol, we want
10855 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10856 nop
10857 addiu $tempreg,$tempreg,<constant>
10858 For a local symbol, we want the same instruction
10859 sequence, but we output a BFD_RELOC_LO16 reloc on the
10860 addiu instruction.
10861
10862 If we have a large constant, and this is a reference to
10863 an external symbol, we want
10864 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10865 lui $at,<hiconstant>
10866 addiu $at,$at,<loconstant>
10867 addu $tempreg,$tempreg,$at
10868 For a local symbol, we want the same instruction
10869 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10870 addiu instruction.
ed6fb7bd
SC
10871 */
10872
4d7206a2 10873 if (offset_expr.X_add_number == 0)
252b5132 10874 {
0a44bf69
RS
10875 if (mips_pic == SVR4_PIC
10876 && breg == 0
10877 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10878 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10879
10880 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10881 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10882 lw_reloc_type, mips_gp_register);
4d7206a2 10883 if (breg != 0)
252b5132
RH
10884 {
10885 /* We're going to put in an addu instruction using
10886 tempreg, so we may as well insert the nop right
10887 now. */
269137b2 10888 load_delay_nop ();
252b5132 10889 }
4d7206a2 10890 relax_switch ();
67c0d1eb
RS
10891 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10892 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10893 load_delay_nop ();
67c0d1eb
RS
10894 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10895 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10896 relax_end ();
252b5132
RH
10897 /* FIXME: If breg == 0, and the next instruction uses
10898 $tempreg, then if this variant case is used an extra
10899 nop will be generated. */
10900 }
4d7206a2
RS
10901 else if (offset_expr.X_add_number >= -0x8000
10902 && offset_expr.X_add_number < 0x8000)
252b5132 10903 {
67c0d1eb 10904 load_got_offset (tempreg, &offset_expr);
269137b2 10905 load_delay_nop ();
67c0d1eb 10906 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10907 }
10908 else
10909 {
4d7206a2
RS
10910 expr1.X_add_number = offset_expr.X_add_number;
10911 offset_expr.X_add_number =
43c0598f 10912 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10913 load_got_offset (tempreg, &offset_expr);
f6a22291 10914 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10915 /* If we are going to add in a base register, and the
10916 target register and the base register are the same,
10917 then we are using AT as a temporary register. Since
10918 we want to load the constant into AT, we add our
10919 current AT (from the global offset table) and the
10920 register into the register now, and pretend we were
10921 not using a base register. */
c0ebe874 10922 if (breg == op[0])
252b5132 10923 {
269137b2 10924 load_delay_nop ();
67c0d1eb 10925 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10926 op[0], AT, breg);
252b5132 10927 breg = 0;
c0ebe874 10928 tempreg = op[0];
252b5132 10929 }
f6a22291 10930 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10931 used_at = 1;
10932 }
10933 }
0a44bf69 10934 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10935 {
67c0d1eb 10936 int add_breg_early = 0;
f5040a92
AO
10937
10938 /* If this is a reference to an external, and there is no
10939 constant, or local symbol (*), with or without a
10940 constant, we want
10941 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10942 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10943 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10944
10945 If we have a small constant, and this is a reference to
10946 an external symbol, we want
10947 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10948 addiu $tempreg,$tempreg,<constant>
10949
10950 If we have a large constant, and this is a reference to
10951 an external symbol, we want
10952 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10953 lui $at,<hiconstant>
10954 addiu $at,$at,<loconstant>
10955 addu $tempreg,$tempreg,$at
10956
10957 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10958 local symbols, even though it introduces an additional
10959 instruction. */
10960
f5040a92
AO
10961 if (offset_expr.X_add_number)
10962 {
4d7206a2 10963 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10964 offset_expr.X_add_number = 0;
10965
4d7206a2 10966 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10967 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10968 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10969
10970 if (expr1.X_add_number >= -0x8000
10971 && expr1.X_add_number < 0x8000)
10972 {
67c0d1eb
RS
10973 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10974 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10975 }
ecd13cd3 10976 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10977 {
c0ebe874
RS
10978 unsigned int dreg;
10979
f5040a92
AO
10980 /* If we are going to add in a base register, and the
10981 target register and the base register are the same,
10982 then we are using AT as a temporary register. Since
10983 we want to load the constant into AT, we add our
10984 current AT (from the global offset table) and the
10985 register into the register now, and pretend we were
10986 not using a base register. */
c0ebe874 10987 if (breg != op[0])
f5040a92
AO
10988 dreg = tempreg;
10989 else
10990 {
9c2799c2 10991 gas_assert (tempreg == AT);
67c0d1eb 10992 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10993 op[0], AT, breg);
10994 dreg = op[0];
67c0d1eb 10995 add_breg_early = 1;
f5040a92
AO
10996 }
10997
f6a22291 10998 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10999 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11000 dreg, dreg, AT);
f5040a92 11001
f5040a92
AO
11002 used_at = 1;
11003 }
11004 else
11005 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11006
4d7206a2 11007 relax_switch ();
f5040a92
AO
11008 offset_expr.X_add_number = expr1.X_add_number;
11009
67c0d1eb
RS
11010 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11011 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11012 if (add_breg_early)
f5040a92 11013 {
67c0d1eb 11014 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11015 op[0], tempreg, breg);
f5040a92 11016 breg = 0;
c0ebe874 11017 tempreg = op[0];
f5040a92 11018 }
4d7206a2 11019 relax_end ();
f5040a92 11020 }
4d7206a2 11021 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11022 {
4d7206a2 11023 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11024 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11025 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11026 relax_switch ();
67c0d1eb
RS
11027 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11028 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11029 relax_end ();
f5040a92 11030 }
4d7206a2 11031 else
f5040a92 11032 {
67c0d1eb
RS
11033 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11034 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11035 }
11036 }
0a44bf69 11037 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11038 {
67c0d1eb 11039 int gpdelay;
9117d219
NC
11040 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11041 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11042 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11043
11044 /* This is the large GOT case. If this is a reference to an
11045 external symbol, and there is no constant, we want
11046 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11047 addu $tempreg,$tempreg,$gp
11048 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11049 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11050 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11051 addu $tempreg,$tempreg,$gp
11052 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11053 For a local symbol, we want
11054 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11055 nop
11056 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11057
11058 If we have a small constant, and this is a reference to
11059 an external symbol, we want
11060 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11061 addu $tempreg,$tempreg,$gp
11062 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11063 nop
11064 addiu $tempreg,$tempreg,<constant>
11065 For a local symbol, we want
11066 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11067 nop
11068 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11069
11070 If we have a large constant, and this is a reference to
11071 an external symbol, we want
11072 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11073 addu $tempreg,$tempreg,$gp
11074 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11075 lui $at,<hiconstant>
11076 addiu $at,$at,<loconstant>
11077 addu $tempreg,$tempreg,$at
11078 For a local symbol, we want
11079 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11080 lui $at,<hiconstant>
11081 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11082 addu $tempreg,$tempreg,$at
f5040a92 11083 */
438c16b8 11084
252b5132
RH
11085 expr1.X_add_number = offset_expr.X_add_number;
11086 offset_expr.X_add_number = 0;
4d7206a2 11087 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11088 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11089 if (expr1.X_add_number == 0 && breg == 0
11090 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11091 {
11092 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11093 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11094 }
df58fc94 11095 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11096 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11097 tempreg, tempreg, mips_gp_register);
67c0d1eb 11098 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11099 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11100 if (expr1.X_add_number == 0)
11101 {
67c0d1eb 11102 if (breg != 0)
252b5132
RH
11103 {
11104 /* We're going to put in an addu instruction using
11105 tempreg, so we may as well insert the nop right
11106 now. */
269137b2 11107 load_delay_nop ();
252b5132 11108 }
252b5132
RH
11109 }
11110 else if (expr1.X_add_number >= -0x8000
11111 && expr1.X_add_number < 0x8000)
11112 {
269137b2 11113 load_delay_nop ();
67c0d1eb 11114 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11115 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11116 }
11117 else
11118 {
c0ebe874
RS
11119 unsigned int dreg;
11120
252b5132
RH
11121 /* If we are going to add in a base register, and the
11122 target register and the base register are the same,
11123 then we are using AT as a temporary register. Since
11124 we want to load the constant into AT, we add our
11125 current AT (from the global offset table) and the
11126 register into the register now, and pretend we were
11127 not using a base register. */
c0ebe874 11128 if (breg != op[0])
67c0d1eb 11129 dreg = tempreg;
252b5132
RH
11130 else
11131 {
9c2799c2 11132 gas_assert (tempreg == AT);
269137b2 11133 load_delay_nop ();
67c0d1eb 11134 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11135 op[0], AT, breg);
11136 dreg = op[0];
252b5132
RH
11137 }
11138
f6a22291 11139 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11140 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11141
252b5132
RH
11142 used_at = 1;
11143 }
43c0598f 11144 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11145 relax_switch ();
252b5132 11146
67c0d1eb 11147 if (gpdelay)
252b5132
RH
11148 {
11149 /* This is needed because this instruction uses $gp, but
f5040a92 11150 the first instruction on the main stream does not. */
67c0d1eb 11151 macro_build (NULL, "nop", "");
252b5132 11152 }
ed6fb7bd 11153
67c0d1eb
RS
11154 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11155 local_reloc_type, mips_gp_register);
f5040a92 11156 if (expr1.X_add_number >= -0x8000
252b5132
RH
11157 && expr1.X_add_number < 0x8000)
11158 {
269137b2 11159 load_delay_nop ();
67c0d1eb
RS
11160 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11161 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11162 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11163 register, the external symbol case ended with a load,
11164 so if the symbol turns out to not be external, and
11165 the next instruction uses tempreg, an unnecessary nop
11166 will be inserted. */
252b5132
RH
11167 }
11168 else
11169 {
c0ebe874 11170 if (breg == op[0])
252b5132
RH
11171 {
11172 /* We must add in the base register now, as in the
f5040a92 11173 external symbol case. */
9c2799c2 11174 gas_assert (tempreg == AT);
269137b2 11175 load_delay_nop ();
67c0d1eb 11176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11177 op[0], AT, breg);
11178 tempreg = op[0];
252b5132 11179 /* We set breg to 0 because we have arranged to add
f5040a92 11180 it in in both cases. */
252b5132
RH
11181 breg = 0;
11182 }
11183
67c0d1eb
RS
11184 macro_build_lui (&expr1, AT);
11185 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11186 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11187 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11188 tempreg, tempreg, AT);
8fc2e39e 11189 used_at = 1;
252b5132 11190 }
4d7206a2 11191 relax_end ();
252b5132 11192 }
0a44bf69 11193 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11194 {
f5040a92
AO
11195 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11196 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11197 int add_breg_early = 0;
f5040a92
AO
11198
11199 /* This is the large GOT case. If this is a reference to an
11200 external symbol, and there is no constant, we want
11201 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11202 add $tempreg,$tempreg,$gp
11203 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11204 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11205 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11206 add $tempreg,$tempreg,$gp
11207 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11208
11209 If we have a small constant, and this is a reference to
11210 an external symbol, we want
11211 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11212 add $tempreg,$tempreg,$gp
11213 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11214 addi $tempreg,$tempreg,<constant>
11215
11216 If we have a large constant, and this is a reference to
11217 an external symbol, we want
11218 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11219 addu $tempreg,$tempreg,$gp
11220 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11221 lui $at,<hiconstant>
11222 addi $at,$at,<loconstant>
11223 add $tempreg,$tempreg,$at
11224
11225 If we have NewABI, and we know it's a local symbol, we want
11226 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11227 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11228 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11229
4d7206a2 11230 relax_start (offset_expr.X_add_symbol);
f5040a92 11231
4d7206a2 11232 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11233 offset_expr.X_add_number = 0;
11234
1abe91b1
MR
11235 if (expr1.X_add_number == 0 && breg == 0
11236 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11237 {
11238 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11239 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11240 }
df58fc94 11241 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11242 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11243 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11244 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11245 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11246
11247 if (expr1.X_add_number == 0)
4d7206a2 11248 ;
f5040a92
AO
11249 else if (expr1.X_add_number >= -0x8000
11250 && expr1.X_add_number < 0x8000)
11251 {
67c0d1eb 11252 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11253 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11254 }
ecd13cd3 11255 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11256 {
c0ebe874
RS
11257 unsigned int dreg;
11258
f5040a92
AO
11259 /* If we are going to add in a base register, and the
11260 target register and the base register are the same,
11261 then we are using AT as a temporary register. Since
11262 we want to load the constant into AT, we add our
11263 current AT (from the global offset table) and the
11264 register into the register now, and pretend we were
11265 not using a base register. */
c0ebe874 11266 if (breg != op[0])
f5040a92
AO
11267 dreg = tempreg;
11268 else
11269 {
9c2799c2 11270 gas_assert (tempreg == AT);
67c0d1eb 11271 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11272 op[0], AT, breg);
11273 dreg = op[0];
67c0d1eb 11274 add_breg_early = 1;
f5040a92
AO
11275 }
11276
f6a22291 11277 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11278 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11279
f5040a92
AO
11280 used_at = 1;
11281 }
11282 else
11283 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11284
4d7206a2 11285 relax_switch ();
f5040a92 11286 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11287 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11288 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11289 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11290 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11291 if (add_breg_early)
f5040a92 11292 {
67c0d1eb 11293 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11294 op[0], tempreg, breg);
f5040a92 11295 breg = 0;
c0ebe874 11296 tempreg = op[0];
f5040a92 11297 }
4d7206a2 11298 relax_end ();
f5040a92 11299 }
252b5132
RH
11300 else
11301 abort ();
11302
11303 if (breg != 0)
c0ebe874 11304 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11305 break;
11306
52b6b6b9 11307 case M_MSGSND:
df58fc94 11308 gas_assert (!mips_opts.micromips);
c0ebe874 11309 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11310 break;
52b6b6b9
JM
11311
11312 case M_MSGLD:
df58fc94 11313 gas_assert (!mips_opts.micromips);
c8276761 11314 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11315 break;
52b6b6b9
JM
11316
11317 case M_MSGLD_T:
df58fc94 11318 gas_assert (!mips_opts.micromips);
c0ebe874 11319 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11320 break;
52b6b6b9
JM
11321
11322 case M_MSGWAIT:
df58fc94 11323 gas_assert (!mips_opts.micromips);
52b6b6b9 11324 macro_build (NULL, "c2", "C", 3);
c7af4273 11325 break;
52b6b6b9
JM
11326
11327 case M_MSGWAIT_T:
df58fc94 11328 gas_assert (!mips_opts.micromips);
c0ebe874 11329 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11330 break;
52b6b6b9 11331
252b5132
RH
11332 case M_J_A:
11333 /* The j instruction may not be used in PIC code, since it
11334 requires an absolute address. We convert it to a b
11335 instruction. */
11336 if (mips_pic == NO_PIC)
67c0d1eb 11337 macro_build (&offset_expr, "j", "a");
252b5132 11338 else
67c0d1eb 11339 macro_build (&offset_expr, "b", "p");
8fc2e39e 11340 break;
252b5132
RH
11341
11342 /* The jal instructions must be handled as macros because when
11343 generating PIC code they expand to multi-instruction
11344 sequences. Normally they are simple instructions. */
df58fc94 11345 case M_JALS_1:
c0ebe874
RS
11346 op[1] = op[0];
11347 op[0] = RA;
df58fc94
RS
11348 /* Fall through. */
11349 case M_JALS_2:
11350 gas_assert (mips_opts.micromips);
833794fc
MR
11351 if (mips_opts.insn32)
11352 {
1661c76c 11353 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11354 break;
11355 }
df58fc94
RS
11356 jals = 1;
11357 goto jal;
252b5132 11358 case M_JAL_1:
c0ebe874
RS
11359 op[1] = op[0];
11360 op[0] = RA;
252b5132
RH
11361 /* Fall through. */
11362 case M_JAL_2:
df58fc94 11363 jal:
3e722fb5 11364 if (mips_pic == NO_PIC)
df58fc94
RS
11365 {
11366 s = jals ? "jalrs" : "jalr";
e64af278 11367 if (mips_opts.micromips
833794fc 11368 && !mips_opts.insn32
c0ebe874 11369 && op[0] == RA
e64af278 11370 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11371 macro_build (NULL, s, "mj", op[1]);
df58fc94 11372 else
c0ebe874 11373 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11374 }
0a44bf69 11375 else
252b5132 11376 {
df58fc94
RS
11377 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11378 && mips_cprestore_offset >= 0);
11379
c0ebe874 11380 if (op[1] != PIC_CALL_REG)
252b5132 11381 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11382
833794fc
MR
11383 s = ((mips_opts.micromips
11384 && !mips_opts.insn32
11385 && (!mips_opts.noreorder || cprestore))
df58fc94 11386 ? "jalrs" : "jalr");
e64af278 11387 if (mips_opts.micromips
833794fc 11388 && !mips_opts.insn32
c0ebe874 11389 && op[0] == RA
e64af278 11390 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11391 macro_build (NULL, s, "mj", op[1]);
df58fc94 11392 else
c0ebe874 11393 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11394 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11395 {
6478892d 11396 if (mips_cprestore_offset < 0)
1661c76c 11397 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11398 else
11399 {
90ecf173 11400 if (!mips_frame_reg_valid)
7a621144 11401 {
1661c76c 11402 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11403 /* Quiet this warning. */
11404 mips_frame_reg_valid = 1;
11405 }
90ecf173 11406 if (!mips_cprestore_valid)
7a621144 11407 {
1661c76c 11408 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11409 /* Quiet this warning. */
11410 mips_cprestore_valid = 1;
11411 }
d3fca0b5
MR
11412 if (mips_opts.noreorder)
11413 macro_build (NULL, "nop", "");
6478892d 11414 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11415 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11416 mips_gp_register,
256ab948
TS
11417 mips_frame_reg,
11418 HAVE_64BIT_ADDRESSES);
6478892d 11419 }
252b5132
RH
11420 }
11421 }
252b5132 11422
8fc2e39e 11423 break;
252b5132 11424
df58fc94
RS
11425 case M_JALS_A:
11426 gas_assert (mips_opts.micromips);
833794fc
MR
11427 if (mips_opts.insn32)
11428 {
1661c76c 11429 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11430 break;
11431 }
df58fc94
RS
11432 jals = 1;
11433 /* Fall through. */
252b5132
RH
11434 case M_JAL_A:
11435 if (mips_pic == NO_PIC)
df58fc94 11436 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11437 else if (mips_pic == SVR4_PIC)
11438 {
11439 /* If this is a reference to an external symbol, and we are
11440 using a small GOT, we want
11441 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11442 nop
f9419b05 11443 jalr $ra,$25
252b5132
RH
11444 nop
11445 lw $gp,cprestore($sp)
11446 The cprestore value is set using the .cprestore
11447 pseudo-op. If we are using a big GOT, we want
11448 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11449 addu $25,$25,$gp
11450 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11451 nop
f9419b05 11452 jalr $ra,$25
252b5132
RH
11453 nop
11454 lw $gp,cprestore($sp)
11455 If the symbol is not external, we want
11456 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11457 nop
11458 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11459 jalr $ra,$25
252b5132 11460 nop
438c16b8 11461 lw $gp,cprestore($sp)
f5040a92
AO
11462
11463 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11464 sequences above, minus nops, unless the symbol is local,
11465 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11466 GOT_DISP. */
438c16b8 11467 if (HAVE_NEWABI)
252b5132 11468 {
90ecf173 11469 if (!mips_big_got)
f5040a92 11470 {
4d7206a2 11471 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11472 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11473 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11474 mips_gp_register);
4d7206a2 11475 relax_switch ();
67c0d1eb
RS
11476 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11477 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11478 mips_gp_register);
11479 relax_end ();
f5040a92
AO
11480 }
11481 else
11482 {
4d7206a2 11483 relax_start (offset_expr.X_add_symbol);
df58fc94 11484 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11485 BFD_RELOC_MIPS_CALL_HI16);
11486 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11487 PIC_CALL_REG, mips_gp_register);
11488 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11489 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11490 PIC_CALL_REG);
4d7206a2 11491 relax_switch ();
67c0d1eb
RS
11492 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11493 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11494 mips_gp_register);
11495 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11496 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11497 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11498 relax_end ();
f5040a92 11499 }
684022ea 11500
df58fc94 11501 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11502 }
11503 else
11504 {
4d7206a2 11505 relax_start (offset_expr.X_add_symbol);
90ecf173 11506 if (!mips_big_got)
438c16b8 11507 {
67c0d1eb
RS
11508 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11509 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11510 mips_gp_register);
269137b2 11511 load_delay_nop ();
4d7206a2 11512 relax_switch ();
438c16b8 11513 }
252b5132 11514 else
252b5132 11515 {
67c0d1eb
RS
11516 int gpdelay;
11517
11518 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11519 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11520 BFD_RELOC_MIPS_CALL_HI16);
11521 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11522 PIC_CALL_REG, mips_gp_register);
11523 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11524 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11525 PIC_CALL_REG);
269137b2 11526 load_delay_nop ();
4d7206a2 11527 relax_switch ();
67c0d1eb
RS
11528 if (gpdelay)
11529 macro_build (NULL, "nop", "");
252b5132 11530 }
67c0d1eb
RS
11531 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11532 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11533 mips_gp_register);
269137b2 11534 load_delay_nop ();
67c0d1eb
RS
11535 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11536 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11537 relax_end ();
df58fc94 11538 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11539
6478892d 11540 if (mips_cprestore_offset < 0)
1661c76c 11541 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11542 else
11543 {
90ecf173 11544 if (!mips_frame_reg_valid)
7a621144 11545 {
1661c76c 11546 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11547 /* Quiet this warning. */
11548 mips_frame_reg_valid = 1;
11549 }
90ecf173 11550 if (!mips_cprestore_valid)
7a621144 11551 {
1661c76c 11552 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11553 /* Quiet this warning. */
11554 mips_cprestore_valid = 1;
11555 }
6478892d 11556 if (mips_opts.noreorder)
67c0d1eb 11557 macro_build (NULL, "nop", "");
6478892d 11558 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11559 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11560 mips_gp_register,
256ab948
TS
11561 mips_frame_reg,
11562 HAVE_64BIT_ADDRESSES);
6478892d 11563 }
252b5132
RH
11564 }
11565 }
0a44bf69 11566 else if (mips_pic == VXWORKS_PIC)
1661c76c 11567 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11568 else
11569 abort ();
11570
8fc2e39e 11571 break;
252b5132 11572
7f3c4072 11573 case M_LBUE_AB:
7f3c4072
CM
11574 s = "lbue";
11575 fmt = "t,+j(b)";
11576 offbits = 9;
11577 goto ld_st;
11578 case M_LHUE_AB:
7f3c4072
CM
11579 s = "lhue";
11580 fmt = "t,+j(b)";
11581 offbits = 9;
11582 goto ld_st;
11583 case M_LBE_AB:
7f3c4072
CM
11584 s = "lbe";
11585 fmt = "t,+j(b)";
11586 offbits = 9;
11587 goto ld_st;
11588 case M_LHE_AB:
7f3c4072
CM
11589 s = "lhe";
11590 fmt = "t,+j(b)";
11591 offbits = 9;
11592 goto ld_st;
11593 case M_LLE_AB:
7f3c4072
CM
11594 s = "lle";
11595 fmt = "t,+j(b)";
11596 offbits = 9;
11597 goto ld_st;
11598 case M_LWE_AB:
7f3c4072
CM
11599 s = "lwe";
11600 fmt = "t,+j(b)";
11601 offbits = 9;
11602 goto ld_st;
11603 case M_LWLE_AB:
7f3c4072
CM
11604 s = "lwle";
11605 fmt = "t,+j(b)";
11606 offbits = 9;
11607 goto ld_st;
11608 case M_LWRE_AB:
7f3c4072
CM
11609 s = "lwre";
11610 fmt = "t,+j(b)";
11611 offbits = 9;
11612 goto ld_st;
11613 case M_SBE_AB:
7f3c4072
CM
11614 s = "sbe";
11615 fmt = "t,+j(b)";
11616 offbits = 9;
11617 goto ld_st;
11618 case M_SCE_AB:
7f3c4072
CM
11619 s = "sce";
11620 fmt = "t,+j(b)";
11621 offbits = 9;
11622 goto ld_st;
11623 case M_SHE_AB:
7f3c4072
CM
11624 s = "she";
11625 fmt = "t,+j(b)";
11626 offbits = 9;
11627 goto ld_st;
11628 case M_SWE_AB:
7f3c4072
CM
11629 s = "swe";
11630 fmt = "t,+j(b)";
11631 offbits = 9;
11632 goto ld_st;
11633 case M_SWLE_AB:
7f3c4072
CM
11634 s = "swle";
11635 fmt = "t,+j(b)";
11636 offbits = 9;
11637 goto ld_st;
11638 case M_SWRE_AB:
7f3c4072
CM
11639 s = "swre";
11640 fmt = "t,+j(b)";
11641 offbits = 9;
11642 goto ld_st;
dec0624d 11643 case M_ACLR_AB:
dec0624d 11644 s = "aclr";
dec0624d 11645 fmt = "\\,~(b)";
7f3c4072 11646 offbits = 12;
dec0624d
MR
11647 goto ld_st;
11648 case M_ASET_AB:
dec0624d 11649 s = "aset";
dec0624d 11650 fmt = "\\,~(b)";
7f3c4072 11651 offbits = 12;
dec0624d 11652 goto ld_st;
252b5132
RH
11653 case M_LB_AB:
11654 s = "lb";
df58fc94 11655 fmt = "t,o(b)";
252b5132
RH
11656 goto ld;
11657 case M_LBU_AB:
11658 s = "lbu";
df58fc94 11659 fmt = "t,o(b)";
252b5132
RH
11660 goto ld;
11661 case M_LH_AB:
11662 s = "lh";
df58fc94 11663 fmt = "t,o(b)";
252b5132
RH
11664 goto ld;
11665 case M_LHU_AB:
11666 s = "lhu";
df58fc94 11667 fmt = "t,o(b)";
252b5132
RH
11668 goto ld;
11669 case M_LW_AB:
11670 s = "lw";
df58fc94 11671 fmt = "t,o(b)";
252b5132
RH
11672 goto ld;
11673 case M_LWC0_AB:
df58fc94 11674 gas_assert (!mips_opts.micromips);
252b5132 11675 s = "lwc0";
df58fc94 11676 fmt = "E,o(b)";
bdaaa2e1 11677 /* Itbl support may require additional care here. */
252b5132 11678 coproc = 1;
df58fc94 11679 goto ld_st;
252b5132
RH
11680 case M_LWC1_AB:
11681 s = "lwc1";
df58fc94 11682 fmt = "T,o(b)";
bdaaa2e1 11683 /* Itbl support may require additional care here. */
252b5132 11684 coproc = 1;
df58fc94 11685 goto ld_st;
252b5132
RH
11686 case M_LWC2_AB:
11687 s = "lwc2";
df58fc94 11688 fmt = COP12_FMT;
7361da2c
AB
11689 offbits = (mips_opts.micromips ? 12
11690 : ISA_IS_R6 (mips_opts.isa) ? 11
11691 : 16);
bdaaa2e1 11692 /* Itbl support may require additional care here. */
252b5132 11693 coproc = 1;
df58fc94 11694 goto ld_st;
252b5132 11695 case M_LWC3_AB:
df58fc94 11696 gas_assert (!mips_opts.micromips);
252b5132 11697 s = "lwc3";
df58fc94 11698 fmt = "E,o(b)";
bdaaa2e1 11699 /* Itbl support may require additional care here. */
252b5132 11700 coproc = 1;
df58fc94 11701 goto ld_st;
252b5132
RH
11702 case M_LWL_AB:
11703 s = "lwl";
df58fc94 11704 fmt = MEM12_FMT;
7f3c4072 11705 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11706 goto ld_st;
252b5132
RH
11707 case M_LWR_AB:
11708 s = "lwr";
df58fc94 11709 fmt = MEM12_FMT;
7f3c4072 11710 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11711 goto ld_st;
252b5132 11712 case M_LDC1_AB:
252b5132 11713 s = "ldc1";
df58fc94 11714 fmt = "T,o(b)";
bdaaa2e1 11715 /* Itbl support may require additional care here. */
252b5132 11716 coproc = 1;
df58fc94 11717 goto ld_st;
252b5132
RH
11718 case M_LDC2_AB:
11719 s = "ldc2";
df58fc94 11720 fmt = COP12_FMT;
7361da2c
AB
11721 offbits = (mips_opts.micromips ? 12
11722 : ISA_IS_R6 (mips_opts.isa) ? 11
11723 : 16);
bdaaa2e1 11724 /* Itbl support may require additional care here. */
252b5132 11725 coproc = 1;
df58fc94 11726 goto ld_st;
c77c0862 11727 case M_LQC2_AB:
c77c0862 11728 s = "lqc2";
14daeee3 11729 fmt = "+7,o(b)";
c77c0862
RS
11730 /* Itbl support may require additional care here. */
11731 coproc = 1;
11732 goto ld_st;
252b5132
RH
11733 case M_LDC3_AB:
11734 s = "ldc3";
df58fc94 11735 fmt = "E,o(b)";
bdaaa2e1 11736 /* Itbl support may require additional care here. */
252b5132 11737 coproc = 1;
df58fc94 11738 goto ld_st;
252b5132
RH
11739 case M_LDL_AB:
11740 s = "ldl";
df58fc94 11741 fmt = MEM12_FMT;
7f3c4072 11742 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11743 goto ld_st;
252b5132
RH
11744 case M_LDR_AB:
11745 s = "ldr";
df58fc94 11746 fmt = MEM12_FMT;
7f3c4072 11747 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11748 goto ld_st;
252b5132
RH
11749 case M_LL_AB:
11750 s = "ll";
7361da2c
AB
11751 fmt = LL_SC_FMT;
11752 offbits = (mips_opts.micromips ? 12
11753 : ISA_IS_R6 (mips_opts.isa) ? 9
11754 : 16);
252b5132
RH
11755 goto ld;
11756 case M_LLD_AB:
11757 s = "lld";
7361da2c
AB
11758 fmt = LL_SC_FMT;
11759 offbits = (mips_opts.micromips ? 12
11760 : ISA_IS_R6 (mips_opts.isa) ? 9
11761 : 16);
252b5132
RH
11762 goto ld;
11763 case M_LWU_AB:
11764 s = "lwu";
df58fc94 11765 fmt = MEM12_FMT;
7f3c4072 11766 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11767 goto ld;
11768 case M_LWP_AB:
df58fc94
RS
11769 gas_assert (mips_opts.micromips);
11770 s = "lwp";
11771 fmt = "t,~(b)";
7f3c4072 11772 offbits = 12;
df58fc94
RS
11773 lp = 1;
11774 goto ld;
11775 case M_LDP_AB:
df58fc94
RS
11776 gas_assert (mips_opts.micromips);
11777 s = "ldp";
11778 fmt = "t,~(b)";
7f3c4072 11779 offbits = 12;
df58fc94
RS
11780 lp = 1;
11781 goto ld;
11782 case M_LWM_AB:
df58fc94
RS
11783 gas_assert (mips_opts.micromips);
11784 s = "lwm";
11785 fmt = "n,~(b)";
7f3c4072 11786 offbits = 12;
df58fc94
RS
11787 goto ld_st;
11788 case M_LDM_AB:
df58fc94
RS
11789 gas_assert (mips_opts.micromips);
11790 s = "ldm";
11791 fmt = "n,~(b)";
7f3c4072 11792 offbits = 12;
df58fc94
RS
11793 goto ld_st;
11794
252b5132 11795 ld:
f19ccbda 11796 /* We don't want to use $0 as tempreg. */
c0ebe874 11797 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11798 goto ld_st;
252b5132 11799 else
c0ebe874 11800 tempreg = op[0] + lp;
df58fc94
RS
11801 goto ld_noat;
11802
252b5132
RH
11803 case M_SB_AB:
11804 s = "sb";
df58fc94
RS
11805 fmt = "t,o(b)";
11806 goto ld_st;
252b5132
RH
11807 case M_SH_AB:
11808 s = "sh";
df58fc94
RS
11809 fmt = "t,o(b)";
11810 goto ld_st;
252b5132
RH
11811 case M_SW_AB:
11812 s = "sw";
df58fc94
RS
11813 fmt = "t,o(b)";
11814 goto ld_st;
252b5132 11815 case M_SWC0_AB:
df58fc94 11816 gas_assert (!mips_opts.micromips);
252b5132 11817 s = "swc0";
df58fc94 11818 fmt = "E,o(b)";
bdaaa2e1 11819 /* Itbl support may require additional care here. */
252b5132 11820 coproc = 1;
df58fc94 11821 goto ld_st;
252b5132
RH
11822 case M_SWC1_AB:
11823 s = "swc1";
df58fc94 11824 fmt = "T,o(b)";
bdaaa2e1 11825 /* Itbl support may require additional care here. */
252b5132 11826 coproc = 1;
df58fc94 11827 goto ld_st;
252b5132
RH
11828 case M_SWC2_AB:
11829 s = "swc2";
df58fc94 11830 fmt = COP12_FMT;
7361da2c
AB
11831 offbits = (mips_opts.micromips ? 12
11832 : ISA_IS_R6 (mips_opts.isa) ? 11
11833 : 16);
bdaaa2e1 11834 /* Itbl support may require additional care here. */
252b5132 11835 coproc = 1;
df58fc94 11836 goto ld_st;
252b5132 11837 case M_SWC3_AB:
df58fc94 11838 gas_assert (!mips_opts.micromips);
252b5132 11839 s = "swc3";
df58fc94 11840 fmt = "E,o(b)";
bdaaa2e1 11841 /* Itbl support may require additional care here. */
252b5132 11842 coproc = 1;
df58fc94 11843 goto ld_st;
252b5132
RH
11844 case M_SWL_AB:
11845 s = "swl";
df58fc94 11846 fmt = MEM12_FMT;
7f3c4072 11847 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11848 goto ld_st;
252b5132
RH
11849 case M_SWR_AB:
11850 s = "swr";
df58fc94 11851 fmt = MEM12_FMT;
7f3c4072 11852 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11853 goto ld_st;
252b5132
RH
11854 case M_SC_AB:
11855 s = "sc";
7361da2c
AB
11856 fmt = LL_SC_FMT;
11857 offbits = (mips_opts.micromips ? 12
11858 : ISA_IS_R6 (mips_opts.isa) ? 9
11859 : 16);
df58fc94 11860 goto ld_st;
252b5132
RH
11861 case M_SCD_AB:
11862 s = "scd";
7361da2c
AB
11863 fmt = LL_SC_FMT;
11864 offbits = (mips_opts.micromips ? 12
11865 : ISA_IS_R6 (mips_opts.isa) ? 9
11866 : 16);
df58fc94 11867 goto ld_st;
d43b4baf
TS
11868 case M_CACHE_AB:
11869 s = "cache";
7361da2c
AB
11870 fmt = (mips_opts.micromips ? "k,~(b)"
11871 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11872 : "k,o(b)");
11873 offbits = (mips_opts.micromips ? 12
11874 : ISA_IS_R6 (mips_opts.isa) ? 9
11875 : 16);
7f3c4072
CM
11876 goto ld_st;
11877 case M_CACHEE_AB:
7f3c4072
CM
11878 s = "cachee";
11879 fmt = "k,+j(b)";
11880 offbits = 9;
df58fc94 11881 goto ld_st;
3eebd5eb
MR
11882 case M_PREF_AB:
11883 s = "pref";
7361da2c
AB
11884 fmt = (mips_opts.micromips ? "k,~(b)"
11885 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11886 : "k,o(b)");
11887 offbits = (mips_opts.micromips ? 12
11888 : ISA_IS_R6 (mips_opts.isa) ? 9
11889 : 16);
7f3c4072
CM
11890 goto ld_st;
11891 case M_PREFE_AB:
7f3c4072
CM
11892 s = "prefe";
11893 fmt = "k,+j(b)";
11894 offbits = 9;
df58fc94 11895 goto ld_st;
252b5132 11896 case M_SDC1_AB:
252b5132 11897 s = "sdc1";
df58fc94 11898 fmt = "T,o(b)";
252b5132 11899 coproc = 1;
bdaaa2e1 11900 /* Itbl support may require additional care here. */
df58fc94 11901 goto ld_st;
252b5132
RH
11902 case M_SDC2_AB:
11903 s = "sdc2";
df58fc94 11904 fmt = COP12_FMT;
7361da2c
AB
11905 offbits = (mips_opts.micromips ? 12
11906 : ISA_IS_R6 (mips_opts.isa) ? 11
11907 : 16);
c77c0862
RS
11908 /* Itbl support may require additional care here. */
11909 coproc = 1;
11910 goto ld_st;
11911 case M_SQC2_AB:
c77c0862 11912 s = "sqc2";
14daeee3 11913 fmt = "+7,o(b)";
bdaaa2e1 11914 /* Itbl support may require additional care here. */
252b5132 11915 coproc = 1;
df58fc94 11916 goto ld_st;
252b5132 11917 case M_SDC3_AB:
df58fc94 11918 gas_assert (!mips_opts.micromips);
252b5132 11919 s = "sdc3";
df58fc94 11920 fmt = "E,o(b)";
bdaaa2e1 11921 /* Itbl support may require additional care here. */
252b5132 11922 coproc = 1;
df58fc94 11923 goto ld_st;
252b5132
RH
11924 case M_SDL_AB:
11925 s = "sdl";
df58fc94 11926 fmt = MEM12_FMT;
7f3c4072 11927 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11928 goto ld_st;
252b5132
RH
11929 case M_SDR_AB:
11930 s = "sdr";
df58fc94 11931 fmt = MEM12_FMT;
7f3c4072 11932 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11933 goto ld_st;
11934 case M_SWP_AB:
df58fc94
RS
11935 gas_assert (mips_opts.micromips);
11936 s = "swp";
11937 fmt = "t,~(b)";
7f3c4072 11938 offbits = 12;
df58fc94
RS
11939 goto ld_st;
11940 case M_SDP_AB:
df58fc94
RS
11941 gas_assert (mips_opts.micromips);
11942 s = "sdp";
11943 fmt = "t,~(b)";
7f3c4072 11944 offbits = 12;
df58fc94
RS
11945 goto ld_st;
11946 case M_SWM_AB:
df58fc94
RS
11947 gas_assert (mips_opts.micromips);
11948 s = "swm";
11949 fmt = "n,~(b)";
7f3c4072 11950 offbits = 12;
df58fc94
RS
11951 goto ld_st;
11952 case M_SDM_AB:
df58fc94
RS
11953 gas_assert (mips_opts.micromips);
11954 s = "sdm";
11955 fmt = "n,~(b)";
7f3c4072 11956 offbits = 12;
df58fc94
RS
11957
11958 ld_st:
8fc2e39e 11959 tempreg = AT;
df58fc94 11960 ld_noat:
c0ebe874 11961 breg = op[2];
f2ae14a1
RS
11962 if (small_offset_p (0, align, 16))
11963 {
11964 /* The first case exists for M_LD_AB and M_SD_AB, which are
11965 macros for o32 but which should act like normal instructions
11966 otherwise. */
11967 if (offbits == 16)
c0ebe874 11968 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11969 offset_reloc[1], offset_reloc[2], breg);
11970 else if (small_offset_p (0, align, offbits))
11971 {
11972 if (offbits == 0)
c0ebe874 11973 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11974 else
c0ebe874 11975 macro_build (NULL, s, fmt, op[0],
c8276761 11976 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11977 }
11978 else
11979 {
11980 if (tempreg == AT)
11981 used_at = 1;
11982 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11983 tempreg, breg, -1, offset_reloc[0],
11984 offset_reloc[1], offset_reloc[2]);
11985 if (offbits == 0)
c0ebe874 11986 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11987 else
c0ebe874 11988 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11989 }
11990 break;
11991 }
11992
11993 if (tempreg == AT)
11994 used_at = 1;
11995
252b5132
RH
11996 if (offset_expr.X_op != O_constant
11997 && offset_expr.X_op != O_symbol)
11998 {
1661c76c 11999 as_bad (_("expression too complex"));
252b5132
RH
12000 offset_expr.X_op = O_constant;
12001 }
12002
2051e8c4
MR
12003 if (HAVE_32BIT_ADDRESSES
12004 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12005 {
12006 char value [32];
12007
12008 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12009 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12010 }
2051e8c4 12011
252b5132
RH
12012 /* A constant expression in PIC code can be handled just as it
12013 is in non PIC code. */
aed1a261
RS
12014 if (offset_expr.X_op == O_constant)
12015 {
f2ae14a1
RS
12016 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12017 offbits == 0 ? 16 : offbits);
12018 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12019
f2ae14a1
RS
12020 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12021 if (breg != 0)
12022 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12023 tempreg, tempreg, breg);
7f3c4072 12024 if (offbits == 0)
dd6a37e7 12025 {
f2ae14a1 12026 if (offset_expr.X_add_number != 0)
dd6a37e7 12027 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12028 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12029 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12030 }
7f3c4072 12031 else if (offbits == 16)
c0ebe874 12032 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12033 else
c0ebe874 12034 macro_build (NULL, s, fmt, op[0],
c8276761 12035 (int) offset_expr.X_add_number, tempreg);
df58fc94 12036 }
7f3c4072 12037 else if (offbits != 16)
df58fc94 12038 {
7f3c4072 12039 /* The offset field is too narrow to be used for a low-part
2b0f3761 12040 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12041 register. */
12042 load_address (tempreg, &offset_expr, &used_at);
12043 if (breg != 0)
12044 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12045 tempreg, tempreg, breg);
7f3c4072 12046 if (offbits == 0)
c0ebe874 12047 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12048 else
c0ebe874 12049 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12050 }
12051 else if (mips_pic == NO_PIC)
252b5132
RH
12052 {
12053 /* If this is a reference to a GP relative symbol, and there
12054 is no base register, we want
c0ebe874 12055 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12056 Otherwise, if there is no base register, we want
12057 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12058 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12059 If we have a constant, we need two instructions anyhow,
12060 so we always use the latter form.
12061
12062 If we have a base register, and this is a reference to a
12063 GP relative symbol, we want
12064 addu $tempreg,$breg,$gp
c0ebe874 12065 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12066 Otherwise we want
12067 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12068 addu $tempreg,$tempreg,$breg
c0ebe874 12069 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12070 With a constant we always use the latter case.
76b3015f 12071
d6bc6245
TS
12072 With 64bit address space and no base register and $at usable,
12073 we want
12074 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12075 lui $at,<sym> (BFD_RELOC_HI16_S)
12076 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12077 dsll32 $tempreg,0
12078 daddu $tempreg,$at
c0ebe874 12079 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12080 If we have a base register, we want
12081 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12082 lui $at,<sym> (BFD_RELOC_HI16_S)
12083 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12084 daddu $at,$breg
12085 dsll32 $tempreg,0
12086 daddu $tempreg,$at
c0ebe874 12087 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12088
12089 Without $at we can't generate the optimal path for superscalar
12090 processors here since this would require two temporary registers.
12091 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12092 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12093 dsll $tempreg,16
12094 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12095 dsll $tempreg,16
c0ebe874 12096 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12097 If we have a base register, we want
12098 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12099 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12100 dsll $tempreg,16
12101 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12102 dsll $tempreg,16
12103 daddu $tempreg,$tempreg,$breg
c0ebe874 12104 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12105
6caf9ef4 12106 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12107 the same sequence as in 32bit address space. */
12108 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12109 {
aed1a261 12110 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12111 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12112 {
12113 relax_start (offset_expr.X_add_symbol);
12114 if (breg == 0)
12115 {
c0ebe874 12116 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12117 BFD_RELOC_GPREL16, mips_gp_register);
12118 }
12119 else
12120 {
12121 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12122 tempreg, breg, mips_gp_register);
c0ebe874 12123 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12124 BFD_RELOC_GPREL16, tempreg);
12125 }
12126 relax_switch ();
12127 }
d6bc6245 12128
741fe287 12129 if (used_at == 0 && mips_opts.at)
d6bc6245 12130 {
df58fc94 12131 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12132 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12133 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12134 BFD_RELOC_HI16_S);
12135 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12136 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12137 if (breg != 0)
67c0d1eb 12138 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12139 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12140 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12141 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12142 tempreg);
d6bc6245
TS
12143 used_at = 1;
12144 }
12145 else
12146 {
df58fc94 12147 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12148 BFD_RELOC_MIPS_HIGHEST);
12149 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12150 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12151 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12152 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12153 tempreg, BFD_RELOC_HI16_S);
df58fc94 12154 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12155 if (breg != 0)
67c0d1eb 12156 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12157 tempreg, tempreg, breg);
c0ebe874 12158 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12159 BFD_RELOC_LO16, tempreg);
d6bc6245 12160 }
6caf9ef4
TS
12161
12162 if (mips_relax.sequence)
12163 relax_end ();
8fc2e39e 12164 break;
d6bc6245 12165 }
256ab948 12166
252b5132
RH
12167 if (breg == 0)
12168 {
67c0d1eb 12169 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12170 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12171 {
4d7206a2 12172 relax_start (offset_expr.X_add_symbol);
c0ebe874 12173 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12174 mips_gp_register);
4d7206a2 12175 relax_switch ();
252b5132 12176 }
67c0d1eb 12177 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12178 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12179 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12180 if (mips_relax.sequence)
12181 relax_end ();
252b5132
RH
12182 }
12183 else
12184 {
67c0d1eb 12185 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12186 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12187 {
4d7206a2 12188 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12189 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12190 tempreg, breg, mips_gp_register);
c0ebe874 12191 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12192 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12193 relax_switch ();
252b5132 12194 }
67c0d1eb
RS
12195 macro_build_lui (&offset_expr, tempreg);
12196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12197 tempreg, tempreg, breg);
c0ebe874 12198 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12199 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12200 if (mips_relax.sequence)
12201 relax_end ();
252b5132
RH
12202 }
12203 }
0a44bf69 12204 else if (!mips_big_got)
252b5132 12205 {
ed6fb7bd 12206 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12207
252b5132
RH
12208 /* If this is a reference to an external symbol, we want
12209 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12210 nop
c0ebe874 12211 <op> op[0],0($tempreg)
252b5132
RH
12212 Otherwise we want
12213 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12214 nop
12215 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12216 <op> op[0],0($tempreg)
f5040a92
AO
12217
12218 For NewABI, we want
12219 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12220 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12221
252b5132
RH
12222 If there is a base register, we add it to $tempreg before
12223 the <op>. If there is a constant, we stick it in the
12224 <op> instruction. We don't handle constants larger than
12225 16 bits, because we have no way to load the upper 16 bits
12226 (actually, we could handle them for the subset of cases
12227 in which we are not using $at). */
9c2799c2 12228 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12229 if (HAVE_NEWABI)
12230 {
67c0d1eb
RS
12231 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12232 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12233 if (breg != 0)
67c0d1eb 12234 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12235 tempreg, tempreg, breg);
c0ebe874 12236 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12237 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12238 break;
12239 }
252b5132
RH
12240 expr1.X_add_number = offset_expr.X_add_number;
12241 offset_expr.X_add_number = 0;
12242 if (expr1.X_add_number < -0x8000
12243 || expr1.X_add_number >= 0x8000)
12244 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12245 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12246 lw_reloc_type, mips_gp_register);
269137b2 12247 load_delay_nop ();
4d7206a2
RS
12248 relax_start (offset_expr.X_add_symbol);
12249 relax_switch ();
67c0d1eb
RS
12250 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12251 tempreg, BFD_RELOC_LO16);
4d7206a2 12252 relax_end ();
252b5132 12253 if (breg != 0)
67c0d1eb 12254 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12255 tempreg, tempreg, breg);
c0ebe874 12256 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12257 }
0a44bf69 12258 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12259 {
67c0d1eb 12260 int gpdelay;
252b5132
RH
12261
12262 /* If this is a reference to an external symbol, we want
12263 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12264 addu $tempreg,$tempreg,$gp
12265 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12266 <op> op[0],0($tempreg)
252b5132
RH
12267 Otherwise we want
12268 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12269 nop
12270 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12271 <op> op[0],0($tempreg)
252b5132
RH
12272 If there is a base register, we add it to $tempreg before
12273 the <op>. If there is a constant, we stick it in the
12274 <op> instruction. We don't handle constants larger than
12275 16 bits, because we have no way to load the upper 16 bits
12276 (actually, we could handle them for the subset of cases
f5040a92 12277 in which we are not using $at). */
9c2799c2 12278 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12279 expr1.X_add_number = offset_expr.X_add_number;
12280 offset_expr.X_add_number = 0;
12281 if (expr1.X_add_number < -0x8000
12282 || expr1.X_add_number >= 0x8000)
12283 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12284 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12285 relax_start (offset_expr.X_add_symbol);
df58fc94 12286 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12287 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12288 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12289 mips_gp_register);
12290 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12291 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12292 relax_switch ();
67c0d1eb
RS
12293 if (gpdelay)
12294 macro_build (NULL, "nop", "");
12295 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12296 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12297 load_delay_nop ();
67c0d1eb
RS
12298 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12299 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12300 relax_end ();
12301
252b5132 12302 if (breg != 0)
67c0d1eb 12303 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12304 tempreg, tempreg, breg);
c0ebe874 12305 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12306 }
0a44bf69 12307 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12308 {
f5040a92
AO
12309 /* If this is a reference to an external symbol, we want
12310 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12311 add $tempreg,$tempreg,$gp
12312 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12313 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12314 Otherwise, for local symbols, we want:
12315 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12316 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12317 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12318 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12319 offset_expr.X_add_number = 0;
12320 if (expr1.X_add_number < -0x8000
12321 || expr1.X_add_number >= 0x8000)
12322 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12323 relax_start (offset_expr.X_add_symbol);
df58fc94 12324 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12325 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12326 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12327 mips_gp_register);
12328 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12329 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12330 if (breg != 0)
67c0d1eb 12331 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12332 tempreg, tempreg, breg);
c0ebe874 12333 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12334
4d7206a2 12335 relax_switch ();
f5040a92 12336 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12337 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12338 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12339 if (breg != 0)
67c0d1eb 12340 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12341 tempreg, tempreg, breg);
c0ebe874 12342 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12343 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12344 relax_end ();
f5040a92 12345 }
252b5132
RH
12346 else
12347 abort ();
12348
252b5132
RH
12349 break;
12350
833794fc
MR
12351 case M_JRADDIUSP:
12352 gas_assert (mips_opts.micromips);
12353 gas_assert (mips_opts.insn32);
12354 start_noreorder ();
12355 macro_build (NULL, "jr", "s", RA);
c0ebe874 12356 expr1.X_add_number = op[0] << 2;
833794fc
MR
12357 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12358 end_noreorder ();
12359 break;
12360
12361 case M_JRC:
12362 gas_assert (mips_opts.micromips);
12363 gas_assert (mips_opts.insn32);
c0ebe874 12364 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12365 if (mips_opts.noreorder)
12366 macro_build (NULL, "nop", "");
12367 break;
12368
252b5132
RH
12369 case M_LI:
12370 case M_LI_S:
c0ebe874 12371 load_register (op[0], &imm_expr, 0);
8fc2e39e 12372 break;
252b5132
RH
12373
12374 case M_DLI:
c0ebe874 12375 load_register (op[0], &imm_expr, 1);
8fc2e39e 12376 break;
252b5132
RH
12377
12378 case M_LI_SS:
12379 if (imm_expr.X_op == O_constant)
12380 {
8fc2e39e 12381 used_at = 1;
67c0d1eb 12382 load_register (AT, &imm_expr, 0);
c0ebe874 12383 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12384 break;
12385 }
12386 else
12387 {
b0e6f033
RS
12388 gas_assert (imm_expr.X_op == O_absent
12389 && offset_expr.X_op == O_symbol
90ecf173
MR
12390 && strcmp (segment_name (S_GET_SEGMENT
12391 (offset_expr.X_add_symbol)),
12392 ".lit4") == 0
12393 && offset_expr.X_add_number == 0);
c0ebe874 12394 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12395 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12396 break;
252b5132
RH
12397 }
12398
12399 case M_LI_D:
ca4e0257
RS
12400 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12401 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12402 order 32 bits of the value and the low order 32 bits are either
12403 zero or in OFFSET_EXPR. */
b0e6f033 12404 if (imm_expr.X_op == O_constant)
252b5132 12405 {
bad1aba3 12406 if (GPR_SIZE == 64)
c0ebe874 12407 load_register (op[0], &imm_expr, 1);
252b5132
RH
12408 else
12409 {
12410 int hreg, lreg;
12411
12412 if (target_big_endian)
12413 {
c0ebe874
RS
12414 hreg = op[0];
12415 lreg = op[0] + 1;
252b5132
RH
12416 }
12417 else
12418 {
c0ebe874
RS
12419 hreg = op[0] + 1;
12420 lreg = op[0];
252b5132
RH
12421 }
12422
12423 if (hreg <= 31)
67c0d1eb 12424 load_register (hreg, &imm_expr, 0);
252b5132
RH
12425 if (lreg <= 31)
12426 {
12427 if (offset_expr.X_op == O_absent)
67c0d1eb 12428 move_register (lreg, 0);
252b5132
RH
12429 else
12430 {
9c2799c2 12431 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12432 load_register (lreg, &offset_expr, 0);
252b5132
RH
12433 }
12434 }
12435 }
8fc2e39e 12436 break;
252b5132 12437 }
b0e6f033 12438 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12439
12440 /* We know that sym is in the .rdata section. First we get the
12441 upper 16 bits of the address. */
12442 if (mips_pic == NO_PIC)
12443 {
67c0d1eb 12444 macro_build_lui (&offset_expr, AT);
8fc2e39e 12445 used_at = 1;
252b5132 12446 }
0a44bf69 12447 else
252b5132 12448 {
67c0d1eb
RS
12449 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12450 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12451 used_at = 1;
252b5132 12452 }
bdaaa2e1 12453
252b5132 12454 /* Now we load the register(s). */
bad1aba3 12455 if (GPR_SIZE == 64)
8fc2e39e
TS
12456 {
12457 used_at = 1;
c0ebe874
RS
12458 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12459 BFD_RELOC_LO16, AT);
8fc2e39e 12460 }
252b5132
RH
12461 else
12462 {
8fc2e39e 12463 used_at = 1;
c0ebe874
RS
12464 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12465 BFD_RELOC_LO16, AT);
12466 if (op[0] != RA)
252b5132
RH
12467 {
12468 /* FIXME: How in the world do we deal with the possible
12469 overflow here? */
12470 offset_expr.X_add_number += 4;
67c0d1eb 12471 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12472 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12473 }
12474 }
252b5132
RH
12475 break;
12476
12477 case M_LI_DD:
ca4e0257
RS
12478 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12479 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12480 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12481 the value and the low order 32 bits are either zero or in
12482 OFFSET_EXPR. */
b0e6f033 12483 if (imm_expr.X_op == O_constant)
252b5132 12484 {
8fc2e39e 12485 used_at = 1;
bad1aba3 12486 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12487 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12488 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12489 else
12490 {
351cdf24
MF
12491 if (ISA_HAS_MXHC1 (mips_opts.isa))
12492 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12493 else if (FPR_SIZE != 32)
12494 as_bad (_("Unable to generate `%s' compliant code "
12495 "without mthc1"),
12496 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12497 else
12498 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12499 if (offset_expr.X_op == O_absent)
c0ebe874 12500 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12501 else
12502 {
9c2799c2 12503 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12504 load_register (AT, &offset_expr, 0);
c0ebe874 12505 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12506 }
12507 }
12508 break;
12509 }
12510
b0e6f033
RS
12511 gas_assert (imm_expr.X_op == O_absent
12512 && offset_expr.X_op == O_symbol
90ecf173 12513 && offset_expr.X_add_number == 0);
252b5132
RH
12514 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12515 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12516 {
12517 op[2] = mips_gp_register;
f2ae14a1
RS
12518 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12519 offset_reloc[1] = BFD_RELOC_UNUSED;
12520 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12521 }
12522 else
12523 {
9c2799c2 12524 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12525 used_at = 1;
0a44bf69 12526 if (mips_pic != NO_PIC)
67c0d1eb
RS
12527 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12528 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12529 else
12530 {
12531 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12532 macro_build_lui (&offset_expr, AT);
252b5132 12533 }
bdaaa2e1 12534
c0ebe874 12535 op[2] = AT;
f2ae14a1
RS
12536 offset_reloc[0] = BFD_RELOC_LO16;
12537 offset_reloc[1] = BFD_RELOC_UNUSED;
12538 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12539 }
f2ae14a1
RS
12540 align = 8;
12541 /* Fall through */
c4a68bea 12542
252b5132
RH
12543 case M_L_DAB:
12544 /*
12545 * The MIPS assembler seems to check for X_add_number not
12546 * being double aligned and generating:
12547 * lui at,%hi(foo+1)
12548 * addu at,at,v1
12549 * addiu at,at,%lo(foo+1)
12550 * lwc1 f2,0(at)
12551 * lwc1 f3,4(at)
12552 * But, the resulting address is the same after relocation so why
12553 * generate the extra instruction?
12554 */
bdaaa2e1 12555 /* Itbl support may require additional care here. */
252b5132 12556 coproc = 1;
df58fc94 12557 fmt = "T,o(b)";
0aa27725 12558 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12559 {
12560 s = "ldc1";
df58fc94 12561 goto ld_st;
252b5132 12562 }
252b5132 12563 s = "lwc1";
252b5132
RH
12564 goto ldd_std;
12565
12566 case M_S_DAB:
df58fc94
RS
12567 gas_assert (!mips_opts.micromips);
12568 /* Itbl support may require additional care here. */
12569 coproc = 1;
12570 fmt = "T,o(b)";
0aa27725 12571 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12572 {
12573 s = "sdc1";
df58fc94 12574 goto ld_st;
252b5132 12575 }
252b5132 12576 s = "swc1";
252b5132
RH
12577 goto ldd_std;
12578
e407c74b
NC
12579 case M_LQ_AB:
12580 fmt = "t,o(b)";
12581 s = "lq";
12582 goto ld;
12583
12584 case M_SQ_AB:
12585 fmt = "t,o(b)";
12586 s = "sq";
12587 goto ld_st;
12588
252b5132 12589 case M_LD_AB:
df58fc94 12590 fmt = "t,o(b)";
bad1aba3 12591 if (GPR_SIZE == 64)
252b5132
RH
12592 {
12593 s = "ld";
12594 goto ld;
12595 }
252b5132 12596 s = "lw";
252b5132
RH
12597 goto ldd_std;
12598
12599 case M_SD_AB:
df58fc94 12600 fmt = "t,o(b)";
bad1aba3 12601 if (GPR_SIZE == 64)
252b5132
RH
12602 {
12603 s = "sd";
df58fc94 12604 goto ld_st;
252b5132 12605 }
252b5132 12606 s = "sw";
252b5132
RH
12607
12608 ldd_std:
f2ae14a1
RS
12609 /* Even on a big endian machine $fn comes before $fn+1. We have
12610 to adjust when loading from memory. We set coproc if we must
12611 load $fn+1 first. */
12612 /* Itbl support may require additional care here. */
12613 if (!target_big_endian)
12614 coproc = 0;
12615
c0ebe874 12616 breg = op[2];
f2ae14a1
RS
12617 if (small_offset_p (0, align, 16))
12618 {
12619 ep = &offset_expr;
12620 if (!small_offset_p (4, align, 16))
12621 {
12622 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12623 -1, offset_reloc[0], offset_reloc[1],
12624 offset_reloc[2]);
12625 expr1.X_add_number = 0;
12626 ep = &expr1;
12627 breg = AT;
12628 used_at = 1;
12629 offset_reloc[0] = BFD_RELOC_LO16;
12630 offset_reloc[1] = BFD_RELOC_UNUSED;
12631 offset_reloc[2] = BFD_RELOC_UNUSED;
12632 }
c0ebe874 12633 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12634 {
12635 ep->X_add_number += 4;
c0ebe874 12636 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12637 offset_reloc[1], offset_reloc[2], breg);
12638 ep->X_add_number -= 4;
c0ebe874 12639 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12640 offset_reloc[1], offset_reloc[2], breg);
12641 }
12642 else
12643 {
c0ebe874 12644 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12645 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12646 breg);
12647 ep->X_add_number += 4;
c0ebe874 12648 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12649 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12650 breg);
12651 }
12652 break;
12653 }
12654
252b5132
RH
12655 if (offset_expr.X_op != O_symbol
12656 && offset_expr.X_op != O_constant)
12657 {
1661c76c 12658 as_bad (_("expression too complex"));
252b5132
RH
12659 offset_expr.X_op = O_constant;
12660 }
12661
2051e8c4
MR
12662 if (HAVE_32BIT_ADDRESSES
12663 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12664 {
12665 char value [32];
12666
12667 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12668 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12669 }
2051e8c4 12670
90ecf173 12671 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12672 {
12673 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12674 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12675 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12676 If we have a base register, we use this
12677 addu $at,$breg,$gp
c0ebe874
RS
12678 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12679 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12680 If this is not a GP relative symbol, we want
12681 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12682 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12683 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12684 If there is a base register, we add it to $at after the
12685 lui instruction. If there is a constant, we always use
12686 the last case. */
39a59cf8
MR
12687 if (offset_expr.X_op == O_symbol
12688 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12689 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12690 {
4d7206a2 12691 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12692 if (breg == 0)
12693 {
c9914766 12694 tempreg = mips_gp_register;
252b5132
RH
12695 }
12696 else
12697 {
67c0d1eb 12698 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12699 AT, breg, mips_gp_register);
252b5132 12700 tempreg = AT;
252b5132
RH
12701 used_at = 1;
12702 }
12703
beae10d5 12704 /* Itbl support may require additional care here. */
c0ebe874 12705 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12706 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12707 offset_expr.X_add_number += 4;
12708
12709 /* Set mips_optimize to 2 to avoid inserting an
12710 undesired nop. */
12711 hold_mips_optimize = mips_optimize;
12712 mips_optimize = 2;
beae10d5 12713 /* Itbl support may require additional care here. */
c0ebe874 12714 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12715 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12716 mips_optimize = hold_mips_optimize;
12717
4d7206a2 12718 relax_switch ();
252b5132 12719
0970e49e 12720 offset_expr.X_add_number -= 4;
252b5132 12721 }
8fc2e39e 12722 used_at = 1;
f2ae14a1
RS
12723 if (offset_high_part (offset_expr.X_add_number, 16)
12724 != offset_high_part (offset_expr.X_add_number + 4, 16))
12725 {
12726 load_address (AT, &offset_expr, &used_at);
12727 offset_expr.X_op = O_constant;
12728 offset_expr.X_add_number = 0;
12729 }
12730 else
12731 macro_build_lui (&offset_expr, AT);
252b5132 12732 if (breg != 0)
67c0d1eb 12733 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12734 /* Itbl support may require additional care here. */
c0ebe874 12735 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12736 BFD_RELOC_LO16, AT);
252b5132
RH
12737 /* FIXME: How do we handle overflow here? */
12738 offset_expr.X_add_number += 4;
beae10d5 12739 /* Itbl support may require additional care here. */
c0ebe874 12740 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12741 BFD_RELOC_LO16, AT);
4d7206a2
RS
12742 if (mips_relax.sequence)
12743 relax_end ();
bdaaa2e1 12744 }
0a44bf69 12745 else if (!mips_big_got)
252b5132 12746 {
252b5132
RH
12747 /* If this is a reference to an external symbol, we want
12748 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12749 nop
c0ebe874
RS
12750 <op> op[0],0($at)
12751 <op> op[0]+1,4($at)
252b5132
RH
12752 Otherwise we want
12753 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12754 nop
c0ebe874
RS
12755 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12756 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12757 If there is a base register we add it to $at before the
12758 lwc1 instructions. If there is a constant we include it
12759 in the lwc1 instructions. */
12760 used_at = 1;
12761 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12762 if (expr1.X_add_number < -0x8000
12763 || expr1.X_add_number >= 0x8000 - 4)
12764 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12765 load_got_offset (AT, &offset_expr);
269137b2 12766 load_delay_nop ();
252b5132 12767 if (breg != 0)
67c0d1eb 12768 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12769
12770 /* Set mips_optimize to 2 to avoid inserting an undesired
12771 nop. */
12772 hold_mips_optimize = mips_optimize;
12773 mips_optimize = 2;
4d7206a2 12774
beae10d5 12775 /* Itbl support may require additional care here. */
4d7206a2 12776 relax_start (offset_expr.X_add_symbol);
c0ebe874 12777 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12778 BFD_RELOC_LO16, AT);
4d7206a2 12779 expr1.X_add_number += 4;
c0ebe874 12780 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12781 BFD_RELOC_LO16, AT);
4d7206a2 12782 relax_switch ();
c0ebe874 12783 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12784 BFD_RELOC_LO16, AT);
4d7206a2 12785 offset_expr.X_add_number += 4;
c0ebe874 12786 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12787 BFD_RELOC_LO16, AT);
4d7206a2 12788 relax_end ();
252b5132 12789
4d7206a2 12790 mips_optimize = hold_mips_optimize;
252b5132 12791 }
0a44bf69 12792 else if (mips_big_got)
252b5132 12793 {
67c0d1eb 12794 int gpdelay;
252b5132
RH
12795
12796 /* If this is a reference to an external symbol, we want
12797 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12798 addu $at,$at,$gp
12799 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12800 nop
c0ebe874
RS
12801 <op> op[0],0($at)
12802 <op> op[0]+1,4($at)
252b5132
RH
12803 Otherwise we want
12804 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12805 nop
c0ebe874
RS
12806 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12807 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12808 If there is a base register we add it to $at before the
12809 lwc1 instructions. If there is a constant we include it
12810 in the lwc1 instructions. */
12811 used_at = 1;
12812 expr1.X_add_number = offset_expr.X_add_number;
12813 offset_expr.X_add_number = 0;
12814 if (expr1.X_add_number < -0x8000
12815 || expr1.X_add_number >= 0x8000 - 4)
12816 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12817 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12818 relax_start (offset_expr.X_add_symbol);
df58fc94 12819 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12820 AT, BFD_RELOC_MIPS_GOT_HI16);
12821 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12822 AT, AT, mips_gp_register);
67c0d1eb 12823 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12824 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12825 load_delay_nop ();
252b5132 12826 if (breg != 0)
67c0d1eb 12827 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12828 /* Itbl support may require additional care here. */
c0ebe874 12829 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12830 BFD_RELOC_LO16, AT);
252b5132
RH
12831 expr1.X_add_number += 4;
12832
12833 /* Set mips_optimize to 2 to avoid inserting an undesired
12834 nop. */
12835 hold_mips_optimize = mips_optimize;
12836 mips_optimize = 2;
beae10d5 12837 /* Itbl support may require additional care here. */
c0ebe874 12838 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12839 BFD_RELOC_LO16, AT);
252b5132
RH
12840 mips_optimize = hold_mips_optimize;
12841 expr1.X_add_number -= 4;
12842
4d7206a2
RS
12843 relax_switch ();
12844 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12845 if (gpdelay)
12846 macro_build (NULL, "nop", "");
12847 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12848 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12849 load_delay_nop ();
252b5132 12850 if (breg != 0)
67c0d1eb 12851 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12852 /* Itbl support may require additional care here. */
c0ebe874 12853 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12854 BFD_RELOC_LO16, AT);
4d7206a2 12855 offset_expr.X_add_number += 4;
252b5132
RH
12856
12857 /* Set mips_optimize to 2 to avoid inserting an undesired
12858 nop. */
12859 hold_mips_optimize = mips_optimize;
12860 mips_optimize = 2;
beae10d5 12861 /* Itbl support may require additional care here. */
c0ebe874 12862 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12863 BFD_RELOC_LO16, AT);
252b5132 12864 mips_optimize = hold_mips_optimize;
4d7206a2 12865 relax_end ();
252b5132 12866 }
252b5132
RH
12867 else
12868 abort ();
12869
252b5132 12870 break;
3739860c 12871
dd6a37e7 12872 case M_SAA_AB:
dd6a37e7 12873 s = "saa";
0db377d0 12874 goto saa_saad;
dd6a37e7 12875 case M_SAAD_AB:
dd6a37e7 12876 s = "saad";
0db377d0
MR
12877 saa_saad:
12878 gas_assert (!mips_opts.micromips);
7f3c4072 12879 offbits = 0;
dd6a37e7
AP
12880 fmt = "t,(b)";
12881 goto ld_st;
12882
252b5132
RH
12883 /* New code added to support COPZ instructions.
12884 This code builds table entries out of the macros in mip_opcodes.
12885 R4000 uses interlocks to handle coproc delays.
12886 Other chips (like the R3000) require nops to be inserted for delays.
12887
f72c8c98 12888 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12889 In order to fill delay slots for non-interlocked chips,
12890 we must have a way to specify delays based on the coprocessor.
12891 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12892 What are the side-effects of the cop instruction?
12893 What cache support might we have and what are its effects?
12894 Both coprocessor & memory require delays. how long???
bdaaa2e1 12895 What registers are read/set/modified?
252b5132
RH
12896
12897 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12898 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12899
12900 case M_COP0:
12901 s = "c0";
12902 goto copz;
12903 case M_COP1:
12904 s = "c1";
12905 goto copz;
12906 case M_COP2:
12907 s = "c2";
12908 goto copz;
12909 case M_COP3:
12910 s = "c3";
12911 copz:
df58fc94 12912 gas_assert (!mips_opts.micromips);
252b5132
RH
12913 /* For now we just do C (same as Cz). The parameter will be
12914 stored in insn_opcode by mips_ip. */
c8276761 12915 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12916 break;
252b5132 12917
ea1fb5dc 12918 case M_MOVE:
c0ebe874 12919 move_register (op[0], op[1]);
8fc2e39e 12920 break;
ea1fb5dc 12921
833794fc
MR
12922 case M_MOVEP:
12923 gas_assert (mips_opts.micromips);
12924 gas_assert (mips_opts.insn32);
c0ebe874
RS
12925 move_register (micromips_to_32_reg_h_map1[op[0]],
12926 micromips_to_32_reg_m_map[op[1]]);
12927 move_register (micromips_to_32_reg_h_map2[op[0]],
12928 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12929 break;
12930
252b5132
RH
12931 case M_DMUL:
12932 dbl = 1;
1a0670f3 12933 /* Fall through. */
252b5132 12934 case M_MUL:
e407c74b 12935 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12936 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12937 op[2]);
e407c74b
NC
12938 else
12939 {
c0ebe874
RS
12940 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12941 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12942 }
8fc2e39e 12943 break;
252b5132
RH
12944
12945 case M_DMUL_I:
12946 dbl = 1;
1a0670f3 12947 /* Fall through. */
252b5132
RH
12948 case M_MUL_I:
12949 /* The MIPS assembler some times generates shifts and adds. I'm
12950 not trying to be that fancy. GCC should do this for us
12951 anyway. */
8fc2e39e 12952 used_at = 1;
67c0d1eb 12953 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12954 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12955 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12956 break;
12957
12958 case M_DMULO_I:
12959 dbl = 1;
1a0670f3 12960 /* Fall through. */
252b5132
RH
12961 case M_MULO_I:
12962 imm = 1;
12963 goto do_mulo;
12964
12965 case M_DMULO:
12966 dbl = 1;
1a0670f3 12967 /* Fall through. */
252b5132
RH
12968 case M_MULO:
12969 do_mulo:
7d10b47d 12970 start_noreorder ();
8fc2e39e 12971 used_at = 1;
252b5132 12972 if (imm)
67c0d1eb 12973 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12974 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12975 op[1], imm ? AT : op[2]);
12976 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12977 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12978 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12979 if (mips_trap)
c0ebe874 12980 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12981 else
12982 {
df58fc94
RS
12983 if (mips_opts.micromips)
12984 micromips_label_expr (&label_expr);
12985 else
12986 label_expr.X_add_number = 8;
c0ebe874 12987 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12988 macro_build (NULL, "nop", "");
df58fc94
RS
12989 macro_build (NULL, "break", BRK_FMT, 6);
12990 if (mips_opts.micromips)
12991 micromips_add_label ();
252b5132 12992 }
7d10b47d 12993 end_noreorder ();
c0ebe874 12994 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12995 break;
12996
12997 case M_DMULOU_I:
12998 dbl = 1;
1a0670f3 12999 /* Fall through. */
252b5132
RH
13000 case M_MULOU_I:
13001 imm = 1;
13002 goto do_mulou;
13003
13004 case M_DMULOU:
13005 dbl = 1;
1a0670f3 13006 /* Fall through. */
252b5132
RH
13007 case M_MULOU:
13008 do_mulou:
7d10b47d 13009 start_noreorder ();
8fc2e39e 13010 used_at = 1;
252b5132 13011 if (imm)
67c0d1eb
RS
13012 load_register (AT, &imm_expr, dbl);
13013 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13014 op[1], imm ? AT : op[2]);
df58fc94 13015 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13016 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13017 if (mips_trap)
df58fc94 13018 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13019 else
13020 {
df58fc94
RS
13021 if (mips_opts.micromips)
13022 micromips_label_expr (&label_expr);
13023 else
13024 label_expr.X_add_number = 8;
13025 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13026 macro_build (NULL, "nop", "");
df58fc94
RS
13027 macro_build (NULL, "break", BRK_FMT, 6);
13028 if (mips_opts.micromips)
13029 micromips_add_label ();
252b5132 13030 }
7d10b47d 13031 end_noreorder ();
252b5132
RH
13032 break;
13033
771c7ce4 13034 case M_DROL:
fef14a42 13035 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13036 {
c0ebe874 13037 if (op[0] == op[1])
82dd0097
CD
13038 {
13039 tempreg = AT;
13040 used_at = 1;
13041 }
13042 else
c0ebe874
RS
13043 tempreg = op[0];
13044 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13045 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13046 break;
82dd0097 13047 }
8fc2e39e 13048 used_at = 1;
c0ebe874
RS
13049 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13050 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13051 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13052 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13053 break;
13054
252b5132 13055 case M_ROL:
fef14a42 13056 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13057 {
c0ebe874 13058 if (op[0] == op[1])
82dd0097
CD
13059 {
13060 tempreg = AT;
13061 used_at = 1;
13062 }
13063 else
c0ebe874
RS
13064 tempreg = op[0];
13065 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13066 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13067 break;
82dd0097 13068 }
8fc2e39e 13069 used_at = 1;
c0ebe874
RS
13070 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13071 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13072 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13073 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13074 break;
13075
771c7ce4
TS
13076 case M_DROL_I:
13077 {
13078 unsigned int rot;
e0471c16
TS
13079 const char *l;
13080 const char *rr;
771c7ce4 13081
771c7ce4 13082 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13083 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13084 {
13085 rot = (64 - rot) & 0x3f;
13086 if (rot >= 32)
c0ebe874 13087 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13088 else
c0ebe874 13089 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13090 break;
60b63b72 13091 }
483fc7cd 13092 if (rot == 0)
483fc7cd 13093 {
c0ebe874 13094 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13095 break;
483fc7cd 13096 }
82dd0097 13097 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13098 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13099 rot &= 0x1f;
8fc2e39e 13100 used_at = 1;
c0ebe874
RS
13101 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13102 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13103 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13104 }
13105 break;
13106
252b5132 13107 case M_ROL_I:
771c7ce4
TS
13108 {
13109 unsigned int rot;
13110
771c7ce4 13111 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13112 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13113 {
c0ebe874
RS
13114 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13115 (32 - rot) & 0x1f);
8fc2e39e 13116 break;
60b63b72 13117 }
483fc7cd 13118 if (rot == 0)
483fc7cd 13119 {
c0ebe874 13120 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13121 break;
483fc7cd 13122 }
8fc2e39e 13123 used_at = 1;
c0ebe874
RS
13124 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13125 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13126 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13127 }
13128 break;
13129
13130 case M_DROR:
fef14a42 13131 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13132 {
c0ebe874 13133 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13134 break;
82dd0097 13135 }
8fc2e39e 13136 used_at = 1;
c0ebe874
RS
13137 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13138 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13139 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13140 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13141 break;
13142
13143 case M_ROR:
fef14a42 13144 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13145 {
c0ebe874 13146 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13147 break;
82dd0097 13148 }
8fc2e39e 13149 used_at = 1;
c0ebe874
RS
13150 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13151 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13152 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13153 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13154 break;
13155
771c7ce4
TS
13156 case M_DROR_I:
13157 {
13158 unsigned int rot;
e0471c16
TS
13159 const char *l;
13160 const char *rr;
771c7ce4 13161
771c7ce4 13162 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13163 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13164 {
13165 if (rot >= 32)
c0ebe874 13166 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13167 else
c0ebe874 13168 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13169 break;
82dd0097 13170 }
483fc7cd 13171 if (rot == 0)
483fc7cd 13172 {
c0ebe874 13173 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13174 break;
483fc7cd 13175 }
91d6fa6a 13176 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13177 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13178 rot &= 0x1f;
8fc2e39e 13179 used_at = 1;
c0ebe874
RS
13180 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13181 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13182 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13183 }
13184 break;
13185
252b5132 13186 case M_ROR_I:
771c7ce4
TS
13187 {
13188 unsigned int rot;
13189
771c7ce4 13190 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13191 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13192 {
c0ebe874 13193 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13194 break;
82dd0097 13195 }
483fc7cd 13196 if (rot == 0)
483fc7cd 13197 {
c0ebe874 13198 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13199 break;
483fc7cd 13200 }
8fc2e39e 13201 used_at = 1;
c0ebe874
RS
13202 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13203 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13204 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13205 }
252b5132
RH
13206 break;
13207
252b5132 13208 case M_SEQ:
c0ebe874
RS
13209 if (op[1] == 0)
13210 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13211 else if (op[2] == 0)
13212 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13213 else
13214 {
c0ebe874
RS
13215 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13216 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13217 }
8fc2e39e 13218 break;
252b5132
RH
13219
13220 case M_SEQ_I:
b0e6f033 13221 if (imm_expr.X_add_number == 0)
252b5132 13222 {
c0ebe874 13223 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13224 break;
252b5132 13225 }
c0ebe874 13226 if (op[1] == 0)
252b5132 13227 {
1661c76c 13228 as_warn (_("instruction %s: result is always false"),
252b5132 13229 ip->insn_mo->name);
c0ebe874 13230 move_register (op[0], 0);
8fc2e39e 13231 break;
252b5132 13232 }
dd3cbb7e
NC
13233 if (CPU_HAS_SEQ (mips_opts.arch)
13234 && -512 <= imm_expr.X_add_number
13235 && imm_expr.X_add_number < 512)
13236 {
c0ebe874 13237 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13238 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13239 break;
13240 }
b0e6f033 13241 if (imm_expr.X_add_number >= 0
252b5132 13242 && imm_expr.X_add_number < 0x10000)
c0ebe874 13243 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13244 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13245 && imm_expr.X_add_number < 0)
13246 {
13247 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13248 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13249 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13250 }
dd3cbb7e
NC
13251 else if (CPU_HAS_SEQ (mips_opts.arch))
13252 {
13253 used_at = 1;
bad1aba3 13254 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13255 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13256 break;
13257 }
252b5132
RH
13258 else
13259 {
bad1aba3 13260 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13261 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13262 used_at = 1;
13263 }
c0ebe874 13264 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13265 break;
252b5132 13266
c0ebe874 13267 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13268 s = "slt";
13269 goto sge;
13270 case M_SGEU:
13271 s = "sltu";
13272 sge:
c0ebe874
RS
13273 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13274 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13275 break;
252b5132 13276
c0ebe874 13277 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13278 case M_SGEU_I:
b0e6f033 13279 if (imm_expr.X_add_number >= -0x8000
252b5132 13280 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13281 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13282 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13283 else
13284 {
bad1aba3 13285 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13286 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13287 op[0], op[1], AT);
252b5132
RH
13288 used_at = 1;
13289 }
c0ebe874 13290 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13291 break;
252b5132 13292
c0ebe874 13293 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13294 s = "slt";
13295 goto sgt;
13296 case M_SGTU:
13297 s = "sltu";
13298 sgt:
c0ebe874 13299 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13300 break;
252b5132 13301
c0ebe874 13302 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13303 s = "slt";
13304 goto sgti;
13305 case M_SGTU_I:
13306 s = "sltu";
13307 sgti:
8fc2e39e 13308 used_at = 1;
bad1aba3 13309 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13310 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13311 break;
13312
c0ebe874 13313 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13314 s = "slt";
13315 goto sle;
13316 case M_SLEU:
13317 s = "sltu";
13318 sle:
c0ebe874
RS
13319 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13320 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13321 break;
252b5132 13322
c0ebe874 13323 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13324 s = "slt";
13325 goto slei;
13326 case M_SLEU_I:
13327 s = "sltu";
13328 slei:
8fc2e39e 13329 used_at = 1;
bad1aba3 13330 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13331 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13332 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13333 break;
13334
13335 case M_SLT_I:
b0e6f033 13336 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13337 && imm_expr.X_add_number < 0x8000)
13338 {
c0ebe874
RS
13339 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13340 BFD_RELOC_LO16);
8fc2e39e 13341 break;
252b5132 13342 }
8fc2e39e 13343 used_at = 1;
bad1aba3 13344 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13345 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13346 break;
13347
13348 case M_SLTU_I:
b0e6f033 13349 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13350 && imm_expr.X_add_number < 0x8000)
13351 {
c0ebe874 13352 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13353 BFD_RELOC_LO16);
8fc2e39e 13354 break;
252b5132 13355 }
8fc2e39e 13356 used_at = 1;
bad1aba3 13357 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13358 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13359 break;
13360
13361 case M_SNE:
c0ebe874
RS
13362 if (op[1] == 0)
13363 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13364 else if (op[2] == 0)
13365 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13366 else
13367 {
c0ebe874
RS
13368 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13369 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13370 }
8fc2e39e 13371 break;
252b5132
RH
13372
13373 case M_SNE_I:
b0e6f033 13374 if (imm_expr.X_add_number == 0)
252b5132 13375 {
c0ebe874 13376 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13377 break;
252b5132 13378 }
c0ebe874 13379 if (op[1] == 0)
252b5132 13380 {
1661c76c 13381 as_warn (_("instruction %s: result is always true"),
252b5132 13382 ip->insn_mo->name);
bad1aba3 13383 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13384 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13385 break;
252b5132 13386 }
dd3cbb7e
NC
13387 if (CPU_HAS_SEQ (mips_opts.arch)
13388 && -512 <= imm_expr.X_add_number
13389 && imm_expr.X_add_number < 512)
13390 {
c0ebe874 13391 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13392 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13393 break;
13394 }
b0e6f033 13395 if (imm_expr.X_add_number >= 0
252b5132
RH
13396 && imm_expr.X_add_number < 0x10000)
13397 {
c0ebe874
RS
13398 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13399 BFD_RELOC_LO16);
252b5132 13400 }
b0e6f033 13401 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13402 && imm_expr.X_add_number < 0)
13403 {
13404 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13405 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13406 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13407 }
dd3cbb7e
NC
13408 else if (CPU_HAS_SEQ (mips_opts.arch))
13409 {
13410 used_at = 1;
bad1aba3 13411 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13412 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13413 break;
13414 }
252b5132
RH
13415 else
13416 {
bad1aba3 13417 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13418 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13419 used_at = 1;
13420 }
c0ebe874 13421 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13422 break;
252b5132 13423
df58fc94
RS
13424 case M_SUB_I:
13425 s = "addi";
13426 s2 = "sub";
13427 goto do_subi;
13428 case M_SUBU_I:
13429 s = "addiu";
13430 s2 = "subu";
13431 goto do_subi;
252b5132
RH
13432 case M_DSUB_I:
13433 dbl = 1;
df58fc94
RS
13434 s = "daddi";
13435 s2 = "dsub";
13436 if (!mips_opts.micromips)
13437 goto do_subi;
b0e6f033 13438 if (imm_expr.X_add_number > -0x200
df58fc94 13439 && imm_expr.X_add_number <= 0x200)
252b5132 13440 {
b0e6f033
RS
13441 macro_build (NULL, s, "t,r,.", op[0], op[1],
13442 (int) -imm_expr.X_add_number);
8fc2e39e 13443 break;
252b5132 13444 }
df58fc94 13445 goto do_subi_i;
252b5132
RH
13446 case M_DSUBU_I:
13447 dbl = 1;
df58fc94
RS
13448 s = "daddiu";
13449 s2 = "dsubu";
13450 do_subi:
b0e6f033 13451 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13452 && imm_expr.X_add_number <= 0x8000)
13453 {
13454 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13455 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13456 break;
252b5132 13457 }
df58fc94 13458 do_subi_i:
8fc2e39e 13459 used_at = 1;
67c0d1eb 13460 load_register (AT, &imm_expr, dbl);
c0ebe874 13461 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13462 break;
13463
13464 case M_TEQ_I:
13465 s = "teq";
13466 goto trap;
13467 case M_TGE_I:
13468 s = "tge";
13469 goto trap;
13470 case M_TGEU_I:
13471 s = "tgeu";
13472 goto trap;
13473 case M_TLT_I:
13474 s = "tlt";
13475 goto trap;
13476 case M_TLTU_I:
13477 s = "tltu";
13478 goto trap;
13479 case M_TNE_I:
13480 s = "tne";
13481 trap:
8fc2e39e 13482 used_at = 1;
bad1aba3 13483 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13484 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13485 break;
13486
252b5132 13487 case M_TRUNCWS:
43841e91 13488 case M_TRUNCWD:
df58fc94 13489 gas_assert (!mips_opts.micromips);
0aa27725 13490 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13491 used_at = 1;
252b5132
RH
13492
13493 /*
13494 * Is the double cfc1 instruction a bug in the mips assembler;
13495 * or is there a reason for it?
13496 */
7d10b47d 13497 start_noreorder ();
c0ebe874
RS
13498 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13499 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13500 macro_build (NULL, "nop", "");
252b5132 13501 expr1.X_add_number = 3;
c0ebe874 13502 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13503 expr1.X_add_number = 2;
67c0d1eb
RS
13504 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13505 macro_build (NULL, "ctc1", "t,G", AT, RA);
13506 macro_build (NULL, "nop", "");
13507 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13508 op[0], op[1]);
13509 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13510 macro_build (NULL, "nop", "");
7d10b47d 13511 end_noreorder ();
252b5132
RH
13512 break;
13513
f2ae14a1 13514 case M_ULH_AB:
252b5132 13515 s = "lb";
df58fc94
RS
13516 s2 = "lbu";
13517 off = 1;
13518 goto uld_st;
f2ae14a1 13519 case M_ULHU_AB:
252b5132 13520 s = "lbu";
df58fc94
RS
13521 s2 = "lbu";
13522 off = 1;
13523 goto uld_st;
f2ae14a1 13524 case M_ULW_AB:
df58fc94
RS
13525 s = "lwl";
13526 s2 = "lwr";
7f3c4072 13527 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13528 off = 3;
13529 goto uld_st;
f2ae14a1 13530 case M_ULD_AB:
252b5132
RH
13531 s = "ldl";
13532 s2 = "ldr";
7f3c4072 13533 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13534 off = 7;
df58fc94 13535 goto uld_st;
f2ae14a1 13536 case M_USH_AB:
df58fc94
RS
13537 s = "sb";
13538 s2 = "sb";
13539 off = 1;
13540 ust = 1;
13541 goto uld_st;
f2ae14a1 13542 case M_USW_AB:
df58fc94
RS
13543 s = "swl";
13544 s2 = "swr";
7f3c4072 13545 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13546 off = 3;
df58fc94
RS
13547 ust = 1;
13548 goto uld_st;
f2ae14a1 13549 case M_USD_AB:
df58fc94
RS
13550 s = "sdl";
13551 s2 = "sdr";
7f3c4072 13552 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13553 off = 7;
13554 ust = 1;
13555
13556 uld_st:
c0ebe874 13557 breg = op[2];
f2ae14a1 13558 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13559 ep = &offset_expr;
13560 expr1.X_add_number = 0;
f2ae14a1 13561 if (large_offset)
df58fc94
RS
13562 {
13563 used_at = 1;
13564 tempreg = AT;
f2ae14a1
RS
13565 if (small_offset_p (0, align, 16))
13566 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13567 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13568 else
13569 {
13570 load_address (tempreg, ep, &used_at);
13571 if (breg != 0)
13572 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13573 tempreg, tempreg, breg);
13574 }
13575 offset_reloc[0] = BFD_RELOC_LO16;
13576 offset_reloc[1] = BFD_RELOC_UNUSED;
13577 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13578 breg = tempreg;
c0ebe874 13579 tempreg = op[0];
df58fc94
RS
13580 ep = &expr1;
13581 }
c0ebe874 13582 else if (!ust && op[0] == breg)
8fc2e39e
TS
13583 {
13584 used_at = 1;
13585 tempreg = AT;
13586 }
252b5132 13587 else
c0ebe874 13588 tempreg = op[0];
af22f5b2 13589
df58fc94
RS
13590 if (off == 1)
13591 goto ulh_sh;
252b5132 13592
90ecf173 13593 if (!target_big_endian)
df58fc94 13594 ep->X_add_number += off;
f2ae14a1 13595 if (offbits == 12)
c8276761 13596 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13597 else
13598 macro_build (ep, s, "t,o(b)", tempreg, -1,
13599 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13600
90ecf173 13601 if (!target_big_endian)
df58fc94 13602 ep->X_add_number -= off;
252b5132 13603 else
df58fc94 13604 ep->X_add_number += off;
f2ae14a1 13605 if (offbits == 12)
df58fc94 13606 macro_build (NULL, s2, "t,~(b)",
c8276761 13607 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13608 else
13609 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13610 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13611
df58fc94 13612 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13613 if (!ust && op[0] != tempreg)
df58fc94
RS
13614 {
13615 /* Protect second load's delay slot. */
13616 load_delay_nop ();
c0ebe874 13617 move_register (op[0], tempreg);
df58fc94 13618 }
8fc2e39e 13619 break;
252b5132 13620
df58fc94 13621 ulh_sh:
d6bc6245 13622 used_at = 1;
df58fc94
RS
13623 if (target_big_endian == ust)
13624 ep->X_add_number += off;
c0ebe874 13625 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13626 macro_build (ep, s, "t,o(b)", tempreg, -1,
13627 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13628
13629 /* For halfword transfers we need a temporary register to shuffle
13630 bytes. Unfortunately for M_USH_A we have none available before
13631 the next store as AT holds the base address. We deal with this
13632 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13633 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13634 if (ust)
c0ebe874 13635 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13636
13637 if (target_big_endian == ust)
13638 ep->X_add_number -= off;
252b5132 13639 else
df58fc94 13640 ep->X_add_number += off;
f2ae14a1
RS
13641 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13642 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13643
df58fc94 13644 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13645 if (ust && large_offset)
df58fc94
RS
13646 {
13647 if (target_big_endian)
13648 ep->X_add_number += off;
13649 else
13650 ep->X_add_number -= off;
f2ae14a1
RS
13651 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13652 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13653 }
13654 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13655 if (!ust || large_offset)
df58fc94 13656 {
c0ebe874 13657 tempreg = !large_offset ? AT : op[0];
df58fc94 13658 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13659 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13660 }
252b5132
RH
13661 break;
13662
13663 default:
13664 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13665 are added dynamically. */
1661c76c 13666 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13667 break;
13668 }
741fe287 13669 if (!mips_opts.at && used_at)
1661c76c 13670 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13671}
13672
13673/* Implement macros in mips16 mode. */
13674
13675static void
17a2f251 13676mips16_macro (struct mips_cl_insn *ip)
252b5132 13677{
c0ebe874 13678 const struct mips_operand_array *operands;
252b5132 13679 int mask;
c0ebe874 13680 int tmp;
252b5132
RH
13681 expressionS expr1;
13682 int dbl;
13683 const char *s, *s2, *s3;
c0ebe874
RS
13684 unsigned int op[MAX_OPERANDS];
13685 unsigned int i;
252b5132
RH
13686
13687 mask = ip->insn_mo->mask;
13688
c0ebe874
RS
13689 operands = insn_operands (ip);
13690 for (i = 0; i < MAX_OPERANDS; i++)
13691 if (operands->operand[i])
13692 op[i] = insn_extract_operand (ip, operands->operand[i]);
13693 else
13694 op[i] = -1;
252b5132 13695
252b5132
RH
13696 expr1.X_op = O_constant;
13697 expr1.X_op_symbol = NULL;
13698 expr1.X_add_symbol = NULL;
13699 expr1.X_add_number = 1;
13700
13701 dbl = 0;
13702
13703 switch (mask)
13704 {
13705 default:
b37df7c4 13706 abort ();
252b5132
RH
13707
13708 case M_DDIV_3:
13709 dbl = 1;
1a0670f3 13710 /* Fall through. */
252b5132
RH
13711 case M_DIV_3:
13712 s = "mflo";
13713 goto do_div3;
13714 case M_DREM_3:
13715 dbl = 1;
1a0670f3 13716 /* Fall through. */
252b5132
RH
13717 case M_REM_3:
13718 s = "mfhi";
13719 do_div3:
7d10b47d 13720 start_noreorder ();
d8722d76 13721 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13722 expr1.X_add_number = 2;
c0ebe874 13723 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13724 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13725
252b5132
RH
13726 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13727 since that causes an overflow. We should do that as well,
13728 but I don't see how to do the comparisons without a temporary
13729 register. */
7d10b47d 13730 end_noreorder ();
c0ebe874 13731 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13732 break;
13733
13734 case M_DIVU_3:
13735 s = "divu";
13736 s2 = "mflo";
13737 goto do_divu3;
13738 case M_REMU_3:
13739 s = "divu";
13740 s2 = "mfhi";
13741 goto do_divu3;
13742 case M_DDIVU_3:
13743 s = "ddivu";
13744 s2 = "mflo";
13745 goto do_divu3;
13746 case M_DREMU_3:
13747 s = "ddivu";
13748 s2 = "mfhi";
13749 do_divu3:
7d10b47d 13750 start_noreorder ();
d8722d76 13751 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13752 expr1.X_add_number = 2;
c0ebe874 13753 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13754 macro_build (NULL, "break", "6", 7);
7d10b47d 13755 end_noreorder ();
c0ebe874 13756 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13757 break;
13758
13759 case M_DMUL:
13760 dbl = 1;
1a0670f3 13761 /* Fall through. */
252b5132 13762 case M_MUL:
c0ebe874
RS
13763 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13764 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13765 break;
252b5132
RH
13766
13767 case M_DSUBU_I:
13768 dbl = 1;
13769 goto do_subu;
13770 case M_SUBU_I:
13771 do_subu:
252b5132 13772 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13773 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13774 break;
13775
13776 case M_SUBU_I_2:
252b5132 13777 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13778 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13779 break;
13780
13781 case M_DSUBU_I_2:
252b5132 13782 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13783 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13784 break;
13785
13786 case M_BEQ:
13787 s = "cmp";
13788 s2 = "bteqz";
13789 goto do_branch;
13790 case M_BNE:
13791 s = "cmp";
13792 s2 = "btnez";
13793 goto do_branch;
13794 case M_BLT:
13795 s = "slt";
13796 s2 = "btnez";
13797 goto do_branch;
13798 case M_BLTU:
13799 s = "sltu";
13800 s2 = "btnez";
13801 goto do_branch;
13802 case M_BLE:
13803 s = "slt";
13804 s2 = "bteqz";
13805 goto do_reverse_branch;
13806 case M_BLEU:
13807 s = "sltu";
13808 s2 = "bteqz";
13809 goto do_reverse_branch;
13810 case M_BGE:
13811 s = "slt";
13812 s2 = "bteqz";
13813 goto do_branch;
13814 case M_BGEU:
13815 s = "sltu";
13816 s2 = "bteqz";
13817 goto do_branch;
13818 case M_BGT:
13819 s = "slt";
13820 s2 = "btnez";
13821 goto do_reverse_branch;
13822 case M_BGTU:
13823 s = "sltu";
13824 s2 = "btnez";
13825
13826 do_reverse_branch:
c0ebe874
RS
13827 tmp = op[1];
13828 op[1] = op[0];
13829 op[0] = tmp;
252b5132
RH
13830
13831 do_branch:
c0ebe874 13832 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13833 macro_build (&offset_expr, s2, "p");
252b5132
RH
13834 break;
13835
13836 case M_BEQ_I:
13837 s = "cmpi";
13838 s2 = "bteqz";
13839 s3 = "x,U";
13840 goto do_branch_i;
13841 case M_BNE_I:
13842 s = "cmpi";
13843 s2 = "btnez";
13844 s3 = "x,U";
13845 goto do_branch_i;
13846 case M_BLT_I:
13847 s = "slti";
13848 s2 = "btnez";
13849 s3 = "x,8";
13850 goto do_branch_i;
13851 case M_BLTU_I:
13852 s = "sltiu";
13853 s2 = "btnez";
13854 s3 = "x,8";
13855 goto do_branch_i;
13856 case M_BLE_I:
13857 s = "slti";
13858 s2 = "btnez";
13859 s3 = "x,8";
13860 goto do_addone_branch_i;
13861 case M_BLEU_I:
13862 s = "sltiu";
13863 s2 = "btnez";
13864 s3 = "x,8";
13865 goto do_addone_branch_i;
13866 case M_BGE_I:
13867 s = "slti";
13868 s2 = "bteqz";
13869 s3 = "x,8";
13870 goto do_branch_i;
13871 case M_BGEU_I:
13872 s = "sltiu";
13873 s2 = "bteqz";
13874 s3 = "x,8";
13875 goto do_branch_i;
13876 case M_BGT_I:
13877 s = "slti";
13878 s2 = "bteqz";
13879 s3 = "x,8";
13880 goto do_addone_branch_i;
13881 case M_BGTU_I:
13882 s = "sltiu";
13883 s2 = "bteqz";
13884 s3 = "x,8";
13885
13886 do_addone_branch_i:
252b5132
RH
13887 ++imm_expr.X_add_number;
13888
13889 do_branch_i:
c0ebe874 13890 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13891 macro_build (&offset_expr, s2, "p");
252b5132
RH
13892 break;
13893
13894 case M_ABS:
13895 expr1.X_add_number = 0;
c0ebe874
RS
13896 macro_build (&expr1, "slti", "x,8", op[1]);
13897 if (op[0] != op[1])
13898 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13899 expr1.X_add_number = 2;
67c0d1eb 13900 macro_build (&expr1, "bteqz", "p");
c0ebe874 13901 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13902 break;
252b5132
RH
13903 }
13904}
13905
14daeee3
RS
13906/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13907 opcode bits in *OPCODE_EXTRA. */
13908
13909static struct mips_opcode *
13910mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13911 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13912{
13913 char *name, *dot, *p;
13914 unsigned int mask, suffix;
da8bca91 13915 ssize_t opend;
14daeee3
RS
13916 struct mips_opcode *insn;
13917
13918 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13919 name = xstrndup (start, length);
14daeee3
RS
13920
13921 /* Look up the instruction as-is. */
13922 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13923 if (insn)
e1fa0163 13924 goto end;
14daeee3
RS
13925
13926 dot = strchr (name, '.');
13927 if (dot && dot[1])
13928 {
13929 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13930 p = mips_parse_vu0_channels (dot + 1, &mask);
13931 if (*p == 0 && mask != 0)
13932 {
13933 *dot = 0;
13934 insn = (struct mips_opcode *) hash_find (hash, name);
13935 *dot = '.';
13936 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13937 {
13938 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13939 goto end;
14daeee3
RS
13940 }
13941 }
13942 }
13943
13944 if (mips_opts.micromips)
13945 {
13946 /* See if there's an instruction size override suffix,
13947 either `16' or `32', at the end of the mnemonic proper,
13948 that defines the operation, i.e. before the first `.'
13949 character if any. Strip it and retry. */
13950 opend = dot != NULL ? dot - name : length;
13951 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13952 suffix = 2;
13953 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13954 suffix = 4;
13955 else
13956 suffix = 0;
13957 if (suffix)
13958 {
13959 memcpy (name + opend - 2, name + opend, length - opend + 1);
13960 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13961 if (insn)
14daeee3
RS
13962 {
13963 forced_insn_length = suffix;
e1fa0163 13964 goto end;
14daeee3
RS
13965 }
13966 }
13967 }
13968
e1fa0163
NC
13969 insn = NULL;
13970 end:
13971 free (name);
13972 return insn;
14daeee3
RS
13973}
13974
77bd4346 13975/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13976 is a macro, set imm_expr and offset_expr to the values associated
13977 with "I" and "A" operands respectively. Otherwise store the value
13978 of the relocatable field (if any) in offset_expr. In both cases
13979 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13980
13981static void
60f20e8b 13982mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13983{
60f20e8b 13984 const struct mips_opcode *first, *past;
df58fc94 13985 struct hash_control *hash;
a92713e6 13986 char format;
14daeee3 13987 size_t end;
a92713e6 13988 struct mips_operand_token *tokens;
14daeee3 13989 unsigned int opcode_extra;
252b5132 13990
df58fc94
RS
13991 if (mips_opts.micromips)
13992 {
13993 hash = micromips_op_hash;
13994 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13995 }
13996 else
13997 {
13998 hash = op_hash;
13999 past = &mips_opcodes[NUMOPCODES];
14000 }
14001 forced_insn_length = 0;
14daeee3 14002 opcode_extra = 0;
252b5132 14003
df58fc94 14004 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14005 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14006 continue;
bdaaa2e1 14007
60f20e8b
RS
14008 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14009 if (first == NULL)
252b5132 14010 {
1661c76c 14011 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14012 return;
252b5132
RH
14013 }
14014
60f20e8b 14015 if (strcmp (first->name, "li.s") == 0)
a92713e6 14016 format = 'f';
60f20e8b 14017 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14018 format = 'd';
14019 else
14020 format = 0;
14021 tokens = mips_parse_arguments (str + end, format);
14022 if (!tokens)
14023 return;
14024
60f20e8b
RS
14025 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14026 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14027 set_insn_error (0, _("invalid operands"));
df58fc94 14028
e3de51ce 14029 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14030}
14031
77bd4346
RS
14032/* As for mips_ip, but used when assembling MIPS16 code.
14033 Also set forced_insn_length to the resulting instruction size in
14034 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14035
14036static void
60f20e8b 14037mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14038{
1a00e612 14039 char *end, *s, c;
60f20e8b 14040 struct mips_opcode *first;
a92713e6 14041 struct mips_operand_token *tokens;
3fb49709 14042 unsigned int l;
252b5132 14043
25499ac7 14044 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14045 ;
1a00e612
RS
14046 end = s;
14047 c = *end;
3fb49709
MR
14048
14049 l = 0;
1a00e612 14050 switch (c)
252b5132
RH
14051 {
14052 case '\0':
14053 break;
14054
14055 case ' ':
1a00e612 14056 s++;
252b5132
RH
14057 break;
14058
14059 case '.':
3fb49709
MR
14060 s++;
14061 if (*s == 't')
252b5132 14062 {
3fb49709
MR
14063 l = 2;
14064 s++;
252b5132 14065 }
3fb49709 14066 else if (*s == 'e')
252b5132 14067 {
3fb49709
MR
14068 l = 4;
14069 s++;
252b5132 14070 }
3fb49709
MR
14071 if (*s == '\0')
14072 break;
14073 else if (*s++ == ' ')
14074 break;
1661c76c 14075 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14076 return;
14077 }
3fb49709 14078 forced_insn_length = l;
252b5132 14079
1a00e612 14080 *end = 0;
60f20e8b 14081 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14082 *end = c;
14083
60f20e8b 14084 if (!first)
252b5132 14085 {
1661c76c 14086 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14087 return;
14088 }
14089
a92713e6
RS
14090 tokens = mips_parse_arguments (s, 0);
14091 if (!tokens)
14092 return;
14093
60f20e8b 14094 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14095 set_insn_error (0, _("invalid operands"));
252b5132 14096
e3de51ce 14097 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14098}
14099
b886a2ab
RS
14100/* Marshal immediate value VAL for an extended MIPS16 instruction.
14101 NBITS is the number of significant bits in VAL. */
14102
14103static unsigned long
14104mips16_immed_extend (offsetT val, unsigned int nbits)
14105{
14106 int extval;
25499ac7
MR
14107
14108 extval = 0;
14109 val &= (1U << nbits) - 1;
14110 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14111 {
14112 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14113 val &= 0x1f;
14114 }
14115 else if (nbits == 15)
14116 {
14117 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14118 val &= 0xf;
14119 }
25499ac7 14120 else if (nbits == 6)
b886a2ab
RS
14121 {
14122 extval = ((val & 0x1f) << 6) | (val & 0x20);
14123 val = 0;
14124 }
14125 return (extval << 16) | val;
14126}
14127
3ccad066
RS
14128/* Like decode_mips16_operand, but require the operand to be defined and
14129 require it to be an integer. */
14130
14131static const struct mips_int_operand *
14132mips16_immed_operand (int type, bfd_boolean extended_p)
14133{
14134 const struct mips_operand *operand;
14135
14136 operand = decode_mips16_operand (type, extended_p);
14137 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14138 abort ();
14139 return (const struct mips_int_operand *) operand;
14140}
14141
14142/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14143
14144static bfd_boolean
14145mips16_immed_in_range_p (const struct mips_int_operand *operand,
14146 bfd_reloc_code_real_type reloc, offsetT sval)
14147{
14148 int min_val, max_val;
14149
14150 min_val = mips_int_operand_min (operand);
14151 max_val = mips_int_operand_max (operand);
14152 if (reloc != BFD_RELOC_UNUSED)
14153 {
14154 if (min_val < 0)
14155 sval = SEXT_16BIT (sval);
14156 else
14157 sval &= 0xffff;
14158 }
14159
14160 return (sval >= min_val
14161 && sval <= max_val
14162 && (sval & ((1 << operand->shift) - 1)) == 0);
14163}
14164
5c04167a
RS
14165/* Install immediate value VAL into MIPS16 instruction *INSN,
14166 extending it if necessary. The instruction in *INSN may
14167 already be extended.
14168
43c0598f
RS
14169 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14170 if none. In the former case, VAL is a 16-bit number with no
14171 defined signedness.
14172
14173 TYPE is the type of the immediate field. USER_INSN_LENGTH
14174 is the length that the user requested, or 0 if none. */
252b5132
RH
14175
14176static void
3b4dbbbf 14177mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14178 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14179 unsigned int user_insn_length, unsigned long *insn)
252b5132 14180{
3ccad066
RS
14181 const struct mips_int_operand *operand;
14182 unsigned int uval, length;
252b5132 14183
3ccad066
RS
14184 operand = mips16_immed_operand (type, FALSE);
14185 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14186 {
14187 /* We need an extended instruction. */
14188 if (user_insn_length == 2)
14189 as_bad_where (file, line, _("invalid unextended operand value"));
14190 else
14191 *insn |= MIPS16_EXTEND;
14192 }
14193 else if (user_insn_length == 4)
14194 {
14195 /* The operand doesn't force an unextended instruction to be extended.
14196 Warn if the user wanted an extended instruction anyway. */
14197 *insn |= MIPS16_EXTEND;
14198 as_warn_where (file, line,
14199 _("extended operand requested but not required"));
14200 }
252b5132 14201
3ccad066
RS
14202 length = mips16_opcode_length (*insn);
14203 if (length == 4)
252b5132 14204 {
3ccad066
RS
14205 operand = mips16_immed_operand (type, TRUE);
14206 if (!mips16_immed_in_range_p (operand, reloc, val))
14207 as_bad_where (file, line,
14208 _("operand value out of range for instruction"));
252b5132 14209 }
3ccad066 14210 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14211 if (length == 2 || operand->root.lsb != 0)
3ccad066 14212 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14213 else
3ccad066 14214 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14215}
14216\f
d6f16593 14217struct percent_op_match
ad8d3bb3 14218{
5e0116d5
RS
14219 const char *str;
14220 bfd_reloc_code_real_type reloc;
d6f16593
MR
14221};
14222
14223static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14224{
5e0116d5 14225 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14226 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14227 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14228 {"%call16", BFD_RELOC_MIPS_CALL16},
14229 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14230 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14231 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14232 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14233 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14234 {"%got", BFD_RELOC_MIPS_GOT16},
14235 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14236 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14237 {"%half", BFD_RELOC_16},
14238 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14239 {"%higher", BFD_RELOC_MIPS_HIGHER},
14240 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14241 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14242 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14243 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14244 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14245 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14246 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14247 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14248 {"%hi", BFD_RELOC_HI16_S},
14249 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14250 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14251};
14252
d6f16593
MR
14253static const struct percent_op_match mips16_percent_op[] =
14254{
14255 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14256 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14257 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14258 {"%got", BFD_RELOC_MIPS16_GOT16},
14259 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14260 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14261 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14262 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14263 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14264 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14265 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14266 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14267 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14268};
14269
252b5132 14270
5e0116d5
RS
14271/* Return true if *STR points to a relocation operator. When returning true,
14272 move *STR over the operator and store its relocation code in *RELOC.
14273 Leave both *STR and *RELOC alone when returning false. */
14274
14275static bfd_boolean
17a2f251 14276parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14277{
d6f16593
MR
14278 const struct percent_op_match *percent_op;
14279 size_t limit, i;
14280
14281 if (mips_opts.mips16)
14282 {
14283 percent_op = mips16_percent_op;
14284 limit = ARRAY_SIZE (mips16_percent_op);
14285 }
14286 else
14287 {
14288 percent_op = mips_percent_op;
14289 limit = ARRAY_SIZE (mips_percent_op);
14290 }
76b3015f 14291
d6f16593 14292 for (i = 0; i < limit; i++)
5e0116d5 14293 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14294 {
3f98094e
DJ
14295 int len = strlen (percent_op[i].str);
14296
14297 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14298 continue;
14299
5e0116d5
RS
14300 *str += strlen (percent_op[i].str);
14301 *reloc = percent_op[i].reloc;
394f9b3a 14302
5e0116d5
RS
14303 /* Check whether the output BFD supports this relocation.
14304 If not, issue an error and fall back on something safe. */
14305 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14306 {
20203fb9 14307 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14308 percent_op[i].str);
01a3f561 14309 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14310 }
5e0116d5 14311 return TRUE;
394f9b3a 14312 }
5e0116d5 14313 return FALSE;
394f9b3a 14314}
ad8d3bb3 14315
ad8d3bb3 14316
5e0116d5
RS
14317/* Parse string STR as a 16-bit relocatable operand. Store the
14318 expression in *EP and the relocations in the array starting
14319 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14320
01a3f561 14321 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14322
5e0116d5 14323static size_t
17a2f251
TS
14324my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14325 char *str)
ad8d3bb3 14326{
5e0116d5
RS
14327 bfd_reloc_code_real_type reversed_reloc[3];
14328 size_t reloc_index, i;
09b8f35a
RS
14329 int crux_depth, str_depth;
14330 char *crux;
5e0116d5
RS
14331
14332 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14333 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14334 of the main expression and with CRUX_DEPTH containing the number
14335 of open brackets at that point. */
14336 reloc_index = -1;
14337 str_depth = 0;
14338 do
fb1b3232 14339 {
09b8f35a
RS
14340 reloc_index++;
14341 crux = str;
14342 crux_depth = str_depth;
14343
14344 /* Skip over whitespace and brackets, keeping count of the number
14345 of brackets. */
14346 while (*str == ' ' || *str == '\t' || *str == '(')
14347 if (*str++ == '(')
14348 str_depth++;
5e0116d5 14349 }
09b8f35a
RS
14350 while (*str == '%'
14351 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14352 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14353
09b8f35a 14354 my_getExpression (ep, crux);
5e0116d5 14355 str = expr_end;
394f9b3a 14356
5e0116d5 14357 /* Match every open bracket. */
09b8f35a 14358 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14359 if (*str++ == ')')
09b8f35a 14360 crux_depth--;
394f9b3a 14361
09b8f35a 14362 if (crux_depth > 0)
20203fb9 14363 as_bad (_("unclosed '('"));
394f9b3a 14364
5e0116d5 14365 expr_end = str;
252b5132 14366
01a3f561 14367 if (reloc_index != 0)
64bdfcaf
RS
14368 {
14369 prev_reloc_op_frag = frag_now;
14370 for (i = 0; i < reloc_index; i++)
14371 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14372 }
fb1b3232 14373
5e0116d5 14374 return reloc_index;
252b5132
RH
14375}
14376
14377static void
17a2f251 14378my_getExpression (expressionS *ep, char *str)
252b5132
RH
14379{
14380 char *save_in;
14381
14382 save_in = input_line_pointer;
14383 input_line_pointer = str;
14384 expression (ep);
14385 expr_end = input_line_pointer;
14386 input_line_pointer = save_in;
252b5132
RH
14387}
14388
6d4af3c2 14389const char *
17a2f251 14390md_atof (int type, char *litP, int *sizeP)
252b5132 14391{
499ac353 14392 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14393}
14394
14395void
17a2f251 14396md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14397{
14398 if (target_big_endian)
14399 number_to_chars_bigendian (buf, val, n);
14400 else
14401 number_to_chars_littleendian (buf, val, n);
14402}
14403\f
e013f690
TS
14404static int support_64bit_objects(void)
14405{
14406 const char **list, **l;
aa3d8fdf 14407 int yes;
e013f690
TS
14408
14409 list = bfd_target_list ();
14410 for (l = list; *l != NULL; l++)
aeffff67
RS
14411 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14412 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14413 break;
aa3d8fdf 14414 yes = (*l != NULL);
e013f690 14415 free (list);
aa3d8fdf 14416 return yes;
e013f690
TS
14417}
14418
316f5878
RS
14419/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14420 NEW_VALUE. Warn if another value was already specified. Note:
14421 we have to defer parsing the -march and -mtune arguments in order
14422 to handle 'from-abi' correctly, since the ABI might be specified
14423 in a later argument. */
14424
14425static void
17a2f251 14426mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14427{
14428 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14429 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14430 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14431 new_value);
14432
14433 *string_ptr = new_value;
14434}
14435
252b5132 14436int
17b9d67d 14437md_parse_option (int c, const char *arg)
252b5132 14438{
c6278170
RS
14439 unsigned int i;
14440
14441 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14442 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14443 {
919731af 14444 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14445 c == mips_ases[i].option_on);
14446 return 1;
14447 }
14448
252b5132
RH
14449 switch (c)
14450 {
119d663a
NC
14451 case OPTION_CONSTRUCT_FLOATS:
14452 mips_disable_float_construction = 0;
14453 break;
bdaaa2e1 14454
119d663a
NC
14455 case OPTION_NO_CONSTRUCT_FLOATS:
14456 mips_disable_float_construction = 1;
14457 break;
bdaaa2e1 14458
252b5132
RH
14459 case OPTION_TRAP:
14460 mips_trap = 1;
14461 break;
14462
14463 case OPTION_BREAK:
14464 mips_trap = 0;
14465 break;
14466
14467 case OPTION_EB:
14468 target_big_endian = 1;
14469 break;
14470
14471 case OPTION_EL:
14472 target_big_endian = 0;
14473 break;
14474
14475 case 'O':
4ffff32f
TS
14476 if (arg == NULL)
14477 mips_optimize = 1;
14478 else if (arg[0] == '0')
14479 mips_optimize = 0;
14480 else if (arg[0] == '1')
252b5132
RH
14481 mips_optimize = 1;
14482 else
14483 mips_optimize = 2;
14484 break;
14485
14486 case 'g':
14487 if (arg == NULL)
14488 mips_debug = 2;
14489 else
14490 mips_debug = atoi (arg);
252b5132
RH
14491 break;
14492
14493 case OPTION_MIPS1:
0b35dfee 14494 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14495 break;
14496
14497 case OPTION_MIPS2:
0b35dfee 14498 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14499 break;
14500
14501 case OPTION_MIPS3:
0b35dfee 14502 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14503 break;
14504
14505 case OPTION_MIPS4:
0b35dfee 14506 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14507 break;
14508
84ea6cf2 14509 case OPTION_MIPS5:
0b35dfee 14510 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14511 break;
14512
e7af610e 14513 case OPTION_MIPS32:
0b35dfee 14514 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14515 break;
14516
af7ee8bf 14517 case OPTION_MIPS32R2:
0b35dfee 14518 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14519 break;
14520
ae52f483 14521 case OPTION_MIPS32R3:
0ae19f05 14522 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14523 break;
14524
14525 case OPTION_MIPS32R5:
0ae19f05 14526 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14527 break;
14528
7361da2c
AB
14529 case OPTION_MIPS32R6:
14530 file_mips_opts.isa = ISA_MIPS32R6;
14531 break;
14532
5f74bc13 14533 case OPTION_MIPS64R2:
0b35dfee 14534 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14535 break;
14536
ae52f483 14537 case OPTION_MIPS64R3:
0ae19f05 14538 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14539 break;
14540
14541 case OPTION_MIPS64R5:
0ae19f05 14542 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14543 break;
14544
7361da2c
AB
14545 case OPTION_MIPS64R6:
14546 file_mips_opts.isa = ISA_MIPS64R6;
14547 break;
14548
84ea6cf2 14549 case OPTION_MIPS64:
0b35dfee 14550 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14551 break;
14552
ec68c924 14553 case OPTION_MTUNE:
316f5878
RS
14554 mips_set_option_string (&mips_tune_string, arg);
14555 break;
ec68c924 14556
316f5878
RS
14557 case OPTION_MARCH:
14558 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14559 break;
14560
14561 case OPTION_M4650:
316f5878
RS
14562 mips_set_option_string (&mips_arch_string, "4650");
14563 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14564 break;
14565
14566 case OPTION_NO_M4650:
14567 break;
14568
14569 case OPTION_M4010:
316f5878
RS
14570 mips_set_option_string (&mips_arch_string, "4010");
14571 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14572 break;
14573
14574 case OPTION_NO_M4010:
14575 break;
14576
14577 case OPTION_M4100:
316f5878
RS
14578 mips_set_option_string (&mips_arch_string, "4100");
14579 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14580 break;
14581
14582 case OPTION_NO_M4100:
14583 break;
14584
252b5132 14585 case OPTION_M3900:
316f5878
RS
14586 mips_set_option_string (&mips_arch_string, "3900");
14587 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14588 break;
bdaaa2e1 14589
252b5132
RH
14590 case OPTION_NO_M3900:
14591 break;
14592
df58fc94 14593 case OPTION_MICROMIPS:
919731af 14594 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14595 {
14596 as_bad (_("-mmicromips cannot be used with -mips16"));
14597 return 0;
14598 }
919731af 14599 file_mips_opts.micromips = 1;
df58fc94
RS
14600 mips_no_prev_insn ();
14601 break;
14602
14603 case OPTION_NO_MICROMIPS:
919731af 14604 file_mips_opts.micromips = 0;
df58fc94
RS
14605 mips_no_prev_insn ();
14606 break;
14607
252b5132 14608 case OPTION_MIPS16:
919731af 14609 if (file_mips_opts.micromips == 1)
df58fc94
RS
14610 {
14611 as_bad (_("-mips16 cannot be used with -micromips"));
14612 return 0;
14613 }
919731af 14614 file_mips_opts.mips16 = 1;
7d10b47d 14615 mips_no_prev_insn ();
252b5132
RH
14616 break;
14617
14618 case OPTION_NO_MIPS16:
919731af 14619 file_mips_opts.mips16 = 0;
7d10b47d 14620 mips_no_prev_insn ();
252b5132
RH
14621 break;
14622
6a32d874
CM
14623 case OPTION_FIX_24K:
14624 mips_fix_24k = 1;
14625 break;
14626
14627 case OPTION_NO_FIX_24K:
14628 mips_fix_24k = 0;
14629 break;
14630
a8d14a88
CM
14631 case OPTION_FIX_RM7000:
14632 mips_fix_rm7000 = 1;
14633 break;
14634
14635 case OPTION_NO_FIX_RM7000:
14636 mips_fix_rm7000 = 0;
14637 break;
14638
c67a084a
NC
14639 case OPTION_FIX_LOONGSON2F_JUMP:
14640 mips_fix_loongson2f_jump = TRUE;
14641 break;
14642
14643 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14644 mips_fix_loongson2f_jump = FALSE;
14645 break;
14646
14647 case OPTION_FIX_LOONGSON2F_NOP:
14648 mips_fix_loongson2f_nop = TRUE;
14649 break;
14650
14651 case OPTION_NO_FIX_LOONGSON2F_NOP:
14652 mips_fix_loongson2f_nop = FALSE;
14653 break;
14654
d766e8ec
RS
14655 case OPTION_FIX_VR4120:
14656 mips_fix_vr4120 = 1;
60b63b72
RS
14657 break;
14658
d766e8ec
RS
14659 case OPTION_NO_FIX_VR4120:
14660 mips_fix_vr4120 = 0;
60b63b72
RS
14661 break;
14662
7d8e00cf
RS
14663 case OPTION_FIX_VR4130:
14664 mips_fix_vr4130 = 1;
14665 break;
14666
14667 case OPTION_NO_FIX_VR4130:
14668 mips_fix_vr4130 = 0;
14669 break;
14670
d954098f
DD
14671 case OPTION_FIX_CN63XXP1:
14672 mips_fix_cn63xxp1 = TRUE;
14673 break;
14674
14675 case OPTION_NO_FIX_CN63XXP1:
14676 mips_fix_cn63xxp1 = FALSE;
14677 break;
14678
4a6a3df4
AO
14679 case OPTION_RELAX_BRANCH:
14680 mips_relax_branch = 1;
14681 break;
14682
14683 case OPTION_NO_RELAX_BRANCH:
14684 mips_relax_branch = 0;
14685 break;
14686
8b10b0b3
MR
14687 case OPTION_IGNORE_BRANCH_ISA:
14688 mips_ignore_branch_isa = TRUE;
14689 break;
14690
14691 case OPTION_NO_IGNORE_BRANCH_ISA:
14692 mips_ignore_branch_isa = FALSE;
14693 break;
14694
833794fc 14695 case OPTION_INSN32:
919731af 14696 file_mips_opts.insn32 = TRUE;
833794fc
MR
14697 break;
14698
14699 case OPTION_NO_INSN32:
919731af 14700 file_mips_opts.insn32 = FALSE;
833794fc
MR
14701 break;
14702
aa6975fb
ILT
14703 case OPTION_MSHARED:
14704 mips_in_shared = TRUE;
14705 break;
14706
14707 case OPTION_MNO_SHARED:
14708 mips_in_shared = FALSE;
14709 break;
14710
aed1a261 14711 case OPTION_MSYM32:
919731af 14712 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14713 break;
14714
14715 case OPTION_MNO_SYM32:
919731af 14716 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14717 break;
14718
252b5132
RH
14719 /* When generating ELF code, we permit -KPIC and -call_shared to
14720 select SVR4_PIC, and -non_shared to select no PIC. This is
14721 intended to be compatible with Irix 5. */
14722 case OPTION_CALL_SHARED:
252b5132 14723 mips_pic = SVR4_PIC;
143d77c5 14724 mips_abicalls = TRUE;
252b5132
RH
14725 break;
14726
861fb55a 14727 case OPTION_CALL_NONPIC:
861fb55a
DJ
14728 mips_pic = NO_PIC;
14729 mips_abicalls = TRUE;
14730 break;
14731
252b5132 14732 case OPTION_NON_SHARED:
252b5132 14733 mips_pic = NO_PIC;
143d77c5 14734 mips_abicalls = FALSE;
252b5132
RH
14735 break;
14736
44075ae2
TS
14737 /* The -xgot option tells the assembler to use 32 bit offsets
14738 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14739 compatibility. */
14740 case OPTION_XGOT:
14741 mips_big_got = 1;
14742 break;
14743
14744 case 'G':
6caf9ef4
TS
14745 g_switch_value = atoi (arg);
14746 g_switch_seen = 1;
252b5132
RH
14747 break;
14748
34ba82a8
TS
14749 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14750 and -mabi=64. */
252b5132 14751 case OPTION_32:
f3ded42a 14752 mips_abi = O32_ABI;
252b5132
RH
14753 break;
14754
e013f690 14755 case OPTION_N32:
316f5878 14756 mips_abi = N32_ABI;
e013f690 14757 break;
252b5132 14758
e013f690 14759 case OPTION_64:
316f5878 14760 mips_abi = N64_ABI;
f43abd2b 14761 if (!support_64bit_objects())
1661c76c 14762 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14763 break;
14764
c97ef257 14765 case OPTION_GP32:
bad1aba3 14766 file_mips_opts.gp = 32;
c97ef257
AH
14767 break;
14768
14769 case OPTION_GP64:
bad1aba3 14770 file_mips_opts.gp = 64;
c97ef257 14771 break;
252b5132 14772
ca4e0257 14773 case OPTION_FP32:
0b35dfee 14774 file_mips_opts.fp = 32;
316f5878
RS
14775 break;
14776
351cdf24
MF
14777 case OPTION_FPXX:
14778 file_mips_opts.fp = 0;
14779 break;
14780
316f5878 14781 case OPTION_FP64:
0b35dfee 14782 file_mips_opts.fp = 64;
ca4e0257
RS
14783 break;
14784
351cdf24
MF
14785 case OPTION_ODD_SPREG:
14786 file_mips_opts.oddspreg = 1;
14787 break;
14788
14789 case OPTION_NO_ODD_SPREG:
14790 file_mips_opts.oddspreg = 0;
14791 break;
14792
037b32b9 14793 case OPTION_SINGLE_FLOAT:
0b35dfee 14794 file_mips_opts.single_float = 1;
037b32b9
AN
14795 break;
14796
14797 case OPTION_DOUBLE_FLOAT:
0b35dfee 14798 file_mips_opts.single_float = 0;
037b32b9
AN
14799 break;
14800
14801 case OPTION_SOFT_FLOAT:
0b35dfee 14802 file_mips_opts.soft_float = 1;
037b32b9
AN
14803 break;
14804
14805 case OPTION_HARD_FLOAT:
0b35dfee 14806 file_mips_opts.soft_float = 0;
037b32b9
AN
14807 break;
14808
252b5132 14809 case OPTION_MABI:
e013f690 14810 if (strcmp (arg, "32") == 0)
316f5878 14811 mips_abi = O32_ABI;
e013f690 14812 else if (strcmp (arg, "o64") == 0)
316f5878 14813 mips_abi = O64_ABI;
e013f690 14814 else if (strcmp (arg, "n32") == 0)
316f5878 14815 mips_abi = N32_ABI;
e013f690
TS
14816 else if (strcmp (arg, "64") == 0)
14817 {
316f5878 14818 mips_abi = N64_ABI;
e013f690 14819 if (! support_64bit_objects())
1661c76c 14820 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14821 "format"));
14822 }
14823 else if (strcmp (arg, "eabi") == 0)
316f5878 14824 mips_abi = EABI_ABI;
e013f690 14825 else
da0e507f
TS
14826 {
14827 as_fatal (_("invalid abi -mabi=%s"), arg);
14828 return 0;
14829 }
252b5132
RH
14830 break;
14831
6b76fefe 14832 case OPTION_M7000_HILO_FIX:
b34976b6 14833 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14834 break;
14835
9ee72ff1 14836 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14837 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14838 break;
14839
ecb4347a 14840 case OPTION_MDEBUG:
b34976b6 14841 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14842 break;
14843
14844 case OPTION_NO_MDEBUG:
b34976b6 14845 mips_flag_mdebug = FALSE;
ecb4347a 14846 break;
dcd410fe
RO
14847
14848 case OPTION_PDR:
14849 mips_flag_pdr = TRUE;
14850 break;
14851
14852 case OPTION_NO_PDR:
14853 mips_flag_pdr = FALSE;
14854 break;
0a44bf69
RS
14855
14856 case OPTION_MVXWORKS_PIC:
14857 mips_pic = VXWORKS_PIC;
14858 break;
ecb4347a 14859
ba92f887
MR
14860 case OPTION_NAN:
14861 if (strcmp (arg, "2008") == 0)
7361da2c 14862 mips_nan2008 = 1;
ba92f887 14863 else if (strcmp (arg, "legacy") == 0)
7361da2c 14864 mips_nan2008 = 0;
ba92f887
MR
14865 else
14866 {
1661c76c 14867 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14868 return 0;
14869 }
14870 break;
14871
252b5132
RH
14872 default:
14873 return 0;
14874 }
14875
c67a084a
NC
14876 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14877
252b5132
RH
14878 return 1;
14879}
316f5878 14880\f
919731af 14881/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14882
316f5878 14883static void
17a2f251 14884mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14885{
14886 if (info != 0)
fef14a42 14887 mips_tune = info->cpu;
316f5878 14888}
80cc45a5 14889
34ba82a8 14890
252b5132 14891void
17a2f251 14892mips_after_parse_args (void)
e9670677 14893{
fef14a42
TS
14894 const struct mips_cpu_info *arch_info = 0;
14895 const struct mips_cpu_info *tune_info = 0;
14896
e9670677 14897 /* GP relative stuff not working for PE */
6caf9ef4 14898 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14899 {
6caf9ef4 14900 if (g_switch_seen && g_switch_value != 0)
1661c76c 14901 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14902 g_switch_value = 0;
14903 }
14904
cac012d6
AO
14905 if (mips_abi == NO_ABI)
14906 mips_abi = MIPS_DEFAULT_ABI;
14907
919731af 14908 /* The following code determines the architecture.
22923709
RS
14909 Similar code was added to GCC 3.3 (see override_options() in
14910 config/mips/mips.c). The GAS and GCC code should be kept in sync
14911 as much as possible. */
e9670677 14912
316f5878 14913 if (mips_arch_string != 0)
fef14a42 14914 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14915
0b35dfee 14916 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14917 {
0b35dfee 14918 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14919 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14920 the -march selection (if any). */
fef14a42 14921 if (arch_info != 0)
e9670677 14922 {
316f5878
RS
14923 /* -march takes precedence over -mipsN, since it is more descriptive.
14924 There's no harm in specifying both as long as the ISA levels
14925 are the same. */
0b35dfee 14926 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14927 as_bad (_("-%s conflicts with the other architecture options,"
14928 " which imply -%s"),
0b35dfee 14929 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14930 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14931 }
316f5878 14932 else
0b35dfee 14933 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14934 }
14935
fef14a42 14936 if (arch_info == 0)
95bfe26e
MF
14937 {
14938 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14939 gas_assert (arch_info);
14940 }
e9670677 14941
fef14a42 14942 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14943 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14944 arch_info->name);
14945
919731af 14946 file_mips_opts.arch = arch_info->cpu;
14947 file_mips_opts.isa = arch_info->isa;
14948
14949 /* Set up initial mips_opts state. */
14950 mips_opts = file_mips_opts;
14951
14952 /* The register size inference code is now placed in
14953 file_mips_check_options. */
fef14a42 14954
0b35dfee 14955 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14956 processor. */
fef14a42
TS
14957 if (mips_tune_string != 0)
14958 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14959
fef14a42
TS
14960 if (tune_info == 0)
14961 mips_set_tune (arch_info);
14962 else
14963 mips_set_tune (tune_info);
e9670677 14964
ecb4347a 14965 if (mips_flag_mdebug < 0)
e8044f35 14966 mips_flag_mdebug = 0;
e9670677
MR
14967}
14968\f
14969void
17a2f251 14970mips_init_after_args (void)
252b5132
RH
14971{
14972 /* initialize opcodes */
14973 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14974 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14975}
14976
14977long
17a2f251 14978md_pcrel_from (fixS *fixP)
252b5132 14979{
a7ebbfdf
TS
14980 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14981 switch (fixP->fx_r_type)
14982 {
df58fc94
RS
14983 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14984 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14985 /* Return the address of the delay slot. */
14986 return addr + 2;
14987
14988 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14989 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 14990 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 14991 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
14992 case BFD_RELOC_MIPS_21_PCREL_S2:
14993 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
14994 case BFD_RELOC_MIPS_JMP:
14995 /* Return the address of the delay slot. */
14996 return addr + 4;
df58fc94 14997
51f6035b
MR
14998 case BFD_RELOC_MIPS_18_PCREL_S3:
14999 /* Return the aligned address of the doubleword containing
15000 the instruction. */
15001 return addr & ~7;
15002
a7ebbfdf
TS
15003 default:
15004 return addr;
15005 }
252b5132
RH
15006}
15007
252b5132
RH
15008/* This is called before the symbol table is processed. In order to
15009 work with gcc when using mips-tfile, we must keep all local labels.
15010 However, in other cases, we want to discard them. If we were
15011 called with -g, but we didn't see any debugging information, it may
15012 mean that gcc is smuggling debugging information through to
15013 mips-tfile, in which case we must generate all local labels. */
15014
15015void
17a2f251 15016mips_frob_file_before_adjust (void)
252b5132
RH
15017{
15018#ifndef NO_ECOFF_DEBUGGING
15019 if (ECOFF_DEBUGGING
15020 && mips_debug != 0
15021 && ! ecoff_debugging_seen)
15022 flag_keep_locals = 1;
15023#endif
15024}
15025
3b91255e 15026/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15027 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15028 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15029 relocation operators.
15030
15031 For our purposes, a %lo() expression matches a %got() or %hi()
15032 expression if:
15033
15034 (a) it refers to the same symbol; and
15035 (b) the offset applied in the %lo() expression is no lower than
15036 the offset applied in the %got() or %hi().
15037
15038 (b) allows us to cope with code like:
15039
15040 lui $4,%hi(foo)
15041 lh $4,%lo(foo+2)($4)
15042
15043 ...which is legal on RELA targets, and has a well-defined behaviour
15044 if the user knows that adding 2 to "foo" will not induce a carry to
15045 the high 16 bits.
15046
15047 When several %lo()s match a particular %got() or %hi(), we use the
15048 following rules to distinguish them:
15049
15050 (1) %lo()s with smaller offsets are a better match than %lo()s with
15051 higher offsets.
15052
15053 (2) %lo()s with no matching %got() or %hi() are better than those
15054 that already have a matching %got() or %hi().
15055
15056 (3) later %lo()s are better than earlier %lo()s.
15057
15058 These rules are applied in order.
15059
15060 (1) means, among other things, that %lo()s with identical offsets are
15061 chosen if they exist.
15062
15063 (2) means that we won't associate several high-part relocations with
15064 the same low-part relocation unless there's no alternative. Having
15065 several high parts for the same low part is a GNU extension; this rule
15066 allows careful users to avoid it.
15067
15068 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15069 with the last high-part relocation being at the front of the list.
15070 It therefore makes sense to choose the last matching low-part
15071 relocation, all other things being equal. It's also easier
15072 to code that way. */
252b5132
RH
15073
15074void
17a2f251 15075mips_frob_file (void)
252b5132
RH
15076{
15077 struct mips_hi_fixup *l;
35903be0 15078 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15079
15080 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15081 {
15082 segment_info_type *seginfo;
3b91255e
RS
15083 bfd_boolean matched_lo_p;
15084 fixS **hi_pos, **lo_pos, **pos;
252b5132 15085
9c2799c2 15086 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15087
5919d012 15088 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15089 there isn't supposed to be a matching LO. Ignore %gots against
15090 constants; we'll report an error for those later. */
738e5348 15091 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15092 && !(l->fixp->fx_addsy
9e009953 15093 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15094 continue;
15095
15096 /* Check quickly whether the next fixup happens to be a matching %lo. */
15097 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15098 continue;
15099
252b5132 15100 seginfo = seg_info (l->seg);
252b5132 15101
3b91255e
RS
15102 /* Set HI_POS to the position of this relocation in the chain.
15103 Set LO_POS to the position of the chosen low-part relocation.
15104 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15105 relocation that matches an immediately-preceding high-part
15106 relocation. */
15107 hi_pos = NULL;
15108 lo_pos = NULL;
15109 matched_lo_p = FALSE;
738e5348 15110 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15111
3b91255e
RS
15112 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15113 {
15114 if (*pos == l->fixp)
15115 hi_pos = pos;
15116
35903be0 15117 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15118 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15119 && (*pos)->fx_offset >= l->fixp->fx_offset
15120 && (lo_pos == NULL
15121 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15122 || (!matched_lo_p
15123 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15124 lo_pos = pos;
15125
15126 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15127 && fixup_has_matching_lo_p (*pos));
15128 }
15129
15130 /* If we found a match, remove the high-part relocation from its
15131 current position and insert it before the low-part relocation.
15132 Make the offsets match so that fixup_has_matching_lo_p()
15133 will return true.
15134
15135 We don't warn about unmatched high-part relocations since some
15136 versions of gcc have been known to emit dead "lui ...%hi(...)"
15137 instructions. */
15138 if (lo_pos != NULL)
15139 {
15140 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15141 if (l->fixp->fx_next != *lo_pos)
252b5132 15142 {
3b91255e
RS
15143 *hi_pos = l->fixp->fx_next;
15144 l->fixp->fx_next = *lo_pos;
15145 *lo_pos = l->fixp;
252b5132 15146 }
252b5132
RH
15147 }
15148 }
15149}
15150
252b5132 15151int
17a2f251 15152mips_force_relocation (fixS *fixp)
252b5132 15153{
ae6063d4 15154 if (generic_force_reloc (fixp))
252b5132
RH
15155 return 1;
15156
df58fc94
RS
15157 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15158 so that the linker relaxation can update targets. */
15159 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15160 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15161 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15162 return 1;
15163
5caa2b07
MR
15164 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15165 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15166 microMIPS symbols so that we can do cross-mode branch diagnostics
15167 and BAL to JALX conversion by the linker. */
15168 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15169 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15170 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15171 && fixp->fx_addsy
15172 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15173 return 1;
15174
7361da2c 15175 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15176 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15177 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15178 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15179 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15180 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15181 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15182 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15183 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15184 return 1;
15185
3e722fb5 15186 return 0;
252b5132
RH
15187}
15188
b416ba9b
MR
15189/* Implement TC_FORCE_RELOCATION_ABS. */
15190
15191bfd_boolean
15192mips_force_relocation_abs (fixS *fixp)
15193{
15194 if (generic_force_reloc (fixp))
15195 return TRUE;
15196
15197 /* These relocations do not have enough bits in the in-place addend
15198 to hold an arbitrary absolute section's offset. */
15199 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15200 return TRUE;
15201
15202 return FALSE;
15203}
15204
b886a2ab
RS
15205/* Read the instruction associated with RELOC from BUF. */
15206
15207static unsigned int
15208read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15209{
15210 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15211 return read_compressed_insn (buf, 4);
15212 else
15213 return read_insn (buf);
15214}
15215
15216/* Write instruction INSN to BUF, given that it has been relocated
15217 by RELOC. */
15218
15219static void
15220write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15221 unsigned long insn)
15222{
15223 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15224 write_compressed_insn (buf, insn, 4);
15225 else
15226 write_insn (buf, insn);
15227}
15228
9d862524
MR
15229/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15230 to a symbol in another ISA mode, which cannot be converted to JALX. */
15231
15232static bfd_boolean
15233fix_bad_cross_mode_jump_p (fixS *fixP)
15234{
15235 unsigned long opcode;
15236 int other;
15237 char *buf;
15238
15239 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15240 return FALSE;
15241
15242 other = S_GET_OTHER (fixP->fx_addsy);
15243 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15244 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15245 switch (fixP->fx_r_type)
15246 {
15247 case BFD_RELOC_MIPS_JMP:
15248 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15249 case BFD_RELOC_MICROMIPS_JMP:
15250 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15251 default:
15252 return FALSE;
15253 }
15254}
15255
15256/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15257 jump to a symbol in the same ISA mode. */
15258
15259static bfd_boolean
15260fix_bad_same_mode_jalx_p (fixS *fixP)
15261{
15262 unsigned long opcode;
15263 int other;
15264 char *buf;
15265
15266 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15267 return FALSE;
15268
15269 other = S_GET_OTHER (fixP->fx_addsy);
15270 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15271 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15272 switch (fixP->fx_r_type)
15273 {
15274 case BFD_RELOC_MIPS_JMP:
15275 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15276 case BFD_RELOC_MIPS16_JMP:
15277 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15278 case BFD_RELOC_MICROMIPS_JMP:
15279 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15280 default:
15281 return FALSE;
15282 }
15283}
15284
15285/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15286 to a symbol whose value plus addend is not aligned according to the
15287 ultimate (after linker relaxation) jump instruction's immediate field
15288 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15289 regular MIPS code, to (1 << 2). */
15290
15291static bfd_boolean
15292fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15293{
15294 bfd_boolean micro_to_mips_p;
15295 valueT val;
15296 int other;
15297
15298 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15299 return FALSE;
15300
15301 other = S_GET_OTHER (fixP->fx_addsy);
15302 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15303 val += fixP->fx_offset;
15304 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15305 && !ELF_ST_IS_MICROMIPS (other));
15306 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15307 != ELF_ST_IS_COMPRESSED (other));
15308}
15309
15310/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15311 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15312 symbols check the ISA bit instead.
15313
15314 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15315 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15316 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15317 may be converted to JALX by the linker. */
9d862524
MR
15318
15319static bfd_boolean
15320fix_bad_cross_mode_branch_p (fixS *fixP)
15321{
15322 bfd_boolean absolute_p;
15323 unsigned long opcode;
15324 asection *symsec;
15325 valueT val;
15326 int other;
15327 char *buf;
15328
8b10b0b3
MR
15329 if (mips_ignore_branch_isa)
15330 return FALSE;
15331
9d862524
MR
15332 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15333 return FALSE;
15334
15335 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15336 absolute_p = bfd_is_abs_section (symsec);
15337
15338 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15339 other = S_GET_OTHER (fixP->fx_addsy);
15340
15341 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15342 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15343 switch (fixP->fx_r_type)
15344 {
15345 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15346 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15347 && opcode != 0x0411);
15348 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15349 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15350 && opcode != 0x4060);
9d862524
MR
15351 case BFD_RELOC_MIPS_21_PCREL_S2:
15352 case BFD_RELOC_MIPS_26_PCREL_S2:
15353 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15354 case BFD_RELOC_MIPS16_16_PCREL_S1:
15355 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15356 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15357 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15358 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15359 default:
15360 abort ();
15361 }
15362}
15363
15364/* Return TRUE if the symbol plus addend associated with a regular MIPS
15365 branch instruction pointed to by FIXP is not aligned according to the
15366 branch instruction's immediate field requirement. We need the addend
15367 to preserve the ISA bit and also the sum must not have bit 2 set. We
15368 must explicitly OR in the ISA bit from symbol annotation as the bit
15369 won't be set in the symbol's value then. */
15370
15371static bfd_boolean
15372fix_bad_misaligned_branch_p (fixS *fixP)
15373{
15374 bfd_boolean absolute_p;
15375 asection *symsec;
15376 valueT isa_bit;
15377 valueT val;
15378 valueT off;
15379 int other;
15380
15381 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15382 return FALSE;
15383
15384 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15385 absolute_p = bfd_is_abs_section (symsec);
15386
15387 val = S_GET_VALUE (fixP->fx_addsy);
15388 other = S_GET_OTHER (fixP->fx_addsy);
15389 off = fixP->fx_offset;
15390
15391 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15392 val |= ELF_ST_IS_COMPRESSED (other);
15393 val += off;
15394 return (val & 0x3) != isa_bit;
15395}
15396
15397/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15398 and its calculated value VAL. */
15399
15400static void
15401fix_validate_branch (fixS *fixP, valueT val)
15402{
15403 if (fixP->fx_done && (val & 0x3) != 0)
15404 as_bad_where (fixP->fx_file, fixP->fx_line,
15405 _("branch to misaligned address (0x%lx)"),
15406 (long) (val + md_pcrel_from (fixP)));
15407 else if (fix_bad_cross_mode_branch_p (fixP))
15408 as_bad_where (fixP->fx_file, fixP->fx_line,
15409 _("branch to a symbol in another ISA mode"));
15410 else if (fix_bad_misaligned_branch_p (fixP))
15411 as_bad_where (fixP->fx_file, fixP->fx_line,
15412 _("branch to misaligned address (0x%lx)"),
15413 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15414 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15415 as_bad_where (fixP->fx_file, fixP->fx_line,
15416 _("cannot encode misaligned addend "
15417 "in the relocatable field (0x%lx)"),
15418 (long) fixP->fx_offset);
15419}
15420
252b5132
RH
15421/* Apply a fixup to the object file. */
15422
94f592af 15423void
55cf6793 15424md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15425{
4d68580a 15426 char *buf;
b886a2ab 15427 unsigned long insn;
a7ebbfdf 15428 reloc_howto_type *howto;
252b5132 15429
d56a8dda
RS
15430 if (fixP->fx_pcrel)
15431 switch (fixP->fx_r_type)
15432 {
15433 case BFD_RELOC_16_PCREL_S2:
c9775dde 15434 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15435 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15436 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15437 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15438 case BFD_RELOC_32_PCREL:
7361da2c
AB
15439 case BFD_RELOC_MIPS_21_PCREL_S2:
15440 case BFD_RELOC_MIPS_26_PCREL_S2:
15441 case BFD_RELOC_MIPS_18_PCREL_S3:
15442 case BFD_RELOC_MIPS_19_PCREL_S2:
15443 case BFD_RELOC_HI16_S_PCREL:
15444 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15445 break;
15446
15447 case BFD_RELOC_32:
15448 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15449 break;
15450
15451 default:
15452 as_bad_where (fixP->fx_file, fixP->fx_line,
15453 _("PC-relative reference to a different section"));
15454 break;
15455 }
15456
15457 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15458 that have no MIPS ELF equivalent. */
15459 if (fixP->fx_r_type != BFD_RELOC_8)
15460 {
15461 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15462 if (!howto)
15463 return;
15464 }
65551fa4 15465
df58fc94
RS
15466 gas_assert (fixP->fx_size == 2
15467 || fixP->fx_size == 4
d56a8dda 15468 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15469 || fixP->fx_r_type == BFD_RELOC_16
15470 || fixP->fx_r_type == BFD_RELOC_64
15471 || fixP->fx_r_type == BFD_RELOC_CTOR
15472 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15473 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15474 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15475 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15476 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15477 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15478
4d68580a 15479 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15480
b1dca8ee
RS
15481 /* Don't treat parts of a composite relocation as done. There are two
15482 reasons for this:
15483
15484 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15485 should nevertheless be emitted if the first part is.
15486
15487 (2) In normal usage, composite relocations are never assembly-time
15488 constants. The easiest way of dealing with the pathological
15489 exceptions is to generate a relocation against STN_UNDEF and
15490 leave everything up to the linker. */
3994f87e 15491 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15492 fixP->fx_done = 1;
15493
15494 switch (fixP->fx_r_type)
15495 {
3f98094e
DJ
15496 case BFD_RELOC_MIPS_TLS_GD:
15497 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15498 case BFD_RELOC_MIPS_TLS_DTPREL32:
15499 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15500 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15501 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15502 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15503 case BFD_RELOC_MIPS_TLS_TPREL32:
15504 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15505 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15506 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15507 case BFD_RELOC_MICROMIPS_TLS_GD:
15508 case BFD_RELOC_MICROMIPS_TLS_LDM:
15509 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15510 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15511 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15512 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15513 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15514 case BFD_RELOC_MIPS16_TLS_GD:
15515 case BFD_RELOC_MIPS16_TLS_LDM:
15516 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15517 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15518 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15519 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15520 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15521 if (fixP->fx_addsy)
15522 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15523 else
15524 as_bad_where (fixP->fx_file, fixP->fx_line,
15525 _("TLS relocation against a constant"));
15526 break;
3f98094e 15527
252b5132 15528 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15529 case BFD_RELOC_MIPS16_JMP:
15530 case BFD_RELOC_MICROMIPS_JMP:
15531 {
15532 int shift;
15533
15534 gas_assert (!fixP->fx_done);
15535
15536 /* Shift is 2, unusually, for microMIPS JALX. */
15537 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15538 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15539 shift = 1;
15540 else
15541 shift = 2;
15542
15543 if (fix_bad_cross_mode_jump_p (fixP))
15544 as_bad_where (fixP->fx_file, fixP->fx_line,
15545 _("jump to a symbol in another ISA mode"));
15546 else if (fix_bad_same_mode_jalx_p (fixP))
15547 as_bad_where (fixP->fx_file, fixP->fx_line,
15548 _("JALX to a symbol in the same ISA mode"));
15549 else if (fix_bad_misaligned_jump_p (fixP, shift))
15550 as_bad_where (fixP->fx_file, fixP->fx_line,
15551 _("jump to misaligned address (0x%lx)"),
15552 (long) (S_GET_VALUE (fixP->fx_addsy)
15553 + fixP->fx_offset));
15554 else if (HAVE_IN_PLACE_ADDENDS
15555 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15556 as_bad_where (fixP->fx_file, fixP->fx_line,
15557 _("cannot encode misaligned addend "
15558 "in the relocatable field (0x%lx)"),
15559 (long) fixP->fx_offset);
15560 }
15561 /* Fall through. */
15562
e369bcce
TS
15563 case BFD_RELOC_MIPS_SHIFT5:
15564 case BFD_RELOC_MIPS_SHIFT6:
15565 case BFD_RELOC_MIPS_GOT_DISP:
15566 case BFD_RELOC_MIPS_GOT_PAGE:
15567 case BFD_RELOC_MIPS_GOT_OFST:
15568 case BFD_RELOC_MIPS_SUB:
15569 case BFD_RELOC_MIPS_INSERT_A:
15570 case BFD_RELOC_MIPS_INSERT_B:
15571 case BFD_RELOC_MIPS_DELETE:
15572 case BFD_RELOC_MIPS_HIGHEST:
15573 case BFD_RELOC_MIPS_HIGHER:
15574 case BFD_RELOC_MIPS_SCN_DISP:
15575 case BFD_RELOC_MIPS_REL16:
15576 case BFD_RELOC_MIPS_RELGOT:
15577 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15578 case BFD_RELOC_HI16:
15579 case BFD_RELOC_HI16_S:
b886a2ab 15580 case BFD_RELOC_LO16:
cdf6fd85 15581 case BFD_RELOC_GPREL16:
252b5132
RH
15582 case BFD_RELOC_MIPS_LITERAL:
15583 case BFD_RELOC_MIPS_CALL16:
15584 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15585 case BFD_RELOC_GPREL32:
252b5132
RH
15586 case BFD_RELOC_MIPS_GOT_HI16:
15587 case BFD_RELOC_MIPS_GOT_LO16:
15588 case BFD_RELOC_MIPS_CALL_HI16:
15589 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15590 case BFD_RELOC_HI16_S_PCREL:
15591 case BFD_RELOC_LO16_PCREL:
252b5132 15592 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15593 case BFD_RELOC_MIPS16_GOT16:
15594 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15595 case BFD_RELOC_MIPS16_HI16:
15596 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15597 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15598 case BFD_RELOC_MICROMIPS_GOT_DISP:
15599 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15600 case BFD_RELOC_MICROMIPS_GOT_OFST:
15601 case BFD_RELOC_MICROMIPS_SUB:
15602 case BFD_RELOC_MICROMIPS_HIGHEST:
15603 case BFD_RELOC_MICROMIPS_HIGHER:
15604 case BFD_RELOC_MICROMIPS_SCN_DISP:
15605 case BFD_RELOC_MICROMIPS_JALR:
15606 case BFD_RELOC_MICROMIPS_HI16:
15607 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15608 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15609 case BFD_RELOC_MICROMIPS_GPREL16:
15610 case BFD_RELOC_MICROMIPS_LITERAL:
15611 case BFD_RELOC_MICROMIPS_CALL16:
15612 case BFD_RELOC_MICROMIPS_GOT16:
15613 case BFD_RELOC_MICROMIPS_GOT_HI16:
15614 case BFD_RELOC_MICROMIPS_GOT_LO16:
15615 case BFD_RELOC_MICROMIPS_CALL_HI16:
15616 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15617 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15618 if (fixP->fx_done)
15619 {
15620 offsetT value;
15621
15622 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15623 {
15624 insn = read_reloc_insn (buf, fixP->fx_r_type);
15625 if (mips16_reloc_p (fixP->fx_r_type))
15626 insn |= mips16_immed_extend (value, 16);
15627 else
15628 insn |= (value & 0xffff);
15629 write_reloc_insn (buf, fixP->fx_r_type, insn);
15630 }
15631 else
15632 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15633 _("unsupported constant in relocation"));
b886a2ab 15634 }
252b5132
RH
15635 break;
15636
252b5132
RH
15637 case BFD_RELOC_64:
15638 /* This is handled like BFD_RELOC_32, but we output a sign
15639 extended value if we are only 32 bits. */
3e722fb5 15640 if (fixP->fx_done)
252b5132
RH
15641 {
15642 if (8 <= sizeof (valueT))
4d68580a 15643 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15644 else
15645 {
a7ebbfdf 15646 valueT hiv;
252b5132 15647
a7ebbfdf 15648 if ((*valP & 0x80000000) != 0)
252b5132
RH
15649 hiv = 0xffffffff;
15650 else
15651 hiv = 0;
4d68580a
RS
15652 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15653 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15654 }
15655 }
15656 break;
15657
056350c6 15658 case BFD_RELOC_RVA:
252b5132 15659 case BFD_RELOC_32:
b47468a6 15660 case BFD_RELOC_32_PCREL:
252b5132 15661 case BFD_RELOC_16:
d56a8dda 15662 case BFD_RELOC_8:
252b5132 15663 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15664 value now. This can happen if we have a .word which is not
15665 resolved when it appears but is later defined. */
252b5132 15666 if (fixP->fx_done)
4d68580a 15667 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15668 break;
15669
7361da2c 15670 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15671 fix_validate_branch (fixP, *valP);
41947d9e
MR
15672 if (!fixP->fx_done)
15673 break;
15674
15675 if (*valP + 0x400000 <= 0x7fffff)
15676 {
15677 insn = read_insn (buf);
15678 insn |= (*valP >> 2) & 0x1fffff;
15679 write_insn (buf, insn);
15680 }
15681 else
15682 as_bad_where (fixP->fx_file, fixP->fx_line,
15683 _("branch out of range"));
15684 break;
15685
7361da2c 15686 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15687 fix_validate_branch (fixP, *valP);
41947d9e
MR
15688 if (!fixP->fx_done)
15689 break;
7361da2c 15690
41947d9e
MR
15691 if (*valP + 0x8000000 <= 0xfffffff)
15692 {
15693 insn = read_insn (buf);
15694 insn |= (*valP >> 2) & 0x3ffffff;
15695 write_insn (buf, insn);
15696 }
15697 else
15698 as_bad_where (fixP->fx_file, fixP->fx_line,
15699 _("branch out of range"));
7361da2c
AB
15700 break;
15701
15702 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15703 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15704 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15705 _("PC-relative access using misaligned symbol (%lx)"),
15706 (long) S_GET_VALUE (fixP->fx_addsy));
15707 if ((fixP->fx_offset & 0x7) != 0)
15708 as_bad_where (fixP->fx_file, fixP->fx_line,
15709 _("PC-relative access using misaligned offset (%lx)"),
15710 (long) fixP->fx_offset);
41947d9e
MR
15711 if (!fixP->fx_done)
15712 break;
7361da2c 15713
41947d9e
MR
15714 if (*valP + 0x100000 <= 0x1fffff)
15715 {
15716 insn = read_insn (buf);
15717 insn |= (*valP >> 3) & 0x3ffff;
15718 write_insn (buf, insn);
15719 }
15720 else
15721 as_bad_where (fixP->fx_file, fixP->fx_line,
15722 _("PC-relative access out of range"));
7361da2c
AB
15723 break;
15724
15725 case BFD_RELOC_MIPS_19_PCREL_S2:
15726 if ((*valP & 0x3) != 0)
15727 as_bad_where (fixP->fx_file, fixP->fx_line,
15728 _("PC-relative access to misaligned address (%lx)"),
717ba204 15729 (long) *valP);
41947d9e
MR
15730 if (!fixP->fx_done)
15731 break;
7361da2c 15732
41947d9e
MR
15733 if (*valP + 0x100000 <= 0x1fffff)
15734 {
15735 insn = read_insn (buf);
15736 insn |= (*valP >> 2) & 0x7ffff;
15737 write_insn (buf, insn);
15738 }
15739 else
15740 as_bad_where (fixP->fx_file, fixP->fx_line,
15741 _("PC-relative access out of range"));
7361da2c
AB
15742 break;
15743
252b5132 15744 case BFD_RELOC_16_PCREL_S2:
9d862524 15745 fix_validate_branch (fixP, *valP);
cb56d3d3 15746
54f4ddb3
TS
15747 /* We need to save the bits in the instruction since fixup_segment()
15748 might be deleting the relocation entry (i.e., a branch within
15749 the current segment). */
a7ebbfdf 15750 if (! fixP->fx_done)
bb2d6cd7 15751 break;
252b5132 15752
54f4ddb3 15753 /* Update old instruction data. */
4d68580a 15754 insn = read_insn (buf);
252b5132 15755
a7ebbfdf
TS
15756 if (*valP + 0x20000 <= 0x3ffff)
15757 {
15758 insn |= (*valP >> 2) & 0xffff;
4d68580a 15759 write_insn (buf, insn);
a7ebbfdf 15760 }
ce8ad872 15761 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15762 && fixP->fx_done
15763 && fixP->fx_frag->fr_address >= text_section->vma
15764 && (fixP->fx_frag->fr_address
587aac4e 15765 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15766 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15767 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15768 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15769 {
15770 /* The branch offset is too large. If this is an
15771 unconditional branch, and we are not generating PIC code,
15772 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15773 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15774 insn = 0x0c000000; /* jal */
252b5132 15775 else
a7ebbfdf
TS
15776 insn = 0x08000000; /* j */
15777 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15778 fixP->fx_done = 0;
15779 fixP->fx_addsy = section_symbol (text_section);
15780 *valP += md_pcrel_from (fixP);
4d68580a 15781 write_insn (buf, insn);
a7ebbfdf
TS
15782 }
15783 else
15784 {
15785 /* If we got here, we have branch-relaxation disabled,
15786 and there's nothing we can do to fix this instruction
15787 without turning it into a longer sequence. */
15788 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15789 _("branch out of range"));
252b5132 15790 }
252b5132
RH
15791 break;
15792
c9775dde 15793 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15794 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15795 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15796 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15797 gas_assert (!fixP->fx_done);
9d862524
MR
15798 if (fix_bad_cross_mode_branch_p (fixP))
15799 as_bad_where (fixP->fx_file, fixP->fx_line,
15800 _("branch to a symbol in another ISA mode"));
15801 else if (fixP->fx_addsy
15802 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15803 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15804 && (fixP->fx_offset & 0x1) != 0)
15805 as_bad_where (fixP->fx_file, fixP->fx_line,
15806 _("branch to misaligned address (0x%lx)"),
15807 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15808 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15809 as_bad_where (fixP->fx_file, fixP->fx_line,
15810 _("cannot encode misaligned addend "
15811 "in the relocatable field (0x%lx)"),
15812 (long) fixP->fx_offset);
df58fc94
RS
15813 break;
15814
252b5132
RH
15815 case BFD_RELOC_VTABLE_INHERIT:
15816 fixP->fx_done = 0;
15817 if (fixP->fx_addsy
15818 && !S_IS_DEFINED (fixP->fx_addsy)
15819 && !S_IS_WEAK (fixP->fx_addsy))
15820 S_SET_WEAK (fixP->fx_addsy);
15821 break;
15822
2f0c68f2 15823 case BFD_RELOC_NONE:
252b5132
RH
15824 case BFD_RELOC_VTABLE_ENTRY:
15825 fixP->fx_done = 0;
15826 break;
15827
15828 default:
b37df7c4 15829 abort ();
252b5132 15830 }
a7ebbfdf
TS
15831
15832 /* Remember value for tc_gen_reloc. */
15833 fixP->fx_addnumber = *valP;
252b5132
RH
15834}
15835
252b5132 15836static symbolS *
17a2f251 15837get_symbol (void)
252b5132
RH
15838{
15839 int c;
15840 char *name;
15841 symbolS *p;
15842
d02603dc 15843 c = get_symbol_name (&name);
252b5132 15844 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15845 (void) restore_line_pointer (c);
252b5132
RH
15846 return p;
15847}
15848
742a56fe
RS
15849/* Align the current frag to a given power of two. If a particular
15850 fill byte should be used, FILL points to an integer that contains
15851 that byte, otherwise FILL is null.
15852
462427c4
RS
15853 This function used to have the comment:
15854
15855 The MIPS assembler also automatically adjusts any preceding label.
15856
15857 The implementation therefore applied the adjustment to a maximum of
15858 one label. However, other label adjustments are applied to batches
15859 of labels, and adjusting just one caused problems when new labels
15860 were added for the sake of debugging or unwind information.
15861 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15862
15863static void
462427c4 15864mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15865{
7d10b47d 15866 mips_emit_delays ();
df58fc94 15867 mips_record_compressed_mode ();
742a56fe
RS
15868 if (fill == NULL && subseg_text_p (now_seg))
15869 frag_align_code (to, 0);
15870 else
15871 frag_align (to, fill ? *fill : 0, 0);
252b5132 15872 record_alignment (now_seg, to);
462427c4 15873 mips_move_labels (labels, FALSE);
252b5132
RH
15874}
15875
15876/* Align to a given power of two. .align 0 turns off the automatic
15877 alignment used by the data creating pseudo-ops. */
15878
15879static void
17a2f251 15880s_align (int x ATTRIBUTE_UNUSED)
252b5132 15881{
742a56fe 15882 int temp, fill_value, *fill_ptr;
49954fb4 15883 long max_alignment = 28;
252b5132 15884
54f4ddb3 15885 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15886 to the aligned address.
54f4ddb3 15887 o It's not documented but auto alignment is reinstated by
252b5132 15888 a .align pseudo instruction.
54f4ddb3 15889 o Note also that after auto alignment is turned off the mips assembler
252b5132 15890 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15891 We don't. */
252b5132
RH
15892
15893 temp = get_absolute_expression ();
15894 if (temp > max_alignment)
1661c76c 15895 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15896 else if (temp < 0)
15897 {
1661c76c 15898 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15899 temp = 0;
15900 }
15901 if (*input_line_pointer == ',')
15902 {
f9419b05 15903 ++input_line_pointer;
742a56fe
RS
15904 fill_value = get_absolute_expression ();
15905 fill_ptr = &fill_value;
252b5132
RH
15906 }
15907 else
742a56fe 15908 fill_ptr = 0;
252b5132
RH
15909 if (temp)
15910 {
a8dbcb85
TS
15911 segment_info_type *si = seg_info (now_seg);
15912 struct insn_label_list *l = si->label_list;
54f4ddb3 15913 /* Auto alignment should be switched on by next section change. */
252b5132 15914 auto_align = 1;
462427c4 15915 mips_align (temp, fill_ptr, l);
252b5132
RH
15916 }
15917 else
15918 {
15919 auto_align = 0;
15920 }
15921
15922 demand_empty_rest_of_line ();
15923}
15924
252b5132 15925static void
17a2f251 15926s_change_sec (int sec)
252b5132
RH
15927{
15928 segT seg;
15929
252b5132
RH
15930 /* The ELF backend needs to know that we are changing sections, so
15931 that .previous works correctly. We could do something like check
b6ff326e 15932 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15933 as it would not be appropriate to use it in the section changing
15934 functions in read.c, since obj-elf.c intercepts those. FIXME:
15935 This should be cleaner, somehow. */
f3ded42a 15936 obj_elf_section_change_hook ();
252b5132 15937
7d10b47d 15938 mips_emit_delays ();
6a32d874 15939
252b5132
RH
15940 switch (sec)
15941 {
15942 case 't':
15943 s_text (0);
15944 break;
15945 case 'd':
15946 s_data (0);
15947 break;
15948 case 'b':
15949 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15950 demand_empty_rest_of_line ();
15951 break;
15952
15953 case 'r':
4d0d148d
TS
15954 seg = subseg_new (RDATA_SECTION_NAME,
15955 (subsegT) get_absolute_expression ());
f3ded42a
RS
15956 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15957 | SEC_READONLY | SEC_RELOC
15958 | SEC_DATA));
15959 if (strncmp (TARGET_OS, "elf", 3) != 0)
15960 record_alignment (seg, 4);
4d0d148d 15961 demand_empty_rest_of_line ();
252b5132
RH
15962 break;
15963
15964 case 's':
4d0d148d 15965 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
15966 bfd_set_section_flags (stdoutput, seg,
15967 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15968 if (strncmp (TARGET_OS, "elf", 3) != 0)
15969 record_alignment (seg, 4);
4d0d148d
TS
15970 demand_empty_rest_of_line ();
15971 break;
998b3c36
MR
15972
15973 case 'B':
15974 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
15975 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15976 if (strncmp (TARGET_OS, "elf", 3) != 0)
15977 record_alignment (seg, 4);
998b3c36
MR
15978 demand_empty_rest_of_line ();
15979 break;
252b5132
RH
15980 }
15981
15982 auto_align = 1;
15983}
b34976b6 15984
cca86cc8 15985void
17a2f251 15986s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15987{
d02603dc 15988 char *saved_ilp;
cca86cc8 15989 char *section_name;
d02603dc 15990 char c, endc;
684022ea 15991 char next_c = 0;
cca86cc8
SC
15992 int section_type;
15993 int section_flag;
15994 int section_entry_size;
15995 int section_alignment;
b34976b6 15996
d02603dc
NC
15997 saved_ilp = input_line_pointer;
15998 endc = get_symbol_name (&section_name);
15999 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16000 if (c)
d02603dc 16001 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16002
4cf0dd0d
TS
16003 /* Do we have .section Name<,"flags">? */
16004 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16005 {
d02603dc
NC
16006 /* Just after name is now '\0'. */
16007 (void) restore_line_pointer (endc);
16008 input_line_pointer = saved_ilp;
cca86cc8
SC
16009 obj_elf_section (ignore);
16010 return;
16011 }
d02603dc
NC
16012
16013 section_name = xstrdup (section_name);
16014 c = restore_line_pointer (endc);
16015
cca86cc8
SC
16016 input_line_pointer++;
16017
16018 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16019 if (c == ',')
16020 section_type = get_absolute_expression ();
16021 else
16022 section_type = 0;
d02603dc 16023
cca86cc8
SC
16024 if (*input_line_pointer++ == ',')
16025 section_flag = get_absolute_expression ();
16026 else
16027 section_flag = 0;
d02603dc 16028
cca86cc8
SC
16029 if (*input_line_pointer++ == ',')
16030 section_entry_size = get_absolute_expression ();
16031 else
16032 section_entry_size = 0;
d02603dc 16033
cca86cc8
SC
16034 if (*input_line_pointer++ == ',')
16035 section_alignment = get_absolute_expression ();
16036 else
16037 section_alignment = 0;
d02603dc 16038
87975d2a
AM
16039 /* FIXME: really ignore? */
16040 (void) section_alignment;
cca86cc8 16041
8ab8a5c8
RS
16042 /* When using the generic form of .section (as implemented by obj-elf.c),
16043 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16044 traditionally had to fall back on the more common @progbits instead.
16045
16046 There's nothing really harmful in this, since bfd will correct
16047 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16048 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16049 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16050
16051 Even so, we shouldn't force users of the MIPS .section syntax to
16052 incorrectly label the sections as SHT_PROGBITS. The best compromise
16053 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16054 generic type-checking code. */
16055 if (section_type == SHT_MIPS_DWARF)
16056 section_type = SHT_PROGBITS;
16057
a91e1603 16058 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16059 section_entry_size, 0, 0, 0);
a816d1ed
AO
16060
16061 if (now_seg->name != section_name)
16062 free (section_name);
cca86cc8 16063}
252b5132
RH
16064
16065void
17a2f251 16066mips_enable_auto_align (void)
252b5132
RH
16067{
16068 auto_align = 1;
16069}
16070
16071static void
17a2f251 16072s_cons (int log_size)
252b5132 16073{
a8dbcb85
TS
16074 segment_info_type *si = seg_info (now_seg);
16075 struct insn_label_list *l = si->label_list;
252b5132 16076
7d10b47d 16077 mips_emit_delays ();
252b5132 16078 if (log_size > 0 && auto_align)
462427c4 16079 mips_align (log_size, 0, l);
252b5132 16080 cons (1 << log_size);
a1facbec 16081 mips_clear_insn_labels ();
252b5132
RH
16082}
16083
16084static void
17a2f251 16085s_float_cons (int type)
252b5132 16086{
a8dbcb85
TS
16087 segment_info_type *si = seg_info (now_seg);
16088 struct insn_label_list *l = si->label_list;
252b5132 16089
7d10b47d 16090 mips_emit_delays ();
252b5132
RH
16091
16092 if (auto_align)
49309057
ILT
16093 {
16094 if (type == 'd')
462427c4 16095 mips_align (3, 0, l);
49309057 16096 else
462427c4 16097 mips_align (2, 0, l);
49309057 16098 }
252b5132 16099
252b5132 16100 float_cons (type);
a1facbec 16101 mips_clear_insn_labels ();
252b5132
RH
16102}
16103
16104/* Handle .globl. We need to override it because on Irix 5 you are
16105 permitted to say
16106 .globl foo .text
16107 where foo is an undefined symbol, to mean that foo should be
16108 considered to be the address of a function. */
16109
16110static void
17a2f251 16111s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16112{
16113 char *name;
16114 int c;
16115 symbolS *symbolP;
16116 flagword flag;
16117
8a06b769 16118 do
252b5132 16119 {
d02603dc 16120 c = get_symbol_name (&name);
8a06b769
TS
16121 symbolP = symbol_find_or_make (name);
16122 S_SET_EXTERNAL (symbolP);
16123
252b5132 16124 *input_line_pointer = c;
d02603dc 16125 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16126
8a06b769
TS
16127 /* On Irix 5, every global symbol that is not explicitly labelled as
16128 being a function is apparently labelled as being an object. */
16129 flag = BSF_OBJECT;
252b5132 16130
8a06b769
TS
16131 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16132 && (*input_line_pointer != ','))
16133 {
16134 char *secname;
16135 asection *sec;
16136
d02603dc 16137 c = get_symbol_name (&secname);
8a06b769
TS
16138 sec = bfd_get_section_by_name (stdoutput, secname);
16139 if (sec == NULL)
16140 as_bad (_("%s: no such section"), secname);
d02603dc 16141 (void) restore_line_pointer (c);
8a06b769
TS
16142
16143 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16144 flag = BSF_FUNCTION;
16145 }
16146
16147 symbol_get_bfdsym (symbolP)->flags |= flag;
16148
16149 c = *input_line_pointer;
16150 if (c == ',')
16151 {
16152 input_line_pointer++;
16153 SKIP_WHITESPACE ();
16154 if (is_end_of_line[(unsigned char) *input_line_pointer])
16155 c = '\n';
16156 }
16157 }
16158 while (c == ',');
252b5132 16159
252b5132
RH
16160 demand_empty_rest_of_line ();
16161}
16162
16163static void
17a2f251 16164s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16165{
16166 char *opt;
16167 char c;
16168
d02603dc 16169 c = get_symbol_name (&opt);
252b5132
RH
16170
16171 if (*opt == 'O')
16172 {
16173 /* FIXME: What does this mean? */
16174 }
41a1578e 16175 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16176 {
16177 int i;
16178
16179 i = atoi (opt + 3);
668c5ebc
MR
16180 if (i != 0 && i != 2)
16181 as_bad (_(".option pic%d not supported"), i);
16182 else if (mips_pic == VXWORKS_PIC)
16183 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16184 else if (i == 0)
252b5132
RH
16185 mips_pic = NO_PIC;
16186 else if (i == 2)
143d77c5 16187 {
8b828383 16188 mips_pic = SVR4_PIC;
143d77c5
EC
16189 mips_abicalls = TRUE;
16190 }
252b5132 16191
4d0d148d 16192 if (mips_pic == SVR4_PIC)
252b5132
RH
16193 {
16194 if (g_switch_seen && g_switch_value != 0)
16195 as_warn (_("-G may not be used with SVR4 PIC code"));
16196 g_switch_value = 0;
16197 bfd_set_gp_size (stdoutput, 0);
16198 }
16199 }
16200 else
1661c76c 16201 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16202
d02603dc 16203 (void) restore_line_pointer (c);
252b5132
RH
16204 demand_empty_rest_of_line ();
16205}
16206
16207/* This structure is used to hold a stack of .set values. */
16208
e972090a
NC
16209struct mips_option_stack
16210{
252b5132
RH
16211 struct mips_option_stack *next;
16212 struct mips_set_options options;
16213};
16214
16215static struct mips_option_stack *mips_opts_stack;
16216
22522f88
MR
16217/* Return status for .set/.module option handling. */
16218
16219enum code_option_type
16220{
16221 /* Unrecognized option. */
16222 OPTION_TYPE_BAD = -1,
16223
16224 /* Ordinary option. */
16225 OPTION_TYPE_NORMAL,
16226
16227 /* ISA changing option. */
16228 OPTION_TYPE_ISA
16229};
16230
16231/* Handle common .set/.module options. Return status indicating option
16232 type. */
16233
16234static enum code_option_type
919731af 16235parse_code_option (char * name)
252b5132 16236{
22522f88 16237 bfd_boolean isa_set = FALSE;
c6278170 16238 const struct mips_ase *ase;
22522f88 16239
919731af 16240 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16241 {
16242 char *s = name + 3;
16243
16244 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16245 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16246 }
252b5132 16247 else if (strcmp (name, "at") == 0)
919731af 16248 mips_opts.at = ATREG;
252b5132 16249 else if (strcmp (name, "noat") == 0)
919731af 16250 mips_opts.at = ZERO;
252b5132 16251 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16252 mips_opts.nomove = 0;
252b5132 16253 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16254 mips_opts.nomove = 1;
252b5132 16255 else if (strcmp (name, "bopt") == 0)
919731af 16256 mips_opts.nobopt = 0;
252b5132 16257 else if (strcmp (name, "nobopt") == 0)
919731af 16258 mips_opts.nobopt = 1;
ad3fea08 16259 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16260 mips_opts.gp = 32;
ad3fea08 16261 else if (strcmp (name, "gp=64") == 0)
919731af 16262 mips_opts.gp = 64;
ad3fea08 16263 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16264 mips_opts.fp = 32;
351cdf24
MF
16265 else if (strcmp (name, "fp=xx") == 0)
16266 mips_opts.fp = 0;
ad3fea08 16267 else if (strcmp (name, "fp=64") == 0)
919731af 16268 mips_opts.fp = 64;
037b32b9
AN
16269 else if (strcmp (name, "softfloat") == 0)
16270 mips_opts.soft_float = 1;
16271 else if (strcmp (name, "hardfloat") == 0)
16272 mips_opts.soft_float = 0;
16273 else if (strcmp (name, "singlefloat") == 0)
16274 mips_opts.single_float = 1;
16275 else if (strcmp (name, "doublefloat") == 0)
16276 mips_opts.single_float = 0;
351cdf24
MF
16277 else if (strcmp (name, "nooddspreg") == 0)
16278 mips_opts.oddspreg = 0;
16279 else if (strcmp (name, "oddspreg") == 0)
16280 mips_opts.oddspreg = 1;
252b5132
RH
16281 else if (strcmp (name, "mips16") == 0
16282 || strcmp (name, "MIPS-16") == 0)
919731af 16283 mips_opts.mips16 = 1;
252b5132
RH
16284 else if (strcmp (name, "nomips16") == 0
16285 || strcmp (name, "noMIPS-16") == 0)
16286 mips_opts.mips16 = 0;
df58fc94 16287 else if (strcmp (name, "micromips") == 0)
919731af 16288 mips_opts.micromips = 1;
df58fc94
RS
16289 else if (strcmp (name, "nomicromips") == 0)
16290 mips_opts.micromips = 0;
c6278170
RS
16291 else if (name[0] == 'n'
16292 && name[1] == 'o'
16293 && (ase = mips_lookup_ase (name + 2)))
919731af 16294 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16295 else if ((ase = mips_lookup_ase (name)))
919731af 16296 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16297 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16298 {
1a2c1fad
CD
16299 /* Permit the user to change the ISA and architecture on the fly.
16300 Needless to say, misuse can cause serious problems. */
919731af 16301 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16302 {
16303 const struct mips_cpu_info *p;
16304
919731af 16305 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16306 if (!p)
16307 as_bad (_("unknown architecture %s"), name + 5);
16308 else
16309 {
16310 mips_opts.arch = p->cpu;
16311 mips_opts.isa = p->isa;
22522f88 16312 isa_set = TRUE;
1a2c1fad
CD
16313 }
16314 }
81a21e38
TS
16315 else if (strncmp (name, "mips", 4) == 0)
16316 {
16317 const struct mips_cpu_info *p;
16318
919731af 16319 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16320 if (!p)
16321 as_bad (_("unknown ISA level %s"), name + 4);
16322 else
16323 {
16324 mips_opts.arch = p->cpu;
16325 mips_opts.isa = p->isa;
22522f88 16326 isa_set = TRUE;
81a21e38
TS
16327 }
16328 }
af7ee8bf 16329 else
81a21e38 16330 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16331 }
16332 else if (strcmp (name, "autoextend") == 0)
16333 mips_opts.noautoextend = 0;
16334 else if (strcmp (name, "noautoextend") == 0)
16335 mips_opts.noautoextend = 1;
833794fc
MR
16336 else if (strcmp (name, "insn32") == 0)
16337 mips_opts.insn32 = TRUE;
16338 else if (strcmp (name, "noinsn32") == 0)
16339 mips_opts.insn32 = FALSE;
919731af 16340 else if (strcmp (name, "sym32") == 0)
16341 mips_opts.sym32 = TRUE;
16342 else if (strcmp (name, "nosym32") == 0)
16343 mips_opts.sym32 = FALSE;
16344 else
22522f88
MR
16345 return OPTION_TYPE_BAD;
16346
16347 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16348}
16349
16350/* Handle the .set pseudo-op. */
16351
16352static void
16353s_mipsset (int x ATTRIBUTE_UNUSED)
16354{
22522f88 16355 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16356 char *name = input_line_pointer, ch;
919731af 16357
16358 file_mips_check_options ();
16359
16360 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16361 ++input_line_pointer;
16362 ch = *input_line_pointer;
16363 *input_line_pointer = '\0';
16364
16365 if (strchr (name, ','))
16366 {
16367 /* Generic ".set" directive; use the generic handler. */
16368 *input_line_pointer = ch;
16369 input_line_pointer = name;
16370 s_set (0);
16371 return;
16372 }
16373
16374 if (strcmp (name, "reorder") == 0)
16375 {
16376 if (mips_opts.noreorder)
16377 end_noreorder ();
16378 }
16379 else if (strcmp (name, "noreorder") == 0)
16380 {
16381 if (!mips_opts.noreorder)
16382 start_noreorder ();
16383 }
16384 else if (strcmp (name, "macro") == 0)
16385 mips_opts.warn_about_macros = 0;
16386 else if (strcmp (name, "nomacro") == 0)
16387 {
16388 if (mips_opts.noreorder == 0)
16389 as_bad (_("`noreorder' must be set before `nomacro'"));
16390 mips_opts.warn_about_macros = 1;
16391 }
16392 else if (strcmp (name, "gp=default") == 0)
16393 mips_opts.gp = file_mips_opts.gp;
16394 else if (strcmp (name, "fp=default") == 0)
16395 mips_opts.fp = file_mips_opts.fp;
16396 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16397 {
16398 mips_opts.isa = file_mips_opts.isa;
16399 mips_opts.arch = file_mips_opts.arch;
16400 mips_opts.gp = file_mips_opts.gp;
16401 mips_opts.fp = file_mips_opts.fp;
16402 }
252b5132
RH
16403 else if (strcmp (name, "push") == 0)
16404 {
16405 struct mips_option_stack *s;
16406
325801bd 16407 s = XNEW (struct mips_option_stack);
252b5132
RH
16408 s->next = mips_opts_stack;
16409 s->options = mips_opts;
16410 mips_opts_stack = s;
16411 }
16412 else if (strcmp (name, "pop") == 0)
16413 {
16414 struct mips_option_stack *s;
16415
16416 s = mips_opts_stack;
16417 if (s == NULL)
16418 as_bad (_(".set pop with no .set push"));
16419 else
16420 {
16421 /* If we're changing the reorder mode we need to handle
16422 delay slots correctly. */
16423 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16424 start_noreorder ();
252b5132 16425 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16426 end_noreorder ();
252b5132
RH
16427
16428 mips_opts = s->options;
16429 mips_opts_stack = s->next;
16430 free (s);
16431 }
16432 }
22522f88
MR
16433 else
16434 {
16435 type = parse_code_option (name);
16436 if (type == OPTION_TYPE_BAD)
16437 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16438 }
919731af 16439
16440 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16441 registers based on what is supported by the arch/cpu. */
22522f88 16442 if (type == OPTION_TYPE_ISA)
e6559e01 16443 {
919731af 16444 switch (mips_opts.isa)
16445 {
16446 case 0:
16447 break;
16448 case ISA_MIPS1:
351cdf24
MF
16449 /* MIPS I cannot support FPXX. */
16450 mips_opts.fp = 32;
16451 /* fall-through. */
919731af 16452 case ISA_MIPS2:
16453 case ISA_MIPS32:
16454 case ISA_MIPS32R2:
16455 case ISA_MIPS32R3:
16456 case ISA_MIPS32R5:
16457 mips_opts.gp = 32;
351cdf24
MF
16458 if (mips_opts.fp != 0)
16459 mips_opts.fp = 32;
919731af 16460 break;
7361da2c
AB
16461 case ISA_MIPS32R6:
16462 mips_opts.gp = 32;
16463 mips_opts.fp = 64;
16464 break;
919731af 16465 case ISA_MIPS3:
16466 case ISA_MIPS4:
16467 case ISA_MIPS5:
16468 case ISA_MIPS64:
16469 case ISA_MIPS64R2:
16470 case ISA_MIPS64R3:
16471 case ISA_MIPS64R5:
7361da2c 16472 case ISA_MIPS64R6:
919731af 16473 mips_opts.gp = 64;
351cdf24
MF
16474 if (mips_opts.fp != 0)
16475 {
16476 if (mips_opts.arch == CPU_R5900)
16477 mips_opts.fp = 32;
16478 else
16479 mips_opts.fp = 64;
16480 }
919731af 16481 break;
16482 default:
16483 as_bad (_("unknown ISA level %s"), name + 4);
16484 break;
16485 }
e6559e01 16486 }
919731af 16487
16488 mips_check_options (&mips_opts, FALSE);
16489
16490 mips_check_isa_supports_ases ();
16491 *input_line_pointer = ch;
16492 demand_empty_rest_of_line ();
16493}
16494
16495/* Handle the .module pseudo-op. */
16496
16497static void
16498s_module (int ignore ATTRIBUTE_UNUSED)
16499{
16500 char *name = input_line_pointer, ch;
16501
16502 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16503 ++input_line_pointer;
16504 ch = *input_line_pointer;
16505 *input_line_pointer = '\0';
16506
16507 if (!file_mips_opts_checked)
252b5132 16508 {
22522f88 16509 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16510 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16511
16512 /* Update module level settings from mips_opts. */
16513 file_mips_opts = mips_opts;
252b5132 16514 }
919731af 16515 else
16516 as_bad (_(".module is not permitted after generating code"));
16517
252b5132
RH
16518 *input_line_pointer = ch;
16519 demand_empty_rest_of_line ();
16520}
16521
16522/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16523 .option pic2. It means to generate SVR4 PIC calls. */
16524
16525static void
17a2f251 16526s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16527{
16528 mips_pic = SVR4_PIC;
143d77c5 16529 mips_abicalls = TRUE;
4d0d148d
TS
16530
16531 if (g_switch_seen && g_switch_value != 0)
16532 as_warn (_("-G may not be used with SVR4 PIC code"));
16533 g_switch_value = 0;
16534
252b5132
RH
16535 bfd_set_gp_size (stdoutput, 0);
16536 demand_empty_rest_of_line ();
16537}
16538
16539/* Handle the .cpload pseudo-op. This is used when generating SVR4
16540 PIC code. It sets the $gp register for the function based on the
16541 function address, which is in the register named in the argument.
16542 This uses a relocation against _gp_disp, which is handled specially
16543 by the linker. The result is:
16544 lui $gp,%hi(_gp_disp)
16545 addiu $gp,$gp,%lo(_gp_disp)
16546 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16547 The .cpload argument is normally $25 == $t9.
16548
16549 The -mno-shared option changes this to:
bbe506e8
TS
16550 lui $gp,%hi(__gnu_local_gp)
16551 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16552 and the argument is ignored. This saves an instruction, but the
16553 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16554 address for __gnu_local_gp. Thus code assembled with -mno-shared
16555 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16556
16557static void
17a2f251 16558s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16559{
16560 expressionS ex;
aa6975fb
ILT
16561 int reg;
16562 int in_shared;
252b5132 16563
919731af 16564 file_mips_check_options ();
16565
6478892d
TS
16566 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16567 .cpload is ignored. */
16568 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16569 {
16570 s_ignore (0);
16571 return;
16572 }
16573
a276b80c
MR
16574 if (mips_opts.mips16)
16575 {
16576 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16577 ignore_rest_of_line ();
16578 return;
16579 }
16580
d3ecfc59 16581 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16582 if (mips_opts.noreorder == 0)
16583 as_warn (_(".cpload not in noreorder section"));
16584
aa6975fb
ILT
16585 reg = tc_get_register (0);
16586
16587 /* If we need to produce a 64-bit address, we are better off using
16588 the default instruction sequence. */
aed1a261 16589 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16590
252b5132 16591 ex.X_op = O_symbol;
bbe506e8
TS
16592 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16593 "__gnu_local_gp");
252b5132
RH
16594 ex.X_op_symbol = NULL;
16595 ex.X_add_number = 0;
16596
16597 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16598 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16599
8a75745d
MR
16600 mips_mark_labels ();
16601 mips_assembling_insn = TRUE;
16602
584892a6 16603 macro_start ();
67c0d1eb
RS
16604 macro_build_lui (&ex, mips_gp_register);
16605 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16606 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16607 if (in_shared)
16608 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16609 mips_gp_register, reg);
584892a6 16610 macro_end ();
252b5132 16611
8a75745d 16612 mips_assembling_insn = FALSE;
252b5132
RH
16613 demand_empty_rest_of_line ();
16614}
16615
6478892d
TS
16616/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16617 .cpsetup $reg1, offset|$reg2, label
16618
16619 If offset is given, this results in:
16620 sd $gp, offset($sp)
956cd1d6 16621 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16622 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16623 daddu $gp, $gp, $reg1
6478892d
TS
16624
16625 If $reg2 is given, this results in:
40fc1451 16626 or $reg2, $gp, $0
956cd1d6 16627 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16628 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16629 daddu $gp, $gp, $reg1
aa6975fb
ILT
16630 $reg1 is normally $25 == $t9.
16631
16632 The -mno-shared option replaces the last three instructions with
16633 lui $gp,%hi(_gp)
54f4ddb3 16634 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16635
6478892d 16636static void
17a2f251 16637s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16638{
16639 expressionS ex_off;
16640 expressionS ex_sym;
16641 int reg1;
6478892d 16642
919731af 16643 file_mips_check_options ();
16644
8586fc66 16645 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16646 We also need NewABI support. */
16647 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16648 {
16649 s_ignore (0);
16650 return;
16651 }
16652
a276b80c
MR
16653 if (mips_opts.mips16)
16654 {
16655 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16656 ignore_rest_of_line ();
16657 return;
16658 }
16659
6478892d
TS
16660 reg1 = tc_get_register (0);
16661 SKIP_WHITESPACE ();
16662 if (*input_line_pointer != ',')
16663 {
16664 as_bad (_("missing argument separator ',' for .cpsetup"));
16665 return;
16666 }
16667 else
80245285 16668 ++input_line_pointer;
6478892d
TS
16669 SKIP_WHITESPACE ();
16670 if (*input_line_pointer == '$')
80245285
TS
16671 {
16672 mips_cpreturn_register = tc_get_register (0);
16673 mips_cpreturn_offset = -1;
16674 }
6478892d 16675 else
80245285
TS
16676 {
16677 mips_cpreturn_offset = get_absolute_expression ();
16678 mips_cpreturn_register = -1;
16679 }
6478892d
TS
16680 SKIP_WHITESPACE ();
16681 if (*input_line_pointer != ',')
16682 {
16683 as_bad (_("missing argument separator ',' for .cpsetup"));
16684 return;
16685 }
16686 else
f9419b05 16687 ++input_line_pointer;
6478892d 16688 SKIP_WHITESPACE ();
f21f8242 16689 expression (&ex_sym);
6478892d 16690
8a75745d
MR
16691 mips_mark_labels ();
16692 mips_assembling_insn = TRUE;
16693
584892a6 16694 macro_start ();
6478892d
TS
16695 if (mips_cpreturn_register == -1)
16696 {
16697 ex_off.X_op = O_constant;
16698 ex_off.X_add_symbol = NULL;
16699 ex_off.X_op_symbol = NULL;
16700 ex_off.X_add_number = mips_cpreturn_offset;
16701
67c0d1eb 16702 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16703 BFD_RELOC_LO16, SP);
6478892d
TS
16704 }
16705 else
40fc1451 16706 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16707
aed1a261 16708 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16709 {
df58fc94 16710 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16711 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16712 BFD_RELOC_HI16_S);
16713
16714 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16715 mips_gp_register, -1, BFD_RELOC_GPREL16,
16716 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16717
16718 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16719 mips_gp_register, reg1);
16720 }
16721 else
16722 {
16723 expressionS ex;
16724
16725 ex.X_op = O_symbol;
4184909a 16726 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16727 ex.X_op_symbol = NULL;
16728 ex.X_add_number = 0;
6e1304d8 16729
aa6975fb
ILT
16730 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16731 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16732
16733 macro_build_lui (&ex, mips_gp_register);
16734 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16735 mips_gp_register, BFD_RELOC_LO16);
16736 }
f21f8242 16737
584892a6 16738 macro_end ();
6478892d 16739
8a75745d 16740 mips_assembling_insn = FALSE;
6478892d
TS
16741 demand_empty_rest_of_line ();
16742}
16743
16744static void
17a2f251 16745s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16746{
919731af 16747 file_mips_check_options ();
16748
6478892d 16749 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16750 .cplocal is ignored. */
6478892d
TS
16751 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16752 {
16753 s_ignore (0);
16754 return;
16755 }
16756
a276b80c
MR
16757 if (mips_opts.mips16)
16758 {
16759 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16760 ignore_rest_of_line ();
16761 return;
16762 }
16763
6478892d 16764 mips_gp_register = tc_get_register (0);
85b51719 16765 demand_empty_rest_of_line ();
6478892d
TS
16766}
16767
252b5132
RH
16768/* Handle the .cprestore pseudo-op. This stores $gp into a given
16769 offset from $sp. The offset is remembered, and after making a PIC
16770 call $gp is restored from that location. */
16771
16772static void
17a2f251 16773s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16774{
16775 expressionS ex;
252b5132 16776
919731af 16777 file_mips_check_options ();
16778
6478892d 16779 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16780 .cprestore is ignored. */
6478892d 16781 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16782 {
16783 s_ignore (0);
16784 return;
16785 }
16786
a276b80c
MR
16787 if (mips_opts.mips16)
16788 {
16789 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16790 ignore_rest_of_line ();
16791 return;
16792 }
16793
252b5132 16794 mips_cprestore_offset = get_absolute_expression ();
7a621144 16795 mips_cprestore_valid = 1;
252b5132
RH
16796
16797 ex.X_op = O_constant;
16798 ex.X_add_symbol = NULL;
16799 ex.X_op_symbol = NULL;
16800 ex.X_add_number = mips_cprestore_offset;
16801
8a75745d
MR
16802 mips_mark_labels ();
16803 mips_assembling_insn = TRUE;
16804
584892a6 16805 macro_start ();
67c0d1eb
RS
16806 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16807 SP, HAVE_64BIT_ADDRESSES);
584892a6 16808 macro_end ();
252b5132 16809
8a75745d 16810 mips_assembling_insn = FALSE;
252b5132
RH
16811 demand_empty_rest_of_line ();
16812}
16813
6478892d 16814/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16815 was given in the preceding .cpsetup, it results in:
6478892d 16816 ld $gp, offset($sp)
76b3015f 16817
6478892d 16818 If a register $reg2 was given there, it results in:
40fc1451 16819 or $gp, $reg2, $0 */
54f4ddb3 16820
6478892d 16821static void
17a2f251 16822s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16823{
16824 expressionS ex;
6478892d 16825
919731af 16826 file_mips_check_options ();
16827
6478892d
TS
16828 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16829 We also need NewABI support. */
16830 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16831 {
16832 s_ignore (0);
16833 return;
16834 }
16835
a276b80c
MR
16836 if (mips_opts.mips16)
16837 {
16838 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16839 ignore_rest_of_line ();
16840 return;
16841 }
16842
8a75745d
MR
16843 mips_mark_labels ();
16844 mips_assembling_insn = TRUE;
16845
584892a6 16846 macro_start ();
6478892d
TS
16847 if (mips_cpreturn_register == -1)
16848 {
16849 ex.X_op = O_constant;
16850 ex.X_add_symbol = NULL;
16851 ex.X_op_symbol = NULL;
16852 ex.X_add_number = mips_cpreturn_offset;
16853
67c0d1eb 16854 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16855 }
16856 else
40fc1451
SD
16857 move_register (mips_gp_register, mips_cpreturn_register);
16858
584892a6 16859 macro_end ();
6478892d 16860
8a75745d 16861 mips_assembling_insn = FALSE;
6478892d
TS
16862 demand_empty_rest_of_line ();
16863}
16864
d0f13682
CLT
16865/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16866 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16867 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16868 debug information or MIPS16 TLS. */
741d6ea8
JM
16869
16870static void
d0f13682
CLT
16871s_tls_rel_directive (const size_t bytes, const char *dirstr,
16872 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16873{
16874 expressionS ex;
16875 char *p;
16876
16877 expression (&ex);
16878
16879 if (ex.X_op != O_symbol)
16880 {
1661c76c 16881 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16882 ignore_rest_of_line ();
16883 }
16884
16885 p = frag_more (bytes);
16886 md_number_to_chars (p, 0, bytes);
d0f13682 16887 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16888 demand_empty_rest_of_line ();
de64cffd 16889 mips_clear_insn_labels ();
741d6ea8
JM
16890}
16891
16892/* Handle .dtprelword. */
16893
16894static void
16895s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16896{
d0f13682 16897 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16898}
16899
16900/* Handle .dtpreldword. */
16901
16902static void
16903s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16904{
d0f13682
CLT
16905 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16906}
16907
16908/* Handle .tprelword. */
16909
16910static void
16911s_tprelword (int ignore ATTRIBUTE_UNUSED)
16912{
16913 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16914}
16915
16916/* Handle .tpreldword. */
16917
16918static void
16919s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16920{
16921 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16922}
16923
6478892d
TS
16924/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16925 code. It sets the offset to use in gp_rel relocations. */
16926
16927static void
17a2f251 16928s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16929{
16930 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16931 We also need NewABI support. */
16932 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16933 {
16934 s_ignore (0);
16935 return;
16936 }
16937
def2e0dd 16938 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16939
16940 demand_empty_rest_of_line ();
16941}
16942
252b5132
RH
16943/* Handle the .gpword pseudo-op. This is used when generating PIC
16944 code. It generates a 32 bit GP relative reloc. */
16945
16946static void
17a2f251 16947s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16948{
a8dbcb85
TS
16949 segment_info_type *si;
16950 struct insn_label_list *l;
252b5132
RH
16951 expressionS ex;
16952 char *p;
16953
16954 /* When not generating PIC code, this is treated as .word. */
16955 if (mips_pic != SVR4_PIC)
16956 {
16957 s_cons (2);
16958 return;
16959 }
16960
a8dbcb85
TS
16961 si = seg_info (now_seg);
16962 l = si->label_list;
7d10b47d 16963 mips_emit_delays ();
252b5132 16964 if (auto_align)
462427c4 16965 mips_align (2, 0, l);
252b5132
RH
16966
16967 expression (&ex);
a1facbec 16968 mips_clear_insn_labels ();
252b5132
RH
16969
16970 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16971 {
1661c76c 16972 as_bad (_("unsupported use of .gpword"));
252b5132
RH
16973 ignore_rest_of_line ();
16974 }
16975
16976 p = frag_more (4);
17a2f251 16977 md_number_to_chars (p, 0, 4);
b34976b6 16978 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16979 BFD_RELOC_GPREL32);
252b5132
RH
16980
16981 demand_empty_rest_of_line ();
16982}
16983
10181a0d 16984static void
17a2f251 16985s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16986{
a8dbcb85
TS
16987 segment_info_type *si;
16988 struct insn_label_list *l;
10181a0d
AO
16989 expressionS ex;
16990 char *p;
16991
16992 /* When not generating PIC code, this is treated as .dword. */
16993 if (mips_pic != SVR4_PIC)
16994 {
16995 s_cons (3);
16996 return;
16997 }
16998
a8dbcb85
TS
16999 si = seg_info (now_seg);
17000 l = si->label_list;
7d10b47d 17001 mips_emit_delays ();
10181a0d 17002 if (auto_align)
462427c4 17003 mips_align (3, 0, l);
10181a0d
AO
17004
17005 expression (&ex);
a1facbec 17006 mips_clear_insn_labels ();
10181a0d
AO
17007
17008 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17009 {
1661c76c 17010 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17011 ignore_rest_of_line ();
17012 }
17013
17014 p = frag_more (8);
17a2f251 17015 md_number_to_chars (p, 0, 8);
a105a300 17016 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17017 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17018
17019 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17020 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17021 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17022
17023 demand_empty_rest_of_line ();
17024}
17025
a3f278e2
CM
17026/* Handle the .ehword pseudo-op. This is used when generating unwinding
17027 tables. It generates a R_MIPS_EH reloc. */
17028
17029static void
17030s_ehword (int ignore ATTRIBUTE_UNUSED)
17031{
17032 expressionS ex;
17033 char *p;
17034
17035 mips_emit_delays ();
17036
17037 expression (&ex);
17038 mips_clear_insn_labels ();
17039
17040 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17041 {
1661c76c 17042 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17043 ignore_rest_of_line ();
17044 }
17045
17046 p = frag_more (4);
17047 md_number_to_chars (p, 0, 4);
17048 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17049 BFD_RELOC_32_PCREL);
a3f278e2
CM
17050
17051 demand_empty_rest_of_line ();
17052}
17053
252b5132
RH
17054/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17055 tables in SVR4 PIC code. */
17056
17057static void
17a2f251 17058s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17059{
252b5132
RH
17060 int reg;
17061
919731af 17062 file_mips_check_options ();
17063
10181a0d
AO
17064 /* This is ignored when not generating SVR4 PIC code. */
17065 if (mips_pic != SVR4_PIC)
252b5132
RH
17066 {
17067 s_ignore (0);
17068 return;
17069 }
17070
8a75745d
MR
17071 mips_mark_labels ();
17072 mips_assembling_insn = TRUE;
17073
252b5132 17074 /* Add $gp to the register named as an argument. */
584892a6 17075 macro_start ();
252b5132 17076 reg = tc_get_register (0);
67c0d1eb 17077 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17078 macro_end ();
252b5132 17079
8a75745d 17080 mips_assembling_insn = FALSE;
bdaaa2e1 17081 demand_empty_rest_of_line ();
252b5132
RH
17082}
17083
17084/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17085 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17086 such as generating jalx instructions when needed. We also make
17087 them odd for the duration of the assembly, in order to generate the
17088 right sort of code. We will make them even in the adjust_symtab
17089 routine, while leaving them marked. This is convenient for the
17090 debugger and the disassembler. The linker knows to make them odd
17091 again. */
17092
17093static void
17a2f251 17094s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17095{
7bb01e2d
MR
17096 file_mips_check_options ();
17097 file_ase_mips16 |= mips_opts.mips16;
17098 file_ase_micromips |= mips_opts.micromips;
17099
df58fc94 17100 mips_mark_labels ();
252b5132
RH
17101
17102 demand_empty_rest_of_line ();
17103}
17104
ba92f887
MR
17105/* Handle the .nan pseudo-op. */
17106
17107static void
17108s_nan (int ignore ATTRIBUTE_UNUSED)
17109{
17110 static const char str_legacy[] = "legacy";
17111 static const char str_2008[] = "2008";
17112 size_t i;
17113
17114 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17115
17116 if (i == sizeof (str_2008) - 1
17117 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17118 mips_nan2008 = 1;
ba92f887
MR
17119 else if (i == sizeof (str_legacy) - 1
17120 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17121 {
17122 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17123 mips_nan2008 = 0;
17124 else
17125 as_bad (_("`%s' does not support legacy NaN"),
17126 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17127 }
ba92f887 17128 else
1661c76c 17129 as_bad (_("bad .nan directive"));
ba92f887
MR
17130
17131 input_line_pointer += i;
17132 demand_empty_rest_of_line ();
17133}
17134
754e2bb9
RS
17135/* Handle a .stab[snd] directive. Ideally these directives would be
17136 implemented in a transparent way, so that removing them would not
17137 have any effect on the generated instructions. However, s_stab
17138 internally changes the section, so in practice we need to decide
17139 now whether the preceding label marks compressed code. We do not
17140 support changing the compression mode of a label after a .stab*
17141 directive, such as in:
17142
17143 foo:
134c0c8b 17144 .stabs ...
754e2bb9
RS
17145 .set mips16
17146
17147 so the current mode wins. */
252b5132
RH
17148
17149static void
17a2f251 17150s_mips_stab (int type)
252b5132 17151{
754e2bb9 17152 mips_mark_labels ();
252b5132
RH
17153 s_stab (type);
17154}
17155
54f4ddb3 17156/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17157
17158static void
17a2f251 17159s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17160{
17161 char *name;
17162 int c;
17163 symbolS *symbolP;
17164 expressionS exp;
17165
d02603dc 17166 c = get_symbol_name (&name);
252b5132
RH
17167 symbolP = symbol_find_or_make (name);
17168 S_SET_WEAK (symbolP);
17169 *input_line_pointer = c;
17170
d02603dc 17171 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17172
17173 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17174 {
17175 if (S_IS_DEFINED (symbolP))
17176 {
20203fb9 17177 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17178 S_GET_NAME (symbolP));
17179 ignore_rest_of_line ();
17180 return;
17181 }
bdaaa2e1 17182
252b5132
RH
17183 if (*input_line_pointer == ',')
17184 {
17185 ++input_line_pointer;
17186 SKIP_WHITESPACE ();
17187 }
bdaaa2e1 17188
252b5132
RH
17189 expression (&exp);
17190 if (exp.X_op != O_symbol)
17191 {
20203fb9 17192 as_bad (_("bad .weakext directive"));
98d3f06f 17193 ignore_rest_of_line ();
252b5132
RH
17194 return;
17195 }
49309057 17196 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17197 }
17198
17199 demand_empty_rest_of_line ();
17200}
17201
17202/* Parse a register string into a number. Called from the ECOFF code
17203 to parse .frame. The argument is non-zero if this is the frame
17204 register, so that we can record it in mips_frame_reg. */
17205
17206int
17a2f251 17207tc_get_register (int frame)
252b5132 17208{
707bfff6 17209 unsigned int reg;
252b5132
RH
17210
17211 SKIP_WHITESPACE ();
707bfff6
TS
17212 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17213 reg = 0;
252b5132 17214 if (frame)
7a621144
DJ
17215 {
17216 mips_frame_reg = reg != 0 ? reg : SP;
17217 mips_frame_reg_valid = 1;
17218 mips_cprestore_valid = 0;
17219 }
252b5132
RH
17220 return reg;
17221}
17222
17223valueT
17a2f251 17224md_section_align (asection *seg, valueT addr)
252b5132
RH
17225{
17226 int align = bfd_get_section_alignment (stdoutput, seg);
17227
f3ded42a
RS
17228 /* We don't need to align ELF sections to the full alignment.
17229 However, Irix 5 may prefer that we align them at least to a 16
17230 byte boundary. We don't bother to align the sections if we
17231 are targeted for an embedded system. */
17232 if (strncmp (TARGET_OS, "elf", 3) == 0)
17233 return addr;
17234 if (align > 4)
17235 align = 4;
252b5132 17236
8d3842cd 17237 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17238}
17239
17240/* Utility routine, called from above as well. If called while the
17241 input file is still being read, it's only an approximation. (For
17242 example, a symbol may later become defined which appeared to be
17243 undefined earlier.) */
17244
17245static int
17a2f251 17246nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17247{
17248 if (sym == 0)
17249 return 0;
17250
4d0d148d 17251 if (g_switch_value > 0)
252b5132
RH
17252 {
17253 const char *symname;
17254 int change;
17255
c9914766 17256 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17257 register. It can be if it is smaller than the -G size or if
17258 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17259 not be referenced off the $gp, although it appears as though
252b5132
RH
17260 they can. */
17261 symname = S_GET_NAME (sym);
17262 if (symname != (const char *) NULL
17263 && (strcmp (symname, "eprol") == 0
17264 || strcmp (symname, "etext") == 0
17265 || strcmp (symname, "_gp") == 0
17266 || strcmp (symname, "edata") == 0
17267 || strcmp (symname, "_fbss") == 0
17268 || strcmp (symname, "_fdata") == 0
17269 || strcmp (symname, "_ftext") == 0
17270 || strcmp (symname, "end") == 0
17271 || strcmp (symname, "_gp_disp") == 0))
17272 change = 1;
17273 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17274 && (0
17275#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17276 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17277 && (symbol_get_obj (sym)->ecoff_extern_size
17278 <= g_switch_value))
252b5132
RH
17279#endif
17280 /* We must defer this decision until after the whole
17281 file has been read, since there might be a .extern
17282 after the first use of this symbol. */
17283 || (before_relaxing
17284#ifndef NO_ECOFF_DEBUGGING
49309057 17285 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17286#endif
17287 && S_GET_VALUE (sym) == 0)
17288 || (S_GET_VALUE (sym) != 0
17289 && S_GET_VALUE (sym) <= g_switch_value)))
17290 change = 0;
17291 else
17292 {
17293 const char *segname;
17294
17295 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17296 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17297 && strcmp (segname, ".lit4") != 0);
17298 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17299 && strcmp (segname, ".sbss") != 0
17300 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17301 && strncmp (segname, ".sbss.", 6) != 0
17302 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17303 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17304 }
17305 return change;
17306 }
17307 else
c9914766 17308 /* We are not optimizing for the $gp register. */
252b5132
RH
17309 return 1;
17310}
17311
5919d012
RS
17312
17313/* Return true if the given symbol should be considered local for SVR4 PIC. */
17314
17315static bfd_boolean
9e009953 17316pic_need_relax (symbolS *sym)
5919d012
RS
17317{
17318 asection *symsec;
5919d012
RS
17319
17320 /* Handle the case of a symbol equated to another symbol. */
17321 while (symbol_equated_reloc_p (sym))
17322 {
17323 symbolS *n;
17324
5f0fe04b 17325 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17326 n = symbol_get_value_expression (sym)->X_add_symbol;
17327 if (n == sym)
17328 break;
17329 sym = n;
17330 }
17331
df1f3cda
DD
17332 if (symbol_section_p (sym))
17333 return TRUE;
17334
5919d012
RS
17335 symsec = S_GET_SEGMENT (sym);
17336
5919d012 17337 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17338 return (!bfd_is_und_section (symsec)
17339 && !bfd_is_abs_section (symsec)
5f0fe04b 17340 && !bfd_is_com_section (symsec)
5919d012 17341 /* A global or weak symbol is treated as external. */
f3ded42a 17342 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17343}
14f72d45
MR
17344\f
17345/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17346 convert a section-relative value VAL to the equivalent PC-relative
17347 value. */
17348
17349static offsetT
17350mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17351 offsetT val, long stretch)
17352{
17353 fragS *sym_frag;
17354 addressT addr;
17355
17356 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17357
17358 sym_frag = symbol_get_frag (fragp->fr_symbol);
17359
17360 /* If the relax_marker of the symbol fragment differs from the
17361 relax_marker of this fragment, we have not yet adjusted the
17362 symbol fragment fr_address. We want to add in STRETCH in
17363 order to get a better estimate of the address. This
17364 particularly matters because of the shift bits. */
17365 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17366 {
17367 fragS *f;
17368
17369 /* Adjust stretch for any alignment frag. Note that if have
17370 been expanding the earlier code, the symbol may be
17371 defined in what appears to be an earlier frag. FIXME:
17372 This doesn't handle the fr_subtype field, which specifies
17373 a maximum number of bytes to skip when doing an
17374 alignment. */
17375 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17376 {
17377 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17378 {
17379 if (stretch < 0)
17380 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17381 else
17382 stretch &= ~((1 << (int) f->fr_offset) - 1);
17383 if (stretch == 0)
17384 break;
17385 }
17386 }
17387 if (f != NULL)
17388 val += stretch;
17389 }
17390
17391 addr = fragp->fr_address + fragp->fr_fix;
17392
17393 /* The base address rules are complicated. The base address of
17394 a branch is the following instruction. The base address of a
17395 PC relative load or add is the instruction itself, but if it
17396 is in a delay slot (in which case it can not be extended) use
17397 the address of the instruction whose delay slot it is in. */
17398 if (pcrel_op->include_isa_bit)
17399 {
17400 addr += 2;
17401
17402 /* If we are currently assuming that this frag should be
17403 extended, then the current address is two bytes higher. */
17404 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17405 addr += 2;
17406
17407 /* Ignore the low bit in the target, since it will be set
17408 for a text label. */
17409 val &= -2;
17410 }
17411 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17412 addr -= 4;
17413 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17414 addr -= 2;
5919d012 17415
14f72d45
MR
17416 val -= addr & -(1 << pcrel_op->align_log2);
17417
17418 return val;
17419}
5919d012 17420
252b5132
RH
17421/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17422 extended opcode. SEC is the section the frag is in. */
17423
17424static int
17a2f251 17425mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17426{
3ccad066 17427 const struct mips_int_operand *operand;
252b5132 17428 offsetT val;
252b5132 17429 segT symsec;
14f72d45 17430 int type;
252b5132
RH
17431
17432 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17433 return 0;
17434 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17435 return 1;
17436
88a7ef16 17437 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17438 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17439 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17440 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17441 || (operand->root.type == OP_PCREL
17442 ? sec != symsec
17443 : !bfd_is_abs_section (symsec)))
17444 return 1;
252b5132 17445
88a7ef16 17446 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17447
3ccad066 17448 if (operand->root.type == OP_PCREL)
252b5132 17449 {
3ccad066 17450 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17451 offsetT maxtiny;
252b5132 17452
1425c41d 17453 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17454 return 1;
252b5132 17455
88a7ef16 17456 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17457 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17458
17459 /* If any of the shifted bits are set, we must use an extended
17460 opcode. If the address depends on the size of this
17461 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17462 use an extended opcode. */
17463 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17464 {
17465 fragp->fr_subtype =
1425c41d 17466 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17467 return 1;
17468 }
17469
17470 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17471 is precisely the next value above maxtiny, then there is a
17472 chance of an infinite loop as in the following code:
252b5132
RH
17473 la $4,foo
17474 .skip 1020
17475 .align 2
17476 foo:
17477 In this case when the la is extended, foo is 0x3fc bytes
17478 away, so the la can be shrunk, but then foo is 0x400 away, so
17479 the la must be extended. To avoid this loop, we mark the
17480 frag as extended if it was small, and is about to become
3ccad066
RS
17481 extended with the next value above maxtiny. */
17482 maxtiny = mips_int_operand_max (operand);
17483 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17484 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17485 {
17486 fragp->fr_subtype =
1425c41d 17487 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17488 return 1;
17489 }
17490 }
252b5132 17491
3ccad066 17492 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17493}
17494
8507b6e7
MR
17495/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17496 macro expansion. SEC is the section the frag is in. We only
17497 support PC-relative instructions (LA, DLA, LW, LD) here, in
17498 non-PIC code using 32-bit addressing. */
17499
17500static int
17501mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17502{
17503 const struct mips_pcrel_operand *pcrel_op;
17504 const struct mips_int_operand *operand;
17505 offsetT val;
17506 segT symsec;
17507 int type;
17508
17509 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17510
17511 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17512 return 0;
17513 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17514 return 0;
17515
17516 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17517 switch (type)
17518 {
17519 case 'A':
17520 case 'B':
17521 case 'E':
17522 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17523 if (bfd_is_abs_section (symsec))
17524 return 1;
17525 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17526 return 0;
17527 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17528 return 1;
17529
17530 operand = mips16_immed_operand (type, TRUE);
17531 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17532 pcrel_op = (const struct mips_pcrel_operand *) operand;
17533 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17534
17535 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17536
17537 default:
17538 return 0;
17539 }
17540}
17541
4a6a3df4
AO
17542/* Compute the length of a branch sequence, and adjust the
17543 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17544 worst-case length is computed, with UPDATE being used to indicate
17545 whether an unconditional (-1), branch-likely (+1) or regular (0)
17546 branch is to be computed. */
17547static int
17a2f251 17548relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17549{
b34976b6 17550 bfd_boolean toofar;
4a6a3df4
AO
17551 int length;
17552
17553 if (fragp
17554 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17555 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17556 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17557 {
17558 addressT addr;
17559 offsetT val;
17560
17561 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17562
17563 addr = fragp->fr_address + fragp->fr_fix + 4;
17564
17565 val -= addr;
17566
17567 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17568 }
4a6a3df4 17569 else
c1f61bd2
MR
17570 /* If the symbol is not defined or it's in a different segment,
17571 we emit the long sequence. */
b34976b6 17572 toofar = TRUE;
4a6a3df4
AO
17573
17574 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17575 fragp->fr_subtype
66b3e8da 17576 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17577 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17578 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17579 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17580 RELAX_BRANCH_LINK (fragp->fr_subtype),
17581 toofar);
17582
17583 length = 4;
17584 if (toofar)
17585 {
17586 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17587 length += 8;
17588
ce8ad872 17589 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17590 {
17591 /* Additional space for PIC loading of target address. */
17592 length += 8;
17593 if (mips_opts.isa == ISA_MIPS1)
17594 /* Additional space for $at-stabilizing nop. */
17595 length += 4;
17596 }
17597
17598 /* If branch is conditional. */
17599 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17600 length += 8;
17601 }
b34976b6 17602
4a6a3df4
AO
17603 return length;
17604}
17605
7bd374a4
MR
17606/* Get a FRAG's branch instruction delay slot size, either from the
17607 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17608 or SHORT_INSN_SIZE otherwise. */
17609
17610static int
17611frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17612{
17613 char *buf = fragp->fr_literal + fragp->fr_fix;
17614
17615 if (al)
17616 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17617 else
17618 return short_insn_size;
17619}
17620
df58fc94
RS
17621/* Compute the length of a branch sequence, and adjust the
17622 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17623 worst-case length is computed, with UPDATE being used to indicate
17624 whether an unconditional (-1), or regular (0) branch is to be
17625 computed. */
17626
17627static int
17628relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17629{
7bd374a4
MR
17630 bfd_boolean insn32 = TRUE;
17631 bfd_boolean nods = TRUE;
ce8ad872 17632 bfd_boolean pic = TRUE;
7bd374a4
MR
17633 bfd_boolean al = TRUE;
17634 int short_insn_size;
df58fc94
RS
17635 bfd_boolean toofar;
17636 int length;
17637
7bd374a4
MR
17638 if (fragp)
17639 {
17640 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17641 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17642 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17643 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17644 }
17645 short_insn_size = insn32 ? 4 : 2;
17646
df58fc94
RS
17647 if (fragp
17648 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17649 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17650 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17651 {
17652 addressT addr;
17653 offsetT val;
17654
17655 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17656 /* Ignore the low bit in the target, since it will be set
17657 for a text label. */
17658 if ((val & 1) != 0)
17659 --val;
17660
17661 addr = fragp->fr_address + fragp->fr_fix + 4;
17662
17663 val -= addr;
17664
17665 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17666 }
df58fc94 17667 else
c1f61bd2
MR
17668 /* If the symbol is not defined or it's in a different segment,
17669 we emit the long sequence. */
df58fc94
RS
17670 toofar = TRUE;
17671
17672 if (fragp && update
17673 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17674 fragp->fr_subtype = (toofar
17675 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17676 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17677
17678 length = 4;
17679 if (toofar)
17680 {
17681 bfd_boolean compact_known = fragp != NULL;
17682 bfd_boolean compact = FALSE;
17683 bfd_boolean uncond;
17684
df58fc94 17685 if (fragp)
8484fb75
MR
17686 {
17687 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17688 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17689 }
df58fc94
RS
17690 else
17691 uncond = update < 0;
17692
17693 /* If label is out of range, we turn branch <br>:
17694
17695 <br> label # 4 bytes
17696 0:
17697
17698 into:
17699
17700 j label # 4 bytes
8484fb75
MR
17701 nop # 2/4 bytes if
17702 # compact && (!PIC || insn32)
df58fc94
RS
17703 0:
17704 */
ce8ad872 17705 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17706 length += short_insn_size;
df58fc94
RS
17707
17708 /* If assembling PIC code, we further turn:
17709
17710 j label # 4 bytes
17711
17712 into:
17713
17714 lw/ld at, %got(label)(gp) # 4 bytes
17715 d/addiu at, %lo(label) # 4 bytes
8484fb75 17716 jr/c at # 2/4 bytes
df58fc94 17717 */
ce8ad872 17718 if (pic)
8484fb75 17719 length += 4 + short_insn_size;
df58fc94 17720
7bd374a4
MR
17721 /* Add an extra nop if the jump has no compact form and we need
17722 to fill the delay slot. */
ce8ad872 17723 if ((!pic || al) && nods)
7bd374a4
MR
17724 length += (fragp
17725 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17726 : short_insn_size);
17727
df58fc94
RS
17728 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17729
17730 <brneg> 0f # 4 bytes
8484fb75 17731 nop # 2/4 bytes if !compact
df58fc94
RS
17732 */
17733 if (!uncond)
8484fb75 17734 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17735 }
7bd374a4
MR
17736 else if (nods)
17737 {
17738 /* Add an extra nop to fill the delay slot. */
17739 gas_assert (fragp);
17740 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17741 }
df58fc94
RS
17742
17743 return length;
17744}
17745
17746/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17747 bit accordingly. */
17748
17749static int
17750relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17751{
17752 bfd_boolean toofar;
17753
df58fc94
RS
17754 if (fragp
17755 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17756 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17757 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17758 {
17759 addressT addr;
17760 offsetT val;
17761 int type;
17762
17763 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17764 /* Ignore the low bit in the target, since it will be set
17765 for a text label. */
17766 if ((val & 1) != 0)
17767 --val;
17768
17769 /* Assume this is a 2-byte branch. */
17770 addr = fragp->fr_address + fragp->fr_fix + 2;
17771
17772 /* We try to avoid the infinite loop by not adding 2 more bytes for
17773 long branches. */
17774
17775 val -= addr;
17776
17777 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17778 if (type == 'D')
17779 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17780 else if (type == 'E')
17781 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17782 else
17783 abort ();
17784 }
17785 else
17786 /* If the symbol is not defined or it's in a different segment,
17787 we emit a normal 32-bit branch. */
17788 toofar = TRUE;
17789
17790 if (fragp && update
17791 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17792 fragp->fr_subtype
17793 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17794 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17795
17796 if (toofar)
17797 return 4;
17798
17799 return 2;
17800}
17801
252b5132
RH
17802/* Estimate the size of a frag before relaxing. Unless this is the
17803 mips16, we are not really relaxing here, and the final size is
17804 encoded in the subtype information. For the mips16, we have to
17805 decide whether we are using an extended opcode or not. */
17806
252b5132 17807int
17a2f251 17808md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17809{
5919d012 17810 int change;
252b5132 17811
4a6a3df4
AO
17812 if (RELAX_BRANCH_P (fragp->fr_subtype))
17813 {
17814
b34976b6
AM
17815 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17816
4a6a3df4
AO
17817 return fragp->fr_var;
17818 }
17819
252b5132 17820 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
17821 {
17822 /* We don't want to modify the EXTENDED bit here; it might get us
17823 into infinite loops. We change it only in mips_relax_frag(). */
17824 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 17825 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
17826 else
17827 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17828 }
252b5132 17829
df58fc94
RS
17830 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17831 {
17832 int length = 4;
17833
17834 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17835 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17836 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17837 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17838 fragp->fr_var = length;
17839
17840 return length;
17841 }
17842
ce8ad872 17843 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17844 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17845 change = 0;
ce8ad872
MR
17846 else if (RELAX_PIC (fragp->fr_subtype))
17847 change = pic_need_relax (fragp->fr_symbol);
252b5132 17848 else
ce8ad872 17849 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17850
17851 if (change)
17852 {
4d7206a2 17853 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17854 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17855 }
4d7206a2
RS
17856 else
17857 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17858}
17859
17860/* This is called to see whether a reloc against a defined symbol
de7e6852 17861 should be converted into a reloc against a section. */
252b5132
RH
17862
17863int
17a2f251 17864mips_fix_adjustable (fixS *fixp)
252b5132 17865{
252b5132
RH
17866 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17867 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17868 return 0;
a161fe53 17869
252b5132
RH
17870 if (fixp->fx_addsy == NULL)
17871 return 1;
a161fe53 17872
2f0c68f2
CM
17873 /* Allow relocs used for EH tables. */
17874 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17875 return 1;
17876
de7e6852
RS
17877 /* If symbol SYM is in a mergeable section, relocations of the form
17878 SYM + 0 can usually be made section-relative. The mergeable data
17879 is then identified by the section offset rather than by the symbol.
17880
17881 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17882 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17883 need to recalculate the complete offset in order to correctly identify
17884 the merge data.
17885
33eaf5de 17886 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17887 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17888 placed anywhere. Rather than break backwards compatibility by changing
17889 this, it seems better not to force the issue, and instead keep the
17890 original symbol. This will work with either linker behavior. */
738e5348 17891 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17892 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17893 && HAVE_IN_PLACE_ADDENDS
17894 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17895 return 0;
17896
97f50151
MR
17897 /* There is no place to store an in-place offset for JALR relocations. */
17898 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17899 return 0;
17900
17901 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17902 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17903 start address of the symbol's containing section.
17904
17905 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17906 section relative to allow linker relaxations to be performed later on. */
97f50151 17907 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17908 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17909 return 0;
17910
b314ec0e
RS
17911 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17912 to a floating-point stub. The same is true for non-R_MIPS16_26
17913 relocations against MIPS16 functions; in this case, the stub becomes
17914 the function's canonical address.
17915
17916 Floating-point stubs are stored in unique .mips16.call.* or
17917 .mips16.fn.* sections. If a stub T for function F is in section S,
17918 the first relocation in section S must be against F; this is how the
17919 linker determines the target function. All relocations that might
17920 resolve to T must also be against F. We therefore have the following
17921 restrictions, which are given in an intentionally-redundant way:
17922
17923 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17924 symbols.
17925
17926 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17927 if that stub might be used.
17928
17929 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17930 symbols.
17931
17932 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17933 that stub might be used.
17934
17935 There is a further restriction:
17936
df58fc94 17937 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 17938 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
17939 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17940 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17941 against MIPS16 or microMIPS symbols because we need to keep the
17942 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
17943 detection and JAL or BAL to JALX instruction conversion in the
17944 linker.
b314ec0e 17945
df58fc94 17946 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 17947 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 17948 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
17949
17950 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17951 relocation against some symbol R, no relocation against R may be
17952 reduced. (Note that this deals with (2) as well as (1) because
17953 relocations against global symbols will never be reduced on ELF
17954 targets.) This approach is a little simpler than trying to detect
17955 stub sections, and gives the "all or nothing" per-symbol consistency
17956 that we have for MIPS16 symbols. */
f3ded42a 17957 if (fixp->fx_subsy == NULL
30c09090 17958 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 17959 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
17960 && (jmp_reloc_p (fixp->fx_r_type)
17961 || b_reloc_p (fixp->fx_r_type)))
44d3da23 17962 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 17963 return 0;
a161fe53 17964
252b5132
RH
17965 return 1;
17966}
17967
17968/* Translate internal representation of relocation info to BFD target
17969 format. */
17970
17971arelent **
17a2f251 17972tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17973{
17974 static arelent *retval[4];
17975 arelent *reloc;
17976 bfd_reloc_code_real_type code;
17977
4b0cff4e 17978 memset (retval, 0, sizeof(retval));
325801bd
TS
17979 reloc = retval[0] = XCNEW (arelent);
17980 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 17981 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17982 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17983
bad36eac
DJ
17984 if (fixp->fx_pcrel)
17985 {
df58fc94 17986 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 17987 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
17988 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17989 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 17990 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
17991 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17992 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17993 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17994 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17995 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17996 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17997 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
17998
17999 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18000 Relocations want only the symbol offset. */
51f6035b
MR
18001 switch (fixp->fx_r_type)
18002 {
18003 case BFD_RELOC_MIPS_18_PCREL_S3:
18004 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18005 break;
18006 default:
18007 reloc->addend = fixp->fx_addnumber + reloc->address;
18008 break;
18009 }
bad36eac 18010 }
17c6c9d9
MR
18011 else if (HAVE_IN_PLACE_ADDENDS
18012 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18013 && (read_compressed_insn (fixp->fx_frag->fr_literal
18014 + fixp->fx_where, 4) >> 26) == 0x3c)
18015 {
18016 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18017 addend accordingly. */
18018 reloc->addend = fixp->fx_addnumber >> 1;
18019 }
bad36eac
DJ
18020 else
18021 reloc->addend = fixp->fx_addnumber;
252b5132 18022
438c16b8
TS
18023 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18024 entry to be used in the relocation's section offset. */
18025 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18026 {
18027 reloc->address = reloc->addend;
18028 reloc->addend = 0;
18029 }
18030
252b5132 18031 code = fixp->fx_r_type;
252b5132 18032
bad36eac 18033 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18034 if (reloc->howto == NULL)
18035 {
18036 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18037 _("cannot represent %s relocation in this object file"
18038 " format"),
252b5132
RH
18039 bfd_get_reloc_code_name (code));
18040 retval[0] = NULL;
18041 }
18042
18043 return retval;
18044}
18045
18046/* Relax a machine dependent frag. This returns the amount by which
18047 the current size of the frag should change. */
18048
18049int
17a2f251 18050mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18051{
4a6a3df4
AO
18052 if (RELAX_BRANCH_P (fragp->fr_subtype))
18053 {
18054 offsetT old_var = fragp->fr_var;
b34976b6
AM
18055
18056 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18057
18058 return fragp->fr_var - old_var;
18059 }
18060
df58fc94
RS
18061 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18062 {
18063 offsetT old_var = fragp->fr_var;
18064 offsetT new_var = 4;
18065
18066 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18067 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18068 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18069 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18070 fragp->fr_var = new_var;
18071
18072 return new_var - old_var;
18073 }
18074
252b5132
RH
18075 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18076 return 0;
18077
8507b6e7 18078 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18079 {
8507b6e7
MR
18080 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18081 {
18082 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18083 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18084 }
18085 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18086 {
18087 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18088 return -2;
18089 }
18090 else
18091 return 0;
18092 }
18093 else if (!mips16_macro_frag (fragp, sec, stretch))
18094 {
18095 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18096 {
18097 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18098 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18099 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18100 }
18101 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18102 {
18103 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18104 return 2;
18105 }
18106 else
252b5132 18107 return 0;
252b5132
RH
18108 }
18109 else
18110 {
8507b6e7 18111 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18112 return 0;
8507b6e7
MR
18113 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18114 {
18115 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18116 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18117 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18118 }
18119 else
18120 {
18121 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18122 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18123 }
252b5132
RH
18124 }
18125
18126 return 0;
18127}
18128
18129/* Convert a machine dependent frag. */
18130
18131void
17a2f251 18132md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18133{
4a6a3df4
AO
18134 if (RELAX_BRANCH_P (fragp->fr_subtype))
18135 {
4d68580a 18136 char *buf;
4a6a3df4 18137 unsigned long insn;
4a6a3df4 18138 fixS *fixp;
b34976b6 18139
4d68580a
RS
18140 buf = fragp->fr_literal + fragp->fr_fix;
18141 insn = read_insn (buf);
b34976b6 18142
4a6a3df4
AO
18143 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18144 {
18145 /* We generate a fixup instead of applying it right now
18146 because, if there are linker relaxations, we're going to
18147 need the relocations. */
bbd27b76
MR
18148 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18149 fragp->fr_symbol, fragp->fr_offset,
18150 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18151 fixp->fx_file = fragp->fr_file;
18152 fixp->fx_line = fragp->fr_line;
b34976b6 18153
4d68580a 18154 buf = write_insn (buf, insn);
4a6a3df4
AO
18155 }
18156 else
18157 {
18158 int i;
18159
18160 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18161 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18162
18163 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18164 goto uncond;
18165
18166 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18167 {
18168 /* Reverse the branch. */
18169 switch ((insn >> 28) & 0xf)
18170 {
18171 case 4:
56d438b1
CF
18172 if ((insn & 0xff000000) == 0x47000000
18173 || (insn & 0xff600000) == 0x45600000)
18174 {
18175 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18176 reversed by tweaking bit 23. */
18177 insn ^= 0x00800000;
18178 }
18179 else
18180 {
18181 /* bc[0-3][tf]l? instructions can have the condition
18182 reversed by tweaking a single TF bit, and their
18183 opcodes all have 0x4???????. */
18184 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18185 insn ^= 0x00010000;
18186 }
4a6a3df4
AO
18187 break;
18188
18189 case 0:
18190 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18191 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18192 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18193 insn ^= 0x00010000;
18194 break;
b34976b6 18195
4a6a3df4
AO
18196 case 1:
18197 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18198 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18199 insn ^= 0x04000000;
18200 break;
18201
18202 default:
18203 abort ();
18204 }
18205 }
18206
18207 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18208 {
18209 /* Clear the and-link bit. */
9c2799c2 18210 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18211
54f4ddb3
TS
18212 /* bltzal 0x04100000 bgezal 0x04110000
18213 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18214 insn &= ~0x00100000;
18215 }
18216
18217 /* Branch over the branch (if the branch was likely) or the
18218 full jump (not likely case). Compute the offset from the
18219 current instruction to branch to. */
18220 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18221 i = 16;
18222 else
18223 {
18224 /* How many bytes in instructions we've already emitted? */
4d68580a 18225 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18226 /* How many bytes in instructions from here to the end? */
18227 i = fragp->fr_var - i;
18228 }
18229 /* Convert to instruction count. */
18230 i >>= 2;
18231 /* Branch counts from the next instruction. */
b34976b6 18232 i--;
4a6a3df4
AO
18233 insn |= i;
18234 /* Branch over the jump. */
4d68580a 18235 buf = write_insn (buf, insn);
4a6a3df4 18236
54f4ddb3 18237 /* nop */
4d68580a 18238 buf = write_insn (buf, 0);
4a6a3df4
AO
18239
18240 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18241 {
18242 /* beql $0, $0, 2f */
18243 insn = 0x50000000;
18244 /* Compute the PC offset from the current instruction to
18245 the end of the variable frag. */
18246 /* How many bytes in instructions we've already emitted? */
4d68580a 18247 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18248 /* How many bytes in instructions from here to the end? */
18249 i = fragp->fr_var - i;
18250 /* Convert to instruction count. */
18251 i >>= 2;
18252 /* Don't decrement i, because we want to branch over the
18253 delay slot. */
4a6a3df4 18254 insn |= i;
4a6a3df4 18255
4d68580a
RS
18256 buf = write_insn (buf, insn);
18257 buf = write_insn (buf, 0);
4a6a3df4
AO
18258 }
18259
18260 uncond:
ce8ad872 18261 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18262 {
18263 /* j or jal. */
18264 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18265 ? 0x0c000000 : 0x08000000);
4a6a3df4 18266
bbd27b76
MR
18267 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18268 fragp->fr_symbol, fragp->fr_offset,
18269 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18270 fixp->fx_file = fragp->fr_file;
18271 fixp->fx_line = fragp->fr_line;
18272
4d68580a 18273 buf = write_insn (buf, insn);
4a6a3df4
AO
18274 }
18275 else
18276 {
66b3e8da
MR
18277 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18278
4a6a3df4 18279 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18280 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18281 insn |= at << OP_SH_RT;
4a6a3df4 18282
bbd27b76
MR
18283 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18284 fragp->fr_symbol, fragp->fr_offset,
18285 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18286 fixp->fx_file = fragp->fr_file;
18287 fixp->fx_line = fragp->fr_line;
18288
4d68580a 18289 buf = write_insn (buf, insn);
b34976b6 18290
4a6a3df4 18291 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18292 /* nop */
18293 buf = write_insn (buf, 0);
4a6a3df4
AO
18294
18295 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18296 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18297 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18298
bbd27b76
MR
18299 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18300 fragp->fr_symbol, fragp->fr_offset,
18301 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18302 fixp->fx_file = fragp->fr_file;
18303 fixp->fx_line = fragp->fr_line;
b34976b6 18304
4d68580a 18305 buf = write_insn (buf, insn);
4a6a3df4
AO
18306
18307 /* j(al)r $at. */
18308 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18309 insn = 0x0000f809;
4a6a3df4 18310 else
66b3e8da
MR
18311 insn = 0x00000008;
18312 insn |= at << OP_SH_RS;
4a6a3df4 18313
4d68580a 18314 buf = write_insn (buf, insn);
4a6a3df4
AO
18315 }
18316 }
18317
4a6a3df4 18318 fragp->fr_fix += fragp->fr_var;
4d68580a 18319 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18320 return;
18321 }
18322
df58fc94
RS
18323 /* Relax microMIPS branches. */
18324 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18325 {
4d68580a 18326 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18327 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18328 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18329 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18330 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18331 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18332 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18333 bfd_boolean short_ds;
df58fc94 18334 unsigned long insn;
df58fc94
RS
18335 fixS *fixp;
18336
df58fc94
RS
18337 fragp->fr_fix += fragp->fr_var;
18338
18339 /* Handle 16-bit branches that fit or are forced to fit. */
18340 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18341 {
18342 /* We generate a fixup instead of applying it right now,
18343 because if there is linker relaxation, we're going to
18344 need the relocations. */
834a65aa
MR
18345 switch (type)
18346 {
18347 case 'D':
18348 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18349 fragp->fr_symbol, fragp->fr_offset,
18350 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18351 break;
18352 case 'E':
18353 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18354 fragp->fr_symbol, fragp->fr_offset,
18355 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18356 break;
18357 default:
18358 abort ();
18359 }
df58fc94
RS
18360
18361 fixp->fx_file = fragp->fr_file;
18362 fixp->fx_line = fragp->fr_line;
18363
18364 /* These relocations can have an addend that won't fit in
18365 2 octets. */
18366 fixp->fx_no_overflow = 1;
18367
18368 return;
18369 }
18370
2309ddf2 18371 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18372 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18373 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18374 {
18375 /* We generate a fixup instead of applying it right now,
18376 because if there is linker relaxation, we're going to
18377 need the relocations. */
bbd27b76
MR
18378 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18379 fragp->fr_symbol, fragp->fr_offset,
18380 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18381 fixp->fx_file = fragp->fr_file;
18382 fixp->fx_line = fragp->fr_line;
18383
18384 if (type == 0)
7bd374a4
MR
18385 {
18386 insn = read_compressed_insn (buf, 4);
18387 buf += 4;
18388
18389 if (nods)
18390 {
18391 /* Check the short-delay-slot bit. */
18392 if (!al || (insn & 0x02000000) != 0)
18393 buf = write_compressed_insn (buf, 0x0c00, 2);
18394 else
18395 buf = write_compressed_insn (buf, 0x00000000, 4);
18396 }
18397
18398 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18399 return;
18400 }
df58fc94
RS
18401 }
18402
18403 /* Relax 16-bit branches to 32-bit branches. */
18404 if (type != 0)
18405 {
4d68580a 18406 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18407
18408 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18409 insn = 0x94000000; /* beq */
18410 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18411 {
18412 unsigned long regno;
18413
18414 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18415 regno = micromips_to_32_reg_d_map [regno];
18416 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18417 insn |= regno << MICROMIPSOP_SH_RS;
18418 }
18419 else
18420 abort ();
18421
18422 /* Nothing else to do, just write it out. */
18423 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18424 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18425 {
4d68580a 18426 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18427 if (nods)
18428 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18429 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18430 return;
18431 }
18432 }
18433 else
4d68580a 18434 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18435
18436 /* Relax 32-bit branches to a sequence of instructions. */
18437 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18438 _("relaxed out-of-range branch into a jump"));
df58fc94 18439
2309ddf2 18440 /* Set the short-delay-slot bit. */
7bd374a4 18441 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18442
18443 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18444 {
18445 symbolS *l;
18446
18447 /* Reverse the branch. */
18448 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18449 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18450 insn ^= 0x20000000;
18451 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18452 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18453 || (insn & 0xffe00000) == 0x40800000 /* blez */
18454 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18455 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18456 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18457 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18458 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18459 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18460 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18461 insn ^= 0x00400000;
18462 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18463 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18464 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18465 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18466 insn ^= 0x00200000;
56d438b1
CF
18467 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18468 BNZ.df */
18469 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18470 BNZ.V */
18471 insn ^= 0x00800000;
df58fc94
RS
18472 else
18473 abort ();
18474
18475 if (al)
18476 {
18477 /* Clear the and-link and short-delay-slot bits. */
18478 gas_assert ((insn & 0xfda00000) == 0x40200000);
18479
18480 /* bltzal 0x40200000 bgezal 0x40600000 */
18481 /* bltzals 0x42200000 bgezals 0x42600000 */
18482 insn &= ~0x02200000;
18483 }
18484
18485 /* Make a label at the end for use with the branch. */
18486 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18487 micromips_label_inc ();
f3ded42a 18488 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18489
18490 /* Refer to it. */
4d68580a
RS
18491 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18492 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18493 fixp->fx_file = fragp->fr_file;
18494 fixp->fx_line = fragp->fr_line;
18495
18496 /* Branch over the jump. */
4d68580a 18497 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18498
df58fc94 18499 if (!compact)
8484fb75
MR
18500 {
18501 /* nop */
18502 if (insn32)
18503 buf = write_compressed_insn (buf, 0x00000000, 4);
18504 else
18505 buf = write_compressed_insn (buf, 0x0c00, 2);
18506 }
df58fc94
RS
18507 }
18508
ce8ad872 18509 if (!pic)
df58fc94 18510 {
7bd374a4
MR
18511 unsigned long jal = (short_ds || nods
18512 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18513
df58fc94
RS
18514 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18515 insn = al ? jal : 0xd4000000;
18516
bbd27b76
MR
18517 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18518 fragp->fr_symbol, fragp->fr_offset,
18519 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18520 fixp->fx_file = fragp->fr_file;
18521 fixp->fx_line = fragp->fr_line;
18522
4d68580a 18523 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18524
7bd374a4 18525 if (compact || nods)
8484fb75
MR
18526 {
18527 /* nop */
18528 if (insn32)
18529 buf = write_compressed_insn (buf, 0x00000000, 4);
18530 else
18531 buf = write_compressed_insn (buf, 0x0c00, 2);
18532 }
df58fc94
RS
18533 }
18534 else
18535 {
18536 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18537
18538 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18539 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18540 insn |= at << MICROMIPSOP_SH_RT;
18541
bbd27b76
MR
18542 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18543 fragp->fr_symbol, fragp->fr_offset,
18544 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18545 fixp->fx_file = fragp->fr_file;
18546 fixp->fx_line = fragp->fr_line;
18547
4d68580a 18548 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18549
18550 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18551 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18552 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18553
bbd27b76
MR
18554 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18555 fragp->fr_symbol, fragp->fr_offset,
18556 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18557 fixp->fx_file = fragp->fr_file;
18558 fixp->fx_line = fragp->fr_line;
18559
4d68580a 18560 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18561
8484fb75
MR
18562 if (insn32)
18563 {
18564 /* jr/jalr $at */
18565 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18566 insn |= at << MICROMIPSOP_SH_RS;
18567
18568 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18569
7bd374a4 18570 if (compact || nods)
8484fb75
MR
18571 /* nop */
18572 buf = write_compressed_insn (buf, 0x00000000, 4);
18573 }
18574 else
18575 {
18576 /* jr/jrc/jalr/jalrs $at */
18577 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18578 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18579
18580 insn = al ? jalr : jr;
18581 insn |= at << MICROMIPSOP_SH_MJ;
18582
18583 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18584 if (al && nods)
18585 {
18586 /* nop */
18587 if (short_ds)
18588 buf = write_compressed_insn (buf, 0x0c00, 2);
18589 else
18590 buf = write_compressed_insn (buf, 0x00000000, 4);
18591 }
8484fb75 18592 }
df58fc94
RS
18593 }
18594
4d68580a 18595 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18596 return;
18597 }
18598
252b5132
RH
18599 if (RELAX_MIPS16_P (fragp->fr_subtype))
18600 {
18601 int type;
3ccad066 18602 const struct mips_int_operand *operand;
252b5132 18603 offsetT val;
5c04167a 18604 char *buf;
8507b6e7 18605 unsigned int user_length;
9d862524 18606 bfd_boolean need_reloc;
252b5132 18607 unsigned long insn;
8507b6e7 18608 bfd_boolean mac;
5c04167a 18609 bfd_boolean ext;
88a7ef16 18610 segT symsec;
252b5132
RH
18611
18612 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18613 operand = mips16_immed_operand (type, FALSE);
252b5132 18614
8507b6e7 18615 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18616 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18617 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18618
18619 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18620 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18621 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18622 ? asec != symsec
18623 : !bfd_is_abs_section (symsec)));
18624
8507b6e7 18625 if (operand->root.type == OP_PCREL && !mac)
252b5132 18626 {
3ccad066 18627 const struct mips_pcrel_operand *pcrel_op;
252b5132 18628
3ccad066 18629 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18630
14f72d45 18631 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18632 {
37b2d327
MR
18633 if (!mips_ignore_branch_isa
18634 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
18635 as_bad_where (fragp->fr_file, fragp->fr_line,
18636 _("branch to a symbol in another ISA mode"));
18637 else if ((fragp->fr_offset & 0x1) != 0)
18638 as_bad_where (fragp->fr_file, fragp->fr_line,
18639 _("branch to misaligned address (0x%lx)"),
18640 (long) val);
252b5132 18641 }
252b5132 18642
14f72d45 18643 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18644
18645 /* Make sure the section winds up with the alignment we have
18646 assumed. */
3ccad066
RS
18647 if (operand->shift > 0)
18648 record_alignment (asec, operand->shift);
252b5132
RH
18649 }
18650
8507b6e7
MR
18651 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18652 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18653 {
18654 if (mac)
18655 as_warn_where (fragp->fr_file, fragp->fr_line,
18656 _("macro instruction expanded into multiple "
18657 "instructions in a branch delay slot"));
18658 else if (ext)
18659 as_warn_where (fragp->fr_file, fragp->fr_line,
18660 _("extended instruction in a branch delay slot"));
18661 }
18662 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18663 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18664 _("macro instruction expanded into multiple "
18665 "instructions"));
252b5132 18666
5c04167a 18667 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18668
4d68580a 18669 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18670 if (ext)
18671 insn |= MIPS16_EXTEND;
252b5132 18672
5c04167a
RS
18673 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18674 user_length = 4;
18675 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18676 user_length = 2;
18677 else
18678 user_length = 0;
18679
8507b6e7 18680 if (mac)
c9775dde 18681 {
8507b6e7
MR
18682 unsigned long reg;
18683 unsigned long new;
18684 unsigned long op;
25499ac7 18685 bfd_boolean e2;
8507b6e7
MR
18686
18687 gas_assert (type == 'A' || type == 'B' || type == 'E');
18688 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18689
25499ac7
MR
18690 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18691
8507b6e7 18692 if (need_reloc)
c9775dde 18693 {
8507b6e7
MR
18694 fixS *fixp;
18695
18696 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18697
18698 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18699 fragp->fr_symbol, fragp->fr_offset,
18700 FALSE, BFD_RELOC_MIPS16_HI16_S);
18701 fixp->fx_file = fragp->fr_file;
18702 fixp->fx_line = fragp->fr_line;
18703
25499ac7 18704 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
18705 fragp->fr_symbol, fragp->fr_offset,
18706 FALSE, BFD_RELOC_MIPS16_LO16);
18707 fixp->fx_file = fragp->fr_file;
18708 fixp->fx_line = fragp->fr_line;
18709
18710 val = 0;
18711 }
18712
18713 switch (insn & 0xf800)
18714 {
18715 case 0x0800: /* ADDIU */
18716 reg = (insn >> 8) & 0x7;
18717 op = 0xf0004800 | (reg << 8);
c9775dde 18718 break;
8507b6e7
MR
18719 case 0xb000: /* LW */
18720 reg = (insn >> 8) & 0x7;
18721 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18722 break;
8507b6e7
MR
18723 case 0xf800: /* I64 */
18724 reg = (insn >> 5) & 0x7;
18725 switch (insn & 0x0700)
18726 {
18727 case 0x0400: /* LD */
18728 op = 0xf0003800 | (reg << 8) | (reg << 5);
18729 break;
18730 case 0x0600: /* DADDIU */
18731 op = 0xf000fd00 | (reg << 5);
18732 break;
18733 default:
18734 abort ();
18735 }
18736 break;
18737 default:
18738 abort ();
c9775dde 18739 }
8507b6e7 18740
25499ac7 18741 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
18742 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18743 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
18744 if (!e2)
18745 {
18746 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18747 buf = write_compressed_insn (buf, new, 4);
18748 }
8507b6e7
MR
18749 op |= mips16_immed_extend (val, 16);
18750 buf = write_compressed_insn (buf, op, 4);
18751
25499ac7 18752 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
18753 }
18754 else
18755 {
18756 unsigned int length = ext ? 4 : 2;
18757
18758 if (need_reloc)
c9775dde 18759 {
8507b6e7 18760 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 18761 fixS *fixp;
c9775dde 18762
8507b6e7
MR
18763 switch (type)
18764 {
18765 case 'p':
18766 case 'q':
18767 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18768 break;
18769 default:
18770 break;
18771 }
18772 if (mac || reloc == BFD_RELOC_NONE)
18773 as_bad_where (fragp->fr_file, fragp->fr_line,
18774 _("unsupported relocation"));
18775 else if (ext)
18776 {
bbd27b76
MR
18777 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18778 fragp->fr_symbol, fragp->fr_offset,
18779 TRUE, reloc);
8507b6e7
MR
18780 fixp->fx_file = fragp->fr_file;
18781 fixp->fx_line = fragp->fr_line;
18782 }
18783 else
18784 as_bad_where (fragp->fr_file, fragp->fr_line,
18785 _("invalid unextended operand value"));
c9775dde 18786 }
eefc3365 18787 else
8507b6e7
MR
18788 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18789 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18790
8507b6e7
MR
18791 gas_assert (mips16_opcode_length (insn) == length);
18792 write_compressed_insn (buf, insn, length);
18793 fragp->fr_fix += length;
18794 }
252b5132
RH
18795 }
18796 else
18797 {
df58fc94
RS
18798 relax_substateT subtype = fragp->fr_subtype;
18799 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18800 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18801 int first, second;
18802 fixS *fixp;
252b5132 18803
df58fc94
RS
18804 first = RELAX_FIRST (subtype);
18805 second = RELAX_SECOND (subtype);
4d7206a2 18806 fixp = (fixS *) fragp->fr_opcode;
252b5132 18807
df58fc94
RS
18808 /* If the delay slot chosen does not match the size of the instruction,
18809 then emit a warning. */
18810 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18811 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18812 {
18813 relax_substateT s;
18814 const char *msg;
18815
18816 s = subtype & (RELAX_DELAY_SLOT_16BIT
18817 | RELAX_DELAY_SLOT_SIZE_FIRST
18818 | RELAX_DELAY_SLOT_SIZE_SECOND);
18819 msg = macro_warning (s);
18820 if (msg != NULL)
db9b2be4 18821 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18822 subtype &= ~s;
18823 }
18824
584892a6 18825 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18826 if (use_second == second_longer)
584892a6 18827 {
df58fc94
RS
18828 relax_substateT s;
18829 const char *msg;
18830
18831 s = (subtype
18832 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18833 msg = macro_warning (s);
18834 if (msg != NULL)
db9b2be4 18835 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18836 subtype &= ~s;
584892a6
RS
18837 }
18838
4d7206a2
RS
18839 /* Go through all the fixups for the first sequence. Disable them
18840 (by marking them as done) if we're going to use the second
18841 sequence instead. */
18842 while (fixp
18843 && fixp->fx_frag == fragp
18844 && fixp->fx_where < fragp->fr_fix - second)
18845 {
df58fc94 18846 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18847 fixp->fx_done = 1;
18848 fixp = fixp->fx_next;
18849 }
252b5132 18850
4d7206a2
RS
18851 /* Go through the fixups for the second sequence. Disable them if
18852 we're going to use the first sequence, otherwise adjust their
18853 addresses to account for the relaxation. */
18854 while (fixp && fixp->fx_frag == fragp)
18855 {
df58fc94 18856 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18857 fixp->fx_where -= first;
18858 else
18859 fixp->fx_done = 1;
18860 fixp = fixp->fx_next;
18861 }
18862
18863 /* Now modify the frag contents. */
df58fc94 18864 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18865 {
18866 char *start;
18867
18868 start = fragp->fr_literal + fragp->fr_fix - first - second;
18869 memmove (start, start + first, second);
18870 fragp->fr_fix -= first;
18871 }
18872 else
18873 fragp->fr_fix -= second;
252b5132
RH
18874 }
18875}
18876
252b5132
RH
18877/* This function is called after the relocs have been generated.
18878 We've been storing mips16 text labels as odd. Here we convert them
18879 back to even for the convenience of the debugger. */
18880
18881void
17a2f251 18882mips_frob_file_after_relocs (void)
252b5132
RH
18883{
18884 asymbol **syms;
18885 unsigned int count, i;
18886
252b5132
RH
18887 syms = bfd_get_outsymbols (stdoutput);
18888 count = bfd_get_symcount (stdoutput);
18889 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18890 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18891 && ((*syms)->value & 1) != 0)
18892 {
18893 (*syms)->value &= ~1;
18894 /* If the symbol has an odd size, it was probably computed
18895 incorrectly, so adjust that as well. */
18896 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18897 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18898 }
252b5132
RH
18899}
18900
a1facbec
MR
18901/* This function is called whenever a label is defined, including fake
18902 labels instantiated off the dot special symbol. It is used when
18903 handling branch delays; if a branch has a label, we assume we cannot
18904 move it. This also bumps the value of the symbol by 1 in compressed
18905 code. */
252b5132 18906
e1b47bd5 18907static void
a1facbec 18908mips_record_label (symbolS *sym)
252b5132 18909{
a8dbcb85 18910 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18911 struct insn_label_list *l;
18912
18913 if (free_insn_labels == NULL)
325801bd 18914 l = XNEW (struct insn_label_list);
252b5132
RH
18915 else
18916 {
18917 l = free_insn_labels;
18918 free_insn_labels = l->next;
18919 }
18920
18921 l->label = sym;
a8dbcb85
TS
18922 l->next = si->label_list;
18923 si->label_list = l;
a1facbec 18924}
07a53e5c 18925
a1facbec
MR
18926/* This function is called as tc_frob_label() whenever a label is defined
18927 and adds a DWARF-2 record we only want for true labels. */
18928
18929void
18930mips_define_label (symbolS *sym)
18931{
18932 mips_record_label (sym);
07a53e5c 18933 dwarf2_emit_label (sym);
252b5132 18934}
e1b47bd5
RS
18935
18936/* This function is called by tc_new_dot_label whenever a new dot symbol
18937 is defined. */
18938
18939void
18940mips_add_dot_label (symbolS *sym)
18941{
18942 mips_record_label (sym);
18943 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18944 mips_compressed_mark_label (sym);
18945}
252b5132 18946\f
351cdf24
MF
18947/* Converting ASE flags from internal to .MIPS.abiflags values. */
18948static unsigned int
18949mips_convert_ase_flags (int ase)
18950{
18951 unsigned int ext_ases = 0;
18952
18953 if (ase & ASE_DSP)
18954 ext_ases |= AFL_ASE_DSP;
18955 if (ase & ASE_DSPR2)
18956 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
18957 if (ase & ASE_DSPR3)
18958 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
18959 if (ase & ASE_EVA)
18960 ext_ases |= AFL_ASE_EVA;
18961 if (ase & ASE_MCU)
18962 ext_ases |= AFL_ASE_MCU;
18963 if (ase & ASE_MDMX)
18964 ext_ases |= AFL_ASE_MDMX;
18965 if (ase & ASE_MIPS3D)
18966 ext_ases |= AFL_ASE_MIPS3D;
18967 if (ase & ASE_MT)
18968 ext_ases |= AFL_ASE_MT;
18969 if (ase & ASE_SMARTMIPS)
18970 ext_ases |= AFL_ASE_SMARTMIPS;
18971 if (ase & ASE_VIRT)
18972 ext_ases |= AFL_ASE_VIRT;
18973 if (ase & ASE_MSA)
18974 ext_ases |= AFL_ASE_MSA;
18975 if (ase & ASE_XPA)
18976 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
18977 if (ase & ASE_MIPS16E2)
18978 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
351cdf24
MF
18979
18980 return ext_ases;
18981}
252b5132
RH
18982/* Some special processing for a MIPS ELF file. */
18983
18984void
17a2f251 18985mips_elf_final_processing (void)
252b5132 18986{
351cdf24
MF
18987 int fpabi;
18988 Elf_Internal_ABIFlags_v0 flags;
18989
18990 flags.version = 0;
18991 flags.isa_rev = 0;
18992 switch (file_mips_opts.isa)
18993 {
18994 case INSN_ISA1:
18995 flags.isa_level = 1;
18996 break;
18997 case INSN_ISA2:
18998 flags.isa_level = 2;
18999 break;
19000 case INSN_ISA3:
19001 flags.isa_level = 3;
19002 break;
19003 case INSN_ISA4:
19004 flags.isa_level = 4;
19005 break;
19006 case INSN_ISA5:
19007 flags.isa_level = 5;
19008 break;
19009 case INSN_ISA32:
19010 flags.isa_level = 32;
19011 flags.isa_rev = 1;
19012 break;
19013 case INSN_ISA32R2:
19014 flags.isa_level = 32;
19015 flags.isa_rev = 2;
19016 break;
19017 case INSN_ISA32R3:
19018 flags.isa_level = 32;
19019 flags.isa_rev = 3;
19020 break;
19021 case INSN_ISA32R5:
19022 flags.isa_level = 32;
19023 flags.isa_rev = 5;
19024 break;
09c14161
MF
19025 case INSN_ISA32R6:
19026 flags.isa_level = 32;
19027 flags.isa_rev = 6;
19028 break;
351cdf24
MF
19029 case INSN_ISA64:
19030 flags.isa_level = 64;
19031 flags.isa_rev = 1;
19032 break;
19033 case INSN_ISA64R2:
19034 flags.isa_level = 64;
19035 flags.isa_rev = 2;
19036 break;
19037 case INSN_ISA64R3:
19038 flags.isa_level = 64;
19039 flags.isa_rev = 3;
19040 break;
19041 case INSN_ISA64R5:
19042 flags.isa_level = 64;
19043 flags.isa_rev = 5;
19044 break;
09c14161
MF
19045 case INSN_ISA64R6:
19046 flags.isa_level = 64;
19047 flags.isa_rev = 6;
19048 break;
351cdf24
MF
19049 }
19050
19051 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19052 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19053 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19054 : (file_mips_opts.fp == 64) ? AFL_REG_64
19055 : AFL_REG_32;
19056 flags.cpr2_size = AFL_REG_NONE;
19057 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19058 Tag_GNU_MIPS_ABI_FP);
19059 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19060 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19061 if (file_ase_mips16)
19062 flags.ases |= AFL_ASE_MIPS16;
19063 if (file_ase_micromips)
19064 flags.ases |= AFL_ASE_MICROMIPS;
19065 flags.flags1 = 0;
19066 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19067 || file_mips_opts.fp == 64)
19068 && file_mips_opts.oddspreg)
19069 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19070 flags.flags2 = 0;
19071
19072 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19073 ((Elf_External_ABIFlags_v0 *)
19074 mips_flags_frag));
19075
252b5132 19076 /* Write out the register information. */
316f5878 19077 if (mips_abi != N64_ABI)
252b5132
RH
19078 {
19079 Elf32_RegInfo s;
19080
19081 s.ri_gprmask = mips_gprmask;
19082 s.ri_cprmask[0] = mips_cprmask[0];
19083 s.ri_cprmask[1] = mips_cprmask[1];
19084 s.ri_cprmask[2] = mips_cprmask[2];
19085 s.ri_cprmask[3] = mips_cprmask[3];
19086 /* The gp_value field is set by the MIPS ELF backend. */
19087
19088 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19089 ((Elf32_External_RegInfo *)
19090 mips_regmask_frag));
19091 }
19092 else
19093 {
19094 Elf64_Internal_RegInfo s;
19095
19096 s.ri_gprmask = mips_gprmask;
19097 s.ri_pad = 0;
19098 s.ri_cprmask[0] = mips_cprmask[0];
19099 s.ri_cprmask[1] = mips_cprmask[1];
19100 s.ri_cprmask[2] = mips_cprmask[2];
19101 s.ri_cprmask[3] = mips_cprmask[3];
19102 /* The gp_value field is set by the MIPS ELF backend. */
19103
19104 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19105 ((Elf64_External_RegInfo *)
19106 mips_regmask_frag));
19107 }
19108
19109 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19110 sort of BFD interface for this. */
19111 if (mips_any_noreorder)
19112 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19113 if (mips_pic != NO_PIC)
143d77c5 19114 {
8b828383 19115 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19116 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19117 }
19118 if (mips_abicalls)
19119 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19120
b015e599
AP
19121 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19122 defined at present; this might need to change in future. */
a4672219
TS
19123 if (file_ase_mips16)
19124 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19125 if (file_ase_micromips)
19126 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19127 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19128 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19129
bdaaa2e1 19130 /* Set the MIPS ELF ABI flags. */
316f5878 19131 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19132 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19133 else if (mips_abi == O64_ABI)
252b5132 19134 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19135 else if (mips_abi == EABI_ABI)
252b5132 19136 {
bad1aba3 19137 if (file_mips_opts.gp == 64)
252b5132
RH
19138 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19139 else
19140 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19141 }
316f5878 19142 else if (mips_abi == N32_ABI)
be00bddd
TS
19143 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
19144
c9914766 19145 /* Nothing to do for N64_ABI. */
252b5132
RH
19146
19147 if (mips_32bitmode)
19148 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19149
7361da2c 19150 if (mips_nan2008 == 1)
ba92f887
MR
19151 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19152
ad3fea08 19153 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19154 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19155 Tag_GNU_MIPS_ABI_FP);
19156 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19157 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19158}
252b5132 19159\f
beae10d5 19160typedef struct proc {
9b2f1d35
EC
19161 symbolS *func_sym;
19162 symbolS *func_end_sym;
beae10d5
KH
19163 unsigned long reg_mask;
19164 unsigned long reg_offset;
19165 unsigned long fpreg_mask;
19166 unsigned long fpreg_offset;
19167 unsigned long frame_offset;
19168 unsigned long frame_reg;
19169 unsigned long pc_reg;
19170} procS;
252b5132
RH
19171
19172static procS cur_proc;
19173static procS *cur_proc_ptr;
19174static int numprocs;
19175
df58fc94
RS
19176/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19177 as "2", and a normal nop as "0". */
19178
19179#define NOP_OPCODE_MIPS 0
19180#define NOP_OPCODE_MIPS16 1
19181#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19182
19183char
19184mips_nop_opcode (void)
19185{
df58fc94
RS
19186 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19187 return NOP_OPCODE_MICROMIPS;
19188 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19189 return NOP_OPCODE_MIPS16;
19190 else
19191 return NOP_OPCODE_MIPS;
742a56fe
RS
19192}
19193
df58fc94
RS
19194/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19195 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19196
0a9ef439 19197void
17a2f251 19198mips_handle_align (fragS *fragp)
a19d8eb0 19199{
df58fc94 19200 char nop_opcode;
742a56fe 19201 char *p;
c67a084a
NC
19202 int bytes, size, excess;
19203 valueT opcode;
742a56fe 19204
0a9ef439
RH
19205 if (fragp->fr_type != rs_align_code)
19206 return;
19207
742a56fe 19208 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19209 nop_opcode = *p;
19210 switch (nop_opcode)
a19d8eb0 19211 {
df58fc94
RS
19212 case NOP_OPCODE_MICROMIPS:
19213 opcode = micromips_nop32_insn.insn_opcode;
19214 size = 4;
19215 break;
19216 case NOP_OPCODE_MIPS16:
c67a084a
NC
19217 opcode = mips16_nop_insn.insn_opcode;
19218 size = 2;
df58fc94
RS
19219 break;
19220 case NOP_OPCODE_MIPS:
19221 default:
c67a084a
NC
19222 opcode = nop_insn.insn_opcode;
19223 size = 4;
df58fc94 19224 break;
c67a084a 19225 }
a19d8eb0 19226
c67a084a
NC
19227 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19228 excess = bytes % size;
df58fc94
RS
19229
19230 /* Handle the leading part if we're not inserting a whole number of
19231 instructions, and make it the end of the fixed part of the frag.
19232 Try to fit in a short microMIPS NOP if applicable and possible,
19233 and use zeroes otherwise. */
19234 gas_assert (excess < 4);
19235 fragp->fr_fix += excess;
19236 switch (excess)
c67a084a 19237 {
df58fc94
RS
19238 case 3:
19239 *p++ = '\0';
19240 /* Fall through. */
19241 case 2:
833794fc 19242 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19243 {
4d68580a 19244 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19245 break;
19246 }
19247 *p++ = '\0';
19248 /* Fall through. */
19249 case 1:
19250 *p++ = '\0';
19251 /* Fall through. */
19252 case 0:
19253 break;
a19d8eb0 19254 }
c67a084a
NC
19255
19256 md_number_to_chars (p, opcode, size);
19257 fragp->fr_var = size;
a19d8eb0
CP
19258}
19259
252b5132 19260static long
17a2f251 19261get_number (void)
252b5132
RH
19262{
19263 int negative = 0;
19264 long val = 0;
19265
19266 if (*input_line_pointer == '-')
19267 {
19268 ++input_line_pointer;
19269 negative = 1;
19270 }
3882b010 19271 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19272 as_bad (_("expected simple number"));
252b5132
RH
19273 if (input_line_pointer[0] == '0')
19274 {
19275 if (input_line_pointer[1] == 'x')
19276 {
19277 input_line_pointer += 2;
3882b010 19278 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19279 {
19280 val <<= 4;
19281 val |= hex_value (*input_line_pointer++);
19282 }
19283 return negative ? -val : val;
19284 }
19285 else
19286 {
19287 ++input_line_pointer;
3882b010 19288 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19289 {
19290 val <<= 3;
19291 val |= *input_line_pointer++ - '0';
19292 }
19293 return negative ? -val : val;
19294 }
19295 }
3882b010 19296 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19297 {
19298 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19299 *input_line_pointer, *input_line_pointer);
956cd1d6 19300 as_warn (_("invalid number"));
252b5132
RH
19301 return -1;
19302 }
3882b010 19303 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19304 {
19305 val *= 10;
19306 val += *input_line_pointer++ - '0';
19307 }
19308 return negative ? -val : val;
19309}
19310
19311/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19312 is an initial number which is the ECOFF file index. In the non-ECOFF
19313 case .file implies DWARF-2. */
19314
19315static void
17a2f251 19316s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19317{
ecb4347a
DJ
19318 static int first_file_directive = 0;
19319
c5dd6aab
DJ
19320 if (ECOFF_DEBUGGING)
19321 {
19322 get_number ();
19323 s_app_file (0);
19324 }
19325 else
ecb4347a
DJ
19326 {
19327 char *filename;
19328
19329 filename = dwarf2_directive_file (0);
19330
19331 /* Versions of GCC up to 3.1 start files with a ".file"
19332 directive even for stabs output. Make sure that this
19333 ".file" is handled. Note that you need a version of GCC
19334 after 3.1 in order to support DWARF-2 on MIPS. */
19335 if (filename != NULL && ! first_file_directive)
19336 {
19337 (void) new_logical_line (filename, -1);
c04f5787 19338 s_app_file_string (filename, 0);
ecb4347a
DJ
19339 }
19340 first_file_directive = 1;
19341 }
c5dd6aab
DJ
19342}
19343
19344/* The .loc directive, implying DWARF-2. */
252b5132
RH
19345
19346static void
17a2f251 19347s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19348{
c5dd6aab
DJ
19349 if (!ECOFF_DEBUGGING)
19350 dwarf2_directive_loc (0);
252b5132
RH
19351}
19352
252b5132
RH
19353/* The .end directive. */
19354
19355static void
17a2f251 19356s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19357{
19358 symbolS *p;
252b5132 19359
7a621144
DJ
19360 /* Following functions need their own .frame and .cprestore directives. */
19361 mips_frame_reg_valid = 0;
19362 mips_cprestore_valid = 0;
19363
252b5132
RH
19364 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19365 {
19366 p = get_symbol ();
19367 demand_empty_rest_of_line ();
19368 }
19369 else
19370 p = NULL;
19371
14949570 19372 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19373 as_warn (_(".end not in text section"));
19374
19375 if (!cur_proc_ptr)
19376 {
1661c76c 19377 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19378 demand_empty_rest_of_line ();
19379 return;
19380 }
19381
19382 if (p != NULL)
19383 {
9c2799c2 19384 gas_assert (S_GET_NAME (p));
9b2f1d35 19385 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19386 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19387
19388 if (debug_type == DEBUG_STABS)
19389 stabs_generate_asm_endfunc (S_GET_NAME (p),
19390 S_GET_NAME (p));
252b5132
RH
19391 }
19392 else
19393 as_warn (_(".end directive missing or unknown symbol"));
19394
9b2f1d35
EC
19395 /* Create an expression to calculate the size of the function. */
19396 if (p && cur_proc_ptr)
19397 {
19398 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19399 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19400
19401 obj->size = exp;
19402 exp->X_op = O_subtract;
19403 exp->X_add_symbol = symbol_temp_new_now ();
19404 exp->X_op_symbol = p;
19405 exp->X_add_number = 0;
19406
19407 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19408 }
19409
5ff6a06c
MR
19410#ifdef md_flush_pending_output
19411 md_flush_pending_output ();
19412#endif
19413
ecb4347a 19414 /* Generate a .pdr section. */
f3ded42a 19415 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19416 {
19417 segT saved_seg = now_seg;
19418 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19419 expressionS exp;
19420 char *fragp;
252b5132 19421
9c2799c2 19422 gas_assert (pdr_seg);
ecb4347a 19423 subseg_set (pdr_seg, 0);
252b5132 19424
ecb4347a
DJ
19425 /* Write the symbol. */
19426 exp.X_op = O_symbol;
19427 exp.X_add_symbol = p;
19428 exp.X_add_number = 0;
19429 emit_expr (&exp, 4);
252b5132 19430
ecb4347a 19431 fragp = frag_more (7 * 4);
252b5132 19432
17a2f251
TS
19433 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19434 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19435 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19436 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19437 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19438 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19439 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19440
ecb4347a
DJ
19441 subseg_set (saved_seg, saved_subseg);
19442 }
252b5132
RH
19443
19444 cur_proc_ptr = NULL;
19445}
19446
19447/* The .aent and .ent directives. */
19448
19449static void
17a2f251 19450s_mips_ent (int aent)
252b5132 19451{
252b5132 19452 symbolS *symbolP;
252b5132
RH
19453
19454 symbolP = get_symbol ();
19455 if (*input_line_pointer == ',')
f9419b05 19456 ++input_line_pointer;
252b5132 19457 SKIP_WHITESPACE ();
3882b010 19458 if (ISDIGIT (*input_line_pointer)
d9a62219 19459 || *input_line_pointer == '-')
874e8986 19460 get_number ();
252b5132 19461
14949570 19462 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19463 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19464
19465 if (!aent && cur_proc_ptr)
9a41af64 19466 as_warn (_("missing .end"));
252b5132
RH
19467
19468 if (!aent)
19469 {
7a621144
DJ
19470 /* This function needs its own .frame and .cprestore directives. */
19471 mips_frame_reg_valid = 0;
19472 mips_cprestore_valid = 0;
19473
252b5132
RH
19474 cur_proc_ptr = &cur_proc;
19475 memset (cur_proc_ptr, '\0', sizeof (procS));
19476
9b2f1d35 19477 cur_proc_ptr->func_sym = symbolP;
252b5132 19478
f9419b05 19479 ++numprocs;
ecb4347a
DJ
19480
19481 if (debug_type == DEBUG_STABS)
19482 stabs_generate_asm_func (S_GET_NAME (symbolP),
19483 S_GET_NAME (symbolP));
252b5132
RH
19484 }
19485
7c0fc524
MR
19486 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19487
252b5132
RH
19488 demand_empty_rest_of_line ();
19489}
19490
19491/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19492 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19493 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19494 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19495 symbol table (in the mdebug section). */
19496
19497static void
17a2f251 19498s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19499{
f3ded42a
RS
19500 if (ECOFF_DEBUGGING)
19501 s_ignore (ignore);
19502 else
ecb4347a
DJ
19503 {
19504 long val;
252b5132 19505
ecb4347a
DJ
19506 if (cur_proc_ptr == (procS *) NULL)
19507 {
19508 as_warn (_(".frame outside of .ent"));
19509 demand_empty_rest_of_line ();
19510 return;
19511 }
252b5132 19512
ecb4347a
DJ
19513 cur_proc_ptr->frame_reg = tc_get_register (1);
19514
19515 SKIP_WHITESPACE ();
19516 if (*input_line_pointer++ != ','
19517 || get_absolute_expression_and_terminator (&val) != ',')
19518 {
1661c76c 19519 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19520 --input_line_pointer;
19521 demand_empty_rest_of_line ();
19522 return;
19523 }
252b5132 19524
ecb4347a
DJ
19525 cur_proc_ptr->frame_offset = val;
19526 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19527
252b5132 19528 demand_empty_rest_of_line ();
252b5132 19529 }
252b5132
RH
19530}
19531
bdaaa2e1
KH
19532/* The .fmask and .mask directives. If the mdebug section is present
19533 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19534 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19535 information correctly. We can't use the ecoff routines because they
252b5132
RH
19536 make reference to the ecoff symbol table (in the mdebug section). */
19537
19538static void
17a2f251 19539s_mips_mask (int reg_type)
252b5132 19540{
f3ded42a
RS
19541 if (ECOFF_DEBUGGING)
19542 s_ignore (reg_type);
19543 else
252b5132 19544 {
ecb4347a 19545 long mask, off;
252b5132 19546
ecb4347a
DJ
19547 if (cur_proc_ptr == (procS *) NULL)
19548 {
19549 as_warn (_(".mask/.fmask outside of .ent"));
19550 demand_empty_rest_of_line ();
19551 return;
19552 }
252b5132 19553
ecb4347a
DJ
19554 if (get_absolute_expression_and_terminator (&mask) != ',')
19555 {
1661c76c 19556 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19557 --input_line_pointer;
19558 demand_empty_rest_of_line ();
19559 return;
19560 }
252b5132 19561
ecb4347a
DJ
19562 off = get_absolute_expression ();
19563
19564 if (reg_type == 'F')
19565 {
19566 cur_proc_ptr->fpreg_mask = mask;
19567 cur_proc_ptr->fpreg_offset = off;
19568 }
19569 else
19570 {
19571 cur_proc_ptr->reg_mask = mask;
19572 cur_proc_ptr->reg_offset = off;
19573 }
19574
19575 demand_empty_rest_of_line ();
252b5132 19576 }
252b5132
RH
19577}
19578
316f5878
RS
19579/* A table describing all the processors gas knows about. Names are
19580 matched in the order listed.
e7af610e 19581
316f5878
RS
19582 To ease comparison, please keep this table in the same order as
19583 gcc's mips_cpu_info_table[]. */
e972090a
NC
19584static const struct mips_cpu_info mips_cpu_info_table[] =
19585{
316f5878 19586 /* Entries for generic ISAs */
d16afab6
RS
19587 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19588 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19589 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19590 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19591 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19592 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19593 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19594 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19595 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19596 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19597 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19598 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19599 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19600 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19601 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19602
19603 /* MIPS I */
d16afab6
RS
19604 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19605 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19606 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19607
19608 /* MIPS II */
d16afab6 19609 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19610
19611 /* MIPS III */
d16afab6
RS
19612 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19613 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19614 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19615 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19616 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19617 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19618 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19619 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19620 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19621 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19622 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19623 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19624 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19625 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19626 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19627 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19628
19629 /* MIPS IV */
d16afab6
RS
19630 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19631 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19632 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19633 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19634 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19635 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19636 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19637 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19638 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19639 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19640 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19641 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19642 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19643 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19644 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19645
19646 /* MIPS 32 */
d16afab6
RS
19647 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19648 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19649 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19650 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19651
19652 /* MIPS 32 Release 2 */
d16afab6
RS
19653 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19654 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19655 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19656 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19657 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19658 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19659 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19660 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19661 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19662 ISA_MIPS32R2, CPU_MIPS32R2 },
19663 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19664 ISA_MIPS32R2, CPU_MIPS32R2 },
19665 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19666 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19667 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19668 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19669 /* Deprecated forms of the above. */
d16afab6
RS
19670 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19671 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19672 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19673 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19674 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19675 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19676 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19677 /* Deprecated forms of the above. */
d16afab6
RS
19678 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19679 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19680 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19681 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19682 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19683 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19684 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19685 /* Deprecated forms of the above. */
d16afab6
RS
19686 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19687 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19688 /* 34Kn is a 34kc without DSP. */
d16afab6 19689 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19690 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19691 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19692 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19693 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19694 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19695 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19696 /* Deprecated forms of the above. */
d16afab6
RS
19697 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19698 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19699 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19700 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19701 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19702 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19703 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19704 /* interaptiv is the new name for 1004kf */
19705 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19706 { "interaptiv-mr2", 0,
19707 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19708 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
c6e5c03a
RS
19709 /* M5100 family */
19710 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19711 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19712 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19713 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19714
316f5878 19715 /* MIPS 64 */
d16afab6
RS
19716 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19717 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19718 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19719 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19720
c7a23324 19721 /* Broadcom SB-1 CPU core */
d16afab6 19722 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19723 /* Broadcom SB-1A CPU core */
d16afab6 19724 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19725
4ba154f5 19726 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 19727
ed163775
MR
19728 /* MIPS 64 Release 2 */
19729
967344c6 19730 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19731 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19732 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19733 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19734 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19735
52b6b6b9 19736 /* RMI Xlr */
d16afab6 19737 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19738
55a36193
MK
19739 /* Broadcom XLP.
19740 XLP is mostly like XLR, with the prominent exception that it is
19741 MIPS64R2 rather than MIPS64. */
d16afab6 19742 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19743
a4968f42 19744 /* MIPS 64 Release 6 */
7ef0d297 19745 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19746 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19747
316f5878 19748 /* End marker */
d16afab6 19749 { NULL, 0, 0, 0, 0 }
316f5878 19750};
e7af610e 19751
84ea6cf2 19752
316f5878
RS
19753/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19754 with a final "000" replaced by "k". Ignore case.
e7af610e 19755
316f5878 19756 Note: this function is shared between GCC and GAS. */
c6c98b38 19757
b34976b6 19758static bfd_boolean
17a2f251 19759mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19760{
19761 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19762 given++, canonical++;
19763
19764 return ((*given == 0 && *canonical == 0)
19765 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19766}
19767
19768
19769/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19770 CPU name. We've traditionally allowed a lot of variation here.
19771
19772 Note: this function is shared between GCC and GAS. */
19773
b34976b6 19774static bfd_boolean
17a2f251 19775mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19776{
19777 /* First see if the name matches exactly, or with a final "000"
19778 turned into "k". */
19779 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19780 return TRUE;
316f5878
RS
19781
19782 /* If not, try comparing based on numerical designation alone.
19783 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19784 if (TOLOWER (*given) == 'r')
19785 given++;
19786 if (!ISDIGIT (*given))
b34976b6 19787 return FALSE;
316f5878
RS
19788
19789 /* Skip over some well-known prefixes in the canonical name,
19790 hoping to find a number there too. */
19791 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19792 canonical += 2;
19793 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19794 canonical += 2;
19795 else if (TOLOWER (canonical[0]) == 'r')
19796 canonical += 1;
19797
19798 return mips_strict_matching_cpu_name_p (canonical, given);
19799}
19800
19801
19802/* Parse an option that takes the name of a processor as its argument.
19803 OPTION is the name of the option and CPU_STRING is the argument.
19804 Return the corresponding processor enumeration if the CPU_STRING is
19805 recognized, otherwise report an error and return null.
19806
19807 A similar function exists in GCC. */
e7af610e
NC
19808
19809static const struct mips_cpu_info *
17a2f251 19810mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19811{
316f5878 19812 const struct mips_cpu_info *p;
e7af610e 19813
316f5878
RS
19814 /* 'from-abi' selects the most compatible architecture for the given
19815 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19816 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19817 version. Look first at the -mgp options, if given, otherwise base
19818 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19819
316f5878
RS
19820 Treat NO_ABI like the EABIs. One reason to do this is that the
19821 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19822 architecture. This code picks MIPS I for 'mips' and MIPS III for
19823 'mips64', just as we did in the days before 'from-abi'. */
19824 if (strcasecmp (cpu_string, "from-abi") == 0)
19825 {
19826 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19827 return mips_cpu_info_from_isa (ISA_MIPS1);
19828
19829 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19830 return mips_cpu_info_from_isa (ISA_MIPS3);
19831
bad1aba3 19832 if (file_mips_opts.gp >= 0)
19833 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19834 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19835
19836 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19837 ? ISA_MIPS3
19838 : ISA_MIPS1);
19839 }
19840
19841 /* 'default' has traditionally been a no-op. Probably not very useful. */
19842 if (strcasecmp (cpu_string, "default") == 0)
19843 return 0;
19844
19845 for (p = mips_cpu_info_table; p->name != 0; p++)
19846 if (mips_matching_cpu_name_p (p->name, cpu_string))
19847 return p;
19848
1661c76c 19849 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19850 return 0;
e7af610e
NC
19851}
19852
316f5878
RS
19853/* Return the canonical processor information for ISA (a member of the
19854 ISA_MIPS* enumeration). */
19855
e7af610e 19856static const struct mips_cpu_info *
17a2f251 19857mips_cpu_info_from_isa (int isa)
e7af610e
NC
19858{
19859 int i;
19860
19861 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19862 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19863 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19864 return (&mips_cpu_info_table[i]);
19865
e972090a 19866 return NULL;
e7af610e 19867}
fef14a42
TS
19868
19869static const struct mips_cpu_info *
17a2f251 19870mips_cpu_info_from_arch (int arch)
fef14a42
TS
19871{
19872 int i;
19873
19874 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19875 if (arch == mips_cpu_info_table[i].cpu)
19876 return (&mips_cpu_info_table[i]);
19877
19878 return NULL;
19879}
316f5878
RS
19880\f
19881static void
17a2f251 19882show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19883{
19884 if (*first_p)
19885 {
19886 fprintf (stream, "%24s", "");
19887 *col_p = 24;
19888 }
19889 else
19890 {
19891 fprintf (stream, ", ");
19892 *col_p += 2;
19893 }
e7af610e 19894
316f5878
RS
19895 if (*col_p + strlen (string) > 72)
19896 {
19897 fprintf (stream, "\n%24s", "");
19898 *col_p = 24;
19899 }
19900
19901 fprintf (stream, "%s", string);
19902 *col_p += strlen (string);
19903
19904 *first_p = 0;
19905}
19906
19907void
17a2f251 19908md_show_usage (FILE *stream)
e7af610e 19909{
316f5878
RS
19910 int column, first;
19911 size_t i;
19912
19913 fprintf (stream, _("\
19914MIPS options:\n\
316f5878
RS
19915-EB generate big endian output\n\
19916-EL generate little endian output\n\
19917-g, -g2 do not remove unneeded NOPs or swap branches\n\
19918-G NUM allow referencing objects up to NUM bytes\n\
19919 implicitly with the gp register [default 8]\n"));
19920 fprintf (stream, _("\
19921-mips1 generate MIPS ISA I instructions\n\
19922-mips2 generate MIPS ISA II instructions\n\
19923-mips3 generate MIPS ISA III instructions\n\
19924-mips4 generate MIPS ISA IV instructions\n\
19925-mips5 generate MIPS ISA V instructions\n\
19926-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19927-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19928-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19929-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19930-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19931-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19932-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19933-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19934-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19935-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19936-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19937
19938 first = 1;
e7af610e
NC
19939
19940 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19941 show (stream, mips_cpu_info_table[i].name, &column, &first);
19942 show (stream, "from-abi", &column, &first);
19943 fputc ('\n', stream);
e7af610e 19944
316f5878
RS
19945 fprintf (stream, _("\
19946-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19947-no-mCPU don't generate code specific to CPU.\n\
19948 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19949
19950 first = 1;
19951
19952 show (stream, "3900", &column, &first);
19953 show (stream, "4010", &column, &first);
19954 show (stream, "4100", &column, &first);
19955 show (stream, "4650", &column, &first);
19956 fputc ('\n', stream);
19957
19958 fprintf (stream, _("\
19959-mips16 generate mips16 instructions\n\
19960-no-mips16 do not generate mips16 instructions\n"));
19961 fprintf (stream, _("\
df58fc94
RS
19962-mmicromips generate microMIPS instructions\n\
19963-mno-micromips do not generate microMIPS instructions\n"));
19964 fprintf (stream, _("\
e16bfa71 19965-msmartmips generate smartmips instructions\n\
3739860c 19966-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 19967 fprintf (stream, _("\
74cd071d
CF
19968-mdsp generate DSP instructions\n\
19969-mno-dsp do not generate DSP instructions\n"));
19970 fprintf (stream, _("\
8b082fb1
TS
19971-mdspr2 generate DSP R2 instructions\n\
19972-mno-dspr2 do not generate DSP R2 instructions\n"));
19973 fprintf (stream, _("\
8f4f9071
MF
19974-mdspr3 generate DSP R3 instructions\n\
19975-mno-dspr3 do not generate DSP R3 instructions\n"));
19976 fprintf (stream, _("\
ef2e4d86
CF
19977-mmt generate MT instructions\n\
19978-mno-mt do not generate MT instructions\n"));
19979 fprintf (stream, _("\
dec0624d
MR
19980-mmcu generate MCU instructions\n\
19981-mno-mcu do not generate MCU instructions\n"));
19982 fprintf (stream, _("\
56d438b1
CF
19983-mmsa generate MSA instructions\n\
19984-mno-msa do not generate MSA instructions\n"));
19985 fprintf (stream, _("\
7d64c587
AB
19986-mxpa generate eXtended Physical Address (XPA) instructions\n\
19987-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19988 fprintf (stream, _("\
b015e599
AP
19989-mvirt generate Virtualization instructions\n\
19990-mno-virt do not generate Virtualization instructions\n"));
19991 fprintf (stream, _("\
833794fc
MR
19992-minsn32 only generate 32-bit microMIPS instructions\n\
19993-mno-insn32 generate all microMIPS instructions\n"));
19994 fprintf (stream, _("\
c67a084a
NC
19995-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19996-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19997-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19998-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19999-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20000-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
20001-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20002-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20003-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
20004-O0 remove unneeded NOPs, do not swap branches\n\
20005-O remove unneeded NOPs and swap branches\n\
316f5878
RS
20006--trap, --no-break trap exception on div by 0 and mult overflow\n\
20007--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20008 fprintf (stream, _("\
20009-mhard-float allow floating-point instructions\n\
20010-msoft-float do not allow floating-point instructions\n\
20011-msingle-float only allow 32-bit floating-point operations\n\
20012-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20013--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20014--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20015-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20016-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20017-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20018
20019 first = 1;
20020
20021 show (stream, "legacy", &column, &first);
20022 show (stream, "2008", &column, &first);
20023
20024 fputc ('\n', stream);
20025
316f5878
RS
20026 fprintf (stream, _("\
20027-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20028-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20029-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20030-non_shared do not generate code that can operate with DSOs\n\
316f5878 20031-xgot assume a 32 bit GOT\n\
dcd410fe 20032-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20033-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20034 position dependent (non shared) code\n\
316f5878
RS
20035-mabi=ABI create ABI conformant object file for:\n"));
20036
20037 first = 1;
20038
20039 show (stream, "32", &column, &first);
20040 show (stream, "o64", &column, &first);
20041 show (stream, "n32", &column, &first);
20042 show (stream, "64", &column, &first);
20043 show (stream, "eabi", &column, &first);
20044
20045 fputc ('\n', stream);
20046
20047 fprintf (stream, _("\
20048-32 create o32 ABI object file (default)\n\
20049-n32 create n32 ABI object file\n\
20050-64 create 64 ABI object file\n"));
e7af610e 20051}
14e777e0 20052
1575952e 20053#ifdef TE_IRIX
14e777e0 20054enum dwarf2_format
413a266c 20055mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20056{
369943fe 20057 if (HAVE_64BIT_SYMBOLS)
1575952e 20058 return dwarf2_format_64bit_irix;
14e777e0
KB
20059 else
20060 return dwarf2_format_32bit;
20061}
1575952e 20062#endif
73369e65
EC
20063
20064int
20065mips_dwarf2_addr_size (void)
20066{
6b6b3450 20067 if (HAVE_64BIT_OBJECTS)
73369e65 20068 return 8;
73369e65
EC
20069 else
20070 return 4;
20071}
5862107c
EC
20072
20073/* Standard calling conventions leave the CFA at SP on entry. */
20074void
20075mips_cfi_frame_initial_instructions (void)
20076{
20077 cfi_add_CFA_def_cfa_register (SP);
20078}
20079
707bfff6
TS
20080int
20081tc_mips_regname_to_dw2regnum (char *regname)
20082{
20083 unsigned int regnum = -1;
20084 unsigned int reg;
20085
20086 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20087 regnum = reg;
20088
20089 return regnum;
20090}
263b2574 20091
20092/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20093 Given a symbolic attribute NAME, return the proper integer value.
20094 Returns -1 if the attribute is not known. */
20095
20096int
20097mips_convert_symbolic_attribute (const char *name)
20098{
20099 static const struct
20100 {
20101 const char * name;
20102 const int tag;
20103 }
20104 attribute_table[] =
20105 {
20106#define T(tag) {#tag, tag}
20107 T (Tag_GNU_MIPS_ABI_FP),
20108 T (Tag_GNU_MIPS_ABI_MSA),
20109#undef T
20110 };
20111 unsigned int i;
20112
20113 if (name == NULL)
20114 return -1;
20115
20116 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20117 if (streq (name, attribute_table[i].name))
20118 return attribute_table[i].tag;
20119
20120 return -1;
20121}
fd5c94ab
RS
20122
20123void
20124md_mips_end (void)
20125{
351cdf24
MF
20126 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20127
fd5c94ab
RS
20128 mips_emit_delays ();
20129 if (cur_proc_ptr)
20130 as_warn (_("missing .end at end of assembly"));
919731af 20131
20132 /* Just in case no code was emitted, do the consistency check. */
20133 file_mips_check_options ();
351cdf24
MF
20134
20135 /* Set a floating-point ABI if the user did not. */
20136 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20137 {
20138 /* Perform consistency checks on the floating-point ABI. */
20139 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20140 Tag_GNU_MIPS_ABI_FP);
20141 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20142 check_fpabi (fpabi);
20143 }
20144 else
20145 {
20146 /* Soft-float gets precedence over single-float, the two options should
20147 not be used together so this should not matter. */
20148 if (file_mips_opts.soft_float == 1)
20149 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20150 /* Single-float gets precedence over all double_float cases. */
20151 else if (file_mips_opts.single_float == 1)
20152 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20153 else
20154 {
20155 switch (file_mips_opts.fp)
20156 {
20157 case 32:
20158 if (file_mips_opts.gp == 32)
20159 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20160 break;
20161 case 0:
20162 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20163 break;
20164 case 64:
20165 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20166 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20167 else if (file_mips_opts.gp == 32)
20168 fpabi = Val_GNU_MIPS_ABI_FP_64;
20169 else
20170 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20171 break;
20172 }
20173 }
20174
20175 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20176 Tag_GNU_MIPS_ABI_FP, fpabi);
20177 }
fd5c94ab 20178}
2f0c68f2
CM
20179
20180/* Returns the relocation type required for a particular CFI encoding. */
20181
20182bfd_reloc_code_real_type
20183mips_cfi_reloc_for_encoding (int encoding)
20184{
20185 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20186 return BFD_RELOC_32_PCREL;
20187 else return BFD_RELOC_NONE;
20188}