Make sure malloc is linked into gdb.cp/oranking.cc.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
2571583a 2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24
MF
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
af7ee8bf 426
ad3fea08
TS
427/* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429#define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439/* Return true if ISA supports legacy NAN. */
440#define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
ae52f483
AB
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
ad3fea08 454
bad1aba3 455#define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
ca4e0257 459
bad1aba3 460#define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
ca4e0257 464
316f5878 465#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 466
316f5878 467#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 468
3b91255e
RS
469/* True if relocations are stored in-place. */
470#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
aed1a261
RS
472/* The ABI-derived address size. */
473#define HAVE_64BIT_ADDRESSES \
bad1aba3 474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 475#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 476
aed1a261
RS
477/* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479#define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 482
b7c7d6c1
TS
483/* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
f899b4b8 486#define ADDRESS_ADD_INSN \
b7c7d6c1 487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
488
489#define ADDRESS_ADDI_INSN \
b7c7d6c1 490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
491
492#define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495#define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
a4672219 498/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
499#define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 502
2309ddf2 503/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
504#define CPU_HAS_MICROMIPS(cpu) 0
505
60b63b72
RS
506/* True if CPU has a dror instruction. */
507#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509/* True if CPU has a ror instruction. */
510#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
dd6a37e7 512/* True if CPU is in the Octeon family */
2c629856
N
513#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 515
dd3cbb7e 516/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 517#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 518
0aa27725
RS
519/* True, if CPU has support for ldc1 and sdc1. */
520#define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
c8978940
CD
523/* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535#define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 540 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 545 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 546 || mips_opts.arch == CPU_R4010 \
e407c74b 547 || mips_opts.arch == CPU_R5900 \
c8978940
CD
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
c8978940 552 || mips_opts.arch == CPU_RM7000 \
c8978940 553 || mips_opts.arch == CPU_VR5500 \
df58fc94 554 || mips_opts.micromips \
c8978940 555 )
252b5132
RH
556
557/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
67dc82bc 560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
561 level I and microMIPS mode instructions are always interlocked. */
562#define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
e407c74b 565 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
566 || mips_opts.micromips \
567 )
252b5132 568
81912461
ILT
569/* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
43885403
MF
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
bdaaa2e1 577/* Itbl support may require additional care here. */
81912461
ILT
578#define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
df58fc94 583 || mips_opts.micromips \
81912461
ILT
584 )
585
586/* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592#define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
252b5132 596
6b76fefe
CM
597/* Is this a mfhi or mflo instruction? */
598#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
df58fc94
RS
601/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604#define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
42429eac 607/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 608#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
609#define GPR_SMIN (-GPR_SMAX - 1)
610
252b5132
RH
611/* MIPS PIC level. */
612
a161fe53 613enum mips_pic_level mips_pic;
252b5132 614
c9914766 615/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 616 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 617static int mips_big_got = 0;
252b5132
RH
618
619/* 1 if trap instructions should used for overflow rather than break
620 instructions. */
c9914766 621static int mips_trap = 0;
252b5132 622
119d663a 623/* 1 if double width floating point constants should not be constructed
b6ff326e 624 by assembling two single width halves into two single width floating
119d663a
NC
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
d547a75e 627 in the status register, and the setting of this bit cannot be determined
119d663a
NC
628 automatically at assemble time. */
629static int mips_disable_float_construction;
630
252b5132
RH
631/* Non-zero if any .set noreorder directives were used. */
632
633static int mips_any_noreorder;
634
6b76fefe
CM
635/* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637static int mips_7000_hilo_fix;
638
02ffd3e4 639/* The size of objects in the small data section. */
156c2f8b 640static unsigned int g_switch_value = 8;
252b5132
RH
641/* Whether the -G option was used. */
642static int g_switch_seen = 0;
643
644#define N_RMASK 0xc4
645#define N_VFP 0xd4
646
647/* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
252b5132
RH
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 659static int nopic_need_relax (symbolS *, int);
252b5132
RH
660
661/* handle of the OPCODE hash table */
662static struct hash_control *op_hash = NULL;
663
664/* The opcode hash table we use for the mips16. */
665static struct hash_control *mips16_op_hash = NULL;
666
df58fc94
RS
667/* The opcode hash table we use for the microMIPS ASE. */
668static struct hash_control *micromips_op_hash = NULL;
669
252b5132
RH
670/* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672const char comment_chars[] = "#";
673
674/* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677/* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
bdaaa2e1 679 #NO_APP at the beginning of its output. */
252b5132
RH
680/* Also note that C style comments are always supported. */
681const char line_comment_chars[] = "#";
682
bdaaa2e1 683/* This array holds machine specific line separator characters. */
63a0b638 684const char line_separator_chars[] = ";";
252b5132
RH
685
686/* Chars that can be used to separate mant from exp in floating point nums */
687const char EXP_CHARS[] = "eE";
688
689/* Chars that mean this number is a floating point constant */
690/* As in 0f12.456 */
691/* or 0d1.2345e12 */
692const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
e3de51ce
RS
699/* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705};
706
707/* Information about an error that was found while assembling the current
708 instruction. */
709struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732};
733
734/* The error that should be reported for the current instruction. */
735static struct mips_insn_error insn_error;
252b5132
RH
736
737static int auto_align = 1;
738
739/* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743static offsetT mips_cprestore_offset = -1;
744
67c1ffbe 745/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 746 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 747 offset and even an other register than $gp as global pointer. */
6478892d
TS
748static offsetT mips_cpreturn_offset = -1;
749static int mips_cpreturn_register = -1;
750static int mips_gp_register = GP;
def2e0dd 751static int mips_gprel_offset = 0;
6478892d 752
7a621144
DJ
753/* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755static int mips_cprestore_valid = 0;
756
252b5132
RH
757/* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759static int mips_frame_reg = SP;
760
7a621144
DJ
761/* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763static int mips_frame_reg_valid = 0;
764
252b5132
RH
765/* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768/* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772static int mips_optimize = 2;
773
774/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776static int mips_debug = 0;
777
7d8e00cf
RS
778/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779#define MAX_VR4130_NOPS 4
780
781/* The maximum number of NOPs needed to fill delay slots. */
782#define MAX_DELAY_NOPS 2
783
784/* The maximum number of NOPs needed for any purpose. */
785#define MAX_NOPS 4
71400594
RS
786
787/* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 793
fc76e730 794/* Arrays of operands for each instruction. */
14daeee3 795#define MAX_OPERANDS 6
fc76e730
RS
796struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798};
799static struct mips_operand_array *mips_operands;
800static struct mips_operand_array *mips16_operands;
801static struct mips_operand_array *micromips_operands;
802
1e915849 803/* Nop instructions used by emit_nop. */
df58fc94
RS
804static struct mips_cl_insn nop_insn;
805static struct mips_cl_insn mips16_nop_insn;
806static struct mips_cl_insn micromips_nop16_insn;
807static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
808
809/* The appropriate nop for the current mode. */
833794fc
MR
810#define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
df58fc94
RS
817
818/* The size of NOP_INSN in bytes. */
833794fc
MR
819#define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
252b5132 822
252b5132
RH
823/* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827static fragS *prev_nop_frag;
828
829/* The number of nop instructions we created in prev_nop_frag. */
830static int prev_nop_frag_holds;
831
832/* The number of nop instructions that we know we need in
bdaaa2e1 833 prev_nop_frag. */
252b5132
RH
834static int prev_nop_frag_required;
835
836/* The number of instructions we've seen since prev_nop_frag. */
837static int prev_nop_frag_since;
838
e8044f35
RS
839/* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
252b5132
RH
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
bdaaa2e1 848 corresponding LO relocation. */
252b5132 849
e972090a
NC
850struct mips_hi_fixup
851{
252b5132
RH
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858};
859
860/* The list of unmatched HI relocs. */
861
862static struct mips_hi_fixup *mips_hi_fixup_list;
863
64bdfcaf
RS
864/* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867static fragS *prev_reloc_op_frag;
868
252b5132
RH
869/* Map mips16 register numbers to normal MIPS register numbers. */
870
e972090a
NC
871static const unsigned int mips16_to_32_reg_map[] =
872{
252b5132
RH
873 16, 17, 2, 3, 4, 5, 6, 7
874};
60b63b72 875
df58fc94
RS
876/* Map microMIPS register numbers to normal MIPS register numbers. */
877
df58fc94 878#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
879
880/* The microMIPS registers with type h. */
e76ff5ab 881static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
882{
883 5, 5, 6, 4, 4, 4, 4, 4
884};
e76ff5ab 885static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
886{
887 6, 7, 7, 21, 22, 5, 6, 7
888};
889
df58fc94
RS
890/* The microMIPS registers with type m. */
891static const unsigned int micromips_to_32_reg_m_map[] =
892{
893 0, 17, 2, 3, 16, 18, 19, 20
894};
895
896#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
71400594
RS
898/* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
c67a084a
NC
900enum fix_vr4120_class
901{
71400594
RS
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909};
910
c67a084a
NC
911/* ...likewise -mfix-loongson2f-jump. */
912static bfd_boolean mips_fix_loongson2f_jump;
913
914/* ...likewise -mfix-loongson2f-nop. */
915static bfd_boolean mips_fix_loongson2f_nop;
916
917/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918static bfd_boolean mips_fix_loongson2f;
919
71400594
RS
920/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925/* True if -mfix-vr4120 is in force. */
d766e8ec 926static int mips_fix_vr4120;
4a6a3df4 927
7d8e00cf
RS
928/* ...likewise -mfix-vr4130. */
929static int mips_fix_vr4130;
930
6a32d874
CM
931/* ...likewise -mfix-24k. */
932static int mips_fix_24k;
933
a8d14a88
CM
934/* ...likewise -mfix-rm7000 */
935static int mips_fix_rm7000;
936
d954098f
DD
937/* ...likewise -mfix-cn63xxp1 */
938static bfd_boolean mips_fix_cn63xxp1;
939
4a6a3df4
AO
940/* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945static int mips_relax_branch;
8b10b0b3
MR
946
947/* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950static bfd_boolean mips_ignore_branch_isa;
252b5132 951\f
4d7206a2
RS
952/* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
584892a6
RS
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
4d7206a2 969
ce8ad872
MR
970 RELAX_PIC
971 Set if generating PIC code.
972
584892a6
RS
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
4d7206a2 989
df58fc94
RS
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
4d7206a2
RS
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
ce8ad872
MR
1014#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1016
584892a6
RS
1017#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1019#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020#define RELAX_USE_SECOND 0x20000
1021#define RELAX_SECOND_LONGER 0x40000
1022#define RELAX_NOMACRO 0x80000
1023#define RELAX_DELAY_SLOT 0x100000
1024#define RELAX_DELAY_SLOT_16BIT 0x200000
1025#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1027
4a6a3df4
AO
1028/* Branch without likely bit. If label is out of range, we turn:
1029
134c0c8b 1030 beq reg1, reg2, label
4a6a3df4
AO
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
b34976b6 1074
4a6a3df4
AO
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1095#define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
66b3e8da
MR
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
ce8ad872
MR
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1105#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1106#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1111#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1112
252b5132
RH
1113/* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
1133#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1134 (0x80000000 \
1135 | ((type) & 0xff) \
1136 | ((small) ? 0x100 : 0) \
1137 | ((ext) ? 0x200 : 0) \
1138 | ((dslot) ? 0x400 : 0) \
1139 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1140#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1141#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1142#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1143#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1144#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1145#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1146#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1147#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1148#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1425c41d
MR
1149#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x2000) != 0)
1150#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x2000)
1151#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x2000)
885add95 1152
df58fc94
RS
1153/* For microMIPS code, we use relaxation similar to one we use for
1154 MIPS16 code. Some instructions that take immediate values support
1155 two encodings: a small one which takes some small value, and a
1156 larger one which takes a 16 bit value. As some branches also follow
1157 this pattern, relaxing these values is required.
1158
1159 We can assemble both microMIPS and normal MIPS code in a single
1160 object. Therefore, we need to support this type of relaxation at
1161 the same time that we support the relaxation described above. We
1162 use one of the high bits of the subtype field to distinguish these
1163 cases.
1164
1165 The information we store for this type of relaxation is the argument
1166 code found in the opcode file for this relocation, the register
8484fb75
MR
1167 selected as the assembler temporary, whether in the 32-bit
1168 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1169 compact, whether there is no delay-slot instruction available to fill
1170 in, whether it stores the link address implicitly in $ra, whether
1171 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1172 instructions is enabled, and whether the displacement of a branch is
1173 too large to fit as an immediate argument of a 16-bit and a 32-bit
1174 branch, respectively. */
ce8ad872 1175#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1176 uncond, compact, link, nods, \
40209cad
MR
1177 relax32, toofar16, toofar32) \
1178 (0x40000000 \
1179 | ((type) & 0xff) \
1180 | (((at) & 0x1f) << 8) \
8484fb75 1181 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1182 | ((pic) ? 0x4000 : 0) \
1183 | ((uncond) ? 0x8000 : 0) \
1184 | ((compact) ? 0x10000 : 0) \
1185 | ((link) ? 0x20000 : 0) \
1186 | ((nods) ? 0x40000 : 0) \
1187 | ((relax32) ? 0x80000 : 0) \
1188 | ((toofar16) ? 0x100000 : 0) \
1189 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1190#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1191#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1192#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1193#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1194#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1195#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1196#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1197#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1198#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1199#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1200
1201#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1202#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1203#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1204#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1205#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1206#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1207
43c0598f
RS
1208/* Sign-extend 16-bit value X. */
1209#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1210
885add95
CD
1211/* Is the given value a sign-extended 32-bit value? */
1212#define IS_SEXT_32BIT_NUM(x) \
1213 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1214 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1215
1216/* Is the given value a sign-extended 16-bit value? */
1217#define IS_SEXT_16BIT_NUM(x) \
1218 (((x) &~ (offsetT) 0x7fff) == 0 \
1219 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1220
df58fc94
RS
1221/* Is the given value a sign-extended 12-bit value? */
1222#define IS_SEXT_12BIT_NUM(x) \
1223 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1224
7f3c4072
CM
1225/* Is the given value a sign-extended 9-bit value? */
1226#define IS_SEXT_9BIT_NUM(x) \
1227 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1228
2051e8c4
MR
1229/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1230#define IS_ZEXT_32BIT_NUM(x) \
1231 (((x) &~ (offsetT) 0xffffffff) == 0 \
1232 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1233
bf12938e
RS
1234/* Extract bits MASK << SHIFT from STRUCT and shift them right
1235 SHIFT places. */
1236#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1237 (((STRUCT) >> (SHIFT)) & (MASK))
1238
bf12938e 1239/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1240#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1241 (!(MICROMIPS) \
1242 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1243 : EXTRACT_BITS ((INSN).insn_opcode, \
1244 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1245#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1246 EXTRACT_BITS ((INSN).insn_opcode, \
1247 MIPS16OP_MASK_##FIELD, \
1248 MIPS16OP_SH_##FIELD)
5c04167a
RS
1249
1250/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1251#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1252\f
df58fc94
RS
1253/* Whether or not we are emitting a branch-likely macro. */
1254static bfd_boolean emit_branch_likely_macro = FALSE;
1255
4d7206a2
RS
1256/* Global variables used when generating relaxable macros. See the
1257 comment above RELAX_ENCODE for more details about how relaxation
1258 is used. */
1259static struct {
1260 /* 0 if we're not emitting a relaxable macro.
1261 1 if we're emitting the first of the two relaxation alternatives.
1262 2 if we're emitting the second alternative. */
1263 int sequence;
1264
1265 /* The first relaxable fixup in the current frag. (In other words,
1266 the first fixup that refers to relaxable code.) */
1267 fixS *first_fixup;
1268
1269 /* sizes[0] says how many bytes of the first alternative are stored in
1270 the current frag. Likewise sizes[1] for the second alternative. */
1271 unsigned int sizes[2];
1272
1273 /* The symbol on which the choice of sequence depends. */
1274 symbolS *symbol;
1275} mips_relax;
252b5132 1276\f
584892a6
RS
1277/* Global variables used to decide whether a macro needs a warning. */
1278static struct {
1279 /* True if the macro is in a branch delay slot. */
1280 bfd_boolean delay_slot_p;
1281
df58fc94
RS
1282 /* Set to the length in bytes required if the macro is in a delay slot
1283 that requires a specific length of instruction, otherwise zero. */
1284 unsigned int delay_slot_length;
1285
584892a6
RS
1286 /* For relaxable macros, sizes[0] is the length of the first alternative
1287 in bytes and sizes[1] is the length of the second alternative.
1288 For non-relaxable macros, both elements give the length of the
1289 macro in bytes. */
1290 unsigned int sizes[2];
1291
df58fc94
RS
1292 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1293 instruction of the first alternative in bytes and first_insn_sizes[1]
1294 is the length of the first instruction of the second alternative.
1295 For non-relaxable macros, both elements give the length of the first
1296 instruction in bytes.
1297
1298 Set to zero if we haven't yet seen the first instruction. */
1299 unsigned int first_insn_sizes[2];
1300
1301 /* For relaxable macros, insns[0] is the number of instructions for the
1302 first alternative and insns[1] is the number of instructions for the
1303 second alternative.
1304
1305 For non-relaxable macros, both elements give the number of
1306 instructions for the macro. */
1307 unsigned int insns[2];
1308
584892a6
RS
1309 /* The first variant frag for this macro. */
1310 fragS *first_frag;
1311} mips_macro_warning;
1312\f
252b5132
RH
1313/* Prototypes for static functions. */
1314
252b5132
RH
1315enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1316
b34976b6 1317static void append_insn
df58fc94
RS
1318 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1319 bfd_boolean expansionp);
7d10b47d 1320static void mips_no_prev_insn (void);
c67a084a 1321static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1322static void mips16_macro_build
03ea81db 1323 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1324static void load_register (int, expressionS *, int);
584892a6
RS
1325static void macro_start (void);
1326static void macro_end (void);
833794fc 1327static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1328static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1329static void mips_ip (char *str, struct mips_cl_insn * ip);
1330static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1331static void mips16_immed
3b4dbbbf 1332 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1333 unsigned int, unsigned long *);
5e0116d5 1334static size_t my_getSmallExpression
17a2f251
TS
1335 (expressionS *, bfd_reloc_code_real_type *, char *);
1336static void my_getExpression (expressionS *, char *);
1337static void s_align (int);
1338static void s_change_sec (int);
1339static void s_change_section (int);
1340static void s_cons (int);
1341static void s_float_cons (int);
1342static void s_mips_globl (int);
1343static void s_option (int);
1344static void s_mipsset (int);
1345static void s_abicalls (int);
1346static void s_cpload (int);
1347static void s_cpsetup (int);
1348static void s_cplocal (int);
1349static void s_cprestore (int);
1350static void s_cpreturn (int);
741d6ea8
JM
1351static void s_dtprelword (int);
1352static void s_dtpreldword (int);
d0f13682
CLT
1353static void s_tprelword (int);
1354static void s_tpreldword (int);
17a2f251
TS
1355static void s_gpvalue (int);
1356static void s_gpword (int);
1357static void s_gpdword (int);
a3f278e2 1358static void s_ehword (int);
17a2f251
TS
1359static void s_cpadd (int);
1360static void s_insn (int);
ba92f887 1361static void s_nan (int);
919731af 1362static void s_module (int);
17a2f251
TS
1363static void s_mips_ent (int);
1364static void s_mips_end (int);
1365static void s_mips_frame (int);
1366static void s_mips_mask (int reg_type);
1367static void s_mips_stab (int);
1368static void s_mips_weakext (int);
1369static void s_mips_file (int);
1370static void s_mips_loc (int);
9e009953 1371static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1372static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1373static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1374static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1375static void file_mips_check_options (void);
e7af610e
NC
1376
1377/* Table and functions used to map between CPU/ISA names, and
1378 ISA levels, and CPU numbers. */
1379
e972090a
NC
1380struct mips_cpu_info
1381{
e7af610e 1382 const char *name; /* CPU or ISA name. */
d16afab6
RS
1383 int flags; /* MIPS_CPU_* flags. */
1384 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1385 int isa; /* ISA level. */
1386 int cpu; /* CPU number (default CPU if ISA). */
1387};
1388
ad3fea08 1389#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1390
17a2f251
TS
1391static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1392static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1393static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1394\f
c31f3936
RS
1395/* Command-line options. */
1396const char *md_shortopts = "O::g::G:";
1397
1398enum options
1399 {
1400 OPTION_MARCH = OPTION_MD_BASE,
1401 OPTION_MTUNE,
1402 OPTION_MIPS1,
1403 OPTION_MIPS2,
1404 OPTION_MIPS3,
1405 OPTION_MIPS4,
1406 OPTION_MIPS5,
1407 OPTION_MIPS32,
1408 OPTION_MIPS64,
1409 OPTION_MIPS32R2,
ae52f483
AB
1410 OPTION_MIPS32R3,
1411 OPTION_MIPS32R5,
7361da2c 1412 OPTION_MIPS32R6,
c31f3936 1413 OPTION_MIPS64R2,
ae52f483
AB
1414 OPTION_MIPS64R3,
1415 OPTION_MIPS64R5,
7361da2c 1416 OPTION_MIPS64R6,
c31f3936
RS
1417 OPTION_MIPS16,
1418 OPTION_NO_MIPS16,
1419 OPTION_MIPS3D,
1420 OPTION_NO_MIPS3D,
1421 OPTION_MDMX,
1422 OPTION_NO_MDMX,
1423 OPTION_DSP,
1424 OPTION_NO_DSP,
1425 OPTION_MT,
1426 OPTION_NO_MT,
1427 OPTION_VIRT,
1428 OPTION_NO_VIRT,
56d438b1
CF
1429 OPTION_MSA,
1430 OPTION_NO_MSA,
c31f3936
RS
1431 OPTION_SMARTMIPS,
1432 OPTION_NO_SMARTMIPS,
1433 OPTION_DSPR2,
1434 OPTION_NO_DSPR2,
8f4f9071
MF
1435 OPTION_DSPR3,
1436 OPTION_NO_DSPR3,
c31f3936
RS
1437 OPTION_EVA,
1438 OPTION_NO_EVA,
7d64c587
AB
1439 OPTION_XPA,
1440 OPTION_NO_XPA,
c31f3936
RS
1441 OPTION_MICROMIPS,
1442 OPTION_NO_MICROMIPS,
1443 OPTION_MCU,
1444 OPTION_NO_MCU,
1445 OPTION_COMPAT_ARCH_BASE,
1446 OPTION_M4650,
1447 OPTION_NO_M4650,
1448 OPTION_M4010,
1449 OPTION_NO_M4010,
1450 OPTION_M4100,
1451 OPTION_NO_M4100,
1452 OPTION_M3900,
1453 OPTION_NO_M3900,
1454 OPTION_M7000_HILO_FIX,
1455 OPTION_MNO_7000_HILO_FIX,
1456 OPTION_FIX_24K,
1457 OPTION_NO_FIX_24K,
a8d14a88
CM
1458 OPTION_FIX_RM7000,
1459 OPTION_NO_FIX_RM7000,
c31f3936
RS
1460 OPTION_FIX_LOONGSON2F_JUMP,
1461 OPTION_NO_FIX_LOONGSON2F_JUMP,
1462 OPTION_FIX_LOONGSON2F_NOP,
1463 OPTION_NO_FIX_LOONGSON2F_NOP,
1464 OPTION_FIX_VR4120,
1465 OPTION_NO_FIX_VR4120,
1466 OPTION_FIX_VR4130,
1467 OPTION_NO_FIX_VR4130,
1468 OPTION_FIX_CN63XXP1,
1469 OPTION_NO_FIX_CN63XXP1,
1470 OPTION_TRAP,
1471 OPTION_BREAK,
1472 OPTION_EB,
1473 OPTION_EL,
1474 OPTION_FP32,
1475 OPTION_GP32,
1476 OPTION_CONSTRUCT_FLOATS,
1477 OPTION_NO_CONSTRUCT_FLOATS,
1478 OPTION_FP64,
351cdf24 1479 OPTION_FPXX,
c31f3936
RS
1480 OPTION_GP64,
1481 OPTION_RELAX_BRANCH,
1482 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1483 OPTION_IGNORE_BRANCH_ISA,
1484 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1485 OPTION_INSN32,
1486 OPTION_NO_INSN32,
c31f3936
RS
1487 OPTION_MSHARED,
1488 OPTION_MNO_SHARED,
1489 OPTION_MSYM32,
1490 OPTION_MNO_SYM32,
1491 OPTION_SOFT_FLOAT,
1492 OPTION_HARD_FLOAT,
1493 OPTION_SINGLE_FLOAT,
1494 OPTION_DOUBLE_FLOAT,
1495 OPTION_32,
c31f3936
RS
1496 OPTION_CALL_SHARED,
1497 OPTION_CALL_NONPIC,
1498 OPTION_NON_SHARED,
1499 OPTION_XGOT,
1500 OPTION_MABI,
1501 OPTION_N32,
1502 OPTION_64,
1503 OPTION_MDEBUG,
1504 OPTION_NO_MDEBUG,
1505 OPTION_PDR,
1506 OPTION_NO_PDR,
1507 OPTION_MVXWORKS_PIC,
ba92f887 1508 OPTION_NAN,
351cdf24
MF
1509 OPTION_ODD_SPREG,
1510 OPTION_NO_ODD_SPREG,
c31f3936
RS
1511 OPTION_END_OF_ENUM
1512 };
1513
1514struct option md_longopts[] =
1515{
1516 /* Options which specify architecture. */
1517 {"march", required_argument, NULL, OPTION_MARCH},
1518 {"mtune", required_argument, NULL, OPTION_MTUNE},
1519 {"mips0", no_argument, NULL, OPTION_MIPS1},
1520 {"mips1", no_argument, NULL, OPTION_MIPS1},
1521 {"mips2", no_argument, NULL, OPTION_MIPS2},
1522 {"mips3", no_argument, NULL, OPTION_MIPS3},
1523 {"mips4", no_argument, NULL, OPTION_MIPS4},
1524 {"mips5", no_argument, NULL, OPTION_MIPS5},
1525 {"mips32", no_argument, NULL, OPTION_MIPS32},
1526 {"mips64", no_argument, NULL, OPTION_MIPS64},
1527 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1528 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1529 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1530 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1531 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1532 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1533 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1534 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1535
1536 /* Options which specify Application Specific Extensions (ASEs). */
1537 {"mips16", no_argument, NULL, OPTION_MIPS16},
1538 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1539 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1540 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1541 {"mdmx", no_argument, NULL, OPTION_MDMX},
1542 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1543 {"mdsp", no_argument, NULL, OPTION_DSP},
1544 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1545 {"mmt", no_argument, NULL, OPTION_MT},
1546 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1547 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1548 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1549 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1550 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1551 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1552 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1553 {"meva", no_argument, NULL, OPTION_EVA},
1554 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1555 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1556 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1557 {"mmcu", no_argument, NULL, OPTION_MCU},
1558 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1559 {"mvirt", no_argument, NULL, OPTION_VIRT},
1560 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1561 {"mmsa", no_argument, NULL, OPTION_MSA},
1562 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1563 {"mxpa", no_argument, NULL, OPTION_XPA},
1564 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
c31f3936
RS
1565
1566 /* Old-style architecture options. Don't add more of these. */
1567 {"m4650", no_argument, NULL, OPTION_M4650},
1568 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1569 {"m4010", no_argument, NULL, OPTION_M4010},
1570 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1571 {"m4100", no_argument, NULL, OPTION_M4100},
1572 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1573 {"m3900", no_argument, NULL, OPTION_M3900},
1574 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1575
1576 /* Options which enable bug fixes. */
1577 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1578 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1579 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1580 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1581 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1582 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1583 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1584 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1585 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1586 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1587 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1588 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1589 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1590 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1591 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1592 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1593 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1594
1595 /* Miscellaneous options. */
1596 {"trap", no_argument, NULL, OPTION_TRAP},
1597 {"no-break", no_argument, NULL, OPTION_TRAP},
1598 {"break", no_argument, NULL, OPTION_BREAK},
1599 {"no-trap", no_argument, NULL, OPTION_BREAK},
1600 {"EB", no_argument, NULL, OPTION_EB},
1601 {"EL", no_argument, NULL, OPTION_EL},
1602 {"mfp32", no_argument, NULL, OPTION_FP32},
1603 {"mgp32", no_argument, NULL, OPTION_GP32},
1604 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1605 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1606 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1607 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1608 {"mgp64", no_argument, NULL, OPTION_GP64},
1609 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1610 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1611 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1612 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1613 {"minsn32", no_argument, NULL, OPTION_INSN32},
1614 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1615 {"mshared", no_argument, NULL, OPTION_MSHARED},
1616 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1617 {"msym32", no_argument, NULL, OPTION_MSYM32},
1618 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1619 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1620 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1621 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1622 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1623 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1624 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1625
1626 /* Strictly speaking this next option is ELF specific,
1627 but we allow it for other ports as well in order to
1628 make testing easier. */
1629 {"32", no_argument, NULL, OPTION_32},
1630
1631 /* ELF-specific options. */
c31f3936
RS
1632 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1633 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1634 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1635 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1636 {"xgot", no_argument, NULL, OPTION_XGOT},
1637 {"mabi", required_argument, NULL, OPTION_MABI},
1638 {"n32", no_argument, NULL, OPTION_N32},
1639 {"64", no_argument, NULL, OPTION_64},
1640 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1641 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1642 {"mpdr", no_argument, NULL, OPTION_PDR},
1643 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1644 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1645 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1646
1647 {NULL, no_argument, NULL, 0}
1648};
1649size_t md_longopts_size = sizeof (md_longopts);
1650\f
c6278170
RS
1651/* Information about either an Application Specific Extension or an
1652 optional architecture feature that, for simplicity, we treat in the
1653 same way as an ASE. */
1654struct mips_ase
1655{
1656 /* The name of the ASE, used in both the command-line and .set options. */
1657 const char *name;
1658
1659 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1660 and 64-bit architectures, the flags here refer to the subset that
1661 is available on both. */
1662 unsigned int flags;
1663
1664 /* The ASE_* flag used for instructions that are available on 64-bit
1665 architectures but that are not included in FLAGS. */
1666 unsigned int flags64;
1667
1668 /* The command-line options that turn the ASE on and off. */
1669 int option_on;
1670 int option_off;
1671
1672 /* The minimum required architecture revisions for MIPS32, MIPS64,
1673 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1674 int mips32_rev;
1675 int mips64_rev;
1676 int micromips32_rev;
1677 int micromips64_rev;
7361da2c
AB
1678
1679 /* The architecture where the ASE was removed or -1 if the extension has not
1680 been removed. */
1681 int rem_rev;
c6278170
RS
1682};
1683
1684/* A table of all supported ASEs. */
1685static const struct mips_ase mips_ases[] = {
1686 { "dsp", ASE_DSP, ASE_DSP64,
1687 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1688 2, 2, 2, 2,
1689 -1 },
c6278170
RS
1690
1691 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1692 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1693 2, 2, 2, 2,
1694 -1 },
c6278170 1695
8f4f9071
MF
1696 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1697 OPTION_DSPR3, OPTION_NO_DSPR3,
1698 6, 6, -1, -1,
1699 -1 },
1700
c6278170
RS
1701 { "eva", ASE_EVA, 0,
1702 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1703 2, 2, 2, 2,
1704 -1 },
c6278170
RS
1705
1706 { "mcu", ASE_MCU, 0,
1707 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1708 2, 2, 2, 2,
1709 -1 },
c6278170
RS
1710
1711 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1712 { "mdmx", ASE_MDMX, 0,
1713 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1714 -1, 1, -1, -1,
1715 6 },
c6278170
RS
1716
1717 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1718 { "mips3d", ASE_MIPS3D, 0,
1719 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1720 2, 1, -1, -1,
1721 6 },
c6278170
RS
1722
1723 { "mt", ASE_MT, 0,
1724 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1725 2, 2, -1, -1,
1726 -1 },
c6278170
RS
1727
1728 { "smartmips", ASE_SMARTMIPS, 0,
1729 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1730 1, -1, -1, -1,
1731 6 },
c6278170
RS
1732
1733 { "virt", ASE_VIRT, ASE_VIRT64,
1734 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1735 2, 2, 2, 2,
1736 -1 },
56d438b1
CF
1737
1738 { "msa", ASE_MSA, ASE_MSA64,
1739 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1740 2, 2, 2, 2,
1741 -1 },
7d64c587
AB
1742
1743 { "xpa", ASE_XPA, 0,
1744 OPTION_XPA, OPTION_NO_XPA,
7361da2c
AB
1745 2, 2, -1, -1,
1746 -1 },
c6278170
RS
1747};
1748
1749/* The set of ASEs that require -mfp64. */
82bda27b 1750#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1751
1752/* Groups of ASE_* flags that represent different revisions of an ASE. */
1753static const unsigned int mips_ase_groups[] = {
8f4f9071 1754 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1755};
1756\f
252b5132
RH
1757/* Pseudo-op table.
1758
1759 The following pseudo-ops from the Kane and Heinrich MIPS book
1760 should be defined here, but are currently unsupported: .alias,
1761 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1762
1763 The following pseudo-ops from the Kane and Heinrich MIPS book are
1764 specific to the type of debugging information being generated, and
1765 should be defined by the object format: .aent, .begin, .bend,
1766 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1767 .vreg.
1768
1769 The following pseudo-ops from the Kane and Heinrich MIPS book are
1770 not MIPS CPU specific, but are also not specific to the object file
1771 format. This file is probably the best place to define them, but
d84bcf09 1772 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1773
e972090a
NC
1774static const pseudo_typeS mips_pseudo_table[] =
1775{
beae10d5 1776 /* MIPS specific pseudo-ops. */
252b5132
RH
1777 {"option", s_option, 0},
1778 {"set", s_mipsset, 0},
1779 {"rdata", s_change_sec, 'r'},
1780 {"sdata", s_change_sec, 's'},
1781 {"livereg", s_ignore, 0},
1782 {"abicalls", s_abicalls, 0},
1783 {"cpload", s_cpload, 0},
6478892d
TS
1784 {"cpsetup", s_cpsetup, 0},
1785 {"cplocal", s_cplocal, 0},
252b5132 1786 {"cprestore", s_cprestore, 0},
6478892d 1787 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1788 {"dtprelword", s_dtprelword, 0},
1789 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1790 {"tprelword", s_tprelword, 0},
1791 {"tpreldword", s_tpreldword, 0},
6478892d 1792 {"gpvalue", s_gpvalue, 0},
252b5132 1793 {"gpword", s_gpword, 0},
10181a0d 1794 {"gpdword", s_gpdword, 0},
a3f278e2 1795 {"ehword", s_ehword, 0},
252b5132
RH
1796 {"cpadd", s_cpadd, 0},
1797 {"insn", s_insn, 0},
ba92f887 1798 {"nan", s_nan, 0},
919731af 1799 {"module", s_module, 0},
252b5132 1800
beae10d5 1801 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1802 chips. */
38a57ae7 1803 {"asciiz", stringer, 8 + 1},
252b5132
RH
1804 {"bss", s_change_sec, 'b'},
1805 {"err", s_err, 0},
1806 {"half", s_cons, 1},
1807 {"dword", s_cons, 3},
1808 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1809 {"origin", s_org, 0},
1810 {"repeat", s_rept, 0},
252b5132 1811
998b3c36
MR
1812 /* For MIPS this is non-standard, but we define it for consistency. */
1813 {"sbss", s_change_sec, 'B'},
1814
beae10d5 1815 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1816 here for one reason or another. */
1817 {"align", s_align, 0},
1818 {"byte", s_cons, 0},
1819 {"data", s_change_sec, 'd'},
1820 {"double", s_float_cons, 'd'},
1821 {"float", s_float_cons, 'f'},
1822 {"globl", s_mips_globl, 0},
1823 {"global", s_mips_globl, 0},
1824 {"hword", s_cons, 1},
1825 {"int", s_cons, 2},
1826 {"long", s_cons, 2},
1827 {"octa", s_cons, 4},
1828 {"quad", s_cons, 3},
cca86cc8 1829 {"section", s_change_section, 0},
252b5132
RH
1830 {"short", s_cons, 1},
1831 {"single", s_float_cons, 'f'},
754e2bb9 1832 {"stabd", s_mips_stab, 'd'},
252b5132 1833 {"stabn", s_mips_stab, 'n'},
754e2bb9 1834 {"stabs", s_mips_stab, 's'},
252b5132
RH
1835 {"text", s_change_sec, 't'},
1836 {"word", s_cons, 2},
add56521 1837
add56521 1838 { "extern", ecoff_directive_extern, 0},
add56521 1839
43841e91 1840 { NULL, NULL, 0 },
252b5132
RH
1841};
1842
e972090a
NC
1843static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1844{
beae10d5
KH
1845 /* These pseudo-ops should be defined by the object file format.
1846 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1847 {"aent", s_mips_ent, 1},
1848 {"bgnb", s_ignore, 0},
1849 {"end", s_mips_end, 0},
1850 {"endb", s_ignore, 0},
1851 {"ent", s_mips_ent, 0},
c5dd6aab 1852 {"file", s_mips_file, 0},
252b5132
RH
1853 {"fmask", s_mips_mask, 'F'},
1854 {"frame", s_mips_frame, 0},
c5dd6aab 1855 {"loc", s_mips_loc, 0},
252b5132
RH
1856 {"mask", s_mips_mask, 'R'},
1857 {"verstamp", s_ignore, 0},
43841e91 1858 { NULL, NULL, 0 },
252b5132
RH
1859};
1860
3ae8dd8d
MR
1861/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1862 purpose of the `.dc.a' internal pseudo-op. */
1863
1864int
1865mips_address_bytes (void)
1866{
919731af 1867 file_mips_check_options ();
3ae8dd8d
MR
1868 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1869}
1870
17a2f251 1871extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1872
1873void
17a2f251 1874mips_pop_insert (void)
252b5132
RH
1875{
1876 pop_insert (mips_pseudo_table);
1877 if (! ECOFF_DEBUGGING)
1878 pop_insert (mips_nonecoff_pseudo_table);
1879}
1880\f
1881/* Symbols labelling the current insn. */
1882
e972090a
NC
1883struct insn_label_list
1884{
252b5132
RH
1885 struct insn_label_list *next;
1886 symbolS *label;
1887};
1888
252b5132 1889static struct insn_label_list *free_insn_labels;
742a56fe 1890#define label_list tc_segment_info_data.labels
252b5132 1891
17a2f251 1892static void mips_clear_insn_labels (void);
df58fc94
RS
1893static void mips_mark_labels (void);
1894static void mips_compressed_mark_labels (void);
252b5132
RH
1895
1896static inline void
17a2f251 1897mips_clear_insn_labels (void)
252b5132 1898{
ed9e98c2 1899 struct insn_label_list **pl;
a8dbcb85 1900 segment_info_type *si;
252b5132 1901
a8dbcb85
TS
1902 if (now_seg)
1903 {
1904 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1905 ;
3739860c 1906
a8dbcb85
TS
1907 si = seg_info (now_seg);
1908 *pl = si->label_list;
1909 si->label_list = NULL;
1910 }
252b5132 1911}
a8dbcb85 1912
df58fc94
RS
1913/* Mark instruction labels in MIPS16/microMIPS mode. */
1914
1915static inline void
1916mips_mark_labels (void)
1917{
1918 if (HAVE_CODE_COMPRESSION)
1919 mips_compressed_mark_labels ();
1920}
252b5132
RH
1921\f
1922static char *expr_end;
1923
e423441d 1924/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1925 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1926
1927static expressionS imm_expr;
252b5132 1928
77bd4346
RS
1929/* The relocatable field in an instruction and the relocs associated
1930 with it. These variables are used for instructions like LUI and
1931 JAL as well as true offsets. They are also used for address
1932 operands in macros. */
252b5132 1933
77bd4346 1934static expressionS offset_expr;
f6688943
TS
1935static bfd_reloc_code_real_type offset_reloc[3]
1936 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1937
df58fc94
RS
1938/* This is set to the resulting size of the instruction to be produced
1939 by mips16_ip if an explicit extension is used or by mips_ip if an
1940 explicit size is supplied. */
252b5132 1941
df58fc94 1942static unsigned int forced_insn_length;
252b5132 1943
e1b47bd5
RS
1944/* True if we are assembling an instruction. All dot symbols defined during
1945 this time should be treated as code labels. */
1946
1947static bfd_boolean mips_assembling_insn;
1948
ecb4347a
DJ
1949/* The pdr segment for per procedure frame/regmask info. Not used for
1950 ECOFF debugging. */
252b5132
RH
1951
1952static segT pdr_seg;
252b5132 1953
e013f690
TS
1954/* The default target format to use. */
1955
aeffff67
RS
1956#if defined (TE_FreeBSD)
1957#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1958#elif defined (TE_TMIPS)
1959#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1960#else
1961#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1962#endif
1963
e013f690 1964const char *
17a2f251 1965mips_target_format (void)
e013f690
TS
1966{
1967 switch (OUTPUT_FLAVOR)
1968 {
e013f690 1969 case bfd_target_elf_flavour:
0a44bf69
RS
1970#ifdef TE_VXWORKS
1971 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1972 return (target_big_endian
1973 ? "elf32-bigmips-vxworks"
1974 : "elf32-littlemips-vxworks");
1975#endif
e013f690 1976 return (target_big_endian
cfe86eaa 1977 ? (HAVE_64BIT_OBJECTS
aeffff67 1978 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1979 : (HAVE_NEWABI
aeffff67
RS
1980 ? ELF_TARGET ("elf32-n", "big")
1981 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1982 : (HAVE_64BIT_OBJECTS
aeffff67 1983 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1984 : (HAVE_NEWABI
aeffff67
RS
1985 ? ELF_TARGET ("elf32-n", "little")
1986 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1987 default:
1988 abort ();
1989 return NULL;
1990 }
1991}
1992
c6278170
RS
1993/* Return the ISA revision that is currently in use, or 0 if we are
1994 generating code for MIPS V or below. */
1995
1996static int
1997mips_isa_rev (void)
1998{
1999 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2000 return 2;
2001
ae52f483
AB
2002 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2003 return 3;
2004
2005 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2006 return 5;
2007
7361da2c
AB
2008 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2009 return 6;
2010
c6278170
RS
2011 /* microMIPS implies revision 2 or above. */
2012 if (mips_opts.micromips)
2013 return 2;
2014
2015 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2016 return 1;
2017
2018 return 0;
2019}
2020
2021/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2022
2023static unsigned int
2024mips_ase_mask (unsigned int flags)
2025{
2026 unsigned int i;
2027
2028 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2029 if (flags & mips_ase_groups[i])
2030 flags |= mips_ase_groups[i];
2031 return flags;
2032}
2033
2034/* Check whether the current ISA supports ASE. Issue a warning if
2035 appropriate. */
2036
2037static void
2038mips_check_isa_supports_ase (const struct mips_ase *ase)
2039{
2040 const char *base;
2041 int min_rev, size;
2042 static unsigned int warned_isa;
2043 static unsigned int warned_fp32;
2044
2045 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2046 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2047 else
2048 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2049 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2050 && (warned_isa & ase->flags) != ase->flags)
2051 {
2052 warned_isa |= ase->flags;
2053 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2054 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2055 if (min_rev < 0)
1661c76c 2056 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2057 " `%s' extension"), size, base, ase->name);
2058 else
1661c76c 2059 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2060 ase->name, base, size, min_rev);
2061 }
7361da2c
AB
2062 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2063 && (warned_isa & ase->flags) != ase->flags)
2064 {
2065 warned_isa |= ase->flags;
2066 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2067 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2068 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2069 ase->name, base, size, ase->rem_rev);
2070 }
2071
c6278170 2072 if ((ase->flags & FP64_ASES)
0b35dfee 2073 && mips_opts.fp != 64
c6278170
RS
2074 && (warned_fp32 & ase->flags) != ase->flags)
2075 {
2076 warned_fp32 |= ase->flags;
1661c76c 2077 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2078 }
2079}
2080
2081/* Check all enabled ASEs to see whether they are supported by the
2082 chosen architecture. */
2083
2084static void
2085mips_check_isa_supports_ases (void)
2086{
2087 unsigned int i, mask;
2088
2089 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2090 {
2091 mask = mips_ase_mask (mips_ases[i].flags);
2092 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2093 mips_check_isa_supports_ase (&mips_ases[i]);
2094 }
2095}
2096
2097/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2098 that were affected. */
2099
2100static unsigned int
919731af 2101mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2102 bfd_boolean enabled_p)
c6278170
RS
2103{
2104 unsigned int mask;
2105
2106 mask = mips_ase_mask (ase->flags);
919731af 2107 opts->ase &= ~mask;
c6278170 2108 if (enabled_p)
919731af 2109 opts->ase |= ase->flags;
c6278170
RS
2110 return mask;
2111}
2112
2113/* Return the ASE called NAME, or null if none. */
2114
2115static const struct mips_ase *
2116mips_lookup_ase (const char *name)
2117{
2118 unsigned int i;
2119
2120 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2121 if (strcmp (name, mips_ases[i].name) == 0)
2122 return &mips_ases[i];
2123 return NULL;
2124}
2125
df58fc94 2126/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2127 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2128 otherwise it is a 32-bit instruction. */
df58fc94
RS
2129
2130static inline unsigned int
2131micromips_insn_length (const struct mips_opcode *mo)
2132{
7fd53920 2133 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2134}
2135
5c04167a
RS
2136/* Return the length of MIPS16 instruction OPCODE. */
2137
2138static inline unsigned int
2139mips16_opcode_length (unsigned long opcode)
2140{
2141 return (opcode >> 16) == 0 ? 2 : 4;
2142}
2143
1e915849
RS
2144/* Return the length of instruction INSN. */
2145
2146static inline unsigned int
2147insn_length (const struct mips_cl_insn *insn)
2148{
df58fc94
RS
2149 if (mips_opts.micromips)
2150 return micromips_insn_length (insn->insn_mo);
2151 else if (mips_opts.mips16)
5c04167a 2152 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2153 else
1e915849 2154 return 4;
1e915849
RS
2155}
2156
2157/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2158
2159static void
2160create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2161{
2162 size_t i;
2163
2164 insn->insn_mo = mo;
1e915849
RS
2165 insn->insn_opcode = mo->match;
2166 insn->frag = NULL;
2167 insn->where = 0;
2168 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2169 insn->fixp[i] = NULL;
2170 insn->fixed_p = (mips_opts.noreorder > 0);
2171 insn->noreorder_p = (mips_opts.noreorder > 0);
2172 insn->mips16_absolute_jump_p = 0;
15be625d 2173 insn->complete_p = 0;
e407c74b 2174 insn->cleared_p = 0;
1e915849
RS
2175}
2176
fc76e730
RS
2177/* Get a list of all the operands in INSN. */
2178
2179static const struct mips_operand_array *
2180insn_operands (const struct mips_cl_insn *insn)
2181{
2182 if (insn->insn_mo >= &mips_opcodes[0]
2183 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2184 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2185
2186 if (insn->insn_mo >= &mips16_opcodes[0]
2187 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2188 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2189
2190 if (insn->insn_mo >= &micromips_opcodes[0]
2191 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2192 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2193
2194 abort ();
2195}
2196
2197/* Get a description of operand OPNO of INSN. */
2198
2199static const struct mips_operand *
2200insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2201{
2202 const struct mips_operand_array *operands;
2203
2204 operands = insn_operands (insn);
2205 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2206 abort ();
2207 return operands->operand[opno];
2208}
2209
e077a1c8
RS
2210/* Install UVAL as the value of OPERAND in INSN. */
2211
2212static inline void
2213insn_insert_operand (struct mips_cl_insn *insn,
2214 const struct mips_operand *operand, unsigned int uval)
2215{
2216 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2217}
2218
fc76e730
RS
2219/* Extract the value of OPERAND from INSN. */
2220
2221static inline unsigned
2222insn_extract_operand (const struct mips_cl_insn *insn,
2223 const struct mips_operand *operand)
2224{
2225 return mips_extract_operand (operand, insn->insn_opcode);
2226}
2227
df58fc94 2228/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2229
2230static void
df58fc94 2231mips_record_compressed_mode (void)
742a56fe
RS
2232{
2233 segment_info_type *si;
2234
2235 si = seg_info (now_seg);
2236 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2237 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2238 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2239 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2240}
2241
4d68580a
RS
2242/* Read a standard MIPS instruction from BUF. */
2243
2244static unsigned long
2245read_insn (char *buf)
2246{
2247 if (target_big_endian)
2248 return bfd_getb32 ((bfd_byte *) buf);
2249 else
2250 return bfd_getl32 ((bfd_byte *) buf);
2251}
2252
2253/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2254 the next byte. */
2255
2256static char *
2257write_insn (char *buf, unsigned int insn)
2258{
2259 md_number_to_chars (buf, insn, 4);
2260 return buf + 4;
2261}
2262
2263/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2264 has length LENGTH. */
2265
2266static unsigned long
2267read_compressed_insn (char *buf, unsigned int length)
2268{
2269 unsigned long insn;
2270 unsigned int i;
2271
2272 insn = 0;
2273 for (i = 0; i < length; i += 2)
2274 {
2275 insn <<= 16;
2276 if (target_big_endian)
2277 insn |= bfd_getb16 ((char *) buf);
2278 else
2279 insn |= bfd_getl16 ((char *) buf);
2280 buf += 2;
2281 }
2282 return insn;
2283}
2284
5c04167a
RS
2285/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2286 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2287
2288static char *
2289write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2290{
2291 unsigned int i;
2292
2293 for (i = 0; i < length; i += 2)
2294 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2295 return buf + length;
2296}
2297
1e915849
RS
2298/* Install INSN at the location specified by its "frag" and "where" fields. */
2299
2300static void
2301install_insn (const struct mips_cl_insn *insn)
2302{
2303 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2304 if (HAVE_CODE_COMPRESSION)
2305 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2306 else
4d68580a 2307 write_insn (f, insn->insn_opcode);
df58fc94 2308 mips_record_compressed_mode ();
1e915849
RS
2309}
2310
2311/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2312 and install the opcode in the new location. */
2313
2314static void
2315move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2316{
2317 size_t i;
2318
2319 insn->frag = frag;
2320 insn->where = where;
2321 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2322 if (insn->fixp[i] != NULL)
2323 {
2324 insn->fixp[i]->fx_frag = frag;
2325 insn->fixp[i]->fx_where = where;
2326 }
2327 install_insn (insn);
2328}
2329
2330/* Add INSN to the end of the output. */
2331
2332static void
2333add_fixed_insn (struct mips_cl_insn *insn)
2334{
2335 char *f = frag_more (insn_length (insn));
2336 move_insn (insn, frag_now, f - frag_now->fr_literal);
2337}
2338
2339/* Start a variant frag and move INSN to the start of the variant part,
2340 marking it as fixed. The other arguments are as for frag_var. */
2341
2342static void
2343add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2344 relax_substateT subtype, symbolS *symbol, offsetT offset)
2345{
2346 frag_grow (max_chars);
2347 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2348 insn->fixed_p = 1;
2349 frag_var (rs_machine_dependent, max_chars, var,
2350 subtype, symbol, offset, NULL);
2351}
2352
2353/* Insert N copies of INSN into the history buffer, starting at
2354 position FIRST. Neither FIRST nor N need to be clipped. */
2355
2356static void
2357insert_into_history (unsigned int first, unsigned int n,
2358 const struct mips_cl_insn *insn)
2359{
2360 if (mips_relax.sequence != 2)
2361 {
2362 unsigned int i;
2363
2364 for (i = ARRAY_SIZE (history); i-- > first;)
2365 if (i >= first + n)
2366 history[i] = history[i - n];
2367 else
2368 history[i] = *insn;
2369 }
2370}
2371
e3de51ce
RS
2372/* Clear the error in insn_error. */
2373
2374static void
2375clear_insn_error (void)
2376{
2377 memset (&insn_error, 0, sizeof (insn_error));
2378}
2379
2380/* Possibly record error message MSG for the current instruction.
2381 If the error is about a particular argument, ARGNUM is the 1-based
2382 number of that argument, otherwise it is 0. FORMAT is the format
2383 of MSG. Return true if MSG was used, false if the current message
2384 was kept. */
2385
2386static bfd_boolean
2387set_insn_error_format (int argnum, enum mips_insn_error_format format,
2388 const char *msg)
2389{
2390 if (argnum == 0)
2391 {
2392 /* Give priority to errors against specific arguments, and to
2393 the first whole-instruction message. */
2394 if (insn_error.msg)
2395 return FALSE;
2396 }
2397 else
2398 {
2399 /* Keep insn_error if it is against a later argument. */
2400 if (argnum < insn_error.min_argnum)
2401 return FALSE;
2402
2403 /* If both errors are against the same argument but are different,
2404 give up on reporting a specific error for this argument.
2405 See the comment about mips_insn_error for details. */
2406 if (argnum == insn_error.min_argnum
2407 && insn_error.msg
2408 && strcmp (insn_error.msg, msg) != 0)
2409 {
2410 insn_error.msg = 0;
2411 insn_error.min_argnum += 1;
2412 return FALSE;
2413 }
2414 }
2415 insn_error.min_argnum = argnum;
2416 insn_error.format = format;
2417 insn_error.msg = msg;
2418 return TRUE;
2419}
2420
2421/* Record an instruction error with no % format fields. ARGNUM and MSG are
2422 as for set_insn_error_format. */
2423
2424static void
2425set_insn_error (int argnum, const char *msg)
2426{
2427 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2428}
2429
2430/* Record an instruction error with one %d field I. ARGNUM and MSG are
2431 as for set_insn_error_format. */
2432
2433static void
2434set_insn_error_i (int argnum, const char *msg, int i)
2435{
2436 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2437 insn_error.u.i = i;
2438}
2439
2440/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2441 are as for set_insn_error_format. */
2442
2443static void
2444set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2445{
2446 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2447 {
2448 insn_error.u.ss[0] = s1;
2449 insn_error.u.ss[1] = s2;
2450 }
2451}
2452
2453/* Report the error in insn_error, which is against assembly code STR. */
2454
2455static void
2456report_insn_error (const char *str)
2457{
e1fa0163 2458 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2459
e3de51ce
RS
2460 switch (insn_error.format)
2461 {
2462 case ERR_FMT_PLAIN:
2463 as_bad (msg, str);
2464 break;
2465
2466 case ERR_FMT_I:
2467 as_bad (msg, insn_error.u.i, str);
2468 break;
2469
2470 case ERR_FMT_SS:
2471 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2472 break;
2473 }
e1fa0163
NC
2474
2475 free ((char *) msg);
e3de51ce
RS
2476}
2477
71400594
RS
2478/* Initialize vr4120_conflicts. There is a bit of duplication here:
2479 the idea is to make it obvious at a glance that each errata is
2480 included. */
2481
2482static void
2483init_vr4120_conflicts (void)
2484{
2485#define CONFLICT(FIRST, SECOND) \
2486 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2487
2488 /* Errata 21 - [D]DIV[U] after [D]MACC */
2489 CONFLICT (MACC, DIV);
2490 CONFLICT (DMACC, DIV);
2491
2492 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2493 CONFLICT (DMULT, DMULT);
2494 CONFLICT (DMULT, DMACC);
2495 CONFLICT (DMACC, DMULT);
2496 CONFLICT (DMACC, DMACC);
2497
2498 /* Errata 24 - MT{LO,HI} after [D]MACC */
2499 CONFLICT (MACC, MTHILO);
2500 CONFLICT (DMACC, MTHILO);
2501
2502 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2503 instruction is executed immediately after a MACC or DMACC
2504 instruction, the result of [either instruction] is incorrect." */
2505 CONFLICT (MACC, MULT);
2506 CONFLICT (MACC, DMULT);
2507 CONFLICT (DMACC, MULT);
2508 CONFLICT (DMACC, DMULT);
2509
2510 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2511 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2512 DDIV or DDIVU instruction, the result of the MACC or
2513 DMACC instruction is incorrect.". */
2514 CONFLICT (DMULT, MACC);
2515 CONFLICT (DMULT, DMACC);
2516 CONFLICT (DIV, MACC);
2517 CONFLICT (DIV, DMACC);
2518
2519#undef CONFLICT
2520}
2521
707bfff6
TS
2522struct regname {
2523 const char *name;
2524 unsigned int num;
2525};
2526
14daeee3 2527#define RNUM_MASK 0x00000ff
56d438b1 2528#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2529#define RTYPE_NUM 0x0000100
2530#define RTYPE_FPU 0x0000200
2531#define RTYPE_FCC 0x0000400
2532#define RTYPE_VEC 0x0000800
2533#define RTYPE_GP 0x0001000
2534#define RTYPE_CP0 0x0002000
2535#define RTYPE_PC 0x0004000
2536#define RTYPE_ACC 0x0008000
2537#define RTYPE_CCC 0x0010000
2538#define RTYPE_VI 0x0020000
2539#define RTYPE_VF 0x0040000
2540#define RTYPE_R5900_I 0x0080000
2541#define RTYPE_R5900_Q 0x0100000
2542#define RTYPE_R5900_R 0x0200000
2543#define RTYPE_R5900_ACC 0x0400000
56d438b1 2544#define RTYPE_MSA 0x0800000
14daeee3 2545#define RWARN 0x8000000
707bfff6
TS
2546
2547#define GENERIC_REGISTER_NUMBERS \
2548 {"$0", RTYPE_NUM | 0}, \
2549 {"$1", RTYPE_NUM | 1}, \
2550 {"$2", RTYPE_NUM | 2}, \
2551 {"$3", RTYPE_NUM | 3}, \
2552 {"$4", RTYPE_NUM | 4}, \
2553 {"$5", RTYPE_NUM | 5}, \
2554 {"$6", RTYPE_NUM | 6}, \
2555 {"$7", RTYPE_NUM | 7}, \
2556 {"$8", RTYPE_NUM | 8}, \
2557 {"$9", RTYPE_NUM | 9}, \
2558 {"$10", RTYPE_NUM | 10}, \
2559 {"$11", RTYPE_NUM | 11}, \
2560 {"$12", RTYPE_NUM | 12}, \
2561 {"$13", RTYPE_NUM | 13}, \
2562 {"$14", RTYPE_NUM | 14}, \
2563 {"$15", RTYPE_NUM | 15}, \
2564 {"$16", RTYPE_NUM | 16}, \
2565 {"$17", RTYPE_NUM | 17}, \
2566 {"$18", RTYPE_NUM | 18}, \
2567 {"$19", RTYPE_NUM | 19}, \
2568 {"$20", RTYPE_NUM | 20}, \
2569 {"$21", RTYPE_NUM | 21}, \
2570 {"$22", RTYPE_NUM | 22}, \
2571 {"$23", RTYPE_NUM | 23}, \
2572 {"$24", RTYPE_NUM | 24}, \
2573 {"$25", RTYPE_NUM | 25}, \
2574 {"$26", RTYPE_NUM | 26}, \
2575 {"$27", RTYPE_NUM | 27}, \
2576 {"$28", RTYPE_NUM | 28}, \
2577 {"$29", RTYPE_NUM | 29}, \
2578 {"$30", RTYPE_NUM | 30}, \
3739860c 2579 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2580
2581#define FPU_REGISTER_NAMES \
2582 {"$f0", RTYPE_FPU | 0}, \
2583 {"$f1", RTYPE_FPU | 1}, \
2584 {"$f2", RTYPE_FPU | 2}, \
2585 {"$f3", RTYPE_FPU | 3}, \
2586 {"$f4", RTYPE_FPU | 4}, \
2587 {"$f5", RTYPE_FPU | 5}, \
2588 {"$f6", RTYPE_FPU | 6}, \
2589 {"$f7", RTYPE_FPU | 7}, \
2590 {"$f8", RTYPE_FPU | 8}, \
2591 {"$f9", RTYPE_FPU | 9}, \
2592 {"$f10", RTYPE_FPU | 10}, \
2593 {"$f11", RTYPE_FPU | 11}, \
2594 {"$f12", RTYPE_FPU | 12}, \
2595 {"$f13", RTYPE_FPU | 13}, \
2596 {"$f14", RTYPE_FPU | 14}, \
2597 {"$f15", RTYPE_FPU | 15}, \
2598 {"$f16", RTYPE_FPU | 16}, \
2599 {"$f17", RTYPE_FPU | 17}, \
2600 {"$f18", RTYPE_FPU | 18}, \
2601 {"$f19", RTYPE_FPU | 19}, \
2602 {"$f20", RTYPE_FPU | 20}, \
2603 {"$f21", RTYPE_FPU | 21}, \
2604 {"$f22", RTYPE_FPU | 22}, \
2605 {"$f23", RTYPE_FPU | 23}, \
2606 {"$f24", RTYPE_FPU | 24}, \
2607 {"$f25", RTYPE_FPU | 25}, \
2608 {"$f26", RTYPE_FPU | 26}, \
2609 {"$f27", RTYPE_FPU | 27}, \
2610 {"$f28", RTYPE_FPU | 28}, \
2611 {"$f29", RTYPE_FPU | 29}, \
2612 {"$f30", RTYPE_FPU | 30}, \
2613 {"$f31", RTYPE_FPU | 31}
2614
2615#define FPU_CONDITION_CODE_NAMES \
2616 {"$fcc0", RTYPE_FCC | 0}, \
2617 {"$fcc1", RTYPE_FCC | 1}, \
2618 {"$fcc2", RTYPE_FCC | 2}, \
2619 {"$fcc3", RTYPE_FCC | 3}, \
2620 {"$fcc4", RTYPE_FCC | 4}, \
2621 {"$fcc5", RTYPE_FCC | 5}, \
2622 {"$fcc6", RTYPE_FCC | 6}, \
2623 {"$fcc7", RTYPE_FCC | 7}
2624
2625#define COPROC_CONDITION_CODE_NAMES \
2626 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2627 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2628 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2629 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2630 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2631 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2632 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2633 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2634
2635#define N32N64_SYMBOLIC_REGISTER_NAMES \
2636 {"$a4", RTYPE_GP | 8}, \
2637 {"$a5", RTYPE_GP | 9}, \
2638 {"$a6", RTYPE_GP | 10}, \
2639 {"$a7", RTYPE_GP | 11}, \
2640 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2641 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2642 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2643 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2644 {"$t0", RTYPE_GP | 12}, \
2645 {"$t1", RTYPE_GP | 13}, \
2646 {"$t2", RTYPE_GP | 14}, \
2647 {"$t3", RTYPE_GP | 15}
2648
2649#define O32_SYMBOLIC_REGISTER_NAMES \
2650 {"$t0", RTYPE_GP | 8}, \
2651 {"$t1", RTYPE_GP | 9}, \
2652 {"$t2", RTYPE_GP | 10}, \
2653 {"$t3", RTYPE_GP | 11}, \
2654 {"$t4", RTYPE_GP | 12}, \
2655 {"$t5", RTYPE_GP | 13}, \
2656 {"$t6", RTYPE_GP | 14}, \
2657 {"$t7", RTYPE_GP | 15}, \
2658 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2659 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2660 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2661 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2662
2663/* Remaining symbolic register names */
2664#define SYMBOLIC_REGISTER_NAMES \
2665 {"$zero", RTYPE_GP | 0}, \
2666 {"$at", RTYPE_GP | 1}, \
2667 {"$AT", RTYPE_GP | 1}, \
2668 {"$v0", RTYPE_GP | 2}, \
2669 {"$v1", RTYPE_GP | 3}, \
2670 {"$a0", RTYPE_GP | 4}, \
2671 {"$a1", RTYPE_GP | 5}, \
2672 {"$a2", RTYPE_GP | 6}, \
2673 {"$a3", RTYPE_GP | 7}, \
2674 {"$s0", RTYPE_GP | 16}, \
2675 {"$s1", RTYPE_GP | 17}, \
2676 {"$s2", RTYPE_GP | 18}, \
2677 {"$s3", RTYPE_GP | 19}, \
2678 {"$s4", RTYPE_GP | 20}, \
2679 {"$s5", RTYPE_GP | 21}, \
2680 {"$s6", RTYPE_GP | 22}, \
2681 {"$s7", RTYPE_GP | 23}, \
2682 {"$t8", RTYPE_GP | 24}, \
2683 {"$t9", RTYPE_GP | 25}, \
2684 {"$k0", RTYPE_GP | 26}, \
2685 {"$kt0", RTYPE_GP | 26}, \
2686 {"$k1", RTYPE_GP | 27}, \
2687 {"$kt1", RTYPE_GP | 27}, \
2688 {"$gp", RTYPE_GP | 28}, \
2689 {"$sp", RTYPE_GP | 29}, \
2690 {"$s8", RTYPE_GP | 30}, \
2691 {"$fp", RTYPE_GP | 30}, \
2692 {"$ra", RTYPE_GP | 31}
2693
2694#define MIPS16_SPECIAL_REGISTER_NAMES \
2695 {"$pc", RTYPE_PC | 0}
2696
2697#define MDMX_VECTOR_REGISTER_NAMES \
2698 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2699 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2700 {"$v2", RTYPE_VEC | 2}, \
2701 {"$v3", RTYPE_VEC | 3}, \
2702 {"$v4", RTYPE_VEC | 4}, \
2703 {"$v5", RTYPE_VEC | 5}, \
2704 {"$v6", RTYPE_VEC | 6}, \
2705 {"$v7", RTYPE_VEC | 7}, \
2706 {"$v8", RTYPE_VEC | 8}, \
2707 {"$v9", RTYPE_VEC | 9}, \
2708 {"$v10", RTYPE_VEC | 10}, \
2709 {"$v11", RTYPE_VEC | 11}, \
2710 {"$v12", RTYPE_VEC | 12}, \
2711 {"$v13", RTYPE_VEC | 13}, \
2712 {"$v14", RTYPE_VEC | 14}, \
2713 {"$v15", RTYPE_VEC | 15}, \
2714 {"$v16", RTYPE_VEC | 16}, \
2715 {"$v17", RTYPE_VEC | 17}, \
2716 {"$v18", RTYPE_VEC | 18}, \
2717 {"$v19", RTYPE_VEC | 19}, \
2718 {"$v20", RTYPE_VEC | 20}, \
2719 {"$v21", RTYPE_VEC | 21}, \
2720 {"$v22", RTYPE_VEC | 22}, \
2721 {"$v23", RTYPE_VEC | 23}, \
2722 {"$v24", RTYPE_VEC | 24}, \
2723 {"$v25", RTYPE_VEC | 25}, \
2724 {"$v26", RTYPE_VEC | 26}, \
2725 {"$v27", RTYPE_VEC | 27}, \
2726 {"$v28", RTYPE_VEC | 28}, \
2727 {"$v29", RTYPE_VEC | 29}, \
2728 {"$v30", RTYPE_VEC | 30}, \
2729 {"$v31", RTYPE_VEC | 31}
2730
14daeee3
RS
2731#define R5900_I_NAMES \
2732 {"$I", RTYPE_R5900_I | 0}
2733
2734#define R5900_Q_NAMES \
2735 {"$Q", RTYPE_R5900_Q | 0}
2736
2737#define R5900_R_NAMES \
2738 {"$R", RTYPE_R5900_R | 0}
2739
2740#define R5900_ACC_NAMES \
2741 {"$ACC", RTYPE_R5900_ACC | 0 }
2742
707bfff6
TS
2743#define MIPS_DSP_ACCUMULATOR_NAMES \
2744 {"$ac0", RTYPE_ACC | 0}, \
2745 {"$ac1", RTYPE_ACC | 1}, \
2746 {"$ac2", RTYPE_ACC | 2}, \
2747 {"$ac3", RTYPE_ACC | 3}
2748
2749static const struct regname reg_names[] = {
2750 GENERIC_REGISTER_NUMBERS,
2751 FPU_REGISTER_NAMES,
2752 FPU_CONDITION_CODE_NAMES,
2753 COPROC_CONDITION_CODE_NAMES,
2754
2755 /* The $txx registers depends on the abi,
2756 these will be added later into the symbol table from
3739860c 2757 one of the tables below once mips_abi is set after
707bfff6
TS
2758 parsing of arguments from the command line. */
2759 SYMBOLIC_REGISTER_NAMES,
2760
2761 MIPS16_SPECIAL_REGISTER_NAMES,
2762 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2763 R5900_I_NAMES,
2764 R5900_Q_NAMES,
2765 R5900_R_NAMES,
2766 R5900_ACC_NAMES,
707bfff6
TS
2767 MIPS_DSP_ACCUMULATOR_NAMES,
2768 {0, 0}
2769};
2770
2771static const struct regname reg_names_o32[] = {
2772 O32_SYMBOLIC_REGISTER_NAMES,
2773 {0, 0}
2774};
2775
2776static const struct regname reg_names_n32n64[] = {
2777 N32N64_SYMBOLIC_REGISTER_NAMES,
2778 {0, 0}
2779};
2780
a92713e6
RS
2781/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2782 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2783 of these register symbols, return the associated vector register,
2784 otherwise return SYMVAL itself. */
df58fc94 2785
a92713e6
RS
2786static unsigned int
2787mips_prefer_vec_regno (unsigned int symval)
707bfff6 2788{
a92713e6
RS
2789 if ((symval & -2) == (RTYPE_GP | 2))
2790 return RTYPE_VEC | (symval & 1);
2791 return symval;
2792}
2793
14daeee3
RS
2794/* Return true if string [S, E) is a valid register name, storing its
2795 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2796
2797static bfd_boolean
14daeee3 2798mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2799{
707bfff6 2800 char save_c;
14daeee3 2801 symbolS *symbol;
707bfff6
TS
2802
2803 /* Terminate name. */
2804 save_c = *e;
2805 *e = '\0';
2806
a92713e6
RS
2807 /* Look up the name. */
2808 symbol = symbol_find (s);
2809 *e = save_c;
2810
2811 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2812 return FALSE;
2813
14daeee3
RS
2814 *symval_ptr = S_GET_VALUE (symbol);
2815 return TRUE;
2816}
2817
2818/* Return true if the string at *SPTR is a valid register name. Allow it
2819 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2820 is nonnull.
2821
2822 When returning true, move *SPTR past the register, store the
2823 register's symbol value in *SYMVAL_PTR and the channel mask in
2824 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2825 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2826 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2827
2828static bfd_boolean
2829mips_parse_register (char **sptr, unsigned int *symval_ptr,
2830 unsigned int *channels_ptr)
2831{
2832 char *s, *e, *m;
2833 const char *q;
2834 unsigned int channels, symval, bit;
2835
2836 /* Find end of name. */
2837 s = e = *sptr;
2838 if (is_name_beginner (*e))
2839 ++e;
2840 while (is_part_of_name (*e))
2841 ++e;
2842
2843 channels = 0;
2844 if (!mips_parse_register_1 (s, e, &symval))
2845 {
2846 if (!channels_ptr)
2847 return FALSE;
2848
2849 /* Eat characters from the end of the string that are valid
2850 channel suffixes. The preceding register must be $ACC or
2851 end with a digit, so there is no ambiguity. */
2852 bit = 1;
2853 m = e;
2854 for (q = "wzyx"; *q; q++, bit <<= 1)
2855 if (m > s && m[-1] == *q)
2856 {
2857 --m;
2858 channels |= bit;
2859 }
2860
2861 if (channels == 0
2862 || !mips_parse_register_1 (s, m, &symval)
2863 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2864 return FALSE;
2865 }
2866
a92713e6 2867 *sptr = e;
14daeee3
RS
2868 *symval_ptr = symval;
2869 if (channels_ptr)
2870 *channels_ptr = channels;
a92713e6
RS
2871 return TRUE;
2872}
2873
2874/* Check if SPTR points at a valid register specifier according to TYPES.
2875 If so, then return 1, advance S to consume the specifier and store
2876 the register's number in REGNOP, otherwise return 0. */
2877
2878static int
2879reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2880{
2881 unsigned int regno;
2882
14daeee3 2883 if (mips_parse_register (s, &regno, NULL))
707bfff6 2884 {
a92713e6
RS
2885 if (types & RTYPE_VEC)
2886 regno = mips_prefer_vec_regno (regno);
2887 if (regno & types)
2888 regno &= RNUM_MASK;
2889 else
2890 regno = ~0;
707bfff6 2891 }
a92713e6 2892 else
707bfff6 2893 {
a92713e6 2894 if (types & RWARN)
1661c76c 2895 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2896 regno = ~0;
707bfff6 2897 }
707bfff6 2898 if (regnop)
a92713e6
RS
2899 *regnop = regno;
2900 return regno <= RNUM_MASK;
707bfff6
TS
2901}
2902
14daeee3
RS
2903/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2904 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2905
2906static char *
2907mips_parse_vu0_channels (char *s, unsigned int *channels)
2908{
2909 unsigned int i;
2910
2911 *channels = 0;
2912 for (i = 0; i < 4; i++)
2913 if (*s == "xyzw"[i])
2914 {
2915 *channels |= 1 << (3 - i);
2916 ++s;
2917 }
2918 return s;
2919}
2920
a92713e6
RS
2921/* Token types for parsed operand lists. */
2922enum mips_operand_token_type {
2923 /* A plain register, e.g. $f2. */
2924 OT_REG,
df58fc94 2925
14daeee3
RS
2926 /* A 4-bit XYZW channel mask. */
2927 OT_CHANNELS,
2928
56d438b1
CF
2929 /* A constant vector index, e.g. [1]. */
2930 OT_INTEGER_INDEX,
2931
2932 /* A register vector index, e.g. [$2]. */
2933 OT_REG_INDEX,
df58fc94 2934
a92713e6
RS
2935 /* A continuous range of registers, e.g. $s0-$s4. */
2936 OT_REG_RANGE,
2937
2938 /* A (possibly relocated) expression. */
2939 OT_INTEGER,
2940
2941 /* A floating-point value. */
2942 OT_FLOAT,
2943
2944 /* A single character. This can be '(', ')' or ',', but '(' only appears
2945 before OT_REGs. */
2946 OT_CHAR,
2947
14daeee3
RS
2948 /* A doubled character, either "--" or "++". */
2949 OT_DOUBLE_CHAR,
2950
a92713e6
RS
2951 /* The end of the operand list. */
2952 OT_END
2953};
2954
2955/* A parsed operand token. */
2956struct mips_operand_token
2957{
2958 /* The type of token. */
2959 enum mips_operand_token_type type;
2960 union
2961 {
56d438b1 2962 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
2963 unsigned int regno;
2964
14daeee3
RS
2965 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2966 unsigned int channels;
2967
56d438b1
CF
2968 /* The integer value of an OT_INTEGER_INDEX. */
2969 addressT index;
a92713e6
RS
2970
2971 /* The two register symbol values involved in an OT_REG_RANGE. */
2972 struct {
2973 unsigned int regno1;
2974 unsigned int regno2;
2975 } reg_range;
2976
2977 /* The value of an OT_INTEGER. The value is represented as an
2978 expression and the relocation operators that were applied to
2979 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2980 relocation operators were used. */
2981 struct {
2982 expressionS value;
2983 bfd_reloc_code_real_type relocs[3];
2984 } integer;
2985
2986 /* The binary data for an OT_FLOAT constant, and the number of bytes
2987 in the constant. */
2988 struct {
2989 unsigned char data[8];
2990 int length;
2991 } flt;
2992
14daeee3 2993 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2994 char ch;
2995 } u;
2996};
2997
2998/* An obstack used to construct lists of mips_operand_tokens. */
2999static struct obstack mips_operand_tokens;
3000
3001/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3002
3003static void
3004mips_add_token (struct mips_operand_token *token,
3005 enum mips_operand_token_type type)
3006{
3007 token->type = type;
3008 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3009}
3010
3011/* Check whether S is '(' followed by a register name. Add OT_CHAR
3012 and OT_REG tokens for them if so, and return a pointer to the first
3013 unconsumed character. Return null otherwise. */
3014
3015static char *
3016mips_parse_base_start (char *s)
3017{
3018 struct mips_operand_token token;
14daeee3
RS
3019 unsigned int regno, channels;
3020 bfd_boolean decrement_p;
df58fc94 3021
a92713e6
RS
3022 if (*s != '(')
3023 return 0;
3024
3025 ++s;
3026 SKIP_SPACE_TABS (s);
14daeee3
RS
3027
3028 /* Only match "--" as part of a base expression. In other contexts "--X"
3029 is a double negative. */
3030 decrement_p = (s[0] == '-' && s[1] == '-');
3031 if (decrement_p)
3032 {
3033 s += 2;
3034 SKIP_SPACE_TABS (s);
3035 }
3036
3037 /* Allow a channel specifier because that leads to better error messages
3038 than treating something like "$vf0x++" as an expression. */
3039 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3040 return 0;
3041
3042 token.u.ch = '(';
3043 mips_add_token (&token, OT_CHAR);
3044
14daeee3
RS
3045 if (decrement_p)
3046 {
3047 token.u.ch = '-';
3048 mips_add_token (&token, OT_DOUBLE_CHAR);
3049 }
3050
a92713e6
RS
3051 token.u.regno = regno;
3052 mips_add_token (&token, OT_REG);
3053
14daeee3
RS
3054 if (channels)
3055 {
3056 token.u.channels = channels;
3057 mips_add_token (&token, OT_CHANNELS);
3058 }
3059
3060 /* For consistency, only match "++" as part of base expressions too. */
3061 SKIP_SPACE_TABS (s);
3062 if (s[0] == '+' && s[1] == '+')
3063 {
3064 s += 2;
3065 token.u.ch = '+';
3066 mips_add_token (&token, OT_DOUBLE_CHAR);
3067 }
3068
a92713e6
RS
3069 return s;
3070}
3071
3072/* Parse one or more tokens from S. Return a pointer to the first
3073 unconsumed character on success. Return null if an error was found
3074 and store the error text in insn_error. FLOAT_FORMAT is as for
3075 mips_parse_arguments. */
3076
3077static char *
3078mips_parse_argument_token (char *s, char float_format)
3079{
6d4af3c2
AM
3080 char *end, *save_in;
3081 const char *err;
14daeee3 3082 unsigned int regno1, regno2, channels;
a92713e6
RS
3083 struct mips_operand_token token;
3084
3085 /* First look for "($reg", since we want to treat that as an
3086 OT_CHAR and OT_REG rather than an expression. */
3087 end = mips_parse_base_start (s);
3088 if (end)
3089 return end;
3090
3091 /* Handle other characters that end up as OT_CHARs. */
3092 if (*s == ')' || *s == ',')
3093 {
3094 token.u.ch = *s;
3095 mips_add_token (&token, OT_CHAR);
3096 ++s;
3097 return s;
3098 }
3099
3100 /* Handle tokens that start with a register. */
14daeee3 3101 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3102 {
14daeee3
RS
3103 if (channels)
3104 {
3105 /* A register and a VU0 channel suffix. */
3106 token.u.regno = regno1;
3107 mips_add_token (&token, OT_REG);
3108
3109 token.u.channels = channels;
3110 mips_add_token (&token, OT_CHANNELS);
3111 return s;
3112 }
3113
a92713e6
RS
3114 SKIP_SPACE_TABS (s);
3115 if (*s == '-')
df58fc94 3116 {
a92713e6
RS
3117 /* A register range. */
3118 ++s;
3119 SKIP_SPACE_TABS (s);
14daeee3 3120 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3121 {
1661c76c 3122 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3123 return 0;
3124 }
df58fc94 3125
a92713e6
RS
3126 token.u.reg_range.regno1 = regno1;
3127 token.u.reg_range.regno2 = regno2;
3128 mips_add_token (&token, OT_REG_RANGE);
3129 return s;
3130 }
a92713e6 3131
56d438b1
CF
3132 /* Add the register itself. */
3133 token.u.regno = regno1;
3134 mips_add_token (&token, OT_REG);
3135
3136 /* Check for a vector index. */
3137 if (*s == '[')
3138 {
a92713e6
RS
3139 ++s;
3140 SKIP_SPACE_TABS (s);
56d438b1
CF
3141 if (mips_parse_register (&s, &token.u.regno, NULL))
3142 mips_add_token (&token, OT_REG_INDEX);
3143 else
a92713e6 3144 {
56d438b1
CF
3145 expressionS element;
3146
3147 my_getExpression (&element, s);
3148 if (element.X_op != O_constant)
3149 {
3150 set_insn_error (0, _("vector element must be constant"));
3151 return 0;
3152 }
3153 s = expr_end;
3154 token.u.index = element.X_add_number;
3155 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3156 }
a92713e6
RS
3157 SKIP_SPACE_TABS (s);
3158 if (*s != ']')
3159 {
1661c76c 3160 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3161 return 0;
3162 }
3163 ++s;
df58fc94 3164 }
a92713e6 3165 return s;
df58fc94
RS
3166 }
3167
a92713e6
RS
3168 if (float_format)
3169 {
3170 /* First try to treat expressions as floats. */
3171 save_in = input_line_pointer;
3172 input_line_pointer = s;
3173 err = md_atof (float_format, (char *) token.u.flt.data,
3174 &token.u.flt.length);
3175 end = input_line_pointer;
3176 input_line_pointer = save_in;
3177 if (err && *err)
3178 {
e3de51ce 3179 set_insn_error (0, err);
a92713e6
RS
3180 return 0;
3181 }
3182 if (s != end)
3183 {
3184 mips_add_token (&token, OT_FLOAT);
3185 return end;
3186 }
3187 }
3188
3189 /* Treat everything else as an integer expression. */
3190 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3191 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3192 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3193 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3194 s = expr_end;
3195 mips_add_token (&token, OT_INTEGER);
3196 return s;
3197}
3198
3199/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3200 if expressions should be treated as 32-bit floating-point constants,
3201 'd' if they should be treated as 64-bit floating-point constants,
3202 or 0 if they should be treated as integer expressions (the usual case).
3203
3204 Return a list of tokens on success, otherwise return 0. The caller
3205 must obstack_free the list after use. */
3206
3207static struct mips_operand_token *
3208mips_parse_arguments (char *s, char float_format)
3209{
3210 struct mips_operand_token token;
3211
3212 SKIP_SPACE_TABS (s);
3213 while (*s)
3214 {
3215 s = mips_parse_argument_token (s, float_format);
3216 if (!s)
3217 {
3218 obstack_free (&mips_operand_tokens,
3219 obstack_finish (&mips_operand_tokens));
3220 return 0;
3221 }
3222 SKIP_SPACE_TABS (s);
3223 }
3224 mips_add_token (&token, OT_END);
3225 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3226}
3227
d301a56b
RS
3228/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3229 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3230
3231static bfd_boolean
f79e2745 3232is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3233{
3234 int isa = mips_opts.isa;
846ef2d0 3235 int ase = mips_opts.ase;
037b32b9 3236 int fp_s, fp_d;
c6278170 3237 unsigned int i;
037b32b9 3238
be0fcbee 3239 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3240 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3241 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3242 ase |= mips_ases[i].flags64;
037b32b9 3243
d301a56b 3244 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3245 return FALSE;
3246
3247 /* Check whether the instruction or macro requires single-precision or
3248 double-precision floating-point support. Note that this information is
3249 stored differently in the opcode table for insns and macros. */
3250 if (mo->pinfo == INSN_MACRO)
3251 {
3252 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3253 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3254 }
3255 else
3256 {
3257 fp_s = mo->pinfo & FP_S;
3258 fp_d = mo->pinfo & FP_D;
3259 }
3260
3261 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3262 return FALSE;
3263
3264 if (fp_s && mips_opts.soft_float)
3265 return FALSE;
3266
3267 return TRUE;
3268}
3269
3270/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3271 selected ISA and architecture. */
3272
3273static bfd_boolean
3274is_opcode_valid_16 (const struct mips_opcode *mo)
3275{
d301a56b 3276 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3277}
3278
df58fc94 3279/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3280 explicitly requested. Always TRUE in the standard MIPS mode.
3281 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3282
3283static bfd_boolean
3284is_size_valid (const struct mips_opcode *mo)
3285{
3286 if (!mips_opts.micromips)
3287 return TRUE;
3288
833794fc
MR
3289 if (mips_opts.insn32)
3290 {
3291 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3292 return FALSE;
3293 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3294 return FALSE;
3295 }
df58fc94
RS
3296 if (!forced_insn_length)
3297 return TRUE;
3298 if (mo->pinfo == INSN_MACRO)
3299 return FALSE;
3300 return forced_insn_length == micromips_insn_length (mo);
3301}
3302
7fd53920
MR
3303/* Return TRUE if the size of the MIPS16 opcode MO matches one
3304 explicitly requested. */
3305
3306static bfd_boolean
3307is_size_valid_16 (const struct mips_opcode *mo)
3308{
3309 if (!forced_insn_length)
3310 return TRUE;
3311 if (mo->pinfo == INSN_MACRO)
3312 return FALSE;
3313 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3314 return FALSE;
0674ee5d
MR
3315 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3316 return FALSE;
7fd53920
MR
3317 return TRUE;
3318}
3319
df58fc94 3320/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3321 of the preceding instruction. Always TRUE in the standard MIPS mode.
3322
3323 We don't accept macros in 16-bit delay slots to avoid a case where
3324 a macro expansion fails because it relies on a preceding 32-bit real
3325 instruction to have matched and does not handle the operands correctly.
3326 The only macros that may expand to 16-bit instructions are JAL that
3327 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3328 and BGT (that likewise cannot be placed in a delay slot) that decay to
3329 a NOP. In all these cases the macros precede any corresponding real
3330 instruction definitions in the opcode table, so they will match in the
3331 second pass where the size of the delay slot is ignored and therefore
3332 produce correct code. */
df58fc94
RS
3333
3334static bfd_boolean
3335is_delay_slot_valid (const struct mips_opcode *mo)
3336{
3337 if (!mips_opts.micromips)
3338 return TRUE;
3339
3340 if (mo->pinfo == INSN_MACRO)
c06dec14 3341 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3342 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3343 && micromips_insn_length (mo) != 4)
3344 return FALSE;
3345 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3346 && micromips_insn_length (mo) != 2)
3347 return FALSE;
3348
3349 return TRUE;
3350}
3351
fc76e730
RS
3352/* For consistency checking, verify that all bits of OPCODE are specified
3353 either by the match/mask part of the instruction definition, or by the
3354 operand list. Also build up a list of operands in OPERANDS.
3355
3356 INSN_BITS says which bits of the instruction are significant.
3357 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3358 provides the mips_operand description of each operand. DECODE_OPERAND
3359 is null for MIPS16 instructions. */
ab902481
RS
3360
3361static int
3362validate_mips_insn (const struct mips_opcode *opcode,
3363 unsigned long insn_bits,
fc76e730
RS
3364 const struct mips_operand *(*decode_operand) (const char *),
3365 struct mips_operand_array *operands)
ab902481
RS
3366{
3367 const char *s;
fc76e730 3368 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3369 const struct mips_operand *operand;
3370
fc76e730
RS
3371 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3372 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3373 {
3374 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3375 opcode->name, opcode->args);
3376 return 0;
3377 }
3378 used_bits = 0;
fc76e730 3379 opno = 0;
14daeee3
RS
3380 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3381 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3382 for (s = opcode->args; *s; ++s)
3383 switch (*s)
3384 {
3385 case ',':
3386 case '(':
3387 case ')':
3388 break;
3389
14daeee3
RS
3390 case '#':
3391 s++;
3392 break;
3393
ab902481 3394 default:
fc76e730 3395 if (!decode_operand)
7fd53920 3396 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3397 else
3398 operand = decode_operand (s);
3399 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3400 {
3401 as_bad (_("internal: unknown operand type: %s %s"),
3402 opcode->name, opcode->args);
3403 return 0;
3404 }
fc76e730
RS
3405 gas_assert (opno < MAX_OPERANDS);
3406 operands->operand[opno] = operand;
14daeee3 3407 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3408 {
14daeee3 3409 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3410 if (operand->type == OP_MDMX_IMM_REG)
3411 /* Bit 5 is the format selector (OB vs QH). The opcode table
3412 has separate entries for each format. */
3413 used_bits &= ~(1 << (operand->lsb + 5));
3414 if (operand->type == OP_ENTRY_EXIT_LIST)
3415 used_bits &= ~(mask & 0x700);
3416 }
ab902481 3417 /* Skip prefix characters. */
7361da2c 3418 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3419 ++s;
fc76e730 3420 opno += 1;
ab902481
RS
3421 break;
3422 }
fc76e730 3423 doubled = used_bits & mask & insn_bits;
ab902481
RS
3424 if (doubled)
3425 {
3426 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3427 " %s %s"), doubled, opcode->name, opcode->args);
3428 return 0;
3429 }
fc76e730 3430 used_bits |= mask;
ab902481 3431 undefined = ~used_bits & insn_bits;
fc76e730 3432 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3433 {
3434 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3435 undefined, opcode->name, opcode->args);
3436 return 0;
3437 }
3438 used_bits &= ~insn_bits;
3439 if (used_bits)
3440 {
3441 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3442 used_bits, opcode->name, opcode->args);
3443 return 0;
3444 }
3445 return 1;
3446}
3447
fc76e730
RS
3448/* The MIPS16 version of validate_mips_insn. */
3449
3450static int
3451validate_mips16_insn (const struct mips_opcode *opcode,
3452 struct mips_operand_array *operands)
3453{
7fd53920 3454 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3455
7fd53920 3456 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3457}
3458
ab902481
RS
3459/* The microMIPS version of validate_mips_insn. */
3460
3461static int
fc76e730
RS
3462validate_micromips_insn (const struct mips_opcode *opc,
3463 struct mips_operand_array *operands)
ab902481
RS
3464{
3465 unsigned long insn_bits;
3466 unsigned long major;
3467 unsigned int length;
3468
fc76e730
RS
3469 if (opc->pinfo == INSN_MACRO)
3470 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3471 operands);
3472
ab902481
RS
3473 length = micromips_insn_length (opc);
3474 if (length != 2 && length != 4)
3475 {
1661c76c 3476 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3477 "%s %s"), length, opc->name, opc->args);
3478 return 0;
3479 }
3480 major = opc->match >> (10 + 8 * (length - 2));
3481 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3482 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3483 {
1661c76c 3484 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3485 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3486 return 0;
3487 }
3488
3489 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3490 insn_bits = 1 << 4 * length;
3491 insn_bits <<= 4 * length;
3492 insn_bits -= 1;
fc76e730
RS
3493 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3494 operands);
ab902481
RS
3495}
3496
707bfff6
TS
3497/* This function is called once, at assembler startup time. It should set up
3498 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3499
252b5132 3500void
17a2f251 3501md_begin (void)
252b5132 3502{
3994f87e 3503 const char *retval = NULL;
156c2f8b 3504 int i = 0;
252b5132 3505 int broken = 0;
1f25f5d3 3506
0a44bf69
RS
3507 if (mips_pic != NO_PIC)
3508 {
3509 if (g_switch_seen && g_switch_value != 0)
3510 as_bad (_("-G may not be used in position-independent code"));
3511 g_switch_value = 0;
3512 }
00acd688
CM
3513 else if (mips_abicalls)
3514 {
3515 if (g_switch_seen && g_switch_value != 0)
3516 as_bad (_("-G may not be used with abicalls"));
3517 g_switch_value = 0;
3518 }
0a44bf69 3519
0b35dfee 3520 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3521 as_warn (_("could not set architecture and machine"));
252b5132 3522
252b5132
RH
3523 op_hash = hash_new ();
3524
fc76e730 3525 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3526 for (i = 0; i < NUMOPCODES;)
3527 {
3528 const char *name = mips_opcodes[i].name;
3529
17a2f251 3530 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3531 if (retval != NULL)
3532 {
3533 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3534 mips_opcodes[i].name, retval);
3535 /* Probably a memory allocation problem? Give up now. */
1661c76c 3536 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3537 }
3538 do
3539 {
fc76e730
RS
3540 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3541 decode_mips_operand, &mips_operands[i]))
3542 broken = 1;
3543 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3544 {
fc76e730
RS
3545 create_insn (&nop_insn, mips_opcodes + i);
3546 if (mips_fix_loongson2f_nop)
3547 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3548 nop_insn.fixed_p = 1;
252b5132
RH
3549 }
3550 ++i;
3551 }
3552 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3553 }
3554
3555 mips16_op_hash = hash_new ();
fc76e730
RS
3556 mips16_operands = XCNEWVEC (struct mips_operand_array,
3557 bfd_mips16_num_opcodes);
252b5132
RH
3558
3559 i = 0;
3560 while (i < bfd_mips16_num_opcodes)
3561 {
3562 const char *name = mips16_opcodes[i].name;
3563
17a2f251 3564 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3565 if (retval != NULL)
3566 as_fatal (_("internal: can't hash `%s': %s"),
3567 mips16_opcodes[i].name, retval);
3568 do
3569 {
fc76e730
RS
3570 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3571 broken = 1;
1e915849
RS
3572 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3573 {
3574 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3575 mips16_nop_insn.fixed_p = 1;
3576 }
252b5132
RH
3577 ++i;
3578 }
3579 while (i < bfd_mips16_num_opcodes
3580 && strcmp (mips16_opcodes[i].name, name) == 0);
3581 }
3582
df58fc94 3583 micromips_op_hash = hash_new ();
fc76e730
RS
3584 micromips_operands = XCNEWVEC (struct mips_operand_array,
3585 bfd_micromips_num_opcodes);
df58fc94
RS
3586
3587 i = 0;
3588 while (i < bfd_micromips_num_opcodes)
3589 {
3590 const char *name = micromips_opcodes[i].name;
3591
3592 retval = hash_insert (micromips_op_hash, name,
3593 (void *) &micromips_opcodes[i]);
3594 if (retval != NULL)
3595 as_fatal (_("internal: can't hash `%s': %s"),
3596 micromips_opcodes[i].name, retval);
3597 do
fc76e730
RS
3598 {
3599 struct mips_cl_insn *micromips_nop_insn;
3600
3601 if (!validate_micromips_insn (&micromips_opcodes[i],
3602 &micromips_operands[i]))
3603 broken = 1;
3604
3605 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3606 {
3607 if (micromips_insn_length (micromips_opcodes + i) == 2)
3608 micromips_nop_insn = &micromips_nop16_insn;
3609 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3610 micromips_nop_insn = &micromips_nop32_insn;
3611 else
3612 continue;
3613
3614 if (micromips_nop_insn->insn_mo == NULL
3615 && strcmp (name, "nop") == 0)
3616 {
3617 create_insn (micromips_nop_insn, micromips_opcodes + i);
3618 micromips_nop_insn->fixed_p = 1;
3619 }
3620 }
3621 }
df58fc94
RS
3622 while (++i < bfd_micromips_num_opcodes
3623 && strcmp (micromips_opcodes[i].name, name) == 0);
3624 }
3625
252b5132 3626 if (broken)
1661c76c 3627 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3628
3629 /* We add all the general register names to the symbol table. This
3630 helps us detect invalid uses of them. */
3739860c 3631 for (i = 0; reg_names[i].name; i++)
707bfff6 3632 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3633 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3634 &zero_address_frag));
3635 if (HAVE_NEWABI)
3739860c 3636 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3637 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3638 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3639 &zero_address_frag));
707bfff6 3640 else
3739860c 3641 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3642 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3643 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3644 &zero_address_frag));
6047c971 3645
14daeee3
RS
3646 for (i = 0; i < 32; i++)
3647 {
92fce9bd 3648 char regname[6];
14daeee3
RS
3649
3650 /* R5900 VU0 floating-point register. */
92fce9bd 3651 sprintf (regname, "$vf%d", i);
14daeee3
RS
3652 symbol_table_insert (symbol_new (regname, reg_section,
3653 RTYPE_VF | i, &zero_address_frag));
3654
3655 /* R5900 VU0 integer register. */
92fce9bd 3656 sprintf (regname, "$vi%d", i);
14daeee3
RS
3657 symbol_table_insert (symbol_new (regname, reg_section,
3658 RTYPE_VI | i, &zero_address_frag));
3659
56d438b1 3660 /* MSA register. */
92fce9bd 3661 sprintf (regname, "$w%d", i);
56d438b1
CF
3662 symbol_table_insert (symbol_new (regname, reg_section,
3663 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3664 }
3665
a92713e6
RS
3666 obstack_init (&mips_operand_tokens);
3667
7d10b47d 3668 mips_no_prev_insn ();
252b5132
RH
3669
3670 mips_gprmask = 0;
3671 mips_cprmask[0] = 0;
3672 mips_cprmask[1] = 0;
3673 mips_cprmask[2] = 0;
3674 mips_cprmask[3] = 0;
3675
3676 /* set the default alignment for the text section (2**2) */
3677 record_alignment (text_section, 2);
3678
4d0d148d 3679 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3680
f3ded42a
RS
3681 /* On a native system other than VxWorks, sections must be aligned
3682 to 16 byte boundaries. When configured for an embedded ELF
3683 target, we don't bother. */
3684 if (strncmp (TARGET_OS, "elf", 3) != 0
3685 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3686 {
f3ded42a
RS
3687 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3688 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3689 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3690 }
252b5132 3691
f3ded42a
RS
3692 /* Create a .reginfo section for register masks and a .mdebug
3693 section for debugging information. */
3694 {
3695 segT seg;
3696 subsegT subseg;
3697 flagword flags;
3698 segT sec;
3699
3700 seg = now_seg;
3701 subseg = now_subseg;
3702
3703 /* The ABI says this section should be loaded so that the
3704 running program can access it. However, we don't load it
3705 if we are configured for an embedded target */
3706 flags = SEC_READONLY | SEC_DATA;
3707 if (strncmp (TARGET_OS, "elf", 3) != 0)
3708 flags |= SEC_ALLOC | SEC_LOAD;
3709
3710 if (mips_abi != N64_ABI)
252b5132 3711 {
f3ded42a 3712 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3713
f3ded42a
RS
3714 bfd_set_section_flags (stdoutput, sec, flags);
3715 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3716
f3ded42a
RS
3717 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3718 }
3719 else
3720 {
3721 /* The 64-bit ABI uses a .MIPS.options section rather than
3722 .reginfo section. */
3723 sec = subseg_new (".MIPS.options", (subsegT) 0);
3724 bfd_set_section_flags (stdoutput, sec, flags);
3725 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3726
f3ded42a
RS
3727 /* Set up the option header. */
3728 {
3729 Elf_Internal_Options opthdr;
3730 char *f;
3731
3732 opthdr.kind = ODK_REGINFO;
3733 opthdr.size = (sizeof (Elf_External_Options)
3734 + sizeof (Elf64_External_RegInfo));
3735 opthdr.section = 0;
3736 opthdr.info = 0;
3737 f = frag_more (sizeof (Elf_External_Options));
3738 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3739 (Elf_External_Options *) f);
3740
3741 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3742 }
3743 }
252b5132 3744
351cdf24
MF
3745 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3746 bfd_set_section_flags (stdoutput, sec,
3747 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3748 bfd_set_section_alignment (stdoutput, sec, 3);
3749 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3750
f3ded42a
RS
3751 if (ECOFF_DEBUGGING)
3752 {
3753 sec = subseg_new (".mdebug", (subsegT) 0);
3754 (void) bfd_set_section_flags (stdoutput, sec,
3755 SEC_HAS_CONTENTS | SEC_READONLY);
3756 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3757 }
f3ded42a
RS
3758 else if (mips_flag_pdr)
3759 {
3760 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3761 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3762 SEC_READONLY | SEC_RELOC
3763 | SEC_DEBUGGING);
3764 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3765 }
3766
3767 subseg_set (seg, subseg);
3768 }
252b5132 3769
71400594
RS
3770 if (mips_fix_vr4120)
3771 init_vr4120_conflicts ();
252b5132
RH
3772}
3773
351cdf24
MF
3774static inline void
3775fpabi_incompatible_with (int fpabi, const char *what)
3776{
3777 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3778 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3779}
3780
3781static inline void
3782fpabi_requires (int fpabi, const char *what)
3783{
3784 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3785 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3786}
3787
3788/* Check -mabi and register sizes against the specified FP ABI. */
3789static void
3790check_fpabi (int fpabi)
3791{
351cdf24
MF
3792 switch (fpabi)
3793 {
3794 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3795 if (file_mips_opts.soft_float)
3796 fpabi_incompatible_with (fpabi, "softfloat");
3797 else if (file_mips_opts.single_float)
3798 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3799 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3800 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3801 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3802 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3803 break;
3804
3805 case Val_GNU_MIPS_ABI_FP_XX:
3806 if (mips_abi != O32_ABI)
3807 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3808 else if (file_mips_opts.soft_float)
3809 fpabi_incompatible_with (fpabi, "softfloat");
3810 else if (file_mips_opts.single_float)
3811 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3812 else if (file_mips_opts.fp != 0)
3813 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3814 break;
3815
3816 case Val_GNU_MIPS_ABI_FP_64A:
3817 case Val_GNU_MIPS_ABI_FP_64:
3818 if (mips_abi != O32_ABI)
3819 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3820 else if (file_mips_opts.soft_float)
3821 fpabi_incompatible_with (fpabi, "softfloat");
3822 else if (file_mips_opts.single_float)
3823 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3824 else if (file_mips_opts.fp != 64)
3825 fpabi_requires (fpabi, "fp=64");
3826 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3827 fpabi_incompatible_with (fpabi, "nooddspreg");
3828 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3829 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3830 break;
3831
3832 case Val_GNU_MIPS_ABI_FP_SINGLE:
3833 if (file_mips_opts.soft_float)
3834 fpabi_incompatible_with (fpabi, "softfloat");
3835 else if (!file_mips_opts.single_float)
3836 fpabi_requires (fpabi, "singlefloat");
3837 break;
3838
3839 case Val_GNU_MIPS_ABI_FP_SOFT:
3840 if (!file_mips_opts.soft_float)
3841 fpabi_requires (fpabi, "softfloat");
3842 break;
3843
3844 case Val_GNU_MIPS_ABI_FP_OLD_64:
3845 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3846 Tag_GNU_MIPS_ABI_FP, fpabi);
3847 break;
3848
3350cc01
CM
3849 case Val_GNU_MIPS_ABI_FP_NAN2008:
3850 /* Silently ignore compatibility value. */
3851 break;
3852
351cdf24
MF
3853 default:
3854 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3855 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3856 break;
3857 }
351cdf24
MF
3858}
3859
919731af 3860/* Perform consistency checks on the current options. */
3861
3862static void
3863mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3864{
3865 /* Check the size of integer registers agrees with the ABI and ISA. */
3866 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3867 as_bad (_("`gp=64' used with a 32-bit processor"));
3868 else if (abi_checks
3869 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3870 as_bad (_("`gp=32' used with a 64-bit ABI"));
3871 else if (abi_checks
3872 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3873 as_bad (_("`gp=64' used with a 32-bit ABI"));
3874
3875 /* Check the size of the float registers agrees with the ABI and ISA. */
3876 switch (opts->fp)
3877 {
351cdf24
MF
3878 case 0:
3879 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3880 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3881 else if (opts->single_float == 1)
3882 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3883 break;
919731af 3884 case 64:
3885 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3886 as_bad (_("`fp=64' used with a 32-bit fpu"));
3887 else if (abi_checks
3888 && ABI_NEEDS_32BIT_REGS (mips_abi)
3889 && !ISA_HAS_MXHC1 (opts->isa))
3890 as_warn (_("`fp=64' used with a 32-bit ABI"));
3891 break;
3892 case 32:
3893 if (abi_checks
3894 && ABI_NEEDS_64BIT_REGS (mips_abi))
3895 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3896 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3897 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3898 break;
3899 default:
3900 as_bad (_("Unknown size of floating point registers"));
3901 break;
3902 }
3903
351cdf24
MF
3904 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3905 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3906
919731af 3907 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3908 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3909 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3910 && (opts->micromips == 1
3911 || opts->mips16 == 1))
1357373c 3912 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3913 opts->micromips ? "micromips" : "mips16",
5f4678bb 3914 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3915
3916 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3917 as_fatal (_("branch relaxation is not supported in `%s'"),
3918 mips_cpu_info_from_isa (opts->isa)->name);
919731af 3919}
3920
3921/* Perform consistency checks on the module level options exactly once.
3922 This is a deferred check that happens:
3923 at the first .set directive
3924 or, at the first pseudo op that generates code (inc .dc.a)
3925 or, at the first instruction
3926 or, at the end. */
3927
3928static void
3929file_mips_check_options (void)
3930{
3931 const struct mips_cpu_info *arch_info = 0;
3932
3933 if (file_mips_opts_checked)
3934 return;
3935
3936 /* The following code determines the register size.
3937 Similar code was added to GCC 3.3 (see override_options() in
3938 config/mips/mips.c). The GAS and GCC code should be kept in sync
3939 as much as possible. */
3940
3941 if (file_mips_opts.gp < 0)
3942 {
3943 /* Infer the integer register size from the ABI and processor.
3944 Restrict ourselves to 32-bit registers if that's all the
3945 processor has, or if the ABI cannot handle 64-bit registers. */
3946 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3947 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3948 ? 32 : 64;
3949 }
3950
3951 if (file_mips_opts.fp < 0)
3952 {
3953 /* No user specified float register size.
3954 ??? GAS treats single-float processors as though they had 64-bit
3955 float registers (although it complains when double-precision
3956 instructions are used). As things stand, saying they have 32-bit
3957 registers would lead to spurious "register must be even" messages.
3958 So here we assume float registers are never smaller than the
3959 integer ones. */
3960 if (file_mips_opts.gp == 64)
3961 /* 64-bit integer registers implies 64-bit float registers. */
3962 file_mips_opts.fp = 64;
3963 else if ((file_mips_opts.ase & FP64_ASES)
3964 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3965 /* Handle ASEs that require 64-bit float registers, if possible. */
3966 file_mips_opts.fp = 64;
7361da2c
AB
3967 else if (ISA_IS_R6 (mips_opts.isa))
3968 /* R6 implies 64-bit float registers. */
3969 file_mips_opts.fp = 64;
919731af 3970 else
3971 /* 32-bit float registers. */
3972 file_mips_opts.fp = 32;
3973 }
3974
3975 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3976
351cdf24
MF
3977 /* Disable operations on odd-numbered floating-point registers by default
3978 when using the FPXX ABI. */
3979 if (file_mips_opts.oddspreg < 0)
3980 {
3981 if (file_mips_opts.fp == 0)
3982 file_mips_opts.oddspreg = 0;
3983 else
3984 file_mips_opts.oddspreg = 1;
3985 }
3986
919731af 3987 /* End of GCC-shared inference code. */
3988
3989 /* This flag is set when we have a 64-bit capable CPU but use only
3990 32-bit wide registers. Note that EABI does not use it. */
3991 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
3992 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
3993 || mips_abi == O32_ABI))
3994 mips_32bitmode = 1;
3995
3996 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
3997 as_bad (_("trap exception not supported at ISA 1"));
3998
3999 /* If the selected architecture includes support for ASEs, enable
4000 generation of code for them. */
4001 if (file_mips_opts.mips16 == -1)
4002 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4003 if (file_mips_opts.micromips == -1)
4004 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4005 ? 1 : 0;
4006
7361da2c
AB
4007 if (mips_nan2008 == -1)
4008 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4009 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4010 as_fatal (_("`%s' does not support legacy NaN"),
4011 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4012
919731af 4013 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4014 being selected implicitly. */
4015 if (file_mips_opts.fp != 64)
4016 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4017
4018 /* If the user didn't explicitly select or deselect a particular ASE,
4019 use the default setting for the CPU. */
4020 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4021
4022 /* Set up the current options. These may change throughout assembly. */
4023 mips_opts = file_mips_opts;
4024
4025 mips_check_isa_supports_ases ();
4026 mips_check_options (&file_mips_opts, TRUE);
4027 file_mips_opts_checked = TRUE;
4028
4029 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4030 as_warn (_("could not set architecture and machine"));
4031}
4032
252b5132 4033void
17a2f251 4034md_assemble (char *str)
252b5132
RH
4035{
4036 struct mips_cl_insn insn;
f6688943
TS
4037 bfd_reloc_code_real_type unused_reloc[3]
4038 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4039
919731af 4040 file_mips_check_options ();
4041
252b5132 4042 imm_expr.X_op = O_absent;
252b5132 4043 offset_expr.X_op = O_absent;
f6688943
TS
4044 offset_reloc[0] = BFD_RELOC_UNUSED;
4045 offset_reloc[1] = BFD_RELOC_UNUSED;
4046 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4047
e1b47bd5
RS
4048 mips_mark_labels ();
4049 mips_assembling_insn = TRUE;
e3de51ce 4050 clear_insn_error ();
e1b47bd5 4051
252b5132
RH
4052 if (mips_opts.mips16)
4053 mips16_ip (str, &insn);
4054 else
4055 {
4056 mips_ip (str, &insn);
beae10d5
KH
4057 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4058 str, insn.insn_opcode));
252b5132
RH
4059 }
4060
e3de51ce
RS
4061 if (insn_error.msg)
4062 report_insn_error (str);
e1b47bd5 4063 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4064 {
584892a6 4065 macro_start ();
252b5132
RH
4066 if (mips_opts.mips16)
4067 mips16_macro (&insn);
4068 else
833794fc 4069 macro (&insn, str);
584892a6 4070 macro_end ();
252b5132
RH
4071 }
4072 else
4073 {
77bd4346 4074 if (offset_expr.X_op != O_absent)
df58fc94 4075 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4076 else
df58fc94 4077 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4078 }
e1b47bd5
RS
4079
4080 mips_assembling_insn = FALSE;
252b5132
RH
4081}
4082
738e5348
RS
4083/* Convenience functions for abstracting away the differences between
4084 MIPS16 and non-MIPS16 relocations. */
4085
4086static inline bfd_boolean
4087mips16_reloc_p (bfd_reloc_code_real_type reloc)
4088{
4089 switch (reloc)
4090 {
4091 case BFD_RELOC_MIPS16_JMP:
4092 case BFD_RELOC_MIPS16_GPREL:
4093 case BFD_RELOC_MIPS16_GOT16:
4094 case BFD_RELOC_MIPS16_CALL16:
4095 case BFD_RELOC_MIPS16_HI16_S:
4096 case BFD_RELOC_MIPS16_HI16:
4097 case BFD_RELOC_MIPS16_LO16:
c9775dde 4098 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4099 return TRUE;
4100
4101 default:
4102 return FALSE;
4103 }
4104}
4105
df58fc94
RS
4106static inline bfd_boolean
4107micromips_reloc_p (bfd_reloc_code_real_type reloc)
4108{
4109 switch (reloc)
4110 {
4111 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4112 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4113 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4114 case BFD_RELOC_MICROMIPS_GPREL16:
4115 case BFD_RELOC_MICROMIPS_JMP:
4116 case BFD_RELOC_MICROMIPS_HI16:
4117 case BFD_RELOC_MICROMIPS_HI16_S:
4118 case BFD_RELOC_MICROMIPS_LO16:
4119 case BFD_RELOC_MICROMIPS_LITERAL:
4120 case BFD_RELOC_MICROMIPS_GOT16:
4121 case BFD_RELOC_MICROMIPS_CALL16:
4122 case BFD_RELOC_MICROMIPS_GOT_HI16:
4123 case BFD_RELOC_MICROMIPS_GOT_LO16:
4124 case BFD_RELOC_MICROMIPS_CALL_HI16:
4125 case BFD_RELOC_MICROMIPS_CALL_LO16:
4126 case BFD_RELOC_MICROMIPS_SUB:
4127 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4128 case BFD_RELOC_MICROMIPS_GOT_OFST:
4129 case BFD_RELOC_MICROMIPS_GOT_DISP:
4130 case BFD_RELOC_MICROMIPS_HIGHEST:
4131 case BFD_RELOC_MICROMIPS_HIGHER:
4132 case BFD_RELOC_MICROMIPS_SCN_DISP:
4133 case BFD_RELOC_MICROMIPS_JALR:
4134 return TRUE;
4135
4136 default:
4137 return FALSE;
4138 }
4139}
4140
2309ddf2
MR
4141static inline bfd_boolean
4142jmp_reloc_p (bfd_reloc_code_real_type reloc)
4143{
4144 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4145}
4146
0e9c5a5c
MR
4147static inline bfd_boolean
4148b_reloc_p (bfd_reloc_code_real_type reloc)
4149{
4150 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4151 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4152 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4153 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4154 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4155 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4156 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4157}
4158
738e5348
RS
4159static inline bfd_boolean
4160got16_reloc_p (bfd_reloc_code_real_type reloc)
4161{
2309ddf2 4162 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4163 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4164}
4165
4166static inline bfd_boolean
4167hi16_reloc_p (bfd_reloc_code_real_type reloc)
4168{
2309ddf2 4169 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4170 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4171}
4172
4173static inline bfd_boolean
4174lo16_reloc_p (bfd_reloc_code_real_type reloc)
4175{
2309ddf2 4176 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4177 || reloc == BFD_RELOC_MICROMIPS_LO16);
4178}
4179
df58fc94
RS
4180static inline bfd_boolean
4181jalr_reloc_p (bfd_reloc_code_real_type reloc)
4182{
2309ddf2 4183 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4184}
4185
f2ae14a1
RS
4186static inline bfd_boolean
4187gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4188{
4189 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4190 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4191}
4192
2de39019
CM
4193/* Return true if RELOC is a PC-relative relocation that does not have
4194 full address range. */
4195
4196static inline bfd_boolean
4197limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4198{
4199 switch (reloc)
4200 {
4201 case BFD_RELOC_16_PCREL_S2:
c9775dde 4202 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4203 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4204 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4205 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4206 case BFD_RELOC_MIPS_21_PCREL_S2:
4207 case BFD_RELOC_MIPS_26_PCREL_S2:
4208 case BFD_RELOC_MIPS_18_PCREL_S3:
4209 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4210 return TRUE;
4211
b47468a6 4212 case BFD_RELOC_32_PCREL:
7361da2c
AB
4213 case BFD_RELOC_HI16_S_PCREL:
4214 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4215 return HAVE_64BIT_ADDRESSES;
4216
2de39019
CM
4217 default:
4218 return FALSE;
4219 }
4220}
b47468a6 4221
5919d012 4222/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4223 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4224 need a matching %lo() when applied to local symbols. */
5919d012
RS
4225
4226static inline bfd_boolean
17a2f251 4227reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4228{
3b91255e 4229 return (HAVE_IN_PLACE_ADDENDS
738e5348 4230 && (hi16_reloc_p (reloc)
0a44bf69
RS
4231 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4232 all GOT16 relocations evaluate to "G". */
738e5348
RS
4233 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4234}
4235
4236/* Return the type of %lo() reloc needed by RELOC, given that
4237 reloc_needs_lo_p. */
4238
4239static inline bfd_reloc_code_real_type
4240matching_lo_reloc (bfd_reloc_code_real_type reloc)
4241{
df58fc94
RS
4242 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4243 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4244 : BFD_RELOC_LO16));
5919d012
RS
4245}
4246
4247/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4248 relocation. */
4249
4250static inline bfd_boolean
17a2f251 4251fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4252{
4253 return (fixp->fx_next != NULL
738e5348 4254 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4255 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4256 && fixp->fx_offset == fixp->fx_next->fx_offset);
4257}
4258
462427c4
RS
4259/* Move all labels in LABELS to the current insertion point. TEXT_P
4260 says whether the labels refer to text or data. */
404a8071
RS
4261
4262static void
462427c4 4263mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4264{
4265 struct insn_label_list *l;
4266 valueT val;
4267
462427c4 4268 for (l = labels; l != NULL; l = l->next)
404a8071 4269 {
9c2799c2 4270 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4271 symbol_set_frag (l->label, frag_now);
4272 val = (valueT) frag_now_fix ();
df58fc94 4273 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4274 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4275 ++val;
4276 S_SET_VALUE (l->label, val);
4277 }
4278}
4279
462427c4
RS
4280/* Move all labels in insn_labels to the current insertion point
4281 and treat them as text labels. */
4282
4283static void
4284mips_move_text_labels (void)
4285{
4286 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4287}
4288
9e009953
MR
4289/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4290
5f0fe04b
TS
4291static bfd_boolean
4292s_is_linkonce (symbolS *sym, segT from_seg)
4293{
4294 bfd_boolean linkonce = FALSE;
4295 segT symseg = S_GET_SEGMENT (sym);
4296
4297 if (symseg != from_seg && !S_IS_LOCAL (sym))
4298 {
4299 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4300 linkonce = TRUE;
5f0fe04b
TS
4301 /* The GNU toolchain uses an extension for ELF: a section
4302 beginning with the magic string .gnu.linkonce is a
4303 linkonce section. */
4304 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4305 sizeof ".gnu.linkonce" - 1) == 0)
4306 linkonce = TRUE;
5f0fe04b
TS
4307 }
4308 return linkonce;
4309}
4310
e1b47bd5 4311/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4312 linker to handle them specially, such as generating jalx instructions
4313 when needed. We also make them odd for the duration of the assembly,
4314 in order to generate the right sort of code. We will make them even
252b5132
RH
4315 in the adjust_symtab routine, while leaving them marked. This is
4316 convenient for the debugger and the disassembler. The linker knows
4317 to make them odd again. */
4318
4319static void
e1b47bd5 4320mips_compressed_mark_label (symbolS *label)
252b5132 4321{
df58fc94 4322 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4323
f3ded42a
RS
4324 if (mips_opts.mips16)
4325 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4326 else
4327 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4328 if ((S_GET_VALUE (label) & 1) == 0
4329 /* Don't adjust the address if the label is global or weak, or
4330 in a link-once section, since we'll be emitting symbol reloc
4331 references to it which will be patched up by the linker, and
4332 the final value of the symbol may or may not be MIPS16/microMIPS. */
4333 && !S_IS_WEAK (label)
4334 && !S_IS_EXTERNAL (label)
4335 && !s_is_linkonce (label, now_seg))
4336 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4337}
4338
4339/* Mark preceding MIPS16 or microMIPS instruction labels. */
4340
4341static void
4342mips_compressed_mark_labels (void)
4343{
4344 struct insn_label_list *l;
4345
4346 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4347 mips_compressed_mark_label (l->label);
252b5132
RH
4348}
4349
4d7206a2
RS
4350/* End the current frag. Make it a variant frag and record the
4351 relaxation info. */
4352
4353static void
4354relax_close_frag (void)
4355{
584892a6 4356 mips_macro_warning.first_frag = frag_now;
4d7206a2 4357 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4358 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4359 mips_pic != NO_PIC),
4d7206a2
RS
4360 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4361
4362 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4363 mips_relax.first_fixup = 0;
4364}
4365
4366/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4367 See the comment above RELAX_ENCODE for more details. */
4368
4369static void
4370relax_start (symbolS *symbol)
4371{
9c2799c2 4372 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4373 mips_relax.sequence = 1;
4374 mips_relax.symbol = symbol;
4375}
4376
4377/* Start generating the second version of a relaxable sequence.
4378 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4379
4380static void
4d7206a2
RS
4381relax_switch (void)
4382{
9c2799c2 4383 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4384 mips_relax.sequence = 2;
4385}
4386
4387/* End the current relaxable sequence. */
4388
4389static void
4390relax_end (void)
4391{
9c2799c2 4392 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4393 relax_close_frag ();
4394 mips_relax.sequence = 0;
4395}
4396
11625dd8
RS
4397/* Return true if IP is a delayed branch or jump. */
4398
4399static inline bfd_boolean
4400delayed_branch_p (const struct mips_cl_insn *ip)
4401{
4402 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4403 | INSN_COND_BRANCH_DELAY
4404 | INSN_COND_BRANCH_LIKELY)) != 0;
4405}
4406
4407/* Return true if IP is a compact branch or jump. */
4408
4409static inline bfd_boolean
4410compact_branch_p (const struct mips_cl_insn *ip)
4411{
26545944
RS
4412 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4413 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4414}
4415
4416/* Return true if IP is an unconditional branch or jump. */
4417
4418static inline bfd_boolean
4419uncond_branch_p (const struct mips_cl_insn *ip)
4420{
4421 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4422 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4423}
4424
4425/* Return true if IP is a branch-likely instruction. */
4426
4427static inline bfd_boolean
4428branch_likely_p (const struct mips_cl_insn *ip)
4429{
4430 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4431}
4432
14fe068b
RS
4433/* Return the type of nop that should be used to fill the delay slot
4434 of delayed branch IP. */
4435
4436static struct mips_cl_insn *
4437get_delay_slot_nop (const struct mips_cl_insn *ip)
4438{
4439 if (mips_opts.micromips
4440 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4441 return &micromips_nop32_insn;
4442 return NOP_INSN;
4443}
4444
fc76e730
RS
4445/* Return a mask that has bit N set if OPCODE reads the register(s)
4446 in operand N. */
df58fc94
RS
4447
4448static unsigned int
fc76e730 4449insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4450{
fc76e730
RS
4451 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4452}
df58fc94 4453
fc76e730
RS
4454/* Return a mask that has bit N set if OPCODE writes to the register(s)
4455 in operand N. */
4456
4457static unsigned int
4458insn_write_mask (const struct mips_opcode *opcode)
4459{
4460 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4461}
4462
4463/* Return a mask of the registers specified by operand OPERAND of INSN.
4464 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4465 is set. */
4466
4467static unsigned int
4468operand_reg_mask (const struct mips_cl_insn *insn,
4469 const struct mips_operand *operand,
4470 unsigned int type_mask)
4471{
4472 unsigned int uval, vsel;
4473
4474 switch (operand->type)
df58fc94 4475 {
fc76e730
RS
4476 case OP_INT:
4477 case OP_MAPPED_INT:
4478 case OP_MSB:
4479 case OP_PCREL:
4480 case OP_PERF_REG:
4481 case OP_ADDIUSP_INT:
4482 case OP_ENTRY_EXIT_LIST:
4483 case OP_REPEAT_DEST_REG:
4484 case OP_REPEAT_PREV_REG:
4485 case OP_PC:
14daeee3
RS
4486 case OP_VU0_SUFFIX:
4487 case OP_VU0_MATCH_SUFFIX:
56d438b1 4488 case OP_IMM_INDEX:
fc76e730
RS
4489 abort ();
4490
4491 case OP_REG:
0f35dbc4 4492 case OP_OPTIONAL_REG:
fc76e730
RS
4493 {
4494 const struct mips_reg_operand *reg_op;
4495
4496 reg_op = (const struct mips_reg_operand *) operand;
4497 if (!(type_mask & (1 << reg_op->reg_type)))
4498 return 0;
4499 uval = insn_extract_operand (insn, operand);
4500 return 1 << mips_decode_reg_operand (reg_op, uval);
4501 }
4502
4503 case OP_REG_PAIR:
4504 {
4505 const struct mips_reg_pair_operand *pair_op;
4506
4507 pair_op = (const struct mips_reg_pair_operand *) operand;
4508 if (!(type_mask & (1 << pair_op->reg_type)))
4509 return 0;
4510 uval = insn_extract_operand (insn, operand);
4511 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4512 }
4513
4514 case OP_CLO_CLZ_DEST:
4515 if (!(type_mask & (1 << OP_REG_GP)))
4516 return 0;
4517 uval = insn_extract_operand (insn, operand);
4518 return (1 << (uval & 31)) | (1 << (uval >> 5));
4519
7361da2c
AB
4520 case OP_SAME_RS_RT:
4521 if (!(type_mask & (1 << OP_REG_GP)))
4522 return 0;
4523 uval = insn_extract_operand (insn, operand);
4524 gas_assert ((uval & 31) == (uval >> 5));
4525 return 1 << (uval & 31);
4526
4527 case OP_CHECK_PREV:
4528 case OP_NON_ZERO_REG:
4529 if (!(type_mask & (1 << OP_REG_GP)))
4530 return 0;
4531 uval = insn_extract_operand (insn, operand);
4532 return 1 << (uval & 31);
4533
fc76e730
RS
4534 case OP_LWM_SWM_LIST:
4535 abort ();
4536
4537 case OP_SAVE_RESTORE_LIST:
4538 abort ();
4539
4540 case OP_MDMX_IMM_REG:
4541 if (!(type_mask & (1 << OP_REG_VEC)))
4542 return 0;
4543 uval = insn_extract_operand (insn, operand);
4544 vsel = uval >> 5;
4545 if ((vsel & 0x18) == 0x18)
4546 return 0;
4547 return 1 << (uval & 31);
56d438b1
CF
4548
4549 case OP_REG_INDEX:
4550 if (!(type_mask & (1 << OP_REG_GP)))
4551 return 0;
4552 return 1 << insn_extract_operand (insn, operand);
df58fc94 4553 }
fc76e730
RS
4554 abort ();
4555}
4556
4557/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4558 where bit N of OPNO_MASK is set if operand N should be included.
4559 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4560 is set. */
4561
4562static unsigned int
4563insn_reg_mask (const struct mips_cl_insn *insn,
4564 unsigned int type_mask, unsigned int opno_mask)
4565{
4566 unsigned int opno, reg_mask;
4567
4568 opno = 0;
4569 reg_mask = 0;
4570 while (opno_mask != 0)
4571 {
4572 if (opno_mask & 1)
4573 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4574 opno_mask >>= 1;
4575 opno += 1;
4576 }
4577 return reg_mask;
df58fc94
RS
4578}
4579
4c260379
RS
4580/* Return the mask of core registers that IP reads. */
4581
4582static unsigned int
4583gpr_read_mask (const struct mips_cl_insn *ip)
4584{
4585 unsigned long pinfo, pinfo2;
4586 unsigned int mask;
4587
fc76e730 4588 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4589 pinfo = ip->insn_mo->pinfo;
4590 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4591 if (pinfo & INSN_UDI)
4c260379 4592 {
fc76e730
RS
4593 /* UDI instructions have traditionally been assumed to read RS
4594 and RT. */
4595 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4596 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4597 }
fc76e730
RS
4598 if (pinfo & INSN_READ_GPR_24)
4599 mask |= 1 << 24;
4600 if (pinfo2 & INSN2_READ_GPR_16)
4601 mask |= 1 << 16;
4602 if (pinfo2 & INSN2_READ_SP)
4603 mask |= 1 << SP;
26545944 4604 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4605 mask |= 1 << 31;
fe35f09f
RS
4606 /* Don't include register 0. */
4607 return mask & ~1;
4c260379
RS
4608}
4609
4610/* Return the mask of core registers that IP writes. */
4611
4612static unsigned int
4613gpr_write_mask (const struct mips_cl_insn *ip)
4614{
4615 unsigned long pinfo, pinfo2;
4616 unsigned int mask;
4617
fc76e730 4618 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4619 pinfo = ip->insn_mo->pinfo;
4620 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4621 if (pinfo & INSN_WRITE_GPR_24)
4622 mask |= 1 << 24;
4623 if (pinfo & INSN_WRITE_GPR_31)
4624 mask |= 1 << 31;
4625 if (pinfo & INSN_UDI)
4626 /* UDI instructions have traditionally been assumed to write to RD. */
4627 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4628 if (pinfo2 & INSN2_WRITE_SP)
4629 mask |= 1 << SP;
fe35f09f
RS
4630 /* Don't include register 0. */
4631 return mask & ~1;
4c260379
RS
4632}
4633
4634/* Return the mask of floating-point registers that IP reads. */
4635
4636static unsigned int
4637fpr_read_mask (const struct mips_cl_insn *ip)
4638{
fc76e730 4639 unsigned long pinfo;
4c260379
RS
4640 unsigned int mask;
4641
9d5de888
CF
4642 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4643 | (1 << OP_REG_MSA)),
fc76e730 4644 insn_read_mask (ip->insn_mo));
4c260379 4645 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4646 /* Conservatively treat all operands to an FP_D instruction are doubles.
4647 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4648 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4649 mask |= mask << 1;
4650 return mask;
4651}
4652
4653/* Return the mask of floating-point registers that IP writes. */
4654
4655static unsigned int
4656fpr_write_mask (const struct mips_cl_insn *ip)
4657{
fc76e730 4658 unsigned long pinfo;
4c260379
RS
4659 unsigned int mask;
4660
9d5de888
CF
4661 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4662 | (1 << OP_REG_MSA)),
fc76e730 4663 insn_write_mask (ip->insn_mo));
4c260379 4664 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4665 /* Conservatively treat all operands to an FP_D instruction are doubles.
4666 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4667 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4668 mask |= mask << 1;
4669 return mask;
4670}
4671
a1d78564
RS
4672/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4673 Check whether that is allowed. */
4674
4675static bfd_boolean
4676mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4677{
4678 const char *s = insn->name;
351cdf24
MF
4679 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4680 || FPR_SIZE == 64)
4681 && mips_opts.oddspreg;
a1d78564
RS
4682
4683 if (insn->pinfo == INSN_MACRO)
4684 /* Let a macro pass, we'll catch it later when it is expanded. */
4685 return TRUE;
4686
351cdf24
MF
4687 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4688 otherwise it depends on oddspreg. */
4689 if ((insn->pinfo & FP_S)
4690 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4691 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4692 return FPR_SIZE == 32 || oddspreg;
a1d78564 4693
351cdf24
MF
4694 /* Allow odd registers for single-precision ops and double-precision if the
4695 floating-point registers are 64-bit wide. */
4696 switch (insn->pinfo & (FP_S | FP_D))
4697 {
4698 case FP_S:
4699 case 0:
4700 return oddspreg;
4701 case FP_D:
4702 return FPR_SIZE == 64;
4703 default:
4704 break;
a1d78564
RS
4705 }
4706
351cdf24
MF
4707 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4708 s = strchr (insn->name, '.');
4709 if (s != NULL && opnum == 2)
4710 s = strchr (s + 1, '.');
4711 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4712 return oddspreg;
a1d78564 4713
351cdf24 4714 return FPR_SIZE == 64;
a1d78564
RS
4715}
4716
a1d78564
RS
4717/* Information about an instruction argument that we're trying to match. */
4718struct mips_arg_info
4719{
4720 /* The instruction so far. */
4721 struct mips_cl_insn *insn;
4722
a92713e6
RS
4723 /* The first unconsumed operand token. */
4724 struct mips_operand_token *token;
4725
a1d78564
RS
4726 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4727 int opnum;
4728
4729 /* The 1-based argument number, for error reporting. This does not
4730 count elided optional registers, etc.. */
4731 int argnum;
4732
4733 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4734 unsigned int last_regno;
4735
4736 /* If the first operand was an OP_REG, this is the register that it
4737 specified, otherwise it is ILLEGAL_REG. */
4738 unsigned int dest_regno;
4739
4740 /* The value of the last OP_INT operand. Only used for OP_MSB,
4741 where it gives the lsb position. */
4742 unsigned int last_op_int;
4743
60f20e8b 4744 /* If true, match routines should assume that no later instruction
2b0f3761 4745 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4746 possible. Match routines should not report errors if something
4747 is only invalid for !LAX_MATCH. */
4748 bfd_boolean lax_match;
a1d78564 4749
a1d78564
RS
4750 /* True if a reference to the current AT register was seen. */
4751 bfd_boolean seen_at;
4752};
4753
1a00e612
RS
4754/* Record that the argument is out of range. */
4755
4756static void
4757match_out_of_range (struct mips_arg_info *arg)
4758{
4759 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4760}
4761
4762/* Record that the argument isn't constant but needs to be. */
4763
4764static void
4765match_not_constant (struct mips_arg_info *arg)
4766{
4767 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4768 arg->argnum);
4769}
4770
a92713e6
RS
4771/* Try to match an OT_CHAR token for character CH. Consume the token
4772 and return true on success, otherwise return false. */
a1d78564 4773
a92713e6
RS
4774static bfd_boolean
4775match_char (struct mips_arg_info *arg, char ch)
a1d78564 4776{
a92713e6
RS
4777 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4778 {
4779 ++arg->token;
4780 if (ch == ',')
4781 arg->argnum += 1;
4782 return TRUE;
4783 }
4784 return FALSE;
4785}
a1d78564 4786
a92713e6
RS
4787/* Try to get an expression from the next tokens in ARG. Consume the
4788 tokens and return true on success, storing the expression value in
4789 VALUE and relocation types in R. */
4790
4791static bfd_boolean
4792match_expression (struct mips_arg_info *arg, expressionS *value,
4793 bfd_reloc_code_real_type *r)
4794{
d436c1c2
RS
4795 /* If the next token is a '(' that was parsed as being part of a base
4796 expression, assume we have an elided offset. The later match will fail
4797 if this turns out to be wrong. */
4798 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4799 {
d436c1c2
RS
4800 value->X_op = O_constant;
4801 value->X_add_number = 0;
4802 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4803 return TRUE;
4804 }
4805
d436c1c2
RS
4806 /* Reject register-based expressions such as "0+$2" and "(($2))".
4807 For plain registers the default error seems more appropriate. */
4808 if (arg->token->type == OT_INTEGER
4809 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4810 {
d436c1c2
RS
4811 set_insn_error (arg->argnum, _("register value used as expression"));
4812 return FALSE;
a1d78564 4813 }
d436c1c2
RS
4814
4815 if (arg->token->type == OT_INTEGER)
a92713e6 4816 {
d436c1c2
RS
4817 *value = arg->token->u.integer.value;
4818 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4819 ++arg->token;
4820 return TRUE;
a92713e6 4821 }
a92713e6 4822
d436c1c2
RS
4823 set_insn_error_i
4824 (arg->argnum, _("operand %d must be an immediate expression"),
4825 arg->argnum);
4826 return FALSE;
a92713e6
RS
4827}
4828
4829/* Try to get a constant expression from the next tokens in ARG. Consume
4830 the tokens and return return true on success, storing the constant value
4831 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4832 error. */
4833
4834static bfd_boolean
1a00e612 4835match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4836{
4837 expressionS ex;
4838 bfd_reloc_code_real_type r[3];
a1d78564 4839
a92713e6
RS
4840 if (!match_expression (arg, &ex, r))
4841 return FALSE;
4842
4843 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4844 *value = ex.X_add_number;
4845 else
4846 {
1a00e612
RS
4847 match_not_constant (arg);
4848 return FALSE;
a1d78564 4849 }
a92713e6 4850 return TRUE;
a1d78564
RS
4851}
4852
4853/* Return the RTYPE_* flags for a register operand of type TYPE that
4854 appears in instruction OPCODE. */
4855
4856static unsigned int
4857convert_reg_type (const struct mips_opcode *opcode,
4858 enum mips_reg_operand_type type)
4859{
4860 switch (type)
4861 {
4862 case OP_REG_GP:
4863 return RTYPE_NUM | RTYPE_GP;
4864
4865 case OP_REG_FP:
4866 /* Allow vector register names for MDMX if the instruction is a 64-bit
4867 FPR load, store or move (including moves to and from GPRs). */
4868 if ((mips_opts.ase & ASE_MDMX)
4869 && (opcode->pinfo & FP_D)
43885403 4870 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4871 | INSN_COPROC_MEMORY_DELAY
43885403 4872 | INSN_LOAD_COPROC
67dc82bc 4873 | INSN_LOAD_MEMORY
a1d78564
RS
4874 | INSN_STORE_MEMORY)))
4875 return RTYPE_FPU | RTYPE_VEC;
4876 return RTYPE_FPU;
4877
4878 case OP_REG_CCC:
4879 if (opcode->pinfo & (FP_D | FP_S))
4880 return RTYPE_CCC | RTYPE_FCC;
4881 return RTYPE_CCC;
4882
4883 case OP_REG_VEC:
4884 if (opcode->membership & INSN_5400)
4885 return RTYPE_FPU;
4886 return RTYPE_FPU | RTYPE_VEC;
4887
4888 case OP_REG_ACC:
4889 return RTYPE_ACC;
4890
4891 case OP_REG_COPRO:
4892 if (opcode->name[strlen (opcode->name) - 1] == '0')
4893 return RTYPE_NUM | RTYPE_CP0;
4894 return RTYPE_NUM;
4895
4896 case OP_REG_HW:
4897 return RTYPE_NUM;
14daeee3
RS
4898
4899 case OP_REG_VI:
4900 return RTYPE_NUM | RTYPE_VI;
4901
4902 case OP_REG_VF:
4903 return RTYPE_NUM | RTYPE_VF;
4904
4905 case OP_REG_R5900_I:
4906 return RTYPE_R5900_I;
4907
4908 case OP_REG_R5900_Q:
4909 return RTYPE_R5900_Q;
4910
4911 case OP_REG_R5900_R:
4912 return RTYPE_R5900_R;
4913
4914 case OP_REG_R5900_ACC:
4915 return RTYPE_R5900_ACC;
56d438b1
CF
4916
4917 case OP_REG_MSA:
4918 return RTYPE_MSA;
4919
4920 case OP_REG_MSA_CTRL:
4921 return RTYPE_NUM;
a1d78564
RS
4922 }
4923 abort ();
4924}
4925
4926/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4927
4928static void
4929check_regno (struct mips_arg_info *arg,
4930 enum mips_reg_operand_type type, unsigned int regno)
4931{
4932 if (AT && type == OP_REG_GP && regno == AT)
4933 arg->seen_at = TRUE;
4934
4935 if (type == OP_REG_FP
4936 && (regno & 1) != 0
a1d78564 4937 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
4938 {
4939 /* This was a warning prior to introducing O32 FPXX and FP64 support
4940 so maintain a warning for FP32 but raise an error for the new
4941 cases. */
4942 if (FPR_SIZE == 32)
4943 as_warn (_("float register should be even, was %d"), regno);
4944 else
4945 as_bad (_("float register should be even, was %d"), regno);
4946 }
a1d78564
RS
4947
4948 if (type == OP_REG_CCC)
4949 {
4950 const char *name;
4951 size_t length;
4952
4953 name = arg->insn->insn_mo->name;
4954 length = strlen (name);
4955 if ((regno & 1) != 0
4956 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4957 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 4958 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
4959 name, regno);
4960
4961 if ((regno & 3) != 0
4962 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 4963 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
4964 name, regno);
4965 }
4966}
4967
a92713e6
RS
4968/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4969 a register of type TYPE. Return true on success, storing the register
4970 number in *REGNO and warning about any dubious uses. */
4971
4972static bfd_boolean
4973match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4974 unsigned int symval, unsigned int *regno)
4975{
4976 if (type == OP_REG_VEC)
4977 symval = mips_prefer_vec_regno (symval);
4978 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4979 return FALSE;
4980
4981 *regno = symval & RNUM_MASK;
4982 check_regno (arg, type, *regno);
4983 return TRUE;
4984}
4985
4986/* Try to interpret the next token in ARG as a register of type TYPE.
4987 Consume the token and return true on success, storing the register
4988 number in *REGNO. Return false on failure. */
4989
4990static bfd_boolean
4991match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4992 unsigned int *regno)
4993{
4994 if (arg->token->type == OT_REG
4995 && match_regno (arg, type, arg->token->u.regno, regno))
4996 {
4997 ++arg->token;
4998 return TRUE;
4999 }
5000 return FALSE;
5001}
5002
5003/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5004 Consume the token and return true on success, storing the register numbers
5005 in *REGNO1 and *REGNO2. Return false on failure. */
5006
5007static bfd_boolean
5008match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5009 unsigned int *regno1, unsigned int *regno2)
5010{
5011 if (match_reg (arg, type, regno1))
5012 {
5013 *regno2 = *regno1;
5014 return TRUE;
5015 }
5016 if (arg->token->type == OT_REG_RANGE
5017 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5018 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5019 && *regno1 <= *regno2)
5020 {
5021 ++arg->token;
5022 return TRUE;
5023 }
5024 return FALSE;
5025}
5026
a1d78564
RS
5027/* OP_INT matcher. */
5028
a92713e6 5029static bfd_boolean
a1d78564 5030match_int_operand (struct mips_arg_info *arg,
a92713e6 5031 const struct mips_operand *operand_base)
a1d78564
RS
5032{
5033 const struct mips_int_operand *operand;
3ccad066 5034 unsigned int uval;
a1d78564
RS
5035 int min_val, max_val, factor;
5036 offsetT sval;
a1d78564
RS
5037
5038 operand = (const struct mips_int_operand *) operand_base;
5039 factor = 1 << operand->shift;
3ccad066
RS
5040 min_val = mips_int_operand_min (operand);
5041 max_val = mips_int_operand_max (operand);
a1d78564 5042
d436c1c2
RS
5043 if (operand_base->lsb == 0
5044 && operand_base->size == 16
5045 && operand->shift == 0
5046 && operand->bias == 0
5047 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5048 {
5049 /* The operand can be relocated. */
a92713e6
RS
5050 if (!match_expression (arg, &offset_expr, offset_reloc))
5051 return FALSE;
5052
5053 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5054 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5055 leave the relocation value in offset_expr and offset_relocs
5056 for the caller to process. */
a92713e6
RS
5057 return TRUE;
5058
5059 if (offset_expr.X_op != O_constant)
a1d78564 5060 {
60f20e8b
RS
5061 /* Accept non-constant operands if no later alternative matches,
5062 leaving it for the caller to process. */
5063 if (!arg->lax_match)
5064 return FALSE;
a92713e6
RS
5065 offset_reloc[0] = BFD_RELOC_LO16;
5066 return TRUE;
a1d78564 5067 }
a92713e6 5068
a1d78564
RS
5069 /* Clear the global state; we're going to install the operand
5070 ourselves. */
a92713e6 5071 sval = offset_expr.X_add_number;
a1d78564 5072 offset_expr.X_op = O_absent;
60f20e8b
RS
5073
5074 /* For compatibility with older assemblers, we accept
5075 0x8000-0xffff as signed 16-bit numbers when only
5076 signed numbers are allowed. */
5077 if (sval > max_val)
5078 {
5079 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5080 if (!arg->lax_match && sval <= max_val)
5081 return FALSE;
5082 }
a1d78564
RS
5083 }
5084 else
5085 {
1a00e612 5086 if (!match_const_int (arg, &sval))
a92713e6 5087 return FALSE;
a1d78564
RS
5088 }
5089
5090 arg->last_op_int = sval;
5091
1a00e612 5092 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5093 {
1a00e612
RS
5094 match_out_of_range (arg);
5095 return FALSE;
a1d78564
RS
5096 }
5097
5098 uval = (unsigned int) sval >> operand->shift;
5099 uval -= operand->bias;
5100
5101 /* Handle -mfix-cn63xxp1. */
5102 if (arg->opnum == 1
5103 && mips_fix_cn63xxp1
5104 && !mips_opts.micromips
5105 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5106 switch (uval)
5107 {
5108 case 5:
5109 case 25:
5110 case 26:
5111 case 27:
5112 case 28:
5113 case 29:
5114 case 30:
5115 case 31:
5116 /* These are ok. */
5117 break;
5118
5119 default:
5120 /* The rest must be changed to 28. */
5121 uval = 28;
5122 break;
5123 }
5124
5125 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5126 return TRUE;
a1d78564
RS
5127}
5128
5129/* OP_MAPPED_INT matcher. */
5130
a92713e6 5131static bfd_boolean
a1d78564 5132match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5133 const struct mips_operand *operand_base)
a1d78564
RS
5134{
5135 const struct mips_mapped_int_operand *operand;
5136 unsigned int uval, num_vals;
5137 offsetT sval;
5138
5139 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5140 if (!match_const_int (arg, &sval))
a92713e6 5141 return FALSE;
a1d78564
RS
5142
5143 num_vals = 1 << operand_base->size;
5144 for (uval = 0; uval < num_vals; uval++)
5145 if (operand->int_map[uval] == sval)
5146 break;
5147 if (uval == num_vals)
1a00e612
RS
5148 {
5149 match_out_of_range (arg);
5150 return FALSE;
5151 }
a1d78564
RS
5152
5153 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5154 return TRUE;
a1d78564
RS
5155}
5156
5157/* OP_MSB matcher. */
5158
a92713e6 5159static bfd_boolean
a1d78564 5160match_msb_operand (struct mips_arg_info *arg,
a92713e6 5161 const struct mips_operand *operand_base)
a1d78564
RS
5162{
5163 const struct mips_msb_operand *operand;
5164 int min_val, max_val, max_high;
5165 offsetT size, sval, high;
5166
5167 operand = (const struct mips_msb_operand *) operand_base;
5168 min_val = operand->bias;
5169 max_val = min_val + (1 << operand_base->size) - 1;
5170 max_high = operand->opsize;
5171
1a00e612 5172 if (!match_const_int (arg, &size))
a92713e6 5173 return FALSE;
a1d78564
RS
5174
5175 high = size + arg->last_op_int;
5176 sval = operand->add_lsb ? high : size;
5177
5178 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5179 {
1a00e612
RS
5180 match_out_of_range (arg);
5181 return FALSE;
a1d78564
RS
5182 }
5183 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5184 return TRUE;
a1d78564
RS
5185}
5186
5187/* OP_REG matcher. */
5188
a92713e6 5189static bfd_boolean
a1d78564 5190match_reg_operand (struct mips_arg_info *arg,
a92713e6 5191 const struct mips_operand *operand_base)
a1d78564
RS
5192{
5193 const struct mips_reg_operand *operand;
a92713e6 5194 unsigned int regno, uval, num_vals;
a1d78564
RS
5195
5196 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5197 if (!match_reg (arg, operand->reg_type, &regno))
5198 return FALSE;
a1d78564
RS
5199
5200 if (operand->reg_map)
5201 {
5202 num_vals = 1 << operand->root.size;
5203 for (uval = 0; uval < num_vals; uval++)
5204 if (operand->reg_map[uval] == regno)
5205 break;
5206 if (num_vals == uval)
a92713e6 5207 return FALSE;
a1d78564
RS
5208 }
5209 else
5210 uval = regno;
5211
a1d78564
RS
5212 arg->last_regno = regno;
5213 if (arg->opnum == 1)
5214 arg->dest_regno = regno;
5215 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5216 return TRUE;
a1d78564
RS
5217}
5218
5219/* OP_REG_PAIR matcher. */
5220
a92713e6 5221static bfd_boolean
a1d78564 5222match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5223 const struct mips_operand *operand_base)
a1d78564
RS
5224{
5225 const struct mips_reg_pair_operand *operand;
a92713e6 5226 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5227
5228 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5229 if (!match_reg (arg, operand->reg_type, &regno1)
5230 || !match_char (arg, ',')
5231 || !match_reg (arg, operand->reg_type, &regno2))
5232 return FALSE;
a1d78564
RS
5233
5234 num_vals = 1 << operand_base->size;
5235 for (uval = 0; uval < num_vals; uval++)
5236 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5237 break;
5238 if (uval == num_vals)
a92713e6 5239 return FALSE;
a1d78564 5240
a1d78564 5241 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5242 return TRUE;
a1d78564
RS
5243}
5244
5245/* OP_PCREL matcher. The caller chooses the relocation type. */
5246
a92713e6
RS
5247static bfd_boolean
5248match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5249{
a92713e6
RS
5250 bfd_reloc_code_real_type r[3];
5251
5252 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5253}
5254
5255/* OP_PERF_REG matcher. */
5256
a92713e6 5257static bfd_boolean
a1d78564 5258match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5259 const struct mips_operand *operand)
a1d78564
RS
5260{
5261 offsetT sval;
5262
1a00e612 5263 if (!match_const_int (arg, &sval))
a92713e6 5264 return FALSE;
a1d78564
RS
5265
5266 if (sval != 0
5267 && (sval != 1
5268 || (mips_opts.arch == CPU_R5900
5269 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5270 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5271 {
1a00e612
RS
5272 set_insn_error (arg->argnum, _("invalid performance register"));
5273 return FALSE;
a1d78564
RS
5274 }
5275
5276 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5277 return TRUE;
a1d78564
RS
5278}
5279
5280/* OP_ADDIUSP matcher. */
5281
a92713e6 5282static bfd_boolean
a1d78564 5283match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5284 const struct mips_operand *operand)
a1d78564
RS
5285{
5286 offsetT sval;
5287 unsigned int uval;
5288
1a00e612 5289 if (!match_const_int (arg, &sval))
a92713e6 5290 return FALSE;
a1d78564
RS
5291
5292 if (sval % 4)
1a00e612
RS
5293 {
5294 match_out_of_range (arg);
5295 return FALSE;
5296 }
a1d78564
RS
5297
5298 sval /= 4;
5299 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5300 {
5301 match_out_of_range (arg);
5302 return FALSE;
5303 }
a1d78564
RS
5304
5305 uval = (unsigned int) sval;
5306 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5307 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5308 return TRUE;
a1d78564
RS
5309}
5310
5311/* OP_CLO_CLZ_DEST matcher. */
5312
a92713e6 5313static bfd_boolean
a1d78564 5314match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5315 const struct mips_operand *operand)
a1d78564
RS
5316{
5317 unsigned int regno;
5318
a92713e6
RS
5319 if (!match_reg (arg, OP_REG_GP, &regno))
5320 return FALSE;
a1d78564 5321
a1d78564 5322 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5323 return TRUE;
a1d78564
RS
5324}
5325
7361da2c
AB
5326/* OP_CHECK_PREV matcher. */
5327
5328static bfd_boolean
5329match_check_prev_operand (struct mips_arg_info *arg,
5330 const struct mips_operand *operand_base)
5331{
5332 const struct mips_check_prev_operand *operand;
5333 unsigned int regno;
5334
5335 operand = (const struct mips_check_prev_operand *) operand_base;
5336
5337 if (!match_reg (arg, OP_REG_GP, &regno))
5338 return FALSE;
5339
5340 if (!operand->zero_ok && regno == 0)
5341 return FALSE;
5342
5343 if ((operand->less_than_ok && regno < arg->last_regno)
5344 || (operand->greater_than_ok && regno > arg->last_regno)
5345 || (operand->equal_ok && regno == arg->last_regno))
5346 {
5347 arg->last_regno = regno;
5348 insn_insert_operand (arg->insn, operand_base, regno);
5349 return TRUE;
5350 }
5351
5352 return FALSE;
5353}
5354
5355/* OP_SAME_RS_RT matcher. */
5356
5357static bfd_boolean
5358match_same_rs_rt_operand (struct mips_arg_info *arg,
5359 const struct mips_operand *operand)
5360{
5361 unsigned int regno;
5362
5363 if (!match_reg (arg, OP_REG_GP, &regno))
5364 return FALSE;
5365
5366 if (regno == 0)
5367 {
5368 set_insn_error (arg->argnum, _("the source register must not be $0"));
5369 return FALSE;
5370 }
5371
5372 arg->last_regno = regno;
5373
5374 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5375 return TRUE;
5376}
5377
a1d78564
RS
5378/* OP_LWM_SWM_LIST matcher. */
5379
a92713e6 5380static bfd_boolean
a1d78564 5381match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5382 const struct mips_operand *operand)
a1d78564 5383{
a92713e6
RS
5384 unsigned int reglist, sregs, ra, regno1, regno2;
5385 struct mips_arg_info reset;
a1d78564 5386
a92713e6
RS
5387 reglist = 0;
5388 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5389 return FALSE;
5390 do
5391 {
5392 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5393 {
5394 reglist |= 1 << FP;
5395 regno2 = S7;
5396 }
5397 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5398 reset = *arg;
5399 }
5400 while (match_char (arg, ',')
5401 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5402 *arg = reset;
a1d78564
RS
5403
5404 if (operand->size == 2)
5405 {
5406 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5407
5408 s0, ra
5409 s0, s1, ra, s2, s3
5410 s0-s2, ra
5411
5412 and any permutations of these. */
5413 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5414 return FALSE;
a1d78564
RS
5415
5416 sregs = (reglist >> 17) & 7;
5417 ra = 0;
5418 }
5419 else
5420 {
5421 /* The list must include at least one of ra and s0-sN,
5422 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5423 which are $23 and $30 respectively.) E.g.:
5424
5425 ra
5426 s0
5427 ra, s0, s1, s2
5428 s0-s8
5429 s0-s5, ra
5430
5431 and any permutations of these. */
5432 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5433 return FALSE;
a1d78564
RS
5434
5435 ra = (reglist >> 27) & 0x10;
5436 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5437 }
5438 sregs += 1;
5439 if ((sregs & -sregs) != sregs)
a92713e6 5440 return FALSE;
a1d78564
RS
5441
5442 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5443 return TRUE;
a1d78564
RS
5444}
5445
364215c8
RS
5446/* OP_ENTRY_EXIT_LIST matcher. */
5447
a92713e6 5448static unsigned int
364215c8 5449match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5450 const struct mips_operand *operand)
364215c8
RS
5451{
5452 unsigned int mask;
5453 bfd_boolean is_exit;
5454
5455 /* The format is the same for both ENTRY and EXIT, but the constraints
5456 are different. */
5457 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5458 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5459 do
364215c8
RS
5460 {
5461 unsigned int regno1, regno2;
5462 bfd_boolean is_freg;
5463
a92713e6 5464 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5465 is_freg = FALSE;
a92713e6 5466 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5467 is_freg = TRUE;
5468 else
a92713e6 5469 return FALSE;
364215c8
RS
5470
5471 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5472 {
5473 mask &= ~(7 << 3);
5474 mask |= (5 + regno2) << 3;
5475 }
5476 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5477 mask |= (regno2 - 3) << 3;
5478 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5479 mask |= (regno2 - 15) << 1;
5480 else if (regno1 == RA && regno2 == RA)
5481 mask |= 1;
5482 else
a92713e6 5483 return FALSE;
364215c8 5484 }
a92713e6
RS
5485 while (match_char (arg, ','));
5486
364215c8 5487 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5488 return TRUE;
364215c8
RS
5489}
5490
5491/* OP_SAVE_RESTORE_LIST matcher. */
5492
a92713e6
RS
5493static bfd_boolean
5494match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5495{
5496 unsigned int opcode, args, statics, sregs;
5497 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 5498 offsetT frame_size;
364215c8 5499
364215c8
RS
5500 opcode = arg->insn->insn_opcode;
5501 frame_size = 0;
5502 num_frame_sizes = 0;
5503 args = 0;
5504 statics = 0;
5505 sregs = 0;
a92713e6 5506 do
364215c8
RS
5507 {
5508 unsigned int regno1, regno2;
5509
a92713e6 5510 if (arg->token->type == OT_INTEGER)
364215c8
RS
5511 {
5512 /* Handle the frame size. */
1a00e612 5513 if (!match_const_int (arg, &frame_size))
a92713e6 5514 return FALSE;
364215c8 5515 num_frame_sizes += 1;
364215c8
RS
5516 }
5517 else
5518 {
a92713e6
RS
5519 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5520 return FALSE;
364215c8
RS
5521
5522 while (regno1 <= regno2)
5523 {
5524 if (regno1 >= 4 && regno1 <= 7)
5525 {
5526 if (num_frame_sizes == 0)
5527 /* args $a0-$a3 */
5528 args |= 1 << (regno1 - 4);
5529 else
5530 /* statics $a0-$a3 */
5531 statics |= 1 << (regno1 - 4);
5532 }
5533 else if (regno1 >= 16 && regno1 <= 23)
5534 /* $s0-$s7 */
5535 sregs |= 1 << (regno1 - 16);
5536 else if (regno1 == 30)
5537 /* $s8 */
5538 sregs |= 1 << 8;
5539 else if (regno1 == 31)
5540 /* Add $ra to insn. */
5541 opcode |= 0x40;
5542 else
a92713e6 5543 return FALSE;
364215c8
RS
5544 regno1 += 1;
5545 if (regno1 == 24)
5546 regno1 = 30;
5547 }
5548 }
364215c8 5549 }
a92713e6 5550 while (match_char (arg, ','));
364215c8
RS
5551
5552 /* Encode args/statics combination. */
5553 if (args & statics)
a92713e6 5554 return FALSE;
364215c8
RS
5555 else if (args == 0xf)
5556 /* All $a0-$a3 are args. */
5557 opcode |= MIPS16_ALL_ARGS << 16;
5558 else if (statics == 0xf)
5559 /* All $a0-$a3 are statics. */
5560 opcode |= MIPS16_ALL_STATICS << 16;
5561 else
5562 {
5563 /* Count arg registers. */
5564 num_args = 0;
5565 while (args & 0x1)
5566 {
5567 args >>= 1;
5568 num_args += 1;
5569 }
5570 if (args != 0)
a92713e6 5571 return FALSE;
364215c8
RS
5572
5573 /* Count static registers. */
5574 num_statics = 0;
5575 while (statics & 0x8)
5576 {
5577 statics = (statics << 1) & 0xf;
5578 num_statics += 1;
5579 }
5580 if (statics != 0)
a92713e6 5581 return FALSE;
364215c8
RS
5582
5583 /* Encode args/statics. */
5584 opcode |= ((num_args << 2) | num_statics) << 16;
5585 }
5586
5587 /* Encode $s0/$s1. */
5588 if (sregs & (1 << 0)) /* $s0 */
5589 opcode |= 0x20;
5590 if (sregs & (1 << 1)) /* $s1 */
5591 opcode |= 0x10;
5592 sregs >>= 2;
5593
5594 /* Encode $s2-$s8. */
5595 num_sregs = 0;
5596 while (sregs & 1)
5597 {
5598 sregs >>= 1;
5599 num_sregs += 1;
5600 }
5601 if (sregs != 0)
a92713e6 5602 return FALSE;
364215c8
RS
5603 opcode |= num_sregs << 24;
5604
5605 /* Encode frame size. */
5606 if (num_frame_sizes == 0)
1a00e612
RS
5607 {
5608 set_insn_error (arg->argnum, _("missing frame size"));
5609 return FALSE;
5610 }
5611 if (num_frame_sizes > 1)
5612 {
5613 set_insn_error (arg->argnum, _("frame size specified twice"));
5614 return FALSE;
5615 }
5616 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5617 {
5618 set_insn_error (arg->argnum, _("invalid frame size"));
5619 return FALSE;
5620 }
5621 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5622 {
5623 frame_size /= 8;
5624 opcode |= (((frame_size & 0xf0) << 16)
5625 | (frame_size & 0x0f));
5626 }
5627
364215c8
RS
5628 /* Finally build the instruction. */
5629 if ((opcode >> 16) != 0 || frame_size == 0)
5630 opcode |= MIPS16_EXTEND;
5631 arg->insn->insn_opcode = opcode;
a92713e6 5632 return TRUE;
364215c8
RS
5633}
5634
a1d78564
RS
5635/* OP_MDMX_IMM_REG matcher. */
5636
a92713e6 5637static bfd_boolean
a1d78564 5638match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5639 const struct mips_operand *operand)
a1d78564 5640{
a92713e6 5641 unsigned int regno, uval;
a1d78564
RS
5642 bfd_boolean is_qh;
5643 const struct mips_opcode *opcode;
5644
5645 /* The mips_opcode records whether this is an octobyte or quadhalf
5646 instruction. Start out with that bit in place. */
5647 opcode = arg->insn->insn_mo;
5648 uval = mips_extract_operand (operand, opcode->match);
5649 is_qh = (uval != 0);
5650
56d438b1 5651 if (arg->token->type == OT_REG)
a1d78564
RS
5652 {
5653 if ((opcode->membership & INSN_5400)
5654 && strcmp (opcode->name, "rzu.ob") == 0)
5655 {
1a00e612
RS
5656 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5657 arg->argnum);
5658 return FALSE;
a1d78564
RS
5659 }
5660
56d438b1
CF
5661 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5662 return FALSE;
5663 ++arg->token;
5664
a1d78564
RS
5665 /* Check whether this is a vector register or a broadcast of
5666 a single element. */
56d438b1 5667 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5668 {
56d438b1 5669 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5670 {
1a00e612
RS
5671 set_insn_error (arg->argnum, _("invalid element selector"));
5672 return FALSE;
a1d78564 5673 }
56d438b1
CF
5674 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5675 ++arg->token;
a1d78564
RS
5676 }
5677 else
5678 {
5679 /* A full vector. */
5680 if ((opcode->membership & INSN_5400)
5681 && (strcmp (opcode->name, "sll.ob") == 0
5682 || strcmp (opcode->name, "srl.ob") == 0))
5683 {
1a00e612
RS
5684 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5685 arg->argnum);
5686 return FALSE;
a1d78564
RS
5687 }
5688
5689 if (is_qh)
5690 uval |= MDMX_FMTSEL_VEC_QH << 5;
5691 else
5692 uval |= MDMX_FMTSEL_VEC_OB << 5;
5693 }
a1d78564
RS
5694 uval |= regno;
5695 }
5696 else
5697 {
5698 offsetT sval;
5699
1a00e612 5700 if (!match_const_int (arg, &sval))
a92713e6 5701 return FALSE;
a1d78564
RS
5702 if (sval < 0 || sval > 31)
5703 {
1a00e612
RS
5704 match_out_of_range (arg);
5705 return FALSE;
a1d78564
RS
5706 }
5707 uval |= (sval & 31);
5708 if (is_qh)
5709 uval |= MDMX_FMTSEL_IMM_QH << 5;
5710 else
5711 uval |= MDMX_FMTSEL_IMM_OB << 5;
5712 }
5713 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5714 return TRUE;
a1d78564
RS
5715}
5716
56d438b1
CF
5717/* OP_IMM_INDEX matcher. */
5718
5719static bfd_boolean
5720match_imm_index_operand (struct mips_arg_info *arg,
5721 const struct mips_operand *operand)
5722{
5723 unsigned int max_val;
5724
5725 if (arg->token->type != OT_INTEGER_INDEX)
5726 return FALSE;
5727
5728 max_val = (1 << operand->size) - 1;
5729 if (arg->token->u.index > max_val)
5730 {
5731 match_out_of_range (arg);
5732 return FALSE;
5733 }
5734 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5735 ++arg->token;
5736 return TRUE;
5737}
5738
5739/* OP_REG_INDEX matcher. */
5740
5741static bfd_boolean
5742match_reg_index_operand (struct mips_arg_info *arg,
5743 const struct mips_operand *operand)
5744{
5745 unsigned int regno;
5746
5747 if (arg->token->type != OT_REG_INDEX)
5748 return FALSE;
5749
5750 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5751 return FALSE;
5752
5753 insn_insert_operand (arg->insn, operand, regno);
5754 ++arg->token;
5755 return TRUE;
5756}
5757
a1d78564
RS
5758/* OP_PC matcher. */
5759
a92713e6
RS
5760static bfd_boolean
5761match_pc_operand (struct mips_arg_info *arg)
a1d78564 5762{
a92713e6
RS
5763 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5764 {
5765 ++arg->token;
5766 return TRUE;
5767 }
5768 return FALSE;
a1d78564
RS
5769}
5770
7361da2c
AB
5771/* OP_NON_ZERO_REG matcher. */
5772
5773static bfd_boolean
5774match_non_zero_reg_operand (struct mips_arg_info *arg,
5775 const struct mips_operand *operand)
5776{
5777 unsigned int regno;
5778
5779 if (!match_reg (arg, OP_REG_GP, &regno))
5780 return FALSE;
5781
5782 if (regno == 0)
5783 return FALSE;
5784
5785 arg->last_regno = regno;
5786 insn_insert_operand (arg->insn, operand, regno);
5787 return TRUE;
5788}
5789
a1d78564
RS
5790/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5791 register that we need to match. */
5792
a92713e6
RS
5793static bfd_boolean
5794match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5795{
5796 unsigned int regno;
5797
a92713e6 5798 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5799}
5800
89565f1b
RS
5801/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5802 the length of the value in bytes (4 for float, 8 for double) and
5803 USING_GPRS says whether the destination is a GPR rather than an FPR.
5804
5805 Return the constant in IMM and OFFSET as follows:
5806
5807 - If the constant should be loaded via memory, set IMM to O_absent and
5808 OFFSET to the memory address.
5809
5810 - Otherwise, if the constant should be loaded into two 32-bit registers,
5811 set IMM to the O_constant to load into the high register and OFFSET
5812 to the corresponding value for the low register.
5813
5814 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5815
5816 These constants only appear as the last operand in an instruction,
5817 and every instruction that accepts them in any variant accepts them
5818 in all variants. This means we don't have to worry about backing out
5819 any changes if the instruction does not match. We just match
5820 unconditionally and report an error if the constant is invalid. */
5821
a92713e6
RS
5822static bfd_boolean
5823match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5824 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5825{
a92713e6 5826 char *p;
89565f1b
RS
5827 segT seg, new_seg;
5828 subsegT subseg;
5829 const char *newname;
a92713e6 5830 unsigned char *data;
89565f1b
RS
5831
5832 /* Where the constant is placed is based on how the MIPS assembler
5833 does things:
5834
5835 length == 4 && using_gprs -- immediate value only
5836 length == 8 && using_gprs -- .rdata or immediate value
5837 length == 4 && !using_gprs -- .lit4 or immediate value
5838 length == 8 && !using_gprs -- .lit8 or immediate value
5839
5840 The .lit4 and .lit8 sections are only used if permitted by the
5841 -G argument. */
a92713e6 5842 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5843 {
5844 set_insn_error (arg->argnum, _("floating-point expression required"));
5845 return FALSE;
5846 }
a92713e6
RS
5847
5848 gas_assert (arg->token->u.flt.length == length);
5849 data = arg->token->u.flt.data;
5850 ++arg->token;
89565f1b
RS
5851
5852 /* Handle 32-bit constants for which an immediate value is best. */
5853 if (length == 4
5854 && (using_gprs
5855 || g_switch_value < 4
5856 || (data[0] == 0 && data[1] == 0)
5857 || (data[2] == 0 && data[3] == 0)))
5858 {
5859 imm->X_op = O_constant;
5860 if (!target_big_endian)
5861 imm->X_add_number = bfd_getl32 (data);
5862 else
5863 imm->X_add_number = bfd_getb32 (data);
5864 offset->X_op = O_absent;
a92713e6 5865 return TRUE;
89565f1b
RS
5866 }
5867
5868 /* Handle 64-bit constants for which an immediate value is best. */
5869 if (length == 8
5870 && !mips_disable_float_construction
351cdf24
MF
5871 /* Constants can only be constructed in GPRs and copied to FPRs if the
5872 GPRs are at least as wide as the FPRs or MTHC1 is available.
5873 Unlike most tests for 32-bit floating-point registers this check
5874 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5875 permit 64-bit moves without MXHC1.
5876 Force the constant into memory otherwise. */
5877 && (using_gprs
5878 || GPR_SIZE == 64
5879 || ISA_HAS_MXHC1 (mips_opts.isa)
5880 || FPR_SIZE == 32)
89565f1b
RS
5881 && ((data[0] == 0 && data[1] == 0)
5882 || (data[2] == 0 && data[3] == 0))
5883 && ((data[4] == 0 && data[5] == 0)
5884 || (data[6] == 0 && data[7] == 0)))
5885 {
5886 /* The value is simple enough to load with a couple of instructions.
5887 If using 32-bit registers, set IMM to the high order 32 bits and
5888 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5889 64 bit constant. */
351cdf24 5890 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
5891 {
5892 imm->X_op = O_constant;
5893 offset->X_op = O_constant;
5894 if (!target_big_endian)
5895 {
5896 imm->X_add_number = bfd_getl32 (data + 4);
5897 offset->X_add_number = bfd_getl32 (data);
5898 }
5899 else
5900 {
5901 imm->X_add_number = bfd_getb32 (data);
5902 offset->X_add_number = bfd_getb32 (data + 4);
5903 }
5904 if (offset->X_add_number == 0)
5905 offset->X_op = O_absent;
5906 }
5907 else
5908 {
5909 imm->X_op = O_constant;
5910 if (!target_big_endian)
5911 imm->X_add_number = bfd_getl64 (data);
5912 else
5913 imm->X_add_number = bfd_getb64 (data);
5914 offset->X_op = O_absent;
5915 }
a92713e6 5916 return TRUE;
89565f1b
RS
5917 }
5918
5919 /* Switch to the right section. */
5920 seg = now_seg;
5921 subseg = now_subseg;
5922 if (length == 4)
5923 {
5924 gas_assert (!using_gprs && g_switch_value >= 4);
5925 newname = ".lit4";
5926 }
5927 else
5928 {
5929 if (using_gprs || g_switch_value < 8)
5930 newname = RDATA_SECTION_NAME;
5931 else
5932 newname = ".lit8";
5933 }
5934
5935 new_seg = subseg_new (newname, (subsegT) 0);
5936 bfd_set_section_flags (stdoutput, new_seg,
5937 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5938 frag_align (length == 4 ? 2 : 3, 0, 0);
5939 if (strncmp (TARGET_OS, "elf", 3) != 0)
5940 record_alignment (new_seg, 4);
5941 else
5942 record_alignment (new_seg, length == 4 ? 2 : 3);
5943 if (seg == now_seg)
1661c76c 5944 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
5945
5946 /* Set the argument to the current address in the section. */
5947 imm->X_op = O_absent;
5948 offset->X_op = O_symbol;
5949 offset->X_add_symbol = symbol_temp_new_now ();
5950 offset->X_add_number = 0;
5951
5952 /* Put the floating point number into the section. */
5953 p = frag_more (length);
5954 memcpy (p, data, length);
5955
5956 /* Switch back to the original section. */
5957 subseg_set (seg, subseg);
a92713e6 5958 return TRUE;
89565f1b
RS
5959}
5960
14daeee3
RS
5961/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5962 them. */
5963
5964static bfd_boolean
5965match_vu0_suffix_operand (struct mips_arg_info *arg,
5966 const struct mips_operand *operand,
5967 bfd_boolean match_p)
5968{
5969 unsigned int uval;
5970
5971 /* The operand can be an XYZW mask or a single 2-bit channel index
5972 (with X being 0). */
5973 gas_assert (operand->size == 2 || operand->size == 4);
5974
ee5734f0 5975 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5976 if (arg->token->type != OT_CHANNELS)
ee5734f0 5977 return match_p;
14daeee3
RS
5978
5979 uval = arg->token->u.channels;
5980 if (operand->size == 2)
5981 {
5982 /* Check that a single bit is set and convert it into a 2-bit index. */
5983 if ((uval & -uval) != uval)
5984 return FALSE;
5985 uval = 4 - ffs (uval);
5986 }
5987
5988 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5989 return FALSE;
5990
5991 ++arg->token;
5992 if (!match_p)
5993 insn_insert_operand (arg->insn, operand, uval);
5994 return TRUE;
5995}
5996
a1d78564
RS
5997/* S is the text seen for ARG. Match it against OPERAND. Return the end
5998 of the argument text if the match is successful, otherwise return null. */
5999
a92713e6 6000static bfd_boolean
a1d78564 6001match_operand (struct mips_arg_info *arg,
a92713e6 6002 const struct mips_operand *operand)
a1d78564
RS
6003{
6004 switch (operand->type)
6005 {
6006 case OP_INT:
a92713e6 6007 return match_int_operand (arg, operand);
a1d78564
RS
6008
6009 case OP_MAPPED_INT:
a92713e6 6010 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6011
6012 case OP_MSB:
a92713e6 6013 return match_msb_operand (arg, operand);
a1d78564
RS
6014
6015 case OP_REG:
0f35dbc4 6016 case OP_OPTIONAL_REG:
a92713e6 6017 return match_reg_operand (arg, operand);
a1d78564
RS
6018
6019 case OP_REG_PAIR:
a92713e6 6020 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6021
6022 case OP_PCREL:
a92713e6 6023 return match_pcrel_operand (arg);
a1d78564
RS
6024
6025 case OP_PERF_REG:
a92713e6 6026 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6027
6028 case OP_ADDIUSP_INT:
a92713e6 6029 return match_addiusp_operand (arg, operand);
a1d78564
RS
6030
6031 case OP_CLO_CLZ_DEST:
a92713e6 6032 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6033
6034 case OP_LWM_SWM_LIST:
a92713e6 6035 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6036
6037 case OP_ENTRY_EXIT_LIST:
a92713e6 6038 return match_entry_exit_operand (arg, operand);
364215c8 6039
a1d78564 6040 case OP_SAVE_RESTORE_LIST:
a92713e6 6041 return match_save_restore_list_operand (arg);
a1d78564
RS
6042
6043 case OP_MDMX_IMM_REG:
a92713e6 6044 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6045
6046 case OP_REPEAT_DEST_REG:
a92713e6 6047 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6048
6049 case OP_REPEAT_PREV_REG:
a92713e6 6050 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6051
6052 case OP_PC:
a92713e6 6053 return match_pc_operand (arg);
14daeee3
RS
6054
6055 case OP_VU0_SUFFIX:
6056 return match_vu0_suffix_operand (arg, operand, FALSE);
6057
6058 case OP_VU0_MATCH_SUFFIX:
6059 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6060
6061 case OP_IMM_INDEX:
6062 return match_imm_index_operand (arg, operand);
6063
6064 case OP_REG_INDEX:
6065 return match_reg_index_operand (arg, operand);
7361da2c
AB
6066
6067 case OP_SAME_RS_RT:
6068 return match_same_rs_rt_operand (arg, operand);
6069
6070 case OP_CHECK_PREV:
6071 return match_check_prev_operand (arg, operand);
6072
6073 case OP_NON_ZERO_REG:
6074 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6075 }
6076 abort ();
6077}
6078
6079/* ARG is the state after successfully matching an instruction.
6080 Issue any queued-up warnings. */
6081
6082static void
6083check_completed_insn (struct mips_arg_info *arg)
6084{
6085 if (arg->seen_at)
6086 {
6087 if (AT == ATREG)
1661c76c 6088 as_warn (_("used $at without \".set noat\""));
a1d78564 6089 else
1661c76c 6090 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6091 }
6092}
a1d78564 6093
85fcb30f
RS
6094/* Return true if modifying general-purpose register REG needs a delay. */
6095
6096static bfd_boolean
6097reg_needs_delay (unsigned int reg)
6098{
6099 unsigned long prev_pinfo;
6100
6101 prev_pinfo = history[0].insn_mo->pinfo;
6102 if (!mips_opts.noreorder
67dc82bc 6103 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6104 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6105 && (gpr_write_mask (&history[0]) & (1 << reg)))
6106 return TRUE;
6107
6108 return FALSE;
6109}
6110
71400594
RS
6111/* Classify an instruction according to the FIX_VR4120_* enumeration.
6112 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6113 by VR4120 errata. */
4d7206a2 6114
71400594
RS
6115static unsigned int
6116classify_vr4120_insn (const char *name)
252b5132 6117{
71400594
RS
6118 if (strncmp (name, "macc", 4) == 0)
6119 return FIX_VR4120_MACC;
6120 if (strncmp (name, "dmacc", 5) == 0)
6121 return FIX_VR4120_DMACC;
6122 if (strncmp (name, "mult", 4) == 0)
6123 return FIX_VR4120_MULT;
6124 if (strncmp (name, "dmult", 5) == 0)
6125 return FIX_VR4120_DMULT;
6126 if (strstr (name, "div"))
6127 return FIX_VR4120_DIV;
6128 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6129 return FIX_VR4120_MTHILO;
6130 return NUM_FIX_VR4120_CLASSES;
6131}
252b5132 6132
a8d14a88
CM
6133#define INSN_ERET 0x42000018
6134#define INSN_DERET 0x4200001f
6135#define INSN_DMULT 0x1c
6136#define INSN_DMULTU 0x1d
ff239038 6137
71400594
RS
6138/* Return the number of instructions that must separate INSN1 and INSN2,
6139 where INSN1 is the earlier instruction. Return the worst-case value
6140 for any INSN2 if INSN2 is null. */
252b5132 6141
71400594
RS
6142static unsigned int
6143insns_between (const struct mips_cl_insn *insn1,
6144 const struct mips_cl_insn *insn2)
6145{
6146 unsigned long pinfo1, pinfo2;
4c260379 6147 unsigned int mask;
71400594 6148
85fcb30f
RS
6149 /* If INFO2 is null, pessimistically assume that all flags are set for
6150 the second instruction. */
71400594
RS
6151 pinfo1 = insn1->insn_mo->pinfo;
6152 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6153
71400594
RS
6154 /* For most targets, write-after-read dependencies on the HI and LO
6155 registers must be separated by at least two instructions. */
6156 if (!hilo_interlocks)
252b5132 6157 {
71400594
RS
6158 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6159 return 2;
6160 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6161 return 2;
6162 }
6163
6164 /* If we're working around r7000 errata, there must be two instructions
6165 between an mfhi or mflo and any instruction that uses the result. */
6166 if (mips_7000_hilo_fix
df58fc94 6167 && !mips_opts.micromips
71400594 6168 && MF_HILO_INSN (pinfo1)
85fcb30f 6169 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6170 return 2;
6171
ff239038
CM
6172 /* If we're working around 24K errata, one instruction is required
6173 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6174 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6175 {
6176 if (insn1->insn_opcode == INSN_ERET
6177 || insn1->insn_opcode == INSN_DERET)
6178 {
6179 if (insn2 == NULL
6180 || insn2->insn_opcode == INSN_ERET
6181 || insn2->insn_opcode == INSN_DERET
11625dd8 6182 || delayed_branch_p (insn2))
ff239038
CM
6183 return 1;
6184 }
6185 }
6186
a8d14a88
CM
6187 /* If we're working around PMC RM7000 errata, there must be three
6188 nops between a dmult and a load instruction. */
6189 if (mips_fix_rm7000 && !mips_opts.micromips)
6190 {
6191 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6192 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6193 {
6194 if (pinfo2 & INSN_LOAD_MEMORY)
6195 return 3;
6196 }
6197 }
6198
71400594
RS
6199 /* If working around VR4120 errata, check for combinations that need
6200 a single intervening instruction. */
df58fc94 6201 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6202 {
6203 unsigned int class1, class2;
252b5132 6204
71400594
RS
6205 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6206 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6207 {
71400594
RS
6208 if (insn2 == NULL)
6209 return 1;
6210 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6211 if (vr4120_conflicts[class1] & (1 << class2))
6212 return 1;
252b5132 6213 }
71400594
RS
6214 }
6215
df58fc94 6216 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6217 {
6218 /* Check for GPR or coprocessor load delays. All such delays
6219 are on the RT register. */
6220 /* Itbl support may require additional care here. */
67dc82bc 6221 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6222 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6223 {
85fcb30f 6224 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6225 return 1;
6226 }
6227
6228 /* Check for generic coprocessor hazards.
6229
6230 This case is not handled very well. There is no special
6231 knowledge of CP0 handling, and the coprocessors other than
6232 the floating point unit are not distinguished at all. */
6233 /* Itbl support may require additional care here. FIXME!
6234 Need to modify this to include knowledge about
6235 user specified delays! */
43885403 6236 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6237 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6238 {
6239 /* Handle cases where INSN1 writes to a known general coprocessor
6240 register. There must be a one instruction delay before INSN2
6241 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6242 mask = fpr_write_mask (insn1);
6243 if (mask != 0)
252b5132 6244 {
4c260379 6245 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6246 return 1;
252b5132
RH
6247 }
6248 else
6249 {
71400594
RS
6250 /* Read-after-write dependencies on the control registers
6251 require a two-instruction gap. */
6252 if ((pinfo1 & INSN_WRITE_COND_CODE)
6253 && (pinfo2 & INSN_READ_COND_CODE))
6254 return 2;
6255
6256 /* We don't know exactly what INSN1 does. If INSN2 is
6257 also a coprocessor instruction, assume there must be
6258 a one instruction gap. */
6259 if (pinfo2 & INSN_COP)
6260 return 1;
252b5132
RH
6261 }
6262 }
6b76fefe 6263
71400594
RS
6264 /* Check for read-after-write dependencies on the coprocessor
6265 control registers in cases where INSN1 does not need a general
6266 coprocessor delay. This means that INSN1 is a floating point
6267 comparison instruction. */
6268 /* Itbl support may require additional care here. */
6269 else if (!cop_interlocks
6270 && (pinfo1 & INSN_WRITE_COND_CODE)
6271 && (pinfo2 & INSN_READ_COND_CODE))
6272 return 1;
6273 }
6b76fefe 6274
7361da2c
AB
6275 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6276 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6277 and pause. */
6278 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6279 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6280 || (insn2 && delayed_branch_p (insn2))))
6281 return 1;
6282
71400594
RS
6283 return 0;
6284}
6b76fefe 6285
7d8e00cf
RS
6286/* Return the number of nops that would be needed to work around the
6287 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6288 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6289 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6290
6291static int
932d1a1b 6292nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6293 const struct mips_cl_insn *insn)
6294{
4c260379
RS
6295 int i, j;
6296 unsigned int mask;
7d8e00cf
RS
6297
6298 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6299 are not affected by the errata. */
6300 if (insn != 0
6301 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6302 || strcmp (insn->insn_mo->name, "mtlo") == 0
6303 || strcmp (insn->insn_mo->name, "mthi") == 0))
6304 return 0;
6305
6306 /* Search for the first MFLO or MFHI. */
6307 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6308 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6309 {
6310 /* Extract the destination register. */
4c260379 6311 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6312
6313 /* No nops are needed if INSN reads that register. */
4c260379 6314 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6315 return 0;
6316
6317 /* ...or if any of the intervening instructions do. */
6318 for (j = 0; j < i; j++)
4c260379 6319 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6320 return 0;
6321
932d1a1b
RS
6322 if (i >= ignore)
6323 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6324 }
6325 return 0;
6326}
6327
134c0c8b
MR
6328#define BASE_REG_EQ(INSN1, INSN2) \
6329 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6330 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6331
6332/* Return the minimum alignment for this store instruction. */
6333
6334static int
6335fix_24k_align_to (const struct mips_opcode *mo)
6336{
6337 if (strcmp (mo->name, "sh") == 0)
6338 return 2;
6339
6340 if (strcmp (mo->name, "swc1") == 0
6341 || strcmp (mo->name, "swc2") == 0
6342 || strcmp (mo->name, "sw") == 0
6343 || strcmp (mo->name, "sc") == 0
6344 || strcmp (mo->name, "s.s") == 0)
6345 return 4;
6346
6347 if (strcmp (mo->name, "sdc1") == 0
6348 || strcmp (mo->name, "sdc2") == 0
6349 || strcmp (mo->name, "s.d") == 0)
6350 return 8;
6351
6352 /* sb, swl, swr */
6353 return 1;
6354}
6355
6356struct fix_24k_store_info
6357 {
6358 /* Immediate offset, if any, for this store instruction. */
6359 short off;
6360 /* Alignment required by this store instruction. */
6361 int align_to;
6362 /* True for register offsets. */
6363 int register_offset;
6364 };
6365
6366/* Comparison function used by qsort. */
6367
6368static int
6369fix_24k_sort (const void *a, const void *b)
6370{
6371 const struct fix_24k_store_info *pos1 = a;
6372 const struct fix_24k_store_info *pos2 = b;
6373
6374 return (pos1->off - pos2->off);
6375}
6376
6377/* INSN is a store instruction. Try to record the store information
6378 in STINFO. Return false if the information isn't known. */
6379
6380static bfd_boolean
6381fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6382 const struct mips_cl_insn *insn)
15be625d
CM
6383{
6384 /* The instruction must have a known offset. */
6385 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6386 return FALSE;
6387
6388 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6389 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6390 return TRUE;
6391}
6392
932d1a1b
RS
6393/* Return the number of nops that would be needed to work around the 24k
6394 "lost data on stores during refill" errata if instruction INSN
6395 immediately followed the 2 instructions described by HIST.
6396 Ignore hazards that are contained within the first IGNORE
6397 instructions of HIST.
6398
6399 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6400 for the data cache refills and store data. The following describes
6401 the scenario where the store data could be lost.
6402
6403 * A data cache miss, due to either a load or a store, causing fill
6404 data to be supplied by the memory subsystem
6405 * The first three doublewords of fill data are returned and written
6406 into the cache
6407 * A sequence of four stores occurs in consecutive cycles around the
6408 final doubleword of the fill:
6409 * Store A
6410 * Store B
6411 * Store C
6412 * Zero, One or more instructions
6413 * Store D
6414
6415 The four stores A-D must be to different doublewords of the line that
6416 is being filled. The fourth instruction in the sequence above permits
6417 the fill of the final doubleword to be transferred from the FSB into
6418 the cache. In the sequence above, the stores may be either integer
6419 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6420 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6421 different doublewords on the line. If the floating point unit is
6422 running in 1:2 mode, it is not possible to create the sequence above
6423 using only floating point store instructions.
15be625d
CM
6424
6425 In this case, the cache line being filled is incorrectly marked
6426 invalid, thereby losing the data from any store to the line that
6427 occurs between the original miss and the completion of the five
6428 cycle sequence shown above.
6429
932d1a1b 6430 The workarounds are:
15be625d 6431
932d1a1b
RS
6432 * Run the data cache in write-through mode.
6433 * Insert a non-store instruction between
6434 Store A and Store B or Store B and Store C. */
3739860c 6435
15be625d 6436static int
932d1a1b 6437nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6438 const struct mips_cl_insn *insn)
6439{
6440 struct fix_24k_store_info pos[3];
6441 int align, i, base_offset;
6442
932d1a1b
RS
6443 if (ignore >= 2)
6444 return 0;
6445
ab9794cf
RS
6446 /* If the previous instruction wasn't a store, there's nothing to
6447 worry about. */
15be625d
CM
6448 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6449 return 0;
6450
ab9794cf
RS
6451 /* If the instructions after the previous one are unknown, we have
6452 to assume the worst. */
6453 if (!insn)
15be625d
CM
6454 return 1;
6455
ab9794cf
RS
6456 /* Check whether we are dealing with three consecutive stores. */
6457 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6458 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6459 return 0;
6460
6461 /* If we don't know the relationship between the store addresses,
6462 assume the worst. */
ab9794cf 6463 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6464 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6465 return 1;
6466
6467 if (!fix_24k_record_store_info (&pos[0], insn)
6468 || !fix_24k_record_store_info (&pos[1], &hist[0])
6469 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6470 return 1;
6471
6472 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6473
6474 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6475 X bytes and such that the base register + X is known to be aligned
6476 to align bytes. */
6477
6478 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6479 align = 8;
6480 else
6481 {
6482 align = pos[0].align_to;
6483 base_offset = pos[0].off;
6484 for (i = 1; i < 3; i++)
6485 if (align < pos[i].align_to)
6486 {
6487 align = pos[i].align_to;
6488 base_offset = pos[i].off;
6489 }
6490 for (i = 0; i < 3; i++)
6491 pos[i].off -= base_offset;
6492 }
6493
6494 pos[0].off &= ~align + 1;
6495 pos[1].off &= ~align + 1;
6496 pos[2].off &= ~align + 1;
6497
6498 /* If any two stores write to the same chunk, they also write to the
6499 same doubleword. The offsets are still sorted at this point. */
6500 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6501 return 0;
6502
6503 /* A range of at least 9 bytes is needed for the stores to be in
6504 non-overlapping doublewords. */
6505 if (pos[2].off - pos[0].off <= 8)
6506 return 0;
6507
6508 if (pos[2].off - pos[1].off >= 24
6509 || pos[1].off - pos[0].off >= 24
6510 || pos[2].off - pos[0].off >= 32)
6511 return 0;
6512
6513 return 1;
6514}
6515
71400594 6516/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6517 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6518 where HIST[0] is the most recent instruction. Ignore hazards
6519 between INSN and the first IGNORE instructions in HIST.
6520
6521 If INSN is null, return the worse-case number of nops for any
6522 instruction. */
bdaaa2e1 6523
71400594 6524static int
932d1a1b 6525nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6526 const struct mips_cl_insn *insn)
6527{
6528 int i, nops, tmp_nops;
bdaaa2e1 6529
71400594 6530 nops = 0;
932d1a1b 6531 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6532 {
91d6fa6a 6533 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6534 if (tmp_nops > nops)
6535 nops = tmp_nops;
6536 }
7d8e00cf 6537
df58fc94 6538 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6539 {
932d1a1b 6540 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6541 if (tmp_nops > nops)
6542 nops = tmp_nops;
6543 }
6544
df58fc94 6545 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6546 {
932d1a1b 6547 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6548 if (tmp_nops > nops)
6549 nops = tmp_nops;
6550 }
6551
71400594
RS
6552 return nops;
6553}
252b5132 6554
71400594 6555/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6556 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6557 would be needed after the extended sequence, ignoring hazards
6558 in the first IGNORE instructions. */
252b5132 6559
71400594 6560static int
932d1a1b
RS
6561nops_for_sequence (int num_insns, int ignore,
6562 const struct mips_cl_insn *hist, ...)
71400594
RS
6563{
6564 va_list args;
6565 struct mips_cl_insn buffer[MAX_NOPS];
6566 struct mips_cl_insn *cursor;
6567 int nops;
6568
91d6fa6a 6569 va_start (args, hist);
71400594 6570 cursor = buffer + num_insns;
91d6fa6a 6571 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6572 while (cursor > buffer)
6573 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6574
932d1a1b 6575 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6576 va_end (args);
6577 return nops;
6578}
252b5132 6579
71400594
RS
6580/* Like nops_for_insn, but if INSN is a branch, take into account the
6581 worst-case delay for the branch target. */
252b5132 6582
71400594 6583static int
932d1a1b 6584nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6585 const struct mips_cl_insn *insn)
6586{
6587 int nops, tmp_nops;
60b63b72 6588
932d1a1b 6589 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6590 if (delayed_branch_p (insn))
71400594 6591 {
932d1a1b 6592 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6593 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6594 if (tmp_nops > nops)
6595 nops = tmp_nops;
6596 }
11625dd8 6597 else if (compact_branch_p (insn))
71400594 6598 {
932d1a1b 6599 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6600 if (tmp_nops > nops)
6601 nops = tmp_nops;
6602 }
6603 return nops;
6604}
6605
c67a084a
NC
6606/* Fix NOP issue: Replace nops by "or at,at,zero". */
6607
6608static void
6609fix_loongson2f_nop (struct mips_cl_insn * ip)
6610{
df58fc94 6611 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6612 if (strcmp (ip->insn_mo->name, "nop") == 0)
6613 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6614}
6615
6616/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6617 jr target pc &= 'hffff_ffff_cfff_ffff. */
6618
6619static void
6620fix_loongson2f_jump (struct mips_cl_insn * ip)
6621{
df58fc94 6622 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6623 if (strcmp (ip->insn_mo->name, "j") == 0
6624 || strcmp (ip->insn_mo->name, "jr") == 0
6625 || strcmp (ip->insn_mo->name, "jalr") == 0)
6626 {
6627 int sreg;
6628 expressionS ep;
6629
6630 if (! mips_opts.at)
6631 return;
6632
df58fc94 6633 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6634 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6635 return;
6636
6637 ep.X_op = O_constant;
6638 ep.X_add_number = 0xcfff0000;
6639 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6640 ep.X_add_number = 0xffff;
6641 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6642 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6643 }
6644}
6645
6646static void
6647fix_loongson2f (struct mips_cl_insn * ip)
6648{
6649 if (mips_fix_loongson2f_nop)
6650 fix_loongson2f_nop (ip);
6651
6652 if (mips_fix_loongson2f_jump)
6653 fix_loongson2f_jump (ip);
6654}
6655
a4e06468
RS
6656/* IP is a branch that has a delay slot, and we need to fill it
6657 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6658 with the previous instruction.
6659 ADDRESS_EXPR is an operand of the instruction to be used with
6660 RELOC_TYPE. */
a4e06468
RS
6661
6662static bfd_boolean
e407c74b 6663can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6664 bfd_reloc_code_real_type *reloc_type)
a4e06468 6665{
2b0c8b40 6666 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6667 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6668 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6669
6670 /* -O2 and above is required for this optimization. */
6671 if (mips_optimize < 2)
6672 return FALSE;
6673
6674 /* If we have seen .set volatile or .set nomove, don't optimize. */
6675 if (mips_opts.nomove)
6676 return FALSE;
6677
6678 /* We can't swap if the previous instruction's position is fixed. */
6679 if (history[0].fixed_p)
6680 return FALSE;
6681
6682 /* If the previous previous insn was in a .set noreorder, we can't
6683 swap. Actually, the MIPS assembler will swap in this situation.
6684 However, gcc configured -with-gnu-as will generate code like
6685
6686 .set noreorder
6687 lw $4,XXX
6688 .set reorder
6689 INSN
6690 bne $4,$0,foo
6691
6692 in which we can not swap the bne and INSN. If gcc is not configured
6693 -with-gnu-as, it does not output the .set pseudo-ops. */
6694 if (history[1].noreorder_p)
6695 return FALSE;
6696
87333bb7
MR
6697 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6698 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6699 if (mips_opts.mips16 && history[0].fixp[0])
6700 return FALSE;
6701
6702 /* If the branch is itself the target of a branch, we can not swap.
6703 We cheat on this; all we check for is whether there is a label on
6704 this instruction. If there are any branches to anything other than
6705 a label, users must use .set noreorder. */
6706 if (seg_info (now_seg)->label_list)
6707 return FALSE;
6708
6709 /* If the previous instruction is in a variant frag other than this
2309ddf2 6710 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6711 MIPS16 code, which uses variant frags for different purposes. */
6712 if (!mips_opts.mips16
a4e06468
RS
6713 && history[0].frag
6714 && history[0].frag->fr_type == rs_machine_dependent)
6715 return FALSE;
6716
bcd530a7
RS
6717 /* We do not swap with instructions that cannot architecturally
6718 be placed in a branch delay slot, such as SYNC or ERET. We
6719 also refrain from swapping with a trap instruction, since it
6720 complicates trap handlers to have the trap instruction be in
6721 a delay slot. */
a4e06468 6722 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6723 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6724 return FALSE;
6725
6726 /* Check for conflicts between the branch and the instructions
6727 before the candidate delay slot. */
6728 if (nops_for_insn (0, history + 1, ip) > 0)
6729 return FALSE;
6730
6731 /* Check for conflicts between the swapped sequence and the
6732 target of the branch. */
6733 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6734 return FALSE;
6735
6736 /* If the branch reads a register that the previous
6737 instruction sets, we can not swap. */
6738 gpr_read = gpr_read_mask (ip);
6739 prev_gpr_write = gpr_write_mask (&history[0]);
6740 if (gpr_read & prev_gpr_write)
6741 return FALSE;
6742
9d5de888
CF
6743 fpr_read = fpr_read_mask (ip);
6744 prev_fpr_write = fpr_write_mask (&history[0]);
6745 if (fpr_read & prev_fpr_write)
6746 return FALSE;
6747
a4e06468
RS
6748 /* If the branch writes a register that the previous
6749 instruction sets, we can not swap. */
6750 gpr_write = gpr_write_mask (ip);
6751 if (gpr_write & prev_gpr_write)
6752 return FALSE;
6753
6754 /* If the branch writes a register that the previous
6755 instruction reads, we can not swap. */
6756 prev_gpr_read = gpr_read_mask (&history[0]);
6757 if (gpr_write & prev_gpr_read)
6758 return FALSE;
6759
6760 /* If one instruction sets a condition code and the
6761 other one uses a condition code, we can not swap. */
6762 pinfo = ip->insn_mo->pinfo;
6763 if ((pinfo & INSN_READ_COND_CODE)
6764 && (prev_pinfo & INSN_WRITE_COND_CODE))
6765 return FALSE;
6766 if ((pinfo & INSN_WRITE_COND_CODE)
6767 && (prev_pinfo & INSN_READ_COND_CODE))
6768 return FALSE;
6769
6770 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6771 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6772 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6773 return FALSE;
a4e06468 6774
df58fc94
RS
6775 /* If the previous instruction has an incorrect size for a fixed
6776 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6777 pinfo2 = ip->insn_mo->pinfo2;
6778 if (mips_opts.micromips
6779 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6780 && insn_length (history) != 2)
6781 return FALSE;
6782 if (mips_opts.micromips
6783 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6784 && insn_length (history) != 4)
6785 return FALSE;
6786
e407c74b
NC
6787 /* On R5900 short loops need to be fixed by inserting a nop in
6788 the branch delay slots.
6789 A short loop can be terminated too early. */
6790 if (mips_opts.arch == CPU_R5900
6791 /* Check if instruction has a parameter, ignore "j $31". */
6792 && (address_expr != NULL)
6793 /* Parameter must be 16 bit. */
6794 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6795 /* Branch to same segment. */
41065f5e 6796 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6797 /* Branch to same code fragment. */
41065f5e 6798 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6799 /* Can only calculate branch offset if value is known. */
41065f5e 6800 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6801 /* Check if branch is really conditional. */
6802 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6803 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6804 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6805 {
6806 int distance;
6807 /* Check if loop is shorter than 6 instructions including
6808 branch and delay slot. */
41065f5e 6809 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6810 if (distance <= 20)
6811 {
6812 int i;
6813 int rv;
6814
6815 rv = FALSE;
6816 /* When the loop includes branches or jumps,
6817 it is not a short loop. */
6818 for (i = 0; i < (distance / 4); i++)
6819 {
6820 if ((history[i].cleared_p)
41065f5e 6821 || delayed_branch_p (&history[i]))
e407c74b
NC
6822 {
6823 rv = TRUE;
6824 break;
6825 }
6826 }
6827 if (rv == FALSE)
6828 {
6829 /* Insert nop after branch to fix short loop. */
6830 return FALSE;
6831 }
6832 }
6833 }
6834
a4e06468
RS
6835 return TRUE;
6836}
6837
e407c74b
NC
6838/* Decide how we should add IP to the instruction stream.
6839 ADDRESS_EXPR is an operand of the instruction to be used with
6840 RELOC_TYPE. */
a4e06468
RS
6841
6842static enum append_method
e407c74b 6843get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6844 bfd_reloc_code_real_type *reloc_type)
a4e06468 6845{
a4e06468
RS
6846 /* The relaxed version of a macro sequence must be inherently
6847 hazard-free. */
6848 if (mips_relax.sequence == 2)
6849 return APPEND_ADD;
6850
3b821a28 6851 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
6852 if (mips_opts.noreorder)
6853 return APPEND_ADD;
6854
6855 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6856 if (delayed_branch_p (ip))
a4e06468 6857 {
e407c74b
NC
6858 if (!branch_likely_p (ip)
6859 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6860 return APPEND_SWAP;
6861
6862 if (mips_opts.mips16
6863 && ISA_SUPPORTS_MIPS16E
fc76e730 6864 && gpr_read_mask (ip) != 0)
a4e06468
RS
6865 return APPEND_ADD_COMPACT;
6866
7bd374a4
MR
6867 if (mips_opts.micromips
6868 && ((ip->insn_opcode & 0xffe0) == 0x4580
6869 || (!forced_insn_length
6870 && ((ip->insn_opcode & 0xfc00) == 0xcc00
6871 || (ip->insn_opcode & 0xdc00) == 0x8c00))
6872 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
6873 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
6874 return APPEND_ADD_COMPACT;
6875
a4e06468
RS
6876 return APPEND_ADD_WITH_NOP;
6877 }
6878
a4e06468
RS
6879 return APPEND_ADD;
6880}
6881
7bd374a4
MR
6882/* IP is an instruction whose opcode we have just changed, END points
6883 to the end of the opcode table processed. Point IP->insn_mo to the
6884 new opcode's definition. */
ceb94aa5
RS
6885
6886static void
7bd374a4 6887find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 6888{
7bd374a4 6889 const struct mips_opcode *mo;
ceb94aa5 6890
ceb94aa5 6891 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
6892 if (mo->pinfo != INSN_MACRO
6893 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
6894 {
6895 ip->insn_mo = mo;
6896 return;
6897 }
6898 abort ();
6899}
6900
7bd374a4
MR
6901/* IP is a MIPS16 instruction whose opcode we have just changed.
6902 Point IP->insn_mo to the new opcode's definition. */
6903
6904static void
6905find_altered_mips16_opcode (struct mips_cl_insn *ip)
6906{
6907 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
6908}
6909
6910/* IP is a microMIPS instruction whose opcode we have just changed.
6911 Point IP->insn_mo to the new opcode's definition. */
6912
6913static void
6914find_altered_micromips_opcode (struct mips_cl_insn *ip)
6915{
6916 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
6917}
6918
df58fc94
RS
6919/* For microMIPS macros, we need to generate a local number label
6920 as the target of branches. */
6921#define MICROMIPS_LABEL_CHAR '\037'
6922static unsigned long micromips_target_label;
6923static char micromips_target_name[32];
6924
6925static char *
6926micromips_label_name (void)
6927{
6928 char *p = micromips_target_name;
6929 char symbol_name_temporary[24];
6930 unsigned long l;
6931 int i;
6932
6933 if (*p)
6934 return p;
6935
6936 i = 0;
6937 l = micromips_target_label;
6938#ifdef LOCAL_LABEL_PREFIX
6939 *p++ = LOCAL_LABEL_PREFIX;
6940#endif
6941 *p++ = 'L';
6942 *p++ = MICROMIPS_LABEL_CHAR;
6943 do
6944 {
6945 symbol_name_temporary[i++] = l % 10 + '0';
6946 l /= 10;
6947 }
6948 while (l != 0);
6949 while (i > 0)
6950 *p++ = symbol_name_temporary[--i];
6951 *p = '\0';
6952
6953 return micromips_target_name;
6954}
6955
6956static void
6957micromips_label_expr (expressionS *label_expr)
6958{
6959 label_expr->X_op = O_symbol;
6960 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6961 label_expr->X_add_number = 0;
6962}
6963
6964static void
6965micromips_label_inc (void)
6966{
6967 micromips_target_label++;
6968 *micromips_target_name = '\0';
6969}
6970
6971static void
6972micromips_add_label (void)
6973{
6974 symbolS *s;
6975
6976 s = colon (micromips_label_name ());
6977 micromips_label_inc ();
f3ded42a 6978 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6979}
6980
6981/* If assembling microMIPS code, then return the microMIPS reloc
6982 corresponding to the requested one if any. Otherwise return
6983 the reloc unchanged. */
6984
6985static bfd_reloc_code_real_type
6986micromips_map_reloc (bfd_reloc_code_real_type reloc)
6987{
6988 static const bfd_reloc_code_real_type relocs[][2] =
6989 {
6990 /* Keep sorted incrementally by the left-hand key. */
6991 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6992 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6993 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6994 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6995 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6996 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6997 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6998 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6999 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7000 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7001 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7002 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7003 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7004 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7005 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7006 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7007 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7008 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7009 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7010 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7011 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7012 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7013 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7014 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7015 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7016 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7017 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7018 };
7019 bfd_reloc_code_real_type r;
7020 size_t i;
7021
7022 if (!mips_opts.micromips)
7023 return reloc;
7024 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7025 {
7026 r = relocs[i][0];
7027 if (r > reloc)
7028 return reloc;
7029 if (r == reloc)
7030 return relocs[i][1];
7031 }
7032 return reloc;
7033}
7034
b886a2ab
RS
7035/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7036 Return true on success, storing the resolved value in RESULT. */
7037
7038static bfd_boolean
7039calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7040 offsetT *result)
7041{
7042 switch (reloc)
7043 {
7044 case BFD_RELOC_MIPS_HIGHEST:
7045 case BFD_RELOC_MICROMIPS_HIGHEST:
7046 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7047 return TRUE;
7048
7049 case BFD_RELOC_MIPS_HIGHER:
7050 case BFD_RELOC_MICROMIPS_HIGHER:
7051 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7052 return TRUE;
7053
7054 case BFD_RELOC_HI16_S:
41947d9e 7055 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7056 case BFD_RELOC_MICROMIPS_HI16_S:
7057 case BFD_RELOC_MIPS16_HI16_S:
7058 *result = ((operand + 0x8000) >> 16) & 0xffff;
7059 return TRUE;
7060
7061 case BFD_RELOC_HI16:
7062 case BFD_RELOC_MICROMIPS_HI16:
7063 case BFD_RELOC_MIPS16_HI16:
7064 *result = (operand >> 16) & 0xffff;
7065 return TRUE;
7066
7067 case BFD_RELOC_LO16:
41947d9e 7068 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7069 case BFD_RELOC_MICROMIPS_LO16:
7070 case BFD_RELOC_MIPS16_LO16:
7071 *result = operand & 0xffff;
7072 return TRUE;
7073
7074 case BFD_RELOC_UNUSED:
7075 *result = operand;
7076 return TRUE;
7077
7078 default:
7079 return FALSE;
7080 }
7081}
7082
71400594
RS
7083/* Output an instruction. IP is the instruction information.
7084 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7085 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7086 a macro expansion. */
71400594
RS
7087
7088static void
7089append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7090 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7091{
14fe068b 7092 unsigned long prev_pinfo2, pinfo;
71400594 7093 bfd_boolean relaxed_branch = FALSE;
a4e06468 7094 enum append_method method;
2309ddf2 7095 bfd_boolean relax32;
2b0c8b40 7096 int branch_disp;
71400594 7097
2309ddf2 7098 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7099 fix_loongson2f (ip);
7100
738f4d98 7101 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7102 file_ase_micromips |= mips_opts.micromips;
738f4d98 7103
df58fc94 7104 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7105 pinfo = ip->insn_mo->pinfo;
df58fc94 7106
7bd374a4
MR
7107 /* Don't raise alarm about `nods' frags as they'll fill in the right
7108 kind of nop in relaxation if required. */
df58fc94
RS
7109 if (mips_opts.micromips
7110 && !expansionp
7bd374a4
MR
7111 && !(history[0].frag
7112 && history[0].frag->fr_type == rs_machine_dependent
7113 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7114 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7115 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7116 && micromips_insn_length (ip->insn_mo) != 2)
7117 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7118 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7119 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7120 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7121
15be625d
CM
7122 if (address_expr == NULL)
7123 ip->complete_p = 1;
b886a2ab
RS
7124 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7125 && reloc_type[1] == BFD_RELOC_UNUSED
7126 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7127 && address_expr->X_op == O_constant)
7128 {
15be625d
CM
7129 switch (*reloc_type)
7130 {
15be625d 7131 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7132 {
7133 int shift;
7134
17c6c9d9
MR
7135 /* Shift is 2, unusually, for microMIPS JALX. */
7136 shift = (mips_opts.micromips
7137 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7138 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7139 as_bad (_("jump to misaligned address (0x%lx)"),
7140 (unsigned long) address_expr->X_add_number);
7141 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7142 & 0x3ffffff);
335574df 7143 ip->complete_p = 1;
df58fc94 7144 }
15be625d
CM
7145 break;
7146
7147 case BFD_RELOC_MIPS16_JMP:
7148 if ((address_expr->X_add_number & 3) != 0)
7149 as_bad (_("jump to misaligned address (0x%lx)"),
7150 (unsigned long) address_expr->X_add_number);
7151 ip->insn_opcode |=
7152 (((address_expr->X_add_number & 0x7c0000) << 3)
7153 | ((address_expr->X_add_number & 0xf800000) >> 7)
7154 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7155 ip->complete_p = 1;
15be625d
CM
7156 break;
7157
7158 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7159 {
7160 int shift;
7161
7162 shift = mips_opts.micromips ? 1 : 2;
7163 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7164 as_bad (_("branch to misaligned address (0x%lx)"),
7165 (unsigned long) address_expr->X_add_number);
7166 if (!mips_relax_branch)
7167 {
7168 if ((address_expr->X_add_number + (1 << (shift + 15)))
7169 & ~((1 << (shift + 16)) - 1))
7170 as_bad (_("branch address range overflow (0x%lx)"),
7171 (unsigned long) address_expr->X_add_number);
7172 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7173 & 0xffff);
7174 }
df58fc94 7175 }
15be625d
CM
7176 break;
7177
7361da2c
AB
7178 case BFD_RELOC_MIPS_21_PCREL_S2:
7179 {
7180 int shift;
7181
7182 shift = 2;
7183 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7184 as_bad (_("branch to misaligned address (0x%lx)"),
7185 (unsigned long) address_expr->X_add_number);
7186 if ((address_expr->X_add_number + (1 << (shift + 20)))
7187 & ~((1 << (shift + 21)) - 1))
7188 as_bad (_("branch address range overflow (0x%lx)"),
7189 (unsigned long) address_expr->X_add_number);
7190 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7191 & 0x1fffff);
7192 }
7193 break;
7194
7195 case BFD_RELOC_MIPS_26_PCREL_S2:
7196 {
7197 int shift;
7198
7199 shift = 2;
7200 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7201 as_bad (_("branch to misaligned address (0x%lx)"),
7202 (unsigned long) address_expr->X_add_number);
7203 if ((address_expr->X_add_number + (1 << (shift + 25)))
7204 & ~((1 << (shift + 26)) - 1))
7205 as_bad (_("branch address range overflow (0x%lx)"),
7206 (unsigned long) address_expr->X_add_number);
7207 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7208 & 0x3ffffff);
7209 }
7210 break;
7211
15be625d 7212 default:
b886a2ab
RS
7213 {
7214 offsetT value;
7215
7216 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7217 &value))
7218 {
7219 ip->insn_opcode |= value & 0xffff;
7220 ip->complete_p = 1;
7221 }
7222 }
7223 break;
7224 }
15be625d
CM
7225 }
7226
71400594
RS
7227 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7228 {
7229 /* There are a lot of optimizations we could do that we don't.
7230 In particular, we do not, in general, reorder instructions.
7231 If you use gcc with optimization, it will reorder
7232 instructions and generally do much more optimization then we
7233 do here; repeating all that work in the assembler would only
7234 benefit hand written assembly code, and does not seem worth
7235 it. */
7236 int nops = (mips_optimize == 0
932d1a1b
RS
7237 ? nops_for_insn (0, history, NULL)
7238 : nops_for_insn_or_target (0, history, ip));
71400594 7239 if (nops > 0)
252b5132
RH
7240 {
7241 fragS *old_frag;
7242 unsigned long old_frag_offset;
7243 int i;
252b5132
RH
7244
7245 old_frag = frag_now;
7246 old_frag_offset = frag_now_fix ();
7247
7248 for (i = 0; i < nops; i++)
14fe068b
RS
7249 add_fixed_insn (NOP_INSN);
7250 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7251
7252 if (listing)
7253 {
7254 listing_prev_line ();
7255 /* We may be at the start of a variant frag. In case we
7256 are, make sure there is enough space for the frag
7257 after the frags created by listing_prev_line. The
7258 argument to frag_grow here must be at least as large
7259 as the argument to all other calls to frag_grow in
7260 this file. We don't have to worry about being in the
7261 middle of a variant frag, because the variants insert
7262 all needed nop instructions themselves. */
7263 frag_grow (40);
7264 }
7265
462427c4 7266 mips_move_text_labels ();
252b5132
RH
7267
7268#ifndef NO_ECOFF_DEBUGGING
7269 if (ECOFF_DEBUGGING)
7270 ecoff_fix_loc (old_frag, old_frag_offset);
7271#endif
7272 }
71400594
RS
7273 }
7274 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7275 {
932d1a1b
RS
7276 int nops;
7277
7278 /* Work out how many nops in prev_nop_frag are needed by IP,
7279 ignoring hazards generated by the first prev_nop_frag_since
7280 instructions. */
7281 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7282 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7283
71400594
RS
7284 /* Enforce NOPS as a minimum. */
7285 if (nops > prev_nop_frag_required)
7286 prev_nop_frag_required = nops;
252b5132 7287
71400594
RS
7288 if (prev_nop_frag_holds == prev_nop_frag_required)
7289 {
7290 /* Settle for the current number of nops. Update the history
7291 accordingly (for the benefit of any future .set reorder code). */
7292 prev_nop_frag = NULL;
7293 insert_into_history (prev_nop_frag_since,
7294 prev_nop_frag_holds, NOP_INSN);
7295 }
7296 else
7297 {
7298 /* Allow this instruction to replace one of the nops that was
7299 tentatively added to prev_nop_frag. */
df58fc94 7300 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7301 prev_nop_frag_holds--;
7302 prev_nop_frag_since++;
252b5132
RH
7303 }
7304 }
7305
e407c74b 7306 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7307 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7308
e410add4
RS
7309 dwarf2_emit_insn (0);
7310 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7311 so "move" the instruction address accordingly.
7312
7313 Also, it doesn't seem appropriate for the assembler to reorder .loc
7314 entries. If this instruction is a branch that we are going to swap
7315 with the previous instruction, the two instructions should be
7316 treated as a unit, and the debug information for both instructions
7317 should refer to the start of the branch sequence. Using the
7318 current position is certainly wrong when swapping a 32-bit branch
7319 and a 16-bit delay slot, since the current position would then be
7320 in the middle of a branch. */
7321 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7322
df58fc94
RS
7323 relax32 = (mips_relax_branch
7324 /* Don't try branch relaxation within .set nomacro, or within
7325 .set noat if we use $at for PIC computations. If it turns
7326 out that the branch was out-of-range, we'll get an error. */
7327 && !mips_opts.warn_about_macros
7328 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7329 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7330 as they have no complementing branches. */
7331 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7332
7333 if (!HAVE_CODE_COMPRESSION
7334 && address_expr
7335 && relax32
0b25d3e6 7336 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7337 && delayed_branch_p (ip))
4a6a3df4 7338 {
895921c9 7339 relaxed_branch = TRUE;
1e915849
RS
7340 add_relaxed_insn (ip, (relaxed_branch_length
7341 (NULL, NULL,
11625dd8
RS
7342 uncond_branch_p (ip) ? -1
7343 : branch_likely_p (ip) ? 1
1e915849
RS
7344 : 0)), 4,
7345 RELAX_BRANCH_ENCODE
ce8ad872 7346 (AT, mips_pic != NO_PIC,
11625dd8
RS
7347 uncond_branch_p (ip),
7348 branch_likely_p (ip),
1e915849
RS
7349 pinfo & INSN_WRITE_GPR_31,
7350 0),
7351 address_expr->X_add_symbol,
7352 address_expr->X_add_number);
4a6a3df4
AO
7353 *reloc_type = BFD_RELOC_UNUSED;
7354 }
df58fc94
RS
7355 else if (mips_opts.micromips
7356 && address_expr
7357 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7358 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7359 && (delayed_branch_p (ip) || compact_branch_p (ip))
7360 /* Don't try branch relaxation when users specify
7361 16-bit/32-bit instructions. */
7362 && !forced_insn_length)
df58fc94 7363 {
7bd374a4
MR
7364 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7365 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7366 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7367 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7368 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7369 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7370 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7371 int length32 = nods ? 8 : 4;
df58fc94
RS
7372
7373 gas_assert (address_expr != NULL);
7374 gas_assert (!mips_relax.sequence);
7375
2b0c8b40 7376 relaxed_branch = TRUE;
7bd374a4
MR
7377 if (nods)
7378 method = APPEND_ADD;
7379 if (relax32)
7380 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7381 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7382 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7383 mips_pic != NO_PIC,
7bd374a4 7384 uncond, compact, al, nods,
40209cad 7385 relax32, 0, 0),
df58fc94
RS
7386 address_expr->X_add_symbol,
7387 address_expr->X_add_number);
7388 *reloc_type = BFD_RELOC_UNUSED;
7389 }
7390 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7391 {
7fd53920
MR
7392 bfd_boolean require_unextended;
7393 bfd_boolean require_extended;
88a7ef16
MR
7394 symbolS *symbol;
7395 offsetT offset;
7396
7fd53920
MR
7397 if (forced_insn_length != 0)
7398 {
7399 require_unextended = forced_insn_length == 2;
7400 require_extended = forced_insn_length == 4;
7401 }
7402 else
7403 {
7404 require_unextended = (mips_opts.noautoextend
7405 && !mips_opcode_32bit_p (ip->insn_mo));
7406 require_extended = 0;
7407 }
7408
252b5132 7409 /* We need to set up a variant frag. */
df58fc94 7410 gas_assert (address_expr != NULL);
88a7ef16
MR
7411 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7412 symbol created by `make_expr_symbol' may not get a necessary
7413 external relocation produced. */
7414 if (address_expr->X_op == O_symbol)
7415 {
7416 symbol = address_expr->X_add_symbol;
7417 offset = address_expr->X_add_number;
7418 }
7419 else
7420 {
7421 symbol = make_expr_symbol (address_expr);
82d808ed 7422 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7423 offset = 0;
7424 }
1e915849
RS
7425 add_relaxed_insn (ip, 4, 0,
7426 RELAX_MIPS16_ENCODE
7427 (*reloc_type - BFD_RELOC_UNUSED,
7fd53920 7428 require_unextended, require_extended,
11625dd8 7429 delayed_branch_p (&history[0]),
1e915849 7430 history[0].mips16_absolute_jump_p),
88a7ef16 7431 symbol, offset);
252b5132 7432 }
5c04167a 7433 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7434 {
11625dd8 7435 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7436 /* Make sure there is enough room to swap this instruction with
7437 a following jump instruction. */
7438 frag_grow (6);
1e915849 7439 add_fixed_insn (ip);
252b5132
RH
7440 }
7441 else
7442 {
7443 if (mips_opts.mips16
7444 && mips_opts.noreorder
11625dd8 7445 && delayed_branch_p (&history[0]))
252b5132
RH
7446 as_warn (_("extended instruction in delay slot"));
7447
4d7206a2
RS
7448 if (mips_relax.sequence)
7449 {
7450 /* If we've reached the end of this frag, turn it into a variant
7451 frag and record the information for the instructions we've
7452 written so far. */
7453 if (frag_room () < 4)
7454 relax_close_frag ();
df58fc94 7455 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7456 }
7457
584892a6 7458 if (mips_relax.sequence != 2)
df58fc94
RS
7459 {
7460 if (mips_macro_warning.first_insn_sizes[0] == 0)
7461 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7462 mips_macro_warning.sizes[0] += insn_length (ip);
7463 mips_macro_warning.insns[0]++;
7464 }
584892a6 7465 if (mips_relax.sequence != 1)
df58fc94
RS
7466 {
7467 if (mips_macro_warning.first_insn_sizes[1] == 0)
7468 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7469 mips_macro_warning.sizes[1] += insn_length (ip);
7470 mips_macro_warning.insns[1]++;
7471 }
584892a6 7472
1e915849
RS
7473 if (mips_opts.mips16)
7474 {
7475 ip->fixed_p = 1;
7476 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7477 }
7478 add_fixed_insn (ip);
252b5132
RH
7479 }
7480
9fe77896 7481 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7482 {
df58fc94 7483 bfd_reloc_code_real_type final_type[3];
2309ddf2 7484 reloc_howto_type *howto0;
9fe77896
RS
7485 reloc_howto_type *howto;
7486 int i;
34ce925e 7487
df58fc94
RS
7488 /* Perform any necessary conversion to microMIPS relocations
7489 and find out how many relocations there actually are. */
7490 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7491 final_type[i] = micromips_map_reloc (reloc_type[i]);
7492
9fe77896
RS
7493 /* In a compound relocation, it is the final (outermost)
7494 operator that determines the relocated field. */
2309ddf2 7495 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7496 if (!howto)
7497 abort ();
2309ddf2
MR
7498
7499 if (i > 1)
7500 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7501 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7502 bfd_get_reloc_size (howto),
7503 address_expr,
2309ddf2
MR
7504 howto0 && howto0->pc_relative,
7505 final_type[0]);
ce8ad872
MR
7506 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7507 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7508
7509 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7510 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7511 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7512
7513 /* These relocations can have an addend that won't fit in
7514 4 octets for 64bit assembly. */
bad1aba3 7515 if (GPR_SIZE == 64
9fe77896
RS
7516 && ! howto->partial_inplace
7517 && (reloc_type[0] == BFD_RELOC_16
7518 || reloc_type[0] == BFD_RELOC_32
7519 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7520 || reloc_type[0] == BFD_RELOC_GPREL16
7521 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7522 || reloc_type[0] == BFD_RELOC_GPREL32
7523 || reloc_type[0] == BFD_RELOC_64
7524 || reloc_type[0] == BFD_RELOC_CTOR
7525 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7526 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7527 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7528 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7529 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7530 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7531 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7532 || hi16_reloc_p (reloc_type[0])
7533 || lo16_reloc_p (reloc_type[0])))
7534 ip->fixp[0]->fx_no_overflow = 1;
7535
ddaf2c41
MR
7536 /* These relocations can have an addend that won't fit in 2 octets. */
7537 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7538 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7539 ip->fixp[0]->fx_no_overflow = 1;
7540
9fe77896
RS
7541 if (mips_relax.sequence)
7542 {
7543 if (mips_relax.first_fixup == 0)
7544 mips_relax.first_fixup = ip->fixp[0];
7545 }
7546 else if (reloc_needs_lo_p (*reloc_type))
7547 {
7548 struct mips_hi_fixup *hi_fixup;
7549
7550 /* Reuse the last entry if it already has a matching %lo. */
7551 hi_fixup = mips_hi_fixup_list;
7552 if (hi_fixup == 0
7553 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7554 {
325801bd 7555 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7556 hi_fixup->next = mips_hi_fixup_list;
7557 mips_hi_fixup_list = hi_fixup;
4d7206a2 7558 }
9fe77896
RS
7559 hi_fixup->fixp = ip->fixp[0];
7560 hi_fixup->seg = now_seg;
7561 }
252b5132 7562
9fe77896
RS
7563 /* Add fixups for the second and third relocations, if given.
7564 Note that the ABI allows the second relocation to be
7565 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7566 moment we only use RSS_UNDEF, but we could add support
7567 for the others if it ever becomes necessary. */
7568 for (i = 1; i < 3; i++)
7569 if (reloc_type[i] != BFD_RELOC_UNUSED)
7570 {
7571 ip->fixp[i] = fix_new (ip->frag, ip->where,
7572 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7573 FALSE, final_type[i]);
f6688943 7574
9fe77896
RS
7575 /* Use fx_tcbit to mark compound relocs. */
7576 ip->fixp[0]->fx_tcbit = 1;
7577 ip->fixp[i]->fx_tcbit = 1;
7578 }
252b5132 7579 }
252b5132
RH
7580
7581 /* Update the register mask information. */
4c260379
RS
7582 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7583 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7584
a4e06468 7585 switch (method)
252b5132 7586 {
a4e06468
RS
7587 case APPEND_ADD:
7588 insert_into_history (0, 1, ip);
7589 break;
7590
7591 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7592 {
7593 struct mips_cl_insn *nop;
7594
7595 insert_into_history (0, 1, ip);
7596 nop = get_delay_slot_nop (ip);
7597 add_fixed_insn (nop);
7598 insert_into_history (0, 1, nop);
7599 if (mips_relax.sequence)
7600 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7601 }
a4e06468
RS
7602 break;
7603
7604 case APPEND_ADD_COMPACT:
7605 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7606 if (mips_opts.mips16)
7607 {
7608 ip->insn_opcode |= 0x0080;
7609 find_altered_mips16_opcode (ip);
7610 }
7611 /* Convert microMIPS instructions. */
7612 else if (mips_opts.micromips)
7613 {
7614 /* jr16->jrc */
7615 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7616 ip->insn_opcode |= 0x0020;
7617 /* b16->bc */
7618 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7619 ip->insn_opcode = 0x40e00000;
7620 /* beqz16->beqzc, bnez16->bnezc */
7621 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7622 {
7623 unsigned long regno;
7624
7625 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7626 regno &= MICROMIPSOP_MASK_MD;
7627 regno = micromips_to_32_reg_d_map[regno];
7628 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7629 | (regno << MICROMIPSOP_SH_RS)
7630 | 0x40a00000) ^ 0x00400000;
7631 }
7632 /* beqz->beqzc, bnez->bnezc */
7633 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7634 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7635 | ((ip->insn_opcode >> 7) & 0x00400000)
7636 | 0x40a00000) ^ 0x00400000;
7637 /* beq $0->beqzc, bne $0->bnezc */
7638 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7639 ip->insn_opcode = (((ip->insn_opcode >>
7640 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7641 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7642 | ((ip->insn_opcode >> 7) & 0x00400000)
7643 | 0x40a00000) ^ 0x00400000;
7644 else
7645 abort ();
7646 find_altered_micromips_opcode (ip);
7647 }
7648 else
7649 abort ();
a4e06468
RS
7650 install_insn (ip);
7651 insert_into_history (0, 1, ip);
7652 break;
7653
7654 case APPEND_SWAP:
7655 {
7656 struct mips_cl_insn delay = history[0];
99e7978b
MF
7657
7658 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7659 {
7660 /* Add the delay slot instruction to the end of the
7661 current frag and shrink the fixed part of the
7662 original frag. If the branch occupies the tail of
7663 the latter, move it backwards to cover the gap. */
2b0c8b40 7664 delay.frag->fr_fix -= branch_disp;
a4e06468 7665 if (delay.frag == ip->frag)
2b0c8b40 7666 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7667 add_fixed_insn (&delay);
7668 }
7669 else
7670 {
5e35670b
MR
7671 /* If this is not a relaxed branch and we are in the
7672 same frag, then just swap the instructions. */
7673 move_insn (ip, delay.frag, delay.where);
7674 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7675 }
7676 history[0] = *ip;
7677 delay.fixed_p = 1;
7678 insert_into_history (0, 1, &delay);
7679 }
7680 break;
252b5132
RH
7681 }
7682
13408f1e 7683 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7684 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7685 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7686 {
7687 unsigned int i;
7688
79850f26 7689 mips_no_prev_insn ();
13408f1e 7690
e407c74b 7691 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7692 history[i].cleared_p = 1;
e407c74b
NC
7693 }
7694
df58fc94
RS
7695 /* We need to emit a label at the end of branch-likely macros. */
7696 if (emit_branch_likely_macro)
7697 {
7698 emit_branch_likely_macro = FALSE;
7699 micromips_add_label ();
7700 }
7701
252b5132
RH
7702 /* We just output an insn, so the next one doesn't have a label. */
7703 mips_clear_insn_labels ();
252b5132
RH
7704}
7705
e407c74b
NC
7706/* Forget that there was any previous instruction or label.
7707 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7708
7709static void
7d10b47d 7710mips_no_prev_insn (void)
252b5132 7711{
7d10b47d
RS
7712 prev_nop_frag = NULL;
7713 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7714 mips_clear_insn_labels ();
7715}
7716
7d10b47d
RS
7717/* This function must be called before we emit something other than
7718 instructions. It is like mips_no_prev_insn except that it inserts
7719 any NOPS that might be needed by previous instructions. */
252b5132 7720
7d10b47d
RS
7721void
7722mips_emit_delays (void)
252b5132
RH
7723{
7724 if (! mips_opts.noreorder)
7725 {
932d1a1b 7726 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7727 if (nops > 0)
7728 {
7d10b47d
RS
7729 while (nops-- > 0)
7730 add_fixed_insn (NOP_INSN);
462427c4 7731 mips_move_text_labels ();
7d10b47d
RS
7732 }
7733 }
7734 mips_no_prev_insn ();
7735}
7736
7737/* Start a (possibly nested) noreorder block. */
7738
7739static void
7740start_noreorder (void)
7741{
7742 if (mips_opts.noreorder == 0)
7743 {
7744 unsigned int i;
7745 int nops;
7746
7747 /* None of the instructions before the .set noreorder can be moved. */
7748 for (i = 0; i < ARRAY_SIZE (history); i++)
7749 history[i].fixed_p = 1;
7750
7751 /* Insert any nops that might be needed between the .set noreorder
7752 block and the previous instructions. We will later remove any
7753 nops that turn out not to be needed. */
932d1a1b 7754 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7755 if (nops > 0)
7756 {
7757 if (mips_optimize != 0)
252b5132
RH
7758 {
7759 /* Record the frag which holds the nop instructions, so
7760 that we can remove them if we don't need them. */
df58fc94 7761 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7762 prev_nop_frag = frag_now;
7763 prev_nop_frag_holds = nops;
7764 prev_nop_frag_required = 0;
7765 prev_nop_frag_since = 0;
7766 }
7767
7768 for (; nops > 0; --nops)
1e915849 7769 add_fixed_insn (NOP_INSN);
252b5132 7770
7d10b47d
RS
7771 /* Move on to a new frag, so that it is safe to simply
7772 decrease the size of prev_nop_frag. */
7773 frag_wane (frag_now);
7774 frag_new (0);
462427c4 7775 mips_move_text_labels ();
252b5132 7776 }
df58fc94 7777 mips_mark_labels ();
7d10b47d 7778 mips_clear_insn_labels ();
252b5132 7779 }
7d10b47d
RS
7780 mips_opts.noreorder++;
7781 mips_any_noreorder = 1;
7782}
252b5132 7783
7d10b47d 7784/* End a nested noreorder block. */
252b5132 7785
7d10b47d
RS
7786static void
7787end_noreorder (void)
7788{
7789 mips_opts.noreorder--;
7790 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7791 {
7792 /* Commit to inserting prev_nop_frag_required nops and go back to
7793 handling nop insertion the .set reorder way. */
7794 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7795 * NOP_INSN_SIZE);
7d10b47d
RS
7796 insert_into_history (prev_nop_frag_since,
7797 prev_nop_frag_required, NOP_INSN);
7798 prev_nop_frag = NULL;
7799 }
252b5132
RH
7800}
7801
97d87491
RS
7802/* Sign-extend 32-bit mode constants that have bit 31 set and all
7803 higher bits unset. */
7804
7805static void
7806normalize_constant_expr (expressionS *ex)
7807{
7808 if (ex->X_op == O_constant
7809 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7810 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7811 - 0x80000000);
7812}
7813
7814/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7815 all higher bits unset. */
7816
7817static void
7818normalize_address_expr (expressionS *ex)
7819{
7820 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7821 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7822 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7823 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7824 - 0x80000000);
7825}
7826
7827/* Try to match TOKENS against OPCODE, storing the result in INSN.
7828 Return true if the match was successful.
7829
7830 OPCODE_EXTRA is a value that should be ORed into the opcode
7831 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7832 there are more alternatives after OPCODE and SOFT_MATCH is
7833 as for mips_arg_info. */
7834
7835static bfd_boolean
7836match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7837 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7838 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7839{
7840 const char *args;
7841 struct mips_arg_info arg;
7842 const struct mips_operand *operand;
7843 char c;
7844
7845 imm_expr.X_op = O_absent;
97d87491
RS
7846 offset_expr.X_op = O_absent;
7847 offset_reloc[0] = BFD_RELOC_UNUSED;
7848 offset_reloc[1] = BFD_RELOC_UNUSED;
7849 offset_reloc[2] = BFD_RELOC_UNUSED;
7850
7851 create_insn (insn, opcode);
60f20e8b
RS
7852 /* When no opcode suffix is specified, assume ".xyzw". */
7853 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7854 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7855 else
7856 insn->insn_opcode |= opcode_extra;
97d87491
RS
7857 memset (&arg, 0, sizeof (arg));
7858 arg.insn = insn;
7859 arg.token = tokens;
7860 arg.argnum = 1;
7861 arg.last_regno = ILLEGAL_REG;
7862 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7863 arg.lax_match = lax_match;
97d87491
RS
7864 for (args = opcode->args;; ++args)
7865 {
7866 if (arg.token->type == OT_END)
7867 {
7868 /* Handle unary instructions in which only one operand is given.
7869 The source is then the same as the destination. */
7870 if (arg.opnum == 1 && *args == ',')
7871 {
7872 operand = (mips_opts.micromips
7873 ? decode_micromips_operand (args + 1)
7874 : decode_mips_operand (args + 1));
7875 if (operand && mips_optional_operand_p (operand))
7876 {
7877 arg.token = tokens;
7878 arg.argnum = 1;
7879 continue;
7880 }
7881 }
7882
7883 /* Treat elided base registers as $0. */
7884 if (strcmp (args, "(b)") == 0)
7885 args += 3;
7886
7887 if (args[0] == '+')
7888 switch (args[1])
7889 {
7890 case 'K':
7891 case 'N':
7892 /* The register suffix is optional. */
7893 args += 2;
7894 break;
7895 }
7896
7897 /* Fail the match if there were too few operands. */
7898 if (*args)
7899 return FALSE;
7900
7901 /* Successful match. */
60f20e8b
RS
7902 if (!complete_p)
7903 return TRUE;
e3de51ce 7904 clear_insn_error ();
97d87491
RS
7905 if (arg.dest_regno == arg.last_regno
7906 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7907 {
7908 if (arg.opnum == 2)
e3de51ce 7909 set_insn_error
1661c76c 7910 (0, _("source and destination must be different"));
97d87491 7911 else if (arg.last_regno == 31)
e3de51ce 7912 set_insn_error
1661c76c 7913 (0, _("a destination register must be supplied"));
97d87491 7914 }
173d3447
CF
7915 else if (arg.last_regno == 31
7916 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7917 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7918 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
7919 check_completed_insn (&arg);
7920 return TRUE;
7921 }
7922
7923 /* Fail the match if the line has too many operands. */
7924 if (*args == 0)
7925 return FALSE;
7926
7927 /* Handle characters that need to match exactly. */
7928 if (*args == '(' || *args == ')' || *args == ',')
7929 {
7930 if (match_char (&arg, *args))
7931 continue;
7932 return FALSE;
7933 }
7934 if (*args == '#')
7935 {
7936 ++args;
7937 if (arg.token->type == OT_DOUBLE_CHAR
7938 && arg.token->u.ch == *args)
7939 {
7940 ++arg.token;
7941 continue;
7942 }
7943 return FALSE;
7944 }
7945
7946 /* Handle special macro operands. Work out the properties of
7947 other operands. */
7948 arg.opnum += 1;
97d87491
RS
7949 switch (*args)
7950 {
7361da2c
AB
7951 case '-':
7952 switch (args[1])
7953 {
7954 case 'A':
7955 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7956 break;
7957
7958 case 'B':
7959 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7960 break;
7961 }
7962 break;
7963
97d87491
RS
7964 case '+':
7965 switch (args[1])
7966 {
97d87491
RS
7967 case 'i':
7968 *offset_reloc = BFD_RELOC_MIPS_JMP;
7969 break;
7361da2c
AB
7970
7971 case '\'':
7972 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7973 break;
7974
7975 case '\"':
7976 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7977 break;
97d87491
RS
7978 }
7979 break;
7980
97d87491 7981 case 'I':
1a00e612
RS
7982 if (!match_const_int (&arg, &imm_expr.X_add_number))
7983 return FALSE;
7984 imm_expr.X_op = O_constant;
bad1aba3 7985 if (GPR_SIZE == 32)
97d87491
RS
7986 normalize_constant_expr (&imm_expr);
7987 continue;
7988
7989 case 'A':
7990 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7991 {
7992 /* Assume that the offset has been elided and that what
7993 we saw was a base register. The match will fail later
7994 if that assumption turns out to be wrong. */
7995 offset_expr.X_op = O_constant;
7996 offset_expr.X_add_number = 0;
7997 }
97d87491 7998 else
1a00e612
RS
7999 {
8000 if (!match_expression (&arg, &offset_expr, offset_reloc))
8001 return FALSE;
8002 normalize_address_expr (&offset_expr);
8003 }
97d87491
RS
8004 continue;
8005
8006 case 'F':
8007 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8008 8, TRUE))
1a00e612 8009 return FALSE;
97d87491
RS
8010 continue;
8011
8012 case 'L':
8013 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8014 8, FALSE))
1a00e612 8015 return FALSE;
97d87491
RS
8016 continue;
8017
8018 case 'f':
8019 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8020 4, TRUE))
1a00e612 8021 return FALSE;
97d87491
RS
8022 continue;
8023
8024 case 'l':
8025 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8026 4, FALSE))
1a00e612 8027 return FALSE;
97d87491
RS
8028 continue;
8029
97d87491
RS
8030 case 'p':
8031 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8032 break;
8033
8034 case 'a':
8035 *offset_reloc = BFD_RELOC_MIPS_JMP;
8036 break;
8037
8038 case 'm':
8039 gas_assert (mips_opts.micromips);
8040 c = args[1];
8041 switch (c)
8042 {
8043 case 'D':
8044 case 'E':
8045 if (!forced_insn_length)
8046 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8047 else if (c == 'D')
8048 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8049 else
8050 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8051 break;
8052 }
8053 break;
8054 }
8055
8056 operand = (mips_opts.micromips
8057 ? decode_micromips_operand (args)
8058 : decode_mips_operand (args));
8059 if (!operand)
8060 abort ();
8061
8062 /* Skip prefixes. */
7361da2c 8063 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8064 args++;
8065
8066 if (mips_optional_operand_p (operand)
8067 && args[1] == ','
8068 && (arg.token[0].type != OT_REG
8069 || arg.token[1].type == OT_END))
8070 {
8071 /* Assume that the register has been elided and is the
8072 same as the first operand. */
8073 arg.token = tokens;
8074 arg.argnum = 1;
8075 }
8076
8077 if (!match_operand (&arg, operand))
8078 return FALSE;
8079 }
8080}
8081
8082/* Like match_insn, but for MIPS16. */
8083
8084static bfd_boolean
8085match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8086 struct mips_operand_token *tokens)
97d87491
RS
8087{
8088 const char *args;
8089 const struct mips_operand *operand;
8090 const struct mips_operand *ext_operand;
82d808ed 8091 bfd_boolean pcrel = FALSE;
7fd53920 8092 int required_insn_length;
97d87491
RS
8093 struct mips_arg_info arg;
8094 int relax_char;
8095
7fd53920
MR
8096 if (forced_insn_length)
8097 required_insn_length = forced_insn_length;
8098 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8099 required_insn_length = 2;
8100 else
8101 required_insn_length = 0;
8102
97d87491
RS
8103 create_insn (insn, opcode);
8104 imm_expr.X_op = O_absent;
97d87491
RS
8105 offset_expr.X_op = O_absent;
8106 offset_reloc[0] = BFD_RELOC_UNUSED;
8107 offset_reloc[1] = BFD_RELOC_UNUSED;
8108 offset_reloc[2] = BFD_RELOC_UNUSED;
8109 relax_char = 0;
8110
8111 memset (&arg, 0, sizeof (arg));
8112 arg.insn = insn;
8113 arg.token = tokens;
8114 arg.argnum = 1;
8115 arg.last_regno = ILLEGAL_REG;
8116 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8117 relax_char = 0;
8118 for (args = opcode->args;; ++args)
8119 {
8120 int c;
8121
8122 if (arg.token->type == OT_END)
8123 {
8124 offsetT value;
8125
8126 /* Handle unary instructions in which only one operand is given.
8127 The source is then the same as the destination. */
8128 if (arg.opnum == 1 && *args == ',')
8129 {
8130 operand = decode_mips16_operand (args[1], FALSE);
8131 if (operand && mips_optional_operand_p (operand))
8132 {
8133 arg.token = tokens;
8134 arg.argnum = 1;
8135 continue;
8136 }
8137 }
8138
8139 /* Fail the match if there were too few operands. */
8140 if (*args)
8141 return FALSE;
8142
8143 /* Successful match. Stuff the immediate value in now, if
8144 we can. */
e3de51ce 8145 clear_insn_error ();
97d87491
RS
8146 if (opcode->pinfo == INSN_MACRO)
8147 {
8148 gas_assert (relax_char == 0 || relax_char == 'p');
8149 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8150 }
8151 else if (relax_char
8152 && offset_expr.X_op == O_constant
82d808ed 8153 && !pcrel
97d87491
RS
8154 && calculate_reloc (*offset_reloc,
8155 offset_expr.X_add_number,
8156 &value))
8157 {
8158 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8159 required_insn_length, &insn->insn_opcode);
97d87491
RS
8160 offset_expr.X_op = O_absent;
8161 *offset_reloc = BFD_RELOC_UNUSED;
8162 }
8163 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8164 {
7fd53920 8165 if (required_insn_length == 2)
e3de51ce 8166 set_insn_error (0, _("invalid unextended operand value"));
1da43acc
MR
8167 else
8168 {
8169 forced_insn_length = 4;
8170 insn->insn_opcode |= MIPS16_EXTEND;
8171 }
97d87491
RS
8172 }
8173 else if (relax_char)
8174 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8175
8176 check_completed_insn (&arg);
8177 return TRUE;
8178 }
8179
8180 /* Fail the match if the line has too many operands. */
8181 if (*args == 0)
8182 return FALSE;
8183
8184 /* Handle characters that need to match exactly. */
8185 if (*args == '(' || *args == ')' || *args == ',')
8186 {
8187 if (match_char (&arg, *args))
8188 continue;
8189 return FALSE;
8190 }
8191
8192 arg.opnum += 1;
8193 c = *args;
8194 switch (c)
8195 {
8196 case 'p':
8197 case 'q':
8198 case 'A':
8199 case 'B':
8200 case 'E':
8201 relax_char = c;
8202 break;
8203
8204 case 'I':
1a00e612
RS
8205 if (!match_const_int (&arg, &imm_expr.X_add_number))
8206 return FALSE;
8207 imm_expr.X_op = O_constant;
bad1aba3 8208 if (GPR_SIZE == 32)
97d87491
RS
8209 normalize_constant_expr (&imm_expr);
8210 continue;
8211
8212 case 'a':
8213 case 'i':
8214 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8215 break;
8216 }
8217
7fd53920 8218 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8219 if (!operand)
8220 abort ();
8221
82d808ed
MR
8222 if (operand->type == OP_PCREL)
8223 pcrel = TRUE;
8224 else
97d87491
RS
8225 {
8226 ext_operand = decode_mips16_operand (c, TRUE);
8227 if (operand != ext_operand)
8228 {
8229 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8230 {
8231 offset_expr.X_op = O_constant;
8232 offset_expr.X_add_number = 0;
8233 relax_char = c;
8234 continue;
8235 }
8236
8237 /* We need the OT_INTEGER check because some MIPS16
8238 immediate variants are listed before the register ones. */
8239 if (arg.token->type != OT_INTEGER
8240 || !match_expression (&arg, &offset_expr, offset_reloc))
8241 return FALSE;
8242
8243 /* '8' is used for SLTI(U) and has traditionally not
8244 been allowed to take relocation operators. */
8245 if (offset_reloc[0] != BFD_RELOC_UNUSED
8246 && (ext_operand->size != 16 || c == '8'))
8247 return FALSE;
8248
8249 relax_char = c;
8250 continue;
8251 }
8252 }
8253
8254 if (mips_optional_operand_p (operand)
8255 && args[1] == ','
8256 && (arg.token[0].type != OT_REG
8257 || arg.token[1].type == OT_END))
8258 {
8259 /* Assume that the register has been elided and is the
8260 same as the first operand. */
8261 arg.token = tokens;
8262 arg.argnum = 1;
8263 }
8264
8265 if (!match_operand (&arg, operand))
8266 return FALSE;
8267 }
8268}
8269
60f20e8b
RS
8270/* Record that the current instruction is invalid for the current ISA. */
8271
8272static void
8273match_invalid_for_isa (void)
8274{
8275 set_insn_error_ss
1661c76c 8276 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8277 mips_cpu_info_from_arch (mips_opts.arch)->name,
8278 mips_cpu_info_from_isa (mips_opts.isa)->name);
8279}
8280
8281/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8282 Return true if a definite match or failure was found, storing any match
8283 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8284 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8285 tried and failed to match under normal conditions and now want to try a
8286 more relaxed match. */
8287
8288static bfd_boolean
8289match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8290 const struct mips_opcode *past, struct mips_operand_token *tokens,
8291 int opcode_extra, bfd_boolean lax_match)
8292{
8293 const struct mips_opcode *opcode;
8294 const struct mips_opcode *invalid_delay_slot;
8295 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8296
8297 /* Search for a match, ignoring alternatives that don't satisfy the
8298 current ISA or forced_length. */
8299 invalid_delay_slot = 0;
8300 seen_valid_for_isa = FALSE;
8301 seen_valid_for_size = FALSE;
8302 opcode = first;
8303 do
8304 {
8305 gas_assert (strcmp (opcode->name, first->name) == 0);
8306 if (is_opcode_valid (opcode))
8307 {
8308 seen_valid_for_isa = TRUE;
8309 if (is_size_valid (opcode))
8310 {
8311 bfd_boolean delay_slot_ok;
8312
8313 seen_valid_for_size = TRUE;
8314 delay_slot_ok = is_delay_slot_valid (opcode);
8315 if (match_insn (insn, opcode, tokens, opcode_extra,
8316 lax_match, delay_slot_ok))
8317 {
8318 if (!delay_slot_ok)
8319 {
8320 if (!invalid_delay_slot)
8321 invalid_delay_slot = opcode;
8322 }
8323 else
8324 return TRUE;
8325 }
8326 }
8327 }
8328 ++opcode;
8329 }
8330 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8331
8332 /* If the only matches we found had the wrong length for the delay slot,
8333 pick the first such match. We'll issue an appropriate warning later. */
8334 if (invalid_delay_slot)
8335 {
8336 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8337 lax_match, TRUE))
8338 return TRUE;
8339 abort ();
8340 }
8341
8342 /* Handle the case where we didn't try to match an instruction because
8343 all the alternatives were incompatible with the current ISA. */
8344 if (!seen_valid_for_isa)
8345 {
8346 match_invalid_for_isa ();
8347 return TRUE;
8348 }
8349
8350 /* Handle the case where we didn't try to match an instruction because
8351 all the alternatives were of the wrong size. */
8352 if (!seen_valid_for_size)
8353 {
8354 if (mips_opts.insn32)
1661c76c 8355 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8356 else
8357 set_insn_error_i
1661c76c 8358 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8359 8 * forced_insn_length);
8360 return TRUE;
8361 }
8362
8363 return FALSE;
8364}
8365
8366/* Like match_insns, but for MIPS16. */
8367
8368static bfd_boolean
8369match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8370 struct mips_operand_token *tokens)
8371{
8372 const struct mips_opcode *opcode;
8373 bfd_boolean seen_valid_for_isa;
7fd53920 8374 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8375
8376 /* Search for a match, ignoring alternatives that don't satisfy the
8377 current ISA. There are no separate entries for extended forms so
8378 we deal with forced_length later. */
8379 seen_valid_for_isa = FALSE;
7fd53920 8380 seen_valid_for_size = FALSE;
60f20e8b
RS
8381 opcode = first;
8382 do
8383 {
8384 gas_assert (strcmp (opcode->name, first->name) == 0);
8385 if (is_opcode_valid_16 (opcode))
8386 {
8387 seen_valid_for_isa = TRUE;
7fd53920
MR
8388 if (is_size_valid_16 (opcode))
8389 {
8390 seen_valid_for_size = TRUE;
8391 if (match_mips16_insn (insn, opcode, tokens))
8392 return TRUE;
8393 }
60f20e8b
RS
8394 }
8395 ++opcode;
8396 }
8397 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8398 && strcmp (opcode->name, first->name) == 0);
8399
8400 /* Handle the case where we didn't try to match an instruction because
8401 all the alternatives were incompatible with the current ISA. */
8402 if (!seen_valid_for_isa)
8403 {
8404 match_invalid_for_isa ();
8405 return TRUE;
8406 }
8407
7fd53920
MR
8408 /* Handle the case where we didn't try to match an instruction because
8409 all the alternatives were of the wrong size. */
8410 if (!seen_valid_for_size)
8411 {
8412 if (forced_insn_length == 2)
8413 set_insn_error
8414 (0, _("unrecognized unextended version of MIPS16 opcode"));
8415 else
8416 set_insn_error
8417 (0, _("unrecognized extended version of MIPS16 opcode"));
8418 return TRUE;
8419 }
8420
60f20e8b
RS
8421 return FALSE;
8422}
8423
584892a6
RS
8424/* Set up global variables for the start of a new macro. */
8425
8426static void
8427macro_start (void)
8428{
8429 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8430 memset (&mips_macro_warning.first_insn_sizes, 0,
8431 sizeof (mips_macro_warning.first_insn_sizes));
8432 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8433 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8434 && delayed_branch_p (&history[0]));
7bd374a4
MR
8435 if (history[0].frag
8436 && history[0].frag->fr_type == rs_machine_dependent
8437 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8438 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8439 mips_macro_warning.delay_slot_length = 0;
8440 else
8441 switch (history[0].insn_mo->pinfo2
8442 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8443 {
8444 case INSN2_BRANCH_DELAY_32BIT:
8445 mips_macro_warning.delay_slot_length = 4;
8446 break;
8447 case INSN2_BRANCH_DELAY_16BIT:
8448 mips_macro_warning.delay_slot_length = 2;
8449 break;
8450 default:
8451 mips_macro_warning.delay_slot_length = 0;
8452 break;
8453 }
df58fc94 8454 mips_macro_warning.first_frag = NULL;
584892a6
RS
8455}
8456
df58fc94
RS
8457/* Given that a macro is longer than one instruction or of the wrong size,
8458 return the appropriate warning for it. Return null if no warning is
8459 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8460 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8461 and RELAX_NOMACRO. */
584892a6
RS
8462
8463static const char *
8464macro_warning (relax_substateT subtype)
8465{
8466 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8467 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8468 " in a branch delay slot");
8469 else if (subtype & RELAX_NOMACRO)
1661c76c 8470 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8471 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8472 | RELAX_DELAY_SLOT_SIZE_SECOND))
8473 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8474 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8475 " in a 16-bit branch delay slot")
1661c76c 8476 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8477 " in a 32-bit branch delay slot"));
584892a6
RS
8478 else
8479 return 0;
8480}
8481
8482/* Finish up a macro. Emit warnings as appropriate. */
8483
8484static void
8485macro_end (void)
8486{
df58fc94
RS
8487 /* Relaxation warning flags. */
8488 relax_substateT subtype = 0;
8489
8490 /* Check delay slot size requirements. */
8491 if (mips_macro_warning.delay_slot_length == 2)
8492 subtype |= RELAX_DELAY_SLOT_16BIT;
8493 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8494 {
df58fc94
RS
8495 if (mips_macro_warning.delay_slot_length
8496 != mips_macro_warning.first_insn_sizes[0])
8497 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8498 if (mips_macro_warning.delay_slot_length
8499 != mips_macro_warning.first_insn_sizes[1])
8500 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8501 }
584892a6 8502
df58fc94
RS
8503 /* Check instruction count requirements. */
8504 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8505 {
8506 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8507 subtype |= RELAX_SECOND_LONGER;
8508 if (mips_opts.warn_about_macros)
8509 subtype |= RELAX_NOMACRO;
8510 if (mips_macro_warning.delay_slot_p)
8511 subtype |= RELAX_DELAY_SLOT;
df58fc94 8512 }
584892a6 8513
df58fc94
RS
8514 /* If both alternatives fail to fill a delay slot correctly,
8515 emit the warning now. */
8516 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8517 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8518 {
8519 relax_substateT s;
8520 const char *msg;
8521
8522 s = subtype & (RELAX_DELAY_SLOT_16BIT
8523 | RELAX_DELAY_SLOT_SIZE_FIRST
8524 | RELAX_DELAY_SLOT_SIZE_SECOND);
8525 msg = macro_warning (s);
8526 if (msg != NULL)
8527 as_warn ("%s", msg);
8528 subtype &= ~s;
8529 }
8530
8531 /* If both implementations are longer than 1 instruction, then emit the
8532 warning now. */
8533 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8534 {
8535 relax_substateT s;
8536 const char *msg;
8537
8538 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8539 msg = macro_warning (s);
8540 if (msg != NULL)
8541 as_warn ("%s", msg);
8542 subtype &= ~s;
584892a6 8543 }
df58fc94
RS
8544
8545 /* If any flags still set, then one implementation might need a warning
8546 and the other either will need one of a different kind or none at all.
8547 Pass any remaining flags over to relaxation. */
8548 if (mips_macro_warning.first_frag != NULL)
8549 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8550}
8551
df58fc94
RS
8552/* Instruction operand formats used in macros that vary between
8553 standard MIPS and microMIPS code. */
8554
833794fc 8555static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8556static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8557static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8558static const char * const lui_fmt[2] = { "t,u", "s,u" };
8559static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8560static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8561static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8562static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8563
833794fc 8564#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8565#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8566 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8567#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8568#define LUI_FMT (lui_fmt[mips_opts.micromips])
8569#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8570#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8571 : mem12_fmt[mips_opts.micromips])
833794fc 8572#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8573#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8574#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8575
6e1304d8
RS
8576/* Read a macro's relocation codes from *ARGS and store them in *R.
8577 The first argument in *ARGS will be either the code for a single
8578 relocation or -1 followed by the three codes that make up a
8579 composite relocation. */
8580
8581static void
8582macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8583{
8584 int i, next;
8585
8586 next = va_arg (*args, int);
8587 if (next >= 0)
8588 r[0] = (bfd_reloc_code_real_type) next;
8589 else
f2ae14a1
RS
8590 {
8591 for (i = 0; i < 3; i++)
8592 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8593 /* This function is only used for 16-bit relocation fields.
8594 To make the macro code simpler, treat an unrelocated value
8595 in the same way as BFD_RELOC_LO16. */
8596 if (r[0] == BFD_RELOC_UNUSED)
8597 r[0] = BFD_RELOC_LO16;
8598 }
6e1304d8
RS
8599}
8600
252b5132
RH
8601/* Build an instruction created by a macro expansion. This is passed
8602 a pointer to the count of instructions created so far, an
8603 expression, the name of the instruction to build, an operand format
8604 string, and corresponding arguments. */
8605
252b5132 8606static void
67c0d1eb 8607macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8608{
df58fc94 8609 const struct mips_opcode *mo = NULL;
f6688943 8610 bfd_reloc_code_real_type r[3];
df58fc94 8611 const struct mips_opcode *amo;
e077a1c8 8612 const struct mips_operand *operand;
df58fc94
RS
8613 struct hash_control *hash;
8614 struct mips_cl_insn insn;
252b5132 8615 va_list args;
e077a1c8 8616 unsigned int uval;
252b5132 8617
252b5132 8618 va_start (args, fmt);
252b5132 8619
252b5132
RH
8620 if (mips_opts.mips16)
8621 {
03ea81db 8622 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8623 va_end (args);
8624 return;
8625 }
8626
f6688943
TS
8627 r[0] = BFD_RELOC_UNUSED;
8628 r[1] = BFD_RELOC_UNUSED;
8629 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8630 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8631 amo = (struct mips_opcode *) hash_find (hash, name);
8632 gas_assert (amo);
8633 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8634
df58fc94 8635 do
8b082fb1
TS
8636 {
8637 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8638 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8639 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8640 slot instruction length requirement (if any) of the previous
8641 instruction. While doing this we record the first instruction
8642 seen that matches all the other conditions and use it anyway
8643 if the requirement cannot be met; we will issue an appropriate
8644 warning later on. */
8645 if (strcmp (fmt, amo->args) == 0
8646 && amo->pinfo != INSN_MACRO
8647 && is_opcode_valid (amo)
8648 && is_size_valid (amo))
8649 {
8650 if (is_delay_slot_valid (amo))
8651 {
8652 mo = amo;
8653 break;
8654 }
8655 else if (!mo)
8656 mo = amo;
8657 }
8b082fb1 8658
df58fc94
RS
8659 ++amo;
8660 gas_assert (amo->name);
252b5132 8661 }
df58fc94 8662 while (strcmp (name, amo->name) == 0);
252b5132 8663
df58fc94 8664 gas_assert (mo);
1e915849 8665 create_insn (&insn, mo);
e077a1c8 8666 for (; *fmt; ++fmt)
252b5132 8667 {
e077a1c8 8668 switch (*fmt)
252b5132 8669 {
252b5132
RH
8670 case ',':
8671 case '(':
8672 case ')':
252b5132 8673 case 'z':
e077a1c8 8674 break;
252b5132
RH
8675
8676 case 'i':
8677 case 'j':
6e1304d8 8678 macro_read_relocs (&args, r);
9c2799c2 8679 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8680 || *r == BFD_RELOC_MIPS_HIGHER
8681 || *r == BFD_RELOC_HI16_S
8682 || *r == BFD_RELOC_LO16
8683 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 8684 break;
e391c024
RS
8685
8686 case 'o':
8687 macro_read_relocs (&args, r);
e077a1c8 8688 break;
252b5132
RH
8689
8690 case 'u':
6e1304d8 8691 macro_read_relocs (&args, r);
9c2799c2 8692 gas_assert (ep != NULL
90ecf173
MR
8693 && (ep->X_op == O_constant
8694 || (ep->X_op == O_symbol
8695 && (*r == BFD_RELOC_MIPS_HIGHEST
8696 || *r == BFD_RELOC_HI16_S
8697 || *r == BFD_RELOC_HI16
8698 || *r == BFD_RELOC_GPREL16
8699 || *r == BFD_RELOC_MIPS_GOT_HI16
8700 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8701 break;
252b5132
RH
8702
8703 case 'p':
9c2799c2 8704 gas_assert (ep != NULL);
bad36eac 8705
252b5132
RH
8706 /*
8707 * This allows macro() to pass an immediate expression for
8708 * creating short branches without creating a symbol.
bad36eac
DJ
8709 *
8710 * We don't allow branch relaxation for these branches, as
8711 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8712 */
8713 if (ep->X_op == O_constant)
8714 {
df58fc94
RS
8715 /* For microMIPS we always use relocations for branches.
8716 So we should not resolve immediate values. */
8717 gas_assert (!mips_opts.micromips);
8718
bad36eac
DJ
8719 if ((ep->X_add_number & 3) != 0)
8720 as_bad (_("branch to misaligned address (0x%lx)"),
8721 (unsigned long) ep->X_add_number);
8722 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8723 as_bad (_("branch address range overflow (0x%lx)"),
8724 (unsigned long) ep->X_add_number);
252b5132
RH
8725 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8726 ep = NULL;
8727 }
8728 else
0b25d3e6 8729 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8730 break;
252b5132
RH
8731
8732 case 'a':
9c2799c2 8733 gas_assert (ep != NULL);
f6688943 8734 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8735 break;
d43b4baf 8736
252b5132 8737 default:
e077a1c8
RS
8738 operand = (mips_opts.micromips
8739 ? decode_micromips_operand (fmt)
8740 : decode_mips_operand (fmt));
8741 if (!operand)
8742 abort ();
8743
8744 uval = va_arg (args, int);
8745 if (operand->type == OP_CLO_CLZ_DEST)
8746 uval |= (uval << 5);
8747 insn_insert_operand (&insn, operand, uval);
8748
7361da2c 8749 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8750 ++fmt;
8751 break;
252b5132 8752 }
252b5132
RH
8753 }
8754 va_end (args);
9c2799c2 8755 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8756
df58fc94 8757 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8758}
8759
8760static void
67c0d1eb 8761mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8762 va_list *args)
252b5132 8763{
1e915849 8764 struct mips_opcode *mo;
252b5132 8765 struct mips_cl_insn insn;
e077a1c8 8766 const struct mips_operand *operand;
f6688943
TS
8767 bfd_reloc_code_real_type r[3]
8768 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8769
1e915849 8770 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8771 gas_assert (mo);
8772 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8773
1e915849 8774 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8775 {
1e915849 8776 ++mo;
9c2799c2
NC
8777 gas_assert (mo->name);
8778 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8779 }
8780
1e915849 8781 create_insn (&insn, mo);
e077a1c8 8782 for (; *fmt; ++fmt)
252b5132
RH
8783 {
8784 int c;
8785
e077a1c8 8786 c = *fmt;
252b5132
RH
8787 switch (c)
8788 {
252b5132
RH
8789 case ',':
8790 case '(':
8791 case ')':
e077a1c8 8792 break;
252b5132 8793
d8722d76 8794 case '.':
252b5132
RH
8795 case 'S':
8796 case 'P':
8797 case 'R':
e077a1c8 8798 break;
252b5132
RH
8799
8800 case '<':
252b5132 8801 case '5':
d8722d76 8802 case 'F':
252b5132
RH
8803 case 'H':
8804 case 'W':
8805 case 'D':
8806 case 'j':
8807 case '8':
8808 case 'V':
8809 case 'C':
8810 case 'U':
8811 case 'k':
8812 case 'K':
8813 case 'p':
8814 case 'q':
8815 {
b886a2ab
RS
8816 offsetT value;
8817
9c2799c2 8818 gas_assert (ep != NULL);
252b5132
RH
8819
8820 if (ep->X_op != O_constant)
874e8986 8821 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8822 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8823 {
b886a2ab 8824 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8825 ep = NULL;
f6688943 8826 *r = BFD_RELOC_UNUSED;
252b5132
RH
8827 }
8828 }
e077a1c8 8829 break;
252b5132 8830
e077a1c8
RS
8831 default:
8832 operand = decode_mips16_operand (c, FALSE);
8833 if (!operand)
8834 abort ();
252b5132 8835
4a06e5a2 8836 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8837 break;
8838 }
252b5132
RH
8839 }
8840
9c2799c2 8841 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8842
df58fc94 8843 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8844}
8845
438c16b8
TS
8846/*
8847 * Generate a "jalr" instruction with a relocation hint to the called
8848 * function. This occurs in NewABI PIC code.
8849 */
8850static void
df58fc94 8851macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 8852{
df58fc94
RS
8853 static const bfd_reloc_code_real_type jalr_relocs[2]
8854 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8855 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8856 const char *jalr;
685736be 8857 char *f = NULL;
b34976b6 8858
1180b5a4 8859 if (MIPS_JALR_HINT_P (ep))
f21f8242 8860 {
cc3d92a5 8861 frag_grow (8);
f21f8242
AO
8862 f = frag_more (0);
8863 }
2906b037 8864 if (mips_opts.micromips)
df58fc94 8865 {
833794fc
MR
8866 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8867 ? "jalr" : "jalrs");
e64af278 8868 if (MIPS_JALR_HINT_P (ep)
833794fc 8869 || mips_opts.insn32
e64af278 8870 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8871 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8872 else
8873 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8874 }
2906b037
MR
8875 else
8876 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 8877 if (MIPS_JALR_HINT_P (ep))
df58fc94 8878 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
8879}
8880
252b5132
RH
8881/*
8882 * Generate a "lui" instruction.
8883 */
8884static void
67c0d1eb 8885macro_build_lui (expressionS *ep, int regnum)
252b5132 8886{
9c2799c2 8887 gas_assert (! mips_opts.mips16);
252b5132 8888
df58fc94 8889 if (ep->X_op != O_constant)
252b5132 8890 {
9c2799c2 8891 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8892 /* _gp_disp is a special case, used from s_cpload.
8893 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8894 gas_assert (mips_pic == NO_PIC
78e1bb40 8895 || (! HAVE_NEWABI
aa6975fb
ILT
8896 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8897 || (! mips_in_shared
bbe506e8
TS
8898 && strcmp (S_GET_NAME (ep->X_add_symbol),
8899 "__gnu_local_gp") == 0));
252b5132
RH
8900 }
8901
df58fc94 8902 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8903}
8904
885add95
CD
8905/* Generate a sequence of instructions to do a load or store from a constant
8906 offset off of a base register (breg) into/from a target register (treg),
8907 using AT if necessary. */
8908static void
67c0d1eb
RS
8909macro_build_ldst_constoffset (expressionS *ep, const char *op,
8910 int treg, int breg, int dbl)
885add95 8911{
9c2799c2 8912 gas_assert (ep->X_op == O_constant);
885add95 8913
256ab948 8914 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8915 if (!dbl)
8916 normalize_constant_expr (ep);
256ab948 8917
67c1ffbe 8918 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8919 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8920 as_warn (_("operand overflow"));
8921
8922 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8923 {
8924 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8925 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8926 }
8927 else
8928 {
8929 /* 32-bit offset, need multiple instructions and AT, like:
8930 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8931 addu $tempreg,$tempreg,$breg
8932 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8933 to handle the complete offset. */
67c0d1eb
RS
8934 macro_build_lui (ep, AT);
8935 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8936 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8937
741fe287 8938 if (!mips_opts.at)
1661c76c 8939 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
8940 }
8941}
8942
252b5132
RH
8943/* set_at()
8944 * Generates code to set the $at register to true (one)
8945 * if reg is less than the immediate expression.
8946 */
8947static void
67c0d1eb 8948set_at (int reg, int unsignedp)
252b5132 8949{
b0e6f033 8950 if (imm_expr.X_add_number >= -0x8000
252b5132 8951 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8952 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8953 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8954 else
8955 {
bad1aba3 8956 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 8957 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8958 }
8959}
8960
252b5132
RH
8961/* Count the leading zeroes by performing a binary chop. This is a
8962 bulky bit of source, but performance is a LOT better for the
8963 majority of values than a simple loop to count the bits:
8964 for (lcnt = 0; (lcnt < 32); lcnt++)
8965 if ((v) & (1 << (31 - lcnt)))
8966 break;
8967 However it is not code size friendly, and the gain will drop a bit
8968 on certain cached systems.
8969*/
8970#define COUNT_TOP_ZEROES(v) \
8971 (((v) & ~0xffff) == 0 \
8972 ? ((v) & ~0xff) == 0 \
8973 ? ((v) & ~0xf) == 0 \
8974 ? ((v) & ~0x3) == 0 \
8975 ? ((v) & ~0x1) == 0 \
8976 ? !(v) \
8977 ? 32 \
8978 : 31 \
8979 : 30 \
8980 : ((v) & ~0x7) == 0 \
8981 ? 29 \
8982 : 28 \
8983 : ((v) & ~0x3f) == 0 \
8984 ? ((v) & ~0x1f) == 0 \
8985 ? 27 \
8986 : 26 \
8987 : ((v) & ~0x7f) == 0 \
8988 ? 25 \
8989 : 24 \
8990 : ((v) & ~0xfff) == 0 \
8991 ? ((v) & ~0x3ff) == 0 \
8992 ? ((v) & ~0x1ff) == 0 \
8993 ? 23 \
8994 : 22 \
8995 : ((v) & ~0x7ff) == 0 \
8996 ? 21 \
8997 : 20 \
8998 : ((v) & ~0x3fff) == 0 \
8999 ? ((v) & ~0x1fff) == 0 \
9000 ? 19 \
9001 : 18 \
9002 : ((v) & ~0x7fff) == 0 \
9003 ? 17 \
9004 : 16 \
9005 : ((v) & ~0xffffff) == 0 \
9006 ? ((v) & ~0xfffff) == 0 \
9007 ? ((v) & ~0x3ffff) == 0 \
9008 ? ((v) & ~0x1ffff) == 0 \
9009 ? 15 \
9010 : 14 \
9011 : ((v) & ~0x7ffff) == 0 \
9012 ? 13 \
9013 : 12 \
9014 : ((v) & ~0x3fffff) == 0 \
9015 ? ((v) & ~0x1fffff) == 0 \
9016 ? 11 \
9017 : 10 \
9018 : ((v) & ~0x7fffff) == 0 \
9019 ? 9 \
9020 : 8 \
9021 : ((v) & ~0xfffffff) == 0 \
9022 ? ((v) & ~0x3ffffff) == 0 \
9023 ? ((v) & ~0x1ffffff) == 0 \
9024 ? 7 \
9025 : 6 \
9026 : ((v) & ~0x7ffffff) == 0 \
9027 ? 5 \
9028 : 4 \
9029 : ((v) & ~0x3fffffff) == 0 \
9030 ? ((v) & ~0x1fffffff) == 0 \
9031 ? 3 \
9032 : 2 \
9033 : ((v) & ~0x7fffffff) == 0 \
9034 ? 1 \
9035 : 0)
9036
9037/* load_register()
67c1ffbe 9038 * This routine generates the least number of instructions necessary to load
252b5132
RH
9039 * an absolute expression value into a register.
9040 */
9041static void
67c0d1eb 9042load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9043{
9044 int freg;
9045 expressionS hi32, lo32;
9046
9047 if (ep->X_op != O_big)
9048 {
9c2799c2 9049 gas_assert (ep->X_op == O_constant);
256ab948
TS
9050
9051 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9052 if (!dbl)
9053 normalize_constant_expr (ep);
256ab948
TS
9054
9055 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9056 {
9057 /* We can handle 16 bit signed values with an addiu to
9058 $zero. No need to ever use daddiu here, since $zero and
9059 the result are always correct in 32 bit mode. */
67c0d1eb 9060 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9061 return;
9062 }
9063 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9064 {
9065 /* We can handle 16 bit unsigned values with an ori to
9066 $zero. */
67c0d1eb 9067 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9068 return;
9069 }
256ab948 9070 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9071 {
9072 /* 32 bit values require an lui. */
df58fc94 9073 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9074 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9075 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9076 return;
9077 }
9078 }
9079
9080 /* The value is larger than 32 bits. */
9081
bad1aba3 9082 if (!dbl || GPR_SIZE == 32)
252b5132 9083 {
55e08f71
NC
9084 char value[32];
9085
9086 sprintf_vma (value, ep->X_add_number);
1661c76c 9087 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9088 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9089 return;
9090 }
9091
9092 if (ep->X_op != O_big)
9093 {
9094 hi32 = *ep;
9095 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9096 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9097 hi32.X_add_number &= 0xffffffff;
9098 lo32 = *ep;
9099 lo32.X_add_number &= 0xffffffff;
9100 }
9101 else
9102 {
9c2799c2 9103 gas_assert (ep->X_add_number > 2);
252b5132
RH
9104 if (ep->X_add_number == 3)
9105 generic_bignum[3] = 0;
9106 else if (ep->X_add_number > 4)
1661c76c 9107 as_bad (_("number larger than 64 bits"));
252b5132
RH
9108 lo32.X_op = O_constant;
9109 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9110 hi32.X_op = O_constant;
9111 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9112 }
9113
9114 if (hi32.X_add_number == 0)
9115 freg = 0;
9116 else
9117 {
9118 int shift, bit;
9119 unsigned long hi, lo;
9120
956cd1d6 9121 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9122 {
9123 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9124 {
67c0d1eb 9125 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9126 return;
9127 }
9128 if (lo32.X_add_number & 0x80000000)
9129 {
df58fc94 9130 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9131 if (lo32.X_add_number & 0xffff)
67c0d1eb 9132 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9133 return;
9134 }
9135 }
252b5132
RH
9136
9137 /* Check for 16bit shifted constant. We know that hi32 is
9138 non-zero, so start the mask on the first bit of the hi32
9139 value. */
9140 shift = 17;
9141 do
beae10d5
KH
9142 {
9143 unsigned long himask, lomask;
9144
9145 if (shift < 32)
9146 {
9147 himask = 0xffff >> (32 - shift);
9148 lomask = (0xffff << shift) & 0xffffffff;
9149 }
9150 else
9151 {
9152 himask = 0xffff << (shift - 32);
9153 lomask = 0;
9154 }
9155 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9156 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9157 {
9158 expressionS tmp;
9159
9160 tmp.X_op = O_constant;
9161 if (shift < 32)
9162 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9163 | (lo32.X_add_number >> shift));
9164 else
9165 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9166 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9167 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9168 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9169 return;
9170 }
f9419b05 9171 ++shift;
beae10d5
KH
9172 }
9173 while (shift <= (64 - 16));
252b5132
RH
9174
9175 /* Find the bit number of the lowest one bit, and store the
9176 shifted value in hi/lo. */
9177 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9178 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9179 if (lo != 0)
9180 {
9181 bit = 0;
9182 while ((lo & 1) == 0)
9183 {
9184 lo >>= 1;
9185 ++bit;
9186 }
9187 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9188 hi >>= bit;
9189 }
9190 else
9191 {
9192 bit = 32;
9193 while ((hi & 1) == 0)
9194 {
9195 hi >>= 1;
9196 ++bit;
9197 }
9198 lo = hi;
9199 hi = 0;
9200 }
9201
9202 /* Optimize if the shifted value is a (power of 2) - 1. */
9203 if ((hi == 0 && ((lo + 1) & lo) == 0)
9204 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9205 {
9206 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9207 if (shift != 0)
beae10d5 9208 {
252b5132
RH
9209 expressionS tmp;
9210
9211 /* This instruction will set the register to be all
9212 ones. */
beae10d5
KH
9213 tmp.X_op = O_constant;
9214 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9215 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9216 if (bit != 0)
9217 {
9218 bit += shift;
df58fc94 9219 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9220 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9221 }
df58fc94 9222 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9223 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9224 return;
9225 }
9226 }
252b5132
RH
9227
9228 /* Sign extend hi32 before calling load_register, because we can
9229 generally get better code when we load a sign extended value. */
9230 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9231 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9232 load_register (reg, &hi32, 0);
252b5132
RH
9233 freg = reg;
9234 }
9235 if ((lo32.X_add_number & 0xffff0000) == 0)
9236 {
9237 if (freg != 0)
9238 {
df58fc94 9239 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9240 freg = reg;
9241 }
9242 }
9243 else
9244 {
9245 expressionS mid16;
9246
956cd1d6 9247 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9248 {
df58fc94
RS
9249 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9250 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9251 return;
9252 }
252b5132
RH
9253
9254 if (freg != 0)
9255 {
df58fc94 9256 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9257 freg = reg;
9258 }
9259 mid16 = lo32;
9260 mid16.X_add_number >>= 16;
67c0d1eb 9261 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9262 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9263 freg = reg;
9264 }
9265 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9266 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9267}
9268
269137b2
TS
9269static inline void
9270load_delay_nop (void)
9271{
9272 if (!gpr_interlocks)
9273 macro_build (NULL, "nop", "");
9274}
9275
252b5132
RH
9276/* Load an address into a register. */
9277
9278static void
67c0d1eb 9279load_address (int reg, expressionS *ep, int *used_at)
252b5132 9280{
252b5132
RH
9281 if (ep->X_op != O_constant
9282 && ep->X_op != O_symbol)
9283 {
9284 as_bad (_("expression too complex"));
9285 ep->X_op = O_constant;
9286 }
9287
9288 if (ep->X_op == O_constant)
9289 {
67c0d1eb 9290 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9291 return;
9292 }
9293
9294 if (mips_pic == NO_PIC)
9295 {
9296 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9297 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9298 Otherwise we want
9299 lui $reg,<sym> (BFD_RELOC_HI16_S)
9300 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9301 If we have an addend, we always use the latter form.
76b3015f 9302
d6bc6245
TS
9303 With 64bit address space and a usable $at we want
9304 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9305 lui $at,<sym> (BFD_RELOC_HI16_S)
9306 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9307 daddiu $at,<sym> (BFD_RELOC_LO16)
9308 dsll32 $reg,0
3a482fd5 9309 daddu $reg,$reg,$at
76b3015f 9310
c03099e6 9311 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9312 on superscalar processors.
9313 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9314 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9315 dsll $reg,16
9316 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9317 dsll $reg,16
9318 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9319
9320 For GP relative symbols in 64bit address space we can use
9321 the same sequence as in 32bit address space. */
aed1a261 9322 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9323 {
6caf9ef4
TS
9324 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9325 && !nopic_need_relax (ep->X_add_symbol, 1))
9326 {
9327 relax_start (ep->X_add_symbol);
9328 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9329 mips_gp_register, BFD_RELOC_GPREL16);
9330 relax_switch ();
9331 }
d6bc6245 9332
741fe287 9333 if (*used_at == 0 && mips_opts.at)
d6bc6245 9334 {
df58fc94
RS
9335 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9336 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9337 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9338 BFD_RELOC_MIPS_HIGHER);
9339 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9340 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9341 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9342 *used_at = 1;
9343 }
9344 else
9345 {
df58fc94 9346 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9347 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9348 BFD_RELOC_MIPS_HIGHER);
df58fc94 9349 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9350 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9351 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9352 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9353 }
6caf9ef4
TS
9354
9355 if (mips_relax.sequence)
9356 relax_end ();
d6bc6245 9357 }
252b5132
RH
9358 else
9359 {
d6bc6245 9360 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9361 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9362 {
4d7206a2 9363 relax_start (ep->X_add_symbol);
67c0d1eb 9364 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9365 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9366 relax_switch ();
d6bc6245 9367 }
67c0d1eb
RS
9368 macro_build_lui (ep, reg);
9369 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9370 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9371 if (mips_relax.sequence)
9372 relax_end ();
d6bc6245 9373 }
252b5132 9374 }
0a44bf69 9375 else if (!mips_big_got)
252b5132
RH
9376 {
9377 expressionS ex;
9378
9379 /* If this is a reference to an external symbol, we want
9380 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9381 Otherwise we want
9382 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9383 nop
9384 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9385 If there is a constant, it must be added in after.
9386
ed6fb7bd 9387 If we have NewABI, we want
f5040a92
AO
9388 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9389 unless we're referencing a global symbol with a non-zero
9390 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9391 if (HAVE_NEWABI)
9392 {
f5040a92
AO
9393 if (ep->X_add_number)
9394 {
4d7206a2 9395 ex.X_add_number = ep->X_add_number;
f5040a92 9396 ep->X_add_number = 0;
4d7206a2 9397 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9398 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9399 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9400 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9401 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9402 ex.X_op = O_constant;
67c0d1eb 9403 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9404 reg, reg, BFD_RELOC_LO16);
f5040a92 9405 ep->X_add_number = ex.X_add_number;
4d7206a2 9406 relax_switch ();
f5040a92 9407 }
67c0d1eb 9408 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9409 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9410 if (mips_relax.sequence)
9411 relax_end ();
ed6fb7bd
SC
9412 }
9413 else
9414 {
f5040a92
AO
9415 ex.X_add_number = ep->X_add_number;
9416 ep->X_add_number = 0;
67c0d1eb
RS
9417 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9418 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9419 load_delay_nop ();
4d7206a2
RS
9420 relax_start (ep->X_add_symbol);
9421 relax_switch ();
67c0d1eb 9422 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9423 BFD_RELOC_LO16);
4d7206a2 9424 relax_end ();
ed6fb7bd 9425
f5040a92
AO
9426 if (ex.X_add_number != 0)
9427 {
9428 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9429 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9430 ex.X_op = O_constant;
67c0d1eb 9431 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9432 reg, reg, BFD_RELOC_LO16);
f5040a92 9433 }
252b5132
RH
9434 }
9435 }
0a44bf69 9436 else if (mips_big_got)
252b5132
RH
9437 {
9438 expressionS ex;
252b5132
RH
9439
9440 /* This is the large GOT case. If this is a reference to an
9441 external symbol, we want
9442 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9443 addu $reg,$reg,$gp
9444 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9445
9446 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9447 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9448 nop
9449 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9450 If there is a constant, it must be added in after.
f5040a92
AO
9451
9452 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9453 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9454 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9455 */
438c16b8
TS
9456 if (HAVE_NEWABI)
9457 {
4d7206a2 9458 ex.X_add_number = ep->X_add_number;
f5040a92 9459 ep->X_add_number = 0;
4d7206a2 9460 relax_start (ep->X_add_symbol);
df58fc94 9461 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9462 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9463 reg, reg, mips_gp_register);
9464 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9465 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9466 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9467 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9468 else if (ex.X_add_number)
9469 {
9470 ex.X_op = O_constant;
67c0d1eb
RS
9471 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9472 BFD_RELOC_LO16);
f5040a92
AO
9473 }
9474
9475 ep->X_add_number = ex.X_add_number;
4d7206a2 9476 relax_switch ();
67c0d1eb 9477 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9478 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9479 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9480 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9481 relax_end ();
438c16b8 9482 }
252b5132 9483 else
438c16b8 9484 {
f5040a92
AO
9485 ex.X_add_number = ep->X_add_number;
9486 ep->X_add_number = 0;
4d7206a2 9487 relax_start (ep->X_add_symbol);
df58fc94 9488 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9489 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9490 reg, reg, mips_gp_register);
9491 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9492 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9493 relax_switch ();
9494 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9495 {
9496 /* We need a nop before loading from $gp. This special
9497 check is required because the lui which starts the main
9498 instruction stream does not refer to $gp, and so will not
9499 insert the nop which may be required. */
67c0d1eb 9500 macro_build (NULL, "nop", "");
438c16b8 9501 }
67c0d1eb 9502 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9503 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9504 load_delay_nop ();
67c0d1eb 9505 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9506 BFD_RELOC_LO16);
4d7206a2 9507 relax_end ();
438c16b8 9508
f5040a92
AO
9509 if (ex.X_add_number != 0)
9510 {
9511 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9512 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9513 ex.X_op = O_constant;
67c0d1eb
RS
9514 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9515 BFD_RELOC_LO16);
f5040a92 9516 }
252b5132
RH
9517 }
9518 }
252b5132
RH
9519 else
9520 abort ();
8fc2e39e 9521
741fe287 9522 if (!mips_opts.at && *used_at == 1)
1661c76c 9523 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9524}
9525
ea1fb5dc
RS
9526/* Move the contents of register SOURCE into register DEST. */
9527
9528static void
67c0d1eb 9529move_register (int dest, int source)
ea1fb5dc 9530{
df58fc94
RS
9531 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9532 instruction specifically requires a 32-bit one. */
9533 if (mips_opts.micromips
833794fc 9534 && !mips_opts.insn32
df58fc94 9535 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9536 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9537 else
40fc1451 9538 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9539}
9540
4d7206a2 9541/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9542 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9543 The two alternatives are:
4d7206a2 9544
33eaf5de 9545 Global symbol Local symbol
4d7206a2
RS
9546 ------------- ------------
9547 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9548 ... ...
9549 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9550
9551 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9552 emits the second for a 16-bit offset or add_got_offset_hilo emits
9553 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9554
9555static void
67c0d1eb 9556load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9557{
9558 expressionS global;
9559
9560 global = *local;
9561 global.X_add_number = 0;
9562
9563 relax_start (local->X_add_symbol);
67c0d1eb
RS
9564 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9565 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9566 relax_switch ();
67c0d1eb
RS
9567 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9568 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9569 relax_end ();
9570}
9571
9572static void
67c0d1eb 9573add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9574{
9575 expressionS global;
9576
9577 global.X_op = O_constant;
9578 global.X_op_symbol = NULL;
9579 global.X_add_symbol = NULL;
9580 global.X_add_number = local->X_add_number;
9581
9582 relax_start (local->X_add_symbol);
67c0d1eb 9583 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9584 dest, dest, BFD_RELOC_LO16);
9585 relax_switch ();
67c0d1eb 9586 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9587 relax_end ();
9588}
9589
f6a22291
MR
9590static void
9591add_got_offset_hilo (int dest, expressionS *local, int tmp)
9592{
9593 expressionS global;
9594 int hold_mips_optimize;
9595
9596 global.X_op = O_constant;
9597 global.X_op_symbol = NULL;
9598 global.X_add_symbol = NULL;
9599 global.X_add_number = local->X_add_number;
9600
9601 relax_start (local->X_add_symbol);
9602 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9603 relax_switch ();
9604 /* Set mips_optimize around the lui instruction to avoid
9605 inserting an unnecessary nop after the lw. */
9606 hold_mips_optimize = mips_optimize;
9607 mips_optimize = 2;
9608 macro_build_lui (&global, tmp);
9609 mips_optimize = hold_mips_optimize;
9610 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9611 relax_end ();
9612
9613 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9614}
9615
df58fc94
RS
9616/* Emit a sequence of instructions to emulate a branch likely operation.
9617 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9618 is its complementing branch with the original condition negated.
9619 CALL is set if the original branch specified the link operation.
9620 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9621
9622 Code like this is produced in the noreorder mode:
9623
9624 BRNEG <args>, 1f
9625 nop
9626 b <sym>
9627 delay slot (executed only if branch taken)
9628 1:
9629
9630 or, if CALL is set:
9631
9632 BRNEG <args>, 1f
9633 nop
9634 bal <sym>
9635 delay slot (executed only if branch taken)
9636 1:
9637
9638 In the reorder mode the delay slot would be filled with a nop anyway,
9639 so code produced is simply:
9640
9641 BR <args>, <sym>
9642 nop
9643
9644 This function is used when producing code for the microMIPS ASE that
9645 does not implement branch likely instructions in hardware. */
9646
9647static void
9648macro_build_branch_likely (const char *br, const char *brneg,
9649 int call, expressionS *ep, const char *fmt,
9650 unsigned int sreg, unsigned int treg)
9651{
9652 int noreorder = mips_opts.noreorder;
9653 expressionS expr1;
9654
9655 gas_assert (mips_opts.micromips);
9656 start_noreorder ();
9657 if (noreorder)
9658 {
9659 micromips_label_expr (&expr1);
9660 macro_build (&expr1, brneg, fmt, sreg, treg);
9661 macro_build (NULL, "nop", "");
9662 macro_build (ep, call ? "bal" : "b", "p");
9663
9664 /* Set to true so that append_insn adds a label. */
9665 emit_branch_likely_macro = TRUE;
9666 }
9667 else
9668 {
9669 macro_build (ep, br, fmt, sreg, treg);
9670 macro_build (NULL, "nop", "");
9671 }
9672 end_noreorder ();
9673}
9674
9675/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9676 the condition code tested. EP specifies the branch target. */
9677
9678static void
9679macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9680{
9681 const int call = 0;
9682 const char *brneg;
9683 const char *br;
9684
9685 switch (type)
9686 {
9687 case M_BC1FL:
9688 br = "bc1f";
9689 brneg = "bc1t";
9690 break;
9691 case M_BC1TL:
9692 br = "bc1t";
9693 brneg = "bc1f";
9694 break;
9695 case M_BC2FL:
9696 br = "bc2f";
9697 brneg = "bc2t";
9698 break;
9699 case M_BC2TL:
9700 br = "bc2t";
9701 brneg = "bc2f";
9702 break;
9703 default:
9704 abort ();
9705 }
9706 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9707}
9708
9709/* Emit a two-argument branch macro specified by TYPE, using SREG as
9710 the register tested. EP specifies the branch target. */
9711
9712static void
9713macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9714{
9715 const char *brneg = NULL;
9716 const char *br;
9717 int call = 0;
9718
9719 switch (type)
9720 {
9721 case M_BGEZ:
9722 br = "bgez";
9723 break;
9724 case M_BGEZL:
9725 br = mips_opts.micromips ? "bgez" : "bgezl";
9726 brneg = "bltz";
9727 break;
9728 case M_BGEZALL:
9729 gas_assert (mips_opts.micromips);
833794fc 9730 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9731 brneg = "bltz";
9732 call = 1;
9733 break;
9734 case M_BGTZ:
9735 br = "bgtz";
9736 break;
9737 case M_BGTZL:
9738 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9739 brneg = "blez";
9740 break;
9741 case M_BLEZ:
9742 br = "blez";
9743 break;
9744 case M_BLEZL:
9745 br = mips_opts.micromips ? "blez" : "blezl";
9746 brneg = "bgtz";
9747 break;
9748 case M_BLTZ:
9749 br = "bltz";
9750 break;
9751 case M_BLTZL:
9752 br = mips_opts.micromips ? "bltz" : "bltzl";
9753 brneg = "bgez";
9754 break;
9755 case M_BLTZALL:
9756 gas_assert (mips_opts.micromips);
833794fc 9757 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9758 brneg = "bgez";
9759 call = 1;
9760 break;
9761 default:
9762 abort ();
9763 }
9764 if (mips_opts.micromips && brneg)
9765 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9766 else
9767 macro_build (ep, br, "s,p", sreg);
9768}
9769
9770/* Emit a three-argument branch macro specified by TYPE, using SREG and
9771 TREG as the registers tested. EP specifies the branch target. */
9772
9773static void
9774macro_build_branch_rsrt (int type, expressionS *ep,
9775 unsigned int sreg, unsigned int treg)
9776{
9777 const char *brneg = NULL;
9778 const int call = 0;
9779 const char *br;
9780
9781 switch (type)
9782 {
9783 case M_BEQ:
9784 case M_BEQ_I:
9785 br = "beq";
9786 break;
9787 case M_BEQL:
9788 case M_BEQL_I:
9789 br = mips_opts.micromips ? "beq" : "beql";
9790 brneg = "bne";
9791 break;
9792 case M_BNE:
9793 case M_BNE_I:
9794 br = "bne";
9795 break;
9796 case M_BNEL:
9797 case M_BNEL_I:
9798 br = mips_opts.micromips ? "bne" : "bnel";
9799 brneg = "beq";
9800 break;
9801 default:
9802 abort ();
9803 }
9804 if (mips_opts.micromips && brneg)
9805 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9806 else
9807 macro_build (ep, br, "s,t,p", sreg, treg);
9808}
9809
f2ae14a1
RS
9810/* Return the high part that should be loaded in order to make the low
9811 part of VALUE accessible using an offset of OFFBITS bits. */
9812
9813static offsetT
9814offset_high_part (offsetT value, unsigned int offbits)
9815{
9816 offsetT bias;
9817 addressT low_mask;
9818
9819 if (offbits == 0)
9820 return value;
9821 bias = 1 << (offbits - 1);
9822 low_mask = bias * 2 - 1;
9823 return (value + bias) & ~low_mask;
9824}
9825
9826/* Return true if the value stored in offset_expr and offset_reloc
9827 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9828 amount that the caller wants to add without inducing overflow
9829 and ALIGN is the known alignment of the value in bytes. */
9830
9831static bfd_boolean
9832small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9833{
9834 if (offbits == 16)
9835 {
9836 /* Accept any relocation operator if overflow isn't a concern. */
9837 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9838 return TRUE;
9839
9840 /* These relocations are guaranteed not to overflow in correct links. */
9841 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9842 || gprel16_reloc_p (*offset_reloc))
9843 return TRUE;
9844 }
9845 if (offset_expr.X_op == O_constant
9846 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9847 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9848 return TRUE;
9849 return FALSE;
9850}
9851
252b5132
RH
9852/*
9853 * Build macros
9854 * This routine implements the seemingly endless macro or synthesized
9855 * instructions and addressing modes in the mips assembly language. Many
9856 * of these macros are simple and are similar to each other. These could
67c1ffbe 9857 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
9858 * this verbose method. Others are not simple macros but are more like
9859 * optimizing code generation.
9860 * One interesting optimization is when several store macros appear
67c1ffbe 9861 * consecutively that would load AT with the upper half of the same address.
2b0f3761 9862 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
9863 * of global optimization. We currently only optimize within a single macro.
9864 * For many of the load and store macros if the address is specified as a
9865 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9866 * first load register 'at' with zero and use it as the base register. The
9867 * mips assembler simply uses register $zero. Just one tiny optimization
9868 * we're missing.
9869 */
9870static void
833794fc 9871macro (struct mips_cl_insn *ip, char *str)
252b5132 9872{
c0ebe874
RS
9873 const struct mips_operand_array *operands;
9874 unsigned int breg, i;
741fe287 9875 unsigned int tempreg;
252b5132 9876 int mask;
43841e91 9877 int used_at = 0;
df58fc94 9878 expressionS label_expr;
252b5132 9879 expressionS expr1;
df58fc94 9880 expressionS *ep;
252b5132
RH
9881 const char *s;
9882 const char *s2;
9883 const char *fmt;
9884 int likely = 0;
252b5132 9885 int coproc = 0;
7f3c4072 9886 int offbits = 16;
1abe91b1 9887 int call = 0;
df58fc94
RS
9888 int jals = 0;
9889 int dbl = 0;
9890 int imm = 0;
9891 int ust = 0;
9892 int lp = 0;
f2ae14a1 9893 bfd_boolean large_offset;
252b5132 9894 int off;
252b5132 9895 int hold_mips_optimize;
f2ae14a1 9896 unsigned int align;
c0ebe874 9897 unsigned int op[MAX_OPERANDS];
252b5132 9898
9c2799c2 9899 gas_assert (! mips_opts.mips16);
252b5132 9900
c0ebe874
RS
9901 operands = insn_operands (ip);
9902 for (i = 0; i < MAX_OPERANDS; i++)
9903 if (operands->operand[i])
9904 op[i] = insn_extract_operand (ip, operands->operand[i]);
9905 else
9906 op[i] = -1;
9907
252b5132
RH
9908 mask = ip->insn_mo->mask;
9909
df58fc94
RS
9910 label_expr.X_op = O_constant;
9911 label_expr.X_op_symbol = NULL;
9912 label_expr.X_add_symbol = NULL;
9913 label_expr.X_add_number = 0;
9914
252b5132
RH
9915 expr1.X_op = O_constant;
9916 expr1.X_op_symbol = NULL;
9917 expr1.X_add_symbol = NULL;
9918 expr1.X_add_number = 1;
f2ae14a1 9919 align = 1;
252b5132
RH
9920
9921 switch (mask)
9922 {
9923 case M_DABS:
9924 dbl = 1;
1a0670f3 9925 /* Fall through. */
252b5132 9926 case M_ABS:
df58fc94
RS
9927 /* bgez $a0,1f
9928 move v0,$a0
9929 sub v0,$zero,$a0
9930 1:
9931 */
252b5132 9932
7d10b47d 9933 start_noreorder ();
252b5132 9934
df58fc94
RS
9935 if (mips_opts.micromips)
9936 micromips_label_expr (&label_expr);
9937 else
9938 label_expr.X_add_number = 8;
c0ebe874
RS
9939 macro_build (&label_expr, "bgez", "s,p", op[1]);
9940 if (op[0] == op[1])
a605d2b3 9941 macro_build (NULL, "nop", "");
252b5132 9942 else
c0ebe874
RS
9943 move_register (op[0], op[1]);
9944 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9945 if (mips_opts.micromips)
9946 micromips_add_label ();
252b5132 9947
7d10b47d 9948 end_noreorder ();
8fc2e39e 9949 break;
252b5132
RH
9950
9951 case M_ADD_I:
9952 s = "addi";
9953 s2 = "add";
9954 goto do_addi;
9955 case M_ADDU_I:
9956 s = "addiu";
9957 s2 = "addu";
9958 goto do_addi;
9959 case M_DADD_I:
9960 dbl = 1;
9961 s = "daddi";
9962 s2 = "dadd";
df58fc94
RS
9963 if (!mips_opts.micromips)
9964 goto do_addi;
b0e6f033 9965 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
9966 && imm_expr.X_add_number < 0x200)
9967 {
b0e6f033
RS
9968 macro_build (NULL, s, "t,r,.", op[0], op[1],
9969 (int) imm_expr.X_add_number);
df58fc94
RS
9970 break;
9971 }
9972 goto do_addi_i;
252b5132
RH
9973 case M_DADDU_I:
9974 dbl = 1;
9975 s = "daddiu";
9976 s2 = "daddu";
9977 do_addi:
b0e6f033 9978 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
9979 && imm_expr.X_add_number < 0x8000)
9980 {
c0ebe874 9981 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 9982 break;
252b5132 9983 }
df58fc94 9984 do_addi_i:
8fc2e39e 9985 used_at = 1;
67c0d1eb 9986 load_register (AT, &imm_expr, dbl);
c0ebe874 9987 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9988 break;
9989
9990 case M_AND_I:
9991 s = "andi";
9992 s2 = "and";
9993 goto do_bit;
9994 case M_OR_I:
9995 s = "ori";
9996 s2 = "or";
9997 goto do_bit;
9998 case M_NOR_I:
9999 s = "";
10000 s2 = "nor";
10001 goto do_bit;
10002 case M_XOR_I:
10003 s = "xori";
10004 s2 = "xor";
10005 do_bit:
b0e6f033 10006 if (imm_expr.X_add_number >= 0
252b5132
RH
10007 && imm_expr.X_add_number < 0x10000)
10008 {
10009 if (mask != M_NOR_I)
c0ebe874 10010 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10011 else
10012 {
67c0d1eb 10013 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10014 op[0], op[1], BFD_RELOC_LO16);
10015 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10016 }
8fc2e39e 10017 break;
252b5132
RH
10018 }
10019
8fc2e39e 10020 used_at = 1;
bad1aba3 10021 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10022 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10023 break;
10024
8b082fb1
TS
10025 case M_BALIGN:
10026 switch (imm_expr.X_add_number)
10027 {
10028 case 0:
10029 macro_build (NULL, "nop", "");
10030 break;
10031 case 2:
c0ebe874 10032 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10033 break;
03f66e8a
MR
10034 case 1:
10035 case 3:
c0ebe874 10036 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10037 (int) imm_expr.X_add_number);
8b082fb1 10038 break;
03f66e8a
MR
10039 default:
10040 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10041 (unsigned long) imm_expr.X_add_number);
10042 break;
8b082fb1
TS
10043 }
10044 break;
10045
df58fc94
RS
10046 case M_BC1FL:
10047 case M_BC1TL:
10048 case M_BC2FL:
10049 case M_BC2TL:
10050 gas_assert (mips_opts.micromips);
10051 macro_build_branch_ccl (mask, &offset_expr,
10052 EXTRACT_OPERAND (1, BCC, *ip));
10053 break;
10054
252b5132 10055 case M_BEQ_I:
252b5132 10056 case M_BEQL_I:
252b5132 10057 case M_BNE_I:
252b5132 10058 case M_BNEL_I:
b0e6f033 10059 if (imm_expr.X_add_number == 0)
c0ebe874 10060 op[1] = 0;
df58fc94 10061 else
252b5132 10062 {
c0ebe874 10063 op[1] = AT;
df58fc94 10064 used_at = 1;
bad1aba3 10065 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10066 }
df58fc94
RS
10067 /* Fall through. */
10068 case M_BEQL:
10069 case M_BNEL:
c0ebe874 10070 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10071 break;
10072
10073 case M_BGEL:
10074 likely = 1;
1a0670f3 10075 /* Fall through. */
252b5132 10076 case M_BGE:
c0ebe874
RS
10077 if (op[1] == 0)
10078 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10079 else if (op[0] == 0)
10080 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10081 else
252b5132 10082 {
df58fc94 10083 used_at = 1;
c0ebe874 10084 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10085 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10086 &offset_expr, AT, ZERO);
252b5132 10087 }
df58fc94
RS
10088 break;
10089
10090 case M_BGEZL:
10091 case M_BGEZALL:
10092 case M_BGTZL:
10093 case M_BLEZL:
10094 case M_BLTZL:
10095 case M_BLTZALL:
c0ebe874 10096 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10097 break;
10098
10099 case M_BGTL_I:
10100 likely = 1;
1a0670f3 10101 /* Fall through. */
252b5132 10102 case M_BGT_I:
90ecf173 10103 /* Check for > max integer. */
b0e6f033 10104 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10105 {
10106 do_false:
90ecf173 10107 /* Result is always false. */
252b5132 10108 if (! likely)
a605d2b3 10109 macro_build (NULL, "nop", "");
252b5132 10110 else
df58fc94 10111 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10112 break;
252b5132 10113 }
f9419b05 10114 ++imm_expr.X_add_number;
252b5132
RH
10115 /* FALLTHROUGH */
10116 case M_BGE_I:
10117 case M_BGEL_I:
10118 if (mask == M_BGEL_I)
10119 likely = 1;
b0e6f033 10120 if (imm_expr.X_add_number == 0)
252b5132 10121 {
df58fc94 10122 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10123 &offset_expr, op[0]);
8fc2e39e 10124 break;
252b5132 10125 }
b0e6f033 10126 if (imm_expr.X_add_number == 1)
252b5132 10127 {
df58fc94 10128 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10129 &offset_expr, op[0]);
8fc2e39e 10130 break;
252b5132 10131 }
b0e6f033 10132 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10133 {
10134 do_true:
10135 /* result is always true */
1661c76c 10136 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10137 macro_build (&offset_expr, "b", "p");
8fc2e39e 10138 break;
252b5132 10139 }
8fc2e39e 10140 used_at = 1;
c0ebe874 10141 set_at (op[0], 0);
df58fc94
RS
10142 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10143 &offset_expr, AT, ZERO);
252b5132
RH
10144 break;
10145
10146 case M_BGEUL:
10147 likely = 1;
1a0670f3 10148 /* Fall through. */
252b5132 10149 case M_BGEU:
c0ebe874 10150 if (op[1] == 0)
252b5132 10151 goto do_true;
c0ebe874 10152 else if (op[0] == 0)
df58fc94 10153 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10154 &offset_expr, ZERO, op[1]);
df58fc94 10155 else
252b5132 10156 {
df58fc94 10157 used_at = 1;
c0ebe874 10158 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10159 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10160 &offset_expr, AT, ZERO);
252b5132 10161 }
252b5132
RH
10162 break;
10163
10164 case M_BGTUL_I:
10165 likely = 1;
1a0670f3 10166 /* Fall through. */
252b5132 10167 case M_BGTU_I:
c0ebe874 10168 if (op[0] == 0
bad1aba3 10169 || (GPR_SIZE == 32
f01dc953 10170 && imm_expr.X_add_number == -1))
252b5132 10171 goto do_false;
f9419b05 10172 ++imm_expr.X_add_number;
252b5132
RH
10173 /* FALLTHROUGH */
10174 case M_BGEU_I:
10175 case M_BGEUL_I:
10176 if (mask == M_BGEUL_I)
10177 likely = 1;
b0e6f033 10178 if (imm_expr.X_add_number == 0)
252b5132 10179 goto do_true;
b0e6f033 10180 else if (imm_expr.X_add_number == 1)
df58fc94 10181 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10182 &offset_expr, op[0], ZERO);
df58fc94 10183 else
252b5132 10184 {
df58fc94 10185 used_at = 1;
c0ebe874 10186 set_at (op[0], 1);
df58fc94
RS
10187 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10188 &offset_expr, AT, ZERO);
252b5132 10189 }
252b5132
RH
10190 break;
10191
10192 case M_BGTL:
10193 likely = 1;
1a0670f3 10194 /* Fall through. */
252b5132 10195 case M_BGT:
c0ebe874
RS
10196 if (op[1] == 0)
10197 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10198 else if (op[0] == 0)
10199 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10200 else
252b5132 10201 {
df58fc94 10202 used_at = 1;
c0ebe874 10203 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10204 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10205 &offset_expr, AT, ZERO);
252b5132 10206 }
252b5132
RH
10207 break;
10208
10209 case M_BGTUL:
10210 likely = 1;
1a0670f3 10211 /* Fall through. */
252b5132 10212 case M_BGTU:
c0ebe874 10213 if (op[1] == 0)
df58fc94 10214 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10215 &offset_expr, op[0], ZERO);
10216 else if (op[0] == 0)
df58fc94
RS
10217 goto do_false;
10218 else
252b5132 10219 {
df58fc94 10220 used_at = 1;
c0ebe874 10221 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10222 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10223 &offset_expr, AT, ZERO);
252b5132 10224 }
252b5132
RH
10225 break;
10226
10227 case M_BLEL:
10228 likely = 1;
1a0670f3 10229 /* Fall through. */
252b5132 10230 case M_BLE:
c0ebe874
RS
10231 if (op[1] == 0)
10232 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10233 else if (op[0] == 0)
10234 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10235 else
252b5132 10236 {
df58fc94 10237 used_at = 1;
c0ebe874 10238 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10239 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10240 &offset_expr, AT, ZERO);
252b5132 10241 }
252b5132
RH
10242 break;
10243
10244 case M_BLEL_I:
10245 likely = 1;
1a0670f3 10246 /* Fall through. */
252b5132 10247 case M_BLE_I:
b0e6f033 10248 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10249 goto do_true;
f9419b05 10250 ++imm_expr.X_add_number;
252b5132
RH
10251 /* FALLTHROUGH */
10252 case M_BLT_I:
10253 case M_BLTL_I:
10254 if (mask == M_BLTL_I)
10255 likely = 1;
b0e6f033 10256 if (imm_expr.X_add_number == 0)
c0ebe874 10257 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10258 else if (imm_expr.X_add_number == 1)
c0ebe874 10259 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10260 else
252b5132 10261 {
df58fc94 10262 used_at = 1;
c0ebe874 10263 set_at (op[0], 0);
df58fc94
RS
10264 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10265 &offset_expr, AT, ZERO);
252b5132 10266 }
252b5132
RH
10267 break;
10268
10269 case M_BLEUL:
10270 likely = 1;
1a0670f3 10271 /* Fall through. */
252b5132 10272 case M_BLEU:
c0ebe874 10273 if (op[1] == 0)
df58fc94 10274 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10275 &offset_expr, op[0], ZERO);
10276 else if (op[0] == 0)
df58fc94
RS
10277 goto do_true;
10278 else
252b5132 10279 {
df58fc94 10280 used_at = 1;
c0ebe874 10281 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10282 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10283 &offset_expr, AT, ZERO);
252b5132 10284 }
252b5132
RH
10285 break;
10286
10287 case M_BLEUL_I:
10288 likely = 1;
1a0670f3 10289 /* Fall through. */
252b5132 10290 case M_BLEU_I:
c0ebe874 10291 if (op[0] == 0
bad1aba3 10292 || (GPR_SIZE == 32
f01dc953 10293 && imm_expr.X_add_number == -1))
252b5132 10294 goto do_true;
f9419b05 10295 ++imm_expr.X_add_number;
252b5132
RH
10296 /* FALLTHROUGH */
10297 case M_BLTU_I:
10298 case M_BLTUL_I:
10299 if (mask == M_BLTUL_I)
10300 likely = 1;
b0e6f033 10301 if (imm_expr.X_add_number == 0)
252b5132 10302 goto do_false;
b0e6f033 10303 else if (imm_expr.X_add_number == 1)
df58fc94 10304 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10305 &offset_expr, op[0], ZERO);
df58fc94 10306 else
252b5132 10307 {
df58fc94 10308 used_at = 1;
c0ebe874 10309 set_at (op[0], 1);
df58fc94
RS
10310 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10311 &offset_expr, AT, ZERO);
252b5132 10312 }
252b5132
RH
10313 break;
10314
10315 case M_BLTL:
10316 likely = 1;
1a0670f3 10317 /* Fall through. */
252b5132 10318 case M_BLT:
c0ebe874
RS
10319 if (op[1] == 0)
10320 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10321 else if (op[0] == 0)
10322 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10323 else
252b5132 10324 {
df58fc94 10325 used_at = 1;
c0ebe874 10326 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10327 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10328 &offset_expr, AT, ZERO);
252b5132 10329 }
252b5132
RH
10330 break;
10331
10332 case M_BLTUL:
10333 likely = 1;
1a0670f3 10334 /* Fall through. */
252b5132 10335 case M_BLTU:
c0ebe874 10336 if (op[1] == 0)
252b5132 10337 goto do_false;
c0ebe874 10338 else if (op[0] == 0)
df58fc94 10339 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10340 &offset_expr, ZERO, op[1]);
df58fc94 10341 else
252b5132 10342 {
df58fc94 10343 used_at = 1;
c0ebe874 10344 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10345 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10346 &offset_expr, AT, ZERO);
252b5132 10347 }
252b5132
RH
10348 break;
10349
10350 case M_DDIV_3:
10351 dbl = 1;
1a0670f3 10352 /* Fall through. */
252b5132
RH
10353 case M_DIV_3:
10354 s = "mflo";
10355 goto do_div3;
10356 case M_DREM_3:
10357 dbl = 1;
1a0670f3 10358 /* Fall through. */
252b5132
RH
10359 case M_REM_3:
10360 s = "mfhi";
10361 do_div3:
c0ebe874 10362 if (op[2] == 0)
252b5132 10363 {
1661c76c 10364 as_warn (_("divide by zero"));
252b5132 10365 if (mips_trap)
df58fc94 10366 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10367 else
df58fc94 10368 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10369 break;
252b5132
RH
10370 }
10371
7d10b47d 10372 start_noreorder ();
252b5132
RH
10373 if (mips_trap)
10374 {
c0ebe874
RS
10375 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10376 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10377 }
10378 else
10379 {
df58fc94
RS
10380 if (mips_opts.micromips)
10381 micromips_label_expr (&label_expr);
10382 else
10383 label_expr.X_add_number = 8;
c0ebe874
RS
10384 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10385 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10386 macro_build (NULL, "break", BRK_FMT, 7);
10387 if (mips_opts.micromips)
10388 micromips_add_label ();
252b5132
RH
10389 }
10390 expr1.X_add_number = -1;
8fc2e39e 10391 used_at = 1;
f6a22291 10392 load_register (AT, &expr1, dbl);
df58fc94
RS
10393 if (mips_opts.micromips)
10394 micromips_label_expr (&label_expr);
10395 else
10396 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10397 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10398 if (dbl)
10399 {
10400 expr1.X_add_number = 1;
f6a22291 10401 load_register (AT, &expr1, dbl);
df58fc94 10402 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10403 }
10404 else
10405 {
10406 expr1.X_add_number = 0x80000000;
df58fc94 10407 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10408 }
10409 if (mips_trap)
10410 {
c0ebe874 10411 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10412 /* We want to close the noreorder block as soon as possible, so
10413 that later insns are available for delay slot filling. */
7d10b47d 10414 end_noreorder ();
252b5132
RH
10415 }
10416 else
10417 {
df58fc94
RS
10418 if (mips_opts.micromips)
10419 micromips_label_expr (&label_expr);
10420 else
10421 label_expr.X_add_number = 8;
c0ebe874 10422 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10423 macro_build (NULL, "nop", "");
252b5132
RH
10424
10425 /* We want to close the noreorder block as soon as possible, so
10426 that later insns are available for delay slot filling. */
7d10b47d 10427 end_noreorder ();
252b5132 10428
df58fc94 10429 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10430 }
df58fc94
RS
10431 if (mips_opts.micromips)
10432 micromips_add_label ();
c0ebe874 10433 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10434 break;
10435
10436 case M_DIV_3I:
10437 s = "div";
10438 s2 = "mflo";
10439 goto do_divi;
10440 case M_DIVU_3I:
10441 s = "divu";
10442 s2 = "mflo";
10443 goto do_divi;
10444 case M_REM_3I:
10445 s = "div";
10446 s2 = "mfhi";
10447 goto do_divi;
10448 case M_REMU_3I:
10449 s = "divu";
10450 s2 = "mfhi";
10451 goto do_divi;
10452 case M_DDIV_3I:
10453 dbl = 1;
10454 s = "ddiv";
10455 s2 = "mflo";
10456 goto do_divi;
10457 case M_DDIVU_3I:
10458 dbl = 1;
10459 s = "ddivu";
10460 s2 = "mflo";
10461 goto do_divi;
10462 case M_DREM_3I:
10463 dbl = 1;
10464 s = "ddiv";
10465 s2 = "mfhi";
10466 goto do_divi;
10467 case M_DREMU_3I:
10468 dbl = 1;
10469 s = "ddivu";
10470 s2 = "mfhi";
10471 do_divi:
b0e6f033 10472 if (imm_expr.X_add_number == 0)
252b5132 10473 {
1661c76c 10474 as_warn (_("divide by zero"));
252b5132 10475 if (mips_trap)
df58fc94 10476 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10477 else
df58fc94 10478 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10479 break;
252b5132 10480 }
b0e6f033 10481 if (imm_expr.X_add_number == 1)
252b5132
RH
10482 {
10483 if (strcmp (s2, "mflo") == 0)
c0ebe874 10484 move_register (op[0], op[1]);
252b5132 10485 else
c0ebe874 10486 move_register (op[0], ZERO);
8fc2e39e 10487 break;
252b5132 10488 }
b0e6f033 10489 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10490 {
10491 if (strcmp (s2, "mflo") == 0)
c0ebe874 10492 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10493 else
c0ebe874 10494 move_register (op[0], ZERO);
8fc2e39e 10495 break;
252b5132
RH
10496 }
10497
8fc2e39e 10498 used_at = 1;
67c0d1eb 10499 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10500 macro_build (NULL, s, "z,s,t", op[1], AT);
10501 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10502 break;
10503
10504 case M_DIVU_3:
10505 s = "divu";
10506 s2 = "mflo";
10507 goto do_divu3;
10508 case M_REMU_3:
10509 s = "divu";
10510 s2 = "mfhi";
10511 goto do_divu3;
10512 case M_DDIVU_3:
10513 s = "ddivu";
10514 s2 = "mflo";
10515 goto do_divu3;
10516 case M_DREMU_3:
10517 s = "ddivu";
10518 s2 = "mfhi";
10519 do_divu3:
7d10b47d 10520 start_noreorder ();
252b5132
RH
10521 if (mips_trap)
10522 {
c0ebe874
RS
10523 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10524 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10525 /* We want to close the noreorder block as soon as possible, so
10526 that later insns are available for delay slot filling. */
7d10b47d 10527 end_noreorder ();
252b5132
RH
10528 }
10529 else
10530 {
df58fc94
RS
10531 if (mips_opts.micromips)
10532 micromips_label_expr (&label_expr);
10533 else
10534 label_expr.X_add_number = 8;
c0ebe874
RS
10535 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10536 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10537
10538 /* We want to close the noreorder block as soon as possible, so
10539 that later insns are available for delay slot filling. */
7d10b47d 10540 end_noreorder ();
df58fc94
RS
10541 macro_build (NULL, "break", BRK_FMT, 7);
10542 if (mips_opts.micromips)
10543 micromips_add_label ();
252b5132 10544 }
c0ebe874 10545 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10546 break;
252b5132 10547
1abe91b1
MR
10548 case M_DLCA_AB:
10549 dbl = 1;
1a0670f3 10550 /* Fall through. */
1abe91b1
MR
10551 case M_LCA_AB:
10552 call = 1;
10553 goto do_la;
252b5132
RH
10554 case M_DLA_AB:
10555 dbl = 1;
1a0670f3 10556 /* Fall through. */
252b5132 10557 case M_LA_AB:
1abe91b1 10558 do_la:
252b5132
RH
10559 /* Load the address of a symbol into a register. If breg is not
10560 zero, we then add a base register to it. */
10561
c0ebe874 10562 breg = op[2];
bad1aba3 10563 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10564 as_warn (_("dla used to load 32-bit register; recommend using la "
10565 "instead"));
3bec30a8 10566
90ecf173 10567 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10568 as_warn (_("la used to load 64-bit address; recommend using dla "
10569 "instead"));
3bec30a8 10570
f2ae14a1 10571 if (small_offset_p (0, align, 16))
0c11417f 10572 {
c0ebe874 10573 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10574 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10575 break;
0c11417f
MR
10576 }
10577
c0ebe874 10578 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10579 {
10580 tempreg = AT;
10581 used_at = 1;
10582 }
10583 else
c0ebe874 10584 tempreg = op[0];
afdbd6d0 10585
252b5132
RH
10586 if (offset_expr.X_op != O_symbol
10587 && offset_expr.X_op != O_constant)
10588 {
1661c76c 10589 as_bad (_("expression too complex"));
252b5132
RH
10590 offset_expr.X_op = O_constant;
10591 }
10592
252b5132 10593 if (offset_expr.X_op == O_constant)
aed1a261 10594 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10595 else if (mips_pic == NO_PIC)
10596 {
d6bc6245 10597 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10598 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10599 Otherwise we want
10600 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10601 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10602 If we have a constant, we need two instructions anyhow,
d6bc6245 10603 so we may as well always use the latter form.
76b3015f 10604
6caf9ef4
TS
10605 With 64bit address space and a usable $at we want
10606 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10607 lui $at,<sym> (BFD_RELOC_HI16_S)
10608 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10609 daddiu $at,<sym> (BFD_RELOC_LO16)
10610 dsll32 $tempreg,0
10611 daddu $tempreg,$tempreg,$at
10612
10613 If $at is already in use, we use a path which is suboptimal
10614 on superscalar processors.
10615 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10616 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10617 dsll $tempreg,16
10618 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10619 dsll $tempreg,16
10620 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10621
10622 For GP relative symbols in 64bit address space we can use
10623 the same sequence as in 32bit address space. */
aed1a261 10624 if (HAVE_64BIT_SYMBOLS)
252b5132 10625 {
6caf9ef4
TS
10626 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10627 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10628 {
10629 relax_start (offset_expr.X_add_symbol);
10630 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10631 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10632 relax_switch ();
10633 }
d6bc6245 10634
741fe287 10635 if (used_at == 0 && mips_opts.at)
98d3f06f 10636 {
df58fc94 10637 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10638 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10639 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10640 AT, BFD_RELOC_HI16_S);
67c0d1eb 10641 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10642 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10643 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10644 AT, AT, BFD_RELOC_LO16);
df58fc94 10645 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10646 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10647 used_at = 1;
10648 }
10649 else
10650 {
df58fc94 10651 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10652 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10653 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10654 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10655 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10656 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10657 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10658 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10659 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10660 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10661 }
6caf9ef4
TS
10662
10663 if (mips_relax.sequence)
10664 relax_end ();
98d3f06f
KH
10665 }
10666 else
10667 {
10668 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10669 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10670 {
4d7206a2 10671 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10672 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10673 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10674 relax_switch ();
98d3f06f 10675 }
6943caf0 10676 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10677 as_bad (_("offset too large"));
67c0d1eb
RS
10678 macro_build_lui (&offset_expr, tempreg);
10679 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10680 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10681 if (mips_relax.sequence)
10682 relax_end ();
98d3f06f 10683 }
252b5132 10684 }
0a44bf69 10685 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10686 {
9117d219
NC
10687 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10688
252b5132
RH
10689 /* If this is a reference to an external symbol, and there
10690 is no constant, we want
10691 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10692 or for lca or if tempreg is PIC_CALL_REG
9117d219 10693 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10694 For a local symbol, we want
10695 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10696 nop
10697 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10698
10699 If we have a small constant, and this is a reference to
10700 an external symbol, we want
10701 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10702 nop
10703 addiu $tempreg,$tempreg,<constant>
10704 For a local symbol, we want the same instruction
10705 sequence, but we output a BFD_RELOC_LO16 reloc on the
10706 addiu instruction.
10707
10708 If we have a large constant, and this is a reference to
10709 an external symbol, we want
10710 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10711 lui $at,<hiconstant>
10712 addiu $at,$at,<loconstant>
10713 addu $tempreg,$tempreg,$at
10714 For a local symbol, we want the same instruction
10715 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10716 addiu instruction.
ed6fb7bd
SC
10717 */
10718
4d7206a2 10719 if (offset_expr.X_add_number == 0)
252b5132 10720 {
0a44bf69
RS
10721 if (mips_pic == SVR4_PIC
10722 && breg == 0
10723 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10724 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10725
10726 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10727 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10728 lw_reloc_type, mips_gp_register);
4d7206a2 10729 if (breg != 0)
252b5132
RH
10730 {
10731 /* We're going to put in an addu instruction using
10732 tempreg, so we may as well insert the nop right
10733 now. */
269137b2 10734 load_delay_nop ();
252b5132 10735 }
4d7206a2 10736 relax_switch ();
67c0d1eb
RS
10737 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10738 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10739 load_delay_nop ();
67c0d1eb
RS
10740 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10741 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10742 relax_end ();
252b5132
RH
10743 /* FIXME: If breg == 0, and the next instruction uses
10744 $tempreg, then if this variant case is used an extra
10745 nop will be generated. */
10746 }
4d7206a2
RS
10747 else if (offset_expr.X_add_number >= -0x8000
10748 && offset_expr.X_add_number < 0x8000)
252b5132 10749 {
67c0d1eb 10750 load_got_offset (tempreg, &offset_expr);
269137b2 10751 load_delay_nop ();
67c0d1eb 10752 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10753 }
10754 else
10755 {
4d7206a2
RS
10756 expr1.X_add_number = offset_expr.X_add_number;
10757 offset_expr.X_add_number =
43c0598f 10758 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10759 load_got_offset (tempreg, &offset_expr);
f6a22291 10760 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10761 /* If we are going to add in a base register, and the
10762 target register and the base register are the same,
10763 then we are using AT as a temporary register. Since
10764 we want to load the constant into AT, we add our
10765 current AT (from the global offset table) and the
10766 register into the register now, and pretend we were
10767 not using a base register. */
c0ebe874 10768 if (breg == op[0])
252b5132 10769 {
269137b2 10770 load_delay_nop ();
67c0d1eb 10771 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10772 op[0], AT, breg);
252b5132 10773 breg = 0;
c0ebe874 10774 tempreg = op[0];
252b5132 10775 }
f6a22291 10776 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10777 used_at = 1;
10778 }
10779 }
0a44bf69 10780 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10781 {
67c0d1eb 10782 int add_breg_early = 0;
f5040a92
AO
10783
10784 /* If this is a reference to an external, and there is no
10785 constant, or local symbol (*), with or without a
10786 constant, we want
10787 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10788 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10789 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10790
10791 If we have a small constant, and this is a reference to
10792 an external symbol, we want
10793 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10794 addiu $tempreg,$tempreg,<constant>
10795
10796 If we have a large constant, and this is a reference to
10797 an external symbol, we want
10798 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10799 lui $at,<hiconstant>
10800 addiu $at,$at,<loconstant>
10801 addu $tempreg,$tempreg,$at
10802
10803 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10804 local symbols, even though it introduces an additional
10805 instruction. */
10806
f5040a92
AO
10807 if (offset_expr.X_add_number)
10808 {
4d7206a2 10809 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10810 offset_expr.X_add_number = 0;
10811
4d7206a2 10812 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10813 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10814 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10815
10816 if (expr1.X_add_number >= -0x8000
10817 && expr1.X_add_number < 0x8000)
10818 {
67c0d1eb
RS
10819 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10820 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10821 }
ecd13cd3 10822 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10823 {
c0ebe874
RS
10824 unsigned int dreg;
10825
f5040a92
AO
10826 /* If we are going to add in a base register, and the
10827 target register and the base register are the same,
10828 then we are using AT as a temporary register. Since
10829 we want to load the constant into AT, we add our
10830 current AT (from the global offset table) and the
10831 register into the register now, and pretend we were
10832 not using a base register. */
c0ebe874 10833 if (breg != op[0])
f5040a92
AO
10834 dreg = tempreg;
10835 else
10836 {
9c2799c2 10837 gas_assert (tempreg == AT);
67c0d1eb 10838 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10839 op[0], AT, breg);
10840 dreg = op[0];
67c0d1eb 10841 add_breg_early = 1;
f5040a92
AO
10842 }
10843
f6a22291 10844 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10845 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10846 dreg, dreg, AT);
f5040a92 10847
f5040a92
AO
10848 used_at = 1;
10849 }
10850 else
10851 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10852
4d7206a2 10853 relax_switch ();
f5040a92
AO
10854 offset_expr.X_add_number = expr1.X_add_number;
10855
67c0d1eb
RS
10856 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10857 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10858 if (add_breg_early)
f5040a92 10859 {
67c0d1eb 10860 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10861 op[0], tempreg, breg);
f5040a92 10862 breg = 0;
c0ebe874 10863 tempreg = op[0];
f5040a92 10864 }
4d7206a2 10865 relax_end ();
f5040a92 10866 }
4d7206a2 10867 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 10868 {
4d7206a2 10869 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10870 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10871 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 10872 relax_switch ();
67c0d1eb
RS
10873 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10874 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 10875 relax_end ();
f5040a92 10876 }
4d7206a2 10877 else
f5040a92 10878 {
67c0d1eb
RS
10879 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10880 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10881 }
10882 }
0a44bf69 10883 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10884 {
67c0d1eb 10885 int gpdelay;
9117d219
NC
10886 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10887 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10888 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10889
10890 /* This is the large GOT case. If this is a reference to an
10891 external symbol, and there is no constant, we want
10892 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10893 addu $tempreg,$tempreg,$gp
10894 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10895 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10896 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10897 addu $tempreg,$tempreg,$gp
10898 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10899 For a local symbol, we want
10900 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10901 nop
10902 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10903
10904 If we have a small constant, and this is a reference to
10905 an external symbol, we want
10906 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10907 addu $tempreg,$tempreg,$gp
10908 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10909 nop
10910 addiu $tempreg,$tempreg,<constant>
10911 For a local symbol, we want
10912 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10913 nop
10914 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10915
10916 If we have a large constant, and this is a reference to
10917 an external symbol, we want
10918 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10919 addu $tempreg,$tempreg,$gp
10920 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10921 lui $at,<hiconstant>
10922 addiu $at,$at,<loconstant>
10923 addu $tempreg,$tempreg,$at
10924 For a local symbol, we want
10925 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10926 lui $at,<hiconstant>
10927 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10928 addu $tempreg,$tempreg,$at
f5040a92 10929 */
438c16b8 10930
252b5132
RH
10931 expr1.X_add_number = offset_expr.X_add_number;
10932 offset_expr.X_add_number = 0;
4d7206a2 10933 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10934 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10935 if (expr1.X_add_number == 0 && breg == 0
10936 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10937 {
10938 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10939 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10940 }
df58fc94 10941 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10942 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10943 tempreg, tempreg, mips_gp_register);
67c0d1eb 10944 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10945 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10946 if (expr1.X_add_number == 0)
10947 {
67c0d1eb 10948 if (breg != 0)
252b5132
RH
10949 {
10950 /* We're going to put in an addu instruction using
10951 tempreg, so we may as well insert the nop right
10952 now. */
269137b2 10953 load_delay_nop ();
252b5132 10954 }
252b5132
RH
10955 }
10956 else if (expr1.X_add_number >= -0x8000
10957 && expr1.X_add_number < 0x8000)
10958 {
269137b2 10959 load_delay_nop ();
67c0d1eb 10960 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10961 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10962 }
10963 else
10964 {
c0ebe874
RS
10965 unsigned int dreg;
10966
252b5132
RH
10967 /* If we are going to add in a base register, and the
10968 target register and the base register are the same,
10969 then we are using AT as a temporary register. Since
10970 we want to load the constant into AT, we add our
10971 current AT (from the global offset table) and the
10972 register into the register now, and pretend we were
10973 not using a base register. */
c0ebe874 10974 if (breg != op[0])
67c0d1eb 10975 dreg = tempreg;
252b5132
RH
10976 else
10977 {
9c2799c2 10978 gas_assert (tempreg == AT);
269137b2 10979 load_delay_nop ();
67c0d1eb 10980 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10981 op[0], AT, breg);
10982 dreg = op[0];
252b5132
RH
10983 }
10984
f6a22291 10985 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10986 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10987
252b5132
RH
10988 used_at = 1;
10989 }
43c0598f 10990 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10991 relax_switch ();
252b5132 10992
67c0d1eb 10993 if (gpdelay)
252b5132
RH
10994 {
10995 /* This is needed because this instruction uses $gp, but
f5040a92 10996 the first instruction on the main stream does not. */
67c0d1eb 10997 macro_build (NULL, "nop", "");
252b5132 10998 }
ed6fb7bd 10999
67c0d1eb
RS
11000 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11001 local_reloc_type, mips_gp_register);
f5040a92 11002 if (expr1.X_add_number >= -0x8000
252b5132
RH
11003 && expr1.X_add_number < 0x8000)
11004 {
269137b2 11005 load_delay_nop ();
67c0d1eb
RS
11006 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11007 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11008 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11009 register, the external symbol case ended with a load,
11010 so if the symbol turns out to not be external, and
11011 the next instruction uses tempreg, an unnecessary nop
11012 will be inserted. */
252b5132
RH
11013 }
11014 else
11015 {
c0ebe874 11016 if (breg == op[0])
252b5132
RH
11017 {
11018 /* We must add in the base register now, as in the
f5040a92 11019 external symbol case. */
9c2799c2 11020 gas_assert (tempreg == AT);
269137b2 11021 load_delay_nop ();
67c0d1eb 11022 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11023 op[0], AT, breg);
11024 tempreg = op[0];
252b5132 11025 /* We set breg to 0 because we have arranged to add
f5040a92 11026 it in in both cases. */
252b5132
RH
11027 breg = 0;
11028 }
11029
67c0d1eb
RS
11030 macro_build_lui (&expr1, AT);
11031 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11032 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11033 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11034 tempreg, tempreg, AT);
8fc2e39e 11035 used_at = 1;
252b5132 11036 }
4d7206a2 11037 relax_end ();
252b5132 11038 }
0a44bf69 11039 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11040 {
f5040a92
AO
11041 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11042 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11043 int add_breg_early = 0;
f5040a92
AO
11044
11045 /* This is the large GOT case. If this is a reference to an
11046 external symbol, and there is no constant, we want
11047 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11048 add $tempreg,$tempreg,$gp
11049 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11050 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11051 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11052 add $tempreg,$tempreg,$gp
11053 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11054
11055 If we have a small constant, and this is a reference to
11056 an external symbol, we want
11057 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11058 add $tempreg,$tempreg,$gp
11059 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11060 addi $tempreg,$tempreg,<constant>
11061
11062 If we have a large constant, and this is a reference to
11063 an external symbol, we want
11064 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11065 addu $tempreg,$tempreg,$gp
11066 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11067 lui $at,<hiconstant>
11068 addi $at,$at,<loconstant>
11069 add $tempreg,$tempreg,$at
11070
11071 If we have NewABI, and we know it's a local symbol, we want
11072 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11073 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11074 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11075
4d7206a2 11076 relax_start (offset_expr.X_add_symbol);
f5040a92 11077
4d7206a2 11078 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11079 offset_expr.X_add_number = 0;
11080
1abe91b1
MR
11081 if (expr1.X_add_number == 0 && breg == 0
11082 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11083 {
11084 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11085 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11086 }
df58fc94 11087 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11088 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11089 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11090 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11091 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11092
11093 if (expr1.X_add_number == 0)
4d7206a2 11094 ;
f5040a92
AO
11095 else if (expr1.X_add_number >= -0x8000
11096 && expr1.X_add_number < 0x8000)
11097 {
67c0d1eb 11098 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11099 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11100 }
ecd13cd3 11101 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11102 {
c0ebe874
RS
11103 unsigned int dreg;
11104
f5040a92
AO
11105 /* If we are going to add in a base register, and the
11106 target register and the base register are the same,
11107 then we are using AT as a temporary register. Since
11108 we want to load the constant into AT, we add our
11109 current AT (from the global offset table) and the
11110 register into the register now, and pretend we were
11111 not using a base register. */
c0ebe874 11112 if (breg != op[0])
f5040a92
AO
11113 dreg = tempreg;
11114 else
11115 {
9c2799c2 11116 gas_assert (tempreg == AT);
67c0d1eb 11117 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11118 op[0], AT, breg);
11119 dreg = op[0];
67c0d1eb 11120 add_breg_early = 1;
f5040a92
AO
11121 }
11122
f6a22291 11123 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11124 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11125
f5040a92
AO
11126 used_at = 1;
11127 }
11128 else
11129 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11130
4d7206a2 11131 relax_switch ();
f5040a92 11132 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11133 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11134 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11135 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11136 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11137 if (add_breg_early)
f5040a92 11138 {
67c0d1eb 11139 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11140 op[0], tempreg, breg);
f5040a92 11141 breg = 0;
c0ebe874 11142 tempreg = op[0];
f5040a92 11143 }
4d7206a2 11144 relax_end ();
f5040a92 11145 }
252b5132
RH
11146 else
11147 abort ();
11148
11149 if (breg != 0)
c0ebe874 11150 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11151 break;
11152
52b6b6b9 11153 case M_MSGSND:
df58fc94 11154 gas_assert (!mips_opts.micromips);
c0ebe874 11155 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11156 break;
52b6b6b9
JM
11157
11158 case M_MSGLD:
df58fc94 11159 gas_assert (!mips_opts.micromips);
c8276761 11160 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11161 break;
52b6b6b9
JM
11162
11163 case M_MSGLD_T:
df58fc94 11164 gas_assert (!mips_opts.micromips);
c0ebe874 11165 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11166 break;
52b6b6b9
JM
11167
11168 case M_MSGWAIT:
df58fc94 11169 gas_assert (!mips_opts.micromips);
52b6b6b9 11170 macro_build (NULL, "c2", "C", 3);
c7af4273 11171 break;
52b6b6b9
JM
11172
11173 case M_MSGWAIT_T:
df58fc94 11174 gas_assert (!mips_opts.micromips);
c0ebe874 11175 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11176 break;
52b6b6b9 11177
252b5132
RH
11178 case M_J_A:
11179 /* The j instruction may not be used in PIC code, since it
11180 requires an absolute address. We convert it to a b
11181 instruction. */
11182 if (mips_pic == NO_PIC)
67c0d1eb 11183 macro_build (&offset_expr, "j", "a");
252b5132 11184 else
67c0d1eb 11185 macro_build (&offset_expr, "b", "p");
8fc2e39e 11186 break;
252b5132
RH
11187
11188 /* The jal instructions must be handled as macros because when
11189 generating PIC code they expand to multi-instruction
11190 sequences. Normally they are simple instructions. */
df58fc94 11191 case M_JALS_1:
c0ebe874
RS
11192 op[1] = op[0];
11193 op[0] = RA;
df58fc94
RS
11194 /* Fall through. */
11195 case M_JALS_2:
11196 gas_assert (mips_opts.micromips);
833794fc
MR
11197 if (mips_opts.insn32)
11198 {
1661c76c 11199 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11200 break;
11201 }
df58fc94
RS
11202 jals = 1;
11203 goto jal;
252b5132 11204 case M_JAL_1:
c0ebe874
RS
11205 op[1] = op[0];
11206 op[0] = RA;
252b5132
RH
11207 /* Fall through. */
11208 case M_JAL_2:
df58fc94 11209 jal:
3e722fb5 11210 if (mips_pic == NO_PIC)
df58fc94
RS
11211 {
11212 s = jals ? "jalrs" : "jalr";
e64af278 11213 if (mips_opts.micromips
833794fc 11214 && !mips_opts.insn32
c0ebe874 11215 && op[0] == RA
e64af278 11216 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11217 macro_build (NULL, s, "mj", op[1]);
df58fc94 11218 else
c0ebe874 11219 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11220 }
0a44bf69 11221 else
252b5132 11222 {
df58fc94
RS
11223 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11224 && mips_cprestore_offset >= 0);
11225
c0ebe874 11226 if (op[1] != PIC_CALL_REG)
252b5132 11227 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11228
833794fc
MR
11229 s = ((mips_opts.micromips
11230 && !mips_opts.insn32
11231 && (!mips_opts.noreorder || cprestore))
df58fc94 11232 ? "jalrs" : "jalr");
e64af278 11233 if (mips_opts.micromips
833794fc 11234 && !mips_opts.insn32
c0ebe874 11235 && op[0] == RA
e64af278 11236 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11237 macro_build (NULL, s, "mj", op[1]);
df58fc94 11238 else
c0ebe874 11239 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11240 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11241 {
6478892d 11242 if (mips_cprestore_offset < 0)
1661c76c 11243 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11244 else
11245 {
90ecf173 11246 if (!mips_frame_reg_valid)
7a621144 11247 {
1661c76c 11248 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11249 /* Quiet this warning. */
11250 mips_frame_reg_valid = 1;
11251 }
90ecf173 11252 if (!mips_cprestore_valid)
7a621144 11253 {
1661c76c 11254 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11255 /* Quiet this warning. */
11256 mips_cprestore_valid = 1;
11257 }
d3fca0b5
MR
11258 if (mips_opts.noreorder)
11259 macro_build (NULL, "nop", "");
6478892d 11260 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11261 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11262 mips_gp_register,
256ab948
TS
11263 mips_frame_reg,
11264 HAVE_64BIT_ADDRESSES);
6478892d 11265 }
252b5132
RH
11266 }
11267 }
252b5132 11268
8fc2e39e 11269 break;
252b5132 11270
df58fc94
RS
11271 case M_JALS_A:
11272 gas_assert (mips_opts.micromips);
833794fc
MR
11273 if (mips_opts.insn32)
11274 {
1661c76c 11275 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11276 break;
11277 }
df58fc94
RS
11278 jals = 1;
11279 /* Fall through. */
252b5132
RH
11280 case M_JAL_A:
11281 if (mips_pic == NO_PIC)
df58fc94 11282 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11283 else if (mips_pic == SVR4_PIC)
11284 {
11285 /* If this is a reference to an external symbol, and we are
11286 using a small GOT, we want
11287 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11288 nop
f9419b05 11289 jalr $ra,$25
252b5132
RH
11290 nop
11291 lw $gp,cprestore($sp)
11292 The cprestore value is set using the .cprestore
11293 pseudo-op. If we are using a big GOT, we want
11294 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11295 addu $25,$25,$gp
11296 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11297 nop
f9419b05 11298 jalr $ra,$25
252b5132
RH
11299 nop
11300 lw $gp,cprestore($sp)
11301 If the symbol is not external, we want
11302 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11303 nop
11304 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11305 jalr $ra,$25
252b5132 11306 nop
438c16b8 11307 lw $gp,cprestore($sp)
f5040a92
AO
11308
11309 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11310 sequences above, minus nops, unless the symbol is local,
11311 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11312 GOT_DISP. */
438c16b8 11313 if (HAVE_NEWABI)
252b5132 11314 {
90ecf173 11315 if (!mips_big_got)
f5040a92 11316 {
4d7206a2 11317 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11318 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11319 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11320 mips_gp_register);
4d7206a2 11321 relax_switch ();
67c0d1eb
RS
11322 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11323 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11324 mips_gp_register);
11325 relax_end ();
f5040a92
AO
11326 }
11327 else
11328 {
4d7206a2 11329 relax_start (offset_expr.X_add_symbol);
df58fc94 11330 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11331 BFD_RELOC_MIPS_CALL_HI16);
11332 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11333 PIC_CALL_REG, mips_gp_register);
11334 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11335 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11336 PIC_CALL_REG);
4d7206a2 11337 relax_switch ();
67c0d1eb
RS
11338 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11339 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11340 mips_gp_register);
11341 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11342 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11343 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11344 relax_end ();
f5040a92 11345 }
684022ea 11346
df58fc94 11347 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11348 }
11349 else
11350 {
4d7206a2 11351 relax_start (offset_expr.X_add_symbol);
90ecf173 11352 if (!mips_big_got)
438c16b8 11353 {
67c0d1eb
RS
11354 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11355 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11356 mips_gp_register);
269137b2 11357 load_delay_nop ();
4d7206a2 11358 relax_switch ();
438c16b8 11359 }
252b5132 11360 else
252b5132 11361 {
67c0d1eb
RS
11362 int gpdelay;
11363
11364 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11365 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11366 BFD_RELOC_MIPS_CALL_HI16);
11367 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11368 PIC_CALL_REG, mips_gp_register);
11369 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11370 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11371 PIC_CALL_REG);
269137b2 11372 load_delay_nop ();
4d7206a2 11373 relax_switch ();
67c0d1eb
RS
11374 if (gpdelay)
11375 macro_build (NULL, "nop", "");
252b5132 11376 }
67c0d1eb
RS
11377 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11378 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11379 mips_gp_register);
269137b2 11380 load_delay_nop ();
67c0d1eb
RS
11381 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11382 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11383 relax_end ();
df58fc94 11384 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11385
6478892d 11386 if (mips_cprestore_offset < 0)
1661c76c 11387 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11388 else
11389 {
90ecf173 11390 if (!mips_frame_reg_valid)
7a621144 11391 {
1661c76c 11392 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11393 /* Quiet this warning. */
11394 mips_frame_reg_valid = 1;
11395 }
90ecf173 11396 if (!mips_cprestore_valid)
7a621144 11397 {
1661c76c 11398 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11399 /* Quiet this warning. */
11400 mips_cprestore_valid = 1;
11401 }
6478892d 11402 if (mips_opts.noreorder)
67c0d1eb 11403 macro_build (NULL, "nop", "");
6478892d 11404 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11405 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11406 mips_gp_register,
256ab948
TS
11407 mips_frame_reg,
11408 HAVE_64BIT_ADDRESSES);
6478892d 11409 }
252b5132
RH
11410 }
11411 }
0a44bf69 11412 else if (mips_pic == VXWORKS_PIC)
1661c76c 11413 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11414 else
11415 abort ();
11416
8fc2e39e 11417 break;
252b5132 11418
7f3c4072 11419 case M_LBUE_AB:
7f3c4072
CM
11420 s = "lbue";
11421 fmt = "t,+j(b)";
11422 offbits = 9;
11423 goto ld_st;
11424 case M_LHUE_AB:
7f3c4072
CM
11425 s = "lhue";
11426 fmt = "t,+j(b)";
11427 offbits = 9;
11428 goto ld_st;
11429 case M_LBE_AB:
7f3c4072
CM
11430 s = "lbe";
11431 fmt = "t,+j(b)";
11432 offbits = 9;
11433 goto ld_st;
11434 case M_LHE_AB:
7f3c4072
CM
11435 s = "lhe";
11436 fmt = "t,+j(b)";
11437 offbits = 9;
11438 goto ld_st;
11439 case M_LLE_AB:
7f3c4072
CM
11440 s = "lle";
11441 fmt = "t,+j(b)";
11442 offbits = 9;
11443 goto ld_st;
11444 case M_LWE_AB:
7f3c4072
CM
11445 s = "lwe";
11446 fmt = "t,+j(b)";
11447 offbits = 9;
11448 goto ld_st;
11449 case M_LWLE_AB:
7f3c4072
CM
11450 s = "lwle";
11451 fmt = "t,+j(b)";
11452 offbits = 9;
11453 goto ld_st;
11454 case M_LWRE_AB:
7f3c4072
CM
11455 s = "lwre";
11456 fmt = "t,+j(b)";
11457 offbits = 9;
11458 goto ld_st;
11459 case M_SBE_AB:
7f3c4072
CM
11460 s = "sbe";
11461 fmt = "t,+j(b)";
11462 offbits = 9;
11463 goto ld_st;
11464 case M_SCE_AB:
7f3c4072
CM
11465 s = "sce";
11466 fmt = "t,+j(b)";
11467 offbits = 9;
11468 goto ld_st;
11469 case M_SHE_AB:
7f3c4072
CM
11470 s = "she";
11471 fmt = "t,+j(b)";
11472 offbits = 9;
11473 goto ld_st;
11474 case M_SWE_AB:
7f3c4072
CM
11475 s = "swe";
11476 fmt = "t,+j(b)";
11477 offbits = 9;
11478 goto ld_st;
11479 case M_SWLE_AB:
7f3c4072
CM
11480 s = "swle";
11481 fmt = "t,+j(b)";
11482 offbits = 9;
11483 goto ld_st;
11484 case M_SWRE_AB:
7f3c4072
CM
11485 s = "swre";
11486 fmt = "t,+j(b)";
11487 offbits = 9;
11488 goto ld_st;
dec0624d 11489 case M_ACLR_AB:
dec0624d 11490 s = "aclr";
dec0624d 11491 fmt = "\\,~(b)";
7f3c4072 11492 offbits = 12;
dec0624d
MR
11493 goto ld_st;
11494 case M_ASET_AB:
dec0624d 11495 s = "aset";
dec0624d 11496 fmt = "\\,~(b)";
7f3c4072 11497 offbits = 12;
dec0624d 11498 goto ld_st;
252b5132
RH
11499 case M_LB_AB:
11500 s = "lb";
df58fc94 11501 fmt = "t,o(b)";
252b5132
RH
11502 goto ld;
11503 case M_LBU_AB:
11504 s = "lbu";
df58fc94 11505 fmt = "t,o(b)";
252b5132
RH
11506 goto ld;
11507 case M_LH_AB:
11508 s = "lh";
df58fc94 11509 fmt = "t,o(b)";
252b5132
RH
11510 goto ld;
11511 case M_LHU_AB:
11512 s = "lhu";
df58fc94 11513 fmt = "t,o(b)";
252b5132
RH
11514 goto ld;
11515 case M_LW_AB:
11516 s = "lw";
df58fc94 11517 fmt = "t,o(b)";
252b5132
RH
11518 goto ld;
11519 case M_LWC0_AB:
df58fc94 11520 gas_assert (!mips_opts.micromips);
252b5132 11521 s = "lwc0";
df58fc94 11522 fmt = "E,o(b)";
bdaaa2e1 11523 /* Itbl support may require additional care here. */
252b5132 11524 coproc = 1;
df58fc94 11525 goto ld_st;
252b5132
RH
11526 case M_LWC1_AB:
11527 s = "lwc1";
df58fc94 11528 fmt = "T,o(b)";
bdaaa2e1 11529 /* Itbl support may require additional care here. */
252b5132 11530 coproc = 1;
df58fc94 11531 goto ld_st;
252b5132
RH
11532 case M_LWC2_AB:
11533 s = "lwc2";
df58fc94 11534 fmt = COP12_FMT;
7361da2c
AB
11535 offbits = (mips_opts.micromips ? 12
11536 : ISA_IS_R6 (mips_opts.isa) ? 11
11537 : 16);
bdaaa2e1 11538 /* Itbl support may require additional care here. */
252b5132 11539 coproc = 1;
df58fc94 11540 goto ld_st;
252b5132 11541 case M_LWC3_AB:
df58fc94 11542 gas_assert (!mips_opts.micromips);
252b5132 11543 s = "lwc3";
df58fc94 11544 fmt = "E,o(b)";
bdaaa2e1 11545 /* Itbl support may require additional care here. */
252b5132 11546 coproc = 1;
df58fc94 11547 goto ld_st;
252b5132
RH
11548 case M_LWL_AB:
11549 s = "lwl";
df58fc94 11550 fmt = MEM12_FMT;
7f3c4072 11551 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11552 goto ld_st;
252b5132
RH
11553 case M_LWR_AB:
11554 s = "lwr";
df58fc94 11555 fmt = MEM12_FMT;
7f3c4072 11556 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11557 goto ld_st;
252b5132 11558 case M_LDC1_AB:
252b5132 11559 s = "ldc1";
df58fc94 11560 fmt = "T,o(b)";
bdaaa2e1 11561 /* Itbl support may require additional care here. */
252b5132 11562 coproc = 1;
df58fc94 11563 goto ld_st;
252b5132
RH
11564 case M_LDC2_AB:
11565 s = "ldc2";
df58fc94 11566 fmt = COP12_FMT;
7361da2c
AB
11567 offbits = (mips_opts.micromips ? 12
11568 : ISA_IS_R6 (mips_opts.isa) ? 11
11569 : 16);
bdaaa2e1 11570 /* Itbl support may require additional care here. */
252b5132 11571 coproc = 1;
df58fc94 11572 goto ld_st;
c77c0862 11573 case M_LQC2_AB:
c77c0862 11574 s = "lqc2";
14daeee3 11575 fmt = "+7,o(b)";
c77c0862
RS
11576 /* Itbl support may require additional care here. */
11577 coproc = 1;
11578 goto ld_st;
252b5132
RH
11579 case M_LDC3_AB:
11580 s = "ldc3";
df58fc94 11581 fmt = "E,o(b)";
bdaaa2e1 11582 /* Itbl support may require additional care here. */
252b5132 11583 coproc = 1;
df58fc94 11584 goto ld_st;
252b5132
RH
11585 case M_LDL_AB:
11586 s = "ldl";
df58fc94 11587 fmt = MEM12_FMT;
7f3c4072 11588 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11589 goto ld_st;
252b5132
RH
11590 case M_LDR_AB:
11591 s = "ldr";
df58fc94 11592 fmt = MEM12_FMT;
7f3c4072 11593 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11594 goto ld_st;
252b5132
RH
11595 case M_LL_AB:
11596 s = "ll";
7361da2c
AB
11597 fmt = LL_SC_FMT;
11598 offbits = (mips_opts.micromips ? 12
11599 : ISA_IS_R6 (mips_opts.isa) ? 9
11600 : 16);
252b5132
RH
11601 goto ld;
11602 case M_LLD_AB:
11603 s = "lld";
7361da2c
AB
11604 fmt = LL_SC_FMT;
11605 offbits = (mips_opts.micromips ? 12
11606 : ISA_IS_R6 (mips_opts.isa) ? 9
11607 : 16);
252b5132
RH
11608 goto ld;
11609 case M_LWU_AB:
11610 s = "lwu";
df58fc94 11611 fmt = MEM12_FMT;
7f3c4072 11612 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11613 goto ld;
11614 case M_LWP_AB:
df58fc94
RS
11615 gas_assert (mips_opts.micromips);
11616 s = "lwp";
11617 fmt = "t,~(b)";
7f3c4072 11618 offbits = 12;
df58fc94
RS
11619 lp = 1;
11620 goto ld;
11621 case M_LDP_AB:
df58fc94
RS
11622 gas_assert (mips_opts.micromips);
11623 s = "ldp";
11624 fmt = "t,~(b)";
7f3c4072 11625 offbits = 12;
df58fc94
RS
11626 lp = 1;
11627 goto ld;
11628 case M_LWM_AB:
df58fc94
RS
11629 gas_assert (mips_opts.micromips);
11630 s = "lwm";
11631 fmt = "n,~(b)";
7f3c4072 11632 offbits = 12;
df58fc94
RS
11633 goto ld_st;
11634 case M_LDM_AB:
df58fc94
RS
11635 gas_assert (mips_opts.micromips);
11636 s = "ldm";
11637 fmt = "n,~(b)";
7f3c4072 11638 offbits = 12;
df58fc94
RS
11639 goto ld_st;
11640
252b5132 11641 ld:
f19ccbda 11642 /* We don't want to use $0 as tempreg. */
c0ebe874 11643 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11644 goto ld_st;
252b5132 11645 else
c0ebe874 11646 tempreg = op[0] + lp;
df58fc94
RS
11647 goto ld_noat;
11648
252b5132
RH
11649 case M_SB_AB:
11650 s = "sb";
df58fc94
RS
11651 fmt = "t,o(b)";
11652 goto ld_st;
252b5132
RH
11653 case M_SH_AB:
11654 s = "sh";
df58fc94
RS
11655 fmt = "t,o(b)";
11656 goto ld_st;
252b5132
RH
11657 case M_SW_AB:
11658 s = "sw";
df58fc94
RS
11659 fmt = "t,o(b)";
11660 goto ld_st;
252b5132 11661 case M_SWC0_AB:
df58fc94 11662 gas_assert (!mips_opts.micromips);
252b5132 11663 s = "swc0";
df58fc94 11664 fmt = "E,o(b)";
bdaaa2e1 11665 /* Itbl support may require additional care here. */
252b5132 11666 coproc = 1;
df58fc94 11667 goto ld_st;
252b5132
RH
11668 case M_SWC1_AB:
11669 s = "swc1";
df58fc94 11670 fmt = "T,o(b)";
bdaaa2e1 11671 /* Itbl support may require additional care here. */
252b5132 11672 coproc = 1;
df58fc94 11673 goto ld_st;
252b5132
RH
11674 case M_SWC2_AB:
11675 s = "swc2";
df58fc94 11676 fmt = COP12_FMT;
7361da2c
AB
11677 offbits = (mips_opts.micromips ? 12
11678 : ISA_IS_R6 (mips_opts.isa) ? 11
11679 : 16);
bdaaa2e1 11680 /* Itbl support may require additional care here. */
252b5132 11681 coproc = 1;
df58fc94 11682 goto ld_st;
252b5132 11683 case M_SWC3_AB:
df58fc94 11684 gas_assert (!mips_opts.micromips);
252b5132 11685 s = "swc3";
df58fc94 11686 fmt = "E,o(b)";
bdaaa2e1 11687 /* Itbl support may require additional care here. */
252b5132 11688 coproc = 1;
df58fc94 11689 goto ld_st;
252b5132
RH
11690 case M_SWL_AB:
11691 s = "swl";
df58fc94 11692 fmt = MEM12_FMT;
7f3c4072 11693 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11694 goto ld_st;
252b5132
RH
11695 case M_SWR_AB:
11696 s = "swr";
df58fc94 11697 fmt = MEM12_FMT;
7f3c4072 11698 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11699 goto ld_st;
252b5132
RH
11700 case M_SC_AB:
11701 s = "sc";
7361da2c
AB
11702 fmt = LL_SC_FMT;
11703 offbits = (mips_opts.micromips ? 12
11704 : ISA_IS_R6 (mips_opts.isa) ? 9
11705 : 16);
df58fc94 11706 goto ld_st;
252b5132
RH
11707 case M_SCD_AB:
11708 s = "scd";
7361da2c
AB
11709 fmt = LL_SC_FMT;
11710 offbits = (mips_opts.micromips ? 12
11711 : ISA_IS_R6 (mips_opts.isa) ? 9
11712 : 16);
df58fc94 11713 goto ld_st;
d43b4baf
TS
11714 case M_CACHE_AB:
11715 s = "cache";
7361da2c
AB
11716 fmt = (mips_opts.micromips ? "k,~(b)"
11717 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11718 : "k,o(b)");
11719 offbits = (mips_opts.micromips ? 12
11720 : ISA_IS_R6 (mips_opts.isa) ? 9
11721 : 16);
7f3c4072
CM
11722 goto ld_st;
11723 case M_CACHEE_AB:
7f3c4072
CM
11724 s = "cachee";
11725 fmt = "k,+j(b)";
11726 offbits = 9;
df58fc94 11727 goto ld_st;
3eebd5eb
MR
11728 case M_PREF_AB:
11729 s = "pref";
7361da2c
AB
11730 fmt = (mips_opts.micromips ? "k,~(b)"
11731 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11732 : "k,o(b)");
11733 offbits = (mips_opts.micromips ? 12
11734 : ISA_IS_R6 (mips_opts.isa) ? 9
11735 : 16);
7f3c4072
CM
11736 goto ld_st;
11737 case M_PREFE_AB:
7f3c4072
CM
11738 s = "prefe";
11739 fmt = "k,+j(b)";
11740 offbits = 9;
df58fc94 11741 goto ld_st;
252b5132 11742 case M_SDC1_AB:
252b5132 11743 s = "sdc1";
df58fc94 11744 fmt = "T,o(b)";
252b5132 11745 coproc = 1;
bdaaa2e1 11746 /* Itbl support may require additional care here. */
df58fc94 11747 goto ld_st;
252b5132
RH
11748 case M_SDC2_AB:
11749 s = "sdc2";
df58fc94 11750 fmt = COP12_FMT;
7361da2c
AB
11751 offbits = (mips_opts.micromips ? 12
11752 : ISA_IS_R6 (mips_opts.isa) ? 11
11753 : 16);
c77c0862
RS
11754 /* Itbl support may require additional care here. */
11755 coproc = 1;
11756 goto ld_st;
11757 case M_SQC2_AB:
c77c0862 11758 s = "sqc2";
14daeee3 11759 fmt = "+7,o(b)";
bdaaa2e1 11760 /* Itbl support may require additional care here. */
252b5132 11761 coproc = 1;
df58fc94 11762 goto ld_st;
252b5132 11763 case M_SDC3_AB:
df58fc94 11764 gas_assert (!mips_opts.micromips);
252b5132 11765 s = "sdc3";
df58fc94 11766 fmt = "E,o(b)";
bdaaa2e1 11767 /* Itbl support may require additional care here. */
252b5132 11768 coproc = 1;
df58fc94 11769 goto ld_st;
252b5132
RH
11770 case M_SDL_AB:
11771 s = "sdl";
df58fc94 11772 fmt = MEM12_FMT;
7f3c4072 11773 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11774 goto ld_st;
252b5132
RH
11775 case M_SDR_AB:
11776 s = "sdr";
df58fc94 11777 fmt = MEM12_FMT;
7f3c4072 11778 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11779 goto ld_st;
11780 case M_SWP_AB:
df58fc94
RS
11781 gas_assert (mips_opts.micromips);
11782 s = "swp";
11783 fmt = "t,~(b)";
7f3c4072 11784 offbits = 12;
df58fc94
RS
11785 goto ld_st;
11786 case M_SDP_AB:
df58fc94
RS
11787 gas_assert (mips_opts.micromips);
11788 s = "sdp";
11789 fmt = "t,~(b)";
7f3c4072 11790 offbits = 12;
df58fc94
RS
11791 goto ld_st;
11792 case M_SWM_AB:
df58fc94
RS
11793 gas_assert (mips_opts.micromips);
11794 s = "swm";
11795 fmt = "n,~(b)";
7f3c4072 11796 offbits = 12;
df58fc94
RS
11797 goto ld_st;
11798 case M_SDM_AB:
df58fc94
RS
11799 gas_assert (mips_opts.micromips);
11800 s = "sdm";
11801 fmt = "n,~(b)";
7f3c4072 11802 offbits = 12;
df58fc94
RS
11803
11804 ld_st:
8fc2e39e 11805 tempreg = AT;
df58fc94 11806 ld_noat:
c0ebe874 11807 breg = op[2];
f2ae14a1
RS
11808 if (small_offset_p (0, align, 16))
11809 {
11810 /* The first case exists for M_LD_AB and M_SD_AB, which are
11811 macros for o32 but which should act like normal instructions
11812 otherwise. */
11813 if (offbits == 16)
c0ebe874 11814 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11815 offset_reloc[1], offset_reloc[2], breg);
11816 else if (small_offset_p (0, align, offbits))
11817 {
11818 if (offbits == 0)
c0ebe874 11819 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11820 else
c0ebe874 11821 macro_build (NULL, s, fmt, op[0],
c8276761 11822 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11823 }
11824 else
11825 {
11826 if (tempreg == AT)
11827 used_at = 1;
11828 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11829 tempreg, breg, -1, offset_reloc[0],
11830 offset_reloc[1], offset_reloc[2]);
11831 if (offbits == 0)
c0ebe874 11832 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11833 else
c0ebe874 11834 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11835 }
11836 break;
11837 }
11838
11839 if (tempreg == AT)
11840 used_at = 1;
11841
252b5132
RH
11842 if (offset_expr.X_op != O_constant
11843 && offset_expr.X_op != O_symbol)
11844 {
1661c76c 11845 as_bad (_("expression too complex"));
252b5132
RH
11846 offset_expr.X_op = O_constant;
11847 }
11848
2051e8c4
MR
11849 if (HAVE_32BIT_ADDRESSES
11850 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11851 {
11852 char value [32];
11853
11854 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11855 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11856 }
2051e8c4 11857
252b5132
RH
11858 /* A constant expression in PIC code can be handled just as it
11859 is in non PIC code. */
aed1a261
RS
11860 if (offset_expr.X_op == O_constant)
11861 {
f2ae14a1
RS
11862 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11863 offbits == 0 ? 16 : offbits);
11864 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 11865
f2ae14a1
RS
11866 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11867 if (breg != 0)
11868 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11869 tempreg, tempreg, breg);
7f3c4072 11870 if (offbits == 0)
dd6a37e7 11871 {
f2ae14a1 11872 if (offset_expr.X_add_number != 0)
dd6a37e7 11873 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 11874 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 11875 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11876 }
7f3c4072 11877 else if (offbits == 16)
c0ebe874 11878 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 11879 else
c0ebe874 11880 macro_build (NULL, s, fmt, op[0],
c8276761 11881 (int) offset_expr.X_add_number, tempreg);
df58fc94 11882 }
7f3c4072 11883 else if (offbits != 16)
df58fc94 11884 {
7f3c4072 11885 /* The offset field is too narrow to be used for a low-part
2b0f3761 11886 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
11887 register. */
11888 load_address (tempreg, &offset_expr, &used_at);
11889 if (breg != 0)
11890 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11891 tempreg, tempreg, breg);
7f3c4072 11892 if (offbits == 0)
c0ebe874 11893 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11894 else
c0ebe874 11895 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
11896 }
11897 else if (mips_pic == NO_PIC)
252b5132
RH
11898 {
11899 /* If this is a reference to a GP relative symbol, and there
11900 is no base register, we want
c0ebe874 11901 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11902 Otherwise, if there is no base register, we want
11903 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 11904 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
11905 If we have a constant, we need two instructions anyhow,
11906 so we always use the latter form.
11907
11908 If we have a base register, and this is a reference to a
11909 GP relative symbol, we want
11910 addu $tempreg,$breg,$gp
c0ebe874 11911 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11912 Otherwise we want
11913 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11914 addu $tempreg,$tempreg,$breg
c0ebe874 11915 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11916 With a constant we always use the latter case.
76b3015f 11917
d6bc6245
TS
11918 With 64bit address space and no base register and $at usable,
11919 we want
11920 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11921 lui $at,<sym> (BFD_RELOC_HI16_S)
11922 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11923 dsll32 $tempreg,0
11924 daddu $tempreg,$at
c0ebe874 11925 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11926 If we have a base register, we want
11927 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11928 lui $at,<sym> (BFD_RELOC_HI16_S)
11929 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11930 daddu $at,$breg
11931 dsll32 $tempreg,0
11932 daddu $tempreg,$at
c0ebe874 11933 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11934
11935 Without $at we can't generate the optimal path for superscalar
11936 processors here since this would require two temporary registers.
11937 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11938 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11939 dsll $tempreg,16
11940 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11941 dsll $tempreg,16
c0ebe874 11942 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11943 If we have a base register, we want
11944 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11945 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11946 dsll $tempreg,16
11947 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11948 dsll $tempreg,16
11949 daddu $tempreg,$tempreg,$breg
c0ebe874 11950 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11951
6caf9ef4 11952 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11953 the same sequence as in 32bit address space. */
11954 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11955 {
aed1a261 11956 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11957 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11958 {
11959 relax_start (offset_expr.X_add_symbol);
11960 if (breg == 0)
11961 {
c0ebe874 11962 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11963 BFD_RELOC_GPREL16, mips_gp_register);
11964 }
11965 else
11966 {
11967 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11968 tempreg, breg, mips_gp_register);
c0ebe874 11969 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11970 BFD_RELOC_GPREL16, tempreg);
11971 }
11972 relax_switch ();
11973 }
d6bc6245 11974
741fe287 11975 if (used_at == 0 && mips_opts.at)
d6bc6245 11976 {
df58fc94 11977 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11978 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11979 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11980 BFD_RELOC_HI16_S);
11981 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11982 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11983 if (breg != 0)
67c0d1eb 11984 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11985 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11986 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11987 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11988 tempreg);
d6bc6245
TS
11989 used_at = 1;
11990 }
11991 else
11992 {
df58fc94 11993 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11994 BFD_RELOC_MIPS_HIGHEST);
11995 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11996 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11997 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11998 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11999 tempreg, BFD_RELOC_HI16_S);
df58fc94 12000 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12001 if (breg != 0)
67c0d1eb 12002 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12003 tempreg, tempreg, breg);
c0ebe874 12004 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12005 BFD_RELOC_LO16, tempreg);
d6bc6245 12006 }
6caf9ef4
TS
12007
12008 if (mips_relax.sequence)
12009 relax_end ();
8fc2e39e 12010 break;
d6bc6245 12011 }
256ab948 12012
252b5132
RH
12013 if (breg == 0)
12014 {
67c0d1eb 12015 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12016 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12017 {
4d7206a2 12018 relax_start (offset_expr.X_add_symbol);
c0ebe874 12019 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12020 mips_gp_register);
4d7206a2 12021 relax_switch ();
252b5132 12022 }
67c0d1eb 12023 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12024 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12025 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12026 if (mips_relax.sequence)
12027 relax_end ();
252b5132
RH
12028 }
12029 else
12030 {
67c0d1eb 12031 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12032 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12033 {
4d7206a2 12034 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12035 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12036 tempreg, breg, mips_gp_register);
c0ebe874 12037 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12038 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12039 relax_switch ();
252b5132 12040 }
67c0d1eb
RS
12041 macro_build_lui (&offset_expr, tempreg);
12042 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12043 tempreg, tempreg, breg);
c0ebe874 12044 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12045 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12046 if (mips_relax.sequence)
12047 relax_end ();
252b5132
RH
12048 }
12049 }
0a44bf69 12050 else if (!mips_big_got)
252b5132 12051 {
ed6fb7bd 12052 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12053
252b5132
RH
12054 /* If this is a reference to an external symbol, we want
12055 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12056 nop
c0ebe874 12057 <op> op[0],0($tempreg)
252b5132
RH
12058 Otherwise we want
12059 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12060 nop
12061 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12062 <op> op[0],0($tempreg)
f5040a92
AO
12063
12064 For NewABI, we want
12065 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12066 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12067
252b5132
RH
12068 If there is a base register, we add it to $tempreg before
12069 the <op>. If there is a constant, we stick it in the
12070 <op> instruction. We don't handle constants larger than
12071 16 bits, because we have no way to load the upper 16 bits
12072 (actually, we could handle them for the subset of cases
12073 in which we are not using $at). */
9c2799c2 12074 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12075 if (HAVE_NEWABI)
12076 {
67c0d1eb
RS
12077 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12078 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12079 if (breg != 0)
67c0d1eb 12080 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12081 tempreg, tempreg, breg);
c0ebe874 12082 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12083 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12084 break;
12085 }
252b5132
RH
12086 expr1.X_add_number = offset_expr.X_add_number;
12087 offset_expr.X_add_number = 0;
12088 if (expr1.X_add_number < -0x8000
12089 || expr1.X_add_number >= 0x8000)
12090 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12091 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12092 lw_reloc_type, mips_gp_register);
269137b2 12093 load_delay_nop ();
4d7206a2
RS
12094 relax_start (offset_expr.X_add_symbol);
12095 relax_switch ();
67c0d1eb
RS
12096 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12097 tempreg, BFD_RELOC_LO16);
4d7206a2 12098 relax_end ();
252b5132 12099 if (breg != 0)
67c0d1eb 12100 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12101 tempreg, tempreg, breg);
c0ebe874 12102 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12103 }
0a44bf69 12104 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12105 {
67c0d1eb 12106 int gpdelay;
252b5132
RH
12107
12108 /* If this is a reference to an external symbol, we want
12109 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12110 addu $tempreg,$tempreg,$gp
12111 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12112 <op> op[0],0($tempreg)
252b5132
RH
12113 Otherwise we want
12114 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12115 nop
12116 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12117 <op> op[0],0($tempreg)
252b5132
RH
12118 If there is a base register, we add it to $tempreg before
12119 the <op>. If there is a constant, we stick it in the
12120 <op> instruction. We don't handle constants larger than
12121 16 bits, because we have no way to load the upper 16 bits
12122 (actually, we could handle them for the subset of cases
f5040a92 12123 in which we are not using $at). */
9c2799c2 12124 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12125 expr1.X_add_number = offset_expr.X_add_number;
12126 offset_expr.X_add_number = 0;
12127 if (expr1.X_add_number < -0x8000
12128 || expr1.X_add_number >= 0x8000)
12129 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12130 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12131 relax_start (offset_expr.X_add_symbol);
df58fc94 12132 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12133 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12134 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12135 mips_gp_register);
12136 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12137 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12138 relax_switch ();
67c0d1eb
RS
12139 if (gpdelay)
12140 macro_build (NULL, "nop", "");
12141 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12142 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12143 load_delay_nop ();
67c0d1eb
RS
12144 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12145 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12146 relax_end ();
12147
252b5132 12148 if (breg != 0)
67c0d1eb 12149 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12150 tempreg, tempreg, breg);
c0ebe874 12151 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12152 }
0a44bf69 12153 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12154 {
f5040a92
AO
12155 /* If this is a reference to an external symbol, we want
12156 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12157 add $tempreg,$tempreg,$gp
12158 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12159 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12160 Otherwise, for local symbols, we want:
12161 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12162 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12163 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12164 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12165 offset_expr.X_add_number = 0;
12166 if (expr1.X_add_number < -0x8000
12167 || expr1.X_add_number >= 0x8000)
12168 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12169 relax_start (offset_expr.X_add_symbol);
df58fc94 12170 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12171 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12172 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12173 mips_gp_register);
12174 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12175 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12176 if (breg != 0)
67c0d1eb 12177 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12178 tempreg, tempreg, breg);
c0ebe874 12179 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12180
4d7206a2 12181 relax_switch ();
f5040a92 12182 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12183 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12184 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12185 if (breg != 0)
67c0d1eb 12186 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12187 tempreg, tempreg, breg);
c0ebe874 12188 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12189 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12190 relax_end ();
f5040a92 12191 }
252b5132
RH
12192 else
12193 abort ();
12194
252b5132
RH
12195 break;
12196
833794fc
MR
12197 case M_JRADDIUSP:
12198 gas_assert (mips_opts.micromips);
12199 gas_assert (mips_opts.insn32);
12200 start_noreorder ();
12201 macro_build (NULL, "jr", "s", RA);
c0ebe874 12202 expr1.X_add_number = op[0] << 2;
833794fc
MR
12203 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12204 end_noreorder ();
12205 break;
12206
12207 case M_JRC:
12208 gas_assert (mips_opts.micromips);
12209 gas_assert (mips_opts.insn32);
c0ebe874 12210 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12211 if (mips_opts.noreorder)
12212 macro_build (NULL, "nop", "");
12213 break;
12214
252b5132
RH
12215 case M_LI:
12216 case M_LI_S:
c0ebe874 12217 load_register (op[0], &imm_expr, 0);
8fc2e39e 12218 break;
252b5132
RH
12219
12220 case M_DLI:
c0ebe874 12221 load_register (op[0], &imm_expr, 1);
8fc2e39e 12222 break;
252b5132
RH
12223
12224 case M_LI_SS:
12225 if (imm_expr.X_op == O_constant)
12226 {
8fc2e39e 12227 used_at = 1;
67c0d1eb 12228 load_register (AT, &imm_expr, 0);
c0ebe874 12229 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12230 break;
12231 }
12232 else
12233 {
b0e6f033
RS
12234 gas_assert (imm_expr.X_op == O_absent
12235 && offset_expr.X_op == O_symbol
90ecf173
MR
12236 && strcmp (segment_name (S_GET_SEGMENT
12237 (offset_expr.X_add_symbol)),
12238 ".lit4") == 0
12239 && offset_expr.X_add_number == 0);
c0ebe874 12240 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12241 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12242 break;
252b5132
RH
12243 }
12244
12245 case M_LI_D:
ca4e0257
RS
12246 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12247 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12248 order 32 bits of the value and the low order 32 bits are either
12249 zero or in OFFSET_EXPR. */
b0e6f033 12250 if (imm_expr.X_op == O_constant)
252b5132 12251 {
bad1aba3 12252 if (GPR_SIZE == 64)
c0ebe874 12253 load_register (op[0], &imm_expr, 1);
252b5132
RH
12254 else
12255 {
12256 int hreg, lreg;
12257
12258 if (target_big_endian)
12259 {
c0ebe874
RS
12260 hreg = op[0];
12261 lreg = op[0] + 1;
252b5132
RH
12262 }
12263 else
12264 {
c0ebe874
RS
12265 hreg = op[0] + 1;
12266 lreg = op[0];
252b5132
RH
12267 }
12268
12269 if (hreg <= 31)
67c0d1eb 12270 load_register (hreg, &imm_expr, 0);
252b5132
RH
12271 if (lreg <= 31)
12272 {
12273 if (offset_expr.X_op == O_absent)
67c0d1eb 12274 move_register (lreg, 0);
252b5132
RH
12275 else
12276 {
9c2799c2 12277 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12278 load_register (lreg, &offset_expr, 0);
252b5132
RH
12279 }
12280 }
12281 }
8fc2e39e 12282 break;
252b5132 12283 }
b0e6f033 12284 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12285
12286 /* We know that sym is in the .rdata section. First we get the
12287 upper 16 bits of the address. */
12288 if (mips_pic == NO_PIC)
12289 {
67c0d1eb 12290 macro_build_lui (&offset_expr, AT);
8fc2e39e 12291 used_at = 1;
252b5132 12292 }
0a44bf69 12293 else
252b5132 12294 {
67c0d1eb
RS
12295 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12296 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12297 used_at = 1;
252b5132 12298 }
bdaaa2e1 12299
252b5132 12300 /* Now we load the register(s). */
bad1aba3 12301 if (GPR_SIZE == 64)
8fc2e39e
TS
12302 {
12303 used_at = 1;
c0ebe874
RS
12304 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12305 BFD_RELOC_LO16, AT);
8fc2e39e 12306 }
252b5132
RH
12307 else
12308 {
8fc2e39e 12309 used_at = 1;
c0ebe874
RS
12310 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12311 BFD_RELOC_LO16, AT);
12312 if (op[0] != RA)
252b5132
RH
12313 {
12314 /* FIXME: How in the world do we deal with the possible
12315 overflow here? */
12316 offset_expr.X_add_number += 4;
67c0d1eb 12317 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12318 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12319 }
12320 }
252b5132
RH
12321 break;
12322
12323 case M_LI_DD:
ca4e0257
RS
12324 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12325 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12326 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12327 the value and the low order 32 bits are either zero or in
12328 OFFSET_EXPR. */
b0e6f033 12329 if (imm_expr.X_op == O_constant)
252b5132 12330 {
8fc2e39e 12331 used_at = 1;
bad1aba3 12332 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12333 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12334 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12335 else
12336 {
351cdf24
MF
12337 if (ISA_HAS_MXHC1 (mips_opts.isa))
12338 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12339 else if (FPR_SIZE != 32)
12340 as_bad (_("Unable to generate `%s' compliant code "
12341 "without mthc1"),
12342 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12343 else
12344 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12345 if (offset_expr.X_op == O_absent)
c0ebe874 12346 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12347 else
12348 {
9c2799c2 12349 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12350 load_register (AT, &offset_expr, 0);
c0ebe874 12351 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12352 }
12353 }
12354 break;
12355 }
12356
b0e6f033
RS
12357 gas_assert (imm_expr.X_op == O_absent
12358 && offset_expr.X_op == O_symbol
90ecf173 12359 && offset_expr.X_add_number == 0);
252b5132
RH
12360 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12361 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12362 {
12363 op[2] = mips_gp_register;
f2ae14a1
RS
12364 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12365 offset_reloc[1] = BFD_RELOC_UNUSED;
12366 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12367 }
12368 else
12369 {
9c2799c2 12370 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12371 used_at = 1;
0a44bf69 12372 if (mips_pic != NO_PIC)
67c0d1eb
RS
12373 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12374 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12375 else
12376 {
12377 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12378 macro_build_lui (&offset_expr, AT);
252b5132 12379 }
bdaaa2e1 12380
c0ebe874 12381 op[2] = AT;
f2ae14a1
RS
12382 offset_reloc[0] = BFD_RELOC_LO16;
12383 offset_reloc[1] = BFD_RELOC_UNUSED;
12384 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12385 }
f2ae14a1
RS
12386 align = 8;
12387 /* Fall through */
c4a68bea 12388
252b5132
RH
12389 case M_L_DAB:
12390 /*
12391 * The MIPS assembler seems to check for X_add_number not
12392 * being double aligned and generating:
12393 * lui at,%hi(foo+1)
12394 * addu at,at,v1
12395 * addiu at,at,%lo(foo+1)
12396 * lwc1 f2,0(at)
12397 * lwc1 f3,4(at)
12398 * But, the resulting address is the same after relocation so why
12399 * generate the extra instruction?
12400 */
bdaaa2e1 12401 /* Itbl support may require additional care here. */
252b5132 12402 coproc = 1;
df58fc94 12403 fmt = "T,o(b)";
0aa27725 12404 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12405 {
12406 s = "ldc1";
df58fc94 12407 goto ld_st;
252b5132 12408 }
252b5132 12409 s = "lwc1";
252b5132
RH
12410 goto ldd_std;
12411
12412 case M_S_DAB:
df58fc94
RS
12413 gas_assert (!mips_opts.micromips);
12414 /* Itbl support may require additional care here. */
12415 coproc = 1;
12416 fmt = "T,o(b)";
0aa27725 12417 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12418 {
12419 s = "sdc1";
df58fc94 12420 goto ld_st;
252b5132 12421 }
252b5132 12422 s = "swc1";
252b5132
RH
12423 goto ldd_std;
12424
e407c74b
NC
12425 case M_LQ_AB:
12426 fmt = "t,o(b)";
12427 s = "lq";
12428 goto ld;
12429
12430 case M_SQ_AB:
12431 fmt = "t,o(b)";
12432 s = "sq";
12433 goto ld_st;
12434
252b5132 12435 case M_LD_AB:
df58fc94 12436 fmt = "t,o(b)";
bad1aba3 12437 if (GPR_SIZE == 64)
252b5132
RH
12438 {
12439 s = "ld";
12440 goto ld;
12441 }
252b5132 12442 s = "lw";
252b5132
RH
12443 goto ldd_std;
12444
12445 case M_SD_AB:
df58fc94 12446 fmt = "t,o(b)";
bad1aba3 12447 if (GPR_SIZE == 64)
252b5132
RH
12448 {
12449 s = "sd";
df58fc94 12450 goto ld_st;
252b5132 12451 }
252b5132 12452 s = "sw";
252b5132
RH
12453
12454 ldd_std:
f2ae14a1
RS
12455 /* Even on a big endian machine $fn comes before $fn+1. We have
12456 to adjust when loading from memory. We set coproc if we must
12457 load $fn+1 first. */
12458 /* Itbl support may require additional care here. */
12459 if (!target_big_endian)
12460 coproc = 0;
12461
c0ebe874 12462 breg = op[2];
f2ae14a1
RS
12463 if (small_offset_p (0, align, 16))
12464 {
12465 ep = &offset_expr;
12466 if (!small_offset_p (4, align, 16))
12467 {
12468 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12469 -1, offset_reloc[0], offset_reloc[1],
12470 offset_reloc[2]);
12471 expr1.X_add_number = 0;
12472 ep = &expr1;
12473 breg = AT;
12474 used_at = 1;
12475 offset_reloc[0] = BFD_RELOC_LO16;
12476 offset_reloc[1] = BFD_RELOC_UNUSED;
12477 offset_reloc[2] = BFD_RELOC_UNUSED;
12478 }
c0ebe874 12479 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12480 {
12481 ep->X_add_number += 4;
c0ebe874 12482 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12483 offset_reloc[1], offset_reloc[2], breg);
12484 ep->X_add_number -= 4;
c0ebe874 12485 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12486 offset_reloc[1], offset_reloc[2], breg);
12487 }
12488 else
12489 {
c0ebe874 12490 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12491 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12492 breg);
12493 ep->X_add_number += 4;
c0ebe874 12494 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12495 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12496 breg);
12497 }
12498 break;
12499 }
12500
252b5132
RH
12501 if (offset_expr.X_op != O_symbol
12502 && offset_expr.X_op != O_constant)
12503 {
1661c76c 12504 as_bad (_("expression too complex"));
252b5132
RH
12505 offset_expr.X_op = O_constant;
12506 }
12507
2051e8c4
MR
12508 if (HAVE_32BIT_ADDRESSES
12509 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12510 {
12511 char value [32];
12512
12513 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12514 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12515 }
2051e8c4 12516
90ecf173 12517 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12518 {
12519 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12520 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12521 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12522 If we have a base register, we use this
12523 addu $at,$breg,$gp
c0ebe874
RS
12524 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12525 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12526 If this is not a GP relative symbol, we want
12527 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12528 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12529 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12530 If there is a base register, we add it to $at after the
12531 lui instruction. If there is a constant, we always use
12532 the last case. */
39a59cf8
MR
12533 if (offset_expr.X_op == O_symbol
12534 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12535 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12536 {
4d7206a2 12537 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12538 if (breg == 0)
12539 {
c9914766 12540 tempreg = mips_gp_register;
252b5132
RH
12541 }
12542 else
12543 {
67c0d1eb 12544 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12545 AT, breg, mips_gp_register);
252b5132 12546 tempreg = AT;
252b5132
RH
12547 used_at = 1;
12548 }
12549
beae10d5 12550 /* Itbl support may require additional care here. */
c0ebe874 12551 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12552 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12553 offset_expr.X_add_number += 4;
12554
12555 /* Set mips_optimize to 2 to avoid inserting an
12556 undesired nop. */
12557 hold_mips_optimize = mips_optimize;
12558 mips_optimize = 2;
beae10d5 12559 /* Itbl support may require additional care here. */
c0ebe874 12560 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12561 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12562 mips_optimize = hold_mips_optimize;
12563
4d7206a2 12564 relax_switch ();
252b5132 12565
0970e49e 12566 offset_expr.X_add_number -= 4;
252b5132 12567 }
8fc2e39e 12568 used_at = 1;
f2ae14a1
RS
12569 if (offset_high_part (offset_expr.X_add_number, 16)
12570 != offset_high_part (offset_expr.X_add_number + 4, 16))
12571 {
12572 load_address (AT, &offset_expr, &used_at);
12573 offset_expr.X_op = O_constant;
12574 offset_expr.X_add_number = 0;
12575 }
12576 else
12577 macro_build_lui (&offset_expr, AT);
252b5132 12578 if (breg != 0)
67c0d1eb 12579 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12580 /* Itbl support may require additional care here. */
c0ebe874 12581 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12582 BFD_RELOC_LO16, AT);
252b5132
RH
12583 /* FIXME: How do we handle overflow here? */
12584 offset_expr.X_add_number += 4;
beae10d5 12585 /* Itbl support may require additional care here. */
c0ebe874 12586 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12587 BFD_RELOC_LO16, AT);
4d7206a2
RS
12588 if (mips_relax.sequence)
12589 relax_end ();
bdaaa2e1 12590 }
0a44bf69 12591 else if (!mips_big_got)
252b5132 12592 {
252b5132
RH
12593 /* If this is a reference to an external symbol, we want
12594 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12595 nop
c0ebe874
RS
12596 <op> op[0],0($at)
12597 <op> op[0]+1,4($at)
252b5132
RH
12598 Otherwise we want
12599 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12600 nop
c0ebe874
RS
12601 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12602 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12603 If there is a base register we add it to $at before the
12604 lwc1 instructions. If there is a constant we include it
12605 in the lwc1 instructions. */
12606 used_at = 1;
12607 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12608 if (expr1.X_add_number < -0x8000
12609 || expr1.X_add_number >= 0x8000 - 4)
12610 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12611 load_got_offset (AT, &offset_expr);
269137b2 12612 load_delay_nop ();
252b5132 12613 if (breg != 0)
67c0d1eb 12614 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12615
12616 /* Set mips_optimize to 2 to avoid inserting an undesired
12617 nop. */
12618 hold_mips_optimize = mips_optimize;
12619 mips_optimize = 2;
4d7206a2 12620
beae10d5 12621 /* Itbl support may require additional care here. */
4d7206a2 12622 relax_start (offset_expr.X_add_symbol);
c0ebe874 12623 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12624 BFD_RELOC_LO16, AT);
4d7206a2 12625 expr1.X_add_number += 4;
c0ebe874 12626 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12627 BFD_RELOC_LO16, AT);
4d7206a2 12628 relax_switch ();
c0ebe874 12629 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12630 BFD_RELOC_LO16, AT);
4d7206a2 12631 offset_expr.X_add_number += 4;
c0ebe874 12632 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12633 BFD_RELOC_LO16, AT);
4d7206a2 12634 relax_end ();
252b5132 12635
4d7206a2 12636 mips_optimize = hold_mips_optimize;
252b5132 12637 }
0a44bf69 12638 else if (mips_big_got)
252b5132 12639 {
67c0d1eb 12640 int gpdelay;
252b5132
RH
12641
12642 /* If this is a reference to an external symbol, we want
12643 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12644 addu $at,$at,$gp
12645 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12646 nop
c0ebe874
RS
12647 <op> op[0],0($at)
12648 <op> op[0]+1,4($at)
252b5132
RH
12649 Otherwise we want
12650 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12651 nop
c0ebe874
RS
12652 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12653 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12654 If there is a base register we add it to $at before the
12655 lwc1 instructions. If there is a constant we include it
12656 in the lwc1 instructions. */
12657 used_at = 1;
12658 expr1.X_add_number = offset_expr.X_add_number;
12659 offset_expr.X_add_number = 0;
12660 if (expr1.X_add_number < -0x8000
12661 || expr1.X_add_number >= 0x8000 - 4)
12662 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12663 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12664 relax_start (offset_expr.X_add_symbol);
df58fc94 12665 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12666 AT, BFD_RELOC_MIPS_GOT_HI16);
12667 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12668 AT, AT, mips_gp_register);
67c0d1eb 12669 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12670 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12671 load_delay_nop ();
252b5132 12672 if (breg != 0)
67c0d1eb 12673 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12674 /* Itbl support may require additional care here. */
c0ebe874 12675 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12676 BFD_RELOC_LO16, AT);
252b5132
RH
12677 expr1.X_add_number += 4;
12678
12679 /* Set mips_optimize to 2 to avoid inserting an undesired
12680 nop. */
12681 hold_mips_optimize = mips_optimize;
12682 mips_optimize = 2;
beae10d5 12683 /* Itbl support may require additional care here. */
c0ebe874 12684 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12685 BFD_RELOC_LO16, AT);
252b5132
RH
12686 mips_optimize = hold_mips_optimize;
12687 expr1.X_add_number -= 4;
12688
4d7206a2
RS
12689 relax_switch ();
12690 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12691 if (gpdelay)
12692 macro_build (NULL, "nop", "");
12693 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12694 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12695 load_delay_nop ();
252b5132 12696 if (breg != 0)
67c0d1eb 12697 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12698 /* Itbl support may require additional care here. */
c0ebe874 12699 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12700 BFD_RELOC_LO16, AT);
4d7206a2 12701 offset_expr.X_add_number += 4;
252b5132
RH
12702
12703 /* Set mips_optimize to 2 to avoid inserting an undesired
12704 nop. */
12705 hold_mips_optimize = mips_optimize;
12706 mips_optimize = 2;
beae10d5 12707 /* Itbl support may require additional care here. */
c0ebe874 12708 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12709 BFD_RELOC_LO16, AT);
252b5132 12710 mips_optimize = hold_mips_optimize;
4d7206a2 12711 relax_end ();
252b5132 12712 }
252b5132
RH
12713 else
12714 abort ();
12715
252b5132 12716 break;
3739860c 12717
dd6a37e7 12718 case M_SAA_AB:
dd6a37e7 12719 s = "saa";
0db377d0 12720 goto saa_saad;
dd6a37e7 12721 case M_SAAD_AB:
dd6a37e7 12722 s = "saad";
0db377d0
MR
12723 saa_saad:
12724 gas_assert (!mips_opts.micromips);
7f3c4072 12725 offbits = 0;
dd6a37e7
AP
12726 fmt = "t,(b)";
12727 goto ld_st;
12728
252b5132
RH
12729 /* New code added to support COPZ instructions.
12730 This code builds table entries out of the macros in mip_opcodes.
12731 R4000 uses interlocks to handle coproc delays.
12732 Other chips (like the R3000) require nops to be inserted for delays.
12733
f72c8c98 12734 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12735 In order to fill delay slots for non-interlocked chips,
12736 we must have a way to specify delays based on the coprocessor.
12737 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12738 What are the side-effects of the cop instruction?
12739 What cache support might we have and what are its effects?
12740 Both coprocessor & memory require delays. how long???
bdaaa2e1 12741 What registers are read/set/modified?
252b5132
RH
12742
12743 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12744 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12745
12746 case M_COP0:
12747 s = "c0";
12748 goto copz;
12749 case M_COP1:
12750 s = "c1";
12751 goto copz;
12752 case M_COP2:
12753 s = "c2";
12754 goto copz;
12755 case M_COP3:
12756 s = "c3";
12757 copz:
df58fc94 12758 gas_assert (!mips_opts.micromips);
252b5132
RH
12759 /* For now we just do C (same as Cz). The parameter will be
12760 stored in insn_opcode by mips_ip. */
c8276761 12761 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12762 break;
252b5132 12763
ea1fb5dc 12764 case M_MOVE:
c0ebe874 12765 move_register (op[0], op[1]);
8fc2e39e 12766 break;
ea1fb5dc 12767
833794fc
MR
12768 case M_MOVEP:
12769 gas_assert (mips_opts.micromips);
12770 gas_assert (mips_opts.insn32);
c0ebe874
RS
12771 move_register (micromips_to_32_reg_h_map1[op[0]],
12772 micromips_to_32_reg_m_map[op[1]]);
12773 move_register (micromips_to_32_reg_h_map2[op[0]],
12774 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12775 break;
12776
252b5132
RH
12777 case M_DMUL:
12778 dbl = 1;
1a0670f3 12779 /* Fall through. */
252b5132 12780 case M_MUL:
e407c74b 12781 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12782 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12783 op[2]);
e407c74b
NC
12784 else
12785 {
c0ebe874
RS
12786 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12787 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12788 }
8fc2e39e 12789 break;
252b5132
RH
12790
12791 case M_DMUL_I:
12792 dbl = 1;
1a0670f3 12793 /* Fall through. */
252b5132
RH
12794 case M_MUL_I:
12795 /* The MIPS assembler some times generates shifts and adds. I'm
12796 not trying to be that fancy. GCC should do this for us
12797 anyway. */
8fc2e39e 12798 used_at = 1;
67c0d1eb 12799 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12800 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12801 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12802 break;
12803
12804 case M_DMULO_I:
12805 dbl = 1;
1a0670f3 12806 /* Fall through. */
252b5132
RH
12807 case M_MULO_I:
12808 imm = 1;
12809 goto do_mulo;
12810
12811 case M_DMULO:
12812 dbl = 1;
1a0670f3 12813 /* Fall through. */
252b5132
RH
12814 case M_MULO:
12815 do_mulo:
7d10b47d 12816 start_noreorder ();
8fc2e39e 12817 used_at = 1;
252b5132 12818 if (imm)
67c0d1eb 12819 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12820 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12821 op[1], imm ? AT : op[2]);
12822 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12823 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12824 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12825 if (mips_trap)
c0ebe874 12826 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12827 else
12828 {
df58fc94
RS
12829 if (mips_opts.micromips)
12830 micromips_label_expr (&label_expr);
12831 else
12832 label_expr.X_add_number = 8;
c0ebe874 12833 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12834 macro_build (NULL, "nop", "");
df58fc94
RS
12835 macro_build (NULL, "break", BRK_FMT, 6);
12836 if (mips_opts.micromips)
12837 micromips_add_label ();
252b5132 12838 }
7d10b47d 12839 end_noreorder ();
c0ebe874 12840 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12841 break;
12842
12843 case M_DMULOU_I:
12844 dbl = 1;
1a0670f3 12845 /* Fall through. */
252b5132
RH
12846 case M_MULOU_I:
12847 imm = 1;
12848 goto do_mulou;
12849
12850 case M_DMULOU:
12851 dbl = 1;
1a0670f3 12852 /* Fall through. */
252b5132
RH
12853 case M_MULOU:
12854 do_mulou:
7d10b47d 12855 start_noreorder ();
8fc2e39e 12856 used_at = 1;
252b5132 12857 if (imm)
67c0d1eb
RS
12858 load_register (AT, &imm_expr, dbl);
12859 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 12860 op[1], imm ? AT : op[2]);
df58fc94 12861 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 12862 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 12863 if (mips_trap)
df58fc94 12864 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
12865 else
12866 {
df58fc94
RS
12867 if (mips_opts.micromips)
12868 micromips_label_expr (&label_expr);
12869 else
12870 label_expr.X_add_number = 8;
12871 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 12872 macro_build (NULL, "nop", "");
df58fc94
RS
12873 macro_build (NULL, "break", BRK_FMT, 6);
12874 if (mips_opts.micromips)
12875 micromips_add_label ();
252b5132 12876 }
7d10b47d 12877 end_noreorder ();
252b5132
RH
12878 break;
12879
771c7ce4 12880 case M_DROL:
fef14a42 12881 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12882 {
c0ebe874 12883 if (op[0] == op[1])
82dd0097
CD
12884 {
12885 tempreg = AT;
12886 used_at = 1;
12887 }
12888 else
c0ebe874
RS
12889 tempreg = op[0];
12890 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12891 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12892 break;
82dd0097 12893 }
8fc2e39e 12894 used_at = 1;
c0ebe874
RS
12895 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12896 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12897 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12898 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12899 break;
12900
252b5132 12901 case M_ROL:
fef14a42 12902 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12903 {
c0ebe874 12904 if (op[0] == op[1])
82dd0097
CD
12905 {
12906 tempreg = AT;
12907 used_at = 1;
12908 }
12909 else
c0ebe874
RS
12910 tempreg = op[0];
12911 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12912 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12913 break;
82dd0097 12914 }
8fc2e39e 12915 used_at = 1;
c0ebe874
RS
12916 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12917 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12918 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12919 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12920 break;
12921
771c7ce4
TS
12922 case M_DROL_I:
12923 {
12924 unsigned int rot;
e0471c16
TS
12925 const char *l;
12926 const char *rr;
771c7ce4 12927
771c7ce4 12928 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12929 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12930 {
12931 rot = (64 - rot) & 0x3f;
12932 if (rot >= 32)
c0ebe874 12933 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12934 else
c0ebe874 12935 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12936 break;
60b63b72 12937 }
483fc7cd 12938 if (rot == 0)
483fc7cd 12939 {
c0ebe874 12940 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12941 break;
483fc7cd 12942 }
82dd0097 12943 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12944 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12945 rot &= 0x1f;
8fc2e39e 12946 used_at = 1;
c0ebe874
RS
12947 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12948 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12949 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12950 }
12951 break;
12952
252b5132 12953 case M_ROL_I:
771c7ce4
TS
12954 {
12955 unsigned int rot;
12956
771c7ce4 12957 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12958 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12959 {
c0ebe874
RS
12960 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12961 (32 - rot) & 0x1f);
8fc2e39e 12962 break;
60b63b72 12963 }
483fc7cd 12964 if (rot == 0)
483fc7cd 12965 {
c0ebe874 12966 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12967 break;
483fc7cd 12968 }
8fc2e39e 12969 used_at = 1;
c0ebe874
RS
12970 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12971 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12972 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12973 }
12974 break;
12975
12976 case M_DROR:
fef14a42 12977 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12978 {
c0ebe874 12979 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12980 break;
82dd0097 12981 }
8fc2e39e 12982 used_at = 1;
c0ebe874
RS
12983 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12984 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12985 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12986 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12987 break;
12988
12989 case M_ROR:
fef14a42 12990 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12991 {
c0ebe874 12992 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12993 break;
82dd0097 12994 }
8fc2e39e 12995 used_at = 1;
c0ebe874
RS
12996 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12997 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12998 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12999 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13000 break;
13001
771c7ce4
TS
13002 case M_DROR_I:
13003 {
13004 unsigned int rot;
e0471c16
TS
13005 const char *l;
13006 const char *rr;
771c7ce4 13007
771c7ce4 13008 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13009 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13010 {
13011 if (rot >= 32)
c0ebe874 13012 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13013 else
c0ebe874 13014 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13015 break;
82dd0097 13016 }
483fc7cd 13017 if (rot == 0)
483fc7cd 13018 {
c0ebe874 13019 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13020 break;
483fc7cd 13021 }
91d6fa6a 13022 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13023 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13024 rot &= 0x1f;
8fc2e39e 13025 used_at = 1;
c0ebe874
RS
13026 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13027 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13028 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13029 }
13030 break;
13031
252b5132 13032 case M_ROR_I:
771c7ce4
TS
13033 {
13034 unsigned int rot;
13035
771c7ce4 13036 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13037 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13038 {
c0ebe874 13039 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13040 break;
82dd0097 13041 }
483fc7cd 13042 if (rot == 0)
483fc7cd 13043 {
c0ebe874 13044 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13045 break;
483fc7cd 13046 }
8fc2e39e 13047 used_at = 1;
c0ebe874
RS
13048 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13049 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13050 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13051 }
252b5132
RH
13052 break;
13053
252b5132 13054 case M_SEQ:
c0ebe874
RS
13055 if (op[1] == 0)
13056 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13057 else if (op[2] == 0)
13058 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13059 else
13060 {
c0ebe874
RS
13061 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13062 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13063 }
8fc2e39e 13064 break;
252b5132
RH
13065
13066 case M_SEQ_I:
b0e6f033 13067 if (imm_expr.X_add_number == 0)
252b5132 13068 {
c0ebe874 13069 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13070 break;
252b5132 13071 }
c0ebe874 13072 if (op[1] == 0)
252b5132 13073 {
1661c76c 13074 as_warn (_("instruction %s: result is always false"),
252b5132 13075 ip->insn_mo->name);
c0ebe874 13076 move_register (op[0], 0);
8fc2e39e 13077 break;
252b5132 13078 }
dd3cbb7e
NC
13079 if (CPU_HAS_SEQ (mips_opts.arch)
13080 && -512 <= imm_expr.X_add_number
13081 && imm_expr.X_add_number < 512)
13082 {
c0ebe874 13083 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13084 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13085 break;
13086 }
b0e6f033 13087 if (imm_expr.X_add_number >= 0
252b5132 13088 && imm_expr.X_add_number < 0x10000)
c0ebe874 13089 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13090 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13091 && imm_expr.X_add_number < 0)
13092 {
13093 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13094 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13095 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13096 }
dd3cbb7e
NC
13097 else if (CPU_HAS_SEQ (mips_opts.arch))
13098 {
13099 used_at = 1;
bad1aba3 13100 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13101 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13102 break;
13103 }
252b5132
RH
13104 else
13105 {
bad1aba3 13106 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13107 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13108 used_at = 1;
13109 }
c0ebe874 13110 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13111 break;
252b5132 13112
c0ebe874 13113 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13114 s = "slt";
13115 goto sge;
13116 case M_SGEU:
13117 s = "sltu";
13118 sge:
c0ebe874
RS
13119 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13120 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13121 break;
252b5132 13122
c0ebe874 13123 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13124 case M_SGEU_I:
b0e6f033 13125 if (imm_expr.X_add_number >= -0x8000
252b5132 13126 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13127 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13128 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13129 else
13130 {
bad1aba3 13131 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13132 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13133 op[0], op[1], AT);
252b5132
RH
13134 used_at = 1;
13135 }
c0ebe874 13136 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13137 break;
252b5132 13138
c0ebe874 13139 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13140 s = "slt";
13141 goto sgt;
13142 case M_SGTU:
13143 s = "sltu";
13144 sgt:
c0ebe874 13145 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13146 break;
252b5132 13147
c0ebe874 13148 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13149 s = "slt";
13150 goto sgti;
13151 case M_SGTU_I:
13152 s = "sltu";
13153 sgti:
8fc2e39e 13154 used_at = 1;
bad1aba3 13155 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13156 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13157 break;
13158
c0ebe874 13159 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13160 s = "slt";
13161 goto sle;
13162 case M_SLEU:
13163 s = "sltu";
13164 sle:
c0ebe874
RS
13165 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13166 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13167 break;
252b5132 13168
c0ebe874 13169 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13170 s = "slt";
13171 goto slei;
13172 case M_SLEU_I:
13173 s = "sltu";
13174 slei:
8fc2e39e 13175 used_at = 1;
bad1aba3 13176 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13177 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13178 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13179 break;
13180
13181 case M_SLT_I:
b0e6f033 13182 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13183 && imm_expr.X_add_number < 0x8000)
13184 {
c0ebe874
RS
13185 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13186 BFD_RELOC_LO16);
8fc2e39e 13187 break;
252b5132 13188 }
8fc2e39e 13189 used_at = 1;
bad1aba3 13190 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13191 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13192 break;
13193
13194 case M_SLTU_I:
b0e6f033 13195 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13196 && imm_expr.X_add_number < 0x8000)
13197 {
c0ebe874 13198 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13199 BFD_RELOC_LO16);
8fc2e39e 13200 break;
252b5132 13201 }
8fc2e39e 13202 used_at = 1;
bad1aba3 13203 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13204 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13205 break;
13206
13207 case M_SNE:
c0ebe874
RS
13208 if (op[1] == 0)
13209 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13210 else if (op[2] == 0)
13211 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13212 else
13213 {
c0ebe874
RS
13214 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13215 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13216 }
8fc2e39e 13217 break;
252b5132
RH
13218
13219 case M_SNE_I:
b0e6f033 13220 if (imm_expr.X_add_number == 0)
252b5132 13221 {
c0ebe874 13222 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13223 break;
252b5132 13224 }
c0ebe874 13225 if (op[1] == 0)
252b5132 13226 {
1661c76c 13227 as_warn (_("instruction %s: result is always true"),
252b5132 13228 ip->insn_mo->name);
bad1aba3 13229 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13230 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13231 break;
252b5132 13232 }
dd3cbb7e
NC
13233 if (CPU_HAS_SEQ (mips_opts.arch)
13234 && -512 <= imm_expr.X_add_number
13235 && imm_expr.X_add_number < 512)
13236 {
c0ebe874 13237 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13238 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13239 break;
13240 }
b0e6f033 13241 if (imm_expr.X_add_number >= 0
252b5132
RH
13242 && imm_expr.X_add_number < 0x10000)
13243 {
c0ebe874
RS
13244 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13245 BFD_RELOC_LO16);
252b5132 13246 }
b0e6f033 13247 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13248 && imm_expr.X_add_number < 0)
13249 {
13250 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13251 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13252 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13253 }
dd3cbb7e
NC
13254 else if (CPU_HAS_SEQ (mips_opts.arch))
13255 {
13256 used_at = 1;
bad1aba3 13257 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13258 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13259 break;
13260 }
252b5132
RH
13261 else
13262 {
bad1aba3 13263 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13264 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13265 used_at = 1;
13266 }
c0ebe874 13267 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13268 break;
252b5132 13269
df58fc94
RS
13270 case M_SUB_I:
13271 s = "addi";
13272 s2 = "sub";
13273 goto do_subi;
13274 case M_SUBU_I:
13275 s = "addiu";
13276 s2 = "subu";
13277 goto do_subi;
252b5132
RH
13278 case M_DSUB_I:
13279 dbl = 1;
df58fc94
RS
13280 s = "daddi";
13281 s2 = "dsub";
13282 if (!mips_opts.micromips)
13283 goto do_subi;
b0e6f033 13284 if (imm_expr.X_add_number > -0x200
df58fc94 13285 && imm_expr.X_add_number <= 0x200)
252b5132 13286 {
b0e6f033
RS
13287 macro_build (NULL, s, "t,r,.", op[0], op[1],
13288 (int) -imm_expr.X_add_number);
8fc2e39e 13289 break;
252b5132 13290 }
df58fc94 13291 goto do_subi_i;
252b5132
RH
13292 case M_DSUBU_I:
13293 dbl = 1;
df58fc94
RS
13294 s = "daddiu";
13295 s2 = "dsubu";
13296 do_subi:
b0e6f033 13297 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13298 && imm_expr.X_add_number <= 0x8000)
13299 {
13300 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13301 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13302 break;
252b5132 13303 }
df58fc94 13304 do_subi_i:
8fc2e39e 13305 used_at = 1;
67c0d1eb 13306 load_register (AT, &imm_expr, dbl);
c0ebe874 13307 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13308 break;
13309
13310 case M_TEQ_I:
13311 s = "teq";
13312 goto trap;
13313 case M_TGE_I:
13314 s = "tge";
13315 goto trap;
13316 case M_TGEU_I:
13317 s = "tgeu";
13318 goto trap;
13319 case M_TLT_I:
13320 s = "tlt";
13321 goto trap;
13322 case M_TLTU_I:
13323 s = "tltu";
13324 goto trap;
13325 case M_TNE_I:
13326 s = "tne";
13327 trap:
8fc2e39e 13328 used_at = 1;
bad1aba3 13329 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13330 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13331 break;
13332
252b5132 13333 case M_TRUNCWS:
43841e91 13334 case M_TRUNCWD:
df58fc94 13335 gas_assert (!mips_opts.micromips);
0aa27725 13336 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13337 used_at = 1;
252b5132
RH
13338
13339 /*
13340 * Is the double cfc1 instruction a bug in the mips assembler;
13341 * or is there a reason for it?
13342 */
7d10b47d 13343 start_noreorder ();
c0ebe874
RS
13344 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13345 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13346 macro_build (NULL, "nop", "");
252b5132 13347 expr1.X_add_number = 3;
c0ebe874 13348 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13349 expr1.X_add_number = 2;
67c0d1eb
RS
13350 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13351 macro_build (NULL, "ctc1", "t,G", AT, RA);
13352 macro_build (NULL, "nop", "");
13353 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13354 op[0], op[1]);
13355 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13356 macro_build (NULL, "nop", "");
7d10b47d 13357 end_noreorder ();
252b5132
RH
13358 break;
13359
f2ae14a1 13360 case M_ULH_AB:
252b5132 13361 s = "lb";
df58fc94
RS
13362 s2 = "lbu";
13363 off = 1;
13364 goto uld_st;
f2ae14a1 13365 case M_ULHU_AB:
252b5132 13366 s = "lbu";
df58fc94
RS
13367 s2 = "lbu";
13368 off = 1;
13369 goto uld_st;
f2ae14a1 13370 case M_ULW_AB:
df58fc94
RS
13371 s = "lwl";
13372 s2 = "lwr";
7f3c4072 13373 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13374 off = 3;
13375 goto uld_st;
f2ae14a1 13376 case M_ULD_AB:
252b5132
RH
13377 s = "ldl";
13378 s2 = "ldr";
7f3c4072 13379 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13380 off = 7;
df58fc94 13381 goto uld_st;
f2ae14a1 13382 case M_USH_AB:
df58fc94
RS
13383 s = "sb";
13384 s2 = "sb";
13385 off = 1;
13386 ust = 1;
13387 goto uld_st;
f2ae14a1 13388 case M_USW_AB:
df58fc94
RS
13389 s = "swl";
13390 s2 = "swr";
7f3c4072 13391 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13392 off = 3;
df58fc94
RS
13393 ust = 1;
13394 goto uld_st;
f2ae14a1 13395 case M_USD_AB:
df58fc94
RS
13396 s = "sdl";
13397 s2 = "sdr";
7f3c4072 13398 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13399 off = 7;
13400 ust = 1;
13401
13402 uld_st:
c0ebe874 13403 breg = op[2];
f2ae14a1 13404 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13405 ep = &offset_expr;
13406 expr1.X_add_number = 0;
f2ae14a1 13407 if (large_offset)
df58fc94
RS
13408 {
13409 used_at = 1;
13410 tempreg = AT;
f2ae14a1
RS
13411 if (small_offset_p (0, align, 16))
13412 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13413 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13414 else
13415 {
13416 load_address (tempreg, ep, &used_at);
13417 if (breg != 0)
13418 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13419 tempreg, tempreg, breg);
13420 }
13421 offset_reloc[0] = BFD_RELOC_LO16;
13422 offset_reloc[1] = BFD_RELOC_UNUSED;
13423 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13424 breg = tempreg;
c0ebe874 13425 tempreg = op[0];
df58fc94
RS
13426 ep = &expr1;
13427 }
c0ebe874 13428 else if (!ust && op[0] == breg)
8fc2e39e
TS
13429 {
13430 used_at = 1;
13431 tempreg = AT;
13432 }
252b5132 13433 else
c0ebe874 13434 tempreg = op[0];
af22f5b2 13435
df58fc94
RS
13436 if (off == 1)
13437 goto ulh_sh;
252b5132 13438
90ecf173 13439 if (!target_big_endian)
df58fc94 13440 ep->X_add_number += off;
f2ae14a1 13441 if (offbits == 12)
c8276761 13442 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13443 else
13444 macro_build (ep, s, "t,o(b)", tempreg, -1,
13445 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13446
90ecf173 13447 if (!target_big_endian)
df58fc94 13448 ep->X_add_number -= off;
252b5132 13449 else
df58fc94 13450 ep->X_add_number += off;
f2ae14a1 13451 if (offbits == 12)
df58fc94 13452 macro_build (NULL, s2, "t,~(b)",
c8276761 13453 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13454 else
13455 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13456 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13457
df58fc94 13458 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13459 if (!ust && op[0] != tempreg)
df58fc94
RS
13460 {
13461 /* Protect second load's delay slot. */
13462 load_delay_nop ();
c0ebe874 13463 move_register (op[0], tempreg);
df58fc94 13464 }
8fc2e39e 13465 break;
252b5132 13466
df58fc94 13467 ulh_sh:
d6bc6245 13468 used_at = 1;
df58fc94
RS
13469 if (target_big_endian == ust)
13470 ep->X_add_number += off;
c0ebe874 13471 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13472 macro_build (ep, s, "t,o(b)", tempreg, -1,
13473 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13474
13475 /* For halfword transfers we need a temporary register to shuffle
13476 bytes. Unfortunately for M_USH_A we have none available before
13477 the next store as AT holds the base address. We deal with this
13478 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13479 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13480 if (ust)
c0ebe874 13481 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13482
13483 if (target_big_endian == ust)
13484 ep->X_add_number -= off;
252b5132 13485 else
df58fc94 13486 ep->X_add_number += off;
f2ae14a1
RS
13487 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13488 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13489
df58fc94 13490 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13491 if (ust && large_offset)
df58fc94
RS
13492 {
13493 if (target_big_endian)
13494 ep->X_add_number += off;
13495 else
13496 ep->X_add_number -= off;
f2ae14a1
RS
13497 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13498 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13499 }
13500 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13501 if (!ust || large_offset)
df58fc94 13502 {
c0ebe874 13503 tempreg = !large_offset ? AT : op[0];
df58fc94 13504 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13505 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13506 }
252b5132
RH
13507 break;
13508
13509 default:
13510 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13511 are added dynamically. */
1661c76c 13512 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13513 break;
13514 }
741fe287 13515 if (!mips_opts.at && used_at)
1661c76c 13516 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13517}
13518
13519/* Implement macros in mips16 mode. */
13520
13521static void
17a2f251 13522mips16_macro (struct mips_cl_insn *ip)
252b5132 13523{
c0ebe874 13524 const struct mips_operand_array *operands;
252b5132 13525 int mask;
c0ebe874 13526 int tmp;
252b5132
RH
13527 expressionS expr1;
13528 int dbl;
13529 const char *s, *s2, *s3;
c0ebe874
RS
13530 unsigned int op[MAX_OPERANDS];
13531 unsigned int i;
252b5132
RH
13532
13533 mask = ip->insn_mo->mask;
13534
c0ebe874
RS
13535 operands = insn_operands (ip);
13536 for (i = 0; i < MAX_OPERANDS; i++)
13537 if (operands->operand[i])
13538 op[i] = insn_extract_operand (ip, operands->operand[i]);
13539 else
13540 op[i] = -1;
252b5132 13541
252b5132
RH
13542 expr1.X_op = O_constant;
13543 expr1.X_op_symbol = NULL;
13544 expr1.X_add_symbol = NULL;
13545 expr1.X_add_number = 1;
13546
13547 dbl = 0;
13548
13549 switch (mask)
13550 {
13551 default:
b37df7c4 13552 abort ();
252b5132
RH
13553
13554 case M_DDIV_3:
13555 dbl = 1;
1a0670f3 13556 /* Fall through. */
252b5132
RH
13557 case M_DIV_3:
13558 s = "mflo";
13559 goto do_div3;
13560 case M_DREM_3:
13561 dbl = 1;
1a0670f3 13562 /* Fall through. */
252b5132
RH
13563 case M_REM_3:
13564 s = "mfhi";
13565 do_div3:
7d10b47d 13566 start_noreorder ();
d8722d76 13567 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13568 expr1.X_add_number = 2;
c0ebe874 13569 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13570 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13571
252b5132
RH
13572 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13573 since that causes an overflow. We should do that as well,
13574 but I don't see how to do the comparisons without a temporary
13575 register. */
7d10b47d 13576 end_noreorder ();
c0ebe874 13577 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13578 break;
13579
13580 case M_DIVU_3:
13581 s = "divu";
13582 s2 = "mflo";
13583 goto do_divu3;
13584 case M_REMU_3:
13585 s = "divu";
13586 s2 = "mfhi";
13587 goto do_divu3;
13588 case M_DDIVU_3:
13589 s = "ddivu";
13590 s2 = "mflo";
13591 goto do_divu3;
13592 case M_DREMU_3:
13593 s = "ddivu";
13594 s2 = "mfhi";
13595 do_divu3:
7d10b47d 13596 start_noreorder ();
d8722d76 13597 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13598 expr1.X_add_number = 2;
c0ebe874 13599 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13600 macro_build (NULL, "break", "6", 7);
7d10b47d 13601 end_noreorder ();
c0ebe874 13602 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13603 break;
13604
13605 case M_DMUL:
13606 dbl = 1;
1a0670f3 13607 /* Fall through. */
252b5132 13608 case M_MUL:
c0ebe874
RS
13609 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13610 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13611 break;
252b5132
RH
13612
13613 case M_DSUBU_I:
13614 dbl = 1;
13615 goto do_subu;
13616 case M_SUBU_I:
13617 do_subu:
252b5132 13618 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13619 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13620 break;
13621
13622 case M_SUBU_I_2:
252b5132 13623 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13624 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13625 break;
13626
13627 case M_DSUBU_I_2:
252b5132 13628 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13629 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13630 break;
13631
13632 case M_BEQ:
13633 s = "cmp";
13634 s2 = "bteqz";
13635 goto do_branch;
13636 case M_BNE:
13637 s = "cmp";
13638 s2 = "btnez";
13639 goto do_branch;
13640 case M_BLT:
13641 s = "slt";
13642 s2 = "btnez";
13643 goto do_branch;
13644 case M_BLTU:
13645 s = "sltu";
13646 s2 = "btnez";
13647 goto do_branch;
13648 case M_BLE:
13649 s = "slt";
13650 s2 = "bteqz";
13651 goto do_reverse_branch;
13652 case M_BLEU:
13653 s = "sltu";
13654 s2 = "bteqz";
13655 goto do_reverse_branch;
13656 case M_BGE:
13657 s = "slt";
13658 s2 = "bteqz";
13659 goto do_branch;
13660 case M_BGEU:
13661 s = "sltu";
13662 s2 = "bteqz";
13663 goto do_branch;
13664 case M_BGT:
13665 s = "slt";
13666 s2 = "btnez";
13667 goto do_reverse_branch;
13668 case M_BGTU:
13669 s = "sltu";
13670 s2 = "btnez";
13671
13672 do_reverse_branch:
c0ebe874
RS
13673 tmp = op[1];
13674 op[1] = op[0];
13675 op[0] = tmp;
252b5132
RH
13676
13677 do_branch:
c0ebe874 13678 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13679 macro_build (&offset_expr, s2, "p");
252b5132
RH
13680 break;
13681
13682 case M_BEQ_I:
13683 s = "cmpi";
13684 s2 = "bteqz";
13685 s3 = "x,U";
13686 goto do_branch_i;
13687 case M_BNE_I:
13688 s = "cmpi";
13689 s2 = "btnez";
13690 s3 = "x,U";
13691 goto do_branch_i;
13692 case M_BLT_I:
13693 s = "slti";
13694 s2 = "btnez";
13695 s3 = "x,8";
13696 goto do_branch_i;
13697 case M_BLTU_I:
13698 s = "sltiu";
13699 s2 = "btnez";
13700 s3 = "x,8";
13701 goto do_branch_i;
13702 case M_BLE_I:
13703 s = "slti";
13704 s2 = "btnez";
13705 s3 = "x,8";
13706 goto do_addone_branch_i;
13707 case M_BLEU_I:
13708 s = "sltiu";
13709 s2 = "btnez";
13710 s3 = "x,8";
13711 goto do_addone_branch_i;
13712 case M_BGE_I:
13713 s = "slti";
13714 s2 = "bteqz";
13715 s3 = "x,8";
13716 goto do_branch_i;
13717 case M_BGEU_I:
13718 s = "sltiu";
13719 s2 = "bteqz";
13720 s3 = "x,8";
13721 goto do_branch_i;
13722 case M_BGT_I:
13723 s = "slti";
13724 s2 = "bteqz";
13725 s3 = "x,8";
13726 goto do_addone_branch_i;
13727 case M_BGTU_I:
13728 s = "sltiu";
13729 s2 = "bteqz";
13730 s3 = "x,8";
13731
13732 do_addone_branch_i:
252b5132
RH
13733 ++imm_expr.X_add_number;
13734
13735 do_branch_i:
c0ebe874 13736 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13737 macro_build (&offset_expr, s2, "p");
252b5132
RH
13738 break;
13739
13740 case M_ABS:
13741 expr1.X_add_number = 0;
c0ebe874
RS
13742 macro_build (&expr1, "slti", "x,8", op[1]);
13743 if (op[0] != op[1])
13744 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13745 expr1.X_add_number = 2;
67c0d1eb 13746 macro_build (&expr1, "bteqz", "p");
c0ebe874 13747 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13748 break;
252b5132
RH
13749 }
13750}
13751
14daeee3
RS
13752/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13753 opcode bits in *OPCODE_EXTRA. */
13754
13755static struct mips_opcode *
13756mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13757 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13758{
13759 char *name, *dot, *p;
13760 unsigned int mask, suffix;
da8bca91 13761 ssize_t opend;
14daeee3
RS
13762 struct mips_opcode *insn;
13763
13764 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13765 name = xstrndup (start, length);
14daeee3
RS
13766
13767 /* Look up the instruction as-is. */
13768 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13769 if (insn)
e1fa0163 13770 goto end;
14daeee3
RS
13771
13772 dot = strchr (name, '.');
13773 if (dot && dot[1])
13774 {
13775 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13776 p = mips_parse_vu0_channels (dot + 1, &mask);
13777 if (*p == 0 && mask != 0)
13778 {
13779 *dot = 0;
13780 insn = (struct mips_opcode *) hash_find (hash, name);
13781 *dot = '.';
13782 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13783 {
13784 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13785 goto end;
14daeee3
RS
13786 }
13787 }
13788 }
13789
13790 if (mips_opts.micromips)
13791 {
13792 /* See if there's an instruction size override suffix,
13793 either `16' or `32', at the end of the mnemonic proper,
13794 that defines the operation, i.e. before the first `.'
13795 character if any. Strip it and retry. */
13796 opend = dot != NULL ? dot - name : length;
13797 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13798 suffix = 2;
13799 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13800 suffix = 4;
13801 else
13802 suffix = 0;
13803 if (suffix)
13804 {
13805 memcpy (name + opend - 2, name + opend, length - opend + 1);
13806 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13807 if (insn)
14daeee3
RS
13808 {
13809 forced_insn_length = suffix;
e1fa0163 13810 goto end;
14daeee3
RS
13811 }
13812 }
13813 }
13814
e1fa0163
NC
13815 insn = NULL;
13816 end:
13817 free (name);
13818 return insn;
14daeee3
RS
13819}
13820
77bd4346 13821/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13822 is a macro, set imm_expr and offset_expr to the values associated
13823 with "I" and "A" operands respectively. Otherwise store the value
13824 of the relocatable field (if any) in offset_expr. In both cases
13825 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13826
13827static void
60f20e8b 13828mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13829{
60f20e8b 13830 const struct mips_opcode *first, *past;
df58fc94 13831 struct hash_control *hash;
a92713e6 13832 char format;
14daeee3 13833 size_t end;
a92713e6 13834 struct mips_operand_token *tokens;
14daeee3 13835 unsigned int opcode_extra;
252b5132 13836
df58fc94
RS
13837 if (mips_opts.micromips)
13838 {
13839 hash = micromips_op_hash;
13840 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13841 }
13842 else
13843 {
13844 hash = op_hash;
13845 past = &mips_opcodes[NUMOPCODES];
13846 }
13847 forced_insn_length = 0;
14daeee3 13848 opcode_extra = 0;
252b5132 13849
df58fc94 13850 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
13851 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13852 continue;
bdaaa2e1 13853
60f20e8b
RS
13854 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13855 if (first == NULL)
252b5132 13856 {
1661c76c 13857 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 13858 return;
252b5132
RH
13859 }
13860
60f20e8b 13861 if (strcmp (first->name, "li.s") == 0)
a92713e6 13862 format = 'f';
60f20e8b 13863 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
13864 format = 'd';
13865 else
13866 format = 0;
13867 tokens = mips_parse_arguments (str + end, format);
13868 if (!tokens)
13869 return;
13870
60f20e8b
RS
13871 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13872 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 13873 set_insn_error (0, _("invalid operands"));
df58fc94 13874
e3de51ce 13875 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13876}
13877
77bd4346
RS
13878/* As for mips_ip, but used when assembling MIPS16 code.
13879 Also set forced_insn_length to the resulting instruction size in
13880 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13881
13882static void
60f20e8b 13883mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 13884{
1a00e612 13885 char *end, *s, c;
60f20e8b 13886 struct mips_opcode *first;
a92713e6 13887 struct mips_operand_token *tokens;
3fb49709 13888 unsigned int l;
252b5132 13889
3882b010 13890 for (s = str; ISLOWER (*s); ++s)
252b5132 13891 ;
1a00e612
RS
13892 end = s;
13893 c = *end;
3fb49709
MR
13894
13895 l = 0;
1a00e612 13896 switch (c)
252b5132
RH
13897 {
13898 case '\0':
13899 break;
13900
13901 case ' ':
1a00e612 13902 s++;
252b5132
RH
13903 break;
13904
13905 case '.':
3fb49709
MR
13906 s++;
13907 if (*s == 't')
252b5132 13908 {
3fb49709
MR
13909 l = 2;
13910 s++;
252b5132 13911 }
3fb49709 13912 else if (*s == 'e')
252b5132 13913 {
3fb49709
MR
13914 l = 4;
13915 s++;
252b5132 13916 }
3fb49709
MR
13917 if (*s == '\0')
13918 break;
13919 else if (*s++ == ' ')
13920 break;
252b5132
RH
13921 /* Fall through. */
13922 default:
1661c76c 13923 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13924 return;
13925 }
3fb49709 13926 forced_insn_length = l;
252b5132 13927
1a00e612 13928 *end = 0;
60f20e8b 13929 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13930 *end = c;
13931
60f20e8b 13932 if (!first)
252b5132 13933 {
1661c76c 13934 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13935 return;
13936 }
13937
a92713e6
RS
13938 tokens = mips_parse_arguments (s, 0);
13939 if (!tokens)
13940 return;
13941
60f20e8b 13942 if (!match_mips16_insns (insn, first, tokens))
1661c76c 13943 set_insn_error (0, _("invalid operands"));
252b5132 13944
e3de51ce 13945 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13946}
13947
b886a2ab
RS
13948/* Marshal immediate value VAL for an extended MIPS16 instruction.
13949 NBITS is the number of significant bits in VAL. */
13950
13951static unsigned long
13952mips16_immed_extend (offsetT val, unsigned int nbits)
13953{
13954 int extval;
13955 if (nbits == 16)
13956 {
13957 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13958 val &= 0x1f;
13959 }
13960 else if (nbits == 15)
13961 {
13962 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13963 val &= 0xf;
13964 }
13965 else
13966 {
13967 extval = ((val & 0x1f) << 6) | (val & 0x20);
13968 val = 0;
13969 }
13970 return (extval << 16) | val;
13971}
13972
3ccad066
RS
13973/* Like decode_mips16_operand, but require the operand to be defined and
13974 require it to be an integer. */
13975
13976static const struct mips_int_operand *
13977mips16_immed_operand (int type, bfd_boolean extended_p)
13978{
13979 const struct mips_operand *operand;
13980
13981 operand = decode_mips16_operand (type, extended_p);
13982 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13983 abort ();
13984 return (const struct mips_int_operand *) operand;
13985}
13986
13987/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13988
13989static bfd_boolean
13990mips16_immed_in_range_p (const struct mips_int_operand *operand,
13991 bfd_reloc_code_real_type reloc, offsetT sval)
13992{
13993 int min_val, max_val;
13994
13995 min_val = mips_int_operand_min (operand);
13996 max_val = mips_int_operand_max (operand);
13997 if (reloc != BFD_RELOC_UNUSED)
13998 {
13999 if (min_val < 0)
14000 sval = SEXT_16BIT (sval);
14001 else
14002 sval &= 0xffff;
14003 }
14004
14005 return (sval >= min_val
14006 && sval <= max_val
14007 && (sval & ((1 << operand->shift) - 1)) == 0);
14008}
14009
5c04167a
RS
14010/* Install immediate value VAL into MIPS16 instruction *INSN,
14011 extending it if necessary. The instruction in *INSN may
14012 already be extended.
14013
43c0598f
RS
14014 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14015 if none. In the former case, VAL is a 16-bit number with no
14016 defined signedness.
14017
14018 TYPE is the type of the immediate field. USER_INSN_LENGTH
14019 is the length that the user requested, or 0 if none. */
252b5132
RH
14020
14021static void
3b4dbbbf 14022mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14023 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14024 unsigned int user_insn_length, unsigned long *insn)
252b5132 14025{
3ccad066
RS
14026 const struct mips_int_operand *operand;
14027 unsigned int uval, length;
252b5132 14028
3ccad066
RS
14029 operand = mips16_immed_operand (type, FALSE);
14030 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14031 {
14032 /* We need an extended instruction. */
14033 if (user_insn_length == 2)
14034 as_bad_where (file, line, _("invalid unextended operand value"));
14035 else
14036 *insn |= MIPS16_EXTEND;
14037 }
14038 else if (user_insn_length == 4)
14039 {
14040 /* The operand doesn't force an unextended instruction to be extended.
14041 Warn if the user wanted an extended instruction anyway. */
14042 *insn |= MIPS16_EXTEND;
14043 as_warn_where (file, line,
14044 _("extended operand requested but not required"));
14045 }
252b5132 14046
3ccad066
RS
14047 length = mips16_opcode_length (*insn);
14048 if (length == 4)
252b5132 14049 {
3ccad066
RS
14050 operand = mips16_immed_operand (type, TRUE);
14051 if (!mips16_immed_in_range_p (operand, reloc, val))
14052 as_bad_where (file, line,
14053 _("operand value out of range for instruction"));
252b5132 14054 }
3ccad066 14055 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14056 if (length == 2 || operand->root.lsb != 0)
3ccad066 14057 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14058 else
3ccad066 14059 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14060}
14061\f
d6f16593 14062struct percent_op_match
ad8d3bb3 14063{
5e0116d5
RS
14064 const char *str;
14065 bfd_reloc_code_real_type reloc;
d6f16593
MR
14066};
14067
14068static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14069{
5e0116d5 14070 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14071 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14072 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14073 {"%call16", BFD_RELOC_MIPS_CALL16},
14074 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14075 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14076 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14077 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14078 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14079 {"%got", BFD_RELOC_MIPS_GOT16},
14080 {"%gp_rel", BFD_RELOC_GPREL16},
14081 {"%half", BFD_RELOC_16},
14082 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14083 {"%higher", BFD_RELOC_MIPS_HIGHER},
14084 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14085 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14086 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14087 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14088 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14089 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14090 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14091 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14092 {"%hi", BFD_RELOC_HI16_S},
14093 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14094 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14095};
14096
d6f16593
MR
14097static const struct percent_op_match mips16_percent_op[] =
14098{
14099 {"%lo", BFD_RELOC_MIPS16_LO16},
14100 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14101 {"%got", BFD_RELOC_MIPS16_GOT16},
14102 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14103 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14104 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14105 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14106 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14107 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14108 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14109 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14110 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14111};
14112
252b5132 14113
5e0116d5
RS
14114/* Return true if *STR points to a relocation operator. When returning true,
14115 move *STR over the operator and store its relocation code in *RELOC.
14116 Leave both *STR and *RELOC alone when returning false. */
14117
14118static bfd_boolean
17a2f251 14119parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14120{
d6f16593
MR
14121 const struct percent_op_match *percent_op;
14122 size_t limit, i;
14123
14124 if (mips_opts.mips16)
14125 {
14126 percent_op = mips16_percent_op;
14127 limit = ARRAY_SIZE (mips16_percent_op);
14128 }
14129 else
14130 {
14131 percent_op = mips_percent_op;
14132 limit = ARRAY_SIZE (mips_percent_op);
14133 }
76b3015f 14134
d6f16593 14135 for (i = 0; i < limit; i++)
5e0116d5 14136 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14137 {
3f98094e
DJ
14138 int len = strlen (percent_op[i].str);
14139
14140 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14141 continue;
14142
5e0116d5
RS
14143 *str += strlen (percent_op[i].str);
14144 *reloc = percent_op[i].reloc;
394f9b3a 14145
5e0116d5
RS
14146 /* Check whether the output BFD supports this relocation.
14147 If not, issue an error and fall back on something safe. */
14148 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14149 {
20203fb9 14150 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14151 percent_op[i].str);
01a3f561 14152 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14153 }
5e0116d5 14154 return TRUE;
394f9b3a 14155 }
5e0116d5 14156 return FALSE;
394f9b3a 14157}
ad8d3bb3 14158
ad8d3bb3 14159
5e0116d5
RS
14160/* Parse string STR as a 16-bit relocatable operand. Store the
14161 expression in *EP and the relocations in the array starting
14162 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14163
01a3f561 14164 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14165
5e0116d5 14166static size_t
17a2f251
TS
14167my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14168 char *str)
ad8d3bb3 14169{
5e0116d5
RS
14170 bfd_reloc_code_real_type reversed_reloc[3];
14171 size_t reloc_index, i;
09b8f35a
RS
14172 int crux_depth, str_depth;
14173 char *crux;
5e0116d5
RS
14174
14175 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14176 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14177 of the main expression and with CRUX_DEPTH containing the number
14178 of open brackets at that point. */
14179 reloc_index = -1;
14180 str_depth = 0;
14181 do
fb1b3232 14182 {
09b8f35a
RS
14183 reloc_index++;
14184 crux = str;
14185 crux_depth = str_depth;
14186
14187 /* Skip over whitespace and brackets, keeping count of the number
14188 of brackets. */
14189 while (*str == ' ' || *str == '\t' || *str == '(')
14190 if (*str++ == '(')
14191 str_depth++;
5e0116d5 14192 }
09b8f35a
RS
14193 while (*str == '%'
14194 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14195 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14196
09b8f35a 14197 my_getExpression (ep, crux);
5e0116d5 14198 str = expr_end;
394f9b3a 14199
5e0116d5 14200 /* Match every open bracket. */
09b8f35a 14201 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14202 if (*str++ == ')')
09b8f35a 14203 crux_depth--;
394f9b3a 14204
09b8f35a 14205 if (crux_depth > 0)
20203fb9 14206 as_bad (_("unclosed '('"));
394f9b3a 14207
5e0116d5 14208 expr_end = str;
252b5132 14209
01a3f561 14210 if (reloc_index != 0)
64bdfcaf
RS
14211 {
14212 prev_reloc_op_frag = frag_now;
14213 for (i = 0; i < reloc_index; i++)
14214 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14215 }
fb1b3232 14216
5e0116d5 14217 return reloc_index;
252b5132
RH
14218}
14219
14220static void
17a2f251 14221my_getExpression (expressionS *ep, char *str)
252b5132
RH
14222{
14223 char *save_in;
14224
14225 save_in = input_line_pointer;
14226 input_line_pointer = str;
14227 expression (ep);
14228 expr_end = input_line_pointer;
14229 input_line_pointer = save_in;
252b5132
RH
14230}
14231
6d4af3c2 14232const char *
17a2f251 14233md_atof (int type, char *litP, int *sizeP)
252b5132 14234{
499ac353 14235 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14236}
14237
14238void
17a2f251 14239md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14240{
14241 if (target_big_endian)
14242 number_to_chars_bigendian (buf, val, n);
14243 else
14244 number_to_chars_littleendian (buf, val, n);
14245}
14246\f
e013f690
TS
14247static int support_64bit_objects(void)
14248{
14249 const char **list, **l;
aa3d8fdf 14250 int yes;
e013f690
TS
14251
14252 list = bfd_target_list ();
14253 for (l = list; *l != NULL; l++)
aeffff67
RS
14254 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14255 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14256 break;
aa3d8fdf 14257 yes = (*l != NULL);
e013f690 14258 free (list);
aa3d8fdf 14259 return yes;
e013f690
TS
14260}
14261
316f5878
RS
14262/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14263 NEW_VALUE. Warn if another value was already specified. Note:
14264 we have to defer parsing the -march and -mtune arguments in order
14265 to handle 'from-abi' correctly, since the ABI might be specified
14266 in a later argument. */
14267
14268static void
17a2f251 14269mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14270{
14271 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14272 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14273 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14274 new_value);
14275
14276 *string_ptr = new_value;
14277}
14278
252b5132 14279int
17b9d67d 14280md_parse_option (int c, const char *arg)
252b5132 14281{
c6278170
RS
14282 unsigned int i;
14283
14284 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14285 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14286 {
919731af 14287 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14288 c == mips_ases[i].option_on);
14289 return 1;
14290 }
14291
252b5132
RH
14292 switch (c)
14293 {
119d663a
NC
14294 case OPTION_CONSTRUCT_FLOATS:
14295 mips_disable_float_construction = 0;
14296 break;
bdaaa2e1 14297
119d663a
NC
14298 case OPTION_NO_CONSTRUCT_FLOATS:
14299 mips_disable_float_construction = 1;
14300 break;
bdaaa2e1 14301
252b5132
RH
14302 case OPTION_TRAP:
14303 mips_trap = 1;
14304 break;
14305
14306 case OPTION_BREAK:
14307 mips_trap = 0;
14308 break;
14309
14310 case OPTION_EB:
14311 target_big_endian = 1;
14312 break;
14313
14314 case OPTION_EL:
14315 target_big_endian = 0;
14316 break;
14317
14318 case 'O':
4ffff32f
TS
14319 if (arg == NULL)
14320 mips_optimize = 1;
14321 else if (arg[0] == '0')
14322 mips_optimize = 0;
14323 else if (arg[0] == '1')
252b5132
RH
14324 mips_optimize = 1;
14325 else
14326 mips_optimize = 2;
14327 break;
14328
14329 case 'g':
14330 if (arg == NULL)
14331 mips_debug = 2;
14332 else
14333 mips_debug = atoi (arg);
252b5132
RH
14334 break;
14335
14336 case OPTION_MIPS1:
0b35dfee 14337 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14338 break;
14339
14340 case OPTION_MIPS2:
0b35dfee 14341 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14342 break;
14343
14344 case OPTION_MIPS3:
0b35dfee 14345 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14346 break;
14347
14348 case OPTION_MIPS4:
0b35dfee 14349 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14350 break;
14351
84ea6cf2 14352 case OPTION_MIPS5:
0b35dfee 14353 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14354 break;
14355
e7af610e 14356 case OPTION_MIPS32:
0b35dfee 14357 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14358 break;
14359
af7ee8bf 14360 case OPTION_MIPS32R2:
0b35dfee 14361 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14362 break;
14363
ae52f483 14364 case OPTION_MIPS32R3:
0ae19f05 14365 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14366 break;
14367
14368 case OPTION_MIPS32R5:
0ae19f05 14369 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14370 break;
14371
7361da2c
AB
14372 case OPTION_MIPS32R6:
14373 file_mips_opts.isa = ISA_MIPS32R6;
14374 break;
14375
5f74bc13 14376 case OPTION_MIPS64R2:
0b35dfee 14377 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14378 break;
14379
ae52f483 14380 case OPTION_MIPS64R3:
0ae19f05 14381 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14382 break;
14383
14384 case OPTION_MIPS64R5:
0ae19f05 14385 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14386 break;
14387
7361da2c
AB
14388 case OPTION_MIPS64R6:
14389 file_mips_opts.isa = ISA_MIPS64R6;
14390 break;
14391
84ea6cf2 14392 case OPTION_MIPS64:
0b35dfee 14393 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14394 break;
14395
ec68c924 14396 case OPTION_MTUNE:
316f5878
RS
14397 mips_set_option_string (&mips_tune_string, arg);
14398 break;
ec68c924 14399
316f5878
RS
14400 case OPTION_MARCH:
14401 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14402 break;
14403
14404 case OPTION_M4650:
316f5878
RS
14405 mips_set_option_string (&mips_arch_string, "4650");
14406 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14407 break;
14408
14409 case OPTION_NO_M4650:
14410 break;
14411
14412 case OPTION_M4010:
316f5878
RS
14413 mips_set_option_string (&mips_arch_string, "4010");
14414 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14415 break;
14416
14417 case OPTION_NO_M4010:
14418 break;
14419
14420 case OPTION_M4100:
316f5878
RS
14421 mips_set_option_string (&mips_arch_string, "4100");
14422 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14423 break;
14424
14425 case OPTION_NO_M4100:
14426 break;
14427
252b5132 14428 case OPTION_M3900:
316f5878
RS
14429 mips_set_option_string (&mips_arch_string, "3900");
14430 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14431 break;
bdaaa2e1 14432
252b5132
RH
14433 case OPTION_NO_M3900:
14434 break;
14435
df58fc94 14436 case OPTION_MICROMIPS:
919731af 14437 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14438 {
14439 as_bad (_("-mmicromips cannot be used with -mips16"));
14440 return 0;
14441 }
919731af 14442 file_mips_opts.micromips = 1;
df58fc94
RS
14443 mips_no_prev_insn ();
14444 break;
14445
14446 case OPTION_NO_MICROMIPS:
919731af 14447 file_mips_opts.micromips = 0;
df58fc94
RS
14448 mips_no_prev_insn ();
14449 break;
14450
252b5132 14451 case OPTION_MIPS16:
919731af 14452 if (file_mips_opts.micromips == 1)
df58fc94
RS
14453 {
14454 as_bad (_("-mips16 cannot be used with -micromips"));
14455 return 0;
14456 }
919731af 14457 file_mips_opts.mips16 = 1;
7d10b47d 14458 mips_no_prev_insn ();
252b5132
RH
14459 break;
14460
14461 case OPTION_NO_MIPS16:
919731af 14462 file_mips_opts.mips16 = 0;
7d10b47d 14463 mips_no_prev_insn ();
252b5132
RH
14464 break;
14465
6a32d874
CM
14466 case OPTION_FIX_24K:
14467 mips_fix_24k = 1;
14468 break;
14469
14470 case OPTION_NO_FIX_24K:
14471 mips_fix_24k = 0;
14472 break;
14473
a8d14a88
CM
14474 case OPTION_FIX_RM7000:
14475 mips_fix_rm7000 = 1;
14476 break;
14477
14478 case OPTION_NO_FIX_RM7000:
14479 mips_fix_rm7000 = 0;
14480 break;
14481
c67a084a
NC
14482 case OPTION_FIX_LOONGSON2F_JUMP:
14483 mips_fix_loongson2f_jump = TRUE;
14484 break;
14485
14486 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14487 mips_fix_loongson2f_jump = FALSE;
14488 break;
14489
14490 case OPTION_FIX_LOONGSON2F_NOP:
14491 mips_fix_loongson2f_nop = TRUE;
14492 break;
14493
14494 case OPTION_NO_FIX_LOONGSON2F_NOP:
14495 mips_fix_loongson2f_nop = FALSE;
14496 break;
14497
d766e8ec
RS
14498 case OPTION_FIX_VR4120:
14499 mips_fix_vr4120 = 1;
60b63b72
RS
14500 break;
14501
d766e8ec
RS
14502 case OPTION_NO_FIX_VR4120:
14503 mips_fix_vr4120 = 0;
60b63b72
RS
14504 break;
14505
7d8e00cf
RS
14506 case OPTION_FIX_VR4130:
14507 mips_fix_vr4130 = 1;
14508 break;
14509
14510 case OPTION_NO_FIX_VR4130:
14511 mips_fix_vr4130 = 0;
14512 break;
14513
d954098f
DD
14514 case OPTION_FIX_CN63XXP1:
14515 mips_fix_cn63xxp1 = TRUE;
14516 break;
14517
14518 case OPTION_NO_FIX_CN63XXP1:
14519 mips_fix_cn63xxp1 = FALSE;
14520 break;
14521
4a6a3df4
AO
14522 case OPTION_RELAX_BRANCH:
14523 mips_relax_branch = 1;
14524 break;
14525
14526 case OPTION_NO_RELAX_BRANCH:
14527 mips_relax_branch = 0;
14528 break;
14529
8b10b0b3
MR
14530 case OPTION_IGNORE_BRANCH_ISA:
14531 mips_ignore_branch_isa = TRUE;
14532 break;
14533
14534 case OPTION_NO_IGNORE_BRANCH_ISA:
14535 mips_ignore_branch_isa = FALSE;
14536 break;
14537
833794fc 14538 case OPTION_INSN32:
919731af 14539 file_mips_opts.insn32 = TRUE;
833794fc
MR
14540 break;
14541
14542 case OPTION_NO_INSN32:
919731af 14543 file_mips_opts.insn32 = FALSE;
833794fc
MR
14544 break;
14545
aa6975fb
ILT
14546 case OPTION_MSHARED:
14547 mips_in_shared = TRUE;
14548 break;
14549
14550 case OPTION_MNO_SHARED:
14551 mips_in_shared = FALSE;
14552 break;
14553
aed1a261 14554 case OPTION_MSYM32:
919731af 14555 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14556 break;
14557
14558 case OPTION_MNO_SYM32:
919731af 14559 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14560 break;
14561
252b5132
RH
14562 /* When generating ELF code, we permit -KPIC and -call_shared to
14563 select SVR4_PIC, and -non_shared to select no PIC. This is
14564 intended to be compatible with Irix 5. */
14565 case OPTION_CALL_SHARED:
252b5132 14566 mips_pic = SVR4_PIC;
143d77c5 14567 mips_abicalls = TRUE;
252b5132
RH
14568 break;
14569
861fb55a 14570 case OPTION_CALL_NONPIC:
861fb55a
DJ
14571 mips_pic = NO_PIC;
14572 mips_abicalls = TRUE;
14573 break;
14574
252b5132 14575 case OPTION_NON_SHARED:
252b5132 14576 mips_pic = NO_PIC;
143d77c5 14577 mips_abicalls = FALSE;
252b5132
RH
14578 break;
14579
44075ae2
TS
14580 /* The -xgot option tells the assembler to use 32 bit offsets
14581 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14582 compatibility. */
14583 case OPTION_XGOT:
14584 mips_big_got = 1;
14585 break;
14586
14587 case 'G':
6caf9ef4
TS
14588 g_switch_value = atoi (arg);
14589 g_switch_seen = 1;
252b5132
RH
14590 break;
14591
34ba82a8
TS
14592 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14593 and -mabi=64. */
252b5132 14594 case OPTION_32:
f3ded42a 14595 mips_abi = O32_ABI;
252b5132
RH
14596 break;
14597
e013f690 14598 case OPTION_N32:
316f5878 14599 mips_abi = N32_ABI;
e013f690 14600 break;
252b5132 14601
e013f690 14602 case OPTION_64:
316f5878 14603 mips_abi = N64_ABI;
f43abd2b 14604 if (!support_64bit_objects())
1661c76c 14605 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14606 break;
14607
c97ef257 14608 case OPTION_GP32:
bad1aba3 14609 file_mips_opts.gp = 32;
c97ef257
AH
14610 break;
14611
14612 case OPTION_GP64:
bad1aba3 14613 file_mips_opts.gp = 64;
c97ef257 14614 break;
252b5132 14615
ca4e0257 14616 case OPTION_FP32:
0b35dfee 14617 file_mips_opts.fp = 32;
316f5878
RS
14618 break;
14619
351cdf24
MF
14620 case OPTION_FPXX:
14621 file_mips_opts.fp = 0;
14622 break;
14623
316f5878 14624 case OPTION_FP64:
0b35dfee 14625 file_mips_opts.fp = 64;
ca4e0257
RS
14626 break;
14627
351cdf24
MF
14628 case OPTION_ODD_SPREG:
14629 file_mips_opts.oddspreg = 1;
14630 break;
14631
14632 case OPTION_NO_ODD_SPREG:
14633 file_mips_opts.oddspreg = 0;
14634 break;
14635
037b32b9 14636 case OPTION_SINGLE_FLOAT:
0b35dfee 14637 file_mips_opts.single_float = 1;
037b32b9
AN
14638 break;
14639
14640 case OPTION_DOUBLE_FLOAT:
0b35dfee 14641 file_mips_opts.single_float = 0;
037b32b9
AN
14642 break;
14643
14644 case OPTION_SOFT_FLOAT:
0b35dfee 14645 file_mips_opts.soft_float = 1;
037b32b9
AN
14646 break;
14647
14648 case OPTION_HARD_FLOAT:
0b35dfee 14649 file_mips_opts.soft_float = 0;
037b32b9
AN
14650 break;
14651
252b5132 14652 case OPTION_MABI:
e013f690 14653 if (strcmp (arg, "32") == 0)
316f5878 14654 mips_abi = O32_ABI;
e013f690 14655 else if (strcmp (arg, "o64") == 0)
316f5878 14656 mips_abi = O64_ABI;
e013f690 14657 else if (strcmp (arg, "n32") == 0)
316f5878 14658 mips_abi = N32_ABI;
e013f690
TS
14659 else if (strcmp (arg, "64") == 0)
14660 {
316f5878 14661 mips_abi = N64_ABI;
e013f690 14662 if (! support_64bit_objects())
1661c76c 14663 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14664 "format"));
14665 }
14666 else if (strcmp (arg, "eabi") == 0)
316f5878 14667 mips_abi = EABI_ABI;
e013f690 14668 else
da0e507f
TS
14669 {
14670 as_fatal (_("invalid abi -mabi=%s"), arg);
14671 return 0;
14672 }
252b5132
RH
14673 break;
14674
6b76fefe 14675 case OPTION_M7000_HILO_FIX:
b34976b6 14676 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14677 break;
14678
9ee72ff1 14679 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14680 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14681 break;
14682
ecb4347a 14683 case OPTION_MDEBUG:
b34976b6 14684 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14685 break;
14686
14687 case OPTION_NO_MDEBUG:
b34976b6 14688 mips_flag_mdebug = FALSE;
ecb4347a 14689 break;
dcd410fe
RO
14690
14691 case OPTION_PDR:
14692 mips_flag_pdr = TRUE;
14693 break;
14694
14695 case OPTION_NO_PDR:
14696 mips_flag_pdr = FALSE;
14697 break;
0a44bf69
RS
14698
14699 case OPTION_MVXWORKS_PIC:
14700 mips_pic = VXWORKS_PIC;
14701 break;
ecb4347a 14702
ba92f887
MR
14703 case OPTION_NAN:
14704 if (strcmp (arg, "2008") == 0)
7361da2c 14705 mips_nan2008 = 1;
ba92f887 14706 else if (strcmp (arg, "legacy") == 0)
7361da2c 14707 mips_nan2008 = 0;
ba92f887
MR
14708 else
14709 {
1661c76c 14710 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14711 return 0;
14712 }
14713 break;
14714
252b5132
RH
14715 default:
14716 return 0;
14717 }
14718
c67a084a
NC
14719 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14720
252b5132
RH
14721 return 1;
14722}
316f5878 14723\f
919731af 14724/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14725
316f5878 14726static void
17a2f251 14727mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14728{
14729 if (info != 0)
fef14a42 14730 mips_tune = info->cpu;
316f5878 14731}
80cc45a5 14732
34ba82a8 14733
252b5132 14734void
17a2f251 14735mips_after_parse_args (void)
e9670677 14736{
fef14a42
TS
14737 const struct mips_cpu_info *arch_info = 0;
14738 const struct mips_cpu_info *tune_info = 0;
14739
e9670677 14740 /* GP relative stuff not working for PE */
6caf9ef4 14741 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14742 {
6caf9ef4 14743 if (g_switch_seen && g_switch_value != 0)
1661c76c 14744 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14745 g_switch_value = 0;
14746 }
14747
cac012d6
AO
14748 if (mips_abi == NO_ABI)
14749 mips_abi = MIPS_DEFAULT_ABI;
14750
919731af 14751 /* The following code determines the architecture.
22923709
RS
14752 Similar code was added to GCC 3.3 (see override_options() in
14753 config/mips/mips.c). The GAS and GCC code should be kept in sync
14754 as much as possible. */
e9670677 14755
316f5878 14756 if (mips_arch_string != 0)
fef14a42 14757 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14758
0b35dfee 14759 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14760 {
0b35dfee 14761 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14762 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14763 the -march selection (if any). */
fef14a42 14764 if (arch_info != 0)
e9670677 14765 {
316f5878
RS
14766 /* -march takes precedence over -mipsN, since it is more descriptive.
14767 There's no harm in specifying both as long as the ISA levels
14768 are the same. */
0b35dfee 14769 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14770 as_bad (_("-%s conflicts with the other architecture options,"
14771 " which imply -%s"),
0b35dfee 14772 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14773 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14774 }
316f5878 14775 else
0b35dfee 14776 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14777 }
14778
fef14a42 14779 if (arch_info == 0)
95bfe26e
MF
14780 {
14781 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14782 gas_assert (arch_info);
14783 }
e9670677 14784
fef14a42 14785 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14786 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14787 arch_info->name);
14788
919731af 14789 file_mips_opts.arch = arch_info->cpu;
14790 file_mips_opts.isa = arch_info->isa;
14791
14792 /* Set up initial mips_opts state. */
14793 mips_opts = file_mips_opts;
14794
14795 /* The register size inference code is now placed in
14796 file_mips_check_options. */
fef14a42 14797
0b35dfee 14798 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14799 processor. */
fef14a42
TS
14800 if (mips_tune_string != 0)
14801 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14802
fef14a42
TS
14803 if (tune_info == 0)
14804 mips_set_tune (arch_info);
14805 else
14806 mips_set_tune (tune_info);
e9670677 14807
ecb4347a 14808 if (mips_flag_mdebug < 0)
e8044f35 14809 mips_flag_mdebug = 0;
e9670677
MR
14810}
14811\f
14812void
17a2f251 14813mips_init_after_args (void)
252b5132
RH
14814{
14815 /* initialize opcodes */
14816 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14817 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14818}
14819
14820long
17a2f251 14821md_pcrel_from (fixS *fixP)
252b5132 14822{
a7ebbfdf
TS
14823 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14824 switch (fixP->fx_r_type)
14825 {
df58fc94
RS
14826 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14827 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14828 /* Return the address of the delay slot. */
14829 return addr + 2;
14830
14831 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14832 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 14833 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 14834 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
14835 case BFD_RELOC_MIPS_21_PCREL_S2:
14836 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
14837 case BFD_RELOC_MIPS_JMP:
14838 /* Return the address of the delay slot. */
14839 return addr + 4;
df58fc94 14840
51f6035b
MR
14841 case BFD_RELOC_MIPS_18_PCREL_S3:
14842 /* Return the aligned address of the doubleword containing
14843 the instruction. */
14844 return addr & ~7;
14845
a7ebbfdf
TS
14846 default:
14847 return addr;
14848 }
252b5132
RH
14849}
14850
252b5132
RH
14851/* This is called before the symbol table is processed. In order to
14852 work with gcc when using mips-tfile, we must keep all local labels.
14853 However, in other cases, we want to discard them. If we were
14854 called with -g, but we didn't see any debugging information, it may
14855 mean that gcc is smuggling debugging information through to
14856 mips-tfile, in which case we must generate all local labels. */
14857
14858void
17a2f251 14859mips_frob_file_before_adjust (void)
252b5132
RH
14860{
14861#ifndef NO_ECOFF_DEBUGGING
14862 if (ECOFF_DEBUGGING
14863 && mips_debug != 0
14864 && ! ecoff_debugging_seen)
14865 flag_keep_locals = 1;
14866#endif
14867}
14868
3b91255e 14869/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14870 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14871 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14872 relocation operators.
14873
14874 For our purposes, a %lo() expression matches a %got() or %hi()
14875 expression if:
14876
14877 (a) it refers to the same symbol; and
14878 (b) the offset applied in the %lo() expression is no lower than
14879 the offset applied in the %got() or %hi().
14880
14881 (b) allows us to cope with code like:
14882
14883 lui $4,%hi(foo)
14884 lh $4,%lo(foo+2)($4)
14885
14886 ...which is legal on RELA targets, and has a well-defined behaviour
14887 if the user knows that adding 2 to "foo" will not induce a carry to
14888 the high 16 bits.
14889
14890 When several %lo()s match a particular %got() or %hi(), we use the
14891 following rules to distinguish them:
14892
14893 (1) %lo()s with smaller offsets are a better match than %lo()s with
14894 higher offsets.
14895
14896 (2) %lo()s with no matching %got() or %hi() are better than those
14897 that already have a matching %got() or %hi().
14898
14899 (3) later %lo()s are better than earlier %lo()s.
14900
14901 These rules are applied in order.
14902
14903 (1) means, among other things, that %lo()s with identical offsets are
14904 chosen if they exist.
14905
14906 (2) means that we won't associate several high-part relocations with
14907 the same low-part relocation unless there's no alternative. Having
14908 several high parts for the same low part is a GNU extension; this rule
14909 allows careful users to avoid it.
14910
14911 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14912 with the last high-part relocation being at the front of the list.
14913 It therefore makes sense to choose the last matching low-part
14914 relocation, all other things being equal. It's also easier
14915 to code that way. */
252b5132
RH
14916
14917void
17a2f251 14918mips_frob_file (void)
252b5132
RH
14919{
14920 struct mips_hi_fixup *l;
35903be0 14921 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14922
14923 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14924 {
14925 segment_info_type *seginfo;
3b91255e
RS
14926 bfd_boolean matched_lo_p;
14927 fixS **hi_pos, **lo_pos, **pos;
252b5132 14928
9c2799c2 14929 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14930
5919d012 14931 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14932 there isn't supposed to be a matching LO. Ignore %gots against
14933 constants; we'll report an error for those later. */
738e5348 14934 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 14935 && !(l->fixp->fx_addsy
9e009953 14936 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
14937 continue;
14938
14939 /* Check quickly whether the next fixup happens to be a matching %lo. */
14940 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14941 continue;
14942
252b5132 14943 seginfo = seg_info (l->seg);
252b5132 14944
3b91255e
RS
14945 /* Set HI_POS to the position of this relocation in the chain.
14946 Set LO_POS to the position of the chosen low-part relocation.
14947 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14948 relocation that matches an immediately-preceding high-part
14949 relocation. */
14950 hi_pos = NULL;
14951 lo_pos = NULL;
14952 matched_lo_p = FALSE;
738e5348 14953 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14954
3b91255e
RS
14955 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14956 {
14957 if (*pos == l->fixp)
14958 hi_pos = pos;
14959
35903be0 14960 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14961 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14962 && (*pos)->fx_offset >= l->fixp->fx_offset
14963 && (lo_pos == NULL
14964 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14965 || (!matched_lo_p
14966 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14967 lo_pos = pos;
14968
14969 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14970 && fixup_has_matching_lo_p (*pos));
14971 }
14972
14973 /* If we found a match, remove the high-part relocation from its
14974 current position and insert it before the low-part relocation.
14975 Make the offsets match so that fixup_has_matching_lo_p()
14976 will return true.
14977
14978 We don't warn about unmatched high-part relocations since some
14979 versions of gcc have been known to emit dead "lui ...%hi(...)"
14980 instructions. */
14981 if (lo_pos != NULL)
14982 {
14983 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14984 if (l->fixp->fx_next != *lo_pos)
252b5132 14985 {
3b91255e
RS
14986 *hi_pos = l->fixp->fx_next;
14987 l->fixp->fx_next = *lo_pos;
14988 *lo_pos = l->fixp;
252b5132 14989 }
252b5132
RH
14990 }
14991 }
14992}
14993
252b5132 14994int
17a2f251 14995mips_force_relocation (fixS *fixp)
252b5132 14996{
ae6063d4 14997 if (generic_force_reloc (fixp))
252b5132
RH
14998 return 1;
14999
df58fc94
RS
15000 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15001 so that the linker relaxation can update targets. */
15002 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15003 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15004 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15005 return 1;
15006
5caa2b07
MR
15007 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15008 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15009 microMIPS symbols so that we can do cross-mode branch diagnostics
15010 and BAL to JALX conversion by the linker. */
15011 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15012 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15013 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15014 && fixp->fx_addsy
15015 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15016 return 1;
15017
7361da2c 15018 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15019 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15020 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15021 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15022 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15023 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15024 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15025 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15026 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15027 return 1;
15028
3e722fb5 15029 return 0;
252b5132
RH
15030}
15031
b416ba9b
MR
15032/* Implement TC_FORCE_RELOCATION_ABS. */
15033
15034bfd_boolean
15035mips_force_relocation_abs (fixS *fixp)
15036{
15037 if (generic_force_reloc (fixp))
15038 return TRUE;
15039
15040 /* These relocations do not have enough bits in the in-place addend
15041 to hold an arbitrary absolute section's offset. */
15042 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15043 return TRUE;
15044
15045 return FALSE;
15046}
15047
b886a2ab
RS
15048/* Read the instruction associated with RELOC from BUF. */
15049
15050static unsigned int
15051read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15052{
15053 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15054 return read_compressed_insn (buf, 4);
15055 else
15056 return read_insn (buf);
15057}
15058
15059/* Write instruction INSN to BUF, given that it has been relocated
15060 by RELOC. */
15061
15062static void
15063write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15064 unsigned long insn)
15065{
15066 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15067 write_compressed_insn (buf, insn, 4);
15068 else
15069 write_insn (buf, insn);
15070}
15071
9d862524
MR
15072/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15073 to a symbol in another ISA mode, which cannot be converted to JALX. */
15074
15075static bfd_boolean
15076fix_bad_cross_mode_jump_p (fixS *fixP)
15077{
15078 unsigned long opcode;
15079 int other;
15080 char *buf;
15081
15082 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15083 return FALSE;
15084
15085 other = S_GET_OTHER (fixP->fx_addsy);
15086 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15087 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15088 switch (fixP->fx_r_type)
15089 {
15090 case BFD_RELOC_MIPS_JMP:
15091 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15092 case BFD_RELOC_MICROMIPS_JMP:
15093 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15094 default:
15095 return FALSE;
15096 }
15097}
15098
15099/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15100 jump to a symbol in the same ISA mode. */
15101
15102static bfd_boolean
15103fix_bad_same_mode_jalx_p (fixS *fixP)
15104{
15105 unsigned long opcode;
15106 int other;
15107 char *buf;
15108
15109 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15110 return FALSE;
15111
15112 other = S_GET_OTHER (fixP->fx_addsy);
15113 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15114 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15115 switch (fixP->fx_r_type)
15116 {
15117 case BFD_RELOC_MIPS_JMP:
15118 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15119 case BFD_RELOC_MIPS16_JMP:
15120 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15121 case BFD_RELOC_MICROMIPS_JMP:
15122 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15123 default:
15124 return FALSE;
15125 }
15126}
15127
15128/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15129 to a symbol whose value plus addend is not aligned according to the
15130 ultimate (after linker relaxation) jump instruction's immediate field
15131 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15132 regular MIPS code, to (1 << 2). */
15133
15134static bfd_boolean
15135fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15136{
15137 bfd_boolean micro_to_mips_p;
15138 valueT val;
15139 int other;
15140
15141 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15142 return FALSE;
15143
15144 other = S_GET_OTHER (fixP->fx_addsy);
15145 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15146 val += fixP->fx_offset;
15147 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15148 && !ELF_ST_IS_MICROMIPS (other));
15149 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15150 != ELF_ST_IS_COMPRESSED (other));
15151}
15152
15153/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15154 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15155 symbols check the ISA bit instead.
15156
15157 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15158 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15159 MIPS symbols and associated with BAL instructions as these instructions
15160 may be be converted to JALX by the linker. */
9d862524
MR
15161
15162static bfd_boolean
15163fix_bad_cross_mode_branch_p (fixS *fixP)
15164{
15165 bfd_boolean absolute_p;
15166 unsigned long opcode;
15167 asection *symsec;
15168 valueT val;
15169 int other;
15170 char *buf;
15171
8b10b0b3
MR
15172 if (mips_ignore_branch_isa)
15173 return FALSE;
15174
9d862524
MR
15175 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15176 return FALSE;
15177
15178 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15179 absolute_p = bfd_is_abs_section (symsec);
15180
15181 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15182 other = S_GET_OTHER (fixP->fx_addsy);
15183
15184 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15185 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15186 switch (fixP->fx_r_type)
15187 {
15188 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15189 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15190 && opcode != 0x0411);
15191 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15192 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15193 && opcode != 0x4060);
9d862524
MR
15194 case BFD_RELOC_MIPS_21_PCREL_S2:
15195 case BFD_RELOC_MIPS_26_PCREL_S2:
15196 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15197 case BFD_RELOC_MIPS16_16_PCREL_S1:
15198 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15199 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15200 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15201 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15202 default:
15203 abort ();
15204 }
15205}
15206
15207/* Return TRUE if the symbol plus addend associated with a regular MIPS
15208 branch instruction pointed to by FIXP is not aligned according to the
15209 branch instruction's immediate field requirement. We need the addend
15210 to preserve the ISA bit and also the sum must not have bit 2 set. We
15211 must explicitly OR in the ISA bit from symbol annotation as the bit
15212 won't be set in the symbol's value then. */
15213
15214static bfd_boolean
15215fix_bad_misaligned_branch_p (fixS *fixP)
15216{
15217 bfd_boolean absolute_p;
15218 asection *symsec;
15219 valueT isa_bit;
15220 valueT val;
15221 valueT off;
15222 int other;
15223
15224 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15225 return FALSE;
15226
15227 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15228 absolute_p = bfd_is_abs_section (symsec);
15229
15230 val = S_GET_VALUE (fixP->fx_addsy);
15231 other = S_GET_OTHER (fixP->fx_addsy);
15232 off = fixP->fx_offset;
15233
15234 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15235 val |= ELF_ST_IS_COMPRESSED (other);
15236 val += off;
15237 return (val & 0x3) != isa_bit;
15238}
15239
15240/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15241 and its calculated value VAL. */
15242
15243static void
15244fix_validate_branch (fixS *fixP, valueT val)
15245{
15246 if (fixP->fx_done && (val & 0x3) != 0)
15247 as_bad_where (fixP->fx_file, fixP->fx_line,
15248 _("branch to misaligned address (0x%lx)"),
15249 (long) (val + md_pcrel_from (fixP)));
15250 else if (fix_bad_cross_mode_branch_p (fixP))
15251 as_bad_where (fixP->fx_file, fixP->fx_line,
15252 _("branch to a symbol in another ISA mode"));
15253 else if (fix_bad_misaligned_branch_p (fixP))
15254 as_bad_where (fixP->fx_file, fixP->fx_line,
15255 _("branch to misaligned address (0x%lx)"),
15256 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15257 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15258 as_bad_where (fixP->fx_file, fixP->fx_line,
15259 _("cannot encode misaligned addend "
15260 "in the relocatable field (0x%lx)"),
15261 (long) fixP->fx_offset);
15262}
15263
252b5132
RH
15264/* Apply a fixup to the object file. */
15265
94f592af 15266void
55cf6793 15267md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15268{
4d68580a 15269 char *buf;
b886a2ab 15270 unsigned long insn;
a7ebbfdf 15271 reloc_howto_type *howto;
252b5132 15272
d56a8dda
RS
15273 if (fixP->fx_pcrel)
15274 switch (fixP->fx_r_type)
15275 {
15276 case BFD_RELOC_16_PCREL_S2:
c9775dde 15277 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15278 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15279 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15280 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15281 case BFD_RELOC_32_PCREL:
7361da2c
AB
15282 case BFD_RELOC_MIPS_21_PCREL_S2:
15283 case BFD_RELOC_MIPS_26_PCREL_S2:
15284 case BFD_RELOC_MIPS_18_PCREL_S3:
15285 case BFD_RELOC_MIPS_19_PCREL_S2:
15286 case BFD_RELOC_HI16_S_PCREL:
15287 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15288 break;
15289
15290 case BFD_RELOC_32:
15291 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15292 break;
15293
15294 default:
15295 as_bad_where (fixP->fx_file, fixP->fx_line,
15296 _("PC-relative reference to a different section"));
15297 break;
15298 }
15299
15300 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15301 that have no MIPS ELF equivalent. */
15302 if (fixP->fx_r_type != BFD_RELOC_8)
15303 {
15304 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15305 if (!howto)
15306 return;
15307 }
65551fa4 15308
df58fc94
RS
15309 gas_assert (fixP->fx_size == 2
15310 || fixP->fx_size == 4
d56a8dda 15311 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15312 || fixP->fx_r_type == BFD_RELOC_16
15313 || fixP->fx_r_type == BFD_RELOC_64
15314 || fixP->fx_r_type == BFD_RELOC_CTOR
15315 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15316 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15317 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15318 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15319 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15320 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15321
4d68580a 15322 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15323
b1dca8ee
RS
15324 /* Don't treat parts of a composite relocation as done. There are two
15325 reasons for this:
15326
15327 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15328 should nevertheless be emitted if the first part is.
15329
15330 (2) In normal usage, composite relocations are never assembly-time
15331 constants. The easiest way of dealing with the pathological
15332 exceptions is to generate a relocation against STN_UNDEF and
15333 leave everything up to the linker. */
3994f87e 15334 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15335 fixP->fx_done = 1;
15336
15337 switch (fixP->fx_r_type)
15338 {
3f98094e
DJ
15339 case BFD_RELOC_MIPS_TLS_GD:
15340 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15341 case BFD_RELOC_MIPS_TLS_DTPREL32:
15342 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15343 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15344 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15345 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15346 case BFD_RELOC_MIPS_TLS_TPREL32:
15347 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15348 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15349 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15350 case BFD_RELOC_MICROMIPS_TLS_GD:
15351 case BFD_RELOC_MICROMIPS_TLS_LDM:
15352 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15353 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15354 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15355 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15356 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15357 case BFD_RELOC_MIPS16_TLS_GD:
15358 case BFD_RELOC_MIPS16_TLS_LDM:
15359 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15360 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15361 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15362 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15363 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15364 if (fixP->fx_addsy)
15365 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15366 else
15367 as_bad_where (fixP->fx_file, fixP->fx_line,
15368 _("TLS relocation against a constant"));
15369 break;
3f98094e 15370
252b5132 15371 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15372 case BFD_RELOC_MIPS16_JMP:
15373 case BFD_RELOC_MICROMIPS_JMP:
15374 {
15375 int shift;
15376
15377 gas_assert (!fixP->fx_done);
15378
15379 /* Shift is 2, unusually, for microMIPS JALX. */
15380 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15381 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15382 shift = 1;
15383 else
15384 shift = 2;
15385
15386 if (fix_bad_cross_mode_jump_p (fixP))
15387 as_bad_where (fixP->fx_file, fixP->fx_line,
15388 _("jump to a symbol in another ISA mode"));
15389 else if (fix_bad_same_mode_jalx_p (fixP))
15390 as_bad_where (fixP->fx_file, fixP->fx_line,
15391 _("JALX to a symbol in the same ISA mode"));
15392 else if (fix_bad_misaligned_jump_p (fixP, shift))
15393 as_bad_where (fixP->fx_file, fixP->fx_line,
15394 _("jump to misaligned address (0x%lx)"),
15395 (long) (S_GET_VALUE (fixP->fx_addsy)
15396 + fixP->fx_offset));
15397 else if (HAVE_IN_PLACE_ADDENDS
15398 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15399 as_bad_where (fixP->fx_file, fixP->fx_line,
15400 _("cannot encode misaligned addend "
15401 "in the relocatable field (0x%lx)"),
15402 (long) fixP->fx_offset);
15403 }
15404 /* Fall through. */
15405
e369bcce
TS
15406 case BFD_RELOC_MIPS_SHIFT5:
15407 case BFD_RELOC_MIPS_SHIFT6:
15408 case BFD_RELOC_MIPS_GOT_DISP:
15409 case BFD_RELOC_MIPS_GOT_PAGE:
15410 case BFD_RELOC_MIPS_GOT_OFST:
15411 case BFD_RELOC_MIPS_SUB:
15412 case BFD_RELOC_MIPS_INSERT_A:
15413 case BFD_RELOC_MIPS_INSERT_B:
15414 case BFD_RELOC_MIPS_DELETE:
15415 case BFD_RELOC_MIPS_HIGHEST:
15416 case BFD_RELOC_MIPS_HIGHER:
15417 case BFD_RELOC_MIPS_SCN_DISP:
15418 case BFD_RELOC_MIPS_REL16:
15419 case BFD_RELOC_MIPS_RELGOT:
15420 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15421 case BFD_RELOC_HI16:
15422 case BFD_RELOC_HI16_S:
b886a2ab 15423 case BFD_RELOC_LO16:
cdf6fd85 15424 case BFD_RELOC_GPREL16:
252b5132
RH
15425 case BFD_RELOC_MIPS_LITERAL:
15426 case BFD_RELOC_MIPS_CALL16:
15427 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15428 case BFD_RELOC_GPREL32:
252b5132
RH
15429 case BFD_RELOC_MIPS_GOT_HI16:
15430 case BFD_RELOC_MIPS_GOT_LO16:
15431 case BFD_RELOC_MIPS_CALL_HI16:
15432 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15433 case BFD_RELOC_HI16_S_PCREL:
15434 case BFD_RELOC_LO16_PCREL:
252b5132 15435 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15436 case BFD_RELOC_MIPS16_GOT16:
15437 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15438 case BFD_RELOC_MIPS16_HI16:
15439 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15440 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15441 case BFD_RELOC_MICROMIPS_GOT_DISP:
15442 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15443 case BFD_RELOC_MICROMIPS_GOT_OFST:
15444 case BFD_RELOC_MICROMIPS_SUB:
15445 case BFD_RELOC_MICROMIPS_HIGHEST:
15446 case BFD_RELOC_MICROMIPS_HIGHER:
15447 case BFD_RELOC_MICROMIPS_SCN_DISP:
15448 case BFD_RELOC_MICROMIPS_JALR:
15449 case BFD_RELOC_MICROMIPS_HI16:
15450 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15451 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15452 case BFD_RELOC_MICROMIPS_GPREL16:
15453 case BFD_RELOC_MICROMIPS_LITERAL:
15454 case BFD_RELOC_MICROMIPS_CALL16:
15455 case BFD_RELOC_MICROMIPS_GOT16:
15456 case BFD_RELOC_MICROMIPS_GOT_HI16:
15457 case BFD_RELOC_MICROMIPS_GOT_LO16:
15458 case BFD_RELOC_MICROMIPS_CALL_HI16:
15459 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15460 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15461 if (fixP->fx_done)
15462 {
15463 offsetT value;
15464
15465 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15466 {
15467 insn = read_reloc_insn (buf, fixP->fx_r_type);
15468 if (mips16_reloc_p (fixP->fx_r_type))
15469 insn |= mips16_immed_extend (value, 16);
15470 else
15471 insn |= (value & 0xffff);
15472 write_reloc_insn (buf, fixP->fx_r_type, insn);
15473 }
15474 else
15475 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15476 _("unsupported constant in relocation"));
b886a2ab 15477 }
252b5132
RH
15478 break;
15479
252b5132
RH
15480 case BFD_RELOC_64:
15481 /* This is handled like BFD_RELOC_32, but we output a sign
15482 extended value if we are only 32 bits. */
3e722fb5 15483 if (fixP->fx_done)
252b5132
RH
15484 {
15485 if (8 <= sizeof (valueT))
4d68580a 15486 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15487 else
15488 {
a7ebbfdf 15489 valueT hiv;
252b5132 15490
a7ebbfdf 15491 if ((*valP & 0x80000000) != 0)
252b5132
RH
15492 hiv = 0xffffffff;
15493 else
15494 hiv = 0;
4d68580a
RS
15495 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15496 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15497 }
15498 }
15499 break;
15500
056350c6 15501 case BFD_RELOC_RVA:
252b5132 15502 case BFD_RELOC_32:
b47468a6 15503 case BFD_RELOC_32_PCREL:
252b5132 15504 case BFD_RELOC_16:
d56a8dda 15505 case BFD_RELOC_8:
252b5132 15506 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15507 value now. This can happen if we have a .word which is not
15508 resolved when it appears but is later defined. */
252b5132 15509 if (fixP->fx_done)
4d68580a 15510 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15511 break;
15512
7361da2c 15513 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15514 fix_validate_branch (fixP, *valP);
41947d9e
MR
15515 if (!fixP->fx_done)
15516 break;
15517
15518 if (*valP + 0x400000 <= 0x7fffff)
15519 {
15520 insn = read_insn (buf);
15521 insn |= (*valP >> 2) & 0x1fffff;
15522 write_insn (buf, insn);
15523 }
15524 else
15525 as_bad_where (fixP->fx_file, fixP->fx_line,
15526 _("branch out of range"));
15527 break;
15528
7361da2c 15529 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15530 fix_validate_branch (fixP, *valP);
41947d9e
MR
15531 if (!fixP->fx_done)
15532 break;
7361da2c 15533
41947d9e
MR
15534 if (*valP + 0x8000000 <= 0xfffffff)
15535 {
15536 insn = read_insn (buf);
15537 insn |= (*valP >> 2) & 0x3ffffff;
15538 write_insn (buf, insn);
15539 }
15540 else
15541 as_bad_where (fixP->fx_file, fixP->fx_line,
15542 _("branch out of range"));
7361da2c
AB
15543 break;
15544
15545 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15546 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15547 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15548 _("PC-relative access using misaligned symbol (%lx)"),
15549 (long) S_GET_VALUE (fixP->fx_addsy));
15550 if ((fixP->fx_offset & 0x7) != 0)
15551 as_bad_where (fixP->fx_file, fixP->fx_line,
15552 _("PC-relative access using misaligned offset (%lx)"),
15553 (long) fixP->fx_offset);
41947d9e
MR
15554 if (!fixP->fx_done)
15555 break;
7361da2c 15556
41947d9e
MR
15557 if (*valP + 0x100000 <= 0x1fffff)
15558 {
15559 insn = read_insn (buf);
15560 insn |= (*valP >> 3) & 0x3ffff;
15561 write_insn (buf, insn);
15562 }
15563 else
15564 as_bad_where (fixP->fx_file, fixP->fx_line,
15565 _("PC-relative access out of range"));
7361da2c
AB
15566 break;
15567
15568 case BFD_RELOC_MIPS_19_PCREL_S2:
15569 if ((*valP & 0x3) != 0)
15570 as_bad_where (fixP->fx_file, fixP->fx_line,
15571 _("PC-relative access to misaligned address (%lx)"),
717ba204 15572 (long) *valP);
41947d9e
MR
15573 if (!fixP->fx_done)
15574 break;
7361da2c 15575
41947d9e
MR
15576 if (*valP + 0x100000 <= 0x1fffff)
15577 {
15578 insn = read_insn (buf);
15579 insn |= (*valP >> 2) & 0x7ffff;
15580 write_insn (buf, insn);
15581 }
15582 else
15583 as_bad_where (fixP->fx_file, fixP->fx_line,
15584 _("PC-relative access out of range"));
7361da2c
AB
15585 break;
15586
252b5132 15587 case BFD_RELOC_16_PCREL_S2:
9d862524 15588 fix_validate_branch (fixP, *valP);
cb56d3d3 15589
54f4ddb3
TS
15590 /* We need to save the bits in the instruction since fixup_segment()
15591 might be deleting the relocation entry (i.e., a branch within
15592 the current segment). */
a7ebbfdf 15593 if (! fixP->fx_done)
bb2d6cd7 15594 break;
252b5132 15595
54f4ddb3 15596 /* Update old instruction data. */
4d68580a 15597 insn = read_insn (buf);
252b5132 15598
a7ebbfdf
TS
15599 if (*valP + 0x20000 <= 0x3ffff)
15600 {
15601 insn |= (*valP >> 2) & 0xffff;
4d68580a 15602 write_insn (buf, insn);
a7ebbfdf 15603 }
ce8ad872 15604 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15605 && fixP->fx_done
15606 && fixP->fx_frag->fr_address >= text_section->vma
15607 && (fixP->fx_frag->fr_address
587aac4e 15608 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15609 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15610 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15611 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15612 {
15613 /* The branch offset is too large. If this is an
15614 unconditional branch, and we are not generating PIC code,
15615 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15616 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15617 insn = 0x0c000000; /* jal */
252b5132 15618 else
a7ebbfdf
TS
15619 insn = 0x08000000; /* j */
15620 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15621 fixP->fx_done = 0;
15622 fixP->fx_addsy = section_symbol (text_section);
15623 *valP += md_pcrel_from (fixP);
4d68580a 15624 write_insn (buf, insn);
a7ebbfdf
TS
15625 }
15626 else
15627 {
15628 /* If we got here, we have branch-relaxation disabled,
15629 and there's nothing we can do to fix this instruction
15630 without turning it into a longer sequence. */
15631 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15632 _("branch out of range"));
252b5132 15633 }
252b5132
RH
15634 break;
15635
c9775dde 15636 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15637 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15638 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15639 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15640 gas_assert (!fixP->fx_done);
9d862524
MR
15641 if (fix_bad_cross_mode_branch_p (fixP))
15642 as_bad_where (fixP->fx_file, fixP->fx_line,
15643 _("branch to a symbol in another ISA mode"));
15644 else if (fixP->fx_addsy
15645 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15646 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15647 && (fixP->fx_offset & 0x1) != 0)
15648 as_bad_where (fixP->fx_file, fixP->fx_line,
15649 _("branch to misaligned address (0x%lx)"),
15650 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15651 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15652 as_bad_where (fixP->fx_file, fixP->fx_line,
15653 _("cannot encode misaligned addend "
15654 "in the relocatable field (0x%lx)"),
15655 (long) fixP->fx_offset);
df58fc94
RS
15656 break;
15657
252b5132
RH
15658 case BFD_RELOC_VTABLE_INHERIT:
15659 fixP->fx_done = 0;
15660 if (fixP->fx_addsy
15661 && !S_IS_DEFINED (fixP->fx_addsy)
15662 && !S_IS_WEAK (fixP->fx_addsy))
15663 S_SET_WEAK (fixP->fx_addsy);
15664 break;
15665
2f0c68f2 15666 case BFD_RELOC_NONE:
252b5132
RH
15667 case BFD_RELOC_VTABLE_ENTRY:
15668 fixP->fx_done = 0;
15669 break;
15670
15671 default:
b37df7c4 15672 abort ();
252b5132 15673 }
a7ebbfdf
TS
15674
15675 /* Remember value for tc_gen_reloc. */
15676 fixP->fx_addnumber = *valP;
252b5132
RH
15677}
15678
252b5132 15679static symbolS *
17a2f251 15680get_symbol (void)
252b5132
RH
15681{
15682 int c;
15683 char *name;
15684 symbolS *p;
15685
d02603dc 15686 c = get_symbol_name (&name);
252b5132 15687 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15688 (void) restore_line_pointer (c);
252b5132
RH
15689 return p;
15690}
15691
742a56fe
RS
15692/* Align the current frag to a given power of two. If a particular
15693 fill byte should be used, FILL points to an integer that contains
15694 that byte, otherwise FILL is null.
15695
462427c4
RS
15696 This function used to have the comment:
15697
15698 The MIPS assembler also automatically adjusts any preceding label.
15699
15700 The implementation therefore applied the adjustment to a maximum of
15701 one label. However, other label adjustments are applied to batches
15702 of labels, and adjusting just one caused problems when new labels
15703 were added for the sake of debugging or unwind information.
15704 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15705
15706static void
462427c4 15707mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15708{
7d10b47d 15709 mips_emit_delays ();
df58fc94 15710 mips_record_compressed_mode ();
742a56fe
RS
15711 if (fill == NULL && subseg_text_p (now_seg))
15712 frag_align_code (to, 0);
15713 else
15714 frag_align (to, fill ? *fill : 0, 0);
252b5132 15715 record_alignment (now_seg, to);
462427c4 15716 mips_move_labels (labels, FALSE);
252b5132
RH
15717}
15718
15719/* Align to a given power of two. .align 0 turns off the automatic
15720 alignment used by the data creating pseudo-ops. */
15721
15722static void
17a2f251 15723s_align (int x ATTRIBUTE_UNUSED)
252b5132 15724{
742a56fe 15725 int temp, fill_value, *fill_ptr;
49954fb4 15726 long max_alignment = 28;
252b5132 15727
54f4ddb3 15728 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15729 to the aligned address.
54f4ddb3 15730 o It's not documented but auto alignment is reinstated by
252b5132 15731 a .align pseudo instruction.
54f4ddb3 15732 o Note also that after auto alignment is turned off the mips assembler
252b5132 15733 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15734 We don't. */
252b5132
RH
15735
15736 temp = get_absolute_expression ();
15737 if (temp > max_alignment)
1661c76c 15738 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15739 else if (temp < 0)
15740 {
1661c76c 15741 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15742 temp = 0;
15743 }
15744 if (*input_line_pointer == ',')
15745 {
f9419b05 15746 ++input_line_pointer;
742a56fe
RS
15747 fill_value = get_absolute_expression ();
15748 fill_ptr = &fill_value;
252b5132
RH
15749 }
15750 else
742a56fe 15751 fill_ptr = 0;
252b5132
RH
15752 if (temp)
15753 {
a8dbcb85
TS
15754 segment_info_type *si = seg_info (now_seg);
15755 struct insn_label_list *l = si->label_list;
54f4ddb3 15756 /* Auto alignment should be switched on by next section change. */
252b5132 15757 auto_align = 1;
462427c4 15758 mips_align (temp, fill_ptr, l);
252b5132
RH
15759 }
15760 else
15761 {
15762 auto_align = 0;
15763 }
15764
15765 demand_empty_rest_of_line ();
15766}
15767
252b5132 15768static void
17a2f251 15769s_change_sec (int sec)
252b5132
RH
15770{
15771 segT seg;
15772
252b5132
RH
15773 /* The ELF backend needs to know that we are changing sections, so
15774 that .previous works correctly. We could do something like check
b6ff326e 15775 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15776 as it would not be appropriate to use it in the section changing
15777 functions in read.c, since obj-elf.c intercepts those. FIXME:
15778 This should be cleaner, somehow. */
f3ded42a 15779 obj_elf_section_change_hook ();
252b5132 15780
7d10b47d 15781 mips_emit_delays ();
6a32d874 15782
252b5132
RH
15783 switch (sec)
15784 {
15785 case 't':
15786 s_text (0);
15787 break;
15788 case 'd':
15789 s_data (0);
15790 break;
15791 case 'b':
15792 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15793 demand_empty_rest_of_line ();
15794 break;
15795
15796 case 'r':
4d0d148d
TS
15797 seg = subseg_new (RDATA_SECTION_NAME,
15798 (subsegT) get_absolute_expression ());
f3ded42a
RS
15799 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15800 | SEC_READONLY | SEC_RELOC
15801 | SEC_DATA));
15802 if (strncmp (TARGET_OS, "elf", 3) != 0)
15803 record_alignment (seg, 4);
4d0d148d 15804 demand_empty_rest_of_line ();
252b5132
RH
15805 break;
15806
15807 case 's':
4d0d148d 15808 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
15809 bfd_set_section_flags (stdoutput, seg,
15810 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15811 if (strncmp (TARGET_OS, "elf", 3) != 0)
15812 record_alignment (seg, 4);
4d0d148d
TS
15813 demand_empty_rest_of_line ();
15814 break;
998b3c36
MR
15815
15816 case 'B':
15817 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
15818 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15819 if (strncmp (TARGET_OS, "elf", 3) != 0)
15820 record_alignment (seg, 4);
998b3c36
MR
15821 demand_empty_rest_of_line ();
15822 break;
252b5132
RH
15823 }
15824
15825 auto_align = 1;
15826}
b34976b6 15827
cca86cc8 15828void
17a2f251 15829s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15830{
d02603dc 15831 char *saved_ilp;
cca86cc8 15832 char *section_name;
d02603dc 15833 char c, endc;
684022ea 15834 char next_c = 0;
cca86cc8
SC
15835 int section_type;
15836 int section_flag;
15837 int section_entry_size;
15838 int section_alignment;
b34976b6 15839
d02603dc
NC
15840 saved_ilp = input_line_pointer;
15841 endc = get_symbol_name (&section_name);
15842 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 15843 if (c)
d02603dc 15844 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 15845
4cf0dd0d
TS
15846 /* Do we have .section Name<,"flags">? */
15847 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 15848 {
d02603dc
NC
15849 /* Just after name is now '\0'. */
15850 (void) restore_line_pointer (endc);
15851 input_line_pointer = saved_ilp;
cca86cc8
SC
15852 obj_elf_section (ignore);
15853 return;
15854 }
d02603dc
NC
15855
15856 section_name = xstrdup (section_name);
15857 c = restore_line_pointer (endc);
15858
cca86cc8
SC
15859 input_line_pointer++;
15860
15861 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15862 if (c == ',')
15863 section_type = get_absolute_expression ();
15864 else
15865 section_type = 0;
d02603dc 15866
cca86cc8
SC
15867 if (*input_line_pointer++ == ',')
15868 section_flag = get_absolute_expression ();
15869 else
15870 section_flag = 0;
d02603dc 15871
cca86cc8
SC
15872 if (*input_line_pointer++ == ',')
15873 section_entry_size = get_absolute_expression ();
15874 else
15875 section_entry_size = 0;
d02603dc 15876
cca86cc8
SC
15877 if (*input_line_pointer++ == ',')
15878 section_alignment = get_absolute_expression ();
15879 else
15880 section_alignment = 0;
d02603dc 15881
87975d2a
AM
15882 /* FIXME: really ignore? */
15883 (void) section_alignment;
cca86cc8 15884
8ab8a5c8
RS
15885 /* When using the generic form of .section (as implemented by obj-elf.c),
15886 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15887 traditionally had to fall back on the more common @progbits instead.
15888
15889 There's nothing really harmful in this, since bfd will correct
15890 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 15891 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
15892 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15893
15894 Even so, we shouldn't force users of the MIPS .section syntax to
15895 incorrectly label the sections as SHT_PROGBITS. The best compromise
15896 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15897 generic type-checking code. */
15898 if (section_type == SHT_MIPS_DWARF)
15899 section_type = SHT_PROGBITS;
15900
a91e1603 15901 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 15902 section_entry_size, 0, 0, 0);
a816d1ed
AO
15903
15904 if (now_seg->name != section_name)
15905 free (section_name);
cca86cc8 15906}
252b5132
RH
15907
15908void
17a2f251 15909mips_enable_auto_align (void)
252b5132
RH
15910{
15911 auto_align = 1;
15912}
15913
15914static void
17a2f251 15915s_cons (int log_size)
252b5132 15916{
a8dbcb85
TS
15917 segment_info_type *si = seg_info (now_seg);
15918 struct insn_label_list *l = si->label_list;
252b5132 15919
7d10b47d 15920 mips_emit_delays ();
252b5132 15921 if (log_size > 0 && auto_align)
462427c4 15922 mips_align (log_size, 0, l);
252b5132 15923 cons (1 << log_size);
a1facbec 15924 mips_clear_insn_labels ();
252b5132
RH
15925}
15926
15927static void
17a2f251 15928s_float_cons (int type)
252b5132 15929{
a8dbcb85
TS
15930 segment_info_type *si = seg_info (now_seg);
15931 struct insn_label_list *l = si->label_list;
252b5132 15932
7d10b47d 15933 mips_emit_delays ();
252b5132
RH
15934
15935 if (auto_align)
49309057
ILT
15936 {
15937 if (type == 'd')
462427c4 15938 mips_align (3, 0, l);
49309057 15939 else
462427c4 15940 mips_align (2, 0, l);
49309057 15941 }
252b5132 15942
252b5132 15943 float_cons (type);
a1facbec 15944 mips_clear_insn_labels ();
252b5132
RH
15945}
15946
15947/* Handle .globl. We need to override it because on Irix 5 you are
15948 permitted to say
15949 .globl foo .text
15950 where foo is an undefined symbol, to mean that foo should be
15951 considered to be the address of a function. */
15952
15953static void
17a2f251 15954s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
15955{
15956 char *name;
15957 int c;
15958 symbolS *symbolP;
15959 flagword flag;
15960
8a06b769 15961 do
252b5132 15962 {
d02603dc 15963 c = get_symbol_name (&name);
8a06b769
TS
15964 symbolP = symbol_find_or_make (name);
15965 S_SET_EXTERNAL (symbolP);
15966
252b5132 15967 *input_line_pointer = c;
d02603dc 15968 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 15969
8a06b769
TS
15970 /* On Irix 5, every global symbol that is not explicitly labelled as
15971 being a function is apparently labelled as being an object. */
15972 flag = BSF_OBJECT;
252b5132 15973
8a06b769
TS
15974 if (!is_end_of_line[(unsigned char) *input_line_pointer]
15975 && (*input_line_pointer != ','))
15976 {
15977 char *secname;
15978 asection *sec;
15979
d02603dc 15980 c = get_symbol_name (&secname);
8a06b769
TS
15981 sec = bfd_get_section_by_name (stdoutput, secname);
15982 if (sec == NULL)
15983 as_bad (_("%s: no such section"), secname);
d02603dc 15984 (void) restore_line_pointer (c);
8a06b769
TS
15985
15986 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15987 flag = BSF_FUNCTION;
15988 }
15989
15990 symbol_get_bfdsym (symbolP)->flags |= flag;
15991
15992 c = *input_line_pointer;
15993 if (c == ',')
15994 {
15995 input_line_pointer++;
15996 SKIP_WHITESPACE ();
15997 if (is_end_of_line[(unsigned char) *input_line_pointer])
15998 c = '\n';
15999 }
16000 }
16001 while (c == ',');
252b5132 16002
252b5132
RH
16003 demand_empty_rest_of_line ();
16004}
16005
16006static void
17a2f251 16007s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16008{
16009 char *opt;
16010 char c;
16011
d02603dc 16012 c = get_symbol_name (&opt);
252b5132
RH
16013
16014 if (*opt == 'O')
16015 {
16016 /* FIXME: What does this mean? */
16017 }
41a1578e 16018 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16019 {
16020 int i;
16021
16022 i = atoi (opt + 3);
668c5ebc
MR
16023 if (i != 0 && i != 2)
16024 as_bad (_(".option pic%d not supported"), i);
16025 else if (mips_pic == VXWORKS_PIC)
16026 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16027 else if (i == 0)
252b5132
RH
16028 mips_pic = NO_PIC;
16029 else if (i == 2)
143d77c5 16030 {
8b828383 16031 mips_pic = SVR4_PIC;
143d77c5
EC
16032 mips_abicalls = TRUE;
16033 }
252b5132 16034
4d0d148d 16035 if (mips_pic == SVR4_PIC)
252b5132
RH
16036 {
16037 if (g_switch_seen && g_switch_value != 0)
16038 as_warn (_("-G may not be used with SVR4 PIC code"));
16039 g_switch_value = 0;
16040 bfd_set_gp_size (stdoutput, 0);
16041 }
16042 }
16043 else
1661c76c 16044 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16045
d02603dc 16046 (void) restore_line_pointer (c);
252b5132
RH
16047 demand_empty_rest_of_line ();
16048}
16049
16050/* This structure is used to hold a stack of .set values. */
16051
e972090a
NC
16052struct mips_option_stack
16053{
252b5132
RH
16054 struct mips_option_stack *next;
16055 struct mips_set_options options;
16056};
16057
16058static struct mips_option_stack *mips_opts_stack;
16059
22522f88
MR
16060/* Return status for .set/.module option handling. */
16061
16062enum code_option_type
16063{
16064 /* Unrecognized option. */
16065 OPTION_TYPE_BAD = -1,
16066
16067 /* Ordinary option. */
16068 OPTION_TYPE_NORMAL,
16069
16070 /* ISA changing option. */
16071 OPTION_TYPE_ISA
16072};
16073
16074/* Handle common .set/.module options. Return status indicating option
16075 type. */
16076
16077static enum code_option_type
919731af 16078parse_code_option (char * name)
252b5132 16079{
22522f88 16080 bfd_boolean isa_set = FALSE;
c6278170 16081 const struct mips_ase *ase;
22522f88 16082
919731af 16083 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16084 {
16085 char *s = name + 3;
16086
16087 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16088 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16089 }
252b5132 16090 else if (strcmp (name, "at") == 0)
919731af 16091 mips_opts.at = ATREG;
252b5132 16092 else if (strcmp (name, "noat") == 0)
919731af 16093 mips_opts.at = ZERO;
252b5132 16094 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16095 mips_opts.nomove = 0;
252b5132 16096 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16097 mips_opts.nomove = 1;
252b5132 16098 else if (strcmp (name, "bopt") == 0)
919731af 16099 mips_opts.nobopt = 0;
252b5132 16100 else if (strcmp (name, "nobopt") == 0)
919731af 16101 mips_opts.nobopt = 1;
ad3fea08 16102 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16103 mips_opts.gp = 32;
ad3fea08 16104 else if (strcmp (name, "gp=64") == 0)
919731af 16105 mips_opts.gp = 64;
ad3fea08 16106 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16107 mips_opts.fp = 32;
351cdf24
MF
16108 else if (strcmp (name, "fp=xx") == 0)
16109 mips_opts.fp = 0;
ad3fea08 16110 else if (strcmp (name, "fp=64") == 0)
919731af 16111 mips_opts.fp = 64;
037b32b9
AN
16112 else if (strcmp (name, "softfloat") == 0)
16113 mips_opts.soft_float = 1;
16114 else if (strcmp (name, "hardfloat") == 0)
16115 mips_opts.soft_float = 0;
16116 else if (strcmp (name, "singlefloat") == 0)
16117 mips_opts.single_float = 1;
16118 else if (strcmp (name, "doublefloat") == 0)
16119 mips_opts.single_float = 0;
351cdf24
MF
16120 else if (strcmp (name, "nooddspreg") == 0)
16121 mips_opts.oddspreg = 0;
16122 else if (strcmp (name, "oddspreg") == 0)
16123 mips_opts.oddspreg = 1;
252b5132
RH
16124 else if (strcmp (name, "mips16") == 0
16125 || strcmp (name, "MIPS-16") == 0)
919731af 16126 mips_opts.mips16 = 1;
252b5132
RH
16127 else if (strcmp (name, "nomips16") == 0
16128 || strcmp (name, "noMIPS-16") == 0)
16129 mips_opts.mips16 = 0;
df58fc94 16130 else if (strcmp (name, "micromips") == 0)
919731af 16131 mips_opts.micromips = 1;
df58fc94
RS
16132 else if (strcmp (name, "nomicromips") == 0)
16133 mips_opts.micromips = 0;
c6278170
RS
16134 else if (name[0] == 'n'
16135 && name[1] == 'o'
16136 && (ase = mips_lookup_ase (name + 2)))
919731af 16137 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16138 else if ((ase = mips_lookup_ase (name)))
919731af 16139 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16140 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16141 {
1a2c1fad
CD
16142 /* Permit the user to change the ISA and architecture on the fly.
16143 Needless to say, misuse can cause serious problems. */
919731af 16144 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16145 {
16146 const struct mips_cpu_info *p;
16147
919731af 16148 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16149 if (!p)
16150 as_bad (_("unknown architecture %s"), name + 5);
16151 else
16152 {
16153 mips_opts.arch = p->cpu;
16154 mips_opts.isa = p->isa;
22522f88 16155 isa_set = TRUE;
1a2c1fad
CD
16156 }
16157 }
81a21e38
TS
16158 else if (strncmp (name, "mips", 4) == 0)
16159 {
16160 const struct mips_cpu_info *p;
16161
919731af 16162 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16163 if (!p)
16164 as_bad (_("unknown ISA level %s"), name + 4);
16165 else
16166 {
16167 mips_opts.arch = p->cpu;
16168 mips_opts.isa = p->isa;
22522f88 16169 isa_set = TRUE;
81a21e38
TS
16170 }
16171 }
af7ee8bf 16172 else
81a21e38 16173 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16174 }
16175 else if (strcmp (name, "autoextend") == 0)
16176 mips_opts.noautoextend = 0;
16177 else if (strcmp (name, "noautoextend") == 0)
16178 mips_opts.noautoextend = 1;
833794fc
MR
16179 else if (strcmp (name, "insn32") == 0)
16180 mips_opts.insn32 = TRUE;
16181 else if (strcmp (name, "noinsn32") == 0)
16182 mips_opts.insn32 = FALSE;
919731af 16183 else if (strcmp (name, "sym32") == 0)
16184 mips_opts.sym32 = TRUE;
16185 else if (strcmp (name, "nosym32") == 0)
16186 mips_opts.sym32 = FALSE;
16187 else
22522f88
MR
16188 return OPTION_TYPE_BAD;
16189
16190 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16191}
16192
16193/* Handle the .set pseudo-op. */
16194
16195static void
16196s_mipsset (int x ATTRIBUTE_UNUSED)
16197{
22522f88 16198 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16199 char *name = input_line_pointer, ch;
919731af 16200
16201 file_mips_check_options ();
16202
16203 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16204 ++input_line_pointer;
16205 ch = *input_line_pointer;
16206 *input_line_pointer = '\0';
16207
16208 if (strchr (name, ','))
16209 {
16210 /* Generic ".set" directive; use the generic handler. */
16211 *input_line_pointer = ch;
16212 input_line_pointer = name;
16213 s_set (0);
16214 return;
16215 }
16216
16217 if (strcmp (name, "reorder") == 0)
16218 {
16219 if (mips_opts.noreorder)
16220 end_noreorder ();
16221 }
16222 else if (strcmp (name, "noreorder") == 0)
16223 {
16224 if (!mips_opts.noreorder)
16225 start_noreorder ();
16226 }
16227 else if (strcmp (name, "macro") == 0)
16228 mips_opts.warn_about_macros = 0;
16229 else if (strcmp (name, "nomacro") == 0)
16230 {
16231 if (mips_opts.noreorder == 0)
16232 as_bad (_("`noreorder' must be set before `nomacro'"));
16233 mips_opts.warn_about_macros = 1;
16234 }
16235 else if (strcmp (name, "gp=default") == 0)
16236 mips_opts.gp = file_mips_opts.gp;
16237 else if (strcmp (name, "fp=default") == 0)
16238 mips_opts.fp = file_mips_opts.fp;
16239 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16240 {
16241 mips_opts.isa = file_mips_opts.isa;
16242 mips_opts.arch = file_mips_opts.arch;
16243 mips_opts.gp = file_mips_opts.gp;
16244 mips_opts.fp = file_mips_opts.fp;
16245 }
252b5132
RH
16246 else if (strcmp (name, "push") == 0)
16247 {
16248 struct mips_option_stack *s;
16249
325801bd 16250 s = XNEW (struct mips_option_stack);
252b5132
RH
16251 s->next = mips_opts_stack;
16252 s->options = mips_opts;
16253 mips_opts_stack = s;
16254 }
16255 else if (strcmp (name, "pop") == 0)
16256 {
16257 struct mips_option_stack *s;
16258
16259 s = mips_opts_stack;
16260 if (s == NULL)
16261 as_bad (_(".set pop with no .set push"));
16262 else
16263 {
16264 /* If we're changing the reorder mode we need to handle
16265 delay slots correctly. */
16266 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16267 start_noreorder ();
252b5132 16268 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16269 end_noreorder ();
252b5132
RH
16270
16271 mips_opts = s->options;
16272 mips_opts_stack = s->next;
16273 free (s);
16274 }
16275 }
22522f88
MR
16276 else
16277 {
16278 type = parse_code_option (name);
16279 if (type == OPTION_TYPE_BAD)
16280 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16281 }
919731af 16282
16283 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16284 registers based on what is supported by the arch/cpu. */
22522f88 16285 if (type == OPTION_TYPE_ISA)
e6559e01 16286 {
919731af 16287 switch (mips_opts.isa)
16288 {
16289 case 0:
16290 break;
16291 case ISA_MIPS1:
351cdf24
MF
16292 /* MIPS I cannot support FPXX. */
16293 mips_opts.fp = 32;
16294 /* fall-through. */
919731af 16295 case ISA_MIPS2:
16296 case ISA_MIPS32:
16297 case ISA_MIPS32R2:
16298 case ISA_MIPS32R3:
16299 case ISA_MIPS32R5:
16300 mips_opts.gp = 32;
351cdf24
MF
16301 if (mips_opts.fp != 0)
16302 mips_opts.fp = 32;
919731af 16303 break;
7361da2c
AB
16304 case ISA_MIPS32R6:
16305 mips_opts.gp = 32;
16306 mips_opts.fp = 64;
16307 break;
919731af 16308 case ISA_MIPS3:
16309 case ISA_MIPS4:
16310 case ISA_MIPS5:
16311 case ISA_MIPS64:
16312 case ISA_MIPS64R2:
16313 case ISA_MIPS64R3:
16314 case ISA_MIPS64R5:
7361da2c 16315 case ISA_MIPS64R6:
919731af 16316 mips_opts.gp = 64;
351cdf24
MF
16317 if (mips_opts.fp != 0)
16318 {
16319 if (mips_opts.arch == CPU_R5900)
16320 mips_opts.fp = 32;
16321 else
16322 mips_opts.fp = 64;
16323 }
919731af 16324 break;
16325 default:
16326 as_bad (_("unknown ISA level %s"), name + 4);
16327 break;
16328 }
e6559e01 16329 }
919731af 16330
16331 mips_check_options (&mips_opts, FALSE);
16332
16333 mips_check_isa_supports_ases ();
16334 *input_line_pointer = ch;
16335 demand_empty_rest_of_line ();
16336}
16337
16338/* Handle the .module pseudo-op. */
16339
16340static void
16341s_module (int ignore ATTRIBUTE_UNUSED)
16342{
16343 char *name = input_line_pointer, ch;
16344
16345 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16346 ++input_line_pointer;
16347 ch = *input_line_pointer;
16348 *input_line_pointer = '\0';
16349
16350 if (!file_mips_opts_checked)
252b5132 16351 {
22522f88 16352 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16353 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16354
16355 /* Update module level settings from mips_opts. */
16356 file_mips_opts = mips_opts;
252b5132 16357 }
919731af 16358 else
16359 as_bad (_(".module is not permitted after generating code"));
16360
252b5132
RH
16361 *input_line_pointer = ch;
16362 demand_empty_rest_of_line ();
16363}
16364
16365/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16366 .option pic2. It means to generate SVR4 PIC calls. */
16367
16368static void
17a2f251 16369s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16370{
16371 mips_pic = SVR4_PIC;
143d77c5 16372 mips_abicalls = TRUE;
4d0d148d
TS
16373
16374 if (g_switch_seen && g_switch_value != 0)
16375 as_warn (_("-G may not be used with SVR4 PIC code"));
16376 g_switch_value = 0;
16377
252b5132
RH
16378 bfd_set_gp_size (stdoutput, 0);
16379 demand_empty_rest_of_line ();
16380}
16381
16382/* Handle the .cpload pseudo-op. This is used when generating SVR4
16383 PIC code. It sets the $gp register for the function based on the
16384 function address, which is in the register named in the argument.
16385 This uses a relocation against _gp_disp, which is handled specially
16386 by the linker. The result is:
16387 lui $gp,%hi(_gp_disp)
16388 addiu $gp,$gp,%lo(_gp_disp)
16389 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16390 The .cpload argument is normally $25 == $t9.
16391
16392 The -mno-shared option changes this to:
bbe506e8
TS
16393 lui $gp,%hi(__gnu_local_gp)
16394 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16395 and the argument is ignored. This saves an instruction, but the
16396 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16397 address for __gnu_local_gp. Thus code assembled with -mno-shared
16398 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16399
16400static void
17a2f251 16401s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16402{
16403 expressionS ex;
aa6975fb
ILT
16404 int reg;
16405 int in_shared;
252b5132 16406
919731af 16407 file_mips_check_options ();
16408
6478892d
TS
16409 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16410 .cpload is ignored. */
16411 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16412 {
16413 s_ignore (0);
16414 return;
16415 }
16416
a276b80c
MR
16417 if (mips_opts.mips16)
16418 {
16419 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16420 ignore_rest_of_line ();
16421 return;
16422 }
16423
d3ecfc59 16424 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16425 if (mips_opts.noreorder == 0)
16426 as_warn (_(".cpload not in noreorder section"));
16427
aa6975fb
ILT
16428 reg = tc_get_register (0);
16429
16430 /* If we need to produce a 64-bit address, we are better off using
16431 the default instruction sequence. */
aed1a261 16432 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16433
252b5132 16434 ex.X_op = O_symbol;
bbe506e8
TS
16435 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16436 "__gnu_local_gp");
252b5132
RH
16437 ex.X_op_symbol = NULL;
16438 ex.X_add_number = 0;
16439
16440 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16441 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16442
8a75745d
MR
16443 mips_mark_labels ();
16444 mips_assembling_insn = TRUE;
16445
584892a6 16446 macro_start ();
67c0d1eb
RS
16447 macro_build_lui (&ex, mips_gp_register);
16448 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16449 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16450 if (in_shared)
16451 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16452 mips_gp_register, reg);
584892a6 16453 macro_end ();
252b5132 16454
8a75745d 16455 mips_assembling_insn = FALSE;
252b5132
RH
16456 demand_empty_rest_of_line ();
16457}
16458
6478892d
TS
16459/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16460 .cpsetup $reg1, offset|$reg2, label
16461
16462 If offset is given, this results in:
16463 sd $gp, offset($sp)
956cd1d6 16464 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16465 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16466 daddu $gp, $gp, $reg1
6478892d
TS
16467
16468 If $reg2 is given, this results in:
40fc1451 16469 or $reg2, $gp, $0
956cd1d6 16470 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16471 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16472 daddu $gp, $gp, $reg1
aa6975fb
ILT
16473 $reg1 is normally $25 == $t9.
16474
16475 The -mno-shared option replaces the last three instructions with
16476 lui $gp,%hi(_gp)
54f4ddb3 16477 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16478
6478892d 16479static void
17a2f251 16480s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16481{
16482 expressionS ex_off;
16483 expressionS ex_sym;
16484 int reg1;
6478892d 16485
919731af 16486 file_mips_check_options ();
16487
8586fc66 16488 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16489 We also need NewABI support. */
16490 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16491 {
16492 s_ignore (0);
16493 return;
16494 }
16495
a276b80c
MR
16496 if (mips_opts.mips16)
16497 {
16498 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16499 ignore_rest_of_line ();
16500 return;
16501 }
16502
6478892d
TS
16503 reg1 = tc_get_register (0);
16504 SKIP_WHITESPACE ();
16505 if (*input_line_pointer != ',')
16506 {
16507 as_bad (_("missing argument separator ',' for .cpsetup"));
16508 return;
16509 }
16510 else
80245285 16511 ++input_line_pointer;
6478892d
TS
16512 SKIP_WHITESPACE ();
16513 if (*input_line_pointer == '$')
80245285
TS
16514 {
16515 mips_cpreturn_register = tc_get_register (0);
16516 mips_cpreturn_offset = -1;
16517 }
6478892d 16518 else
80245285
TS
16519 {
16520 mips_cpreturn_offset = get_absolute_expression ();
16521 mips_cpreturn_register = -1;
16522 }
6478892d
TS
16523 SKIP_WHITESPACE ();
16524 if (*input_line_pointer != ',')
16525 {
16526 as_bad (_("missing argument separator ',' for .cpsetup"));
16527 return;
16528 }
16529 else
f9419b05 16530 ++input_line_pointer;
6478892d 16531 SKIP_WHITESPACE ();
f21f8242 16532 expression (&ex_sym);
6478892d 16533
8a75745d
MR
16534 mips_mark_labels ();
16535 mips_assembling_insn = TRUE;
16536
584892a6 16537 macro_start ();
6478892d
TS
16538 if (mips_cpreturn_register == -1)
16539 {
16540 ex_off.X_op = O_constant;
16541 ex_off.X_add_symbol = NULL;
16542 ex_off.X_op_symbol = NULL;
16543 ex_off.X_add_number = mips_cpreturn_offset;
16544
67c0d1eb 16545 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16546 BFD_RELOC_LO16, SP);
6478892d
TS
16547 }
16548 else
40fc1451 16549 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16550
aed1a261 16551 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16552 {
df58fc94 16553 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16554 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16555 BFD_RELOC_HI16_S);
16556
16557 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16558 mips_gp_register, -1, BFD_RELOC_GPREL16,
16559 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16560
16561 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16562 mips_gp_register, reg1);
16563 }
16564 else
16565 {
16566 expressionS ex;
16567
16568 ex.X_op = O_symbol;
4184909a 16569 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16570 ex.X_op_symbol = NULL;
16571 ex.X_add_number = 0;
6e1304d8 16572
aa6975fb
ILT
16573 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16574 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16575
16576 macro_build_lui (&ex, mips_gp_register);
16577 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16578 mips_gp_register, BFD_RELOC_LO16);
16579 }
f21f8242 16580
584892a6 16581 macro_end ();
6478892d 16582
8a75745d 16583 mips_assembling_insn = FALSE;
6478892d
TS
16584 demand_empty_rest_of_line ();
16585}
16586
16587static void
17a2f251 16588s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16589{
919731af 16590 file_mips_check_options ();
16591
6478892d 16592 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16593 .cplocal is ignored. */
6478892d
TS
16594 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16595 {
16596 s_ignore (0);
16597 return;
16598 }
16599
a276b80c
MR
16600 if (mips_opts.mips16)
16601 {
16602 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16603 ignore_rest_of_line ();
16604 return;
16605 }
16606
6478892d 16607 mips_gp_register = tc_get_register (0);
85b51719 16608 demand_empty_rest_of_line ();
6478892d
TS
16609}
16610
252b5132
RH
16611/* Handle the .cprestore pseudo-op. This stores $gp into a given
16612 offset from $sp. The offset is remembered, and after making a PIC
16613 call $gp is restored from that location. */
16614
16615static void
17a2f251 16616s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16617{
16618 expressionS ex;
252b5132 16619
919731af 16620 file_mips_check_options ();
16621
6478892d 16622 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16623 .cprestore is ignored. */
6478892d 16624 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16625 {
16626 s_ignore (0);
16627 return;
16628 }
16629
a276b80c
MR
16630 if (mips_opts.mips16)
16631 {
16632 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16633 ignore_rest_of_line ();
16634 return;
16635 }
16636
252b5132 16637 mips_cprestore_offset = get_absolute_expression ();
7a621144 16638 mips_cprestore_valid = 1;
252b5132
RH
16639
16640 ex.X_op = O_constant;
16641 ex.X_add_symbol = NULL;
16642 ex.X_op_symbol = NULL;
16643 ex.X_add_number = mips_cprestore_offset;
16644
8a75745d
MR
16645 mips_mark_labels ();
16646 mips_assembling_insn = TRUE;
16647
584892a6 16648 macro_start ();
67c0d1eb
RS
16649 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16650 SP, HAVE_64BIT_ADDRESSES);
584892a6 16651 macro_end ();
252b5132 16652
8a75745d 16653 mips_assembling_insn = FALSE;
252b5132
RH
16654 demand_empty_rest_of_line ();
16655}
16656
6478892d 16657/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16658 was given in the preceding .cpsetup, it results in:
6478892d 16659 ld $gp, offset($sp)
76b3015f 16660
6478892d 16661 If a register $reg2 was given there, it results in:
40fc1451 16662 or $gp, $reg2, $0 */
54f4ddb3 16663
6478892d 16664static void
17a2f251 16665s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16666{
16667 expressionS ex;
6478892d 16668
919731af 16669 file_mips_check_options ();
16670
6478892d
TS
16671 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16672 We also need NewABI support. */
16673 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16674 {
16675 s_ignore (0);
16676 return;
16677 }
16678
a276b80c
MR
16679 if (mips_opts.mips16)
16680 {
16681 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16682 ignore_rest_of_line ();
16683 return;
16684 }
16685
8a75745d
MR
16686 mips_mark_labels ();
16687 mips_assembling_insn = TRUE;
16688
584892a6 16689 macro_start ();
6478892d
TS
16690 if (mips_cpreturn_register == -1)
16691 {
16692 ex.X_op = O_constant;
16693 ex.X_add_symbol = NULL;
16694 ex.X_op_symbol = NULL;
16695 ex.X_add_number = mips_cpreturn_offset;
16696
67c0d1eb 16697 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16698 }
16699 else
40fc1451
SD
16700 move_register (mips_gp_register, mips_cpreturn_register);
16701
584892a6 16702 macro_end ();
6478892d 16703
8a75745d 16704 mips_assembling_insn = FALSE;
6478892d
TS
16705 demand_empty_rest_of_line ();
16706}
16707
d0f13682
CLT
16708/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16709 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16710 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16711 debug information or MIPS16 TLS. */
741d6ea8
JM
16712
16713static void
d0f13682
CLT
16714s_tls_rel_directive (const size_t bytes, const char *dirstr,
16715 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16716{
16717 expressionS ex;
16718 char *p;
16719
16720 expression (&ex);
16721
16722 if (ex.X_op != O_symbol)
16723 {
1661c76c 16724 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16725 ignore_rest_of_line ();
16726 }
16727
16728 p = frag_more (bytes);
16729 md_number_to_chars (p, 0, bytes);
d0f13682 16730 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16731 demand_empty_rest_of_line ();
de64cffd 16732 mips_clear_insn_labels ();
741d6ea8
JM
16733}
16734
16735/* Handle .dtprelword. */
16736
16737static void
16738s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16739{
d0f13682 16740 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16741}
16742
16743/* Handle .dtpreldword. */
16744
16745static void
16746s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16747{
d0f13682
CLT
16748 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16749}
16750
16751/* Handle .tprelword. */
16752
16753static void
16754s_tprelword (int ignore ATTRIBUTE_UNUSED)
16755{
16756 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16757}
16758
16759/* Handle .tpreldword. */
16760
16761static void
16762s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16763{
16764 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16765}
16766
6478892d
TS
16767/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16768 code. It sets the offset to use in gp_rel relocations. */
16769
16770static void
17a2f251 16771s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16772{
16773 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16774 We also need NewABI support. */
16775 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16776 {
16777 s_ignore (0);
16778 return;
16779 }
16780
def2e0dd 16781 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16782
16783 demand_empty_rest_of_line ();
16784}
16785
252b5132
RH
16786/* Handle the .gpword pseudo-op. This is used when generating PIC
16787 code. It generates a 32 bit GP relative reloc. */
16788
16789static void
17a2f251 16790s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16791{
a8dbcb85
TS
16792 segment_info_type *si;
16793 struct insn_label_list *l;
252b5132
RH
16794 expressionS ex;
16795 char *p;
16796
16797 /* When not generating PIC code, this is treated as .word. */
16798 if (mips_pic != SVR4_PIC)
16799 {
16800 s_cons (2);
16801 return;
16802 }
16803
a8dbcb85
TS
16804 si = seg_info (now_seg);
16805 l = si->label_list;
7d10b47d 16806 mips_emit_delays ();
252b5132 16807 if (auto_align)
462427c4 16808 mips_align (2, 0, l);
252b5132
RH
16809
16810 expression (&ex);
a1facbec 16811 mips_clear_insn_labels ();
252b5132
RH
16812
16813 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16814 {
1661c76c 16815 as_bad (_("unsupported use of .gpword"));
252b5132
RH
16816 ignore_rest_of_line ();
16817 }
16818
16819 p = frag_more (4);
17a2f251 16820 md_number_to_chars (p, 0, 4);
b34976b6 16821 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16822 BFD_RELOC_GPREL32);
252b5132
RH
16823
16824 demand_empty_rest_of_line ();
16825}
16826
10181a0d 16827static void
17a2f251 16828s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16829{
a8dbcb85
TS
16830 segment_info_type *si;
16831 struct insn_label_list *l;
10181a0d
AO
16832 expressionS ex;
16833 char *p;
16834
16835 /* When not generating PIC code, this is treated as .dword. */
16836 if (mips_pic != SVR4_PIC)
16837 {
16838 s_cons (3);
16839 return;
16840 }
16841
a8dbcb85
TS
16842 si = seg_info (now_seg);
16843 l = si->label_list;
7d10b47d 16844 mips_emit_delays ();
10181a0d 16845 if (auto_align)
462427c4 16846 mips_align (3, 0, l);
10181a0d
AO
16847
16848 expression (&ex);
a1facbec 16849 mips_clear_insn_labels ();
10181a0d
AO
16850
16851 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16852 {
1661c76c 16853 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
16854 ignore_rest_of_line ();
16855 }
16856
16857 p = frag_more (8);
17a2f251 16858 md_number_to_chars (p, 0, 8);
a105a300 16859 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 16860 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
16861
16862 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
16863 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16864 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
16865
16866 demand_empty_rest_of_line ();
16867}
16868
a3f278e2
CM
16869/* Handle the .ehword pseudo-op. This is used when generating unwinding
16870 tables. It generates a R_MIPS_EH reloc. */
16871
16872static void
16873s_ehword (int ignore ATTRIBUTE_UNUSED)
16874{
16875 expressionS ex;
16876 char *p;
16877
16878 mips_emit_delays ();
16879
16880 expression (&ex);
16881 mips_clear_insn_labels ();
16882
16883 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16884 {
1661c76c 16885 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
16886 ignore_rest_of_line ();
16887 }
16888
16889 p = frag_more (4);
16890 md_number_to_chars (p, 0, 4);
16891 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 16892 BFD_RELOC_32_PCREL);
a3f278e2
CM
16893
16894 demand_empty_rest_of_line ();
16895}
16896
252b5132
RH
16897/* Handle the .cpadd pseudo-op. This is used when dealing with switch
16898 tables in SVR4 PIC code. */
16899
16900static void
17a2f251 16901s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 16902{
252b5132
RH
16903 int reg;
16904
919731af 16905 file_mips_check_options ();
16906
10181a0d
AO
16907 /* This is ignored when not generating SVR4 PIC code. */
16908 if (mips_pic != SVR4_PIC)
252b5132
RH
16909 {
16910 s_ignore (0);
16911 return;
16912 }
16913
8a75745d
MR
16914 mips_mark_labels ();
16915 mips_assembling_insn = TRUE;
16916
252b5132 16917 /* Add $gp to the register named as an argument. */
584892a6 16918 macro_start ();
252b5132 16919 reg = tc_get_register (0);
67c0d1eb 16920 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 16921 macro_end ();
252b5132 16922
8a75745d 16923 mips_assembling_insn = FALSE;
bdaaa2e1 16924 demand_empty_rest_of_line ();
252b5132
RH
16925}
16926
16927/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 16928 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
16929 such as generating jalx instructions when needed. We also make
16930 them odd for the duration of the assembly, in order to generate the
16931 right sort of code. We will make them even in the adjust_symtab
16932 routine, while leaving them marked. This is convenient for the
16933 debugger and the disassembler. The linker knows to make them odd
16934 again. */
16935
16936static void
17a2f251 16937s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 16938{
7bb01e2d
MR
16939 file_mips_check_options ();
16940 file_ase_mips16 |= mips_opts.mips16;
16941 file_ase_micromips |= mips_opts.micromips;
16942
df58fc94 16943 mips_mark_labels ();
252b5132
RH
16944
16945 demand_empty_rest_of_line ();
16946}
16947
ba92f887
MR
16948/* Handle the .nan pseudo-op. */
16949
16950static void
16951s_nan (int ignore ATTRIBUTE_UNUSED)
16952{
16953 static const char str_legacy[] = "legacy";
16954 static const char str_2008[] = "2008";
16955 size_t i;
16956
16957 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16958
16959 if (i == sizeof (str_2008) - 1
16960 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 16961 mips_nan2008 = 1;
ba92f887
MR
16962 else if (i == sizeof (str_legacy) - 1
16963 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
16964 {
16965 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16966 mips_nan2008 = 0;
16967 else
16968 as_bad (_("`%s' does not support legacy NaN"),
16969 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16970 }
ba92f887 16971 else
1661c76c 16972 as_bad (_("bad .nan directive"));
ba92f887
MR
16973
16974 input_line_pointer += i;
16975 demand_empty_rest_of_line ();
16976}
16977
754e2bb9
RS
16978/* Handle a .stab[snd] directive. Ideally these directives would be
16979 implemented in a transparent way, so that removing them would not
16980 have any effect on the generated instructions. However, s_stab
16981 internally changes the section, so in practice we need to decide
16982 now whether the preceding label marks compressed code. We do not
16983 support changing the compression mode of a label after a .stab*
16984 directive, such as in:
16985
16986 foo:
134c0c8b 16987 .stabs ...
754e2bb9
RS
16988 .set mips16
16989
16990 so the current mode wins. */
252b5132
RH
16991
16992static void
17a2f251 16993s_mips_stab (int type)
252b5132 16994{
754e2bb9 16995 mips_mark_labels ();
252b5132
RH
16996 s_stab (type);
16997}
16998
54f4ddb3 16999/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17000
17001static void
17a2f251 17002s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17003{
17004 char *name;
17005 int c;
17006 symbolS *symbolP;
17007 expressionS exp;
17008
d02603dc 17009 c = get_symbol_name (&name);
252b5132
RH
17010 symbolP = symbol_find_or_make (name);
17011 S_SET_WEAK (symbolP);
17012 *input_line_pointer = c;
17013
d02603dc 17014 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17015
17016 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17017 {
17018 if (S_IS_DEFINED (symbolP))
17019 {
20203fb9 17020 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17021 S_GET_NAME (symbolP));
17022 ignore_rest_of_line ();
17023 return;
17024 }
bdaaa2e1 17025
252b5132
RH
17026 if (*input_line_pointer == ',')
17027 {
17028 ++input_line_pointer;
17029 SKIP_WHITESPACE ();
17030 }
bdaaa2e1 17031
252b5132
RH
17032 expression (&exp);
17033 if (exp.X_op != O_symbol)
17034 {
20203fb9 17035 as_bad (_("bad .weakext directive"));
98d3f06f 17036 ignore_rest_of_line ();
252b5132
RH
17037 return;
17038 }
49309057 17039 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17040 }
17041
17042 demand_empty_rest_of_line ();
17043}
17044
17045/* Parse a register string into a number. Called from the ECOFF code
17046 to parse .frame. The argument is non-zero if this is the frame
17047 register, so that we can record it in mips_frame_reg. */
17048
17049int
17a2f251 17050tc_get_register (int frame)
252b5132 17051{
707bfff6 17052 unsigned int reg;
252b5132
RH
17053
17054 SKIP_WHITESPACE ();
707bfff6
TS
17055 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17056 reg = 0;
252b5132 17057 if (frame)
7a621144
DJ
17058 {
17059 mips_frame_reg = reg != 0 ? reg : SP;
17060 mips_frame_reg_valid = 1;
17061 mips_cprestore_valid = 0;
17062 }
252b5132
RH
17063 return reg;
17064}
17065
17066valueT
17a2f251 17067md_section_align (asection *seg, valueT addr)
252b5132
RH
17068{
17069 int align = bfd_get_section_alignment (stdoutput, seg);
17070
f3ded42a
RS
17071 /* We don't need to align ELF sections to the full alignment.
17072 However, Irix 5 may prefer that we align them at least to a 16
17073 byte boundary. We don't bother to align the sections if we
17074 are targeted for an embedded system. */
17075 if (strncmp (TARGET_OS, "elf", 3) == 0)
17076 return addr;
17077 if (align > 4)
17078 align = 4;
252b5132 17079
8d3842cd 17080 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17081}
17082
17083/* Utility routine, called from above as well. If called while the
17084 input file is still being read, it's only an approximation. (For
17085 example, a symbol may later become defined which appeared to be
17086 undefined earlier.) */
17087
17088static int
17a2f251 17089nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17090{
17091 if (sym == 0)
17092 return 0;
17093
4d0d148d 17094 if (g_switch_value > 0)
252b5132
RH
17095 {
17096 const char *symname;
17097 int change;
17098
c9914766 17099 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17100 register. It can be if it is smaller than the -G size or if
17101 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17102 not be referenced off the $gp, although it appears as though
252b5132
RH
17103 they can. */
17104 symname = S_GET_NAME (sym);
17105 if (symname != (const char *) NULL
17106 && (strcmp (symname, "eprol") == 0
17107 || strcmp (symname, "etext") == 0
17108 || strcmp (symname, "_gp") == 0
17109 || strcmp (symname, "edata") == 0
17110 || strcmp (symname, "_fbss") == 0
17111 || strcmp (symname, "_fdata") == 0
17112 || strcmp (symname, "_ftext") == 0
17113 || strcmp (symname, "end") == 0
17114 || strcmp (symname, "_gp_disp") == 0))
17115 change = 1;
17116 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17117 && (0
17118#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17119 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17120 && (symbol_get_obj (sym)->ecoff_extern_size
17121 <= g_switch_value))
252b5132
RH
17122#endif
17123 /* We must defer this decision until after the whole
17124 file has been read, since there might be a .extern
17125 after the first use of this symbol. */
17126 || (before_relaxing
17127#ifndef NO_ECOFF_DEBUGGING
49309057 17128 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17129#endif
17130 && S_GET_VALUE (sym) == 0)
17131 || (S_GET_VALUE (sym) != 0
17132 && S_GET_VALUE (sym) <= g_switch_value)))
17133 change = 0;
17134 else
17135 {
17136 const char *segname;
17137
17138 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17139 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17140 && strcmp (segname, ".lit4") != 0);
17141 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17142 && strcmp (segname, ".sbss") != 0
17143 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17144 && strncmp (segname, ".sbss.", 6) != 0
17145 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17146 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17147 }
17148 return change;
17149 }
17150 else
c9914766 17151 /* We are not optimizing for the $gp register. */
252b5132
RH
17152 return 1;
17153}
17154
5919d012
RS
17155
17156/* Return true if the given symbol should be considered local for SVR4 PIC. */
17157
17158static bfd_boolean
9e009953 17159pic_need_relax (symbolS *sym)
5919d012
RS
17160{
17161 asection *symsec;
5919d012
RS
17162
17163 /* Handle the case of a symbol equated to another symbol. */
17164 while (symbol_equated_reloc_p (sym))
17165 {
17166 symbolS *n;
17167
5f0fe04b 17168 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17169 n = symbol_get_value_expression (sym)->X_add_symbol;
17170 if (n == sym)
17171 break;
17172 sym = n;
17173 }
17174
df1f3cda
DD
17175 if (symbol_section_p (sym))
17176 return TRUE;
17177
5919d012
RS
17178 symsec = S_GET_SEGMENT (sym);
17179
5919d012 17180 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17181 return (!bfd_is_und_section (symsec)
17182 && !bfd_is_abs_section (symsec)
5f0fe04b 17183 && !bfd_is_com_section (symsec)
5919d012 17184 /* A global or weak symbol is treated as external. */
f3ded42a 17185 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17186}
14f72d45
MR
17187\f
17188/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17189 convert a section-relative value VAL to the equivalent PC-relative
17190 value. */
17191
17192static offsetT
17193mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17194 offsetT val, long stretch)
17195{
17196 fragS *sym_frag;
17197 addressT addr;
17198
17199 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17200
17201 sym_frag = symbol_get_frag (fragp->fr_symbol);
17202
17203 /* If the relax_marker of the symbol fragment differs from the
17204 relax_marker of this fragment, we have not yet adjusted the
17205 symbol fragment fr_address. We want to add in STRETCH in
17206 order to get a better estimate of the address. This
17207 particularly matters because of the shift bits. */
17208 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17209 {
17210 fragS *f;
17211
17212 /* Adjust stretch for any alignment frag. Note that if have
17213 been expanding the earlier code, the symbol may be
17214 defined in what appears to be an earlier frag. FIXME:
17215 This doesn't handle the fr_subtype field, which specifies
17216 a maximum number of bytes to skip when doing an
17217 alignment. */
17218 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17219 {
17220 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17221 {
17222 if (stretch < 0)
17223 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17224 else
17225 stretch &= ~((1 << (int) f->fr_offset) - 1);
17226 if (stretch == 0)
17227 break;
17228 }
17229 }
17230 if (f != NULL)
17231 val += stretch;
17232 }
17233
17234 addr = fragp->fr_address + fragp->fr_fix;
17235
17236 /* The base address rules are complicated. The base address of
17237 a branch is the following instruction. The base address of a
17238 PC relative load or add is the instruction itself, but if it
17239 is in a delay slot (in which case it can not be extended) use
17240 the address of the instruction whose delay slot it is in. */
17241 if (pcrel_op->include_isa_bit)
17242 {
17243 addr += 2;
17244
17245 /* If we are currently assuming that this frag should be
17246 extended, then the current address is two bytes higher. */
17247 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17248 addr += 2;
17249
17250 /* Ignore the low bit in the target, since it will be set
17251 for a text label. */
17252 val &= -2;
17253 }
17254 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17255 addr -= 4;
17256 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17257 addr -= 2;
5919d012 17258
14f72d45
MR
17259 val -= addr & -(1 << pcrel_op->align_log2);
17260
17261 return val;
17262}
5919d012 17263
252b5132
RH
17264/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17265 extended opcode. SEC is the section the frag is in. */
17266
17267static int
17a2f251 17268mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17269{
3ccad066 17270 const struct mips_int_operand *operand;
252b5132 17271 offsetT val;
252b5132 17272 segT symsec;
14f72d45 17273 int type;
252b5132
RH
17274
17275 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17276 return 0;
17277 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17278 return 1;
17279
88a7ef16 17280 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17281 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17282 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17283 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17284 || (operand->root.type == OP_PCREL
17285 ? sec != symsec
17286 : !bfd_is_abs_section (symsec)))
17287 return 1;
252b5132 17288
88a7ef16 17289 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17290
3ccad066 17291 if (operand->root.type == OP_PCREL)
252b5132 17292 {
3ccad066 17293 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17294 offsetT maxtiny;
252b5132 17295
1425c41d 17296 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17297 return 1;
252b5132 17298
88a7ef16 17299 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17300 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17301
17302 /* If any of the shifted bits are set, we must use an extended
17303 opcode. If the address depends on the size of this
17304 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17305 use an extended opcode. */
17306 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17307 {
17308 fragp->fr_subtype =
1425c41d 17309 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17310 return 1;
17311 }
17312
17313 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17314 is precisely the next value above maxtiny, then there is a
17315 chance of an infinite loop as in the following code:
252b5132
RH
17316 la $4,foo
17317 .skip 1020
17318 .align 2
17319 foo:
17320 In this case when the la is extended, foo is 0x3fc bytes
17321 away, so the la can be shrunk, but then foo is 0x400 away, so
17322 the la must be extended. To avoid this loop, we mark the
17323 frag as extended if it was small, and is about to become
3ccad066
RS
17324 extended with the next value above maxtiny. */
17325 maxtiny = mips_int_operand_max (operand);
17326 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17327 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17328 {
17329 fragp->fr_subtype =
1425c41d 17330 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17331 return 1;
17332 }
17333 }
252b5132 17334
3ccad066 17335 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17336}
17337
4a6a3df4
AO
17338/* Compute the length of a branch sequence, and adjust the
17339 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17340 worst-case length is computed, with UPDATE being used to indicate
17341 whether an unconditional (-1), branch-likely (+1) or regular (0)
17342 branch is to be computed. */
17343static int
17a2f251 17344relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17345{
b34976b6 17346 bfd_boolean toofar;
4a6a3df4
AO
17347 int length;
17348
17349 if (fragp
17350 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17351 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17352 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17353 {
17354 addressT addr;
17355 offsetT val;
17356
17357 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17358
17359 addr = fragp->fr_address + fragp->fr_fix + 4;
17360
17361 val -= addr;
17362
17363 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17364 }
4a6a3df4 17365 else
c1f61bd2
MR
17366 /* If the symbol is not defined or it's in a different segment,
17367 we emit the long sequence. */
b34976b6 17368 toofar = TRUE;
4a6a3df4
AO
17369
17370 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17371 fragp->fr_subtype
66b3e8da 17372 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17373 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17374 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17375 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17376 RELAX_BRANCH_LINK (fragp->fr_subtype),
17377 toofar);
17378
17379 length = 4;
17380 if (toofar)
17381 {
17382 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17383 length += 8;
17384
ce8ad872 17385 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17386 {
17387 /* Additional space for PIC loading of target address. */
17388 length += 8;
17389 if (mips_opts.isa == ISA_MIPS1)
17390 /* Additional space for $at-stabilizing nop. */
17391 length += 4;
17392 }
17393
17394 /* If branch is conditional. */
17395 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17396 length += 8;
17397 }
b34976b6 17398
4a6a3df4
AO
17399 return length;
17400}
17401
7bd374a4
MR
17402/* Get a FRAG's branch instruction delay slot size, either from the
17403 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17404 or SHORT_INSN_SIZE otherwise. */
17405
17406static int
17407frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17408{
17409 char *buf = fragp->fr_literal + fragp->fr_fix;
17410
17411 if (al)
17412 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17413 else
17414 return short_insn_size;
17415}
17416
df58fc94
RS
17417/* Compute the length of a branch sequence, and adjust the
17418 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17419 worst-case length is computed, with UPDATE being used to indicate
17420 whether an unconditional (-1), or regular (0) branch is to be
17421 computed. */
17422
17423static int
17424relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17425{
7bd374a4
MR
17426 bfd_boolean insn32 = TRUE;
17427 bfd_boolean nods = TRUE;
ce8ad872 17428 bfd_boolean pic = TRUE;
7bd374a4
MR
17429 bfd_boolean al = TRUE;
17430 int short_insn_size;
df58fc94
RS
17431 bfd_boolean toofar;
17432 int length;
17433
7bd374a4
MR
17434 if (fragp)
17435 {
17436 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17437 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17438 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17439 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17440 }
17441 short_insn_size = insn32 ? 4 : 2;
17442
df58fc94
RS
17443 if (fragp
17444 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17445 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17446 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17447 {
17448 addressT addr;
17449 offsetT val;
17450
17451 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17452 /* Ignore the low bit in the target, since it will be set
17453 for a text label. */
17454 if ((val & 1) != 0)
17455 --val;
17456
17457 addr = fragp->fr_address + fragp->fr_fix + 4;
17458
17459 val -= addr;
17460
17461 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17462 }
df58fc94 17463 else
c1f61bd2
MR
17464 /* If the symbol is not defined or it's in a different segment,
17465 we emit the long sequence. */
df58fc94
RS
17466 toofar = TRUE;
17467
17468 if (fragp && update
17469 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17470 fragp->fr_subtype = (toofar
17471 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17472 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17473
17474 length = 4;
17475 if (toofar)
17476 {
17477 bfd_boolean compact_known = fragp != NULL;
17478 bfd_boolean compact = FALSE;
17479 bfd_boolean uncond;
17480
df58fc94 17481 if (fragp)
8484fb75
MR
17482 {
17483 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17484 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17485 }
df58fc94
RS
17486 else
17487 uncond = update < 0;
17488
17489 /* If label is out of range, we turn branch <br>:
17490
17491 <br> label # 4 bytes
17492 0:
17493
17494 into:
17495
17496 j label # 4 bytes
8484fb75
MR
17497 nop # 2/4 bytes if
17498 # compact && (!PIC || insn32)
df58fc94
RS
17499 0:
17500 */
ce8ad872 17501 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17502 length += short_insn_size;
df58fc94
RS
17503
17504 /* If assembling PIC code, we further turn:
17505
17506 j label # 4 bytes
17507
17508 into:
17509
17510 lw/ld at, %got(label)(gp) # 4 bytes
17511 d/addiu at, %lo(label) # 4 bytes
8484fb75 17512 jr/c at # 2/4 bytes
df58fc94 17513 */
ce8ad872 17514 if (pic)
8484fb75 17515 length += 4 + short_insn_size;
df58fc94 17516
7bd374a4
MR
17517 /* Add an extra nop if the jump has no compact form and we need
17518 to fill the delay slot. */
ce8ad872 17519 if ((!pic || al) && nods)
7bd374a4
MR
17520 length += (fragp
17521 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17522 : short_insn_size);
17523
df58fc94
RS
17524 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17525
17526 <brneg> 0f # 4 bytes
8484fb75 17527 nop # 2/4 bytes if !compact
df58fc94
RS
17528 */
17529 if (!uncond)
8484fb75 17530 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17531 }
7bd374a4
MR
17532 else if (nods)
17533 {
17534 /* Add an extra nop to fill the delay slot. */
17535 gas_assert (fragp);
17536 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17537 }
df58fc94
RS
17538
17539 return length;
17540}
17541
17542/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17543 bit accordingly. */
17544
17545static int
17546relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17547{
17548 bfd_boolean toofar;
17549
df58fc94
RS
17550 if (fragp
17551 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17552 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17553 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17554 {
17555 addressT addr;
17556 offsetT val;
17557 int type;
17558
17559 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17560 /* Ignore the low bit in the target, since it will be set
17561 for a text label. */
17562 if ((val & 1) != 0)
17563 --val;
17564
17565 /* Assume this is a 2-byte branch. */
17566 addr = fragp->fr_address + fragp->fr_fix + 2;
17567
17568 /* We try to avoid the infinite loop by not adding 2 more bytes for
17569 long branches. */
17570
17571 val -= addr;
17572
17573 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17574 if (type == 'D')
17575 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17576 else if (type == 'E')
17577 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17578 else
17579 abort ();
17580 }
17581 else
17582 /* If the symbol is not defined or it's in a different segment,
17583 we emit a normal 32-bit branch. */
17584 toofar = TRUE;
17585
17586 if (fragp && update
17587 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17588 fragp->fr_subtype
17589 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17590 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17591
17592 if (toofar)
17593 return 4;
17594
17595 return 2;
17596}
17597
252b5132
RH
17598/* Estimate the size of a frag before relaxing. Unless this is the
17599 mips16, we are not really relaxing here, and the final size is
17600 encoded in the subtype information. For the mips16, we have to
17601 decide whether we are using an extended opcode or not. */
17602
252b5132 17603int
17a2f251 17604md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17605{
5919d012 17606 int change;
252b5132 17607
4a6a3df4
AO
17608 if (RELAX_BRANCH_P (fragp->fr_subtype))
17609 {
17610
b34976b6
AM
17611 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17612
4a6a3df4
AO
17613 return fragp->fr_var;
17614 }
17615
252b5132 17616 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17617 /* We don't want to modify the EXTENDED bit here; it might get us
17618 into infinite loops. We change it only in mips_relax_frag(). */
17619 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17620
df58fc94
RS
17621 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17622 {
17623 int length = 4;
17624
17625 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17626 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17627 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17628 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17629 fragp->fr_var = length;
17630
17631 return length;
17632 }
17633
ce8ad872 17634 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17635 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17636 change = 0;
ce8ad872
MR
17637 else if (RELAX_PIC (fragp->fr_subtype))
17638 change = pic_need_relax (fragp->fr_symbol);
252b5132 17639 else
ce8ad872 17640 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17641
17642 if (change)
17643 {
4d7206a2 17644 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17645 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17646 }
4d7206a2
RS
17647 else
17648 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17649}
17650
17651/* This is called to see whether a reloc against a defined symbol
de7e6852 17652 should be converted into a reloc against a section. */
252b5132
RH
17653
17654int
17a2f251 17655mips_fix_adjustable (fixS *fixp)
252b5132 17656{
252b5132
RH
17657 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17658 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17659 return 0;
a161fe53 17660
252b5132
RH
17661 if (fixp->fx_addsy == NULL)
17662 return 1;
a161fe53 17663
2f0c68f2
CM
17664 /* Allow relocs used for EH tables. */
17665 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17666 return 1;
17667
de7e6852
RS
17668 /* If symbol SYM is in a mergeable section, relocations of the form
17669 SYM + 0 can usually be made section-relative. The mergeable data
17670 is then identified by the section offset rather than by the symbol.
17671
17672 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17673 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17674 need to recalculate the complete offset in order to correctly identify
17675 the merge data.
17676
33eaf5de 17677 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17678 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17679 placed anywhere. Rather than break backwards compatibility by changing
17680 this, it seems better not to force the issue, and instead keep the
17681 original symbol. This will work with either linker behavior. */
738e5348 17682 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17683 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17684 && HAVE_IN_PLACE_ADDENDS
17685 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17686 return 0;
17687
97f50151
MR
17688 /* There is no place to store an in-place offset for JALR relocations. */
17689 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17690 return 0;
17691
17692 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17693 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17694 start address of the symbol's containing section.
17695
17696 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17697 section relative to allow linker relaxations to be performed later on. */
97f50151 17698 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17699 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17700 return 0;
17701
b314ec0e
RS
17702 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17703 to a floating-point stub. The same is true for non-R_MIPS16_26
17704 relocations against MIPS16 functions; in this case, the stub becomes
17705 the function's canonical address.
17706
17707 Floating-point stubs are stored in unique .mips16.call.* or
17708 .mips16.fn.* sections. If a stub T for function F is in section S,
17709 the first relocation in section S must be against F; this is how the
17710 linker determines the target function. All relocations that might
17711 resolve to T must also be against F. We therefore have the following
17712 restrictions, which are given in an intentionally-redundant way:
17713
17714 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17715 symbols.
17716
17717 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17718 if that stub might be used.
17719
17720 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17721 symbols.
17722
17723 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17724 that stub might be used.
17725
17726 There is a further restriction:
17727
df58fc94 17728 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 17729 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
17730 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17731 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17732 against MIPS16 or microMIPS symbols because we need to keep the
17733 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
17734 detection and JAL or BAL to JALX instruction conversion in the
17735 linker.
b314ec0e 17736
df58fc94 17737 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 17738 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 17739 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
17740
17741 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17742 relocation against some symbol R, no relocation against R may be
17743 reduced. (Note that this deals with (2) as well as (1) because
17744 relocations against global symbols will never be reduced on ELF
17745 targets.) This approach is a little simpler than trying to detect
17746 stub sections, and gives the "all or nothing" per-symbol consistency
17747 that we have for MIPS16 symbols. */
f3ded42a 17748 if (fixp->fx_subsy == NULL
30c09090 17749 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 17750 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
17751 && (jmp_reloc_p (fixp->fx_r_type)
17752 || b_reloc_p (fixp->fx_r_type)))
44d3da23 17753 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 17754 return 0;
a161fe53 17755
252b5132
RH
17756 return 1;
17757}
17758
17759/* Translate internal representation of relocation info to BFD target
17760 format. */
17761
17762arelent **
17a2f251 17763tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17764{
17765 static arelent *retval[4];
17766 arelent *reloc;
17767 bfd_reloc_code_real_type code;
17768
4b0cff4e 17769 memset (retval, 0, sizeof(retval));
325801bd
TS
17770 reloc = retval[0] = XCNEW (arelent);
17771 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 17772 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17773 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17774
bad36eac
DJ
17775 if (fixp->fx_pcrel)
17776 {
df58fc94 17777 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 17778 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
17779 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17780 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 17781 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
17782 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17783 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17784 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17785 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17786 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17787 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17788 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
17789
17790 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17791 Relocations want only the symbol offset. */
51f6035b
MR
17792 switch (fixp->fx_r_type)
17793 {
17794 case BFD_RELOC_MIPS_18_PCREL_S3:
17795 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17796 break;
17797 default:
17798 reloc->addend = fixp->fx_addnumber + reloc->address;
17799 break;
17800 }
bad36eac 17801 }
17c6c9d9
MR
17802 else if (HAVE_IN_PLACE_ADDENDS
17803 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17804 && (read_compressed_insn (fixp->fx_frag->fr_literal
17805 + fixp->fx_where, 4) >> 26) == 0x3c)
17806 {
17807 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17808 addend accordingly. */
17809 reloc->addend = fixp->fx_addnumber >> 1;
17810 }
bad36eac
DJ
17811 else
17812 reloc->addend = fixp->fx_addnumber;
252b5132 17813
438c16b8
TS
17814 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17815 entry to be used in the relocation's section offset. */
17816 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17817 {
17818 reloc->address = reloc->addend;
17819 reloc->addend = 0;
17820 }
17821
252b5132 17822 code = fixp->fx_r_type;
252b5132 17823
bad36eac 17824 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17825 if (reloc->howto == NULL)
17826 {
17827 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
17828 _("cannot represent %s relocation in this object file"
17829 " format"),
252b5132
RH
17830 bfd_get_reloc_code_name (code));
17831 retval[0] = NULL;
17832 }
17833
17834 return retval;
17835}
17836
17837/* Relax a machine dependent frag. This returns the amount by which
17838 the current size of the frag should change. */
17839
17840int
17a2f251 17841mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17842{
4a6a3df4
AO
17843 if (RELAX_BRANCH_P (fragp->fr_subtype))
17844 {
17845 offsetT old_var = fragp->fr_var;
b34976b6
AM
17846
17847 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17848
17849 return fragp->fr_var - old_var;
17850 }
17851
df58fc94
RS
17852 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17853 {
17854 offsetT old_var = fragp->fr_var;
17855 offsetT new_var = 4;
17856
17857 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17858 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17859 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17860 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17861 fragp->fr_var = new_var;
17862
17863 return new_var - old_var;
17864 }
17865
252b5132
RH
17866 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17867 return 0;
17868
88a7ef16 17869 if (mips16_extended_frag (fragp, sec, stretch))
252b5132
RH
17870 {
17871 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17872 return 0;
17873 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17874 return 2;
17875 }
17876 else
17877 {
17878 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17879 return 0;
17880 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17881 return -2;
17882 }
17883
17884 return 0;
17885}
17886
17887/* Convert a machine dependent frag. */
17888
17889void
17a2f251 17890md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17891{
4a6a3df4
AO
17892 if (RELAX_BRANCH_P (fragp->fr_subtype))
17893 {
4d68580a 17894 char *buf;
4a6a3df4
AO
17895 unsigned long insn;
17896 expressionS exp;
17897 fixS *fixp;
b34976b6 17898
4d68580a
RS
17899 buf = fragp->fr_literal + fragp->fr_fix;
17900 insn = read_insn (buf);
b34976b6 17901
4a6a3df4
AO
17902 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17903 {
17904 /* We generate a fixup instead of applying it right now
17905 because, if there are linker relaxations, we're going to
17906 need the relocations. */
17907 exp.X_op = O_symbol;
17908 exp.X_add_symbol = fragp->fr_symbol;
17909 exp.X_add_number = fragp->fr_offset;
17910
4d68580a
RS
17911 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17912 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
17913 fixp->fx_file = fragp->fr_file;
17914 fixp->fx_line = fragp->fr_line;
b34976b6 17915
4d68580a 17916 buf = write_insn (buf, insn);
4a6a3df4
AO
17917 }
17918 else
17919 {
17920 int i;
17921
17922 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 17923 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
17924
17925 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17926 goto uncond;
17927
17928 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17929 {
17930 /* Reverse the branch. */
17931 switch ((insn >> 28) & 0xf)
17932 {
17933 case 4:
56d438b1
CF
17934 if ((insn & 0xff000000) == 0x47000000
17935 || (insn & 0xff600000) == 0x45600000)
17936 {
17937 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
17938 reversed by tweaking bit 23. */
17939 insn ^= 0x00800000;
17940 }
17941 else
17942 {
17943 /* bc[0-3][tf]l? instructions can have the condition
17944 reversed by tweaking a single TF bit, and their
17945 opcodes all have 0x4???????. */
17946 gas_assert ((insn & 0xf3e00000) == 0x41000000);
17947 insn ^= 0x00010000;
17948 }
4a6a3df4
AO
17949 break;
17950
17951 case 0:
17952 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 17953 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 17954 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
17955 insn ^= 0x00010000;
17956 break;
b34976b6 17957
4a6a3df4
AO
17958 case 1:
17959 /* beq 0x10000000 bne 0x14000000
54f4ddb3 17960 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
17961 insn ^= 0x04000000;
17962 break;
17963
17964 default:
17965 abort ();
17966 }
17967 }
17968
17969 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17970 {
17971 /* Clear the and-link bit. */
9c2799c2 17972 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 17973
54f4ddb3
TS
17974 /* bltzal 0x04100000 bgezal 0x04110000
17975 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
17976 insn &= ~0x00100000;
17977 }
17978
17979 /* Branch over the branch (if the branch was likely) or the
17980 full jump (not likely case). Compute the offset from the
17981 current instruction to branch to. */
17982 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17983 i = 16;
17984 else
17985 {
17986 /* How many bytes in instructions we've already emitted? */
4d68580a 17987 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
17988 /* How many bytes in instructions from here to the end? */
17989 i = fragp->fr_var - i;
17990 }
17991 /* Convert to instruction count. */
17992 i >>= 2;
17993 /* Branch counts from the next instruction. */
b34976b6 17994 i--;
4a6a3df4
AO
17995 insn |= i;
17996 /* Branch over the jump. */
4d68580a 17997 buf = write_insn (buf, insn);
4a6a3df4 17998
54f4ddb3 17999 /* nop */
4d68580a 18000 buf = write_insn (buf, 0);
4a6a3df4
AO
18001
18002 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18003 {
18004 /* beql $0, $0, 2f */
18005 insn = 0x50000000;
18006 /* Compute the PC offset from the current instruction to
18007 the end of the variable frag. */
18008 /* How many bytes in instructions we've already emitted? */
4d68580a 18009 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18010 /* How many bytes in instructions from here to the end? */
18011 i = fragp->fr_var - i;
18012 /* Convert to instruction count. */
18013 i >>= 2;
18014 /* Don't decrement i, because we want to branch over the
18015 delay slot. */
4a6a3df4 18016 insn |= i;
4a6a3df4 18017
4d68580a
RS
18018 buf = write_insn (buf, insn);
18019 buf = write_insn (buf, 0);
4a6a3df4
AO
18020 }
18021
18022 uncond:
ce8ad872 18023 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18024 {
18025 /* j or jal. */
18026 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18027 ? 0x0c000000 : 0x08000000);
18028 exp.X_op = O_symbol;
18029 exp.X_add_symbol = fragp->fr_symbol;
18030 exp.X_add_number = fragp->fr_offset;
18031
4d68580a
RS
18032 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18033 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18034 fixp->fx_file = fragp->fr_file;
18035 fixp->fx_line = fragp->fr_line;
18036
4d68580a 18037 buf = write_insn (buf, insn);
4a6a3df4
AO
18038 }
18039 else
18040 {
66b3e8da
MR
18041 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18042
4a6a3df4 18043 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18044 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18045 insn |= at << OP_SH_RT;
4a6a3df4
AO
18046 exp.X_op = O_symbol;
18047 exp.X_add_symbol = fragp->fr_symbol;
18048 exp.X_add_number = fragp->fr_offset;
18049
18050 if (fragp->fr_offset)
18051 {
18052 exp.X_add_symbol = make_expr_symbol (&exp);
18053 exp.X_add_number = 0;
18054 }
18055
4d68580a
RS
18056 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18057 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18058 fixp->fx_file = fragp->fr_file;
18059 fixp->fx_line = fragp->fr_line;
18060
4d68580a 18061 buf = write_insn (buf, insn);
b34976b6 18062
4a6a3df4 18063 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18064 /* nop */
18065 buf = write_insn (buf, 0);
4a6a3df4
AO
18066
18067 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18068 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18069 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18070
4d68580a
RS
18071 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18072 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18073 fixp->fx_file = fragp->fr_file;
18074 fixp->fx_line = fragp->fr_line;
b34976b6 18075
4d68580a 18076 buf = write_insn (buf, insn);
4a6a3df4
AO
18077
18078 /* j(al)r $at. */
18079 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18080 insn = 0x0000f809;
4a6a3df4 18081 else
66b3e8da
MR
18082 insn = 0x00000008;
18083 insn |= at << OP_SH_RS;
4a6a3df4 18084
4d68580a 18085 buf = write_insn (buf, insn);
4a6a3df4
AO
18086 }
18087 }
18088
4a6a3df4 18089 fragp->fr_fix += fragp->fr_var;
4d68580a 18090 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18091 return;
18092 }
18093
df58fc94
RS
18094 /* Relax microMIPS branches. */
18095 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18096 {
4d68580a 18097 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18098 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18099 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18100 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18101 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18102 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18103 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18104 bfd_boolean short_ds;
df58fc94
RS
18105 unsigned long insn;
18106 expressionS exp;
18107 fixS *fixp;
18108
18109 exp.X_op = O_symbol;
18110 exp.X_add_symbol = fragp->fr_symbol;
18111 exp.X_add_number = fragp->fr_offset;
18112
18113 fragp->fr_fix += fragp->fr_var;
18114
18115 /* Handle 16-bit branches that fit or are forced to fit. */
18116 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18117 {
18118 /* We generate a fixup instead of applying it right now,
18119 because if there is linker relaxation, we're going to
18120 need the relocations. */
18121 if (type == 'D')
4d68580a 18122 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18123 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18124 else if (type == 'E')
4d68580a 18125 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18126 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18127 else
18128 abort ();
18129
18130 fixp->fx_file = fragp->fr_file;
18131 fixp->fx_line = fragp->fr_line;
18132
18133 /* These relocations can have an addend that won't fit in
18134 2 octets. */
18135 fixp->fx_no_overflow = 1;
18136
18137 return;
18138 }
18139
2309ddf2 18140 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18141 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18142 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18143 {
18144 /* We generate a fixup instead of applying it right now,
18145 because if there is linker relaxation, we're going to
18146 need the relocations. */
4d68580a
RS
18147 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18148 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18149 fixp->fx_file = fragp->fr_file;
18150 fixp->fx_line = fragp->fr_line;
18151
18152 if (type == 0)
7bd374a4
MR
18153 {
18154 insn = read_compressed_insn (buf, 4);
18155 buf += 4;
18156
18157 if (nods)
18158 {
18159 /* Check the short-delay-slot bit. */
18160 if (!al || (insn & 0x02000000) != 0)
18161 buf = write_compressed_insn (buf, 0x0c00, 2);
18162 else
18163 buf = write_compressed_insn (buf, 0x00000000, 4);
18164 }
18165
18166 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18167 return;
18168 }
df58fc94
RS
18169 }
18170
18171 /* Relax 16-bit branches to 32-bit branches. */
18172 if (type != 0)
18173 {
4d68580a 18174 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18175
18176 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18177 insn = 0x94000000; /* beq */
18178 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18179 {
18180 unsigned long regno;
18181
18182 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18183 regno = micromips_to_32_reg_d_map [regno];
18184 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18185 insn |= regno << MICROMIPSOP_SH_RS;
18186 }
18187 else
18188 abort ();
18189
18190 /* Nothing else to do, just write it out. */
18191 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18192 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18193 {
4d68580a 18194 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18195 if (nods)
18196 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18197 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18198 return;
18199 }
18200 }
18201 else
4d68580a 18202 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18203
18204 /* Relax 32-bit branches to a sequence of instructions. */
18205 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18206 _("relaxed out-of-range branch into a jump"));
df58fc94 18207
2309ddf2 18208 /* Set the short-delay-slot bit. */
7bd374a4 18209 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18210
18211 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18212 {
18213 symbolS *l;
18214
18215 /* Reverse the branch. */
18216 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18217 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18218 insn ^= 0x20000000;
18219 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18220 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18221 || (insn & 0xffe00000) == 0x40800000 /* blez */
18222 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18223 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18224 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18225 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18226 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18227 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18228 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18229 insn ^= 0x00400000;
18230 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18231 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18232 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18233 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18234 insn ^= 0x00200000;
56d438b1
CF
18235 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18236 BNZ.df */
18237 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18238 BNZ.V */
18239 insn ^= 0x00800000;
df58fc94
RS
18240 else
18241 abort ();
18242
18243 if (al)
18244 {
18245 /* Clear the and-link and short-delay-slot bits. */
18246 gas_assert ((insn & 0xfda00000) == 0x40200000);
18247
18248 /* bltzal 0x40200000 bgezal 0x40600000 */
18249 /* bltzals 0x42200000 bgezals 0x42600000 */
18250 insn &= ~0x02200000;
18251 }
18252
18253 /* Make a label at the end for use with the branch. */
18254 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18255 micromips_label_inc ();
f3ded42a 18256 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18257
18258 /* Refer to it. */
4d68580a
RS
18259 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18260 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18261 fixp->fx_file = fragp->fr_file;
18262 fixp->fx_line = fragp->fr_line;
18263
18264 /* Branch over the jump. */
4d68580a 18265 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18266
df58fc94 18267 if (!compact)
8484fb75
MR
18268 {
18269 /* nop */
18270 if (insn32)
18271 buf = write_compressed_insn (buf, 0x00000000, 4);
18272 else
18273 buf = write_compressed_insn (buf, 0x0c00, 2);
18274 }
df58fc94
RS
18275 }
18276
ce8ad872 18277 if (!pic)
df58fc94 18278 {
7bd374a4
MR
18279 unsigned long jal = (short_ds || nods
18280 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18281
df58fc94
RS
18282 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18283 insn = al ? jal : 0xd4000000;
18284
4d68580a
RS
18285 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18286 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18287 fixp->fx_file = fragp->fr_file;
18288 fixp->fx_line = fragp->fr_line;
18289
4d68580a 18290 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18291
7bd374a4 18292 if (compact || nods)
8484fb75
MR
18293 {
18294 /* nop */
18295 if (insn32)
18296 buf = write_compressed_insn (buf, 0x00000000, 4);
18297 else
18298 buf = write_compressed_insn (buf, 0x0c00, 2);
18299 }
df58fc94
RS
18300 }
18301 else
18302 {
18303 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18304
18305 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18306 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18307 insn |= at << MICROMIPSOP_SH_RT;
18308
18309 if (exp.X_add_number)
18310 {
18311 exp.X_add_symbol = make_expr_symbol (&exp);
18312 exp.X_add_number = 0;
18313 }
18314
4d68580a
RS
18315 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18316 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18317 fixp->fx_file = fragp->fr_file;
18318 fixp->fx_line = fragp->fr_line;
18319
4d68580a 18320 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18321
18322 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18323 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18324 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18325
4d68580a
RS
18326 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18327 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18328 fixp->fx_file = fragp->fr_file;
18329 fixp->fx_line = fragp->fr_line;
18330
4d68580a 18331 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18332
8484fb75
MR
18333 if (insn32)
18334 {
18335 /* jr/jalr $at */
18336 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18337 insn |= at << MICROMIPSOP_SH_RS;
18338
18339 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18340
7bd374a4 18341 if (compact || nods)
8484fb75
MR
18342 /* nop */
18343 buf = write_compressed_insn (buf, 0x00000000, 4);
18344 }
18345 else
18346 {
18347 /* jr/jrc/jalr/jalrs $at */
18348 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18349 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18350
18351 insn = al ? jalr : jr;
18352 insn |= at << MICROMIPSOP_SH_MJ;
18353
18354 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18355 if (al && nods)
18356 {
18357 /* nop */
18358 if (short_ds)
18359 buf = write_compressed_insn (buf, 0x0c00, 2);
18360 else
18361 buf = write_compressed_insn (buf, 0x00000000, 4);
18362 }
8484fb75 18363 }
df58fc94
RS
18364 }
18365
4d68580a 18366 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18367 return;
18368 }
18369
252b5132
RH
18370 if (RELAX_MIPS16_P (fragp->fr_subtype))
18371 {
18372 int type;
3ccad066 18373 const struct mips_int_operand *operand;
252b5132 18374 offsetT val;
5c04167a
RS
18375 char *buf;
18376 unsigned int user_length, length;
9d862524 18377 bfd_boolean need_reloc;
252b5132 18378 unsigned long insn;
5c04167a 18379 bfd_boolean ext;
88a7ef16 18380 segT symsec;
252b5132
RH
18381
18382 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18383 operand = mips16_immed_operand (type, FALSE);
252b5132 18384
5c04167a 18385 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18386 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18387
18388 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18389 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
18390 || (operand->root.type == OP_PCREL
18391 ? asec != symsec
18392 : !bfd_is_abs_section (symsec)));
18393
3ccad066 18394 if (operand->root.type == OP_PCREL)
252b5132 18395 {
3ccad066 18396 const struct mips_pcrel_operand *pcrel_op;
252b5132 18397
3ccad066 18398 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18399
14f72d45 18400 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18401 {
14f72d45
MR
18402 if (!ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18403 as_bad_where (fragp->fr_file, fragp->fr_line,
18404 _("branch to a symbol in another ISA mode"));
18405 else if ((fragp->fr_offset & 0x1) != 0)
18406 as_bad_where (fragp->fr_file, fragp->fr_line,
18407 _("branch to misaligned address (0x%lx)"),
18408 (long) val);
252b5132 18409 }
252b5132 18410
14f72d45 18411 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18412
18413 /* Make sure the section winds up with the alignment we have
18414 assumed. */
3ccad066
RS
18415 if (operand->shift > 0)
18416 record_alignment (asec, operand->shift);
252b5132
RH
18417 }
18418
18419 if (ext
18420 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18421 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
18422 as_warn_where (fragp->fr_file, fragp->fr_line,
18423 _("extended instruction in delay slot"));
18424
5c04167a 18425 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18426
4d68580a 18427 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18428 if (ext)
18429 insn |= MIPS16_EXTEND;
252b5132 18430
5c04167a
RS
18431 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18432 user_length = 4;
18433 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18434 user_length = 2;
18435 else
18436 user_length = 0;
18437
9d862524 18438 if (need_reloc)
c9775dde
MR
18439 {
18440 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18441 expressionS exp;
18442 fixS *fixp;
18443
18444 switch (type)
18445 {
18446 case 'p':
18447 case 'q':
18448 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18449 break;
18450 default:
18451 as_bad_where (fragp->fr_file, fragp->fr_line,
18452 _("unsupported relocation"));
18453 break;
18454 }
eefc3365
MR
18455 if (reloc == BFD_RELOC_NONE)
18456 ;
18457 else if (ext)
c9775dde 18458 {
c9775dde
MR
18459 exp.X_op = O_symbol;
18460 exp.X_add_symbol = fragp->fr_symbol;
18461 exp.X_add_number = fragp->fr_offset;
18462
adc1273c 18463 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
c9775dde
MR
18464 TRUE, reloc);
18465
18466 fixp->fx_file = fragp->fr_file;
18467 fixp->fx_line = fragp->fr_line;
c9775dde 18468 }
eefc3365
MR
18469 else
18470 as_bad_where (fragp->fr_file, fragp->fr_line,
18471 _("invalid unextended operand value"));
c9775dde 18472 }
88a7ef16
MR
18473 else
18474 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18475 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18476
5c04167a
RS
18477 length = (ext ? 4 : 2);
18478 gas_assert (mips16_opcode_length (insn) == length);
18479 write_compressed_insn (buf, insn, length);
18480 fragp->fr_fix += length;
252b5132
RH
18481 }
18482 else
18483 {
df58fc94
RS
18484 relax_substateT subtype = fragp->fr_subtype;
18485 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18486 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18487 int first, second;
18488 fixS *fixp;
252b5132 18489
df58fc94
RS
18490 first = RELAX_FIRST (subtype);
18491 second = RELAX_SECOND (subtype);
4d7206a2 18492 fixp = (fixS *) fragp->fr_opcode;
252b5132 18493
df58fc94
RS
18494 /* If the delay slot chosen does not match the size of the instruction,
18495 then emit a warning. */
18496 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18497 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18498 {
18499 relax_substateT s;
18500 const char *msg;
18501
18502 s = subtype & (RELAX_DELAY_SLOT_16BIT
18503 | RELAX_DELAY_SLOT_SIZE_FIRST
18504 | RELAX_DELAY_SLOT_SIZE_SECOND);
18505 msg = macro_warning (s);
18506 if (msg != NULL)
db9b2be4 18507 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18508 subtype &= ~s;
18509 }
18510
584892a6 18511 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18512 if (use_second == second_longer)
584892a6 18513 {
df58fc94
RS
18514 relax_substateT s;
18515 const char *msg;
18516
18517 s = (subtype
18518 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18519 msg = macro_warning (s);
18520 if (msg != NULL)
db9b2be4 18521 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18522 subtype &= ~s;
584892a6
RS
18523 }
18524
4d7206a2
RS
18525 /* Go through all the fixups for the first sequence. Disable them
18526 (by marking them as done) if we're going to use the second
18527 sequence instead. */
18528 while (fixp
18529 && fixp->fx_frag == fragp
18530 && fixp->fx_where < fragp->fr_fix - second)
18531 {
df58fc94 18532 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18533 fixp->fx_done = 1;
18534 fixp = fixp->fx_next;
18535 }
252b5132 18536
4d7206a2
RS
18537 /* Go through the fixups for the second sequence. Disable them if
18538 we're going to use the first sequence, otherwise adjust their
18539 addresses to account for the relaxation. */
18540 while (fixp && fixp->fx_frag == fragp)
18541 {
df58fc94 18542 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18543 fixp->fx_where -= first;
18544 else
18545 fixp->fx_done = 1;
18546 fixp = fixp->fx_next;
18547 }
18548
18549 /* Now modify the frag contents. */
df58fc94 18550 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18551 {
18552 char *start;
18553
18554 start = fragp->fr_literal + fragp->fr_fix - first - second;
18555 memmove (start, start + first, second);
18556 fragp->fr_fix -= first;
18557 }
18558 else
18559 fragp->fr_fix -= second;
252b5132
RH
18560 }
18561}
18562
252b5132
RH
18563/* This function is called after the relocs have been generated.
18564 We've been storing mips16 text labels as odd. Here we convert them
18565 back to even for the convenience of the debugger. */
18566
18567void
17a2f251 18568mips_frob_file_after_relocs (void)
252b5132
RH
18569{
18570 asymbol **syms;
18571 unsigned int count, i;
18572
252b5132
RH
18573 syms = bfd_get_outsymbols (stdoutput);
18574 count = bfd_get_symcount (stdoutput);
18575 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18576 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18577 && ((*syms)->value & 1) != 0)
18578 {
18579 (*syms)->value &= ~1;
18580 /* If the symbol has an odd size, it was probably computed
18581 incorrectly, so adjust that as well. */
18582 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18583 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18584 }
252b5132
RH
18585}
18586
a1facbec
MR
18587/* This function is called whenever a label is defined, including fake
18588 labels instantiated off the dot special symbol. It is used when
18589 handling branch delays; if a branch has a label, we assume we cannot
18590 move it. This also bumps the value of the symbol by 1 in compressed
18591 code. */
252b5132 18592
e1b47bd5 18593static void
a1facbec 18594mips_record_label (symbolS *sym)
252b5132 18595{
a8dbcb85 18596 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18597 struct insn_label_list *l;
18598
18599 if (free_insn_labels == NULL)
325801bd 18600 l = XNEW (struct insn_label_list);
252b5132
RH
18601 else
18602 {
18603 l = free_insn_labels;
18604 free_insn_labels = l->next;
18605 }
18606
18607 l->label = sym;
a8dbcb85
TS
18608 l->next = si->label_list;
18609 si->label_list = l;
a1facbec 18610}
07a53e5c 18611
a1facbec
MR
18612/* This function is called as tc_frob_label() whenever a label is defined
18613 and adds a DWARF-2 record we only want for true labels. */
18614
18615void
18616mips_define_label (symbolS *sym)
18617{
18618 mips_record_label (sym);
07a53e5c 18619 dwarf2_emit_label (sym);
252b5132 18620}
e1b47bd5
RS
18621
18622/* This function is called by tc_new_dot_label whenever a new dot symbol
18623 is defined. */
18624
18625void
18626mips_add_dot_label (symbolS *sym)
18627{
18628 mips_record_label (sym);
18629 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18630 mips_compressed_mark_label (sym);
18631}
252b5132 18632\f
351cdf24
MF
18633/* Converting ASE flags from internal to .MIPS.abiflags values. */
18634static unsigned int
18635mips_convert_ase_flags (int ase)
18636{
18637 unsigned int ext_ases = 0;
18638
18639 if (ase & ASE_DSP)
18640 ext_ases |= AFL_ASE_DSP;
18641 if (ase & ASE_DSPR2)
18642 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
18643 if (ase & ASE_DSPR3)
18644 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
18645 if (ase & ASE_EVA)
18646 ext_ases |= AFL_ASE_EVA;
18647 if (ase & ASE_MCU)
18648 ext_ases |= AFL_ASE_MCU;
18649 if (ase & ASE_MDMX)
18650 ext_ases |= AFL_ASE_MDMX;
18651 if (ase & ASE_MIPS3D)
18652 ext_ases |= AFL_ASE_MIPS3D;
18653 if (ase & ASE_MT)
18654 ext_ases |= AFL_ASE_MT;
18655 if (ase & ASE_SMARTMIPS)
18656 ext_ases |= AFL_ASE_SMARTMIPS;
18657 if (ase & ASE_VIRT)
18658 ext_ases |= AFL_ASE_VIRT;
18659 if (ase & ASE_MSA)
18660 ext_ases |= AFL_ASE_MSA;
18661 if (ase & ASE_XPA)
18662 ext_ases |= AFL_ASE_XPA;
18663
18664 return ext_ases;
18665}
252b5132
RH
18666/* Some special processing for a MIPS ELF file. */
18667
18668void
17a2f251 18669mips_elf_final_processing (void)
252b5132 18670{
351cdf24
MF
18671 int fpabi;
18672 Elf_Internal_ABIFlags_v0 flags;
18673
18674 flags.version = 0;
18675 flags.isa_rev = 0;
18676 switch (file_mips_opts.isa)
18677 {
18678 case INSN_ISA1:
18679 flags.isa_level = 1;
18680 break;
18681 case INSN_ISA2:
18682 flags.isa_level = 2;
18683 break;
18684 case INSN_ISA3:
18685 flags.isa_level = 3;
18686 break;
18687 case INSN_ISA4:
18688 flags.isa_level = 4;
18689 break;
18690 case INSN_ISA5:
18691 flags.isa_level = 5;
18692 break;
18693 case INSN_ISA32:
18694 flags.isa_level = 32;
18695 flags.isa_rev = 1;
18696 break;
18697 case INSN_ISA32R2:
18698 flags.isa_level = 32;
18699 flags.isa_rev = 2;
18700 break;
18701 case INSN_ISA32R3:
18702 flags.isa_level = 32;
18703 flags.isa_rev = 3;
18704 break;
18705 case INSN_ISA32R5:
18706 flags.isa_level = 32;
18707 flags.isa_rev = 5;
18708 break;
09c14161
MF
18709 case INSN_ISA32R6:
18710 flags.isa_level = 32;
18711 flags.isa_rev = 6;
18712 break;
351cdf24
MF
18713 case INSN_ISA64:
18714 flags.isa_level = 64;
18715 flags.isa_rev = 1;
18716 break;
18717 case INSN_ISA64R2:
18718 flags.isa_level = 64;
18719 flags.isa_rev = 2;
18720 break;
18721 case INSN_ISA64R3:
18722 flags.isa_level = 64;
18723 flags.isa_rev = 3;
18724 break;
18725 case INSN_ISA64R5:
18726 flags.isa_level = 64;
18727 flags.isa_rev = 5;
18728 break;
09c14161
MF
18729 case INSN_ISA64R6:
18730 flags.isa_level = 64;
18731 flags.isa_rev = 6;
18732 break;
351cdf24
MF
18733 }
18734
18735 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18736 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18737 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18738 : (file_mips_opts.fp == 64) ? AFL_REG_64
18739 : AFL_REG_32;
18740 flags.cpr2_size = AFL_REG_NONE;
18741 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18742 Tag_GNU_MIPS_ABI_FP);
18743 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18744 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18745 if (file_ase_mips16)
18746 flags.ases |= AFL_ASE_MIPS16;
18747 if (file_ase_micromips)
18748 flags.ases |= AFL_ASE_MICROMIPS;
18749 flags.flags1 = 0;
18750 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18751 || file_mips_opts.fp == 64)
18752 && file_mips_opts.oddspreg)
18753 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18754 flags.flags2 = 0;
18755
18756 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18757 ((Elf_External_ABIFlags_v0 *)
18758 mips_flags_frag));
18759
252b5132 18760 /* Write out the register information. */
316f5878 18761 if (mips_abi != N64_ABI)
252b5132
RH
18762 {
18763 Elf32_RegInfo s;
18764
18765 s.ri_gprmask = mips_gprmask;
18766 s.ri_cprmask[0] = mips_cprmask[0];
18767 s.ri_cprmask[1] = mips_cprmask[1];
18768 s.ri_cprmask[2] = mips_cprmask[2];
18769 s.ri_cprmask[3] = mips_cprmask[3];
18770 /* The gp_value field is set by the MIPS ELF backend. */
18771
18772 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18773 ((Elf32_External_RegInfo *)
18774 mips_regmask_frag));
18775 }
18776 else
18777 {
18778 Elf64_Internal_RegInfo s;
18779
18780 s.ri_gprmask = mips_gprmask;
18781 s.ri_pad = 0;
18782 s.ri_cprmask[0] = mips_cprmask[0];
18783 s.ri_cprmask[1] = mips_cprmask[1];
18784 s.ri_cprmask[2] = mips_cprmask[2];
18785 s.ri_cprmask[3] = mips_cprmask[3];
18786 /* The gp_value field is set by the MIPS ELF backend. */
18787
18788 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18789 ((Elf64_External_RegInfo *)
18790 mips_regmask_frag));
18791 }
18792
18793 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18794 sort of BFD interface for this. */
18795 if (mips_any_noreorder)
18796 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18797 if (mips_pic != NO_PIC)
143d77c5 18798 {
8b828383 18799 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18800 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18801 }
18802 if (mips_abicalls)
18803 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18804
b015e599
AP
18805 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18806 defined at present; this might need to change in future. */
a4672219
TS
18807 if (file_ase_mips16)
18808 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18809 if (file_ase_micromips)
18810 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 18811 if (file_mips_opts.ase & ASE_MDMX)
deec1734 18812 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18813
bdaaa2e1 18814 /* Set the MIPS ELF ABI flags. */
316f5878 18815 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18816 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18817 else if (mips_abi == O64_ABI)
252b5132 18818 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18819 else if (mips_abi == EABI_ABI)
252b5132 18820 {
bad1aba3 18821 if (file_mips_opts.gp == 64)
252b5132
RH
18822 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18823 else
18824 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18825 }
316f5878 18826 else if (mips_abi == N32_ABI)
be00bddd
TS
18827 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18828
c9914766 18829 /* Nothing to do for N64_ABI. */
252b5132
RH
18830
18831 if (mips_32bitmode)
18832 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 18833
7361da2c 18834 if (mips_nan2008 == 1)
ba92f887
MR
18835 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
18836
ad3fea08 18837 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
18838 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18839 Tag_GNU_MIPS_ABI_FP);
18840 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 18841 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 18842}
252b5132 18843\f
beae10d5 18844typedef struct proc {
9b2f1d35
EC
18845 symbolS *func_sym;
18846 symbolS *func_end_sym;
beae10d5
KH
18847 unsigned long reg_mask;
18848 unsigned long reg_offset;
18849 unsigned long fpreg_mask;
18850 unsigned long fpreg_offset;
18851 unsigned long frame_offset;
18852 unsigned long frame_reg;
18853 unsigned long pc_reg;
18854} procS;
252b5132
RH
18855
18856static procS cur_proc;
18857static procS *cur_proc_ptr;
18858static int numprocs;
18859
df58fc94
RS
18860/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18861 as "2", and a normal nop as "0". */
18862
18863#define NOP_OPCODE_MIPS 0
18864#define NOP_OPCODE_MIPS16 1
18865#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18866
18867char
18868mips_nop_opcode (void)
18869{
df58fc94
RS
18870 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18871 return NOP_OPCODE_MICROMIPS;
18872 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18873 return NOP_OPCODE_MIPS16;
18874 else
18875 return NOP_OPCODE_MIPS;
742a56fe
RS
18876}
18877
df58fc94
RS
18878/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18879 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18880
0a9ef439 18881void
17a2f251 18882mips_handle_align (fragS *fragp)
a19d8eb0 18883{
df58fc94 18884 char nop_opcode;
742a56fe 18885 char *p;
c67a084a
NC
18886 int bytes, size, excess;
18887 valueT opcode;
742a56fe 18888
0a9ef439
RH
18889 if (fragp->fr_type != rs_align_code)
18890 return;
18891
742a56fe 18892 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18893 nop_opcode = *p;
18894 switch (nop_opcode)
a19d8eb0 18895 {
df58fc94
RS
18896 case NOP_OPCODE_MICROMIPS:
18897 opcode = micromips_nop32_insn.insn_opcode;
18898 size = 4;
18899 break;
18900 case NOP_OPCODE_MIPS16:
c67a084a
NC
18901 opcode = mips16_nop_insn.insn_opcode;
18902 size = 2;
df58fc94
RS
18903 break;
18904 case NOP_OPCODE_MIPS:
18905 default:
c67a084a
NC
18906 opcode = nop_insn.insn_opcode;
18907 size = 4;
df58fc94 18908 break;
c67a084a 18909 }
a19d8eb0 18910
c67a084a
NC
18911 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18912 excess = bytes % size;
df58fc94
RS
18913
18914 /* Handle the leading part if we're not inserting a whole number of
18915 instructions, and make it the end of the fixed part of the frag.
18916 Try to fit in a short microMIPS NOP if applicable and possible,
18917 and use zeroes otherwise. */
18918 gas_assert (excess < 4);
18919 fragp->fr_fix += excess;
18920 switch (excess)
c67a084a 18921 {
df58fc94
RS
18922 case 3:
18923 *p++ = '\0';
18924 /* Fall through. */
18925 case 2:
833794fc 18926 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 18927 {
4d68580a 18928 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18929 break;
18930 }
18931 *p++ = '\0';
18932 /* Fall through. */
18933 case 1:
18934 *p++ = '\0';
18935 /* Fall through. */
18936 case 0:
18937 break;
a19d8eb0 18938 }
c67a084a
NC
18939
18940 md_number_to_chars (p, opcode, size);
18941 fragp->fr_var = size;
a19d8eb0
CP
18942}
18943
252b5132 18944static long
17a2f251 18945get_number (void)
252b5132
RH
18946{
18947 int negative = 0;
18948 long val = 0;
18949
18950 if (*input_line_pointer == '-')
18951 {
18952 ++input_line_pointer;
18953 negative = 1;
18954 }
3882b010 18955 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18956 as_bad (_("expected simple number"));
252b5132
RH
18957 if (input_line_pointer[0] == '0')
18958 {
18959 if (input_line_pointer[1] == 'x')
18960 {
18961 input_line_pointer += 2;
3882b010 18962 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18963 {
18964 val <<= 4;
18965 val |= hex_value (*input_line_pointer++);
18966 }
18967 return negative ? -val : val;
18968 }
18969 else
18970 {
18971 ++input_line_pointer;
3882b010 18972 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18973 {
18974 val <<= 3;
18975 val |= *input_line_pointer++ - '0';
18976 }
18977 return negative ? -val : val;
18978 }
18979 }
3882b010 18980 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18981 {
18982 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18983 *input_line_pointer, *input_line_pointer);
956cd1d6 18984 as_warn (_("invalid number"));
252b5132
RH
18985 return -1;
18986 }
3882b010 18987 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18988 {
18989 val *= 10;
18990 val += *input_line_pointer++ - '0';
18991 }
18992 return negative ? -val : val;
18993}
18994
18995/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18996 is an initial number which is the ECOFF file index. In the non-ECOFF
18997 case .file implies DWARF-2. */
18998
18999static void
17a2f251 19000s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19001{
ecb4347a
DJ
19002 static int first_file_directive = 0;
19003
c5dd6aab
DJ
19004 if (ECOFF_DEBUGGING)
19005 {
19006 get_number ();
19007 s_app_file (0);
19008 }
19009 else
ecb4347a
DJ
19010 {
19011 char *filename;
19012
19013 filename = dwarf2_directive_file (0);
19014
19015 /* Versions of GCC up to 3.1 start files with a ".file"
19016 directive even for stabs output. Make sure that this
19017 ".file" is handled. Note that you need a version of GCC
19018 after 3.1 in order to support DWARF-2 on MIPS. */
19019 if (filename != NULL && ! first_file_directive)
19020 {
19021 (void) new_logical_line (filename, -1);
c04f5787 19022 s_app_file_string (filename, 0);
ecb4347a
DJ
19023 }
19024 first_file_directive = 1;
19025 }
c5dd6aab
DJ
19026}
19027
19028/* The .loc directive, implying DWARF-2. */
252b5132
RH
19029
19030static void
17a2f251 19031s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19032{
c5dd6aab
DJ
19033 if (!ECOFF_DEBUGGING)
19034 dwarf2_directive_loc (0);
252b5132
RH
19035}
19036
252b5132
RH
19037/* The .end directive. */
19038
19039static void
17a2f251 19040s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19041{
19042 symbolS *p;
252b5132 19043
7a621144
DJ
19044 /* Following functions need their own .frame and .cprestore directives. */
19045 mips_frame_reg_valid = 0;
19046 mips_cprestore_valid = 0;
19047
252b5132
RH
19048 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19049 {
19050 p = get_symbol ();
19051 demand_empty_rest_of_line ();
19052 }
19053 else
19054 p = NULL;
19055
14949570 19056 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19057 as_warn (_(".end not in text section"));
19058
19059 if (!cur_proc_ptr)
19060 {
1661c76c 19061 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19062 demand_empty_rest_of_line ();
19063 return;
19064 }
19065
19066 if (p != NULL)
19067 {
9c2799c2 19068 gas_assert (S_GET_NAME (p));
9b2f1d35 19069 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19070 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19071
19072 if (debug_type == DEBUG_STABS)
19073 stabs_generate_asm_endfunc (S_GET_NAME (p),
19074 S_GET_NAME (p));
252b5132
RH
19075 }
19076 else
19077 as_warn (_(".end directive missing or unknown symbol"));
19078
9b2f1d35
EC
19079 /* Create an expression to calculate the size of the function. */
19080 if (p && cur_proc_ptr)
19081 {
19082 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19083 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19084
19085 obj->size = exp;
19086 exp->X_op = O_subtract;
19087 exp->X_add_symbol = symbol_temp_new_now ();
19088 exp->X_op_symbol = p;
19089 exp->X_add_number = 0;
19090
19091 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19092 }
19093
5ff6a06c
MR
19094#ifdef md_flush_pending_output
19095 md_flush_pending_output ();
19096#endif
19097
ecb4347a 19098 /* Generate a .pdr section. */
f3ded42a 19099 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19100 {
19101 segT saved_seg = now_seg;
19102 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19103 expressionS exp;
19104 char *fragp;
252b5132 19105
9c2799c2 19106 gas_assert (pdr_seg);
ecb4347a 19107 subseg_set (pdr_seg, 0);
252b5132 19108
ecb4347a
DJ
19109 /* Write the symbol. */
19110 exp.X_op = O_symbol;
19111 exp.X_add_symbol = p;
19112 exp.X_add_number = 0;
19113 emit_expr (&exp, 4);
252b5132 19114
ecb4347a 19115 fragp = frag_more (7 * 4);
252b5132 19116
17a2f251
TS
19117 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19118 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19119 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19120 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19121 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19122 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19123 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19124
ecb4347a
DJ
19125 subseg_set (saved_seg, saved_subseg);
19126 }
252b5132
RH
19127
19128 cur_proc_ptr = NULL;
19129}
19130
19131/* The .aent and .ent directives. */
19132
19133static void
17a2f251 19134s_mips_ent (int aent)
252b5132 19135{
252b5132 19136 symbolS *symbolP;
252b5132
RH
19137
19138 symbolP = get_symbol ();
19139 if (*input_line_pointer == ',')
f9419b05 19140 ++input_line_pointer;
252b5132 19141 SKIP_WHITESPACE ();
3882b010 19142 if (ISDIGIT (*input_line_pointer)
d9a62219 19143 || *input_line_pointer == '-')
874e8986 19144 get_number ();
252b5132 19145
14949570 19146 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19147 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19148
19149 if (!aent && cur_proc_ptr)
9a41af64 19150 as_warn (_("missing .end"));
252b5132
RH
19151
19152 if (!aent)
19153 {
7a621144
DJ
19154 /* This function needs its own .frame and .cprestore directives. */
19155 mips_frame_reg_valid = 0;
19156 mips_cprestore_valid = 0;
19157
252b5132
RH
19158 cur_proc_ptr = &cur_proc;
19159 memset (cur_proc_ptr, '\0', sizeof (procS));
19160
9b2f1d35 19161 cur_proc_ptr->func_sym = symbolP;
252b5132 19162
f9419b05 19163 ++numprocs;
ecb4347a
DJ
19164
19165 if (debug_type == DEBUG_STABS)
19166 stabs_generate_asm_func (S_GET_NAME (symbolP),
19167 S_GET_NAME (symbolP));
252b5132
RH
19168 }
19169
7c0fc524
MR
19170 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19171
252b5132
RH
19172 demand_empty_rest_of_line ();
19173}
19174
19175/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19176 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19177 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19178 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19179 symbol table (in the mdebug section). */
19180
19181static void
17a2f251 19182s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19183{
f3ded42a
RS
19184 if (ECOFF_DEBUGGING)
19185 s_ignore (ignore);
19186 else
ecb4347a
DJ
19187 {
19188 long val;
252b5132 19189
ecb4347a
DJ
19190 if (cur_proc_ptr == (procS *) NULL)
19191 {
19192 as_warn (_(".frame outside of .ent"));
19193 demand_empty_rest_of_line ();
19194 return;
19195 }
252b5132 19196
ecb4347a
DJ
19197 cur_proc_ptr->frame_reg = tc_get_register (1);
19198
19199 SKIP_WHITESPACE ();
19200 if (*input_line_pointer++ != ','
19201 || get_absolute_expression_and_terminator (&val) != ',')
19202 {
1661c76c 19203 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19204 --input_line_pointer;
19205 demand_empty_rest_of_line ();
19206 return;
19207 }
252b5132 19208
ecb4347a
DJ
19209 cur_proc_ptr->frame_offset = val;
19210 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19211
252b5132 19212 demand_empty_rest_of_line ();
252b5132 19213 }
252b5132
RH
19214}
19215
bdaaa2e1
KH
19216/* The .fmask and .mask directives. If the mdebug section is present
19217 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19218 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19219 information correctly. We can't use the ecoff routines because they
252b5132
RH
19220 make reference to the ecoff symbol table (in the mdebug section). */
19221
19222static void
17a2f251 19223s_mips_mask (int reg_type)
252b5132 19224{
f3ded42a
RS
19225 if (ECOFF_DEBUGGING)
19226 s_ignore (reg_type);
19227 else
252b5132 19228 {
ecb4347a 19229 long mask, off;
252b5132 19230
ecb4347a
DJ
19231 if (cur_proc_ptr == (procS *) NULL)
19232 {
19233 as_warn (_(".mask/.fmask outside of .ent"));
19234 demand_empty_rest_of_line ();
19235 return;
19236 }
252b5132 19237
ecb4347a
DJ
19238 if (get_absolute_expression_and_terminator (&mask) != ',')
19239 {
1661c76c 19240 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19241 --input_line_pointer;
19242 demand_empty_rest_of_line ();
19243 return;
19244 }
252b5132 19245
ecb4347a
DJ
19246 off = get_absolute_expression ();
19247
19248 if (reg_type == 'F')
19249 {
19250 cur_proc_ptr->fpreg_mask = mask;
19251 cur_proc_ptr->fpreg_offset = off;
19252 }
19253 else
19254 {
19255 cur_proc_ptr->reg_mask = mask;
19256 cur_proc_ptr->reg_offset = off;
19257 }
19258
19259 demand_empty_rest_of_line ();
252b5132 19260 }
252b5132
RH
19261}
19262
316f5878
RS
19263/* A table describing all the processors gas knows about. Names are
19264 matched in the order listed.
e7af610e 19265
316f5878
RS
19266 To ease comparison, please keep this table in the same order as
19267 gcc's mips_cpu_info_table[]. */
e972090a
NC
19268static const struct mips_cpu_info mips_cpu_info_table[] =
19269{
316f5878 19270 /* Entries for generic ISAs */
d16afab6
RS
19271 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19272 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19273 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19274 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19275 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19276 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19277 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19278 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19279 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19280 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19281 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19282 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19283 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19284 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19285 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19286
19287 /* MIPS I */
d16afab6
RS
19288 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19289 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19290 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19291
19292 /* MIPS II */
d16afab6 19293 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19294
19295 /* MIPS III */
d16afab6
RS
19296 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19297 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19298 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19299 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19300 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19301 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19302 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19303 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19304 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19305 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19306 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19307 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19308 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19309 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19310 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19311 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19312
19313 /* MIPS IV */
d16afab6
RS
19314 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19315 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19316 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19317 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19318 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19319 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19320 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19321 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19322 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19323 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19324 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19325 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19326 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19327 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19328 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19329
19330 /* MIPS 32 */
d16afab6
RS
19331 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19332 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19333 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19334 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19335
19336 /* MIPS 32 Release 2 */
d16afab6
RS
19337 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19338 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19339 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19340 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19341 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19342 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19343 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19344 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19345 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19346 ISA_MIPS32R2, CPU_MIPS32R2 },
19347 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19348 ISA_MIPS32R2, CPU_MIPS32R2 },
19349 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19350 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19351 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19352 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19353 /* Deprecated forms of the above. */
d16afab6
RS
19354 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19355 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19356 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19357 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19358 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19359 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19360 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19361 /* Deprecated forms of the above. */
d16afab6
RS
19362 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19363 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19364 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19365 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19366 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19367 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19368 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19369 /* Deprecated forms of the above. */
d16afab6
RS
19370 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19371 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19372 /* 34Kn is a 34kc without DSP. */
d16afab6 19373 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19374 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19375 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19376 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19377 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19378 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19379 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19380 /* Deprecated forms of the above. */
d16afab6
RS
19381 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19382 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19383 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19384 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19385 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19386 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19387 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19388 /* interaptiv is the new name for 1004kf */
19389 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
c6e5c03a
RS
19390 /* M5100 family */
19391 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19392 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19393 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19394 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19395
316f5878 19396 /* MIPS 64 */
d16afab6
RS
19397 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19398 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19399 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19400 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19401
c7a23324 19402 /* Broadcom SB-1 CPU core */
d16afab6 19403 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19404 /* Broadcom SB-1A CPU core */
d16afab6 19405 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19406
4ba154f5 19407 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 19408
ed163775
MR
19409 /* MIPS 64 Release 2 */
19410
967344c6 19411 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19412 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19413 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19414 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19415 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19416
52b6b6b9 19417 /* RMI Xlr */
d16afab6 19418 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19419
55a36193
MK
19420 /* Broadcom XLP.
19421 XLP is mostly like XLR, with the prominent exception that it is
19422 MIPS64R2 rather than MIPS64. */
d16afab6 19423 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19424
a4968f42 19425 /* MIPS 64 Release 6 */
7ef0d297 19426 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19427 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19428
316f5878 19429 /* End marker */
d16afab6 19430 { NULL, 0, 0, 0, 0 }
316f5878 19431};
e7af610e 19432
84ea6cf2 19433
316f5878
RS
19434/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19435 with a final "000" replaced by "k". Ignore case.
e7af610e 19436
316f5878 19437 Note: this function is shared between GCC and GAS. */
c6c98b38 19438
b34976b6 19439static bfd_boolean
17a2f251 19440mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19441{
19442 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19443 given++, canonical++;
19444
19445 return ((*given == 0 && *canonical == 0)
19446 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19447}
19448
19449
19450/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19451 CPU name. We've traditionally allowed a lot of variation here.
19452
19453 Note: this function is shared between GCC and GAS. */
19454
b34976b6 19455static bfd_boolean
17a2f251 19456mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19457{
19458 /* First see if the name matches exactly, or with a final "000"
19459 turned into "k". */
19460 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19461 return TRUE;
316f5878
RS
19462
19463 /* If not, try comparing based on numerical designation alone.
19464 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19465 if (TOLOWER (*given) == 'r')
19466 given++;
19467 if (!ISDIGIT (*given))
b34976b6 19468 return FALSE;
316f5878
RS
19469
19470 /* Skip over some well-known prefixes in the canonical name,
19471 hoping to find a number there too. */
19472 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19473 canonical += 2;
19474 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19475 canonical += 2;
19476 else if (TOLOWER (canonical[0]) == 'r')
19477 canonical += 1;
19478
19479 return mips_strict_matching_cpu_name_p (canonical, given);
19480}
19481
19482
19483/* Parse an option that takes the name of a processor as its argument.
19484 OPTION is the name of the option and CPU_STRING is the argument.
19485 Return the corresponding processor enumeration if the CPU_STRING is
19486 recognized, otherwise report an error and return null.
19487
19488 A similar function exists in GCC. */
e7af610e
NC
19489
19490static const struct mips_cpu_info *
17a2f251 19491mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19492{
316f5878 19493 const struct mips_cpu_info *p;
e7af610e 19494
316f5878
RS
19495 /* 'from-abi' selects the most compatible architecture for the given
19496 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19497 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19498 version. Look first at the -mgp options, if given, otherwise base
19499 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19500
316f5878
RS
19501 Treat NO_ABI like the EABIs. One reason to do this is that the
19502 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19503 architecture. This code picks MIPS I for 'mips' and MIPS III for
19504 'mips64', just as we did in the days before 'from-abi'. */
19505 if (strcasecmp (cpu_string, "from-abi") == 0)
19506 {
19507 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19508 return mips_cpu_info_from_isa (ISA_MIPS1);
19509
19510 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19511 return mips_cpu_info_from_isa (ISA_MIPS3);
19512
bad1aba3 19513 if (file_mips_opts.gp >= 0)
19514 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19515 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19516
19517 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19518 ? ISA_MIPS3
19519 : ISA_MIPS1);
19520 }
19521
19522 /* 'default' has traditionally been a no-op. Probably not very useful. */
19523 if (strcasecmp (cpu_string, "default") == 0)
19524 return 0;
19525
19526 for (p = mips_cpu_info_table; p->name != 0; p++)
19527 if (mips_matching_cpu_name_p (p->name, cpu_string))
19528 return p;
19529
1661c76c 19530 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19531 return 0;
e7af610e
NC
19532}
19533
316f5878
RS
19534/* Return the canonical processor information for ISA (a member of the
19535 ISA_MIPS* enumeration). */
19536
e7af610e 19537static const struct mips_cpu_info *
17a2f251 19538mips_cpu_info_from_isa (int isa)
e7af610e
NC
19539{
19540 int i;
19541
19542 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19543 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19544 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19545 return (&mips_cpu_info_table[i]);
19546
e972090a 19547 return NULL;
e7af610e 19548}
fef14a42
TS
19549
19550static const struct mips_cpu_info *
17a2f251 19551mips_cpu_info_from_arch (int arch)
fef14a42
TS
19552{
19553 int i;
19554
19555 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19556 if (arch == mips_cpu_info_table[i].cpu)
19557 return (&mips_cpu_info_table[i]);
19558
19559 return NULL;
19560}
316f5878
RS
19561\f
19562static void
17a2f251 19563show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19564{
19565 if (*first_p)
19566 {
19567 fprintf (stream, "%24s", "");
19568 *col_p = 24;
19569 }
19570 else
19571 {
19572 fprintf (stream, ", ");
19573 *col_p += 2;
19574 }
e7af610e 19575
316f5878
RS
19576 if (*col_p + strlen (string) > 72)
19577 {
19578 fprintf (stream, "\n%24s", "");
19579 *col_p = 24;
19580 }
19581
19582 fprintf (stream, "%s", string);
19583 *col_p += strlen (string);
19584
19585 *first_p = 0;
19586}
19587
19588void
17a2f251 19589md_show_usage (FILE *stream)
e7af610e 19590{
316f5878
RS
19591 int column, first;
19592 size_t i;
19593
19594 fprintf (stream, _("\
19595MIPS options:\n\
316f5878
RS
19596-EB generate big endian output\n\
19597-EL generate little endian output\n\
19598-g, -g2 do not remove unneeded NOPs or swap branches\n\
19599-G NUM allow referencing objects up to NUM bytes\n\
19600 implicitly with the gp register [default 8]\n"));
19601 fprintf (stream, _("\
19602-mips1 generate MIPS ISA I instructions\n\
19603-mips2 generate MIPS ISA II instructions\n\
19604-mips3 generate MIPS ISA III instructions\n\
19605-mips4 generate MIPS ISA IV instructions\n\
19606-mips5 generate MIPS ISA V instructions\n\
19607-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19608-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19609-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19610-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19611-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19612-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19613-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19614-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19615-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19616-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19617-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19618
19619 first = 1;
e7af610e
NC
19620
19621 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19622 show (stream, mips_cpu_info_table[i].name, &column, &first);
19623 show (stream, "from-abi", &column, &first);
19624 fputc ('\n', stream);
e7af610e 19625
316f5878
RS
19626 fprintf (stream, _("\
19627-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19628-no-mCPU don't generate code specific to CPU.\n\
19629 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19630
19631 first = 1;
19632
19633 show (stream, "3900", &column, &first);
19634 show (stream, "4010", &column, &first);
19635 show (stream, "4100", &column, &first);
19636 show (stream, "4650", &column, &first);
19637 fputc ('\n', stream);
19638
19639 fprintf (stream, _("\
19640-mips16 generate mips16 instructions\n\
19641-no-mips16 do not generate mips16 instructions\n"));
19642 fprintf (stream, _("\
df58fc94
RS
19643-mmicromips generate microMIPS instructions\n\
19644-mno-micromips do not generate microMIPS instructions\n"));
19645 fprintf (stream, _("\
e16bfa71 19646-msmartmips generate smartmips instructions\n\
3739860c 19647-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 19648 fprintf (stream, _("\
74cd071d
CF
19649-mdsp generate DSP instructions\n\
19650-mno-dsp do not generate DSP instructions\n"));
19651 fprintf (stream, _("\
8b082fb1
TS
19652-mdspr2 generate DSP R2 instructions\n\
19653-mno-dspr2 do not generate DSP R2 instructions\n"));
19654 fprintf (stream, _("\
8f4f9071
MF
19655-mdspr3 generate DSP R3 instructions\n\
19656-mno-dspr3 do not generate DSP R3 instructions\n"));
19657 fprintf (stream, _("\
ef2e4d86
CF
19658-mmt generate MT instructions\n\
19659-mno-mt do not generate MT instructions\n"));
19660 fprintf (stream, _("\
dec0624d
MR
19661-mmcu generate MCU instructions\n\
19662-mno-mcu do not generate MCU instructions\n"));
19663 fprintf (stream, _("\
56d438b1
CF
19664-mmsa generate MSA instructions\n\
19665-mno-msa do not generate MSA instructions\n"));
19666 fprintf (stream, _("\
7d64c587
AB
19667-mxpa generate eXtended Physical Address (XPA) instructions\n\
19668-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19669 fprintf (stream, _("\
b015e599
AP
19670-mvirt generate Virtualization instructions\n\
19671-mno-virt do not generate Virtualization instructions\n"));
19672 fprintf (stream, _("\
833794fc
MR
19673-minsn32 only generate 32-bit microMIPS instructions\n\
19674-mno-insn32 generate all microMIPS instructions\n"));
19675 fprintf (stream, _("\
c67a084a
NC
19676-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19677-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19678-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19679-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19680-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19681-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19682-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19683-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19684-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19685-O0 remove unneeded NOPs, do not swap branches\n\
19686-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19687--trap, --no-break trap exception on div by 0 and mult overflow\n\
19688--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19689 fprintf (stream, _("\
19690-mhard-float allow floating-point instructions\n\
19691-msoft-float do not allow floating-point instructions\n\
19692-msingle-float only allow 32-bit floating-point operations\n\
19693-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 19694--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 19695--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
19696-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
19697-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
19698-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19699
19700 first = 1;
19701
19702 show (stream, "legacy", &column, &first);
19703 show (stream, "2008", &column, &first);
19704
19705 fputc ('\n', stream);
19706
316f5878
RS
19707 fprintf (stream, _("\
19708-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19709-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19710-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19711-non_shared do not generate code that can operate with DSOs\n\
316f5878 19712-xgot assume a 32 bit GOT\n\
dcd410fe 19713-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19714-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19715 position dependent (non shared) code\n\
316f5878
RS
19716-mabi=ABI create ABI conformant object file for:\n"));
19717
19718 first = 1;
19719
19720 show (stream, "32", &column, &first);
19721 show (stream, "o64", &column, &first);
19722 show (stream, "n32", &column, &first);
19723 show (stream, "64", &column, &first);
19724 show (stream, "eabi", &column, &first);
19725
19726 fputc ('\n', stream);
19727
19728 fprintf (stream, _("\
19729-32 create o32 ABI object file (default)\n\
19730-n32 create n32 ABI object file\n\
19731-64 create 64 ABI object file\n"));
e7af610e 19732}
14e777e0 19733
1575952e 19734#ifdef TE_IRIX
14e777e0 19735enum dwarf2_format
413a266c 19736mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19737{
369943fe 19738 if (HAVE_64BIT_SYMBOLS)
1575952e 19739 return dwarf2_format_64bit_irix;
14e777e0
KB
19740 else
19741 return dwarf2_format_32bit;
19742}
1575952e 19743#endif
73369e65
EC
19744
19745int
19746mips_dwarf2_addr_size (void)
19747{
6b6b3450 19748 if (HAVE_64BIT_OBJECTS)
73369e65 19749 return 8;
73369e65
EC
19750 else
19751 return 4;
19752}
5862107c
EC
19753
19754/* Standard calling conventions leave the CFA at SP on entry. */
19755void
19756mips_cfi_frame_initial_instructions (void)
19757{
19758 cfi_add_CFA_def_cfa_register (SP);
19759}
19760
707bfff6
TS
19761int
19762tc_mips_regname_to_dw2regnum (char *regname)
19763{
19764 unsigned int regnum = -1;
19765 unsigned int reg;
19766
19767 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19768 regnum = reg;
19769
19770 return regnum;
19771}
263b2574 19772
19773/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19774 Given a symbolic attribute NAME, return the proper integer value.
19775 Returns -1 if the attribute is not known. */
19776
19777int
19778mips_convert_symbolic_attribute (const char *name)
19779{
19780 static const struct
19781 {
19782 const char * name;
19783 const int tag;
19784 }
19785 attribute_table[] =
19786 {
19787#define T(tag) {#tag, tag}
19788 T (Tag_GNU_MIPS_ABI_FP),
19789 T (Tag_GNU_MIPS_ABI_MSA),
19790#undef T
19791 };
19792 unsigned int i;
19793
19794 if (name == NULL)
19795 return -1;
19796
19797 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19798 if (streq (name, attribute_table[i].name))
19799 return attribute_table[i].tag;
19800
19801 return -1;
19802}
fd5c94ab
RS
19803
19804void
19805md_mips_end (void)
19806{
351cdf24
MF
19807 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19808
fd5c94ab
RS
19809 mips_emit_delays ();
19810 if (cur_proc_ptr)
19811 as_warn (_("missing .end at end of assembly"));
919731af 19812
19813 /* Just in case no code was emitted, do the consistency check. */
19814 file_mips_check_options ();
351cdf24
MF
19815
19816 /* Set a floating-point ABI if the user did not. */
19817 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
19818 {
19819 /* Perform consistency checks on the floating-point ABI. */
19820 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19821 Tag_GNU_MIPS_ABI_FP);
19822 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
19823 check_fpabi (fpabi);
19824 }
19825 else
19826 {
19827 /* Soft-float gets precedence over single-float, the two options should
19828 not be used together so this should not matter. */
19829 if (file_mips_opts.soft_float == 1)
19830 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
19831 /* Single-float gets precedence over all double_float cases. */
19832 else if (file_mips_opts.single_float == 1)
19833 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
19834 else
19835 {
19836 switch (file_mips_opts.fp)
19837 {
19838 case 32:
19839 if (file_mips_opts.gp == 32)
19840 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19841 break;
19842 case 0:
19843 fpabi = Val_GNU_MIPS_ABI_FP_XX;
19844 break;
19845 case 64:
19846 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
19847 fpabi = Val_GNU_MIPS_ABI_FP_64A;
19848 else if (file_mips_opts.gp == 32)
19849 fpabi = Val_GNU_MIPS_ABI_FP_64;
19850 else
19851 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19852 break;
19853 }
19854 }
19855
19856 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19857 Tag_GNU_MIPS_ABI_FP, fpabi);
19858 }
fd5c94ab 19859}
2f0c68f2
CM
19860
19861/* Returns the relocation type required for a particular CFI encoding. */
19862
19863bfd_reloc_code_real_type
19864mips_cfi_reloc_for_encoding (int encoding)
19865{
19866 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
19867 return BFD_RELOC_32_PCREL;
19868 else return BFD_RELOC_NONE;
19869}
This page took 3.440614 seconds and 4 git commands to generate.