[MIPS] Add Loongson 2K1000 proccessor support.
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
219d1afa 2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24 424 || (CPU) == CPU_R5900) \
bd782c07 425 && ((CPU) != CPU_GS464 \
9108bc33
CX
426 || (CPU) != CPU_GS464E \
427 || (CPU) != CPU_GS264E))
af7ee8bf 428
ad3fea08
TS
429/* Return true if ISA supports move to/from high part of a 64-bit
430 floating-point register. */
431#define ISA_HAS_MXHC1(ISA) \
432 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
433 || (ISA) == ISA_MIPS32R3 \
434 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
435 || (ISA) == ISA_MIPS32R6 \
436 || (ISA) == ISA_MIPS64R2 \
437 || (ISA) == ISA_MIPS64R3 \
438 || (ISA) == ISA_MIPS64R5 \
439 || (ISA) == ISA_MIPS64R6)
440
441/* Return true if ISA supports legacy NAN. */
442#define ISA_HAS_LEGACY_NAN(ISA) \
443 ((ISA) == ISA_MIPS1 \
444 || (ISA) == ISA_MIPS2 \
445 || (ISA) == ISA_MIPS3 \
446 || (ISA) == ISA_MIPS4 \
447 || (ISA) == ISA_MIPS5 \
448 || (ISA) == ISA_MIPS32 \
449 || (ISA) == ISA_MIPS32R2 \
450 || (ISA) == ISA_MIPS32R3 \
451 || (ISA) == ISA_MIPS32R5 \
452 || (ISA) == ISA_MIPS64 \
ae52f483
AB
453 || (ISA) == ISA_MIPS64R2 \
454 || (ISA) == ISA_MIPS64R3 \
455 || (ISA) == ISA_MIPS64R5)
ad3fea08 456
bad1aba3 457#define GPR_SIZE \
458 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
459 ? 32 \
460 : mips_opts.gp)
ca4e0257 461
bad1aba3 462#define FPR_SIZE \
463 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
464 ? 32 \
465 : mips_opts.fp)
ca4e0257 466
316f5878 467#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 468
316f5878 469#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 470
3b91255e
RS
471/* True if relocations are stored in-place. */
472#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
473
aed1a261
RS
474/* The ABI-derived address size. */
475#define HAVE_64BIT_ADDRESSES \
bad1aba3 476 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 477#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 478
aed1a261
RS
479/* The size of symbolic constants (i.e., expressions of the form
480 "SYMBOL" or "SYMBOL + OFFSET"). */
481#define HAVE_32BIT_SYMBOLS \
482 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
483#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 484
b7c7d6c1
TS
485/* Addresses are loaded in different ways, depending on the address size
486 in use. The n32 ABI Documentation also mandates the use of additions
487 with overflow checking, but existing implementations don't follow it. */
f899b4b8 488#define ADDRESS_ADD_INSN \
b7c7d6c1 489 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
490
491#define ADDRESS_ADDI_INSN \
b7c7d6c1 492 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
493
494#define ADDRESS_LOAD_INSN \
495 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
496
497#define ADDRESS_STORE_INSN \
498 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
499
a4672219 500/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
501#define CPU_HAS_MIPS16(cpu) \
502 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
503 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 504
2309ddf2 505/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
506#define CPU_HAS_MICROMIPS(cpu) 0
507
60b63b72
RS
508/* True if CPU has a dror instruction. */
509#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
510
511/* True if CPU has a ror instruction. */
512#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
513
dd6a37e7 514/* True if CPU is in the Octeon family */
2c629856
N
515#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
516 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 517
dd3cbb7e 518/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 519#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 520
0aa27725
RS
521/* True, if CPU has support for ldc1 and sdc1. */
522#define CPU_HAS_LDC1_SDC1(CPU) \
523 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
524
c8978940
CD
525/* True if mflo and mfhi can be immediately followed by instructions
526 which write to the HI and LO registers.
527
528 According to MIPS specifications, MIPS ISAs I, II, and III need
529 (at least) two instructions between the reads of HI/LO and
530 instructions which write them, and later ISAs do not. Contradicting
531 the MIPS specifications, some MIPS IV processor user manuals (e.g.
532 the UM for the NEC Vr5000) document needing the instructions between
533 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
534 MIPS64 and later ISAs to have the interlocks, plus any specific
535 earlier-ISA CPUs for which CPU documentation declares that the
536 instructions are really interlocked. */
537#define hilo_interlocks \
538 (mips_opts.isa == ISA_MIPS32 \
539 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
540 || mips_opts.isa == ISA_MIPS32R3 \
541 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 542 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
543 || mips_opts.isa == ISA_MIPS64 \
544 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
545 || mips_opts.isa == ISA_MIPS64R3 \
546 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 547 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 548 || mips_opts.arch == CPU_R4010 \
e407c74b 549 || mips_opts.arch == CPU_R5900 \
c8978940
CD
550 || mips_opts.arch == CPU_R10000 \
551 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
552 || mips_opts.arch == CPU_R14000 \
553 || mips_opts.arch == CPU_R16000 \
c8978940 554 || mips_opts.arch == CPU_RM7000 \
c8978940 555 || mips_opts.arch == CPU_VR5500 \
df58fc94 556 || mips_opts.micromips \
c8978940 557 )
252b5132
RH
558
559/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
560 from the GPRs after they are loaded from memory, and thus does not
561 require nops to be inserted. This applies to instructions marked
67dc82bc 562 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
563 level I and microMIPS mode instructions are always interlocked. */
564#define gpr_interlocks \
565 (mips_opts.isa != ISA_MIPS1 \
566 || mips_opts.arch == CPU_R3900 \
e407c74b 567 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
568 || mips_opts.micromips \
569 )
252b5132 570
81912461
ILT
571/* Whether the processor uses hardware interlocks to avoid delays
572 required by coprocessor instructions, and thus does not require
573 nops to be inserted. This applies to instructions marked
43885403
MF
574 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
575 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 576 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
577 levels I, II, and III and microMIPS mode instructions are always
578 interlocked. */
bdaaa2e1 579/* Itbl support may require additional care here. */
81912461
ILT
580#define cop_interlocks \
581 ((mips_opts.isa != ISA_MIPS1 \
582 && mips_opts.isa != ISA_MIPS2 \
583 && mips_opts.isa != ISA_MIPS3) \
584 || mips_opts.arch == CPU_R4300 \
df58fc94 585 || mips_opts.micromips \
81912461
ILT
586 )
587
588/* Whether the processor uses hardware interlocks to protect reads
589 from coprocessor registers after they are loaded from memory, and
590 thus does not require nops to be inserted. This applies to
591 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
592 requires at MIPS ISA level I and microMIPS mode instructions are
593 always interlocked. */
594#define cop_mem_interlocks \
595 (mips_opts.isa != ISA_MIPS1 \
596 || mips_opts.micromips \
597 )
252b5132 598
6b76fefe
CM
599/* Is this a mfhi or mflo instruction? */
600#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
601 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
602
df58fc94
RS
603/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
604 has been selected. This implies, in particular, that addresses of text
605 labels have their LSB set. */
606#define HAVE_CODE_COMPRESSION \
607 ((mips_opts.mips16 | mips_opts.micromips) != 0)
608
42429eac 609/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 610#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
611#define GPR_SMIN (-GPR_SMAX - 1)
612
252b5132
RH
613/* MIPS PIC level. */
614
a161fe53 615enum mips_pic_level mips_pic;
252b5132 616
c9914766 617/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 618 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 619static int mips_big_got = 0;
252b5132
RH
620
621/* 1 if trap instructions should used for overflow rather than break
622 instructions. */
c9914766 623static int mips_trap = 0;
252b5132 624
119d663a 625/* 1 if double width floating point constants should not be constructed
b6ff326e 626 by assembling two single width halves into two single width floating
119d663a
NC
627 point registers which just happen to alias the double width destination
628 register. On some architectures this aliasing can be disabled by a bit
d547a75e 629 in the status register, and the setting of this bit cannot be determined
119d663a
NC
630 automatically at assemble time. */
631static int mips_disable_float_construction;
632
252b5132
RH
633/* Non-zero if any .set noreorder directives were used. */
634
635static int mips_any_noreorder;
636
6b76fefe
CM
637/* Non-zero if nops should be inserted when the register referenced in
638 an mfhi/mflo instruction is read in the next two instructions. */
639static int mips_7000_hilo_fix;
640
02ffd3e4 641/* The size of objects in the small data section. */
156c2f8b 642static unsigned int g_switch_value = 8;
252b5132
RH
643/* Whether the -G option was used. */
644static int g_switch_seen = 0;
645
646#define N_RMASK 0xc4
647#define N_VFP 0xd4
648
649/* If we can determine in advance that GP optimization won't be
650 possible, we can skip the relaxation stuff that tries to produce
651 GP-relative references. This makes delay slot optimization work
652 better.
653
654 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
655 gcc output. It needs to guess right for gcc, otherwise gcc
656 will put what it thinks is a GP-relative instruction in a branch
657 delay slot.
252b5132
RH
658
659 I don't know if a fix is needed for the SVR4_PIC mode. I've only
660 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 661static int nopic_need_relax (symbolS *, int);
252b5132
RH
662
663/* handle of the OPCODE hash table */
664static struct hash_control *op_hash = NULL;
665
666/* The opcode hash table we use for the mips16. */
667static struct hash_control *mips16_op_hash = NULL;
668
df58fc94
RS
669/* The opcode hash table we use for the microMIPS ASE. */
670static struct hash_control *micromips_op_hash = NULL;
671
252b5132
RH
672/* This array holds the chars that always start a comment. If the
673 pre-processor is disabled, these aren't very useful */
674const char comment_chars[] = "#";
675
676/* This array holds the chars that only start a comment at the beginning of
677 a line. If the line seems to have the form '# 123 filename'
678 .line and .file directives will appear in the pre-processed output */
679/* Note that input_file.c hand checks for '#' at the beginning of the
680 first line of the input file. This is because the compiler outputs
bdaaa2e1 681 #NO_APP at the beginning of its output. */
252b5132
RH
682/* Also note that C style comments are always supported. */
683const char line_comment_chars[] = "#";
684
bdaaa2e1 685/* This array holds machine specific line separator characters. */
63a0b638 686const char line_separator_chars[] = ";";
252b5132
RH
687
688/* Chars that can be used to separate mant from exp in floating point nums */
689const char EXP_CHARS[] = "eE";
690
691/* Chars that mean this number is a floating point constant */
692/* As in 0f12.456 */
693/* or 0d1.2345e12 */
694const char FLT_CHARS[] = "rRsSfFdDxXpP";
695
696/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
697 changed in read.c . Ideally it shouldn't have to know about it at all,
698 but nothing is ideal around here.
699 */
700
e3de51ce
RS
701/* Types of printf format used for instruction-related error messages.
702 "I" means int ("%d") and "S" means string ("%s"). */
703enum mips_insn_error_format {
704 ERR_FMT_PLAIN,
705 ERR_FMT_I,
706 ERR_FMT_SS,
707};
708
709/* Information about an error that was found while assembling the current
710 instruction. */
711struct mips_insn_error {
712 /* We sometimes need to match an instruction against more than one
713 opcode table entry. Errors found during this matching are reported
714 against a particular syntactic argument rather than against the
715 instruction as a whole. We grade these messages so that errors
716 against argument N have a greater priority than an error against
717 any argument < N, since the former implies that arguments up to N
718 were acceptable and that the opcode entry was therefore a closer match.
719 If several matches report an error against the same argument,
720 we only use that error if it is the same in all cases.
721
722 min_argnum is the minimum argument number for which an error message
723 should be accepted. It is 0 if MSG is against the instruction as
724 a whole. */
725 int min_argnum;
726
727 /* The printf()-style message, including its format and arguments. */
728 enum mips_insn_error_format format;
729 const char *msg;
730 union {
731 int i;
732 const char *ss[2];
733 } u;
734};
735
736/* The error that should be reported for the current instruction. */
737static struct mips_insn_error insn_error;
252b5132
RH
738
739static int auto_align = 1;
740
741/* When outputting SVR4 PIC code, the assembler needs to know the
742 offset in the stack frame from which to restore the $gp register.
743 This is set by the .cprestore pseudo-op, and saved in this
744 variable. */
745static offsetT mips_cprestore_offset = -1;
746
67c1ffbe 747/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 748 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 749 offset and even an other register than $gp as global pointer. */
6478892d
TS
750static offsetT mips_cpreturn_offset = -1;
751static int mips_cpreturn_register = -1;
752static int mips_gp_register = GP;
def2e0dd 753static int mips_gprel_offset = 0;
6478892d 754
7a621144
DJ
755/* Whether mips_cprestore_offset has been set in the current function
756 (or whether it has already been warned about, if not). */
757static int mips_cprestore_valid = 0;
758
252b5132
RH
759/* This is the register which holds the stack frame, as set by the
760 .frame pseudo-op. This is needed to implement .cprestore. */
761static int mips_frame_reg = SP;
762
7a621144
DJ
763/* Whether mips_frame_reg has been set in the current function
764 (or whether it has already been warned about, if not). */
765static int mips_frame_reg_valid = 0;
766
252b5132
RH
767/* To output NOP instructions correctly, we need to keep information
768 about the previous two instructions. */
769
770/* Whether we are optimizing. The default value of 2 means to remove
771 unneeded NOPs and swap branch instructions when possible. A value
772 of 1 means to not swap branches. A value of 0 means to always
773 insert NOPs. */
774static int mips_optimize = 2;
775
776/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
777 equivalent to seeing no -g option at all. */
778static int mips_debug = 0;
779
7d8e00cf
RS
780/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
781#define MAX_VR4130_NOPS 4
782
783/* The maximum number of NOPs needed to fill delay slots. */
784#define MAX_DELAY_NOPS 2
785
786/* The maximum number of NOPs needed for any purpose. */
787#define MAX_NOPS 4
71400594
RS
788
789/* A list of previous instructions, with index 0 being the most recent.
790 We need to look back MAX_NOPS instructions when filling delay slots
791 or working around processor errata. We need to look back one
792 instruction further if we're thinking about using history[0] to
793 fill a branch delay slot. */
794static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 795
fc76e730 796/* Arrays of operands for each instruction. */
14daeee3 797#define MAX_OPERANDS 6
fc76e730
RS
798struct mips_operand_array {
799 const struct mips_operand *operand[MAX_OPERANDS];
800};
801static struct mips_operand_array *mips_operands;
802static struct mips_operand_array *mips16_operands;
803static struct mips_operand_array *micromips_operands;
804
1e915849 805/* Nop instructions used by emit_nop. */
df58fc94
RS
806static struct mips_cl_insn nop_insn;
807static struct mips_cl_insn mips16_nop_insn;
808static struct mips_cl_insn micromips_nop16_insn;
809static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
810
811/* The appropriate nop for the current mode. */
833794fc
MR
812#define NOP_INSN (mips_opts.mips16 \
813 ? &mips16_nop_insn \
814 : (mips_opts.micromips \
815 ? (mips_opts.insn32 \
816 ? &micromips_nop32_insn \
817 : &micromips_nop16_insn) \
818 : &nop_insn))
df58fc94
RS
819
820/* The size of NOP_INSN in bytes. */
833794fc
MR
821#define NOP_INSN_SIZE ((mips_opts.mips16 \
822 || (mips_opts.micromips && !mips_opts.insn32)) \
823 ? 2 : 4)
252b5132 824
252b5132
RH
825/* If this is set, it points to a frag holding nop instructions which
826 were inserted before the start of a noreorder section. If those
827 nops turn out to be unnecessary, the size of the frag can be
828 decreased. */
829static fragS *prev_nop_frag;
830
831/* The number of nop instructions we created in prev_nop_frag. */
832static int prev_nop_frag_holds;
833
834/* The number of nop instructions that we know we need in
bdaaa2e1 835 prev_nop_frag. */
252b5132
RH
836static int prev_nop_frag_required;
837
838/* The number of instructions we've seen since prev_nop_frag. */
839static int prev_nop_frag_since;
840
e8044f35
RS
841/* Relocations against symbols are sometimes done in two parts, with a HI
842 relocation and a LO relocation. Each relocation has only 16 bits of
843 space to store an addend. This means that in order for the linker to
844 handle carries correctly, it must be able to locate both the HI and
845 the LO relocation. This means that the relocations must appear in
846 order in the relocation table.
252b5132
RH
847
848 In order to implement this, we keep track of each unmatched HI
849 relocation. We then sort them so that they immediately precede the
bdaaa2e1 850 corresponding LO relocation. */
252b5132 851
e972090a
NC
852struct mips_hi_fixup
853{
252b5132
RH
854 /* Next HI fixup. */
855 struct mips_hi_fixup *next;
856 /* This fixup. */
857 fixS *fixp;
858 /* The section this fixup is in. */
859 segT seg;
860};
861
862/* The list of unmatched HI relocs. */
863
864static struct mips_hi_fixup *mips_hi_fixup_list;
865
64bdfcaf
RS
866/* The frag containing the last explicit relocation operator.
867 Null if explicit relocations have not been used. */
868
869static fragS *prev_reloc_op_frag;
870
252b5132
RH
871/* Map mips16 register numbers to normal MIPS register numbers. */
872
e972090a
NC
873static const unsigned int mips16_to_32_reg_map[] =
874{
252b5132
RH
875 16, 17, 2, 3, 4, 5, 6, 7
876};
60b63b72 877
df58fc94
RS
878/* Map microMIPS register numbers to normal MIPS register numbers. */
879
df58fc94 880#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
881
882/* The microMIPS registers with type h. */
e76ff5ab 883static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
884{
885 5, 5, 6, 4, 4, 4, 4, 4
886};
e76ff5ab 887static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
888{
889 6, 7, 7, 21, 22, 5, 6, 7
890};
891
df58fc94
RS
892/* The microMIPS registers with type m. */
893static const unsigned int micromips_to_32_reg_m_map[] =
894{
895 0, 17, 2, 3, 16, 18, 19, 20
896};
897
898#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
899
71400594
RS
900/* Classifies the kind of instructions we're interested in when
901 implementing -mfix-vr4120. */
c67a084a
NC
902enum fix_vr4120_class
903{
71400594
RS
904 FIX_VR4120_MACC,
905 FIX_VR4120_DMACC,
906 FIX_VR4120_MULT,
907 FIX_VR4120_DMULT,
908 FIX_VR4120_DIV,
909 FIX_VR4120_MTHILO,
910 NUM_FIX_VR4120_CLASSES
911};
912
c67a084a
NC
913/* ...likewise -mfix-loongson2f-jump. */
914static bfd_boolean mips_fix_loongson2f_jump;
915
916/* ...likewise -mfix-loongson2f-nop. */
917static bfd_boolean mips_fix_loongson2f_nop;
918
919/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
920static bfd_boolean mips_fix_loongson2f;
921
71400594
RS
922/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
923 there must be at least one other instruction between an instruction
924 of type X and an instruction of type Y. */
925static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
926
927/* True if -mfix-vr4120 is in force. */
d766e8ec 928static int mips_fix_vr4120;
4a6a3df4 929
7d8e00cf
RS
930/* ...likewise -mfix-vr4130. */
931static int mips_fix_vr4130;
932
6a32d874
CM
933/* ...likewise -mfix-24k. */
934static int mips_fix_24k;
935
a8d14a88
CM
936/* ...likewise -mfix-rm7000 */
937static int mips_fix_rm7000;
938
d954098f
DD
939/* ...likewise -mfix-cn63xxp1 */
940static bfd_boolean mips_fix_cn63xxp1;
941
4a6a3df4
AO
942/* We don't relax branches by default, since this causes us to expand
943 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
944 fail to compute the offset before expanding the macro to the most
945 efficient expansion. */
946
947static int mips_relax_branch;
8b10b0b3
MR
948
949/* TRUE if checks are suppressed for invalid branches between ISA modes.
950 Needed for broken assembly produced by some GCC versions and some
951 sloppy code out there, where branches to data labels are present. */
952static bfd_boolean mips_ignore_branch_isa;
252b5132 953\f
4d7206a2
RS
954/* The expansion of many macros depends on the type of symbol that
955 they refer to. For example, when generating position-dependent code,
956 a macro that refers to a symbol may have two different expansions,
957 one which uses GP-relative addresses and one which uses absolute
958 addresses. When generating SVR4-style PIC, a macro may have
959 different expansions for local and global symbols.
960
961 We handle these situations by generating both sequences and putting
962 them in variant frags. In position-dependent code, the first sequence
963 will be the GP-relative one and the second sequence will be the
964 absolute one. In SVR4 PIC, the first sequence will be for global
965 symbols and the second will be for local symbols.
966
584892a6
RS
967 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
968 SECOND are the lengths of the two sequences in bytes. These fields
969 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
970 the subtype has the following flags:
4d7206a2 971
ce8ad872
MR
972 RELAX_PIC
973 Set if generating PIC code.
974
584892a6
RS
975 RELAX_USE_SECOND
976 Set if it has been decided that we should use the second
977 sequence instead of the first.
978
979 RELAX_SECOND_LONGER
980 Set in the first variant frag if the macro's second implementation
981 is longer than its first. This refers to the macro as a whole,
982 not an individual relaxation.
983
984 RELAX_NOMACRO
985 Set in the first variant frag if the macro appeared in a .set nomacro
986 block and if one alternative requires a warning but the other does not.
987
988 RELAX_DELAY_SLOT
989 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
990 delay slot.
4d7206a2 991
df58fc94
RS
992 RELAX_DELAY_SLOT_16BIT
993 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
994 16-bit instruction.
995
996 RELAX_DELAY_SLOT_SIZE_FIRST
997 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
998 the macro is of the wrong size for the branch delay slot.
999
1000 RELAX_DELAY_SLOT_SIZE_SECOND
1001 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1002 the macro is of the wrong size for the branch delay slot.
1003
4d7206a2
RS
1004 The frag's "opcode" points to the first fixup for relaxable code.
1005
1006 Relaxable macros are generated using a sequence such as:
1007
1008 relax_start (SYMBOL);
1009 ... generate first expansion ...
1010 relax_switch ();
1011 ... generate second expansion ...
1012 relax_end ();
1013
1014 The code and fixups for the unwanted alternative are discarded
1015 by md_convert_frag. */
ce8ad872
MR
1016#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1017 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1018
584892a6
RS
1019#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1020#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1021#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1022#define RELAX_USE_SECOND 0x20000
1023#define RELAX_SECOND_LONGER 0x40000
1024#define RELAX_NOMACRO 0x80000
1025#define RELAX_DELAY_SLOT 0x100000
1026#define RELAX_DELAY_SLOT_16BIT 0x200000
1027#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1028#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1029
4a6a3df4
AO
1030/* Branch without likely bit. If label is out of range, we turn:
1031
134c0c8b 1032 beq reg1, reg2, label
4a6a3df4
AO
1033 delay slot
1034
1035 into
1036
1037 bne reg1, reg2, 0f
1038 nop
1039 j label
1040 0: delay slot
1041
1042 with the following opcode replacements:
1043
1044 beq <-> bne
1045 blez <-> bgtz
1046 bltz <-> bgez
1047 bc1f <-> bc1t
1048
1049 bltzal <-> bgezal (with jal label instead of j label)
1050
1051 Even though keeping the delay slot instruction in the delay slot of
1052 the branch would be more efficient, it would be very tricky to do
1053 correctly, because we'd have to introduce a variable frag *after*
1054 the delay slot instruction, and expand that instead. Let's do it
1055 the easy way for now, even if the branch-not-taken case now costs
1056 one additional instruction. Out-of-range branches are not supposed
1057 to be common, anyway.
1058
1059 Branch likely. If label is out of range, we turn:
1060
1061 beql reg1, reg2, label
1062 delay slot (annulled if branch not taken)
1063
1064 into
1065
1066 beql reg1, reg2, 1f
1067 nop
1068 beql $0, $0, 2f
1069 nop
1070 1: j[al] label
1071 delay slot (executed only if branch taken)
1072 2:
1073
1074 It would be possible to generate a shorter sequence by losing the
1075 likely bit, generating something like:
b34976b6 1076
4a6a3df4
AO
1077 bne reg1, reg2, 0f
1078 nop
1079 j[al] label
1080 delay slot (executed only if branch taken)
1081 0:
1082
1083 beql -> bne
1084 bnel -> beq
1085 blezl -> bgtz
1086 bgtzl -> blez
1087 bltzl -> bgez
1088 bgezl -> bltz
1089 bc1fl -> bc1t
1090 bc1tl -> bc1f
1091
1092 bltzall -> bgezal (with jal label instead of j label)
1093 bgezall -> bltzal (ditto)
1094
1095
1096 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1097#define RELAX_BRANCH_ENCODE(at, pic, \
1098 uncond, likely, link, toofar) \
66b3e8da
MR
1099 ((relax_substateT) \
1100 (0xc0000000 \
1101 | ((at) & 0x1f) \
ce8ad872
MR
1102 | ((pic) ? 0x20 : 0) \
1103 | ((toofar) ? 0x40 : 0) \
1104 | ((link) ? 0x80 : 0) \
1105 | ((likely) ? 0x100 : 0) \
1106 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1107#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1108#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1109#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1110#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1111#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1112#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1113#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1114
252b5132
RH
1115/* For mips16 code, we use an entirely different form of relaxation.
1116 mips16 supports two versions of most instructions which take
1117 immediate values: a small one which takes some small value, and a
1118 larger one which takes a 16 bit value. Since branches also follow
1119 this pattern, relaxing these values is required.
1120
1121 We can assemble both mips16 and normal MIPS code in a single
1122 object. Therefore, we need to support this type of relaxation at
1123 the same time that we support the relaxation described above. We
1124 use the high bit of the subtype field to distinguish these cases.
1125
1126 The information we store for this type of relaxation is the
1127 argument code found in the opcode file for this relocation, whether
1128 the user explicitly requested a small or extended form, and whether
1129 the relocation is in a jump or jal delay slot. That tells us the
1130 size of the value, and how it should be stored. We also store
1131 whether the fragment is considered to be extended or not. We also
1132 store whether this is known to be a branch to a different section,
1133 whether we have tried to relax this frag yet, and whether we have
1134 ever extended a PC relative fragment because of a shift count. */
25499ac7 1135#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1136 small, ext, \
1137 dslot, jal_dslot) \
252b5132
RH
1138 (0x80000000 \
1139 | ((type) & 0xff) \
25499ac7
MR
1140 | ((e2) ? 0x100 : 0) \
1141 | ((pic) ? 0x200 : 0) \
1142 | ((sym32) ? 0x400 : 0) \
1143 | ((nomacro) ? 0x800 : 0) \
1144 | ((small) ? 0x1000 : 0) \
1145 | ((ext) ? 0x2000 : 0) \
1146 | ((dslot) ? 0x4000 : 0) \
1147 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1148
4a6a3df4 1149#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1150#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1151#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1152#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1153#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1154#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1155#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1156#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1157#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1158#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1159
1160#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1161#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1162#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1163#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1164#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1165#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1166#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1167#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1168#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1169
df58fc94
RS
1170/* For microMIPS code, we use relaxation similar to one we use for
1171 MIPS16 code. Some instructions that take immediate values support
1172 two encodings: a small one which takes some small value, and a
1173 larger one which takes a 16 bit value. As some branches also follow
1174 this pattern, relaxing these values is required.
1175
1176 We can assemble both microMIPS and normal MIPS code in a single
1177 object. Therefore, we need to support this type of relaxation at
1178 the same time that we support the relaxation described above. We
1179 use one of the high bits of the subtype field to distinguish these
1180 cases.
1181
1182 The information we store for this type of relaxation is the argument
1183 code found in the opcode file for this relocation, the register
8484fb75
MR
1184 selected as the assembler temporary, whether in the 32-bit
1185 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1186 compact, whether there is no delay-slot instruction available to fill
1187 in, whether it stores the link address implicitly in $ra, whether
1188 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1189 instructions is enabled, and whether the displacement of a branch is
1190 too large to fit as an immediate argument of a 16-bit and a 32-bit
1191 branch, respectively. */
ce8ad872 1192#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1193 uncond, compact, link, nods, \
40209cad
MR
1194 relax32, toofar16, toofar32) \
1195 (0x40000000 \
1196 | ((type) & 0xff) \
1197 | (((at) & 0x1f) << 8) \
8484fb75 1198 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1199 | ((pic) ? 0x4000 : 0) \
1200 | ((uncond) ? 0x8000 : 0) \
1201 | ((compact) ? 0x10000 : 0) \
1202 | ((link) ? 0x20000 : 0) \
1203 | ((nods) ? 0x40000 : 0) \
1204 | ((relax32) ? 0x80000 : 0) \
1205 | ((toofar16) ? 0x100000 : 0) \
1206 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1207#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1208#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1209#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1210#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1211#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1212#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1213#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1214#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1215#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1216#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1217
1218#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1219#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1220#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1221#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1222#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1223#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1224
43c0598f
RS
1225/* Sign-extend 16-bit value X. */
1226#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1227
885add95
CD
1228/* Is the given value a sign-extended 32-bit value? */
1229#define IS_SEXT_32BIT_NUM(x) \
1230 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1231 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1232
1233/* Is the given value a sign-extended 16-bit value? */
1234#define IS_SEXT_16BIT_NUM(x) \
1235 (((x) &~ (offsetT) 0x7fff) == 0 \
1236 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1237
df58fc94
RS
1238/* Is the given value a sign-extended 12-bit value? */
1239#define IS_SEXT_12BIT_NUM(x) \
1240 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1241
7f3c4072
CM
1242/* Is the given value a sign-extended 9-bit value? */
1243#define IS_SEXT_9BIT_NUM(x) \
1244 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1245
2051e8c4
MR
1246/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1247#define IS_ZEXT_32BIT_NUM(x) \
1248 (((x) &~ (offsetT) 0xffffffff) == 0 \
1249 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1250
bf12938e
RS
1251/* Extract bits MASK << SHIFT from STRUCT and shift them right
1252 SHIFT places. */
1253#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1254 (((STRUCT) >> (SHIFT)) & (MASK))
1255
bf12938e 1256/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1257#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1258 (!(MICROMIPS) \
1259 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1260 : EXTRACT_BITS ((INSN).insn_opcode, \
1261 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1262#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1263 EXTRACT_BITS ((INSN).insn_opcode, \
1264 MIPS16OP_MASK_##FIELD, \
1265 MIPS16OP_SH_##FIELD)
5c04167a
RS
1266
1267/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1268#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1269\f
df58fc94
RS
1270/* Whether or not we are emitting a branch-likely macro. */
1271static bfd_boolean emit_branch_likely_macro = FALSE;
1272
4d7206a2
RS
1273/* Global variables used when generating relaxable macros. See the
1274 comment above RELAX_ENCODE for more details about how relaxation
1275 is used. */
1276static struct {
1277 /* 0 if we're not emitting a relaxable macro.
1278 1 if we're emitting the first of the two relaxation alternatives.
1279 2 if we're emitting the second alternative. */
1280 int sequence;
1281
1282 /* The first relaxable fixup in the current frag. (In other words,
1283 the first fixup that refers to relaxable code.) */
1284 fixS *first_fixup;
1285
1286 /* sizes[0] says how many bytes of the first alternative are stored in
1287 the current frag. Likewise sizes[1] for the second alternative. */
1288 unsigned int sizes[2];
1289
1290 /* The symbol on which the choice of sequence depends. */
1291 symbolS *symbol;
1292} mips_relax;
252b5132 1293\f
584892a6
RS
1294/* Global variables used to decide whether a macro needs a warning. */
1295static struct {
1296 /* True if the macro is in a branch delay slot. */
1297 bfd_boolean delay_slot_p;
1298
df58fc94
RS
1299 /* Set to the length in bytes required if the macro is in a delay slot
1300 that requires a specific length of instruction, otherwise zero. */
1301 unsigned int delay_slot_length;
1302
584892a6
RS
1303 /* For relaxable macros, sizes[0] is the length of the first alternative
1304 in bytes and sizes[1] is the length of the second alternative.
1305 For non-relaxable macros, both elements give the length of the
1306 macro in bytes. */
1307 unsigned int sizes[2];
1308
df58fc94
RS
1309 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1310 instruction of the first alternative in bytes and first_insn_sizes[1]
1311 is the length of the first instruction of the second alternative.
1312 For non-relaxable macros, both elements give the length of the first
1313 instruction in bytes.
1314
1315 Set to zero if we haven't yet seen the first instruction. */
1316 unsigned int first_insn_sizes[2];
1317
1318 /* For relaxable macros, insns[0] is the number of instructions for the
1319 first alternative and insns[1] is the number of instructions for the
1320 second alternative.
1321
1322 For non-relaxable macros, both elements give the number of
1323 instructions for the macro. */
1324 unsigned int insns[2];
1325
584892a6
RS
1326 /* The first variant frag for this macro. */
1327 fragS *first_frag;
1328} mips_macro_warning;
1329\f
252b5132
RH
1330/* Prototypes for static functions. */
1331
252b5132
RH
1332enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1333
b34976b6 1334static void append_insn
df58fc94
RS
1335 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1336 bfd_boolean expansionp);
7d10b47d 1337static void mips_no_prev_insn (void);
c67a084a 1338static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1339static void mips16_macro_build
03ea81db 1340 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1341static void load_register (int, expressionS *, int);
584892a6
RS
1342static void macro_start (void);
1343static void macro_end (void);
833794fc 1344static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1345static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1346static void mips_ip (char *str, struct mips_cl_insn * ip);
1347static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1348static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1349static void mips16_immed
3b4dbbbf 1350 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1351 unsigned int, unsigned long *);
5e0116d5 1352static size_t my_getSmallExpression
17a2f251
TS
1353 (expressionS *, bfd_reloc_code_real_type *, char *);
1354static void my_getExpression (expressionS *, char *);
1355static void s_align (int);
1356static void s_change_sec (int);
1357static void s_change_section (int);
1358static void s_cons (int);
1359static void s_float_cons (int);
1360static void s_mips_globl (int);
1361static void s_option (int);
1362static void s_mipsset (int);
1363static void s_abicalls (int);
1364static void s_cpload (int);
1365static void s_cpsetup (int);
1366static void s_cplocal (int);
1367static void s_cprestore (int);
1368static void s_cpreturn (int);
741d6ea8
JM
1369static void s_dtprelword (int);
1370static void s_dtpreldword (int);
d0f13682
CLT
1371static void s_tprelword (int);
1372static void s_tpreldword (int);
17a2f251
TS
1373static void s_gpvalue (int);
1374static void s_gpword (int);
1375static void s_gpdword (int);
a3f278e2 1376static void s_ehword (int);
17a2f251
TS
1377static void s_cpadd (int);
1378static void s_insn (int);
ba92f887 1379static void s_nan (int);
919731af 1380static void s_module (int);
17a2f251
TS
1381static void s_mips_ent (int);
1382static void s_mips_end (int);
1383static void s_mips_frame (int);
1384static void s_mips_mask (int reg_type);
1385static void s_mips_stab (int);
1386static void s_mips_weakext (int);
1387static void s_mips_file (int);
1388static void s_mips_loc (int);
9e009953 1389static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1390static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1391static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1392static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1393static void file_mips_check_options (void);
e7af610e
NC
1394
1395/* Table and functions used to map between CPU/ISA names, and
1396 ISA levels, and CPU numbers. */
1397
e972090a
NC
1398struct mips_cpu_info
1399{
e7af610e 1400 const char *name; /* CPU or ISA name. */
d16afab6
RS
1401 int flags; /* MIPS_CPU_* flags. */
1402 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1403 int isa; /* ISA level. */
1404 int cpu; /* CPU number (default CPU if ISA). */
1405};
1406
ad3fea08 1407#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1408
17a2f251
TS
1409static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1410static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1411static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1412\f
c31f3936
RS
1413/* Command-line options. */
1414const char *md_shortopts = "O::g::G:";
1415
1416enum options
1417 {
1418 OPTION_MARCH = OPTION_MD_BASE,
1419 OPTION_MTUNE,
1420 OPTION_MIPS1,
1421 OPTION_MIPS2,
1422 OPTION_MIPS3,
1423 OPTION_MIPS4,
1424 OPTION_MIPS5,
1425 OPTION_MIPS32,
1426 OPTION_MIPS64,
1427 OPTION_MIPS32R2,
ae52f483
AB
1428 OPTION_MIPS32R3,
1429 OPTION_MIPS32R5,
7361da2c 1430 OPTION_MIPS32R6,
c31f3936 1431 OPTION_MIPS64R2,
ae52f483
AB
1432 OPTION_MIPS64R3,
1433 OPTION_MIPS64R5,
7361da2c 1434 OPTION_MIPS64R6,
c31f3936
RS
1435 OPTION_MIPS16,
1436 OPTION_NO_MIPS16,
1437 OPTION_MIPS3D,
1438 OPTION_NO_MIPS3D,
1439 OPTION_MDMX,
1440 OPTION_NO_MDMX,
1441 OPTION_DSP,
1442 OPTION_NO_DSP,
1443 OPTION_MT,
1444 OPTION_NO_MT,
1445 OPTION_VIRT,
1446 OPTION_NO_VIRT,
56d438b1
CF
1447 OPTION_MSA,
1448 OPTION_NO_MSA,
c31f3936
RS
1449 OPTION_SMARTMIPS,
1450 OPTION_NO_SMARTMIPS,
1451 OPTION_DSPR2,
1452 OPTION_NO_DSPR2,
8f4f9071
MF
1453 OPTION_DSPR3,
1454 OPTION_NO_DSPR3,
c31f3936
RS
1455 OPTION_EVA,
1456 OPTION_NO_EVA,
7d64c587
AB
1457 OPTION_XPA,
1458 OPTION_NO_XPA,
c31f3936
RS
1459 OPTION_MICROMIPS,
1460 OPTION_NO_MICROMIPS,
1461 OPTION_MCU,
1462 OPTION_NO_MCU,
25499ac7
MR
1463 OPTION_MIPS16E2,
1464 OPTION_NO_MIPS16E2,
730c3174
SE
1465 OPTION_CRC,
1466 OPTION_NO_CRC,
c31f3936
RS
1467 OPTION_M4650,
1468 OPTION_NO_M4650,
1469 OPTION_M4010,
1470 OPTION_NO_M4010,
1471 OPTION_M4100,
1472 OPTION_NO_M4100,
1473 OPTION_M3900,
1474 OPTION_NO_M3900,
1475 OPTION_M7000_HILO_FIX,
1476 OPTION_MNO_7000_HILO_FIX,
1477 OPTION_FIX_24K,
1478 OPTION_NO_FIX_24K,
a8d14a88
CM
1479 OPTION_FIX_RM7000,
1480 OPTION_NO_FIX_RM7000,
c31f3936
RS
1481 OPTION_FIX_LOONGSON2F_JUMP,
1482 OPTION_NO_FIX_LOONGSON2F_JUMP,
1483 OPTION_FIX_LOONGSON2F_NOP,
1484 OPTION_NO_FIX_LOONGSON2F_NOP,
1485 OPTION_FIX_VR4120,
1486 OPTION_NO_FIX_VR4120,
1487 OPTION_FIX_VR4130,
1488 OPTION_NO_FIX_VR4130,
1489 OPTION_FIX_CN63XXP1,
1490 OPTION_NO_FIX_CN63XXP1,
1491 OPTION_TRAP,
1492 OPTION_BREAK,
1493 OPTION_EB,
1494 OPTION_EL,
1495 OPTION_FP32,
1496 OPTION_GP32,
1497 OPTION_CONSTRUCT_FLOATS,
1498 OPTION_NO_CONSTRUCT_FLOATS,
1499 OPTION_FP64,
351cdf24 1500 OPTION_FPXX,
c31f3936
RS
1501 OPTION_GP64,
1502 OPTION_RELAX_BRANCH,
1503 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1504 OPTION_IGNORE_BRANCH_ISA,
1505 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1506 OPTION_INSN32,
1507 OPTION_NO_INSN32,
c31f3936
RS
1508 OPTION_MSHARED,
1509 OPTION_MNO_SHARED,
1510 OPTION_MSYM32,
1511 OPTION_MNO_SYM32,
1512 OPTION_SOFT_FLOAT,
1513 OPTION_HARD_FLOAT,
1514 OPTION_SINGLE_FLOAT,
1515 OPTION_DOUBLE_FLOAT,
1516 OPTION_32,
c31f3936
RS
1517 OPTION_CALL_SHARED,
1518 OPTION_CALL_NONPIC,
1519 OPTION_NON_SHARED,
1520 OPTION_XGOT,
1521 OPTION_MABI,
1522 OPTION_N32,
1523 OPTION_64,
1524 OPTION_MDEBUG,
1525 OPTION_NO_MDEBUG,
1526 OPTION_PDR,
1527 OPTION_NO_PDR,
1528 OPTION_MVXWORKS_PIC,
ba92f887 1529 OPTION_NAN,
351cdf24
MF
1530 OPTION_ODD_SPREG,
1531 OPTION_NO_ODD_SPREG,
6f20c942
FS
1532 OPTION_GINV,
1533 OPTION_NO_GINV,
8095d2f7
CX
1534 OPTION_LOONGSON_MMI,
1535 OPTION_NO_LOONGSON_MMI,
716c08de
CX
1536 OPTION_LOONGSON_CAM,
1537 OPTION_NO_LOONGSON_CAM,
bdc6c06e
CX
1538 OPTION_LOONGSON_EXT,
1539 OPTION_NO_LOONGSON_EXT,
a693765e
CX
1540 OPTION_LOONGSON_EXT2,
1541 OPTION_NO_LOONGSON_EXT2,
c31f3936
RS
1542 OPTION_END_OF_ENUM
1543 };
1544
1545struct option md_longopts[] =
1546{
1547 /* Options which specify architecture. */
1548 {"march", required_argument, NULL, OPTION_MARCH},
1549 {"mtune", required_argument, NULL, OPTION_MTUNE},
1550 {"mips0", no_argument, NULL, OPTION_MIPS1},
1551 {"mips1", no_argument, NULL, OPTION_MIPS1},
1552 {"mips2", no_argument, NULL, OPTION_MIPS2},
1553 {"mips3", no_argument, NULL, OPTION_MIPS3},
1554 {"mips4", no_argument, NULL, OPTION_MIPS4},
1555 {"mips5", no_argument, NULL, OPTION_MIPS5},
1556 {"mips32", no_argument, NULL, OPTION_MIPS32},
1557 {"mips64", no_argument, NULL, OPTION_MIPS64},
1558 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1559 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1560 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1561 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1562 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1563 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1564 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1565 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1566
1567 /* Options which specify Application Specific Extensions (ASEs). */
1568 {"mips16", no_argument, NULL, OPTION_MIPS16},
1569 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1570 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1571 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1572 {"mdmx", no_argument, NULL, OPTION_MDMX},
1573 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1574 {"mdsp", no_argument, NULL, OPTION_DSP},
1575 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1576 {"mmt", no_argument, NULL, OPTION_MT},
1577 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1578 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1579 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1580 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1581 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1582 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1583 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1584 {"meva", no_argument, NULL, OPTION_EVA},
1585 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1586 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1587 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1588 {"mmcu", no_argument, NULL, OPTION_MCU},
1589 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1590 {"mvirt", no_argument, NULL, OPTION_VIRT},
1591 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1592 {"mmsa", no_argument, NULL, OPTION_MSA},
1593 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1594 {"mxpa", no_argument, NULL, OPTION_XPA},
1595 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1596 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1597 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1598 {"mcrc", no_argument, NULL, OPTION_CRC},
1599 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1600 {"mginv", no_argument, NULL, OPTION_GINV},
1601 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
8095d2f7
CX
1602 {"mloongson-mmi", no_argument, NULL, OPTION_LOONGSON_MMI},
1603 {"mno-loongson-mmi", no_argument, NULL, OPTION_NO_LOONGSON_MMI},
716c08de
CX
1604 {"mloongson-cam", no_argument, NULL, OPTION_LOONGSON_CAM},
1605 {"mno-loongson-cam", no_argument, NULL, OPTION_NO_LOONGSON_CAM},
bdc6c06e
CX
1606 {"mloongson-ext", no_argument, NULL, OPTION_LOONGSON_EXT},
1607 {"mno-loongson-ext", no_argument, NULL, OPTION_NO_LOONGSON_EXT},
a693765e
CX
1608 {"mloongson-ext2", no_argument, NULL, OPTION_LOONGSON_EXT2},
1609 {"mno-loongson-ext2", no_argument, NULL, OPTION_NO_LOONGSON_EXT2},
c31f3936
RS
1610
1611 /* Old-style architecture options. Don't add more of these. */
1612 {"m4650", no_argument, NULL, OPTION_M4650},
1613 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1614 {"m4010", no_argument, NULL, OPTION_M4010},
1615 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1616 {"m4100", no_argument, NULL, OPTION_M4100},
1617 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1618 {"m3900", no_argument, NULL, OPTION_M3900},
1619 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1620
1621 /* Options which enable bug fixes. */
1622 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1623 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1624 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1625 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1626 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1627 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1628 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1629 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1630 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1631 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1632 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1633 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1634 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1635 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1636 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1637 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1638 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1639
1640 /* Miscellaneous options. */
1641 {"trap", no_argument, NULL, OPTION_TRAP},
1642 {"no-break", no_argument, NULL, OPTION_TRAP},
1643 {"break", no_argument, NULL, OPTION_BREAK},
1644 {"no-trap", no_argument, NULL, OPTION_BREAK},
1645 {"EB", no_argument, NULL, OPTION_EB},
1646 {"EL", no_argument, NULL, OPTION_EL},
1647 {"mfp32", no_argument, NULL, OPTION_FP32},
1648 {"mgp32", no_argument, NULL, OPTION_GP32},
1649 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1650 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1651 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1652 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1653 {"mgp64", no_argument, NULL, OPTION_GP64},
1654 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1655 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1656 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1657 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1658 {"minsn32", no_argument, NULL, OPTION_INSN32},
1659 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1660 {"mshared", no_argument, NULL, OPTION_MSHARED},
1661 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1662 {"msym32", no_argument, NULL, OPTION_MSYM32},
1663 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1664 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1665 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1666 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1667 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1668 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1669 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1670
1671 /* Strictly speaking this next option is ELF specific,
1672 but we allow it for other ports as well in order to
1673 make testing easier. */
1674 {"32", no_argument, NULL, OPTION_32},
1675
1676 /* ELF-specific options. */
c31f3936
RS
1677 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1678 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1679 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1680 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1681 {"xgot", no_argument, NULL, OPTION_XGOT},
1682 {"mabi", required_argument, NULL, OPTION_MABI},
1683 {"n32", no_argument, NULL, OPTION_N32},
1684 {"64", no_argument, NULL, OPTION_64},
1685 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1686 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1687 {"mpdr", no_argument, NULL, OPTION_PDR},
1688 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1689 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1690 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1691
1692 {NULL, no_argument, NULL, 0}
1693};
1694size_t md_longopts_size = sizeof (md_longopts);
1695\f
c6278170
RS
1696/* Information about either an Application Specific Extension or an
1697 optional architecture feature that, for simplicity, we treat in the
1698 same way as an ASE. */
1699struct mips_ase
1700{
1701 /* The name of the ASE, used in both the command-line and .set options. */
1702 const char *name;
1703
1704 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1705 and 64-bit architectures, the flags here refer to the subset that
1706 is available on both. */
1707 unsigned int flags;
1708
1709 /* The ASE_* flag used for instructions that are available on 64-bit
1710 architectures but that are not included in FLAGS. */
1711 unsigned int flags64;
1712
1713 /* The command-line options that turn the ASE on and off. */
1714 int option_on;
1715 int option_off;
1716
1717 /* The minimum required architecture revisions for MIPS32, MIPS64,
1718 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1719 int mips32_rev;
1720 int mips64_rev;
1721 int micromips32_rev;
1722 int micromips64_rev;
7361da2c
AB
1723
1724 /* The architecture where the ASE was removed or -1 if the extension has not
1725 been removed. */
1726 int rem_rev;
c6278170
RS
1727};
1728
1729/* A table of all supported ASEs. */
1730static const struct mips_ase mips_ases[] = {
1731 { "dsp", ASE_DSP, ASE_DSP64,
1732 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1733 2, 2, 2, 2,
1734 -1 },
c6278170
RS
1735
1736 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1737 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1738 2, 2, 2, 2,
1739 -1 },
c6278170 1740
8f4f9071
MF
1741 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1742 OPTION_DSPR3, OPTION_NO_DSPR3,
1743 6, 6, -1, -1,
1744 -1 },
1745
c6278170
RS
1746 { "eva", ASE_EVA, 0,
1747 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1748 2, 2, 2, 2,
1749 -1 },
c6278170
RS
1750
1751 { "mcu", ASE_MCU, 0,
1752 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1753 2, 2, 2, 2,
1754 -1 },
c6278170
RS
1755
1756 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1757 { "mdmx", ASE_MDMX, 0,
1758 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1759 -1, 1, -1, -1,
1760 6 },
c6278170
RS
1761
1762 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1763 { "mips3d", ASE_MIPS3D, 0,
1764 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1765 2, 1, -1, -1,
1766 6 },
c6278170
RS
1767
1768 { "mt", ASE_MT, 0,
1769 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1770 2, 2, -1, -1,
1771 -1 },
c6278170
RS
1772
1773 { "smartmips", ASE_SMARTMIPS, 0,
1774 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1775 1, -1, -1, -1,
1776 6 },
c6278170
RS
1777
1778 { "virt", ASE_VIRT, ASE_VIRT64,
1779 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1780 2, 2, 2, 2,
1781 -1 },
56d438b1
CF
1782
1783 { "msa", ASE_MSA, ASE_MSA64,
1784 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1785 2, 2, 2, 2,
1786 -1 },
7d64c587
AB
1787
1788 { "xpa", ASE_XPA, 0,
1789 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1790 2, 2, 2, 2,
7361da2c 1791 -1 },
25499ac7
MR
1792
1793 { "mips16e2", ASE_MIPS16E2, 0,
1794 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1795 2, 2, -1, -1,
1796 6 },
730c3174
SE
1797
1798 { "crc", ASE_CRC, ASE_CRC64,
1799 OPTION_CRC, OPTION_NO_CRC,
1800 6, 6, -1, -1,
1801 -1 },
6f20c942
FS
1802
1803 { "ginv", ASE_GINV, 0,
1804 OPTION_GINV, OPTION_NO_GINV,
1805 6, 6, 6, 6,
1806 -1 },
8095d2f7
CX
1807
1808 { "loongson-mmi", ASE_LOONGSON_MMI, 0,
1809 OPTION_LOONGSON_MMI, OPTION_NO_LOONGSON_MMI,
1810 0, 0, -1, -1,
1811 -1 },
716c08de
CX
1812
1813 { "loongson-cam", ASE_LOONGSON_CAM, 0,
1814 OPTION_LOONGSON_CAM, OPTION_NO_LOONGSON_CAM,
1815 0, 0, -1, -1,
1816 -1 },
bdc6c06e
CX
1817
1818 { "loongson-ext", ASE_LOONGSON_EXT, 0,
1819 OPTION_LOONGSON_EXT, OPTION_NO_LOONGSON_EXT,
1820 0, 0, -1, -1,
1821 -1 },
a693765e
CX
1822
1823 { "loongson-ext2", ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2, 0,
1824 OPTION_LOONGSON_EXT2, OPTION_NO_LOONGSON_EXT2,
1825 0, 0, -1, -1,
1826 -1 },
c6278170
RS
1827};
1828
1829/* The set of ASEs that require -mfp64. */
82bda27b 1830#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1831
1832/* Groups of ASE_* flags that represent different revisions of an ASE. */
1833static const unsigned int mips_ase_groups[] = {
a693765e
CX
1834 ASE_DSP | ASE_DSPR2 | ASE_DSPR3,
1835 ASE_LOONGSON_EXT | ASE_LOONGSON_EXT2
c6278170
RS
1836};
1837\f
252b5132
RH
1838/* Pseudo-op table.
1839
1840 The following pseudo-ops from the Kane and Heinrich MIPS book
1841 should be defined here, but are currently unsupported: .alias,
1842 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1843
1844 The following pseudo-ops from the Kane and Heinrich MIPS book are
1845 specific to the type of debugging information being generated, and
1846 should be defined by the object format: .aent, .begin, .bend,
1847 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1848 .vreg.
1849
1850 The following pseudo-ops from the Kane and Heinrich MIPS book are
1851 not MIPS CPU specific, but are also not specific to the object file
1852 format. This file is probably the best place to define them, but
d84bcf09 1853 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1854
e972090a
NC
1855static const pseudo_typeS mips_pseudo_table[] =
1856{
beae10d5 1857 /* MIPS specific pseudo-ops. */
252b5132
RH
1858 {"option", s_option, 0},
1859 {"set", s_mipsset, 0},
1860 {"rdata", s_change_sec, 'r'},
1861 {"sdata", s_change_sec, 's'},
1862 {"livereg", s_ignore, 0},
1863 {"abicalls", s_abicalls, 0},
1864 {"cpload", s_cpload, 0},
6478892d
TS
1865 {"cpsetup", s_cpsetup, 0},
1866 {"cplocal", s_cplocal, 0},
252b5132 1867 {"cprestore", s_cprestore, 0},
6478892d 1868 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1869 {"dtprelword", s_dtprelword, 0},
1870 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1871 {"tprelword", s_tprelword, 0},
1872 {"tpreldword", s_tpreldword, 0},
6478892d 1873 {"gpvalue", s_gpvalue, 0},
252b5132 1874 {"gpword", s_gpword, 0},
10181a0d 1875 {"gpdword", s_gpdword, 0},
a3f278e2 1876 {"ehword", s_ehword, 0},
252b5132
RH
1877 {"cpadd", s_cpadd, 0},
1878 {"insn", s_insn, 0},
ba92f887 1879 {"nan", s_nan, 0},
919731af 1880 {"module", s_module, 0},
252b5132 1881
beae10d5 1882 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1883 chips. */
38a57ae7 1884 {"asciiz", stringer, 8 + 1},
252b5132
RH
1885 {"bss", s_change_sec, 'b'},
1886 {"err", s_err, 0},
1887 {"half", s_cons, 1},
1888 {"dword", s_cons, 3},
1889 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1890 {"origin", s_org, 0},
1891 {"repeat", s_rept, 0},
252b5132 1892
998b3c36
MR
1893 /* For MIPS this is non-standard, but we define it for consistency. */
1894 {"sbss", s_change_sec, 'B'},
1895
beae10d5 1896 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1897 here for one reason or another. */
1898 {"align", s_align, 0},
1899 {"byte", s_cons, 0},
1900 {"data", s_change_sec, 'd'},
1901 {"double", s_float_cons, 'd'},
1902 {"float", s_float_cons, 'f'},
1903 {"globl", s_mips_globl, 0},
1904 {"global", s_mips_globl, 0},
1905 {"hword", s_cons, 1},
1906 {"int", s_cons, 2},
1907 {"long", s_cons, 2},
1908 {"octa", s_cons, 4},
1909 {"quad", s_cons, 3},
cca86cc8 1910 {"section", s_change_section, 0},
252b5132
RH
1911 {"short", s_cons, 1},
1912 {"single", s_float_cons, 'f'},
754e2bb9 1913 {"stabd", s_mips_stab, 'd'},
252b5132 1914 {"stabn", s_mips_stab, 'n'},
754e2bb9 1915 {"stabs", s_mips_stab, 's'},
252b5132
RH
1916 {"text", s_change_sec, 't'},
1917 {"word", s_cons, 2},
add56521 1918
add56521 1919 { "extern", ecoff_directive_extern, 0},
add56521 1920
43841e91 1921 { NULL, NULL, 0 },
252b5132
RH
1922};
1923
e972090a
NC
1924static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1925{
beae10d5
KH
1926 /* These pseudo-ops should be defined by the object file format.
1927 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1928 {"aent", s_mips_ent, 1},
1929 {"bgnb", s_ignore, 0},
1930 {"end", s_mips_end, 0},
1931 {"endb", s_ignore, 0},
1932 {"ent", s_mips_ent, 0},
c5dd6aab 1933 {"file", s_mips_file, 0},
252b5132
RH
1934 {"fmask", s_mips_mask, 'F'},
1935 {"frame", s_mips_frame, 0},
c5dd6aab 1936 {"loc", s_mips_loc, 0},
252b5132
RH
1937 {"mask", s_mips_mask, 'R'},
1938 {"verstamp", s_ignore, 0},
43841e91 1939 { NULL, NULL, 0 },
252b5132
RH
1940};
1941
3ae8dd8d
MR
1942/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1943 purpose of the `.dc.a' internal pseudo-op. */
1944
1945int
1946mips_address_bytes (void)
1947{
919731af 1948 file_mips_check_options ();
3ae8dd8d
MR
1949 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1950}
1951
17a2f251 1952extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1953
1954void
17a2f251 1955mips_pop_insert (void)
252b5132
RH
1956{
1957 pop_insert (mips_pseudo_table);
1958 if (! ECOFF_DEBUGGING)
1959 pop_insert (mips_nonecoff_pseudo_table);
1960}
1961\f
1962/* Symbols labelling the current insn. */
1963
e972090a
NC
1964struct insn_label_list
1965{
252b5132
RH
1966 struct insn_label_list *next;
1967 symbolS *label;
1968};
1969
252b5132 1970static struct insn_label_list *free_insn_labels;
742a56fe 1971#define label_list tc_segment_info_data.labels
252b5132 1972
17a2f251 1973static void mips_clear_insn_labels (void);
df58fc94
RS
1974static void mips_mark_labels (void);
1975static void mips_compressed_mark_labels (void);
252b5132
RH
1976
1977static inline void
17a2f251 1978mips_clear_insn_labels (void)
252b5132 1979{
ed9e98c2 1980 struct insn_label_list **pl;
a8dbcb85 1981 segment_info_type *si;
252b5132 1982
a8dbcb85
TS
1983 if (now_seg)
1984 {
1985 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1986 ;
3739860c 1987
a8dbcb85
TS
1988 si = seg_info (now_seg);
1989 *pl = si->label_list;
1990 si->label_list = NULL;
1991 }
252b5132 1992}
a8dbcb85 1993
df58fc94
RS
1994/* Mark instruction labels in MIPS16/microMIPS mode. */
1995
1996static inline void
1997mips_mark_labels (void)
1998{
1999 if (HAVE_CODE_COMPRESSION)
2000 mips_compressed_mark_labels ();
2001}
252b5132
RH
2002\f
2003static char *expr_end;
2004
e423441d 2005/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 2006 mips16_ip and when populated is always an O_constant. */
252b5132
RH
2007
2008static expressionS imm_expr;
252b5132 2009
77bd4346
RS
2010/* The relocatable field in an instruction and the relocs associated
2011 with it. These variables are used for instructions like LUI and
2012 JAL as well as true offsets. They are also used for address
2013 operands in macros. */
252b5132 2014
77bd4346 2015static expressionS offset_expr;
f6688943
TS
2016static bfd_reloc_code_real_type offset_reloc[3]
2017 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 2018
df58fc94
RS
2019/* This is set to the resulting size of the instruction to be produced
2020 by mips16_ip if an explicit extension is used or by mips_ip if an
2021 explicit size is supplied. */
252b5132 2022
df58fc94 2023static unsigned int forced_insn_length;
252b5132 2024
e1b47bd5
RS
2025/* True if we are assembling an instruction. All dot symbols defined during
2026 this time should be treated as code labels. */
2027
2028static bfd_boolean mips_assembling_insn;
2029
ecb4347a
DJ
2030/* The pdr segment for per procedure frame/regmask info. Not used for
2031 ECOFF debugging. */
252b5132
RH
2032
2033static segT pdr_seg;
252b5132 2034
e013f690
TS
2035/* The default target format to use. */
2036
aeffff67
RS
2037#if defined (TE_FreeBSD)
2038#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2039#elif defined (TE_TMIPS)
2040#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2041#else
2042#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2043#endif
2044
e013f690 2045const char *
17a2f251 2046mips_target_format (void)
e013f690
TS
2047{
2048 switch (OUTPUT_FLAVOR)
2049 {
e013f690 2050 case bfd_target_elf_flavour:
0a44bf69
RS
2051#ifdef TE_VXWORKS
2052 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2053 return (target_big_endian
2054 ? "elf32-bigmips-vxworks"
2055 : "elf32-littlemips-vxworks");
2056#endif
e013f690 2057 return (target_big_endian
cfe86eaa 2058 ? (HAVE_64BIT_OBJECTS
aeffff67 2059 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2060 : (HAVE_NEWABI
aeffff67
RS
2061 ? ELF_TARGET ("elf32-n", "big")
2062 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2063 : (HAVE_64BIT_OBJECTS
aeffff67 2064 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2065 : (HAVE_NEWABI
aeffff67
RS
2066 ? ELF_TARGET ("elf32-n", "little")
2067 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2068 default:
2069 abort ();
2070 return NULL;
2071 }
2072}
2073
c6278170
RS
2074/* Return the ISA revision that is currently in use, or 0 if we are
2075 generating code for MIPS V or below. */
2076
2077static int
2078mips_isa_rev (void)
2079{
2080 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2081 return 2;
2082
ae52f483
AB
2083 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2084 return 3;
2085
2086 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2087 return 5;
2088
7361da2c
AB
2089 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2090 return 6;
2091
c6278170
RS
2092 /* microMIPS implies revision 2 or above. */
2093 if (mips_opts.micromips)
2094 return 2;
2095
2096 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2097 return 1;
2098
2099 return 0;
2100}
2101
2102/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2103
2104static unsigned int
2105mips_ase_mask (unsigned int flags)
2106{
2107 unsigned int i;
2108
2109 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2110 if (flags & mips_ase_groups[i])
2111 flags |= mips_ase_groups[i];
2112 return flags;
2113}
2114
2115/* Check whether the current ISA supports ASE. Issue a warning if
2116 appropriate. */
2117
2118static void
2119mips_check_isa_supports_ase (const struct mips_ase *ase)
2120{
2121 const char *base;
2122 int min_rev, size;
2123 static unsigned int warned_isa;
2124 static unsigned int warned_fp32;
2125
2126 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2127 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2128 else
2129 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2130 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2131 && (warned_isa & ase->flags) != ase->flags)
2132 {
2133 warned_isa |= ase->flags;
2134 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2135 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2136 if (min_rev < 0)
1661c76c 2137 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2138 " `%s' extension"), size, base, ase->name);
2139 else
1661c76c 2140 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2141 ase->name, base, size, min_rev);
2142 }
7361da2c
AB
2143 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2144 && (warned_isa & ase->flags) != ase->flags)
2145 {
2146 warned_isa |= ase->flags;
2147 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2148 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2149 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2150 ase->name, base, size, ase->rem_rev);
2151 }
2152
c6278170 2153 if ((ase->flags & FP64_ASES)
0b35dfee 2154 && mips_opts.fp != 64
c6278170
RS
2155 && (warned_fp32 & ase->flags) != ase->flags)
2156 {
2157 warned_fp32 |= ase->flags;
1661c76c 2158 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2159 }
2160}
2161
2162/* Check all enabled ASEs to see whether they are supported by the
2163 chosen architecture. */
2164
2165static void
2166mips_check_isa_supports_ases (void)
2167{
2168 unsigned int i, mask;
2169
2170 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2171 {
2172 mask = mips_ase_mask (mips_ases[i].flags);
2173 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2174 mips_check_isa_supports_ase (&mips_ases[i]);
2175 }
2176}
2177
2178/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2179 that were affected. */
2180
2181static unsigned int
919731af 2182mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2183 bfd_boolean enabled_p)
c6278170
RS
2184{
2185 unsigned int mask;
2186
2187 mask = mips_ase_mask (ase->flags);
919731af 2188 opts->ase &= ~mask;
92cebb3d
MR
2189
2190 /* Clear combination ASE flags, which need to be recalculated based on
2191 updated regular ASE settings. */
9785fc2a 2192 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2193
c6278170 2194 if (enabled_p)
919731af 2195 opts->ase |= ase->flags;
25499ac7 2196
9785fc2a
MR
2197 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2198 instructions which are only valid when both ASEs are enabled.
2199 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2200 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2201 {
2202 opts->ase |= ASE_XPA_VIRT;
2203 mask |= ASE_XPA_VIRT;
2204 }
25499ac7
MR
2205 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2206 {
2207 opts->ase |= ASE_MIPS16E2_MT;
2208 mask |= ASE_MIPS16E2_MT;
2209 }
2210
c6278170
RS
2211 return mask;
2212}
2213
2214/* Return the ASE called NAME, or null if none. */
2215
2216static const struct mips_ase *
2217mips_lookup_ase (const char *name)
2218{
2219 unsigned int i;
2220
2221 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2222 if (strcmp (name, mips_ases[i].name) == 0)
2223 return &mips_ases[i];
2224 return NULL;
2225}
2226
df58fc94 2227/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2228 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2229 otherwise it is a 32-bit instruction. */
df58fc94
RS
2230
2231static inline unsigned int
2232micromips_insn_length (const struct mips_opcode *mo)
2233{
7fd53920 2234 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2235}
2236
5c04167a
RS
2237/* Return the length of MIPS16 instruction OPCODE. */
2238
2239static inline unsigned int
2240mips16_opcode_length (unsigned long opcode)
2241{
2242 return (opcode >> 16) == 0 ? 2 : 4;
2243}
2244
1e915849
RS
2245/* Return the length of instruction INSN. */
2246
2247static inline unsigned int
2248insn_length (const struct mips_cl_insn *insn)
2249{
df58fc94
RS
2250 if (mips_opts.micromips)
2251 return micromips_insn_length (insn->insn_mo);
2252 else if (mips_opts.mips16)
5c04167a 2253 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2254 else
1e915849 2255 return 4;
1e915849
RS
2256}
2257
2258/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2259
2260static void
2261create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2262{
2263 size_t i;
2264
2265 insn->insn_mo = mo;
1e915849
RS
2266 insn->insn_opcode = mo->match;
2267 insn->frag = NULL;
2268 insn->where = 0;
2269 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2270 insn->fixp[i] = NULL;
2271 insn->fixed_p = (mips_opts.noreorder > 0);
2272 insn->noreorder_p = (mips_opts.noreorder > 0);
2273 insn->mips16_absolute_jump_p = 0;
15be625d 2274 insn->complete_p = 0;
e407c74b 2275 insn->cleared_p = 0;
1e915849
RS
2276}
2277
fc76e730
RS
2278/* Get a list of all the operands in INSN. */
2279
2280static const struct mips_operand_array *
2281insn_operands (const struct mips_cl_insn *insn)
2282{
2283 if (insn->insn_mo >= &mips_opcodes[0]
2284 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2285 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2286
2287 if (insn->insn_mo >= &mips16_opcodes[0]
2288 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2289 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2290
2291 if (insn->insn_mo >= &micromips_opcodes[0]
2292 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2293 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2294
2295 abort ();
2296}
2297
2298/* Get a description of operand OPNO of INSN. */
2299
2300static const struct mips_operand *
2301insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2302{
2303 const struct mips_operand_array *operands;
2304
2305 operands = insn_operands (insn);
2306 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2307 abort ();
2308 return operands->operand[opno];
2309}
2310
e077a1c8
RS
2311/* Install UVAL as the value of OPERAND in INSN. */
2312
2313static inline void
2314insn_insert_operand (struct mips_cl_insn *insn,
2315 const struct mips_operand *operand, unsigned int uval)
2316{
25499ac7
MR
2317 if (mips_opts.mips16
2318 && operand->type == OP_INT && operand->lsb == 0
2319 && mips_opcode_32bit_p (insn->insn_mo))
2320 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2321 else
2322 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2323}
2324
fc76e730
RS
2325/* Extract the value of OPERAND from INSN. */
2326
2327static inline unsigned
2328insn_extract_operand (const struct mips_cl_insn *insn,
2329 const struct mips_operand *operand)
2330{
2331 return mips_extract_operand (operand, insn->insn_opcode);
2332}
2333
df58fc94 2334/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2335
2336static void
df58fc94 2337mips_record_compressed_mode (void)
742a56fe
RS
2338{
2339 segment_info_type *si;
2340
2341 si = seg_info (now_seg);
2342 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2343 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2344 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2345 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2346}
2347
4d68580a
RS
2348/* Read a standard MIPS instruction from BUF. */
2349
2350static unsigned long
2351read_insn (char *buf)
2352{
2353 if (target_big_endian)
2354 return bfd_getb32 ((bfd_byte *) buf);
2355 else
2356 return bfd_getl32 ((bfd_byte *) buf);
2357}
2358
2359/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2360 the next byte. */
2361
2362static char *
2363write_insn (char *buf, unsigned int insn)
2364{
2365 md_number_to_chars (buf, insn, 4);
2366 return buf + 4;
2367}
2368
2369/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2370 has length LENGTH. */
2371
2372static unsigned long
2373read_compressed_insn (char *buf, unsigned int length)
2374{
2375 unsigned long insn;
2376 unsigned int i;
2377
2378 insn = 0;
2379 for (i = 0; i < length; i += 2)
2380 {
2381 insn <<= 16;
2382 if (target_big_endian)
2383 insn |= bfd_getb16 ((char *) buf);
2384 else
2385 insn |= bfd_getl16 ((char *) buf);
2386 buf += 2;
2387 }
2388 return insn;
2389}
2390
5c04167a
RS
2391/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2392 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2393
2394static char *
2395write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2396{
2397 unsigned int i;
2398
2399 for (i = 0; i < length; i += 2)
2400 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2401 return buf + length;
2402}
2403
1e915849
RS
2404/* Install INSN at the location specified by its "frag" and "where" fields. */
2405
2406static void
2407install_insn (const struct mips_cl_insn *insn)
2408{
2409 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2410 if (HAVE_CODE_COMPRESSION)
2411 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2412 else
4d68580a 2413 write_insn (f, insn->insn_opcode);
df58fc94 2414 mips_record_compressed_mode ();
1e915849
RS
2415}
2416
2417/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2418 and install the opcode in the new location. */
2419
2420static void
2421move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2422{
2423 size_t i;
2424
2425 insn->frag = frag;
2426 insn->where = where;
2427 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2428 if (insn->fixp[i] != NULL)
2429 {
2430 insn->fixp[i]->fx_frag = frag;
2431 insn->fixp[i]->fx_where = where;
2432 }
2433 install_insn (insn);
2434}
2435
2436/* Add INSN to the end of the output. */
2437
2438static void
2439add_fixed_insn (struct mips_cl_insn *insn)
2440{
2441 char *f = frag_more (insn_length (insn));
2442 move_insn (insn, frag_now, f - frag_now->fr_literal);
2443}
2444
2445/* Start a variant frag and move INSN to the start of the variant part,
2446 marking it as fixed. The other arguments are as for frag_var. */
2447
2448static void
2449add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2450 relax_substateT subtype, symbolS *symbol, offsetT offset)
2451{
2452 frag_grow (max_chars);
2453 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2454 insn->fixed_p = 1;
2455 frag_var (rs_machine_dependent, max_chars, var,
2456 subtype, symbol, offset, NULL);
2457}
2458
2459/* Insert N copies of INSN into the history buffer, starting at
2460 position FIRST. Neither FIRST nor N need to be clipped. */
2461
2462static void
2463insert_into_history (unsigned int first, unsigned int n,
2464 const struct mips_cl_insn *insn)
2465{
2466 if (mips_relax.sequence != 2)
2467 {
2468 unsigned int i;
2469
2470 for (i = ARRAY_SIZE (history); i-- > first;)
2471 if (i >= first + n)
2472 history[i] = history[i - n];
2473 else
2474 history[i] = *insn;
2475 }
2476}
2477
e3de51ce
RS
2478/* Clear the error in insn_error. */
2479
2480static void
2481clear_insn_error (void)
2482{
2483 memset (&insn_error, 0, sizeof (insn_error));
2484}
2485
2486/* Possibly record error message MSG for the current instruction.
2487 If the error is about a particular argument, ARGNUM is the 1-based
2488 number of that argument, otherwise it is 0. FORMAT is the format
2489 of MSG. Return true if MSG was used, false if the current message
2490 was kept. */
2491
2492static bfd_boolean
2493set_insn_error_format (int argnum, enum mips_insn_error_format format,
2494 const char *msg)
2495{
2496 if (argnum == 0)
2497 {
2498 /* Give priority to errors against specific arguments, and to
2499 the first whole-instruction message. */
2500 if (insn_error.msg)
2501 return FALSE;
2502 }
2503 else
2504 {
2505 /* Keep insn_error if it is against a later argument. */
2506 if (argnum < insn_error.min_argnum)
2507 return FALSE;
2508
2509 /* If both errors are against the same argument but are different,
2510 give up on reporting a specific error for this argument.
2511 See the comment about mips_insn_error for details. */
2512 if (argnum == insn_error.min_argnum
2513 && insn_error.msg
2514 && strcmp (insn_error.msg, msg) != 0)
2515 {
2516 insn_error.msg = 0;
2517 insn_error.min_argnum += 1;
2518 return FALSE;
2519 }
2520 }
2521 insn_error.min_argnum = argnum;
2522 insn_error.format = format;
2523 insn_error.msg = msg;
2524 return TRUE;
2525}
2526
2527/* Record an instruction error with no % format fields. ARGNUM and MSG are
2528 as for set_insn_error_format. */
2529
2530static void
2531set_insn_error (int argnum, const char *msg)
2532{
2533 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2534}
2535
2536/* Record an instruction error with one %d field I. ARGNUM and MSG are
2537 as for set_insn_error_format. */
2538
2539static void
2540set_insn_error_i (int argnum, const char *msg, int i)
2541{
2542 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2543 insn_error.u.i = i;
2544}
2545
2546/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2547 are as for set_insn_error_format. */
2548
2549static void
2550set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2551{
2552 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2553 {
2554 insn_error.u.ss[0] = s1;
2555 insn_error.u.ss[1] = s2;
2556 }
2557}
2558
2559/* Report the error in insn_error, which is against assembly code STR. */
2560
2561static void
2562report_insn_error (const char *str)
2563{
e1fa0163 2564 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2565
e3de51ce
RS
2566 switch (insn_error.format)
2567 {
2568 case ERR_FMT_PLAIN:
2569 as_bad (msg, str);
2570 break;
2571
2572 case ERR_FMT_I:
2573 as_bad (msg, insn_error.u.i, str);
2574 break;
2575
2576 case ERR_FMT_SS:
2577 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2578 break;
2579 }
e1fa0163
NC
2580
2581 free ((char *) msg);
e3de51ce
RS
2582}
2583
71400594
RS
2584/* Initialize vr4120_conflicts. There is a bit of duplication here:
2585 the idea is to make it obvious at a glance that each errata is
2586 included. */
2587
2588static void
2589init_vr4120_conflicts (void)
2590{
2591#define CONFLICT(FIRST, SECOND) \
2592 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2593
2594 /* Errata 21 - [D]DIV[U] after [D]MACC */
2595 CONFLICT (MACC, DIV);
2596 CONFLICT (DMACC, DIV);
2597
2598 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2599 CONFLICT (DMULT, DMULT);
2600 CONFLICT (DMULT, DMACC);
2601 CONFLICT (DMACC, DMULT);
2602 CONFLICT (DMACC, DMACC);
2603
2604 /* Errata 24 - MT{LO,HI} after [D]MACC */
2605 CONFLICT (MACC, MTHILO);
2606 CONFLICT (DMACC, MTHILO);
2607
2608 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2609 instruction is executed immediately after a MACC or DMACC
2610 instruction, the result of [either instruction] is incorrect." */
2611 CONFLICT (MACC, MULT);
2612 CONFLICT (MACC, DMULT);
2613 CONFLICT (DMACC, MULT);
2614 CONFLICT (DMACC, DMULT);
2615
2616 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2617 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2618 DDIV or DDIVU instruction, the result of the MACC or
2619 DMACC instruction is incorrect.". */
2620 CONFLICT (DMULT, MACC);
2621 CONFLICT (DMULT, DMACC);
2622 CONFLICT (DIV, MACC);
2623 CONFLICT (DIV, DMACC);
2624
2625#undef CONFLICT
2626}
2627
707bfff6
TS
2628struct regname {
2629 const char *name;
2630 unsigned int num;
2631};
2632
14daeee3 2633#define RNUM_MASK 0x00000ff
56d438b1 2634#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2635#define RTYPE_NUM 0x0000100
2636#define RTYPE_FPU 0x0000200
2637#define RTYPE_FCC 0x0000400
2638#define RTYPE_VEC 0x0000800
2639#define RTYPE_GP 0x0001000
2640#define RTYPE_CP0 0x0002000
2641#define RTYPE_PC 0x0004000
2642#define RTYPE_ACC 0x0008000
2643#define RTYPE_CCC 0x0010000
2644#define RTYPE_VI 0x0020000
2645#define RTYPE_VF 0x0040000
2646#define RTYPE_R5900_I 0x0080000
2647#define RTYPE_R5900_Q 0x0100000
2648#define RTYPE_R5900_R 0x0200000
2649#define RTYPE_R5900_ACC 0x0400000
56d438b1 2650#define RTYPE_MSA 0x0800000
14daeee3 2651#define RWARN 0x8000000
707bfff6
TS
2652
2653#define GENERIC_REGISTER_NUMBERS \
2654 {"$0", RTYPE_NUM | 0}, \
2655 {"$1", RTYPE_NUM | 1}, \
2656 {"$2", RTYPE_NUM | 2}, \
2657 {"$3", RTYPE_NUM | 3}, \
2658 {"$4", RTYPE_NUM | 4}, \
2659 {"$5", RTYPE_NUM | 5}, \
2660 {"$6", RTYPE_NUM | 6}, \
2661 {"$7", RTYPE_NUM | 7}, \
2662 {"$8", RTYPE_NUM | 8}, \
2663 {"$9", RTYPE_NUM | 9}, \
2664 {"$10", RTYPE_NUM | 10}, \
2665 {"$11", RTYPE_NUM | 11}, \
2666 {"$12", RTYPE_NUM | 12}, \
2667 {"$13", RTYPE_NUM | 13}, \
2668 {"$14", RTYPE_NUM | 14}, \
2669 {"$15", RTYPE_NUM | 15}, \
2670 {"$16", RTYPE_NUM | 16}, \
2671 {"$17", RTYPE_NUM | 17}, \
2672 {"$18", RTYPE_NUM | 18}, \
2673 {"$19", RTYPE_NUM | 19}, \
2674 {"$20", RTYPE_NUM | 20}, \
2675 {"$21", RTYPE_NUM | 21}, \
2676 {"$22", RTYPE_NUM | 22}, \
2677 {"$23", RTYPE_NUM | 23}, \
2678 {"$24", RTYPE_NUM | 24}, \
2679 {"$25", RTYPE_NUM | 25}, \
2680 {"$26", RTYPE_NUM | 26}, \
2681 {"$27", RTYPE_NUM | 27}, \
2682 {"$28", RTYPE_NUM | 28}, \
2683 {"$29", RTYPE_NUM | 29}, \
2684 {"$30", RTYPE_NUM | 30}, \
3739860c 2685 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2686
2687#define FPU_REGISTER_NAMES \
2688 {"$f0", RTYPE_FPU | 0}, \
2689 {"$f1", RTYPE_FPU | 1}, \
2690 {"$f2", RTYPE_FPU | 2}, \
2691 {"$f3", RTYPE_FPU | 3}, \
2692 {"$f4", RTYPE_FPU | 4}, \
2693 {"$f5", RTYPE_FPU | 5}, \
2694 {"$f6", RTYPE_FPU | 6}, \
2695 {"$f7", RTYPE_FPU | 7}, \
2696 {"$f8", RTYPE_FPU | 8}, \
2697 {"$f9", RTYPE_FPU | 9}, \
2698 {"$f10", RTYPE_FPU | 10}, \
2699 {"$f11", RTYPE_FPU | 11}, \
2700 {"$f12", RTYPE_FPU | 12}, \
2701 {"$f13", RTYPE_FPU | 13}, \
2702 {"$f14", RTYPE_FPU | 14}, \
2703 {"$f15", RTYPE_FPU | 15}, \
2704 {"$f16", RTYPE_FPU | 16}, \
2705 {"$f17", RTYPE_FPU | 17}, \
2706 {"$f18", RTYPE_FPU | 18}, \
2707 {"$f19", RTYPE_FPU | 19}, \
2708 {"$f20", RTYPE_FPU | 20}, \
2709 {"$f21", RTYPE_FPU | 21}, \
2710 {"$f22", RTYPE_FPU | 22}, \
2711 {"$f23", RTYPE_FPU | 23}, \
2712 {"$f24", RTYPE_FPU | 24}, \
2713 {"$f25", RTYPE_FPU | 25}, \
2714 {"$f26", RTYPE_FPU | 26}, \
2715 {"$f27", RTYPE_FPU | 27}, \
2716 {"$f28", RTYPE_FPU | 28}, \
2717 {"$f29", RTYPE_FPU | 29}, \
2718 {"$f30", RTYPE_FPU | 30}, \
2719 {"$f31", RTYPE_FPU | 31}
2720
2721#define FPU_CONDITION_CODE_NAMES \
2722 {"$fcc0", RTYPE_FCC | 0}, \
2723 {"$fcc1", RTYPE_FCC | 1}, \
2724 {"$fcc2", RTYPE_FCC | 2}, \
2725 {"$fcc3", RTYPE_FCC | 3}, \
2726 {"$fcc4", RTYPE_FCC | 4}, \
2727 {"$fcc5", RTYPE_FCC | 5}, \
2728 {"$fcc6", RTYPE_FCC | 6}, \
2729 {"$fcc7", RTYPE_FCC | 7}
2730
2731#define COPROC_CONDITION_CODE_NAMES \
2732 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2733 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2734 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2735 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2736 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2737 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2738 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2739 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2740
2741#define N32N64_SYMBOLIC_REGISTER_NAMES \
2742 {"$a4", RTYPE_GP | 8}, \
2743 {"$a5", RTYPE_GP | 9}, \
2744 {"$a6", RTYPE_GP | 10}, \
2745 {"$a7", RTYPE_GP | 11}, \
2746 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2747 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2748 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2749 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2750 {"$t0", RTYPE_GP | 12}, \
2751 {"$t1", RTYPE_GP | 13}, \
2752 {"$t2", RTYPE_GP | 14}, \
2753 {"$t3", RTYPE_GP | 15}
2754
2755#define O32_SYMBOLIC_REGISTER_NAMES \
2756 {"$t0", RTYPE_GP | 8}, \
2757 {"$t1", RTYPE_GP | 9}, \
2758 {"$t2", RTYPE_GP | 10}, \
2759 {"$t3", RTYPE_GP | 11}, \
2760 {"$t4", RTYPE_GP | 12}, \
2761 {"$t5", RTYPE_GP | 13}, \
2762 {"$t6", RTYPE_GP | 14}, \
2763 {"$t7", RTYPE_GP | 15}, \
2764 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2765 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2766 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2767 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2768
2769/* Remaining symbolic register names */
2770#define SYMBOLIC_REGISTER_NAMES \
2771 {"$zero", RTYPE_GP | 0}, \
2772 {"$at", RTYPE_GP | 1}, \
2773 {"$AT", RTYPE_GP | 1}, \
2774 {"$v0", RTYPE_GP | 2}, \
2775 {"$v1", RTYPE_GP | 3}, \
2776 {"$a0", RTYPE_GP | 4}, \
2777 {"$a1", RTYPE_GP | 5}, \
2778 {"$a2", RTYPE_GP | 6}, \
2779 {"$a3", RTYPE_GP | 7}, \
2780 {"$s0", RTYPE_GP | 16}, \
2781 {"$s1", RTYPE_GP | 17}, \
2782 {"$s2", RTYPE_GP | 18}, \
2783 {"$s3", RTYPE_GP | 19}, \
2784 {"$s4", RTYPE_GP | 20}, \
2785 {"$s5", RTYPE_GP | 21}, \
2786 {"$s6", RTYPE_GP | 22}, \
2787 {"$s7", RTYPE_GP | 23}, \
2788 {"$t8", RTYPE_GP | 24}, \
2789 {"$t9", RTYPE_GP | 25}, \
2790 {"$k0", RTYPE_GP | 26}, \
2791 {"$kt0", RTYPE_GP | 26}, \
2792 {"$k1", RTYPE_GP | 27}, \
2793 {"$kt1", RTYPE_GP | 27}, \
2794 {"$gp", RTYPE_GP | 28}, \
2795 {"$sp", RTYPE_GP | 29}, \
2796 {"$s8", RTYPE_GP | 30}, \
2797 {"$fp", RTYPE_GP | 30}, \
2798 {"$ra", RTYPE_GP | 31}
2799
2800#define MIPS16_SPECIAL_REGISTER_NAMES \
2801 {"$pc", RTYPE_PC | 0}
2802
2803#define MDMX_VECTOR_REGISTER_NAMES \
2804 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2805 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2806 {"$v2", RTYPE_VEC | 2}, \
2807 {"$v3", RTYPE_VEC | 3}, \
2808 {"$v4", RTYPE_VEC | 4}, \
2809 {"$v5", RTYPE_VEC | 5}, \
2810 {"$v6", RTYPE_VEC | 6}, \
2811 {"$v7", RTYPE_VEC | 7}, \
2812 {"$v8", RTYPE_VEC | 8}, \
2813 {"$v9", RTYPE_VEC | 9}, \
2814 {"$v10", RTYPE_VEC | 10}, \
2815 {"$v11", RTYPE_VEC | 11}, \
2816 {"$v12", RTYPE_VEC | 12}, \
2817 {"$v13", RTYPE_VEC | 13}, \
2818 {"$v14", RTYPE_VEC | 14}, \
2819 {"$v15", RTYPE_VEC | 15}, \
2820 {"$v16", RTYPE_VEC | 16}, \
2821 {"$v17", RTYPE_VEC | 17}, \
2822 {"$v18", RTYPE_VEC | 18}, \
2823 {"$v19", RTYPE_VEC | 19}, \
2824 {"$v20", RTYPE_VEC | 20}, \
2825 {"$v21", RTYPE_VEC | 21}, \
2826 {"$v22", RTYPE_VEC | 22}, \
2827 {"$v23", RTYPE_VEC | 23}, \
2828 {"$v24", RTYPE_VEC | 24}, \
2829 {"$v25", RTYPE_VEC | 25}, \
2830 {"$v26", RTYPE_VEC | 26}, \
2831 {"$v27", RTYPE_VEC | 27}, \
2832 {"$v28", RTYPE_VEC | 28}, \
2833 {"$v29", RTYPE_VEC | 29}, \
2834 {"$v30", RTYPE_VEC | 30}, \
2835 {"$v31", RTYPE_VEC | 31}
2836
14daeee3
RS
2837#define R5900_I_NAMES \
2838 {"$I", RTYPE_R5900_I | 0}
2839
2840#define R5900_Q_NAMES \
2841 {"$Q", RTYPE_R5900_Q | 0}
2842
2843#define R5900_R_NAMES \
2844 {"$R", RTYPE_R5900_R | 0}
2845
2846#define R5900_ACC_NAMES \
2847 {"$ACC", RTYPE_R5900_ACC | 0 }
2848
707bfff6
TS
2849#define MIPS_DSP_ACCUMULATOR_NAMES \
2850 {"$ac0", RTYPE_ACC | 0}, \
2851 {"$ac1", RTYPE_ACC | 1}, \
2852 {"$ac2", RTYPE_ACC | 2}, \
2853 {"$ac3", RTYPE_ACC | 3}
2854
2855static const struct regname reg_names[] = {
2856 GENERIC_REGISTER_NUMBERS,
2857 FPU_REGISTER_NAMES,
2858 FPU_CONDITION_CODE_NAMES,
2859 COPROC_CONDITION_CODE_NAMES,
2860
2861 /* The $txx registers depends on the abi,
2862 these will be added later into the symbol table from
3739860c 2863 one of the tables below once mips_abi is set after
707bfff6
TS
2864 parsing of arguments from the command line. */
2865 SYMBOLIC_REGISTER_NAMES,
2866
2867 MIPS16_SPECIAL_REGISTER_NAMES,
2868 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2869 R5900_I_NAMES,
2870 R5900_Q_NAMES,
2871 R5900_R_NAMES,
2872 R5900_ACC_NAMES,
707bfff6
TS
2873 MIPS_DSP_ACCUMULATOR_NAMES,
2874 {0, 0}
2875};
2876
2877static const struct regname reg_names_o32[] = {
2878 O32_SYMBOLIC_REGISTER_NAMES,
2879 {0, 0}
2880};
2881
2882static const struct regname reg_names_n32n64[] = {
2883 N32N64_SYMBOLIC_REGISTER_NAMES,
2884 {0, 0}
2885};
2886
a92713e6
RS
2887/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2888 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2889 of these register symbols, return the associated vector register,
2890 otherwise return SYMVAL itself. */
df58fc94 2891
a92713e6
RS
2892static unsigned int
2893mips_prefer_vec_regno (unsigned int symval)
707bfff6 2894{
a92713e6
RS
2895 if ((symval & -2) == (RTYPE_GP | 2))
2896 return RTYPE_VEC | (symval & 1);
2897 return symval;
2898}
2899
14daeee3
RS
2900/* Return true if string [S, E) is a valid register name, storing its
2901 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2902
2903static bfd_boolean
14daeee3 2904mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2905{
707bfff6 2906 char save_c;
14daeee3 2907 symbolS *symbol;
707bfff6
TS
2908
2909 /* Terminate name. */
2910 save_c = *e;
2911 *e = '\0';
2912
a92713e6
RS
2913 /* Look up the name. */
2914 symbol = symbol_find (s);
2915 *e = save_c;
2916
2917 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2918 return FALSE;
2919
14daeee3
RS
2920 *symval_ptr = S_GET_VALUE (symbol);
2921 return TRUE;
2922}
2923
2924/* Return true if the string at *SPTR is a valid register name. Allow it
2925 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2926 is nonnull.
2927
2928 When returning true, move *SPTR past the register, store the
2929 register's symbol value in *SYMVAL_PTR and the channel mask in
2930 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2931 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2932 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2933
2934static bfd_boolean
2935mips_parse_register (char **sptr, unsigned int *symval_ptr,
2936 unsigned int *channels_ptr)
2937{
2938 char *s, *e, *m;
2939 const char *q;
2940 unsigned int channels, symval, bit;
2941
2942 /* Find end of name. */
2943 s = e = *sptr;
2944 if (is_name_beginner (*e))
2945 ++e;
2946 while (is_part_of_name (*e))
2947 ++e;
2948
2949 channels = 0;
2950 if (!mips_parse_register_1 (s, e, &symval))
2951 {
2952 if (!channels_ptr)
2953 return FALSE;
2954
2955 /* Eat characters from the end of the string that are valid
2956 channel suffixes. The preceding register must be $ACC or
2957 end with a digit, so there is no ambiguity. */
2958 bit = 1;
2959 m = e;
2960 for (q = "wzyx"; *q; q++, bit <<= 1)
2961 if (m > s && m[-1] == *q)
2962 {
2963 --m;
2964 channels |= bit;
2965 }
2966
2967 if (channels == 0
2968 || !mips_parse_register_1 (s, m, &symval)
2969 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2970 return FALSE;
2971 }
2972
a92713e6 2973 *sptr = e;
14daeee3
RS
2974 *symval_ptr = symval;
2975 if (channels_ptr)
2976 *channels_ptr = channels;
a92713e6
RS
2977 return TRUE;
2978}
2979
2980/* Check if SPTR points at a valid register specifier according to TYPES.
2981 If so, then return 1, advance S to consume the specifier and store
2982 the register's number in REGNOP, otherwise return 0. */
2983
2984static int
2985reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2986{
2987 unsigned int regno;
2988
14daeee3 2989 if (mips_parse_register (s, &regno, NULL))
707bfff6 2990 {
a92713e6
RS
2991 if (types & RTYPE_VEC)
2992 regno = mips_prefer_vec_regno (regno);
2993 if (regno & types)
2994 regno &= RNUM_MASK;
2995 else
2996 regno = ~0;
707bfff6 2997 }
a92713e6 2998 else
707bfff6 2999 {
a92713e6 3000 if (types & RWARN)
1661c76c 3001 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 3002 regno = ~0;
707bfff6 3003 }
707bfff6 3004 if (regnop)
a92713e6
RS
3005 *regnop = regno;
3006 return regno <= RNUM_MASK;
707bfff6
TS
3007}
3008
14daeee3
RS
3009/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
3010 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
3011
3012static char *
3013mips_parse_vu0_channels (char *s, unsigned int *channels)
3014{
3015 unsigned int i;
3016
3017 *channels = 0;
3018 for (i = 0; i < 4; i++)
3019 if (*s == "xyzw"[i])
3020 {
3021 *channels |= 1 << (3 - i);
3022 ++s;
3023 }
3024 return s;
3025}
3026
a92713e6
RS
3027/* Token types for parsed operand lists. */
3028enum mips_operand_token_type {
3029 /* A plain register, e.g. $f2. */
3030 OT_REG,
df58fc94 3031
14daeee3
RS
3032 /* A 4-bit XYZW channel mask. */
3033 OT_CHANNELS,
3034
56d438b1
CF
3035 /* A constant vector index, e.g. [1]. */
3036 OT_INTEGER_INDEX,
3037
3038 /* A register vector index, e.g. [$2]. */
3039 OT_REG_INDEX,
df58fc94 3040
a92713e6
RS
3041 /* A continuous range of registers, e.g. $s0-$s4. */
3042 OT_REG_RANGE,
3043
3044 /* A (possibly relocated) expression. */
3045 OT_INTEGER,
3046
3047 /* A floating-point value. */
3048 OT_FLOAT,
3049
3050 /* A single character. This can be '(', ')' or ',', but '(' only appears
3051 before OT_REGs. */
3052 OT_CHAR,
3053
14daeee3
RS
3054 /* A doubled character, either "--" or "++". */
3055 OT_DOUBLE_CHAR,
3056
a92713e6
RS
3057 /* The end of the operand list. */
3058 OT_END
3059};
3060
3061/* A parsed operand token. */
3062struct mips_operand_token
3063{
3064 /* The type of token. */
3065 enum mips_operand_token_type type;
3066 union
3067 {
56d438b1 3068 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3069 unsigned int regno;
3070
14daeee3
RS
3071 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3072 unsigned int channels;
3073
56d438b1
CF
3074 /* The integer value of an OT_INTEGER_INDEX. */
3075 addressT index;
a92713e6
RS
3076
3077 /* The two register symbol values involved in an OT_REG_RANGE. */
3078 struct {
3079 unsigned int regno1;
3080 unsigned int regno2;
3081 } reg_range;
3082
3083 /* The value of an OT_INTEGER. The value is represented as an
3084 expression and the relocation operators that were applied to
3085 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3086 relocation operators were used. */
3087 struct {
3088 expressionS value;
3089 bfd_reloc_code_real_type relocs[3];
3090 } integer;
3091
3092 /* The binary data for an OT_FLOAT constant, and the number of bytes
3093 in the constant. */
3094 struct {
3095 unsigned char data[8];
3096 int length;
3097 } flt;
3098
14daeee3 3099 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3100 char ch;
3101 } u;
3102};
3103
3104/* An obstack used to construct lists of mips_operand_tokens. */
3105static struct obstack mips_operand_tokens;
3106
3107/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3108
3109static void
3110mips_add_token (struct mips_operand_token *token,
3111 enum mips_operand_token_type type)
3112{
3113 token->type = type;
3114 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3115}
3116
3117/* Check whether S is '(' followed by a register name. Add OT_CHAR
3118 and OT_REG tokens for them if so, and return a pointer to the first
3119 unconsumed character. Return null otherwise. */
3120
3121static char *
3122mips_parse_base_start (char *s)
3123{
3124 struct mips_operand_token token;
14daeee3
RS
3125 unsigned int regno, channels;
3126 bfd_boolean decrement_p;
df58fc94 3127
a92713e6
RS
3128 if (*s != '(')
3129 return 0;
3130
3131 ++s;
3132 SKIP_SPACE_TABS (s);
14daeee3
RS
3133
3134 /* Only match "--" as part of a base expression. In other contexts "--X"
3135 is a double negative. */
3136 decrement_p = (s[0] == '-' && s[1] == '-');
3137 if (decrement_p)
3138 {
3139 s += 2;
3140 SKIP_SPACE_TABS (s);
3141 }
3142
3143 /* Allow a channel specifier because that leads to better error messages
3144 than treating something like "$vf0x++" as an expression. */
3145 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3146 return 0;
3147
3148 token.u.ch = '(';
3149 mips_add_token (&token, OT_CHAR);
3150
14daeee3
RS
3151 if (decrement_p)
3152 {
3153 token.u.ch = '-';
3154 mips_add_token (&token, OT_DOUBLE_CHAR);
3155 }
3156
a92713e6
RS
3157 token.u.regno = regno;
3158 mips_add_token (&token, OT_REG);
3159
14daeee3
RS
3160 if (channels)
3161 {
3162 token.u.channels = channels;
3163 mips_add_token (&token, OT_CHANNELS);
3164 }
3165
3166 /* For consistency, only match "++" as part of base expressions too. */
3167 SKIP_SPACE_TABS (s);
3168 if (s[0] == '+' && s[1] == '+')
3169 {
3170 s += 2;
3171 token.u.ch = '+';
3172 mips_add_token (&token, OT_DOUBLE_CHAR);
3173 }
3174
a92713e6
RS
3175 return s;
3176}
3177
3178/* Parse one or more tokens from S. Return a pointer to the first
3179 unconsumed character on success. Return null if an error was found
3180 and store the error text in insn_error. FLOAT_FORMAT is as for
3181 mips_parse_arguments. */
3182
3183static char *
3184mips_parse_argument_token (char *s, char float_format)
3185{
6d4af3c2
AM
3186 char *end, *save_in;
3187 const char *err;
14daeee3 3188 unsigned int regno1, regno2, channels;
a92713e6
RS
3189 struct mips_operand_token token;
3190
3191 /* First look for "($reg", since we want to treat that as an
3192 OT_CHAR and OT_REG rather than an expression. */
3193 end = mips_parse_base_start (s);
3194 if (end)
3195 return end;
3196
3197 /* Handle other characters that end up as OT_CHARs. */
3198 if (*s == ')' || *s == ',')
3199 {
3200 token.u.ch = *s;
3201 mips_add_token (&token, OT_CHAR);
3202 ++s;
3203 return s;
3204 }
3205
3206 /* Handle tokens that start with a register. */
14daeee3 3207 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3208 {
14daeee3
RS
3209 if (channels)
3210 {
3211 /* A register and a VU0 channel suffix. */
3212 token.u.regno = regno1;
3213 mips_add_token (&token, OT_REG);
3214
3215 token.u.channels = channels;
3216 mips_add_token (&token, OT_CHANNELS);
3217 return s;
3218 }
3219
a92713e6
RS
3220 SKIP_SPACE_TABS (s);
3221 if (*s == '-')
df58fc94 3222 {
a92713e6
RS
3223 /* A register range. */
3224 ++s;
3225 SKIP_SPACE_TABS (s);
14daeee3 3226 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3227 {
1661c76c 3228 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3229 return 0;
3230 }
df58fc94 3231
a92713e6
RS
3232 token.u.reg_range.regno1 = regno1;
3233 token.u.reg_range.regno2 = regno2;
3234 mips_add_token (&token, OT_REG_RANGE);
3235 return s;
3236 }
a92713e6 3237
56d438b1
CF
3238 /* Add the register itself. */
3239 token.u.regno = regno1;
3240 mips_add_token (&token, OT_REG);
3241
3242 /* Check for a vector index. */
3243 if (*s == '[')
3244 {
a92713e6
RS
3245 ++s;
3246 SKIP_SPACE_TABS (s);
56d438b1
CF
3247 if (mips_parse_register (&s, &token.u.regno, NULL))
3248 mips_add_token (&token, OT_REG_INDEX);
3249 else
a92713e6 3250 {
56d438b1
CF
3251 expressionS element;
3252
3253 my_getExpression (&element, s);
3254 if (element.X_op != O_constant)
3255 {
3256 set_insn_error (0, _("vector element must be constant"));
3257 return 0;
3258 }
3259 s = expr_end;
3260 token.u.index = element.X_add_number;
3261 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3262 }
a92713e6
RS
3263 SKIP_SPACE_TABS (s);
3264 if (*s != ']')
3265 {
1661c76c 3266 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3267 return 0;
3268 }
3269 ++s;
df58fc94 3270 }
a92713e6 3271 return s;
df58fc94
RS
3272 }
3273
a92713e6
RS
3274 if (float_format)
3275 {
3276 /* First try to treat expressions as floats. */
3277 save_in = input_line_pointer;
3278 input_line_pointer = s;
3279 err = md_atof (float_format, (char *) token.u.flt.data,
3280 &token.u.flt.length);
3281 end = input_line_pointer;
3282 input_line_pointer = save_in;
3283 if (err && *err)
3284 {
e3de51ce 3285 set_insn_error (0, err);
a92713e6
RS
3286 return 0;
3287 }
3288 if (s != end)
3289 {
3290 mips_add_token (&token, OT_FLOAT);
3291 return end;
3292 }
3293 }
3294
3295 /* Treat everything else as an integer expression. */
3296 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3297 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3298 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3299 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3300 s = expr_end;
3301 mips_add_token (&token, OT_INTEGER);
3302 return s;
3303}
3304
3305/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3306 if expressions should be treated as 32-bit floating-point constants,
3307 'd' if they should be treated as 64-bit floating-point constants,
3308 or 0 if they should be treated as integer expressions (the usual case).
3309
3310 Return a list of tokens on success, otherwise return 0. The caller
3311 must obstack_free the list after use. */
3312
3313static struct mips_operand_token *
3314mips_parse_arguments (char *s, char float_format)
3315{
3316 struct mips_operand_token token;
3317
3318 SKIP_SPACE_TABS (s);
3319 while (*s)
3320 {
3321 s = mips_parse_argument_token (s, float_format);
3322 if (!s)
3323 {
3324 obstack_free (&mips_operand_tokens,
3325 obstack_finish (&mips_operand_tokens));
3326 return 0;
3327 }
3328 SKIP_SPACE_TABS (s);
3329 }
3330 mips_add_token (&token, OT_END);
3331 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3332}
3333
d301a56b
RS
3334/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3335 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3336
3337static bfd_boolean
f79e2745 3338is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3339{
3340 int isa = mips_opts.isa;
846ef2d0 3341 int ase = mips_opts.ase;
037b32b9 3342 int fp_s, fp_d;
c6278170 3343 unsigned int i;
037b32b9 3344
be0fcbee 3345 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3346 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3347 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3348 ase |= mips_ases[i].flags64;
037b32b9 3349
d301a56b 3350 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3351 return FALSE;
3352
3353 /* Check whether the instruction or macro requires single-precision or
3354 double-precision floating-point support. Note that this information is
3355 stored differently in the opcode table for insns and macros. */
3356 if (mo->pinfo == INSN_MACRO)
3357 {
3358 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3359 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3360 }
3361 else
3362 {
3363 fp_s = mo->pinfo & FP_S;
3364 fp_d = mo->pinfo & FP_D;
3365 }
3366
3367 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3368 return FALSE;
3369
3370 if (fp_s && mips_opts.soft_float)
3371 return FALSE;
3372
3373 return TRUE;
3374}
3375
3376/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3377 selected ISA and architecture. */
3378
3379static bfd_boolean
3380is_opcode_valid_16 (const struct mips_opcode *mo)
3381{
25499ac7
MR
3382 int isa = mips_opts.isa;
3383 int ase = mips_opts.ase;
3384 unsigned int i;
3385
3386 if (ISA_HAS_64BIT_REGS (isa))
3387 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3388 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3389 ase |= mips_ases[i].flags64;
3390
3391 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3392}
3393
df58fc94 3394/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3395 explicitly requested. Always TRUE in the standard MIPS mode.
3396 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3397
3398static bfd_boolean
3399is_size_valid (const struct mips_opcode *mo)
3400{
3401 if (!mips_opts.micromips)
3402 return TRUE;
3403
833794fc
MR
3404 if (mips_opts.insn32)
3405 {
3406 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3407 return FALSE;
3408 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3409 return FALSE;
3410 }
df58fc94
RS
3411 if (!forced_insn_length)
3412 return TRUE;
3413 if (mo->pinfo == INSN_MACRO)
3414 return FALSE;
3415 return forced_insn_length == micromips_insn_length (mo);
3416}
3417
7fd53920
MR
3418/* Return TRUE if the size of the MIPS16 opcode MO matches one
3419 explicitly requested. */
3420
3421static bfd_boolean
3422is_size_valid_16 (const struct mips_opcode *mo)
3423{
3424 if (!forced_insn_length)
3425 return TRUE;
3426 if (mo->pinfo == INSN_MACRO)
3427 return FALSE;
3428 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3429 return FALSE;
0674ee5d
MR
3430 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3431 return FALSE;
7fd53920
MR
3432 return TRUE;
3433}
3434
df58fc94 3435/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3436 of the preceding instruction. Always TRUE in the standard MIPS mode.
3437
3438 We don't accept macros in 16-bit delay slots to avoid a case where
3439 a macro expansion fails because it relies on a preceding 32-bit real
3440 instruction to have matched and does not handle the operands correctly.
3441 The only macros that may expand to 16-bit instructions are JAL that
3442 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3443 and BGT (that likewise cannot be placed in a delay slot) that decay to
3444 a NOP. In all these cases the macros precede any corresponding real
3445 instruction definitions in the opcode table, so they will match in the
3446 second pass where the size of the delay slot is ignored and therefore
3447 produce correct code. */
df58fc94
RS
3448
3449static bfd_boolean
3450is_delay_slot_valid (const struct mips_opcode *mo)
3451{
3452 if (!mips_opts.micromips)
3453 return TRUE;
3454
3455 if (mo->pinfo == INSN_MACRO)
c06dec14 3456 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3457 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3458 && micromips_insn_length (mo) != 4)
3459 return FALSE;
3460 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3461 && micromips_insn_length (mo) != 2)
3462 return FALSE;
3463
3464 return TRUE;
3465}
3466
fc76e730
RS
3467/* For consistency checking, verify that all bits of OPCODE are specified
3468 either by the match/mask part of the instruction definition, or by the
3469 operand list. Also build up a list of operands in OPERANDS.
3470
3471 INSN_BITS says which bits of the instruction are significant.
3472 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3473 provides the mips_operand description of each operand. DECODE_OPERAND
3474 is null for MIPS16 instructions. */
ab902481
RS
3475
3476static int
3477validate_mips_insn (const struct mips_opcode *opcode,
3478 unsigned long insn_bits,
fc76e730
RS
3479 const struct mips_operand *(*decode_operand) (const char *),
3480 struct mips_operand_array *operands)
ab902481
RS
3481{
3482 const char *s;
fc76e730 3483 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3484 const struct mips_operand *operand;
3485
fc76e730
RS
3486 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3487 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3488 {
3489 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3490 opcode->name, opcode->args);
3491 return 0;
3492 }
3493 used_bits = 0;
fc76e730 3494 opno = 0;
14daeee3
RS
3495 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3496 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3497 for (s = opcode->args; *s; ++s)
3498 switch (*s)
3499 {
3500 case ',':
3501 case '(':
3502 case ')':
3503 break;
3504
14daeee3
RS
3505 case '#':
3506 s++;
3507 break;
3508
ab902481 3509 default:
fc76e730 3510 if (!decode_operand)
7fd53920 3511 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3512 else
3513 operand = decode_operand (s);
3514 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3515 {
3516 as_bad (_("internal: unknown operand type: %s %s"),
3517 opcode->name, opcode->args);
3518 return 0;
3519 }
fc76e730
RS
3520 gas_assert (opno < MAX_OPERANDS);
3521 operands->operand[opno] = operand;
25499ac7
MR
3522 if (!decode_operand && operand
3523 && operand->type == OP_INT && operand->lsb == 0
3524 && mips_opcode_32bit_p (opcode))
3525 used_bits |= mips16_immed_extend (-1, operand->size);
3526 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3527 {
14daeee3 3528 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3529 if (operand->type == OP_MDMX_IMM_REG)
3530 /* Bit 5 is the format selector (OB vs QH). The opcode table
3531 has separate entries for each format. */
3532 used_bits &= ~(1 << (operand->lsb + 5));
3533 if (operand->type == OP_ENTRY_EXIT_LIST)
3534 used_bits &= ~(mask & 0x700);
38bf472a
MR
3535 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3536 operand field that cannot be fully described with LSB/SIZE. */
3537 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3538 used_bits &= ~0x6000;
fc76e730 3539 }
ab902481 3540 /* Skip prefix characters. */
7361da2c 3541 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3542 ++s;
fc76e730 3543 opno += 1;
ab902481
RS
3544 break;
3545 }
fc76e730 3546 doubled = used_bits & mask & insn_bits;
ab902481
RS
3547 if (doubled)
3548 {
3549 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3550 " %s %s"), doubled, opcode->name, opcode->args);
3551 return 0;
3552 }
fc76e730 3553 used_bits |= mask;
ab902481 3554 undefined = ~used_bits & insn_bits;
fc76e730 3555 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3556 {
3557 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3558 undefined, opcode->name, opcode->args);
3559 return 0;
3560 }
3561 used_bits &= ~insn_bits;
3562 if (used_bits)
3563 {
3564 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3565 used_bits, opcode->name, opcode->args);
3566 return 0;
3567 }
3568 return 1;
3569}
3570
fc76e730
RS
3571/* The MIPS16 version of validate_mips_insn. */
3572
3573static int
3574validate_mips16_insn (const struct mips_opcode *opcode,
3575 struct mips_operand_array *operands)
3576{
7fd53920 3577 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3578
7fd53920 3579 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3580}
3581
ab902481
RS
3582/* The microMIPS version of validate_mips_insn. */
3583
3584static int
fc76e730
RS
3585validate_micromips_insn (const struct mips_opcode *opc,
3586 struct mips_operand_array *operands)
ab902481
RS
3587{
3588 unsigned long insn_bits;
3589 unsigned long major;
3590 unsigned int length;
3591
fc76e730
RS
3592 if (opc->pinfo == INSN_MACRO)
3593 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3594 operands);
3595
ab902481
RS
3596 length = micromips_insn_length (opc);
3597 if (length != 2 && length != 4)
3598 {
1661c76c 3599 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3600 "%s %s"), length, opc->name, opc->args);
3601 return 0;
3602 }
3603 major = opc->match >> (10 + 8 * (length - 2));
3604 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3605 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3606 {
1661c76c 3607 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3608 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3609 return 0;
3610 }
3611
3612 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3613 insn_bits = 1 << 4 * length;
3614 insn_bits <<= 4 * length;
3615 insn_bits -= 1;
fc76e730
RS
3616 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3617 operands);
ab902481
RS
3618}
3619
707bfff6
TS
3620/* This function is called once, at assembler startup time. It should set up
3621 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3622
252b5132 3623void
17a2f251 3624md_begin (void)
252b5132 3625{
3994f87e 3626 const char *retval = NULL;
156c2f8b 3627 int i = 0;
252b5132 3628 int broken = 0;
1f25f5d3 3629
0a44bf69
RS
3630 if (mips_pic != NO_PIC)
3631 {
3632 if (g_switch_seen && g_switch_value != 0)
3633 as_bad (_("-G may not be used in position-independent code"));
3634 g_switch_value = 0;
3635 }
00acd688
CM
3636 else if (mips_abicalls)
3637 {
3638 if (g_switch_seen && g_switch_value != 0)
3639 as_bad (_("-G may not be used with abicalls"));
3640 g_switch_value = 0;
3641 }
0a44bf69 3642
0b35dfee 3643 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3644 as_warn (_("could not set architecture and machine"));
252b5132 3645
252b5132
RH
3646 op_hash = hash_new ();
3647
fc76e730 3648 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3649 for (i = 0; i < NUMOPCODES;)
3650 {
3651 const char *name = mips_opcodes[i].name;
3652
17a2f251 3653 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3654 if (retval != NULL)
3655 {
3656 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3657 mips_opcodes[i].name, retval);
3658 /* Probably a memory allocation problem? Give up now. */
1661c76c 3659 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3660 }
3661 do
3662 {
fc76e730
RS
3663 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3664 decode_mips_operand, &mips_operands[i]))
3665 broken = 1;
3666 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3667 {
fc76e730
RS
3668 create_insn (&nop_insn, mips_opcodes + i);
3669 if (mips_fix_loongson2f_nop)
3670 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3671 nop_insn.fixed_p = 1;
252b5132
RH
3672 }
3673 ++i;
3674 }
3675 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3676 }
3677
3678 mips16_op_hash = hash_new ();
fc76e730
RS
3679 mips16_operands = XCNEWVEC (struct mips_operand_array,
3680 bfd_mips16_num_opcodes);
252b5132
RH
3681
3682 i = 0;
3683 while (i < bfd_mips16_num_opcodes)
3684 {
3685 const char *name = mips16_opcodes[i].name;
3686
17a2f251 3687 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3688 if (retval != NULL)
3689 as_fatal (_("internal: can't hash `%s': %s"),
3690 mips16_opcodes[i].name, retval);
3691 do
3692 {
fc76e730
RS
3693 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3694 broken = 1;
1e915849
RS
3695 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3696 {
3697 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3698 mips16_nop_insn.fixed_p = 1;
3699 }
252b5132
RH
3700 ++i;
3701 }
3702 while (i < bfd_mips16_num_opcodes
3703 && strcmp (mips16_opcodes[i].name, name) == 0);
3704 }
3705
df58fc94 3706 micromips_op_hash = hash_new ();
fc76e730
RS
3707 micromips_operands = XCNEWVEC (struct mips_operand_array,
3708 bfd_micromips_num_opcodes);
df58fc94
RS
3709
3710 i = 0;
3711 while (i < bfd_micromips_num_opcodes)
3712 {
3713 const char *name = micromips_opcodes[i].name;
3714
3715 retval = hash_insert (micromips_op_hash, name,
3716 (void *) &micromips_opcodes[i]);
3717 if (retval != NULL)
3718 as_fatal (_("internal: can't hash `%s': %s"),
3719 micromips_opcodes[i].name, retval);
3720 do
fc76e730
RS
3721 {
3722 struct mips_cl_insn *micromips_nop_insn;
3723
3724 if (!validate_micromips_insn (&micromips_opcodes[i],
3725 &micromips_operands[i]))
3726 broken = 1;
3727
3728 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3729 {
3730 if (micromips_insn_length (micromips_opcodes + i) == 2)
3731 micromips_nop_insn = &micromips_nop16_insn;
3732 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3733 micromips_nop_insn = &micromips_nop32_insn;
3734 else
3735 continue;
3736
3737 if (micromips_nop_insn->insn_mo == NULL
3738 && strcmp (name, "nop") == 0)
3739 {
3740 create_insn (micromips_nop_insn, micromips_opcodes + i);
3741 micromips_nop_insn->fixed_p = 1;
3742 }
3743 }
3744 }
df58fc94
RS
3745 while (++i < bfd_micromips_num_opcodes
3746 && strcmp (micromips_opcodes[i].name, name) == 0);
3747 }
3748
252b5132 3749 if (broken)
1661c76c 3750 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3751
3752 /* We add all the general register names to the symbol table. This
3753 helps us detect invalid uses of them. */
3739860c 3754 for (i = 0; reg_names[i].name; i++)
707bfff6 3755 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3756 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3757 &zero_address_frag));
3758 if (HAVE_NEWABI)
3739860c 3759 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3760 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3761 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3762 &zero_address_frag));
707bfff6 3763 else
3739860c 3764 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3765 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3766 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3767 &zero_address_frag));
6047c971 3768
14daeee3
RS
3769 for (i = 0; i < 32; i++)
3770 {
92fce9bd 3771 char regname[6];
14daeee3
RS
3772
3773 /* R5900 VU0 floating-point register. */
92fce9bd 3774 sprintf (regname, "$vf%d", i);
14daeee3
RS
3775 symbol_table_insert (symbol_new (regname, reg_section,
3776 RTYPE_VF | i, &zero_address_frag));
3777
3778 /* R5900 VU0 integer register. */
92fce9bd 3779 sprintf (regname, "$vi%d", i);
14daeee3
RS
3780 symbol_table_insert (symbol_new (regname, reg_section,
3781 RTYPE_VI | i, &zero_address_frag));
3782
56d438b1 3783 /* MSA register. */
92fce9bd 3784 sprintf (regname, "$w%d", i);
56d438b1
CF
3785 symbol_table_insert (symbol_new (regname, reg_section,
3786 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3787 }
3788
a92713e6
RS
3789 obstack_init (&mips_operand_tokens);
3790
7d10b47d 3791 mips_no_prev_insn ();
252b5132
RH
3792
3793 mips_gprmask = 0;
3794 mips_cprmask[0] = 0;
3795 mips_cprmask[1] = 0;
3796 mips_cprmask[2] = 0;
3797 mips_cprmask[3] = 0;
3798
3799 /* set the default alignment for the text section (2**2) */
3800 record_alignment (text_section, 2);
3801
4d0d148d 3802 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3803
f3ded42a
RS
3804 /* On a native system other than VxWorks, sections must be aligned
3805 to 16 byte boundaries. When configured for an embedded ELF
3806 target, we don't bother. */
3807 if (strncmp (TARGET_OS, "elf", 3) != 0
3808 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3809 {
f3ded42a
RS
3810 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3811 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3812 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3813 }
252b5132 3814
f3ded42a
RS
3815 /* Create a .reginfo section for register masks and a .mdebug
3816 section for debugging information. */
3817 {
3818 segT seg;
3819 subsegT subseg;
3820 flagword flags;
3821 segT sec;
3822
3823 seg = now_seg;
3824 subseg = now_subseg;
3825
3826 /* The ABI says this section should be loaded so that the
3827 running program can access it. However, we don't load it
3828 if we are configured for an embedded target */
3829 flags = SEC_READONLY | SEC_DATA;
3830 if (strncmp (TARGET_OS, "elf", 3) != 0)
3831 flags |= SEC_ALLOC | SEC_LOAD;
3832
3833 if (mips_abi != N64_ABI)
252b5132 3834 {
f3ded42a 3835 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3836
f3ded42a
RS
3837 bfd_set_section_flags (stdoutput, sec, flags);
3838 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3839
f3ded42a
RS
3840 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3841 }
3842 else
3843 {
3844 /* The 64-bit ABI uses a .MIPS.options section rather than
3845 .reginfo section. */
3846 sec = subseg_new (".MIPS.options", (subsegT) 0);
3847 bfd_set_section_flags (stdoutput, sec, flags);
3848 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3849
f3ded42a
RS
3850 /* Set up the option header. */
3851 {
3852 Elf_Internal_Options opthdr;
3853 char *f;
3854
3855 opthdr.kind = ODK_REGINFO;
3856 opthdr.size = (sizeof (Elf_External_Options)
3857 + sizeof (Elf64_External_RegInfo));
3858 opthdr.section = 0;
3859 opthdr.info = 0;
3860 f = frag_more (sizeof (Elf_External_Options));
3861 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3862 (Elf_External_Options *) f);
3863
3864 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3865 }
3866 }
252b5132 3867
351cdf24
MF
3868 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3869 bfd_set_section_flags (stdoutput, sec,
3870 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3871 bfd_set_section_alignment (stdoutput, sec, 3);
3872 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3873
f3ded42a
RS
3874 if (ECOFF_DEBUGGING)
3875 {
3876 sec = subseg_new (".mdebug", (subsegT) 0);
3877 (void) bfd_set_section_flags (stdoutput, sec,
3878 SEC_HAS_CONTENTS | SEC_READONLY);
3879 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3880 }
f3ded42a
RS
3881 else if (mips_flag_pdr)
3882 {
3883 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3884 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3885 SEC_READONLY | SEC_RELOC
3886 | SEC_DEBUGGING);
3887 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3888 }
3889
3890 subseg_set (seg, subseg);
3891 }
252b5132 3892
71400594
RS
3893 if (mips_fix_vr4120)
3894 init_vr4120_conflicts ();
252b5132
RH
3895}
3896
351cdf24
MF
3897static inline void
3898fpabi_incompatible_with (int fpabi, const char *what)
3899{
3900 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3901 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3902}
3903
3904static inline void
3905fpabi_requires (int fpabi, const char *what)
3906{
3907 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3908 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3909}
3910
3911/* Check -mabi and register sizes against the specified FP ABI. */
3912static void
3913check_fpabi (int fpabi)
3914{
351cdf24
MF
3915 switch (fpabi)
3916 {
3917 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3918 if (file_mips_opts.soft_float)
3919 fpabi_incompatible_with (fpabi, "softfloat");
3920 else if (file_mips_opts.single_float)
3921 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3922 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3923 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3924 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3925 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3926 break;
3927
3928 case Val_GNU_MIPS_ABI_FP_XX:
3929 if (mips_abi != O32_ABI)
3930 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3931 else if (file_mips_opts.soft_float)
3932 fpabi_incompatible_with (fpabi, "softfloat");
3933 else if (file_mips_opts.single_float)
3934 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3935 else if (file_mips_opts.fp != 0)
3936 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3937 break;
3938
3939 case Val_GNU_MIPS_ABI_FP_64A:
3940 case Val_GNU_MIPS_ABI_FP_64:
3941 if (mips_abi != O32_ABI)
3942 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3943 else if (file_mips_opts.soft_float)
3944 fpabi_incompatible_with (fpabi, "softfloat");
3945 else if (file_mips_opts.single_float)
3946 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3947 else if (file_mips_opts.fp != 64)
3948 fpabi_requires (fpabi, "fp=64");
3949 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3950 fpabi_incompatible_with (fpabi, "nooddspreg");
3951 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3952 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3953 break;
3954
3955 case Val_GNU_MIPS_ABI_FP_SINGLE:
3956 if (file_mips_opts.soft_float)
3957 fpabi_incompatible_with (fpabi, "softfloat");
3958 else if (!file_mips_opts.single_float)
3959 fpabi_requires (fpabi, "singlefloat");
3960 break;
3961
3962 case Val_GNU_MIPS_ABI_FP_SOFT:
3963 if (!file_mips_opts.soft_float)
3964 fpabi_requires (fpabi, "softfloat");
3965 break;
3966
3967 case Val_GNU_MIPS_ABI_FP_OLD_64:
3968 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3969 Tag_GNU_MIPS_ABI_FP, fpabi);
3970 break;
3971
3350cc01
CM
3972 case Val_GNU_MIPS_ABI_FP_NAN2008:
3973 /* Silently ignore compatibility value. */
3974 break;
3975
351cdf24
MF
3976 default:
3977 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3978 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3979 break;
3980 }
351cdf24
MF
3981}
3982
919731af 3983/* Perform consistency checks on the current options. */
3984
3985static void
3986mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3987{
3988 /* Check the size of integer registers agrees with the ABI and ISA. */
3989 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3990 as_bad (_("`gp=64' used with a 32-bit processor"));
3991 else if (abi_checks
3992 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3993 as_bad (_("`gp=32' used with a 64-bit ABI"));
3994 else if (abi_checks
3995 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3996 as_bad (_("`gp=64' used with a 32-bit ABI"));
3997
3998 /* Check the size of the float registers agrees with the ABI and ISA. */
3999 switch (opts->fp)
4000 {
351cdf24
MF
4001 case 0:
4002 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
4003 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
4004 else if (opts->single_float == 1)
4005 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
4006 break;
919731af 4007 case 64:
4008 if (!ISA_HAS_64BIT_FPRS (opts->isa))
4009 as_bad (_("`fp=64' used with a 32-bit fpu"));
4010 else if (abi_checks
4011 && ABI_NEEDS_32BIT_REGS (mips_abi)
4012 && !ISA_HAS_MXHC1 (opts->isa))
4013 as_warn (_("`fp=64' used with a 32-bit ABI"));
4014 break;
4015 case 32:
4016 if (abi_checks
4017 && ABI_NEEDS_64BIT_REGS (mips_abi))
4018 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 4019 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 4020 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 4021 break;
4022 default:
4023 as_bad (_("Unknown size of floating point registers"));
4024 break;
4025 }
4026
351cdf24
MF
4027 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
4028 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
4029
919731af 4030 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 4031 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 4032 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
4033 && (opts->micromips == 1
4034 || opts->mips16 == 1))
1357373c 4035 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 4036 opts->micromips ? "micromips" : "mips16",
5f4678bb 4037 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
4038
4039 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4040 as_fatal (_("branch relaxation is not supported in `%s'"),
4041 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4042}
4043
4044/* Perform consistency checks on the module level options exactly once.
4045 This is a deferred check that happens:
4046 at the first .set directive
4047 or, at the first pseudo op that generates code (inc .dc.a)
4048 or, at the first instruction
4049 or, at the end. */
4050
4051static void
4052file_mips_check_options (void)
4053{
4054 const struct mips_cpu_info *arch_info = 0;
4055
4056 if (file_mips_opts_checked)
4057 return;
4058
4059 /* The following code determines the register size.
4060 Similar code was added to GCC 3.3 (see override_options() in
4061 config/mips/mips.c). The GAS and GCC code should be kept in sync
4062 as much as possible. */
4063
4064 if (file_mips_opts.gp < 0)
4065 {
4066 /* Infer the integer register size from the ABI and processor.
4067 Restrict ourselves to 32-bit registers if that's all the
4068 processor has, or if the ABI cannot handle 64-bit registers. */
4069 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4070 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4071 ? 32 : 64;
4072 }
4073
4074 if (file_mips_opts.fp < 0)
4075 {
4076 /* No user specified float register size.
4077 ??? GAS treats single-float processors as though they had 64-bit
4078 float registers (although it complains when double-precision
4079 instructions are used). As things stand, saying they have 32-bit
4080 registers would lead to spurious "register must be even" messages.
4081 So here we assume float registers are never smaller than the
4082 integer ones. */
4083 if (file_mips_opts.gp == 64)
4084 /* 64-bit integer registers implies 64-bit float registers. */
4085 file_mips_opts.fp = 64;
4086 else if ((file_mips_opts.ase & FP64_ASES)
4087 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4088 /* Handle ASEs that require 64-bit float registers, if possible. */
4089 file_mips_opts.fp = 64;
7361da2c
AB
4090 else if (ISA_IS_R6 (mips_opts.isa))
4091 /* R6 implies 64-bit float registers. */
4092 file_mips_opts.fp = 64;
919731af 4093 else
4094 /* 32-bit float registers. */
4095 file_mips_opts.fp = 32;
4096 }
4097
4098 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4099
351cdf24
MF
4100 /* Disable operations on odd-numbered floating-point registers by default
4101 when using the FPXX ABI. */
4102 if (file_mips_opts.oddspreg < 0)
4103 {
4104 if (file_mips_opts.fp == 0)
4105 file_mips_opts.oddspreg = 0;
4106 else
4107 file_mips_opts.oddspreg = 1;
4108 }
4109
919731af 4110 /* End of GCC-shared inference code. */
4111
4112 /* This flag is set when we have a 64-bit capable CPU but use only
4113 32-bit wide registers. Note that EABI does not use it. */
4114 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4115 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4116 || mips_abi == O32_ABI))
4117 mips_32bitmode = 1;
4118
4119 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4120 as_bad (_("trap exception not supported at ISA 1"));
4121
4122 /* If the selected architecture includes support for ASEs, enable
4123 generation of code for them. */
4124 if (file_mips_opts.mips16 == -1)
4125 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4126 if (file_mips_opts.micromips == -1)
4127 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4128 ? 1 : 0;
4129
7361da2c
AB
4130 if (mips_nan2008 == -1)
4131 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4132 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4133 as_fatal (_("`%s' does not support legacy NaN"),
4134 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4135
919731af 4136 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4137 being selected implicitly. */
4138 if (file_mips_opts.fp != 64)
4139 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4140
4141 /* If the user didn't explicitly select or deselect a particular ASE,
4142 use the default setting for the CPU. */
4143 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4144
4145 /* Set up the current options. These may change throughout assembly. */
4146 mips_opts = file_mips_opts;
4147
4148 mips_check_isa_supports_ases ();
4149 mips_check_options (&file_mips_opts, TRUE);
4150 file_mips_opts_checked = TRUE;
4151
4152 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4153 as_warn (_("could not set architecture and machine"));
4154}
4155
252b5132 4156void
17a2f251 4157md_assemble (char *str)
252b5132
RH
4158{
4159 struct mips_cl_insn insn;
f6688943
TS
4160 bfd_reloc_code_real_type unused_reloc[3]
4161 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4162
919731af 4163 file_mips_check_options ();
4164
252b5132 4165 imm_expr.X_op = O_absent;
252b5132 4166 offset_expr.X_op = O_absent;
f6688943
TS
4167 offset_reloc[0] = BFD_RELOC_UNUSED;
4168 offset_reloc[1] = BFD_RELOC_UNUSED;
4169 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4170
e1b47bd5
RS
4171 mips_mark_labels ();
4172 mips_assembling_insn = TRUE;
e3de51ce 4173 clear_insn_error ();
e1b47bd5 4174
252b5132
RH
4175 if (mips_opts.mips16)
4176 mips16_ip (str, &insn);
4177 else
4178 {
4179 mips_ip (str, &insn);
beae10d5
KH
4180 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4181 str, insn.insn_opcode));
252b5132
RH
4182 }
4183
e3de51ce
RS
4184 if (insn_error.msg)
4185 report_insn_error (str);
e1b47bd5 4186 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4187 {
584892a6 4188 macro_start ();
252b5132
RH
4189 if (mips_opts.mips16)
4190 mips16_macro (&insn);
4191 else
833794fc 4192 macro (&insn, str);
584892a6 4193 macro_end ();
252b5132
RH
4194 }
4195 else
4196 {
77bd4346 4197 if (offset_expr.X_op != O_absent)
df58fc94 4198 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4199 else
df58fc94 4200 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4201 }
e1b47bd5
RS
4202
4203 mips_assembling_insn = FALSE;
252b5132
RH
4204}
4205
738e5348
RS
4206/* Convenience functions for abstracting away the differences between
4207 MIPS16 and non-MIPS16 relocations. */
4208
4209static inline bfd_boolean
4210mips16_reloc_p (bfd_reloc_code_real_type reloc)
4211{
4212 switch (reloc)
4213 {
4214 case BFD_RELOC_MIPS16_JMP:
4215 case BFD_RELOC_MIPS16_GPREL:
4216 case BFD_RELOC_MIPS16_GOT16:
4217 case BFD_RELOC_MIPS16_CALL16:
4218 case BFD_RELOC_MIPS16_HI16_S:
4219 case BFD_RELOC_MIPS16_HI16:
4220 case BFD_RELOC_MIPS16_LO16:
c9775dde 4221 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4222 return TRUE;
4223
4224 default:
4225 return FALSE;
4226 }
4227}
4228
df58fc94
RS
4229static inline bfd_boolean
4230micromips_reloc_p (bfd_reloc_code_real_type reloc)
4231{
4232 switch (reloc)
4233 {
4234 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4235 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4236 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4237 case BFD_RELOC_MICROMIPS_GPREL16:
4238 case BFD_RELOC_MICROMIPS_JMP:
4239 case BFD_RELOC_MICROMIPS_HI16:
4240 case BFD_RELOC_MICROMIPS_HI16_S:
4241 case BFD_RELOC_MICROMIPS_LO16:
4242 case BFD_RELOC_MICROMIPS_LITERAL:
4243 case BFD_RELOC_MICROMIPS_GOT16:
4244 case BFD_RELOC_MICROMIPS_CALL16:
4245 case BFD_RELOC_MICROMIPS_GOT_HI16:
4246 case BFD_RELOC_MICROMIPS_GOT_LO16:
4247 case BFD_RELOC_MICROMIPS_CALL_HI16:
4248 case BFD_RELOC_MICROMIPS_CALL_LO16:
4249 case BFD_RELOC_MICROMIPS_SUB:
4250 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4251 case BFD_RELOC_MICROMIPS_GOT_OFST:
4252 case BFD_RELOC_MICROMIPS_GOT_DISP:
4253 case BFD_RELOC_MICROMIPS_HIGHEST:
4254 case BFD_RELOC_MICROMIPS_HIGHER:
4255 case BFD_RELOC_MICROMIPS_SCN_DISP:
4256 case BFD_RELOC_MICROMIPS_JALR:
4257 return TRUE;
4258
4259 default:
4260 return FALSE;
4261 }
4262}
4263
2309ddf2
MR
4264static inline bfd_boolean
4265jmp_reloc_p (bfd_reloc_code_real_type reloc)
4266{
4267 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4268}
4269
0e9c5a5c
MR
4270static inline bfd_boolean
4271b_reloc_p (bfd_reloc_code_real_type reloc)
4272{
4273 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4274 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4275 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4276 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4277 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4278 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4279 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4280}
4281
738e5348
RS
4282static inline bfd_boolean
4283got16_reloc_p (bfd_reloc_code_real_type reloc)
4284{
2309ddf2 4285 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4286 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4287}
4288
4289static inline bfd_boolean
4290hi16_reloc_p (bfd_reloc_code_real_type reloc)
4291{
2309ddf2 4292 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4293 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4294}
4295
4296static inline bfd_boolean
4297lo16_reloc_p (bfd_reloc_code_real_type reloc)
4298{
2309ddf2 4299 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4300 || reloc == BFD_RELOC_MICROMIPS_LO16);
4301}
4302
df58fc94
RS
4303static inline bfd_boolean
4304jalr_reloc_p (bfd_reloc_code_real_type reloc)
4305{
2309ddf2 4306 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4307}
4308
f2ae14a1
RS
4309static inline bfd_boolean
4310gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4311{
4312 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4313 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4314}
4315
2de39019
CM
4316/* Return true if RELOC is a PC-relative relocation that does not have
4317 full address range. */
4318
4319static inline bfd_boolean
4320limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4321{
4322 switch (reloc)
4323 {
4324 case BFD_RELOC_16_PCREL_S2:
c9775dde 4325 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4326 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4327 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4328 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4329 case BFD_RELOC_MIPS_21_PCREL_S2:
4330 case BFD_RELOC_MIPS_26_PCREL_S2:
4331 case BFD_RELOC_MIPS_18_PCREL_S3:
4332 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4333 return TRUE;
4334
b47468a6 4335 case BFD_RELOC_32_PCREL:
7361da2c
AB
4336 case BFD_RELOC_HI16_S_PCREL:
4337 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4338 return HAVE_64BIT_ADDRESSES;
4339
2de39019
CM
4340 default:
4341 return FALSE;
4342 }
4343}
b47468a6 4344
5919d012 4345/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4346 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4347 need a matching %lo() when applied to local symbols. */
5919d012
RS
4348
4349static inline bfd_boolean
17a2f251 4350reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4351{
3b91255e 4352 return (HAVE_IN_PLACE_ADDENDS
738e5348 4353 && (hi16_reloc_p (reloc)
0a44bf69
RS
4354 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4355 all GOT16 relocations evaluate to "G". */
738e5348
RS
4356 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4357}
4358
4359/* Return the type of %lo() reloc needed by RELOC, given that
4360 reloc_needs_lo_p. */
4361
4362static inline bfd_reloc_code_real_type
4363matching_lo_reloc (bfd_reloc_code_real_type reloc)
4364{
df58fc94
RS
4365 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4366 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4367 : BFD_RELOC_LO16));
5919d012
RS
4368}
4369
4370/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4371 relocation. */
4372
4373static inline bfd_boolean
17a2f251 4374fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4375{
4376 return (fixp->fx_next != NULL
738e5348 4377 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4378 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4379 && fixp->fx_offset == fixp->fx_next->fx_offset);
4380}
4381
462427c4
RS
4382/* Move all labels in LABELS to the current insertion point. TEXT_P
4383 says whether the labels refer to text or data. */
404a8071
RS
4384
4385static void
462427c4 4386mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4387{
4388 struct insn_label_list *l;
4389 valueT val;
4390
462427c4 4391 for (l = labels; l != NULL; l = l->next)
404a8071 4392 {
9c2799c2 4393 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4394 symbol_set_frag (l->label, frag_now);
4395 val = (valueT) frag_now_fix ();
df58fc94 4396 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4397 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4398 ++val;
4399 S_SET_VALUE (l->label, val);
4400 }
4401}
4402
462427c4
RS
4403/* Move all labels in insn_labels to the current insertion point
4404 and treat them as text labels. */
4405
4406static void
4407mips_move_text_labels (void)
4408{
4409 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4410}
4411
9e009953
MR
4412/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4413
5f0fe04b
TS
4414static bfd_boolean
4415s_is_linkonce (symbolS *sym, segT from_seg)
4416{
4417 bfd_boolean linkonce = FALSE;
4418 segT symseg = S_GET_SEGMENT (sym);
4419
4420 if (symseg != from_seg && !S_IS_LOCAL (sym))
4421 {
4422 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4423 linkonce = TRUE;
5f0fe04b
TS
4424 /* The GNU toolchain uses an extension for ELF: a section
4425 beginning with the magic string .gnu.linkonce is a
4426 linkonce section. */
4427 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4428 sizeof ".gnu.linkonce" - 1) == 0)
4429 linkonce = TRUE;
5f0fe04b
TS
4430 }
4431 return linkonce;
4432}
4433
e1b47bd5 4434/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4435 linker to handle them specially, such as generating jalx instructions
4436 when needed. We also make them odd for the duration of the assembly,
4437 in order to generate the right sort of code. We will make them even
252b5132
RH
4438 in the adjust_symtab routine, while leaving them marked. This is
4439 convenient for the debugger and the disassembler. The linker knows
4440 to make them odd again. */
4441
4442static void
e1b47bd5 4443mips_compressed_mark_label (symbolS *label)
252b5132 4444{
df58fc94 4445 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4446
f3ded42a
RS
4447 if (mips_opts.mips16)
4448 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4449 else
4450 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4451 if ((S_GET_VALUE (label) & 1) == 0
4452 /* Don't adjust the address if the label is global or weak, or
4453 in a link-once section, since we'll be emitting symbol reloc
4454 references to it which will be patched up by the linker, and
4455 the final value of the symbol may or may not be MIPS16/microMIPS. */
4456 && !S_IS_WEAK (label)
4457 && !S_IS_EXTERNAL (label)
4458 && !s_is_linkonce (label, now_seg))
4459 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4460}
4461
4462/* Mark preceding MIPS16 or microMIPS instruction labels. */
4463
4464static void
4465mips_compressed_mark_labels (void)
4466{
4467 struct insn_label_list *l;
4468
4469 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4470 mips_compressed_mark_label (l->label);
252b5132
RH
4471}
4472
4d7206a2
RS
4473/* End the current frag. Make it a variant frag and record the
4474 relaxation info. */
4475
4476static void
4477relax_close_frag (void)
4478{
584892a6 4479 mips_macro_warning.first_frag = frag_now;
4d7206a2 4480 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4481 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4482 mips_pic != NO_PIC),
4d7206a2
RS
4483 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4484
4485 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4486 mips_relax.first_fixup = 0;
4487}
4488
4489/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4490 See the comment above RELAX_ENCODE for more details. */
4491
4492static void
4493relax_start (symbolS *symbol)
4494{
9c2799c2 4495 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4496 mips_relax.sequence = 1;
4497 mips_relax.symbol = symbol;
4498}
4499
4500/* Start generating the second version of a relaxable sequence.
4501 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4502
4503static void
4d7206a2
RS
4504relax_switch (void)
4505{
9c2799c2 4506 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4507 mips_relax.sequence = 2;
4508}
4509
4510/* End the current relaxable sequence. */
4511
4512static void
4513relax_end (void)
4514{
9c2799c2 4515 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4516 relax_close_frag ();
4517 mips_relax.sequence = 0;
4518}
4519
11625dd8
RS
4520/* Return true if IP is a delayed branch or jump. */
4521
4522static inline bfd_boolean
4523delayed_branch_p (const struct mips_cl_insn *ip)
4524{
4525 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4526 | INSN_COND_BRANCH_DELAY
4527 | INSN_COND_BRANCH_LIKELY)) != 0;
4528}
4529
4530/* Return true if IP is a compact branch or jump. */
4531
4532static inline bfd_boolean
4533compact_branch_p (const struct mips_cl_insn *ip)
4534{
26545944
RS
4535 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4536 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4537}
4538
4539/* Return true if IP is an unconditional branch or jump. */
4540
4541static inline bfd_boolean
4542uncond_branch_p (const struct mips_cl_insn *ip)
4543{
4544 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4545 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4546}
4547
4548/* Return true if IP is a branch-likely instruction. */
4549
4550static inline bfd_boolean
4551branch_likely_p (const struct mips_cl_insn *ip)
4552{
4553 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4554}
4555
14fe068b
RS
4556/* Return the type of nop that should be used to fill the delay slot
4557 of delayed branch IP. */
4558
4559static struct mips_cl_insn *
4560get_delay_slot_nop (const struct mips_cl_insn *ip)
4561{
4562 if (mips_opts.micromips
4563 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4564 return &micromips_nop32_insn;
4565 return NOP_INSN;
4566}
4567
fc76e730
RS
4568/* Return a mask that has bit N set if OPCODE reads the register(s)
4569 in operand N. */
df58fc94
RS
4570
4571static unsigned int
fc76e730 4572insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4573{
fc76e730
RS
4574 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4575}
df58fc94 4576
fc76e730
RS
4577/* Return a mask that has bit N set if OPCODE writes to the register(s)
4578 in operand N. */
4579
4580static unsigned int
4581insn_write_mask (const struct mips_opcode *opcode)
4582{
4583 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4584}
4585
4586/* Return a mask of the registers specified by operand OPERAND of INSN.
4587 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4588 is set. */
4589
4590static unsigned int
4591operand_reg_mask (const struct mips_cl_insn *insn,
4592 const struct mips_operand *operand,
4593 unsigned int type_mask)
4594{
4595 unsigned int uval, vsel;
4596
4597 switch (operand->type)
df58fc94 4598 {
fc76e730
RS
4599 case OP_INT:
4600 case OP_MAPPED_INT:
4601 case OP_MSB:
4602 case OP_PCREL:
4603 case OP_PERF_REG:
4604 case OP_ADDIUSP_INT:
4605 case OP_ENTRY_EXIT_LIST:
4606 case OP_REPEAT_DEST_REG:
4607 case OP_REPEAT_PREV_REG:
4608 case OP_PC:
14daeee3
RS
4609 case OP_VU0_SUFFIX:
4610 case OP_VU0_MATCH_SUFFIX:
56d438b1 4611 case OP_IMM_INDEX:
fc76e730
RS
4612 abort ();
4613
25499ac7
MR
4614 case OP_REG28:
4615 return 1 << 28;
4616
fc76e730 4617 case OP_REG:
0f35dbc4 4618 case OP_OPTIONAL_REG:
fc76e730
RS
4619 {
4620 const struct mips_reg_operand *reg_op;
4621
4622 reg_op = (const struct mips_reg_operand *) operand;
4623 if (!(type_mask & (1 << reg_op->reg_type)))
4624 return 0;
4625 uval = insn_extract_operand (insn, operand);
4626 return 1 << mips_decode_reg_operand (reg_op, uval);
4627 }
4628
4629 case OP_REG_PAIR:
4630 {
4631 const struct mips_reg_pair_operand *pair_op;
4632
4633 pair_op = (const struct mips_reg_pair_operand *) operand;
4634 if (!(type_mask & (1 << pair_op->reg_type)))
4635 return 0;
4636 uval = insn_extract_operand (insn, operand);
4637 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4638 }
4639
4640 case OP_CLO_CLZ_DEST:
4641 if (!(type_mask & (1 << OP_REG_GP)))
4642 return 0;
4643 uval = insn_extract_operand (insn, operand);
4644 return (1 << (uval & 31)) | (1 << (uval >> 5));
4645
7361da2c
AB
4646 case OP_SAME_RS_RT:
4647 if (!(type_mask & (1 << OP_REG_GP)))
4648 return 0;
4649 uval = insn_extract_operand (insn, operand);
4650 gas_assert ((uval & 31) == (uval >> 5));
4651 return 1 << (uval & 31);
4652
4653 case OP_CHECK_PREV:
4654 case OP_NON_ZERO_REG:
4655 if (!(type_mask & (1 << OP_REG_GP)))
4656 return 0;
4657 uval = insn_extract_operand (insn, operand);
4658 return 1 << (uval & 31);
4659
fc76e730
RS
4660 case OP_LWM_SWM_LIST:
4661 abort ();
4662
4663 case OP_SAVE_RESTORE_LIST:
4664 abort ();
4665
4666 case OP_MDMX_IMM_REG:
4667 if (!(type_mask & (1 << OP_REG_VEC)))
4668 return 0;
4669 uval = insn_extract_operand (insn, operand);
4670 vsel = uval >> 5;
4671 if ((vsel & 0x18) == 0x18)
4672 return 0;
4673 return 1 << (uval & 31);
56d438b1
CF
4674
4675 case OP_REG_INDEX:
4676 if (!(type_mask & (1 << OP_REG_GP)))
4677 return 0;
4678 return 1 << insn_extract_operand (insn, operand);
df58fc94 4679 }
fc76e730
RS
4680 abort ();
4681}
4682
4683/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4684 where bit N of OPNO_MASK is set if operand N should be included.
4685 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4686 is set. */
4687
4688static unsigned int
4689insn_reg_mask (const struct mips_cl_insn *insn,
4690 unsigned int type_mask, unsigned int opno_mask)
4691{
4692 unsigned int opno, reg_mask;
4693
4694 opno = 0;
4695 reg_mask = 0;
4696 while (opno_mask != 0)
4697 {
4698 if (opno_mask & 1)
4699 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4700 opno_mask >>= 1;
4701 opno += 1;
4702 }
4703 return reg_mask;
df58fc94
RS
4704}
4705
4c260379
RS
4706/* Return the mask of core registers that IP reads. */
4707
4708static unsigned int
4709gpr_read_mask (const struct mips_cl_insn *ip)
4710{
4711 unsigned long pinfo, pinfo2;
4712 unsigned int mask;
4713
fc76e730 4714 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4715 pinfo = ip->insn_mo->pinfo;
4716 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4717 if (pinfo & INSN_UDI)
4c260379 4718 {
fc76e730
RS
4719 /* UDI instructions have traditionally been assumed to read RS
4720 and RT. */
4721 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4722 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4723 }
fc76e730
RS
4724 if (pinfo & INSN_READ_GPR_24)
4725 mask |= 1 << 24;
4726 if (pinfo2 & INSN2_READ_GPR_16)
4727 mask |= 1 << 16;
4728 if (pinfo2 & INSN2_READ_SP)
4729 mask |= 1 << SP;
26545944 4730 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4731 mask |= 1 << 31;
fe35f09f
RS
4732 /* Don't include register 0. */
4733 return mask & ~1;
4c260379
RS
4734}
4735
4736/* Return the mask of core registers that IP writes. */
4737
4738static unsigned int
4739gpr_write_mask (const struct mips_cl_insn *ip)
4740{
4741 unsigned long pinfo, pinfo2;
4742 unsigned int mask;
4743
fc76e730 4744 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4745 pinfo = ip->insn_mo->pinfo;
4746 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4747 if (pinfo & INSN_WRITE_GPR_24)
4748 mask |= 1 << 24;
4749 if (pinfo & INSN_WRITE_GPR_31)
4750 mask |= 1 << 31;
4751 if (pinfo & INSN_UDI)
4752 /* UDI instructions have traditionally been assumed to write to RD. */
4753 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4754 if (pinfo2 & INSN2_WRITE_SP)
4755 mask |= 1 << SP;
fe35f09f
RS
4756 /* Don't include register 0. */
4757 return mask & ~1;
4c260379
RS
4758}
4759
4760/* Return the mask of floating-point registers that IP reads. */
4761
4762static unsigned int
4763fpr_read_mask (const struct mips_cl_insn *ip)
4764{
fc76e730 4765 unsigned long pinfo;
4c260379
RS
4766 unsigned int mask;
4767
9d5de888
CF
4768 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4769 | (1 << OP_REG_MSA)),
fc76e730 4770 insn_read_mask (ip->insn_mo));
4c260379 4771 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4772 /* Conservatively treat all operands to an FP_D instruction are doubles.
4773 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4774 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4775 mask |= mask << 1;
4776 return mask;
4777}
4778
4779/* Return the mask of floating-point registers that IP writes. */
4780
4781static unsigned int
4782fpr_write_mask (const struct mips_cl_insn *ip)
4783{
fc76e730 4784 unsigned long pinfo;
4c260379
RS
4785 unsigned int mask;
4786
9d5de888
CF
4787 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4788 | (1 << OP_REG_MSA)),
fc76e730 4789 insn_write_mask (ip->insn_mo));
4c260379 4790 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4791 /* Conservatively treat all operands to an FP_D instruction are doubles.
4792 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4793 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4794 mask |= mask << 1;
4795 return mask;
4796}
4797
a1d78564
RS
4798/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4799 Check whether that is allowed. */
4800
4801static bfd_boolean
4802mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4803{
4804 const char *s = insn->name;
351cdf24
MF
4805 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4806 || FPR_SIZE == 64)
4807 && mips_opts.oddspreg;
a1d78564
RS
4808
4809 if (insn->pinfo == INSN_MACRO)
4810 /* Let a macro pass, we'll catch it later when it is expanded. */
4811 return TRUE;
4812
351cdf24
MF
4813 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4814 otherwise it depends on oddspreg. */
4815 if ((insn->pinfo & FP_S)
4816 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4817 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4818 return FPR_SIZE == 32 || oddspreg;
a1d78564 4819
351cdf24
MF
4820 /* Allow odd registers for single-precision ops and double-precision if the
4821 floating-point registers are 64-bit wide. */
4822 switch (insn->pinfo & (FP_S | FP_D))
4823 {
4824 case FP_S:
4825 case 0:
4826 return oddspreg;
4827 case FP_D:
4828 return FPR_SIZE == 64;
4829 default:
4830 break;
a1d78564
RS
4831 }
4832
351cdf24
MF
4833 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4834 s = strchr (insn->name, '.');
4835 if (s != NULL && opnum == 2)
4836 s = strchr (s + 1, '.');
4837 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4838 return oddspreg;
a1d78564 4839
351cdf24 4840 return FPR_SIZE == 64;
a1d78564
RS
4841}
4842
a1d78564
RS
4843/* Information about an instruction argument that we're trying to match. */
4844struct mips_arg_info
4845{
4846 /* The instruction so far. */
4847 struct mips_cl_insn *insn;
4848
a92713e6
RS
4849 /* The first unconsumed operand token. */
4850 struct mips_operand_token *token;
4851
a1d78564
RS
4852 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4853 int opnum;
4854
4855 /* The 1-based argument number, for error reporting. This does not
4856 count elided optional registers, etc.. */
4857 int argnum;
4858
4859 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4860 unsigned int last_regno;
4861
4862 /* If the first operand was an OP_REG, this is the register that it
4863 specified, otherwise it is ILLEGAL_REG. */
4864 unsigned int dest_regno;
4865
4866 /* The value of the last OP_INT operand. Only used for OP_MSB,
4867 where it gives the lsb position. */
4868 unsigned int last_op_int;
4869
60f20e8b 4870 /* If true, match routines should assume that no later instruction
2b0f3761 4871 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4872 possible. Match routines should not report errors if something
4873 is only invalid for !LAX_MATCH. */
4874 bfd_boolean lax_match;
a1d78564 4875
a1d78564
RS
4876 /* True if a reference to the current AT register was seen. */
4877 bfd_boolean seen_at;
4878};
4879
1a00e612
RS
4880/* Record that the argument is out of range. */
4881
4882static void
4883match_out_of_range (struct mips_arg_info *arg)
4884{
4885 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4886}
4887
4888/* Record that the argument isn't constant but needs to be. */
4889
4890static void
4891match_not_constant (struct mips_arg_info *arg)
4892{
4893 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4894 arg->argnum);
4895}
4896
a92713e6
RS
4897/* Try to match an OT_CHAR token for character CH. Consume the token
4898 and return true on success, otherwise return false. */
a1d78564 4899
a92713e6
RS
4900static bfd_boolean
4901match_char (struct mips_arg_info *arg, char ch)
a1d78564 4902{
a92713e6
RS
4903 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4904 {
4905 ++arg->token;
4906 if (ch == ',')
4907 arg->argnum += 1;
4908 return TRUE;
4909 }
4910 return FALSE;
4911}
a1d78564 4912
a92713e6
RS
4913/* Try to get an expression from the next tokens in ARG. Consume the
4914 tokens and return true on success, storing the expression value in
4915 VALUE and relocation types in R. */
4916
4917static bfd_boolean
4918match_expression (struct mips_arg_info *arg, expressionS *value,
4919 bfd_reloc_code_real_type *r)
4920{
d436c1c2
RS
4921 /* If the next token is a '(' that was parsed as being part of a base
4922 expression, assume we have an elided offset. The later match will fail
4923 if this turns out to be wrong. */
4924 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4925 {
d436c1c2
RS
4926 value->X_op = O_constant;
4927 value->X_add_number = 0;
4928 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4929 return TRUE;
4930 }
4931
d436c1c2
RS
4932 /* Reject register-based expressions such as "0+$2" and "(($2))".
4933 For plain registers the default error seems more appropriate. */
4934 if (arg->token->type == OT_INTEGER
4935 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4936 {
d436c1c2
RS
4937 set_insn_error (arg->argnum, _("register value used as expression"));
4938 return FALSE;
a1d78564 4939 }
d436c1c2
RS
4940
4941 if (arg->token->type == OT_INTEGER)
a92713e6 4942 {
d436c1c2
RS
4943 *value = arg->token->u.integer.value;
4944 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4945 ++arg->token;
4946 return TRUE;
a92713e6 4947 }
a92713e6 4948
d436c1c2
RS
4949 set_insn_error_i
4950 (arg->argnum, _("operand %d must be an immediate expression"),
4951 arg->argnum);
4952 return FALSE;
a92713e6
RS
4953}
4954
4955/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4956 the tokens and return true on success, storing the constant value
a54d5f8b 4957 in *VALUE. */
a92713e6
RS
4958
4959static bfd_boolean
1a00e612 4960match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4961{
4962 expressionS ex;
4963 bfd_reloc_code_real_type r[3];
a1d78564 4964
a92713e6
RS
4965 if (!match_expression (arg, &ex, r))
4966 return FALSE;
4967
4968 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4969 *value = ex.X_add_number;
4970 else
4971 {
c96425c5
MR
4972 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4973 match_out_of_range (arg);
4974 else
4975 match_not_constant (arg);
1a00e612 4976 return FALSE;
a1d78564 4977 }
a92713e6 4978 return TRUE;
a1d78564
RS
4979}
4980
4981/* Return the RTYPE_* flags for a register operand of type TYPE that
4982 appears in instruction OPCODE. */
4983
4984static unsigned int
4985convert_reg_type (const struct mips_opcode *opcode,
4986 enum mips_reg_operand_type type)
4987{
4988 switch (type)
4989 {
4990 case OP_REG_GP:
4991 return RTYPE_NUM | RTYPE_GP;
4992
4993 case OP_REG_FP:
4994 /* Allow vector register names for MDMX if the instruction is a 64-bit
4995 FPR load, store or move (including moves to and from GPRs). */
4996 if ((mips_opts.ase & ASE_MDMX)
4997 && (opcode->pinfo & FP_D)
43885403 4998 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4999 | INSN_COPROC_MEMORY_DELAY
43885403 5000 | INSN_LOAD_COPROC
67dc82bc 5001 | INSN_LOAD_MEMORY
a1d78564
RS
5002 | INSN_STORE_MEMORY)))
5003 return RTYPE_FPU | RTYPE_VEC;
5004 return RTYPE_FPU;
5005
5006 case OP_REG_CCC:
5007 if (opcode->pinfo & (FP_D | FP_S))
5008 return RTYPE_CCC | RTYPE_FCC;
5009 return RTYPE_CCC;
5010
5011 case OP_REG_VEC:
5012 if (opcode->membership & INSN_5400)
5013 return RTYPE_FPU;
5014 return RTYPE_FPU | RTYPE_VEC;
5015
5016 case OP_REG_ACC:
5017 return RTYPE_ACC;
5018
5019 case OP_REG_COPRO:
5020 if (opcode->name[strlen (opcode->name) - 1] == '0')
5021 return RTYPE_NUM | RTYPE_CP0;
5022 return RTYPE_NUM;
5023
5024 case OP_REG_HW:
5025 return RTYPE_NUM;
14daeee3
RS
5026
5027 case OP_REG_VI:
5028 return RTYPE_NUM | RTYPE_VI;
5029
5030 case OP_REG_VF:
5031 return RTYPE_NUM | RTYPE_VF;
5032
5033 case OP_REG_R5900_I:
5034 return RTYPE_R5900_I;
5035
5036 case OP_REG_R5900_Q:
5037 return RTYPE_R5900_Q;
5038
5039 case OP_REG_R5900_R:
5040 return RTYPE_R5900_R;
5041
5042 case OP_REG_R5900_ACC:
5043 return RTYPE_R5900_ACC;
56d438b1
CF
5044
5045 case OP_REG_MSA:
5046 return RTYPE_MSA;
5047
5048 case OP_REG_MSA_CTRL:
5049 return RTYPE_NUM;
a1d78564
RS
5050 }
5051 abort ();
5052}
5053
5054/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5055
5056static void
5057check_regno (struct mips_arg_info *arg,
5058 enum mips_reg_operand_type type, unsigned int regno)
5059{
5060 if (AT && type == OP_REG_GP && regno == AT)
5061 arg->seen_at = TRUE;
5062
5063 if (type == OP_REG_FP
5064 && (regno & 1) != 0
a1d78564 5065 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5066 {
5067 /* This was a warning prior to introducing O32 FPXX and FP64 support
5068 so maintain a warning for FP32 but raise an error for the new
5069 cases. */
5070 if (FPR_SIZE == 32)
5071 as_warn (_("float register should be even, was %d"), regno);
5072 else
5073 as_bad (_("float register should be even, was %d"), regno);
5074 }
a1d78564
RS
5075
5076 if (type == OP_REG_CCC)
5077 {
5078 const char *name;
5079 size_t length;
5080
5081 name = arg->insn->insn_mo->name;
5082 length = strlen (name);
5083 if ((regno & 1) != 0
5084 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5085 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5086 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5087 name, regno);
5088
5089 if ((regno & 3) != 0
5090 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5091 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5092 name, regno);
5093 }
5094}
5095
a92713e6
RS
5096/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5097 a register of type TYPE. Return true on success, storing the register
5098 number in *REGNO and warning about any dubious uses. */
5099
5100static bfd_boolean
5101match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5102 unsigned int symval, unsigned int *regno)
5103{
5104 if (type == OP_REG_VEC)
5105 symval = mips_prefer_vec_regno (symval);
5106 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5107 return FALSE;
5108
5109 *regno = symval & RNUM_MASK;
5110 check_regno (arg, type, *regno);
5111 return TRUE;
5112}
5113
5114/* Try to interpret the next token in ARG as a register of type TYPE.
5115 Consume the token and return true on success, storing the register
5116 number in *REGNO. Return false on failure. */
5117
5118static bfd_boolean
5119match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5120 unsigned int *regno)
5121{
5122 if (arg->token->type == OT_REG
5123 && match_regno (arg, type, arg->token->u.regno, regno))
5124 {
5125 ++arg->token;
5126 return TRUE;
5127 }
5128 return FALSE;
5129}
5130
5131/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5132 Consume the token and return true on success, storing the register numbers
5133 in *REGNO1 and *REGNO2. Return false on failure. */
5134
5135static bfd_boolean
5136match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5137 unsigned int *regno1, unsigned int *regno2)
5138{
5139 if (match_reg (arg, type, regno1))
5140 {
5141 *regno2 = *regno1;
5142 return TRUE;
5143 }
5144 if (arg->token->type == OT_REG_RANGE
5145 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5146 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5147 && *regno1 <= *regno2)
5148 {
5149 ++arg->token;
5150 return TRUE;
5151 }
5152 return FALSE;
5153}
5154
a1d78564
RS
5155/* OP_INT matcher. */
5156
a92713e6 5157static bfd_boolean
a1d78564 5158match_int_operand (struct mips_arg_info *arg,
a92713e6 5159 const struct mips_operand *operand_base)
a1d78564
RS
5160{
5161 const struct mips_int_operand *operand;
3ccad066 5162 unsigned int uval;
a1d78564
RS
5163 int min_val, max_val, factor;
5164 offsetT sval;
a1d78564
RS
5165
5166 operand = (const struct mips_int_operand *) operand_base;
5167 factor = 1 << operand->shift;
3ccad066
RS
5168 min_val = mips_int_operand_min (operand);
5169 max_val = mips_int_operand_max (operand);
a1d78564 5170
d436c1c2
RS
5171 if (operand_base->lsb == 0
5172 && operand_base->size == 16
5173 && operand->shift == 0
5174 && operand->bias == 0
5175 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5176 {
5177 /* The operand can be relocated. */
a92713e6
RS
5178 if (!match_expression (arg, &offset_expr, offset_reloc))
5179 return FALSE;
5180
c96425c5
MR
5181 if (offset_expr.X_op == O_big)
5182 {
5183 match_out_of_range (arg);
5184 return FALSE;
5185 }
5186
a92713e6 5187 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5188 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5189 leave the relocation value in offset_expr and offset_relocs
5190 for the caller to process. */
a92713e6
RS
5191 return TRUE;
5192
5193 if (offset_expr.X_op != O_constant)
a1d78564 5194 {
60f20e8b
RS
5195 /* Accept non-constant operands if no later alternative matches,
5196 leaving it for the caller to process. */
5197 if (!arg->lax_match)
602b88e3
MR
5198 {
5199 match_not_constant (arg);
5200 return FALSE;
5201 }
a92713e6
RS
5202 offset_reloc[0] = BFD_RELOC_LO16;
5203 return TRUE;
a1d78564 5204 }
a92713e6 5205
a1d78564
RS
5206 /* Clear the global state; we're going to install the operand
5207 ourselves. */
a92713e6 5208 sval = offset_expr.X_add_number;
a1d78564 5209 offset_expr.X_op = O_absent;
60f20e8b
RS
5210
5211 /* For compatibility with older assemblers, we accept
5212 0x8000-0xffff as signed 16-bit numbers when only
5213 signed numbers are allowed. */
5214 if (sval > max_val)
5215 {
5216 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5217 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5218 {
5219 match_out_of_range (arg);
5220 return FALSE;
5221 }
60f20e8b 5222 }
a1d78564
RS
5223 }
5224 else
5225 {
1a00e612 5226 if (!match_const_int (arg, &sval))
a92713e6 5227 return FALSE;
a1d78564
RS
5228 }
5229
5230 arg->last_op_int = sval;
5231
1a00e612 5232 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5233 {
1a00e612
RS
5234 match_out_of_range (arg);
5235 return FALSE;
a1d78564
RS
5236 }
5237
5238 uval = (unsigned int) sval >> operand->shift;
5239 uval -= operand->bias;
5240
5241 /* Handle -mfix-cn63xxp1. */
5242 if (arg->opnum == 1
5243 && mips_fix_cn63xxp1
5244 && !mips_opts.micromips
5245 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5246 switch (uval)
5247 {
5248 case 5:
5249 case 25:
5250 case 26:
5251 case 27:
5252 case 28:
5253 case 29:
5254 case 30:
5255 case 31:
5256 /* These are ok. */
5257 break;
5258
5259 default:
5260 /* The rest must be changed to 28. */
5261 uval = 28;
5262 break;
5263 }
5264
5265 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5266 return TRUE;
a1d78564
RS
5267}
5268
5269/* OP_MAPPED_INT matcher. */
5270
a92713e6 5271static bfd_boolean
a1d78564 5272match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5273 const struct mips_operand *operand_base)
a1d78564
RS
5274{
5275 const struct mips_mapped_int_operand *operand;
5276 unsigned int uval, num_vals;
5277 offsetT sval;
5278
5279 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5280 if (!match_const_int (arg, &sval))
a92713e6 5281 return FALSE;
a1d78564
RS
5282
5283 num_vals = 1 << operand_base->size;
5284 for (uval = 0; uval < num_vals; uval++)
5285 if (operand->int_map[uval] == sval)
5286 break;
5287 if (uval == num_vals)
1a00e612
RS
5288 {
5289 match_out_of_range (arg);
5290 return FALSE;
5291 }
a1d78564
RS
5292
5293 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5294 return TRUE;
a1d78564
RS
5295}
5296
5297/* OP_MSB matcher. */
5298
a92713e6 5299static bfd_boolean
a1d78564 5300match_msb_operand (struct mips_arg_info *arg,
a92713e6 5301 const struct mips_operand *operand_base)
a1d78564
RS
5302{
5303 const struct mips_msb_operand *operand;
5304 int min_val, max_val, max_high;
5305 offsetT size, sval, high;
5306
5307 operand = (const struct mips_msb_operand *) operand_base;
5308 min_val = operand->bias;
5309 max_val = min_val + (1 << operand_base->size) - 1;
5310 max_high = operand->opsize;
5311
1a00e612 5312 if (!match_const_int (arg, &size))
a92713e6 5313 return FALSE;
a1d78564
RS
5314
5315 high = size + arg->last_op_int;
5316 sval = operand->add_lsb ? high : size;
5317
5318 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5319 {
1a00e612
RS
5320 match_out_of_range (arg);
5321 return FALSE;
a1d78564
RS
5322 }
5323 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5324 return TRUE;
a1d78564
RS
5325}
5326
5327/* OP_REG matcher. */
5328
a92713e6 5329static bfd_boolean
a1d78564 5330match_reg_operand (struct mips_arg_info *arg,
a92713e6 5331 const struct mips_operand *operand_base)
a1d78564
RS
5332{
5333 const struct mips_reg_operand *operand;
a92713e6 5334 unsigned int regno, uval, num_vals;
a1d78564
RS
5335
5336 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5337 if (!match_reg (arg, operand->reg_type, &regno))
5338 return FALSE;
a1d78564
RS
5339
5340 if (operand->reg_map)
5341 {
5342 num_vals = 1 << operand->root.size;
5343 for (uval = 0; uval < num_vals; uval++)
5344 if (operand->reg_map[uval] == regno)
5345 break;
5346 if (num_vals == uval)
a92713e6 5347 return FALSE;
a1d78564
RS
5348 }
5349 else
5350 uval = regno;
5351
a1d78564
RS
5352 arg->last_regno = regno;
5353 if (arg->opnum == 1)
5354 arg->dest_regno = regno;
5355 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5356 return TRUE;
a1d78564
RS
5357}
5358
5359/* OP_REG_PAIR matcher. */
5360
a92713e6 5361static bfd_boolean
a1d78564 5362match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5363 const struct mips_operand *operand_base)
a1d78564
RS
5364{
5365 const struct mips_reg_pair_operand *operand;
a92713e6 5366 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5367
5368 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5369 if (!match_reg (arg, operand->reg_type, &regno1)
5370 || !match_char (arg, ',')
5371 || !match_reg (arg, operand->reg_type, &regno2))
5372 return FALSE;
a1d78564
RS
5373
5374 num_vals = 1 << operand_base->size;
5375 for (uval = 0; uval < num_vals; uval++)
5376 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5377 break;
5378 if (uval == num_vals)
a92713e6 5379 return FALSE;
a1d78564 5380
a1d78564 5381 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5382 return TRUE;
a1d78564
RS
5383}
5384
5385/* OP_PCREL matcher. The caller chooses the relocation type. */
5386
a92713e6
RS
5387static bfd_boolean
5388match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5389{
a92713e6
RS
5390 bfd_reloc_code_real_type r[3];
5391
5392 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5393}
5394
5395/* OP_PERF_REG matcher. */
5396
a92713e6 5397static bfd_boolean
a1d78564 5398match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5399 const struct mips_operand *operand)
a1d78564
RS
5400{
5401 offsetT sval;
5402
1a00e612 5403 if (!match_const_int (arg, &sval))
a92713e6 5404 return FALSE;
a1d78564
RS
5405
5406 if (sval != 0
5407 && (sval != 1
5408 || (mips_opts.arch == CPU_R5900
5409 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5410 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5411 {
1a00e612
RS
5412 set_insn_error (arg->argnum, _("invalid performance register"));
5413 return FALSE;
a1d78564
RS
5414 }
5415
5416 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5417 return TRUE;
a1d78564
RS
5418}
5419
5420/* OP_ADDIUSP matcher. */
5421
a92713e6 5422static bfd_boolean
a1d78564 5423match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5424 const struct mips_operand *operand)
a1d78564
RS
5425{
5426 offsetT sval;
5427 unsigned int uval;
5428
1a00e612 5429 if (!match_const_int (arg, &sval))
a92713e6 5430 return FALSE;
a1d78564
RS
5431
5432 if (sval % 4)
1a00e612
RS
5433 {
5434 match_out_of_range (arg);
5435 return FALSE;
5436 }
a1d78564
RS
5437
5438 sval /= 4;
5439 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5440 {
5441 match_out_of_range (arg);
5442 return FALSE;
5443 }
a1d78564
RS
5444
5445 uval = (unsigned int) sval;
5446 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5447 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5448 return TRUE;
a1d78564
RS
5449}
5450
5451/* OP_CLO_CLZ_DEST matcher. */
5452
a92713e6 5453static bfd_boolean
a1d78564 5454match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5455 const struct mips_operand *operand)
a1d78564
RS
5456{
5457 unsigned int regno;
5458
a92713e6
RS
5459 if (!match_reg (arg, OP_REG_GP, &regno))
5460 return FALSE;
a1d78564 5461
a1d78564 5462 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5463 return TRUE;
a1d78564
RS
5464}
5465
7361da2c
AB
5466/* OP_CHECK_PREV matcher. */
5467
5468static bfd_boolean
5469match_check_prev_operand (struct mips_arg_info *arg,
5470 const struct mips_operand *operand_base)
5471{
5472 const struct mips_check_prev_operand *operand;
5473 unsigned int regno;
5474
5475 operand = (const struct mips_check_prev_operand *) operand_base;
5476
5477 if (!match_reg (arg, OP_REG_GP, &regno))
5478 return FALSE;
5479
5480 if (!operand->zero_ok && regno == 0)
5481 return FALSE;
5482
5483 if ((operand->less_than_ok && regno < arg->last_regno)
5484 || (operand->greater_than_ok && regno > arg->last_regno)
5485 || (operand->equal_ok && regno == arg->last_regno))
5486 {
5487 arg->last_regno = regno;
5488 insn_insert_operand (arg->insn, operand_base, regno);
5489 return TRUE;
5490 }
5491
5492 return FALSE;
5493}
5494
5495/* OP_SAME_RS_RT matcher. */
5496
5497static bfd_boolean
5498match_same_rs_rt_operand (struct mips_arg_info *arg,
5499 const struct mips_operand *operand)
5500{
5501 unsigned int regno;
5502
5503 if (!match_reg (arg, OP_REG_GP, &regno))
5504 return FALSE;
5505
5506 if (regno == 0)
5507 {
5508 set_insn_error (arg->argnum, _("the source register must not be $0"));
5509 return FALSE;
5510 }
5511
5512 arg->last_regno = regno;
5513
5514 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5515 return TRUE;
5516}
5517
a1d78564
RS
5518/* OP_LWM_SWM_LIST matcher. */
5519
a92713e6 5520static bfd_boolean
a1d78564 5521match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5522 const struct mips_operand *operand)
a1d78564 5523{
a92713e6
RS
5524 unsigned int reglist, sregs, ra, regno1, regno2;
5525 struct mips_arg_info reset;
a1d78564 5526
a92713e6
RS
5527 reglist = 0;
5528 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5529 return FALSE;
5530 do
5531 {
5532 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5533 {
5534 reglist |= 1 << FP;
5535 regno2 = S7;
5536 }
5537 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5538 reset = *arg;
5539 }
5540 while (match_char (arg, ',')
5541 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5542 *arg = reset;
a1d78564
RS
5543
5544 if (operand->size == 2)
5545 {
5546 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5547
5548 s0, ra
5549 s0, s1, ra, s2, s3
5550 s0-s2, ra
5551
5552 and any permutations of these. */
5553 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5554 return FALSE;
a1d78564
RS
5555
5556 sregs = (reglist >> 17) & 7;
5557 ra = 0;
5558 }
5559 else
5560 {
5561 /* The list must include at least one of ra and s0-sN,
5562 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5563 which are $23 and $30 respectively.) E.g.:
5564
5565 ra
5566 s0
5567 ra, s0, s1, s2
5568 s0-s8
5569 s0-s5, ra
5570
5571 and any permutations of these. */
5572 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5573 return FALSE;
a1d78564
RS
5574
5575 ra = (reglist >> 27) & 0x10;
5576 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5577 }
5578 sregs += 1;
5579 if ((sregs & -sregs) != sregs)
a92713e6 5580 return FALSE;
a1d78564
RS
5581
5582 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5583 return TRUE;
a1d78564
RS
5584}
5585
364215c8
RS
5586/* OP_ENTRY_EXIT_LIST matcher. */
5587
a92713e6 5588static unsigned int
364215c8 5589match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5590 const struct mips_operand *operand)
364215c8
RS
5591{
5592 unsigned int mask;
5593 bfd_boolean is_exit;
5594
5595 /* The format is the same for both ENTRY and EXIT, but the constraints
5596 are different. */
5597 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5598 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5599 do
364215c8
RS
5600 {
5601 unsigned int regno1, regno2;
5602 bfd_boolean is_freg;
5603
a92713e6 5604 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5605 is_freg = FALSE;
a92713e6 5606 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5607 is_freg = TRUE;
5608 else
a92713e6 5609 return FALSE;
364215c8
RS
5610
5611 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5612 {
5613 mask &= ~(7 << 3);
5614 mask |= (5 + regno2) << 3;
5615 }
5616 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5617 mask |= (regno2 - 3) << 3;
5618 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5619 mask |= (regno2 - 15) << 1;
5620 else if (regno1 == RA && regno2 == RA)
5621 mask |= 1;
5622 else
a92713e6 5623 return FALSE;
364215c8 5624 }
a92713e6
RS
5625 while (match_char (arg, ','));
5626
364215c8 5627 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5628 return TRUE;
364215c8
RS
5629}
5630
38bf472a
MR
5631/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5632 the argument register mask AMASK, the number of static registers
5633 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5634 respectively, and the frame size FRAME_SIZE. */
5635
5636static unsigned int
5637mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5638 unsigned int ra, unsigned int s0, unsigned int s1,
5639 unsigned int frame_size)
5640{
5641 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5642 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5643}
5644
5645/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5646 argument register mask AMASK, the number of static registers saved
5647 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5648 respectively, and the frame size FRAME_SIZE. */
5649
5650static unsigned int
5651mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5652 unsigned int ra, unsigned int s0, unsigned int s1,
5653 unsigned int frame_size)
5654{
5655 unsigned int args;
5656
5657 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5658 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5659 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5660 | ((frame_size & 0xf0) << 16));
5661 return args;
5662}
5663
364215c8
RS
5664/* OP_SAVE_RESTORE_LIST matcher. */
5665
a92713e6
RS
5666static bfd_boolean
5667match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5668{
5669 unsigned int opcode, args, statics, sregs;
5670 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5671 unsigned int arg_mask, ra, s0, s1;
364215c8 5672 offsetT frame_size;
364215c8 5673
364215c8
RS
5674 opcode = arg->insn->insn_opcode;
5675 frame_size = 0;
5676 num_frame_sizes = 0;
5677 args = 0;
5678 statics = 0;
5679 sregs = 0;
38bf472a
MR
5680 ra = 0;
5681 s0 = 0;
5682 s1 = 0;
a92713e6 5683 do
364215c8
RS
5684 {
5685 unsigned int regno1, regno2;
5686
a92713e6 5687 if (arg->token->type == OT_INTEGER)
364215c8
RS
5688 {
5689 /* Handle the frame size. */
1a00e612 5690 if (!match_const_int (arg, &frame_size))
a92713e6 5691 return FALSE;
364215c8 5692 num_frame_sizes += 1;
364215c8
RS
5693 }
5694 else
5695 {
a92713e6
RS
5696 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5697 return FALSE;
364215c8
RS
5698
5699 while (regno1 <= regno2)
5700 {
5701 if (regno1 >= 4 && regno1 <= 7)
5702 {
5703 if (num_frame_sizes == 0)
5704 /* args $a0-$a3 */
5705 args |= 1 << (regno1 - 4);
5706 else
5707 /* statics $a0-$a3 */
5708 statics |= 1 << (regno1 - 4);
5709 }
5710 else if (regno1 >= 16 && regno1 <= 23)
5711 /* $s0-$s7 */
5712 sregs |= 1 << (regno1 - 16);
5713 else if (regno1 == 30)
5714 /* $s8 */
5715 sregs |= 1 << 8;
5716 else if (regno1 == 31)
5717 /* Add $ra to insn. */
38bf472a 5718 ra = 1;
364215c8 5719 else
a92713e6 5720 return FALSE;
364215c8
RS
5721 regno1 += 1;
5722 if (regno1 == 24)
5723 regno1 = 30;
5724 }
5725 }
364215c8 5726 }
a92713e6 5727 while (match_char (arg, ','));
364215c8
RS
5728
5729 /* Encode args/statics combination. */
5730 if (args & statics)
a92713e6 5731 return FALSE;
364215c8
RS
5732 else if (args == 0xf)
5733 /* All $a0-$a3 are args. */
38bf472a 5734 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5735 else if (statics == 0xf)
5736 /* All $a0-$a3 are statics. */
38bf472a 5737 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5738 else
5739 {
5740 /* Count arg registers. */
5741 num_args = 0;
5742 while (args & 0x1)
5743 {
5744 args >>= 1;
5745 num_args += 1;
5746 }
5747 if (args != 0)
a92713e6 5748 return FALSE;
364215c8
RS
5749
5750 /* Count static registers. */
5751 num_statics = 0;
5752 while (statics & 0x8)
5753 {
5754 statics = (statics << 1) & 0xf;
5755 num_statics += 1;
5756 }
5757 if (statics != 0)
a92713e6 5758 return FALSE;
364215c8
RS
5759
5760 /* Encode args/statics. */
38bf472a 5761 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5762 }
5763
5764 /* Encode $s0/$s1. */
5765 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5766 s0 = 1;
364215c8 5767 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5768 s1 = 1;
364215c8
RS
5769 sregs >>= 2;
5770
5771 /* Encode $s2-$s8. */
5772 num_sregs = 0;
5773 while (sregs & 1)
5774 {
5775 sregs >>= 1;
5776 num_sregs += 1;
5777 }
5778 if (sregs != 0)
a92713e6 5779 return FALSE;
364215c8
RS
5780
5781 /* Encode frame size. */
5782 if (num_frame_sizes == 0)
1a00e612
RS
5783 {
5784 set_insn_error (arg->argnum, _("missing frame size"));
5785 return FALSE;
5786 }
5787 if (num_frame_sizes > 1)
5788 {
5789 set_insn_error (arg->argnum, _("frame size specified twice"));
5790 return FALSE;
5791 }
5792 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5793 {
5794 set_insn_error (arg->argnum, _("invalid frame size"));
5795 return FALSE;
5796 }
38bf472a 5797 frame_size /= 8;
364215c8 5798
364215c8 5799 /* Finally build the instruction. */
38bf472a
MR
5800 if (mips_opts.mips16)
5801 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5802 frame_size);
5803 else if (!mips_opts.micromips)
5804 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5805 frame_size);
5806 else
5807 abort ();
5808
364215c8 5809 arg->insn->insn_opcode = opcode;
a92713e6 5810 return TRUE;
364215c8
RS
5811}
5812
a1d78564
RS
5813/* OP_MDMX_IMM_REG matcher. */
5814
a92713e6 5815static bfd_boolean
a1d78564 5816match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5817 const struct mips_operand *operand)
a1d78564 5818{
a92713e6 5819 unsigned int regno, uval;
a1d78564
RS
5820 bfd_boolean is_qh;
5821 const struct mips_opcode *opcode;
5822
5823 /* The mips_opcode records whether this is an octobyte or quadhalf
5824 instruction. Start out with that bit in place. */
5825 opcode = arg->insn->insn_mo;
5826 uval = mips_extract_operand (operand, opcode->match);
5827 is_qh = (uval != 0);
5828
56d438b1 5829 if (arg->token->type == OT_REG)
a1d78564
RS
5830 {
5831 if ((opcode->membership & INSN_5400)
5832 && strcmp (opcode->name, "rzu.ob") == 0)
5833 {
1a00e612
RS
5834 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5835 arg->argnum);
5836 return FALSE;
a1d78564
RS
5837 }
5838
56d438b1
CF
5839 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5840 return FALSE;
5841 ++arg->token;
5842
a1d78564
RS
5843 /* Check whether this is a vector register or a broadcast of
5844 a single element. */
56d438b1 5845 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5846 {
56d438b1 5847 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5848 {
1a00e612
RS
5849 set_insn_error (arg->argnum, _("invalid element selector"));
5850 return FALSE;
a1d78564 5851 }
56d438b1
CF
5852 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5853 ++arg->token;
a1d78564
RS
5854 }
5855 else
5856 {
5857 /* A full vector. */
5858 if ((opcode->membership & INSN_5400)
5859 && (strcmp (opcode->name, "sll.ob") == 0
5860 || strcmp (opcode->name, "srl.ob") == 0))
5861 {
1a00e612
RS
5862 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5863 arg->argnum);
5864 return FALSE;
a1d78564
RS
5865 }
5866
5867 if (is_qh)
5868 uval |= MDMX_FMTSEL_VEC_QH << 5;
5869 else
5870 uval |= MDMX_FMTSEL_VEC_OB << 5;
5871 }
a1d78564
RS
5872 uval |= regno;
5873 }
5874 else
5875 {
5876 offsetT sval;
5877
1a00e612 5878 if (!match_const_int (arg, &sval))
a92713e6 5879 return FALSE;
a1d78564
RS
5880 if (sval < 0 || sval > 31)
5881 {
1a00e612
RS
5882 match_out_of_range (arg);
5883 return FALSE;
a1d78564
RS
5884 }
5885 uval |= (sval & 31);
5886 if (is_qh)
5887 uval |= MDMX_FMTSEL_IMM_QH << 5;
5888 else
5889 uval |= MDMX_FMTSEL_IMM_OB << 5;
5890 }
5891 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5892 return TRUE;
a1d78564
RS
5893}
5894
56d438b1
CF
5895/* OP_IMM_INDEX matcher. */
5896
5897static bfd_boolean
5898match_imm_index_operand (struct mips_arg_info *arg,
5899 const struct mips_operand *operand)
5900{
5901 unsigned int max_val;
5902
5903 if (arg->token->type != OT_INTEGER_INDEX)
5904 return FALSE;
5905
5906 max_val = (1 << operand->size) - 1;
5907 if (arg->token->u.index > max_val)
5908 {
5909 match_out_of_range (arg);
5910 return FALSE;
5911 }
5912 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5913 ++arg->token;
5914 return TRUE;
5915}
5916
5917/* OP_REG_INDEX matcher. */
5918
5919static bfd_boolean
5920match_reg_index_operand (struct mips_arg_info *arg,
5921 const struct mips_operand *operand)
5922{
5923 unsigned int regno;
5924
5925 if (arg->token->type != OT_REG_INDEX)
5926 return FALSE;
5927
5928 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5929 return FALSE;
5930
5931 insn_insert_operand (arg->insn, operand, regno);
5932 ++arg->token;
5933 return TRUE;
5934}
5935
a1d78564
RS
5936/* OP_PC matcher. */
5937
a92713e6
RS
5938static bfd_boolean
5939match_pc_operand (struct mips_arg_info *arg)
a1d78564 5940{
a92713e6
RS
5941 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5942 {
5943 ++arg->token;
5944 return TRUE;
5945 }
5946 return FALSE;
a1d78564
RS
5947}
5948
25499ac7
MR
5949/* OP_REG28 matcher. */
5950
5951static bfd_boolean
5952match_reg28_operand (struct mips_arg_info *arg)
5953{
5954 unsigned int regno;
5955
5956 if (arg->token->type == OT_REG
5957 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5958 && regno == GP)
5959 {
5960 ++arg->token;
5961 return TRUE;
5962 }
5963 return FALSE;
5964}
5965
7361da2c
AB
5966/* OP_NON_ZERO_REG matcher. */
5967
5968static bfd_boolean
5969match_non_zero_reg_operand (struct mips_arg_info *arg,
5970 const struct mips_operand *operand)
5971{
5972 unsigned int regno;
5973
5974 if (!match_reg (arg, OP_REG_GP, &regno))
5975 return FALSE;
5976
5977 if (regno == 0)
5978 return FALSE;
5979
5980 arg->last_regno = regno;
5981 insn_insert_operand (arg->insn, operand, regno);
5982 return TRUE;
5983}
5984
a1d78564
RS
5985/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5986 register that we need to match. */
5987
a92713e6
RS
5988static bfd_boolean
5989match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5990{
5991 unsigned int regno;
5992
a92713e6 5993 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5994}
5995
33f46696
MR
5996/* Try to match a floating-point constant from ARG for LI.S or LI.D.
5997 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5998 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
5999
6000 Return the constant in IMM and OFFSET as follows:
6001
6002 - If the constant should be loaded via memory, set IMM to O_absent and
6003 OFFSET to the memory address.
6004
6005 - Otherwise, if the constant should be loaded into two 32-bit registers,
6006 set IMM to the O_constant to load into the high register and OFFSET
6007 to the corresponding value for the low register.
6008
6009 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
6010
6011 These constants only appear as the last operand in an instruction,
6012 and every instruction that accepts them in any variant accepts them
6013 in all variants. This means we don't have to worry about backing out
6014 any changes if the instruction does not match. We just match
6015 unconditionally and report an error if the constant is invalid. */
6016
a92713e6
RS
6017static bfd_boolean
6018match_float_constant (struct mips_arg_info *arg, expressionS *imm,
6019 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 6020{
a92713e6 6021 char *p;
89565f1b
RS
6022 segT seg, new_seg;
6023 subsegT subseg;
6024 const char *newname;
a92713e6 6025 unsigned char *data;
89565f1b
RS
6026
6027 /* Where the constant is placed is based on how the MIPS assembler
6028 does things:
6029
6030 length == 4 && using_gprs -- immediate value only
6031 length == 8 && using_gprs -- .rdata or immediate value
6032 length == 4 && !using_gprs -- .lit4 or immediate value
6033 length == 8 && !using_gprs -- .lit8 or immediate value
6034
6035 The .lit4 and .lit8 sections are only used if permitted by the
6036 -G argument. */
a92713e6 6037 if (arg->token->type != OT_FLOAT)
1a00e612
RS
6038 {
6039 set_insn_error (arg->argnum, _("floating-point expression required"));
6040 return FALSE;
6041 }
a92713e6
RS
6042
6043 gas_assert (arg->token->u.flt.length == length);
6044 data = arg->token->u.flt.data;
6045 ++arg->token;
89565f1b
RS
6046
6047 /* Handle 32-bit constants for which an immediate value is best. */
6048 if (length == 4
6049 && (using_gprs
6050 || g_switch_value < 4
6051 || (data[0] == 0 && data[1] == 0)
6052 || (data[2] == 0 && data[3] == 0)))
6053 {
6054 imm->X_op = O_constant;
6055 if (!target_big_endian)
6056 imm->X_add_number = bfd_getl32 (data);
6057 else
6058 imm->X_add_number = bfd_getb32 (data);
6059 offset->X_op = O_absent;
a92713e6 6060 return TRUE;
89565f1b
RS
6061 }
6062
6063 /* Handle 64-bit constants for which an immediate value is best. */
6064 if (length == 8
6065 && !mips_disable_float_construction
351cdf24
MF
6066 /* Constants can only be constructed in GPRs and copied to FPRs if the
6067 GPRs are at least as wide as the FPRs or MTHC1 is available.
6068 Unlike most tests for 32-bit floating-point registers this check
6069 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6070 permit 64-bit moves without MXHC1.
6071 Force the constant into memory otherwise. */
6072 && (using_gprs
6073 || GPR_SIZE == 64
6074 || ISA_HAS_MXHC1 (mips_opts.isa)
6075 || FPR_SIZE == 32)
89565f1b
RS
6076 && ((data[0] == 0 && data[1] == 0)
6077 || (data[2] == 0 && data[3] == 0))
6078 && ((data[4] == 0 && data[5] == 0)
6079 || (data[6] == 0 && data[7] == 0)))
6080 {
6081 /* The value is simple enough to load with a couple of instructions.
6082 If using 32-bit registers, set IMM to the high order 32 bits and
6083 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6084 64 bit constant. */
351cdf24 6085 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6086 {
6087 imm->X_op = O_constant;
6088 offset->X_op = O_constant;
6089 if (!target_big_endian)
6090 {
6091 imm->X_add_number = bfd_getl32 (data + 4);
6092 offset->X_add_number = bfd_getl32 (data);
6093 }
6094 else
6095 {
6096 imm->X_add_number = bfd_getb32 (data);
6097 offset->X_add_number = bfd_getb32 (data + 4);
6098 }
6099 if (offset->X_add_number == 0)
6100 offset->X_op = O_absent;
6101 }
6102 else
6103 {
6104 imm->X_op = O_constant;
6105 if (!target_big_endian)
6106 imm->X_add_number = bfd_getl64 (data);
6107 else
6108 imm->X_add_number = bfd_getb64 (data);
6109 offset->X_op = O_absent;
6110 }
a92713e6 6111 return TRUE;
89565f1b
RS
6112 }
6113
6114 /* Switch to the right section. */
6115 seg = now_seg;
6116 subseg = now_subseg;
6117 if (length == 4)
6118 {
6119 gas_assert (!using_gprs && g_switch_value >= 4);
6120 newname = ".lit4";
6121 }
6122 else
6123 {
6124 if (using_gprs || g_switch_value < 8)
6125 newname = RDATA_SECTION_NAME;
6126 else
6127 newname = ".lit8";
6128 }
6129
6130 new_seg = subseg_new (newname, (subsegT) 0);
6131 bfd_set_section_flags (stdoutput, new_seg,
6132 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6133 frag_align (length == 4 ? 2 : 3, 0, 0);
6134 if (strncmp (TARGET_OS, "elf", 3) != 0)
6135 record_alignment (new_seg, 4);
6136 else
6137 record_alignment (new_seg, length == 4 ? 2 : 3);
6138 if (seg == now_seg)
1661c76c 6139 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6140
6141 /* Set the argument to the current address in the section. */
6142 imm->X_op = O_absent;
6143 offset->X_op = O_symbol;
6144 offset->X_add_symbol = symbol_temp_new_now ();
6145 offset->X_add_number = 0;
6146
6147 /* Put the floating point number into the section. */
6148 p = frag_more (length);
6149 memcpy (p, data, length);
6150
6151 /* Switch back to the original section. */
6152 subseg_set (seg, subseg);
a92713e6 6153 return TRUE;
89565f1b
RS
6154}
6155
14daeee3
RS
6156/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6157 them. */
6158
6159static bfd_boolean
6160match_vu0_suffix_operand (struct mips_arg_info *arg,
6161 const struct mips_operand *operand,
6162 bfd_boolean match_p)
6163{
6164 unsigned int uval;
6165
6166 /* The operand can be an XYZW mask or a single 2-bit channel index
6167 (with X being 0). */
6168 gas_assert (operand->size == 2 || operand->size == 4);
6169
ee5734f0 6170 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6171 if (arg->token->type != OT_CHANNELS)
ee5734f0 6172 return match_p;
14daeee3
RS
6173
6174 uval = arg->token->u.channels;
6175 if (operand->size == 2)
6176 {
6177 /* Check that a single bit is set and convert it into a 2-bit index. */
6178 if ((uval & -uval) != uval)
6179 return FALSE;
6180 uval = 4 - ffs (uval);
6181 }
6182
6183 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6184 return FALSE;
6185
6186 ++arg->token;
6187 if (!match_p)
6188 insn_insert_operand (arg->insn, operand, uval);
6189 return TRUE;
6190}
6191
33f46696
MR
6192/* Try to match a token from ARG against OPERAND. Consume the token
6193 and return true on success, otherwise return false. */
a1d78564 6194
a92713e6 6195static bfd_boolean
a1d78564 6196match_operand (struct mips_arg_info *arg,
a92713e6 6197 const struct mips_operand *operand)
a1d78564
RS
6198{
6199 switch (operand->type)
6200 {
6201 case OP_INT:
a92713e6 6202 return match_int_operand (arg, operand);
a1d78564
RS
6203
6204 case OP_MAPPED_INT:
a92713e6 6205 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6206
6207 case OP_MSB:
a92713e6 6208 return match_msb_operand (arg, operand);
a1d78564
RS
6209
6210 case OP_REG:
0f35dbc4 6211 case OP_OPTIONAL_REG:
a92713e6 6212 return match_reg_operand (arg, operand);
a1d78564
RS
6213
6214 case OP_REG_PAIR:
a92713e6 6215 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6216
6217 case OP_PCREL:
a92713e6 6218 return match_pcrel_operand (arg);
a1d78564
RS
6219
6220 case OP_PERF_REG:
a92713e6 6221 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6222
6223 case OP_ADDIUSP_INT:
a92713e6 6224 return match_addiusp_operand (arg, operand);
a1d78564
RS
6225
6226 case OP_CLO_CLZ_DEST:
a92713e6 6227 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6228
6229 case OP_LWM_SWM_LIST:
a92713e6 6230 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6231
6232 case OP_ENTRY_EXIT_LIST:
a92713e6 6233 return match_entry_exit_operand (arg, operand);
364215c8 6234
a1d78564 6235 case OP_SAVE_RESTORE_LIST:
a92713e6 6236 return match_save_restore_list_operand (arg);
a1d78564
RS
6237
6238 case OP_MDMX_IMM_REG:
a92713e6 6239 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6240
6241 case OP_REPEAT_DEST_REG:
a92713e6 6242 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6243
6244 case OP_REPEAT_PREV_REG:
a92713e6 6245 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6246
6247 case OP_PC:
a92713e6 6248 return match_pc_operand (arg);
14daeee3 6249
25499ac7
MR
6250 case OP_REG28:
6251 return match_reg28_operand (arg);
6252
14daeee3
RS
6253 case OP_VU0_SUFFIX:
6254 return match_vu0_suffix_operand (arg, operand, FALSE);
6255
6256 case OP_VU0_MATCH_SUFFIX:
6257 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6258
6259 case OP_IMM_INDEX:
6260 return match_imm_index_operand (arg, operand);
6261
6262 case OP_REG_INDEX:
6263 return match_reg_index_operand (arg, operand);
7361da2c
AB
6264
6265 case OP_SAME_RS_RT:
6266 return match_same_rs_rt_operand (arg, operand);
6267
6268 case OP_CHECK_PREV:
6269 return match_check_prev_operand (arg, operand);
6270
6271 case OP_NON_ZERO_REG:
6272 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6273 }
6274 abort ();
6275}
6276
6277/* ARG is the state after successfully matching an instruction.
6278 Issue any queued-up warnings. */
6279
6280static void
6281check_completed_insn (struct mips_arg_info *arg)
6282{
6283 if (arg->seen_at)
6284 {
6285 if (AT == ATREG)
1661c76c 6286 as_warn (_("used $at without \".set noat\""));
a1d78564 6287 else
1661c76c 6288 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6289 }
6290}
a1d78564 6291
85fcb30f
RS
6292/* Return true if modifying general-purpose register REG needs a delay. */
6293
6294static bfd_boolean
6295reg_needs_delay (unsigned int reg)
6296{
6297 unsigned long prev_pinfo;
6298
6299 prev_pinfo = history[0].insn_mo->pinfo;
6300 if (!mips_opts.noreorder
67dc82bc 6301 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6302 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6303 && (gpr_write_mask (&history[0]) & (1 << reg)))
6304 return TRUE;
6305
6306 return FALSE;
6307}
6308
71400594
RS
6309/* Classify an instruction according to the FIX_VR4120_* enumeration.
6310 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6311 by VR4120 errata. */
4d7206a2 6312
71400594
RS
6313static unsigned int
6314classify_vr4120_insn (const char *name)
252b5132 6315{
71400594
RS
6316 if (strncmp (name, "macc", 4) == 0)
6317 return FIX_VR4120_MACC;
6318 if (strncmp (name, "dmacc", 5) == 0)
6319 return FIX_VR4120_DMACC;
6320 if (strncmp (name, "mult", 4) == 0)
6321 return FIX_VR4120_MULT;
6322 if (strncmp (name, "dmult", 5) == 0)
6323 return FIX_VR4120_DMULT;
6324 if (strstr (name, "div"))
6325 return FIX_VR4120_DIV;
6326 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6327 return FIX_VR4120_MTHILO;
6328 return NUM_FIX_VR4120_CLASSES;
6329}
252b5132 6330
a8d14a88
CM
6331#define INSN_ERET 0x42000018
6332#define INSN_DERET 0x4200001f
6333#define INSN_DMULT 0x1c
6334#define INSN_DMULTU 0x1d
ff239038 6335
71400594
RS
6336/* Return the number of instructions that must separate INSN1 and INSN2,
6337 where INSN1 is the earlier instruction. Return the worst-case value
6338 for any INSN2 if INSN2 is null. */
252b5132 6339
71400594
RS
6340static unsigned int
6341insns_between (const struct mips_cl_insn *insn1,
6342 const struct mips_cl_insn *insn2)
6343{
6344 unsigned long pinfo1, pinfo2;
4c260379 6345 unsigned int mask;
71400594 6346
85fcb30f
RS
6347 /* If INFO2 is null, pessimistically assume that all flags are set for
6348 the second instruction. */
71400594
RS
6349 pinfo1 = insn1->insn_mo->pinfo;
6350 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6351
71400594
RS
6352 /* For most targets, write-after-read dependencies on the HI and LO
6353 registers must be separated by at least two instructions. */
6354 if (!hilo_interlocks)
252b5132 6355 {
71400594
RS
6356 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6357 return 2;
6358 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6359 return 2;
6360 }
6361
6362 /* If we're working around r7000 errata, there must be two instructions
6363 between an mfhi or mflo and any instruction that uses the result. */
6364 if (mips_7000_hilo_fix
df58fc94 6365 && !mips_opts.micromips
71400594 6366 && MF_HILO_INSN (pinfo1)
85fcb30f 6367 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6368 return 2;
6369
ff239038
CM
6370 /* If we're working around 24K errata, one instruction is required
6371 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6372 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6373 {
6374 if (insn1->insn_opcode == INSN_ERET
6375 || insn1->insn_opcode == INSN_DERET)
6376 {
6377 if (insn2 == NULL
6378 || insn2->insn_opcode == INSN_ERET
6379 || insn2->insn_opcode == INSN_DERET
11625dd8 6380 || delayed_branch_p (insn2))
ff239038
CM
6381 return 1;
6382 }
6383 }
6384
a8d14a88
CM
6385 /* If we're working around PMC RM7000 errata, there must be three
6386 nops between a dmult and a load instruction. */
6387 if (mips_fix_rm7000 && !mips_opts.micromips)
6388 {
6389 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6390 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6391 {
6392 if (pinfo2 & INSN_LOAD_MEMORY)
6393 return 3;
6394 }
6395 }
6396
71400594
RS
6397 /* If working around VR4120 errata, check for combinations that need
6398 a single intervening instruction. */
df58fc94 6399 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6400 {
6401 unsigned int class1, class2;
252b5132 6402
71400594
RS
6403 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6404 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6405 {
71400594
RS
6406 if (insn2 == NULL)
6407 return 1;
6408 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6409 if (vr4120_conflicts[class1] & (1 << class2))
6410 return 1;
252b5132 6411 }
71400594
RS
6412 }
6413
df58fc94 6414 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6415 {
6416 /* Check for GPR or coprocessor load delays. All such delays
6417 are on the RT register. */
6418 /* Itbl support may require additional care here. */
67dc82bc 6419 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6420 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6421 {
85fcb30f 6422 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6423 return 1;
6424 }
6425
6426 /* Check for generic coprocessor hazards.
6427
6428 This case is not handled very well. There is no special
6429 knowledge of CP0 handling, and the coprocessors other than
6430 the floating point unit are not distinguished at all. */
6431 /* Itbl support may require additional care here. FIXME!
6432 Need to modify this to include knowledge about
6433 user specified delays! */
43885403 6434 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6435 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6436 {
6437 /* Handle cases where INSN1 writes to a known general coprocessor
6438 register. There must be a one instruction delay before INSN2
6439 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6440 mask = fpr_write_mask (insn1);
6441 if (mask != 0)
252b5132 6442 {
4c260379 6443 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6444 return 1;
252b5132
RH
6445 }
6446 else
6447 {
71400594
RS
6448 /* Read-after-write dependencies on the control registers
6449 require a two-instruction gap. */
6450 if ((pinfo1 & INSN_WRITE_COND_CODE)
6451 && (pinfo2 & INSN_READ_COND_CODE))
6452 return 2;
6453
6454 /* We don't know exactly what INSN1 does. If INSN2 is
6455 also a coprocessor instruction, assume there must be
6456 a one instruction gap. */
6457 if (pinfo2 & INSN_COP)
6458 return 1;
252b5132
RH
6459 }
6460 }
6b76fefe 6461
71400594
RS
6462 /* Check for read-after-write dependencies on the coprocessor
6463 control registers in cases where INSN1 does not need a general
6464 coprocessor delay. This means that INSN1 is a floating point
6465 comparison instruction. */
6466 /* Itbl support may require additional care here. */
6467 else if (!cop_interlocks
6468 && (pinfo1 & INSN_WRITE_COND_CODE)
6469 && (pinfo2 & INSN_READ_COND_CODE))
6470 return 1;
6471 }
6b76fefe 6472
7361da2c
AB
6473 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6474 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6475 and pause. */
6476 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6477 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6478 || (insn2 && delayed_branch_p (insn2))))
6479 return 1;
6480
71400594
RS
6481 return 0;
6482}
6b76fefe 6483
7d8e00cf
RS
6484/* Return the number of nops that would be needed to work around the
6485 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6486 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6487 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6488
6489static int
932d1a1b 6490nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6491 const struct mips_cl_insn *insn)
6492{
4c260379
RS
6493 int i, j;
6494 unsigned int mask;
7d8e00cf
RS
6495
6496 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6497 are not affected by the errata. */
6498 if (insn != 0
6499 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6500 || strcmp (insn->insn_mo->name, "mtlo") == 0
6501 || strcmp (insn->insn_mo->name, "mthi") == 0))
6502 return 0;
6503
6504 /* Search for the first MFLO or MFHI. */
6505 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6506 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6507 {
6508 /* Extract the destination register. */
4c260379 6509 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6510
6511 /* No nops are needed if INSN reads that register. */
4c260379 6512 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6513 return 0;
6514
6515 /* ...or if any of the intervening instructions do. */
6516 for (j = 0; j < i; j++)
4c260379 6517 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6518 return 0;
6519
932d1a1b
RS
6520 if (i >= ignore)
6521 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6522 }
6523 return 0;
6524}
6525
134c0c8b
MR
6526#define BASE_REG_EQ(INSN1, INSN2) \
6527 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6528 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6529
6530/* Return the minimum alignment for this store instruction. */
6531
6532static int
6533fix_24k_align_to (const struct mips_opcode *mo)
6534{
6535 if (strcmp (mo->name, "sh") == 0)
6536 return 2;
6537
6538 if (strcmp (mo->name, "swc1") == 0
6539 || strcmp (mo->name, "swc2") == 0
6540 || strcmp (mo->name, "sw") == 0
6541 || strcmp (mo->name, "sc") == 0
6542 || strcmp (mo->name, "s.s") == 0)
6543 return 4;
6544
6545 if (strcmp (mo->name, "sdc1") == 0
6546 || strcmp (mo->name, "sdc2") == 0
6547 || strcmp (mo->name, "s.d") == 0)
6548 return 8;
6549
6550 /* sb, swl, swr */
6551 return 1;
6552}
6553
6554struct fix_24k_store_info
6555 {
6556 /* Immediate offset, if any, for this store instruction. */
6557 short off;
6558 /* Alignment required by this store instruction. */
6559 int align_to;
6560 /* True for register offsets. */
6561 int register_offset;
6562 };
6563
6564/* Comparison function used by qsort. */
6565
6566static int
6567fix_24k_sort (const void *a, const void *b)
6568{
6569 const struct fix_24k_store_info *pos1 = a;
6570 const struct fix_24k_store_info *pos2 = b;
6571
6572 return (pos1->off - pos2->off);
6573}
6574
6575/* INSN is a store instruction. Try to record the store information
6576 in STINFO. Return false if the information isn't known. */
6577
6578static bfd_boolean
6579fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6580 const struct mips_cl_insn *insn)
15be625d
CM
6581{
6582 /* The instruction must have a known offset. */
6583 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6584 return FALSE;
6585
6586 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6587 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6588 return TRUE;
6589}
6590
932d1a1b
RS
6591/* Return the number of nops that would be needed to work around the 24k
6592 "lost data on stores during refill" errata if instruction INSN
6593 immediately followed the 2 instructions described by HIST.
6594 Ignore hazards that are contained within the first IGNORE
6595 instructions of HIST.
6596
6597 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6598 for the data cache refills and store data. The following describes
6599 the scenario where the store data could be lost.
6600
6601 * A data cache miss, due to either a load or a store, causing fill
6602 data to be supplied by the memory subsystem
6603 * The first three doublewords of fill data are returned and written
6604 into the cache
6605 * A sequence of four stores occurs in consecutive cycles around the
6606 final doubleword of the fill:
6607 * Store A
6608 * Store B
6609 * Store C
6610 * Zero, One or more instructions
6611 * Store D
6612
6613 The four stores A-D must be to different doublewords of the line that
6614 is being filled. The fourth instruction in the sequence above permits
6615 the fill of the final doubleword to be transferred from the FSB into
6616 the cache. In the sequence above, the stores may be either integer
6617 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6618 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6619 different doublewords on the line. If the floating point unit is
6620 running in 1:2 mode, it is not possible to create the sequence above
6621 using only floating point store instructions.
15be625d
CM
6622
6623 In this case, the cache line being filled is incorrectly marked
6624 invalid, thereby losing the data from any store to the line that
6625 occurs between the original miss and the completion of the five
6626 cycle sequence shown above.
6627
932d1a1b 6628 The workarounds are:
15be625d 6629
932d1a1b
RS
6630 * Run the data cache in write-through mode.
6631 * Insert a non-store instruction between
6632 Store A and Store B or Store B and Store C. */
3739860c 6633
15be625d 6634static int
932d1a1b 6635nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6636 const struct mips_cl_insn *insn)
6637{
6638 struct fix_24k_store_info pos[3];
6639 int align, i, base_offset;
6640
932d1a1b
RS
6641 if (ignore >= 2)
6642 return 0;
6643
ab9794cf
RS
6644 /* If the previous instruction wasn't a store, there's nothing to
6645 worry about. */
15be625d
CM
6646 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6647 return 0;
6648
ab9794cf
RS
6649 /* If the instructions after the previous one are unknown, we have
6650 to assume the worst. */
6651 if (!insn)
15be625d
CM
6652 return 1;
6653
ab9794cf
RS
6654 /* Check whether we are dealing with three consecutive stores. */
6655 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6656 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6657 return 0;
6658
6659 /* If we don't know the relationship between the store addresses,
6660 assume the worst. */
ab9794cf 6661 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6662 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6663 return 1;
6664
6665 if (!fix_24k_record_store_info (&pos[0], insn)
6666 || !fix_24k_record_store_info (&pos[1], &hist[0])
6667 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6668 return 1;
6669
6670 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6671
6672 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6673 X bytes and such that the base register + X is known to be aligned
6674 to align bytes. */
6675
6676 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6677 align = 8;
6678 else
6679 {
6680 align = pos[0].align_to;
6681 base_offset = pos[0].off;
6682 for (i = 1; i < 3; i++)
6683 if (align < pos[i].align_to)
6684 {
6685 align = pos[i].align_to;
6686 base_offset = pos[i].off;
6687 }
6688 for (i = 0; i < 3; i++)
6689 pos[i].off -= base_offset;
6690 }
6691
6692 pos[0].off &= ~align + 1;
6693 pos[1].off &= ~align + 1;
6694 pos[2].off &= ~align + 1;
6695
6696 /* If any two stores write to the same chunk, they also write to the
6697 same doubleword. The offsets are still sorted at this point. */
6698 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6699 return 0;
6700
6701 /* A range of at least 9 bytes is needed for the stores to be in
6702 non-overlapping doublewords. */
6703 if (pos[2].off - pos[0].off <= 8)
6704 return 0;
6705
6706 if (pos[2].off - pos[1].off >= 24
6707 || pos[1].off - pos[0].off >= 24
6708 || pos[2].off - pos[0].off >= 32)
6709 return 0;
6710
6711 return 1;
6712}
6713
71400594 6714/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6715 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6716 where HIST[0] is the most recent instruction. Ignore hazards
6717 between INSN and the first IGNORE instructions in HIST.
6718
6719 If INSN is null, return the worse-case number of nops for any
6720 instruction. */
bdaaa2e1 6721
71400594 6722static int
932d1a1b 6723nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6724 const struct mips_cl_insn *insn)
6725{
6726 int i, nops, tmp_nops;
bdaaa2e1 6727
71400594 6728 nops = 0;
932d1a1b 6729 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6730 {
91d6fa6a 6731 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6732 if (tmp_nops > nops)
6733 nops = tmp_nops;
6734 }
7d8e00cf 6735
df58fc94 6736 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6737 {
932d1a1b 6738 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6739 if (tmp_nops > nops)
6740 nops = tmp_nops;
6741 }
6742
df58fc94 6743 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6744 {
932d1a1b 6745 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6746 if (tmp_nops > nops)
6747 nops = tmp_nops;
6748 }
6749
71400594
RS
6750 return nops;
6751}
252b5132 6752
71400594 6753/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6754 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6755 would be needed after the extended sequence, ignoring hazards
6756 in the first IGNORE instructions. */
252b5132 6757
71400594 6758static int
932d1a1b
RS
6759nops_for_sequence (int num_insns, int ignore,
6760 const struct mips_cl_insn *hist, ...)
71400594
RS
6761{
6762 va_list args;
6763 struct mips_cl_insn buffer[MAX_NOPS];
6764 struct mips_cl_insn *cursor;
6765 int nops;
6766
91d6fa6a 6767 va_start (args, hist);
71400594 6768 cursor = buffer + num_insns;
91d6fa6a 6769 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6770 while (cursor > buffer)
6771 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6772
932d1a1b 6773 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6774 va_end (args);
6775 return nops;
6776}
252b5132 6777
71400594
RS
6778/* Like nops_for_insn, but if INSN is a branch, take into account the
6779 worst-case delay for the branch target. */
252b5132 6780
71400594 6781static int
932d1a1b 6782nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6783 const struct mips_cl_insn *insn)
6784{
6785 int nops, tmp_nops;
60b63b72 6786
932d1a1b 6787 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6788 if (delayed_branch_p (insn))
71400594 6789 {
932d1a1b 6790 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6791 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6792 if (tmp_nops > nops)
6793 nops = tmp_nops;
6794 }
11625dd8 6795 else if (compact_branch_p (insn))
71400594 6796 {
932d1a1b 6797 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6798 if (tmp_nops > nops)
6799 nops = tmp_nops;
6800 }
6801 return nops;
6802}
6803
c67a084a
NC
6804/* Fix NOP issue: Replace nops by "or at,at,zero". */
6805
6806static void
6807fix_loongson2f_nop (struct mips_cl_insn * ip)
6808{
df58fc94 6809 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6810 if (strcmp (ip->insn_mo->name, "nop") == 0)
6811 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6812}
6813
6814/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6815 jr target pc &= 'hffff_ffff_cfff_ffff. */
6816
6817static void
6818fix_loongson2f_jump (struct mips_cl_insn * ip)
6819{
df58fc94 6820 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6821 if (strcmp (ip->insn_mo->name, "j") == 0
6822 || strcmp (ip->insn_mo->name, "jr") == 0
6823 || strcmp (ip->insn_mo->name, "jalr") == 0)
6824 {
6825 int sreg;
6826 expressionS ep;
6827
6828 if (! mips_opts.at)
6829 return;
6830
df58fc94 6831 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6832 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6833 return;
6834
6835 ep.X_op = O_constant;
6836 ep.X_add_number = 0xcfff0000;
6837 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6838 ep.X_add_number = 0xffff;
6839 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6840 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6841 }
6842}
6843
6844static void
6845fix_loongson2f (struct mips_cl_insn * ip)
6846{
6847 if (mips_fix_loongson2f_nop)
6848 fix_loongson2f_nop (ip);
6849
6850 if (mips_fix_loongson2f_jump)
6851 fix_loongson2f_jump (ip);
6852}
6853
a4e06468
RS
6854/* IP is a branch that has a delay slot, and we need to fill it
6855 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6856 with the previous instruction.
6857 ADDRESS_EXPR is an operand of the instruction to be used with
6858 RELOC_TYPE. */
a4e06468
RS
6859
6860static bfd_boolean
e407c74b 6861can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6862 bfd_reloc_code_real_type *reloc_type)
a4e06468 6863{
2b0c8b40 6864 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6865 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6866 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6867
6868 /* -O2 and above is required for this optimization. */
6869 if (mips_optimize < 2)
6870 return FALSE;
6871
6872 /* If we have seen .set volatile or .set nomove, don't optimize. */
6873 if (mips_opts.nomove)
6874 return FALSE;
6875
6876 /* We can't swap if the previous instruction's position is fixed. */
6877 if (history[0].fixed_p)
6878 return FALSE;
6879
6880 /* If the previous previous insn was in a .set noreorder, we can't
6881 swap. Actually, the MIPS assembler will swap in this situation.
6882 However, gcc configured -with-gnu-as will generate code like
6883
6884 .set noreorder
6885 lw $4,XXX
6886 .set reorder
6887 INSN
6888 bne $4,$0,foo
6889
6890 in which we can not swap the bne and INSN. If gcc is not configured
6891 -with-gnu-as, it does not output the .set pseudo-ops. */
6892 if (history[1].noreorder_p)
6893 return FALSE;
6894
87333bb7
MR
6895 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6896 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6897 if (mips_opts.mips16 && history[0].fixp[0])
6898 return FALSE;
6899
6900 /* If the branch is itself the target of a branch, we can not swap.
6901 We cheat on this; all we check for is whether there is a label on
6902 this instruction. If there are any branches to anything other than
6903 a label, users must use .set noreorder. */
6904 if (seg_info (now_seg)->label_list)
6905 return FALSE;
6906
6907 /* If the previous instruction is in a variant frag other than this
2309ddf2 6908 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6909 MIPS16 code, which uses variant frags for different purposes. */
6910 if (!mips_opts.mips16
a4e06468
RS
6911 && history[0].frag
6912 && history[0].frag->fr_type == rs_machine_dependent)
6913 return FALSE;
6914
bcd530a7
RS
6915 /* We do not swap with instructions that cannot architecturally
6916 be placed in a branch delay slot, such as SYNC or ERET. We
6917 also refrain from swapping with a trap instruction, since it
6918 complicates trap handlers to have the trap instruction be in
6919 a delay slot. */
a4e06468 6920 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6921 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6922 return FALSE;
6923
6924 /* Check for conflicts between the branch and the instructions
6925 before the candidate delay slot. */
6926 if (nops_for_insn (0, history + 1, ip) > 0)
6927 return FALSE;
6928
6929 /* Check for conflicts between the swapped sequence and the
6930 target of the branch. */
6931 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6932 return FALSE;
6933
6934 /* If the branch reads a register that the previous
6935 instruction sets, we can not swap. */
6936 gpr_read = gpr_read_mask (ip);
6937 prev_gpr_write = gpr_write_mask (&history[0]);
6938 if (gpr_read & prev_gpr_write)
6939 return FALSE;
6940
9d5de888
CF
6941 fpr_read = fpr_read_mask (ip);
6942 prev_fpr_write = fpr_write_mask (&history[0]);
6943 if (fpr_read & prev_fpr_write)
6944 return FALSE;
6945
a4e06468
RS
6946 /* If the branch writes a register that the previous
6947 instruction sets, we can not swap. */
6948 gpr_write = gpr_write_mask (ip);
6949 if (gpr_write & prev_gpr_write)
6950 return FALSE;
6951
6952 /* If the branch writes a register that the previous
6953 instruction reads, we can not swap. */
6954 prev_gpr_read = gpr_read_mask (&history[0]);
6955 if (gpr_write & prev_gpr_read)
6956 return FALSE;
6957
6958 /* If one instruction sets a condition code and the
6959 other one uses a condition code, we can not swap. */
6960 pinfo = ip->insn_mo->pinfo;
6961 if ((pinfo & INSN_READ_COND_CODE)
6962 && (prev_pinfo & INSN_WRITE_COND_CODE))
6963 return FALSE;
6964 if ((pinfo & INSN_WRITE_COND_CODE)
6965 && (prev_pinfo & INSN_READ_COND_CODE))
6966 return FALSE;
6967
6968 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6969 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6970 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6971 return FALSE;
a4e06468 6972
df58fc94
RS
6973 /* If the previous instruction has an incorrect size for a fixed
6974 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6975 pinfo2 = ip->insn_mo->pinfo2;
6976 if (mips_opts.micromips
6977 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6978 && insn_length (history) != 2)
6979 return FALSE;
6980 if (mips_opts.micromips
6981 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6982 && insn_length (history) != 4)
6983 return FALSE;
6984
e407c74b
NC
6985 /* On R5900 short loops need to be fixed by inserting a nop in
6986 the branch delay slots.
6987 A short loop can be terminated too early. */
6988 if (mips_opts.arch == CPU_R5900
6989 /* Check if instruction has a parameter, ignore "j $31". */
6990 && (address_expr != NULL)
6991 /* Parameter must be 16 bit. */
6992 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6993 /* Branch to same segment. */
41065f5e 6994 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6995 /* Branch to same code fragment. */
41065f5e 6996 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6997 /* Can only calculate branch offset if value is known. */
41065f5e 6998 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6999 /* Check if branch is really conditional. */
7000 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
7001 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
7002 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
7003 {
7004 int distance;
7005 /* Check if loop is shorter than 6 instructions including
7006 branch and delay slot. */
41065f5e 7007 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
7008 if (distance <= 20)
7009 {
7010 int i;
7011 int rv;
7012
7013 rv = FALSE;
7014 /* When the loop includes branches or jumps,
7015 it is not a short loop. */
7016 for (i = 0; i < (distance / 4); i++)
7017 {
7018 if ((history[i].cleared_p)
41065f5e 7019 || delayed_branch_p (&history[i]))
e407c74b
NC
7020 {
7021 rv = TRUE;
7022 break;
7023 }
7024 }
535b785f 7025 if (!rv)
e407c74b
NC
7026 {
7027 /* Insert nop after branch to fix short loop. */
7028 return FALSE;
7029 }
7030 }
7031 }
7032
a4e06468
RS
7033 return TRUE;
7034}
7035
e407c74b
NC
7036/* Decide how we should add IP to the instruction stream.
7037 ADDRESS_EXPR is an operand of the instruction to be used with
7038 RELOC_TYPE. */
a4e06468
RS
7039
7040static enum append_method
e407c74b 7041get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7042 bfd_reloc_code_real_type *reloc_type)
a4e06468 7043{
a4e06468
RS
7044 /* The relaxed version of a macro sequence must be inherently
7045 hazard-free. */
7046 if (mips_relax.sequence == 2)
7047 return APPEND_ADD;
7048
3b821a28 7049 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7050 if (mips_opts.noreorder)
7051 return APPEND_ADD;
7052
7053 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7054 if (delayed_branch_p (ip))
a4e06468 7055 {
e407c74b
NC
7056 if (!branch_likely_p (ip)
7057 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7058 return APPEND_SWAP;
7059
7060 if (mips_opts.mips16
7061 && ISA_SUPPORTS_MIPS16E
fc76e730 7062 && gpr_read_mask (ip) != 0)
a4e06468
RS
7063 return APPEND_ADD_COMPACT;
7064
7bd374a4
MR
7065 if (mips_opts.micromips
7066 && ((ip->insn_opcode & 0xffe0) == 0x4580
7067 || (!forced_insn_length
7068 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7069 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7070 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7071 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7072 return APPEND_ADD_COMPACT;
7073
a4e06468
RS
7074 return APPEND_ADD_WITH_NOP;
7075 }
7076
a4e06468
RS
7077 return APPEND_ADD;
7078}
7079
7bd374a4
MR
7080/* IP is an instruction whose opcode we have just changed, END points
7081 to the end of the opcode table processed. Point IP->insn_mo to the
7082 new opcode's definition. */
ceb94aa5
RS
7083
7084static void
7bd374a4 7085find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7086{
7bd374a4 7087 const struct mips_opcode *mo;
ceb94aa5 7088
ceb94aa5 7089 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7090 if (mo->pinfo != INSN_MACRO
7091 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7092 {
7093 ip->insn_mo = mo;
7094 return;
7095 }
7096 abort ();
7097}
7098
7bd374a4
MR
7099/* IP is a MIPS16 instruction whose opcode we have just changed.
7100 Point IP->insn_mo to the new opcode's definition. */
7101
7102static void
7103find_altered_mips16_opcode (struct mips_cl_insn *ip)
7104{
7105 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7106}
7107
7108/* IP is a microMIPS instruction whose opcode we have just changed.
7109 Point IP->insn_mo to the new opcode's definition. */
7110
7111static void
7112find_altered_micromips_opcode (struct mips_cl_insn *ip)
7113{
7114 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7115}
7116
df58fc94
RS
7117/* For microMIPS macros, we need to generate a local number label
7118 as the target of branches. */
7119#define MICROMIPS_LABEL_CHAR '\037'
7120static unsigned long micromips_target_label;
7121static char micromips_target_name[32];
7122
7123static char *
7124micromips_label_name (void)
7125{
7126 char *p = micromips_target_name;
7127 char symbol_name_temporary[24];
7128 unsigned long l;
7129 int i;
7130
7131 if (*p)
7132 return p;
7133
7134 i = 0;
7135 l = micromips_target_label;
7136#ifdef LOCAL_LABEL_PREFIX
7137 *p++ = LOCAL_LABEL_PREFIX;
7138#endif
7139 *p++ = 'L';
7140 *p++ = MICROMIPS_LABEL_CHAR;
7141 do
7142 {
7143 symbol_name_temporary[i++] = l % 10 + '0';
7144 l /= 10;
7145 }
7146 while (l != 0);
7147 while (i > 0)
7148 *p++ = symbol_name_temporary[--i];
7149 *p = '\0';
7150
7151 return micromips_target_name;
7152}
7153
7154static void
7155micromips_label_expr (expressionS *label_expr)
7156{
7157 label_expr->X_op = O_symbol;
7158 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7159 label_expr->X_add_number = 0;
7160}
7161
7162static void
7163micromips_label_inc (void)
7164{
7165 micromips_target_label++;
7166 *micromips_target_name = '\0';
7167}
7168
7169static void
7170micromips_add_label (void)
7171{
7172 symbolS *s;
7173
7174 s = colon (micromips_label_name ());
7175 micromips_label_inc ();
f3ded42a 7176 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7177}
7178
7179/* If assembling microMIPS code, then return the microMIPS reloc
7180 corresponding to the requested one if any. Otherwise return
7181 the reloc unchanged. */
7182
7183static bfd_reloc_code_real_type
7184micromips_map_reloc (bfd_reloc_code_real_type reloc)
7185{
7186 static const bfd_reloc_code_real_type relocs[][2] =
7187 {
7188 /* Keep sorted incrementally by the left-hand key. */
7189 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7190 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7191 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7192 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7193 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7194 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7195 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7196 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7197 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7198 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7199 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7200 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7201 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7202 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7203 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7204 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7205 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7206 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7207 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7208 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7209 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7210 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7211 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7212 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7213 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7214 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7215 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7216 };
7217 bfd_reloc_code_real_type r;
7218 size_t i;
7219
7220 if (!mips_opts.micromips)
7221 return reloc;
7222 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7223 {
7224 r = relocs[i][0];
7225 if (r > reloc)
7226 return reloc;
7227 if (r == reloc)
7228 return relocs[i][1];
7229 }
7230 return reloc;
7231}
7232
b886a2ab
RS
7233/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7234 Return true on success, storing the resolved value in RESULT. */
7235
7236static bfd_boolean
7237calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7238 offsetT *result)
7239{
7240 switch (reloc)
7241 {
7242 case BFD_RELOC_MIPS_HIGHEST:
7243 case BFD_RELOC_MICROMIPS_HIGHEST:
7244 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7245 return TRUE;
7246
7247 case BFD_RELOC_MIPS_HIGHER:
7248 case BFD_RELOC_MICROMIPS_HIGHER:
7249 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7250 return TRUE;
7251
7252 case BFD_RELOC_HI16_S:
41947d9e 7253 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7254 case BFD_RELOC_MICROMIPS_HI16_S:
7255 case BFD_RELOC_MIPS16_HI16_S:
7256 *result = ((operand + 0x8000) >> 16) & 0xffff;
7257 return TRUE;
7258
7259 case BFD_RELOC_HI16:
7260 case BFD_RELOC_MICROMIPS_HI16:
7261 case BFD_RELOC_MIPS16_HI16:
7262 *result = (operand >> 16) & 0xffff;
7263 return TRUE;
7264
7265 case BFD_RELOC_LO16:
41947d9e 7266 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7267 case BFD_RELOC_MICROMIPS_LO16:
7268 case BFD_RELOC_MIPS16_LO16:
7269 *result = operand & 0xffff;
7270 return TRUE;
7271
7272 case BFD_RELOC_UNUSED:
7273 *result = operand;
7274 return TRUE;
7275
7276 default:
7277 return FALSE;
7278 }
7279}
7280
71400594
RS
7281/* Output an instruction. IP is the instruction information.
7282 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7283 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7284 a macro expansion. */
71400594
RS
7285
7286static void
7287append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7288 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7289{
14fe068b 7290 unsigned long prev_pinfo2, pinfo;
71400594 7291 bfd_boolean relaxed_branch = FALSE;
a4e06468 7292 enum append_method method;
2309ddf2 7293 bfd_boolean relax32;
2b0c8b40 7294 int branch_disp;
71400594 7295
2309ddf2 7296 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7297 fix_loongson2f (ip);
7298
738f4d98 7299 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7300 file_ase_micromips |= mips_opts.micromips;
738f4d98 7301
df58fc94 7302 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7303 pinfo = ip->insn_mo->pinfo;
df58fc94 7304
7bd374a4
MR
7305 /* Don't raise alarm about `nods' frags as they'll fill in the right
7306 kind of nop in relaxation if required. */
df58fc94
RS
7307 if (mips_opts.micromips
7308 && !expansionp
7bd374a4
MR
7309 && !(history[0].frag
7310 && history[0].frag->fr_type == rs_machine_dependent
7311 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7312 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7313 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7314 && micromips_insn_length (ip->insn_mo) != 2)
7315 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7316 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7317 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7318 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7319
15be625d
CM
7320 if (address_expr == NULL)
7321 ip->complete_p = 1;
b886a2ab
RS
7322 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7323 && reloc_type[1] == BFD_RELOC_UNUSED
7324 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7325 && address_expr->X_op == O_constant)
7326 {
15be625d
CM
7327 switch (*reloc_type)
7328 {
15be625d 7329 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7330 {
7331 int shift;
7332
17c6c9d9
MR
7333 /* Shift is 2, unusually, for microMIPS JALX. */
7334 shift = (mips_opts.micromips
7335 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7336 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7337 as_bad (_("jump to misaligned address (0x%lx)"),
7338 (unsigned long) address_expr->X_add_number);
7339 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7340 & 0x3ffffff);
335574df 7341 ip->complete_p = 1;
df58fc94 7342 }
15be625d
CM
7343 break;
7344
7345 case BFD_RELOC_MIPS16_JMP:
7346 if ((address_expr->X_add_number & 3) != 0)
7347 as_bad (_("jump to misaligned address (0x%lx)"),
7348 (unsigned long) address_expr->X_add_number);
7349 ip->insn_opcode |=
7350 (((address_expr->X_add_number & 0x7c0000) << 3)
7351 | ((address_expr->X_add_number & 0xf800000) >> 7)
7352 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7353 ip->complete_p = 1;
15be625d
CM
7354 break;
7355
7356 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7357 {
7358 int shift;
7359
7360 shift = mips_opts.micromips ? 1 : 2;
7361 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7362 as_bad (_("branch to misaligned address (0x%lx)"),
7363 (unsigned long) address_expr->X_add_number);
7364 if (!mips_relax_branch)
7365 {
7366 if ((address_expr->X_add_number + (1 << (shift + 15)))
7367 & ~((1 << (shift + 16)) - 1))
7368 as_bad (_("branch address range overflow (0x%lx)"),
7369 (unsigned long) address_expr->X_add_number);
7370 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7371 & 0xffff);
7372 }
df58fc94 7373 }
15be625d
CM
7374 break;
7375
7361da2c
AB
7376 case BFD_RELOC_MIPS_21_PCREL_S2:
7377 {
7378 int shift;
7379
7380 shift = 2;
7381 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7382 as_bad (_("branch to misaligned address (0x%lx)"),
7383 (unsigned long) address_expr->X_add_number);
7384 if ((address_expr->X_add_number + (1 << (shift + 20)))
7385 & ~((1 << (shift + 21)) - 1))
7386 as_bad (_("branch address range overflow (0x%lx)"),
7387 (unsigned long) address_expr->X_add_number);
7388 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7389 & 0x1fffff);
7390 }
7391 break;
7392
7393 case BFD_RELOC_MIPS_26_PCREL_S2:
7394 {
7395 int shift;
7396
7397 shift = 2;
7398 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7399 as_bad (_("branch to misaligned address (0x%lx)"),
7400 (unsigned long) address_expr->X_add_number);
7401 if ((address_expr->X_add_number + (1 << (shift + 25)))
7402 & ~((1 << (shift + 26)) - 1))
7403 as_bad (_("branch address range overflow (0x%lx)"),
7404 (unsigned long) address_expr->X_add_number);
7405 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7406 & 0x3ffffff);
7407 }
7408 break;
7409
15be625d 7410 default:
b886a2ab
RS
7411 {
7412 offsetT value;
7413
7414 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7415 &value))
7416 {
7417 ip->insn_opcode |= value & 0xffff;
7418 ip->complete_p = 1;
7419 }
7420 }
7421 break;
7422 }
15be625d
CM
7423 }
7424
71400594
RS
7425 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7426 {
7427 /* There are a lot of optimizations we could do that we don't.
7428 In particular, we do not, in general, reorder instructions.
7429 If you use gcc with optimization, it will reorder
7430 instructions and generally do much more optimization then we
7431 do here; repeating all that work in the assembler would only
7432 benefit hand written assembly code, and does not seem worth
7433 it. */
7434 int nops = (mips_optimize == 0
932d1a1b
RS
7435 ? nops_for_insn (0, history, NULL)
7436 : nops_for_insn_or_target (0, history, ip));
71400594 7437 if (nops > 0)
252b5132
RH
7438 {
7439 fragS *old_frag;
7440 unsigned long old_frag_offset;
7441 int i;
252b5132
RH
7442
7443 old_frag = frag_now;
7444 old_frag_offset = frag_now_fix ();
7445
7446 for (i = 0; i < nops; i++)
14fe068b
RS
7447 add_fixed_insn (NOP_INSN);
7448 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7449
7450 if (listing)
7451 {
7452 listing_prev_line ();
7453 /* We may be at the start of a variant frag. In case we
7454 are, make sure there is enough space for the frag
7455 after the frags created by listing_prev_line. The
7456 argument to frag_grow here must be at least as large
7457 as the argument to all other calls to frag_grow in
7458 this file. We don't have to worry about being in the
7459 middle of a variant frag, because the variants insert
7460 all needed nop instructions themselves. */
7461 frag_grow (40);
7462 }
7463
462427c4 7464 mips_move_text_labels ();
252b5132
RH
7465
7466#ifndef NO_ECOFF_DEBUGGING
7467 if (ECOFF_DEBUGGING)
7468 ecoff_fix_loc (old_frag, old_frag_offset);
7469#endif
7470 }
71400594
RS
7471 }
7472 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7473 {
932d1a1b
RS
7474 int nops;
7475
7476 /* Work out how many nops in prev_nop_frag are needed by IP,
7477 ignoring hazards generated by the first prev_nop_frag_since
7478 instructions. */
7479 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7480 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7481
71400594
RS
7482 /* Enforce NOPS as a minimum. */
7483 if (nops > prev_nop_frag_required)
7484 prev_nop_frag_required = nops;
252b5132 7485
71400594
RS
7486 if (prev_nop_frag_holds == prev_nop_frag_required)
7487 {
7488 /* Settle for the current number of nops. Update the history
7489 accordingly (for the benefit of any future .set reorder code). */
7490 prev_nop_frag = NULL;
7491 insert_into_history (prev_nop_frag_since,
7492 prev_nop_frag_holds, NOP_INSN);
7493 }
7494 else
7495 {
7496 /* Allow this instruction to replace one of the nops that was
7497 tentatively added to prev_nop_frag. */
df58fc94 7498 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7499 prev_nop_frag_holds--;
7500 prev_nop_frag_since++;
252b5132
RH
7501 }
7502 }
7503
e407c74b 7504 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7505 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7506
e410add4
RS
7507 dwarf2_emit_insn (0);
7508 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7509 so "move" the instruction address accordingly.
7510
7511 Also, it doesn't seem appropriate for the assembler to reorder .loc
7512 entries. If this instruction is a branch that we are going to swap
7513 with the previous instruction, the two instructions should be
7514 treated as a unit, and the debug information for both instructions
7515 should refer to the start of the branch sequence. Using the
7516 current position is certainly wrong when swapping a 32-bit branch
7517 and a 16-bit delay slot, since the current position would then be
7518 in the middle of a branch. */
7519 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7520
df58fc94
RS
7521 relax32 = (mips_relax_branch
7522 /* Don't try branch relaxation within .set nomacro, or within
7523 .set noat if we use $at for PIC computations. If it turns
7524 out that the branch was out-of-range, we'll get an error. */
7525 && !mips_opts.warn_about_macros
7526 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7527 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7528 as they have no complementing branches. */
7529 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7530
7531 if (!HAVE_CODE_COMPRESSION
7532 && address_expr
7533 && relax32
0b25d3e6 7534 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7535 && delayed_branch_p (ip))
4a6a3df4 7536 {
895921c9 7537 relaxed_branch = TRUE;
1e915849
RS
7538 add_relaxed_insn (ip, (relaxed_branch_length
7539 (NULL, NULL,
11625dd8
RS
7540 uncond_branch_p (ip) ? -1
7541 : branch_likely_p (ip) ? 1
1e915849
RS
7542 : 0)), 4,
7543 RELAX_BRANCH_ENCODE
ce8ad872 7544 (AT, mips_pic != NO_PIC,
11625dd8
RS
7545 uncond_branch_p (ip),
7546 branch_likely_p (ip),
1e915849
RS
7547 pinfo & INSN_WRITE_GPR_31,
7548 0),
7549 address_expr->X_add_symbol,
7550 address_expr->X_add_number);
4a6a3df4
AO
7551 *reloc_type = BFD_RELOC_UNUSED;
7552 }
df58fc94
RS
7553 else if (mips_opts.micromips
7554 && address_expr
7555 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7556 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7557 && (delayed_branch_p (ip) || compact_branch_p (ip))
7558 /* Don't try branch relaxation when users specify
7559 16-bit/32-bit instructions. */
7560 && !forced_insn_length)
df58fc94 7561 {
7bd374a4
MR
7562 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7563 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7564 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7565 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7566 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7567 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7568 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7569 int length32 = nods ? 8 : 4;
df58fc94
RS
7570
7571 gas_assert (address_expr != NULL);
7572 gas_assert (!mips_relax.sequence);
7573
2b0c8b40 7574 relaxed_branch = TRUE;
7bd374a4
MR
7575 if (nods)
7576 method = APPEND_ADD;
7577 if (relax32)
7578 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7579 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7580 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7581 mips_pic != NO_PIC,
7bd374a4 7582 uncond, compact, al, nods,
40209cad 7583 relax32, 0, 0),
df58fc94
RS
7584 address_expr->X_add_symbol,
7585 address_expr->X_add_number);
7586 *reloc_type = BFD_RELOC_UNUSED;
7587 }
7588 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7589 {
7fd53920
MR
7590 bfd_boolean require_unextended;
7591 bfd_boolean require_extended;
88a7ef16
MR
7592 symbolS *symbol;
7593 offsetT offset;
7594
7fd53920
MR
7595 if (forced_insn_length != 0)
7596 {
7597 require_unextended = forced_insn_length == 2;
7598 require_extended = forced_insn_length == 4;
7599 }
7600 else
7601 {
7602 require_unextended = (mips_opts.noautoextend
7603 && !mips_opcode_32bit_p (ip->insn_mo));
7604 require_extended = 0;
7605 }
7606
252b5132 7607 /* We need to set up a variant frag. */
df58fc94 7608 gas_assert (address_expr != NULL);
88a7ef16
MR
7609 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7610 symbol created by `make_expr_symbol' may not get a necessary
7611 external relocation produced. */
7612 if (address_expr->X_op == O_symbol)
7613 {
7614 symbol = address_expr->X_add_symbol;
7615 offset = address_expr->X_add_number;
7616 }
7617 else
7618 {
7619 symbol = make_expr_symbol (address_expr);
82d808ed 7620 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7621 offset = 0;
7622 }
8507b6e7 7623 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7624 RELAX_MIPS16_ENCODE
7625 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7626 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7627 mips_pic != NO_PIC,
7628 HAVE_32BIT_SYMBOLS,
7629 mips_opts.warn_about_macros,
7fd53920 7630 require_unextended, require_extended,
11625dd8 7631 delayed_branch_p (&history[0]),
1e915849 7632 history[0].mips16_absolute_jump_p),
88a7ef16 7633 symbol, offset);
252b5132 7634 }
5c04167a 7635 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7636 {
11625dd8 7637 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7638 /* Make sure there is enough room to swap this instruction with
7639 a following jump instruction. */
7640 frag_grow (6);
1e915849 7641 add_fixed_insn (ip);
252b5132
RH
7642 }
7643 else
7644 {
7645 if (mips_opts.mips16
7646 && mips_opts.noreorder
11625dd8 7647 && delayed_branch_p (&history[0]))
252b5132
RH
7648 as_warn (_("extended instruction in delay slot"));
7649
4d7206a2
RS
7650 if (mips_relax.sequence)
7651 {
7652 /* If we've reached the end of this frag, turn it into a variant
7653 frag and record the information for the instructions we've
7654 written so far. */
7655 if (frag_room () < 4)
7656 relax_close_frag ();
df58fc94 7657 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7658 }
7659
584892a6 7660 if (mips_relax.sequence != 2)
df58fc94
RS
7661 {
7662 if (mips_macro_warning.first_insn_sizes[0] == 0)
7663 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7664 mips_macro_warning.sizes[0] += insn_length (ip);
7665 mips_macro_warning.insns[0]++;
7666 }
584892a6 7667 if (mips_relax.sequence != 1)
df58fc94
RS
7668 {
7669 if (mips_macro_warning.first_insn_sizes[1] == 0)
7670 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7671 mips_macro_warning.sizes[1] += insn_length (ip);
7672 mips_macro_warning.insns[1]++;
7673 }
584892a6 7674
1e915849
RS
7675 if (mips_opts.mips16)
7676 {
7677 ip->fixed_p = 1;
7678 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7679 }
7680 add_fixed_insn (ip);
252b5132
RH
7681 }
7682
9fe77896 7683 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7684 {
df58fc94 7685 bfd_reloc_code_real_type final_type[3];
2309ddf2 7686 reloc_howto_type *howto0;
9fe77896
RS
7687 reloc_howto_type *howto;
7688 int i;
34ce925e 7689
df58fc94
RS
7690 /* Perform any necessary conversion to microMIPS relocations
7691 and find out how many relocations there actually are. */
7692 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7693 final_type[i] = micromips_map_reloc (reloc_type[i]);
7694
9fe77896
RS
7695 /* In a compound relocation, it is the final (outermost)
7696 operator that determines the relocated field. */
2309ddf2 7697 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7698 if (!howto)
7699 abort ();
2309ddf2
MR
7700
7701 if (i > 1)
7702 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7703 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7704 bfd_get_reloc_size (howto),
7705 address_expr,
2309ddf2
MR
7706 howto0 && howto0->pc_relative,
7707 final_type[0]);
ce8ad872
MR
7708 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7709 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7710
7711 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7712 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7713 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7714
7715 /* These relocations can have an addend that won't fit in
7716 4 octets for 64bit assembly. */
bad1aba3 7717 if (GPR_SIZE == 64
9fe77896
RS
7718 && ! howto->partial_inplace
7719 && (reloc_type[0] == BFD_RELOC_16
7720 || reloc_type[0] == BFD_RELOC_32
7721 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7722 || reloc_type[0] == BFD_RELOC_GPREL16
7723 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7724 || reloc_type[0] == BFD_RELOC_GPREL32
7725 || reloc_type[0] == BFD_RELOC_64
7726 || reloc_type[0] == BFD_RELOC_CTOR
7727 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7728 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7729 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7730 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7731 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7732 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7733 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7734 || hi16_reloc_p (reloc_type[0])
7735 || lo16_reloc_p (reloc_type[0])))
7736 ip->fixp[0]->fx_no_overflow = 1;
7737
ddaf2c41
MR
7738 /* These relocations can have an addend that won't fit in 2 octets. */
7739 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7740 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7741 ip->fixp[0]->fx_no_overflow = 1;
7742
9fe77896
RS
7743 if (mips_relax.sequence)
7744 {
7745 if (mips_relax.first_fixup == 0)
7746 mips_relax.first_fixup = ip->fixp[0];
7747 }
7748 else if (reloc_needs_lo_p (*reloc_type))
7749 {
7750 struct mips_hi_fixup *hi_fixup;
7751
7752 /* Reuse the last entry if it already has a matching %lo. */
7753 hi_fixup = mips_hi_fixup_list;
7754 if (hi_fixup == 0
7755 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7756 {
325801bd 7757 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7758 hi_fixup->next = mips_hi_fixup_list;
7759 mips_hi_fixup_list = hi_fixup;
4d7206a2 7760 }
9fe77896
RS
7761 hi_fixup->fixp = ip->fixp[0];
7762 hi_fixup->seg = now_seg;
7763 }
252b5132 7764
9fe77896
RS
7765 /* Add fixups for the second and third relocations, if given.
7766 Note that the ABI allows the second relocation to be
7767 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7768 moment we only use RSS_UNDEF, but we could add support
7769 for the others if it ever becomes necessary. */
7770 for (i = 1; i < 3; i++)
7771 if (reloc_type[i] != BFD_RELOC_UNUSED)
7772 {
7773 ip->fixp[i] = fix_new (ip->frag, ip->where,
7774 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7775 FALSE, final_type[i]);
f6688943 7776
9fe77896
RS
7777 /* Use fx_tcbit to mark compound relocs. */
7778 ip->fixp[0]->fx_tcbit = 1;
7779 ip->fixp[i]->fx_tcbit = 1;
7780 }
252b5132 7781 }
252b5132
RH
7782
7783 /* Update the register mask information. */
4c260379
RS
7784 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7785 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7786
a4e06468 7787 switch (method)
252b5132 7788 {
a4e06468
RS
7789 case APPEND_ADD:
7790 insert_into_history (0, 1, ip);
7791 break;
7792
7793 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7794 {
7795 struct mips_cl_insn *nop;
7796
7797 insert_into_history (0, 1, ip);
7798 nop = get_delay_slot_nop (ip);
7799 add_fixed_insn (nop);
7800 insert_into_history (0, 1, nop);
7801 if (mips_relax.sequence)
7802 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7803 }
a4e06468
RS
7804 break;
7805
7806 case APPEND_ADD_COMPACT:
7807 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7808 if (mips_opts.mips16)
7809 {
7810 ip->insn_opcode |= 0x0080;
7811 find_altered_mips16_opcode (ip);
7812 }
7813 /* Convert microMIPS instructions. */
7814 else if (mips_opts.micromips)
7815 {
7816 /* jr16->jrc */
7817 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7818 ip->insn_opcode |= 0x0020;
7819 /* b16->bc */
7820 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7821 ip->insn_opcode = 0x40e00000;
7822 /* beqz16->beqzc, bnez16->bnezc */
7823 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7824 {
7825 unsigned long regno;
7826
7827 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7828 regno &= MICROMIPSOP_MASK_MD;
7829 regno = micromips_to_32_reg_d_map[regno];
7830 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7831 | (regno << MICROMIPSOP_SH_RS)
7832 | 0x40a00000) ^ 0x00400000;
7833 }
7834 /* beqz->beqzc, bnez->bnezc */
7835 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7836 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7837 | ((ip->insn_opcode >> 7) & 0x00400000)
7838 | 0x40a00000) ^ 0x00400000;
7839 /* beq $0->beqzc, bne $0->bnezc */
7840 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7841 ip->insn_opcode = (((ip->insn_opcode >>
7842 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7843 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7844 | ((ip->insn_opcode >> 7) & 0x00400000)
7845 | 0x40a00000) ^ 0x00400000;
7846 else
7847 abort ();
7848 find_altered_micromips_opcode (ip);
7849 }
7850 else
7851 abort ();
a4e06468
RS
7852 install_insn (ip);
7853 insert_into_history (0, 1, ip);
7854 break;
7855
7856 case APPEND_SWAP:
7857 {
7858 struct mips_cl_insn delay = history[0];
99e7978b
MF
7859
7860 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7861 {
7862 /* Add the delay slot instruction to the end of the
7863 current frag and shrink the fixed part of the
7864 original frag. If the branch occupies the tail of
7865 the latter, move it backwards to cover the gap. */
2b0c8b40 7866 delay.frag->fr_fix -= branch_disp;
a4e06468 7867 if (delay.frag == ip->frag)
2b0c8b40 7868 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7869 add_fixed_insn (&delay);
7870 }
7871 else
7872 {
5e35670b
MR
7873 /* If this is not a relaxed branch and we are in the
7874 same frag, then just swap the instructions. */
7875 move_insn (ip, delay.frag, delay.where);
7876 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7877 }
7878 history[0] = *ip;
7879 delay.fixed_p = 1;
7880 insert_into_history (0, 1, &delay);
7881 }
7882 break;
252b5132
RH
7883 }
7884
13408f1e 7885 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7886 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7887 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7888 {
7889 unsigned int i;
7890
79850f26 7891 mips_no_prev_insn ();
13408f1e 7892
e407c74b 7893 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7894 history[i].cleared_p = 1;
e407c74b
NC
7895 }
7896
df58fc94
RS
7897 /* We need to emit a label at the end of branch-likely macros. */
7898 if (emit_branch_likely_macro)
7899 {
7900 emit_branch_likely_macro = FALSE;
7901 micromips_add_label ();
7902 }
7903
252b5132
RH
7904 /* We just output an insn, so the next one doesn't have a label. */
7905 mips_clear_insn_labels ();
252b5132
RH
7906}
7907
e407c74b
NC
7908/* Forget that there was any previous instruction or label.
7909 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7910
7911static void
7d10b47d 7912mips_no_prev_insn (void)
252b5132 7913{
7d10b47d
RS
7914 prev_nop_frag = NULL;
7915 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7916 mips_clear_insn_labels ();
7917}
7918
7d10b47d
RS
7919/* This function must be called before we emit something other than
7920 instructions. It is like mips_no_prev_insn except that it inserts
7921 any NOPS that might be needed by previous instructions. */
252b5132 7922
7d10b47d
RS
7923void
7924mips_emit_delays (void)
252b5132
RH
7925{
7926 if (! mips_opts.noreorder)
7927 {
932d1a1b 7928 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7929 if (nops > 0)
7930 {
7d10b47d
RS
7931 while (nops-- > 0)
7932 add_fixed_insn (NOP_INSN);
462427c4 7933 mips_move_text_labels ();
7d10b47d
RS
7934 }
7935 }
7936 mips_no_prev_insn ();
7937}
7938
7939/* Start a (possibly nested) noreorder block. */
7940
7941static void
7942start_noreorder (void)
7943{
7944 if (mips_opts.noreorder == 0)
7945 {
7946 unsigned int i;
7947 int nops;
7948
7949 /* None of the instructions before the .set noreorder can be moved. */
7950 for (i = 0; i < ARRAY_SIZE (history); i++)
7951 history[i].fixed_p = 1;
7952
7953 /* Insert any nops that might be needed between the .set noreorder
7954 block and the previous instructions. We will later remove any
7955 nops that turn out not to be needed. */
932d1a1b 7956 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7957 if (nops > 0)
7958 {
7959 if (mips_optimize != 0)
252b5132
RH
7960 {
7961 /* Record the frag which holds the nop instructions, so
7962 that we can remove them if we don't need them. */
df58fc94 7963 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7964 prev_nop_frag = frag_now;
7965 prev_nop_frag_holds = nops;
7966 prev_nop_frag_required = 0;
7967 prev_nop_frag_since = 0;
7968 }
7969
7970 for (; nops > 0; --nops)
1e915849 7971 add_fixed_insn (NOP_INSN);
252b5132 7972
7d10b47d
RS
7973 /* Move on to a new frag, so that it is safe to simply
7974 decrease the size of prev_nop_frag. */
7975 frag_wane (frag_now);
7976 frag_new (0);
462427c4 7977 mips_move_text_labels ();
252b5132 7978 }
df58fc94 7979 mips_mark_labels ();
7d10b47d 7980 mips_clear_insn_labels ();
252b5132 7981 }
7d10b47d
RS
7982 mips_opts.noreorder++;
7983 mips_any_noreorder = 1;
7984}
252b5132 7985
7d10b47d 7986/* End a nested noreorder block. */
252b5132 7987
7d10b47d
RS
7988static void
7989end_noreorder (void)
7990{
7991 mips_opts.noreorder--;
7992 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7993 {
7994 /* Commit to inserting prev_nop_frag_required nops and go back to
7995 handling nop insertion the .set reorder way. */
7996 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7997 * NOP_INSN_SIZE);
7d10b47d
RS
7998 insert_into_history (prev_nop_frag_since,
7999 prev_nop_frag_required, NOP_INSN);
8000 prev_nop_frag = NULL;
8001 }
252b5132
RH
8002}
8003
97d87491
RS
8004/* Sign-extend 32-bit mode constants that have bit 31 set and all
8005 higher bits unset. */
8006
8007static void
8008normalize_constant_expr (expressionS *ex)
8009{
8010 if (ex->X_op == O_constant
8011 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8012 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8013 - 0x80000000);
8014}
8015
8016/* Sign-extend 32-bit mode address offsets that have bit 31 set and
8017 all higher bits unset. */
8018
8019static void
8020normalize_address_expr (expressionS *ex)
8021{
8022 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
8023 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
8024 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
8025 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
8026 - 0x80000000);
8027}
8028
8029/* Try to match TOKENS against OPCODE, storing the result in INSN.
8030 Return true if the match was successful.
8031
8032 OPCODE_EXTRA is a value that should be ORed into the opcode
8033 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
8034 there are more alternatives after OPCODE and SOFT_MATCH is
8035 as for mips_arg_info. */
8036
8037static bfd_boolean
8038match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8039 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8040 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8041{
8042 const char *args;
8043 struct mips_arg_info arg;
8044 const struct mips_operand *operand;
8045 char c;
8046
8047 imm_expr.X_op = O_absent;
97d87491
RS
8048 offset_expr.X_op = O_absent;
8049 offset_reloc[0] = BFD_RELOC_UNUSED;
8050 offset_reloc[1] = BFD_RELOC_UNUSED;
8051 offset_reloc[2] = BFD_RELOC_UNUSED;
8052
8053 create_insn (insn, opcode);
60f20e8b
RS
8054 /* When no opcode suffix is specified, assume ".xyzw". */
8055 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8056 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8057 else
8058 insn->insn_opcode |= opcode_extra;
97d87491
RS
8059 memset (&arg, 0, sizeof (arg));
8060 arg.insn = insn;
8061 arg.token = tokens;
8062 arg.argnum = 1;
8063 arg.last_regno = ILLEGAL_REG;
8064 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8065 arg.lax_match = lax_match;
97d87491
RS
8066 for (args = opcode->args;; ++args)
8067 {
8068 if (arg.token->type == OT_END)
8069 {
8070 /* Handle unary instructions in which only one operand is given.
8071 The source is then the same as the destination. */
8072 if (arg.opnum == 1 && *args == ',')
8073 {
8074 operand = (mips_opts.micromips
8075 ? decode_micromips_operand (args + 1)
8076 : decode_mips_operand (args + 1));
8077 if (operand && mips_optional_operand_p (operand))
8078 {
8079 arg.token = tokens;
8080 arg.argnum = 1;
8081 continue;
8082 }
8083 }
8084
8085 /* Treat elided base registers as $0. */
8086 if (strcmp (args, "(b)") == 0)
8087 args += 3;
8088
8089 if (args[0] == '+')
8090 switch (args[1])
8091 {
8092 case 'K':
8093 case 'N':
8094 /* The register suffix is optional. */
8095 args += 2;
8096 break;
8097 }
8098
8099 /* Fail the match if there were too few operands. */
8100 if (*args)
8101 return FALSE;
8102
8103 /* Successful match. */
60f20e8b
RS
8104 if (!complete_p)
8105 return TRUE;
e3de51ce 8106 clear_insn_error ();
97d87491
RS
8107 if (arg.dest_regno == arg.last_regno
8108 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8109 {
8110 if (arg.opnum == 2)
e3de51ce 8111 set_insn_error
1661c76c 8112 (0, _("source and destination must be different"));
97d87491 8113 else if (arg.last_regno == 31)
e3de51ce 8114 set_insn_error
1661c76c 8115 (0, _("a destination register must be supplied"));
97d87491 8116 }
173d3447
CF
8117 else if (arg.last_regno == 31
8118 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8119 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8120 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8121 check_completed_insn (&arg);
8122 return TRUE;
8123 }
8124
8125 /* Fail the match if the line has too many operands. */
8126 if (*args == 0)
8127 return FALSE;
8128
8129 /* Handle characters that need to match exactly. */
8130 if (*args == '(' || *args == ')' || *args == ',')
8131 {
8132 if (match_char (&arg, *args))
8133 continue;
8134 return FALSE;
8135 }
8136 if (*args == '#')
8137 {
8138 ++args;
8139 if (arg.token->type == OT_DOUBLE_CHAR
8140 && arg.token->u.ch == *args)
8141 {
8142 ++arg.token;
8143 continue;
8144 }
8145 return FALSE;
8146 }
8147
8148 /* Handle special macro operands. Work out the properties of
8149 other operands. */
8150 arg.opnum += 1;
97d87491
RS
8151 switch (*args)
8152 {
7361da2c
AB
8153 case '-':
8154 switch (args[1])
8155 {
8156 case 'A':
8157 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8158 break;
8159
8160 case 'B':
8161 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8162 break;
8163 }
8164 break;
8165
97d87491
RS
8166 case '+':
8167 switch (args[1])
8168 {
97d87491
RS
8169 case 'i':
8170 *offset_reloc = BFD_RELOC_MIPS_JMP;
8171 break;
7361da2c
AB
8172
8173 case '\'':
8174 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8175 break;
8176
8177 case '\"':
8178 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8179 break;
97d87491
RS
8180 }
8181 break;
8182
97d87491 8183 case 'I':
1a00e612
RS
8184 if (!match_const_int (&arg, &imm_expr.X_add_number))
8185 return FALSE;
8186 imm_expr.X_op = O_constant;
bad1aba3 8187 if (GPR_SIZE == 32)
97d87491
RS
8188 normalize_constant_expr (&imm_expr);
8189 continue;
8190
8191 case 'A':
8192 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8193 {
8194 /* Assume that the offset has been elided and that what
8195 we saw was a base register. The match will fail later
8196 if that assumption turns out to be wrong. */
8197 offset_expr.X_op = O_constant;
8198 offset_expr.X_add_number = 0;
8199 }
97d87491 8200 else
1a00e612
RS
8201 {
8202 if (!match_expression (&arg, &offset_expr, offset_reloc))
8203 return FALSE;
8204 normalize_address_expr (&offset_expr);
8205 }
97d87491
RS
8206 continue;
8207
8208 case 'F':
8209 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8210 8, TRUE))
1a00e612 8211 return FALSE;
97d87491
RS
8212 continue;
8213
8214 case 'L':
8215 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8216 8, FALSE))
1a00e612 8217 return FALSE;
97d87491
RS
8218 continue;
8219
8220 case 'f':
8221 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8222 4, TRUE))
1a00e612 8223 return FALSE;
97d87491
RS
8224 continue;
8225
8226 case 'l':
8227 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8228 4, FALSE))
1a00e612 8229 return FALSE;
97d87491
RS
8230 continue;
8231
97d87491
RS
8232 case 'p':
8233 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8234 break;
8235
8236 case 'a':
8237 *offset_reloc = BFD_RELOC_MIPS_JMP;
8238 break;
8239
8240 case 'm':
8241 gas_assert (mips_opts.micromips);
8242 c = args[1];
8243 switch (c)
8244 {
8245 case 'D':
8246 case 'E':
8247 if (!forced_insn_length)
8248 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8249 else if (c == 'D')
8250 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8251 else
8252 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8253 break;
8254 }
8255 break;
8256 }
8257
8258 operand = (mips_opts.micromips
8259 ? decode_micromips_operand (args)
8260 : decode_mips_operand (args));
8261 if (!operand)
8262 abort ();
8263
8264 /* Skip prefixes. */
7361da2c 8265 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8266 args++;
8267
8268 if (mips_optional_operand_p (operand)
8269 && args[1] == ','
8270 && (arg.token[0].type != OT_REG
8271 || arg.token[1].type == OT_END))
8272 {
8273 /* Assume that the register has been elided and is the
8274 same as the first operand. */
8275 arg.token = tokens;
8276 arg.argnum = 1;
8277 }
8278
8279 if (!match_operand (&arg, operand))
8280 return FALSE;
8281 }
8282}
8283
8284/* Like match_insn, but for MIPS16. */
8285
8286static bfd_boolean
8287match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8288 struct mips_operand_token *tokens)
97d87491
RS
8289{
8290 const char *args;
8291 const struct mips_operand *operand;
8292 const struct mips_operand *ext_operand;
82d808ed 8293 bfd_boolean pcrel = FALSE;
7fd53920 8294 int required_insn_length;
97d87491
RS
8295 struct mips_arg_info arg;
8296 int relax_char;
8297
7fd53920
MR
8298 if (forced_insn_length)
8299 required_insn_length = forced_insn_length;
8300 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8301 required_insn_length = 2;
8302 else
8303 required_insn_length = 0;
8304
97d87491
RS
8305 create_insn (insn, opcode);
8306 imm_expr.X_op = O_absent;
97d87491
RS
8307 offset_expr.X_op = O_absent;
8308 offset_reloc[0] = BFD_RELOC_UNUSED;
8309 offset_reloc[1] = BFD_RELOC_UNUSED;
8310 offset_reloc[2] = BFD_RELOC_UNUSED;
8311 relax_char = 0;
8312
8313 memset (&arg, 0, sizeof (arg));
8314 arg.insn = insn;
8315 arg.token = tokens;
8316 arg.argnum = 1;
8317 arg.last_regno = ILLEGAL_REG;
8318 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8319 relax_char = 0;
8320 for (args = opcode->args;; ++args)
8321 {
8322 int c;
8323
8324 if (arg.token->type == OT_END)
8325 {
8326 offsetT value;
8327
8328 /* Handle unary instructions in which only one operand is given.
8329 The source is then the same as the destination. */
8330 if (arg.opnum == 1 && *args == ',')
8331 {
8332 operand = decode_mips16_operand (args[1], FALSE);
8333 if (operand && mips_optional_operand_p (operand))
8334 {
8335 arg.token = tokens;
8336 arg.argnum = 1;
8337 continue;
8338 }
8339 }
8340
8341 /* Fail the match if there were too few operands. */
8342 if (*args)
8343 return FALSE;
8344
8345 /* Successful match. Stuff the immediate value in now, if
8346 we can. */
e3de51ce 8347 clear_insn_error ();
97d87491
RS
8348 if (opcode->pinfo == INSN_MACRO)
8349 {
8350 gas_assert (relax_char == 0 || relax_char == 'p');
8351 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8352 }
8353 else if (relax_char
8354 && offset_expr.X_op == O_constant
82d808ed 8355 && !pcrel
97d87491
RS
8356 && calculate_reloc (*offset_reloc,
8357 offset_expr.X_add_number,
8358 &value))
8359 {
8360 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8361 required_insn_length, &insn->insn_opcode);
97d87491
RS
8362 offset_expr.X_op = O_absent;
8363 *offset_reloc = BFD_RELOC_UNUSED;
8364 }
8365 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8366 {
7fd53920 8367 if (required_insn_length == 2)
e3de51ce 8368 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8369 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8370 {
8371 forced_insn_length = 4;
8372 insn->insn_opcode |= MIPS16_EXTEND;
8373 }
97d87491
RS
8374 }
8375 else if (relax_char)
8376 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8377
8378 check_completed_insn (&arg);
8379 return TRUE;
8380 }
8381
8382 /* Fail the match if the line has too many operands. */
8383 if (*args == 0)
8384 return FALSE;
8385
8386 /* Handle characters that need to match exactly. */
8387 if (*args == '(' || *args == ')' || *args == ',')
8388 {
8389 if (match_char (&arg, *args))
8390 continue;
8391 return FALSE;
8392 }
8393
8394 arg.opnum += 1;
8395 c = *args;
8396 switch (c)
8397 {
8398 case 'p':
8399 case 'q':
8400 case 'A':
8401 case 'B':
8402 case 'E':
25499ac7
MR
8403 case 'V':
8404 case 'u':
97d87491
RS
8405 relax_char = c;
8406 break;
8407
8408 case 'I':
1a00e612
RS
8409 if (!match_const_int (&arg, &imm_expr.X_add_number))
8410 return FALSE;
8411 imm_expr.X_op = O_constant;
bad1aba3 8412 if (GPR_SIZE == 32)
97d87491
RS
8413 normalize_constant_expr (&imm_expr);
8414 continue;
8415
8416 case 'a':
8417 case 'i':
8418 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8419 break;
8420 }
8421
7fd53920 8422 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8423 if (!operand)
8424 abort ();
8425
82d808ed
MR
8426 if (operand->type == OP_PCREL)
8427 pcrel = TRUE;
8428 else
97d87491
RS
8429 {
8430 ext_operand = decode_mips16_operand (c, TRUE);
8431 if (operand != ext_operand)
8432 {
8433 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8434 {
8435 offset_expr.X_op = O_constant;
8436 offset_expr.X_add_number = 0;
8437 relax_char = c;
8438 continue;
8439 }
8440
1a7bf198 8441 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8442 return FALSE;
8443
8444 /* '8' is used for SLTI(U) and has traditionally not
8445 been allowed to take relocation operators. */
8446 if (offset_reloc[0] != BFD_RELOC_UNUSED
8447 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8448 {
8449 match_not_constant (&arg);
8450 return FALSE;
8451 }
97d87491 8452
c96425c5
MR
8453 if (offset_expr.X_op == O_big)
8454 {
8455 match_out_of_range (&arg);
8456 return FALSE;
8457 }
8458
97d87491
RS
8459 relax_char = c;
8460 continue;
8461 }
8462 }
8463
8464 if (mips_optional_operand_p (operand)
8465 && args[1] == ','
8466 && (arg.token[0].type != OT_REG
8467 || arg.token[1].type == OT_END))
8468 {
8469 /* Assume that the register has been elided and is the
8470 same as the first operand. */
8471 arg.token = tokens;
8472 arg.argnum = 1;
8473 }
8474
8475 if (!match_operand (&arg, operand))
8476 return FALSE;
8477 }
8478}
8479
60f20e8b
RS
8480/* Record that the current instruction is invalid for the current ISA. */
8481
8482static void
8483match_invalid_for_isa (void)
8484{
8485 set_insn_error_ss
1661c76c 8486 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8487 mips_cpu_info_from_arch (mips_opts.arch)->name,
8488 mips_cpu_info_from_isa (mips_opts.isa)->name);
8489}
8490
8491/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8492 Return true if a definite match or failure was found, storing any match
8493 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8494 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8495 tried and failed to match under normal conditions and now want to try a
8496 more relaxed match. */
8497
8498static bfd_boolean
8499match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8500 const struct mips_opcode *past, struct mips_operand_token *tokens,
8501 int opcode_extra, bfd_boolean lax_match)
8502{
8503 const struct mips_opcode *opcode;
8504 const struct mips_opcode *invalid_delay_slot;
8505 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8506
8507 /* Search for a match, ignoring alternatives that don't satisfy the
8508 current ISA or forced_length. */
8509 invalid_delay_slot = 0;
8510 seen_valid_for_isa = FALSE;
8511 seen_valid_for_size = FALSE;
8512 opcode = first;
8513 do
8514 {
8515 gas_assert (strcmp (opcode->name, first->name) == 0);
8516 if (is_opcode_valid (opcode))
8517 {
8518 seen_valid_for_isa = TRUE;
8519 if (is_size_valid (opcode))
8520 {
8521 bfd_boolean delay_slot_ok;
8522
8523 seen_valid_for_size = TRUE;
8524 delay_slot_ok = is_delay_slot_valid (opcode);
8525 if (match_insn (insn, opcode, tokens, opcode_extra,
8526 lax_match, delay_slot_ok))
8527 {
8528 if (!delay_slot_ok)
8529 {
8530 if (!invalid_delay_slot)
8531 invalid_delay_slot = opcode;
8532 }
8533 else
8534 return TRUE;
8535 }
8536 }
8537 }
8538 ++opcode;
8539 }
8540 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8541
8542 /* If the only matches we found had the wrong length for the delay slot,
8543 pick the first such match. We'll issue an appropriate warning later. */
8544 if (invalid_delay_slot)
8545 {
8546 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8547 lax_match, TRUE))
8548 return TRUE;
8549 abort ();
8550 }
8551
8552 /* Handle the case where we didn't try to match an instruction because
8553 all the alternatives were incompatible with the current ISA. */
8554 if (!seen_valid_for_isa)
8555 {
8556 match_invalid_for_isa ();
8557 return TRUE;
8558 }
8559
8560 /* Handle the case where we didn't try to match an instruction because
8561 all the alternatives were of the wrong size. */
8562 if (!seen_valid_for_size)
8563 {
8564 if (mips_opts.insn32)
1661c76c 8565 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8566 else
8567 set_insn_error_i
1661c76c 8568 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8569 8 * forced_insn_length);
8570 return TRUE;
8571 }
8572
8573 return FALSE;
8574}
8575
8576/* Like match_insns, but for MIPS16. */
8577
8578static bfd_boolean
8579match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8580 struct mips_operand_token *tokens)
8581{
8582 const struct mips_opcode *opcode;
8583 bfd_boolean seen_valid_for_isa;
7fd53920 8584 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8585
8586 /* Search for a match, ignoring alternatives that don't satisfy the
8587 current ISA. There are no separate entries for extended forms so
8588 we deal with forced_length later. */
8589 seen_valid_for_isa = FALSE;
7fd53920 8590 seen_valid_for_size = FALSE;
60f20e8b
RS
8591 opcode = first;
8592 do
8593 {
8594 gas_assert (strcmp (opcode->name, first->name) == 0);
8595 if (is_opcode_valid_16 (opcode))
8596 {
8597 seen_valid_for_isa = TRUE;
7fd53920
MR
8598 if (is_size_valid_16 (opcode))
8599 {
8600 seen_valid_for_size = TRUE;
8601 if (match_mips16_insn (insn, opcode, tokens))
8602 return TRUE;
8603 }
60f20e8b
RS
8604 }
8605 ++opcode;
8606 }
8607 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8608 && strcmp (opcode->name, first->name) == 0);
8609
8610 /* Handle the case where we didn't try to match an instruction because
8611 all the alternatives were incompatible with the current ISA. */
8612 if (!seen_valid_for_isa)
8613 {
8614 match_invalid_for_isa ();
8615 return TRUE;
8616 }
8617
7fd53920
MR
8618 /* Handle the case where we didn't try to match an instruction because
8619 all the alternatives were of the wrong size. */
8620 if (!seen_valid_for_size)
8621 {
8622 if (forced_insn_length == 2)
8623 set_insn_error
8624 (0, _("unrecognized unextended version of MIPS16 opcode"));
8625 else
8626 set_insn_error
8627 (0, _("unrecognized extended version of MIPS16 opcode"));
8628 return TRUE;
8629 }
8630
60f20e8b
RS
8631 return FALSE;
8632}
8633
584892a6
RS
8634/* Set up global variables for the start of a new macro. */
8635
8636static void
8637macro_start (void)
8638{
8639 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8640 memset (&mips_macro_warning.first_insn_sizes, 0,
8641 sizeof (mips_macro_warning.first_insn_sizes));
8642 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8643 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8644 && delayed_branch_p (&history[0]));
7bd374a4
MR
8645 if (history[0].frag
8646 && history[0].frag->fr_type == rs_machine_dependent
8647 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8648 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8649 mips_macro_warning.delay_slot_length = 0;
8650 else
8651 switch (history[0].insn_mo->pinfo2
8652 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8653 {
8654 case INSN2_BRANCH_DELAY_32BIT:
8655 mips_macro_warning.delay_slot_length = 4;
8656 break;
8657 case INSN2_BRANCH_DELAY_16BIT:
8658 mips_macro_warning.delay_slot_length = 2;
8659 break;
8660 default:
8661 mips_macro_warning.delay_slot_length = 0;
8662 break;
8663 }
df58fc94 8664 mips_macro_warning.first_frag = NULL;
584892a6
RS
8665}
8666
df58fc94
RS
8667/* Given that a macro is longer than one instruction or of the wrong size,
8668 return the appropriate warning for it. Return null if no warning is
8669 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8670 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8671 and RELAX_NOMACRO. */
584892a6
RS
8672
8673static const char *
8674macro_warning (relax_substateT subtype)
8675{
8676 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8677 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8678 " in a branch delay slot");
8679 else if (subtype & RELAX_NOMACRO)
1661c76c 8680 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8681 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8682 | RELAX_DELAY_SLOT_SIZE_SECOND))
8683 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8684 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8685 " in a 16-bit branch delay slot")
1661c76c 8686 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8687 " in a 32-bit branch delay slot"));
584892a6
RS
8688 else
8689 return 0;
8690}
8691
8692/* Finish up a macro. Emit warnings as appropriate. */
8693
8694static void
8695macro_end (void)
8696{
df58fc94
RS
8697 /* Relaxation warning flags. */
8698 relax_substateT subtype = 0;
8699
8700 /* Check delay slot size requirements. */
8701 if (mips_macro_warning.delay_slot_length == 2)
8702 subtype |= RELAX_DELAY_SLOT_16BIT;
8703 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8704 {
df58fc94
RS
8705 if (mips_macro_warning.delay_slot_length
8706 != mips_macro_warning.first_insn_sizes[0])
8707 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8708 if (mips_macro_warning.delay_slot_length
8709 != mips_macro_warning.first_insn_sizes[1])
8710 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8711 }
584892a6 8712
df58fc94
RS
8713 /* Check instruction count requirements. */
8714 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8715 {
8716 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8717 subtype |= RELAX_SECOND_LONGER;
8718 if (mips_opts.warn_about_macros)
8719 subtype |= RELAX_NOMACRO;
8720 if (mips_macro_warning.delay_slot_p)
8721 subtype |= RELAX_DELAY_SLOT;
df58fc94 8722 }
584892a6 8723
df58fc94
RS
8724 /* If both alternatives fail to fill a delay slot correctly,
8725 emit the warning now. */
8726 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8727 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8728 {
8729 relax_substateT s;
8730 const char *msg;
8731
8732 s = subtype & (RELAX_DELAY_SLOT_16BIT
8733 | RELAX_DELAY_SLOT_SIZE_FIRST
8734 | RELAX_DELAY_SLOT_SIZE_SECOND);
8735 msg = macro_warning (s);
8736 if (msg != NULL)
8737 as_warn ("%s", msg);
8738 subtype &= ~s;
8739 }
8740
8741 /* If both implementations are longer than 1 instruction, then emit the
8742 warning now. */
8743 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8744 {
8745 relax_substateT s;
8746 const char *msg;
8747
8748 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8749 msg = macro_warning (s);
8750 if (msg != NULL)
8751 as_warn ("%s", msg);
8752 subtype &= ~s;
584892a6 8753 }
df58fc94
RS
8754
8755 /* If any flags still set, then one implementation might need a warning
8756 and the other either will need one of a different kind or none at all.
8757 Pass any remaining flags over to relaxation. */
8758 if (mips_macro_warning.first_frag != NULL)
8759 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8760}
8761
df58fc94
RS
8762/* Instruction operand formats used in macros that vary between
8763 standard MIPS and microMIPS code. */
8764
833794fc 8765static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8766static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8767static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8768static const char * const lui_fmt[2] = { "t,u", "s,u" };
8769static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8770static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8771static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8772static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8773
833794fc 8774#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8775#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8776 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8777#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8778#define LUI_FMT (lui_fmt[mips_opts.micromips])
8779#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8780#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8781 : mem12_fmt[mips_opts.micromips])
833794fc 8782#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8783#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8784#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8785
6e1304d8
RS
8786/* Read a macro's relocation codes from *ARGS and store them in *R.
8787 The first argument in *ARGS will be either the code for a single
8788 relocation or -1 followed by the three codes that make up a
8789 composite relocation. */
8790
8791static void
8792macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8793{
8794 int i, next;
8795
8796 next = va_arg (*args, int);
8797 if (next >= 0)
8798 r[0] = (bfd_reloc_code_real_type) next;
8799 else
f2ae14a1
RS
8800 {
8801 for (i = 0; i < 3; i++)
8802 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8803 /* This function is only used for 16-bit relocation fields.
8804 To make the macro code simpler, treat an unrelocated value
8805 in the same way as BFD_RELOC_LO16. */
8806 if (r[0] == BFD_RELOC_UNUSED)
8807 r[0] = BFD_RELOC_LO16;
8808 }
6e1304d8
RS
8809}
8810
252b5132
RH
8811/* Build an instruction created by a macro expansion. This is passed
8812 a pointer to the count of instructions created so far, an
8813 expression, the name of the instruction to build, an operand format
8814 string, and corresponding arguments. */
8815
252b5132 8816static void
67c0d1eb 8817macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8818{
df58fc94 8819 const struct mips_opcode *mo = NULL;
f6688943 8820 bfd_reloc_code_real_type r[3];
df58fc94 8821 const struct mips_opcode *amo;
e077a1c8 8822 const struct mips_operand *operand;
df58fc94
RS
8823 struct hash_control *hash;
8824 struct mips_cl_insn insn;
252b5132 8825 va_list args;
e077a1c8 8826 unsigned int uval;
252b5132 8827
252b5132 8828 va_start (args, fmt);
252b5132 8829
252b5132
RH
8830 if (mips_opts.mips16)
8831 {
03ea81db 8832 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8833 va_end (args);
8834 return;
8835 }
8836
f6688943
TS
8837 r[0] = BFD_RELOC_UNUSED;
8838 r[1] = BFD_RELOC_UNUSED;
8839 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8840 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8841 amo = (struct mips_opcode *) hash_find (hash, name);
8842 gas_assert (amo);
8843 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8844
df58fc94 8845 do
8b082fb1
TS
8846 {
8847 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8848 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8849 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8850 slot instruction length requirement (if any) of the previous
8851 instruction. While doing this we record the first instruction
8852 seen that matches all the other conditions and use it anyway
8853 if the requirement cannot be met; we will issue an appropriate
8854 warning later on. */
8855 if (strcmp (fmt, amo->args) == 0
8856 && amo->pinfo != INSN_MACRO
8857 && is_opcode_valid (amo)
8858 && is_size_valid (amo))
8859 {
8860 if (is_delay_slot_valid (amo))
8861 {
8862 mo = amo;
8863 break;
8864 }
8865 else if (!mo)
8866 mo = amo;
8867 }
8b082fb1 8868
df58fc94
RS
8869 ++amo;
8870 gas_assert (amo->name);
252b5132 8871 }
df58fc94 8872 while (strcmp (name, amo->name) == 0);
252b5132 8873
df58fc94 8874 gas_assert (mo);
1e915849 8875 create_insn (&insn, mo);
e077a1c8 8876 for (; *fmt; ++fmt)
252b5132 8877 {
e077a1c8 8878 switch (*fmt)
252b5132 8879 {
252b5132
RH
8880 case ',':
8881 case '(':
8882 case ')':
252b5132 8883 case 'z':
e077a1c8 8884 break;
252b5132
RH
8885
8886 case 'i':
8887 case 'j':
6e1304d8 8888 macro_read_relocs (&args, r);
9c2799c2 8889 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8890 || *r == BFD_RELOC_MIPS_HIGHER
8891 || *r == BFD_RELOC_HI16_S
8892 || *r == BFD_RELOC_LO16
14c80123
MR
8893 || *r == BFD_RELOC_MIPS_GOT_OFST
8894 || (mips_opts.micromips
8895 && (*r == BFD_RELOC_16
8896 || *r == BFD_RELOC_MIPS_GOT16
8897 || *r == BFD_RELOC_MIPS_CALL16
8898 || *r == BFD_RELOC_MIPS_GOT_HI16
8899 || *r == BFD_RELOC_MIPS_GOT_LO16
8900 || *r == BFD_RELOC_MIPS_CALL_HI16
8901 || *r == BFD_RELOC_MIPS_CALL_LO16
8902 || *r == BFD_RELOC_MIPS_SUB
8903 || *r == BFD_RELOC_MIPS_GOT_PAGE
8904 || *r == BFD_RELOC_MIPS_HIGHEST
8905 || *r == BFD_RELOC_MIPS_GOT_DISP
8906 || *r == BFD_RELOC_MIPS_TLS_GD
8907 || *r == BFD_RELOC_MIPS_TLS_LDM
8908 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
8909 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
8910 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
8911 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
8912 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 8913 break;
e391c024
RS
8914
8915 case 'o':
8916 macro_read_relocs (&args, r);
e077a1c8 8917 break;
252b5132
RH
8918
8919 case 'u':
6e1304d8 8920 macro_read_relocs (&args, r);
9c2799c2 8921 gas_assert (ep != NULL
90ecf173
MR
8922 && (ep->X_op == O_constant
8923 || (ep->X_op == O_symbol
8924 && (*r == BFD_RELOC_MIPS_HIGHEST
8925 || *r == BFD_RELOC_HI16_S
8926 || *r == BFD_RELOC_HI16
8927 || *r == BFD_RELOC_GPREL16
8928 || *r == BFD_RELOC_MIPS_GOT_HI16
8929 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8930 break;
252b5132
RH
8931
8932 case 'p':
9c2799c2 8933 gas_assert (ep != NULL);
bad36eac 8934
252b5132
RH
8935 /*
8936 * This allows macro() to pass an immediate expression for
8937 * creating short branches without creating a symbol.
bad36eac
DJ
8938 *
8939 * We don't allow branch relaxation for these branches, as
8940 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8941 */
8942 if (ep->X_op == O_constant)
8943 {
df58fc94
RS
8944 /* For microMIPS we always use relocations for branches.
8945 So we should not resolve immediate values. */
8946 gas_assert (!mips_opts.micromips);
8947
bad36eac
DJ
8948 if ((ep->X_add_number & 3) != 0)
8949 as_bad (_("branch to misaligned address (0x%lx)"),
8950 (unsigned long) ep->X_add_number);
8951 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8952 as_bad (_("branch address range overflow (0x%lx)"),
8953 (unsigned long) ep->X_add_number);
252b5132
RH
8954 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8955 ep = NULL;
8956 }
8957 else
0b25d3e6 8958 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8959 break;
252b5132
RH
8960
8961 case 'a':
9c2799c2 8962 gas_assert (ep != NULL);
f6688943 8963 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8964 break;
d43b4baf 8965
252b5132 8966 default:
e077a1c8
RS
8967 operand = (mips_opts.micromips
8968 ? decode_micromips_operand (fmt)
8969 : decode_mips_operand (fmt));
8970 if (!operand)
8971 abort ();
8972
8973 uval = va_arg (args, int);
8974 if (operand->type == OP_CLO_CLZ_DEST)
8975 uval |= (uval << 5);
8976 insn_insert_operand (&insn, operand, uval);
8977
7361da2c 8978 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8979 ++fmt;
8980 break;
252b5132 8981 }
252b5132
RH
8982 }
8983 va_end (args);
9c2799c2 8984 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8985
df58fc94 8986 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8987}
8988
8989static void
67c0d1eb 8990mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8991 va_list *args)
252b5132 8992{
1e915849 8993 struct mips_opcode *mo;
252b5132 8994 struct mips_cl_insn insn;
e077a1c8 8995 const struct mips_operand *operand;
f6688943
TS
8996 bfd_reloc_code_real_type r[3]
8997 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8998
1e915849 8999 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
9000 gas_assert (mo);
9001 gas_assert (strcmp (name, mo->name) == 0);
252b5132 9002
1e915849 9003 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 9004 {
1e915849 9005 ++mo;
9c2799c2
NC
9006 gas_assert (mo->name);
9007 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
9008 }
9009
1e915849 9010 create_insn (&insn, mo);
e077a1c8 9011 for (; *fmt; ++fmt)
252b5132
RH
9012 {
9013 int c;
9014
e077a1c8 9015 c = *fmt;
252b5132
RH
9016 switch (c)
9017 {
252b5132
RH
9018 case ',':
9019 case '(':
9020 case ')':
e077a1c8 9021 break;
252b5132 9022
d8722d76 9023 case '.':
252b5132
RH
9024 case 'S':
9025 case 'P':
9026 case 'R':
e077a1c8 9027 break;
252b5132
RH
9028
9029 case '<':
252b5132 9030 case '5':
d8722d76 9031 case 'F':
252b5132
RH
9032 case 'H':
9033 case 'W':
9034 case 'D':
9035 case 'j':
9036 case '8':
9037 case 'V':
9038 case 'C':
9039 case 'U':
9040 case 'k':
9041 case 'K':
9042 case 'p':
9043 case 'q':
9044 {
b886a2ab
RS
9045 offsetT value;
9046
9c2799c2 9047 gas_assert (ep != NULL);
252b5132
RH
9048
9049 if (ep->X_op != O_constant)
874e8986 9050 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9051 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9052 {
b886a2ab 9053 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9054 ep = NULL;
f6688943 9055 *r = BFD_RELOC_UNUSED;
252b5132
RH
9056 }
9057 }
e077a1c8 9058 break;
252b5132 9059
e077a1c8
RS
9060 default:
9061 operand = decode_mips16_operand (c, FALSE);
9062 if (!operand)
9063 abort ();
252b5132 9064
4a06e5a2 9065 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9066 break;
9067 }
252b5132
RH
9068 }
9069
9c2799c2 9070 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9071
df58fc94 9072 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9073}
9074
438c16b8
TS
9075/*
9076 * Generate a "jalr" instruction with a relocation hint to the called
9077 * function. This occurs in NewABI PIC code.
9078 */
9079static void
df58fc94 9080macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9081{
df58fc94
RS
9082 static const bfd_reloc_code_real_type jalr_relocs[2]
9083 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9084 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9085 const char *jalr;
685736be 9086 char *f = NULL;
b34976b6 9087
1180b5a4 9088 if (MIPS_JALR_HINT_P (ep))
f21f8242 9089 {
cc3d92a5 9090 frag_grow (8);
f21f8242
AO
9091 f = frag_more (0);
9092 }
2906b037 9093 if (mips_opts.micromips)
df58fc94 9094 {
833794fc
MR
9095 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9096 ? "jalr" : "jalrs");
e64af278 9097 if (MIPS_JALR_HINT_P (ep)
833794fc 9098 || mips_opts.insn32
e64af278 9099 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9100 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9101 else
9102 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9103 }
2906b037
MR
9104 else
9105 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9106 if (MIPS_JALR_HINT_P (ep))
df58fc94 9107 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9108}
9109
252b5132
RH
9110/*
9111 * Generate a "lui" instruction.
9112 */
9113static void
67c0d1eb 9114macro_build_lui (expressionS *ep, int regnum)
252b5132 9115{
9c2799c2 9116 gas_assert (! mips_opts.mips16);
252b5132 9117
df58fc94 9118 if (ep->X_op != O_constant)
252b5132 9119 {
9c2799c2 9120 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9121 /* _gp_disp is a special case, used from s_cpload.
9122 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9123 gas_assert (mips_pic == NO_PIC
78e1bb40 9124 || (! HAVE_NEWABI
aa6975fb
ILT
9125 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9126 || (! mips_in_shared
bbe506e8
TS
9127 && strcmp (S_GET_NAME (ep->X_add_symbol),
9128 "__gnu_local_gp") == 0));
252b5132
RH
9129 }
9130
df58fc94 9131 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9132}
9133
885add95
CD
9134/* Generate a sequence of instructions to do a load or store from a constant
9135 offset off of a base register (breg) into/from a target register (treg),
9136 using AT if necessary. */
9137static void
67c0d1eb
RS
9138macro_build_ldst_constoffset (expressionS *ep, const char *op,
9139 int treg, int breg, int dbl)
885add95 9140{
9c2799c2 9141 gas_assert (ep->X_op == O_constant);
885add95 9142
256ab948 9143 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9144 if (!dbl)
9145 normalize_constant_expr (ep);
256ab948 9146
67c1ffbe 9147 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9148 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9149 as_warn (_("operand overflow"));
9150
9151 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9152 {
9153 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9154 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9155 }
9156 else
9157 {
9158 /* 32-bit offset, need multiple instructions and AT, like:
9159 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9160 addu $tempreg,$tempreg,$breg
9161 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9162 to handle the complete offset. */
67c0d1eb
RS
9163 macro_build_lui (ep, AT);
9164 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9165 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9166
741fe287 9167 if (!mips_opts.at)
1661c76c 9168 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9169 }
9170}
9171
252b5132
RH
9172/* set_at()
9173 * Generates code to set the $at register to true (one)
9174 * if reg is less than the immediate expression.
9175 */
9176static void
67c0d1eb 9177set_at (int reg, int unsignedp)
252b5132 9178{
b0e6f033 9179 if (imm_expr.X_add_number >= -0x8000
252b5132 9180 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9181 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9182 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9183 else
9184 {
bad1aba3 9185 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9186 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9187 }
9188}
9189
252b5132
RH
9190/* Count the leading zeroes by performing a binary chop. This is a
9191 bulky bit of source, but performance is a LOT better for the
9192 majority of values than a simple loop to count the bits:
9193 for (lcnt = 0; (lcnt < 32); lcnt++)
9194 if ((v) & (1 << (31 - lcnt)))
9195 break;
9196 However it is not code size friendly, and the gain will drop a bit
9197 on certain cached systems.
9198*/
9199#define COUNT_TOP_ZEROES(v) \
9200 (((v) & ~0xffff) == 0 \
9201 ? ((v) & ~0xff) == 0 \
9202 ? ((v) & ~0xf) == 0 \
9203 ? ((v) & ~0x3) == 0 \
9204 ? ((v) & ~0x1) == 0 \
9205 ? !(v) \
9206 ? 32 \
9207 : 31 \
9208 : 30 \
9209 : ((v) & ~0x7) == 0 \
9210 ? 29 \
9211 : 28 \
9212 : ((v) & ~0x3f) == 0 \
9213 ? ((v) & ~0x1f) == 0 \
9214 ? 27 \
9215 : 26 \
9216 : ((v) & ~0x7f) == 0 \
9217 ? 25 \
9218 : 24 \
9219 : ((v) & ~0xfff) == 0 \
9220 ? ((v) & ~0x3ff) == 0 \
9221 ? ((v) & ~0x1ff) == 0 \
9222 ? 23 \
9223 : 22 \
9224 : ((v) & ~0x7ff) == 0 \
9225 ? 21 \
9226 : 20 \
9227 : ((v) & ~0x3fff) == 0 \
9228 ? ((v) & ~0x1fff) == 0 \
9229 ? 19 \
9230 : 18 \
9231 : ((v) & ~0x7fff) == 0 \
9232 ? 17 \
9233 : 16 \
9234 : ((v) & ~0xffffff) == 0 \
9235 ? ((v) & ~0xfffff) == 0 \
9236 ? ((v) & ~0x3ffff) == 0 \
9237 ? ((v) & ~0x1ffff) == 0 \
9238 ? 15 \
9239 : 14 \
9240 : ((v) & ~0x7ffff) == 0 \
9241 ? 13 \
9242 : 12 \
9243 : ((v) & ~0x3fffff) == 0 \
9244 ? ((v) & ~0x1fffff) == 0 \
9245 ? 11 \
9246 : 10 \
9247 : ((v) & ~0x7fffff) == 0 \
9248 ? 9 \
9249 : 8 \
9250 : ((v) & ~0xfffffff) == 0 \
9251 ? ((v) & ~0x3ffffff) == 0 \
9252 ? ((v) & ~0x1ffffff) == 0 \
9253 ? 7 \
9254 : 6 \
9255 : ((v) & ~0x7ffffff) == 0 \
9256 ? 5 \
9257 : 4 \
9258 : ((v) & ~0x3fffffff) == 0 \
9259 ? ((v) & ~0x1fffffff) == 0 \
9260 ? 3 \
9261 : 2 \
9262 : ((v) & ~0x7fffffff) == 0 \
9263 ? 1 \
9264 : 0)
9265
9266/* load_register()
67c1ffbe 9267 * This routine generates the least number of instructions necessary to load
252b5132
RH
9268 * an absolute expression value into a register.
9269 */
9270static void
67c0d1eb 9271load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9272{
9273 int freg;
9274 expressionS hi32, lo32;
9275
9276 if (ep->X_op != O_big)
9277 {
9c2799c2 9278 gas_assert (ep->X_op == O_constant);
256ab948
TS
9279
9280 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9281 if (!dbl)
9282 normalize_constant_expr (ep);
256ab948
TS
9283
9284 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9285 {
9286 /* We can handle 16 bit signed values with an addiu to
9287 $zero. No need to ever use daddiu here, since $zero and
9288 the result are always correct in 32 bit mode. */
67c0d1eb 9289 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9290 return;
9291 }
9292 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9293 {
9294 /* We can handle 16 bit unsigned values with an ori to
9295 $zero. */
67c0d1eb 9296 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9297 return;
9298 }
256ab948 9299 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9300 {
9301 /* 32 bit values require an lui. */
df58fc94 9302 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9303 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9304 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9305 return;
9306 }
9307 }
9308
9309 /* The value is larger than 32 bits. */
9310
bad1aba3 9311 if (!dbl || GPR_SIZE == 32)
252b5132 9312 {
55e08f71
NC
9313 char value[32];
9314
9315 sprintf_vma (value, ep->X_add_number);
1661c76c 9316 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9317 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9318 return;
9319 }
9320
9321 if (ep->X_op != O_big)
9322 {
9323 hi32 = *ep;
9324 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9325 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9326 hi32.X_add_number &= 0xffffffff;
9327 lo32 = *ep;
9328 lo32.X_add_number &= 0xffffffff;
9329 }
9330 else
9331 {
9c2799c2 9332 gas_assert (ep->X_add_number > 2);
252b5132
RH
9333 if (ep->X_add_number == 3)
9334 generic_bignum[3] = 0;
9335 else if (ep->X_add_number > 4)
1661c76c 9336 as_bad (_("number larger than 64 bits"));
252b5132
RH
9337 lo32.X_op = O_constant;
9338 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9339 hi32.X_op = O_constant;
9340 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9341 }
9342
9343 if (hi32.X_add_number == 0)
9344 freg = 0;
9345 else
9346 {
9347 int shift, bit;
9348 unsigned long hi, lo;
9349
956cd1d6 9350 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9351 {
9352 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9353 {
67c0d1eb 9354 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9355 return;
9356 }
9357 if (lo32.X_add_number & 0x80000000)
9358 {
df58fc94 9359 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9360 if (lo32.X_add_number & 0xffff)
67c0d1eb 9361 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9362 return;
9363 }
9364 }
252b5132
RH
9365
9366 /* Check for 16bit shifted constant. We know that hi32 is
9367 non-zero, so start the mask on the first bit of the hi32
9368 value. */
9369 shift = 17;
9370 do
beae10d5
KH
9371 {
9372 unsigned long himask, lomask;
9373
9374 if (shift < 32)
9375 {
9376 himask = 0xffff >> (32 - shift);
9377 lomask = (0xffff << shift) & 0xffffffff;
9378 }
9379 else
9380 {
9381 himask = 0xffff << (shift - 32);
9382 lomask = 0;
9383 }
9384 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9385 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9386 {
9387 expressionS tmp;
9388
9389 tmp.X_op = O_constant;
9390 if (shift < 32)
9391 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9392 | (lo32.X_add_number >> shift));
9393 else
9394 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9395 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9396 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9397 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9398 return;
9399 }
f9419b05 9400 ++shift;
beae10d5
KH
9401 }
9402 while (shift <= (64 - 16));
252b5132
RH
9403
9404 /* Find the bit number of the lowest one bit, and store the
9405 shifted value in hi/lo. */
9406 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9407 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9408 if (lo != 0)
9409 {
9410 bit = 0;
9411 while ((lo & 1) == 0)
9412 {
9413 lo >>= 1;
9414 ++bit;
9415 }
9416 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9417 hi >>= bit;
9418 }
9419 else
9420 {
9421 bit = 32;
9422 while ((hi & 1) == 0)
9423 {
9424 hi >>= 1;
9425 ++bit;
9426 }
9427 lo = hi;
9428 hi = 0;
9429 }
9430
9431 /* Optimize if the shifted value is a (power of 2) - 1. */
9432 if ((hi == 0 && ((lo + 1) & lo) == 0)
9433 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9434 {
9435 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9436 if (shift != 0)
beae10d5 9437 {
252b5132
RH
9438 expressionS tmp;
9439
9440 /* This instruction will set the register to be all
9441 ones. */
beae10d5
KH
9442 tmp.X_op = O_constant;
9443 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9444 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9445 if (bit != 0)
9446 {
9447 bit += shift;
df58fc94 9448 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9449 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9450 }
df58fc94 9451 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9452 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9453 return;
9454 }
9455 }
252b5132
RH
9456
9457 /* Sign extend hi32 before calling load_register, because we can
9458 generally get better code when we load a sign extended value. */
9459 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9460 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9461 load_register (reg, &hi32, 0);
252b5132
RH
9462 freg = reg;
9463 }
9464 if ((lo32.X_add_number & 0xffff0000) == 0)
9465 {
9466 if (freg != 0)
9467 {
df58fc94 9468 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9469 freg = reg;
9470 }
9471 }
9472 else
9473 {
9474 expressionS mid16;
9475
956cd1d6 9476 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9477 {
df58fc94
RS
9478 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9479 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9480 return;
9481 }
252b5132
RH
9482
9483 if (freg != 0)
9484 {
df58fc94 9485 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9486 freg = reg;
9487 }
9488 mid16 = lo32;
9489 mid16.X_add_number >>= 16;
67c0d1eb 9490 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9491 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9492 freg = reg;
9493 }
9494 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9495 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9496}
9497
269137b2
TS
9498static inline void
9499load_delay_nop (void)
9500{
9501 if (!gpr_interlocks)
9502 macro_build (NULL, "nop", "");
9503}
9504
252b5132
RH
9505/* Load an address into a register. */
9506
9507static void
67c0d1eb 9508load_address (int reg, expressionS *ep, int *used_at)
252b5132 9509{
252b5132
RH
9510 if (ep->X_op != O_constant
9511 && ep->X_op != O_symbol)
9512 {
9513 as_bad (_("expression too complex"));
9514 ep->X_op = O_constant;
9515 }
9516
9517 if (ep->X_op == O_constant)
9518 {
67c0d1eb 9519 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9520 return;
9521 }
9522
9523 if (mips_pic == NO_PIC)
9524 {
9525 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9526 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9527 Otherwise we want
9528 lui $reg,<sym> (BFD_RELOC_HI16_S)
9529 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9530 If we have an addend, we always use the latter form.
76b3015f 9531
d6bc6245
TS
9532 With 64bit address space and a usable $at we want
9533 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9534 lui $at,<sym> (BFD_RELOC_HI16_S)
9535 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9536 daddiu $at,<sym> (BFD_RELOC_LO16)
9537 dsll32 $reg,0
3a482fd5 9538 daddu $reg,$reg,$at
76b3015f 9539
c03099e6 9540 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9541 on superscalar processors.
9542 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9543 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9544 dsll $reg,16
9545 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9546 dsll $reg,16
9547 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9548
9549 For GP relative symbols in 64bit address space we can use
9550 the same sequence as in 32bit address space. */
aed1a261 9551 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9552 {
6caf9ef4
TS
9553 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9554 && !nopic_need_relax (ep->X_add_symbol, 1))
9555 {
9556 relax_start (ep->X_add_symbol);
9557 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9558 mips_gp_register, BFD_RELOC_GPREL16);
9559 relax_switch ();
9560 }
d6bc6245 9561
741fe287 9562 if (*used_at == 0 && mips_opts.at)
d6bc6245 9563 {
df58fc94
RS
9564 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9565 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9566 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9567 BFD_RELOC_MIPS_HIGHER);
9568 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9569 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9570 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9571 *used_at = 1;
9572 }
9573 else
9574 {
df58fc94 9575 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9576 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9577 BFD_RELOC_MIPS_HIGHER);
df58fc94 9578 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9579 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9580 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9581 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9582 }
6caf9ef4
TS
9583
9584 if (mips_relax.sequence)
9585 relax_end ();
d6bc6245 9586 }
252b5132
RH
9587 else
9588 {
d6bc6245 9589 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9590 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9591 {
4d7206a2 9592 relax_start (ep->X_add_symbol);
67c0d1eb 9593 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9594 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9595 relax_switch ();
d6bc6245 9596 }
67c0d1eb
RS
9597 macro_build_lui (ep, reg);
9598 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9599 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9600 if (mips_relax.sequence)
9601 relax_end ();
d6bc6245 9602 }
252b5132 9603 }
0a44bf69 9604 else if (!mips_big_got)
252b5132
RH
9605 {
9606 expressionS ex;
9607
9608 /* If this is a reference to an external symbol, we want
9609 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9610 Otherwise we want
9611 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9612 nop
9613 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9614 If there is a constant, it must be added in after.
9615
ed6fb7bd 9616 If we have NewABI, we want
f5040a92
AO
9617 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9618 unless we're referencing a global symbol with a non-zero
9619 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9620 if (HAVE_NEWABI)
9621 {
f5040a92
AO
9622 if (ep->X_add_number)
9623 {
4d7206a2 9624 ex.X_add_number = ep->X_add_number;
f5040a92 9625 ep->X_add_number = 0;
4d7206a2 9626 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9627 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9628 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9629 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9630 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9631 ex.X_op = O_constant;
67c0d1eb 9632 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9633 reg, reg, BFD_RELOC_LO16);
f5040a92 9634 ep->X_add_number = ex.X_add_number;
4d7206a2 9635 relax_switch ();
f5040a92 9636 }
67c0d1eb 9637 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9638 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9639 if (mips_relax.sequence)
9640 relax_end ();
ed6fb7bd
SC
9641 }
9642 else
9643 {
f5040a92
AO
9644 ex.X_add_number = ep->X_add_number;
9645 ep->X_add_number = 0;
67c0d1eb
RS
9646 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9647 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9648 load_delay_nop ();
4d7206a2
RS
9649 relax_start (ep->X_add_symbol);
9650 relax_switch ();
67c0d1eb 9651 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9652 BFD_RELOC_LO16);
4d7206a2 9653 relax_end ();
ed6fb7bd 9654
f5040a92
AO
9655 if (ex.X_add_number != 0)
9656 {
9657 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9658 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9659 ex.X_op = O_constant;
67c0d1eb 9660 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9661 reg, reg, BFD_RELOC_LO16);
f5040a92 9662 }
252b5132
RH
9663 }
9664 }
0a44bf69 9665 else if (mips_big_got)
252b5132
RH
9666 {
9667 expressionS ex;
252b5132
RH
9668
9669 /* This is the large GOT case. If this is a reference to an
9670 external symbol, we want
9671 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9672 addu $reg,$reg,$gp
9673 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9674
9675 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9676 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9677 nop
9678 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9679 If there is a constant, it must be added in after.
f5040a92
AO
9680
9681 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9682 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9683 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9684 */
438c16b8
TS
9685 if (HAVE_NEWABI)
9686 {
4d7206a2 9687 ex.X_add_number = ep->X_add_number;
f5040a92 9688 ep->X_add_number = 0;
4d7206a2 9689 relax_start (ep->X_add_symbol);
df58fc94 9690 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9691 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9692 reg, reg, mips_gp_register);
9693 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9694 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9695 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9696 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9697 else if (ex.X_add_number)
9698 {
9699 ex.X_op = O_constant;
67c0d1eb
RS
9700 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9701 BFD_RELOC_LO16);
f5040a92
AO
9702 }
9703
9704 ep->X_add_number = ex.X_add_number;
4d7206a2 9705 relax_switch ();
67c0d1eb 9706 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9707 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9708 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9709 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9710 relax_end ();
438c16b8 9711 }
252b5132 9712 else
438c16b8 9713 {
f5040a92
AO
9714 ex.X_add_number = ep->X_add_number;
9715 ep->X_add_number = 0;
4d7206a2 9716 relax_start (ep->X_add_symbol);
df58fc94 9717 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9718 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9719 reg, reg, mips_gp_register);
9720 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9721 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9722 relax_switch ();
9723 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9724 {
9725 /* We need a nop before loading from $gp. This special
9726 check is required because the lui which starts the main
9727 instruction stream does not refer to $gp, and so will not
9728 insert the nop which may be required. */
67c0d1eb 9729 macro_build (NULL, "nop", "");
438c16b8 9730 }
67c0d1eb 9731 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9732 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9733 load_delay_nop ();
67c0d1eb 9734 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9735 BFD_RELOC_LO16);
4d7206a2 9736 relax_end ();
438c16b8 9737
f5040a92
AO
9738 if (ex.X_add_number != 0)
9739 {
9740 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9741 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9742 ex.X_op = O_constant;
67c0d1eb
RS
9743 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9744 BFD_RELOC_LO16);
f5040a92 9745 }
252b5132
RH
9746 }
9747 }
252b5132
RH
9748 else
9749 abort ();
8fc2e39e 9750
741fe287 9751 if (!mips_opts.at && *used_at == 1)
1661c76c 9752 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9753}
9754
ea1fb5dc
RS
9755/* Move the contents of register SOURCE into register DEST. */
9756
9757static void
67c0d1eb 9758move_register (int dest, int source)
ea1fb5dc 9759{
df58fc94
RS
9760 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9761 instruction specifically requires a 32-bit one. */
9762 if (mips_opts.micromips
833794fc 9763 && !mips_opts.insn32
df58fc94 9764 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9765 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9766 else
40fc1451 9767 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9768}
9769
4d7206a2 9770/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9771 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9772 The two alternatives are:
4d7206a2 9773
33eaf5de 9774 Global symbol Local symbol
4d7206a2
RS
9775 ------------- ------------
9776 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9777 ... ...
9778 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9779
9780 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9781 emits the second for a 16-bit offset or add_got_offset_hilo emits
9782 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9783
9784static void
67c0d1eb 9785load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9786{
9787 expressionS global;
9788
9789 global = *local;
9790 global.X_add_number = 0;
9791
9792 relax_start (local->X_add_symbol);
67c0d1eb
RS
9793 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9794 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9795 relax_switch ();
67c0d1eb
RS
9796 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9797 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9798 relax_end ();
9799}
9800
9801static void
67c0d1eb 9802add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9803{
9804 expressionS global;
9805
9806 global.X_op = O_constant;
9807 global.X_op_symbol = NULL;
9808 global.X_add_symbol = NULL;
9809 global.X_add_number = local->X_add_number;
9810
9811 relax_start (local->X_add_symbol);
67c0d1eb 9812 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9813 dest, dest, BFD_RELOC_LO16);
9814 relax_switch ();
67c0d1eb 9815 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9816 relax_end ();
9817}
9818
f6a22291
MR
9819static void
9820add_got_offset_hilo (int dest, expressionS *local, int tmp)
9821{
9822 expressionS global;
9823 int hold_mips_optimize;
9824
9825 global.X_op = O_constant;
9826 global.X_op_symbol = NULL;
9827 global.X_add_symbol = NULL;
9828 global.X_add_number = local->X_add_number;
9829
9830 relax_start (local->X_add_symbol);
9831 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9832 relax_switch ();
9833 /* Set mips_optimize around the lui instruction to avoid
9834 inserting an unnecessary nop after the lw. */
9835 hold_mips_optimize = mips_optimize;
9836 mips_optimize = 2;
9837 macro_build_lui (&global, tmp);
9838 mips_optimize = hold_mips_optimize;
9839 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9840 relax_end ();
9841
9842 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9843}
9844
df58fc94
RS
9845/* Emit a sequence of instructions to emulate a branch likely operation.
9846 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9847 is its complementing branch with the original condition negated.
9848 CALL is set if the original branch specified the link operation.
9849 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9850
9851 Code like this is produced in the noreorder mode:
9852
9853 BRNEG <args>, 1f
9854 nop
9855 b <sym>
9856 delay slot (executed only if branch taken)
9857 1:
9858
9859 or, if CALL is set:
9860
9861 BRNEG <args>, 1f
9862 nop
9863 bal <sym>
9864 delay slot (executed only if branch taken)
9865 1:
9866
9867 In the reorder mode the delay slot would be filled with a nop anyway,
9868 so code produced is simply:
9869
9870 BR <args>, <sym>
9871 nop
9872
9873 This function is used when producing code for the microMIPS ASE that
9874 does not implement branch likely instructions in hardware. */
9875
9876static void
9877macro_build_branch_likely (const char *br, const char *brneg,
9878 int call, expressionS *ep, const char *fmt,
9879 unsigned int sreg, unsigned int treg)
9880{
9881 int noreorder = mips_opts.noreorder;
9882 expressionS expr1;
9883
9884 gas_assert (mips_opts.micromips);
9885 start_noreorder ();
9886 if (noreorder)
9887 {
9888 micromips_label_expr (&expr1);
9889 macro_build (&expr1, brneg, fmt, sreg, treg);
9890 macro_build (NULL, "nop", "");
9891 macro_build (ep, call ? "bal" : "b", "p");
9892
9893 /* Set to true so that append_insn adds a label. */
9894 emit_branch_likely_macro = TRUE;
9895 }
9896 else
9897 {
9898 macro_build (ep, br, fmt, sreg, treg);
9899 macro_build (NULL, "nop", "");
9900 }
9901 end_noreorder ();
9902}
9903
9904/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9905 the condition code tested. EP specifies the branch target. */
9906
9907static void
9908macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9909{
9910 const int call = 0;
9911 const char *brneg;
9912 const char *br;
9913
9914 switch (type)
9915 {
9916 case M_BC1FL:
9917 br = "bc1f";
9918 brneg = "bc1t";
9919 break;
9920 case M_BC1TL:
9921 br = "bc1t";
9922 brneg = "bc1f";
9923 break;
9924 case M_BC2FL:
9925 br = "bc2f";
9926 brneg = "bc2t";
9927 break;
9928 case M_BC2TL:
9929 br = "bc2t";
9930 brneg = "bc2f";
9931 break;
9932 default:
9933 abort ();
9934 }
9935 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9936}
9937
9938/* Emit a two-argument branch macro specified by TYPE, using SREG as
9939 the register tested. EP specifies the branch target. */
9940
9941static void
9942macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9943{
9944 const char *brneg = NULL;
9945 const char *br;
9946 int call = 0;
9947
9948 switch (type)
9949 {
9950 case M_BGEZ:
9951 br = "bgez";
9952 break;
9953 case M_BGEZL:
9954 br = mips_opts.micromips ? "bgez" : "bgezl";
9955 brneg = "bltz";
9956 break;
9957 case M_BGEZALL:
9958 gas_assert (mips_opts.micromips);
833794fc 9959 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9960 brneg = "bltz";
9961 call = 1;
9962 break;
9963 case M_BGTZ:
9964 br = "bgtz";
9965 break;
9966 case M_BGTZL:
9967 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9968 brneg = "blez";
9969 break;
9970 case M_BLEZ:
9971 br = "blez";
9972 break;
9973 case M_BLEZL:
9974 br = mips_opts.micromips ? "blez" : "blezl";
9975 brneg = "bgtz";
9976 break;
9977 case M_BLTZ:
9978 br = "bltz";
9979 break;
9980 case M_BLTZL:
9981 br = mips_opts.micromips ? "bltz" : "bltzl";
9982 brneg = "bgez";
9983 break;
9984 case M_BLTZALL:
9985 gas_assert (mips_opts.micromips);
833794fc 9986 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9987 brneg = "bgez";
9988 call = 1;
9989 break;
9990 default:
9991 abort ();
9992 }
9993 if (mips_opts.micromips && brneg)
9994 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9995 else
9996 macro_build (ep, br, "s,p", sreg);
9997}
9998
9999/* Emit a three-argument branch macro specified by TYPE, using SREG and
10000 TREG as the registers tested. EP specifies the branch target. */
10001
10002static void
10003macro_build_branch_rsrt (int type, expressionS *ep,
10004 unsigned int sreg, unsigned int treg)
10005{
10006 const char *brneg = NULL;
10007 const int call = 0;
10008 const char *br;
10009
10010 switch (type)
10011 {
10012 case M_BEQ:
10013 case M_BEQ_I:
10014 br = "beq";
10015 break;
10016 case M_BEQL:
10017 case M_BEQL_I:
10018 br = mips_opts.micromips ? "beq" : "beql";
10019 brneg = "bne";
10020 break;
10021 case M_BNE:
10022 case M_BNE_I:
10023 br = "bne";
10024 break;
10025 case M_BNEL:
10026 case M_BNEL_I:
10027 br = mips_opts.micromips ? "bne" : "bnel";
10028 brneg = "beq";
10029 break;
10030 default:
10031 abort ();
10032 }
10033 if (mips_opts.micromips && brneg)
10034 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
10035 else
10036 macro_build (ep, br, "s,t,p", sreg, treg);
10037}
10038
f2ae14a1
RS
10039/* Return the high part that should be loaded in order to make the low
10040 part of VALUE accessible using an offset of OFFBITS bits. */
10041
10042static offsetT
10043offset_high_part (offsetT value, unsigned int offbits)
10044{
10045 offsetT bias;
10046 addressT low_mask;
10047
10048 if (offbits == 0)
10049 return value;
10050 bias = 1 << (offbits - 1);
10051 low_mask = bias * 2 - 1;
10052 return (value + bias) & ~low_mask;
10053}
10054
10055/* Return true if the value stored in offset_expr and offset_reloc
10056 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10057 amount that the caller wants to add without inducing overflow
10058 and ALIGN is the known alignment of the value in bytes. */
10059
10060static bfd_boolean
10061small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10062{
10063 if (offbits == 16)
10064 {
10065 /* Accept any relocation operator if overflow isn't a concern. */
10066 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10067 return TRUE;
10068
10069 /* These relocations are guaranteed not to overflow in correct links. */
10070 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10071 || gprel16_reloc_p (*offset_reloc))
10072 return TRUE;
10073 }
10074 if (offset_expr.X_op == O_constant
10075 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10076 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10077 return TRUE;
10078 return FALSE;
10079}
10080
252b5132
RH
10081/*
10082 * Build macros
10083 * This routine implements the seemingly endless macro or synthesized
10084 * instructions and addressing modes in the mips assembly language. Many
10085 * of these macros are simple and are similar to each other. These could
67c1ffbe 10086 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10087 * this verbose method. Others are not simple macros but are more like
10088 * optimizing code generation.
10089 * One interesting optimization is when several store macros appear
67c1ffbe 10090 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10091 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10092 * of global optimization. We currently only optimize within a single macro.
10093 * For many of the load and store macros if the address is specified as a
10094 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10095 * first load register 'at' with zero and use it as the base register. The
10096 * mips assembler simply uses register $zero. Just one tiny optimization
10097 * we're missing.
10098 */
10099static void
833794fc 10100macro (struct mips_cl_insn *ip, char *str)
252b5132 10101{
c0ebe874
RS
10102 const struct mips_operand_array *operands;
10103 unsigned int breg, i;
741fe287 10104 unsigned int tempreg;
252b5132 10105 int mask;
43841e91 10106 int used_at = 0;
df58fc94 10107 expressionS label_expr;
252b5132 10108 expressionS expr1;
df58fc94 10109 expressionS *ep;
252b5132
RH
10110 const char *s;
10111 const char *s2;
10112 const char *fmt;
10113 int likely = 0;
252b5132 10114 int coproc = 0;
7f3c4072 10115 int offbits = 16;
1abe91b1 10116 int call = 0;
df58fc94
RS
10117 int jals = 0;
10118 int dbl = 0;
10119 int imm = 0;
10120 int ust = 0;
10121 int lp = 0;
f2ae14a1 10122 bfd_boolean large_offset;
252b5132 10123 int off;
252b5132 10124 int hold_mips_optimize;
f2ae14a1 10125 unsigned int align;
c0ebe874 10126 unsigned int op[MAX_OPERANDS];
252b5132 10127
9c2799c2 10128 gas_assert (! mips_opts.mips16);
252b5132 10129
c0ebe874
RS
10130 operands = insn_operands (ip);
10131 for (i = 0; i < MAX_OPERANDS; i++)
10132 if (operands->operand[i])
10133 op[i] = insn_extract_operand (ip, operands->operand[i]);
10134 else
10135 op[i] = -1;
10136
252b5132
RH
10137 mask = ip->insn_mo->mask;
10138
df58fc94
RS
10139 label_expr.X_op = O_constant;
10140 label_expr.X_op_symbol = NULL;
10141 label_expr.X_add_symbol = NULL;
10142 label_expr.X_add_number = 0;
10143
252b5132
RH
10144 expr1.X_op = O_constant;
10145 expr1.X_op_symbol = NULL;
10146 expr1.X_add_symbol = NULL;
10147 expr1.X_add_number = 1;
f2ae14a1 10148 align = 1;
252b5132
RH
10149
10150 switch (mask)
10151 {
10152 case M_DABS:
10153 dbl = 1;
1a0670f3 10154 /* Fall through. */
252b5132 10155 case M_ABS:
df58fc94
RS
10156 /* bgez $a0,1f
10157 move v0,$a0
10158 sub v0,$zero,$a0
10159 1:
10160 */
252b5132 10161
7d10b47d 10162 start_noreorder ();
252b5132 10163
df58fc94
RS
10164 if (mips_opts.micromips)
10165 micromips_label_expr (&label_expr);
10166 else
10167 label_expr.X_add_number = 8;
c0ebe874
RS
10168 macro_build (&label_expr, "bgez", "s,p", op[1]);
10169 if (op[0] == op[1])
a605d2b3 10170 macro_build (NULL, "nop", "");
252b5132 10171 else
c0ebe874
RS
10172 move_register (op[0], op[1]);
10173 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10174 if (mips_opts.micromips)
10175 micromips_add_label ();
252b5132 10176
7d10b47d 10177 end_noreorder ();
8fc2e39e 10178 break;
252b5132
RH
10179
10180 case M_ADD_I:
10181 s = "addi";
10182 s2 = "add";
10183 goto do_addi;
10184 case M_ADDU_I:
10185 s = "addiu";
10186 s2 = "addu";
10187 goto do_addi;
10188 case M_DADD_I:
10189 dbl = 1;
10190 s = "daddi";
10191 s2 = "dadd";
df58fc94
RS
10192 if (!mips_opts.micromips)
10193 goto do_addi;
b0e6f033 10194 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10195 && imm_expr.X_add_number < 0x200)
10196 {
b0e6f033
RS
10197 macro_build (NULL, s, "t,r,.", op[0], op[1],
10198 (int) imm_expr.X_add_number);
df58fc94
RS
10199 break;
10200 }
10201 goto do_addi_i;
252b5132
RH
10202 case M_DADDU_I:
10203 dbl = 1;
10204 s = "daddiu";
10205 s2 = "daddu";
10206 do_addi:
b0e6f033 10207 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10208 && imm_expr.X_add_number < 0x8000)
10209 {
c0ebe874 10210 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10211 break;
252b5132 10212 }
df58fc94 10213 do_addi_i:
8fc2e39e 10214 used_at = 1;
67c0d1eb 10215 load_register (AT, &imm_expr, dbl);
c0ebe874 10216 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10217 break;
10218
10219 case M_AND_I:
10220 s = "andi";
10221 s2 = "and";
10222 goto do_bit;
10223 case M_OR_I:
10224 s = "ori";
10225 s2 = "or";
10226 goto do_bit;
10227 case M_NOR_I:
10228 s = "";
10229 s2 = "nor";
10230 goto do_bit;
10231 case M_XOR_I:
10232 s = "xori";
10233 s2 = "xor";
10234 do_bit:
b0e6f033 10235 if (imm_expr.X_add_number >= 0
252b5132
RH
10236 && imm_expr.X_add_number < 0x10000)
10237 {
10238 if (mask != M_NOR_I)
c0ebe874 10239 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10240 else
10241 {
67c0d1eb 10242 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10243 op[0], op[1], BFD_RELOC_LO16);
10244 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10245 }
8fc2e39e 10246 break;
252b5132
RH
10247 }
10248
8fc2e39e 10249 used_at = 1;
bad1aba3 10250 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10251 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10252 break;
10253
8b082fb1
TS
10254 case M_BALIGN:
10255 switch (imm_expr.X_add_number)
10256 {
10257 case 0:
10258 macro_build (NULL, "nop", "");
10259 break;
10260 case 2:
c0ebe874 10261 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10262 break;
03f66e8a
MR
10263 case 1:
10264 case 3:
c0ebe874 10265 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10266 (int) imm_expr.X_add_number);
8b082fb1 10267 break;
03f66e8a
MR
10268 default:
10269 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10270 (unsigned long) imm_expr.X_add_number);
10271 break;
8b082fb1
TS
10272 }
10273 break;
10274
df58fc94
RS
10275 case M_BC1FL:
10276 case M_BC1TL:
10277 case M_BC2FL:
10278 case M_BC2TL:
10279 gas_assert (mips_opts.micromips);
10280 macro_build_branch_ccl (mask, &offset_expr,
10281 EXTRACT_OPERAND (1, BCC, *ip));
10282 break;
10283
252b5132 10284 case M_BEQ_I:
252b5132 10285 case M_BEQL_I:
252b5132 10286 case M_BNE_I:
252b5132 10287 case M_BNEL_I:
b0e6f033 10288 if (imm_expr.X_add_number == 0)
c0ebe874 10289 op[1] = 0;
df58fc94 10290 else
252b5132 10291 {
c0ebe874 10292 op[1] = AT;
df58fc94 10293 used_at = 1;
bad1aba3 10294 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10295 }
df58fc94
RS
10296 /* Fall through. */
10297 case M_BEQL:
10298 case M_BNEL:
c0ebe874 10299 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10300 break;
10301
10302 case M_BGEL:
10303 likely = 1;
1a0670f3 10304 /* Fall through. */
252b5132 10305 case M_BGE:
c0ebe874
RS
10306 if (op[1] == 0)
10307 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10308 else if (op[0] == 0)
10309 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10310 else
252b5132 10311 {
df58fc94 10312 used_at = 1;
c0ebe874 10313 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10314 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10315 &offset_expr, AT, ZERO);
252b5132 10316 }
df58fc94
RS
10317 break;
10318
10319 case M_BGEZL:
10320 case M_BGEZALL:
10321 case M_BGTZL:
10322 case M_BLEZL:
10323 case M_BLTZL:
10324 case M_BLTZALL:
c0ebe874 10325 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10326 break;
10327
10328 case M_BGTL_I:
10329 likely = 1;
1a0670f3 10330 /* Fall through. */
252b5132 10331 case M_BGT_I:
90ecf173 10332 /* Check for > max integer. */
b0e6f033 10333 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10334 {
10335 do_false:
90ecf173 10336 /* Result is always false. */
252b5132 10337 if (! likely)
a605d2b3 10338 macro_build (NULL, "nop", "");
252b5132 10339 else
df58fc94 10340 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10341 break;
252b5132 10342 }
f9419b05 10343 ++imm_expr.X_add_number;
252b5132
RH
10344 /* FALLTHROUGH */
10345 case M_BGE_I:
10346 case M_BGEL_I:
10347 if (mask == M_BGEL_I)
10348 likely = 1;
b0e6f033 10349 if (imm_expr.X_add_number == 0)
252b5132 10350 {
df58fc94 10351 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10352 &offset_expr, op[0]);
8fc2e39e 10353 break;
252b5132 10354 }
b0e6f033 10355 if (imm_expr.X_add_number == 1)
252b5132 10356 {
df58fc94 10357 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10358 &offset_expr, op[0]);
8fc2e39e 10359 break;
252b5132 10360 }
b0e6f033 10361 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10362 {
10363 do_true:
10364 /* result is always true */
1661c76c 10365 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10366 macro_build (&offset_expr, "b", "p");
8fc2e39e 10367 break;
252b5132 10368 }
8fc2e39e 10369 used_at = 1;
c0ebe874 10370 set_at (op[0], 0);
df58fc94
RS
10371 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10372 &offset_expr, AT, ZERO);
252b5132
RH
10373 break;
10374
10375 case M_BGEUL:
10376 likely = 1;
1a0670f3 10377 /* Fall through. */
252b5132 10378 case M_BGEU:
c0ebe874 10379 if (op[1] == 0)
252b5132 10380 goto do_true;
c0ebe874 10381 else if (op[0] == 0)
df58fc94 10382 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10383 &offset_expr, ZERO, op[1]);
df58fc94 10384 else
252b5132 10385 {
df58fc94 10386 used_at = 1;
c0ebe874 10387 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10388 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10389 &offset_expr, AT, ZERO);
252b5132 10390 }
252b5132
RH
10391 break;
10392
10393 case M_BGTUL_I:
10394 likely = 1;
1a0670f3 10395 /* Fall through. */
252b5132 10396 case M_BGTU_I:
c0ebe874 10397 if (op[0] == 0
bad1aba3 10398 || (GPR_SIZE == 32
f01dc953 10399 && imm_expr.X_add_number == -1))
252b5132 10400 goto do_false;
f9419b05 10401 ++imm_expr.X_add_number;
252b5132
RH
10402 /* FALLTHROUGH */
10403 case M_BGEU_I:
10404 case M_BGEUL_I:
10405 if (mask == M_BGEUL_I)
10406 likely = 1;
b0e6f033 10407 if (imm_expr.X_add_number == 0)
252b5132 10408 goto do_true;
b0e6f033 10409 else if (imm_expr.X_add_number == 1)
df58fc94 10410 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10411 &offset_expr, op[0], ZERO);
df58fc94 10412 else
252b5132 10413 {
df58fc94 10414 used_at = 1;
c0ebe874 10415 set_at (op[0], 1);
df58fc94
RS
10416 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10417 &offset_expr, AT, ZERO);
252b5132 10418 }
252b5132
RH
10419 break;
10420
10421 case M_BGTL:
10422 likely = 1;
1a0670f3 10423 /* Fall through. */
252b5132 10424 case M_BGT:
c0ebe874
RS
10425 if (op[1] == 0)
10426 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10427 else if (op[0] == 0)
10428 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10429 else
252b5132 10430 {
df58fc94 10431 used_at = 1;
c0ebe874 10432 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10433 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10434 &offset_expr, AT, ZERO);
252b5132 10435 }
252b5132
RH
10436 break;
10437
10438 case M_BGTUL:
10439 likely = 1;
1a0670f3 10440 /* Fall through. */
252b5132 10441 case M_BGTU:
c0ebe874 10442 if (op[1] == 0)
df58fc94 10443 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10444 &offset_expr, op[0], ZERO);
10445 else if (op[0] == 0)
df58fc94
RS
10446 goto do_false;
10447 else
252b5132 10448 {
df58fc94 10449 used_at = 1;
c0ebe874 10450 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10451 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10452 &offset_expr, AT, ZERO);
252b5132 10453 }
252b5132
RH
10454 break;
10455
10456 case M_BLEL:
10457 likely = 1;
1a0670f3 10458 /* Fall through. */
252b5132 10459 case M_BLE:
c0ebe874
RS
10460 if (op[1] == 0)
10461 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10462 else if (op[0] == 0)
10463 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10464 else
252b5132 10465 {
df58fc94 10466 used_at = 1;
c0ebe874 10467 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10468 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10469 &offset_expr, AT, ZERO);
252b5132 10470 }
252b5132
RH
10471 break;
10472
10473 case M_BLEL_I:
10474 likely = 1;
1a0670f3 10475 /* Fall through. */
252b5132 10476 case M_BLE_I:
b0e6f033 10477 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10478 goto do_true;
f9419b05 10479 ++imm_expr.X_add_number;
252b5132
RH
10480 /* FALLTHROUGH */
10481 case M_BLT_I:
10482 case M_BLTL_I:
10483 if (mask == M_BLTL_I)
10484 likely = 1;
b0e6f033 10485 if (imm_expr.X_add_number == 0)
c0ebe874 10486 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10487 else if (imm_expr.X_add_number == 1)
c0ebe874 10488 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10489 else
252b5132 10490 {
df58fc94 10491 used_at = 1;
c0ebe874 10492 set_at (op[0], 0);
df58fc94
RS
10493 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10494 &offset_expr, AT, ZERO);
252b5132 10495 }
252b5132
RH
10496 break;
10497
10498 case M_BLEUL:
10499 likely = 1;
1a0670f3 10500 /* Fall through. */
252b5132 10501 case M_BLEU:
c0ebe874 10502 if (op[1] == 0)
df58fc94 10503 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10504 &offset_expr, op[0], ZERO);
10505 else if (op[0] == 0)
df58fc94
RS
10506 goto do_true;
10507 else
252b5132 10508 {
df58fc94 10509 used_at = 1;
c0ebe874 10510 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10511 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10512 &offset_expr, AT, ZERO);
252b5132 10513 }
252b5132
RH
10514 break;
10515
10516 case M_BLEUL_I:
10517 likely = 1;
1a0670f3 10518 /* Fall through. */
252b5132 10519 case M_BLEU_I:
c0ebe874 10520 if (op[0] == 0
bad1aba3 10521 || (GPR_SIZE == 32
f01dc953 10522 && imm_expr.X_add_number == -1))
252b5132 10523 goto do_true;
f9419b05 10524 ++imm_expr.X_add_number;
252b5132
RH
10525 /* FALLTHROUGH */
10526 case M_BLTU_I:
10527 case M_BLTUL_I:
10528 if (mask == M_BLTUL_I)
10529 likely = 1;
b0e6f033 10530 if (imm_expr.X_add_number == 0)
252b5132 10531 goto do_false;
b0e6f033 10532 else if (imm_expr.X_add_number == 1)
df58fc94 10533 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10534 &offset_expr, op[0], ZERO);
df58fc94 10535 else
252b5132 10536 {
df58fc94 10537 used_at = 1;
c0ebe874 10538 set_at (op[0], 1);
df58fc94
RS
10539 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10540 &offset_expr, AT, ZERO);
252b5132 10541 }
252b5132
RH
10542 break;
10543
10544 case M_BLTL:
10545 likely = 1;
1a0670f3 10546 /* Fall through. */
252b5132 10547 case M_BLT:
c0ebe874
RS
10548 if (op[1] == 0)
10549 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10550 else if (op[0] == 0)
10551 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10552 else
252b5132 10553 {
df58fc94 10554 used_at = 1;
c0ebe874 10555 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10556 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10557 &offset_expr, AT, ZERO);
252b5132 10558 }
252b5132
RH
10559 break;
10560
10561 case M_BLTUL:
10562 likely = 1;
1a0670f3 10563 /* Fall through. */
252b5132 10564 case M_BLTU:
c0ebe874 10565 if (op[1] == 0)
252b5132 10566 goto do_false;
c0ebe874 10567 else if (op[0] == 0)
df58fc94 10568 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10569 &offset_expr, ZERO, op[1]);
df58fc94 10570 else
252b5132 10571 {
df58fc94 10572 used_at = 1;
c0ebe874 10573 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10574 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10575 &offset_expr, AT, ZERO);
252b5132 10576 }
252b5132
RH
10577 break;
10578
10579 case M_DDIV_3:
10580 dbl = 1;
1a0670f3 10581 /* Fall through. */
252b5132
RH
10582 case M_DIV_3:
10583 s = "mflo";
10584 goto do_div3;
10585 case M_DREM_3:
10586 dbl = 1;
1a0670f3 10587 /* Fall through. */
252b5132
RH
10588 case M_REM_3:
10589 s = "mfhi";
10590 do_div3:
c0ebe874 10591 if (op[2] == 0)
252b5132 10592 {
1661c76c 10593 as_warn (_("divide by zero"));
252b5132 10594 if (mips_trap)
df58fc94 10595 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10596 else
df58fc94 10597 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10598 break;
252b5132
RH
10599 }
10600
7d10b47d 10601 start_noreorder ();
252b5132
RH
10602 if (mips_trap)
10603 {
c0ebe874
RS
10604 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10605 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10606 }
10607 else
10608 {
df58fc94
RS
10609 if (mips_opts.micromips)
10610 micromips_label_expr (&label_expr);
10611 else
10612 label_expr.X_add_number = 8;
c0ebe874
RS
10613 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10614 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10615 macro_build (NULL, "break", BRK_FMT, 7);
10616 if (mips_opts.micromips)
10617 micromips_add_label ();
252b5132
RH
10618 }
10619 expr1.X_add_number = -1;
8fc2e39e 10620 used_at = 1;
f6a22291 10621 load_register (AT, &expr1, dbl);
df58fc94
RS
10622 if (mips_opts.micromips)
10623 micromips_label_expr (&label_expr);
10624 else
10625 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10626 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10627 if (dbl)
10628 {
10629 expr1.X_add_number = 1;
f6a22291 10630 load_register (AT, &expr1, dbl);
df58fc94 10631 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10632 }
10633 else
10634 {
10635 expr1.X_add_number = 0x80000000;
df58fc94 10636 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10637 }
10638 if (mips_trap)
10639 {
c0ebe874 10640 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10641 /* We want to close the noreorder block as soon as possible, so
10642 that later insns are available for delay slot filling. */
7d10b47d 10643 end_noreorder ();
252b5132
RH
10644 }
10645 else
10646 {
df58fc94
RS
10647 if (mips_opts.micromips)
10648 micromips_label_expr (&label_expr);
10649 else
10650 label_expr.X_add_number = 8;
c0ebe874 10651 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10652 macro_build (NULL, "nop", "");
252b5132
RH
10653
10654 /* We want to close the noreorder block as soon as possible, so
10655 that later insns are available for delay slot filling. */
7d10b47d 10656 end_noreorder ();
252b5132 10657
df58fc94 10658 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10659 }
df58fc94
RS
10660 if (mips_opts.micromips)
10661 micromips_add_label ();
c0ebe874 10662 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10663 break;
10664
10665 case M_DIV_3I:
10666 s = "div";
10667 s2 = "mflo";
10668 goto do_divi;
10669 case M_DIVU_3I:
10670 s = "divu";
10671 s2 = "mflo";
10672 goto do_divi;
10673 case M_REM_3I:
10674 s = "div";
10675 s2 = "mfhi";
10676 goto do_divi;
10677 case M_REMU_3I:
10678 s = "divu";
10679 s2 = "mfhi";
10680 goto do_divi;
10681 case M_DDIV_3I:
10682 dbl = 1;
10683 s = "ddiv";
10684 s2 = "mflo";
10685 goto do_divi;
10686 case M_DDIVU_3I:
10687 dbl = 1;
10688 s = "ddivu";
10689 s2 = "mflo";
10690 goto do_divi;
10691 case M_DREM_3I:
10692 dbl = 1;
10693 s = "ddiv";
10694 s2 = "mfhi";
10695 goto do_divi;
10696 case M_DREMU_3I:
10697 dbl = 1;
10698 s = "ddivu";
10699 s2 = "mfhi";
10700 do_divi:
b0e6f033 10701 if (imm_expr.X_add_number == 0)
252b5132 10702 {
1661c76c 10703 as_warn (_("divide by zero"));
252b5132 10704 if (mips_trap)
df58fc94 10705 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10706 else
df58fc94 10707 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10708 break;
252b5132 10709 }
b0e6f033 10710 if (imm_expr.X_add_number == 1)
252b5132
RH
10711 {
10712 if (strcmp (s2, "mflo") == 0)
c0ebe874 10713 move_register (op[0], op[1]);
252b5132 10714 else
c0ebe874 10715 move_register (op[0], ZERO);
8fc2e39e 10716 break;
252b5132 10717 }
b0e6f033 10718 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10719 {
10720 if (strcmp (s2, "mflo") == 0)
c0ebe874 10721 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10722 else
c0ebe874 10723 move_register (op[0], ZERO);
8fc2e39e 10724 break;
252b5132
RH
10725 }
10726
8fc2e39e 10727 used_at = 1;
67c0d1eb 10728 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10729 macro_build (NULL, s, "z,s,t", op[1], AT);
10730 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10731 break;
10732
10733 case M_DIVU_3:
10734 s = "divu";
10735 s2 = "mflo";
10736 goto do_divu3;
10737 case M_REMU_3:
10738 s = "divu";
10739 s2 = "mfhi";
10740 goto do_divu3;
10741 case M_DDIVU_3:
10742 s = "ddivu";
10743 s2 = "mflo";
10744 goto do_divu3;
10745 case M_DREMU_3:
10746 s = "ddivu";
10747 s2 = "mfhi";
10748 do_divu3:
7d10b47d 10749 start_noreorder ();
252b5132
RH
10750 if (mips_trap)
10751 {
c0ebe874
RS
10752 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10753 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10754 /* We want to close the noreorder block as soon as possible, so
10755 that later insns are available for delay slot filling. */
7d10b47d 10756 end_noreorder ();
252b5132
RH
10757 }
10758 else
10759 {
df58fc94
RS
10760 if (mips_opts.micromips)
10761 micromips_label_expr (&label_expr);
10762 else
10763 label_expr.X_add_number = 8;
c0ebe874
RS
10764 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10765 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10766
10767 /* We want to close the noreorder block as soon as possible, so
10768 that later insns are available for delay slot filling. */
7d10b47d 10769 end_noreorder ();
df58fc94
RS
10770 macro_build (NULL, "break", BRK_FMT, 7);
10771 if (mips_opts.micromips)
10772 micromips_add_label ();
252b5132 10773 }
c0ebe874 10774 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10775 break;
252b5132 10776
1abe91b1
MR
10777 case M_DLCA_AB:
10778 dbl = 1;
1a0670f3 10779 /* Fall through. */
1abe91b1
MR
10780 case M_LCA_AB:
10781 call = 1;
10782 goto do_la;
252b5132
RH
10783 case M_DLA_AB:
10784 dbl = 1;
1a0670f3 10785 /* Fall through. */
252b5132 10786 case M_LA_AB:
1abe91b1 10787 do_la:
252b5132
RH
10788 /* Load the address of a symbol into a register. If breg is not
10789 zero, we then add a base register to it. */
10790
c0ebe874 10791 breg = op[2];
bad1aba3 10792 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10793 as_warn (_("dla used to load 32-bit register; recommend using la "
10794 "instead"));
3bec30a8 10795
90ecf173 10796 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10797 as_warn (_("la used to load 64-bit address; recommend using dla "
10798 "instead"));
3bec30a8 10799
f2ae14a1 10800 if (small_offset_p (0, align, 16))
0c11417f 10801 {
c0ebe874 10802 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10803 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10804 break;
0c11417f
MR
10805 }
10806
c0ebe874 10807 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10808 {
10809 tempreg = AT;
10810 used_at = 1;
10811 }
10812 else
c0ebe874 10813 tempreg = op[0];
afdbd6d0 10814
252b5132
RH
10815 if (offset_expr.X_op != O_symbol
10816 && offset_expr.X_op != O_constant)
10817 {
1661c76c 10818 as_bad (_("expression too complex"));
252b5132
RH
10819 offset_expr.X_op = O_constant;
10820 }
10821
252b5132 10822 if (offset_expr.X_op == O_constant)
aed1a261 10823 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10824 else if (mips_pic == NO_PIC)
10825 {
d6bc6245 10826 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10827 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10828 Otherwise we want
10829 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10830 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10831 If we have a constant, we need two instructions anyhow,
d6bc6245 10832 so we may as well always use the latter form.
76b3015f 10833
6caf9ef4
TS
10834 With 64bit address space and a usable $at we want
10835 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10836 lui $at,<sym> (BFD_RELOC_HI16_S)
10837 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10838 daddiu $at,<sym> (BFD_RELOC_LO16)
10839 dsll32 $tempreg,0
10840 daddu $tempreg,$tempreg,$at
10841
10842 If $at is already in use, we use a path which is suboptimal
10843 on superscalar processors.
10844 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10845 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10846 dsll $tempreg,16
10847 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10848 dsll $tempreg,16
10849 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10850
10851 For GP relative symbols in 64bit address space we can use
10852 the same sequence as in 32bit address space. */
aed1a261 10853 if (HAVE_64BIT_SYMBOLS)
252b5132 10854 {
6caf9ef4
TS
10855 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10856 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10857 {
10858 relax_start (offset_expr.X_add_symbol);
10859 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10860 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10861 relax_switch ();
10862 }
d6bc6245 10863
741fe287 10864 if (used_at == 0 && mips_opts.at)
98d3f06f 10865 {
df58fc94 10866 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10867 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10868 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10869 AT, BFD_RELOC_HI16_S);
67c0d1eb 10870 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10871 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10872 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10873 AT, AT, BFD_RELOC_LO16);
df58fc94 10874 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10875 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10876 used_at = 1;
10877 }
10878 else
10879 {
df58fc94 10880 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10881 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10882 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10883 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10884 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10885 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10886 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10887 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10888 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10889 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10890 }
6caf9ef4
TS
10891
10892 if (mips_relax.sequence)
10893 relax_end ();
98d3f06f
KH
10894 }
10895 else
10896 {
10897 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10898 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10899 {
4d7206a2 10900 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10901 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10902 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10903 relax_switch ();
98d3f06f 10904 }
6943caf0 10905 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10906 as_bad (_("offset too large"));
67c0d1eb
RS
10907 macro_build_lui (&offset_expr, tempreg);
10908 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10909 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10910 if (mips_relax.sequence)
10911 relax_end ();
98d3f06f 10912 }
252b5132 10913 }
0a44bf69 10914 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10915 {
9117d219
NC
10916 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10917
252b5132
RH
10918 /* If this is a reference to an external symbol, and there
10919 is no constant, we want
10920 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10921 or for lca or if tempreg is PIC_CALL_REG
9117d219 10922 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10923 For a local symbol, we want
10924 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10925 nop
10926 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10927
10928 If we have a small constant, and this is a reference to
10929 an external symbol, we want
10930 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10931 nop
10932 addiu $tempreg,$tempreg,<constant>
10933 For a local symbol, we want the same instruction
10934 sequence, but we output a BFD_RELOC_LO16 reloc on the
10935 addiu instruction.
10936
10937 If we have a large constant, and this is a reference to
10938 an external symbol, we want
10939 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10940 lui $at,<hiconstant>
10941 addiu $at,$at,<loconstant>
10942 addu $tempreg,$tempreg,$at
10943 For a local symbol, we want the same instruction
10944 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10945 addiu instruction.
ed6fb7bd
SC
10946 */
10947
4d7206a2 10948 if (offset_expr.X_add_number == 0)
252b5132 10949 {
0a44bf69
RS
10950 if (mips_pic == SVR4_PIC
10951 && breg == 0
10952 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10953 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10954
10955 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10956 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10957 lw_reloc_type, mips_gp_register);
4d7206a2 10958 if (breg != 0)
252b5132
RH
10959 {
10960 /* We're going to put in an addu instruction using
10961 tempreg, so we may as well insert the nop right
10962 now. */
269137b2 10963 load_delay_nop ();
252b5132 10964 }
4d7206a2 10965 relax_switch ();
67c0d1eb
RS
10966 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10967 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10968 load_delay_nop ();
67c0d1eb
RS
10969 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10970 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10971 relax_end ();
252b5132
RH
10972 /* FIXME: If breg == 0, and the next instruction uses
10973 $tempreg, then if this variant case is used an extra
10974 nop will be generated. */
10975 }
4d7206a2
RS
10976 else if (offset_expr.X_add_number >= -0x8000
10977 && offset_expr.X_add_number < 0x8000)
252b5132 10978 {
67c0d1eb 10979 load_got_offset (tempreg, &offset_expr);
269137b2 10980 load_delay_nop ();
67c0d1eb 10981 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10982 }
10983 else
10984 {
4d7206a2
RS
10985 expr1.X_add_number = offset_expr.X_add_number;
10986 offset_expr.X_add_number =
43c0598f 10987 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10988 load_got_offset (tempreg, &offset_expr);
f6a22291 10989 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10990 /* If we are going to add in a base register, and the
10991 target register and the base register are the same,
10992 then we are using AT as a temporary register. Since
10993 we want to load the constant into AT, we add our
10994 current AT (from the global offset table) and the
10995 register into the register now, and pretend we were
10996 not using a base register. */
c0ebe874 10997 if (breg == op[0])
252b5132 10998 {
269137b2 10999 load_delay_nop ();
67c0d1eb 11000 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11001 op[0], AT, breg);
252b5132 11002 breg = 0;
c0ebe874 11003 tempreg = op[0];
252b5132 11004 }
f6a22291 11005 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
11006 used_at = 1;
11007 }
11008 }
0a44bf69 11009 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 11010 {
67c0d1eb 11011 int add_breg_early = 0;
f5040a92
AO
11012
11013 /* If this is a reference to an external, and there is no
11014 constant, or local symbol (*), with or without a
11015 constant, we want
11016 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 11017 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11018 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11019
11020 If we have a small constant, and this is a reference to
11021 an external symbol, we want
11022 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11023 addiu $tempreg,$tempreg,<constant>
11024
11025 If we have a large constant, and this is a reference to
11026 an external symbol, we want
11027 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
11028 lui $at,<hiconstant>
11029 addiu $at,$at,<loconstant>
11030 addu $tempreg,$tempreg,$at
11031
11032 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
11033 local symbols, even though it introduces an additional
11034 instruction. */
11035
f5040a92
AO
11036 if (offset_expr.X_add_number)
11037 {
4d7206a2 11038 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11039 offset_expr.X_add_number = 0;
11040
4d7206a2 11041 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11042 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11043 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11044
11045 if (expr1.X_add_number >= -0x8000
11046 && expr1.X_add_number < 0x8000)
11047 {
67c0d1eb
RS
11048 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11049 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11050 }
ecd13cd3 11051 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11052 {
c0ebe874
RS
11053 unsigned int dreg;
11054
f5040a92
AO
11055 /* If we are going to add in a base register, and the
11056 target register and the base register are the same,
11057 then we are using AT as a temporary register. Since
11058 we want to load the constant into AT, we add our
11059 current AT (from the global offset table) and the
11060 register into the register now, and pretend we were
11061 not using a base register. */
c0ebe874 11062 if (breg != op[0])
f5040a92
AO
11063 dreg = tempreg;
11064 else
11065 {
9c2799c2 11066 gas_assert (tempreg == AT);
67c0d1eb 11067 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11068 op[0], AT, breg);
11069 dreg = op[0];
67c0d1eb 11070 add_breg_early = 1;
f5040a92
AO
11071 }
11072
f6a22291 11073 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11074 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11075 dreg, dreg, AT);
f5040a92 11076
f5040a92
AO
11077 used_at = 1;
11078 }
11079 else
11080 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11081
4d7206a2 11082 relax_switch ();
f5040a92
AO
11083 offset_expr.X_add_number = expr1.X_add_number;
11084
67c0d1eb
RS
11085 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11086 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11087 if (add_breg_early)
f5040a92 11088 {
67c0d1eb 11089 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11090 op[0], tempreg, breg);
f5040a92 11091 breg = 0;
c0ebe874 11092 tempreg = op[0];
f5040a92 11093 }
4d7206a2 11094 relax_end ();
f5040a92 11095 }
4d7206a2 11096 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11097 {
4d7206a2 11098 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11099 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11100 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11101 relax_switch ();
67c0d1eb
RS
11102 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11103 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11104 relax_end ();
f5040a92 11105 }
4d7206a2 11106 else
f5040a92 11107 {
67c0d1eb
RS
11108 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11109 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11110 }
11111 }
0a44bf69 11112 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11113 {
67c0d1eb 11114 int gpdelay;
9117d219
NC
11115 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11116 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11117 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11118
11119 /* This is the large GOT case. If this is a reference to an
11120 external symbol, and there is no constant, we want
11121 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11122 addu $tempreg,$tempreg,$gp
11123 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11124 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11125 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11126 addu $tempreg,$tempreg,$gp
11127 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11128 For a local symbol, we want
11129 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11130 nop
11131 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11132
11133 If we have a small constant, and this is a reference to
11134 an external symbol, we want
11135 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11136 addu $tempreg,$tempreg,$gp
11137 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11138 nop
11139 addiu $tempreg,$tempreg,<constant>
11140 For a local symbol, we want
11141 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11142 nop
11143 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11144
11145 If we have a large constant, and this is a reference to
11146 an external symbol, we want
11147 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11148 addu $tempreg,$tempreg,$gp
11149 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11150 lui $at,<hiconstant>
11151 addiu $at,$at,<loconstant>
11152 addu $tempreg,$tempreg,$at
11153 For a local symbol, we want
11154 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11155 lui $at,<hiconstant>
11156 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11157 addu $tempreg,$tempreg,$at
f5040a92 11158 */
438c16b8 11159
252b5132
RH
11160 expr1.X_add_number = offset_expr.X_add_number;
11161 offset_expr.X_add_number = 0;
4d7206a2 11162 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11163 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11164 if (expr1.X_add_number == 0 && breg == 0
11165 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11166 {
11167 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11168 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11169 }
df58fc94 11170 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11171 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11172 tempreg, tempreg, mips_gp_register);
67c0d1eb 11173 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11174 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11175 if (expr1.X_add_number == 0)
11176 {
67c0d1eb 11177 if (breg != 0)
252b5132
RH
11178 {
11179 /* We're going to put in an addu instruction using
11180 tempreg, so we may as well insert the nop right
11181 now. */
269137b2 11182 load_delay_nop ();
252b5132 11183 }
252b5132
RH
11184 }
11185 else if (expr1.X_add_number >= -0x8000
11186 && expr1.X_add_number < 0x8000)
11187 {
269137b2 11188 load_delay_nop ();
67c0d1eb 11189 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11190 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11191 }
11192 else
11193 {
c0ebe874
RS
11194 unsigned int dreg;
11195
252b5132
RH
11196 /* If we are going to add in a base register, and the
11197 target register and the base register are the same,
11198 then we are using AT as a temporary register. Since
11199 we want to load the constant into AT, we add our
11200 current AT (from the global offset table) and the
11201 register into the register now, and pretend we were
11202 not using a base register. */
c0ebe874 11203 if (breg != op[0])
67c0d1eb 11204 dreg = tempreg;
252b5132
RH
11205 else
11206 {
9c2799c2 11207 gas_assert (tempreg == AT);
269137b2 11208 load_delay_nop ();
67c0d1eb 11209 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11210 op[0], AT, breg);
11211 dreg = op[0];
252b5132
RH
11212 }
11213
f6a22291 11214 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11215 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11216
252b5132
RH
11217 used_at = 1;
11218 }
43c0598f 11219 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11220 relax_switch ();
252b5132 11221
67c0d1eb 11222 if (gpdelay)
252b5132
RH
11223 {
11224 /* This is needed because this instruction uses $gp, but
f5040a92 11225 the first instruction on the main stream does not. */
67c0d1eb 11226 macro_build (NULL, "nop", "");
252b5132 11227 }
ed6fb7bd 11228
67c0d1eb
RS
11229 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11230 local_reloc_type, mips_gp_register);
f5040a92 11231 if (expr1.X_add_number >= -0x8000
252b5132
RH
11232 && expr1.X_add_number < 0x8000)
11233 {
269137b2 11234 load_delay_nop ();
67c0d1eb
RS
11235 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11236 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11237 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11238 register, the external symbol case ended with a load,
11239 so if the symbol turns out to not be external, and
11240 the next instruction uses tempreg, an unnecessary nop
11241 will be inserted. */
252b5132
RH
11242 }
11243 else
11244 {
c0ebe874 11245 if (breg == op[0])
252b5132
RH
11246 {
11247 /* We must add in the base register now, as in the
f5040a92 11248 external symbol case. */
9c2799c2 11249 gas_assert (tempreg == AT);
269137b2 11250 load_delay_nop ();
67c0d1eb 11251 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11252 op[0], AT, breg);
11253 tempreg = op[0];
252b5132 11254 /* We set breg to 0 because we have arranged to add
f5040a92 11255 it in in both cases. */
252b5132
RH
11256 breg = 0;
11257 }
11258
67c0d1eb
RS
11259 macro_build_lui (&expr1, AT);
11260 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11261 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11262 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11263 tempreg, tempreg, AT);
8fc2e39e 11264 used_at = 1;
252b5132 11265 }
4d7206a2 11266 relax_end ();
252b5132 11267 }
0a44bf69 11268 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11269 {
f5040a92
AO
11270 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11271 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11272 int add_breg_early = 0;
f5040a92
AO
11273
11274 /* This is the large GOT case. If this is a reference to an
11275 external symbol, and there is no constant, we want
11276 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11277 add $tempreg,$tempreg,$gp
11278 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11279 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11280 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11281 add $tempreg,$tempreg,$gp
11282 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11283
11284 If we have a small constant, and this is a reference to
11285 an external symbol, we want
11286 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11287 add $tempreg,$tempreg,$gp
11288 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11289 addi $tempreg,$tempreg,<constant>
11290
11291 If we have a large constant, and this is a reference to
11292 an external symbol, we want
11293 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11294 addu $tempreg,$tempreg,$gp
11295 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11296 lui $at,<hiconstant>
11297 addi $at,$at,<loconstant>
11298 add $tempreg,$tempreg,$at
11299
11300 If we have NewABI, and we know it's a local symbol, we want
11301 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11302 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11303 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11304
4d7206a2 11305 relax_start (offset_expr.X_add_symbol);
f5040a92 11306
4d7206a2 11307 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11308 offset_expr.X_add_number = 0;
11309
1abe91b1
MR
11310 if (expr1.X_add_number == 0 && breg == 0
11311 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11312 {
11313 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11314 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11315 }
df58fc94 11316 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11317 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11318 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11319 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11320 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11321
11322 if (expr1.X_add_number == 0)
4d7206a2 11323 ;
f5040a92
AO
11324 else if (expr1.X_add_number >= -0x8000
11325 && expr1.X_add_number < 0x8000)
11326 {
67c0d1eb 11327 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11328 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11329 }
ecd13cd3 11330 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11331 {
c0ebe874
RS
11332 unsigned int dreg;
11333
f5040a92
AO
11334 /* If we are going to add in a base register, and the
11335 target register and the base register are the same,
11336 then we are using AT as a temporary register. Since
11337 we want to load the constant into AT, we add our
11338 current AT (from the global offset table) and the
11339 register into the register now, and pretend we were
11340 not using a base register. */
c0ebe874 11341 if (breg != op[0])
f5040a92
AO
11342 dreg = tempreg;
11343 else
11344 {
9c2799c2 11345 gas_assert (tempreg == AT);
67c0d1eb 11346 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11347 op[0], AT, breg);
11348 dreg = op[0];
67c0d1eb 11349 add_breg_early = 1;
f5040a92
AO
11350 }
11351
f6a22291 11352 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11353 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11354
f5040a92
AO
11355 used_at = 1;
11356 }
11357 else
11358 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11359
4d7206a2 11360 relax_switch ();
f5040a92 11361 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11362 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11363 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11364 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11365 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11366 if (add_breg_early)
f5040a92 11367 {
67c0d1eb 11368 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11369 op[0], tempreg, breg);
f5040a92 11370 breg = 0;
c0ebe874 11371 tempreg = op[0];
f5040a92 11372 }
4d7206a2 11373 relax_end ();
f5040a92 11374 }
252b5132
RH
11375 else
11376 abort ();
11377
11378 if (breg != 0)
c0ebe874 11379 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11380 break;
11381
52b6b6b9 11382 case M_MSGSND:
df58fc94 11383 gas_assert (!mips_opts.micromips);
c0ebe874 11384 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11385 break;
52b6b6b9
JM
11386
11387 case M_MSGLD:
df58fc94 11388 gas_assert (!mips_opts.micromips);
c8276761 11389 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11390 break;
52b6b6b9
JM
11391
11392 case M_MSGLD_T:
df58fc94 11393 gas_assert (!mips_opts.micromips);
c0ebe874 11394 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11395 break;
52b6b6b9
JM
11396
11397 case M_MSGWAIT:
df58fc94 11398 gas_assert (!mips_opts.micromips);
52b6b6b9 11399 macro_build (NULL, "c2", "C", 3);
c7af4273 11400 break;
52b6b6b9
JM
11401
11402 case M_MSGWAIT_T:
df58fc94 11403 gas_assert (!mips_opts.micromips);
c0ebe874 11404 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11405 break;
52b6b6b9 11406
252b5132
RH
11407 case M_J_A:
11408 /* The j instruction may not be used in PIC code, since it
11409 requires an absolute address. We convert it to a b
11410 instruction. */
11411 if (mips_pic == NO_PIC)
67c0d1eb 11412 macro_build (&offset_expr, "j", "a");
252b5132 11413 else
67c0d1eb 11414 macro_build (&offset_expr, "b", "p");
8fc2e39e 11415 break;
252b5132
RH
11416
11417 /* The jal instructions must be handled as macros because when
11418 generating PIC code they expand to multi-instruction
11419 sequences. Normally they are simple instructions. */
df58fc94 11420 case M_JALS_1:
c0ebe874
RS
11421 op[1] = op[0];
11422 op[0] = RA;
df58fc94
RS
11423 /* Fall through. */
11424 case M_JALS_2:
11425 gas_assert (mips_opts.micromips);
833794fc
MR
11426 if (mips_opts.insn32)
11427 {
1661c76c 11428 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11429 break;
11430 }
df58fc94
RS
11431 jals = 1;
11432 goto jal;
252b5132 11433 case M_JAL_1:
c0ebe874
RS
11434 op[1] = op[0];
11435 op[0] = RA;
252b5132
RH
11436 /* Fall through. */
11437 case M_JAL_2:
df58fc94 11438 jal:
3e722fb5 11439 if (mips_pic == NO_PIC)
df58fc94
RS
11440 {
11441 s = jals ? "jalrs" : "jalr";
e64af278 11442 if (mips_opts.micromips
833794fc 11443 && !mips_opts.insn32
c0ebe874 11444 && op[0] == RA
e64af278 11445 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11446 macro_build (NULL, s, "mj", op[1]);
df58fc94 11447 else
c0ebe874 11448 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11449 }
0a44bf69 11450 else
252b5132 11451 {
df58fc94
RS
11452 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11453 && mips_cprestore_offset >= 0);
11454
c0ebe874 11455 if (op[1] != PIC_CALL_REG)
252b5132 11456 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11457
833794fc
MR
11458 s = ((mips_opts.micromips
11459 && !mips_opts.insn32
11460 && (!mips_opts.noreorder || cprestore))
df58fc94 11461 ? "jalrs" : "jalr");
e64af278 11462 if (mips_opts.micromips
833794fc 11463 && !mips_opts.insn32
c0ebe874 11464 && op[0] == RA
e64af278 11465 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11466 macro_build (NULL, s, "mj", op[1]);
df58fc94 11467 else
c0ebe874 11468 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11469 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11470 {
6478892d 11471 if (mips_cprestore_offset < 0)
1661c76c 11472 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11473 else
11474 {
90ecf173 11475 if (!mips_frame_reg_valid)
7a621144 11476 {
1661c76c 11477 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11478 /* Quiet this warning. */
11479 mips_frame_reg_valid = 1;
11480 }
90ecf173 11481 if (!mips_cprestore_valid)
7a621144 11482 {
1661c76c 11483 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11484 /* Quiet this warning. */
11485 mips_cprestore_valid = 1;
11486 }
d3fca0b5
MR
11487 if (mips_opts.noreorder)
11488 macro_build (NULL, "nop", "");
6478892d 11489 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11490 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11491 mips_gp_register,
256ab948
TS
11492 mips_frame_reg,
11493 HAVE_64BIT_ADDRESSES);
6478892d 11494 }
252b5132
RH
11495 }
11496 }
252b5132 11497
8fc2e39e 11498 break;
252b5132 11499
df58fc94
RS
11500 case M_JALS_A:
11501 gas_assert (mips_opts.micromips);
833794fc
MR
11502 if (mips_opts.insn32)
11503 {
1661c76c 11504 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11505 break;
11506 }
df58fc94
RS
11507 jals = 1;
11508 /* Fall through. */
252b5132
RH
11509 case M_JAL_A:
11510 if (mips_pic == NO_PIC)
df58fc94 11511 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11512 else if (mips_pic == SVR4_PIC)
11513 {
11514 /* If this is a reference to an external symbol, and we are
11515 using a small GOT, we want
11516 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11517 nop
f9419b05 11518 jalr $ra,$25
252b5132
RH
11519 nop
11520 lw $gp,cprestore($sp)
11521 The cprestore value is set using the .cprestore
11522 pseudo-op. If we are using a big GOT, we want
11523 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11524 addu $25,$25,$gp
11525 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11526 nop
f9419b05 11527 jalr $ra,$25
252b5132
RH
11528 nop
11529 lw $gp,cprestore($sp)
11530 If the symbol is not external, we want
11531 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11532 nop
11533 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11534 jalr $ra,$25
252b5132 11535 nop
438c16b8 11536 lw $gp,cprestore($sp)
f5040a92
AO
11537
11538 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11539 sequences above, minus nops, unless the symbol is local,
11540 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11541 GOT_DISP. */
438c16b8 11542 if (HAVE_NEWABI)
252b5132 11543 {
90ecf173 11544 if (!mips_big_got)
f5040a92 11545 {
4d7206a2 11546 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11547 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11548 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11549 mips_gp_register);
4d7206a2 11550 relax_switch ();
67c0d1eb
RS
11551 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11552 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11553 mips_gp_register);
11554 relax_end ();
f5040a92
AO
11555 }
11556 else
11557 {
4d7206a2 11558 relax_start (offset_expr.X_add_symbol);
df58fc94 11559 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11560 BFD_RELOC_MIPS_CALL_HI16);
11561 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11562 PIC_CALL_REG, mips_gp_register);
11563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11564 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11565 PIC_CALL_REG);
4d7206a2 11566 relax_switch ();
67c0d1eb
RS
11567 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11568 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11569 mips_gp_register);
11570 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11571 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11572 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11573 relax_end ();
f5040a92 11574 }
684022ea 11575
df58fc94 11576 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11577 }
11578 else
11579 {
4d7206a2 11580 relax_start (offset_expr.X_add_symbol);
90ecf173 11581 if (!mips_big_got)
438c16b8 11582 {
67c0d1eb
RS
11583 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11584 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11585 mips_gp_register);
269137b2 11586 load_delay_nop ();
4d7206a2 11587 relax_switch ();
438c16b8 11588 }
252b5132 11589 else
252b5132 11590 {
67c0d1eb
RS
11591 int gpdelay;
11592
11593 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11594 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11595 BFD_RELOC_MIPS_CALL_HI16);
11596 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11597 PIC_CALL_REG, mips_gp_register);
11598 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11599 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11600 PIC_CALL_REG);
269137b2 11601 load_delay_nop ();
4d7206a2 11602 relax_switch ();
67c0d1eb
RS
11603 if (gpdelay)
11604 macro_build (NULL, "nop", "");
252b5132 11605 }
67c0d1eb
RS
11606 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11607 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11608 mips_gp_register);
269137b2 11609 load_delay_nop ();
67c0d1eb
RS
11610 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11611 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11612 relax_end ();
df58fc94 11613 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11614
6478892d 11615 if (mips_cprestore_offset < 0)
1661c76c 11616 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11617 else
11618 {
90ecf173 11619 if (!mips_frame_reg_valid)
7a621144 11620 {
1661c76c 11621 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11622 /* Quiet this warning. */
11623 mips_frame_reg_valid = 1;
11624 }
90ecf173 11625 if (!mips_cprestore_valid)
7a621144 11626 {
1661c76c 11627 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11628 /* Quiet this warning. */
11629 mips_cprestore_valid = 1;
11630 }
6478892d 11631 if (mips_opts.noreorder)
67c0d1eb 11632 macro_build (NULL, "nop", "");
6478892d 11633 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11634 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11635 mips_gp_register,
256ab948
TS
11636 mips_frame_reg,
11637 HAVE_64BIT_ADDRESSES);
6478892d 11638 }
252b5132
RH
11639 }
11640 }
0a44bf69 11641 else if (mips_pic == VXWORKS_PIC)
1661c76c 11642 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11643 else
11644 abort ();
11645
8fc2e39e 11646 break;
252b5132 11647
7f3c4072 11648 case M_LBUE_AB:
7f3c4072
CM
11649 s = "lbue";
11650 fmt = "t,+j(b)";
11651 offbits = 9;
11652 goto ld_st;
11653 case M_LHUE_AB:
7f3c4072
CM
11654 s = "lhue";
11655 fmt = "t,+j(b)";
11656 offbits = 9;
11657 goto ld_st;
11658 case M_LBE_AB:
7f3c4072
CM
11659 s = "lbe";
11660 fmt = "t,+j(b)";
11661 offbits = 9;
11662 goto ld_st;
11663 case M_LHE_AB:
7f3c4072
CM
11664 s = "lhe";
11665 fmt = "t,+j(b)";
11666 offbits = 9;
11667 goto ld_st;
11668 case M_LLE_AB:
7f3c4072
CM
11669 s = "lle";
11670 fmt = "t,+j(b)";
11671 offbits = 9;
11672 goto ld_st;
11673 case M_LWE_AB:
7f3c4072
CM
11674 s = "lwe";
11675 fmt = "t,+j(b)";
11676 offbits = 9;
11677 goto ld_st;
11678 case M_LWLE_AB:
7f3c4072
CM
11679 s = "lwle";
11680 fmt = "t,+j(b)";
11681 offbits = 9;
11682 goto ld_st;
11683 case M_LWRE_AB:
7f3c4072
CM
11684 s = "lwre";
11685 fmt = "t,+j(b)";
11686 offbits = 9;
11687 goto ld_st;
11688 case M_SBE_AB:
7f3c4072
CM
11689 s = "sbe";
11690 fmt = "t,+j(b)";
11691 offbits = 9;
11692 goto ld_st;
11693 case M_SCE_AB:
7f3c4072
CM
11694 s = "sce";
11695 fmt = "t,+j(b)";
11696 offbits = 9;
11697 goto ld_st;
11698 case M_SHE_AB:
7f3c4072
CM
11699 s = "she";
11700 fmt = "t,+j(b)";
11701 offbits = 9;
11702 goto ld_st;
11703 case M_SWE_AB:
7f3c4072
CM
11704 s = "swe";
11705 fmt = "t,+j(b)";
11706 offbits = 9;
11707 goto ld_st;
11708 case M_SWLE_AB:
7f3c4072
CM
11709 s = "swle";
11710 fmt = "t,+j(b)";
11711 offbits = 9;
11712 goto ld_st;
11713 case M_SWRE_AB:
7f3c4072
CM
11714 s = "swre";
11715 fmt = "t,+j(b)";
11716 offbits = 9;
11717 goto ld_st;
dec0624d 11718 case M_ACLR_AB:
dec0624d 11719 s = "aclr";
dec0624d 11720 fmt = "\\,~(b)";
7f3c4072 11721 offbits = 12;
dec0624d
MR
11722 goto ld_st;
11723 case M_ASET_AB:
dec0624d 11724 s = "aset";
dec0624d 11725 fmt = "\\,~(b)";
7f3c4072 11726 offbits = 12;
dec0624d 11727 goto ld_st;
252b5132
RH
11728 case M_LB_AB:
11729 s = "lb";
df58fc94 11730 fmt = "t,o(b)";
252b5132
RH
11731 goto ld;
11732 case M_LBU_AB:
11733 s = "lbu";
df58fc94 11734 fmt = "t,o(b)";
252b5132
RH
11735 goto ld;
11736 case M_LH_AB:
11737 s = "lh";
df58fc94 11738 fmt = "t,o(b)";
252b5132
RH
11739 goto ld;
11740 case M_LHU_AB:
11741 s = "lhu";
df58fc94 11742 fmt = "t,o(b)";
252b5132
RH
11743 goto ld;
11744 case M_LW_AB:
11745 s = "lw";
df58fc94 11746 fmt = "t,o(b)";
252b5132
RH
11747 goto ld;
11748 case M_LWC0_AB:
df58fc94 11749 gas_assert (!mips_opts.micromips);
252b5132 11750 s = "lwc0";
df58fc94 11751 fmt = "E,o(b)";
bdaaa2e1 11752 /* Itbl support may require additional care here. */
252b5132 11753 coproc = 1;
df58fc94 11754 goto ld_st;
252b5132
RH
11755 case M_LWC1_AB:
11756 s = "lwc1";
df58fc94 11757 fmt = "T,o(b)";
bdaaa2e1 11758 /* Itbl support may require additional care here. */
252b5132 11759 coproc = 1;
df58fc94 11760 goto ld_st;
252b5132
RH
11761 case M_LWC2_AB:
11762 s = "lwc2";
df58fc94 11763 fmt = COP12_FMT;
7361da2c
AB
11764 offbits = (mips_opts.micromips ? 12
11765 : ISA_IS_R6 (mips_opts.isa) ? 11
11766 : 16);
bdaaa2e1 11767 /* Itbl support may require additional care here. */
252b5132 11768 coproc = 1;
df58fc94 11769 goto ld_st;
252b5132 11770 case M_LWC3_AB:
df58fc94 11771 gas_assert (!mips_opts.micromips);
252b5132 11772 s = "lwc3";
df58fc94 11773 fmt = "E,o(b)";
bdaaa2e1 11774 /* Itbl support may require additional care here. */
252b5132 11775 coproc = 1;
df58fc94 11776 goto ld_st;
252b5132
RH
11777 case M_LWL_AB:
11778 s = "lwl";
df58fc94 11779 fmt = MEM12_FMT;
7f3c4072 11780 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11781 goto ld_st;
252b5132
RH
11782 case M_LWR_AB:
11783 s = "lwr";
df58fc94 11784 fmt = MEM12_FMT;
7f3c4072 11785 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11786 goto ld_st;
252b5132 11787 case M_LDC1_AB:
252b5132 11788 s = "ldc1";
df58fc94 11789 fmt = "T,o(b)";
bdaaa2e1 11790 /* Itbl support may require additional care here. */
252b5132 11791 coproc = 1;
df58fc94 11792 goto ld_st;
252b5132
RH
11793 case M_LDC2_AB:
11794 s = "ldc2";
df58fc94 11795 fmt = COP12_FMT;
7361da2c
AB
11796 offbits = (mips_opts.micromips ? 12
11797 : ISA_IS_R6 (mips_opts.isa) ? 11
11798 : 16);
bdaaa2e1 11799 /* Itbl support may require additional care here. */
252b5132 11800 coproc = 1;
df58fc94 11801 goto ld_st;
c77c0862 11802 case M_LQC2_AB:
c77c0862 11803 s = "lqc2";
14daeee3 11804 fmt = "+7,o(b)";
c77c0862
RS
11805 /* Itbl support may require additional care here. */
11806 coproc = 1;
11807 goto ld_st;
252b5132
RH
11808 case M_LDC3_AB:
11809 s = "ldc3";
df58fc94 11810 fmt = "E,o(b)";
bdaaa2e1 11811 /* Itbl support may require additional care here. */
252b5132 11812 coproc = 1;
df58fc94 11813 goto ld_st;
252b5132
RH
11814 case M_LDL_AB:
11815 s = "ldl";
df58fc94 11816 fmt = MEM12_FMT;
7f3c4072 11817 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11818 goto ld_st;
252b5132
RH
11819 case M_LDR_AB:
11820 s = "ldr";
df58fc94 11821 fmt = MEM12_FMT;
7f3c4072 11822 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11823 goto ld_st;
252b5132
RH
11824 case M_LL_AB:
11825 s = "ll";
7361da2c
AB
11826 fmt = LL_SC_FMT;
11827 offbits = (mips_opts.micromips ? 12
11828 : ISA_IS_R6 (mips_opts.isa) ? 9
11829 : 16);
252b5132
RH
11830 goto ld;
11831 case M_LLD_AB:
11832 s = "lld";
7361da2c
AB
11833 fmt = LL_SC_FMT;
11834 offbits = (mips_opts.micromips ? 12
11835 : ISA_IS_R6 (mips_opts.isa) ? 9
11836 : 16);
252b5132
RH
11837 goto ld;
11838 case M_LWU_AB:
11839 s = "lwu";
df58fc94 11840 fmt = MEM12_FMT;
7f3c4072 11841 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11842 goto ld;
11843 case M_LWP_AB:
df58fc94
RS
11844 gas_assert (mips_opts.micromips);
11845 s = "lwp";
11846 fmt = "t,~(b)";
7f3c4072 11847 offbits = 12;
df58fc94
RS
11848 lp = 1;
11849 goto ld;
11850 case M_LDP_AB:
df58fc94
RS
11851 gas_assert (mips_opts.micromips);
11852 s = "ldp";
11853 fmt = "t,~(b)";
7f3c4072 11854 offbits = 12;
df58fc94
RS
11855 lp = 1;
11856 goto ld;
11857 case M_LWM_AB:
df58fc94
RS
11858 gas_assert (mips_opts.micromips);
11859 s = "lwm";
11860 fmt = "n,~(b)";
7f3c4072 11861 offbits = 12;
df58fc94
RS
11862 goto ld_st;
11863 case M_LDM_AB:
df58fc94
RS
11864 gas_assert (mips_opts.micromips);
11865 s = "ldm";
11866 fmt = "n,~(b)";
7f3c4072 11867 offbits = 12;
df58fc94
RS
11868 goto ld_st;
11869
252b5132 11870 ld:
f19ccbda 11871 /* We don't want to use $0 as tempreg. */
c0ebe874 11872 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11873 goto ld_st;
252b5132 11874 else
c0ebe874 11875 tempreg = op[0] + lp;
df58fc94
RS
11876 goto ld_noat;
11877
252b5132
RH
11878 case M_SB_AB:
11879 s = "sb";
df58fc94
RS
11880 fmt = "t,o(b)";
11881 goto ld_st;
252b5132
RH
11882 case M_SH_AB:
11883 s = "sh";
df58fc94
RS
11884 fmt = "t,o(b)";
11885 goto ld_st;
252b5132
RH
11886 case M_SW_AB:
11887 s = "sw";
df58fc94
RS
11888 fmt = "t,o(b)";
11889 goto ld_st;
252b5132 11890 case M_SWC0_AB:
df58fc94 11891 gas_assert (!mips_opts.micromips);
252b5132 11892 s = "swc0";
df58fc94 11893 fmt = "E,o(b)";
bdaaa2e1 11894 /* Itbl support may require additional care here. */
252b5132 11895 coproc = 1;
df58fc94 11896 goto ld_st;
252b5132
RH
11897 case M_SWC1_AB:
11898 s = "swc1";
df58fc94 11899 fmt = "T,o(b)";
bdaaa2e1 11900 /* Itbl support may require additional care here. */
252b5132 11901 coproc = 1;
df58fc94 11902 goto ld_st;
252b5132
RH
11903 case M_SWC2_AB:
11904 s = "swc2";
df58fc94 11905 fmt = COP12_FMT;
7361da2c
AB
11906 offbits = (mips_opts.micromips ? 12
11907 : ISA_IS_R6 (mips_opts.isa) ? 11
11908 : 16);
bdaaa2e1 11909 /* Itbl support may require additional care here. */
252b5132 11910 coproc = 1;
df58fc94 11911 goto ld_st;
252b5132 11912 case M_SWC3_AB:
df58fc94 11913 gas_assert (!mips_opts.micromips);
252b5132 11914 s = "swc3";
df58fc94 11915 fmt = "E,o(b)";
bdaaa2e1 11916 /* Itbl support may require additional care here. */
252b5132 11917 coproc = 1;
df58fc94 11918 goto ld_st;
252b5132
RH
11919 case M_SWL_AB:
11920 s = "swl";
df58fc94 11921 fmt = MEM12_FMT;
7f3c4072 11922 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11923 goto ld_st;
252b5132
RH
11924 case M_SWR_AB:
11925 s = "swr";
df58fc94 11926 fmt = MEM12_FMT;
7f3c4072 11927 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11928 goto ld_st;
252b5132
RH
11929 case M_SC_AB:
11930 s = "sc";
7361da2c
AB
11931 fmt = LL_SC_FMT;
11932 offbits = (mips_opts.micromips ? 12
11933 : ISA_IS_R6 (mips_opts.isa) ? 9
11934 : 16);
df58fc94 11935 goto ld_st;
252b5132
RH
11936 case M_SCD_AB:
11937 s = "scd";
7361da2c
AB
11938 fmt = LL_SC_FMT;
11939 offbits = (mips_opts.micromips ? 12
11940 : ISA_IS_R6 (mips_opts.isa) ? 9
11941 : 16);
df58fc94 11942 goto ld_st;
d43b4baf
TS
11943 case M_CACHE_AB:
11944 s = "cache";
7361da2c
AB
11945 fmt = (mips_opts.micromips ? "k,~(b)"
11946 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11947 : "k,o(b)");
11948 offbits = (mips_opts.micromips ? 12
11949 : ISA_IS_R6 (mips_opts.isa) ? 9
11950 : 16);
7f3c4072
CM
11951 goto ld_st;
11952 case M_CACHEE_AB:
7f3c4072
CM
11953 s = "cachee";
11954 fmt = "k,+j(b)";
11955 offbits = 9;
df58fc94 11956 goto ld_st;
3eebd5eb
MR
11957 case M_PREF_AB:
11958 s = "pref";
7361da2c
AB
11959 fmt = (mips_opts.micromips ? "k,~(b)"
11960 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11961 : "k,o(b)");
11962 offbits = (mips_opts.micromips ? 12
11963 : ISA_IS_R6 (mips_opts.isa) ? 9
11964 : 16);
7f3c4072
CM
11965 goto ld_st;
11966 case M_PREFE_AB:
7f3c4072
CM
11967 s = "prefe";
11968 fmt = "k,+j(b)";
11969 offbits = 9;
df58fc94 11970 goto ld_st;
252b5132 11971 case M_SDC1_AB:
252b5132 11972 s = "sdc1";
df58fc94 11973 fmt = "T,o(b)";
252b5132 11974 coproc = 1;
bdaaa2e1 11975 /* Itbl support may require additional care here. */
df58fc94 11976 goto ld_st;
252b5132
RH
11977 case M_SDC2_AB:
11978 s = "sdc2";
df58fc94 11979 fmt = COP12_FMT;
7361da2c
AB
11980 offbits = (mips_opts.micromips ? 12
11981 : ISA_IS_R6 (mips_opts.isa) ? 11
11982 : 16);
c77c0862
RS
11983 /* Itbl support may require additional care here. */
11984 coproc = 1;
11985 goto ld_st;
11986 case M_SQC2_AB:
c77c0862 11987 s = "sqc2";
14daeee3 11988 fmt = "+7,o(b)";
bdaaa2e1 11989 /* Itbl support may require additional care here. */
252b5132 11990 coproc = 1;
df58fc94 11991 goto ld_st;
252b5132 11992 case M_SDC3_AB:
df58fc94 11993 gas_assert (!mips_opts.micromips);
252b5132 11994 s = "sdc3";
df58fc94 11995 fmt = "E,o(b)";
bdaaa2e1 11996 /* Itbl support may require additional care here. */
252b5132 11997 coproc = 1;
df58fc94 11998 goto ld_st;
252b5132
RH
11999 case M_SDL_AB:
12000 s = "sdl";
df58fc94 12001 fmt = MEM12_FMT;
7f3c4072 12002 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 12003 goto ld_st;
252b5132
RH
12004 case M_SDR_AB:
12005 s = "sdr";
df58fc94 12006 fmt = MEM12_FMT;
7f3c4072 12007 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
12008 goto ld_st;
12009 case M_SWP_AB:
df58fc94
RS
12010 gas_assert (mips_opts.micromips);
12011 s = "swp";
12012 fmt = "t,~(b)";
7f3c4072 12013 offbits = 12;
df58fc94
RS
12014 goto ld_st;
12015 case M_SDP_AB:
df58fc94
RS
12016 gas_assert (mips_opts.micromips);
12017 s = "sdp";
12018 fmt = "t,~(b)";
7f3c4072 12019 offbits = 12;
df58fc94
RS
12020 goto ld_st;
12021 case M_SWM_AB:
df58fc94
RS
12022 gas_assert (mips_opts.micromips);
12023 s = "swm";
12024 fmt = "n,~(b)";
7f3c4072 12025 offbits = 12;
df58fc94
RS
12026 goto ld_st;
12027 case M_SDM_AB:
df58fc94
RS
12028 gas_assert (mips_opts.micromips);
12029 s = "sdm";
12030 fmt = "n,~(b)";
7f3c4072 12031 offbits = 12;
df58fc94
RS
12032
12033 ld_st:
8fc2e39e 12034 tempreg = AT;
df58fc94 12035 ld_noat:
c0ebe874 12036 breg = op[2];
f2ae14a1
RS
12037 if (small_offset_p (0, align, 16))
12038 {
12039 /* The first case exists for M_LD_AB and M_SD_AB, which are
12040 macros for o32 but which should act like normal instructions
12041 otherwise. */
12042 if (offbits == 16)
c0ebe874 12043 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12044 offset_reloc[1], offset_reloc[2], breg);
12045 else if (small_offset_p (0, align, offbits))
12046 {
12047 if (offbits == 0)
c0ebe874 12048 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 12049 else
c0ebe874 12050 macro_build (NULL, s, fmt, op[0],
c8276761 12051 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12052 }
12053 else
12054 {
12055 if (tempreg == AT)
12056 used_at = 1;
12057 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12058 tempreg, breg, -1, offset_reloc[0],
12059 offset_reloc[1], offset_reloc[2]);
12060 if (offbits == 0)
c0ebe874 12061 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 12062 else
c0ebe874 12063 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12064 }
12065 break;
12066 }
12067
12068 if (tempreg == AT)
12069 used_at = 1;
12070
252b5132
RH
12071 if (offset_expr.X_op != O_constant
12072 && offset_expr.X_op != O_symbol)
12073 {
1661c76c 12074 as_bad (_("expression too complex"));
252b5132
RH
12075 offset_expr.X_op = O_constant;
12076 }
12077
2051e8c4
MR
12078 if (HAVE_32BIT_ADDRESSES
12079 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12080 {
12081 char value [32];
12082
12083 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12084 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12085 }
2051e8c4 12086
252b5132
RH
12087 /* A constant expression in PIC code can be handled just as it
12088 is in non PIC code. */
aed1a261
RS
12089 if (offset_expr.X_op == O_constant)
12090 {
f2ae14a1
RS
12091 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12092 offbits == 0 ? 16 : offbits);
12093 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12094
f2ae14a1
RS
12095 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12096 if (breg != 0)
12097 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12098 tempreg, tempreg, breg);
7f3c4072 12099 if (offbits == 0)
dd6a37e7 12100 {
f2ae14a1 12101 if (offset_expr.X_add_number != 0)
dd6a37e7 12102 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12103 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12104 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12105 }
7f3c4072 12106 else if (offbits == 16)
c0ebe874 12107 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12108 else
c0ebe874 12109 macro_build (NULL, s, fmt, op[0],
c8276761 12110 (int) offset_expr.X_add_number, tempreg);
df58fc94 12111 }
7f3c4072 12112 else if (offbits != 16)
df58fc94 12113 {
7f3c4072 12114 /* The offset field is too narrow to be used for a low-part
2b0f3761 12115 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12116 register. */
12117 load_address (tempreg, &offset_expr, &used_at);
12118 if (breg != 0)
12119 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12120 tempreg, tempreg, breg);
7f3c4072 12121 if (offbits == 0)
c0ebe874 12122 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12123 else
c0ebe874 12124 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12125 }
12126 else if (mips_pic == NO_PIC)
252b5132
RH
12127 {
12128 /* If this is a reference to a GP relative symbol, and there
12129 is no base register, we want
c0ebe874 12130 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12131 Otherwise, if there is no base register, we want
12132 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12133 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12134 If we have a constant, we need two instructions anyhow,
12135 so we always use the latter form.
12136
12137 If we have a base register, and this is a reference to a
12138 GP relative symbol, we want
12139 addu $tempreg,$breg,$gp
c0ebe874 12140 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12141 Otherwise we want
12142 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12143 addu $tempreg,$tempreg,$breg
c0ebe874 12144 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12145 With a constant we always use the latter case.
76b3015f 12146
d6bc6245
TS
12147 With 64bit address space and no base register and $at usable,
12148 we want
12149 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12150 lui $at,<sym> (BFD_RELOC_HI16_S)
12151 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12152 dsll32 $tempreg,0
12153 daddu $tempreg,$at
c0ebe874 12154 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12155 If we have a base register, we want
12156 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12157 lui $at,<sym> (BFD_RELOC_HI16_S)
12158 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12159 daddu $at,$breg
12160 dsll32 $tempreg,0
12161 daddu $tempreg,$at
c0ebe874 12162 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12163
12164 Without $at we can't generate the optimal path for superscalar
12165 processors here since this would require two temporary registers.
12166 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12167 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12168 dsll $tempreg,16
12169 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12170 dsll $tempreg,16
c0ebe874 12171 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12172 If we have a base register, we want
12173 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12174 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12175 dsll $tempreg,16
12176 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12177 dsll $tempreg,16
12178 daddu $tempreg,$tempreg,$breg
c0ebe874 12179 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12180
6caf9ef4 12181 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12182 the same sequence as in 32bit address space. */
12183 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12184 {
aed1a261 12185 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12186 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12187 {
12188 relax_start (offset_expr.X_add_symbol);
12189 if (breg == 0)
12190 {
c0ebe874 12191 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12192 BFD_RELOC_GPREL16, mips_gp_register);
12193 }
12194 else
12195 {
12196 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12197 tempreg, breg, mips_gp_register);
c0ebe874 12198 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12199 BFD_RELOC_GPREL16, tempreg);
12200 }
12201 relax_switch ();
12202 }
d6bc6245 12203
741fe287 12204 if (used_at == 0 && mips_opts.at)
d6bc6245 12205 {
df58fc94 12206 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12207 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12208 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12209 BFD_RELOC_HI16_S);
12210 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12211 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12212 if (breg != 0)
67c0d1eb 12213 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12214 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12215 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12216 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12217 tempreg);
d6bc6245
TS
12218 used_at = 1;
12219 }
12220 else
12221 {
df58fc94 12222 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12223 BFD_RELOC_MIPS_HIGHEST);
12224 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12225 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12226 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12227 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12228 tempreg, BFD_RELOC_HI16_S);
df58fc94 12229 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12230 if (breg != 0)
67c0d1eb 12231 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12232 tempreg, tempreg, breg);
c0ebe874 12233 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12234 BFD_RELOC_LO16, tempreg);
d6bc6245 12235 }
6caf9ef4
TS
12236
12237 if (mips_relax.sequence)
12238 relax_end ();
8fc2e39e 12239 break;
d6bc6245 12240 }
256ab948 12241
252b5132
RH
12242 if (breg == 0)
12243 {
67c0d1eb 12244 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12245 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12246 {
4d7206a2 12247 relax_start (offset_expr.X_add_symbol);
c0ebe874 12248 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12249 mips_gp_register);
4d7206a2 12250 relax_switch ();
252b5132 12251 }
67c0d1eb 12252 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12253 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12254 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12255 if (mips_relax.sequence)
12256 relax_end ();
252b5132
RH
12257 }
12258 else
12259 {
67c0d1eb 12260 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12261 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12262 {
4d7206a2 12263 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12264 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12265 tempreg, breg, mips_gp_register);
c0ebe874 12266 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12267 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12268 relax_switch ();
252b5132 12269 }
67c0d1eb
RS
12270 macro_build_lui (&offset_expr, tempreg);
12271 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12272 tempreg, tempreg, breg);
c0ebe874 12273 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12274 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12275 if (mips_relax.sequence)
12276 relax_end ();
252b5132
RH
12277 }
12278 }
0a44bf69 12279 else if (!mips_big_got)
252b5132 12280 {
ed6fb7bd 12281 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12282
252b5132
RH
12283 /* If this is a reference to an external symbol, we want
12284 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12285 nop
c0ebe874 12286 <op> op[0],0($tempreg)
252b5132
RH
12287 Otherwise we want
12288 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12289 nop
12290 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12291 <op> op[0],0($tempreg)
f5040a92
AO
12292
12293 For NewABI, we want
12294 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12295 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12296
252b5132
RH
12297 If there is a base register, we add it to $tempreg before
12298 the <op>. If there is a constant, we stick it in the
12299 <op> instruction. We don't handle constants larger than
12300 16 bits, because we have no way to load the upper 16 bits
12301 (actually, we could handle them for the subset of cases
12302 in which we are not using $at). */
9c2799c2 12303 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12304 if (HAVE_NEWABI)
12305 {
67c0d1eb
RS
12306 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12307 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12308 if (breg != 0)
67c0d1eb 12309 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12310 tempreg, tempreg, breg);
c0ebe874 12311 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12312 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12313 break;
12314 }
252b5132
RH
12315 expr1.X_add_number = offset_expr.X_add_number;
12316 offset_expr.X_add_number = 0;
12317 if (expr1.X_add_number < -0x8000
12318 || expr1.X_add_number >= 0x8000)
12319 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12320 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12321 lw_reloc_type, mips_gp_register);
269137b2 12322 load_delay_nop ();
4d7206a2
RS
12323 relax_start (offset_expr.X_add_symbol);
12324 relax_switch ();
67c0d1eb
RS
12325 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12326 tempreg, BFD_RELOC_LO16);
4d7206a2 12327 relax_end ();
252b5132 12328 if (breg != 0)
67c0d1eb 12329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12330 tempreg, tempreg, breg);
c0ebe874 12331 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12332 }
0a44bf69 12333 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12334 {
67c0d1eb 12335 int gpdelay;
252b5132
RH
12336
12337 /* If this is a reference to an external symbol, we want
12338 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12339 addu $tempreg,$tempreg,$gp
12340 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12341 <op> op[0],0($tempreg)
252b5132
RH
12342 Otherwise we want
12343 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12344 nop
12345 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12346 <op> op[0],0($tempreg)
252b5132
RH
12347 If there is a base register, we add it to $tempreg before
12348 the <op>. If there is a constant, we stick it in the
12349 <op> instruction. We don't handle constants larger than
12350 16 bits, because we have no way to load the upper 16 bits
12351 (actually, we could handle them for the subset of cases
f5040a92 12352 in which we are not using $at). */
9c2799c2 12353 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12354 expr1.X_add_number = offset_expr.X_add_number;
12355 offset_expr.X_add_number = 0;
12356 if (expr1.X_add_number < -0x8000
12357 || expr1.X_add_number >= 0x8000)
12358 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12359 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12360 relax_start (offset_expr.X_add_symbol);
df58fc94 12361 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12362 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12363 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12364 mips_gp_register);
12365 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12366 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12367 relax_switch ();
67c0d1eb
RS
12368 if (gpdelay)
12369 macro_build (NULL, "nop", "");
12370 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12371 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12372 load_delay_nop ();
67c0d1eb
RS
12373 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12374 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12375 relax_end ();
12376
252b5132 12377 if (breg != 0)
67c0d1eb 12378 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12379 tempreg, tempreg, breg);
c0ebe874 12380 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12381 }
0a44bf69 12382 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12383 {
f5040a92
AO
12384 /* If this is a reference to an external symbol, we want
12385 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12386 add $tempreg,$tempreg,$gp
12387 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12388 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12389 Otherwise, for local symbols, we want:
12390 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12391 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12392 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12393 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12394 offset_expr.X_add_number = 0;
12395 if (expr1.X_add_number < -0x8000
12396 || expr1.X_add_number >= 0x8000)
12397 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12398 relax_start (offset_expr.X_add_symbol);
df58fc94 12399 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12400 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12401 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12402 mips_gp_register);
12403 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12404 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12405 if (breg != 0)
67c0d1eb 12406 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12407 tempreg, tempreg, breg);
c0ebe874 12408 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12409
4d7206a2 12410 relax_switch ();
f5040a92 12411 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12412 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12413 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12414 if (breg != 0)
67c0d1eb 12415 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12416 tempreg, tempreg, breg);
c0ebe874 12417 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12418 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12419 relax_end ();
f5040a92 12420 }
252b5132
RH
12421 else
12422 abort ();
12423
252b5132
RH
12424 break;
12425
833794fc
MR
12426 case M_JRADDIUSP:
12427 gas_assert (mips_opts.micromips);
12428 gas_assert (mips_opts.insn32);
12429 start_noreorder ();
12430 macro_build (NULL, "jr", "s", RA);
c0ebe874 12431 expr1.X_add_number = op[0] << 2;
833794fc
MR
12432 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12433 end_noreorder ();
12434 break;
12435
12436 case M_JRC:
12437 gas_assert (mips_opts.micromips);
12438 gas_assert (mips_opts.insn32);
c0ebe874 12439 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12440 if (mips_opts.noreorder)
12441 macro_build (NULL, "nop", "");
12442 break;
12443
252b5132
RH
12444 case M_LI:
12445 case M_LI_S:
c0ebe874 12446 load_register (op[0], &imm_expr, 0);
8fc2e39e 12447 break;
252b5132
RH
12448
12449 case M_DLI:
c0ebe874 12450 load_register (op[0], &imm_expr, 1);
8fc2e39e 12451 break;
252b5132
RH
12452
12453 case M_LI_SS:
12454 if (imm_expr.X_op == O_constant)
12455 {
8fc2e39e 12456 used_at = 1;
67c0d1eb 12457 load_register (AT, &imm_expr, 0);
c0ebe874 12458 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12459 break;
12460 }
12461 else
12462 {
b0e6f033
RS
12463 gas_assert (imm_expr.X_op == O_absent
12464 && offset_expr.X_op == O_symbol
90ecf173
MR
12465 && strcmp (segment_name (S_GET_SEGMENT
12466 (offset_expr.X_add_symbol)),
12467 ".lit4") == 0
12468 && offset_expr.X_add_number == 0);
c0ebe874 12469 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12470 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12471 break;
252b5132
RH
12472 }
12473
12474 case M_LI_D:
ca4e0257
RS
12475 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12476 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12477 order 32 bits of the value and the low order 32 bits are either
12478 zero or in OFFSET_EXPR. */
b0e6f033 12479 if (imm_expr.X_op == O_constant)
252b5132 12480 {
bad1aba3 12481 if (GPR_SIZE == 64)
c0ebe874 12482 load_register (op[0], &imm_expr, 1);
252b5132
RH
12483 else
12484 {
12485 int hreg, lreg;
12486
12487 if (target_big_endian)
12488 {
c0ebe874
RS
12489 hreg = op[0];
12490 lreg = op[0] + 1;
252b5132
RH
12491 }
12492 else
12493 {
c0ebe874
RS
12494 hreg = op[0] + 1;
12495 lreg = op[0];
252b5132
RH
12496 }
12497
12498 if (hreg <= 31)
67c0d1eb 12499 load_register (hreg, &imm_expr, 0);
252b5132
RH
12500 if (lreg <= 31)
12501 {
12502 if (offset_expr.X_op == O_absent)
67c0d1eb 12503 move_register (lreg, 0);
252b5132
RH
12504 else
12505 {
9c2799c2 12506 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12507 load_register (lreg, &offset_expr, 0);
252b5132
RH
12508 }
12509 }
12510 }
8fc2e39e 12511 break;
252b5132 12512 }
b0e6f033 12513 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12514
12515 /* We know that sym is in the .rdata section. First we get the
12516 upper 16 bits of the address. */
12517 if (mips_pic == NO_PIC)
12518 {
67c0d1eb 12519 macro_build_lui (&offset_expr, AT);
8fc2e39e 12520 used_at = 1;
252b5132 12521 }
0a44bf69 12522 else
252b5132 12523 {
67c0d1eb
RS
12524 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12525 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12526 used_at = 1;
252b5132 12527 }
bdaaa2e1 12528
252b5132 12529 /* Now we load the register(s). */
bad1aba3 12530 if (GPR_SIZE == 64)
8fc2e39e
TS
12531 {
12532 used_at = 1;
c0ebe874
RS
12533 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12534 BFD_RELOC_LO16, AT);
8fc2e39e 12535 }
252b5132
RH
12536 else
12537 {
8fc2e39e 12538 used_at = 1;
c0ebe874
RS
12539 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12540 BFD_RELOC_LO16, AT);
12541 if (op[0] != RA)
252b5132
RH
12542 {
12543 /* FIXME: How in the world do we deal with the possible
12544 overflow here? */
12545 offset_expr.X_add_number += 4;
67c0d1eb 12546 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12547 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12548 }
12549 }
252b5132
RH
12550 break;
12551
12552 case M_LI_DD:
ca4e0257
RS
12553 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12554 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12555 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12556 the value and the low order 32 bits are either zero or in
12557 OFFSET_EXPR. */
b0e6f033 12558 if (imm_expr.X_op == O_constant)
252b5132 12559 {
8fc2e39e 12560 used_at = 1;
bad1aba3 12561 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12562 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12563 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12564 else
12565 {
351cdf24
MF
12566 if (ISA_HAS_MXHC1 (mips_opts.isa))
12567 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12568 else if (FPR_SIZE != 32)
12569 as_bad (_("Unable to generate `%s' compliant code "
12570 "without mthc1"),
12571 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12572 else
12573 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12574 if (offset_expr.X_op == O_absent)
c0ebe874 12575 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12576 else
12577 {
9c2799c2 12578 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12579 load_register (AT, &offset_expr, 0);
c0ebe874 12580 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12581 }
12582 }
12583 break;
12584 }
12585
b0e6f033
RS
12586 gas_assert (imm_expr.X_op == O_absent
12587 && offset_expr.X_op == O_symbol
90ecf173 12588 && offset_expr.X_add_number == 0);
252b5132
RH
12589 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12590 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12591 {
12592 op[2] = mips_gp_register;
f2ae14a1
RS
12593 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12594 offset_reloc[1] = BFD_RELOC_UNUSED;
12595 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12596 }
12597 else
12598 {
9c2799c2 12599 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12600 used_at = 1;
0a44bf69 12601 if (mips_pic != NO_PIC)
67c0d1eb
RS
12602 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12603 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12604 else
12605 {
12606 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12607 macro_build_lui (&offset_expr, AT);
252b5132 12608 }
bdaaa2e1 12609
c0ebe874 12610 op[2] = AT;
f2ae14a1
RS
12611 offset_reloc[0] = BFD_RELOC_LO16;
12612 offset_reloc[1] = BFD_RELOC_UNUSED;
12613 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12614 }
f2ae14a1
RS
12615 align = 8;
12616 /* Fall through */
c4a68bea 12617
252b5132
RH
12618 case M_L_DAB:
12619 /*
12620 * The MIPS assembler seems to check for X_add_number not
12621 * being double aligned and generating:
12622 * lui at,%hi(foo+1)
12623 * addu at,at,v1
12624 * addiu at,at,%lo(foo+1)
12625 * lwc1 f2,0(at)
12626 * lwc1 f3,4(at)
12627 * But, the resulting address is the same after relocation so why
12628 * generate the extra instruction?
12629 */
bdaaa2e1 12630 /* Itbl support may require additional care here. */
252b5132 12631 coproc = 1;
df58fc94 12632 fmt = "T,o(b)";
0aa27725 12633 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12634 {
12635 s = "ldc1";
df58fc94 12636 goto ld_st;
252b5132 12637 }
252b5132 12638 s = "lwc1";
252b5132
RH
12639 goto ldd_std;
12640
12641 case M_S_DAB:
df58fc94
RS
12642 gas_assert (!mips_opts.micromips);
12643 /* Itbl support may require additional care here. */
12644 coproc = 1;
12645 fmt = "T,o(b)";
0aa27725 12646 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12647 {
12648 s = "sdc1";
df58fc94 12649 goto ld_st;
252b5132 12650 }
252b5132 12651 s = "swc1";
252b5132
RH
12652 goto ldd_std;
12653
e407c74b
NC
12654 case M_LQ_AB:
12655 fmt = "t,o(b)";
12656 s = "lq";
12657 goto ld;
12658
12659 case M_SQ_AB:
12660 fmt = "t,o(b)";
12661 s = "sq";
12662 goto ld_st;
12663
252b5132 12664 case M_LD_AB:
df58fc94 12665 fmt = "t,o(b)";
bad1aba3 12666 if (GPR_SIZE == 64)
252b5132
RH
12667 {
12668 s = "ld";
12669 goto ld;
12670 }
252b5132 12671 s = "lw";
252b5132
RH
12672 goto ldd_std;
12673
12674 case M_SD_AB:
df58fc94 12675 fmt = "t,o(b)";
bad1aba3 12676 if (GPR_SIZE == 64)
252b5132
RH
12677 {
12678 s = "sd";
df58fc94 12679 goto ld_st;
252b5132 12680 }
252b5132 12681 s = "sw";
252b5132
RH
12682
12683 ldd_std:
f2ae14a1
RS
12684 /* Even on a big endian machine $fn comes before $fn+1. We have
12685 to adjust when loading from memory. We set coproc if we must
12686 load $fn+1 first. */
12687 /* Itbl support may require additional care here. */
12688 if (!target_big_endian)
12689 coproc = 0;
12690
c0ebe874 12691 breg = op[2];
f2ae14a1
RS
12692 if (small_offset_p (0, align, 16))
12693 {
12694 ep = &offset_expr;
12695 if (!small_offset_p (4, align, 16))
12696 {
12697 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12698 -1, offset_reloc[0], offset_reloc[1],
12699 offset_reloc[2]);
12700 expr1.X_add_number = 0;
12701 ep = &expr1;
12702 breg = AT;
12703 used_at = 1;
12704 offset_reloc[0] = BFD_RELOC_LO16;
12705 offset_reloc[1] = BFD_RELOC_UNUSED;
12706 offset_reloc[2] = BFD_RELOC_UNUSED;
12707 }
c0ebe874 12708 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12709 {
12710 ep->X_add_number += 4;
c0ebe874 12711 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12712 offset_reloc[1], offset_reloc[2], breg);
12713 ep->X_add_number -= 4;
c0ebe874 12714 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12715 offset_reloc[1], offset_reloc[2], breg);
12716 }
12717 else
12718 {
c0ebe874 12719 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12720 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12721 breg);
12722 ep->X_add_number += 4;
c0ebe874 12723 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12724 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12725 breg);
12726 }
12727 break;
12728 }
12729
252b5132
RH
12730 if (offset_expr.X_op != O_symbol
12731 && offset_expr.X_op != O_constant)
12732 {
1661c76c 12733 as_bad (_("expression too complex"));
252b5132
RH
12734 offset_expr.X_op = O_constant;
12735 }
12736
2051e8c4
MR
12737 if (HAVE_32BIT_ADDRESSES
12738 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12739 {
12740 char value [32];
12741
12742 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12743 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12744 }
2051e8c4 12745
90ecf173 12746 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12747 {
12748 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12749 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12750 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12751 If we have a base register, we use this
12752 addu $at,$breg,$gp
c0ebe874
RS
12753 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12754 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12755 If this is not a GP relative symbol, we want
12756 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12757 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12758 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12759 If there is a base register, we add it to $at after the
12760 lui instruction. If there is a constant, we always use
12761 the last case. */
39a59cf8
MR
12762 if (offset_expr.X_op == O_symbol
12763 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12764 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12765 {
4d7206a2 12766 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12767 if (breg == 0)
12768 {
c9914766 12769 tempreg = mips_gp_register;
252b5132
RH
12770 }
12771 else
12772 {
67c0d1eb 12773 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12774 AT, breg, mips_gp_register);
252b5132 12775 tempreg = AT;
252b5132
RH
12776 used_at = 1;
12777 }
12778
beae10d5 12779 /* Itbl support may require additional care here. */
c0ebe874 12780 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12781 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12782 offset_expr.X_add_number += 4;
12783
12784 /* Set mips_optimize to 2 to avoid inserting an
12785 undesired nop. */
12786 hold_mips_optimize = mips_optimize;
12787 mips_optimize = 2;
beae10d5 12788 /* Itbl support may require additional care here. */
c0ebe874 12789 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12790 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12791 mips_optimize = hold_mips_optimize;
12792
4d7206a2 12793 relax_switch ();
252b5132 12794
0970e49e 12795 offset_expr.X_add_number -= 4;
252b5132 12796 }
8fc2e39e 12797 used_at = 1;
f2ae14a1
RS
12798 if (offset_high_part (offset_expr.X_add_number, 16)
12799 != offset_high_part (offset_expr.X_add_number + 4, 16))
12800 {
12801 load_address (AT, &offset_expr, &used_at);
12802 offset_expr.X_op = O_constant;
12803 offset_expr.X_add_number = 0;
12804 }
12805 else
12806 macro_build_lui (&offset_expr, AT);
252b5132 12807 if (breg != 0)
67c0d1eb 12808 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12809 /* Itbl support may require additional care here. */
c0ebe874 12810 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12811 BFD_RELOC_LO16, AT);
252b5132
RH
12812 /* FIXME: How do we handle overflow here? */
12813 offset_expr.X_add_number += 4;
beae10d5 12814 /* Itbl support may require additional care here. */
c0ebe874 12815 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12816 BFD_RELOC_LO16, AT);
4d7206a2
RS
12817 if (mips_relax.sequence)
12818 relax_end ();
bdaaa2e1 12819 }
0a44bf69 12820 else if (!mips_big_got)
252b5132 12821 {
252b5132
RH
12822 /* If this is a reference to an external symbol, we want
12823 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12824 nop
c0ebe874
RS
12825 <op> op[0],0($at)
12826 <op> op[0]+1,4($at)
252b5132
RH
12827 Otherwise we want
12828 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12829 nop
c0ebe874
RS
12830 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12831 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12832 If there is a base register we add it to $at before the
12833 lwc1 instructions. If there is a constant we include it
12834 in the lwc1 instructions. */
12835 used_at = 1;
12836 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12837 if (expr1.X_add_number < -0x8000
12838 || expr1.X_add_number >= 0x8000 - 4)
12839 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12840 load_got_offset (AT, &offset_expr);
269137b2 12841 load_delay_nop ();
252b5132 12842 if (breg != 0)
67c0d1eb 12843 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12844
12845 /* Set mips_optimize to 2 to avoid inserting an undesired
12846 nop. */
12847 hold_mips_optimize = mips_optimize;
12848 mips_optimize = 2;
4d7206a2 12849
beae10d5 12850 /* Itbl support may require additional care here. */
4d7206a2 12851 relax_start (offset_expr.X_add_symbol);
c0ebe874 12852 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12853 BFD_RELOC_LO16, AT);
4d7206a2 12854 expr1.X_add_number += 4;
c0ebe874 12855 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12856 BFD_RELOC_LO16, AT);
4d7206a2 12857 relax_switch ();
c0ebe874 12858 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12859 BFD_RELOC_LO16, AT);
4d7206a2 12860 offset_expr.X_add_number += 4;
c0ebe874 12861 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12862 BFD_RELOC_LO16, AT);
4d7206a2 12863 relax_end ();
252b5132 12864
4d7206a2 12865 mips_optimize = hold_mips_optimize;
252b5132 12866 }
0a44bf69 12867 else if (mips_big_got)
252b5132 12868 {
67c0d1eb 12869 int gpdelay;
252b5132
RH
12870
12871 /* If this is a reference to an external symbol, we want
12872 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12873 addu $at,$at,$gp
12874 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12875 nop
c0ebe874
RS
12876 <op> op[0],0($at)
12877 <op> op[0]+1,4($at)
252b5132
RH
12878 Otherwise we want
12879 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12880 nop
c0ebe874
RS
12881 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12882 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12883 If there is a base register we add it to $at before the
12884 lwc1 instructions. If there is a constant we include it
12885 in the lwc1 instructions. */
12886 used_at = 1;
12887 expr1.X_add_number = offset_expr.X_add_number;
12888 offset_expr.X_add_number = 0;
12889 if (expr1.X_add_number < -0x8000
12890 || expr1.X_add_number >= 0x8000 - 4)
12891 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12892 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12893 relax_start (offset_expr.X_add_symbol);
df58fc94 12894 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12895 AT, BFD_RELOC_MIPS_GOT_HI16);
12896 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12897 AT, AT, mips_gp_register);
67c0d1eb 12898 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12899 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12900 load_delay_nop ();
252b5132 12901 if (breg != 0)
67c0d1eb 12902 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12903 /* Itbl support may require additional care here. */
c0ebe874 12904 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12905 BFD_RELOC_LO16, AT);
252b5132
RH
12906 expr1.X_add_number += 4;
12907
12908 /* Set mips_optimize to 2 to avoid inserting an undesired
12909 nop. */
12910 hold_mips_optimize = mips_optimize;
12911 mips_optimize = 2;
beae10d5 12912 /* Itbl support may require additional care here. */
c0ebe874 12913 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12914 BFD_RELOC_LO16, AT);
252b5132
RH
12915 mips_optimize = hold_mips_optimize;
12916 expr1.X_add_number -= 4;
12917
4d7206a2
RS
12918 relax_switch ();
12919 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12920 if (gpdelay)
12921 macro_build (NULL, "nop", "");
12922 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12923 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12924 load_delay_nop ();
252b5132 12925 if (breg != 0)
67c0d1eb 12926 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12927 /* Itbl support may require additional care here. */
c0ebe874 12928 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12929 BFD_RELOC_LO16, AT);
4d7206a2 12930 offset_expr.X_add_number += 4;
252b5132
RH
12931
12932 /* Set mips_optimize to 2 to avoid inserting an undesired
12933 nop. */
12934 hold_mips_optimize = mips_optimize;
12935 mips_optimize = 2;
beae10d5 12936 /* Itbl support may require additional care here. */
c0ebe874 12937 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12938 BFD_RELOC_LO16, AT);
252b5132 12939 mips_optimize = hold_mips_optimize;
4d7206a2 12940 relax_end ();
252b5132 12941 }
252b5132
RH
12942 else
12943 abort ();
12944
252b5132 12945 break;
3739860c 12946
dd6a37e7 12947 case M_SAA_AB:
dd6a37e7 12948 s = "saa";
0db377d0 12949 goto saa_saad;
dd6a37e7 12950 case M_SAAD_AB:
dd6a37e7 12951 s = "saad";
0db377d0
MR
12952 saa_saad:
12953 gas_assert (!mips_opts.micromips);
7f3c4072 12954 offbits = 0;
dd6a37e7
AP
12955 fmt = "t,(b)";
12956 goto ld_st;
12957
252b5132
RH
12958 /* New code added to support COPZ instructions.
12959 This code builds table entries out of the macros in mip_opcodes.
12960 R4000 uses interlocks to handle coproc delays.
12961 Other chips (like the R3000) require nops to be inserted for delays.
12962
f72c8c98 12963 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12964 In order to fill delay slots for non-interlocked chips,
12965 we must have a way to specify delays based on the coprocessor.
12966 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12967 What are the side-effects of the cop instruction?
12968 What cache support might we have and what are its effects?
12969 Both coprocessor & memory require delays. how long???
bdaaa2e1 12970 What registers are read/set/modified?
252b5132
RH
12971
12972 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12973 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12974
12975 case M_COP0:
12976 s = "c0";
12977 goto copz;
12978 case M_COP1:
12979 s = "c1";
12980 goto copz;
12981 case M_COP2:
12982 s = "c2";
12983 goto copz;
12984 case M_COP3:
12985 s = "c3";
12986 copz:
df58fc94 12987 gas_assert (!mips_opts.micromips);
252b5132
RH
12988 /* For now we just do C (same as Cz). The parameter will be
12989 stored in insn_opcode by mips_ip. */
c8276761 12990 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12991 break;
252b5132 12992
ea1fb5dc 12993 case M_MOVE:
c0ebe874 12994 move_register (op[0], op[1]);
8fc2e39e 12995 break;
ea1fb5dc 12996
833794fc
MR
12997 case M_MOVEP:
12998 gas_assert (mips_opts.micromips);
12999 gas_assert (mips_opts.insn32);
c0ebe874
RS
13000 move_register (micromips_to_32_reg_h_map1[op[0]],
13001 micromips_to_32_reg_m_map[op[1]]);
13002 move_register (micromips_to_32_reg_h_map2[op[0]],
13003 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
13004 break;
13005
252b5132
RH
13006 case M_DMUL:
13007 dbl = 1;
1a0670f3 13008 /* Fall through. */
252b5132 13009 case M_MUL:
e407c74b 13010 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
13011 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
13012 op[2]);
e407c74b
NC
13013 else
13014 {
c0ebe874
RS
13015 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
13016 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 13017 }
8fc2e39e 13018 break;
252b5132
RH
13019
13020 case M_DMUL_I:
13021 dbl = 1;
1a0670f3 13022 /* Fall through. */
252b5132
RH
13023 case M_MUL_I:
13024 /* The MIPS assembler some times generates shifts and adds. I'm
13025 not trying to be that fancy. GCC should do this for us
13026 anyway. */
8fc2e39e 13027 used_at = 1;
67c0d1eb 13028 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13029 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
13030 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13031 break;
13032
13033 case M_DMULO_I:
13034 dbl = 1;
1a0670f3 13035 /* Fall through. */
252b5132
RH
13036 case M_MULO_I:
13037 imm = 1;
13038 goto do_mulo;
13039
13040 case M_DMULO:
13041 dbl = 1;
1a0670f3 13042 /* Fall through. */
252b5132
RH
13043 case M_MULO:
13044 do_mulo:
7d10b47d 13045 start_noreorder ();
8fc2e39e 13046 used_at = 1;
252b5132 13047 if (imm)
67c0d1eb 13048 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13049 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13050 op[1], imm ? AT : op[2]);
13051 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13052 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13053 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13054 if (mips_trap)
c0ebe874 13055 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13056 else
13057 {
df58fc94
RS
13058 if (mips_opts.micromips)
13059 micromips_label_expr (&label_expr);
13060 else
13061 label_expr.X_add_number = 8;
c0ebe874 13062 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13063 macro_build (NULL, "nop", "");
df58fc94
RS
13064 macro_build (NULL, "break", BRK_FMT, 6);
13065 if (mips_opts.micromips)
13066 micromips_add_label ();
252b5132 13067 }
7d10b47d 13068 end_noreorder ();
c0ebe874 13069 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13070 break;
13071
13072 case M_DMULOU_I:
13073 dbl = 1;
1a0670f3 13074 /* Fall through. */
252b5132
RH
13075 case M_MULOU_I:
13076 imm = 1;
13077 goto do_mulou;
13078
13079 case M_DMULOU:
13080 dbl = 1;
1a0670f3 13081 /* Fall through. */
252b5132
RH
13082 case M_MULOU:
13083 do_mulou:
7d10b47d 13084 start_noreorder ();
8fc2e39e 13085 used_at = 1;
252b5132 13086 if (imm)
67c0d1eb
RS
13087 load_register (AT, &imm_expr, dbl);
13088 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13089 op[1], imm ? AT : op[2]);
df58fc94 13090 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13091 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13092 if (mips_trap)
df58fc94 13093 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13094 else
13095 {
df58fc94
RS
13096 if (mips_opts.micromips)
13097 micromips_label_expr (&label_expr);
13098 else
13099 label_expr.X_add_number = 8;
13100 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13101 macro_build (NULL, "nop", "");
df58fc94
RS
13102 macro_build (NULL, "break", BRK_FMT, 6);
13103 if (mips_opts.micromips)
13104 micromips_add_label ();
252b5132 13105 }
7d10b47d 13106 end_noreorder ();
252b5132
RH
13107 break;
13108
771c7ce4 13109 case M_DROL:
fef14a42 13110 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13111 {
c0ebe874 13112 if (op[0] == op[1])
82dd0097
CD
13113 {
13114 tempreg = AT;
13115 used_at = 1;
13116 }
13117 else
c0ebe874
RS
13118 tempreg = op[0];
13119 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13120 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13121 break;
82dd0097 13122 }
8fc2e39e 13123 used_at = 1;
c0ebe874
RS
13124 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13125 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13126 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13127 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13128 break;
13129
252b5132 13130 case M_ROL:
fef14a42 13131 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13132 {
c0ebe874 13133 if (op[0] == op[1])
82dd0097
CD
13134 {
13135 tempreg = AT;
13136 used_at = 1;
13137 }
13138 else
c0ebe874
RS
13139 tempreg = op[0];
13140 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13141 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13142 break;
82dd0097 13143 }
8fc2e39e 13144 used_at = 1;
c0ebe874
RS
13145 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13146 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13147 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13148 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13149 break;
13150
771c7ce4
TS
13151 case M_DROL_I:
13152 {
13153 unsigned int rot;
e0471c16
TS
13154 const char *l;
13155 const char *rr;
771c7ce4 13156
771c7ce4 13157 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13158 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13159 {
13160 rot = (64 - rot) & 0x3f;
13161 if (rot >= 32)
c0ebe874 13162 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13163 else
c0ebe874 13164 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13165 break;
60b63b72 13166 }
483fc7cd 13167 if (rot == 0)
483fc7cd 13168 {
c0ebe874 13169 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13170 break;
483fc7cd 13171 }
82dd0097 13172 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13173 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13174 rot &= 0x1f;
8fc2e39e 13175 used_at = 1;
c0ebe874
RS
13176 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13177 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13178 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13179 }
13180 break;
13181
252b5132 13182 case M_ROL_I:
771c7ce4
TS
13183 {
13184 unsigned int rot;
13185
771c7ce4 13186 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13187 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13188 {
c0ebe874
RS
13189 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13190 (32 - rot) & 0x1f);
8fc2e39e 13191 break;
60b63b72 13192 }
483fc7cd 13193 if (rot == 0)
483fc7cd 13194 {
c0ebe874 13195 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13196 break;
483fc7cd 13197 }
8fc2e39e 13198 used_at = 1;
c0ebe874
RS
13199 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13200 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13201 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13202 }
13203 break;
13204
13205 case M_DROR:
fef14a42 13206 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13207 {
c0ebe874 13208 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13209 break;
82dd0097 13210 }
8fc2e39e 13211 used_at = 1;
c0ebe874
RS
13212 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13213 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13214 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13215 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13216 break;
13217
13218 case M_ROR:
fef14a42 13219 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13220 {
c0ebe874 13221 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13222 break;
82dd0097 13223 }
8fc2e39e 13224 used_at = 1;
c0ebe874
RS
13225 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13226 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13227 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13228 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13229 break;
13230
771c7ce4
TS
13231 case M_DROR_I:
13232 {
13233 unsigned int rot;
e0471c16
TS
13234 const char *l;
13235 const char *rr;
771c7ce4 13236
771c7ce4 13237 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13238 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13239 {
13240 if (rot >= 32)
c0ebe874 13241 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13242 else
c0ebe874 13243 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13244 break;
82dd0097 13245 }
483fc7cd 13246 if (rot == 0)
483fc7cd 13247 {
c0ebe874 13248 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13249 break;
483fc7cd 13250 }
91d6fa6a 13251 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13252 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13253 rot &= 0x1f;
8fc2e39e 13254 used_at = 1;
c0ebe874
RS
13255 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13256 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13257 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13258 }
13259 break;
13260
252b5132 13261 case M_ROR_I:
771c7ce4
TS
13262 {
13263 unsigned int rot;
13264
771c7ce4 13265 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13266 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13267 {
c0ebe874 13268 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13269 break;
82dd0097 13270 }
483fc7cd 13271 if (rot == 0)
483fc7cd 13272 {
c0ebe874 13273 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13274 break;
483fc7cd 13275 }
8fc2e39e 13276 used_at = 1;
c0ebe874
RS
13277 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13278 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13279 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13280 }
252b5132
RH
13281 break;
13282
252b5132 13283 case M_SEQ:
c0ebe874
RS
13284 if (op[1] == 0)
13285 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13286 else if (op[2] == 0)
13287 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13288 else
13289 {
c0ebe874
RS
13290 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13291 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13292 }
8fc2e39e 13293 break;
252b5132
RH
13294
13295 case M_SEQ_I:
b0e6f033 13296 if (imm_expr.X_add_number == 0)
252b5132 13297 {
c0ebe874 13298 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13299 break;
252b5132 13300 }
c0ebe874 13301 if (op[1] == 0)
252b5132 13302 {
1661c76c 13303 as_warn (_("instruction %s: result is always false"),
252b5132 13304 ip->insn_mo->name);
c0ebe874 13305 move_register (op[0], 0);
8fc2e39e 13306 break;
252b5132 13307 }
dd3cbb7e
NC
13308 if (CPU_HAS_SEQ (mips_opts.arch)
13309 && -512 <= imm_expr.X_add_number
13310 && imm_expr.X_add_number < 512)
13311 {
c0ebe874 13312 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13313 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13314 break;
13315 }
b0e6f033 13316 if (imm_expr.X_add_number >= 0
252b5132 13317 && imm_expr.X_add_number < 0x10000)
c0ebe874 13318 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13319 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13320 && imm_expr.X_add_number < 0)
13321 {
13322 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13323 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13324 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13325 }
dd3cbb7e
NC
13326 else if (CPU_HAS_SEQ (mips_opts.arch))
13327 {
13328 used_at = 1;
bad1aba3 13329 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13330 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13331 break;
13332 }
252b5132
RH
13333 else
13334 {
bad1aba3 13335 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13336 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13337 used_at = 1;
13338 }
c0ebe874 13339 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13340 break;
252b5132 13341
c0ebe874 13342 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13343 s = "slt";
13344 goto sge;
13345 case M_SGEU:
13346 s = "sltu";
13347 sge:
c0ebe874
RS
13348 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13349 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13350 break;
252b5132 13351
c0ebe874 13352 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13353 case M_SGEU_I:
b0e6f033 13354 if (imm_expr.X_add_number >= -0x8000
252b5132 13355 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13356 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13357 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13358 else
13359 {
bad1aba3 13360 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13361 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13362 op[0], op[1], AT);
252b5132
RH
13363 used_at = 1;
13364 }
c0ebe874 13365 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13366 break;
252b5132 13367
c0ebe874 13368 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13369 s = "slt";
13370 goto sgt;
13371 case M_SGTU:
13372 s = "sltu";
13373 sgt:
c0ebe874 13374 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13375 break;
252b5132 13376
c0ebe874 13377 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13378 s = "slt";
13379 goto sgti;
13380 case M_SGTU_I:
13381 s = "sltu";
13382 sgti:
8fc2e39e 13383 used_at = 1;
bad1aba3 13384 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13385 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13386 break;
13387
c0ebe874 13388 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13389 s = "slt";
13390 goto sle;
13391 case M_SLEU:
13392 s = "sltu";
13393 sle:
c0ebe874
RS
13394 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13395 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13396 break;
252b5132 13397
c0ebe874 13398 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13399 s = "slt";
13400 goto slei;
13401 case M_SLEU_I:
13402 s = "sltu";
13403 slei:
8fc2e39e 13404 used_at = 1;
bad1aba3 13405 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13406 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13407 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13408 break;
13409
13410 case M_SLT_I:
b0e6f033 13411 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13412 && imm_expr.X_add_number < 0x8000)
13413 {
c0ebe874
RS
13414 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13415 BFD_RELOC_LO16);
8fc2e39e 13416 break;
252b5132 13417 }
8fc2e39e 13418 used_at = 1;
bad1aba3 13419 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13420 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13421 break;
13422
13423 case M_SLTU_I:
b0e6f033 13424 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13425 && imm_expr.X_add_number < 0x8000)
13426 {
c0ebe874 13427 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13428 BFD_RELOC_LO16);
8fc2e39e 13429 break;
252b5132 13430 }
8fc2e39e 13431 used_at = 1;
bad1aba3 13432 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13433 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13434 break;
13435
13436 case M_SNE:
c0ebe874
RS
13437 if (op[1] == 0)
13438 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13439 else if (op[2] == 0)
13440 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13441 else
13442 {
c0ebe874
RS
13443 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13444 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13445 }
8fc2e39e 13446 break;
252b5132
RH
13447
13448 case M_SNE_I:
b0e6f033 13449 if (imm_expr.X_add_number == 0)
252b5132 13450 {
c0ebe874 13451 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13452 break;
252b5132 13453 }
c0ebe874 13454 if (op[1] == 0)
252b5132 13455 {
1661c76c 13456 as_warn (_("instruction %s: result is always true"),
252b5132 13457 ip->insn_mo->name);
bad1aba3 13458 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13459 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13460 break;
252b5132 13461 }
dd3cbb7e
NC
13462 if (CPU_HAS_SEQ (mips_opts.arch)
13463 && -512 <= imm_expr.X_add_number
13464 && imm_expr.X_add_number < 512)
13465 {
c0ebe874 13466 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13467 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13468 break;
13469 }
b0e6f033 13470 if (imm_expr.X_add_number >= 0
252b5132
RH
13471 && imm_expr.X_add_number < 0x10000)
13472 {
c0ebe874
RS
13473 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13474 BFD_RELOC_LO16);
252b5132 13475 }
b0e6f033 13476 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13477 && imm_expr.X_add_number < 0)
13478 {
13479 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13480 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13481 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13482 }
dd3cbb7e
NC
13483 else if (CPU_HAS_SEQ (mips_opts.arch))
13484 {
13485 used_at = 1;
bad1aba3 13486 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13487 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13488 break;
13489 }
252b5132
RH
13490 else
13491 {
bad1aba3 13492 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13493 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13494 used_at = 1;
13495 }
c0ebe874 13496 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13497 break;
252b5132 13498
df58fc94
RS
13499 case M_SUB_I:
13500 s = "addi";
13501 s2 = "sub";
13502 goto do_subi;
13503 case M_SUBU_I:
13504 s = "addiu";
13505 s2 = "subu";
13506 goto do_subi;
252b5132
RH
13507 case M_DSUB_I:
13508 dbl = 1;
df58fc94
RS
13509 s = "daddi";
13510 s2 = "dsub";
13511 if (!mips_opts.micromips)
13512 goto do_subi;
b0e6f033 13513 if (imm_expr.X_add_number > -0x200
df58fc94 13514 && imm_expr.X_add_number <= 0x200)
252b5132 13515 {
b0e6f033
RS
13516 macro_build (NULL, s, "t,r,.", op[0], op[1],
13517 (int) -imm_expr.X_add_number);
8fc2e39e 13518 break;
252b5132 13519 }
df58fc94 13520 goto do_subi_i;
252b5132
RH
13521 case M_DSUBU_I:
13522 dbl = 1;
df58fc94
RS
13523 s = "daddiu";
13524 s2 = "dsubu";
13525 do_subi:
b0e6f033 13526 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13527 && imm_expr.X_add_number <= 0x8000)
13528 {
13529 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13530 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13531 break;
252b5132 13532 }
df58fc94 13533 do_subi_i:
8fc2e39e 13534 used_at = 1;
67c0d1eb 13535 load_register (AT, &imm_expr, dbl);
c0ebe874 13536 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13537 break;
13538
13539 case M_TEQ_I:
13540 s = "teq";
13541 goto trap;
13542 case M_TGE_I:
13543 s = "tge";
13544 goto trap;
13545 case M_TGEU_I:
13546 s = "tgeu";
13547 goto trap;
13548 case M_TLT_I:
13549 s = "tlt";
13550 goto trap;
13551 case M_TLTU_I:
13552 s = "tltu";
13553 goto trap;
13554 case M_TNE_I:
13555 s = "tne";
13556 trap:
8fc2e39e 13557 used_at = 1;
bad1aba3 13558 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13559 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13560 break;
13561
252b5132 13562 case M_TRUNCWS:
43841e91 13563 case M_TRUNCWD:
df58fc94 13564 gas_assert (!mips_opts.micromips);
0aa27725 13565 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13566 used_at = 1;
252b5132
RH
13567
13568 /*
13569 * Is the double cfc1 instruction a bug in the mips assembler;
13570 * or is there a reason for it?
13571 */
7d10b47d 13572 start_noreorder ();
c0ebe874
RS
13573 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13574 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13575 macro_build (NULL, "nop", "");
252b5132 13576 expr1.X_add_number = 3;
c0ebe874 13577 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13578 expr1.X_add_number = 2;
67c0d1eb
RS
13579 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13580 macro_build (NULL, "ctc1", "t,G", AT, RA);
13581 macro_build (NULL, "nop", "");
13582 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13583 op[0], op[1]);
13584 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13585 macro_build (NULL, "nop", "");
7d10b47d 13586 end_noreorder ();
252b5132
RH
13587 break;
13588
f2ae14a1 13589 case M_ULH_AB:
252b5132 13590 s = "lb";
df58fc94
RS
13591 s2 = "lbu";
13592 off = 1;
13593 goto uld_st;
f2ae14a1 13594 case M_ULHU_AB:
252b5132 13595 s = "lbu";
df58fc94
RS
13596 s2 = "lbu";
13597 off = 1;
13598 goto uld_st;
f2ae14a1 13599 case M_ULW_AB:
df58fc94
RS
13600 s = "lwl";
13601 s2 = "lwr";
7f3c4072 13602 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13603 off = 3;
13604 goto uld_st;
f2ae14a1 13605 case M_ULD_AB:
252b5132
RH
13606 s = "ldl";
13607 s2 = "ldr";
7f3c4072 13608 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13609 off = 7;
df58fc94 13610 goto uld_st;
f2ae14a1 13611 case M_USH_AB:
df58fc94
RS
13612 s = "sb";
13613 s2 = "sb";
13614 off = 1;
13615 ust = 1;
13616 goto uld_st;
f2ae14a1 13617 case M_USW_AB:
df58fc94
RS
13618 s = "swl";
13619 s2 = "swr";
7f3c4072 13620 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13621 off = 3;
df58fc94
RS
13622 ust = 1;
13623 goto uld_st;
f2ae14a1 13624 case M_USD_AB:
df58fc94
RS
13625 s = "sdl";
13626 s2 = "sdr";
7f3c4072 13627 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13628 off = 7;
13629 ust = 1;
13630
13631 uld_st:
c0ebe874 13632 breg = op[2];
f2ae14a1 13633 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13634 ep = &offset_expr;
13635 expr1.X_add_number = 0;
f2ae14a1 13636 if (large_offset)
df58fc94
RS
13637 {
13638 used_at = 1;
13639 tempreg = AT;
f2ae14a1
RS
13640 if (small_offset_p (0, align, 16))
13641 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13642 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13643 else
13644 {
13645 load_address (tempreg, ep, &used_at);
13646 if (breg != 0)
13647 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13648 tempreg, tempreg, breg);
13649 }
13650 offset_reloc[0] = BFD_RELOC_LO16;
13651 offset_reloc[1] = BFD_RELOC_UNUSED;
13652 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13653 breg = tempreg;
c0ebe874 13654 tempreg = op[0];
df58fc94
RS
13655 ep = &expr1;
13656 }
c0ebe874 13657 else if (!ust && op[0] == breg)
8fc2e39e
TS
13658 {
13659 used_at = 1;
13660 tempreg = AT;
13661 }
252b5132 13662 else
c0ebe874 13663 tempreg = op[0];
af22f5b2 13664
df58fc94
RS
13665 if (off == 1)
13666 goto ulh_sh;
252b5132 13667
90ecf173 13668 if (!target_big_endian)
df58fc94 13669 ep->X_add_number += off;
f2ae14a1 13670 if (offbits == 12)
c8276761 13671 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13672 else
13673 macro_build (ep, s, "t,o(b)", tempreg, -1,
13674 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13675
90ecf173 13676 if (!target_big_endian)
df58fc94 13677 ep->X_add_number -= off;
252b5132 13678 else
df58fc94 13679 ep->X_add_number += off;
f2ae14a1 13680 if (offbits == 12)
df58fc94 13681 macro_build (NULL, s2, "t,~(b)",
c8276761 13682 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13683 else
13684 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13685 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13686
df58fc94 13687 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13688 if (!ust && op[0] != tempreg)
df58fc94
RS
13689 {
13690 /* Protect second load's delay slot. */
13691 load_delay_nop ();
c0ebe874 13692 move_register (op[0], tempreg);
df58fc94 13693 }
8fc2e39e 13694 break;
252b5132 13695
df58fc94 13696 ulh_sh:
d6bc6245 13697 used_at = 1;
df58fc94
RS
13698 if (target_big_endian == ust)
13699 ep->X_add_number += off;
c0ebe874 13700 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13701 macro_build (ep, s, "t,o(b)", tempreg, -1,
13702 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13703
13704 /* For halfword transfers we need a temporary register to shuffle
13705 bytes. Unfortunately for M_USH_A we have none available before
13706 the next store as AT holds the base address. We deal with this
13707 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13708 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13709 if (ust)
c0ebe874 13710 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13711
13712 if (target_big_endian == ust)
13713 ep->X_add_number -= off;
252b5132 13714 else
df58fc94 13715 ep->X_add_number += off;
f2ae14a1
RS
13716 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13717 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13718
df58fc94 13719 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13720 if (ust && large_offset)
df58fc94
RS
13721 {
13722 if (target_big_endian)
13723 ep->X_add_number += off;
13724 else
13725 ep->X_add_number -= off;
f2ae14a1
RS
13726 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13727 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13728 }
13729 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13730 if (!ust || large_offset)
df58fc94 13731 {
c0ebe874 13732 tempreg = !large_offset ? AT : op[0];
df58fc94 13733 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13734 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13735 }
252b5132
RH
13736 break;
13737
13738 default:
13739 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13740 are added dynamically. */
1661c76c 13741 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13742 break;
13743 }
741fe287 13744 if (!mips_opts.at && used_at)
1661c76c 13745 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13746}
13747
13748/* Implement macros in mips16 mode. */
13749
13750static void
17a2f251 13751mips16_macro (struct mips_cl_insn *ip)
252b5132 13752{
c0ebe874 13753 const struct mips_operand_array *operands;
252b5132 13754 int mask;
c0ebe874 13755 int tmp;
252b5132
RH
13756 expressionS expr1;
13757 int dbl;
13758 const char *s, *s2, *s3;
c0ebe874
RS
13759 unsigned int op[MAX_OPERANDS];
13760 unsigned int i;
252b5132
RH
13761
13762 mask = ip->insn_mo->mask;
13763
c0ebe874
RS
13764 operands = insn_operands (ip);
13765 for (i = 0; i < MAX_OPERANDS; i++)
13766 if (operands->operand[i])
13767 op[i] = insn_extract_operand (ip, operands->operand[i]);
13768 else
13769 op[i] = -1;
252b5132 13770
252b5132
RH
13771 expr1.X_op = O_constant;
13772 expr1.X_op_symbol = NULL;
13773 expr1.X_add_symbol = NULL;
13774 expr1.X_add_number = 1;
13775
13776 dbl = 0;
13777
13778 switch (mask)
13779 {
13780 default:
b37df7c4 13781 abort ();
252b5132
RH
13782
13783 case M_DDIV_3:
13784 dbl = 1;
1a0670f3 13785 /* Fall through. */
252b5132
RH
13786 case M_DIV_3:
13787 s = "mflo";
13788 goto do_div3;
13789 case M_DREM_3:
13790 dbl = 1;
1a0670f3 13791 /* Fall through. */
252b5132
RH
13792 case M_REM_3:
13793 s = "mfhi";
13794 do_div3:
7d10b47d 13795 start_noreorder ();
d8722d76 13796 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13797 expr1.X_add_number = 2;
c0ebe874 13798 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13799 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13800
252b5132
RH
13801 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13802 since that causes an overflow. We should do that as well,
13803 but I don't see how to do the comparisons without a temporary
13804 register. */
7d10b47d 13805 end_noreorder ();
c0ebe874 13806 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13807 break;
13808
13809 case M_DIVU_3:
13810 s = "divu";
13811 s2 = "mflo";
13812 goto do_divu3;
13813 case M_REMU_3:
13814 s = "divu";
13815 s2 = "mfhi";
13816 goto do_divu3;
13817 case M_DDIVU_3:
13818 s = "ddivu";
13819 s2 = "mflo";
13820 goto do_divu3;
13821 case M_DREMU_3:
13822 s = "ddivu";
13823 s2 = "mfhi";
13824 do_divu3:
7d10b47d 13825 start_noreorder ();
d8722d76 13826 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13827 expr1.X_add_number = 2;
c0ebe874 13828 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13829 macro_build (NULL, "break", "6", 7);
7d10b47d 13830 end_noreorder ();
c0ebe874 13831 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13832 break;
13833
13834 case M_DMUL:
13835 dbl = 1;
1a0670f3 13836 /* Fall through. */
252b5132 13837 case M_MUL:
c0ebe874
RS
13838 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13839 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13840 break;
252b5132
RH
13841
13842 case M_DSUBU_I:
13843 dbl = 1;
13844 goto do_subu;
13845 case M_SUBU_I:
13846 do_subu:
252b5132 13847 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13848 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13849 break;
13850
13851 case M_SUBU_I_2:
252b5132 13852 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13853 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13854 break;
13855
13856 case M_DSUBU_I_2:
252b5132 13857 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13858 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13859 break;
13860
13861 case M_BEQ:
13862 s = "cmp";
13863 s2 = "bteqz";
13864 goto do_branch;
13865 case M_BNE:
13866 s = "cmp";
13867 s2 = "btnez";
13868 goto do_branch;
13869 case M_BLT:
13870 s = "slt";
13871 s2 = "btnez";
13872 goto do_branch;
13873 case M_BLTU:
13874 s = "sltu";
13875 s2 = "btnez";
13876 goto do_branch;
13877 case M_BLE:
13878 s = "slt";
13879 s2 = "bteqz";
13880 goto do_reverse_branch;
13881 case M_BLEU:
13882 s = "sltu";
13883 s2 = "bteqz";
13884 goto do_reverse_branch;
13885 case M_BGE:
13886 s = "slt";
13887 s2 = "bteqz";
13888 goto do_branch;
13889 case M_BGEU:
13890 s = "sltu";
13891 s2 = "bteqz";
13892 goto do_branch;
13893 case M_BGT:
13894 s = "slt";
13895 s2 = "btnez";
13896 goto do_reverse_branch;
13897 case M_BGTU:
13898 s = "sltu";
13899 s2 = "btnez";
13900
13901 do_reverse_branch:
c0ebe874
RS
13902 tmp = op[1];
13903 op[1] = op[0];
13904 op[0] = tmp;
252b5132
RH
13905
13906 do_branch:
c0ebe874 13907 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13908 macro_build (&offset_expr, s2, "p");
252b5132
RH
13909 break;
13910
13911 case M_BEQ_I:
13912 s = "cmpi";
13913 s2 = "bteqz";
13914 s3 = "x,U";
13915 goto do_branch_i;
13916 case M_BNE_I:
13917 s = "cmpi";
13918 s2 = "btnez";
13919 s3 = "x,U";
13920 goto do_branch_i;
13921 case M_BLT_I:
13922 s = "slti";
13923 s2 = "btnez";
13924 s3 = "x,8";
13925 goto do_branch_i;
13926 case M_BLTU_I:
13927 s = "sltiu";
13928 s2 = "btnez";
13929 s3 = "x,8";
13930 goto do_branch_i;
13931 case M_BLE_I:
13932 s = "slti";
13933 s2 = "btnez";
13934 s3 = "x,8";
13935 goto do_addone_branch_i;
13936 case M_BLEU_I:
13937 s = "sltiu";
13938 s2 = "btnez";
13939 s3 = "x,8";
13940 goto do_addone_branch_i;
13941 case M_BGE_I:
13942 s = "slti";
13943 s2 = "bteqz";
13944 s3 = "x,8";
13945 goto do_branch_i;
13946 case M_BGEU_I:
13947 s = "sltiu";
13948 s2 = "bteqz";
13949 s3 = "x,8";
13950 goto do_branch_i;
13951 case M_BGT_I:
13952 s = "slti";
13953 s2 = "bteqz";
13954 s3 = "x,8";
13955 goto do_addone_branch_i;
13956 case M_BGTU_I:
13957 s = "sltiu";
13958 s2 = "bteqz";
13959 s3 = "x,8";
13960
13961 do_addone_branch_i:
252b5132
RH
13962 ++imm_expr.X_add_number;
13963
13964 do_branch_i:
c0ebe874 13965 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13966 macro_build (&offset_expr, s2, "p");
252b5132
RH
13967 break;
13968
13969 case M_ABS:
13970 expr1.X_add_number = 0;
c0ebe874
RS
13971 macro_build (&expr1, "slti", "x,8", op[1]);
13972 if (op[0] != op[1])
13973 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13974 expr1.X_add_number = 2;
67c0d1eb 13975 macro_build (&expr1, "bteqz", "p");
c0ebe874 13976 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13977 break;
252b5132
RH
13978 }
13979}
13980
14daeee3
RS
13981/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13982 opcode bits in *OPCODE_EXTRA. */
13983
13984static struct mips_opcode *
13985mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13986 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13987{
13988 char *name, *dot, *p;
13989 unsigned int mask, suffix;
da8bca91 13990 ssize_t opend;
14daeee3
RS
13991 struct mips_opcode *insn;
13992
13993 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13994 name = xstrndup (start, length);
14daeee3
RS
13995
13996 /* Look up the instruction as-is. */
13997 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13998 if (insn)
e1fa0163 13999 goto end;
14daeee3
RS
14000
14001 dot = strchr (name, '.');
14002 if (dot && dot[1])
14003 {
14004 /* Try to interpret the text after the dot as a VU0 channel suffix. */
14005 p = mips_parse_vu0_channels (dot + 1, &mask);
14006 if (*p == 0 && mask != 0)
14007 {
14008 *dot = 0;
14009 insn = (struct mips_opcode *) hash_find (hash, name);
14010 *dot = '.';
14011 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
14012 {
14013 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 14014 goto end;
14daeee3
RS
14015 }
14016 }
14017 }
14018
14019 if (mips_opts.micromips)
14020 {
14021 /* See if there's an instruction size override suffix,
14022 either `16' or `32', at the end of the mnemonic proper,
14023 that defines the operation, i.e. before the first `.'
14024 character if any. Strip it and retry. */
14025 opend = dot != NULL ? dot - name : length;
14026 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
14027 suffix = 2;
14028 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
14029 suffix = 4;
14030 else
14031 suffix = 0;
14032 if (suffix)
14033 {
39334a61 14034 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 14035 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 14036 if (insn)
14daeee3
RS
14037 {
14038 forced_insn_length = suffix;
e1fa0163 14039 goto end;
14daeee3
RS
14040 }
14041 }
14042 }
14043
e1fa0163
NC
14044 insn = NULL;
14045 end:
14046 free (name);
14047 return insn;
14daeee3
RS
14048}
14049
77bd4346 14050/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14051 is a macro, set imm_expr and offset_expr to the values associated
14052 with "I" and "A" operands respectively. Otherwise store the value
14053 of the relocatable field (if any) in offset_expr. In both cases
14054 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14055
14056static void
60f20e8b 14057mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14058{
60f20e8b 14059 const struct mips_opcode *first, *past;
df58fc94 14060 struct hash_control *hash;
a92713e6 14061 char format;
14daeee3 14062 size_t end;
a92713e6 14063 struct mips_operand_token *tokens;
14daeee3 14064 unsigned int opcode_extra;
252b5132 14065
df58fc94
RS
14066 if (mips_opts.micromips)
14067 {
14068 hash = micromips_op_hash;
14069 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14070 }
14071 else
14072 {
14073 hash = op_hash;
14074 past = &mips_opcodes[NUMOPCODES];
14075 }
14076 forced_insn_length = 0;
14daeee3 14077 opcode_extra = 0;
252b5132 14078
df58fc94 14079 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14080 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14081 continue;
bdaaa2e1 14082
60f20e8b
RS
14083 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14084 if (first == NULL)
252b5132 14085 {
1661c76c 14086 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14087 return;
252b5132
RH
14088 }
14089
60f20e8b 14090 if (strcmp (first->name, "li.s") == 0)
a92713e6 14091 format = 'f';
60f20e8b 14092 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14093 format = 'd';
14094 else
14095 format = 0;
14096 tokens = mips_parse_arguments (str + end, format);
14097 if (!tokens)
14098 return;
14099
60f20e8b
RS
14100 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14101 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14102 set_insn_error (0, _("invalid operands"));
df58fc94 14103
e3de51ce 14104 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14105}
14106
77bd4346
RS
14107/* As for mips_ip, but used when assembling MIPS16 code.
14108 Also set forced_insn_length to the resulting instruction size in
14109 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14110
14111static void
60f20e8b 14112mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14113{
1a00e612 14114 char *end, *s, c;
60f20e8b 14115 struct mips_opcode *first;
a92713e6 14116 struct mips_operand_token *tokens;
3fb49709 14117 unsigned int l;
252b5132 14118
25499ac7 14119 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14120 ;
1a00e612
RS
14121 end = s;
14122 c = *end;
3fb49709
MR
14123
14124 l = 0;
1a00e612 14125 switch (c)
252b5132
RH
14126 {
14127 case '\0':
14128 break;
14129
14130 case ' ':
1a00e612 14131 s++;
252b5132
RH
14132 break;
14133
14134 case '.':
3fb49709
MR
14135 s++;
14136 if (*s == 't')
252b5132 14137 {
3fb49709
MR
14138 l = 2;
14139 s++;
252b5132 14140 }
3fb49709 14141 else if (*s == 'e')
252b5132 14142 {
3fb49709
MR
14143 l = 4;
14144 s++;
252b5132 14145 }
3fb49709
MR
14146 if (*s == '\0')
14147 break;
14148 else if (*s++ == ' ')
14149 break;
1661c76c 14150 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14151 return;
14152 }
3fb49709 14153 forced_insn_length = l;
252b5132 14154
1a00e612 14155 *end = 0;
60f20e8b 14156 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14157 *end = c;
14158
60f20e8b 14159 if (!first)
252b5132 14160 {
1661c76c 14161 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14162 return;
14163 }
14164
a92713e6
RS
14165 tokens = mips_parse_arguments (s, 0);
14166 if (!tokens)
14167 return;
14168
60f20e8b 14169 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14170 set_insn_error (0, _("invalid operands"));
252b5132 14171
e3de51ce 14172 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14173}
14174
b886a2ab
RS
14175/* Marshal immediate value VAL for an extended MIPS16 instruction.
14176 NBITS is the number of significant bits in VAL. */
14177
14178static unsigned long
14179mips16_immed_extend (offsetT val, unsigned int nbits)
14180{
14181 int extval;
25499ac7
MR
14182
14183 extval = 0;
14184 val &= (1U << nbits) - 1;
14185 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14186 {
14187 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14188 val &= 0x1f;
14189 }
14190 else if (nbits == 15)
14191 {
14192 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14193 val &= 0xf;
14194 }
25499ac7 14195 else if (nbits == 6)
b886a2ab
RS
14196 {
14197 extval = ((val & 0x1f) << 6) | (val & 0x20);
14198 val = 0;
14199 }
14200 return (extval << 16) | val;
14201}
14202
3ccad066
RS
14203/* Like decode_mips16_operand, but require the operand to be defined and
14204 require it to be an integer. */
14205
14206static const struct mips_int_operand *
14207mips16_immed_operand (int type, bfd_boolean extended_p)
14208{
14209 const struct mips_operand *operand;
14210
14211 operand = decode_mips16_operand (type, extended_p);
14212 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14213 abort ();
14214 return (const struct mips_int_operand *) operand;
14215}
14216
14217/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14218
14219static bfd_boolean
14220mips16_immed_in_range_p (const struct mips_int_operand *operand,
14221 bfd_reloc_code_real_type reloc, offsetT sval)
14222{
14223 int min_val, max_val;
14224
14225 min_val = mips_int_operand_min (operand);
14226 max_val = mips_int_operand_max (operand);
14227 if (reloc != BFD_RELOC_UNUSED)
14228 {
14229 if (min_val < 0)
14230 sval = SEXT_16BIT (sval);
14231 else
14232 sval &= 0xffff;
14233 }
14234
14235 return (sval >= min_val
14236 && sval <= max_val
14237 && (sval & ((1 << operand->shift) - 1)) == 0);
14238}
14239
5c04167a
RS
14240/* Install immediate value VAL into MIPS16 instruction *INSN,
14241 extending it if necessary. The instruction in *INSN may
14242 already be extended.
14243
43c0598f
RS
14244 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14245 if none. In the former case, VAL is a 16-bit number with no
14246 defined signedness.
14247
14248 TYPE is the type of the immediate field. USER_INSN_LENGTH
14249 is the length that the user requested, or 0 if none. */
252b5132
RH
14250
14251static void
3b4dbbbf 14252mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14253 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14254 unsigned int user_insn_length, unsigned long *insn)
252b5132 14255{
3ccad066
RS
14256 const struct mips_int_operand *operand;
14257 unsigned int uval, length;
252b5132 14258
3ccad066
RS
14259 operand = mips16_immed_operand (type, FALSE);
14260 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14261 {
14262 /* We need an extended instruction. */
14263 if (user_insn_length == 2)
14264 as_bad_where (file, line, _("invalid unextended operand value"));
14265 else
14266 *insn |= MIPS16_EXTEND;
14267 }
14268 else if (user_insn_length == 4)
14269 {
14270 /* The operand doesn't force an unextended instruction to be extended.
14271 Warn if the user wanted an extended instruction anyway. */
14272 *insn |= MIPS16_EXTEND;
14273 as_warn_where (file, line,
14274 _("extended operand requested but not required"));
14275 }
252b5132 14276
3ccad066
RS
14277 length = mips16_opcode_length (*insn);
14278 if (length == 4)
252b5132 14279 {
3ccad066
RS
14280 operand = mips16_immed_operand (type, TRUE);
14281 if (!mips16_immed_in_range_p (operand, reloc, val))
14282 as_bad_where (file, line,
14283 _("operand value out of range for instruction"));
252b5132 14284 }
3ccad066 14285 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14286 if (length == 2 || operand->root.lsb != 0)
3ccad066 14287 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14288 else
3ccad066 14289 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14290}
14291\f
d6f16593 14292struct percent_op_match
ad8d3bb3 14293{
5e0116d5
RS
14294 const char *str;
14295 bfd_reloc_code_real_type reloc;
d6f16593
MR
14296};
14297
14298static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14299{
5e0116d5 14300 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14301 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14302 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14303 {"%call16", BFD_RELOC_MIPS_CALL16},
14304 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14305 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14306 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14307 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14308 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14309 {"%got", BFD_RELOC_MIPS_GOT16},
14310 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14311 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14312 {"%half", BFD_RELOC_16},
14313 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14314 {"%higher", BFD_RELOC_MIPS_HIGHER},
14315 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14316 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14317 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14318 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14319 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14320 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14321 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14322 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14323 {"%hi", BFD_RELOC_HI16_S},
14324 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14325 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14326};
14327
d6f16593
MR
14328static const struct percent_op_match mips16_percent_op[] =
14329{
14330 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14331 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14332 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14333 {"%got", BFD_RELOC_MIPS16_GOT16},
14334 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14335 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14336 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14337 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14338 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14339 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14340 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14341 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14342 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14343};
14344
252b5132 14345
5e0116d5
RS
14346/* Return true if *STR points to a relocation operator. When returning true,
14347 move *STR over the operator and store its relocation code in *RELOC.
14348 Leave both *STR and *RELOC alone when returning false. */
14349
14350static bfd_boolean
17a2f251 14351parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14352{
d6f16593
MR
14353 const struct percent_op_match *percent_op;
14354 size_t limit, i;
14355
14356 if (mips_opts.mips16)
14357 {
14358 percent_op = mips16_percent_op;
14359 limit = ARRAY_SIZE (mips16_percent_op);
14360 }
14361 else
14362 {
14363 percent_op = mips_percent_op;
14364 limit = ARRAY_SIZE (mips_percent_op);
14365 }
76b3015f 14366
d6f16593 14367 for (i = 0; i < limit; i++)
5e0116d5 14368 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14369 {
3f98094e
DJ
14370 int len = strlen (percent_op[i].str);
14371
14372 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14373 continue;
14374
5e0116d5
RS
14375 *str += strlen (percent_op[i].str);
14376 *reloc = percent_op[i].reloc;
394f9b3a 14377
5e0116d5
RS
14378 /* Check whether the output BFD supports this relocation.
14379 If not, issue an error and fall back on something safe. */
14380 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14381 {
20203fb9 14382 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14383 percent_op[i].str);
01a3f561 14384 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14385 }
5e0116d5 14386 return TRUE;
394f9b3a 14387 }
5e0116d5 14388 return FALSE;
394f9b3a 14389}
ad8d3bb3 14390
ad8d3bb3 14391
5e0116d5
RS
14392/* Parse string STR as a 16-bit relocatable operand. Store the
14393 expression in *EP and the relocations in the array starting
14394 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14395
01a3f561 14396 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14397
5e0116d5 14398static size_t
17a2f251
TS
14399my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14400 char *str)
ad8d3bb3 14401{
5e0116d5
RS
14402 bfd_reloc_code_real_type reversed_reloc[3];
14403 size_t reloc_index, i;
09b8f35a
RS
14404 int crux_depth, str_depth;
14405 char *crux;
5e0116d5
RS
14406
14407 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14408 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14409 of the main expression and with CRUX_DEPTH containing the number
14410 of open brackets at that point. */
14411 reloc_index = -1;
14412 str_depth = 0;
14413 do
fb1b3232 14414 {
09b8f35a
RS
14415 reloc_index++;
14416 crux = str;
14417 crux_depth = str_depth;
14418
14419 /* Skip over whitespace and brackets, keeping count of the number
14420 of brackets. */
14421 while (*str == ' ' || *str == '\t' || *str == '(')
14422 if (*str++ == '(')
14423 str_depth++;
5e0116d5 14424 }
09b8f35a
RS
14425 while (*str == '%'
14426 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14427 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14428
09b8f35a 14429 my_getExpression (ep, crux);
5e0116d5 14430 str = expr_end;
394f9b3a 14431
5e0116d5 14432 /* Match every open bracket. */
09b8f35a 14433 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14434 if (*str++ == ')')
09b8f35a 14435 crux_depth--;
394f9b3a 14436
09b8f35a 14437 if (crux_depth > 0)
20203fb9 14438 as_bad (_("unclosed '('"));
394f9b3a 14439
5e0116d5 14440 expr_end = str;
252b5132 14441
01a3f561 14442 if (reloc_index != 0)
64bdfcaf
RS
14443 {
14444 prev_reloc_op_frag = frag_now;
14445 for (i = 0; i < reloc_index; i++)
14446 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14447 }
fb1b3232 14448
5e0116d5 14449 return reloc_index;
252b5132
RH
14450}
14451
14452static void
17a2f251 14453my_getExpression (expressionS *ep, char *str)
252b5132
RH
14454{
14455 char *save_in;
14456
14457 save_in = input_line_pointer;
14458 input_line_pointer = str;
14459 expression (ep);
14460 expr_end = input_line_pointer;
14461 input_line_pointer = save_in;
252b5132
RH
14462}
14463
6d4af3c2 14464const char *
17a2f251 14465md_atof (int type, char *litP, int *sizeP)
252b5132 14466{
499ac353 14467 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14468}
14469
14470void
17a2f251 14471md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14472{
14473 if (target_big_endian)
14474 number_to_chars_bigendian (buf, val, n);
14475 else
14476 number_to_chars_littleendian (buf, val, n);
14477}
14478\f
e013f690
TS
14479static int support_64bit_objects(void)
14480{
14481 const char **list, **l;
aa3d8fdf 14482 int yes;
e013f690
TS
14483
14484 list = bfd_target_list ();
14485 for (l = list; *l != NULL; l++)
aeffff67
RS
14486 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14487 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14488 break;
aa3d8fdf 14489 yes = (*l != NULL);
e013f690 14490 free (list);
aa3d8fdf 14491 return yes;
e013f690
TS
14492}
14493
316f5878
RS
14494/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14495 NEW_VALUE. Warn if another value was already specified. Note:
14496 we have to defer parsing the -march and -mtune arguments in order
14497 to handle 'from-abi' correctly, since the ABI might be specified
14498 in a later argument. */
14499
14500static void
17a2f251 14501mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14502{
14503 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14504 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14505 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14506 new_value);
14507
14508 *string_ptr = new_value;
14509}
14510
252b5132 14511int
17b9d67d 14512md_parse_option (int c, const char *arg)
252b5132 14513{
c6278170
RS
14514 unsigned int i;
14515
14516 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14517 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14518 {
919731af 14519 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14520 c == mips_ases[i].option_on);
14521 return 1;
14522 }
14523
252b5132
RH
14524 switch (c)
14525 {
119d663a
NC
14526 case OPTION_CONSTRUCT_FLOATS:
14527 mips_disable_float_construction = 0;
14528 break;
bdaaa2e1 14529
119d663a
NC
14530 case OPTION_NO_CONSTRUCT_FLOATS:
14531 mips_disable_float_construction = 1;
14532 break;
bdaaa2e1 14533
252b5132
RH
14534 case OPTION_TRAP:
14535 mips_trap = 1;
14536 break;
14537
14538 case OPTION_BREAK:
14539 mips_trap = 0;
14540 break;
14541
14542 case OPTION_EB:
14543 target_big_endian = 1;
14544 break;
14545
14546 case OPTION_EL:
14547 target_big_endian = 0;
14548 break;
14549
14550 case 'O':
4ffff32f
TS
14551 if (arg == NULL)
14552 mips_optimize = 1;
14553 else if (arg[0] == '0')
14554 mips_optimize = 0;
14555 else if (arg[0] == '1')
252b5132
RH
14556 mips_optimize = 1;
14557 else
14558 mips_optimize = 2;
14559 break;
14560
14561 case 'g':
14562 if (arg == NULL)
14563 mips_debug = 2;
14564 else
14565 mips_debug = atoi (arg);
252b5132
RH
14566 break;
14567
14568 case OPTION_MIPS1:
0b35dfee 14569 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14570 break;
14571
14572 case OPTION_MIPS2:
0b35dfee 14573 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14574 break;
14575
14576 case OPTION_MIPS3:
0b35dfee 14577 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14578 break;
14579
14580 case OPTION_MIPS4:
0b35dfee 14581 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14582 break;
14583
84ea6cf2 14584 case OPTION_MIPS5:
0b35dfee 14585 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14586 break;
14587
e7af610e 14588 case OPTION_MIPS32:
0b35dfee 14589 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14590 break;
14591
af7ee8bf 14592 case OPTION_MIPS32R2:
0b35dfee 14593 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14594 break;
14595
ae52f483 14596 case OPTION_MIPS32R3:
0ae19f05 14597 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14598 break;
14599
14600 case OPTION_MIPS32R5:
0ae19f05 14601 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14602 break;
14603
7361da2c
AB
14604 case OPTION_MIPS32R6:
14605 file_mips_opts.isa = ISA_MIPS32R6;
14606 break;
14607
5f74bc13 14608 case OPTION_MIPS64R2:
0b35dfee 14609 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14610 break;
14611
ae52f483 14612 case OPTION_MIPS64R3:
0ae19f05 14613 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14614 break;
14615
14616 case OPTION_MIPS64R5:
0ae19f05 14617 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14618 break;
14619
7361da2c
AB
14620 case OPTION_MIPS64R6:
14621 file_mips_opts.isa = ISA_MIPS64R6;
14622 break;
14623
84ea6cf2 14624 case OPTION_MIPS64:
0b35dfee 14625 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14626 break;
14627
ec68c924 14628 case OPTION_MTUNE:
316f5878
RS
14629 mips_set_option_string (&mips_tune_string, arg);
14630 break;
ec68c924 14631
316f5878
RS
14632 case OPTION_MARCH:
14633 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14634 break;
14635
14636 case OPTION_M4650:
316f5878
RS
14637 mips_set_option_string (&mips_arch_string, "4650");
14638 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14639 break;
14640
14641 case OPTION_NO_M4650:
14642 break;
14643
14644 case OPTION_M4010:
316f5878
RS
14645 mips_set_option_string (&mips_arch_string, "4010");
14646 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14647 break;
14648
14649 case OPTION_NO_M4010:
14650 break;
14651
14652 case OPTION_M4100:
316f5878
RS
14653 mips_set_option_string (&mips_arch_string, "4100");
14654 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14655 break;
14656
14657 case OPTION_NO_M4100:
14658 break;
14659
252b5132 14660 case OPTION_M3900:
316f5878
RS
14661 mips_set_option_string (&mips_arch_string, "3900");
14662 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14663 break;
bdaaa2e1 14664
252b5132
RH
14665 case OPTION_NO_M3900:
14666 break;
14667
df58fc94 14668 case OPTION_MICROMIPS:
919731af 14669 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14670 {
14671 as_bad (_("-mmicromips cannot be used with -mips16"));
14672 return 0;
14673 }
919731af 14674 file_mips_opts.micromips = 1;
df58fc94
RS
14675 mips_no_prev_insn ();
14676 break;
14677
14678 case OPTION_NO_MICROMIPS:
919731af 14679 file_mips_opts.micromips = 0;
df58fc94
RS
14680 mips_no_prev_insn ();
14681 break;
14682
252b5132 14683 case OPTION_MIPS16:
919731af 14684 if (file_mips_opts.micromips == 1)
df58fc94
RS
14685 {
14686 as_bad (_("-mips16 cannot be used with -micromips"));
14687 return 0;
14688 }
919731af 14689 file_mips_opts.mips16 = 1;
7d10b47d 14690 mips_no_prev_insn ();
252b5132
RH
14691 break;
14692
14693 case OPTION_NO_MIPS16:
919731af 14694 file_mips_opts.mips16 = 0;
7d10b47d 14695 mips_no_prev_insn ();
252b5132
RH
14696 break;
14697
6a32d874
CM
14698 case OPTION_FIX_24K:
14699 mips_fix_24k = 1;
14700 break;
14701
14702 case OPTION_NO_FIX_24K:
14703 mips_fix_24k = 0;
14704 break;
14705
a8d14a88
CM
14706 case OPTION_FIX_RM7000:
14707 mips_fix_rm7000 = 1;
14708 break;
14709
14710 case OPTION_NO_FIX_RM7000:
14711 mips_fix_rm7000 = 0;
14712 break;
14713
c67a084a
NC
14714 case OPTION_FIX_LOONGSON2F_JUMP:
14715 mips_fix_loongson2f_jump = TRUE;
14716 break;
14717
14718 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14719 mips_fix_loongson2f_jump = FALSE;
14720 break;
14721
14722 case OPTION_FIX_LOONGSON2F_NOP:
14723 mips_fix_loongson2f_nop = TRUE;
14724 break;
14725
14726 case OPTION_NO_FIX_LOONGSON2F_NOP:
14727 mips_fix_loongson2f_nop = FALSE;
14728 break;
14729
d766e8ec
RS
14730 case OPTION_FIX_VR4120:
14731 mips_fix_vr4120 = 1;
60b63b72
RS
14732 break;
14733
d766e8ec
RS
14734 case OPTION_NO_FIX_VR4120:
14735 mips_fix_vr4120 = 0;
60b63b72
RS
14736 break;
14737
7d8e00cf
RS
14738 case OPTION_FIX_VR4130:
14739 mips_fix_vr4130 = 1;
14740 break;
14741
14742 case OPTION_NO_FIX_VR4130:
14743 mips_fix_vr4130 = 0;
14744 break;
14745
d954098f
DD
14746 case OPTION_FIX_CN63XXP1:
14747 mips_fix_cn63xxp1 = TRUE;
14748 break;
14749
14750 case OPTION_NO_FIX_CN63XXP1:
14751 mips_fix_cn63xxp1 = FALSE;
14752 break;
14753
4a6a3df4
AO
14754 case OPTION_RELAX_BRANCH:
14755 mips_relax_branch = 1;
14756 break;
14757
14758 case OPTION_NO_RELAX_BRANCH:
14759 mips_relax_branch = 0;
14760 break;
14761
8b10b0b3
MR
14762 case OPTION_IGNORE_BRANCH_ISA:
14763 mips_ignore_branch_isa = TRUE;
14764 break;
14765
14766 case OPTION_NO_IGNORE_BRANCH_ISA:
14767 mips_ignore_branch_isa = FALSE;
14768 break;
14769
833794fc 14770 case OPTION_INSN32:
919731af 14771 file_mips_opts.insn32 = TRUE;
833794fc
MR
14772 break;
14773
14774 case OPTION_NO_INSN32:
919731af 14775 file_mips_opts.insn32 = FALSE;
833794fc
MR
14776 break;
14777
aa6975fb
ILT
14778 case OPTION_MSHARED:
14779 mips_in_shared = TRUE;
14780 break;
14781
14782 case OPTION_MNO_SHARED:
14783 mips_in_shared = FALSE;
14784 break;
14785
aed1a261 14786 case OPTION_MSYM32:
919731af 14787 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14788 break;
14789
14790 case OPTION_MNO_SYM32:
919731af 14791 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14792 break;
14793
252b5132
RH
14794 /* When generating ELF code, we permit -KPIC and -call_shared to
14795 select SVR4_PIC, and -non_shared to select no PIC. This is
14796 intended to be compatible with Irix 5. */
14797 case OPTION_CALL_SHARED:
252b5132 14798 mips_pic = SVR4_PIC;
143d77c5 14799 mips_abicalls = TRUE;
252b5132
RH
14800 break;
14801
861fb55a 14802 case OPTION_CALL_NONPIC:
861fb55a
DJ
14803 mips_pic = NO_PIC;
14804 mips_abicalls = TRUE;
14805 break;
14806
252b5132 14807 case OPTION_NON_SHARED:
252b5132 14808 mips_pic = NO_PIC;
143d77c5 14809 mips_abicalls = FALSE;
252b5132
RH
14810 break;
14811
44075ae2
TS
14812 /* The -xgot option tells the assembler to use 32 bit offsets
14813 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14814 compatibility. */
14815 case OPTION_XGOT:
14816 mips_big_got = 1;
14817 break;
14818
14819 case 'G':
6caf9ef4
TS
14820 g_switch_value = atoi (arg);
14821 g_switch_seen = 1;
252b5132
RH
14822 break;
14823
34ba82a8
TS
14824 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14825 and -mabi=64. */
252b5132 14826 case OPTION_32:
f3ded42a 14827 mips_abi = O32_ABI;
252b5132
RH
14828 break;
14829
e013f690 14830 case OPTION_N32:
316f5878 14831 mips_abi = N32_ABI;
e013f690 14832 break;
252b5132 14833
e013f690 14834 case OPTION_64:
316f5878 14835 mips_abi = N64_ABI;
f43abd2b 14836 if (!support_64bit_objects())
1661c76c 14837 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14838 break;
14839
c97ef257 14840 case OPTION_GP32:
bad1aba3 14841 file_mips_opts.gp = 32;
c97ef257
AH
14842 break;
14843
14844 case OPTION_GP64:
bad1aba3 14845 file_mips_opts.gp = 64;
c97ef257 14846 break;
252b5132 14847
ca4e0257 14848 case OPTION_FP32:
0b35dfee 14849 file_mips_opts.fp = 32;
316f5878
RS
14850 break;
14851
351cdf24
MF
14852 case OPTION_FPXX:
14853 file_mips_opts.fp = 0;
14854 break;
14855
316f5878 14856 case OPTION_FP64:
0b35dfee 14857 file_mips_opts.fp = 64;
ca4e0257
RS
14858 break;
14859
351cdf24
MF
14860 case OPTION_ODD_SPREG:
14861 file_mips_opts.oddspreg = 1;
14862 break;
14863
14864 case OPTION_NO_ODD_SPREG:
14865 file_mips_opts.oddspreg = 0;
14866 break;
14867
037b32b9 14868 case OPTION_SINGLE_FLOAT:
0b35dfee 14869 file_mips_opts.single_float = 1;
037b32b9
AN
14870 break;
14871
14872 case OPTION_DOUBLE_FLOAT:
0b35dfee 14873 file_mips_opts.single_float = 0;
037b32b9
AN
14874 break;
14875
14876 case OPTION_SOFT_FLOAT:
0b35dfee 14877 file_mips_opts.soft_float = 1;
037b32b9
AN
14878 break;
14879
14880 case OPTION_HARD_FLOAT:
0b35dfee 14881 file_mips_opts.soft_float = 0;
037b32b9
AN
14882 break;
14883
252b5132 14884 case OPTION_MABI:
e013f690 14885 if (strcmp (arg, "32") == 0)
316f5878 14886 mips_abi = O32_ABI;
e013f690 14887 else if (strcmp (arg, "o64") == 0)
316f5878 14888 mips_abi = O64_ABI;
e013f690 14889 else if (strcmp (arg, "n32") == 0)
316f5878 14890 mips_abi = N32_ABI;
e013f690
TS
14891 else if (strcmp (arg, "64") == 0)
14892 {
316f5878 14893 mips_abi = N64_ABI;
e013f690 14894 if (! support_64bit_objects())
1661c76c 14895 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14896 "format"));
14897 }
14898 else if (strcmp (arg, "eabi") == 0)
316f5878 14899 mips_abi = EABI_ABI;
e013f690 14900 else
da0e507f
TS
14901 {
14902 as_fatal (_("invalid abi -mabi=%s"), arg);
14903 return 0;
14904 }
252b5132
RH
14905 break;
14906
6b76fefe 14907 case OPTION_M7000_HILO_FIX:
b34976b6 14908 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14909 break;
14910
9ee72ff1 14911 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14912 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14913 break;
14914
ecb4347a 14915 case OPTION_MDEBUG:
b34976b6 14916 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14917 break;
14918
14919 case OPTION_NO_MDEBUG:
b34976b6 14920 mips_flag_mdebug = FALSE;
ecb4347a 14921 break;
dcd410fe
RO
14922
14923 case OPTION_PDR:
14924 mips_flag_pdr = TRUE;
14925 break;
14926
14927 case OPTION_NO_PDR:
14928 mips_flag_pdr = FALSE;
14929 break;
0a44bf69
RS
14930
14931 case OPTION_MVXWORKS_PIC:
14932 mips_pic = VXWORKS_PIC;
14933 break;
ecb4347a 14934
ba92f887
MR
14935 case OPTION_NAN:
14936 if (strcmp (arg, "2008") == 0)
7361da2c 14937 mips_nan2008 = 1;
ba92f887 14938 else if (strcmp (arg, "legacy") == 0)
7361da2c 14939 mips_nan2008 = 0;
ba92f887
MR
14940 else
14941 {
1661c76c 14942 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14943 return 0;
14944 }
14945 break;
14946
252b5132
RH
14947 default:
14948 return 0;
14949 }
14950
c67a084a
NC
14951 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14952
252b5132
RH
14953 return 1;
14954}
316f5878 14955\f
919731af 14956/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14957
316f5878 14958static void
17a2f251 14959mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14960{
14961 if (info != 0)
fef14a42 14962 mips_tune = info->cpu;
316f5878 14963}
80cc45a5 14964
34ba82a8 14965
252b5132 14966void
17a2f251 14967mips_after_parse_args (void)
e9670677 14968{
fef14a42
TS
14969 const struct mips_cpu_info *arch_info = 0;
14970 const struct mips_cpu_info *tune_info = 0;
14971
e9670677 14972 /* GP relative stuff not working for PE */
6caf9ef4 14973 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14974 {
6caf9ef4 14975 if (g_switch_seen && g_switch_value != 0)
1661c76c 14976 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14977 g_switch_value = 0;
14978 }
14979
cac012d6
AO
14980 if (mips_abi == NO_ABI)
14981 mips_abi = MIPS_DEFAULT_ABI;
14982
919731af 14983 /* The following code determines the architecture.
22923709
RS
14984 Similar code was added to GCC 3.3 (see override_options() in
14985 config/mips/mips.c). The GAS and GCC code should be kept in sync
14986 as much as possible. */
e9670677 14987
316f5878 14988 if (mips_arch_string != 0)
fef14a42 14989 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14990
0b35dfee 14991 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14992 {
0b35dfee 14993 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14994 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14995 the -march selection (if any). */
fef14a42 14996 if (arch_info != 0)
e9670677 14997 {
316f5878
RS
14998 /* -march takes precedence over -mipsN, since it is more descriptive.
14999 There's no harm in specifying both as long as the ISA levels
15000 are the same. */
0b35dfee 15001 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
15002 as_bad (_("-%s conflicts with the other architecture options,"
15003 " which imply -%s"),
0b35dfee 15004 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 15005 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 15006 }
316f5878 15007 else
0b35dfee 15008 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
15009 }
15010
fef14a42 15011 if (arch_info == 0)
95bfe26e
MF
15012 {
15013 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
15014 gas_assert (arch_info);
15015 }
e9670677 15016
fef14a42 15017 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 15018 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
15019 arch_info->name);
15020
919731af 15021 file_mips_opts.arch = arch_info->cpu;
15022 file_mips_opts.isa = arch_info->isa;
15023
15024 /* Set up initial mips_opts state. */
15025 mips_opts = file_mips_opts;
15026
15027 /* The register size inference code is now placed in
15028 file_mips_check_options. */
fef14a42 15029
0b35dfee 15030 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
15031 processor. */
fef14a42
TS
15032 if (mips_tune_string != 0)
15033 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 15034
fef14a42
TS
15035 if (tune_info == 0)
15036 mips_set_tune (arch_info);
15037 else
15038 mips_set_tune (tune_info);
e9670677 15039
ecb4347a 15040 if (mips_flag_mdebug < 0)
e8044f35 15041 mips_flag_mdebug = 0;
e9670677
MR
15042}
15043\f
15044void
17a2f251 15045mips_init_after_args (void)
252b5132
RH
15046{
15047 /* initialize opcodes */
15048 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15049 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15050}
15051
15052long
17a2f251 15053md_pcrel_from (fixS *fixP)
252b5132 15054{
a7ebbfdf
TS
15055 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15056 switch (fixP->fx_r_type)
15057 {
df58fc94
RS
15058 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15059 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15060 /* Return the address of the delay slot. */
15061 return addr + 2;
15062
15063 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15064 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15065 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15066 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15067 case BFD_RELOC_MIPS_21_PCREL_S2:
15068 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15069 case BFD_RELOC_MIPS_JMP:
15070 /* Return the address of the delay slot. */
15071 return addr + 4;
df58fc94 15072
51f6035b
MR
15073 case BFD_RELOC_MIPS_18_PCREL_S3:
15074 /* Return the aligned address of the doubleword containing
15075 the instruction. */
15076 return addr & ~7;
15077
a7ebbfdf
TS
15078 default:
15079 return addr;
15080 }
252b5132
RH
15081}
15082
252b5132
RH
15083/* This is called before the symbol table is processed. In order to
15084 work with gcc when using mips-tfile, we must keep all local labels.
15085 However, in other cases, we want to discard them. If we were
15086 called with -g, but we didn't see any debugging information, it may
15087 mean that gcc is smuggling debugging information through to
15088 mips-tfile, in which case we must generate all local labels. */
15089
15090void
17a2f251 15091mips_frob_file_before_adjust (void)
252b5132
RH
15092{
15093#ifndef NO_ECOFF_DEBUGGING
15094 if (ECOFF_DEBUGGING
15095 && mips_debug != 0
15096 && ! ecoff_debugging_seen)
15097 flag_keep_locals = 1;
15098#endif
15099}
15100
3b91255e 15101/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15102 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15103 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15104 relocation operators.
15105
15106 For our purposes, a %lo() expression matches a %got() or %hi()
15107 expression if:
15108
15109 (a) it refers to the same symbol; and
15110 (b) the offset applied in the %lo() expression is no lower than
15111 the offset applied in the %got() or %hi().
15112
15113 (b) allows us to cope with code like:
15114
15115 lui $4,%hi(foo)
15116 lh $4,%lo(foo+2)($4)
15117
15118 ...which is legal on RELA targets, and has a well-defined behaviour
15119 if the user knows that adding 2 to "foo" will not induce a carry to
15120 the high 16 bits.
15121
15122 When several %lo()s match a particular %got() or %hi(), we use the
15123 following rules to distinguish them:
15124
15125 (1) %lo()s with smaller offsets are a better match than %lo()s with
15126 higher offsets.
15127
15128 (2) %lo()s with no matching %got() or %hi() are better than those
15129 that already have a matching %got() or %hi().
15130
15131 (3) later %lo()s are better than earlier %lo()s.
15132
15133 These rules are applied in order.
15134
15135 (1) means, among other things, that %lo()s with identical offsets are
15136 chosen if they exist.
15137
15138 (2) means that we won't associate several high-part relocations with
15139 the same low-part relocation unless there's no alternative. Having
15140 several high parts for the same low part is a GNU extension; this rule
15141 allows careful users to avoid it.
15142
15143 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15144 with the last high-part relocation being at the front of the list.
15145 It therefore makes sense to choose the last matching low-part
15146 relocation, all other things being equal. It's also easier
15147 to code that way. */
252b5132
RH
15148
15149void
17a2f251 15150mips_frob_file (void)
252b5132
RH
15151{
15152 struct mips_hi_fixup *l;
35903be0 15153 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15154
15155 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15156 {
15157 segment_info_type *seginfo;
3b91255e
RS
15158 bfd_boolean matched_lo_p;
15159 fixS **hi_pos, **lo_pos, **pos;
252b5132 15160
9c2799c2 15161 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15162
5919d012 15163 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15164 there isn't supposed to be a matching LO. Ignore %gots against
15165 constants; we'll report an error for those later. */
738e5348 15166 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15167 && !(l->fixp->fx_addsy
9e009953 15168 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15169 continue;
15170
15171 /* Check quickly whether the next fixup happens to be a matching %lo. */
15172 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15173 continue;
15174
252b5132 15175 seginfo = seg_info (l->seg);
252b5132 15176
3b91255e
RS
15177 /* Set HI_POS to the position of this relocation in the chain.
15178 Set LO_POS to the position of the chosen low-part relocation.
15179 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15180 relocation that matches an immediately-preceding high-part
15181 relocation. */
15182 hi_pos = NULL;
15183 lo_pos = NULL;
15184 matched_lo_p = FALSE;
738e5348 15185 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15186
3b91255e
RS
15187 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15188 {
15189 if (*pos == l->fixp)
15190 hi_pos = pos;
15191
35903be0 15192 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15193 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15194 && (*pos)->fx_offset >= l->fixp->fx_offset
15195 && (lo_pos == NULL
15196 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15197 || (!matched_lo_p
15198 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15199 lo_pos = pos;
15200
15201 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15202 && fixup_has_matching_lo_p (*pos));
15203 }
15204
15205 /* If we found a match, remove the high-part relocation from its
15206 current position and insert it before the low-part relocation.
15207 Make the offsets match so that fixup_has_matching_lo_p()
15208 will return true.
15209
15210 We don't warn about unmatched high-part relocations since some
15211 versions of gcc have been known to emit dead "lui ...%hi(...)"
15212 instructions. */
15213 if (lo_pos != NULL)
15214 {
15215 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15216 if (l->fixp->fx_next != *lo_pos)
252b5132 15217 {
3b91255e
RS
15218 *hi_pos = l->fixp->fx_next;
15219 l->fixp->fx_next = *lo_pos;
15220 *lo_pos = l->fixp;
252b5132 15221 }
252b5132
RH
15222 }
15223 }
15224}
15225
252b5132 15226int
17a2f251 15227mips_force_relocation (fixS *fixp)
252b5132 15228{
ae6063d4 15229 if (generic_force_reloc (fixp))
252b5132
RH
15230 return 1;
15231
df58fc94
RS
15232 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15233 so that the linker relaxation can update targets. */
15234 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15235 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15236 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15237 return 1;
15238
5caa2b07
MR
15239 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15240 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15241 microMIPS symbols so that we can do cross-mode branch diagnostics
15242 and BAL to JALX conversion by the linker. */
15243 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15244 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15245 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15246 && fixp->fx_addsy
15247 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15248 return 1;
15249
7361da2c 15250 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15251 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15252 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15253 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15254 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15255 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15256 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15257 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15258 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15259 return 1;
15260
3e722fb5 15261 return 0;
252b5132
RH
15262}
15263
b416ba9b
MR
15264/* Implement TC_FORCE_RELOCATION_ABS. */
15265
15266bfd_boolean
15267mips_force_relocation_abs (fixS *fixp)
15268{
15269 if (generic_force_reloc (fixp))
15270 return TRUE;
15271
15272 /* These relocations do not have enough bits in the in-place addend
15273 to hold an arbitrary absolute section's offset. */
15274 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15275 return TRUE;
15276
15277 return FALSE;
15278}
15279
b886a2ab
RS
15280/* Read the instruction associated with RELOC from BUF. */
15281
15282static unsigned int
15283read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15284{
15285 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15286 return read_compressed_insn (buf, 4);
15287 else
15288 return read_insn (buf);
15289}
15290
15291/* Write instruction INSN to BUF, given that it has been relocated
15292 by RELOC. */
15293
15294static void
15295write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15296 unsigned long insn)
15297{
15298 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15299 write_compressed_insn (buf, insn, 4);
15300 else
15301 write_insn (buf, insn);
15302}
15303
9d862524
MR
15304/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15305 to a symbol in another ISA mode, which cannot be converted to JALX. */
15306
15307static bfd_boolean
15308fix_bad_cross_mode_jump_p (fixS *fixP)
15309{
15310 unsigned long opcode;
15311 int other;
15312 char *buf;
15313
15314 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15315 return FALSE;
15316
15317 other = S_GET_OTHER (fixP->fx_addsy);
15318 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15319 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15320 switch (fixP->fx_r_type)
15321 {
15322 case BFD_RELOC_MIPS_JMP:
15323 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15324 case BFD_RELOC_MICROMIPS_JMP:
15325 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15326 default:
15327 return FALSE;
15328 }
15329}
15330
15331/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15332 jump to a symbol in the same ISA mode. */
15333
15334static bfd_boolean
15335fix_bad_same_mode_jalx_p (fixS *fixP)
15336{
15337 unsigned long opcode;
15338 int other;
15339 char *buf;
15340
15341 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15342 return FALSE;
15343
15344 other = S_GET_OTHER (fixP->fx_addsy);
15345 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15346 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15347 switch (fixP->fx_r_type)
15348 {
15349 case BFD_RELOC_MIPS_JMP:
15350 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15351 case BFD_RELOC_MIPS16_JMP:
15352 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15353 case BFD_RELOC_MICROMIPS_JMP:
15354 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15355 default:
15356 return FALSE;
15357 }
15358}
15359
15360/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15361 to a symbol whose value plus addend is not aligned according to the
15362 ultimate (after linker relaxation) jump instruction's immediate field
15363 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15364 regular MIPS code, to (1 << 2). */
15365
15366static bfd_boolean
15367fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15368{
15369 bfd_boolean micro_to_mips_p;
15370 valueT val;
15371 int other;
15372
15373 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15374 return FALSE;
15375
15376 other = S_GET_OTHER (fixP->fx_addsy);
15377 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15378 val += fixP->fx_offset;
15379 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15380 && !ELF_ST_IS_MICROMIPS (other));
15381 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15382 != ELF_ST_IS_COMPRESSED (other));
15383}
15384
15385/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15386 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15387 symbols check the ISA bit instead.
15388
15389 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15390 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15391 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15392 may be converted to JALX by the linker. */
9d862524
MR
15393
15394static bfd_boolean
15395fix_bad_cross_mode_branch_p (fixS *fixP)
15396{
15397 bfd_boolean absolute_p;
15398 unsigned long opcode;
15399 asection *symsec;
15400 valueT val;
15401 int other;
15402 char *buf;
15403
8b10b0b3
MR
15404 if (mips_ignore_branch_isa)
15405 return FALSE;
15406
9d862524
MR
15407 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15408 return FALSE;
15409
15410 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15411 absolute_p = bfd_is_abs_section (symsec);
15412
15413 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15414 other = S_GET_OTHER (fixP->fx_addsy);
15415
15416 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15417 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15418 switch (fixP->fx_r_type)
15419 {
15420 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15421 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15422 && opcode != 0x0411);
15423 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15424 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15425 && opcode != 0x4060);
9d862524
MR
15426 case BFD_RELOC_MIPS_21_PCREL_S2:
15427 case BFD_RELOC_MIPS_26_PCREL_S2:
15428 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15429 case BFD_RELOC_MIPS16_16_PCREL_S1:
15430 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15431 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15432 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15433 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15434 default:
15435 abort ();
15436 }
15437}
15438
15439/* Return TRUE if the symbol plus addend associated with a regular MIPS
15440 branch instruction pointed to by FIXP is not aligned according to the
15441 branch instruction's immediate field requirement. We need the addend
15442 to preserve the ISA bit and also the sum must not have bit 2 set. We
15443 must explicitly OR in the ISA bit from symbol annotation as the bit
15444 won't be set in the symbol's value then. */
15445
15446static bfd_boolean
15447fix_bad_misaligned_branch_p (fixS *fixP)
15448{
15449 bfd_boolean absolute_p;
15450 asection *symsec;
15451 valueT isa_bit;
15452 valueT val;
15453 valueT off;
15454 int other;
15455
15456 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15457 return FALSE;
15458
15459 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15460 absolute_p = bfd_is_abs_section (symsec);
15461
15462 val = S_GET_VALUE (fixP->fx_addsy);
15463 other = S_GET_OTHER (fixP->fx_addsy);
15464 off = fixP->fx_offset;
15465
15466 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15467 val |= ELF_ST_IS_COMPRESSED (other);
15468 val += off;
15469 return (val & 0x3) != isa_bit;
15470}
15471
15472/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15473 and its calculated value VAL. */
15474
15475static void
15476fix_validate_branch (fixS *fixP, valueT val)
15477{
15478 if (fixP->fx_done && (val & 0x3) != 0)
15479 as_bad_where (fixP->fx_file, fixP->fx_line,
15480 _("branch to misaligned address (0x%lx)"),
15481 (long) (val + md_pcrel_from (fixP)));
15482 else if (fix_bad_cross_mode_branch_p (fixP))
15483 as_bad_where (fixP->fx_file, fixP->fx_line,
15484 _("branch to a symbol in another ISA mode"));
15485 else if (fix_bad_misaligned_branch_p (fixP))
15486 as_bad_where (fixP->fx_file, fixP->fx_line,
15487 _("branch to misaligned address (0x%lx)"),
15488 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15489 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15490 as_bad_where (fixP->fx_file, fixP->fx_line,
15491 _("cannot encode misaligned addend "
15492 "in the relocatable field (0x%lx)"),
15493 (long) fixP->fx_offset);
15494}
15495
252b5132
RH
15496/* Apply a fixup to the object file. */
15497
94f592af 15498void
55cf6793 15499md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15500{
4d68580a 15501 char *buf;
b886a2ab 15502 unsigned long insn;
a7ebbfdf 15503 reloc_howto_type *howto;
252b5132 15504
d56a8dda
RS
15505 if (fixP->fx_pcrel)
15506 switch (fixP->fx_r_type)
15507 {
15508 case BFD_RELOC_16_PCREL_S2:
c9775dde 15509 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15510 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15511 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15512 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15513 case BFD_RELOC_32_PCREL:
7361da2c
AB
15514 case BFD_RELOC_MIPS_21_PCREL_S2:
15515 case BFD_RELOC_MIPS_26_PCREL_S2:
15516 case BFD_RELOC_MIPS_18_PCREL_S3:
15517 case BFD_RELOC_MIPS_19_PCREL_S2:
15518 case BFD_RELOC_HI16_S_PCREL:
15519 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15520 break;
15521
15522 case BFD_RELOC_32:
15523 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15524 break;
15525
15526 default:
15527 as_bad_where (fixP->fx_file, fixP->fx_line,
15528 _("PC-relative reference to a different section"));
15529 break;
15530 }
15531
15532 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15533 that have no MIPS ELF equivalent. */
15534 if (fixP->fx_r_type != BFD_RELOC_8)
15535 {
15536 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15537 if (!howto)
15538 return;
15539 }
65551fa4 15540
df58fc94
RS
15541 gas_assert (fixP->fx_size == 2
15542 || fixP->fx_size == 4
d56a8dda 15543 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15544 || fixP->fx_r_type == BFD_RELOC_16
15545 || fixP->fx_r_type == BFD_RELOC_64
15546 || fixP->fx_r_type == BFD_RELOC_CTOR
15547 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15548 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15549 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15550 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15551 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15552 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15553
4d68580a 15554 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15555
b1dca8ee
RS
15556 /* Don't treat parts of a composite relocation as done. There are two
15557 reasons for this:
15558
15559 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15560 should nevertheless be emitted if the first part is.
15561
15562 (2) In normal usage, composite relocations are never assembly-time
15563 constants. The easiest way of dealing with the pathological
15564 exceptions is to generate a relocation against STN_UNDEF and
15565 leave everything up to the linker. */
3994f87e 15566 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15567 fixP->fx_done = 1;
15568
15569 switch (fixP->fx_r_type)
15570 {
3f98094e
DJ
15571 case BFD_RELOC_MIPS_TLS_GD:
15572 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15573 case BFD_RELOC_MIPS_TLS_DTPREL32:
15574 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15575 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15576 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15577 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15578 case BFD_RELOC_MIPS_TLS_TPREL32:
15579 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15580 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15581 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15582 case BFD_RELOC_MICROMIPS_TLS_GD:
15583 case BFD_RELOC_MICROMIPS_TLS_LDM:
15584 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15585 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15586 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15587 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15588 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15589 case BFD_RELOC_MIPS16_TLS_GD:
15590 case BFD_RELOC_MIPS16_TLS_LDM:
15591 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15592 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15593 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15594 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15595 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15596 if (fixP->fx_addsy)
15597 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15598 else
15599 as_bad_where (fixP->fx_file, fixP->fx_line,
15600 _("TLS relocation against a constant"));
15601 break;
3f98094e 15602
252b5132 15603 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15604 case BFD_RELOC_MIPS16_JMP:
15605 case BFD_RELOC_MICROMIPS_JMP:
15606 {
15607 int shift;
15608
15609 gas_assert (!fixP->fx_done);
15610
15611 /* Shift is 2, unusually, for microMIPS JALX. */
15612 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15613 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15614 shift = 1;
15615 else
15616 shift = 2;
15617
15618 if (fix_bad_cross_mode_jump_p (fixP))
15619 as_bad_where (fixP->fx_file, fixP->fx_line,
15620 _("jump to a symbol in another ISA mode"));
15621 else if (fix_bad_same_mode_jalx_p (fixP))
15622 as_bad_where (fixP->fx_file, fixP->fx_line,
15623 _("JALX to a symbol in the same ISA mode"));
15624 else if (fix_bad_misaligned_jump_p (fixP, shift))
15625 as_bad_where (fixP->fx_file, fixP->fx_line,
15626 _("jump to misaligned address (0x%lx)"),
15627 (long) (S_GET_VALUE (fixP->fx_addsy)
15628 + fixP->fx_offset));
15629 else if (HAVE_IN_PLACE_ADDENDS
15630 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15631 as_bad_where (fixP->fx_file, fixP->fx_line,
15632 _("cannot encode misaligned addend "
15633 "in the relocatable field (0x%lx)"),
15634 (long) fixP->fx_offset);
15635 }
15636 /* Fall through. */
15637
e369bcce
TS
15638 case BFD_RELOC_MIPS_SHIFT5:
15639 case BFD_RELOC_MIPS_SHIFT6:
15640 case BFD_RELOC_MIPS_GOT_DISP:
15641 case BFD_RELOC_MIPS_GOT_PAGE:
15642 case BFD_RELOC_MIPS_GOT_OFST:
15643 case BFD_RELOC_MIPS_SUB:
15644 case BFD_RELOC_MIPS_INSERT_A:
15645 case BFD_RELOC_MIPS_INSERT_B:
15646 case BFD_RELOC_MIPS_DELETE:
15647 case BFD_RELOC_MIPS_HIGHEST:
15648 case BFD_RELOC_MIPS_HIGHER:
15649 case BFD_RELOC_MIPS_SCN_DISP:
15650 case BFD_RELOC_MIPS_REL16:
15651 case BFD_RELOC_MIPS_RELGOT:
15652 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15653 case BFD_RELOC_HI16:
15654 case BFD_RELOC_HI16_S:
b886a2ab 15655 case BFD_RELOC_LO16:
cdf6fd85 15656 case BFD_RELOC_GPREL16:
252b5132
RH
15657 case BFD_RELOC_MIPS_LITERAL:
15658 case BFD_RELOC_MIPS_CALL16:
15659 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15660 case BFD_RELOC_GPREL32:
252b5132
RH
15661 case BFD_RELOC_MIPS_GOT_HI16:
15662 case BFD_RELOC_MIPS_GOT_LO16:
15663 case BFD_RELOC_MIPS_CALL_HI16:
15664 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15665 case BFD_RELOC_HI16_S_PCREL:
15666 case BFD_RELOC_LO16_PCREL:
252b5132 15667 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15668 case BFD_RELOC_MIPS16_GOT16:
15669 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15670 case BFD_RELOC_MIPS16_HI16:
15671 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15672 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15673 case BFD_RELOC_MICROMIPS_GOT_DISP:
15674 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15675 case BFD_RELOC_MICROMIPS_GOT_OFST:
15676 case BFD_RELOC_MICROMIPS_SUB:
15677 case BFD_RELOC_MICROMIPS_HIGHEST:
15678 case BFD_RELOC_MICROMIPS_HIGHER:
15679 case BFD_RELOC_MICROMIPS_SCN_DISP:
15680 case BFD_RELOC_MICROMIPS_JALR:
15681 case BFD_RELOC_MICROMIPS_HI16:
15682 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15683 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15684 case BFD_RELOC_MICROMIPS_GPREL16:
15685 case BFD_RELOC_MICROMIPS_LITERAL:
15686 case BFD_RELOC_MICROMIPS_CALL16:
15687 case BFD_RELOC_MICROMIPS_GOT16:
15688 case BFD_RELOC_MICROMIPS_GOT_HI16:
15689 case BFD_RELOC_MICROMIPS_GOT_LO16:
15690 case BFD_RELOC_MICROMIPS_CALL_HI16:
15691 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15692 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15693 if (fixP->fx_done)
15694 {
15695 offsetT value;
15696
15697 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15698 {
15699 insn = read_reloc_insn (buf, fixP->fx_r_type);
15700 if (mips16_reloc_p (fixP->fx_r_type))
15701 insn |= mips16_immed_extend (value, 16);
15702 else
15703 insn |= (value & 0xffff);
15704 write_reloc_insn (buf, fixP->fx_r_type, insn);
15705 }
15706 else
15707 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15708 _("unsupported constant in relocation"));
b886a2ab 15709 }
252b5132
RH
15710 break;
15711
252b5132
RH
15712 case BFD_RELOC_64:
15713 /* This is handled like BFD_RELOC_32, but we output a sign
15714 extended value if we are only 32 bits. */
3e722fb5 15715 if (fixP->fx_done)
252b5132
RH
15716 {
15717 if (8 <= sizeof (valueT))
4d68580a 15718 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15719 else
15720 {
a7ebbfdf 15721 valueT hiv;
252b5132 15722
a7ebbfdf 15723 if ((*valP & 0x80000000) != 0)
252b5132
RH
15724 hiv = 0xffffffff;
15725 else
15726 hiv = 0;
4d68580a
RS
15727 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15728 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15729 }
15730 }
15731 break;
15732
056350c6 15733 case BFD_RELOC_RVA:
252b5132 15734 case BFD_RELOC_32:
b47468a6 15735 case BFD_RELOC_32_PCREL:
252b5132 15736 case BFD_RELOC_16:
d56a8dda 15737 case BFD_RELOC_8:
252b5132 15738 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15739 value now. This can happen if we have a .word which is not
15740 resolved when it appears but is later defined. */
252b5132 15741 if (fixP->fx_done)
4d68580a 15742 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15743 break;
15744
7361da2c 15745 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15746 fix_validate_branch (fixP, *valP);
41947d9e
MR
15747 if (!fixP->fx_done)
15748 break;
15749
15750 if (*valP + 0x400000 <= 0x7fffff)
15751 {
15752 insn = read_insn (buf);
15753 insn |= (*valP >> 2) & 0x1fffff;
15754 write_insn (buf, insn);
15755 }
15756 else
15757 as_bad_where (fixP->fx_file, fixP->fx_line,
15758 _("branch out of range"));
15759 break;
15760
7361da2c 15761 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15762 fix_validate_branch (fixP, *valP);
41947d9e
MR
15763 if (!fixP->fx_done)
15764 break;
7361da2c 15765
41947d9e
MR
15766 if (*valP + 0x8000000 <= 0xfffffff)
15767 {
15768 insn = read_insn (buf);
15769 insn |= (*valP >> 2) & 0x3ffffff;
15770 write_insn (buf, insn);
15771 }
15772 else
15773 as_bad_where (fixP->fx_file, fixP->fx_line,
15774 _("branch out of range"));
7361da2c
AB
15775 break;
15776
15777 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15778 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15779 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15780 _("PC-relative access using misaligned symbol (%lx)"),
15781 (long) S_GET_VALUE (fixP->fx_addsy));
15782 if ((fixP->fx_offset & 0x7) != 0)
15783 as_bad_where (fixP->fx_file, fixP->fx_line,
15784 _("PC-relative access using misaligned offset (%lx)"),
15785 (long) fixP->fx_offset);
41947d9e
MR
15786 if (!fixP->fx_done)
15787 break;
7361da2c 15788
41947d9e
MR
15789 if (*valP + 0x100000 <= 0x1fffff)
15790 {
15791 insn = read_insn (buf);
15792 insn |= (*valP >> 3) & 0x3ffff;
15793 write_insn (buf, insn);
15794 }
15795 else
15796 as_bad_where (fixP->fx_file, fixP->fx_line,
15797 _("PC-relative access out of range"));
7361da2c
AB
15798 break;
15799
15800 case BFD_RELOC_MIPS_19_PCREL_S2:
15801 if ((*valP & 0x3) != 0)
15802 as_bad_where (fixP->fx_file, fixP->fx_line,
15803 _("PC-relative access to misaligned address (%lx)"),
717ba204 15804 (long) *valP);
41947d9e
MR
15805 if (!fixP->fx_done)
15806 break;
7361da2c 15807
41947d9e
MR
15808 if (*valP + 0x100000 <= 0x1fffff)
15809 {
15810 insn = read_insn (buf);
15811 insn |= (*valP >> 2) & 0x7ffff;
15812 write_insn (buf, insn);
15813 }
15814 else
15815 as_bad_where (fixP->fx_file, fixP->fx_line,
15816 _("PC-relative access out of range"));
7361da2c
AB
15817 break;
15818
252b5132 15819 case BFD_RELOC_16_PCREL_S2:
9d862524 15820 fix_validate_branch (fixP, *valP);
cb56d3d3 15821
54f4ddb3
TS
15822 /* We need to save the bits in the instruction since fixup_segment()
15823 might be deleting the relocation entry (i.e., a branch within
15824 the current segment). */
a7ebbfdf 15825 if (! fixP->fx_done)
bb2d6cd7 15826 break;
252b5132 15827
54f4ddb3 15828 /* Update old instruction data. */
4d68580a 15829 insn = read_insn (buf);
252b5132 15830
a7ebbfdf
TS
15831 if (*valP + 0x20000 <= 0x3ffff)
15832 {
15833 insn |= (*valP >> 2) & 0xffff;
4d68580a 15834 write_insn (buf, insn);
a7ebbfdf 15835 }
ce8ad872 15836 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15837 && fixP->fx_done
15838 && fixP->fx_frag->fr_address >= text_section->vma
15839 && (fixP->fx_frag->fr_address
587aac4e 15840 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15841 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15842 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15843 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15844 {
15845 /* The branch offset is too large. If this is an
15846 unconditional branch, and we are not generating PIC code,
15847 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15848 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15849 insn = 0x0c000000; /* jal */
252b5132 15850 else
a7ebbfdf
TS
15851 insn = 0x08000000; /* j */
15852 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15853 fixP->fx_done = 0;
15854 fixP->fx_addsy = section_symbol (text_section);
15855 *valP += md_pcrel_from (fixP);
4d68580a 15856 write_insn (buf, insn);
a7ebbfdf
TS
15857 }
15858 else
15859 {
15860 /* If we got here, we have branch-relaxation disabled,
15861 and there's nothing we can do to fix this instruction
15862 without turning it into a longer sequence. */
15863 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15864 _("branch out of range"));
252b5132 15865 }
252b5132
RH
15866 break;
15867
c9775dde 15868 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15869 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15870 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15871 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15872 gas_assert (!fixP->fx_done);
9d862524
MR
15873 if (fix_bad_cross_mode_branch_p (fixP))
15874 as_bad_where (fixP->fx_file, fixP->fx_line,
15875 _("branch to a symbol in another ISA mode"));
15876 else if (fixP->fx_addsy
15877 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15878 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15879 && (fixP->fx_offset & 0x1) != 0)
15880 as_bad_where (fixP->fx_file, fixP->fx_line,
15881 _("branch to misaligned address (0x%lx)"),
15882 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15883 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15884 as_bad_where (fixP->fx_file, fixP->fx_line,
15885 _("cannot encode misaligned addend "
15886 "in the relocatable field (0x%lx)"),
15887 (long) fixP->fx_offset);
df58fc94
RS
15888 break;
15889
252b5132
RH
15890 case BFD_RELOC_VTABLE_INHERIT:
15891 fixP->fx_done = 0;
15892 if (fixP->fx_addsy
15893 && !S_IS_DEFINED (fixP->fx_addsy)
15894 && !S_IS_WEAK (fixP->fx_addsy))
15895 S_SET_WEAK (fixP->fx_addsy);
15896 break;
15897
2f0c68f2 15898 case BFD_RELOC_NONE:
252b5132
RH
15899 case BFD_RELOC_VTABLE_ENTRY:
15900 fixP->fx_done = 0;
15901 break;
15902
15903 default:
b37df7c4 15904 abort ();
252b5132 15905 }
a7ebbfdf
TS
15906
15907 /* Remember value for tc_gen_reloc. */
15908 fixP->fx_addnumber = *valP;
252b5132
RH
15909}
15910
252b5132 15911static symbolS *
17a2f251 15912get_symbol (void)
252b5132
RH
15913{
15914 int c;
15915 char *name;
15916 symbolS *p;
15917
d02603dc 15918 c = get_symbol_name (&name);
252b5132 15919 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15920 (void) restore_line_pointer (c);
252b5132
RH
15921 return p;
15922}
15923
742a56fe
RS
15924/* Align the current frag to a given power of two. If a particular
15925 fill byte should be used, FILL points to an integer that contains
15926 that byte, otherwise FILL is null.
15927
462427c4
RS
15928 This function used to have the comment:
15929
15930 The MIPS assembler also automatically adjusts any preceding label.
15931
15932 The implementation therefore applied the adjustment to a maximum of
15933 one label. However, other label adjustments are applied to batches
15934 of labels, and adjusting just one caused problems when new labels
15935 were added for the sake of debugging or unwind information.
15936 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15937
15938static void
462427c4 15939mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15940{
7d10b47d 15941 mips_emit_delays ();
df58fc94 15942 mips_record_compressed_mode ();
742a56fe
RS
15943 if (fill == NULL && subseg_text_p (now_seg))
15944 frag_align_code (to, 0);
15945 else
15946 frag_align (to, fill ? *fill : 0, 0);
252b5132 15947 record_alignment (now_seg, to);
462427c4 15948 mips_move_labels (labels, FALSE);
252b5132
RH
15949}
15950
15951/* Align to a given power of two. .align 0 turns off the automatic
15952 alignment used by the data creating pseudo-ops. */
15953
15954static void
17a2f251 15955s_align (int x ATTRIBUTE_UNUSED)
252b5132 15956{
742a56fe 15957 int temp, fill_value, *fill_ptr;
49954fb4 15958 long max_alignment = 28;
252b5132 15959
54f4ddb3 15960 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15961 to the aligned address.
54f4ddb3 15962 o It's not documented but auto alignment is reinstated by
252b5132 15963 a .align pseudo instruction.
54f4ddb3 15964 o Note also that after auto alignment is turned off the mips assembler
252b5132 15965 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15966 We don't. */
252b5132
RH
15967
15968 temp = get_absolute_expression ();
15969 if (temp > max_alignment)
1661c76c 15970 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15971 else if (temp < 0)
15972 {
1661c76c 15973 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15974 temp = 0;
15975 }
15976 if (*input_line_pointer == ',')
15977 {
f9419b05 15978 ++input_line_pointer;
742a56fe
RS
15979 fill_value = get_absolute_expression ();
15980 fill_ptr = &fill_value;
252b5132
RH
15981 }
15982 else
742a56fe 15983 fill_ptr = 0;
252b5132
RH
15984 if (temp)
15985 {
a8dbcb85
TS
15986 segment_info_type *si = seg_info (now_seg);
15987 struct insn_label_list *l = si->label_list;
54f4ddb3 15988 /* Auto alignment should be switched on by next section change. */
252b5132 15989 auto_align = 1;
462427c4 15990 mips_align (temp, fill_ptr, l);
252b5132
RH
15991 }
15992 else
15993 {
15994 auto_align = 0;
15995 }
15996
15997 demand_empty_rest_of_line ();
15998}
15999
252b5132 16000static void
17a2f251 16001s_change_sec (int sec)
252b5132
RH
16002{
16003 segT seg;
16004
252b5132
RH
16005 /* The ELF backend needs to know that we are changing sections, so
16006 that .previous works correctly. We could do something like check
b6ff326e 16007 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
16008 as it would not be appropriate to use it in the section changing
16009 functions in read.c, since obj-elf.c intercepts those. FIXME:
16010 This should be cleaner, somehow. */
f3ded42a 16011 obj_elf_section_change_hook ();
252b5132 16012
7d10b47d 16013 mips_emit_delays ();
6a32d874 16014
252b5132
RH
16015 switch (sec)
16016 {
16017 case 't':
16018 s_text (0);
16019 break;
16020 case 'd':
16021 s_data (0);
16022 break;
16023 case 'b':
16024 subseg_set (bss_section, (subsegT) get_absolute_expression ());
16025 demand_empty_rest_of_line ();
16026 break;
16027
16028 case 'r':
4d0d148d
TS
16029 seg = subseg_new (RDATA_SECTION_NAME,
16030 (subsegT) get_absolute_expression ());
f3ded42a
RS
16031 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
16032 | SEC_READONLY | SEC_RELOC
16033 | SEC_DATA));
16034 if (strncmp (TARGET_OS, "elf", 3) != 0)
16035 record_alignment (seg, 4);
4d0d148d 16036 demand_empty_rest_of_line ();
252b5132
RH
16037 break;
16038
16039 case 's':
4d0d148d 16040 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16041 bfd_set_section_flags (stdoutput, seg,
16042 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16043 if (strncmp (TARGET_OS, "elf", 3) != 0)
16044 record_alignment (seg, 4);
4d0d148d
TS
16045 demand_empty_rest_of_line ();
16046 break;
998b3c36
MR
16047
16048 case 'B':
16049 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16050 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16051 if (strncmp (TARGET_OS, "elf", 3) != 0)
16052 record_alignment (seg, 4);
998b3c36
MR
16053 demand_empty_rest_of_line ();
16054 break;
252b5132
RH
16055 }
16056
16057 auto_align = 1;
16058}
b34976b6 16059
cca86cc8 16060void
17a2f251 16061s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16062{
d02603dc 16063 char *saved_ilp;
cca86cc8 16064 char *section_name;
d02603dc 16065 char c, endc;
684022ea 16066 char next_c = 0;
cca86cc8
SC
16067 int section_type;
16068 int section_flag;
16069 int section_entry_size;
16070 int section_alignment;
b34976b6 16071
d02603dc
NC
16072 saved_ilp = input_line_pointer;
16073 endc = get_symbol_name (&section_name);
16074 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16075 if (c)
d02603dc 16076 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16077
4cf0dd0d
TS
16078 /* Do we have .section Name<,"flags">? */
16079 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16080 {
d02603dc
NC
16081 /* Just after name is now '\0'. */
16082 (void) restore_line_pointer (endc);
16083 input_line_pointer = saved_ilp;
cca86cc8
SC
16084 obj_elf_section (ignore);
16085 return;
16086 }
d02603dc
NC
16087
16088 section_name = xstrdup (section_name);
16089 c = restore_line_pointer (endc);
16090
cca86cc8
SC
16091 input_line_pointer++;
16092
16093 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16094 if (c == ',')
16095 section_type = get_absolute_expression ();
16096 else
16097 section_type = 0;
d02603dc 16098
cca86cc8
SC
16099 if (*input_line_pointer++ == ',')
16100 section_flag = get_absolute_expression ();
16101 else
16102 section_flag = 0;
d02603dc 16103
cca86cc8
SC
16104 if (*input_line_pointer++ == ',')
16105 section_entry_size = get_absolute_expression ();
16106 else
16107 section_entry_size = 0;
d02603dc 16108
cca86cc8
SC
16109 if (*input_line_pointer++ == ',')
16110 section_alignment = get_absolute_expression ();
16111 else
16112 section_alignment = 0;
d02603dc 16113
87975d2a
AM
16114 /* FIXME: really ignore? */
16115 (void) section_alignment;
cca86cc8 16116
8ab8a5c8
RS
16117 /* When using the generic form of .section (as implemented by obj-elf.c),
16118 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16119 traditionally had to fall back on the more common @progbits instead.
16120
16121 There's nothing really harmful in this, since bfd will correct
16122 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16123 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16124 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16125
16126 Even so, we shouldn't force users of the MIPS .section syntax to
16127 incorrectly label the sections as SHT_PROGBITS. The best compromise
16128 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16129 generic type-checking code. */
16130 if (section_type == SHT_MIPS_DWARF)
16131 section_type = SHT_PROGBITS;
16132
a91e1603 16133 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16134 section_entry_size, 0, 0, 0);
a816d1ed
AO
16135
16136 if (now_seg->name != section_name)
16137 free (section_name);
cca86cc8 16138}
252b5132
RH
16139
16140void
17a2f251 16141mips_enable_auto_align (void)
252b5132
RH
16142{
16143 auto_align = 1;
16144}
16145
16146static void
17a2f251 16147s_cons (int log_size)
252b5132 16148{
a8dbcb85
TS
16149 segment_info_type *si = seg_info (now_seg);
16150 struct insn_label_list *l = si->label_list;
252b5132 16151
7d10b47d 16152 mips_emit_delays ();
252b5132 16153 if (log_size > 0 && auto_align)
462427c4 16154 mips_align (log_size, 0, l);
252b5132 16155 cons (1 << log_size);
a1facbec 16156 mips_clear_insn_labels ();
252b5132
RH
16157}
16158
16159static void
17a2f251 16160s_float_cons (int type)
252b5132 16161{
a8dbcb85
TS
16162 segment_info_type *si = seg_info (now_seg);
16163 struct insn_label_list *l = si->label_list;
252b5132 16164
7d10b47d 16165 mips_emit_delays ();
252b5132
RH
16166
16167 if (auto_align)
49309057
ILT
16168 {
16169 if (type == 'd')
462427c4 16170 mips_align (3, 0, l);
49309057 16171 else
462427c4 16172 mips_align (2, 0, l);
49309057 16173 }
252b5132 16174
252b5132 16175 float_cons (type);
a1facbec 16176 mips_clear_insn_labels ();
252b5132
RH
16177}
16178
16179/* Handle .globl. We need to override it because on Irix 5 you are
16180 permitted to say
16181 .globl foo .text
16182 where foo is an undefined symbol, to mean that foo should be
16183 considered to be the address of a function. */
16184
16185static void
17a2f251 16186s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16187{
16188 char *name;
16189 int c;
16190 symbolS *symbolP;
16191 flagword flag;
16192
8a06b769 16193 do
252b5132 16194 {
d02603dc 16195 c = get_symbol_name (&name);
8a06b769
TS
16196 symbolP = symbol_find_or_make (name);
16197 S_SET_EXTERNAL (symbolP);
16198
252b5132 16199 *input_line_pointer = c;
d02603dc 16200 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16201
8a06b769
TS
16202 /* On Irix 5, every global symbol that is not explicitly labelled as
16203 being a function is apparently labelled as being an object. */
16204 flag = BSF_OBJECT;
252b5132 16205
8a06b769
TS
16206 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16207 && (*input_line_pointer != ','))
16208 {
16209 char *secname;
16210 asection *sec;
16211
d02603dc 16212 c = get_symbol_name (&secname);
8a06b769
TS
16213 sec = bfd_get_section_by_name (stdoutput, secname);
16214 if (sec == NULL)
16215 as_bad (_("%s: no such section"), secname);
d02603dc 16216 (void) restore_line_pointer (c);
8a06b769
TS
16217
16218 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16219 flag = BSF_FUNCTION;
16220 }
16221
16222 symbol_get_bfdsym (symbolP)->flags |= flag;
16223
16224 c = *input_line_pointer;
16225 if (c == ',')
16226 {
16227 input_line_pointer++;
16228 SKIP_WHITESPACE ();
16229 if (is_end_of_line[(unsigned char) *input_line_pointer])
16230 c = '\n';
16231 }
16232 }
16233 while (c == ',');
252b5132 16234
252b5132
RH
16235 demand_empty_rest_of_line ();
16236}
16237
16238static void
17a2f251 16239s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16240{
16241 char *opt;
16242 char c;
16243
d02603dc 16244 c = get_symbol_name (&opt);
252b5132
RH
16245
16246 if (*opt == 'O')
16247 {
16248 /* FIXME: What does this mean? */
16249 }
41a1578e 16250 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16251 {
16252 int i;
16253
16254 i = atoi (opt + 3);
668c5ebc
MR
16255 if (i != 0 && i != 2)
16256 as_bad (_(".option pic%d not supported"), i);
16257 else if (mips_pic == VXWORKS_PIC)
16258 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16259 else if (i == 0)
252b5132
RH
16260 mips_pic = NO_PIC;
16261 else if (i == 2)
143d77c5 16262 {
8b828383 16263 mips_pic = SVR4_PIC;
143d77c5
EC
16264 mips_abicalls = TRUE;
16265 }
252b5132 16266
4d0d148d 16267 if (mips_pic == SVR4_PIC)
252b5132
RH
16268 {
16269 if (g_switch_seen && g_switch_value != 0)
16270 as_warn (_("-G may not be used with SVR4 PIC code"));
16271 g_switch_value = 0;
16272 bfd_set_gp_size (stdoutput, 0);
16273 }
16274 }
16275 else
1661c76c 16276 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16277
d02603dc 16278 (void) restore_line_pointer (c);
252b5132
RH
16279 demand_empty_rest_of_line ();
16280}
16281
16282/* This structure is used to hold a stack of .set values. */
16283
e972090a
NC
16284struct mips_option_stack
16285{
252b5132
RH
16286 struct mips_option_stack *next;
16287 struct mips_set_options options;
16288};
16289
16290static struct mips_option_stack *mips_opts_stack;
16291
22522f88
MR
16292/* Return status for .set/.module option handling. */
16293
16294enum code_option_type
16295{
16296 /* Unrecognized option. */
16297 OPTION_TYPE_BAD = -1,
16298
16299 /* Ordinary option. */
16300 OPTION_TYPE_NORMAL,
16301
16302 /* ISA changing option. */
16303 OPTION_TYPE_ISA
16304};
16305
16306/* Handle common .set/.module options. Return status indicating option
16307 type. */
16308
16309static enum code_option_type
919731af 16310parse_code_option (char * name)
252b5132 16311{
22522f88 16312 bfd_boolean isa_set = FALSE;
c6278170 16313 const struct mips_ase *ase;
22522f88 16314
919731af 16315 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16316 {
16317 char *s = name + 3;
16318
16319 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16320 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16321 }
252b5132 16322 else if (strcmp (name, "at") == 0)
919731af 16323 mips_opts.at = ATREG;
252b5132 16324 else if (strcmp (name, "noat") == 0)
919731af 16325 mips_opts.at = ZERO;
252b5132 16326 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16327 mips_opts.nomove = 0;
252b5132 16328 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16329 mips_opts.nomove = 1;
252b5132 16330 else if (strcmp (name, "bopt") == 0)
919731af 16331 mips_opts.nobopt = 0;
252b5132 16332 else if (strcmp (name, "nobopt") == 0)
919731af 16333 mips_opts.nobopt = 1;
ad3fea08 16334 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16335 mips_opts.gp = 32;
ad3fea08 16336 else if (strcmp (name, "gp=64") == 0)
919731af 16337 mips_opts.gp = 64;
ad3fea08 16338 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16339 mips_opts.fp = 32;
351cdf24
MF
16340 else if (strcmp (name, "fp=xx") == 0)
16341 mips_opts.fp = 0;
ad3fea08 16342 else if (strcmp (name, "fp=64") == 0)
919731af 16343 mips_opts.fp = 64;
037b32b9
AN
16344 else if (strcmp (name, "softfloat") == 0)
16345 mips_opts.soft_float = 1;
16346 else if (strcmp (name, "hardfloat") == 0)
16347 mips_opts.soft_float = 0;
16348 else if (strcmp (name, "singlefloat") == 0)
16349 mips_opts.single_float = 1;
16350 else if (strcmp (name, "doublefloat") == 0)
16351 mips_opts.single_float = 0;
351cdf24
MF
16352 else if (strcmp (name, "nooddspreg") == 0)
16353 mips_opts.oddspreg = 0;
16354 else if (strcmp (name, "oddspreg") == 0)
16355 mips_opts.oddspreg = 1;
252b5132
RH
16356 else if (strcmp (name, "mips16") == 0
16357 || strcmp (name, "MIPS-16") == 0)
919731af 16358 mips_opts.mips16 = 1;
252b5132
RH
16359 else if (strcmp (name, "nomips16") == 0
16360 || strcmp (name, "noMIPS-16") == 0)
16361 mips_opts.mips16 = 0;
df58fc94 16362 else if (strcmp (name, "micromips") == 0)
919731af 16363 mips_opts.micromips = 1;
df58fc94
RS
16364 else if (strcmp (name, "nomicromips") == 0)
16365 mips_opts.micromips = 0;
c6278170
RS
16366 else if (name[0] == 'n'
16367 && name[1] == 'o'
16368 && (ase = mips_lookup_ase (name + 2)))
919731af 16369 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16370 else if ((ase = mips_lookup_ase (name)))
919731af 16371 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16372 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16373 {
1a2c1fad
CD
16374 /* Permit the user to change the ISA and architecture on the fly.
16375 Needless to say, misuse can cause serious problems. */
919731af 16376 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16377 {
16378 const struct mips_cpu_info *p;
16379
919731af 16380 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16381 if (!p)
16382 as_bad (_("unknown architecture %s"), name + 5);
16383 else
16384 {
16385 mips_opts.arch = p->cpu;
16386 mips_opts.isa = p->isa;
22522f88 16387 isa_set = TRUE;
1a2c1fad
CD
16388 }
16389 }
81a21e38
TS
16390 else if (strncmp (name, "mips", 4) == 0)
16391 {
16392 const struct mips_cpu_info *p;
16393
919731af 16394 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16395 if (!p)
16396 as_bad (_("unknown ISA level %s"), name + 4);
16397 else
16398 {
16399 mips_opts.arch = p->cpu;
16400 mips_opts.isa = p->isa;
22522f88 16401 isa_set = TRUE;
81a21e38
TS
16402 }
16403 }
af7ee8bf 16404 else
81a21e38 16405 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16406 }
16407 else if (strcmp (name, "autoextend") == 0)
16408 mips_opts.noautoextend = 0;
16409 else if (strcmp (name, "noautoextend") == 0)
16410 mips_opts.noautoextend = 1;
833794fc
MR
16411 else if (strcmp (name, "insn32") == 0)
16412 mips_opts.insn32 = TRUE;
16413 else if (strcmp (name, "noinsn32") == 0)
16414 mips_opts.insn32 = FALSE;
919731af 16415 else if (strcmp (name, "sym32") == 0)
16416 mips_opts.sym32 = TRUE;
16417 else if (strcmp (name, "nosym32") == 0)
16418 mips_opts.sym32 = FALSE;
16419 else
22522f88
MR
16420 return OPTION_TYPE_BAD;
16421
16422 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16423}
16424
16425/* Handle the .set pseudo-op. */
16426
16427static void
16428s_mipsset (int x ATTRIBUTE_UNUSED)
16429{
22522f88 16430 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16431 char *name = input_line_pointer, ch;
919731af 16432
16433 file_mips_check_options ();
16434
16435 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16436 ++input_line_pointer;
16437 ch = *input_line_pointer;
16438 *input_line_pointer = '\0';
16439
16440 if (strchr (name, ','))
16441 {
16442 /* Generic ".set" directive; use the generic handler. */
16443 *input_line_pointer = ch;
16444 input_line_pointer = name;
16445 s_set (0);
16446 return;
16447 }
16448
16449 if (strcmp (name, "reorder") == 0)
16450 {
16451 if (mips_opts.noreorder)
16452 end_noreorder ();
16453 }
16454 else if (strcmp (name, "noreorder") == 0)
16455 {
16456 if (!mips_opts.noreorder)
16457 start_noreorder ();
16458 }
16459 else if (strcmp (name, "macro") == 0)
16460 mips_opts.warn_about_macros = 0;
16461 else if (strcmp (name, "nomacro") == 0)
16462 {
16463 if (mips_opts.noreorder == 0)
16464 as_bad (_("`noreorder' must be set before `nomacro'"));
16465 mips_opts.warn_about_macros = 1;
16466 }
16467 else if (strcmp (name, "gp=default") == 0)
16468 mips_opts.gp = file_mips_opts.gp;
16469 else if (strcmp (name, "fp=default") == 0)
16470 mips_opts.fp = file_mips_opts.fp;
16471 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16472 {
16473 mips_opts.isa = file_mips_opts.isa;
16474 mips_opts.arch = file_mips_opts.arch;
16475 mips_opts.gp = file_mips_opts.gp;
16476 mips_opts.fp = file_mips_opts.fp;
16477 }
252b5132
RH
16478 else if (strcmp (name, "push") == 0)
16479 {
16480 struct mips_option_stack *s;
16481
325801bd 16482 s = XNEW (struct mips_option_stack);
252b5132
RH
16483 s->next = mips_opts_stack;
16484 s->options = mips_opts;
16485 mips_opts_stack = s;
16486 }
16487 else if (strcmp (name, "pop") == 0)
16488 {
16489 struct mips_option_stack *s;
16490
16491 s = mips_opts_stack;
16492 if (s == NULL)
16493 as_bad (_(".set pop with no .set push"));
16494 else
16495 {
16496 /* If we're changing the reorder mode we need to handle
16497 delay slots correctly. */
16498 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16499 start_noreorder ();
252b5132 16500 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16501 end_noreorder ();
252b5132
RH
16502
16503 mips_opts = s->options;
16504 mips_opts_stack = s->next;
16505 free (s);
16506 }
16507 }
22522f88
MR
16508 else
16509 {
16510 type = parse_code_option (name);
16511 if (type == OPTION_TYPE_BAD)
16512 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16513 }
919731af 16514
16515 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16516 registers based on what is supported by the arch/cpu. */
22522f88 16517 if (type == OPTION_TYPE_ISA)
e6559e01 16518 {
919731af 16519 switch (mips_opts.isa)
16520 {
16521 case 0:
16522 break;
16523 case ISA_MIPS1:
351cdf24
MF
16524 /* MIPS I cannot support FPXX. */
16525 mips_opts.fp = 32;
16526 /* fall-through. */
919731af 16527 case ISA_MIPS2:
16528 case ISA_MIPS32:
16529 case ISA_MIPS32R2:
16530 case ISA_MIPS32R3:
16531 case ISA_MIPS32R5:
16532 mips_opts.gp = 32;
351cdf24
MF
16533 if (mips_opts.fp != 0)
16534 mips_opts.fp = 32;
919731af 16535 break;
7361da2c
AB
16536 case ISA_MIPS32R6:
16537 mips_opts.gp = 32;
16538 mips_opts.fp = 64;
16539 break;
919731af 16540 case ISA_MIPS3:
16541 case ISA_MIPS4:
16542 case ISA_MIPS5:
16543 case ISA_MIPS64:
16544 case ISA_MIPS64R2:
16545 case ISA_MIPS64R3:
16546 case ISA_MIPS64R5:
7361da2c 16547 case ISA_MIPS64R6:
919731af 16548 mips_opts.gp = 64;
351cdf24
MF
16549 if (mips_opts.fp != 0)
16550 {
16551 if (mips_opts.arch == CPU_R5900)
16552 mips_opts.fp = 32;
16553 else
16554 mips_opts.fp = 64;
16555 }
919731af 16556 break;
16557 default:
16558 as_bad (_("unknown ISA level %s"), name + 4);
16559 break;
16560 }
e6559e01 16561 }
919731af 16562
16563 mips_check_options (&mips_opts, FALSE);
16564
16565 mips_check_isa_supports_ases ();
16566 *input_line_pointer = ch;
16567 demand_empty_rest_of_line ();
16568}
16569
16570/* Handle the .module pseudo-op. */
16571
16572static void
16573s_module (int ignore ATTRIBUTE_UNUSED)
16574{
16575 char *name = input_line_pointer, ch;
16576
16577 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16578 ++input_line_pointer;
16579 ch = *input_line_pointer;
16580 *input_line_pointer = '\0';
16581
16582 if (!file_mips_opts_checked)
252b5132 16583 {
22522f88 16584 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16585 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16586
16587 /* Update module level settings from mips_opts. */
16588 file_mips_opts = mips_opts;
252b5132 16589 }
919731af 16590 else
16591 as_bad (_(".module is not permitted after generating code"));
16592
252b5132
RH
16593 *input_line_pointer = ch;
16594 demand_empty_rest_of_line ();
16595}
16596
16597/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16598 .option pic2. It means to generate SVR4 PIC calls. */
16599
16600static void
17a2f251 16601s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16602{
16603 mips_pic = SVR4_PIC;
143d77c5 16604 mips_abicalls = TRUE;
4d0d148d
TS
16605
16606 if (g_switch_seen && g_switch_value != 0)
16607 as_warn (_("-G may not be used with SVR4 PIC code"));
16608 g_switch_value = 0;
16609
252b5132
RH
16610 bfd_set_gp_size (stdoutput, 0);
16611 demand_empty_rest_of_line ();
16612}
16613
16614/* Handle the .cpload pseudo-op. This is used when generating SVR4
16615 PIC code. It sets the $gp register for the function based on the
16616 function address, which is in the register named in the argument.
16617 This uses a relocation against _gp_disp, which is handled specially
16618 by the linker. The result is:
16619 lui $gp,%hi(_gp_disp)
16620 addiu $gp,$gp,%lo(_gp_disp)
16621 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16622 The .cpload argument is normally $25 == $t9.
16623
16624 The -mno-shared option changes this to:
bbe506e8
TS
16625 lui $gp,%hi(__gnu_local_gp)
16626 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16627 and the argument is ignored. This saves an instruction, but the
16628 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16629 address for __gnu_local_gp. Thus code assembled with -mno-shared
16630 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16631
16632static void
17a2f251 16633s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16634{
16635 expressionS ex;
aa6975fb
ILT
16636 int reg;
16637 int in_shared;
252b5132 16638
919731af 16639 file_mips_check_options ();
16640
6478892d
TS
16641 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16642 .cpload is ignored. */
16643 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16644 {
16645 s_ignore (0);
16646 return;
16647 }
16648
a276b80c
MR
16649 if (mips_opts.mips16)
16650 {
16651 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16652 ignore_rest_of_line ();
16653 return;
16654 }
16655
d3ecfc59 16656 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16657 if (mips_opts.noreorder == 0)
16658 as_warn (_(".cpload not in noreorder section"));
16659
aa6975fb
ILT
16660 reg = tc_get_register (0);
16661
16662 /* If we need to produce a 64-bit address, we are better off using
16663 the default instruction sequence. */
aed1a261 16664 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16665
252b5132 16666 ex.X_op = O_symbol;
bbe506e8
TS
16667 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16668 "__gnu_local_gp");
252b5132
RH
16669 ex.X_op_symbol = NULL;
16670 ex.X_add_number = 0;
16671
16672 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16673 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16674
8a75745d
MR
16675 mips_mark_labels ();
16676 mips_assembling_insn = TRUE;
16677
584892a6 16678 macro_start ();
67c0d1eb
RS
16679 macro_build_lui (&ex, mips_gp_register);
16680 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16681 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16682 if (in_shared)
16683 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16684 mips_gp_register, reg);
584892a6 16685 macro_end ();
252b5132 16686
8a75745d 16687 mips_assembling_insn = FALSE;
252b5132
RH
16688 demand_empty_rest_of_line ();
16689}
16690
6478892d
TS
16691/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16692 .cpsetup $reg1, offset|$reg2, label
16693
16694 If offset is given, this results in:
16695 sd $gp, offset($sp)
956cd1d6 16696 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16697 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16698 daddu $gp, $gp, $reg1
6478892d
TS
16699
16700 If $reg2 is given, this results in:
40fc1451 16701 or $reg2, $gp, $0
956cd1d6 16702 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16703 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16704 daddu $gp, $gp, $reg1
aa6975fb
ILT
16705 $reg1 is normally $25 == $t9.
16706
16707 The -mno-shared option replaces the last three instructions with
16708 lui $gp,%hi(_gp)
54f4ddb3 16709 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16710
6478892d 16711static void
17a2f251 16712s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16713{
16714 expressionS ex_off;
16715 expressionS ex_sym;
16716 int reg1;
6478892d 16717
919731af 16718 file_mips_check_options ();
16719
8586fc66 16720 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16721 We also need NewABI support. */
16722 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16723 {
16724 s_ignore (0);
16725 return;
16726 }
16727
a276b80c
MR
16728 if (mips_opts.mips16)
16729 {
16730 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16731 ignore_rest_of_line ();
16732 return;
16733 }
16734
6478892d
TS
16735 reg1 = tc_get_register (0);
16736 SKIP_WHITESPACE ();
16737 if (*input_line_pointer != ',')
16738 {
16739 as_bad (_("missing argument separator ',' for .cpsetup"));
16740 return;
16741 }
16742 else
80245285 16743 ++input_line_pointer;
6478892d
TS
16744 SKIP_WHITESPACE ();
16745 if (*input_line_pointer == '$')
80245285
TS
16746 {
16747 mips_cpreturn_register = tc_get_register (0);
16748 mips_cpreturn_offset = -1;
16749 }
6478892d 16750 else
80245285
TS
16751 {
16752 mips_cpreturn_offset = get_absolute_expression ();
16753 mips_cpreturn_register = -1;
16754 }
6478892d
TS
16755 SKIP_WHITESPACE ();
16756 if (*input_line_pointer != ',')
16757 {
16758 as_bad (_("missing argument separator ',' for .cpsetup"));
16759 return;
16760 }
16761 else
f9419b05 16762 ++input_line_pointer;
6478892d 16763 SKIP_WHITESPACE ();
f21f8242 16764 expression (&ex_sym);
6478892d 16765
8a75745d
MR
16766 mips_mark_labels ();
16767 mips_assembling_insn = TRUE;
16768
584892a6 16769 macro_start ();
6478892d
TS
16770 if (mips_cpreturn_register == -1)
16771 {
16772 ex_off.X_op = O_constant;
16773 ex_off.X_add_symbol = NULL;
16774 ex_off.X_op_symbol = NULL;
16775 ex_off.X_add_number = mips_cpreturn_offset;
16776
67c0d1eb 16777 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16778 BFD_RELOC_LO16, SP);
6478892d
TS
16779 }
16780 else
40fc1451 16781 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16782
aed1a261 16783 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16784 {
df58fc94 16785 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16786 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16787 BFD_RELOC_HI16_S);
16788
16789 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16790 mips_gp_register, -1, BFD_RELOC_GPREL16,
16791 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16792
16793 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16794 mips_gp_register, reg1);
16795 }
16796 else
16797 {
16798 expressionS ex;
16799
16800 ex.X_op = O_symbol;
4184909a 16801 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16802 ex.X_op_symbol = NULL;
16803 ex.X_add_number = 0;
6e1304d8 16804
aa6975fb
ILT
16805 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16806 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16807
16808 macro_build_lui (&ex, mips_gp_register);
16809 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16810 mips_gp_register, BFD_RELOC_LO16);
16811 }
f21f8242 16812
584892a6 16813 macro_end ();
6478892d 16814
8a75745d 16815 mips_assembling_insn = FALSE;
6478892d
TS
16816 demand_empty_rest_of_line ();
16817}
16818
16819static void
17a2f251 16820s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16821{
919731af 16822 file_mips_check_options ();
16823
6478892d 16824 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16825 .cplocal is ignored. */
6478892d
TS
16826 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16827 {
16828 s_ignore (0);
16829 return;
16830 }
16831
a276b80c
MR
16832 if (mips_opts.mips16)
16833 {
16834 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16835 ignore_rest_of_line ();
16836 return;
16837 }
16838
6478892d 16839 mips_gp_register = tc_get_register (0);
85b51719 16840 demand_empty_rest_of_line ();
6478892d
TS
16841}
16842
252b5132
RH
16843/* Handle the .cprestore pseudo-op. This stores $gp into a given
16844 offset from $sp. The offset is remembered, and after making a PIC
16845 call $gp is restored from that location. */
16846
16847static void
17a2f251 16848s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16849{
16850 expressionS ex;
252b5132 16851
919731af 16852 file_mips_check_options ();
16853
6478892d 16854 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16855 .cprestore is ignored. */
6478892d 16856 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16857 {
16858 s_ignore (0);
16859 return;
16860 }
16861
a276b80c
MR
16862 if (mips_opts.mips16)
16863 {
16864 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16865 ignore_rest_of_line ();
16866 return;
16867 }
16868
252b5132 16869 mips_cprestore_offset = get_absolute_expression ();
7a621144 16870 mips_cprestore_valid = 1;
252b5132
RH
16871
16872 ex.X_op = O_constant;
16873 ex.X_add_symbol = NULL;
16874 ex.X_op_symbol = NULL;
16875 ex.X_add_number = mips_cprestore_offset;
16876
8a75745d
MR
16877 mips_mark_labels ();
16878 mips_assembling_insn = TRUE;
16879
584892a6 16880 macro_start ();
67c0d1eb
RS
16881 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16882 SP, HAVE_64BIT_ADDRESSES);
584892a6 16883 macro_end ();
252b5132 16884
8a75745d 16885 mips_assembling_insn = FALSE;
252b5132
RH
16886 demand_empty_rest_of_line ();
16887}
16888
6478892d 16889/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16890 was given in the preceding .cpsetup, it results in:
6478892d 16891 ld $gp, offset($sp)
76b3015f 16892
6478892d 16893 If a register $reg2 was given there, it results in:
40fc1451 16894 or $gp, $reg2, $0 */
54f4ddb3 16895
6478892d 16896static void
17a2f251 16897s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16898{
16899 expressionS ex;
6478892d 16900
919731af 16901 file_mips_check_options ();
16902
6478892d
TS
16903 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16904 We also need NewABI support. */
16905 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16906 {
16907 s_ignore (0);
16908 return;
16909 }
16910
a276b80c
MR
16911 if (mips_opts.mips16)
16912 {
16913 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16914 ignore_rest_of_line ();
16915 return;
16916 }
16917
8a75745d
MR
16918 mips_mark_labels ();
16919 mips_assembling_insn = TRUE;
16920
584892a6 16921 macro_start ();
6478892d
TS
16922 if (mips_cpreturn_register == -1)
16923 {
16924 ex.X_op = O_constant;
16925 ex.X_add_symbol = NULL;
16926 ex.X_op_symbol = NULL;
16927 ex.X_add_number = mips_cpreturn_offset;
16928
67c0d1eb 16929 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16930 }
16931 else
40fc1451
SD
16932 move_register (mips_gp_register, mips_cpreturn_register);
16933
584892a6 16934 macro_end ();
6478892d 16935
8a75745d 16936 mips_assembling_insn = FALSE;
6478892d
TS
16937 demand_empty_rest_of_line ();
16938}
16939
d0f13682
CLT
16940/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16941 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16942 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16943 debug information or MIPS16 TLS. */
741d6ea8
JM
16944
16945static void
d0f13682
CLT
16946s_tls_rel_directive (const size_t bytes, const char *dirstr,
16947 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16948{
16949 expressionS ex;
16950 char *p;
16951
16952 expression (&ex);
16953
16954 if (ex.X_op != O_symbol)
16955 {
1661c76c 16956 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16957 ignore_rest_of_line ();
16958 }
16959
16960 p = frag_more (bytes);
16961 md_number_to_chars (p, 0, bytes);
d0f13682 16962 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16963 demand_empty_rest_of_line ();
de64cffd 16964 mips_clear_insn_labels ();
741d6ea8
JM
16965}
16966
16967/* Handle .dtprelword. */
16968
16969static void
16970s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16971{
d0f13682 16972 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16973}
16974
16975/* Handle .dtpreldword. */
16976
16977static void
16978s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16979{
d0f13682
CLT
16980 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16981}
16982
16983/* Handle .tprelword. */
16984
16985static void
16986s_tprelword (int ignore ATTRIBUTE_UNUSED)
16987{
16988 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16989}
16990
16991/* Handle .tpreldword. */
16992
16993static void
16994s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16995{
16996 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16997}
16998
6478892d
TS
16999/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
17000 code. It sets the offset to use in gp_rel relocations. */
17001
17002static void
17a2f251 17003s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
17004{
17005 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
17006 We also need NewABI support. */
17007 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
17008 {
17009 s_ignore (0);
17010 return;
17011 }
17012
def2e0dd 17013 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
17014
17015 demand_empty_rest_of_line ();
17016}
17017
252b5132
RH
17018/* Handle the .gpword pseudo-op. This is used when generating PIC
17019 code. It generates a 32 bit GP relative reloc. */
17020
17021static void
17a2f251 17022s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 17023{
a8dbcb85
TS
17024 segment_info_type *si;
17025 struct insn_label_list *l;
252b5132
RH
17026 expressionS ex;
17027 char *p;
17028
17029 /* When not generating PIC code, this is treated as .word. */
17030 if (mips_pic != SVR4_PIC)
17031 {
17032 s_cons (2);
17033 return;
17034 }
17035
a8dbcb85
TS
17036 si = seg_info (now_seg);
17037 l = si->label_list;
7d10b47d 17038 mips_emit_delays ();
252b5132 17039 if (auto_align)
462427c4 17040 mips_align (2, 0, l);
252b5132
RH
17041
17042 expression (&ex);
a1facbec 17043 mips_clear_insn_labels ();
252b5132
RH
17044
17045 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17046 {
1661c76c 17047 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17048 ignore_rest_of_line ();
17049 }
17050
17051 p = frag_more (4);
17a2f251 17052 md_number_to_chars (p, 0, 4);
b34976b6 17053 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17054 BFD_RELOC_GPREL32);
252b5132
RH
17055
17056 demand_empty_rest_of_line ();
17057}
17058
10181a0d 17059static void
17a2f251 17060s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17061{
a8dbcb85
TS
17062 segment_info_type *si;
17063 struct insn_label_list *l;
10181a0d
AO
17064 expressionS ex;
17065 char *p;
17066
17067 /* When not generating PIC code, this is treated as .dword. */
17068 if (mips_pic != SVR4_PIC)
17069 {
17070 s_cons (3);
17071 return;
17072 }
17073
a8dbcb85
TS
17074 si = seg_info (now_seg);
17075 l = si->label_list;
7d10b47d 17076 mips_emit_delays ();
10181a0d 17077 if (auto_align)
462427c4 17078 mips_align (3, 0, l);
10181a0d
AO
17079
17080 expression (&ex);
a1facbec 17081 mips_clear_insn_labels ();
10181a0d
AO
17082
17083 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17084 {
1661c76c 17085 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17086 ignore_rest_of_line ();
17087 }
17088
17089 p = frag_more (8);
17a2f251 17090 md_number_to_chars (p, 0, 8);
a105a300 17091 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17092 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17093
17094 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17095 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17096 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17097
17098 demand_empty_rest_of_line ();
17099}
17100
a3f278e2
CM
17101/* Handle the .ehword pseudo-op. This is used when generating unwinding
17102 tables. It generates a R_MIPS_EH reloc. */
17103
17104static void
17105s_ehword (int ignore ATTRIBUTE_UNUSED)
17106{
17107 expressionS ex;
17108 char *p;
17109
17110 mips_emit_delays ();
17111
17112 expression (&ex);
17113 mips_clear_insn_labels ();
17114
17115 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17116 {
1661c76c 17117 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17118 ignore_rest_of_line ();
17119 }
17120
17121 p = frag_more (4);
17122 md_number_to_chars (p, 0, 4);
17123 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17124 BFD_RELOC_32_PCREL);
a3f278e2
CM
17125
17126 demand_empty_rest_of_line ();
17127}
17128
252b5132
RH
17129/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17130 tables in SVR4 PIC code. */
17131
17132static void
17a2f251 17133s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17134{
252b5132
RH
17135 int reg;
17136
919731af 17137 file_mips_check_options ();
17138
10181a0d
AO
17139 /* This is ignored when not generating SVR4 PIC code. */
17140 if (mips_pic != SVR4_PIC)
252b5132
RH
17141 {
17142 s_ignore (0);
17143 return;
17144 }
17145
8a75745d
MR
17146 mips_mark_labels ();
17147 mips_assembling_insn = TRUE;
17148
252b5132 17149 /* Add $gp to the register named as an argument. */
584892a6 17150 macro_start ();
252b5132 17151 reg = tc_get_register (0);
67c0d1eb 17152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17153 macro_end ();
252b5132 17154
8a75745d 17155 mips_assembling_insn = FALSE;
bdaaa2e1 17156 demand_empty_rest_of_line ();
252b5132
RH
17157}
17158
17159/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17160 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17161 such as generating jalx instructions when needed. We also make
17162 them odd for the duration of the assembly, in order to generate the
17163 right sort of code. We will make them even in the adjust_symtab
17164 routine, while leaving them marked. This is convenient for the
17165 debugger and the disassembler. The linker knows to make them odd
17166 again. */
17167
17168static void
17a2f251 17169s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17170{
7bb01e2d
MR
17171 file_mips_check_options ();
17172 file_ase_mips16 |= mips_opts.mips16;
17173 file_ase_micromips |= mips_opts.micromips;
17174
df58fc94 17175 mips_mark_labels ();
252b5132
RH
17176
17177 demand_empty_rest_of_line ();
17178}
17179
ba92f887
MR
17180/* Handle the .nan pseudo-op. */
17181
17182static void
17183s_nan (int ignore ATTRIBUTE_UNUSED)
17184{
17185 static const char str_legacy[] = "legacy";
17186 static const char str_2008[] = "2008";
17187 size_t i;
17188
17189 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17190
17191 if (i == sizeof (str_2008) - 1
17192 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17193 mips_nan2008 = 1;
ba92f887
MR
17194 else if (i == sizeof (str_legacy) - 1
17195 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17196 {
17197 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17198 mips_nan2008 = 0;
17199 else
17200 as_bad (_("`%s' does not support legacy NaN"),
17201 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17202 }
ba92f887 17203 else
1661c76c 17204 as_bad (_("bad .nan directive"));
ba92f887
MR
17205
17206 input_line_pointer += i;
17207 demand_empty_rest_of_line ();
17208}
17209
754e2bb9
RS
17210/* Handle a .stab[snd] directive. Ideally these directives would be
17211 implemented in a transparent way, so that removing them would not
17212 have any effect on the generated instructions. However, s_stab
17213 internally changes the section, so in practice we need to decide
17214 now whether the preceding label marks compressed code. We do not
17215 support changing the compression mode of a label after a .stab*
17216 directive, such as in:
17217
17218 foo:
134c0c8b 17219 .stabs ...
754e2bb9
RS
17220 .set mips16
17221
17222 so the current mode wins. */
252b5132
RH
17223
17224static void
17a2f251 17225s_mips_stab (int type)
252b5132 17226{
42c0794e 17227 file_mips_check_options ();
754e2bb9 17228 mips_mark_labels ();
252b5132
RH
17229 s_stab (type);
17230}
17231
54f4ddb3 17232/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17233
17234static void
17a2f251 17235s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17236{
17237 char *name;
17238 int c;
17239 symbolS *symbolP;
17240 expressionS exp;
17241
d02603dc 17242 c = get_symbol_name (&name);
252b5132
RH
17243 symbolP = symbol_find_or_make (name);
17244 S_SET_WEAK (symbolP);
17245 *input_line_pointer = c;
17246
d02603dc 17247 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17248
17249 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17250 {
17251 if (S_IS_DEFINED (symbolP))
17252 {
20203fb9 17253 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17254 S_GET_NAME (symbolP));
17255 ignore_rest_of_line ();
17256 return;
17257 }
bdaaa2e1 17258
252b5132
RH
17259 if (*input_line_pointer == ',')
17260 {
17261 ++input_line_pointer;
17262 SKIP_WHITESPACE ();
17263 }
bdaaa2e1 17264
252b5132
RH
17265 expression (&exp);
17266 if (exp.X_op != O_symbol)
17267 {
20203fb9 17268 as_bad (_("bad .weakext directive"));
98d3f06f 17269 ignore_rest_of_line ();
252b5132
RH
17270 return;
17271 }
49309057 17272 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17273 }
17274
17275 demand_empty_rest_of_line ();
17276}
17277
17278/* Parse a register string into a number. Called from the ECOFF code
17279 to parse .frame. The argument is non-zero if this is the frame
17280 register, so that we can record it in mips_frame_reg. */
17281
17282int
17a2f251 17283tc_get_register (int frame)
252b5132 17284{
707bfff6 17285 unsigned int reg;
252b5132
RH
17286
17287 SKIP_WHITESPACE ();
707bfff6
TS
17288 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17289 reg = 0;
252b5132 17290 if (frame)
7a621144
DJ
17291 {
17292 mips_frame_reg = reg != 0 ? reg : SP;
17293 mips_frame_reg_valid = 1;
17294 mips_cprestore_valid = 0;
17295 }
252b5132
RH
17296 return reg;
17297}
17298
17299valueT
17a2f251 17300md_section_align (asection *seg, valueT addr)
252b5132
RH
17301{
17302 int align = bfd_get_section_alignment (stdoutput, seg);
17303
f3ded42a
RS
17304 /* We don't need to align ELF sections to the full alignment.
17305 However, Irix 5 may prefer that we align them at least to a 16
17306 byte boundary. We don't bother to align the sections if we
17307 are targeted for an embedded system. */
17308 if (strncmp (TARGET_OS, "elf", 3) == 0)
17309 return addr;
17310 if (align > 4)
17311 align = 4;
252b5132 17312
8d3842cd 17313 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17314}
17315
17316/* Utility routine, called from above as well. If called while the
17317 input file is still being read, it's only an approximation. (For
17318 example, a symbol may later become defined which appeared to be
17319 undefined earlier.) */
17320
17321static int
17a2f251 17322nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17323{
17324 if (sym == 0)
17325 return 0;
17326
4d0d148d 17327 if (g_switch_value > 0)
252b5132
RH
17328 {
17329 const char *symname;
17330 int change;
17331
c9914766 17332 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17333 register. It can be if it is smaller than the -G size or if
17334 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17335 not be referenced off the $gp, although it appears as though
252b5132
RH
17336 they can. */
17337 symname = S_GET_NAME (sym);
17338 if (symname != (const char *) NULL
17339 && (strcmp (symname, "eprol") == 0
17340 || strcmp (symname, "etext") == 0
17341 || strcmp (symname, "_gp") == 0
17342 || strcmp (symname, "edata") == 0
17343 || strcmp (symname, "_fbss") == 0
17344 || strcmp (symname, "_fdata") == 0
17345 || strcmp (symname, "_ftext") == 0
17346 || strcmp (symname, "end") == 0
17347 || strcmp (symname, "_gp_disp") == 0))
17348 change = 1;
17349 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17350 && (0
17351#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17352 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17353 && (symbol_get_obj (sym)->ecoff_extern_size
17354 <= g_switch_value))
252b5132
RH
17355#endif
17356 /* We must defer this decision until after the whole
17357 file has been read, since there might be a .extern
17358 after the first use of this symbol. */
17359 || (before_relaxing
17360#ifndef NO_ECOFF_DEBUGGING
49309057 17361 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17362#endif
17363 && S_GET_VALUE (sym) == 0)
17364 || (S_GET_VALUE (sym) != 0
17365 && S_GET_VALUE (sym) <= g_switch_value)))
17366 change = 0;
17367 else
17368 {
17369 const char *segname;
17370
17371 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17372 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17373 && strcmp (segname, ".lit4") != 0);
17374 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17375 && strcmp (segname, ".sbss") != 0
17376 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17377 && strncmp (segname, ".sbss.", 6) != 0
17378 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17379 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17380 }
17381 return change;
17382 }
17383 else
c9914766 17384 /* We are not optimizing for the $gp register. */
252b5132
RH
17385 return 1;
17386}
17387
5919d012
RS
17388
17389/* Return true if the given symbol should be considered local for SVR4 PIC. */
17390
17391static bfd_boolean
9e009953 17392pic_need_relax (symbolS *sym)
5919d012
RS
17393{
17394 asection *symsec;
5919d012
RS
17395
17396 /* Handle the case of a symbol equated to another symbol. */
17397 while (symbol_equated_reloc_p (sym))
17398 {
17399 symbolS *n;
17400
5f0fe04b 17401 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17402 n = symbol_get_value_expression (sym)->X_add_symbol;
17403 if (n == sym)
17404 break;
17405 sym = n;
17406 }
17407
df1f3cda
DD
17408 if (symbol_section_p (sym))
17409 return TRUE;
17410
5919d012
RS
17411 symsec = S_GET_SEGMENT (sym);
17412
5919d012 17413 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17414 return (!bfd_is_und_section (symsec)
17415 && !bfd_is_abs_section (symsec)
5f0fe04b 17416 && !bfd_is_com_section (symsec)
5919d012 17417 /* A global or weak symbol is treated as external. */
f3ded42a 17418 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17419}
14f72d45
MR
17420\f
17421/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17422 convert a section-relative value VAL to the equivalent PC-relative
17423 value. */
17424
17425static offsetT
17426mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17427 offsetT val, long stretch)
17428{
17429 fragS *sym_frag;
17430 addressT addr;
17431
17432 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17433
17434 sym_frag = symbol_get_frag (fragp->fr_symbol);
17435
17436 /* If the relax_marker of the symbol fragment differs from the
17437 relax_marker of this fragment, we have not yet adjusted the
17438 symbol fragment fr_address. We want to add in STRETCH in
17439 order to get a better estimate of the address. This
17440 particularly matters because of the shift bits. */
17441 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17442 {
17443 fragS *f;
17444
17445 /* Adjust stretch for any alignment frag. Note that if have
17446 been expanding the earlier code, the symbol may be
17447 defined in what appears to be an earlier frag. FIXME:
17448 This doesn't handle the fr_subtype field, which specifies
17449 a maximum number of bytes to skip when doing an
17450 alignment. */
17451 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17452 {
17453 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17454 {
17455 if (stretch < 0)
17456 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17457 else
17458 stretch &= ~((1 << (int) f->fr_offset) - 1);
17459 if (stretch == 0)
17460 break;
17461 }
17462 }
17463 if (f != NULL)
17464 val += stretch;
17465 }
17466
17467 addr = fragp->fr_address + fragp->fr_fix;
17468
17469 /* The base address rules are complicated. The base address of
17470 a branch is the following instruction. The base address of a
17471 PC relative load or add is the instruction itself, but if it
17472 is in a delay slot (in which case it can not be extended) use
17473 the address of the instruction whose delay slot it is in. */
17474 if (pcrel_op->include_isa_bit)
17475 {
17476 addr += 2;
17477
17478 /* If we are currently assuming that this frag should be
17479 extended, then the current address is two bytes higher. */
17480 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17481 addr += 2;
17482
17483 /* Ignore the low bit in the target, since it will be set
17484 for a text label. */
17485 val &= -2;
17486 }
17487 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17488 addr -= 4;
17489 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17490 addr -= 2;
5919d012 17491
14f72d45
MR
17492 val -= addr & -(1 << pcrel_op->align_log2);
17493
17494 return val;
17495}
5919d012 17496
252b5132
RH
17497/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17498 extended opcode. SEC is the section the frag is in. */
17499
17500static int
17a2f251 17501mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17502{
3ccad066 17503 const struct mips_int_operand *operand;
252b5132 17504 offsetT val;
252b5132 17505 segT symsec;
14f72d45 17506 int type;
252b5132
RH
17507
17508 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17509 return 0;
17510 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17511 return 1;
17512
88a7ef16 17513 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17514 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17515 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17516 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17517 || (operand->root.type == OP_PCREL
17518 ? sec != symsec
17519 : !bfd_is_abs_section (symsec)))
17520 return 1;
252b5132 17521
88a7ef16 17522 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17523
3ccad066 17524 if (operand->root.type == OP_PCREL)
252b5132 17525 {
3ccad066 17526 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17527 offsetT maxtiny;
252b5132 17528
1425c41d 17529 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17530 return 1;
252b5132 17531
88a7ef16 17532 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17533 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17534
17535 /* If any of the shifted bits are set, we must use an extended
17536 opcode. If the address depends on the size of this
17537 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17538 use an extended opcode. */
17539 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17540 {
17541 fragp->fr_subtype =
1425c41d 17542 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17543 return 1;
17544 }
17545
17546 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17547 is precisely the next value above maxtiny, then there is a
17548 chance of an infinite loop as in the following code:
252b5132
RH
17549 la $4,foo
17550 .skip 1020
17551 .align 2
17552 foo:
17553 In this case when the la is extended, foo is 0x3fc bytes
17554 away, so the la can be shrunk, but then foo is 0x400 away, so
17555 the la must be extended. To avoid this loop, we mark the
17556 frag as extended if it was small, and is about to become
3ccad066
RS
17557 extended with the next value above maxtiny. */
17558 maxtiny = mips_int_operand_max (operand);
17559 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17560 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17561 {
17562 fragp->fr_subtype =
1425c41d 17563 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17564 return 1;
17565 }
17566 }
252b5132 17567
3ccad066 17568 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17569}
17570
8507b6e7
MR
17571/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17572 macro expansion. SEC is the section the frag is in. We only
17573 support PC-relative instructions (LA, DLA, LW, LD) here, in
17574 non-PIC code using 32-bit addressing. */
17575
17576static int
17577mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17578{
17579 const struct mips_pcrel_operand *pcrel_op;
17580 const struct mips_int_operand *operand;
17581 offsetT val;
17582 segT symsec;
17583 int type;
17584
17585 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17586
17587 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17588 return 0;
17589 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17590 return 0;
17591
17592 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17593 switch (type)
17594 {
17595 case 'A':
17596 case 'B':
17597 case 'E':
17598 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17599 if (bfd_is_abs_section (symsec))
17600 return 1;
17601 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17602 return 0;
17603 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17604 return 1;
17605
17606 operand = mips16_immed_operand (type, TRUE);
17607 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17608 pcrel_op = (const struct mips_pcrel_operand *) operand;
17609 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17610
17611 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17612
17613 default:
17614 return 0;
17615 }
17616}
17617
4a6a3df4
AO
17618/* Compute the length of a branch sequence, and adjust the
17619 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17620 worst-case length is computed, with UPDATE being used to indicate
17621 whether an unconditional (-1), branch-likely (+1) or regular (0)
17622 branch is to be computed. */
17623static int
17a2f251 17624relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17625{
b34976b6 17626 bfd_boolean toofar;
4a6a3df4
AO
17627 int length;
17628
17629 if (fragp
17630 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17631 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17632 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17633 {
17634 addressT addr;
17635 offsetT val;
17636
17637 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17638
17639 addr = fragp->fr_address + fragp->fr_fix + 4;
17640
17641 val -= addr;
17642
17643 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17644 }
4a6a3df4 17645 else
c1f61bd2
MR
17646 /* If the symbol is not defined or it's in a different segment,
17647 we emit the long sequence. */
b34976b6 17648 toofar = TRUE;
4a6a3df4
AO
17649
17650 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17651 fragp->fr_subtype
66b3e8da 17652 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17653 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17654 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17655 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17656 RELAX_BRANCH_LINK (fragp->fr_subtype),
17657 toofar);
17658
17659 length = 4;
17660 if (toofar)
17661 {
17662 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17663 length += 8;
17664
ce8ad872 17665 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17666 {
17667 /* Additional space for PIC loading of target address. */
17668 length += 8;
17669 if (mips_opts.isa == ISA_MIPS1)
17670 /* Additional space for $at-stabilizing nop. */
17671 length += 4;
17672 }
17673
17674 /* If branch is conditional. */
17675 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17676 length += 8;
17677 }
b34976b6 17678
4a6a3df4
AO
17679 return length;
17680}
17681
7bd374a4
MR
17682/* Get a FRAG's branch instruction delay slot size, either from the
17683 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17684 or SHORT_INSN_SIZE otherwise. */
17685
17686static int
17687frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17688{
17689 char *buf = fragp->fr_literal + fragp->fr_fix;
17690
17691 if (al)
17692 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17693 else
17694 return short_insn_size;
17695}
17696
df58fc94
RS
17697/* Compute the length of a branch sequence, and adjust the
17698 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17699 worst-case length is computed, with UPDATE being used to indicate
17700 whether an unconditional (-1), or regular (0) branch is to be
17701 computed. */
17702
17703static int
17704relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17705{
7bd374a4
MR
17706 bfd_boolean insn32 = TRUE;
17707 bfd_boolean nods = TRUE;
ce8ad872 17708 bfd_boolean pic = TRUE;
7bd374a4
MR
17709 bfd_boolean al = TRUE;
17710 int short_insn_size;
df58fc94
RS
17711 bfd_boolean toofar;
17712 int length;
17713
7bd374a4
MR
17714 if (fragp)
17715 {
17716 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17717 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17718 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17719 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17720 }
17721 short_insn_size = insn32 ? 4 : 2;
17722
df58fc94
RS
17723 if (fragp
17724 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17725 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17726 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17727 {
17728 addressT addr;
17729 offsetT val;
17730
17731 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17732 /* Ignore the low bit in the target, since it will be set
17733 for a text label. */
17734 if ((val & 1) != 0)
17735 --val;
17736
17737 addr = fragp->fr_address + fragp->fr_fix + 4;
17738
17739 val -= addr;
17740
17741 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17742 }
df58fc94 17743 else
c1f61bd2
MR
17744 /* If the symbol is not defined or it's in a different segment,
17745 we emit the long sequence. */
df58fc94
RS
17746 toofar = TRUE;
17747
17748 if (fragp && update
17749 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17750 fragp->fr_subtype = (toofar
17751 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17752 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17753
17754 length = 4;
17755 if (toofar)
17756 {
17757 bfd_boolean compact_known = fragp != NULL;
17758 bfd_boolean compact = FALSE;
17759 bfd_boolean uncond;
17760
df58fc94 17761 if (fragp)
8484fb75
MR
17762 {
17763 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17764 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17765 }
df58fc94
RS
17766 else
17767 uncond = update < 0;
17768
17769 /* If label is out of range, we turn branch <br>:
17770
17771 <br> label # 4 bytes
17772 0:
17773
17774 into:
17775
17776 j label # 4 bytes
8484fb75
MR
17777 nop # 2/4 bytes if
17778 # compact && (!PIC || insn32)
df58fc94
RS
17779 0:
17780 */
ce8ad872 17781 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17782 length += short_insn_size;
df58fc94
RS
17783
17784 /* If assembling PIC code, we further turn:
17785
17786 j label # 4 bytes
17787
17788 into:
17789
17790 lw/ld at, %got(label)(gp) # 4 bytes
17791 d/addiu at, %lo(label) # 4 bytes
8484fb75 17792 jr/c at # 2/4 bytes
df58fc94 17793 */
ce8ad872 17794 if (pic)
8484fb75 17795 length += 4 + short_insn_size;
df58fc94 17796
7bd374a4
MR
17797 /* Add an extra nop if the jump has no compact form and we need
17798 to fill the delay slot. */
ce8ad872 17799 if ((!pic || al) && nods)
7bd374a4
MR
17800 length += (fragp
17801 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17802 : short_insn_size);
17803
df58fc94
RS
17804 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17805
17806 <brneg> 0f # 4 bytes
8484fb75 17807 nop # 2/4 bytes if !compact
df58fc94
RS
17808 */
17809 if (!uncond)
8484fb75 17810 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17811 }
7bd374a4
MR
17812 else if (nods)
17813 {
17814 /* Add an extra nop to fill the delay slot. */
17815 gas_assert (fragp);
17816 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17817 }
df58fc94
RS
17818
17819 return length;
17820}
17821
17822/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17823 bit accordingly. */
17824
17825static int
17826relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17827{
17828 bfd_boolean toofar;
17829
df58fc94
RS
17830 if (fragp
17831 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17832 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17833 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17834 {
17835 addressT addr;
17836 offsetT val;
17837 int type;
17838
17839 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17840 /* Ignore the low bit in the target, since it will be set
17841 for a text label. */
17842 if ((val & 1) != 0)
17843 --val;
17844
17845 /* Assume this is a 2-byte branch. */
17846 addr = fragp->fr_address + fragp->fr_fix + 2;
17847
17848 /* We try to avoid the infinite loop by not adding 2 more bytes for
17849 long branches. */
17850
17851 val -= addr;
17852
17853 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17854 if (type == 'D')
17855 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17856 else if (type == 'E')
17857 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17858 else
17859 abort ();
17860 }
17861 else
17862 /* If the symbol is not defined or it's in a different segment,
17863 we emit a normal 32-bit branch. */
17864 toofar = TRUE;
17865
17866 if (fragp && update
17867 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17868 fragp->fr_subtype
17869 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17870 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17871
17872 if (toofar)
17873 return 4;
17874
17875 return 2;
17876}
17877
252b5132
RH
17878/* Estimate the size of a frag before relaxing. Unless this is the
17879 mips16, we are not really relaxing here, and the final size is
17880 encoded in the subtype information. For the mips16, we have to
17881 decide whether we are using an extended opcode or not. */
17882
252b5132 17883int
17a2f251 17884md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17885{
5919d012 17886 int change;
252b5132 17887
4a6a3df4
AO
17888 if (RELAX_BRANCH_P (fragp->fr_subtype))
17889 {
17890
b34976b6
AM
17891 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17892
4a6a3df4
AO
17893 return fragp->fr_var;
17894 }
17895
252b5132 17896 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
17897 {
17898 /* We don't want to modify the EXTENDED bit here; it might get us
17899 into infinite loops. We change it only in mips_relax_frag(). */
17900 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 17901 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
17902 else
17903 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17904 }
252b5132 17905
df58fc94
RS
17906 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17907 {
17908 int length = 4;
17909
17910 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17911 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17912 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17913 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17914 fragp->fr_var = length;
17915
17916 return length;
17917 }
17918
ce8ad872 17919 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17920 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17921 change = 0;
ce8ad872
MR
17922 else if (RELAX_PIC (fragp->fr_subtype))
17923 change = pic_need_relax (fragp->fr_symbol);
252b5132 17924 else
ce8ad872 17925 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17926
17927 if (change)
17928 {
4d7206a2 17929 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17930 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17931 }
4d7206a2
RS
17932 else
17933 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17934}
17935
17936/* This is called to see whether a reloc against a defined symbol
de7e6852 17937 should be converted into a reloc against a section. */
252b5132
RH
17938
17939int
17a2f251 17940mips_fix_adjustable (fixS *fixp)
252b5132 17941{
252b5132
RH
17942 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17943 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17944 return 0;
a161fe53 17945
252b5132
RH
17946 if (fixp->fx_addsy == NULL)
17947 return 1;
a161fe53 17948
2f0c68f2
CM
17949 /* Allow relocs used for EH tables. */
17950 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17951 return 1;
17952
de7e6852
RS
17953 /* If symbol SYM is in a mergeable section, relocations of the form
17954 SYM + 0 can usually be made section-relative. The mergeable data
17955 is then identified by the section offset rather than by the symbol.
17956
17957 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17958 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17959 need to recalculate the complete offset in order to correctly identify
17960 the merge data.
17961
33eaf5de 17962 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17963 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17964 placed anywhere. Rather than break backwards compatibility by changing
17965 this, it seems better not to force the issue, and instead keep the
17966 original symbol. This will work with either linker behavior. */
738e5348 17967 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17968 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17969 && HAVE_IN_PLACE_ADDENDS
17970 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17971 return 0;
17972
97f50151
MR
17973 /* There is no place to store an in-place offset for JALR relocations. */
17974 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17975 return 0;
17976
17977 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17978 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17979 start address of the symbol's containing section.
17980
17981 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17982 section relative to allow linker relaxations to be performed later on. */
97f50151 17983 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17984 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17985 return 0;
17986
b314ec0e
RS
17987 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17988 to a floating-point stub. The same is true for non-R_MIPS16_26
17989 relocations against MIPS16 functions; in this case, the stub becomes
17990 the function's canonical address.
17991
17992 Floating-point stubs are stored in unique .mips16.call.* or
17993 .mips16.fn.* sections. If a stub T for function F is in section S,
17994 the first relocation in section S must be against F; this is how the
17995 linker determines the target function. All relocations that might
17996 resolve to T must also be against F. We therefore have the following
17997 restrictions, which are given in an intentionally-redundant way:
17998
17999 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
18000 symbols.
18001
18002 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
18003 if that stub might be used.
18004
18005 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
18006 symbols.
18007
18008 4. We cannot reduce a stub's relocations against MIPS16 symbols if
18009 that stub might be used.
18010
18011 There is a further restriction:
18012
df58fc94 18013 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 18014 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
18015 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
18016 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
18017 against MIPS16 or microMIPS symbols because we need to keep the
18018 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
18019 detection and JAL or BAL to JALX instruction conversion in the
18020 linker.
b314ec0e 18021
df58fc94 18022 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 18023 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 18024 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
18025
18026 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
18027 relocation against some symbol R, no relocation against R may be
18028 reduced. (Note that this deals with (2) as well as (1) because
18029 relocations against global symbols will never be reduced on ELF
18030 targets.) This approach is a little simpler than trying to detect
18031 stub sections, and gives the "all or nothing" per-symbol consistency
18032 that we have for MIPS16 symbols. */
f3ded42a 18033 if (fixp->fx_subsy == NULL
30c09090 18034 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 18035 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
18036 && (jmp_reloc_p (fixp->fx_r_type)
18037 || b_reloc_p (fixp->fx_r_type)))
44d3da23 18038 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18039 return 0;
a161fe53 18040
252b5132
RH
18041 return 1;
18042}
18043
18044/* Translate internal representation of relocation info to BFD target
18045 format. */
18046
18047arelent **
17a2f251 18048tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18049{
18050 static arelent *retval[4];
18051 arelent *reloc;
18052 bfd_reloc_code_real_type code;
18053
4b0cff4e 18054 memset (retval, 0, sizeof(retval));
325801bd
TS
18055 reloc = retval[0] = XCNEW (arelent);
18056 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18057 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18058 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18059
bad36eac
DJ
18060 if (fixp->fx_pcrel)
18061 {
df58fc94 18062 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18063 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18064 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18065 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18066 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18067 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18068 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18069 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18070 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18071 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18072 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18073 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18074
18075 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18076 Relocations want only the symbol offset. */
51f6035b
MR
18077 switch (fixp->fx_r_type)
18078 {
18079 case BFD_RELOC_MIPS_18_PCREL_S3:
18080 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18081 break;
18082 default:
18083 reloc->addend = fixp->fx_addnumber + reloc->address;
18084 break;
18085 }
bad36eac 18086 }
17c6c9d9
MR
18087 else if (HAVE_IN_PLACE_ADDENDS
18088 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18089 && (read_compressed_insn (fixp->fx_frag->fr_literal
18090 + fixp->fx_where, 4) >> 26) == 0x3c)
18091 {
18092 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18093 addend accordingly. */
18094 reloc->addend = fixp->fx_addnumber >> 1;
18095 }
bad36eac
DJ
18096 else
18097 reloc->addend = fixp->fx_addnumber;
252b5132 18098
438c16b8
TS
18099 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18100 entry to be used in the relocation's section offset. */
18101 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18102 {
18103 reloc->address = reloc->addend;
18104 reloc->addend = 0;
18105 }
18106
252b5132 18107 code = fixp->fx_r_type;
252b5132 18108
bad36eac 18109 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18110 if (reloc->howto == NULL)
18111 {
18112 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18113 _("cannot represent %s relocation in this object file"
18114 " format"),
252b5132
RH
18115 bfd_get_reloc_code_name (code));
18116 retval[0] = NULL;
18117 }
18118
18119 return retval;
18120}
18121
18122/* Relax a machine dependent frag. This returns the amount by which
18123 the current size of the frag should change. */
18124
18125int
17a2f251 18126mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18127{
4a6a3df4
AO
18128 if (RELAX_BRANCH_P (fragp->fr_subtype))
18129 {
18130 offsetT old_var = fragp->fr_var;
b34976b6
AM
18131
18132 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18133
18134 return fragp->fr_var - old_var;
18135 }
18136
df58fc94
RS
18137 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18138 {
18139 offsetT old_var = fragp->fr_var;
18140 offsetT new_var = 4;
18141
18142 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18143 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18144 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18145 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18146 fragp->fr_var = new_var;
18147
18148 return new_var - old_var;
18149 }
18150
252b5132
RH
18151 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18152 return 0;
18153
8507b6e7 18154 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18155 {
8507b6e7
MR
18156 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18157 {
18158 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18159 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18160 }
18161 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18162 {
18163 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18164 return -2;
18165 }
18166 else
18167 return 0;
18168 }
18169 else if (!mips16_macro_frag (fragp, sec, stretch))
18170 {
18171 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18172 {
18173 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18174 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18175 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18176 }
18177 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18178 {
18179 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18180 return 2;
18181 }
18182 else
252b5132 18183 return 0;
252b5132
RH
18184 }
18185 else
18186 {
8507b6e7 18187 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18188 return 0;
8507b6e7
MR
18189 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18190 {
18191 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18192 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18193 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18194 }
18195 else
18196 {
18197 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18198 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18199 }
252b5132
RH
18200 }
18201
18202 return 0;
18203}
18204
18205/* Convert a machine dependent frag. */
18206
18207void
17a2f251 18208md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18209{
4a6a3df4
AO
18210 if (RELAX_BRANCH_P (fragp->fr_subtype))
18211 {
4d68580a 18212 char *buf;
4a6a3df4 18213 unsigned long insn;
4a6a3df4 18214 fixS *fixp;
b34976b6 18215
4d68580a
RS
18216 buf = fragp->fr_literal + fragp->fr_fix;
18217 insn = read_insn (buf);
b34976b6 18218
4a6a3df4
AO
18219 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18220 {
18221 /* We generate a fixup instead of applying it right now
18222 because, if there are linker relaxations, we're going to
18223 need the relocations. */
bbd27b76
MR
18224 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18225 fragp->fr_symbol, fragp->fr_offset,
18226 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18227 fixp->fx_file = fragp->fr_file;
18228 fixp->fx_line = fragp->fr_line;
b34976b6 18229
4d68580a 18230 buf = write_insn (buf, insn);
4a6a3df4
AO
18231 }
18232 else
18233 {
18234 int i;
18235
18236 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18237 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18238
18239 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18240 goto uncond;
18241
18242 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18243 {
18244 /* Reverse the branch. */
18245 switch ((insn >> 28) & 0xf)
18246 {
18247 case 4:
56d438b1
CF
18248 if ((insn & 0xff000000) == 0x47000000
18249 || (insn & 0xff600000) == 0x45600000)
18250 {
18251 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18252 reversed by tweaking bit 23. */
18253 insn ^= 0x00800000;
18254 }
18255 else
18256 {
18257 /* bc[0-3][tf]l? instructions can have the condition
18258 reversed by tweaking a single TF bit, and their
18259 opcodes all have 0x4???????. */
18260 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18261 insn ^= 0x00010000;
18262 }
4a6a3df4
AO
18263 break;
18264
18265 case 0:
18266 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18267 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18268 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18269 insn ^= 0x00010000;
18270 break;
b34976b6 18271
4a6a3df4
AO
18272 case 1:
18273 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18274 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18275 insn ^= 0x04000000;
18276 break;
18277
18278 default:
18279 abort ();
18280 }
18281 }
18282
18283 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18284 {
18285 /* Clear the and-link bit. */
9c2799c2 18286 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18287
54f4ddb3
TS
18288 /* bltzal 0x04100000 bgezal 0x04110000
18289 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18290 insn &= ~0x00100000;
18291 }
18292
18293 /* Branch over the branch (if the branch was likely) or the
18294 full jump (not likely case). Compute the offset from the
18295 current instruction to branch to. */
18296 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18297 i = 16;
18298 else
18299 {
18300 /* How many bytes in instructions we've already emitted? */
4d68580a 18301 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18302 /* How many bytes in instructions from here to the end? */
18303 i = fragp->fr_var - i;
18304 }
18305 /* Convert to instruction count. */
18306 i >>= 2;
18307 /* Branch counts from the next instruction. */
b34976b6 18308 i--;
4a6a3df4
AO
18309 insn |= i;
18310 /* Branch over the jump. */
4d68580a 18311 buf = write_insn (buf, insn);
4a6a3df4 18312
54f4ddb3 18313 /* nop */
4d68580a 18314 buf = write_insn (buf, 0);
4a6a3df4
AO
18315
18316 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18317 {
18318 /* beql $0, $0, 2f */
18319 insn = 0x50000000;
18320 /* Compute the PC offset from the current instruction to
18321 the end of the variable frag. */
18322 /* How many bytes in instructions we've already emitted? */
4d68580a 18323 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18324 /* How many bytes in instructions from here to the end? */
18325 i = fragp->fr_var - i;
18326 /* Convert to instruction count. */
18327 i >>= 2;
18328 /* Don't decrement i, because we want to branch over the
18329 delay slot. */
4a6a3df4 18330 insn |= i;
4a6a3df4 18331
4d68580a
RS
18332 buf = write_insn (buf, insn);
18333 buf = write_insn (buf, 0);
4a6a3df4
AO
18334 }
18335
18336 uncond:
ce8ad872 18337 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18338 {
18339 /* j or jal. */
18340 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18341 ? 0x0c000000 : 0x08000000);
4a6a3df4 18342
bbd27b76
MR
18343 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18344 fragp->fr_symbol, fragp->fr_offset,
18345 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18346 fixp->fx_file = fragp->fr_file;
18347 fixp->fx_line = fragp->fr_line;
18348
4d68580a 18349 buf = write_insn (buf, insn);
4a6a3df4
AO
18350 }
18351 else
18352 {
66b3e8da
MR
18353 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18354
4a6a3df4 18355 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18356 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18357 insn |= at << OP_SH_RT;
4a6a3df4 18358
bbd27b76
MR
18359 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18360 fragp->fr_symbol, fragp->fr_offset,
18361 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18362 fixp->fx_file = fragp->fr_file;
18363 fixp->fx_line = fragp->fr_line;
18364
4d68580a 18365 buf = write_insn (buf, insn);
b34976b6 18366
4a6a3df4 18367 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18368 /* nop */
18369 buf = write_insn (buf, 0);
4a6a3df4
AO
18370
18371 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18372 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18373 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18374
bbd27b76
MR
18375 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18376 fragp->fr_symbol, fragp->fr_offset,
18377 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18378 fixp->fx_file = fragp->fr_file;
18379 fixp->fx_line = fragp->fr_line;
b34976b6 18380
4d68580a 18381 buf = write_insn (buf, insn);
4a6a3df4
AO
18382
18383 /* j(al)r $at. */
18384 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18385 insn = 0x0000f809;
4a6a3df4 18386 else
66b3e8da
MR
18387 insn = 0x00000008;
18388 insn |= at << OP_SH_RS;
4a6a3df4 18389
4d68580a 18390 buf = write_insn (buf, insn);
4a6a3df4
AO
18391 }
18392 }
18393
4a6a3df4 18394 fragp->fr_fix += fragp->fr_var;
4d68580a 18395 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18396 return;
18397 }
18398
df58fc94
RS
18399 /* Relax microMIPS branches. */
18400 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18401 {
4d68580a 18402 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18403 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18404 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18405 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18406 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18407 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18408 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18409 bfd_boolean short_ds;
df58fc94 18410 unsigned long insn;
df58fc94
RS
18411 fixS *fixp;
18412
df58fc94
RS
18413 fragp->fr_fix += fragp->fr_var;
18414
18415 /* Handle 16-bit branches that fit or are forced to fit. */
18416 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18417 {
18418 /* We generate a fixup instead of applying it right now,
18419 because if there is linker relaxation, we're going to
18420 need the relocations. */
834a65aa
MR
18421 switch (type)
18422 {
18423 case 'D':
18424 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18425 fragp->fr_symbol, fragp->fr_offset,
18426 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18427 break;
18428 case 'E':
18429 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18430 fragp->fr_symbol, fragp->fr_offset,
18431 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18432 break;
18433 default:
18434 abort ();
18435 }
df58fc94
RS
18436
18437 fixp->fx_file = fragp->fr_file;
18438 fixp->fx_line = fragp->fr_line;
18439
18440 /* These relocations can have an addend that won't fit in
18441 2 octets. */
18442 fixp->fx_no_overflow = 1;
18443
18444 return;
18445 }
18446
2309ddf2 18447 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18448 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18449 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18450 {
18451 /* We generate a fixup instead of applying it right now,
18452 because if there is linker relaxation, we're going to
18453 need the relocations. */
bbd27b76
MR
18454 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18455 fragp->fr_symbol, fragp->fr_offset,
18456 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18457 fixp->fx_file = fragp->fr_file;
18458 fixp->fx_line = fragp->fr_line;
18459
18460 if (type == 0)
7bd374a4
MR
18461 {
18462 insn = read_compressed_insn (buf, 4);
18463 buf += 4;
18464
18465 if (nods)
18466 {
18467 /* Check the short-delay-slot bit. */
18468 if (!al || (insn & 0x02000000) != 0)
18469 buf = write_compressed_insn (buf, 0x0c00, 2);
18470 else
18471 buf = write_compressed_insn (buf, 0x00000000, 4);
18472 }
18473
18474 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18475 return;
18476 }
df58fc94
RS
18477 }
18478
18479 /* Relax 16-bit branches to 32-bit branches. */
18480 if (type != 0)
18481 {
4d68580a 18482 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18483
18484 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18485 insn = 0x94000000; /* beq */
18486 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18487 {
18488 unsigned long regno;
18489
18490 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18491 regno = micromips_to_32_reg_d_map [regno];
18492 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18493 insn |= regno << MICROMIPSOP_SH_RS;
18494 }
18495 else
18496 abort ();
18497
18498 /* Nothing else to do, just write it out. */
18499 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18500 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18501 {
4d68580a 18502 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18503 if (nods)
18504 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18505 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18506 return;
18507 }
18508 }
18509 else
4d68580a 18510 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18511
18512 /* Relax 32-bit branches to a sequence of instructions. */
18513 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18514 _("relaxed out-of-range branch into a jump"));
df58fc94 18515
2309ddf2 18516 /* Set the short-delay-slot bit. */
7bd374a4 18517 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18518
18519 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18520 {
18521 symbolS *l;
18522
18523 /* Reverse the branch. */
18524 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18525 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18526 insn ^= 0x20000000;
18527 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18528 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18529 || (insn & 0xffe00000) == 0x40800000 /* blez */
18530 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18531 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18532 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18533 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18534 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18535 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18536 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18537 insn ^= 0x00400000;
18538 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18539 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18540 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18541 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18542 insn ^= 0x00200000;
56d438b1
CF
18543 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18544 BNZ.df */
18545 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18546 BNZ.V */
18547 insn ^= 0x00800000;
df58fc94
RS
18548 else
18549 abort ();
18550
18551 if (al)
18552 {
18553 /* Clear the and-link and short-delay-slot bits. */
18554 gas_assert ((insn & 0xfda00000) == 0x40200000);
18555
18556 /* bltzal 0x40200000 bgezal 0x40600000 */
18557 /* bltzals 0x42200000 bgezals 0x42600000 */
18558 insn &= ~0x02200000;
18559 }
18560
18561 /* Make a label at the end for use with the branch. */
18562 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18563 micromips_label_inc ();
f3ded42a 18564 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18565
18566 /* Refer to it. */
4d68580a
RS
18567 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18568 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18569 fixp->fx_file = fragp->fr_file;
18570 fixp->fx_line = fragp->fr_line;
18571
18572 /* Branch over the jump. */
4d68580a 18573 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18574
df58fc94 18575 if (!compact)
8484fb75
MR
18576 {
18577 /* nop */
18578 if (insn32)
18579 buf = write_compressed_insn (buf, 0x00000000, 4);
18580 else
18581 buf = write_compressed_insn (buf, 0x0c00, 2);
18582 }
df58fc94
RS
18583 }
18584
ce8ad872 18585 if (!pic)
df58fc94 18586 {
7bd374a4
MR
18587 unsigned long jal = (short_ds || nods
18588 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18589
df58fc94
RS
18590 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18591 insn = al ? jal : 0xd4000000;
18592
bbd27b76
MR
18593 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18594 fragp->fr_symbol, fragp->fr_offset,
18595 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18596 fixp->fx_file = fragp->fr_file;
18597 fixp->fx_line = fragp->fr_line;
18598
4d68580a 18599 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18600
7bd374a4 18601 if (compact || nods)
8484fb75
MR
18602 {
18603 /* nop */
18604 if (insn32)
18605 buf = write_compressed_insn (buf, 0x00000000, 4);
18606 else
18607 buf = write_compressed_insn (buf, 0x0c00, 2);
18608 }
df58fc94
RS
18609 }
18610 else
18611 {
18612 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18613
18614 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18615 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18616 insn |= at << MICROMIPSOP_SH_RT;
18617
bbd27b76
MR
18618 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18619 fragp->fr_symbol, fragp->fr_offset,
18620 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18621 fixp->fx_file = fragp->fr_file;
18622 fixp->fx_line = fragp->fr_line;
18623
4d68580a 18624 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18625
18626 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18627 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18628 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18629
bbd27b76
MR
18630 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18631 fragp->fr_symbol, fragp->fr_offset,
18632 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18633 fixp->fx_file = fragp->fr_file;
18634 fixp->fx_line = fragp->fr_line;
18635
4d68580a 18636 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18637
8484fb75
MR
18638 if (insn32)
18639 {
18640 /* jr/jalr $at */
18641 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18642 insn |= at << MICROMIPSOP_SH_RS;
18643
18644 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18645
7bd374a4 18646 if (compact || nods)
8484fb75
MR
18647 /* nop */
18648 buf = write_compressed_insn (buf, 0x00000000, 4);
18649 }
18650 else
18651 {
18652 /* jr/jrc/jalr/jalrs $at */
18653 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18654 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18655
18656 insn = al ? jalr : jr;
18657 insn |= at << MICROMIPSOP_SH_MJ;
18658
18659 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18660 if (al && nods)
18661 {
18662 /* nop */
18663 if (short_ds)
18664 buf = write_compressed_insn (buf, 0x0c00, 2);
18665 else
18666 buf = write_compressed_insn (buf, 0x00000000, 4);
18667 }
8484fb75 18668 }
df58fc94
RS
18669 }
18670
4d68580a 18671 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18672 return;
18673 }
18674
252b5132
RH
18675 if (RELAX_MIPS16_P (fragp->fr_subtype))
18676 {
18677 int type;
3ccad066 18678 const struct mips_int_operand *operand;
252b5132 18679 offsetT val;
5c04167a 18680 char *buf;
8507b6e7 18681 unsigned int user_length;
9d862524 18682 bfd_boolean need_reloc;
252b5132 18683 unsigned long insn;
8507b6e7 18684 bfd_boolean mac;
5c04167a 18685 bfd_boolean ext;
88a7ef16 18686 segT symsec;
252b5132
RH
18687
18688 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18689 operand = mips16_immed_operand (type, FALSE);
252b5132 18690
8507b6e7 18691 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18692 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18693 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18694
18695 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18696 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18697 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18698 ? asec != symsec
18699 : !bfd_is_abs_section (symsec)));
18700
8507b6e7 18701 if (operand->root.type == OP_PCREL && !mac)
252b5132 18702 {
3ccad066 18703 const struct mips_pcrel_operand *pcrel_op;
252b5132 18704
3ccad066 18705 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18706
14f72d45 18707 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18708 {
37b2d327
MR
18709 if (!mips_ignore_branch_isa
18710 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
18711 as_bad_where (fragp->fr_file, fragp->fr_line,
18712 _("branch to a symbol in another ISA mode"));
18713 else if ((fragp->fr_offset & 0x1) != 0)
18714 as_bad_where (fragp->fr_file, fragp->fr_line,
18715 _("branch to misaligned address (0x%lx)"),
18716 (long) val);
252b5132 18717 }
252b5132 18718
14f72d45 18719 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18720
18721 /* Make sure the section winds up with the alignment we have
18722 assumed. */
3ccad066
RS
18723 if (operand->shift > 0)
18724 record_alignment (asec, operand->shift);
252b5132
RH
18725 }
18726
8507b6e7
MR
18727 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18728 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18729 {
18730 if (mac)
18731 as_warn_where (fragp->fr_file, fragp->fr_line,
18732 _("macro instruction expanded into multiple "
18733 "instructions in a branch delay slot"));
18734 else if (ext)
18735 as_warn_where (fragp->fr_file, fragp->fr_line,
18736 _("extended instruction in a branch delay slot"));
18737 }
18738 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18739 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18740 _("macro instruction expanded into multiple "
18741 "instructions"));
252b5132 18742
5c04167a 18743 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18744
4d68580a 18745 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18746 if (ext)
18747 insn |= MIPS16_EXTEND;
252b5132 18748
5c04167a
RS
18749 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18750 user_length = 4;
18751 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18752 user_length = 2;
18753 else
18754 user_length = 0;
18755
8507b6e7 18756 if (mac)
c9775dde 18757 {
8507b6e7
MR
18758 unsigned long reg;
18759 unsigned long new;
18760 unsigned long op;
25499ac7 18761 bfd_boolean e2;
8507b6e7
MR
18762
18763 gas_assert (type == 'A' || type == 'B' || type == 'E');
18764 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18765
25499ac7
MR
18766 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18767
8507b6e7 18768 if (need_reloc)
c9775dde 18769 {
8507b6e7
MR
18770 fixS *fixp;
18771
18772 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18773
18774 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18775 fragp->fr_symbol, fragp->fr_offset,
18776 FALSE, BFD_RELOC_MIPS16_HI16_S);
18777 fixp->fx_file = fragp->fr_file;
18778 fixp->fx_line = fragp->fr_line;
18779
25499ac7 18780 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
18781 fragp->fr_symbol, fragp->fr_offset,
18782 FALSE, BFD_RELOC_MIPS16_LO16);
18783 fixp->fx_file = fragp->fr_file;
18784 fixp->fx_line = fragp->fr_line;
18785
18786 val = 0;
18787 }
18788
18789 switch (insn & 0xf800)
18790 {
18791 case 0x0800: /* ADDIU */
18792 reg = (insn >> 8) & 0x7;
18793 op = 0xf0004800 | (reg << 8);
c9775dde 18794 break;
8507b6e7
MR
18795 case 0xb000: /* LW */
18796 reg = (insn >> 8) & 0x7;
18797 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18798 break;
8507b6e7
MR
18799 case 0xf800: /* I64 */
18800 reg = (insn >> 5) & 0x7;
18801 switch (insn & 0x0700)
18802 {
18803 case 0x0400: /* LD */
18804 op = 0xf0003800 | (reg << 8) | (reg << 5);
18805 break;
18806 case 0x0600: /* DADDIU */
18807 op = 0xf000fd00 | (reg << 5);
18808 break;
18809 default:
18810 abort ();
18811 }
18812 break;
18813 default:
18814 abort ();
c9775dde 18815 }
8507b6e7 18816
25499ac7 18817 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
18818 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18819 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
18820 if (!e2)
18821 {
18822 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18823 buf = write_compressed_insn (buf, new, 4);
18824 }
8507b6e7
MR
18825 op |= mips16_immed_extend (val, 16);
18826 buf = write_compressed_insn (buf, op, 4);
18827
25499ac7 18828 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
18829 }
18830 else
18831 {
18832 unsigned int length = ext ? 4 : 2;
18833
18834 if (need_reloc)
c9775dde 18835 {
8507b6e7 18836 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 18837 fixS *fixp;
c9775dde 18838
8507b6e7
MR
18839 switch (type)
18840 {
18841 case 'p':
18842 case 'q':
18843 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18844 break;
18845 default:
18846 break;
18847 }
18848 if (mac || reloc == BFD_RELOC_NONE)
18849 as_bad_where (fragp->fr_file, fragp->fr_line,
18850 _("unsupported relocation"));
18851 else if (ext)
18852 {
bbd27b76
MR
18853 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18854 fragp->fr_symbol, fragp->fr_offset,
18855 TRUE, reloc);
8507b6e7
MR
18856 fixp->fx_file = fragp->fr_file;
18857 fixp->fx_line = fragp->fr_line;
18858 }
18859 else
18860 as_bad_where (fragp->fr_file, fragp->fr_line,
18861 _("invalid unextended operand value"));
c9775dde 18862 }
eefc3365 18863 else
8507b6e7
MR
18864 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18865 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18866
8507b6e7
MR
18867 gas_assert (mips16_opcode_length (insn) == length);
18868 write_compressed_insn (buf, insn, length);
18869 fragp->fr_fix += length;
18870 }
252b5132
RH
18871 }
18872 else
18873 {
df58fc94
RS
18874 relax_substateT subtype = fragp->fr_subtype;
18875 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18876 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18877 int first, second;
18878 fixS *fixp;
252b5132 18879
df58fc94
RS
18880 first = RELAX_FIRST (subtype);
18881 second = RELAX_SECOND (subtype);
4d7206a2 18882 fixp = (fixS *) fragp->fr_opcode;
252b5132 18883
df58fc94
RS
18884 /* If the delay slot chosen does not match the size of the instruction,
18885 then emit a warning. */
18886 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18887 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18888 {
18889 relax_substateT s;
18890 const char *msg;
18891
18892 s = subtype & (RELAX_DELAY_SLOT_16BIT
18893 | RELAX_DELAY_SLOT_SIZE_FIRST
18894 | RELAX_DELAY_SLOT_SIZE_SECOND);
18895 msg = macro_warning (s);
18896 if (msg != NULL)
db9b2be4 18897 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18898 subtype &= ~s;
18899 }
18900
584892a6 18901 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18902 if (use_second == second_longer)
584892a6 18903 {
df58fc94
RS
18904 relax_substateT s;
18905 const char *msg;
18906
18907 s = (subtype
18908 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18909 msg = macro_warning (s);
18910 if (msg != NULL)
db9b2be4 18911 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18912 subtype &= ~s;
584892a6
RS
18913 }
18914
4d7206a2
RS
18915 /* Go through all the fixups for the first sequence. Disable them
18916 (by marking them as done) if we're going to use the second
18917 sequence instead. */
18918 while (fixp
18919 && fixp->fx_frag == fragp
18920 && fixp->fx_where < fragp->fr_fix - second)
18921 {
df58fc94 18922 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18923 fixp->fx_done = 1;
18924 fixp = fixp->fx_next;
18925 }
252b5132 18926
4d7206a2
RS
18927 /* Go through the fixups for the second sequence. Disable them if
18928 we're going to use the first sequence, otherwise adjust their
18929 addresses to account for the relaxation. */
18930 while (fixp && fixp->fx_frag == fragp)
18931 {
df58fc94 18932 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18933 fixp->fx_where -= first;
18934 else
18935 fixp->fx_done = 1;
18936 fixp = fixp->fx_next;
18937 }
18938
18939 /* Now modify the frag contents. */
df58fc94 18940 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18941 {
18942 char *start;
18943
18944 start = fragp->fr_literal + fragp->fr_fix - first - second;
18945 memmove (start, start + first, second);
18946 fragp->fr_fix -= first;
18947 }
18948 else
18949 fragp->fr_fix -= second;
252b5132
RH
18950 }
18951}
18952
252b5132
RH
18953/* This function is called after the relocs have been generated.
18954 We've been storing mips16 text labels as odd. Here we convert them
18955 back to even for the convenience of the debugger. */
18956
18957void
17a2f251 18958mips_frob_file_after_relocs (void)
252b5132
RH
18959{
18960 asymbol **syms;
18961 unsigned int count, i;
18962
252b5132
RH
18963 syms = bfd_get_outsymbols (stdoutput);
18964 count = bfd_get_symcount (stdoutput);
18965 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18966 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18967 && ((*syms)->value & 1) != 0)
18968 {
18969 (*syms)->value &= ~1;
18970 /* If the symbol has an odd size, it was probably computed
18971 incorrectly, so adjust that as well. */
18972 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18973 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18974 }
252b5132
RH
18975}
18976
a1facbec
MR
18977/* This function is called whenever a label is defined, including fake
18978 labels instantiated off the dot special symbol. It is used when
18979 handling branch delays; if a branch has a label, we assume we cannot
18980 move it. This also bumps the value of the symbol by 1 in compressed
18981 code. */
252b5132 18982
e1b47bd5 18983static void
a1facbec 18984mips_record_label (symbolS *sym)
252b5132 18985{
a8dbcb85 18986 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18987 struct insn_label_list *l;
18988
18989 if (free_insn_labels == NULL)
325801bd 18990 l = XNEW (struct insn_label_list);
252b5132
RH
18991 else
18992 {
18993 l = free_insn_labels;
18994 free_insn_labels = l->next;
18995 }
18996
18997 l->label = sym;
a8dbcb85
TS
18998 l->next = si->label_list;
18999 si->label_list = l;
a1facbec 19000}
07a53e5c 19001
a1facbec
MR
19002/* This function is called as tc_frob_label() whenever a label is defined
19003 and adds a DWARF-2 record we only want for true labels. */
19004
19005void
19006mips_define_label (symbolS *sym)
19007{
19008 mips_record_label (sym);
07a53e5c 19009 dwarf2_emit_label (sym);
252b5132 19010}
e1b47bd5
RS
19011
19012/* This function is called by tc_new_dot_label whenever a new dot symbol
19013 is defined. */
19014
19015void
19016mips_add_dot_label (symbolS *sym)
19017{
19018 mips_record_label (sym);
19019 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
19020 mips_compressed_mark_label (sym);
19021}
252b5132 19022\f
351cdf24
MF
19023/* Converting ASE flags from internal to .MIPS.abiflags values. */
19024static unsigned int
19025mips_convert_ase_flags (int ase)
19026{
19027 unsigned int ext_ases = 0;
19028
19029 if (ase & ASE_DSP)
19030 ext_ases |= AFL_ASE_DSP;
19031 if (ase & ASE_DSPR2)
19032 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
19033 if (ase & ASE_DSPR3)
19034 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
19035 if (ase & ASE_EVA)
19036 ext_ases |= AFL_ASE_EVA;
19037 if (ase & ASE_MCU)
19038 ext_ases |= AFL_ASE_MCU;
19039 if (ase & ASE_MDMX)
19040 ext_ases |= AFL_ASE_MDMX;
19041 if (ase & ASE_MIPS3D)
19042 ext_ases |= AFL_ASE_MIPS3D;
19043 if (ase & ASE_MT)
19044 ext_ases |= AFL_ASE_MT;
19045 if (ase & ASE_SMARTMIPS)
19046 ext_ases |= AFL_ASE_SMARTMIPS;
19047 if (ase & ASE_VIRT)
19048 ext_ases |= AFL_ASE_VIRT;
19049 if (ase & ASE_MSA)
19050 ext_ases |= AFL_ASE_MSA;
19051 if (ase & ASE_XPA)
19052 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19053 if (ase & ASE_MIPS16E2)
19054 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19055 if (ase & ASE_CRC)
19056 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19057 if (ase & ASE_GINV)
19058 ext_ases |= AFL_ASE_GINV;
8095d2f7
CX
19059 if (ase & ASE_LOONGSON_MMI)
19060 ext_ases |= AFL_ASE_LOONGSON_MMI;
716c08de
CX
19061 if (ase & ASE_LOONGSON_CAM)
19062 ext_ases |= AFL_ASE_LOONGSON_CAM;
bdc6c06e
CX
19063 if (ase & ASE_LOONGSON_EXT)
19064 ext_ases |= AFL_ASE_LOONGSON_EXT;
a693765e
CX
19065 if (ase & ASE_LOONGSON_EXT2)
19066 ext_ases |= AFL_ASE_LOONGSON_EXT2;
351cdf24
MF
19067
19068 return ext_ases;
19069}
252b5132
RH
19070/* Some special processing for a MIPS ELF file. */
19071
19072void
17a2f251 19073mips_elf_final_processing (void)
252b5132 19074{
351cdf24
MF
19075 int fpabi;
19076 Elf_Internal_ABIFlags_v0 flags;
19077
19078 flags.version = 0;
19079 flags.isa_rev = 0;
19080 switch (file_mips_opts.isa)
19081 {
19082 case INSN_ISA1:
19083 flags.isa_level = 1;
19084 break;
19085 case INSN_ISA2:
19086 flags.isa_level = 2;
19087 break;
19088 case INSN_ISA3:
19089 flags.isa_level = 3;
19090 break;
19091 case INSN_ISA4:
19092 flags.isa_level = 4;
19093 break;
19094 case INSN_ISA5:
19095 flags.isa_level = 5;
19096 break;
19097 case INSN_ISA32:
19098 flags.isa_level = 32;
19099 flags.isa_rev = 1;
19100 break;
19101 case INSN_ISA32R2:
19102 flags.isa_level = 32;
19103 flags.isa_rev = 2;
19104 break;
19105 case INSN_ISA32R3:
19106 flags.isa_level = 32;
19107 flags.isa_rev = 3;
19108 break;
19109 case INSN_ISA32R5:
19110 flags.isa_level = 32;
19111 flags.isa_rev = 5;
19112 break;
09c14161
MF
19113 case INSN_ISA32R6:
19114 flags.isa_level = 32;
19115 flags.isa_rev = 6;
19116 break;
351cdf24
MF
19117 case INSN_ISA64:
19118 flags.isa_level = 64;
19119 flags.isa_rev = 1;
19120 break;
19121 case INSN_ISA64R2:
19122 flags.isa_level = 64;
19123 flags.isa_rev = 2;
19124 break;
19125 case INSN_ISA64R3:
19126 flags.isa_level = 64;
19127 flags.isa_rev = 3;
19128 break;
19129 case INSN_ISA64R5:
19130 flags.isa_level = 64;
19131 flags.isa_rev = 5;
19132 break;
09c14161
MF
19133 case INSN_ISA64R6:
19134 flags.isa_level = 64;
19135 flags.isa_rev = 6;
19136 break;
351cdf24
MF
19137 }
19138
19139 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19140 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19141 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19142 : (file_mips_opts.fp == 64) ? AFL_REG_64
19143 : AFL_REG_32;
19144 flags.cpr2_size = AFL_REG_NONE;
19145 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19146 Tag_GNU_MIPS_ABI_FP);
19147 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19148 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19149 if (file_ase_mips16)
19150 flags.ases |= AFL_ASE_MIPS16;
19151 if (file_ase_micromips)
19152 flags.ases |= AFL_ASE_MICROMIPS;
19153 flags.flags1 = 0;
19154 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19155 || file_mips_opts.fp == 64)
19156 && file_mips_opts.oddspreg)
19157 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19158 flags.flags2 = 0;
19159
19160 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19161 ((Elf_External_ABIFlags_v0 *)
19162 mips_flags_frag));
19163
252b5132 19164 /* Write out the register information. */
316f5878 19165 if (mips_abi != N64_ABI)
252b5132
RH
19166 {
19167 Elf32_RegInfo s;
19168
19169 s.ri_gprmask = mips_gprmask;
19170 s.ri_cprmask[0] = mips_cprmask[0];
19171 s.ri_cprmask[1] = mips_cprmask[1];
19172 s.ri_cprmask[2] = mips_cprmask[2];
19173 s.ri_cprmask[3] = mips_cprmask[3];
19174 /* The gp_value field is set by the MIPS ELF backend. */
19175
19176 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19177 ((Elf32_External_RegInfo *)
19178 mips_regmask_frag));
19179 }
19180 else
19181 {
19182 Elf64_Internal_RegInfo s;
19183
19184 s.ri_gprmask = mips_gprmask;
19185 s.ri_pad = 0;
19186 s.ri_cprmask[0] = mips_cprmask[0];
19187 s.ri_cprmask[1] = mips_cprmask[1];
19188 s.ri_cprmask[2] = mips_cprmask[2];
19189 s.ri_cprmask[3] = mips_cprmask[3];
19190 /* The gp_value field is set by the MIPS ELF backend. */
19191
19192 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19193 ((Elf64_External_RegInfo *)
19194 mips_regmask_frag));
19195 }
19196
19197 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19198 sort of BFD interface for this. */
19199 if (mips_any_noreorder)
19200 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19201 if (mips_pic != NO_PIC)
143d77c5 19202 {
8b828383 19203 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19204 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19205 }
19206 if (mips_abicalls)
19207 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19208
b015e599
AP
19209 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19210 defined at present; this might need to change in future. */
a4672219
TS
19211 if (file_ase_mips16)
19212 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19213 if (file_ase_micromips)
19214 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19215 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19216 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19217
bdaaa2e1 19218 /* Set the MIPS ELF ABI flags. */
316f5878 19219 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19220 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19221 else if (mips_abi == O64_ABI)
252b5132 19222 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19223 else if (mips_abi == EABI_ABI)
252b5132 19224 {
bad1aba3 19225 if (file_mips_opts.gp == 64)
252b5132
RH
19226 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19227 else
19228 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19229 }
be00bddd 19230
defc8e2b 19231 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19232
19233 if (mips_32bitmode)
19234 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19235
7361da2c 19236 if (mips_nan2008 == 1)
ba92f887
MR
19237 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19238
ad3fea08 19239 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19240 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19241 Tag_GNU_MIPS_ABI_FP);
19242 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19243 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19244}
252b5132 19245\f
beae10d5 19246typedef struct proc {
9b2f1d35
EC
19247 symbolS *func_sym;
19248 symbolS *func_end_sym;
beae10d5
KH
19249 unsigned long reg_mask;
19250 unsigned long reg_offset;
19251 unsigned long fpreg_mask;
19252 unsigned long fpreg_offset;
19253 unsigned long frame_offset;
19254 unsigned long frame_reg;
19255 unsigned long pc_reg;
19256} procS;
252b5132
RH
19257
19258static procS cur_proc;
19259static procS *cur_proc_ptr;
19260static int numprocs;
19261
df58fc94
RS
19262/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19263 as "2", and a normal nop as "0". */
19264
19265#define NOP_OPCODE_MIPS 0
19266#define NOP_OPCODE_MIPS16 1
19267#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19268
19269char
19270mips_nop_opcode (void)
19271{
df58fc94
RS
19272 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19273 return NOP_OPCODE_MICROMIPS;
19274 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19275 return NOP_OPCODE_MIPS16;
19276 else
19277 return NOP_OPCODE_MIPS;
742a56fe
RS
19278}
19279
df58fc94
RS
19280/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19281 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19282
0a9ef439 19283void
17a2f251 19284mips_handle_align (fragS *fragp)
a19d8eb0 19285{
df58fc94 19286 char nop_opcode;
742a56fe 19287 char *p;
c67a084a
NC
19288 int bytes, size, excess;
19289 valueT opcode;
742a56fe 19290
0a9ef439
RH
19291 if (fragp->fr_type != rs_align_code)
19292 return;
19293
742a56fe 19294 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19295 nop_opcode = *p;
19296 switch (nop_opcode)
a19d8eb0 19297 {
df58fc94
RS
19298 case NOP_OPCODE_MICROMIPS:
19299 opcode = micromips_nop32_insn.insn_opcode;
19300 size = 4;
19301 break;
19302 case NOP_OPCODE_MIPS16:
c67a084a
NC
19303 opcode = mips16_nop_insn.insn_opcode;
19304 size = 2;
df58fc94
RS
19305 break;
19306 case NOP_OPCODE_MIPS:
19307 default:
c67a084a
NC
19308 opcode = nop_insn.insn_opcode;
19309 size = 4;
df58fc94 19310 break;
c67a084a 19311 }
a19d8eb0 19312
c67a084a
NC
19313 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19314 excess = bytes % size;
df58fc94
RS
19315
19316 /* Handle the leading part if we're not inserting a whole number of
19317 instructions, and make it the end of the fixed part of the frag.
19318 Try to fit in a short microMIPS NOP if applicable and possible,
19319 and use zeroes otherwise. */
19320 gas_assert (excess < 4);
19321 fragp->fr_fix += excess;
19322 switch (excess)
c67a084a 19323 {
df58fc94
RS
19324 case 3:
19325 *p++ = '\0';
19326 /* Fall through. */
19327 case 2:
833794fc 19328 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19329 {
4d68580a 19330 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19331 break;
19332 }
19333 *p++ = '\0';
19334 /* Fall through. */
19335 case 1:
19336 *p++ = '\0';
19337 /* Fall through. */
19338 case 0:
19339 break;
a19d8eb0 19340 }
c67a084a
NC
19341
19342 md_number_to_chars (p, opcode, size);
19343 fragp->fr_var = size;
a19d8eb0
CP
19344}
19345
252b5132 19346static long
17a2f251 19347get_number (void)
252b5132
RH
19348{
19349 int negative = 0;
19350 long val = 0;
19351
19352 if (*input_line_pointer == '-')
19353 {
19354 ++input_line_pointer;
19355 negative = 1;
19356 }
3882b010 19357 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19358 as_bad (_("expected simple number"));
252b5132
RH
19359 if (input_line_pointer[0] == '0')
19360 {
19361 if (input_line_pointer[1] == 'x')
19362 {
19363 input_line_pointer += 2;
3882b010 19364 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19365 {
19366 val <<= 4;
19367 val |= hex_value (*input_line_pointer++);
19368 }
19369 return negative ? -val : val;
19370 }
19371 else
19372 {
19373 ++input_line_pointer;
3882b010 19374 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19375 {
19376 val <<= 3;
19377 val |= *input_line_pointer++ - '0';
19378 }
19379 return negative ? -val : val;
19380 }
19381 }
3882b010 19382 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19383 {
19384 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19385 *input_line_pointer, *input_line_pointer);
956cd1d6 19386 as_warn (_("invalid number"));
252b5132
RH
19387 return -1;
19388 }
3882b010 19389 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19390 {
19391 val *= 10;
19392 val += *input_line_pointer++ - '0';
19393 }
19394 return negative ? -val : val;
19395}
19396
19397/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19398 is an initial number which is the ECOFF file index. In the non-ECOFF
19399 case .file implies DWARF-2. */
19400
19401static void
17a2f251 19402s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19403{
ecb4347a
DJ
19404 static int first_file_directive = 0;
19405
c5dd6aab
DJ
19406 if (ECOFF_DEBUGGING)
19407 {
19408 get_number ();
19409 s_app_file (0);
19410 }
19411 else
ecb4347a
DJ
19412 {
19413 char *filename;
19414
68d20676 19415 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19416
19417 /* Versions of GCC up to 3.1 start files with a ".file"
19418 directive even for stabs output. Make sure that this
19419 ".file" is handled. Note that you need a version of GCC
19420 after 3.1 in order to support DWARF-2 on MIPS. */
19421 if (filename != NULL && ! first_file_directive)
19422 {
19423 (void) new_logical_line (filename, -1);
c04f5787 19424 s_app_file_string (filename, 0);
ecb4347a
DJ
19425 }
19426 first_file_directive = 1;
19427 }
c5dd6aab
DJ
19428}
19429
19430/* The .loc directive, implying DWARF-2. */
252b5132
RH
19431
19432static void
17a2f251 19433s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19434{
c5dd6aab
DJ
19435 if (!ECOFF_DEBUGGING)
19436 dwarf2_directive_loc (0);
252b5132
RH
19437}
19438
252b5132
RH
19439/* The .end directive. */
19440
19441static void
17a2f251 19442s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19443{
19444 symbolS *p;
252b5132 19445
7a621144
DJ
19446 /* Following functions need their own .frame and .cprestore directives. */
19447 mips_frame_reg_valid = 0;
19448 mips_cprestore_valid = 0;
19449
252b5132
RH
19450 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19451 {
19452 p = get_symbol ();
19453 demand_empty_rest_of_line ();
19454 }
19455 else
19456 p = NULL;
19457
14949570 19458 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19459 as_warn (_(".end not in text section"));
19460
19461 if (!cur_proc_ptr)
19462 {
1661c76c 19463 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19464 demand_empty_rest_of_line ();
19465 return;
19466 }
19467
19468 if (p != NULL)
19469 {
9c2799c2 19470 gas_assert (S_GET_NAME (p));
9b2f1d35 19471 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19472 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19473
19474 if (debug_type == DEBUG_STABS)
19475 stabs_generate_asm_endfunc (S_GET_NAME (p),
19476 S_GET_NAME (p));
252b5132
RH
19477 }
19478 else
19479 as_warn (_(".end directive missing or unknown symbol"));
19480
9b2f1d35
EC
19481 /* Create an expression to calculate the size of the function. */
19482 if (p && cur_proc_ptr)
19483 {
19484 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19485 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19486
19487 obj->size = exp;
19488 exp->X_op = O_subtract;
19489 exp->X_add_symbol = symbol_temp_new_now ();
19490 exp->X_op_symbol = p;
19491 exp->X_add_number = 0;
19492
19493 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19494 }
19495
5ff6a06c
MR
19496#ifdef md_flush_pending_output
19497 md_flush_pending_output ();
19498#endif
19499
ecb4347a 19500 /* Generate a .pdr section. */
f3ded42a 19501 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19502 {
19503 segT saved_seg = now_seg;
19504 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19505 expressionS exp;
19506 char *fragp;
252b5132 19507
9c2799c2 19508 gas_assert (pdr_seg);
ecb4347a 19509 subseg_set (pdr_seg, 0);
252b5132 19510
ecb4347a
DJ
19511 /* Write the symbol. */
19512 exp.X_op = O_symbol;
19513 exp.X_add_symbol = p;
19514 exp.X_add_number = 0;
19515 emit_expr (&exp, 4);
252b5132 19516
ecb4347a 19517 fragp = frag_more (7 * 4);
252b5132 19518
17a2f251
TS
19519 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19520 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19521 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19522 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19523 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19524 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19525 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19526
ecb4347a
DJ
19527 subseg_set (saved_seg, saved_subseg);
19528 }
252b5132
RH
19529
19530 cur_proc_ptr = NULL;
19531}
19532
19533/* The .aent and .ent directives. */
19534
19535static void
17a2f251 19536s_mips_ent (int aent)
252b5132 19537{
252b5132 19538 symbolS *symbolP;
252b5132
RH
19539
19540 symbolP = get_symbol ();
19541 if (*input_line_pointer == ',')
f9419b05 19542 ++input_line_pointer;
252b5132 19543 SKIP_WHITESPACE ();
3882b010 19544 if (ISDIGIT (*input_line_pointer)
d9a62219 19545 || *input_line_pointer == '-')
874e8986 19546 get_number ();
252b5132 19547
14949570 19548 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19549 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19550
19551 if (!aent && cur_proc_ptr)
9a41af64 19552 as_warn (_("missing .end"));
252b5132
RH
19553
19554 if (!aent)
19555 {
7a621144
DJ
19556 /* This function needs its own .frame and .cprestore directives. */
19557 mips_frame_reg_valid = 0;
19558 mips_cprestore_valid = 0;
19559
252b5132
RH
19560 cur_proc_ptr = &cur_proc;
19561 memset (cur_proc_ptr, '\0', sizeof (procS));
19562
9b2f1d35 19563 cur_proc_ptr->func_sym = symbolP;
252b5132 19564
f9419b05 19565 ++numprocs;
ecb4347a
DJ
19566
19567 if (debug_type == DEBUG_STABS)
19568 stabs_generate_asm_func (S_GET_NAME (symbolP),
19569 S_GET_NAME (symbolP));
252b5132
RH
19570 }
19571
7c0fc524
MR
19572 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19573
252b5132
RH
19574 demand_empty_rest_of_line ();
19575}
19576
19577/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19578 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19579 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19580 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19581 symbol table (in the mdebug section). */
19582
19583static void
17a2f251 19584s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19585{
f3ded42a
RS
19586 if (ECOFF_DEBUGGING)
19587 s_ignore (ignore);
19588 else
ecb4347a
DJ
19589 {
19590 long val;
252b5132 19591
ecb4347a
DJ
19592 if (cur_proc_ptr == (procS *) NULL)
19593 {
19594 as_warn (_(".frame outside of .ent"));
19595 demand_empty_rest_of_line ();
19596 return;
19597 }
252b5132 19598
ecb4347a
DJ
19599 cur_proc_ptr->frame_reg = tc_get_register (1);
19600
19601 SKIP_WHITESPACE ();
19602 if (*input_line_pointer++ != ','
19603 || get_absolute_expression_and_terminator (&val) != ',')
19604 {
1661c76c 19605 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19606 --input_line_pointer;
19607 demand_empty_rest_of_line ();
19608 return;
19609 }
252b5132 19610
ecb4347a
DJ
19611 cur_proc_ptr->frame_offset = val;
19612 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19613
252b5132 19614 demand_empty_rest_of_line ();
252b5132 19615 }
252b5132
RH
19616}
19617
bdaaa2e1
KH
19618/* The .fmask and .mask directives. If the mdebug section is present
19619 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19620 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19621 information correctly. We can't use the ecoff routines because they
252b5132
RH
19622 make reference to the ecoff symbol table (in the mdebug section). */
19623
19624static void
17a2f251 19625s_mips_mask (int reg_type)
252b5132 19626{
f3ded42a
RS
19627 if (ECOFF_DEBUGGING)
19628 s_ignore (reg_type);
19629 else
252b5132 19630 {
ecb4347a 19631 long mask, off;
252b5132 19632
ecb4347a
DJ
19633 if (cur_proc_ptr == (procS *) NULL)
19634 {
19635 as_warn (_(".mask/.fmask outside of .ent"));
19636 demand_empty_rest_of_line ();
19637 return;
19638 }
252b5132 19639
ecb4347a
DJ
19640 if (get_absolute_expression_and_terminator (&mask) != ',')
19641 {
1661c76c 19642 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19643 --input_line_pointer;
19644 demand_empty_rest_of_line ();
19645 return;
19646 }
252b5132 19647
ecb4347a
DJ
19648 off = get_absolute_expression ();
19649
19650 if (reg_type == 'F')
19651 {
19652 cur_proc_ptr->fpreg_mask = mask;
19653 cur_proc_ptr->fpreg_offset = off;
19654 }
19655 else
19656 {
19657 cur_proc_ptr->reg_mask = mask;
19658 cur_proc_ptr->reg_offset = off;
19659 }
19660
19661 demand_empty_rest_of_line ();
252b5132 19662 }
252b5132
RH
19663}
19664
316f5878
RS
19665/* A table describing all the processors gas knows about. Names are
19666 matched in the order listed.
e7af610e 19667
316f5878
RS
19668 To ease comparison, please keep this table in the same order as
19669 gcc's mips_cpu_info_table[]. */
e972090a
NC
19670static const struct mips_cpu_info mips_cpu_info_table[] =
19671{
316f5878 19672 /* Entries for generic ISAs */
d16afab6
RS
19673 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19674 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19675 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19676 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19677 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19678 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19679 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19680 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19681 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19682 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19683 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19684 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19685 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19686 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19687 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19688
19689 /* MIPS I */
d16afab6
RS
19690 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19691 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19692 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19693
19694 /* MIPS II */
d16afab6 19695 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19696
19697 /* MIPS III */
d16afab6
RS
19698 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19699 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19700 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19701 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19702 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19703 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19704 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19705 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19706 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19707 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19708 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19709 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19710 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19711 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6 19712 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
8095d2f7 19713 { "loongson2f", 0, ASE_LOONGSON_MMI, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19714
19715 /* MIPS IV */
d16afab6
RS
19716 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19717 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19718 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19719 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19720 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19721 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19722 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19723 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19724 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19725 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19726 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19727 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19728 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19729 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19730 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19731
19732 /* MIPS 32 */
d16afab6
RS
19733 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19734 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19735 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19736 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19737
19738 /* MIPS 32 Release 2 */
d16afab6
RS
19739 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19740 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19741 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19742 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19743 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19744 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19745 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19746 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19747 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19748 ISA_MIPS32R2, CPU_MIPS32R2 },
19749 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19750 ISA_MIPS32R2, CPU_MIPS32R2 },
19751 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19752 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19753 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19754 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19755 /* Deprecated forms of the above. */
d16afab6
RS
19756 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19757 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19758 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19759 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19760 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19761 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19762 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19763 /* Deprecated forms of the above. */
d16afab6
RS
19764 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19765 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19766 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19767 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19768 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19769 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19770 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19771 /* Deprecated forms of the above. */
d16afab6
RS
19772 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19773 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19774 /* 34Kn is a 34kc without DSP. */
d16afab6 19775 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19776 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19777 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19778 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19779 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19780 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19781 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19782 /* Deprecated forms of the above. */
d16afab6
RS
19783 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19784 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19785 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19786 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19787 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19788 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19789 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19790 /* interaptiv is the new name for 1004kf */
19791 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19792 { "interaptiv-mr2", 0,
19793 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19794 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
c6e5c03a
RS
19795 /* M5100 family */
19796 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19797 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19798 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19799 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19800
316f5878 19801 /* MIPS 64 */
d16afab6
RS
19802 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19803 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19804 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19805 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19806
c7a23324 19807 /* Broadcom SB-1 CPU core */
d16afab6 19808 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19809 /* Broadcom SB-1A CPU core */
d16afab6 19810 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19811
ed163775 19812 /* MIPS 64 Release 2 */
716c08de 19813 /* Loongson CPU core */
ac8cb70f 19814 /* -march=loongson3a is an alias of -march=gs464 for compatibility */
bdc6c06e 19815 { "loongson3a", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
ac8cb70f
CX
19816 ISA_MIPS64R2, CPU_GS464 },
19817 { "gs464", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT,
19818 ISA_MIPS64R2, CPU_GS464 },
bd782c07
CX
19819 { "gs464e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
19820 | ASE_LOONGSON_EXT2, ISA_MIPS64R2, CPU_GS464E },
9108bc33
CX
19821 { "gs264e", 0, ASE_LOONGSON_MMI | ASE_LOONGSON_CAM | ASE_LOONGSON_EXT
19822 | ASE_LOONGSON_EXT2 | ASE_MSA | ASE_MSA64, ISA_MIPS64R2, CPU_GS264E },
ed163775 19823
967344c6 19824 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19825 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19826 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19827 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19828 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19829
52b6b6b9 19830 /* RMI Xlr */
d16afab6 19831 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19832
55a36193
MK
19833 /* Broadcom XLP.
19834 XLP is mostly like XLR, with the prominent exception that it is
19835 MIPS64R2 rather than MIPS64. */
d16afab6 19836 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19837
a4968f42 19838 /* MIPS 64 Release 6 */
7ef0d297 19839 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19840 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19841
316f5878 19842 /* End marker */
d16afab6 19843 { NULL, 0, 0, 0, 0 }
316f5878 19844};
e7af610e 19845
84ea6cf2 19846
316f5878
RS
19847/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19848 with a final "000" replaced by "k". Ignore case.
e7af610e 19849
316f5878 19850 Note: this function is shared between GCC and GAS. */
c6c98b38 19851
b34976b6 19852static bfd_boolean
17a2f251 19853mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19854{
19855 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19856 given++, canonical++;
19857
19858 return ((*given == 0 && *canonical == 0)
19859 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19860}
19861
19862
19863/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19864 CPU name. We've traditionally allowed a lot of variation here.
19865
19866 Note: this function is shared between GCC and GAS. */
19867
b34976b6 19868static bfd_boolean
17a2f251 19869mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19870{
19871 /* First see if the name matches exactly, or with a final "000"
19872 turned into "k". */
19873 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19874 return TRUE;
316f5878
RS
19875
19876 /* If not, try comparing based on numerical designation alone.
19877 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19878 if (TOLOWER (*given) == 'r')
19879 given++;
19880 if (!ISDIGIT (*given))
b34976b6 19881 return FALSE;
316f5878
RS
19882
19883 /* Skip over some well-known prefixes in the canonical name,
19884 hoping to find a number there too. */
19885 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19886 canonical += 2;
19887 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19888 canonical += 2;
19889 else if (TOLOWER (canonical[0]) == 'r')
19890 canonical += 1;
19891
19892 return mips_strict_matching_cpu_name_p (canonical, given);
19893}
19894
19895
19896/* Parse an option that takes the name of a processor as its argument.
19897 OPTION is the name of the option and CPU_STRING is the argument.
19898 Return the corresponding processor enumeration if the CPU_STRING is
19899 recognized, otherwise report an error and return null.
19900
19901 A similar function exists in GCC. */
e7af610e
NC
19902
19903static const struct mips_cpu_info *
17a2f251 19904mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19905{
316f5878 19906 const struct mips_cpu_info *p;
e7af610e 19907
316f5878
RS
19908 /* 'from-abi' selects the most compatible architecture for the given
19909 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19910 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19911 version. Look first at the -mgp options, if given, otherwise base
19912 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19913
316f5878
RS
19914 Treat NO_ABI like the EABIs. One reason to do this is that the
19915 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19916 architecture. This code picks MIPS I for 'mips' and MIPS III for
19917 'mips64', just as we did in the days before 'from-abi'. */
19918 if (strcasecmp (cpu_string, "from-abi") == 0)
19919 {
19920 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19921 return mips_cpu_info_from_isa (ISA_MIPS1);
19922
19923 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19924 return mips_cpu_info_from_isa (ISA_MIPS3);
19925
bad1aba3 19926 if (file_mips_opts.gp >= 0)
19927 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19928 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19929
19930 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19931 ? ISA_MIPS3
19932 : ISA_MIPS1);
19933 }
19934
19935 /* 'default' has traditionally been a no-op. Probably not very useful. */
19936 if (strcasecmp (cpu_string, "default") == 0)
19937 return 0;
19938
19939 for (p = mips_cpu_info_table; p->name != 0; p++)
19940 if (mips_matching_cpu_name_p (p->name, cpu_string))
19941 return p;
19942
1661c76c 19943 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19944 return 0;
e7af610e
NC
19945}
19946
316f5878
RS
19947/* Return the canonical processor information for ISA (a member of the
19948 ISA_MIPS* enumeration). */
19949
e7af610e 19950static const struct mips_cpu_info *
17a2f251 19951mips_cpu_info_from_isa (int isa)
e7af610e
NC
19952{
19953 int i;
19954
19955 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19956 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19957 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19958 return (&mips_cpu_info_table[i]);
19959
e972090a 19960 return NULL;
e7af610e 19961}
fef14a42
TS
19962
19963static const struct mips_cpu_info *
17a2f251 19964mips_cpu_info_from_arch (int arch)
fef14a42
TS
19965{
19966 int i;
19967
19968 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19969 if (arch == mips_cpu_info_table[i].cpu)
19970 return (&mips_cpu_info_table[i]);
19971
19972 return NULL;
19973}
316f5878
RS
19974\f
19975static void
17a2f251 19976show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19977{
19978 if (*first_p)
19979 {
19980 fprintf (stream, "%24s", "");
19981 *col_p = 24;
19982 }
19983 else
19984 {
19985 fprintf (stream, ", ");
19986 *col_p += 2;
19987 }
e7af610e 19988
316f5878
RS
19989 if (*col_p + strlen (string) > 72)
19990 {
19991 fprintf (stream, "\n%24s", "");
19992 *col_p = 24;
19993 }
19994
19995 fprintf (stream, "%s", string);
19996 *col_p += strlen (string);
19997
19998 *first_p = 0;
19999}
20000
20001void
17a2f251 20002md_show_usage (FILE *stream)
e7af610e 20003{
316f5878
RS
20004 int column, first;
20005 size_t i;
20006
20007 fprintf (stream, _("\
20008MIPS options:\n\
316f5878
RS
20009-EB generate big endian output\n\
20010-EL generate little endian output\n\
20011-g, -g2 do not remove unneeded NOPs or swap branches\n\
20012-G NUM allow referencing objects up to NUM bytes\n\
20013 implicitly with the gp register [default 8]\n"));
20014 fprintf (stream, _("\
20015-mips1 generate MIPS ISA I instructions\n\
20016-mips2 generate MIPS ISA II instructions\n\
20017-mips3 generate MIPS ISA III instructions\n\
20018-mips4 generate MIPS ISA IV instructions\n\
20019-mips5 generate MIPS ISA V instructions\n\
20020-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 20021-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
20022-mips32r3 generate MIPS32 release 3 ISA instructions\n\
20023-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 20024-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 20025-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 20026-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
20027-mips64r3 generate MIPS64 release 3 ISA instructions\n\
20028-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 20029-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
20030-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
20031
20032 first = 1;
e7af610e
NC
20033
20034 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
20035 show (stream, mips_cpu_info_table[i].name, &column, &first);
20036 show (stream, "from-abi", &column, &first);
20037 fputc ('\n', stream);
e7af610e 20038
316f5878
RS
20039 fprintf (stream, _("\
20040-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
20041-no-mCPU don't generate code specific to CPU.\n\
20042 For -mCPU and -no-mCPU, CPU must be one of:\n"));
20043
20044 first = 1;
20045
20046 show (stream, "3900", &column, &first);
20047 show (stream, "4010", &column, &first);
20048 show (stream, "4100", &column, &first);
20049 show (stream, "4650", &column, &first);
20050 fputc ('\n', stream);
20051
20052 fprintf (stream, _("\
20053-mips16 generate mips16 instructions\n\
20054-no-mips16 do not generate mips16 instructions\n"));
20055 fprintf (stream, _("\
f866b262
MR
20056-mmips16e2 generate MIPS16e2 instructions\n\
20057-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20058 fprintf (stream, _("\
df58fc94
RS
20059-mmicromips generate microMIPS instructions\n\
20060-mno-micromips do not generate microMIPS instructions\n"));
20061 fprintf (stream, _("\
e16bfa71 20062-msmartmips generate smartmips instructions\n\
3739860c 20063-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20064 fprintf (stream, _("\
74cd071d
CF
20065-mdsp generate DSP instructions\n\
20066-mno-dsp do not generate DSP instructions\n"));
20067 fprintf (stream, _("\
8b082fb1
TS
20068-mdspr2 generate DSP R2 instructions\n\
20069-mno-dspr2 do not generate DSP R2 instructions\n"));
20070 fprintf (stream, _("\
8f4f9071
MF
20071-mdspr3 generate DSP R3 instructions\n\
20072-mno-dspr3 do not generate DSP R3 instructions\n"));
20073 fprintf (stream, _("\
ef2e4d86
CF
20074-mmt generate MT instructions\n\
20075-mno-mt do not generate MT instructions\n"));
20076 fprintf (stream, _("\
dec0624d
MR
20077-mmcu generate MCU instructions\n\
20078-mno-mcu do not generate MCU instructions\n"));
20079 fprintf (stream, _("\
56d438b1
CF
20080-mmsa generate MSA instructions\n\
20081-mno-msa do not generate MSA instructions\n"));
20082 fprintf (stream, _("\
7d64c587
AB
20083-mxpa generate eXtended Physical Address (XPA) instructions\n\
20084-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20085 fprintf (stream, _("\
b015e599
AP
20086-mvirt generate Virtualization instructions\n\
20087-mno-virt do not generate Virtualization instructions\n"));
20088 fprintf (stream, _("\
730c3174
SE
20089-mcrc generate CRC instructions\n\
20090-mno-crc do not generate CRC instructions\n"));
20091 fprintf (stream, _("\
6f20c942
FS
20092-mginv generate Global INValidate (GINV) instructions\n\
20093-mno-ginv do not generate Global INValidate instructions\n"));
20094 fprintf (stream, _("\
8095d2f7
CX
20095-mloongson-mmi generate Loongson MultiMedia extensions Instructions (MMI) instructions\n\
20096-mno-loongson-mmi do not generate Loongson MultiMedia extensions Instructions\n"));
20097 fprintf (stream, _("\
716c08de
CX
20098-mloongson-cam generate Loongson Content Address Memory (CAM) instructions\n\
20099-mno-loongson-cam do not generate Loongson Content Address Memory Instructions\n"));
20100 fprintf (stream, _("\
bdc6c06e
CX
20101-mloongson-ext generate Loongson EXTensions (EXT) instructions\n\
20102-mno-loongson-ext do not generate Loongson EXTensions Instructions\n"));
20103 fprintf (stream, _("\
a693765e
CX
20104-mloongson-ext2 generate Loongson EXTensions R2 (EXT2) instructions\n\
20105-mno-loongson-ext2 do not generate Loongson EXTensions R2 Instructions\n"));
20106 fprintf (stream, _("\
833794fc
MR
20107-minsn32 only generate 32-bit microMIPS instructions\n\
20108-mno-insn32 generate all microMIPS instructions\n"));
20109 fprintf (stream, _("\
c67a084a
NC
20110-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20111-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 20112-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20113-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20114-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20115-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
20116-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20117-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20118-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20119-O0 do not remove unneeded NOPs, do not swap branches\n\
20120-O, -O1 remove unneeded NOPs, do not swap branches\n\
20121-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20122--trap, --no-break trap exception on div by 0 and mult overflow\n\
20123--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20124 fprintf (stream, _("\
20125-mhard-float allow floating-point instructions\n\
20126-msoft-float do not allow floating-point instructions\n\
20127-msingle-float only allow 32-bit floating-point operations\n\
20128-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20129--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20130--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20131-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20132-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20133-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20134
20135 first = 1;
20136
20137 show (stream, "legacy", &column, &first);
20138 show (stream, "2008", &column, &first);
20139
20140 fputc ('\n', stream);
20141
316f5878
RS
20142 fprintf (stream, _("\
20143-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20144-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20145-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20146-non_shared do not generate code that can operate with DSOs\n\
316f5878 20147-xgot assume a 32 bit GOT\n\
dcd410fe 20148-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20149-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20150 position dependent (non shared) code\n\
316f5878
RS
20151-mabi=ABI create ABI conformant object file for:\n"));
20152
20153 first = 1;
20154
20155 show (stream, "32", &column, &first);
20156 show (stream, "o64", &column, &first);
20157 show (stream, "n32", &column, &first);
20158 show (stream, "64", &column, &first);
20159 show (stream, "eabi", &column, &first);
20160
20161 fputc ('\n', stream);
20162
20163 fprintf (stream, _("\
b4f6242e
MR
20164-32 create o32 ABI object file%s\n"),
20165 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20166 fprintf (stream, _("\
20167-n32 create n32 ABI object file%s\n"),
20168 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20169 fprintf (stream, _("\
20170-64 create 64 ABI object file%s\n"),
20171 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20172}
14e777e0 20173
1575952e 20174#ifdef TE_IRIX
14e777e0 20175enum dwarf2_format
413a266c 20176mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20177{
369943fe 20178 if (HAVE_64BIT_SYMBOLS)
1575952e 20179 return dwarf2_format_64bit_irix;
14e777e0
KB
20180 else
20181 return dwarf2_format_32bit;
20182}
1575952e 20183#endif
73369e65
EC
20184
20185int
20186mips_dwarf2_addr_size (void)
20187{
6b6b3450 20188 if (HAVE_64BIT_OBJECTS)
73369e65 20189 return 8;
73369e65
EC
20190 else
20191 return 4;
20192}
5862107c
EC
20193
20194/* Standard calling conventions leave the CFA at SP on entry. */
20195void
20196mips_cfi_frame_initial_instructions (void)
20197{
20198 cfi_add_CFA_def_cfa_register (SP);
20199}
20200
707bfff6
TS
20201int
20202tc_mips_regname_to_dw2regnum (char *regname)
20203{
20204 unsigned int regnum = -1;
20205 unsigned int reg;
20206
20207 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20208 regnum = reg;
20209
20210 return regnum;
20211}
263b2574 20212
20213/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20214 Given a symbolic attribute NAME, return the proper integer value.
20215 Returns -1 if the attribute is not known. */
20216
20217int
20218mips_convert_symbolic_attribute (const char *name)
20219{
20220 static const struct
20221 {
20222 const char * name;
20223 const int tag;
20224 }
20225 attribute_table[] =
20226 {
20227#define T(tag) {#tag, tag}
20228 T (Tag_GNU_MIPS_ABI_FP),
20229 T (Tag_GNU_MIPS_ABI_MSA),
20230#undef T
20231 };
20232 unsigned int i;
20233
20234 if (name == NULL)
20235 return -1;
20236
20237 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20238 if (streq (name, attribute_table[i].name))
20239 return attribute_table[i].tag;
20240
20241 return -1;
20242}
fd5c94ab
RS
20243
20244void
20245md_mips_end (void)
20246{
351cdf24
MF
20247 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20248
fd5c94ab
RS
20249 mips_emit_delays ();
20250 if (cur_proc_ptr)
20251 as_warn (_("missing .end at end of assembly"));
919731af 20252
20253 /* Just in case no code was emitted, do the consistency check. */
20254 file_mips_check_options ();
351cdf24
MF
20255
20256 /* Set a floating-point ABI if the user did not. */
20257 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20258 {
20259 /* Perform consistency checks on the floating-point ABI. */
20260 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20261 Tag_GNU_MIPS_ABI_FP);
20262 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20263 check_fpabi (fpabi);
20264 }
20265 else
20266 {
20267 /* Soft-float gets precedence over single-float, the two options should
20268 not be used together so this should not matter. */
20269 if (file_mips_opts.soft_float == 1)
20270 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20271 /* Single-float gets precedence over all double_float cases. */
20272 else if (file_mips_opts.single_float == 1)
20273 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20274 else
20275 {
20276 switch (file_mips_opts.fp)
20277 {
20278 case 32:
20279 if (file_mips_opts.gp == 32)
20280 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20281 break;
20282 case 0:
20283 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20284 break;
20285 case 64:
20286 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20287 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20288 else if (file_mips_opts.gp == 32)
20289 fpabi = Val_GNU_MIPS_ABI_FP_64;
20290 else
20291 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20292 break;
20293 }
20294 }
20295
20296 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20297 Tag_GNU_MIPS_ABI_FP, fpabi);
20298 }
fd5c94ab 20299}
2f0c68f2
CM
20300
20301/* Returns the relocation type required for a particular CFI encoding. */
20302
20303bfd_reloc_code_real_type
20304mips_cfi_reloc_for_encoding (int encoding)
20305{
20306 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20307 return BFD_RELOC_32_PCREL;
20308 else return BFD_RELOC_NONE;
20309}
This page took 3.271225 seconds and 4 git commands to generate.