Make power8 the default cpu when assembling for 64-bit little endian targets.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
219d1afa 2 Copyright (C) 1993-2018 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_M4650,
1464 OPTION_NO_M4650,
1465 OPTION_M4010,
1466 OPTION_NO_M4010,
1467 OPTION_M4100,
1468 OPTION_NO_M4100,
1469 OPTION_M3900,
1470 OPTION_NO_M3900,
1471 OPTION_M7000_HILO_FIX,
1472 OPTION_MNO_7000_HILO_FIX,
1473 OPTION_FIX_24K,
1474 OPTION_NO_FIX_24K,
a8d14a88
CM
1475 OPTION_FIX_RM7000,
1476 OPTION_NO_FIX_RM7000,
c31f3936
RS
1477 OPTION_FIX_LOONGSON2F_JUMP,
1478 OPTION_NO_FIX_LOONGSON2F_JUMP,
1479 OPTION_FIX_LOONGSON2F_NOP,
1480 OPTION_NO_FIX_LOONGSON2F_NOP,
1481 OPTION_FIX_VR4120,
1482 OPTION_NO_FIX_VR4120,
1483 OPTION_FIX_VR4130,
1484 OPTION_NO_FIX_VR4130,
1485 OPTION_FIX_CN63XXP1,
1486 OPTION_NO_FIX_CN63XXP1,
1487 OPTION_TRAP,
1488 OPTION_BREAK,
1489 OPTION_EB,
1490 OPTION_EL,
1491 OPTION_FP32,
1492 OPTION_GP32,
1493 OPTION_CONSTRUCT_FLOATS,
1494 OPTION_NO_CONSTRUCT_FLOATS,
1495 OPTION_FP64,
351cdf24 1496 OPTION_FPXX,
c31f3936
RS
1497 OPTION_GP64,
1498 OPTION_RELAX_BRANCH,
1499 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1500 OPTION_IGNORE_BRANCH_ISA,
1501 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1502 OPTION_INSN32,
1503 OPTION_NO_INSN32,
c31f3936
RS
1504 OPTION_MSHARED,
1505 OPTION_MNO_SHARED,
1506 OPTION_MSYM32,
1507 OPTION_MNO_SYM32,
1508 OPTION_SOFT_FLOAT,
1509 OPTION_HARD_FLOAT,
1510 OPTION_SINGLE_FLOAT,
1511 OPTION_DOUBLE_FLOAT,
1512 OPTION_32,
c31f3936
RS
1513 OPTION_CALL_SHARED,
1514 OPTION_CALL_NONPIC,
1515 OPTION_NON_SHARED,
1516 OPTION_XGOT,
1517 OPTION_MABI,
1518 OPTION_N32,
1519 OPTION_64,
1520 OPTION_MDEBUG,
1521 OPTION_NO_MDEBUG,
1522 OPTION_PDR,
1523 OPTION_NO_PDR,
1524 OPTION_MVXWORKS_PIC,
ba92f887 1525 OPTION_NAN,
351cdf24
MF
1526 OPTION_ODD_SPREG,
1527 OPTION_NO_ODD_SPREG,
c31f3936
RS
1528 OPTION_END_OF_ENUM
1529 };
1530
1531struct option md_longopts[] =
1532{
1533 /* Options which specify architecture. */
1534 {"march", required_argument, NULL, OPTION_MARCH},
1535 {"mtune", required_argument, NULL, OPTION_MTUNE},
1536 {"mips0", no_argument, NULL, OPTION_MIPS1},
1537 {"mips1", no_argument, NULL, OPTION_MIPS1},
1538 {"mips2", no_argument, NULL, OPTION_MIPS2},
1539 {"mips3", no_argument, NULL, OPTION_MIPS3},
1540 {"mips4", no_argument, NULL, OPTION_MIPS4},
1541 {"mips5", no_argument, NULL, OPTION_MIPS5},
1542 {"mips32", no_argument, NULL, OPTION_MIPS32},
1543 {"mips64", no_argument, NULL, OPTION_MIPS64},
1544 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1545 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1546 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1547 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1548 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1549 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1550 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1551 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1552
1553 /* Options which specify Application Specific Extensions (ASEs). */
1554 {"mips16", no_argument, NULL, OPTION_MIPS16},
1555 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1556 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1557 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1558 {"mdmx", no_argument, NULL, OPTION_MDMX},
1559 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1560 {"mdsp", no_argument, NULL, OPTION_DSP},
1561 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1562 {"mmt", no_argument, NULL, OPTION_MT},
1563 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1564 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1565 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1566 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1567 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1568 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1569 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1570 {"meva", no_argument, NULL, OPTION_EVA},
1571 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1572 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1573 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1574 {"mmcu", no_argument, NULL, OPTION_MCU},
1575 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1576 {"mvirt", no_argument, NULL, OPTION_VIRT},
1577 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1578 {"mmsa", no_argument, NULL, OPTION_MSA},
1579 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1580 {"mxpa", no_argument, NULL, OPTION_XPA},
1581 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1582 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1583 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
c31f3936
RS
1584
1585 /* Old-style architecture options. Don't add more of these. */
1586 {"m4650", no_argument, NULL, OPTION_M4650},
1587 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1588 {"m4010", no_argument, NULL, OPTION_M4010},
1589 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1590 {"m4100", no_argument, NULL, OPTION_M4100},
1591 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1592 {"m3900", no_argument, NULL, OPTION_M3900},
1593 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1594
1595 /* Options which enable bug fixes. */
1596 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1597 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1598 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1599 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1600 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1601 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1602 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1603 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1604 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1605 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1606 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1607 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1608 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1609 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1610 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1611 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1612 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1613
1614 /* Miscellaneous options. */
1615 {"trap", no_argument, NULL, OPTION_TRAP},
1616 {"no-break", no_argument, NULL, OPTION_TRAP},
1617 {"break", no_argument, NULL, OPTION_BREAK},
1618 {"no-trap", no_argument, NULL, OPTION_BREAK},
1619 {"EB", no_argument, NULL, OPTION_EB},
1620 {"EL", no_argument, NULL, OPTION_EL},
1621 {"mfp32", no_argument, NULL, OPTION_FP32},
1622 {"mgp32", no_argument, NULL, OPTION_GP32},
1623 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1624 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1625 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1626 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1627 {"mgp64", no_argument, NULL, OPTION_GP64},
1628 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1629 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1630 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1631 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1632 {"minsn32", no_argument, NULL, OPTION_INSN32},
1633 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1634 {"mshared", no_argument, NULL, OPTION_MSHARED},
1635 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1636 {"msym32", no_argument, NULL, OPTION_MSYM32},
1637 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1638 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1639 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1640 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1641 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1642 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1643 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1644
1645 /* Strictly speaking this next option is ELF specific,
1646 but we allow it for other ports as well in order to
1647 make testing easier. */
1648 {"32", no_argument, NULL, OPTION_32},
1649
1650 /* ELF-specific options. */
c31f3936
RS
1651 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1652 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1653 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1654 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1655 {"xgot", no_argument, NULL, OPTION_XGOT},
1656 {"mabi", required_argument, NULL, OPTION_MABI},
1657 {"n32", no_argument, NULL, OPTION_N32},
1658 {"64", no_argument, NULL, OPTION_64},
1659 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1660 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1661 {"mpdr", no_argument, NULL, OPTION_PDR},
1662 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1663 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1664 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1665
1666 {NULL, no_argument, NULL, 0}
1667};
1668size_t md_longopts_size = sizeof (md_longopts);
1669\f
c6278170
RS
1670/* Information about either an Application Specific Extension or an
1671 optional architecture feature that, for simplicity, we treat in the
1672 same way as an ASE. */
1673struct mips_ase
1674{
1675 /* The name of the ASE, used in both the command-line and .set options. */
1676 const char *name;
1677
1678 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1679 and 64-bit architectures, the flags here refer to the subset that
1680 is available on both. */
1681 unsigned int flags;
1682
1683 /* The ASE_* flag used for instructions that are available on 64-bit
1684 architectures but that are not included in FLAGS. */
1685 unsigned int flags64;
1686
1687 /* The command-line options that turn the ASE on and off. */
1688 int option_on;
1689 int option_off;
1690
1691 /* The minimum required architecture revisions for MIPS32, MIPS64,
1692 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1693 int mips32_rev;
1694 int mips64_rev;
1695 int micromips32_rev;
1696 int micromips64_rev;
7361da2c
AB
1697
1698 /* The architecture where the ASE was removed or -1 if the extension has not
1699 been removed. */
1700 int rem_rev;
c6278170
RS
1701};
1702
1703/* A table of all supported ASEs. */
1704static const struct mips_ase mips_ases[] = {
1705 { "dsp", ASE_DSP, ASE_DSP64,
1706 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1707 2, 2, 2, 2,
1708 -1 },
c6278170
RS
1709
1710 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1711 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1712 2, 2, 2, 2,
1713 -1 },
c6278170 1714
8f4f9071
MF
1715 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1716 OPTION_DSPR3, OPTION_NO_DSPR3,
1717 6, 6, -1, -1,
1718 -1 },
1719
c6278170
RS
1720 { "eva", ASE_EVA, 0,
1721 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1722 2, 2, 2, 2,
1723 -1 },
c6278170
RS
1724
1725 { "mcu", ASE_MCU, 0,
1726 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1727 2, 2, 2, 2,
1728 -1 },
c6278170
RS
1729
1730 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1731 { "mdmx", ASE_MDMX, 0,
1732 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1733 -1, 1, -1, -1,
1734 6 },
c6278170
RS
1735
1736 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1737 { "mips3d", ASE_MIPS3D, 0,
1738 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1739 2, 1, -1, -1,
1740 6 },
c6278170
RS
1741
1742 { "mt", ASE_MT, 0,
1743 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1744 2, 2, -1, -1,
1745 -1 },
c6278170
RS
1746
1747 { "smartmips", ASE_SMARTMIPS, 0,
1748 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1749 1, -1, -1, -1,
1750 6 },
c6278170
RS
1751
1752 { "virt", ASE_VIRT, ASE_VIRT64,
1753 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1754 2, 2, 2, 2,
1755 -1 },
56d438b1
CF
1756
1757 { "msa", ASE_MSA, ASE_MSA64,
1758 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1759 2, 2, 2, 2,
1760 -1 },
7d64c587
AB
1761
1762 { "xpa", ASE_XPA, 0,
1763 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1764 2, 2, 2, 2,
7361da2c 1765 -1 },
25499ac7
MR
1766
1767 { "mips16e2", ASE_MIPS16E2, 0,
1768 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1769 2, 2, -1, -1,
1770 6 },
c6278170
RS
1771};
1772
1773/* The set of ASEs that require -mfp64. */
82bda27b 1774#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1775
1776/* Groups of ASE_* flags that represent different revisions of an ASE. */
1777static const unsigned int mips_ase_groups[] = {
8f4f9071 1778 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1779};
1780\f
252b5132
RH
1781/* Pseudo-op table.
1782
1783 The following pseudo-ops from the Kane and Heinrich MIPS book
1784 should be defined here, but are currently unsupported: .alias,
1785 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1786
1787 The following pseudo-ops from the Kane and Heinrich MIPS book are
1788 specific to the type of debugging information being generated, and
1789 should be defined by the object format: .aent, .begin, .bend,
1790 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1791 .vreg.
1792
1793 The following pseudo-ops from the Kane and Heinrich MIPS book are
1794 not MIPS CPU specific, but are also not specific to the object file
1795 format. This file is probably the best place to define them, but
d84bcf09 1796 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1797
e972090a
NC
1798static const pseudo_typeS mips_pseudo_table[] =
1799{
beae10d5 1800 /* MIPS specific pseudo-ops. */
252b5132
RH
1801 {"option", s_option, 0},
1802 {"set", s_mipsset, 0},
1803 {"rdata", s_change_sec, 'r'},
1804 {"sdata", s_change_sec, 's'},
1805 {"livereg", s_ignore, 0},
1806 {"abicalls", s_abicalls, 0},
1807 {"cpload", s_cpload, 0},
6478892d
TS
1808 {"cpsetup", s_cpsetup, 0},
1809 {"cplocal", s_cplocal, 0},
252b5132 1810 {"cprestore", s_cprestore, 0},
6478892d 1811 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1812 {"dtprelword", s_dtprelword, 0},
1813 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1814 {"tprelword", s_tprelword, 0},
1815 {"tpreldword", s_tpreldword, 0},
6478892d 1816 {"gpvalue", s_gpvalue, 0},
252b5132 1817 {"gpword", s_gpword, 0},
10181a0d 1818 {"gpdword", s_gpdword, 0},
a3f278e2 1819 {"ehword", s_ehword, 0},
252b5132
RH
1820 {"cpadd", s_cpadd, 0},
1821 {"insn", s_insn, 0},
ba92f887 1822 {"nan", s_nan, 0},
919731af 1823 {"module", s_module, 0},
252b5132 1824
beae10d5 1825 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1826 chips. */
38a57ae7 1827 {"asciiz", stringer, 8 + 1},
252b5132
RH
1828 {"bss", s_change_sec, 'b'},
1829 {"err", s_err, 0},
1830 {"half", s_cons, 1},
1831 {"dword", s_cons, 3},
1832 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1833 {"origin", s_org, 0},
1834 {"repeat", s_rept, 0},
252b5132 1835
998b3c36
MR
1836 /* For MIPS this is non-standard, but we define it for consistency. */
1837 {"sbss", s_change_sec, 'B'},
1838
beae10d5 1839 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1840 here for one reason or another. */
1841 {"align", s_align, 0},
1842 {"byte", s_cons, 0},
1843 {"data", s_change_sec, 'd'},
1844 {"double", s_float_cons, 'd'},
1845 {"float", s_float_cons, 'f'},
1846 {"globl", s_mips_globl, 0},
1847 {"global", s_mips_globl, 0},
1848 {"hword", s_cons, 1},
1849 {"int", s_cons, 2},
1850 {"long", s_cons, 2},
1851 {"octa", s_cons, 4},
1852 {"quad", s_cons, 3},
cca86cc8 1853 {"section", s_change_section, 0},
252b5132
RH
1854 {"short", s_cons, 1},
1855 {"single", s_float_cons, 'f'},
754e2bb9 1856 {"stabd", s_mips_stab, 'd'},
252b5132 1857 {"stabn", s_mips_stab, 'n'},
754e2bb9 1858 {"stabs", s_mips_stab, 's'},
252b5132
RH
1859 {"text", s_change_sec, 't'},
1860 {"word", s_cons, 2},
add56521 1861
add56521 1862 { "extern", ecoff_directive_extern, 0},
add56521 1863
43841e91 1864 { NULL, NULL, 0 },
252b5132
RH
1865};
1866
e972090a
NC
1867static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1868{
beae10d5
KH
1869 /* These pseudo-ops should be defined by the object file format.
1870 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1871 {"aent", s_mips_ent, 1},
1872 {"bgnb", s_ignore, 0},
1873 {"end", s_mips_end, 0},
1874 {"endb", s_ignore, 0},
1875 {"ent", s_mips_ent, 0},
c5dd6aab 1876 {"file", s_mips_file, 0},
252b5132
RH
1877 {"fmask", s_mips_mask, 'F'},
1878 {"frame", s_mips_frame, 0},
c5dd6aab 1879 {"loc", s_mips_loc, 0},
252b5132
RH
1880 {"mask", s_mips_mask, 'R'},
1881 {"verstamp", s_ignore, 0},
43841e91 1882 { NULL, NULL, 0 },
252b5132
RH
1883};
1884
3ae8dd8d
MR
1885/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1886 purpose of the `.dc.a' internal pseudo-op. */
1887
1888int
1889mips_address_bytes (void)
1890{
919731af 1891 file_mips_check_options ();
3ae8dd8d
MR
1892 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1893}
1894
17a2f251 1895extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1896
1897void
17a2f251 1898mips_pop_insert (void)
252b5132
RH
1899{
1900 pop_insert (mips_pseudo_table);
1901 if (! ECOFF_DEBUGGING)
1902 pop_insert (mips_nonecoff_pseudo_table);
1903}
1904\f
1905/* Symbols labelling the current insn. */
1906
e972090a
NC
1907struct insn_label_list
1908{
252b5132
RH
1909 struct insn_label_list *next;
1910 symbolS *label;
1911};
1912
252b5132 1913static struct insn_label_list *free_insn_labels;
742a56fe 1914#define label_list tc_segment_info_data.labels
252b5132 1915
17a2f251 1916static void mips_clear_insn_labels (void);
df58fc94
RS
1917static void mips_mark_labels (void);
1918static void mips_compressed_mark_labels (void);
252b5132
RH
1919
1920static inline void
17a2f251 1921mips_clear_insn_labels (void)
252b5132 1922{
ed9e98c2 1923 struct insn_label_list **pl;
a8dbcb85 1924 segment_info_type *si;
252b5132 1925
a8dbcb85
TS
1926 if (now_seg)
1927 {
1928 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1929 ;
3739860c 1930
a8dbcb85
TS
1931 si = seg_info (now_seg);
1932 *pl = si->label_list;
1933 si->label_list = NULL;
1934 }
252b5132 1935}
a8dbcb85 1936
df58fc94
RS
1937/* Mark instruction labels in MIPS16/microMIPS mode. */
1938
1939static inline void
1940mips_mark_labels (void)
1941{
1942 if (HAVE_CODE_COMPRESSION)
1943 mips_compressed_mark_labels ();
1944}
252b5132
RH
1945\f
1946static char *expr_end;
1947
e423441d 1948/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1949 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1950
1951static expressionS imm_expr;
252b5132 1952
77bd4346
RS
1953/* The relocatable field in an instruction and the relocs associated
1954 with it. These variables are used for instructions like LUI and
1955 JAL as well as true offsets. They are also used for address
1956 operands in macros. */
252b5132 1957
77bd4346 1958static expressionS offset_expr;
f6688943
TS
1959static bfd_reloc_code_real_type offset_reloc[3]
1960 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1961
df58fc94
RS
1962/* This is set to the resulting size of the instruction to be produced
1963 by mips16_ip if an explicit extension is used or by mips_ip if an
1964 explicit size is supplied. */
252b5132 1965
df58fc94 1966static unsigned int forced_insn_length;
252b5132 1967
e1b47bd5
RS
1968/* True if we are assembling an instruction. All dot symbols defined during
1969 this time should be treated as code labels. */
1970
1971static bfd_boolean mips_assembling_insn;
1972
ecb4347a
DJ
1973/* The pdr segment for per procedure frame/regmask info. Not used for
1974 ECOFF debugging. */
252b5132
RH
1975
1976static segT pdr_seg;
252b5132 1977
e013f690
TS
1978/* The default target format to use. */
1979
aeffff67
RS
1980#if defined (TE_FreeBSD)
1981#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1982#elif defined (TE_TMIPS)
1983#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1984#else
1985#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1986#endif
1987
e013f690 1988const char *
17a2f251 1989mips_target_format (void)
e013f690
TS
1990{
1991 switch (OUTPUT_FLAVOR)
1992 {
e013f690 1993 case bfd_target_elf_flavour:
0a44bf69
RS
1994#ifdef TE_VXWORKS
1995 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1996 return (target_big_endian
1997 ? "elf32-bigmips-vxworks"
1998 : "elf32-littlemips-vxworks");
1999#endif
e013f690 2000 return (target_big_endian
cfe86eaa 2001 ? (HAVE_64BIT_OBJECTS
aeffff67 2002 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2003 : (HAVE_NEWABI
aeffff67
RS
2004 ? ELF_TARGET ("elf32-n", "big")
2005 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2006 : (HAVE_64BIT_OBJECTS
aeffff67 2007 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2008 : (HAVE_NEWABI
aeffff67
RS
2009 ? ELF_TARGET ("elf32-n", "little")
2010 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2011 default:
2012 abort ();
2013 return NULL;
2014 }
2015}
2016
c6278170
RS
2017/* Return the ISA revision that is currently in use, or 0 if we are
2018 generating code for MIPS V or below. */
2019
2020static int
2021mips_isa_rev (void)
2022{
2023 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2024 return 2;
2025
ae52f483
AB
2026 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2027 return 3;
2028
2029 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2030 return 5;
2031
7361da2c
AB
2032 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2033 return 6;
2034
c6278170
RS
2035 /* microMIPS implies revision 2 or above. */
2036 if (mips_opts.micromips)
2037 return 2;
2038
2039 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2040 return 1;
2041
2042 return 0;
2043}
2044
2045/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2046
2047static unsigned int
2048mips_ase_mask (unsigned int flags)
2049{
2050 unsigned int i;
2051
2052 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2053 if (flags & mips_ase_groups[i])
2054 flags |= mips_ase_groups[i];
2055 return flags;
2056}
2057
2058/* Check whether the current ISA supports ASE. Issue a warning if
2059 appropriate. */
2060
2061static void
2062mips_check_isa_supports_ase (const struct mips_ase *ase)
2063{
2064 const char *base;
2065 int min_rev, size;
2066 static unsigned int warned_isa;
2067 static unsigned int warned_fp32;
2068
2069 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2070 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2071 else
2072 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2073 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2074 && (warned_isa & ase->flags) != ase->flags)
2075 {
2076 warned_isa |= ase->flags;
2077 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2078 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2079 if (min_rev < 0)
1661c76c 2080 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2081 " `%s' extension"), size, base, ase->name);
2082 else
1661c76c 2083 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2084 ase->name, base, size, min_rev);
2085 }
7361da2c
AB
2086 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2087 && (warned_isa & ase->flags) != ase->flags)
2088 {
2089 warned_isa |= ase->flags;
2090 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2091 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2092 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2093 ase->name, base, size, ase->rem_rev);
2094 }
2095
c6278170 2096 if ((ase->flags & FP64_ASES)
0b35dfee 2097 && mips_opts.fp != 64
c6278170
RS
2098 && (warned_fp32 & ase->flags) != ase->flags)
2099 {
2100 warned_fp32 |= ase->flags;
1661c76c 2101 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2102 }
2103}
2104
2105/* Check all enabled ASEs to see whether they are supported by the
2106 chosen architecture. */
2107
2108static void
2109mips_check_isa_supports_ases (void)
2110{
2111 unsigned int i, mask;
2112
2113 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2114 {
2115 mask = mips_ase_mask (mips_ases[i].flags);
2116 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2117 mips_check_isa_supports_ase (&mips_ases[i]);
2118 }
2119}
2120
2121/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2122 that were affected. */
2123
2124static unsigned int
919731af 2125mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2126 bfd_boolean enabled_p)
c6278170
RS
2127{
2128 unsigned int mask;
2129
2130 mask = mips_ase_mask (ase->flags);
919731af 2131 opts->ase &= ~mask;
92cebb3d
MR
2132
2133 /* Clear combination ASE flags, which need to be recalculated based on
2134 updated regular ASE settings. */
9785fc2a 2135 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2136
c6278170 2137 if (enabled_p)
919731af 2138 opts->ase |= ase->flags;
25499ac7 2139
9785fc2a
MR
2140 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2141 instructions which are only valid when both ASEs are enabled.
2142 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2143 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2144 {
2145 opts->ase |= ASE_XPA_VIRT;
2146 mask |= ASE_XPA_VIRT;
2147 }
25499ac7
MR
2148 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2149 {
2150 opts->ase |= ASE_MIPS16E2_MT;
2151 mask |= ASE_MIPS16E2_MT;
2152 }
2153
c6278170
RS
2154 return mask;
2155}
2156
2157/* Return the ASE called NAME, or null if none. */
2158
2159static const struct mips_ase *
2160mips_lookup_ase (const char *name)
2161{
2162 unsigned int i;
2163
2164 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2165 if (strcmp (name, mips_ases[i].name) == 0)
2166 return &mips_ases[i];
2167 return NULL;
2168}
2169
df58fc94 2170/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2171 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2172 otherwise it is a 32-bit instruction. */
df58fc94
RS
2173
2174static inline unsigned int
2175micromips_insn_length (const struct mips_opcode *mo)
2176{
7fd53920 2177 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2178}
2179
5c04167a
RS
2180/* Return the length of MIPS16 instruction OPCODE. */
2181
2182static inline unsigned int
2183mips16_opcode_length (unsigned long opcode)
2184{
2185 return (opcode >> 16) == 0 ? 2 : 4;
2186}
2187
1e915849
RS
2188/* Return the length of instruction INSN. */
2189
2190static inline unsigned int
2191insn_length (const struct mips_cl_insn *insn)
2192{
df58fc94
RS
2193 if (mips_opts.micromips)
2194 return micromips_insn_length (insn->insn_mo);
2195 else if (mips_opts.mips16)
5c04167a 2196 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2197 else
1e915849 2198 return 4;
1e915849
RS
2199}
2200
2201/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2202
2203static void
2204create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2205{
2206 size_t i;
2207
2208 insn->insn_mo = mo;
1e915849
RS
2209 insn->insn_opcode = mo->match;
2210 insn->frag = NULL;
2211 insn->where = 0;
2212 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2213 insn->fixp[i] = NULL;
2214 insn->fixed_p = (mips_opts.noreorder > 0);
2215 insn->noreorder_p = (mips_opts.noreorder > 0);
2216 insn->mips16_absolute_jump_p = 0;
15be625d 2217 insn->complete_p = 0;
e407c74b 2218 insn->cleared_p = 0;
1e915849
RS
2219}
2220
fc76e730
RS
2221/* Get a list of all the operands in INSN. */
2222
2223static const struct mips_operand_array *
2224insn_operands (const struct mips_cl_insn *insn)
2225{
2226 if (insn->insn_mo >= &mips_opcodes[0]
2227 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2228 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2229
2230 if (insn->insn_mo >= &mips16_opcodes[0]
2231 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2232 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2233
2234 if (insn->insn_mo >= &micromips_opcodes[0]
2235 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2236 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2237
2238 abort ();
2239}
2240
2241/* Get a description of operand OPNO of INSN. */
2242
2243static const struct mips_operand *
2244insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2245{
2246 const struct mips_operand_array *operands;
2247
2248 operands = insn_operands (insn);
2249 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2250 abort ();
2251 return operands->operand[opno];
2252}
2253
e077a1c8
RS
2254/* Install UVAL as the value of OPERAND in INSN. */
2255
2256static inline void
2257insn_insert_operand (struct mips_cl_insn *insn,
2258 const struct mips_operand *operand, unsigned int uval)
2259{
25499ac7
MR
2260 if (mips_opts.mips16
2261 && operand->type == OP_INT && operand->lsb == 0
2262 && mips_opcode_32bit_p (insn->insn_mo))
2263 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2264 else
2265 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2266}
2267
fc76e730
RS
2268/* Extract the value of OPERAND from INSN. */
2269
2270static inline unsigned
2271insn_extract_operand (const struct mips_cl_insn *insn,
2272 const struct mips_operand *operand)
2273{
2274 return mips_extract_operand (operand, insn->insn_opcode);
2275}
2276
df58fc94 2277/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2278
2279static void
df58fc94 2280mips_record_compressed_mode (void)
742a56fe
RS
2281{
2282 segment_info_type *si;
2283
2284 si = seg_info (now_seg);
2285 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2286 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2287 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2288 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2289}
2290
4d68580a
RS
2291/* Read a standard MIPS instruction from BUF. */
2292
2293static unsigned long
2294read_insn (char *buf)
2295{
2296 if (target_big_endian)
2297 return bfd_getb32 ((bfd_byte *) buf);
2298 else
2299 return bfd_getl32 ((bfd_byte *) buf);
2300}
2301
2302/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2303 the next byte. */
2304
2305static char *
2306write_insn (char *buf, unsigned int insn)
2307{
2308 md_number_to_chars (buf, insn, 4);
2309 return buf + 4;
2310}
2311
2312/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2313 has length LENGTH. */
2314
2315static unsigned long
2316read_compressed_insn (char *buf, unsigned int length)
2317{
2318 unsigned long insn;
2319 unsigned int i;
2320
2321 insn = 0;
2322 for (i = 0; i < length; i += 2)
2323 {
2324 insn <<= 16;
2325 if (target_big_endian)
2326 insn |= bfd_getb16 ((char *) buf);
2327 else
2328 insn |= bfd_getl16 ((char *) buf);
2329 buf += 2;
2330 }
2331 return insn;
2332}
2333
5c04167a
RS
2334/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2335 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2336
2337static char *
2338write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2339{
2340 unsigned int i;
2341
2342 for (i = 0; i < length; i += 2)
2343 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2344 return buf + length;
2345}
2346
1e915849
RS
2347/* Install INSN at the location specified by its "frag" and "where" fields. */
2348
2349static void
2350install_insn (const struct mips_cl_insn *insn)
2351{
2352 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2353 if (HAVE_CODE_COMPRESSION)
2354 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2355 else
4d68580a 2356 write_insn (f, insn->insn_opcode);
df58fc94 2357 mips_record_compressed_mode ();
1e915849
RS
2358}
2359
2360/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2361 and install the opcode in the new location. */
2362
2363static void
2364move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2365{
2366 size_t i;
2367
2368 insn->frag = frag;
2369 insn->where = where;
2370 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2371 if (insn->fixp[i] != NULL)
2372 {
2373 insn->fixp[i]->fx_frag = frag;
2374 insn->fixp[i]->fx_where = where;
2375 }
2376 install_insn (insn);
2377}
2378
2379/* Add INSN to the end of the output. */
2380
2381static void
2382add_fixed_insn (struct mips_cl_insn *insn)
2383{
2384 char *f = frag_more (insn_length (insn));
2385 move_insn (insn, frag_now, f - frag_now->fr_literal);
2386}
2387
2388/* Start a variant frag and move INSN to the start of the variant part,
2389 marking it as fixed. The other arguments are as for frag_var. */
2390
2391static void
2392add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2393 relax_substateT subtype, symbolS *symbol, offsetT offset)
2394{
2395 frag_grow (max_chars);
2396 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2397 insn->fixed_p = 1;
2398 frag_var (rs_machine_dependent, max_chars, var,
2399 subtype, symbol, offset, NULL);
2400}
2401
2402/* Insert N copies of INSN into the history buffer, starting at
2403 position FIRST. Neither FIRST nor N need to be clipped. */
2404
2405static void
2406insert_into_history (unsigned int first, unsigned int n,
2407 const struct mips_cl_insn *insn)
2408{
2409 if (mips_relax.sequence != 2)
2410 {
2411 unsigned int i;
2412
2413 for (i = ARRAY_SIZE (history); i-- > first;)
2414 if (i >= first + n)
2415 history[i] = history[i - n];
2416 else
2417 history[i] = *insn;
2418 }
2419}
2420
e3de51ce
RS
2421/* Clear the error in insn_error. */
2422
2423static void
2424clear_insn_error (void)
2425{
2426 memset (&insn_error, 0, sizeof (insn_error));
2427}
2428
2429/* Possibly record error message MSG for the current instruction.
2430 If the error is about a particular argument, ARGNUM is the 1-based
2431 number of that argument, otherwise it is 0. FORMAT is the format
2432 of MSG. Return true if MSG was used, false if the current message
2433 was kept. */
2434
2435static bfd_boolean
2436set_insn_error_format (int argnum, enum mips_insn_error_format format,
2437 const char *msg)
2438{
2439 if (argnum == 0)
2440 {
2441 /* Give priority to errors against specific arguments, and to
2442 the first whole-instruction message. */
2443 if (insn_error.msg)
2444 return FALSE;
2445 }
2446 else
2447 {
2448 /* Keep insn_error if it is against a later argument. */
2449 if (argnum < insn_error.min_argnum)
2450 return FALSE;
2451
2452 /* If both errors are against the same argument but are different,
2453 give up on reporting a specific error for this argument.
2454 See the comment about mips_insn_error for details. */
2455 if (argnum == insn_error.min_argnum
2456 && insn_error.msg
2457 && strcmp (insn_error.msg, msg) != 0)
2458 {
2459 insn_error.msg = 0;
2460 insn_error.min_argnum += 1;
2461 return FALSE;
2462 }
2463 }
2464 insn_error.min_argnum = argnum;
2465 insn_error.format = format;
2466 insn_error.msg = msg;
2467 return TRUE;
2468}
2469
2470/* Record an instruction error with no % format fields. ARGNUM and MSG are
2471 as for set_insn_error_format. */
2472
2473static void
2474set_insn_error (int argnum, const char *msg)
2475{
2476 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2477}
2478
2479/* Record an instruction error with one %d field I. ARGNUM and MSG are
2480 as for set_insn_error_format. */
2481
2482static void
2483set_insn_error_i (int argnum, const char *msg, int i)
2484{
2485 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2486 insn_error.u.i = i;
2487}
2488
2489/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2490 are as for set_insn_error_format. */
2491
2492static void
2493set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2494{
2495 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2496 {
2497 insn_error.u.ss[0] = s1;
2498 insn_error.u.ss[1] = s2;
2499 }
2500}
2501
2502/* Report the error in insn_error, which is against assembly code STR. */
2503
2504static void
2505report_insn_error (const char *str)
2506{
e1fa0163 2507 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2508
e3de51ce
RS
2509 switch (insn_error.format)
2510 {
2511 case ERR_FMT_PLAIN:
2512 as_bad (msg, str);
2513 break;
2514
2515 case ERR_FMT_I:
2516 as_bad (msg, insn_error.u.i, str);
2517 break;
2518
2519 case ERR_FMT_SS:
2520 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2521 break;
2522 }
e1fa0163
NC
2523
2524 free ((char *) msg);
e3de51ce
RS
2525}
2526
71400594
RS
2527/* Initialize vr4120_conflicts. There is a bit of duplication here:
2528 the idea is to make it obvious at a glance that each errata is
2529 included. */
2530
2531static void
2532init_vr4120_conflicts (void)
2533{
2534#define CONFLICT(FIRST, SECOND) \
2535 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2536
2537 /* Errata 21 - [D]DIV[U] after [D]MACC */
2538 CONFLICT (MACC, DIV);
2539 CONFLICT (DMACC, DIV);
2540
2541 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2542 CONFLICT (DMULT, DMULT);
2543 CONFLICT (DMULT, DMACC);
2544 CONFLICT (DMACC, DMULT);
2545 CONFLICT (DMACC, DMACC);
2546
2547 /* Errata 24 - MT{LO,HI} after [D]MACC */
2548 CONFLICT (MACC, MTHILO);
2549 CONFLICT (DMACC, MTHILO);
2550
2551 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2552 instruction is executed immediately after a MACC or DMACC
2553 instruction, the result of [either instruction] is incorrect." */
2554 CONFLICT (MACC, MULT);
2555 CONFLICT (MACC, DMULT);
2556 CONFLICT (DMACC, MULT);
2557 CONFLICT (DMACC, DMULT);
2558
2559 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2560 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2561 DDIV or DDIVU instruction, the result of the MACC or
2562 DMACC instruction is incorrect.". */
2563 CONFLICT (DMULT, MACC);
2564 CONFLICT (DMULT, DMACC);
2565 CONFLICT (DIV, MACC);
2566 CONFLICT (DIV, DMACC);
2567
2568#undef CONFLICT
2569}
2570
707bfff6
TS
2571struct regname {
2572 const char *name;
2573 unsigned int num;
2574};
2575
14daeee3 2576#define RNUM_MASK 0x00000ff
56d438b1 2577#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2578#define RTYPE_NUM 0x0000100
2579#define RTYPE_FPU 0x0000200
2580#define RTYPE_FCC 0x0000400
2581#define RTYPE_VEC 0x0000800
2582#define RTYPE_GP 0x0001000
2583#define RTYPE_CP0 0x0002000
2584#define RTYPE_PC 0x0004000
2585#define RTYPE_ACC 0x0008000
2586#define RTYPE_CCC 0x0010000
2587#define RTYPE_VI 0x0020000
2588#define RTYPE_VF 0x0040000
2589#define RTYPE_R5900_I 0x0080000
2590#define RTYPE_R5900_Q 0x0100000
2591#define RTYPE_R5900_R 0x0200000
2592#define RTYPE_R5900_ACC 0x0400000
56d438b1 2593#define RTYPE_MSA 0x0800000
14daeee3 2594#define RWARN 0x8000000
707bfff6
TS
2595
2596#define GENERIC_REGISTER_NUMBERS \
2597 {"$0", RTYPE_NUM | 0}, \
2598 {"$1", RTYPE_NUM | 1}, \
2599 {"$2", RTYPE_NUM | 2}, \
2600 {"$3", RTYPE_NUM | 3}, \
2601 {"$4", RTYPE_NUM | 4}, \
2602 {"$5", RTYPE_NUM | 5}, \
2603 {"$6", RTYPE_NUM | 6}, \
2604 {"$7", RTYPE_NUM | 7}, \
2605 {"$8", RTYPE_NUM | 8}, \
2606 {"$9", RTYPE_NUM | 9}, \
2607 {"$10", RTYPE_NUM | 10}, \
2608 {"$11", RTYPE_NUM | 11}, \
2609 {"$12", RTYPE_NUM | 12}, \
2610 {"$13", RTYPE_NUM | 13}, \
2611 {"$14", RTYPE_NUM | 14}, \
2612 {"$15", RTYPE_NUM | 15}, \
2613 {"$16", RTYPE_NUM | 16}, \
2614 {"$17", RTYPE_NUM | 17}, \
2615 {"$18", RTYPE_NUM | 18}, \
2616 {"$19", RTYPE_NUM | 19}, \
2617 {"$20", RTYPE_NUM | 20}, \
2618 {"$21", RTYPE_NUM | 21}, \
2619 {"$22", RTYPE_NUM | 22}, \
2620 {"$23", RTYPE_NUM | 23}, \
2621 {"$24", RTYPE_NUM | 24}, \
2622 {"$25", RTYPE_NUM | 25}, \
2623 {"$26", RTYPE_NUM | 26}, \
2624 {"$27", RTYPE_NUM | 27}, \
2625 {"$28", RTYPE_NUM | 28}, \
2626 {"$29", RTYPE_NUM | 29}, \
2627 {"$30", RTYPE_NUM | 30}, \
3739860c 2628 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2629
2630#define FPU_REGISTER_NAMES \
2631 {"$f0", RTYPE_FPU | 0}, \
2632 {"$f1", RTYPE_FPU | 1}, \
2633 {"$f2", RTYPE_FPU | 2}, \
2634 {"$f3", RTYPE_FPU | 3}, \
2635 {"$f4", RTYPE_FPU | 4}, \
2636 {"$f5", RTYPE_FPU | 5}, \
2637 {"$f6", RTYPE_FPU | 6}, \
2638 {"$f7", RTYPE_FPU | 7}, \
2639 {"$f8", RTYPE_FPU | 8}, \
2640 {"$f9", RTYPE_FPU | 9}, \
2641 {"$f10", RTYPE_FPU | 10}, \
2642 {"$f11", RTYPE_FPU | 11}, \
2643 {"$f12", RTYPE_FPU | 12}, \
2644 {"$f13", RTYPE_FPU | 13}, \
2645 {"$f14", RTYPE_FPU | 14}, \
2646 {"$f15", RTYPE_FPU | 15}, \
2647 {"$f16", RTYPE_FPU | 16}, \
2648 {"$f17", RTYPE_FPU | 17}, \
2649 {"$f18", RTYPE_FPU | 18}, \
2650 {"$f19", RTYPE_FPU | 19}, \
2651 {"$f20", RTYPE_FPU | 20}, \
2652 {"$f21", RTYPE_FPU | 21}, \
2653 {"$f22", RTYPE_FPU | 22}, \
2654 {"$f23", RTYPE_FPU | 23}, \
2655 {"$f24", RTYPE_FPU | 24}, \
2656 {"$f25", RTYPE_FPU | 25}, \
2657 {"$f26", RTYPE_FPU | 26}, \
2658 {"$f27", RTYPE_FPU | 27}, \
2659 {"$f28", RTYPE_FPU | 28}, \
2660 {"$f29", RTYPE_FPU | 29}, \
2661 {"$f30", RTYPE_FPU | 30}, \
2662 {"$f31", RTYPE_FPU | 31}
2663
2664#define FPU_CONDITION_CODE_NAMES \
2665 {"$fcc0", RTYPE_FCC | 0}, \
2666 {"$fcc1", RTYPE_FCC | 1}, \
2667 {"$fcc2", RTYPE_FCC | 2}, \
2668 {"$fcc3", RTYPE_FCC | 3}, \
2669 {"$fcc4", RTYPE_FCC | 4}, \
2670 {"$fcc5", RTYPE_FCC | 5}, \
2671 {"$fcc6", RTYPE_FCC | 6}, \
2672 {"$fcc7", RTYPE_FCC | 7}
2673
2674#define COPROC_CONDITION_CODE_NAMES \
2675 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2676 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2677 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2678 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2679 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2680 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2681 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2682 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2683
2684#define N32N64_SYMBOLIC_REGISTER_NAMES \
2685 {"$a4", RTYPE_GP | 8}, \
2686 {"$a5", RTYPE_GP | 9}, \
2687 {"$a6", RTYPE_GP | 10}, \
2688 {"$a7", RTYPE_GP | 11}, \
2689 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2690 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2691 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2692 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2693 {"$t0", RTYPE_GP | 12}, \
2694 {"$t1", RTYPE_GP | 13}, \
2695 {"$t2", RTYPE_GP | 14}, \
2696 {"$t3", RTYPE_GP | 15}
2697
2698#define O32_SYMBOLIC_REGISTER_NAMES \
2699 {"$t0", RTYPE_GP | 8}, \
2700 {"$t1", RTYPE_GP | 9}, \
2701 {"$t2", RTYPE_GP | 10}, \
2702 {"$t3", RTYPE_GP | 11}, \
2703 {"$t4", RTYPE_GP | 12}, \
2704 {"$t5", RTYPE_GP | 13}, \
2705 {"$t6", RTYPE_GP | 14}, \
2706 {"$t7", RTYPE_GP | 15}, \
2707 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2708 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2709 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2710 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2711
2712/* Remaining symbolic register names */
2713#define SYMBOLIC_REGISTER_NAMES \
2714 {"$zero", RTYPE_GP | 0}, \
2715 {"$at", RTYPE_GP | 1}, \
2716 {"$AT", RTYPE_GP | 1}, \
2717 {"$v0", RTYPE_GP | 2}, \
2718 {"$v1", RTYPE_GP | 3}, \
2719 {"$a0", RTYPE_GP | 4}, \
2720 {"$a1", RTYPE_GP | 5}, \
2721 {"$a2", RTYPE_GP | 6}, \
2722 {"$a3", RTYPE_GP | 7}, \
2723 {"$s0", RTYPE_GP | 16}, \
2724 {"$s1", RTYPE_GP | 17}, \
2725 {"$s2", RTYPE_GP | 18}, \
2726 {"$s3", RTYPE_GP | 19}, \
2727 {"$s4", RTYPE_GP | 20}, \
2728 {"$s5", RTYPE_GP | 21}, \
2729 {"$s6", RTYPE_GP | 22}, \
2730 {"$s7", RTYPE_GP | 23}, \
2731 {"$t8", RTYPE_GP | 24}, \
2732 {"$t9", RTYPE_GP | 25}, \
2733 {"$k0", RTYPE_GP | 26}, \
2734 {"$kt0", RTYPE_GP | 26}, \
2735 {"$k1", RTYPE_GP | 27}, \
2736 {"$kt1", RTYPE_GP | 27}, \
2737 {"$gp", RTYPE_GP | 28}, \
2738 {"$sp", RTYPE_GP | 29}, \
2739 {"$s8", RTYPE_GP | 30}, \
2740 {"$fp", RTYPE_GP | 30}, \
2741 {"$ra", RTYPE_GP | 31}
2742
2743#define MIPS16_SPECIAL_REGISTER_NAMES \
2744 {"$pc", RTYPE_PC | 0}
2745
2746#define MDMX_VECTOR_REGISTER_NAMES \
2747 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2748 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2749 {"$v2", RTYPE_VEC | 2}, \
2750 {"$v3", RTYPE_VEC | 3}, \
2751 {"$v4", RTYPE_VEC | 4}, \
2752 {"$v5", RTYPE_VEC | 5}, \
2753 {"$v6", RTYPE_VEC | 6}, \
2754 {"$v7", RTYPE_VEC | 7}, \
2755 {"$v8", RTYPE_VEC | 8}, \
2756 {"$v9", RTYPE_VEC | 9}, \
2757 {"$v10", RTYPE_VEC | 10}, \
2758 {"$v11", RTYPE_VEC | 11}, \
2759 {"$v12", RTYPE_VEC | 12}, \
2760 {"$v13", RTYPE_VEC | 13}, \
2761 {"$v14", RTYPE_VEC | 14}, \
2762 {"$v15", RTYPE_VEC | 15}, \
2763 {"$v16", RTYPE_VEC | 16}, \
2764 {"$v17", RTYPE_VEC | 17}, \
2765 {"$v18", RTYPE_VEC | 18}, \
2766 {"$v19", RTYPE_VEC | 19}, \
2767 {"$v20", RTYPE_VEC | 20}, \
2768 {"$v21", RTYPE_VEC | 21}, \
2769 {"$v22", RTYPE_VEC | 22}, \
2770 {"$v23", RTYPE_VEC | 23}, \
2771 {"$v24", RTYPE_VEC | 24}, \
2772 {"$v25", RTYPE_VEC | 25}, \
2773 {"$v26", RTYPE_VEC | 26}, \
2774 {"$v27", RTYPE_VEC | 27}, \
2775 {"$v28", RTYPE_VEC | 28}, \
2776 {"$v29", RTYPE_VEC | 29}, \
2777 {"$v30", RTYPE_VEC | 30}, \
2778 {"$v31", RTYPE_VEC | 31}
2779
14daeee3
RS
2780#define R5900_I_NAMES \
2781 {"$I", RTYPE_R5900_I | 0}
2782
2783#define R5900_Q_NAMES \
2784 {"$Q", RTYPE_R5900_Q | 0}
2785
2786#define R5900_R_NAMES \
2787 {"$R", RTYPE_R5900_R | 0}
2788
2789#define R5900_ACC_NAMES \
2790 {"$ACC", RTYPE_R5900_ACC | 0 }
2791
707bfff6
TS
2792#define MIPS_DSP_ACCUMULATOR_NAMES \
2793 {"$ac0", RTYPE_ACC | 0}, \
2794 {"$ac1", RTYPE_ACC | 1}, \
2795 {"$ac2", RTYPE_ACC | 2}, \
2796 {"$ac3", RTYPE_ACC | 3}
2797
2798static const struct regname reg_names[] = {
2799 GENERIC_REGISTER_NUMBERS,
2800 FPU_REGISTER_NAMES,
2801 FPU_CONDITION_CODE_NAMES,
2802 COPROC_CONDITION_CODE_NAMES,
2803
2804 /* The $txx registers depends on the abi,
2805 these will be added later into the symbol table from
3739860c 2806 one of the tables below once mips_abi is set after
707bfff6
TS
2807 parsing of arguments from the command line. */
2808 SYMBOLIC_REGISTER_NAMES,
2809
2810 MIPS16_SPECIAL_REGISTER_NAMES,
2811 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2812 R5900_I_NAMES,
2813 R5900_Q_NAMES,
2814 R5900_R_NAMES,
2815 R5900_ACC_NAMES,
707bfff6
TS
2816 MIPS_DSP_ACCUMULATOR_NAMES,
2817 {0, 0}
2818};
2819
2820static const struct regname reg_names_o32[] = {
2821 O32_SYMBOLIC_REGISTER_NAMES,
2822 {0, 0}
2823};
2824
2825static const struct regname reg_names_n32n64[] = {
2826 N32N64_SYMBOLIC_REGISTER_NAMES,
2827 {0, 0}
2828};
2829
a92713e6
RS
2830/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2831 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2832 of these register symbols, return the associated vector register,
2833 otherwise return SYMVAL itself. */
df58fc94 2834
a92713e6
RS
2835static unsigned int
2836mips_prefer_vec_regno (unsigned int symval)
707bfff6 2837{
a92713e6
RS
2838 if ((symval & -2) == (RTYPE_GP | 2))
2839 return RTYPE_VEC | (symval & 1);
2840 return symval;
2841}
2842
14daeee3
RS
2843/* Return true if string [S, E) is a valid register name, storing its
2844 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2845
2846static bfd_boolean
14daeee3 2847mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2848{
707bfff6 2849 char save_c;
14daeee3 2850 symbolS *symbol;
707bfff6
TS
2851
2852 /* Terminate name. */
2853 save_c = *e;
2854 *e = '\0';
2855
a92713e6
RS
2856 /* Look up the name. */
2857 symbol = symbol_find (s);
2858 *e = save_c;
2859
2860 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2861 return FALSE;
2862
14daeee3
RS
2863 *symval_ptr = S_GET_VALUE (symbol);
2864 return TRUE;
2865}
2866
2867/* Return true if the string at *SPTR is a valid register name. Allow it
2868 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2869 is nonnull.
2870
2871 When returning true, move *SPTR past the register, store the
2872 register's symbol value in *SYMVAL_PTR and the channel mask in
2873 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2874 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2875 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2876
2877static bfd_boolean
2878mips_parse_register (char **sptr, unsigned int *symval_ptr,
2879 unsigned int *channels_ptr)
2880{
2881 char *s, *e, *m;
2882 const char *q;
2883 unsigned int channels, symval, bit;
2884
2885 /* Find end of name. */
2886 s = e = *sptr;
2887 if (is_name_beginner (*e))
2888 ++e;
2889 while (is_part_of_name (*e))
2890 ++e;
2891
2892 channels = 0;
2893 if (!mips_parse_register_1 (s, e, &symval))
2894 {
2895 if (!channels_ptr)
2896 return FALSE;
2897
2898 /* Eat characters from the end of the string that are valid
2899 channel suffixes. The preceding register must be $ACC or
2900 end with a digit, so there is no ambiguity. */
2901 bit = 1;
2902 m = e;
2903 for (q = "wzyx"; *q; q++, bit <<= 1)
2904 if (m > s && m[-1] == *q)
2905 {
2906 --m;
2907 channels |= bit;
2908 }
2909
2910 if (channels == 0
2911 || !mips_parse_register_1 (s, m, &symval)
2912 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2913 return FALSE;
2914 }
2915
a92713e6 2916 *sptr = e;
14daeee3
RS
2917 *symval_ptr = symval;
2918 if (channels_ptr)
2919 *channels_ptr = channels;
a92713e6
RS
2920 return TRUE;
2921}
2922
2923/* Check if SPTR points at a valid register specifier according to TYPES.
2924 If so, then return 1, advance S to consume the specifier and store
2925 the register's number in REGNOP, otherwise return 0. */
2926
2927static int
2928reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2929{
2930 unsigned int regno;
2931
14daeee3 2932 if (mips_parse_register (s, &regno, NULL))
707bfff6 2933 {
a92713e6
RS
2934 if (types & RTYPE_VEC)
2935 regno = mips_prefer_vec_regno (regno);
2936 if (regno & types)
2937 regno &= RNUM_MASK;
2938 else
2939 regno = ~0;
707bfff6 2940 }
a92713e6 2941 else
707bfff6 2942 {
a92713e6 2943 if (types & RWARN)
1661c76c 2944 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2945 regno = ~0;
707bfff6 2946 }
707bfff6 2947 if (regnop)
a92713e6
RS
2948 *regnop = regno;
2949 return regno <= RNUM_MASK;
707bfff6
TS
2950}
2951
14daeee3
RS
2952/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2953 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2954
2955static char *
2956mips_parse_vu0_channels (char *s, unsigned int *channels)
2957{
2958 unsigned int i;
2959
2960 *channels = 0;
2961 for (i = 0; i < 4; i++)
2962 if (*s == "xyzw"[i])
2963 {
2964 *channels |= 1 << (3 - i);
2965 ++s;
2966 }
2967 return s;
2968}
2969
a92713e6
RS
2970/* Token types for parsed operand lists. */
2971enum mips_operand_token_type {
2972 /* A plain register, e.g. $f2. */
2973 OT_REG,
df58fc94 2974
14daeee3
RS
2975 /* A 4-bit XYZW channel mask. */
2976 OT_CHANNELS,
2977
56d438b1
CF
2978 /* A constant vector index, e.g. [1]. */
2979 OT_INTEGER_INDEX,
2980
2981 /* A register vector index, e.g. [$2]. */
2982 OT_REG_INDEX,
df58fc94 2983
a92713e6
RS
2984 /* A continuous range of registers, e.g. $s0-$s4. */
2985 OT_REG_RANGE,
2986
2987 /* A (possibly relocated) expression. */
2988 OT_INTEGER,
2989
2990 /* A floating-point value. */
2991 OT_FLOAT,
2992
2993 /* A single character. This can be '(', ')' or ',', but '(' only appears
2994 before OT_REGs. */
2995 OT_CHAR,
2996
14daeee3
RS
2997 /* A doubled character, either "--" or "++". */
2998 OT_DOUBLE_CHAR,
2999
a92713e6
RS
3000 /* The end of the operand list. */
3001 OT_END
3002};
3003
3004/* A parsed operand token. */
3005struct mips_operand_token
3006{
3007 /* The type of token. */
3008 enum mips_operand_token_type type;
3009 union
3010 {
56d438b1 3011 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3012 unsigned int regno;
3013
14daeee3
RS
3014 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3015 unsigned int channels;
3016
56d438b1
CF
3017 /* The integer value of an OT_INTEGER_INDEX. */
3018 addressT index;
a92713e6
RS
3019
3020 /* The two register symbol values involved in an OT_REG_RANGE. */
3021 struct {
3022 unsigned int regno1;
3023 unsigned int regno2;
3024 } reg_range;
3025
3026 /* The value of an OT_INTEGER. The value is represented as an
3027 expression and the relocation operators that were applied to
3028 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3029 relocation operators were used. */
3030 struct {
3031 expressionS value;
3032 bfd_reloc_code_real_type relocs[3];
3033 } integer;
3034
3035 /* The binary data for an OT_FLOAT constant, and the number of bytes
3036 in the constant. */
3037 struct {
3038 unsigned char data[8];
3039 int length;
3040 } flt;
3041
14daeee3 3042 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3043 char ch;
3044 } u;
3045};
3046
3047/* An obstack used to construct lists of mips_operand_tokens. */
3048static struct obstack mips_operand_tokens;
3049
3050/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3051
3052static void
3053mips_add_token (struct mips_operand_token *token,
3054 enum mips_operand_token_type type)
3055{
3056 token->type = type;
3057 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3058}
3059
3060/* Check whether S is '(' followed by a register name. Add OT_CHAR
3061 and OT_REG tokens for them if so, and return a pointer to the first
3062 unconsumed character. Return null otherwise. */
3063
3064static char *
3065mips_parse_base_start (char *s)
3066{
3067 struct mips_operand_token token;
14daeee3
RS
3068 unsigned int regno, channels;
3069 bfd_boolean decrement_p;
df58fc94 3070
a92713e6
RS
3071 if (*s != '(')
3072 return 0;
3073
3074 ++s;
3075 SKIP_SPACE_TABS (s);
14daeee3
RS
3076
3077 /* Only match "--" as part of a base expression. In other contexts "--X"
3078 is a double negative. */
3079 decrement_p = (s[0] == '-' && s[1] == '-');
3080 if (decrement_p)
3081 {
3082 s += 2;
3083 SKIP_SPACE_TABS (s);
3084 }
3085
3086 /* Allow a channel specifier because that leads to better error messages
3087 than treating something like "$vf0x++" as an expression. */
3088 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3089 return 0;
3090
3091 token.u.ch = '(';
3092 mips_add_token (&token, OT_CHAR);
3093
14daeee3
RS
3094 if (decrement_p)
3095 {
3096 token.u.ch = '-';
3097 mips_add_token (&token, OT_DOUBLE_CHAR);
3098 }
3099
a92713e6
RS
3100 token.u.regno = regno;
3101 mips_add_token (&token, OT_REG);
3102
14daeee3
RS
3103 if (channels)
3104 {
3105 token.u.channels = channels;
3106 mips_add_token (&token, OT_CHANNELS);
3107 }
3108
3109 /* For consistency, only match "++" as part of base expressions too. */
3110 SKIP_SPACE_TABS (s);
3111 if (s[0] == '+' && s[1] == '+')
3112 {
3113 s += 2;
3114 token.u.ch = '+';
3115 mips_add_token (&token, OT_DOUBLE_CHAR);
3116 }
3117
a92713e6
RS
3118 return s;
3119}
3120
3121/* Parse one or more tokens from S. Return a pointer to the first
3122 unconsumed character on success. Return null if an error was found
3123 and store the error text in insn_error. FLOAT_FORMAT is as for
3124 mips_parse_arguments. */
3125
3126static char *
3127mips_parse_argument_token (char *s, char float_format)
3128{
6d4af3c2
AM
3129 char *end, *save_in;
3130 const char *err;
14daeee3 3131 unsigned int regno1, regno2, channels;
a92713e6
RS
3132 struct mips_operand_token token;
3133
3134 /* First look for "($reg", since we want to treat that as an
3135 OT_CHAR and OT_REG rather than an expression. */
3136 end = mips_parse_base_start (s);
3137 if (end)
3138 return end;
3139
3140 /* Handle other characters that end up as OT_CHARs. */
3141 if (*s == ')' || *s == ',')
3142 {
3143 token.u.ch = *s;
3144 mips_add_token (&token, OT_CHAR);
3145 ++s;
3146 return s;
3147 }
3148
3149 /* Handle tokens that start with a register. */
14daeee3 3150 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3151 {
14daeee3
RS
3152 if (channels)
3153 {
3154 /* A register and a VU0 channel suffix. */
3155 token.u.regno = regno1;
3156 mips_add_token (&token, OT_REG);
3157
3158 token.u.channels = channels;
3159 mips_add_token (&token, OT_CHANNELS);
3160 return s;
3161 }
3162
a92713e6
RS
3163 SKIP_SPACE_TABS (s);
3164 if (*s == '-')
df58fc94 3165 {
a92713e6
RS
3166 /* A register range. */
3167 ++s;
3168 SKIP_SPACE_TABS (s);
14daeee3 3169 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3170 {
1661c76c 3171 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3172 return 0;
3173 }
df58fc94 3174
a92713e6
RS
3175 token.u.reg_range.regno1 = regno1;
3176 token.u.reg_range.regno2 = regno2;
3177 mips_add_token (&token, OT_REG_RANGE);
3178 return s;
3179 }
a92713e6 3180
56d438b1
CF
3181 /* Add the register itself. */
3182 token.u.regno = regno1;
3183 mips_add_token (&token, OT_REG);
3184
3185 /* Check for a vector index. */
3186 if (*s == '[')
3187 {
a92713e6
RS
3188 ++s;
3189 SKIP_SPACE_TABS (s);
56d438b1
CF
3190 if (mips_parse_register (&s, &token.u.regno, NULL))
3191 mips_add_token (&token, OT_REG_INDEX);
3192 else
a92713e6 3193 {
56d438b1
CF
3194 expressionS element;
3195
3196 my_getExpression (&element, s);
3197 if (element.X_op != O_constant)
3198 {
3199 set_insn_error (0, _("vector element must be constant"));
3200 return 0;
3201 }
3202 s = expr_end;
3203 token.u.index = element.X_add_number;
3204 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3205 }
a92713e6
RS
3206 SKIP_SPACE_TABS (s);
3207 if (*s != ']')
3208 {
1661c76c 3209 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3210 return 0;
3211 }
3212 ++s;
df58fc94 3213 }
a92713e6 3214 return s;
df58fc94
RS
3215 }
3216
a92713e6
RS
3217 if (float_format)
3218 {
3219 /* First try to treat expressions as floats. */
3220 save_in = input_line_pointer;
3221 input_line_pointer = s;
3222 err = md_atof (float_format, (char *) token.u.flt.data,
3223 &token.u.flt.length);
3224 end = input_line_pointer;
3225 input_line_pointer = save_in;
3226 if (err && *err)
3227 {
e3de51ce 3228 set_insn_error (0, err);
a92713e6
RS
3229 return 0;
3230 }
3231 if (s != end)
3232 {
3233 mips_add_token (&token, OT_FLOAT);
3234 return end;
3235 }
3236 }
3237
3238 /* Treat everything else as an integer expression. */
3239 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3240 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3241 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3242 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3243 s = expr_end;
3244 mips_add_token (&token, OT_INTEGER);
3245 return s;
3246}
3247
3248/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3249 if expressions should be treated as 32-bit floating-point constants,
3250 'd' if they should be treated as 64-bit floating-point constants,
3251 or 0 if they should be treated as integer expressions (the usual case).
3252
3253 Return a list of tokens on success, otherwise return 0. The caller
3254 must obstack_free the list after use. */
3255
3256static struct mips_operand_token *
3257mips_parse_arguments (char *s, char float_format)
3258{
3259 struct mips_operand_token token;
3260
3261 SKIP_SPACE_TABS (s);
3262 while (*s)
3263 {
3264 s = mips_parse_argument_token (s, float_format);
3265 if (!s)
3266 {
3267 obstack_free (&mips_operand_tokens,
3268 obstack_finish (&mips_operand_tokens));
3269 return 0;
3270 }
3271 SKIP_SPACE_TABS (s);
3272 }
3273 mips_add_token (&token, OT_END);
3274 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3275}
3276
d301a56b
RS
3277/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3278 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3279
3280static bfd_boolean
f79e2745 3281is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3282{
3283 int isa = mips_opts.isa;
846ef2d0 3284 int ase = mips_opts.ase;
037b32b9 3285 int fp_s, fp_d;
c6278170 3286 unsigned int i;
037b32b9 3287
be0fcbee 3288 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3289 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3290 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3291 ase |= mips_ases[i].flags64;
037b32b9 3292
d301a56b 3293 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3294 return FALSE;
3295
3296 /* Check whether the instruction or macro requires single-precision or
3297 double-precision floating-point support. Note that this information is
3298 stored differently in the opcode table for insns and macros. */
3299 if (mo->pinfo == INSN_MACRO)
3300 {
3301 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3302 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3303 }
3304 else
3305 {
3306 fp_s = mo->pinfo & FP_S;
3307 fp_d = mo->pinfo & FP_D;
3308 }
3309
3310 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3311 return FALSE;
3312
3313 if (fp_s && mips_opts.soft_float)
3314 return FALSE;
3315
3316 return TRUE;
3317}
3318
3319/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3320 selected ISA and architecture. */
3321
3322static bfd_boolean
3323is_opcode_valid_16 (const struct mips_opcode *mo)
3324{
25499ac7
MR
3325 int isa = mips_opts.isa;
3326 int ase = mips_opts.ase;
3327 unsigned int i;
3328
3329 if (ISA_HAS_64BIT_REGS (isa))
3330 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3331 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3332 ase |= mips_ases[i].flags64;
3333
3334 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3335}
3336
df58fc94 3337/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3338 explicitly requested. Always TRUE in the standard MIPS mode.
3339 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3340
3341static bfd_boolean
3342is_size_valid (const struct mips_opcode *mo)
3343{
3344 if (!mips_opts.micromips)
3345 return TRUE;
3346
833794fc
MR
3347 if (mips_opts.insn32)
3348 {
3349 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3350 return FALSE;
3351 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3352 return FALSE;
3353 }
df58fc94
RS
3354 if (!forced_insn_length)
3355 return TRUE;
3356 if (mo->pinfo == INSN_MACRO)
3357 return FALSE;
3358 return forced_insn_length == micromips_insn_length (mo);
3359}
3360
7fd53920
MR
3361/* Return TRUE if the size of the MIPS16 opcode MO matches one
3362 explicitly requested. */
3363
3364static bfd_boolean
3365is_size_valid_16 (const struct mips_opcode *mo)
3366{
3367 if (!forced_insn_length)
3368 return TRUE;
3369 if (mo->pinfo == INSN_MACRO)
3370 return FALSE;
3371 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3372 return FALSE;
0674ee5d
MR
3373 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3374 return FALSE;
7fd53920
MR
3375 return TRUE;
3376}
3377
df58fc94 3378/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3379 of the preceding instruction. Always TRUE in the standard MIPS mode.
3380
3381 We don't accept macros in 16-bit delay slots to avoid a case where
3382 a macro expansion fails because it relies on a preceding 32-bit real
3383 instruction to have matched and does not handle the operands correctly.
3384 The only macros that may expand to 16-bit instructions are JAL that
3385 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3386 and BGT (that likewise cannot be placed in a delay slot) that decay to
3387 a NOP. In all these cases the macros precede any corresponding real
3388 instruction definitions in the opcode table, so they will match in the
3389 second pass where the size of the delay slot is ignored and therefore
3390 produce correct code. */
df58fc94
RS
3391
3392static bfd_boolean
3393is_delay_slot_valid (const struct mips_opcode *mo)
3394{
3395 if (!mips_opts.micromips)
3396 return TRUE;
3397
3398 if (mo->pinfo == INSN_MACRO)
c06dec14 3399 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3400 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3401 && micromips_insn_length (mo) != 4)
3402 return FALSE;
3403 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3404 && micromips_insn_length (mo) != 2)
3405 return FALSE;
3406
3407 return TRUE;
3408}
3409
fc76e730
RS
3410/* For consistency checking, verify that all bits of OPCODE are specified
3411 either by the match/mask part of the instruction definition, or by the
3412 operand list. Also build up a list of operands in OPERANDS.
3413
3414 INSN_BITS says which bits of the instruction are significant.
3415 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3416 provides the mips_operand description of each operand. DECODE_OPERAND
3417 is null for MIPS16 instructions. */
ab902481
RS
3418
3419static int
3420validate_mips_insn (const struct mips_opcode *opcode,
3421 unsigned long insn_bits,
fc76e730
RS
3422 const struct mips_operand *(*decode_operand) (const char *),
3423 struct mips_operand_array *operands)
ab902481
RS
3424{
3425 const char *s;
fc76e730 3426 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3427 const struct mips_operand *operand;
3428
fc76e730
RS
3429 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3430 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3431 {
3432 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3433 opcode->name, opcode->args);
3434 return 0;
3435 }
3436 used_bits = 0;
fc76e730 3437 opno = 0;
14daeee3
RS
3438 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3439 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3440 for (s = opcode->args; *s; ++s)
3441 switch (*s)
3442 {
3443 case ',':
3444 case '(':
3445 case ')':
3446 break;
3447
14daeee3
RS
3448 case '#':
3449 s++;
3450 break;
3451
ab902481 3452 default:
fc76e730 3453 if (!decode_operand)
7fd53920 3454 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3455 else
3456 operand = decode_operand (s);
3457 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3458 {
3459 as_bad (_("internal: unknown operand type: %s %s"),
3460 opcode->name, opcode->args);
3461 return 0;
3462 }
fc76e730
RS
3463 gas_assert (opno < MAX_OPERANDS);
3464 operands->operand[opno] = operand;
25499ac7
MR
3465 if (!decode_operand && operand
3466 && operand->type == OP_INT && operand->lsb == 0
3467 && mips_opcode_32bit_p (opcode))
3468 used_bits |= mips16_immed_extend (-1, operand->size);
3469 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3470 {
14daeee3 3471 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3472 if (operand->type == OP_MDMX_IMM_REG)
3473 /* Bit 5 is the format selector (OB vs QH). The opcode table
3474 has separate entries for each format. */
3475 used_bits &= ~(1 << (operand->lsb + 5));
3476 if (operand->type == OP_ENTRY_EXIT_LIST)
3477 used_bits &= ~(mask & 0x700);
38bf472a
MR
3478 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3479 operand field that cannot be fully described with LSB/SIZE. */
3480 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3481 used_bits &= ~0x6000;
fc76e730 3482 }
ab902481 3483 /* Skip prefix characters. */
7361da2c 3484 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3485 ++s;
fc76e730 3486 opno += 1;
ab902481
RS
3487 break;
3488 }
fc76e730 3489 doubled = used_bits & mask & insn_bits;
ab902481
RS
3490 if (doubled)
3491 {
3492 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3493 " %s %s"), doubled, opcode->name, opcode->args);
3494 return 0;
3495 }
fc76e730 3496 used_bits |= mask;
ab902481 3497 undefined = ~used_bits & insn_bits;
fc76e730 3498 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3499 {
3500 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3501 undefined, opcode->name, opcode->args);
3502 return 0;
3503 }
3504 used_bits &= ~insn_bits;
3505 if (used_bits)
3506 {
3507 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3508 used_bits, opcode->name, opcode->args);
3509 return 0;
3510 }
3511 return 1;
3512}
3513
fc76e730
RS
3514/* The MIPS16 version of validate_mips_insn. */
3515
3516static int
3517validate_mips16_insn (const struct mips_opcode *opcode,
3518 struct mips_operand_array *operands)
3519{
7fd53920 3520 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3521
7fd53920 3522 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3523}
3524
ab902481
RS
3525/* The microMIPS version of validate_mips_insn. */
3526
3527static int
fc76e730
RS
3528validate_micromips_insn (const struct mips_opcode *opc,
3529 struct mips_operand_array *operands)
ab902481
RS
3530{
3531 unsigned long insn_bits;
3532 unsigned long major;
3533 unsigned int length;
3534
fc76e730
RS
3535 if (opc->pinfo == INSN_MACRO)
3536 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3537 operands);
3538
ab902481
RS
3539 length = micromips_insn_length (opc);
3540 if (length != 2 && length != 4)
3541 {
1661c76c 3542 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3543 "%s %s"), length, opc->name, opc->args);
3544 return 0;
3545 }
3546 major = opc->match >> (10 + 8 * (length - 2));
3547 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3548 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3549 {
1661c76c 3550 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3551 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3552 return 0;
3553 }
3554
3555 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3556 insn_bits = 1 << 4 * length;
3557 insn_bits <<= 4 * length;
3558 insn_bits -= 1;
fc76e730
RS
3559 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3560 operands);
ab902481
RS
3561}
3562
707bfff6
TS
3563/* This function is called once, at assembler startup time. It should set up
3564 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3565
252b5132 3566void
17a2f251 3567md_begin (void)
252b5132 3568{
3994f87e 3569 const char *retval = NULL;
156c2f8b 3570 int i = 0;
252b5132 3571 int broken = 0;
1f25f5d3 3572
0a44bf69
RS
3573 if (mips_pic != NO_PIC)
3574 {
3575 if (g_switch_seen && g_switch_value != 0)
3576 as_bad (_("-G may not be used in position-independent code"));
3577 g_switch_value = 0;
3578 }
00acd688
CM
3579 else if (mips_abicalls)
3580 {
3581 if (g_switch_seen && g_switch_value != 0)
3582 as_bad (_("-G may not be used with abicalls"));
3583 g_switch_value = 0;
3584 }
0a44bf69 3585
0b35dfee 3586 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3587 as_warn (_("could not set architecture and machine"));
252b5132 3588
252b5132
RH
3589 op_hash = hash_new ();
3590
fc76e730 3591 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3592 for (i = 0; i < NUMOPCODES;)
3593 {
3594 const char *name = mips_opcodes[i].name;
3595
17a2f251 3596 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3597 if (retval != NULL)
3598 {
3599 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3600 mips_opcodes[i].name, retval);
3601 /* Probably a memory allocation problem? Give up now. */
1661c76c 3602 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3603 }
3604 do
3605 {
fc76e730
RS
3606 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3607 decode_mips_operand, &mips_operands[i]))
3608 broken = 1;
3609 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3610 {
fc76e730
RS
3611 create_insn (&nop_insn, mips_opcodes + i);
3612 if (mips_fix_loongson2f_nop)
3613 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3614 nop_insn.fixed_p = 1;
252b5132
RH
3615 }
3616 ++i;
3617 }
3618 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3619 }
3620
3621 mips16_op_hash = hash_new ();
fc76e730
RS
3622 mips16_operands = XCNEWVEC (struct mips_operand_array,
3623 bfd_mips16_num_opcodes);
252b5132
RH
3624
3625 i = 0;
3626 while (i < bfd_mips16_num_opcodes)
3627 {
3628 const char *name = mips16_opcodes[i].name;
3629
17a2f251 3630 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3631 if (retval != NULL)
3632 as_fatal (_("internal: can't hash `%s': %s"),
3633 mips16_opcodes[i].name, retval);
3634 do
3635 {
fc76e730
RS
3636 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3637 broken = 1;
1e915849
RS
3638 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3639 {
3640 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3641 mips16_nop_insn.fixed_p = 1;
3642 }
252b5132
RH
3643 ++i;
3644 }
3645 while (i < bfd_mips16_num_opcodes
3646 && strcmp (mips16_opcodes[i].name, name) == 0);
3647 }
3648
df58fc94 3649 micromips_op_hash = hash_new ();
fc76e730
RS
3650 micromips_operands = XCNEWVEC (struct mips_operand_array,
3651 bfd_micromips_num_opcodes);
df58fc94
RS
3652
3653 i = 0;
3654 while (i < bfd_micromips_num_opcodes)
3655 {
3656 const char *name = micromips_opcodes[i].name;
3657
3658 retval = hash_insert (micromips_op_hash, name,
3659 (void *) &micromips_opcodes[i]);
3660 if (retval != NULL)
3661 as_fatal (_("internal: can't hash `%s': %s"),
3662 micromips_opcodes[i].name, retval);
3663 do
fc76e730
RS
3664 {
3665 struct mips_cl_insn *micromips_nop_insn;
3666
3667 if (!validate_micromips_insn (&micromips_opcodes[i],
3668 &micromips_operands[i]))
3669 broken = 1;
3670
3671 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3672 {
3673 if (micromips_insn_length (micromips_opcodes + i) == 2)
3674 micromips_nop_insn = &micromips_nop16_insn;
3675 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3676 micromips_nop_insn = &micromips_nop32_insn;
3677 else
3678 continue;
3679
3680 if (micromips_nop_insn->insn_mo == NULL
3681 && strcmp (name, "nop") == 0)
3682 {
3683 create_insn (micromips_nop_insn, micromips_opcodes + i);
3684 micromips_nop_insn->fixed_p = 1;
3685 }
3686 }
3687 }
df58fc94
RS
3688 while (++i < bfd_micromips_num_opcodes
3689 && strcmp (micromips_opcodes[i].name, name) == 0);
3690 }
3691
252b5132 3692 if (broken)
1661c76c 3693 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3694
3695 /* We add all the general register names to the symbol table. This
3696 helps us detect invalid uses of them. */
3739860c 3697 for (i = 0; reg_names[i].name; i++)
707bfff6 3698 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3699 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3700 &zero_address_frag));
3701 if (HAVE_NEWABI)
3739860c 3702 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3703 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3704 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3705 &zero_address_frag));
707bfff6 3706 else
3739860c 3707 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3708 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3709 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3710 &zero_address_frag));
6047c971 3711
14daeee3
RS
3712 for (i = 0; i < 32; i++)
3713 {
92fce9bd 3714 char regname[6];
14daeee3
RS
3715
3716 /* R5900 VU0 floating-point register. */
92fce9bd 3717 sprintf (regname, "$vf%d", i);
14daeee3
RS
3718 symbol_table_insert (symbol_new (regname, reg_section,
3719 RTYPE_VF | i, &zero_address_frag));
3720
3721 /* R5900 VU0 integer register. */
92fce9bd 3722 sprintf (regname, "$vi%d", i);
14daeee3
RS
3723 symbol_table_insert (symbol_new (regname, reg_section,
3724 RTYPE_VI | i, &zero_address_frag));
3725
56d438b1 3726 /* MSA register. */
92fce9bd 3727 sprintf (regname, "$w%d", i);
56d438b1
CF
3728 symbol_table_insert (symbol_new (regname, reg_section,
3729 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3730 }
3731
a92713e6
RS
3732 obstack_init (&mips_operand_tokens);
3733
7d10b47d 3734 mips_no_prev_insn ();
252b5132
RH
3735
3736 mips_gprmask = 0;
3737 mips_cprmask[0] = 0;
3738 mips_cprmask[1] = 0;
3739 mips_cprmask[2] = 0;
3740 mips_cprmask[3] = 0;
3741
3742 /* set the default alignment for the text section (2**2) */
3743 record_alignment (text_section, 2);
3744
4d0d148d 3745 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3746
f3ded42a
RS
3747 /* On a native system other than VxWorks, sections must be aligned
3748 to 16 byte boundaries. When configured for an embedded ELF
3749 target, we don't bother. */
3750 if (strncmp (TARGET_OS, "elf", 3) != 0
3751 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3752 {
f3ded42a
RS
3753 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3754 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3755 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3756 }
252b5132 3757
f3ded42a
RS
3758 /* Create a .reginfo section for register masks and a .mdebug
3759 section for debugging information. */
3760 {
3761 segT seg;
3762 subsegT subseg;
3763 flagword flags;
3764 segT sec;
3765
3766 seg = now_seg;
3767 subseg = now_subseg;
3768
3769 /* The ABI says this section should be loaded so that the
3770 running program can access it. However, we don't load it
3771 if we are configured for an embedded target */
3772 flags = SEC_READONLY | SEC_DATA;
3773 if (strncmp (TARGET_OS, "elf", 3) != 0)
3774 flags |= SEC_ALLOC | SEC_LOAD;
3775
3776 if (mips_abi != N64_ABI)
252b5132 3777 {
f3ded42a 3778 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3779
f3ded42a
RS
3780 bfd_set_section_flags (stdoutput, sec, flags);
3781 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3782
f3ded42a
RS
3783 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3784 }
3785 else
3786 {
3787 /* The 64-bit ABI uses a .MIPS.options section rather than
3788 .reginfo section. */
3789 sec = subseg_new (".MIPS.options", (subsegT) 0);
3790 bfd_set_section_flags (stdoutput, sec, flags);
3791 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3792
f3ded42a
RS
3793 /* Set up the option header. */
3794 {
3795 Elf_Internal_Options opthdr;
3796 char *f;
3797
3798 opthdr.kind = ODK_REGINFO;
3799 opthdr.size = (sizeof (Elf_External_Options)
3800 + sizeof (Elf64_External_RegInfo));
3801 opthdr.section = 0;
3802 opthdr.info = 0;
3803 f = frag_more (sizeof (Elf_External_Options));
3804 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3805 (Elf_External_Options *) f);
3806
3807 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3808 }
3809 }
252b5132 3810
351cdf24
MF
3811 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3812 bfd_set_section_flags (stdoutput, sec,
3813 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3814 bfd_set_section_alignment (stdoutput, sec, 3);
3815 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3816
f3ded42a
RS
3817 if (ECOFF_DEBUGGING)
3818 {
3819 sec = subseg_new (".mdebug", (subsegT) 0);
3820 (void) bfd_set_section_flags (stdoutput, sec,
3821 SEC_HAS_CONTENTS | SEC_READONLY);
3822 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3823 }
f3ded42a
RS
3824 else if (mips_flag_pdr)
3825 {
3826 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3827 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3828 SEC_READONLY | SEC_RELOC
3829 | SEC_DEBUGGING);
3830 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3831 }
3832
3833 subseg_set (seg, subseg);
3834 }
252b5132 3835
71400594
RS
3836 if (mips_fix_vr4120)
3837 init_vr4120_conflicts ();
252b5132
RH
3838}
3839
351cdf24
MF
3840static inline void
3841fpabi_incompatible_with (int fpabi, const char *what)
3842{
3843 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3844 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3845}
3846
3847static inline void
3848fpabi_requires (int fpabi, const char *what)
3849{
3850 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3851 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3852}
3853
3854/* Check -mabi and register sizes against the specified FP ABI. */
3855static void
3856check_fpabi (int fpabi)
3857{
351cdf24
MF
3858 switch (fpabi)
3859 {
3860 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3861 if (file_mips_opts.soft_float)
3862 fpabi_incompatible_with (fpabi, "softfloat");
3863 else if (file_mips_opts.single_float)
3864 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3865 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3866 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3867 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3868 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3869 break;
3870
3871 case Val_GNU_MIPS_ABI_FP_XX:
3872 if (mips_abi != O32_ABI)
3873 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3874 else if (file_mips_opts.soft_float)
3875 fpabi_incompatible_with (fpabi, "softfloat");
3876 else if (file_mips_opts.single_float)
3877 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3878 else if (file_mips_opts.fp != 0)
3879 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3880 break;
3881
3882 case Val_GNU_MIPS_ABI_FP_64A:
3883 case Val_GNU_MIPS_ABI_FP_64:
3884 if (mips_abi != O32_ABI)
3885 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3886 else if (file_mips_opts.soft_float)
3887 fpabi_incompatible_with (fpabi, "softfloat");
3888 else if (file_mips_opts.single_float)
3889 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3890 else if (file_mips_opts.fp != 64)
3891 fpabi_requires (fpabi, "fp=64");
3892 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3893 fpabi_incompatible_with (fpabi, "nooddspreg");
3894 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3895 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3896 break;
3897
3898 case Val_GNU_MIPS_ABI_FP_SINGLE:
3899 if (file_mips_opts.soft_float)
3900 fpabi_incompatible_with (fpabi, "softfloat");
3901 else if (!file_mips_opts.single_float)
3902 fpabi_requires (fpabi, "singlefloat");
3903 break;
3904
3905 case Val_GNU_MIPS_ABI_FP_SOFT:
3906 if (!file_mips_opts.soft_float)
3907 fpabi_requires (fpabi, "softfloat");
3908 break;
3909
3910 case Val_GNU_MIPS_ABI_FP_OLD_64:
3911 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3912 Tag_GNU_MIPS_ABI_FP, fpabi);
3913 break;
3914
3350cc01
CM
3915 case Val_GNU_MIPS_ABI_FP_NAN2008:
3916 /* Silently ignore compatibility value. */
3917 break;
3918
351cdf24
MF
3919 default:
3920 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3921 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3922 break;
3923 }
351cdf24
MF
3924}
3925
919731af 3926/* Perform consistency checks on the current options. */
3927
3928static void
3929mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3930{
3931 /* Check the size of integer registers agrees with the ABI and ISA. */
3932 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3933 as_bad (_("`gp=64' used with a 32-bit processor"));
3934 else if (abi_checks
3935 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3936 as_bad (_("`gp=32' used with a 64-bit ABI"));
3937 else if (abi_checks
3938 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3939 as_bad (_("`gp=64' used with a 32-bit ABI"));
3940
3941 /* Check the size of the float registers agrees with the ABI and ISA. */
3942 switch (opts->fp)
3943 {
351cdf24
MF
3944 case 0:
3945 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3946 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3947 else if (opts->single_float == 1)
3948 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3949 break;
919731af 3950 case 64:
3951 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3952 as_bad (_("`fp=64' used with a 32-bit fpu"));
3953 else if (abi_checks
3954 && ABI_NEEDS_32BIT_REGS (mips_abi)
3955 && !ISA_HAS_MXHC1 (opts->isa))
3956 as_warn (_("`fp=64' used with a 32-bit ABI"));
3957 break;
3958 case 32:
3959 if (abi_checks
3960 && ABI_NEEDS_64BIT_REGS (mips_abi))
3961 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3962 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3963 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3964 break;
3965 default:
3966 as_bad (_("Unknown size of floating point registers"));
3967 break;
3968 }
3969
351cdf24
MF
3970 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3971 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3972
919731af 3973 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3974 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3975 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3976 && (opts->micromips == 1
3977 || opts->mips16 == 1))
1357373c 3978 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3979 opts->micromips ? "micromips" : "mips16",
5f4678bb 3980 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3981
3982 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3983 as_fatal (_("branch relaxation is not supported in `%s'"),
3984 mips_cpu_info_from_isa (opts->isa)->name);
919731af 3985}
3986
3987/* Perform consistency checks on the module level options exactly once.
3988 This is a deferred check that happens:
3989 at the first .set directive
3990 or, at the first pseudo op that generates code (inc .dc.a)
3991 or, at the first instruction
3992 or, at the end. */
3993
3994static void
3995file_mips_check_options (void)
3996{
3997 const struct mips_cpu_info *arch_info = 0;
3998
3999 if (file_mips_opts_checked)
4000 return;
4001
4002 /* The following code determines the register size.
4003 Similar code was added to GCC 3.3 (see override_options() in
4004 config/mips/mips.c). The GAS and GCC code should be kept in sync
4005 as much as possible. */
4006
4007 if (file_mips_opts.gp < 0)
4008 {
4009 /* Infer the integer register size from the ABI and processor.
4010 Restrict ourselves to 32-bit registers if that's all the
4011 processor has, or if the ABI cannot handle 64-bit registers. */
4012 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4013 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4014 ? 32 : 64;
4015 }
4016
4017 if (file_mips_opts.fp < 0)
4018 {
4019 /* No user specified float register size.
4020 ??? GAS treats single-float processors as though they had 64-bit
4021 float registers (although it complains when double-precision
4022 instructions are used). As things stand, saying they have 32-bit
4023 registers would lead to spurious "register must be even" messages.
4024 So here we assume float registers are never smaller than the
4025 integer ones. */
4026 if (file_mips_opts.gp == 64)
4027 /* 64-bit integer registers implies 64-bit float registers. */
4028 file_mips_opts.fp = 64;
4029 else if ((file_mips_opts.ase & FP64_ASES)
4030 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4031 /* Handle ASEs that require 64-bit float registers, if possible. */
4032 file_mips_opts.fp = 64;
7361da2c
AB
4033 else if (ISA_IS_R6 (mips_opts.isa))
4034 /* R6 implies 64-bit float registers. */
4035 file_mips_opts.fp = 64;
919731af 4036 else
4037 /* 32-bit float registers. */
4038 file_mips_opts.fp = 32;
4039 }
4040
4041 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4042
351cdf24
MF
4043 /* Disable operations on odd-numbered floating-point registers by default
4044 when using the FPXX ABI. */
4045 if (file_mips_opts.oddspreg < 0)
4046 {
4047 if (file_mips_opts.fp == 0)
4048 file_mips_opts.oddspreg = 0;
4049 else
4050 file_mips_opts.oddspreg = 1;
4051 }
4052
919731af 4053 /* End of GCC-shared inference code. */
4054
4055 /* This flag is set when we have a 64-bit capable CPU but use only
4056 32-bit wide registers. Note that EABI does not use it. */
4057 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4058 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4059 || mips_abi == O32_ABI))
4060 mips_32bitmode = 1;
4061
4062 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4063 as_bad (_("trap exception not supported at ISA 1"));
4064
4065 /* If the selected architecture includes support for ASEs, enable
4066 generation of code for them. */
4067 if (file_mips_opts.mips16 == -1)
4068 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4069 if (file_mips_opts.micromips == -1)
4070 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4071 ? 1 : 0;
4072
7361da2c
AB
4073 if (mips_nan2008 == -1)
4074 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4075 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4076 as_fatal (_("`%s' does not support legacy NaN"),
4077 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4078
919731af 4079 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4080 being selected implicitly. */
4081 if (file_mips_opts.fp != 64)
4082 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4083
4084 /* If the user didn't explicitly select or deselect a particular ASE,
4085 use the default setting for the CPU. */
4086 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4087
4088 /* Set up the current options. These may change throughout assembly. */
4089 mips_opts = file_mips_opts;
4090
4091 mips_check_isa_supports_ases ();
4092 mips_check_options (&file_mips_opts, TRUE);
4093 file_mips_opts_checked = TRUE;
4094
4095 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4096 as_warn (_("could not set architecture and machine"));
4097}
4098
252b5132 4099void
17a2f251 4100md_assemble (char *str)
252b5132
RH
4101{
4102 struct mips_cl_insn insn;
f6688943
TS
4103 bfd_reloc_code_real_type unused_reloc[3]
4104 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4105
919731af 4106 file_mips_check_options ();
4107
252b5132 4108 imm_expr.X_op = O_absent;
252b5132 4109 offset_expr.X_op = O_absent;
f6688943
TS
4110 offset_reloc[0] = BFD_RELOC_UNUSED;
4111 offset_reloc[1] = BFD_RELOC_UNUSED;
4112 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4113
e1b47bd5
RS
4114 mips_mark_labels ();
4115 mips_assembling_insn = TRUE;
e3de51ce 4116 clear_insn_error ();
e1b47bd5 4117
252b5132
RH
4118 if (mips_opts.mips16)
4119 mips16_ip (str, &insn);
4120 else
4121 {
4122 mips_ip (str, &insn);
beae10d5
KH
4123 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4124 str, insn.insn_opcode));
252b5132
RH
4125 }
4126
e3de51ce
RS
4127 if (insn_error.msg)
4128 report_insn_error (str);
e1b47bd5 4129 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4130 {
584892a6 4131 macro_start ();
252b5132
RH
4132 if (mips_opts.mips16)
4133 mips16_macro (&insn);
4134 else
833794fc 4135 macro (&insn, str);
584892a6 4136 macro_end ();
252b5132
RH
4137 }
4138 else
4139 {
77bd4346 4140 if (offset_expr.X_op != O_absent)
df58fc94 4141 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4142 else
df58fc94 4143 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4144 }
e1b47bd5
RS
4145
4146 mips_assembling_insn = FALSE;
252b5132
RH
4147}
4148
738e5348
RS
4149/* Convenience functions for abstracting away the differences between
4150 MIPS16 and non-MIPS16 relocations. */
4151
4152static inline bfd_boolean
4153mips16_reloc_p (bfd_reloc_code_real_type reloc)
4154{
4155 switch (reloc)
4156 {
4157 case BFD_RELOC_MIPS16_JMP:
4158 case BFD_RELOC_MIPS16_GPREL:
4159 case BFD_RELOC_MIPS16_GOT16:
4160 case BFD_RELOC_MIPS16_CALL16:
4161 case BFD_RELOC_MIPS16_HI16_S:
4162 case BFD_RELOC_MIPS16_HI16:
4163 case BFD_RELOC_MIPS16_LO16:
c9775dde 4164 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4165 return TRUE;
4166
4167 default:
4168 return FALSE;
4169 }
4170}
4171
df58fc94
RS
4172static inline bfd_boolean
4173micromips_reloc_p (bfd_reloc_code_real_type reloc)
4174{
4175 switch (reloc)
4176 {
4177 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4178 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4179 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4180 case BFD_RELOC_MICROMIPS_GPREL16:
4181 case BFD_RELOC_MICROMIPS_JMP:
4182 case BFD_RELOC_MICROMIPS_HI16:
4183 case BFD_RELOC_MICROMIPS_HI16_S:
4184 case BFD_RELOC_MICROMIPS_LO16:
4185 case BFD_RELOC_MICROMIPS_LITERAL:
4186 case BFD_RELOC_MICROMIPS_GOT16:
4187 case BFD_RELOC_MICROMIPS_CALL16:
4188 case BFD_RELOC_MICROMIPS_GOT_HI16:
4189 case BFD_RELOC_MICROMIPS_GOT_LO16:
4190 case BFD_RELOC_MICROMIPS_CALL_HI16:
4191 case BFD_RELOC_MICROMIPS_CALL_LO16:
4192 case BFD_RELOC_MICROMIPS_SUB:
4193 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4194 case BFD_RELOC_MICROMIPS_GOT_OFST:
4195 case BFD_RELOC_MICROMIPS_GOT_DISP:
4196 case BFD_RELOC_MICROMIPS_HIGHEST:
4197 case BFD_RELOC_MICROMIPS_HIGHER:
4198 case BFD_RELOC_MICROMIPS_SCN_DISP:
4199 case BFD_RELOC_MICROMIPS_JALR:
4200 return TRUE;
4201
4202 default:
4203 return FALSE;
4204 }
4205}
4206
2309ddf2
MR
4207static inline bfd_boolean
4208jmp_reloc_p (bfd_reloc_code_real_type reloc)
4209{
4210 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4211}
4212
0e9c5a5c
MR
4213static inline bfd_boolean
4214b_reloc_p (bfd_reloc_code_real_type reloc)
4215{
4216 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4217 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4218 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4219 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4220 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4221 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4222 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4223}
4224
738e5348
RS
4225static inline bfd_boolean
4226got16_reloc_p (bfd_reloc_code_real_type reloc)
4227{
2309ddf2 4228 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4229 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4230}
4231
4232static inline bfd_boolean
4233hi16_reloc_p (bfd_reloc_code_real_type reloc)
4234{
2309ddf2 4235 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4236 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4237}
4238
4239static inline bfd_boolean
4240lo16_reloc_p (bfd_reloc_code_real_type reloc)
4241{
2309ddf2 4242 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4243 || reloc == BFD_RELOC_MICROMIPS_LO16);
4244}
4245
df58fc94
RS
4246static inline bfd_boolean
4247jalr_reloc_p (bfd_reloc_code_real_type reloc)
4248{
2309ddf2 4249 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4250}
4251
f2ae14a1
RS
4252static inline bfd_boolean
4253gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4254{
4255 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4256 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4257}
4258
2de39019
CM
4259/* Return true if RELOC is a PC-relative relocation that does not have
4260 full address range. */
4261
4262static inline bfd_boolean
4263limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4264{
4265 switch (reloc)
4266 {
4267 case BFD_RELOC_16_PCREL_S2:
c9775dde 4268 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4269 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4270 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4271 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4272 case BFD_RELOC_MIPS_21_PCREL_S2:
4273 case BFD_RELOC_MIPS_26_PCREL_S2:
4274 case BFD_RELOC_MIPS_18_PCREL_S3:
4275 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4276 return TRUE;
4277
b47468a6 4278 case BFD_RELOC_32_PCREL:
7361da2c
AB
4279 case BFD_RELOC_HI16_S_PCREL:
4280 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4281 return HAVE_64BIT_ADDRESSES;
4282
2de39019
CM
4283 default:
4284 return FALSE;
4285 }
4286}
b47468a6 4287
5919d012 4288/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4289 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4290 need a matching %lo() when applied to local symbols. */
5919d012
RS
4291
4292static inline bfd_boolean
17a2f251 4293reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4294{
3b91255e 4295 return (HAVE_IN_PLACE_ADDENDS
738e5348 4296 && (hi16_reloc_p (reloc)
0a44bf69
RS
4297 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4298 all GOT16 relocations evaluate to "G". */
738e5348
RS
4299 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4300}
4301
4302/* Return the type of %lo() reloc needed by RELOC, given that
4303 reloc_needs_lo_p. */
4304
4305static inline bfd_reloc_code_real_type
4306matching_lo_reloc (bfd_reloc_code_real_type reloc)
4307{
df58fc94
RS
4308 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4309 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4310 : BFD_RELOC_LO16));
5919d012
RS
4311}
4312
4313/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4314 relocation. */
4315
4316static inline bfd_boolean
17a2f251 4317fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4318{
4319 return (fixp->fx_next != NULL
738e5348 4320 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4321 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4322 && fixp->fx_offset == fixp->fx_next->fx_offset);
4323}
4324
462427c4
RS
4325/* Move all labels in LABELS to the current insertion point. TEXT_P
4326 says whether the labels refer to text or data. */
404a8071
RS
4327
4328static void
462427c4 4329mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4330{
4331 struct insn_label_list *l;
4332 valueT val;
4333
462427c4 4334 for (l = labels; l != NULL; l = l->next)
404a8071 4335 {
9c2799c2 4336 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4337 symbol_set_frag (l->label, frag_now);
4338 val = (valueT) frag_now_fix ();
df58fc94 4339 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4340 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4341 ++val;
4342 S_SET_VALUE (l->label, val);
4343 }
4344}
4345
462427c4
RS
4346/* Move all labels in insn_labels to the current insertion point
4347 and treat them as text labels. */
4348
4349static void
4350mips_move_text_labels (void)
4351{
4352 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4353}
4354
9e009953
MR
4355/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4356
5f0fe04b
TS
4357static bfd_boolean
4358s_is_linkonce (symbolS *sym, segT from_seg)
4359{
4360 bfd_boolean linkonce = FALSE;
4361 segT symseg = S_GET_SEGMENT (sym);
4362
4363 if (symseg != from_seg && !S_IS_LOCAL (sym))
4364 {
4365 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4366 linkonce = TRUE;
5f0fe04b
TS
4367 /* The GNU toolchain uses an extension for ELF: a section
4368 beginning with the magic string .gnu.linkonce is a
4369 linkonce section. */
4370 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4371 sizeof ".gnu.linkonce" - 1) == 0)
4372 linkonce = TRUE;
5f0fe04b
TS
4373 }
4374 return linkonce;
4375}
4376
e1b47bd5 4377/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4378 linker to handle them specially, such as generating jalx instructions
4379 when needed. We also make them odd for the duration of the assembly,
4380 in order to generate the right sort of code. We will make them even
252b5132
RH
4381 in the adjust_symtab routine, while leaving them marked. This is
4382 convenient for the debugger and the disassembler. The linker knows
4383 to make them odd again. */
4384
4385static void
e1b47bd5 4386mips_compressed_mark_label (symbolS *label)
252b5132 4387{
df58fc94 4388 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4389
f3ded42a
RS
4390 if (mips_opts.mips16)
4391 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4392 else
4393 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4394 if ((S_GET_VALUE (label) & 1) == 0
4395 /* Don't adjust the address if the label is global or weak, or
4396 in a link-once section, since we'll be emitting symbol reloc
4397 references to it which will be patched up by the linker, and
4398 the final value of the symbol may or may not be MIPS16/microMIPS. */
4399 && !S_IS_WEAK (label)
4400 && !S_IS_EXTERNAL (label)
4401 && !s_is_linkonce (label, now_seg))
4402 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4403}
4404
4405/* Mark preceding MIPS16 or microMIPS instruction labels. */
4406
4407static void
4408mips_compressed_mark_labels (void)
4409{
4410 struct insn_label_list *l;
4411
4412 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4413 mips_compressed_mark_label (l->label);
252b5132
RH
4414}
4415
4d7206a2
RS
4416/* End the current frag. Make it a variant frag and record the
4417 relaxation info. */
4418
4419static void
4420relax_close_frag (void)
4421{
584892a6 4422 mips_macro_warning.first_frag = frag_now;
4d7206a2 4423 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4424 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4425 mips_pic != NO_PIC),
4d7206a2
RS
4426 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4427
4428 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4429 mips_relax.first_fixup = 0;
4430}
4431
4432/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4433 See the comment above RELAX_ENCODE for more details. */
4434
4435static void
4436relax_start (symbolS *symbol)
4437{
9c2799c2 4438 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4439 mips_relax.sequence = 1;
4440 mips_relax.symbol = symbol;
4441}
4442
4443/* Start generating the second version of a relaxable sequence.
4444 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4445
4446static void
4d7206a2
RS
4447relax_switch (void)
4448{
9c2799c2 4449 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4450 mips_relax.sequence = 2;
4451}
4452
4453/* End the current relaxable sequence. */
4454
4455static void
4456relax_end (void)
4457{
9c2799c2 4458 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4459 relax_close_frag ();
4460 mips_relax.sequence = 0;
4461}
4462
11625dd8
RS
4463/* Return true if IP is a delayed branch or jump. */
4464
4465static inline bfd_boolean
4466delayed_branch_p (const struct mips_cl_insn *ip)
4467{
4468 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4469 | INSN_COND_BRANCH_DELAY
4470 | INSN_COND_BRANCH_LIKELY)) != 0;
4471}
4472
4473/* Return true if IP is a compact branch or jump. */
4474
4475static inline bfd_boolean
4476compact_branch_p (const struct mips_cl_insn *ip)
4477{
26545944
RS
4478 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4479 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4480}
4481
4482/* Return true if IP is an unconditional branch or jump. */
4483
4484static inline bfd_boolean
4485uncond_branch_p (const struct mips_cl_insn *ip)
4486{
4487 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4488 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4489}
4490
4491/* Return true if IP is a branch-likely instruction. */
4492
4493static inline bfd_boolean
4494branch_likely_p (const struct mips_cl_insn *ip)
4495{
4496 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4497}
4498
14fe068b
RS
4499/* Return the type of nop that should be used to fill the delay slot
4500 of delayed branch IP. */
4501
4502static struct mips_cl_insn *
4503get_delay_slot_nop (const struct mips_cl_insn *ip)
4504{
4505 if (mips_opts.micromips
4506 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4507 return &micromips_nop32_insn;
4508 return NOP_INSN;
4509}
4510
fc76e730
RS
4511/* Return a mask that has bit N set if OPCODE reads the register(s)
4512 in operand N. */
df58fc94
RS
4513
4514static unsigned int
fc76e730 4515insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4516{
fc76e730
RS
4517 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4518}
df58fc94 4519
fc76e730
RS
4520/* Return a mask that has bit N set if OPCODE writes to the register(s)
4521 in operand N. */
4522
4523static unsigned int
4524insn_write_mask (const struct mips_opcode *opcode)
4525{
4526 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4527}
4528
4529/* Return a mask of the registers specified by operand OPERAND of INSN.
4530 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4531 is set. */
4532
4533static unsigned int
4534operand_reg_mask (const struct mips_cl_insn *insn,
4535 const struct mips_operand *operand,
4536 unsigned int type_mask)
4537{
4538 unsigned int uval, vsel;
4539
4540 switch (operand->type)
df58fc94 4541 {
fc76e730
RS
4542 case OP_INT:
4543 case OP_MAPPED_INT:
4544 case OP_MSB:
4545 case OP_PCREL:
4546 case OP_PERF_REG:
4547 case OP_ADDIUSP_INT:
4548 case OP_ENTRY_EXIT_LIST:
4549 case OP_REPEAT_DEST_REG:
4550 case OP_REPEAT_PREV_REG:
4551 case OP_PC:
14daeee3
RS
4552 case OP_VU0_SUFFIX:
4553 case OP_VU0_MATCH_SUFFIX:
56d438b1 4554 case OP_IMM_INDEX:
fc76e730
RS
4555 abort ();
4556
25499ac7
MR
4557 case OP_REG28:
4558 return 1 << 28;
4559
fc76e730 4560 case OP_REG:
0f35dbc4 4561 case OP_OPTIONAL_REG:
fc76e730
RS
4562 {
4563 const struct mips_reg_operand *reg_op;
4564
4565 reg_op = (const struct mips_reg_operand *) operand;
4566 if (!(type_mask & (1 << reg_op->reg_type)))
4567 return 0;
4568 uval = insn_extract_operand (insn, operand);
4569 return 1 << mips_decode_reg_operand (reg_op, uval);
4570 }
4571
4572 case OP_REG_PAIR:
4573 {
4574 const struct mips_reg_pair_operand *pair_op;
4575
4576 pair_op = (const struct mips_reg_pair_operand *) operand;
4577 if (!(type_mask & (1 << pair_op->reg_type)))
4578 return 0;
4579 uval = insn_extract_operand (insn, operand);
4580 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4581 }
4582
4583 case OP_CLO_CLZ_DEST:
4584 if (!(type_mask & (1 << OP_REG_GP)))
4585 return 0;
4586 uval = insn_extract_operand (insn, operand);
4587 return (1 << (uval & 31)) | (1 << (uval >> 5));
4588
7361da2c
AB
4589 case OP_SAME_RS_RT:
4590 if (!(type_mask & (1 << OP_REG_GP)))
4591 return 0;
4592 uval = insn_extract_operand (insn, operand);
4593 gas_assert ((uval & 31) == (uval >> 5));
4594 return 1 << (uval & 31);
4595
4596 case OP_CHECK_PREV:
4597 case OP_NON_ZERO_REG:
4598 if (!(type_mask & (1 << OP_REG_GP)))
4599 return 0;
4600 uval = insn_extract_operand (insn, operand);
4601 return 1 << (uval & 31);
4602
fc76e730
RS
4603 case OP_LWM_SWM_LIST:
4604 abort ();
4605
4606 case OP_SAVE_RESTORE_LIST:
4607 abort ();
4608
4609 case OP_MDMX_IMM_REG:
4610 if (!(type_mask & (1 << OP_REG_VEC)))
4611 return 0;
4612 uval = insn_extract_operand (insn, operand);
4613 vsel = uval >> 5;
4614 if ((vsel & 0x18) == 0x18)
4615 return 0;
4616 return 1 << (uval & 31);
56d438b1
CF
4617
4618 case OP_REG_INDEX:
4619 if (!(type_mask & (1 << OP_REG_GP)))
4620 return 0;
4621 return 1 << insn_extract_operand (insn, operand);
df58fc94 4622 }
fc76e730
RS
4623 abort ();
4624}
4625
4626/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4627 where bit N of OPNO_MASK is set if operand N should be included.
4628 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4629 is set. */
4630
4631static unsigned int
4632insn_reg_mask (const struct mips_cl_insn *insn,
4633 unsigned int type_mask, unsigned int opno_mask)
4634{
4635 unsigned int opno, reg_mask;
4636
4637 opno = 0;
4638 reg_mask = 0;
4639 while (opno_mask != 0)
4640 {
4641 if (opno_mask & 1)
4642 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4643 opno_mask >>= 1;
4644 opno += 1;
4645 }
4646 return reg_mask;
df58fc94
RS
4647}
4648
4c260379
RS
4649/* Return the mask of core registers that IP reads. */
4650
4651static unsigned int
4652gpr_read_mask (const struct mips_cl_insn *ip)
4653{
4654 unsigned long pinfo, pinfo2;
4655 unsigned int mask;
4656
fc76e730 4657 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4658 pinfo = ip->insn_mo->pinfo;
4659 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4660 if (pinfo & INSN_UDI)
4c260379 4661 {
fc76e730
RS
4662 /* UDI instructions have traditionally been assumed to read RS
4663 and RT. */
4664 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4665 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4666 }
fc76e730
RS
4667 if (pinfo & INSN_READ_GPR_24)
4668 mask |= 1 << 24;
4669 if (pinfo2 & INSN2_READ_GPR_16)
4670 mask |= 1 << 16;
4671 if (pinfo2 & INSN2_READ_SP)
4672 mask |= 1 << SP;
26545944 4673 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4674 mask |= 1 << 31;
fe35f09f
RS
4675 /* Don't include register 0. */
4676 return mask & ~1;
4c260379
RS
4677}
4678
4679/* Return the mask of core registers that IP writes. */
4680
4681static unsigned int
4682gpr_write_mask (const struct mips_cl_insn *ip)
4683{
4684 unsigned long pinfo, pinfo2;
4685 unsigned int mask;
4686
fc76e730 4687 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4688 pinfo = ip->insn_mo->pinfo;
4689 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4690 if (pinfo & INSN_WRITE_GPR_24)
4691 mask |= 1 << 24;
4692 if (pinfo & INSN_WRITE_GPR_31)
4693 mask |= 1 << 31;
4694 if (pinfo & INSN_UDI)
4695 /* UDI instructions have traditionally been assumed to write to RD. */
4696 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4697 if (pinfo2 & INSN2_WRITE_SP)
4698 mask |= 1 << SP;
fe35f09f
RS
4699 /* Don't include register 0. */
4700 return mask & ~1;
4c260379
RS
4701}
4702
4703/* Return the mask of floating-point registers that IP reads. */
4704
4705static unsigned int
4706fpr_read_mask (const struct mips_cl_insn *ip)
4707{
fc76e730 4708 unsigned long pinfo;
4c260379
RS
4709 unsigned int mask;
4710
9d5de888
CF
4711 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4712 | (1 << OP_REG_MSA)),
fc76e730 4713 insn_read_mask (ip->insn_mo));
4c260379 4714 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4715 /* Conservatively treat all operands to an FP_D instruction are doubles.
4716 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4717 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4718 mask |= mask << 1;
4719 return mask;
4720}
4721
4722/* Return the mask of floating-point registers that IP writes. */
4723
4724static unsigned int
4725fpr_write_mask (const struct mips_cl_insn *ip)
4726{
fc76e730 4727 unsigned long pinfo;
4c260379
RS
4728 unsigned int mask;
4729
9d5de888
CF
4730 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4731 | (1 << OP_REG_MSA)),
fc76e730 4732 insn_write_mask (ip->insn_mo));
4c260379 4733 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4734 /* Conservatively treat all operands to an FP_D instruction are doubles.
4735 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4736 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4737 mask |= mask << 1;
4738 return mask;
4739}
4740
a1d78564
RS
4741/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4742 Check whether that is allowed. */
4743
4744static bfd_boolean
4745mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4746{
4747 const char *s = insn->name;
351cdf24
MF
4748 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4749 || FPR_SIZE == 64)
4750 && mips_opts.oddspreg;
a1d78564
RS
4751
4752 if (insn->pinfo == INSN_MACRO)
4753 /* Let a macro pass, we'll catch it later when it is expanded. */
4754 return TRUE;
4755
351cdf24
MF
4756 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4757 otherwise it depends on oddspreg. */
4758 if ((insn->pinfo & FP_S)
4759 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4760 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4761 return FPR_SIZE == 32 || oddspreg;
a1d78564 4762
351cdf24
MF
4763 /* Allow odd registers for single-precision ops and double-precision if the
4764 floating-point registers are 64-bit wide. */
4765 switch (insn->pinfo & (FP_S | FP_D))
4766 {
4767 case FP_S:
4768 case 0:
4769 return oddspreg;
4770 case FP_D:
4771 return FPR_SIZE == 64;
4772 default:
4773 break;
a1d78564
RS
4774 }
4775
351cdf24
MF
4776 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4777 s = strchr (insn->name, '.');
4778 if (s != NULL && opnum == 2)
4779 s = strchr (s + 1, '.');
4780 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4781 return oddspreg;
a1d78564 4782
351cdf24 4783 return FPR_SIZE == 64;
a1d78564
RS
4784}
4785
a1d78564
RS
4786/* Information about an instruction argument that we're trying to match. */
4787struct mips_arg_info
4788{
4789 /* The instruction so far. */
4790 struct mips_cl_insn *insn;
4791
a92713e6
RS
4792 /* The first unconsumed operand token. */
4793 struct mips_operand_token *token;
4794
a1d78564
RS
4795 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4796 int opnum;
4797
4798 /* The 1-based argument number, for error reporting. This does not
4799 count elided optional registers, etc.. */
4800 int argnum;
4801
4802 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4803 unsigned int last_regno;
4804
4805 /* If the first operand was an OP_REG, this is the register that it
4806 specified, otherwise it is ILLEGAL_REG. */
4807 unsigned int dest_regno;
4808
4809 /* The value of the last OP_INT operand. Only used for OP_MSB,
4810 where it gives the lsb position. */
4811 unsigned int last_op_int;
4812
60f20e8b 4813 /* If true, match routines should assume that no later instruction
2b0f3761 4814 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4815 possible. Match routines should not report errors if something
4816 is only invalid for !LAX_MATCH. */
4817 bfd_boolean lax_match;
a1d78564 4818
a1d78564
RS
4819 /* True if a reference to the current AT register was seen. */
4820 bfd_boolean seen_at;
4821};
4822
1a00e612
RS
4823/* Record that the argument is out of range. */
4824
4825static void
4826match_out_of_range (struct mips_arg_info *arg)
4827{
4828 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4829}
4830
4831/* Record that the argument isn't constant but needs to be. */
4832
4833static void
4834match_not_constant (struct mips_arg_info *arg)
4835{
4836 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4837 arg->argnum);
4838}
4839
a92713e6
RS
4840/* Try to match an OT_CHAR token for character CH. Consume the token
4841 and return true on success, otherwise return false. */
a1d78564 4842
a92713e6
RS
4843static bfd_boolean
4844match_char (struct mips_arg_info *arg, char ch)
a1d78564 4845{
a92713e6
RS
4846 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4847 {
4848 ++arg->token;
4849 if (ch == ',')
4850 arg->argnum += 1;
4851 return TRUE;
4852 }
4853 return FALSE;
4854}
a1d78564 4855
a92713e6
RS
4856/* Try to get an expression from the next tokens in ARG. Consume the
4857 tokens and return true on success, storing the expression value in
4858 VALUE and relocation types in R. */
4859
4860static bfd_boolean
4861match_expression (struct mips_arg_info *arg, expressionS *value,
4862 bfd_reloc_code_real_type *r)
4863{
d436c1c2
RS
4864 /* If the next token is a '(' that was parsed as being part of a base
4865 expression, assume we have an elided offset. The later match will fail
4866 if this turns out to be wrong. */
4867 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4868 {
d436c1c2
RS
4869 value->X_op = O_constant;
4870 value->X_add_number = 0;
4871 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4872 return TRUE;
4873 }
4874
d436c1c2
RS
4875 /* Reject register-based expressions such as "0+$2" and "(($2))".
4876 For plain registers the default error seems more appropriate. */
4877 if (arg->token->type == OT_INTEGER
4878 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4879 {
d436c1c2
RS
4880 set_insn_error (arg->argnum, _("register value used as expression"));
4881 return FALSE;
a1d78564 4882 }
d436c1c2
RS
4883
4884 if (arg->token->type == OT_INTEGER)
a92713e6 4885 {
d436c1c2
RS
4886 *value = arg->token->u.integer.value;
4887 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4888 ++arg->token;
4889 return TRUE;
a92713e6 4890 }
a92713e6 4891
d436c1c2
RS
4892 set_insn_error_i
4893 (arg->argnum, _("operand %d must be an immediate expression"),
4894 arg->argnum);
4895 return FALSE;
a92713e6
RS
4896}
4897
4898/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4899 the tokens and return true on success, storing the constant value
a54d5f8b 4900 in *VALUE. */
a92713e6
RS
4901
4902static bfd_boolean
1a00e612 4903match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4904{
4905 expressionS ex;
4906 bfd_reloc_code_real_type r[3];
a1d78564 4907
a92713e6
RS
4908 if (!match_expression (arg, &ex, r))
4909 return FALSE;
4910
4911 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4912 *value = ex.X_add_number;
4913 else
4914 {
c96425c5
MR
4915 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4916 match_out_of_range (arg);
4917 else
4918 match_not_constant (arg);
1a00e612 4919 return FALSE;
a1d78564 4920 }
a92713e6 4921 return TRUE;
a1d78564
RS
4922}
4923
4924/* Return the RTYPE_* flags for a register operand of type TYPE that
4925 appears in instruction OPCODE. */
4926
4927static unsigned int
4928convert_reg_type (const struct mips_opcode *opcode,
4929 enum mips_reg_operand_type type)
4930{
4931 switch (type)
4932 {
4933 case OP_REG_GP:
4934 return RTYPE_NUM | RTYPE_GP;
4935
4936 case OP_REG_FP:
4937 /* Allow vector register names for MDMX if the instruction is a 64-bit
4938 FPR load, store or move (including moves to and from GPRs). */
4939 if ((mips_opts.ase & ASE_MDMX)
4940 && (opcode->pinfo & FP_D)
43885403 4941 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4942 | INSN_COPROC_MEMORY_DELAY
43885403 4943 | INSN_LOAD_COPROC
67dc82bc 4944 | INSN_LOAD_MEMORY
a1d78564
RS
4945 | INSN_STORE_MEMORY)))
4946 return RTYPE_FPU | RTYPE_VEC;
4947 return RTYPE_FPU;
4948
4949 case OP_REG_CCC:
4950 if (opcode->pinfo & (FP_D | FP_S))
4951 return RTYPE_CCC | RTYPE_FCC;
4952 return RTYPE_CCC;
4953
4954 case OP_REG_VEC:
4955 if (opcode->membership & INSN_5400)
4956 return RTYPE_FPU;
4957 return RTYPE_FPU | RTYPE_VEC;
4958
4959 case OP_REG_ACC:
4960 return RTYPE_ACC;
4961
4962 case OP_REG_COPRO:
4963 if (opcode->name[strlen (opcode->name) - 1] == '0')
4964 return RTYPE_NUM | RTYPE_CP0;
4965 return RTYPE_NUM;
4966
4967 case OP_REG_HW:
4968 return RTYPE_NUM;
14daeee3
RS
4969
4970 case OP_REG_VI:
4971 return RTYPE_NUM | RTYPE_VI;
4972
4973 case OP_REG_VF:
4974 return RTYPE_NUM | RTYPE_VF;
4975
4976 case OP_REG_R5900_I:
4977 return RTYPE_R5900_I;
4978
4979 case OP_REG_R5900_Q:
4980 return RTYPE_R5900_Q;
4981
4982 case OP_REG_R5900_R:
4983 return RTYPE_R5900_R;
4984
4985 case OP_REG_R5900_ACC:
4986 return RTYPE_R5900_ACC;
56d438b1
CF
4987
4988 case OP_REG_MSA:
4989 return RTYPE_MSA;
4990
4991 case OP_REG_MSA_CTRL:
4992 return RTYPE_NUM;
a1d78564
RS
4993 }
4994 abort ();
4995}
4996
4997/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4998
4999static void
5000check_regno (struct mips_arg_info *arg,
5001 enum mips_reg_operand_type type, unsigned int regno)
5002{
5003 if (AT && type == OP_REG_GP && regno == AT)
5004 arg->seen_at = TRUE;
5005
5006 if (type == OP_REG_FP
5007 && (regno & 1) != 0
a1d78564 5008 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5009 {
5010 /* This was a warning prior to introducing O32 FPXX and FP64 support
5011 so maintain a warning for FP32 but raise an error for the new
5012 cases. */
5013 if (FPR_SIZE == 32)
5014 as_warn (_("float register should be even, was %d"), regno);
5015 else
5016 as_bad (_("float register should be even, was %d"), regno);
5017 }
a1d78564
RS
5018
5019 if (type == OP_REG_CCC)
5020 {
5021 const char *name;
5022 size_t length;
5023
5024 name = arg->insn->insn_mo->name;
5025 length = strlen (name);
5026 if ((regno & 1) != 0
5027 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5028 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5029 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5030 name, regno);
5031
5032 if ((regno & 3) != 0
5033 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5034 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5035 name, regno);
5036 }
5037}
5038
a92713e6
RS
5039/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5040 a register of type TYPE. Return true on success, storing the register
5041 number in *REGNO and warning about any dubious uses. */
5042
5043static bfd_boolean
5044match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5045 unsigned int symval, unsigned int *regno)
5046{
5047 if (type == OP_REG_VEC)
5048 symval = mips_prefer_vec_regno (symval);
5049 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5050 return FALSE;
5051
5052 *regno = symval & RNUM_MASK;
5053 check_regno (arg, type, *regno);
5054 return TRUE;
5055}
5056
5057/* Try to interpret the next token in ARG as a register of type TYPE.
5058 Consume the token and return true on success, storing the register
5059 number in *REGNO. Return false on failure. */
5060
5061static bfd_boolean
5062match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5063 unsigned int *regno)
5064{
5065 if (arg->token->type == OT_REG
5066 && match_regno (arg, type, arg->token->u.regno, regno))
5067 {
5068 ++arg->token;
5069 return TRUE;
5070 }
5071 return FALSE;
5072}
5073
5074/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5075 Consume the token and return true on success, storing the register numbers
5076 in *REGNO1 and *REGNO2. Return false on failure. */
5077
5078static bfd_boolean
5079match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5080 unsigned int *regno1, unsigned int *regno2)
5081{
5082 if (match_reg (arg, type, regno1))
5083 {
5084 *regno2 = *regno1;
5085 return TRUE;
5086 }
5087 if (arg->token->type == OT_REG_RANGE
5088 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5089 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5090 && *regno1 <= *regno2)
5091 {
5092 ++arg->token;
5093 return TRUE;
5094 }
5095 return FALSE;
5096}
5097
a1d78564
RS
5098/* OP_INT matcher. */
5099
a92713e6 5100static bfd_boolean
a1d78564 5101match_int_operand (struct mips_arg_info *arg,
a92713e6 5102 const struct mips_operand *operand_base)
a1d78564
RS
5103{
5104 const struct mips_int_operand *operand;
3ccad066 5105 unsigned int uval;
a1d78564
RS
5106 int min_val, max_val, factor;
5107 offsetT sval;
a1d78564
RS
5108
5109 operand = (const struct mips_int_operand *) operand_base;
5110 factor = 1 << operand->shift;
3ccad066
RS
5111 min_val = mips_int_operand_min (operand);
5112 max_val = mips_int_operand_max (operand);
a1d78564 5113
d436c1c2
RS
5114 if (operand_base->lsb == 0
5115 && operand_base->size == 16
5116 && operand->shift == 0
5117 && operand->bias == 0
5118 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5119 {
5120 /* The operand can be relocated. */
a92713e6
RS
5121 if (!match_expression (arg, &offset_expr, offset_reloc))
5122 return FALSE;
5123
c96425c5
MR
5124 if (offset_expr.X_op == O_big)
5125 {
5126 match_out_of_range (arg);
5127 return FALSE;
5128 }
5129
a92713e6 5130 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5131 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5132 leave the relocation value in offset_expr and offset_relocs
5133 for the caller to process. */
a92713e6
RS
5134 return TRUE;
5135
5136 if (offset_expr.X_op != O_constant)
a1d78564 5137 {
60f20e8b
RS
5138 /* Accept non-constant operands if no later alternative matches,
5139 leaving it for the caller to process. */
5140 if (!arg->lax_match)
602b88e3
MR
5141 {
5142 match_not_constant (arg);
5143 return FALSE;
5144 }
a92713e6
RS
5145 offset_reloc[0] = BFD_RELOC_LO16;
5146 return TRUE;
a1d78564 5147 }
a92713e6 5148
a1d78564
RS
5149 /* Clear the global state; we're going to install the operand
5150 ourselves. */
a92713e6 5151 sval = offset_expr.X_add_number;
a1d78564 5152 offset_expr.X_op = O_absent;
60f20e8b
RS
5153
5154 /* For compatibility with older assemblers, we accept
5155 0x8000-0xffff as signed 16-bit numbers when only
5156 signed numbers are allowed. */
5157 if (sval > max_val)
5158 {
5159 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5160 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5161 {
5162 match_out_of_range (arg);
5163 return FALSE;
5164 }
60f20e8b 5165 }
a1d78564
RS
5166 }
5167 else
5168 {
1a00e612 5169 if (!match_const_int (arg, &sval))
a92713e6 5170 return FALSE;
a1d78564
RS
5171 }
5172
5173 arg->last_op_int = sval;
5174
1a00e612 5175 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5176 {
1a00e612
RS
5177 match_out_of_range (arg);
5178 return FALSE;
a1d78564
RS
5179 }
5180
5181 uval = (unsigned int) sval >> operand->shift;
5182 uval -= operand->bias;
5183
5184 /* Handle -mfix-cn63xxp1. */
5185 if (arg->opnum == 1
5186 && mips_fix_cn63xxp1
5187 && !mips_opts.micromips
5188 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5189 switch (uval)
5190 {
5191 case 5:
5192 case 25:
5193 case 26:
5194 case 27:
5195 case 28:
5196 case 29:
5197 case 30:
5198 case 31:
5199 /* These are ok. */
5200 break;
5201
5202 default:
5203 /* The rest must be changed to 28. */
5204 uval = 28;
5205 break;
5206 }
5207
5208 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5209 return TRUE;
a1d78564
RS
5210}
5211
5212/* OP_MAPPED_INT matcher. */
5213
a92713e6 5214static bfd_boolean
a1d78564 5215match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5216 const struct mips_operand *operand_base)
a1d78564
RS
5217{
5218 const struct mips_mapped_int_operand *operand;
5219 unsigned int uval, num_vals;
5220 offsetT sval;
5221
5222 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5223 if (!match_const_int (arg, &sval))
a92713e6 5224 return FALSE;
a1d78564
RS
5225
5226 num_vals = 1 << operand_base->size;
5227 for (uval = 0; uval < num_vals; uval++)
5228 if (operand->int_map[uval] == sval)
5229 break;
5230 if (uval == num_vals)
1a00e612
RS
5231 {
5232 match_out_of_range (arg);
5233 return FALSE;
5234 }
a1d78564
RS
5235
5236 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5237 return TRUE;
a1d78564
RS
5238}
5239
5240/* OP_MSB matcher. */
5241
a92713e6 5242static bfd_boolean
a1d78564 5243match_msb_operand (struct mips_arg_info *arg,
a92713e6 5244 const struct mips_operand *operand_base)
a1d78564
RS
5245{
5246 const struct mips_msb_operand *operand;
5247 int min_val, max_val, max_high;
5248 offsetT size, sval, high;
5249
5250 operand = (const struct mips_msb_operand *) operand_base;
5251 min_val = operand->bias;
5252 max_val = min_val + (1 << operand_base->size) - 1;
5253 max_high = operand->opsize;
5254
1a00e612 5255 if (!match_const_int (arg, &size))
a92713e6 5256 return FALSE;
a1d78564
RS
5257
5258 high = size + arg->last_op_int;
5259 sval = operand->add_lsb ? high : size;
5260
5261 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5262 {
1a00e612
RS
5263 match_out_of_range (arg);
5264 return FALSE;
a1d78564
RS
5265 }
5266 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5267 return TRUE;
a1d78564
RS
5268}
5269
5270/* OP_REG matcher. */
5271
a92713e6 5272static bfd_boolean
a1d78564 5273match_reg_operand (struct mips_arg_info *arg,
a92713e6 5274 const struct mips_operand *operand_base)
a1d78564
RS
5275{
5276 const struct mips_reg_operand *operand;
a92713e6 5277 unsigned int regno, uval, num_vals;
a1d78564
RS
5278
5279 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5280 if (!match_reg (arg, operand->reg_type, &regno))
5281 return FALSE;
a1d78564
RS
5282
5283 if (operand->reg_map)
5284 {
5285 num_vals = 1 << operand->root.size;
5286 for (uval = 0; uval < num_vals; uval++)
5287 if (operand->reg_map[uval] == regno)
5288 break;
5289 if (num_vals == uval)
a92713e6 5290 return FALSE;
a1d78564
RS
5291 }
5292 else
5293 uval = regno;
5294
a1d78564
RS
5295 arg->last_regno = regno;
5296 if (arg->opnum == 1)
5297 arg->dest_regno = regno;
5298 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5299 return TRUE;
a1d78564
RS
5300}
5301
5302/* OP_REG_PAIR matcher. */
5303
a92713e6 5304static bfd_boolean
a1d78564 5305match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5306 const struct mips_operand *operand_base)
a1d78564
RS
5307{
5308 const struct mips_reg_pair_operand *operand;
a92713e6 5309 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5310
5311 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5312 if (!match_reg (arg, operand->reg_type, &regno1)
5313 || !match_char (arg, ',')
5314 || !match_reg (arg, operand->reg_type, &regno2))
5315 return FALSE;
a1d78564
RS
5316
5317 num_vals = 1 << operand_base->size;
5318 for (uval = 0; uval < num_vals; uval++)
5319 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5320 break;
5321 if (uval == num_vals)
a92713e6 5322 return FALSE;
a1d78564 5323
a1d78564 5324 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5325 return TRUE;
a1d78564
RS
5326}
5327
5328/* OP_PCREL matcher. The caller chooses the relocation type. */
5329
a92713e6
RS
5330static bfd_boolean
5331match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5332{
a92713e6
RS
5333 bfd_reloc_code_real_type r[3];
5334
5335 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5336}
5337
5338/* OP_PERF_REG matcher. */
5339
a92713e6 5340static bfd_boolean
a1d78564 5341match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5342 const struct mips_operand *operand)
a1d78564
RS
5343{
5344 offsetT sval;
5345
1a00e612 5346 if (!match_const_int (arg, &sval))
a92713e6 5347 return FALSE;
a1d78564
RS
5348
5349 if (sval != 0
5350 && (sval != 1
5351 || (mips_opts.arch == CPU_R5900
5352 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5353 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5354 {
1a00e612
RS
5355 set_insn_error (arg->argnum, _("invalid performance register"));
5356 return FALSE;
a1d78564
RS
5357 }
5358
5359 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5360 return TRUE;
a1d78564
RS
5361}
5362
5363/* OP_ADDIUSP matcher. */
5364
a92713e6 5365static bfd_boolean
a1d78564 5366match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5367 const struct mips_operand *operand)
a1d78564
RS
5368{
5369 offsetT sval;
5370 unsigned int uval;
5371
1a00e612 5372 if (!match_const_int (arg, &sval))
a92713e6 5373 return FALSE;
a1d78564
RS
5374
5375 if (sval % 4)
1a00e612
RS
5376 {
5377 match_out_of_range (arg);
5378 return FALSE;
5379 }
a1d78564
RS
5380
5381 sval /= 4;
5382 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5383 {
5384 match_out_of_range (arg);
5385 return FALSE;
5386 }
a1d78564
RS
5387
5388 uval = (unsigned int) sval;
5389 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5390 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5391 return TRUE;
a1d78564
RS
5392}
5393
5394/* OP_CLO_CLZ_DEST matcher. */
5395
a92713e6 5396static bfd_boolean
a1d78564 5397match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5398 const struct mips_operand *operand)
a1d78564
RS
5399{
5400 unsigned int regno;
5401
a92713e6
RS
5402 if (!match_reg (arg, OP_REG_GP, &regno))
5403 return FALSE;
a1d78564 5404
a1d78564 5405 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5406 return TRUE;
a1d78564
RS
5407}
5408
7361da2c
AB
5409/* OP_CHECK_PREV matcher. */
5410
5411static bfd_boolean
5412match_check_prev_operand (struct mips_arg_info *arg,
5413 const struct mips_operand *operand_base)
5414{
5415 const struct mips_check_prev_operand *operand;
5416 unsigned int regno;
5417
5418 operand = (const struct mips_check_prev_operand *) operand_base;
5419
5420 if (!match_reg (arg, OP_REG_GP, &regno))
5421 return FALSE;
5422
5423 if (!operand->zero_ok && regno == 0)
5424 return FALSE;
5425
5426 if ((operand->less_than_ok && regno < arg->last_regno)
5427 || (operand->greater_than_ok && regno > arg->last_regno)
5428 || (operand->equal_ok && regno == arg->last_regno))
5429 {
5430 arg->last_regno = regno;
5431 insn_insert_operand (arg->insn, operand_base, regno);
5432 return TRUE;
5433 }
5434
5435 return FALSE;
5436}
5437
5438/* OP_SAME_RS_RT matcher. */
5439
5440static bfd_boolean
5441match_same_rs_rt_operand (struct mips_arg_info *arg,
5442 const struct mips_operand *operand)
5443{
5444 unsigned int regno;
5445
5446 if (!match_reg (arg, OP_REG_GP, &regno))
5447 return FALSE;
5448
5449 if (regno == 0)
5450 {
5451 set_insn_error (arg->argnum, _("the source register must not be $0"));
5452 return FALSE;
5453 }
5454
5455 arg->last_regno = regno;
5456
5457 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5458 return TRUE;
5459}
5460
a1d78564
RS
5461/* OP_LWM_SWM_LIST matcher. */
5462
a92713e6 5463static bfd_boolean
a1d78564 5464match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5465 const struct mips_operand *operand)
a1d78564 5466{
a92713e6
RS
5467 unsigned int reglist, sregs, ra, regno1, regno2;
5468 struct mips_arg_info reset;
a1d78564 5469
a92713e6
RS
5470 reglist = 0;
5471 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5472 return FALSE;
5473 do
5474 {
5475 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5476 {
5477 reglist |= 1 << FP;
5478 regno2 = S7;
5479 }
5480 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5481 reset = *arg;
5482 }
5483 while (match_char (arg, ',')
5484 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5485 *arg = reset;
a1d78564
RS
5486
5487 if (operand->size == 2)
5488 {
5489 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5490
5491 s0, ra
5492 s0, s1, ra, s2, s3
5493 s0-s2, ra
5494
5495 and any permutations of these. */
5496 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5497 return FALSE;
a1d78564
RS
5498
5499 sregs = (reglist >> 17) & 7;
5500 ra = 0;
5501 }
5502 else
5503 {
5504 /* The list must include at least one of ra and s0-sN,
5505 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5506 which are $23 and $30 respectively.) E.g.:
5507
5508 ra
5509 s0
5510 ra, s0, s1, s2
5511 s0-s8
5512 s0-s5, ra
5513
5514 and any permutations of these. */
5515 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5516 return FALSE;
a1d78564
RS
5517
5518 ra = (reglist >> 27) & 0x10;
5519 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5520 }
5521 sregs += 1;
5522 if ((sregs & -sregs) != sregs)
a92713e6 5523 return FALSE;
a1d78564
RS
5524
5525 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5526 return TRUE;
a1d78564
RS
5527}
5528
364215c8
RS
5529/* OP_ENTRY_EXIT_LIST matcher. */
5530
a92713e6 5531static unsigned int
364215c8 5532match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5533 const struct mips_operand *operand)
364215c8
RS
5534{
5535 unsigned int mask;
5536 bfd_boolean is_exit;
5537
5538 /* The format is the same for both ENTRY and EXIT, but the constraints
5539 are different. */
5540 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5541 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5542 do
364215c8
RS
5543 {
5544 unsigned int regno1, regno2;
5545 bfd_boolean is_freg;
5546
a92713e6 5547 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5548 is_freg = FALSE;
a92713e6 5549 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5550 is_freg = TRUE;
5551 else
a92713e6 5552 return FALSE;
364215c8
RS
5553
5554 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5555 {
5556 mask &= ~(7 << 3);
5557 mask |= (5 + regno2) << 3;
5558 }
5559 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5560 mask |= (regno2 - 3) << 3;
5561 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5562 mask |= (regno2 - 15) << 1;
5563 else if (regno1 == RA && regno2 == RA)
5564 mask |= 1;
5565 else
a92713e6 5566 return FALSE;
364215c8 5567 }
a92713e6
RS
5568 while (match_char (arg, ','));
5569
364215c8 5570 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5571 return TRUE;
364215c8
RS
5572}
5573
38bf472a
MR
5574/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5575 the argument register mask AMASK, the number of static registers
5576 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5577 respectively, and the frame size FRAME_SIZE. */
5578
5579static unsigned int
5580mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5581 unsigned int ra, unsigned int s0, unsigned int s1,
5582 unsigned int frame_size)
5583{
5584 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5585 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5586}
5587
5588/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5589 argument register mask AMASK, the number of static registers saved
5590 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5591 respectively, and the frame size FRAME_SIZE. */
5592
5593static unsigned int
5594mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5595 unsigned int ra, unsigned int s0, unsigned int s1,
5596 unsigned int frame_size)
5597{
5598 unsigned int args;
5599
5600 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5601 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5602 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5603 | ((frame_size & 0xf0) << 16));
5604 return args;
5605}
5606
364215c8
RS
5607/* OP_SAVE_RESTORE_LIST matcher. */
5608
a92713e6
RS
5609static bfd_boolean
5610match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5611{
5612 unsigned int opcode, args, statics, sregs;
5613 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5614 unsigned int arg_mask, ra, s0, s1;
364215c8 5615 offsetT frame_size;
364215c8 5616
364215c8
RS
5617 opcode = arg->insn->insn_opcode;
5618 frame_size = 0;
5619 num_frame_sizes = 0;
5620 args = 0;
5621 statics = 0;
5622 sregs = 0;
38bf472a
MR
5623 ra = 0;
5624 s0 = 0;
5625 s1 = 0;
a92713e6 5626 do
364215c8
RS
5627 {
5628 unsigned int regno1, regno2;
5629
a92713e6 5630 if (arg->token->type == OT_INTEGER)
364215c8
RS
5631 {
5632 /* Handle the frame size. */
1a00e612 5633 if (!match_const_int (arg, &frame_size))
a92713e6 5634 return FALSE;
364215c8 5635 num_frame_sizes += 1;
364215c8
RS
5636 }
5637 else
5638 {
a92713e6
RS
5639 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5640 return FALSE;
364215c8
RS
5641
5642 while (regno1 <= regno2)
5643 {
5644 if (regno1 >= 4 && regno1 <= 7)
5645 {
5646 if (num_frame_sizes == 0)
5647 /* args $a0-$a3 */
5648 args |= 1 << (regno1 - 4);
5649 else
5650 /* statics $a0-$a3 */
5651 statics |= 1 << (regno1 - 4);
5652 }
5653 else if (regno1 >= 16 && regno1 <= 23)
5654 /* $s0-$s7 */
5655 sregs |= 1 << (regno1 - 16);
5656 else if (regno1 == 30)
5657 /* $s8 */
5658 sregs |= 1 << 8;
5659 else if (regno1 == 31)
5660 /* Add $ra to insn. */
38bf472a 5661 ra = 1;
364215c8 5662 else
a92713e6 5663 return FALSE;
364215c8
RS
5664 regno1 += 1;
5665 if (regno1 == 24)
5666 regno1 = 30;
5667 }
5668 }
364215c8 5669 }
a92713e6 5670 while (match_char (arg, ','));
364215c8
RS
5671
5672 /* Encode args/statics combination. */
5673 if (args & statics)
a92713e6 5674 return FALSE;
364215c8
RS
5675 else if (args == 0xf)
5676 /* All $a0-$a3 are args. */
38bf472a 5677 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5678 else if (statics == 0xf)
5679 /* All $a0-$a3 are statics. */
38bf472a 5680 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5681 else
5682 {
5683 /* Count arg registers. */
5684 num_args = 0;
5685 while (args & 0x1)
5686 {
5687 args >>= 1;
5688 num_args += 1;
5689 }
5690 if (args != 0)
a92713e6 5691 return FALSE;
364215c8
RS
5692
5693 /* Count static registers. */
5694 num_statics = 0;
5695 while (statics & 0x8)
5696 {
5697 statics = (statics << 1) & 0xf;
5698 num_statics += 1;
5699 }
5700 if (statics != 0)
a92713e6 5701 return FALSE;
364215c8
RS
5702
5703 /* Encode args/statics. */
38bf472a 5704 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5705 }
5706
5707 /* Encode $s0/$s1. */
5708 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5709 s0 = 1;
364215c8 5710 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5711 s1 = 1;
364215c8
RS
5712 sregs >>= 2;
5713
5714 /* Encode $s2-$s8. */
5715 num_sregs = 0;
5716 while (sregs & 1)
5717 {
5718 sregs >>= 1;
5719 num_sregs += 1;
5720 }
5721 if (sregs != 0)
a92713e6 5722 return FALSE;
364215c8
RS
5723
5724 /* Encode frame size. */
5725 if (num_frame_sizes == 0)
1a00e612
RS
5726 {
5727 set_insn_error (arg->argnum, _("missing frame size"));
5728 return FALSE;
5729 }
5730 if (num_frame_sizes > 1)
5731 {
5732 set_insn_error (arg->argnum, _("frame size specified twice"));
5733 return FALSE;
5734 }
5735 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5736 {
5737 set_insn_error (arg->argnum, _("invalid frame size"));
5738 return FALSE;
5739 }
38bf472a 5740 frame_size /= 8;
364215c8 5741
364215c8 5742 /* Finally build the instruction. */
38bf472a
MR
5743 if (mips_opts.mips16)
5744 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5745 frame_size);
5746 else if (!mips_opts.micromips)
5747 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5748 frame_size);
5749 else
5750 abort ();
5751
364215c8 5752 arg->insn->insn_opcode = opcode;
a92713e6 5753 return TRUE;
364215c8
RS
5754}
5755
a1d78564
RS
5756/* OP_MDMX_IMM_REG matcher. */
5757
a92713e6 5758static bfd_boolean
a1d78564 5759match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5760 const struct mips_operand *operand)
a1d78564 5761{
a92713e6 5762 unsigned int regno, uval;
a1d78564
RS
5763 bfd_boolean is_qh;
5764 const struct mips_opcode *opcode;
5765
5766 /* The mips_opcode records whether this is an octobyte or quadhalf
5767 instruction. Start out with that bit in place. */
5768 opcode = arg->insn->insn_mo;
5769 uval = mips_extract_operand (operand, opcode->match);
5770 is_qh = (uval != 0);
5771
56d438b1 5772 if (arg->token->type == OT_REG)
a1d78564
RS
5773 {
5774 if ((opcode->membership & INSN_5400)
5775 && strcmp (opcode->name, "rzu.ob") == 0)
5776 {
1a00e612
RS
5777 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5778 arg->argnum);
5779 return FALSE;
a1d78564
RS
5780 }
5781
56d438b1
CF
5782 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5783 return FALSE;
5784 ++arg->token;
5785
a1d78564
RS
5786 /* Check whether this is a vector register or a broadcast of
5787 a single element. */
56d438b1 5788 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5789 {
56d438b1 5790 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5791 {
1a00e612
RS
5792 set_insn_error (arg->argnum, _("invalid element selector"));
5793 return FALSE;
a1d78564 5794 }
56d438b1
CF
5795 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5796 ++arg->token;
a1d78564
RS
5797 }
5798 else
5799 {
5800 /* A full vector. */
5801 if ((opcode->membership & INSN_5400)
5802 && (strcmp (opcode->name, "sll.ob") == 0
5803 || strcmp (opcode->name, "srl.ob") == 0))
5804 {
1a00e612
RS
5805 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5806 arg->argnum);
5807 return FALSE;
a1d78564
RS
5808 }
5809
5810 if (is_qh)
5811 uval |= MDMX_FMTSEL_VEC_QH << 5;
5812 else
5813 uval |= MDMX_FMTSEL_VEC_OB << 5;
5814 }
a1d78564
RS
5815 uval |= regno;
5816 }
5817 else
5818 {
5819 offsetT sval;
5820
1a00e612 5821 if (!match_const_int (arg, &sval))
a92713e6 5822 return FALSE;
a1d78564
RS
5823 if (sval < 0 || sval > 31)
5824 {
1a00e612
RS
5825 match_out_of_range (arg);
5826 return FALSE;
a1d78564
RS
5827 }
5828 uval |= (sval & 31);
5829 if (is_qh)
5830 uval |= MDMX_FMTSEL_IMM_QH << 5;
5831 else
5832 uval |= MDMX_FMTSEL_IMM_OB << 5;
5833 }
5834 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5835 return TRUE;
a1d78564
RS
5836}
5837
56d438b1
CF
5838/* OP_IMM_INDEX matcher. */
5839
5840static bfd_boolean
5841match_imm_index_operand (struct mips_arg_info *arg,
5842 const struct mips_operand *operand)
5843{
5844 unsigned int max_val;
5845
5846 if (arg->token->type != OT_INTEGER_INDEX)
5847 return FALSE;
5848
5849 max_val = (1 << operand->size) - 1;
5850 if (arg->token->u.index > max_val)
5851 {
5852 match_out_of_range (arg);
5853 return FALSE;
5854 }
5855 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5856 ++arg->token;
5857 return TRUE;
5858}
5859
5860/* OP_REG_INDEX matcher. */
5861
5862static bfd_boolean
5863match_reg_index_operand (struct mips_arg_info *arg,
5864 const struct mips_operand *operand)
5865{
5866 unsigned int regno;
5867
5868 if (arg->token->type != OT_REG_INDEX)
5869 return FALSE;
5870
5871 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5872 return FALSE;
5873
5874 insn_insert_operand (arg->insn, operand, regno);
5875 ++arg->token;
5876 return TRUE;
5877}
5878
a1d78564
RS
5879/* OP_PC matcher. */
5880
a92713e6
RS
5881static bfd_boolean
5882match_pc_operand (struct mips_arg_info *arg)
a1d78564 5883{
a92713e6
RS
5884 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5885 {
5886 ++arg->token;
5887 return TRUE;
5888 }
5889 return FALSE;
a1d78564
RS
5890}
5891
25499ac7
MR
5892/* OP_REG28 matcher. */
5893
5894static bfd_boolean
5895match_reg28_operand (struct mips_arg_info *arg)
5896{
5897 unsigned int regno;
5898
5899 if (arg->token->type == OT_REG
5900 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5901 && regno == GP)
5902 {
5903 ++arg->token;
5904 return TRUE;
5905 }
5906 return FALSE;
5907}
5908
7361da2c
AB
5909/* OP_NON_ZERO_REG matcher. */
5910
5911static bfd_boolean
5912match_non_zero_reg_operand (struct mips_arg_info *arg,
5913 const struct mips_operand *operand)
5914{
5915 unsigned int regno;
5916
5917 if (!match_reg (arg, OP_REG_GP, &regno))
5918 return FALSE;
5919
5920 if (regno == 0)
5921 return FALSE;
5922
5923 arg->last_regno = regno;
5924 insn_insert_operand (arg->insn, operand, regno);
5925 return TRUE;
5926}
5927
a1d78564
RS
5928/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5929 register that we need to match. */
5930
a92713e6
RS
5931static bfd_boolean
5932match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5933{
5934 unsigned int regno;
5935
a92713e6 5936 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5937}
5938
33f46696
MR
5939/* Try to match a floating-point constant from ARG for LI.S or LI.D.
5940 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5941 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
5942
5943 Return the constant in IMM and OFFSET as follows:
5944
5945 - If the constant should be loaded via memory, set IMM to O_absent and
5946 OFFSET to the memory address.
5947
5948 - Otherwise, if the constant should be loaded into two 32-bit registers,
5949 set IMM to the O_constant to load into the high register and OFFSET
5950 to the corresponding value for the low register.
5951
5952 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5953
5954 These constants only appear as the last operand in an instruction,
5955 and every instruction that accepts them in any variant accepts them
5956 in all variants. This means we don't have to worry about backing out
5957 any changes if the instruction does not match. We just match
5958 unconditionally and report an error if the constant is invalid. */
5959
a92713e6
RS
5960static bfd_boolean
5961match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5962 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5963{
a92713e6 5964 char *p;
89565f1b
RS
5965 segT seg, new_seg;
5966 subsegT subseg;
5967 const char *newname;
a92713e6 5968 unsigned char *data;
89565f1b
RS
5969
5970 /* Where the constant is placed is based on how the MIPS assembler
5971 does things:
5972
5973 length == 4 && using_gprs -- immediate value only
5974 length == 8 && using_gprs -- .rdata or immediate value
5975 length == 4 && !using_gprs -- .lit4 or immediate value
5976 length == 8 && !using_gprs -- .lit8 or immediate value
5977
5978 The .lit4 and .lit8 sections are only used if permitted by the
5979 -G argument. */
a92713e6 5980 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5981 {
5982 set_insn_error (arg->argnum, _("floating-point expression required"));
5983 return FALSE;
5984 }
a92713e6
RS
5985
5986 gas_assert (arg->token->u.flt.length == length);
5987 data = arg->token->u.flt.data;
5988 ++arg->token;
89565f1b
RS
5989
5990 /* Handle 32-bit constants for which an immediate value is best. */
5991 if (length == 4
5992 && (using_gprs
5993 || g_switch_value < 4
5994 || (data[0] == 0 && data[1] == 0)
5995 || (data[2] == 0 && data[3] == 0)))
5996 {
5997 imm->X_op = O_constant;
5998 if (!target_big_endian)
5999 imm->X_add_number = bfd_getl32 (data);
6000 else
6001 imm->X_add_number = bfd_getb32 (data);
6002 offset->X_op = O_absent;
a92713e6 6003 return TRUE;
89565f1b
RS
6004 }
6005
6006 /* Handle 64-bit constants for which an immediate value is best. */
6007 if (length == 8
6008 && !mips_disable_float_construction
351cdf24
MF
6009 /* Constants can only be constructed in GPRs and copied to FPRs if the
6010 GPRs are at least as wide as the FPRs or MTHC1 is available.
6011 Unlike most tests for 32-bit floating-point registers this check
6012 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6013 permit 64-bit moves without MXHC1.
6014 Force the constant into memory otherwise. */
6015 && (using_gprs
6016 || GPR_SIZE == 64
6017 || ISA_HAS_MXHC1 (mips_opts.isa)
6018 || FPR_SIZE == 32)
89565f1b
RS
6019 && ((data[0] == 0 && data[1] == 0)
6020 || (data[2] == 0 && data[3] == 0))
6021 && ((data[4] == 0 && data[5] == 0)
6022 || (data[6] == 0 && data[7] == 0)))
6023 {
6024 /* The value is simple enough to load with a couple of instructions.
6025 If using 32-bit registers, set IMM to the high order 32 bits and
6026 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6027 64 bit constant. */
351cdf24 6028 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6029 {
6030 imm->X_op = O_constant;
6031 offset->X_op = O_constant;
6032 if (!target_big_endian)
6033 {
6034 imm->X_add_number = bfd_getl32 (data + 4);
6035 offset->X_add_number = bfd_getl32 (data);
6036 }
6037 else
6038 {
6039 imm->X_add_number = bfd_getb32 (data);
6040 offset->X_add_number = bfd_getb32 (data + 4);
6041 }
6042 if (offset->X_add_number == 0)
6043 offset->X_op = O_absent;
6044 }
6045 else
6046 {
6047 imm->X_op = O_constant;
6048 if (!target_big_endian)
6049 imm->X_add_number = bfd_getl64 (data);
6050 else
6051 imm->X_add_number = bfd_getb64 (data);
6052 offset->X_op = O_absent;
6053 }
a92713e6 6054 return TRUE;
89565f1b
RS
6055 }
6056
6057 /* Switch to the right section. */
6058 seg = now_seg;
6059 subseg = now_subseg;
6060 if (length == 4)
6061 {
6062 gas_assert (!using_gprs && g_switch_value >= 4);
6063 newname = ".lit4";
6064 }
6065 else
6066 {
6067 if (using_gprs || g_switch_value < 8)
6068 newname = RDATA_SECTION_NAME;
6069 else
6070 newname = ".lit8";
6071 }
6072
6073 new_seg = subseg_new (newname, (subsegT) 0);
6074 bfd_set_section_flags (stdoutput, new_seg,
6075 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6076 frag_align (length == 4 ? 2 : 3, 0, 0);
6077 if (strncmp (TARGET_OS, "elf", 3) != 0)
6078 record_alignment (new_seg, 4);
6079 else
6080 record_alignment (new_seg, length == 4 ? 2 : 3);
6081 if (seg == now_seg)
1661c76c 6082 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6083
6084 /* Set the argument to the current address in the section. */
6085 imm->X_op = O_absent;
6086 offset->X_op = O_symbol;
6087 offset->X_add_symbol = symbol_temp_new_now ();
6088 offset->X_add_number = 0;
6089
6090 /* Put the floating point number into the section. */
6091 p = frag_more (length);
6092 memcpy (p, data, length);
6093
6094 /* Switch back to the original section. */
6095 subseg_set (seg, subseg);
a92713e6 6096 return TRUE;
89565f1b
RS
6097}
6098
14daeee3
RS
6099/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6100 them. */
6101
6102static bfd_boolean
6103match_vu0_suffix_operand (struct mips_arg_info *arg,
6104 const struct mips_operand *operand,
6105 bfd_boolean match_p)
6106{
6107 unsigned int uval;
6108
6109 /* The operand can be an XYZW mask or a single 2-bit channel index
6110 (with X being 0). */
6111 gas_assert (operand->size == 2 || operand->size == 4);
6112
ee5734f0 6113 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6114 if (arg->token->type != OT_CHANNELS)
ee5734f0 6115 return match_p;
14daeee3
RS
6116
6117 uval = arg->token->u.channels;
6118 if (operand->size == 2)
6119 {
6120 /* Check that a single bit is set and convert it into a 2-bit index. */
6121 if ((uval & -uval) != uval)
6122 return FALSE;
6123 uval = 4 - ffs (uval);
6124 }
6125
6126 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6127 return FALSE;
6128
6129 ++arg->token;
6130 if (!match_p)
6131 insn_insert_operand (arg->insn, operand, uval);
6132 return TRUE;
6133}
6134
33f46696
MR
6135/* Try to match a token from ARG against OPERAND. Consume the token
6136 and return true on success, otherwise return false. */
a1d78564 6137
a92713e6 6138static bfd_boolean
a1d78564 6139match_operand (struct mips_arg_info *arg,
a92713e6 6140 const struct mips_operand *operand)
a1d78564
RS
6141{
6142 switch (operand->type)
6143 {
6144 case OP_INT:
a92713e6 6145 return match_int_operand (arg, operand);
a1d78564
RS
6146
6147 case OP_MAPPED_INT:
a92713e6 6148 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6149
6150 case OP_MSB:
a92713e6 6151 return match_msb_operand (arg, operand);
a1d78564
RS
6152
6153 case OP_REG:
0f35dbc4 6154 case OP_OPTIONAL_REG:
a92713e6 6155 return match_reg_operand (arg, operand);
a1d78564
RS
6156
6157 case OP_REG_PAIR:
a92713e6 6158 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6159
6160 case OP_PCREL:
a92713e6 6161 return match_pcrel_operand (arg);
a1d78564
RS
6162
6163 case OP_PERF_REG:
a92713e6 6164 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6165
6166 case OP_ADDIUSP_INT:
a92713e6 6167 return match_addiusp_operand (arg, operand);
a1d78564
RS
6168
6169 case OP_CLO_CLZ_DEST:
a92713e6 6170 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6171
6172 case OP_LWM_SWM_LIST:
a92713e6 6173 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6174
6175 case OP_ENTRY_EXIT_LIST:
a92713e6 6176 return match_entry_exit_operand (arg, operand);
364215c8 6177
a1d78564 6178 case OP_SAVE_RESTORE_LIST:
a92713e6 6179 return match_save_restore_list_operand (arg);
a1d78564
RS
6180
6181 case OP_MDMX_IMM_REG:
a92713e6 6182 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6183
6184 case OP_REPEAT_DEST_REG:
a92713e6 6185 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6186
6187 case OP_REPEAT_PREV_REG:
a92713e6 6188 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6189
6190 case OP_PC:
a92713e6 6191 return match_pc_operand (arg);
14daeee3 6192
25499ac7
MR
6193 case OP_REG28:
6194 return match_reg28_operand (arg);
6195
14daeee3
RS
6196 case OP_VU0_SUFFIX:
6197 return match_vu0_suffix_operand (arg, operand, FALSE);
6198
6199 case OP_VU0_MATCH_SUFFIX:
6200 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6201
6202 case OP_IMM_INDEX:
6203 return match_imm_index_operand (arg, operand);
6204
6205 case OP_REG_INDEX:
6206 return match_reg_index_operand (arg, operand);
7361da2c
AB
6207
6208 case OP_SAME_RS_RT:
6209 return match_same_rs_rt_operand (arg, operand);
6210
6211 case OP_CHECK_PREV:
6212 return match_check_prev_operand (arg, operand);
6213
6214 case OP_NON_ZERO_REG:
6215 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6216 }
6217 abort ();
6218}
6219
6220/* ARG is the state after successfully matching an instruction.
6221 Issue any queued-up warnings. */
6222
6223static void
6224check_completed_insn (struct mips_arg_info *arg)
6225{
6226 if (arg->seen_at)
6227 {
6228 if (AT == ATREG)
1661c76c 6229 as_warn (_("used $at without \".set noat\""));
a1d78564 6230 else
1661c76c 6231 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6232 }
6233}
a1d78564 6234
85fcb30f
RS
6235/* Return true if modifying general-purpose register REG needs a delay. */
6236
6237static bfd_boolean
6238reg_needs_delay (unsigned int reg)
6239{
6240 unsigned long prev_pinfo;
6241
6242 prev_pinfo = history[0].insn_mo->pinfo;
6243 if (!mips_opts.noreorder
67dc82bc 6244 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6245 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6246 && (gpr_write_mask (&history[0]) & (1 << reg)))
6247 return TRUE;
6248
6249 return FALSE;
6250}
6251
71400594
RS
6252/* Classify an instruction according to the FIX_VR4120_* enumeration.
6253 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6254 by VR4120 errata. */
4d7206a2 6255
71400594
RS
6256static unsigned int
6257classify_vr4120_insn (const char *name)
252b5132 6258{
71400594
RS
6259 if (strncmp (name, "macc", 4) == 0)
6260 return FIX_VR4120_MACC;
6261 if (strncmp (name, "dmacc", 5) == 0)
6262 return FIX_VR4120_DMACC;
6263 if (strncmp (name, "mult", 4) == 0)
6264 return FIX_VR4120_MULT;
6265 if (strncmp (name, "dmult", 5) == 0)
6266 return FIX_VR4120_DMULT;
6267 if (strstr (name, "div"))
6268 return FIX_VR4120_DIV;
6269 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6270 return FIX_VR4120_MTHILO;
6271 return NUM_FIX_VR4120_CLASSES;
6272}
252b5132 6273
a8d14a88
CM
6274#define INSN_ERET 0x42000018
6275#define INSN_DERET 0x4200001f
6276#define INSN_DMULT 0x1c
6277#define INSN_DMULTU 0x1d
ff239038 6278
71400594
RS
6279/* Return the number of instructions that must separate INSN1 and INSN2,
6280 where INSN1 is the earlier instruction. Return the worst-case value
6281 for any INSN2 if INSN2 is null. */
252b5132 6282
71400594
RS
6283static unsigned int
6284insns_between (const struct mips_cl_insn *insn1,
6285 const struct mips_cl_insn *insn2)
6286{
6287 unsigned long pinfo1, pinfo2;
4c260379 6288 unsigned int mask;
71400594 6289
85fcb30f
RS
6290 /* If INFO2 is null, pessimistically assume that all flags are set for
6291 the second instruction. */
71400594
RS
6292 pinfo1 = insn1->insn_mo->pinfo;
6293 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6294
71400594
RS
6295 /* For most targets, write-after-read dependencies on the HI and LO
6296 registers must be separated by at least two instructions. */
6297 if (!hilo_interlocks)
252b5132 6298 {
71400594
RS
6299 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6300 return 2;
6301 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6302 return 2;
6303 }
6304
6305 /* If we're working around r7000 errata, there must be two instructions
6306 between an mfhi or mflo and any instruction that uses the result. */
6307 if (mips_7000_hilo_fix
df58fc94 6308 && !mips_opts.micromips
71400594 6309 && MF_HILO_INSN (pinfo1)
85fcb30f 6310 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6311 return 2;
6312
ff239038
CM
6313 /* If we're working around 24K errata, one instruction is required
6314 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6315 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6316 {
6317 if (insn1->insn_opcode == INSN_ERET
6318 || insn1->insn_opcode == INSN_DERET)
6319 {
6320 if (insn2 == NULL
6321 || insn2->insn_opcode == INSN_ERET
6322 || insn2->insn_opcode == INSN_DERET
11625dd8 6323 || delayed_branch_p (insn2))
ff239038
CM
6324 return 1;
6325 }
6326 }
6327
a8d14a88
CM
6328 /* If we're working around PMC RM7000 errata, there must be three
6329 nops between a dmult and a load instruction. */
6330 if (mips_fix_rm7000 && !mips_opts.micromips)
6331 {
6332 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6333 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6334 {
6335 if (pinfo2 & INSN_LOAD_MEMORY)
6336 return 3;
6337 }
6338 }
6339
71400594
RS
6340 /* If working around VR4120 errata, check for combinations that need
6341 a single intervening instruction. */
df58fc94 6342 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6343 {
6344 unsigned int class1, class2;
252b5132 6345
71400594
RS
6346 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6347 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6348 {
71400594
RS
6349 if (insn2 == NULL)
6350 return 1;
6351 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6352 if (vr4120_conflicts[class1] & (1 << class2))
6353 return 1;
252b5132 6354 }
71400594
RS
6355 }
6356
df58fc94 6357 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6358 {
6359 /* Check for GPR or coprocessor load delays. All such delays
6360 are on the RT register. */
6361 /* Itbl support may require additional care here. */
67dc82bc 6362 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6363 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6364 {
85fcb30f 6365 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6366 return 1;
6367 }
6368
6369 /* Check for generic coprocessor hazards.
6370
6371 This case is not handled very well. There is no special
6372 knowledge of CP0 handling, and the coprocessors other than
6373 the floating point unit are not distinguished at all. */
6374 /* Itbl support may require additional care here. FIXME!
6375 Need to modify this to include knowledge about
6376 user specified delays! */
43885403 6377 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6378 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6379 {
6380 /* Handle cases where INSN1 writes to a known general coprocessor
6381 register. There must be a one instruction delay before INSN2
6382 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6383 mask = fpr_write_mask (insn1);
6384 if (mask != 0)
252b5132 6385 {
4c260379 6386 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6387 return 1;
252b5132
RH
6388 }
6389 else
6390 {
71400594
RS
6391 /* Read-after-write dependencies on the control registers
6392 require a two-instruction gap. */
6393 if ((pinfo1 & INSN_WRITE_COND_CODE)
6394 && (pinfo2 & INSN_READ_COND_CODE))
6395 return 2;
6396
6397 /* We don't know exactly what INSN1 does. If INSN2 is
6398 also a coprocessor instruction, assume there must be
6399 a one instruction gap. */
6400 if (pinfo2 & INSN_COP)
6401 return 1;
252b5132
RH
6402 }
6403 }
6b76fefe 6404
71400594
RS
6405 /* Check for read-after-write dependencies on the coprocessor
6406 control registers in cases where INSN1 does not need a general
6407 coprocessor delay. This means that INSN1 is a floating point
6408 comparison instruction. */
6409 /* Itbl support may require additional care here. */
6410 else if (!cop_interlocks
6411 && (pinfo1 & INSN_WRITE_COND_CODE)
6412 && (pinfo2 & INSN_READ_COND_CODE))
6413 return 1;
6414 }
6b76fefe 6415
7361da2c
AB
6416 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6417 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6418 and pause. */
6419 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6420 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6421 || (insn2 && delayed_branch_p (insn2))))
6422 return 1;
6423
71400594
RS
6424 return 0;
6425}
6b76fefe 6426
7d8e00cf
RS
6427/* Return the number of nops that would be needed to work around the
6428 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6429 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6430 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6431
6432static int
932d1a1b 6433nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6434 const struct mips_cl_insn *insn)
6435{
4c260379
RS
6436 int i, j;
6437 unsigned int mask;
7d8e00cf
RS
6438
6439 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6440 are not affected by the errata. */
6441 if (insn != 0
6442 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6443 || strcmp (insn->insn_mo->name, "mtlo") == 0
6444 || strcmp (insn->insn_mo->name, "mthi") == 0))
6445 return 0;
6446
6447 /* Search for the first MFLO or MFHI. */
6448 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6449 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6450 {
6451 /* Extract the destination register. */
4c260379 6452 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6453
6454 /* No nops are needed if INSN reads that register. */
4c260379 6455 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6456 return 0;
6457
6458 /* ...or if any of the intervening instructions do. */
6459 for (j = 0; j < i; j++)
4c260379 6460 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6461 return 0;
6462
932d1a1b
RS
6463 if (i >= ignore)
6464 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6465 }
6466 return 0;
6467}
6468
134c0c8b
MR
6469#define BASE_REG_EQ(INSN1, INSN2) \
6470 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6471 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6472
6473/* Return the minimum alignment for this store instruction. */
6474
6475static int
6476fix_24k_align_to (const struct mips_opcode *mo)
6477{
6478 if (strcmp (mo->name, "sh") == 0)
6479 return 2;
6480
6481 if (strcmp (mo->name, "swc1") == 0
6482 || strcmp (mo->name, "swc2") == 0
6483 || strcmp (mo->name, "sw") == 0
6484 || strcmp (mo->name, "sc") == 0
6485 || strcmp (mo->name, "s.s") == 0)
6486 return 4;
6487
6488 if (strcmp (mo->name, "sdc1") == 0
6489 || strcmp (mo->name, "sdc2") == 0
6490 || strcmp (mo->name, "s.d") == 0)
6491 return 8;
6492
6493 /* sb, swl, swr */
6494 return 1;
6495}
6496
6497struct fix_24k_store_info
6498 {
6499 /* Immediate offset, if any, for this store instruction. */
6500 short off;
6501 /* Alignment required by this store instruction. */
6502 int align_to;
6503 /* True for register offsets. */
6504 int register_offset;
6505 };
6506
6507/* Comparison function used by qsort. */
6508
6509static int
6510fix_24k_sort (const void *a, const void *b)
6511{
6512 const struct fix_24k_store_info *pos1 = a;
6513 const struct fix_24k_store_info *pos2 = b;
6514
6515 return (pos1->off - pos2->off);
6516}
6517
6518/* INSN is a store instruction. Try to record the store information
6519 in STINFO. Return false if the information isn't known. */
6520
6521static bfd_boolean
6522fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6523 const struct mips_cl_insn *insn)
15be625d
CM
6524{
6525 /* The instruction must have a known offset. */
6526 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6527 return FALSE;
6528
6529 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6530 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6531 return TRUE;
6532}
6533
932d1a1b
RS
6534/* Return the number of nops that would be needed to work around the 24k
6535 "lost data on stores during refill" errata if instruction INSN
6536 immediately followed the 2 instructions described by HIST.
6537 Ignore hazards that are contained within the first IGNORE
6538 instructions of HIST.
6539
6540 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6541 for the data cache refills and store data. The following describes
6542 the scenario where the store data could be lost.
6543
6544 * A data cache miss, due to either a load or a store, causing fill
6545 data to be supplied by the memory subsystem
6546 * The first three doublewords of fill data are returned and written
6547 into the cache
6548 * A sequence of four stores occurs in consecutive cycles around the
6549 final doubleword of the fill:
6550 * Store A
6551 * Store B
6552 * Store C
6553 * Zero, One or more instructions
6554 * Store D
6555
6556 The four stores A-D must be to different doublewords of the line that
6557 is being filled. The fourth instruction in the sequence above permits
6558 the fill of the final doubleword to be transferred from the FSB into
6559 the cache. In the sequence above, the stores may be either integer
6560 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6561 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6562 different doublewords on the line. If the floating point unit is
6563 running in 1:2 mode, it is not possible to create the sequence above
6564 using only floating point store instructions.
15be625d
CM
6565
6566 In this case, the cache line being filled is incorrectly marked
6567 invalid, thereby losing the data from any store to the line that
6568 occurs between the original miss and the completion of the five
6569 cycle sequence shown above.
6570
932d1a1b 6571 The workarounds are:
15be625d 6572
932d1a1b
RS
6573 * Run the data cache in write-through mode.
6574 * Insert a non-store instruction between
6575 Store A and Store B or Store B and Store C. */
3739860c 6576
15be625d 6577static int
932d1a1b 6578nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6579 const struct mips_cl_insn *insn)
6580{
6581 struct fix_24k_store_info pos[3];
6582 int align, i, base_offset;
6583
932d1a1b
RS
6584 if (ignore >= 2)
6585 return 0;
6586
ab9794cf
RS
6587 /* If the previous instruction wasn't a store, there's nothing to
6588 worry about. */
15be625d
CM
6589 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6590 return 0;
6591
ab9794cf
RS
6592 /* If the instructions after the previous one are unknown, we have
6593 to assume the worst. */
6594 if (!insn)
15be625d
CM
6595 return 1;
6596
ab9794cf
RS
6597 /* Check whether we are dealing with three consecutive stores. */
6598 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6599 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6600 return 0;
6601
6602 /* If we don't know the relationship between the store addresses,
6603 assume the worst. */
ab9794cf 6604 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6605 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6606 return 1;
6607
6608 if (!fix_24k_record_store_info (&pos[0], insn)
6609 || !fix_24k_record_store_info (&pos[1], &hist[0])
6610 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6611 return 1;
6612
6613 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6614
6615 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6616 X bytes and such that the base register + X is known to be aligned
6617 to align bytes. */
6618
6619 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6620 align = 8;
6621 else
6622 {
6623 align = pos[0].align_to;
6624 base_offset = pos[0].off;
6625 for (i = 1; i < 3; i++)
6626 if (align < pos[i].align_to)
6627 {
6628 align = pos[i].align_to;
6629 base_offset = pos[i].off;
6630 }
6631 for (i = 0; i < 3; i++)
6632 pos[i].off -= base_offset;
6633 }
6634
6635 pos[0].off &= ~align + 1;
6636 pos[1].off &= ~align + 1;
6637 pos[2].off &= ~align + 1;
6638
6639 /* If any two stores write to the same chunk, they also write to the
6640 same doubleword. The offsets are still sorted at this point. */
6641 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6642 return 0;
6643
6644 /* A range of at least 9 bytes is needed for the stores to be in
6645 non-overlapping doublewords. */
6646 if (pos[2].off - pos[0].off <= 8)
6647 return 0;
6648
6649 if (pos[2].off - pos[1].off >= 24
6650 || pos[1].off - pos[0].off >= 24
6651 || pos[2].off - pos[0].off >= 32)
6652 return 0;
6653
6654 return 1;
6655}
6656
71400594 6657/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6658 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6659 where HIST[0] is the most recent instruction. Ignore hazards
6660 between INSN and the first IGNORE instructions in HIST.
6661
6662 If INSN is null, return the worse-case number of nops for any
6663 instruction. */
bdaaa2e1 6664
71400594 6665static int
932d1a1b 6666nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6667 const struct mips_cl_insn *insn)
6668{
6669 int i, nops, tmp_nops;
bdaaa2e1 6670
71400594 6671 nops = 0;
932d1a1b 6672 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6673 {
91d6fa6a 6674 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6675 if (tmp_nops > nops)
6676 nops = tmp_nops;
6677 }
7d8e00cf 6678
df58fc94 6679 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6680 {
932d1a1b 6681 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6682 if (tmp_nops > nops)
6683 nops = tmp_nops;
6684 }
6685
df58fc94 6686 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6687 {
932d1a1b 6688 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6689 if (tmp_nops > nops)
6690 nops = tmp_nops;
6691 }
6692
71400594
RS
6693 return nops;
6694}
252b5132 6695
71400594 6696/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6697 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6698 would be needed after the extended sequence, ignoring hazards
6699 in the first IGNORE instructions. */
252b5132 6700
71400594 6701static int
932d1a1b
RS
6702nops_for_sequence (int num_insns, int ignore,
6703 const struct mips_cl_insn *hist, ...)
71400594
RS
6704{
6705 va_list args;
6706 struct mips_cl_insn buffer[MAX_NOPS];
6707 struct mips_cl_insn *cursor;
6708 int nops;
6709
91d6fa6a 6710 va_start (args, hist);
71400594 6711 cursor = buffer + num_insns;
91d6fa6a 6712 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6713 while (cursor > buffer)
6714 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6715
932d1a1b 6716 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6717 va_end (args);
6718 return nops;
6719}
252b5132 6720
71400594
RS
6721/* Like nops_for_insn, but if INSN is a branch, take into account the
6722 worst-case delay for the branch target. */
252b5132 6723
71400594 6724static int
932d1a1b 6725nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6726 const struct mips_cl_insn *insn)
6727{
6728 int nops, tmp_nops;
60b63b72 6729
932d1a1b 6730 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6731 if (delayed_branch_p (insn))
71400594 6732 {
932d1a1b 6733 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6734 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6735 if (tmp_nops > nops)
6736 nops = tmp_nops;
6737 }
11625dd8 6738 else if (compact_branch_p (insn))
71400594 6739 {
932d1a1b 6740 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6741 if (tmp_nops > nops)
6742 nops = tmp_nops;
6743 }
6744 return nops;
6745}
6746
c67a084a
NC
6747/* Fix NOP issue: Replace nops by "or at,at,zero". */
6748
6749static void
6750fix_loongson2f_nop (struct mips_cl_insn * ip)
6751{
df58fc94 6752 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6753 if (strcmp (ip->insn_mo->name, "nop") == 0)
6754 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6755}
6756
6757/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6758 jr target pc &= 'hffff_ffff_cfff_ffff. */
6759
6760static void
6761fix_loongson2f_jump (struct mips_cl_insn * ip)
6762{
df58fc94 6763 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6764 if (strcmp (ip->insn_mo->name, "j") == 0
6765 || strcmp (ip->insn_mo->name, "jr") == 0
6766 || strcmp (ip->insn_mo->name, "jalr") == 0)
6767 {
6768 int sreg;
6769 expressionS ep;
6770
6771 if (! mips_opts.at)
6772 return;
6773
df58fc94 6774 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6775 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6776 return;
6777
6778 ep.X_op = O_constant;
6779 ep.X_add_number = 0xcfff0000;
6780 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6781 ep.X_add_number = 0xffff;
6782 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6783 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6784 }
6785}
6786
6787static void
6788fix_loongson2f (struct mips_cl_insn * ip)
6789{
6790 if (mips_fix_loongson2f_nop)
6791 fix_loongson2f_nop (ip);
6792
6793 if (mips_fix_loongson2f_jump)
6794 fix_loongson2f_jump (ip);
6795}
6796
a4e06468
RS
6797/* IP is a branch that has a delay slot, and we need to fill it
6798 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6799 with the previous instruction.
6800 ADDRESS_EXPR is an operand of the instruction to be used with
6801 RELOC_TYPE. */
a4e06468
RS
6802
6803static bfd_boolean
e407c74b 6804can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6805 bfd_reloc_code_real_type *reloc_type)
a4e06468 6806{
2b0c8b40 6807 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6808 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6809 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6810
6811 /* -O2 and above is required for this optimization. */
6812 if (mips_optimize < 2)
6813 return FALSE;
6814
6815 /* If we have seen .set volatile or .set nomove, don't optimize. */
6816 if (mips_opts.nomove)
6817 return FALSE;
6818
6819 /* We can't swap if the previous instruction's position is fixed. */
6820 if (history[0].fixed_p)
6821 return FALSE;
6822
6823 /* If the previous previous insn was in a .set noreorder, we can't
6824 swap. Actually, the MIPS assembler will swap in this situation.
6825 However, gcc configured -with-gnu-as will generate code like
6826
6827 .set noreorder
6828 lw $4,XXX
6829 .set reorder
6830 INSN
6831 bne $4,$0,foo
6832
6833 in which we can not swap the bne and INSN. If gcc is not configured
6834 -with-gnu-as, it does not output the .set pseudo-ops. */
6835 if (history[1].noreorder_p)
6836 return FALSE;
6837
87333bb7
MR
6838 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6839 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6840 if (mips_opts.mips16 && history[0].fixp[0])
6841 return FALSE;
6842
6843 /* If the branch is itself the target of a branch, we can not swap.
6844 We cheat on this; all we check for is whether there is a label on
6845 this instruction. If there are any branches to anything other than
6846 a label, users must use .set noreorder. */
6847 if (seg_info (now_seg)->label_list)
6848 return FALSE;
6849
6850 /* If the previous instruction is in a variant frag other than this
2309ddf2 6851 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6852 MIPS16 code, which uses variant frags for different purposes. */
6853 if (!mips_opts.mips16
a4e06468
RS
6854 && history[0].frag
6855 && history[0].frag->fr_type == rs_machine_dependent)
6856 return FALSE;
6857
bcd530a7
RS
6858 /* We do not swap with instructions that cannot architecturally
6859 be placed in a branch delay slot, such as SYNC or ERET. We
6860 also refrain from swapping with a trap instruction, since it
6861 complicates trap handlers to have the trap instruction be in
6862 a delay slot. */
a4e06468 6863 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6864 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6865 return FALSE;
6866
6867 /* Check for conflicts between the branch and the instructions
6868 before the candidate delay slot. */
6869 if (nops_for_insn (0, history + 1, ip) > 0)
6870 return FALSE;
6871
6872 /* Check for conflicts between the swapped sequence and the
6873 target of the branch. */
6874 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6875 return FALSE;
6876
6877 /* If the branch reads a register that the previous
6878 instruction sets, we can not swap. */
6879 gpr_read = gpr_read_mask (ip);
6880 prev_gpr_write = gpr_write_mask (&history[0]);
6881 if (gpr_read & prev_gpr_write)
6882 return FALSE;
6883
9d5de888
CF
6884 fpr_read = fpr_read_mask (ip);
6885 prev_fpr_write = fpr_write_mask (&history[0]);
6886 if (fpr_read & prev_fpr_write)
6887 return FALSE;
6888
a4e06468
RS
6889 /* If the branch writes a register that the previous
6890 instruction sets, we can not swap. */
6891 gpr_write = gpr_write_mask (ip);
6892 if (gpr_write & prev_gpr_write)
6893 return FALSE;
6894
6895 /* If the branch writes a register that the previous
6896 instruction reads, we can not swap. */
6897 prev_gpr_read = gpr_read_mask (&history[0]);
6898 if (gpr_write & prev_gpr_read)
6899 return FALSE;
6900
6901 /* If one instruction sets a condition code and the
6902 other one uses a condition code, we can not swap. */
6903 pinfo = ip->insn_mo->pinfo;
6904 if ((pinfo & INSN_READ_COND_CODE)
6905 && (prev_pinfo & INSN_WRITE_COND_CODE))
6906 return FALSE;
6907 if ((pinfo & INSN_WRITE_COND_CODE)
6908 && (prev_pinfo & INSN_READ_COND_CODE))
6909 return FALSE;
6910
6911 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6912 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6913 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6914 return FALSE;
a4e06468 6915
df58fc94
RS
6916 /* If the previous instruction has an incorrect size for a fixed
6917 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6918 pinfo2 = ip->insn_mo->pinfo2;
6919 if (mips_opts.micromips
6920 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6921 && insn_length (history) != 2)
6922 return FALSE;
6923 if (mips_opts.micromips
6924 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6925 && insn_length (history) != 4)
6926 return FALSE;
6927
e407c74b
NC
6928 /* On R5900 short loops need to be fixed by inserting a nop in
6929 the branch delay slots.
6930 A short loop can be terminated too early. */
6931 if (mips_opts.arch == CPU_R5900
6932 /* Check if instruction has a parameter, ignore "j $31". */
6933 && (address_expr != NULL)
6934 /* Parameter must be 16 bit. */
6935 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6936 /* Branch to same segment. */
41065f5e 6937 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6938 /* Branch to same code fragment. */
41065f5e 6939 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6940 /* Can only calculate branch offset if value is known. */
41065f5e 6941 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6942 /* Check if branch is really conditional. */
6943 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6944 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6945 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6946 {
6947 int distance;
6948 /* Check if loop is shorter than 6 instructions including
6949 branch and delay slot. */
41065f5e 6950 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6951 if (distance <= 20)
6952 {
6953 int i;
6954 int rv;
6955
6956 rv = FALSE;
6957 /* When the loop includes branches or jumps,
6958 it is not a short loop. */
6959 for (i = 0; i < (distance / 4); i++)
6960 {
6961 if ((history[i].cleared_p)
41065f5e 6962 || delayed_branch_p (&history[i]))
e407c74b
NC
6963 {
6964 rv = TRUE;
6965 break;
6966 }
6967 }
535b785f 6968 if (!rv)
e407c74b
NC
6969 {
6970 /* Insert nop after branch to fix short loop. */
6971 return FALSE;
6972 }
6973 }
6974 }
6975
a4e06468
RS
6976 return TRUE;
6977}
6978
e407c74b
NC
6979/* Decide how we should add IP to the instruction stream.
6980 ADDRESS_EXPR is an operand of the instruction to be used with
6981 RELOC_TYPE. */
a4e06468
RS
6982
6983static enum append_method
e407c74b 6984get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6985 bfd_reloc_code_real_type *reloc_type)
a4e06468 6986{
a4e06468
RS
6987 /* The relaxed version of a macro sequence must be inherently
6988 hazard-free. */
6989 if (mips_relax.sequence == 2)
6990 return APPEND_ADD;
6991
3b821a28 6992 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
6993 if (mips_opts.noreorder)
6994 return APPEND_ADD;
6995
6996 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6997 if (delayed_branch_p (ip))
a4e06468 6998 {
e407c74b
NC
6999 if (!branch_likely_p (ip)
7000 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7001 return APPEND_SWAP;
7002
7003 if (mips_opts.mips16
7004 && ISA_SUPPORTS_MIPS16E
fc76e730 7005 && gpr_read_mask (ip) != 0)
a4e06468
RS
7006 return APPEND_ADD_COMPACT;
7007
7bd374a4
MR
7008 if (mips_opts.micromips
7009 && ((ip->insn_opcode & 0xffe0) == 0x4580
7010 || (!forced_insn_length
7011 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7012 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7013 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7014 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7015 return APPEND_ADD_COMPACT;
7016
a4e06468
RS
7017 return APPEND_ADD_WITH_NOP;
7018 }
7019
a4e06468
RS
7020 return APPEND_ADD;
7021}
7022
7bd374a4
MR
7023/* IP is an instruction whose opcode we have just changed, END points
7024 to the end of the opcode table processed. Point IP->insn_mo to the
7025 new opcode's definition. */
ceb94aa5
RS
7026
7027static void
7bd374a4 7028find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7029{
7bd374a4 7030 const struct mips_opcode *mo;
ceb94aa5 7031
ceb94aa5 7032 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7033 if (mo->pinfo != INSN_MACRO
7034 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7035 {
7036 ip->insn_mo = mo;
7037 return;
7038 }
7039 abort ();
7040}
7041
7bd374a4
MR
7042/* IP is a MIPS16 instruction whose opcode we have just changed.
7043 Point IP->insn_mo to the new opcode's definition. */
7044
7045static void
7046find_altered_mips16_opcode (struct mips_cl_insn *ip)
7047{
7048 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7049}
7050
7051/* IP is a microMIPS instruction whose opcode we have just changed.
7052 Point IP->insn_mo to the new opcode's definition. */
7053
7054static void
7055find_altered_micromips_opcode (struct mips_cl_insn *ip)
7056{
7057 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7058}
7059
df58fc94
RS
7060/* For microMIPS macros, we need to generate a local number label
7061 as the target of branches. */
7062#define MICROMIPS_LABEL_CHAR '\037'
7063static unsigned long micromips_target_label;
7064static char micromips_target_name[32];
7065
7066static char *
7067micromips_label_name (void)
7068{
7069 char *p = micromips_target_name;
7070 char symbol_name_temporary[24];
7071 unsigned long l;
7072 int i;
7073
7074 if (*p)
7075 return p;
7076
7077 i = 0;
7078 l = micromips_target_label;
7079#ifdef LOCAL_LABEL_PREFIX
7080 *p++ = LOCAL_LABEL_PREFIX;
7081#endif
7082 *p++ = 'L';
7083 *p++ = MICROMIPS_LABEL_CHAR;
7084 do
7085 {
7086 symbol_name_temporary[i++] = l % 10 + '0';
7087 l /= 10;
7088 }
7089 while (l != 0);
7090 while (i > 0)
7091 *p++ = symbol_name_temporary[--i];
7092 *p = '\0';
7093
7094 return micromips_target_name;
7095}
7096
7097static void
7098micromips_label_expr (expressionS *label_expr)
7099{
7100 label_expr->X_op = O_symbol;
7101 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7102 label_expr->X_add_number = 0;
7103}
7104
7105static void
7106micromips_label_inc (void)
7107{
7108 micromips_target_label++;
7109 *micromips_target_name = '\0';
7110}
7111
7112static void
7113micromips_add_label (void)
7114{
7115 symbolS *s;
7116
7117 s = colon (micromips_label_name ());
7118 micromips_label_inc ();
f3ded42a 7119 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7120}
7121
7122/* If assembling microMIPS code, then return the microMIPS reloc
7123 corresponding to the requested one if any. Otherwise return
7124 the reloc unchanged. */
7125
7126static bfd_reloc_code_real_type
7127micromips_map_reloc (bfd_reloc_code_real_type reloc)
7128{
7129 static const bfd_reloc_code_real_type relocs[][2] =
7130 {
7131 /* Keep sorted incrementally by the left-hand key. */
7132 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7133 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7134 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7135 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7136 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7137 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7138 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7139 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7140 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7141 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7142 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7143 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7144 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7145 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7146 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7147 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7148 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7149 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7150 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7151 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7152 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7153 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7154 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7155 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7156 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7157 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7158 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7159 };
7160 bfd_reloc_code_real_type r;
7161 size_t i;
7162
7163 if (!mips_opts.micromips)
7164 return reloc;
7165 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7166 {
7167 r = relocs[i][0];
7168 if (r > reloc)
7169 return reloc;
7170 if (r == reloc)
7171 return relocs[i][1];
7172 }
7173 return reloc;
7174}
7175
b886a2ab
RS
7176/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7177 Return true on success, storing the resolved value in RESULT. */
7178
7179static bfd_boolean
7180calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7181 offsetT *result)
7182{
7183 switch (reloc)
7184 {
7185 case BFD_RELOC_MIPS_HIGHEST:
7186 case BFD_RELOC_MICROMIPS_HIGHEST:
7187 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7188 return TRUE;
7189
7190 case BFD_RELOC_MIPS_HIGHER:
7191 case BFD_RELOC_MICROMIPS_HIGHER:
7192 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7193 return TRUE;
7194
7195 case BFD_RELOC_HI16_S:
41947d9e 7196 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7197 case BFD_RELOC_MICROMIPS_HI16_S:
7198 case BFD_RELOC_MIPS16_HI16_S:
7199 *result = ((operand + 0x8000) >> 16) & 0xffff;
7200 return TRUE;
7201
7202 case BFD_RELOC_HI16:
7203 case BFD_RELOC_MICROMIPS_HI16:
7204 case BFD_RELOC_MIPS16_HI16:
7205 *result = (operand >> 16) & 0xffff;
7206 return TRUE;
7207
7208 case BFD_RELOC_LO16:
41947d9e 7209 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7210 case BFD_RELOC_MICROMIPS_LO16:
7211 case BFD_RELOC_MIPS16_LO16:
7212 *result = operand & 0xffff;
7213 return TRUE;
7214
7215 case BFD_RELOC_UNUSED:
7216 *result = operand;
7217 return TRUE;
7218
7219 default:
7220 return FALSE;
7221 }
7222}
7223
71400594
RS
7224/* Output an instruction. IP is the instruction information.
7225 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7226 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7227 a macro expansion. */
71400594
RS
7228
7229static void
7230append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7231 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7232{
14fe068b 7233 unsigned long prev_pinfo2, pinfo;
71400594 7234 bfd_boolean relaxed_branch = FALSE;
a4e06468 7235 enum append_method method;
2309ddf2 7236 bfd_boolean relax32;
2b0c8b40 7237 int branch_disp;
71400594 7238
2309ddf2 7239 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7240 fix_loongson2f (ip);
7241
738f4d98 7242 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7243 file_ase_micromips |= mips_opts.micromips;
738f4d98 7244
df58fc94 7245 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7246 pinfo = ip->insn_mo->pinfo;
df58fc94 7247
7bd374a4
MR
7248 /* Don't raise alarm about `nods' frags as they'll fill in the right
7249 kind of nop in relaxation if required. */
df58fc94
RS
7250 if (mips_opts.micromips
7251 && !expansionp
7bd374a4
MR
7252 && !(history[0].frag
7253 && history[0].frag->fr_type == rs_machine_dependent
7254 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7255 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7256 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7257 && micromips_insn_length (ip->insn_mo) != 2)
7258 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7259 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7260 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7261 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7262
15be625d
CM
7263 if (address_expr == NULL)
7264 ip->complete_p = 1;
b886a2ab
RS
7265 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7266 && reloc_type[1] == BFD_RELOC_UNUSED
7267 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7268 && address_expr->X_op == O_constant)
7269 {
15be625d
CM
7270 switch (*reloc_type)
7271 {
15be625d 7272 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7273 {
7274 int shift;
7275
17c6c9d9
MR
7276 /* Shift is 2, unusually, for microMIPS JALX. */
7277 shift = (mips_opts.micromips
7278 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7279 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7280 as_bad (_("jump to misaligned address (0x%lx)"),
7281 (unsigned long) address_expr->X_add_number);
7282 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7283 & 0x3ffffff);
335574df 7284 ip->complete_p = 1;
df58fc94 7285 }
15be625d
CM
7286 break;
7287
7288 case BFD_RELOC_MIPS16_JMP:
7289 if ((address_expr->X_add_number & 3) != 0)
7290 as_bad (_("jump to misaligned address (0x%lx)"),
7291 (unsigned long) address_expr->X_add_number);
7292 ip->insn_opcode |=
7293 (((address_expr->X_add_number & 0x7c0000) << 3)
7294 | ((address_expr->X_add_number & 0xf800000) >> 7)
7295 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7296 ip->complete_p = 1;
15be625d
CM
7297 break;
7298
7299 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7300 {
7301 int shift;
7302
7303 shift = mips_opts.micromips ? 1 : 2;
7304 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7305 as_bad (_("branch to misaligned address (0x%lx)"),
7306 (unsigned long) address_expr->X_add_number);
7307 if (!mips_relax_branch)
7308 {
7309 if ((address_expr->X_add_number + (1 << (shift + 15)))
7310 & ~((1 << (shift + 16)) - 1))
7311 as_bad (_("branch address range overflow (0x%lx)"),
7312 (unsigned long) address_expr->X_add_number);
7313 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7314 & 0xffff);
7315 }
df58fc94 7316 }
15be625d
CM
7317 break;
7318
7361da2c
AB
7319 case BFD_RELOC_MIPS_21_PCREL_S2:
7320 {
7321 int shift;
7322
7323 shift = 2;
7324 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7325 as_bad (_("branch to misaligned address (0x%lx)"),
7326 (unsigned long) address_expr->X_add_number);
7327 if ((address_expr->X_add_number + (1 << (shift + 20)))
7328 & ~((1 << (shift + 21)) - 1))
7329 as_bad (_("branch address range overflow (0x%lx)"),
7330 (unsigned long) address_expr->X_add_number);
7331 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7332 & 0x1fffff);
7333 }
7334 break;
7335
7336 case BFD_RELOC_MIPS_26_PCREL_S2:
7337 {
7338 int shift;
7339
7340 shift = 2;
7341 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7342 as_bad (_("branch to misaligned address (0x%lx)"),
7343 (unsigned long) address_expr->X_add_number);
7344 if ((address_expr->X_add_number + (1 << (shift + 25)))
7345 & ~((1 << (shift + 26)) - 1))
7346 as_bad (_("branch address range overflow (0x%lx)"),
7347 (unsigned long) address_expr->X_add_number);
7348 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7349 & 0x3ffffff);
7350 }
7351 break;
7352
15be625d 7353 default:
b886a2ab
RS
7354 {
7355 offsetT value;
7356
7357 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7358 &value))
7359 {
7360 ip->insn_opcode |= value & 0xffff;
7361 ip->complete_p = 1;
7362 }
7363 }
7364 break;
7365 }
15be625d
CM
7366 }
7367
71400594
RS
7368 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7369 {
7370 /* There are a lot of optimizations we could do that we don't.
7371 In particular, we do not, in general, reorder instructions.
7372 If you use gcc with optimization, it will reorder
7373 instructions and generally do much more optimization then we
7374 do here; repeating all that work in the assembler would only
7375 benefit hand written assembly code, and does not seem worth
7376 it. */
7377 int nops = (mips_optimize == 0
932d1a1b
RS
7378 ? nops_for_insn (0, history, NULL)
7379 : nops_for_insn_or_target (0, history, ip));
71400594 7380 if (nops > 0)
252b5132
RH
7381 {
7382 fragS *old_frag;
7383 unsigned long old_frag_offset;
7384 int i;
252b5132
RH
7385
7386 old_frag = frag_now;
7387 old_frag_offset = frag_now_fix ();
7388
7389 for (i = 0; i < nops; i++)
14fe068b
RS
7390 add_fixed_insn (NOP_INSN);
7391 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7392
7393 if (listing)
7394 {
7395 listing_prev_line ();
7396 /* We may be at the start of a variant frag. In case we
7397 are, make sure there is enough space for the frag
7398 after the frags created by listing_prev_line. The
7399 argument to frag_grow here must be at least as large
7400 as the argument to all other calls to frag_grow in
7401 this file. We don't have to worry about being in the
7402 middle of a variant frag, because the variants insert
7403 all needed nop instructions themselves. */
7404 frag_grow (40);
7405 }
7406
462427c4 7407 mips_move_text_labels ();
252b5132
RH
7408
7409#ifndef NO_ECOFF_DEBUGGING
7410 if (ECOFF_DEBUGGING)
7411 ecoff_fix_loc (old_frag, old_frag_offset);
7412#endif
7413 }
71400594
RS
7414 }
7415 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7416 {
932d1a1b
RS
7417 int nops;
7418
7419 /* Work out how many nops in prev_nop_frag are needed by IP,
7420 ignoring hazards generated by the first prev_nop_frag_since
7421 instructions. */
7422 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7423 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7424
71400594
RS
7425 /* Enforce NOPS as a minimum. */
7426 if (nops > prev_nop_frag_required)
7427 prev_nop_frag_required = nops;
252b5132 7428
71400594
RS
7429 if (prev_nop_frag_holds == prev_nop_frag_required)
7430 {
7431 /* Settle for the current number of nops. Update the history
7432 accordingly (for the benefit of any future .set reorder code). */
7433 prev_nop_frag = NULL;
7434 insert_into_history (prev_nop_frag_since,
7435 prev_nop_frag_holds, NOP_INSN);
7436 }
7437 else
7438 {
7439 /* Allow this instruction to replace one of the nops that was
7440 tentatively added to prev_nop_frag. */
df58fc94 7441 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7442 prev_nop_frag_holds--;
7443 prev_nop_frag_since++;
252b5132
RH
7444 }
7445 }
7446
e407c74b 7447 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7448 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7449
e410add4
RS
7450 dwarf2_emit_insn (0);
7451 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7452 so "move" the instruction address accordingly.
7453
7454 Also, it doesn't seem appropriate for the assembler to reorder .loc
7455 entries. If this instruction is a branch that we are going to swap
7456 with the previous instruction, the two instructions should be
7457 treated as a unit, and the debug information for both instructions
7458 should refer to the start of the branch sequence. Using the
7459 current position is certainly wrong when swapping a 32-bit branch
7460 and a 16-bit delay slot, since the current position would then be
7461 in the middle of a branch. */
7462 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7463
df58fc94
RS
7464 relax32 = (mips_relax_branch
7465 /* Don't try branch relaxation within .set nomacro, or within
7466 .set noat if we use $at for PIC computations. If it turns
7467 out that the branch was out-of-range, we'll get an error. */
7468 && !mips_opts.warn_about_macros
7469 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7470 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7471 as they have no complementing branches. */
7472 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7473
7474 if (!HAVE_CODE_COMPRESSION
7475 && address_expr
7476 && relax32
0b25d3e6 7477 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7478 && delayed_branch_p (ip))
4a6a3df4 7479 {
895921c9 7480 relaxed_branch = TRUE;
1e915849
RS
7481 add_relaxed_insn (ip, (relaxed_branch_length
7482 (NULL, NULL,
11625dd8
RS
7483 uncond_branch_p (ip) ? -1
7484 : branch_likely_p (ip) ? 1
1e915849
RS
7485 : 0)), 4,
7486 RELAX_BRANCH_ENCODE
ce8ad872 7487 (AT, mips_pic != NO_PIC,
11625dd8
RS
7488 uncond_branch_p (ip),
7489 branch_likely_p (ip),
1e915849
RS
7490 pinfo & INSN_WRITE_GPR_31,
7491 0),
7492 address_expr->X_add_symbol,
7493 address_expr->X_add_number);
4a6a3df4
AO
7494 *reloc_type = BFD_RELOC_UNUSED;
7495 }
df58fc94
RS
7496 else if (mips_opts.micromips
7497 && address_expr
7498 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7499 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7500 && (delayed_branch_p (ip) || compact_branch_p (ip))
7501 /* Don't try branch relaxation when users specify
7502 16-bit/32-bit instructions. */
7503 && !forced_insn_length)
df58fc94 7504 {
7bd374a4
MR
7505 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7506 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7507 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7508 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7509 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7510 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7511 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7512 int length32 = nods ? 8 : 4;
df58fc94
RS
7513
7514 gas_assert (address_expr != NULL);
7515 gas_assert (!mips_relax.sequence);
7516
2b0c8b40 7517 relaxed_branch = TRUE;
7bd374a4
MR
7518 if (nods)
7519 method = APPEND_ADD;
7520 if (relax32)
7521 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7522 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7523 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7524 mips_pic != NO_PIC,
7bd374a4 7525 uncond, compact, al, nods,
40209cad 7526 relax32, 0, 0),
df58fc94
RS
7527 address_expr->X_add_symbol,
7528 address_expr->X_add_number);
7529 *reloc_type = BFD_RELOC_UNUSED;
7530 }
7531 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7532 {
7fd53920
MR
7533 bfd_boolean require_unextended;
7534 bfd_boolean require_extended;
88a7ef16
MR
7535 symbolS *symbol;
7536 offsetT offset;
7537
7fd53920
MR
7538 if (forced_insn_length != 0)
7539 {
7540 require_unextended = forced_insn_length == 2;
7541 require_extended = forced_insn_length == 4;
7542 }
7543 else
7544 {
7545 require_unextended = (mips_opts.noautoextend
7546 && !mips_opcode_32bit_p (ip->insn_mo));
7547 require_extended = 0;
7548 }
7549
252b5132 7550 /* We need to set up a variant frag. */
df58fc94 7551 gas_assert (address_expr != NULL);
88a7ef16
MR
7552 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7553 symbol created by `make_expr_symbol' may not get a necessary
7554 external relocation produced. */
7555 if (address_expr->X_op == O_symbol)
7556 {
7557 symbol = address_expr->X_add_symbol;
7558 offset = address_expr->X_add_number;
7559 }
7560 else
7561 {
7562 symbol = make_expr_symbol (address_expr);
82d808ed 7563 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7564 offset = 0;
7565 }
8507b6e7 7566 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7567 RELAX_MIPS16_ENCODE
7568 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7569 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7570 mips_pic != NO_PIC,
7571 HAVE_32BIT_SYMBOLS,
7572 mips_opts.warn_about_macros,
7fd53920 7573 require_unextended, require_extended,
11625dd8 7574 delayed_branch_p (&history[0]),
1e915849 7575 history[0].mips16_absolute_jump_p),
88a7ef16 7576 symbol, offset);
252b5132 7577 }
5c04167a 7578 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7579 {
11625dd8 7580 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7581 /* Make sure there is enough room to swap this instruction with
7582 a following jump instruction. */
7583 frag_grow (6);
1e915849 7584 add_fixed_insn (ip);
252b5132
RH
7585 }
7586 else
7587 {
7588 if (mips_opts.mips16
7589 && mips_opts.noreorder
11625dd8 7590 && delayed_branch_p (&history[0]))
252b5132
RH
7591 as_warn (_("extended instruction in delay slot"));
7592
4d7206a2
RS
7593 if (mips_relax.sequence)
7594 {
7595 /* If we've reached the end of this frag, turn it into a variant
7596 frag and record the information for the instructions we've
7597 written so far. */
7598 if (frag_room () < 4)
7599 relax_close_frag ();
df58fc94 7600 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7601 }
7602
584892a6 7603 if (mips_relax.sequence != 2)
df58fc94
RS
7604 {
7605 if (mips_macro_warning.first_insn_sizes[0] == 0)
7606 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7607 mips_macro_warning.sizes[0] += insn_length (ip);
7608 mips_macro_warning.insns[0]++;
7609 }
584892a6 7610 if (mips_relax.sequence != 1)
df58fc94
RS
7611 {
7612 if (mips_macro_warning.first_insn_sizes[1] == 0)
7613 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7614 mips_macro_warning.sizes[1] += insn_length (ip);
7615 mips_macro_warning.insns[1]++;
7616 }
584892a6 7617
1e915849
RS
7618 if (mips_opts.mips16)
7619 {
7620 ip->fixed_p = 1;
7621 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7622 }
7623 add_fixed_insn (ip);
252b5132
RH
7624 }
7625
9fe77896 7626 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7627 {
df58fc94 7628 bfd_reloc_code_real_type final_type[3];
2309ddf2 7629 reloc_howto_type *howto0;
9fe77896
RS
7630 reloc_howto_type *howto;
7631 int i;
34ce925e 7632
df58fc94
RS
7633 /* Perform any necessary conversion to microMIPS relocations
7634 and find out how many relocations there actually are. */
7635 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7636 final_type[i] = micromips_map_reloc (reloc_type[i]);
7637
9fe77896
RS
7638 /* In a compound relocation, it is the final (outermost)
7639 operator that determines the relocated field. */
2309ddf2 7640 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7641 if (!howto)
7642 abort ();
2309ddf2
MR
7643
7644 if (i > 1)
7645 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7646 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7647 bfd_get_reloc_size (howto),
7648 address_expr,
2309ddf2
MR
7649 howto0 && howto0->pc_relative,
7650 final_type[0]);
ce8ad872
MR
7651 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7652 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7653
7654 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7655 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7656 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7657
7658 /* These relocations can have an addend that won't fit in
7659 4 octets for 64bit assembly. */
bad1aba3 7660 if (GPR_SIZE == 64
9fe77896
RS
7661 && ! howto->partial_inplace
7662 && (reloc_type[0] == BFD_RELOC_16
7663 || reloc_type[0] == BFD_RELOC_32
7664 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7665 || reloc_type[0] == BFD_RELOC_GPREL16
7666 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7667 || reloc_type[0] == BFD_RELOC_GPREL32
7668 || reloc_type[0] == BFD_RELOC_64
7669 || reloc_type[0] == BFD_RELOC_CTOR
7670 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7671 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7672 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7673 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7674 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7675 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7676 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7677 || hi16_reloc_p (reloc_type[0])
7678 || lo16_reloc_p (reloc_type[0])))
7679 ip->fixp[0]->fx_no_overflow = 1;
7680
ddaf2c41
MR
7681 /* These relocations can have an addend that won't fit in 2 octets. */
7682 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7683 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7684 ip->fixp[0]->fx_no_overflow = 1;
7685
9fe77896
RS
7686 if (mips_relax.sequence)
7687 {
7688 if (mips_relax.first_fixup == 0)
7689 mips_relax.first_fixup = ip->fixp[0];
7690 }
7691 else if (reloc_needs_lo_p (*reloc_type))
7692 {
7693 struct mips_hi_fixup *hi_fixup;
7694
7695 /* Reuse the last entry if it already has a matching %lo. */
7696 hi_fixup = mips_hi_fixup_list;
7697 if (hi_fixup == 0
7698 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7699 {
325801bd 7700 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7701 hi_fixup->next = mips_hi_fixup_list;
7702 mips_hi_fixup_list = hi_fixup;
4d7206a2 7703 }
9fe77896
RS
7704 hi_fixup->fixp = ip->fixp[0];
7705 hi_fixup->seg = now_seg;
7706 }
252b5132 7707
9fe77896
RS
7708 /* Add fixups for the second and third relocations, if given.
7709 Note that the ABI allows the second relocation to be
7710 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7711 moment we only use RSS_UNDEF, but we could add support
7712 for the others if it ever becomes necessary. */
7713 for (i = 1; i < 3; i++)
7714 if (reloc_type[i] != BFD_RELOC_UNUSED)
7715 {
7716 ip->fixp[i] = fix_new (ip->frag, ip->where,
7717 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7718 FALSE, final_type[i]);
f6688943 7719
9fe77896
RS
7720 /* Use fx_tcbit to mark compound relocs. */
7721 ip->fixp[0]->fx_tcbit = 1;
7722 ip->fixp[i]->fx_tcbit = 1;
7723 }
252b5132 7724 }
252b5132
RH
7725
7726 /* Update the register mask information. */
4c260379
RS
7727 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7728 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7729
a4e06468 7730 switch (method)
252b5132 7731 {
a4e06468
RS
7732 case APPEND_ADD:
7733 insert_into_history (0, 1, ip);
7734 break;
7735
7736 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7737 {
7738 struct mips_cl_insn *nop;
7739
7740 insert_into_history (0, 1, ip);
7741 nop = get_delay_slot_nop (ip);
7742 add_fixed_insn (nop);
7743 insert_into_history (0, 1, nop);
7744 if (mips_relax.sequence)
7745 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7746 }
a4e06468
RS
7747 break;
7748
7749 case APPEND_ADD_COMPACT:
7750 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7751 if (mips_opts.mips16)
7752 {
7753 ip->insn_opcode |= 0x0080;
7754 find_altered_mips16_opcode (ip);
7755 }
7756 /* Convert microMIPS instructions. */
7757 else if (mips_opts.micromips)
7758 {
7759 /* jr16->jrc */
7760 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7761 ip->insn_opcode |= 0x0020;
7762 /* b16->bc */
7763 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7764 ip->insn_opcode = 0x40e00000;
7765 /* beqz16->beqzc, bnez16->bnezc */
7766 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7767 {
7768 unsigned long regno;
7769
7770 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7771 regno &= MICROMIPSOP_MASK_MD;
7772 regno = micromips_to_32_reg_d_map[regno];
7773 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7774 | (regno << MICROMIPSOP_SH_RS)
7775 | 0x40a00000) ^ 0x00400000;
7776 }
7777 /* beqz->beqzc, bnez->bnezc */
7778 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7779 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7780 | ((ip->insn_opcode >> 7) & 0x00400000)
7781 | 0x40a00000) ^ 0x00400000;
7782 /* beq $0->beqzc, bne $0->bnezc */
7783 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7784 ip->insn_opcode = (((ip->insn_opcode >>
7785 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7786 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7787 | ((ip->insn_opcode >> 7) & 0x00400000)
7788 | 0x40a00000) ^ 0x00400000;
7789 else
7790 abort ();
7791 find_altered_micromips_opcode (ip);
7792 }
7793 else
7794 abort ();
a4e06468
RS
7795 install_insn (ip);
7796 insert_into_history (0, 1, ip);
7797 break;
7798
7799 case APPEND_SWAP:
7800 {
7801 struct mips_cl_insn delay = history[0];
99e7978b
MF
7802
7803 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7804 {
7805 /* Add the delay slot instruction to the end of the
7806 current frag and shrink the fixed part of the
7807 original frag. If the branch occupies the tail of
7808 the latter, move it backwards to cover the gap. */
2b0c8b40 7809 delay.frag->fr_fix -= branch_disp;
a4e06468 7810 if (delay.frag == ip->frag)
2b0c8b40 7811 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7812 add_fixed_insn (&delay);
7813 }
7814 else
7815 {
5e35670b
MR
7816 /* If this is not a relaxed branch and we are in the
7817 same frag, then just swap the instructions. */
7818 move_insn (ip, delay.frag, delay.where);
7819 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7820 }
7821 history[0] = *ip;
7822 delay.fixed_p = 1;
7823 insert_into_history (0, 1, &delay);
7824 }
7825 break;
252b5132
RH
7826 }
7827
13408f1e 7828 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7829 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7830 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7831 {
7832 unsigned int i;
7833
79850f26 7834 mips_no_prev_insn ();
13408f1e 7835
e407c74b 7836 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7837 history[i].cleared_p = 1;
e407c74b
NC
7838 }
7839
df58fc94
RS
7840 /* We need to emit a label at the end of branch-likely macros. */
7841 if (emit_branch_likely_macro)
7842 {
7843 emit_branch_likely_macro = FALSE;
7844 micromips_add_label ();
7845 }
7846
252b5132
RH
7847 /* We just output an insn, so the next one doesn't have a label. */
7848 mips_clear_insn_labels ();
252b5132
RH
7849}
7850
e407c74b
NC
7851/* Forget that there was any previous instruction or label.
7852 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7853
7854static void
7d10b47d 7855mips_no_prev_insn (void)
252b5132 7856{
7d10b47d
RS
7857 prev_nop_frag = NULL;
7858 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7859 mips_clear_insn_labels ();
7860}
7861
7d10b47d
RS
7862/* This function must be called before we emit something other than
7863 instructions. It is like mips_no_prev_insn except that it inserts
7864 any NOPS that might be needed by previous instructions. */
252b5132 7865
7d10b47d
RS
7866void
7867mips_emit_delays (void)
252b5132
RH
7868{
7869 if (! mips_opts.noreorder)
7870 {
932d1a1b 7871 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7872 if (nops > 0)
7873 {
7d10b47d
RS
7874 while (nops-- > 0)
7875 add_fixed_insn (NOP_INSN);
462427c4 7876 mips_move_text_labels ();
7d10b47d
RS
7877 }
7878 }
7879 mips_no_prev_insn ();
7880}
7881
7882/* Start a (possibly nested) noreorder block. */
7883
7884static void
7885start_noreorder (void)
7886{
7887 if (mips_opts.noreorder == 0)
7888 {
7889 unsigned int i;
7890 int nops;
7891
7892 /* None of the instructions before the .set noreorder can be moved. */
7893 for (i = 0; i < ARRAY_SIZE (history); i++)
7894 history[i].fixed_p = 1;
7895
7896 /* Insert any nops that might be needed between the .set noreorder
7897 block and the previous instructions. We will later remove any
7898 nops that turn out not to be needed. */
932d1a1b 7899 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7900 if (nops > 0)
7901 {
7902 if (mips_optimize != 0)
252b5132
RH
7903 {
7904 /* Record the frag which holds the nop instructions, so
7905 that we can remove them if we don't need them. */
df58fc94 7906 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7907 prev_nop_frag = frag_now;
7908 prev_nop_frag_holds = nops;
7909 prev_nop_frag_required = 0;
7910 prev_nop_frag_since = 0;
7911 }
7912
7913 for (; nops > 0; --nops)
1e915849 7914 add_fixed_insn (NOP_INSN);
252b5132 7915
7d10b47d
RS
7916 /* Move on to a new frag, so that it is safe to simply
7917 decrease the size of prev_nop_frag. */
7918 frag_wane (frag_now);
7919 frag_new (0);
462427c4 7920 mips_move_text_labels ();
252b5132 7921 }
df58fc94 7922 mips_mark_labels ();
7d10b47d 7923 mips_clear_insn_labels ();
252b5132 7924 }
7d10b47d
RS
7925 mips_opts.noreorder++;
7926 mips_any_noreorder = 1;
7927}
252b5132 7928
7d10b47d 7929/* End a nested noreorder block. */
252b5132 7930
7d10b47d
RS
7931static void
7932end_noreorder (void)
7933{
7934 mips_opts.noreorder--;
7935 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7936 {
7937 /* Commit to inserting prev_nop_frag_required nops and go back to
7938 handling nop insertion the .set reorder way. */
7939 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7940 * NOP_INSN_SIZE);
7d10b47d
RS
7941 insert_into_history (prev_nop_frag_since,
7942 prev_nop_frag_required, NOP_INSN);
7943 prev_nop_frag = NULL;
7944 }
252b5132
RH
7945}
7946
97d87491
RS
7947/* Sign-extend 32-bit mode constants that have bit 31 set and all
7948 higher bits unset. */
7949
7950static void
7951normalize_constant_expr (expressionS *ex)
7952{
7953 if (ex->X_op == O_constant
7954 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7955 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7956 - 0x80000000);
7957}
7958
7959/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7960 all higher bits unset. */
7961
7962static void
7963normalize_address_expr (expressionS *ex)
7964{
7965 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7966 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7967 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7968 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7969 - 0x80000000);
7970}
7971
7972/* Try to match TOKENS against OPCODE, storing the result in INSN.
7973 Return true if the match was successful.
7974
7975 OPCODE_EXTRA is a value that should be ORed into the opcode
7976 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7977 there are more alternatives after OPCODE and SOFT_MATCH is
7978 as for mips_arg_info. */
7979
7980static bfd_boolean
7981match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7982 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7983 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7984{
7985 const char *args;
7986 struct mips_arg_info arg;
7987 const struct mips_operand *operand;
7988 char c;
7989
7990 imm_expr.X_op = O_absent;
97d87491
RS
7991 offset_expr.X_op = O_absent;
7992 offset_reloc[0] = BFD_RELOC_UNUSED;
7993 offset_reloc[1] = BFD_RELOC_UNUSED;
7994 offset_reloc[2] = BFD_RELOC_UNUSED;
7995
7996 create_insn (insn, opcode);
60f20e8b
RS
7997 /* When no opcode suffix is specified, assume ".xyzw". */
7998 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7999 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8000 else
8001 insn->insn_opcode |= opcode_extra;
97d87491
RS
8002 memset (&arg, 0, sizeof (arg));
8003 arg.insn = insn;
8004 arg.token = tokens;
8005 arg.argnum = 1;
8006 arg.last_regno = ILLEGAL_REG;
8007 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8008 arg.lax_match = lax_match;
97d87491
RS
8009 for (args = opcode->args;; ++args)
8010 {
8011 if (arg.token->type == OT_END)
8012 {
8013 /* Handle unary instructions in which only one operand is given.
8014 The source is then the same as the destination. */
8015 if (arg.opnum == 1 && *args == ',')
8016 {
8017 operand = (mips_opts.micromips
8018 ? decode_micromips_operand (args + 1)
8019 : decode_mips_operand (args + 1));
8020 if (operand && mips_optional_operand_p (operand))
8021 {
8022 arg.token = tokens;
8023 arg.argnum = 1;
8024 continue;
8025 }
8026 }
8027
8028 /* Treat elided base registers as $0. */
8029 if (strcmp (args, "(b)") == 0)
8030 args += 3;
8031
8032 if (args[0] == '+')
8033 switch (args[1])
8034 {
8035 case 'K':
8036 case 'N':
8037 /* The register suffix is optional. */
8038 args += 2;
8039 break;
8040 }
8041
8042 /* Fail the match if there were too few operands. */
8043 if (*args)
8044 return FALSE;
8045
8046 /* Successful match. */
60f20e8b
RS
8047 if (!complete_p)
8048 return TRUE;
e3de51ce 8049 clear_insn_error ();
97d87491
RS
8050 if (arg.dest_regno == arg.last_regno
8051 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8052 {
8053 if (arg.opnum == 2)
e3de51ce 8054 set_insn_error
1661c76c 8055 (0, _("source and destination must be different"));
97d87491 8056 else if (arg.last_regno == 31)
e3de51ce 8057 set_insn_error
1661c76c 8058 (0, _("a destination register must be supplied"));
97d87491 8059 }
173d3447
CF
8060 else if (arg.last_regno == 31
8061 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8062 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8063 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8064 check_completed_insn (&arg);
8065 return TRUE;
8066 }
8067
8068 /* Fail the match if the line has too many operands. */
8069 if (*args == 0)
8070 return FALSE;
8071
8072 /* Handle characters that need to match exactly. */
8073 if (*args == '(' || *args == ')' || *args == ',')
8074 {
8075 if (match_char (&arg, *args))
8076 continue;
8077 return FALSE;
8078 }
8079 if (*args == '#')
8080 {
8081 ++args;
8082 if (arg.token->type == OT_DOUBLE_CHAR
8083 && arg.token->u.ch == *args)
8084 {
8085 ++arg.token;
8086 continue;
8087 }
8088 return FALSE;
8089 }
8090
8091 /* Handle special macro operands. Work out the properties of
8092 other operands. */
8093 arg.opnum += 1;
97d87491
RS
8094 switch (*args)
8095 {
7361da2c
AB
8096 case '-':
8097 switch (args[1])
8098 {
8099 case 'A':
8100 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8101 break;
8102
8103 case 'B':
8104 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8105 break;
8106 }
8107 break;
8108
97d87491
RS
8109 case '+':
8110 switch (args[1])
8111 {
97d87491
RS
8112 case 'i':
8113 *offset_reloc = BFD_RELOC_MIPS_JMP;
8114 break;
7361da2c
AB
8115
8116 case '\'':
8117 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8118 break;
8119
8120 case '\"':
8121 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8122 break;
97d87491
RS
8123 }
8124 break;
8125
97d87491 8126 case 'I':
1a00e612
RS
8127 if (!match_const_int (&arg, &imm_expr.X_add_number))
8128 return FALSE;
8129 imm_expr.X_op = O_constant;
bad1aba3 8130 if (GPR_SIZE == 32)
97d87491
RS
8131 normalize_constant_expr (&imm_expr);
8132 continue;
8133
8134 case 'A':
8135 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8136 {
8137 /* Assume that the offset has been elided and that what
8138 we saw was a base register. The match will fail later
8139 if that assumption turns out to be wrong. */
8140 offset_expr.X_op = O_constant;
8141 offset_expr.X_add_number = 0;
8142 }
97d87491 8143 else
1a00e612
RS
8144 {
8145 if (!match_expression (&arg, &offset_expr, offset_reloc))
8146 return FALSE;
8147 normalize_address_expr (&offset_expr);
8148 }
97d87491
RS
8149 continue;
8150
8151 case 'F':
8152 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8153 8, TRUE))
1a00e612 8154 return FALSE;
97d87491
RS
8155 continue;
8156
8157 case 'L':
8158 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8159 8, FALSE))
1a00e612 8160 return FALSE;
97d87491
RS
8161 continue;
8162
8163 case 'f':
8164 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8165 4, TRUE))
1a00e612 8166 return FALSE;
97d87491
RS
8167 continue;
8168
8169 case 'l':
8170 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8171 4, FALSE))
1a00e612 8172 return FALSE;
97d87491
RS
8173 continue;
8174
97d87491
RS
8175 case 'p':
8176 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8177 break;
8178
8179 case 'a':
8180 *offset_reloc = BFD_RELOC_MIPS_JMP;
8181 break;
8182
8183 case 'm':
8184 gas_assert (mips_opts.micromips);
8185 c = args[1];
8186 switch (c)
8187 {
8188 case 'D':
8189 case 'E':
8190 if (!forced_insn_length)
8191 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8192 else if (c == 'D')
8193 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8194 else
8195 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8196 break;
8197 }
8198 break;
8199 }
8200
8201 operand = (mips_opts.micromips
8202 ? decode_micromips_operand (args)
8203 : decode_mips_operand (args));
8204 if (!operand)
8205 abort ();
8206
8207 /* Skip prefixes. */
7361da2c 8208 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8209 args++;
8210
8211 if (mips_optional_operand_p (operand)
8212 && args[1] == ','
8213 && (arg.token[0].type != OT_REG
8214 || arg.token[1].type == OT_END))
8215 {
8216 /* Assume that the register has been elided and is the
8217 same as the first operand. */
8218 arg.token = tokens;
8219 arg.argnum = 1;
8220 }
8221
8222 if (!match_operand (&arg, operand))
8223 return FALSE;
8224 }
8225}
8226
8227/* Like match_insn, but for MIPS16. */
8228
8229static bfd_boolean
8230match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8231 struct mips_operand_token *tokens)
97d87491
RS
8232{
8233 const char *args;
8234 const struct mips_operand *operand;
8235 const struct mips_operand *ext_operand;
82d808ed 8236 bfd_boolean pcrel = FALSE;
7fd53920 8237 int required_insn_length;
97d87491
RS
8238 struct mips_arg_info arg;
8239 int relax_char;
8240
7fd53920
MR
8241 if (forced_insn_length)
8242 required_insn_length = forced_insn_length;
8243 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8244 required_insn_length = 2;
8245 else
8246 required_insn_length = 0;
8247
97d87491
RS
8248 create_insn (insn, opcode);
8249 imm_expr.X_op = O_absent;
97d87491
RS
8250 offset_expr.X_op = O_absent;
8251 offset_reloc[0] = BFD_RELOC_UNUSED;
8252 offset_reloc[1] = BFD_RELOC_UNUSED;
8253 offset_reloc[2] = BFD_RELOC_UNUSED;
8254 relax_char = 0;
8255
8256 memset (&arg, 0, sizeof (arg));
8257 arg.insn = insn;
8258 arg.token = tokens;
8259 arg.argnum = 1;
8260 arg.last_regno = ILLEGAL_REG;
8261 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8262 relax_char = 0;
8263 for (args = opcode->args;; ++args)
8264 {
8265 int c;
8266
8267 if (arg.token->type == OT_END)
8268 {
8269 offsetT value;
8270
8271 /* Handle unary instructions in which only one operand is given.
8272 The source is then the same as the destination. */
8273 if (arg.opnum == 1 && *args == ',')
8274 {
8275 operand = decode_mips16_operand (args[1], FALSE);
8276 if (operand && mips_optional_operand_p (operand))
8277 {
8278 arg.token = tokens;
8279 arg.argnum = 1;
8280 continue;
8281 }
8282 }
8283
8284 /* Fail the match if there were too few operands. */
8285 if (*args)
8286 return FALSE;
8287
8288 /* Successful match. Stuff the immediate value in now, if
8289 we can. */
e3de51ce 8290 clear_insn_error ();
97d87491
RS
8291 if (opcode->pinfo == INSN_MACRO)
8292 {
8293 gas_assert (relax_char == 0 || relax_char == 'p');
8294 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8295 }
8296 else if (relax_char
8297 && offset_expr.X_op == O_constant
82d808ed 8298 && !pcrel
97d87491
RS
8299 && calculate_reloc (*offset_reloc,
8300 offset_expr.X_add_number,
8301 &value))
8302 {
8303 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8304 required_insn_length, &insn->insn_opcode);
97d87491
RS
8305 offset_expr.X_op = O_absent;
8306 *offset_reloc = BFD_RELOC_UNUSED;
8307 }
8308 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8309 {
7fd53920 8310 if (required_insn_length == 2)
e3de51ce 8311 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8312 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8313 {
8314 forced_insn_length = 4;
8315 insn->insn_opcode |= MIPS16_EXTEND;
8316 }
97d87491
RS
8317 }
8318 else if (relax_char)
8319 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8320
8321 check_completed_insn (&arg);
8322 return TRUE;
8323 }
8324
8325 /* Fail the match if the line has too many operands. */
8326 if (*args == 0)
8327 return FALSE;
8328
8329 /* Handle characters that need to match exactly. */
8330 if (*args == '(' || *args == ')' || *args == ',')
8331 {
8332 if (match_char (&arg, *args))
8333 continue;
8334 return FALSE;
8335 }
8336
8337 arg.opnum += 1;
8338 c = *args;
8339 switch (c)
8340 {
8341 case 'p':
8342 case 'q':
8343 case 'A':
8344 case 'B':
8345 case 'E':
25499ac7
MR
8346 case 'V':
8347 case 'u':
97d87491
RS
8348 relax_char = c;
8349 break;
8350
8351 case 'I':
1a00e612
RS
8352 if (!match_const_int (&arg, &imm_expr.X_add_number))
8353 return FALSE;
8354 imm_expr.X_op = O_constant;
bad1aba3 8355 if (GPR_SIZE == 32)
97d87491
RS
8356 normalize_constant_expr (&imm_expr);
8357 continue;
8358
8359 case 'a':
8360 case 'i':
8361 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8362 break;
8363 }
8364
7fd53920 8365 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8366 if (!operand)
8367 abort ();
8368
82d808ed
MR
8369 if (operand->type == OP_PCREL)
8370 pcrel = TRUE;
8371 else
97d87491
RS
8372 {
8373 ext_operand = decode_mips16_operand (c, TRUE);
8374 if (operand != ext_operand)
8375 {
8376 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8377 {
8378 offset_expr.X_op = O_constant;
8379 offset_expr.X_add_number = 0;
8380 relax_char = c;
8381 continue;
8382 }
8383
1a7bf198 8384 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8385 return FALSE;
8386
8387 /* '8' is used for SLTI(U) and has traditionally not
8388 been allowed to take relocation operators. */
8389 if (offset_reloc[0] != BFD_RELOC_UNUSED
8390 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8391 {
8392 match_not_constant (&arg);
8393 return FALSE;
8394 }
97d87491 8395
c96425c5
MR
8396 if (offset_expr.X_op == O_big)
8397 {
8398 match_out_of_range (&arg);
8399 return FALSE;
8400 }
8401
97d87491
RS
8402 relax_char = c;
8403 continue;
8404 }
8405 }
8406
8407 if (mips_optional_operand_p (operand)
8408 && args[1] == ','
8409 && (arg.token[0].type != OT_REG
8410 || arg.token[1].type == OT_END))
8411 {
8412 /* Assume that the register has been elided and is the
8413 same as the first operand. */
8414 arg.token = tokens;
8415 arg.argnum = 1;
8416 }
8417
8418 if (!match_operand (&arg, operand))
8419 return FALSE;
8420 }
8421}
8422
60f20e8b
RS
8423/* Record that the current instruction is invalid for the current ISA. */
8424
8425static void
8426match_invalid_for_isa (void)
8427{
8428 set_insn_error_ss
1661c76c 8429 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8430 mips_cpu_info_from_arch (mips_opts.arch)->name,
8431 mips_cpu_info_from_isa (mips_opts.isa)->name);
8432}
8433
8434/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8435 Return true if a definite match or failure was found, storing any match
8436 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8437 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8438 tried and failed to match under normal conditions and now want to try a
8439 more relaxed match. */
8440
8441static bfd_boolean
8442match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8443 const struct mips_opcode *past, struct mips_operand_token *tokens,
8444 int opcode_extra, bfd_boolean lax_match)
8445{
8446 const struct mips_opcode *opcode;
8447 const struct mips_opcode *invalid_delay_slot;
8448 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8449
8450 /* Search for a match, ignoring alternatives that don't satisfy the
8451 current ISA or forced_length. */
8452 invalid_delay_slot = 0;
8453 seen_valid_for_isa = FALSE;
8454 seen_valid_for_size = FALSE;
8455 opcode = first;
8456 do
8457 {
8458 gas_assert (strcmp (opcode->name, first->name) == 0);
8459 if (is_opcode_valid (opcode))
8460 {
8461 seen_valid_for_isa = TRUE;
8462 if (is_size_valid (opcode))
8463 {
8464 bfd_boolean delay_slot_ok;
8465
8466 seen_valid_for_size = TRUE;
8467 delay_slot_ok = is_delay_slot_valid (opcode);
8468 if (match_insn (insn, opcode, tokens, opcode_extra,
8469 lax_match, delay_slot_ok))
8470 {
8471 if (!delay_slot_ok)
8472 {
8473 if (!invalid_delay_slot)
8474 invalid_delay_slot = opcode;
8475 }
8476 else
8477 return TRUE;
8478 }
8479 }
8480 }
8481 ++opcode;
8482 }
8483 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8484
8485 /* If the only matches we found had the wrong length for the delay slot,
8486 pick the first such match. We'll issue an appropriate warning later. */
8487 if (invalid_delay_slot)
8488 {
8489 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8490 lax_match, TRUE))
8491 return TRUE;
8492 abort ();
8493 }
8494
8495 /* Handle the case where we didn't try to match an instruction because
8496 all the alternatives were incompatible with the current ISA. */
8497 if (!seen_valid_for_isa)
8498 {
8499 match_invalid_for_isa ();
8500 return TRUE;
8501 }
8502
8503 /* Handle the case where we didn't try to match an instruction because
8504 all the alternatives were of the wrong size. */
8505 if (!seen_valid_for_size)
8506 {
8507 if (mips_opts.insn32)
1661c76c 8508 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8509 else
8510 set_insn_error_i
1661c76c 8511 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8512 8 * forced_insn_length);
8513 return TRUE;
8514 }
8515
8516 return FALSE;
8517}
8518
8519/* Like match_insns, but for MIPS16. */
8520
8521static bfd_boolean
8522match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8523 struct mips_operand_token *tokens)
8524{
8525 const struct mips_opcode *opcode;
8526 bfd_boolean seen_valid_for_isa;
7fd53920 8527 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8528
8529 /* Search for a match, ignoring alternatives that don't satisfy the
8530 current ISA. There are no separate entries for extended forms so
8531 we deal with forced_length later. */
8532 seen_valid_for_isa = FALSE;
7fd53920 8533 seen_valid_for_size = FALSE;
60f20e8b
RS
8534 opcode = first;
8535 do
8536 {
8537 gas_assert (strcmp (opcode->name, first->name) == 0);
8538 if (is_opcode_valid_16 (opcode))
8539 {
8540 seen_valid_for_isa = TRUE;
7fd53920
MR
8541 if (is_size_valid_16 (opcode))
8542 {
8543 seen_valid_for_size = TRUE;
8544 if (match_mips16_insn (insn, opcode, tokens))
8545 return TRUE;
8546 }
60f20e8b
RS
8547 }
8548 ++opcode;
8549 }
8550 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8551 && strcmp (opcode->name, first->name) == 0);
8552
8553 /* Handle the case where we didn't try to match an instruction because
8554 all the alternatives were incompatible with the current ISA. */
8555 if (!seen_valid_for_isa)
8556 {
8557 match_invalid_for_isa ();
8558 return TRUE;
8559 }
8560
7fd53920
MR
8561 /* Handle the case where we didn't try to match an instruction because
8562 all the alternatives were of the wrong size. */
8563 if (!seen_valid_for_size)
8564 {
8565 if (forced_insn_length == 2)
8566 set_insn_error
8567 (0, _("unrecognized unextended version of MIPS16 opcode"));
8568 else
8569 set_insn_error
8570 (0, _("unrecognized extended version of MIPS16 opcode"));
8571 return TRUE;
8572 }
8573
60f20e8b
RS
8574 return FALSE;
8575}
8576
584892a6
RS
8577/* Set up global variables for the start of a new macro. */
8578
8579static void
8580macro_start (void)
8581{
8582 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8583 memset (&mips_macro_warning.first_insn_sizes, 0,
8584 sizeof (mips_macro_warning.first_insn_sizes));
8585 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8586 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8587 && delayed_branch_p (&history[0]));
7bd374a4
MR
8588 if (history[0].frag
8589 && history[0].frag->fr_type == rs_machine_dependent
8590 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8591 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8592 mips_macro_warning.delay_slot_length = 0;
8593 else
8594 switch (history[0].insn_mo->pinfo2
8595 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8596 {
8597 case INSN2_BRANCH_DELAY_32BIT:
8598 mips_macro_warning.delay_slot_length = 4;
8599 break;
8600 case INSN2_BRANCH_DELAY_16BIT:
8601 mips_macro_warning.delay_slot_length = 2;
8602 break;
8603 default:
8604 mips_macro_warning.delay_slot_length = 0;
8605 break;
8606 }
df58fc94 8607 mips_macro_warning.first_frag = NULL;
584892a6
RS
8608}
8609
df58fc94
RS
8610/* Given that a macro is longer than one instruction or of the wrong size,
8611 return the appropriate warning for it. Return null if no warning is
8612 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8613 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8614 and RELAX_NOMACRO. */
584892a6
RS
8615
8616static const char *
8617macro_warning (relax_substateT subtype)
8618{
8619 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8620 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8621 " in a branch delay slot");
8622 else if (subtype & RELAX_NOMACRO)
1661c76c 8623 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8624 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8625 | RELAX_DELAY_SLOT_SIZE_SECOND))
8626 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8627 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8628 " in a 16-bit branch delay slot")
1661c76c 8629 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8630 " in a 32-bit branch delay slot"));
584892a6
RS
8631 else
8632 return 0;
8633}
8634
8635/* Finish up a macro. Emit warnings as appropriate. */
8636
8637static void
8638macro_end (void)
8639{
df58fc94
RS
8640 /* Relaxation warning flags. */
8641 relax_substateT subtype = 0;
8642
8643 /* Check delay slot size requirements. */
8644 if (mips_macro_warning.delay_slot_length == 2)
8645 subtype |= RELAX_DELAY_SLOT_16BIT;
8646 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8647 {
df58fc94
RS
8648 if (mips_macro_warning.delay_slot_length
8649 != mips_macro_warning.first_insn_sizes[0])
8650 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8651 if (mips_macro_warning.delay_slot_length
8652 != mips_macro_warning.first_insn_sizes[1])
8653 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8654 }
584892a6 8655
df58fc94
RS
8656 /* Check instruction count requirements. */
8657 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8658 {
8659 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8660 subtype |= RELAX_SECOND_LONGER;
8661 if (mips_opts.warn_about_macros)
8662 subtype |= RELAX_NOMACRO;
8663 if (mips_macro_warning.delay_slot_p)
8664 subtype |= RELAX_DELAY_SLOT;
df58fc94 8665 }
584892a6 8666
df58fc94
RS
8667 /* If both alternatives fail to fill a delay slot correctly,
8668 emit the warning now. */
8669 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8670 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8671 {
8672 relax_substateT s;
8673 const char *msg;
8674
8675 s = subtype & (RELAX_DELAY_SLOT_16BIT
8676 | RELAX_DELAY_SLOT_SIZE_FIRST
8677 | RELAX_DELAY_SLOT_SIZE_SECOND);
8678 msg = macro_warning (s);
8679 if (msg != NULL)
8680 as_warn ("%s", msg);
8681 subtype &= ~s;
8682 }
8683
8684 /* If both implementations are longer than 1 instruction, then emit the
8685 warning now. */
8686 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8687 {
8688 relax_substateT s;
8689 const char *msg;
8690
8691 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8692 msg = macro_warning (s);
8693 if (msg != NULL)
8694 as_warn ("%s", msg);
8695 subtype &= ~s;
584892a6 8696 }
df58fc94
RS
8697
8698 /* If any flags still set, then one implementation might need a warning
8699 and the other either will need one of a different kind or none at all.
8700 Pass any remaining flags over to relaxation. */
8701 if (mips_macro_warning.first_frag != NULL)
8702 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8703}
8704
df58fc94
RS
8705/* Instruction operand formats used in macros that vary between
8706 standard MIPS and microMIPS code. */
8707
833794fc 8708static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8709static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8710static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8711static const char * const lui_fmt[2] = { "t,u", "s,u" };
8712static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8713static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8714static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8715static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8716
833794fc 8717#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8718#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8719 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8720#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8721#define LUI_FMT (lui_fmt[mips_opts.micromips])
8722#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8723#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8724 : mem12_fmt[mips_opts.micromips])
833794fc 8725#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8726#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8727#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8728
6e1304d8
RS
8729/* Read a macro's relocation codes from *ARGS and store them in *R.
8730 The first argument in *ARGS will be either the code for a single
8731 relocation or -1 followed by the three codes that make up a
8732 composite relocation. */
8733
8734static void
8735macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8736{
8737 int i, next;
8738
8739 next = va_arg (*args, int);
8740 if (next >= 0)
8741 r[0] = (bfd_reloc_code_real_type) next;
8742 else
f2ae14a1
RS
8743 {
8744 for (i = 0; i < 3; i++)
8745 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8746 /* This function is only used for 16-bit relocation fields.
8747 To make the macro code simpler, treat an unrelocated value
8748 in the same way as BFD_RELOC_LO16. */
8749 if (r[0] == BFD_RELOC_UNUSED)
8750 r[0] = BFD_RELOC_LO16;
8751 }
6e1304d8
RS
8752}
8753
252b5132
RH
8754/* Build an instruction created by a macro expansion. This is passed
8755 a pointer to the count of instructions created so far, an
8756 expression, the name of the instruction to build, an operand format
8757 string, and corresponding arguments. */
8758
252b5132 8759static void
67c0d1eb 8760macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8761{
df58fc94 8762 const struct mips_opcode *mo = NULL;
f6688943 8763 bfd_reloc_code_real_type r[3];
df58fc94 8764 const struct mips_opcode *amo;
e077a1c8 8765 const struct mips_operand *operand;
df58fc94
RS
8766 struct hash_control *hash;
8767 struct mips_cl_insn insn;
252b5132 8768 va_list args;
e077a1c8 8769 unsigned int uval;
252b5132 8770
252b5132 8771 va_start (args, fmt);
252b5132 8772
252b5132
RH
8773 if (mips_opts.mips16)
8774 {
03ea81db 8775 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8776 va_end (args);
8777 return;
8778 }
8779
f6688943
TS
8780 r[0] = BFD_RELOC_UNUSED;
8781 r[1] = BFD_RELOC_UNUSED;
8782 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8783 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8784 amo = (struct mips_opcode *) hash_find (hash, name);
8785 gas_assert (amo);
8786 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8787
df58fc94 8788 do
8b082fb1
TS
8789 {
8790 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8791 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8792 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8793 slot instruction length requirement (if any) of the previous
8794 instruction. While doing this we record the first instruction
8795 seen that matches all the other conditions and use it anyway
8796 if the requirement cannot be met; we will issue an appropriate
8797 warning later on. */
8798 if (strcmp (fmt, amo->args) == 0
8799 && amo->pinfo != INSN_MACRO
8800 && is_opcode_valid (amo)
8801 && is_size_valid (amo))
8802 {
8803 if (is_delay_slot_valid (amo))
8804 {
8805 mo = amo;
8806 break;
8807 }
8808 else if (!mo)
8809 mo = amo;
8810 }
8b082fb1 8811
df58fc94
RS
8812 ++amo;
8813 gas_assert (amo->name);
252b5132 8814 }
df58fc94 8815 while (strcmp (name, amo->name) == 0);
252b5132 8816
df58fc94 8817 gas_assert (mo);
1e915849 8818 create_insn (&insn, mo);
e077a1c8 8819 for (; *fmt; ++fmt)
252b5132 8820 {
e077a1c8 8821 switch (*fmt)
252b5132 8822 {
252b5132
RH
8823 case ',':
8824 case '(':
8825 case ')':
252b5132 8826 case 'z':
e077a1c8 8827 break;
252b5132
RH
8828
8829 case 'i':
8830 case 'j':
6e1304d8 8831 macro_read_relocs (&args, r);
9c2799c2 8832 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8833 || *r == BFD_RELOC_MIPS_HIGHER
8834 || *r == BFD_RELOC_HI16_S
8835 || *r == BFD_RELOC_LO16
8836 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 8837 break;
e391c024
RS
8838
8839 case 'o':
8840 macro_read_relocs (&args, r);
e077a1c8 8841 break;
252b5132
RH
8842
8843 case 'u':
6e1304d8 8844 macro_read_relocs (&args, r);
9c2799c2 8845 gas_assert (ep != NULL
90ecf173
MR
8846 && (ep->X_op == O_constant
8847 || (ep->X_op == O_symbol
8848 && (*r == BFD_RELOC_MIPS_HIGHEST
8849 || *r == BFD_RELOC_HI16_S
8850 || *r == BFD_RELOC_HI16
8851 || *r == BFD_RELOC_GPREL16
8852 || *r == BFD_RELOC_MIPS_GOT_HI16
8853 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8854 break;
252b5132
RH
8855
8856 case 'p':
9c2799c2 8857 gas_assert (ep != NULL);
bad36eac 8858
252b5132
RH
8859 /*
8860 * This allows macro() to pass an immediate expression for
8861 * creating short branches without creating a symbol.
bad36eac
DJ
8862 *
8863 * We don't allow branch relaxation for these branches, as
8864 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8865 */
8866 if (ep->X_op == O_constant)
8867 {
df58fc94
RS
8868 /* For microMIPS we always use relocations for branches.
8869 So we should not resolve immediate values. */
8870 gas_assert (!mips_opts.micromips);
8871
bad36eac
DJ
8872 if ((ep->X_add_number & 3) != 0)
8873 as_bad (_("branch to misaligned address (0x%lx)"),
8874 (unsigned long) ep->X_add_number);
8875 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8876 as_bad (_("branch address range overflow (0x%lx)"),
8877 (unsigned long) ep->X_add_number);
252b5132
RH
8878 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8879 ep = NULL;
8880 }
8881 else
0b25d3e6 8882 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8883 break;
252b5132
RH
8884
8885 case 'a':
9c2799c2 8886 gas_assert (ep != NULL);
f6688943 8887 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8888 break;
d43b4baf 8889
252b5132 8890 default:
e077a1c8
RS
8891 operand = (mips_opts.micromips
8892 ? decode_micromips_operand (fmt)
8893 : decode_mips_operand (fmt));
8894 if (!operand)
8895 abort ();
8896
8897 uval = va_arg (args, int);
8898 if (operand->type == OP_CLO_CLZ_DEST)
8899 uval |= (uval << 5);
8900 insn_insert_operand (&insn, operand, uval);
8901
7361da2c 8902 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8903 ++fmt;
8904 break;
252b5132 8905 }
252b5132
RH
8906 }
8907 va_end (args);
9c2799c2 8908 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8909
df58fc94 8910 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8911}
8912
8913static void
67c0d1eb 8914mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8915 va_list *args)
252b5132 8916{
1e915849 8917 struct mips_opcode *mo;
252b5132 8918 struct mips_cl_insn insn;
e077a1c8 8919 const struct mips_operand *operand;
f6688943
TS
8920 bfd_reloc_code_real_type r[3]
8921 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8922
1e915849 8923 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8924 gas_assert (mo);
8925 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8926
1e915849 8927 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8928 {
1e915849 8929 ++mo;
9c2799c2
NC
8930 gas_assert (mo->name);
8931 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8932 }
8933
1e915849 8934 create_insn (&insn, mo);
e077a1c8 8935 for (; *fmt; ++fmt)
252b5132
RH
8936 {
8937 int c;
8938
e077a1c8 8939 c = *fmt;
252b5132
RH
8940 switch (c)
8941 {
252b5132
RH
8942 case ',':
8943 case '(':
8944 case ')':
e077a1c8 8945 break;
252b5132 8946
d8722d76 8947 case '.':
252b5132
RH
8948 case 'S':
8949 case 'P':
8950 case 'R':
e077a1c8 8951 break;
252b5132
RH
8952
8953 case '<':
252b5132 8954 case '5':
d8722d76 8955 case 'F':
252b5132
RH
8956 case 'H':
8957 case 'W':
8958 case 'D':
8959 case 'j':
8960 case '8':
8961 case 'V':
8962 case 'C':
8963 case 'U':
8964 case 'k':
8965 case 'K':
8966 case 'p':
8967 case 'q':
8968 {
b886a2ab
RS
8969 offsetT value;
8970
9c2799c2 8971 gas_assert (ep != NULL);
252b5132
RH
8972
8973 if (ep->X_op != O_constant)
874e8986 8974 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8975 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8976 {
b886a2ab 8977 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8978 ep = NULL;
f6688943 8979 *r = BFD_RELOC_UNUSED;
252b5132
RH
8980 }
8981 }
e077a1c8 8982 break;
252b5132 8983
e077a1c8
RS
8984 default:
8985 operand = decode_mips16_operand (c, FALSE);
8986 if (!operand)
8987 abort ();
252b5132 8988
4a06e5a2 8989 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8990 break;
8991 }
252b5132
RH
8992 }
8993
9c2799c2 8994 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8995
df58fc94 8996 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8997}
8998
438c16b8
TS
8999/*
9000 * Generate a "jalr" instruction with a relocation hint to the called
9001 * function. This occurs in NewABI PIC code.
9002 */
9003static void
df58fc94 9004macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9005{
df58fc94
RS
9006 static const bfd_reloc_code_real_type jalr_relocs[2]
9007 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9008 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9009 const char *jalr;
685736be 9010 char *f = NULL;
b34976b6 9011
1180b5a4 9012 if (MIPS_JALR_HINT_P (ep))
f21f8242 9013 {
cc3d92a5 9014 frag_grow (8);
f21f8242
AO
9015 f = frag_more (0);
9016 }
2906b037 9017 if (mips_opts.micromips)
df58fc94 9018 {
833794fc
MR
9019 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9020 ? "jalr" : "jalrs");
e64af278 9021 if (MIPS_JALR_HINT_P (ep)
833794fc 9022 || mips_opts.insn32
e64af278 9023 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9024 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9025 else
9026 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9027 }
2906b037
MR
9028 else
9029 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9030 if (MIPS_JALR_HINT_P (ep))
df58fc94 9031 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9032}
9033
252b5132
RH
9034/*
9035 * Generate a "lui" instruction.
9036 */
9037static void
67c0d1eb 9038macro_build_lui (expressionS *ep, int regnum)
252b5132 9039{
9c2799c2 9040 gas_assert (! mips_opts.mips16);
252b5132 9041
df58fc94 9042 if (ep->X_op != O_constant)
252b5132 9043 {
9c2799c2 9044 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9045 /* _gp_disp is a special case, used from s_cpload.
9046 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9047 gas_assert (mips_pic == NO_PIC
78e1bb40 9048 || (! HAVE_NEWABI
aa6975fb
ILT
9049 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9050 || (! mips_in_shared
bbe506e8
TS
9051 && strcmp (S_GET_NAME (ep->X_add_symbol),
9052 "__gnu_local_gp") == 0));
252b5132
RH
9053 }
9054
df58fc94 9055 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9056}
9057
885add95
CD
9058/* Generate a sequence of instructions to do a load or store from a constant
9059 offset off of a base register (breg) into/from a target register (treg),
9060 using AT if necessary. */
9061static void
67c0d1eb
RS
9062macro_build_ldst_constoffset (expressionS *ep, const char *op,
9063 int treg, int breg, int dbl)
885add95 9064{
9c2799c2 9065 gas_assert (ep->X_op == O_constant);
885add95 9066
256ab948 9067 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9068 if (!dbl)
9069 normalize_constant_expr (ep);
256ab948 9070
67c1ffbe 9071 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9072 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9073 as_warn (_("operand overflow"));
9074
9075 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9076 {
9077 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9078 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9079 }
9080 else
9081 {
9082 /* 32-bit offset, need multiple instructions and AT, like:
9083 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9084 addu $tempreg,$tempreg,$breg
9085 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9086 to handle the complete offset. */
67c0d1eb
RS
9087 macro_build_lui (ep, AT);
9088 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9089 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9090
741fe287 9091 if (!mips_opts.at)
1661c76c 9092 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9093 }
9094}
9095
252b5132
RH
9096/* set_at()
9097 * Generates code to set the $at register to true (one)
9098 * if reg is less than the immediate expression.
9099 */
9100static void
67c0d1eb 9101set_at (int reg, int unsignedp)
252b5132 9102{
b0e6f033 9103 if (imm_expr.X_add_number >= -0x8000
252b5132 9104 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9105 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9106 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9107 else
9108 {
bad1aba3 9109 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9110 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9111 }
9112}
9113
252b5132
RH
9114/* Count the leading zeroes by performing a binary chop. This is a
9115 bulky bit of source, but performance is a LOT better for the
9116 majority of values than a simple loop to count the bits:
9117 for (lcnt = 0; (lcnt < 32); lcnt++)
9118 if ((v) & (1 << (31 - lcnt)))
9119 break;
9120 However it is not code size friendly, and the gain will drop a bit
9121 on certain cached systems.
9122*/
9123#define COUNT_TOP_ZEROES(v) \
9124 (((v) & ~0xffff) == 0 \
9125 ? ((v) & ~0xff) == 0 \
9126 ? ((v) & ~0xf) == 0 \
9127 ? ((v) & ~0x3) == 0 \
9128 ? ((v) & ~0x1) == 0 \
9129 ? !(v) \
9130 ? 32 \
9131 : 31 \
9132 : 30 \
9133 : ((v) & ~0x7) == 0 \
9134 ? 29 \
9135 : 28 \
9136 : ((v) & ~0x3f) == 0 \
9137 ? ((v) & ~0x1f) == 0 \
9138 ? 27 \
9139 : 26 \
9140 : ((v) & ~0x7f) == 0 \
9141 ? 25 \
9142 : 24 \
9143 : ((v) & ~0xfff) == 0 \
9144 ? ((v) & ~0x3ff) == 0 \
9145 ? ((v) & ~0x1ff) == 0 \
9146 ? 23 \
9147 : 22 \
9148 : ((v) & ~0x7ff) == 0 \
9149 ? 21 \
9150 : 20 \
9151 : ((v) & ~0x3fff) == 0 \
9152 ? ((v) & ~0x1fff) == 0 \
9153 ? 19 \
9154 : 18 \
9155 : ((v) & ~0x7fff) == 0 \
9156 ? 17 \
9157 : 16 \
9158 : ((v) & ~0xffffff) == 0 \
9159 ? ((v) & ~0xfffff) == 0 \
9160 ? ((v) & ~0x3ffff) == 0 \
9161 ? ((v) & ~0x1ffff) == 0 \
9162 ? 15 \
9163 : 14 \
9164 : ((v) & ~0x7ffff) == 0 \
9165 ? 13 \
9166 : 12 \
9167 : ((v) & ~0x3fffff) == 0 \
9168 ? ((v) & ~0x1fffff) == 0 \
9169 ? 11 \
9170 : 10 \
9171 : ((v) & ~0x7fffff) == 0 \
9172 ? 9 \
9173 : 8 \
9174 : ((v) & ~0xfffffff) == 0 \
9175 ? ((v) & ~0x3ffffff) == 0 \
9176 ? ((v) & ~0x1ffffff) == 0 \
9177 ? 7 \
9178 : 6 \
9179 : ((v) & ~0x7ffffff) == 0 \
9180 ? 5 \
9181 : 4 \
9182 : ((v) & ~0x3fffffff) == 0 \
9183 ? ((v) & ~0x1fffffff) == 0 \
9184 ? 3 \
9185 : 2 \
9186 : ((v) & ~0x7fffffff) == 0 \
9187 ? 1 \
9188 : 0)
9189
9190/* load_register()
67c1ffbe 9191 * This routine generates the least number of instructions necessary to load
252b5132
RH
9192 * an absolute expression value into a register.
9193 */
9194static void
67c0d1eb 9195load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9196{
9197 int freg;
9198 expressionS hi32, lo32;
9199
9200 if (ep->X_op != O_big)
9201 {
9c2799c2 9202 gas_assert (ep->X_op == O_constant);
256ab948
TS
9203
9204 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9205 if (!dbl)
9206 normalize_constant_expr (ep);
256ab948
TS
9207
9208 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9209 {
9210 /* We can handle 16 bit signed values with an addiu to
9211 $zero. No need to ever use daddiu here, since $zero and
9212 the result are always correct in 32 bit mode. */
67c0d1eb 9213 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9214 return;
9215 }
9216 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9217 {
9218 /* We can handle 16 bit unsigned values with an ori to
9219 $zero. */
67c0d1eb 9220 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9221 return;
9222 }
256ab948 9223 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9224 {
9225 /* 32 bit values require an lui. */
df58fc94 9226 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9227 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9228 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9229 return;
9230 }
9231 }
9232
9233 /* The value is larger than 32 bits. */
9234
bad1aba3 9235 if (!dbl || GPR_SIZE == 32)
252b5132 9236 {
55e08f71
NC
9237 char value[32];
9238
9239 sprintf_vma (value, ep->X_add_number);
1661c76c 9240 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9241 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9242 return;
9243 }
9244
9245 if (ep->X_op != O_big)
9246 {
9247 hi32 = *ep;
9248 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9249 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9250 hi32.X_add_number &= 0xffffffff;
9251 lo32 = *ep;
9252 lo32.X_add_number &= 0xffffffff;
9253 }
9254 else
9255 {
9c2799c2 9256 gas_assert (ep->X_add_number > 2);
252b5132
RH
9257 if (ep->X_add_number == 3)
9258 generic_bignum[3] = 0;
9259 else if (ep->X_add_number > 4)
1661c76c 9260 as_bad (_("number larger than 64 bits"));
252b5132
RH
9261 lo32.X_op = O_constant;
9262 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9263 hi32.X_op = O_constant;
9264 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9265 }
9266
9267 if (hi32.X_add_number == 0)
9268 freg = 0;
9269 else
9270 {
9271 int shift, bit;
9272 unsigned long hi, lo;
9273
956cd1d6 9274 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9275 {
9276 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9277 {
67c0d1eb 9278 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9279 return;
9280 }
9281 if (lo32.X_add_number & 0x80000000)
9282 {
df58fc94 9283 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9284 if (lo32.X_add_number & 0xffff)
67c0d1eb 9285 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9286 return;
9287 }
9288 }
252b5132
RH
9289
9290 /* Check for 16bit shifted constant. We know that hi32 is
9291 non-zero, so start the mask on the first bit of the hi32
9292 value. */
9293 shift = 17;
9294 do
beae10d5
KH
9295 {
9296 unsigned long himask, lomask;
9297
9298 if (shift < 32)
9299 {
9300 himask = 0xffff >> (32 - shift);
9301 lomask = (0xffff << shift) & 0xffffffff;
9302 }
9303 else
9304 {
9305 himask = 0xffff << (shift - 32);
9306 lomask = 0;
9307 }
9308 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9309 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9310 {
9311 expressionS tmp;
9312
9313 tmp.X_op = O_constant;
9314 if (shift < 32)
9315 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9316 | (lo32.X_add_number >> shift));
9317 else
9318 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9319 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9320 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9321 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9322 return;
9323 }
f9419b05 9324 ++shift;
beae10d5
KH
9325 }
9326 while (shift <= (64 - 16));
252b5132
RH
9327
9328 /* Find the bit number of the lowest one bit, and store the
9329 shifted value in hi/lo. */
9330 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9331 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9332 if (lo != 0)
9333 {
9334 bit = 0;
9335 while ((lo & 1) == 0)
9336 {
9337 lo >>= 1;
9338 ++bit;
9339 }
9340 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9341 hi >>= bit;
9342 }
9343 else
9344 {
9345 bit = 32;
9346 while ((hi & 1) == 0)
9347 {
9348 hi >>= 1;
9349 ++bit;
9350 }
9351 lo = hi;
9352 hi = 0;
9353 }
9354
9355 /* Optimize if the shifted value is a (power of 2) - 1. */
9356 if ((hi == 0 && ((lo + 1) & lo) == 0)
9357 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9358 {
9359 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9360 if (shift != 0)
beae10d5 9361 {
252b5132
RH
9362 expressionS tmp;
9363
9364 /* This instruction will set the register to be all
9365 ones. */
beae10d5
KH
9366 tmp.X_op = O_constant;
9367 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9368 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9369 if (bit != 0)
9370 {
9371 bit += shift;
df58fc94 9372 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9373 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9374 }
df58fc94 9375 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9376 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9377 return;
9378 }
9379 }
252b5132
RH
9380
9381 /* Sign extend hi32 before calling load_register, because we can
9382 generally get better code when we load a sign extended value. */
9383 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9384 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9385 load_register (reg, &hi32, 0);
252b5132
RH
9386 freg = reg;
9387 }
9388 if ((lo32.X_add_number & 0xffff0000) == 0)
9389 {
9390 if (freg != 0)
9391 {
df58fc94 9392 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9393 freg = reg;
9394 }
9395 }
9396 else
9397 {
9398 expressionS mid16;
9399
956cd1d6 9400 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9401 {
df58fc94
RS
9402 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9403 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9404 return;
9405 }
252b5132
RH
9406
9407 if (freg != 0)
9408 {
df58fc94 9409 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9410 freg = reg;
9411 }
9412 mid16 = lo32;
9413 mid16.X_add_number >>= 16;
67c0d1eb 9414 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9415 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9416 freg = reg;
9417 }
9418 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9419 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9420}
9421
269137b2
TS
9422static inline void
9423load_delay_nop (void)
9424{
9425 if (!gpr_interlocks)
9426 macro_build (NULL, "nop", "");
9427}
9428
252b5132
RH
9429/* Load an address into a register. */
9430
9431static void
67c0d1eb 9432load_address (int reg, expressionS *ep, int *used_at)
252b5132 9433{
252b5132
RH
9434 if (ep->X_op != O_constant
9435 && ep->X_op != O_symbol)
9436 {
9437 as_bad (_("expression too complex"));
9438 ep->X_op = O_constant;
9439 }
9440
9441 if (ep->X_op == O_constant)
9442 {
67c0d1eb 9443 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9444 return;
9445 }
9446
9447 if (mips_pic == NO_PIC)
9448 {
9449 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9450 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9451 Otherwise we want
9452 lui $reg,<sym> (BFD_RELOC_HI16_S)
9453 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9454 If we have an addend, we always use the latter form.
76b3015f 9455
d6bc6245
TS
9456 With 64bit address space and a usable $at we want
9457 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9458 lui $at,<sym> (BFD_RELOC_HI16_S)
9459 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9460 daddiu $at,<sym> (BFD_RELOC_LO16)
9461 dsll32 $reg,0
3a482fd5 9462 daddu $reg,$reg,$at
76b3015f 9463
c03099e6 9464 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9465 on superscalar processors.
9466 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9467 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9468 dsll $reg,16
9469 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9470 dsll $reg,16
9471 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9472
9473 For GP relative symbols in 64bit address space we can use
9474 the same sequence as in 32bit address space. */
aed1a261 9475 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9476 {
6caf9ef4
TS
9477 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9478 && !nopic_need_relax (ep->X_add_symbol, 1))
9479 {
9480 relax_start (ep->X_add_symbol);
9481 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9482 mips_gp_register, BFD_RELOC_GPREL16);
9483 relax_switch ();
9484 }
d6bc6245 9485
741fe287 9486 if (*used_at == 0 && mips_opts.at)
d6bc6245 9487 {
df58fc94
RS
9488 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9489 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9490 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9491 BFD_RELOC_MIPS_HIGHER);
9492 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9493 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9494 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9495 *used_at = 1;
9496 }
9497 else
9498 {
df58fc94 9499 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9500 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9501 BFD_RELOC_MIPS_HIGHER);
df58fc94 9502 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9503 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9504 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9505 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9506 }
6caf9ef4
TS
9507
9508 if (mips_relax.sequence)
9509 relax_end ();
d6bc6245 9510 }
252b5132
RH
9511 else
9512 {
d6bc6245 9513 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9514 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9515 {
4d7206a2 9516 relax_start (ep->X_add_symbol);
67c0d1eb 9517 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9518 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9519 relax_switch ();
d6bc6245 9520 }
67c0d1eb
RS
9521 macro_build_lui (ep, reg);
9522 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9523 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9524 if (mips_relax.sequence)
9525 relax_end ();
d6bc6245 9526 }
252b5132 9527 }
0a44bf69 9528 else if (!mips_big_got)
252b5132
RH
9529 {
9530 expressionS ex;
9531
9532 /* If this is a reference to an external symbol, we want
9533 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9534 Otherwise we want
9535 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9536 nop
9537 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9538 If there is a constant, it must be added in after.
9539
ed6fb7bd 9540 If we have NewABI, we want
f5040a92
AO
9541 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9542 unless we're referencing a global symbol with a non-zero
9543 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9544 if (HAVE_NEWABI)
9545 {
f5040a92
AO
9546 if (ep->X_add_number)
9547 {
4d7206a2 9548 ex.X_add_number = ep->X_add_number;
f5040a92 9549 ep->X_add_number = 0;
4d7206a2 9550 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9551 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9552 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9553 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9554 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9555 ex.X_op = O_constant;
67c0d1eb 9556 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9557 reg, reg, BFD_RELOC_LO16);
f5040a92 9558 ep->X_add_number = ex.X_add_number;
4d7206a2 9559 relax_switch ();
f5040a92 9560 }
67c0d1eb 9561 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9562 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9563 if (mips_relax.sequence)
9564 relax_end ();
ed6fb7bd
SC
9565 }
9566 else
9567 {
f5040a92
AO
9568 ex.X_add_number = ep->X_add_number;
9569 ep->X_add_number = 0;
67c0d1eb
RS
9570 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9571 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9572 load_delay_nop ();
4d7206a2
RS
9573 relax_start (ep->X_add_symbol);
9574 relax_switch ();
67c0d1eb 9575 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9576 BFD_RELOC_LO16);
4d7206a2 9577 relax_end ();
ed6fb7bd 9578
f5040a92
AO
9579 if (ex.X_add_number != 0)
9580 {
9581 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9582 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9583 ex.X_op = O_constant;
67c0d1eb 9584 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9585 reg, reg, BFD_RELOC_LO16);
f5040a92 9586 }
252b5132
RH
9587 }
9588 }
0a44bf69 9589 else if (mips_big_got)
252b5132
RH
9590 {
9591 expressionS ex;
252b5132
RH
9592
9593 /* This is the large GOT case. If this is a reference to an
9594 external symbol, we want
9595 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9596 addu $reg,$reg,$gp
9597 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9598
9599 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9600 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9601 nop
9602 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9603 If there is a constant, it must be added in after.
f5040a92
AO
9604
9605 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9606 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9607 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9608 */
438c16b8
TS
9609 if (HAVE_NEWABI)
9610 {
4d7206a2 9611 ex.X_add_number = ep->X_add_number;
f5040a92 9612 ep->X_add_number = 0;
4d7206a2 9613 relax_start (ep->X_add_symbol);
df58fc94 9614 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9615 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9616 reg, reg, mips_gp_register);
9617 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9618 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9619 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9620 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9621 else if (ex.X_add_number)
9622 {
9623 ex.X_op = O_constant;
67c0d1eb
RS
9624 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9625 BFD_RELOC_LO16);
f5040a92
AO
9626 }
9627
9628 ep->X_add_number = ex.X_add_number;
4d7206a2 9629 relax_switch ();
67c0d1eb 9630 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9631 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9632 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9633 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9634 relax_end ();
438c16b8 9635 }
252b5132 9636 else
438c16b8 9637 {
f5040a92
AO
9638 ex.X_add_number = ep->X_add_number;
9639 ep->X_add_number = 0;
4d7206a2 9640 relax_start (ep->X_add_symbol);
df58fc94 9641 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9642 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9643 reg, reg, mips_gp_register);
9644 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9645 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9646 relax_switch ();
9647 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9648 {
9649 /* We need a nop before loading from $gp. This special
9650 check is required because the lui which starts the main
9651 instruction stream does not refer to $gp, and so will not
9652 insert the nop which may be required. */
67c0d1eb 9653 macro_build (NULL, "nop", "");
438c16b8 9654 }
67c0d1eb 9655 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9656 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9657 load_delay_nop ();
67c0d1eb 9658 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9659 BFD_RELOC_LO16);
4d7206a2 9660 relax_end ();
438c16b8 9661
f5040a92
AO
9662 if (ex.X_add_number != 0)
9663 {
9664 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9665 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9666 ex.X_op = O_constant;
67c0d1eb
RS
9667 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9668 BFD_RELOC_LO16);
f5040a92 9669 }
252b5132
RH
9670 }
9671 }
252b5132
RH
9672 else
9673 abort ();
8fc2e39e 9674
741fe287 9675 if (!mips_opts.at && *used_at == 1)
1661c76c 9676 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9677}
9678
ea1fb5dc
RS
9679/* Move the contents of register SOURCE into register DEST. */
9680
9681static void
67c0d1eb 9682move_register (int dest, int source)
ea1fb5dc 9683{
df58fc94
RS
9684 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9685 instruction specifically requires a 32-bit one. */
9686 if (mips_opts.micromips
833794fc 9687 && !mips_opts.insn32
df58fc94 9688 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9689 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9690 else
40fc1451 9691 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9692}
9693
4d7206a2 9694/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9695 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9696 The two alternatives are:
4d7206a2 9697
33eaf5de 9698 Global symbol Local symbol
4d7206a2
RS
9699 ------------- ------------
9700 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9701 ... ...
9702 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9703
9704 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9705 emits the second for a 16-bit offset or add_got_offset_hilo emits
9706 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9707
9708static void
67c0d1eb 9709load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9710{
9711 expressionS global;
9712
9713 global = *local;
9714 global.X_add_number = 0;
9715
9716 relax_start (local->X_add_symbol);
67c0d1eb
RS
9717 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9718 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9719 relax_switch ();
67c0d1eb
RS
9720 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9721 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9722 relax_end ();
9723}
9724
9725static void
67c0d1eb 9726add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9727{
9728 expressionS global;
9729
9730 global.X_op = O_constant;
9731 global.X_op_symbol = NULL;
9732 global.X_add_symbol = NULL;
9733 global.X_add_number = local->X_add_number;
9734
9735 relax_start (local->X_add_symbol);
67c0d1eb 9736 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9737 dest, dest, BFD_RELOC_LO16);
9738 relax_switch ();
67c0d1eb 9739 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9740 relax_end ();
9741}
9742
f6a22291
MR
9743static void
9744add_got_offset_hilo (int dest, expressionS *local, int tmp)
9745{
9746 expressionS global;
9747 int hold_mips_optimize;
9748
9749 global.X_op = O_constant;
9750 global.X_op_symbol = NULL;
9751 global.X_add_symbol = NULL;
9752 global.X_add_number = local->X_add_number;
9753
9754 relax_start (local->X_add_symbol);
9755 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9756 relax_switch ();
9757 /* Set mips_optimize around the lui instruction to avoid
9758 inserting an unnecessary nop after the lw. */
9759 hold_mips_optimize = mips_optimize;
9760 mips_optimize = 2;
9761 macro_build_lui (&global, tmp);
9762 mips_optimize = hold_mips_optimize;
9763 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9764 relax_end ();
9765
9766 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9767}
9768
df58fc94
RS
9769/* Emit a sequence of instructions to emulate a branch likely operation.
9770 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9771 is its complementing branch with the original condition negated.
9772 CALL is set if the original branch specified the link operation.
9773 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9774
9775 Code like this is produced in the noreorder mode:
9776
9777 BRNEG <args>, 1f
9778 nop
9779 b <sym>
9780 delay slot (executed only if branch taken)
9781 1:
9782
9783 or, if CALL is set:
9784
9785 BRNEG <args>, 1f
9786 nop
9787 bal <sym>
9788 delay slot (executed only if branch taken)
9789 1:
9790
9791 In the reorder mode the delay slot would be filled with a nop anyway,
9792 so code produced is simply:
9793
9794 BR <args>, <sym>
9795 nop
9796
9797 This function is used when producing code for the microMIPS ASE that
9798 does not implement branch likely instructions in hardware. */
9799
9800static void
9801macro_build_branch_likely (const char *br, const char *brneg,
9802 int call, expressionS *ep, const char *fmt,
9803 unsigned int sreg, unsigned int treg)
9804{
9805 int noreorder = mips_opts.noreorder;
9806 expressionS expr1;
9807
9808 gas_assert (mips_opts.micromips);
9809 start_noreorder ();
9810 if (noreorder)
9811 {
9812 micromips_label_expr (&expr1);
9813 macro_build (&expr1, brneg, fmt, sreg, treg);
9814 macro_build (NULL, "nop", "");
9815 macro_build (ep, call ? "bal" : "b", "p");
9816
9817 /* Set to true so that append_insn adds a label. */
9818 emit_branch_likely_macro = TRUE;
9819 }
9820 else
9821 {
9822 macro_build (ep, br, fmt, sreg, treg);
9823 macro_build (NULL, "nop", "");
9824 }
9825 end_noreorder ();
9826}
9827
9828/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9829 the condition code tested. EP specifies the branch target. */
9830
9831static void
9832macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9833{
9834 const int call = 0;
9835 const char *brneg;
9836 const char *br;
9837
9838 switch (type)
9839 {
9840 case M_BC1FL:
9841 br = "bc1f";
9842 brneg = "bc1t";
9843 break;
9844 case M_BC1TL:
9845 br = "bc1t";
9846 brneg = "bc1f";
9847 break;
9848 case M_BC2FL:
9849 br = "bc2f";
9850 brneg = "bc2t";
9851 break;
9852 case M_BC2TL:
9853 br = "bc2t";
9854 brneg = "bc2f";
9855 break;
9856 default:
9857 abort ();
9858 }
9859 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9860}
9861
9862/* Emit a two-argument branch macro specified by TYPE, using SREG as
9863 the register tested. EP specifies the branch target. */
9864
9865static void
9866macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9867{
9868 const char *brneg = NULL;
9869 const char *br;
9870 int call = 0;
9871
9872 switch (type)
9873 {
9874 case M_BGEZ:
9875 br = "bgez";
9876 break;
9877 case M_BGEZL:
9878 br = mips_opts.micromips ? "bgez" : "bgezl";
9879 brneg = "bltz";
9880 break;
9881 case M_BGEZALL:
9882 gas_assert (mips_opts.micromips);
833794fc 9883 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9884 brneg = "bltz";
9885 call = 1;
9886 break;
9887 case M_BGTZ:
9888 br = "bgtz";
9889 break;
9890 case M_BGTZL:
9891 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9892 brneg = "blez";
9893 break;
9894 case M_BLEZ:
9895 br = "blez";
9896 break;
9897 case M_BLEZL:
9898 br = mips_opts.micromips ? "blez" : "blezl";
9899 brneg = "bgtz";
9900 break;
9901 case M_BLTZ:
9902 br = "bltz";
9903 break;
9904 case M_BLTZL:
9905 br = mips_opts.micromips ? "bltz" : "bltzl";
9906 brneg = "bgez";
9907 break;
9908 case M_BLTZALL:
9909 gas_assert (mips_opts.micromips);
833794fc 9910 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9911 brneg = "bgez";
9912 call = 1;
9913 break;
9914 default:
9915 abort ();
9916 }
9917 if (mips_opts.micromips && brneg)
9918 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9919 else
9920 macro_build (ep, br, "s,p", sreg);
9921}
9922
9923/* Emit a three-argument branch macro specified by TYPE, using SREG and
9924 TREG as the registers tested. EP specifies the branch target. */
9925
9926static void
9927macro_build_branch_rsrt (int type, expressionS *ep,
9928 unsigned int sreg, unsigned int treg)
9929{
9930 const char *brneg = NULL;
9931 const int call = 0;
9932 const char *br;
9933
9934 switch (type)
9935 {
9936 case M_BEQ:
9937 case M_BEQ_I:
9938 br = "beq";
9939 break;
9940 case M_BEQL:
9941 case M_BEQL_I:
9942 br = mips_opts.micromips ? "beq" : "beql";
9943 brneg = "bne";
9944 break;
9945 case M_BNE:
9946 case M_BNE_I:
9947 br = "bne";
9948 break;
9949 case M_BNEL:
9950 case M_BNEL_I:
9951 br = mips_opts.micromips ? "bne" : "bnel";
9952 brneg = "beq";
9953 break;
9954 default:
9955 abort ();
9956 }
9957 if (mips_opts.micromips && brneg)
9958 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9959 else
9960 macro_build (ep, br, "s,t,p", sreg, treg);
9961}
9962
f2ae14a1
RS
9963/* Return the high part that should be loaded in order to make the low
9964 part of VALUE accessible using an offset of OFFBITS bits. */
9965
9966static offsetT
9967offset_high_part (offsetT value, unsigned int offbits)
9968{
9969 offsetT bias;
9970 addressT low_mask;
9971
9972 if (offbits == 0)
9973 return value;
9974 bias = 1 << (offbits - 1);
9975 low_mask = bias * 2 - 1;
9976 return (value + bias) & ~low_mask;
9977}
9978
9979/* Return true if the value stored in offset_expr and offset_reloc
9980 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9981 amount that the caller wants to add without inducing overflow
9982 and ALIGN is the known alignment of the value in bytes. */
9983
9984static bfd_boolean
9985small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9986{
9987 if (offbits == 16)
9988 {
9989 /* Accept any relocation operator if overflow isn't a concern. */
9990 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9991 return TRUE;
9992
9993 /* These relocations are guaranteed not to overflow in correct links. */
9994 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9995 || gprel16_reloc_p (*offset_reloc))
9996 return TRUE;
9997 }
9998 if (offset_expr.X_op == O_constant
9999 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10000 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10001 return TRUE;
10002 return FALSE;
10003}
10004
252b5132
RH
10005/*
10006 * Build macros
10007 * This routine implements the seemingly endless macro or synthesized
10008 * instructions and addressing modes in the mips assembly language. Many
10009 * of these macros are simple and are similar to each other. These could
67c1ffbe 10010 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10011 * this verbose method. Others are not simple macros but are more like
10012 * optimizing code generation.
10013 * One interesting optimization is when several store macros appear
67c1ffbe 10014 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10015 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10016 * of global optimization. We currently only optimize within a single macro.
10017 * For many of the load and store macros if the address is specified as a
10018 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10019 * first load register 'at' with zero and use it as the base register. The
10020 * mips assembler simply uses register $zero. Just one tiny optimization
10021 * we're missing.
10022 */
10023static void
833794fc 10024macro (struct mips_cl_insn *ip, char *str)
252b5132 10025{
c0ebe874
RS
10026 const struct mips_operand_array *operands;
10027 unsigned int breg, i;
741fe287 10028 unsigned int tempreg;
252b5132 10029 int mask;
43841e91 10030 int used_at = 0;
df58fc94 10031 expressionS label_expr;
252b5132 10032 expressionS expr1;
df58fc94 10033 expressionS *ep;
252b5132
RH
10034 const char *s;
10035 const char *s2;
10036 const char *fmt;
10037 int likely = 0;
252b5132 10038 int coproc = 0;
7f3c4072 10039 int offbits = 16;
1abe91b1 10040 int call = 0;
df58fc94
RS
10041 int jals = 0;
10042 int dbl = 0;
10043 int imm = 0;
10044 int ust = 0;
10045 int lp = 0;
f2ae14a1 10046 bfd_boolean large_offset;
252b5132 10047 int off;
252b5132 10048 int hold_mips_optimize;
f2ae14a1 10049 unsigned int align;
c0ebe874 10050 unsigned int op[MAX_OPERANDS];
252b5132 10051
9c2799c2 10052 gas_assert (! mips_opts.mips16);
252b5132 10053
c0ebe874
RS
10054 operands = insn_operands (ip);
10055 for (i = 0; i < MAX_OPERANDS; i++)
10056 if (operands->operand[i])
10057 op[i] = insn_extract_operand (ip, operands->operand[i]);
10058 else
10059 op[i] = -1;
10060
252b5132
RH
10061 mask = ip->insn_mo->mask;
10062
df58fc94
RS
10063 label_expr.X_op = O_constant;
10064 label_expr.X_op_symbol = NULL;
10065 label_expr.X_add_symbol = NULL;
10066 label_expr.X_add_number = 0;
10067
252b5132
RH
10068 expr1.X_op = O_constant;
10069 expr1.X_op_symbol = NULL;
10070 expr1.X_add_symbol = NULL;
10071 expr1.X_add_number = 1;
f2ae14a1 10072 align = 1;
252b5132
RH
10073
10074 switch (mask)
10075 {
10076 case M_DABS:
10077 dbl = 1;
1a0670f3 10078 /* Fall through. */
252b5132 10079 case M_ABS:
df58fc94
RS
10080 /* bgez $a0,1f
10081 move v0,$a0
10082 sub v0,$zero,$a0
10083 1:
10084 */
252b5132 10085
7d10b47d 10086 start_noreorder ();
252b5132 10087
df58fc94
RS
10088 if (mips_opts.micromips)
10089 micromips_label_expr (&label_expr);
10090 else
10091 label_expr.X_add_number = 8;
c0ebe874
RS
10092 macro_build (&label_expr, "bgez", "s,p", op[1]);
10093 if (op[0] == op[1])
a605d2b3 10094 macro_build (NULL, "nop", "");
252b5132 10095 else
c0ebe874
RS
10096 move_register (op[0], op[1]);
10097 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10098 if (mips_opts.micromips)
10099 micromips_add_label ();
252b5132 10100
7d10b47d 10101 end_noreorder ();
8fc2e39e 10102 break;
252b5132
RH
10103
10104 case M_ADD_I:
10105 s = "addi";
10106 s2 = "add";
10107 goto do_addi;
10108 case M_ADDU_I:
10109 s = "addiu";
10110 s2 = "addu";
10111 goto do_addi;
10112 case M_DADD_I:
10113 dbl = 1;
10114 s = "daddi";
10115 s2 = "dadd";
df58fc94
RS
10116 if (!mips_opts.micromips)
10117 goto do_addi;
b0e6f033 10118 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10119 && imm_expr.X_add_number < 0x200)
10120 {
b0e6f033
RS
10121 macro_build (NULL, s, "t,r,.", op[0], op[1],
10122 (int) imm_expr.X_add_number);
df58fc94
RS
10123 break;
10124 }
10125 goto do_addi_i;
252b5132
RH
10126 case M_DADDU_I:
10127 dbl = 1;
10128 s = "daddiu";
10129 s2 = "daddu";
10130 do_addi:
b0e6f033 10131 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10132 && imm_expr.X_add_number < 0x8000)
10133 {
c0ebe874 10134 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10135 break;
252b5132 10136 }
df58fc94 10137 do_addi_i:
8fc2e39e 10138 used_at = 1;
67c0d1eb 10139 load_register (AT, &imm_expr, dbl);
c0ebe874 10140 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10141 break;
10142
10143 case M_AND_I:
10144 s = "andi";
10145 s2 = "and";
10146 goto do_bit;
10147 case M_OR_I:
10148 s = "ori";
10149 s2 = "or";
10150 goto do_bit;
10151 case M_NOR_I:
10152 s = "";
10153 s2 = "nor";
10154 goto do_bit;
10155 case M_XOR_I:
10156 s = "xori";
10157 s2 = "xor";
10158 do_bit:
b0e6f033 10159 if (imm_expr.X_add_number >= 0
252b5132
RH
10160 && imm_expr.X_add_number < 0x10000)
10161 {
10162 if (mask != M_NOR_I)
c0ebe874 10163 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10164 else
10165 {
67c0d1eb 10166 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10167 op[0], op[1], BFD_RELOC_LO16);
10168 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10169 }
8fc2e39e 10170 break;
252b5132
RH
10171 }
10172
8fc2e39e 10173 used_at = 1;
bad1aba3 10174 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10175 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10176 break;
10177
8b082fb1
TS
10178 case M_BALIGN:
10179 switch (imm_expr.X_add_number)
10180 {
10181 case 0:
10182 macro_build (NULL, "nop", "");
10183 break;
10184 case 2:
c0ebe874 10185 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10186 break;
03f66e8a
MR
10187 case 1:
10188 case 3:
c0ebe874 10189 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10190 (int) imm_expr.X_add_number);
8b082fb1 10191 break;
03f66e8a
MR
10192 default:
10193 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10194 (unsigned long) imm_expr.X_add_number);
10195 break;
8b082fb1
TS
10196 }
10197 break;
10198
df58fc94
RS
10199 case M_BC1FL:
10200 case M_BC1TL:
10201 case M_BC2FL:
10202 case M_BC2TL:
10203 gas_assert (mips_opts.micromips);
10204 macro_build_branch_ccl (mask, &offset_expr,
10205 EXTRACT_OPERAND (1, BCC, *ip));
10206 break;
10207
252b5132 10208 case M_BEQ_I:
252b5132 10209 case M_BEQL_I:
252b5132 10210 case M_BNE_I:
252b5132 10211 case M_BNEL_I:
b0e6f033 10212 if (imm_expr.X_add_number == 0)
c0ebe874 10213 op[1] = 0;
df58fc94 10214 else
252b5132 10215 {
c0ebe874 10216 op[1] = AT;
df58fc94 10217 used_at = 1;
bad1aba3 10218 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10219 }
df58fc94
RS
10220 /* Fall through. */
10221 case M_BEQL:
10222 case M_BNEL:
c0ebe874 10223 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10224 break;
10225
10226 case M_BGEL:
10227 likely = 1;
1a0670f3 10228 /* Fall through. */
252b5132 10229 case M_BGE:
c0ebe874
RS
10230 if (op[1] == 0)
10231 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10232 else if (op[0] == 0)
10233 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10234 else
252b5132 10235 {
df58fc94 10236 used_at = 1;
c0ebe874 10237 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10238 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10239 &offset_expr, AT, ZERO);
252b5132 10240 }
df58fc94
RS
10241 break;
10242
10243 case M_BGEZL:
10244 case M_BGEZALL:
10245 case M_BGTZL:
10246 case M_BLEZL:
10247 case M_BLTZL:
10248 case M_BLTZALL:
c0ebe874 10249 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10250 break;
10251
10252 case M_BGTL_I:
10253 likely = 1;
1a0670f3 10254 /* Fall through. */
252b5132 10255 case M_BGT_I:
90ecf173 10256 /* Check for > max integer. */
b0e6f033 10257 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10258 {
10259 do_false:
90ecf173 10260 /* Result is always false. */
252b5132 10261 if (! likely)
a605d2b3 10262 macro_build (NULL, "nop", "");
252b5132 10263 else
df58fc94 10264 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10265 break;
252b5132 10266 }
f9419b05 10267 ++imm_expr.X_add_number;
252b5132
RH
10268 /* FALLTHROUGH */
10269 case M_BGE_I:
10270 case M_BGEL_I:
10271 if (mask == M_BGEL_I)
10272 likely = 1;
b0e6f033 10273 if (imm_expr.X_add_number == 0)
252b5132 10274 {
df58fc94 10275 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10276 &offset_expr, op[0]);
8fc2e39e 10277 break;
252b5132 10278 }
b0e6f033 10279 if (imm_expr.X_add_number == 1)
252b5132 10280 {
df58fc94 10281 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10282 &offset_expr, op[0]);
8fc2e39e 10283 break;
252b5132 10284 }
b0e6f033 10285 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10286 {
10287 do_true:
10288 /* result is always true */
1661c76c 10289 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10290 macro_build (&offset_expr, "b", "p");
8fc2e39e 10291 break;
252b5132 10292 }
8fc2e39e 10293 used_at = 1;
c0ebe874 10294 set_at (op[0], 0);
df58fc94
RS
10295 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10296 &offset_expr, AT, ZERO);
252b5132
RH
10297 break;
10298
10299 case M_BGEUL:
10300 likely = 1;
1a0670f3 10301 /* Fall through. */
252b5132 10302 case M_BGEU:
c0ebe874 10303 if (op[1] == 0)
252b5132 10304 goto do_true;
c0ebe874 10305 else if (op[0] == 0)
df58fc94 10306 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10307 &offset_expr, ZERO, op[1]);
df58fc94 10308 else
252b5132 10309 {
df58fc94 10310 used_at = 1;
c0ebe874 10311 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10312 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10313 &offset_expr, AT, ZERO);
252b5132 10314 }
252b5132
RH
10315 break;
10316
10317 case M_BGTUL_I:
10318 likely = 1;
1a0670f3 10319 /* Fall through. */
252b5132 10320 case M_BGTU_I:
c0ebe874 10321 if (op[0] == 0
bad1aba3 10322 || (GPR_SIZE == 32
f01dc953 10323 && imm_expr.X_add_number == -1))
252b5132 10324 goto do_false;
f9419b05 10325 ++imm_expr.X_add_number;
252b5132
RH
10326 /* FALLTHROUGH */
10327 case M_BGEU_I:
10328 case M_BGEUL_I:
10329 if (mask == M_BGEUL_I)
10330 likely = 1;
b0e6f033 10331 if (imm_expr.X_add_number == 0)
252b5132 10332 goto do_true;
b0e6f033 10333 else if (imm_expr.X_add_number == 1)
df58fc94 10334 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10335 &offset_expr, op[0], ZERO);
df58fc94 10336 else
252b5132 10337 {
df58fc94 10338 used_at = 1;
c0ebe874 10339 set_at (op[0], 1);
df58fc94
RS
10340 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10341 &offset_expr, AT, ZERO);
252b5132 10342 }
252b5132
RH
10343 break;
10344
10345 case M_BGTL:
10346 likely = 1;
1a0670f3 10347 /* Fall through. */
252b5132 10348 case M_BGT:
c0ebe874
RS
10349 if (op[1] == 0)
10350 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10351 else if (op[0] == 0)
10352 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10353 else
252b5132 10354 {
df58fc94 10355 used_at = 1;
c0ebe874 10356 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10357 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10358 &offset_expr, AT, ZERO);
252b5132 10359 }
252b5132
RH
10360 break;
10361
10362 case M_BGTUL:
10363 likely = 1;
1a0670f3 10364 /* Fall through. */
252b5132 10365 case M_BGTU:
c0ebe874 10366 if (op[1] == 0)
df58fc94 10367 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10368 &offset_expr, op[0], ZERO);
10369 else if (op[0] == 0)
df58fc94
RS
10370 goto do_false;
10371 else
252b5132 10372 {
df58fc94 10373 used_at = 1;
c0ebe874 10374 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10375 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10376 &offset_expr, AT, ZERO);
252b5132 10377 }
252b5132
RH
10378 break;
10379
10380 case M_BLEL:
10381 likely = 1;
1a0670f3 10382 /* Fall through. */
252b5132 10383 case M_BLE:
c0ebe874
RS
10384 if (op[1] == 0)
10385 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10386 else if (op[0] == 0)
10387 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10388 else
252b5132 10389 {
df58fc94 10390 used_at = 1;
c0ebe874 10391 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10392 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10393 &offset_expr, AT, ZERO);
252b5132 10394 }
252b5132
RH
10395 break;
10396
10397 case M_BLEL_I:
10398 likely = 1;
1a0670f3 10399 /* Fall through. */
252b5132 10400 case M_BLE_I:
b0e6f033 10401 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10402 goto do_true;
f9419b05 10403 ++imm_expr.X_add_number;
252b5132
RH
10404 /* FALLTHROUGH */
10405 case M_BLT_I:
10406 case M_BLTL_I:
10407 if (mask == M_BLTL_I)
10408 likely = 1;
b0e6f033 10409 if (imm_expr.X_add_number == 0)
c0ebe874 10410 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10411 else if (imm_expr.X_add_number == 1)
c0ebe874 10412 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10413 else
252b5132 10414 {
df58fc94 10415 used_at = 1;
c0ebe874 10416 set_at (op[0], 0);
df58fc94
RS
10417 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10418 &offset_expr, AT, ZERO);
252b5132 10419 }
252b5132
RH
10420 break;
10421
10422 case M_BLEUL:
10423 likely = 1;
1a0670f3 10424 /* Fall through. */
252b5132 10425 case M_BLEU:
c0ebe874 10426 if (op[1] == 0)
df58fc94 10427 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10428 &offset_expr, op[0], ZERO);
10429 else if (op[0] == 0)
df58fc94
RS
10430 goto do_true;
10431 else
252b5132 10432 {
df58fc94 10433 used_at = 1;
c0ebe874 10434 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10435 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10436 &offset_expr, AT, ZERO);
252b5132 10437 }
252b5132
RH
10438 break;
10439
10440 case M_BLEUL_I:
10441 likely = 1;
1a0670f3 10442 /* Fall through. */
252b5132 10443 case M_BLEU_I:
c0ebe874 10444 if (op[0] == 0
bad1aba3 10445 || (GPR_SIZE == 32
f01dc953 10446 && imm_expr.X_add_number == -1))
252b5132 10447 goto do_true;
f9419b05 10448 ++imm_expr.X_add_number;
252b5132
RH
10449 /* FALLTHROUGH */
10450 case M_BLTU_I:
10451 case M_BLTUL_I:
10452 if (mask == M_BLTUL_I)
10453 likely = 1;
b0e6f033 10454 if (imm_expr.X_add_number == 0)
252b5132 10455 goto do_false;
b0e6f033 10456 else if (imm_expr.X_add_number == 1)
df58fc94 10457 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10458 &offset_expr, op[0], ZERO);
df58fc94 10459 else
252b5132 10460 {
df58fc94 10461 used_at = 1;
c0ebe874 10462 set_at (op[0], 1);
df58fc94
RS
10463 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10464 &offset_expr, AT, ZERO);
252b5132 10465 }
252b5132
RH
10466 break;
10467
10468 case M_BLTL:
10469 likely = 1;
1a0670f3 10470 /* Fall through. */
252b5132 10471 case M_BLT:
c0ebe874
RS
10472 if (op[1] == 0)
10473 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10474 else if (op[0] == 0)
10475 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10476 else
252b5132 10477 {
df58fc94 10478 used_at = 1;
c0ebe874 10479 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10480 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10481 &offset_expr, AT, ZERO);
252b5132 10482 }
252b5132
RH
10483 break;
10484
10485 case M_BLTUL:
10486 likely = 1;
1a0670f3 10487 /* Fall through. */
252b5132 10488 case M_BLTU:
c0ebe874 10489 if (op[1] == 0)
252b5132 10490 goto do_false;
c0ebe874 10491 else if (op[0] == 0)
df58fc94 10492 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10493 &offset_expr, ZERO, op[1]);
df58fc94 10494 else
252b5132 10495 {
df58fc94 10496 used_at = 1;
c0ebe874 10497 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10498 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10499 &offset_expr, AT, ZERO);
252b5132 10500 }
252b5132
RH
10501 break;
10502
10503 case M_DDIV_3:
10504 dbl = 1;
1a0670f3 10505 /* Fall through. */
252b5132
RH
10506 case M_DIV_3:
10507 s = "mflo";
10508 goto do_div3;
10509 case M_DREM_3:
10510 dbl = 1;
1a0670f3 10511 /* Fall through. */
252b5132
RH
10512 case M_REM_3:
10513 s = "mfhi";
10514 do_div3:
c0ebe874 10515 if (op[2] == 0)
252b5132 10516 {
1661c76c 10517 as_warn (_("divide by zero"));
252b5132 10518 if (mips_trap)
df58fc94 10519 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10520 else
df58fc94 10521 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10522 break;
252b5132
RH
10523 }
10524
7d10b47d 10525 start_noreorder ();
252b5132
RH
10526 if (mips_trap)
10527 {
c0ebe874
RS
10528 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10529 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10530 }
10531 else
10532 {
df58fc94
RS
10533 if (mips_opts.micromips)
10534 micromips_label_expr (&label_expr);
10535 else
10536 label_expr.X_add_number = 8;
c0ebe874
RS
10537 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10538 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10539 macro_build (NULL, "break", BRK_FMT, 7);
10540 if (mips_opts.micromips)
10541 micromips_add_label ();
252b5132
RH
10542 }
10543 expr1.X_add_number = -1;
8fc2e39e 10544 used_at = 1;
f6a22291 10545 load_register (AT, &expr1, dbl);
df58fc94
RS
10546 if (mips_opts.micromips)
10547 micromips_label_expr (&label_expr);
10548 else
10549 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10550 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10551 if (dbl)
10552 {
10553 expr1.X_add_number = 1;
f6a22291 10554 load_register (AT, &expr1, dbl);
df58fc94 10555 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10556 }
10557 else
10558 {
10559 expr1.X_add_number = 0x80000000;
df58fc94 10560 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10561 }
10562 if (mips_trap)
10563 {
c0ebe874 10564 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10565 /* We want to close the noreorder block as soon as possible, so
10566 that later insns are available for delay slot filling. */
7d10b47d 10567 end_noreorder ();
252b5132
RH
10568 }
10569 else
10570 {
df58fc94
RS
10571 if (mips_opts.micromips)
10572 micromips_label_expr (&label_expr);
10573 else
10574 label_expr.X_add_number = 8;
c0ebe874 10575 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10576 macro_build (NULL, "nop", "");
252b5132
RH
10577
10578 /* We want to close the noreorder block as soon as possible, so
10579 that later insns are available for delay slot filling. */
7d10b47d 10580 end_noreorder ();
252b5132 10581
df58fc94 10582 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10583 }
df58fc94
RS
10584 if (mips_opts.micromips)
10585 micromips_add_label ();
c0ebe874 10586 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10587 break;
10588
10589 case M_DIV_3I:
10590 s = "div";
10591 s2 = "mflo";
10592 goto do_divi;
10593 case M_DIVU_3I:
10594 s = "divu";
10595 s2 = "mflo";
10596 goto do_divi;
10597 case M_REM_3I:
10598 s = "div";
10599 s2 = "mfhi";
10600 goto do_divi;
10601 case M_REMU_3I:
10602 s = "divu";
10603 s2 = "mfhi";
10604 goto do_divi;
10605 case M_DDIV_3I:
10606 dbl = 1;
10607 s = "ddiv";
10608 s2 = "mflo";
10609 goto do_divi;
10610 case M_DDIVU_3I:
10611 dbl = 1;
10612 s = "ddivu";
10613 s2 = "mflo";
10614 goto do_divi;
10615 case M_DREM_3I:
10616 dbl = 1;
10617 s = "ddiv";
10618 s2 = "mfhi";
10619 goto do_divi;
10620 case M_DREMU_3I:
10621 dbl = 1;
10622 s = "ddivu";
10623 s2 = "mfhi";
10624 do_divi:
b0e6f033 10625 if (imm_expr.X_add_number == 0)
252b5132 10626 {
1661c76c 10627 as_warn (_("divide by zero"));
252b5132 10628 if (mips_trap)
df58fc94 10629 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10630 else
df58fc94 10631 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10632 break;
252b5132 10633 }
b0e6f033 10634 if (imm_expr.X_add_number == 1)
252b5132
RH
10635 {
10636 if (strcmp (s2, "mflo") == 0)
c0ebe874 10637 move_register (op[0], op[1]);
252b5132 10638 else
c0ebe874 10639 move_register (op[0], ZERO);
8fc2e39e 10640 break;
252b5132 10641 }
b0e6f033 10642 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10643 {
10644 if (strcmp (s2, "mflo") == 0)
c0ebe874 10645 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10646 else
c0ebe874 10647 move_register (op[0], ZERO);
8fc2e39e 10648 break;
252b5132
RH
10649 }
10650
8fc2e39e 10651 used_at = 1;
67c0d1eb 10652 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10653 macro_build (NULL, s, "z,s,t", op[1], AT);
10654 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10655 break;
10656
10657 case M_DIVU_3:
10658 s = "divu";
10659 s2 = "mflo";
10660 goto do_divu3;
10661 case M_REMU_3:
10662 s = "divu";
10663 s2 = "mfhi";
10664 goto do_divu3;
10665 case M_DDIVU_3:
10666 s = "ddivu";
10667 s2 = "mflo";
10668 goto do_divu3;
10669 case M_DREMU_3:
10670 s = "ddivu";
10671 s2 = "mfhi";
10672 do_divu3:
7d10b47d 10673 start_noreorder ();
252b5132
RH
10674 if (mips_trap)
10675 {
c0ebe874
RS
10676 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10677 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10678 /* We want to close the noreorder block as soon as possible, so
10679 that later insns are available for delay slot filling. */
7d10b47d 10680 end_noreorder ();
252b5132
RH
10681 }
10682 else
10683 {
df58fc94
RS
10684 if (mips_opts.micromips)
10685 micromips_label_expr (&label_expr);
10686 else
10687 label_expr.X_add_number = 8;
c0ebe874
RS
10688 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10689 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10690
10691 /* We want to close the noreorder block as soon as possible, so
10692 that later insns are available for delay slot filling. */
7d10b47d 10693 end_noreorder ();
df58fc94
RS
10694 macro_build (NULL, "break", BRK_FMT, 7);
10695 if (mips_opts.micromips)
10696 micromips_add_label ();
252b5132 10697 }
c0ebe874 10698 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10699 break;
252b5132 10700
1abe91b1
MR
10701 case M_DLCA_AB:
10702 dbl = 1;
1a0670f3 10703 /* Fall through. */
1abe91b1
MR
10704 case M_LCA_AB:
10705 call = 1;
10706 goto do_la;
252b5132
RH
10707 case M_DLA_AB:
10708 dbl = 1;
1a0670f3 10709 /* Fall through. */
252b5132 10710 case M_LA_AB:
1abe91b1 10711 do_la:
252b5132
RH
10712 /* Load the address of a symbol into a register. If breg is not
10713 zero, we then add a base register to it. */
10714
c0ebe874 10715 breg = op[2];
bad1aba3 10716 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10717 as_warn (_("dla used to load 32-bit register; recommend using la "
10718 "instead"));
3bec30a8 10719
90ecf173 10720 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10721 as_warn (_("la used to load 64-bit address; recommend using dla "
10722 "instead"));
3bec30a8 10723
f2ae14a1 10724 if (small_offset_p (0, align, 16))
0c11417f 10725 {
c0ebe874 10726 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10727 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10728 break;
0c11417f
MR
10729 }
10730
c0ebe874 10731 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10732 {
10733 tempreg = AT;
10734 used_at = 1;
10735 }
10736 else
c0ebe874 10737 tempreg = op[0];
afdbd6d0 10738
252b5132
RH
10739 if (offset_expr.X_op != O_symbol
10740 && offset_expr.X_op != O_constant)
10741 {
1661c76c 10742 as_bad (_("expression too complex"));
252b5132
RH
10743 offset_expr.X_op = O_constant;
10744 }
10745
252b5132 10746 if (offset_expr.X_op == O_constant)
aed1a261 10747 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10748 else if (mips_pic == NO_PIC)
10749 {
d6bc6245 10750 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10751 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10752 Otherwise we want
10753 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10754 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10755 If we have a constant, we need two instructions anyhow,
d6bc6245 10756 so we may as well always use the latter form.
76b3015f 10757
6caf9ef4
TS
10758 With 64bit address space and a usable $at we want
10759 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10760 lui $at,<sym> (BFD_RELOC_HI16_S)
10761 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10762 daddiu $at,<sym> (BFD_RELOC_LO16)
10763 dsll32 $tempreg,0
10764 daddu $tempreg,$tempreg,$at
10765
10766 If $at is already in use, we use a path which is suboptimal
10767 on superscalar processors.
10768 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10769 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10770 dsll $tempreg,16
10771 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10772 dsll $tempreg,16
10773 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10774
10775 For GP relative symbols in 64bit address space we can use
10776 the same sequence as in 32bit address space. */
aed1a261 10777 if (HAVE_64BIT_SYMBOLS)
252b5132 10778 {
6caf9ef4
TS
10779 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10780 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10781 {
10782 relax_start (offset_expr.X_add_symbol);
10783 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10784 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10785 relax_switch ();
10786 }
d6bc6245 10787
741fe287 10788 if (used_at == 0 && mips_opts.at)
98d3f06f 10789 {
df58fc94 10790 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10791 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10792 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10793 AT, BFD_RELOC_HI16_S);
67c0d1eb 10794 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10795 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10796 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10797 AT, AT, BFD_RELOC_LO16);
df58fc94 10798 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10799 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10800 used_at = 1;
10801 }
10802 else
10803 {
df58fc94 10804 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10805 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10806 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10807 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10808 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10809 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10810 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10811 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10812 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10813 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10814 }
6caf9ef4
TS
10815
10816 if (mips_relax.sequence)
10817 relax_end ();
98d3f06f
KH
10818 }
10819 else
10820 {
10821 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10822 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10823 {
4d7206a2 10824 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10825 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10826 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10827 relax_switch ();
98d3f06f 10828 }
6943caf0 10829 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10830 as_bad (_("offset too large"));
67c0d1eb
RS
10831 macro_build_lui (&offset_expr, tempreg);
10832 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10833 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10834 if (mips_relax.sequence)
10835 relax_end ();
98d3f06f 10836 }
252b5132 10837 }
0a44bf69 10838 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10839 {
9117d219
NC
10840 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10841
252b5132
RH
10842 /* If this is a reference to an external symbol, and there
10843 is no constant, we want
10844 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10845 or for lca or if tempreg is PIC_CALL_REG
9117d219 10846 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10847 For a local symbol, we want
10848 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10849 nop
10850 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10851
10852 If we have a small constant, and this is a reference to
10853 an external symbol, we want
10854 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10855 nop
10856 addiu $tempreg,$tempreg,<constant>
10857 For a local symbol, we want the same instruction
10858 sequence, but we output a BFD_RELOC_LO16 reloc on the
10859 addiu instruction.
10860
10861 If we have a large constant, and this is a reference to
10862 an external symbol, we want
10863 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10864 lui $at,<hiconstant>
10865 addiu $at,$at,<loconstant>
10866 addu $tempreg,$tempreg,$at
10867 For a local symbol, we want the same instruction
10868 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10869 addiu instruction.
ed6fb7bd
SC
10870 */
10871
4d7206a2 10872 if (offset_expr.X_add_number == 0)
252b5132 10873 {
0a44bf69
RS
10874 if (mips_pic == SVR4_PIC
10875 && breg == 0
10876 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10877 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10878
10879 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10880 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10881 lw_reloc_type, mips_gp_register);
4d7206a2 10882 if (breg != 0)
252b5132
RH
10883 {
10884 /* We're going to put in an addu instruction using
10885 tempreg, so we may as well insert the nop right
10886 now. */
269137b2 10887 load_delay_nop ();
252b5132 10888 }
4d7206a2 10889 relax_switch ();
67c0d1eb
RS
10890 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10891 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10892 load_delay_nop ();
67c0d1eb
RS
10893 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10894 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10895 relax_end ();
252b5132
RH
10896 /* FIXME: If breg == 0, and the next instruction uses
10897 $tempreg, then if this variant case is used an extra
10898 nop will be generated. */
10899 }
4d7206a2
RS
10900 else if (offset_expr.X_add_number >= -0x8000
10901 && offset_expr.X_add_number < 0x8000)
252b5132 10902 {
67c0d1eb 10903 load_got_offset (tempreg, &offset_expr);
269137b2 10904 load_delay_nop ();
67c0d1eb 10905 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10906 }
10907 else
10908 {
4d7206a2
RS
10909 expr1.X_add_number = offset_expr.X_add_number;
10910 offset_expr.X_add_number =
43c0598f 10911 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10912 load_got_offset (tempreg, &offset_expr);
f6a22291 10913 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10914 /* If we are going to add in a base register, and the
10915 target register and the base register are the same,
10916 then we are using AT as a temporary register. Since
10917 we want to load the constant into AT, we add our
10918 current AT (from the global offset table) and the
10919 register into the register now, and pretend we were
10920 not using a base register. */
c0ebe874 10921 if (breg == op[0])
252b5132 10922 {
269137b2 10923 load_delay_nop ();
67c0d1eb 10924 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10925 op[0], AT, breg);
252b5132 10926 breg = 0;
c0ebe874 10927 tempreg = op[0];
252b5132 10928 }
f6a22291 10929 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10930 used_at = 1;
10931 }
10932 }
0a44bf69 10933 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10934 {
67c0d1eb 10935 int add_breg_early = 0;
f5040a92
AO
10936
10937 /* If this is a reference to an external, and there is no
10938 constant, or local symbol (*), with or without a
10939 constant, we want
10940 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10941 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10942 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10943
10944 If we have a small constant, and this is a reference to
10945 an external symbol, we want
10946 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10947 addiu $tempreg,$tempreg,<constant>
10948
10949 If we have a large constant, and this is a reference to
10950 an external symbol, we want
10951 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10952 lui $at,<hiconstant>
10953 addiu $at,$at,<loconstant>
10954 addu $tempreg,$tempreg,$at
10955
10956 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10957 local symbols, even though it introduces an additional
10958 instruction. */
10959
f5040a92
AO
10960 if (offset_expr.X_add_number)
10961 {
4d7206a2 10962 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10963 offset_expr.X_add_number = 0;
10964
4d7206a2 10965 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10966 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10967 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10968
10969 if (expr1.X_add_number >= -0x8000
10970 && expr1.X_add_number < 0x8000)
10971 {
67c0d1eb
RS
10972 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10973 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10974 }
ecd13cd3 10975 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10976 {
c0ebe874
RS
10977 unsigned int dreg;
10978
f5040a92
AO
10979 /* If we are going to add in a base register, and the
10980 target register and the base register are the same,
10981 then we are using AT as a temporary register. Since
10982 we want to load the constant into AT, we add our
10983 current AT (from the global offset table) and the
10984 register into the register now, and pretend we were
10985 not using a base register. */
c0ebe874 10986 if (breg != op[0])
f5040a92
AO
10987 dreg = tempreg;
10988 else
10989 {
9c2799c2 10990 gas_assert (tempreg == AT);
67c0d1eb 10991 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10992 op[0], AT, breg);
10993 dreg = op[0];
67c0d1eb 10994 add_breg_early = 1;
f5040a92
AO
10995 }
10996
f6a22291 10997 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10998 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10999 dreg, dreg, AT);
f5040a92 11000
f5040a92
AO
11001 used_at = 1;
11002 }
11003 else
11004 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11005
4d7206a2 11006 relax_switch ();
f5040a92
AO
11007 offset_expr.X_add_number = expr1.X_add_number;
11008
67c0d1eb
RS
11009 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11010 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11011 if (add_breg_early)
f5040a92 11012 {
67c0d1eb 11013 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11014 op[0], tempreg, breg);
f5040a92 11015 breg = 0;
c0ebe874 11016 tempreg = op[0];
f5040a92 11017 }
4d7206a2 11018 relax_end ();
f5040a92 11019 }
4d7206a2 11020 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11021 {
4d7206a2 11022 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11023 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11024 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11025 relax_switch ();
67c0d1eb
RS
11026 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11027 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11028 relax_end ();
f5040a92 11029 }
4d7206a2 11030 else
f5040a92 11031 {
67c0d1eb
RS
11032 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11033 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11034 }
11035 }
0a44bf69 11036 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11037 {
67c0d1eb 11038 int gpdelay;
9117d219
NC
11039 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11040 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11041 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11042
11043 /* This is the large GOT case. If this is a reference to an
11044 external symbol, and there is no constant, we want
11045 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11046 addu $tempreg,$tempreg,$gp
11047 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11048 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11049 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11050 addu $tempreg,$tempreg,$gp
11051 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11052 For a local symbol, we want
11053 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11054 nop
11055 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11056
11057 If we have a small constant, and this is a reference to
11058 an external symbol, we want
11059 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11060 addu $tempreg,$tempreg,$gp
11061 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11062 nop
11063 addiu $tempreg,$tempreg,<constant>
11064 For a local symbol, we want
11065 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11066 nop
11067 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11068
11069 If we have a large constant, and this is a reference to
11070 an external symbol, we want
11071 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11072 addu $tempreg,$tempreg,$gp
11073 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11074 lui $at,<hiconstant>
11075 addiu $at,$at,<loconstant>
11076 addu $tempreg,$tempreg,$at
11077 For a local symbol, we want
11078 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11079 lui $at,<hiconstant>
11080 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11081 addu $tempreg,$tempreg,$at
f5040a92 11082 */
438c16b8 11083
252b5132
RH
11084 expr1.X_add_number = offset_expr.X_add_number;
11085 offset_expr.X_add_number = 0;
4d7206a2 11086 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11087 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11088 if (expr1.X_add_number == 0 && breg == 0
11089 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11090 {
11091 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11092 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11093 }
df58fc94 11094 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11095 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11096 tempreg, tempreg, mips_gp_register);
67c0d1eb 11097 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11098 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11099 if (expr1.X_add_number == 0)
11100 {
67c0d1eb 11101 if (breg != 0)
252b5132
RH
11102 {
11103 /* We're going to put in an addu instruction using
11104 tempreg, so we may as well insert the nop right
11105 now. */
269137b2 11106 load_delay_nop ();
252b5132 11107 }
252b5132
RH
11108 }
11109 else if (expr1.X_add_number >= -0x8000
11110 && expr1.X_add_number < 0x8000)
11111 {
269137b2 11112 load_delay_nop ();
67c0d1eb 11113 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11114 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11115 }
11116 else
11117 {
c0ebe874
RS
11118 unsigned int dreg;
11119
252b5132
RH
11120 /* If we are going to add in a base register, and the
11121 target register and the base register are the same,
11122 then we are using AT as a temporary register. Since
11123 we want to load the constant into AT, we add our
11124 current AT (from the global offset table) and the
11125 register into the register now, and pretend we were
11126 not using a base register. */
c0ebe874 11127 if (breg != op[0])
67c0d1eb 11128 dreg = tempreg;
252b5132
RH
11129 else
11130 {
9c2799c2 11131 gas_assert (tempreg == AT);
269137b2 11132 load_delay_nop ();
67c0d1eb 11133 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11134 op[0], AT, breg);
11135 dreg = op[0];
252b5132
RH
11136 }
11137
f6a22291 11138 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11139 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11140
252b5132
RH
11141 used_at = 1;
11142 }
43c0598f 11143 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11144 relax_switch ();
252b5132 11145
67c0d1eb 11146 if (gpdelay)
252b5132
RH
11147 {
11148 /* This is needed because this instruction uses $gp, but
f5040a92 11149 the first instruction on the main stream does not. */
67c0d1eb 11150 macro_build (NULL, "nop", "");
252b5132 11151 }
ed6fb7bd 11152
67c0d1eb
RS
11153 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11154 local_reloc_type, mips_gp_register);
f5040a92 11155 if (expr1.X_add_number >= -0x8000
252b5132
RH
11156 && expr1.X_add_number < 0x8000)
11157 {
269137b2 11158 load_delay_nop ();
67c0d1eb
RS
11159 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11160 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11161 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11162 register, the external symbol case ended with a load,
11163 so if the symbol turns out to not be external, and
11164 the next instruction uses tempreg, an unnecessary nop
11165 will be inserted. */
252b5132
RH
11166 }
11167 else
11168 {
c0ebe874 11169 if (breg == op[0])
252b5132
RH
11170 {
11171 /* We must add in the base register now, as in the
f5040a92 11172 external symbol case. */
9c2799c2 11173 gas_assert (tempreg == AT);
269137b2 11174 load_delay_nop ();
67c0d1eb 11175 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11176 op[0], AT, breg);
11177 tempreg = op[0];
252b5132 11178 /* We set breg to 0 because we have arranged to add
f5040a92 11179 it in in both cases. */
252b5132
RH
11180 breg = 0;
11181 }
11182
67c0d1eb
RS
11183 macro_build_lui (&expr1, AT);
11184 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11185 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11186 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11187 tempreg, tempreg, AT);
8fc2e39e 11188 used_at = 1;
252b5132 11189 }
4d7206a2 11190 relax_end ();
252b5132 11191 }
0a44bf69 11192 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11193 {
f5040a92
AO
11194 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11195 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11196 int add_breg_early = 0;
f5040a92
AO
11197
11198 /* This is the large GOT case. If this is a reference to an
11199 external symbol, and there is no constant, we want
11200 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11201 add $tempreg,$tempreg,$gp
11202 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11203 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11204 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11205 add $tempreg,$tempreg,$gp
11206 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11207
11208 If we have a small constant, and this is a reference to
11209 an external symbol, we want
11210 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11211 add $tempreg,$tempreg,$gp
11212 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11213 addi $tempreg,$tempreg,<constant>
11214
11215 If we have a large constant, and this is a reference to
11216 an external symbol, we want
11217 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11218 addu $tempreg,$tempreg,$gp
11219 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11220 lui $at,<hiconstant>
11221 addi $at,$at,<loconstant>
11222 add $tempreg,$tempreg,$at
11223
11224 If we have NewABI, and we know it's a local symbol, we want
11225 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11226 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11227 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11228
4d7206a2 11229 relax_start (offset_expr.X_add_symbol);
f5040a92 11230
4d7206a2 11231 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11232 offset_expr.X_add_number = 0;
11233
1abe91b1
MR
11234 if (expr1.X_add_number == 0 && breg == 0
11235 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11236 {
11237 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11238 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11239 }
df58fc94 11240 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11241 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11242 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11243 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11244 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11245
11246 if (expr1.X_add_number == 0)
4d7206a2 11247 ;
f5040a92
AO
11248 else if (expr1.X_add_number >= -0x8000
11249 && expr1.X_add_number < 0x8000)
11250 {
67c0d1eb 11251 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11252 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11253 }
ecd13cd3 11254 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11255 {
c0ebe874
RS
11256 unsigned int dreg;
11257
f5040a92
AO
11258 /* If we are going to add in a base register, and the
11259 target register and the base register are the same,
11260 then we are using AT as a temporary register. Since
11261 we want to load the constant into AT, we add our
11262 current AT (from the global offset table) and the
11263 register into the register now, and pretend we were
11264 not using a base register. */
c0ebe874 11265 if (breg != op[0])
f5040a92
AO
11266 dreg = tempreg;
11267 else
11268 {
9c2799c2 11269 gas_assert (tempreg == AT);
67c0d1eb 11270 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11271 op[0], AT, breg);
11272 dreg = op[0];
67c0d1eb 11273 add_breg_early = 1;
f5040a92
AO
11274 }
11275
f6a22291 11276 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11277 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11278
f5040a92
AO
11279 used_at = 1;
11280 }
11281 else
11282 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11283
4d7206a2 11284 relax_switch ();
f5040a92 11285 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11286 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11287 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11288 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11289 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11290 if (add_breg_early)
f5040a92 11291 {
67c0d1eb 11292 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11293 op[0], tempreg, breg);
f5040a92 11294 breg = 0;
c0ebe874 11295 tempreg = op[0];
f5040a92 11296 }
4d7206a2 11297 relax_end ();
f5040a92 11298 }
252b5132
RH
11299 else
11300 abort ();
11301
11302 if (breg != 0)
c0ebe874 11303 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11304 break;
11305
52b6b6b9 11306 case M_MSGSND:
df58fc94 11307 gas_assert (!mips_opts.micromips);
c0ebe874 11308 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11309 break;
52b6b6b9
JM
11310
11311 case M_MSGLD:
df58fc94 11312 gas_assert (!mips_opts.micromips);
c8276761 11313 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11314 break;
52b6b6b9
JM
11315
11316 case M_MSGLD_T:
df58fc94 11317 gas_assert (!mips_opts.micromips);
c0ebe874 11318 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11319 break;
52b6b6b9
JM
11320
11321 case M_MSGWAIT:
df58fc94 11322 gas_assert (!mips_opts.micromips);
52b6b6b9 11323 macro_build (NULL, "c2", "C", 3);
c7af4273 11324 break;
52b6b6b9
JM
11325
11326 case M_MSGWAIT_T:
df58fc94 11327 gas_assert (!mips_opts.micromips);
c0ebe874 11328 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11329 break;
52b6b6b9 11330
252b5132
RH
11331 case M_J_A:
11332 /* The j instruction may not be used in PIC code, since it
11333 requires an absolute address. We convert it to a b
11334 instruction. */
11335 if (mips_pic == NO_PIC)
67c0d1eb 11336 macro_build (&offset_expr, "j", "a");
252b5132 11337 else
67c0d1eb 11338 macro_build (&offset_expr, "b", "p");
8fc2e39e 11339 break;
252b5132
RH
11340
11341 /* The jal instructions must be handled as macros because when
11342 generating PIC code they expand to multi-instruction
11343 sequences. Normally they are simple instructions. */
df58fc94 11344 case M_JALS_1:
c0ebe874
RS
11345 op[1] = op[0];
11346 op[0] = RA;
df58fc94
RS
11347 /* Fall through. */
11348 case M_JALS_2:
11349 gas_assert (mips_opts.micromips);
833794fc
MR
11350 if (mips_opts.insn32)
11351 {
1661c76c 11352 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11353 break;
11354 }
df58fc94
RS
11355 jals = 1;
11356 goto jal;
252b5132 11357 case M_JAL_1:
c0ebe874
RS
11358 op[1] = op[0];
11359 op[0] = RA;
252b5132
RH
11360 /* Fall through. */
11361 case M_JAL_2:
df58fc94 11362 jal:
3e722fb5 11363 if (mips_pic == NO_PIC)
df58fc94
RS
11364 {
11365 s = jals ? "jalrs" : "jalr";
e64af278 11366 if (mips_opts.micromips
833794fc 11367 && !mips_opts.insn32
c0ebe874 11368 && op[0] == RA
e64af278 11369 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11370 macro_build (NULL, s, "mj", op[1]);
df58fc94 11371 else
c0ebe874 11372 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11373 }
0a44bf69 11374 else
252b5132 11375 {
df58fc94
RS
11376 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11377 && mips_cprestore_offset >= 0);
11378
c0ebe874 11379 if (op[1] != PIC_CALL_REG)
252b5132 11380 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11381
833794fc
MR
11382 s = ((mips_opts.micromips
11383 && !mips_opts.insn32
11384 && (!mips_opts.noreorder || cprestore))
df58fc94 11385 ? "jalrs" : "jalr");
e64af278 11386 if (mips_opts.micromips
833794fc 11387 && !mips_opts.insn32
c0ebe874 11388 && op[0] == RA
e64af278 11389 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11390 macro_build (NULL, s, "mj", op[1]);
df58fc94 11391 else
c0ebe874 11392 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11393 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11394 {
6478892d 11395 if (mips_cprestore_offset < 0)
1661c76c 11396 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11397 else
11398 {
90ecf173 11399 if (!mips_frame_reg_valid)
7a621144 11400 {
1661c76c 11401 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11402 /* Quiet this warning. */
11403 mips_frame_reg_valid = 1;
11404 }
90ecf173 11405 if (!mips_cprestore_valid)
7a621144 11406 {
1661c76c 11407 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11408 /* Quiet this warning. */
11409 mips_cprestore_valid = 1;
11410 }
d3fca0b5
MR
11411 if (mips_opts.noreorder)
11412 macro_build (NULL, "nop", "");
6478892d 11413 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11414 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11415 mips_gp_register,
256ab948
TS
11416 mips_frame_reg,
11417 HAVE_64BIT_ADDRESSES);
6478892d 11418 }
252b5132
RH
11419 }
11420 }
252b5132 11421
8fc2e39e 11422 break;
252b5132 11423
df58fc94
RS
11424 case M_JALS_A:
11425 gas_assert (mips_opts.micromips);
833794fc
MR
11426 if (mips_opts.insn32)
11427 {
1661c76c 11428 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11429 break;
11430 }
df58fc94
RS
11431 jals = 1;
11432 /* Fall through. */
252b5132
RH
11433 case M_JAL_A:
11434 if (mips_pic == NO_PIC)
df58fc94 11435 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11436 else if (mips_pic == SVR4_PIC)
11437 {
11438 /* If this is a reference to an external symbol, and we are
11439 using a small GOT, we want
11440 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11441 nop
f9419b05 11442 jalr $ra,$25
252b5132
RH
11443 nop
11444 lw $gp,cprestore($sp)
11445 The cprestore value is set using the .cprestore
11446 pseudo-op. If we are using a big GOT, we want
11447 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11448 addu $25,$25,$gp
11449 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11450 nop
f9419b05 11451 jalr $ra,$25
252b5132
RH
11452 nop
11453 lw $gp,cprestore($sp)
11454 If the symbol is not external, we want
11455 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11456 nop
11457 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11458 jalr $ra,$25
252b5132 11459 nop
438c16b8 11460 lw $gp,cprestore($sp)
f5040a92
AO
11461
11462 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11463 sequences above, minus nops, unless the symbol is local,
11464 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11465 GOT_DISP. */
438c16b8 11466 if (HAVE_NEWABI)
252b5132 11467 {
90ecf173 11468 if (!mips_big_got)
f5040a92 11469 {
4d7206a2 11470 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11471 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11472 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11473 mips_gp_register);
4d7206a2 11474 relax_switch ();
67c0d1eb
RS
11475 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11476 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11477 mips_gp_register);
11478 relax_end ();
f5040a92
AO
11479 }
11480 else
11481 {
4d7206a2 11482 relax_start (offset_expr.X_add_symbol);
df58fc94 11483 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11484 BFD_RELOC_MIPS_CALL_HI16);
11485 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11486 PIC_CALL_REG, mips_gp_register);
11487 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11488 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11489 PIC_CALL_REG);
4d7206a2 11490 relax_switch ();
67c0d1eb
RS
11491 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11492 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11493 mips_gp_register);
11494 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11495 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11496 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11497 relax_end ();
f5040a92 11498 }
684022ea 11499
df58fc94 11500 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11501 }
11502 else
11503 {
4d7206a2 11504 relax_start (offset_expr.X_add_symbol);
90ecf173 11505 if (!mips_big_got)
438c16b8 11506 {
67c0d1eb
RS
11507 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11508 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11509 mips_gp_register);
269137b2 11510 load_delay_nop ();
4d7206a2 11511 relax_switch ();
438c16b8 11512 }
252b5132 11513 else
252b5132 11514 {
67c0d1eb
RS
11515 int gpdelay;
11516
11517 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11518 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11519 BFD_RELOC_MIPS_CALL_HI16);
11520 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11521 PIC_CALL_REG, mips_gp_register);
11522 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11523 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11524 PIC_CALL_REG);
269137b2 11525 load_delay_nop ();
4d7206a2 11526 relax_switch ();
67c0d1eb
RS
11527 if (gpdelay)
11528 macro_build (NULL, "nop", "");
252b5132 11529 }
67c0d1eb
RS
11530 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11531 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11532 mips_gp_register);
269137b2 11533 load_delay_nop ();
67c0d1eb
RS
11534 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11535 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11536 relax_end ();
df58fc94 11537 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11538
6478892d 11539 if (mips_cprestore_offset < 0)
1661c76c 11540 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11541 else
11542 {
90ecf173 11543 if (!mips_frame_reg_valid)
7a621144 11544 {
1661c76c 11545 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11546 /* Quiet this warning. */
11547 mips_frame_reg_valid = 1;
11548 }
90ecf173 11549 if (!mips_cprestore_valid)
7a621144 11550 {
1661c76c 11551 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11552 /* Quiet this warning. */
11553 mips_cprestore_valid = 1;
11554 }
6478892d 11555 if (mips_opts.noreorder)
67c0d1eb 11556 macro_build (NULL, "nop", "");
6478892d 11557 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11558 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11559 mips_gp_register,
256ab948
TS
11560 mips_frame_reg,
11561 HAVE_64BIT_ADDRESSES);
6478892d 11562 }
252b5132
RH
11563 }
11564 }
0a44bf69 11565 else if (mips_pic == VXWORKS_PIC)
1661c76c 11566 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11567 else
11568 abort ();
11569
8fc2e39e 11570 break;
252b5132 11571
7f3c4072 11572 case M_LBUE_AB:
7f3c4072
CM
11573 s = "lbue";
11574 fmt = "t,+j(b)";
11575 offbits = 9;
11576 goto ld_st;
11577 case M_LHUE_AB:
7f3c4072
CM
11578 s = "lhue";
11579 fmt = "t,+j(b)";
11580 offbits = 9;
11581 goto ld_st;
11582 case M_LBE_AB:
7f3c4072
CM
11583 s = "lbe";
11584 fmt = "t,+j(b)";
11585 offbits = 9;
11586 goto ld_st;
11587 case M_LHE_AB:
7f3c4072
CM
11588 s = "lhe";
11589 fmt = "t,+j(b)";
11590 offbits = 9;
11591 goto ld_st;
11592 case M_LLE_AB:
7f3c4072
CM
11593 s = "lle";
11594 fmt = "t,+j(b)";
11595 offbits = 9;
11596 goto ld_st;
11597 case M_LWE_AB:
7f3c4072
CM
11598 s = "lwe";
11599 fmt = "t,+j(b)";
11600 offbits = 9;
11601 goto ld_st;
11602 case M_LWLE_AB:
7f3c4072
CM
11603 s = "lwle";
11604 fmt = "t,+j(b)";
11605 offbits = 9;
11606 goto ld_st;
11607 case M_LWRE_AB:
7f3c4072
CM
11608 s = "lwre";
11609 fmt = "t,+j(b)";
11610 offbits = 9;
11611 goto ld_st;
11612 case M_SBE_AB:
7f3c4072
CM
11613 s = "sbe";
11614 fmt = "t,+j(b)";
11615 offbits = 9;
11616 goto ld_st;
11617 case M_SCE_AB:
7f3c4072
CM
11618 s = "sce";
11619 fmt = "t,+j(b)";
11620 offbits = 9;
11621 goto ld_st;
11622 case M_SHE_AB:
7f3c4072
CM
11623 s = "she";
11624 fmt = "t,+j(b)";
11625 offbits = 9;
11626 goto ld_st;
11627 case M_SWE_AB:
7f3c4072
CM
11628 s = "swe";
11629 fmt = "t,+j(b)";
11630 offbits = 9;
11631 goto ld_st;
11632 case M_SWLE_AB:
7f3c4072
CM
11633 s = "swle";
11634 fmt = "t,+j(b)";
11635 offbits = 9;
11636 goto ld_st;
11637 case M_SWRE_AB:
7f3c4072
CM
11638 s = "swre";
11639 fmt = "t,+j(b)";
11640 offbits = 9;
11641 goto ld_st;
dec0624d 11642 case M_ACLR_AB:
dec0624d 11643 s = "aclr";
dec0624d 11644 fmt = "\\,~(b)";
7f3c4072 11645 offbits = 12;
dec0624d
MR
11646 goto ld_st;
11647 case M_ASET_AB:
dec0624d 11648 s = "aset";
dec0624d 11649 fmt = "\\,~(b)";
7f3c4072 11650 offbits = 12;
dec0624d 11651 goto ld_st;
252b5132
RH
11652 case M_LB_AB:
11653 s = "lb";
df58fc94 11654 fmt = "t,o(b)";
252b5132
RH
11655 goto ld;
11656 case M_LBU_AB:
11657 s = "lbu";
df58fc94 11658 fmt = "t,o(b)";
252b5132
RH
11659 goto ld;
11660 case M_LH_AB:
11661 s = "lh";
df58fc94 11662 fmt = "t,o(b)";
252b5132
RH
11663 goto ld;
11664 case M_LHU_AB:
11665 s = "lhu";
df58fc94 11666 fmt = "t,o(b)";
252b5132
RH
11667 goto ld;
11668 case M_LW_AB:
11669 s = "lw";
df58fc94 11670 fmt = "t,o(b)";
252b5132
RH
11671 goto ld;
11672 case M_LWC0_AB:
df58fc94 11673 gas_assert (!mips_opts.micromips);
252b5132 11674 s = "lwc0";
df58fc94 11675 fmt = "E,o(b)";
bdaaa2e1 11676 /* Itbl support may require additional care here. */
252b5132 11677 coproc = 1;
df58fc94 11678 goto ld_st;
252b5132
RH
11679 case M_LWC1_AB:
11680 s = "lwc1";
df58fc94 11681 fmt = "T,o(b)";
bdaaa2e1 11682 /* Itbl support may require additional care here. */
252b5132 11683 coproc = 1;
df58fc94 11684 goto ld_st;
252b5132
RH
11685 case M_LWC2_AB:
11686 s = "lwc2";
df58fc94 11687 fmt = COP12_FMT;
7361da2c
AB
11688 offbits = (mips_opts.micromips ? 12
11689 : ISA_IS_R6 (mips_opts.isa) ? 11
11690 : 16);
bdaaa2e1 11691 /* Itbl support may require additional care here. */
252b5132 11692 coproc = 1;
df58fc94 11693 goto ld_st;
252b5132 11694 case M_LWC3_AB:
df58fc94 11695 gas_assert (!mips_opts.micromips);
252b5132 11696 s = "lwc3";
df58fc94 11697 fmt = "E,o(b)";
bdaaa2e1 11698 /* Itbl support may require additional care here. */
252b5132 11699 coproc = 1;
df58fc94 11700 goto ld_st;
252b5132
RH
11701 case M_LWL_AB:
11702 s = "lwl";
df58fc94 11703 fmt = MEM12_FMT;
7f3c4072 11704 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11705 goto ld_st;
252b5132
RH
11706 case M_LWR_AB:
11707 s = "lwr";
df58fc94 11708 fmt = MEM12_FMT;
7f3c4072 11709 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11710 goto ld_st;
252b5132 11711 case M_LDC1_AB:
252b5132 11712 s = "ldc1";
df58fc94 11713 fmt = "T,o(b)";
bdaaa2e1 11714 /* Itbl support may require additional care here. */
252b5132 11715 coproc = 1;
df58fc94 11716 goto ld_st;
252b5132
RH
11717 case M_LDC2_AB:
11718 s = "ldc2";
df58fc94 11719 fmt = COP12_FMT;
7361da2c
AB
11720 offbits = (mips_opts.micromips ? 12
11721 : ISA_IS_R6 (mips_opts.isa) ? 11
11722 : 16);
bdaaa2e1 11723 /* Itbl support may require additional care here. */
252b5132 11724 coproc = 1;
df58fc94 11725 goto ld_st;
c77c0862 11726 case M_LQC2_AB:
c77c0862 11727 s = "lqc2";
14daeee3 11728 fmt = "+7,o(b)";
c77c0862
RS
11729 /* Itbl support may require additional care here. */
11730 coproc = 1;
11731 goto ld_st;
252b5132
RH
11732 case M_LDC3_AB:
11733 s = "ldc3";
df58fc94 11734 fmt = "E,o(b)";
bdaaa2e1 11735 /* Itbl support may require additional care here. */
252b5132 11736 coproc = 1;
df58fc94 11737 goto ld_st;
252b5132
RH
11738 case M_LDL_AB:
11739 s = "ldl";
df58fc94 11740 fmt = MEM12_FMT;
7f3c4072 11741 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11742 goto ld_st;
252b5132
RH
11743 case M_LDR_AB:
11744 s = "ldr";
df58fc94 11745 fmt = MEM12_FMT;
7f3c4072 11746 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11747 goto ld_st;
252b5132
RH
11748 case M_LL_AB:
11749 s = "ll";
7361da2c
AB
11750 fmt = LL_SC_FMT;
11751 offbits = (mips_opts.micromips ? 12
11752 : ISA_IS_R6 (mips_opts.isa) ? 9
11753 : 16);
252b5132
RH
11754 goto ld;
11755 case M_LLD_AB:
11756 s = "lld";
7361da2c
AB
11757 fmt = LL_SC_FMT;
11758 offbits = (mips_opts.micromips ? 12
11759 : ISA_IS_R6 (mips_opts.isa) ? 9
11760 : 16);
252b5132
RH
11761 goto ld;
11762 case M_LWU_AB:
11763 s = "lwu";
df58fc94 11764 fmt = MEM12_FMT;
7f3c4072 11765 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11766 goto ld;
11767 case M_LWP_AB:
df58fc94
RS
11768 gas_assert (mips_opts.micromips);
11769 s = "lwp";
11770 fmt = "t,~(b)";
7f3c4072 11771 offbits = 12;
df58fc94
RS
11772 lp = 1;
11773 goto ld;
11774 case M_LDP_AB:
df58fc94
RS
11775 gas_assert (mips_opts.micromips);
11776 s = "ldp";
11777 fmt = "t,~(b)";
7f3c4072 11778 offbits = 12;
df58fc94
RS
11779 lp = 1;
11780 goto ld;
11781 case M_LWM_AB:
df58fc94
RS
11782 gas_assert (mips_opts.micromips);
11783 s = "lwm";
11784 fmt = "n,~(b)";
7f3c4072 11785 offbits = 12;
df58fc94
RS
11786 goto ld_st;
11787 case M_LDM_AB:
df58fc94
RS
11788 gas_assert (mips_opts.micromips);
11789 s = "ldm";
11790 fmt = "n,~(b)";
7f3c4072 11791 offbits = 12;
df58fc94
RS
11792 goto ld_st;
11793
252b5132 11794 ld:
f19ccbda 11795 /* We don't want to use $0 as tempreg. */
c0ebe874 11796 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11797 goto ld_st;
252b5132 11798 else
c0ebe874 11799 tempreg = op[0] + lp;
df58fc94
RS
11800 goto ld_noat;
11801
252b5132
RH
11802 case M_SB_AB:
11803 s = "sb";
df58fc94
RS
11804 fmt = "t,o(b)";
11805 goto ld_st;
252b5132
RH
11806 case M_SH_AB:
11807 s = "sh";
df58fc94
RS
11808 fmt = "t,o(b)";
11809 goto ld_st;
252b5132
RH
11810 case M_SW_AB:
11811 s = "sw";
df58fc94
RS
11812 fmt = "t,o(b)";
11813 goto ld_st;
252b5132 11814 case M_SWC0_AB:
df58fc94 11815 gas_assert (!mips_opts.micromips);
252b5132 11816 s = "swc0";
df58fc94 11817 fmt = "E,o(b)";
bdaaa2e1 11818 /* Itbl support may require additional care here. */
252b5132 11819 coproc = 1;
df58fc94 11820 goto ld_st;
252b5132
RH
11821 case M_SWC1_AB:
11822 s = "swc1";
df58fc94 11823 fmt = "T,o(b)";
bdaaa2e1 11824 /* Itbl support may require additional care here. */
252b5132 11825 coproc = 1;
df58fc94 11826 goto ld_st;
252b5132
RH
11827 case M_SWC2_AB:
11828 s = "swc2";
df58fc94 11829 fmt = COP12_FMT;
7361da2c
AB
11830 offbits = (mips_opts.micromips ? 12
11831 : ISA_IS_R6 (mips_opts.isa) ? 11
11832 : 16);
bdaaa2e1 11833 /* Itbl support may require additional care here. */
252b5132 11834 coproc = 1;
df58fc94 11835 goto ld_st;
252b5132 11836 case M_SWC3_AB:
df58fc94 11837 gas_assert (!mips_opts.micromips);
252b5132 11838 s = "swc3";
df58fc94 11839 fmt = "E,o(b)";
bdaaa2e1 11840 /* Itbl support may require additional care here. */
252b5132 11841 coproc = 1;
df58fc94 11842 goto ld_st;
252b5132
RH
11843 case M_SWL_AB:
11844 s = "swl";
df58fc94 11845 fmt = MEM12_FMT;
7f3c4072 11846 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11847 goto ld_st;
252b5132
RH
11848 case M_SWR_AB:
11849 s = "swr";
df58fc94 11850 fmt = MEM12_FMT;
7f3c4072 11851 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11852 goto ld_st;
252b5132
RH
11853 case M_SC_AB:
11854 s = "sc";
7361da2c
AB
11855 fmt = LL_SC_FMT;
11856 offbits = (mips_opts.micromips ? 12
11857 : ISA_IS_R6 (mips_opts.isa) ? 9
11858 : 16);
df58fc94 11859 goto ld_st;
252b5132
RH
11860 case M_SCD_AB:
11861 s = "scd";
7361da2c
AB
11862 fmt = LL_SC_FMT;
11863 offbits = (mips_opts.micromips ? 12
11864 : ISA_IS_R6 (mips_opts.isa) ? 9
11865 : 16);
df58fc94 11866 goto ld_st;
d43b4baf
TS
11867 case M_CACHE_AB:
11868 s = "cache";
7361da2c
AB
11869 fmt = (mips_opts.micromips ? "k,~(b)"
11870 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11871 : "k,o(b)");
11872 offbits = (mips_opts.micromips ? 12
11873 : ISA_IS_R6 (mips_opts.isa) ? 9
11874 : 16);
7f3c4072
CM
11875 goto ld_st;
11876 case M_CACHEE_AB:
7f3c4072
CM
11877 s = "cachee";
11878 fmt = "k,+j(b)";
11879 offbits = 9;
df58fc94 11880 goto ld_st;
3eebd5eb
MR
11881 case M_PREF_AB:
11882 s = "pref";
7361da2c
AB
11883 fmt = (mips_opts.micromips ? "k,~(b)"
11884 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11885 : "k,o(b)");
11886 offbits = (mips_opts.micromips ? 12
11887 : ISA_IS_R6 (mips_opts.isa) ? 9
11888 : 16);
7f3c4072
CM
11889 goto ld_st;
11890 case M_PREFE_AB:
7f3c4072
CM
11891 s = "prefe";
11892 fmt = "k,+j(b)";
11893 offbits = 9;
df58fc94 11894 goto ld_st;
252b5132 11895 case M_SDC1_AB:
252b5132 11896 s = "sdc1";
df58fc94 11897 fmt = "T,o(b)";
252b5132 11898 coproc = 1;
bdaaa2e1 11899 /* Itbl support may require additional care here. */
df58fc94 11900 goto ld_st;
252b5132
RH
11901 case M_SDC2_AB:
11902 s = "sdc2";
df58fc94 11903 fmt = COP12_FMT;
7361da2c
AB
11904 offbits = (mips_opts.micromips ? 12
11905 : ISA_IS_R6 (mips_opts.isa) ? 11
11906 : 16);
c77c0862
RS
11907 /* Itbl support may require additional care here. */
11908 coproc = 1;
11909 goto ld_st;
11910 case M_SQC2_AB:
c77c0862 11911 s = "sqc2";
14daeee3 11912 fmt = "+7,o(b)";
bdaaa2e1 11913 /* Itbl support may require additional care here. */
252b5132 11914 coproc = 1;
df58fc94 11915 goto ld_st;
252b5132 11916 case M_SDC3_AB:
df58fc94 11917 gas_assert (!mips_opts.micromips);
252b5132 11918 s = "sdc3";
df58fc94 11919 fmt = "E,o(b)";
bdaaa2e1 11920 /* Itbl support may require additional care here. */
252b5132 11921 coproc = 1;
df58fc94 11922 goto ld_st;
252b5132
RH
11923 case M_SDL_AB:
11924 s = "sdl";
df58fc94 11925 fmt = MEM12_FMT;
7f3c4072 11926 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11927 goto ld_st;
252b5132
RH
11928 case M_SDR_AB:
11929 s = "sdr";
df58fc94 11930 fmt = MEM12_FMT;
7f3c4072 11931 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11932 goto ld_st;
11933 case M_SWP_AB:
df58fc94
RS
11934 gas_assert (mips_opts.micromips);
11935 s = "swp";
11936 fmt = "t,~(b)";
7f3c4072 11937 offbits = 12;
df58fc94
RS
11938 goto ld_st;
11939 case M_SDP_AB:
df58fc94
RS
11940 gas_assert (mips_opts.micromips);
11941 s = "sdp";
11942 fmt = "t,~(b)";
7f3c4072 11943 offbits = 12;
df58fc94
RS
11944 goto ld_st;
11945 case M_SWM_AB:
df58fc94
RS
11946 gas_assert (mips_opts.micromips);
11947 s = "swm";
11948 fmt = "n,~(b)";
7f3c4072 11949 offbits = 12;
df58fc94
RS
11950 goto ld_st;
11951 case M_SDM_AB:
df58fc94
RS
11952 gas_assert (mips_opts.micromips);
11953 s = "sdm";
11954 fmt = "n,~(b)";
7f3c4072 11955 offbits = 12;
df58fc94
RS
11956
11957 ld_st:
8fc2e39e 11958 tempreg = AT;
df58fc94 11959 ld_noat:
c0ebe874 11960 breg = op[2];
f2ae14a1
RS
11961 if (small_offset_p (0, align, 16))
11962 {
11963 /* The first case exists for M_LD_AB and M_SD_AB, which are
11964 macros for o32 but which should act like normal instructions
11965 otherwise. */
11966 if (offbits == 16)
c0ebe874 11967 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11968 offset_reloc[1], offset_reloc[2], breg);
11969 else if (small_offset_p (0, align, offbits))
11970 {
11971 if (offbits == 0)
c0ebe874 11972 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11973 else
c0ebe874 11974 macro_build (NULL, s, fmt, op[0],
c8276761 11975 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11976 }
11977 else
11978 {
11979 if (tempreg == AT)
11980 used_at = 1;
11981 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11982 tempreg, breg, -1, offset_reloc[0],
11983 offset_reloc[1], offset_reloc[2]);
11984 if (offbits == 0)
c0ebe874 11985 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11986 else
c0ebe874 11987 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11988 }
11989 break;
11990 }
11991
11992 if (tempreg == AT)
11993 used_at = 1;
11994
252b5132
RH
11995 if (offset_expr.X_op != O_constant
11996 && offset_expr.X_op != O_symbol)
11997 {
1661c76c 11998 as_bad (_("expression too complex"));
252b5132
RH
11999 offset_expr.X_op = O_constant;
12000 }
12001
2051e8c4
MR
12002 if (HAVE_32BIT_ADDRESSES
12003 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12004 {
12005 char value [32];
12006
12007 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12008 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12009 }
2051e8c4 12010
252b5132
RH
12011 /* A constant expression in PIC code can be handled just as it
12012 is in non PIC code. */
aed1a261
RS
12013 if (offset_expr.X_op == O_constant)
12014 {
f2ae14a1
RS
12015 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12016 offbits == 0 ? 16 : offbits);
12017 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12018
f2ae14a1
RS
12019 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12020 if (breg != 0)
12021 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12022 tempreg, tempreg, breg);
7f3c4072 12023 if (offbits == 0)
dd6a37e7 12024 {
f2ae14a1 12025 if (offset_expr.X_add_number != 0)
dd6a37e7 12026 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12027 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12028 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12029 }
7f3c4072 12030 else if (offbits == 16)
c0ebe874 12031 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12032 else
c0ebe874 12033 macro_build (NULL, s, fmt, op[0],
c8276761 12034 (int) offset_expr.X_add_number, tempreg);
df58fc94 12035 }
7f3c4072 12036 else if (offbits != 16)
df58fc94 12037 {
7f3c4072 12038 /* The offset field is too narrow to be used for a low-part
2b0f3761 12039 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12040 register. */
12041 load_address (tempreg, &offset_expr, &used_at);
12042 if (breg != 0)
12043 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12044 tempreg, tempreg, breg);
7f3c4072 12045 if (offbits == 0)
c0ebe874 12046 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12047 else
c0ebe874 12048 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12049 }
12050 else if (mips_pic == NO_PIC)
252b5132
RH
12051 {
12052 /* If this is a reference to a GP relative symbol, and there
12053 is no base register, we want
c0ebe874 12054 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12055 Otherwise, if there is no base register, we want
12056 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12057 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12058 If we have a constant, we need two instructions anyhow,
12059 so we always use the latter form.
12060
12061 If we have a base register, and this is a reference to a
12062 GP relative symbol, we want
12063 addu $tempreg,$breg,$gp
c0ebe874 12064 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12065 Otherwise we want
12066 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12067 addu $tempreg,$tempreg,$breg
c0ebe874 12068 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12069 With a constant we always use the latter case.
76b3015f 12070
d6bc6245
TS
12071 With 64bit address space and no base register and $at usable,
12072 we want
12073 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12074 lui $at,<sym> (BFD_RELOC_HI16_S)
12075 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12076 dsll32 $tempreg,0
12077 daddu $tempreg,$at
c0ebe874 12078 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12079 If we have a base register, we want
12080 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12081 lui $at,<sym> (BFD_RELOC_HI16_S)
12082 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12083 daddu $at,$breg
12084 dsll32 $tempreg,0
12085 daddu $tempreg,$at
c0ebe874 12086 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12087
12088 Without $at we can't generate the optimal path for superscalar
12089 processors here since this would require two temporary registers.
12090 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12091 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12092 dsll $tempreg,16
12093 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12094 dsll $tempreg,16
c0ebe874 12095 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12096 If we have a base register, we want
12097 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12098 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12099 dsll $tempreg,16
12100 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12101 dsll $tempreg,16
12102 daddu $tempreg,$tempreg,$breg
c0ebe874 12103 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12104
6caf9ef4 12105 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12106 the same sequence as in 32bit address space. */
12107 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12108 {
aed1a261 12109 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12110 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12111 {
12112 relax_start (offset_expr.X_add_symbol);
12113 if (breg == 0)
12114 {
c0ebe874 12115 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12116 BFD_RELOC_GPREL16, mips_gp_register);
12117 }
12118 else
12119 {
12120 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12121 tempreg, breg, mips_gp_register);
c0ebe874 12122 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12123 BFD_RELOC_GPREL16, tempreg);
12124 }
12125 relax_switch ();
12126 }
d6bc6245 12127
741fe287 12128 if (used_at == 0 && mips_opts.at)
d6bc6245 12129 {
df58fc94 12130 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12131 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12132 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12133 BFD_RELOC_HI16_S);
12134 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12135 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12136 if (breg != 0)
67c0d1eb 12137 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12138 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12139 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12140 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12141 tempreg);
d6bc6245
TS
12142 used_at = 1;
12143 }
12144 else
12145 {
df58fc94 12146 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12147 BFD_RELOC_MIPS_HIGHEST);
12148 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12149 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12150 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12151 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12152 tempreg, BFD_RELOC_HI16_S);
df58fc94 12153 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12154 if (breg != 0)
67c0d1eb 12155 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12156 tempreg, tempreg, breg);
c0ebe874 12157 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12158 BFD_RELOC_LO16, tempreg);
d6bc6245 12159 }
6caf9ef4
TS
12160
12161 if (mips_relax.sequence)
12162 relax_end ();
8fc2e39e 12163 break;
d6bc6245 12164 }
256ab948 12165
252b5132
RH
12166 if (breg == 0)
12167 {
67c0d1eb 12168 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12169 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12170 {
4d7206a2 12171 relax_start (offset_expr.X_add_symbol);
c0ebe874 12172 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12173 mips_gp_register);
4d7206a2 12174 relax_switch ();
252b5132 12175 }
67c0d1eb 12176 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12177 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12178 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12179 if (mips_relax.sequence)
12180 relax_end ();
252b5132
RH
12181 }
12182 else
12183 {
67c0d1eb 12184 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12185 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12186 {
4d7206a2 12187 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12188 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12189 tempreg, breg, mips_gp_register);
c0ebe874 12190 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12191 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12192 relax_switch ();
252b5132 12193 }
67c0d1eb
RS
12194 macro_build_lui (&offset_expr, tempreg);
12195 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12196 tempreg, tempreg, breg);
c0ebe874 12197 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12198 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12199 if (mips_relax.sequence)
12200 relax_end ();
252b5132
RH
12201 }
12202 }
0a44bf69 12203 else if (!mips_big_got)
252b5132 12204 {
ed6fb7bd 12205 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12206
252b5132
RH
12207 /* If this is a reference to an external symbol, we want
12208 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12209 nop
c0ebe874 12210 <op> op[0],0($tempreg)
252b5132
RH
12211 Otherwise we want
12212 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12213 nop
12214 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12215 <op> op[0],0($tempreg)
f5040a92
AO
12216
12217 For NewABI, we want
12218 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12219 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12220
252b5132
RH
12221 If there is a base register, we add it to $tempreg before
12222 the <op>. If there is a constant, we stick it in the
12223 <op> instruction. We don't handle constants larger than
12224 16 bits, because we have no way to load the upper 16 bits
12225 (actually, we could handle them for the subset of cases
12226 in which we are not using $at). */
9c2799c2 12227 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12228 if (HAVE_NEWABI)
12229 {
67c0d1eb
RS
12230 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12231 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12232 if (breg != 0)
67c0d1eb 12233 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12234 tempreg, tempreg, breg);
c0ebe874 12235 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12236 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12237 break;
12238 }
252b5132
RH
12239 expr1.X_add_number = offset_expr.X_add_number;
12240 offset_expr.X_add_number = 0;
12241 if (expr1.X_add_number < -0x8000
12242 || expr1.X_add_number >= 0x8000)
12243 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12244 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12245 lw_reloc_type, mips_gp_register);
269137b2 12246 load_delay_nop ();
4d7206a2
RS
12247 relax_start (offset_expr.X_add_symbol);
12248 relax_switch ();
67c0d1eb
RS
12249 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12250 tempreg, BFD_RELOC_LO16);
4d7206a2 12251 relax_end ();
252b5132 12252 if (breg != 0)
67c0d1eb 12253 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12254 tempreg, tempreg, breg);
c0ebe874 12255 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12256 }
0a44bf69 12257 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12258 {
67c0d1eb 12259 int gpdelay;
252b5132
RH
12260
12261 /* If this is a reference to an external symbol, we want
12262 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12263 addu $tempreg,$tempreg,$gp
12264 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12265 <op> op[0],0($tempreg)
252b5132
RH
12266 Otherwise we want
12267 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12268 nop
12269 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12270 <op> op[0],0($tempreg)
252b5132
RH
12271 If there is a base register, we add it to $tempreg before
12272 the <op>. If there is a constant, we stick it in the
12273 <op> instruction. We don't handle constants larger than
12274 16 bits, because we have no way to load the upper 16 bits
12275 (actually, we could handle them for the subset of cases
f5040a92 12276 in which we are not using $at). */
9c2799c2 12277 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12278 expr1.X_add_number = offset_expr.X_add_number;
12279 offset_expr.X_add_number = 0;
12280 if (expr1.X_add_number < -0x8000
12281 || expr1.X_add_number >= 0x8000)
12282 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12283 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12284 relax_start (offset_expr.X_add_symbol);
df58fc94 12285 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12286 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12287 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12288 mips_gp_register);
12289 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12290 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12291 relax_switch ();
67c0d1eb
RS
12292 if (gpdelay)
12293 macro_build (NULL, "nop", "");
12294 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12295 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12296 load_delay_nop ();
67c0d1eb
RS
12297 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12298 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12299 relax_end ();
12300
252b5132 12301 if (breg != 0)
67c0d1eb 12302 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12303 tempreg, tempreg, breg);
c0ebe874 12304 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12305 }
0a44bf69 12306 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12307 {
f5040a92
AO
12308 /* If this is a reference to an external symbol, we want
12309 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12310 add $tempreg,$tempreg,$gp
12311 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12312 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12313 Otherwise, for local symbols, we want:
12314 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12315 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12316 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12317 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12318 offset_expr.X_add_number = 0;
12319 if (expr1.X_add_number < -0x8000
12320 || expr1.X_add_number >= 0x8000)
12321 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12322 relax_start (offset_expr.X_add_symbol);
df58fc94 12323 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12324 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12325 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12326 mips_gp_register);
12327 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12328 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12329 if (breg != 0)
67c0d1eb 12330 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12331 tempreg, tempreg, breg);
c0ebe874 12332 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12333
4d7206a2 12334 relax_switch ();
f5040a92 12335 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12336 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12337 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12338 if (breg != 0)
67c0d1eb 12339 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12340 tempreg, tempreg, breg);
c0ebe874 12341 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12342 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12343 relax_end ();
f5040a92 12344 }
252b5132
RH
12345 else
12346 abort ();
12347
252b5132
RH
12348 break;
12349
833794fc
MR
12350 case M_JRADDIUSP:
12351 gas_assert (mips_opts.micromips);
12352 gas_assert (mips_opts.insn32);
12353 start_noreorder ();
12354 macro_build (NULL, "jr", "s", RA);
c0ebe874 12355 expr1.X_add_number = op[0] << 2;
833794fc
MR
12356 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12357 end_noreorder ();
12358 break;
12359
12360 case M_JRC:
12361 gas_assert (mips_opts.micromips);
12362 gas_assert (mips_opts.insn32);
c0ebe874 12363 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12364 if (mips_opts.noreorder)
12365 macro_build (NULL, "nop", "");
12366 break;
12367
252b5132
RH
12368 case M_LI:
12369 case M_LI_S:
c0ebe874 12370 load_register (op[0], &imm_expr, 0);
8fc2e39e 12371 break;
252b5132
RH
12372
12373 case M_DLI:
c0ebe874 12374 load_register (op[0], &imm_expr, 1);
8fc2e39e 12375 break;
252b5132
RH
12376
12377 case M_LI_SS:
12378 if (imm_expr.X_op == O_constant)
12379 {
8fc2e39e 12380 used_at = 1;
67c0d1eb 12381 load_register (AT, &imm_expr, 0);
c0ebe874 12382 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12383 break;
12384 }
12385 else
12386 {
b0e6f033
RS
12387 gas_assert (imm_expr.X_op == O_absent
12388 && offset_expr.X_op == O_symbol
90ecf173
MR
12389 && strcmp (segment_name (S_GET_SEGMENT
12390 (offset_expr.X_add_symbol)),
12391 ".lit4") == 0
12392 && offset_expr.X_add_number == 0);
c0ebe874 12393 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12394 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12395 break;
252b5132
RH
12396 }
12397
12398 case M_LI_D:
ca4e0257
RS
12399 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12400 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12401 order 32 bits of the value and the low order 32 bits are either
12402 zero or in OFFSET_EXPR. */
b0e6f033 12403 if (imm_expr.X_op == O_constant)
252b5132 12404 {
bad1aba3 12405 if (GPR_SIZE == 64)
c0ebe874 12406 load_register (op[0], &imm_expr, 1);
252b5132
RH
12407 else
12408 {
12409 int hreg, lreg;
12410
12411 if (target_big_endian)
12412 {
c0ebe874
RS
12413 hreg = op[0];
12414 lreg = op[0] + 1;
252b5132
RH
12415 }
12416 else
12417 {
c0ebe874
RS
12418 hreg = op[0] + 1;
12419 lreg = op[0];
252b5132
RH
12420 }
12421
12422 if (hreg <= 31)
67c0d1eb 12423 load_register (hreg, &imm_expr, 0);
252b5132
RH
12424 if (lreg <= 31)
12425 {
12426 if (offset_expr.X_op == O_absent)
67c0d1eb 12427 move_register (lreg, 0);
252b5132
RH
12428 else
12429 {
9c2799c2 12430 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12431 load_register (lreg, &offset_expr, 0);
252b5132
RH
12432 }
12433 }
12434 }
8fc2e39e 12435 break;
252b5132 12436 }
b0e6f033 12437 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12438
12439 /* We know that sym is in the .rdata section. First we get the
12440 upper 16 bits of the address. */
12441 if (mips_pic == NO_PIC)
12442 {
67c0d1eb 12443 macro_build_lui (&offset_expr, AT);
8fc2e39e 12444 used_at = 1;
252b5132 12445 }
0a44bf69 12446 else
252b5132 12447 {
67c0d1eb
RS
12448 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12449 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12450 used_at = 1;
252b5132 12451 }
bdaaa2e1 12452
252b5132 12453 /* Now we load the register(s). */
bad1aba3 12454 if (GPR_SIZE == 64)
8fc2e39e
TS
12455 {
12456 used_at = 1;
c0ebe874
RS
12457 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12458 BFD_RELOC_LO16, AT);
8fc2e39e 12459 }
252b5132
RH
12460 else
12461 {
8fc2e39e 12462 used_at = 1;
c0ebe874
RS
12463 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12464 BFD_RELOC_LO16, AT);
12465 if (op[0] != RA)
252b5132
RH
12466 {
12467 /* FIXME: How in the world do we deal with the possible
12468 overflow here? */
12469 offset_expr.X_add_number += 4;
67c0d1eb 12470 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12471 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12472 }
12473 }
252b5132
RH
12474 break;
12475
12476 case M_LI_DD:
ca4e0257
RS
12477 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12478 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12479 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12480 the value and the low order 32 bits are either zero or in
12481 OFFSET_EXPR. */
b0e6f033 12482 if (imm_expr.X_op == O_constant)
252b5132 12483 {
8fc2e39e 12484 used_at = 1;
bad1aba3 12485 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12486 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12487 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12488 else
12489 {
351cdf24
MF
12490 if (ISA_HAS_MXHC1 (mips_opts.isa))
12491 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12492 else if (FPR_SIZE != 32)
12493 as_bad (_("Unable to generate `%s' compliant code "
12494 "without mthc1"),
12495 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12496 else
12497 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12498 if (offset_expr.X_op == O_absent)
c0ebe874 12499 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12500 else
12501 {
9c2799c2 12502 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12503 load_register (AT, &offset_expr, 0);
c0ebe874 12504 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12505 }
12506 }
12507 break;
12508 }
12509
b0e6f033
RS
12510 gas_assert (imm_expr.X_op == O_absent
12511 && offset_expr.X_op == O_symbol
90ecf173 12512 && offset_expr.X_add_number == 0);
252b5132
RH
12513 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12514 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12515 {
12516 op[2] = mips_gp_register;
f2ae14a1
RS
12517 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12518 offset_reloc[1] = BFD_RELOC_UNUSED;
12519 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12520 }
12521 else
12522 {
9c2799c2 12523 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12524 used_at = 1;
0a44bf69 12525 if (mips_pic != NO_PIC)
67c0d1eb
RS
12526 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12527 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12528 else
12529 {
12530 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12531 macro_build_lui (&offset_expr, AT);
252b5132 12532 }
bdaaa2e1 12533
c0ebe874 12534 op[2] = AT;
f2ae14a1
RS
12535 offset_reloc[0] = BFD_RELOC_LO16;
12536 offset_reloc[1] = BFD_RELOC_UNUSED;
12537 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12538 }
f2ae14a1
RS
12539 align = 8;
12540 /* Fall through */
c4a68bea 12541
252b5132
RH
12542 case M_L_DAB:
12543 /*
12544 * The MIPS assembler seems to check for X_add_number not
12545 * being double aligned and generating:
12546 * lui at,%hi(foo+1)
12547 * addu at,at,v1
12548 * addiu at,at,%lo(foo+1)
12549 * lwc1 f2,0(at)
12550 * lwc1 f3,4(at)
12551 * But, the resulting address is the same after relocation so why
12552 * generate the extra instruction?
12553 */
bdaaa2e1 12554 /* Itbl support may require additional care here. */
252b5132 12555 coproc = 1;
df58fc94 12556 fmt = "T,o(b)";
0aa27725 12557 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12558 {
12559 s = "ldc1";
df58fc94 12560 goto ld_st;
252b5132 12561 }
252b5132 12562 s = "lwc1";
252b5132
RH
12563 goto ldd_std;
12564
12565 case M_S_DAB:
df58fc94
RS
12566 gas_assert (!mips_opts.micromips);
12567 /* Itbl support may require additional care here. */
12568 coproc = 1;
12569 fmt = "T,o(b)";
0aa27725 12570 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12571 {
12572 s = "sdc1";
df58fc94 12573 goto ld_st;
252b5132 12574 }
252b5132 12575 s = "swc1";
252b5132
RH
12576 goto ldd_std;
12577
e407c74b
NC
12578 case M_LQ_AB:
12579 fmt = "t,o(b)";
12580 s = "lq";
12581 goto ld;
12582
12583 case M_SQ_AB:
12584 fmt = "t,o(b)";
12585 s = "sq";
12586 goto ld_st;
12587
252b5132 12588 case M_LD_AB:
df58fc94 12589 fmt = "t,o(b)";
bad1aba3 12590 if (GPR_SIZE == 64)
252b5132
RH
12591 {
12592 s = "ld";
12593 goto ld;
12594 }
252b5132 12595 s = "lw";
252b5132
RH
12596 goto ldd_std;
12597
12598 case M_SD_AB:
df58fc94 12599 fmt = "t,o(b)";
bad1aba3 12600 if (GPR_SIZE == 64)
252b5132
RH
12601 {
12602 s = "sd";
df58fc94 12603 goto ld_st;
252b5132 12604 }
252b5132 12605 s = "sw";
252b5132
RH
12606
12607 ldd_std:
f2ae14a1
RS
12608 /* Even on a big endian machine $fn comes before $fn+1. We have
12609 to adjust when loading from memory. We set coproc if we must
12610 load $fn+1 first. */
12611 /* Itbl support may require additional care here. */
12612 if (!target_big_endian)
12613 coproc = 0;
12614
c0ebe874 12615 breg = op[2];
f2ae14a1
RS
12616 if (small_offset_p (0, align, 16))
12617 {
12618 ep = &offset_expr;
12619 if (!small_offset_p (4, align, 16))
12620 {
12621 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12622 -1, offset_reloc[0], offset_reloc[1],
12623 offset_reloc[2]);
12624 expr1.X_add_number = 0;
12625 ep = &expr1;
12626 breg = AT;
12627 used_at = 1;
12628 offset_reloc[0] = BFD_RELOC_LO16;
12629 offset_reloc[1] = BFD_RELOC_UNUSED;
12630 offset_reloc[2] = BFD_RELOC_UNUSED;
12631 }
c0ebe874 12632 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12633 {
12634 ep->X_add_number += 4;
c0ebe874 12635 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12636 offset_reloc[1], offset_reloc[2], breg);
12637 ep->X_add_number -= 4;
c0ebe874 12638 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12639 offset_reloc[1], offset_reloc[2], breg);
12640 }
12641 else
12642 {
c0ebe874 12643 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12644 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12645 breg);
12646 ep->X_add_number += 4;
c0ebe874 12647 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12648 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12649 breg);
12650 }
12651 break;
12652 }
12653
252b5132
RH
12654 if (offset_expr.X_op != O_symbol
12655 && offset_expr.X_op != O_constant)
12656 {
1661c76c 12657 as_bad (_("expression too complex"));
252b5132
RH
12658 offset_expr.X_op = O_constant;
12659 }
12660
2051e8c4
MR
12661 if (HAVE_32BIT_ADDRESSES
12662 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12663 {
12664 char value [32];
12665
12666 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12667 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12668 }
2051e8c4 12669
90ecf173 12670 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12671 {
12672 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12673 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12674 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12675 If we have a base register, we use this
12676 addu $at,$breg,$gp
c0ebe874
RS
12677 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12678 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12679 If this is not a GP relative symbol, we want
12680 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12681 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12682 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12683 If there is a base register, we add it to $at after the
12684 lui instruction. If there is a constant, we always use
12685 the last case. */
39a59cf8
MR
12686 if (offset_expr.X_op == O_symbol
12687 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12688 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12689 {
4d7206a2 12690 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12691 if (breg == 0)
12692 {
c9914766 12693 tempreg = mips_gp_register;
252b5132
RH
12694 }
12695 else
12696 {
67c0d1eb 12697 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12698 AT, breg, mips_gp_register);
252b5132 12699 tempreg = AT;
252b5132
RH
12700 used_at = 1;
12701 }
12702
beae10d5 12703 /* Itbl support may require additional care here. */
c0ebe874 12704 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12705 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12706 offset_expr.X_add_number += 4;
12707
12708 /* Set mips_optimize to 2 to avoid inserting an
12709 undesired nop. */
12710 hold_mips_optimize = mips_optimize;
12711 mips_optimize = 2;
beae10d5 12712 /* Itbl support may require additional care here. */
c0ebe874 12713 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12714 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12715 mips_optimize = hold_mips_optimize;
12716
4d7206a2 12717 relax_switch ();
252b5132 12718
0970e49e 12719 offset_expr.X_add_number -= 4;
252b5132 12720 }
8fc2e39e 12721 used_at = 1;
f2ae14a1
RS
12722 if (offset_high_part (offset_expr.X_add_number, 16)
12723 != offset_high_part (offset_expr.X_add_number + 4, 16))
12724 {
12725 load_address (AT, &offset_expr, &used_at);
12726 offset_expr.X_op = O_constant;
12727 offset_expr.X_add_number = 0;
12728 }
12729 else
12730 macro_build_lui (&offset_expr, AT);
252b5132 12731 if (breg != 0)
67c0d1eb 12732 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12733 /* Itbl support may require additional care here. */
c0ebe874 12734 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12735 BFD_RELOC_LO16, AT);
252b5132
RH
12736 /* FIXME: How do we handle overflow here? */
12737 offset_expr.X_add_number += 4;
beae10d5 12738 /* Itbl support may require additional care here. */
c0ebe874 12739 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12740 BFD_RELOC_LO16, AT);
4d7206a2
RS
12741 if (mips_relax.sequence)
12742 relax_end ();
bdaaa2e1 12743 }
0a44bf69 12744 else if (!mips_big_got)
252b5132 12745 {
252b5132
RH
12746 /* If this is a reference to an external symbol, we want
12747 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12748 nop
c0ebe874
RS
12749 <op> op[0],0($at)
12750 <op> op[0]+1,4($at)
252b5132
RH
12751 Otherwise we want
12752 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12753 nop
c0ebe874
RS
12754 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12755 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12756 If there is a base register we add it to $at before the
12757 lwc1 instructions. If there is a constant we include it
12758 in the lwc1 instructions. */
12759 used_at = 1;
12760 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12761 if (expr1.X_add_number < -0x8000
12762 || expr1.X_add_number >= 0x8000 - 4)
12763 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12764 load_got_offset (AT, &offset_expr);
269137b2 12765 load_delay_nop ();
252b5132 12766 if (breg != 0)
67c0d1eb 12767 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12768
12769 /* Set mips_optimize to 2 to avoid inserting an undesired
12770 nop. */
12771 hold_mips_optimize = mips_optimize;
12772 mips_optimize = 2;
4d7206a2 12773
beae10d5 12774 /* Itbl support may require additional care here. */
4d7206a2 12775 relax_start (offset_expr.X_add_symbol);
c0ebe874 12776 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12777 BFD_RELOC_LO16, AT);
4d7206a2 12778 expr1.X_add_number += 4;
c0ebe874 12779 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12780 BFD_RELOC_LO16, AT);
4d7206a2 12781 relax_switch ();
c0ebe874 12782 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12783 BFD_RELOC_LO16, AT);
4d7206a2 12784 offset_expr.X_add_number += 4;
c0ebe874 12785 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12786 BFD_RELOC_LO16, AT);
4d7206a2 12787 relax_end ();
252b5132 12788
4d7206a2 12789 mips_optimize = hold_mips_optimize;
252b5132 12790 }
0a44bf69 12791 else if (mips_big_got)
252b5132 12792 {
67c0d1eb 12793 int gpdelay;
252b5132
RH
12794
12795 /* If this is a reference to an external symbol, we want
12796 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12797 addu $at,$at,$gp
12798 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12799 nop
c0ebe874
RS
12800 <op> op[0],0($at)
12801 <op> op[0]+1,4($at)
252b5132
RH
12802 Otherwise we want
12803 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12804 nop
c0ebe874
RS
12805 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12806 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12807 If there is a base register we add it to $at before the
12808 lwc1 instructions. If there is a constant we include it
12809 in the lwc1 instructions. */
12810 used_at = 1;
12811 expr1.X_add_number = offset_expr.X_add_number;
12812 offset_expr.X_add_number = 0;
12813 if (expr1.X_add_number < -0x8000
12814 || expr1.X_add_number >= 0x8000 - 4)
12815 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12816 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12817 relax_start (offset_expr.X_add_symbol);
df58fc94 12818 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12819 AT, BFD_RELOC_MIPS_GOT_HI16);
12820 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12821 AT, AT, mips_gp_register);
67c0d1eb 12822 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12823 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12824 load_delay_nop ();
252b5132 12825 if (breg != 0)
67c0d1eb 12826 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12827 /* Itbl support may require additional care here. */
c0ebe874 12828 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12829 BFD_RELOC_LO16, AT);
252b5132
RH
12830 expr1.X_add_number += 4;
12831
12832 /* Set mips_optimize to 2 to avoid inserting an undesired
12833 nop. */
12834 hold_mips_optimize = mips_optimize;
12835 mips_optimize = 2;
beae10d5 12836 /* Itbl support may require additional care here. */
c0ebe874 12837 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12838 BFD_RELOC_LO16, AT);
252b5132
RH
12839 mips_optimize = hold_mips_optimize;
12840 expr1.X_add_number -= 4;
12841
4d7206a2
RS
12842 relax_switch ();
12843 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12844 if (gpdelay)
12845 macro_build (NULL, "nop", "");
12846 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12847 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12848 load_delay_nop ();
252b5132 12849 if (breg != 0)
67c0d1eb 12850 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12851 /* Itbl support may require additional care here. */
c0ebe874 12852 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12853 BFD_RELOC_LO16, AT);
4d7206a2 12854 offset_expr.X_add_number += 4;
252b5132
RH
12855
12856 /* Set mips_optimize to 2 to avoid inserting an undesired
12857 nop. */
12858 hold_mips_optimize = mips_optimize;
12859 mips_optimize = 2;
beae10d5 12860 /* Itbl support may require additional care here. */
c0ebe874 12861 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12862 BFD_RELOC_LO16, AT);
252b5132 12863 mips_optimize = hold_mips_optimize;
4d7206a2 12864 relax_end ();
252b5132 12865 }
252b5132
RH
12866 else
12867 abort ();
12868
252b5132 12869 break;
3739860c 12870
dd6a37e7 12871 case M_SAA_AB:
dd6a37e7 12872 s = "saa";
0db377d0 12873 goto saa_saad;
dd6a37e7 12874 case M_SAAD_AB:
dd6a37e7 12875 s = "saad";
0db377d0
MR
12876 saa_saad:
12877 gas_assert (!mips_opts.micromips);
7f3c4072 12878 offbits = 0;
dd6a37e7
AP
12879 fmt = "t,(b)";
12880 goto ld_st;
12881
252b5132
RH
12882 /* New code added to support COPZ instructions.
12883 This code builds table entries out of the macros in mip_opcodes.
12884 R4000 uses interlocks to handle coproc delays.
12885 Other chips (like the R3000) require nops to be inserted for delays.
12886
f72c8c98 12887 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12888 In order to fill delay slots for non-interlocked chips,
12889 we must have a way to specify delays based on the coprocessor.
12890 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12891 What are the side-effects of the cop instruction?
12892 What cache support might we have and what are its effects?
12893 Both coprocessor & memory require delays. how long???
bdaaa2e1 12894 What registers are read/set/modified?
252b5132
RH
12895
12896 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12897 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12898
12899 case M_COP0:
12900 s = "c0";
12901 goto copz;
12902 case M_COP1:
12903 s = "c1";
12904 goto copz;
12905 case M_COP2:
12906 s = "c2";
12907 goto copz;
12908 case M_COP3:
12909 s = "c3";
12910 copz:
df58fc94 12911 gas_assert (!mips_opts.micromips);
252b5132
RH
12912 /* For now we just do C (same as Cz). The parameter will be
12913 stored in insn_opcode by mips_ip. */
c8276761 12914 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12915 break;
252b5132 12916
ea1fb5dc 12917 case M_MOVE:
c0ebe874 12918 move_register (op[0], op[1]);
8fc2e39e 12919 break;
ea1fb5dc 12920
833794fc
MR
12921 case M_MOVEP:
12922 gas_assert (mips_opts.micromips);
12923 gas_assert (mips_opts.insn32);
c0ebe874
RS
12924 move_register (micromips_to_32_reg_h_map1[op[0]],
12925 micromips_to_32_reg_m_map[op[1]]);
12926 move_register (micromips_to_32_reg_h_map2[op[0]],
12927 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12928 break;
12929
252b5132
RH
12930 case M_DMUL:
12931 dbl = 1;
1a0670f3 12932 /* Fall through. */
252b5132 12933 case M_MUL:
e407c74b 12934 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12935 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12936 op[2]);
e407c74b
NC
12937 else
12938 {
c0ebe874
RS
12939 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12940 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12941 }
8fc2e39e 12942 break;
252b5132
RH
12943
12944 case M_DMUL_I:
12945 dbl = 1;
1a0670f3 12946 /* Fall through. */
252b5132
RH
12947 case M_MUL_I:
12948 /* The MIPS assembler some times generates shifts and adds. I'm
12949 not trying to be that fancy. GCC should do this for us
12950 anyway. */
8fc2e39e 12951 used_at = 1;
67c0d1eb 12952 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12953 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12954 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12955 break;
12956
12957 case M_DMULO_I:
12958 dbl = 1;
1a0670f3 12959 /* Fall through. */
252b5132
RH
12960 case M_MULO_I:
12961 imm = 1;
12962 goto do_mulo;
12963
12964 case M_DMULO:
12965 dbl = 1;
1a0670f3 12966 /* Fall through. */
252b5132
RH
12967 case M_MULO:
12968 do_mulo:
7d10b47d 12969 start_noreorder ();
8fc2e39e 12970 used_at = 1;
252b5132 12971 if (imm)
67c0d1eb 12972 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12973 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12974 op[1], imm ? AT : op[2]);
12975 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12976 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12977 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12978 if (mips_trap)
c0ebe874 12979 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12980 else
12981 {
df58fc94
RS
12982 if (mips_opts.micromips)
12983 micromips_label_expr (&label_expr);
12984 else
12985 label_expr.X_add_number = 8;
c0ebe874 12986 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12987 macro_build (NULL, "nop", "");
df58fc94
RS
12988 macro_build (NULL, "break", BRK_FMT, 6);
12989 if (mips_opts.micromips)
12990 micromips_add_label ();
252b5132 12991 }
7d10b47d 12992 end_noreorder ();
c0ebe874 12993 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12994 break;
12995
12996 case M_DMULOU_I:
12997 dbl = 1;
1a0670f3 12998 /* Fall through. */
252b5132
RH
12999 case M_MULOU_I:
13000 imm = 1;
13001 goto do_mulou;
13002
13003 case M_DMULOU:
13004 dbl = 1;
1a0670f3 13005 /* Fall through. */
252b5132
RH
13006 case M_MULOU:
13007 do_mulou:
7d10b47d 13008 start_noreorder ();
8fc2e39e 13009 used_at = 1;
252b5132 13010 if (imm)
67c0d1eb
RS
13011 load_register (AT, &imm_expr, dbl);
13012 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13013 op[1], imm ? AT : op[2]);
df58fc94 13014 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13015 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13016 if (mips_trap)
df58fc94 13017 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13018 else
13019 {
df58fc94
RS
13020 if (mips_opts.micromips)
13021 micromips_label_expr (&label_expr);
13022 else
13023 label_expr.X_add_number = 8;
13024 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13025 macro_build (NULL, "nop", "");
df58fc94
RS
13026 macro_build (NULL, "break", BRK_FMT, 6);
13027 if (mips_opts.micromips)
13028 micromips_add_label ();
252b5132 13029 }
7d10b47d 13030 end_noreorder ();
252b5132
RH
13031 break;
13032
771c7ce4 13033 case M_DROL:
fef14a42 13034 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13035 {
c0ebe874 13036 if (op[0] == op[1])
82dd0097
CD
13037 {
13038 tempreg = AT;
13039 used_at = 1;
13040 }
13041 else
c0ebe874
RS
13042 tempreg = op[0];
13043 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13044 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13045 break;
82dd0097 13046 }
8fc2e39e 13047 used_at = 1;
c0ebe874
RS
13048 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13049 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13050 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13051 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13052 break;
13053
252b5132 13054 case M_ROL:
fef14a42 13055 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13056 {
c0ebe874 13057 if (op[0] == op[1])
82dd0097
CD
13058 {
13059 tempreg = AT;
13060 used_at = 1;
13061 }
13062 else
c0ebe874
RS
13063 tempreg = op[0];
13064 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13065 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13066 break;
82dd0097 13067 }
8fc2e39e 13068 used_at = 1;
c0ebe874
RS
13069 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13070 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13071 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13072 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13073 break;
13074
771c7ce4
TS
13075 case M_DROL_I:
13076 {
13077 unsigned int rot;
e0471c16
TS
13078 const char *l;
13079 const char *rr;
771c7ce4 13080
771c7ce4 13081 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13082 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13083 {
13084 rot = (64 - rot) & 0x3f;
13085 if (rot >= 32)
c0ebe874 13086 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13087 else
c0ebe874 13088 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13089 break;
60b63b72 13090 }
483fc7cd 13091 if (rot == 0)
483fc7cd 13092 {
c0ebe874 13093 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13094 break;
483fc7cd 13095 }
82dd0097 13096 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13097 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13098 rot &= 0x1f;
8fc2e39e 13099 used_at = 1;
c0ebe874
RS
13100 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13101 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13102 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13103 }
13104 break;
13105
252b5132 13106 case M_ROL_I:
771c7ce4
TS
13107 {
13108 unsigned int rot;
13109
771c7ce4 13110 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13111 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13112 {
c0ebe874
RS
13113 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13114 (32 - rot) & 0x1f);
8fc2e39e 13115 break;
60b63b72 13116 }
483fc7cd 13117 if (rot == 0)
483fc7cd 13118 {
c0ebe874 13119 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13120 break;
483fc7cd 13121 }
8fc2e39e 13122 used_at = 1;
c0ebe874
RS
13123 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13124 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13125 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13126 }
13127 break;
13128
13129 case M_DROR:
fef14a42 13130 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13131 {
c0ebe874 13132 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13133 break;
82dd0097 13134 }
8fc2e39e 13135 used_at = 1;
c0ebe874
RS
13136 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13137 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13138 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13139 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13140 break;
13141
13142 case M_ROR:
fef14a42 13143 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13144 {
c0ebe874 13145 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13146 break;
82dd0097 13147 }
8fc2e39e 13148 used_at = 1;
c0ebe874
RS
13149 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13150 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13151 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13152 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13153 break;
13154
771c7ce4
TS
13155 case M_DROR_I:
13156 {
13157 unsigned int rot;
e0471c16
TS
13158 const char *l;
13159 const char *rr;
771c7ce4 13160
771c7ce4 13161 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13162 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13163 {
13164 if (rot >= 32)
c0ebe874 13165 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13166 else
c0ebe874 13167 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13168 break;
82dd0097 13169 }
483fc7cd 13170 if (rot == 0)
483fc7cd 13171 {
c0ebe874 13172 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13173 break;
483fc7cd 13174 }
91d6fa6a 13175 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13176 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13177 rot &= 0x1f;
8fc2e39e 13178 used_at = 1;
c0ebe874
RS
13179 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13180 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13181 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13182 }
13183 break;
13184
252b5132 13185 case M_ROR_I:
771c7ce4
TS
13186 {
13187 unsigned int rot;
13188
771c7ce4 13189 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13190 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13191 {
c0ebe874 13192 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13193 break;
82dd0097 13194 }
483fc7cd 13195 if (rot == 0)
483fc7cd 13196 {
c0ebe874 13197 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13198 break;
483fc7cd 13199 }
8fc2e39e 13200 used_at = 1;
c0ebe874
RS
13201 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13202 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13203 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13204 }
252b5132
RH
13205 break;
13206
252b5132 13207 case M_SEQ:
c0ebe874
RS
13208 if (op[1] == 0)
13209 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13210 else if (op[2] == 0)
13211 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13212 else
13213 {
c0ebe874
RS
13214 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13215 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13216 }
8fc2e39e 13217 break;
252b5132
RH
13218
13219 case M_SEQ_I:
b0e6f033 13220 if (imm_expr.X_add_number == 0)
252b5132 13221 {
c0ebe874 13222 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13223 break;
252b5132 13224 }
c0ebe874 13225 if (op[1] == 0)
252b5132 13226 {
1661c76c 13227 as_warn (_("instruction %s: result is always false"),
252b5132 13228 ip->insn_mo->name);
c0ebe874 13229 move_register (op[0], 0);
8fc2e39e 13230 break;
252b5132 13231 }
dd3cbb7e
NC
13232 if (CPU_HAS_SEQ (mips_opts.arch)
13233 && -512 <= imm_expr.X_add_number
13234 && imm_expr.X_add_number < 512)
13235 {
c0ebe874 13236 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13237 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13238 break;
13239 }
b0e6f033 13240 if (imm_expr.X_add_number >= 0
252b5132 13241 && imm_expr.X_add_number < 0x10000)
c0ebe874 13242 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13243 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13244 && imm_expr.X_add_number < 0)
13245 {
13246 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13247 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13248 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13249 }
dd3cbb7e
NC
13250 else if (CPU_HAS_SEQ (mips_opts.arch))
13251 {
13252 used_at = 1;
bad1aba3 13253 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13254 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13255 break;
13256 }
252b5132
RH
13257 else
13258 {
bad1aba3 13259 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13260 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13261 used_at = 1;
13262 }
c0ebe874 13263 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13264 break;
252b5132 13265
c0ebe874 13266 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13267 s = "slt";
13268 goto sge;
13269 case M_SGEU:
13270 s = "sltu";
13271 sge:
c0ebe874
RS
13272 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13273 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13274 break;
252b5132 13275
c0ebe874 13276 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13277 case M_SGEU_I:
b0e6f033 13278 if (imm_expr.X_add_number >= -0x8000
252b5132 13279 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13280 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13281 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13282 else
13283 {
bad1aba3 13284 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13285 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13286 op[0], op[1], AT);
252b5132
RH
13287 used_at = 1;
13288 }
c0ebe874 13289 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13290 break;
252b5132 13291
c0ebe874 13292 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13293 s = "slt";
13294 goto sgt;
13295 case M_SGTU:
13296 s = "sltu";
13297 sgt:
c0ebe874 13298 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13299 break;
252b5132 13300
c0ebe874 13301 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13302 s = "slt";
13303 goto sgti;
13304 case M_SGTU_I:
13305 s = "sltu";
13306 sgti:
8fc2e39e 13307 used_at = 1;
bad1aba3 13308 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13309 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13310 break;
13311
c0ebe874 13312 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13313 s = "slt";
13314 goto sle;
13315 case M_SLEU:
13316 s = "sltu";
13317 sle:
c0ebe874
RS
13318 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13319 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13320 break;
252b5132 13321
c0ebe874 13322 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13323 s = "slt";
13324 goto slei;
13325 case M_SLEU_I:
13326 s = "sltu";
13327 slei:
8fc2e39e 13328 used_at = 1;
bad1aba3 13329 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13330 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13331 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13332 break;
13333
13334 case M_SLT_I:
b0e6f033 13335 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13336 && imm_expr.X_add_number < 0x8000)
13337 {
c0ebe874
RS
13338 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13339 BFD_RELOC_LO16);
8fc2e39e 13340 break;
252b5132 13341 }
8fc2e39e 13342 used_at = 1;
bad1aba3 13343 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13344 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13345 break;
13346
13347 case M_SLTU_I:
b0e6f033 13348 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13349 && imm_expr.X_add_number < 0x8000)
13350 {
c0ebe874 13351 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13352 BFD_RELOC_LO16);
8fc2e39e 13353 break;
252b5132 13354 }
8fc2e39e 13355 used_at = 1;
bad1aba3 13356 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13357 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13358 break;
13359
13360 case M_SNE:
c0ebe874
RS
13361 if (op[1] == 0)
13362 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13363 else if (op[2] == 0)
13364 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13365 else
13366 {
c0ebe874
RS
13367 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13368 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13369 }
8fc2e39e 13370 break;
252b5132
RH
13371
13372 case M_SNE_I:
b0e6f033 13373 if (imm_expr.X_add_number == 0)
252b5132 13374 {
c0ebe874 13375 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13376 break;
252b5132 13377 }
c0ebe874 13378 if (op[1] == 0)
252b5132 13379 {
1661c76c 13380 as_warn (_("instruction %s: result is always true"),
252b5132 13381 ip->insn_mo->name);
bad1aba3 13382 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13383 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13384 break;
252b5132 13385 }
dd3cbb7e
NC
13386 if (CPU_HAS_SEQ (mips_opts.arch)
13387 && -512 <= imm_expr.X_add_number
13388 && imm_expr.X_add_number < 512)
13389 {
c0ebe874 13390 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13391 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13392 break;
13393 }
b0e6f033 13394 if (imm_expr.X_add_number >= 0
252b5132
RH
13395 && imm_expr.X_add_number < 0x10000)
13396 {
c0ebe874
RS
13397 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13398 BFD_RELOC_LO16);
252b5132 13399 }
b0e6f033 13400 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13401 && imm_expr.X_add_number < 0)
13402 {
13403 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13404 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13405 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13406 }
dd3cbb7e
NC
13407 else if (CPU_HAS_SEQ (mips_opts.arch))
13408 {
13409 used_at = 1;
bad1aba3 13410 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13411 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13412 break;
13413 }
252b5132
RH
13414 else
13415 {
bad1aba3 13416 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13417 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13418 used_at = 1;
13419 }
c0ebe874 13420 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13421 break;
252b5132 13422
df58fc94
RS
13423 case M_SUB_I:
13424 s = "addi";
13425 s2 = "sub";
13426 goto do_subi;
13427 case M_SUBU_I:
13428 s = "addiu";
13429 s2 = "subu";
13430 goto do_subi;
252b5132
RH
13431 case M_DSUB_I:
13432 dbl = 1;
df58fc94
RS
13433 s = "daddi";
13434 s2 = "dsub";
13435 if (!mips_opts.micromips)
13436 goto do_subi;
b0e6f033 13437 if (imm_expr.X_add_number > -0x200
df58fc94 13438 && imm_expr.X_add_number <= 0x200)
252b5132 13439 {
b0e6f033
RS
13440 macro_build (NULL, s, "t,r,.", op[0], op[1],
13441 (int) -imm_expr.X_add_number);
8fc2e39e 13442 break;
252b5132 13443 }
df58fc94 13444 goto do_subi_i;
252b5132
RH
13445 case M_DSUBU_I:
13446 dbl = 1;
df58fc94
RS
13447 s = "daddiu";
13448 s2 = "dsubu";
13449 do_subi:
b0e6f033 13450 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13451 && imm_expr.X_add_number <= 0x8000)
13452 {
13453 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13454 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13455 break;
252b5132 13456 }
df58fc94 13457 do_subi_i:
8fc2e39e 13458 used_at = 1;
67c0d1eb 13459 load_register (AT, &imm_expr, dbl);
c0ebe874 13460 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13461 break;
13462
13463 case M_TEQ_I:
13464 s = "teq";
13465 goto trap;
13466 case M_TGE_I:
13467 s = "tge";
13468 goto trap;
13469 case M_TGEU_I:
13470 s = "tgeu";
13471 goto trap;
13472 case M_TLT_I:
13473 s = "tlt";
13474 goto trap;
13475 case M_TLTU_I:
13476 s = "tltu";
13477 goto trap;
13478 case M_TNE_I:
13479 s = "tne";
13480 trap:
8fc2e39e 13481 used_at = 1;
bad1aba3 13482 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13483 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13484 break;
13485
252b5132 13486 case M_TRUNCWS:
43841e91 13487 case M_TRUNCWD:
df58fc94 13488 gas_assert (!mips_opts.micromips);
0aa27725 13489 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13490 used_at = 1;
252b5132
RH
13491
13492 /*
13493 * Is the double cfc1 instruction a bug in the mips assembler;
13494 * or is there a reason for it?
13495 */
7d10b47d 13496 start_noreorder ();
c0ebe874
RS
13497 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13498 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13499 macro_build (NULL, "nop", "");
252b5132 13500 expr1.X_add_number = 3;
c0ebe874 13501 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13502 expr1.X_add_number = 2;
67c0d1eb
RS
13503 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13504 macro_build (NULL, "ctc1", "t,G", AT, RA);
13505 macro_build (NULL, "nop", "");
13506 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13507 op[0], op[1]);
13508 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13509 macro_build (NULL, "nop", "");
7d10b47d 13510 end_noreorder ();
252b5132
RH
13511 break;
13512
f2ae14a1 13513 case M_ULH_AB:
252b5132 13514 s = "lb";
df58fc94
RS
13515 s2 = "lbu";
13516 off = 1;
13517 goto uld_st;
f2ae14a1 13518 case M_ULHU_AB:
252b5132 13519 s = "lbu";
df58fc94
RS
13520 s2 = "lbu";
13521 off = 1;
13522 goto uld_st;
f2ae14a1 13523 case M_ULW_AB:
df58fc94
RS
13524 s = "lwl";
13525 s2 = "lwr";
7f3c4072 13526 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13527 off = 3;
13528 goto uld_st;
f2ae14a1 13529 case M_ULD_AB:
252b5132
RH
13530 s = "ldl";
13531 s2 = "ldr";
7f3c4072 13532 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13533 off = 7;
df58fc94 13534 goto uld_st;
f2ae14a1 13535 case M_USH_AB:
df58fc94
RS
13536 s = "sb";
13537 s2 = "sb";
13538 off = 1;
13539 ust = 1;
13540 goto uld_st;
f2ae14a1 13541 case M_USW_AB:
df58fc94
RS
13542 s = "swl";
13543 s2 = "swr";
7f3c4072 13544 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13545 off = 3;
df58fc94
RS
13546 ust = 1;
13547 goto uld_st;
f2ae14a1 13548 case M_USD_AB:
df58fc94
RS
13549 s = "sdl";
13550 s2 = "sdr";
7f3c4072 13551 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13552 off = 7;
13553 ust = 1;
13554
13555 uld_st:
c0ebe874 13556 breg = op[2];
f2ae14a1 13557 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13558 ep = &offset_expr;
13559 expr1.X_add_number = 0;
f2ae14a1 13560 if (large_offset)
df58fc94
RS
13561 {
13562 used_at = 1;
13563 tempreg = AT;
f2ae14a1
RS
13564 if (small_offset_p (0, align, 16))
13565 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13566 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13567 else
13568 {
13569 load_address (tempreg, ep, &used_at);
13570 if (breg != 0)
13571 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13572 tempreg, tempreg, breg);
13573 }
13574 offset_reloc[0] = BFD_RELOC_LO16;
13575 offset_reloc[1] = BFD_RELOC_UNUSED;
13576 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13577 breg = tempreg;
c0ebe874 13578 tempreg = op[0];
df58fc94
RS
13579 ep = &expr1;
13580 }
c0ebe874 13581 else if (!ust && op[0] == breg)
8fc2e39e
TS
13582 {
13583 used_at = 1;
13584 tempreg = AT;
13585 }
252b5132 13586 else
c0ebe874 13587 tempreg = op[0];
af22f5b2 13588
df58fc94
RS
13589 if (off == 1)
13590 goto ulh_sh;
252b5132 13591
90ecf173 13592 if (!target_big_endian)
df58fc94 13593 ep->X_add_number += off;
f2ae14a1 13594 if (offbits == 12)
c8276761 13595 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13596 else
13597 macro_build (ep, s, "t,o(b)", tempreg, -1,
13598 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13599
90ecf173 13600 if (!target_big_endian)
df58fc94 13601 ep->X_add_number -= off;
252b5132 13602 else
df58fc94 13603 ep->X_add_number += off;
f2ae14a1 13604 if (offbits == 12)
df58fc94 13605 macro_build (NULL, s2, "t,~(b)",
c8276761 13606 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13607 else
13608 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13609 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13610
df58fc94 13611 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13612 if (!ust && op[0] != tempreg)
df58fc94
RS
13613 {
13614 /* Protect second load's delay slot. */
13615 load_delay_nop ();
c0ebe874 13616 move_register (op[0], tempreg);
df58fc94 13617 }
8fc2e39e 13618 break;
252b5132 13619
df58fc94 13620 ulh_sh:
d6bc6245 13621 used_at = 1;
df58fc94
RS
13622 if (target_big_endian == ust)
13623 ep->X_add_number += off;
c0ebe874 13624 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13625 macro_build (ep, s, "t,o(b)", tempreg, -1,
13626 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13627
13628 /* For halfword transfers we need a temporary register to shuffle
13629 bytes. Unfortunately for M_USH_A we have none available before
13630 the next store as AT holds the base address. We deal with this
13631 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13632 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13633 if (ust)
c0ebe874 13634 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13635
13636 if (target_big_endian == ust)
13637 ep->X_add_number -= off;
252b5132 13638 else
df58fc94 13639 ep->X_add_number += off;
f2ae14a1
RS
13640 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13641 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13642
df58fc94 13643 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13644 if (ust && large_offset)
df58fc94
RS
13645 {
13646 if (target_big_endian)
13647 ep->X_add_number += off;
13648 else
13649 ep->X_add_number -= off;
f2ae14a1
RS
13650 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13651 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13652 }
13653 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13654 if (!ust || large_offset)
df58fc94 13655 {
c0ebe874 13656 tempreg = !large_offset ? AT : op[0];
df58fc94 13657 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13658 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13659 }
252b5132
RH
13660 break;
13661
13662 default:
13663 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13664 are added dynamically. */
1661c76c 13665 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13666 break;
13667 }
741fe287 13668 if (!mips_opts.at && used_at)
1661c76c 13669 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13670}
13671
13672/* Implement macros in mips16 mode. */
13673
13674static void
17a2f251 13675mips16_macro (struct mips_cl_insn *ip)
252b5132 13676{
c0ebe874 13677 const struct mips_operand_array *operands;
252b5132 13678 int mask;
c0ebe874 13679 int tmp;
252b5132
RH
13680 expressionS expr1;
13681 int dbl;
13682 const char *s, *s2, *s3;
c0ebe874
RS
13683 unsigned int op[MAX_OPERANDS];
13684 unsigned int i;
252b5132
RH
13685
13686 mask = ip->insn_mo->mask;
13687
c0ebe874
RS
13688 operands = insn_operands (ip);
13689 for (i = 0; i < MAX_OPERANDS; i++)
13690 if (operands->operand[i])
13691 op[i] = insn_extract_operand (ip, operands->operand[i]);
13692 else
13693 op[i] = -1;
252b5132 13694
252b5132
RH
13695 expr1.X_op = O_constant;
13696 expr1.X_op_symbol = NULL;
13697 expr1.X_add_symbol = NULL;
13698 expr1.X_add_number = 1;
13699
13700 dbl = 0;
13701
13702 switch (mask)
13703 {
13704 default:
b37df7c4 13705 abort ();
252b5132
RH
13706
13707 case M_DDIV_3:
13708 dbl = 1;
1a0670f3 13709 /* Fall through. */
252b5132
RH
13710 case M_DIV_3:
13711 s = "mflo";
13712 goto do_div3;
13713 case M_DREM_3:
13714 dbl = 1;
1a0670f3 13715 /* Fall through. */
252b5132
RH
13716 case M_REM_3:
13717 s = "mfhi";
13718 do_div3:
7d10b47d 13719 start_noreorder ();
d8722d76 13720 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13721 expr1.X_add_number = 2;
c0ebe874 13722 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13723 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13724
252b5132
RH
13725 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13726 since that causes an overflow. We should do that as well,
13727 but I don't see how to do the comparisons without a temporary
13728 register. */
7d10b47d 13729 end_noreorder ();
c0ebe874 13730 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13731 break;
13732
13733 case M_DIVU_3:
13734 s = "divu";
13735 s2 = "mflo";
13736 goto do_divu3;
13737 case M_REMU_3:
13738 s = "divu";
13739 s2 = "mfhi";
13740 goto do_divu3;
13741 case M_DDIVU_3:
13742 s = "ddivu";
13743 s2 = "mflo";
13744 goto do_divu3;
13745 case M_DREMU_3:
13746 s = "ddivu";
13747 s2 = "mfhi";
13748 do_divu3:
7d10b47d 13749 start_noreorder ();
d8722d76 13750 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13751 expr1.X_add_number = 2;
c0ebe874 13752 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13753 macro_build (NULL, "break", "6", 7);
7d10b47d 13754 end_noreorder ();
c0ebe874 13755 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13756 break;
13757
13758 case M_DMUL:
13759 dbl = 1;
1a0670f3 13760 /* Fall through. */
252b5132 13761 case M_MUL:
c0ebe874
RS
13762 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13763 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13764 break;
252b5132
RH
13765
13766 case M_DSUBU_I:
13767 dbl = 1;
13768 goto do_subu;
13769 case M_SUBU_I:
13770 do_subu:
252b5132 13771 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13772 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13773 break;
13774
13775 case M_SUBU_I_2:
252b5132 13776 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13777 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13778 break;
13779
13780 case M_DSUBU_I_2:
252b5132 13781 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13782 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13783 break;
13784
13785 case M_BEQ:
13786 s = "cmp";
13787 s2 = "bteqz";
13788 goto do_branch;
13789 case M_BNE:
13790 s = "cmp";
13791 s2 = "btnez";
13792 goto do_branch;
13793 case M_BLT:
13794 s = "slt";
13795 s2 = "btnez";
13796 goto do_branch;
13797 case M_BLTU:
13798 s = "sltu";
13799 s2 = "btnez";
13800 goto do_branch;
13801 case M_BLE:
13802 s = "slt";
13803 s2 = "bteqz";
13804 goto do_reverse_branch;
13805 case M_BLEU:
13806 s = "sltu";
13807 s2 = "bteqz";
13808 goto do_reverse_branch;
13809 case M_BGE:
13810 s = "slt";
13811 s2 = "bteqz";
13812 goto do_branch;
13813 case M_BGEU:
13814 s = "sltu";
13815 s2 = "bteqz";
13816 goto do_branch;
13817 case M_BGT:
13818 s = "slt";
13819 s2 = "btnez";
13820 goto do_reverse_branch;
13821 case M_BGTU:
13822 s = "sltu";
13823 s2 = "btnez";
13824
13825 do_reverse_branch:
c0ebe874
RS
13826 tmp = op[1];
13827 op[1] = op[0];
13828 op[0] = tmp;
252b5132
RH
13829
13830 do_branch:
c0ebe874 13831 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13832 macro_build (&offset_expr, s2, "p");
252b5132
RH
13833 break;
13834
13835 case M_BEQ_I:
13836 s = "cmpi";
13837 s2 = "bteqz";
13838 s3 = "x,U";
13839 goto do_branch_i;
13840 case M_BNE_I:
13841 s = "cmpi";
13842 s2 = "btnez";
13843 s3 = "x,U";
13844 goto do_branch_i;
13845 case M_BLT_I:
13846 s = "slti";
13847 s2 = "btnez";
13848 s3 = "x,8";
13849 goto do_branch_i;
13850 case M_BLTU_I:
13851 s = "sltiu";
13852 s2 = "btnez";
13853 s3 = "x,8";
13854 goto do_branch_i;
13855 case M_BLE_I:
13856 s = "slti";
13857 s2 = "btnez";
13858 s3 = "x,8";
13859 goto do_addone_branch_i;
13860 case M_BLEU_I:
13861 s = "sltiu";
13862 s2 = "btnez";
13863 s3 = "x,8";
13864 goto do_addone_branch_i;
13865 case M_BGE_I:
13866 s = "slti";
13867 s2 = "bteqz";
13868 s3 = "x,8";
13869 goto do_branch_i;
13870 case M_BGEU_I:
13871 s = "sltiu";
13872 s2 = "bteqz";
13873 s3 = "x,8";
13874 goto do_branch_i;
13875 case M_BGT_I:
13876 s = "slti";
13877 s2 = "bteqz";
13878 s3 = "x,8";
13879 goto do_addone_branch_i;
13880 case M_BGTU_I:
13881 s = "sltiu";
13882 s2 = "bteqz";
13883 s3 = "x,8";
13884
13885 do_addone_branch_i:
252b5132
RH
13886 ++imm_expr.X_add_number;
13887
13888 do_branch_i:
c0ebe874 13889 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13890 macro_build (&offset_expr, s2, "p");
252b5132
RH
13891 break;
13892
13893 case M_ABS:
13894 expr1.X_add_number = 0;
c0ebe874
RS
13895 macro_build (&expr1, "slti", "x,8", op[1]);
13896 if (op[0] != op[1])
13897 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13898 expr1.X_add_number = 2;
67c0d1eb 13899 macro_build (&expr1, "bteqz", "p");
c0ebe874 13900 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13901 break;
252b5132
RH
13902 }
13903}
13904
14daeee3
RS
13905/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13906 opcode bits in *OPCODE_EXTRA. */
13907
13908static struct mips_opcode *
13909mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13910 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13911{
13912 char *name, *dot, *p;
13913 unsigned int mask, suffix;
da8bca91 13914 ssize_t opend;
14daeee3
RS
13915 struct mips_opcode *insn;
13916
13917 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13918 name = xstrndup (start, length);
14daeee3
RS
13919
13920 /* Look up the instruction as-is. */
13921 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13922 if (insn)
e1fa0163 13923 goto end;
14daeee3
RS
13924
13925 dot = strchr (name, '.');
13926 if (dot && dot[1])
13927 {
13928 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13929 p = mips_parse_vu0_channels (dot + 1, &mask);
13930 if (*p == 0 && mask != 0)
13931 {
13932 *dot = 0;
13933 insn = (struct mips_opcode *) hash_find (hash, name);
13934 *dot = '.';
13935 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13936 {
13937 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13938 goto end;
14daeee3
RS
13939 }
13940 }
13941 }
13942
13943 if (mips_opts.micromips)
13944 {
13945 /* See if there's an instruction size override suffix,
13946 either `16' or `32', at the end of the mnemonic proper,
13947 that defines the operation, i.e. before the first `.'
13948 character if any. Strip it and retry. */
13949 opend = dot != NULL ? dot - name : length;
13950 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13951 suffix = 2;
13952 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13953 suffix = 4;
13954 else
13955 suffix = 0;
13956 if (suffix)
13957 {
39334a61 13958 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 13959 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13960 if (insn)
14daeee3
RS
13961 {
13962 forced_insn_length = suffix;
e1fa0163 13963 goto end;
14daeee3
RS
13964 }
13965 }
13966 }
13967
e1fa0163
NC
13968 insn = NULL;
13969 end:
13970 free (name);
13971 return insn;
14daeee3
RS
13972}
13973
77bd4346 13974/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13975 is a macro, set imm_expr and offset_expr to the values associated
13976 with "I" and "A" operands respectively. Otherwise store the value
13977 of the relocatable field (if any) in offset_expr. In both cases
13978 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13979
13980static void
60f20e8b 13981mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13982{
60f20e8b 13983 const struct mips_opcode *first, *past;
df58fc94 13984 struct hash_control *hash;
a92713e6 13985 char format;
14daeee3 13986 size_t end;
a92713e6 13987 struct mips_operand_token *tokens;
14daeee3 13988 unsigned int opcode_extra;
252b5132 13989
df58fc94
RS
13990 if (mips_opts.micromips)
13991 {
13992 hash = micromips_op_hash;
13993 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13994 }
13995 else
13996 {
13997 hash = op_hash;
13998 past = &mips_opcodes[NUMOPCODES];
13999 }
14000 forced_insn_length = 0;
14daeee3 14001 opcode_extra = 0;
252b5132 14002
df58fc94 14003 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14004 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14005 continue;
bdaaa2e1 14006
60f20e8b
RS
14007 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14008 if (first == NULL)
252b5132 14009 {
1661c76c 14010 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14011 return;
252b5132
RH
14012 }
14013
60f20e8b 14014 if (strcmp (first->name, "li.s") == 0)
a92713e6 14015 format = 'f';
60f20e8b 14016 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14017 format = 'd';
14018 else
14019 format = 0;
14020 tokens = mips_parse_arguments (str + end, format);
14021 if (!tokens)
14022 return;
14023
60f20e8b
RS
14024 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14025 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14026 set_insn_error (0, _("invalid operands"));
df58fc94 14027
e3de51ce 14028 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14029}
14030
77bd4346
RS
14031/* As for mips_ip, but used when assembling MIPS16 code.
14032 Also set forced_insn_length to the resulting instruction size in
14033 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14034
14035static void
60f20e8b 14036mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14037{
1a00e612 14038 char *end, *s, c;
60f20e8b 14039 struct mips_opcode *first;
a92713e6 14040 struct mips_operand_token *tokens;
3fb49709 14041 unsigned int l;
252b5132 14042
25499ac7 14043 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14044 ;
1a00e612
RS
14045 end = s;
14046 c = *end;
3fb49709
MR
14047
14048 l = 0;
1a00e612 14049 switch (c)
252b5132
RH
14050 {
14051 case '\0':
14052 break;
14053
14054 case ' ':
1a00e612 14055 s++;
252b5132
RH
14056 break;
14057
14058 case '.':
3fb49709
MR
14059 s++;
14060 if (*s == 't')
252b5132 14061 {
3fb49709
MR
14062 l = 2;
14063 s++;
252b5132 14064 }
3fb49709 14065 else if (*s == 'e')
252b5132 14066 {
3fb49709
MR
14067 l = 4;
14068 s++;
252b5132 14069 }
3fb49709
MR
14070 if (*s == '\0')
14071 break;
14072 else if (*s++ == ' ')
14073 break;
1661c76c 14074 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14075 return;
14076 }
3fb49709 14077 forced_insn_length = l;
252b5132 14078
1a00e612 14079 *end = 0;
60f20e8b 14080 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14081 *end = c;
14082
60f20e8b 14083 if (!first)
252b5132 14084 {
1661c76c 14085 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14086 return;
14087 }
14088
a92713e6
RS
14089 tokens = mips_parse_arguments (s, 0);
14090 if (!tokens)
14091 return;
14092
60f20e8b 14093 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14094 set_insn_error (0, _("invalid operands"));
252b5132 14095
e3de51ce 14096 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14097}
14098
b886a2ab
RS
14099/* Marshal immediate value VAL for an extended MIPS16 instruction.
14100 NBITS is the number of significant bits in VAL. */
14101
14102static unsigned long
14103mips16_immed_extend (offsetT val, unsigned int nbits)
14104{
14105 int extval;
25499ac7
MR
14106
14107 extval = 0;
14108 val &= (1U << nbits) - 1;
14109 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14110 {
14111 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14112 val &= 0x1f;
14113 }
14114 else if (nbits == 15)
14115 {
14116 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14117 val &= 0xf;
14118 }
25499ac7 14119 else if (nbits == 6)
b886a2ab
RS
14120 {
14121 extval = ((val & 0x1f) << 6) | (val & 0x20);
14122 val = 0;
14123 }
14124 return (extval << 16) | val;
14125}
14126
3ccad066
RS
14127/* Like decode_mips16_operand, but require the operand to be defined and
14128 require it to be an integer. */
14129
14130static const struct mips_int_operand *
14131mips16_immed_operand (int type, bfd_boolean extended_p)
14132{
14133 const struct mips_operand *operand;
14134
14135 operand = decode_mips16_operand (type, extended_p);
14136 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14137 abort ();
14138 return (const struct mips_int_operand *) operand;
14139}
14140
14141/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14142
14143static bfd_boolean
14144mips16_immed_in_range_p (const struct mips_int_operand *operand,
14145 bfd_reloc_code_real_type reloc, offsetT sval)
14146{
14147 int min_val, max_val;
14148
14149 min_val = mips_int_operand_min (operand);
14150 max_val = mips_int_operand_max (operand);
14151 if (reloc != BFD_RELOC_UNUSED)
14152 {
14153 if (min_val < 0)
14154 sval = SEXT_16BIT (sval);
14155 else
14156 sval &= 0xffff;
14157 }
14158
14159 return (sval >= min_val
14160 && sval <= max_val
14161 && (sval & ((1 << operand->shift) - 1)) == 0);
14162}
14163
5c04167a
RS
14164/* Install immediate value VAL into MIPS16 instruction *INSN,
14165 extending it if necessary. The instruction in *INSN may
14166 already be extended.
14167
43c0598f
RS
14168 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14169 if none. In the former case, VAL is a 16-bit number with no
14170 defined signedness.
14171
14172 TYPE is the type of the immediate field. USER_INSN_LENGTH
14173 is the length that the user requested, or 0 if none. */
252b5132
RH
14174
14175static void
3b4dbbbf 14176mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14177 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14178 unsigned int user_insn_length, unsigned long *insn)
252b5132 14179{
3ccad066
RS
14180 const struct mips_int_operand *operand;
14181 unsigned int uval, length;
252b5132 14182
3ccad066
RS
14183 operand = mips16_immed_operand (type, FALSE);
14184 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14185 {
14186 /* We need an extended instruction. */
14187 if (user_insn_length == 2)
14188 as_bad_where (file, line, _("invalid unextended operand value"));
14189 else
14190 *insn |= MIPS16_EXTEND;
14191 }
14192 else if (user_insn_length == 4)
14193 {
14194 /* The operand doesn't force an unextended instruction to be extended.
14195 Warn if the user wanted an extended instruction anyway. */
14196 *insn |= MIPS16_EXTEND;
14197 as_warn_where (file, line,
14198 _("extended operand requested but not required"));
14199 }
252b5132 14200
3ccad066
RS
14201 length = mips16_opcode_length (*insn);
14202 if (length == 4)
252b5132 14203 {
3ccad066
RS
14204 operand = mips16_immed_operand (type, TRUE);
14205 if (!mips16_immed_in_range_p (operand, reloc, val))
14206 as_bad_where (file, line,
14207 _("operand value out of range for instruction"));
252b5132 14208 }
3ccad066 14209 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14210 if (length == 2 || operand->root.lsb != 0)
3ccad066 14211 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14212 else
3ccad066 14213 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14214}
14215\f
d6f16593 14216struct percent_op_match
ad8d3bb3 14217{
5e0116d5
RS
14218 const char *str;
14219 bfd_reloc_code_real_type reloc;
d6f16593
MR
14220};
14221
14222static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14223{
5e0116d5 14224 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14225 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14226 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14227 {"%call16", BFD_RELOC_MIPS_CALL16},
14228 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14229 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14230 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14231 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14232 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14233 {"%got", BFD_RELOC_MIPS_GOT16},
14234 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14235 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14236 {"%half", BFD_RELOC_16},
14237 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14238 {"%higher", BFD_RELOC_MIPS_HIGHER},
14239 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14240 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14241 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14242 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14243 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14244 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14245 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14246 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14247 {"%hi", BFD_RELOC_HI16_S},
14248 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14249 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14250};
14251
d6f16593
MR
14252static const struct percent_op_match mips16_percent_op[] =
14253{
14254 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14255 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14256 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14257 {"%got", BFD_RELOC_MIPS16_GOT16},
14258 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14259 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14260 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14261 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14262 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14263 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14264 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14265 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14266 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14267};
14268
252b5132 14269
5e0116d5
RS
14270/* Return true if *STR points to a relocation operator. When returning true,
14271 move *STR over the operator and store its relocation code in *RELOC.
14272 Leave both *STR and *RELOC alone when returning false. */
14273
14274static bfd_boolean
17a2f251 14275parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14276{
d6f16593
MR
14277 const struct percent_op_match *percent_op;
14278 size_t limit, i;
14279
14280 if (mips_opts.mips16)
14281 {
14282 percent_op = mips16_percent_op;
14283 limit = ARRAY_SIZE (mips16_percent_op);
14284 }
14285 else
14286 {
14287 percent_op = mips_percent_op;
14288 limit = ARRAY_SIZE (mips_percent_op);
14289 }
76b3015f 14290
d6f16593 14291 for (i = 0; i < limit; i++)
5e0116d5 14292 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14293 {
3f98094e
DJ
14294 int len = strlen (percent_op[i].str);
14295
14296 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14297 continue;
14298
5e0116d5
RS
14299 *str += strlen (percent_op[i].str);
14300 *reloc = percent_op[i].reloc;
394f9b3a 14301
5e0116d5
RS
14302 /* Check whether the output BFD supports this relocation.
14303 If not, issue an error and fall back on something safe. */
14304 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14305 {
20203fb9 14306 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14307 percent_op[i].str);
01a3f561 14308 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14309 }
5e0116d5 14310 return TRUE;
394f9b3a 14311 }
5e0116d5 14312 return FALSE;
394f9b3a 14313}
ad8d3bb3 14314
ad8d3bb3 14315
5e0116d5
RS
14316/* Parse string STR as a 16-bit relocatable operand. Store the
14317 expression in *EP and the relocations in the array starting
14318 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14319
01a3f561 14320 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14321
5e0116d5 14322static size_t
17a2f251
TS
14323my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14324 char *str)
ad8d3bb3 14325{
5e0116d5
RS
14326 bfd_reloc_code_real_type reversed_reloc[3];
14327 size_t reloc_index, i;
09b8f35a
RS
14328 int crux_depth, str_depth;
14329 char *crux;
5e0116d5
RS
14330
14331 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14332 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14333 of the main expression and with CRUX_DEPTH containing the number
14334 of open brackets at that point. */
14335 reloc_index = -1;
14336 str_depth = 0;
14337 do
fb1b3232 14338 {
09b8f35a
RS
14339 reloc_index++;
14340 crux = str;
14341 crux_depth = str_depth;
14342
14343 /* Skip over whitespace and brackets, keeping count of the number
14344 of brackets. */
14345 while (*str == ' ' || *str == '\t' || *str == '(')
14346 if (*str++ == '(')
14347 str_depth++;
5e0116d5 14348 }
09b8f35a
RS
14349 while (*str == '%'
14350 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14351 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14352
09b8f35a 14353 my_getExpression (ep, crux);
5e0116d5 14354 str = expr_end;
394f9b3a 14355
5e0116d5 14356 /* Match every open bracket. */
09b8f35a 14357 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14358 if (*str++ == ')')
09b8f35a 14359 crux_depth--;
394f9b3a 14360
09b8f35a 14361 if (crux_depth > 0)
20203fb9 14362 as_bad (_("unclosed '('"));
394f9b3a 14363
5e0116d5 14364 expr_end = str;
252b5132 14365
01a3f561 14366 if (reloc_index != 0)
64bdfcaf
RS
14367 {
14368 prev_reloc_op_frag = frag_now;
14369 for (i = 0; i < reloc_index; i++)
14370 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14371 }
fb1b3232 14372
5e0116d5 14373 return reloc_index;
252b5132
RH
14374}
14375
14376static void
17a2f251 14377my_getExpression (expressionS *ep, char *str)
252b5132
RH
14378{
14379 char *save_in;
14380
14381 save_in = input_line_pointer;
14382 input_line_pointer = str;
14383 expression (ep);
14384 expr_end = input_line_pointer;
14385 input_line_pointer = save_in;
252b5132
RH
14386}
14387
6d4af3c2 14388const char *
17a2f251 14389md_atof (int type, char *litP, int *sizeP)
252b5132 14390{
499ac353 14391 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14392}
14393
14394void
17a2f251 14395md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14396{
14397 if (target_big_endian)
14398 number_to_chars_bigendian (buf, val, n);
14399 else
14400 number_to_chars_littleendian (buf, val, n);
14401}
14402\f
e013f690
TS
14403static int support_64bit_objects(void)
14404{
14405 const char **list, **l;
aa3d8fdf 14406 int yes;
e013f690
TS
14407
14408 list = bfd_target_list ();
14409 for (l = list; *l != NULL; l++)
aeffff67
RS
14410 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14411 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14412 break;
aa3d8fdf 14413 yes = (*l != NULL);
e013f690 14414 free (list);
aa3d8fdf 14415 return yes;
e013f690
TS
14416}
14417
316f5878
RS
14418/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14419 NEW_VALUE. Warn if another value was already specified. Note:
14420 we have to defer parsing the -march and -mtune arguments in order
14421 to handle 'from-abi' correctly, since the ABI might be specified
14422 in a later argument. */
14423
14424static void
17a2f251 14425mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14426{
14427 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14428 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14429 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14430 new_value);
14431
14432 *string_ptr = new_value;
14433}
14434
252b5132 14435int
17b9d67d 14436md_parse_option (int c, const char *arg)
252b5132 14437{
c6278170
RS
14438 unsigned int i;
14439
14440 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14441 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14442 {
919731af 14443 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14444 c == mips_ases[i].option_on);
14445 return 1;
14446 }
14447
252b5132
RH
14448 switch (c)
14449 {
119d663a
NC
14450 case OPTION_CONSTRUCT_FLOATS:
14451 mips_disable_float_construction = 0;
14452 break;
bdaaa2e1 14453
119d663a
NC
14454 case OPTION_NO_CONSTRUCT_FLOATS:
14455 mips_disable_float_construction = 1;
14456 break;
bdaaa2e1 14457
252b5132
RH
14458 case OPTION_TRAP:
14459 mips_trap = 1;
14460 break;
14461
14462 case OPTION_BREAK:
14463 mips_trap = 0;
14464 break;
14465
14466 case OPTION_EB:
14467 target_big_endian = 1;
14468 break;
14469
14470 case OPTION_EL:
14471 target_big_endian = 0;
14472 break;
14473
14474 case 'O':
4ffff32f
TS
14475 if (arg == NULL)
14476 mips_optimize = 1;
14477 else if (arg[0] == '0')
14478 mips_optimize = 0;
14479 else if (arg[0] == '1')
252b5132
RH
14480 mips_optimize = 1;
14481 else
14482 mips_optimize = 2;
14483 break;
14484
14485 case 'g':
14486 if (arg == NULL)
14487 mips_debug = 2;
14488 else
14489 mips_debug = atoi (arg);
252b5132
RH
14490 break;
14491
14492 case OPTION_MIPS1:
0b35dfee 14493 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14494 break;
14495
14496 case OPTION_MIPS2:
0b35dfee 14497 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14498 break;
14499
14500 case OPTION_MIPS3:
0b35dfee 14501 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14502 break;
14503
14504 case OPTION_MIPS4:
0b35dfee 14505 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14506 break;
14507
84ea6cf2 14508 case OPTION_MIPS5:
0b35dfee 14509 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14510 break;
14511
e7af610e 14512 case OPTION_MIPS32:
0b35dfee 14513 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14514 break;
14515
af7ee8bf 14516 case OPTION_MIPS32R2:
0b35dfee 14517 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14518 break;
14519
ae52f483 14520 case OPTION_MIPS32R3:
0ae19f05 14521 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14522 break;
14523
14524 case OPTION_MIPS32R5:
0ae19f05 14525 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14526 break;
14527
7361da2c
AB
14528 case OPTION_MIPS32R6:
14529 file_mips_opts.isa = ISA_MIPS32R6;
14530 break;
14531
5f74bc13 14532 case OPTION_MIPS64R2:
0b35dfee 14533 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14534 break;
14535
ae52f483 14536 case OPTION_MIPS64R3:
0ae19f05 14537 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14538 break;
14539
14540 case OPTION_MIPS64R5:
0ae19f05 14541 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14542 break;
14543
7361da2c
AB
14544 case OPTION_MIPS64R6:
14545 file_mips_opts.isa = ISA_MIPS64R6;
14546 break;
14547
84ea6cf2 14548 case OPTION_MIPS64:
0b35dfee 14549 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14550 break;
14551
ec68c924 14552 case OPTION_MTUNE:
316f5878
RS
14553 mips_set_option_string (&mips_tune_string, arg);
14554 break;
ec68c924 14555
316f5878
RS
14556 case OPTION_MARCH:
14557 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14558 break;
14559
14560 case OPTION_M4650:
316f5878
RS
14561 mips_set_option_string (&mips_arch_string, "4650");
14562 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14563 break;
14564
14565 case OPTION_NO_M4650:
14566 break;
14567
14568 case OPTION_M4010:
316f5878
RS
14569 mips_set_option_string (&mips_arch_string, "4010");
14570 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14571 break;
14572
14573 case OPTION_NO_M4010:
14574 break;
14575
14576 case OPTION_M4100:
316f5878
RS
14577 mips_set_option_string (&mips_arch_string, "4100");
14578 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14579 break;
14580
14581 case OPTION_NO_M4100:
14582 break;
14583
252b5132 14584 case OPTION_M3900:
316f5878
RS
14585 mips_set_option_string (&mips_arch_string, "3900");
14586 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14587 break;
bdaaa2e1 14588
252b5132
RH
14589 case OPTION_NO_M3900:
14590 break;
14591
df58fc94 14592 case OPTION_MICROMIPS:
919731af 14593 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14594 {
14595 as_bad (_("-mmicromips cannot be used with -mips16"));
14596 return 0;
14597 }
919731af 14598 file_mips_opts.micromips = 1;
df58fc94
RS
14599 mips_no_prev_insn ();
14600 break;
14601
14602 case OPTION_NO_MICROMIPS:
919731af 14603 file_mips_opts.micromips = 0;
df58fc94
RS
14604 mips_no_prev_insn ();
14605 break;
14606
252b5132 14607 case OPTION_MIPS16:
919731af 14608 if (file_mips_opts.micromips == 1)
df58fc94
RS
14609 {
14610 as_bad (_("-mips16 cannot be used with -micromips"));
14611 return 0;
14612 }
919731af 14613 file_mips_opts.mips16 = 1;
7d10b47d 14614 mips_no_prev_insn ();
252b5132
RH
14615 break;
14616
14617 case OPTION_NO_MIPS16:
919731af 14618 file_mips_opts.mips16 = 0;
7d10b47d 14619 mips_no_prev_insn ();
252b5132
RH
14620 break;
14621
6a32d874
CM
14622 case OPTION_FIX_24K:
14623 mips_fix_24k = 1;
14624 break;
14625
14626 case OPTION_NO_FIX_24K:
14627 mips_fix_24k = 0;
14628 break;
14629
a8d14a88
CM
14630 case OPTION_FIX_RM7000:
14631 mips_fix_rm7000 = 1;
14632 break;
14633
14634 case OPTION_NO_FIX_RM7000:
14635 mips_fix_rm7000 = 0;
14636 break;
14637
c67a084a
NC
14638 case OPTION_FIX_LOONGSON2F_JUMP:
14639 mips_fix_loongson2f_jump = TRUE;
14640 break;
14641
14642 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14643 mips_fix_loongson2f_jump = FALSE;
14644 break;
14645
14646 case OPTION_FIX_LOONGSON2F_NOP:
14647 mips_fix_loongson2f_nop = TRUE;
14648 break;
14649
14650 case OPTION_NO_FIX_LOONGSON2F_NOP:
14651 mips_fix_loongson2f_nop = FALSE;
14652 break;
14653
d766e8ec
RS
14654 case OPTION_FIX_VR4120:
14655 mips_fix_vr4120 = 1;
60b63b72
RS
14656 break;
14657
d766e8ec
RS
14658 case OPTION_NO_FIX_VR4120:
14659 mips_fix_vr4120 = 0;
60b63b72
RS
14660 break;
14661
7d8e00cf
RS
14662 case OPTION_FIX_VR4130:
14663 mips_fix_vr4130 = 1;
14664 break;
14665
14666 case OPTION_NO_FIX_VR4130:
14667 mips_fix_vr4130 = 0;
14668 break;
14669
d954098f
DD
14670 case OPTION_FIX_CN63XXP1:
14671 mips_fix_cn63xxp1 = TRUE;
14672 break;
14673
14674 case OPTION_NO_FIX_CN63XXP1:
14675 mips_fix_cn63xxp1 = FALSE;
14676 break;
14677
4a6a3df4
AO
14678 case OPTION_RELAX_BRANCH:
14679 mips_relax_branch = 1;
14680 break;
14681
14682 case OPTION_NO_RELAX_BRANCH:
14683 mips_relax_branch = 0;
14684 break;
14685
8b10b0b3
MR
14686 case OPTION_IGNORE_BRANCH_ISA:
14687 mips_ignore_branch_isa = TRUE;
14688 break;
14689
14690 case OPTION_NO_IGNORE_BRANCH_ISA:
14691 mips_ignore_branch_isa = FALSE;
14692 break;
14693
833794fc 14694 case OPTION_INSN32:
919731af 14695 file_mips_opts.insn32 = TRUE;
833794fc
MR
14696 break;
14697
14698 case OPTION_NO_INSN32:
919731af 14699 file_mips_opts.insn32 = FALSE;
833794fc
MR
14700 break;
14701
aa6975fb
ILT
14702 case OPTION_MSHARED:
14703 mips_in_shared = TRUE;
14704 break;
14705
14706 case OPTION_MNO_SHARED:
14707 mips_in_shared = FALSE;
14708 break;
14709
aed1a261 14710 case OPTION_MSYM32:
919731af 14711 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14712 break;
14713
14714 case OPTION_MNO_SYM32:
919731af 14715 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14716 break;
14717
252b5132
RH
14718 /* When generating ELF code, we permit -KPIC and -call_shared to
14719 select SVR4_PIC, and -non_shared to select no PIC. This is
14720 intended to be compatible with Irix 5. */
14721 case OPTION_CALL_SHARED:
252b5132 14722 mips_pic = SVR4_PIC;
143d77c5 14723 mips_abicalls = TRUE;
252b5132
RH
14724 break;
14725
861fb55a 14726 case OPTION_CALL_NONPIC:
861fb55a
DJ
14727 mips_pic = NO_PIC;
14728 mips_abicalls = TRUE;
14729 break;
14730
252b5132 14731 case OPTION_NON_SHARED:
252b5132 14732 mips_pic = NO_PIC;
143d77c5 14733 mips_abicalls = FALSE;
252b5132
RH
14734 break;
14735
44075ae2
TS
14736 /* The -xgot option tells the assembler to use 32 bit offsets
14737 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14738 compatibility. */
14739 case OPTION_XGOT:
14740 mips_big_got = 1;
14741 break;
14742
14743 case 'G':
6caf9ef4
TS
14744 g_switch_value = atoi (arg);
14745 g_switch_seen = 1;
252b5132
RH
14746 break;
14747
34ba82a8
TS
14748 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14749 and -mabi=64. */
252b5132 14750 case OPTION_32:
f3ded42a 14751 mips_abi = O32_ABI;
252b5132
RH
14752 break;
14753
e013f690 14754 case OPTION_N32:
316f5878 14755 mips_abi = N32_ABI;
e013f690 14756 break;
252b5132 14757
e013f690 14758 case OPTION_64:
316f5878 14759 mips_abi = N64_ABI;
f43abd2b 14760 if (!support_64bit_objects())
1661c76c 14761 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14762 break;
14763
c97ef257 14764 case OPTION_GP32:
bad1aba3 14765 file_mips_opts.gp = 32;
c97ef257
AH
14766 break;
14767
14768 case OPTION_GP64:
bad1aba3 14769 file_mips_opts.gp = 64;
c97ef257 14770 break;
252b5132 14771
ca4e0257 14772 case OPTION_FP32:
0b35dfee 14773 file_mips_opts.fp = 32;
316f5878
RS
14774 break;
14775
351cdf24
MF
14776 case OPTION_FPXX:
14777 file_mips_opts.fp = 0;
14778 break;
14779
316f5878 14780 case OPTION_FP64:
0b35dfee 14781 file_mips_opts.fp = 64;
ca4e0257
RS
14782 break;
14783
351cdf24
MF
14784 case OPTION_ODD_SPREG:
14785 file_mips_opts.oddspreg = 1;
14786 break;
14787
14788 case OPTION_NO_ODD_SPREG:
14789 file_mips_opts.oddspreg = 0;
14790 break;
14791
037b32b9 14792 case OPTION_SINGLE_FLOAT:
0b35dfee 14793 file_mips_opts.single_float = 1;
037b32b9
AN
14794 break;
14795
14796 case OPTION_DOUBLE_FLOAT:
0b35dfee 14797 file_mips_opts.single_float = 0;
037b32b9
AN
14798 break;
14799
14800 case OPTION_SOFT_FLOAT:
0b35dfee 14801 file_mips_opts.soft_float = 1;
037b32b9
AN
14802 break;
14803
14804 case OPTION_HARD_FLOAT:
0b35dfee 14805 file_mips_opts.soft_float = 0;
037b32b9
AN
14806 break;
14807
252b5132 14808 case OPTION_MABI:
e013f690 14809 if (strcmp (arg, "32") == 0)
316f5878 14810 mips_abi = O32_ABI;
e013f690 14811 else if (strcmp (arg, "o64") == 0)
316f5878 14812 mips_abi = O64_ABI;
e013f690 14813 else if (strcmp (arg, "n32") == 0)
316f5878 14814 mips_abi = N32_ABI;
e013f690
TS
14815 else if (strcmp (arg, "64") == 0)
14816 {
316f5878 14817 mips_abi = N64_ABI;
e013f690 14818 if (! support_64bit_objects())
1661c76c 14819 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14820 "format"));
14821 }
14822 else if (strcmp (arg, "eabi") == 0)
316f5878 14823 mips_abi = EABI_ABI;
e013f690 14824 else
da0e507f
TS
14825 {
14826 as_fatal (_("invalid abi -mabi=%s"), arg);
14827 return 0;
14828 }
252b5132
RH
14829 break;
14830
6b76fefe 14831 case OPTION_M7000_HILO_FIX:
b34976b6 14832 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14833 break;
14834
9ee72ff1 14835 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14836 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14837 break;
14838
ecb4347a 14839 case OPTION_MDEBUG:
b34976b6 14840 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14841 break;
14842
14843 case OPTION_NO_MDEBUG:
b34976b6 14844 mips_flag_mdebug = FALSE;
ecb4347a 14845 break;
dcd410fe
RO
14846
14847 case OPTION_PDR:
14848 mips_flag_pdr = TRUE;
14849 break;
14850
14851 case OPTION_NO_PDR:
14852 mips_flag_pdr = FALSE;
14853 break;
0a44bf69
RS
14854
14855 case OPTION_MVXWORKS_PIC:
14856 mips_pic = VXWORKS_PIC;
14857 break;
ecb4347a 14858
ba92f887
MR
14859 case OPTION_NAN:
14860 if (strcmp (arg, "2008") == 0)
7361da2c 14861 mips_nan2008 = 1;
ba92f887 14862 else if (strcmp (arg, "legacy") == 0)
7361da2c 14863 mips_nan2008 = 0;
ba92f887
MR
14864 else
14865 {
1661c76c 14866 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14867 return 0;
14868 }
14869 break;
14870
252b5132
RH
14871 default:
14872 return 0;
14873 }
14874
c67a084a
NC
14875 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14876
252b5132
RH
14877 return 1;
14878}
316f5878 14879\f
919731af 14880/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14881
316f5878 14882static void
17a2f251 14883mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14884{
14885 if (info != 0)
fef14a42 14886 mips_tune = info->cpu;
316f5878 14887}
80cc45a5 14888
34ba82a8 14889
252b5132 14890void
17a2f251 14891mips_after_parse_args (void)
e9670677 14892{
fef14a42
TS
14893 const struct mips_cpu_info *arch_info = 0;
14894 const struct mips_cpu_info *tune_info = 0;
14895
e9670677 14896 /* GP relative stuff not working for PE */
6caf9ef4 14897 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14898 {
6caf9ef4 14899 if (g_switch_seen && g_switch_value != 0)
1661c76c 14900 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14901 g_switch_value = 0;
14902 }
14903
cac012d6
AO
14904 if (mips_abi == NO_ABI)
14905 mips_abi = MIPS_DEFAULT_ABI;
14906
919731af 14907 /* The following code determines the architecture.
22923709
RS
14908 Similar code was added to GCC 3.3 (see override_options() in
14909 config/mips/mips.c). The GAS and GCC code should be kept in sync
14910 as much as possible. */
e9670677 14911
316f5878 14912 if (mips_arch_string != 0)
fef14a42 14913 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14914
0b35dfee 14915 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14916 {
0b35dfee 14917 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14918 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14919 the -march selection (if any). */
fef14a42 14920 if (arch_info != 0)
e9670677 14921 {
316f5878
RS
14922 /* -march takes precedence over -mipsN, since it is more descriptive.
14923 There's no harm in specifying both as long as the ISA levels
14924 are the same. */
0b35dfee 14925 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14926 as_bad (_("-%s conflicts with the other architecture options,"
14927 " which imply -%s"),
0b35dfee 14928 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14929 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14930 }
316f5878 14931 else
0b35dfee 14932 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14933 }
14934
fef14a42 14935 if (arch_info == 0)
95bfe26e
MF
14936 {
14937 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14938 gas_assert (arch_info);
14939 }
e9670677 14940
fef14a42 14941 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14942 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14943 arch_info->name);
14944
919731af 14945 file_mips_opts.arch = arch_info->cpu;
14946 file_mips_opts.isa = arch_info->isa;
14947
14948 /* Set up initial mips_opts state. */
14949 mips_opts = file_mips_opts;
14950
14951 /* The register size inference code is now placed in
14952 file_mips_check_options. */
fef14a42 14953
0b35dfee 14954 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14955 processor. */
fef14a42
TS
14956 if (mips_tune_string != 0)
14957 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14958
fef14a42
TS
14959 if (tune_info == 0)
14960 mips_set_tune (arch_info);
14961 else
14962 mips_set_tune (tune_info);
e9670677 14963
ecb4347a 14964 if (mips_flag_mdebug < 0)
e8044f35 14965 mips_flag_mdebug = 0;
e9670677
MR
14966}
14967\f
14968void
17a2f251 14969mips_init_after_args (void)
252b5132
RH
14970{
14971 /* initialize opcodes */
14972 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14973 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14974}
14975
14976long
17a2f251 14977md_pcrel_from (fixS *fixP)
252b5132 14978{
a7ebbfdf
TS
14979 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14980 switch (fixP->fx_r_type)
14981 {
df58fc94
RS
14982 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14983 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14984 /* Return the address of the delay slot. */
14985 return addr + 2;
14986
14987 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14988 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 14989 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 14990 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
14991 case BFD_RELOC_MIPS_21_PCREL_S2:
14992 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
14993 case BFD_RELOC_MIPS_JMP:
14994 /* Return the address of the delay slot. */
14995 return addr + 4;
df58fc94 14996
51f6035b
MR
14997 case BFD_RELOC_MIPS_18_PCREL_S3:
14998 /* Return the aligned address of the doubleword containing
14999 the instruction. */
15000 return addr & ~7;
15001
a7ebbfdf
TS
15002 default:
15003 return addr;
15004 }
252b5132
RH
15005}
15006
252b5132
RH
15007/* This is called before the symbol table is processed. In order to
15008 work with gcc when using mips-tfile, we must keep all local labels.
15009 However, in other cases, we want to discard them. If we were
15010 called with -g, but we didn't see any debugging information, it may
15011 mean that gcc is smuggling debugging information through to
15012 mips-tfile, in which case we must generate all local labels. */
15013
15014void
17a2f251 15015mips_frob_file_before_adjust (void)
252b5132
RH
15016{
15017#ifndef NO_ECOFF_DEBUGGING
15018 if (ECOFF_DEBUGGING
15019 && mips_debug != 0
15020 && ! ecoff_debugging_seen)
15021 flag_keep_locals = 1;
15022#endif
15023}
15024
3b91255e 15025/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15026 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15027 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15028 relocation operators.
15029
15030 For our purposes, a %lo() expression matches a %got() or %hi()
15031 expression if:
15032
15033 (a) it refers to the same symbol; and
15034 (b) the offset applied in the %lo() expression is no lower than
15035 the offset applied in the %got() or %hi().
15036
15037 (b) allows us to cope with code like:
15038
15039 lui $4,%hi(foo)
15040 lh $4,%lo(foo+2)($4)
15041
15042 ...which is legal on RELA targets, and has a well-defined behaviour
15043 if the user knows that adding 2 to "foo" will not induce a carry to
15044 the high 16 bits.
15045
15046 When several %lo()s match a particular %got() or %hi(), we use the
15047 following rules to distinguish them:
15048
15049 (1) %lo()s with smaller offsets are a better match than %lo()s with
15050 higher offsets.
15051
15052 (2) %lo()s with no matching %got() or %hi() are better than those
15053 that already have a matching %got() or %hi().
15054
15055 (3) later %lo()s are better than earlier %lo()s.
15056
15057 These rules are applied in order.
15058
15059 (1) means, among other things, that %lo()s with identical offsets are
15060 chosen if they exist.
15061
15062 (2) means that we won't associate several high-part relocations with
15063 the same low-part relocation unless there's no alternative. Having
15064 several high parts for the same low part is a GNU extension; this rule
15065 allows careful users to avoid it.
15066
15067 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15068 with the last high-part relocation being at the front of the list.
15069 It therefore makes sense to choose the last matching low-part
15070 relocation, all other things being equal. It's also easier
15071 to code that way. */
252b5132
RH
15072
15073void
17a2f251 15074mips_frob_file (void)
252b5132
RH
15075{
15076 struct mips_hi_fixup *l;
35903be0 15077 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15078
15079 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15080 {
15081 segment_info_type *seginfo;
3b91255e
RS
15082 bfd_boolean matched_lo_p;
15083 fixS **hi_pos, **lo_pos, **pos;
252b5132 15084
9c2799c2 15085 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15086
5919d012 15087 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15088 there isn't supposed to be a matching LO. Ignore %gots against
15089 constants; we'll report an error for those later. */
738e5348 15090 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15091 && !(l->fixp->fx_addsy
9e009953 15092 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15093 continue;
15094
15095 /* Check quickly whether the next fixup happens to be a matching %lo. */
15096 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15097 continue;
15098
252b5132 15099 seginfo = seg_info (l->seg);
252b5132 15100
3b91255e
RS
15101 /* Set HI_POS to the position of this relocation in the chain.
15102 Set LO_POS to the position of the chosen low-part relocation.
15103 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15104 relocation that matches an immediately-preceding high-part
15105 relocation. */
15106 hi_pos = NULL;
15107 lo_pos = NULL;
15108 matched_lo_p = FALSE;
738e5348 15109 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15110
3b91255e
RS
15111 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15112 {
15113 if (*pos == l->fixp)
15114 hi_pos = pos;
15115
35903be0 15116 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15117 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15118 && (*pos)->fx_offset >= l->fixp->fx_offset
15119 && (lo_pos == NULL
15120 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15121 || (!matched_lo_p
15122 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15123 lo_pos = pos;
15124
15125 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15126 && fixup_has_matching_lo_p (*pos));
15127 }
15128
15129 /* If we found a match, remove the high-part relocation from its
15130 current position and insert it before the low-part relocation.
15131 Make the offsets match so that fixup_has_matching_lo_p()
15132 will return true.
15133
15134 We don't warn about unmatched high-part relocations since some
15135 versions of gcc have been known to emit dead "lui ...%hi(...)"
15136 instructions. */
15137 if (lo_pos != NULL)
15138 {
15139 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15140 if (l->fixp->fx_next != *lo_pos)
252b5132 15141 {
3b91255e
RS
15142 *hi_pos = l->fixp->fx_next;
15143 l->fixp->fx_next = *lo_pos;
15144 *lo_pos = l->fixp;
252b5132 15145 }
252b5132
RH
15146 }
15147 }
15148}
15149
252b5132 15150int
17a2f251 15151mips_force_relocation (fixS *fixp)
252b5132 15152{
ae6063d4 15153 if (generic_force_reloc (fixp))
252b5132
RH
15154 return 1;
15155
df58fc94
RS
15156 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15157 so that the linker relaxation can update targets. */
15158 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15159 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15160 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15161 return 1;
15162
5caa2b07
MR
15163 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15164 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15165 microMIPS symbols so that we can do cross-mode branch diagnostics
15166 and BAL to JALX conversion by the linker. */
15167 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15168 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15169 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15170 && fixp->fx_addsy
15171 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15172 return 1;
15173
7361da2c 15174 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15175 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15176 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15177 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15178 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15179 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15180 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15181 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15182 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15183 return 1;
15184
3e722fb5 15185 return 0;
252b5132
RH
15186}
15187
b416ba9b
MR
15188/* Implement TC_FORCE_RELOCATION_ABS. */
15189
15190bfd_boolean
15191mips_force_relocation_abs (fixS *fixp)
15192{
15193 if (generic_force_reloc (fixp))
15194 return TRUE;
15195
15196 /* These relocations do not have enough bits in the in-place addend
15197 to hold an arbitrary absolute section's offset. */
15198 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15199 return TRUE;
15200
15201 return FALSE;
15202}
15203
b886a2ab
RS
15204/* Read the instruction associated with RELOC from BUF. */
15205
15206static unsigned int
15207read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15208{
15209 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15210 return read_compressed_insn (buf, 4);
15211 else
15212 return read_insn (buf);
15213}
15214
15215/* Write instruction INSN to BUF, given that it has been relocated
15216 by RELOC. */
15217
15218static void
15219write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15220 unsigned long insn)
15221{
15222 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15223 write_compressed_insn (buf, insn, 4);
15224 else
15225 write_insn (buf, insn);
15226}
15227
9d862524
MR
15228/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15229 to a symbol in another ISA mode, which cannot be converted to JALX. */
15230
15231static bfd_boolean
15232fix_bad_cross_mode_jump_p (fixS *fixP)
15233{
15234 unsigned long opcode;
15235 int other;
15236 char *buf;
15237
15238 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15239 return FALSE;
15240
15241 other = S_GET_OTHER (fixP->fx_addsy);
15242 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15243 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15244 switch (fixP->fx_r_type)
15245 {
15246 case BFD_RELOC_MIPS_JMP:
15247 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15248 case BFD_RELOC_MICROMIPS_JMP:
15249 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15250 default:
15251 return FALSE;
15252 }
15253}
15254
15255/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15256 jump to a symbol in the same ISA mode. */
15257
15258static bfd_boolean
15259fix_bad_same_mode_jalx_p (fixS *fixP)
15260{
15261 unsigned long opcode;
15262 int other;
15263 char *buf;
15264
15265 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15266 return FALSE;
15267
15268 other = S_GET_OTHER (fixP->fx_addsy);
15269 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15270 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15271 switch (fixP->fx_r_type)
15272 {
15273 case BFD_RELOC_MIPS_JMP:
15274 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15275 case BFD_RELOC_MIPS16_JMP:
15276 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15277 case BFD_RELOC_MICROMIPS_JMP:
15278 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15279 default:
15280 return FALSE;
15281 }
15282}
15283
15284/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15285 to a symbol whose value plus addend is not aligned according to the
15286 ultimate (after linker relaxation) jump instruction's immediate field
15287 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15288 regular MIPS code, to (1 << 2). */
15289
15290static bfd_boolean
15291fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15292{
15293 bfd_boolean micro_to_mips_p;
15294 valueT val;
15295 int other;
15296
15297 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15298 return FALSE;
15299
15300 other = S_GET_OTHER (fixP->fx_addsy);
15301 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15302 val += fixP->fx_offset;
15303 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15304 && !ELF_ST_IS_MICROMIPS (other));
15305 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15306 != ELF_ST_IS_COMPRESSED (other));
15307}
15308
15309/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15310 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15311 symbols check the ISA bit instead.
15312
15313 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15314 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15315 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15316 may be converted to JALX by the linker. */
9d862524
MR
15317
15318static bfd_boolean
15319fix_bad_cross_mode_branch_p (fixS *fixP)
15320{
15321 bfd_boolean absolute_p;
15322 unsigned long opcode;
15323 asection *symsec;
15324 valueT val;
15325 int other;
15326 char *buf;
15327
8b10b0b3
MR
15328 if (mips_ignore_branch_isa)
15329 return FALSE;
15330
9d862524
MR
15331 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15332 return FALSE;
15333
15334 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15335 absolute_p = bfd_is_abs_section (symsec);
15336
15337 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15338 other = S_GET_OTHER (fixP->fx_addsy);
15339
15340 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15341 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15342 switch (fixP->fx_r_type)
15343 {
15344 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15345 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15346 && opcode != 0x0411);
15347 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15348 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15349 && opcode != 0x4060);
9d862524
MR
15350 case BFD_RELOC_MIPS_21_PCREL_S2:
15351 case BFD_RELOC_MIPS_26_PCREL_S2:
15352 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15353 case BFD_RELOC_MIPS16_16_PCREL_S1:
15354 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15355 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15356 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15357 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15358 default:
15359 abort ();
15360 }
15361}
15362
15363/* Return TRUE if the symbol plus addend associated with a regular MIPS
15364 branch instruction pointed to by FIXP is not aligned according to the
15365 branch instruction's immediate field requirement. We need the addend
15366 to preserve the ISA bit and also the sum must not have bit 2 set. We
15367 must explicitly OR in the ISA bit from symbol annotation as the bit
15368 won't be set in the symbol's value then. */
15369
15370static bfd_boolean
15371fix_bad_misaligned_branch_p (fixS *fixP)
15372{
15373 bfd_boolean absolute_p;
15374 asection *symsec;
15375 valueT isa_bit;
15376 valueT val;
15377 valueT off;
15378 int other;
15379
15380 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15381 return FALSE;
15382
15383 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15384 absolute_p = bfd_is_abs_section (symsec);
15385
15386 val = S_GET_VALUE (fixP->fx_addsy);
15387 other = S_GET_OTHER (fixP->fx_addsy);
15388 off = fixP->fx_offset;
15389
15390 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15391 val |= ELF_ST_IS_COMPRESSED (other);
15392 val += off;
15393 return (val & 0x3) != isa_bit;
15394}
15395
15396/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15397 and its calculated value VAL. */
15398
15399static void
15400fix_validate_branch (fixS *fixP, valueT val)
15401{
15402 if (fixP->fx_done && (val & 0x3) != 0)
15403 as_bad_where (fixP->fx_file, fixP->fx_line,
15404 _("branch to misaligned address (0x%lx)"),
15405 (long) (val + md_pcrel_from (fixP)));
15406 else if (fix_bad_cross_mode_branch_p (fixP))
15407 as_bad_where (fixP->fx_file, fixP->fx_line,
15408 _("branch to a symbol in another ISA mode"));
15409 else if (fix_bad_misaligned_branch_p (fixP))
15410 as_bad_where (fixP->fx_file, fixP->fx_line,
15411 _("branch to misaligned address (0x%lx)"),
15412 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15413 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15414 as_bad_where (fixP->fx_file, fixP->fx_line,
15415 _("cannot encode misaligned addend "
15416 "in the relocatable field (0x%lx)"),
15417 (long) fixP->fx_offset);
15418}
15419
252b5132
RH
15420/* Apply a fixup to the object file. */
15421
94f592af 15422void
55cf6793 15423md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15424{
4d68580a 15425 char *buf;
b886a2ab 15426 unsigned long insn;
a7ebbfdf 15427 reloc_howto_type *howto;
252b5132 15428
d56a8dda
RS
15429 if (fixP->fx_pcrel)
15430 switch (fixP->fx_r_type)
15431 {
15432 case BFD_RELOC_16_PCREL_S2:
c9775dde 15433 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15434 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15435 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15436 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15437 case BFD_RELOC_32_PCREL:
7361da2c
AB
15438 case BFD_RELOC_MIPS_21_PCREL_S2:
15439 case BFD_RELOC_MIPS_26_PCREL_S2:
15440 case BFD_RELOC_MIPS_18_PCREL_S3:
15441 case BFD_RELOC_MIPS_19_PCREL_S2:
15442 case BFD_RELOC_HI16_S_PCREL:
15443 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15444 break;
15445
15446 case BFD_RELOC_32:
15447 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15448 break;
15449
15450 default:
15451 as_bad_where (fixP->fx_file, fixP->fx_line,
15452 _("PC-relative reference to a different section"));
15453 break;
15454 }
15455
15456 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15457 that have no MIPS ELF equivalent. */
15458 if (fixP->fx_r_type != BFD_RELOC_8)
15459 {
15460 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15461 if (!howto)
15462 return;
15463 }
65551fa4 15464
df58fc94
RS
15465 gas_assert (fixP->fx_size == 2
15466 || fixP->fx_size == 4
d56a8dda 15467 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15468 || fixP->fx_r_type == BFD_RELOC_16
15469 || fixP->fx_r_type == BFD_RELOC_64
15470 || fixP->fx_r_type == BFD_RELOC_CTOR
15471 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15472 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15473 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15474 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15475 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15476 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15477
4d68580a 15478 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15479
b1dca8ee
RS
15480 /* Don't treat parts of a composite relocation as done. There are two
15481 reasons for this:
15482
15483 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15484 should nevertheless be emitted if the first part is.
15485
15486 (2) In normal usage, composite relocations are never assembly-time
15487 constants. The easiest way of dealing with the pathological
15488 exceptions is to generate a relocation against STN_UNDEF and
15489 leave everything up to the linker. */
3994f87e 15490 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15491 fixP->fx_done = 1;
15492
15493 switch (fixP->fx_r_type)
15494 {
3f98094e
DJ
15495 case BFD_RELOC_MIPS_TLS_GD:
15496 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15497 case BFD_RELOC_MIPS_TLS_DTPREL32:
15498 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15499 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15500 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15501 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15502 case BFD_RELOC_MIPS_TLS_TPREL32:
15503 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15504 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15505 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15506 case BFD_RELOC_MICROMIPS_TLS_GD:
15507 case BFD_RELOC_MICROMIPS_TLS_LDM:
15508 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15509 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15510 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15511 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15512 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15513 case BFD_RELOC_MIPS16_TLS_GD:
15514 case BFD_RELOC_MIPS16_TLS_LDM:
15515 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15516 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15517 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15518 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15519 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15520 if (fixP->fx_addsy)
15521 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15522 else
15523 as_bad_where (fixP->fx_file, fixP->fx_line,
15524 _("TLS relocation against a constant"));
15525 break;
3f98094e 15526
252b5132 15527 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15528 case BFD_RELOC_MIPS16_JMP:
15529 case BFD_RELOC_MICROMIPS_JMP:
15530 {
15531 int shift;
15532
15533 gas_assert (!fixP->fx_done);
15534
15535 /* Shift is 2, unusually, for microMIPS JALX. */
15536 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15537 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15538 shift = 1;
15539 else
15540 shift = 2;
15541
15542 if (fix_bad_cross_mode_jump_p (fixP))
15543 as_bad_where (fixP->fx_file, fixP->fx_line,
15544 _("jump to a symbol in another ISA mode"));
15545 else if (fix_bad_same_mode_jalx_p (fixP))
15546 as_bad_where (fixP->fx_file, fixP->fx_line,
15547 _("JALX to a symbol in the same ISA mode"));
15548 else if (fix_bad_misaligned_jump_p (fixP, shift))
15549 as_bad_where (fixP->fx_file, fixP->fx_line,
15550 _("jump to misaligned address (0x%lx)"),
15551 (long) (S_GET_VALUE (fixP->fx_addsy)
15552 + fixP->fx_offset));
15553 else if (HAVE_IN_PLACE_ADDENDS
15554 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15555 as_bad_where (fixP->fx_file, fixP->fx_line,
15556 _("cannot encode misaligned addend "
15557 "in the relocatable field (0x%lx)"),
15558 (long) fixP->fx_offset);
15559 }
15560 /* Fall through. */
15561
e369bcce
TS
15562 case BFD_RELOC_MIPS_SHIFT5:
15563 case BFD_RELOC_MIPS_SHIFT6:
15564 case BFD_RELOC_MIPS_GOT_DISP:
15565 case BFD_RELOC_MIPS_GOT_PAGE:
15566 case BFD_RELOC_MIPS_GOT_OFST:
15567 case BFD_RELOC_MIPS_SUB:
15568 case BFD_RELOC_MIPS_INSERT_A:
15569 case BFD_RELOC_MIPS_INSERT_B:
15570 case BFD_RELOC_MIPS_DELETE:
15571 case BFD_RELOC_MIPS_HIGHEST:
15572 case BFD_RELOC_MIPS_HIGHER:
15573 case BFD_RELOC_MIPS_SCN_DISP:
15574 case BFD_RELOC_MIPS_REL16:
15575 case BFD_RELOC_MIPS_RELGOT:
15576 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15577 case BFD_RELOC_HI16:
15578 case BFD_RELOC_HI16_S:
b886a2ab 15579 case BFD_RELOC_LO16:
cdf6fd85 15580 case BFD_RELOC_GPREL16:
252b5132
RH
15581 case BFD_RELOC_MIPS_LITERAL:
15582 case BFD_RELOC_MIPS_CALL16:
15583 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15584 case BFD_RELOC_GPREL32:
252b5132
RH
15585 case BFD_RELOC_MIPS_GOT_HI16:
15586 case BFD_RELOC_MIPS_GOT_LO16:
15587 case BFD_RELOC_MIPS_CALL_HI16:
15588 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15589 case BFD_RELOC_HI16_S_PCREL:
15590 case BFD_RELOC_LO16_PCREL:
252b5132 15591 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15592 case BFD_RELOC_MIPS16_GOT16:
15593 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15594 case BFD_RELOC_MIPS16_HI16:
15595 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15596 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15597 case BFD_RELOC_MICROMIPS_GOT_DISP:
15598 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15599 case BFD_RELOC_MICROMIPS_GOT_OFST:
15600 case BFD_RELOC_MICROMIPS_SUB:
15601 case BFD_RELOC_MICROMIPS_HIGHEST:
15602 case BFD_RELOC_MICROMIPS_HIGHER:
15603 case BFD_RELOC_MICROMIPS_SCN_DISP:
15604 case BFD_RELOC_MICROMIPS_JALR:
15605 case BFD_RELOC_MICROMIPS_HI16:
15606 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15607 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15608 case BFD_RELOC_MICROMIPS_GPREL16:
15609 case BFD_RELOC_MICROMIPS_LITERAL:
15610 case BFD_RELOC_MICROMIPS_CALL16:
15611 case BFD_RELOC_MICROMIPS_GOT16:
15612 case BFD_RELOC_MICROMIPS_GOT_HI16:
15613 case BFD_RELOC_MICROMIPS_GOT_LO16:
15614 case BFD_RELOC_MICROMIPS_CALL_HI16:
15615 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15616 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15617 if (fixP->fx_done)
15618 {
15619 offsetT value;
15620
15621 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15622 {
15623 insn = read_reloc_insn (buf, fixP->fx_r_type);
15624 if (mips16_reloc_p (fixP->fx_r_type))
15625 insn |= mips16_immed_extend (value, 16);
15626 else
15627 insn |= (value & 0xffff);
15628 write_reloc_insn (buf, fixP->fx_r_type, insn);
15629 }
15630 else
15631 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15632 _("unsupported constant in relocation"));
b886a2ab 15633 }
252b5132
RH
15634 break;
15635
252b5132
RH
15636 case BFD_RELOC_64:
15637 /* This is handled like BFD_RELOC_32, but we output a sign
15638 extended value if we are only 32 bits. */
3e722fb5 15639 if (fixP->fx_done)
252b5132
RH
15640 {
15641 if (8 <= sizeof (valueT))
4d68580a 15642 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15643 else
15644 {
a7ebbfdf 15645 valueT hiv;
252b5132 15646
a7ebbfdf 15647 if ((*valP & 0x80000000) != 0)
252b5132
RH
15648 hiv = 0xffffffff;
15649 else
15650 hiv = 0;
4d68580a
RS
15651 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15652 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15653 }
15654 }
15655 break;
15656
056350c6 15657 case BFD_RELOC_RVA:
252b5132 15658 case BFD_RELOC_32:
b47468a6 15659 case BFD_RELOC_32_PCREL:
252b5132 15660 case BFD_RELOC_16:
d56a8dda 15661 case BFD_RELOC_8:
252b5132 15662 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15663 value now. This can happen if we have a .word which is not
15664 resolved when it appears but is later defined. */
252b5132 15665 if (fixP->fx_done)
4d68580a 15666 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15667 break;
15668
7361da2c 15669 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15670 fix_validate_branch (fixP, *valP);
41947d9e
MR
15671 if (!fixP->fx_done)
15672 break;
15673
15674 if (*valP + 0x400000 <= 0x7fffff)
15675 {
15676 insn = read_insn (buf);
15677 insn |= (*valP >> 2) & 0x1fffff;
15678 write_insn (buf, insn);
15679 }
15680 else
15681 as_bad_where (fixP->fx_file, fixP->fx_line,
15682 _("branch out of range"));
15683 break;
15684
7361da2c 15685 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15686 fix_validate_branch (fixP, *valP);
41947d9e
MR
15687 if (!fixP->fx_done)
15688 break;
7361da2c 15689
41947d9e
MR
15690 if (*valP + 0x8000000 <= 0xfffffff)
15691 {
15692 insn = read_insn (buf);
15693 insn |= (*valP >> 2) & 0x3ffffff;
15694 write_insn (buf, insn);
15695 }
15696 else
15697 as_bad_where (fixP->fx_file, fixP->fx_line,
15698 _("branch out of range"));
7361da2c
AB
15699 break;
15700
15701 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15702 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15703 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15704 _("PC-relative access using misaligned symbol (%lx)"),
15705 (long) S_GET_VALUE (fixP->fx_addsy));
15706 if ((fixP->fx_offset & 0x7) != 0)
15707 as_bad_where (fixP->fx_file, fixP->fx_line,
15708 _("PC-relative access using misaligned offset (%lx)"),
15709 (long) fixP->fx_offset);
41947d9e
MR
15710 if (!fixP->fx_done)
15711 break;
7361da2c 15712
41947d9e
MR
15713 if (*valP + 0x100000 <= 0x1fffff)
15714 {
15715 insn = read_insn (buf);
15716 insn |= (*valP >> 3) & 0x3ffff;
15717 write_insn (buf, insn);
15718 }
15719 else
15720 as_bad_where (fixP->fx_file, fixP->fx_line,
15721 _("PC-relative access out of range"));
7361da2c
AB
15722 break;
15723
15724 case BFD_RELOC_MIPS_19_PCREL_S2:
15725 if ((*valP & 0x3) != 0)
15726 as_bad_where (fixP->fx_file, fixP->fx_line,
15727 _("PC-relative access to misaligned address (%lx)"),
717ba204 15728 (long) *valP);
41947d9e
MR
15729 if (!fixP->fx_done)
15730 break;
7361da2c 15731
41947d9e
MR
15732 if (*valP + 0x100000 <= 0x1fffff)
15733 {
15734 insn = read_insn (buf);
15735 insn |= (*valP >> 2) & 0x7ffff;
15736 write_insn (buf, insn);
15737 }
15738 else
15739 as_bad_where (fixP->fx_file, fixP->fx_line,
15740 _("PC-relative access out of range"));
7361da2c
AB
15741 break;
15742
252b5132 15743 case BFD_RELOC_16_PCREL_S2:
9d862524 15744 fix_validate_branch (fixP, *valP);
cb56d3d3 15745
54f4ddb3
TS
15746 /* We need to save the bits in the instruction since fixup_segment()
15747 might be deleting the relocation entry (i.e., a branch within
15748 the current segment). */
a7ebbfdf 15749 if (! fixP->fx_done)
bb2d6cd7 15750 break;
252b5132 15751
54f4ddb3 15752 /* Update old instruction data. */
4d68580a 15753 insn = read_insn (buf);
252b5132 15754
a7ebbfdf
TS
15755 if (*valP + 0x20000 <= 0x3ffff)
15756 {
15757 insn |= (*valP >> 2) & 0xffff;
4d68580a 15758 write_insn (buf, insn);
a7ebbfdf 15759 }
ce8ad872 15760 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15761 && fixP->fx_done
15762 && fixP->fx_frag->fr_address >= text_section->vma
15763 && (fixP->fx_frag->fr_address
587aac4e 15764 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15765 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15766 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15767 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15768 {
15769 /* The branch offset is too large. If this is an
15770 unconditional branch, and we are not generating PIC code,
15771 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15772 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15773 insn = 0x0c000000; /* jal */
252b5132 15774 else
a7ebbfdf
TS
15775 insn = 0x08000000; /* j */
15776 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15777 fixP->fx_done = 0;
15778 fixP->fx_addsy = section_symbol (text_section);
15779 *valP += md_pcrel_from (fixP);
4d68580a 15780 write_insn (buf, insn);
a7ebbfdf
TS
15781 }
15782 else
15783 {
15784 /* If we got here, we have branch-relaxation disabled,
15785 and there's nothing we can do to fix this instruction
15786 without turning it into a longer sequence. */
15787 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15788 _("branch out of range"));
252b5132 15789 }
252b5132
RH
15790 break;
15791
c9775dde 15792 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15793 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15794 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15795 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15796 gas_assert (!fixP->fx_done);
9d862524
MR
15797 if (fix_bad_cross_mode_branch_p (fixP))
15798 as_bad_where (fixP->fx_file, fixP->fx_line,
15799 _("branch to a symbol in another ISA mode"));
15800 else if (fixP->fx_addsy
15801 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15802 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15803 && (fixP->fx_offset & 0x1) != 0)
15804 as_bad_where (fixP->fx_file, fixP->fx_line,
15805 _("branch to misaligned address (0x%lx)"),
15806 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15807 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15808 as_bad_where (fixP->fx_file, fixP->fx_line,
15809 _("cannot encode misaligned addend "
15810 "in the relocatable field (0x%lx)"),
15811 (long) fixP->fx_offset);
df58fc94
RS
15812 break;
15813
252b5132
RH
15814 case BFD_RELOC_VTABLE_INHERIT:
15815 fixP->fx_done = 0;
15816 if (fixP->fx_addsy
15817 && !S_IS_DEFINED (fixP->fx_addsy)
15818 && !S_IS_WEAK (fixP->fx_addsy))
15819 S_SET_WEAK (fixP->fx_addsy);
15820 break;
15821
2f0c68f2 15822 case BFD_RELOC_NONE:
252b5132
RH
15823 case BFD_RELOC_VTABLE_ENTRY:
15824 fixP->fx_done = 0;
15825 break;
15826
15827 default:
b37df7c4 15828 abort ();
252b5132 15829 }
a7ebbfdf
TS
15830
15831 /* Remember value for tc_gen_reloc. */
15832 fixP->fx_addnumber = *valP;
252b5132
RH
15833}
15834
252b5132 15835static symbolS *
17a2f251 15836get_symbol (void)
252b5132
RH
15837{
15838 int c;
15839 char *name;
15840 symbolS *p;
15841
d02603dc 15842 c = get_symbol_name (&name);
252b5132 15843 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15844 (void) restore_line_pointer (c);
252b5132
RH
15845 return p;
15846}
15847
742a56fe
RS
15848/* Align the current frag to a given power of two. If a particular
15849 fill byte should be used, FILL points to an integer that contains
15850 that byte, otherwise FILL is null.
15851
462427c4
RS
15852 This function used to have the comment:
15853
15854 The MIPS assembler also automatically adjusts any preceding label.
15855
15856 The implementation therefore applied the adjustment to a maximum of
15857 one label. However, other label adjustments are applied to batches
15858 of labels, and adjusting just one caused problems when new labels
15859 were added for the sake of debugging or unwind information.
15860 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15861
15862static void
462427c4 15863mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15864{
7d10b47d 15865 mips_emit_delays ();
df58fc94 15866 mips_record_compressed_mode ();
742a56fe
RS
15867 if (fill == NULL && subseg_text_p (now_seg))
15868 frag_align_code (to, 0);
15869 else
15870 frag_align (to, fill ? *fill : 0, 0);
252b5132 15871 record_alignment (now_seg, to);
462427c4 15872 mips_move_labels (labels, FALSE);
252b5132
RH
15873}
15874
15875/* Align to a given power of two. .align 0 turns off the automatic
15876 alignment used by the data creating pseudo-ops. */
15877
15878static void
17a2f251 15879s_align (int x ATTRIBUTE_UNUSED)
252b5132 15880{
742a56fe 15881 int temp, fill_value, *fill_ptr;
49954fb4 15882 long max_alignment = 28;
252b5132 15883
54f4ddb3 15884 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15885 to the aligned address.
54f4ddb3 15886 o It's not documented but auto alignment is reinstated by
252b5132 15887 a .align pseudo instruction.
54f4ddb3 15888 o Note also that after auto alignment is turned off the mips assembler
252b5132 15889 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15890 We don't. */
252b5132
RH
15891
15892 temp = get_absolute_expression ();
15893 if (temp > max_alignment)
1661c76c 15894 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15895 else if (temp < 0)
15896 {
1661c76c 15897 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15898 temp = 0;
15899 }
15900 if (*input_line_pointer == ',')
15901 {
f9419b05 15902 ++input_line_pointer;
742a56fe
RS
15903 fill_value = get_absolute_expression ();
15904 fill_ptr = &fill_value;
252b5132
RH
15905 }
15906 else
742a56fe 15907 fill_ptr = 0;
252b5132
RH
15908 if (temp)
15909 {
a8dbcb85
TS
15910 segment_info_type *si = seg_info (now_seg);
15911 struct insn_label_list *l = si->label_list;
54f4ddb3 15912 /* Auto alignment should be switched on by next section change. */
252b5132 15913 auto_align = 1;
462427c4 15914 mips_align (temp, fill_ptr, l);
252b5132
RH
15915 }
15916 else
15917 {
15918 auto_align = 0;
15919 }
15920
15921 demand_empty_rest_of_line ();
15922}
15923
252b5132 15924static void
17a2f251 15925s_change_sec (int sec)
252b5132
RH
15926{
15927 segT seg;
15928
252b5132
RH
15929 /* The ELF backend needs to know that we are changing sections, so
15930 that .previous works correctly. We could do something like check
b6ff326e 15931 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15932 as it would not be appropriate to use it in the section changing
15933 functions in read.c, since obj-elf.c intercepts those. FIXME:
15934 This should be cleaner, somehow. */
f3ded42a 15935 obj_elf_section_change_hook ();
252b5132 15936
7d10b47d 15937 mips_emit_delays ();
6a32d874 15938
252b5132
RH
15939 switch (sec)
15940 {
15941 case 't':
15942 s_text (0);
15943 break;
15944 case 'd':
15945 s_data (0);
15946 break;
15947 case 'b':
15948 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15949 demand_empty_rest_of_line ();
15950 break;
15951
15952 case 'r':
4d0d148d
TS
15953 seg = subseg_new (RDATA_SECTION_NAME,
15954 (subsegT) get_absolute_expression ());
f3ded42a
RS
15955 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15956 | SEC_READONLY | SEC_RELOC
15957 | SEC_DATA));
15958 if (strncmp (TARGET_OS, "elf", 3) != 0)
15959 record_alignment (seg, 4);
4d0d148d 15960 demand_empty_rest_of_line ();
252b5132
RH
15961 break;
15962
15963 case 's':
4d0d148d 15964 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
15965 bfd_set_section_flags (stdoutput, seg,
15966 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15967 if (strncmp (TARGET_OS, "elf", 3) != 0)
15968 record_alignment (seg, 4);
4d0d148d
TS
15969 demand_empty_rest_of_line ();
15970 break;
998b3c36
MR
15971
15972 case 'B':
15973 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
15974 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15975 if (strncmp (TARGET_OS, "elf", 3) != 0)
15976 record_alignment (seg, 4);
998b3c36
MR
15977 demand_empty_rest_of_line ();
15978 break;
252b5132
RH
15979 }
15980
15981 auto_align = 1;
15982}
b34976b6 15983
cca86cc8 15984void
17a2f251 15985s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15986{
d02603dc 15987 char *saved_ilp;
cca86cc8 15988 char *section_name;
d02603dc 15989 char c, endc;
684022ea 15990 char next_c = 0;
cca86cc8
SC
15991 int section_type;
15992 int section_flag;
15993 int section_entry_size;
15994 int section_alignment;
b34976b6 15995
d02603dc
NC
15996 saved_ilp = input_line_pointer;
15997 endc = get_symbol_name (&section_name);
15998 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 15999 if (c)
d02603dc 16000 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16001
4cf0dd0d
TS
16002 /* Do we have .section Name<,"flags">? */
16003 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16004 {
d02603dc
NC
16005 /* Just after name is now '\0'. */
16006 (void) restore_line_pointer (endc);
16007 input_line_pointer = saved_ilp;
cca86cc8
SC
16008 obj_elf_section (ignore);
16009 return;
16010 }
d02603dc
NC
16011
16012 section_name = xstrdup (section_name);
16013 c = restore_line_pointer (endc);
16014
cca86cc8
SC
16015 input_line_pointer++;
16016
16017 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16018 if (c == ',')
16019 section_type = get_absolute_expression ();
16020 else
16021 section_type = 0;
d02603dc 16022
cca86cc8
SC
16023 if (*input_line_pointer++ == ',')
16024 section_flag = get_absolute_expression ();
16025 else
16026 section_flag = 0;
d02603dc 16027
cca86cc8
SC
16028 if (*input_line_pointer++ == ',')
16029 section_entry_size = get_absolute_expression ();
16030 else
16031 section_entry_size = 0;
d02603dc 16032
cca86cc8
SC
16033 if (*input_line_pointer++ == ',')
16034 section_alignment = get_absolute_expression ();
16035 else
16036 section_alignment = 0;
d02603dc 16037
87975d2a
AM
16038 /* FIXME: really ignore? */
16039 (void) section_alignment;
cca86cc8 16040
8ab8a5c8
RS
16041 /* When using the generic form of .section (as implemented by obj-elf.c),
16042 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16043 traditionally had to fall back on the more common @progbits instead.
16044
16045 There's nothing really harmful in this, since bfd will correct
16046 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16047 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16048 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16049
16050 Even so, we shouldn't force users of the MIPS .section syntax to
16051 incorrectly label the sections as SHT_PROGBITS. The best compromise
16052 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16053 generic type-checking code. */
16054 if (section_type == SHT_MIPS_DWARF)
16055 section_type = SHT_PROGBITS;
16056
a91e1603 16057 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16058 section_entry_size, 0, 0, 0);
a816d1ed
AO
16059
16060 if (now_seg->name != section_name)
16061 free (section_name);
cca86cc8 16062}
252b5132
RH
16063
16064void
17a2f251 16065mips_enable_auto_align (void)
252b5132
RH
16066{
16067 auto_align = 1;
16068}
16069
16070static void
17a2f251 16071s_cons (int log_size)
252b5132 16072{
a8dbcb85
TS
16073 segment_info_type *si = seg_info (now_seg);
16074 struct insn_label_list *l = si->label_list;
252b5132 16075
7d10b47d 16076 mips_emit_delays ();
252b5132 16077 if (log_size > 0 && auto_align)
462427c4 16078 mips_align (log_size, 0, l);
252b5132 16079 cons (1 << log_size);
a1facbec 16080 mips_clear_insn_labels ();
252b5132
RH
16081}
16082
16083static void
17a2f251 16084s_float_cons (int type)
252b5132 16085{
a8dbcb85
TS
16086 segment_info_type *si = seg_info (now_seg);
16087 struct insn_label_list *l = si->label_list;
252b5132 16088
7d10b47d 16089 mips_emit_delays ();
252b5132
RH
16090
16091 if (auto_align)
49309057
ILT
16092 {
16093 if (type == 'd')
462427c4 16094 mips_align (3, 0, l);
49309057 16095 else
462427c4 16096 mips_align (2, 0, l);
49309057 16097 }
252b5132 16098
252b5132 16099 float_cons (type);
a1facbec 16100 mips_clear_insn_labels ();
252b5132
RH
16101}
16102
16103/* Handle .globl. We need to override it because on Irix 5 you are
16104 permitted to say
16105 .globl foo .text
16106 where foo is an undefined symbol, to mean that foo should be
16107 considered to be the address of a function. */
16108
16109static void
17a2f251 16110s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16111{
16112 char *name;
16113 int c;
16114 symbolS *symbolP;
16115 flagword flag;
16116
8a06b769 16117 do
252b5132 16118 {
d02603dc 16119 c = get_symbol_name (&name);
8a06b769
TS
16120 symbolP = symbol_find_or_make (name);
16121 S_SET_EXTERNAL (symbolP);
16122
252b5132 16123 *input_line_pointer = c;
d02603dc 16124 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16125
8a06b769
TS
16126 /* On Irix 5, every global symbol that is not explicitly labelled as
16127 being a function is apparently labelled as being an object. */
16128 flag = BSF_OBJECT;
252b5132 16129
8a06b769
TS
16130 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16131 && (*input_line_pointer != ','))
16132 {
16133 char *secname;
16134 asection *sec;
16135
d02603dc 16136 c = get_symbol_name (&secname);
8a06b769
TS
16137 sec = bfd_get_section_by_name (stdoutput, secname);
16138 if (sec == NULL)
16139 as_bad (_("%s: no such section"), secname);
d02603dc 16140 (void) restore_line_pointer (c);
8a06b769
TS
16141
16142 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16143 flag = BSF_FUNCTION;
16144 }
16145
16146 symbol_get_bfdsym (symbolP)->flags |= flag;
16147
16148 c = *input_line_pointer;
16149 if (c == ',')
16150 {
16151 input_line_pointer++;
16152 SKIP_WHITESPACE ();
16153 if (is_end_of_line[(unsigned char) *input_line_pointer])
16154 c = '\n';
16155 }
16156 }
16157 while (c == ',');
252b5132 16158
252b5132
RH
16159 demand_empty_rest_of_line ();
16160}
16161
16162static void
17a2f251 16163s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16164{
16165 char *opt;
16166 char c;
16167
d02603dc 16168 c = get_symbol_name (&opt);
252b5132
RH
16169
16170 if (*opt == 'O')
16171 {
16172 /* FIXME: What does this mean? */
16173 }
41a1578e 16174 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16175 {
16176 int i;
16177
16178 i = atoi (opt + 3);
668c5ebc
MR
16179 if (i != 0 && i != 2)
16180 as_bad (_(".option pic%d not supported"), i);
16181 else if (mips_pic == VXWORKS_PIC)
16182 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16183 else if (i == 0)
252b5132
RH
16184 mips_pic = NO_PIC;
16185 else if (i == 2)
143d77c5 16186 {
8b828383 16187 mips_pic = SVR4_PIC;
143d77c5
EC
16188 mips_abicalls = TRUE;
16189 }
252b5132 16190
4d0d148d 16191 if (mips_pic == SVR4_PIC)
252b5132
RH
16192 {
16193 if (g_switch_seen && g_switch_value != 0)
16194 as_warn (_("-G may not be used with SVR4 PIC code"));
16195 g_switch_value = 0;
16196 bfd_set_gp_size (stdoutput, 0);
16197 }
16198 }
16199 else
1661c76c 16200 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16201
d02603dc 16202 (void) restore_line_pointer (c);
252b5132
RH
16203 demand_empty_rest_of_line ();
16204}
16205
16206/* This structure is used to hold a stack of .set values. */
16207
e972090a
NC
16208struct mips_option_stack
16209{
252b5132
RH
16210 struct mips_option_stack *next;
16211 struct mips_set_options options;
16212};
16213
16214static struct mips_option_stack *mips_opts_stack;
16215
22522f88
MR
16216/* Return status for .set/.module option handling. */
16217
16218enum code_option_type
16219{
16220 /* Unrecognized option. */
16221 OPTION_TYPE_BAD = -1,
16222
16223 /* Ordinary option. */
16224 OPTION_TYPE_NORMAL,
16225
16226 /* ISA changing option. */
16227 OPTION_TYPE_ISA
16228};
16229
16230/* Handle common .set/.module options. Return status indicating option
16231 type. */
16232
16233static enum code_option_type
919731af 16234parse_code_option (char * name)
252b5132 16235{
22522f88 16236 bfd_boolean isa_set = FALSE;
c6278170 16237 const struct mips_ase *ase;
22522f88 16238
919731af 16239 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16240 {
16241 char *s = name + 3;
16242
16243 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16244 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16245 }
252b5132 16246 else if (strcmp (name, "at") == 0)
919731af 16247 mips_opts.at = ATREG;
252b5132 16248 else if (strcmp (name, "noat") == 0)
919731af 16249 mips_opts.at = ZERO;
252b5132 16250 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16251 mips_opts.nomove = 0;
252b5132 16252 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16253 mips_opts.nomove = 1;
252b5132 16254 else if (strcmp (name, "bopt") == 0)
919731af 16255 mips_opts.nobopt = 0;
252b5132 16256 else if (strcmp (name, "nobopt") == 0)
919731af 16257 mips_opts.nobopt = 1;
ad3fea08 16258 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16259 mips_opts.gp = 32;
ad3fea08 16260 else if (strcmp (name, "gp=64") == 0)
919731af 16261 mips_opts.gp = 64;
ad3fea08 16262 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16263 mips_opts.fp = 32;
351cdf24
MF
16264 else if (strcmp (name, "fp=xx") == 0)
16265 mips_opts.fp = 0;
ad3fea08 16266 else if (strcmp (name, "fp=64") == 0)
919731af 16267 mips_opts.fp = 64;
037b32b9
AN
16268 else if (strcmp (name, "softfloat") == 0)
16269 mips_opts.soft_float = 1;
16270 else if (strcmp (name, "hardfloat") == 0)
16271 mips_opts.soft_float = 0;
16272 else if (strcmp (name, "singlefloat") == 0)
16273 mips_opts.single_float = 1;
16274 else if (strcmp (name, "doublefloat") == 0)
16275 mips_opts.single_float = 0;
351cdf24
MF
16276 else if (strcmp (name, "nooddspreg") == 0)
16277 mips_opts.oddspreg = 0;
16278 else if (strcmp (name, "oddspreg") == 0)
16279 mips_opts.oddspreg = 1;
252b5132
RH
16280 else if (strcmp (name, "mips16") == 0
16281 || strcmp (name, "MIPS-16") == 0)
919731af 16282 mips_opts.mips16 = 1;
252b5132
RH
16283 else if (strcmp (name, "nomips16") == 0
16284 || strcmp (name, "noMIPS-16") == 0)
16285 mips_opts.mips16 = 0;
df58fc94 16286 else if (strcmp (name, "micromips") == 0)
919731af 16287 mips_opts.micromips = 1;
df58fc94
RS
16288 else if (strcmp (name, "nomicromips") == 0)
16289 mips_opts.micromips = 0;
c6278170
RS
16290 else if (name[0] == 'n'
16291 && name[1] == 'o'
16292 && (ase = mips_lookup_ase (name + 2)))
919731af 16293 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16294 else if ((ase = mips_lookup_ase (name)))
919731af 16295 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16296 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16297 {
1a2c1fad
CD
16298 /* Permit the user to change the ISA and architecture on the fly.
16299 Needless to say, misuse can cause serious problems. */
919731af 16300 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16301 {
16302 const struct mips_cpu_info *p;
16303
919731af 16304 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16305 if (!p)
16306 as_bad (_("unknown architecture %s"), name + 5);
16307 else
16308 {
16309 mips_opts.arch = p->cpu;
16310 mips_opts.isa = p->isa;
22522f88 16311 isa_set = TRUE;
1a2c1fad
CD
16312 }
16313 }
81a21e38
TS
16314 else if (strncmp (name, "mips", 4) == 0)
16315 {
16316 const struct mips_cpu_info *p;
16317
919731af 16318 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16319 if (!p)
16320 as_bad (_("unknown ISA level %s"), name + 4);
16321 else
16322 {
16323 mips_opts.arch = p->cpu;
16324 mips_opts.isa = p->isa;
22522f88 16325 isa_set = TRUE;
81a21e38
TS
16326 }
16327 }
af7ee8bf 16328 else
81a21e38 16329 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16330 }
16331 else if (strcmp (name, "autoextend") == 0)
16332 mips_opts.noautoextend = 0;
16333 else if (strcmp (name, "noautoextend") == 0)
16334 mips_opts.noautoextend = 1;
833794fc
MR
16335 else if (strcmp (name, "insn32") == 0)
16336 mips_opts.insn32 = TRUE;
16337 else if (strcmp (name, "noinsn32") == 0)
16338 mips_opts.insn32 = FALSE;
919731af 16339 else if (strcmp (name, "sym32") == 0)
16340 mips_opts.sym32 = TRUE;
16341 else if (strcmp (name, "nosym32") == 0)
16342 mips_opts.sym32 = FALSE;
16343 else
22522f88
MR
16344 return OPTION_TYPE_BAD;
16345
16346 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16347}
16348
16349/* Handle the .set pseudo-op. */
16350
16351static void
16352s_mipsset (int x ATTRIBUTE_UNUSED)
16353{
22522f88 16354 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16355 char *name = input_line_pointer, ch;
919731af 16356
16357 file_mips_check_options ();
16358
16359 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16360 ++input_line_pointer;
16361 ch = *input_line_pointer;
16362 *input_line_pointer = '\0';
16363
16364 if (strchr (name, ','))
16365 {
16366 /* Generic ".set" directive; use the generic handler. */
16367 *input_line_pointer = ch;
16368 input_line_pointer = name;
16369 s_set (0);
16370 return;
16371 }
16372
16373 if (strcmp (name, "reorder") == 0)
16374 {
16375 if (mips_opts.noreorder)
16376 end_noreorder ();
16377 }
16378 else if (strcmp (name, "noreorder") == 0)
16379 {
16380 if (!mips_opts.noreorder)
16381 start_noreorder ();
16382 }
16383 else if (strcmp (name, "macro") == 0)
16384 mips_opts.warn_about_macros = 0;
16385 else if (strcmp (name, "nomacro") == 0)
16386 {
16387 if (mips_opts.noreorder == 0)
16388 as_bad (_("`noreorder' must be set before `nomacro'"));
16389 mips_opts.warn_about_macros = 1;
16390 }
16391 else if (strcmp (name, "gp=default") == 0)
16392 mips_opts.gp = file_mips_opts.gp;
16393 else if (strcmp (name, "fp=default") == 0)
16394 mips_opts.fp = file_mips_opts.fp;
16395 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16396 {
16397 mips_opts.isa = file_mips_opts.isa;
16398 mips_opts.arch = file_mips_opts.arch;
16399 mips_opts.gp = file_mips_opts.gp;
16400 mips_opts.fp = file_mips_opts.fp;
16401 }
252b5132
RH
16402 else if (strcmp (name, "push") == 0)
16403 {
16404 struct mips_option_stack *s;
16405
325801bd 16406 s = XNEW (struct mips_option_stack);
252b5132
RH
16407 s->next = mips_opts_stack;
16408 s->options = mips_opts;
16409 mips_opts_stack = s;
16410 }
16411 else if (strcmp (name, "pop") == 0)
16412 {
16413 struct mips_option_stack *s;
16414
16415 s = mips_opts_stack;
16416 if (s == NULL)
16417 as_bad (_(".set pop with no .set push"));
16418 else
16419 {
16420 /* If we're changing the reorder mode we need to handle
16421 delay slots correctly. */
16422 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16423 start_noreorder ();
252b5132 16424 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16425 end_noreorder ();
252b5132
RH
16426
16427 mips_opts = s->options;
16428 mips_opts_stack = s->next;
16429 free (s);
16430 }
16431 }
22522f88
MR
16432 else
16433 {
16434 type = parse_code_option (name);
16435 if (type == OPTION_TYPE_BAD)
16436 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16437 }
919731af 16438
16439 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16440 registers based on what is supported by the arch/cpu. */
22522f88 16441 if (type == OPTION_TYPE_ISA)
e6559e01 16442 {
919731af 16443 switch (mips_opts.isa)
16444 {
16445 case 0:
16446 break;
16447 case ISA_MIPS1:
351cdf24
MF
16448 /* MIPS I cannot support FPXX. */
16449 mips_opts.fp = 32;
16450 /* fall-through. */
919731af 16451 case ISA_MIPS2:
16452 case ISA_MIPS32:
16453 case ISA_MIPS32R2:
16454 case ISA_MIPS32R3:
16455 case ISA_MIPS32R5:
16456 mips_opts.gp = 32;
351cdf24
MF
16457 if (mips_opts.fp != 0)
16458 mips_opts.fp = 32;
919731af 16459 break;
7361da2c
AB
16460 case ISA_MIPS32R6:
16461 mips_opts.gp = 32;
16462 mips_opts.fp = 64;
16463 break;
919731af 16464 case ISA_MIPS3:
16465 case ISA_MIPS4:
16466 case ISA_MIPS5:
16467 case ISA_MIPS64:
16468 case ISA_MIPS64R2:
16469 case ISA_MIPS64R3:
16470 case ISA_MIPS64R5:
7361da2c 16471 case ISA_MIPS64R6:
919731af 16472 mips_opts.gp = 64;
351cdf24
MF
16473 if (mips_opts.fp != 0)
16474 {
16475 if (mips_opts.arch == CPU_R5900)
16476 mips_opts.fp = 32;
16477 else
16478 mips_opts.fp = 64;
16479 }
919731af 16480 break;
16481 default:
16482 as_bad (_("unknown ISA level %s"), name + 4);
16483 break;
16484 }
e6559e01 16485 }
919731af 16486
16487 mips_check_options (&mips_opts, FALSE);
16488
16489 mips_check_isa_supports_ases ();
16490 *input_line_pointer = ch;
16491 demand_empty_rest_of_line ();
16492}
16493
16494/* Handle the .module pseudo-op. */
16495
16496static void
16497s_module (int ignore ATTRIBUTE_UNUSED)
16498{
16499 char *name = input_line_pointer, ch;
16500
16501 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16502 ++input_line_pointer;
16503 ch = *input_line_pointer;
16504 *input_line_pointer = '\0';
16505
16506 if (!file_mips_opts_checked)
252b5132 16507 {
22522f88 16508 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16509 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16510
16511 /* Update module level settings from mips_opts. */
16512 file_mips_opts = mips_opts;
252b5132 16513 }
919731af 16514 else
16515 as_bad (_(".module is not permitted after generating code"));
16516
252b5132
RH
16517 *input_line_pointer = ch;
16518 demand_empty_rest_of_line ();
16519}
16520
16521/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16522 .option pic2. It means to generate SVR4 PIC calls. */
16523
16524static void
17a2f251 16525s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16526{
16527 mips_pic = SVR4_PIC;
143d77c5 16528 mips_abicalls = TRUE;
4d0d148d
TS
16529
16530 if (g_switch_seen && g_switch_value != 0)
16531 as_warn (_("-G may not be used with SVR4 PIC code"));
16532 g_switch_value = 0;
16533
252b5132
RH
16534 bfd_set_gp_size (stdoutput, 0);
16535 demand_empty_rest_of_line ();
16536}
16537
16538/* Handle the .cpload pseudo-op. This is used when generating SVR4
16539 PIC code. It sets the $gp register for the function based on the
16540 function address, which is in the register named in the argument.
16541 This uses a relocation against _gp_disp, which is handled specially
16542 by the linker. The result is:
16543 lui $gp,%hi(_gp_disp)
16544 addiu $gp,$gp,%lo(_gp_disp)
16545 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16546 The .cpload argument is normally $25 == $t9.
16547
16548 The -mno-shared option changes this to:
bbe506e8
TS
16549 lui $gp,%hi(__gnu_local_gp)
16550 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16551 and the argument is ignored. This saves an instruction, but the
16552 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16553 address for __gnu_local_gp. Thus code assembled with -mno-shared
16554 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16555
16556static void
17a2f251 16557s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16558{
16559 expressionS ex;
aa6975fb
ILT
16560 int reg;
16561 int in_shared;
252b5132 16562
919731af 16563 file_mips_check_options ();
16564
6478892d
TS
16565 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16566 .cpload is ignored. */
16567 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16568 {
16569 s_ignore (0);
16570 return;
16571 }
16572
a276b80c
MR
16573 if (mips_opts.mips16)
16574 {
16575 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16576 ignore_rest_of_line ();
16577 return;
16578 }
16579
d3ecfc59 16580 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16581 if (mips_opts.noreorder == 0)
16582 as_warn (_(".cpload not in noreorder section"));
16583
aa6975fb
ILT
16584 reg = tc_get_register (0);
16585
16586 /* If we need to produce a 64-bit address, we are better off using
16587 the default instruction sequence. */
aed1a261 16588 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16589
252b5132 16590 ex.X_op = O_symbol;
bbe506e8
TS
16591 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16592 "__gnu_local_gp");
252b5132
RH
16593 ex.X_op_symbol = NULL;
16594 ex.X_add_number = 0;
16595
16596 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16597 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16598
8a75745d
MR
16599 mips_mark_labels ();
16600 mips_assembling_insn = TRUE;
16601
584892a6 16602 macro_start ();
67c0d1eb
RS
16603 macro_build_lui (&ex, mips_gp_register);
16604 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16605 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16606 if (in_shared)
16607 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16608 mips_gp_register, reg);
584892a6 16609 macro_end ();
252b5132 16610
8a75745d 16611 mips_assembling_insn = FALSE;
252b5132
RH
16612 demand_empty_rest_of_line ();
16613}
16614
6478892d
TS
16615/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16616 .cpsetup $reg1, offset|$reg2, label
16617
16618 If offset is given, this results in:
16619 sd $gp, offset($sp)
956cd1d6 16620 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16621 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16622 daddu $gp, $gp, $reg1
6478892d
TS
16623
16624 If $reg2 is given, this results in:
40fc1451 16625 or $reg2, $gp, $0
956cd1d6 16626 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16627 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16628 daddu $gp, $gp, $reg1
aa6975fb
ILT
16629 $reg1 is normally $25 == $t9.
16630
16631 The -mno-shared option replaces the last three instructions with
16632 lui $gp,%hi(_gp)
54f4ddb3 16633 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16634
6478892d 16635static void
17a2f251 16636s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16637{
16638 expressionS ex_off;
16639 expressionS ex_sym;
16640 int reg1;
6478892d 16641
919731af 16642 file_mips_check_options ();
16643
8586fc66 16644 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16645 We also need NewABI support. */
16646 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16647 {
16648 s_ignore (0);
16649 return;
16650 }
16651
a276b80c
MR
16652 if (mips_opts.mips16)
16653 {
16654 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16655 ignore_rest_of_line ();
16656 return;
16657 }
16658
6478892d
TS
16659 reg1 = tc_get_register (0);
16660 SKIP_WHITESPACE ();
16661 if (*input_line_pointer != ',')
16662 {
16663 as_bad (_("missing argument separator ',' for .cpsetup"));
16664 return;
16665 }
16666 else
80245285 16667 ++input_line_pointer;
6478892d
TS
16668 SKIP_WHITESPACE ();
16669 if (*input_line_pointer == '$')
80245285
TS
16670 {
16671 mips_cpreturn_register = tc_get_register (0);
16672 mips_cpreturn_offset = -1;
16673 }
6478892d 16674 else
80245285
TS
16675 {
16676 mips_cpreturn_offset = get_absolute_expression ();
16677 mips_cpreturn_register = -1;
16678 }
6478892d
TS
16679 SKIP_WHITESPACE ();
16680 if (*input_line_pointer != ',')
16681 {
16682 as_bad (_("missing argument separator ',' for .cpsetup"));
16683 return;
16684 }
16685 else
f9419b05 16686 ++input_line_pointer;
6478892d 16687 SKIP_WHITESPACE ();
f21f8242 16688 expression (&ex_sym);
6478892d 16689
8a75745d
MR
16690 mips_mark_labels ();
16691 mips_assembling_insn = TRUE;
16692
584892a6 16693 macro_start ();
6478892d
TS
16694 if (mips_cpreturn_register == -1)
16695 {
16696 ex_off.X_op = O_constant;
16697 ex_off.X_add_symbol = NULL;
16698 ex_off.X_op_symbol = NULL;
16699 ex_off.X_add_number = mips_cpreturn_offset;
16700
67c0d1eb 16701 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16702 BFD_RELOC_LO16, SP);
6478892d
TS
16703 }
16704 else
40fc1451 16705 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16706
aed1a261 16707 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16708 {
df58fc94 16709 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16710 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16711 BFD_RELOC_HI16_S);
16712
16713 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16714 mips_gp_register, -1, BFD_RELOC_GPREL16,
16715 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16716
16717 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16718 mips_gp_register, reg1);
16719 }
16720 else
16721 {
16722 expressionS ex;
16723
16724 ex.X_op = O_symbol;
4184909a 16725 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16726 ex.X_op_symbol = NULL;
16727 ex.X_add_number = 0;
6e1304d8 16728
aa6975fb
ILT
16729 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16730 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16731
16732 macro_build_lui (&ex, mips_gp_register);
16733 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16734 mips_gp_register, BFD_RELOC_LO16);
16735 }
f21f8242 16736
584892a6 16737 macro_end ();
6478892d 16738
8a75745d 16739 mips_assembling_insn = FALSE;
6478892d
TS
16740 demand_empty_rest_of_line ();
16741}
16742
16743static void
17a2f251 16744s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16745{
919731af 16746 file_mips_check_options ();
16747
6478892d 16748 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16749 .cplocal is ignored. */
6478892d
TS
16750 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16751 {
16752 s_ignore (0);
16753 return;
16754 }
16755
a276b80c
MR
16756 if (mips_opts.mips16)
16757 {
16758 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16759 ignore_rest_of_line ();
16760 return;
16761 }
16762
6478892d 16763 mips_gp_register = tc_get_register (0);
85b51719 16764 demand_empty_rest_of_line ();
6478892d
TS
16765}
16766
252b5132
RH
16767/* Handle the .cprestore pseudo-op. This stores $gp into a given
16768 offset from $sp. The offset is remembered, and after making a PIC
16769 call $gp is restored from that location. */
16770
16771static void
17a2f251 16772s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16773{
16774 expressionS ex;
252b5132 16775
919731af 16776 file_mips_check_options ();
16777
6478892d 16778 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16779 .cprestore is ignored. */
6478892d 16780 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16781 {
16782 s_ignore (0);
16783 return;
16784 }
16785
a276b80c
MR
16786 if (mips_opts.mips16)
16787 {
16788 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16789 ignore_rest_of_line ();
16790 return;
16791 }
16792
252b5132 16793 mips_cprestore_offset = get_absolute_expression ();
7a621144 16794 mips_cprestore_valid = 1;
252b5132
RH
16795
16796 ex.X_op = O_constant;
16797 ex.X_add_symbol = NULL;
16798 ex.X_op_symbol = NULL;
16799 ex.X_add_number = mips_cprestore_offset;
16800
8a75745d
MR
16801 mips_mark_labels ();
16802 mips_assembling_insn = TRUE;
16803
584892a6 16804 macro_start ();
67c0d1eb
RS
16805 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16806 SP, HAVE_64BIT_ADDRESSES);
584892a6 16807 macro_end ();
252b5132 16808
8a75745d 16809 mips_assembling_insn = FALSE;
252b5132
RH
16810 demand_empty_rest_of_line ();
16811}
16812
6478892d 16813/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16814 was given in the preceding .cpsetup, it results in:
6478892d 16815 ld $gp, offset($sp)
76b3015f 16816
6478892d 16817 If a register $reg2 was given there, it results in:
40fc1451 16818 or $gp, $reg2, $0 */
54f4ddb3 16819
6478892d 16820static void
17a2f251 16821s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16822{
16823 expressionS ex;
6478892d 16824
919731af 16825 file_mips_check_options ();
16826
6478892d
TS
16827 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16828 We also need NewABI support. */
16829 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16830 {
16831 s_ignore (0);
16832 return;
16833 }
16834
a276b80c
MR
16835 if (mips_opts.mips16)
16836 {
16837 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16838 ignore_rest_of_line ();
16839 return;
16840 }
16841
8a75745d
MR
16842 mips_mark_labels ();
16843 mips_assembling_insn = TRUE;
16844
584892a6 16845 macro_start ();
6478892d
TS
16846 if (mips_cpreturn_register == -1)
16847 {
16848 ex.X_op = O_constant;
16849 ex.X_add_symbol = NULL;
16850 ex.X_op_symbol = NULL;
16851 ex.X_add_number = mips_cpreturn_offset;
16852
67c0d1eb 16853 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16854 }
16855 else
40fc1451
SD
16856 move_register (mips_gp_register, mips_cpreturn_register);
16857
584892a6 16858 macro_end ();
6478892d 16859
8a75745d 16860 mips_assembling_insn = FALSE;
6478892d
TS
16861 demand_empty_rest_of_line ();
16862}
16863
d0f13682
CLT
16864/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16865 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16866 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16867 debug information or MIPS16 TLS. */
741d6ea8
JM
16868
16869static void
d0f13682
CLT
16870s_tls_rel_directive (const size_t bytes, const char *dirstr,
16871 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16872{
16873 expressionS ex;
16874 char *p;
16875
16876 expression (&ex);
16877
16878 if (ex.X_op != O_symbol)
16879 {
1661c76c 16880 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16881 ignore_rest_of_line ();
16882 }
16883
16884 p = frag_more (bytes);
16885 md_number_to_chars (p, 0, bytes);
d0f13682 16886 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16887 demand_empty_rest_of_line ();
de64cffd 16888 mips_clear_insn_labels ();
741d6ea8
JM
16889}
16890
16891/* Handle .dtprelword. */
16892
16893static void
16894s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16895{
d0f13682 16896 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16897}
16898
16899/* Handle .dtpreldword. */
16900
16901static void
16902s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16903{
d0f13682
CLT
16904 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16905}
16906
16907/* Handle .tprelword. */
16908
16909static void
16910s_tprelword (int ignore ATTRIBUTE_UNUSED)
16911{
16912 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16913}
16914
16915/* Handle .tpreldword. */
16916
16917static void
16918s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16919{
16920 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16921}
16922
6478892d
TS
16923/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16924 code. It sets the offset to use in gp_rel relocations. */
16925
16926static void
17a2f251 16927s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16928{
16929 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16930 We also need NewABI support. */
16931 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16932 {
16933 s_ignore (0);
16934 return;
16935 }
16936
def2e0dd 16937 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16938
16939 demand_empty_rest_of_line ();
16940}
16941
252b5132
RH
16942/* Handle the .gpword pseudo-op. This is used when generating PIC
16943 code. It generates a 32 bit GP relative reloc. */
16944
16945static void
17a2f251 16946s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16947{
a8dbcb85
TS
16948 segment_info_type *si;
16949 struct insn_label_list *l;
252b5132
RH
16950 expressionS ex;
16951 char *p;
16952
16953 /* When not generating PIC code, this is treated as .word. */
16954 if (mips_pic != SVR4_PIC)
16955 {
16956 s_cons (2);
16957 return;
16958 }
16959
a8dbcb85
TS
16960 si = seg_info (now_seg);
16961 l = si->label_list;
7d10b47d 16962 mips_emit_delays ();
252b5132 16963 if (auto_align)
462427c4 16964 mips_align (2, 0, l);
252b5132
RH
16965
16966 expression (&ex);
a1facbec 16967 mips_clear_insn_labels ();
252b5132
RH
16968
16969 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16970 {
1661c76c 16971 as_bad (_("unsupported use of .gpword"));
252b5132
RH
16972 ignore_rest_of_line ();
16973 }
16974
16975 p = frag_more (4);
17a2f251 16976 md_number_to_chars (p, 0, 4);
b34976b6 16977 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16978 BFD_RELOC_GPREL32);
252b5132
RH
16979
16980 demand_empty_rest_of_line ();
16981}
16982
10181a0d 16983static void
17a2f251 16984s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16985{
a8dbcb85
TS
16986 segment_info_type *si;
16987 struct insn_label_list *l;
10181a0d
AO
16988 expressionS ex;
16989 char *p;
16990
16991 /* When not generating PIC code, this is treated as .dword. */
16992 if (mips_pic != SVR4_PIC)
16993 {
16994 s_cons (3);
16995 return;
16996 }
16997
a8dbcb85
TS
16998 si = seg_info (now_seg);
16999 l = si->label_list;
7d10b47d 17000 mips_emit_delays ();
10181a0d 17001 if (auto_align)
462427c4 17002 mips_align (3, 0, l);
10181a0d
AO
17003
17004 expression (&ex);
a1facbec 17005 mips_clear_insn_labels ();
10181a0d
AO
17006
17007 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17008 {
1661c76c 17009 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17010 ignore_rest_of_line ();
17011 }
17012
17013 p = frag_more (8);
17a2f251 17014 md_number_to_chars (p, 0, 8);
a105a300 17015 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17016 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17017
17018 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17019 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17020 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17021
17022 demand_empty_rest_of_line ();
17023}
17024
a3f278e2
CM
17025/* Handle the .ehword pseudo-op. This is used when generating unwinding
17026 tables. It generates a R_MIPS_EH reloc. */
17027
17028static void
17029s_ehword (int ignore ATTRIBUTE_UNUSED)
17030{
17031 expressionS ex;
17032 char *p;
17033
17034 mips_emit_delays ();
17035
17036 expression (&ex);
17037 mips_clear_insn_labels ();
17038
17039 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17040 {
1661c76c 17041 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17042 ignore_rest_of_line ();
17043 }
17044
17045 p = frag_more (4);
17046 md_number_to_chars (p, 0, 4);
17047 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17048 BFD_RELOC_32_PCREL);
a3f278e2
CM
17049
17050 demand_empty_rest_of_line ();
17051}
17052
252b5132
RH
17053/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17054 tables in SVR4 PIC code. */
17055
17056static void
17a2f251 17057s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17058{
252b5132
RH
17059 int reg;
17060
919731af 17061 file_mips_check_options ();
17062
10181a0d
AO
17063 /* This is ignored when not generating SVR4 PIC code. */
17064 if (mips_pic != SVR4_PIC)
252b5132
RH
17065 {
17066 s_ignore (0);
17067 return;
17068 }
17069
8a75745d
MR
17070 mips_mark_labels ();
17071 mips_assembling_insn = TRUE;
17072
252b5132 17073 /* Add $gp to the register named as an argument. */
584892a6 17074 macro_start ();
252b5132 17075 reg = tc_get_register (0);
67c0d1eb 17076 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17077 macro_end ();
252b5132 17078
8a75745d 17079 mips_assembling_insn = FALSE;
bdaaa2e1 17080 demand_empty_rest_of_line ();
252b5132
RH
17081}
17082
17083/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17084 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17085 such as generating jalx instructions when needed. We also make
17086 them odd for the duration of the assembly, in order to generate the
17087 right sort of code. We will make them even in the adjust_symtab
17088 routine, while leaving them marked. This is convenient for the
17089 debugger and the disassembler. The linker knows to make them odd
17090 again. */
17091
17092static void
17a2f251 17093s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17094{
7bb01e2d
MR
17095 file_mips_check_options ();
17096 file_ase_mips16 |= mips_opts.mips16;
17097 file_ase_micromips |= mips_opts.micromips;
17098
df58fc94 17099 mips_mark_labels ();
252b5132
RH
17100
17101 demand_empty_rest_of_line ();
17102}
17103
ba92f887
MR
17104/* Handle the .nan pseudo-op. */
17105
17106static void
17107s_nan (int ignore ATTRIBUTE_UNUSED)
17108{
17109 static const char str_legacy[] = "legacy";
17110 static const char str_2008[] = "2008";
17111 size_t i;
17112
17113 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17114
17115 if (i == sizeof (str_2008) - 1
17116 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17117 mips_nan2008 = 1;
ba92f887
MR
17118 else if (i == sizeof (str_legacy) - 1
17119 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17120 {
17121 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17122 mips_nan2008 = 0;
17123 else
17124 as_bad (_("`%s' does not support legacy NaN"),
17125 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17126 }
ba92f887 17127 else
1661c76c 17128 as_bad (_("bad .nan directive"));
ba92f887
MR
17129
17130 input_line_pointer += i;
17131 demand_empty_rest_of_line ();
17132}
17133
754e2bb9
RS
17134/* Handle a .stab[snd] directive. Ideally these directives would be
17135 implemented in a transparent way, so that removing them would not
17136 have any effect on the generated instructions. However, s_stab
17137 internally changes the section, so in practice we need to decide
17138 now whether the preceding label marks compressed code. We do not
17139 support changing the compression mode of a label after a .stab*
17140 directive, such as in:
17141
17142 foo:
134c0c8b 17143 .stabs ...
754e2bb9
RS
17144 .set mips16
17145
17146 so the current mode wins. */
252b5132
RH
17147
17148static void
17a2f251 17149s_mips_stab (int type)
252b5132 17150{
42c0794e 17151 file_mips_check_options ();
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 }
be00bddd 19142
defc8e2b 19143 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19144
19145 if (mips_32bitmode)
19146 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19147
7361da2c 19148 if (mips_nan2008 == 1)
ba92f887
MR
19149 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19150
ad3fea08 19151 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19152 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19153 Tag_GNU_MIPS_ABI_FP);
19154 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19155 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19156}
252b5132 19157\f
beae10d5 19158typedef struct proc {
9b2f1d35
EC
19159 symbolS *func_sym;
19160 symbolS *func_end_sym;
beae10d5
KH
19161 unsigned long reg_mask;
19162 unsigned long reg_offset;
19163 unsigned long fpreg_mask;
19164 unsigned long fpreg_offset;
19165 unsigned long frame_offset;
19166 unsigned long frame_reg;
19167 unsigned long pc_reg;
19168} procS;
252b5132
RH
19169
19170static procS cur_proc;
19171static procS *cur_proc_ptr;
19172static int numprocs;
19173
df58fc94
RS
19174/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19175 as "2", and a normal nop as "0". */
19176
19177#define NOP_OPCODE_MIPS 0
19178#define NOP_OPCODE_MIPS16 1
19179#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19180
19181char
19182mips_nop_opcode (void)
19183{
df58fc94
RS
19184 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19185 return NOP_OPCODE_MICROMIPS;
19186 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19187 return NOP_OPCODE_MIPS16;
19188 else
19189 return NOP_OPCODE_MIPS;
742a56fe
RS
19190}
19191
df58fc94
RS
19192/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19193 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19194
0a9ef439 19195void
17a2f251 19196mips_handle_align (fragS *fragp)
a19d8eb0 19197{
df58fc94 19198 char nop_opcode;
742a56fe 19199 char *p;
c67a084a
NC
19200 int bytes, size, excess;
19201 valueT opcode;
742a56fe 19202
0a9ef439
RH
19203 if (fragp->fr_type != rs_align_code)
19204 return;
19205
742a56fe 19206 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19207 nop_opcode = *p;
19208 switch (nop_opcode)
a19d8eb0 19209 {
df58fc94
RS
19210 case NOP_OPCODE_MICROMIPS:
19211 opcode = micromips_nop32_insn.insn_opcode;
19212 size = 4;
19213 break;
19214 case NOP_OPCODE_MIPS16:
c67a084a
NC
19215 opcode = mips16_nop_insn.insn_opcode;
19216 size = 2;
df58fc94
RS
19217 break;
19218 case NOP_OPCODE_MIPS:
19219 default:
c67a084a
NC
19220 opcode = nop_insn.insn_opcode;
19221 size = 4;
df58fc94 19222 break;
c67a084a 19223 }
a19d8eb0 19224
c67a084a
NC
19225 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19226 excess = bytes % size;
df58fc94
RS
19227
19228 /* Handle the leading part if we're not inserting a whole number of
19229 instructions, and make it the end of the fixed part of the frag.
19230 Try to fit in a short microMIPS NOP if applicable and possible,
19231 and use zeroes otherwise. */
19232 gas_assert (excess < 4);
19233 fragp->fr_fix += excess;
19234 switch (excess)
c67a084a 19235 {
df58fc94
RS
19236 case 3:
19237 *p++ = '\0';
19238 /* Fall through. */
19239 case 2:
833794fc 19240 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19241 {
4d68580a 19242 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19243 break;
19244 }
19245 *p++ = '\0';
19246 /* Fall through. */
19247 case 1:
19248 *p++ = '\0';
19249 /* Fall through. */
19250 case 0:
19251 break;
a19d8eb0 19252 }
c67a084a
NC
19253
19254 md_number_to_chars (p, opcode, size);
19255 fragp->fr_var = size;
a19d8eb0
CP
19256}
19257
252b5132 19258static long
17a2f251 19259get_number (void)
252b5132
RH
19260{
19261 int negative = 0;
19262 long val = 0;
19263
19264 if (*input_line_pointer == '-')
19265 {
19266 ++input_line_pointer;
19267 negative = 1;
19268 }
3882b010 19269 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19270 as_bad (_("expected simple number"));
252b5132
RH
19271 if (input_line_pointer[0] == '0')
19272 {
19273 if (input_line_pointer[1] == 'x')
19274 {
19275 input_line_pointer += 2;
3882b010 19276 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19277 {
19278 val <<= 4;
19279 val |= hex_value (*input_line_pointer++);
19280 }
19281 return negative ? -val : val;
19282 }
19283 else
19284 {
19285 ++input_line_pointer;
3882b010 19286 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19287 {
19288 val <<= 3;
19289 val |= *input_line_pointer++ - '0';
19290 }
19291 return negative ? -val : val;
19292 }
19293 }
3882b010 19294 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19295 {
19296 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19297 *input_line_pointer, *input_line_pointer);
956cd1d6 19298 as_warn (_("invalid number"));
252b5132
RH
19299 return -1;
19300 }
3882b010 19301 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19302 {
19303 val *= 10;
19304 val += *input_line_pointer++ - '0';
19305 }
19306 return negative ? -val : val;
19307}
19308
19309/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19310 is an initial number which is the ECOFF file index. In the non-ECOFF
19311 case .file implies DWARF-2. */
19312
19313static void
17a2f251 19314s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19315{
ecb4347a
DJ
19316 static int first_file_directive = 0;
19317
c5dd6aab
DJ
19318 if (ECOFF_DEBUGGING)
19319 {
19320 get_number ();
19321 s_app_file (0);
19322 }
19323 else
ecb4347a
DJ
19324 {
19325 char *filename;
19326
68d20676 19327 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19328
19329 /* Versions of GCC up to 3.1 start files with a ".file"
19330 directive even for stabs output. Make sure that this
19331 ".file" is handled. Note that you need a version of GCC
19332 after 3.1 in order to support DWARF-2 on MIPS. */
19333 if (filename != NULL && ! first_file_directive)
19334 {
19335 (void) new_logical_line (filename, -1);
c04f5787 19336 s_app_file_string (filename, 0);
ecb4347a
DJ
19337 }
19338 first_file_directive = 1;
19339 }
c5dd6aab
DJ
19340}
19341
19342/* The .loc directive, implying DWARF-2. */
252b5132
RH
19343
19344static void
17a2f251 19345s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19346{
c5dd6aab
DJ
19347 if (!ECOFF_DEBUGGING)
19348 dwarf2_directive_loc (0);
252b5132
RH
19349}
19350
252b5132
RH
19351/* The .end directive. */
19352
19353static void
17a2f251 19354s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19355{
19356 symbolS *p;
252b5132 19357
7a621144
DJ
19358 /* Following functions need their own .frame and .cprestore directives. */
19359 mips_frame_reg_valid = 0;
19360 mips_cprestore_valid = 0;
19361
252b5132
RH
19362 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19363 {
19364 p = get_symbol ();
19365 demand_empty_rest_of_line ();
19366 }
19367 else
19368 p = NULL;
19369
14949570 19370 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19371 as_warn (_(".end not in text section"));
19372
19373 if (!cur_proc_ptr)
19374 {
1661c76c 19375 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19376 demand_empty_rest_of_line ();
19377 return;
19378 }
19379
19380 if (p != NULL)
19381 {
9c2799c2 19382 gas_assert (S_GET_NAME (p));
9b2f1d35 19383 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19384 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19385
19386 if (debug_type == DEBUG_STABS)
19387 stabs_generate_asm_endfunc (S_GET_NAME (p),
19388 S_GET_NAME (p));
252b5132
RH
19389 }
19390 else
19391 as_warn (_(".end directive missing or unknown symbol"));
19392
9b2f1d35
EC
19393 /* Create an expression to calculate the size of the function. */
19394 if (p && cur_proc_ptr)
19395 {
19396 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19397 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19398
19399 obj->size = exp;
19400 exp->X_op = O_subtract;
19401 exp->X_add_symbol = symbol_temp_new_now ();
19402 exp->X_op_symbol = p;
19403 exp->X_add_number = 0;
19404
19405 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19406 }
19407
5ff6a06c
MR
19408#ifdef md_flush_pending_output
19409 md_flush_pending_output ();
19410#endif
19411
ecb4347a 19412 /* Generate a .pdr section. */
f3ded42a 19413 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19414 {
19415 segT saved_seg = now_seg;
19416 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19417 expressionS exp;
19418 char *fragp;
252b5132 19419
9c2799c2 19420 gas_assert (pdr_seg);
ecb4347a 19421 subseg_set (pdr_seg, 0);
252b5132 19422
ecb4347a
DJ
19423 /* Write the symbol. */
19424 exp.X_op = O_symbol;
19425 exp.X_add_symbol = p;
19426 exp.X_add_number = 0;
19427 emit_expr (&exp, 4);
252b5132 19428
ecb4347a 19429 fragp = frag_more (7 * 4);
252b5132 19430
17a2f251
TS
19431 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19432 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19433 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19434 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19435 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19436 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19437 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19438
ecb4347a
DJ
19439 subseg_set (saved_seg, saved_subseg);
19440 }
252b5132
RH
19441
19442 cur_proc_ptr = NULL;
19443}
19444
19445/* The .aent and .ent directives. */
19446
19447static void
17a2f251 19448s_mips_ent (int aent)
252b5132 19449{
252b5132 19450 symbolS *symbolP;
252b5132
RH
19451
19452 symbolP = get_symbol ();
19453 if (*input_line_pointer == ',')
f9419b05 19454 ++input_line_pointer;
252b5132 19455 SKIP_WHITESPACE ();
3882b010 19456 if (ISDIGIT (*input_line_pointer)
d9a62219 19457 || *input_line_pointer == '-')
874e8986 19458 get_number ();
252b5132 19459
14949570 19460 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19461 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19462
19463 if (!aent && cur_proc_ptr)
9a41af64 19464 as_warn (_("missing .end"));
252b5132
RH
19465
19466 if (!aent)
19467 {
7a621144
DJ
19468 /* This function needs its own .frame and .cprestore directives. */
19469 mips_frame_reg_valid = 0;
19470 mips_cprestore_valid = 0;
19471
252b5132
RH
19472 cur_proc_ptr = &cur_proc;
19473 memset (cur_proc_ptr, '\0', sizeof (procS));
19474
9b2f1d35 19475 cur_proc_ptr->func_sym = symbolP;
252b5132 19476
f9419b05 19477 ++numprocs;
ecb4347a
DJ
19478
19479 if (debug_type == DEBUG_STABS)
19480 stabs_generate_asm_func (S_GET_NAME (symbolP),
19481 S_GET_NAME (symbolP));
252b5132
RH
19482 }
19483
7c0fc524
MR
19484 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19485
252b5132
RH
19486 demand_empty_rest_of_line ();
19487}
19488
19489/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19490 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19491 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19492 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19493 symbol table (in the mdebug section). */
19494
19495static void
17a2f251 19496s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19497{
f3ded42a
RS
19498 if (ECOFF_DEBUGGING)
19499 s_ignore (ignore);
19500 else
ecb4347a
DJ
19501 {
19502 long val;
252b5132 19503
ecb4347a
DJ
19504 if (cur_proc_ptr == (procS *) NULL)
19505 {
19506 as_warn (_(".frame outside of .ent"));
19507 demand_empty_rest_of_line ();
19508 return;
19509 }
252b5132 19510
ecb4347a
DJ
19511 cur_proc_ptr->frame_reg = tc_get_register (1);
19512
19513 SKIP_WHITESPACE ();
19514 if (*input_line_pointer++ != ','
19515 || get_absolute_expression_and_terminator (&val) != ',')
19516 {
1661c76c 19517 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19518 --input_line_pointer;
19519 demand_empty_rest_of_line ();
19520 return;
19521 }
252b5132 19522
ecb4347a
DJ
19523 cur_proc_ptr->frame_offset = val;
19524 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19525
252b5132 19526 demand_empty_rest_of_line ();
252b5132 19527 }
252b5132
RH
19528}
19529
bdaaa2e1
KH
19530/* The .fmask and .mask directives. If the mdebug section is present
19531 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19532 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19533 information correctly. We can't use the ecoff routines because they
252b5132
RH
19534 make reference to the ecoff symbol table (in the mdebug section). */
19535
19536static void
17a2f251 19537s_mips_mask (int reg_type)
252b5132 19538{
f3ded42a
RS
19539 if (ECOFF_DEBUGGING)
19540 s_ignore (reg_type);
19541 else
252b5132 19542 {
ecb4347a 19543 long mask, off;
252b5132 19544
ecb4347a
DJ
19545 if (cur_proc_ptr == (procS *) NULL)
19546 {
19547 as_warn (_(".mask/.fmask outside of .ent"));
19548 demand_empty_rest_of_line ();
19549 return;
19550 }
252b5132 19551
ecb4347a
DJ
19552 if (get_absolute_expression_and_terminator (&mask) != ',')
19553 {
1661c76c 19554 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19555 --input_line_pointer;
19556 demand_empty_rest_of_line ();
19557 return;
19558 }
252b5132 19559
ecb4347a
DJ
19560 off = get_absolute_expression ();
19561
19562 if (reg_type == 'F')
19563 {
19564 cur_proc_ptr->fpreg_mask = mask;
19565 cur_proc_ptr->fpreg_offset = off;
19566 }
19567 else
19568 {
19569 cur_proc_ptr->reg_mask = mask;
19570 cur_proc_ptr->reg_offset = off;
19571 }
19572
19573 demand_empty_rest_of_line ();
252b5132 19574 }
252b5132
RH
19575}
19576
316f5878
RS
19577/* A table describing all the processors gas knows about. Names are
19578 matched in the order listed.
e7af610e 19579
316f5878
RS
19580 To ease comparison, please keep this table in the same order as
19581 gcc's mips_cpu_info_table[]. */
e972090a
NC
19582static const struct mips_cpu_info mips_cpu_info_table[] =
19583{
316f5878 19584 /* Entries for generic ISAs */
d16afab6
RS
19585 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19586 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19587 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19588 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19589 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19590 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19591 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19592 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19593 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19594 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19595 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19596 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19597 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19598 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19599 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19600
19601 /* MIPS I */
d16afab6
RS
19602 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19603 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19604 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19605
19606 /* MIPS II */
d16afab6 19607 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19608
19609 /* MIPS III */
d16afab6
RS
19610 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19611 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19612 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19613 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19614 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19615 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19616 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19617 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19618 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19619 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19620 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19621 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19622 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19623 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19624 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19625 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19626
19627 /* MIPS IV */
d16afab6
RS
19628 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19629 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19630 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19631 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19632 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19633 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19634 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19635 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19636 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19637 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19638 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19639 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19640 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19641 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19642 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19643
19644 /* MIPS 32 */
d16afab6
RS
19645 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19646 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19647 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19648 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19649
19650 /* MIPS 32 Release 2 */
d16afab6
RS
19651 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19652 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19653 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19654 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19655 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19656 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19657 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19658 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19659 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19660 ISA_MIPS32R2, CPU_MIPS32R2 },
19661 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19662 ISA_MIPS32R2, CPU_MIPS32R2 },
19663 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19664 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19665 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19666 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19667 /* Deprecated forms of the above. */
d16afab6
RS
19668 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19669 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19670 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19671 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19672 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19673 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19674 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19675 /* Deprecated forms of the above. */
d16afab6
RS
19676 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19677 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19678 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19679 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19680 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19681 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19682 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19683 /* Deprecated forms of the above. */
d16afab6
RS
19684 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19685 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19686 /* 34Kn is a 34kc without DSP. */
d16afab6 19687 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19688 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19689 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19690 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19691 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19692 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19693 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19694 /* Deprecated forms of the above. */
d16afab6
RS
19695 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19696 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19697 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19698 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19699 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19700 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19701 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19702 /* interaptiv is the new name for 1004kf */
19703 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19704 { "interaptiv-mr2", 0,
19705 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19706 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
c6e5c03a
RS
19707 /* M5100 family */
19708 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19709 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19710 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19711 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19712
316f5878 19713 /* MIPS 64 */
d16afab6
RS
19714 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19715 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19716 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19717 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19718
c7a23324 19719 /* Broadcom SB-1 CPU core */
d16afab6 19720 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19721 /* Broadcom SB-1A CPU core */
d16afab6 19722 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19723
4ba154f5 19724 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 19725
ed163775
MR
19726 /* MIPS 64 Release 2 */
19727
967344c6 19728 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19729 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19730 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19731 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19732 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19733
52b6b6b9 19734 /* RMI Xlr */
d16afab6 19735 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19736
55a36193
MK
19737 /* Broadcom XLP.
19738 XLP is mostly like XLR, with the prominent exception that it is
19739 MIPS64R2 rather than MIPS64. */
d16afab6 19740 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19741
a4968f42 19742 /* MIPS 64 Release 6 */
7ef0d297 19743 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19744 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19745
316f5878 19746 /* End marker */
d16afab6 19747 { NULL, 0, 0, 0, 0 }
316f5878 19748};
e7af610e 19749
84ea6cf2 19750
316f5878
RS
19751/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19752 with a final "000" replaced by "k". Ignore case.
e7af610e 19753
316f5878 19754 Note: this function is shared between GCC and GAS. */
c6c98b38 19755
b34976b6 19756static bfd_boolean
17a2f251 19757mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19758{
19759 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19760 given++, canonical++;
19761
19762 return ((*given == 0 && *canonical == 0)
19763 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19764}
19765
19766
19767/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19768 CPU name. We've traditionally allowed a lot of variation here.
19769
19770 Note: this function is shared between GCC and GAS. */
19771
b34976b6 19772static bfd_boolean
17a2f251 19773mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19774{
19775 /* First see if the name matches exactly, or with a final "000"
19776 turned into "k". */
19777 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19778 return TRUE;
316f5878
RS
19779
19780 /* If not, try comparing based on numerical designation alone.
19781 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19782 if (TOLOWER (*given) == 'r')
19783 given++;
19784 if (!ISDIGIT (*given))
b34976b6 19785 return FALSE;
316f5878
RS
19786
19787 /* Skip over some well-known prefixes in the canonical name,
19788 hoping to find a number there too. */
19789 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19790 canonical += 2;
19791 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19792 canonical += 2;
19793 else if (TOLOWER (canonical[0]) == 'r')
19794 canonical += 1;
19795
19796 return mips_strict_matching_cpu_name_p (canonical, given);
19797}
19798
19799
19800/* Parse an option that takes the name of a processor as its argument.
19801 OPTION is the name of the option and CPU_STRING is the argument.
19802 Return the corresponding processor enumeration if the CPU_STRING is
19803 recognized, otherwise report an error and return null.
19804
19805 A similar function exists in GCC. */
e7af610e
NC
19806
19807static const struct mips_cpu_info *
17a2f251 19808mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19809{
316f5878 19810 const struct mips_cpu_info *p;
e7af610e 19811
316f5878
RS
19812 /* 'from-abi' selects the most compatible architecture for the given
19813 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19814 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19815 version. Look first at the -mgp options, if given, otherwise base
19816 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19817
316f5878
RS
19818 Treat NO_ABI like the EABIs. One reason to do this is that the
19819 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19820 architecture. This code picks MIPS I for 'mips' and MIPS III for
19821 'mips64', just as we did in the days before 'from-abi'. */
19822 if (strcasecmp (cpu_string, "from-abi") == 0)
19823 {
19824 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19825 return mips_cpu_info_from_isa (ISA_MIPS1);
19826
19827 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19828 return mips_cpu_info_from_isa (ISA_MIPS3);
19829
bad1aba3 19830 if (file_mips_opts.gp >= 0)
19831 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19832 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19833
19834 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19835 ? ISA_MIPS3
19836 : ISA_MIPS1);
19837 }
19838
19839 /* 'default' has traditionally been a no-op. Probably not very useful. */
19840 if (strcasecmp (cpu_string, "default") == 0)
19841 return 0;
19842
19843 for (p = mips_cpu_info_table; p->name != 0; p++)
19844 if (mips_matching_cpu_name_p (p->name, cpu_string))
19845 return p;
19846
1661c76c 19847 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19848 return 0;
e7af610e
NC
19849}
19850
316f5878
RS
19851/* Return the canonical processor information for ISA (a member of the
19852 ISA_MIPS* enumeration). */
19853
e7af610e 19854static const struct mips_cpu_info *
17a2f251 19855mips_cpu_info_from_isa (int isa)
e7af610e
NC
19856{
19857 int i;
19858
19859 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19860 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19861 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19862 return (&mips_cpu_info_table[i]);
19863
e972090a 19864 return NULL;
e7af610e 19865}
fef14a42
TS
19866
19867static const struct mips_cpu_info *
17a2f251 19868mips_cpu_info_from_arch (int arch)
fef14a42
TS
19869{
19870 int i;
19871
19872 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19873 if (arch == mips_cpu_info_table[i].cpu)
19874 return (&mips_cpu_info_table[i]);
19875
19876 return NULL;
19877}
316f5878
RS
19878\f
19879static void
17a2f251 19880show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19881{
19882 if (*first_p)
19883 {
19884 fprintf (stream, "%24s", "");
19885 *col_p = 24;
19886 }
19887 else
19888 {
19889 fprintf (stream, ", ");
19890 *col_p += 2;
19891 }
e7af610e 19892
316f5878
RS
19893 if (*col_p + strlen (string) > 72)
19894 {
19895 fprintf (stream, "\n%24s", "");
19896 *col_p = 24;
19897 }
19898
19899 fprintf (stream, "%s", string);
19900 *col_p += strlen (string);
19901
19902 *first_p = 0;
19903}
19904
19905void
17a2f251 19906md_show_usage (FILE *stream)
e7af610e 19907{
316f5878
RS
19908 int column, first;
19909 size_t i;
19910
19911 fprintf (stream, _("\
19912MIPS options:\n\
316f5878
RS
19913-EB generate big endian output\n\
19914-EL generate little endian output\n\
19915-g, -g2 do not remove unneeded NOPs or swap branches\n\
19916-G NUM allow referencing objects up to NUM bytes\n\
19917 implicitly with the gp register [default 8]\n"));
19918 fprintf (stream, _("\
19919-mips1 generate MIPS ISA I instructions\n\
19920-mips2 generate MIPS ISA II instructions\n\
19921-mips3 generate MIPS ISA III instructions\n\
19922-mips4 generate MIPS ISA IV instructions\n\
19923-mips5 generate MIPS ISA V instructions\n\
19924-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19925-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19926-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19927-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19928-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19929-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19930-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19931-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19932-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19933-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19934-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19935
19936 first = 1;
e7af610e
NC
19937
19938 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19939 show (stream, mips_cpu_info_table[i].name, &column, &first);
19940 show (stream, "from-abi", &column, &first);
19941 fputc ('\n', stream);
e7af610e 19942
316f5878
RS
19943 fprintf (stream, _("\
19944-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19945-no-mCPU don't generate code specific to CPU.\n\
19946 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19947
19948 first = 1;
19949
19950 show (stream, "3900", &column, &first);
19951 show (stream, "4010", &column, &first);
19952 show (stream, "4100", &column, &first);
19953 show (stream, "4650", &column, &first);
19954 fputc ('\n', stream);
19955
19956 fprintf (stream, _("\
19957-mips16 generate mips16 instructions\n\
19958-no-mips16 do not generate mips16 instructions\n"));
19959 fprintf (stream, _("\
f866b262
MR
19960-mmips16e2 generate MIPS16e2 instructions\n\
19961-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
19962 fprintf (stream, _("\
df58fc94
RS
19963-mmicromips generate microMIPS instructions\n\
19964-mno-micromips do not generate microMIPS instructions\n"));
19965 fprintf (stream, _("\
e16bfa71 19966-msmartmips generate smartmips instructions\n\
3739860c 19967-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 19968 fprintf (stream, _("\
74cd071d
CF
19969-mdsp generate DSP instructions\n\
19970-mno-dsp do not generate DSP instructions\n"));
19971 fprintf (stream, _("\
8b082fb1
TS
19972-mdspr2 generate DSP R2 instructions\n\
19973-mno-dspr2 do not generate DSP R2 instructions\n"));
19974 fprintf (stream, _("\
8f4f9071
MF
19975-mdspr3 generate DSP R3 instructions\n\
19976-mno-dspr3 do not generate DSP R3 instructions\n"));
19977 fprintf (stream, _("\
ef2e4d86
CF
19978-mmt generate MT instructions\n\
19979-mno-mt do not generate MT instructions\n"));
19980 fprintf (stream, _("\
dec0624d
MR
19981-mmcu generate MCU instructions\n\
19982-mno-mcu do not generate MCU instructions\n"));
19983 fprintf (stream, _("\
56d438b1
CF
19984-mmsa generate MSA instructions\n\
19985-mno-msa do not generate MSA instructions\n"));
19986 fprintf (stream, _("\
7d64c587
AB
19987-mxpa generate eXtended Physical Address (XPA) instructions\n\
19988-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19989 fprintf (stream, _("\
b015e599
AP
19990-mvirt generate Virtualization instructions\n\
19991-mno-virt do not generate Virtualization instructions\n"));
19992 fprintf (stream, _("\
833794fc
MR
19993-minsn32 only generate 32-bit microMIPS instructions\n\
19994-mno-insn32 generate all microMIPS instructions\n"));
19995 fprintf (stream, _("\
c67a084a
NC
19996-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19997-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19998-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19999-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20000-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20001-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
20002-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20003-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20004-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
20005-O0 remove unneeded NOPs, do not swap branches\n\
20006-O remove unneeded NOPs and swap branches\n\
316f5878
RS
20007--trap, --no-break trap exception on div by 0 and mult overflow\n\
20008--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20009 fprintf (stream, _("\
20010-mhard-float allow floating-point instructions\n\
20011-msoft-float do not allow floating-point instructions\n\
20012-msingle-float only allow 32-bit floating-point operations\n\
20013-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20014--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20015--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20016-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20017-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20018-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20019
20020 first = 1;
20021
20022 show (stream, "legacy", &column, &first);
20023 show (stream, "2008", &column, &first);
20024
20025 fputc ('\n', stream);
20026
316f5878
RS
20027 fprintf (stream, _("\
20028-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20029-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20030-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20031-non_shared do not generate code that can operate with DSOs\n\
316f5878 20032-xgot assume a 32 bit GOT\n\
dcd410fe 20033-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20034-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20035 position dependent (non shared) code\n\
316f5878
RS
20036-mabi=ABI create ABI conformant object file for:\n"));
20037
20038 first = 1;
20039
20040 show (stream, "32", &column, &first);
20041 show (stream, "o64", &column, &first);
20042 show (stream, "n32", &column, &first);
20043 show (stream, "64", &column, &first);
20044 show (stream, "eabi", &column, &first);
20045
20046 fputc ('\n', stream);
20047
20048 fprintf (stream, _("\
b4f6242e
MR
20049-32 create o32 ABI object file%s\n"),
20050 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20051 fprintf (stream, _("\
20052-n32 create n32 ABI object file%s\n"),
20053 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20054 fprintf (stream, _("\
20055-64 create 64 ABI object file%s\n"),
20056 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20057}
14e777e0 20058
1575952e 20059#ifdef TE_IRIX
14e777e0 20060enum dwarf2_format
413a266c 20061mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20062{
369943fe 20063 if (HAVE_64BIT_SYMBOLS)
1575952e 20064 return dwarf2_format_64bit_irix;
14e777e0
KB
20065 else
20066 return dwarf2_format_32bit;
20067}
1575952e 20068#endif
73369e65
EC
20069
20070int
20071mips_dwarf2_addr_size (void)
20072{
6b6b3450 20073 if (HAVE_64BIT_OBJECTS)
73369e65 20074 return 8;
73369e65
EC
20075 else
20076 return 4;
20077}
5862107c
EC
20078
20079/* Standard calling conventions leave the CFA at SP on entry. */
20080void
20081mips_cfi_frame_initial_instructions (void)
20082{
20083 cfi_add_CFA_def_cfa_register (SP);
20084}
20085
707bfff6
TS
20086int
20087tc_mips_regname_to_dw2regnum (char *regname)
20088{
20089 unsigned int regnum = -1;
20090 unsigned int reg;
20091
20092 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20093 regnum = reg;
20094
20095 return regnum;
20096}
263b2574 20097
20098/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20099 Given a symbolic attribute NAME, return the proper integer value.
20100 Returns -1 if the attribute is not known. */
20101
20102int
20103mips_convert_symbolic_attribute (const char *name)
20104{
20105 static const struct
20106 {
20107 const char * name;
20108 const int tag;
20109 }
20110 attribute_table[] =
20111 {
20112#define T(tag) {#tag, tag}
20113 T (Tag_GNU_MIPS_ABI_FP),
20114 T (Tag_GNU_MIPS_ABI_MSA),
20115#undef T
20116 };
20117 unsigned int i;
20118
20119 if (name == NULL)
20120 return -1;
20121
20122 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20123 if (streq (name, attribute_table[i].name))
20124 return attribute_table[i].tag;
20125
20126 return -1;
20127}
fd5c94ab
RS
20128
20129void
20130md_mips_end (void)
20131{
351cdf24
MF
20132 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20133
fd5c94ab
RS
20134 mips_emit_delays ();
20135 if (cur_proc_ptr)
20136 as_warn (_("missing .end at end of assembly"));
919731af 20137
20138 /* Just in case no code was emitted, do the consistency check. */
20139 file_mips_check_options ();
351cdf24
MF
20140
20141 /* Set a floating-point ABI if the user did not. */
20142 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20143 {
20144 /* Perform consistency checks on the floating-point ABI. */
20145 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20146 Tag_GNU_MIPS_ABI_FP);
20147 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20148 check_fpabi (fpabi);
20149 }
20150 else
20151 {
20152 /* Soft-float gets precedence over single-float, the two options should
20153 not be used together so this should not matter. */
20154 if (file_mips_opts.soft_float == 1)
20155 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20156 /* Single-float gets precedence over all double_float cases. */
20157 else if (file_mips_opts.single_float == 1)
20158 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20159 else
20160 {
20161 switch (file_mips_opts.fp)
20162 {
20163 case 32:
20164 if (file_mips_opts.gp == 32)
20165 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20166 break;
20167 case 0:
20168 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20169 break;
20170 case 64:
20171 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20172 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20173 else if (file_mips_opts.gp == 32)
20174 fpabi = Val_GNU_MIPS_ABI_FP_64;
20175 else
20176 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20177 break;
20178 }
20179 }
20180
20181 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20182 Tag_GNU_MIPS_ABI_FP, fpabi);
20183 }
fd5c94ab 20184}
2f0c68f2
CM
20185
20186/* Returns the relocation type required for a particular CFI encoding. */
20187
20188bfd_reloc_code_real_type
20189mips_cfi_reloc_for_encoding (int encoding)
20190{
20191 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20192 return BFD_RELOC_32_PCREL;
20193 else return BFD_RELOC_NONE;
20194}
This page took 3.121142 seconds and 4 git commands to generate.