MIPS16/GAS: Improve disallowed relocation operand error diagnostics
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
2571583a 2 Copyright (C) 1993-2017 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24
MF
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
af7ee8bf 426
ad3fea08
TS
427/* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429#define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439/* Return true if ISA supports legacy NAN. */
440#define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
ae52f483
AB
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
ad3fea08 454
bad1aba3 455#define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
ca4e0257 459
bad1aba3 460#define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
ca4e0257 464
316f5878 465#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 466
316f5878 467#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 468
3b91255e
RS
469/* True if relocations are stored in-place. */
470#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
aed1a261
RS
472/* The ABI-derived address size. */
473#define HAVE_64BIT_ADDRESSES \
bad1aba3 474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 475#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 476
aed1a261
RS
477/* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479#define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 482
b7c7d6c1
TS
483/* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
f899b4b8 486#define ADDRESS_ADD_INSN \
b7c7d6c1 487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
488
489#define ADDRESS_ADDI_INSN \
b7c7d6c1 490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
491
492#define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495#define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
a4672219 498/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
499#define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 502
2309ddf2 503/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
504#define CPU_HAS_MICROMIPS(cpu) 0
505
60b63b72
RS
506/* True if CPU has a dror instruction. */
507#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509/* True if CPU has a ror instruction. */
510#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
dd6a37e7 512/* True if CPU is in the Octeon family */
2c629856
N
513#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 515
dd3cbb7e 516/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 517#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 518
0aa27725
RS
519/* True, if CPU has support for ldc1 and sdc1. */
520#define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
c8978940
CD
523/* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535#define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 540 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 545 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 546 || mips_opts.arch == CPU_R4010 \
e407c74b 547 || mips_opts.arch == CPU_R5900 \
c8978940
CD
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
c8978940 552 || mips_opts.arch == CPU_RM7000 \
c8978940 553 || mips_opts.arch == CPU_VR5500 \
df58fc94 554 || mips_opts.micromips \
c8978940 555 )
252b5132
RH
556
557/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
67dc82bc 560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
561 level I and microMIPS mode instructions are always interlocked. */
562#define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
e407c74b 565 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
566 || mips_opts.micromips \
567 )
252b5132 568
81912461
ILT
569/* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
43885403
MF
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
bdaaa2e1 577/* Itbl support may require additional care here. */
81912461
ILT
578#define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
df58fc94 583 || mips_opts.micromips \
81912461
ILT
584 )
585
586/* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592#define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
252b5132 596
6b76fefe
CM
597/* Is this a mfhi or mflo instruction? */
598#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
df58fc94
RS
601/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604#define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
42429eac 607/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 608#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
609#define GPR_SMIN (-GPR_SMAX - 1)
610
252b5132
RH
611/* MIPS PIC level. */
612
a161fe53 613enum mips_pic_level mips_pic;
252b5132 614
c9914766 615/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 616 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 617static int mips_big_got = 0;
252b5132
RH
618
619/* 1 if trap instructions should used for overflow rather than break
620 instructions. */
c9914766 621static int mips_trap = 0;
252b5132 622
119d663a 623/* 1 if double width floating point constants should not be constructed
b6ff326e 624 by assembling two single width halves into two single width floating
119d663a
NC
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
d547a75e 627 in the status register, and the setting of this bit cannot be determined
119d663a
NC
628 automatically at assemble time. */
629static int mips_disable_float_construction;
630
252b5132
RH
631/* Non-zero if any .set noreorder directives were used. */
632
633static int mips_any_noreorder;
634
6b76fefe
CM
635/* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637static int mips_7000_hilo_fix;
638
02ffd3e4 639/* The size of objects in the small data section. */
156c2f8b 640static unsigned int g_switch_value = 8;
252b5132
RH
641/* Whether the -G option was used. */
642static int g_switch_seen = 0;
643
644#define N_RMASK 0xc4
645#define N_VFP 0xd4
646
647/* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
252b5132
RH
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 659static int nopic_need_relax (symbolS *, int);
252b5132
RH
660
661/* handle of the OPCODE hash table */
662static struct hash_control *op_hash = NULL;
663
664/* The opcode hash table we use for the mips16. */
665static struct hash_control *mips16_op_hash = NULL;
666
df58fc94
RS
667/* The opcode hash table we use for the microMIPS ASE. */
668static struct hash_control *micromips_op_hash = NULL;
669
252b5132
RH
670/* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672const char comment_chars[] = "#";
673
674/* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677/* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
bdaaa2e1 679 #NO_APP at the beginning of its output. */
252b5132
RH
680/* Also note that C style comments are always supported. */
681const char line_comment_chars[] = "#";
682
bdaaa2e1 683/* This array holds machine specific line separator characters. */
63a0b638 684const char line_separator_chars[] = ";";
252b5132
RH
685
686/* Chars that can be used to separate mant from exp in floating point nums */
687const char EXP_CHARS[] = "eE";
688
689/* Chars that mean this number is a floating point constant */
690/* As in 0f12.456 */
691/* or 0d1.2345e12 */
692const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
e3de51ce
RS
699/* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705};
706
707/* Information about an error that was found while assembling the current
708 instruction. */
709struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732};
733
734/* The error that should be reported for the current instruction. */
735static struct mips_insn_error insn_error;
252b5132
RH
736
737static int auto_align = 1;
738
739/* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743static offsetT mips_cprestore_offset = -1;
744
67c1ffbe 745/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 746 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 747 offset and even an other register than $gp as global pointer. */
6478892d
TS
748static offsetT mips_cpreturn_offset = -1;
749static int mips_cpreturn_register = -1;
750static int mips_gp_register = GP;
def2e0dd 751static int mips_gprel_offset = 0;
6478892d 752
7a621144
DJ
753/* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755static int mips_cprestore_valid = 0;
756
252b5132
RH
757/* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759static int mips_frame_reg = SP;
760
7a621144
DJ
761/* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763static int mips_frame_reg_valid = 0;
764
252b5132
RH
765/* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768/* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772static int mips_optimize = 2;
773
774/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776static int mips_debug = 0;
777
7d8e00cf
RS
778/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779#define MAX_VR4130_NOPS 4
780
781/* The maximum number of NOPs needed to fill delay slots. */
782#define MAX_DELAY_NOPS 2
783
784/* The maximum number of NOPs needed for any purpose. */
785#define MAX_NOPS 4
71400594
RS
786
787/* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 793
fc76e730 794/* Arrays of operands for each instruction. */
14daeee3 795#define MAX_OPERANDS 6
fc76e730
RS
796struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798};
799static struct mips_operand_array *mips_operands;
800static struct mips_operand_array *mips16_operands;
801static struct mips_operand_array *micromips_operands;
802
1e915849 803/* Nop instructions used by emit_nop. */
df58fc94
RS
804static struct mips_cl_insn nop_insn;
805static struct mips_cl_insn mips16_nop_insn;
806static struct mips_cl_insn micromips_nop16_insn;
807static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
808
809/* The appropriate nop for the current mode. */
833794fc
MR
810#define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
df58fc94
RS
817
818/* The size of NOP_INSN in bytes. */
833794fc
MR
819#define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
252b5132 822
252b5132
RH
823/* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827static fragS *prev_nop_frag;
828
829/* The number of nop instructions we created in prev_nop_frag. */
830static int prev_nop_frag_holds;
831
832/* The number of nop instructions that we know we need in
bdaaa2e1 833 prev_nop_frag. */
252b5132
RH
834static int prev_nop_frag_required;
835
836/* The number of instructions we've seen since prev_nop_frag. */
837static int prev_nop_frag_since;
838
e8044f35
RS
839/* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
252b5132
RH
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
bdaaa2e1 848 corresponding LO relocation. */
252b5132 849
e972090a
NC
850struct mips_hi_fixup
851{
252b5132
RH
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858};
859
860/* The list of unmatched HI relocs. */
861
862static struct mips_hi_fixup *mips_hi_fixup_list;
863
64bdfcaf
RS
864/* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867static fragS *prev_reloc_op_frag;
868
252b5132
RH
869/* Map mips16 register numbers to normal MIPS register numbers. */
870
e972090a
NC
871static const unsigned int mips16_to_32_reg_map[] =
872{
252b5132
RH
873 16, 17, 2, 3, 4, 5, 6, 7
874};
60b63b72 875
df58fc94
RS
876/* Map microMIPS register numbers to normal MIPS register numbers. */
877
df58fc94 878#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
879
880/* The microMIPS registers with type h. */
e76ff5ab 881static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
882{
883 5, 5, 6, 4, 4, 4, 4, 4
884};
e76ff5ab 885static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
886{
887 6, 7, 7, 21, 22, 5, 6, 7
888};
889
df58fc94
RS
890/* The microMIPS registers with type m. */
891static const unsigned int micromips_to_32_reg_m_map[] =
892{
893 0, 17, 2, 3, 16, 18, 19, 20
894};
895
896#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
71400594
RS
898/* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
c67a084a
NC
900enum fix_vr4120_class
901{
71400594
RS
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909};
910
c67a084a
NC
911/* ...likewise -mfix-loongson2f-jump. */
912static bfd_boolean mips_fix_loongson2f_jump;
913
914/* ...likewise -mfix-loongson2f-nop. */
915static bfd_boolean mips_fix_loongson2f_nop;
916
917/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918static bfd_boolean mips_fix_loongson2f;
919
71400594
RS
920/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925/* True if -mfix-vr4120 is in force. */
d766e8ec 926static int mips_fix_vr4120;
4a6a3df4 927
7d8e00cf
RS
928/* ...likewise -mfix-vr4130. */
929static int mips_fix_vr4130;
930
6a32d874
CM
931/* ...likewise -mfix-24k. */
932static int mips_fix_24k;
933
a8d14a88
CM
934/* ...likewise -mfix-rm7000 */
935static int mips_fix_rm7000;
936
d954098f
DD
937/* ...likewise -mfix-cn63xxp1 */
938static bfd_boolean mips_fix_cn63xxp1;
939
4a6a3df4
AO
940/* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945static int mips_relax_branch;
8b10b0b3
MR
946
947/* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950static bfd_boolean mips_ignore_branch_isa;
252b5132 951\f
4d7206a2
RS
952/* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
584892a6
RS
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
4d7206a2 969
ce8ad872
MR
970 RELAX_PIC
971 Set if generating PIC code.
972
584892a6
RS
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
4d7206a2 989
df58fc94
RS
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
4d7206a2
RS
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
ce8ad872
MR
1014#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1016
584892a6
RS
1017#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1019#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020#define RELAX_USE_SECOND 0x20000
1021#define RELAX_SECOND_LONGER 0x40000
1022#define RELAX_NOMACRO 0x80000
1023#define RELAX_DELAY_SLOT 0x100000
1024#define RELAX_DELAY_SLOT_16BIT 0x200000
1025#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1027
4a6a3df4
AO
1028/* Branch without likely bit. If label is out of range, we turn:
1029
134c0c8b 1030 beq reg1, reg2, label
4a6a3df4
AO
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
b34976b6 1074
4a6a3df4
AO
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1095#define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
66b3e8da
MR
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
ce8ad872
MR
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1105#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1106#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1111#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1112
252b5132
RH
1113/* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
8507b6e7
MR
1133#define RELAX_MIPS16_ENCODE(type, pic, sym32, nomacro, \
1134 small, ext, \
1135 dslot, jal_dslot) \
252b5132
RH
1136 (0x80000000 \
1137 | ((type) & 0xff) \
8507b6e7
MR
1138 | ((pic) ? 0x100 : 0) \
1139 | ((sym32) ? 0x200 : 0) \
1140 | ((nomacro) ? 0x400 : 0) \
1141 | ((small) ? 0x800 : 0) \
1142 | ((ext) ? 0x1000 : 0) \
1143 | ((dslot) ? 0x2000 : 0) \
1144 | ((jal_dslot) ? 0x4000 : 0))
1145
4a6a3df4 1146#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1147#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
8507b6e7
MR
1148#define RELAX_MIPS16_PIC(i) (((i) & 0x100) != 0)
1149#define RELAX_MIPS16_SYM32(i) (((i) & 0x200) != 0)
1150#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x400) != 0)
1151#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x800) != 0)
1152#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x1000) != 0)
1153#define RELAX_MIPS16_DSLOT(i) (((i) & 0x2000) != 0)
1154#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x4000) != 0)
1155
1156#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x8000) != 0)
1157#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x8000)
1158#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x8000)
1159#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x10000) != 0)
1160#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x10000)
1161#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x10000)
1162#define RELAX_MIPS16_MACRO(i) (((i) & 0x20000) != 0)
1163#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x20000)
1164#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x20000)
885add95 1165
df58fc94
RS
1166/* For microMIPS code, we use relaxation similar to one we use for
1167 MIPS16 code. Some instructions that take immediate values support
1168 two encodings: a small one which takes some small value, and a
1169 larger one which takes a 16 bit value. As some branches also follow
1170 this pattern, relaxing these values is required.
1171
1172 We can assemble both microMIPS and normal MIPS code in a single
1173 object. Therefore, we need to support this type of relaxation at
1174 the same time that we support the relaxation described above. We
1175 use one of the high bits of the subtype field to distinguish these
1176 cases.
1177
1178 The information we store for this type of relaxation is the argument
1179 code found in the opcode file for this relocation, the register
8484fb75
MR
1180 selected as the assembler temporary, whether in the 32-bit
1181 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1182 compact, whether there is no delay-slot instruction available to fill
1183 in, whether it stores the link address implicitly in $ra, whether
1184 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1185 instructions is enabled, and whether the displacement of a branch is
1186 too large to fit as an immediate argument of a 16-bit and a 32-bit
1187 branch, respectively. */
ce8ad872 1188#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1189 uncond, compact, link, nods, \
40209cad
MR
1190 relax32, toofar16, toofar32) \
1191 (0x40000000 \
1192 | ((type) & 0xff) \
1193 | (((at) & 0x1f) << 8) \
8484fb75 1194 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1195 | ((pic) ? 0x4000 : 0) \
1196 | ((uncond) ? 0x8000 : 0) \
1197 | ((compact) ? 0x10000 : 0) \
1198 | ((link) ? 0x20000 : 0) \
1199 | ((nods) ? 0x40000 : 0) \
1200 | ((relax32) ? 0x80000 : 0) \
1201 | ((toofar16) ? 0x100000 : 0) \
1202 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1203#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1204#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1205#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1206#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1207#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1208#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1209#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1210#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1211#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1212#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1213
1214#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1215#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1216#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1217#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1218#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1219#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1220
43c0598f
RS
1221/* Sign-extend 16-bit value X. */
1222#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1223
885add95
CD
1224/* Is the given value a sign-extended 32-bit value? */
1225#define IS_SEXT_32BIT_NUM(x) \
1226 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1227 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1228
1229/* Is the given value a sign-extended 16-bit value? */
1230#define IS_SEXT_16BIT_NUM(x) \
1231 (((x) &~ (offsetT) 0x7fff) == 0 \
1232 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1233
df58fc94
RS
1234/* Is the given value a sign-extended 12-bit value? */
1235#define IS_SEXT_12BIT_NUM(x) \
1236 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1237
7f3c4072
CM
1238/* Is the given value a sign-extended 9-bit value? */
1239#define IS_SEXT_9BIT_NUM(x) \
1240 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1241
2051e8c4
MR
1242/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1243#define IS_ZEXT_32BIT_NUM(x) \
1244 (((x) &~ (offsetT) 0xffffffff) == 0 \
1245 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1246
bf12938e
RS
1247/* Extract bits MASK << SHIFT from STRUCT and shift them right
1248 SHIFT places. */
1249#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1250 (((STRUCT) >> (SHIFT)) & (MASK))
1251
bf12938e 1252/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1253#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1254 (!(MICROMIPS) \
1255 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1256 : EXTRACT_BITS ((INSN).insn_opcode, \
1257 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1258#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1259 EXTRACT_BITS ((INSN).insn_opcode, \
1260 MIPS16OP_MASK_##FIELD, \
1261 MIPS16OP_SH_##FIELD)
5c04167a
RS
1262
1263/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1264#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1265\f
df58fc94
RS
1266/* Whether or not we are emitting a branch-likely macro. */
1267static bfd_boolean emit_branch_likely_macro = FALSE;
1268
4d7206a2
RS
1269/* Global variables used when generating relaxable macros. See the
1270 comment above RELAX_ENCODE for more details about how relaxation
1271 is used. */
1272static struct {
1273 /* 0 if we're not emitting a relaxable macro.
1274 1 if we're emitting the first of the two relaxation alternatives.
1275 2 if we're emitting the second alternative. */
1276 int sequence;
1277
1278 /* The first relaxable fixup in the current frag. (In other words,
1279 the first fixup that refers to relaxable code.) */
1280 fixS *first_fixup;
1281
1282 /* sizes[0] says how many bytes of the first alternative are stored in
1283 the current frag. Likewise sizes[1] for the second alternative. */
1284 unsigned int sizes[2];
1285
1286 /* The symbol on which the choice of sequence depends. */
1287 symbolS *symbol;
1288} mips_relax;
252b5132 1289\f
584892a6
RS
1290/* Global variables used to decide whether a macro needs a warning. */
1291static struct {
1292 /* True if the macro is in a branch delay slot. */
1293 bfd_boolean delay_slot_p;
1294
df58fc94
RS
1295 /* Set to the length in bytes required if the macro is in a delay slot
1296 that requires a specific length of instruction, otherwise zero. */
1297 unsigned int delay_slot_length;
1298
584892a6
RS
1299 /* For relaxable macros, sizes[0] is the length of the first alternative
1300 in bytes and sizes[1] is the length of the second alternative.
1301 For non-relaxable macros, both elements give the length of the
1302 macro in bytes. */
1303 unsigned int sizes[2];
1304
df58fc94
RS
1305 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1306 instruction of the first alternative in bytes and first_insn_sizes[1]
1307 is the length of the first instruction of the second alternative.
1308 For non-relaxable macros, both elements give the length of the first
1309 instruction in bytes.
1310
1311 Set to zero if we haven't yet seen the first instruction. */
1312 unsigned int first_insn_sizes[2];
1313
1314 /* For relaxable macros, insns[0] is the number of instructions for the
1315 first alternative and insns[1] is the number of instructions for the
1316 second alternative.
1317
1318 For non-relaxable macros, both elements give the number of
1319 instructions for the macro. */
1320 unsigned int insns[2];
1321
584892a6
RS
1322 /* The first variant frag for this macro. */
1323 fragS *first_frag;
1324} mips_macro_warning;
1325\f
252b5132
RH
1326/* Prototypes for static functions. */
1327
252b5132
RH
1328enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1329
b34976b6 1330static void append_insn
df58fc94
RS
1331 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1332 bfd_boolean expansionp);
7d10b47d 1333static void mips_no_prev_insn (void);
c67a084a 1334static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1335static void mips16_macro_build
03ea81db 1336 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1337static void load_register (int, expressionS *, int);
584892a6
RS
1338static void macro_start (void);
1339static void macro_end (void);
833794fc 1340static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1341static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1342static void mips_ip (char *str, struct mips_cl_insn * ip);
1343static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1344static void mips16_immed
3b4dbbbf 1345 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1346 unsigned int, unsigned long *);
5e0116d5 1347static size_t my_getSmallExpression
17a2f251
TS
1348 (expressionS *, bfd_reloc_code_real_type *, char *);
1349static void my_getExpression (expressionS *, char *);
1350static void s_align (int);
1351static void s_change_sec (int);
1352static void s_change_section (int);
1353static void s_cons (int);
1354static void s_float_cons (int);
1355static void s_mips_globl (int);
1356static void s_option (int);
1357static void s_mipsset (int);
1358static void s_abicalls (int);
1359static void s_cpload (int);
1360static void s_cpsetup (int);
1361static void s_cplocal (int);
1362static void s_cprestore (int);
1363static void s_cpreturn (int);
741d6ea8
JM
1364static void s_dtprelword (int);
1365static void s_dtpreldword (int);
d0f13682
CLT
1366static void s_tprelword (int);
1367static void s_tpreldword (int);
17a2f251
TS
1368static void s_gpvalue (int);
1369static void s_gpword (int);
1370static void s_gpdword (int);
a3f278e2 1371static void s_ehword (int);
17a2f251
TS
1372static void s_cpadd (int);
1373static void s_insn (int);
ba92f887 1374static void s_nan (int);
919731af 1375static void s_module (int);
17a2f251
TS
1376static void s_mips_ent (int);
1377static void s_mips_end (int);
1378static void s_mips_frame (int);
1379static void s_mips_mask (int reg_type);
1380static void s_mips_stab (int);
1381static void s_mips_weakext (int);
1382static void s_mips_file (int);
1383static void s_mips_loc (int);
9e009953 1384static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1385static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1386static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1387static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1388static void file_mips_check_options (void);
e7af610e
NC
1389
1390/* Table and functions used to map between CPU/ISA names, and
1391 ISA levels, and CPU numbers. */
1392
e972090a
NC
1393struct mips_cpu_info
1394{
e7af610e 1395 const char *name; /* CPU or ISA name. */
d16afab6
RS
1396 int flags; /* MIPS_CPU_* flags. */
1397 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1398 int isa; /* ISA level. */
1399 int cpu; /* CPU number (default CPU if ISA). */
1400};
1401
ad3fea08 1402#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1403
17a2f251
TS
1404static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1405static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1406static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1407\f
c31f3936
RS
1408/* Command-line options. */
1409const char *md_shortopts = "O::g::G:";
1410
1411enum options
1412 {
1413 OPTION_MARCH = OPTION_MD_BASE,
1414 OPTION_MTUNE,
1415 OPTION_MIPS1,
1416 OPTION_MIPS2,
1417 OPTION_MIPS3,
1418 OPTION_MIPS4,
1419 OPTION_MIPS5,
1420 OPTION_MIPS32,
1421 OPTION_MIPS64,
1422 OPTION_MIPS32R2,
ae52f483
AB
1423 OPTION_MIPS32R3,
1424 OPTION_MIPS32R5,
7361da2c 1425 OPTION_MIPS32R6,
c31f3936 1426 OPTION_MIPS64R2,
ae52f483
AB
1427 OPTION_MIPS64R3,
1428 OPTION_MIPS64R5,
7361da2c 1429 OPTION_MIPS64R6,
c31f3936
RS
1430 OPTION_MIPS16,
1431 OPTION_NO_MIPS16,
1432 OPTION_MIPS3D,
1433 OPTION_NO_MIPS3D,
1434 OPTION_MDMX,
1435 OPTION_NO_MDMX,
1436 OPTION_DSP,
1437 OPTION_NO_DSP,
1438 OPTION_MT,
1439 OPTION_NO_MT,
1440 OPTION_VIRT,
1441 OPTION_NO_VIRT,
56d438b1
CF
1442 OPTION_MSA,
1443 OPTION_NO_MSA,
c31f3936
RS
1444 OPTION_SMARTMIPS,
1445 OPTION_NO_SMARTMIPS,
1446 OPTION_DSPR2,
1447 OPTION_NO_DSPR2,
8f4f9071
MF
1448 OPTION_DSPR3,
1449 OPTION_NO_DSPR3,
c31f3936
RS
1450 OPTION_EVA,
1451 OPTION_NO_EVA,
7d64c587
AB
1452 OPTION_XPA,
1453 OPTION_NO_XPA,
c31f3936
RS
1454 OPTION_MICROMIPS,
1455 OPTION_NO_MICROMIPS,
1456 OPTION_MCU,
1457 OPTION_NO_MCU,
1458 OPTION_COMPAT_ARCH_BASE,
1459 OPTION_M4650,
1460 OPTION_NO_M4650,
1461 OPTION_M4010,
1462 OPTION_NO_M4010,
1463 OPTION_M4100,
1464 OPTION_NO_M4100,
1465 OPTION_M3900,
1466 OPTION_NO_M3900,
1467 OPTION_M7000_HILO_FIX,
1468 OPTION_MNO_7000_HILO_FIX,
1469 OPTION_FIX_24K,
1470 OPTION_NO_FIX_24K,
a8d14a88
CM
1471 OPTION_FIX_RM7000,
1472 OPTION_NO_FIX_RM7000,
c31f3936
RS
1473 OPTION_FIX_LOONGSON2F_JUMP,
1474 OPTION_NO_FIX_LOONGSON2F_JUMP,
1475 OPTION_FIX_LOONGSON2F_NOP,
1476 OPTION_NO_FIX_LOONGSON2F_NOP,
1477 OPTION_FIX_VR4120,
1478 OPTION_NO_FIX_VR4120,
1479 OPTION_FIX_VR4130,
1480 OPTION_NO_FIX_VR4130,
1481 OPTION_FIX_CN63XXP1,
1482 OPTION_NO_FIX_CN63XXP1,
1483 OPTION_TRAP,
1484 OPTION_BREAK,
1485 OPTION_EB,
1486 OPTION_EL,
1487 OPTION_FP32,
1488 OPTION_GP32,
1489 OPTION_CONSTRUCT_FLOATS,
1490 OPTION_NO_CONSTRUCT_FLOATS,
1491 OPTION_FP64,
351cdf24 1492 OPTION_FPXX,
c31f3936
RS
1493 OPTION_GP64,
1494 OPTION_RELAX_BRANCH,
1495 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1496 OPTION_IGNORE_BRANCH_ISA,
1497 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1498 OPTION_INSN32,
1499 OPTION_NO_INSN32,
c31f3936
RS
1500 OPTION_MSHARED,
1501 OPTION_MNO_SHARED,
1502 OPTION_MSYM32,
1503 OPTION_MNO_SYM32,
1504 OPTION_SOFT_FLOAT,
1505 OPTION_HARD_FLOAT,
1506 OPTION_SINGLE_FLOAT,
1507 OPTION_DOUBLE_FLOAT,
1508 OPTION_32,
c31f3936
RS
1509 OPTION_CALL_SHARED,
1510 OPTION_CALL_NONPIC,
1511 OPTION_NON_SHARED,
1512 OPTION_XGOT,
1513 OPTION_MABI,
1514 OPTION_N32,
1515 OPTION_64,
1516 OPTION_MDEBUG,
1517 OPTION_NO_MDEBUG,
1518 OPTION_PDR,
1519 OPTION_NO_PDR,
1520 OPTION_MVXWORKS_PIC,
ba92f887 1521 OPTION_NAN,
351cdf24
MF
1522 OPTION_ODD_SPREG,
1523 OPTION_NO_ODD_SPREG,
c31f3936
RS
1524 OPTION_END_OF_ENUM
1525 };
1526
1527struct option md_longopts[] =
1528{
1529 /* Options which specify architecture. */
1530 {"march", required_argument, NULL, OPTION_MARCH},
1531 {"mtune", required_argument, NULL, OPTION_MTUNE},
1532 {"mips0", no_argument, NULL, OPTION_MIPS1},
1533 {"mips1", no_argument, NULL, OPTION_MIPS1},
1534 {"mips2", no_argument, NULL, OPTION_MIPS2},
1535 {"mips3", no_argument, NULL, OPTION_MIPS3},
1536 {"mips4", no_argument, NULL, OPTION_MIPS4},
1537 {"mips5", no_argument, NULL, OPTION_MIPS5},
1538 {"mips32", no_argument, NULL, OPTION_MIPS32},
1539 {"mips64", no_argument, NULL, OPTION_MIPS64},
1540 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1541 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1542 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1543 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1544 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1545 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1546 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1547 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1548
1549 /* Options which specify Application Specific Extensions (ASEs). */
1550 {"mips16", no_argument, NULL, OPTION_MIPS16},
1551 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1552 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1553 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1554 {"mdmx", no_argument, NULL, OPTION_MDMX},
1555 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1556 {"mdsp", no_argument, NULL, OPTION_DSP},
1557 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1558 {"mmt", no_argument, NULL, OPTION_MT},
1559 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1560 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1561 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1562 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1563 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1564 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1565 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1566 {"meva", no_argument, NULL, OPTION_EVA},
1567 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1568 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1569 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1570 {"mmcu", no_argument, NULL, OPTION_MCU},
1571 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1572 {"mvirt", no_argument, NULL, OPTION_VIRT},
1573 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1574 {"mmsa", no_argument, NULL, OPTION_MSA},
1575 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1576 {"mxpa", no_argument, NULL, OPTION_XPA},
1577 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
c31f3936
RS
1578
1579 /* Old-style architecture options. Don't add more of these. */
1580 {"m4650", no_argument, NULL, OPTION_M4650},
1581 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1582 {"m4010", no_argument, NULL, OPTION_M4010},
1583 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1584 {"m4100", no_argument, NULL, OPTION_M4100},
1585 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1586 {"m3900", no_argument, NULL, OPTION_M3900},
1587 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1588
1589 /* Options which enable bug fixes. */
1590 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1591 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1592 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1593 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1594 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1595 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1596 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1597 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1598 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1599 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1600 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1601 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1602 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1603 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1604 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1605 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1606 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1607
1608 /* Miscellaneous options. */
1609 {"trap", no_argument, NULL, OPTION_TRAP},
1610 {"no-break", no_argument, NULL, OPTION_TRAP},
1611 {"break", no_argument, NULL, OPTION_BREAK},
1612 {"no-trap", no_argument, NULL, OPTION_BREAK},
1613 {"EB", no_argument, NULL, OPTION_EB},
1614 {"EL", no_argument, NULL, OPTION_EL},
1615 {"mfp32", no_argument, NULL, OPTION_FP32},
1616 {"mgp32", no_argument, NULL, OPTION_GP32},
1617 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1618 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1619 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1620 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1621 {"mgp64", no_argument, NULL, OPTION_GP64},
1622 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1623 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1624 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1625 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1626 {"minsn32", no_argument, NULL, OPTION_INSN32},
1627 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1628 {"mshared", no_argument, NULL, OPTION_MSHARED},
1629 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1630 {"msym32", no_argument, NULL, OPTION_MSYM32},
1631 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1632 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1633 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1634 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1635 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1636 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1637 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1638
1639 /* Strictly speaking this next option is ELF specific,
1640 but we allow it for other ports as well in order to
1641 make testing easier. */
1642 {"32", no_argument, NULL, OPTION_32},
1643
1644 /* ELF-specific options. */
c31f3936
RS
1645 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1646 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1647 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1648 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1649 {"xgot", no_argument, NULL, OPTION_XGOT},
1650 {"mabi", required_argument, NULL, OPTION_MABI},
1651 {"n32", no_argument, NULL, OPTION_N32},
1652 {"64", no_argument, NULL, OPTION_64},
1653 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1654 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1655 {"mpdr", no_argument, NULL, OPTION_PDR},
1656 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1657 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1658 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1659
1660 {NULL, no_argument, NULL, 0}
1661};
1662size_t md_longopts_size = sizeof (md_longopts);
1663\f
c6278170
RS
1664/* Information about either an Application Specific Extension or an
1665 optional architecture feature that, for simplicity, we treat in the
1666 same way as an ASE. */
1667struct mips_ase
1668{
1669 /* The name of the ASE, used in both the command-line and .set options. */
1670 const char *name;
1671
1672 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1673 and 64-bit architectures, the flags here refer to the subset that
1674 is available on both. */
1675 unsigned int flags;
1676
1677 /* The ASE_* flag used for instructions that are available on 64-bit
1678 architectures but that are not included in FLAGS. */
1679 unsigned int flags64;
1680
1681 /* The command-line options that turn the ASE on and off. */
1682 int option_on;
1683 int option_off;
1684
1685 /* The minimum required architecture revisions for MIPS32, MIPS64,
1686 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1687 int mips32_rev;
1688 int mips64_rev;
1689 int micromips32_rev;
1690 int micromips64_rev;
7361da2c
AB
1691
1692 /* The architecture where the ASE was removed or -1 if the extension has not
1693 been removed. */
1694 int rem_rev;
c6278170
RS
1695};
1696
1697/* A table of all supported ASEs. */
1698static const struct mips_ase mips_ases[] = {
1699 { "dsp", ASE_DSP, ASE_DSP64,
1700 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1701 2, 2, 2, 2,
1702 -1 },
c6278170
RS
1703
1704 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1705 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1706 2, 2, 2, 2,
1707 -1 },
c6278170 1708
8f4f9071
MF
1709 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1710 OPTION_DSPR3, OPTION_NO_DSPR3,
1711 6, 6, -1, -1,
1712 -1 },
1713
c6278170
RS
1714 { "eva", ASE_EVA, 0,
1715 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1716 2, 2, 2, 2,
1717 -1 },
c6278170
RS
1718
1719 { "mcu", ASE_MCU, 0,
1720 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1721 2, 2, 2, 2,
1722 -1 },
c6278170
RS
1723
1724 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1725 { "mdmx", ASE_MDMX, 0,
1726 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1727 -1, 1, -1, -1,
1728 6 },
c6278170
RS
1729
1730 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1731 { "mips3d", ASE_MIPS3D, 0,
1732 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1733 2, 1, -1, -1,
1734 6 },
c6278170
RS
1735
1736 { "mt", ASE_MT, 0,
1737 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1738 2, 2, -1, -1,
1739 -1 },
c6278170
RS
1740
1741 { "smartmips", ASE_SMARTMIPS, 0,
1742 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1743 1, -1, -1, -1,
1744 6 },
c6278170
RS
1745
1746 { "virt", ASE_VIRT, ASE_VIRT64,
1747 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1748 2, 2, 2, 2,
1749 -1 },
56d438b1
CF
1750
1751 { "msa", ASE_MSA, ASE_MSA64,
1752 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1753 2, 2, 2, 2,
1754 -1 },
7d64c587
AB
1755
1756 { "xpa", ASE_XPA, 0,
1757 OPTION_XPA, OPTION_NO_XPA,
7361da2c
AB
1758 2, 2, -1, -1,
1759 -1 },
c6278170
RS
1760};
1761
1762/* The set of ASEs that require -mfp64. */
82bda27b 1763#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1764
1765/* Groups of ASE_* flags that represent different revisions of an ASE. */
1766static const unsigned int mips_ase_groups[] = {
8f4f9071 1767 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1768};
1769\f
252b5132
RH
1770/* Pseudo-op table.
1771
1772 The following pseudo-ops from the Kane and Heinrich MIPS book
1773 should be defined here, but are currently unsupported: .alias,
1774 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1775
1776 The following pseudo-ops from the Kane and Heinrich MIPS book are
1777 specific to the type of debugging information being generated, and
1778 should be defined by the object format: .aent, .begin, .bend,
1779 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1780 .vreg.
1781
1782 The following pseudo-ops from the Kane and Heinrich MIPS book are
1783 not MIPS CPU specific, but are also not specific to the object file
1784 format. This file is probably the best place to define them, but
d84bcf09 1785 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1786
e972090a
NC
1787static const pseudo_typeS mips_pseudo_table[] =
1788{
beae10d5 1789 /* MIPS specific pseudo-ops. */
252b5132
RH
1790 {"option", s_option, 0},
1791 {"set", s_mipsset, 0},
1792 {"rdata", s_change_sec, 'r'},
1793 {"sdata", s_change_sec, 's'},
1794 {"livereg", s_ignore, 0},
1795 {"abicalls", s_abicalls, 0},
1796 {"cpload", s_cpload, 0},
6478892d
TS
1797 {"cpsetup", s_cpsetup, 0},
1798 {"cplocal", s_cplocal, 0},
252b5132 1799 {"cprestore", s_cprestore, 0},
6478892d 1800 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1801 {"dtprelword", s_dtprelword, 0},
1802 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1803 {"tprelword", s_tprelword, 0},
1804 {"tpreldword", s_tpreldword, 0},
6478892d 1805 {"gpvalue", s_gpvalue, 0},
252b5132 1806 {"gpword", s_gpword, 0},
10181a0d 1807 {"gpdword", s_gpdword, 0},
a3f278e2 1808 {"ehword", s_ehword, 0},
252b5132
RH
1809 {"cpadd", s_cpadd, 0},
1810 {"insn", s_insn, 0},
ba92f887 1811 {"nan", s_nan, 0},
919731af 1812 {"module", s_module, 0},
252b5132 1813
beae10d5 1814 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1815 chips. */
38a57ae7 1816 {"asciiz", stringer, 8 + 1},
252b5132
RH
1817 {"bss", s_change_sec, 'b'},
1818 {"err", s_err, 0},
1819 {"half", s_cons, 1},
1820 {"dword", s_cons, 3},
1821 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1822 {"origin", s_org, 0},
1823 {"repeat", s_rept, 0},
252b5132 1824
998b3c36
MR
1825 /* For MIPS this is non-standard, but we define it for consistency. */
1826 {"sbss", s_change_sec, 'B'},
1827
beae10d5 1828 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1829 here for one reason or another. */
1830 {"align", s_align, 0},
1831 {"byte", s_cons, 0},
1832 {"data", s_change_sec, 'd'},
1833 {"double", s_float_cons, 'd'},
1834 {"float", s_float_cons, 'f'},
1835 {"globl", s_mips_globl, 0},
1836 {"global", s_mips_globl, 0},
1837 {"hword", s_cons, 1},
1838 {"int", s_cons, 2},
1839 {"long", s_cons, 2},
1840 {"octa", s_cons, 4},
1841 {"quad", s_cons, 3},
cca86cc8 1842 {"section", s_change_section, 0},
252b5132
RH
1843 {"short", s_cons, 1},
1844 {"single", s_float_cons, 'f'},
754e2bb9 1845 {"stabd", s_mips_stab, 'd'},
252b5132 1846 {"stabn", s_mips_stab, 'n'},
754e2bb9 1847 {"stabs", s_mips_stab, 's'},
252b5132
RH
1848 {"text", s_change_sec, 't'},
1849 {"word", s_cons, 2},
add56521 1850
add56521 1851 { "extern", ecoff_directive_extern, 0},
add56521 1852
43841e91 1853 { NULL, NULL, 0 },
252b5132
RH
1854};
1855
e972090a
NC
1856static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1857{
beae10d5
KH
1858 /* These pseudo-ops should be defined by the object file format.
1859 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1860 {"aent", s_mips_ent, 1},
1861 {"bgnb", s_ignore, 0},
1862 {"end", s_mips_end, 0},
1863 {"endb", s_ignore, 0},
1864 {"ent", s_mips_ent, 0},
c5dd6aab 1865 {"file", s_mips_file, 0},
252b5132
RH
1866 {"fmask", s_mips_mask, 'F'},
1867 {"frame", s_mips_frame, 0},
c5dd6aab 1868 {"loc", s_mips_loc, 0},
252b5132
RH
1869 {"mask", s_mips_mask, 'R'},
1870 {"verstamp", s_ignore, 0},
43841e91 1871 { NULL, NULL, 0 },
252b5132
RH
1872};
1873
3ae8dd8d
MR
1874/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1875 purpose of the `.dc.a' internal pseudo-op. */
1876
1877int
1878mips_address_bytes (void)
1879{
919731af 1880 file_mips_check_options ();
3ae8dd8d
MR
1881 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1882}
1883
17a2f251 1884extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1885
1886void
17a2f251 1887mips_pop_insert (void)
252b5132
RH
1888{
1889 pop_insert (mips_pseudo_table);
1890 if (! ECOFF_DEBUGGING)
1891 pop_insert (mips_nonecoff_pseudo_table);
1892}
1893\f
1894/* Symbols labelling the current insn. */
1895
e972090a
NC
1896struct insn_label_list
1897{
252b5132
RH
1898 struct insn_label_list *next;
1899 symbolS *label;
1900};
1901
252b5132 1902static struct insn_label_list *free_insn_labels;
742a56fe 1903#define label_list tc_segment_info_data.labels
252b5132 1904
17a2f251 1905static void mips_clear_insn_labels (void);
df58fc94
RS
1906static void mips_mark_labels (void);
1907static void mips_compressed_mark_labels (void);
252b5132
RH
1908
1909static inline void
17a2f251 1910mips_clear_insn_labels (void)
252b5132 1911{
ed9e98c2 1912 struct insn_label_list **pl;
a8dbcb85 1913 segment_info_type *si;
252b5132 1914
a8dbcb85
TS
1915 if (now_seg)
1916 {
1917 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1918 ;
3739860c 1919
a8dbcb85
TS
1920 si = seg_info (now_seg);
1921 *pl = si->label_list;
1922 si->label_list = NULL;
1923 }
252b5132 1924}
a8dbcb85 1925
df58fc94
RS
1926/* Mark instruction labels in MIPS16/microMIPS mode. */
1927
1928static inline void
1929mips_mark_labels (void)
1930{
1931 if (HAVE_CODE_COMPRESSION)
1932 mips_compressed_mark_labels ();
1933}
252b5132
RH
1934\f
1935static char *expr_end;
1936
e423441d 1937/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1938 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1939
1940static expressionS imm_expr;
252b5132 1941
77bd4346
RS
1942/* The relocatable field in an instruction and the relocs associated
1943 with it. These variables are used for instructions like LUI and
1944 JAL as well as true offsets. They are also used for address
1945 operands in macros. */
252b5132 1946
77bd4346 1947static expressionS offset_expr;
f6688943
TS
1948static bfd_reloc_code_real_type offset_reloc[3]
1949 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1950
df58fc94
RS
1951/* This is set to the resulting size of the instruction to be produced
1952 by mips16_ip if an explicit extension is used or by mips_ip if an
1953 explicit size is supplied. */
252b5132 1954
df58fc94 1955static unsigned int forced_insn_length;
252b5132 1956
e1b47bd5
RS
1957/* True if we are assembling an instruction. All dot symbols defined during
1958 this time should be treated as code labels. */
1959
1960static bfd_boolean mips_assembling_insn;
1961
ecb4347a
DJ
1962/* The pdr segment for per procedure frame/regmask info. Not used for
1963 ECOFF debugging. */
252b5132
RH
1964
1965static segT pdr_seg;
252b5132 1966
e013f690
TS
1967/* The default target format to use. */
1968
aeffff67
RS
1969#if defined (TE_FreeBSD)
1970#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1971#elif defined (TE_TMIPS)
1972#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1973#else
1974#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1975#endif
1976
e013f690 1977const char *
17a2f251 1978mips_target_format (void)
e013f690
TS
1979{
1980 switch (OUTPUT_FLAVOR)
1981 {
e013f690 1982 case bfd_target_elf_flavour:
0a44bf69
RS
1983#ifdef TE_VXWORKS
1984 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1985 return (target_big_endian
1986 ? "elf32-bigmips-vxworks"
1987 : "elf32-littlemips-vxworks");
1988#endif
e013f690 1989 return (target_big_endian
cfe86eaa 1990 ? (HAVE_64BIT_OBJECTS
aeffff67 1991 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1992 : (HAVE_NEWABI
aeffff67
RS
1993 ? ELF_TARGET ("elf32-n", "big")
1994 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1995 : (HAVE_64BIT_OBJECTS
aeffff67 1996 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1997 : (HAVE_NEWABI
aeffff67
RS
1998 ? ELF_TARGET ("elf32-n", "little")
1999 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2000 default:
2001 abort ();
2002 return NULL;
2003 }
2004}
2005
c6278170
RS
2006/* Return the ISA revision that is currently in use, or 0 if we are
2007 generating code for MIPS V or below. */
2008
2009static int
2010mips_isa_rev (void)
2011{
2012 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2013 return 2;
2014
ae52f483
AB
2015 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2016 return 3;
2017
2018 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2019 return 5;
2020
7361da2c
AB
2021 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2022 return 6;
2023
c6278170
RS
2024 /* microMIPS implies revision 2 or above. */
2025 if (mips_opts.micromips)
2026 return 2;
2027
2028 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2029 return 1;
2030
2031 return 0;
2032}
2033
2034/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2035
2036static unsigned int
2037mips_ase_mask (unsigned int flags)
2038{
2039 unsigned int i;
2040
2041 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2042 if (flags & mips_ase_groups[i])
2043 flags |= mips_ase_groups[i];
2044 return flags;
2045}
2046
2047/* Check whether the current ISA supports ASE. Issue a warning if
2048 appropriate. */
2049
2050static void
2051mips_check_isa_supports_ase (const struct mips_ase *ase)
2052{
2053 const char *base;
2054 int min_rev, size;
2055 static unsigned int warned_isa;
2056 static unsigned int warned_fp32;
2057
2058 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2059 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2060 else
2061 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2062 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2063 && (warned_isa & ase->flags) != ase->flags)
2064 {
2065 warned_isa |= ase->flags;
2066 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2067 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2068 if (min_rev < 0)
1661c76c 2069 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2070 " `%s' extension"), size, base, ase->name);
2071 else
1661c76c 2072 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2073 ase->name, base, size, min_rev);
2074 }
7361da2c
AB
2075 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2076 && (warned_isa & ase->flags) != ase->flags)
2077 {
2078 warned_isa |= ase->flags;
2079 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2080 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2081 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2082 ase->name, base, size, ase->rem_rev);
2083 }
2084
c6278170 2085 if ((ase->flags & FP64_ASES)
0b35dfee 2086 && mips_opts.fp != 64
c6278170
RS
2087 && (warned_fp32 & ase->flags) != ase->flags)
2088 {
2089 warned_fp32 |= ase->flags;
1661c76c 2090 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2091 }
2092}
2093
2094/* Check all enabled ASEs to see whether they are supported by the
2095 chosen architecture. */
2096
2097static void
2098mips_check_isa_supports_ases (void)
2099{
2100 unsigned int i, mask;
2101
2102 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2103 {
2104 mask = mips_ase_mask (mips_ases[i].flags);
2105 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2106 mips_check_isa_supports_ase (&mips_ases[i]);
2107 }
2108}
2109
2110/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2111 that were affected. */
2112
2113static unsigned int
919731af 2114mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2115 bfd_boolean enabled_p)
c6278170
RS
2116{
2117 unsigned int mask;
2118
2119 mask = mips_ase_mask (ase->flags);
919731af 2120 opts->ase &= ~mask;
c6278170 2121 if (enabled_p)
919731af 2122 opts->ase |= ase->flags;
c6278170
RS
2123 return mask;
2124}
2125
2126/* Return the ASE called NAME, or null if none. */
2127
2128static const struct mips_ase *
2129mips_lookup_ase (const char *name)
2130{
2131 unsigned int i;
2132
2133 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2134 if (strcmp (name, mips_ases[i].name) == 0)
2135 return &mips_ases[i];
2136 return NULL;
2137}
2138
df58fc94 2139/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2140 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2141 otherwise it is a 32-bit instruction. */
df58fc94
RS
2142
2143static inline unsigned int
2144micromips_insn_length (const struct mips_opcode *mo)
2145{
7fd53920 2146 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2147}
2148
5c04167a
RS
2149/* Return the length of MIPS16 instruction OPCODE. */
2150
2151static inline unsigned int
2152mips16_opcode_length (unsigned long opcode)
2153{
2154 return (opcode >> 16) == 0 ? 2 : 4;
2155}
2156
1e915849
RS
2157/* Return the length of instruction INSN. */
2158
2159static inline unsigned int
2160insn_length (const struct mips_cl_insn *insn)
2161{
df58fc94
RS
2162 if (mips_opts.micromips)
2163 return micromips_insn_length (insn->insn_mo);
2164 else if (mips_opts.mips16)
5c04167a 2165 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2166 else
1e915849 2167 return 4;
1e915849
RS
2168}
2169
2170/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2171
2172static void
2173create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2174{
2175 size_t i;
2176
2177 insn->insn_mo = mo;
1e915849
RS
2178 insn->insn_opcode = mo->match;
2179 insn->frag = NULL;
2180 insn->where = 0;
2181 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2182 insn->fixp[i] = NULL;
2183 insn->fixed_p = (mips_opts.noreorder > 0);
2184 insn->noreorder_p = (mips_opts.noreorder > 0);
2185 insn->mips16_absolute_jump_p = 0;
15be625d 2186 insn->complete_p = 0;
e407c74b 2187 insn->cleared_p = 0;
1e915849
RS
2188}
2189
fc76e730
RS
2190/* Get a list of all the operands in INSN. */
2191
2192static const struct mips_operand_array *
2193insn_operands (const struct mips_cl_insn *insn)
2194{
2195 if (insn->insn_mo >= &mips_opcodes[0]
2196 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2197 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2198
2199 if (insn->insn_mo >= &mips16_opcodes[0]
2200 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2201 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2202
2203 if (insn->insn_mo >= &micromips_opcodes[0]
2204 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2205 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2206
2207 abort ();
2208}
2209
2210/* Get a description of operand OPNO of INSN. */
2211
2212static const struct mips_operand *
2213insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2214{
2215 const struct mips_operand_array *operands;
2216
2217 operands = insn_operands (insn);
2218 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2219 abort ();
2220 return operands->operand[opno];
2221}
2222
e077a1c8
RS
2223/* Install UVAL as the value of OPERAND in INSN. */
2224
2225static inline void
2226insn_insert_operand (struct mips_cl_insn *insn,
2227 const struct mips_operand *operand, unsigned int uval)
2228{
2229 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2230}
2231
fc76e730
RS
2232/* Extract the value of OPERAND from INSN. */
2233
2234static inline unsigned
2235insn_extract_operand (const struct mips_cl_insn *insn,
2236 const struct mips_operand *operand)
2237{
2238 return mips_extract_operand (operand, insn->insn_opcode);
2239}
2240
df58fc94 2241/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2242
2243static void
df58fc94 2244mips_record_compressed_mode (void)
742a56fe
RS
2245{
2246 segment_info_type *si;
2247
2248 si = seg_info (now_seg);
2249 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2250 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2251 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2252 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2253}
2254
4d68580a
RS
2255/* Read a standard MIPS instruction from BUF. */
2256
2257static unsigned long
2258read_insn (char *buf)
2259{
2260 if (target_big_endian)
2261 return bfd_getb32 ((bfd_byte *) buf);
2262 else
2263 return bfd_getl32 ((bfd_byte *) buf);
2264}
2265
2266/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2267 the next byte. */
2268
2269static char *
2270write_insn (char *buf, unsigned int insn)
2271{
2272 md_number_to_chars (buf, insn, 4);
2273 return buf + 4;
2274}
2275
2276/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2277 has length LENGTH. */
2278
2279static unsigned long
2280read_compressed_insn (char *buf, unsigned int length)
2281{
2282 unsigned long insn;
2283 unsigned int i;
2284
2285 insn = 0;
2286 for (i = 0; i < length; i += 2)
2287 {
2288 insn <<= 16;
2289 if (target_big_endian)
2290 insn |= bfd_getb16 ((char *) buf);
2291 else
2292 insn |= bfd_getl16 ((char *) buf);
2293 buf += 2;
2294 }
2295 return insn;
2296}
2297
5c04167a
RS
2298/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2299 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2300
2301static char *
2302write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2303{
2304 unsigned int i;
2305
2306 for (i = 0; i < length; i += 2)
2307 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2308 return buf + length;
2309}
2310
1e915849
RS
2311/* Install INSN at the location specified by its "frag" and "where" fields. */
2312
2313static void
2314install_insn (const struct mips_cl_insn *insn)
2315{
2316 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2317 if (HAVE_CODE_COMPRESSION)
2318 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2319 else
4d68580a 2320 write_insn (f, insn->insn_opcode);
df58fc94 2321 mips_record_compressed_mode ();
1e915849
RS
2322}
2323
2324/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2325 and install the opcode in the new location. */
2326
2327static void
2328move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2329{
2330 size_t i;
2331
2332 insn->frag = frag;
2333 insn->where = where;
2334 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2335 if (insn->fixp[i] != NULL)
2336 {
2337 insn->fixp[i]->fx_frag = frag;
2338 insn->fixp[i]->fx_where = where;
2339 }
2340 install_insn (insn);
2341}
2342
2343/* Add INSN to the end of the output. */
2344
2345static void
2346add_fixed_insn (struct mips_cl_insn *insn)
2347{
2348 char *f = frag_more (insn_length (insn));
2349 move_insn (insn, frag_now, f - frag_now->fr_literal);
2350}
2351
2352/* Start a variant frag and move INSN to the start of the variant part,
2353 marking it as fixed. The other arguments are as for frag_var. */
2354
2355static void
2356add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2357 relax_substateT subtype, symbolS *symbol, offsetT offset)
2358{
2359 frag_grow (max_chars);
2360 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2361 insn->fixed_p = 1;
2362 frag_var (rs_machine_dependent, max_chars, var,
2363 subtype, symbol, offset, NULL);
2364}
2365
2366/* Insert N copies of INSN into the history buffer, starting at
2367 position FIRST. Neither FIRST nor N need to be clipped. */
2368
2369static void
2370insert_into_history (unsigned int first, unsigned int n,
2371 const struct mips_cl_insn *insn)
2372{
2373 if (mips_relax.sequence != 2)
2374 {
2375 unsigned int i;
2376
2377 for (i = ARRAY_SIZE (history); i-- > first;)
2378 if (i >= first + n)
2379 history[i] = history[i - n];
2380 else
2381 history[i] = *insn;
2382 }
2383}
2384
e3de51ce
RS
2385/* Clear the error in insn_error. */
2386
2387static void
2388clear_insn_error (void)
2389{
2390 memset (&insn_error, 0, sizeof (insn_error));
2391}
2392
2393/* Possibly record error message MSG for the current instruction.
2394 If the error is about a particular argument, ARGNUM is the 1-based
2395 number of that argument, otherwise it is 0. FORMAT is the format
2396 of MSG. Return true if MSG was used, false if the current message
2397 was kept. */
2398
2399static bfd_boolean
2400set_insn_error_format (int argnum, enum mips_insn_error_format format,
2401 const char *msg)
2402{
2403 if (argnum == 0)
2404 {
2405 /* Give priority to errors against specific arguments, and to
2406 the first whole-instruction message. */
2407 if (insn_error.msg)
2408 return FALSE;
2409 }
2410 else
2411 {
2412 /* Keep insn_error if it is against a later argument. */
2413 if (argnum < insn_error.min_argnum)
2414 return FALSE;
2415
2416 /* If both errors are against the same argument but are different,
2417 give up on reporting a specific error for this argument.
2418 See the comment about mips_insn_error for details. */
2419 if (argnum == insn_error.min_argnum
2420 && insn_error.msg
2421 && strcmp (insn_error.msg, msg) != 0)
2422 {
2423 insn_error.msg = 0;
2424 insn_error.min_argnum += 1;
2425 return FALSE;
2426 }
2427 }
2428 insn_error.min_argnum = argnum;
2429 insn_error.format = format;
2430 insn_error.msg = msg;
2431 return TRUE;
2432}
2433
2434/* Record an instruction error with no % format fields. ARGNUM and MSG are
2435 as for set_insn_error_format. */
2436
2437static void
2438set_insn_error (int argnum, const char *msg)
2439{
2440 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2441}
2442
2443/* Record an instruction error with one %d field I. ARGNUM and MSG are
2444 as for set_insn_error_format. */
2445
2446static void
2447set_insn_error_i (int argnum, const char *msg, int i)
2448{
2449 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2450 insn_error.u.i = i;
2451}
2452
2453/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2454 are as for set_insn_error_format. */
2455
2456static void
2457set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2458{
2459 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2460 {
2461 insn_error.u.ss[0] = s1;
2462 insn_error.u.ss[1] = s2;
2463 }
2464}
2465
2466/* Report the error in insn_error, which is against assembly code STR. */
2467
2468static void
2469report_insn_error (const char *str)
2470{
e1fa0163 2471 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2472
e3de51ce
RS
2473 switch (insn_error.format)
2474 {
2475 case ERR_FMT_PLAIN:
2476 as_bad (msg, str);
2477 break;
2478
2479 case ERR_FMT_I:
2480 as_bad (msg, insn_error.u.i, str);
2481 break;
2482
2483 case ERR_FMT_SS:
2484 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2485 break;
2486 }
e1fa0163
NC
2487
2488 free ((char *) msg);
e3de51ce
RS
2489}
2490
71400594
RS
2491/* Initialize vr4120_conflicts. There is a bit of duplication here:
2492 the idea is to make it obvious at a glance that each errata is
2493 included. */
2494
2495static void
2496init_vr4120_conflicts (void)
2497{
2498#define CONFLICT(FIRST, SECOND) \
2499 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2500
2501 /* Errata 21 - [D]DIV[U] after [D]MACC */
2502 CONFLICT (MACC, DIV);
2503 CONFLICT (DMACC, DIV);
2504
2505 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2506 CONFLICT (DMULT, DMULT);
2507 CONFLICT (DMULT, DMACC);
2508 CONFLICT (DMACC, DMULT);
2509 CONFLICT (DMACC, DMACC);
2510
2511 /* Errata 24 - MT{LO,HI} after [D]MACC */
2512 CONFLICT (MACC, MTHILO);
2513 CONFLICT (DMACC, MTHILO);
2514
2515 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2516 instruction is executed immediately after a MACC or DMACC
2517 instruction, the result of [either instruction] is incorrect." */
2518 CONFLICT (MACC, MULT);
2519 CONFLICT (MACC, DMULT);
2520 CONFLICT (DMACC, MULT);
2521 CONFLICT (DMACC, DMULT);
2522
2523 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2524 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2525 DDIV or DDIVU instruction, the result of the MACC or
2526 DMACC instruction is incorrect.". */
2527 CONFLICT (DMULT, MACC);
2528 CONFLICT (DMULT, DMACC);
2529 CONFLICT (DIV, MACC);
2530 CONFLICT (DIV, DMACC);
2531
2532#undef CONFLICT
2533}
2534
707bfff6
TS
2535struct regname {
2536 const char *name;
2537 unsigned int num;
2538};
2539
14daeee3 2540#define RNUM_MASK 0x00000ff
56d438b1 2541#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2542#define RTYPE_NUM 0x0000100
2543#define RTYPE_FPU 0x0000200
2544#define RTYPE_FCC 0x0000400
2545#define RTYPE_VEC 0x0000800
2546#define RTYPE_GP 0x0001000
2547#define RTYPE_CP0 0x0002000
2548#define RTYPE_PC 0x0004000
2549#define RTYPE_ACC 0x0008000
2550#define RTYPE_CCC 0x0010000
2551#define RTYPE_VI 0x0020000
2552#define RTYPE_VF 0x0040000
2553#define RTYPE_R5900_I 0x0080000
2554#define RTYPE_R5900_Q 0x0100000
2555#define RTYPE_R5900_R 0x0200000
2556#define RTYPE_R5900_ACC 0x0400000
56d438b1 2557#define RTYPE_MSA 0x0800000
14daeee3 2558#define RWARN 0x8000000
707bfff6
TS
2559
2560#define GENERIC_REGISTER_NUMBERS \
2561 {"$0", RTYPE_NUM | 0}, \
2562 {"$1", RTYPE_NUM | 1}, \
2563 {"$2", RTYPE_NUM | 2}, \
2564 {"$3", RTYPE_NUM | 3}, \
2565 {"$4", RTYPE_NUM | 4}, \
2566 {"$5", RTYPE_NUM | 5}, \
2567 {"$6", RTYPE_NUM | 6}, \
2568 {"$7", RTYPE_NUM | 7}, \
2569 {"$8", RTYPE_NUM | 8}, \
2570 {"$9", RTYPE_NUM | 9}, \
2571 {"$10", RTYPE_NUM | 10}, \
2572 {"$11", RTYPE_NUM | 11}, \
2573 {"$12", RTYPE_NUM | 12}, \
2574 {"$13", RTYPE_NUM | 13}, \
2575 {"$14", RTYPE_NUM | 14}, \
2576 {"$15", RTYPE_NUM | 15}, \
2577 {"$16", RTYPE_NUM | 16}, \
2578 {"$17", RTYPE_NUM | 17}, \
2579 {"$18", RTYPE_NUM | 18}, \
2580 {"$19", RTYPE_NUM | 19}, \
2581 {"$20", RTYPE_NUM | 20}, \
2582 {"$21", RTYPE_NUM | 21}, \
2583 {"$22", RTYPE_NUM | 22}, \
2584 {"$23", RTYPE_NUM | 23}, \
2585 {"$24", RTYPE_NUM | 24}, \
2586 {"$25", RTYPE_NUM | 25}, \
2587 {"$26", RTYPE_NUM | 26}, \
2588 {"$27", RTYPE_NUM | 27}, \
2589 {"$28", RTYPE_NUM | 28}, \
2590 {"$29", RTYPE_NUM | 29}, \
2591 {"$30", RTYPE_NUM | 30}, \
3739860c 2592 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2593
2594#define FPU_REGISTER_NAMES \
2595 {"$f0", RTYPE_FPU | 0}, \
2596 {"$f1", RTYPE_FPU | 1}, \
2597 {"$f2", RTYPE_FPU | 2}, \
2598 {"$f3", RTYPE_FPU | 3}, \
2599 {"$f4", RTYPE_FPU | 4}, \
2600 {"$f5", RTYPE_FPU | 5}, \
2601 {"$f6", RTYPE_FPU | 6}, \
2602 {"$f7", RTYPE_FPU | 7}, \
2603 {"$f8", RTYPE_FPU | 8}, \
2604 {"$f9", RTYPE_FPU | 9}, \
2605 {"$f10", RTYPE_FPU | 10}, \
2606 {"$f11", RTYPE_FPU | 11}, \
2607 {"$f12", RTYPE_FPU | 12}, \
2608 {"$f13", RTYPE_FPU | 13}, \
2609 {"$f14", RTYPE_FPU | 14}, \
2610 {"$f15", RTYPE_FPU | 15}, \
2611 {"$f16", RTYPE_FPU | 16}, \
2612 {"$f17", RTYPE_FPU | 17}, \
2613 {"$f18", RTYPE_FPU | 18}, \
2614 {"$f19", RTYPE_FPU | 19}, \
2615 {"$f20", RTYPE_FPU | 20}, \
2616 {"$f21", RTYPE_FPU | 21}, \
2617 {"$f22", RTYPE_FPU | 22}, \
2618 {"$f23", RTYPE_FPU | 23}, \
2619 {"$f24", RTYPE_FPU | 24}, \
2620 {"$f25", RTYPE_FPU | 25}, \
2621 {"$f26", RTYPE_FPU | 26}, \
2622 {"$f27", RTYPE_FPU | 27}, \
2623 {"$f28", RTYPE_FPU | 28}, \
2624 {"$f29", RTYPE_FPU | 29}, \
2625 {"$f30", RTYPE_FPU | 30}, \
2626 {"$f31", RTYPE_FPU | 31}
2627
2628#define FPU_CONDITION_CODE_NAMES \
2629 {"$fcc0", RTYPE_FCC | 0}, \
2630 {"$fcc1", RTYPE_FCC | 1}, \
2631 {"$fcc2", RTYPE_FCC | 2}, \
2632 {"$fcc3", RTYPE_FCC | 3}, \
2633 {"$fcc4", RTYPE_FCC | 4}, \
2634 {"$fcc5", RTYPE_FCC | 5}, \
2635 {"$fcc6", RTYPE_FCC | 6}, \
2636 {"$fcc7", RTYPE_FCC | 7}
2637
2638#define COPROC_CONDITION_CODE_NAMES \
2639 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2640 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2641 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2642 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2643 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2644 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2645 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2646 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2647
2648#define N32N64_SYMBOLIC_REGISTER_NAMES \
2649 {"$a4", RTYPE_GP | 8}, \
2650 {"$a5", RTYPE_GP | 9}, \
2651 {"$a6", RTYPE_GP | 10}, \
2652 {"$a7", RTYPE_GP | 11}, \
2653 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2654 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2655 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2656 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2657 {"$t0", RTYPE_GP | 12}, \
2658 {"$t1", RTYPE_GP | 13}, \
2659 {"$t2", RTYPE_GP | 14}, \
2660 {"$t3", RTYPE_GP | 15}
2661
2662#define O32_SYMBOLIC_REGISTER_NAMES \
2663 {"$t0", RTYPE_GP | 8}, \
2664 {"$t1", RTYPE_GP | 9}, \
2665 {"$t2", RTYPE_GP | 10}, \
2666 {"$t3", RTYPE_GP | 11}, \
2667 {"$t4", RTYPE_GP | 12}, \
2668 {"$t5", RTYPE_GP | 13}, \
2669 {"$t6", RTYPE_GP | 14}, \
2670 {"$t7", RTYPE_GP | 15}, \
2671 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2672 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2673 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2674 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2675
2676/* Remaining symbolic register names */
2677#define SYMBOLIC_REGISTER_NAMES \
2678 {"$zero", RTYPE_GP | 0}, \
2679 {"$at", RTYPE_GP | 1}, \
2680 {"$AT", RTYPE_GP | 1}, \
2681 {"$v0", RTYPE_GP | 2}, \
2682 {"$v1", RTYPE_GP | 3}, \
2683 {"$a0", RTYPE_GP | 4}, \
2684 {"$a1", RTYPE_GP | 5}, \
2685 {"$a2", RTYPE_GP | 6}, \
2686 {"$a3", RTYPE_GP | 7}, \
2687 {"$s0", RTYPE_GP | 16}, \
2688 {"$s1", RTYPE_GP | 17}, \
2689 {"$s2", RTYPE_GP | 18}, \
2690 {"$s3", RTYPE_GP | 19}, \
2691 {"$s4", RTYPE_GP | 20}, \
2692 {"$s5", RTYPE_GP | 21}, \
2693 {"$s6", RTYPE_GP | 22}, \
2694 {"$s7", RTYPE_GP | 23}, \
2695 {"$t8", RTYPE_GP | 24}, \
2696 {"$t9", RTYPE_GP | 25}, \
2697 {"$k0", RTYPE_GP | 26}, \
2698 {"$kt0", RTYPE_GP | 26}, \
2699 {"$k1", RTYPE_GP | 27}, \
2700 {"$kt1", RTYPE_GP | 27}, \
2701 {"$gp", RTYPE_GP | 28}, \
2702 {"$sp", RTYPE_GP | 29}, \
2703 {"$s8", RTYPE_GP | 30}, \
2704 {"$fp", RTYPE_GP | 30}, \
2705 {"$ra", RTYPE_GP | 31}
2706
2707#define MIPS16_SPECIAL_REGISTER_NAMES \
2708 {"$pc", RTYPE_PC | 0}
2709
2710#define MDMX_VECTOR_REGISTER_NAMES \
2711 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2712 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2713 {"$v2", RTYPE_VEC | 2}, \
2714 {"$v3", RTYPE_VEC | 3}, \
2715 {"$v4", RTYPE_VEC | 4}, \
2716 {"$v5", RTYPE_VEC | 5}, \
2717 {"$v6", RTYPE_VEC | 6}, \
2718 {"$v7", RTYPE_VEC | 7}, \
2719 {"$v8", RTYPE_VEC | 8}, \
2720 {"$v9", RTYPE_VEC | 9}, \
2721 {"$v10", RTYPE_VEC | 10}, \
2722 {"$v11", RTYPE_VEC | 11}, \
2723 {"$v12", RTYPE_VEC | 12}, \
2724 {"$v13", RTYPE_VEC | 13}, \
2725 {"$v14", RTYPE_VEC | 14}, \
2726 {"$v15", RTYPE_VEC | 15}, \
2727 {"$v16", RTYPE_VEC | 16}, \
2728 {"$v17", RTYPE_VEC | 17}, \
2729 {"$v18", RTYPE_VEC | 18}, \
2730 {"$v19", RTYPE_VEC | 19}, \
2731 {"$v20", RTYPE_VEC | 20}, \
2732 {"$v21", RTYPE_VEC | 21}, \
2733 {"$v22", RTYPE_VEC | 22}, \
2734 {"$v23", RTYPE_VEC | 23}, \
2735 {"$v24", RTYPE_VEC | 24}, \
2736 {"$v25", RTYPE_VEC | 25}, \
2737 {"$v26", RTYPE_VEC | 26}, \
2738 {"$v27", RTYPE_VEC | 27}, \
2739 {"$v28", RTYPE_VEC | 28}, \
2740 {"$v29", RTYPE_VEC | 29}, \
2741 {"$v30", RTYPE_VEC | 30}, \
2742 {"$v31", RTYPE_VEC | 31}
2743
14daeee3
RS
2744#define R5900_I_NAMES \
2745 {"$I", RTYPE_R5900_I | 0}
2746
2747#define R5900_Q_NAMES \
2748 {"$Q", RTYPE_R5900_Q | 0}
2749
2750#define R5900_R_NAMES \
2751 {"$R", RTYPE_R5900_R | 0}
2752
2753#define R5900_ACC_NAMES \
2754 {"$ACC", RTYPE_R5900_ACC | 0 }
2755
707bfff6
TS
2756#define MIPS_DSP_ACCUMULATOR_NAMES \
2757 {"$ac0", RTYPE_ACC | 0}, \
2758 {"$ac1", RTYPE_ACC | 1}, \
2759 {"$ac2", RTYPE_ACC | 2}, \
2760 {"$ac3", RTYPE_ACC | 3}
2761
2762static const struct regname reg_names[] = {
2763 GENERIC_REGISTER_NUMBERS,
2764 FPU_REGISTER_NAMES,
2765 FPU_CONDITION_CODE_NAMES,
2766 COPROC_CONDITION_CODE_NAMES,
2767
2768 /* The $txx registers depends on the abi,
2769 these will be added later into the symbol table from
3739860c 2770 one of the tables below once mips_abi is set after
707bfff6
TS
2771 parsing of arguments from the command line. */
2772 SYMBOLIC_REGISTER_NAMES,
2773
2774 MIPS16_SPECIAL_REGISTER_NAMES,
2775 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2776 R5900_I_NAMES,
2777 R5900_Q_NAMES,
2778 R5900_R_NAMES,
2779 R5900_ACC_NAMES,
707bfff6
TS
2780 MIPS_DSP_ACCUMULATOR_NAMES,
2781 {0, 0}
2782};
2783
2784static const struct regname reg_names_o32[] = {
2785 O32_SYMBOLIC_REGISTER_NAMES,
2786 {0, 0}
2787};
2788
2789static const struct regname reg_names_n32n64[] = {
2790 N32N64_SYMBOLIC_REGISTER_NAMES,
2791 {0, 0}
2792};
2793
a92713e6
RS
2794/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2795 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2796 of these register symbols, return the associated vector register,
2797 otherwise return SYMVAL itself. */
df58fc94 2798
a92713e6
RS
2799static unsigned int
2800mips_prefer_vec_regno (unsigned int symval)
707bfff6 2801{
a92713e6
RS
2802 if ((symval & -2) == (RTYPE_GP | 2))
2803 return RTYPE_VEC | (symval & 1);
2804 return symval;
2805}
2806
14daeee3
RS
2807/* Return true if string [S, E) is a valid register name, storing its
2808 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2809
2810static bfd_boolean
14daeee3 2811mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2812{
707bfff6 2813 char save_c;
14daeee3 2814 symbolS *symbol;
707bfff6
TS
2815
2816 /* Terminate name. */
2817 save_c = *e;
2818 *e = '\0';
2819
a92713e6
RS
2820 /* Look up the name. */
2821 symbol = symbol_find (s);
2822 *e = save_c;
2823
2824 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2825 return FALSE;
2826
14daeee3
RS
2827 *symval_ptr = S_GET_VALUE (symbol);
2828 return TRUE;
2829}
2830
2831/* Return true if the string at *SPTR is a valid register name. Allow it
2832 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2833 is nonnull.
2834
2835 When returning true, move *SPTR past the register, store the
2836 register's symbol value in *SYMVAL_PTR and the channel mask in
2837 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2838 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2839 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2840
2841static bfd_boolean
2842mips_parse_register (char **sptr, unsigned int *symval_ptr,
2843 unsigned int *channels_ptr)
2844{
2845 char *s, *e, *m;
2846 const char *q;
2847 unsigned int channels, symval, bit;
2848
2849 /* Find end of name. */
2850 s = e = *sptr;
2851 if (is_name_beginner (*e))
2852 ++e;
2853 while (is_part_of_name (*e))
2854 ++e;
2855
2856 channels = 0;
2857 if (!mips_parse_register_1 (s, e, &symval))
2858 {
2859 if (!channels_ptr)
2860 return FALSE;
2861
2862 /* Eat characters from the end of the string that are valid
2863 channel suffixes. The preceding register must be $ACC or
2864 end with a digit, so there is no ambiguity. */
2865 bit = 1;
2866 m = e;
2867 for (q = "wzyx"; *q; q++, bit <<= 1)
2868 if (m > s && m[-1] == *q)
2869 {
2870 --m;
2871 channels |= bit;
2872 }
2873
2874 if (channels == 0
2875 || !mips_parse_register_1 (s, m, &symval)
2876 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2877 return FALSE;
2878 }
2879
a92713e6 2880 *sptr = e;
14daeee3
RS
2881 *symval_ptr = symval;
2882 if (channels_ptr)
2883 *channels_ptr = channels;
a92713e6
RS
2884 return TRUE;
2885}
2886
2887/* Check if SPTR points at a valid register specifier according to TYPES.
2888 If so, then return 1, advance S to consume the specifier and store
2889 the register's number in REGNOP, otherwise return 0. */
2890
2891static int
2892reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2893{
2894 unsigned int regno;
2895
14daeee3 2896 if (mips_parse_register (s, &regno, NULL))
707bfff6 2897 {
a92713e6
RS
2898 if (types & RTYPE_VEC)
2899 regno = mips_prefer_vec_regno (regno);
2900 if (regno & types)
2901 regno &= RNUM_MASK;
2902 else
2903 regno = ~0;
707bfff6 2904 }
a92713e6 2905 else
707bfff6 2906 {
a92713e6 2907 if (types & RWARN)
1661c76c 2908 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2909 regno = ~0;
707bfff6 2910 }
707bfff6 2911 if (regnop)
a92713e6
RS
2912 *regnop = regno;
2913 return regno <= RNUM_MASK;
707bfff6
TS
2914}
2915
14daeee3
RS
2916/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2917 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2918
2919static char *
2920mips_parse_vu0_channels (char *s, unsigned int *channels)
2921{
2922 unsigned int i;
2923
2924 *channels = 0;
2925 for (i = 0; i < 4; i++)
2926 if (*s == "xyzw"[i])
2927 {
2928 *channels |= 1 << (3 - i);
2929 ++s;
2930 }
2931 return s;
2932}
2933
a92713e6
RS
2934/* Token types for parsed operand lists. */
2935enum mips_operand_token_type {
2936 /* A plain register, e.g. $f2. */
2937 OT_REG,
df58fc94 2938
14daeee3
RS
2939 /* A 4-bit XYZW channel mask. */
2940 OT_CHANNELS,
2941
56d438b1
CF
2942 /* A constant vector index, e.g. [1]. */
2943 OT_INTEGER_INDEX,
2944
2945 /* A register vector index, e.g. [$2]. */
2946 OT_REG_INDEX,
df58fc94 2947
a92713e6
RS
2948 /* A continuous range of registers, e.g. $s0-$s4. */
2949 OT_REG_RANGE,
2950
2951 /* A (possibly relocated) expression. */
2952 OT_INTEGER,
2953
2954 /* A floating-point value. */
2955 OT_FLOAT,
2956
2957 /* A single character. This can be '(', ')' or ',', but '(' only appears
2958 before OT_REGs. */
2959 OT_CHAR,
2960
14daeee3
RS
2961 /* A doubled character, either "--" or "++". */
2962 OT_DOUBLE_CHAR,
2963
a92713e6
RS
2964 /* The end of the operand list. */
2965 OT_END
2966};
2967
2968/* A parsed operand token. */
2969struct mips_operand_token
2970{
2971 /* The type of token. */
2972 enum mips_operand_token_type type;
2973 union
2974 {
56d438b1 2975 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
2976 unsigned int regno;
2977
14daeee3
RS
2978 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2979 unsigned int channels;
2980
56d438b1
CF
2981 /* The integer value of an OT_INTEGER_INDEX. */
2982 addressT index;
a92713e6
RS
2983
2984 /* The two register symbol values involved in an OT_REG_RANGE. */
2985 struct {
2986 unsigned int regno1;
2987 unsigned int regno2;
2988 } reg_range;
2989
2990 /* The value of an OT_INTEGER. The value is represented as an
2991 expression and the relocation operators that were applied to
2992 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2993 relocation operators were used. */
2994 struct {
2995 expressionS value;
2996 bfd_reloc_code_real_type relocs[3];
2997 } integer;
2998
2999 /* The binary data for an OT_FLOAT constant, and the number of bytes
3000 in the constant. */
3001 struct {
3002 unsigned char data[8];
3003 int length;
3004 } flt;
3005
14daeee3 3006 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3007 char ch;
3008 } u;
3009};
3010
3011/* An obstack used to construct lists of mips_operand_tokens. */
3012static struct obstack mips_operand_tokens;
3013
3014/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3015
3016static void
3017mips_add_token (struct mips_operand_token *token,
3018 enum mips_operand_token_type type)
3019{
3020 token->type = type;
3021 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3022}
3023
3024/* Check whether S is '(' followed by a register name. Add OT_CHAR
3025 and OT_REG tokens for them if so, and return a pointer to the first
3026 unconsumed character. Return null otherwise. */
3027
3028static char *
3029mips_parse_base_start (char *s)
3030{
3031 struct mips_operand_token token;
14daeee3
RS
3032 unsigned int regno, channels;
3033 bfd_boolean decrement_p;
df58fc94 3034
a92713e6
RS
3035 if (*s != '(')
3036 return 0;
3037
3038 ++s;
3039 SKIP_SPACE_TABS (s);
14daeee3
RS
3040
3041 /* Only match "--" as part of a base expression. In other contexts "--X"
3042 is a double negative. */
3043 decrement_p = (s[0] == '-' && s[1] == '-');
3044 if (decrement_p)
3045 {
3046 s += 2;
3047 SKIP_SPACE_TABS (s);
3048 }
3049
3050 /* Allow a channel specifier because that leads to better error messages
3051 than treating something like "$vf0x++" as an expression. */
3052 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3053 return 0;
3054
3055 token.u.ch = '(';
3056 mips_add_token (&token, OT_CHAR);
3057
14daeee3
RS
3058 if (decrement_p)
3059 {
3060 token.u.ch = '-';
3061 mips_add_token (&token, OT_DOUBLE_CHAR);
3062 }
3063
a92713e6
RS
3064 token.u.regno = regno;
3065 mips_add_token (&token, OT_REG);
3066
14daeee3
RS
3067 if (channels)
3068 {
3069 token.u.channels = channels;
3070 mips_add_token (&token, OT_CHANNELS);
3071 }
3072
3073 /* For consistency, only match "++" as part of base expressions too. */
3074 SKIP_SPACE_TABS (s);
3075 if (s[0] == '+' && s[1] == '+')
3076 {
3077 s += 2;
3078 token.u.ch = '+';
3079 mips_add_token (&token, OT_DOUBLE_CHAR);
3080 }
3081
a92713e6
RS
3082 return s;
3083}
3084
3085/* Parse one or more tokens from S. Return a pointer to the first
3086 unconsumed character on success. Return null if an error was found
3087 and store the error text in insn_error. FLOAT_FORMAT is as for
3088 mips_parse_arguments. */
3089
3090static char *
3091mips_parse_argument_token (char *s, char float_format)
3092{
6d4af3c2
AM
3093 char *end, *save_in;
3094 const char *err;
14daeee3 3095 unsigned int regno1, regno2, channels;
a92713e6
RS
3096 struct mips_operand_token token;
3097
3098 /* First look for "($reg", since we want to treat that as an
3099 OT_CHAR and OT_REG rather than an expression. */
3100 end = mips_parse_base_start (s);
3101 if (end)
3102 return end;
3103
3104 /* Handle other characters that end up as OT_CHARs. */
3105 if (*s == ')' || *s == ',')
3106 {
3107 token.u.ch = *s;
3108 mips_add_token (&token, OT_CHAR);
3109 ++s;
3110 return s;
3111 }
3112
3113 /* Handle tokens that start with a register. */
14daeee3 3114 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3115 {
14daeee3
RS
3116 if (channels)
3117 {
3118 /* A register and a VU0 channel suffix. */
3119 token.u.regno = regno1;
3120 mips_add_token (&token, OT_REG);
3121
3122 token.u.channels = channels;
3123 mips_add_token (&token, OT_CHANNELS);
3124 return s;
3125 }
3126
a92713e6
RS
3127 SKIP_SPACE_TABS (s);
3128 if (*s == '-')
df58fc94 3129 {
a92713e6
RS
3130 /* A register range. */
3131 ++s;
3132 SKIP_SPACE_TABS (s);
14daeee3 3133 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3134 {
1661c76c 3135 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3136 return 0;
3137 }
df58fc94 3138
a92713e6
RS
3139 token.u.reg_range.regno1 = regno1;
3140 token.u.reg_range.regno2 = regno2;
3141 mips_add_token (&token, OT_REG_RANGE);
3142 return s;
3143 }
a92713e6 3144
56d438b1
CF
3145 /* Add the register itself. */
3146 token.u.regno = regno1;
3147 mips_add_token (&token, OT_REG);
3148
3149 /* Check for a vector index. */
3150 if (*s == '[')
3151 {
a92713e6
RS
3152 ++s;
3153 SKIP_SPACE_TABS (s);
56d438b1
CF
3154 if (mips_parse_register (&s, &token.u.regno, NULL))
3155 mips_add_token (&token, OT_REG_INDEX);
3156 else
a92713e6 3157 {
56d438b1
CF
3158 expressionS element;
3159
3160 my_getExpression (&element, s);
3161 if (element.X_op != O_constant)
3162 {
3163 set_insn_error (0, _("vector element must be constant"));
3164 return 0;
3165 }
3166 s = expr_end;
3167 token.u.index = element.X_add_number;
3168 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3169 }
a92713e6
RS
3170 SKIP_SPACE_TABS (s);
3171 if (*s != ']')
3172 {
1661c76c 3173 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3174 return 0;
3175 }
3176 ++s;
df58fc94 3177 }
a92713e6 3178 return s;
df58fc94
RS
3179 }
3180
a92713e6
RS
3181 if (float_format)
3182 {
3183 /* First try to treat expressions as floats. */
3184 save_in = input_line_pointer;
3185 input_line_pointer = s;
3186 err = md_atof (float_format, (char *) token.u.flt.data,
3187 &token.u.flt.length);
3188 end = input_line_pointer;
3189 input_line_pointer = save_in;
3190 if (err && *err)
3191 {
e3de51ce 3192 set_insn_error (0, err);
a92713e6
RS
3193 return 0;
3194 }
3195 if (s != end)
3196 {
3197 mips_add_token (&token, OT_FLOAT);
3198 return end;
3199 }
3200 }
3201
3202 /* Treat everything else as an integer expression. */
3203 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3204 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3205 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3206 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3207 s = expr_end;
3208 mips_add_token (&token, OT_INTEGER);
3209 return s;
3210}
3211
3212/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3213 if expressions should be treated as 32-bit floating-point constants,
3214 'd' if they should be treated as 64-bit floating-point constants,
3215 or 0 if they should be treated as integer expressions (the usual case).
3216
3217 Return a list of tokens on success, otherwise return 0. The caller
3218 must obstack_free the list after use. */
3219
3220static struct mips_operand_token *
3221mips_parse_arguments (char *s, char float_format)
3222{
3223 struct mips_operand_token token;
3224
3225 SKIP_SPACE_TABS (s);
3226 while (*s)
3227 {
3228 s = mips_parse_argument_token (s, float_format);
3229 if (!s)
3230 {
3231 obstack_free (&mips_operand_tokens,
3232 obstack_finish (&mips_operand_tokens));
3233 return 0;
3234 }
3235 SKIP_SPACE_TABS (s);
3236 }
3237 mips_add_token (&token, OT_END);
3238 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3239}
3240
d301a56b
RS
3241/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3242 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3243
3244static bfd_boolean
f79e2745 3245is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3246{
3247 int isa = mips_opts.isa;
846ef2d0 3248 int ase = mips_opts.ase;
037b32b9 3249 int fp_s, fp_d;
c6278170 3250 unsigned int i;
037b32b9 3251
be0fcbee 3252 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3253 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3254 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3255 ase |= mips_ases[i].flags64;
037b32b9 3256
d301a56b 3257 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3258 return FALSE;
3259
3260 /* Check whether the instruction or macro requires single-precision or
3261 double-precision floating-point support. Note that this information is
3262 stored differently in the opcode table for insns and macros. */
3263 if (mo->pinfo == INSN_MACRO)
3264 {
3265 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3266 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3267 }
3268 else
3269 {
3270 fp_s = mo->pinfo & FP_S;
3271 fp_d = mo->pinfo & FP_D;
3272 }
3273
3274 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3275 return FALSE;
3276
3277 if (fp_s && mips_opts.soft_float)
3278 return FALSE;
3279
3280 return TRUE;
3281}
3282
3283/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3284 selected ISA and architecture. */
3285
3286static bfd_boolean
3287is_opcode_valid_16 (const struct mips_opcode *mo)
3288{
d301a56b 3289 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3290}
3291
df58fc94 3292/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3293 explicitly requested. Always TRUE in the standard MIPS mode.
3294 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3295
3296static bfd_boolean
3297is_size_valid (const struct mips_opcode *mo)
3298{
3299 if (!mips_opts.micromips)
3300 return TRUE;
3301
833794fc
MR
3302 if (mips_opts.insn32)
3303 {
3304 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3305 return FALSE;
3306 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3307 return FALSE;
3308 }
df58fc94
RS
3309 if (!forced_insn_length)
3310 return TRUE;
3311 if (mo->pinfo == INSN_MACRO)
3312 return FALSE;
3313 return forced_insn_length == micromips_insn_length (mo);
3314}
3315
7fd53920
MR
3316/* Return TRUE if the size of the MIPS16 opcode MO matches one
3317 explicitly requested. */
3318
3319static bfd_boolean
3320is_size_valid_16 (const struct mips_opcode *mo)
3321{
3322 if (!forced_insn_length)
3323 return TRUE;
3324 if (mo->pinfo == INSN_MACRO)
3325 return FALSE;
3326 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3327 return FALSE;
0674ee5d
MR
3328 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3329 return FALSE;
7fd53920
MR
3330 return TRUE;
3331}
3332
df58fc94 3333/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3334 of the preceding instruction. Always TRUE in the standard MIPS mode.
3335
3336 We don't accept macros in 16-bit delay slots to avoid a case where
3337 a macro expansion fails because it relies on a preceding 32-bit real
3338 instruction to have matched and does not handle the operands correctly.
3339 The only macros that may expand to 16-bit instructions are JAL that
3340 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3341 and BGT (that likewise cannot be placed in a delay slot) that decay to
3342 a NOP. In all these cases the macros precede any corresponding real
3343 instruction definitions in the opcode table, so they will match in the
3344 second pass where the size of the delay slot is ignored and therefore
3345 produce correct code. */
df58fc94
RS
3346
3347static bfd_boolean
3348is_delay_slot_valid (const struct mips_opcode *mo)
3349{
3350 if (!mips_opts.micromips)
3351 return TRUE;
3352
3353 if (mo->pinfo == INSN_MACRO)
c06dec14 3354 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3355 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3356 && micromips_insn_length (mo) != 4)
3357 return FALSE;
3358 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3359 && micromips_insn_length (mo) != 2)
3360 return FALSE;
3361
3362 return TRUE;
3363}
3364
fc76e730
RS
3365/* For consistency checking, verify that all bits of OPCODE are specified
3366 either by the match/mask part of the instruction definition, or by the
3367 operand list. Also build up a list of operands in OPERANDS.
3368
3369 INSN_BITS says which bits of the instruction are significant.
3370 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3371 provides the mips_operand description of each operand. DECODE_OPERAND
3372 is null for MIPS16 instructions. */
ab902481
RS
3373
3374static int
3375validate_mips_insn (const struct mips_opcode *opcode,
3376 unsigned long insn_bits,
fc76e730
RS
3377 const struct mips_operand *(*decode_operand) (const char *),
3378 struct mips_operand_array *operands)
ab902481
RS
3379{
3380 const char *s;
fc76e730 3381 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3382 const struct mips_operand *operand;
3383
fc76e730
RS
3384 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3385 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3386 {
3387 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3388 opcode->name, opcode->args);
3389 return 0;
3390 }
3391 used_bits = 0;
fc76e730 3392 opno = 0;
14daeee3
RS
3393 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3394 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3395 for (s = opcode->args; *s; ++s)
3396 switch (*s)
3397 {
3398 case ',':
3399 case '(':
3400 case ')':
3401 break;
3402
14daeee3
RS
3403 case '#':
3404 s++;
3405 break;
3406
ab902481 3407 default:
fc76e730 3408 if (!decode_operand)
7fd53920 3409 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3410 else
3411 operand = decode_operand (s);
3412 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3413 {
3414 as_bad (_("internal: unknown operand type: %s %s"),
3415 opcode->name, opcode->args);
3416 return 0;
3417 }
fc76e730
RS
3418 gas_assert (opno < MAX_OPERANDS);
3419 operands->operand[opno] = operand;
14daeee3 3420 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3421 {
14daeee3 3422 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3423 if (operand->type == OP_MDMX_IMM_REG)
3424 /* Bit 5 is the format selector (OB vs QH). The opcode table
3425 has separate entries for each format. */
3426 used_bits &= ~(1 << (operand->lsb + 5));
3427 if (operand->type == OP_ENTRY_EXIT_LIST)
3428 used_bits &= ~(mask & 0x700);
3429 }
ab902481 3430 /* Skip prefix characters. */
7361da2c 3431 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3432 ++s;
fc76e730 3433 opno += 1;
ab902481
RS
3434 break;
3435 }
fc76e730 3436 doubled = used_bits & mask & insn_bits;
ab902481
RS
3437 if (doubled)
3438 {
3439 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3440 " %s %s"), doubled, opcode->name, opcode->args);
3441 return 0;
3442 }
fc76e730 3443 used_bits |= mask;
ab902481 3444 undefined = ~used_bits & insn_bits;
fc76e730 3445 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3446 {
3447 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3448 undefined, opcode->name, opcode->args);
3449 return 0;
3450 }
3451 used_bits &= ~insn_bits;
3452 if (used_bits)
3453 {
3454 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3455 used_bits, opcode->name, opcode->args);
3456 return 0;
3457 }
3458 return 1;
3459}
3460
fc76e730
RS
3461/* The MIPS16 version of validate_mips_insn. */
3462
3463static int
3464validate_mips16_insn (const struct mips_opcode *opcode,
3465 struct mips_operand_array *operands)
3466{
7fd53920 3467 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3468
7fd53920 3469 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3470}
3471
ab902481
RS
3472/* The microMIPS version of validate_mips_insn. */
3473
3474static int
fc76e730
RS
3475validate_micromips_insn (const struct mips_opcode *opc,
3476 struct mips_operand_array *operands)
ab902481
RS
3477{
3478 unsigned long insn_bits;
3479 unsigned long major;
3480 unsigned int length;
3481
fc76e730
RS
3482 if (opc->pinfo == INSN_MACRO)
3483 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3484 operands);
3485
ab902481
RS
3486 length = micromips_insn_length (opc);
3487 if (length != 2 && length != 4)
3488 {
1661c76c 3489 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3490 "%s %s"), length, opc->name, opc->args);
3491 return 0;
3492 }
3493 major = opc->match >> (10 + 8 * (length - 2));
3494 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3495 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3496 {
1661c76c 3497 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3498 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3499 return 0;
3500 }
3501
3502 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3503 insn_bits = 1 << 4 * length;
3504 insn_bits <<= 4 * length;
3505 insn_bits -= 1;
fc76e730
RS
3506 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3507 operands);
ab902481
RS
3508}
3509
707bfff6
TS
3510/* This function is called once, at assembler startup time. It should set up
3511 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3512
252b5132 3513void
17a2f251 3514md_begin (void)
252b5132 3515{
3994f87e 3516 const char *retval = NULL;
156c2f8b 3517 int i = 0;
252b5132 3518 int broken = 0;
1f25f5d3 3519
0a44bf69
RS
3520 if (mips_pic != NO_PIC)
3521 {
3522 if (g_switch_seen && g_switch_value != 0)
3523 as_bad (_("-G may not be used in position-independent code"));
3524 g_switch_value = 0;
3525 }
00acd688
CM
3526 else if (mips_abicalls)
3527 {
3528 if (g_switch_seen && g_switch_value != 0)
3529 as_bad (_("-G may not be used with abicalls"));
3530 g_switch_value = 0;
3531 }
0a44bf69 3532
0b35dfee 3533 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3534 as_warn (_("could not set architecture and machine"));
252b5132 3535
252b5132
RH
3536 op_hash = hash_new ();
3537
fc76e730 3538 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3539 for (i = 0; i < NUMOPCODES;)
3540 {
3541 const char *name = mips_opcodes[i].name;
3542
17a2f251 3543 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3544 if (retval != NULL)
3545 {
3546 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3547 mips_opcodes[i].name, retval);
3548 /* Probably a memory allocation problem? Give up now. */
1661c76c 3549 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3550 }
3551 do
3552 {
fc76e730
RS
3553 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3554 decode_mips_operand, &mips_operands[i]))
3555 broken = 1;
3556 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3557 {
fc76e730
RS
3558 create_insn (&nop_insn, mips_opcodes + i);
3559 if (mips_fix_loongson2f_nop)
3560 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3561 nop_insn.fixed_p = 1;
252b5132
RH
3562 }
3563 ++i;
3564 }
3565 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3566 }
3567
3568 mips16_op_hash = hash_new ();
fc76e730
RS
3569 mips16_operands = XCNEWVEC (struct mips_operand_array,
3570 bfd_mips16_num_opcodes);
252b5132
RH
3571
3572 i = 0;
3573 while (i < bfd_mips16_num_opcodes)
3574 {
3575 const char *name = mips16_opcodes[i].name;
3576
17a2f251 3577 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3578 if (retval != NULL)
3579 as_fatal (_("internal: can't hash `%s': %s"),
3580 mips16_opcodes[i].name, retval);
3581 do
3582 {
fc76e730
RS
3583 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3584 broken = 1;
1e915849
RS
3585 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3586 {
3587 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3588 mips16_nop_insn.fixed_p = 1;
3589 }
252b5132
RH
3590 ++i;
3591 }
3592 while (i < bfd_mips16_num_opcodes
3593 && strcmp (mips16_opcodes[i].name, name) == 0);
3594 }
3595
df58fc94 3596 micromips_op_hash = hash_new ();
fc76e730
RS
3597 micromips_operands = XCNEWVEC (struct mips_operand_array,
3598 bfd_micromips_num_opcodes);
df58fc94
RS
3599
3600 i = 0;
3601 while (i < bfd_micromips_num_opcodes)
3602 {
3603 const char *name = micromips_opcodes[i].name;
3604
3605 retval = hash_insert (micromips_op_hash, name,
3606 (void *) &micromips_opcodes[i]);
3607 if (retval != NULL)
3608 as_fatal (_("internal: can't hash `%s': %s"),
3609 micromips_opcodes[i].name, retval);
3610 do
fc76e730
RS
3611 {
3612 struct mips_cl_insn *micromips_nop_insn;
3613
3614 if (!validate_micromips_insn (&micromips_opcodes[i],
3615 &micromips_operands[i]))
3616 broken = 1;
3617
3618 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3619 {
3620 if (micromips_insn_length (micromips_opcodes + i) == 2)
3621 micromips_nop_insn = &micromips_nop16_insn;
3622 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3623 micromips_nop_insn = &micromips_nop32_insn;
3624 else
3625 continue;
3626
3627 if (micromips_nop_insn->insn_mo == NULL
3628 && strcmp (name, "nop") == 0)
3629 {
3630 create_insn (micromips_nop_insn, micromips_opcodes + i);
3631 micromips_nop_insn->fixed_p = 1;
3632 }
3633 }
3634 }
df58fc94
RS
3635 while (++i < bfd_micromips_num_opcodes
3636 && strcmp (micromips_opcodes[i].name, name) == 0);
3637 }
3638
252b5132 3639 if (broken)
1661c76c 3640 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3641
3642 /* We add all the general register names to the symbol table. This
3643 helps us detect invalid uses of them. */
3739860c 3644 for (i = 0; reg_names[i].name; i++)
707bfff6 3645 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3646 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3647 &zero_address_frag));
3648 if (HAVE_NEWABI)
3739860c 3649 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3650 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3651 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3652 &zero_address_frag));
707bfff6 3653 else
3739860c 3654 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3655 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3656 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3657 &zero_address_frag));
6047c971 3658
14daeee3
RS
3659 for (i = 0; i < 32; i++)
3660 {
92fce9bd 3661 char regname[6];
14daeee3
RS
3662
3663 /* R5900 VU0 floating-point register. */
92fce9bd 3664 sprintf (regname, "$vf%d", i);
14daeee3
RS
3665 symbol_table_insert (symbol_new (regname, reg_section,
3666 RTYPE_VF | i, &zero_address_frag));
3667
3668 /* R5900 VU0 integer register. */
92fce9bd 3669 sprintf (regname, "$vi%d", i);
14daeee3
RS
3670 symbol_table_insert (symbol_new (regname, reg_section,
3671 RTYPE_VI | i, &zero_address_frag));
3672
56d438b1 3673 /* MSA register. */
92fce9bd 3674 sprintf (regname, "$w%d", i);
56d438b1
CF
3675 symbol_table_insert (symbol_new (regname, reg_section,
3676 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3677 }
3678
a92713e6
RS
3679 obstack_init (&mips_operand_tokens);
3680
7d10b47d 3681 mips_no_prev_insn ();
252b5132
RH
3682
3683 mips_gprmask = 0;
3684 mips_cprmask[0] = 0;
3685 mips_cprmask[1] = 0;
3686 mips_cprmask[2] = 0;
3687 mips_cprmask[3] = 0;
3688
3689 /* set the default alignment for the text section (2**2) */
3690 record_alignment (text_section, 2);
3691
4d0d148d 3692 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3693
f3ded42a
RS
3694 /* On a native system other than VxWorks, sections must be aligned
3695 to 16 byte boundaries. When configured for an embedded ELF
3696 target, we don't bother. */
3697 if (strncmp (TARGET_OS, "elf", 3) != 0
3698 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3699 {
f3ded42a
RS
3700 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3701 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3702 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3703 }
252b5132 3704
f3ded42a
RS
3705 /* Create a .reginfo section for register masks and a .mdebug
3706 section for debugging information. */
3707 {
3708 segT seg;
3709 subsegT subseg;
3710 flagword flags;
3711 segT sec;
3712
3713 seg = now_seg;
3714 subseg = now_subseg;
3715
3716 /* The ABI says this section should be loaded so that the
3717 running program can access it. However, we don't load it
3718 if we are configured for an embedded target */
3719 flags = SEC_READONLY | SEC_DATA;
3720 if (strncmp (TARGET_OS, "elf", 3) != 0)
3721 flags |= SEC_ALLOC | SEC_LOAD;
3722
3723 if (mips_abi != N64_ABI)
252b5132 3724 {
f3ded42a 3725 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3726
f3ded42a
RS
3727 bfd_set_section_flags (stdoutput, sec, flags);
3728 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3729
f3ded42a
RS
3730 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3731 }
3732 else
3733 {
3734 /* The 64-bit ABI uses a .MIPS.options section rather than
3735 .reginfo section. */
3736 sec = subseg_new (".MIPS.options", (subsegT) 0);
3737 bfd_set_section_flags (stdoutput, sec, flags);
3738 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3739
f3ded42a
RS
3740 /* Set up the option header. */
3741 {
3742 Elf_Internal_Options opthdr;
3743 char *f;
3744
3745 opthdr.kind = ODK_REGINFO;
3746 opthdr.size = (sizeof (Elf_External_Options)
3747 + sizeof (Elf64_External_RegInfo));
3748 opthdr.section = 0;
3749 opthdr.info = 0;
3750 f = frag_more (sizeof (Elf_External_Options));
3751 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3752 (Elf_External_Options *) f);
3753
3754 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3755 }
3756 }
252b5132 3757
351cdf24
MF
3758 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3759 bfd_set_section_flags (stdoutput, sec,
3760 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3761 bfd_set_section_alignment (stdoutput, sec, 3);
3762 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3763
f3ded42a
RS
3764 if (ECOFF_DEBUGGING)
3765 {
3766 sec = subseg_new (".mdebug", (subsegT) 0);
3767 (void) bfd_set_section_flags (stdoutput, sec,
3768 SEC_HAS_CONTENTS | SEC_READONLY);
3769 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3770 }
f3ded42a
RS
3771 else if (mips_flag_pdr)
3772 {
3773 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3774 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3775 SEC_READONLY | SEC_RELOC
3776 | SEC_DEBUGGING);
3777 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3778 }
3779
3780 subseg_set (seg, subseg);
3781 }
252b5132 3782
71400594
RS
3783 if (mips_fix_vr4120)
3784 init_vr4120_conflicts ();
252b5132
RH
3785}
3786
351cdf24
MF
3787static inline void
3788fpabi_incompatible_with (int fpabi, const char *what)
3789{
3790 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3791 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3792}
3793
3794static inline void
3795fpabi_requires (int fpabi, const char *what)
3796{
3797 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3798 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3799}
3800
3801/* Check -mabi and register sizes against the specified FP ABI. */
3802static void
3803check_fpabi (int fpabi)
3804{
351cdf24
MF
3805 switch (fpabi)
3806 {
3807 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3808 if (file_mips_opts.soft_float)
3809 fpabi_incompatible_with (fpabi, "softfloat");
3810 else if (file_mips_opts.single_float)
3811 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3812 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3813 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3814 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3815 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3816 break;
3817
3818 case Val_GNU_MIPS_ABI_FP_XX:
3819 if (mips_abi != O32_ABI)
3820 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3821 else if (file_mips_opts.soft_float)
3822 fpabi_incompatible_with (fpabi, "softfloat");
3823 else if (file_mips_opts.single_float)
3824 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3825 else if (file_mips_opts.fp != 0)
3826 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3827 break;
3828
3829 case Val_GNU_MIPS_ABI_FP_64A:
3830 case Val_GNU_MIPS_ABI_FP_64:
3831 if (mips_abi != O32_ABI)
3832 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3833 else if (file_mips_opts.soft_float)
3834 fpabi_incompatible_with (fpabi, "softfloat");
3835 else if (file_mips_opts.single_float)
3836 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3837 else if (file_mips_opts.fp != 64)
3838 fpabi_requires (fpabi, "fp=64");
3839 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3840 fpabi_incompatible_with (fpabi, "nooddspreg");
3841 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3842 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3843 break;
3844
3845 case Val_GNU_MIPS_ABI_FP_SINGLE:
3846 if (file_mips_opts.soft_float)
3847 fpabi_incompatible_with (fpabi, "softfloat");
3848 else if (!file_mips_opts.single_float)
3849 fpabi_requires (fpabi, "singlefloat");
3850 break;
3851
3852 case Val_GNU_MIPS_ABI_FP_SOFT:
3853 if (!file_mips_opts.soft_float)
3854 fpabi_requires (fpabi, "softfloat");
3855 break;
3856
3857 case Val_GNU_MIPS_ABI_FP_OLD_64:
3858 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3859 Tag_GNU_MIPS_ABI_FP, fpabi);
3860 break;
3861
3350cc01
CM
3862 case Val_GNU_MIPS_ABI_FP_NAN2008:
3863 /* Silently ignore compatibility value. */
3864 break;
3865
351cdf24
MF
3866 default:
3867 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3868 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3869 break;
3870 }
351cdf24
MF
3871}
3872
919731af 3873/* Perform consistency checks on the current options. */
3874
3875static void
3876mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3877{
3878 /* Check the size of integer registers agrees with the ABI and ISA. */
3879 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3880 as_bad (_("`gp=64' used with a 32-bit processor"));
3881 else if (abi_checks
3882 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3883 as_bad (_("`gp=32' used with a 64-bit ABI"));
3884 else if (abi_checks
3885 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3886 as_bad (_("`gp=64' used with a 32-bit ABI"));
3887
3888 /* Check the size of the float registers agrees with the ABI and ISA. */
3889 switch (opts->fp)
3890 {
351cdf24
MF
3891 case 0:
3892 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3893 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3894 else if (opts->single_float == 1)
3895 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3896 break;
919731af 3897 case 64:
3898 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3899 as_bad (_("`fp=64' used with a 32-bit fpu"));
3900 else if (abi_checks
3901 && ABI_NEEDS_32BIT_REGS (mips_abi)
3902 && !ISA_HAS_MXHC1 (opts->isa))
3903 as_warn (_("`fp=64' used with a 32-bit ABI"));
3904 break;
3905 case 32:
3906 if (abi_checks
3907 && ABI_NEEDS_64BIT_REGS (mips_abi))
3908 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3909 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3910 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3911 break;
3912 default:
3913 as_bad (_("Unknown size of floating point registers"));
3914 break;
3915 }
3916
351cdf24
MF
3917 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3918 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3919
919731af 3920 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3921 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3922 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3923 && (opts->micromips == 1
3924 || opts->mips16 == 1))
1357373c 3925 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3926 opts->micromips ? "micromips" : "mips16",
5f4678bb 3927 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3928
3929 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3930 as_fatal (_("branch relaxation is not supported in `%s'"),
3931 mips_cpu_info_from_isa (opts->isa)->name);
919731af 3932}
3933
3934/* Perform consistency checks on the module level options exactly once.
3935 This is a deferred check that happens:
3936 at the first .set directive
3937 or, at the first pseudo op that generates code (inc .dc.a)
3938 or, at the first instruction
3939 or, at the end. */
3940
3941static void
3942file_mips_check_options (void)
3943{
3944 const struct mips_cpu_info *arch_info = 0;
3945
3946 if (file_mips_opts_checked)
3947 return;
3948
3949 /* The following code determines the register size.
3950 Similar code was added to GCC 3.3 (see override_options() in
3951 config/mips/mips.c). The GAS and GCC code should be kept in sync
3952 as much as possible. */
3953
3954 if (file_mips_opts.gp < 0)
3955 {
3956 /* Infer the integer register size from the ABI and processor.
3957 Restrict ourselves to 32-bit registers if that's all the
3958 processor has, or if the ABI cannot handle 64-bit registers. */
3959 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3960 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3961 ? 32 : 64;
3962 }
3963
3964 if (file_mips_opts.fp < 0)
3965 {
3966 /* No user specified float register size.
3967 ??? GAS treats single-float processors as though they had 64-bit
3968 float registers (although it complains when double-precision
3969 instructions are used). As things stand, saying they have 32-bit
3970 registers would lead to spurious "register must be even" messages.
3971 So here we assume float registers are never smaller than the
3972 integer ones. */
3973 if (file_mips_opts.gp == 64)
3974 /* 64-bit integer registers implies 64-bit float registers. */
3975 file_mips_opts.fp = 64;
3976 else if ((file_mips_opts.ase & FP64_ASES)
3977 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3978 /* Handle ASEs that require 64-bit float registers, if possible. */
3979 file_mips_opts.fp = 64;
7361da2c
AB
3980 else if (ISA_IS_R6 (mips_opts.isa))
3981 /* R6 implies 64-bit float registers. */
3982 file_mips_opts.fp = 64;
919731af 3983 else
3984 /* 32-bit float registers. */
3985 file_mips_opts.fp = 32;
3986 }
3987
3988 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3989
351cdf24
MF
3990 /* Disable operations on odd-numbered floating-point registers by default
3991 when using the FPXX ABI. */
3992 if (file_mips_opts.oddspreg < 0)
3993 {
3994 if (file_mips_opts.fp == 0)
3995 file_mips_opts.oddspreg = 0;
3996 else
3997 file_mips_opts.oddspreg = 1;
3998 }
3999
919731af 4000 /* End of GCC-shared inference code. */
4001
4002 /* This flag is set when we have a 64-bit capable CPU but use only
4003 32-bit wide registers. Note that EABI does not use it. */
4004 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4005 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4006 || mips_abi == O32_ABI))
4007 mips_32bitmode = 1;
4008
4009 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4010 as_bad (_("trap exception not supported at ISA 1"));
4011
4012 /* If the selected architecture includes support for ASEs, enable
4013 generation of code for them. */
4014 if (file_mips_opts.mips16 == -1)
4015 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4016 if (file_mips_opts.micromips == -1)
4017 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4018 ? 1 : 0;
4019
7361da2c
AB
4020 if (mips_nan2008 == -1)
4021 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4022 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4023 as_fatal (_("`%s' does not support legacy NaN"),
4024 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4025
919731af 4026 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4027 being selected implicitly. */
4028 if (file_mips_opts.fp != 64)
4029 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4030
4031 /* If the user didn't explicitly select or deselect a particular ASE,
4032 use the default setting for the CPU. */
4033 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4034
4035 /* Set up the current options. These may change throughout assembly. */
4036 mips_opts = file_mips_opts;
4037
4038 mips_check_isa_supports_ases ();
4039 mips_check_options (&file_mips_opts, TRUE);
4040 file_mips_opts_checked = TRUE;
4041
4042 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4043 as_warn (_("could not set architecture and machine"));
4044}
4045
252b5132 4046void
17a2f251 4047md_assemble (char *str)
252b5132
RH
4048{
4049 struct mips_cl_insn insn;
f6688943
TS
4050 bfd_reloc_code_real_type unused_reloc[3]
4051 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4052
919731af 4053 file_mips_check_options ();
4054
252b5132 4055 imm_expr.X_op = O_absent;
252b5132 4056 offset_expr.X_op = O_absent;
f6688943
TS
4057 offset_reloc[0] = BFD_RELOC_UNUSED;
4058 offset_reloc[1] = BFD_RELOC_UNUSED;
4059 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4060
e1b47bd5
RS
4061 mips_mark_labels ();
4062 mips_assembling_insn = TRUE;
e3de51ce 4063 clear_insn_error ();
e1b47bd5 4064
252b5132
RH
4065 if (mips_opts.mips16)
4066 mips16_ip (str, &insn);
4067 else
4068 {
4069 mips_ip (str, &insn);
beae10d5
KH
4070 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4071 str, insn.insn_opcode));
252b5132
RH
4072 }
4073
e3de51ce
RS
4074 if (insn_error.msg)
4075 report_insn_error (str);
e1b47bd5 4076 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4077 {
584892a6 4078 macro_start ();
252b5132
RH
4079 if (mips_opts.mips16)
4080 mips16_macro (&insn);
4081 else
833794fc 4082 macro (&insn, str);
584892a6 4083 macro_end ();
252b5132
RH
4084 }
4085 else
4086 {
77bd4346 4087 if (offset_expr.X_op != O_absent)
df58fc94 4088 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4089 else
df58fc94 4090 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4091 }
e1b47bd5
RS
4092
4093 mips_assembling_insn = FALSE;
252b5132
RH
4094}
4095
738e5348
RS
4096/* Convenience functions for abstracting away the differences between
4097 MIPS16 and non-MIPS16 relocations. */
4098
4099static inline bfd_boolean
4100mips16_reloc_p (bfd_reloc_code_real_type reloc)
4101{
4102 switch (reloc)
4103 {
4104 case BFD_RELOC_MIPS16_JMP:
4105 case BFD_RELOC_MIPS16_GPREL:
4106 case BFD_RELOC_MIPS16_GOT16:
4107 case BFD_RELOC_MIPS16_CALL16:
4108 case BFD_RELOC_MIPS16_HI16_S:
4109 case BFD_RELOC_MIPS16_HI16:
4110 case BFD_RELOC_MIPS16_LO16:
c9775dde 4111 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4112 return TRUE;
4113
4114 default:
4115 return FALSE;
4116 }
4117}
4118
df58fc94
RS
4119static inline bfd_boolean
4120micromips_reloc_p (bfd_reloc_code_real_type reloc)
4121{
4122 switch (reloc)
4123 {
4124 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4125 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4126 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4127 case BFD_RELOC_MICROMIPS_GPREL16:
4128 case BFD_RELOC_MICROMIPS_JMP:
4129 case BFD_RELOC_MICROMIPS_HI16:
4130 case BFD_RELOC_MICROMIPS_HI16_S:
4131 case BFD_RELOC_MICROMIPS_LO16:
4132 case BFD_RELOC_MICROMIPS_LITERAL:
4133 case BFD_RELOC_MICROMIPS_GOT16:
4134 case BFD_RELOC_MICROMIPS_CALL16:
4135 case BFD_RELOC_MICROMIPS_GOT_HI16:
4136 case BFD_RELOC_MICROMIPS_GOT_LO16:
4137 case BFD_RELOC_MICROMIPS_CALL_HI16:
4138 case BFD_RELOC_MICROMIPS_CALL_LO16:
4139 case BFD_RELOC_MICROMIPS_SUB:
4140 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4141 case BFD_RELOC_MICROMIPS_GOT_OFST:
4142 case BFD_RELOC_MICROMIPS_GOT_DISP:
4143 case BFD_RELOC_MICROMIPS_HIGHEST:
4144 case BFD_RELOC_MICROMIPS_HIGHER:
4145 case BFD_RELOC_MICROMIPS_SCN_DISP:
4146 case BFD_RELOC_MICROMIPS_JALR:
4147 return TRUE;
4148
4149 default:
4150 return FALSE;
4151 }
4152}
4153
2309ddf2
MR
4154static inline bfd_boolean
4155jmp_reloc_p (bfd_reloc_code_real_type reloc)
4156{
4157 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4158}
4159
0e9c5a5c
MR
4160static inline bfd_boolean
4161b_reloc_p (bfd_reloc_code_real_type reloc)
4162{
4163 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4164 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4165 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4166 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4167 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4168 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4169 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4170}
4171
738e5348
RS
4172static inline bfd_boolean
4173got16_reloc_p (bfd_reloc_code_real_type reloc)
4174{
2309ddf2 4175 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4176 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4177}
4178
4179static inline bfd_boolean
4180hi16_reloc_p (bfd_reloc_code_real_type reloc)
4181{
2309ddf2 4182 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4183 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4184}
4185
4186static inline bfd_boolean
4187lo16_reloc_p (bfd_reloc_code_real_type reloc)
4188{
2309ddf2 4189 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4190 || reloc == BFD_RELOC_MICROMIPS_LO16);
4191}
4192
df58fc94
RS
4193static inline bfd_boolean
4194jalr_reloc_p (bfd_reloc_code_real_type reloc)
4195{
2309ddf2 4196 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4197}
4198
f2ae14a1
RS
4199static inline bfd_boolean
4200gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4201{
4202 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4203 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4204}
4205
2de39019
CM
4206/* Return true if RELOC is a PC-relative relocation that does not have
4207 full address range. */
4208
4209static inline bfd_boolean
4210limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4211{
4212 switch (reloc)
4213 {
4214 case BFD_RELOC_16_PCREL_S2:
c9775dde 4215 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4216 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4217 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4218 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4219 case BFD_RELOC_MIPS_21_PCREL_S2:
4220 case BFD_RELOC_MIPS_26_PCREL_S2:
4221 case BFD_RELOC_MIPS_18_PCREL_S3:
4222 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4223 return TRUE;
4224
b47468a6 4225 case BFD_RELOC_32_PCREL:
7361da2c
AB
4226 case BFD_RELOC_HI16_S_PCREL:
4227 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4228 return HAVE_64BIT_ADDRESSES;
4229
2de39019
CM
4230 default:
4231 return FALSE;
4232 }
4233}
b47468a6 4234
5919d012 4235/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4236 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4237 need a matching %lo() when applied to local symbols. */
5919d012
RS
4238
4239static inline bfd_boolean
17a2f251 4240reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4241{
3b91255e 4242 return (HAVE_IN_PLACE_ADDENDS
738e5348 4243 && (hi16_reloc_p (reloc)
0a44bf69
RS
4244 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4245 all GOT16 relocations evaluate to "G". */
738e5348
RS
4246 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4247}
4248
4249/* Return the type of %lo() reloc needed by RELOC, given that
4250 reloc_needs_lo_p. */
4251
4252static inline bfd_reloc_code_real_type
4253matching_lo_reloc (bfd_reloc_code_real_type reloc)
4254{
df58fc94
RS
4255 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4256 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4257 : BFD_RELOC_LO16));
5919d012
RS
4258}
4259
4260/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4261 relocation. */
4262
4263static inline bfd_boolean
17a2f251 4264fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4265{
4266 return (fixp->fx_next != NULL
738e5348 4267 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4268 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4269 && fixp->fx_offset == fixp->fx_next->fx_offset);
4270}
4271
462427c4
RS
4272/* Move all labels in LABELS to the current insertion point. TEXT_P
4273 says whether the labels refer to text or data. */
404a8071
RS
4274
4275static void
462427c4 4276mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4277{
4278 struct insn_label_list *l;
4279 valueT val;
4280
462427c4 4281 for (l = labels; l != NULL; l = l->next)
404a8071 4282 {
9c2799c2 4283 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4284 symbol_set_frag (l->label, frag_now);
4285 val = (valueT) frag_now_fix ();
df58fc94 4286 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4287 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4288 ++val;
4289 S_SET_VALUE (l->label, val);
4290 }
4291}
4292
462427c4
RS
4293/* Move all labels in insn_labels to the current insertion point
4294 and treat them as text labels. */
4295
4296static void
4297mips_move_text_labels (void)
4298{
4299 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4300}
4301
9e009953
MR
4302/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4303
5f0fe04b
TS
4304static bfd_boolean
4305s_is_linkonce (symbolS *sym, segT from_seg)
4306{
4307 bfd_boolean linkonce = FALSE;
4308 segT symseg = S_GET_SEGMENT (sym);
4309
4310 if (symseg != from_seg && !S_IS_LOCAL (sym))
4311 {
4312 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4313 linkonce = TRUE;
5f0fe04b
TS
4314 /* The GNU toolchain uses an extension for ELF: a section
4315 beginning with the magic string .gnu.linkonce is a
4316 linkonce section. */
4317 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4318 sizeof ".gnu.linkonce" - 1) == 0)
4319 linkonce = TRUE;
5f0fe04b
TS
4320 }
4321 return linkonce;
4322}
4323
e1b47bd5 4324/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4325 linker to handle them specially, such as generating jalx instructions
4326 when needed. We also make them odd for the duration of the assembly,
4327 in order to generate the right sort of code. We will make them even
252b5132
RH
4328 in the adjust_symtab routine, while leaving them marked. This is
4329 convenient for the debugger and the disassembler. The linker knows
4330 to make them odd again. */
4331
4332static void
e1b47bd5 4333mips_compressed_mark_label (symbolS *label)
252b5132 4334{
df58fc94 4335 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4336
f3ded42a
RS
4337 if (mips_opts.mips16)
4338 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4339 else
4340 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4341 if ((S_GET_VALUE (label) & 1) == 0
4342 /* Don't adjust the address if the label is global or weak, or
4343 in a link-once section, since we'll be emitting symbol reloc
4344 references to it which will be patched up by the linker, and
4345 the final value of the symbol may or may not be MIPS16/microMIPS. */
4346 && !S_IS_WEAK (label)
4347 && !S_IS_EXTERNAL (label)
4348 && !s_is_linkonce (label, now_seg))
4349 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4350}
4351
4352/* Mark preceding MIPS16 or microMIPS instruction labels. */
4353
4354static void
4355mips_compressed_mark_labels (void)
4356{
4357 struct insn_label_list *l;
4358
4359 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4360 mips_compressed_mark_label (l->label);
252b5132
RH
4361}
4362
4d7206a2
RS
4363/* End the current frag. Make it a variant frag and record the
4364 relaxation info. */
4365
4366static void
4367relax_close_frag (void)
4368{
584892a6 4369 mips_macro_warning.first_frag = frag_now;
4d7206a2 4370 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4371 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4372 mips_pic != NO_PIC),
4d7206a2
RS
4373 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4374
4375 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4376 mips_relax.first_fixup = 0;
4377}
4378
4379/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4380 See the comment above RELAX_ENCODE for more details. */
4381
4382static void
4383relax_start (symbolS *symbol)
4384{
9c2799c2 4385 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4386 mips_relax.sequence = 1;
4387 mips_relax.symbol = symbol;
4388}
4389
4390/* Start generating the second version of a relaxable sequence.
4391 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4392
4393static void
4d7206a2
RS
4394relax_switch (void)
4395{
9c2799c2 4396 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4397 mips_relax.sequence = 2;
4398}
4399
4400/* End the current relaxable sequence. */
4401
4402static void
4403relax_end (void)
4404{
9c2799c2 4405 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4406 relax_close_frag ();
4407 mips_relax.sequence = 0;
4408}
4409
11625dd8
RS
4410/* Return true if IP is a delayed branch or jump. */
4411
4412static inline bfd_boolean
4413delayed_branch_p (const struct mips_cl_insn *ip)
4414{
4415 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4416 | INSN_COND_BRANCH_DELAY
4417 | INSN_COND_BRANCH_LIKELY)) != 0;
4418}
4419
4420/* Return true if IP is a compact branch or jump. */
4421
4422static inline bfd_boolean
4423compact_branch_p (const struct mips_cl_insn *ip)
4424{
26545944
RS
4425 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4426 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4427}
4428
4429/* Return true if IP is an unconditional branch or jump. */
4430
4431static inline bfd_boolean
4432uncond_branch_p (const struct mips_cl_insn *ip)
4433{
4434 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4435 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4436}
4437
4438/* Return true if IP is a branch-likely instruction. */
4439
4440static inline bfd_boolean
4441branch_likely_p (const struct mips_cl_insn *ip)
4442{
4443 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4444}
4445
14fe068b
RS
4446/* Return the type of nop that should be used to fill the delay slot
4447 of delayed branch IP. */
4448
4449static struct mips_cl_insn *
4450get_delay_slot_nop (const struct mips_cl_insn *ip)
4451{
4452 if (mips_opts.micromips
4453 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4454 return &micromips_nop32_insn;
4455 return NOP_INSN;
4456}
4457
fc76e730
RS
4458/* Return a mask that has bit N set if OPCODE reads the register(s)
4459 in operand N. */
df58fc94
RS
4460
4461static unsigned int
fc76e730 4462insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4463{
fc76e730
RS
4464 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4465}
df58fc94 4466
fc76e730
RS
4467/* Return a mask that has bit N set if OPCODE writes to the register(s)
4468 in operand N. */
4469
4470static unsigned int
4471insn_write_mask (const struct mips_opcode *opcode)
4472{
4473 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4474}
4475
4476/* Return a mask of the registers specified by operand OPERAND of INSN.
4477 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4478 is set. */
4479
4480static unsigned int
4481operand_reg_mask (const struct mips_cl_insn *insn,
4482 const struct mips_operand *operand,
4483 unsigned int type_mask)
4484{
4485 unsigned int uval, vsel;
4486
4487 switch (operand->type)
df58fc94 4488 {
fc76e730
RS
4489 case OP_INT:
4490 case OP_MAPPED_INT:
4491 case OP_MSB:
4492 case OP_PCREL:
4493 case OP_PERF_REG:
4494 case OP_ADDIUSP_INT:
4495 case OP_ENTRY_EXIT_LIST:
4496 case OP_REPEAT_DEST_REG:
4497 case OP_REPEAT_PREV_REG:
4498 case OP_PC:
14daeee3
RS
4499 case OP_VU0_SUFFIX:
4500 case OP_VU0_MATCH_SUFFIX:
56d438b1 4501 case OP_IMM_INDEX:
fc76e730
RS
4502 abort ();
4503
4504 case OP_REG:
0f35dbc4 4505 case OP_OPTIONAL_REG:
fc76e730
RS
4506 {
4507 const struct mips_reg_operand *reg_op;
4508
4509 reg_op = (const struct mips_reg_operand *) operand;
4510 if (!(type_mask & (1 << reg_op->reg_type)))
4511 return 0;
4512 uval = insn_extract_operand (insn, operand);
4513 return 1 << mips_decode_reg_operand (reg_op, uval);
4514 }
4515
4516 case OP_REG_PAIR:
4517 {
4518 const struct mips_reg_pair_operand *pair_op;
4519
4520 pair_op = (const struct mips_reg_pair_operand *) operand;
4521 if (!(type_mask & (1 << pair_op->reg_type)))
4522 return 0;
4523 uval = insn_extract_operand (insn, operand);
4524 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4525 }
4526
4527 case OP_CLO_CLZ_DEST:
4528 if (!(type_mask & (1 << OP_REG_GP)))
4529 return 0;
4530 uval = insn_extract_operand (insn, operand);
4531 return (1 << (uval & 31)) | (1 << (uval >> 5));
4532
7361da2c
AB
4533 case OP_SAME_RS_RT:
4534 if (!(type_mask & (1 << OP_REG_GP)))
4535 return 0;
4536 uval = insn_extract_operand (insn, operand);
4537 gas_assert ((uval & 31) == (uval >> 5));
4538 return 1 << (uval & 31);
4539
4540 case OP_CHECK_PREV:
4541 case OP_NON_ZERO_REG:
4542 if (!(type_mask & (1 << OP_REG_GP)))
4543 return 0;
4544 uval = insn_extract_operand (insn, operand);
4545 return 1 << (uval & 31);
4546
fc76e730
RS
4547 case OP_LWM_SWM_LIST:
4548 abort ();
4549
4550 case OP_SAVE_RESTORE_LIST:
4551 abort ();
4552
4553 case OP_MDMX_IMM_REG:
4554 if (!(type_mask & (1 << OP_REG_VEC)))
4555 return 0;
4556 uval = insn_extract_operand (insn, operand);
4557 vsel = uval >> 5;
4558 if ((vsel & 0x18) == 0x18)
4559 return 0;
4560 return 1 << (uval & 31);
56d438b1
CF
4561
4562 case OP_REG_INDEX:
4563 if (!(type_mask & (1 << OP_REG_GP)))
4564 return 0;
4565 return 1 << insn_extract_operand (insn, operand);
df58fc94 4566 }
fc76e730
RS
4567 abort ();
4568}
4569
4570/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4571 where bit N of OPNO_MASK is set if operand N should be included.
4572 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4573 is set. */
4574
4575static unsigned int
4576insn_reg_mask (const struct mips_cl_insn *insn,
4577 unsigned int type_mask, unsigned int opno_mask)
4578{
4579 unsigned int opno, reg_mask;
4580
4581 opno = 0;
4582 reg_mask = 0;
4583 while (opno_mask != 0)
4584 {
4585 if (opno_mask & 1)
4586 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4587 opno_mask >>= 1;
4588 opno += 1;
4589 }
4590 return reg_mask;
df58fc94
RS
4591}
4592
4c260379
RS
4593/* Return the mask of core registers that IP reads. */
4594
4595static unsigned int
4596gpr_read_mask (const struct mips_cl_insn *ip)
4597{
4598 unsigned long pinfo, pinfo2;
4599 unsigned int mask;
4600
fc76e730 4601 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4602 pinfo = ip->insn_mo->pinfo;
4603 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4604 if (pinfo & INSN_UDI)
4c260379 4605 {
fc76e730
RS
4606 /* UDI instructions have traditionally been assumed to read RS
4607 and RT. */
4608 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4609 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4610 }
fc76e730
RS
4611 if (pinfo & INSN_READ_GPR_24)
4612 mask |= 1 << 24;
4613 if (pinfo2 & INSN2_READ_GPR_16)
4614 mask |= 1 << 16;
4615 if (pinfo2 & INSN2_READ_SP)
4616 mask |= 1 << SP;
26545944 4617 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4618 mask |= 1 << 31;
fe35f09f
RS
4619 /* Don't include register 0. */
4620 return mask & ~1;
4c260379
RS
4621}
4622
4623/* Return the mask of core registers that IP writes. */
4624
4625static unsigned int
4626gpr_write_mask (const struct mips_cl_insn *ip)
4627{
4628 unsigned long pinfo, pinfo2;
4629 unsigned int mask;
4630
fc76e730 4631 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4632 pinfo = ip->insn_mo->pinfo;
4633 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4634 if (pinfo & INSN_WRITE_GPR_24)
4635 mask |= 1 << 24;
4636 if (pinfo & INSN_WRITE_GPR_31)
4637 mask |= 1 << 31;
4638 if (pinfo & INSN_UDI)
4639 /* UDI instructions have traditionally been assumed to write to RD. */
4640 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4641 if (pinfo2 & INSN2_WRITE_SP)
4642 mask |= 1 << SP;
fe35f09f
RS
4643 /* Don't include register 0. */
4644 return mask & ~1;
4c260379
RS
4645}
4646
4647/* Return the mask of floating-point registers that IP reads. */
4648
4649static unsigned int
4650fpr_read_mask (const struct mips_cl_insn *ip)
4651{
fc76e730 4652 unsigned long pinfo;
4c260379
RS
4653 unsigned int mask;
4654
9d5de888
CF
4655 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4656 | (1 << OP_REG_MSA)),
fc76e730 4657 insn_read_mask (ip->insn_mo));
4c260379 4658 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4659 /* Conservatively treat all operands to an FP_D instruction are doubles.
4660 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4661 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4662 mask |= mask << 1;
4663 return mask;
4664}
4665
4666/* Return the mask of floating-point registers that IP writes. */
4667
4668static unsigned int
4669fpr_write_mask (const struct mips_cl_insn *ip)
4670{
fc76e730 4671 unsigned long pinfo;
4c260379
RS
4672 unsigned int mask;
4673
9d5de888
CF
4674 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4675 | (1 << OP_REG_MSA)),
fc76e730 4676 insn_write_mask (ip->insn_mo));
4c260379 4677 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4678 /* Conservatively treat all operands to an FP_D instruction are doubles.
4679 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4680 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4681 mask |= mask << 1;
4682 return mask;
4683}
4684
a1d78564
RS
4685/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4686 Check whether that is allowed. */
4687
4688static bfd_boolean
4689mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4690{
4691 const char *s = insn->name;
351cdf24
MF
4692 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4693 || FPR_SIZE == 64)
4694 && mips_opts.oddspreg;
a1d78564
RS
4695
4696 if (insn->pinfo == INSN_MACRO)
4697 /* Let a macro pass, we'll catch it later when it is expanded. */
4698 return TRUE;
4699
351cdf24
MF
4700 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4701 otherwise it depends on oddspreg. */
4702 if ((insn->pinfo & FP_S)
4703 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4704 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4705 return FPR_SIZE == 32 || oddspreg;
a1d78564 4706
351cdf24
MF
4707 /* Allow odd registers for single-precision ops and double-precision if the
4708 floating-point registers are 64-bit wide. */
4709 switch (insn->pinfo & (FP_S | FP_D))
4710 {
4711 case FP_S:
4712 case 0:
4713 return oddspreg;
4714 case FP_D:
4715 return FPR_SIZE == 64;
4716 default:
4717 break;
a1d78564
RS
4718 }
4719
351cdf24
MF
4720 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4721 s = strchr (insn->name, '.');
4722 if (s != NULL && opnum == 2)
4723 s = strchr (s + 1, '.');
4724 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4725 return oddspreg;
a1d78564 4726
351cdf24 4727 return FPR_SIZE == 64;
a1d78564
RS
4728}
4729
a1d78564
RS
4730/* Information about an instruction argument that we're trying to match. */
4731struct mips_arg_info
4732{
4733 /* The instruction so far. */
4734 struct mips_cl_insn *insn;
4735
a92713e6
RS
4736 /* The first unconsumed operand token. */
4737 struct mips_operand_token *token;
4738
a1d78564
RS
4739 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4740 int opnum;
4741
4742 /* The 1-based argument number, for error reporting. This does not
4743 count elided optional registers, etc.. */
4744 int argnum;
4745
4746 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4747 unsigned int last_regno;
4748
4749 /* If the first operand was an OP_REG, this is the register that it
4750 specified, otherwise it is ILLEGAL_REG. */
4751 unsigned int dest_regno;
4752
4753 /* The value of the last OP_INT operand. Only used for OP_MSB,
4754 where it gives the lsb position. */
4755 unsigned int last_op_int;
4756
60f20e8b 4757 /* If true, match routines should assume that no later instruction
2b0f3761 4758 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4759 possible. Match routines should not report errors if something
4760 is only invalid for !LAX_MATCH. */
4761 bfd_boolean lax_match;
a1d78564 4762
a1d78564
RS
4763 /* True if a reference to the current AT register was seen. */
4764 bfd_boolean seen_at;
4765};
4766
1a00e612
RS
4767/* Record that the argument is out of range. */
4768
4769static void
4770match_out_of_range (struct mips_arg_info *arg)
4771{
4772 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4773}
4774
4775/* Record that the argument isn't constant but needs to be. */
4776
4777static void
4778match_not_constant (struct mips_arg_info *arg)
4779{
4780 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4781 arg->argnum);
4782}
4783
a92713e6
RS
4784/* Try to match an OT_CHAR token for character CH. Consume the token
4785 and return true on success, otherwise return false. */
a1d78564 4786
a92713e6
RS
4787static bfd_boolean
4788match_char (struct mips_arg_info *arg, char ch)
a1d78564 4789{
a92713e6
RS
4790 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4791 {
4792 ++arg->token;
4793 if (ch == ',')
4794 arg->argnum += 1;
4795 return TRUE;
4796 }
4797 return FALSE;
4798}
a1d78564 4799
a92713e6
RS
4800/* Try to get an expression from the next tokens in ARG. Consume the
4801 tokens and return true on success, storing the expression value in
4802 VALUE and relocation types in R. */
4803
4804static bfd_boolean
4805match_expression (struct mips_arg_info *arg, expressionS *value,
4806 bfd_reloc_code_real_type *r)
4807{
d436c1c2
RS
4808 /* If the next token is a '(' that was parsed as being part of a base
4809 expression, assume we have an elided offset. The later match will fail
4810 if this turns out to be wrong. */
4811 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4812 {
d436c1c2
RS
4813 value->X_op = O_constant;
4814 value->X_add_number = 0;
4815 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4816 return TRUE;
4817 }
4818
d436c1c2
RS
4819 /* Reject register-based expressions such as "0+$2" and "(($2))".
4820 For plain registers the default error seems more appropriate. */
4821 if (arg->token->type == OT_INTEGER
4822 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4823 {
d436c1c2
RS
4824 set_insn_error (arg->argnum, _("register value used as expression"));
4825 return FALSE;
a1d78564 4826 }
d436c1c2
RS
4827
4828 if (arg->token->type == OT_INTEGER)
a92713e6 4829 {
d436c1c2
RS
4830 *value = arg->token->u.integer.value;
4831 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4832 ++arg->token;
4833 return TRUE;
a92713e6 4834 }
a92713e6 4835
d436c1c2
RS
4836 set_insn_error_i
4837 (arg->argnum, _("operand %d must be an immediate expression"),
4838 arg->argnum);
4839 return FALSE;
a92713e6
RS
4840}
4841
4842/* Try to get a constant expression from the next tokens in ARG. Consume
4843 the tokens and return return true on success, storing the constant value
a54d5f8b 4844 in *VALUE. */
a92713e6
RS
4845
4846static bfd_boolean
1a00e612 4847match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4848{
4849 expressionS ex;
4850 bfd_reloc_code_real_type r[3];
a1d78564 4851
a92713e6
RS
4852 if (!match_expression (arg, &ex, r))
4853 return FALSE;
4854
4855 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4856 *value = ex.X_add_number;
4857 else
4858 {
1a00e612
RS
4859 match_not_constant (arg);
4860 return FALSE;
a1d78564 4861 }
a92713e6 4862 return TRUE;
a1d78564
RS
4863}
4864
4865/* Return the RTYPE_* flags for a register operand of type TYPE that
4866 appears in instruction OPCODE. */
4867
4868static unsigned int
4869convert_reg_type (const struct mips_opcode *opcode,
4870 enum mips_reg_operand_type type)
4871{
4872 switch (type)
4873 {
4874 case OP_REG_GP:
4875 return RTYPE_NUM | RTYPE_GP;
4876
4877 case OP_REG_FP:
4878 /* Allow vector register names for MDMX if the instruction is a 64-bit
4879 FPR load, store or move (including moves to and from GPRs). */
4880 if ((mips_opts.ase & ASE_MDMX)
4881 && (opcode->pinfo & FP_D)
43885403 4882 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4883 | INSN_COPROC_MEMORY_DELAY
43885403 4884 | INSN_LOAD_COPROC
67dc82bc 4885 | INSN_LOAD_MEMORY
a1d78564
RS
4886 | INSN_STORE_MEMORY)))
4887 return RTYPE_FPU | RTYPE_VEC;
4888 return RTYPE_FPU;
4889
4890 case OP_REG_CCC:
4891 if (opcode->pinfo & (FP_D | FP_S))
4892 return RTYPE_CCC | RTYPE_FCC;
4893 return RTYPE_CCC;
4894
4895 case OP_REG_VEC:
4896 if (opcode->membership & INSN_5400)
4897 return RTYPE_FPU;
4898 return RTYPE_FPU | RTYPE_VEC;
4899
4900 case OP_REG_ACC:
4901 return RTYPE_ACC;
4902
4903 case OP_REG_COPRO:
4904 if (opcode->name[strlen (opcode->name) - 1] == '0')
4905 return RTYPE_NUM | RTYPE_CP0;
4906 return RTYPE_NUM;
4907
4908 case OP_REG_HW:
4909 return RTYPE_NUM;
14daeee3
RS
4910
4911 case OP_REG_VI:
4912 return RTYPE_NUM | RTYPE_VI;
4913
4914 case OP_REG_VF:
4915 return RTYPE_NUM | RTYPE_VF;
4916
4917 case OP_REG_R5900_I:
4918 return RTYPE_R5900_I;
4919
4920 case OP_REG_R5900_Q:
4921 return RTYPE_R5900_Q;
4922
4923 case OP_REG_R5900_R:
4924 return RTYPE_R5900_R;
4925
4926 case OP_REG_R5900_ACC:
4927 return RTYPE_R5900_ACC;
56d438b1
CF
4928
4929 case OP_REG_MSA:
4930 return RTYPE_MSA;
4931
4932 case OP_REG_MSA_CTRL:
4933 return RTYPE_NUM;
a1d78564
RS
4934 }
4935 abort ();
4936}
4937
4938/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4939
4940static void
4941check_regno (struct mips_arg_info *arg,
4942 enum mips_reg_operand_type type, unsigned int regno)
4943{
4944 if (AT && type == OP_REG_GP && regno == AT)
4945 arg->seen_at = TRUE;
4946
4947 if (type == OP_REG_FP
4948 && (regno & 1) != 0
a1d78564 4949 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
4950 {
4951 /* This was a warning prior to introducing O32 FPXX and FP64 support
4952 so maintain a warning for FP32 but raise an error for the new
4953 cases. */
4954 if (FPR_SIZE == 32)
4955 as_warn (_("float register should be even, was %d"), regno);
4956 else
4957 as_bad (_("float register should be even, was %d"), regno);
4958 }
a1d78564
RS
4959
4960 if (type == OP_REG_CCC)
4961 {
4962 const char *name;
4963 size_t length;
4964
4965 name = arg->insn->insn_mo->name;
4966 length = strlen (name);
4967 if ((regno & 1) != 0
4968 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4969 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 4970 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
4971 name, regno);
4972
4973 if ((regno & 3) != 0
4974 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 4975 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
4976 name, regno);
4977 }
4978}
4979
a92713e6
RS
4980/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4981 a register of type TYPE. Return true on success, storing the register
4982 number in *REGNO and warning about any dubious uses. */
4983
4984static bfd_boolean
4985match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4986 unsigned int symval, unsigned int *regno)
4987{
4988 if (type == OP_REG_VEC)
4989 symval = mips_prefer_vec_regno (symval);
4990 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4991 return FALSE;
4992
4993 *regno = symval & RNUM_MASK;
4994 check_regno (arg, type, *regno);
4995 return TRUE;
4996}
4997
4998/* Try to interpret the next token in ARG as a register of type TYPE.
4999 Consume the token and return true on success, storing the register
5000 number in *REGNO. Return false on failure. */
5001
5002static bfd_boolean
5003match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5004 unsigned int *regno)
5005{
5006 if (arg->token->type == OT_REG
5007 && match_regno (arg, type, arg->token->u.regno, regno))
5008 {
5009 ++arg->token;
5010 return TRUE;
5011 }
5012 return FALSE;
5013}
5014
5015/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5016 Consume the token and return true on success, storing the register numbers
5017 in *REGNO1 and *REGNO2. Return false on failure. */
5018
5019static bfd_boolean
5020match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5021 unsigned int *regno1, unsigned int *regno2)
5022{
5023 if (match_reg (arg, type, regno1))
5024 {
5025 *regno2 = *regno1;
5026 return TRUE;
5027 }
5028 if (arg->token->type == OT_REG_RANGE
5029 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5030 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5031 && *regno1 <= *regno2)
5032 {
5033 ++arg->token;
5034 return TRUE;
5035 }
5036 return FALSE;
5037}
5038
a1d78564
RS
5039/* OP_INT matcher. */
5040
a92713e6 5041static bfd_boolean
a1d78564 5042match_int_operand (struct mips_arg_info *arg,
a92713e6 5043 const struct mips_operand *operand_base)
a1d78564
RS
5044{
5045 const struct mips_int_operand *operand;
3ccad066 5046 unsigned int uval;
a1d78564
RS
5047 int min_val, max_val, factor;
5048 offsetT sval;
a1d78564
RS
5049
5050 operand = (const struct mips_int_operand *) operand_base;
5051 factor = 1 << operand->shift;
3ccad066
RS
5052 min_val = mips_int_operand_min (operand);
5053 max_val = mips_int_operand_max (operand);
a1d78564 5054
d436c1c2
RS
5055 if (operand_base->lsb == 0
5056 && operand_base->size == 16
5057 && operand->shift == 0
5058 && operand->bias == 0
5059 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5060 {
5061 /* The operand can be relocated. */
a92713e6
RS
5062 if (!match_expression (arg, &offset_expr, offset_reloc))
5063 return FALSE;
5064
5065 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5066 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5067 leave the relocation value in offset_expr and offset_relocs
5068 for the caller to process. */
a92713e6
RS
5069 return TRUE;
5070
5071 if (offset_expr.X_op != O_constant)
a1d78564 5072 {
60f20e8b
RS
5073 /* Accept non-constant operands if no later alternative matches,
5074 leaving it for the caller to process. */
5075 if (!arg->lax_match)
5076 return FALSE;
a92713e6
RS
5077 offset_reloc[0] = BFD_RELOC_LO16;
5078 return TRUE;
a1d78564 5079 }
a92713e6 5080
a1d78564
RS
5081 /* Clear the global state; we're going to install the operand
5082 ourselves. */
a92713e6 5083 sval = offset_expr.X_add_number;
a1d78564 5084 offset_expr.X_op = O_absent;
60f20e8b
RS
5085
5086 /* For compatibility with older assemblers, we accept
5087 0x8000-0xffff as signed 16-bit numbers when only
5088 signed numbers are allowed. */
5089 if (sval > max_val)
5090 {
5091 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5092 if (!arg->lax_match && sval <= max_val)
5093 return FALSE;
5094 }
a1d78564
RS
5095 }
5096 else
5097 {
1a00e612 5098 if (!match_const_int (arg, &sval))
a92713e6 5099 return FALSE;
a1d78564
RS
5100 }
5101
5102 arg->last_op_int = sval;
5103
1a00e612 5104 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5105 {
1a00e612
RS
5106 match_out_of_range (arg);
5107 return FALSE;
a1d78564
RS
5108 }
5109
5110 uval = (unsigned int) sval >> operand->shift;
5111 uval -= operand->bias;
5112
5113 /* Handle -mfix-cn63xxp1. */
5114 if (arg->opnum == 1
5115 && mips_fix_cn63xxp1
5116 && !mips_opts.micromips
5117 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5118 switch (uval)
5119 {
5120 case 5:
5121 case 25:
5122 case 26:
5123 case 27:
5124 case 28:
5125 case 29:
5126 case 30:
5127 case 31:
5128 /* These are ok. */
5129 break;
5130
5131 default:
5132 /* The rest must be changed to 28. */
5133 uval = 28;
5134 break;
5135 }
5136
5137 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5138 return TRUE;
a1d78564
RS
5139}
5140
5141/* OP_MAPPED_INT matcher. */
5142
a92713e6 5143static bfd_boolean
a1d78564 5144match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5145 const struct mips_operand *operand_base)
a1d78564
RS
5146{
5147 const struct mips_mapped_int_operand *operand;
5148 unsigned int uval, num_vals;
5149 offsetT sval;
5150
5151 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5152 if (!match_const_int (arg, &sval))
a92713e6 5153 return FALSE;
a1d78564
RS
5154
5155 num_vals = 1 << operand_base->size;
5156 for (uval = 0; uval < num_vals; uval++)
5157 if (operand->int_map[uval] == sval)
5158 break;
5159 if (uval == num_vals)
1a00e612
RS
5160 {
5161 match_out_of_range (arg);
5162 return FALSE;
5163 }
a1d78564
RS
5164
5165 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5166 return TRUE;
a1d78564
RS
5167}
5168
5169/* OP_MSB matcher. */
5170
a92713e6 5171static bfd_boolean
a1d78564 5172match_msb_operand (struct mips_arg_info *arg,
a92713e6 5173 const struct mips_operand *operand_base)
a1d78564
RS
5174{
5175 const struct mips_msb_operand *operand;
5176 int min_val, max_val, max_high;
5177 offsetT size, sval, high;
5178
5179 operand = (const struct mips_msb_operand *) operand_base;
5180 min_val = operand->bias;
5181 max_val = min_val + (1 << operand_base->size) - 1;
5182 max_high = operand->opsize;
5183
1a00e612 5184 if (!match_const_int (arg, &size))
a92713e6 5185 return FALSE;
a1d78564
RS
5186
5187 high = size + arg->last_op_int;
5188 sval = operand->add_lsb ? high : size;
5189
5190 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5191 {
1a00e612
RS
5192 match_out_of_range (arg);
5193 return FALSE;
a1d78564
RS
5194 }
5195 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5196 return TRUE;
a1d78564
RS
5197}
5198
5199/* OP_REG matcher. */
5200
a92713e6 5201static bfd_boolean
a1d78564 5202match_reg_operand (struct mips_arg_info *arg,
a92713e6 5203 const struct mips_operand *operand_base)
a1d78564
RS
5204{
5205 const struct mips_reg_operand *operand;
a92713e6 5206 unsigned int regno, uval, num_vals;
a1d78564
RS
5207
5208 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5209 if (!match_reg (arg, operand->reg_type, &regno))
5210 return FALSE;
a1d78564
RS
5211
5212 if (operand->reg_map)
5213 {
5214 num_vals = 1 << operand->root.size;
5215 for (uval = 0; uval < num_vals; uval++)
5216 if (operand->reg_map[uval] == regno)
5217 break;
5218 if (num_vals == uval)
a92713e6 5219 return FALSE;
a1d78564
RS
5220 }
5221 else
5222 uval = regno;
5223
a1d78564
RS
5224 arg->last_regno = regno;
5225 if (arg->opnum == 1)
5226 arg->dest_regno = regno;
5227 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5228 return TRUE;
a1d78564
RS
5229}
5230
5231/* OP_REG_PAIR matcher. */
5232
a92713e6 5233static bfd_boolean
a1d78564 5234match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5235 const struct mips_operand *operand_base)
a1d78564
RS
5236{
5237 const struct mips_reg_pair_operand *operand;
a92713e6 5238 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5239
5240 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5241 if (!match_reg (arg, operand->reg_type, &regno1)
5242 || !match_char (arg, ',')
5243 || !match_reg (arg, operand->reg_type, &regno2))
5244 return FALSE;
a1d78564
RS
5245
5246 num_vals = 1 << operand_base->size;
5247 for (uval = 0; uval < num_vals; uval++)
5248 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5249 break;
5250 if (uval == num_vals)
a92713e6 5251 return FALSE;
a1d78564 5252
a1d78564 5253 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5254 return TRUE;
a1d78564
RS
5255}
5256
5257/* OP_PCREL matcher. The caller chooses the relocation type. */
5258
a92713e6
RS
5259static bfd_boolean
5260match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5261{
a92713e6
RS
5262 bfd_reloc_code_real_type r[3];
5263
5264 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5265}
5266
5267/* OP_PERF_REG matcher. */
5268
a92713e6 5269static bfd_boolean
a1d78564 5270match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5271 const struct mips_operand *operand)
a1d78564
RS
5272{
5273 offsetT sval;
5274
1a00e612 5275 if (!match_const_int (arg, &sval))
a92713e6 5276 return FALSE;
a1d78564
RS
5277
5278 if (sval != 0
5279 && (sval != 1
5280 || (mips_opts.arch == CPU_R5900
5281 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5282 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5283 {
1a00e612
RS
5284 set_insn_error (arg->argnum, _("invalid performance register"));
5285 return FALSE;
a1d78564
RS
5286 }
5287
5288 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5289 return TRUE;
a1d78564
RS
5290}
5291
5292/* OP_ADDIUSP matcher. */
5293
a92713e6 5294static bfd_boolean
a1d78564 5295match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5296 const struct mips_operand *operand)
a1d78564
RS
5297{
5298 offsetT sval;
5299 unsigned int uval;
5300
1a00e612 5301 if (!match_const_int (arg, &sval))
a92713e6 5302 return FALSE;
a1d78564
RS
5303
5304 if (sval % 4)
1a00e612
RS
5305 {
5306 match_out_of_range (arg);
5307 return FALSE;
5308 }
a1d78564
RS
5309
5310 sval /= 4;
5311 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5312 {
5313 match_out_of_range (arg);
5314 return FALSE;
5315 }
a1d78564
RS
5316
5317 uval = (unsigned int) sval;
5318 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5319 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5320 return TRUE;
a1d78564
RS
5321}
5322
5323/* OP_CLO_CLZ_DEST matcher. */
5324
a92713e6 5325static bfd_boolean
a1d78564 5326match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5327 const struct mips_operand *operand)
a1d78564
RS
5328{
5329 unsigned int regno;
5330
a92713e6
RS
5331 if (!match_reg (arg, OP_REG_GP, &regno))
5332 return FALSE;
a1d78564 5333
a1d78564 5334 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5335 return TRUE;
a1d78564
RS
5336}
5337
7361da2c
AB
5338/* OP_CHECK_PREV matcher. */
5339
5340static bfd_boolean
5341match_check_prev_operand (struct mips_arg_info *arg,
5342 const struct mips_operand *operand_base)
5343{
5344 const struct mips_check_prev_operand *operand;
5345 unsigned int regno;
5346
5347 operand = (const struct mips_check_prev_operand *) operand_base;
5348
5349 if (!match_reg (arg, OP_REG_GP, &regno))
5350 return FALSE;
5351
5352 if (!operand->zero_ok && regno == 0)
5353 return FALSE;
5354
5355 if ((operand->less_than_ok && regno < arg->last_regno)
5356 || (operand->greater_than_ok && regno > arg->last_regno)
5357 || (operand->equal_ok && regno == arg->last_regno))
5358 {
5359 arg->last_regno = regno;
5360 insn_insert_operand (arg->insn, operand_base, regno);
5361 return TRUE;
5362 }
5363
5364 return FALSE;
5365}
5366
5367/* OP_SAME_RS_RT matcher. */
5368
5369static bfd_boolean
5370match_same_rs_rt_operand (struct mips_arg_info *arg,
5371 const struct mips_operand *operand)
5372{
5373 unsigned int regno;
5374
5375 if (!match_reg (arg, OP_REG_GP, &regno))
5376 return FALSE;
5377
5378 if (regno == 0)
5379 {
5380 set_insn_error (arg->argnum, _("the source register must not be $0"));
5381 return FALSE;
5382 }
5383
5384 arg->last_regno = regno;
5385
5386 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5387 return TRUE;
5388}
5389
a1d78564
RS
5390/* OP_LWM_SWM_LIST matcher. */
5391
a92713e6 5392static bfd_boolean
a1d78564 5393match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5394 const struct mips_operand *operand)
a1d78564 5395{
a92713e6
RS
5396 unsigned int reglist, sregs, ra, regno1, regno2;
5397 struct mips_arg_info reset;
a1d78564 5398
a92713e6
RS
5399 reglist = 0;
5400 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5401 return FALSE;
5402 do
5403 {
5404 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5405 {
5406 reglist |= 1 << FP;
5407 regno2 = S7;
5408 }
5409 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5410 reset = *arg;
5411 }
5412 while (match_char (arg, ',')
5413 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5414 *arg = reset;
a1d78564
RS
5415
5416 if (operand->size == 2)
5417 {
5418 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5419
5420 s0, ra
5421 s0, s1, ra, s2, s3
5422 s0-s2, ra
5423
5424 and any permutations of these. */
5425 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5426 return FALSE;
a1d78564
RS
5427
5428 sregs = (reglist >> 17) & 7;
5429 ra = 0;
5430 }
5431 else
5432 {
5433 /* The list must include at least one of ra and s0-sN,
5434 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5435 which are $23 and $30 respectively.) E.g.:
5436
5437 ra
5438 s0
5439 ra, s0, s1, s2
5440 s0-s8
5441 s0-s5, ra
5442
5443 and any permutations of these. */
5444 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5445 return FALSE;
a1d78564
RS
5446
5447 ra = (reglist >> 27) & 0x10;
5448 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5449 }
5450 sregs += 1;
5451 if ((sregs & -sregs) != sregs)
a92713e6 5452 return FALSE;
a1d78564
RS
5453
5454 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5455 return TRUE;
a1d78564
RS
5456}
5457
364215c8
RS
5458/* OP_ENTRY_EXIT_LIST matcher. */
5459
a92713e6 5460static unsigned int
364215c8 5461match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5462 const struct mips_operand *operand)
364215c8
RS
5463{
5464 unsigned int mask;
5465 bfd_boolean is_exit;
5466
5467 /* The format is the same for both ENTRY and EXIT, but the constraints
5468 are different. */
5469 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5470 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5471 do
364215c8
RS
5472 {
5473 unsigned int regno1, regno2;
5474 bfd_boolean is_freg;
5475
a92713e6 5476 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5477 is_freg = FALSE;
a92713e6 5478 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5479 is_freg = TRUE;
5480 else
a92713e6 5481 return FALSE;
364215c8
RS
5482
5483 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5484 {
5485 mask &= ~(7 << 3);
5486 mask |= (5 + regno2) << 3;
5487 }
5488 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5489 mask |= (regno2 - 3) << 3;
5490 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5491 mask |= (regno2 - 15) << 1;
5492 else if (regno1 == RA && regno2 == RA)
5493 mask |= 1;
5494 else
a92713e6 5495 return FALSE;
364215c8 5496 }
a92713e6
RS
5497 while (match_char (arg, ','));
5498
364215c8 5499 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5500 return TRUE;
364215c8
RS
5501}
5502
5503/* OP_SAVE_RESTORE_LIST matcher. */
5504
a92713e6
RS
5505static bfd_boolean
5506match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5507{
5508 unsigned int opcode, args, statics, sregs;
5509 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 5510 offsetT frame_size;
364215c8 5511
364215c8
RS
5512 opcode = arg->insn->insn_opcode;
5513 frame_size = 0;
5514 num_frame_sizes = 0;
5515 args = 0;
5516 statics = 0;
5517 sregs = 0;
a92713e6 5518 do
364215c8
RS
5519 {
5520 unsigned int regno1, regno2;
5521
a92713e6 5522 if (arg->token->type == OT_INTEGER)
364215c8
RS
5523 {
5524 /* Handle the frame size. */
1a00e612 5525 if (!match_const_int (arg, &frame_size))
a92713e6 5526 return FALSE;
364215c8 5527 num_frame_sizes += 1;
364215c8
RS
5528 }
5529 else
5530 {
a92713e6
RS
5531 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5532 return FALSE;
364215c8
RS
5533
5534 while (regno1 <= regno2)
5535 {
5536 if (regno1 >= 4 && regno1 <= 7)
5537 {
5538 if (num_frame_sizes == 0)
5539 /* args $a0-$a3 */
5540 args |= 1 << (regno1 - 4);
5541 else
5542 /* statics $a0-$a3 */
5543 statics |= 1 << (regno1 - 4);
5544 }
5545 else if (regno1 >= 16 && regno1 <= 23)
5546 /* $s0-$s7 */
5547 sregs |= 1 << (regno1 - 16);
5548 else if (regno1 == 30)
5549 /* $s8 */
5550 sregs |= 1 << 8;
5551 else if (regno1 == 31)
5552 /* Add $ra to insn. */
5553 opcode |= 0x40;
5554 else
a92713e6 5555 return FALSE;
364215c8
RS
5556 regno1 += 1;
5557 if (regno1 == 24)
5558 regno1 = 30;
5559 }
5560 }
364215c8 5561 }
a92713e6 5562 while (match_char (arg, ','));
364215c8
RS
5563
5564 /* Encode args/statics combination. */
5565 if (args & statics)
a92713e6 5566 return FALSE;
364215c8
RS
5567 else if (args == 0xf)
5568 /* All $a0-$a3 are args. */
5569 opcode |= MIPS16_ALL_ARGS << 16;
5570 else if (statics == 0xf)
5571 /* All $a0-$a3 are statics. */
5572 opcode |= MIPS16_ALL_STATICS << 16;
5573 else
5574 {
5575 /* Count arg registers. */
5576 num_args = 0;
5577 while (args & 0x1)
5578 {
5579 args >>= 1;
5580 num_args += 1;
5581 }
5582 if (args != 0)
a92713e6 5583 return FALSE;
364215c8
RS
5584
5585 /* Count static registers. */
5586 num_statics = 0;
5587 while (statics & 0x8)
5588 {
5589 statics = (statics << 1) & 0xf;
5590 num_statics += 1;
5591 }
5592 if (statics != 0)
a92713e6 5593 return FALSE;
364215c8
RS
5594
5595 /* Encode args/statics. */
5596 opcode |= ((num_args << 2) | num_statics) << 16;
5597 }
5598
5599 /* Encode $s0/$s1. */
5600 if (sregs & (1 << 0)) /* $s0 */
5601 opcode |= 0x20;
5602 if (sregs & (1 << 1)) /* $s1 */
5603 opcode |= 0x10;
5604 sregs >>= 2;
5605
5606 /* Encode $s2-$s8. */
5607 num_sregs = 0;
5608 while (sregs & 1)
5609 {
5610 sregs >>= 1;
5611 num_sregs += 1;
5612 }
5613 if (sregs != 0)
a92713e6 5614 return FALSE;
364215c8
RS
5615 opcode |= num_sregs << 24;
5616
5617 /* Encode frame size. */
5618 if (num_frame_sizes == 0)
1a00e612
RS
5619 {
5620 set_insn_error (arg->argnum, _("missing frame size"));
5621 return FALSE;
5622 }
5623 if (num_frame_sizes > 1)
5624 {
5625 set_insn_error (arg->argnum, _("frame size specified twice"));
5626 return FALSE;
5627 }
5628 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5629 {
5630 set_insn_error (arg->argnum, _("invalid frame size"));
5631 return FALSE;
5632 }
5633 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5634 {
5635 frame_size /= 8;
5636 opcode |= (((frame_size & 0xf0) << 16)
5637 | (frame_size & 0x0f));
5638 }
5639
364215c8
RS
5640 /* Finally build the instruction. */
5641 if ((opcode >> 16) != 0 || frame_size == 0)
5642 opcode |= MIPS16_EXTEND;
5643 arg->insn->insn_opcode = opcode;
a92713e6 5644 return TRUE;
364215c8
RS
5645}
5646
a1d78564
RS
5647/* OP_MDMX_IMM_REG matcher. */
5648
a92713e6 5649static bfd_boolean
a1d78564 5650match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5651 const struct mips_operand *operand)
a1d78564 5652{
a92713e6 5653 unsigned int regno, uval;
a1d78564
RS
5654 bfd_boolean is_qh;
5655 const struct mips_opcode *opcode;
5656
5657 /* The mips_opcode records whether this is an octobyte or quadhalf
5658 instruction. Start out with that bit in place. */
5659 opcode = arg->insn->insn_mo;
5660 uval = mips_extract_operand (operand, opcode->match);
5661 is_qh = (uval != 0);
5662
56d438b1 5663 if (arg->token->type == OT_REG)
a1d78564
RS
5664 {
5665 if ((opcode->membership & INSN_5400)
5666 && strcmp (opcode->name, "rzu.ob") == 0)
5667 {
1a00e612
RS
5668 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5669 arg->argnum);
5670 return FALSE;
a1d78564
RS
5671 }
5672
56d438b1
CF
5673 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5674 return FALSE;
5675 ++arg->token;
5676
a1d78564
RS
5677 /* Check whether this is a vector register or a broadcast of
5678 a single element. */
56d438b1 5679 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5680 {
56d438b1 5681 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5682 {
1a00e612
RS
5683 set_insn_error (arg->argnum, _("invalid element selector"));
5684 return FALSE;
a1d78564 5685 }
56d438b1
CF
5686 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5687 ++arg->token;
a1d78564
RS
5688 }
5689 else
5690 {
5691 /* A full vector. */
5692 if ((opcode->membership & INSN_5400)
5693 && (strcmp (opcode->name, "sll.ob") == 0
5694 || strcmp (opcode->name, "srl.ob") == 0))
5695 {
1a00e612
RS
5696 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5697 arg->argnum);
5698 return FALSE;
a1d78564
RS
5699 }
5700
5701 if (is_qh)
5702 uval |= MDMX_FMTSEL_VEC_QH << 5;
5703 else
5704 uval |= MDMX_FMTSEL_VEC_OB << 5;
5705 }
a1d78564
RS
5706 uval |= regno;
5707 }
5708 else
5709 {
5710 offsetT sval;
5711
1a00e612 5712 if (!match_const_int (arg, &sval))
a92713e6 5713 return FALSE;
a1d78564
RS
5714 if (sval < 0 || sval > 31)
5715 {
1a00e612
RS
5716 match_out_of_range (arg);
5717 return FALSE;
a1d78564
RS
5718 }
5719 uval |= (sval & 31);
5720 if (is_qh)
5721 uval |= MDMX_FMTSEL_IMM_QH << 5;
5722 else
5723 uval |= MDMX_FMTSEL_IMM_OB << 5;
5724 }
5725 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5726 return TRUE;
a1d78564
RS
5727}
5728
56d438b1
CF
5729/* OP_IMM_INDEX matcher. */
5730
5731static bfd_boolean
5732match_imm_index_operand (struct mips_arg_info *arg,
5733 const struct mips_operand *operand)
5734{
5735 unsigned int max_val;
5736
5737 if (arg->token->type != OT_INTEGER_INDEX)
5738 return FALSE;
5739
5740 max_val = (1 << operand->size) - 1;
5741 if (arg->token->u.index > max_val)
5742 {
5743 match_out_of_range (arg);
5744 return FALSE;
5745 }
5746 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5747 ++arg->token;
5748 return TRUE;
5749}
5750
5751/* OP_REG_INDEX matcher. */
5752
5753static bfd_boolean
5754match_reg_index_operand (struct mips_arg_info *arg,
5755 const struct mips_operand *operand)
5756{
5757 unsigned int regno;
5758
5759 if (arg->token->type != OT_REG_INDEX)
5760 return FALSE;
5761
5762 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5763 return FALSE;
5764
5765 insn_insert_operand (arg->insn, operand, regno);
5766 ++arg->token;
5767 return TRUE;
5768}
5769
a1d78564
RS
5770/* OP_PC matcher. */
5771
a92713e6
RS
5772static bfd_boolean
5773match_pc_operand (struct mips_arg_info *arg)
a1d78564 5774{
a92713e6
RS
5775 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5776 {
5777 ++arg->token;
5778 return TRUE;
5779 }
5780 return FALSE;
a1d78564
RS
5781}
5782
7361da2c
AB
5783/* OP_NON_ZERO_REG matcher. */
5784
5785static bfd_boolean
5786match_non_zero_reg_operand (struct mips_arg_info *arg,
5787 const struct mips_operand *operand)
5788{
5789 unsigned int regno;
5790
5791 if (!match_reg (arg, OP_REG_GP, &regno))
5792 return FALSE;
5793
5794 if (regno == 0)
5795 return FALSE;
5796
5797 arg->last_regno = regno;
5798 insn_insert_operand (arg->insn, operand, regno);
5799 return TRUE;
5800}
5801
a1d78564
RS
5802/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5803 register that we need to match. */
5804
a92713e6
RS
5805static bfd_boolean
5806match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5807{
5808 unsigned int regno;
5809
a92713e6 5810 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5811}
5812
89565f1b
RS
5813/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5814 the length of the value in bytes (4 for float, 8 for double) and
5815 USING_GPRS says whether the destination is a GPR rather than an FPR.
5816
5817 Return the constant in IMM and OFFSET as follows:
5818
5819 - If the constant should be loaded via memory, set IMM to O_absent and
5820 OFFSET to the memory address.
5821
5822 - Otherwise, if the constant should be loaded into two 32-bit registers,
5823 set IMM to the O_constant to load into the high register and OFFSET
5824 to the corresponding value for the low register.
5825
5826 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5827
5828 These constants only appear as the last operand in an instruction,
5829 and every instruction that accepts them in any variant accepts them
5830 in all variants. This means we don't have to worry about backing out
5831 any changes if the instruction does not match. We just match
5832 unconditionally and report an error if the constant is invalid. */
5833
a92713e6
RS
5834static bfd_boolean
5835match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5836 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5837{
a92713e6 5838 char *p;
89565f1b
RS
5839 segT seg, new_seg;
5840 subsegT subseg;
5841 const char *newname;
a92713e6 5842 unsigned char *data;
89565f1b
RS
5843
5844 /* Where the constant is placed is based on how the MIPS assembler
5845 does things:
5846
5847 length == 4 && using_gprs -- immediate value only
5848 length == 8 && using_gprs -- .rdata or immediate value
5849 length == 4 && !using_gprs -- .lit4 or immediate value
5850 length == 8 && !using_gprs -- .lit8 or immediate value
5851
5852 The .lit4 and .lit8 sections are only used if permitted by the
5853 -G argument. */
a92713e6 5854 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5855 {
5856 set_insn_error (arg->argnum, _("floating-point expression required"));
5857 return FALSE;
5858 }
a92713e6
RS
5859
5860 gas_assert (arg->token->u.flt.length == length);
5861 data = arg->token->u.flt.data;
5862 ++arg->token;
89565f1b
RS
5863
5864 /* Handle 32-bit constants for which an immediate value is best. */
5865 if (length == 4
5866 && (using_gprs
5867 || g_switch_value < 4
5868 || (data[0] == 0 && data[1] == 0)
5869 || (data[2] == 0 && data[3] == 0)))
5870 {
5871 imm->X_op = O_constant;
5872 if (!target_big_endian)
5873 imm->X_add_number = bfd_getl32 (data);
5874 else
5875 imm->X_add_number = bfd_getb32 (data);
5876 offset->X_op = O_absent;
a92713e6 5877 return TRUE;
89565f1b
RS
5878 }
5879
5880 /* Handle 64-bit constants for which an immediate value is best. */
5881 if (length == 8
5882 && !mips_disable_float_construction
351cdf24
MF
5883 /* Constants can only be constructed in GPRs and copied to FPRs if the
5884 GPRs are at least as wide as the FPRs or MTHC1 is available.
5885 Unlike most tests for 32-bit floating-point registers this check
5886 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5887 permit 64-bit moves without MXHC1.
5888 Force the constant into memory otherwise. */
5889 && (using_gprs
5890 || GPR_SIZE == 64
5891 || ISA_HAS_MXHC1 (mips_opts.isa)
5892 || FPR_SIZE == 32)
89565f1b
RS
5893 && ((data[0] == 0 && data[1] == 0)
5894 || (data[2] == 0 && data[3] == 0))
5895 && ((data[4] == 0 && data[5] == 0)
5896 || (data[6] == 0 && data[7] == 0)))
5897 {
5898 /* The value is simple enough to load with a couple of instructions.
5899 If using 32-bit registers, set IMM to the high order 32 bits and
5900 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5901 64 bit constant. */
351cdf24 5902 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
5903 {
5904 imm->X_op = O_constant;
5905 offset->X_op = O_constant;
5906 if (!target_big_endian)
5907 {
5908 imm->X_add_number = bfd_getl32 (data + 4);
5909 offset->X_add_number = bfd_getl32 (data);
5910 }
5911 else
5912 {
5913 imm->X_add_number = bfd_getb32 (data);
5914 offset->X_add_number = bfd_getb32 (data + 4);
5915 }
5916 if (offset->X_add_number == 0)
5917 offset->X_op = O_absent;
5918 }
5919 else
5920 {
5921 imm->X_op = O_constant;
5922 if (!target_big_endian)
5923 imm->X_add_number = bfd_getl64 (data);
5924 else
5925 imm->X_add_number = bfd_getb64 (data);
5926 offset->X_op = O_absent;
5927 }
a92713e6 5928 return TRUE;
89565f1b
RS
5929 }
5930
5931 /* Switch to the right section. */
5932 seg = now_seg;
5933 subseg = now_subseg;
5934 if (length == 4)
5935 {
5936 gas_assert (!using_gprs && g_switch_value >= 4);
5937 newname = ".lit4";
5938 }
5939 else
5940 {
5941 if (using_gprs || g_switch_value < 8)
5942 newname = RDATA_SECTION_NAME;
5943 else
5944 newname = ".lit8";
5945 }
5946
5947 new_seg = subseg_new (newname, (subsegT) 0);
5948 bfd_set_section_flags (stdoutput, new_seg,
5949 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5950 frag_align (length == 4 ? 2 : 3, 0, 0);
5951 if (strncmp (TARGET_OS, "elf", 3) != 0)
5952 record_alignment (new_seg, 4);
5953 else
5954 record_alignment (new_seg, length == 4 ? 2 : 3);
5955 if (seg == now_seg)
1661c76c 5956 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
5957
5958 /* Set the argument to the current address in the section. */
5959 imm->X_op = O_absent;
5960 offset->X_op = O_symbol;
5961 offset->X_add_symbol = symbol_temp_new_now ();
5962 offset->X_add_number = 0;
5963
5964 /* Put the floating point number into the section. */
5965 p = frag_more (length);
5966 memcpy (p, data, length);
5967
5968 /* Switch back to the original section. */
5969 subseg_set (seg, subseg);
a92713e6 5970 return TRUE;
89565f1b
RS
5971}
5972
14daeee3
RS
5973/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5974 them. */
5975
5976static bfd_boolean
5977match_vu0_suffix_operand (struct mips_arg_info *arg,
5978 const struct mips_operand *operand,
5979 bfd_boolean match_p)
5980{
5981 unsigned int uval;
5982
5983 /* The operand can be an XYZW mask or a single 2-bit channel index
5984 (with X being 0). */
5985 gas_assert (operand->size == 2 || operand->size == 4);
5986
ee5734f0 5987 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5988 if (arg->token->type != OT_CHANNELS)
ee5734f0 5989 return match_p;
14daeee3
RS
5990
5991 uval = arg->token->u.channels;
5992 if (operand->size == 2)
5993 {
5994 /* Check that a single bit is set and convert it into a 2-bit index. */
5995 if ((uval & -uval) != uval)
5996 return FALSE;
5997 uval = 4 - ffs (uval);
5998 }
5999
6000 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6001 return FALSE;
6002
6003 ++arg->token;
6004 if (!match_p)
6005 insn_insert_operand (arg->insn, operand, uval);
6006 return TRUE;
6007}
6008
a1d78564
RS
6009/* S is the text seen for ARG. Match it against OPERAND. Return the end
6010 of the argument text if the match is successful, otherwise return null. */
6011
a92713e6 6012static bfd_boolean
a1d78564 6013match_operand (struct mips_arg_info *arg,
a92713e6 6014 const struct mips_operand *operand)
a1d78564
RS
6015{
6016 switch (operand->type)
6017 {
6018 case OP_INT:
a92713e6 6019 return match_int_operand (arg, operand);
a1d78564
RS
6020
6021 case OP_MAPPED_INT:
a92713e6 6022 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6023
6024 case OP_MSB:
a92713e6 6025 return match_msb_operand (arg, operand);
a1d78564
RS
6026
6027 case OP_REG:
0f35dbc4 6028 case OP_OPTIONAL_REG:
a92713e6 6029 return match_reg_operand (arg, operand);
a1d78564
RS
6030
6031 case OP_REG_PAIR:
a92713e6 6032 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6033
6034 case OP_PCREL:
a92713e6 6035 return match_pcrel_operand (arg);
a1d78564
RS
6036
6037 case OP_PERF_REG:
a92713e6 6038 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6039
6040 case OP_ADDIUSP_INT:
a92713e6 6041 return match_addiusp_operand (arg, operand);
a1d78564
RS
6042
6043 case OP_CLO_CLZ_DEST:
a92713e6 6044 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6045
6046 case OP_LWM_SWM_LIST:
a92713e6 6047 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6048
6049 case OP_ENTRY_EXIT_LIST:
a92713e6 6050 return match_entry_exit_operand (arg, operand);
364215c8 6051
a1d78564 6052 case OP_SAVE_RESTORE_LIST:
a92713e6 6053 return match_save_restore_list_operand (arg);
a1d78564
RS
6054
6055 case OP_MDMX_IMM_REG:
a92713e6 6056 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6057
6058 case OP_REPEAT_DEST_REG:
a92713e6 6059 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6060
6061 case OP_REPEAT_PREV_REG:
a92713e6 6062 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6063
6064 case OP_PC:
a92713e6 6065 return match_pc_operand (arg);
14daeee3
RS
6066
6067 case OP_VU0_SUFFIX:
6068 return match_vu0_suffix_operand (arg, operand, FALSE);
6069
6070 case OP_VU0_MATCH_SUFFIX:
6071 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6072
6073 case OP_IMM_INDEX:
6074 return match_imm_index_operand (arg, operand);
6075
6076 case OP_REG_INDEX:
6077 return match_reg_index_operand (arg, operand);
7361da2c
AB
6078
6079 case OP_SAME_RS_RT:
6080 return match_same_rs_rt_operand (arg, operand);
6081
6082 case OP_CHECK_PREV:
6083 return match_check_prev_operand (arg, operand);
6084
6085 case OP_NON_ZERO_REG:
6086 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6087 }
6088 abort ();
6089}
6090
6091/* ARG is the state after successfully matching an instruction.
6092 Issue any queued-up warnings. */
6093
6094static void
6095check_completed_insn (struct mips_arg_info *arg)
6096{
6097 if (arg->seen_at)
6098 {
6099 if (AT == ATREG)
1661c76c 6100 as_warn (_("used $at without \".set noat\""));
a1d78564 6101 else
1661c76c 6102 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6103 }
6104}
a1d78564 6105
85fcb30f
RS
6106/* Return true if modifying general-purpose register REG needs a delay. */
6107
6108static bfd_boolean
6109reg_needs_delay (unsigned int reg)
6110{
6111 unsigned long prev_pinfo;
6112
6113 prev_pinfo = history[0].insn_mo->pinfo;
6114 if (!mips_opts.noreorder
67dc82bc 6115 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6116 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6117 && (gpr_write_mask (&history[0]) & (1 << reg)))
6118 return TRUE;
6119
6120 return FALSE;
6121}
6122
71400594
RS
6123/* Classify an instruction according to the FIX_VR4120_* enumeration.
6124 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6125 by VR4120 errata. */
4d7206a2 6126
71400594
RS
6127static unsigned int
6128classify_vr4120_insn (const char *name)
252b5132 6129{
71400594
RS
6130 if (strncmp (name, "macc", 4) == 0)
6131 return FIX_VR4120_MACC;
6132 if (strncmp (name, "dmacc", 5) == 0)
6133 return FIX_VR4120_DMACC;
6134 if (strncmp (name, "mult", 4) == 0)
6135 return FIX_VR4120_MULT;
6136 if (strncmp (name, "dmult", 5) == 0)
6137 return FIX_VR4120_DMULT;
6138 if (strstr (name, "div"))
6139 return FIX_VR4120_DIV;
6140 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6141 return FIX_VR4120_MTHILO;
6142 return NUM_FIX_VR4120_CLASSES;
6143}
252b5132 6144
a8d14a88
CM
6145#define INSN_ERET 0x42000018
6146#define INSN_DERET 0x4200001f
6147#define INSN_DMULT 0x1c
6148#define INSN_DMULTU 0x1d
ff239038 6149
71400594
RS
6150/* Return the number of instructions that must separate INSN1 and INSN2,
6151 where INSN1 is the earlier instruction. Return the worst-case value
6152 for any INSN2 if INSN2 is null. */
252b5132 6153
71400594
RS
6154static unsigned int
6155insns_between (const struct mips_cl_insn *insn1,
6156 const struct mips_cl_insn *insn2)
6157{
6158 unsigned long pinfo1, pinfo2;
4c260379 6159 unsigned int mask;
71400594 6160
85fcb30f
RS
6161 /* If INFO2 is null, pessimistically assume that all flags are set for
6162 the second instruction. */
71400594
RS
6163 pinfo1 = insn1->insn_mo->pinfo;
6164 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6165
71400594
RS
6166 /* For most targets, write-after-read dependencies on the HI and LO
6167 registers must be separated by at least two instructions. */
6168 if (!hilo_interlocks)
252b5132 6169 {
71400594
RS
6170 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6171 return 2;
6172 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6173 return 2;
6174 }
6175
6176 /* If we're working around r7000 errata, there must be two instructions
6177 between an mfhi or mflo and any instruction that uses the result. */
6178 if (mips_7000_hilo_fix
df58fc94 6179 && !mips_opts.micromips
71400594 6180 && MF_HILO_INSN (pinfo1)
85fcb30f 6181 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6182 return 2;
6183
ff239038
CM
6184 /* If we're working around 24K errata, one instruction is required
6185 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6186 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6187 {
6188 if (insn1->insn_opcode == INSN_ERET
6189 || insn1->insn_opcode == INSN_DERET)
6190 {
6191 if (insn2 == NULL
6192 || insn2->insn_opcode == INSN_ERET
6193 || insn2->insn_opcode == INSN_DERET
11625dd8 6194 || delayed_branch_p (insn2))
ff239038
CM
6195 return 1;
6196 }
6197 }
6198
a8d14a88
CM
6199 /* If we're working around PMC RM7000 errata, there must be three
6200 nops between a dmult and a load instruction. */
6201 if (mips_fix_rm7000 && !mips_opts.micromips)
6202 {
6203 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6204 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6205 {
6206 if (pinfo2 & INSN_LOAD_MEMORY)
6207 return 3;
6208 }
6209 }
6210
71400594
RS
6211 /* If working around VR4120 errata, check for combinations that need
6212 a single intervening instruction. */
df58fc94 6213 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6214 {
6215 unsigned int class1, class2;
252b5132 6216
71400594
RS
6217 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6218 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6219 {
71400594
RS
6220 if (insn2 == NULL)
6221 return 1;
6222 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6223 if (vr4120_conflicts[class1] & (1 << class2))
6224 return 1;
252b5132 6225 }
71400594
RS
6226 }
6227
df58fc94 6228 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6229 {
6230 /* Check for GPR or coprocessor load delays. All such delays
6231 are on the RT register. */
6232 /* Itbl support may require additional care here. */
67dc82bc 6233 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6234 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6235 {
85fcb30f 6236 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6237 return 1;
6238 }
6239
6240 /* Check for generic coprocessor hazards.
6241
6242 This case is not handled very well. There is no special
6243 knowledge of CP0 handling, and the coprocessors other than
6244 the floating point unit are not distinguished at all. */
6245 /* Itbl support may require additional care here. FIXME!
6246 Need to modify this to include knowledge about
6247 user specified delays! */
43885403 6248 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6249 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6250 {
6251 /* Handle cases where INSN1 writes to a known general coprocessor
6252 register. There must be a one instruction delay before INSN2
6253 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6254 mask = fpr_write_mask (insn1);
6255 if (mask != 0)
252b5132 6256 {
4c260379 6257 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6258 return 1;
252b5132
RH
6259 }
6260 else
6261 {
71400594
RS
6262 /* Read-after-write dependencies on the control registers
6263 require a two-instruction gap. */
6264 if ((pinfo1 & INSN_WRITE_COND_CODE)
6265 && (pinfo2 & INSN_READ_COND_CODE))
6266 return 2;
6267
6268 /* We don't know exactly what INSN1 does. If INSN2 is
6269 also a coprocessor instruction, assume there must be
6270 a one instruction gap. */
6271 if (pinfo2 & INSN_COP)
6272 return 1;
252b5132
RH
6273 }
6274 }
6b76fefe 6275
71400594
RS
6276 /* Check for read-after-write dependencies on the coprocessor
6277 control registers in cases where INSN1 does not need a general
6278 coprocessor delay. This means that INSN1 is a floating point
6279 comparison instruction. */
6280 /* Itbl support may require additional care here. */
6281 else if (!cop_interlocks
6282 && (pinfo1 & INSN_WRITE_COND_CODE)
6283 && (pinfo2 & INSN_READ_COND_CODE))
6284 return 1;
6285 }
6b76fefe 6286
7361da2c
AB
6287 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6288 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6289 and pause. */
6290 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6291 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6292 || (insn2 && delayed_branch_p (insn2))))
6293 return 1;
6294
71400594
RS
6295 return 0;
6296}
6b76fefe 6297
7d8e00cf
RS
6298/* Return the number of nops that would be needed to work around the
6299 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6300 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6301 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6302
6303static int
932d1a1b 6304nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6305 const struct mips_cl_insn *insn)
6306{
4c260379
RS
6307 int i, j;
6308 unsigned int mask;
7d8e00cf
RS
6309
6310 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6311 are not affected by the errata. */
6312 if (insn != 0
6313 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6314 || strcmp (insn->insn_mo->name, "mtlo") == 0
6315 || strcmp (insn->insn_mo->name, "mthi") == 0))
6316 return 0;
6317
6318 /* Search for the first MFLO or MFHI. */
6319 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6320 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6321 {
6322 /* Extract the destination register. */
4c260379 6323 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6324
6325 /* No nops are needed if INSN reads that register. */
4c260379 6326 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6327 return 0;
6328
6329 /* ...or if any of the intervening instructions do. */
6330 for (j = 0; j < i; j++)
4c260379 6331 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6332 return 0;
6333
932d1a1b
RS
6334 if (i >= ignore)
6335 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6336 }
6337 return 0;
6338}
6339
134c0c8b
MR
6340#define BASE_REG_EQ(INSN1, INSN2) \
6341 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6342 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6343
6344/* Return the minimum alignment for this store instruction. */
6345
6346static int
6347fix_24k_align_to (const struct mips_opcode *mo)
6348{
6349 if (strcmp (mo->name, "sh") == 0)
6350 return 2;
6351
6352 if (strcmp (mo->name, "swc1") == 0
6353 || strcmp (mo->name, "swc2") == 0
6354 || strcmp (mo->name, "sw") == 0
6355 || strcmp (mo->name, "sc") == 0
6356 || strcmp (mo->name, "s.s") == 0)
6357 return 4;
6358
6359 if (strcmp (mo->name, "sdc1") == 0
6360 || strcmp (mo->name, "sdc2") == 0
6361 || strcmp (mo->name, "s.d") == 0)
6362 return 8;
6363
6364 /* sb, swl, swr */
6365 return 1;
6366}
6367
6368struct fix_24k_store_info
6369 {
6370 /* Immediate offset, if any, for this store instruction. */
6371 short off;
6372 /* Alignment required by this store instruction. */
6373 int align_to;
6374 /* True for register offsets. */
6375 int register_offset;
6376 };
6377
6378/* Comparison function used by qsort. */
6379
6380static int
6381fix_24k_sort (const void *a, const void *b)
6382{
6383 const struct fix_24k_store_info *pos1 = a;
6384 const struct fix_24k_store_info *pos2 = b;
6385
6386 return (pos1->off - pos2->off);
6387}
6388
6389/* INSN is a store instruction. Try to record the store information
6390 in STINFO. Return false if the information isn't known. */
6391
6392static bfd_boolean
6393fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6394 const struct mips_cl_insn *insn)
15be625d
CM
6395{
6396 /* The instruction must have a known offset. */
6397 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6398 return FALSE;
6399
6400 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6401 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6402 return TRUE;
6403}
6404
932d1a1b
RS
6405/* Return the number of nops that would be needed to work around the 24k
6406 "lost data on stores during refill" errata if instruction INSN
6407 immediately followed the 2 instructions described by HIST.
6408 Ignore hazards that are contained within the first IGNORE
6409 instructions of HIST.
6410
6411 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6412 for the data cache refills and store data. The following describes
6413 the scenario where the store data could be lost.
6414
6415 * A data cache miss, due to either a load or a store, causing fill
6416 data to be supplied by the memory subsystem
6417 * The first three doublewords of fill data are returned and written
6418 into the cache
6419 * A sequence of four stores occurs in consecutive cycles around the
6420 final doubleword of the fill:
6421 * Store A
6422 * Store B
6423 * Store C
6424 * Zero, One or more instructions
6425 * Store D
6426
6427 The four stores A-D must be to different doublewords of the line that
6428 is being filled. The fourth instruction in the sequence above permits
6429 the fill of the final doubleword to be transferred from the FSB into
6430 the cache. In the sequence above, the stores may be either integer
6431 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6432 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6433 different doublewords on the line. If the floating point unit is
6434 running in 1:2 mode, it is not possible to create the sequence above
6435 using only floating point store instructions.
15be625d
CM
6436
6437 In this case, the cache line being filled is incorrectly marked
6438 invalid, thereby losing the data from any store to the line that
6439 occurs between the original miss and the completion of the five
6440 cycle sequence shown above.
6441
932d1a1b 6442 The workarounds are:
15be625d 6443
932d1a1b
RS
6444 * Run the data cache in write-through mode.
6445 * Insert a non-store instruction between
6446 Store A and Store B or Store B and Store C. */
3739860c 6447
15be625d 6448static int
932d1a1b 6449nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6450 const struct mips_cl_insn *insn)
6451{
6452 struct fix_24k_store_info pos[3];
6453 int align, i, base_offset;
6454
932d1a1b
RS
6455 if (ignore >= 2)
6456 return 0;
6457
ab9794cf
RS
6458 /* If the previous instruction wasn't a store, there's nothing to
6459 worry about. */
15be625d
CM
6460 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6461 return 0;
6462
ab9794cf
RS
6463 /* If the instructions after the previous one are unknown, we have
6464 to assume the worst. */
6465 if (!insn)
15be625d
CM
6466 return 1;
6467
ab9794cf
RS
6468 /* Check whether we are dealing with three consecutive stores. */
6469 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6470 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6471 return 0;
6472
6473 /* If we don't know the relationship between the store addresses,
6474 assume the worst. */
ab9794cf 6475 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6476 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6477 return 1;
6478
6479 if (!fix_24k_record_store_info (&pos[0], insn)
6480 || !fix_24k_record_store_info (&pos[1], &hist[0])
6481 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6482 return 1;
6483
6484 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6485
6486 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6487 X bytes and such that the base register + X is known to be aligned
6488 to align bytes. */
6489
6490 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6491 align = 8;
6492 else
6493 {
6494 align = pos[0].align_to;
6495 base_offset = pos[0].off;
6496 for (i = 1; i < 3; i++)
6497 if (align < pos[i].align_to)
6498 {
6499 align = pos[i].align_to;
6500 base_offset = pos[i].off;
6501 }
6502 for (i = 0; i < 3; i++)
6503 pos[i].off -= base_offset;
6504 }
6505
6506 pos[0].off &= ~align + 1;
6507 pos[1].off &= ~align + 1;
6508 pos[2].off &= ~align + 1;
6509
6510 /* If any two stores write to the same chunk, they also write to the
6511 same doubleword. The offsets are still sorted at this point. */
6512 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6513 return 0;
6514
6515 /* A range of at least 9 bytes is needed for the stores to be in
6516 non-overlapping doublewords. */
6517 if (pos[2].off - pos[0].off <= 8)
6518 return 0;
6519
6520 if (pos[2].off - pos[1].off >= 24
6521 || pos[1].off - pos[0].off >= 24
6522 || pos[2].off - pos[0].off >= 32)
6523 return 0;
6524
6525 return 1;
6526}
6527
71400594 6528/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6529 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6530 where HIST[0] is the most recent instruction. Ignore hazards
6531 between INSN and the first IGNORE instructions in HIST.
6532
6533 If INSN is null, return the worse-case number of nops for any
6534 instruction. */
bdaaa2e1 6535
71400594 6536static int
932d1a1b 6537nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6538 const struct mips_cl_insn *insn)
6539{
6540 int i, nops, tmp_nops;
bdaaa2e1 6541
71400594 6542 nops = 0;
932d1a1b 6543 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6544 {
91d6fa6a 6545 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6546 if (tmp_nops > nops)
6547 nops = tmp_nops;
6548 }
7d8e00cf 6549
df58fc94 6550 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6551 {
932d1a1b 6552 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6553 if (tmp_nops > nops)
6554 nops = tmp_nops;
6555 }
6556
df58fc94 6557 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6558 {
932d1a1b 6559 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6560 if (tmp_nops > nops)
6561 nops = tmp_nops;
6562 }
6563
71400594
RS
6564 return nops;
6565}
252b5132 6566
71400594 6567/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6568 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6569 would be needed after the extended sequence, ignoring hazards
6570 in the first IGNORE instructions. */
252b5132 6571
71400594 6572static int
932d1a1b
RS
6573nops_for_sequence (int num_insns, int ignore,
6574 const struct mips_cl_insn *hist, ...)
71400594
RS
6575{
6576 va_list args;
6577 struct mips_cl_insn buffer[MAX_NOPS];
6578 struct mips_cl_insn *cursor;
6579 int nops;
6580
91d6fa6a 6581 va_start (args, hist);
71400594 6582 cursor = buffer + num_insns;
91d6fa6a 6583 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6584 while (cursor > buffer)
6585 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6586
932d1a1b 6587 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6588 va_end (args);
6589 return nops;
6590}
252b5132 6591
71400594
RS
6592/* Like nops_for_insn, but if INSN is a branch, take into account the
6593 worst-case delay for the branch target. */
252b5132 6594
71400594 6595static int
932d1a1b 6596nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6597 const struct mips_cl_insn *insn)
6598{
6599 int nops, tmp_nops;
60b63b72 6600
932d1a1b 6601 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6602 if (delayed_branch_p (insn))
71400594 6603 {
932d1a1b 6604 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6605 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6606 if (tmp_nops > nops)
6607 nops = tmp_nops;
6608 }
11625dd8 6609 else if (compact_branch_p (insn))
71400594 6610 {
932d1a1b 6611 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6612 if (tmp_nops > nops)
6613 nops = tmp_nops;
6614 }
6615 return nops;
6616}
6617
c67a084a
NC
6618/* Fix NOP issue: Replace nops by "or at,at,zero". */
6619
6620static void
6621fix_loongson2f_nop (struct mips_cl_insn * ip)
6622{
df58fc94 6623 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6624 if (strcmp (ip->insn_mo->name, "nop") == 0)
6625 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6626}
6627
6628/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6629 jr target pc &= 'hffff_ffff_cfff_ffff. */
6630
6631static void
6632fix_loongson2f_jump (struct mips_cl_insn * ip)
6633{
df58fc94 6634 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6635 if (strcmp (ip->insn_mo->name, "j") == 0
6636 || strcmp (ip->insn_mo->name, "jr") == 0
6637 || strcmp (ip->insn_mo->name, "jalr") == 0)
6638 {
6639 int sreg;
6640 expressionS ep;
6641
6642 if (! mips_opts.at)
6643 return;
6644
df58fc94 6645 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6646 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6647 return;
6648
6649 ep.X_op = O_constant;
6650 ep.X_add_number = 0xcfff0000;
6651 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6652 ep.X_add_number = 0xffff;
6653 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6654 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6655 }
6656}
6657
6658static void
6659fix_loongson2f (struct mips_cl_insn * ip)
6660{
6661 if (mips_fix_loongson2f_nop)
6662 fix_loongson2f_nop (ip);
6663
6664 if (mips_fix_loongson2f_jump)
6665 fix_loongson2f_jump (ip);
6666}
6667
a4e06468
RS
6668/* IP is a branch that has a delay slot, and we need to fill it
6669 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6670 with the previous instruction.
6671 ADDRESS_EXPR is an operand of the instruction to be used with
6672 RELOC_TYPE. */
a4e06468
RS
6673
6674static bfd_boolean
e407c74b 6675can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6676 bfd_reloc_code_real_type *reloc_type)
a4e06468 6677{
2b0c8b40 6678 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6679 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6680 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6681
6682 /* -O2 and above is required for this optimization. */
6683 if (mips_optimize < 2)
6684 return FALSE;
6685
6686 /* If we have seen .set volatile or .set nomove, don't optimize. */
6687 if (mips_opts.nomove)
6688 return FALSE;
6689
6690 /* We can't swap if the previous instruction's position is fixed. */
6691 if (history[0].fixed_p)
6692 return FALSE;
6693
6694 /* If the previous previous insn was in a .set noreorder, we can't
6695 swap. Actually, the MIPS assembler will swap in this situation.
6696 However, gcc configured -with-gnu-as will generate code like
6697
6698 .set noreorder
6699 lw $4,XXX
6700 .set reorder
6701 INSN
6702 bne $4,$0,foo
6703
6704 in which we can not swap the bne and INSN. If gcc is not configured
6705 -with-gnu-as, it does not output the .set pseudo-ops. */
6706 if (history[1].noreorder_p)
6707 return FALSE;
6708
87333bb7
MR
6709 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6710 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6711 if (mips_opts.mips16 && history[0].fixp[0])
6712 return FALSE;
6713
6714 /* If the branch is itself the target of a branch, we can not swap.
6715 We cheat on this; all we check for is whether there is a label on
6716 this instruction. If there are any branches to anything other than
6717 a label, users must use .set noreorder. */
6718 if (seg_info (now_seg)->label_list)
6719 return FALSE;
6720
6721 /* If the previous instruction is in a variant frag other than this
2309ddf2 6722 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6723 MIPS16 code, which uses variant frags for different purposes. */
6724 if (!mips_opts.mips16
a4e06468
RS
6725 && history[0].frag
6726 && history[0].frag->fr_type == rs_machine_dependent)
6727 return FALSE;
6728
bcd530a7
RS
6729 /* We do not swap with instructions that cannot architecturally
6730 be placed in a branch delay slot, such as SYNC or ERET. We
6731 also refrain from swapping with a trap instruction, since it
6732 complicates trap handlers to have the trap instruction be in
6733 a delay slot. */
a4e06468 6734 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6735 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6736 return FALSE;
6737
6738 /* Check for conflicts between the branch and the instructions
6739 before the candidate delay slot. */
6740 if (nops_for_insn (0, history + 1, ip) > 0)
6741 return FALSE;
6742
6743 /* Check for conflicts between the swapped sequence and the
6744 target of the branch. */
6745 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6746 return FALSE;
6747
6748 /* If the branch reads a register that the previous
6749 instruction sets, we can not swap. */
6750 gpr_read = gpr_read_mask (ip);
6751 prev_gpr_write = gpr_write_mask (&history[0]);
6752 if (gpr_read & prev_gpr_write)
6753 return FALSE;
6754
9d5de888
CF
6755 fpr_read = fpr_read_mask (ip);
6756 prev_fpr_write = fpr_write_mask (&history[0]);
6757 if (fpr_read & prev_fpr_write)
6758 return FALSE;
6759
a4e06468
RS
6760 /* If the branch writes a register that the previous
6761 instruction sets, we can not swap. */
6762 gpr_write = gpr_write_mask (ip);
6763 if (gpr_write & prev_gpr_write)
6764 return FALSE;
6765
6766 /* If the branch writes a register that the previous
6767 instruction reads, we can not swap. */
6768 prev_gpr_read = gpr_read_mask (&history[0]);
6769 if (gpr_write & prev_gpr_read)
6770 return FALSE;
6771
6772 /* If one instruction sets a condition code and the
6773 other one uses a condition code, we can not swap. */
6774 pinfo = ip->insn_mo->pinfo;
6775 if ((pinfo & INSN_READ_COND_CODE)
6776 && (prev_pinfo & INSN_WRITE_COND_CODE))
6777 return FALSE;
6778 if ((pinfo & INSN_WRITE_COND_CODE)
6779 && (prev_pinfo & INSN_READ_COND_CODE))
6780 return FALSE;
6781
6782 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6783 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6784 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6785 return FALSE;
a4e06468 6786
df58fc94
RS
6787 /* If the previous instruction has an incorrect size for a fixed
6788 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6789 pinfo2 = ip->insn_mo->pinfo2;
6790 if (mips_opts.micromips
6791 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6792 && insn_length (history) != 2)
6793 return FALSE;
6794 if (mips_opts.micromips
6795 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6796 && insn_length (history) != 4)
6797 return FALSE;
6798
e407c74b
NC
6799 /* On R5900 short loops need to be fixed by inserting a nop in
6800 the branch delay slots.
6801 A short loop can be terminated too early. */
6802 if (mips_opts.arch == CPU_R5900
6803 /* Check if instruction has a parameter, ignore "j $31". */
6804 && (address_expr != NULL)
6805 /* Parameter must be 16 bit. */
6806 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6807 /* Branch to same segment. */
41065f5e 6808 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6809 /* Branch to same code fragment. */
41065f5e 6810 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6811 /* Can only calculate branch offset if value is known. */
41065f5e 6812 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6813 /* Check if branch is really conditional. */
6814 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6815 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6816 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6817 {
6818 int distance;
6819 /* Check if loop is shorter than 6 instructions including
6820 branch and delay slot. */
41065f5e 6821 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6822 if (distance <= 20)
6823 {
6824 int i;
6825 int rv;
6826
6827 rv = FALSE;
6828 /* When the loop includes branches or jumps,
6829 it is not a short loop. */
6830 for (i = 0; i < (distance / 4); i++)
6831 {
6832 if ((history[i].cleared_p)
41065f5e 6833 || delayed_branch_p (&history[i]))
e407c74b
NC
6834 {
6835 rv = TRUE;
6836 break;
6837 }
6838 }
6839 if (rv == FALSE)
6840 {
6841 /* Insert nop after branch to fix short loop. */
6842 return FALSE;
6843 }
6844 }
6845 }
6846
a4e06468
RS
6847 return TRUE;
6848}
6849
e407c74b
NC
6850/* Decide how we should add IP to the instruction stream.
6851 ADDRESS_EXPR is an operand of the instruction to be used with
6852 RELOC_TYPE. */
a4e06468
RS
6853
6854static enum append_method
e407c74b 6855get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6856 bfd_reloc_code_real_type *reloc_type)
a4e06468 6857{
a4e06468
RS
6858 /* The relaxed version of a macro sequence must be inherently
6859 hazard-free. */
6860 if (mips_relax.sequence == 2)
6861 return APPEND_ADD;
6862
3b821a28 6863 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
6864 if (mips_opts.noreorder)
6865 return APPEND_ADD;
6866
6867 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6868 if (delayed_branch_p (ip))
a4e06468 6869 {
e407c74b
NC
6870 if (!branch_likely_p (ip)
6871 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6872 return APPEND_SWAP;
6873
6874 if (mips_opts.mips16
6875 && ISA_SUPPORTS_MIPS16E
fc76e730 6876 && gpr_read_mask (ip) != 0)
a4e06468
RS
6877 return APPEND_ADD_COMPACT;
6878
7bd374a4
MR
6879 if (mips_opts.micromips
6880 && ((ip->insn_opcode & 0xffe0) == 0x4580
6881 || (!forced_insn_length
6882 && ((ip->insn_opcode & 0xfc00) == 0xcc00
6883 || (ip->insn_opcode & 0xdc00) == 0x8c00))
6884 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
6885 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
6886 return APPEND_ADD_COMPACT;
6887
a4e06468
RS
6888 return APPEND_ADD_WITH_NOP;
6889 }
6890
a4e06468
RS
6891 return APPEND_ADD;
6892}
6893
7bd374a4
MR
6894/* IP is an instruction whose opcode we have just changed, END points
6895 to the end of the opcode table processed. Point IP->insn_mo to the
6896 new opcode's definition. */
ceb94aa5
RS
6897
6898static void
7bd374a4 6899find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 6900{
7bd374a4 6901 const struct mips_opcode *mo;
ceb94aa5 6902
ceb94aa5 6903 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
6904 if (mo->pinfo != INSN_MACRO
6905 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
6906 {
6907 ip->insn_mo = mo;
6908 return;
6909 }
6910 abort ();
6911}
6912
7bd374a4
MR
6913/* IP is a MIPS16 instruction whose opcode we have just changed.
6914 Point IP->insn_mo to the new opcode's definition. */
6915
6916static void
6917find_altered_mips16_opcode (struct mips_cl_insn *ip)
6918{
6919 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
6920}
6921
6922/* IP is a microMIPS instruction whose opcode we have just changed.
6923 Point IP->insn_mo to the new opcode's definition. */
6924
6925static void
6926find_altered_micromips_opcode (struct mips_cl_insn *ip)
6927{
6928 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
6929}
6930
df58fc94
RS
6931/* For microMIPS macros, we need to generate a local number label
6932 as the target of branches. */
6933#define MICROMIPS_LABEL_CHAR '\037'
6934static unsigned long micromips_target_label;
6935static char micromips_target_name[32];
6936
6937static char *
6938micromips_label_name (void)
6939{
6940 char *p = micromips_target_name;
6941 char symbol_name_temporary[24];
6942 unsigned long l;
6943 int i;
6944
6945 if (*p)
6946 return p;
6947
6948 i = 0;
6949 l = micromips_target_label;
6950#ifdef LOCAL_LABEL_PREFIX
6951 *p++ = LOCAL_LABEL_PREFIX;
6952#endif
6953 *p++ = 'L';
6954 *p++ = MICROMIPS_LABEL_CHAR;
6955 do
6956 {
6957 symbol_name_temporary[i++] = l % 10 + '0';
6958 l /= 10;
6959 }
6960 while (l != 0);
6961 while (i > 0)
6962 *p++ = symbol_name_temporary[--i];
6963 *p = '\0';
6964
6965 return micromips_target_name;
6966}
6967
6968static void
6969micromips_label_expr (expressionS *label_expr)
6970{
6971 label_expr->X_op = O_symbol;
6972 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6973 label_expr->X_add_number = 0;
6974}
6975
6976static void
6977micromips_label_inc (void)
6978{
6979 micromips_target_label++;
6980 *micromips_target_name = '\0';
6981}
6982
6983static void
6984micromips_add_label (void)
6985{
6986 symbolS *s;
6987
6988 s = colon (micromips_label_name ());
6989 micromips_label_inc ();
f3ded42a 6990 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6991}
6992
6993/* If assembling microMIPS code, then return the microMIPS reloc
6994 corresponding to the requested one if any. Otherwise return
6995 the reloc unchanged. */
6996
6997static bfd_reloc_code_real_type
6998micromips_map_reloc (bfd_reloc_code_real_type reloc)
6999{
7000 static const bfd_reloc_code_real_type relocs[][2] =
7001 {
7002 /* Keep sorted incrementally by the left-hand key. */
7003 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7004 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7005 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7006 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7007 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7008 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7009 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7010 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7011 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7012 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7013 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7014 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7015 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7016 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7017 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7018 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7019 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7020 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7021 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7022 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7023 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7024 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7025 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7026 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7027 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7028 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7029 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7030 };
7031 bfd_reloc_code_real_type r;
7032 size_t i;
7033
7034 if (!mips_opts.micromips)
7035 return reloc;
7036 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7037 {
7038 r = relocs[i][0];
7039 if (r > reloc)
7040 return reloc;
7041 if (r == reloc)
7042 return relocs[i][1];
7043 }
7044 return reloc;
7045}
7046
b886a2ab
RS
7047/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7048 Return true on success, storing the resolved value in RESULT. */
7049
7050static bfd_boolean
7051calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7052 offsetT *result)
7053{
7054 switch (reloc)
7055 {
7056 case BFD_RELOC_MIPS_HIGHEST:
7057 case BFD_RELOC_MICROMIPS_HIGHEST:
7058 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7059 return TRUE;
7060
7061 case BFD_RELOC_MIPS_HIGHER:
7062 case BFD_RELOC_MICROMIPS_HIGHER:
7063 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7064 return TRUE;
7065
7066 case BFD_RELOC_HI16_S:
41947d9e 7067 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7068 case BFD_RELOC_MICROMIPS_HI16_S:
7069 case BFD_RELOC_MIPS16_HI16_S:
7070 *result = ((operand + 0x8000) >> 16) & 0xffff;
7071 return TRUE;
7072
7073 case BFD_RELOC_HI16:
7074 case BFD_RELOC_MICROMIPS_HI16:
7075 case BFD_RELOC_MIPS16_HI16:
7076 *result = (operand >> 16) & 0xffff;
7077 return TRUE;
7078
7079 case BFD_RELOC_LO16:
41947d9e 7080 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7081 case BFD_RELOC_MICROMIPS_LO16:
7082 case BFD_RELOC_MIPS16_LO16:
7083 *result = operand & 0xffff;
7084 return TRUE;
7085
7086 case BFD_RELOC_UNUSED:
7087 *result = operand;
7088 return TRUE;
7089
7090 default:
7091 return FALSE;
7092 }
7093}
7094
71400594
RS
7095/* Output an instruction. IP is the instruction information.
7096 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7097 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7098 a macro expansion. */
71400594
RS
7099
7100static void
7101append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7102 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7103{
14fe068b 7104 unsigned long prev_pinfo2, pinfo;
71400594 7105 bfd_boolean relaxed_branch = FALSE;
a4e06468 7106 enum append_method method;
2309ddf2 7107 bfd_boolean relax32;
2b0c8b40 7108 int branch_disp;
71400594 7109
2309ddf2 7110 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7111 fix_loongson2f (ip);
7112
738f4d98 7113 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7114 file_ase_micromips |= mips_opts.micromips;
738f4d98 7115
df58fc94 7116 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7117 pinfo = ip->insn_mo->pinfo;
df58fc94 7118
7bd374a4
MR
7119 /* Don't raise alarm about `nods' frags as they'll fill in the right
7120 kind of nop in relaxation if required. */
df58fc94
RS
7121 if (mips_opts.micromips
7122 && !expansionp
7bd374a4
MR
7123 && !(history[0].frag
7124 && history[0].frag->fr_type == rs_machine_dependent
7125 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7126 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7127 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7128 && micromips_insn_length (ip->insn_mo) != 2)
7129 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7130 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7131 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7132 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7133
15be625d
CM
7134 if (address_expr == NULL)
7135 ip->complete_p = 1;
b886a2ab
RS
7136 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7137 && reloc_type[1] == BFD_RELOC_UNUSED
7138 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7139 && address_expr->X_op == O_constant)
7140 {
15be625d
CM
7141 switch (*reloc_type)
7142 {
15be625d 7143 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7144 {
7145 int shift;
7146
17c6c9d9
MR
7147 /* Shift is 2, unusually, for microMIPS JALX. */
7148 shift = (mips_opts.micromips
7149 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7150 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7151 as_bad (_("jump to misaligned address (0x%lx)"),
7152 (unsigned long) address_expr->X_add_number);
7153 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7154 & 0x3ffffff);
335574df 7155 ip->complete_p = 1;
df58fc94 7156 }
15be625d
CM
7157 break;
7158
7159 case BFD_RELOC_MIPS16_JMP:
7160 if ((address_expr->X_add_number & 3) != 0)
7161 as_bad (_("jump to misaligned address (0x%lx)"),
7162 (unsigned long) address_expr->X_add_number);
7163 ip->insn_opcode |=
7164 (((address_expr->X_add_number & 0x7c0000) << 3)
7165 | ((address_expr->X_add_number & 0xf800000) >> 7)
7166 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7167 ip->complete_p = 1;
15be625d
CM
7168 break;
7169
7170 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7171 {
7172 int shift;
7173
7174 shift = mips_opts.micromips ? 1 : 2;
7175 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7176 as_bad (_("branch to misaligned address (0x%lx)"),
7177 (unsigned long) address_expr->X_add_number);
7178 if (!mips_relax_branch)
7179 {
7180 if ((address_expr->X_add_number + (1 << (shift + 15)))
7181 & ~((1 << (shift + 16)) - 1))
7182 as_bad (_("branch address range overflow (0x%lx)"),
7183 (unsigned long) address_expr->X_add_number);
7184 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7185 & 0xffff);
7186 }
df58fc94 7187 }
15be625d
CM
7188 break;
7189
7361da2c
AB
7190 case BFD_RELOC_MIPS_21_PCREL_S2:
7191 {
7192 int shift;
7193
7194 shift = 2;
7195 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7196 as_bad (_("branch to misaligned address (0x%lx)"),
7197 (unsigned long) address_expr->X_add_number);
7198 if ((address_expr->X_add_number + (1 << (shift + 20)))
7199 & ~((1 << (shift + 21)) - 1))
7200 as_bad (_("branch address range overflow (0x%lx)"),
7201 (unsigned long) address_expr->X_add_number);
7202 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7203 & 0x1fffff);
7204 }
7205 break;
7206
7207 case BFD_RELOC_MIPS_26_PCREL_S2:
7208 {
7209 int shift;
7210
7211 shift = 2;
7212 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7213 as_bad (_("branch to misaligned address (0x%lx)"),
7214 (unsigned long) address_expr->X_add_number);
7215 if ((address_expr->X_add_number + (1 << (shift + 25)))
7216 & ~((1 << (shift + 26)) - 1))
7217 as_bad (_("branch address range overflow (0x%lx)"),
7218 (unsigned long) address_expr->X_add_number);
7219 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7220 & 0x3ffffff);
7221 }
7222 break;
7223
15be625d 7224 default:
b886a2ab
RS
7225 {
7226 offsetT value;
7227
7228 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7229 &value))
7230 {
7231 ip->insn_opcode |= value & 0xffff;
7232 ip->complete_p = 1;
7233 }
7234 }
7235 break;
7236 }
15be625d
CM
7237 }
7238
71400594
RS
7239 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7240 {
7241 /* There are a lot of optimizations we could do that we don't.
7242 In particular, we do not, in general, reorder instructions.
7243 If you use gcc with optimization, it will reorder
7244 instructions and generally do much more optimization then we
7245 do here; repeating all that work in the assembler would only
7246 benefit hand written assembly code, and does not seem worth
7247 it. */
7248 int nops = (mips_optimize == 0
932d1a1b
RS
7249 ? nops_for_insn (0, history, NULL)
7250 : nops_for_insn_or_target (0, history, ip));
71400594 7251 if (nops > 0)
252b5132
RH
7252 {
7253 fragS *old_frag;
7254 unsigned long old_frag_offset;
7255 int i;
252b5132
RH
7256
7257 old_frag = frag_now;
7258 old_frag_offset = frag_now_fix ();
7259
7260 for (i = 0; i < nops; i++)
14fe068b
RS
7261 add_fixed_insn (NOP_INSN);
7262 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7263
7264 if (listing)
7265 {
7266 listing_prev_line ();
7267 /* We may be at the start of a variant frag. In case we
7268 are, make sure there is enough space for the frag
7269 after the frags created by listing_prev_line. The
7270 argument to frag_grow here must be at least as large
7271 as the argument to all other calls to frag_grow in
7272 this file. We don't have to worry about being in the
7273 middle of a variant frag, because the variants insert
7274 all needed nop instructions themselves. */
7275 frag_grow (40);
7276 }
7277
462427c4 7278 mips_move_text_labels ();
252b5132
RH
7279
7280#ifndef NO_ECOFF_DEBUGGING
7281 if (ECOFF_DEBUGGING)
7282 ecoff_fix_loc (old_frag, old_frag_offset);
7283#endif
7284 }
71400594
RS
7285 }
7286 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7287 {
932d1a1b
RS
7288 int nops;
7289
7290 /* Work out how many nops in prev_nop_frag are needed by IP,
7291 ignoring hazards generated by the first prev_nop_frag_since
7292 instructions. */
7293 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7294 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7295
71400594
RS
7296 /* Enforce NOPS as a minimum. */
7297 if (nops > prev_nop_frag_required)
7298 prev_nop_frag_required = nops;
252b5132 7299
71400594
RS
7300 if (prev_nop_frag_holds == prev_nop_frag_required)
7301 {
7302 /* Settle for the current number of nops. Update the history
7303 accordingly (for the benefit of any future .set reorder code). */
7304 prev_nop_frag = NULL;
7305 insert_into_history (prev_nop_frag_since,
7306 prev_nop_frag_holds, NOP_INSN);
7307 }
7308 else
7309 {
7310 /* Allow this instruction to replace one of the nops that was
7311 tentatively added to prev_nop_frag. */
df58fc94 7312 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7313 prev_nop_frag_holds--;
7314 prev_nop_frag_since++;
252b5132
RH
7315 }
7316 }
7317
e407c74b 7318 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7319 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7320
e410add4
RS
7321 dwarf2_emit_insn (0);
7322 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7323 so "move" the instruction address accordingly.
7324
7325 Also, it doesn't seem appropriate for the assembler to reorder .loc
7326 entries. If this instruction is a branch that we are going to swap
7327 with the previous instruction, the two instructions should be
7328 treated as a unit, and the debug information for both instructions
7329 should refer to the start of the branch sequence. Using the
7330 current position is certainly wrong when swapping a 32-bit branch
7331 and a 16-bit delay slot, since the current position would then be
7332 in the middle of a branch. */
7333 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7334
df58fc94
RS
7335 relax32 = (mips_relax_branch
7336 /* Don't try branch relaxation within .set nomacro, or within
7337 .set noat if we use $at for PIC computations. If it turns
7338 out that the branch was out-of-range, we'll get an error. */
7339 && !mips_opts.warn_about_macros
7340 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7341 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7342 as they have no complementing branches. */
7343 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7344
7345 if (!HAVE_CODE_COMPRESSION
7346 && address_expr
7347 && relax32
0b25d3e6 7348 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7349 && delayed_branch_p (ip))
4a6a3df4 7350 {
895921c9 7351 relaxed_branch = TRUE;
1e915849
RS
7352 add_relaxed_insn (ip, (relaxed_branch_length
7353 (NULL, NULL,
11625dd8
RS
7354 uncond_branch_p (ip) ? -1
7355 : branch_likely_p (ip) ? 1
1e915849
RS
7356 : 0)), 4,
7357 RELAX_BRANCH_ENCODE
ce8ad872 7358 (AT, mips_pic != NO_PIC,
11625dd8
RS
7359 uncond_branch_p (ip),
7360 branch_likely_p (ip),
1e915849
RS
7361 pinfo & INSN_WRITE_GPR_31,
7362 0),
7363 address_expr->X_add_symbol,
7364 address_expr->X_add_number);
4a6a3df4
AO
7365 *reloc_type = BFD_RELOC_UNUSED;
7366 }
df58fc94
RS
7367 else if (mips_opts.micromips
7368 && address_expr
7369 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7370 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7371 && (delayed_branch_p (ip) || compact_branch_p (ip))
7372 /* Don't try branch relaxation when users specify
7373 16-bit/32-bit instructions. */
7374 && !forced_insn_length)
df58fc94 7375 {
7bd374a4
MR
7376 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7377 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7378 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7379 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7380 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7381 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7382 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7383 int length32 = nods ? 8 : 4;
df58fc94
RS
7384
7385 gas_assert (address_expr != NULL);
7386 gas_assert (!mips_relax.sequence);
7387
2b0c8b40 7388 relaxed_branch = TRUE;
7bd374a4
MR
7389 if (nods)
7390 method = APPEND_ADD;
7391 if (relax32)
7392 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7393 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7394 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7395 mips_pic != NO_PIC,
7bd374a4 7396 uncond, compact, al, nods,
40209cad 7397 relax32, 0, 0),
df58fc94
RS
7398 address_expr->X_add_symbol,
7399 address_expr->X_add_number);
7400 *reloc_type = BFD_RELOC_UNUSED;
7401 }
7402 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7403 {
7fd53920
MR
7404 bfd_boolean require_unextended;
7405 bfd_boolean require_extended;
88a7ef16
MR
7406 symbolS *symbol;
7407 offsetT offset;
7408
7fd53920
MR
7409 if (forced_insn_length != 0)
7410 {
7411 require_unextended = forced_insn_length == 2;
7412 require_extended = forced_insn_length == 4;
7413 }
7414 else
7415 {
7416 require_unextended = (mips_opts.noautoextend
7417 && !mips_opcode_32bit_p (ip->insn_mo));
7418 require_extended = 0;
7419 }
7420
252b5132 7421 /* We need to set up a variant frag. */
df58fc94 7422 gas_assert (address_expr != NULL);
88a7ef16
MR
7423 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7424 symbol created by `make_expr_symbol' may not get a necessary
7425 external relocation produced. */
7426 if (address_expr->X_op == O_symbol)
7427 {
7428 symbol = address_expr->X_add_symbol;
7429 offset = address_expr->X_add_number;
7430 }
7431 else
7432 {
7433 symbol = make_expr_symbol (address_expr);
82d808ed 7434 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7435 offset = 0;
7436 }
8507b6e7 7437 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7438 RELAX_MIPS16_ENCODE
7439 (*reloc_type - BFD_RELOC_UNUSED,
8507b6e7
MR
7440 mips_pic != NO_PIC,
7441 HAVE_32BIT_SYMBOLS,
7442 mips_opts.warn_about_macros,
7fd53920 7443 require_unextended, require_extended,
11625dd8 7444 delayed_branch_p (&history[0]),
1e915849 7445 history[0].mips16_absolute_jump_p),
88a7ef16 7446 symbol, offset);
252b5132 7447 }
5c04167a 7448 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7449 {
11625dd8 7450 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7451 /* Make sure there is enough room to swap this instruction with
7452 a following jump instruction. */
7453 frag_grow (6);
1e915849 7454 add_fixed_insn (ip);
252b5132
RH
7455 }
7456 else
7457 {
7458 if (mips_opts.mips16
7459 && mips_opts.noreorder
11625dd8 7460 && delayed_branch_p (&history[0]))
252b5132
RH
7461 as_warn (_("extended instruction in delay slot"));
7462
4d7206a2
RS
7463 if (mips_relax.sequence)
7464 {
7465 /* If we've reached the end of this frag, turn it into a variant
7466 frag and record the information for the instructions we've
7467 written so far. */
7468 if (frag_room () < 4)
7469 relax_close_frag ();
df58fc94 7470 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7471 }
7472
584892a6 7473 if (mips_relax.sequence != 2)
df58fc94
RS
7474 {
7475 if (mips_macro_warning.first_insn_sizes[0] == 0)
7476 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7477 mips_macro_warning.sizes[0] += insn_length (ip);
7478 mips_macro_warning.insns[0]++;
7479 }
584892a6 7480 if (mips_relax.sequence != 1)
df58fc94
RS
7481 {
7482 if (mips_macro_warning.first_insn_sizes[1] == 0)
7483 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7484 mips_macro_warning.sizes[1] += insn_length (ip);
7485 mips_macro_warning.insns[1]++;
7486 }
584892a6 7487
1e915849
RS
7488 if (mips_opts.mips16)
7489 {
7490 ip->fixed_p = 1;
7491 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7492 }
7493 add_fixed_insn (ip);
252b5132
RH
7494 }
7495
9fe77896 7496 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7497 {
df58fc94 7498 bfd_reloc_code_real_type final_type[3];
2309ddf2 7499 reloc_howto_type *howto0;
9fe77896
RS
7500 reloc_howto_type *howto;
7501 int i;
34ce925e 7502
df58fc94
RS
7503 /* Perform any necessary conversion to microMIPS relocations
7504 and find out how many relocations there actually are. */
7505 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7506 final_type[i] = micromips_map_reloc (reloc_type[i]);
7507
9fe77896
RS
7508 /* In a compound relocation, it is the final (outermost)
7509 operator that determines the relocated field. */
2309ddf2 7510 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7511 if (!howto)
7512 abort ();
2309ddf2
MR
7513
7514 if (i > 1)
7515 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7516 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7517 bfd_get_reloc_size (howto),
7518 address_expr,
2309ddf2
MR
7519 howto0 && howto0->pc_relative,
7520 final_type[0]);
ce8ad872
MR
7521 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7522 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7523
7524 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7525 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7526 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7527
7528 /* These relocations can have an addend that won't fit in
7529 4 octets for 64bit assembly. */
bad1aba3 7530 if (GPR_SIZE == 64
9fe77896
RS
7531 && ! howto->partial_inplace
7532 && (reloc_type[0] == BFD_RELOC_16
7533 || reloc_type[0] == BFD_RELOC_32
7534 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7535 || reloc_type[0] == BFD_RELOC_GPREL16
7536 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7537 || reloc_type[0] == BFD_RELOC_GPREL32
7538 || reloc_type[0] == BFD_RELOC_64
7539 || reloc_type[0] == BFD_RELOC_CTOR
7540 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7541 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7542 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7543 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7544 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7545 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7546 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7547 || hi16_reloc_p (reloc_type[0])
7548 || lo16_reloc_p (reloc_type[0])))
7549 ip->fixp[0]->fx_no_overflow = 1;
7550
ddaf2c41
MR
7551 /* These relocations can have an addend that won't fit in 2 octets. */
7552 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7553 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7554 ip->fixp[0]->fx_no_overflow = 1;
7555
9fe77896
RS
7556 if (mips_relax.sequence)
7557 {
7558 if (mips_relax.first_fixup == 0)
7559 mips_relax.first_fixup = ip->fixp[0];
7560 }
7561 else if (reloc_needs_lo_p (*reloc_type))
7562 {
7563 struct mips_hi_fixup *hi_fixup;
7564
7565 /* Reuse the last entry if it already has a matching %lo. */
7566 hi_fixup = mips_hi_fixup_list;
7567 if (hi_fixup == 0
7568 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7569 {
325801bd 7570 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7571 hi_fixup->next = mips_hi_fixup_list;
7572 mips_hi_fixup_list = hi_fixup;
4d7206a2 7573 }
9fe77896
RS
7574 hi_fixup->fixp = ip->fixp[0];
7575 hi_fixup->seg = now_seg;
7576 }
252b5132 7577
9fe77896
RS
7578 /* Add fixups for the second and third relocations, if given.
7579 Note that the ABI allows the second relocation to be
7580 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7581 moment we only use RSS_UNDEF, but we could add support
7582 for the others if it ever becomes necessary. */
7583 for (i = 1; i < 3; i++)
7584 if (reloc_type[i] != BFD_RELOC_UNUSED)
7585 {
7586 ip->fixp[i] = fix_new (ip->frag, ip->where,
7587 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7588 FALSE, final_type[i]);
f6688943 7589
9fe77896
RS
7590 /* Use fx_tcbit to mark compound relocs. */
7591 ip->fixp[0]->fx_tcbit = 1;
7592 ip->fixp[i]->fx_tcbit = 1;
7593 }
252b5132 7594 }
252b5132
RH
7595
7596 /* Update the register mask information. */
4c260379
RS
7597 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7598 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7599
a4e06468 7600 switch (method)
252b5132 7601 {
a4e06468
RS
7602 case APPEND_ADD:
7603 insert_into_history (0, 1, ip);
7604 break;
7605
7606 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7607 {
7608 struct mips_cl_insn *nop;
7609
7610 insert_into_history (0, 1, ip);
7611 nop = get_delay_slot_nop (ip);
7612 add_fixed_insn (nop);
7613 insert_into_history (0, 1, nop);
7614 if (mips_relax.sequence)
7615 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7616 }
a4e06468
RS
7617 break;
7618
7619 case APPEND_ADD_COMPACT:
7620 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7621 if (mips_opts.mips16)
7622 {
7623 ip->insn_opcode |= 0x0080;
7624 find_altered_mips16_opcode (ip);
7625 }
7626 /* Convert microMIPS instructions. */
7627 else if (mips_opts.micromips)
7628 {
7629 /* jr16->jrc */
7630 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7631 ip->insn_opcode |= 0x0020;
7632 /* b16->bc */
7633 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7634 ip->insn_opcode = 0x40e00000;
7635 /* beqz16->beqzc, bnez16->bnezc */
7636 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7637 {
7638 unsigned long regno;
7639
7640 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7641 regno &= MICROMIPSOP_MASK_MD;
7642 regno = micromips_to_32_reg_d_map[regno];
7643 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7644 | (regno << MICROMIPSOP_SH_RS)
7645 | 0x40a00000) ^ 0x00400000;
7646 }
7647 /* beqz->beqzc, bnez->bnezc */
7648 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7649 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7650 | ((ip->insn_opcode >> 7) & 0x00400000)
7651 | 0x40a00000) ^ 0x00400000;
7652 /* beq $0->beqzc, bne $0->bnezc */
7653 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7654 ip->insn_opcode = (((ip->insn_opcode >>
7655 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7656 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7657 | ((ip->insn_opcode >> 7) & 0x00400000)
7658 | 0x40a00000) ^ 0x00400000;
7659 else
7660 abort ();
7661 find_altered_micromips_opcode (ip);
7662 }
7663 else
7664 abort ();
a4e06468
RS
7665 install_insn (ip);
7666 insert_into_history (0, 1, ip);
7667 break;
7668
7669 case APPEND_SWAP:
7670 {
7671 struct mips_cl_insn delay = history[0];
99e7978b
MF
7672
7673 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7674 {
7675 /* Add the delay slot instruction to the end of the
7676 current frag and shrink the fixed part of the
7677 original frag. If the branch occupies the tail of
7678 the latter, move it backwards to cover the gap. */
2b0c8b40 7679 delay.frag->fr_fix -= branch_disp;
a4e06468 7680 if (delay.frag == ip->frag)
2b0c8b40 7681 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7682 add_fixed_insn (&delay);
7683 }
7684 else
7685 {
5e35670b
MR
7686 /* If this is not a relaxed branch and we are in the
7687 same frag, then just swap the instructions. */
7688 move_insn (ip, delay.frag, delay.where);
7689 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7690 }
7691 history[0] = *ip;
7692 delay.fixed_p = 1;
7693 insert_into_history (0, 1, &delay);
7694 }
7695 break;
252b5132
RH
7696 }
7697
13408f1e 7698 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7699 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7700 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7701 {
7702 unsigned int i;
7703
79850f26 7704 mips_no_prev_insn ();
13408f1e 7705
e407c74b 7706 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7707 history[i].cleared_p = 1;
e407c74b
NC
7708 }
7709
df58fc94
RS
7710 /* We need to emit a label at the end of branch-likely macros. */
7711 if (emit_branch_likely_macro)
7712 {
7713 emit_branch_likely_macro = FALSE;
7714 micromips_add_label ();
7715 }
7716
252b5132
RH
7717 /* We just output an insn, so the next one doesn't have a label. */
7718 mips_clear_insn_labels ();
252b5132
RH
7719}
7720
e407c74b
NC
7721/* Forget that there was any previous instruction or label.
7722 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7723
7724static void
7d10b47d 7725mips_no_prev_insn (void)
252b5132 7726{
7d10b47d
RS
7727 prev_nop_frag = NULL;
7728 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7729 mips_clear_insn_labels ();
7730}
7731
7d10b47d
RS
7732/* This function must be called before we emit something other than
7733 instructions. It is like mips_no_prev_insn except that it inserts
7734 any NOPS that might be needed by previous instructions. */
252b5132 7735
7d10b47d
RS
7736void
7737mips_emit_delays (void)
252b5132
RH
7738{
7739 if (! mips_opts.noreorder)
7740 {
932d1a1b 7741 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7742 if (nops > 0)
7743 {
7d10b47d
RS
7744 while (nops-- > 0)
7745 add_fixed_insn (NOP_INSN);
462427c4 7746 mips_move_text_labels ();
7d10b47d
RS
7747 }
7748 }
7749 mips_no_prev_insn ();
7750}
7751
7752/* Start a (possibly nested) noreorder block. */
7753
7754static void
7755start_noreorder (void)
7756{
7757 if (mips_opts.noreorder == 0)
7758 {
7759 unsigned int i;
7760 int nops;
7761
7762 /* None of the instructions before the .set noreorder can be moved. */
7763 for (i = 0; i < ARRAY_SIZE (history); i++)
7764 history[i].fixed_p = 1;
7765
7766 /* Insert any nops that might be needed between the .set noreorder
7767 block and the previous instructions. We will later remove any
7768 nops that turn out not to be needed. */
932d1a1b 7769 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7770 if (nops > 0)
7771 {
7772 if (mips_optimize != 0)
252b5132
RH
7773 {
7774 /* Record the frag which holds the nop instructions, so
7775 that we can remove them if we don't need them. */
df58fc94 7776 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7777 prev_nop_frag = frag_now;
7778 prev_nop_frag_holds = nops;
7779 prev_nop_frag_required = 0;
7780 prev_nop_frag_since = 0;
7781 }
7782
7783 for (; nops > 0; --nops)
1e915849 7784 add_fixed_insn (NOP_INSN);
252b5132 7785
7d10b47d
RS
7786 /* Move on to a new frag, so that it is safe to simply
7787 decrease the size of prev_nop_frag. */
7788 frag_wane (frag_now);
7789 frag_new (0);
462427c4 7790 mips_move_text_labels ();
252b5132 7791 }
df58fc94 7792 mips_mark_labels ();
7d10b47d 7793 mips_clear_insn_labels ();
252b5132 7794 }
7d10b47d
RS
7795 mips_opts.noreorder++;
7796 mips_any_noreorder = 1;
7797}
252b5132 7798
7d10b47d 7799/* End a nested noreorder block. */
252b5132 7800
7d10b47d
RS
7801static void
7802end_noreorder (void)
7803{
7804 mips_opts.noreorder--;
7805 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7806 {
7807 /* Commit to inserting prev_nop_frag_required nops and go back to
7808 handling nop insertion the .set reorder way. */
7809 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7810 * NOP_INSN_SIZE);
7d10b47d
RS
7811 insert_into_history (prev_nop_frag_since,
7812 prev_nop_frag_required, NOP_INSN);
7813 prev_nop_frag = NULL;
7814 }
252b5132
RH
7815}
7816
97d87491
RS
7817/* Sign-extend 32-bit mode constants that have bit 31 set and all
7818 higher bits unset. */
7819
7820static void
7821normalize_constant_expr (expressionS *ex)
7822{
7823 if (ex->X_op == O_constant
7824 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7825 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7826 - 0x80000000);
7827}
7828
7829/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7830 all higher bits unset. */
7831
7832static void
7833normalize_address_expr (expressionS *ex)
7834{
7835 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7836 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7837 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7838 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7839 - 0x80000000);
7840}
7841
7842/* Try to match TOKENS against OPCODE, storing the result in INSN.
7843 Return true if the match was successful.
7844
7845 OPCODE_EXTRA is a value that should be ORed into the opcode
7846 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7847 there are more alternatives after OPCODE and SOFT_MATCH is
7848 as for mips_arg_info. */
7849
7850static bfd_boolean
7851match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7852 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7853 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7854{
7855 const char *args;
7856 struct mips_arg_info arg;
7857 const struct mips_operand *operand;
7858 char c;
7859
7860 imm_expr.X_op = O_absent;
97d87491
RS
7861 offset_expr.X_op = O_absent;
7862 offset_reloc[0] = BFD_RELOC_UNUSED;
7863 offset_reloc[1] = BFD_RELOC_UNUSED;
7864 offset_reloc[2] = BFD_RELOC_UNUSED;
7865
7866 create_insn (insn, opcode);
60f20e8b
RS
7867 /* When no opcode suffix is specified, assume ".xyzw". */
7868 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7869 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7870 else
7871 insn->insn_opcode |= opcode_extra;
97d87491
RS
7872 memset (&arg, 0, sizeof (arg));
7873 arg.insn = insn;
7874 arg.token = tokens;
7875 arg.argnum = 1;
7876 arg.last_regno = ILLEGAL_REG;
7877 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7878 arg.lax_match = lax_match;
97d87491
RS
7879 for (args = opcode->args;; ++args)
7880 {
7881 if (arg.token->type == OT_END)
7882 {
7883 /* Handle unary instructions in which only one operand is given.
7884 The source is then the same as the destination. */
7885 if (arg.opnum == 1 && *args == ',')
7886 {
7887 operand = (mips_opts.micromips
7888 ? decode_micromips_operand (args + 1)
7889 : decode_mips_operand (args + 1));
7890 if (operand && mips_optional_operand_p (operand))
7891 {
7892 arg.token = tokens;
7893 arg.argnum = 1;
7894 continue;
7895 }
7896 }
7897
7898 /* Treat elided base registers as $0. */
7899 if (strcmp (args, "(b)") == 0)
7900 args += 3;
7901
7902 if (args[0] == '+')
7903 switch (args[1])
7904 {
7905 case 'K':
7906 case 'N':
7907 /* The register suffix is optional. */
7908 args += 2;
7909 break;
7910 }
7911
7912 /* Fail the match if there were too few operands. */
7913 if (*args)
7914 return FALSE;
7915
7916 /* Successful match. */
60f20e8b
RS
7917 if (!complete_p)
7918 return TRUE;
e3de51ce 7919 clear_insn_error ();
97d87491
RS
7920 if (arg.dest_regno == arg.last_regno
7921 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7922 {
7923 if (arg.opnum == 2)
e3de51ce 7924 set_insn_error
1661c76c 7925 (0, _("source and destination must be different"));
97d87491 7926 else if (arg.last_regno == 31)
e3de51ce 7927 set_insn_error
1661c76c 7928 (0, _("a destination register must be supplied"));
97d87491 7929 }
173d3447
CF
7930 else if (arg.last_regno == 31
7931 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7932 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7933 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
7934 check_completed_insn (&arg);
7935 return TRUE;
7936 }
7937
7938 /* Fail the match if the line has too many operands. */
7939 if (*args == 0)
7940 return FALSE;
7941
7942 /* Handle characters that need to match exactly. */
7943 if (*args == '(' || *args == ')' || *args == ',')
7944 {
7945 if (match_char (&arg, *args))
7946 continue;
7947 return FALSE;
7948 }
7949 if (*args == '#')
7950 {
7951 ++args;
7952 if (arg.token->type == OT_DOUBLE_CHAR
7953 && arg.token->u.ch == *args)
7954 {
7955 ++arg.token;
7956 continue;
7957 }
7958 return FALSE;
7959 }
7960
7961 /* Handle special macro operands. Work out the properties of
7962 other operands. */
7963 arg.opnum += 1;
97d87491
RS
7964 switch (*args)
7965 {
7361da2c
AB
7966 case '-':
7967 switch (args[1])
7968 {
7969 case 'A':
7970 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7971 break;
7972
7973 case 'B':
7974 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7975 break;
7976 }
7977 break;
7978
97d87491
RS
7979 case '+':
7980 switch (args[1])
7981 {
97d87491
RS
7982 case 'i':
7983 *offset_reloc = BFD_RELOC_MIPS_JMP;
7984 break;
7361da2c
AB
7985
7986 case '\'':
7987 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7988 break;
7989
7990 case '\"':
7991 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7992 break;
97d87491
RS
7993 }
7994 break;
7995
97d87491 7996 case 'I':
1a00e612
RS
7997 if (!match_const_int (&arg, &imm_expr.X_add_number))
7998 return FALSE;
7999 imm_expr.X_op = O_constant;
bad1aba3 8000 if (GPR_SIZE == 32)
97d87491
RS
8001 normalize_constant_expr (&imm_expr);
8002 continue;
8003
8004 case 'A':
8005 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8006 {
8007 /* Assume that the offset has been elided and that what
8008 we saw was a base register. The match will fail later
8009 if that assumption turns out to be wrong. */
8010 offset_expr.X_op = O_constant;
8011 offset_expr.X_add_number = 0;
8012 }
97d87491 8013 else
1a00e612
RS
8014 {
8015 if (!match_expression (&arg, &offset_expr, offset_reloc))
8016 return FALSE;
8017 normalize_address_expr (&offset_expr);
8018 }
97d87491
RS
8019 continue;
8020
8021 case 'F':
8022 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8023 8, TRUE))
1a00e612 8024 return FALSE;
97d87491
RS
8025 continue;
8026
8027 case 'L':
8028 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8029 8, FALSE))
1a00e612 8030 return FALSE;
97d87491
RS
8031 continue;
8032
8033 case 'f':
8034 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8035 4, TRUE))
1a00e612 8036 return FALSE;
97d87491
RS
8037 continue;
8038
8039 case 'l':
8040 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8041 4, FALSE))
1a00e612 8042 return FALSE;
97d87491
RS
8043 continue;
8044
97d87491
RS
8045 case 'p':
8046 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8047 break;
8048
8049 case 'a':
8050 *offset_reloc = BFD_RELOC_MIPS_JMP;
8051 break;
8052
8053 case 'm':
8054 gas_assert (mips_opts.micromips);
8055 c = args[1];
8056 switch (c)
8057 {
8058 case 'D':
8059 case 'E':
8060 if (!forced_insn_length)
8061 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8062 else if (c == 'D')
8063 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8064 else
8065 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8066 break;
8067 }
8068 break;
8069 }
8070
8071 operand = (mips_opts.micromips
8072 ? decode_micromips_operand (args)
8073 : decode_mips_operand (args));
8074 if (!operand)
8075 abort ();
8076
8077 /* Skip prefixes. */
7361da2c 8078 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8079 args++;
8080
8081 if (mips_optional_operand_p (operand)
8082 && args[1] == ','
8083 && (arg.token[0].type != OT_REG
8084 || arg.token[1].type == OT_END))
8085 {
8086 /* Assume that the register has been elided and is the
8087 same as the first operand. */
8088 arg.token = tokens;
8089 arg.argnum = 1;
8090 }
8091
8092 if (!match_operand (&arg, operand))
8093 return FALSE;
8094 }
8095}
8096
8097/* Like match_insn, but for MIPS16. */
8098
8099static bfd_boolean
8100match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8101 struct mips_operand_token *tokens)
97d87491
RS
8102{
8103 const char *args;
8104 const struct mips_operand *operand;
8105 const struct mips_operand *ext_operand;
82d808ed 8106 bfd_boolean pcrel = FALSE;
7fd53920 8107 int required_insn_length;
97d87491
RS
8108 struct mips_arg_info arg;
8109 int relax_char;
8110
7fd53920
MR
8111 if (forced_insn_length)
8112 required_insn_length = forced_insn_length;
8113 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8114 required_insn_length = 2;
8115 else
8116 required_insn_length = 0;
8117
97d87491
RS
8118 create_insn (insn, opcode);
8119 imm_expr.X_op = O_absent;
97d87491
RS
8120 offset_expr.X_op = O_absent;
8121 offset_reloc[0] = BFD_RELOC_UNUSED;
8122 offset_reloc[1] = BFD_RELOC_UNUSED;
8123 offset_reloc[2] = BFD_RELOC_UNUSED;
8124 relax_char = 0;
8125
8126 memset (&arg, 0, sizeof (arg));
8127 arg.insn = insn;
8128 arg.token = tokens;
8129 arg.argnum = 1;
8130 arg.last_regno = ILLEGAL_REG;
8131 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8132 relax_char = 0;
8133 for (args = opcode->args;; ++args)
8134 {
8135 int c;
8136
8137 if (arg.token->type == OT_END)
8138 {
8139 offsetT value;
8140
8141 /* Handle unary instructions in which only one operand is given.
8142 The source is then the same as the destination. */
8143 if (arg.opnum == 1 && *args == ',')
8144 {
8145 operand = decode_mips16_operand (args[1], FALSE);
8146 if (operand && mips_optional_operand_p (operand))
8147 {
8148 arg.token = tokens;
8149 arg.argnum = 1;
8150 continue;
8151 }
8152 }
8153
8154 /* Fail the match if there were too few operands. */
8155 if (*args)
8156 return FALSE;
8157
8158 /* Successful match. Stuff the immediate value in now, if
8159 we can. */
e3de51ce 8160 clear_insn_error ();
97d87491
RS
8161 if (opcode->pinfo == INSN_MACRO)
8162 {
8163 gas_assert (relax_char == 0 || relax_char == 'p');
8164 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8165 }
8166 else if (relax_char
8167 && offset_expr.X_op == O_constant
82d808ed 8168 && !pcrel
97d87491
RS
8169 && calculate_reloc (*offset_reloc,
8170 offset_expr.X_add_number,
8171 &value))
8172 {
8173 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8174 required_insn_length, &insn->insn_opcode);
97d87491
RS
8175 offset_expr.X_op = O_absent;
8176 *offset_reloc = BFD_RELOC_UNUSED;
8177 }
8178 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8179 {
7fd53920 8180 if (required_insn_length == 2)
e3de51ce 8181 set_insn_error (0, _("invalid unextended operand value"));
1da43acc
MR
8182 else
8183 {
8184 forced_insn_length = 4;
8185 insn->insn_opcode |= MIPS16_EXTEND;
8186 }
97d87491
RS
8187 }
8188 else if (relax_char)
8189 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8190
8191 check_completed_insn (&arg);
8192 return TRUE;
8193 }
8194
8195 /* Fail the match if the line has too many operands. */
8196 if (*args == 0)
8197 return FALSE;
8198
8199 /* Handle characters that need to match exactly. */
8200 if (*args == '(' || *args == ')' || *args == ',')
8201 {
8202 if (match_char (&arg, *args))
8203 continue;
8204 return FALSE;
8205 }
8206
8207 arg.opnum += 1;
8208 c = *args;
8209 switch (c)
8210 {
8211 case 'p':
8212 case 'q':
8213 case 'A':
8214 case 'B':
8215 case 'E':
8216 relax_char = c;
8217 break;
8218
8219 case 'I':
1a00e612
RS
8220 if (!match_const_int (&arg, &imm_expr.X_add_number))
8221 return FALSE;
8222 imm_expr.X_op = O_constant;
bad1aba3 8223 if (GPR_SIZE == 32)
97d87491
RS
8224 normalize_constant_expr (&imm_expr);
8225 continue;
8226
8227 case 'a':
8228 case 'i':
8229 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8230 break;
8231 }
8232
7fd53920 8233 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8234 if (!operand)
8235 abort ();
8236
82d808ed
MR
8237 if (operand->type == OP_PCREL)
8238 pcrel = TRUE;
8239 else
97d87491
RS
8240 {
8241 ext_operand = decode_mips16_operand (c, TRUE);
8242 if (operand != ext_operand)
8243 {
8244 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8245 {
8246 offset_expr.X_op = O_constant;
8247 offset_expr.X_add_number = 0;
8248 relax_char = c;
8249 continue;
8250 }
8251
8252 /* We need the OT_INTEGER check because some MIPS16
8253 immediate variants are listed before the register ones. */
8254 if (arg.token->type != OT_INTEGER
8255 || !match_expression (&arg, &offset_expr, offset_reloc))
8256 return FALSE;
8257
8258 /* '8' is used for SLTI(U) and has traditionally not
8259 been allowed to take relocation operators. */
8260 if (offset_reloc[0] != BFD_RELOC_UNUSED
8261 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8262 {
8263 match_not_constant (&arg);
8264 return FALSE;
8265 }
97d87491
RS
8266
8267 relax_char = c;
8268 continue;
8269 }
8270 }
8271
8272 if (mips_optional_operand_p (operand)
8273 && args[1] == ','
8274 && (arg.token[0].type != OT_REG
8275 || arg.token[1].type == OT_END))
8276 {
8277 /* Assume that the register has been elided and is the
8278 same as the first operand. */
8279 arg.token = tokens;
8280 arg.argnum = 1;
8281 }
8282
8283 if (!match_operand (&arg, operand))
8284 return FALSE;
8285 }
8286}
8287
60f20e8b
RS
8288/* Record that the current instruction is invalid for the current ISA. */
8289
8290static void
8291match_invalid_for_isa (void)
8292{
8293 set_insn_error_ss
1661c76c 8294 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8295 mips_cpu_info_from_arch (mips_opts.arch)->name,
8296 mips_cpu_info_from_isa (mips_opts.isa)->name);
8297}
8298
8299/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8300 Return true if a definite match or failure was found, storing any match
8301 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8302 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8303 tried and failed to match under normal conditions and now want to try a
8304 more relaxed match. */
8305
8306static bfd_boolean
8307match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8308 const struct mips_opcode *past, struct mips_operand_token *tokens,
8309 int opcode_extra, bfd_boolean lax_match)
8310{
8311 const struct mips_opcode *opcode;
8312 const struct mips_opcode *invalid_delay_slot;
8313 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8314
8315 /* Search for a match, ignoring alternatives that don't satisfy the
8316 current ISA or forced_length. */
8317 invalid_delay_slot = 0;
8318 seen_valid_for_isa = FALSE;
8319 seen_valid_for_size = FALSE;
8320 opcode = first;
8321 do
8322 {
8323 gas_assert (strcmp (opcode->name, first->name) == 0);
8324 if (is_opcode_valid (opcode))
8325 {
8326 seen_valid_for_isa = TRUE;
8327 if (is_size_valid (opcode))
8328 {
8329 bfd_boolean delay_slot_ok;
8330
8331 seen_valid_for_size = TRUE;
8332 delay_slot_ok = is_delay_slot_valid (opcode);
8333 if (match_insn (insn, opcode, tokens, opcode_extra,
8334 lax_match, delay_slot_ok))
8335 {
8336 if (!delay_slot_ok)
8337 {
8338 if (!invalid_delay_slot)
8339 invalid_delay_slot = opcode;
8340 }
8341 else
8342 return TRUE;
8343 }
8344 }
8345 }
8346 ++opcode;
8347 }
8348 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8349
8350 /* If the only matches we found had the wrong length for the delay slot,
8351 pick the first such match. We'll issue an appropriate warning later. */
8352 if (invalid_delay_slot)
8353 {
8354 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8355 lax_match, TRUE))
8356 return TRUE;
8357 abort ();
8358 }
8359
8360 /* Handle the case where we didn't try to match an instruction because
8361 all the alternatives were incompatible with the current ISA. */
8362 if (!seen_valid_for_isa)
8363 {
8364 match_invalid_for_isa ();
8365 return TRUE;
8366 }
8367
8368 /* Handle the case where we didn't try to match an instruction because
8369 all the alternatives were of the wrong size. */
8370 if (!seen_valid_for_size)
8371 {
8372 if (mips_opts.insn32)
1661c76c 8373 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8374 else
8375 set_insn_error_i
1661c76c 8376 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8377 8 * forced_insn_length);
8378 return TRUE;
8379 }
8380
8381 return FALSE;
8382}
8383
8384/* Like match_insns, but for MIPS16. */
8385
8386static bfd_boolean
8387match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8388 struct mips_operand_token *tokens)
8389{
8390 const struct mips_opcode *opcode;
8391 bfd_boolean seen_valid_for_isa;
7fd53920 8392 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8393
8394 /* Search for a match, ignoring alternatives that don't satisfy the
8395 current ISA. There are no separate entries for extended forms so
8396 we deal with forced_length later. */
8397 seen_valid_for_isa = FALSE;
7fd53920 8398 seen_valid_for_size = FALSE;
60f20e8b
RS
8399 opcode = first;
8400 do
8401 {
8402 gas_assert (strcmp (opcode->name, first->name) == 0);
8403 if (is_opcode_valid_16 (opcode))
8404 {
8405 seen_valid_for_isa = TRUE;
7fd53920
MR
8406 if (is_size_valid_16 (opcode))
8407 {
8408 seen_valid_for_size = TRUE;
8409 if (match_mips16_insn (insn, opcode, tokens))
8410 return TRUE;
8411 }
60f20e8b
RS
8412 }
8413 ++opcode;
8414 }
8415 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8416 && strcmp (opcode->name, first->name) == 0);
8417
8418 /* Handle the case where we didn't try to match an instruction because
8419 all the alternatives were incompatible with the current ISA. */
8420 if (!seen_valid_for_isa)
8421 {
8422 match_invalid_for_isa ();
8423 return TRUE;
8424 }
8425
7fd53920
MR
8426 /* Handle the case where we didn't try to match an instruction because
8427 all the alternatives were of the wrong size. */
8428 if (!seen_valid_for_size)
8429 {
8430 if (forced_insn_length == 2)
8431 set_insn_error
8432 (0, _("unrecognized unextended version of MIPS16 opcode"));
8433 else
8434 set_insn_error
8435 (0, _("unrecognized extended version of MIPS16 opcode"));
8436 return TRUE;
8437 }
8438
60f20e8b
RS
8439 return FALSE;
8440}
8441
584892a6
RS
8442/* Set up global variables for the start of a new macro. */
8443
8444static void
8445macro_start (void)
8446{
8447 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8448 memset (&mips_macro_warning.first_insn_sizes, 0,
8449 sizeof (mips_macro_warning.first_insn_sizes));
8450 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8451 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8452 && delayed_branch_p (&history[0]));
7bd374a4
MR
8453 if (history[0].frag
8454 && history[0].frag->fr_type == rs_machine_dependent
8455 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8456 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8457 mips_macro_warning.delay_slot_length = 0;
8458 else
8459 switch (history[0].insn_mo->pinfo2
8460 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8461 {
8462 case INSN2_BRANCH_DELAY_32BIT:
8463 mips_macro_warning.delay_slot_length = 4;
8464 break;
8465 case INSN2_BRANCH_DELAY_16BIT:
8466 mips_macro_warning.delay_slot_length = 2;
8467 break;
8468 default:
8469 mips_macro_warning.delay_slot_length = 0;
8470 break;
8471 }
df58fc94 8472 mips_macro_warning.first_frag = NULL;
584892a6
RS
8473}
8474
df58fc94
RS
8475/* Given that a macro is longer than one instruction or of the wrong size,
8476 return the appropriate warning for it. Return null if no warning is
8477 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8478 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8479 and RELAX_NOMACRO. */
584892a6
RS
8480
8481static const char *
8482macro_warning (relax_substateT subtype)
8483{
8484 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8485 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8486 " in a branch delay slot");
8487 else if (subtype & RELAX_NOMACRO)
1661c76c 8488 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8489 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8490 | RELAX_DELAY_SLOT_SIZE_SECOND))
8491 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8492 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8493 " in a 16-bit branch delay slot")
1661c76c 8494 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8495 " in a 32-bit branch delay slot"));
584892a6
RS
8496 else
8497 return 0;
8498}
8499
8500/* Finish up a macro. Emit warnings as appropriate. */
8501
8502static void
8503macro_end (void)
8504{
df58fc94
RS
8505 /* Relaxation warning flags. */
8506 relax_substateT subtype = 0;
8507
8508 /* Check delay slot size requirements. */
8509 if (mips_macro_warning.delay_slot_length == 2)
8510 subtype |= RELAX_DELAY_SLOT_16BIT;
8511 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8512 {
df58fc94
RS
8513 if (mips_macro_warning.delay_slot_length
8514 != mips_macro_warning.first_insn_sizes[0])
8515 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8516 if (mips_macro_warning.delay_slot_length
8517 != mips_macro_warning.first_insn_sizes[1])
8518 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8519 }
584892a6 8520
df58fc94
RS
8521 /* Check instruction count requirements. */
8522 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8523 {
8524 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8525 subtype |= RELAX_SECOND_LONGER;
8526 if (mips_opts.warn_about_macros)
8527 subtype |= RELAX_NOMACRO;
8528 if (mips_macro_warning.delay_slot_p)
8529 subtype |= RELAX_DELAY_SLOT;
df58fc94 8530 }
584892a6 8531
df58fc94
RS
8532 /* If both alternatives fail to fill a delay slot correctly,
8533 emit the warning now. */
8534 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8535 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8536 {
8537 relax_substateT s;
8538 const char *msg;
8539
8540 s = subtype & (RELAX_DELAY_SLOT_16BIT
8541 | RELAX_DELAY_SLOT_SIZE_FIRST
8542 | RELAX_DELAY_SLOT_SIZE_SECOND);
8543 msg = macro_warning (s);
8544 if (msg != NULL)
8545 as_warn ("%s", msg);
8546 subtype &= ~s;
8547 }
8548
8549 /* If both implementations are longer than 1 instruction, then emit the
8550 warning now. */
8551 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8552 {
8553 relax_substateT s;
8554 const char *msg;
8555
8556 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8557 msg = macro_warning (s);
8558 if (msg != NULL)
8559 as_warn ("%s", msg);
8560 subtype &= ~s;
584892a6 8561 }
df58fc94
RS
8562
8563 /* If any flags still set, then one implementation might need a warning
8564 and the other either will need one of a different kind or none at all.
8565 Pass any remaining flags over to relaxation. */
8566 if (mips_macro_warning.first_frag != NULL)
8567 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8568}
8569
df58fc94
RS
8570/* Instruction operand formats used in macros that vary between
8571 standard MIPS and microMIPS code. */
8572
833794fc 8573static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8574static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8575static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8576static const char * const lui_fmt[2] = { "t,u", "s,u" };
8577static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8578static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8579static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8580static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8581
833794fc 8582#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8583#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8584 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8585#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8586#define LUI_FMT (lui_fmt[mips_opts.micromips])
8587#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8588#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8589 : mem12_fmt[mips_opts.micromips])
833794fc 8590#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8591#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8592#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8593
6e1304d8
RS
8594/* Read a macro's relocation codes from *ARGS and store them in *R.
8595 The first argument in *ARGS will be either the code for a single
8596 relocation or -1 followed by the three codes that make up a
8597 composite relocation. */
8598
8599static void
8600macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8601{
8602 int i, next;
8603
8604 next = va_arg (*args, int);
8605 if (next >= 0)
8606 r[0] = (bfd_reloc_code_real_type) next;
8607 else
f2ae14a1
RS
8608 {
8609 for (i = 0; i < 3; i++)
8610 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8611 /* This function is only used for 16-bit relocation fields.
8612 To make the macro code simpler, treat an unrelocated value
8613 in the same way as BFD_RELOC_LO16. */
8614 if (r[0] == BFD_RELOC_UNUSED)
8615 r[0] = BFD_RELOC_LO16;
8616 }
6e1304d8
RS
8617}
8618
252b5132
RH
8619/* Build an instruction created by a macro expansion. This is passed
8620 a pointer to the count of instructions created so far, an
8621 expression, the name of the instruction to build, an operand format
8622 string, and corresponding arguments. */
8623
252b5132 8624static void
67c0d1eb 8625macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8626{
df58fc94 8627 const struct mips_opcode *mo = NULL;
f6688943 8628 bfd_reloc_code_real_type r[3];
df58fc94 8629 const struct mips_opcode *amo;
e077a1c8 8630 const struct mips_operand *operand;
df58fc94
RS
8631 struct hash_control *hash;
8632 struct mips_cl_insn insn;
252b5132 8633 va_list args;
e077a1c8 8634 unsigned int uval;
252b5132 8635
252b5132 8636 va_start (args, fmt);
252b5132 8637
252b5132
RH
8638 if (mips_opts.mips16)
8639 {
03ea81db 8640 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8641 va_end (args);
8642 return;
8643 }
8644
f6688943
TS
8645 r[0] = BFD_RELOC_UNUSED;
8646 r[1] = BFD_RELOC_UNUSED;
8647 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8648 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8649 amo = (struct mips_opcode *) hash_find (hash, name);
8650 gas_assert (amo);
8651 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8652
df58fc94 8653 do
8b082fb1
TS
8654 {
8655 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8656 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8657 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8658 slot instruction length requirement (if any) of the previous
8659 instruction. While doing this we record the first instruction
8660 seen that matches all the other conditions and use it anyway
8661 if the requirement cannot be met; we will issue an appropriate
8662 warning later on. */
8663 if (strcmp (fmt, amo->args) == 0
8664 && amo->pinfo != INSN_MACRO
8665 && is_opcode_valid (amo)
8666 && is_size_valid (amo))
8667 {
8668 if (is_delay_slot_valid (amo))
8669 {
8670 mo = amo;
8671 break;
8672 }
8673 else if (!mo)
8674 mo = amo;
8675 }
8b082fb1 8676
df58fc94
RS
8677 ++amo;
8678 gas_assert (amo->name);
252b5132 8679 }
df58fc94 8680 while (strcmp (name, amo->name) == 0);
252b5132 8681
df58fc94 8682 gas_assert (mo);
1e915849 8683 create_insn (&insn, mo);
e077a1c8 8684 for (; *fmt; ++fmt)
252b5132 8685 {
e077a1c8 8686 switch (*fmt)
252b5132 8687 {
252b5132
RH
8688 case ',':
8689 case '(':
8690 case ')':
252b5132 8691 case 'z':
e077a1c8 8692 break;
252b5132
RH
8693
8694 case 'i':
8695 case 'j':
6e1304d8 8696 macro_read_relocs (&args, r);
9c2799c2 8697 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8698 || *r == BFD_RELOC_MIPS_HIGHER
8699 || *r == BFD_RELOC_HI16_S
8700 || *r == BFD_RELOC_LO16
8701 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 8702 break;
e391c024
RS
8703
8704 case 'o':
8705 macro_read_relocs (&args, r);
e077a1c8 8706 break;
252b5132
RH
8707
8708 case 'u':
6e1304d8 8709 macro_read_relocs (&args, r);
9c2799c2 8710 gas_assert (ep != NULL
90ecf173
MR
8711 && (ep->X_op == O_constant
8712 || (ep->X_op == O_symbol
8713 && (*r == BFD_RELOC_MIPS_HIGHEST
8714 || *r == BFD_RELOC_HI16_S
8715 || *r == BFD_RELOC_HI16
8716 || *r == BFD_RELOC_GPREL16
8717 || *r == BFD_RELOC_MIPS_GOT_HI16
8718 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8719 break;
252b5132
RH
8720
8721 case 'p':
9c2799c2 8722 gas_assert (ep != NULL);
bad36eac 8723
252b5132
RH
8724 /*
8725 * This allows macro() to pass an immediate expression for
8726 * creating short branches without creating a symbol.
bad36eac
DJ
8727 *
8728 * We don't allow branch relaxation for these branches, as
8729 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8730 */
8731 if (ep->X_op == O_constant)
8732 {
df58fc94
RS
8733 /* For microMIPS we always use relocations for branches.
8734 So we should not resolve immediate values. */
8735 gas_assert (!mips_opts.micromips);
8736
bad36eac
DJ
8737 if ((ep->X_add_number & 3) != 0)
8738 as_bad (_("branch to misaligned address (0x%lx)"),
8739 (unsigned long) ep->X_add_number);
8740 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8741 as_bad (_("branch address range overflow (0x%lx)"),
8742 (unsigned long) ep->X_add_number);
252b5132
RH
8743 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8744 ep = NULL;
8745 }
8746 else
0b25d3e6 8747 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8748 break;
252b5132
RH
8749
8750 case 'a':
9c2799c2 8751 gas_assert (ep != NULL);
f6688943 8752 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8753 break;
d43b4baf 8754
252b5132 8755 default:
e077a1c8
RS
8756 operand = (mips_opts.micromips
8757 ? decode_micromips_operand (fmt)
8758 : decode_mips_operand (fmt));
8759 if (!operand)
8760 abort ();
8761
8762 uval = va_arg (args, int);
8763 if (operand->type == OP_CLO_CLZ_DEST)
8764 uval |= (uval << 5);
8765 insn_insert_operand (&insn, operand, uval);
8766
7361da2c 8767 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8768 ++fmt;
8769 break;
252b5132 8770 }
252b5132
RH
8771 }
8772 va_end (args);
9c2799c2 8773 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8774
df58fc94 8775 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8776}
8777
8778static void
67c0d1eb 8779mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8780 va_list *args)
252b5132 8781{
1e915849 8782 struct mips_opcode *mo;
252b5132 8783 struct mips_cl_insn insn;
e077a1c8 8784 const struct mips_operand *operand;
f6688943
TS
8785 bfd_reloc_code_real_type r[3]
8786 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8787
1e915849 8788 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8789 gas_assert (mo);
8790 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8791
1e915849 8792 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8793 {
1e915849 8794 ++mo;
9c2799c2
NC
8795 gas_assert (mo->name);
8796 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8797 }
8798
1e915849 8799 create_insn (&insn, mo);
e077a1c8 8800 for (; *fmt; ++fmt)
252b5132
RH
8801 {
8802 int c;
8803
e077a1c8 8804 c = *fmt;
252b5132
RH
8805 switch (c)
8806 {
252b5132
RH
8807 case ',':
8808 case '(':
8809 case ')':
e077a1c8 8810 break;
252b5132 8811
d8722d76 8812 case '.':
252b5132
RH
8813 case 'S':
8814 case 'P':
8815 case 'R':
e077a1c8 8816 break;
252b5132
RH
8817
8818 case '<':
252b5132 8819 case '5':
d8722d76 8820 case 'F':
252b5132
RH
8821 case 'H':
8822 case 'W':
8823 case 'D':
8824 case 'j':
8825 case '8':
8826 case 'V':
8827 case 'C':
8828 case 'U':
8829 case 'k':
8830 case 'K':
8831 case 'p':
8832 case 'q':
8833 {
b886a2ab
RS
8834 offsetT value;
8835
9c2799c2 8836 gas_assert (ep != NULL);
252b5132
RH
8837
8838 if (ep->X_op != O_constant)
874e8986 8839 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8840 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8841 {
b886a2ab 8842 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8843 ep = NULL;
f6688943 8844 *r = BFD_RELOC_UNUSED;
252b5132
RH
8845 }
8846 }
e077a1c8 8847 break;
252b5132 8848
e077a1c8
RS
8849 default:
8850 operand = decode_mips16_operand (c, FALSE);
8851 if (!operand)
8852 abort ();
252b5132 8853
4a06e5a2 8854 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8855 break;
8856 }
252b5132
RH
8857 }
8858
9c2799c2 8859 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8860
df58fc94 8861 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8862}
8863
438c16b8
TS
8864/*
8865 * Generate a "jalr" instruction with a relocation hint to the called
8866 * function. This occurs in NewABI PIC code.
8867 */
8868static void
df58fc94 8869macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 8870{
df58fc94
RS
8871 static const bfd_reloc_code_real_type jalr_relocs[2]
8872 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8873 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8874 const char *jalr;
685736be 8875 char *f = NULL;
b34976b6 8876
1180b5a4 8877 if (MIPS_JALR_HINT_P (ep))
f21f8242 8878 {
cc3d92a5 8879 frag_grow (8);
f21f8242
AO
8880 f = frag_more (0);
8881 }
2906b037 8882 if (mips_opts.micromips)
df58fc94 8883 {
833794fc
MR
8884 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8885 ? "jalr" : "jalrs");
e64af278 8886 if (MIPS_JALR_HINT_P (ep)
833794fc 8887 || mips_opts.insn32
e64af278 8888 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8889 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8890 else
8891 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8892 }
2906b037
MR
8893 else
8894 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 8895 if (MIPS_JALR_HINT_P (ep))
df58fc94 8896 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
8897}
8898
252b5132
RH
8899/*
8900 * Generate a "lui" instruction.
8901 */
8902static void
67c0d1eb 8903macro_build_lui (expressionS *ep, int regnum)
252b5132 8904{
9c2799c2 8905 gas_assert (! mips_opts.mips16);
252b5132 8906
df58fc94 8907 if (ep->X_op != O_constant)
252b5132 8908 {
9c2799c2 8909 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8910 /* _gp_disp is a special case, used from s_cpload.
8911 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8912 gas_assert (mips_pic == NO_PIC
78e1bb40 8913 || (! HAVE_NEWABI
aa6975fb
ILT
8914 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8915 || (! mips_in_shared
bbe506e8
TS
8916 && strcmp (S_GET_NAME (ep->X_add_symbol),
8917 "__gnu_local_gp") == 0));
252b5132
RH
8918 }
8919
df58fc94 8920 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8921}
8922
885add95
CD
8923/* Generate a sequence of instructions to do a load or store from a constant
8924 offset off of a base register (breg) into/from a target register (treg),
8925 using AT if necessary. */
8926static void
67c0d1eb
RS
8927macro_build_ldst_constoffset (expressionS *ep, const char *op,
8928 int treg, int breg, int dbl)
885add95 8929{
9c2799c2 8930 gas_assert (ep->X_op == O_constant);
885add95 8931
256ab948 8932 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8933 if (!dbl)
8934 normalize_constant_expr (ep);
256ab948 8935
67c1ffbe 8936 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8937 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8938 as_warn (_("operand overflow"));
8939
8940 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8941 {
8942 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8943 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8944 }
8945 else
8946 {
8947 /* 32-bit offset, need multiple instructions and AT, like:
8948 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8949 addu $tempreg,$tempreg,$breg
8950 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8951 to handle the complete offset. */
67c0d1eb
RS
8952 macro_build_lui (ep, AT);
8953 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8954 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8955
741fe287 8956 if (!mips_opts.at)
1661c76c 8957 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
8958 }
8959}
8960
252b5132
RH
8961/* set_at()
8962 * Generates code to set the $at register to true (one)
8963 * if reg is less than the immediate expression.
8964 */
8965static void
67c0d1eb 8966set_at (int reg, int unsignedp)
252b5132 8967{
b0e6f033 8968 if (imm_expr.X_add_number >= -0x8000
252b5132 8969 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8970 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8971 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8972 else
8973 {
bad1aba3 8974 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 8975 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8976 }
8977}
8978
252b5132
RH
8979/* Count the leading zeroes by performing a binary chop. This is a
8980 bulky bit of source, but performance is a LOT better for the
8981 majority of values than a simple loop to count the bits:
8982 for (lcnt = 0; (lcnt < 32); lcnt++)
8983 if ((v) & (1 << (31 - lcnt)))
8984 break;
8985 However it is not code size friendly, and the gain will drop a bit
8986 on certain cached systems.
8987*/
8988#define COUNT_TOP_ZEROES(v) \
8989 (((v) & ~0xffff) == 0 \
8990 ? ((v) & ~0xff) == 0 \
8991 ? ((v) & ~0xf) == 0 \
8992 ? ((v) & ~0x3) == 0 \
8993 ? ((v) & ~0x1) == 0 \
8994 ? !(v) \
8995 ? 32 \
8996 : 31 \
8997 : 30 \
8998 : ((v) & ~0x7) == 0 \
8999 ? 29 \
9000 : 28 \
9001 : ((v) & ~0x3f) == 0 \
9002 ? ((v) & ~0x1f) == 0 \
9003 ? 27 \
9004 : 26 \
9005 : ((v) & ~0x7f) == 0 \
9006 ? 25 \
9007 : 24 \
9008 : ((v) & ~0xfff) == 0 \
9009 ? ((v) & ~0x3ff) == 0 \
9010 ? ((v) & ~0x1ff) == 0 \
9011 ? 23 \
9012 : 22 \
9013 : ((v) & ~0x7ff) == 0 \
9014 ? 21 \
9015 : 20 \
9016 : ((v) & ~0x3fff) == 0 \
9017 ? ((v) & ~0x1fff) == 0 \
9018 ? 19 \
9019 : 18 \
9020 : ((v) & ~0x7fff) == 0 \
9021 ? 17 \
9022 : 16 \
9023 : ((v) & ~0xffffff) == 0 \
9024 ? ((v) & ~0xfffff) == 0 \
9025 ? ((v) & ~0x3ffff) == 0 \
9026 ? ((v) & ~0x1ffff) == 0 \
9027 ? 15 \
9028 : 14 \
9029 : ((v) & ~0x7ffff) == 0 \
9030 ? 13 \
9031 : 12 \
9032 : ((v) & ~0x3fffff) == 0 \
9033 ? ((v) & ~0x1fffff) == 0 \
9034 ? 11 \
9035 : 10 \
9036 : ((v) & ~0x7fffff) == 0 \
9037 ? 9 \
9038 : 8 \
9039 : ((v) & ~0xfffffff) == 0 \
9040 ? ((v) & ~0x3ffffff) == 0 \
9041 ? ((v) & ~0x1ffffff) == 0 \
9042 ? 7 \
9043 : 6 \
9044 : ((v) & ~0x7ffffff) == 0 \
9045 ? 5 \
9046 : 4 \
9047 : ((v) & ~0x3fffffff) == 0 \
9048 ? ((v) & ~0x1fffffff) == 0 \
9049 ? 3 \
9050 : 2 \
9051 : ((v) & ~0x7fffffff) == 0 \
9052 ? 1 \
9053 : 0)
9054
9055/* load_register()
67c1ffbe 9056 * This routine generates the least number of instructions necessary to load
252b5132
RH
9057 * an absolute expression value into a register.
9058 */
9059static void
67c0d1eb 9060load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9061{
9062 int freg;
9063 expressionS hi32, lo32;
9064
9065 if (ep->X_op != O_big)
9066 {
9c2799c2 9067 gas_assert (ep->X_op == O_constant);
256ab948
TS
9068
9069 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9070 if (!dbl)
9071 normalize_constant_expr (ep);
256ab948
TS
9072
9073 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9074 {
9075 /* We can handle 16 bit signed values with an addiu to
9076 $zero. No need to ever use daddiu here, since $zero and
9077 the result are always correct in 32 bit mode. */
67c0d1eb 9078 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9079 return;
9080 }
9081 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9082 {
9083 /* We can handle 16 bit unsigned values with an ori to
9084 $zero. */
67c0d1eb 9085 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9086 return;
9087 }
256ab948 9088 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9089 {
9090 /* 32 bit values require an lui. */
df58fc94 9091 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9092 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9093 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9094 return;
9095 }
9096 }
9097
9098 /* The value is larger than 32 bits. */
9099
bad1aba3 9100 if (!dbl || GPR_SIZE == 32)
252b5132 9101 {
55e08f71
NC
9102 char value[32];
9103
9104 sprintf_vma (value, ep->X_add_number);
1661c76c 9105 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9106 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9107 return;
9108 }
9109
9110 if (ep->X_op != O_big)
9111 {
9112 hi32 = *ep;
9113 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9114 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9115 hi32.X_add_number &= 0xffffffff;
9116 lo32 = *ep;
9117 lo32.X_add_number &= 0xffffffff;
9118 }
9119 else
9120 {
9c2799c2 9121 gas_assert (ep->X_add_number > 2);
252b5132
RH
9122 if (ep->X_add_number == 3)
9123 generic_bignum[3] = 0;
9124 else if (ep->X_add_number > 4)
1661c76c 9125 as_bad (_("number larger than 64 bits"));
252b5132
RH
9126 lo32.X_op = O_constant;
9127 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9128 hi32.X_op = O_constant;
9129 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9130 }
9131
9132 if (hi32.X_add_number == 0)
9133 freg = 0;
9134 else
9135 {
9136 int shift, bit;
9137 unsigned long hi, lo;
9138
956cd1d6 9139 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9140 {
9141 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9142 {
67c0d1eb 9143 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9144 return;
9145 }
9146 if (lo32.X_add_number & 0x80000000)
9147 {
df58fc94 9148 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9149 if (lo32.X_add_number & 0xffff)
67c0d1eb 9150 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9151 return;
9152 }
9153 }
252b5132
RH
9154
9155 /* Check for 16bit shifted constant. We know that hi32 is
9156 non-zero, so start the mask on the first bit of the hi32
9157 value. */
9158 shift = 17;
9159 do
beae10d5
KH
9160 {
9161 unsigned long himask, lomask;
9162
9163 if (shift < 32)
9164 {
9165 himask = 0xffff >> (32 - shift);
9166 lomask = (0xffff << shift) & 0xffffffff;
9167 }
9168 else
9169 {
9170 himask = 0xffff << (shift - 32);
9171 lomask = 0;
9172 }
9173 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9174 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9175 {
9176 expressionS tmp;
9177
9178 tmp.X_op = O_constant;
9179 if (shift < 32)
9180 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9181 | (lo32.X_add_number >> shift));
9182 else
9183 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9184 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9185 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9186 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9187 return;
9188 }
f9419b05 9189 ++shift;
beae10d5
KH
9190 }
9191 while (shift <= (64 - 16));
252b5132
RH
9192
9193 /* Find the bit number of the lowest one bit, and store the
9194 shifted value in hi/lo. */
9195 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9196 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9197 if (lo != 0)
9198 {
9199 bit = 0;
9200 while ((lo & 1) == 0)
9201 {
9202 lo >>= 1;
9203 ++bit;
9204 }
9205 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9206 hi >>= bit;
9207 }
9208 else
9209 {
9210 bit = 32;
9211 while ((hi & 1) == 0)
9212 {
9213 hi >>= 1;
9214 ++bit;
9215 }
9216 lo = hi;
9217 hi = 0;
9218 }
9219
9220 /* Optimize if the shifted value is a (power of 2) - 1. */
9221 if ((hi == 0 && ((lo + 1) & lo) == 0)
9222 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9223 {
9224 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9225 if (shift != 0)
beae10d5 9226 {
252b5132
RH
9227 expressionS tmp;
9228
9229 /* This instruction will set the register to be all
9230 ones. */
beae10d5
KH
9231 tmp.X_op = O_constant;
9232 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9233 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9234 if (bit != 0)
9235 {
9236 bit += shift;
df58fc94 9237 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9238 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9239 }
df58fc94 9240 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9241 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9242 return;
9243 }
9244 }
252b5132
RH
9245
9246 /* Sign extend hi32 before calling load_register, because we can
9247 generally get better code when we load a sign extended value. */
9248 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9249 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9250 load_register (reg, &hi32, 0);
252b5132
RH
9251 freg = reg;
9252 }
9253 if ((lo32.X_add_number & 0xffff0000) == 0)
9254 {
9255 if (freg != 0)
9256 {
df58fc94 9257 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9258 freg = reg;
9259 }
9260 }
9261 else
9262 {
9263 expressionS mid16;
9264
956cd1d6 9265 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9266 {
df58fc94
RS
9267 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9268 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9269 return;
9270 }
252b5132
RH
9271
9272 if (freg != 0)
9273 {
df58fc94 9274 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9275 freg = reg;
9276 }
9277 mid16 = lo32;
9278 mid16.X_add_number >>= 16;
67c0d1eb 9279 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9280 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9281 freg = reg;
9282 }
9283 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9284 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9285}
9286
269137b2
TS
9287static inline void
9288load_delay_nop (void)
9289{
9290 if (!gpr_interlocks)
9291 macro_build (NULL, "nop", "");
9292}
9293
252b5132
RH
9294/* Load an address into a register. */
9295
9296static void
67c0d1eb 9297load_address (int reg, expressionS *ep, int *used_at)
252b5132 9298{
252b5132
RH
9299 if (ep->X_op != O_constant
9300 && ep->X_op != O_symbol)
9301 {
9302 as_bad (_("expression too complex"));
9303 ep->X_op = O_constant;
9304 }
9305
9306 if (ep->X_op == O_constant)
9307 {
67c0d1eb 9308 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9309 return;
9310 }
9311
9312 if (mips_pic == NO_PIC)
9313 {
9314 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9315 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9316 Otherwise we want
9317 lui $reg,<sym> (BFD_RELOC_HI16_S)
9318 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9319 If we have an addend, we always use the latter form.
76b3015f 9320
d6bc6245
TS
9321 With 64bit address space and a usable $at we want
9322 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9323 lui $at,<sym> (BFD_RELOC_HI16_S)
9324 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9325 daddiu $at,<sym> (BFD_RELOC_LO16)
9326 dsll32 $reg,0
3a482fd5 9327 daddu $reg,$reg,$at
76b3015f 9328
c03099e6 9329 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9330 on superscalar processors.
9331 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9332 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9333 dsll $reg,16
9334 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9335 dsll $reg,16
9336 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9337
9338 For GP relative symbols in 64bit address space we can use
9339 the same sequence as in 32bit address space. */
aed1a261 9340 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9341 {
6caf9ef4
TS
9342 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9343 && !nopic_need_relax (ep->X_add_symbol, 1))
9344 {
9345 relax_start (ep->X_add_symbol);
9346 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9347 mips_gp_register, BFD_RELOC_GPREL16);
9348 relax_switch ();
9349 }
d6bc6245 9350
741fe287 9351 if (*used_at == 0 && mips_opts.at)
d6bc6245 9352 {
df58fc94
RS
9353 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9354 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9355 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9356 BFD_RELOC_MIPS_HIGHER);
9357 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9358 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9359 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9360 *used_at = 1;
9361 }
9362 else
9363 {
df58fc94 9364 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9365 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9366 BFD_RELOC_MIPS_HIGHER);
df58fc94 9367 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9368 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9369 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9370 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9371 }
6caf9ef4
TS
9372
9373 if (mips_relax.sequence)
9374 relax_end ();
d6bc6245 9375 }
252b5132
RH
9376 else
9377 {
d6bc6245 9378 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9379 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9380 {
4d7206a2 9381 relax_start (ep->X_add_symbol);
67c0d1eb 9382 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9383 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9384 relax_switch ();
d6bc6245 9385 }
67c0d1eb
RS
9386 macro_build_lui (ep, reg);
9387 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9388 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9389 if (mips_relax.sequence)
9390 relax_end ();
d6bc6245 9391 }
252b5132 9392 }
0a44bf69 9393 else if (!mips_big_got)
252b5132
RH
9394 {
9395 expressionS ex;
9396
9397 /* If this is a reference to an external symbol, we want
9398 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9399 Otherwise we want
9400 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9401 nop
9402 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9403 If there is a constant, it must be added in after.
9404
ed6fb7bd 9405 If we have NewABI, we want
f5040a92
AO
9406 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9407 unless we're referencing a global symbol with a non-zero
9408 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9409 if (HAVE_NEWABI)
9410 {
f5040a92
AO
9411 if (ep->X_add_number)
9412 {
4d7206a2 9413 ex.X_add_number = ep->X_add_number;
f5040a92 9414 ep->X_add_number = 0;
4d7206a2 9415 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9416 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9417 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9418 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9419 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9420 ex.X_op = O_constant;
67c0d1eb 9421 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9422 reg, reg, BFD_RELOC_LO16);
f5040a92 9423 ep->X_add_number = ex.X_add_number;
4d7206a2 9424 relax_switch ();
f5040a92 9425 }
67c0d1eb 9426 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9427 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9428 if (mips_relax.sequence)
9429 relax_end ();
ed6fb7bd
SC
9430 }
9431 else
9432 {
f5040a92
AO
9433 ex.X_add_number = ep->X_add_number;
9434 ep->X_add_number = 0;
67c0d1eb
RS
9435 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9436 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9437 load_delay_nop ();
4d7206a2
RS
9438 relax_start (ep->X_add_symbol);
9439 relax_switch ();
67c0d1eb 9440 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9441 BFD_RELOC_LO16);
4d7206a2 9442 relax_end ();
ed6fb7bd 9443
f5040a92
AO
9444 if (ex.X_add_number != 0)
9445 {
9446 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9447 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9448 ex.X_op = O_constant;
67c0d1eb 9449 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9450 reg, reg, BFD_RELOC_LO16);
f5040a92 9451 }
252b5132
RH
9452 }
9453 }
0a44bf69 9454 else if (mips_big_got)
252b5132
RH
9455 {
9456 expressionS ex;
252b5132
RH
9457
9458 /* This is the large GOT case. If this is a reference to an
9459 external symbol, we want
9460 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9461 addu $reg,$reg,$gp
9462 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9463
9464 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9465 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9466 nop
9467 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9468 If there is a constant, it must be added in after.
f5040a92
AO
9469
9470 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9471 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9472 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9473 */
438c16b8
TS
9474 if (HAVE_NEWABI)
9475 {
4d7206a2 9476 ex.X_add_number = ep->X_add_number;
f5040a92 9477 ep->X_add_number = 0;
4d7206a2 9478 relax_start (ep->X_add_symbol);
df58fc94 9479 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9480 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9481 reg, reg, mips_gp_register);
9482 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9483 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9484 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9485 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9486 else if (ex.X_add_number)
9487 {
9488 ex.X_op = O_constant;
67c0d1eb
RS
9489 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9490 BFD_RELOC_LO16);
f5040a92
AO
9491 }
9492
9493 ep->X_add_number = ex.X_add_number;
4d7206a2 9494 relax_switch ();
67c0d1eb 9495 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9496 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9497 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9498 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9499 relax_end ();
438c16b8 9500 }
252b5132 9501 else
438c16b8 9502 {
f5040a92
AO
9503 ex.X_add_number = ep->X_add_number;
9504 ep->X_add_number = 0;
4d7206a2 9505 relax_start (ep->X_add_symbol);
df58fc94 9506 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9507 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9508 reg, reg, mips_gp_register);
9509 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9510 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9511 relax_switch ();
9512 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9513 {
9514 /* We need a nop before loading from $gp. This special
9515 check is required because the lui which starts the main
9516 instruction stream does not refer to $gp, and so will not
9517 insert the nop which may be required. */
67c0d1eb 9518 macro_build (NULL, "nop", "");
438c16b8 9519 }
67c0d1eb 9520 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9521 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9522 load_delay_nop ();
67c0d1eb 9523 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9524 BFD_RELOC_LO16);
4d7206a2 9525 relax_end ();
438c16b8 9526
f5040a92
AO
9527 if (ex.X_add_number != 0)
9528 {
9529 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9530 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9531 ex.X_op = O_constant;
67c0d1eb
RS
9532 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9533 BFD_RELOC_LO16);
f5040a92 9534 }
252b5132
RH
9535 }
9536 }
252b5132
RH
9537 else
9538 abort ();
8fc2e39e 9539
741fe287 9540 if (!mips_opts.at && *used_at == 1)
1661c76c 9541 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9542}
9543
ea1fb5dc
RS
9544/* Move the contents of register SOURCE into register DEST. */
9545
9546static void
67c0d1eb 9547move_register (int dest, int source)
ea1fb5dc 9548{
df58fc94
RS
9549 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9550 instruction specifically requires a 32-bit one. */
9551 if (mips_opts.micromips
833794fc 9552 && !mips_opts.insn32
df58fc94 9553 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9554 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9555 else
40fc1451 9556 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9557}
9558
4d7206a2 9559/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9560 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9561 The two alternatives are:
4d7206a2 9562
33eaf5de 9563 Global symbol Local symbol
4d7206a2
RS
9564 ------------- ------------
9565 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9566 ... ...
9567 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9568
9569 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9570 emits the second for a 16-bit offset or add_got_offset_hilo emits
9571 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9572
9573static void
67c0d1eb 9574load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9575{
9576 expressionS global;
9577
9578 global = *local;
9579 global.X_add_number = 0;
9580
9581 relax_start (local->X_add_symbol);
67c0d1eb
RS
9582 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9583 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9584 relax_switch ();
67c0d1eb
RS
9585 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9586 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9587 relax_end ();
9588}
9589
9590static void
67c0d1eb 9591add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9592{
9593 expressionS global;
9594
9595 global.X_op = O_constant;
9596 global.X_op_symbol = NULL;
9597 global.X_add_symbol = NULL;
9598 global.X_add_number = local->X_add_number;
9599
9600 relax_start (local->X_add_symbol);
67c0d1eb 9601 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9602 dest, dest, BFD_RELOC_LO16);
9603 relax_switch ();
67c0d1eb 9604 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9605 relax_end ();
9606}
9607
f6a22291
MR
9608static void
9609add_got_offset_hilo (int dest, expressionS *local, int tmp)
9610{
9611 expressionS global;
9612 int hold_mips_optimize;
9613
9614 global.X_op = O_constant;
9615 global.X_op_symbol = NULL;
9616 global.X_add_symbol = NULL;
9617 global.X_add_number = local->X_add_number;
9618
9619 relax_start (local->X_add_symbol);
9620 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9621 relax_switch ();
9622 /* Set mips_optimize around the lui instruction to avoid
9623 inserting an unnecessary nop after the lw. */
9624 hold_mips_optimize = mips_optimize;
9625 mips_optimize = 2;
9626 macro_build_lui (&global, tmp);
9627 mips_optimize = hold_mips_optimize;
9628 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9629 relax_end ();
9630
9631 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9632}
9633
df58fc94
RS
9634/* Emit a sequence of instructions to emulate a branch likely operation.
9635 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9636 is its complementing branch with the original condition negated.
9637 CALL is set if the original branch specified the link operation.
9638 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9639
9640 Code like this is produced in the noreorder mode:
9641
9642 BRNEG <args>, 1f
9643 nop
9644 b <sym>
9645 delay slot (executed only if branch taken)
9646 1:
9647
9648 or, if CALL is set:
9649
9650 BRNEG <args>, 1f
9651 nop
9652 bal <sym>
9653 delay slot (executed only if branch taken)
9654 1:
9655
9656 In the reorder mode the delay slot would be filled with a nop anyway,
9657 so code produced is simply:
9658
9659 BR <args>, <sym>
9660 nop
9661
9662 This function is used when producing code for the microMIPS ASE that
9663 does not implement branch likely instructions in hardware. */
9664
9665static void
9666macro_build_branch_likely (const char *br, const char *brneg,
9667 int call, expressionS *ep, const char *fmt,
9668 unsigned int sreg, unsigned int treg)
9669{
9670 int noreorder = mips_opts.noreorder;
9671 expressionS expr1;
9672
9673 gas_assert (mips_opts.micromips);
9674 start_noreorder ();
9675 if (noreorder)
9676 {
9677 micromips_label_expr (&expr1);
9678 macro_build (&expr1, brneg, fmt, sreg, treg);
9679 macro_build (NULL, "nop", "");
9680 macro_build (ep, call ? "bal" : "b", "p");
9681
9682 /* Set to true so that append_insn adds a label. */
9683 emit_branch_likely_macro = TRUE;
9684 }
9685 else
9686 {
9687 macro_build (ep, br, fmt, sreg, treg);
9688 macro_build (NULL, "nop", "");
9689 }
9690 end_noreorder ();
9691}
9692
9693/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9694 the condition code tested. EP specifies the branch target. */
9695
9696static void
9697macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9698{
9699 const int call = 0;
9700 const char *brneg;
9701 const char *br;
9702
9703 switch (type)
9704 {
9705 case M_BC1FL:
9706 br = "bc1f";
9707 brneg = "bc1t";
9708 break;
9709 case M_BC1TL:
9710 br = "bc1t";
9711 brneg = "bc1f";
9712 break;
9713 case M_BC2FL:
9714 br = "bc2f";
9715 brneg = "bc2t";
9716 break;
9717 case M_BC2TL:
9718 br = "bc2t";
9719 brneg = "bc2f";
9720 break;
9721 default:
9722 abort ();
9723 }
9724 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9725}
9726
9727/* Emit a two-argument branch macro specified by TYPE, using SREG as
9728 the register tested. EP specifies the branch target. */
9729
9730static void
9731macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9732{
9733 const char *brneg = NULL;
9734 const char *br;
9735 int call = 0;
9736
9737 switch (type)
9738 {
9739 case M_BGEZ:
9740 br = "bgez";
9741 break;
9742 case M_BGEZL:
9743 br = mips_opts.micromips ? "bgez" : "bgezl";
9744 brneg = "bltz";
9745 break;
9746 case M_BGEZALL:
9747 gas_assert (mips_opts.micromips);
833794fc 9748 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9749 brneg = "bltz";
9750 call = 1;
9751 break;
9752 case M_BGTZ:
9753 br = "bgtz";
9754 break;
9755 case M_BGTZL:
9756 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9757 brneg = "blez";
9758 break;
9759 case M_BLEZ:
9760 br = "blez";
9761 break;
9762 case M_BLEZL:
9763 br = mips_opts.micromips ? "blez" : "blezl";
9764 brneg = "bgtz";
9765 break;
9766 case M_BLTZ:
9767 br = "bltz";
9768 break;
9769 case M_BLTZL:
9770 br = mips_opts.micromips ? "bltz" : "bltzl";
9771 brneg = "bgez";
9772 break;
9773 case M_BLTZALL:
9774 gas_assert (mips_opts.micromips);
833794fc 9775 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9776 brneg = "bgez";
9777 call = 1;
9778 break;
9779 default:
9780 abort ();
9781 }
9782 if (mips_opts.micromips && brneg)
9783 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9784 else
9785 macro_build (ep, br, "s,p", sreg);
9786}
9787
9788/* Emit a three-argument branch macro specified by TYPE, using SREG and
9789 TREG as the registers tested. EP specifies the branch target. */
9790
9791static void
9792macro_build_branch_rsrt (int type, expressionS *ep,
9793 unsigned int sreg, unsigned int treg)
9794{
9795 const char *brneg = NULL;
9796 const int call = 0;
9797 const char *br;
9798
9799 switch (type)
9800 {
9801 case M_BEQ:
9802 case M_BEQ_I:
9803 br = "beq";
9804 break;
9805 case M_BEQL:
9806 case M_BEQL_I:
9807 br = mips_opts.micromips ? "beq" : "beql";
9808 brneg = "bne";
9809 break;
9810 case M_BNE:
9811 case M_BNE_I:
9812 br = "bne";
9813 break;
9814 case M_BNEL:
9815 case M_BNEL_I:
9816 br = mips_opts.micromips ? "bne" : "bnel";
9817 brneg = "beq";
9818 break;
9819 default:
9820 abort ();
9821 }
9822 if (mips_opts.micromips && brneg)
9823 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9824 else
9825 macro_build (ep, br, "s,t,p", sreg, treg);
9826}
9827
f2ae14a1
RS
9828/* Return the high part that should be loaded in order to make the low
9829 part of VALUE accessible using an offset of OFFBITS bits. */
9830
9831static offsetT
9832offset_high_part (offsetT value, unsigned int offbits)
9833{
9834 offsetT bias;
9835 addressT low_mask;
9836
9837 if (offbits == 0)
9838 return value;
9839 bias = 1 << (offbits - 1);
9840 low_mask = bias * 2 - 1;
9841 return (value + bias) & ~low_mask;
9842}
9843
9844/* Return true if the value stored in offset_expr and offset_reloc
9845 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9846 amount that the caller wants to add without inducing overflow
9847 and ALIGN is the known alignment of the value in bytes. */
9848
9849static bfd_boolean
9850small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9851{
9852 if (offbits == 16)
9853 {
9854 /* Accept any relocation operator if overflow isn't a concern. */
9855 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9856 return TRUE;
9857
9858 /* These relocations are guaranteed not to overflow in correct links. */
9859 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9860 || gprel16_reloc_p (*offset_reloc))
9861 return TRUE;
9862 }
9863 if (offset_expr.X_op == O_constant
9864 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9865 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9866 return TRUE;
9867 return FALSE;
9868}
9869
252b5132
RH
9870/*
9871 * Build macros
9872 * This routine implements the seemingly endless macro or synthesized
9873 * instructions and addressing modes in the mips assembly language. Many
9874 * of these macros are simple and are similar to each other. These could
67c1ffbe 9875 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
9876 * this verbose method. Others are not simple macros but are more like
9877 * optimizing code generation.
9878 * One interesting optimization is when several store macros appear
67c1ffbe 9879 * consecutively that would load AT with the upper half of the same address.
2b0f3761 9880 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
9881 * of global optimization. We currently only optimize within a single macro.
9882 * For many of the load and store macros if the address is specified as a
9883 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9884 * first load register 'at' with zero and use it as the base register. The
9885 * mips assembler simply uses register $zero. Just one tiny optimization
9886 * we're missing.
9887 */
9888static void
833794fc 9889macro (struct mips_cl_insn *ip, char *str)
252b5132 9890{
c0ebe874
RS
9891 const struct mips_operand_array *operands;
9892 unsigned int breg, i;
741fe287 9893 unsigned int tempreg;
252b5132 9894 int mask;
43841e91 9895 int used_at = 0;
df58fc94 9896 expressionS label_expr;
252b5132 9897 expressionS expr1;
df58fc94 9898 expressionS *ep;
252b5132
RH
9899 const char *s;
9900 const char *s2;
9901 const char *fmt;
9902 int likely = 0;
252b5132 9903 int coproc = 0;
7f3c4072 9904 int offbits = 16;
1abe91b1 9905 int call = 0;
df58fc94
RS
9906 int jals = 0;
9907 int dbl = 0;
9908 int imm = 0;
9909 int ust = 0;
9910 int lp = 0;
f2ae14a1 9911 bfd_boolean large_offset;
252b5132 9912 int off;
252b5132 9913 int hold_mips_optimize;
f2ae14a1 9914 unsigned int align;
c0ebe874 9915 unsigned int op[MAX_OPERANDS];
252b5132 9916
9c2799c2 9917 gas_assert (! mips_opts.mips16);
252b5132 9918
c0ebe874
RS
9919 operands = insn_operands (ip);
9920 for (i = 0; i < MAX_OPERANDS; i++)
9921 if (operands->operand[i])
9922 op[i] = insn_extract_operand (ip, operands->operand[i]);
9923 else
9924 op[i] = -1;
9925
252b5132
RH
9926 mask = ip->insn_mo->mask;
9927
df58fc94
RS
9928 label_expr.X_op = O_constant;
9929 label_expr.X_op_symbol = NULL;
9930 label_expr.X_add_symbol = NULL;
9931 label_expr.X_add_number = 0;
9932
252b5132
RH
9933 expr1.X_op = O_constant;
9934 expr1.X_op_symbol = NULL;
9935 expr1.X_add_symbol = NULL;
9936 expr1.X_add_number = 1;
f2ae14a1 9937 align = 1;
252b5132
RH
9938
9939 switch (mask)
9940 {
9941 case M_DABS:
9942 dbl = 1;
1a0670f3 9943 /* Fall through. */
252b5132 9944 case M_ABS:
df58fc94
RS
9945 /* bgez $a0,1f
9946 move v0,$a0
9947 sub v0,$zero,$a0
9948 1:
9949 */
252b5132 9950
7d10b47d 9951 start_noreorder ();
252b5132 9952
df58fc94
RS
9953 if (mips_opts.micromips)
9954 micromips_label_expr (&label_expr);
9955 else
9956 label_expr.X_add_number = 8;
c0ebe874
RS
9957 macro_build (&label_expr, "bgez", "s,p", op[1]);
9958 if (op[0] == op[1])
a605d2b3 9959 macro_build (NULL, "nop", "");
252b5132 9960 else
c0ebe874
RS
9961 move_register (op[0], op[1]);
9962 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9963 if (mips_opts.micromips)
9964 micromips_add_label ();
252b5132 9965
7d10b47d 9966 end_noreorder ();
8fc2e39e 9967 break;
252b5132
RH
9968
9969 case M_ADD_I:
9970 s = "addi";
9971 s2 = "add";
9972 goto do_addi;
9973 case M_ADDU_I:
9974 s = "addiu";
9975 s2 = "addu";
9976 goto do_addi;
9977 case M_DADD_I:
9978 dbl = 1;
9979 s = "daddi";
9980 s2 = "dadd";
df58fc94
RS
9981 if (!mips_opts.micromips)
9982 goto do_addi;
b0e6f033 9983 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
9984 && imm_expr.X_add_number < 0x200)
9985 {
b0e6f033
RS
9986 macro_build (NULL, s, "t,r,.", op[0], op[1],
9987 (int) imm_expr.X_add_number);
df58fc94
RS
9988 break;
9989 }
9990 goto do_addi_i;
252b5132
RH
9991 case M_DADDU_I:
9992 dbl = 1;
9993 s = "daddiu";
9994 s2 = "daddu";
9995 do_addi:
b0e6f033 9996 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
9997 && imm_expr.X_add_number < 0x8000)
9998 {
c0ebe874 9999 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10000 break;
252b5132 10001 }
df58fc94 10002 do_addi_i:
8fc2e39e 10003 used_at = 1;
67c0d1eb 10004 load_register (AT, &imm_expr, dbl);
c0ebe874 10005 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10006 break;
10007
10008 case M_AND_I:
10009 s = "andi";
10010 s2 = "and";
10011 goto do_bit;
10012 case M_OR_I:
10013 s = "ori";
10014 s2 = "or";
10015 goto do_bit;
10016 case M_NOR_I:
10017 s = "";
10018 s2 = "nor";
10019 goto do_bit;
10020 case M_XOR_I:
10021 s = "xori";
10022 s2 = "xor";
10023 do_bit:
b0e6f033 10024 if (imm_expr.X_add_number >= 0
252b5132
RH
10025 && imm_expr.X_add_number < 0x10000)
10026 {
10027 if (mask != M_NOR_I)
c0ebe874 10028 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10029 else
10030 {
67c0d1eb 10031 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10032 op[0], op[1], BFD_RELOC_LO16);
10033 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10034 }
8fc2e39e 10035 break;
252b5132
RH
10036 }
10037
8fc2e39e 10038 used_at = 1;
bad1aba3 10039 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10040 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10041 break;
10042
8b082fb1
TS
10043 case M_BALIGN:
10044 switch (imm_expr.X_add_number)
10045 {
10046 case 0:
10047 macro_build (NULL, "nop", "");
10048 break;
10049 case 2:
c0ebe874 10050 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10051 break;
03f66e8a
MR
10052 case 1:
10053 case 3:
c0ebe874 10054 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10055 (int) imm_expr.X_add_number);
8b082fb1 10056 break;
03f66e8a
MR
10057 default:
10058 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10059 (unsigned long) imm_expr.X_add_number);
10060 break;
8b082fb1
TS
10061 }
10062 break;
10063
df58fc94
RS
10064 case M_BC1FL:
10065 case M_BC1TL:
10066 case M_BC2FL:
10067 case M_BC2TL:
10068 gas_assert (mips_opts.micromips);
10069 macro_build_branch_ccl (mask, &offset_expr,
10070 EXTRACT_OPERAND (1, BCC, *ip));
10071 break;
10072
252b5132 10073 case M_BEQ_I:
252b5132 10074 case M_BEQL_I:
252b5132 10075 case M_BNE_I:
252b5132 10076 case M_BNEL_I:
b0e6f033 10077 if (imm_expr.X_add_number == 0)
c0ebe874 10078 op[1] = 0;
df58fc94 10079 else
252b5132 10080 {
c0ebe874 10081 op[1] = AT;
df58fc94 10082 used_at = 1;
bad1aba3 10083 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10084 }
df58fc94
RS
10085 /* Fall through. */
10086 case M_BEQL:
10087 case M_BNEL:
c0ebe874 10088 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10089 break;
10090
10091 case M_BGEL:
10092 likely = 1;
1a0670f3 10093 /* Fall through. */
252b5132 10094 case M_BGE:
c0ebe874
RS
10095 if (op[1] == 0)
10096 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10097 else if (op[0] == 0)
10098 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10099 else
252b5132 10100 {
df58fc94 10101 used_at = 1;
c0ebe874 10102 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10103 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10104 &offset_expr, AT, ZERO);
252b5132 10105 }
df58fc94
RS
10106 break;
10107
10108 case M_BGEZL:
10109 case M_BGEZALL:
10110 case M_BGTZL:
10111 case M_BLEZL:
10112 case M_BLTZL:
10113 case M_BLTZALL:
c0ebe874 10114 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10115 break;
10116
10117 case M_BGTL_I:
10118 likely = 1;
1a0670f3 10119 /* Fall through. */
252b5132 10120 case M_BGT_I:
90ecf173 10121 /* Check for > max integer. */
b0e6f033 10122 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10123 {
10124 do_false:
90ecf173 10125 /* Result is always false. */
252b5132 10126 if (! likely)
a605d2b3 10127 macro_build (NULL, "nop", "");
252b5132 10128 else
df58fc94 10129 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10130 break;
252b5132 10131 }
f9419b05 10132 ++imm_expr.X_add_number;
252b5132
RH
10133 /* FALLTHROUGH */
10134 case M_BGE_I:
10135 case M_BGEL_I:
10136 if (mask == M_BGEL_I)
10137 likely = 1;
b0e6f033 10138 if (imm_expr.X_add_number == 0)
252b5132 10139 {
df58fc94 10140 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10141 &offset_expr, op[0]);
8fc2e39e 10142 break;
252b5132 10143 }
b0e6f033 10144 if (imm_expr.X_add_number == 1)
252b5132 10145 {
df58fc94 10146 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10147 &offset_expr, op[0]);
8fc2e39e 10148 break;
252b5132 10149 }
b0e6f033 10150 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10151 {
10152 do_true:
10153 /* result is always true */
1661c76c 10154 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10155 macro_build (&offset_expr, "b", "p");
8fc2e39e 10156 break;
252b5132 10157 }
8fc2e39e 10158 used_at = 1;
c0ebe874 10159 set_at (op[0], 0);
df58fc94
RS
10160 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10161 &offset_expr, AT, ZERO);
252b5132
RH
10162 break;
10163
10164 case M_BGEUL:
10165 likely = 1;
1a0670f3 10166 /* Fall through. */
252b5132 10167 case M_BGEU:
c0ebe874 10168 if (op[1] == 0)
252b5132 10169 goto do_true;
c0ebe874 10170 else if (op[0] == 0)
df58fc94 10171 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10172 &offset_expr, ZERO, op[1]);
df58fc94 10173 else
252b5132 10174 {
df58fc94 10175 used_at = 1;
c0ebe874 10176 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10177 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10178 &offset_expr, AT, ZERO);
252b5132 10179 }
252b5132
RH
10180 break;
10181
10182 case M_BGTUL_I:
10183 likely = 1;
1a0670f3 10184 /* Fall through. */
252b5132 10185 case M_BGTU_I:
c0ebe874 10186 if (op[0] == 0
bad1aba3 10187 || (GPR_SIZE == 32
f01dc953 10188 && imm_expr.X_add_number == -1))
252b5132 10189 goto do_false;
f9419b05 10190 ++imm_expr.X_add_number;
252b5132
RH
10191 /* FALLTHROUGH */
10192 case M_BGEU_I:
10193 case M_BGEUL_I:
10194 if (mask == M_BGEUL_I)
10195 likely = 1;
b0e6f033 10196 if (imm_expr.X_add_number == 0)
252b5132 10197 goto do_true;
b0e6f033 10198 else if (imm_expr.X_add_number == 1)
df58fc94 10199 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10200 &offset_expr, op[0], ZERO);
df58fc94 10201 else
252b5132 10202 {
df58fc94 10203 used_at = 1;
c0ebe874 10204 set_at (op[0], 1);
df58fc94
RS
10205 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10206 &offset_expr, AT, ZERO);
252b5132 10207 }
252b5132
RH
10208 break;
10209
10210 case M_BGTL:
10211 likely = 1;
1a0670f3 10212 /* Fall through. */
252b5132 10213 case M_BGT:
c0ebe874
RS
10214 if (op[1] == 0)
10215 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10216 else if (op[0] == 0)
10217 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10218 else
252b5132 10219 {
df58fc94 10220 used_at = 1;
c0ebe874 10221 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10222 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10223 &offset_expr, AT, ZERO);
252b5132 10224 }
252b5132
RH
10225 break;
10226
10227 case M_BGTUL:
10228 likely = 1;
1a0670f3 10229 /* Fall through. */
252b5132 10230 case M_BGTU:
c0ebe874 10231 if (op[1] == 0)
df58fc94 10232 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10233 &offset_expr, op[0], ZERO);
10234 else if (op[0] == 0)
df58fc94
RS
10235 goto do_false;
10236 else
252b5132 10237 {
df58fc94 10238 used_at = 1;
c0ebe874 10239 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10240 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10241 &offset_expr, AT, ZERO);
252b5132 10242 }
252b5132
RH
10243 break;
10244
10245 case M_BLEL:
10246 likely = 1;
1a0670f3 10247 /* Fall through. */
252b5132 10248 case M_BLE:
c0ebe874
RS
10249 if (op[1] == 0)
10250 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10251 else if (op[0] == 0)
10252 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10253 else
252b5132 10254 {
df58fc94 10255 used_at = 1;
c0ebe874 10256 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10257 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10258 &offset_expr, AT, ZERO);
252b5132 10259 }
252b5132
RH
10260 break;
10261
10262 case M_BLEL_I:
10263 likely = 1;
1a0670f3 10264 /* Fall through. */
252b5132 10265 case M_BLE_I:
b0e6f033 10266 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10267 goto do_true;
f9419b05 10268 ++imm_expr.X_add_number;
252b5132
RH
10269 /* FALLTHROUGH */
10270 case M_BLT_I:
10271 case M_BLTL_I:
10272 if (mask == M_BLTL_I)
10273 likely = 1;
b0e6f033 10274 if (imm_expr.X_add_number == 0)
c0ebe874 10275 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10276 else if (imm_expr.X_add_number == 1)
c0ebe874 10277 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10278 else
252b5132 10279 {
df58fc94 10280 used_at = 1;
c0ebe874 10281 set_at (op[0], 0);
df58fc94
RS
10282 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10283 &offset_expr, AT, ZERO);
252b5132 10284 }
252b5132
RH
10285 break;
10286
10287 case M_BLEUL:
10288 likely = 1;
1a0670f3 10289 /* Fall through. */
252b5132 10290 case M_BLEU:
c0ebe874 10291 if (op[1] == 0)
df58fc94 10292 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10293 &offset_expr, op[0], ZERO);
10294 else if (op[0] == 0)
df58fc94
RS
10295 goto do_true;
10296 else
252b5132 10297 {
df58fc94 10298 used_at = 1;
c0ebe874 10299 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10300 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10301 &offset_expr, AT, ZERO);
252b5132 10302 }
252b5132
RH
10303 break;
10304
10305 case M_BLEUL_I:
10306 likely = 1;
1a0670f3 10307 /* Fall through. */
252b5132 10308 case M_BLEU_I:
c0ebe874 10309 if (op[0] == 0
bad1aba3 10310 || (GPR_SIZE == 32
f01dc953 10311 && imm_expr.X_add_number == -1))
252b5132 10312 goto do_true;
f9419b05 10313 ++imm_expr.X_add_number;
252b5132
RH
10314 /* FALLTHROUGH */
10315 case M_BLTU_I:
10316 case M_BLTUL_I:
10317 if (mask == M_BLTUL_I)
10318 likely = 1;
b0e6f033 10319 if (imm_expr.X_add_number == 0)
252b5132 10320 goto do_false;
b0e6f033 10321 else if (imm_expr.X_add_number == 1)
df58fc94 10322 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10323 &offset_expr, op[0], ZERO);
df58fc94 10324 else
252b5132 10325 {
df58fc94 10326 used_at = 1;
c0ebe874 10327 set_at (op[0], 1);
df58fc94
RS
10328 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10329 &offset_expr, AT, ZERO);
252b5132 10330 }
252b5132
RH
10331 break;
10332
10333 case M_BLTL:
10334 likely = 1;
1a0670f3 10335 /* Fall through. */
252b5132 10336 case M_BLT:
c0ebe874
RS
10337 if (op[1] == 0)
10338 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10339 else if (op[0] == 0)
10340 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10341 else
252b5132 10342 {
df58fc94 10343 used_at = 1;
c0ebe874 10344 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10345 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10346 &offset_expr, AT, ZERO);
252b5132 10347 }
252b5132
RH
10348 break;
10349
10350 case M_BLTUL:
10351 likely = 1;
1a0670f3 10352 /* Fall through. */
252b5132 10353 case M_BLTU:
c0ebe874 10354 if (op[1] == 0)
252b5132 10355 goto do_false;
c0ebe874 10356 else if (op[0] == 0)
df58fc94 10357 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10358 &offset_expr, ZERO, op[1]);
df58fc94 10359 else
252b5132 10360 {
df58fc94 10361 used_at = 1;
c0ebe874 10362 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10363 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10364 &offset_expr, AT, ZERO);
252b5132 10365 }
252b5132
RH
10366 break;
10367
10368 case M_DDIV_3:
10369 dbl = 1;
1a0670f3 10370 /* Fall through. */
252b5132
RH
10371 case M_DIV_3:
10372 s = "mflo";
10373 goto do_div3;
10374 case M_DREM_3:
10375 dbl = 1;
1a0670f3 10376 /* Fall through. */
252b5132
RH
10377 case M_REM_3:
10378 s = "mfhi";
10379 do_div3:
c0ebe874 10380 if (op[2] == 0)
252b5132 10381 {
1661c76c 10382 as_warn (_("divide by zero"));
252b5132 10383 if (mips_trap)
df58fc94 10384 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10385 else
df58fc94 10386 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10387 break;
252b5132
RH
10388 }
10389
7d10b47d 10390 start_noreorder ();
252b5132
RH
10391 if (mips_trap)
10392 {
c0ebe874
RS
10393 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10394 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10395 }
10396 else
10397 {
df58fc94
RS
10398 if (mips_opts.micromips)
10399 micromips_label_expr (&label_expr);
10400 else
10401 label_expr.X_add_number = 8;
c0ebe874
RS
10402 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10403 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10404 macro_build (NULL, "break", BRK_FMT, 7);
10405 if (mips_opts.micromips)
10406 micromips_add_label ();
252b5132
RH
10407 }
10408 expr1.X_add_number = -1;
8fc2e39e 10409 used_at = 1;
f6a22291 10410 load_register (AT, &expr1, dbl);
df58fc94
RS
10411 if (mips_opts.micromips)
10412 micromips_label_expr (&label_expr);
10413 else
10414 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10415 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10416 if (dbl)
10417 {
10418 expr1.X_add_number = 1;
f6a22291 10419 load_register (AT, &expr1, dbl);
df58fc94 10420 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10421 }
10422 else
10423 {
10424 expr1.X_add_number = 0x80000000;
df58fc94 10425 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10426 }
10427 if (mips_trap)
10428 {
c0ebe874 10429 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10430 /* We want to close the noreorder block as soon as possible, so
10431 that later insns are available for delay slot filling. */
7d10b47d 10432 end_noreorder ();
252b5132
RH
10433 }
10434 else
10435 {
df58fc94
RS
10436 if (mips_opts.micromips)
10437 micromips_label_expr (&label_expr);
10438 else
10439 label_expr.X_add_number = 8;
c0ebe874 10440 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10441 macro_build (NULL, "nop", "");
252b5132
RH
10442
10443 /* We want to close the noreorder block as soon as possible, so
10444 that later insns are available for delay slot filling. */
7d10b47d 10445 end_noreorder ();
252b5132 10446
df58fc94 10447 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10448 }
df58fc94
RS
10449 if (mips_opts.micromips)
10450 micromips_add_label ();
c0ebe874 10451 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10452 break;
10453
10454 case M_DIV_3I:
10455 s = "div";
10456 s2 = "mflo";
10457 goto do_divi;
10458 case M_DIVU_3I:
10459 s = "divu";
10460 s2 = "mflo";
10461 goto do_divi;
10462 case M_REM_3I:
10463 s = "div";
10464 s2 = "mfhi";
10465 goto do_divi;
10466 case M_REMU_3I:
10467 s = "divu";
10468 s2 = "mfhi";
10469 goto do_divi;
10470 case M_DDIV_3I:
10471 dbl = 1;
10472 s = "ddiv";
10473 s2 = "mflo";
10474 goto do_divi;
10475 case M_DDIVU_3I:
10476 dbl = 1;
10477 s = "ddivu";
10478 s2 = "mflo";
10479 goto do_divi;
10480 case M_DREM_3I:
10481 dbl = 1;
10482 s = "ddiv";
10483 s2 = "mfhi";
10484 goto do_divi;
10485 case M_DREMU_3I:
10486 dbl = 1;
10487 s = "ddivu";
10488 s2 = "mfhi";
10489 do_divi:
b0e6f033 10490 if (imm_expr.X_add_number == 0)
252b5132 10491 {
1661c76c 10492 as_warn (_("divide by zero"));
252b5132 10493 if (mips_trap)
df58fc94 10494 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10495 else
df58fc94 10496 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10497 break;
252b5132 10498 }
b0e6f033 10499 if (imm_expr.X_add_number == 1)
252b5132
RH
10500 {
10501 if (strcmp (s2, "mflo") == 0)
c0ebe874 10502 move_register (op[0], op[1]);
252b5132 10503 else
c0ebe874 10504 move_register (op[0], ZERO);
8fc2e39e 10505 break;
252b5132 10506 }
b0e6f033 10507 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10508 {
10509 if (strcmp (s2, "mflo") == 0)
c0ebe874 10510 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10511 else
c0ebe874 10512 move_register (op[0], ZERO);
8fc2e39e 10513 break;
252b5132
RH
10514 }
10515
8fc2e39e 10516 used_at = 1;
67c0d1eb 10517 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10518 macro_build (NULL, s, "z,s,t", op[1], AT);
10519 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10520 break;
10521
10522 case M_DIVU_3:
10523 s = "divu";
10524 s2 = "mflo";
10525 goto do_divu3;
10526 case M_REMU_3:
10527 s = "divu";
10528 s2 = "mfhi";
10529 goto do_divu3;
10530 case M_DDIVU_3:
10531 s = "ddivu";
10532 s2 = "mflo";
10533 goto do_divu3;
10534 case M_DREMU_3:
10535 s = "ddivu";
10536 s2 = "mfhi";
10537 do_divu3:
7d10b47d 10538 start_noreorder ();
252b5132
RH
10539 if (mips_trap)
10540 {
c0ebe874
RS
10541 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10542 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10543 /* We want to close the noreorder block as soon as possible, so
10544 that later insns are available for delay slot filling. */
7d10b47d 10545 end_noreorder ();
252b5132
RH
10546 }
10547 else
10548 {
df58fc94
RS
10549 if (mips_opts.micromips)
10550 micromips_label_expr (&label_expr);
10551 else
10552 label_expr.X_add_number = 8;
c0ebe874
RS
10553 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10554 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10555
10556 /* We want to close the noreorder block as soon as possible, so
10557 that later insns are available for delay slot filling. */
7d10b47d 10558 end_noreorder ();
df58fc94
RS
10559 macro_build (NULL, "break", BRK_FMT, 7);
10560 if (mips_opts.micromips)
10561 micromips_add_label ();
252b5132 10562 }
c0ebe874 10563 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10564 break;
252b5132 10565
1abe91b1
MR
10566 case M_DLCA_AB:
10567 dbl = 1;
1a0670f3 10568 /* Fall through. */
1abe91b1
MR
10569 case M_LCA_AB:
10570 call = 1;
10571 goto do_la;
252b5132
RH
10572 case M_DLA_AB:
10573 dbl = 1;
1a0670f3 10574 /* Fall through. */
252b5132 10575 case M_LA_AB:
1abe91b1 10576 do_la:
252b5132
RH
10577 /* Load the address of a symbol into a register. If breg is not
10578 zero, we then add a base register to it. */
10579
c0ebe874 10580 breg = op[2];
bad1aba3 10581 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10582 as_warn (_("dla used to load 32-bit register; recommend using la "
10583 "instead"));
3bec30a8 10584
90ecf173 10585 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10586 as_warn (_("la used to load 64-bit address; recommend using dla "
10587 "instead"));
3bec30a8 10588
f2ae14a1 10589 if (small_offset_p (0, align, 16))
0c11417f 10590 {
c0ebe874 10591 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10592 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10593 break;
0c11417f
MR
10594 }
10595
c0ebe874 10596 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10597 {
10598 tempreg = AT;
10599 used_at = 1;
10600 }
10601 else
c0ebe874 10602 tempreg = op[0];
afdbd6d0 10603
252b5132
RH
10604 if (offset_expr.X_op != O_symbol
10605 && offset_expr.X_op != O_constant)
10606 {
1661c76c 10607 as_bad (_("expression too complex"));
252b5132
RH
10608 offset_expr.X_op = O_constant;
10609 }
10610
252b5132 10611 if (offset_expr.X_op == O_constant)
aed1a261 10612 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10613 else if (mips_pic == NO_PIC)
10614 {
d6bc6245 10615 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10616 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10617 Otherwise we want
10618 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10619 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10620 If we have a constant, we need two instructions anyhow,
d6bc6245 10621 so we may as well always use the latter form.
76b3015f 10622
6caf9ef4
TS
10623 With 64bit address space and a usable $at we want
10624 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10625 lui $at,<sym> (BFD_RELOC_HI16_S)
10626 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10627 daddiu $at,<sym> (BFD_RELOC_LO16)
10628 dsll32 $tempreg,0
10629 daddu $tempreg,$tempreg,$at
10630
10631 If $at is already in use, we use a path which is suboptimal
10632 on superscalar processors.
10633 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10634 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10635 dsll $tempreg,16
10636 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10637 dsll $tempreg,16
10638 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10639
10640 For GP relative symbols in 64bit address space we can use
10641 the same sequence as in 32bit address space. */
aed1a261 10642 if (HAVE_64BIT_SYMBOLS)
252b5132 10643 {
6caf9ef4
TS
10644 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10645 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10646 {
10647 relax_start (offset_expr.X_add_symbol);
10648 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10649 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10650 relax_switch ();
10651 }
d6bc6245 10652
741fe287 10653 if (used_at == 0 && mips_opts.at)
98d3f06f 10654 {
df58fc94 10655 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10656 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10657 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10658 AT, BFD_RELOC_HI16_S);
67c0d1eb 10659 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10660 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10661 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10662 AT, AT, BFD_RELOC_LO16);
df58fc94 10663 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10664 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10665 used_at = 1;
10666 }
10667 else
10668 {
df58fc94 10669 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10670 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10671 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10672 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10673 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10674 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10675 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10676 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10677 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10678 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10679 }
6caf9ef4
TS
10680
10681 if (mips_relax.sequence)
10682 relax_end ();
98d3f06f
KH
10683 }
10684 else
10685 {
10686 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10687 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10688 {
4d7206a2 10689 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10690 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10691 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10692 relax_switch ();
98d3f06f 10693 }
6943caf0 10694 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10695 as_bad (_("offset too large"));
67c0d1eb
RS
10696 macro_build_lui (&offset_expr, tempreg);
10697 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10698 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10699 if (mips_relax.sequence)
10700 relax_end ();
98d3f06f 10701 }
252b5132 10702 }
0a44bf69 10703 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10704 {
9117d219
NC
10705 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10706
252b5132
RH
10707 /* If this is a reference to an external symbol, and there
10708 is no constant, we want
10709 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10710 or for lca or if tempreg is PIC_CALL_REG
9117d219 10711 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10712 For a local symbol, we want
10713 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10714 nop
10715 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10716
10717 If we have a small constant, and this is a reference to
10718 an external symbol, we want
10719 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10720 nop
10721 addiu $tempreg,$tempreg,<constant>
10722 For a local symbol, we want the same instruction
10723 sequence, but we output a BFD_RELOC_LO16 reloc on the
10724 addiu instruction.
10725
10726 If we have a large constant, and this is a reference to
10727 an external symbol, we want
10728 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10729 lui $at,<hiconstant>
10730 addiu $at,$at,<loconstant>
10731 addu $tempreg,$tempreg,$at
10732 For a local symbol, we want the same instruction
10733 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10734 addiu instruction.
ed6fb7bd
SC
10735 */
10736
4d7206a2 10737 if (offset_expr.X_add_number == 0)
252b5132 10738 {
0a44bf69
RS
10739 if (mips_pic == SVR4_PIC
10740 && breg == 0
10741 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10742 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10743
10744 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10745 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10746 lw_reloc_type, mips_gp_register);
4d7206a2 10747 if (breg != 0)
252b5132
RH
10748 {
10749 /* We're going to put in an addu instruction using
10750 tempreg, so we may as well insert the nop right
10751 now. */
269137b2 10752 load_delay_nop ();
252b5132 10753 }
4d7206a2 10754 relax_switch ();
67c0d1eb
RS
10755 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10756 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10757 load_delay_nop ();
67c0d1eb
RS
10758 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10759 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10760 relax_end ();
252b5132
RH
10761 /* FIXME: If breg == 0, and the next instruction uses
10762 $tempreg, then if this variant case is used an extra
10763 nop will be generated. */
10764 }
4d7206a2
RS
10765 else if (offset_expr.X_add_number >= -0x8000
10766 && offset_expr.X_add_number < 0x8000)
252b5132 10767 {
67c0d1eb 10768 load_got_offset (tempreg, &offset_expr);
269137b2 10769 load_delay_nop ();
67c0d1eb 10770 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10771 }
10772 else
10773 {
4d7206a2
RS
10774 expr1.X_add_number = offset_expr.X_add_number;
10775 offset_expr.X_add_number =
43c0598f 10776 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10777 load_got_offset (tempreg, &offset_expr);
f6a22291 10778 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10779 /* If we are going to add in a base register, and the
10780 target register and the base register are the same,
10781 then we are using AT as a temporary register. Since
10782 we want to load the constant into AT, we add our
10783 current AT (from the global offset table) and the
10784 register into the register now, and pretend we were
10785 not using a base register. */
c0ebe874 10786 if (breg == op[0])
252b5132 10787 {
269137b2 10788 load_delay_nop ();
67c0d1eb 10789 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10790 op[0], AT, breg);
252b5132 10791 breg = 0;
c0ebe874 10792 tempreg = op[0];
252b5132 10793 }
f6a22291 10794 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10795 used_at = 1;
10796 }
10797 }
0a44bf69 10798 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10799 {
67c0d1eb 10800 int add_breg_early = 0;
f5040a92
AO
10801
10802 /* If this is a reference to an external, and there is no
10803 constant, or local symbol (*), with or without a
10804 constant, we want
10805 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10806 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10807 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10808
10809 If we have a small constant, and this is a reference to
10810 an external symbol, we want
10811 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10812 addiu $tempreg,$tempreg,<constant>
10813
10814 If we have a large constant, and this is a reference to
10815 an external symbol, we want
10816 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10817 lui $at,<hiconstant>
10818 addiu $at,$at,<loconstant>
10819 addu $tempreg,$tempreg,$at
10820
10821 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10822 local symbols, even though it introduces an additional
10823 instruction. */
10824
f5040a92
AO
10825 if (offset_expr.X_add_number)
10826 {
4d7206a2 10827 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10828 offset_expr.X_add_number = 0;
10829
4d7206a2 10830 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10831 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10832 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10833
10834 if (expr1.X_add_number >= -0x8000
10835 && expr1.X_add_number < 0x8000)
10836 {
67c0d1eb
RS
10837 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10838 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10839 }
ecd13cd3 10840 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10841 {
c0ebe874
RS
10842 unsigned int dreg;
10843
f5040a92
AO
10844 /* If we are going to add in a base register, and the
10845 target register and the base register are the same,
10846 then we are using AT as a temporary register. Since
10847 we want to load the constant into AT, we add our
10848 current AT (from the global offset table) and the
10849 register into the register now, and pretend we were
10850 not using a base register. */
c0ebe874 10851 if (breg != op[0])
f5040a92
AO
10852 dreg = tempreg;
10853 else
10854 {
9c2799c2 10855 gas_assert (tempreg == AT);
67c0d1eb 10856 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10857 op[0], AT, breg);
10858 dreg = op[0];
67c0d1eb 10859 add_breg_early = 1;
f5040a92
AO
10860 }
10861
f6a22291 10862 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10863 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10864 dreg, dreg, AT);
f5040a92 10865
f5040a92
AO
10866 used_at = 1;
10867 }
10868 else
10869 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10870
4d7206a2 10871 relax_switch ();
f5040a92
AO
10872 offset_expr.X_add_number = expr1.X_add_number;
10873
67c0d1eb
RS
10874 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10875 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10876 if (add_breg_early)
f5040a92 10877 {
67c0d1eb 10878 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10879 op[0], tempreg, breg);
f5040a92 10880 breg = 0;
c0ebe874 10881 tempreg = op[0];
f5040a92 10882 }
4d7206a2 10883 relax_end ();
f5040a92 10884 }
4d7206a2 10885 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 10886 {
4d7206a2 10887 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10888 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10889 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 10890 relax_switch ();
67c0d1eb
RS
10891 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10892 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 10893 relax_end ();
f5040a92 10894 }
4d7206a2 10895 else
f5040a92 10896 {
67c0d1eb
RS
10897 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10898 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10899 }
10900 }
0a44bf69 10901 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10902 {
67c0d1eb 10903 int gpdelay;
9117d219
NC
10904 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10905 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10906 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10907
10908 /* This is the large GOT case. If this is a reference to an
10909 external symbol, and there is no constant, we want
10910 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10911 addu $tempreg,$tempreg,$gp
10912 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10913 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10914 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10915 addu $tempreg,$tempreg,$gp
10916 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10917 For a local symbol, we want
10918 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10919 nop
10920 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10921
10922 If we have a small constant, and this is a reference to
10923 an external symbol, we want
10924 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10925 addu $tempreg,$tempreg,$gp
10926 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10927 nop
10928 addiu $tempreg,$tempreg,<constant>
10929 For a local symbol, we want
10930 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10931 nop
10932 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10933
10934 If we have a large constant, and this is a reference to
10935 an external symbol, we want
10936 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10937 addu $tempreg,$tempreg,$gp
10938 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10939 lui $at,<hiconstant>
10940 addiu $at,$at,<loconstant>
10941 addu $tempreg,$tempreg,$at
10942 For a local symbol, we want
10943 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10944 lui $at,<hiconstant>
10945 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10946 addu $tempreg,$tempreg,$at
f5040a92 10947 */
438c16b8 10948
252b5132
RH
10949 expr1.X_add_number = offset_expr.X_add_number;
10950 offset_expr.X_add_number = 0;
4d7206a2 10951 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10952 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10953 if (expr1.X_add_number == 0 && breg == 0
10954 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10955 {
10956 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10957 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10958 }
df58fc94 10959 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10960 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10961 tempreg, tempreg, mips_gp_register);
67c0d1eb 10962 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10963 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10964 if (expr1.X_add_number == 0)
10965 {
67c0d1eb 10966 if (breg != 0)
252b5132
RH
10967 {
10968 /* We're going to put in an addu instruction using
10969 tempreg, so we may as well insert the nop right
10970 now. */
269137b2 10971 load_delay_nop ();
252b5132 10972 }
252b5132
RH
10973 }
10974 else if (expr1.X_add_number >= -0x8000
10975 && expr1.X_add_number < 0x8000)
10976 {
269137b2 10977 load_delay_nop ();
67c0d1eb 10978 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10979 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10980 }
10981 else
10982 {
c0ebe874
RS
10983 unsigned int dreg;
10984
252b5132
RH
10985 /* If we are going to add in a base register, and the
10986 target register and the base register are the same,
10987 then we are using AT as a temporary register. Since
10988 we want to load the constant into AT, we add our
10989 current AT (from the global offset table) and the
10990 register into the register now, and pretend we were
10991 not using a base register. */
c0ebe874 10992 if (breg != op[0])
67c0d1eb 10993 dreg = tempreg;
252b5132
RH
10994 else
10995 {
9c2799c2 10996 gas_assert (tempreg == AT);
269137b2 10997 load_delay_nop ();
67c0d1eb 10998 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10999 op[0], AT, breg);
11000 dreg = op[0];
252b5132
RH
11001 }
11002
f6a22291 11003 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11004 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11005
252b5132
RH
11006 used_at = 1;
11007 }
43c0598f 11008 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11009 relax_switch ();
252b5132 11010
67c0d1eb 11011 if (gpdelay)
252b5132
RH
11012 {
11013 /* This is needed because this instruction uses $gp, but
f5040a92 11014 the first instruction on the main stream does not. */
67c0d1eb 11015 macro_build (NULL, "nop", "");
252b5132 11016 }
ed6fb7bd 11017
67c0d1eb
RS
11018 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11019 local_reloc_type, mips_gp_register);
f5040a92 11020 if (expr1.X_add_number >= -0x8000
252b5132
RH
11021 && expr1.X_add_number < 0x8000)
11022 {
269137b2 11023 load_delay_nop ();
67c0d1eb
RS
11024 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11025 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11026 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11027 register, the external symbol case ended with a load,
11028 so if the symbol turns out to not be external, and
11029 the next instruction uses tempreg, an unnecessary nop
11030 will be inserted. */
252b5132
RH
11031 }
11032 else
11033 {
c0ebe874 11034 if (breg == op[0])
252b5132
RH
11035 {
11036 /* We must add in the base register now, as in the
f5040a92 11037 external symbol case. */
9c2799c2 11038 gas_assert (tempreg == AT);
269137b2 11039 load_delay_nop ();
67c0d1eb 11040 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11041 op[0], AT, breg);
11042 tempreg = op[0];
252b5132 11043 /* We set breg to 0 because we have arranged to add
f5040a92 11044 it in in both cases. */
252b5132
RH
11045 breg = 0;
11046 }
11047
67c0d1eb
RS
11048 macro_build_lui (&expr1, AT);
11049 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11050 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11051 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11052 tempreg, tempreg, AT);
8fc2e39e 11053 used_at = 1;
252b5132 11054 }
4d7206a2 11055 relax_end ();
252b5132 11056 }
0a44bf69 11057 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11058 {
f5040a92
AO
11059 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11060 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11061 int add_breg_early = 0;
f5040a92
AO
11062
11063 /* This is the large GOT case. If this is a reference to an
11064 external symbol, and there is no constant, we want
11065 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11066 add $tempreg,$tempreg,$gp
11067 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11068 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11069 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11070 add $tempreg,$tempreg,$gp
11071 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11072
11073 If we have a small constant, and this is a reference to
11074 an external symbol, we want
11075 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11076 add $tempreg,$tempreg,$gp
11077 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11078 addi $tempreg,$tempreg,<constant>
11079
11080 If we have a large constant, and this is a reference to
11081 an external symbol, we want
11082 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11083 addu $tempreg,$tempreg,$gp
11084 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11085 lui $at,<hiconstant>
11086 addi $at,$at,<loconstant>
11087 add $tempreg,$tempreg,$at
11088
11089 If we have NewABI, and we know it's a local symbol, we want
11090 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11091 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11092 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11093
4d7206a2 11094 relax_start (offset_expr.X_add_symbol);
f5040a92 11095
4d7206a2 11096 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11097 offset_expr.X_add_number = 0;
11098
1abe91b1
MR
11099 if (expr1.X_add_number == 0 && breg == 0
11100 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11101 {
11102 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11103 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11104 }
df58fc94 11105 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11106 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11107 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11108 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11109 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11110
11111 if (expr1.X_add_number == 0)
4d7206a2 11112 ;
f5040a92
AO
11113 else if (expr1.X_add_number >= -0x8000
11114 && expr1.X_add_number < 0x8000)
11115 {
67c0d1eb 11116 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11117 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11118 }
ecd13cd3 11119 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11120 {
c0ebe874
RS
11121 unsigned int dreg;
11122
f5040a92
AO
11123 /* If we are going to add in a base register, and the
11124 target register and the base register are the same,
11125 then we are using AT as a temporary register. Since
11126 we want to load the constant into AT, we add our
11127 current AT (from the global offset table) and the
11128 register into the register now, and pretend we were
11129 not using a base register. */
c0ebe874 11130 if (breg != op[0])
f5040a92
AO
11131 dreg = tempreg;
11132 else
11133 {
9c2799c2 11134 gas_assert (tempreg == AT);
67c0d1eb 11135 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11136 op[0], AT, breg);
11137 dreg = op[0];
67c0d1eb 11138 add_breg_early = 1;
f5040a92
AO
11139 }
11140
f6a22291 11141 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11142 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11143
f5040a92
AO
11144 used_at = 1;
11145 }
11146 else
11147 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11148
4d7206a2 11149 relax_switch ();
f5040a92 11150 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11151 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11152 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11153 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11154 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11155 if (add_breg_early)
f5040a92 11156 {
67c0d1eb 11157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11158 op[0], tempreg, breg);
f5040a92 11159 breg = 0;
c0ebe874 11160 tempreg = op[0];
f5040a92 11161 }
4d7206a2 11162 relax_end ();
f5040a92 11163 }
252b5132
RH
11164 else
11165 abort ();
11166
11167 if (breg != 0)
c0ebe874 11168 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11169 break;
11170
52b6b6b9 11171 case M_MSGSND:
df58fc94 11172 gas_assert (!mips_opts.micromips);
c0ebe874 11173 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11174 break;
52b6b6b9
JM
11175
11176 case M_MSGLD:
df58fc94 11177 gas_assert (!mips_opts.micromips);
c8276761 11178 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11179 break;
52b6b6b9
JM
11180
11181 case M_MSGLD_T:
df58fc94 11182 gas_assert (!mips_opts.micromips);
c0ebe874 11183 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11184 break;
52b6b6b9
JM
11185
11186 case M_MSGWAIT:
df58fc94 11187 gas_assert (!mips_opts.micromips);
52b6b6b9 11188 macro_build (NULL, "c2", "C", 3);
c7af4273 11189 break;
52b6b6b9
JM
11190
11191 case M_MSGWAIT_T:
df58fc94 11192 gas_assert (!mips_opts.micromips);
c0ebe874 11193 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11194 break;
52b6b6b9 11195
252b5132
RH
11196 case M_J_A:
11197 /* The j instruction may not be used in PIC code, since it
11198 requires an absolute address. We convert it to a b
11199 instruction. */
11200 if (mips_pic == NO_PIC)
67c0d1eb 11201 macro_build (&offset_expr, "j", "a");
252b5132 11202 else
67c0d1eb 11203 macro_build (&offset_expr, "b", "p");
8fc2e39e 11204 break;
252b5132
RH
11205
11206 /* The jal instructions must be handled as macros because when
11207 generating PIC code they expand to multi-instruction
11208 sequences. Normally they are simple instructions. */
df58fc94 11209 case M_JALS_1:
c0ebe874
RS
11210 op[1] = op[0];
11211 op[0] = RA;
df58fc94
RS
11212 /* Fall through. */
11213 case M_JALS_2:
11214 gas_assert (mips_opts.micromips);
833794fc
MR
11215 if (mips_opts.insn32)
11216 {
1661c76c 11217 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11218 break;
11219 }
df58fc94
RS
11220 jals = 1;
11221 goto jal;
252b5132 11222 case M_JAL_1:
c0ebe874
RS
11223 op[1] = op[0];
11224 op[0] = RA;
252b5132
RH
11225 /* Fall through. */
11226 case M_JAL_2:
df58fc94 11227 jal:
3e722fb5 11228 if (mips_pic == NO_PIC)
df58fc94
RS
11229 {
11230 s = jals ? "jalrs" : "jalr";
e64af278 11231 if (mips_opts.micromips
833794fc 11232 && !mips_opts.insn32
c0ebe874 11233 && op[0] == RA
e64af278 11234 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11235 macro_build (NULL, s, "mj", op[1]);
df58fc94 11236 else
c0ebe874 11237 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11238 }
0a44bf69 11239 else
252b5132 11240 {
df58fc94
RS
11241 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11242 && mips_cprestore_offset >= 0);
11243
c0ebe874 11244 if (op[1] != PIC_CALL_REG)
252b5132 11245 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11246
833794fc
MR
11247 s = ((mips_opts.micromips
11248 && !mips_opts.insn32
11249 && (!mips_opts.noreorder || cprestore))
df58fc94 11250 ? "jalrs" : "jalr");
e64af278 11251 if (mips_opts.micromips
833794fc 11252 && !mips_opts.insn32
c0ebe874 11253 && op[0] == RA
e64af278 11254 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11255 macro_build (NULL, s, "mj", op[1]);
df58fc94 11256 else
c0ebe874 11257 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11258 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11259 {
6478892d 11260 if (mips_cprestore_offset < 0)
1661c76c 11261 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11262 else
11263 {
90ecf173 11264 if (!mips_frame_reg_valid)
7a621144 11265 {
1661c76c 11266 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11267 /* Quiet this warning. */
11268 mips_frame_reg_valid = 1;
11269 }
90ecf173 11270 if (!mips_cprestore_valid)
7a621144 11271 {
1661c76c 11272 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11273 /* Quiet this warning. */
11274 mips_cprestore_valid = 1;
11275 }
d3fca0b5
MR
11276 if (mips_opts.noreorder)
11277 macro_build (NULL, "nop", "");
6478892d 11278 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11279 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11280 mips_gp_register,
256ab948
TS
11281 mips_frame_reg,
11282 HAVE_64BIT_ADDRESSES);
6478892d 11283 }
252b5132
RH
11284 }
11285 }
252b5132 11286
8fc2e39e 11287 break;
252b5132 11288
df58fc94
RS
11289 case M_JALS_A:
11290 gas_assert (mips_opts.micromips);
833794fc
MR
11291 if (mips_opts.insn32)
11292 {
1661c76c 11293 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11294 break;
11295 }
df58fc94
RS
11296 jals = 1;
11297 /* Fall through. */
252b5132
RH
11298 case M_JAL_A:
11299 if (mips_pic == NO_PIC)
df58fc94 11300 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11301 else if (mips_pic == SVR4_PIC)
11302 {
11303 /* If this is a reference to an external symbol, and we are
11304 using a small GOT, we want
11305 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11306 nop
f9419b05 11307 jalr $ra,$25
252b5132
RH
11308 nop
11309 lw $gp,cprestore($sp)
11310 The cprestore value is set using the .cprestore
11311 pseudo-op. If we are using a big GOT, we want
11312 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11313 addu $25,$25,$gp
11314 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11315 nop
f9419b05 11316 jalr $ra,$25
252b5132
RH
11317 nop
11318 lw $gp,cprestore($sp)
11319 If the symbol is not external, we want
11320 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11321 nop
11322 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11323 jalr $ra,$25
252b5132 11324 nop
438c16b8 11325 lw $gp,cprestore($sp)
f5040a92
AO
11326
11327 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11328 sequences above, minus nops, unless the symbol is local,
11329 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11330 GOT_DISP. */
438c16b8 11331 if (HAVE_NEWABI)
252b5132 11332 {
90ecf173 11333 if (!mips_big_got)
f5040a92 11334 {
4d7206a2 11335 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11336 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11337 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11338 mips_gp_register);
4d7206a2 11339 relax_switch ();
67c0d1eb
RS
11340 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11341 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11342 mips_gp_register);
11343 relax_end ();
f5040a92
AO
11344 }
11345 else
11346 {
4d7206a2 11347 relax_start (offset_expr.X_add_symbol);
df58fc94 11348 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11349 BFD_RELOC_MIPS_CALL_HI16);
11350 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11351 PIC_CALL_REG, mips_gp_register);
11352 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11353 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11354 PIC_CALL_REG);
4d7206a2 11355 relax_switch ();
67c0d1eb
RS
11356 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11357 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11358 mips_gp_register);
11359 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11360 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11361 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11362 relax_end ();
f5040a92 11363 }
684022ea 11364
df58fc94 11365 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11366 }
11367 else
11368 {
4d7206a2 11369 relax_start (offset_expr.X_add_symbol);
90ecf173 11370 if (!mips_big_got)
438c16b8 11371 {
67c0d1eb
RS
11372 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11373 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11374 mips_gp_register);
269137b2 11375 load_delay_nop ();
4d7206a2 11376 relax_switch ();
438c16b8 11377 }
252b5132 11378 else
252b5132 11379 {
67c0d1eb
RS
11380 int gpdelay;
11381
11382 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11383 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11384 BFD_RELOC_MIPS_CALL_HI16);
11385 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11386 PIC_CALL_REG, mips_gp_register);
11387 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11388 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11389 PIC_CALL_REG);
269137b2 11390 load_delay_nop ();
4d7206a2 11391 relax_switch ();
67c0d1eb
RS
11392 if (gpdelay)
11393 macro_build (NULL, "nop", "");
252b5132 11394 }
67c0d1eb
RS
11395 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11396 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11397 mips_gp_register);
269137b2 11398 load_delay_nop ();
67c0d1eb
RS
11399 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11400 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11401 relax_end ();
df58fc94 11402 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11403
6478892d 11404 if (mips_cprestore_offset < 0)
1661c76c 11405 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11406 else
11407 {
90ecf173 11408 if (!mips_frame_reg_valid)
7a621144 11409 {
1661c76c 11410 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11411 /* Quiet this warning. */
11412 mips_frame_reg_valid = 1;
11413 }
90ecf173 11414 if (!mips_cprestore_valid)
7a621144 11415 {
1661c76c 11416 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11417 /* Quiet this warning. */
11418 mips_cprestore_valid = 1;
11419 }
6478892d 11420 if (mips_opts.noreorder)
67c0d1eb 11421 macro_build (NULL, "nop", "");
6478892d 11422 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11423 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11424 mips_gp_register,
256ab948
TS
11425 mips_frame_reg,
11426 HAVE_64BIT_ADDRESSES);
6478892d 11427 }
252b5132
RH
11428 }
11429 }
0a44bf69 11430 else if (mips_pic == VXWORKS_PIC)
1661c76c 11431 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11432 else
11433 abort ();
11434
8fc2e39e 11435 break;
252b5132 11436
7f3c4072 11437 case M_LBUE_AB:
7f3c4072
CM
11438 s = "lbue";
11439 fmt = "t,+j(b)";
11440 offbits = 9;
11441 goto ld_st;
11442 case M_LHUE_AB:
7f3c4072
CM
11443 s = "lhue";
11444 fmt = "t,+j(b)";
11445 offbits = 9;
11446 goto ld_st;
11447 case M_LBE_AB:
7f3c4072
CM
11448 s = "lbe";
11449 fmt = "t,+j(b)";
11450 offbits = 9;
11451 goto ld_st;
11452 case M_LHE_AB:
7f3c4072
CM
11453 s = "lhe";
11454 fmt = "t,+j(b)";
11455 offbits = 9;
11456 goto ld_st;
11457 case M_LLE_AB:
7f3c4072
CM
11458 s = "lle";
11459 fmt = "t,+j(b)";
11460 offbits = 9;
11461 goto ld_st;
11462 case M_LWE_AB:
7f3c4072
CM
11463 s = "lwe";
11464 fmt = "t,+j(b)";
11465 offbits = 9;
11466 goto ld_st;
11467 case M_LWLE_AB:
7f3c4072
CM
11468 s = "lwle";
11469 fmt = "t,+j(b)";
11470 offbits = 9;
11471 goto ld_st;
11472 case M_LWRE_AB:
7f3c4072
CM
11473 s = "lwre";
11474 fmt = "t,+j(b)";
11475 offbits = 9;
11476 goto ld_st;
11477 case M_SBE_AB:
7f3c4072
CM
11478 s = "sbe";
11479 fmt = "t,+j(b)";
11480 offbits = 9;
11481 goto ld_st;
11482 case M_SCE_AB:
7f3c4072
CM
11483 s = "sce";
11484 fmt = "t,+j(b)";
11485 offbits = 9;
11486 goto ld_st;
11487 case M_SHE_AB:
7f3c4072
CM
11488 s = "she";
11489 fmt = "t,+j(b)";
11490 offbits = 9;
11491 goto ld_st;
11492 case M_SWE_AB:
7f3c4072
CM
11493 s = "swe";
11494 fmt = "t,+j(b)";
11495 offbits = 9;
11496 goto ld_st;
11497 case M_SWLE_AB:
7f3c4072
CM
11498 s = "swle";
11499 fmt = "t,+j(b)";
11500 offbits = 9;
11501 goto ld_st;
11502 case M_SWRE_AB:
7f3c4072
CM
11503 s = "swre";
11504 fmt = "t,+j(b)";
11505 offbits = 9;
11506 goto ld_st;
dec0624d 11507 case M_ACLR_AB:
dec0624d 11508 s = "aclr";
dec0624d 11509 fmt = "\\,~(b)";
7f3c4072 11510 offbits = 12;
dec0624d
MR
11511 goto ld_st;
11512 case M_ASET_AB:
dec0624d 11513 s = "aset";
dec0624d 11514 fmt = "\\,~(b)";
7f3c4072 11515 offbits = 12;
dec0624d 11516 goto ld_st;
252b5132
RH
11517 case M_LB_AB:
11518 s = "lb";
df58fc94 11519 fmt = "t,o(b)";
252b5132
RH
11520 goto ld;
11521 case M_LBU_AB:
11522 s = "lbu";
df58fc94 11523 fmt = "t,o(b)";
252b5132
RH
11524 goto ld;
11525 case M_LH_AB:
11526 s = "lh";
df58fc94 11527 fmt = "t,o(b)";
252b5132
RH
11528 goto ld;
11529 case M_LHU_AB:
11530 s = "lhu";
df58fc94 11531 fmt = "t,o(b)";
252b5132
RH
11532 goto ld;
11533 case M_LW_AB:
11534 s = "lw";
df58fc94 11535 fmt = "t,o(b)";
252b5132
RH
11536 goto ld;
11537 case M_LWC0_AB:
df58fc94 11538 gas_assert (!mips_opts.micromips);
252b5132 11539 s = "lwc0";
df58fc94 11540 fmt = "E,o(b)";
bdaaa2e1 11541 /* Itbl support may require additional care here. */
252b5132 11542 coproc = 1;
df58fc94 11543 goto ld_st;
252b5132
RH
11544 case M_LWC1_AB:
11545 s = "lwc1";
df58fc94 11546 fmt = "T,o(b)";
bdaaa2e1 11547 /* Itbl support may require additional care here. */
252b5132 11548 coproc = 1;
df58fc94 11549 goto ld_st;
252b5132
RH
11550 case M_LWC2_AB:
11551 s = "lwc2";
df58fc94 11552 fmt = COP12_FMT;
7361da2c
AB
11553 offbits = (mips_opts.micromips ? 12
11554 : ISA_IS_R6 (mips_opts.isa) ? 11
11555 : 16);
bdaaa2e1 11556 /* Itbl support may require additional care here. */
252b5132 11557 coproc = 1;
df58fc94 11558 goto ld_st;
252b5132 11559 case M_LWC3_AB:
df58fc94 11560 gas_assert (!mips_opts.micromips);
252b5132 11561 s = "lwc3";
df58fc94 11562 fmt = "E,o(b)";
bdaaa2e1 11563 /* Itbl support may require additional care here. */
252b5132 11564 coproc = 1;
df58fc94 11565 goto ld_st;
252b5132
RH
11566 case M_LWL_AB:
11567 s = "lwl";
df58fc94 11568 fmt = MEM12_FMT;
7f3c4072 11569 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11570 goto ld_st;
252b5132
RH
11571 case M_LWR_AB:
11572 s = "lwr";
df58fc94 11573 fmt = MEM12_FMT;
7f3c4072 11574 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11575 goto ld_st;
252b5132 11576 case M_LDC1_AB:
252b5132 11577 s = "ldc1";
df58fc94 11578 fmt = "T,o(b)";
bdaaa2e1 11579 /* Itbl support may require additional care here. */
252b5132 11580 coproc = 1;
df58fc94 11581 goto ld_st;
252b5132
RH
11582 case M_LDC2_AB:
11583 s = "ldc2";
df58fc94 11584 fmt = COP12_FMT;
7361da2c
AB
11585 offbits = (mips_opts.micromips ? 12
11586 : ISA_IS_R6 (mips_opts.isa) ? 11
11587 : 16);
bdaaa2e1 11588 /* Itbl support may require additional care here. */
252b5132 11589 coproc = 1;
df58fc94 11590 goto ld_st;
c77c0862 11591 case M_LQC2_AB:
c77c0862 11592 s = "lqc2";
14daeee3 11593 fmt = "+7,o(b)";
c77c0862
RS
11594 /* Itbl support may require additional care here. */
11595 coproc = 1;
11596 goto ld_st;
252b5132
RH
11597 case M_LDC3_AB:
11598 s = "ldc3";
df58fc94 11599 fmt = "E,o(b)";
bdaaa2e1 11600 /* Itbl support may require additional care here. */
252b5132 11601 coproc = 1;
df58fc94 11602 goto ld_st;
252b5132
RH
11603 case M_LDL_AB:
11604 s = "ldl";
df58fc94 11605 fmt = MEM12_FMT;
7f3c4072 11606 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11607 goto ld_st;
252b5132
RH
11608 case M_LDR_AB:
11609 s = "ldr";
df58fc94 11610 fmt = MEM12_FMT;
7f3c4072 11611 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11612 goto ld_st;
252b5132
RH
11613 case M_LL_AB:
11614 s = "ll";
7361da2c
AB
11615 fmt = LL_SC_FMT;
11616 offbits = (mips_opts.micromips ? 12
11617 : ISA_IS_R6 (mips_opts.isa) ? 9
11618 : 16);
252b5132
RH
11619 goto ld;
11620 case M_LLD_AB:
11621 s = "lld";
7361da2c
AB
11622 fmt = LL_SC_FMT;
11623 offbits = (mips_opts.micromips ? 12
11624 : ISA_IS_R6 (mips_opts.isa) ? 9
11625 : 16);
252b5132
RH
11626 goto ld;
11627 case M_LWU_AB:
11628 s = "lwu";
df58fc94 11629 fmt = MEM12_FMT;
7f3c4072 11630 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11631 goto ld;
11632 case M_LWP_AB:
df58fc94
RS
11633 gas_assert (mips_opts.micromips);
11634 s = "lwp";
11635 fmt = "t,~(b)";
7f3c4072 11636 offbits = 12;
df58fc94
RS
11637 lp = 1;
11638 goto ld;
11639 case M_LDP_AB:
df58fc94
RS
11640 gas_assert (mips_opts.micromips);
11641 s = "ldp";
11642 fmt = "t,~(b)";
7f3c4072 11643 offbits = 12;
df58fc94
RS
11644 lp = 1;
11645 goto ld;
11646 case M_LWM_AB:
df58fc94
RS
11647 gas_assert (mips_opts.micromips);
11648 s = "lwm";
11649 fmt = "n,~(b)";
7f3c4072 11650 offbits = 12;
df58fc94
RS
11651 goto ld_st;
11652 case M_LDM_AB:
df58fc94
RS
11653 gas_assert (mips_opts.micromips);
11654 s = "ldm";
11655 fmt = "n,~(b)";
7f3c4072 11656 offbits = 12;
df58fc94
RS
11657 goto ld_st;
11658
252b5132 11659 ld:
f19ccbda 11660 /* We don't want to use $0 as tempreg. */
c0ebe874 11661 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11662 goto ld_st;
252b5132 11663 else
c0ebe874 11664 tempreg = op[0] + lp;
df58fc94
RS
11665 goto ld_noat;
11666
252b5132
RH
11667 case M_SB_AB:
11668 s = "sb";
df58fc94
RS
11669 fmt = "t,o(b)";
11670 goto ld_st;
252b5132
RH
11671 case M_SH_AB:
11672 s = "sh";
df58fc94
RS
11673 fmt = "t,o(b)";
11674 goto ld_st;
252b5132
RH
11675 case M_SW_AB:
11676 s = "sw";
df58fc94
RS
11677 fmt = "t,o(b)";
11678 goto ld_st;
252b5132 11679 case M_SWC0_AB:
df58fc94 11680 gas_assert (!mips_opts.micromips);
252b5132 11681 s = "swc0";
df58fc94 11682 fmt = "E,o(b)";
bdaaa2e1 11683 /* Itbl support may require additional care here. */
252b5132 11684 coproc = 1;
df58fc94 11685 goto ld_st;
252b5132
RH
11686 case M_SWC1_AB:
11687 s = "swc1";
df58fc94 11688 fmt = "T,o(b)";
bdaaa2e1 11689 /* Itbl support may require additional care here. */
252b5132 11690 coproc = 1;
df58fc94 11691 goto ld_st;
252b5132
RH
11692 case M_SWC2_AB:
11693 s = "swc2";
df58fc94 11694 fmt = COP12_FMT;
7361da2c
AB
11695 offbits = (mips_opts.micromips ? 12
11696 : ISA_IS_R6 (mips_opts.isa) ? 11
11697 : 16);
bdaaa2e1 11698 /* Itbl support may require additional care here. */
252b5132 11699 coproc = 1;
df58fc94 11700 goto ld_st;
252b5132 11701 case M_SWC3_AB:
df58fc94 11702 gas_assert (!mips_opts.micromips);
252b5132 11703 s = "swc3";
df58fc94 11704 fmt = "E,o(b)";
bdaaa2e1 11705 /* Itbl support may require additional care here. */
252b5132 11706 coproc = 1;
df58fc94 11707 goto ld_st;
252b5132
RH
11708 case M_SWL_AB:
11709 s = "swl";
df58fc94 11710 fmt = MEM12_FMT;
7f3c4072 11711 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11712 goto ld_st;
252b5132
RH
11713 case M_SWR_AB:
11714 s = "swr";
df58fc94 11715 fmt = MEM12_FMT;
7f3c4072 11716 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11717 goto ld_st;
252b5132
RH
11718 case M_SC_AB:
11719 s = "sc";
7361da2c
AB
11720 fmt = LL_SC_FMT;
11721 offbits = (mips_opts.micromips ? 12
11722 : ISA_IS_R6 (mips_opts.isa) ? 9
11723 : 16);
df58fc94 11724 goto ld_st;
252b5132
RH
11725 case M_SCD_AB:
11726 s = "scd";
7361da2c
AB
11727 fmt = LL_SC_FMT;
11728 offbits = (mips_opts.micromips ? 12
11729 : ISA_IS_R6 (mips_opts.isa) ? 9
11730 : 16);
df58fc94 11731 goto ld_st;
d43b4baf
TS
11732 case M_CACHE_AB:
11733 s = "cache";
7361da2c
AB
11734 fmt = (mips_opts.micromips ? "k,~(b)"
11735 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11736 : "k,o(b)");
11737 offbits = (mips_opts.micromips ? 12
11738 : ISA_IS_R6 (mips_opts.isa) ? 9
11739 : 16);
7f3c4072
CM
11740 goto ld_st;
11741 case M_CACHEE_AB:
7f3c4072
CM
11742 s = "cachee";
11743 fmt = "k,+j(b)";
11744 offbits = 9;
df58fc94 11745 goto ld_st;
3eebd5eb
MR
11746 case M_PREF_AB:
11747 s = "pref";
7361da2c
AB
11748 fmt = (mips_opts.micromips ? "k,~(b)"
11749 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11750 : "k,o(b)");
11751 offbits = (mips_opts.micromips ? 12
11752 : ISA_IS_R6 (mips_opts.isa) ? 9
11753 : 16);
7f3c4072
CM
11754 goto ld_st;
11755 case M_PREFE_AB:
7f3c4072
CM
11756 s = "prefe";
11757 fmt = "k,+j(b)";
11758 offbits = 9;
df58fc94 11759 goto ld_st;
252b5132 11760 case M_SDC1_AB:
252b5132 11761 s = "sdc1";
df58fc94 11762 fmt = "T,o(b)";
252b5132 11763 coproc = 1;
bdaaa2e1 11764 /* Itbl support may require additional care here. */
df58fc94 11765 goto ld_st;
252b5132
RH
11766 case M_SDC2_AB:
11767 s = "sdc2";
df58fc94 11768 fmt = COP12_FMT;
7361da2c
AB
11769 offbits = (mips_opts.micromips ? 12
11770 : ISA_IS_R6 (mips_opts.isa) ? 11
11771 : 16);
c77c0862
RS
11772 /* Itbl support may require additional care here. */
11773 coproc = 1;
11774 goto ld_st;
11775 case M_SQC2_AB:
c77c0862 11776 s = "sqc2";
14daeee3 11777 fmt = "+7,o(b)";
bdaaa2e1 11778 /* Itbl support may require additional care here. */
252b5132 11779 coproc = 1;
df58fc94 11780 goto ld_st;
252b5132 11781 case M_SDC3_AB:
df58fc94 11782 gas_assert (!mips_opts.micromips);
252b5132 11783 s = "sdc3";
df58fc94 11784 fmt = "E,o(b)";
bdaaa2e1 11785 /* Itbl support may require additional care here. */
252b5132 11786 coproc = 1;
df58fc94 11787 goto ld_st;
252b5132
RH
11788 case M_SDL_AB:
11789 s = "sdl";
df58fc94 11790 fmt = MEM12_FMT;
7f3c4072 11791 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11792 goto ld_st;
252b5132
RH
11793 case M_SDR_AB:
11794 s = "sdr";
df58fc94 11795 fmt = MEM12_FMT;
7f3c4072 11796 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11797 goto ld_st;
11798 case M_SWP_AB:
df58fc94
RS
11799 gas_assert (mips_opts.micromips);
11800 s = "swp";
11801 fmt = "t,~(b)";
7f3c4072 11802 offbits = 12;
df58fc94
RS
11803 goto ld_st;
11804 case M_SDP_AB:
df58fc94
RS
11805 gas_assert (mips_opts.micromips);
11806 s = "sdp";
11807 fmt = "t,~(b)";
7f3c4072 11808 offbits = 12;
df58fc94
RS
11809 goto ld_st;
11810 case M_SWM_AB:
df58fc94
RS
11811 gas_assert (mips_opts.micromips);
11812 s = "swm";
11813 fmt = "n,~(b)";
7f3c4072 11814 offbits = 12;
df58fc94
RS
11815 goto ld_st;
11816 case M_SDM_AB:
df58fc94
RS
11817 gas_assert (mips_opts.micromips);
11818 s = "sdm";
11819 fmt = "n,~(b)";
7f3c4072 11820 offbits = 12;
df58fc94
RS
11821
11822 ld_st:
8fc2e39e 11823 tempreg = AT;
df58fc94 11824 ld_noat:
c0ebe874 11825 breg = op[2];
f2ae14a1
RS
11826 if (small_offset_p (0, align, 16))
11827 {
11828 /* The first case exists for M_LD_AB and M_SD_AB, which are
11829 macros for o32 but which should act like normal instructions
11830 otherwise. */
11831 if (offbits == 16)
c0ebe874 11832 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11833 offset_reloc[1], offset_reloc[2], breg);
11834 else if (small_offset_p (0, align, offbits))
11835 {
11836 if (offbits == 0)
c0ebe874 11837 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11838 else
c0ebe874 11839 macro_build (NULL, s, fmt, op[0],
c8276761 11840 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11841 }
11842 else
11843 {
11844 if (tempreg == AT)
11845 used_at = 1;
11846 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11847 tempreg, breg, -1, offset_reloc[0],
11848 offset_reloc[1], offset_reloc[2]);
11849 if (offbits == 0)
c0ebe874 11850 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11851 else
c0ebe874 11852 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11853 }
11854 break;
11855 }
11856
11857 if (tempreg == AT)
11858 used_at = 1;
11859
252b5132
RH
11860 if (offset_expr.X_op != O_constant
11861 && offset_expr.X_op != O_symbol)
11862 {
1661c76c 11863 as_bad (_("expression too complex"));
252b5132
RH
11864 offset_expr.X_op = O_constant;
11865 }
11866
2051e8c4
MR
11867 if (HAVE_32BIT_ADDRESSES
11868 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11869 {
11870 char value [32];
11871
11872 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11873 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11874 }
2051e8c4 11875
252b5132
RH
11876 /* A constant expression in PIC code can be handled just as it
11877 is in non PIC code. */
aed1a261
RS
11878 if (offset_expr.X_op == O_constant)
11879 {
f2ae14a1
RS
11880 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11881 offbits == 0 ? 16 : offbits);
11882 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 11883
f2ae14a1
RS
11884 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11885 if (breg != 0)
11886 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11887 tempreg, tempreg, breg);
7f3c4072 11888 if (offbits == 0)
dd6a37e7 11889 {
f2ae14a1 11890 if (offset_expr.X_add_number != 0)
dd6a37e7 11891 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 11892 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 11893 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11894 }
7f3c4072 11895 else if (offbits == 16)
c0ebe874 11896 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 11897 else
c0ebe874 11898 macro_build (NULL, s, fmt, op[0],
c8276761 11899 (int) offset_expr.X_add_number, tempreg);
df58fc94 11900 }
7f3c4072 11901 else if (offbits != 16)
df58fc94 11902 {
7f3c4072 11903 /* The offset field is too narrow to be used for a low-part
2b0f3761 11904 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
11905 register. */
11906 load_address (tempreg, &offset_expr, &used_at);
11907 if (breg != 0)
11908 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11909 tempreg, tempreg, breg);
7f3c4072 11910 if (offbits == 0)
c0ebe874 11911 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11912 else
c0ebe874 11913 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
11914 }
11915 else if (mips_pic == NO_PIC)
252b5132
RH
11916 {
11917 /* If this is a reference to a GP relative symbol, and there
11918 is no base register, we want
c0ebe874 11919 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11920 Otherwise, if there is no base register, we want
11921 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 11922 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
11923 If we have a constant, we need two instructions anyhow,
11924 so we always use the latter form.
11925
11926 If we have a base register, and this is a reference to a
11927 GP relative symbol, we want
11928 addu $tempreg,$breg,$gp
c0ebe874 11929 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11930 Otherwise we want
11931 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11932 addu $tempreg,$tempreg,$breg
c0ebe874 11933 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11934 With a constant we always use the latter case.
76b3015f 11935
d6bc6245
TS
11936 With 64bit address space and no base register and $at usable,
11937 we want
11938 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11939 lui $at,<sym> (BFD_RELOC_HI16_S)
11940 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11941 dsll32 $tempreg,0
11942 daddu $tempreg,$at
c0ebe874 11943 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11944 If we have a base register, we want
11945 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11946 lui $at,<sym> (BFD_RELOC_HI16_S)
11947 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11948 daddu $at,$breg
11949 dsll32 $tempreg,0
11950 daddu $tempreg,$at
c0ebe874 11951 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11952
11953 Without $at we can't generate the optimal path for superscalar
11954 processors here since this would require two temporary registers.
11955 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11956 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11957 dsll $tempreg,16
11958 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11959 dsll $tempreg,16
c0ebe874 11960 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11961 If we have a base register, we want
11962 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11963 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11964 dsll $tempreg,16
11965 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11966 dsll $tempreg,16
11967 daddu $tempreg,$tempreg,$breg
c0ebe874 11968 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11969
6caf9ef4 11970 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11971 the same sequence as in 32bit address space. */
11972 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11973 {
aed1a261 11974 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11975 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11976 {
11977 relax_start (offset_expr.X_add_symbol);
11978 if (breg == 0)
11979 {
c0ebe874 11980 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11981 BFD_RELOC_GPREL16, mips_gp_register);
11982 }
11983 else
11984 {
11985 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11986 tempreg, breg, mips_gp_register);
c0ebe874 11987 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11988 BFD_RELOC_GPREL16, tempreg);
11989 }
11990 relax_switch ();
11991 }
d6bc6245 11992
741fe287 11993 if (used_at == 0 && mips_opts.at)
d6bc6245 11994 {
df58fc94 11995 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11996 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11997 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11998 BFD_RELOC_HI16_S);
11999 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12000 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12001 if (breg != 0)
67c0d1eb 12002 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12003 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12004 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12005 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12006 tempreg);
d6bc6245
TS
12007 used_at = 1;
12008 }
12009 else
12010 {
df58fc94 12011 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12012 BFD_RELOC_MIPS_HIGHEST);
12013 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12014 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12015 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12016 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12017 tempreg, BFD_RELOC_HI16_S);
df58fc94 12018 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12019 if (breg != 0)
67c0d1eb 12020 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12021 tempreg, tempreg, breg);
c0ebe874 12022 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12023 BFD_RELOC_LO16, tempreg);
d6bc6245 12024 }
6caf9ef4
TS
12025
12026 if (mips_relax.sequence)
12027 relax_end ();
8fc2e39e 12028 break;
d6bc6245 12029 }
256ab948 12030
252b5132
RH
12031 if (breg == 0)
12032 {
67c0d1eb 12033 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12034 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12035 {
4d7206a2 12036 relax_start (offset_expr.X_add_symbol);
c0ebe874 12037 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12038 mips_gp_register);
4d7206a2 12039 relax_switch ();
252b5132 12040 }
67c0d1eb 12041 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12042 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12043 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12044 if (mips_relax.sequence)
12045 relax_end ();
252b5132
RH
12046 }
12047 else
12048 {
67c0d1eb 12049 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12050 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12051 {
4d7206a2 12052 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12053 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12054 tempreg, breg, mips_gp_register);
c0ebe874 12055 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12056 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12057 relax_switch ();
252b5132 12058 }
67c0d1eb
RS
12059 macro_build_lui (&offset_expr, tempreg);
12060 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12061 tempreg, tempreg, breg);
c0ebe874 12062 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12063 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12064 if (mips_relax.sequence)
12065 relax_end ();
252b5132
RH
12066 }
12067 }
0a44bf69 12068 else if (!mips_big_got)
252b5132 12069 {
ed6fb7bd 12070 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12071
252b5132
RH
12072 /* If this is a reference to an external symbol, we want
12073 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12074 nop
c0ebe874 12075 <op> op[0],0($tempreg)
252b5132
RH
12076 Otherwise we want
12077 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12078 nop
12079 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12080 <op> op[0],0($tempreg)
f5040a92
AO
12081
12082 For NewABI, we want
12083 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12084 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12085
252b5132
RH
12086 If there is a base register, we add it to $tempreg before
12087 the <op>. If there is a constant, we stick it in the
12088 <op> instruction. We don't handle constants larger than
12089 16 bits, because we have no way to load the upper 16 bits
12090 (actually, we could handle them for the subset of cases
12091 in which we are not using $at). */
9c2799c2 12092 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12093 if (HAVE_NEWABI)
12094 {
67c0d1eb
RS
12095 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12096 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12097 if (breg != 0)
67c0d1eb 12098 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12099 tempreg, tempreg, breg);
c0ebe874 12100 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12101 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12102 break;
12103 }
252b5132
RH
12104 expr1.X_add_number = offset_expr.X_add_number;
12105 offset_expr.X_add_number = 0;
12106 if (expr1.X_add_number < -0x8000
12107 || expr1.X_add_number >= 0x8000)
12108 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12109 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12110 lw_reloc_type, mips_gp_register);
269137b2 12111 load_delay_nop ();
4d7206a2
RS
12112 relax_start (offset_expr.X_add_symbol);
12113 relax_switch ();
67c0d1eb
RS
12114 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12115 tempreg, BFD_RELOC_LO16);
4d7206a2 12116 relax_end ();
252b5132 12117 if (breg != 0)
67c0d1eb 12118 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12119 tempreg, tempreg, breg);
c0ebe874 12120 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12121 }
0a44bf69 12122 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12123 {
67c0d1eb 12124 int gpdelay;
252b5132
RH
12125
12126 /* If this is a reference to an external symbol, we want
12127 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12128 addu $tempreg,$tempreg,$gp
12129 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12130 <op> op[0],0($tempreg)
252b5132
RH
12131 Otherwise we want
12132 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12133 nop
12134 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12135 <op> op[0],0($tempreg)
252b5132
RH
12136 If there is a base register, we add it to $tempreg before
12137 the <op>. If there is a constant, we stick it in the
12138 <op> instruction. We don't handle constants larger than
12139 16 bits, because we have no way to load the upper 16 bits
12140 (actually, we could handle them for the subset of cases
f5040a92 12141 in which we are not using $at). */
9c2799c2 12142 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12143 expr1.X_add_number = offset_expr.X_add_number;
12144 offset_expr.X_add_number = 0;
12145 if (expr1.X_add_number < -0x8000
12146 || expr1.X_add_number >= 0x8000)
12147 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12148 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12149 relax_start (offset_expr.X_add_symbol);
df58fc94 12150 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12151 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12152 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12153 mips_gp_register);
12154 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12155 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12156 relax_switch ();
67c0d1eb
RS
12157 if (gpdelay)
12158 macro_build (NULL, "nop", "");
12159 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12160 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12161 load_delay_nop ();
67c0d1eb
RS
12162 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12163 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12164 relax_end ();
12165
252b5132 12166 if (breg != 0)
67c0d1eb 12167 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12168 tempreg, tempreg, breg);
c0ebe874 12169 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12170 }
0a44bf69 12171 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12172 {
f5040a92
AO
12173 /* If this is a reference to an external symbol, we want
12174 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12175 add $tempreg,$tempreg,$gp
12176 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12177 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12178 Otherwise, for local symbols, we want:
12179 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12180 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12181 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12182 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12183 offset_expr.X_add_number = 0;
12184 if (expr1.X_add_number < -0x8000
12185 || expr1.X_add_number >= 0x8000)
12186 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12187 relax_start (offset_expr.X_add_symbol);
df58fc94 12188 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12189 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12190 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12191 mips_gp_register);
12192 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12193 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12194 if (breg != 0)
67c0d1eb 12195 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12196 tempreg, tempreg, breg);
c0ebe874 12197 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12198
4d7206a2 12199 relax_switch ();
f5040a92 12200 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12201 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12202 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12203 if (breg != 0)
67c0d1eb 12204 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12205 tempreg, tempreg, breg);
c0ebe874 12206 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12207 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12208 relax_end ();
f5040a92 12209 }
252b5132
RH
12210 else
12211 abort ();
12212
252b5132
RH
12213 break;
12214
833794fc
MR
12215 case M_JRADDIUSP:
12216 gas_assert (mips_opts.micromips);
12217 gas_assert (mips_opts.insn32);
12218 start_noreorder ();
12219 macro_build (NULL, "jr", "s", RA);
c0ebe874 12220 expr1.X_add_number = op[0] << 2;
833794fc
MR
12221 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12222 end_noreorder ();
12223 break;
12224
12225 case M_JRC:
12226 gas_assert (mips_opts.micromips);
12227 gas_assert (mips_opts.insn32);
c0ebe874 12228 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12229 if (mips_opts.noreorder)
12230 macro_build (NULL, "nop", "");
12231 break;
12232
252b5132
RH
12233 case M_LI:
12234 case M_LI_S:
c0ebe874 12235 load_register (op[0], &imm_expr, 0);
8fc2e39e 12236 break;
252b5132
RH
12237
12238 case M_DLI:
c0ebe874 12239 load_register (op[0], &imm_expr, 1);
8fc2e39e 12240 break;
252b5132
RH
12241
12242 case M_LI_SS:
12243 if (imm_expr.X_op == O_constant)
12244 {
8fc2e39e 12245 used_at = 1;
67c0d1eb 12246 load_register (AT, &imm_expr, 0);
c0ebe874 12247 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12248 break;
12249 }
12250 else
12251 {
b0e6f033
RS
12252 gas_assert (imm_expr.X_op == O_absent
12253 && offset_expr.X_op == O_symbol
90ecf173
MR
12254 && strcmp (segment_name (S_GET_SEGMENT
12255 (offset_expr.X_add_symbol)),
12256 ".lit4") == 0
12257 && offset_expr.X_add_number == 0);
c0ebe874 12258 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12259 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12260 break;
252b5132
RH
12261 }
12262
12263 case M_LI_D:
ca4e0257
RS
12264 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12265 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12266 order 32 bits of the value and the low order 32 bits are either
12267 zero or in OFFSET_EXPR. */
b0e6f033 12268 if (imm_expr.X_op == O_constant)
252b5132 12269 {
bad1aba3 12270 if (GPR_SIZE == 64)
c0ebe874 12271 load_register (op[0], &imm_expr, 1);
252b5132
RH
12272 else
12273 {
12274 int hreg, lreg;
12275
12276 if (target_big_endian)
12277 {
c0ebe874
RS
12278 hreg = op[0];
12279 lreg = op[0] + 1;
252b5132
RH
12280 }
12281 else
12282 {
c0ebe874
RS
12283 hreg = op[0] + 1;
12284 lreg = op[0];
252b5132
RH
12285 }
12286
12287 if (hreg <= 31)
67c0d1eb 12288 load_register (hreg, &imm_expr, 0);
252b5132
RH
12289 if (lreg <= 31)
12290 {
12291 if (offset_expr.X_op == O_absent)
67c0d1eb 12292 move_register (lreg, 0);
252b5132
RH
12293 else
12294 {
9c2799c2 12295 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12296 load_register (lreg, &offset_expr, 0);
252b5132
RH
12297 }
12298 }
12299 }
8fc2e39e 12300 break;
252b5132 12301 }
b0e6f033 12302 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12303
12304 /* We know that sym is in the .rdata section. First we get the
12305 upper 16 bits of the address. */
12306 if (mips_pic == NO_PIC)
12307 {
67c0d1eb 12308 macro_build_lui (&offset_expr, AT);
8fc2e39e 12309 used_at = 1;
252b5132 12310 }
0a44bf69 12311 else
252b5132 12312 {
67c0d1eb
RS
12313 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12314 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12315 used_at = 1;
252b5132 12316 }
bdaaa2e1 12317
252b5132 12318 /* Now we load the register(s). */
bad1aba3 12319 if (GPR_SIZE == 64)
8fc2e39e
TS
12320 {
12321 used_at = 1;
c0ebe874
RS
12322 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12323 BFD_RELOC_LO16, AT);
8fc2e39e 12324 }
252b5132
RH
12325 else
12326 {
8fc2e39e 12327 used_at = 1;
c0ebe874
RS
12328 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12329 BFD_RELOC_LO16, AT);
12330 if (op[0] != RA)
252b5132
RH
12331 {
12332 /* FIXME: How in the world do we deal with the possible
12333 overflow here? */
12334 offset_expr.X_add_number += 4;
67c0d1eb 12335 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12336 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12337 }
12338 }
252b5132
RH
12339 break;
12340
12341 case M_LI_DD:
ca4e0257
RS
12342 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12343 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12344 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12345 the value and the low order 32 bits are either zero or in
12346 OFFSET_EXPR. */
b0e6f033 12347 if (imm_expr.X_op == O_constant)
252b5132 12348 {
8fc2e39e 12349 used_at = 1;
bad1aba3 12350 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12351 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12352 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12353 else
12354 {
351cdf24
MF
12355 if (ISA_HAS_MXHC1 (mips_opts.isa))
12356 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12357 else if (FPR_SIZE != 32)
12358 as_bad (_("Unable to generate `%s' compliant code "
12359 "without mthc1"),
12360 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12361 else
12362 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12363 if (offset_expr.X_op == O_absent)
c0ebe874 12364 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12365 else
12366 {
9c2799c2 12367 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12368 load_register (AT, &offset_expr, 0);
c0ebe874 12369 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12370 }
12371 }
12372 break;
12373 }
12374
b0e6f033
RS
12375 gas_assert (imm_expr.X_op == O_absent
12376 && offset_expr.X_op == O_symbol
90ecf173 12377 && offset_expr.X_add_number == 0);
252b5132
RH
12378 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12379 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12380 {
12381 op[2] = mips_gp_register;
f2ae14a1
RS
12382 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12383 offset_reloc[1] = BFD_RELOC_UNUSED;
12384 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12385 }
12386 else
12387 {
9c2799c2 12388 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12389 used_at = 1;
0a44bf69 12390 if (mips_pic != NO_PIC)
67c0d1eb
RS
12391 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12392 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12393 else
12394 {
12395 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12396 macro_build_lui (&offset_expr, AT);
252b5132 12397 }
bdaaa2e1 12398
c0ebe874 12399 op[2] = AT;
f2ae14a1
RS
12400 offset_reloc[0] = BFD_RELOC_LO16;
12401 offset_reloc[1] = BFD_RELOC_UNUSED;
12402 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12403 }
f2ae14a1
RS
12404 align = 8;
12405 /* Fall through */
c4a68bea 12406
252b5132
RH
12407 case M_L_DAB:
12408 /*
12409 * The MIPS assembler seems to check for X_add_number not
12410 * being double aligned and generating:
12411 * lui at,%hi(foo+1)
12412 * addu at,at,v1
12413 * addiu at,at,%lo(foo+1)
12414 * lwc1 f2,0(at)
12415 * lwc1 f3,4(at)
12416 * But, the resulting address is the same after relocation so why
12417 * generate the extra instruction?
12418 */
bdaaa2e1 12419 /* Itbl support may require additional care here. */
252b5132 12420 coproc = 1;
df58fc94 12421 fmt = "T,o(b)";
0aa27725 12422 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12423 {
12424 s = "ldc1";
df58fc94 12425 goto ld_st;
252b5132 12426 }
252b5132 12427 s = "lwc1";
252b5132
RH
12428 goto ldd_std;
12429
12430 case M_S_DAB:
df58fc94
RS
12431 gas_assert (!mips_opts.micromips);
12432 /* Itbl support may require additional care here. */
12433 coproc = 1;
12434 fmt = "T,o(b)";
0aa27725 12435 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12436 {
12437 s = "sdc1";
df58fc94 12438 goto ld_st;
252b5132 12439 }
252b5132 12440 s = "swc1";
252b5132
RH
12441 goto ldd_std;
12442
e407c74b
NC
12443 case M_LQ_AB:
12444 fmt = "t,o(b)";
12445 s = "lq";
12446 goto ld;
12447
12448 case M_SQ_AB:
12449 fmt = "t,o(b)";
12450 s = "sq";
12451 goto ld_st;
12452
252b5132 12453 case M_LD_AB:
df58fc94 12454 fmt = "t,o(b)";
bad1aba3 12455 if (GPR_SIZE == 64)
252b5132
RH
12456 {
12457 s = "ld";
12458 goto ld;
12459 }
252b5132 12460 s = "lw";
252b5132
RH
12461 goto ldd_std;
12462
12463 case M_SD_AB:
df58fc94 12464 fmt = "t,o(b)";
bad1aba3 12465 if (GPR_SIZE == 64)
252b5132
RH
12466 {
12467 s = "sd";
df58fc94 12468 goto ld_st;
252b5132 12469 }
252b5132 12470 s = "sw";
252b5132
RH
12471
12472 ldd_std:
f2ae14a1
RS
12473 /* Even on a big endian machine $fn comes before $fn+1. We have
12474 to adjust when loading from memory. We set coproc if we must
12475 load $fn+1 first. */
12476 /* Itbl support may require additional care here. */
12477 if (!target_big_endian)
12478 coproc = 0;
12479
c0ebe874 12480 breg = op[2];
f2ae14a1
RS
12481 if (small_offset_p (0, align, 16))
12482 {
12483 ep = &offset_expr;
12484 if (!small_offset_p (4, align, 16))
12485 {
12486 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12487 -1, offset_reloc[0], offset_reloc[1],
12488 offset_reloc[2]);
12489 expr1.X_add_number = 0;
12490 ep = &expr1;
12491 breg = AT;
12492 used_at = 1;
12493 offset_reloc[0] = BFD_RELOC_LO16;
12494 offset_reloc[1] = BFD_RELOC_UNUSED;
12495 offset_reloc[2] = BFD_RELOC_UNUSED;
12496 }
c0ebe874 12497 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12498 {
12499 ep->X_add_number += 4;
c0ebe874 12500 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12501 offset_reloc[1], offset_reloc[2], breg);
12502 ep->X_add_number -= 4;
c0ebe874 12503 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12504 offset_reloc[1], offset_reloc[2], breg);
12505 }
12506 else
12507 {
c0ebe874 12508 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12509 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12510 breg);
12511 ep->X_add_number += 4;
c0ebe874 12512 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12513 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12514 breg);
12515 }
12516 break;
12517 }
12518
252b5132
RH
12519 if (offset_expr.X_op != O_symbol
12520 && offset_expr.X_op != O_constant)
12521 {
1661c76c 12522 as_bad (_("expression too complex"));
252b5132
RH
12523 offset_expr.X_op = O_constant;
12524 }
12525
2051e8c4
MR
12526 if (HAVE_32BIT_ADDRESSES
12527 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12528 {
12529 char value [32];
12530
12531 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12532 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12533 }
2051e8c4 12534
90ecf173 12535 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12536 {
12537 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12538 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12539 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12540 If we have a base register, we use this
12541 addu $at,$breg,$gp
c0ebe874
RS
12542 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12543 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12544 If this is not a GP relative symbol, we want
12545 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12546 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12547 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12548 If there is a base register, we add it to $at after the
12549 lui instruction. If there is a constant, we always use
12550 the last case. */
39a59cf8
MR
12551 if (offset_expr.X_op == O_symbol
12552 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12553 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12554 {
4d7206a2 12555 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12556 if (breg == 0)
12557 {
c9914766 12558 tempreg = mips_gp_register;
252b5132
RH
12559 }
12560 else
12561 {
67c0d1eb 12562 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12563 AT, breg, mips_gp_register);
252b5132 12564 tempreg = AT;
252b5132
RH
12565 used_at = 1;
12566 }
12567
beae10d5 12568 /* Itbl support may require additional care here. */
c0ebe874 12569 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12570 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12571 offset_expr.X_add_number += 4;
12572
12573 /* Set mips_optimize to 2 to avoid inserting an
12574 undesired nop. */
12575 hold_mips_optimize = mips_optimize;
12576 mips_optimize = 2;
beae10d5 12577 /* Itbl support may require additional care here. */
c0ebe874 12578 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12579 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12580 mips_optimize = hold_mips_optimize;
12581
4d7206a2 12582 relax_switch ();
252b5132 12583
0970e49e 12584 offset_expr.X_add_number -= 4;
252b5132 12585 }
8fc2e39e 12586 used_at = 1;
f2ae14a1
RS
12587 if (offset_high_part (offset_expr.X_add_number, 16)
12588 != offset_high_part (offset_expr.X_add_number + 4, 16))
12589 {
12590 load_address (AT, &offset_expr, &used_at);
12591 offset_expr.X_op = O_constant;
12592 offset_expr.X_add_number = 0;
12593 }
12594 else
12595 macro_build_lui (&offset_expr, AT);
252b5132 12596 if (breg != 0)
67c0d1eb 12597 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12598 /* Itbl support may require additional care here. */
c0ebe874 12599 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12600 BFD_RELOC_LO16, AT);
252b5132
RH
12601 /* FIXME: How do we handle overflow here? */
12602 offset_expr.X_add_number += 4;
beae10d5 12603 /* Itbl support may require additional care here. */
c0ebe874 12604 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12605 BFD_RELOC_LO16, AT);
4d7206a2
RS
12606 if (mips_relax.sequence)
12607 relax_end ();
bdaaa2e1 12608 }
0a44bf69 12609 else if (!mips_big_got)
252b5132 12610 {
252b5132
RH
12611 /* If this is a reference to an external symbol, we want
12612 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12613 nop
c0ebe874
RS
12614 <op> op[0],0($at)
12615 <op> op[0]+1,4($at)
252b5132
RH
12616 Otherwise we want
12617 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12618 nop
c0ebe874
RS
12619 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12620 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12621 If there is a base register we add it to $at before the
12622 lwc1 instructions. If there is a constant we include it
12623 in the lwc1 instructions. */
12624 used_at = 1;
12625 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12626 if (expr1.X_add_number < -0x8000
12627 || expr1.X_add_number >= 0x8000 - 4)
12628 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12629 load_got_offset (AT, &offset_expr);
269137b2 12630 load_delay_nop ();
252b5132 12631 if (breg != 0)
67c0d1eb 12632 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12633
12634 /* Set mips_optimize to 2 to avoid inserting an undesired
12635 nop. */
12636 hold_mips_optimize = mips_optimize;
12637 mips_optimize = 2;
4d7206a2 12638
beae10d5 12639 /* Itbl support may require additional care here. */
4d7206a2 12640 relax_start (offset_expr.X_add_symbol);
c0ebe874 12641 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12642 BFD_RELOC_LO16, AT);
4d7206a2 12643 expr1.X_add_number += 4;
c0ebe874 12644 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12645 BFD_RELOC_LO16, AT);
4d7206a2 12646 relax_switch ();
c0ebe874 12647 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12648 BFD_RELOC_LO16, AT);
4d7206a2 12649 offset_expr.X_add_number += 4;
c0ebe874 12650 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12651 BFD_RELOC_LO16, AT);
4d7206a2 12652 relax_end ();
252b5132 12653
4d7206a2 12654 mips_optimize = hold_mips_optimize;
252b5132 12655 }
0a44bf69 12656 else if (mips_big_got)
252b5132 12657 {
67c0d1eb 12658 int gpdelay;
252b5132
RH
12659
12660 /* If this is a reference to an external symbol, we want
12661 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12662 addu $at,$at,$gp
12663 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12664 nop
c0ebe874
RS
12665 <op> op[0],0($at)
12666 <op> op[0]+1,4($at)
252b5132
RH
12667 Otherwise we want
12668 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12669 nop
c0ebe874
RS
12670 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12671 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12672 If there is a base register we add it to $at before the
12673 lwc1 instructions. If there is a constant we include it
12674 in the lwc1 instructions. */
12675 used_at = 1;
12676 expr1.X_add_number = offset_expr.X_add_number;
12677 offset_expr.X_add_number = 0;
12678 if (expr1.X_add_number < -0x8000
12679 || expr1.X_add_number >= 0x8000 - 4)
12680 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12681 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12682 relax_start (offset_expr.X_add_symbol);
df58fc94 12683 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12684 AT, BFD_RELOC_MIPS_GOT_HI16);
12685 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12686 AT, AT, mips_gp_register);
67c0d1eb 12687 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12688 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12689 load_delay_nop ();
252b5132 12690 if (breg != 0)
67c0d1eb 12691 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12692 /* Itbl support may require additional care here. */
c0ebe874 12693 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12694 BFD_RELOC_LO16, AT);
252b5132
RH
12695 expr1.X_add_number += 4;
12696
12697 /* Set mips_optimize to 2 to avoid inserting an undesired
12698 nop. */
12699 hold_mips_optimize = mips_optimize;
12700 mips_optimize = 2;
beae10d5 12701 /* Itbl support may require additional care here. */
c0ebe874 12702 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12703 BFD_RELOC_LO16, AT);
252b5132
RH
12704 mips_optimize = hold_mips_optimize;
12705 expr1.X_add_number -= 4;
12706
4d7206a2
RS
12707 relax_switch ();
12708 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12709 if (gpdelay)
12710 macro_build (NULL, "nop", "");
12711 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12712 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12713 load_delay_nop ();
252b5132 12714 if (breg != 0)
67c0d1eb 12715 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12716 /* Itbl support may require additional care here. */
c0ebe874 12717 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12718 BFD_RELOC_LO16, AT);
4d7206a2 12719 offset_expr.X_add_number += 4;
252b5132
RH
12720
12721 /* Set mips_optimize to 2 to avoid inserting an undesired
12722 nop. */
12723 hold_mips_optimize = mips_optimize;
12724 mips_optimize = 2;
beae10d5 12725 /* Itbl support may require additional care here. */
c0ebe874 12726 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12727 BFD_RELOC_LO16, AT);
252b5132 12728 mips_optimize = hold_mips_optimize;
4d7206a2 12729 relax_end ();
252b5132 12730 }
252b5132
RH
12731 else
12732 abort ();
12733
252b5132 12734 break;
3739860c 12735
dd6a37e7 12736 case M_SAA_AB:
dd6a37e7 12737 s = "saa";
0db377d0 12738 goto saa_saad;
dd6a37e7 12739 case M_SAAD_AB:
dd6a37e7 12740 s = "saad";
0db377d0
MR
12741 saa_saad:
12742 gas_assert (!mips_opts.micromips);
7f3c4072 12743 offbits = 0;
dd6a37e7
AP
12744 fmt = "t,(b)";
12745 goto ld_st;
12746
252b5132
RH
12747 /* New code added to support COPZ instructions.
12748 This code builds table entries out of the macros in mip_opcodes.
12749 R4000 uses interlocks to handle coproc delays.
12750 Other chips (like the R3000) require nops to be inserted for delays.
12751
f72c8c98 12752 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12753 In order to fill delay slots for non-interlocked chips,
12754 we must have a way to specify delays based on the coprocessor.
12755 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12756 What are the side-effects of the cop instruction?
12757 What cache support might we have and what are its effects?
12758 Both coprocessor & memory require delays. how long???
bdaaa2e1 12759 What registers are read/set/modified?
252b5132
RH
12760
12761 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12762 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12763
12764 case M_COP0:
12765 s = "c0";
12766 goto copz;
12767 case M_COP1:
12768 s = "c1";
12769 goto copz;
12770 case M_COP2:
12771 s = "c2";
12772 goto copz;
12773 case M_COP3:
12774 s = "c3";
12775 copz:
df58fc94 12776 gas_assert (!mips_opts.micromips);
252b5132
RH
12777 /* For now we just do C (same as Cz). The parameter will be
12778 stored in insn_opcode by mips_ip. */
c8276761 12779 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12780 break;
252b5132 12781
ea1fb5dc 12782 case M_MOVE:
c0ebe874 12783 move_register (op[0], op[1]);
8fc2e39e 12784 break;
ea1fb5dc 12785
833794fc
MR
12786 case M_MOVEP:
12787 gas_assert (mips_opts.micromips);
12788 gas_assert (mips_opts.insn32);
c0ebe874
RS
12789 move_register (micromips_to_32_reg_h_map1[op[0]],
12790 micromips_to_32_reg_m_map[op[1]]);
12791 move_register (micromips_to_32_reg_h_map2[op[0]],
12792 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12793 break;
12794
252b5132
RH
12795 case M_DMUL:
12796 dbl = 1;
1a0670f3 12797 /* Fall through. */
252b5132 12798 case M_MUL:
e407c74b 12799 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12800 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12801 op[2]);
e407c74b
NC
12802 else
12803 {
c0ebe874
RS
12804 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12805 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12806 }
8fc2e39e 12807 break;
252b5132
RH
12808
12809 case M_DMUL_I:
12810 dbl = 1;
1a0670f3 12811 /* Fall through. */
252b5132
RH
12812 case M_MUL_I:
12813 /* The MIPS assembler some times generates shifts and adds. I'm
12814 not trying to be that fancy. GCC should do this for us
12815 anyway. */
8fc2e39e 12816 used_at = 1;
67c0d1eb 12817 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12818 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12819 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12820 break;
12821
12822 case M_DMULO_I:
12823 dbl = 1;
1a0670f3 12824 /* Fall through. */
252b5132
RH
12825 case M_MULO_I:
12826 imm = 1;
12827 goto do_mulo;
12828
12829 case M_DMULO:
12830 dbl = 1;
1a0670f3 12831 /* Fall through. */
252b5132
RH
12832 case M_MULO:
12833 do_mulo:
7d10b47d 12834 start_noreorder ();
8fc2e39e 12835 used_at = 1;
252b5132 12836 if (imm)
67c0d1eb 12837 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12838 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12839 op[1], imm ? AT : op[2]);
12840 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12841 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12842 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12843 if (mips_trap)
c0ebe874 12844 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12845 else
12846 {
df58fc94
RS
12847 if (mips_opts.micromips)
12848 micromips_label_expr (&label_expr);
12849 else
12850 label_expr.X_add_number = 8;
c0ebe874 12851 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12852 macro_build (NULL, "nop", "");
df58fc94
RS
12853 macro_build (NULL, "break", BRK_FMT, 6);
12854 if (mips_opts.micromips)
12855 micromips_add_label ();
252b5132 12856 }
7d10b47d 12857 end_noreorder ();
c0ebe874 12858 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12859 break;
12860
12861 case M_DMULOU_I:
12862 dbl = 1;
1a0670f3 12863 /* Fall through. */
252b5132
RH
12864 case M_MULOU_I:
12865 imm = 1;
12866 goto do_mulou;
12867
12868 case M_DMULOU:
12869 dbl = 1;
1a0670f3 12870 /* Fall through. */
252b5132
RH
12871 case M_MULOU:
12872 do_mulou:
7d10b47d 12873 start_noreorder ();
8fc2e39e 12874 used_at = 1;
252b5132 12875 if (imm)
67c0d1eb
RS
12876 load_register (AT, &imm_expr, dbl);
12877 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 12878 op[1], imm ? AT : op[2]);
df58fc94 12879 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 12880 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 12881 if (mips_trap)
df58fc94 12882 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
12883 else
12884 {
df58fc94
RS
12885 if (mips_opts.micromips)
12886 micromips_label_expr (&label_expr);
12887 else
12888 label_expr.X_add_number = 8;
12889 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 12890 macro_build (NULL, "nop", "");
df58fc94
RS
12891 macro_build (NULL, "break", BRK_FMT, 6);
12892 if (mips_opts.micromips)
12893 micromips_add_label ();
252b5132 12894 }
7d10b47d 12895 end_noreorder ();
252b5132
RH
12896 break;
12897
771c7ce4 12898 case M_DROL:
fef14a42 12899 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12900 {
c0ebe874 12901 if (op[0] == op[1])
82dd0097
CD
12902 {
12903 tempreg = AT;
12904 used_at = 1;
12905 }
12906 else
c0ebe874
RS
12907 tempreg = op[0];
12908 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12909 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12910 break;
82dd0097 12911 }
8fc2e39e 12912 used_at = 1;
c0ebe874
RS
12913 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12914 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12915 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12916 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12917 break;
12918
252b5132 12919 case M_ROL:
fef14a42 12920 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12921 {
c0ebe874 12922 if (op[0] == op[1])
82dd0097
CD
12923 {
12924 tempreg = AT;
12925 used_at = 1;
12926 }
12927 else
c0ebe874
RS
12928 tempreg = op[0];
12929 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12930 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12931 break;
82dd0097 12932 }
8fc2e39e 12933 used_at = 1;
c0ebe874
RS
12934 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12935 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12936 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12937 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12938 break;
12939
771c7ce4
TS
12940 case M_DROL_I:
12941 {
12942 unsigned int rot;
e0471c16
TS
12943 const char *l;
12944 const char *rr;
771c7ce4 12945
771c7ce4 12946 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12947 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12948 {
12949 rot = (64 - rot) & 0x3f;
12950 if (rot >= 32)
c0ebe874 12951 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12952 else
c0ebe874 12953 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12954 break;
60b63b72 12955 }
483fc7cd 12956 if (rot == 0)
483fc7cd 12957 {
c0ebe874 12958 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12959 break;
483fc7cd 12960 }
82dd0097 12961 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12962 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12963 rot &= 0x1f;
8fc2e39e 12964 used_at = 1;
c0ebe874
RS
12965 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12966 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12967 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12968 }
12969 break;
12970
252b5132 12971 case M_ROL_I:
771c7ce4
TS
12972 {
12973 unsigned int rot;
12974
771c7ce4 12975 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12976 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12977 {
c0ebe874
RS
12978 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12979 (32 - rot) & 0x1f);
8fc2e39e 12980 break;
60b63b72 12981 }
483fc7cd 12982 if (rot == 0)
483fc7cd 12983 {
c0ebe874 12984 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12985 break;
483fc7cd 12986 }
8fc2e39e 12987 used_at = 1;
c0ebe874
RS
12988 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12989 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12990 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12991 }
12992 break;
12993
12994 case M_DROR:
fef14a42 12995 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12996 {
c0ebe874 12997 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12998 break;
82dd0097 12999 }
8fc2e39e 13000 used_at = 1;
c0ebe874
RS
13001 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13002 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13003 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13004 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13005 break;
13006
13007 case M_ROR:
fef14a42 13008 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13009 {
c0ebe874 13010 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13011 break;
82dd0097 13012 }
8fc2e39e 13013 used_at = 1;
c0ebe874
RS
13014 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13015 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13016 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13017 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13018 break;
13019
771c7ce4
TS
13020 case M_DROR_I:
13021 {
13022 unsigned int rot;
e0471c16
TS
13023 const char *l;
13024 const char *rr;
771c7ce4 13025
771c7ce4 13026 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13027 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13028 {
13029 if (rot >= 32)
c0ebe874 13030 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13031 else
c0ebe874 13032 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13033 break;
82dd0097 13034 }
483fc7cd 13035 if (rot == 0)
483fc7cd 13036 {
c0ebe874 13037 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13038 break;
483fc7cd 13039 }
91d6fa6a 13040 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13041 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13042 rot &= 0x1f;
8fc2e39e 13043 used_at = 1;
c0ebe874
RS
13044 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13045 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13046 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13047 }
13048 break;
13049
252b5132 13050 case M_ROR_I:
771c7ce4
TS
13051 {
13052 unsigned int rot;
13053
771c7ce4 13054 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13055 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13056 {
c0ebe874 13057 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13058 break;
82dd0097 13059 }
483fc7cd 13060 if (rot == 0)
483fc7cd 13061 {
c0ebe874 13062 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13063 break;
483fc7cd 13064 }
8fc2e39e 13065 used_at = 1;
c0ebe874
RS
13066 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13067 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13068 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13069 }
252b5132
RH
13070 break;
13071
252b5132 13072 case M_SEQ:
c0ebe874
RS
13073 if (op[1] == 0)
13074 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13075 else if (op[2] == 0)
13076 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13077 else
13078 {
c0ebe874
RS
13079 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13080 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13081 }
8fc2e39e 13082 break;
252b5132
RH
13083
13084 case M_SEQ_I:
b0e6f033 13085 if (imm_expr.X_add_number == 0)
252b5132 13086 {
c0ebe874 13087 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13088 break;
252b5132 13089 }
c0ebe874 13090 if (op[1] == 0)
252b5132 13091 {
1661c76c 13092 as_warn (_("instruction %s: result is always false"),
252b5132 13093 ip->insn_mo->name);
c0ebe874 13094 move_register (op[0], 0);
8fc2e39e 13095 break;
252b5132 13096 }
dd3cbb7e
NC
13097 if (CPU_HAS_SEQ (mips_opts.arch)
13098 && -512 <= imm_expr.X_add_number
13099 && imm_expr.X_add_number < 512)
13100 {
c0ebe874 13101 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13102 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13103 break;
13104 }
b0e6f033 13105 if (imm_expr.X_add_number >= 0
252b5132 13106 && imm_expr.X_add_number < 0x10000)
c0ebe874 13107 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13108 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13109 && imm_expr.X_add_number < 0)
13110 {
13111 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13112 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13113 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13114 }
dd3cbb7e
NC
13115 else if (CPU_HAS_SEQ (mips_opts.arch))
13116 {
13117 used_at = 1;
bad1aba3 13118 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13119 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13120 break;
13121 }
252b5132
RH
13122 else
13123 {
bad1aba3 13124 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13125 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13126 used_at = 1;
13127 }
c0ebe874 13128 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13129 break;
252b5132 13130
c0ebe874 13131 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13132 s = "slt";
13133 goto sge;
13134 case M_SGEU:
13135 s = "sltu";
13136 sge:
c0ebe874
RS
13137 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13138 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13139 break;
252b5132 13140
c0ebe874 13141 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13142 case M_SGEU_I:
b0e6f033 13143 if (imm_expr.X_add_number >= -0x8000
252b5132 13144 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13145 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13146 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13147 else
13148 {
bad1aba3 13149 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13150 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13151 op[0], op[1], AT);
252b5132
RH
13152 used_at = 1;
13153 }
c0ebe874 13154 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13155 break;
252b5132 13156
c0ebe874 13157 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13158 s = "slt";
13159 goto sgt;
13160 case M_SGTU:
13161 s = "sltu";
13162 sgt:
c0ebe874 13163 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13164 break;
252b5132 13165
c0ebe874 13166 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13167 s = "slt";
13168 goto sgti;
13169 case M_SGTU_I:
13170 s = "sltu";
13171 sgti:
8fc2e39e 13172 used_at = 1;
bad1aba3 13173 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13174 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13175 break;
13176
c0ebe874 13177 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13178 s = "slt";
13179 goto sle;
13180 case M_SLEU:
13181 s = "sltu";
13182 sle:
c0ebe874
RS
13183 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13184 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13185 break;
252b5132 13186
c0ebe874 13187 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13188 s = "slt";
13189 goto slei;
13190 case M_SLEU_I:
13191 s = "sltu";
13192 slei:
8fc2e39e 13193 used_at = 1;
bad1aba3 13194 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13195 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13196 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13197 break;
13198
13199 case M_SLT_I:
b0e6f033 13200 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13201 && imm_expr.X_add_number < 0x8000)
13202 {
c0ebe874
RS
13203 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13204 BFD_RELOC_LO16);
8fc2e39e 13205 break;
252b5132 13206 }
8fc2e39e 13207 used_at = 1;
bad1aba3 13208 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13209 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13210 break;
13211
13212 case M_SLTU_I:
b0e6f033 13213 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13214 && imm_expr.X_add_number < 0x8000)
13215 {
c0ebe874 13216 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13217 BFD_RELOC_LO16);
8fc2e39e 13218 break;
252b5132 13219 }
8fc2e39e 13220 used_at = 1;
bad1aba3 13221 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13222 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13223 break;
13224
13225 case M_SNE:
c0ebe874
RS
13226 if (op[1] == 0)
13227 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13228 else if (op[2] == 0)
13229 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13230 else
13231 {
c0ebe874
RS
13232 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13233 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13234 }
8fc2e39e 13235 break;
252b5132
RH
13236
13237 case M_SNE_I:
b0e6f033 13238 if (imm_expr.X_add_number == 0)
252b5132 13239 {
c0ebe874 13240 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13241 break;
252b5132 13242 }
c0ebe874 13243 if (op[1] == 0)
252b5132 13244 {
1661c76c 13245 as_warn (_("instruction %s: result is always true"),
252b5132 13246 ip->insn_mo->name);
bad1aba3 13247 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13248 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13249 break;
252b5132 13250 }
dd3cbb7e
NC
13251 if (CPU_HAS_SEQ (mips_opts.arch)
13252 && -512 <= imm_expr.X_add_number
13253 && imm_expr.X_add_number < 512)
13254 {
c0ebe874 13255 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13256 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13257 break;
13258 }
b0e6f033 13259 if (imm_expr.X_add_number >= 0
252b5132
RH
13260 && imm_expr.X_add_number < 0x10000)
13261 {
c0ebe874
RS
13262 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13263 BFD_RELOC_LO16);
252b5132 13264 }
b0e6f033 13265 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13266 && imm_expr.X_add_number < 0)
13267 {
13268 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13269 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13270 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13271 }
dd3cbb7e
NC
13272 else if (CPU_HAS_SEQ (mips_opts.arch))
13273 {
13274 used_at = 1;
bad1aba3 13275 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13276 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13277 break;
13278 }
252b5132
RH
13279 else
13280 {
bad1aba3 13281 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13282 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13283 used_at = 1;
13284 }
c0ebe874 13285 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13286 break;
252b5132 13287
df58fc94
RS
13288 case M_SUB_I:
13289 s = "addi";
13290 s2 = "sub";
13291 goto do_subi;
13292 case M_SUBU_I:
13293 s = "addiu";
13294 s2 = "subu";
13295 goto do_subi;
252b5132
RH
13296 case M_DSUB_I:
13297 dbl = 1;
df58fc94
RS
13298 s = "daddi";
13299 s2 = "dsub";
13300 if (!mips_opts.micromips)
13301 goto do_subi;
b0e6f033 13302 if (imm_expr.X_add_number > -0x200
df58fc94 13303 && imm_expr.X_add_number <= 0x200)
252b5132 13304 {
b0e6f033
RS
13305 macro_build (NULL, s, "t,r,.", op[0], op[1],
13306 (int) -imm_expr.X_add_number);
8fc2e39e 13307 break;
252b5132 13308 }
df58fc94 13309 goto do_subi_i;
252b5132
RH
13310 case M_DSUBU_I:
13311 dbl = 1;
df58fc94
RS
13312 s = "daddiu";
13313 s2 = "dsubu";
13314 do_subi:
b0e6f033 13315 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13316 && imm_expr.X_add_number <= 0x8000)
13317 {
13318 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13319 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13320 break;
252b5132 13321 }
df58fc94 13322 do_subi_i:
8fc2e39e 13323 used_at = 1;
67c0d1eb 13324 load_register (AT, &imm_expr, dbl);
c0ebe874 13325 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13326 break;
13327
13328 case M_TEQ_I:
13329 s = "teq";
13330 goto trap;
13331 case M_TGE_I:
13332 s = "tge";
13333 goto trap;
13334 case M_TGEU_I:
13335 s = "tgeu";
13336 goto trap;
13337 case M_TLT_I:
13338 s = "tlt";
13339 goto trap;
13340 case M_TLTU_I:
13341 s = "tltu";
13342 goto trap;
13343 case M_TNE_I:
13344 s = "tne";
13345 trap:
8fc2e39e 13346 used_at = 1;
bad1aba3 13347 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13348 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13349 break;
13350
252b5132 13351 case M_TRUNCWS:
43841e91 13352 case M_TRUNCWD:
df58fc94 13353 gas_assert (!mips_opts.micromips);
0aa27725 13354 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13355 used_at = 1;
252b5132
RH
13356
13357 /*
13358 * Is the double cfc1 instruction a bug in the mips assembler;
13359 * or is there a reason for it?
13360 */
7d10b47d 13361 start_noreorder ();
c0ebe874
RS
13362 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13363 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13364 macro_build (NULL, "nop", "");
252b5132 13365 expr1.X_add_number = 3;
c0ebe874 13366 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13367 expr1.X_add_number = 2;
67c0d1eb
RS
13368 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13369 macro_build (NULL, "ctc1", "t,G", AT, RA);
13370 macro_build (NULL, "nop", "");
13371 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13372 op[0], op[1]);
13373 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13374 macro_build (NULL, "nop", "");
7d10b47d 13375 end_noreorder ();
252b5132
RH
13376 break;
13377
f2ae14a1 13378 case M_ULH_AB:
252b5132 13379 s = "lb";
df58fc94
RS
13380 s2 = "lbu";
13381 off = 1;
13382 goto uld_st;
f2ae14a1 13383 case M_ULHU_AB:
252b5132 13384 s = "lbu";
df58fc94
RS
13385 s2 = "lbu";
13386 off = 1;
13387 goto uld_st;
f2ae14a1 13388 case M_ULW_AB:
df58fc94
RS
13389 s = "lwl";
13390 s2 = "lwr";
7f3c4072 13391 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13392 off = 3;
13393 goto uld_st;
f2ae14a1 13394 case M_ULD_AB:
252b5132
RH
13395 s = "ldl";
13396 s2 = "ldr";
7f3c4072 13397 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13398 off = 7;
df58fc94 13399 goto uld_st;
f2ae14a1 13400 case M_USH_AB:
df58fc94
RS
13401 s = "sb";
13402 s2 = "sb";
13403 off = 1;
13404 ust = 1;
13405 goto uld_st;
f2ae14a1 13406 case M_USW_AB:
df58fc94
RS
13407 s = "swl";
13408 s2 = "swr";
7f3c4072 13409 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13410 off = 3;
df58fc94
RS
13411 ust = 1;
13412 goto uld_st;
f2ae14a1 13413 case M_USD_AB:
df58fc94
RS
13414 s = "sdl";
13415 s2 = "sdr";
7f3c4072 13416 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13417 off = 7;
13418 ust = 1;
13419
13420 uld_st:
c0ebe874 13421 breg = op[2];
f2ae14a1 13422 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13423 ep = &offset_expr;
13424 expr1.X_add_number = 0;
f2ae14a1 13425 if (large_offset)
df58fc94
RS
13426 {
13427 used_at = 1;
13428 tempreg = AT;
f2ae14a1
RS
13429 if (small_offset_p (0, align, 16))
13430 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13431 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13432 else
13433 {
13434 load_address (tempreg, ep, &used_at);
13435 if (breg != 0)
13436 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13437 tempreg, tempreg, breg);
13438 }
13439 offset_reloc[0] = BFD_RELOC_LO16;
13440 offset_reloc[1] = BFD_RELOC_UNUSED;
13441 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13442 breg = tempreg;
c0ebe874 13443 tempreg = op[0];
df58fc94
RS
13444 ep = &expr1;
13445 }
c0ebe874 13446 else if (!ust && op[0] == breg)
8fc2e39e
TS
13447 {
13448 used_at = 1;
13449 tempreg = AT;
13450 }
252b5132 13451 else
c0ebe874 13452 tempreg = op[0];
af22f5b2 13453
df58fc94
RS
13454 if (off == 1)
13455 goto ulh_sh;
252b5132 13456
90ecf173 13457 if (!target_big_endian)
df58fc94 13458 ep->X_add_number += off;
f2ae14a1 13459 if (offbits == 12)
c8276761 13460 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13461 else
13462 macro_build (ep, s, "t,o(b)", tempreg, -1,
13463 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13464
90ecf173 13465 if (!target_big_endian)
df58fc94 13466 ep->X_add_number -= off;
252b5132 13467 else
df58fc94 13468 ep->X_add_number += off;
f2ae14a1 13469 if (offbits == 12)
df58fc94 13470 macro_build (NULL, s2, "t,~(b)",
c8276761 13471 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13472 else
13473 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13474 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13475
df58fc94 13476 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13477 if (!ust && op[0] != tempreg)
df58fc94
RS
13478 {
13479 /* Protect second load's delay slot. */
13480 load_delay_nop ();
c0ebe874 13481 move_register (op[0], tempreg);
df58fc94 13482 }
8fc2e39e 13483 break;
252b5132 13484
df58fc94 13485 ulh_sh:
d6bc6245 13486 used_at = 1;
df58fc94
RS
13487 if (target_big_endian == ust)
13488 ep->X_add_number += off;
c0ebe874 13489 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13490 macro_build (ep, s, "t,o(b)", tempreg, -1,
13491 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13492
13493 /* For halfword transfers we need a temporary register to shuffle
13494 bytes. Unfortunately for M_USH_A we have none available before
13495 the next store as AT holds the base address. We deal with this
13496 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13497 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13498 if (ust)
c0ebe874 13499 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13500
13501 if (target_big_endian == ust)
13502 ep->X_add_number -= off;
252b5132 13503 else
df58fc94 13504 ep->X_add_number += off;
f2ae14a1
RS
13505 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13506 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13507
df58fc94 13508 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13509 if (ust && large_offset)
df58fc94
RS
13510 {
13511 if (target_big_endian)
13512 ep->X_add_number += off;
13513 else
13514 ep->X_add_number -= off;
f2ae14a1
RS
13515 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13516 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13517 }
13518 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13519 if (!ust || large_offset)
df58fc94 13520 {
c0ebe874 13521 tempreg = !large_offset ? AT : op[0];
df58fc94 13522 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13523 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13524 }
252b5132
RH
13525 break;
13526
13527 default:
13528 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13529 are added dynamically. */
1661c76c 13530 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13531 break;
13532 }
741fe287 13533 if (!mips_opts.at && used_at)
1661c76c 13534 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13535}
13536
13537/* Implement macros in mips16 mode. */
13538
13539static void
17a2f251 13540mips16_macro (struct mips_cl_insn *ip)
252b5132 13541{
c0ebe874 13542 const struct mips_operand_array *operands;
252b5132 13543 int mask;
c0ebe874 13544 int tmp;
252b5132
RH
13545 expressionS expr1;
13546 int dbl;
13547 const char *s, *s2, *s3;
c0ebe874
RS
13548 unsigned int op[MAX_OPERANDS];
13549 unsigned int i;
252b5132
RH
13550
13551 mask = ip->insn_mo->mask;
13552
c0ebe874
RS
13553 operands = insn_operands (ip);
13554 for (i = 0; i < MAX_OPERANDS; i++)
13555 if (operands->operand[i])
13556 op[i] = insn_extract_operand (ip, operands->operand[i]);
13557 else
13558 op[i] = -1;
252b5132 13559
252b5132
RH
13560 expr1.X_op = O_constant;
13561 expr1.X_op_symbol = NULL;
13562 expr1.X_add_symbol = NULL;
13563 expr1.X_add_number = 1;
13564
13565 dbl = 0;
13566
13567 switch (mask)
13568 {
13569 default:
b37df7c4 13570 abort ();
252b5132
RH
13571
13572 case M_DDIV_3:
13573 dbl = 1;
1a0670f3 13574 /* Fall through. */
252b5132
RH
13575 case M_DIV_3:
13576 s = "mflo";
13577 goto do_div3;
13578 case M_DREM_3:
13579 dbl = 1;
1a0670f3 13580 /* Fall through. */
252b5132
RH
13581 case M_REM_3:
13582 s = "mfhi";
13583 do_div3:
7d10b47d 13584 start_noreorder ();
d8722d76 13585 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13586 expr1.X_add_number = 2;
c0ebe874 13587 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13588 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13589
252b5132
RH
13590 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13591 since that causes an overflow. We should do that as well,
13592 but I don't see how to do the comparisons without a temporary
13593 register. */
7d10b47d 13594 end_noreorder ();
c0ebe874 13595 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13596 break;
13597
13598 case M_DIVU_3:
13599 s = "divu";
13600 s2 = "mflo";
13601 goto do_divu3;
13602 case M_REMU_3:
13603 s = "divu";
13604 s2 = "mfhi";
13605 goto do_divu3;
13606 case M_DDIVU_3:
13607 s = "ddivu";
13608 s2 = "mflo";
13609 goto do_divu3;
13610 case M_DREMU_3:
13611 s = "ddivu";
13612 s2 = "mfhi";
13613 do_divu3:
7d10b47d 13614 start_noreorder ();
d8722d76 13615 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13616 expr1.X_add_number = 2;
c0ebe874 13617 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13618 macro_build (NULL, "break", "6", 7);
7d10b47d 13619 end_noreorder ();
c0ebe874 13620 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13621 break;
13622
13623 case M_DMUL:
13624 dbl = 1;
1a0670f3 13625 /* Fall through. */
252b5132 13626 case M_MUL:
c0ebe874
RS
13627 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13628 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13629 break;
252b5132
RH
13630
13631 case M_DSUBU_I:
13632 dbl = 1;
13633 goto do_subu;
13634 case M_SUBU_I:
13635 do_subu:
252b5132 13636 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13637 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13638 break;
13639
13640 case M_SUBU_I_2:
252b5132 13641 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13642 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13643 break;
13644
13645 case M_DSUBU_I_2:
252b5132 13646 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13647 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13648 break;
13649
13650 case M_BEQ:
13651 s = "cmp";
13652 s2 = "bteqz";
13653 goto do_branch;
13654 case M_BNE:
13655 s = "cmp";
13656 s2 = "btnez";
13657 goto do_branch;
13658 case M_BLT:
13659 s = "slt";
13660 s2 = "btnez";
13661 goto do_branch;
13662 case M_BLTU:
13663 s = "sltu";
13664 s2 = "btnez";
13665 goto do_branch;
13666 case M_BLE:
13667 s = "slt";
13668 s2 = "bteqz";
13669 goto do_reverse_branch;
13670 case M_BLEU:
13671 s = "sltu";
13672 s2 = "bteqz";
13673 goto do_reverse_branch;
13674 case M_BGE:
13675 s = "slt";
13676 s2 = "bteqz";
13677 goto do_branch;
13678 case M_BGEU:
13679 s = "sltu";
13680 s2 = "bteqz";
13681 goto do_branch;
13682 case M_BGT:
13683 s = "slt";
13684 s2 = "btnez";
13685 goto do_reverse_branch;
13686 case M_BGTU:
13687 s = "sltu";
13688 s2 = "btnez";
13689
13690 do_reverse_branch:
c0ebe874
RS
13691 tmp = op[1];
13692 op[1] = op[0];
13693 op[0] = tmp;
252b5132
RH
13694
13695 do_branch:
c0ebe874 13696 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13697 macro_build (&offset_expr, s2, "p");
252b5132
RH
13698 break;
13699
13700 case M_BEQ_I:
13701 s = "cmpi";
13702 s2 = "bteqz";
13703 s3 = "x,U";
13704 goto do_branch_i;
13705 case M_BNE_I:
13706 s = "cmpi";
13707 s2 = "btnez";
13708 s3 = "x,U";
13709 goto do_branch_i;
13710 case M_BLT_I:
13711 s = "slti";
13712 s2 = "btnez";
13713 s3 = "x,8";
13714 goto do_branch_i;
13715 case M_BLTU_I:
13716 s = "sltiu";
13717 s2 = "btnez";
13718 s3 = "x,8";
13719 goto do_branch_i;
13720 case M_BLE_I:
13721 s = "slti";
13722 s2 = "btnez";
13723 s3 = "x,8";
13724 goto do_addone_branch_i;
13725 case M_BLEU_I:
13726 s = "sltiu";
13727 s2 = "btnez";
13728 s3 = "x,8";
13729 goto do_addone_branch_i;
13730 case M_BGE_I:
13731 s = "slti";
13732 s2 = "bteqz";
13733 s3 = "x,8";
13734 goto do_branch_i;
13735 case M_BGEU_I:
13736 s = "sltiu";
13737 s2 = "bteqz";
13738 s3 = "x,8";
13739 goto do_branch_i;
13740 case M_BGT_I:
13741 s = "slti";
13742 s2 = "bteqz";
13743 s3 = "x,8";
13744 goto do_addone_branch_i;
13745 case M_BGTU_I:
13746 s = "sltiu";
13747 s2 = "bteqz";
13748 s3 = "x,8";
13749
13750 do_addone_branch_i:
252b5132
RH
13751 ++imm_expr.X_add_number;
13752
13753 do_branch_i:
c0ebe874 13754 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13755 macro_build (&offset_expr, s2, "p");
252b5132
RH
13756 break;
13757
13758 case M_ABS:
13759 expr1.X_add_number = 0;
c0ebe874
RS
13760 macro_build (&expr1, "slti", "x,8", op[1]);
13761 if (op[0] != op[1])
13762 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13763 expr1.X_add_number = 2;
67c0d1eb 13764 macro_build (&expr1, "bteqz", "p");
c0ebe874 13765 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13766 break;
252b5132
RH
13767 }
13768}
13769
14daeee3
RS
13770/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13771 opcode bits in *OPCODE_EXTRA. */
13772
13773static struct mips_opcode *
13774mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13775 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13776{
13777 char *name, *dot, *p;
13778 unsigned int mask, suffix;
da8bca91 13779 ssize_t opend;
14daeee3
RS
13780 struct mips_opcode *insn;
13781
13782 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13783 name = xstrndup (start, length);
14daeee3
RS
13784
13785 /* Look up the instruction as-is. */
13786 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13787 if (insn)
e1fa0163 13788 goto end;
14daeee3
RS
13789
13790 dot = strchr (name, '.');
13791 if (dot && dot[1])
13792 {
13793 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13794 p = mips_parse_vu0_channels (dot + 1, &mask);
13795 if (*p == 0 && mask != 0)
13796 {
13797 *dot = 0;
13798 insn = (struct mips_opcode *) hash_find (hash, name);
13799 *dot = '.';
13800 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13801 {
13802 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13803 goto end;
14daeee3
RS
13804 }
13805 }
13806 }
13807
13808 if (mips_opts.micromips)
13809 {
13810 /* See if there's an instruction size override suffix,
13811 either `16' or `32', at the end of the mnemonic proper,
13812 that defines the operation, i.e. before the first `.'
13813 character if any. Strip it and retry. */
13814 opend = dot != NULL ? dot - name : length;
13815 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13816 suffix = 2;
13817 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13818 suffix = 4;
13819 else
13820 suffix = 0;
13821 if (suffix)
13822 {
13823 memcpy (name + opend - 2, name + opend, length - opend + 1);
13824 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13825 if (insn)
14daeee3
RS
13826 {
13827 forced_insn_length = suffix;
e1fa0163 13828 goto end;
14daeee3
RS
13829 }
13830 }
13831 }
13832
e1fa0163
NC
13833 insn = NULL;
13834 end:
13835 free (name);
13836 return insn;
14daeee3
RS
13837}
13838
77bd4346 13839/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13840 is a macro, set imm_expr and offset_expr to the values associated
13841 with "I" and "A" operands respectively. Otherwise store the value
13842 of the relocatable field (if any) in offset_expr. In both cases
13843 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13844
13845static void
60f20e8b 13846mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13847{
60f20e8b 13848 const struct mips_opcode *first, *past;
df58fc94 13849 struct hash_control *hash;
a92713e6 13850 char format;
14daeee3 13851 size_t end;
a92713e6 13852 struct mips_operand_token *tokens;
14daeee3 13853 unsigned int opcode_extra;
252b5132 13854
df58fc94
RS
13855 if (mips_opts.micromips)
13856 {
13857 hash = micromips_op_hash;
13858 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13859 }
13860 else
13861 {
13862 hash = op_hash;
13863 past = &mips_opcodes[NUMOPCODES];
13864 }
13865 forced_insn_length = 0;
14daeee3 13866 opcode_extra = 0;
252b5132 13867
df58fc94 13868 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
13869 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13870 continue;
bdaaa2e1 13871
60f20e8b
RS
13872 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13873 if (first == NULL)
252b5132 13874 {
1661c76c 13875 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 13876 return;
252b5132
RH
13877 }
13878
60f20e8b 13879 if (strcmp (first->name, "li.s") == 0)
a92713e6 13880 format = 'f';
60f20e8b 13881 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
13882 format = 'd';
13883 else
13884 format = 0;
13885 tokens = mips_parse_arguments (str + end, format);
13886 if (!tokens)
13887 return;
13888
60f20e8b
RS
13889 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13890 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 13891 set_insn_error (0, _("invalid operands"));
df58fc94 13892
e3de51ce 13893 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13894}
13895
77bd4346
RS
13896/* As for mips_ip, but used when assembling MIPS16 code.
13897 Also set forced_insn_length to the resulting instruction size in
13898 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13899
13900static void
60f20e8b 13901mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 13902{
1a00e612 13903 char *end, *s, c;
60f20e8b 13904 struct mips_opcode *first;
a92713e6 13905 struct mips_operand_token *tokens;
3fb49709 13906 unsigned int l;
252b5132 13907
3882b010 13908 for (s = str; ISLOWER (*s); ++s)
252b5132 13909 ;
1a00e612
RS
13910 end = s;
13911 c = *end;
3fb49709
MR
13912
13913 l = 0;
1a00e612 13914 switch (c)
252b5132
RH
13915 {
13916 case '\0':
13917 break;
13918
13919 case ' ':
1a00e612 13920 s++;
252b5132
RH
13921 break;
13922
13923 case '.':
3fb49709
MR
13924 s++;
13925 if (*s == 't')
252b5132 13926 {
3fb49709
MR
13927 l = 2;
13928 s++;
252b5132 13929 }
3fb49709 13930 else if (*s == 'e')
252b5132 13931 {
3fb49709
MR
13932 l = 4;
13933 s++;
252b5132 13934 }
3fb49709
MR
13935 if (*s == '\0')
13936 break;
13937 else if (*s++ == ' ')
13938 break;
252b5132
RH
13939 /* Fall through. */
13940 default:
1661c76c 13941 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13942 return;
13943 }
3fb49709 13944 forced_insn_length = l;
252b5132 13945
1a00e612 13946 *end = 0;
60f20e8b 13947 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13948 *end = c;
13949
60f20e8b 13950 if (!first)
252b5132 13951 {
1661c76c 13952 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13953 return;
13954 }
13955
a92713e6
RS
13956 tokens = mips_parse_arguments (s, 0);
13957 if (!tokens)
13958 return;
13959
60f20e8b 13960 if (!match_mips16_insns (insn, first, tokens))
1661c76c 13961 set_insn_error (0, _("invalid operands"));
252b5132 13962
e3de51ce 13963 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13964}
13965
b886a2ab
RS
13966/* Marshal immediate value VAL for an extended MIPS16 instruction.
13967 NBITS is the number of significant bits in VAL. */
13968
13969static unsigned long
13970mips16_immed_extend (offsetT val, unsigned int nbits)
13971{
13972 int extval;
13973 if (nbits == 16)
13974 {
13975 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13976 val &= 0x1f;
13977 }
13978 else if (nbits == 15)
13979 {
13980 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13981 val &= 0xf;
13982 }
13983 else
13984 {
13985 extval = ((val & 0x1f) << 6) | (val & 0x20);
13986 val = 0;
13987 }
13988 return (extval << 16) | val;
13989}
13990
3ccad066
RS
13991/* Like decode_mips16_operand, but require the operand to be defined and
13992 require it to be an integer. */
13993
13994static const struct mips_int_operand *
13995mips16_immed_operand (int type, bfd_boolean extended_p)
13996{
13997 const struct mips_operand *operand;
13998
13999 operand = decode_mips16_operand (type, extended_p);
14000 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14001 abort ();
14002 return (const struct mips_int_operand *) operand;
14003}
14004
14005/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14006
14007static bfd_boolean
14008mips16_immed_in_range_p (const struct mips_int_operand *operand,
14009 bfd_reloc_code_real_type reloc, offsetT sval)
14010{
14011 int min_val, max_val;
14012
14013 min_val = mips_int_operand_min (operand);
14014 max_val = mips_int_operand_max (operand);
14015 if (reloc != BFD_RELOC_UNUSED)
14016 {
14017 if (min_val < 0)
14018 sval = SEXT_16BIT (sval);
14019 else
14020 sval &= 0xffff;
14021 }
14022
14023 return (sval >= min_val
14024 && sval <= max_val
14025 && (sval & ((1 << operand->shift) - 1)) == 0);
14026}
14027
5c04167a
RS
14028/* Install immediate value VAL into MIPS16 instruction *INSN,
14029 extending it if necessary. The instruction in *INSN may
14030 already be extended.
14031
43c0598f
RS
14032 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14033 if none. In the former case, VAL is a 16-bit number with no
14034 defined signedness.
14035
14036 TYPE is the type of the immediate field. USER_INSN_LENGTH
14037 is the length that the user requested, or 0 if none. */
252b5132
RH
14038
14039static void
3b4dbbbf 14040mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14041 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14042 unsigned int user_insn_length, unsigned long *insn)
252b5132 14043{
3ccad066
RS
14044 const struct mips_int_operand *operand;
14045 unsigned int uval, length;
252b5132 14046
3ccad066
RS
14047 operand = mips16_immed_operand (type, FALSE);
14048 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14049 {
14050 /* We need an extended instruction. */
14051 if (user_insn_length == 2)
14052 as_bad_where (file, line, _("invalid unextended operand value"));
14053 else
14054 *insn |= MIPS16_EXTEND;
14055 }
14056 else if (user_insn_length == 4)
14057 {
14058 /* The operand doesn't force an unextended instruction to be extended.
14059 Warn if the user wanted an extended instruction anyway. */
14060 *insn |= MIPS16_EXTEND;
14061 as_warn_where (file, line,
14062 _("extended operand requested but not required"));
14063 }
252b5132 14064
3ccad066
RS
14065 length = mips16_opcode_length (*insn);
14066 if (length == 4)
252b5132 14067 {
3ccad066
RS
14068 operand = mips16_immed_operand (type, TRUE);
14069 if (!mips16_immed_in_range_p (operand, reloc, val))
14070 as_bad_where (file, line,
14071 _("operand value out of range for instruction"));
252b5132 14072 }
3ccad066 14073 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14074 if (length == 2 || operand->root.lsb != 0)
3ccad066 14075 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14076 else
3ccad066 14077 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14078}
14079\f
d6f16593 14080struct percent_op_match
ad8d3bb3 14081{
5e0116d5
RS
14082 const char *str;
14083 bfd_reloc_code_real_type reloc;
d6f16593
MR
14084};
14085
14086static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14087{
5e0116d5 14088 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14089 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14090 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14091 {"%call16", BFD_RELOC_MIPS_CALL16},
14092 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14093 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14094 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14095 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14096 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14097 {"%got", BFD_RELOC_MIPS_GOT16},
14098 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14099 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14100 {"%half", BFD_RELOC_16},
14101 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14102 {"%higher", BFD_RELOC_MIPS_HIGHER},
14103 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14104 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14105 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14106 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14107 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14108 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14109 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14110 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14111 {"%hi", BFD_RELOC_HI16_S},
14112 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14113 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14114};
14115
d6f16593
MR
14116static const struct percent_op_match mips16_percent_op[] =
14117{
14118 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14119 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14120 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14121 {"%got", BFD_RELOC_MIPS16_GOT16},
14122 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14123 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14124 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14125 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14126 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14127 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14128 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14129 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14130 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14131};
14132
252b5132 14133
5e0116d5
RS
14134/* Return true if *STR points to a relocation operator. When returning true,
14135 move *STR over the operator and store its relocation code in *RELOC.
14136 Leave both *STR and *RELOC alone when returning false. */
14137
14138static bfd_boolean
17a2f251 14139parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14140{
d6f16593
MR
14141 const struct percent_op_match *percent_op;
14142 size_t limit, i;
14143
14144 if (mips_opts.mips16)
14145 {
14146 percent_op = mips16_percent_op;
14147 limit = ARRAY_SIZE (mips16_percent_op);
14148 }
14149 else
14150 {
14151 percent_op = mips_percent_op;
14152 limit = ARRAY_SIZE (mips_percent_op);
14153 }
76b3015f 14154
d6f16593 14155 for (i = 0; i < limit; i++)
5e0116d5 14156 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14157 {
3f98094e
DJ
14158 int len = strlen (percent_op[i].str);
14159
14160 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14161 continue;
14162
5e0116d5
RS
14163 *str += strlen (percent_op[i].str);
14164 *reloc = percent_op[i].reloc;
394f9b3a 14165
5e0116d5
RS
14166 /* Check whether the output BFD supports this relocation.
14167 If not, issue an error and fall back on something safe. */
14168 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14169 {
20203fb9 14170 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14171 percent_op[i].str);
01a3f561 14172 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14173 }
5e0116d5 14174 return TRUE;
394f9b3a 14175 }
5e0116d5 14176 return FALSE;
394f9b3a 14177}
ad8d3bb3 14178
ad8d3bb3 14179
5e0116d5
RS
14180/* Parse string STR as a 16-bit relocatable operand. Store the
14181 expression in *EP and the relocations in the array starting
14182 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14183
01a3f561 14184 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14185
5e0116d5 14186static size_t
17a2f251
TS
14187my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14188 char *str)
ad8d3bb3 14189{
5e0116d5
RS
14190 bfd_reloc_code_real_type reversed_reloc[3];
14191 size_t reloc_index, i;
09b8f35a
RS
14192 int crux_depth, str_depth;
14193 char *crux;
5e0116d5
RS
14194
14195 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14196 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14197 of the main expression and with CRUX_DEPTH containing the number
14198 of open brackets at that point. */
14199 reloc_index = -1;
14200 str_depth = 0;
14201 do
fb1b3232 14202 {
09b8f35a
RS
14203 reloc_index++;
14204 crux = str;
14205 crux_depth = str_depth;
14206
14207 /* Skip over whitespace and brackets, keeping count of the number
14208 of brackets. */
14209 while (*str == ' ' || *str == '\t' || *str == '(')
14210 if (*str++ == '(')
14211 str_depth++;
5e0116d5 14212 }
09b8f35a
RS
14213 while (*str == '%'
14214 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14215 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14216
09b8f35a 14217 my_getExpression (ep, crux);
5e0116d5 14218 str = expr_end;
394f9b3a 14219
5e0116d5 14220 /* Match every open bracket. */
09b8f35a 14221 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14222 if (*str++ == ')')
09b8f35a 14223 crux_depth--;
394f9b3a 14224
09b8f35a 14225 if (crux_depth > 0)
20203fb9 14226 as_bad (_("unclosed '('"));
394f9b3a 14227
5e0116d5 14228 expr_end = str;
252b5132 14229
01a3f561 14230 if (reloc_index != 0)
64bdfcaf
RS
14231 {
14232 prev_reloc_op_frag = frag_now;
14233 for (i = 0; i < reloc_index; i++)
14234 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14235 }
fb1b3232 14236
5e0116d5 14237 return reloc_index;
252b5132
RH
14238}
14239
14240static void
17a2f251 14241my_getExpression (expressionS *ep, char *str)
252b5132
RH
14242{
14243 char *save_in;
14244
14245 save_in = input_line_pointer;
14246 input_line_pointer = str;
14247 expression (ep);
14248 expr_end = input_line_pointer;
14249 input_line_pointer = save_in;
252b5132
RH
14250}
14251
6d4af3c2 14252const char *
17a2f251 14253md_atof (int type, char *litP, int *sizeP)
252b5132 14254{
499ac353 14255 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14256}
14257
14258void
17a2f251 14259md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14260{
14261 if (target_big_endian)
14262 number_to_chars_bigendian (buf, val, n);
14263 else
14264 number_to_chars_littleendian (buf, val, n);
14265}
14266\f
e013f690
TS
14267static int support_64bit_objects(void)
14268{
14269 const char **list, **l;
aa3d8fdf 14270 int yes;
e013f690
TS
14271
14272 list = bfd_target_list ();
14273 for (l = list; *l != NULL; l++)
aeffff67
RS
14274 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14275 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14276 break;
aa3d8fdf 14277 yes = (*l != NULL);
e013f690 14278 free (list);
aa3d8fdf 14279 return yes;
e013f690
TS
14280}
14281
316f5878
RS
14282/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14283 NEW_VALUE. Warn if another value was already specified. Note:
14284 we have to defer parsing the -march and -mtune arguments in order
14285 to handle 'from-abi' correctly, since the ABI might be specified
14286 in a later argument. */
14287
14288static void
17a2f251 14289mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14290{
14291 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14292 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14293 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14294 new_value);
14295
14296 *string_ptr = new_value;
14297}
14298
252b5132 14299int
17b9d67d 14300md_parse_option (int c, const char *arg)
252b5132 14301{
c6278170
RS
14302 unsigned int i;
14303
14304 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14305 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14306 {
919731af 14307 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14308 c == mips_ases[i].option_on);
14309 return 1;
14310 }
14311
252b5132
RH
14312 switch (c)
14313 {
119d663a
NC
14314 case OPTION_CONSTRUCT_FLOATS:
14315 mips_disable_float_construction = 0;
14316 break;
bdaaa2e1 14317
119d663a
NC
14318 case OPTION_NO_CONSTRUCT_FLOATS:
14319 mips_disable_float_construction = 1;
14320 break;
bdaaa2e1 14321
252b5132
RH
14322 case OPTION_TRAP:
14323 mips_trap = 1;
14324 break;
14325
14326 case OPTION_BREAK:
14327 mips_trap = 0;
14328 break;
14329
14330 case OPTION_EB:
14331 target_big_endian = 1;
14332 break;
14333
14334 case OPTION_EL:
14335 target_big_endian = 0;
14336 break;
14337
14338 case 'O':
4ffff32f
TS
14339 if (arg == NULL)
14340 mips_optimize = 1;
14341 else if (arg[0] == '0')
14342 mips_optimize = 0;
14343 else if (arg[0] == '1')
252b5132
RH
14344 mips_optimize = 1;
14345 else
14346 mips_optimize = 2;
14347 break;
14348
14349 case 'g':
14350 if (arg == NULL)
14351 mips_debug = 2;
14352 else
14353 mips_debug = atoi (arg);
252b5132
RH
14354 break;
14355
14356 case OPTION_MIPS1:
0b35dfee 14357 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14358 break;
14359
14360 case OPTION_MIPS2:
0b35dfee 14361 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14362 break;
14363
14364 case OPTION_MIPS3:
0b35dfee 14365 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14366 break;
14367
14368 case OPTION_MIPS4:
0b35dfee 14369 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14370 break;
14371
84ea6cf2 14372 case OPTION_MIPS5:
0b35dfee 14373 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14374 break;
14375
e7af610e 14376 case OPTION_MIPS32:
0b35dfee 14377 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14378 break;
14379
af7ee8bf 14380 case OPTION_MIPS32R2:
0b35dfee 14381 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14382 break;
14383
ae52f483 14384 case OPTION_MIPS32R3:
0ae19f05 14385 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14386 break;
14387
14388 case OPTION_MIPS32R5:
0ae19f05 14389 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14390 break;
14391
7361da2c
AB
14392 case OPTION_MIPS32R6:
14393 file_mips_opts.isa = ISA_MIPS32R6;
14394 break;
14395
5f74bc13 14396 case OPTION_MIPS64R2:
0b35dfee 14397 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14398 break;
14399
ae52f483 14400 case OPTION_MIPS64R3:
0ae19f05 14401 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14402 break;
14403
14404 case OPTION_MIPS64R5:
0ae19f05 14405 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14406 break;
14407
7361da2c
AB
14408 case OPTION_MIPS64R6:
14409 file_mips_opts.isa = ISA_MIPS64R6;
14410 break;
14411
84ea6cf2 14412 case OPTION_MIPS64:
0b35dfee 14413 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14414 break;
14415
ec68c924 14416 case OPTION_MTUNE:
316f5878
RS
14417 mips_set_option_string (&mips_tune_string, arg);
14418 break;
ec68c924 14419
316f5878
RS
14420 case OPTION_MARCH:
14421 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14422 break;
14423
14424 case OPTION_M4650:
316f5878
RS
14425 mips_set_option_string (&mips_arch_string, "4650");
14426 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14427 break;
14428
14429 case OPTION_NO_M4650:
14430 break;
14431
14432 case OPTION_M4010:
316f5878
RS
14433 mips_set_option_string (&mips_arch_string, "4010");
14434 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14435 break;
14436
14437 case OPTION_NO_M4010:
14438 break;
14439
14440 case OPTION_M4100:
316f5878
RS
14441 mips_set_option_string (&mips_arch_string, "4100");
14442 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14443 break;
14444
14445 case OPTION_NO_M4100:
14446 break;
14447
252b5132 14448 case OPTION_M3900:
316f5878
RS
14449 mips_set_option_string (&mips_arch_string, "3900");
14450 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14451 break;
bdaaa2e1 14452
252b5132
RH
14453 case OPTION_NO_M3900:
14454 break;
14455
df58fc94 14456 case OPTION_MICROMIPS:
919731af 14457 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14458 {
14459 as_bad (_("-mmicromips cannot be used with -mips16"));
14460 return 0;
14461 }
919731af 14462 file_mips_opts.micromips = 1;
df58fc94
RS
14463 mips_no_prev_insn ();
14464 break;
14465
14466 case OPTION_NO_MICROMIPS:
919731af 14467 file_mips_opts.micromips = 0;
df58fc94
RS
14468 mips_no_prev_insn ();
14469 break;
14470
252b5132 14471 case OPTION_MIPS16:
919731af 14472 if (file_mips_opts.micromips == 1)
df58fc94
RS
14473 {
14474 as_bad (_("-mips16 cannot be used with -micromips"));
14475 return 0;
14476 }
919731af 14477 file_mips_opts.mips16 = 1;
7d10b47d 14478 mips_no_prev_insn ();
252b5132
RH
14479 break;
14480
14481 case OPTION_NO_MIPS16:
919731af 14482 file_mips_opts.mips16 = 0;
7d10b47d 14483 mips_no_prev_insn ();
252b5132
RH
14484 break;
14485
6a32d874
CM
14486 case OPTION_FIX_24K:
14487 mips_fix_24k = 1;
14488 break;
14489
14490 case OPTION_NO_FIX_24K:
14491 mips_fix_24k = 0;
14492 break;
14493
a8d14a88
CM
14494 case OPTION_FIX_RM7000:
14495 mips_fix_rm7000 = 1;
14496 break;
14497
14498 case OPTION_NO_FIX_RM7000:
14499 mips_fix_rm7000 = 0;
14500 break;
14501
c67a084a
NC
14502 case OPTION_FIX_LOONGSON2F_JUMP:
14503 mips_fix_loongson2f_jump = TRUE;
14504 break;
14505
14506 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14507 mips_fix_loongson2f_jump = FALSE;
14508 break;
14509
14510 case OPTION_FIX_LOONGSON2F_NOP:
14511 mips_fix_loongson2f_nop = TRUE;
14512 break;
14513
14514 case OPTION_NO_FIX_LOONGSON2F_NOP:
14515 mips_fix_loongson2f_nop = FALSE;
14516 break;
14517
d766e8ec
RS
14518 case OPTION_FIX_VR4120:
14519 mips_fix_vr4120 = 1;
60b63b72
RS
14520 break;
14521
d766e8ec
RS
14522 case OPTION_NO_FIX_VR4120:
14523 mips_fix_vr4120 = 0;
60b63b72
RS
14524 break;
14525
7d8e00cf
RS
14526 case OPTION_FIX_VR4130:
14527 mips_fix_vr4130 = 1;
14528 break;
14529
14530 case OPTION_NO_FIX_VR4130:
14531 mips_fix_vr4130 = 0;
14532 break;
14533
d954098f
DD
14534 case OPTION_FIX_CN63XXP1:
14535 mips_fix_cn63xxp1 = TRUE;
14536 break;
14537
14538 case OPTION_NO_FIX_CN63XXP1:
14539 mips_fix_cn63xxp1 = FALSE;
14540 break;
14541
4a6a3df4
AO
14542 case OPTION_RELAX_BRANCH:
14543 mips_relax_branch = 1;
14544 break;
14545
14546 case OPTION_NO_RELAX_BRANCH:
14547 mips_relax_branch = 0;
14548 break;
14549
8b10b0b3
MR
14550 case OPTION_IGNORE_BRANCH_ISA:
14551 mips_ignore_branch_isa = TRUE;
14552 break;
14553
14554 case OPTION_NO_IGNORE_BRANCH_ISA:
14555 mips_ignore_branch_isa = FALSE;
14556 break;
14557
833794fc 14558 case OPTION_INSN32:
919731af 14559 file_mips_opts.insn32 = TRUE;
833794fc
MR
14560 break;
14561
14562 case OPTION_NO_INSN32:
919731af 14563 file_mips_opts.insn32 = FALSE;
833794fc
MR
14564 break;
14565
aa6975fb
ILT
14566 case OPTION_MSHARED:
14567 mips_in_shared = TRUE;
14568 break;
14569
14570 case OPTION_MNO_SHARED:
14571 mips_in_shared = FALSE;
14572 break;
14573
aed1a261 14574 case OPTION_MSYM32:
919731af 14575 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14576 break;
14577
14578 case OPTION_MNO_SYM32:
919731af 14579 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14580 break;
14581
252b5132
RH
14582 /* When generating ELF code, we permit -KPIC and -call_shared to
14583 select SVR4_PIC, and -non_shared to select no PIC. This is
14584 intended to be compatible with Irix 5. */
14585 case OPTION_CALL_SHARED:
252b5132 14586 mips_pic = SVR4_PIC;
143d77c5 14587 mips_abicalls = TRUE;
252b5132
RH
14588 break;
14589
861fb55a 14590 case OPTION_CALL_NONPIC:
861fb55a
DJ
14591 mips_pic = NO_PIC;
14592 mips_abicalls = TRUE;
14593 break;
14594
252b5132 14595 case OPTION_NON_SHARED:
252b5132 14596 mips_pic = NO_PIC;
143d77c5 14597 mips_abicalls = FALSE;
252b5132
RH
14598 break;
14599
44075ae2
TS
14600 /* The -xgot option tells the assembler to use 32 bit offsets
14601 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14602 compatibility. */
14603 case OPTION_XGOT:
14604 mips_big_got = 1;
14605 break;
14606
14607 case 'G':
6caf9ef4
TS
14608 g_switch_value = atoi (arg);
14609 g_switch_seen = 1;
252b5132
RH
14610 break;
14611
34ba82a8
TS
14612 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14613 and -mabi=64. */
252b5132 14614 case OPTION_32:
f3ded42a 14615 mips_abi = O32_ABI;
252b5132
RH
14616 break;
14617
e013f690 14618 case OPTION_N32:
316f5878 14619 mips_abi = N32_ABI;
e013f690 14620 break;
252b5132 14621
e013f690 14622 case OPTION_64:
316f5878 14623 mips_abi = N64_ABI;
f43abd2b 14624 if (!support_64bit_objects())
1661c76c 14625 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14626 break;
14627
c97ef257 14628 case OPTION_GP32:
bad1aba3 14629 file_mips_opts.gp = 32;
c97ef257
AH
14630 break;
14631
14632 case OPTION_GP64:
bad1aba3 14633 file_mips_opts.gp = 64;
c97ef257 14634 break;
252b5132 14635
ca4e0257 14636 case OPTION_FP32:
0b35dfee 14637 file_mips_opts.fp = 32;
316f5878
RS
14638 break;
14639
351cdf24
MF
14640 case OPTION_FPXX:
14641 file_mips_opts.fp = 0;
14642 break;
14643
316f5878 14644 case OPTION_FP64:
0b35dfee 14645 file_mips_opts.fp = 64;
ca4e0257
RS
14646 break;
14647
351cdf24
MF
14648 case OPTION_ODD_SPREG:
14649 file_mips_opts.oddspreg = 1;
14650 break;
14651
14652 case OPTION_NO_ODD_SPREG:
14653 file_mips_opts.oddspreg = 0;
14654 break;
14655
037b32b9 14656 case OPTION_SINGLE_FLOAT:
0b35dfee 14657 file_mips_opts.single_float = 1;
037b32b9
AN
14658 break;
14659
14660 case OPTION_DOUBLE_FLOAT:
0b35dfee 14661 file_mips_opts.single_float = 0;
037b32b9
AN
14662 break;
14663
14664 case OPTION_SOFT_FLOAT:
0b35dfee 14665 file_mips_opts.soft_float = 1;
037b32b9
AN
14666 break;
14667
14668 case OPTION_HARD_FLOAT:
0b35dfee 14669 file_mips_opts.soft_float = 0;
037b32b9
AN
14670 break;
14671
252b5132 14672 case OPTION_MABI:
e013f690 14673 if (strcmp (arg, "32") == 0)
316f5878 14674 mips_abi = O32_ABI;
e013f690 14675 else if (strcmp (arg, "o64") == 0)
316f5878 14676 mips_abi = O64_ABI;
e013f690 14677 else if (strcmp (arg, "n32") == 0)
316f5878 14678 mips_abi = N32_ABI;
e013f690
TS
14679 else if (strcmp (arg, "64") == 0)
14680 {
316f5878 14681 mips_abi = N64_ABI;
e013f690 14682 if (! support_64bit_objects())
1661c76c 14683 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14684 "format"));
14685 }
14686 else if (strcmp (arg, "eabi") == 0)
316f5878 14687 mips_abi = EABI_ABI;
e013f690 14688 else
da0e507f
TS
14689 {
14690 as_fatal (_("invalid abi -mabi=%s"), arg);
14691 return 0;
14692 }
252b5132
RH
14693 break;
14694
6b76fefe 14695 case OPTION_M7000_HILO_FIX:
b34976b6 14696 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14697 break;
14698
9ee72ff1 14699 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14700 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14701 break;
14702
ecb4347a 14703 case OPTION_MDEBUG:
b34976b6 14704 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14705 break;
14706
14707 case OPTION_NO_MDEBUG:
b34976b6 14708 mips_flag_mdebug = FALSE;
ecb4347a 14709 break;
dcd410fe
RO
14710
14711 case OPTION_PDR:
14712 mips_flag_pdr = TRUE;
14713 break;
14714
14715 case OPTION_NO_PDR:
14716 mips_flag_pdr = FALSE;
14717 break;
0a44bf69
RS
14718
14719 case OPTION_MVXWORKS_PIC:
14720 mips_pic = VXWORKS_PIC;
14721 break;
ecb4347a 14722
ba92f887
MR
14723 case OPTION_NAN:
14724 if (strcmp (arg, "2008") == 0)
7361da2c 14725 mips_nan2008 = 1;
ba92f887 14726 else if (strcmp (arg, "legacy") == 0)
7361da2c 14727 mips_nan2008 = 0;
ba92f887
MR
14728 else
14729 {
1661c76c 14730 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14731 return 0;
14732 }
14733 break;
14734
252b5132
RH
14735 default:
14736 return 0;
14737 }
14738
c67a084a
NC
14739 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14740
252b5132
RH
14741 return 1;
14742}
316f5878 14743\f
919731af 14744/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14745
316f5878 14746static void
17a2f251 14747mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14748{
14749 if (info != 0)
fef14a42 14750 mips_tune = info->cpu;
316f5878 14751}
80cc45a5 14752
34ba82a8 14753
252b5132 14754void
17a2f251 14755mips_after_parse_args (void)
e9670677 14756{
fef14a42
TS
14757 const struct mips_cpu_info *arch_info = 0;
14758 const struct mips_cpu_info *tune_info = 0;
14759
e9670677 14760 /* GP relative stuff not working for PE */
6caf9ef4 14761 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14762 {
6caf9ef4 14763 if (g_switch_seen && g_switch_value != 0)
1661c76c 14764 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14765 g_switch_value = 0;
14766 }
14767
cac012d6
AO
14768 if (mips_abi == NO_ABI)
14769 mips_abi = MIPS_DEFAULT_ABI;
14770
919731af 14771 /* The following code determines the architecture.
22923709
RS
14772 Similar code was added to GCC 3.3 (see override_options() in
14773 config/mips/mips.c). The GAS and GCC code should be kept in sync
14774 as much as possible. */
e9670677 14775
316f5878 14776 if (mips_arch_string != 0)
fef14a42 14777 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14778
0b35dfee 14779 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14780 {
0b35dfee 14781 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14782 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14783 the -march selection (if any). */
fef14a42 14784 if (arch_info != 0)
e9670677 14785 {
316f5878
RS
14786 /* -march takes precedence over -mipsN, since it is more descriptive.
14787 There's no harm in specifying both as long as the ISA levels
14788 are the same. */
0b35dfee 14789 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14790 as_bad (_("-%s conflicts with the other architecture options,"
14791 " which imply -%s"),
0b35dfee 14792 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14793 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14794 }
316f5878 14795 else
0b35dfee 14796 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14797 }
14798
fef14a42 14799 if (arch_info == 0)
95bfe26e
MF
14800 {
14801 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14802 gas_assert (arch_info);
14803 }
e9670677 14804
fef14a42 14805 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14806 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14807 arch_info->name);
14808
919731af 14809 file_mips_opts.arch = arch_info->cpu;
14810 file_mips_opts.isa = arch_info->isa;
14811
14812 /* Set up initial mips_opts state. */
14813 mips_opts = file_mips_opts;
14814
14815 /* The register size inference code is now placed in
14816 file_mips_check_options. */
fef14a42 14817
0b35dfee 14818 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14819 processor. */
fef14a42
TS
14820 if (mips_tune_string != 0)
14821 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14822
fef14a42
TS
14823 if (tune_info == 0)
14824 mips_set_tune (arch_info);
14825 else
14826 mips_set_tune (tune_info);
e9670677 14827
ecb4347a 14828 if (mips_flag_mdebug < 0)
e8044f35 14829 mips_flag_mdebug = 0;
e9670677
MR
14830}
14831\f
14832void
17a2f251 14833mips_init_after_args (void)
252b5132
RH
14834{
14835 /* initialize opcodes */
14836 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14837 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14838}
14839
14840long
17a2f251 14841md_pcrel_from (fixS *fixP)
252b5132 14842{
a7ebbfdf
TS
14843 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14844 switch (fixP->fx_r_type)
14845 {
df58fc94
RS
14846 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14847 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14848 /* Return the address of the delay slot. */
14849 return addr + 2;
14850
14851 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14852 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 14853 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 14854 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
14855 case BFD_RELOC_MIPS_21_PCREL_S2:
14856 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
14857 case BFD_RELOC_MIPS_JMP:
14858 /* Return the address of the delay slot. */
14859 return addr + 4;
df58fc94 14860
51f6035b
MR
14861 case BFD_RELOC_MIPS_18_PCREL_S3:
14862 /* Return the aligned address of the doubleword containing
14863 the instruction. */
14864 return addr & ~7;
14865
a7ebbfdf
TS
14866 default:
14867 return addr;
14868 }
252b5132
RH
14869}
14870
252b5132
RH
14871/* This is called before the symbol table is processed. In order to
14872 work with gcc when using mips-tfile, we must keep all local labels.
14873 However, in other cases, we want to discard them. If we were
14874 called with -g, but we didn't see any debugging information, it may
14875 mean that gcc is smuggling debugging information through to
14876 mips-tfile, in which case we must generate all local labels. */
14877
14878void
17a2f251 14879mips_frob_file_before_adjust (void)
252b5132
RH
14880{
14881#ifndef NO_ECOFF_DEBUGGING
14882 if (ECOFF_DEBUGGING
14883 && mips_debug != 0
14884 && ! ecoff_debugging_seen)
14885 flag_keep_locals = 1;
14886#endif
14887}
14888
3b91255e 14889/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14890 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14891 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14892 relocation operators.
14893
14894 For our purposes, a %lo() expression matches a %got() or %hi()
14895 expression if:
14896
14897 (a) it refers to the same symbol; and
14898 (b) the offset applied in the %lo() expression is no lower than
14899 the offset applied in the %got() or %hi().
14900
14901 (b) allows us to cope with code like:
14902
14903 lui $4,%hi(foo)
14904 lh $4,%lo(foo+2)($4)
14905
14906 ...which is legal on RELA targets, and has a well-defined behaviour
14907 if the user knows that adding 2 to "foo" will not induce a carry to
14908 the high 16 bits.
14909
14910 When several %lo()s match a particular %got() or %hi(), we use the
14911 following rules to distinguish them:
14912
14913 (1) %lo()s with smaller offsets are a better match than %lo()s with
14914 higher offsets.
14915
14916 (2) %lo()s with no matching %got() or %hi() are better than those
14917 that already have a matching %got() or %hi().
14918
14919 (3) later %lo()s are better than earlier %lo()s.
14920
14921 These rules are applied in order.
14922
14923 (1) means, among other things, that %lo()s with identical offsets are
14924 chosen if they exist.
14925
14926 (2) means that we won't associate several high-part relocations with
14927 the same low-part relocation unless there's no alternative. Having
14928 several high parts for the same low part is a GNU extension; this rule
14929 allows careful users to avoid it.
14930
14931 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14932 with the last high-part relocation being at the front of the list.
14933 It therefore makes sense to choose the last matching low-part
14934 relocation, all other things being equal. It's also easier
14935 to code that way. */
252b5132
RH
14936
14937void
17a2f251 14938mips_frob_file (void)
252b5132
RH
14939{
14940 struct mips_hi_fixup *l;
35903be0 14941 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14942
14943 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14944 {
14945 segment_info_type *seginfo;
3b91255e
RS
14946 bfd_boolean matched_lo_p;
14947 fixS **hi_pos, **lo_pos, **pos;
252b5132 14948
9c2799c2 14949 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14950
5919d012 14951 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14952 there isn't supposed to be a matching LO. Ignore %gots against
14953 constants; we'll report an error for those later. */
738e5348 14954 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 14955 && !(l->fixp->fx_addsy
9e009953 14956 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
14957 continue;
14958
14959 /* Check quickly whether the next fixup happens to be a matching %lo. */
14960 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14961 continue;
14962
252b5132 14963 seginfo = seg_info (l->seg);
252b5132 14964
3b91255e
RS
14965 /* Set HI_POS to the position of this relocation in the chain.
14966 Set LO_POS to the position of the chosen low-part relocation.
14967 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14968 relocation that matches an immediately-preceding high-part
14969 relocation. */
14970 hi_pos = NULL;
14971 lo_pos = NULL;
14972 matched_lo_p = FALSE;
738e5348 14973 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14974
3b91255e
RS
14975 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14976 {
14977 if (*pos == l->fixp)
14978 hi_pos = pos;
14979
35903be0 14980 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14981 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14982 && (*pos)->fx_offset >= l->fixp->fx_offset
14983 && (lo_pos == NULL
14984 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14985 || (!matched_lo_p
14986 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14987 lo_pos = pos;
14988
14989 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14990 && fixup_has_matching_lo_p (*pos));
14991 }
14992
14993 /* If we found a match, remove the high-part relocation from its
14994 current position and insert it before the low-part relocation.
14995 Make the offsets match so that fixup_has_matching_lo_p()
14996 will return true.
14997
14998 We don't warn about unmatched high-part relocations since some
14999 versions of gcc have been known to emit dead "lui ...%hi(...)"
15000 instructions. */
15001 if (lo_pos != NULL)
15002 {
15003 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15004 if (l->fixp->fx_next != *lo_pos)
252b5132 15005 {
3b91255e
RS
15006 *hi_pos = l->fixp->fx_next;
15007 l->fixp->fx_next = *lo_pos;
15008 *lo_pos = l->fixp;
252b5132 15009 }
252b5132
RH
15010 }
15011 }
15012}
15013
252b5132 15014int
17a2f251 15015mips_force_relocation (fixS *fixp)
252b5132 15016{
ae6063d4 15017 if (generic_force_reloc (fixp))
252b5132
RH
15018 return 1;
15019
df58fc94
RS
15020 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15021 so that the linker relaxation can update targets. */
15022 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15023 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15024 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15025 return 1;
15026
5caa2b07
MR
15027 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15028 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15029 microMIPS symbols so that we can do cross-mode branch diagnostics
15030 and BAL to JALX conversion by the linker. */
15031 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15032 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15033 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15034 && fixp->fx_addsy
15035 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15036 return 1;
15037
7361da2c 15038 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15039 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15040 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15041 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15042 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15043 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15044 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15045 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15046 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15047 return 1;
15048
3e722fb5 15049 return 0;
252b5132
RH
15050}
15051
b416ba9b
MR
15052/* Implement TC_FORCE_RELOCATION_ABS. */
15053
15054bfd_boolean
15055mips_force_relocation_abs (fixS *fixp)
15056{
15057 if (generic_force_reloc (fixp))
15058 return TRUE;
15059
15060 /* These relocations do not have enough bits in the in-place addend
15061 to hold an arbitrary absolute section's offset. */
15062 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15063 return TRUE;
15064
15065 return FALSE;
15066}
15067
b886a2ab
RS
15068/* Read the instruction associated with RELOC from BUF. */
15069
15070static unsigned int
15071read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15072{
15073 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15074 return read_compressed_insn (buf, 4);
15075 else
15076 return read_insn (buf);
15077}
15078
15079/* Write instruction INSN to BUF, given that it has been relocated
15080 by RELOC. */
15081
15082static void
15083write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15084 unsigned long insn)
15085{
15086 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15087 write_compressed_insn (buf, insn, 4);
15088 else
15089 write_insn (buf, insn);
15090}
15091
9d862524
MR
15092/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15093 to a symbol in another ISA mode, which cannot be converted to JALX. */
15094
15095static bfd_boolean
15096fix_bad_cross_mode_jump_p (fixS *fixP)
15097{
15098 unsigned long opcode;
15099 int other;
15100 char *buf;
15101
15102 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15103 return FALSE;
15104
15105 other = S_GET_OTHER (fixP->fx_addsy);
15106 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15107 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15108 switch (fixP->fx_r_type)
15109 {
15110 case BFD_RELOC_MIPS_JMP:
15111 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15112 case BFD_RELOC_MICROMIPS_JMP:
15113 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15114 default:
15115 return FALSE;
15116 }
15117}
15118
15119/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15120 jump to a symbol in the same ISA mode. */
15121
15122static bfd_boolean
15123fix_bad_same_mode_jalx_p (fixS *fixP)
15124{
15125 unsigned long opcode;
15126 int other;
15127 char *buf;
15128
15129 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15130 return FALSE;
15131
15132 other = S_GET_OTHER (fixP->fx_addsy);
15133 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15134 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15135 switch (fixP->fx_r_type)
15136 {
15137 case BFD_RELOC_MIPS_JMP:
15138 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15139 case BFD_RELOC_MIPS16_JMP:
15140 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15141 case BFD_RELOC_MICROMIPS_JMP:
15142 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15143 default:
15144 return FALSE;
15145 }
15146}
15147
15148/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15149 to a symbol whose value plus addend is not aligned according to the
15150 ultimate (after linker relaxation) jump instruction's immediate field
15151 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15152 regular MIPS code, to (1 << 2). */
15153
15154static bfd_boolean
15155fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15156{
15157 bfd_boolean micro_to_mips_p;
15158 valueT val;
15159 int other;
15160
15161 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15162 return FALSE;
15163
15164 other = S_GET_OTHER (fixP->fx_addsy);
15165 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15166 val += fixP->fx_offset;
15167 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15168 && !ELF_ST_IS_MICROMIPS (other));
15169 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15170 != ELF_ST_IS_COMPRESSED (other));
15171}
15172
15173/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15174 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15175 symbols check the ISA bit instead.
15176
15177 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15178 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15179 MIPS symbols and associated with BAL instructions as these instructions
15180 may be be converted to JALX by the linker. */
9d862524
MR
15181
15182static bfd_boolean
15183fix_bad_cross_mode_branch_p (fixS *fixP)
15184{
15185 bfd_boolean absolute_p;
15186 unsigned long opcode;
15187 asection *symsec;
15188 valueT val;
15189 int other;
15190 char *buf;
15191
8b10b0b3
MR
15192 if (mips_ignore_branch_isa)
15193 return FALSE;
15194
9d862524
MR
15195 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15196 return FALSE;
15197
15198 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15199 absolute_p = bfd_is_abs_section (symsec);
15200
15201 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15202 other = S_GET_OTHER (fixP->fx_addsy);
15203
15204 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15205 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15206 switch (fixP->fx_r_type)
15207 {
15208 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15209 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15210 && opcode != 0x0411);
15211 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15212 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15213 && opcode != 0x4060);
9d862524
MR
15214 case BFD_RELOC_MIPS_21_PCREL_S2:
15215 case BFD_RELOC_MIPS_26_PCREL_S2:
15216 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15217 case BFD_RELOC_MIPS16_16_PCREL_S1:
15218 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15219 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15220 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15221 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15222 default:
15223 abort ();
15224 }
15225}
15226
15227/* Return TRUE if the symbol plus addend associated with a regular MIPS
15228 branch instruction pointed to by FIXP is not aligned according to the
15229 branch instruction's immediate field requirement. We need the addend
15230 to preserve the ISA bit and also the sum must not have bit 2 set. We
15231 must explicitly OR in the ISA bit from symbol annotation as the bit
15232 won't be set in the symbol's value then. */
15233
15234static bfd_boolean
15235fix_bad_misaligned_branch_p (fixS *fixP)
15236{
15237 bfd_boolean absolute_p;
15238 asection *symsec;
15239 valueT isa_bit;
15240 valueT val;
15241 valueT off;
15242 int other;
15243
15244 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15245 return FALSE;
15246
15247 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15248 absolute_p = bfd_is_abs_section (symsec);
15249
15250 val = S_GET_VALUE (fixP->fx_addsy);
15251 other = S_GET_OTHER (fixP->fx_addsy);
15252 off = fixP->fx_offset;
15253
15254 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15255 val |= ELF_ST_IS_COMPRESSED (other);
15256 val += off;
15257 return (val & 0x3) != isa_bit;
15258}
15259
15260/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15261 and its calculated value VAL. */
15262
15263static void
15264fix_validate_branch (fixS *fixP, valueT val)
15265{
15266 if (fixP->fx_done && (val & 0x3) != 0)
15267 as_bad_where (fixP->fx_file, fixP->fx_line,
15268 _("branch to misaligned address (0x%lx)"),
15269 (long) (val + md_pcrel_from (fixP)));
15270 else if (fix_bad_cross_mode_branch_p (fixP))
15271 as_bad_where (fixP->fx_file, fixP->fx_line,
15272 _("branch to a symbol in another ISA mode"));
15273 else if (fix_bad_misaligned_branch_p (fixP))
15274 as_bad_where (fixP->fx_file, fixP->fx_line,
15275 _("branch to misaligned address (0x%lx)"),
15276 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15277 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15278 as_bad_where (fixP->fx_file, fixP->fx_line,
15279 _("cannot encode misaligned addend "
15280 "in the relocatable field (0x%lx)"),
15281 (long) fixP->fx_offset);
15282}
15283
252b5132
RH
15284/* Apply a fixup to the object file. */
15285
94f592af 15286void
55cf6793 15287md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15288{
4d68580a 15289 char *buf;
b886a2ab 15290 unsigned long insn;
a7ebbfdf 15291 reloc_howto_type *howto;
252b5132 15292
d56a8dda
RS
15293 if (fixP->fx_pcrel)
15294 switch (fixP->fx_r_type)
15295 {
15296 case BFD_RELOC_16_PCREL_S2:
c9775dde 15297 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15298 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15299 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15300 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15301 case BFD_RELOC_32_PCREL:
7361da2c
AB
15302 case BFD_RELOC_MIPS_21_PCREL_S2:
15303 case BFD_RELOC_MIPS_26_PCREL_S2:
15304 case BFD_RELOC_MIPS_18_PCREL_S3:
15305 case BFD_RELOC_MIPS_19_PCREL_S2:
15306 case BFD_RELOC_HI16_S_PCREL:
15307 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15308 break;
15309
15310 case BFD_RELOC_32:
15311 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15312 break;
15313
15314 default:
15315 as_bad_where (fixP->fx_file, fixP->fx_line,
15316 _("PC-relative reference to a different section"));
15317 break;
15318 }
15319
15320 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15321 that have no MIPS ELF equivalent. */
15322 if (fixP->fx_r_type != BFD_RELOC_8)
15323 {
15324 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15325 if (!howto)
15326 return;
15327 }
65551fa4 15328
df58fc94
RS
15329 gas_assert (fixP->fx_size == 2
15330 || fixP->fx_size == 4
d56a8dda 15331 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15332 || fixP->fx_r_type == BFD_RELOC_16
15333 || fixP->fx_r_type == BFD_RELOC_64
15334 || fixP->fx_r_type == BFD_RELOC_CTOR
15335 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15336 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15337 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15338 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15339 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15340 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15341
4d68580a 15342 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15343
b1dca8ee
RS
15344 /* Don't treat parts of a composite relocation as done. There are two
15345 reasons for this:
15346
15347 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15348 should nevertheless be emitted if the first part is.
15349
15350 (2) In normal usage, composite relocations are never assembly-time
15351 constants. The easiest way of dealing with the pathological
15352 exceptions is to generate a relocation against STN_UNDEF and
15353 leave everything up to the linker. */
3994f87e 15354 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15355 fixP->fx_done = 1;
15356
15357 switch (fixP->fx_r_type)
15358 {
3f98094e
DJ
15359 case BFD_RELOC_MIPS_TLS_GD:
15360 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15361 case BFD_RELOC_MIPS_TLS_DTPREL32:
15362 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15363 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15364 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15365 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15366 case BFD_RELOC_MIPS_TLS_TPREL32:
15367 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15368 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15369 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15370 case BFD_RELOC_MICROMIPS_TLS_GD:
15371 case BFD_RELOC_MICROMIPS_TLS_LDM:
15372 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15373 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15374 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15375 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15376 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15377 case BFD_RELOC_MIPS16_TLS_GD:
15378 case BFD_RELOC_MIPS16_TLS_LDM:
15379 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15380 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15381 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15382 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15383 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15384 if (fixP->fx_addsy)
15385 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15386 else
15387 as_bad_where (fixP->fx_file, fixP->fx_line,
15388 _("TLS relocation against a constant"));
15389 break;
3f98094e 15390
252b5132 15391 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15392 case BFD_RELOC_MIPS16_JMP:
15393 case BFD_RELOC_MICROMIPS_JMP:
15394 {
15395 int shift;
15396
15397 gas_assert (!fixP->fx_done);
15398
15399 /* Shift is 2, unusually, for microMIPS JALX. */
15400 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15401 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15402 shift = 1;
15403 else
15404 shift = 2;
15405
15406 if (fix_bad_cross_mode_jump_p (fixP))
15407 as_bad_where (fixP->fx_file, fixP->fx_line,
15408 _("jump to a symbol in another ISA mode"));
15409 else if (fix_bad_same_mode_jalx_p (fixP))
15410 as_bad_where (fixP->fx_file, fixP->fx_line,
15411 _("JALX to a symbol in the same ISA mode"));
15412 else if (fix_bad_misaligned_jump_p (fixP, shift))
15413 as_bad_where (fixP->fx_file, fixP->fx_line,
15414 _("jump to misaligned address (0x%lx)"),
15415 (long) (S_GET_VALUE (fixP->fx_addsy)
15416 + fixP->fx_offset));
15417 else if (HAVE_IN_PLACE_ADDENDS
15418 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15419 as_bad_where (fixP->fx_file, fixP->fx_line,
15420 _("cannot encode misaligned addend "
15421 "in the relocatable field (0x%lx)"),
15422 (long) fixP->fx_offset);
15423 }
15424 /* Fall through. */
15425
e369bcce
TS
15426 case BFD_RELOC_MIPS_SHIFT5:
15427 case BFD_RELOC_MIPS_SHIFT6:
15428 case BFD_RELOC_MIPS_GOT_DISP:
15429 case BFD_RELOC_MIPS_GOT_PAGE:
15430 case BFD_RELOC_MIPS_GOT_OFST:
15431 case BFD_RELOC_MIPS_SUB:
15432 case BFD_RELOC_MIPS_INSERT_A:
15433 case BFD_RELOC_MIPS_INSERT_B:
15434 case BFD_RELOC_MIPS_DELETE:
15435 case BFD_RELOC_MIPS_HIGHEST:
15436 case BFD_RELOC_MIPS_HIGHER:
15437 case BFD_RELOC_MIPS_SCN_DISP:
15438 case BFD_RELOC_MIPS_REL16:
15439 case BFD_RELOC_MIPS_RELGOT:
15440 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15441 case BFD_RELOC_HI16:
15442 case BFD_RELOC_HI16_S:
b886a2ab 15443 case BFD_RELOC_LO16:
cdf6fd85 15444 case BFD_RELOC_GPREL16:
252b5132
RH
15445 case BFD_RELOC_MIPS_LITERAL:
15446 case BFD_RELOC_MIPS_CALL16:
15447 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15448 case BFD_RELOC_GPREL32:
252b5132
RH
15449 case BFD_RELOC_MIPS_GOT_HI16:
15450 case BFD_RELOC_MIPS_GOT_LO16:
15451 case BFD_RELOC_MIPS_CALL_HI16:
15452 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15453 case BFD_RELOC_HI16_S_PCREL:
15454 case BFD_RELOC_LO16_PCREL:
252b5132 15455 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15456 case BFD_RELOC_MIPS16_GOT16:
15457 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15458 case BFD_RELOC_MIPS16_HI16:
15459 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15460 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15461 case BFD_RELOC_MICROMIPS_GOT_DISP:
15462 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15463 case BFD_RELOC_MICROMIPS_GOT_OFST:
15464 case BFD_RELOC_MICROMIPS_SUB:
15465 case BFD_RELOC_MICROMIPS_HIGHEST:
15466 case BFD_RELOC_MICROMIPS_HIGHER:
15467 case BFD_RELOC_MICROMIPS_SCN_DISP:
15468 case BFD_RELOC_MICROMIPS_JALR:
15469 case BFD_RELOC_MICROMIPS_HI16:
15470 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15471 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15472 case BFD_RELOC_MICROMIPS_GPREL16:
15473 case BFD_RELOC_MICROMIPS_LITERAL:
15474 case BFD_RELOC_MICROMIPS_CALL16:
15475 case BFD_RELOC_MICROMIPS_GOT16:
15476 case BFD_RELOC_MICROMIPS_GOT_HI16:
15477 case BFD_RELOC_MICROMIPS_GOT_LO16:
15478 case BFD_RELOC_MICROMIPS_CALL_HI16:
15479 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15480 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15481 if (fixP->fx_done)
15482 {
15483 offsetT value;
15484
15485 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15486 {
15487 insn = read_reloc_insn (buf, fixP->fx_r_type);
15488 if (mips16_reloc_p (fixP->fx_r_type))
15489 insn |= mips16_immed_extend (value, 16);
15490 else
15491 insn |= (value & 0xffff);
15492 write_reloc_insn (buf, fixP->fx_r_type, insn);
15493 }
15494 else
15495 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15496 _("unsupported constant in relocation"));
b886a2ab 15497 }
252b5132
RH
15498 break;
15499
252b5132
RH
15500 case BFD_RELOC_64:
15501 /* This is handled like BFD_RELOC_32, but we output a sign
15502 extended value if we are only 32 bits. */
3e722fb5 15503 if (fixP->fx_done)
252b5132
RH
15504 {
15505 if (8 <= sizeof (valueT))
4d68580a 15506 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15507 else
15508 {
a7ebbfdf 15509 valueT hiv;
252b5132 15510
a7ebbfdf 15511 if ((*valP & 0x80000000) != 0)
252b5132
RH
15512 hiv = 0xffffffff;
15513 else
15514 hiv = 0;
4d68580a
RS
15515 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15516 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15517 }
15518 }
15519 break;
15520
056350c6 15521 case BFD_RELOC_RVA:
252b5132 15522 case BFD_RELOC_32:
b47468a6 15523 case BFD_RELOC_32_PCREL:
252b5132 15524 case BFD_RELOC_16:
d56a8dda 15525 case BFD_RELOC_8:
252b5132 15526 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15527 value now. This can happen if we have a .word which is not
15528 resolved when it appears but is later defined. */
252b5132 15529 if (fixP->fx_done)
4d68580a 15530 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15531 break;
15532
7361da2c 15533 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15534 fix_validate_branch (fixP, *valP);
41947d9e
MR
15535 if (!fixP->fx_done)
15536 break;
15537
15538 if (*valP + 0x400000 <= 0x7fffff)
15539 {
15540 insn = read_insn (buf);
15541 insn |= (*valP >> 2) & 0x1fffff;
15542 write_insn (buf, insn);
15543 }
15544 else
15545 as_bad_where (fixP->fx_file, fixP->fx_line,
15546 _("branch out of range"));
15547 break;
15548
7361da2c 15549 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15550 fix_validate_branch (fixP, *valP);
41947d9e
MR
15551 if (!fixP->fx_done)
15552 break;
7361da2c 15553
41947d9e
MR
15554 if (*valP + 0x8000000 <= 0xfffffff)
15555 {
15556 insn = read_insn (buf);
15557 insn |= (*valP >> 2) & 0x3ffffff;
15558 write_insn (buf, insn);
15559 }
15560 else
15561 as_bad_where (fixP->fx_file, fixP->fx_line,
15562 _("branch out of range"));
7361da2c
AB
15563 break;
15564
15565 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15566 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15567 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15568 _("PC-relative access using misaligned symbol (%lx)"),
15569 (long) S_GET_VALUE (fixP->fx_addsy));
15570 if ((fixP->fx_offset & 0x7) != 0)
15571 as_bad_where (fixP->fx_file, fixP->fx_line,
15572 _("PC-relative access using misaligned offset (%lx)"),
15573 (long) fixP->fx_offset);
41947d9e
MR
15574 if (!fixP->fx_done)
15575 break;
7361da2c 15576
41947d9e
MR
15577 if (*valP + 0x100000 <= 0x1fffff)
15578 {
15579 insn = read_insn (buf);
15580 insn |= (*valP >> 3) & 0x3ffff;
15581 write_insn (buf, insn);
15582 }
15583 else
15584 as_bad_where (fixP->fx_file, fixP->fx_line,
15585 _("PC-relative access out of range"));
7361da2c
AB
15586 break;
15587
15588 case BFD_RELOC_MIPS_19_PCREL_S2:
15589 if ((*valP & 0x3) != 0)
15590 as_bad_where (fixP->fx_file, fixP->fx_line,
15591 _("PC-relative access to misaligned address (%lx)"),
717ba204 15592 (long) *valP);
41947d9e
MR
15593 if (!fixP->fx_done)
15594 break;
7361da2c 15595
41947d9e
MR
15596 if (*valP + 0x100000 <= 0x1fffff)
15597 {
15598 insn = read_insn (buf);
15599 insn |= (*valP >> 2) & 0x7ffff;
15600 write_insn (buf, insn);
15601 }
15602 else
15603 as_bad_where (fixP->fx_file, fixP->fx_line,
15604 _("PC-relative access out of range"));
7361da2c
AB
15605 break;
15606
252b5132 15607 case BFD_RELOC_16_PCREL_S2:
9d862524 15608 fix_validate_branch (fixP, *valP);
cb56d3d3 15609
54f4ddb3
TS
15610 /* We need to save the bits in the instruction since fixup_segment()
15611 might be deleting the relocation entry (i.e., a branch within
15612 the current segment). */
a7ebbfdf 15613 if (! fixP->fx_done)
bb2d6cd7 15614 break;
252b5132 15615
54f4ddb3 15616 /* Update old instruction data. */
4d68580a 15617 insn = read_insn (buf);
252b5132 15618
a7ebbfdf
TS
15619 if (*valP + 0x20000 <= 0x3ffff)
15620 {
15621 insn |= (*valP >> 2) & 0xffff;
4d68580a 15622 write_insn (buf, insn);
a7ebbfdf 15623 }
ce8ad872 15624 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15625 && fixP->fx_done
15626 && fixP->fx_frag->fr_address >= text_section->vma
15627 && (fixP->fx_frag->fr_address
587aac4e 15628 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15629 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15630 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15631 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15632 {
15633 /* The branch offset is too large. If this is an
15634 unconditional branch, and we are not generating PIC code,
15635 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15636 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15637 insn = 0x0c000000; /* jal */
252b5132 15638 else
a7ebbfdf
TS
15639 insn = 0x08000000; /* j */
15640 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15641 fixP->fx_done = 0;
15642 fixP->fx_addsy = section_symbol (text_section);
15643 *valP += md_pcrel_from (fixP);
4d68580a 15644 write_insn (buf, insn);
a7ebbfdf
TS
15645 }
15646 else
15647 {
15648 /* If we got here, we have branch-relaxation disabled,
15649 and there's nothing we can do to fix this instruction
15650 without turning it into a longer sequence. */
15651 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15652 _("branch out of range"));
252b5132 15653 }
252b5132
RH
15654 break;
15655
c9775dde 15656 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15657 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15658 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15659 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15660 gas_assert (!fixP->fx_done);
9d862524
MR
15661 if (fix_bad_cross_mode_branch_p (fixP))
15662 as_bad_where (fixP->fx_file, fixP->fx_line,
15663 _("branch to a symbol in another ISA mode"));
15664 else if (fixP->fx_addsy
15665 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15666 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15667 && (fixP->fx_offset & 0x1) != 0)
15668 as_bad_where (fixP->fx_file, fixP->fx_line,
15669 _("branch to misaligned address (0x%lx)"),
15670 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15671 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15672 as_bad_where (fixP->fx_file, fixP->fx_line,
15673 _("cannot encode misaligned addend "
15674 "in the relocatable field (0x%lx)"),
15675 (long) fixP->fx_offset);
df58fc94
RS
15676 break;
15677
252b5132
RH
15678 case BFD_RELOC_VTABLE_INHERIT:
15679 fixP->fx_done = 0;
15680 if (fixP->fx_addsy
15681 && !S_IS_DEFINED (fixP->fx_addsy)
15682 && !S_IS_WEAK (fixP->fx_addsy))
15683 S_SET_WEAK (fixP->fx_addsy);
15684 break;
15685
2f0c68f2 15686 case BFD_RELOC_NONE:
252b5132
RH
15687 case BFD_RELOC_VTABLE_ENTRY:
15688 fixP->fx_done = 0;
15689 break;
15690
15691 default:
b37df7c4 15692 abort ();
252b5132 15693 }
a7ebbfdf
TS
15694
15695 /* Remember value for tc_gen_reloc. */
15696 fixP->fx_addnumber = *valP;
252b5132
RH
15697}
15698
252b5132 15699static symbolS *
17a2f251 15700get_symbol (void)
252b5132
RH
15701{
15702 int c;
15703 char *name;
15704 symbolS *p;
15705
d02603dc 15706 c = get_symbol_name (&name);
252b5132 15707 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15708 (void) restore_line_pointer (c);
252b5132
RH
15709 return p;
15710}
15711
742a56fe
RS
15712/* Align the current frag to a given power of two. If a particular
15713 fill byte should be used, FILL points to an integer that contains
15714 that byte, otherwise FILL is null.
15715
462427c4
RS
15716 This function used to have the comment:
15717
15718 The MIPS assembler also automatically adjusts any preceding label.
15719
15720 The implementation therefore applied the adjustment to a maximum of
15721 one label. However, other label adjustments are applied to batches
15722 of labels, and adjusting just one caused problems when new labels
15723 were added for the sake of debugging or unwind information.
15724 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15725
15726static void
462427c4 15727mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15728{
7d10b47d 15729 mips_emit_delays ();
df58fc94 15730 mips_record_compressed_mode ();
742a56fe
RS
15731 if (fill == NULL && subseg_text_p (now_seg))
15732 frag_align_code (to, 0);
15733 else
15734 frag_align (to, fill ? *fill : 0, 0);
252b5132 15735 record_alignment (now_seg, to);
462427c4 15736 mips_move_labels (labels, FALSE);
252b5132
RH
15737}
15738
15739/* Align to a given power of two. .align 0 turns off the automatic
15740 alignment used by the data creating pseudo-ops. */
15741
15742static void
17a2f251 15743s_align (int x ATTRIBUTE_UNUSED)
252b5132 15744{
742a56fe 15745 int temp, fill_value, *fill_ptr;
49954fb4 15746 long max_alignment = 28;
252b5132 15747
54f4ddb3 15748 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15749 to the aligned address.
54f4ddb3 15750 o It's not documented but auto alignment is reinstated by
252b5132 15751 a .align pseudo instruction.
54f4ddb3 15752 o Note also that after auto alignment is turned off the mips assembler
252b5132 15753 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15754 We don't. */
252b5132
RH
15755
15756 temp = get_absolute_expression ();
15757 if (temp > max_alignment)
1661c76c 15758 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15759 else if (temp < 0)
15760 {
1661c76c 15761 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15762 temp = 0;
15763 }
15764 if (*input_line_pointer == ',')
15765 {
f9419b05 15766 ++input_line_pointer;
742a56fe
RS
15767 fill_value = get_absolute_expression ();
15768 fill_ptr = &fill_value;
252b5132
RH
15769 }
15770 else
742a56fe 15771 fill_ptr = 0;
252b5132
RH
15772 if (temp)
15773 {
a8dbcb85
TS
15774 segment_info_type *si = seg_info (now_seg);
15775 struct insn_label_list *l = si->label_list;
54f4ddb3 15776 /* Auto alignment should be switched on by next section change. */
252b5132 15777 auto_align = 1;
462427c4 15778 mips_align (temp, fill_ptr, l);
252b5132
RH
15779 }
15780 else
15781 {
15782 auto_align = 0;
15783 }
15784
15785 demand_empty_rest_of_line ();
15786}
15787
252b5132 15788static void
17a2f251 15789s_change_sec (int sec)
252b5132
RH
15790{
15791 segT seg;
15792
252b5132
RH
15793 /* The ELF backend needs to know that we are changing sections, so
15794 that .previous works correctly. We could do something like check
b6ff326e 15795 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15796 as it would not be appropriate to use it in the section changing
15797 functions in read.c, since obj-elf.c intercepts those. FIXME:
15798 This should be cleaner, somehow. */
f3ded42a 15799 obj_elf_section_change_hook ();
252b5132 15800
7d10b47d 15801 mips_emit_delays ();
6a32d874 15802
252b5132
RH
15803 switch (sec)
15804 {
15805 case 't':
15806 s_text (0);
15807 break;
15808 case 'd':
15809 s_data (0);
15810 break;
15811 case 'b':
15812 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15813 demand_empty_rest_of_line ();
15814 break;
15815
15816 case 'r':
4d0d148d
TS
15817 seg = subseg_new (RDATA_SECTION_NAME,
15818 (subsegT) get_absolute_expression ());
f3ded42a
RS
15819 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15820 | SEC_READONLY | SEC_RELOC
15821 | SEC_DATA));
15822 if (strncmp (TARGET_OS, "elf", 3) != 0)
15823 record_alignment (seg, 4);
4d0d148d 15824 demand_empty_rest_of_line ();
252b5132
RH
15825 break;
15826
15827 case 's':
4d0d148d 15828 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
15829 bfd_set_section_flags (stdoutput, seg,
15830 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15831 if (strncmp (TARGET_OS, "elf", 3) != 0)
15832 record_alignment (seg, 4);
4d0d148d
TS
15833 demand_empty_rest_of_line ();
15834 break;
998b3c36
MR
15835
15836 case 'B':
15837 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
15838 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15839 if (strncmp (TARGET_OS, "elf", 3) != 0)
15840 record_alignment (seg, 4);
998b3c36
MR
15841 demand_empty_rest_of_line ();
15842 break;
252b5132
RH
15843 }
15844
15845 auto_align = 1;
15846}
b34976b6 15847
cca86cc8 15848void
17a2f251 15849s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15850{
d02603dc 15851 char *saved_ilp;
cca86cc8 15852 char *section_name;
d02603dc 15853 char c, endc;
684022ea 15854 char next_c = 0;
cca86cc8
SC
15855 int section_type;
15856 int section_flag;
15857 int section_entry_size;
15858 int section_alignment;
b34976b6 15859
d02603dc
NC
15860 saved_ilp = input_line_pointer;
15861 endc = get_symbol_name (&section_name);
15862 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 15863 if (c)
d02603dc 15864 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 15865
4cf0dd0d
TS
15866 /* Do we have .section Name<,"flags">? */
15867 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 15868 {
d02603dc
NC
15869 /* Just after name is now '\0'. */
15870 (void) restore_line_pointer (endc);
15871 input_line_pointer = saved_ilp;
cca86cc8
SC
15872 obj_elf_section (ignore);
15873 return;
15874 }
d02603dc
NC
15875
15876 section_name = xstrdup (section_name);
15877 c = restore_line_pointer (endc);
15878
cca86cc8
SC
15879 input_line_pointer++;
15880
15881 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15882 if (c == ',')
15883 section_type = get_absolute_expression ();
15884 else
15885 section_type = 0;
d02603dc 15886
cca86cc8
SC
15887 if (*input_line_pointer++ == ',')
15888 section_flag = get_absolute_expression ();
15889 else
15890 section_flag = 0;
d02603dc 15891
cca86cc8
SC
15892 if (*input_line_pointer++ == ',')
15893 section_entry_size = get_absolute_expression ();
15894 else
15895 section_entry_size = 0;
d02603dc 15896
cca86cc8
SC
15897 if (*input_line_pointer++ == ',')
15898 section_alignment = get_absolute_expression ();
15899 else
15900 section_alignment = 0;
d02603dc 15901
87975d2a
AM
15902 /* FIXME: really ignore? */
15903 (void) section_alignment;
cca86cc8 15904
8ab8a5c8
RS
15905 /* When using the generic form of .section (as implemented by obj-elf.c),
15906 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15907 traditionally had to fall back on the more common @progbits instead.
15908
15909 There's nothing really harmful in this, since bfd will correct
15910 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 15911 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
15912 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15913
15914 Even so, we shouldn't force users of the MIPS .section syntax to
15915 incorrectly label the sections as SHT_PROGBITS. The best compromise
15916 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15917 generic type-checking code. */
15918 if (section_type == SHT_MIPS_DWARF)
15919 section_type = SHT_PROGBITS;
15920
a91e1603 15921 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 15922 section_entry_size, 0, 0, 0);
a816d1ed
AO
15923
15924 if (now_seg->name != section_name)
15925 free (section_name);
cca86cc8 15926}
252b5132
RH
15927
15928void
17a2f251 15929mips_enable_auto_align (void)
252b5132
RH
15930{
15931 auto_align = 1;
15932}
15933
15934static void
17a2f251 15935s_cons (int log_size)
252b5132 15936{
a8dbcb85
TS
15937 segment_info_type *si = seg_info (now_seg);
15938 struct insn_label_list *l = si->label_list;
252b5132 15939
7d10b47d 15940 mips_emit_delays ();
252b5132 15941 if (log_size > 0 && auto_align)
462427c4 15942 mips_align (log_size, 0, l);
252b5132 15943 cons (1 << log_size);
a1facbec 15944 mips_clear_insn_labels ();
252b5132
RH
15945}
15946
15947static void
17a2f251 15948s_float_cons (int type)
252b5132 15949{
a8dbcb85
TS
15950 segment_info_type *si = seg_info (now_seg);
15951 struct insn_label_list *l = si->label_list;
252b5132 15952
7d10b47d 15953 mips_emit_delays ();
252b5132
RH
15954
15955 if (auto_align)
49309057
ILT
15956 {
15957 if (type == 'd')
462427c4 15958 mips_align (3, 0, l);
49309057 15959 else
462427c4 15960 mips_align (2, 0, l);
49309057 15961 }
252b5132 15962
252b5132 15963 float_cons (type);
a1facbec 15964 mips_clear_insn_labels ();
252b5132
RH
15965}
15966
15967/* Handle .globl. We need to override it because on Irix 5 you are
15968 permitted to say
15969 .globl foo .text
15970 where foo is an undefined symbol, to mean that foo should be
15971 considered to be the address of a function. */
15972
15973static void
17a2f251 15974s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
15975{
15976 char *name;
15977 int c;
15978 symbolS *symbolP;
15979 flagword flag;
15980
8a06b769 15981 do
252b5132 15982 {
d02603dc 15983 c = get_symbol_name (&name);
8a06b769
TS
15984 symbolP = symbol_find_or_make (name);
15985 S_SET_EXTERNAL (symbolP);
15986
252b5132 15987 *input_line_pointer = c;
d02603dc 15988 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 15989
8a06b769
TS
15990 /* On Irix 5, every global symbol that is not explicitly labelled as
15991 being a function is apparently labelled as being an object. */
15992 flag = BSF_OBJECT;
252b5132 15993
8a06b769
TS
15994 if (!is_end_of_line[(unsigned char) *input_line_pointer]
15995 && (*input_line_pointer != ','))
15996 {
15997 char *secname;
15998 asection *sec;
15999
d02603dc 16000 c = get_symbol_name (&secname);
8a06b769
TS
16001 sec = bfd_get_section_by_name (stdoutput, secname);
16002 if (sec == NULL)
16003 as_bad (_("%s: no such section"), secname);
d02603dc 16004 (void) restore_line_pointer (c);
8a06b769
TS
16005
16006 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16007 flag = BSF_FUNCTION;
16008 }
16009
16010 symbol_get_bfdsym (symbolP)->flags |= flag;
16011
16012 c = *input_line_pointer;
16013 if (c == ',')
16014 {
16015 input_line_pointer++;
16016 SKIP_WHITESPACE ();
16017 if (is_end_of_line[(unsigned char) *input_line_pointer])
16018 c = '\n';
16019 }
16020 }
16021 while (c == ',');
252b5132 16022
252b5132
RH
16023 demand_empty_rest_of_line ();
16024}
16025
16026static void
17a2f251 16027s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16028{
16029 char *opt;
16030 char c;
16031
d02603dc 16032 c = get_symbol_name (&opt);
252b5132
RH
16033
16034 if (*opt == 'O')
16035 {
16036 /* FIXME: What does this mean? */
16037 }
41a1578e 16038 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16039 {
16040 int i;
16041
16042 i = atoi (opt + 3);
668c5ebc
MR
16043 if (i != 0 && i != 2)
16044 as_bad (_(".option pic%d not supported"), i);
16045 else if (mips_pic == VXWORKS_PIC)
16046 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16047 else if (i == 0)
252b5132
RH
16048 mips_pic = NO_PIC;
16049 else if (i == 2)
143d77c5 16050 {
8b828383 16051 mips_pic = SVR4_PIC;
143d77c5
EC
16052 mips_abicalls = TRUE;
16053 }
252b5132 16054
4d0d148d 16055 if (mips_pic == SVR4_PIC)
252b5132
RH
16056 {
16057 if (g_switch_seen && g_switch_value != 0)
16058 as_warn (_("-G may not be used with SVR4 PIC code"));
16059 g_switch_value = 0;
16060 bfd_set_gp_size (stdoutput, 0);
16061 }
16062 }
16063 else
1661c76c 16064 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16065
d02603dc 16066 (void) restore_line_pointer (c);
252b5132
RH
16067 demand_empty_rest_of_line ();
16068}
16069
16070/* This structure is used to hold a stack of .set values. */
16071
e972090a
NC
16072struct mips_option_stack
16073{
252b5132
RH
16074 struct mips_option_stack *next;
16075 struct mips_set_options options;
16076};
16077
16078static struct mips_option_stack *mips_opts_stack;
16079
22522f88
MR
16080/* Return status for .set/.module option handling. */
16081
16082enum code_option_type
16083{
16084 /* Unrecognized option. */
16085 OPTION_TYPE_BAD = -1,
16086
16087 /* Ordinary option. */
16088 OPTION_TYPE_NORMAL,
16089
16090 /* ISA changing option. */
16091 OPTION_TYPE_ISA
16092};
16093
16094/* Handle common .set/.module options. Return status indicating option
16095 type. */
16096
16097static enum code_option_type
919731af 16098parse_code_option (char * name)
252b5132 16099{
22522f88 16100 bfd_boolean isa_set = FALSE;
c6278170 16101 const struct mips_ase *ase;
22522f88 16102
919731af 16103 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16104 {
16105 char *s = name + 3;
16106
16107 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16108 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16109 }
252b5132 16110 else if (strcmp (name, "at") == 0)
919731af 16111 mips_opts.at = ATREG;
252b5132 16112 else if (strcmp (name, "noat") == 0)
919731af 16113 mips_opts.at = ZERO;
252b5132 16114 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16115 mips_opts.nomove = 0;
252b5132 16116 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16117 mips_opts.nomove = 1;
252b5132 16118 else if (strcmp (name, "bopt") == 0)
919731af 16119 mips_opts.nobopt = 0;
252b5132 16120 else if (strcmp (name, "nobopt") == 0)
919731af 16121 mips_opts.nobopt = 1;
ad3fea08 16122 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16123 mips_opts.gp = 32;
ad3fea08 16124 else if (strcmp (name, "gp=64") == 0)
919731af 16125 mips_opts.gp = 64;
ad3fea08 16126 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16127 mips_opts.fp = 32;
351cdf24
MF
16128 else if (strcmp (name, "fp=xx") == 0)
16129 mips_opts.fp = 0;
ad3fea08 16130 else if (strcmp (name, "fp=64") == 0)
919731af 16131 mips_opts.fp = 64;
037b32b9
AN
16132 else if (strcmp (name, "softfloat") == 0)
16133 mips_opts.soft_float = 1;
16134 else if (strcmp (name, "hardfloat") == 0)
16135 mips_opts.soft_float = 0;
16136 else if (strcmp (name, "singlefloat") == 0)
16137 mips_opts.single_float = 1;
16138 else if (strcmp (name, "doublefloat") == 0)
16139 mips_opts.single_float = 0;
351cdf24
MF
16140 else if (strcmp (name, "nooddspreg") == 0)
16141 mips_opts.oddspreg = 0;
16142 else if (strcmp (name, "oddspreg") == 0)
16143 mips_opts.oddspreg = 1;
252b5132
RH
16144 else if (strcmp (name, "mips16") == 0
16145 || strcmp (name, "MIPS-16") == 0)
919731af 16146 mips_opts.mips16 = 1;
252b5132
RH
16147 else if (strcmp (name, "nomips16") == 0
16148 || strcmp (name, "noMIPS-16") == 0)
16149 mips_opts.mips16 = 0;
df58fc94 16150 else if (strcmp (name, "micromips") == 0)
919731af 16151 mips_opts.micromips = 1;
df58fc94
RS
16152 else if (strcmp (name, "nomicromips") == 0)
16153 mips_opts.micromips = 0;
c6278170
RS
16154 else if (name[0] == 'n'
16155 && name[1] == 'o'
16156 && (ase = mips_lookup_ase (name + 2)))
919731af 16157 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16158 else if ((ase = mips_lookup_ase (name)))
919731af 16159 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16160 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16161 {
1a2c1fad
CD
16162 /* Permit the user to change the ISA and architecture on the fly.
16163 Needless to say, misuse can cause serious problems. */
919731af 16164 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16165 {
16166 const struct mips_cpu_info *p;
16167
919731af 16168 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16169 if (!p)
16170 as_bad (_("unknown architecture %s"), name + 5);
16171 else
16172 {
16173 mips_opts.arch = p->cpu;
16174 mips_opts.isa = p->isa;
22522f88 16175 isa_set = TRUE;
1a2c1fad
CD
16176 }
16177 }
81a21e38
TS
16178 else if (strncmp (name, "mips", 4) == 0)
16179 {
16180 const struct mips_cpu_info *p;
16181
919731af 16182 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16183 if (!p)
16184 as_bad (_("unknown ISA level %s"), name + 4);
16185 else
16186 {
16187 mips_opts.arch = p->cpu;
16188 mips_opts.isa = p->isa;
22522f88 16189 isa_set = TRUE;
81a21e38
TS
16190 }
16191 }
af7ee8bf 16192 else
81a21e38 16193 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16194 }
16195 else if (strcmp (name, "autoextend") == 0)
16196 mips_opts.noautoextend = 0;
16197 else if (strcmp (name, "noautoextend") == 0)
16198 mips_opts.noautoextend = 1;
833794fc
MR
16199 else if (strcmp (name, "insn32") == 0)
16200 mips_opts.insn32 = TRUE;
16201 else if (strcmp (name, "noinsn32") == 0)
16202 mips_opts.insn32 = FALSE;
919731af 16203 else if (strcmp (name, "sym32") == 0)
16204 mips_opts.sym32 = TRUE;
16205 else if (strcmp (name, "nosym32") == 0)
16206 mips_opts.sym32 = FALSE;
16207 else
22522f88
MR
16208 return OPTION_TYPE_BAD;
16209
16210 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16211}
16212
16213/* Handle the .set pseudo-op. */
16214
16215static void
16216s_mipsset (int x ATTRIBUTE_UNUSED)
16217{
22522f88 16218 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16219 char *name = input_line_pointer, ch;
919731af 16220
16221 file_mips_check_options ();
16222
16223 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16224 ++input_line_pointer;
16225 ch = *input_line_pointer;
16226 *input_line_pointer = '\0';
16227
16228 if (strchr (name, ','))
16229 {
16230 /* Generic ".set" directive; use the generic handler. */
16231 *input_line_pointer = ch;
16232 input_line_pointer = name;
16233 s_set (0);
16234 return;
16235 }
16236
16237 if (strcmp (name, "reorder") == 0)
16238 {
16239 if (mips_opts.noreorder)
16240 end_noreorder ();
16241 }
16242 else if (strcmp (name, "noreorder") == 0)
16243 {
16244 if (!mips_opts.noreorder)
16245 start_noreorder ();
16246 }
16247 else if (strcmp (name, "macro") == 0)
16248 mips_opts.warn_about_macros = 0;
16249 else if (strcmp (name, "nomacro") == 0)
16250 {
16251 if (mips_opts.noreorder == 0)
16252 as_bad (_("`noreorder' must be set before `nomacro'"));
16253 mips_opts.warn_about_macros = 1;
16254 }
16255 else if (strcmp (name, "gp=default") == 0)
16256 mips_opts.gp = file_mips_opts.gp;
16257 else if (strcmp (name, "fp=default") == 0)
16258 mips_opts.fp = file_mips_opts.fp;
16259 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16260 {
16261 mips_opts.isa = file_mips_opts.isa;
16262 mips_opts.arch = file_mips_opts.arch;
16263 mips_opts.gp = file_mips_opts.gp;
16264 mips_opts.fp = file_mips_opts.fp;
16265 }
252b5132
RH
16266 else if (strcmp (name, "push") == 0)
16267 {
16268 struct mips_option_stack *s;
16269
325801bd 16270 s = XNEW (struct mips_option_stack);
252b5132
RH
16271 s->next = mips_opts_stack;
16272 s->options = mips_opts;
16273 mips_opts_stack = s;
16274 }
16275 else if (strcmp (name, "pop") == 0)
16276 {
16277 struct mips_option_stack *s;
16278
16279 s = mips_opts_stack;
16280 if (s == NULL)
16281 as_bad (_(".set pop with no .set push"));
16282 else
16283 {
16284 /* If we're changing the reorder mode we need to handle
16285 delay slots correctly. */
16286 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16287 start_noreorder ();
252b5132 16288 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16289 end_noreorder ();
252b5132
RH
16290
16291 mips_opts = s->options;
16292 mips_opts_stack = s->next;
16293 free (s);
16294 }
16295 }
22522f88
MR
16296 else
16297 {
16298 type = parse_code_option (name);
16299 if (type == OPTION_TYPE_BAD)
16300 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16301 }
919731af 16302
16303 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16304 registers based on what is supported by the arch/cpu. */
22522f88 16305 if (type == OPTION_TYPE_ISA)
e6559e01 16306 {
919731af 16307 switch (mips_opts.isa)
16308 {
16309 case 0:
16310 break;
16311 case ISA_MIPS1:
351cdf24
MF
16312 /* MIPS I cannot support FPXX. */
16313 mips_opts.fp = 32;
16314 /* fall-through. */
919731af 16315 case ISA_MIPS2:
16316 case ISA_MIPS32:
16317 case ISA_MIPS32R2:
16318 case ISA_MIPS32R3:
16319 case ISA_MIPS32R5:
16320 mips_opts.gp = 32;
351cdf24
MF
16321 if (mips_opts.fp != 0)
16322 mips_opts.fp = 32;
919731af 16323 break;
7361da2c
AB
16324 case ISA_MIPS32R6:
16325 mips_opts.gp = 32;
16326 mips_opts.fp = 64;
16327 break;
919731af 16328 case ISA_MIPS3:
16329 case ISA_MIPS4:
16330 case ISA_MIPS5:
16331 case ISA_MIPS64:
16332 case ISA_MIPS64R2:
16333 case ISA_MIPS64R3:
16334 case ISA_MIPS64R5:
7361da2c 16335 case ISA_MIPS64R6:
919731af 16336 mips_opts.gp = 64;
351cdf24
MF
16337 if (mips_opts.fp != 0)
16338 {
16339 if (mips_opts.arch == CPU_R5900)
16340 mips_opts.fp = 32;
16341 else
16342 mips_opts.fp = 64;
16343 }
919731af 16344 break;
16345 default:
16346 as_bad (_("unknown ISA level %s"), name + 4);
16347 break;
16348 }
e6559e01 16349 }
919731af 16350
16351 mips_check_options (&mips_opts, FALSE);
16352
16353 mips_check_isa_supports_ases ();
16354 *input_line_pointer = ch;
16355 demand_empty_rest_of_line ();
16356}
16357
16358/* Handle the .module pseudo-op. */
16359
16360static void
16361s_module (int ignore ATTRIBUTE_UNUSED)
16362{
16363 char *name = input_line_pointer, ch;
16364
16365 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16366 ++input_line_pointer;
16367 ch = *input_line_pointer;
16368 *input_line_pointer = '\0';
16369
16370 if (!file_mips_opts_checked)
252b5132 16371 {
22522f88 16372 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16373 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16374
16375 /* Update module level settings from mips_opts. */
16376 file_mips_opts = mips_opts;
252b5132 16377 }
919731af 16378 else
16379 as_bad (_(".module is not permitted after generating code"));
16380
252b5132
RH
16381 *input_line_pointer = ch;
16382 demand_empty_rest_of_line ();
16383}
16384
16385/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16386 .option pic2. It means to generate SVR4 PIC calls. */
16387
16388static void
17a2f251 16389s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16390{
16391 mips_pic = SVR4_PIC;
143d77c5 16392 mips_abicalls = TRUE;
4d0d148d
TS
16393
16394 if (g_switch_seen && g_switch_value != 0)
16395 as_warn (_("-G may not be used with SVR4 PIC code"));
16396 g_switch_value = 0;
16397
252b5132
RH
16398 bfd_set_gp_size (stdoutput, 0);
16399 demand_empty_rest_of_line ();
16400}
16401
16402/* Handle the .cpload pseudo-op. This is used when generating SVR4
16403 PIC code. It sets the $gp register for the function based on the
16404 function address, which is in the register named in the argument.
16405 This uses a relocation against _gp_disp, which is handled specially
16406 by the linker. The result is:
16407 lui $gp,%hi(_gp_disp)
16408 addiu $gp,$gp,%lo(_gp_disp)
16409 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16410 The .cpload argument is normally $25 == $t9.
16411
16412 The -mno-shared option changes this to:
bbe506e8
TS
16413 lui $gp,%hi(__gnu_local_gp)
16414 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16415 and the argument is ignored. This saves an instruction, but the
16416 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16417 address for __gnu_local_gp. Thus code assembled with -mno-shared
16418 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16419
16420static void
17a2f251 16421s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16422{
16423 expressionS ex;
aa6975fb
ILT
16424 int reg;
16425 int in_shared;
252b5132 16426
919731af 16427 file_mips_check_options ();
16428
6478892d
TS
16429 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16430 .cpload is ignored. */
16431 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16432 {
16433 s_ignore (0);
16434 return;
16435 }
16436
a276b80c
MR
16437 if (mips_opts.mips16)
16438 {
16439 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16440 ignore_rest_of_line ();
16441 return;
16442 }
16443
d3ecfc59 16444 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16445 if (mips_opts.noreorder == 0)
16446 as_warn (_(".cpload not in noreorder section"));
16447
aa6975fb
ILT
16448 reg = tc_get_register (0);
16449
16450 /* If we need to produce a 64-bit address, we are better off using
16451 the default instruction sequence. */
aed1a261 16452 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16453
252b5132 16454 ex.X_op = O_symbol;
bbe506e8
TS
16455 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16456 "__gnu_local_gp");
252b5132
RH
16457 ex.X_op_symbol = NULL;
16458 ex.X_add_number = 0;
16459
16460 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16461 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16462
8a75745d
MR
16463 mips_mark_labels ();
16464 mips_assembling_insn = TRUE;
16465
584892a6 16466 macro_start ();
67c0d1eb
RS
16467 macro_build_lui (&ex, mips_gp_register);
16468 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16469 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16470 if (in_shared)
16471 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16472 mips_gp_register, reg);
584892a6 16473 macro_end ();
252b5132 16474
8a75745d 16475 mips_assembling_insn = FALSE;
252b5132
RH
16476 demand_empty_rest_of_line ();
16477}
16478
6478892d
TS
16479/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16480 .cpsetup $reg1, offset|$reg2, label
16481
16482 If offset is given, this results in:
16483 sd $gp, offset($sp)
956cd1d6 16484 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16485 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16486 daddu $gp, $gp, $reg1
6478892d
TS
16487
16488 If $reg2 is given, this results in:
40fc1451 16489 or $reg2, $gp, $0
956cd1d6 16490 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16491 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16492 daddu $gp, $gp, $reg1
aa6975fb
ILT
16493 $reg1 is normally $25 == $t9.
16494
16495 The -mno-shared option replaces the last three instructions with
16496 lui $gp,%hi(_gp)
54f4ddb3 16497 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16498
6478892d 16499static void
17a2f251 16500s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16501{
16502 expressionS ex_off;
16503 expressionS ex_sym;
16504 int reg1;
6478892d 16505
919731af 16506 file_mips_check_options ();
16507
8586fc66 16508 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16509 We also need NewABI support. */
16510 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16511 {
16512 s_ignore (0);
16513 return;
16514 }
16515
a276b80c
MR
16516 if (mips_opts.mips16)
16517 {
16518 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16519 ignore_rest_of_line ();
16520 return;
16521 }
16522
6478892d
TS
16523 reg1 = tc_get_register (0);
16524 SKIP_WHITESPACE ();
16525 if (*input_line_pointer != ',')
16526 {
16527 as_bad (_("missing argument separator ',' for .cpsetup"));
16528 return;
16529 }
16530 else
80245285 16531 ++input_line_pointer;
6478892d
TS
16532 SKIP_WHITESPACE ();
16533 if (*input_line_pointer == '$')
80245285
TS
16534 {
16535 mips_cpreturn_register = tc_get_register (0);
16536 mips_cpreturn_offset = -1;
16537 }
6478892d 16538 else
80245285
TS
16539 {
16540 mips_cpreturn_offset = get_absolute_expression ();
16541 mips_cpreturn_register = -1;
16542 }
6478892d
TS
16543 SKIP_WHITESPACE ();
16544 if (*input_line_pointer != ',')
16545 {
16546 as_bad (_("missing argument separator ',' for .cpsetup"));
16547 return;
16548 }
16549 else
f9419b05 16550 ++input_line_pointer;
6478892d 16551 SKIP_WHITESPACE ();
f21f8242 16552 expression (&ex_sym);
6478892d 16553
8a75745d
MR
16554 mips_mark_labels ();
16555 mips_assembling_insn = TRUE;
16556
584892a6 16557 macro_start ();
6478892d
TS
16558 if (mips_cpreturn_register == -1)
16559 {
16560 ex_off.X_op = O_constant;
16561 ex_off.X_add_symbol = NULL;
16562 ex_off.X_op_symbol = NULL;
16563 ex_off.X_add_number = mips_cpreturn_offset;
16564
67c0d1eb 16565 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16566 BFD_RELOC_LO16, SP);
6478892d
TS
16567 }
16568 else
40fc1451 16569 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16570
aed1a261 16571 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16572 {
df58fc94 16573 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16574 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16575 BFD_RELOC_HI16_S);
16576
16577 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16578 mips_gp_register, -1, BFD_RELOC_GPREL16,
16579 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16580
16581 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16582 mips_gp_register, reg1);
16583 }
16584 else
16585 {
16586 expressionS ex;
16587
16588 ex.X_op = O_symbol;
4184909a 16589 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16590 ex.X_op_symbol = NULL;
16591 ex.X_add_number = 0;
6e1304d8 16592
aa6975fb
ILT
16593 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16594 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16595
16596 macro_build_lui (&ex, mips_gp_register);
16597 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16598 mips_gp_register, BFD_RELOC_LO16);
16599 }
f21f8242 16600
584892a6 16601 macro_end ();
6478892d 16602
8a75745d 16603 mips_assembling_insn = FALSE;
6478892d
TS
16604 demand_empty_rest_of_line ();
16605}
16606
16607static void
17a2f251 16608s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16609{
919731af 16610 file_mips_check_options ();
16611
6478892d 16612 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16613 .cplocal is ignored. */
6478892d
TS
16614 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16615 {
16616 s_ignore (0);
16617 return;
16618 }
16619
a276b80c
MR
16620 if (mips_opts.mips16)
16621 {
16622 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16623 ignore_rest_of_line ();
16624 return;
16625 }
16626
6478892d 16627 mips_gp_register = tc_get_register (0);
85b51719 16628 demand_empty_rest_of_line ();
6478892d
TS
16629}
16630
252b5132
RH
16631/* Handle the .cprestore pseudo-op. This stores $gp into a given
16632 offset from $sp. The offset is remembered, and after making a PIC
16633 call $gp is restored from that location. */
16634
16635static void
17a2f251 16636s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16637{
16638 expressionS ex;
252b5132 16639
919731af 16640 file_mips_check_options ();
16641
6478892d 16642 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16643 .cprestore is ignored. */
6478892d 16644 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16645 {
16646 s_ignore (0);
16647 return;
16648 }
16649
a276b80c
MR
16650 if (mips_opts.mips16)
16651 {
16652 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16653 ignore_rest_of_line ();
16654 return;
16655 }
16656
252b5132 16657 mips_cprestore_offset = get_absolute_expression ();
7a621144 16658 mips_cprestore_valid = 1;
252b5132
RH
16659
16660 ex.X_op = O_constant;
16661 ex.X_add_symbol = NULL;
16662 ex.X_op_symbol = NULL;
16663 ex.X_add_number = mips_cprestore_offset;
16664
8a75745d
MR
16665 mips_mark_labels ();
16666 mips_assembling_insn = TRUE;
16667
584892a6 16668 macro_start ();
67c0d1eb
RS
16669 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16670 SP, HAVE_64BIT_ADDRESSES);
584892a6 16671 macro_end ();
252b5132 16672
8a75745d 16673 mips_assembling_insn = FALSE;
252b5132
RH
16674 demand_empty_rest_of_line ();
16675}
16676
6478892d 16677/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16678 was given in the preceding .cpsetup, it results in:
6478892d 16679 ld $gp, offset($sp)
76b3015f 16680
6478892d 16681 If a register $reg2 was given there, it results in:
40fc1451 16682 or $gp, $reg2, $0 */
54f4ddb3 16683
6478892d 16684static void
17a2f251 16685s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16686{
16687 expressionS ex;
6478892d 16688
919731af 16689 file_mips_check_options ();
16690
6478892d
TS
16691 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16692 We also need NewABI support. */
16693 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16694 {
16695 s_ignore (0);
16696 return;
16697 }
16698
a276b80c
MR
16699 if (mips_opts.mips16)
16700 {
16701 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16702 ignore_rest_of_line ();
16703 return;
16704 }
16705
8a75745d
MR
16706 mips_mark_labels ();
16707 mips_assembling_insn = TRUE;
16708
584892a6 16709 macro_start ();
6478892d
TS
16710 if (mips_cpreturn_register == -1)
16711 {
16712 ex.X_op = O_constant;
16713 ex.X_add_symbol = NULL;
16714 ex.X_op_symbol = NULL;
16715 ex.X_add_number = mips_cpreturn_offset;
16716
67c0d1eb 16717 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16718 }
16719 else
40fc1451
SD
16720 move_register (mips_gp_register, mips_cpreturn_register);
16721
584892a6 16722 macro_end ();
6478892d 16723
8a75745d 16724 mips_assembling_insn = FALSE;
6478892d
TS
16725 demand_empty_rest_of_line ();
16726}
16727
d0f13682
CLT
16728/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16729 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16730 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16731 debug information or MIPS16 TLS. */
741d6ea8
JM
16732
16733static void
d0f13682
CLT
16734s_tls_rel_directive (const size_t bytes, const char *dirstr,
16735 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16736{
16737 expressionS ex;
16738 char *p;
16739
16740 expression (&ex);
16741
16742 if (ex.X_op != O_symbol)
16743 {
1661c76c 16744 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16745 ignore_rest_of_line ();
16746 }
16747
16748 p = frag_more (bytes);
16749 md_number_to_chars (p, 0, bytes);
d0f13682 16750 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16751 demand_empty_rest_of_line ();
de64cffd 16752 mips_clear_insn_labels ();
741d6ea8
JM
16753}
16754
16755/* Handle .dtprelword. */
16756
16757static void
16758s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16759{
d0f13682 16760 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16761}
16762
16763/* Handle .dtpreldword. */
16764
16765static void
16766s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16767{
d0f13682
CLT
16768 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16769}
16770
16771/* Handle .tprelword. */
16772
16773static void
16774s_tprelword (int ignore ATTRIBUTE_UNUSED)
16775{
16776 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16777}
16778
16779/* Handle .tpreldword. */
16780
16781static void
16782s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16783{
16784 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16785}
16786
6478892d
TS
16787/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16788 code. It sets the offset to use in gp_rel relocations. */
16789
16790static void
17a2f251 16791s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16792{
16793 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16794 We also need NewABI support. */
16795 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16796 {
16797 s_ignore (0);
16798 return;
16799 }
16800
def2e0dd 16801 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16802
16803 demand_empty_rest_of_line ();
16804}
16805
252b5132
RH
16806/* Handle the .gpword pseudo-op. This is used when generating PIC
16807 code. It generates a 32 bit GP relative reloc. */
16808
16809static void
17a2f251 16810s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16811{
a8dbcb85
TS
16812 segment_info_type *si;
16813 struct insn_label_list *l;
252b5132
RH
16814 expressionS ex;
16815 char *p;
16816
16817 /* When not generating PIC code, this is treated as .word. */
16818 if (mips_pic != SVR4_PIC)
16819 {
16820 s_cons (2);
16821 return;
16822 }
16823
a8dbcb85
TS
16824 si = seg_info (now_seg);
16825 l = si->label_list;
7d10b47d 16826 mips_emit_delays ();
252b5132 16827 if (auto_align)
462427c4 16828 mips_align (2, 0, l);
252b5132
RH
16829
16830 expression (&ex);
a1facbec 16831 mips_clear_insn_labels ();
252b5132
RH
16832
16833 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16834 {
1661c76c 16835 as_bad (_("unsupported use of .gpword"));
252b5132
RH
16836 ignore_rest_of_line ();
16837 }
16838
16839 p = frag_more (4);
17a2f251 16840 md_number_to_chars (p, 0, 4);
b34976b6 16841 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16842 BFD_RELOC_GPREL32);
252b5132
RH
16843
16844 demand_empty_rest_of_line ();
16845}
16846
10181a0d 16847static void
17a2f251 16848s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16849{
a8dbcb85
TS
16850 segment_info_type *si;
16851 struct insn_label_list *l;
10181a0d
AO
16852 expressionS ex;
16853 char *p;
16854
16855 /* When not generating PIC code, this is treated as .dword. */
16856 if (mips_pic != SVR4_PIC)
16857 {
16858 s_cons (3);
16859 return;
16860 }
16861
a8dbcb85
TS
16862 si = seg_info (now_seg);
16863 l = si->label_list;
7d10b47d 16864 mips_emit_delays ();
10181a0d 16865 if (auto_align)
462427c4 16866 mips_align (3, 0, l);
10181a0d
AO
16867
16868 expression (&ex);
a1facbec 16869 mips_clear_insn_labels ();
10181a0d
AO
16870
16871 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16872 {
1661c76c 16873 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
16874 ignore_rest_of_line ();
16875 }
16876
16877 p = frag_more (8);
17a2f251 16878 md_number_to_chars (p, 0, 8);
a105a300 16879 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 16880 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
16881
16882 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
16883 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16884 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
16885
16886 demand_empty_rest_of_line ();
16887}
16888
a3f278e2
CM
16889/* Handle the .ehword pseudo-op. This is used when generating unwinding
16890 tables. It generates a R_MIPS_EH reloc. */
16891
16892static void
16893s_ehword (int ignore ATTRIBUTE_UNUSED)
16894{
16895 expressionS ex;
16896 char *p;
16897
16898 mips_emit_delays ();
16899
16900 expression (&ex);
16901 mips_clear_insn_labels ();
16902
16903 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16904 {
1661c76c 16905 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
16906 ignore_rest_of_line ();
16907 }
16908
16909 p = frag_more (4);
16910 md_number_to_chars (p, 0, 4);
16911 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 16912 BFD_RELOC_32_PCREL);
a3f278e2
CM
16913
16914 demand_empty_rest_of_line ();
16915}
16916
252b5132
RH
16917/* Handle the .cpadd pseudo-op. This is used when dealing with switch
16918 tables in SVR4 PIC code. */
16919
16920static void
17a2f251 16921s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 16922{
252b5132
RH
16923 int reg;
16924
919731af 16925 file_mips_check_options ();
16926
10181a0d
AO
16927 /* This is ignored when not generating SVR4 PIC code. */
16928 if (mips_pic != SVR4_PIC)
252b5132
RH
16929 {
16930 s_ignore (0);
16931 return;
16932 }
16933
8a75745d
MR
16934 mips_mark_labels ();
16935 mips_assembling_insn = TRUE;
16936
252b5132 16937 /* Add $gp to the register named as an argument. */
584892a6 16938 macro_start ();
252b5132 16939 reg = tc_get_register (0);
67c0d1eb 16940 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 16941 macro_end ();
252b5132 16942
8a75745d 16943 mips_assembling_insn = FALSE;
bdaaa2e1 16944 demand_empty_rest_of_line ();
252b5132
RH
16945}
16946
16947/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 16948 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
16949 such as generating jalx instructions when needed. We also make
16950 them odd for the duration of the assembly, in order to generate the
16951 right sort of code. We will make them even in the adjust_symtab
16952 routine, while leaving them marked. This is convenient for the
16953 debugger and the disassembler. The linker knows to make them odd
16954 again. */
16955
16956static void
17a2f251 16957s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 16958{
7bb01e2d
MR
16959 file_mips_check_options ();
16960 file_ase_mips16 |= mips_opts.mips16;
16961 file_ase_micromips |= mips_opts.micromips;
16962
df58fc94 16963 mips_mark_labels ();
252b5132
RH
16964
16965 demand_empty_rest_of_line ();
16966}
16967
ba92f887
MR
16968/* Handle the .nan pseudo-op. */
16969
16970static void
16971s_nan (int ignore ATTRIBUTE_UNUSED)
16972{
16973 static const char str_legacy[] = "legacy";
16974 static const char str_2008[] = "2008";
16975 size_t i;
16976
16977 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16978
16979 if (i == sizeof (str_2008) - 1
16980 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 16981 mips_nan2008 = 1;
ba92f887
MR
16982 else if (i == sizeof (str_legacy) - 1
16983 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
16984 {
16985 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16986 mips_nan2008 = 0;
16987 else
16988 as_bad (_("`%s' does not support legacy NaN"),
16989 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16990 }
ba92f887 16991 else
1661c76c 16992 as_bad (_("bad .nan directive"));
ba92f887
MR
16993
16994 input_line_pointer += i;
16995 demand_empty_rest_of_line ();
16996}
16997
754e2bb9
RS
16998/* Handle a .stab[snd] directive. Ideally these directives would be
16999 implemented in a transparent way, so that removing them would not
17000 have any effect on the generated instructions. However, s_stab
17001 internally changes the section, so in practice we need to decide
17002 now whether the preceding label marks compressed code. We do not
17003 support changing the compression mode of a label after a .stab*
17004 directive, such as in:
17005
17006 foo:
134c0c8b 17007 .stabs ...
754e2bb9
RS
17008 .set mips16
17009
17010 so the current mode wins. */
252b5132
RH
17011
17012static void
17a2f251 17013s_mips_stab (int type)
252b5132 17014{
754e2bb9 17015 mips_mark_labels ();
252b5132
RH
17016 s_stab (type);
17017}
17018
54f4ddb3 17019/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17020
17021static void
17a2f251 17022s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17023{
17024 char *name;
17025 int c;
17026 symbolS *symbolP;
17027 expressionS exp;
17028
d02603dc 17029 c = get_symbol_name (&name);
252b5132
RH
17030 symbolP = symbol_find_or_make (name);
17031 S_SET_WEAK (symbolP);
17032 *input_line_pointer = c;
17033
d02603dc 17034 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17035
17036 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17037 {
17038 if (S_IS_DEFINED (symbolP))
17039 {
20203fb9 17040 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17041 S_GET_NAME (symbolP));
17042 ignore_rest_of_line ();
17043 return;
17044 }
bdaaa2e1 17045
252b5132
RH
17046 if (*input_line_pointer == ',')
17047 {
17048 ++input_line_pointer;
17049 SKIP_WHITESPACE ();
17050 }
bdaaa2e1 17051
252b5132
RH
17052 expression (&exp);
17053 if (exp.X_op != O_symbol)
17054 {
20203fb9 17055 as_bad (_("bad .weakext directive"));
98d3f06f 17056 ignore_rest_of_line ();
252b5132
RH
17057 return;
17058 }
49309057 17059 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17060 }
17061
17062 demand_empty_rest_of_line ();
17063}
17064
17065/* Parse a register string into a number. Called from the ECOFF code
17066 to parse .frame. The argument is non-zero if this is the frame
17067 register, so that we can record it in mips_frame_reg. */
17068
17069int
17a2f251 17070tc_get_register (int frame)
252b5132 17071{
707bfff6 17072 unsigned int reg;
252b5132
RH
17073
17074 SKIP_WHITESPACE ();
707bfff6
TS
17075 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17076 reg = 0;
252b5132 17077 if (frame)
7a621144
DJ
17078 {
17079 mips_frame_reg = reg != 0 ? reg : SP;
17080 mips_frame_reg_valid = 1;
17081 mips_cprestore_valid = 0;
17082 }
252b5132
RH
17083 return reg;
17084}
17085
17086valueT
17a2f251 17087md_section_align (asection *seg, valueT addr)
252b5132
RH
17088{
17089 int align = bfd_get_section_alignment (stdoutput, seg);
17090
f3ded42a
RS
17091 /* We don't need to align ELF sections to the full alignment.
17092 However, Irix 5 may prefer that we align them at least to a 16
17093 byte boundary. We don't bother to align the sections if we
17094 are targeted for an embedded system. */
17095 if (strncmp (TARGET_OS, "elf", 3) == 0)
17096 return addr;
17097 if (align > 4)
17098 align = 4;
252b5132 17099
8d3842cd 17100 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17101}
17102
17103/* Utility routine, called from above as well. If called while the
17104 input file is still being read, it's only an approximation. (For
17105 example, a symbol may later become defined which appeared to be
17106 undefined earlier.) */
17107
17108static int
17a2f251 17109nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17110{
17111 if (sym == 0)
17112 return 0;
17113
4d0d148d 17114 if (g_switch_value > 0)
252b5132
RH
17115 {
17116 const char *symname;
17117 int change;
17118
c9914766 17119 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17120 register. It can be if it is smaller than the -G size or if
17121 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17122 not be referenced off the $gp, although it appears as though
252b5132
RH
17123 they can. */
17124 symname = S_GET_NAME (sym);
17125 if (symname != (const char *) NULL
17126 && (strcmp (symname, "eprol") == 0
17127 || strcmp (symname, "etext") == 0
17128 || strcmp (symname, "_gp") == 0
17129 || strcmp (symname, "edata") == 0
17130 || strcmp (symname, "_fbss") == 0
17131 || strcmp (symname, "_fdata") == 0
17132 || strcmp (symname, "_ftext") == 0
17133 || strcmp (symname, "end") == 0
17134 || strcmp (symname, "_gp_disp") == 0))
17135 change = 1;
17136 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17137 && (0
17138#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17139 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17140 && (symbol_get_obj (sym)->ecoff_extern_size
17141 <= g_switch_value))
252b5132
RH
17142#endif
17143 /* We must defer this decision until after the whole
17144 file has been read, since there might be a .extern
17145 after the first use of this symbol. */
17146 || (before_relaxing
17147#ifndef NO_ECOFF_DEBUGGING
49309057 17148 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17149#endif
17150 && S_GET_VALUE (sym) == 0)
17151 || (S_GET_VALUE (sym) != 0
17152 && S_GET_VALUE (sym) <= g_switch_value)))
17153 change = 0;
17154 else
17155 {
17156 const char *segname;
17157
17158 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17159 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17160 && strcmp (segname, ".lit4") != 0);
17161 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17162 && strcmp (segname, ".sbss") != 0
17163 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17164 && strncmp (segname, ".sbss.", 6) != 0
17165 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17166 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17167 }
17168 return change;
17169 }
17170 else
c9914766 17171 /* We are not optimizing for the $gp register. */
252b5132
RH
17172 return 1;
17173}
17174
5919d012
RS
17175
17176/* Return true if the given symbol should be considered local for SVR4 PIC. */
17177
17178static bfd_boolean
9e009953 17179pic_need_relax (symbolS *sym)
5919d012
RS
17180{
17181 asection *symsec;
5919d012
RS
17182
17183 /* Handle the case of a symbol equated to another symbol. */
17184 while (symbol_equated_reloc_p (sym))
17185 {
17186 symbolS *n;
17187
5f0fe04b 17188 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17189 n = symbol_get_value_expression (sym)->X_add_symbol;
17190 if (n == sym)
17191 break;
17192 sym = n;
17193 }
17194
df1f3cda
DD
17195 if (symbol_section_p (sym))
17196 return TRUE;
17197
5919d012
RS
17198 symsec = S_GET_SEGMENT (sym);
17199
5919d012 17200 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17201 return (!bfd_is_und_section (symsec)
17202 && !bfd_is_abs_section (symsec)
5f0fe04b 17203 && !bfd_is_com_section (symsec)
5919d012 17204 /* A global or weak symbol is treated as external. */
f3ded42a 17205 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17206}
14f72d45
MR
17207\f
17208/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17209 convert a section-relative value VAL to the equivalent PC-relative
17210 value. */
17211
17212static offsetT
17213mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17214 offsetT val, long stretch)
17215{
17216 fragS *sym_frag;
17217 addressT addr;
17218
17219 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17220
17221 sym_frag = symbol_get_frag (fragp->fr_symbol);
17222
17223 /* If the relax_marker of the symbol fragment differs from the
17224 relax_marker of this fragment, we have not yet adjusted the
17225 symbol fragment fr_address. We want to add in STRETCH in
17226 order to get a better estimate of the address. This
17227 particularly matters because of the shift bits. */
17228 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17229 {
17230 fragS *f;
17231
17232 /* Adjust stretch for any alignment frag. Note that if have
17233 been expanding the earlier code, the symbol may be
17234 defined in what appears to be an earlier frag. FIXME:
17235 This doesn't handle the fr_subtype field, which specifies
17236 a maximum number of bytes to skip when doing an
17237 alignment. */
17238 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17239 {
17240 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17241 {
17242 if (stretch < 0)
17243 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17244 else
17245 stretch &= ~((1 << (int) f->fr_offset) - 1);
17246 if (stretch == 0)
17247 break;
17248 }
17249 }
17250 if (f != NULL)
17251 val += stretch;
17252 }
17253
17254 addr = fragp->fr_address + fragp->fr_fix;
17255
17256 /* The base address rules are complicated. The base address of
17257 a branch is the following instruction. The base address of a
17258 PC relative load or add is the instruction itself, but if it
17259 is in a delay slot (in which case it can not be extended) use
17260 the address of the instruction whose delay slot it is in. */
17261 if (pcrel_op->include_isa_bit)
17262 {
17263 addr += 2;
17264
17265 /* If we are currently assuming that this frag should be
17266 extended, then the current address is two bytes higher. */
17267 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17268 addr += 2;
17269
17270 /* Ignore the low bit in the target, since it will be set
17271 for a text label. */
17272 val &= -2;
17273 }
17274 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17275 addr -= 4;
17276 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17277 addr -= 2;
5919d012 17278
14f72d45
MR
17279 val -= addr & -(1 << pcrel_op->align_log2);
17280
17281 return val;
17282}
5919d012 17283
252b5132
RH
17284/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17285 extended opcode. SEC is the section the frag is in. */
17286
17287static int
17a2f251 17288mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17289{
3ccad066 17290 const struct mips_int_operand *operand;
252b5132 17291 offsetT val;
252b5132 17292 segT symsec;
14f72d45 17293 int type;
252b5132
RH
17294
17295 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17296 return 0;
17297 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17298 return 1;
17299
88a7ef16 17300 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17301 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17302 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17303 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17304 || (operand->root.type == OP_PCREL
17305 ? sec != symsec
17306 : !bfd_is_abs_section (symsec)))
17307 return 1;
252b5132 17308
88a7ef16 17309 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17310
3ccad066 17311 if (operand->root.type == OP_PCREL)
252b5132 17312 {
3ccad066 17313 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17314 offsetT maxtiny;
252b5132 17315
1425c41d 17316 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17317 return 1;
252b5132 17318
88a7ef16 17319 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17320 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17321
17322 /* If any of the shifted bits are set, we must use an extended
17323 opcode. If the address depends on the size of this
17324 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17325 use an extended opcode. */
17326 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17327 {
17328 fragp->fr_subtype =
1425c41d 17329 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17330 return 1;
17331 }
17332
17333 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17334 is precisely the next value above maxtiny, then there is a
17335 chance of an infinite loop as in the following code:
252b5132
RH
17336 la $4,foo
17337 .skip 1020
17338 .align 2
17339 foo:
17340 In this case when the la is extended, foo is 0x3fc bytes
17341 away, so the la can be shrunk, but then foo is 0x400 away, so
17342 the la must be extended. To avoid this loop, we mark the
17343 frag as extended if it was small, and is about to become
3ccad066
RS
17344 extended with the next value above maxtiny. */
17345 maxtiny = mips_int_operand_max (operand);
17346 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17347 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17348 {
17349 fragp->fr_subtype =
1425c41d 17350 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17351 return 1;
17352 }
17353 }
252b5132 17354
3ccad066 17355 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17356}
17357
8507b6e7
MR
17358/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17359 macro expansion. SEC is the section the frag is in. We only
17360 support PC-relative instructions (LA, DLA, LW, LD) here, in
17361 non-PIC code using 32-bit addressing. */
17362
17363static int
17364mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17365{
17366 const struct mips_pcrel_operand *pcrel_op;
17367 const struct mips_int_operand *operand;
17368 offsetT val;
17369 segT symsec;
17370 int type;
17371
17372 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17373
17374 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17375 return 0;
17376 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17377 return 0;
17378
17379 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17380 switch (type)
17381 {
17382 case 'A':
17383 case 'B':
17384 case 'E':
17385 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17386 if (bfd_is_abs_section (symsec))
17387 return 1;
17388 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17389 return 0;
17390 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17391 return 1;
17392
17393 operand = mips16_immed_operand (type, TRUE);
17394 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17395 pcrel_op = (const struct mips_pcrel_operand *) operand;
17396 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17397
17398 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17399
17400 default:
17401 return 0;
17402 }
17403}
17404
4a6a3df4
AO
17405/* Compute the length of a branch sequence, and adjust the
17406 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17407 worst-case length is computed, with UPDATE being used to indicate
17408 whether an unconditional (-1), branch-likely (+1) or regular (0)
17409 branch is to be computed. */
17410static int
17a2f251 17411relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17412{
b34976b6 17413 bfd_boolean toofar;
4a6a3df4
AO
17414 int length;
17415
17416 if (fragp
17417 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17418 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17419 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17420 {
17421 addressT addr;
17422 offsetT val;
17423
17424 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17425
17426 addr = fragp->fr_address + fragp->fr_fix + 4;
17427
17428 val -= addr;
17429
17430 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17431 }
4a6a3df4 17432 else
c1f61bd2
MR
17433 /* If the symbol is not defined or it's in a different segment,
17434 we emit the long sequence. */
b34976b6 17435 toofar = TRUE;
4a6a3df4
AO
17436
17437 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17438 fragp->fr_subtype
66b3e8da 17439 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17440 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17441 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17442 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17443 RELAX_BRANCH_LINK (fragp->fr_subtype),
17444 toofar);
17445
17446 length = 4;
17447 if (toofar)
17448 {
17449 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17450 length += 8;
17451
ce8ad872 17452 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17453 {
17454 /* Additional space for PIC loading of target address. */
17455 length += 8;
17456 if (mips_opts.isa == ISA_MIPS1)
17457 /* Additional space for $at-stabilizing nop. */
17458 length += 4;
17459 }
17460
17461 /* If branch is conditional. */
17462 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17463 length += 8;
17464 }
b34976b6 17465
4a6a3df4
AO
17466 return length;
17467}
17468
7bd374a4
MR
17469/* Get a FRAG's branch instruction delay slot size, either from the
17470 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17471 or SHORT_INSN_SIZE otherwise. */
17472
17473static int
17474frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17475{
17476 char *buf = fragp->fr_literal + fragp->fr_fix;
17477
17478 if (al)
17479 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17480 else
17481 return short_insn_size;
17482}
17483
df58fc94
RS
17484/* Compute the length of a branch sequence, and adjust the
17485 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17486 worst-case length is computed, with UPDATE being used to indicate
17487 whether an unconditional (-1), or regular (0) branch is to be
17488 computed. */
17489
17490static int
17491relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17492{
7bd374a4
MR
17493 bfd_boolean insn32 = TRUE;
17494 bfd_boolean nods = TRUE;
ce8ad872 17495 bfd_boolean pic = TRUE;
7bd374a4
MR
17496 bfd_boolean al = TRUE;
17497 int short_insn_size;
df58fc94
RS
17498 bfd_boolean toofar;
17499 int length;
17500
7bd374a4
MR
17501 if (fragp)
17502 {
17503 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17504 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17505 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17506 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17507 }
17508 short_insn_size = insn32 ? 4 : 2;
17509
df58fc94
RS
17510 if (fragp
17511 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17512 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17513 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17514 {
17515 addressT addr;
17516 offsetT val;
17517
17518 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17519 /* Ignore the low bit in the target, since it will be set
17520 for a text label. */
17521 if ((val & 1) != 0)
17522 --val;
17523
17524 addr = fragp->fr_address + fragp->fr_fix + 4;
17525
17526 val -= addr;
17527
17528 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17529 }
df58fc94 17530 else
c1f61bd2
MR
17531 /* If the symbol is not defined or it's in a different segment,
17532 we emit the long sequence. */
df58fc94
RS
17533 toofar = TRUE;
17534
17535 if (fragp && update
17536 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17537 fragp->fr_subtype = (toofar
17538 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17539 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17540
17541 length = 4;
17542 if (toofar)
17543 {
17544 bfd_boolean compact_known = fragp != NULL;
17545 bfd_boolean compact = FALSE;
17546 bfd_boolean uncond;
17547
df58fc94 17548 if (fragp)
8484fb75
MR
17549 {
17550 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17551 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17552 }
df58fc94
RS
17553 else
17554 uncond = update < 0;
17555
17556 /* If label is out of range, we turn branch <br>:
17557
17558 <br> label # 4 bytes
17559 0:
17560
17561 into:
17562
17563 j label # 4 bytes
8484fb75
MR
17564 nop # 2/4 bytes if
17565 # compact && (!PIC || insn32)
df58fc94
RS
17566 0:
17567 */
ce8ad872 17568 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17569 length += short_insn_size;
df58fc94
RS
17570
17571 /* If assembling PIC code, we further turn:
17572
17573 j label # 4 bytes
17574
17575 into:
17576
17577 lw/ld at, %got(label)(gp) # 4 bytes
17578 d/addiu at, %lo(label) # 4 bytes
8484fb75 17579 jr/c at # 2/4 bytes
df58fc94 17580 */
ce8ad872 17581 if (pic)
8484fb75 17582 length += 4 + short_insn_size;
df58fc94 17583
7bd374a4
MR
17584 /* Add an extra nop if the jump has no compact form and we need
17585 to fill the delay slot. */
ce8ad872 17586 if ((!pic || al) && nods)
7bd374a4
MR
17587 length += (fragp
17588 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17589 : short_insn_size);
17590
df58fc94
RS
17591 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17592
17593 <brneg> 0f # 4 bytes
8484fb75 17594 nop # 2/4 bytes if !compact
df58fc94
RS
17595 */
17596 if (!uncond)
8484fb75 17597 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17598 }
7bd374a4
MR
17599 else if (nods)
17600 {
17601 /* Add an extra nop to fill the delay slot. */
17602 gas_assert (fragp);
17603 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17604 }
df58fc94
RS
17605
17606 return length;
17607}
17608
17609/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17610 bit accordingly. */
17611
17612static int
17613relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17614{
17615 bfd_boolean toofar;
17616
df58fc94
RS
17617 if (fragp
17618 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17619 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17620 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17621 {
17622 addressT addr;
17623 offsetT val;
17624 int type;
17625
17626 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17627 /* Ignore the low bit in the target, since it will be set
17628 for a text label. */
17629 if ((val & 1) != 0)
17630 --val;
17631
17632 /* Assume this is a 2-byte branch. */
17633 addr = fragp->fr_address + fragp->fr_fix + 2;
17634
17635 /* We try to avoid the infinite loop by not adding 2 more bytes for
17636 long branches. */
17637
17638 val -= addr;
17639
17640 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17641 if (type == 'D')
17642 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17643 else if (type == 'E')
17644 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17645 else
17646 abort ();
17647 }
17648 else
17649 /* If the symbol is not defined or it's in a different segment,
17650 we emit a normal 32-bit branch. */
17651 toofar = TRUE;
17652
17653 if (fragp && update
17654 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17655 fragp->fr_subtype
17656 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17657 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17658
17659 if (toofar)
17660 return 4;
17661
17662 return 2;
17663}
17664
252b5132
RH
17665/* Estimate the size of a frag before relaxing. Unless this is the
17666 mips16, we are not really relaxing here, and the final size is
17667 encoded in the subtype information. For the mips16, we have to
17668 decide whether we are using an extended opcode or not. */
17669
252b5132 17670int
17a2f251 17671md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17672{
5919d012 17673 int change;
252b5132 17674
4a6a3df4
AO
17675 if (RELAX_BRANCH_P (fragp->fr_subtype))
17676 {
17677
b34976b6
AM
17678 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17679
4a6a3df4
AO
17680 return fragp->fr_var;
17681 }
17682
252b5132 17683 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
17684 {
17685 /* We don't want to modify the EXTENDED bit here; it might get us
17686 into infinite loops. We change it only in mips_relax_frag(). */
17687 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17688 return 12;
17689 else
17690 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17691 }
252b5132 17692
df58fc94
RS
17693 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17694 {
17695 int length = 4;
17696
17697 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17698 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17699 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17700 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17701 fragp->fr_var = length;
17702
17703 return length;
17704 }
17705
ce8ad872 17706 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17707 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17708 change = 0;
ce8ad872
MR
17709 else if (RELAX_PIC (fragp->fr_subtype))
17710 change = pic_need_relax (fragp->fr_symbol);
252b5132 17711 else
ce8ad872 17712 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17713
17714 if (change)
17715 {
4d7206a2 17716 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17717 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17718 }
4d7206a2
RS
17719 else
17720 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17721}
17722
17723/* This is called to see whether a reloc against a defined symbol
de7e6852 17724 should be converted into a reloc against a section. */
252b5132
RH
17725
17726int
17a2f251 17727mips_fix_adjustable (fixS *fixp)
252b5132 17728{
252b5132
RH
17729 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17730 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17731 return 0;
a161fe53 17732
252b5132
RH
17733 if (fixp->fx_addsy == NULL)
17734 return 1;
a161fe53 17735
2f0c68f2
CM
17736 /* Allow relocs used for EH tables. */
17737 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17738 return 1;
17739
de7e6852
RS
17740 /* If symbol SYM is in a mergeable section, relocations of the form
17741 SYM + 0 can usually be made section-relative. The mergeable data
17742 is then identified by the section offset rather than by the symbol.
17743
17744 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17745 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17746 need to recalculate the complete offset in order to correctly identify
17747 the merge data.
17748
33eaf5de 17749 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17750 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17751 placed anywhere. Rather than break backwards compatibility by changing
17752 this, it seems better not to force the issue, and instead keep the
17753 original symbol. This will work with either linker behavior. */
738e5348 17754 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17755 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17756 && HAVE_IN_PLACE_ADDENDS
17757 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17758 return 0;
17759
97f50151
MR
17760 /* There is no place to store an in-place offset for JALR relocations. */
17761 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17762 return 0;
17763
17764 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17765 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17766 start address of the symbol's containing section.
17767
17768 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17769 section relative to allow linker relaxations to be performed later on. */
97f50151 17770 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17771 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17772 return 0;
17773
b314ec0e
RS
17774 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17775 to a floating-point stub. The same is true for non-R_MIPS16_26
17776 relocations against MIPS16 functions; in this case, the stub becomes
17777 the function's canonical address.
17778
17779 Floating-point stubs are stored in unique .mips16.call.* or
17780 .mips16.fn.* sections. If a stub T for function F is in section S,
17781 the first relocation in section S must be against F; this is how the
17782 linker determines the target function. All relocations that might
17783 resolve to T must also be against F. We therefore have the following
17784 restrictions, which are given in an intentionally-redundant way:
17785
17786 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17787 symbols.
17788
17789 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17790 if that stub might be used.
17791
17792 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17793 symbols.
17794
17795 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17796 that stub might be used.
17797
17798 There is a further restriction:
17799
df58fc94 17800 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 17801 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
17802 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17803 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17804 against MIPS16 or microMIPS symbols because we need to keep the
17805 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
17806 detection and JAL or BAL to JALX instruction conversion in the
17807 linker.
b314ec0e 17808
df58fc94 17809 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 17810 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 17811 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
17812
17813 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17814 relocation against some symbol R, no relocation against R may be
17815 reduced. (Note that this deals with (2) as well as (1) because
17816 relocations against global symbols will never be reduced on ELF
17817 targets.) This approach is a little simpler than trying to detect
17818 stub sections, and gives the "all or nothing" per-symbol consistency
17819 that we have for MIPS16 symbols. */
f3ded42a 17820 if (fixp->fx_subsy == NULL
30c09090 17821 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 17822 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
17823 && (jmp_reloc_p (fixp->fx_r_type)
17824 || b_reloc_p (fixp->fx_r_type)))
44d3da23 17825 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 17826 return 0;
a161fe53 17827
252b5132
RH
17828 return 1;
17829}
17830
17831/* Translate internal representation of relocation info to BFD target
17832 format. */
17833
17834arelent **
17a2f251 17835tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17836{
17837 static arelent *retval[4];
17838 arelent *reloc;
17839 bfd_reloc_code_real_type code;
17840
4b0cff4e 17841 memset (retval, 0, sizeof(retval));
325801bd
TS
17842 reloc = retval[0] = XCNEW (arelent);
17843 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 17844 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17845 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17846
bad36eac
DJ
17847 if (fixp->fx_pcrel)
17848 {
df58fc94 17849 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 17850 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
17851 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17852 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 17853 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
17854 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17855 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17856 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17857 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17858 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17859 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17860 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
17861
17862 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17863 Relocations want only the symbol offset. */
51f6035b
MR
17864 switch (fixp->fx_r_type)
17865 {
17866 case BFD_RELOC_MIPS_18_PCREL_S3:
17867 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17868 break;
17869 default:
17870 reloc->addend = fixp->fx_addnumber + reloc->address;
17871 break;
17872 }
bad36eac 17873 }
17c6c9d9
MR
17874 else if (HAVE_IN_PLACE_ADDENDS
17875 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17876 && (read_compressed_insn (fixp->fx_frag->fr_literal
17877 + fixp->fx_where, 4) >> 26) == 0x3c)
17878 {
17879 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17880 addend accordingly. */
17881 reloc->addend = fixp->fx_addnumber >> 1;
17882 }
bad36eac
DJ
17883 else
17884 reloc->addend = fixp->fx_addnumber;
252b5132 17885
438c16b8
TS
17886 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17887 entry to be used in the relocation's section offset. */
17888 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17889 {
17890 reloc->address = reloc->addend;
17891 reloc->addend = 0;
17892 }
17893
252b5132 17894 code = fixp->fx_r_type;
252b5132 17895
bad36eac 17896 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17897 if (reloc->howto == NULL)
17898 {
17899 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
17900 _("cannot represent %s relocation in this object file"
17901 " format"),
252b5132
RH
17902 bfd_get_reloc_code_name (code));
17903 retval[0] = NULL;
17904 }
17905
17906 return retval;
17907}
17908
17909/* Relax a machine dependent frag. This returns the amount by which
17910 the current size of the frag should change. */
17911
17912int
17a2f251 17913mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17914{
4a6a3df4
AO
17915 if (RELAX_BRANCH_P (fragp->fr_subtype))
17916 {
17917 offsetT old_var = fragp->fr_var;
b34976b6
AM
17918
17919 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17920
17921 return fragp->fr_var - old_var;
17922 }
17923
df58fc94
RS
17924 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17925 {
17926 offsetT old_var = fragp->fr_var;
17927 offsetT new_var = 4;
17928
17929 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17930 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17931 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17932 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17933 fragp->fr_var = new_var;
17934
17935 return new_var - old_var;
17936 }
17937
252b5132
RH
17938 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17939 return 0;
17940
8507b6e7 17941 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 17942 {
8507b6e7
MR
17943 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17944 {
17945 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
17946 return -10;
17947 }
17948 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17949 {
17950 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17951 return -2;
17952 }
17953 else
17954 return 0;
17955 }
17956 else if (!mips16_macro_frag (fragp, sec, stretch))
17957 {
17958 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
17959 {
17960 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
17961 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17962 return -8;
17963 }
17964 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17965 {
17966 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17967 return 2;
17968 }
17969 else
252b5132 17970 return 0;
252b5132
RH
17971 }
17972 else
17973 {
8507b6e7 17974 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 17975 return 0;
8507b6e7
MR
17976 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17977 {
17978 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17979 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
17980 return 8;
17981 }
17982 else
17983 {
17984 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
17985 return 10;
17986 }
252b5132
RH
17987 }
17988
17989 return 0;
17990}
17991
17992/* Convert a machine dependent frag. */
17993
17994void
17a2f251 17995md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17996{
4a6a3df4
AO
17997 if (RELAX_BRANCH_P (fragp->fr_subtype))
17998 {
4d68580a 17999 char *buf;
4a6a3df4
AO
18000 unsigned long insn;
18001 expressionS exp;
18002 fixS *fixp;
b34976b6 18003
4d68580a
RS
18004 buf = fragp->fr_literal + fragp->fr_fix;
18005 insn = read_insn (buf);
b34976b6 18006
4a6a3df4
AO
18007 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18008 {
18009 /* We generate a fixup instead of applying it right now
18010 because, if there are linker relaxations, we're going to
18011 need the relocations. */
18012 exp.X_op = O_symbol;
18013 exp.X_add_symbol = fragp->fr_symbol;
18014 exp.X_add_number = fragp->fr_offset;
18015
4d68580a
RS
18016 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18017 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18018 fixp->fx_file = fragp->fr_file;
18019 fixp->fx_line = fragp->fr_line;
b34976b6 18020
4d68580a 18021 buf = write_insn (buf, insn);
4a6a3df4
AO
18022 }
18023 else
18024 {
18025 int i;
18026
18027 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18028 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18029
18030 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18031 goto uncond;
18032
18033 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18034 {
18035 /* Reverse the branch. */
18036 switch ((insn >> 28) & 0xf)
18037 {
18038 case 4:
56d438b1
CF
18039 if ((insn & 0xff000000) == 0x47000000
18040 || (insn & 0xff600000) == 0x45600000)
18041 {
18042 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18043 reversed by tweaking bit 23. */
18044 insn ^= 0x00800000;
18045 }
18046 else
18047 {
18048 /* bc[0-3][tf]l? instructions can have the condition
18049 reversed by tweaking a single TF bit, and their
18050 opcodes all have 0x4???????. */
18051 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18052 insn ^= 0x00010000;
18053 }
4a6a3df4
AO
18054 break;
18055
18056 case 0:
18057 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18058 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18059 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18060 insn ^= 0x00010000;
18061 break;
b34976b6 18062
4a6a3df4
AO
18063 case 1:
18064 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18065 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18066 insn ^= 0x04000000;
18067 break;
18068
18069 default:
18070 abort ();
18071 }
18072 }
18073
18074 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18075 {
18076 /* Clear the and-link bit. */
9c2799c2 18077 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18078
54f4ddb3
TS
18079 /* bltzal 0x04100000 bgezal 0x04110000
18080 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18081 insn &= ~0x00100000;
18082 }
18083
18084 /* Branch over the branch (if the branch was likely) or the
18085 full jump (not likely case). Compute the offset from the
18086 current instruction to branch to. */
18087 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18088 i = 16;
18089 else
18090 {
18091 /* How many bytes in instructions we've already emitted? */
4d68580a 18092 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18093 /* How many bytes in instructions from here to the end? */
18094 i = fragp->fr_var - i;
18095 }
18096 /* Convert to instruction count. */
18097 i >>= 2;
18098 /* Branch counts from the next instruction. */
b34976b6 18099 i--;
4a6a3df4
AO
18100 insn |= i;
18101 /* Branch over the jump. */
4d68580a 18102 buf = write_insn (buf, insn);
4a6a3df4 18103
54f4ddb3 18104 /* nop */
4d68580a 18105 buf = write_insn (buf, 0);
4a6a3df4
AO
18106
18107 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18108 {
18109 /* beql $0, $0, 2f */
18110 insn = 0x50000000;
18111 /* Compute the PC offset from the current instruction to
18112 the end of the variable frag. */
18113 /* How many bytes in instructions we've already emitted? */
4d68580a 18114 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18115 /* How many bytes in instructions from here to the end? */
18116 i = fragp->fr_var - i;
18117 /* Convert to instruction count. */
18118 i >>= 2;
18119 /* Don't decrement i, because we want to branch over the
18120 delay slot. */
4a6a3df4 18121 insn |= i;
4a6a3df4 18122
4d68580a
RS
18123 buf = write_insn (buf, insn);
18124 buf = write_insn (buf, 0);
4a6a3df4
AO
18125 }
18126
18127 uncond:
ce8ad872 18128 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18129 {
18130 /* j or jal. */
18131 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18132 ? 0x0c000000 : 0x08000000);
18133 exp.X_op = O_symbol;
18134 exp.X_add_symbol = fragp->fr_symbol;
18135 exp.X_add_number = fragp->fr_offset;
18136
4d68580a
RS
18137 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18138 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18139 fixp->fx_file = fragp->fr_file;
18140 fixp->fx_line = fragp->fr_line;
18141
4d68580a 18142 buf = write_insn (buf, insn);
4a6a3df4
AO
18143 }
18144 else
18145 {
66b3e8da
MR
18146 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18147
4a6a3df4 18148 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18149 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18150 insn |= at << OP_SH_RT;
4a6a3df4
AO
18151 exp.X_op = O_symbol;
18152 exp.X_add_symbol = fragp->fr_symbol;
18153 exp.X_add_number = fragp->fr_offset;
18154
18155 if (fragp->fr_offset)
18156 {
18157 exp.X_add_symbol = make_expr_symbol (&exp);
18158 exp.X_add_number = 0;
18159 }
18160
4d68580a
RS
18161 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18162 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18163 fixp->fx_file = fragp->fr_file;
18164 fixp->fx_line = fragp->fr_line;
18165
4d68580a 18166 buf = write_insn (buf, insn);
b34976b6 18167
4a6a3df4 18168 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18169 /* nop */
18170 buf = write_insn (buf, 0);
4a6a3df4
AO
18171
18172 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18173 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18174 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18175
4d68580a
RS
18176 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18177 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18178 fixp->fx_file = fragp->fr_file;
18179 fixp->fx_line = fragp->fr_line;
b34976b6 18180
4d68580a 18181 buf = write_insn (buf, insn);
4a6a3df4
AO
18182
18183 /* j(al)r $at. */
18184 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18185 insn = 0x0000f809;
4a6a3df4 18186 else
66b3e8da
MR
18187 insn = 0x00000008;
18188 insn |= at << OP_SH_RS;
4a6a3df4 18189
4d68580a 18190 buf = write_insn (buf, insn);
4a6a3df4
AO
18191 }
18192 }
18193
4a6a3df4 18194 fragp->fr_fix += fragp->fr_var;
4d68580a 18195 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18196 return;
18197 }
18198
df58fc94
RS
18199 /* Relax microMIPS branches. */
18200 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18201 {
4d68580a 18202 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18203 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18204 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18205 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18206 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18207 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18208 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18209 bfd_boolean short_ds;
df58fc94
RS
18210 unsigned long insn;
18211 expressionS exp;
18212 fixS *fixp;
18213
18214 exp.X_op = O_symbol;
18215 exp.X_add_symbol = fragp->fr_symbol;
18216 exp.X_add_number = fragp->fr_offset;
18217
18218 fragp->fr_fix += fragp->fr_var;
18219
18220 /* Handle 16-bit branches that fit or are forced to fit. */
18221 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18222 {
18223 /* We generate a fixup instead of applying it right now,
18224 because if there is linker relaxation, we're going to
18225 need the relocations. */
18226 if (type == 'D')
4d68580a 18227 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18228 BFD_RELOC_MICROMIPS_10_PCREL_S1);
18229 else if (type == 'E')
4d68580a 18230 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
18231 BFD_RELOC_MICROMIPS_7_PCREL_S1);
18232 else
18233 abort ();
18234
18235 fixp->fx_file = fragp->fr_file;
18236 fixp->fx_line = fragp->fr_line;
18237
18238 /* These relocations can have an addend that won't fit in
18239 2 octets. */
18240 fixp->fx_no_overflow = 1;
18241
18242 return;
18243 }
18244
2309ddf2 18245 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18246 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18247 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18248 {
18249 /* We generate a fixup instead of applying it right now,
18250 because if there is linker relaxation, we're going to
18251 need the relocations. */
4d68580a
RS
18252 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
18253 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18254 fixp->fx_file = fragp->fr_file;
18255 fixp->fx_line = fragp->fr_line;
18256
18257 if (type == 0)
7bd374a4
MR
18258 {
18259 insn = read_compressed_insn (buf, 4);
18260 buf += 4;
18261
18262 if (nods)
18263 {
18264 /* Check the short-delay-slot bit. */
18265 if (!al || (insn & 0x02000000) != 0)
18266 buf = write_compressed_insn (buf, 0x0c00, 2);
18267 else
18268 buf = write_compressed_insn (buf, 0x00000000, 4);
18269 }
18270
18271 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18272 return;
18273 }
df58fc94
RS
18274 }
18275
18276 /* Relax 16-bit branches to 32-bit branches. */
18277 if (type != 0)
18278 {
4d68580a 18279 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18280
18281 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18282 insn = 0x94000000; /* beq */
18283 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18284 {
18285 unsigned long regno;
18286
18287 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18288 regno = micromips_to_32_reg_d_map [regno];
18289 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18290 insn |= regno << MICROMIPSOP_SH_RS;
18291 }
18292 else
18293 abort ();
18294
18295 /* Nothing else to do, just write it out. */
18296 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18297 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18298 {
4d68580a 18299 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18300 if (nods)
18301 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18302 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18303 return;
18304 }
18305 }
18306 else
4d68580a 18307 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18308
18309 /* Relax 32-bit branches to a sequence of instructions. */
18310 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18311 _("relaxed out-of-range branch into a jump"));
df58fc94 18312
2309ddf2 18313 /* Set the short-delay-slot bit. */
7bd374a4 18314 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18315
18316 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18317 {
18318 symbolS *l;
18319
18320 /* Reverse the branch. */
18321 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18322 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18323 insn ^= 0x20000000;
18324 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18325 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18326 || (insn & 0xffe00000) == 0x40800000 /* blez */
18327 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18328 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18329 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18330 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18331 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18332 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18333 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18334 insn ^= 0x00400000;
18335 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18336 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18337 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18338 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18339 insn ^= 0x00200000;
56d438b1
CF
18340 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18341 BNZ.df */
18342 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18343 BNZ.V */
18344 insn ^= 0x00800000;
df58fc94
RS
18345 else
18346 abort ();
18347
18348 if (al)
18349 {
18350 /* Clear the and-link and short-delay-slot bits. */
18351 gas_assert ((insn & 0xfda00000) == 0x40200000);
18352
18353 /* bltzal 0x40200000 bgezal 0x40600000 */
18354 /* bltzals 0x42200000 bgezals 0x42600000 */
18355 insn &= ~0x02200000;
18356 }
18357
18358 /* Make a label at the end for use with the branch. */
18359 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18360 micromips_label_inc ();
f3ded42a 18361 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18362
18363 /* Refer to it. */
4d68580a
RS
18364 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18365 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18366 fixp->fx_file = fragp->fr_file;
18367 fixp->fx_line = fragp->fr_line;
18368
18369 /* Branch over the jump. */
4d68580a 18370 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18371
df58fc94 18372 if (!compact)
8484fb75
MR
18373 {
18374 /* nop */
18375 if (insn32)
18376 buf = write_compressed_insn (buf, 0x00000000, 4);
18377 else
18378 buf = write_compressed_insn (buf, 0x0c00, 2);
18379 }
df58fc94
RS
18380 }
18381
ce8ad872 18382 if (!pic)
df58fc94 18383 {
7bd374a4
MR
18384 unsigned long jal = (short_ds || nods
18385 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18386
df58fc94
RS
18387 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18388 insn = al ? jal : 0xd4000000;
18389
4d68580a
RS
18390 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18391 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18392 fixp->fx_file = fragp->fr_file;
18393 fixp->fx_line = fragp->fr_line;
18394
4d68580a 18395 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18396
7bd374a4 18397 if (compact || nods)
8484fb75
MR
18398 {
18399 /* nop */
18400 if (insn32)
18401 buf = write_compressed_insn (buf, 0x00000000, 4);
18402 else
18403 buf = write_compressed_insn (buf, 0x0c00, 2);
18404 }
df58fc94
RS
18405 }
18406 else
18407 {
18408 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18409
18410 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18411 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18412 insn |= at << MICROMIPSOP_SH_RT;
18413
18414 if (exp.X_add_number)
18415 {
18416 exp.X_add_symbol = make_expr_symbol (&exp);
18417 exp.X_add_number = 0;
18418 }
18419
4d68580a
RS
18420 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18421 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18422 fixp->fx_file = fragp->fr_file;
18423 fixp->fx_line = fragp->fr_line;
18424
4d68580a 18425 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18426
18427 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18428 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18429 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18430
4d68580a
RS
18431 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
18432 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18433 fixp->fx_file = fragp->fr_file;
18434 fixp->fx_line = fragp->fr_line;
18435
4d68580a 18436 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18437
8484fb75
MR
18438 if (insn32)
18439 {
18440 /* jr/jalr $at */
18441 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18442 insn |= at << MICROMIPSOP_SH_RS;
18443
18444 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18445
7bd374a4 18446 if (compact || nods)
8484fb75
MR
18447 /* nop */
18448 buf = write_compressed_insn (buf, 0x00000000, 4);
18449 }
18450 else
18451 {
18452 /* jr/jrc/jalr/jalrs $at */
18453 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18454 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18455
18456 insn = al ? jalr : jr;
18457 insn |= at << MICROMIPSOP_SH_MJ;
18458
18459 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18460 if (al && nods)
18461 {
18462 /* nop */
18463 if (short_ds)
18464 buf = write_compressed_insn (buf, 0x0c00, 2);
18465 else
18466 buf = write_compressed_insn (buf, 0x00000000, 4);
18467 }
8484fb75 18468 }
df58fc94
RS
18469 }
18470
4d68580a 18471 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18472 return;
18473 }
18474
252b5132
RH
18475 if (RELAX_MIPS16_P (fragp->fr_subtype))
18476 {
18477 int type;
3ccad066 18478 const struct mips_int_operand *operand;
252b5132 18479 offsetT val;
5c04167a 18480 char *buf;
8507b6e7 18481 unsigned int user_length;
9d862524 18482 bfd_boolean need_reloc;
252b5132 18483 unsigned long insn;
8507b6e7 18484 bfd_boolean mac;
5c04167a 18485 bfd_boolean ext;
88a7ef16 18486 segT symsec;
252b5132
RH
18487
18488 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18489 operand = mips16_immed_operand (type, FALSE);
252b5132 18490
8507b6e7 18491 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18492 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18493 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18494
18495 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18496 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18497 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18498 ? asec != symsec
18499 : !bfd_is_abs_section (symsec)));
18500
8507b6e7 18501 if (operand->root.type == OP_PCREL && !mac)
252b5132 18502 {
3ccad066 18503 const struct mips_pcrel_operand *pcrel_op;
252b5132 18504
3ccad066 18505 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18506
14f72d45 18507 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18508 {
14f72d45
MR
18509 if (!ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
18510 as_bad_where (fragp->fr_file, fragp->fr_line,
18511 _("branch to a symbol in another ISA mode"));
18512 else if ((fragp->fr_offset & 0x1) != 0)
18513 as_bad_where (fragp->fr_file, fragp->fr_line,
18514 _("branch to misaligned address (0x%lx)"),
18515 (long) val);
252b5132 18516 }
252b5132 18517
14f72d45 18518 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18519
18520 /* Make sure the section winds up with the alignment we have
18521 assumed. */
3ccad066
RS
18522 if (operand->shift > 0)
18523 record_alignment (asec, operand->shift);
252b5132
RH
18524 }
18525
8507b6e7
MR
18526 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18527 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18528 {
18529 if (mac)
18530 as_warn_where (fragp->fr_file, fragp->fr_line,
18531 _("macro instruction expanded into multiple "
18532 "instructions in a branch delay slot"));
18533 else if (ext)
18534 as_warn_where (fragp->fr_file, fragp->fr_line,
18535 _("extended instruction in a branch delay slot"));
18536 }
18537 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18538 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18539 _("macro instruction expanded into multiple "
18540 "instructions"));
252b5132 18541
5c04167a 18542 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18543
4d68580a 18544 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18545 if (ext)
18546 insn |= MIPS16_EXTEND;
252b5132 18547
5c04167a
RS
18548 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18549 user_length = 4;
18550 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18551 user_length = 2;
18552 else
18553 user_length = 0;
18554
8507b6e7 18555 if (mac)
c9775dde 18556 {
8507b6e7
MR
18557 unsigned long reg;
18558 unsigned long new;
18559 unsigned long op;
18560
18561 gas_assert (type == 'A' || type == 'B' || type == 'E');
18562 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18563
8507b6e7 18564 if (need_reloc)
c9775dde 18565 {
8507b6e7
MR
18566 fixS *fixp;
18567
18568 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18569
18570 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18571 fragp->fr_symbol, fragp->fr_offset,
18572 FALSE, BFD_RELOC_MIPS16_HI16_S);
18573 fixp->fx_file = fragp->fr_file;
18574 fixp->fx_line = fragp->fr_line;
18575
18576 fixp = fix_new (fragp, buf - fragp->fr_literal + 8, 4,
18577 fragp->fr_symbol, fragp->fr_offset,
18578 FALSE, BFD_RELOC_MIPS16_LO16);
18579 fixp->fx_file = fragp->fr_file;
18580 fixp->fx_line = fragp->fr_line;
18581
18582 val = 0;
18583 }
18584
18585 switch (insn & 0xf800)
18586 {
18587 case 0x0800: /* ADDIU */
18588 reg = (insn >> 8) & 0x7;
18589 op = 0xf0004800 | (reg << 8);
c9775dde 18590 break;
8507b6e7
MR
18591 case 0xb000: /* LW */
18592 reg = (insn >> 8) & 0x7;
18593 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18594 break;
8507b6e7
MR
18595 case 0xf800: /* I64 */
18596 reg = (insn >> 5) & 0x7;
18597 switch (insn & 0x0700)
18598 {
18599 case 0x0400: /* LD */
18600 op = 0xf0003800 | (reg << 8) | (reg << 5);
18601 break;
18602 case 0x0600: /* DADDIU */
18603 op = 0xf000fd00 | (reg << 5);
18604 break;
18605 default:
18606 abort ();
18607 }
18608 break;
18609 default:
18610 abort ();
c9775dde 18611 }
8507b6e7
MR
18612
18613 new = 0xf0006800 | (reg << 8); /* LI */
18614 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18615 buf = write_compressed_insn (buf, new, 4);
18616 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18617 buf = write_compressed_insn (buf, new, 4);
18618 op |= mips16_immed_extend (val, 16);
18619 buf = write_compressed_insn (buf, op, 4);
18620
18621 fragp->fr_fix += 12;
18622 }
18623 else
18624 {
18625 unsigned int length = ext ? 4 : 2;
18626
18627 if (need_reloc)
c9775dde 18628 {
8507b6e7
MR
18629 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
18630 expressionS exp;
18631 fixS *fixp;
c9775dde 18632
8507b6e7
MR
18633 switch (type)
18634 {
18635 case 'p':
18636 case 'q':
18637 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18638 break;
18639 default:
18640 break;
18641 }
18642 if (mac || reloc == BFD_RELOC_NONE)
18643 as_bad_where (fragp->fr_file, fragp->fr_line,
18644 _("unsupported relocation"));
18645 else if (ext)
18646 {
18647 exp.X_op = O_symbol;
18648 exp.X_add_symbol = fragp->fr_symbol;
18649 exp.X_add_number = fragp->fr_offset;
c9775dde 18650
8507b6e7
MR
18651 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
18652 TRUE, reloc);
18653
18654 fixp->fx_file = fragp->fr_file;
18655 fixp->fx_line = fragp->fr_line;
18656 }
18657 else
18658 as_bad_where (fragp->fr_file, fragp->fr_line,
18659 _("invalid unextended operand value"));
c9775dde 18660 }
eefc3365 18661 else
8507b6e7
MR
18662 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18663 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18664
8507b6e7
MR
18665 gas_assert (mips16_opcode_length (insn) == length);
18666 write_compressed_insn (buf, insn, length);
18667 fragp->fr_fix += length;
18668 }
252b5132
RH
18669 }
18670 else
18671 {
df58fc94
RS
18672 relax_substateT subtype = fragp->fr_subtype;
18673 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18674 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18675 int first, second;
18676 fixS *fixp;
252b5132 18677
df58fc94
RS
18678 first = RELAX_FIRST (subtype);
18679 second = RELAX_SECOND (subtype);
4d7206a2 18680 fixp = (fixS *) fragp->fr_opcode;
252b5132 18681
df58fc94
RS
18682 /* If the delay slot chosen does not match the size of the instruction,
18683 then emit a warning. */
18684 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18685 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18686 {
18687 relax_substateT s;
18688 const char *msg;
18689
18690 s = subtype & (RELAX_DELAY_SLOT_16BIT
18691 | RELAX_DELAY_SLOT_SIZE_FIRST
18692 | RELAX_DELAY_SLOT_SIZE_SECOND);
18693 msg = macro_warning (s);
18694 if (msg != NULL)
db9b2be4 18695 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18696 subtype &= ~s;
18697 }
18698
584892a6 18699 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18700 if (use_second == second_longer)
584892a6 18701 {
df58fc94
RS
18702 relax_substateT s;
18703 const char *msg;
18704
18705 s = (subtype
18706 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18707 msg = macro_warning (s);
18708 if (msg != NULL)
db9b2be4 18709 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18710 subtype &= ~s;
584892a6
RS
18711 }
18712
4d7206a2
RS
18713 /* Go through all the fixups for the first sequence. Disable them
18714 (by marking them as done) if we're going to use the second
18715 sequence instead. */
18716 while (fixp
18717 && fixp->fx_frag == fragp
18718 && fixp->fx_where < fragp->fr_fix - second)
18719 {
df58fc94 18720 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18721 fixp->fx_done = 1;
18722 fixp = fixp->fx_next;
18723 }
252b5132 18724
4d7206a2
RS
18725 /* Go through the fixups for the second sequence. Disable them if
18726 we're going to use the first sequence, otherwise adjust their
18727 addresses to account for the relaxation. */
18728 while (fixp && fixp->fx_frag == fragp)
18729 {
df58fc94 18730 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18731 fixp->fx_where -= first;
18732 else
18733 fixp->fx_done = 1;
18734 fixp = fixp->fx_next;
18735 }
18736
18737 /* Now modify the frag contents. */
df58fc94 18738 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18739 {
18740 char *start;
18741
18742 start = fragp->fr_literal + fragp->fr_fix - first - second;
18743 memmove (start, start + first, second);
18744 fragp->fr_fix -= first;
18745 }
18746 else
18747 fragp->fr_fix -= second;
252b5132
RH
18748 }
18749}
18750
252b5132
RH
18751/* This function is called after the relocs have been generated.
18752 We've been storing mips16 text labels as odd. Here we convert them
18753 back to even for the convenience of the debugger. */
18754
18755void
17a2f251 18756mips_frob_file_after_relocs (void)
252b5132
RH
18757{
18758 asymbol **syms;
18759 unsigned int count, i;
18760
252b5132
RH
18761 syms = bfd_get_outsymbols (stdoutput);
18762 count = bfd_get_symcount (stdoutput);
18763 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18764 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18765 && ((*syms)->value & 1) != 0)
18766 {
18767 (*syms)->value &= ~1;
18768 /* If the symbol has an odd size, it was probably computed
18769 incorrectly, so adjust that as well. */
18770 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18771 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18772 }
252b5132
RH
18773}
18774
a1facbec
MR
18775/* This function is called whenever a label is defined, including fake
18776 labels instantiated off the dot special symbol. It is used when
18777 handling branch delays; if a branch has a label, we assume we cannot
18778 move it. This also bumps the value of the symbol by 1 in compressed
18779 code. */
252b5132 18780
e1b47bd5 18781static void
a1facbec 18782mips_record_label (symbolS *sym)
252b5132 18783{
a8dbcb85 18784 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18785 struct insn_label_list *l;
18786
18787 if (free_insn_labels == NULL)
325801bd 18788 l = XNEW (struct insn_label_list);
252b5132
RH
18789 else
18790 {
18791 l = free_insn_labels;
18792 free_insn_labels = l->next;
18793 }
18794
18795 l->label = sym;
a8dbcb85
TS
18796 l->next = si->label_list;
18797 si->label_list = l;
a1facbec 18798}
07a53e5c 18799
a1facbec
MR
18800/* This function is called as tc_frob_label() whenever a label is defined
18801 and adds a DWARF-2 record we only want for true labels. */
18802
18803void
18804mips_define_label (symbolS *sym)
18805{
18806 mips_record_label (sym);
07a53e5c 18807 dwarf2_emit_label (sym);
252b5132 18808}
e1b47bd5
RS
18809
18810/* This function is called by tc_new_dot_label whenever a new dot symbol
18811 is defined. */
18812
18813void
18814mips_add_dot_label (symbolS *sym)
18815{
18816 mips_record_label (sym);
18817 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18818 mips_compressed_mark_label (sym);
18819}
252b5132 18820\f
351cdf24
MF
18821/* Converting ASE flags from internal to .MIPS.abiflags values. */
18822static unsigned int
18823mips_convert_ase_flags (int ase)
18824{
18825 unsigned int ext_ases = 0;
18826
18827 if (ase & ASE_DSP)
18828 ext_ases |= AFL_ASE_DSP;
18829 if (ase & ASE_DSPR2)
18830 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
18831 if (ase & ASE_DSPR3)
18832 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
18833 if (ase & ASE_EVA)
18834 ext_ases |= AFL_ASE_EVA;
18835 if (ase & ASE_MCU)
18836 ext_ases |= AFL_ASE_MCU;
18837 if (ase & ASE_MDMX)
18838 ext_ases |= AFL_ASE_MDMX;
18839 if (ase & ASE_MIPS3D)
18840 ext_ases |= AFL_ASE_MIPS3D;
18841 if (ase & ASE_MT)
18842 ext_ases |= AFL_ASE_MT;
18843 if (ase & ASE_SMARTMIPS)
18844 ext_ases |= AFL_ASE_SMARTMIPS;
18845 if (ase & ASE_VIRT)
18846 ext_ases |= AFL_ASE_VIRT;
18847 if (ase & ASE_MSA)
18848 ext_ases |= AFL_ASE_MSA;
18849 if (ase & ASE_XPA)
18850 ext_ases |= AFL_ASE_XPA;
18851
18852 return ext_ases;
18853}
252b5132
RH
18854/* Some special processing for a MIPS ELF file. */
18855
18856void
17a2f251 18857mips_elf_final_processing (void)
252b5132 18858{
351cdf24
MF
18859 int fpabi;
18860 Elf_Internal_ABIFlags_v0 flags;
18861
18862 flags.version = 0;
18863 flags.isa_rev = 0;
18864 switch (file_mips_opts.isa)
18865 {
18866 case INSN_ISA1:
18867 flags.isa_level = 1;
18868 break;
18869 case INSN_ISA2:
18870 flags.isa_level = 2;
18871 break;
18872 case INSN_ISA3:
18873 flags.isa_level = 3;
18874 break;
18875 case INSN_ISA4:
18876 flags.isa_level = 4;
18877 break;
18878 case INSN_ISA5:
18879 flags.isa_level = 5;
18880 break;
18881 case INSN_ISA32:
18882 flags.isa_level = 32;
18883 flags.isa_rev = 1;
18884 break;
18885 case INSN_ISA32R2:
18886 flags.isa_level = 32;
18887 flags.isa_rev = 2;
18888 break;
18889 case INSN_ISA32R3:
18890 flags.isa_level = 32;
18891 flags.isa_rev = 3;
18892 break;
18893 case INSN_ISA32R5:
18894 flags.isa_level = 32;
18895 flags.isa_rev = 5;
18896 break;
09c14161
MF
18897 case INSN_ISA32R6:
18898 flags.isa_level = 32;
18899 flags.isa_rev = 6;
18900 break;
351cdf24
MF
18901 case INSN_ISA64:
18902 flags.isa_level = 64;
18903 flags.isa_rev = 1;
18904 break;
18905 case INSN_ISA64R2:
18906 flags.isa_level = 64;
18907 flags.isa_rev = 2;
18908 break;
18909 case INSN_ISA64R3:
18910 flags.isa_level = 64;
18911 flags.isa_rev = 3;
18912 break;
18913 case INSN_ISA64R5:
18914 flags.isa_level = 64;
18915 flags.isa_rev = 5;
18916 break;
09c14161
MF
18917 case INSN_ISA64R6:
18918 flags.isa_level = 64;
18919 flags.isa_rev = 6;
18920 break;
351cdf24
MF
18921 }
18922
18923 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18924 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18925 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18926 : (file_mips_opts.fp == 64) ? AFL_REG_64
18927 : AFL_REG_32;
18928 flags.cpr2_size = AFL_REG_NONE;
18929 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18930 Tag_GNU_MIPS_ABI_FP);
18931 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18932 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18933 if (file_ase_mips16)
18934 flags.ases |= AFL_ASE_MIPS16;
18935 if (file_ase_micromips)
18936 flags.ases |= AFL_ASE_MICROMIPS;
18937 flags.flags1 = 0;
18938 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18939 || file_mips_opts.fp == 64)
18940 && file_mips_opts.oddspreg)
18941 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18942 flags.flags2 = 0;
18943
18944 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18945 ((Elf_External_ABIFlags_v0 *)
18946 mips_flags_frag));
18947
252b5132 18948 /* Write out the register information. */
316f5878 18949 if (mips_abi != N64_ABI)
252b5132
RH
18950 {
18951 Elf32_RegInfo s;
18952
18953 s.ri_gprmask = mips_gprmask;
18954 s.ri_cprmask[0] = mips_cprmask[0];
18955 s.ri_cprmask[1] = mips_cprmask[1];
18956 s.ri_cprmask[2] = mips_cprmask[2];
18957 s.ri_cprmask[3] = mips_cprmask[3];
18958 /* The gp_value field is set by the MIPS ELF backend. */
18959
18960 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18961 ((Elf32_External_RegInfo *)
18962 mips_regmask_frag));
18963 }
18964 else
18965 {
18966 Elf64_Internal_RegInfo s;
18967
18968 s.ri_gprmask = mips_gprmask;
18969 s.ri_pad = 0;
18970 s.ri_cprmask[0] = mips_cprmask[0];
18971 s.ri_cprmask[1] = mips_cprmask[1];
18972 s.ri_cprmask[2] = mips_cprmask[2];
18973 s.ri_cprmask[3] = mips_cprmask[3];
18974 /* The gp_value field is set by the MIPS ELF backend. */
18975
18976 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18977 ((Elf64_External_RegInfo *)
18978 mips_regmask_frag));
18979 }
18980
18981 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18982 sort of BFD interface for this. */
18983 if (mips_any_noreorder)
18984 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18985 if (mips_pic != NO_PIC)
143d77c5 18986 {
8b828383 18987 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18988 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18989 }
18990 if (mips_abicalls)
18991 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18992
b015e599
AP
18993 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18994 defined at present; this might need to change in future. */
a4672219
TS
18995 if (file_ase_mips16)
18996 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18997 if (file_ase_micromips)
18998 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 18999 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19000 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19001
bdaaa2e1 19002 /* Set the MIPS ELF ABI flags. */
316f5878 19003 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19004 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19005 else if (mips_abi == O64_ABI)
252b5132 19006 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19007 else if (mips_abi == EABI_ABI)
252b5132 19008 {
bad1aba3 19009 if (file_mips_opts.gp == 64)
252b5132
RH
19010 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19011 else
19012 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19013 }
316f5878 19014 else if (mips_abi == N32_ABI)
be00bddd
TS
19015 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
19016
c9914766 19017 /* Nothing to do for N64_ABI. */
252b5132
RH
19018
19019 if (mips_32bitmode)
19020 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19021
7361da2c 19022 if (mips_nan2008 == 1)
ba92f887
MR
19023 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19024
ad3fea08 19025 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19026 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19027 Tag_GNU_MIPS_ABI_FP);
19028 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19029 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19030}
252b5132 19031\f
beae10d5 19032typedef struct proc {
9b2f1d35
EC
19033 symbolS *func_sym;
19034 symbolS *func_end_sym;
beae10d5
KH
19035 unsigned long reg_mask;
19036 unsigned long reg_offset;
19037 unsigned long fpreg_mask;
19038 unsigned long fpreg_offset;
19039 unsigned long frame_offset;
19040 unsigned long frame_reg;
19041 unsigned long pc_reg;
19042} procS;
252b5132
RH
19043
19044static procS cur_proc;
19045static procS *cur_proc_ptr;
19046static int numprocs;
19047
df58fc94
RS
19048/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19049 as "2", and a normal nop as "0". */
19050
19051#define NOP_OPCODE_MIPS 0
19052#define NOP_OPCODE_MIPS16 1
19053#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19054
19055char
19056mips_nop_opcode (void)
19057{
df58fc94
RS
19058 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19059 return NOP_OPCODE_MICROMIPS;
19060 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19061 return NOP_OPCODE_MIPS16;
19062 else
19063 return NOP_OPCODE_MIPS;
742a56fe
RS
19064}
19065
df58fc94
RS
19066/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19067 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19068
0a9ef439 19069void
17a2f251 19070mips_handle_align (fragS *fragp)
a19d8eb0 19071{
df58fc94 19072 char nop_opcode;
742a56fe 19073 char *p;
c67a084a
NC
19074 int bytes, size, excess;
19075 valueT opcode;
742a56fe 19076
0a9ef439
RH
19077 if (fragp->fr_type != rs_align_code)
19078 return;
19079
742a56fe 19080 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19081 nop_opcode = *p;
19082 switch (nop_opcode)
a19d8eb0 19083 {
df58fc94
RS
19084 case NOP_OPCODE_MICROMIPS:
19085 opcode = micromips_nop32_insn.insn_opcode;
19086 size = 4;
19087 break;
19088 case NOP_OPCODE_MIPS16:
c67a084a
NC
19089 opcode = mips16_nop_insn.insn_opcode;
19090 size = 2;
df58fc94
RS
19091 break;
19092 case NOP_OPCODE_MIPS:
19093 default:
c67a084a
NC
19094 opcode = nop_insn.insn_opcode;
19095 size = 4;
df58fc94 19096 break;
c67a084a 19097 }
a19d8eb0 19098
c67a084a
NC
19099 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19100 excess = bytes % size;
df58fc94
RS
19101
19102 /* Handle the leading part if we're not inserting a whole number of
19103 instructions, and make it the end of the fixed part of the frag.
19104 Try to fit in a short microMIPS NOP if applicable and possible,
19105 and use zeroes otherwise. */
19106 gas_assert (excess < 4);
19107 fragp->fr_fix += excess;
19108 switch (excess)
c67a084a 19109 {
df58fc94
RS
19110 case 3:
19111 *p++ = '\0';
19112 /* Fall through. */
19113 case 2:
833794fc 19114 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19115 {
4d68580a 19116 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19117 break;
19118 }
19119 *p++ = '\0';
19120 /* Fall through. */
19121 case 1:
19122 *p++ = '\0';
19123 /* Fall through. */
19124 case 0:
19125 break;
a19d8eb0 19126 }
c67a084a
NC
19127
19128 md_number_to_chars (p, opcode, size);
19129 fragp->fr_var = size;
a19d8eb0
CP
19130}
19131
252b5132 19132static long
17a2f251 19133get_number (void)
252b5132
RH
19134{
19135 int negative = 0;
19136 long val = 0;
19137
19138 if (*input_line_pointer == '-')
19139 {
19140 ++input_line_pointer;
19141 negative = 1;
19142 }
3882b010 19143 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19144 as_bad (_("expected simple number"));
252b5132
RH
19145 if (input_line_pointer[0] == '0')
19146 {
19147 if (input_line_pointer[1] == 'x')
19148 {
19149 input_line_pointer += 2;
3882b010 19150 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19151 {
19152 val <<= 4;
19153 val |= hex_value (*input_line_pointer++);
19154 }
19155 return negative ? -val : val;
19156 }
19157 else
19158 {
19159 ++input_line_pointer;
3882b010 19160 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19161 {
19162 val <<= 3;
19163 val |= *input_line_pointer++ - '0';
19164 }
19165 return negative ? -val : val;
19166 }
19167 }
3882b010 19168 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19169 {
19170 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19171 *input_line_pointer, *input_line_pointer);
956cd1d6 19172 as_warn (_("invalid number"));
252b5132
RH
19173 return -1;
19174 }
3882b010 19175 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19176 {
19177 val *= 10;
19178 val += *input_line_pointer++ - '0';
19179 }
19180 return negative ? -val : val;
19181}
19182
19183/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19184 is an initial number which is the ECOFF file index. In the non-ECOFF
19185 case .file implies DWARF-2. */
19186
19187static void
17a2f251 19188s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19189{
ecb4347a
DJ
19190 static int first_file_directive = 0;
19191
c5dd6aab
DJ
19192 if (ECOFF_DEBUGGING)
19193 {
19194 get_number ();
19195 s_app_file (0);
19196 }
19197 else
ecb4347a
DJ
19198 {
19199 char *filename;
19200
19201 filename = dwarf2_directive_file (0);
19202
19203 /* Versions of GCC up to 3.1 start files with a ".file"
19204 directive even for stabs output. Make sure that this
19205 ".file" is handled. Note that you need a version of GCC
19206 after 3.1 in order to support DWARF-2 on MIPS. */
19207 if (filename != NULL && ! first_file_directive)
19208 {
19209 (void) new_logical_line (filename, -1);
c04f5787 19210 s_app_file_string (filename, 0);
ecb4347a
DJ
19211 }
19212 first_file_directive = 1;
19213 }
c5dd6aab
DJ
19214}
19215
19216/* The .loc directive, implying DWARF-2. */
252b5132
RH
19217
19218static void
17a2f251 19219s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19220{
c5dd6aab
DJ
19221 if (!ECOFF_DEBUGGING)
19222 dwarf2_directive_loc (0);
252b5132
RH
19223}
19224
252b5132
RH
19225/* The .end directive. */
19226
19227static void
17a2f251 19228s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19229{
19230 symbolS *p;
252b5132 19231
7a621144
DJ
19232 /* Following functions need their own .frame and .cprestore directives. */
19233 mips_frame_reg_valid = 0;
19234 mips_cprestore_valid = 0;
19235
252b5132
RH
19236 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19237 {
19238 p = get_symbol ();
19239 demand_empty_rest_of_line ();
19240 }
19241 else
19242 p = NULL;
19243
14949570 19244 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19245 as_warn (_(".end not in text section"));
19246
19247 if (!cur_proc_ptr)
19248 {
1661c76c 19249 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19250 demand_empty_rest_of_line ();
19251 return;
19252 }
19253
19254 if (p != NULL)
19255 {
9c2799c2 19256 gas_assert (S_GET_NAME (p));
9b2f1d35 19257 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19258 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19259
19260 if (debug_type == DEBUG_STABS)
19261 stabs_generate_asm_endfunc (S_GET_NAME (p),
19262 S_GET_NAME (p));
252b5132
RH
19263 }
19264 else
19265 as_warn (_(".end directive missing or unknown symbol"));
19266
9b2f1d35
EC
19267 /* Create an expression to calculate the size of the function. */
19268 if (p && cur_proc_ptr)
19269 {
19270 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19271 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19272
19273 obj->size = exp;
19274 exp->X_op = O_subtract;
19275 exp->X_add_symbol = symbol_temp_new_now ();
19276 exp->X_op_symbol = p;
19277 exp->X_add_number = 0;
19278
19279 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19280 }
19281
5ff6a06c
MR
19282#ifdef md_flush_pending_output
19283 md_flush_pending_output ();
19284#endif
19285
ecb4347a 19286 /* Generate a .pdr section. */
f3ded42a 19287 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19288 {
19289 segT saved_seg = now_seg;
19290 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19291 expressionS exp;
19292 char *fragp;
252b5132 19293
9c2799c2 19294 gas_assert (pdr_seg);
ecb4347a 19295 subseg_set (pdr_seg, 0);
252b5132 19296
ecb4347a
DJ
19297 /* Write the symbol. */
19298 exp.X_op = O_symbol;
19299 exp.X_add_symbol = p;
19300 exp.X_add_number = 0;
19301 emit_expr (&exp, 4);
252b5132 19302
ecb4347a 19303 fragp = frag_more (7 * 4);
252b5132 19304
17a2f251
TS
19305 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19306 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19307 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19308 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19309 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19310 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19311 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19312
ecb4347a
DJ
19313 subseg_set (saved_seg, saved_subseg);
19314 }
252b5132
RH
19315
19316 cur_proc_ptr = NULL;
19317}
19318
19319/* The .aent and .ent directives. */
19320
19321static void
17a2f251 19322s_mips_ent (int aent)
252b5132 19323{
252b5132 19324 symbolS *symbolP;
252b5132
RH
19325
19326 symbolP = get_symbol ();
19327 if (*input_line_pointer == ',')
f9419b05 19328 ++input_line_pointer;
252b5132 19329 SKIP_WHITESPACE ();
3882b010 19330 if (ISDIGIT (*input_line_pointer)
d9a62219 19331 || *input_line_pointer == '-')
874e8986 19332 get_number ();
252b5132 19333
14949570 19334 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19335 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19336
19337 if (!aent && cur_proc_ptr)
9a41af64 19338 as_warn (_("missing .end"));
252b5132
RH
19339
19340 if (!aent)
19341 {
7a621144
DJ
19342 /* This function needs its own .frame and .cprestore directives. */
19343 mips_frame_reg_valid = 0;
19344 mips_cprestore_valid = 0;
19345
252b5132
RH
19346 cur_proc_ptr = &cur_proc;
19347 memset (cur_proc_ptr, '\0', sizeof (procS));
19348
9b2f1d35 19349 cur_proc_ptr->func_sym = symbolP;
252b5132 19350
f9419b05 19351 ++numprocs;
ecb4347a
DJ
19352
19353 if (debug_type == DEBUG_STABS)
19354 stabs_generate_asm_func (S_GET_NAME (symbolP),
19355 S_GET_NAME (symbolP));
252b5132
RH
19356 }
19357
7c0fc524
MR
19358 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19359
252b5132
RH
19360 demand_empty_rest_of_line ();
19361}
19362
19363/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19364 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19365 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19366 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19367 symbol table (in the mdebug section). */
19368
19369static void
17a2f251 19370s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19371{
f3ded42a
RS
19372 if (ECOFF_DEBUGGING)
19373 s_ignore (ignore);
19374 else
ecb4347a
DJ
19375 {
19376 long val;
252b5132 19377
ecb4347a
DJ
19378 if (cur_proc_ptr == (procS *) NULL)
19379 {
19380 as_warn (_(".frame outside of .ent"));
19381 demand_empty_rest_of_line ();
19382 return;
19383 }
252b5132 19384
ecb4347a
DJ
19385 cur_proc_ptr->frame_reg = tc_get_register (1);
19386
19387 SKIP_WHITESPACE ();
19388 if (*input_line_pointer++ != ','
19389 || get_absolute_expression_and_terminator (&val) != ',')
19390 {
1661c76c 19391 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19392 --input_line_pointer;
19393 demand_empty_rest_of_line ();
19394 return;
19395 }
252b5132 19396
ecb4347a
DJ
19397 cur_proc_ptr->frame_offset = val;
19398 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19399
252b5132 19400 demand_empty_rest_of_line ();
252b5132 19401 }
252b5132
RH
19402}
19403
bdaaa2e1
KH
19404/* The .fmask and .mask directives. If the mdebug section is present
19405 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19406 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19407 information correctly. We can't use the ecoff routines because they
252b5132
RH
19408 make reference to the ecoff symbol table (in the mdebug section). */
19409
19410static void
17a2f251 19411s_mips_mask (int reg_type)
252b5132 19412{
f3ded42a
RS
19413 if (ECOFF_DEBUGGING)
19414 s_ignore (reg_type);
19415 else
252b5132 19416 {
ecb4347a 19417 long mask, off;
252b5132 19418
ecb4347a
DJ
19419 if (cur_proc_ptr == (procS *) NULL)
19420 {
19421 as_warn (_(".mask/.fmask outside of .ent"));
19422 demand_empty_rest_of_line ();
19423 return;
19424 }
252b5132 19425
ecb4347a
DJ
19426 if (get_absolute_expression_and_terminator (&mask) != ',')
19427 {
1661c76c 19428 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19429 --input_line_pointer;
19430 demand_empty_rest_of_line ();
19431 return;
19432 }
252b5132 19433
ecb4347a
DJ
19434 off = get_absolute_expression ();
19435
19436 if (reg_type == 'F')
19437 {
19438 cur_proc_ptr->fpreg_mask = mask;
19439 cur_proc_ptr->fpreg_offset = off;
19440 }
19441 else
19442 {
19443 cur_proc_ptr->reg_mask = mask;
19444 cur_proc_ptr->reg_offset = off;
19445 }
19446
19447 demand_empty_rest_of_line ();
252b5132 19448 }
252b5132
RH
19449}
19450
316f5878
RS
19451/* A table describing all the processors gas knows about. Names are
19452 matched in the order listed.
e7af610e 19453
316f5878
RS
19454 To ease comparison, please keep this table in the same order as
19455 gcc's mips_cpu_info_table[]. */
e972090a
NC
19456static const struct mips_cpu_info mips_cpu_info_table[] =
19457{
316f5878 19458 /* Entries for generic ISAs */
d16afab6
RS
19459 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19460 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19461 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19462 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19463 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19464 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19465 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19466 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19467 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19468 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19469 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19470 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19471 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19472 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19473 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19474
19475 /* MIPS I */
d16afab6
RS
19476 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19477 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19478 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19479
19480 /* MIPS II */
d16afab6 19481 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19482
19483 /* MIPS III */
d16afab6
RS
19484 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19485 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19486 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19487 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19488 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19489 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19490 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19491 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19492 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19493 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19494 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19495 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19496 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19497 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19498 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19499 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19500
19501 /* MIPS IV */
d16afab6
RS
19502 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19503 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19504 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19505 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19506 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19507 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19508 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19509 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19510 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19511 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19512 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19513 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19514 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19515 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19516 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19517
19518 /* MIPS 32 */
d16afab6
RS
19519 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19520 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19521 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19522 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19523
19524 /* MIPS 32 Release 2 */
d16afab6
RS
19525 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19526 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19527 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19528 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19529 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19530 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19531 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19532 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19533 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19534 ISA_MIPS32R2, CPU_MIPS32R2 },
19535 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19536 ISA_MIPS32R2, CPU_MIPS32R2 },
19537 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19538 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19539 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19540 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19541 /* Deprecated forms of the above. */
d16afab6
RS
19542 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19543 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19544 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19545 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19546 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19547 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19548 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19549 /* Deprecated forms of the above. */
d16afab6
RS
19550 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19551 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19552 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19553 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19554 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19555 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19556 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19557 /* Deprecated forms of the above. */
d16afab6
RS
19558 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19559 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19560 /* 34Kn is a 34kc without DSP. */
d16afab6 19561 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19562 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19563 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19564 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19565 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19566 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19567 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19568 /* Deprecated forms of the above. */
d16afab6
RS
19569 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19570 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19571 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19572 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19573 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19574 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19575 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19576 /* interaptiv is the new name for 1004kf */
19577 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
c6e5c03a
RS
19578 /* M5100 family */
19579 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19580 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19581 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19582 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19583
316f5878 19584 /* MIPS 64 */
d16afab6
RS
19585 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19586 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19587 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19588 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19589
c7a23324 19590 /* Broadcom SB-1 CPU core */
d16afab6 19591 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19592 /* Broadcom SB-1A CPU core */
d16afab6 19593 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19594
4ba154f5 19595 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 19596
ed163775
MR
19597 /* MIPS 64 Release 2 */
19598
967344c6 19599 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19600 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19601 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19602 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19603 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19604
52b6b6b9 19605 /* RMI Xlr */
d16afab6 19606 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19607
55a36193
MK
19608 /* Broadcom XLP.
19609 XLP is mostly like XLR, with the prominent exception that it is
19610 MIPS64R2 rather than MIPS64. */
d16afab6 19611 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19612
a4968f42 19613 /* MIPS 64 Release 6 */
7ef0d297 19614 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19615 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19616
316f5878 19617 /* End marker */
d16afab6 19618 { NULL, 0, 0, 0, 0 }
316f5878 19619};
e7af610e 19620
84ea6cf2 19621
316f5878
RS
19622/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19623 with a final "000" replaced by "k". Ignore case.
e7af610e 19624
316f5878 19625 Note: this function is shared between GCC and GAS. */
c6c98b38 19626
b34976b6 19627static bfd_boolean
17a2f251 19628mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19629{
19630 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19631 given++, canonical++;
19632
19633 return ((*given == 0 && *canonical == 0)
19634 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19635}
19636
19637
19638/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19639 CPU name. We've traditionally allowed a lot of variation here.
19640
19641 Note: this function is shared between GCC and GAS. */
19642
b34976b6 19643static bfd_boolean
17a2f251 19644mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19645{
19646 /* First see if the name matches exactly, or with a final "000"
19647 turned into "k". */
19648 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19649 return TRUE;
316f5878
RS
19650
19651 /* If not, try comparing based on numerical designation alone.
19652 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19653 if (TOLOWER (*given) == 'r')
19654 given++;
19655 if (!ISDIGIT (*given))
b34976b6 19656 return FALSE;
316f5878
RS
19657
19658 /* Skip over some well-known prefixes in the canonical name,
19659 hoping to find a number there too. */
19660 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19661 canonical += 2;
19662 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19663 canonical += 2;
19664 else if (TOLOWER (canonical[0]) == 'r')
19665 canonical += 1;
19666
19667 return mips_strict_matching_cpu_name_p (canonical, given);
19668}
19669
19670
19671/* Parse an option that takes the name of a processor as its argument.
19672 OPTION is the name of the option and CPU_STRING is the argument.
19673 Return the corresponding processor enumeration if the CPU_STRING is
19674 recognized, otherwise report an error and return null.
19675
19676 A similar function exists in GCC. */
e7af610e
NC
19677
19678static const struct mips_cpu_info *
17a2f251 19679mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19680{
316f5878 19681 const struct mips_cpu_info *p;
e7af610e 19682
316f5878
RS
19683 /* 'from-abi' selects the most compatible architecture for the given
19684 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19685 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19686 version. Look first at the -mgp options, if given, otherwise base
19687 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19688
316f5878
RS
19689 Treat NO_ABI like the EABIs. One reason to do this is that the
19690 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19691 architecture. This code picks MIPS I for 'mips' and MIPS III for
19692 'mips64', just as we did in the days before 'from-abi'. */
19693 if (strcasecmp (cpu_string, "from-abi") == 0)
19694 {
19695 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19696 return mips_cpu_info_from_isa (ISA_MIPS1);
19697
19698 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19699 return mips_cpu_info_from_isa (ISA_MIPS3);
19700
bad1aba3 19701 if (file_mips_opts.gp >= 0)
19702 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19703 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19704
19705 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19706 ? ISA_MIPS3
19707 : ISA_MIPS1);
19708 }
19709
19710 /* 'default' has traditionally been a no-op. Probably not very useful. */
19711 if (strcasecmp (cpu_string, "default") == 0)
19712 return 0;
19713
19714 for (p = mips_cpu_info_table; p->name != 0; p++)
19715 if (mips_matching_cpu_name_p (p->name, cpu_string))
19716 return p;
19717
1661c76c 19718 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19719 return 0;
e7af610e
NC
19720}
19721
316f5878
RS
19722/* Return the canonical processor information for ISA (a member of the
19723 ISA_MIPS* enumeration). */
19724
e7af610e 19725static const struct mips_cpu_info *
17a2f251 19726mips_cpu_info_from_isa (int isa)
e7af610e
NC
19727{
19728 int i;
19729
19730 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19731 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19732 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19733 return (&mips_cpu_info_table[i]);
19734
e972090a 19735 return NULL;
e7af610e 19736}
fef14a42
TS
19737
19738static const struct mips_cpu_info *
17a2f251 19739mips_cpu_info_from_arch (int arch)
fef14a42
TS
19740{
19741 int i;
19742
19743 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19744 if (arch == mips_cpu_info_table[i].cpu)
19745 return (&mips_cpu_info_table[i]);
19746
19747 return NULL;
19748}
316f5878
RS
19749\f
19750static void
17a2f251 19751show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19752{
19753 if (*first_p)
19754 {
19755 fprintf (stream, "%24s", "");
19756 *col_p = 24;
19757 }
19758 else
19759 {
19760 fprintf (stream, ", ");
19761 *col_p += 2;
19762 }
e7af610e 19763
316f5878
RS
19764 if (*col_p + strlen (string) > 72)
19765 {
19766 fprintf (stream, "\n%24s", "");
19767 *col_p = 24;
19768 }
19769
19770 fprintf (stream, "%s", string);
19771 *col_p += strlen (string);
19772
19773 *first_p = 0;
19774}
19775
19776void
17a2f251 19777md_show_usage (FILE *stream)
e7af610e 19778{
316f5878
RS
19779 int column, first;
19780 size_t i;
19781
19782 fprintf (stream, _("\
19783MIPS options:\n\
316f5878
RS
19784-EB generate big endian output\n\
19785-EL generate little endian output\n\
19786-g, -g2 do not remove unneeded NOPs or swap branches\n\
19787-G NUM allow referencing objects up to NUM bytes\n\
19788 implicitly with the gp register [default 8]\n"));
19789 fprintf (stream, _("\
19790-mips1 generate MIPS ISA I instructions\n\
19791-mips2 generate MIPS ISA II instructions\n\
19792-mips3 generate MIPS ISA III instructions\n\
19793-mips4 generate MIPS ISA IV instructions\n\
19794-mips5 generate MIPS ISA V instructions\n\
19795-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19796-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19797-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19798-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19799-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19800-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19801-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19802-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19803-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19804-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19805-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19806
19807 first = 1;
e7af610e
NC
19808
19809 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19810 show (stream, mips_cpu_info_table[i].name, &column, &first);
19811 show (stream, "from-abi", &column, &first);
19812 fputc ('\n', stream);
e7af610e 19813
316f5878
RS
19814 fprintf (stream, _("\
19815-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19816-no-mCPU don't generate code specific to CPU.\n\
19817 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19818
19819 first = 1;
19820
19821 show (stream, "3900", &column, &first);
19822 show (stream, "4010", &column, &first);
19823 show (stream, "4100", &column, &first);
19824 show (stream, "4650", &column, &first);
19825 fputc ('\n', stream);
19826
19827 fprintf (stream, _("\
19828-mips16 generate mips16 instructions\n\
19829-no-mips16 do not generate mips16 instructions\n"));
19830 fprintf (stream, _("\
df58fc94
RS
19831-mmicromips generate microMIPS instructions\n\
19832-mno-micromips do not generate microMIPS instructions\n"));
19833 fprintf (stream, _("\
e16bfa71 19834-msmartmips generate smartmips instructions\n\
3739860c 19835-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 19836 fprintf (stream, _("\
74cd071d
CF
19837-mdsp generate DSP instructions\n\
19838-mno-dsp do not generate DSP instructions\n"));
19839 fprintf (stream, _("\
8b082fb1
TS
19840-mdspr2 generate DSP R2 instructions\n\
19841-mno-dspr2 do not generate DSP R2 instructions\n"));
19842 fprintf (stream, _("\
8f4f9071
MF
19843-mdspr3 generate DSP R3 instructions\n\
19844-mno-dspr3 do not generate DSP R3 instructions\n"));
19845 fprintf (stream, _("\
ef2e4d86
CF
19846-mmt generate MT instructions\n\
19847-mno-mt do not generate MT instructions\n"));
19848 fprintf (stream, _("\
dec0624d
MR
19849-mmcu generate MCU instructions\n\
19850-mno-mcu do not generate MCU instructions\n"));
19851 fprintf (stream, _("\
56d438b1
CF
19852-mmsa generate MSA instructions\n\
19853-mno-msa do not generate MSA instructions\n"));
19854 fprintf (stream, _("\
7d64c587
AB
19855-mxpa generate eXtended Physical Address (XPA) instructions\n\
19856-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19857 fprintf (stream, _("\
b015e599
AP
19858-mvirt generate Virtualization instructions\n\
19859-mno-virt do not generate Virtualization instructions\n"));
19860 fprintf (stream, _("\
833794fc
MR
19861-minsn32 only generate 32-bit microMIPS instructions\n\
19862-mno-insn32 generate all microMIPS instructions\n"));
19863 fprintf (stream, _("\
c67a084a
NC
19864-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19865-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19866-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19867-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19868-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19869-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19870-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19871-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19872-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19873-O0 remove unneeded NOPs, do not swap branches\n\
19874-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19875--trap, --no-break trap exception on div by 0 and mult overflow\n\
19876--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19877 fprintf (stream, _("\
19878-mhard-float allow floating-point instructions\n\
19879-msoft-float do not allow floating-point instructions\n\
19880-msingle-float only allow 32-bit floating-point operations\n\
19881-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 19882--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 19883--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
19884-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
19885-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
19886-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19887
19888 first = 1;
19889
19890 show (stream, "legacy", &column, &first);
19891 show (stream, "2008", &column, &first);
19892
19893 fputc ('\n', stream);
19894
316f5878
RS
19895 fprintf (stream, _("\
19896-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19897-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19898-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19899-non_shared do not generate code that can operate with DSOs\n\
316f5878 19900-xgot assume a 32 bit GOT\n\
dcd410fe 19901-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19902-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19903 position dependent (non shared) code\n\
316f5878
RS
19904-mabi=ABI create ABI conformant object file for:\n"));
19905
19906 first = 1;
19907
19908 show (stream, "32", &column, &first);
19909 show (stream, "o64", &column, &first);
19910 show (stream, "n32", &column, &first);
19911 show (stream, "64", &column, &first);
19912 show (stream, "eabi", &column, &first);
19913
19914 fputc ('\n', stream);
19915
19916 fprintf (stream, _("\
19917-32 create o32 ABI object file (default)\n\
19918-n32 create n32 ABI object file\n\
19919-64 create 64 ABI object file\n"));
e7af610e 19920}
14e777e0 19921
1575952e 19922#ifdef TE_IRIX
14e777e0 19923enum dwarf2_format
413a266c 19924mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19925{
369943fe 19926 if (HAVE_64BIT_SYMBOLS)
1575952e 19927 return dwarf2_format_64bit_irix;
14e777e0
KB
19928 else
19929 return dwarf2_format_32bit;
19930}
1575952e 19931#endif
73369e65
EC
19932
19933int
19934mips_dwarf2_addr_size (void)
19935{
6b6b3450 19936 if (HAVE_64BIT_OBJECTS)
73369e65 19937 return 8;
73369e65
EC
19938 else
19939 return 4;
19940}
5862107c
EC
19941
19942/* Standard calling conventions leave the CFA at SP on entry. */
19943void
19944mips_cfi_frame_initial_instructions (void)
19945{
19946 cfi_add_CFA_def_cfa_register (SP);
19947}
19948
707bfff6
TS
19949int
19950tc_mips_regname_to_dw2regnum (char *regname)
19951{
19952 unsigned int regnum = -1;
19953 unsigned int reg;
19954
19955 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19956 regnum = reg;
19957
19958 return regnum;
19959}
263b2574 19960
19961/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19962 Given a symbolic attribute NAME, return the proper integer value.
19963 Returns -1 if the attribute is not known. */
19964
19965int
19966mips_convert_symbolic_attribute (const char *name)
19967{
19968 static const struct
19969 {
19970 const char * name;
19971 const int tag;
19972 }
19973 attribute_table[] =
19974 {
19975#define T(tag) {#tag, tag}
19976 T (Tag_GNU_MIPS_ABI_FP),
19977 T (Tag_GNU_MIPS_ABI_MSA),
19978#undef T
19979 };
19980 unsigned int i;
19981
19982 if (name == NULL)
19983 return -1;
19984
19985 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19986 if (streq (name, attribute_table[i].name))
19987 return attribute_table[i].tag;
19988
19989 return -1;
19990}
fd5c94ab
RS
19991
19992void
19993md_mips_end (void)
19994{
351cdf24
MF
19995 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19996
fd5c94ab
RS
19997 mips_emit_delays ();
19998 if (cur_proc_ptr)
19999 as_warn (_("missing .end at end of assembly"));
919731af 20000
20001 /* Just in case no code was emitted, do the consistency check. */
20002 file_mips_check_options ();
351cdf24
MF
20003
20004 /* Set a floating-point ABI if the user did not. */
20005 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20006 {
20007 /* Perform consistency checks on the floating-point ABI. */
20008 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20009 Tag_GNU_MIPS_ABI_FP);
20010 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20011 check_fpabi (fpabi);
20012 }
20013 else
20014 {
20015 /* Soft-float gets precedence over single-float, the two options should
20016 not be used together so this should not matter. */
20017 if (file_mips_opts.soft_float == 1)
20018 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20019 /* Single-float gets precedence over all double_float cases. */
20020 else if (file_mips_opts.single_float == 1)
20021 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20022 else
20023 {
20024 switch (file_mips_opts.fp)
20025 {
20026 case 32:
20027 if (file_mips_opts.gp == 32)
20028 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20029 break;
20030 case 0:
20031 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20032 break;
20033 case 64:
20034 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20035 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20036 else if (file_mips_opts.gp == 32)
20037 fpabi = Val_GNU_MIPS_ABI_FP_64;
20038 else
20039 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20040 break;
20041 }
20042 }
20043
20044 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20045 Tag_GNU_MIPS_ABI_FP, fpabi);
20046 }
fd5c94ab 20047}
2f0c68f2
CM
20048
20049/* Returns the relocation type required for a particular CFI encoding. */
20050
20051bfd_reloc_code_real_type
20052mips_cfi_reloc_for_encoding (int encoding)
20053{
20054 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20055 return BFD_RELOC_32_PCREL;
20056 else return BFD_RELOC_NONE;
20057}
This page took 2.995965 seconds and 4 git commands to generate.