MIPS16/GAS: Restore unsupported relocation diagnostics
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
6f2750fe 2 Copyright (C) 1993-2016 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;
252b5132 946\f
4d7206a2
RS
947/* The expansion of many macros depends on the type of symbol that
948 they refer to. For example, when generating position-dependent code,
949 a macro that refers to a symbol may have two different expansions,
950 one which uses GP-relative addresses and one which uses absolute
951 addresses. When generating SVR4-style PIC, a macro may have
952 different expansions for local and global symbols.
953
954 We handle these situations by generating both sequences and putting
955 them in variant frags. In position-dependent code, the first sequence
956 will be the GP-relative one and the second sequence will be the
957 absolute one. In SVR4 PIC, the first sequence will be for global
958 symbols and the second will be for local symbols.
959
584892a6
RS
960 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
961 SECOND are the lengths of the two sequences in bytes. These fields
962 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
963 the subtype has the following flags:
4d7206a2 964
584892a6
RS
965 RELAX_USE_SECOND
966 Set if it has been decided that we should use the second
967 sequence instead of the first.
968
969 RELAX_SECOND_LONGER
970 Set in the first variant frag if the macro's second implementation
971 is longer than its first. This refers to the macro as a whole,
972 not an individual relaxation.
973
974 RELAX_NOMACRO
975 Set in the first variant frag if the macro appeared in a .set nomacro
976 block and if one alternative requires a warning but the other does not.
977
978 RELAX_DELAY_SLOT
979 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
980 delay slot.
4d7206a2 981
df58fc94
RS
982 RELAX_DELAY_SLOT_16BIT
983 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
984 16-bit instruction.
985
986 RELAX_DELAY_SLOT_SIZE_FIRST
987 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
988 the macro is of the wrong size for the branch delay slot.
989
990 RELAX_DELAY_SLOT_SIZE_SECOND
991 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
992 the macro is of the wrong size for the branch delay slot.
993
4d7206a2
RS
994 The frag's "opcode" points to the first fixup for relaxable code.
995
996 Relaxable macros are generated using a sequence such as:
997
998 relax_start (SYMBOL);
999 ... generate first expansion ...
1000 relax_switch ();
1001 ... generate second expansion ...
1002 relax_end ();
1003
1004 The code and fixups for the unwanted alternative are discarded
1005 by md_convert_frag. */
584892a6 1006#define RELAX_ENCODE(FIRST, SECOND) (((FIRST) << 8) | (SECOND))
4d7206a2 1007
584892a6
RS
1008#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1009#define RELAX_SECOND(X) ((X) & 0xff)
1010#define RELAX_USE_SECOND 0x10000
1011#define RELAX_SECOND_LONGER 0x20000
1012#define RELAX_NOMACRO 0x40000
1013#define RELAX_DELAY_SLOT 0x80000
df58fc94
RS
1014#define RELAX_DELAY_SLOT_16BIT 0x100000
1015#define RELAX_DELAY_SLOT_SIZE_FIRST 0x200000
1016#define RELAX_DELAY_SLOT_SIZE_SECOND 0x400000
252b5132 1017
4a6a3df4
AO
1018/* Branch without likely bit. If label is out of range, we turn:
1019
134c0c8b 1020 beq reg1, reg2, label
4a6a3df4
AO
1021 delay slot
1022
1023 into
1024
1025 bne reg1, reg2, 0f
1026 nop
1027 j label
1028 0: delay slot
1029
1030 with the following opcode replacements:
1031
1032 beq <-> bne
1033 blez <-> bgtz
1034 bltz <-> bgez
1035 bc1f <-> bc1t
1036
1037 bltzal <-> bgezal (with jal label instead of j label)
1038
1039 Even though keeping the delay slot instruction in the delay slot of
1040 the branch would be more efficient, it would be very tricky to do
1041 correctly, because we'd have to introduce a variable frag *after*
1042 the delay slot instruction, and expand that instead. Let's do it
1043 the easy way for now, even if the branch-not-taken case now costs
1044 one additional instruction. Out-of-range branches are not supposed
1045 to be common, anyway.
1046
1047 Branch likely. If label is out of range, we turn:
1048
1049 beql reg1, reg2, label
1050 delay slot (annulled if branch not taken)
1051
1052 into
1053
1054 beql reg1, reg2, 1f
1055 nop
1056 beql $0, $0, 2f
1057 nop
1058 1: j[al] label
1059 delay slot (executed only if branch taken)
1060 2:
1061
1062 It would be possible to generate a shorter sequence by losing the
1063 likely bit, generating something like:
b34976b6 1064
4a6a3df4
AO
1065 bne reg1, reg2, 0f
1066 nop
1067 j[al] label
1068 delay slot (executed only if branch taken)
1069 0:
1070
1071 beql -> bne
1072 bnel -> beq
1073 blezl -> bgtz
1074 bgtzl -> blez
1075 bltzl -> bgez
1076 bgezl -> bltz
1077 bc1fl -> bc1t
1078 bc1tl -> bc1f
1079
1080 bltzall -> bgezal (with jal label instead of j label)
1081 bgezall -> bltzal (ditto)
1082
1083
1084 but it's not clear that it would actually improve performance. */
66b3e8da
MR
1085#define RELAX_BRANCH_ENCODE(at, uncond, likely, link, toofar) \
1086 ((relax_substateT) \
1087 (0xc0000000 \
1088 | ((at) & 0x1f) \
1089 | ((toofar) ? 0x20 : 0) \
1090 | ((link) ? 0x40 : 0) \
1091 | ((likely) ? 0x80 : 0) \
1092 | ((uncond) ? 0x100 : 0)))
4a6a3df4 1093#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
66b3e8da
MR
1094#define RELAX_BRANCH_UNCOND(i) (((i) & 0x100) != 0)
1095#define RELAX_BRANCH_LIKELY(i) (((i) & 0x80) != 0)
1096#define RELAX_BRANCH_LINK(i) (((i) & 0x40) != 0)
1097#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x20) != 0)
1098#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1099
252b5132
RH
1100/* For mips16 code, we use an entirely different form of relaxation.
1101 mips16 supports two versions of most instructions which take
1102 immediate values: a small one which takes some small value, and a
1103 larger one which takes a 16 bit value. Since branches also follow
1104 this pattern, relaxing these values is required.
1105
1106 We can assemble both mips16 and normal MIPS code in a single
1107 object. Therefore, we need to support this type of relaxation at
1108 the same time that we support the relaxation described above. We
1109 use the high bit of the subtype field to distinguish these cases.
1110
1111 The information we store for this type of relaxation is the
1112 argument code found in the opcode file for this relocation, whether
1113 the user explicitly requested a small or extended form, and whether
1114 the relocation is in a jump or jal delay slot. That tells us the
1115 size of the value, and how it should be stored. We also store
1116 whether the fragment is considered to be extended or not. We also
1117 store whether this is known to be a branch to a different section,
1118 whether we have tried to relax this frag yet, and whether we have
1119 ever extended a PC relative fragment because of a shift count. */
1120#define RELAX_MIPS16_ENCODE(type, small, ext, dslot, jal_dslot) \
1121 (0x80000000 \
1122 | ((type) & 0xff) \
1123 | ((small) ? 0x100 : 0) \
1124 | ((ext) ? 0x200 : 0) \
1125 | ((dslot) ? 0x400 : 0) \
1126 | ((jal_dslot) ? 0x800 : 0))
4a6a3df4 1127#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132
RH
1128#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
1129#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x100) != 0)
1130#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x200) != 0)
1131#define RELAX_MIPS16_DSLOT(i) (((i) & 0x400) != 0)
1132#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x800) != 0)
1133#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x1000) != 0)
1134#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x1000)
1135#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) &~ 0x1000)
1136#define RELAX_MIPS16_LONG_BRANCH(i) (((i) & 0x2000) != 0)
1137#define RELAX_MIPS16_MARK_LONG_BRANCH(i) ((i) | 0x2000)
1138#define RELAX_MIPS16_CLEAR_LONG_BRANCH(i) ((i) &~ 0x2000)
885add95 1139
df58fc94
RS
1140/* For microMIPS code, we use relaxation similar to one we use for
1141 MIPS16 code. Some instructions that take immediate values support
1142 two encodings: a small one which takes some small value, and a
1143 larger one which takes a 16 bit value. As some branches also follow
1144 this pattern, relaxing these values is required.
1145
1146 We can assemble both microMIPS and normal MIPS code in a single
1147 object. Therefore, we need to support this type of relaxation at
1148 the same time that we support the relaxation described above. We
1149 use one of the high bits of the subtype field to distinguish these
1150 cases.
1151
1152 The information we store for this type of relaxation is the argument
1153 code found in the opcode file for this relocation, the register
40209cad
MR
1154 selected as the assembler temporary, whether the branch is
1155 unconditional, whether it is compact, whether it stores the link
1156 address implicitly in $ra, whether relaxation of out-of-range 32-bit
1157 branches to a sequence of instructions is enabled, and whether the
1158 displacement of a branch is too large to fit as an immediate argument
1159 of a 16-bit and a 32-bit branch, respectively. */
1160#define RELAX_MICROMIPS_ENCODE(type, at, uncond, compact, link, \
1161 relax32, toofar16, toofar32) \
1162 (0x40000000 \
1163 | ((type) & 0xff) \
1164 | (((at) & 0x1f) << 8) \
1165 | ((uncond) ? 0x2000 : 0) \
1166 | ((compact) ? 0x4000 : 0) \
1167 | ((link) ? 0x8000 : 0) \
1168 | ((relax32) ? 0x10000 : 0) \
1169 | ((toofar16) ? 0x20000 : 0) \
1170 | ((toofar32) ? 0x40000 : 0))
df58fc94
RS
1171#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1172#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1173#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
40209cad
MR
1174#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x2000) != 0)
1175#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x4000) != 0)
1176#define RELAX_MICROMIPS_LINK(i) (((i) & 0x8000) != 0)
1177#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x10000) != 0)
1178
1179#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x20000) != 0)
1180#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x20000)
1181#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x20000)
1182#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x40000) != 0)
1183#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x40000)
1184#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x40000)
df58fc94 1185
43c0598f
RS
1186/* Sign-extend 16-bit value X. */
1187#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1188
885add95
CD
1189/* Is the given value a sign-extended 32-bit value? */
1190#define IS_SEXT_32BIT_NUM(x) \
1191 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1192 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1193
1194/* Is the given value a sign-extended 16-bit value? */
1195#define IS_SEXT_16BIT_NUM(x) \
1196 (((x) &~ (offsetT) 0x7fff) == 0 \
1197 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1198
df58fc94
RS
1199/* Is the given value a sign-extended 12-bit value? */
1200#define IS_SEXT_12BIT_NUM(x) \
1201 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1202
7f3c4072
CM
1203/* Is the given value a sign-extended 9-bit value? */
1204#define IS_SEXT_9BIT_NUM(x) \
1205 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1206
2051e8c4
MR
1207/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1208#define IS_ZEXT_32BIT_NUM(x) \
1209 (((x) &~ (offsetT) 0xffffffff) == 0 \
1210 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1211
bf12938e
RS
1212/* Extract bits MASK << SHIFT from STRUCT and shift them right
1213 SHIFT places. */
1214#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1215 (((STRUCT) >> (SHIFT)) & (MASK))
1216
bf12938e 1217/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1218#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1219 (!(MICROMIPS) \
1220 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1221 : EXTRACT_BITS ((INSN).insn_opcode, \
1222 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1223#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1224 EXTRACT_BITS ((INSN).insn_opcode, \
1225 MIPS16OP_MASK_##FIELD, \
1226 MIPS16OP_SH_##FIELD)
5c04167a
RS
1227
1228/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1229#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1230\f
df58fc94
RS
1231/* Whether or not we are emitting a branch-likely macro. */
1232static bfd_boolean emit_branch_likely_macro = FALSE;
1233
4d7206a2
RS
1234/* Global variables used when generating relaxable macros. See the
1235 comment above RELAX_ENCODE for more details about how relaxation
1236 is used. */
1237static struct {
1238 /* 0 if we're not emitting a relaxable macro.
1239 1 if we're emitting the first of the two relaxation alternatives.
1240 2 if we're emitting the second alternative. */
1241 int sequence;
1242
1243 /* The first relaxable fixup in the current frag. (In other words,
1244 the first fixup that refers to relaxable code.) */
1245 fixS *first_fixup;
1246
1247 /* sizes[0] says how many bytes of the first alternative are stored in
1248 the current frag. Likewise sizes[1] for the second alternative. */
1249 unsigned int sizes[2];
1250
1251 /* The symbol on which the choice of sequence depends. */
1252 symbolS *symbol;
1253} mips_relax;
252b5132 1254\f
584892a6
RS
1255/* Global variables used to decide whether a macro needs a warning. */
1256static struct {
1257 /* True if the macro is in a branch delay slot. */
1258 bfd_boolean delay_slot_p;
1259
df58fc94
RS
1260 /* Set to the length in bytes required if the macro is in a delay slot
1261 that requires a specific length of instruction, otherwise zero. */
1262 unsigned int delay_slot_length;
1263
584892a6
RS
1264 /* For relaxable macros, sizes[0] is the length of the first alternative
1265 in bytes and sizes[1] is the length of the second alternative.
1266 For non-relaxable macros, both elements give the length of the
1267 macro in bytes. */
1268 unsigned int sizes[2];
1269
df58fc94
RS
1270 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1271 instruction of the first alternative in bytes and first_insn_sizes[1]
1272 is the length of the first instruction of the second alternative.
1273 For non-relaxable macros, both elements give the length of the first
1274 instruction in bytes.
1275
1276 Set to zero if we haven't yet seen the first instruction. */
1277 unsigned int first_insn_sizes[2];
1278
1279 /* For relaxable macros, insns[0] is the number of instructions for the
1280 first alternative and insns[1] is the number of instructions for the
1281 second alternative.
1282
1283 For non-relaxable macros, both elements give the number of
1284 instructions for the macro. */
1285 unsigned int insns[2];
1286
584892a6
RS
1287 /* The first variant frag for this macro. */
1288 fragS *first_frag;
1289} mips_macro_warning;
1290\f
252b5132
RH
1291/* Prototypes for static functions. */
1292
252b5132
RH
1293enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1294
b34976b6 1295static void append_insn
df58fc94
RS
1296 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1297 bfd_boolean expansionp);
7d10b47d 1298static void mips_no_prev_insn (void);
c67a084a 1299static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1300static void mips16_macro_build
03ea81db 1301 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1302static void load_register (int, expressionS *, int);
584892a6
RS
1303static void macro_start (void);
1304static void macro_end (void);
833794fc 1305static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1306static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1307static void mips_ip (char *str, struct mips_cl_insn * ip);
1308static void mips16_ip (char *str, struct mips_cl_insn * ip);
b34976b6 1309static void mips16_immed
3b4dbbbf 1310 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1311 unsigned int, unsigned long *);
5e0116d5 1312static size_t my_getSmallExpression
17a2f251
TS
1313 (expressionS *, bfd_reloc_code_real_type *, char *);
1314static void my_getExpression (expressionS *, char *);
1315static void s_align (int);
1316static void s_change_sec (int);
1317static void s_change_section (int);
1318static void s_cons (int);
1319static void s_float_cons (int);
1320static void s_mips_globl (int);
1321static void s_option (int);
1322static void s_mipsset (int);
1323static void s_abicalls (int);
1324static void s_cpload (int);
1325static void s_cpsetup (int);
1326static void s_cplocal (int);
1327static void s_cprestore (int);
1328static void s_cpreturn (int);
741d6ea8
JM
1329static void s_dtprelword (int);
1330static void s_dtpreldword (int);
d0f13682
CLT
1331static void s_tprelword (int);
1332static void s_tpreldword (int);
17a2f251
TS
1333static void s_gpvalue (int);
1334static void s_gpword (int);
1335static void s_gpdword (int);
a3f278e2 1336static void s_ehword (int);
17a2f251
TS
1337static void s_cpadd (int);
1338static void s_insn (int);
ba92f887 1339static void s_nan (int);
919731af 1340static void s_module (int);
17a2f251
TS
1341static void s_mips_ent (int);
1342static void s_mips_end (int);
1343static void s_mips_frame (int);
1344static void s_mips_mask (int reg_type);
1345static void s_mips_stab (int);
1346static void s_mips_weakext (int);
1347static void s_mips_file (int);
1348static void s_mips_loc (int);
1349static bfd_boolean pic_need_relax (symbolS *, asection *);
4a6a3df4 1350static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1351static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1352static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1353static void file_mips_check_options (void);
e7af610e
NC
1354
1355/* Table and functions used to map between CPU/ISA names, and
1356 ISA levels, and CPU numbers. */
1357
e972090a
NC
1358struct mips_cpu_info
1359{
e7af610e 1360 const char *name; /* CPU or ISA name. */
d16afab6
RS
1361 int flags; /* MIPS_CPU_* flags. */
1362 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1363 int isa; /* ISA level. */
1364 int cpu; /* CPU number (default CPU if ISA). */
1365};
1366
ad3fea08 1367#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1368
17a2f251
TS
1369static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1370static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1371static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1372\f
c31f3936
RS
1373/* Command-line options. */
1374const char *md_shortopts = "O::g::G:";
1375
1376enum options
1377 {
1378 OPTION_MARCH = OPTION_MD_BASE,
1379 OPTION_MTUNE,
1380 OPTION_MIPS1,
1381 OPTION_MIPS2,
1382 OPTION_MIPS3,
1383 OPTION_MIPS4,
1384 OPTION_MIPS5,
1385 OPTION_MIPS32,
1386 OPTION_MIPS64,
1387 OPTION_MIPS32R2,
ae52f483
AB
1388 OPTION_MIPS32R3,
1389 OPTION_MIPS32R5,
7361da2c 1390 OPTION_MIPS32R6,
c31f3936 1391 OPTION_MIPS64R2,
ae52f483
AB
1392 OPTION_MIPS64R3,
1393 OPTION_MIPS64R5,
7361da2c 1394 OPTION_MIPS64R6,
c31f3936
RS
1395 OPTION_MIPS16,
1396 OPTION_NO_MIPS16,
1397 OPTION_MIPS3D,
1398 OPTION_NO_MIPS3D,
1399 OPTION_MDMX,
1400 OPTION_NO_MDMX,
1401 OPTION_DSP,
1402 OPTION_NO_DSP,
1403 OPTION_MT,
1404 OPTION_NO_MT,
1405 OPTION_VIRT,
1406 OPTION_NO_VIRT,
56d438b1
CF
1407 OPTION_MSA,
1408 OPTION_NO_MSA,
c31f3936
RS
1409 OPTION_SMARTMIPS,
1410 OPTION_NO_SMARTMIPS,
1411 OPTION_DSPR2,
1412 OPTION_NO_DSPR2,
8f4f9071
MF
1413 OPTION_DSPR3,
1414 OPTION_NO_DSPR3,
c31f3936
RS
1415 OPTION_EVA,
1416 OPTION_NO_EVA,
7d64c587
AB
1417 OPTION_XPA,
1418 OPTION_NO_XPA,
c31f3936
RS
1419 OPTION_MICROMIPS,
1420 OPTION_NO_MICROMIPS,
1421 OPTION_MCU,
1422 OPTION_NO_MCU,
1423 OPTION_COMPAT_ARCH_BASE,
1424 OPTION_M4650,
1425 OPTION_NO_M4650,
1426 OPTION_M4010,
1427 OPTION_NO_M4010,
1428 OPTION_M4100,
1429 OPTION_NO_M4100,
1430 OPTION_M3900,
1431 OPTION_NO_M3900,
1432 OPTION_M7000_HILO_FIX,
1433 OPTION_MNO_7000_HILO_FIX,
1434 OPTION_FIX_24K,
1435 OPTION_NO_FIX_24K,
a8d14a88
CM
1436 OPTION_FIX_RM7000,
1437 OPTION_NO_FIX_RM7000,
c31f3936
RS
1438 OPTION_FIX_LOONGSON2F_JUMP,
1439 OPTION_NO_FIX_LOONGSON2F_JUMP,
1440 OPTION_FIX_LOONGSON2F_NOP,
1441 OPTION_NO_FIX_LOONGSON2F_NOP,
1442 OPTION_FIX_VR4120,
1443 OPTION_NO_FIX_VR4120,
1444 OPTION_FIX_VR4130,
1445 OPTION_NO_FIX_VR4130,
1446 OPTION_FIX_CN63XXP1,
1447 OPTION_NO_FIX_CN63XXP1,
1448 OPTION_TRAP,
1449 OPTION_BREAK,
1450 OPTION_EB,
1451 OPTION_EL,
1452 OPTION_FP32,
1453 OPTION_GP32,
1454 OPTION_CONSTRUCT_FLOATS,
1455 OPTION_NO_CONSTRUCT_FLOATS,
1456 OPTION_FP64,
351cdf24 1457 OPTION_FPXX,
c31f3936
RS
1458 OPTION_GP64,
1459 OPTION_RELAX_BRANCH,
1460 OPTION_NO_RELAX_BRANCH,
833794fc
MR
1461 OPTION_INSN32,
1462 OPTION_NO_INSN32,
c31f3936
RS
1463 OPTION_MSHARED,
1464 OPTION_MNO_SHARED,
1465 OPTION_MSYM32,
1466 OPTION_MNO_SYM32,
1467 OPTION_SOFT_FLOAT,
1468 OPTION_HARD_FLOAT,
1469 OPTION_SINGLE_FLOAT,
1470 OPTION_DOUBLE_FLOAT,
1471 OPTION_32,
c31f3936
RS
1472 OPTION_CALL_SHARED,
1473 OPTION_CALL_NONPIC,
1474 OPTION_NON_SHARED,
1475 OPTION_XGOT,
1476 OPTION_MABI,
1477 OPTION_N32,
1478 OPTION_64,
1479 OPTION_MDEBUG,
1480 OPTION_NO_MDEBUG,
1481 OPTION_PDR,
1482 OPTION_NO_PDR,
1483 OPTION_MVXWORKS_PIC,
ba92f887 1484 OPTION_NAN,
351cdf24
MF
1485 OPTION_ODD_SPREG,
1486 OPTION_NO_ODD_SPREG,
c31f3936
RS
1487 OPTION_END_OF_ENUM
1488 };
1489
1490struct option md_longopts[] =
1491{
1492 /* Options which specify architecture. */
1493 {"march", required_argument, NULL, OPTION_MARCH},
1494 {"mtune", required_argument, NULL, OPTION_MTUNE},
1495 {"mips0", no_argument, NULL, OPTION_MIPS1},
1496 {"mips1", no_argument, NULL, OPTION_MIPS1},
1497 {"mips2", no_argument, NULL, OPTION_MIPS2},
1498 {"mips3", no_argument, NULL, OPTION_MIPS3},
1499 {"mips4", no_argument, NULL, OPTION_MIPS4},
1500 {"mips5", no_argument, NULL, OPTION_MIPS5},
1501 {"mips32", no_argument, NULL, OPTION_MIPS32},
1502 {"mips64", no_argument, NULL, OPTION_MIPS64},
1503 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1504 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1505 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1506 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1507 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1508 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1509 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1510 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1511
1512 /* Options which specify Application Specific Extensions (ASEs). */
1513 {"mips16", no_argument, NULL, OPTION_MIPS16},
1514 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1515 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1516 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1517 {"mdmx", no_argument, NULL, OPTION_MDMX},
1518 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1519 {"mdsp", no_argument, NULL, OPTION_DSP},
1520 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1521 {"mmt", no_argument, NULL, OPTION_MT},
1522 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1523 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1524 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1525 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1526 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1527 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1528 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1529 {"meva", no_argument, NULL, OPTION_EVA},
1530 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1531 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1532 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1533 {"mmcu", no_argument, NULL, OPTION_MCU},
1534 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1535 {"mvirt", no_argument, NULL, OPTION_VIRT},
1536 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1537 {"mmsa", no_argument, NULL, OPTION_MSA},
1538 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1539 {"mxpa", no_argument, NULL, OPTION_XPA},
1540 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
c31f3936
RS
1541
1542 /* Old-style architecture options. Don't add more of these. */
1543 {"m4650", no_argument, NULL, OPTION_M4650},
1544 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1545 {"m4010", no_argument, NULL, OPTION_M4010},
1546 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1547 {"m4100", no_argument, NULL, OPTION_M4100},
1548 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1549 {"m3900", no_argument, NULL, OPTION_M3900},
1550 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1551
1552 /* Options which enable bug fixes. */
1553 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1554 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1555 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1556 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1557 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1558 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1559 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1560 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1561 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1562 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1563 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1564 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1565 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1566 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1567 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1568 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1569 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1570
1571 /* Miscellaneous options. */
1572 {"trap", no_argument, NULL, OPTION_TRAP},
1573 {"no-break", no_argument, NULL, OPTION_TRAP},
1574 {"break", no_argument, NULL, OPTION_BREAK},
1575 {"no-trap", no_argument, NULL, OPTION_BREAK},
1576 {"EB", no_argument, NULL, OPTION_EB},
1577 {"EL", no_argument, NULL, OPTION_EL},
1578 {"mfp32", no_argument, NULL, OPTION_FP32},
1579 {"mgp32", no_argument, NULL, OPTION_GP32},
1580 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1581 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1582 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1583 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1584 {"mgp64", no_argument, NULL, OPTION_GP64},
1585 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1586 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
833794fc
MR
1587 {"minsn32", no_argument, NULL, OPTION_INSN32},
1588 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1589 {"mshared", no_argument, NULL, OPTION_MSHARED},
1590 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1591 {"msym32", no_argument, NULL, OPTION_MSYM32},
1592 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1593 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1594 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1595 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1596 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1597 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1598 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1599
1600 /* Strictly speaking this next option is ELF specific,
1601 but we allow it for other ports as well in order to
1602 make testing easier. */
1603 {"32", no_argument, NULL, OPTION_32},
1604
1605 /* ELF-specific options. */
c31f3936
RS
1606 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1607 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1608 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1609 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1610 {"xgot", no_argument, NULL, OPTION_XGOT},
1611 {"mabi", required_argument, NULL, OPTION_MABI},
1612 {"n32", no_argument, NULL, OPTION_N32},
1613 {"64", no_argument, NULL, OPTION_64},
1614 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1615 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1616 {"mpdr", no_argument, NULL, OPTION_PDR},
1617 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1618 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1619 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1620
1621 {NULL, no_argument, NULL, 0}
1622};
1623size_t md_longopts_size = sizeof (md_longopts);
1624\f
c6278170
RS
1625/* Information about either an Application Specific Extension or an
1626 optional architecture feature that, for simplicity, we treat in the
1627 same way as an ASE. */
1628struct mips_ase
1629{
1630 /* The name of the ASE, used in both the command-line and .set options. */
1631 const char *name;
1632
1633 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1634 and 64-bit architectures, the flags here refer to the subset that
1635 is available on both. */
1636 unsigned int flags;
1637
1638 /* The ASE_* flag used for instructions that are available on 64-bit
1639 architectures but that are not included in FLAGS. */
1640 unsigned int flags64;
1641
1642 /* The command-line options that turn the ASE on and off. */
1643 int option_on;
1644 int option_off;
1645
1646 /* The minimum required architecture revisions for MIPS32, MIPS64,
1647 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1648 int mips32_rev;
1649 int mips64_rev;
1650 int micromips32_rev;
1651 int micromips64_rev;
7361da2c
AB
1652
1653 /* The architecture where the ASE was removed or -1 if the extension has not
1654 been removed. */
1655 int rem_rev;
c6278170
RS
1656};
1657
1658/* A table of all supported ASEs. */
1659static const struct mips_ase mips_ases[] = {
1660 { "dsp", ASE_DSP, ASE_DSP64,
1661 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1662 2, 2, 2, 2,
1663 -1 },
c6278170
RS
1664
1665 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1666 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1667 2, 2, 2, 2,
1668 -1 },
c6278170 1669
8f4f9071
MF
1670 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1671 OPTION_DSPR3, OPTION_NO_DSPR3,
1672 6, 6, -1, -1,
1673 -1 },
1674
c6278170
RS
1675 { "eva", ASE_EVA, 0,
1676 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1677 2, 2, 2, 2,
1678 -1 },
c6278170
RS
1679
1680 { "mcu", ASE_MCU, 0,
1681 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1682 2, 2, 2, 2,
1683 -1 },
c6278170
RS
1684
1685 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1686 { "mdmx", ASE_MDMX, 0,
1687 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1688 -1, 1, -1, -1,
1689 6 },
c6278170
RS
1690
1691 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1692 { "mips3d", ASE_MIPS3D, 0,
1693 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1694 2, 1, -1, -1,
1695 6 },
c6278170
RS
1696
1697 { "mt", ASE_MT, 0,
1698 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1699 2, 2, -1, -1,
1700 -1 },
c6278170
RS
1701
1702 { "smartmips", ASE_SMARTMIPS, 0,
1703 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1704 1, -1, -1, -1,
1705 6 },
c6278170
RS
1706
1707 { "virt", ASE_VIRT, ASE_VIRT64,
1708 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1709 2, 2, 2, 2,
1710 -1 },
56d438b1
CF
1711
1712 { "msa", ASE_MSA, ASE_MSA64,
1713 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1714 2, 2, 2, 2,
1715 -1 },
7d64c587
AB
1716
1717 { "xpa", ASE_XPA, 0,
1718 OPTION_XPA, OPTION_NO_XPA,
7361da2c
AB
1719 2, 2, -1, -1,
1720 -1 },
c6278170
RS
1721};
1722
1723/* The set of ASEs that require -mfp64. */
82bda27b 1724#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1725
1726/* Groups of ASE_* flags that represent different revisions of an ASE. */
1727static const unsigned int mips_ase_groups[] = {
8f4f9071 1728 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1729};
1730\f
252b5132
RH
1731/* Pseudo-op table.
1732
1733 The following pseudo-ops from the Kane and Heinrich MIPS book
1734 should be defined here, but are currently unsupported: .alias,
1735 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1736
1737 The following pseudo-ops from the Kane and Heinrich MIPS book are
1738 specific to the type of debugging information being generated, and
1739 should be defined by the object format: .aent, .begin, .bend,
1740 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1741 .vreg.
1742
1743 The following pseudo-ops from the Kane and Heinrich MIPS book are
1744 not MIPS CPU specific, but are also not specific to the object file
1745 format. This file is probably the best place to define them, but
d84bcf09 1746 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1747
e972090a
NC
1748static const pseudo_typeS mips_pseudo_table[] =
1749{
beae10d5 1750 /* MIPS specific pseudo-ops. */
252b5132
RH
1751 {"option", s_option, 0},
1752 {"set", s_mipsset, 0},
1753 {"rdata", s_change_sec, 'r'},
1754 {"sdata", s_change_sec, 's'},
1755 {"livereg", s_ignore, 0},
1756 {"abicalls", s_abicalls, 0},
1757 {"cpload", s_cpload, 0},
6478892d
TS
1758 {"cpsetup", s_cpsetup, 0},
1759 {"cplocal", s_cplocal, 0},
252b5132 1760 {"cprestore", s_cprestore, 0},
6478892d 1761 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1762 {"dtprelword", s_dtprelword, 0},
1763 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1764 {"tprelword", s_tprelword, 0},
1765 {"tpreldword", s_tpreldword, 0},
6478892d 1766 {"gpvalue", s_gpvalue, 0},
252b5132 1767 {"gpword", s_gpword, 0},
10181a0d 1768 {"gpdword", s_gpdword, 0},
a3f278e2 1769 {"ehword", s_ehword, 0},
252b5132
RH
1770 {"cpadd", s_cpadd, 0},
1771 {"insn", s_insn, 0},
ba92f887 1772 {"nan", s_nan, 0},
919731af 1773 {"module", s_module, 0},
252b5132 1774
beae10d5 1775 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1776 chips. */
38a57ae7 1777 {"asciiz", stringer, 8 + 1},
252b5132
RH
1778 {"bss", s_change_sec, 'b'},
1779 {"err", s_err, 0},
1780 {"half", s_cons, 1},
1781 {"dword", s_cons, 3},
1782 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1783 {"origin", s_org, 0},
1784 {"repeat", s_rept, 0},
252b5132 1785
998b3c36
MR
1786 /* For MIPS this is non-standard, but we define it for consistency. */
1787 {"sbss", s_change_sec, 'B'},
1788
beae10d5 1789 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1790 here for one reason or another. */
1791 {"align", s_align, 0},
1792 {"byte", s_cons, 0},
1793 {"data", s_change_sec, 'd'},
1794 {"double", s_float_cons, 'd'},
1795 {"float", s_float_cons, 'f'},
1796 {"globl", s_mips_globl, 0},
1797 {"global", s_mips_globl, 0},
1798 {"hword", s_cons, 1},
1799 {"int", s_cons, 2},
1800 {"long", s_cons, 2},
1801 {"octa", s_cons, 4},
1802 {"quad", s_cons, 3},
cca86cc8 1803 {"section", s_change_section, 0},
252b5132
RH
1804 {"short", s_cons, 1},
1805 {"single", s_float_cons, 'f'},
754e2bb9 1806 {"stabd", s_mips_stab, 'd'},
252b5132 1807 {"stabn", s_mips_stab, 'n'},
754e2bb9 1808 {"stabs", s_mips_stab, 's'},
252b5132
RH
1809 {"text", s_change_sec, 't'},
1810 {"word", s_cons, 2},
add56521 1811
add56521 1812 { "extern", ecoff_directive_extern, 0},
add56521 1813
43841e91 1814 { NULL, NULL, 0 },
252b5132
RH
1815};
1816
e972090a
NC
1817static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1818{
beae10d5
KH
1819 /* These pseudo-ops should be defined by the object file format.
1820 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1821 {"aent", s_mips_ent, 1},
1822 {"bgnb", s_ignore, 0},
1823 {"end", s_mips_end, 0},
1824 {"endb", s_ignore, 0},
1825 {"ent", s_mips_ent, 0},
c5dd6aab 1826 {"file", s_mips_file, 0},
252b5132
RH
1827 {"fmask", s_mips_mask, 'F'},
1828 {"frame", s_mips_frame, 0},
c5dd6aab 1829 {"loc", s_mips_loc, 0},
252b5132
RH
1830 {"mask", s_mips_mask, 'R'},
1831 {"verstamp", s_ignore, 0},
43841e91 1832 { NULL, NULL, 0 },
252b5132
RH
1833};
1834
3ae8dd8d
MR
1835/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1836 purpose of the `.dc.a' internal pseudo-op. */
1837
1838int
1839mips_address_bytes (void)
1840{
919731af 1841 file_mips_check_options ();
3ae8dd8d
MR
1842 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1843}
1844
17a2f251 1845extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1846
1847void
17a2f251 1848mips_pop_insert (void)
252b5132
RH
1849{
1850 pop_insert (mips_pseudo_table);
1851 if (! ECOFF_DEBUGGING)
1852 pop_insert (mips_nonecoff_pseudo_table);
1853}
1854\f
1855/* Symbols labelling the current insn. */
1856
e972090a
NC
1857struct insn_label_list
1858{
252b5132
RH
1859 struct insn_label_list *next;
1860 symbolS *label;
1861};
1862
252b5132 1863static struct insn_label_list *free_insn_labels;
742a56fe 1864#define label_list tc_segment_info_data.labels
252b5132 1865
17a2f251 1866static void mips_clear_insn_labels (void);
df58fc94
RS
1867static void mips_mark_labels (void);
1868static void mips_compressed_mark_labels (void);
252b5132
RH
1869
1870static inline void
17a2f251 1871mips_clear_insn_labels (void)
252b5132 1872{
ed9e98c2 1873 struct insn_label_list **pl;
a8dbcb85 1874 segment_info_type *si;
252b5132 1875
a8dbcb85
TS
1876 if (now_seg)
1877 {
1878 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1879 ;
3739860c 1880
a8dbcb85
TS
1881 si = seg_info (now_seg);
1882 *pl = si->label_list;
1883 si->label_list = NULL;
1884 }
252b5132 1885}
a8dbcb85 1886
df58fc94
RS
1887/* Mark instruction labels in MIPS16/microMIPS mode. */
1888
1889static inline void
1890mips_mark_labels (void)
1891{
1892 if (HAVE_CODE_COMPRESSION)
1893 mips_compressed_mark_labels ();
1894}
252b5132
RH
1895\f
1896static char *expr_end;
1897
e423441d 1898/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1899 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1900
1901static expressionS imm_expr;
252b5132 1902
77bd4346
RS
1903/* The relocatable field in an instruction and the relocs associated
1904 with it. These variables are used for instructions like LUI and
1905 JAL as well as true offsets. They are also used for address
1906 operands in macros. */
252b5132 1907
77bd4346 1908static expressionS offset_expr;
f6688943
TS
1909static bfd_reloc_code_real_type offset_reloc[3]
1910 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1911
df58fc94
RS
1912/* This is set to the resulting size of the instruction to be produced
1913 by mips16_ip if an explicit extension is used or by mips_ip if an
1914 explicit size is supplied. */
252b5132 1915
df58fc94 1916static unsigned int forced_insn_length;
252b5132 1917
e1b47bd5
RS
1918/* True if we are assembling an instruction. All dot symbols defined during
1919 this time should be treated as code labels. */
1920
1921static bfd_boolean mips_assembling_insn;
1922
ecb4347a
DJ
1923/* The pdr segment for per procedure frame/regmask info. Not used for
1924 ECOFF debugging. */
252b5132
RH
1925
1926static segT pdr_seg;
252b5132 1927
e013f690
TS
1928/* The default target format to use. */
1929
aeffff67
RS
1930#if defined (TE_FreeBSD)
1931#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
1932#elif defined (TE_TMIPS)
1933#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
1934#else
1935#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
1936#endif
1937
e013f690 1938const char *
17a2f251 1939mips_target_format (void)
e013f690
TS
1940{
1941 switch (OUTPUT_FLAVOR)
1942 {
e013f690 1943 case bfd_target_elf_flavour:
0a44bf69
RS
1944#ifdef TE_VXWORKS
1945 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
1946 return (target_big_endian
1947 ? "elf32-bigmips-vxworks"
1948 : "elf32-littlemips-vxworks");
1949#endif
e013f690 1950 return (target_big_endian
cfe86eaa 1951 ? (HAVE_64BIT_OBJECTS
aeffff67 1952 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 1953 : (HAVE_NEWABI
aeffff67
RS
1954 ? ELF_TARGET ("elf32-n", "big")
1955 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 1956 : (HAVE_64BIT_OBJECTS
aeffff67 1957 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 1958 : (HAVE_NEWABI
aeffff67
RS
1959 ? ELF_TARGET ("elf32-n", "little")
1960 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
1961 default:
1962 abort ();
1963 return NULL;
1964 }
1965}
1966
c6278170
RS
1967/* Return the ISA revision that is currently in use, or 0 if we are
1968 generating code for MIPS V or below. */
1969
1970static int
1971mips_isa_rev (void)
1972{
1973 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
1974 return 2;
1975
ae52f483
AB
1976 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
1977 return 3;
1978
1979 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
1980 return 5;
1981
7361da2c
AB
1982 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
1983 return 6;
1984
c6278170
RS
1985 /* microMIPS implies revision 2 or above. */
1986 if (mips_opts.micromips)
1987 return 2;
1988
1989 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
1990 return 1;
1991
1992 return 0;
1993}
1994
1995/* Return the mask of all ASEs that are revisions of those in FLAGS. */
1996
1997static unsigned int
1998mips_ase_mask (unsigned int flags)
1999{
2000 unsigned int i;
2001
2002 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2003 if (flags & mips_ase_groups[i])
2004 flags |= mips_ase_groups[i];
2005 return flags;
2006}
2007
2008/* Check whether the current ISA supports ASE. Issue a warning if
2009 appropriate. */
2010
2011static void
2012mips_check_isa_supports_ase (const struct mips_ase *ase)
2013{
2014 const char *base;
2015 int min_rev, size;
2016 static unsigned int warned_isa;
2017 static unsigned int warned_fp32;
2018
2019 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2020 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2021 else
2022 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2023 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2024 && (warned_isa & ase->flags) != ase->flags)
2025 {
2026 warned_isa |= ase->flags;
2027 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2028 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2029 if (min_rev < 0)
1661c76c 2030 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2031 " `%s' extension"), size, base, ase->name);
2032 else
1661c76c 2033 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2034 ase->name, base, size, min_rev);
2035 }
7361da2c
AB
2036 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2037 && (warned_isa & ase->flags) != ase->flags)
2038 {
2039 warned_isa |= ase->flags;
2040 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2041 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2042 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2043 ase->name, base, size, ase->rem_rev);
2044 }
2045
c6278170 2046 if ((ase->flags & FP64_ASES)
0b35dfee 2047 && mips_opts.fp != 64
c6278170
RS
2048 && (warned_fp32 & ase->flags) != ase->flags)
2049 {
2050 warned_fp32 |= ase->flags;
1661c76c 2051 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2052 }
2053}
2054
2055/* Check all enabled ASEs to see whether they are supported by the
2056 chosen architecture. */
2057
2058static void
2059mips_check_isa_supports_ases (void)
2060{
2061 unsigned int i, mask;
2062
2063 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2064 {
2065 mask = mips_ase_mask (mips_ases[i].flags);
2066 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2067 mips_check_isa_supports_ase (&mips_ases[i]);
2068 }
2069}
2070
2071/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2072 that were affected. */
2073
2074static unsigned int
919731af 2075mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2076 bfd_boolean enabled_p)
c6278170
RS
2077{
2078 unsigned int mask;
2079
2080 mask = mips_ase_mask (ase->flags);
919731af 2081 opts->ase &= ~mask;
c6278170 2082 if (enabled_p)
919731af 2083 opts->ase |= ase->flags;
c6278170
RS
2084 return mask;
2085}
2086
2087/* Return the ASE called NAME, or null if none. */
2088
2089static const struct mips_ase *
2090mips_lookup_ase (const char *name)
2091{
2092 unsigned int i;
2093
2094 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2095 if (strcmp (name, mips_ases[i].name) == 0)
2096 return &mips_ases[i];
2097 return NULL;
2098}
2099
df58fc94 2100/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2101 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2102 otherwise it is a 32-bit instruction. */
df58fc94
RS
2103
2104static inline unsigned int
2105micromips_insn_length (const struct mips_opcode *mo)
2106{
2107 return (mo->mask >> 16) == 0 ? 2 : 4;
2108}
2109
5c04167a
RS
2110/* Return the length of MIPS16 instruction OPCODE. */
2111
2112static inline unsigned int
2113mips16_opcode_length (unsigned long opcode)
2114{
2115 return (opcode >> 16) == 0 ? 2 : 4;
2116}
2117
1e915849
RS
2118/* Return the length of instruction INSN. */
2119
2120static inline unsigned int
2121insn_length (const struct mips_cl_insn *insn)
2122{
df58fc94
RS
2123 if (mips_opts.micromips)
2124 return micromips_insn_length (insn->insn_mo);
2125 else if (mips_opts.mips16)
5c04167a 2126 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2127 else
1e915849 2128 return 4;
1e915849
RS
2129}
2130
2131/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2132
2133static void
2134create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2135{
2136 size_t i;
2137
2138 insn->insn_mo = mo;
1e915849
RS
2139 insn->insn_opcode = mo->match;
2140 insn->frag = NULL;
2141 insn->where = 0;
2142 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2143 insn->fixp[i] = NULL;
2144 insn->fixed_p = (mips_opts.noreorder > 0);
2145 insn->noreorder_p = (mips_opts.noreorder > 0);
2146 insn->mips16_absolute_jump_p = 0;
15be625d 2147 insn->complete_p = 0;
e407c74b 2148 insn->cleared_p = 0;
1e915849
RS
2149}
2150
fc76e730
RS
2151/* Get a list of all the operands in INSN. */
2152
2153static const struct mips_operand_array *
2154insn_operands (const struct mips_cl_insn *insn)
2155{
2156 if (insn->insn_mo >= &mips_opcodes[0]
2157 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2158 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2159
2160 if (insn->insn_mo >= &mips16_opcodes[0]
2161 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2162 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2163
2164 if (insn->insn_mo >= &micromips_opcodes[0]
2165 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2166 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2167
2168 abort ();
2169}
2170
2171/* Get a description of operand OPNO of INSN. */
2172
2173static const struct mips_operand *
2174insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2175{
2176 const struct mips_operand_array *operands;
2177
2178 operands = insn_operands (insn);
2179 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2180 abort ();
2181 return operands->operand[opno];
2182}
2183
e077a1c8
RS
2184/* Install UVAL as the value of OPERAND in INSN. */
2185
2186static inline void
2187insn_insert_operand (struct mips_cl_insn *insn,
2188 const struct mips_operand *operand, unsigned int uval)
2189{
2190 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
2191}
2192
fc76e730
RS
2193/* Extract the value of OPERAND from INSN. */
2194
2195static inline unsigned
2196insn_extract_operand (const struct mips_cl_insn *insn,
2197 const struct mips_operand *operand)
2198{
2199 return mips_extract_operand (operand, insn->insn_opcode);
2200}
2201
df58fc94 2202/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2203
2204static void
df58fc94 2205mips_record_compressed_mode (void)
742a56fe
RS
2206{
2207 segment_info_type *si;
2208
2209 si = seg_info (now_seg);
2210 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2211 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2212 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2213 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2214}
2215
4d68580a
RS
2216/* Read a standard MIPS instruction from BUF. */
2217
2218static unsigned long
2219read_insn (char *buf)
2220{
2221 if (target_big_endian)
2222 return bfd_getb32 ((bfd_byte *) buf);
2223 else
2224 return bfd_getl32 ((bfd_byte *) buf);
2225}
2226
2227/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2228 the next byte. */
2229
2230static char *
2231write_insn (char *buf, unsigned int insn)
2232{
2233 md_number_to_chars (buf, insn, 4);
2234 return buf + 4;
2235}
2236
2237/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2238 has length LENGTH. */
2239
2240static unsigned long
2241read_compressed_insn (char *buf, unsigned int length)
2242{
2243 unsigned long insn;
2244 unsigned int i;
2245
2246 insn = 0;
2247 for (i = 0; i < length; i += 2)
2248 {
2249 insn <<= 16;
2250 if (target_big_endian)
2251 insn |= bfd_getb16 ((char *) buf);
2252 else
2253 insn |= bfd_getl16 ((char *) buf);
2254 buf += 2;
2255 }
2256 return insn;
2257}
2258
5c04167a
RS
2259/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2260 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2261
2262static char *
2263write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2264{
2265 unsigned int i;
2266
2267 for (i = 0; i < length; i += 2)
2268 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2269 return buf + length;
2270}
2271
1e915849
RS
2272/* Install INSN at the location specified by its "frag" and "where" fields. */
2273
2274static void
2275install_insn (const struct mips_cl_insn *insn)
2276{
2277 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2278 if (HAVE_CODE_COMPRESSION)
2279 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2280 else
4d68580a 2281 write_insn (f, insn->insn_opcode);
df58fc94 2282 mips_record_compressed_mode ();
1e915849
RS
2283}
2284
2285/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2286 and install the opcode in the new location. */
2287
2288static void
2289move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2290{
2291 size_t i;
2292
2293 insn->frag = frag;
2294 insn->where = where;
2295 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2296 if (insn->fixp[i] != NULL)
2297 {
2298 insn->fixp[i]->fx_frag = frag;
2299 insn->fixp[i]->fx_where = where;
2300 }
2301 install_insn (insn);
2302}
2303
2304/* Add INSN to the end of the output. */
2305
2306static void
2307add_fixed_insn (struct mips_cl_insn *insn)
2308{
2309 char *f = frag_more (insn_length (insn));
2310 move_insn (insn, frag_now, f - frag_now->fr_literal);
2311}
2312
2313/* Start a variant frag and move INSN to the start of the variant part,
2314 marking it as fixed. The other arguments are as for frag_var. */
2315
2316static void
2317add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2318 relax_substateT subtype, symbolS *symbol, offsetT offset)
2319{
2320 frag_grow (max_chars);
2321 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2322 insn->fixed_p = 1;
2323 frag_var (rs_machine_dependent, max_chars, var,
2324 subtype, symbol, offset, NULL);
2325}
2326
2327/* Insert N copies of INSN into the history buffer, starting at
2328 position FIRST. Neither FIRST nor N need to be clipped. */
2329
2330static void
2331insert_into_history (unsigned int first, unsigned int n,
2332 const struct mips_cl_insn *insn)
2333{
2334 if (mips_relax.sequence != 2)
2335 {
2336 unsigned int i;
2337
2338 for (i = ARRAY_SIZE (history); i-- > first;)
2339 if (i >= first + n)
2340 history[i] = history[i - n];
2341 else
2342 history[i] = *insn;
2343 }
2344}
2345
e3de51ce
RS
2346/* Clear the error in insn_error. */
2347
2348static void
2349clear_insn_error (void)
2350{
2351 memset (&insn_error, 0, sizeof (insn_error));
2352}
2353
2354/* Possibly record error message MSG for the current instruction.
2355 If the error is about a particular argument, ARGNUM is the 1-based
2356 number of that argument, otherwise it is 0. FORMAT is the format
2357 of MSG. Return true if MSG was used, false if the current message
2358 was kept. */
2359
2360static bfd_boolean
2361set_insn_error_format (int argnum, enum mips_insn_error_format format,
2362 const char *msg)
2363{
2364 if (argnum == 0)
2365 {
2366 /* Give priority to errors against specific arguments, and to
2367 the first whole-instruction message. */
2368 if (insn_error.msg)
2369 return FALSE;
2370 }
2371 else
2372 {
2373 /* Keep insn_error if it is against a later argument. */
2374 if (argnum < insn_error.min_argnum)
2375 return FALSE;
2376
2377 /* If both errors are against the same argument but are different,
2378 give up on reporting a specific error for this argument.
2379 See the comment about mips_insn_error for details. */
2380 if (argnum == insn_error.min_argnum
2381 && insn_error.msg
2382 && strcmp (insn_error.msg, msg) != 0)
2383 {
2384 insn_error.msg = 0;
2385 insn_error.min_argnum += 1;
2386 return FALSE;
2387 }
2388 }
2389 insn_error.min_argnum = argnum;
2390 insn_error.format = format;
2391 insn_error.msg = msg;
2392 return TRUE;
2393}
2394
2395/* Record an instruction error with no % format fields. ARGNUM and MSG are
2396 as for set_insn_error_format. */
2397
2398static void
2399set_insn_error (int argnum, const char *msg)
2400{
2401 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2402}
2403
2404/* Record an instruction error with one %d field I. ARGNUM and MSG are
2405 as for set_insn_error_format. */
2406
2407static void
2408set_insn_error_i (int argnum, const char *msg, int i)
2409{
2410 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2411 insn_error.u.i = i;
2412}
2413
2414/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2415 are as for set_insn_error_format. */
2416
2417static void
2418set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2419{
2420 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2421 {
2422 insn_error.u.ss[0] = s1;
2423 insn_error.u.ss[1] = s2;
2424 }
2425}
2426
2427/* Report the error in insn_error, which is against assembly code STR. */
2428
2429static void
2430report_insn_error (const char *str)
2431{
e1fa0163 2432 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2433
e3de51ce
RS
2434 switch (insn_error.format)
2435 {
2436 case ERR_FMT_PLAIN:
2437 as_bad (msg, str);
2438 break;
2439
2440 case ERR_FMT_I:
2441 as_bad (msg, insn_error.u.i, str);
2442 break;
2443
2444 case ERR_FMT_SS:
2445 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2446 break;
2447 }
e1fa0163
NC
2448
2449 free ((char *) msg);
e3de51ce
RS
2450}
2451
71400594
RS
2452/* Initialize vr4120_conflicts. There is a bit of duplication here:
2453 the idea is to make it obvious at a glance that each errata is
2454 included. */
2455
2456static void
2457init_vr4120_conflicts (void)
2458{
2459#define CONFLICT(FIRST, SECOND) \
2460 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2461
2462 /* Errata 21 - [D]DIV[U] after [D]MACC */
2463 CONFLICT (MACC, DIV);
2464 CONFLICT (DMACC, DIV);
2465
2466 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2467 CONFLICT (DMULT, DMULT);
2468 CONFLICT (DMULT, DMACC);
2469 CONFLICT (DMACC, DMULT);
2470 CONFLICT (DMACC, DMACC);
2471
2472 /* Errata 24 - MT{LO,HI} after [D]MACC */
2473 CONFLICT (MACC, MTHILO);
2474 CONFLICT (DMACC, MTHILO);
2475
2476 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2477 instruction is executed immediately after a MACC or DMACC
2478 instruction, the result of [either instruction] is incorrect." */
2479 CONFLICT (MACC, MULT);
2480 CONFLICT (MACC, DMULT);
2481 CONFLICT (DMACC, MULT);
2482 CONFLICT (DMACC, DMULT);
2483
2484 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2485 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2486 DDIV or DDIVU instruction, the result of the MACC or
2487 DMACC instruction is incorrect.". */
2488 CONFLICT (DMULT, MACC);
2489 CONFLICT (DMULT, DMACC);
2490 CONFLICT (DIV, MACC);
2491 CONFLICT (DIV, DMACC);
2492
2493#undef CONFLICT
2494}
2495
707bfff6
TS
2496struct regname {
2497 const char *name;
2498 unsigned int num;
2499};
2500
14daeee3 2501#define RNUM_MASK 0x00000ff
56d438b1 2502#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2503#define RTYPE_NUM 0x0000100
2504#define RTYPE_FPU 0x0000200
2505#define RTYPE_FCC 0x0000400
2506#define RTYPE_VEC 0x0000800
2507#define RTYPE_GP 0x0001000
2508#define RTYPE_CP0 0x0002000
2509#define RTYPE_PC 0x0004000
2510#define RTYPE_ACC 0x0008000
2511#define RTYPE_CCC 0x0010000
2512#define RTYPE_VI 0x0020000
2513#define RTYPE_VF 0x0040000
2514#define RTYPE_R5900_I 0x0080000
2515#define RTYPE_R5900_Q 0x0100000
2516#define RTYPE_R5900_R 0x0200000
2517#define RTYPE_R5900_ACC 0x0400000
56d438b1 2518#define RTYPE_MSA 0x0800000
14daeee3 2519#define RWARN 0x8000000
707bfff6
TS
2520
2521#define GENERIC_REGISTER_NUMBERS \
2522 {"$0", RTYPE_NUM | 0}, \
2523 {"$1", RTYPE_NUM | 1}, \
2524 {"$2", RTYPE_NUM | 2}, \
2525 {"$3", RTYPE_NUM | 3}, \
2526 {"$4", RTYPE_NUM | 4}, \
2527 {"$5", RTYPE_NUM | 5}, \
2528 {"$6", RTYPE_NUM | 6}, \
2529 {"$7", RTYPE_NUM | 7}, \
2530 {"$8", RTYPE_NUM | 8}, \
2531 {"$9", RTYPE_NUM | 9}, \
2532 {"$10", RTYPE_NUM | 10}, \
2533 {"$11", RTYPE_NUM | 11}, \
2534 {"$12", RTYPE_NUM | 12}, \
2535 {"$13", RTYPE_NUM | 13}, \
2536 {"$14", RTYPE_NUM | 14}, \
2537 {"$15", RTYPE_NUM | 15}, \
2538 {"$16", RTYPE_NUM | 16}, \
2539 {"$17", RTYPE_NUM | 17}, \
2540 {"$18", RTYPE_NUM | 18}, \
2541 {"$19", RTYPE_NUM | 19}, \
2542 {"$20", RTYPE_NUM | 20}, \
2543 {"$21", RTYPE_NUM | 21}, \
2544 {"$22", RTYPE_NUM | 22}, \
2545 {"$23", RTYPE_NUM | 23}, \
2546 {"$24", RTYPE_NUM | 24}, \
2547 {"$25", RTYPE_NUM | 25}, \
2548 {"$26", RTYPE_NUM | 26}, \
2549 {"$27", RTYPE_NUM | 27}, \
2550 {"$28", RTYPE_NUM | 28}, \
2551 {"$29", RTYPE_NUM | 29}, \
2552 {"$30", RTYPE_NUM | 30}, \
3739860c 2553 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2554
2555#define FPU_REGISTER_NAMES \
2556 {"$f0", RTYPE_FPU | 0}, \
2557 {"$f1", RTYPE_FPU | 1}, \
2558 {"$f2", RTYPE_FPU | 2}, \
2559 {"$f3", RTYPE_FPU | 3}, \
2560 {"$f4", RTYPE_FPU | 4}, \
2561 {"$f5", RTYPE_FPU | 5}, \
2562 {"$f6", RTYPE_FPU | 6}, \
2563 {"$f7", RTYPE_FPU | 7}, \
2564 {"$f8", RTYPE_FPU | 8}, \
2565 {"$f9", RTYPE_FPU | 9}, \
2566 {"$f10", RTYPE_FPU | 10}, \
2567 {"$f11", RTYPE_FPU | 11}, \
2568 {"$f12", RTYPE_FPU | 12}, \
2569 {"$f13", RTYPE_FPU | 13}, \
2570 {"$f14", RTYPE_FPU | 14}, \
2571 {"$f15", RTYPE_FPU | 15}, \
2572 {"$f16", RTYPE_FPU | 16}, \
2573 {"$f17", RTYPE_FPU | 17}, \
2574 {"$f18", RTYPE_FPU | 18}, \
2575 {"$f19", RTYPE_FPU | 19}, \
2576 {"$f20", RTYPE_FPU | 20}, \
2577 {"$f21", RTYPE_FPU | 21}, \
2578 {"$f22", RTYPE_FPU | 22}, \
2579 {"$f23", RTYPE_FPU | 23}, \
2580 {"$f24", RTYPE_FPU | 24}, \
2581 {"$f25", RTYPE_FPU | 25}, \
2582 {"$f26", RTYPE_FPU | 26}, \
2583 {"$f27", RTYPE_FPU | 27}, \
2584 {"$f28", RTYPE_FPU | 28}, \
2585 {"$f29", RTYPE_FPU | 29}, \
2586 {"$f30", RTYPE_FPU | 30}, \
2587 {"$f31", RTYPE_FPU | 31}
2588
2589#define FPU_CONDITION_CODE_NAMES \
2590 {"$fcc0", RTYPE_FCC | 0}, \
2591 {"$fcc1", RTYPE_FCC | 1}, \
2592 {"$fcc2", RTYPE_FCC | 2}, \
2593 {"$fcc3", RTYPE_FCC | 3}, \
2594 {"$fcc4", RTYPE_FCC | 4}, \
2595 {"$fcc5", RTYPE_FCC | 5}, \
2596 {"$fcc6", RTYPE_FCC | 6}, \
2597 {"$fcc7", RTYPE_FCC | 7}
2598
2599#define COPROC_CONDITION_CODE_NAMES \
2600 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2601 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2602 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2603 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2604 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2605 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2606 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2607 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2608
2609#define N32N64_SYMBOLIC_REGISTER_NAMES \
2610 {"$a4", RTYPE_GP | 8}, \
2611 {"$a5", RTYPE_GP | 9}, \
2612 {"$a6", RTYPE_GP | 10}, \
2613 {"$a7", RTYPE_GP | 11}, \
2614 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2615 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2616 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2617 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2618 {"$t0", RTYPE_GP | 12}, \
2619 {"$t1", RTYPE_GP | 13}, \
2620 {"$t2", RTYPE_GP | 14}, \
2621 {"$t3", RTYPE_GP | 15}
2622
2623#define O32_SYMBOLIC_REGISTER_NAMES \
2624 {"$t0", RTYPE_GP | 8}, \
2625 {"$t1", RTYPE_GP | 9}, \
2626 {"$t2", RTYPE_GP | 10}, \
2627 {"$t3", RTYPE_GP | 11}, \
2628 {"$t4", RTYPE_GP | 12}, \
2629 {"$t5", RTYPE_GP | 13}, \
2630 {"$t6", RTYPE_GP | 14}, \
2631 {"$t7", RTYPE_GP | 15}, \
2632 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2633 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2634 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2635 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2636
2637/* Remaining symbolic register names */
2638#define SYMBOLIC_REGISTER_NAMES \
2639 {"$zero", RTYPE_GP | 0}, \
2640 {"$at", RTYPE_GP | 1}, \
2641 {"$AT", RTYPE_GP | 1}, \
2642 {"$v0", RTYPE_GP | 2}, \
2643 {"$v1", RTYPE_GP | 3}, \
2644 {"$a0", RTYPE_GP | 4}, \
2645 {"$a1", RTYPE_GP | 5}, \
2646 {"$a2", RTYPE_GP | 6}, \
2647 {"$a3", RTYPE_GP | 7}, \
2648 {"$s0", RTYPE_GP | 16}, \
2649 {"$s1", RTYPE_GP | 17}, \
2650 {"$s2", RTYPE_GP | 18}, \
2651 {"$s3", RTYPE_GP | 19}, \
2652 {"$s4", RTYPE_GP | 20}, \
2653 {"$s5", RTYPE_GP | 21}, \
2654 {"$s6", RTYPE_GP | 22}, \
2655 {"$s7", RTYPE_GP | 23}, \
2656 {"$t8", RTYPE_GP | 24}, \
2657 {"$t9", RTYPE_GP | 25}, \
2658 {"$k0", RTYPE_GP | 26}, \
2659 {"$kt0", RTYPE_GP | 26}, \
2660 {"$k1", RTYPE_GP | 27}, \
2661 {"$kt1", RTYPE_GP | 27}, \
2662 {"$gp", RTYPE_GP | 28}, \
2663 {"$sp", RTYPE_GP | 29}, \
2664 {"$s8", RTYPE_GP | 30}, \
2665 {"$fp", RTYPE_GP | 30}, \
2666 {"$ra", RTYPE_GP | 31}
2667
2668#define MIPS16_SPECIAL_REGISTER_NAMES \
2669 {"$pc", RTYPE_PC | 0}
2670
2671#define MDMX_VECTOR_REGISTER_NAMES \
2672 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2673 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2674 {"$v2", RTYPE_VEC | 2}, \
2675 {"$v3", RTYPE_VEC | 3}, \
2676 {"$v4", RTYPE_VEC | 4}, \
2677 {"$v5", RTYPE_VEC | 5}, \
2678 {"$v6", RTYPE_VEC | 6}, \
2679 {"$v7", RTYPE_VEC | 7}, \
2680 {"$v8", RTYPE_VEC | 8}, \
2681 {"$v9", RTYPE_VEC | 9}, \
2682 {"$v10", RTYPE_VEC | 10}, \
2683 {"$v11", RTYPE_VEC | 11}, \
2684 {"$v12", RTYPE_VEC | 12}, \
2685 {"$v13", RTYPE_VEC | 13}, \
2686 {"$v14", RTYPE_VEC | 14}, \
2687 {"$v15", RTYPE_VEC | 15}, \
2688 {"$v16", RTYPE_VEC | 16}, \
2689 {"$v17", RTYPE_VEC | 17}, \
2690 {"$v18", RTYPE_VEC | 18}, \
2691 {"$v19", RTYPE_VEC | 19}, \
2692 {"$v20", RTYPE_VEC | 20}, \
2693 {"$v21", RTYPE_VEC | 21}, \
2694 {"$v22", RTYPE_VEC | 22}, \
2695 {"$v23", RTYPE_VEC | 23}, \
2696 {"$v24", RTYPE_VEC | 24}, \
2697 {"$v25", RTYPE_VEC | 25}, \
2698 {"$v26", RTYPE_VEC | 26}, \
2699 {"$v27", RTYPE_VEC | 27}, \
2700 {"$v28", RTYPE_VEC | 28}, \
2701 {"$v29", RTYPE_VEC | 29}, \
2702 {"$v30", RTYPE_VEC | 30}, \
2703 {"$v31", RTYPE_VEC | 31}
2704
14daeee3
RS
2705#define R5900_I_NAMES \
2706 {"$I", RTYPE_R5900_I | 0}
2707
2708#define R5900_Q_NAMES \
2709 {"$Q", RTYPE_R5900_Q | 0}
2710
2711#define R5900_R_NAMES \
2712 {"$R", RTYPE_R5900_R | 0}
2713
2714#define R5900_ACC_NAMES \
2715 {"$ACC", RTYPE_R5900_ACC | 0 }
2716
707bfff6
TS
2717#define MIPS_DSP_ACCUMULATOR_NAMES \
2718 {"$ac0", RTYPE_ACC | 0}, \
2719 {"$ac1", RTYPE_ACC | 1}, \
2720 {"$ac2", RTYPE_ACC | 2}, \
2721 {"$ac3", RTYPE_ACC | 3}
2722
2723static const struct regname reg_names[] = {
2724 GENERIC_REGISTER_NUMBERS,
2725 FPU_REGISTER_NAMES,
2726 FPU_CONDITION_CODE_NAMES,
2727 COPROC_CONDITION_CODE_NAMES,
2728
2729 /* The $txx registers depends on the abi,
2730 these will be added later into the symbol table from
3739860c 2731 one of the tables below once mips_abi is set after
707bfff6
TS
2732 parsing of arguments from the command line. */
2733 SYMBOLIC_REGISTER_NAMES,
2734
2735 MIPS16_SPECIAL_REGISTER_NAMES,
2736 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2737 R5900_I_NAMES,
2738 R5900_Q_NAMES,
2739 R5900_R_NAMES,
2740 R5900_ACC_NAMES,
707bfff6
TS
2741 MIPS_DSP_ACCUMULATOR_NAMES,
2742 {0, 0}
2743};
2744
2745static const struct regname reg_names_o32[] = {
2746 O32_SYMBOLIC_REGISTER_NAMES,
2747 {0, 0}
2748};
2749
2750static const struct regname reg_names_n32n64[] = {
2751 N32N64_SYMBOLIC_REGISTER_NAMES,
2752 {0, 0}
2753};
2754
a92713e6
RS
2755/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2756 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2757 of these register symbols, return the associated vector register,
2758 otherwise return SYMVAL itself. */
df58fc94 2759
a92713e6
RS
2760static unsigned int
2761mips_prefer_vec_regno (unsigned int symval)
707bfff6 2762{
a92713e6
RS
2763 if ((symval & -2) == (RTYPE_GP | 2))
2764 return RTYPE_VEC | (symval & 1);
2765 return symval;
2766}
2767
14daeee3
RS
2768/* Return true if string [S, E) is a valid register name, storing its
2769 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2770
2771static bfd_boolean
14daeee3 2772mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2773{
707bfff6 2774 char save_c;
14daeee3 2775 symbolS *symbol;
707bfff6
TS
2776
2777 /* Terminate name. */
2778 save_c = *e;
2779 *e = '\0';
2780
a92713e6
RS
2781 /* Look up the name. */
2782 symbol = symbol_find (s);
2783 *e = save_c;
2784
2785 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2786 return FALSE;
2787
14daeee3
RS
2788 *symval_ptr = S_GET_VALUE (symbol);
2789 return TRUE;
2790}
2791
2792/* Return true if the string at *SPTR is a valid register name. Allow it
2793 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2794 is nonnull.
2795
2796 When returning true, move *SPTR past the register, store the
2797 register's symbol value in *SYMVAL_PTR and the channel mask in
2798 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2799 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2800 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2801
2802static bfd_boolean
2803mips_parse_register (char **sptr, unsigned int *symval_ptr,
2804 unsigned int *channels_ptr)
2805{
2806 char *s, *e, *m;
2807 const char *q;
2808 unsigned int channels, symval, bit;
2809
2810 /* Find end of name. */
2811 s = e = *sptr;
2812 if (is_name_beginner (*e))
2813 ++e;
2814 while (is_part_of_name (*e))
2815 ++e;
2816
2817 channels = 0;
2818 if (!mips_parse_register_1 (s, e, &symval))
2819 {
2820 if (!channels_ptr)
2821 return FALSE;
2822
2823 /* Eat characters from the end of the string that are valid
2824 channel suffixes. The preceding register must be $ACC or
2825 end with a digit, so there is no ambiguity. */
2826 bit = 1;
2827 m = e;
2828 for (q = "wzyx"; *q; q++, bit <<= 1)
2829 if (m > s && m[-1] == *q)
2830 {
2831 --m;
2832 channels |= bit;
2833 }
2834
2835 if (channels == 0
2836 || !mips_parse_register_1 (s, m, &symval)
2837 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2838 return FALSE;
2839 }
2840
a92713e6 2841 *sptr = e;
14daeee3
RS
2842 *symval_ptr = symval;
2843 if (channels_ptr)
2844 *channels_ptr = channels;
a92713e6
RS
2845 return TRUE;
2846}
2847
2848/* Check if SPTR points at a valid register specifier according to TYPES.
2849 If so, then return 1, advance S to consume the specifier and store
2850 the register's number in REGNOP, otherwise return 0. */
2851
2852static int
2853reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2854{
2855 unsigned int regno;
2856
14daeee3 2857 if (mips_parse_register (s, &regno, NULL))
707bfff6 2858 {
a92713e6
RS
2859 if (types & RTYPE_VEC)
2860 regno = mips_prefer_vec_regno (regno);
2861 if (regno & types)
2862 regno &= RNUM_MASK;
2863 else
2864 regno = ~0;
707bfff6 2865 }
a92713e6 2866 else
707bfff6 2867 {
a92713e6 2868 if (types & RWARN)
1661c76c 2869 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2870 regno = ~0;
707bfff6 2871 }
707bfff6 2872 if (regnop)
a92713e6
RS
2873 *regnop = regno;
2874 return regno <= RNUM_MASK;
707bfff6
TS
2875}
2876
14daeee3
RS
2877/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2878 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2879
2880static char *
2881mips_parse_vu0_channels (char *s, unsigned int *channels)
2882{
2883 unsigned int i;
2884
2885 *channels = 0;
2886 for (i = 0; i < 4; i++)
2887 if (*s == "xyzw"[i])
2888 {
2889 *channels |= 1 << (3 - i);
2890 ++s;
2891 }
2892 return s;
2893}
2894
a92713e6
RS
2895/* Token types for parsed operand lists. */
2896enum mips_operand_token_type {
2897 /* A plain register, e.g. $f2. */
2898 OT_REG,
df58fc94 2899
14daeee3
RS
2900 /* A 4-bit XYZW channel mask. */
2901 OT_CHANNELS,
2902
56d438b1
CF
2903 /* A constant vector index, e.g. [1]. */
2904 OT_INTEGER_INDEX,
2905
2906 /* A register vector index, e.g. [$2]. */
2907 OT_REG_INDEX,
df58fc94 2908
a92713e6
RS
2909 /* A continuous range of registers, e.g. $s0-$s4. */
2910 OT_REG_RANGE,
2911
2912 /* A (possibly relocated) expression. */
2913 OT_INTEGER,
2914
2915 /* A floating-point value. */
2916 OT_FLOAT,
2917
2918 /* A single character. This can be '(', ')' or ',', but '(' only appears
2919 before OT_REGs. */
2920 OT_CHAR,
2921
14daeee3
RS
2922 /* A doubled character, either "--" or "++". */
2923 OT_DOUBLE_CHAR,
2924
a92713e6
RS
2925 /* The end of the operand list. */
2926 OT_END
2927};
2928
2929/* A parsed operand token. */
2930struct mips_operand_token
2931{
2932 /* The type of token. */
2933 enum mips_operand_token_type type;
2934 union
2935 {
56d438b1 2936 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
2937 unsigned int regno;
2938
14daeee3
RS
2939 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
2940 unsigned int channels;
2941
56d438b1
CF
2942 /* The integer value of an OT_INTEGER_INDEX. */
2943 addressT index;
a92713e6
RS
2944
2945 /* The two register symbol values involved in an OT_REG_RANGE. */
2946 struct {
2947 unsigned int regno1;
2948 unsigned int regno2;
2949 } reg_range;
2950
2951 /* The value of an OT_INTEGER. The value is represented as an
2952 expression and the relocation operators that were applied to
2953 that expression. The reloc entries are BFD_RELOC_UNUSED if no
2954 relocation operators were used. */
2955 struct {
2956 expressionS value;
2957 bfd_reloc_code_real_type relocs[3];
2958 } integer;
2959
2960 /* The binary data for an OT_FLOAT constant, and the number of bytes
2961 in the constant. */
2962 struct {
2963 unsigned char data[8];
2964 int length;
2965 } flt;
2966
14daeee3 2967 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
2968 char ch;
2969 } u;
2970};
2971
2972/* An obstack used to construct lists of mips_operand_tokens. */
2973static struct obstack mips_operand_tokens;
2974
2975/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
2976
2977static void
2978mips_add_token (struct mips_operand_token *token,
2979 enum mips_operand_token_type type)
2980{
2981 token->type = type;
2982 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
2983}
2984
2985/* Check whether S is '(' followed by a register name. Add OT_CHAR
2986 and OT_REG tokens for them if so, and return a pointer to the first
2987 unconsumed character. Return null otherwise. */
2988
2989static char *
2990mips_parse_base_start (char *s)
2991{
2992 struct mips_operand_token token;
14daeee3
RS
2993 unsigned int regno, channels;
2994 bfd_boolean decrement_p;
df58fc94 2995
a92713e6
RS
2996 if (*s != '(')
2997 return 0;
2998
2999 ++s;
3000 SKIP_SPACE_TABS (s);
14daeee3
RS
3001
3002 /* Only match "--" as part of a base expression. In other contexts "--X"
3003 is a double negative. */
3004 decrement_p = (s[0] == '-' && s[1] == '-');
3005 if (decrement_p)
3006 {
3007 s += 2;
3008 SKIP_SPACE_TABS (s);
3009 }
3010
3011 /* Allow a channel specifier because that leads to better error messages
3012 than treating something like "$vf0x++" as an expression. */
3013 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3014 return 0;
3015
3016 token.u.ch = '(';
3017 mips_add_token (&token, OT_CHAR);
3018
14daeee3
RS
3019 if (decrement_p)
3020 {
3021 token.u.ch = '-';
3022 mips_add_token (&token, OT_DOUBLE_CHAR);
3023 }
3024
a92713e6
RS
3025 token.u.regno = regno;
3026 mips_add_token (&token, OT_REG);
3027
14daeee3
RS
3028 if (channels)
3029 {
3030 token.u.channels = channels;
3031 mips_add_token (&token, OT_CHANNELS);
3032 }
3033
3034 /* For consistency, only match "++" as part of base expressions too. */
3035 SKIP_SPACE_TABS (s);
3036 if (s[0] == '+' && s[1] == '+')
3037 {
3038 s += 2;
3039 token.u.ch = '+';
3040 mips_add_token (&token, OT_DOUBLE_CHAR);
3041 }
3042
a92713e6
RS
3043 return s;
3044}
3045
3046/* Parse one or more tokens from S. Return a pointer to the first
3047 unconsumed character on success. Return null if an error was found
3048 and store the error text in insn_error. FLOAT_FORMAT is as for
3049 mips_parse_arguments. */
3050
3051static char *
3052mips_parse_argument_token (char *s, char float_format)
3053{
6d4af3c2
AM
3054 char *end, *save_in;
3055 const char *err;
14daeee3 3056 unsigned int regno1, regno2, channels;
a92713e6
RS
3057 struct mips_operand_token token;
3058
3059 /* First look for "($reg", since we want to treat that as an
3060 OT_CHAR and OT_REG rather than an expression. */
3061 end = mips_parse_base_start (s);
3062 if (end)
3063 return end;
3064
3065 /* Handle other characters that end up as OT_CHARs. */
3066 if (*s == ')' || *s == ',')
3067 {
3068 token.u.ch = *s;
3069 mips_add_token (&token, OT_CHAR);
3070 ++s;
3071 return s;
3072 }
3073
3074 /* Handle tokens that start with a register. */
14daeee3 3075 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3076 {
14daeee3
RS
3077 if (channels)
3078 {
3079 /* A register and a VU0 channel suffix. */
3080 token.u.regno = regno1;
3081 mips_add_token (&token, OT_REG);
3082
3083 token.u.channels = channels;
3084 mips_add_token (&token, OT_CHANNELS);
3085 return s;
3086 }
3087
a92713e6
RS
3088 SKIP_SPACE_TABS (s);
3089 if (*s == '-')
df58fc94 3090 {
a92713e6
RS
3091 /* A register range. */
3092 ++s;
3093 SKIP_SPACE_TABS (s);
14daeee3 3094 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3095 {
1661c76c 3096 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3097 return 0;
3098 }
df58fc94 3099
a92713e6
RS
3100 token.u.reg_range.regno1 = regno1;
3101 token.u.reg_range.regno2 = regno2;
3102 mips_add_token (&token, OT_REG_RANGE);
3103 return s;
3104 }
a92713e6 3105
56d438b1
CF
3106 /* Add the register itself. */
3107 token.u.regno = regno1;
3108 mips_add_token (&token, OT_REG);
3109
3110 /* Check for a vector index. */
3111 if (*s == '[')
3112 {
a92713e6
RS
3113 ++s;
3114 SKIP_SPACE_TABS (s);
56d438b1
CF
3115 if (mips_parse_register (&s, &token.u.regno, NULL))
3116 mips_add_token (&token, OT_REG_INDEX);
3117 else
a92713e6 3118 {
56d438b1
CF
3119 expressionS element;
3120
3121 my_getExpression (&element, s);
3122 if (element.X_op != O_constant)
3123 {
3124 set_insn_error (0, _("vector element must be constant"));
3125 return 0;
3126 }
3127 s = expr_end;
3128 token.u.index = element.X_add_number;
3129 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3130 }
a92713e6
RS
3131 SKIP_SPACE_TABS (s);
3132 if (*s != ']')
3133 {
1661c76c 3134 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3135 return 0;
3136 }
3137 ++s;
df58fc94 3138 }
a92713e6 3139 return s;
df58fc94
RS
3140 }
3141
a92713e6
RS
3142 if (float_format)
3143 {
3144 /* First try to treat expressions as floats. */
3145 save_in = input_line_pointer;
3146 input_line_pointer = s;
3147 err = md_atof (float_format, (char *) token.u.flt.data,
3148 &token.u.flt.length);
3149 end = input_line_pointer;
3150 input_line_pointer = save_in;
3151 if (err && *err)
3152 {
e3de51ce 3153 set_insn_error (0, err);
a92713e6
RS
3154 return 0;
3155 }
3156 if (s != end)
3157 {
3158 mips_add_token (&token, OT_FLOAT);
3159 return end;
3160 }
3161 }
3162
3163 /* Treat everything else as an integer expression. */
3164 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3165 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3166 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3167 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3168 s = expr_end;
3169 mips_add_token (&token, OT_INTEGER);
3170 return s;
3171}
3172
3173/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3174 if expressions should be treated as 32-bit floating-point constants,
3175 'd' if they should be treated as 64-bit floating-point constants,
3176 or 0 if they should be treated as integer expressions (the usual case).
3177
3178 Return a list of tokens on success, otherwise return 0. The caller
3179 must obstack_free the list after use. */
3180
3181static struct mips_operand_token *
3182mips_parse_arguments (char *s, char float_format)
3183{
3184 struct mips_operand_token token;
3185
3186 SKIP_SPACE_TABS (s);
3187 while (*s)
3188 {
3189 s = mips_parse_argument_token (s, float_format);
3190 if (!s)
3191 {
3192 obstack_free (&mips_operand_tokens,
3193 obstack_finish (&mips_operand_tokens));
3194 return 0;
3195 }
3196 SKIP_SPACE_TABS (s);
3197 }
3198 mips_add_token (&token, OT_END);
3199 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3200}
3201
d301a56b
RS
3202/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3203 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3204
3205static bfd_boolean
f79e2745 3206is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3207{
3208 int isa = mips_opts.isa;
846ef2d0 3209 int ase = mips_opts.ase;
037b32b9 3210 int fp_s, fp_d;
c6278170 3211 unsigned int i;
037b32b9 3212
c6278170
RS
3213 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
3214 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3215 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3216 ase |= mips_ases[i].flags64;
037b32b9 3217
d301a56b 3218 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3219 return FALSE;
3220
3221 /* Check whether the instruction or macro requires single-precision or
3222 double-precision floating-point support. Note that this information is
3223 stored differently in the opcode table for insns and macros. */
3224 if (mo->pinfo == INSN_MACRO)
3225 {
3226 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3227 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3228 }
3229 else
3230 {
3231 fp_s = mo->pinfo & FP_S;
3232 fp_d = mo->pinfo & FP_D;
3233 }
3234
3235 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3236 return FALSE;
3237
3238 if (fp_s && mips_opts.soft_float)
3239 return FALSE;
3240
3241 return TRUE;
3242}
3243
3244/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3245 selected ISA and architecture. */
3246
3247static bfd_boolean
3248is_opcode_valid_16 (const struct mips_opcode *mo)
3249{
d301a56b 3250 return opcode_is_member (mo, mips_opts.isa, 0, mips_opts.arch);
037b32b9
AN
3251}
3252
df58fc94
RS
3253/* Return TRUE if the size of the microMIPS opcode MO matches one
3254 explicitly requested. Always TRUE in the standard MIPS mode. */
3255
3256static bfd_boolean
3257is_size_valid (const struct mips_opcode *mo)
3258{
3259 if (!mips_opts.micromips)
3260 return TRUE;
3261
833794fc
MR
3262 if (mips_opts.insn32)
3263 {
3264 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3265 return FALSE;
3266 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3267 return FALSE;
3268 }
df58fc94
RS
3269 if (!forced_insn_length)
3270 return TRUE;
3271 if (mo->pinfo == INSN_MACRO)
3272 return FALSE;
3273 return forced_insn_length == micromips_insn_length (mo);
3274}
3275
3276/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3277 of the preceding instruction. Always TRUE in the standard MIPS mode.
3278
3279 We don't accept macros in 16-bit delay slots to avoid a case where
3280 a macro expansion fails because it relies on a preceding 32-bit real
3281 instruction to have matched and does not handle the operands correctly.
3282 The only macros that may expand to 16-bit instructions are JAL that
3283 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3284 and BGT (that likewise cannot be placed in a delay slot) that decay to
3285 a NOP. In all these cases the macros precede any corresponding real
3286 instruction definitions in the opcode table, so they will match in the
3287 second pass where the size of the delay slot is ignored and therefore
3288 produce correct code. */
df58fc94
RS
3289
3290static bfd_boolean
3291is_delay_slot_valid (const struct mips_opcode *mo)
3292{
3293 if (!mips_opts.micromips)
3294 return TRUE;
3295
3296 if (mo->pinfo == INSN_MACRO)
c06dec14 3297 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3298 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3299 && micromips_insn_length (mo) != 4)
3300 return FALSE;
3301 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3302 && micromips_insn_length (mo) != 2)
3303 return FALSE;
3304
3305 return TRUE;
3306}
3307
fc76e730
RS
3308/* For consistency checking, verify that all bits of OPCODE are specified
3309 either by the match/mask part of the instruction definition, or by the
3310 operand list. Also build up a list of operands in OPERANDS.
3311
3312 INSN_BITS says which bits of the instruction are significant.
3313 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3314 provides the mips_operand description of each operand. DECODE_OPERAND
3315 is null for MIPS16 instructions. */
ab902481
RS
3316
3317static int
3318validate_mips_insn (const struct mips_opcode *opcode,
3319 unsigned long insn_bits,
fc76e730
RS
3320 const struct mips_operand *(*decode_operand) (const char *),
3321 struct mips_operand_array *operands)
ab902481
RS
3322{
3323 const char *s;
fc76e730 3324 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3325 const struct mips_operand *operand;
3326
fc76e730
RS
3327 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3328 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3329 {
3330 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3331 opcode->name, opcode->args);
3332 return 0;
3333 }
3334 used_bits = 0;
fc76e730 3335 opno = 0;
14daeee3
RS
3336 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3337 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3338 for (s = opcode->args; *s; ++s)
3339 switch (*s)
3340 {
3341 case ',':
3342 case '(':
3343 case ')':
3344 break;
3345
14daeee3
RS
3346 case '#':
3347 s++;
3348 break;
3349
ab902481 3350 default:
fc76e730
RS
3351 if (!decode_operand)
3352 operand = decode_mips16_operand (*s, FALSE);
3353 else
3354 operand = decode_operand (s);
3355 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3356 {
3357 as_bad (_("internal: unknown operand type: %s %s"),
3358 opcode->name, opcode->args);
3359 return 0;
3360 }
fc76e730
RS
3361 gas_assert (opno < MAX_OPERANDS);
3362 operands->operand[opno] = operand;
14daeee3 3363 if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3364 {
14daeee3 3365 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3366 if (operand->type == OP_MDMX_IMM_REG)
3367 /* Bit 5 is the format selector (OB vs QH). The opcode table
3368 has separate entries for each format. */
3369 used_bits &= ~(1 << (operand->lsb + 5));
3370 if (operand->type == OP_ENTRY_EXIT_LIST)
3371 used_bits &= ~(mask & 0x700);
3372 }
ab902481 3373 /* Skip prefix characters. */
7361da2c 3374 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3375 ++s;
fc76e730 3376 opno += 1;
ab902481
RS
3377 break;
3378 }
fc76e730 3379 doubled = used_bits & mask & insn_bits;
ab902481
RS
3380 if (doubled)
3381 {
3382 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3383 " %s %s"), doubled, opcode->name, opcode->args);
3384 return 0;
3385 }
fc76e730 3386 used_bits |= mask;
ab902481 3387 undefined = ~used_bits & insn_bits;
fc76e730 3388 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3389 {
3390 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3391 undefined, opcode->name, opcode->args);
3392 return 0;
3393 }
3394 used_bits &= ~insn_bits;
3395 if (used_bits)
3396 {
3397 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3398 used_bits, opcode->name, opcode->args);
3399 return 0;
3400 }
3401 return 1;
3402}
3403
fc76e730
RS
3404/* The MIPS16 version of validate_mips_insn. */
3405
3406static int
3407validate_mips16_insn (const struct mips_opcode *opcode,
3408 struct mips_operand_array *operands)
3409{
3410 if (opcode->args[0] == 'a' || opcode->args[0] == 'i')
3411 {
3412 /* In this case OPCODE defines the first 16 bits in a 32-bit jump
3413 instruction. Use TMP to describe the full instruction. */
3414 struct mips_opcode tmp;
3415
3416 tmp = *opcode;
3417 tmp.match <<= 16;
3418 tmp.mask <<= 16;
3419 return validate_mips_insn (&tmp, 0xffffffff, 0, operands);
3420 }
3421 return validate_mips_insn (opcode, 0xffff, 0, operands);
3422}
3423
ab902481
RS
3424/* The microMIPS version of validate_mips_insn. */
3425
3426static int
fc76e730
RS
3427validate_micromips_insn (const struct mips_opcode *opc,
3428 struct mips_operand_array *operands)
ab902481
RS
3429{
3430 unsigned long insn_bits;
3431 unsigned long major;
3432 unsigned int length;
3433
fc76e730
RS
3434 if (opc->pinfo == INSN_MACRO)
3435 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3436 operands);
3437
ab902481
RS
3438 length = micromips_insn_length (opc);
3439 if (length != 2 && length != 4)
3440 {
1661c76c 3441 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3442 "%s %s"), length, opc->name, opc->args);
3443 return 0;
3444 }
3445 major = opc->match >> (10 + 8 * (length - 2));
3446 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3447 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3448 {
1661c76c 3449 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3450 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3451 return 0;
3452 }
3453
3454 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3455 insn_bits = 1 << 4 * length;
3456 insn_bits <<= 4 * length;
3457 insn_bits -= 1;
fc76e730
RS
3458 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3459 operands);
ab902481
RS
3460}
3461
707bfff6
TS
3462/* This function is called once, at assembler startup time. It should set up
3463 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3464
252b5132 3465void
17a2f251 3466md_begin (void)
252b5132 3467{
3994f87e 3468 const char *retval = NULL;
156c2f8b 3469 int i = 0;
252b5132 3470 int broken = 0;
1f25f5d3 3471
0a44bf69
RS
3472 if (mips_pic != NO_PIC)
3473 {
3474 if (g_switch_seen && g_switch_value != 0)
3475 as_bad (_("-G may not be used in position-independent code"));
3476 g_switch_value = 0;
3477 }
00acd688
CM
3478 else if (mips_abicalls)
3479 {
3480 if (g_switch_seen && g_switch_value != 0)
3481 as_bad (_("-G may not be used with abicalls"));
3482 g_switch_value = 0;
3483 }
0a44bf69 3484
0b35dfee 3485 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3486 as_warn (_("could not set architecture and machine"));
252b5132 3487
252b5132
RH
3488 op_hash = hash_new ();
3489
fc76e730 3490 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3491 for (i = 0; i < NUMOPCODES;)
3492 {
3493 const char *name = mips_opcodes[i].name;
3494
17a2f251 3495 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3496 if (retval != NULL)
3497 {
3498 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3499 mips_opcodes[i].name, retval);
3500 /* Probably a memory allocation problem? Give up now. */
1661c76c 3501 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3502 }
3503 do
3504 {
fc76e730
RS
3505 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3506 decode_mips_operand, &mips_operands[i]))
3507 broken = 1;
3508 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3509 {
fc76e730
RS
3510 create_insn (&nop_insn, mips_opcodes + i);
3511 if (mips_fix_loongson2f_nop)
3512 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3513 nop_insn.fixed_p = 1;
252b5132
RH
3514 }
3515 ++i;
3516 }
3517 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3518 }
3519
3520 mips16_op_hash = hash_new ();
fc76e730
RS
3521 mips16_operands = XCNEWVEC (struct mips_operand_array,
3522 bfd_mips16_num_opcodes);
252b5132
RH
3523
3524 i = 0;
3525 while (i < bfd_mips16_num_opcodes)
3526 {
3527 const char *name = mips16_opcodes[i].name;
3528
17a2f251 3529 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3530 if (retval != NULL)
3531 as_fatal (_("internal: can't hash `%s': %s"),
3532 mips16_opcodes[i].name, retval);
3533 do
3534 {
fc76e730
RS
3535 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3536 broken = 1;
1e915849
RS
3537 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3538 {
3539 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3540 mips16_nop_insn.fixed_p = 1;
3541 }
252b5132
RH
3542 ++i;
3543 }
3544 while (i < bfd_mips16_num_opcodes
3545 && strcmp (mips16_opcodes[i].name, name) == 0);
3546 }
3547
df58fc94 3548 micromips_op_hash = hash_new ();
fc76e730
RS
3549 micromips_operands = XCNEWVEC (struct mips_operand_array,
3550 bfd_micromips_num_opcodes);
df58fc94
RS
3551
3552 i = 0;
3553 while (i < bfd_micromips_num_opcodes)
3554 {
3555 const char *name = micromips_opcodes[i].name;
3556
3557 retval = hash_insert (micromips_op_hash, name,
3558 (void *) &micromips_opcodes[i]);
3559 if (retval != NULL)
3560 as_fatal (_("internal: can't hash `%s': %s"),
3561 micromips_opcodes[i].name, retval);
3562 do
fc76e730
RS
3563 {
3564 struct mips_cl_insn *micromips_nop_insn;
3565
3566 if (!validate_micromips_insn (&micromips_opcodes[i],
3567 &micromips_operands[i]))
3568 broken = 1;
3569
3570 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3571 {
3572 if (micromips_insn_length (micromips_opcodes + i) == 2)
3573 micromips_nop_insn = &micromips_nop16_insn;
3574 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3575 micromips_nop_insn = &micromips_nop32_insn;
3576 else
3577 continue;
3578
3579 if (micromips_nop_insn->insn_mo == NULL
3580 && strcmp (name, "nop") == 0)
3581 {
3582 create_insn (micromips_nop_insn, micromips_opcodes + i);
3583 micromips_nop_insn->fixed_p = 1;
3584 }
3585 }
3586 }
df58fc94
RS
3587 while (++i < bfd_micromips_num_opcodes
3588 && strcmp (micromips_opcodes[i].name, name) == 0);
3589 }
3590
252b5132 3591 if (broken)
1661c76c 3592 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3593
3594 /* We add all the general register names to the symbol table. This
3595 helps us detect invalid uses of them. */
3739860c 3596 for (i = 0; reg_names[i].name; i++)
707bfff6 3597 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3598 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3599 &zero_address_frag));
3600 if (HAVE_NEWABI)
3739860c 3601 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3602 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3603 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3604 &zero_address_frag));
707bfff6 3605 else
3739860c 3606 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3607 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3608 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3609 &zero_address_frag));
6047c971 3610
14daeee3
RS
3611 for (i = 0; i < 32; i++)
3612 {
92fce9bd 3613 char regname[6];
14daeee3
RS
3614
3615 /* R5900 VU0 floating-point register. */
92fce9bd 3616 sprintf (regname, "$vf%d", i);
14daeee3
RS
3617 symbol_table_insert (symbol_new (regname, reg_section,
3618 RTYPE_VF | i, &zero_address_frag));
3619
3620 /* R5900 VU0 integer register. */
92fce9bd 3621 sprintf (regname, "$vi%d", i);
14daeee3
RS
3622 symbol_table_insert (symbol_new (regname, reg_section,
3623 RTYPE_VI | i, &zero_address_frag));
3624
56d438b1 3625 /* MSA register. */
92fce9bd 3626 sprintf (regname, "$w%d", i);
56d438b1
CF
3627 symbol_table_insert (symbol_new (regname, reg_section,
3628 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3629 }
3630
a92713e6
RS
3631 obstack_init (&mips_operand_tokens);
3632
7d10b47d 3633 mips_no_prev_insn ();
252b5132
RH
3634
3635 mips_gprmask = 0;
3636 mips_cprmask[0] = 0;
3637 mips_cprmask[1] = 0;
3638 mips_cprmask[2] = 0;
3639 mips_cprmask[3] = 0;
3640
3641 /* set the default alignment for the text section (2**2) */
3642 record_alignment (text_section, 2);
3643
4d0d148d 3644 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3645
f3ded42a
RS
3646 /* On a native system other than VxWorks, sections must be aligned
3647 to 16 byte boundaries. When configured for an embedded ELF
3648 target, we don't bother. */
3649 if (strncmp (TARGET_OS, "elf", 3) != 0
3650 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3651 {
f3ded42a
RS
3652 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3653 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3654 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3655 }
252b5132 3656
f3ded42a
RS
3657 /* Create a .reginfo section for register masks and a .mdebug
3658 section for debugging information. */
3659 {
3660 segT seg;
3661 subsegT subseg;
3662 flagword flags;
3663 segT sec;
3664
3665 seg = now_seg;
3666 subseg = now_subseg;
3667
3668 /* The ABI says this section should be loaded so that the
3669 running program can access it. However, we don't load it
3670 if we are configured for an embedded target */
3671 flags = SEC_READONLY | SEC_DATA;
3672 if (strncmp (TARGET_OS, "elf", 3) != 0)
3673 flags |= SEC_ALLOC | SEC_LOAD;
3674
3675 if (mips_abi != N64_ABI)
252b5132 3676 {
f3ded42a 3677 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3678
f3ded42a
RS
3679 bfd_set_section_flags (stdoutput, sec, flags);
3680 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3681
f3ded42a
RS
3682 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3683 }
3684 else
3685 {
3686 /* The 64-bit ABI uses a .MIPS.options section rather than
3687 .reginfo section. */
3688 sec = subseg_new (".MIPS.options", (subsegT) 0);
3689 bfd_set_section_flags (stdoutput, sec, flags);
3690 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3691
f3ded42a
RS
3692 /* Set up the option header. */
3693 {
3694 Elf_Internal_Options opthdr;
3695 char *f;
3696
3697 opthdr.kind = ODK_REGINFO;
3698 opthdr.size = (sizeof (Elf_External_Options)
3699 + sizeof (Elf64_External_RegInfo));
3700 opthdr.section = 0;
3701 opthdr.info = 0;
3702 f = frag_more (sizeof (Elf_External_Options));
3703 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3704 (Elf_External_Options *) f);
3705
3706 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3707 }
3708 }
252b5132 3709
351cdf24
MF
3710 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3711 bfd_set_section_flags (stdoutput, sec,
3712 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3713 bfd_set_section_alignment (stdoutput, sec, 3);
3714 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3715
f3ded42a
RS
3716 if (ECOFF_DEBUGGING)
3717 {
3718 sec = subseg_new (".mdebug", (subsegT) 0);
3719 (void) bfd_set_section_flags (stdoutput, sec,
3720 SEC_HAS_CONTENTS | SEC_READONLY);
3721 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3722 }
f3ded42a
RS
3723 else if (mips_flag_pdr)
3724 {
3725 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3726 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3727 SEC_READONLY | SEC_RELOC
3728 | SEC_DEBUGGING);
3729 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3730 }
3731
3732 subseg_set (seg, subseg);
3733 }
252b5132 3734
71400594
RS
3735 if (mips_fix_vr4120)
3736 init_vr4120_conflicts ();
252b5132
RH
3737}
3738
351cdf24
MF
3739static inline void
3740fpabi_incompatible_with (int fpabi, const char *what)
3741{
3742 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3743 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3744}
3745
3746static inline void
3747fpabi_requires (int fpabi, const char *what)
3748{
3749 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3750 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3751}
3752
3753/* Check -mabi and register sizes against the specified FP ABI. */
3754static void
3755check_fpabi (int fpabi)
3756{
351cdf24
MF
3757 switch (fpabi)
3758 {
3759 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3760 if (file_mips_opts.soft_float)
3761 fpabi_incompatible_with (fpabi, "softfloat");
3762 else if (file_mips_opts.single_float)
3763 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3764 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3765 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3766 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3767 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3768 break;
3769
3770 case Val_GNU_MIPS_ABI_FP_XX:
3771 if (mips_abi != O32_ABI)
3772 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3773 else if (file_mips_opts.soft_float)
3774 fpabi_incompatible_with (fpabi, "softfloat");
3775 else if (file_mips_opts.single_float)
3776 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3777 else if (file_mips_opts.fp != 0)
3778 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3779 break;
3780
3781 case Val_GNU_MIPS_ABI_FP_64A:
3782 case Val_GNU_MIPS_ABI_FP_64:
3783 if (mips_abi != O32_ABI)
3784 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3785 else if (file_mips_opts.soft_float)
3786 fpabi_incompatible_with (fpabi, "softfloat");
3787 else if (file_mips_opts.single_float)
3788 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3789 else if (file_mips_opts.fp != 64)
3790 fpabi_requires (fpabi, "fp=64");
3791 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3792 fpabi_incompatible_with (fpabi, "nooddspreg");
3793 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3794 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3795 break;
3796
3797 case Val_GNU_MIPS_ABI_FP_SINGLE:
3798 if (file_mips_opts.soft_float)
3799 fpabi_incompatible_with (fpabi, "softfloat");
3800 else if (!file_mips_opts.single_float)
3801 fpabi_requires (fpabi, "singlefloat");
3802 break;
3803
3804 case Val_GNU_MIPS_ABI_FP_SOFT:
3805 if (!file_mips_opts.soft_float)
3806 fpabi_requires (fpabi, "softfloat");
3807 break;
3808
3809 case Val_GNU_MIPS_ABI_FP_OLD_64:
3810 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3811 Tag_GNU_MIPS_ABI_FP, fpabi);
3812 break;
3813
3350cc01
CM
3814 case Val_GNU_MIPS_ABI_FP_NAN2008:
3815 /* Silently ignore compatibility value. */
3816 break;
3817
351cdf24
MF
3818 default:
3819 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3820 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3821 break;
3822 }
351cdf24
MF
3823}
3824
919731af 3825/* Perform consistency checks on the current options. */
3826
3827static void
3828mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3829{
3830 /* Check the size of integer registers agrees with the ABI and ISA. */
3831 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3832 as_bad (_("`gp=64' used with a 32-bit processor"));
3833 else if (abi_checks
3834 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3835 as_bad (_("`gp=32' used with a 64-bit ABI"));
3836 else if (abi_checks
3837 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3838 as_bad (_("`gp=64' used with a 32-bit ABI"));
3839
3840 /* Check the size of the float registers agrees with the ABI and ISA. */
3841 switch (opts->fp)
3842 {
351cdf24
MF
3843 case 0:
3844 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3845 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3846 else if (opts->single_float == 1)
3847 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3848 break;
919731af 3849 case 64:
3850 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3851 as_bad (_("`fp=64' used with a 32-bit fpu"));
3852 else if (abi_checks
3853 && ABI_NEEDS_32BIT_REGS (mips_abi)
3854 && !ISA_HAS_MXHC1 (opts->isa))
3855 as_warn (_("`fp=64' used with a 32-bit ABI"));
3856 break;
3857 case 32:
3858 if (abi_checks
3859 && ABI_NEEDS_64BIT_REGS (mips_abi))
3860 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3861 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3862 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3863 break;
3864 default:
3865 as_bad (_("Unknown size of floating point registers"));
3866 break;
3867 }
3868
351cdf24
MF
3869 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3870 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3871
919731af 3872 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3873 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3874 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3875 && (opts->micromips == 1
3876 || opts->mips16 == 1))
1357373c 3877 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3878 opts->micromips ? "micromips" : "mips16",
5f4678bb 3879 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3880
3881 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
3882 as_fatal (_("branch relaxation is not supported in `%s'"),
3883 mips_cpu_info_from_isa (opts->isa)->name);
919731af 3884}
3885
3886/* Perform consistency checks on the module level options exactly once.
3887 This is a deferred check that happens:
3888 at the first .set directive
3889 or, at the first pseudo op that generates code (inc .dc.a)
3890 or, at the first instruction
3891 or, at the end. */
3892
3893static void
3894file_mips_check_options (void)
3895{
3896 const struct mips_cpu_info *arch_info = 0;
3897
3898 if (file_mips_opts_checked)
3899 return;
3900
3901 /* The following code determines the register size.
3902 Similar code was added to GCC 3.3 (see override_options() in
3903 config/mips/mips.c). The GAS and GCC code should be kept in sync
3904 as much as possible. */
3905
3906 if (file_mips_opts.gp < 0)
3907 {
3908 /* Infer the integer register size from the ABI and processor.
3909 Restrict ourselves to 32-bit registers if that's all the
3910 processor has, or if the ABI cannot handle 64-bit registers. */
3911 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
3912 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
3913 ? 32 : 64;
3914 }
3915
3916 if (file_mips_opts.fp < 0)
3917 {
3918 /* No user specified float register size.
3919 ??? GAS treats single-float processors as though they had 64-bit
3920 float registers (although it complains when double-precision
3921 instructions are used). As things stand, saying they have 32-bit
3922 registers would lead to spurious "register must be even" messages.
3923 So here we assume float registers are never smaller than the
3924 integer ones. */
3925 if (file_mips_opts.gp == 64)
3926 /* 64-bit integer registers implies 64-bit float registers. */
3927 file_mips_opts.fp = 64;
3928 else if ((file_mips_opts.ase & FP64_ASES)
3929 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
3930 /* Handle ASEs that require 64-bit float registers, if possible. */
3931 file_mips_opts.fp = 64;
7361da2c
AB
3932 else if (ISA_IS_R6 (mips_opts.isa))
3933 /* R6 implies 64-bit float registers. */
3934 file_mips_opts.fp = 64;
919731af 3935 else
3936 /* 32-bit float registers. */
3937 file_mips_opts.fp = 32;
3938 }
3939
3940 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
3941
351cdf24
MF
3942 /* Disable operations on odd-numbered floating-point registers by default
3943 when using the FPXX ABI. */
3944 if (file_mips_opts.oddspreg < 0)
3945 {
3946 if (file_mips_opts.fp == 0)
3947 file_mips_opts.oddspreg = 0;
3948 else
3949 file_mips_opts.oddspreg = 1;
3950 }
3951
919731af 3952 /* End of GCC-shared inference code. */
3953
3954 /* This flag is set when we have a 64-bit capable CPU but use only
3955 32-bit wide registers. Note that EABI does not use it. */
3956 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
3957 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
3958 || mips_abi == O32_ABI))
3959 mips_32bitmode = 1;
3960
3961 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
3962 as_bad (_("trap exception not supported at ISA 1"));
3963
3964 /* If the selected architecture includes support for ASEs, enable
3965 generation of code for them. */
3966 if (file_mips_opts.mips16 == -1)
3967 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
3968 if (file_mips_opts.micromips == -1)
3969 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
3970 ? 1 : 0;
3971
7361da2c
AB
3972 if (mips_nan2008 == -1)
3973 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
3974 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
3975 as_fatal (_("`%s' does not support legacy NaN"),
3976 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
3977
919731af 3978 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
3979 being selected implicitly. */
3980 if (file_mips_opts.fp != 64)
3981 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
3982
3983 /* If the user didn't explicitly select or deselect a particular ASE,
3984 use the default setting for the CPU. */
3985 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
3986
3987 /* Set up the current options. These may change throughout assembly. */
3988 mips_opts = file_mips_opts;
3989
3990 mips_check_isa_supports_ases ();
3991 mips_check_options (&file_mips_opts, TRUE);
3992 file_mips_opts_checked = TRUE;
3993
3994 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
3995 as_warn (_("could not set architecture and machine"));
3996}
3997
252b5132 3998void
17a2f251 3999md_assemble (char *str)
252b5132
RH
4000{
4001 struct mips_cl_insn insn;
f6688943
TS
4002 bfd_reloc_code_real_type unused_reloc[3]
4003 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4004
919731af 4005 file_mips_check_options ();
4006
252b5132 4007 imm_expr.X_op = O_absent;
252b5132 4008 offset_expr.X_op = O_absent;
f6688943
TS
4009 offset_reloc[0] = BFD_RELOC_UNUSED;
4010 offset_reloc[1] = BFD_RELOC_UNUSED;
4011 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4012
e1b47bd5
RS
4013 mips_mark_labels ();
4014 mips_assembling_insn = TRUE;
e3de51ce 4015 clear_insn_error ();
e1b47bd5 4016
252b5132
RH
4017 if (mips_opts.mips16)
4018 mips16_ip (str, &insn);
4019 else
4020 {
4021 mips_ip (str, &insn);
beae10d5
KH
4022 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4023 str, insn.insn_opcode));
252b5132
RH
4024 }
4025
e3de51ce
RS
4026 if (insn_error.msg)
4027 report_insn_error (str);
e1b47bd5 4028 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4029 {
584892a6 4030 macro_start ();
252b5132
RH
4031 if (mips_opts.mips16)
4032 mips16_macro (&insn);
4033 else
833794fc 4034 macro (&insn, str);
584892a6 4035 macro_end ();
252b5132
RH
4036 }
4037 else
4038 {
77bd4346 4039 if (offset_expr.X_op != O_absent)
df58fc94 4040 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4041 else
df58fc94 4042 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4043 }
e1b47bd5
RS
4044
4045 mips_assembling_insn = FALSE;
252b5132
RH
4046}
4047
738e5348
RS
4048/* Convenience functions for abstracting away the differences between
4049 MIPS16 and non-MIPS16 relocations. */
4050
4051static inline bfd_boolean
4052mips16_reloc_p (bfd_reloc_code_real_type reloc)
4053{
4054 switch (reloc)
4055 {
4056 case BFD_RELOC_MIPS16_JMP:
4057 case BFD_RELOC_MIPS16_GPREL:
4058 case BFD_RELOC_MIPS16_GOT16:
4059 case BFD_RELOC_MIPS16_CALL16:
4060 case BFD_RELOC_MIPS16_HI16_S:
4061 case BFD_RELOC_MIPS16_HI16:
4062 case BFD_RELOC_MIPS16_LO16:
4063 return TRUE;
4064
4065 default:
4066 return FALSE;
4067 }
4068}
4069
df58fc94
RS
4070static inline bfd_boolean
4071micromips_reloc_p (bfd_reloc_code_real_type reloc)
4072{
4073 switch (reloc)
4074 {
4075 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4076 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4077 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4078 case BFD_RELOC_MICROMIPS_GPREL16:
4079 case BFD_RELOC_MICROMIPS_JMP:
4080 case BFD_RELOC_MICROMIPS_HI16:
4081 case BFD_RELOC_MICROMIPS_HI16_S:
4082 case BFD_RELOC_MICROMIPS_LO16:
4083 case BFD_RELOC_MICROMIPS_LITERAL:
4084 case BFD_RELOC_MICROMIPS_GOT16:
4085 case BFD_RELOC_MICROMIPS_CALL16:
4086 case BFD_RELOC_MICROMIPS_GOT_HI16:
4087 case BFD_RELOC_MICROMIPS_GOT_LO16:
4088 case BFD_RELOC_MICROMIPS_CALL_HI16:
4089 case BFD_RELOC_MICROMIPS_CALL_LO16:
4090 case BFD_RELOC_MICROMIPS_SUB:
4091 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4092 case BFD_RELOC_MICROMIPS_GOT_OFST:
4093 case BFD_RELOC_MICROMIPS_GOT_DISP:
4094 case BFD_RELOC_MICROMIPS_HIGHEST:
4095 case BFD_RELOC_MICROMIPS_HIGHER:
4096 case BFD_RELOC_MICROMIPS_SCN_DISP:
4097 case BFD_RELOC_MICROMIPS_JALR:
4098 return TRUE;
4099
4100 default:
4101 return FALSE;
4102 }
4103}
4104
2309ddf2
MR
4105static inline bfd_boolean
4106jmp_reloc_p (bfd_reloc_code_real_type reloc)
4107{
4108 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4109}
4110
0e9c5a5c
MR
4111static inline bfd_boolean
4112b_reloc_p (bfd_reloc_code_real_type reloc)
4113{
4114 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4115 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4116 || reloc == BFD_RELOC_16_PCREL_S2
4117 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4118 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4119 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4120}
4121
738e5348
RS
4122static inline bfd_boolean
4123got16_reloc_p (bfd_reloc_code_real_type reloc)
4124{
2309ddf2 4125 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4126 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4127}
4128
4129static inline bfd_boolean
4130hi16_reloc_p (bfd_reloc_code_real_type reloc)
4131{
2309ddf2 4132 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4133 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4134}
4135
4136static inline bfd_boolean
4137lo16_reloc_p (bfd_reloc_code_real_type reloc)
4138{
2309ddf2 4139 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4140 || reloc == BFD_RELOC_MICROMIPS_LO16);
4141}
4142
df58fc94
RS
4143static inline bfd_boolean
4144jalr_reloc_p (bfd_reloc_code_real_type reloc)
4145{
2309ddf2 4146 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4147}
4148
f2ae14a1
RS
4149static inline bfd_boolean
4150gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4151{
4152 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4153 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4154}
4155
2de39019
CM
4156/* Return true if RELOC is a PC-relative relocation that does not have
4157 full address range. */
4158
4159static inline bfd_boolean
4160limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4161{
4162 switch (reloc)
4163 {
4164 case BFD_RELOC_16_PCREL_S2:
4165 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4166 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4167 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4168 case BFD_RELOC_MIPS_21_PCREL_S2:
4169 case BFD_RELOC_MIPS_26_PCREL_S2:
4170 case BFD_RELOC_MIPS_18_PCREL_S3:
4171 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4172 return TRUE;
4173
b47468a6 4174 case BFD_RELOC_32_PCREL:
7361da2c
AB
4175 case BFD_RELOC_HI16_S_PCREL:
4176 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4177 return HAVE_64BIT_ADDRESSES;
4178
2de39019
CM
4179 default:
4180 return FALSE;
4181 }
4182}
b47468a6 4183
5919d012 4184/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4185 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4186 need a matching %lo() when applied to local symbols. */
5919d012
RS
4187
4188static inline bfd_boolean
17a2f251 4189reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4190{
3b91255e 4191 return (HAVE_IN_PLACE_ADDENDS
738e5348 4192 && (hi16_reloc_p (reloc)
0a44bf69
RS
4193 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4194 all GOT16 relocations evaluate to "G". */
738e5348
RS
4195 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4196}
4197
4198/* Return the type of %lo() reloc needed by RELOC, given that
4199 reloc_needs_lo_p. */
4200
4201static inline bfd_reloc_code_real_type
4202matching_lo_reloc (bfd_reloc_code_real_type reloc)
4203{
df58fc94
RS
4204 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4205 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4206 : BFD_RELOC_LO16));
5919d012
RS
4207}
4208
4209/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4210 relocation. */
4211
4212static inline bfd_boolean
17a2f251 4213fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4214{
4215 return (fixp->fx_next != NULL
738e5348 4216 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4217 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4218 && fixp->fx_offset == fixp->fx_next->fx_offset);
4219}
4220
462427c4
RS
4221/* Move all labels in LABELS to the current insertion point. TEXT_P
4222 says whether the labels refer to text or data. */
404a8071
RS
4223
4224static void
462427c4 4225mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4226{
4227 struct insn_label_list *l;
4228 valueT val;
4229
462427c4 4230 for (l = labels; l != NULL; l = l->next)
404a8071 4231 {
9c2799c2 4232 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4233 symbol_set_frag (l->label, frag_now);
4234 val = (valueT) frag_now_fix ();
df58fc94 4235 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4236 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4237 ++val;
4238 S_SET_VALUE (l->label, val);
4239 }
4240}
4241
462427c4
RS
4242/* Move all labels in insn_labels to the current insertion point
4243 and treat them as text labels. */
4244
4245static void
4246mips_move_text_labels (void)
4247{
4248 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4249}
4250
5f0fe04b
TS
4251static bfd_boolean
4252s_is_linkonce (symbolS *sym, segT from_seg)
4253{
4254 bfd_boolean linkonce = FALSE;
4255 segT symseg = S_GET_SEGMENT (sym);
4256
4257 if (symseg != from_seg && !S_IS_LOCAL (sym))
4258 {
4259 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4260 linkonce = TRUE;
5f0fe04b
TS
4261 /* The GNU toolchain uses an extension for ELF: a section
4262 beginning with the magic string .gnu.linkonce is a
4263 linkonce section. */
4264 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4265 sizeof ".gnu.linkonce" - 1) == 0)
4266 linkonce = TRUE;
5f0fe04b
TS
4267 }
4268 return linkonce;
4269}
4270
e1b47bd5 4271/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4272 linker to handle them specially, such as generating jalx instructions
4273 when needed. We also make them odd for the duration of the assembly,
4274 in order to generate the right sort of code. We will make them even
252b5132
RH
4275 in the adjust_symtab routine, while leaving them marked. This is
4276 convenient for the debugger and the disassembler. The linker knows
4277 to make them odd again. */
4278
4279static void
e1b47bd5 4280mips_compressed_mark_label (symbolS *label)
252b5132 4281{
df58fc94 4282 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4283
f3ded42a
RS
4284 if (mips_opts.mips16)
4285 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4286 else
4287 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4288 if ((S_GET_VALUE (label) & 1) == 0
4289 /* Don't adjust the address if the label is global or weak, or
4290 in a link-once section, since we'll be emitting symbol reloc
4291 references to it which will be patched up by the linker, and
4292 the final value of the symbol may or may not be MIPS16/microMIPS. */
4293 && !S_IS_WEAK (label)
4294 && !S_IS_EXTERNAL (label)
4295 && !s_is_linkonce (label, now_seg))
4296 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4297}
4298
4299/* Mark preceding MIPS16 or microMIPS instruction labels. */
4300
4301static void
4302mips_compressed_mark_labels (void)
4303{
4304 struct insn_label_list *l;
4305
4306 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4307 mips_compressed_mark_label (l->label);
252b5132
RH
4308}
4309
4d7206a2
RS
4310/* End the current frag. Make it a variant frag and record the
4311 relaxation info. */
4312
4313static void
4314relax_close_frag (void)
4315{
584892a6 4316 mips_macro_warning.first_frag = frag_now;
4d7206a2 4317 frag_var (rs_machine_dependent, 0, 0,
584892a6 4318 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1]),
4d7206a2
RS
4319 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4320
4321 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4322 mips_relax.first_fixup = 0;
4323}
4324
4325/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4326 See the comment above RELAX_ENCODE for more details. */
4327
4328static void
4329relax_start (symbolS *symbol)
4330{
9c2799c2 4331 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4332 mips_relax.sequence = 1;
4333 mips_relax.symbol = symbol;
4334}
4335
4336/* Start generating the second version of a relaxable sequence.
4337 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4338
4339static void
4d7206a2
RS
4340relax_switch (void)
4341{
9c2799c2 4342 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4343 mips_relax.sequence = 2;
4344}
4345
4346/* End the current relaxable sequence. */
4347
4348static void
4349relax_end (void)
4350{
9c2799c2 4351 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4352 relax_close_frag ();
4353 mips_relax.sequence = 0;
4354}
4355
11625dd8
RS
4356/* Return true if IP is a delayed branch or jump. */
4357
4358static inline bfd_boolean
4359delayed_branch_p (const struct mips_cl_insn *ip)
4360{
4361 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4362 | INSN_COND_BRANCH_DELAY
4363 | INSN_COND_BRANCH_LIKELY)) != 0;
4364}
4365
4366/* Return true if IP is a compact branch or jump. */
4367
4368static inline bfd_boolean
4369compact_branch_p (const struct mips_cl_insn *ip)
4370{
26545944
RS
4371 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4372 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4373}
4374
4375/* Return true if IP is an unconditional branch or jump. */
4376
4377static inline bfd_boolean
4378uncond_branch_p (const struct mips_cl_insn *ip)
4379{
4380 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4381 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4382}
4383
4384/* Return true if IP is a branch-likely instruction. */
4385
4386static inline bfd_boolean
4387branch_likely_p (const struct mips_cl_insn *ip)
4388{
4389 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4390}
4391
14fe068b
RS
4392/* Return the type of nop that should be used to fill the delay slot
4393 of delayed branch IP. */
4394
4395static struct mips_cl_insn *
4396get_delay_slot_nop (const struct mips_cl_insn *ip)
4397{
4398 if (mips_opts.micromips
4399 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4400 return &micromips_nop32_insn;
4401 return NOP_INSN;
4402}
4403
fc76e730
RS
4404/* Return a mask that has bit N set if OPCODE reads the register(s)
4405 in operand N. */
df58fc94
RS
4406
4407static unsigned int
fc76e730 4408insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4409{
fc76e730
RS
4410 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4411}
df58fc94 4412
fc76e730
RS
4413/* Return a mask that has bit N set if OPCODE writes to the register(s)
4414 in operand N. */
4415
4416static unsigned int
4417insn_write_mask (const struct mips_opcode *opcode)
4418{
4419 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4420}
4421
4422/* Return a mask of the registers specified by operand OPERAND of INSN.
4423 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4424 is set. */
4425
4426static unsigned int
4427operand_reg_mask (const struct mips_cl_insn *insn,
4428 const struct mips_operand *operand,
4429 unsigned int type_mask)
4430{
4431 unsigned int uval, vsel;
4432
4433 switch (operand->type)
df58fc94 4434 {
fc76e730
RS
4435 case OP_INT:
4436 case OP_MAPPED_INT:
4437 case OP_MSB:
4438 case OP_PCREL:
4439 case OP_PERF_REG:
4440 case OP_ADDIUSP_INT:
4441 case OP_ENTRY_EXIT_LIST:
4442 case OP_REPEAT_DEST_REG:
4443 case OP_REPEAT_PREV_REG:
4444 case OP_PC:
14daeee3
RS
4445 case OP_VU0_SUFFIX:
4446 case OP_VU0_MATCH_SUFFIX:
56d438b1 4447 case OP_IMM_INDEX:
fc76e730
RS
4448 abort ();
4449
4450 case OP_REG:
0f35dbc4 4451 case OP_OPTIONAL_REG:
fc76e730
RS
4452 {
4453 const struct mips_reg_operand *reg_op;
4454
4455 reg_op = (const struct mips_reg_operand *) operand;
4456 if (!(type_mask & (1 << reg_op->reg_type)))
4457 return 0;
4458 uval = insn_extract_operand (insn, operand);
4459 return 1 << mips_decode_reg_operand (reg_op, uval);
4460 }
4461
4462 case OP_REG_PAIR:
4463 {
4464 const struct mips_reg_pair_operand *pair_op;
4465
4466 pair_op = (const struct mips_reg_pair_operand *) operand;
4467 if (!(type_mask & (1 << pair_op->reg_type)))
4468 return 0;
4469 uval = insn_extract_operand (insn, operand);
4470 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4471 }
4472
4473 case OP_CLO_CLZ_DEST:
4474 if (!(type_mask & (1 << OP_REG_GP)))
4475 return 0;
4476 uval = insn_extract_operand (insn, operand);
4477 return (1 << (uval & 31)) | (1 << (uval >> 5));
4478
7361da2c
AB
4479 case OP_SAME_RS_RT:
4480 if (!(type_mask & (1 << OP_REG_GP)))
4481 return 0;
4482 uval = insn_extract_operand (insn, operand);
4483 gas_assert ((uval & 31) == (uval >> 5));
4484 return 1 << (uval & 31);
4485
4486 case OP_CHECK_PREV:
4487 case OP_NON_ZERO_REG:
4488 if (!(type_mask & (1 << OP_REG_GP)))
4489 return 0;
4490 uval = insn_extract_operand (insn, operand);
4491 return 1 << (uval & 31);
4492
fc76e730
RS
4493 case OP_LWM_SWM_LIST:
4494 abort ();
4495
4496 case OP_SAVE_RESTORE_LIST:
4497 abort ();
4498
4499 case OP_MDMX_IMM_REG:
4500 if (!(type_mask & (1 << OP_REG_VEC)))
4501 return 0;
4502 uval = insn_extract_operand (insn, operand);
4503 vsel = uval >> 5;
4504 if ((vsel & 0x18) == 0x18)
4505 return 0;
4506 return 1 << (uval & 31);
56d438b1
CF
4507
4508 case OP_REG_INDEX:
4509 if (!(type_mask & (1 << OP_REG_GP)))
4510 return 0;
4511 return 1 << insn_extract_operand (insn, operand);
df58fc94 4512 }
fc76e730
RS
4513 abort ();
4514}
4515
4516/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4517 where bit N of OPNO_MASK is set if operand N should be included.
4518 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4519 is set. */
4520
4521static unsigned int
4522insn_reg_mask (const struct mips_cl_insn *insn,
4523 unsigned int type_mask, unsigned int opno_mask)
4524{
4525 unsigned int opno, reg_mask;
4526
4527 opno = 0;
4528 reg_mask = 0;
4529 while (opno_mask != 0)
4530 {
4531 if (opno_mask & 1)
4532 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4533 opno_mask >>= 1;
4534 opno += 1;
4535 }
4536 return reg_mask;
df58fc94
RS
4537}
4538
4c260379
RS
4539/* Return the mask of core registers that IP reads. */
4540
4541static unsigned int
4542gpr_read_mask (const struct mips_cl_insn *ip)
4543{
4544 unsigned long pinfo, pinfo2;
4545 unsigned int mask;
4546
fc76e730 4547 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4548 pinfo = ip->insn_mo->pinfo;
4549 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4550 if (pinfo & INSN_UDI)
4c260379 4551 {
fc76e730
RS
4552 /* UDI instructions have traditionally been assumed to read RS
4553 and RT. */
4554 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4555 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4556 }
fc76e730
RS
4557 if (pinfo & INSN_READ_GPR_24)
4558 mask |= 1 << 24;
4559 if (pinfo2 & INSN2_READ_GPR_16)
4560 mask |= 1 << 16;
4561 if (pinfo2 & INSN2_READ_SP)
4562 mask |= 1 << SP;
26545944 4563 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4564 mask |= 1 << 31;
fe35f09f
RS
4565 /* Don't include register 0. */
4566 return mask & ~1;
4c260379
RS
4567}
4568
4569/* Return the mask of core registers that IP writes. */
4570
4571static unsigned int
4572gpr_write_mask (const struct mips_cl_insn *ip)
4573{
4574 unsigned long pinfo, pinfo2;
4575 unsigned int mask;
4576
fc76e730 4577 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4578 pinfo = ip->insn_mo->pinfo;
4579 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4580 if (pinfo & INSN_WRITE_GPR_24)
4581 mask |= 1 << 24;
4582 if (pinfo & INSN_WRITE_GPR_31)
4583 mask |= 1 << 31;
4584 if (pinfo & INSN_UDI)
4585 /* UDI instructions have traditionally been assumed to write to RD. */
4586 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4587 if (pinfo2 & INSN2_WRITE_SP)
4588 mask |= 1 << SP;
fe35f09f
RS
4589 /* Don't include register 0. */
4590 return mask & ~1;
4c260379
RS
4591}
4592
4593/* Return the mask of floating-point registers that IP reads. */
4594
4595static unsigned int
4596fpr_read_mask (const struct mips_cl_insn *ip)
4597{
fc76e730 4598 unsigned long pinfo;
4c260379
RS
4599 unsigned int mask;
4600
9d5de888
CF
4601 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4602 | (1 << OP_REG_MSA)),
fc76e730 4603 insn_read_mask (ip->insn_mo));
4c260379 4604 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4605 /* Conservatively treat all operands to an FP_D instruction are doubles.
4606 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4607 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4608 mask |= mask << 1;
4609 return mask;
4610}
4611
4612/* Return the mask of floating-point registers that IP writes. */
4613
4614static unsigned int
4615fpr_write_mask (const struct mips_cl_insn *ip)
4616{
fc76e730 4617 unsigned long pinfo;
4c260379
RS
4618 unsigned int mask;
4619
9d5de888
CF
4620 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4621 | (1 << OP_REG_MSA)),
fc76e730 4622 insn_write_mask (ip->insn_mo));
4c260379 4623 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4624 /* Conservatively treat all operands to an FP_D instruction are doubles.
4625 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4626 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4627 mask |= mask << 1;
4628 return mask;
4629}
4630
a1d78564
RS
4631/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4632 Check whether that is allowed. */
4633
4634static bfd_boolean
4635mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4636{
4637 const char *s = insn->name;
351cdf24
MF
4638 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4639 || FPR_SIZE == 64)
4640 && mips_opts.oddspreg;
a1d78564
RS
4641
4642 if (insn->pinfo == INSN_MACRO)
4643 /* Let a macro pass, we'll catch it later when it is expanded. */
4644 return TRUE;
4645
351cdf24
MF
4646 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4647 otherwise it depends on oddspreg. */
4648 if ((insn->pinfo & FP_S)
4649 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4650 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4651 return FPR_SIZE == 32 || oddspreg;
a1d78564 4652
351cdf24
MF
4653 /* Allow odd registers for single-precision ops and double-precision if the
4654 floating-point registers are 64-bit wide. */
4655 switch (insn->pinfo & (FP_S | FP_D))
4656 {
4657 case FP_S:
4658 case 0:
4659 return oddspreg;
4660 case FP_D:
4661 return FPR_SIZE == 64;
4662 default:
4663 break;
a1d78564
RS
4664 }
4665
351cdf24
MF
4666 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4667 s = strchr (insn->name, '.');
4668 if (s != NULL && opnum == 2)
4669 s = strchr (s + 1, '.');
4670 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4671 return oddspreg;
a1d78564 4672
351cdf24 4673 return FPR_SIZE == 64;
a1d78564
RS
4674}
4675
a1d78564
RS
4676/* Information about an instruction argument that we're trying to match. */
4677struct mips_arg_info
4678{
4679 /* The instruction so far. */
4680 struct mips_cl_insn *insn;
4681
a92713e6
RS
4682 /* The first unconsumed operand token. */
4683 struct mips_operand_token *token;
4684
a1d78564
RS
4685 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4686 int opnum;
4687
4688 /* The 1-based argument number, for error reporting. This does not
4689 count elided optional registers, etc.. */
4690 int argnum;
4691
4692 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4693 unsigned int last_regno;
4694
4695 /* If the first operand was an OP_REG, this is the register that it
4696 specified, otherwise it is ILLEGAL_REG. */
4697 unsigned int dest_regno;
4698
4699 /* The value of the last OP_INT operand. Only used for OP_MSB,
4700 where it gives the lsb position. */
4701 unsigned int last_op_int;
4702
60f20e8b
RS
4703 /* If true, match routines should assume that no later instruction
4704 alternative matches and should therefore be as accomodating as
4705 possible. Match routines should not report errors if something
4706 is only invalid for !LAX_MATCH. */
4707 bfd_boolean lax_match;
a1d78564 4708
a1d78564
RS
4709 /* True if a reference to the current AT register was seen. */
4710 bfd_boolean seen_at;
4711};
4712
1a00e612
RS
4713/* Record that the argument is out of range. */
4714
4715static void
4716match_out_of_range (struct mips_arg_info *arg)
4717{
4718 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4719}
4720
4721/* Record that the argument isn't constant but needs to be. */
4722
4723static void
4724match_not_constant (struct mips_arg_info *arg)
4725{
4726 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4727 arg->argnum);
4728}
4729
a92713e6
RS
4730/* Try to match an OT_CHAR token for character CH. Consume the token
4731 and return true on success, otherwise return false. */
a1d78564 4732
a92713e6
RS
4733static bfd_boolean
4734match_char (struct mips_arg_info *arg, char ch)
a1d78564 4735{
a92713e6
RS
4736 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4737 {
4738 ++arg->token;
4739 if (ch == ',')
4740 arg->argnum += 1;
4741 return TRUE;
4742 }
4743 return FALSE;
4744}
a1d78564 4745
a92713e6
RS
4746/* Try to get an expression from the next tokens in ARG. Consume the
4747 tokens and return true on success, storing the expression value in
4748 VALUE and relocation types in R. */
4749
4750static bfd_boolean
4751match_expression (struct mips_arg_info *arg, expressionS *value,
4752 bfd_reloc_code_real_type *r)
4753{
d436c1c2
RS
4754 /* If the next token is a '(' that was parsed as being part of a base
4755 expression, assume we have an elided offset. The later match will fail
4756 if this turns out to be wrong. */
4757 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4758 {
d436c1c2
RS
4759 value->X_op = O_constant;
4760 value->X_add_number = 0;
4761 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4762 return TRUE;
4763 }
4764
d436c1c2
RS
4765 /* Reject register-based expressions such as "0+$2" and "(($2))".
4766 For plain registers the default error seems more appropriate. */
4767 if (arg->token->type == OT_INTEGER
4768 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4769 {
d436c1c2
RS
4770 set_insn_error (arg->argnum, _("register value used as expression"));
4771 return FALSE;
a1d78564 4772 }
d436c1c2
RS
4773
4774 if (arg->token->type == OT_INTEGER)
a92713e6 4775 {
d436c1c2
RS
4776 *value = arg->token->u.integer.value;
4777 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4778 ++arg->token;
4779 return TRUE;
a92713e6 4780 }
a92713e6 4781
d436c1c2
RS
4782 set_insn_error_i
4783 (arg->argnum, _("operand %d must be an immediate expression"),
4784 arg->argnum);
4785 return FALSE;
a92713e6
RS
4786}
4787
4788/* Try to get a constant expression from the next tokens in ARG. Consume
4789 the tokens and return return true on success, storing the constant value
4790 in *VALUE. Use FALLBACK as the value if the match succeeded with an
4791 error. */
4792
4793static bfd_boolean
1a00e612 4794match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4795{
4796 expressionS ex;
4797 bfd_reloc_code_real_type r[3];
a1d78564 4798
a92713e6
RS
4799 if (!match_expression (arg, &ex, r))
4800 return FALSE;
4801
4802 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4803 *value = ex.X_add_number;
4804 else
4805 {
1a00e612
RS
4806 match_not_constant (arg);
4807 return FALSE;
a1d78564 4808 }
a92713e6 4809 return TRUE;
a1d78564
RS
4810}
4811
4812/* Return the RTYPE_* flags for a register operand of type TYPE that
4813 appears in instruction OPCODE. */
4814
4815static unsigned int
4816convert_reg_type (const struct mips_opcode *opcode,
4817 enum mips_reg_operand_type type)
4818{
4819 switch (type)
4820 {
4821 case OP_REG_GP:
4822 return RTYPE_NUM | RTYPE_GP;
4823
4824 case OP_REG_FP:
4825 /* Allow vector register names for MDMX if the instruction is a 64-bit
4826 FPR load, store or move (including moves to and from GPRs). */
4827 if ((mips_opts.ase & ASE_MDMX)
4828 && (opcode->pinfo & FP_D)
43885403 4829 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4830 | INSN_COPROC_MEMORY_DELAY
43885403 4831 | INSN_LOAD_COPROC
67dc82bc 4832 | INSN_LOAD_MEMORY
a1d78564
RS
4833 | INSN_STORE_MEMORY)))
4834 return RTYPE_FPU | RTYPE_VEC;
4835 return RTYPE_FPU;
4836
4837 case OP_REG_CCC:
4838 if (opcode->pinfo & (FP_D | FP_S))
4839 return RTYPE_CCC | RTYPE_FCC;
4840 return RTYPE_CCC;
4841
4842 case OP_REG_VEC:
4843 if (opcode->membership & INSN_5400)
4844 return RTYPE_FPU;
4845 return RTYPE_FPU | RTYPE_VEC;
4846
4847 case OP_REG_ACC:
4848 return RTYPE_ACC;
4849
4850 case OP_REG_COPRO:
4851 if (opcode->name[strlen (opcode->name) - 1] == '0')
4852 return RTYPE_NUM | RTYPE_CP0;
4853 return RTYPE_NUM;
4854
4855 case OP_REG_HW:
4856 return RTYPE_NUM;
14daeee3
RS
4857
4858 case OP_REG_VI:
4859 return RTYPE_NUM | RTYPE_VI;
4860
4861 case OP_REG_VF:
4862 return RTYPE_NUM | RTYPE_VF;
4863
4864 case OP_REG_R5900_I:
4865 return RTYPE_R5900_I;
4866
4867 case OP_REG_R5900_Q:
4868 return RTYPE_R5900_Q;
4869
4870 case OP_REG_R5900_R:
4871 return RTYPE_R5900_R;
4872
4873 case OP_REG_R5900_ACC:
4874 return RTYPE_R5900_ACC;
56d438b1
CF
4875
4876 case OP_REG_MSA:
4877 return RTYPE_MSA;
4878
4879 case OP_REG_MSA_CTRL:
4880 return RTYPE_NUM;
a1d78564
RS
4881 }
4882 abort ();
4883}
4884
4885/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
4886
4887static void
4888check_regno (struct mips_arg_info *arg,
4889 enum mips_reg_operand_type type, unsigned int regno)
4890{
4891 if (AT && type == OP_REG_GP && regno == AT)
4892 arg->seen_at = TRUE;
4893
4894 if (type == OP_REG_FP
4895 && (regno & 1) != 0
a1d78564 4896 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
4897 {
4898 /* This was a warning prior to introducing O32 FPXX and FP64 support
4899 so maintain a warning for FP32 but raise an error for the new
4900 cases. */
4901 if (FPR_SIZE == 32)
4902 as_warn (_("float register should be even, was %d"), regno);
4903 else
4904 as_bad (_("float register should be even, was %d"), regno);
4905 }
a1d78564
RS
4906
4907 if (type == OP_REG_CCC)
4908 {
4909 const char *name;
4910 size_t length;
4911
4912 name = arg->insn->insn_mo->name;
4913 length = strlen (name);
4914 if ((regno & 1) != 0
4915 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
4916 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 4917 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
4918 name, regno);
4919
4920 if ((regno & 3) != 0
4921 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 4922 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
4923 name, regno);
4924 }
4925}
4926
a92713e6
RS
4927/* ARG is a register with symbol value SYMVAL. Try to interpret it as
4928 a register of type TYPE. Return true on success, storing the register
4929 number in *REGNO and warning about any dubious uses. */
4930
4931static bfd_boolean
4932match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4933 unsigned int symval, unsigned int *regno)
4934{
4935 if (type == OP_REG_VEC)
4936 symval = mips_prefer_vec_regno (symval);
4937 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
4938 return FALSE;
4939
4940 *regno = symval & RNUM_MASK;
4941 check_regno (arg, type, *regno);
4942 return TRUE;
4943}
4944
4945/* Try to interpret the next token in ARG as a register of type TYPE.
4946 Consume the token and return true on success, storing the register
4947 number in *REGNO. Return false on failure. */
4948
4949static bfd_boolean
4950match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4951 unsigned int *regno)
4952{
4953 if (arg->token->type == OT_REG
4954 && match_regno (arg, type, arg->token->u.regno, regno))
4955 {
4956 ++arg->token;
4957 return TRUE;
4958 }
4959 return FALSE;
4960}
4961
4962/* Try to interpret the next token in ARG as a range of registers of type TYPE.
4963 Consume the token and return true on success, storing the register numbers
4964 in *REGNO1 and *REGNO2. Return false on failure. */
4965
4966static bfd_boolean
4967match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
4968 unsigned int *regno1, unsigned int *regno2)
4969{
4970 if (match_reg (arg, type, regno1))
4971 {
4972 *regno2 = *regno1;
4973 return TRUE;
4974 }
4975 if (arg->token->type == OT_REG_RANGE
4976 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
4977 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
4978 && *regno1 <= *regno2)
4979 {
4980 ++arg->token;
4981 return TRUE;
4982 }
4983 return FALSE;
4984}
4985
a1d78564
RS
4986/* OP_INT matcher. */
4987
a92713e6 4988static bfd_boolean
a1d78564 4989match_int_operand (struct mips_arg_info *arg,
a92713e6 4990 const struct mips_operand *operand_base)
a1d78564
RS
4991{
4992 const struct mips_int_operand *operand;
3ccad066 4993 unsigned int uval;
a1d78564
RS
4994 int min_val, max_val, factor;
4995 offsetT sval;
a1d78564
RS
4996
4997 operand = (const struct mips_int_operand *) operand_base;
4998 factor = 1 << operand->shift;
3ccad066
RS
4999 min_val = mips_int_operand_min (operand);
5000 max_val = mips_int_operand_max (operand);
a1d78564 5001
d436c1c2
RS
5002 if (operand_base->lsb == 0
5003 && operand_base->size == 16
5004 && operand->shift == 0
5005 && operand->bias == 0
5006 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5007 {
5008 /* The operand can be relocated. */
a92713e6
RS
5009 if (!match_expression (arg, &offset_expr, offset_reloc))
5010 return FALSE;
5011
5012 if (offset_reloc[0] != BFD_RELOC_UNUSED)
a1d78564
RS
5013 /* Relocation operators were used. Accept the arguent and
5014 leave the relocation value in offset_expr and offset_relocs
5015 for the caller to process. */
a92713e6
RS
5016 return TRUE;
5017
5018 if (offset_expr.X_op != O_constant)
a1d78564 5019 {
60f20e8b
RS
5020 /* Accept non-constant operands if no later alternative matches,
5021 leaving it for the caller to process. */
5022 if (!arg->lax_match)
5023 return FALSE;
a92713e6
RS
5024 offset_reloc[0] = BFD_RELOC_LO16;
5025 return TRUE;
a1d78564 5026 }
a92713e6 5027
a1d78564
RS
5028 /* Clear the global state; we're going to install the operand
5029 ourselves. */
a92713e6 5030 sval = offset_expr.X_add_number;
a1d78564 5031 offset_expr.X_op = O_absent;
60f20e8b
RS
5032
5033 /* For compatibility with older assemblers, we accept
5034 0x8000-0xffff as signed 16-bit numbers when only
5035 signed numbers are allowed. */
5036 if (sval > max_val)
5037 {
5038 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5039 if (!arg->lax_match && sval <= max_val)
5040 return FALSE;
5041 }
a1d78564
RS
5042 }
5043 else
5044 {
1a00e612 5045 if (!match_const_int (arg, &sval))
a92713e6 5046 return FALSE;
a1d78564
RS
5047 }
5048
5049 arg->last_op_int = sval;
5050
1a00e612 5051 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5052 {
1a00e612
RS
5053 match_out_of_range (arg);
5054 return FALSE;
a1d78564
RS
5055 }
5056
5057 uval = (unsigned int) sval >> operand->shift;
5058 uval -= operand->bias;
5059
5060 /* Handle -mfix-cn63xxp1. */
5061 if (arg->opnum == 1
5062 && mips_fix_cn63xxp1
5063 && !mips_opts.micromips
5064 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5065 switch (uval)
5066 {
5067 case 5:
5068 case 25:
5069 case 26:
5070 case 27:
5071 case 28:
5072 case 29:
5073 case 30:
5074 case 31:
5075 /* These are ok. */
5076 break;
5077
5078 default:
5079 /* The rest must be changed to 28. */
5080 uval = 28;
5081 break;
5082 }
5083
5084 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5085 return TRUE;
a1d78564
RS
5086}
5087
5088/* OP_MAPPED_INT matcher. */
5089
a92713e6 5090static bfd_boolean
a1d78564 5091match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5092 const struct mips_operand *operand_base)
a1d78564
RS
5093{
5094 const struct mips_mapped_int_operand *operand;
5095 unsigned int uval, num_vals;
5096 offsetT sval;
5097
5098 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5099 if (!match_const_int (arg, &sval))
a92713e6 5100 return FALSE;
a1d78564
RS
5101
5102 num_vals = 1 << operand_base->size;
5103 for (uval = 0; uval < num_vals; uval++)
5104 if (operand->int_map[uval] == sval)
5105 break;
5106 if (uval == num_vals)
1a00e612
RS
5107 {
5108 match_out_of_range (arg);
5109 return FALSE;
5110 }
a1d78564
RS
5111
5112 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5113 return TRUE;
a1d78564
RS
5114}
5115
5116/* OP_MSB matcher. */
5117
a92713e6 5118static bfd_boolean
a1d78564 5119match_msb_operand (struct mips_arg_info *arg,
a92713e6 5120 const struct mips_operand *operand_base)
a1d78564
RS
5121{
5122 const struct mips_msb_operand *operand;
5123 int min_val, max_val, max_high;
5124 offsetT size, sval, high;
5125
5126 operand = (const struct mips_msb_operand *) operand_base;
5127 min_val = operand->bias;
5128 max_val = min_val + (1 << operand_base->size) - 1;
5129 max_high = operand->opsize;
5130
1a00e612 5131 if (!match_const_int (arg, &size))
a92713e6 5132 return FALSE;
a1d78564
RS
5133
5134 high = size + arg->last_op_int;
5135 sval = operand->add_lsb ? high : size;
5136
5137 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5138 {
1a00e612
RS
5139 match_out_of_range (arg);
5140 return FALSE;
a1d78564
RS
5141 }
5142 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5143 return TRUE;
a1d78564
RS
5144}
5145
5146/* OP_REG matcher. */
5147
a92713e6 5148static bfd_boolean
a1d78564 5149match_reg_operand (struct mips_arg_info *arg,
a92713e6 5150 const struct mips_operand *operand_base)
a1d78564
RS
5151{
5152 const struct mips_reg_operand *operand;
a92713e6 5153 unsigned int regno, uval, num_vals;
a1d78564
RS
5154
5155 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5156 if (!match_reg (arg, operand->reg_type, &regno))
5157 return FALSE;
a1d78564
RS
5158
5159 if (operand->reg_map)
5160 {
5161 num_vals = 1 << operand->root.size;
5162 for (uval = 0; uval < num_vals; uval++)
5163 if (operand->reg_map[uval] == regno)
5164 break;
5165 if (num_vals == uval)
a92713e6 5166 return FALSE;
a1d78564
RS
5167 }
5168 else
5169 uval = regno;
5170
a1d78564
RS
5171 arg->last_regno = regno;
5172 if (arg->opnum == 1)
5173 arg->dest_regno = regno;
5174 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5175 return TRUE;
a1d78564
RS
5176}
5177
5178/* OP_REG_PAIR matcher. */
5179
a92713e6 5180static bfd_boolean
a1d78564 5181match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5182 const struct mips_operand *operand_base)
a1d78564
RS
5183{
5184 const struct mips_reg_pair_operand *operand;
a92713e6 5185 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5186
5187 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5188 if (!match_reg (arg, operand->reg_type, &regno1)
5189 || !match_char (arg, ',')
5190 || !match_reg (arg, operand->reg_type, &regno2))
5191 return FALSE;
a1d78564
RS
5192
5193 num_vals = 1 << operand_base->size;
5194 for (uval = 0; uval < num_vals; uval++)
5195 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5196 break;
5197 if (uval == num_vals)
a92713e6 5198 return FALSE;
a1d78564 5199
a1d78564 5200 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5201 return TRUE;
a1d78564
RS
5202}
5203
5204/* OP_PCREL matcher. The caller chooses the relocation type. */
5205
a92713e6
RS
5206static bfd_boolean
5207match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5208{
a92713e6
RS
5209 bfd_reloc_code_real_type r[3];
5210
5211 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5212}
5213
5214/* OP_PERF_REG matcher. */
5215
a92713e6 5216static bfd_boolean
a1d78564 5217match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5218 const struct mips_operand *operand)
a1d78564
RS
5219{
5220 offsetT sval;
5221
1a00e612 5222 if (!match_const_int (arg, &sval))
a92713e6 5223 return FALSE;
a1d78564
RS
5224
5225 if (sval != 0
5226 && (sval != 1
5227 || (mips_opts.arch == CPU_R5900
5228 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5229 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5230 {
1a00e612
RS
5231 set_insn_error (arg->argnum, _("invalid performance register"));
5232 return FALSE;
a1d78564
RS
5233 }
5234
5235 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5236 return TRUE;
a1d78564
RS
5237}
5238
5239/* OP_ADDIUSP matcher. */
5240
a92713e6 5241static bfd_boolean
a1d78564 5242match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5243 const struct mips_operand *operand)
a1d78564
RS
5244{
5245 offsetT sval;
5246 unsigned int uval;
5247
1a00e612 5248 if (!match_const_int (arg, &sval))
a92713e6 5249 return FALSE;
a1d78564
RS
5250
5251 if (sval % 4)
1a00e612
RS
5252 {
5253 match_out_of_range (arg);
5254 return FALSE;
5255 }
a1d78564
RS
5256
5257 sval /= 4;
5258 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5259 {
5260 match_out_of_range (arg);
5261 return FALSE;
5262 }
a1d78564
RS
5263
5264 uval = (unsigned int) sval;
5265 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5266 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5267 return TRUE;
a1d78564
RS
5268}
5269
5270/* OP_CLO_CLZ_DEST matcher. */
5271
a92713e6 5272static bfd_boolean
a1d78564 5273match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5274 const struct mips_operand *operand)
a1d78564
RS
5275{
5276 unsigned int regno;
5277
a92713e6
RS
5278 if (!match_reg (arg, OP_REG_GP, &regno))
5279 return FALSE;
a1d78564 5280
a1d78564 5281 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5282 return TRUE;
a1d78564
RS
5283}
5284
7361da2c
AB
5285/* OP_CHECK_PREV matcher. */
5286
5287static bfd_boolean
5288match_check_prev_operand (struct mips_arg_info *arg,
5289 const struct mips_operand *operand_base)
5290{
5291 const struct mips_check_prev_operand *operand;
5292 unsigned int regno;
5293
5294 operand = (const struct mips_check_prev_operand *) operand_base;
5295
5296 if (!match_reg (arg, OP_REG_GP, &regno))
5297 return FALSE;
5298
5299 if (!operand->zero_ok && regno == 0)
5300 return FALSE;
5301
5302 if ((operand->less_than_ok && regno < arg->last_regno)
5303 || (operand->greater_than_ok && regno > arg->last_regno)
5304 || (operand->equal_ok && regno == arg->last_regno))
5305 {
5306 arg->last_regno = regno;
5307 insn_insert_operand (arg->insn, operand_base, regno);
5308 return TRUE;
5309 }
5310
5311 return FALSE;
5312}
5313
5314/* OP_SAME_RS_RT matcher. */
5315
5316static bfd_boolean
5317match_same_rs_rt_operand (struct mips_arg_info *arg,
5318 const struct mips_operand *operand)
5319{
5320 unsigned int regno;
5321
5322 if (!match_reg (arg, OP_REG_GP, &regno))
5323 return FALSE;
5324
5325 if (regno == 0)
5326 {
5327 set_insn_error (arg->argnum, _("the source register must not be $0"));
5328 return FALSE;
5329 }
5330
5331 arg->last_regno = regno;
5332
5333 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5334 return TRUE;
5335}
5336
a1d78564
RS
5337/* OP_LWM_SWM_LIST matcher. */
5338
a92713e6 5339static bfd_boolean
a1d78564 5340match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5341 const struct mips_operand *operand)
a1d78564 5342{
a92713e6
RS
5343 unsigned int reglist, sregs, ra, regno1, regno2;
5344 struct mips_arg_info reset;
a1d78564 5345
a92713e6
RS
5346 reglist = 0;
5347 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5348 return FALSE;
5349 do
5350 {
5351 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5352 {
5353 reglist |= 1 << FP;
5354 regno2 = S7;
5355 }
5356 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5357 reset = *arg;
5358 }
5359 while (match_char (arg, ',')
5360 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5361 *arg = reset;
a1d78564
RS
5362
5363 if (operand->size == 2)
5364 {
5365 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5366
5367 s0, ra
5368 s0, s1, ra, s2, s3
5369 s0-s2, ra
5370
5371 and any permutations of these. */
5372 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5373 return FALSE;
a1d78564
RS
5374
5375 sregs = (reglist >> 17) & 7;
5376 ra = 0;
5377 }
5378 else
5379 {
5380 /* The list must include at least one of ra and s0-sN,
5381 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5382 which are $23 and $30 respectively.) E.g.:
5383
5384 ra
5385 s0
5386 ra, s0, s1, s2
5387 s0-s8
5388 s0-s5, ra
5389
5390 and any permutations of these. */
5391 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5392 return FALSE;
a1d78564
RS
5393
5394 ra = (reglist >> 27) & 0x10;
5395 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5396 }
5397 sregs += 1;
5398 if ((sregs & -sregs) != sregs)
a92713e6 5399 return FALSE;
a1d78564
RS
5400
5401 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5402 return TRUE;
a1d78564
RS
5403}
5404
364215c8
RS
5405/* OP_ENTRY_EXIT_LIST matcher. */
5406
a92713e6 5407static unsigned int
364215c8 5408match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5409 const struct mips_operand *operand)
364215c8
RS
5410{
5411 unsigned int mask;
5412 bfd_boolean is_exit;
5413
5414 /* The format is the same for both ENTRY and EXIT, but the constraints
5415 are different. */
5416 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5417 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5418 do
364215c8
RS
5419 {
5420 unsigned int regno1, regno2;
5421 bfd_boolean is_freg;
5422
a92713e6 5423 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5424 is_freg = FALSE;
a92713e6 5425 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5426 is_freg = TRUE;
5427 else
a92713e6 5428 return FALSE;
364215c8
RS
5429
5430 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5431 {
5432 mask &= ~(7 << 3);
5433 mask |= (5 + regno2) << 3;
5434 }
5435 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5436 mask |= (regno2 - 3) << 3;
5437 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5438 mask |= (regno2 - 15) << 1;
5439 else if (regno1 == RA && regno2 == RA)
5440 mask |= 1;
5441 else
a92713e6 5442 return FALSE;
364215c8 5443 }
a92713e6
RS
5444 while (match_char (arg, ','));
5445
364215c8 5446 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5447 return TRUE;
364215c8
RS
5448}
5449
5450/* OP_SAVE_RESTORE_LIST matcher. */
5451
a92713e6
RS
5452static bfd_boolean
5453match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5454{
5455 unsigned int opcode, args, statics, sregs;
5456 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
364215c8 5457 offsetT frame_size;
364215c8 5458
364215c8
RS
5459 opcode = arg->insn->insn_opcode;
5460 frame_size = 0;
5461 num_frame_sizes = 0;
5462 args = 0;
5463 statics = 0;
5464 sregs = 0;
a92713e6 5465 do
364215c8
RS
5466 {
5467 unsigned int regno1, regno2;
5468
a92713e6 5469 if (arg->token->type == OT_INTEGER)
364215c8
RS
5470 {
5471 /* Handle the frame size. */
1a00e612 5472 if (!match_const_int (arg, &frame_size))
a92713e6 5473 return FALSE;
364215c8 5474 num_frame_sizes += 1;
364215c8
RS
5475 }
5476 else
5477 {
a92713e6
RS
5478 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5479 return FALSE;
364215c8
RS
5480
5481 while (regno1 <= regno2)
5482 {
5483 if (regno1 >= 4 && regno1 <= 7)
5484 {
5485 if (num_frame_sizes == 0)
5486 /* args $a0-$a3 */
5487 args |= 1 << (regno1 - 4);
5488 else
5489 /* statics $a0-$a3 */
5490 statics |= 1 << (regno1 - 4);
5491 }
5492 else if (regno1 >= 16 && regno1 <= 23)
5493 /* $s0-$s7 */
5494 sregs |= 1 << (regno1 - 16);
5495 else if (regno1 == 30)
5496 /* $s8 */
5497 sregs |= 1 << 8;
5498 else if (regno1 == 31)
5499 /* Add $ra to insn. */
5500 opcode |= 0x40;
5501 else
a92713e6 5502 return FALSE;
364215c8
RS
5503 regno1 += 1;
5504 if (regno1 == 24)
5505 regno1 = 30;
5506 }
5507 }
364215c8 5508 }
a92713e6 5509 while (match_char (arg, ','));
364215c8
RS
5510
5511 /* Encode args/statics combination. */
5512 if (args & statics)
a92713e6 5513 return FALSE;
364215c8
RS
5514 else if (args == 0xf)
5515 /* All $a0-$a3 are args. */
5516 opcode |= MIPS16_ALL_ARGS << 16;
5517 else if (statics == 0xf)
5518 /* All $a0-$a3 are statics. */
5519 opcode |= MIPS16_ALL_STATICS << 16;
5520 else
5521 {
5522 /* Count arg registers. */
5523 num_args = 0;
5524 while (args & 0x1)
5525 {
5526 args >>= 1;
5527 num_args += 1;
5528 }
5529 if (args != 0)
a92713e6 5530 return FALSE;
364215c8
RS
5531
5532 /* Count static registers. */
5533 num_statics = 0;
5534 while (statics & 0x8)
5535 {
5536 statics = (statics << 1) & 0xf;
5537 num_statics += 1;
5538 }
5539 if (statics != 0)
a92713e6 5540 return FALSE;
364215c8
RS
5541
5542 /* Encode args/statics. */
5543 opcode |= ((num_args << 2) | num_statics) << 16;
5544 }
5545
5546 /* Encode $s0/$s1. */
5547 if (sregs & (1 << 0)) /* $s0 */
5548 opcode |= 0x20;
5549 if (sregs & (1 << 1)) /* $s1 */
5550 opcode |= 0x10;
5551 sregs >>= 2;
5552
5553 /* Encode $s2-$s8. */
5554 num_sregs = 0;
5555 while (sregs & 1)
5556 {
5557 sregs >>= 1;
5558 num_sregs += 1;
5559 }
5560 if (sregs != 0)
a92713e6 5561 return FALSE;
364215c8
RS
5562 opcode |= num_sregs << 24;
5563
5564 /* Encode frame size. */
5565 if (num_frame_sizes == 0)
1a00e612
RS
5566 {
5567 set_insn_error (arg->argnum, _("missing frame size"));
5568 return FALSE;
5569 }
5570 if (num_frame_sizes > 1)
5571 {
5572 set_insn_error (arg->argnum, _("frame size specified twice"));
5573 return FALSE;
5574 }
5575 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5576 {
5577 set_insn_error (arg->argnum, _("invalid frame size"));
5578 return FALSE;
5579 }
5580 if (frame_size != 128 || (opcode >> 16) != 0)
364215c8
RS
5581 {
5582 frame_size /= 8;
5583 opcode |= (((frame_size & 0xf0) << 16)
5584 | (frame_size & 0x0f));
5585 }
5586
364215c8
RS
5587 /* Finally build the instruction. */
5588 if ((opcode >> 16) != 0 || frame_size == 0)
5589 opcode |= MIPS16_EXTEND;
5590 arg->insn->insn_opcode = opcode;
a92713e6 5591 return TRUE;
364215c8
RS
5592}
5593
a1d78564
RS
5594/* OP_MDMX_IMM_REG matcher. */
5595
a92713e6 5596static bfd_boolean
a1d78564 5597match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5598 const struct mips_operand *operand)
a1d78564 5599{
a92713e6 5600 unsigned int regno, uval;
a1d78564
RS
5601 bfd_boolean is_qh;
5602 const struct mips_opcode *opcode;
5603
5604 /* The mips_opcode records whether this is an octobyte or quadhalf
5605 instruction. Start out with that bit in place. */
5606 opcode = arg->insn->insn_mo;
5607 uval = mips_extract_operand (operand, opcode->match);
5608 is_qh = (uval != 0);
5609
56d438b1 5610 if (arg->token->type == OT_REG)
a1d78564
RS
5611 {
5612 if ((opcode->membership & INSN_5400)
5613 && strcmp (opcode->name, "rzu.ob") == 0)
5614 {
1a00e612
RS
5615 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5616 arg->argnum);
5617 return FALSE;
a1d78564
RS
5618 }
5619
56d438b1
CF
5620 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5621 return FALSE;
5622 ++arg->token;
5623
a1d78564
RS
5624 /* Check whether this is a vector register or a broadcast of
5625 a single element. */
56d438b1 5626 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5627 {
56d438b1 5628 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5629 {
1a00e612
RS
5630 set_insn_error (arg->argnum, _("invalid element selector"));
5631 return FALSE;
a1d78564 5632 }
56d438b1
CF
5633 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5634 ++arg->token;
a1d78564
RS
5635 }
5636 else
5637 {
5638 /* A full vector. */
5639 if ((opcode->membership & INSN_5400)
5640 && (strcmp (opcode->name, "sll.ob") == 0
5641 || strcmp (opcode->name, "srl.ob") == 0))
5642 {
1a00e612
RS
5643 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5644 arg->argnum);
5645 return FALSE;
a1d78564
RS
5646 }
5647
5648 if (is_qh)
5649 uval |= MDMX_FMTSEL_VEC_QH << 5;
5650 else
5651 uval |= MDMX_FMTSEL_VEC_OB << 5;
5652 }
a1d78564
RS
5653 uval |= regno;
5654 }
5655 else
5656 {
5657 offsetT sval;
5658
1a00e612 5659 if (!match_const_int (arg, &sval))
a92713e6 5660 return FALSE;
a1d78564
RS
5661 if (sval < 0 || sval > 31)
5662 {
1a00e612
RS
5663 match_out_of_range (arg);
5664 return FALSE;
a1d78564
RS
5665 }
5666 uval |= (sval & 31);
5667 if (is_qh)
5668 uval |= MDMX_FMTSEL_IMM_QH << 5;
5669 else
5670 uval |= MDMX_FMTSEL_IMM_OB << 5;
5671 }
5672 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5673 return TRUE;
a1d78564
RS
5674}
5675
56d438b1
CF
5676/* OP_IMM_INDEX matcher. */
5677
5678static bfd_boolean
5679match_imm_index_operand (struct mips_arg_info *arg,
5680 const struct mips_operand *operand)
5681{
5682 unsigned int max_val;
5683
5684 if (arg->token->type != OT_INTEGER_INDEX)
5685 return FALSE;
5686
5687 max_val = (1 << operand->size) - 1;
5688 if (arg->token->u.index > max_val)
5689 {
5690 match_out_of_range (arg);
5691 return FALSE;
5692 }
5693 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5694 ++arg->token;
5695 return TRUE;
5696}
5697
5698/* OP_REG_INDEX matcher. */
5699
5700static bfd_boolean
5701match_reg_index_operand (struct mips_arg_info *arg,
5702 const struct mips_operand *operand)
5703{
5704 unsigned int regno;
5705
5706 if (arg->token->type != OT_REG_INDEX)
5707 return FALSE;
5708
5709 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5710 return FALSE;
5711
5712 insn_insert_operand (arg->insn, operand, regno);
5713 ++arg->token;
5714 return TRUE;
5715}
5716
a1d78564
RS
5717/* OP_PC matcher. */
5718
a92713e6
RS
5719static bfd_boolean
5720match_pc_operand (struct mips_arg_info *arg)
a1d78564 5721{
a92713e6
RS
5722 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5723 {
5724 ++arg->token;
5725 return TRUE;
5726 }
5727 return FALSE;
a1d78564
RS
5728}
5729
7361da2c
AB
5730/* OP_NON_ZERO_REG matcher. */
5731
5732static bfd_boolean
5733match_non_zero_reg_operand (struct mips_arg_info *arg,
5734 const struct mips_operand *operand)
5735{
5736 unsigned int regno;
5737
5738 if (!match_reg (arg, OP_REG_GP, &regno))
5739 return FALSE;
5740
5741 if (regno == 0)
5742 return FALSE;
5743
5744 arg->last_regno = regno;
5745 insn_insert_operand (arg->insn, operand, regno);
5746 return TRUE;
5747}
5748
a1d78564
RS
5749/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5750 register that we need to match. */
5751
a92713e6
RS
5752static bfd_boolean
5753match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5754{
5755 unsigned int regno;
5756
a92713e6 5757 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5758}
5759
89565f1b
RS
5760/* Read a floating-point constant from S for LI.S or LI.D. LENGTH is
5761 the length of the value in bytes (4 for float, 8 for double) and
5762 USING_GPRS says whether the destination is a GPR rather than an FPR.
5763
5764 Return the constant in IMM and OFFSET as follows:
5765
5766 - If the constant should be loaded via memory, set IMM to O_absent and
5767 OFFSET to the memory address.
5768
5769 - Otherwise, if the constant should be loaded into two 32-bit registers,
5770 set IMM to the O_constant to load into the high register and OFFSET
5771 to the corresponding value for the low register.
5772
5773 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5774
5775 These constants only appear as the last operand in an instruction,
5776 and every instruction that accepts them in any variant accepts them
5777 in all variants. This means we don't have to worry about backing out
5778 any changes if the instruction does not match. We just match
5779 unconditionally and report an error if the constant is invalid. */
5780
a92713e6
RS
5781static bfd_boolean
5782match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5783 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5784{
a92713e6 5785 char *p;
89565f1b
RS
5786 segT seg, new_seg;
5787 subsegT subseg;
5788 const char *newname;
a92713e6 5789 unsigned char *data;
89565f1b
RS
5790
5791 /* Where the constant is placed is based on how the MIPS assembler
5792 does things:
5793
5794 length == 4 && using_gprs -- immediate value only
5795 length == 8 && using_gprs -- .rdata or immediate value
5796 length == 4 && !using_gprs -- .lit4 or immediate value
5797 length == 8 && !using_gprs -- .lit8 or immediate value
5798
5799 The .lit4 and .lit8 sections are only used if permitted by the
5800 -G argument. */
a92713e6 5801 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5802 {
5803 set_insn_error (arg->argnum, _("floating-point expression required"));
5804 return FALSE;
5805 }
a92713e6
RS
5806
5807 gas_assert (arg->token->u.flt.length == length);
5808 data = arg->token->u.flt.data;
5809 ++arg->token;
89565f1b
RS
5810
5811 /* Handle 32-bit constants for which an immediate value is best. */
5812 if (length == 4
5813 && (using_gprs
5814 || g_switch_value < 4
5815 || (data[0] == 0 && data[1] == 0)
5816 || (data[2] == 0 && data[3] == 0)))
5817 {
5818 imm->X_op = O_constant;
5819 if (!target_big_endian)
5820 imm->X_add_number = bfd_getl32 (data);
5821 else
5822 imm->X_add_number = bfd_getb32 (data);
5823 offset->X_op = O_absent;
a92713e6 5824 return TRUE;
89565f1b
RS
5825 }
5826
5827 /* Handle 64-bit constants for which an immediate value is best. */
5828 if (length == 8
5829 && !mips_disable_float_construction
351cdf24
MF
5830 /* Constants can only be constructed in GPRs and copied to FPRs if the
5831 GPRs are at least as wide as the FPRs or MTHC1 is available.
5832 Unlike most tests for 32-bit floating-point registers this check
5833 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
5834 permit 64-bit moves without MXHC1.
5835 Force the constant into memory otherwise. */
5836 && (using_gprs
5837 || GPR_SIZE == 64
5838 || ISA_HAS_MXHC1 (mips_opts.isa)
5839 || FPR_SIZE == 32)
89565f1b
RS
5840 && ((data[0] == 0 && data[1] == 0)
5841 || (data[2] == 0 && data[3] == 0))
5842 && ((data[4] == 0 && data[5] == 0)
5843 || (data[6] == 0 && data[7] == 0)))
5844 {
5845 /* The value is simple enough to load with a couple of instructions.
5846 If using 32-bit registers, set IMM to the high order 32 bits and
5847 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
5848 64 bit constant. */
351cdf24 5849 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
5850 {
5851 imm->X_op = O_constant;
5852 offset->X_op = O_constant;
5853 if (!target_big_endian)
5854 {
5855 imm->X_add_number = bfd_getl32 (data + 4);
5856 offset->X_add_number = bfd_getl32 (data);
5857 }
5858 else
5859 {
5860 imm->X_add_number = bfd_getb32 (data);
5861 offset->X_add_number = bfd_getb32 (data + 4);
5862 }
5863 if (offset->X_add_number == 0)
5864 offset->X_op = O_absent;
5865 }
5866 else
5867 {
5868 imm->X_op = O_constant;
5869 if (!target_big_endian)
5870 imm->X_add_number = bfd_getl64 (data);
5871 else
5872 imm->X_add_number = bfd_getb64 (data);
5873 offset->X_op = O_absent;
5874 }
a92713e6 5875 return TRUE;
89565f1b
RS
5876 }
5877
5878 /* Switch to the right section. */
5879 seg = now_seg;
5880 subseg = now_subseg;
5881 if (length == 4)
5882 {
5883 gas_assert (!using_gprs && g_switch_value >= 4);
5884 newname = ".lit4";
5885 }
5886 else
5887 {
5888 if (using_gprs || g_switch_value < 8)
5889 newname = RDATA_SECTION_NAME;
5890 else
5891 newname = ".lit8";
5892 }
5893
5894 new_seg = subseg_new (newname, (subsegT) 0);
5895 bfd_set_section_flags (stdoutput, new_seg,
5896 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
5897 frag_align (length == 4 ? 2 : 3, 0, 0);
5898 if (strncmp (TARGET_OS, "elf", 3) != 0)
5899 record_alignment (new_seg, 4);
5900 else
5901 record_alignment (new_seg, length == 4 ? 2 : 3);
5902 if (seg == now_seg)
1661c76c 5903 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
5904
5905 /* Set the argument to the current address in the section. */
5906 imm->X_op = O_absent;
5907 offset->X_op = O_symbol;
5908 offset->X_add_symbol = symbol_temp_new_now ();
5909 offset->X_add_number = 0;
5910
5911 /* Put the floating point number into the section. */
5912 p = frag_more (length);
5913 memcpy (p, data, length);
5914
5915 /* Switch back to the original section. */
5916 subseg_set (seg, subseg);
a92713e6 5917 return TRUE;
89565f1b
RS
5918}
5919
14daeee3
RS
5920/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
5921 them. */
5922
5923static bfd_boolean
5924match_vu0_suffix_operand (struct mips_arg_info *arg,
5925 const struct mips_operand *operand,
5926 bfd_boolean match_p)
5927{
5928 unsigned int uval;
5929
5930 /* The operand can be an XYZW mask or a single 2-bit channel index
5931 (with X being 0). */
5932 gas_assert (operand->size == 2 || operand->size == 4);
5933
ee5734f0 5934 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 5935 if (arg->token->type != OT_CHANNELS)
ee5734f0 5936 return match_p;
14daeee3
RS
5937
5938 uval = arg->token->u.channels;
5939 if (operand->size == 2)
5940 {
5941 /* Check that a single bit is set and convert it into a 2-bit index. */
5942 if ((uval & -uval) != uval)
5943 return FALSE;
5944 uval = 4 - ffs (uval);
5945 }
5946
5947 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
5948 return FALSE;
5949
5950 ++arg->token;
5951 if (!match_p)
5952 insn_insert_operand (arg->insn, operand, uval);
5953 return TRUE;
5954}
5955
a1d78564
RS
5956/* S is the text seen for ARG. Match it against OPERAND. Return the end
5957 of the argument text if the match is successful, otherwise return null. */
5958
a92713e6 5959static bfd_boolean
a1d78564 5960match_operand (struct mips_arg_info *arg,
a92713e6 5961 const struct mips_operand *operand)
a1d78564
RS
5962{
5963 switch (operand->type)
5964 {
5965 case OP_INT:
a92713e6 5966 return match_int_operand (arg, operand);
a1d78564
RS
5967
5968 case OP_MAPPED_INT:
a92713e6 5969 return match_mapped_int_operand (arg, operand);
a1d78564
RS
5970
5971 case OP_MSB:
a92713e6 5972 return match_msb_operand (arg, operand);
a1d78564
RS
5973
5974 case OP_REG:
0f35dbc4 5975 case OP_OPTIONAL_REG:
a92713e6 5976 return match_reg_operand (arg, operand);
a1d78564
RS
5977
5978 case OP_REG_PAIR:
a92713e6 5979 return match_reg_pair_operand (arg, operand);
a1d78564
RS
5980
5981 case OP_PCREL:
a92713e6 5982 return match_pcrel_operand (arg);
a1d78564
RS
5983
5984 case OP_PERF_REG:
a92713e6 5985 return match_perf_reg_operand (arg, operand);
a1d78564
RS
5986
5987 case OP_ADDIUSP_INT:
a92713e6 5988 return match_addiusp_operand (arg, operand);
a1d78564
RS
5989
5990 case OP_CLO_CLZ_DEST:
a92713e6 5991 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
5992
5993 case OP_LWM_SWM_LIST:
a92713e6 5994 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
5995
5996 case OP_ENTRY_EXIT_LIST:
a92713e6 5997 return match_entry_exit_operand (arg, operand);
364215c8 5998
a1d78564 5999 case OP_SAVE_RESTORE_LIST:
a92713e6 6000 return match_save_restore_list_operand (arg);
a1d78564
RS
6001
6002 case OP_MDMX_IMM_REG:
a92713e6 6003 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6004
6005 case OP_REPEAT_DEST_REG:
a92713e6 6006 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6007
6008 case OP_REPEAT_PREV_REG:
a92713e6 6009 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6010
6011 case OP_PC:
a92713e6 6012 return match_pc_operand (arg);
14daeee3
RS
6013
6014 case OP_VU0_SUFFIX:
6015 return match_vu0_suffix_operand (arg, operand, FALSE);
6016
6017 case OP_VU0_MATCH_SUFFIX:
6018 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6019
6020 case OP_IMM_INDEX:
6021 return match_imm_index_operand (arg, operand);
6022
6023 case OP_REG_INDEX:
6024 return match_reg_index_operand (arg, operand);
7361da2c
AB
6025
6026 case OP_SAME_RS_RT:
6027 return match_same_rs_rt_operand (arg, operand);
6028
6029 case OP_CHECK_PREV:
6030 return match_check_prev_operand (arg, operand);
6031
6032 case OP_NON_ZERO_REG:
6033 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6034 }
6035 abort ();
6036}
6037
6038/* ARG is the state after successfully matching an instruction.
6039 Issue any queued-up warnings. */
6040
6041static void
6042check_completed_insn (struct mips_arg_info *arg)
6043{
6044 if (arg->seen_at)
6045 {
6046 if (AT == ATREG)
1661c76c 6047 as_warn (_("used $at without \".set noat\""));
a1d78564 6048 else
1661c76c 6049 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6050 }
6051}
a1d78564 6052
85fcb30f
RS
6053/* Return true if modifying general-purpose register REG needs a delay. */
6054
6055static bfd_boolean
6056reg_needs_delay (unsigned int reg)
6057{
6058 unsigned long prev_pinfo;
6059
6060 prev_pinfo = history[0].insn_mo->pinfo;
6061 if (!mips_opts.noreorder
67dc82bc 6062 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6063 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6064 && (gpr_write_mask (&history[0]) & (1 << reg)))
6065 return TRUE;
6066
6067 return FALSE;
6068}
6069
71400594
RS
6070/* Classify an instruction according to the FIX_VR4120_* enumeration.
6071 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6072 by VR4120 errata. */
4d7206a2 6073
71400594
RS
6074static unsigned int
6075classify_vr4120_insn (const char *name)
252b5132 6076{
71400594
RS
6077 if (strncmp (name, "macc", 4) == 0)
6078 return FIX_VR4120_MACC;
6079 if (strncmp (name, "dmacc", 5) == 0)
6080 return FIX_VR4120_DMACC;
6081 if (strncmp (name, "mult", 4) == 0)
6082 return FIX_VR4120_MULT;
6083 if (strncmp (name, "dmult", 5) == 0)
6084 return FIX_VR4120_DMULT;
6085 if (strstr (name, "div"))
6086 return FIX_VR4120_DIV;
6087 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6088 return FIX_VR4120_MTHILO;
6089 return NUM_FIX_VR4120_CLASSES;
6090}
252b5132 6091
a8d14a88
CM
6092#define INSN_ERET 0x42000018
6093#define INSN_DERET 0x4200001f
6094#define INSN_DMULT 0x1c
6095#define INSN_DMULTU 0x1d
ff239038 6096
71400594
RS
6097/* Return the number of instructions that must separate INSN1 and INSN2,
6098 where INSN1 is the earlier instruction. Return the worst-case value
6099 for any INSN2 if INSN2 is null. */
252b5132 6100
71400594
RS
6101static unsigned int
6102insns_between (const struct mips_cl_insn *insn1,
6103 const struct mips_cl_insn *insn2)
6104{
6105 unsigned long pinfo1, pinfo2;
4c260379 6106 unsigned int mask;
71400594 6107
85fcb30f
RS
6108 /* If INFO2 is null, pessimistically assume that all flags are set for
6109 the second instruction. */
71400594
RS
6110 pinfo1 = insn1->insn_mo->pinfo;
6111 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6112
71400594
RS
6113 /* For most targets, write-after-read dependencies on the HI and LO
6114 registers must be separated by at least two instructions. */
6115 if (!hilo_interlocks)
252b5132 6116 {
71400594
RS
6117 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6118 return 2;
6119 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6120 return 2;
6121 }
6122
6123 /* If we're working around r7000 errata, there must be two instructions
6124 between an mfhi or mflo and any instruction that uses the result. */
6125 if (mips_7000_hilo_fix
df58fc94 6126 && !mips_opts.micromips
71400594 6127 && MF_HILO_INSN (pinfo1)
85fcb30f 6128 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6129 return 2;
6130
ff239038
CM
6131 /* If we're working around 24K errata, one instruction is required
6132 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6133 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6134 {
6135 if (insn1->insn_opcode == INSN_ERET
6136 || insn1->insn_opcode == INSN_DERET)
6137 {
6138 if (insn2 == NULL
6139 || insn2->insn_opcode == INSN_ERET
6140 || insn2->insn_opcode == INSN_DERET
11625dd8 6141 || delayed_branch_p (insn2))
ff239038
CM
6142 return 1;
6143 }
6144 }
6145
a8d14a88
CM
6146 /* If we're working around PMC RM7000 errata, there must be three
6147 nops between a dmult and a load instruction. */
6148 if (mips_fix_rm7000 && !mips_opts.micromips)
6149 {
6150 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6151 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6152 {
6153 if (pinfo2 & INSN_LOAD_MEMORY)
6154 return 3;
6155 }
6156 }
6157
71400594
RS
6158 /* If working around VR4120 errata, check for combinations that need
6159 a single intervening instruction. */
df58fc94 6160 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6161 {
6162 unsigned int class1, class2;
252b5132 6163
71400594
RS
6164 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6165 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6166 {
71400594
RS
6167 if (insn2 == NULL)
6168 return 1;
6169 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6170 if (vr4120_conflicts[class1] & (1 << class2))
6171 return 1;
252b5132 6172 }
71400594
RS
6173 }
6174
df58fc94 6175 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6176 {
6177 /* Check for GPR or coprocessor load delays. All such delays
6178 are on the RT register. */
6179 /* Itbl support may require additional care here. */
67dc82bc 6180 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6181 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6182 {
85fcb30f 6183 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6184 return 1;
6185 }
6186
6187 /* Check for generic coprocessor hazards.
6188
6189 This case is not handled very well. There is no special
6190 knowledge of CP0 handling, and the coprocessors other than
6191 the floating point unit are not distinguished at all. */
6192 /* Itbl support may require additional care here. FIXME!
6193 Need to modify this to include knowledge about
6194 user specified delays! */
43885403 6195 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6196 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6197 {
6198 /* Handle cases where INSN1 writes to a known general coprocessor
6199 register. There must be a one instruction delay before INSN2
6200 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6201 mask = fpr_write_mask (insn1);
6202 if (mask != 0)
252b5132 6203 {
4c260379 6204 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6205 return 1;
252b5132
RH
6206 }
6207 else
6208 {
71400594
RS
6209 /* Read-after-write dependencies on the control registers
6210 require a two-instruction gap. */
6211 if ((pinfo1 & INSN_WRITE_COND_CODE)
6212 && (pinfo2 & INSN_READ_COND_CODE))
6213 return 2;
6214
6215 /* We don't know exactly what INSN1 does. If INSN2 is
6216 also a coprocessor instruction, assume there must be
6217 a one instruction gap. */
6218 if (pinfo2 & INSN_COP)
6219 return 1;
252b5132
RH
6220 }
6221 }
6b76fefe 6222
71400594
RS
6223 /* Check for read-after-write dependencies on the coprocessor
6224 control registers in cases where INSN1 does not need a general
6225 coprocessor delay. This means that INSN1 is a floating point
6226 comparison instruction. */
6227 /* Itbl support may require additional care here. */
6228 else if (!cop_interlocks
6229 && (pinfo1 & INSN_WRITE_COND_CODE)
6230 && (pinfo2 & INSN_READ_COND_CODE))
6231 return 1;
6232 }
6b76fefe 6233
7361da2c
AB
6234 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6235 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6236 and pause. */
6237 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6238 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6239 || (insn2 && delayed_branch_p (insn2))))
6240 return 1;
6241
71400594
RS
6242 return 0;
6243}
6b76fefe 6244
7d8e00cf
RS
6245/* Return the number of nops that would be needed to work around the
6246 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6247 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6248 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6249
6250static int
932d1a1b 6251nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6252 const struct mips_cl_insn *insn)
6253{
4c260379
RS
6254 int i, j;
6255 unsigned int mask;
7d8e00cf
RS
6256
6257 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6258 are not affected by the errata. */
6259 if (insn != 0
6260 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6261 || strcmp (insn->insn_mo->name, "mtlo") == 0
6262 || strcmp (insn->insn_mo->name, "mthi") == 0))
6263 return 0;
6264
6265 /* Search for the first MFLO or MFHI. */
6266 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6267 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6268 {
6269 /* Extract the destination register. */
4c260379 6270 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6271
6272 /* No nops are needed if INSN reads that register. */
4c260379 6273 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6274 return 0;
6275
6276 /* ...or if any of the intervening instructions do. */
6277 for (j = 0; j < i; j++)
4c260379 6278 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6279 return 0;
6280
932d1a1b
RS
6281 if (i >= ignore)
6282 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6283 }
6284 return 0;
6285}
6286
134c0c8b
MR
6287#define BASE_REG_EQ(INSN1, INSN2) \
6288 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6289 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6290
6291/* Return the minimum alignment for this store instruction. */
6292
6293static int
6294fix_24k_align_to (const struct mips_opcode *mo)
6295{
6296 if (strcmp (mo->name, "sh") == 0)
6297 return 2;
6298
6299 if (strcmp (mo->name, "swc1") == 0
6300 || strcmp (mo->name, "swc2") == 0
6301 || strcmp (mo->name, "sw") == 0
6302 || strcmp (mo->name, "sc") == 0
6303 || strcmp (mo->name, "s.s") == 0)
6304 return 4;
6305
6306 if (strcmp (mo->name, "sdc1") == 0
6307 || strcmp (mo->name, "sdc2") == 0
6308 || strcmp (mo->name, "s.d") == 0)
6309 return 8;
6310
6311 /* sb, swl, swr */
6312 return 1;
6313}
6314
6315struct fix_24k_store_info
6316 {
6317 /* Immediate offset, if any, for this store instruction. */
6318 short off;
6319 /* Alignment required by this store instruction. */
6320 int align_to;
6321 /* True for register offsets. */
6322 int register_offset;
6323 };
6324
6325/* Comparison function used by qsort. */
6326
6327static int
6328fix_24k_sort (const void *a, const void *b)
6329{
6330 const struct fix_24k_store_info *pos1 = a;
6331 const struct fix_24k_store_info *pos2 = b;
6332
6333 return (pos1->off - pos2->off);
6334}
6335
6336/* INSN is a store instruction. Try to record the store information
6337 in STINFO. Return false if the information isn't known. */
6338
6339static bfd_boolean
6340fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6341 const struct mips_cl_insn *insn)
15be625d
CM
6342{
6343 /* The instruction must have a known offset. */
6344 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6345 return FALSE;
6346
6347 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6348 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6349 return TRUE;
6350}
6351
932d1a1b
RS
6352/* Return the number of nops that would be needed to work around the 24k
6353 "lost data on stores during refill" errata if instruction INSN
6354 immediately followed the 2 instructions described by HIST.
6355 Ignore hazards that are contained within the first IGNORE
6356 instructions of HIST.
6357
6358 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6359 for the data cache refills and store data. The following describes
6360 the scenario where the store data could be lost.
6361
6362 * A data cache miss, due to either a load or a store, causing fill
6363 data to be supplied by the memory subsystem
6364 * The first three doublewords of fill data are returned and written
6365 into the cache
6366 * A sequence of four stores occurs in consecutive cycles around the
6367 final doubleword of the fill:
6368 * Store A
6369 * Store B
6370 * Store C
6371 * Zero, One or more instructions
6372 * Store D
6373
6374 The four stores A-D must be to different doublewords of the line that
6375 is being filled. The fourth instruction in the sequence above permits
6376 the fill of the final doubleword to be transferred from the FSB into
6377 the cache. In the sequence above, the stores may be either integer
6378 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6379 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6380 different doublewords on the line. If the floating point unit is
6381 running in 1:2 mode, it is not possible to create the sequence above
6382 using only floating point store instructions.
15be625d
CM
6383
6384 In this case, the cache line being filled is incorrectly marked
6385 invalid, thereby losing the data from any store to the line that
6386 occurs between the original miss and the completion of the five
6387 cycle sequence shown above.
6388
932d1a1b 6389 The workarounds are:
15be625d 6390
932d1a1b
RS
6391 * Run the data cache in write-through mode.
6392 * Insert a non-store instruction between
6393 Store A and Store B or Store B and Store C. */
3739860c 6394
15be625d 6395static int
932d1a1b 6396nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6397 const struct mips_cl_insn *insn)
6398{
6399 struct fix_24k_store_info pos[3];
6400 int align, i, base_offset;
6401
932d1a1b
RS
6402 if (ignore >= 2)
6403 return 0;
6404
ab9794cf
RS
6405 /* If the previous instruction wasn't a store, there's nothing to
6406 worry about. */
15be625d
CM
6407 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6408 return 0;
6409
ab9794cf
RS
6410 /* If the instructions after the previous one are unknown, we have
6411 to assume the worst. */
6412 if (!insn)
15be625d
CM
6413 return 1;
6414
ab9794cf
RS
6415 /* Check whether we are dealing with three consecutive stores. */
6416 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6417 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6418 return 0;
6419
6420 /* If we don't know the relationship between the store addresses,
6421 assume the worst. */
ab9794cf 6422 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6423 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6424 return 1;
6425
6426 if (!fix_24k_record_store_info (&pos[0], insn)
6427 || !fix_24k_record_store_info (&pos[1], &hist[0])
6428 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6429 return 1;
6430
6431 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6432
6433 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6434 X bytes and such that the base register + X is known to be aligned
6435 to align bytes. */
6436
6437 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6438 align = 8;
6439 else
6440 {
6441 align = pos[0].align_to;
6442 base_offset = pos[0].off;
6443 for (i = 1; i < 3; i++)
6444 if (align < pos[i].align_to)
6445 {
6446 align = pos[i].align_to;
6447 base_offset = pos[i].off;
6448 }
6449 for (i = 0; i < 3; i++)
6450 pos[i].off -= base_offset;
6451 }
6452
6453 pos[0].off &= ~align + 1;
6454 pos[1].off &= ~align + 1;
6455 pos[2].off &= ~align + 1;
6456
6457 /* If any two stores write to the same chunk, they also write to the
6458 same doubleword. The offsets are still sorted at this point. */
6459 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6460 return 0;
6461
6462 /* A range of at least 9 bytes is needed for the stores to be in
6463 non-overlapping doublewords. */
6464 if (pos[2].off - pos[0].off <= 8)
6465 return 0;
6466
6467 if (pos[2].off - pos[1].off >= 24
6468 || pos[1].off - pos[0].off >= 24
6469 || pos[2].off - pos[0].off >= 32)
6470 return 0;
6471
6472 return 1;
6473}
6474
71400594 6475/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6476 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6477 where HIST[0] is the most recent instruction. Ignore hazards
6478 between INSN and the first IGNORE instructions in HIST.
6479
6480 If INSN is null, return the worse-case number of nops for any
6481 instruction. */
bdaaa2e1 6482
71400594 6483static int
932d1a1b 6484nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6485 const struct mips_cl_insn *insn)
6486{
6487 int i, nops, tmp_nops;
bdaaa2e1 6488
71400594 6489 nops = 0;
932d1a1b 6490 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6491 {
91d6fa6a 6492 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6493 if (tmp_nops > nops)
6494 nops = tmp_nops;
6495 }
7d8e00cf 6496
df58fc94 6497 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6498 {
932d1a1b 6499 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6500 if (tmp_nops > nops)
6501 nops = tmp_nops;
6502 }
6503
df58fc94 6504 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6505 {
932d1a1b 6506 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6507 if (tmp_nops > nops)
6508 nops = tmp_nops;
6509 }
6510
71400594
RS
6511 return nops;
6512}
252b5132 6513
71400594 6514/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6515 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6516 would be needed after the extended sequence, ignoring hazards
6517 in the first IGNORE instructions. */
252b5132 6518
71400594 6519static int
932d1a1b
RS
6520nops_for_sequence (int num_insns, int ignore,
6521 const struct mips_cl_insn *hist, ...)
71400594
RS
6522{
6523 va_list args;
6524 struct mips_cl_insn buffer[MAX_NOPS];
6525 struct mips_cl_insn *cursor;
6526 int nops;
6527
91d6fa6a 6528 va_start (args, hist);
71400594 6529 cursor = buffer + num_insns;
91d6fa6a 6530 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6531 while (cursor > buffer)
6532 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6533
932d1a1b 6534 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6535 va_end (args);
6536 return nops;
6537}
252b5132 6538
71400594
RS
6539/* Like nops_for_insn, but if INSN is a branch, take into account the
6540 worst-case delay for the branch target. */
252b5132 6541
71400594 6542static int
932d1a1b 6543nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6544 const struct mips_cl_insn *insn)
6545{
6546 int nops, tmp_nops;
60b63b72 6547
932d1a1b 6548 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6549 if (delayed_branch_p (insn))
71400594 6550 {
932d1a1b 6551 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6552 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6553 if (tmp_nops > nops)
6554 nops = tmp_nops;
6555 }
11625dd8 6556 else if (compact_branch_p (insn))
71400594 6557 {
932d1a1b 6558 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6559 if (tmp_nops > nops)
6560 nops = tmp_nops;
6561 }
6562 return nops;
6563}
6564
c67a084a
NC
6565/* Fix NOP issue: Replace nops by "or at,at,zero". */
6566
6567static void
6568fix_loongson2f_nop (struct mips_cl_insn * ip)
6569{
df58fc94 6570 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6571 if (strcmp (ip->insn_mo->name, "nop") == 0)
6572 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6573}
6574
6575/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6576 jr target pc &= 'hffff_ffff_cfff_ffff. */
6577
6578static void
6579fix_loongson2f_jump (struct mips_cl_insn * ip)
6580{
df58fc94 6581 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6582 if (strcmp (ip->insn_mo->name, "j") == 0
6583 || strcmp (ip->insn_mo->name, "jr") == 0
6584 || strcmp (ip->insn_mo->name, "jalr") == 0)
6585 {
6586 int sreg;
6587 expressionS ep;
6588
6589 if (! mips_opts.at)
6590 return;
6591
df58fc94 6592 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6593 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6594 return;
6595
6596 ep.X_op = O_constant;
6597 ep.X_add_number = 0xcfff0000;
6598 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6599 ep.X_add_number = 0xffff;
6600 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6601 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6602 }
6603}
6604
6605static void
6606fix_loongson2f (struct mips_cl_insn * ip)
6607{
6608 if (mips_fix_loongson2f_nop)
6609 fix_loongson2f_nop (ip);
6610
6611 if (mips_fix_loongson2f_jump)
6612 fix_loongson2f_jump (ip);
6613}
6614
a4e06468
RS
6615/* IP is a branch that has a delay slot, and we need to fill it
6616 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6617 with the previous instruction.
6618 ADDRESS_EXPR is an operand of the instruction to be used with
6619 RELOC_TYPE. */
a4e06468
RS
6620
6621static bfd_boolean
e407c74b 6622can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6623 bfd_reloc_code_real_type *reloc_type)
a4e06468 6624{
2b0c8b40 6625 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6626 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6627 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6628
6629 /* -O2 and above is required for this optimization. */
6630 if (mips_optimize < 2)
6631 return FALSE;
6632
6633 /* If we have seen .set volatile or .set nomove, don't optimize. */
6634 if (mips_opts.nomove)
6635 return FALSE;
6636
6637 /* We can't swap if the previous instruction's position is fixed. */
6638 if (history[0].fixed_p)
6639 return FALSE;
6640
6641 /* If the previous previous insn was in a .set noreorder, we can't
6642 swap. Actually, the MIPS assembler will swap in this situation.
6643 However, gcc configured -with-gnu-as will generate code like
6644
6645 .set noreorder
6646 lw $4,XXX
6647 .set reorder
6648 INSN
6649 bne $4,$0,foo
6650
6651 in which we can not swap the bne and INSN. If gcc is not configured
6652 -with-gnu-as, it does not output the .set pseudo-ops. */
6653 if (history[1].noreorder_p)
6654 return FALSE;
6655
87333bb7
MR
6656 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6657 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6658 if (mips_opts.mips16 && history[0].fixp[0])
6659 return FALSE;
6660
6661 /* If the branch is itself the target of a branch, we can not swap.
6662 We cheat on this; all we check for is whether there is a label on
6663 this instruction. If there are any branches to anything other than
6664 a label, users must use .set noreorder. */
6665 if (seg_info (now_seg)->label_list)
6666 return FALSE;
6667
6668 /* If the previous instruction is in a variant frag other than this
2309ddf2 6669 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6670 MIPS16 code, which uses variant frags for different purposes. */
6671 if (!mips_opts.mips16
a4e06468
RS
6672 && history[0].frag
6673 && history[0].frag->fr_type == rs_machine_dependent)
6674 return FALSE;
6675
bcd530a7
RS
6676 /* We do not swap with instructions that cannot architecturally
6677 be placed in a branch delay slot, such as SYNC or ERET. We
6678 also refrain from swapping with a trap instruction, since it
6679 complicates trap handlers to have the trap instruction be in
6680 a delay slot. */
a4e06468 6681 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6682 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6683 return FALSE;
6684
6685 /* Check for conflicts between the branch and the instructions
6686 before the candidate delay slot. */
6687 if (nops_for_insn (0, history + 1, ip) > 0)
6688 return FALSE;
6689
6690 /* Check for conflicts between the swapped sequence and the
6691 target of the branch. */
6692 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6693 return FALSE;
6694
6695 /* If the branch reads a register that the previous
6696 instruction sets, we can not swap. */
6697 gpr_read = gpr_read_mask (ip);
6698 prev_gpr_write = gpr_write_mask (&history[0]);
6699 if (gpr_read & prev_gpr_write)
6700 return FALSE;
6701
9d5de888
CF
6702 fpr_read = fpr_read_mask (ip);
6703 prev_fpr_write = fpr_write_mask (&history[0]);
6704 if (fpr_read & prev_fpr_write)
6705 return FALSE;
6706
a4e06468
RS
6707 /* If the branch writes a register that the previous
6708 instruction sets, we can not swap. */
6709 gpr_write = gpr_write_mask (ip);
6710 if (gpr_write & prev_gpr_write)
6711 return FALSE;
6712
6713 /* If the branch writes a register that the previous
6714 instruction reads, we can not swap. */
6715 prev_gpr_read = gpr_read_mask (&history[0]);
6716 if (gpr_write & prev_gpr_read)
6717 return FALSE;
6718
6719 /* If one instruction sets a condition code and the
6720 other one uses a condition code, we can not swap. */
6721 pinfo = ip->insn_mo->pinfo;
6722 if ((pinfo & INSN_READ_COND_CODE)
6723 && (prev_pinfo & INSN_WRITE_COND_CODE))
6724 return FALSE;
6725 if ((pinfo & INSN_WRITE_COND_CODE)
6726 && (prev_pinfo & INSN_READ_COND_CODE))
6727 return FALSE;
6728
6729 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6730 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6731 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6732 return FALSE;
a4e06468 6733
df58fc94
RS
6734 /* If the previous instruction has an incorrect size for a fixed
6735 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6736 pinfo2 = ip->insn_mo->pinfo2;
6737 if (mips_opts.micromips
6738 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6739 && insn_length (history) != 2)
6740 return FALSE;
6741 if (mips_opts.micromips
6742 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6743 && insn_length (history) != 4)
6744 return FALSE;
6745
e407c74b
NC
6746 /* On R5900 short loops need to be fixed by inserting a nop in
6747 the branch delay slots.
6748 A short loop can be terminated too early. */
6749 if (mips_opts.arch == CPU_R5900
6750 /* Check if instruction has a parameter, ignore "j $31". */
6751 && (address_expr != NULL)
6752 /* Parameter must be 16 bit. */
6753 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6754 /* Branch to same segment. */
41065f5e 6755 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6756 /* Branch to same code fragment. */
41065f5e 6757 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6758 /* Can only calculate branch offset if value is known. */
41065f5e 6759 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6760 /* Check if branch is really conditional. */
6761 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6762 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6763 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6764 {
6765 int distance;
6766 /* Check if loop is shorter than 6 instructions including
6767 branch and delay slot. */
41065f5e 6768 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6769 if (distance <= 20)
6770 {
6771 int i;
6772 int rv;
6773
6774 rv = FALSE;
6775 /* When the loop includes branches or jumps,
6776 it is not a short loop. */
6777 for (i = 0; i < (distance / 4); i++)
6778 {
6779 if ((history[i].cleared_p)
41065f5e 6780 || delayed_branch_p (&history[i]))
e407c74b
NC
6781 {
6782 rv = TRUE;
6783 break;
6784 }
6785 }
6786 if (rv == FALSE)
6787 {
6788 /* Insert nop after branch to fix short loop. */
6789 return FALSE;
6790 }
6791 }
6792 }
6793
a4e06468
RS
6794 return TRUE;
6795}
6796
e407c74b
NC
6797/* Decide how we should add IP to the instruction stream.
6798 ADDRESS_EXPR is an operand of the instruction to be used with
6799 RELOC_TYPE. */
a4e06468
RS
6800
6801static enum append_method
e407c74b 6802get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6803 bfd_reloc_code_real_type *reloc_type)
a4e06468 6804{
a4e06468
RS
6805 /* The relaxed version of a macro sequence must be inherently
6806 hazard-free. */
6807 if (mips_relax.sequence == 2)
6808 return APPEND_ADD;
6809
6810 /* We must not dabble with instructions in a ".set norerorder" block. */
6811 if (mips_opts.noreorder)
6812 return APPEND_ADD;
6813
6814 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 6815 if (delayed_branch_p (ip))
a4e06468 6816 {
e407c74b
NC
6817 if (!branch_likely_p (ip)
6818 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
6819 return APPEND_SWAP;
6820
6821 if (mips_opts.mips16
6822 && ISA_SUPPORTS_MIPS16E
fc76e730 6823 && gpr_read_mask (ip) != 0)
a4e06468
RS
6824 return APPEND_ADD_COMPACT;
6825
6826 return APPEND_ADD_WITH_NOP;
6827 }
6828
a4e06468
RS
6829 return APPEND_ADD;
6830}
6831
ceb94aa5
RS
6832/* IP is a MIPS16 instruction whose opcode we have just changed.
6833 Point IP->insn_mo to the new opcode's definition. */
6834
6835static void
6836find_altered_mips16_opcode (struct mips_cl_insn *ip)
6837{
6838 const struct mips_opcode *mo, *end;
6839
6840 end = &mips16_opcodes[bfd_mips16_num_opcodes];
6841 for (mo = ip->insn_mo; mo < end; mo++)
6842 if ((ip->insn_opcode & mo->mask) == mo->match)
6843 {
6844 ip->insn_mo = mo;
6845 return;
6846 }
6847 abort ();
6848}
6849
df58fc94
RS
6850/* For microMIPS macros, we need to generate a local number label
6851 as the target of branches. */
6852#define MICROMIPS_LABEL_CHAR '\037'
6853static unsigned long micromips_target_label;
6854static char micromips_target_name[32];
6855
6856static char *
6857micromips_label_name (void)
6858{
6859 char *p = micromips_target_name;
6860 char symbol_name_temporary[24];
6861 unsigned long l;
6862 int i;
6863
6864 if (*p)
6865 return p;
6866
6867 i = 0;
6868 l = micromips_target_label;
6869#ifdef LOCAL_LABEL_PREFIX
6870 *p++ = LOCAL_LABEL_PREFIX;
6871#endif
6872 *p++ = 'L';
6873 *p++ = MICROMIPS_LABEL_CHAR;
6874 do
6875 {
6876 symbol_name_temporary[i++] = l % 10 + '0';
6877 l /= 10;
6878 }
6879 while (l != 0);
6880 while (i > 0)
6881 *p++ = symbol_name_temporary[--i];
6882 *p = '\0';
6883
6884 return micromips_target_name;
6885}
6886
6887static void
6888micromips_label_expr (expressionS *label_expr)
6889{
6890 label_expr->X_op = O_symbol;
6891 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
6892 label_expr->X_add_number = 0;
6893}
6894
6895static void
6896micromips_label_inc (void)
6897{
6898 micromips_target_label++;
6899 *micromips_target_name = '\0';
6900}
6901
6902static void
6903micromips_add_label (void)
6904{
6905 symbolS *s;
6906
6907 s = colon (micromips_label_name ());
6908 micromips_label_inc ();
f3ded42a 6909 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
6910}
6911
6912/* If assembling microMIPS code, then return the microMIPS reloc
6913 corresponding to the requested one if any. Otherwise return
6914 the reloc unchanged. */
6915
6916static bfd_reloc_code_real_type
6917micromips_map_reloc (bfd_reloc_code_real_type reloc)
6918{
6919 static const bfd_reloc_code_real_type relocs[][2] =
6920 {
6921 /* Keep sorted incrementally by the left-hand key. */
6922 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
6923 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
6924 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
6925 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
6926 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
6927 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
6928 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
6929 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
6930 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
6931 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
6932 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
6933 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
6934 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
6935 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
6936 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
6937 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
6938 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
6939 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
6940 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
6941 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
6942 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
6943 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
6944 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
6945 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
6946 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
6947 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
6948 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
6949 };
6950 bfd_reloc_code_real_type r;
6951 size_t i;
6952
6953 if (!mips_opts.micromips)
6954 return reloc;
6955 for (i = 0; i < ARRAY_SIZE (relocs); i++)
6956 {
6957 r = relocs[i][0];
6958 if (r > reloc)
6959 return reloc;
6960 if (r == reloc)
6961 return relocs[i][1];
6962 }
6963 return reloc;
6964}
6965
b886a2ab
RS
6966/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
6967 Return true on success, storing the resolved value in RESULT. */
6968
6969static bfd_boolean
6970calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
6971 offsetT *result)
6972{
6973 switch (reloc)
6974 {
6975 case BFD_RELOC_MIPS_HIGHEST:
6976 case BFD_RELOC_MICROMIPS_HIGHEST:
6977 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
6978 return TRUE;
6979
6980 case BFD_RELOC_MIPS_HIGHER:
6981 case BFD_RELOC_MICROMIPS_HIGHER:
6982 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
6983 return TRUE;
6984
6985 case BFD_RELOC_HI16_S:
41947d9e 6986 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
6987 case BFD_RELOC_MICROMIPS_HI16_S:
6988 case BFD_RELOC_MIPS16_HI16_S:
6989 *result = ((operand + 0x8000) >> 16) & 0xffff;
6990 return TRUE;
6991
6992 case BFD_RELOC_HI16:
6993 case BFD_RELOC_MICROMIPS_HI16:
6994 case BFD_RELOC_MIPS16_HI16:
6995 *result = (operand >> 16) & 0xffff;
6996 return TRUE;
6997
6998 case BFD_RELOC_LO16:
41947d9e 6999 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7000 case BFD_RELOC_MICROMIPS_LO16:
7001 case BFD_RELOC_MIPS16_LO16:
7002 *result = operand & 0xffff;
7003 return TRUE;
7004
7005 case BFD_RELOC_UNUSED:
7006 *result = operand;
7007 return TRUE;
7008
7009 default:
7010 return FALSE;
7011 }
7012}
7013
71400594
RS
7014/* Output an instruction. IP is the instruction information.
7015 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7016 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7017 a macro expansion. */
71400594
RS
7018
7019static void
7020append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7021 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7022{
14fe068b 7023 unsigned long prev_pinfo2, pinfo;
71400594 7024 bfd_boolean relaxed_branch = FALSE;
a4e06468 7025 enum append_method method;
2309ddf2 7026 bfd_boolean relax32;
2b0c8b40 7027 int branch_disp;
71400594 7028
2309ddf2 7029 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7030 fix_loongson2f (ip);
7031
738f4d98 7032 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7033 file_ase_micromips |= mips_opts.micromips;
738f4d98 7034
df58fc94 7035 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7036 pinfo = ip->insn_mo->pinfo;
df58fc94
RS
7037
7038 if (mips_opts.micromips
7039 && !expansionp
7040 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7041 && micromips_insn_length (ip->insn_mo) != 2)
7042 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7043 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7044 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7045 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7046
15be625d
CM
7047 if (address_expr == NULL)
7048 ip->complete_p = 1;
b886a2ab
RS
7049 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7050 && reloc_type[1] == BFD_RELOC_UNUSED
7051 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7052 && address_expr->X_op == O_constant)
7053 {
15be625d
CM
7054 switch (*reloc_type)
7055 {
15be625d 7056 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7057 {
7058 int shift;
7059
17c6c9d9
MR
7060 /* Shift is 2, unusually, for microMIPS JALX. */
7061 shift = (mips_opts.micromips
7062 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7063 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7064 as_bad (_("jump to misaligned address (0x%lx)"),
7065 (unsigned long) address_expr->X_add_number);
7066 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7067 & 0x3ffffff);
335574df 7068 ip->complete_p = 1;
df58fc94 7069 }
15be625d
CM
7070 break;
7071
7072 case BFD_RELOC_MIPS16_JMP:
7073 if ((address_expr->X_add_number & 3) != 0)
7074 as_bad (_("jump to misaligned address (0x%lx)"),
7075 (unsigned long) address_expr->X_add_number);
7076 ip->insn_opcode |=
7077 (((address_expr->X_add_number & 0x7c0000) << 3)
7078 | ((address_expr->X_add_number & 0xf800000) >> 7)
7079 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7080 ip->complete_p = 1;
15be625d
CM
7081 break;
7082
7083 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7084 {
7085 int shift;
7086
7087 shift = mips_opts.micromips ? 1 : 2;
7088 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7089 as_bad (_("branch to misaligned address (0x%lx)"),
7090 (unsigned long) address_expr->X_add_number);
7091 if (!mips_relax_branch)
7092 {
7093 if ((address_expr->X_add_number + (1 << (shift + 15)))
7094 & ~((1 << (shift + 16)) - 1))
7095 as_bad (_("branch address range overflow (0x%lx)"),
7096 (unsigned long) address_expr->X_add_number);
7097 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7098 & 0xffff);
7099 }
df58fc94 7100 }
15be625d
CM
7101 break;
7102
7361da2c
AB
7103 case BFD_RELOC_MIPS_21_PCREL_S2:
7104 {
7105 int shift;
7106
7107 shift = 2;
7108 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7109 as_bad (_("branch to misaligned address (0x%lx)"),
7110 (unsigned long) address_expr->X_add_number);
7111 if ((address_expr->X_add_number + (1 << (shift + 20)))
7112 & ~((1 << (shift + 21)) - 1))
7113 as_bad (_("branch address range overflow (0x%lx)"),
7114 (unsigned long) address_expr->X_add_number);
7115 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7116 & 0x1fffff);
7117 }
7118 break;
7119
7120 case BFD_RELOC_MIPS_26_PCREL_S2:
7121 {
7122 int shift;
7123
7124 shift = 2;
7125 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7126 as_bad (_("branch to misaligned address (0x%lx)"),
7127 (unsigned long) address_expr->X_add_number);
7128 if ((address_expr->X_add_number + (1 << (shift + 25)))
7129 & ~((1 << (shift + 26)) - 1))
7130 as_bad (_("branch address range overflow (0x%lx)"),
7131 (unsigned long) address_expr->X_add_number);
7132 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7133 & 0x3ffffff);
7134 }
7135 break;
7136
15be625d 7137 default:
b886a2ab
RS
7138 {
7139 offsetT value;
7140
7141 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7142 &value))
7143 {
7144 ip->insn_opcode |= value & 0xffff;
7145 ip->complete_p = 1;
7146 }
7147 }
7148 break;
7149 }
15be625d
CM
7150 }
7151
71400594
RS
7152 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7153 {
7154 /* There are a lot of optimizations we could do that we don't.
7155 In particular, we do not, in general, reorder instructions.
7156 If you use gcc with optimization, it will reorder
7157 instructions and generally do much more optimization then we
7158 do here; repeating all that work in the assembler would only
7159 benefit hand written assembly code, and does not seem worth
7160 it. */
7161 int nops = (mips_optimize == 0
932d1a1b
RS
7162 ? nops_for_insn (0, history, NULL)
7163 : nops_for_insn_or_target (0, history, ip));
71400594 7164 if (nops > 0)
252b5132
RH
7165 {
7166 fragS *old_frag;
7167 unsigned long old_frag_offset;
7168 int i;
252b5132
RH
7169
7170 old_frag = frag_now;
7171 old_frag_offset = frag_now_fix ();
7172
7173 for (i = 0; i < nops; i++)
14fe068b
RS
7174 add_fixed_insn (NOP_INSN);
7175 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7176
7177 if (listing)
7178 {
7179 listing_prev_line ();
7180 /* We may be at the start of a variant frag. In case we
7181 are, make sure there is enough space for the frag
7182 after the frags created by listing_prev_line. The
7183 argument to frag_grow here must be at least as large
7184 as the argument to all other calls to frag_grow in
7185 this file. We don't have to worry about being in the
7186 middle of a variant frag, because the variants insert
7187 all needed nop instructions themselves. */
7188 frag_grow (40);
7189 }
7190
462427c4 7191 mips_move_text_labels ();
252b5132
RH
7192
7193#ifndef NO_ECOFF_DEBUGGING
7194 if (ECOFF_DEBUGGING)
7195 ecoff_fix_loc (old_frag, old_frag_offset);
7196#endif
7197 }
71400594
RS
7198 }
7199 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7200 {
932d1a1b
RS
7201 int nops;
7202
7203 /* Work out how many nops in prev_nop_frag are needed by IP,
7204 ignoring hazards generated by the first prev_nop_frag_since
7205 instructions. */
7206 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7207 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7208
71400594
RS
7209 /* Enforce NOPS as a minimum. */
7210 if (nops > prev_nop_frag_required)
7211 prev_nop_frag_required = nops;
252b5132 7212
71400594
RS
7213 if (prev_nop_frag_holds == prev_nop_frag_required)
7214 {
7215 /* Settle for the current number of nops. Update the history
7216 accordingly (for the benefit of any future .set reorder code). */
7217 prev_nop_frag = NULL;
7218 insert_into_history (prev_nop_frag_since,
7219 prev_nop_frag_holds, NOP_INSN);
7220 }
7221 else
7222 {
7223 /* Allow this instruction to replace one of the nops that was
7224 tentatively added to prev_nop_frag. */
df58fc94 7225 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7226 prev_nop_frag_holds--;
7227 prev_nop_frag_since++;
252b5132
RH
7228 }
7229 }
7230
e407c74b 7231 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7232 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7233
e410add4
RS
7234 dwarf2_emit_insn (0);
7235 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7236 so "move" the instruction address accordingly.
7237
7238 Also, it doesn't seem appropriate for the assembler to reorder .loc
7239 entries. If this instruction is a branch that we are going to swap
7240 with the previous instruction, the two instructions should be
7241 treated as a unit, and the debug information for both instructions
7242 should refer to the start of the branch sequence. Using the
7243 current position is certainly wrong when swapping a 32-bit branch
7244 and a 16-bit delay slot, since the current position would then be
7245 in the middle of a branch. */
7246 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7247
df58fc94
RS
7248 relax32 = (mips_relax_branch
7249 /* Don't try branch relaxation within .set nomacro, or within
7250 .set noat if we use $at for PIC computations. If it turns
7251 out that the branch was out-of-range, we'll get an error. */
7252 && !mips_opts.warn_about_macros
7253 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7254 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7255 as they have no complementing branches. */
7256 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7257
7258 if (!HAVE_CODE_COMPRESSION
7259 && address_expr
7260 && relax32
0b25d3e6 7261 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7262 && delayed_branch_p (ip))
4a6a3df4 7263 {
895921c9 7264 relaxed_branch = TRUE;
1e915849
RS
7265 add_relaxed_insn (ip, (relaxed_branch_length
7266 (NULL, NULL,
11625dd8
RS
7267 uncond_branch_p (ip) ? -1
7268 : branch_likely_p (ip) ? 1
1e915849
RS
7269 : 0)), 4,
7270 RELAX_BRANCH_ENCODE
66b3e8da 7271 (AT,
11625dd8
RS
7272 uncond_branch_p (ip),
7273 branch_likely_p (ip),
1e915849
RS
7274 pinfo & INSN_WRITE_GPR_31,
7275 0),
7276 address_expr->X_add_symbol,
7277 address_expr->X_add_number);
4a6a3df4
AO
7278 *reloc_type = BFD_RELOC_UNUSED;
7279 }
df58fc94
RS
7280 else if (mips_opts.micromips
7281 && address_expr
7282 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7283 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7284 && (delayed_branch_p (ip) || compact_branch_p (ip))
7285 /* Don't try branch relaxation when users specify
7286 16-bit/32-bit instructions. */
7287 && !forced_insn_length)
df58fc94
RS
7288 {
7289 bfd_boolean relax16 = *reloc_type > BFD_RELOC_UNUSED;
7290 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8
RS
7291 int uncond = uncond_branch_p (ip) ? -1 : 0;
7292 int compact = compact_branch_p (ip);
df58fc94
RS
7293 int al = pinfo & INSN_WRITE_GPR_31;
7294 int length32;
7295
7296 gas_assert (address_expr != NULL);
7297 gas_assert (!mips_relax.sequence);
7298
2b0c8b40 7299 relaxed_branch = TRUE;
df58fc94
RS
7300 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7301 add_relaxed_insn (ip, relax32 ? length32 : 4, relax16 ? 2 : 4,
40209cad
MR
7302 RELAX_MICROMIPS_ENCODE (type, AT, uncond, compact, al,
7303 relax32, 0, 0),
df58fc94
RS
7304 address_expr->X_add_symbol,
7305 address_expr->X_add_number);
7306 *reloc_type = BFD_RELOC_UNUSED;
7307 }
7308 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7309 {
88a7ef16
MR
7310 symbolS *symbol;
7311 offsetT offset;
7312
252b5132 7313 /* We need to set up a variant frag. */
df58fc94 7314 gas_assert (address_expr != NULL);
88a7ef16
MR
7315 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7316 symbol created by `make_expr_symbol' may not get a necessary
7317 external relocation produced. */
7318 if (address_expr->X_op == O_symbol)
7319 {
7320 symbol = address_expr->X_add_symbol;
7321 offset = address_expr->X_add_number;
7322 }
7323 else
7324 {
7325 symbol = make_expr_symbol (address_expr);
7326 offset = 0;
7327 }
1e915849
RS
7328 add_relaxed_insn (ip, 4, 0,
7329 RELAX_MIPS16_ENCODE
7330 (*reloc_type - BFD_RELOC_UNUSED,
df58fc94 7331 forced_insn_length == 2, forced_insn_length == 4,
11625dd8 7332 delayed_branch_p (&history[0]),
1e915849 7333 history[0].mips16_absolute_jump_p),
88a7ef16 7334 symbol, offset);
252b5132 7335 }
5c04167a 7336 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7337 {
11625dd8 7338 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7339 /* Make sure there is enough room to swap this instruction with
7340 a following jump instruction. */
7341 frag_grow (6);
1e915849 7342 add_fixed_insn (ip);
252b5132
RH
7343 }
7344 else
7345 {
7346 if (mips_opts.mips16
7347 && mips_opts.noreorder
11625dd8 7348 && delayed_branch_p (&history[0]))
252b5132
RH
7349 as_warn (_("extended instruction in delay slot"));
7350
4d7206a2
RS
7351 if (mips_relax.sequence)
7352 {
7353 /* If we've reached the end of this frag, turn it into a variant
7354 frag and record the information for the instructions we've
7355 written so far. */
7356 if (frag_room () < 4)
7357 relax_close_frag ();
df58fc94 7358 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7359 }
7360
584892a6 7361 if (mips_relax.sequence != 2)
df58fc94
RS
7362 {
7363 if (mips_macro_warning.first_insn_sizes[0] == 0)
7364 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7365 mips_macro_warning.sizes[0] += insn_length (ip);
7366 mips_macro_warning.insns[0]++;
7367 }
584892a6 7368 if (mips_relax.sequence != 1)
df58fc94
RS
7369 {
7370 if (mips_macro_warning.first_insn_sizes[1] == 0)
7371 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7372 mips_macro_warning.sizes[1] += insn_length (ip);
7373 mips_macro_warning.insns[1]++;
7374 }
584892a6 7375
1e915849
RS
7376 if (mips_opts.mips16)
7377 {
7378 ip->fixed_p = 1;
7379 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7380 }
7381 add_fixed_insn (ip);
252b5132
RH
7382 }
7383
9fe77896 7384 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7385 {
df58fc94 7386 bfd_reloc_code_real_type final_type[3];
2309ddf2 7387 reloc_howto_type *howto0;
9fe77896
RS
7388 reloc_howto_type *howto;
7389 int i;
34ce925e 7390
df58fc94
RS
7391 /* Perform any necessary conversion to microMIPS relocations
7392 and find out how many relocations there actually are. */
7393 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7394 final_type[i] = micromips_map_reloc (reloc_type[i]);
7395
9fe77896
RS
7396 /* In a compound relocation, it is the final (outermost)
7397 operator that determines the relocated field. */
2309ddf2 7398 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7399 if (!howto)
7400 abort ();
2309ddf2
MR
7401
7402 if (i > 1)
7403 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7404 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7405 bfd_get_reloc_size (howto),
7406 address_expr,
2309ddf2
MR
7407 howto0 && howto0->pc_relative,
7408 final_type[0]);
9fe77896
RS
7409
7410 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7411 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7412 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7413
7414 /* These relocations can have an addend that won't fit in
7415 4 octets for 64bit assembly. */
bad1aba3 7416 if (GPR_SIZE == 64
9fe77896
RS
7417 && ! howto->partial_inplace
7418 && (reloc_type[0] == BFD_RELOC_16
7419 || reloc_type[0] == BFD_RELOC_32
7420 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7421 || reloc_type[0] == BFD_RELOC_GPREL16
7422 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7423 || reloc_type[0] == BFD_RELOC_GPREL32
7424 || reloc_type[0] == BFD_RELOC_64
7425 || reloc_type[0] == BFD_RELOC_CTOR
7426 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7427 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7428 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7429 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7430 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7431 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7432 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7433 || hi16_reloc_p (reloc_type[0])
7434 || lo16_reloc_p (reloc_type[0])))
7435 ip->fixp[0]->fx_no_overflow = 1;
7436
ddaf2c41
MR
7437 /* These relocations can have an addend that won't fit in 2 octets. */
7438 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7439 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7440 ip->fixp[0]->fx_no_overflow = 1;
7441
9fe77896
RS
7442 if (mips_relax.sequence)
7443 {
7444 if (mips_relax.first_fixup == 0)
7445 mips_relax.first_fixup = ip->fixp[0];
7446 }
7447 else if (reloc_needs_lo_p (*reloc_type))
7448 {
7449 struct mips_hi_fixup *hi_fixup;
7450
7451 /* Reuse the last entry if it already has a matching %lo. */
7452 hi_fixup = mips_hi_fixup_list;
7453 if (hi_fixup == 0
7454 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7455 {
325801bd 7456 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7457 hi_fixup->next = mips_hi_fixup_list;
7458 mips_hi_fixup_list = hi_fixup;
4d7206a2 7459 }
9fe77896
RS
7460 hi_fixup->fixp = ip->fixp[0];
7461 hi_fixup->seg = now_seg;
7462 }
252b5132 7463
9fe77896
RS
7464 /* Add fixups for the second and third relocations, if given.
7465 Note that the ABI allows the second relocation to be
7466 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7467 moment we only use RSS_UNDEF, but we could add support
7468 for the others if it ever becomes necessary. */
7469 for (i = 1; i < 3; i++)
7470 if (reloc_type[i] != BFD_RELOC_UNUSED)
7471 {
7472 ip->fixp[i] = fix_new (ip->frag, ip->where,
7473 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7474 FALSE, final_type[i]);
f6688943 7475
9fe77896
RS
7476 /* Use fx_tcbit to mark compound relocs. */
7477 ip->fixp[0]->fx_tcbit = 1;
7478 ip->fixp[i]->fx_tcbit = 1;
7479 }
252b5132 7480 }
1e915849 7481 install_insn (ip);
252b5132
RH
7482
7483 /* Update the register mask information. */
4c260379
RS
7484 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7485 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7486
a4e06468 7487 switch (method)
252b5132 7488 {
a4e06468
RS
7489 case APPEND_ADD:
7490 insert_into_history (0, 1, ip);
7491 break;
7492
7493 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7494 {
7495 struct mips_cl_insn *nop;
7496
7497 insert_into_history (0, 1, ip);
7498 nop = get_delay_slot_nop (ip);
7499 add_fixed_insn (nop);
7500 insert_into_history (0, 1, nop);
7501 if (mips_relax.sequence)
7502 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7503 }
a4e06468
RS
7504 break;
7505
7506 case APPEND_ADD_COMPACT:
7507 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7508 gas_assert (mips_opts.mips16);
7509 ip->insn_opcode |= 0x0080;
7510 find_altered_mips16_opcode (ip);
7511 install_insn (ip);
7512 insert_into_history (0, 1, ip);
7513 break;
7514
7515 case APPEND_SWAP:
7516 {
7517 struct mips_cl_insn delay = history[0];
7518 if (mips_opts.mips16)
7519 {
7520 know (delay.frag == ip->frag);
7521 move_insn (ip, delay.frag, delay.where);
7522 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
7523 }
464ab0e5 7524 else if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7525 {
7526 /* Add the delay slot instruction to the end of the
7527 current frag and shrink the fixed part of the
7528 original frag. If the branch occupies the tail of
7529 the latter, move it backwards to cover the gap. */
2b0c8b40 7530 delay.frag->fr_fix -= branch_disp;
a4e06468 7531 if (delay.frag == ip->frag)
2b0c8b40 7532 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7533 add_fixed_insn (&delay);
7534 }
7535 else
7536 {
2b0c8b40
MR
7537 move_insn (&delay, ip->frag,
7538 ip->where - branch_disp + insn_length (ip));
a4e06468
RS
7539 move_insn (ip, history[0].frag, history[0].where);
7540 }
7541 history[0] = *ip;
7542 delay.fixed_p = 1;
7543 insert_into_history (0, 1, &delay);
7544 }
7545 break;
252b5132
RH
7546 }
7547
13408f1e 7548 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7549 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7550 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7551 {
7552 unsigned int i;
7553
79850f26 7554 mips_no_prev_insn ();
13408f1e 7555
e407c74b 7556 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7557 history[i].cleared_p = 1;
e407c74b
NC
7558 }
7559
df58fc94
RS
7560 /* We need to emit a label at the end of branch-likely macros. */
7561 if (emit_branch_likely_macro)
7562 {
7563 emit_branch_likely_macro = FALSE;
7564 micromips_add_label ();
7565 }
7566
252b5132
RH
7567 /* We just output an insn, so the next one doesn't have a label. */
7568 mips_clear_insn_labels ();
252b5132
RH
7569}
7570
e407c74b
NC
7571/* Forget that there was any previous instruction or label.
7572 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7573
7574static void
7d10b47d 7575mips_no_prev_insn (void)
252b5132 7576{
7d10b47d
RS
7577 prev_nop_frag = NULL;
7578 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7579 mips_clear_insn_labels ();
7580}
7581
7d10b47d
RS
7582/* This function must be called before we emit something other than
7583 instructions. It is like mips_no_prev_insn except that it inserts
7584 any NOPS that might be needed by previous instructions. */
252b5132 7585
7d10b47d
RS
7586void
7587mips_emit_delays (void)
252b5132
RH
7588{
7589 if (! mips_opts.noreorder)
7590 {
932d1a1b 7591 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7592 if (nops > 0)
7593 {
7d10b47d
RS
7594 while (nops-- > 0)
7595 add_fixed_insn (NOP_INSN);
462427c4 7596 mips_move_text_labels ();
7d10b47d
RS
7597 }
7598 }
7599 mips_no_prev_insn ();
7600}
7601
7602/* Start a (possibly nested) noreorder block. */
7603
7604static void
7605start_noreorder (void)
7606{
7607 if (mips_opts.noreorder == 0)
7608 {
7609 unsigned int i;
7610 int nops;
7611
7612 /* None of the instructions before the .set noreorder can be moved. */
7613 for (i = 0; i < ARRAY_SIZE (history); i++)
7614 history[i].fixed_p = 1;
7615
7616 /* Insert any nops that might be needed between the .set noreorder
7617 block and the previous instructions. We will later remove any
7618 nops that turn out not to be needed. */
932d1a1b 7619 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7620 if (nops > 0)
7621 {
7622 if (mips_optimize != 0)
252b5132
RH
7623 {
7624 /* Record the frag which holds the nop instructions, so
7625 that we can remove them if we don't need them. */
df58fc94 7626 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7627 prev_nop_frag = frag_now;
7628 prev_nop_frag_holds = nops;
7629 prev_nop_frag_required = 0;
7630 prev_nop_frag_since = 0;
7631 }
7632
7633 for (; nops > 0; --nops)
1e915849 7634 add_fixed_insn (NOP_INSN);
252b5132 7635
7d10b47d
RS
7636 /* Move on to a new frag, so that it is safe to simply
7637 decrease the size of prev_nop_frag. */
7638 frag_wane (frag_now);
7639 frag_new (0);
462427c4 7640 mips_move_text_labels ();
252b5132 7641 }
df58fc94 7642 mips_mark_labels ();
7d10b47d 7643 mips_clear_insn_labels ();
252b5132 7644 }
7d10b47d
RS
7645 mips_opts.noreorder++;
7646 mips_any_noreorder = 1;
7647}
252b5132 7648
7d10b47d 7649/* End a nested noreorder block. */
252b5132 7650
7d10b47d
RS
7651static void
7652end_noreorder (void)
7653{
7654 mips_opts.noreorder--;
7655 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7656 {
7657 /* Commit to inserting prev_nop_frag_required nops and go back to
7658 handling nop insertion the .set reorder way. */
7659 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7660 * NOP_INSN_SIZE);
7d10b47d
RS
7661 insert_into_history (prev_nop_frag_since,
7662 prev_nop_frag_required, NOP_INSN);
7663 prev_nop_frag = NULL;
7664 }
252b5132
RH
7665}
7666
97d87491
RS
7667/* Sign-extend 32-bit mode constants that have bit 31 set and all
7668 higher bits unset. */
7669
7670static void
7671normalize_constant_expr (expressionS *ex)
7672{
7673 if (ex->X_op == O_constant
7674 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7675 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7676 - 0x80000000);
7677}
7678
7679/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7680 all higher bits unset. */
7681
7682static void
7683normalize_address_expr (expressionS *ex)
7684{
7685 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7686 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7687 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7688 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7689 - 0x80000000);
7690}
7691
7692/* Try to match TOKENS against OPCODE, storing the result in INSN.
7693 Return true if the match was successful.
7694
7695 OPCODE_EXTRA is a value that should be ORed into the opcode
7696 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7697 there are more alternatives after OPCODE and SOFT_MATCH is
7698 as for mips_arg_info. */
7699
7700static bfd_boolean
7701match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
7702 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 7703 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
7704{
7705 const char *args;
7706 struct mips_arg_info arg;
7707 const struct mips_operand *operand;
7708 char c;
7709
7710 imm_expr.X_op = O_absent;
97d87491
RS
7711 offset_expr.X_op = O_absent;
7712 offset_reloc[0] = BFD_RELOC_UNUSED;
7713 offset_reloc[1] = BFD_RELOC_UNUSED;
7714 offset_reloc[2] = BFD_RELOC_UNUSED;
7715
7716 create_insn (insn, opcode);
60f20e8b
RS
7717 /* When no opcode suffix is specified, assume ".xyzw". */
7718 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
7719 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
7720 else
7721 insn->insn_opcode |= opcode_extra;
97d87491
RS
7722 memset (&arg, 0, sizeof (arg));
7723 arg.insn = insn;
7724 arg.token = tokens;
7725 arg.argnum = 1;
7726 arg.last_regno = ILLEGAL_REG;
7727 arg.dest_regno = ILLEGAL_REG;
60f20e8b 7728 arg.lax_match = lax_match;
97d87491
RS
7729 for (args = opcode->args;; ++args)
7730 {
7731 if (arg.token->type == OT_END)
7732 {
7733 /* Handle unary instructions in which only one operand is given.
7734 The source is then the same as the destination. */
7735 if (arg.opnum == 1 && *args == ',')
7736 {
7737 operand = (mips_opts.micromips
7738 ? decode_micromips_operand (args + 1)
7739 : decode_mips_operand (args + 1));
7740 if (operand && mips_optional_operand_p (operand))
7741 {
7742 arg.token = tokens;
7743 arg.argnum = 1;
7744 continue;
7745 }
7746 }
7747
7748 /* Treat elided base registers as $0. */
7749 if (strcmp (args, "(b)") == 0)
7750 args += 3;
7751
7752 if (args[0] == '+')
7753 switch (args[1])
7754 {
7755 case 'K':
7756 case 'N':
7757 /* The register suffix is optional. */
7758 args += 2;
7759 break;
7760 }
7761
7762 /* Fail the match if there were too few operands. */
7763 if (*args)
7764 return FALSE;
7765
7766 /* Successful match. */
60f20e8b
RS
7767 if (!complete_p)
7768 return TRUE;
e3de51ce 7769 clear_insn_error ();
97d87491
RS
7770 if (arg.dest_regno == arg.last_regno
7771 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
7772 {
7773 if (arg.opnum == 2)
e3de51ce 7774 set_insn_error
1661c76c 7775 (0, _("source and destination must be different"));
97d87491 7776 else if (arg.last_regno == 31)
e3de51ce 7777 set_insn_error
1661c76c 7778 (0, _("a destination register must be supplied"));
97d87491 7779 }
173d3447
CF
7780 else if (arg.last_regno == 31
7781 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
7782 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
7783 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
7784 check_completed_insn (&arg);
7785 return TRUE;
7786 }
7787
7788 /* Fail the match if the line has too many operands. */
7789 if (*args == 0)
7790 return FALSE;
7791
7792 /* Handle characters that need to match exactly. */
7793 if (*args == '(' || *args == ')' || *args == ',')
7794 {
7795 if (match_char (&arg, *args))
7796 continue;
7797 return FALSE;
7798 }
7799 if (*args == '#')
7800 {
7801 ++args;
7802 if (arg.token->type == OT_DOUBLE_CHAR
7803 && arg.token->u.ch == *args)
7804 {
7805 ++arg.token;
7806 continue;
7807 }
7808 return FALSE;
7809 }
7810
7811 /* Handle special macro operands. Work out the properties of
7812 other operands. */
7813 arg.opnum += 1;
97d87491
RS
7814 switch (*args)
7815 {
7361da2c
AB
7816 case '-':
7817 switch (args[1])
7818 {
7819 case 'A':
7820 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
7821 break;
7822
7823 case 'B':
7824 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
7825 break;
7826 }
7827 break;
7828
97d87491
RS
7829 case '+':
7830 switch (args[1])
7831 {
97d87491
RS
7832 case 'i':
7833 *offset_reloc = BFD_RELOC_MIPS_JMP;
7834 break;
7361da2c
AB
7835
7836 case '\'':
7837 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
7838 break;
7839
7840 case '\"':
7841 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
7842 break;
97d87491
RS
7843 }
7844 break;
7845
97d87491 7846 case 'I':
1a00e612
RS
7847 if (!match_const_int (&arg, &imm_expr.X_add_number))
7848 return FALSE;
7849 imm_expr.X_op = O_constant;
bad1aba3 7850 if (GPR_SIZE == 32)
97d87491
RS
7851 normalize_constant_expr (&imm_expr);
7852 continue;
7853
7854 case 'A':
7855 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
7856 {
7857 /* Assume that the offset has been elided and that what
7858 we saw was a base register. The match will fail later
7859 if that assumption turns out to be wrong. */
7860 offset_expr.X_op = O_constant;
7861 offset_expr.X_add_number = 0;
7862 }
97d87491 7863 else
1a00e612
RS
7864 {
7865 if (!match_expression (&arg, &offset_expr, offset_reloc))
7866 return FALSE;
7867 normalize_address_expr (&offset_expr);
7868 }
97d87491
RS
7869 continue;
7870
7871 case 'F':
7872 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7873 8, TRUE))
1a00e612 7874 return FALSE;
97d87491
RS
7875 continue;
7876
7877 case 'L':
7878 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7879 8, FALSE))
1a00e612 7880 return FALSE;
97d87491
RS
7881 continue;
7882
7883 case 'f':
7884 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7885 4, TRUE))
1a00e612 7886 return FALSE;
97d87491
RS
7887 continue;
7888
7889 case 'l':
7890 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
7891 4, FALSE))
1a00e612 7892 return FALSE;
97d87491
RS
7893 continue;
7894
97d87491
RS
7895 case 'p':
7896 *offset_reloc = BFD_RELOC_16_PCREL_S2;
7897 break;
7898
7899 case 'a':
7900 *offset_reloc = BFD_RELOC_MIPS_JMP;
7901 break;
7902
7903 case 'm':
7904 gas_assert (mips_opts.micromips);
7905 c = args[1];
7906 switch (c)
7907 {
7908 case 'D':
7909 case 'E':
7910 if (!forced_insn_length)
7911 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
7912 else if (c == 'D')
7913 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
7914 else
7915 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
7916 break;
7917 }
7918 break;
7919 }
7920
7921 operand = (mips_opts.micromips
7922 ? decode_micromips_operand (args)
7923 : decode_mips_operand (args));
7924 if (!operand)
7925 abort ();
7926
7927 /* Skip prefixes. */
7361da2c 7928 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
7929 args++;
7930
7931 if (mips_optional_operand_p (operand)
7932 && args[1] == ','
7933 && (arg.token[0].type != OT_REG
7934 || arg.token[1].type == OT_END))
7935 {
7936 /* Assume that the register has been elided and is the
7937 same as the first operand. */
7938 arg.token = tokens;
7939 arg.argnum = 1;
7940 }
7941
7942 if (!match_operand (&arg, operand))
7943 return FALSE;
7944 }
7945}
7946
7947/* Like match_insn, but for MIPS16. */
7948
7949static bfd_boolean
7950match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 7951 struct mips_operand_token *tokens)
97d87491
RS
7952{
7953 const char *args;
7954 const struct mips_operand *operand;
7955 const struct mips_operand *ext_operand;
7956 struct mips_arg_info arg;
7957 int relax_char;
7958
7959 create_insn (insn, opcode);
7960 imm_expr.X_op = O_absent;
97d87491
RS
7961 offset_expr.X_op = O_absent;
7962 offset_reloc[0] = BFD_RELOC_UNUSED;
7963 offset_reloc[1] = BFD_RELOC_UNUSED;
7964 offset_reloc[2] = BFD_RELOC_UNUSED;
7965 relax_char = 0;
7966
7967 memset (&arg, 0, sizeof (arg));
7968 arg.insn = insn;
7969 arg.token = tokens;
7970 arg.argnum = 1;
7971 arg.last_regno = ILLEGAL_REG;
7972 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
7973 relax_char = 0;
7974 for (args = opcode->args;; ++args)
7975 {
7976 int c;
7977
7978 if (arg.token->type == OT_END)
7979 {
7980 offsetT value;
7981
7982 /* Handle unary instructions in which only one operand is given.
7983 The source is then the same as the destination. */
7984 if (arg.opnum == 1 && *args == ',')
7985 {
7986 operand = decode_mips16_operand (args[1], FALSE);
7987 if (operand && mips_optional_operand_p (operand))
7988 {
7989 arg.token = tokens;
7990 arg.argnum = 1;
7991 continue;
7992 }
7993 }
7994
7995 /* Fail the match if there were too few operands. */
7996 if (*args)
7997 return FALSE;
7998
7999 /* Successful match. Stuff the immediate value in now, if
8000 we can. */
e3de51ce 8001 clear_insn_error ();
97d87491
RS
8002 if (opcode->pinfo == INSN_MACRO)
8003 {
8004 gas_assert (relax_char == 0 || relax_char == 'p');
8005 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8006 }
8007 else if (relax_char
8008 && offset_expr.X_op == O_constant
8009 && calculate_reloc (*offset_reloc,
8010 offset_expr.X_add_number,
8011 &value))
8012 {
8013 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
8014 forced_insn_length, &insn->insn_opcode);
8015 offset_expr.X_op = O_absent;
8016 *offset_reloc = BFD_RELOC_UNUSED;
8017 }
8018 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8019 {
8020 if (forced_insn_length == 2)
e3de51ce 8021 set_insn_error (0, _("invalid unextended operand value"));
97d87491
RS
8022 forced_insn_length = 4;
8023 insn->insn_opcode |= MIPS16_EXTEND;
8024 }
8025 else if (relax_char)
8026 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8027
8028 check_completed_insn (&arg);
8029 return TRUE;
8030 }
8031
8032 /* Fail the match if the line has too many operands. */
8033 if (*args == 0)
8034 return FALSE;
8035
8036 /* Handle characters that need to match exactly. */
8037 if (*args == '(' || *args == ')' || *args == ',')
8038 {
8039 if (match_char (&arg, *args))
8040 continue;
8041 return FALSE;
8042 }
8043
8044 arg.opnum += 1;
8045 c = *args;
8046 switch (c)
8047 {
8048 case 'p':
8049 case 'q':
8050 case 'A':
8051 case 'B':
8052 case 'E':
8053 relax_char = c;
8054 break;
8055
8056 case 'I':
1a00e612
RS
8057 if (!match_const_int (&arg, &imm_expr.X_add_number))
8058 return FALSE;
8059 imm_expr.X_op = O_constant;
bad1aba3 8060 if (GPR_SIZE == 32)
97d87491
RS
8061 normalize_constant_expr (&imm_expr);
8062 continue;
8063
8064 case 'a':
8065 case 'i':
8066 *offset_reloc = BFD_RELOC_MIPS16_JMP;
8067 insn->insn_opcode <<= 16;
8068 break;
8069 }
8070
8071 operand = decode_mips16_operand (c, FALSE);
8072 if (!operand)
8073 abort ();
8074
8075 /* '6' is a special case. It is used for BREAK and SDBBP,
8076 whose operands are only meaningful to the software that decodes
8077 them. This means that there is no architectural reason why
8078 they cannot be prefixed by EXTEND, but in practice,
8079 exception handlers will only look at the instruction
8080 itself. We therefore allow '6' to be extended when
8081 disassembling but not when assembling. */
8082 if (operand->type != OP_PCREL && c != '6')
8083 {
8084 ext_operand = decode_mips16_operand (c, TRUE);
8085 if (operand != ext_operand)
8086 {
8087 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8088 {
8089 offset_expr.X_op = O_constant;
8090 offset_expr.X_add_number = 0;
8091 relax_char = c;
8092 continue;
8093 }
8094
8095 /* We need the OT_INTEGER check because some MIPS16
8096 immediate variants are listed before the register ones. */
8097 if (arg.token->type != OT_INTEGER
8098 || !match_expression (&arg, &offset_expr, offset_reloc))
8099 return FALSE;
8100
8101 /* '8' is used for SLTI(U) and has traditionally not
8102 been allowed to take relocation operators. */
8103 if (offset_reloc[0] != BFD_RELOC_UNUSED
8104 && (ext_operand->size != 16 || c == '8'))
8105 return FALSE;
8106
8107 relax_char = c;
8108 continue;
8109 }
8110 }
8111
8112 if (mips_optional_operand_p (operand)
8113 && args[1] == ','
8114 && (arg.token[0].type != OT_REG
8115 || arg.token[1].type == OT_END))
8116 {
8117 /* Assume that the register has been elided and is the
8118 same as the first operand. */
8119 arg.token = tokens;
8120 arg.argnum = 1;
8121 }
8122
8123 if (!match_operand (&arg, operand))
8124 return FALSE;
8125 }
8126}
8127
60f20e8b
RS
8128/* Record that the current instruction is invalid for the current ISA. */
8129
8130static void
8131match_invalid_for_isa (void)
8132{
8133 set_insn_error_ss
1661c76c 8134 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8135 mips_cpu_info_from_arch (mips_opts.arch)->name,
8136 mips_cpu_info_from_isa (mips_opts.isa)->name);
8137}
8138
8139/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8140 Return true if a definite match or failure was found, storing any match
8141 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8142 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8143 tried and failed to match under normal conditions and now want to try a
8144 more relaxed match. */
8145
8146static bfd_boolean
8147match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8148 const struct mips_opcode *past, struct mips_operand_token *tokens,
8149 int opcode_extra, bfd_boolean lax_match)
8150{
8151 const struct mips_opcode *opcode;
8152 const struct mips_opcode *invalid_delay_slot;
8153 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8154
8155 /* Search for a match, ignoring alternatives that don't satisfy the
8156 current ISA or forced_length. */
8157 invalid_delay_slot = 0;
8158 seen_valid_for_isa = FALSE;
8159 seen_valid_for_size = FALSE;
8160 opcode = first;
8161 do
8162 {
8163 gas_assert (strcmp (opcode->name, first->name) == 0);
8164 if (is_opcode_valid (opcode))
8165 {
8166 seen_valid_for_isa = TRUE;
8167 if (is_size_valid (opcode))
8168 {
8169 bfd_boolean delay_slot_ok;
8170
8171 seen_valid_for_size = TRUE;
8172 delay_slot_ok = is_delay_slot_valid (opcode);
8173 if (match_insn (insn, opcode, tokens, opcode_extra,
8174 lax_match, delay_slot_ok))
8175 {
8176 if (!delay_slot_ok)
8177 {
8178 if (!invalid_delay_slot)
8179 invalid_delay_slot = opcode;
8180 }
8181 else
8182 return TRUE;
8183 }
8184 }
8185 }
8186 ++opcode;
8187 }
8188 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8189
8190 /* If the only matches we found had the wrong length for the delay slot,
8191 pick the first such match. We'll issue an appropriate warning later. */
8192 if (invalid_delay_slot)
8193 {
8194 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8195 lax_match, TRUE))
8196 return TRUE;
8197 abort ();
8198 }
8199
8200 /* Handle the case where we didn't try to match an instruction because
8201 all the alternatives were incompatible with the current ISA. */
8202 if (!seen_valid_for_isa)
8203 {
8204 match_invalid_for_isa ();
8205 return TRUE;
8206 }
8207
8208 /* Handle the case where we didn't try to match an instruction because
8209 all the alternatives were of the wrong size. */
8210 if (!seen_valid_for_size)
8211 {
8212 if (mips_opts.insn32)
1661c76c 8213 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8214 else
8215 set_insn_error_i
1661c76c 8216 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8217 8 * forced_insn_length);
8218 return TRUE;
8219 }
8220
8221 return FALSE;
8222}
8223
8224/* Like match_insns, but for MIPS16. */
8225
8226static bfd_boolean
8227match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8228 struct mips_operand_token *tokens)
8229{
8230 const struct mips_opcode *opcode;
8231 bfd_boolean seen_valid_for_isa;
8232
8233 /* Search for a match, ignoring alternatives that don't satisfy the
8234 current ISA. There are no separate entries for extended forms so
8235 we deal with forced_length later. */
8236 seen_valid_for_isa = FALSE;
8237 opcode = first;
8238 do
8239 {
8240 gas_assert (strcmp (opcode->name, first->name) == 0);
8241 if (is_opcode_valid_16 (opcode))
8242 {
8243 seen_valid_for_isa = TRUE;
8244 if (match_mips16_insn (insn, opcode, tokens))
8245 return TRUE;
8246 }
8247 ++opcode;
8248 }
8249 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8250 && strcmp (opcode->name, first->name) == 0);
8251
8252 /* Handle the case where we didn't try to match an instruction because
8253 all the alternatives were incompatible with the current ISA. */
8254 if (!seen_valid_for_isa)
8255 {
8256 match_invalid_for_isa ();
8257 return TRUE;
8258 }
8259
8260 return FALSE;
8261}
8262
584892a6
RS
8263/* Set up global variables for the start of a new macro. */
8264
8265static void
8266macro_start (void)
8267{
8268 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8269 memset (&mips_macro_warning.first_insn_sizes, 0,
8270 sizeof (mips_macro_warning.first_insn_sizes));
8271 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8272 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8273 && delayed_branch_p (&history[0]));
df58fc94
RS
8274 switch (history[0].insn_mo->pinfo2
8275 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8276 {
8277 case INSN2_BRANCH_DELAY_32BIT:
8278 mips_macro_warning.delay_slot_length = 4;
8279 break;
8280 case INSN2_BRANCH_DELAY_16BIT:
8281 mips_macro_warning.delay_slot_length = 2;
8282 break;
8283 default:
8284 mips_macro_warning.delay_slot_length = 0;
8285 break;
8286 }
8287 mips_macro_warning.first_frag = NULL;
584892a6
RS
8288}
8289
df58fc94
RS
8290/* Given that a macro is longer than one instruction or of the wrong size,
8291 return the appropriate warning for it. Return null if no warning is
8292 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8293 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8294 and RELAX_NOMACRO. */
584892a6
RS
8295
8296static const char *
8297macro_warning (relax_substateT subtype)
8298{
8299 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8300 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8301 " in a branch delay slot");
8302 else if (subtype & RELAX_NOMACRO)
1661c76c 8303 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8304 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8305 | RELAX_DELAY_SLOT_SIZE_SECOND))
8306 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8307 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8308 " in a 16-bit branch delay slot")
1661c76c 8309 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8310 " in a 32-bit branch delay slot"));
584892a6
RS
8311 else
8312 return 0;
8313}
8314
8315/* Finish up a macro. Emit warnings as appropriate. */
8316
8317static void
8318macro_end (void)
8319{
df58fc94
RS
8320 /* Relaxation warning flags. */
8321 relax_substateT subtype = 0;
8322
8323 /* Check delay slot size requirements. */
8324 if (mips_macro_warning.delay_slot_length == 2)
8325 subtype |= RELAX_DELAY_SLOT_16BIT;
8326 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8327 {
df58fc94
RS
8328 if (mips_macro_warning.delay_slot_length
8329 != mips_macro_warning.first_insn_sizes[0])
8330 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8331 if (mips_macro_warning.delay_slot_length
8332 != mips_macro_warning.first_insn_sizes[1])
8333 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8334 }
584892a6 8335
df58fc94
RS
8336 /* Check instruction count requirements. */
8337 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8338 {
8339 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8340 subtype |= RELAX_SECOND_LONGER;
8341 if (mips_opts.warn_about_macros)
8342 subtype |= RELAX_NOMACRO;
8343 if (mips_macro_warning.delay_slot_p)
8344 subtype |= RELAX_DELAY_SLOT;
df58fc94 8345 }
584892a6 8346
df58fc94
RS
8347 /* If both alternatives fail to fill a delay slot correctly,
8348 emit the warning now. */
8349 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8350 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8351 {
8352 relax_substateT s;
8353 const char *msg;
8354
8355 s = subtype & (RELAX_DELAY_SLOT_16BIT
8356 | RELAX_DELAY_SLOT_SIZE_FIRST
8357 | RELAX_DELAY_SLOT_SIZE_SECOND);
8358 msg = macro_warning (s);
8359 if (msg != NULL)
8360 as_warn ("%s", msg);
8361 subtype &= ~s;
8362 }
8363
8364 /* If both implementations are longer than 1 instruction, then emit the
8365 warning now. */
8366 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8367 {
8368 relax_substateT s;
8369 const char *msg;
8370
8371 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8372 msg = macro_warning (s);
8373 if (msg != NULL)
8374 as_warn ("%s", msg);
8375 subtype &= ~s;
584892a6 8376 }
df58fc94
RS
8377
8378 /* If any flags still set, then one implementation might need a warning
8379 and the other either will need one of a different kind or none at all.
8380 Pass any remaining flags over to relaxation. */
8381 if (mips_macro_warning.first_frag != NULL)
8382 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8383}
8384
df58fc94
RS
8385/* Instruction operand formats used in macros that vary between
8386 standard MIPS and microMIPS code. */
8387
833794fc 8388static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8389static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8390static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8391static const char * const lui_fmt[2] = { "t,u", "s,u" };
8392static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8393static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8394static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8395static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8396
833794fc 8397#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8398#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8399 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8400#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8401#define LUI_FMT (lui_fmt[mips_opts.micromips])
8402#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8403#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8404 : mem12_fmt[mips_opts.micromips])
833794fc 8405#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8406#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8407#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8408
6e1304d8
RS
8409/* Read a macro's relocation codes from *ARGS and store them in *R.
8410 The first argument in *ARGS will be either the code for a single
8411 relocation or -1 followed by the three codes that make up a
8412 composite relocation. */
8413
8414static void
8415macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8416{
8417 int i, next;
8418
8419 next = va_arg (*args, int);
8420 if (next >= 0)
8421 r[0] = (bfd_reloc_code_real_type) next;
8422 else
f2ae14a1
RS
8423 {
8424 for (i = 0; i < 3; i++)
8425 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8426 /* This function is only used for 16-bit relocation fields.
8427 To make the macro code simpler, treat an unrelocated value
8428 in the same way as BFD_RELOC_LO16. */
8429 if (r[0] == BFD_RELOC_UNUSED)
8430 r[0] = BFD_RELOC_LO16;
8431 }
6e1304d8
RS
8432}
8433
252b5132
RH
8434/* Build an instruction created by a macro expansion. This is passed
8435 a pointer to the count of instructions created so far, an
8436 expression, the name of the instruction to build, an operand format
8437 string, and corresponding arguments. */
8438
252b5132 8439static void
67c0d1eb 8440macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8441{
df58fc94 8442 const struct mips_opcode *mo = NULL;
f6688943 8443 bfd_reloc_code_real_type r[3];
df58fc94 8444 const struct mips_opcode *amo;
e077a1c8 8445 const struct mips_operand *operand;
df58fc94
RS
8446 struct hash_control *hash;
8447 struct mips_cl_insn insn;
252b5132 8448 va_list args;
e077a1c8 8449 unsigned int uval;
252b5132 8450
252b5132 8451 va_start (args, fmt);
252b5132 8452
252b5132
RH
8453 if (mips_opts.mips16)
8454 {
03ea81db 8455 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8456 va_end (args);
8457 return;
8458 }
8459
f6688943
TS
8460 r[0] = BFD_RELOC_UNUSED;
8461 r[1] = BFD_RELOC_UNUSED;
8462 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8463 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8464 amo = (struct mips_opcode *) hash_find (hash, name);
8465 gas_assert (amo);
8466 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8467
df58fc94 8468 do
8b082fb1
TS
8469 {
8470 /* Search until we get a match for NAME. It is assumed here that
df58fc94
RS
8471 macros will never generate MDMX, MIPS-3D, or MT instructions.
8472 We try to match an instruction that fulfils the branch delay
8473 slot instruction length requirement (if any) of the previous
8474 instruction. While doing this we record the first instruction
8475 seen that matches all the other conditions and use it anyway
8476 if the requirement cannot be met; we will issue an appropriate
8477 warning later on. */
8478 if (strcmp (fmt, amo->args) == 0
8479 && amo->pinfo != INSN_MACRO
8480 && is_opcode_valid (amo)
8481 && is_size_valid (amo))
8482 {
8483 if (is_delay_slot_valid (amo))
8484 {
8485 mo = amo;
8486 break;
8487 }
8488 else if (!mo)
8489 mo = amo;
8490 }
8b082fb1 8491
df58fc94
RS
8492 ++amo;
8493 gas_assert (amo->name);
252b5132 8494 }
df58fc94 8495 while (strcmp (name, amo->name) == 0);
252b5132 8496
df58fc94 8497 gas_assert (mo);
1e915849 8498 create_insn (&insn, mo);
e077a1c8 8499 for (; *fmt; ++fmt)
252b5132 8500 {
e077a1c8 8501 switch (*fmt)
252b5132 8502 {
252b5132
RH
8503 case ',':
8504 case '(':
8505 case ')':
252b5132 8506 case 'z':
e077a1c8 8507 break;
252b5132
RH
8508
8509 case 'i':
8510 case 'j':
6e1304d8 8511 macro_read_relocs (&args, r);
9c2799c2 8512 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8513 || *r == BFD_RELOC_MIPS_HIGHER
8514 || *r == BFD_RELOC_HI16_S
8515 || *r == BFD_RELOC_LO16
8516 || *r == BFD_RELOC_MIPS_GOT_OFST);
e077a1c8 8517 break;
e391c024
RS
8518
8519 case 'o':
8520 macro_read_relocs (&args, r);
e077a1c8 8521 break;
252b5132
RH
8522
8523 case 'u':
6e1304d8 8524 macro_read_relocs (&args, r);
9c2799c2 8525 gas_assert (ep != NULL
90ecf173
MR
8526 && (ep->X_op == O_constant
8527 || (ep->X_op == O_symbol
8528 && (*r == BFD_RELOC_MIPS_HIGHEST
8529 || *r == BFD_RELOC_HI16_S
8530 || *r == BFD_RELOC_HI16
8531 || *r == BFD_RELOC_GPREL16
8532 || *r == BFD_RELOC_MIPS_GOT_HI16
8533 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8534 break;
252b5132
RH
8535
8536 case 'p':
9c2799c2 8537 gas_assert (ep != NULL);
bad36eac 8538
252b5132
RH
8539 /*
8540 * This allows macro() to pass an immediate expression for
8541 * creating short branches without creating a symbol.
bad36eac
DJ
8542 *
8543 * We don't allow branch relaxation for these branches, as
8544 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8545 */
8546 if (ep->X_op == O_constant)
8547 {
df58fc94
RS
8548 /* For microMIPS we always use relocations for branches.
8549 So we should not resolve immediate values. */
8550 gas_assert (!mips_opts.micromips);
8551
bad36eac
DJ
8552 if ((ep->X_add_number & 3) != 0)
8553 as_bad (_("branch to misaligned address (0x%lx)"),
8554 (unsigned long) ep->X_add_number);
8555 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8556 as_bad (_("branch address range overflow (0x%lx)"),
8557 (unsigned long) ep->X_add_number);
252b5132
RH
8558 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8559 ep = NULL;
8560 }
8561 else
0b25d3e6 8562 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8563 break;
252b5132
RH
8564
8565 case 'a':
9c2799c2 8566 gas_assert (ep != NULL);
f6688943 8567 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8568 break;
d43b4baf 8569
252b5132 8570 default:
e077a1c8
RS
8571 operand = (mips_opts.micromips
8572 ? decode_micromips_operand (fmt)
8573 : decode_mips_operand (fmt));
8574 if (!operand)
8575 abort ();
8576
8577 uval = va_arg (args, int);
8578 if (operand->type == OP_CLO_CLZ_DEST)
8579 uval |= (uval << 5);
8580 insn_insert_operand (&insn, operand, uval);
8581
7361da2c 8582 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8583 ++fmt;
8584 break;
252b5132 8585 }
252b5132
RH
8586 }
8587 va_end (args);
9c2799c2 8588 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8589
df58fc94 8590 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8591}
8592
8593static void
67c0d1eb 8594mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8595 va_list *args)
252b5132 8596{
1e915849 8597 struct mips_opcode *mo;
252b5132 8598 struct mips_cl_insn insn;
e077a1c8 8599 const struct mips_operand *operand;
f6688943
TS
8600 bfd_reloc_code_real_type r[3]
8601 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8602
1e915849 8603 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8604 gas_assert (mo);
8605 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8606
1e915849 8607 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8608 {
1e915849 8609 ++mo;
9c2799c2
NC
8610 gas_assert (mo->name);
8611 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8612 }
8613
1e915849 8614 create_insn (&insn, mo);
e077a1c8 8615 for (; *fmt; ++fmt)
252b5132
RH
8616 {
8617 int c;
8618
e077a1c8 8619 c = *fmt;
252b5132
RH
8620 switch (c)
8621 {
252b5132
RH
8622 case ',':
8623 case '(':
8624 case ')':
e077a1c8 8625 break;
252b5132
RH
8626
8627 case '0':
8628 case 'S':
8629 case 'P':
8630 case 'R':
e077a1c8 8631 break;
252b5132
RH
8632
8633 case '<':
8634 case '>':
8635 case '4':
8636 case '5':
8637 case 'H':
8638 case 'W':
8639 case 'D':
8640 case 'j':
8641 case '8':
8642 case 'V':
8643 case 'C':
8644 case 'U':
8645 case 'k':
8646 case 'K':
8647 case 'p':
8648 case 'q':
8649 {
b886a2ab
RS
8650 offsetT value;
8651
9c2799c2 8652 gas_assert (ep != NULL);
252b5132
RH
8653
8654 if (ep->X_op != O_constant)
874e8986 8655 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 8656 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 8657 {
b886a2ab 8658 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 8659 ep = NULL;
f6688943 8660 *r = BFD_RELOC_UNUSED;
252b5132
RH
8661 }
8662 }
e077a1c8 8663 break;
252b5132 8664
e077a1c8
RS
8665 default:
8666 operand = decode_mips16_operand (c, FALSE);
8667 if (!operand)
8668 abort ();
252b5132 8669
4a06e5a2 8670 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
8671 break;
8672 }
252b5132
RH
8673 }
8674
9c2799c2 8675 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8676
df58fc94 8677 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8678}
8679
438c16b8
TS
8680/*
8681 * Generate a "jalr" instruction with a relocation hint to the called
8682 * function. This occurs in NewABI PIC code.
8683 */
8684static void
df58fc94 8685macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 8686{
df58fc94
RS
8687 static const bfd_reloc_code_real_type jalr_relocs[2]
8688 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
8689 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
8690 const char *jalr;
685736be 8691 char *f = NULL;
b34976b6 8692
1180b5a4 8693 if (MIPS_JALR_HINT_P (ep))
f21f8242 8694 {
cc3d92a5 8695 frag_grow (8);
f21f8242
AO
8696 f = frag_more (0);
8697 }
2906b037 8698 if (mips_opts.micromips)
df58fc94 8699 {
833794fc
MR
8700 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
8701 ? "jalr" : "jalrs");
e64af278 8702 if (MIPS_JALR_HINT_P (ep)
833794fc 8703 || mips_opts.insn32
e64af278 8704 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
8705 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
8706 else
8707 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
8708 }
2906b037
MR
8709 else
8710 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 8711 if (MIPS_JALR_HINT_P (ep))
df58fc94 8712 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
8713}
8714
252b5132
RH
8715/*
8716 * Generate a "lui" instruction.
8717 */
8718static void
67c0d1eb 8719macro_build_lui (expressionS *ep, int regnum)
252b5132 8720{
9c2799c2 8721 gas_assert (! mips_opts.mips16);
252b5132 8722
df58fc94 8723 if (ep->X_op != O_constant)
252b5132 8724 {
9c2799c2 8725 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
8726 /* _gp_disp is a special case, used from s_cpload.
8727 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 8728 gas_assert (mips_pic == NO_PIC
78e1bb40 8729 || (! HAVE_NEWABI
aa6975fb
ILT
8730 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
8731 || (! mips_in_shared
bbe506e8
TS
8732 && strcmp (S_GET_NAME (ep->X_add_symbol),
8733 "__gnu_local_gp") == 0));
252b5132
RH
8734 }
8735
df58fc94 8736 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
8737}
8738
885add95
CD
8739/* Generate a sequence of instructions to do a load or store from a constant
8740 offset off of a base register (breg) into/from a target register (treg),
8741 using AT if necessary. */
8742static void
67c0d1eb
RS
8743macro_build_ldst_constoffset (expressionS *ep, const char *op,
8744 int treg, int breg, int dbl)
885add95 8745{
9c2799c2 8746 gas_assert (ep->X_op == O_constant);
885add95 8747
256ab948 8748 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8749 if (!dbl)
8750 normalize_constant_expr (ep);
256ab948 8751
67c1ffbe 8752 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 8753 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
8754 as_warn (_("operand overflow"));
8755
8756 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
8757 {
8758 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 8759 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
8760 }
8761 else
8762 {
8763 /* 32-bit offset, need multiple instructions and AT, like:
8764 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
8765 addu $tempreg,$tempreg,$breg
8766 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
8767 to handle the complete offset. */
67c0d1eb
RS
8768 macro_build_lui (ep, AT);
8769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
8770 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 8771
741fe287 8772 if (!mips_opts.at)
1661c76c 8773 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
8774 }
8775}
8776
252b5132
RH
8777/* set_at()
8778 * Generates code to set the $at register to true (one)
8779 * if reg is less than the immediate expression.
8780 */
8781static void
67c0d1eb 8782set_at (int reg, int unsignedp)
252b5132 8783{
b0e6f033 8784 if (imm_expr.X_add_number >= -0x8000
252b5132 8785 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
8786 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
8787 AT, reg, BFD_RELOC_LO16);
252b5132
RH
8788 else
8789 {
bad1aba3 8790 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 8791 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
8792 }
8793}
8794
252b5132
RH
8795/* Count the leading zeroes by performing a binary chop. This is a
8796 bulky bit of source, but performance is a LOT better for the
8797 majority of values than a simple loop to count the bits:
8798 for (lcnt = 0; (lcnt < 32); lcnt++)
8799 if ((v) & (1 << (31 - lcnt)))
8800 break;
8801 However it is not code size friendly, and the gain will drop a bit
8802 on certain cached systems.
8803*/
8804#define COUNT_TOP_ZEROES(v) \
8805 (((v) & ~0xffff) == 0 \
8806 ? ((v) & ~0xff) == 0 \
8807 ? ((v) & ~0xf) == 0 \
8808 ? ((v) & ~0x3) == 0 \
8809 ? ((v) & ~0x1) == 0 \
8810 ? !(v) \
8811 ? 32 \
8812 : 31 \
8813 : 30 \
8814 : ((v) & ~0x7) == 0 \
8815 ? 29 \
8816 : 28 \
8817 : ((v) & ~0x3f) == 0 \
8818 ? ((v) & ~0x1f) == 0 \
8819 ? 27 \
8820 : 26 \
8821 : ((v) & ~0x7f) == 0 \
8822 ? 25 \
8823 : 24 \
8824 : ((v) & ~0xfff) == 0 \
8825 ? ((v) & ~0x3ff) == 0 \
8826 ? ((v) & ~0x1ff) == 0 \
8827 ? 23 \
8828 : 22 \
8829 : ((v) & ~0x7ff) == 0 \
8830 ? 21 \
8831 : 20 \
8832 : ((v) & ~0x3fff) == 0 \
8833 ? ((v) & ~0x1fff) == 0 \
8834 ? 19 \
8835 : 18 \
8836 : ((v) & ~0x7fff) == 0 \
8837 ? 17 \
8838 : 16 \
8839 : ((v) & ~0xffffff) == 0 \
8840 ? ((v) & ~0xfffff) == 0 \
8841 ? ((v) & ~0x3ffff) == 0 \
8842 ? ((v) & ~0x1ffff) == 0 \
8843 ? 15 \
8844 : 14 \
8845 : ((v) & ~0x7ffff) == 0 \
8846 ? 13 \
8847 : 12 \
8848 : ((v) & ~0x3fffff) == 0 \
8849 ? ((v) & ~0x1fffff) == 0 \
8850 ? 11 \
8851 : 10 \
8852 : ((v) & ~0x7fffff) == 0 \
8853 ? 9 \
8854 : 8 \
8855 : ((v) & ~0xfffffff) == 0 \
8856 ? ((v) & ~0x3ffffff) == 0 \
8857 ? ((v) & ~0x1ffffff) == 0 \
8858 ? 7 \
8859 : 6 \
8860 : ((v) & ~0x7ffffff) == 0 \
8861 ? 5 \
8862 : 4 \
8863 : ((v) & ~0x3fffffff) == 0 \
8864 ? ((v) & ~0x1fffffff) == 0 \
8865 ? 3 \
8866 : 2 \
8867 : ((v) & ~0x7fffffff) == 0 \
8868 ? 1 \
8869 : 0)
8870
8871/* load_register()
67c1ffbe 8872 * This routine generates the least number of instructions necessary to load
252b5132
RH
8873 * an absolute expression value into a register.
8874 */
8875static void
67c0d1eb 8876load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
8877{
8878 int freg;
8879 expressionS hi32, lo32;
8880
8881 if (ep->X_op != O_big)
8882 {
9c2799c2 8883 gas_assert (ep->X_op == O_constant);
256ab948
TS
8884
8885 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
8886 if (!dbl)
8887 normalize_constant_expr (ep);
256ab948
TS
8888
8889 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
8890 {
8891 /* We can handle 16 bit signed values with an addiu to
8892 $zero. No need to ever use daddiu here, since $zero and
8893 the result are always correct in 32 bit mode. */
67c0d1eb 8894 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8895 return;
8896 }
8897 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
8898 {
8899 /* We can handle 16 bit unsigned values with an ori to
8900 $zero. */
67c0d1eb 8901 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8902 return;
8903 }
256ab948 8904 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
8905 {
8906 /* 32 bit values require an lui. */
df58fc94 8907 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8908 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 8909 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
8910 return;
8911 }
8912 }
8913
8914 /* The value is larger than 32 bits. */
8915
bad1aba3 8916 if (!dbl || GPR_SIZE == 32)
252b5132 8917 {
55e08f71
NC
8918 char value[32];
8919
8920 sprintf_vma (value, ep->X_add_number);
1661c76c 8921 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 8922 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
8923 return;
8924 }
8925
8926 if (ep->X_op != O_big)
8927 {
8928 hi32 = *ep;
8929 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8930 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
8931 hi32.X_add_number &= 0xffffffff;
8932 lo32 = *ep;
8933 lo32.X_add_number &= 0xffffffff;
8934 }
8935 else
8936 {
9c2799c2 8937 gas_assert (ep->X_add_number > 2);
252b5132
RH
8938 if (ep->X_add_number == 3)
8939 generic_bignum[3] = 0;
8940 else if (ep->X_add_number > 4)
1661c76c 8941 as_bad (_("number larger than 64 bits"));
252b5132
RH
8942 lo32.X_op = O_constant;
8943 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
8944 hi32.X_op = O_constant;
8945 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
8946 }
8947
8948 if (hi32.X_add_number == 0)
8949 freg = 0;
8950 else
8951 {
8952 int shift, bit;
8953 unsigned long hi, lo;
8954
956cd1d6 8955 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
8956 {
8957 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
8958 {
67c0d1eb 8959 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
8960 return;
8961 }
8962 if (lo32.X_add_number & 0x80000000)
8963 {
df58fc94 8964 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 8965 if (lo32.X_add_number & 0xffff)
67c0d1eb 8966 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
8967 return;
8968 }
8969 }
252b5132
RH
8970
8971 /* Check for 16bit shifted constant. We know that hi32 is
8972 non-zero, so start the mask on the first bit of the hi32
8973 value. */
8974 shift = 17;
8975 do
beae10d5
KH
8976 {
8977 unsigned long himask, lomask;
8978
8979 if (shift < 32)
8980 {
8981 himask = 0xffff >> (32 - shift);
8982 lomask = (0xffff << shift) & 0xffffffff;
8983 }
8984 else
8985 {
8986 himask = 0xffff << (shift - 32);
8987 lomask = 0;
8988 }
8989 if ((hi32.X_add_number & ~(offsetT) himask) == 0
8990 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
8991 {
8992 expressionS tmp;
8993
8994 tmp.X_op = O_constant;
8995 if (shift < 32)
8996 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
8997 | (lo32.X_add_number >> shift));
8998 else
8999 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9000 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9001 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9002 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9003 return;
9004 }
f9419b05 9005 ++shift;
beae10d5
KH
9006 }
9007 while (shift <= (64 - 16));
252b5132
RH
9008
9009 /* Find the bit number of the lowest one bit, and store the
9010 shifted value in hi/lo. */
9011 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9012 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9013 if (lo != 0)
9014 {
9015 bit = 0;
9016 while ((lo & 1) == 0)
9017 {
9018 lo >>= 1;
9019 ++bit;
9020 }
9021 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9022 hi >>= bit;
9023 }
9024 else
9025 {
9026 bit = 32;
9027 while ((hi & 1) == 0)
9028 {
9029 hi >>= 1;
9030 ++bit;
9031 }
9032 lo = hi;
9033 hi = 0;
9034 }
9035
9036 /* Optimize if the shifted value is a (power of 2) - 1. */
9037 if ((hi == 0 && ((lo + 1) & lo) == 0)
9038 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9039 {
9040 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9041 if (shift != 0)
beae10d5 9042 {
252b5132
RH
9043 expressionS tmp;
9044
9045 /* This instruction will set the register to be all
9046 ones. */
beae10d5
KH
9047 tmp.X_op = O_constant;
9048 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9049 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9050 if (bit != 0)
9051 {
9052 bit += shift;
df58fc94 9053 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9054 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9055 }
df58fc94 9056 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9057 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9058 return;
9059 }
9060 }
252b5132
RH
9061
9062 /* Sign extend hi32 before calling load_register, because we can
9063 generally get better code when we load a sign extended value. */
9064 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9065 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9066 load_register (reg, &hi32, 0);
252b5132
RH
9067 freg = reg;
9068 }
9069 if ((lo32.X_add_number & 0xffff0000) == 0)
9070 {
9071 if (freg != 0)
9072 {
df58fc94 9073 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9074 freg = reg;
9075 }
9076 }
9077 else
9078 {
9079 expressionS mid16;
9080
956cd1d6 9081 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9082 {
df58fc94
RS
9083 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9084 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9085 return;
9086 }
252b5132
RH
9087
9088 if (freg != 0)
9089 {
df58fc94 9090 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9091 freg = reg;
9092 }
9093 mid16 = lo32;
9094 mid16.X_add_number >>= 16;
67c0d1eb 9095 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9096 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9097 freg = reg;
9098 }
9099 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9100 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9101}
9102
269137b2
TS
9103static inline void
9104load_delay_nop (void)
9105{
9106 if (!gpr_interlocks)
9107 macro_build (NULL, "nop", "");
9108}
9109
252b5132
RH
9110/* Load an address into a register. */
9111
9112static void
67c0d1eb 9113load_address (int reg, expressionS *ep, int *used_at)
252b5132 9114{
252b5132
RH
9115 if (ep->X_op != O_constant
9116 && ep->X_op != O_symbol)
9117 {
9118 as_bad (_("expression too complex"));
9119 ep->X_op = O_constant;
9120 }
9121
9122 if (ep->X_op == O_constant)
9123 {
67c0d1eb 9124 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9125 return;
9126 }
9127
9128 if (mips_pic == NO_PIC)
9129 {
9130 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9131 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9132 Otherwise we want
9133 lui $reg,<sym> (BFD_RELOC_HI16_S)
9134 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9135 If we have an addend, we always use the latter form.
76b3015f 9136
d6bc6245
TS
9137 With 64bit address space and a usable $at we want
9138 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9139 lui $at,<sym> (BFD_RELOC_HI16_S)
9140 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9141 daddiu $at,<sym> (BFD_RELOC_LO16)
9142 dsll32 $reg,0
3a482fd5 9143 daddu $reg,$reg,$at
76b3015f 9144
c03099e6 9145 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9146 on superscalar processors.
9147 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9148 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9149 dsll $reg,16
9150 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9151 dsll $reg,16
9152 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9153
9154 For GP relative symbols in 64bit address space we can use
9155 the same sequence as in 32bit address space. */
aed1a261 9156 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9157 {
6caf9ef4
TS
9158 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9159 && !nopic_need_relax (ep->X_add_symbol, 1))
9160 {
9161 relax_start (ep->X_add_symbol);
9162 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9163 mips_gp_register, BFD_RELOC_GPREL16);
9164 relax_switch ();
9165 }
d6bc6245 9166
741fe287 9167 if (*used_at == 0 && mips_opts.at)
d6bc6245 9168 {
df58fc94
RS
9169 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9170 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9171 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9172 BFD_RELOC_MIPS_HIGHER);
9173 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9174 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9175 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9176 *used_at = 1;
9177 }
9178 else
9179 {
df58fc94 9180 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9181 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9182 BFD_RELOC_MIPS_HIGHER);
df58fc94 9183 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9184 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9185 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9186 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9187 }
6caf9ef4
TS
9188
9189 if (mips_relax.sequence)
9190 relax_end ();
d6bc6245 9191 }
252b5132
RH
9192 else
9193 {
d6bc6245 9194 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9195 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9196 {
4d7206a2 9197 relax_start (ep->X_add_symbol);
67c0d1eb 9198 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9199 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9200 relax_switch ();
d6bc6245 9201 }
67c0d1eb
RS
9202 macro_build_lui (ep, reg);
9203 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9204 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9205 if (mips_relax.sequence)
9206 relax_end ();
d6bc6245 9207 }
252b5132 9208 }
0a44bf69 9209 else if (!mips_big_got)
252b5132
RH
9210 {
9211 expressionS ex;
9212
9213 /* If this is a reference to an external symbol, we want
9214 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9215 Otherwise we want
9216 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9217 nop
9218 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9219 If there is a constant, it must be added in after.
9220
ed6fb7bd 9221 If we have NewABI, we want
f5040a92
AO
9222 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9223 unless we're referencing a global symbol with a non-zero
9224 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9225 if (HAVE_NEWABI)
9226 {
f5040a92
AO
9227 if (ep->X_add_number)
9228 {
4d7206a2 9229 ex.X_add_number = ep->X_add_number;
f5040a92 9230 ep->X_add_number = 0;
4d7206a2 9231 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9232 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9233 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9234 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9235 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9236 ex.X_op = O_constant;
67c0d1eb 9237 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9238 reg, reg, BFD_RELOC_LO16);
f5040a92 9239 ep->X_add_number = ex.X_add_number;
4d7206a2 9240 relax_switch ();
f5040a92 9241 }
67c0d1eb 9242 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9243 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9244 if (mips_relax.sequence)
9245 relax_end ();
ed6fb7bd
SC
9246 }
9247 else
9248 {
f5040a92
AO
9249 ex.X_add_number = ep->X_add_number;
9250 ep->X_add_number = 0;
67c0d1eb
RS
9251 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9252 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9253 load_delay_nop ();
4d7206a2
RS
9254 relax_start (ep->X_add_symbol);
9255 relax_switch ();
67c0d1eb 9256 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9257 BFD_RELOC_LO16);
4d7206a2 9258 relax_end ();
ed6fb7bd 9259
f5040a92
AO
9260 if (ex.X_add_number != 0)
9261 {
9262 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9263 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9264 ex.X_op = O_constant;
67c0d1eb 9265 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9266 reg, reg, BFD_RELOC_LO16);
f5040a92 9267 }
252b5132
RH
9268 }
9269 }
0a44bf69 9270 else if (mips_big_got)
252b5132
RH
9271 {
9272 expressionS ex;
252b5132
RH
9273
9274 /* This is the large GOT case. If this is a reference to an
9275 external symbol, we want
9276 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9277 addu $reg,$reg,$gp
9278 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9279
9280 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9281 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9282 nop
9283 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9284 If there is a constant, it must be added in after.
f5040a92
AO
9285
9286 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9287 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9288 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9289 */
438c16b8
TS
9290 if (HAVE_NEWABI)
9291 {
4d7206a2 9292 ex.X_add_number = ep->X_add_number;
f5040a92 9293 ep->X_add_number = 0;
4d7206a2 9294 relax_start (ep->X_add_symbol);
df58fc94 9295 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9296 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9297 reg, reg, mips_gp_register);
9298 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9299 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9300 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9301 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9302 else if (ex.X_add_number)
9303 {
9304 ex.X_op = O_constant;
67c0d1eb
RS
9305 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9306 BFD_RELOC_LO16);
f5040a92
AO
9307 }
9308
9309 ep->X_add_number = ex.X_add_number;
4d7206a2 9310 relax_switch ();
67c0d1eb 9311 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9312 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9313 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9314 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9315 relax_end ();
438c16b8 9316 }
252b5132 9317 else
438c16b8 9318 {
f5040a92
AO
9319 ex.X_add_number = ep->X_add_number;
9320 ep->X_add_number = 0;
4d7206a2 9321 relax_start (ep->X_add_symbol);
df58fc94 9322 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9323 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9324 reg, reg, mips_gp_register);
9325 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9326 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9327 relax_switch ();
9328 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9329 {
9330 /* We need a nop before loading from $gp. This special
9331 check is required because the lui which starts the main
9332 instruction stream does not refer to $gp, and so will not
9333 insert the nop which may be required. */
67c0d1eb 9334 macro_build (NULL, "nop", "");
438c16b8 9335 }
67c0d1eb 9336 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9337 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9338 load_delay_nop ();
67c0d1eb 9339 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9340 BFD_RELOC_LO16);
4d7206a2 9341 relax_end ();
438c16b8 9342
f5040a92
AO
9343 if (ex.X_add_number != 0)
9344 {
9345 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9346 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9347 ex.X_op = O_constant;
67c0d1eb
RS
9348 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9349 BFD_RELOC_LO16);
f5040a92 9350 }
252b5132
RH
9351 }
9352 }
252b5132
RH
9353 else
9354 abort ();
8fc2e39e 9355
741fe287 9356 if (!mips_opts.at && *used_at == 1)
1661c76c 9357 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9358}
9359
ea1fb5dc
RS
9360/* Move the contents of register SOURCE into register DEST. */
9361
9362static void
67c0d1eb 9363move_register (int dest, int source)
ea1fb5dc 9364{
df58fc94
RS
9365 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9366 instruction specifically requires a 32-bit one. */
9367 if (mips_opts.micromips
833794fc 9368 && !mips_opts.insn32
df58fc94 9369 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9370 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9371 else
40fc1451 9372 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9373}
9374
4d7206a2 9375/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9376 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9377 The two alternatives are:
4d7206a2
RS
9378
9379 Global symbol Local sybmol
9380 ------------- ------------
9381 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9382 ... ...
9383 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9384
9385 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9386 emits the second for a 16-bit offset or add_got_offset_hilo emits
9387 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9388
9389static void
67c0d1eb 9390load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9391{
9392 expressionS global;
9393
9394 global = *local;
9395 global.X_add_number = 0;
9396
9397 relax_start (local->X_add_symbol);
67c0d1eb
RS
9398 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9399 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9400 relax_switch ();
67c0d1eb
RS
9401 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9402 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9403 relax_end ();
9404}
9405
9406static void
67c0d1eb 9407add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9408{
9409 expressionS global;
9410
9411 global.X_op = O_constant;
9412 global.X_op_symbol = NULL;
9413 global.X_add_symbol = NULL;
9414 global.X_add_number = local->X_add_number;
9415
9416 relax_start (local->X_add_symbol);
67c0d1eb 9417 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9418 dest, dest, BFD_RELOC_LO16);
9419 relax_switch ();
67c0d1eb 9420 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9421 relax_end ();
9422}
9423
f6a22291
MR
9424static void
9425add_got_offset_hilo (int dest, expressionS *local, int tmp)
9426{
9427 expressionS global;
9428 int hold_mips_optimize;
9429
9430 global.X_op = O_constant;
9431 global.X_op_symbol = NULL;
9432 global.X_add_symbol = NULL;
9433 global.X_add_number = local->X_add_number;
9434
9435 relax_start (local->X_add_symbol);
9436 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9437 relax_switch ();
9438 /* Set mips_optimize around the lui instruction to avoid
9439 inserting an unnecessary nop after the lw. */
9440 hold_mips_optimize = mips_optimize;
9441 mips_optimize = 2;
9442 macro_build_lui (&global, tmp);
9443 mips_optimize = hold_mips_optimize;
9444 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9445 relax_end ();
9446
9447 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9448}
9449
df58fc94
RS
9450/* Emit a sequence of instructions to emulate a branch likely operation.
9451 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9452 is its complementing branch with the original condition negated.
9453 CALL is set if the original branch specified the link operation.
9454 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9455
9456 Code like this is produced in the noreorder mode:
9457
9458 BRNEG <args>, 1f
9459 nop
9460 b <sym>
9461 delay slot (executed only if branch taken)
9462 1:
9463
9464 or, if CALL is set:
9465
9466 BRNEG <args>, 1f
9467 nop
9468 bal <sym>
9469 delay slot (executed only if branch taken)
9470 1:
9471
9472 In the reorder mode the delay slot would be filled with a nop anyway,
9473 so code produced is simply:
9474
9475 BR <args>, <sym>
9476 nop
9477
9478 This function is used when producing code for the microMIPS ASE that
9479 does not implement branch likely instructions in hardware. */
9480
9481static void
9482macro_build_branch_likely (const char *br, const char *brneg,
9483 int call, expressionS *ep, const char *fmt,
9484 unsigned int sreg, unsigned int treg)
9485{
9486 int noreorder = mips_opts.noreorder;
9487 expressionS expr1;
9488
9489 gas_assert (mips_opts.micromips);
9490 start_noreorder ();
9491 if (noreorder)
9492 {
9493 micromips_label_expr (&expr1);
9494 macro_build (&expr1, brneg, fmt, sreg, treg);
9495 macro_build (NULL, "nop", "");
9496 macro_build (ep, call ? "bal" : "b", "p");
9497
9498 /* Set to true so that append_insn adds a label. */
9499 emit_branch_likely_macro = TRUE;
9500 }
9501 else
9502 {
9503 macro_build (ep, br, fmt, sreg, treg);
9504 macro_build (NULL, "nop", "");
9505 }
9506 end_noreorder ();
9507}
9508
9509/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9510 the condition code tested. EP specifies the branch target. */
9511
9512static void
9513macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9514{
9515 const int call = 0;
9516 const char *brneg;
9517 const char *br;
9518
9519 switch (type)
9520 {
9521 case M_BC1FL:
9522 br = "bc1f";
9523 brneg = "bc1t";
9524 break;
9525 case M_BC1TL:
9526 br = "bc1t";
9527 brneg = "bc1f";
9528 break;
9529 case M_BC2FL:
9530 br = "bc2f";
9531 brneg = "bc2t";
9532 break;
9533 case M_BC2TL:
9534 br = "bc2t";
9535 brneg = "bc2f";
9536 break;
9537 default:
9538 abort ();
9539 }
9540 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9541}
9542
9543/* Emit a two-argument branch macro specified by TYPE, using SREG as
9544 the register tested. EP specifies the branch target. */
9545
9546static void
9547macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9548{
9549 const char *brneg = NULL;
9550 const char *br;
9551 int call = 0;
9552
9553 switch (type)
9554 {
9555 case M_BGEZ:
9556 br = "bgez";
9557 break;
9558 case M_BGEZL:
9559 br = mips_opts.micromips ? "bgez" : "bgezl";
9560 brneg = "bltz";
9561 break;
9562 case M_BGEZALL:
9563 gas_assert (mips_opts.micromips);
833794fc 9564 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9565 brneg = "bltz";
9566 call = 1;
9567 break;
9568 case M_BGTZ:
9569 br = "bgtz";
9570 break;
9571 case M_BGTZL:
9572 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9573 brneg = "blez";
9574 break;
9575 case M_BLEZ:
9576 br = "blez";
9577 break;
9578 case M_BLEZL:
9579 br = mips_opts.micromips ? "blez" : "blezl";
9580 brneg = "bgtz";
9581 break;
9582 case M_BLTZ:
9583 br = "bltz";
9584 break;
9585 case M_BLTZL:
9586 br = mips_opts.micromips ? "bltz" : "bltzl";
9587 brneg = "bgez";
9588 break;
9589 case M_BLTZALL:
9590 gas_assert (mips_opts.micromips);
833794fc 9591 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9592 brneg = "bgez";
9593 call = 1;
9594 break;
9595 default:
9596 abort ();
9597 }
9598 if (mips_opts.micromips && brneg)
9599 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9600 else
9601 macro_build (ep, br, "s,p", sreg);
9602}
9603
9604/* Emit a three-argument branch macro specified by TYPE, using SREG and
9605 TREG as the registers tested. EP specifies the branch target. */
9606
9607static void
9608macro_build_branch_rsrt (int type, expressionS *ep,
9609 unsigned int sreg, unsigned int treg)
9610{
9611 const char *brneg = NULL;
9612 const int call = 0;
9613 const char *br;
9614
9615 switch (type)
9616 {
9617 case M_BEQ:
9618 case M_BEQ_I:
9619 br = "beq";
9620 break;
9621 case M_BEQL:
9622 case M_BEQL_I:
9623 br = mips_opts.micromips ? "beq" : "beql";
9624 brneg = "bne";
9625 break;
9626 case M_BNE:
9627 case M_BNE_I:
9628 br = "bne";
9629 break;
9630 case M_BNEL:
9631 case M_BNEL_I:
9632 br = mips_opts.micromips ? "bne" : "bnel";
9633 brneg = "beq";
9634 break;
9635 default:
9636 abort ();
9637 }
9638 if (mips_opts.micromips && brneg)
9639 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9640 else
9641 macro_build (ep, br, "s,t,p", sreg, treg);
9642}
9643
f2ae14a1
RS
9644/* Return the high part that should be loaded in order to make the low
9645 part of VALUE accessible using an offset of OFFBITS bits. */
9646
9647static offsetT
9648offset_high_part (offsetT value, unsigned int offbits)
9649{
9650 offsetT bias;
9651 addressT low_mask;
9652
9653 if (offbits == 0)
9654 return value;
9655 bias = 1 << (offbits - 1);
9656 low_mask = bias * 2 - 1;
9657 return (value + bias) & ~low_mask;
9658}
9659
9660/* Return true if the value stored in offset_expr and offset_reloc
9661 fits into a signed offset of OFFBITS bits. RANGE is the maximum
9662 amount that the caller wants to add without inducing overflow
9663 and ALIGN is the known alignment of the value in bytes. */
9664
9665static bfd_boolean
9666small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
9667{
9668 if (offbits == 16)
9669 {
9670 /* Accept any relocation operator if overflow isn't a concern. */
9671 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
9672 return TRUE;
9673
9674 /* These relocations are guaranteed not to overflow in correct links. */
9675 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
9676 || gprel16_reloc_p (*offset_reloc))
9677 return TRUE;
9678 }
9679 if (offset_expr.X_op == O_constant
9680 && offset_high_part (offset_expr.X_add_number, offbits) == 0
9681 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
9682 return TRUE;
9683 return FALSE;
9684}
9685
252b5132
RH
9686/*
9687 * Build macros
9688 * This routine implements the seemingly endless macro or synthesized
9689 * instructions and addressing modes in the mips assembly language. Many
9690 * of these macros are simple and are similar to each other. These could
67c1ffbe 9691 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
9692 * this verbose method. Others are not simple macros but are more like
9693 * optimizing code generation.
9694 * One interesting optimization is when several store macros appear
67c1ffbe 9695 * consecutively that would load AT with the upper half of the same address.
252b5132
RH
9696 * The ensuing load upper instructions are ommited. This implies some kind
9697 * of global optimization. We currently only optimize within a single macro.
9698 * For many of the load and store macros if the address is specified as a
9699 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
9700 * first load register 'at' with zero and use it as the base register. The
9701 * mips assembler simply uses register $zero. Just one tiny optimization
9702 * we're missing.
9703 */
9704static void
833794fc 9705macro (struct mips_cl_insn *ip, char *str)
252b5132 9706{
c0ebe874
RS
9707 const struct mips_operand_array *operands;
9708 unsigned int breg, i;
741fe287 9709 unsigned int tempreg;
252b5132 9710 int mask;
43841e91 9711 int used_at = 0;
df58fc94 9712 expressionS label_expr;
252b5132 9713 expressionS expr1;
df58fc94 9714 expressionS *ep;
252b5132
RH
9715 const char *s;
9716 const char *s2;
9717 const char *fmt;
9718 int likely = 0;
252b5132 9719 int coproc = 0;
7f3c4072 9720 int offbits = 16;
1abe91b1 9721 int call = 0;
df58fc94
RS
9722 int jals = 0;
9723 int dbl = 0;
9724 int imm = 0;
9725 int ust = 0;
9726 int lp = 0;
f2ae14a1 9727 bfd_boolean large_offset;
252b5132 9728 int off;
252b5132 9729 int hold_mips_optimize;
f2ae14a1 9730 unsigned int align;
c0ebe874 9731 unsigned int op[MAX_OPERANDS];
252b5132 9732
9c2799c2 9733 gas_assert (! mips_opts.mips16);
252b5132 9734
c0ebe874
RS
9735 operands = insn_operands (ip);
9736 for (i = 0; i < MAX_OPERANDS; i++)
9737 if (operands->operand[i])
9738 op[i] = insn_extract_operand (ip, operands->operand[i]);
9739 else
9740 op[i] = -1;
9741
252b5132
RH
9742 mask = ip->insn_mo->mask;
9743
df58fc94
RS
9744 label_expr.X_op = O_constant;
9745 label_expr.X_op_symbol = NULL;
9746 label_expr.X_add_symbol = NULL;
9747 label_expr.X_add_number = 0;
9748
252b5132
RH
9749 expr1.X_op = O_constant;
9750 expr1.X_op_symbol = NULL;
9751 expr1.X_add_symbol = NULL;
9752 expr1.X_add_number = 1;
f2ae14a1 9753 align = 1;
252b5132
RH
9754
9755 switch (mask)
9756 {
9757 case M_DABS:
9758 dbl = 1;
9759 case M_ABS:
df58fc94
RS
9760 /* bgez $a0,1f
9761 move v0,$a0
9762 sub v0,$zero,$a0
9763 1:
9764 */
252b5132 9765
7d10b47d 9766 start_noreorder ();
252b5132 9767
df58fc94
RS
9768 if (mips_opts.micromips)
9769 micromips_label_expr (&label_expr);
9770 else
9771 label_expr.X_add_number = 8;
c0ebe874
RS
9772 macro_build (&label_expr, "bgez", "s,p", op[1]);
9773 if (op[0] == op[1])
a605d2b3 9774 macro_build (NULL, "nop", "");
252b5132 9775 else
c0ebe874
RS
9776 move_register (op[0], op[1]);
9777 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
9778 if (mips_opts.micromips)
9779 micromips_add_label ();
252b5132 9780
7d10b47d 9781 end_noreorder ();
8fc2e39e 9782 break;
252b5132
RH
9783
9784 case M_ADD_I:
9785 s = "addi";
9786 s2 = "add";
9787 goto do_addi;
9788 case M_ADDU_I:
9789 s = "addiu";
9790 s2 = "addu";
9791 goto do_addi;
9792 case M_DADD_I:
9793 dbl = 1;
9794 s = "daddi";
9795 s2 = "dadd";
df58fc94
RS
9796 if (!mips_opts.micromips)
9797 goto do_addi;
b0e6f033 9798 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
9799 && imm_expr.X_add_number < 0x200)
9800 {
b0e6f033
RS
9801 macro_build (NULL, s, "t,r,.", op[0], op[1],
9802 (int) imm_expr.X_add_number);
df58fc94
RS
9803 break;
9804 }
9805 goto do_addi_i;
252b5132
RH
9806 case M_DADDU_I:
9807 dbl = 1;
9808 s = "daddiu";
9809 s2 = "daddu";
9810 do_addi:
b0e6f033 9811 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
9812 && imm_expr.X_add_number < 0x8000)
9813 {
c0ebe874 9814 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 9815 break;
252b5132 9816 }
df58fc94 9817 do_addi_i:
8fc2e39e 9818 used_at = 1;
67c0d1eb 9819 load_register (AT, &imm_expr, dbl);
c0ebe874 9820 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9821 break;
9822
9823 case M_AND_I:
9824 s = "andi";
9825 s2 = "and";
9826 goto do_bit;
9827 case M_OR_I:
9828 s = "ori";
9829 s2 = "or";
9830 goto do_bit;
9831 case M_NOR_I:
9832 s = "";
9833 s2 = "nor";
9834 goto do_bit;
9835 case M_XOR_I:
9836 s = "xori";
9837 s2 = "xor";
9838 do_bit:
b0e6f033 9839 if (imm_expr.X_add_number >= 0
252b5132
RH
9840 && imm_expr.X_add_number < 0x10000)
9841 {
9842 if (mask != M_NOR_I)
c0ebe874 9843 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
9844 else
9845 {
67c0d1eb 9846 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
9847 op[0], op[1], BFD_RELOC_LO16);
9848 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 9849 }
8fc2e39e 9850 break;
252b5132
RH
9851 }
9852
8fc2e39e 9853 used_at = 1;
bad1aba3 9854 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 9855 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
9856 break;
9857
8b082fb1
TS
9858 case M_BALIGN:
9859 switch (imm_expr.X_add_number)
9860 {
9861 case 0:
9862 macro_build (NULL, "nop", "");
9863 break;
9864 case 2:
c0ebe874 9865 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 9866 break;
03f66e8a
MR
9867 case 1:
9868 case 3:
c0ebe874 9869 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 9870 (int) imm_expr.X_add_number);
8b082fb1 9871 break;
03f66e8a
MR
9872 default:
9873 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
9874 (unsigned long) imm_expr.X_add_number);
9875 break;
8b082fb1
TS
9876 }
9877 break;
9878
df58fc94
RS
9879 case M_BC1FL:
9880 case M_BC1TL:
9881 case M_BC2FL:
9882 case M_BC2TL:
9883 gas_assert (mips_opts.micromips);
9884 macro_build_branch_ccl (mask, &offset_expr,
9885 EXTRACT_OPERAND (1, BCC, *ip));
9886 break;
9887
252b5132 9888 case M_BEQ_I:
252b5132 9889 case M_BEQL_I:
252b5132 9890 case M_BNE_I:
252b5132 9891 case M_BNEL_I:
b0e6f033 9892 if (imm_expr.X_add_number == 0)
c0ebe874 9893 op[1] = 0;
df58fc94 9894 else
252b5132 9895 {
c0ebe874 9896 op[1] = AT;
df58fc94 9897 used_at = 1;
bad1aba3 9898 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 9899 }
df58fc94
RS
9900 /* Fall through. */
9901 case M_BEQL:
9902 case M_BNEL:
c0ebe874 9903 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
9904 break;
9905
9906 case M_BGEL:
9907 likely = 1;
9908 case M_BGE:
c0ebe874
RS
9909 if (op[1] == 0)
9910 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
9911 else if (op[0] == 0)
9912 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 9913 else
252b5132 9914 {
df58fc94 9915 used_at = 1;
c0ebe874 9916 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9917 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9918 &offset_expr, AT, ZERO);
252b5132 9919 }
df58fc94
RS
9920 break;
9921
9922 case M_BGEZL:
9923 case M_BGEZALL:
9924 case M_BGTZL:
9925 case M_BLEZL:
9926 case M_BLTZL:
9927 case M_BLTZALL:
c0ebe874 9928 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
9929 break;
9930
9931 case M_BGTL_I:
9932 likely = 1;
9933 case M_BGT_I:
90ecf173 9934 /* Check for > max integer. */
b0e6f033 9935 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
9936 {
9937 do_false:
90ecf173 9938 /* Result is always false. */
252b5132 9939 if (! likely)
a605d2b3 9940 macro_build (NULL, "nop", "");
252b5132 9941 else
df58fc94 9942 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 9943 break;
252b5132 9944 }
f9419b05 9945 ++imm_expr.X_add_number;
252b5132
RH
9946 /* FALLTHROUGH */
9947 case M_BGE_I:
9948 case M_BGEL_I:
9949 if (mask == M_BGEL_I)
9950 likely = 1;
b0e6f033 9951 if (imm_expr.X_add_number == 0)
252b5132 9952 {
df58fc94 9953 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 9954 &offset_expr, op[0]);
8fc2e39e 9955 break;
252b5132 9956 }
b0e6f033 9957 if (imm_expr.X_add_number == 1)
252b5132 9958 {
df58fc94 9959 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 9960 &offset_expr, op[0]);
8fc2e39e 9961 break;
252b5132 9962 }
b0e6f033 9963 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
9964 {
9965 do_true:
9966 /* result is always true */
1661c76c 9967 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 9968 macro_build (&offset_expr, "b", "p");
8fc2e39e 9969 break;
252b5132 9970 }
8fc2e39e 9971 used_at = 1;
c0ebe874 9972 set_at (op[0], 0);
df58fc94
RS
9973 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9974 &offset_expr, AT, ZERO);
252b5132
RH
9975 break;
9976
9977 case M_BGEUL:
9978 likely = 1;
9979 case M_BGEU:
c0ebe874 9980 if (op[1] == 0)
252b5132 9981 goto do_true;
c0ebe874 9982 else if (op[0] == 0)
df58fc94 9983 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 9984 &offset_expr, ZERO, op[1]);
df58fc94 9985 else
252b5132 9986 {
df58fc94 9987 used_at = 1;
c0ebe874 9988 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
9989 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
9990 &offset_expr, AT, ZERO);
252b5132 9991 }
252b5132
RH
9992 break;
9993
9994 case M_BGTUL_I:
9995 likely = 1;
9996 case M_BGTU_I:
c0ebe874 9997 if (op[0] == 0
bad1aba3 9998 || (GPR_SIZE == 32
f01dc953 9999 && imm_expr.X_add_number == -1))
252b5132 10000 goto do_false;
f9419b05 10001 ++imm_expr.X_add_number;
252b5132
RH
10002 /* FALLTHROUGH */
10003 case M_BGEU_I:
10004 case M_BGEUL_I:
10005 if (mask == M_BGEUL_I)
10006 likely = 1;
b0e6f033 10007 if (imm_expr.X_add_number == 0)
252b5132 10008 goto do_true;
b0e6f033 10009 else if (imm_expr.X_add_number == 1)
df58fc94 10010 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10011 &offset_expr, op[0], ZERO);
df58fc94 10012 else
252b5132 10013 {
df58fc94 10014 used_at = 1;
c0ebe874 10015 set_at (op[0], 1);
df58fc94
RS
10016 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10017 &offset_expr, AT, ZERO);
252b5132 10018 }
252b5132
RH
10019 break;
10020
10021 case M_BGTL:
10022 likely = 1;
10023 case M_BGT:
c0ebe874
RS
10024 if (op[1] == 0)
10025 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10026 else if (op[0] == 0)
10027 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10028 else
252b5132 10029 {
df58fc94 10030 used_at = 1;
c0ebe874 10031 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10032 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10033 &offset_expr, AT, ZERO);
252b5132 10034 }
252b5132
RH
10035 break;
10036
10037 case M_BGTUL:
10038 likely = 1;
10039 case M_BGTU:
c0ebe874 10040 if (op[1] == 0)
df58fc94 10041 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10042 &offset_expr, op[0], ZERO);
10043 else if (op[0] == 0)
df58fc94
RS
10044 goto do_false;
10045 else
252b5132 10046 {
df58fc94 10047 used_at = 1;
c0ebe874 10048 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10049 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10050 &offset_expr, AT, ZERO);
252b5132 10051 }
252b5132
RH
10052 break;
10053
10054 case M_BLEL:
10055 likely = 1;
10056 case M_BLE:
c0ebe874
RS
10057 if (op[1] == 0)
10058 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10059 else if (op[0] == 0)
10060 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10061 else
252b5132 10062 {
df58fc94 10063 used_at = 1;
c0ebe874 10064 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10065 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10066 &offset_expr, AT, ZERO);
252b5132 10067 }
252b5132
RH
10068 break;
10069
10070 case M_BLEL_I:
10071 likely = 1;
10072 case M_BLE_I:
b0e6f033 10073 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10074 goto do_true;
f9419b05 10075 ++imm_expr.X_add_number;
252b5132
RH
10076 /* FALLTHROUGH */
10077 case M_BLT_I:
10078 case M_BLTL_I:
10079 if (mask == M_BLTL_I)
10080 likely = 1;
b0e6f033 10081 if (imm_expr.X_add_number == 0)
c0ebe874 10082 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10083 else if (imm_expr.X_add_number == 1)
c0ebe874 10084 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10085 else
252b5132 10086 {
df58fc94 10087 used_at = 1;
c0ebe874 10088 set_at (op[0], 0);
df58fc94
RS
10089 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10090 &offset_expr, AT, ZERO);
252b5132 10091 }
252b5132
RH
10092 break;
10093
10094 case M_BLEUL:
10095 likely = 1;
10096 case M_BLEU:
c0ebe874 10097 if (op[1] == 0)
df58fc94 10098 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10099 &offset_expr, op[0], ZERO);
10100 else if (op[0] == 0)
df58fc94
RS
10101 goto do_true;
10102 else
252b5132 10103 {
df58fc94 10104 used_at = 1;
c0ebe874 10105 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10106 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10107 &offset_expr, AT, ZERO);
252b5132 10108 }
252b5132
RH
10109 break;
10110
10111 case M_BLEUL_I:
10112 likely = 1;
10113 case M_BLEU_I:
c0ebe874 10114 if (op[0] == 0
bad1aba3 10115 || (GPR_SIZE == 32
f01dc953 10116 && imm_expr.X_add_number == -1))
252b5132 10117 goto do_true;
f9419b05 10118 ++imm_expr.X_add_number;
252b5132
RH
10119 /* FALLTHROUGH */
10120 case M_BLTU_I:
10121 case M_BLTUL_I:
10122 if (mask == M_BLTUL_I)
10123 likely = 1;
b0e6f033 10124 if (imm_expr.X_add_number == 0)
252b5132 10125 goto do_false;
b0e6f033 10126 else if (imm_expr.X_add_number == 1)
df58fc94 10127 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10128 &offset_expr, op[0], ZERO);
df58fc94 10129 else
252b5132 10130 {
df58fc94 10131 used_at = 1;
c0ebe874 10132 set_at (op[0], 1);
df58fc94
RS
10133 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10134 &offset_expr, AT, ZERO);
252b5132 10135 }
252b5132
RH
10136 break;
10137
10138 case M_BLTL:
10139 likely = 1;
10140 case M_BLT:
c0ebe874
RS
10141 if (op[1] == 0)
10142 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10143 else if (op[0] == 0)
10144 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10145 else
252b5132 10146 {
df58fc94 10147 used_at = 1;
c0ebe874 10148 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10149 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10150 &offset_expr, AT, ZERO);
252b5132 10151 }
252b5132
RH
10152 break;
10153
10154 case M_BLTUL:
10155 likely = 1;
10156 case M_BLTU:
c0ebe874 10157 if (op[1] == 0)
252b5132 10158 goto do_false;
c0ebe874 10159 else if (op[0] == 0)
df58fc94 10160 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10161 &offset_expr, ZERO, op[1]);
df58fc94 10162 else
252b5132 10163 {
df58fc94 10164 used_at = 1;
c0ebe874 10165 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10166 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10167 &offset_expr, AT, ZERO);
252b5132 10168 }
252b5132
RH
10169 break;
10170
10171 case M_DDIV_3:
10172 dbl = 1;
10173 case M_DIV_3:
10174 s = "mflo";
10175 goto do_div3;
10176 case M_DREM_3:
10177 dbl = 1;
10178 case M_REM_3:
10179 s = "mfhi";
10180 do_div3:
c0ebe874 10181 if (op[2] == 0)
252b5132 10182 {
1661c76c 10183 as_warn (_("divide by zero"));
252b5132 10184 if (mips_trap)
df58fc94 10185 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10186 else
df58fc94 10187 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10188 break;
252b5132
RH
10189 }
10190
7d10b47d 10191 start_noreorder ();
252b5132
RH
10192 if (mips_trap)
10193 {
c0ebe874
RS
10194 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10195 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10196 }
10197 else
10198 {
df58fc94
RS
10199 if (mips_opts.micromips)
10200 micromips_label_expr (&label_expr);
10201 else
10202 label_expr.X_add_number = 8;
c0ebe874
RS
10203 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10204 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10205 macro_build (NULL, "break", BRK_FMT, 7);
10206 if (mips_opts.micromips)
10207 micromips_add_label ();
252b5132
RH
10208 }
10209 expr1.X_add_number = -1;
8fc2e39e 10210 used_at = 1;
f6a22291 10211 load_register (AT, &expr1, dbl);
df58fc94
RS
10212 if (mips_opts.micromips)
10213 micromips_label_expr (&label_expr);
10214 else
10215 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10216 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10217 if (dbl)
10218 {
10219 expr1.X_add_number = 1;
f6a22291 10220 load_register (AT, &expr1, dbl);
df58fc94 10221 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10222 }
10223 else
10224 {
10225 expr1.X_add_number = 0x80000000;
df58fc94 10226 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10227 }
10228 if (mips_trap)
10229 {
c0ebe874 10230 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10231 /* We want to close the noreorder block as soon as possible, so
10232 that later insns are available for delay slot filling. */
7d10b47d 10233 end_noreorder ();
252b5132
RH
10234 }
10235 else
10236 {
df58fc94
RS
10237 if (mips_opts.micromips)
10238 micromips_label_expr (&label_expr);
10239 else
10240 label_expr.X_add_number = 8;
c0ebe874 10241 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10242 macro_build (NULL, "nop", "");
252b5132
RH
10243
10244 /* We want to close the noreorder block as soon as possible, so
10245 that later insns are available for delay slot filling. */
7d10b47d 10246 end_noreorder ();
252b5132 10247
df58fc94 10248 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10249 }
df58fc94
RS
10250 if (mips_opts.micromips)
10251 micromips_add_label ();
c0ebe874 10252 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10253 break;
10254
10255 case M_DIV_3I:
10256 s = "div";
10257 s2 = "mflo";
10258 goto do_divi;
10259 case M_DIVU_3I:
10260 s = "divu";
10261 s2 = "mflo";
10262 goto do_divi;
10263 case M_REM_3I:
10264 s = "div";
10265 s2 = "mfhi";
10266 goto do_divi;
10267 case M_REMU_3I:
10268 s = "divu";
10269 s2 = "mfhi";
10270 goto do_divi;
10271 case M_DDIV_3I:
10272 dbl = 1;
10273 s = "ddiv";
10274 s2 = "mflo";
10275 goto do_divi;
10276 case M_DDIVU_3I:
10277 dbl = 1;
10278 s = "ddivu";
10279 s2 = "mflo";
10280 goto do_divi;
10281 case M_DREM_3I:
10282 dbl = 1;
10283 s = "ddiv";
10284 s2 = "mfhi";
10285 goto do_divi;
10286 case M_DREMU_3I:
10287 dbl = 1;
10288 s = "ddivu";
10289 s2 = "mfhi";
10290 do_divi:
b0e6f033 10291 if (imm_expr.X_add_number == 0)
252b5132 10292 {
1661c76c 10293 as_warn (_("divide by zero"));
252b5132 10294 if (mips_trap)
df58fc94 10295 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10296 else
df58fc94 10297 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10298 break;
252b5132 10299 }
b0e6f033 10300 if (imm_expr.X_add_number == 1)
252b5132
RH
10301 {
10302 if (strcmp (s2, "mflo") == 0)
c0ebe874 10303 move_register (op[0], op[1]);
252b5132 10304 else
c0ebe874 10305 move_register (op[0], ZERO);
8fc2e39e 10306 break;
252b5132 10307 }
b0e6f033 10308 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10309 {
10310 if (strcmp (s2, "mflo") == 0)
c0ebe874 10311 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10312 else
c0ebe874 10313 move_register (op[0], ZERO);
8fc2e39e 10314 break;
252b5132
RH
10315 }
10316
8fc2e39e 10317 used_at = 1;
67c0d1eb 10318 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10319 macro_build (NULL, s, "z,s,t", op[1], AT);
10320 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10321 break;
10322
10323 case M_DIVU_3:
10324 s = "divu";
10325 s2 = "mflo";
10326 goto do_divu3;
10327 case M_REMU_3:
10328 s = "divu";
10329 s2 = "mfhi";
10330 goto do_divu3;
10331 case M_DDIVU_3:
10332 s = "ddivu";
10333 s2 = "mflo";
10334 goto do_divu3;
10335 case M_DREMU_3:
10336 s = "ddivu";
10337 s2 = "mfhi";
10338 do_divu3:
7d10b47d 10339 start_noreorder ();
252b5132
RH
10340 if (mips_trap)
10341 {
c0ebe874
RS
10342 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10343 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10344 /* We want to close the noreorder block as soon as possible, so
10345 that later insns are available for delay slot filling. */
7d10b47d 10346 end_noreorder ();
252b5132
RH
10347 }
10348 else
10349 {
df58fc94
RS
10350 if (mips_opts.micromips)
10351 micromips_label_expr (&label_expr);
10352 else
10353 label_expr.X_add_number = 8;
c0ebe874
RS
10354 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10355 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10356
10357 /* We want to close the noreorder block as soon as possible, so
10358 that later insns are available for delay slot filling. */
7d10b47d 10359 end_noreorder ();
df58fc94
RS
10360 macro_build (NULL, "break", BRK_FMT, 7);
10361 if (mips_opts.micromips)
10362 micromips_add_label ();
252b5132 10363 }
c0ebe874 10364 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10365 break;
252b5132 10366
1abe91b1
MR
10367 case M_DLCA_AB:
10368 dbl = 1;
10369 case M_LCA_AB:
10370 call = 1;
10371 goto do_la;
252b5132
RH
10372 case M_DLA_AB:
10373 dbl = 1;
10374 case M_LA_AB:
1abe91b1 10375 do_la:
252b5132
RH
10376 /* Load the address of a symbol into a register. If breg is not
10377 zero, we then add a base register to it. */
10378
c0ebe874 10379 breg = op[2];
bad1aba3 10380 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10381 as_warn (_("dla used to load 32-bit register; recommend using la "
10382 "instead"));
3bec30a8 10383
90ecf173 10384 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10385 as_warn (_("la used to load 64-bit address; recommend using dla "
10386 "instead"));
3bec30a8 10387
f2ae14a1 10388 if (small_offset_p (0, align, 16))
0c11417f 10389 {
c0ebe874 10390 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10391 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10392 break;
0c11417f
MR
10393 }
10394
c0ebe874 10395 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10396 {
10397 tempreg = AT;
10398 used_at = 1;
10399 }
10400 else
c0ebe874 10401 tempreg = op[0];
afdbd6d0 10402
252b5132
RH
10403 if (offset_expr.X_op != O_symbol
10404 && offset_expr.X_op != O_constant)
10405 {
1661c76c 10406 as_bad (_("expression too complex"));
252b5132
RH
10407 offset_expr.X_op = O_constant;
10408 }
10409
252b5132 10410 if (offset_expr.X_op == O_constant)
aed1a261 10411 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10412 else if (mips_pic == NO_PIC)
10413 {
d6bc6245 10414 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10415 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10416 Otherwise we want
10417 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10418 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10419 If we have a constant, we need two instructions anyhow,
d6bc6245 10420 so we may as well always use the latter form.
76b3015f 10421
6caf9ef4
TS
10422 With 64bit address space and a usable $at we want
10423 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10424 lui $at,<sym> (BFD_RELOC_HI16_S)
10425 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10426 daddiu $at,<sym> (BFD_RELOC_LO16)
10427 dsll32 $tempreg,0
10428 daddu $tempreg,$tempreg,$at
10429
10430 If $at is already in use, we use a path which is suboptimal
10431 on superscalar processors.
10432 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10433 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10434 dsll $tempreg,16
10435 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10436 dsll $tempreg,16
10437 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10438
10439 For GP relative symbols in 64bit address space we can use
10440 the same sequence as in 32bit address space. */
aed1a261 10441 if (HAVE_64BIT_SYMBOLS)
252b5132 10442 {
6caf9ef4
TS
10443 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10444 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10445 {
10446 relax_start (offset_expr.X_add_symbol);
10447 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10448 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10449 relax_switch ();
10450 }
d6bc6245 10451
741fe287 10452 if (used_at == 0 && mips_opts.at)
98d3f06f 10453 {
df58fc94 10454 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10455 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10456 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10457 AT, BFD_RELOC_HI16_S);
67c0d1eb 10458 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10459 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10460 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10461 AT, AT, BFD_RELOC_LO16);
df58fc94 10462 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10463 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10464 used_at = 1;
10465 }
10466 else
10467 {
df58fc94 10468 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10469 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10470 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10471 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10472 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10473 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10474 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10475 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10476 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10477 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10478 }
6caf9ef4
TS
10479
10480 if (mips_relax.sequence)
10481 relax_end ();
98d3f06f
KH
10482 }
10483 else
10484 {
10485 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10486 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10487 {
4d7206a2 10488 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10489 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10490 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10491 relax_switch ();
98d3f06f 10492 }
6943caf0 10493 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10494 as_bad (_("offset too large"));
67c0d1eb
RS
10495 macro_build_lui (&offset_expr, tempreg);
10496 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10497 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10498 if (mips_relax.sequence)
10499 relax_end ();
98d3f06f 10500 }
252b5132 10501 }
0a44bf69 10502 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10503 {
9117d219
NC
10504 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10505
252b5132
RH
10506 /* If this is a reference to an external symbol, and there
10507 is no constant, we want
10508 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10509 or for lca or if tempreg is PIC_CALL_REG
9117d219 10510 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10511 For a local symbol, we want
10512 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10513 nop
10514 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10515
10516 If we have a small constant, and this is a reference to
10517 an external symbol, we want
10518 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10519 nop
10520 addiu $tempreg,$tempreg,<constant>
10521 For a local symbol, we want the same instruction
10522 sequence, but we output a BFD_RELOC_LO16 reloc on the
10523 addiu instruction.
10524
10525 If we have a large constant, and this is a reference to
10526 an external symbol, we want
10527 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10528 lui $at,<hiconstant>
10529 addiu $at,$at,<loconstant>
10530 addu $tempreg,$tempreg,$at
10531 For a local symbol, we want the same instruction
10532 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10533 addiu instruction.
ed6fb7bd
SC
10534 */
10535
4d7206a2 10536 if (offset_expr.X_add_number == 0)
252b5132 10537 {
0a44bf69
RS
10538 if (mips_pic == SVR4_PIC
10539 && breg == 0
10540 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10541 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10542
10543 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10545 lw_reloc_type, mips_gp_register);
4d7206a2 10546 if (breg != 0)
252b5132
RH
10547 {
10548 /* We're going to put in an addu instruction using
10549 tempreg, so we may as well insert the nop right
10550 now. */
269137b2 10551 load_delay_nop ();
252b5132 10552 }
4d7206a2 10553 relax_switch ();
67c0d1eb
RS
10554 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10555 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10556 load_delay_nop ();
67c0d1eb
RS
10557 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10558 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10559 relax_end ();
252b5132
RH
10560 /* FIXME: If breg == 0, and the next instruction uses
10561 $tempreg, then if this variant case is used an extra
10562 nop will be generated. */
10563 }
4d7206a2
RS
10564 else if (offset_expr.X_add_number >= -0x8000
10565 && offset_expr.X_add_number < 0x8000)
252b5132 10566 {
67c0d1eb 10567 load_got_offset (tempreg, &offset_expr);
269137b2 10568 load_delay_nop ();
67c0d1eb 10569 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10570 }
10571 else
10572 {
4d7206a2
RS
10573 expr1.X_add_number = offset_expr.X_add_number;
10574 offset_expr.X_add_number =
43c0598f 10575 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10576 load_got_offset (tempreg, &offset_expr);
f6a22291 10577 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10578 /* If we are going to add in a base register, and the
10579 target register and the base register are the same,
10580 then we are using AT as a temporary register. Since
10581 we want to load the constant into AT, we add our
10582 current AT (from the global offset table) and the
10583 register into the register now, and pretend we were
10584 not using a base register. */
c0ebe874 10585 if (breg == op[0])
252b5132 10586 {
269137b2 10587 load_delay_nop ();
67c0d1eb 10588 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10589 op[0], AT, breg);
252b5132 10590 breg = 0;
c0ebe874 10591 tempreg = op[0];
252b5132 10592 }
f6a22291 10593 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10594 used_at = 1;
10595 }
10596 }
0a44bf69 10597 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10598 {
67c0d1eb 10599 int add_breg_early = 0;
f5040a92
AO
10600
10601 /* If this is a reference to an external, and there is no
10602 constant, or local symbol (*), with or without a
10603 constant, we want
10604 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10605 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10606 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10607
10608 If we have a small constant, and this is a reference to
10609 an external symbol, we want
10610 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10611 addiu $tempreg,$tempreg,<constant>
10612
10613 If we have a large constant, and this is a reference to
10614 an external symbol, we want
10615 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10616 lui $at,<hiconstant>
10617 addiu $at,$at,<loconstant>
10618 addu $tempreg,$tempreg,$at
10619
10620 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10621 local symbols, even though it introduces an additional
10622 instruction. */
10623
f5040a92
AO
10624 if (offset_expr.X_add_number)
10625 {
4d7206a2 10626 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10627 offset_expr.X_add_number = 0;
10628
4d7206a2 10629 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10630 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10631 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10632
10633 if (expr1.X_add_number >= -0x8000
10634 && expr1.X_add_number < 0x8000)
10635 {
67c0d1eb
RS
10636 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
10637 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10638 }
ecd13cd3 10639 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10640 {
c0ebe874
RS
10641 unsigned int dreg;
10642
f5040a92
AO
10643 /* If we are going to add in a base register, and the
10644 target register and the base register are the same,
10645 then we are using AT as a temporary register. Since
10646 we want to load the constant into AT, we add our
10647 current AT (from the global offset table) and the
10648 register into the register now, and pretend we were
10649 not using a base register. */
c0ebe874 10650 if (breg != op[0])
f5040a92
AO
10651 dreg = tempreg;
10652 else
10653 {
9c2799c2 10654 gas_assert (tempreg == AT);
67c0d1eb 10655 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10656 op[0], AT, breg);
10657 dreg = op[0];
67c0d1eb 10658 add_breg_early = 1;
f5040a92
AO
10659 }
10660
f6a22291 10661 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10662 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10663 dreg, dreg, AT);
f5040a92 10664
f5040a92
AO
10665 used_at = 1;
10666 }
10667 else
10668 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10669
4d7206a2 10670 relax_switch ();
f5040a92
AO
10671 offset_expr.X_add_number = expr1.X_add_number;
10672
67c0d1eb
RS
10673 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10674 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
10675 if (add_breg_early)
f5040a92 10676 {
67c0d1eb 10677 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10678 op[0], tempreg, breg);
f5040a92 10679 breg = 0;
c0ebe874 10680 tempreg = op[0];
f5040a92 10681 }
4d7206a2 10682 relax_end ();
f5040a92 10683 }
4d7206a2 10684 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 10685 {
4d7206a2 10686 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10687 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10688 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 10689 relax_switch ();
67c0d1eb
RS
10690 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10691 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 10692 relax_end ();
f5040a92 10693 }
4d7206a2 10694 else
f5040a92 10695 {
67c0d1eb
RS
10696 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10697 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
10698 }
10699 }
0a44bf69 10700 else if (mips_big_got && !HAVE_NEWABI)
252b5132 10701 {
67c0d1eb 10702 int gpdelay;
9117d219
NC
10703 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10704 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 10705 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
10706
10707 /* This is the large GOT case. If this is a reference to an
10708 external symbol, and there is no constant, we want
10709 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10710 addu $tempreg,$tempreg,$gp
10711 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10712 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
10713 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10714 addu $tempreg,$tempreg,$gp
10715 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
10716 For a local symbol, we want
10717 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10718 nop
10719 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10720
10721 If we have a small constant, and this is a reference to
10722 an external symbol, we want
10723 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10724 addu $tempreg,$tempreg,$gp
10725 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10726 nop
10727 addiu $tempreg,$tempreg,<constant>
10728 For a local symbol, we want
10729 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10730 nop
10731 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
10732
10733 If we have a large constant, and this is a reference to
10734 an external symbol, we want
10735 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10736 addu $tempreg,$tempreg,$gp
10737 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10738 lui $at,<hiconstant>
10739 addiu $at,$at,<loconstant>
10740 addu $tempreg,$tempreg,$at
10741 For a local symbol, we want
10742 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10743 lui $at,<hiconstant>
10744 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
10745 addu $tempreg,$tempreg,$at
f5040a92 10746 */
438c16b8 10747
252b5132
RH
10748 expr1.X_add_number = offset_expr.X_add_number;
10749 offset_expr.X_add_number = 0;
4d7206a2 10750 relax_start (offset_expr.X_add_symbol);
67c0d1eb 10751 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
10752 if (expr1.X_add_number == 0 && breg == 0
10753 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
10754 {
10755 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10756 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10757 }
df58fc94 10758 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10759 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10760 tempreg, tempreg, mips_gp_register);
67c0d1eb 10761 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 10762 tempreg, lw_reloc_type, tempreg);
252b5132
RH
10763 if (expr1.X_add_number == 0)
10764 {
67c0d1eb 10765 if (breg != 0)
252b5132
RH
10766 {
10767 /* We're going to put in an addu instruction using
10768 tempreg, so we may as well insert the nop right
10769 now. */
269137b2 10770 load_delay_nop ();
252b5132 10771 }
252b5132
RH
10772 }
10773 else if (expr1.X_add_number >= -0x8000
10774 && expr1.X_add_number < 0x8000)
10775 {
269137b2 10776 load_delay_nop ();
67c0d1eb 10777 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10778 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
10779 }
10780 else
10781 {
c0ebe874
RS
10782 unsigned int dreg;
10783
252b5132
RH
10784 /* If we are going to add in a base register, and the
10785 target register and the base register are the same,
10786 then we are using AT as a temporary register. Since
10787 we want to load the constant into AT, we add our
10788 current AT (from the global offset table) and the
10789 register into the register now, and pretend we were
10790 not using a base register. */
c0ebe874 10791 if (breg != op[0])
67c0d1eb 10792 dreg = tempreg;
252b5132
RH
10793 else
10794 {
9c2799c2 10795 gas_assert (tempreg == AT);
269137b2 10796 load_delay_nop ();
67c0d1eb 10797 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10798 op[0], AT, breg);
10799 dreg = op[0];
252b5132
RH
10800 }
10801
f6a22291 10802 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10803 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 10804
252b5132
RH
10805 used_at = 1;
10806 }
43c0598f 10807 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 10808 relax_switch ();
252b5132 10809
67c0d1eb 10810 if (gpdelay)
252b5132
RH
10811 {
10812 /* This is needed because this instruction uses $gp, but
f5040a92 10813 the first instruction on the main stream does not. */
67c0d1eb 10814 macro_build (NULL, "nop", "");
252b5132 10815 }
ed6fb7bd 10816
67c0d1eb
RS
10817 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10818 local_reloc_type, mips_gp_register);
f5040a92 10819 if (expr1.X_add_number >= -0x8000
252b5132
RH
10820 && expr1.X_add_number < 0x8000)
10821 {
269137b2 10822 load_delay_nop ();
67c0d1eb
RS
10823 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10824 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 10825 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
10826 register, the external symbol case ended with a load,
10827 so if the symbol turns out to not be external, and
10828 the next instruction uses tempreg, an unnecessary nop
10829 will be inserted. */
252b5132
RH
10830 }
10831 else
10832 {
c0ebe874 10833 if (breg == op[0])
252b5132
RH
10834 {
10835 /* We must add in the base register now, as in the
f5040a92 10836 external symbol case. */
9c2799c2 10837 gas_assert (tempreg == AT);
269137b2 10838 load_delay_nop ();
67c0d1eb 10839 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10840 op[0], AT, breg);
10841 tempreg = op[0];
252b5132 10842 /* We set breg to 0 because we have arranged to add
f5040a92 10843 it in in both cases. */
252b5132
RH
10844 breg = 0;
10845 }
10846
67c0d1eb
RS
10847 macro_build_lui (&expr1, AT);
10848 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10849 AT, AT, BFD_RELOC_LO16);
67c0d1eb 10850 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10851 tempreg, tempreg, AT);
8fc2e39e 10852 used_at = 1;
252b5132 10853 }
4d7206a2 10854 relax_end ();
252b5132 10855 }
0a44bf69 10856 else if (mips_big_got && HAVE_NEWABI)
f5040a92 10857 {
f5040a92
AO
10858 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
10859 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 10860 int add_breg_early = 0;
f5040a92
AO
10861
10862 /* This is the large GOT case. If this is a reference to an
10863 external symbol, and there is no constant, we want
10864 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10865 add $tempreg,$tempreg,$gp
10866 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 10867 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10868 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
10869 add $tempreg,$tempreg,$gp
10870 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
10871
10872 If we have a small constant, and this is a reference to
10873 an external symbol, we want
10874 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10875 add $tempreg,$tempreg,$gp
10876 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10877 addi $tempreg,$tempreg,<constant>
10878
10879 If we have a large constant, and this is a reference to
10880 an external symbol, we want
10881 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
10882 addu $tempreg,$tempreg,$gp
10883 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
10884 lui $at,<hiconstant>
10885 addi $at,$at,<loconstant>
10886 add $tempreg,$tempreg,$at
10887
10888 If we have NewABI, and we know it's a local symbol, we want
10889 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
10890 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
10891 otherwise we have to resort to GOT_HI16/GOT_LO16. */
10892
4d7206a2 10893 relax_start (offset_expr.X_add_symbol);
f5040a92 10894
4d7206a2 10895 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
10896 offset_expr.X_add_number = 0;
10897
1abe91b1
MR
10898 if (expr1.X_add_number == 0 && breg == 0
10899 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
10900 {
10901 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
10902 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
10903 }
df58fc94 10904 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 10905 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 10906 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
10907 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10908 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
10909
10910 if (expr1.X_add_number == 0)
4d7206a2 10911 ;
f5040a92
AO
10912 else if (expr1.X_add_number >= -0x8000
10913 && expr1.X_add_number < 0x8000)
10914 {
67c0d1eb 10915 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 10916 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 10917 }
ecd13cd3 10918 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 10919 {
c0ebe874
RS
10920 unsigned int dreg;
10921
f5040a92
AO
10922 /* If we are going to add in a base register, and the
10923 target register and the base register are the same,
10924 then we are using AT as a temporary register. Since
10925 we want to load the constant into AT, we add our
10926 current AT (from the global offset table) and the
10927 register into the register now, and pretend we were
10928 not using a base register. */
c0ebe874 10929 if (breg != op[0])
f5040a92
AO
10930 dreg = tempreg;
10931 else
10932 {
9c2799c2 10933 gas_assert (tempreg == AT);
67c0d1eb 10934 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
10935 op[0], AT, breg);
10936 dreg = op[0];
67c0d1eb 10937 add_breg_early = 1;
f5040a92
AO
10938 }
10939
f6a22291 10940 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 10941 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 10942
f5040a92
AO
10943 used_at = 1;
10944 }
10945 else
10946 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
10947
4d7206a2 10948 relax_switch ();
f5040a92 10949 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
10950 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10951 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
10952 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
10953 tempreg, BFD_RELOC_MIPS_GOT_OFST);
10954 if (add_breg_early)
f5040a92 10955 {
67c0d1eb 10956 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10957 op[0], tempreg, breg);
f5040a92 10958 breg = 0;
c0ebe874 10959 tempreg = op[0];
f5040a92 10960 }
4d7206a2 10961 relax_end ();
f5040a92 10962 }
252b5132
RH
10963 else
10964 abort ();
10965
10966 if (breg != 0)
c0ebe874 10967 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
10968 break;
10969
52b6b6b9 10970 case M_MSGSND:
df58fc94 10971 gas_assert (!mips_opts.micromips);
c0ebe874 10972 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 10973 break;
52b6b6b9
JM
10974
10975 case M_MSGLD:
df58fc94 10976 gas_assert (!mips_opts.micromips);
c8276761 10977 macro_build (NULL, "c2", "C", 0x02);
c7af4273 10978 break;
52b6b6b9
JM
10979
10980 case M_MSGLD_T:
df58fc94 10981 gas_assert (!mips_opts.micromips);
c0ebe874 10982 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 10983 break;
52b6b6b9
JM
10984
10985 case M_MSGWAIT:
df58fc94 10986 gas_assert (!mips_opts.micromips);
52b6b6b9 10987 macro_build (NULL, "c2", "C", 3);
c7af4273 10988 break;
52b6b6b9
JM
10989
10990 case M_MSGWAIT_T:
df58fc94 10991 gas_assert (!mips_opts.micromips);
c0ebe874 10992 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 10993 break;
52b6b6b9 10994
252b5132
RH
10995 case M_J_A:
10996 /* The j instruction may not be used in PIC code, since it
10997 requires an absolute address. We convert it to a b
10998 instruction. */
10999 if (mips_pic == NO_PIC)
67c0d1eb 11000 macro_build (&offset_expr, "j", "a");
252b5132 11001 else
67c0d1eb 11002 macro_build (&offset_expr, "b", "p");
8fc2e39e 11003 break;
252b5132
RH
11004
11005 /* The jal instructions must be handled as macros because when
11006 generating PIC code they expand to multi-instruction
11007 sequences. Normally they are simple instructions. */
df58fc94 11008 case M_JALS_1:
c0ebe874
RS
11009 op[1] = op[0];
11010 op[0] = RA;
df58fc94
RS
11011 /* Fall through. */
11012 case M_JALS_2:
11013 gas_assert (mips_opts.micromips);
833794fc
MR
11014 if (mips_opts.insn32)
11015 {
1661c76c 11016 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11017 break;
11018 }
df58fc94
RS
11019 jals = 1;
11020 goto jal;
252b5132 11021 case M_JAL_1:
c0ebe874
RS
11022 op[1] = op[0];
11023 op[0] = RA;
252b5132
RH
11024 /* Fall through. */
11025 case M_JAL_2:
df58fc94 11026 jal:
3e722fb5 11027 if (mips_pic == NO_PIC)
df58fc94
RS
11028 {
11029 s = jals ? "jalrs" : "jalr";
e64af278 11030 if (mips_opts.micromips
833794fc 11031 && !mips_opts.insn32
c0ebe874 11032 && op[0] == RA
e64af278 11033 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11034 macro_build (NULL, s, "mj", op[1]);
df58fc94 11035 else
c0ebe874 11036 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11037 }
0a44bf69 11038 else
252b5132 11039 {
df58fc94
RS
11040 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11041 && mips_cprestore_offset >= 0);
11042
c0ebe874 11043 if (op[1] != PIC_CALL_REG)
252b5132 11044 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11045
833794fc
MR
11046 s = ((mips_opts.micromips
11047 && !mips_opts.insn32
11048 && (!mips_opts.noreorder || cprestore))
df58fc94 11049 ? "jalrs" : "jalr");
e64af278 11050 if (mips_opts.micromips
833794fc 11051 && !mips_opts.insn32
c0ebe874 11052 && op[0] == RA
e64af278 11053 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11054 macro_build (NULL, s, "mj", op[1]);
df58fc94 11055 else
c0ebe874 11056 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11057 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11058 {
6478892d 11059 if (mips_cprestore_offset < 0)
1661c76c 11060 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11061 else
11062 {
90ecf173 11063 if (!mips_frame_reg_valid)
7a621144 11064 {
1661c76c 11065 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11066 /* Quiet this warning. */
11067 mips_frame_reg_valid = 1;
11068 }
90ecf173 11069 if (!mips_cprestore_valid)
7a621144 11070 {
1661c76c 11071 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11072 /* Quiet this warning. */
11073 mips_cprestore_valid = 1;
11074 }
d3fca0b5
MR
11075 if (mips_opts.noreorder)
11076 macro_build (NULL, "nop", "");
6478892d 11077 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11078 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11079 mips_gp_register,
256ab948
TS
11080 mips_frame_reg,
11081 HAVE_64BIT_ADDRESSES);
6478892d 11082 }
252b5132
RH
11083 }
11084 }
252b5132 11085
8fc2e39e 11086 break;
252b5132 11087
df58fc94
RS
11088 case M_JALS_A:
11089 gas_assert (mips_opts.micromips);
833794fc
MR
11090 if (mips_opts.insn32)
11091 {
1661c76c 11092 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11093 break;
11094 }
df58fc94
RS
11095 jals = 1;
11096 /* Fall through. */
252b5132
RH
11097 case M_JAL_A:
11098 if (mips_pic == NO_PIC)
df58fc94 11099 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11100 else if (mips_pic == SVR4_PIC)
11101 {
11102 /* If this is a reference to an external symbol, and we are
11103 using a small GOT, we want
11104 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11105 nop
f9419b05 11106 jalr $ra,$25
252b5132
RH
11107 nop
11108 lw $gp,cprestore($sp)
11109 The cprestore value is set using the .cprestore
11110 pseudo-op. If we are using a big GOT, we want
11111 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11112 addu $25,$25,$gp
11113 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11114 nop
f9419b05 11115 jalr $ra,$25
252b5132
RH
11116 nop
11117 lw $gp,cprestore($sp)
11118 If the symbol is not external, we want
11119 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11120 nop
11121 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11122 jalr $ra,$25
252b5132 11123 nop
438c16b8 11124 lw $gp,cprestore($sp)
f5040a92
AO
11125
11126 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11127 sequences above, minus nops, unless the symbol is local,
11128 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11129 GOT_DISP. */
438c16b8 11130 if (HAVE_NEWABI)
252b5132 11131 {
90ecf173 11132 if (!mips_big_got)
f5040a92 11133 {
4d7206a2 11134 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11135 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11136 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11137 mips_gp_register);
4d7206a2 11138 relax_switch ();
67c0d1eb
RS
11139 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11140 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11141 mips_gp_register);
11142 relax_end ();
f5040a92
AO
11143 }
11144 else
11145 {
4d7206a2 11146 relax_start (offset_expr.X_add_symbol);
df58fc94 11147 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11148 BFD_RELOC_MIPS_CALL_HI16);
11149 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11150 PIC_CALL_REG, mips_gp_register);
11151 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11152 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11153 PIC_CALL_REG);
4d7206a2 11154 relax_switch ();
67c0d1eb
RS
11155 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11156 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11157 mips_gp_register);
11158 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11159 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11160 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11161 relax_end ();
f5040a92 11162 }
684022ea 11163
df58fc94 11164 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11165 }
11166 else
11167 {
4d7206a2 11168 relax_start (offset_expr.X_add_symbol);
90ecf173 11169 if (!mips_big_got)
438c16b8 11170 {
67c0d1eb
RS
11171 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11172 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11173 mips_gp_register);
269137b2 11174 load_delay_nop ();
4d7206a2 11175 relax_switch ();
438c16b8 11176 }
252b5132 11177 else
252b5132 11178 {
67c0d1eb
RS
11179 int gpdelay;
11180
11181 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11182 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11183 BFD_RELOC_MIPS_CALL_HI16);
11184 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11185 PIC_CALL_REG, mips_gp_register);
11186 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11187 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11188 PIC_CALL_REG);
269137b2 11189 load_delay_nop ();
4d7206a2 11190 relax_switch ();
67c0d1eb
RS
11191 if (gpdelay)
11192 macro_build (NULL, "nop", "");
252b5132 11193 }
67c0d1eb
RS
11194 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11195 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11196 mips_gp_register);
269137b2 11197 load_delay_nop ();
67c0d1eb
RS
11198 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11199 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11200 relax_end ();
df58fc94 11201 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11202
6478892d 11203 if (mips_cprestore_offset < 0)
1661c76c 11204 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11205 else
11206 {
90ecf173 11207 if (!mips_frame_reg_valid)
7a621144 11208 {
1661c76c 11209 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11210 /* Quiet this warning. */
11211 mips_frame_reg_valid = 1;
11212 }
90ecf173 11213 if (!mips_cprestore_valid)
7a621144 11214 {
1661c76c 11215 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11216 /* Quiet this warning. */
11217 mips_cprestore_valid = 1;
11218 }
6478892d 11219 if (mips_opts.noreorder)
67c0d1eb 11220 macro_build (NULL, "nop", "");
6478892d 11221 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11222 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11223 mips_gp_register,
256ab948
TS
11224 mips_frame_reg,
11225 HAVE_64BIT_ADDRESSES);
6478892d 11226 }
252b5132
RH
11227 }
11228 }
0a44bf69 11229 else if (mips_pic == VXWORKS_PIC)
1661c76c 11230 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11231 else
11232 abort ();
11233
8fc2e39e 11234 break;
252b5132 11235
7f3c4072 11236 case M_LBUE_AB:
7f3c4072
CM
11237 s = "lbue";
11238 fmt = "t,+j(b)";
11239 offbits = 9;
11240 goto ld_st;
11241 case M_LHUE_AB:
7f3c4072
CM
11242 s = "lhue";
11243 fmt = "t,+j(b)";
11244 offbits = 9;
11245 goto ld_st;
11246 case M_LBE_AB:
7f3c4072
CM
11247 s = "lbe";
11248 fmt = "t,+j(b)";
11249 offbits = 9;
11250 goto ld_st;
11251 case M_LHE_AB:
7f3c4072
CM
11252 s = "lhe";
11253 fmt = "t,+j(b)";
11254 offbits = 9;
11255 goto ld_st;
11256 case M_LLE_AB:
7f3c4072
CM
11257 s = "lle";
11258 fmt = "t,+j(b)";
11259 offbits = 9;
11260 goto ld_st;
11261 case M_LWE_AB:
7f3c4072
CM
11262 s = "lwe";
11263 fmt = "t,+j(b)";
11264 offbits = 9;
11265 goto ld_st;
11266 case M_LWLE_AB:
7f3c4072
CM
11267 s = "lwle";
11268 fmt = "t,+j(b)";
11269 offbits = 9;
11270 goto ld_st;
11271 case M_LWRE_AB:
7f3c4072
CM
11272 s = "lwre";
11273 fmt = "t,+j(b)";
11274 offbits = 9;
11275 goto ld_st;
11276 case M_SBE_AB:
7f3c4072
CM
11277 s = "sbe";
11278 fmt = "t,+j(b)";
11279 offbits = 9;
11280 goto ld_st;
11281 case M_SCE_AB:
7f3c4072
CM
11282 s = "sce";
11283 fmt = "t,+j(b)";
11284 offbits = 9;
11285 goto ld_st;
11286 case M_SHE_AB:
7f3c4072
CM
11287 s = "she";
11288 fmt = "t,+j(b)";
11289 offbits = 9;
11290 goto ld_st;
11291 case M_SWE_AB:
7f3c4072
CM
11292 s = "swe";
11293 fmt = "t,+j(b)";
11294 offbits = 9;
11295 goto ld_st;
11296 case M_SWLE_AB:
7f3c4072
CM
11297 s = "swle";
11298 fmt = "t,+j(b)";
11299 offbits = 9;
11300 goto ld_st;
11301 case M_SWRE_AB:
7f3c4072
CM
11302 s = "swre";
11303 fmt = "t,+j(b)";
11304 offbits = 9;
11305 goto ld_st;
dec0624d 11306 case M_ACLR_AB:
dec0624d 11307 s = "aclr";
dec0624d 11308 fmt = "\\,~(b)";
7f3c4072 11309 offbits = 12;
dec0624d
MR
11310 goto ld_st;
11311 case M_ASET_AB:
dec0624d 11312 s = "aset";
dec0624d 11313 fmt = "\\,~(b)";
7f3c4072 11314 offbits = 12;
dec0624d 11315 goto ld_st;
252b5132
RH
11316 case M_LB_AB:
11317 s = "lb";
df58fc94 11318 fmt = "t,o(b)";
252b5132
RH
11319 goto ld;
11320 case M_LBU_AB:
11321 s = "lbu";
df58fc94 11322 fmt = "t,o(b)";
252b5132
RH
11323 goto ld;
11324 case M_LH_AB:
11325 s = "lh";
df58fc94 11326 fmt = "t,o(b)";
252b5132
RH
11327 goto ld;
11328 case M_LHU_AB:
11329 s = "lhu";
df58fc94 11330 fmt = "t,o(b)";
252b5132
RH
11331 goto ld;
11332 case M_LW_AB:
11333 s = "lw";
df58fc94 11334 fmt = "t,o(b)";
252b5132
RH
11335 goto ld;
11336 case M_LWC0_AB:
df58fc94 11337 gas_assert (!mips_opts.micromips);
252b5132 11338 s = "lwc0";
df58fc94 11339 fmt = "E,o(b)";
bdaaa2e1 11340 /* Itbl support may require additional care here. */
252b5132 11341 coproc = 1;
df58fc94 11342 goto ld_st;
252b5132
RH
11343 case M_LWC1_AB:
11344 s = "lwc1";
df58fc94 11345 fmt = "T,o(b)";
bdaaa2e1 11346 /* Itbl support may require additional care here. */
252b5132 11347 coproc = 1;
df58fc94 11348 goto ld_st;
252b5132
RH
11349 case M_LWC2_AB:
11350 s = "lwc2";
df58fc94 11351 fmt = COP12_FMT;
7361da2c
AB
11352 offbits = (mips_opts.micromips ? 12
11353 : ISA_IS_R6 (mips_opts.isa) ? 11
11354 : 16);
bdaaa2e1 11355 /* Itbl support may require additional care here. */
252b5132 11356 coproc = 1;
df58fc94 11357 goto ld_st;
252b5132 11358 case M_LWC3_AB:
df58fc94 11359 gas_assert (!mips_opts.micromips);
252b5132 11360 s = "lwc3";
df58fc94 11361 fmt = "E,o(b)";
bdaaa2e1 11362 /* Itbl support may require additional care here. */
252b5132 11363 coproc = 1;
df58fc94 11364 goto ld_st;
252b5132
RH
11365 case M_LWL_AB:
11366 s = "lwl";
df58fc94 11367 fmt = MEM12_FMT;
7f3c4072 11368 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11369 goto ld_st;
252b5132
RH
11370 case M_LWR_AB:
11371 s = "lwr";
df58fc94 11372 fmt = MEM12_FMT;
7f3c4072 11373 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11374 goto ld_st;
252b5132 11375 case M_LDC1_AB:
252b5132 11376 s = "ldc1";
df58fc94 11377 fmt = "T,o(b)";
bdaaa2e1 11378 /* Itbl support may require additional care here. */
252b5132 11379 coproc = 1;
df58fc94 11380 goto ld_st;
252b5132
RH
11381 case M_LDC2_AB:
11382 s = "ldc2";
df58fc94 11383 fmt = COP12_FMT;
7361da2c
AB
11384 offbits = (mips_opts.micromips ? 12
11385 : ISA_IS_R6 (mips_opts.isa) ? 11
11386 : 16);
bdaaa2e1 11387 /* Itbl support may require additional care here. */
252b5132 11388 coproc = 1;
df58fc94 11389 goto ld_st;
c77c0862 11390 case M_LQC2_AB:
c77c0862 11391 s = "lqc2";
14daeee3 11392 fmt = "+7,o(b)";
c77c0862
RS
11393 /* Itbl support may require additional care here. */
11394 coproc = 1;
11395 goto ld_st;
252b5132
RH
11396 case M_LDC3_AB:
11397 s = "ldc3";
df58fc94 11398 fmt = "E,o(b)";
bdaaa2e1 11399 /* Itbl support may require additional care here. */
252b5132 11400 coproc = 1;
df58fc94 11401 goto ld_st;
252b5132
RH
11402 case M_LDL_AB:
11403 s = "ldl";
df58fc94 11404 fmt = MEM12_FMT;
7f3c4072 11405 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11406 goto ld_st;
252b5132
RH
11407 case M_LDR_AB:
11408 s = "ldr";
df58fc94 11409 fmt = MEM12_FMT;
7f3c4072 11410 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11411 goto ld_st;
252b5132
RH
11412 case M_LL_AB:
11413 s = "ll";
7361da2c
AB
11414 fmt = LL_SC_FMT;
11415 offbits = (mips_opts.micromips ? 12
11416 : ISA_IS_R6 (mips_opts.isa) ? 9
11417 : 16);
252b5132
RH
11418 goto ld;
11419 case M_LLD_AB:
11420 s = "lld";
7361da2c
AB
11421 fmt = LL_SC_FMT;
11422 offbits = (mips_opts.micromips ? 12
11423 : ISA_IS_R6 (mips_opts.isa) ? 9
11424 : 16);
252b5132
RH
11425 goto ld;
11426 case M_LWU_AB:
11427 s = "lwu";
df58fc94 11428 fmt = MEM12_FMT;
7f3c4072 11429 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11430 goto ld;
11431 case M_LWP_AB:
df58fc94
RS
11432 gas_assert (mips_opts.micromips);
11433 s = "lwp";
11434 fmt = "t,~(b)";
7f3c4072 11435 offbits = 12;
df58fc94
RS
11436 lp = 1;
11437 goto ld;
11438 case M_LDP_AB:
df58fc94
RS
11439 gas_assert (mips_opts.micromips);
11440 s = "ldp";
11441 fmt = "t,~(b)";
7f3c4072 11442 offbits = 12;
df58fc94
RS
11443 lp = 1;
11444 goto ld;
11445 case M_LWM_AB:
df58fc94
RS
11446 gas_assert (mips_opts.micromips);
11447 s = "lwm";
11448 fmt = "n,~(b)";
7f3c4072 11449 offbits = 12;
df58fc94
RS
11450 goto ld_st;
11451 case M_LDM_AB:
df58fc94
RS
11452 gas_assert (mips_opts.micromips);
11453 s = "ldm";
11454 fmt = "n,~(b)";
7f3c4072 11455 offbits = 12;
df58fc94
RS
11456 goto ld_st;
11457
252b5132 11458 ld:
f19ccbda 11459 /* We don't want to use $0 as tempreg. */
c0ebe874 11460 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11461 goto ld_st;
252b5132 11462 else
c0ebe874 11463 tempreg = op[0] + lp;
df58fc94
RS
11464 goto ld_noat;
11465
252b5132
RH
11466 case M_SB_AB:
11467 s = "sb";
df58fc94
RS
11468 fmt = "t,o(b)";
11469 goto ld_st;
252b5132
RH
11470 case M_SH_AB:
11471 s = "sh";
df58fc94
RS
11472 fmt = "t,o(b)";
11473 goto ld_st;
252b5132
RH
11474 case M_SW_AB:
11475 s = "sw";
df58fc94
RS
11476 fmt = "t,o(b)";
11477 goto ld_st;
252b5132 11478 case M_SWC0_AB:
df58fc94 11479 gas_assert (!mips_opts.micromips);
252b5132 11480 s = "swc0";
df58fc94 11481 fmt = "E,o(b)";
bdaaa2e1 11482 /* Itbl support may require additional care here. */
252b5132 11483 coproc = 1;
df58fc94 11484 goto ld_st;
252b5132
RH
11485 case M_SWC1_AB:
11486 s = "swc1";
df58fc94 11487 fmt = "T,o(b)";
bdaaa2e1 11488 /* Itbl support may require additional care here. */
252b5132 11489 coproc = 1;
df58fc94 11490 goto ld_st;
252b5132
RH
11491 case M_SWC2_AB:
11492 s = "swc2";
df58fc94 11493 fmt = COP12_FMT;
7361da2c
AB
11494 offbits = (mips_opts.micromips ? 12
11495 : ISA_IS_R6 (mips_opts.isa) ? 11
11496 : 16);
bdaaa2e1 11497 /* Itbl support may require additional care here. */
252b5132 11498 coproc = 1;
df58fc94 11499 goto ld_st;
252b5132 11500 case M_SWC3_AB:
df58fc94 11501 gas_assert (!mips_opts.micromips);
252b5132 11502 s = "swc3";
df58fc94 11503 fmt = "E,o(b)";
bdaaa2e1 11504 /* Itbl support may require additional care here. */
252b5132 11505 coproc = 1;
df58fc94 11506 goto ld_st;
252b5132
RH
11507 case M_SWL_AB:
11508 s = "swl";
df58fc94 11509 fmt = MEM12_FMT;
7f3c4072 11510 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11511 goto ld_st;
252b5132
RH
11512 case M_SWR_AB:
11513 s = "swr";
df58fc94 11514 fmt = MEM12_FMT;
7f3c4072 11515 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11516 goto ld_st;
252b5132
RH
11517 case M_SC_AB:
11518 s = "sc";
7361da2c
AB
11519 fmt = LL_SC_FMT;
11520 offbits = (mips_opts.micromips ? 12
11521 : ISA_IS_R6 (mips_opts.isa) ? 9
11522 : 16);
df58fc94 11523 goto ld_st;
252b5132
RH
11524 case M_SCD_AB:
11525 s = "scd";
7361da2c
AB
11526 fmt = LL_SC_FMT;
11527 offbits = (mips_opts.micromips ? 12
11528 : ISA_IS_R6 (mips_opts.isa) ? 9
11529 : 16);
df58fc94 11530 goto ld_st;
d43b4baf
TS
11531 case M_CACHE_AB:
11532 s = "cache";
7361da2c
AB
11533 fmt = (mips_opts.micromips ? "k,~(b)"
11534 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11535 : "k,o(b)");
11536 offbits = (mips_opts.micromips ? 12
11537 : ISA_IS_R6 (mips_opts.isa) ? 9
11538 : 16);
7f3c4072
CM
11539 goto ld_st;
11540 case M_CACHEE_AB:
7f3c4072
CM
11541 s = "cachee";
11542 fmt = "k,+j(b)";
11543 offbits = 9;
df58fc94 11544 goto ld_st;
3eebd5eb
MR
11545 case M_PREF_AB:
11546 s = "pref";
7361da2c
AB
11547 fmt = (mips_opts.micromips ? "k,~(b)"
11548 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11549 : "k,o(b)");
11550 offbits = (mips_opts.micromips ? 12
11551 : ISA_IS_R6 (mips_opts.isa) ? 9
11552 : 16);
7f3c4072
CM
11553 goto ld_st;
11554 case M_PREFE_AB:
7f3c4072
CM
11555 s = "prefe";
11556 fmt = "k,+j(b)";
11557 offbits = 9;
df58fc94 11558 goto ld_st;
252b5132 11559 case M_SDC1_AB:
252b5132 11560 s = "sdc1";
df58fc94 11561 fmt = "T,o(b)";
252b5132 11562 coproc = 1;
bdaaa2e1 11563 /* Itbl support may require additional care here. */
df58fc94 11564 goto ld_st;
252b5132
RH
11565 case M_SDC2_AB:
11566 s = "sdc2";
df58fc94 11567 fmt = COP12_FMT;
7361da2c
AB
11568 offbits = (mips_opts.micromips ? 12
11569 : ISA_IS_R6 (mips_opts.isa) ? 11
11570 : 16);
c77c0862
RS
11571 /* Itbl support may require additional care here. */
11572 coproc = 1;
11573 goto ld_st;
11574 case M_SQC2_AB:
c77c0862 11575 s = "sqc2";
14daeee3 11576 fmt = "+7,o(b)";
bdaaa2e1 11577 /* Itbl support may require additional care here. */
252b5132 11578 coproc = 1;
df58fc94 11579 goto ld_st;
252b5132 11580 case M_SDC3_AB:
df58fc94 11581 gas_assert (!mips_opts.micromips);
252b5132 11582 s = "sdc3";
df58fc94 11583 fmt = "E,o(b)";
bdaaa2e1 11584 /* Itbl support may require additional care here. */
252b5132 11585 coproc = 1;
df58fc94 11586 goto ld_st;
252b5132
RH
11587 case M_SDL_AB:
11588 s = "sdl";
df58fc94 11589 fmt = MEM12_FMT;
7f3c4072 11590 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11591 goto ld_st;
252b5132
RH
11592 case M_SDR_AB:
11593 s = "sdr";
df58fc94 11594 fmt = MEM12_FMT;
7f3c4072 11595 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11596 goto ld_st;
11597 case M_SWP_AB:
df58fc94
RS
11598 gas_assert (mips_opts.micromips);
11599 s = "swp";
11600 fmt = "t,~(b)";
7f3c4072 11601 offbits = 12;
df58fc94
RS
11602 goto ld_st;
11603 case M_SDP_AB:
df58fc94
RS
11604 gas_assert (mips_opts.micromips);
11605 s = "sdp";
11606 fmt = "t,~(b)";
7f3c4072 11607 offbits = 12;
df58fc94
RS
11608 goto ld_st;
11609 case M_SWM_AB:
df58fc94
RS
11610 gas_assert (mips_opts.micromips);
11611 s = "swm";
11612 fmt = "n,~(b)";
7f3c4072 11613 offbits = 12;
df58fc94
RS
11614 goto ld_st;
11615 case M_SDM_AB:
df58fc94
RS
11616 gas_assert (mips_opts.micromips);
11617 s = "sdm";
11618 fmt = "n,~(b)";
7f3c4072 11619 offbits = 12;
df58fc94
RS
11620
11621 ld_st:
8fc2e39e 11622 tempreg = AT;
df58fc94 11623 ld_noat:
c0ebe874 11624 breg = op[2];
f2ae14a1
RS
11625 if (small_offset_p (0, align, 16))
11626 {
11627 /* The first case exists for M_LD_AB and M_SD_AB, which are
11628 macros for o32 but which should act like normal instructions
11629 otherwise. */
11630 if (offbits == 16)
c0ebe874 11631 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
11632 offset_reloc[1], offset_reloc[2], breg);
11633 else if (small_offset_p (0, align, offbits))
11634 {
11635 if (offbits == 0)
c0ebe874 11636 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 11637 else
c0ebe874 11638 macro_build (NULL, s, fmt, op[0],
c8276761 11639 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
11640 }
11641 else
11642 {
11643 if (tempreg == AT)
11644 used_at = 1;
11645 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11646 tempreg, breg, -1, offset_reloc[0],
11647 offset_reloc[1], offset_reloc[2]);
11648 if (offbits == 0)
c0ebe874 11649 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 11650 else
c0ebe874 11651 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
11652 }
11653 break;
11654 }
11655
11656 if (tempreg == AT)
11657 used_at = 1;
11658
252b5132
RH
11659 if (offset_expr.X_op != O_constant
11660 && offset_expr.X_op != O_symbol)
11661 {
1661c76c 11662 as_bad (_("expression too complex"));
252b5132
RH
11663 offset_expr.X_op = O_constant;
11664 }
11665
2051e8c4
MR
11666 if (HAVE_32BIT_ADDRESSES
11667 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
11668 {
11669 char value [32];
11670
11671 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 11672 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 11673 }
2051e8c4 11674
252b5132
RH
11675 /* A constant expression in PIC code can be handled just as it
11676 is in non PIC code. */
aed1a261
RS
11677 if (offset_expr.X_op == O_constant)
11678 {
f2ae14a1
RS
11679 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
11680 offbits == 0 ? 16 : offbits);
11681 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 11682
f2ae14a1
RS
11683 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
11684 if (breg != 0)
11685 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11686 tempreg, tempreg, breg);
7f3c4072 11687 if (offbits == 0)
dd6a37e7 11688 {
f2ae14a1 11689 if (offset_expr.X_add_number != 0)
dd6a37e7 11690 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 11691 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 11692 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11693 }
7f3c4072 11694 else if (offbits == 16)
c0ebe874 11695 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 11696 else
c0ebe874 11697 macro_build (NULL, s, fmt, op[0],
c8276761 11698 (int) offset_expr.X_add_number, tempreg);
df58fc94 11699 }
7f3c4072 11700 else if (offbits != 16)
df58fc94 11701 {
7f3c4072
CM
11702 /* The offset field is too narrow to be used for a low-part
11703 relocation, so load the whole address into the auxillary
f2ae14a1
RS
11704 register. */
11705 load_address (tempreg, &offset_expr, &used_at);
11706 if (breg != 0)
11707 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11708 tempreg, tempreg, breg);
7f3c4072 11709 if (offbits == 0)
c0ebe874 11710 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 11711 else
c0ebe874 11712 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
11713 }
11714 else if (mips_pic == NO_PIC)
252b5132
RH
11715 {
11716 /* If this is a reference to a GP relative symbol, and there
11717 is no base register, we want
c0ebe874 11718 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
11719 Otherwise, if there is no base register, we want
11720 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 11721 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
11722 If we have a constant, we need two instructions anyhow,
11723 so we always use the latter form.
11724
11725 If we have a base register, and this is a reference to a
11726 GP relative symbol, we want
11727 addu $tempreg,$breg,$gp
c0ebe874 11728 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
11729 Otherwise we want
11730 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
11731 addu $tempreg,$tempreg,$breg
c0ebe874 11732 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 11733 With a constant we always use the latter case.
76b3015f 11734
d6bc6245
TS
11735 With 64bit address space and no base register and $at usable,
11736 we want
11737 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11738 lui $at,<sym> (BFD_RELOC_HI16_S)
11739 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11740 dsll32 $tempreg,0
11741 daddu $tempreg,$at
c0ebe874 11742 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11743 If we have a base register, we want
11744 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11745 lui $at,<sym> (BFD_RELOC_HI16_S)
11746 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11747 daddu $at,$breg
11748 dsll32 $tempreg,0
11749 daddu $tempreg,$at
c0ebe874 11750 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11751
11752 Without $at we can't generate the optimal path for superscalar
11753 processors here since this would require two temporary registers.
11754 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11755 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11756 dsll $tempreg,16
11757 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11758 dsll $tempreg,16
c0ebe874 11759 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
11760 If we have a base register, we want
11761 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
11762 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
11763 dsll $tempreg,16
11764 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
11765 dsll $tempreg,16
11766 daddu $tempreg,$tempreg,$breg
c0ebe874 11767 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 11768
6caf9ef4 11769 For GP relative symbols in 64bit address space we can use
aed1a261
RS
11770 the same sequence as in 32bit address space. */
11771 if (HAVE_64BIT_SYMBOLS)
d6bc6245 11772 {
aed1a261 11773 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
11774 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
11775 {
11776 relax_start (offset_expr.X_add_symbol);
11777 if (breg == 0)
11778 {
c0ebe874 11779 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11780 BFD_RELOC_GPREL16, mips_gp_register);
11781 }
11782 else
11783 {
11784 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
11785 tempreg, breg, mips_gp_register);
c0ebe874 11786 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
11787 BFD_RELOC_GPREL16, tempreg);
11788 }
11789 relax_switch ();
11790 }
d6bc6245 11791
741fe287 11792 if (used_at == 0 && mips_opts.at)
d6bc6245 11793 {
df58fc94 11794 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 11795 BFD_RELOC_MIPS_HIGHEST);
df58fc94 11796 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
11797 BFD_RELOC_HI16_S);
11798 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11799 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 11800 if (breg != 0)
67c0d1eb 11801 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 11802 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 11803 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 11804 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 11805 tempreg);
d6bc6245
TS
11806 used_at = 1;
11807 }
11808 else
11809 {
df58fc94 11810 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
11811 BFD_RELOC_MIPS_HIGHEST);
11812 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11813 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 11814 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
11815 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
11816 tempreg, BFD_RELOC_HI16_S);
df58fc94 11817 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 11818 if (breg != 0)
67c0d1eb 11819 macro_build (NULL, "daddu", "d,v,t",
17a2f251 11820 tempreg, tempreg, breg);
c0ebe874 11821 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11822 BFD_RELOC_LO16, tempreg);
d6bc6245 11823 }
6caf9ef4
TS
11824
11825 if (mips_relax.sequence)
11826 relax_end ();
8fc2e39e 11827 break;
d6bc6245 11828 }
256ab948 11829
252b5132
RH
11830 if (breg == 0)
11831 {
67c0d1eb 11832 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11833 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11834 {
4d7206a2 11835 relax_start (offset_expr.X_add_symbol);
c0ebe874 11836 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 11837 mips_gp_register);
4d7206a2 11838 relax_switch ();
252b5132 11839 }
67c0d1eb 11840 macro_build_lui (&offset_expr, tempreg);
c0ebe874 11841 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11842 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11843 if (mips_relax.sequence)
11844 relax_end ();
252b5132
RH
11845 }
11846 else
11847 {
67c0d1eb 11848 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 11849 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 11850 {
4d7206a2 11851 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11852 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11853 tempreg, breg, mips_gp_register);
c0ebe874 11854 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11855 BFD_RELOC_GPREL16, tempreg);
4d7206a2 11856 relax_switch ();
252b5132 11857 }
67c0d1eb
RS
11858 macro_build_lui (&offset_expr, tempreg);
11859 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11860 tempreg, tempreg, breg);
c0ebe874 11861 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11862 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
11863 if (mips_relax.sequence)
11864 relax_end ();
252b5132
RH
11865 }
11866 }
0a44bf69 11867 else if (!mips_big_got)
252b5132 11868 {
ed6fb7bd 11869 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 11870
252b5132
RH
11871 /* If this is a reference to an external symbol, we want
11872 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11873 nop
c0ebe874 11874 <op> op[0],0($tempreg)
252b5132
RH
11875 Otherwise we want
11876 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11877 nop
11878 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11879 <op> op[0],0($tempreg)
f5040a92
AO
11880
11881 For NewABI, we want
11882 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11883 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 11884
252b5132
RH
11885 If there is a base register, we add it to $tempreg before
11886 the <op>. If there is a constant, we stick it in the
11887 <op> instruction. We don't handle constants larger than
11888 16 bits, because we have no way to load the upper 16 bits
11889 (actually, we could handle them for the subset of cases
11890 in which we are not using $at). */
9c2799c2 11891 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
11892 if (HAVE_NEWABI)
11893 {
67c0d1eb
RS
11894 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11895 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 11896 if (breg != 0)
67c0d1eb 11897 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11898 tempreg, tempreg, breg);
c0ebe874 11899 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 11900 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
11901 break;
11902 }
252b5132
RH
11903 expr1.X_add_number = offset_expr.X_add_number;
11904 offset_expr.X_add_number = 0;
11905 if (expr1.X_add_number < -0x8000
11906 || expr1.X_add_number >= 0x8000)
11907 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
11908 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11909 lw_reloc_type, mips_gp_register);
269137b2 11910 load_delay_nop ();
4d7206a2
RS
11911 relax_start (offset_expr.X_add_symbol);
11912 relax_switch ();
67c0d1eb
RS
11913 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11914 tempreg, BFD_RELOC_LO16);
4d7206a2 11915 relax_end ();
252b5132 11916 if (breg != 0)
67c0d1eb 11917 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11918 tempreg, tempreg, breg);
c0ebe874 11919 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11920 }
0a44bf69 11921 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11922 {
67c0d1eb 11923 int gpdelay;
252b5132
RH
11924
11925 /* If this is a reference to an external symbol, we want
11926 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11927 addu $tempreg,$tempreg,$gp
11928 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11929 <op> op[0],0($tempreg)
252b5132
RH
11930 Otherwise we want
11931 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11932 nop
11933 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 11934 <op> op[0],0($tempreg)
252b5132
RH
11935 If there is a base register, we add it to $tempreg before
11936 the <op>. If there is a constant, we stick it in the
11937 <op> instruction. We don't handle constants larger than
11938 16 bits, because we have no way to load the upper 16 bits
11939 (actually, we could handle them for the subset of cases
f5040a92 11940 in which we are not using $at). */
9c2799c2 11941 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
11942 expr1.X_add_number = offset_expr.X_add_number;
11943 offset_expr.X_add_number = 0;
11944 if (expr1.X_add_number < -0x8000
11945 || expr1.X_add_number >= 0x8000)
11946 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 11947 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 11948 relax_start (offset_expr.X_add_symbol);
df58fc94 11949 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11950 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11951 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11952 mips_gp_register);
11953 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11954 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 11955 relax_switch ();
67c0d1eb
RS
11956 if (gpdelay)
11957 macro_build (NULL, "nop", "");
11958 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11959 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 11960 load_delay_nop ();
67c0d1eb
RS
11961 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11962 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
11963 relax_end ();
11964
252b5132 11965 if (breg != 0)
67c0d1eb 11966 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11967 tempreg, tempreg, breg);
c0ebe874 11968 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 11969 }
0a44bf69 11970 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11971 {
f5040a92
AO
11972 /* If this is a reference to an external symbol, we want
11973 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11974 add $tempreg,$tempreg,$gp
11975 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 11976 <op> op[0],<ofst>($tempreg)
f5040a92
AO
11977 Otherwise, for local symbols, we want:
11978 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 11979 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 11980 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 11981 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11982 offset_expr.X_add_number = 0;
11983 if (expr1.X_add_number < -0x8000
11984 || expr1.X_add_number >= 0x8000)
11985 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 11986 relax_start (offset_expr.X_add_symbol);
df58fc94 11987 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 11988 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
11989 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
11990 mips_gp_register);
11991 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11992 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 11993 if (breg != 0)
67c0d1eb 11994 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11995 tempreg, tempreg, breg);
c0ebe874 11996 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 11997
4d7206a2 11998 relax_switch ();
f5040a92 11999 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12000 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12001 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12002 if (breg != 0)
67c0d1eb 12003 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12004 tempreg, tempreg, breg);
c0ebe874 12005 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12006 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12007 relax_end ();
f5040a92 12008 }
252b5132
RH
12009 else
12010 abort ();
12011
252b5132
RH
12012 break;
12013
833794fc
MR
12014 case M_JRADDIUSP:
12015 gas_assert (mips_opts.micromips);
12016 gas_assert (mips_opts.insn32);
12017 start_noreorder ();
12018 macro_build (NULL, "jr", "s", RA);
c0ebe874 12019 expr1.X_add_number = op[0] << 2;
833794fc
MR
12020 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12021 end_noreorder ();
12022 break;
12023
12024 case M_JRC:
12025 gas_assert (mips_opts.micromips);
12026 gas_assert (mips_opts.insn32);
c0ebe874 12027 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12028 if (mips_opts.noreorder)
12029 macro_build (NULL, "nop", "");
12030 break;
12031
252b5132
RH
12032 case M_LI:
12033 case M_LI_S:
c0ebe874 12034 load_register (op[0], &imm_expr, 0);
8fc2e39e 12035 break;
252b5132
RH
12036
12037 case M_DLI:
c0ebe874 12038 load_register (op[0], &imm_expr, 1);
8fc2e39e 12039 break;
252b5132
RH
12040
12041 case M_LI_SS:
12042 if (imm_expr.X_op == O_constant)
12043 {
8fc2e39e 12044 used_at = 1;
67c0d1eb 12045 load_register (AT, &imm_expr, 0);
c0ebe874 12046 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12047 break;
12048 }
12049 else
12050 {
b0e6f033
RS
12051 gas_assert (imm_expr.X_op == O_absent
12052 && offset_expr.X_op == O_symbol
90ecf173
MR
12053 && strcmp (segment_name (S_GET_SEGMENT
12054 (offset_expr.X_add_symbol)),
12055 ".lit4") == 0
12056 && offset_expr.X_add_number == 0);
c0ebe874 12057 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12058 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12059 break;
252b5132
RH
12060 }
12061
12062 case M_LI_D:
ca4e0257
RS
12063 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12064 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12065 order 32 bits of the value and the low order 32 bits are either
12066 zero or in OFFSET_EXPR. */
b0e6f033 12067 if (imm_expr.X_op == O_constant)
252b5132 12068 {
bad1aba3 12069 if (GPR_SIZE == 64)
c0ebe874 12070 load_register (op[0], &imm_expr, 1);
252b5132
RH
12071 else
12072 {
12073 int hreg, lreg;
12074
12075 if (target_big_endian)
12076 {
c0ebe874
RS
12077 hreg = op[0];
12078 lreg = op[0] + 1;
252b5132
RH
12079 }
12080 else
12081 {
c0ebe874
RS
12082 hreg = op[0] + 1;
12083 lreg = op[0];
252b5132
RH
12084 }
12085
12086 if (hreg <= 31)
67c0d1eb 12087 load_register (hreg, &imm_expr, 0);
252b5132
RH
12088 if (lreg <= 31)
12089 {
12090 if (offset_expr.X_op == O_absent)
67c0d1eb 12091 move_register (lreg, 0);
252b5132
RH
12092 else
12093 {
9c2799c2 12094 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12095 load_register (lreg, &offset_expr, 0);
252b5132
RH
12096 }
12097 }
12098 }
8fc2e39e 12099 break;
252b5132 12100 }
b0e6f033 12101 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12102
12103 /* We know that sym is in the .rdata section. First we get the
12104 upper 16 bits of the address. */
12105 if (mips_pic == NO_PIC)
12106 {
67c0d1eb 12107 macro_build_lui (&offset_expr, AT);
8fc2e39e 12108 used_at = 1;
252b5132 12109 }
0a44bf69 12110 else
252b5132 12111 {
67c0d1eb
RS
12112 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12113 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12114 used_at = 1;
252b5132 12115 }
bdaaa2e1 12116
252b5132 12117 /* Now we load the register(s). */
bad1aba3 12118 if (GPR_SIZE == 64)
8fc2e39e
TS
12119 {
12120 used_at = 1;
c0ebe874
RS
12121 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12122 BFD_RELOC_LO16, AT);
8fc2e39e 12123 }
252b5132
RH
12124 else
12125 {
8fc2e39e 12126 used_at = 1;
c0ebe874
RS
12127 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12128 BFD_RELOC_LO16, AT);
12129 if (op[0] != RA)
252b5132
RH
12130 {
12131 /* FIXME: How in the world do we deal with the possible
12132 overflow here? */
12133 offset_expr.X_add_number += 4;
67c0d1eb 12134 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12135 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12136 }
12137 }
252b5132
RH
12138 break;
12139
12140 case M_LI_DD:
ca4e0257
RS
12141 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12142 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12143 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12144 the value and the low order 32 bits are either zero or in
12145 OFFSET_EXPR. */
b0e6f033 12146 if (imm_expr.X_op == O_constant)
252b5132 12147 {
8fc2e39e 12148 used_at = 1;
bad1aba3 12149 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12150 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12151 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12152 else
12153 {
351cdf24
MF
12154 if (ISA_HAS_MXHC1 (mips_opts.isa))
12155 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12156 else if (FPR_SIZE != 32)
12157 as_bad (_("Unable to generate `%s' compliant code "
12158 "without mthc1"),
12159 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12160 else
12161 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12162 if (offset_expr.X_op == O_absent)
c0ebe874 12163 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12164 else
12165 {
9c2799c2 12166 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12167 load_register (AT, &offset_expr, 0);
c0ebe874 12168 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12169 }
12170 }
12171 break;
12172 }
12173
b0e6f033
RS
12174 gas_assert (imm_expr.X_op == O_absent
12175 && offset_expr.X_op == O_symbol
90ecf173 12176 && offset_expr.X_add_number == 0);
252b5132
RH
12177 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12178 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12179 {
12180 op[2] = mips_gp_register;
f2ae14a1
RS
12181 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12182 offset_reloc[1] = BFD_RELOC_UNUSED;
12183 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12184 }
12185 else
12186 {
9c2799c2 12187 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12188 used_at = 1;
0a44bf69 12189 if (mips_pic != NO_PIC)
67c0d1eb
RS
12190 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12191 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12192 else
12193 {
12194 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12195 macro_build_lui (&offset_expr, AT);
252b5132 12196 }
bdaaa2e1 12197
c0ebe874 12198 op[2] = AT;
f2ae14a1
RS
12199 offset_reloc[0] = BFD_RELOC_LO16;
12200 offset_reloc[1] = BFD_RELOC_UNUSED;
12201 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12202 }
f2ae14a1
RS
12203 align = 8;
12204 /* Fall through */
c4a68bea 12205
252b5132
RH
12206 case M_L_DAB:
12207 /*
12208 * The MIPS assembler seems to check for X_add_number not
12209 * being double aligned and generating:
12210 * lui at,%hi(foo+1)
12211 * addu at,at,v1
12212 * addiu at,at,%lo(foo+1)
12213 * lwc1 f2,0(at)
12214 * lwc1 f3,4(at)
12215 * But, the resulting address is the same after relocation so why
12216 * generate the extra instruction?
12217 */
bdaaa2e1 12218 /* Itbl support may require additional care here. */
252b5132 12219 coproc = 1;
df58fc94 12220 fmt = "T,o(b)";
0aa27725 12221 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12222 {
12223 s = "ldc1";
df58fc94 12224 goto ld_st;
252b5132 12225 }
252b5132 12226 s = "lwc1";
252b5132
RH
12227 goto ldd_std;
12228
12229 case M_S_DAB:
df58fc94
RS
12230 gas_assert (!mips_opts.micromips);
12231 /* Itbl support may require additional care here. */
12232 coproc = 1;
12233 fmt = "T,o(b)";
0aa27725 12234 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12235 {
12236 s = "sdc1";
df58fc94 12237 goto ld_st;
252b5132 12238 }
252b5132 12239 s = "swc1";
252b5132
RH
12240 goto ldd_std;
12241
e407c74b
NC
12242 case M_LQ_AB:
12243 fmt = "t,o(b)";
12244 s = "lq";
12245 goto ld;
12246
12247 case M_SQ_AB:
12248 fmt = "t,o(b)";
12249 s = "sq";
12250 goto ld_st;
12251
252b5132 12252 case M_LD_AB:
df58fc94 12253 fmt = "t,o(b)";
bad1aba3 12254 if (GPR_SIZE == 64)
252b5132
RH
12255 {
12256 s = "ld";
12257 goto ld;
12258 }
252b5132 12259 s = "lw";
252b5132
RH
12260 goto ldd_std;
12261
12262 case M_SD_AB:
df58fc94 12263 fmt = "t,o(b)";
bad1aba3 12264 if (GPR_SIZE == 64)
252b5132
RH
12265 {
12266 s = "sd";
df58fc94 12267 goto ld_st;
252b5132 12268 }
252b5132 12269 s = "sw";
252b5132
RH
12270
12271 ldd_std:
f2ae14a1
RS
12272 /* Even on a big endian machine $fn comes before $fn+1. We have
12273 to adjust when loading from memory. We set coproc if we must
12274 load $fn+1 first. */
12275 /* Itbl support may require additional care here. */
12276 if (!target_big_endian)
12277 coproc = 0;
12278
c0ebe874 12279 breg = op[2];
f2ae14a1
RS
12280 if (small_offset_p (0, align, 16))
12281 {
12282 ep = &offset_expr;
12283 if (!small_offset_p (4, align, 16))
12284 {
12285 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12286 -1, offset_reloc[0], offset_reloc[1],
12287 offset_reloc[2]);
12288 expr1.X_add_number = 0;
12289 ep = &expr1;
12290 breg = AT;
12291 used_at = 1;
12292 offset_reloc[0] = BFD_RELOC_LO16;
12293 offset_reloc[1] = BFD_RELOC_UNUSED;
12294 offset_reloc[2] = BFD_RELOC_UNUSED;
12295 }
c0ebe874 12296 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12297 {
12298 ep->X_add_number += 4;
c0ebe874 12299 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12300 offset_reloc[1], offset_reloc[2], breg);
12301 ep->X_add_number -= 4;
c0ebe874 12302 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12303 offset_reloc[1], offset_reloc[2], breg);
12304 }
12305 else
12306 {
c0ebe874 12307 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12308 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12309 breg);
12310 ep->X_add_number += 4;
c0ebe874 12311 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12312 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12313 breg);
12314 }
12315 break;
12316 }
12317
252b5132
RH
12318 if (offset_expr.X_op != O_symbol
12319 && offset_expr.X_op != O_constant)
12320 {
1661c76c 12321 as_bad (_("expression too complex"));
252b5132
RH
12322 offset_expr.X_op = O_constant;
12323 }
12324
2051e8c4
MR
12325 if (HAVE_32BIT_ADDRESSES
12326 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12327 {
12328 char value [32];
12329
12330 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12331 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12332 }
2051e8c4 12333
90ecf173 12334 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12335 {
12336 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12337 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12338 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12339 If we have a base register, we use this
12340 addu $at,$breg,$gp
c0ebe874
RS
12341 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12342 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12343 If this is not a GP relative symbol, we want
12344 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12345 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12346 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12347 If there is a base register, we add it to $at after the
12348 lui instruction. If there is a constant, we always use
12349 the last case. */
39a59cf8
MR
12350 if (offset_expr.X_op == O_symbol
12351 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12352 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12353 {
4d7206a2 12354 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12355 if (breg == 0)
12356 {
c9914766 12357 tempreg = mips_gp_register;
252b5132
RH
12358 }
12359 else
12360 {
67c0d1eb 12361 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12362 AT, breg, mips_gp_register);
252b5132 12363 tempreg = AT;
252b5132
RH
12364 used_at = 1;
12365 }
12366
beae10d5 12367 /* Itbl support may require additional care here. */
c0ebe874 12368 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12369 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12370 offset_expr.X_add_number += 4;
12371
12372 /* Set mips_optimize to 2 to avoid inserting an
12373 undesired nop. */
12374 hold_mips_optimize = mips_optimize;
12375 mips_optimize = 2;
beae10d5 12376 /* Itbl support may require additional care here. */
c0ebe874 12377 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12378 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12379 mips_optimize = hold_mips_optimize;
12380
4d7206a2 12381 relax_switch ();
252b5132 12382
0970e49e 12383 offset_expr.X_add_number -= 4;
252b5132 12384 }
8fc2e39e 12385 used_at = 1;
f2ae14a1
RS
12386 if (offset_high_part (offset_expr.X_add_number, 16)
12387 != offset_high_part (offset_expr.X_add_number + 4, 16))
12388 {
12389 load_address (AT, &offset_expr, &used_at);
12390 offset_expr.X_op = O_constant;
12391 offset_expr.X_add_number = 0;
12392 }
12393 else
12394 macro_build_lui (&offset_expr, AT);
252b5132 12395 if (breg != 0)
67c0d1eb 12396 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12397 /* Itbl support may require additional care here. */
c0ebe874 12398 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12399 BFD_RELOC_LO16, AT);
252b5132
RH
12400 /* FIXME: How do we handle overflow here? */
12401 offset_expr.X_add_number += 4;
beae10d5 12402 /* Itbl support may require additional care here. */
c0ebe874 12403 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12404 BFD_RELOC_LO16, AT);
4d7206a2
RS
12405 if (mips_relax.sequence)
12406 relax_end ();
bdaaa2e1 12407 }
0a44bf69 12408 else if (!mips_big_got)
252b5132 12409 {
252b5132
RH
12410 /* If this is a reference to an external symbol, we want
12411 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12412 nop
c0ebe874
RS
12413 <op> op[0],0($at)
12414 <op> op[0]+1,4($at)
252b5132
RH
12415 Otherwise we want
12416 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12417 nop
c0ebe874
RS
12418 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12419 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12420 If there is a base register we add it to $at before the
12421 lwc1 instructions. If there is a constant we include it
12422 in the lwc1 instructions. */
12423 used_at = 1;
12424 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12425 if (expr1.X_add_number < -0x8000
12426 || expr1.X_add_number >= 0x8000 - 4)
12427 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12428 load_got_offset (AT, &offset_expr);
269137b2 12429 load_delay_nop ();
252b5132 12430 if (breg != 0)
67c0d1eb 12431 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12432
12433 /* Set mips_optimize to 2 to avoid inserting an undesired
12434 nop. */
12435 hold_mips_optimize = mips_optimize;
12436 mips_optimize = 2;
4d7206a2 12437
beae10d5 12438 /* Itbl support may require additional care here. */
4d7206a2 12439 relax_start (offset_expr.X_add_symbol);
c0ebe874 12440 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12441 BFD_RELOC_LO16, AT);
4d7206a2 12442 expr1.X_add_number += 4;
c0ebe874 12443 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12444 BFD_RELOC_LO16, AT);
4d7206a2 12445 relax_switch ();
c0ebe874 12446 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12447 BFD_RELOC_LO16, AT);
4d7206a2 12448 offset_expr.X_add_number += 4;
c0ebe874 12449 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12450 BFD_RELOC_LO16, AT);
4d7206a2 12451 relax_end ();
252b5132 12452
4d7206a2 12453 mips_optimize = hold_mips_optimize;
252b5132 12454 }
0a44bf69 12455 else if (mips_big_got)
252b5132 12456 {
67c0d1eb 12457 int gpdelay;
252b5132
RH
12458
12459 /* If this is a reference to an external symbol, we want
12460 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12461 addu $at,$at,$gp
12462 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12463 nop
c0ebe874
RS
12464 <op> op[0],0($at)
12465 <op> op[0]+1,4($at)
252b5132
RH
12466 Otherwise we want
12467 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12468 nop
c0ebe874
RS
12469 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12470 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12471 If there is a base register we add it to $at before the
12472 lwc1 instructions. If there is a constant we include it
12473 in the lwc1 instructions. */
12474 used_at = 1;
12475 expr1.X_add_number = offset_expr.X_add_number;
12476 offset_expr.X_add_number = 0;
12477 if (expr1.X_add_number < -0x8000
12478 || expr1.X_add_number >= 0x8000 - 4)
12479 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12480 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12481 relax_start (offset_expr.X_add_symbol);
df58fc94 12482 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12483 AT, BFD_RELOC_MIPS_GOT_HI16);
12484 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12485 AT, AT, mips_gp_register);
67c0d1eb 12486 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12487 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12488 load_delay_nop ();
252b5132 12489 if (breg != 0)
67c0d1eb 12490 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12491 /* Itbl support may require additional care here. */
c0ebe874 12492 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12493 BFD_RELOC_LO16, AT);
252b5132
RH
12494 expr1.X_add_number += 4;
12495
12496 /* Set mips_optimize to 2 to avoid inserting an undesired
12497 nop. */
12498 hold_mips_optimize = mips_optimize;
12499 mips_optimize = 2;
beae10d5 12500 /* Itbl support may require additional care here. */
c0ebe874 12501 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12502 BFD_RELOC_LO16, AT);
252b5132
RH
12503 mips_optimize = hold_mips_optimize;
12504 expr1.X_add_number -= 4;
12505
4d7206a2
RS
12506 relax_switch ();
12507 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12508 if (gpdelay)
12509 macro_build (NULL, "nop", "");
12510 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12511 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12512 load_delay_nop ();
252b5132 12513 if (breg != 0)
67c0d1eb 12514 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12515 /* Itbl support may require additional care here. */
c0ebe874 12516 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12517 BFD_RELOC_LO16, AT);
4d7206a2 12518 offset_expr.X_add_number += 4;
252b5132
RH
12519
12520 /* Set mips_optimize to 2 to avoid inserting an undesired
12521 nop. */
12522 hold_mips_optimize = mips_optimize;
12523 mips_optimize = 2;
beae10d5 12524 /* Itbl support may require additional care here. */
c0ebe874 12525 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12526 BFD_RELOC_LO16, AT);
252b5132 12527 mips_optimize = hold_mips_optimize;
4d7206a2 12528 relax_end ();
252b5132 12529 }
252b5132
RH
12530 else
12531 abort ();
12532
252b5132 12533 break;
3739860c 12534
dd6a37e7 12535 case M_SAA_AB:
dd6a37e7 12536 s = "saa";
0db377d0 12537 goto saa_saad;
dd6a37e7 12538 case M_SAAD_AB:
dd6a37e7 12539 s = "saad";
0db377d0
MR
12540 saa_saad:
12541 gas_assert (!mips_opts.micromips);
7f3c4072 12542 offbits = 0;
dd6a37e7
AP
12543 fmt = "t,(b)";
12544 goto ld_st;
12545
252b5132
RH
12546 /* New code added to support COPZ instructions.
12547 This code builds table entries out of the macros in mip_opcodes.
12548 R4000 uses interlocks to handle coproc delays.
12549 Other chips (like the R3000) require nops to be inserted for delays.
12550
f72c8c98 12551 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12552 In order to fill delay slots for non-interlocked chips,
12553 we must have a way to specify delays based on the coprocessor.
12554 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12555 What are the side-effects of the cop instruction?
12556 What cache support might we have and what are its effects?
12557 Both coprocessor & memory require delays. how long???
bdaaa2e1 12558 What registers are read/set/modified?
252b5132
RH
12559
12560 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12561 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12562
12563 case M_COP0:
12564 s = "c0";
12565 goto copz;
12566 case M_COP1:
12567 s = "c1";
12568 goto copz;
12569 case M_COP2:
12570 s = "c2";
12571 goto copz;
12572 case M_COP3:
12573 s = "c3";
12574 copz:
df58fc94 12575 gas_assert (!mips_opts.micromips);
252b5132
RH
12576 /* For now we just do C (same as Cz). The parameter will be
12577 stored in insn_opcode by mips_ip. */
c8276761 12578 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12579 break;
252b5132 12580
ea1fb5dc 12581 case M_MOVE:
c0ebe874 12582 move_register (op[0], op[1]);
8fc2e39e 12583 break;
ea1fb5dc 12584
833794fc
MR
12585 case M_MOVEP:
12586 gas_assert (mips_opts.micromips);
12587 gas_assert (mips_opts.insn32);
c0ebe874
RS
12588 move_register (micromips_to_32_reg_h_map1[op[0]],
12589 micromips_to_32_reg_m_map[op[1]]);
12590 move_register (micromips_to_32_reg_h_map2[op[0]],
12591 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12592 break;
12593
252b5132
RH
12594 case M_DMUL:
12595 dbl = 1;
12596 case M_MUL:
e407c74b 12597 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12598 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12599 op[2]);
e407c74b
NC
12600 else
12601 {
c0ebe874
RS
12602 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12603 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12604 }
8fc2e39e 12605 break;
252b5132
RH
12606
12607 case M_DMUL_I:
12608 dbl = 1;
12609 case M_MUL_I:
12610 /* The MIPS assembler some times generates shifts and adds. I'm
12611 not trying to be that fancy. GCC should do this for us
12612 anyway. */
8fc2e39e 12613 used_at = 1;
67c0d1eb 12614 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12615 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12616 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12617 break;
12618
12619 case M_DMULO_I:
12620 dbl = 1;
12621 case M_MULO_I:
12622 imm = 1;
12623 goto do_mulo;
12624
12625 case M_DMULO:
12626 dbl = 1;
12627 case M_MULO:
12628 do_mulo:
7d10b47d 12629 start_noreorder ();
8fc2e39e 12630 used_at = 1;
252b5132 12631 if (imm)
67c0d1eb 12632 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12633 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
12634 op[1], imm ? AT : op[2]);
12635 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
12636 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 12637 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 12638 if (mips_trap)
c0ebe874 12639 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
12640 else
12641 {
df58fc94
RS
12642 if (mips_opts.micromips)
12643 micromips_label_expr (&label_expr);
12644 else
12645 label_expr.X_add_number = 8;
c0ebe874 12646 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 12647 macro_build (NULL, "nop", "");
df58fc94
RS
12648 macro_build (NULL, "break", BRK_FMT, 6);
12649 if (mips_opts.micromips)
12650 micromips_add_label ();
252b5132 12651 }
7d10b47d 12652 end_noreorder ();
c0ebe874 12653 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12654 break;
12655
12656 case M_DMULOU_I:
12657 dbl = 1;
12658 case M_MULOU_I:
12659 imm = 1;
12660 goto do_mulou;
12661
12662 case M_DMULOU:
12663 dbl = 1;
12664 case M_MULOU:
12665 do_mulou:
7d10b47d 12666 start_noreorder ();
8fc2e39e 12667 used_at = 1;
252b5132 12668 if (imm)
67c0d1eb
RS
12669 load_register (AT, &imm_expr, dbl);
12670 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 12671 op[1], imm ? AT : op[2]);
df58fc94 12672 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 12673 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 12674 if (mips_trap)
df58fc94 12675 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
12676 else
12677 {
df58fc94
RS
12678 if (mips_opts.micromips)
12679 micromips_label_expr (&label_expr);
12680 else
12681 label_expr.X_add_number = 8;
12682 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 12683 macro_build (NULL, "nop", "");
df58fc94
RS
12684 macro_build (NULL, "break", BRK_FMT, 6);
12685 if (mips_opts.micromips)
12686 micromips_add_label ();
252b5132 12687 }
7d10b47d 12688 end_noreorder ();
252b5132
RH
12689 break;
12690
771c7ce4 12691 case M_DROL:
fef14a42 12692 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12693 {
c0ebe874 12694 if (op[0] == op[1])
82dd0097
CD
12695 {
12696 tempreg = AT;
12697 used_at = 1;
12698 }
12699 else
c0ebe874
RS
12700 tempreg = op[0];
12701 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
12702 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12703 break;
82dd0097 12704 }
8fc2e39e 12705 used_at = 1;
c0ebe874
RS
12706 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12707 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
12708 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
12709 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12710 break;
12711
252b5132 12712 case M_ROL:
fef14a42 12713 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12714 {
c0ebe874 12715 if (op[0] == op[1])
82dd0097
CD
12716 {
12717 tempreg = AT;
12718 used_at = 1;
12719 }
12720 else
c0ebe874
RS
12721 tempreg = op[0];
12722 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
12723 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 12724 break;
82dd0097 12725 }
8fc2e39e 12726 used_at = 1;
c0ebe874
RS
12727 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12728 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
12729 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
12730 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12731 break;
12732
771c7ce4
TS
12733 case M_DROL_I:
12734 {
12735 unsigned int rot;
e0471c16
TS
12736 const char *l;
12737 const char *rr;
771c7ce4 12738
771c7ce4 12739 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12740 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
12741 {
12742 rot = (64 - rot) & 0x3f;
12743 if (rot >= 32)
c0ebe874 12744 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 12745 else
c0ebe874 12746 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12747 break;
60b63b72 12748 }
483fc7cd 12749 if (rot == 0)
483fc7cd 12750 {
c0ebe874 12751 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12752 break;
483fc7cd 12753 }
82dd0097 12754 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 12755 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 12756 rot &= 0x1f;
8fc2e39e 12757 used_at = 1;
c0ebe874
RS
12758 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
12759 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12760 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12761 }
12762 break;
12763
252b5132 12764 case M_ROL_I:
771c7ce4
TS
12765 {
12766 unsigned int rot;
12767
771c7ce4 12768 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12769 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 12770 {
c0ebe874
RS
12771 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
12772 (32 - rot) & 0x1f);
8fc2e39e 12773 break;
60b63b72 12774 }
483fc7cd 12775 if (rot == 0)
483fc7cd 12776 {
c0ebe874 12777 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12778 break;
483fc7cd 12779 }
8fc2e39e 12780 used_at = 1;
c0ebe874
RS
12781 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
12782 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12783 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12784 }
12785 break;
12786
12787 case M_DROR:
fef14a42 12788 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 12789 {
c0ebe874 12790 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12791 break;
82dd0097 12792 }
8fc2e39e 12793 used_at = 1;
c0ebe874
RS
12794 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
12795 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
12796 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
12797 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12798 break;
12799
12800 case M_ROR:
fef14a42 12801 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12802 {
c0ebe874 12803 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 12804 break;
82dd0097 12805 }
8fc2e39e 12806 used_at = 1;
c0ebe874
RS
12807 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
12808 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
12809 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
12810 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
12811 break;
12812
771c7ce4
TS
12813 case M_DROR_I:
12814 {
12815 unsigned int rot;
e0471c16
TS
12816 const char *l;
12817 const char *rr;
771c7ce4 12818
771c7ce4 12819 rot = imm_expr.X_add_number & 0x3f;
fef14a42 12820 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
12821 {
12822 if (rot >= 32)
c0ebe874 12823 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 12824 else
c0ebe874 12825 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12826 break;
82dd0097 12827 }
483fc7cd 12828 if (rot == 0)
483fc7cd 12829 {
c0ebe874 12830 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12831 break;
483fc7cd 12832 }
91d6fa6a 12833 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
12834 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
12835 rot &= 0x1f;
8fc2e39e 12836 used_at = 1;
c0ebe874
RS
12837 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
12838 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12839 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
12840 }
12841 break;
12842
252b5132 12843 case M_ROR_I:
771c7ce4
TS
12844 {
12845 unsigned int rot;
12846
771c7ce4 12847 rot = imm_expr.X_add_number & 0x1f;
fef14a42 12848 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 12849 {
c0ebe874 12850 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 12851 break;
82dd0097 12852 }
483fc7cd 12853 if (rot == 0)
483fc7cd 12854 {
c0ebe874 12855 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 12856 break;
483fc7cd 12857 }
8fc2e39e 12858 used_at = 1;
c0ebe874
RS
12859 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
12860 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
12861 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 12862 }
252b5132
RH
12863 break;
12864
252b5132 12865 case M_SEQ:
c0ebe874
RS
12866 if (op[1] == 0)
12867 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
12868 else if (op[2] == 0)
12869 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12870 else
12871 {
c0ebe874
RS
12872 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
12873 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 12874 }
8fc2e39e 12875 break;
252b5132
RH
12876
12877 case M_SEQ_I:
b0e6f033 12878 if (imm_expr.X_add_number == 0)
252b5132 12879 {
c0ebe874 12880 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 12881 break;
252b5132 12882 }
c0ebe874 12883 if (op[1] == 0)
252b5132 12884 {
1661c76c 12885 as_warn (_("instruction %s: result is always false"),
252b5132 12886 ip->insn_mo->name);
c0ebe874 12887 move_register (op[0], 0);
8fc2e39e 12888 break;
252b5132 12889 }
dd3cbb7e
NC
12890 if (CPU_HAS_SEQ (mips_opts.arch)
12891 && -512 <= imm_expr.X_add_number
12892 && imm_expr.X_add_number < 512)
12893 {
c0ebe874 12894 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 12895 (int) imm_expr.X_add_number);
dd3cbb7e
NC
12896 break;
12897 }
b0e6f033 12898 if (imm_expr.X_add_number >= 0
252b5132 12899 && imm_expr.X_add_number < 0x10000)
c0ebe874 12900 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 12901 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
12902 && imm_expr.X_add_number < 0)
12903 {
12904 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 12905 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 12906 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 12907 }
dd3cbb7e
NC
12908 else if (CPU_HAS_SEQ (mips_opts.arch))
12909 {
12910 used_at = 1;
bad1aba3 12911 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12912 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
12913 break;
12914 }
252b5132
RH
12915 else
12916 {
bad1aba3 12917 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12918 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
12919 used_at = 1;
12920 }
c0ebe874 12921 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12922 break;
252b5132 12923
c0ebe874 12924 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
12925 s = "slt";
12926 goto sge;
12927 case M_SGEU:
12928 s = "sltu";
12929 sge:
c0ebe874
RS
12930 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
12931 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12932 break;
252b5132 12933
c0ebe874 12934 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 12935 case M_SGEU_I:
b0e6f033 12936 if (imm_expr.X_add_number >= -0x8000
252b5132 12937 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
12938 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
12939 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
12940 else
12941 {
bad1aba3 12942 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 12943 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 12944 op[0], op[1], AT);
252b5132
RH
12945 used_at = 1;
12946 }
c0ebe874 12947 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12948 break;
252b5132 12949
c0ebe874 12950 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
12951 s = "slt";
12952 goto sgt;
12953 case M_SGTU:
12954 s = "sltu";
12955 sgt:
c0ebe874 12956 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 12957 break;
252b5132 12958
c0ebe874 12959 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
12960 s = "slt";
12961 goto sgti;
12962 case M_SGTU_I:
12963 s = "sltu";
12964 sgti:
8fc2e39e 12965 used_at = 1;
bad1aba3 12966 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 12967 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
12968 break;
12969
c0ebe874 12970 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
12971 s = "slt";
12972 goto sle;
12973 case M_SLEU:
12974 s = "sltu";
12975 sle:
c0ebe874
RS
12976 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
12977 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 12978 break;
252b5132 12979
c0ebe874 12980 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
12981 s = "slt";
12982 goto slei;
12983 case M_SLEU_I:
12984 s = "sltu";
12985 slei:
8fc2e39e 12986 used_at = 1;
bad1aba3 12987 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
12988 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
12989 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
12990 break;
12991
12992 case M_SLT_I:
b0e6f033 12993 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
12994 && imm_expr.X_add_number < 0x8000)
12995 {
c0ebe874
RS
12996 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
12997 BFD_RELOC_LO16);
8fc2e39e 12998 break;
252b5132 12999 }
8fc2e39e 13000 used_at = 1;
bad1aba3 13001 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13002 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13003 break;
13004
13005 case M_SLTU_I:
b0e6f033 13006 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13007 && imm_expr.X_add_number < 0x8000)
13008 {
c0ebe874 13009 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13010 BFD_RELOC_LO16);
8fc2e39e 13011 break;
252b5132 13012 }
8fc2e39e 13013 used_at = 1;
bad1aba3 13014 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13015 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13016 break;
13017
13018 case M_SNE:
c0ebe874
RS
13019 if (op[1] == 0)
13020 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13021 else if (op[2] == 0)
13022 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13023 else
13024 {
c0ebe874
RS
13025 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13026 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13027 }
8fc2e39e 13028 break;
252b5132
RH
13029
13030 case M_SNE_I:
b0e6f033 13031 if (imm_expr.X_add_number == 0)
252b5132 13032 {
c0ebe874 13033 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13034 break;
252b5132 13035 }
c0ebe874 13036 if (op[1] == 0)
252b5132 13037 {
1661c76c 13038 as_warn (_("instruction %s: result is always true"),
252b5132 13039 ip->insn_mo->name);
bad1aba3 13040 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13041 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13042 break;
252b5132 13043 }
dd3cbb7e
NC
13044 if (CPU_HAS_SEQ (mips_opts.arch)
13045 && -512 <= imm_expr.X_add_number
13046 && imm_expr.X_add_number < 512)
13047 {
c0ebe874 13048 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13049 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13050 break;
13051 }
b0e6f033 13052 if (imm_expr.X_add_number >= 0
252b5132
RH
13053 && imm_expr.X_add_number < 0x10000)
13054 {
c0ebe874
RS
13055 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13056 BFD_RELOC_LO16);
252b5132 13057 }
b0e6f033 13058 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13059 && imm_expr.X_add_number < 0)
13060 {
13061 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13062 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13063 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13064 }
dd3cbb7e
NC
13065 else if (CPU_HAS_SEQ (mips_opts.arch))
13066 {
13067 used_at = 1;
bad1aba3 13068 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13069 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13070 break;
13071 }
252b5132
RH
13072 else
13073 {
bad1aba3 13074 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13075 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13076 used_at = 1;
13077 }
c0ebe874 13078 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13079 break;
252b5132 13080
df58fc94
RS
13081 case M_SUB_I:
13082 s = "addi";
13083 s2 = "sub";
13084 goto do_subi;
13085 case M_SUBU_I:
13086 s = "addiu";
13087 s2 = "subu";
13088 goto do_subi;
252b5132
RH
13089 case M_DSUB_I:
13090 dbl = 1;
df58fc94
RS
13091 s = "daddi";
13092 s2 = "dsub";
13093 if (!mips_opts.micromips)
13094 goto do_subi;
b0e6f033 13095 if (imm_expr.X_add_number > -0x200
df58fc94 13096 && imm_expr.X_add_number <= 0x200)
252b5132 13097 {
b0e6f033
RS
13098 macro_build (NULL, s, "t,r,.", op[0], op[1],
13099 (int) -imm_expr.X_add_number);
8fc2e39e 13100 break;
252b5132 13101 }
df58fc94 13102 goto do_subi_i;
252b5132
RH
13103 case M_DSUBU_I:
13104 dbl = 1;
df58fc94
RS
13105 s = "daddiu";
13106 s2 = "dsubu";
13107 do_subi:
b0e6f033 13108 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13109 && imm_expr.X_add_number <= 0x8000)
13110 {
13111 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13112 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13113 break;
252b5132 13114 }
df58fc94 13115 do_subi_i:
8fc2e39e 13116 used_at = 1;
67c0d1eb 13117 load_register (AT, &imm_expr, dbl);
c0ebe874 13118 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13119 break;
13120
13121 case M_TEQ_I:
13122 s = "teq";
13123 goto trap;
13124 case M_TGE_I:
13125 s = "tge";
13126 goto trap;
13127 case M_TGEU_I:
13128 s = "tgeu";
13129 goto trap;
13130 case M_TLT_I:
13131 s = "tlt";
13132 goto trap;
13133 case M_TLTU_I:
13134 s = "tltu";
13135 goto trap;
13136 case M_TNE_I:
13137 s = "tne";
13138 trap:
8fc2e39e 13139 used_at = 1;
bad1aba3 13140 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13141 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13142 break;
13143
252b5132 13144 case M_TRUNCWS:
43841e91 13145 case M_TRUNCWD:
df58fc94 13146 gas_assert (!mips_opts.micromips);
0aa27725 13147 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13148 used_at = 1;
252b5132
RH
13149
13150 /*
13151 * Is the double cfc1 instruction a bug in the mips assembler;
13152 * or is there a reason for it?
13153 */
7d10b47d 13154 start_noreorder ();
c0ebe874
RS
13155 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13156 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13157 macro_build (NULL, "nop", "");
252b5132 13158 expr1.X_add_number = 3;
c0ebe874 13159 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13160 expr1.X_add_number = 2;
67c0d1eb
RS
13161 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13162 macro_build (NULL, "ctc1", "t,G", AT, RA);
13163 macro_build (NULL, "nop", "");
13164 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13165 op[0], op[1]);
13166 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13167 macro_build (NULL, "nop", "");
7d10b47d 13168 end_noreorder ();
252b5132
RH
13169 break;
13170
f2ae14a1 13171 case M_ULH_AB:
252b5132 13172 s = "lb";
df58fc94
RS
13173 s2 = "lbu";
13174 off = 1;
13175 goto uld_st;
f2ae14a1 13176 case M_ULHU_AB:
252b5132 13177 s = "lbu";
df58fc94
RS
13178 s2 = "lbu";
13179 off = 1;
13180 goto uld_st;
f2ae14a1 13181 case M_ULW_AB:
df58fc94
RS
13182 s = "lwl";
13183 s2 = "lwr";
7f3c4072 13184 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13185 off = 3;
13186 goto uld_st;
f2ae14a1 13187 case M_ULD_AB:
252b5132
RH
13188 s = "ldl";
13189 s2 = "ldr";
7f3c4072 13190 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13191 off = 7;
df58fc94 13192 goto uld_st;
f2ae14a1 13193 case M_USH_AB:
df58fc94
RS
13194 s = "sb";
13195 s2 = "sb";
13196 off = 1;
13197 ust = 1;
13198 goto uld_st;
f2ae14a1 13199 case M_USW_AB:
df58fc94
RS
13200 s = "swl";
13201 s2 = "swr";
7f3c4072 13202 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13203 off = 3;
df58fc94
RS
13204 ust = 1;
13205 goto uld_st;
f2ae14a1 13206 case M_USD_AB:
df58fc94
RS
13207 s = "sdl";
13208 s2 = "sdr";
7f3c4072 13209 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13210 off = 7;
13211 ust = 1;
13212
13213 uld_st:
c0ebe874 13214 breg = op[2];
f2ae14a1 13215 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13216 ep = &offset_expr;
13217 expr1.X_add_number = 0;
f2ae14a1 13218 if (large_offset)
df58fc94
RS
13219 {
13220 used_at = 1;
13221 tempreg = AT;
f2ae14a1
RS
13222 if (small_offset_p (0, align, 16))
13223 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13224 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13225 else
13226 {
13227 load_address (tempreg, ep, &used_at);
13228 if (breg != 0)
13229 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13230 tempreg, tempreg, breg);
13231 }
13232 offset_reloc[0] = BFD_RELOC_LO16;
13233 offset_reloc[1] = BFD_RELOC_UNUSED;
13234 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13235 breg = tempreg;
c0ebe874 13236 tempreg = op[0];
df58fc94
RS
13237 ep = &expr1;
13238 }
c0ebe874 13239 else if (!ust && op[0] == breg)
8fc2e39e
TS
13240 {
13241 used_at = 1;
13242 tempreg = AT;
13243 }
252b5132 13244 else
c0ebe874 13245 tempreg = op[0];
af22f5b2 13246
df58fc94
RS
13247 if (off == 1)
13248 goto ulh_sh;
252b5132 13249
90ecf173 13250 if (!target_big_endian)
df58fc94 13251 ep->X_add_number += off;
f2ae14a1 13252 if (offbits == 12)
c8276761 13253 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13254 else
13255 macro_build (ep, s, "t,o(b)", tempreg, -1,
13256 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13257
90ecf173 13258 if (!target_big_endian)
df58fc94 13259 ep->X_add_number -= off;
252b5132 13260 else
df58fc94 13261 ep->X_add_number += off;
f2ae14a1 13262 if (offbits == 12)
df58fc94 13263 macro_build (NULL, s2, "t,~(b)",
c8276761 13264 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13265 else
13266 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13267 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13268
df58fc94 13269 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13270 if (!ust && op[0] != tempreg)
df58fc94
RS
13271 {
13272 /* Protect second load's delay slot. */
13273 load_delay_nop ();
c0ebe874 13274 move_register (op[0], tempreg);
df58fc94 13275 }
8fc2e39e 13276 break;
252b5132 13277
df58fc94 13278 ulh_sh:
d6bc6245 13279 used_at = 1;
df58fc94
RS
13280 if (target_big_endian == ust)
13281 ep->X_add_number += off;
c0ebe874 13282 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13283 macro_build (ep, s, "t,o(b)", tempreg, -1,
13284 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13285
13286 /* For halfword transfers we need a temporary register to shuffle
13287 bytes. Unfortunately for M_USH_A we have none available before
13288 the next store as AT holds the base address. We deal with this
13289 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13290 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13291 if (ust)
c0ebe874 13292 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13293
13294 if (target_big_endian == ust)
13295 ep->X_add_number -= off;
252b5132 13296 else
df58fc94 13297 ep->X_add_number += off;
f2ae14a1
RS
13298 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13299 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13300
df58fc94 13301 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13302 if (ust && large_offset)
df58fc94
RS
13303 {
13304 if (target_big_endian)
13305 ep->X_add_number += off;
13306 else
13307 ep->X_add_number -= off;
f2ae14a1
RS
13308 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13309 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13310 }
13311 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13312 if (!ust || large_offset)
df58fc94 13313 {
c0ebe874 13314 tempreg = !large_offset ? AT : op[0];
df58fc94 13315 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13316 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13317 }
252b5132
RH
13318 break;
13319
13320 default:
13321 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13322 are added dynamically. */
1661c76c 13323 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13324 break;
13325 }
741fe287 13326 if (!mips_opts.at && used_at)
1661c76c 13327 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13328}
13329
13330/* Implement macros in mips16 mode. */
13331
13332static void
17a2f251 13333mips16_macro (struct mips_cl_insn *ip)
252b5132 13334{
c0ebe874 13335 const struct mips_operand_array *operands;
252b5132 13336 int mask;
c0ebe874 13337 int tmp;
252b5132
RH
13338 expressionS expr1;
13339 int dbl;
13340 const char *s, *s2, *s3;
c0ebe874
RS
13341 unsigned int op[MAX_OPERANDS];
13342 unsigned int i;
252b5132
RH
13343
13344 mask = ip->insn_mo->mask;
13345
c0ebe874
RS
13346 operands = insn_operands (ip);
13347 for (i = 0; i < MAX_OPERANDS; i++)
13348 if (operands->operand[i])
13349 op[i] = insn_extract_operand (ip, operands->operand[i]);
13350 else
13351 op[i] = -1;
252b5132 13352
252b5132
RH
13353 expr1.X_op = O_constant;
13354 expr1.X_op_symbol = NULL;
13355 expr1.X_add_symbol = NULL;
13356 expr1.X_add_number = 1;
13357
13358 dbl = 0;
13359
13360 switch (mask)
13361 {
13362 default:
b37df7c4 13363 abort ();
252b5132
RH
13364
13365 case M_DDIV_3:
13366 dbl = 1;
13367 case M_DIV_3:
13368 s = "mflo";
13369 goto do_div3;
13370 case M_DREM_3:
13371 dbl = 1;
13372 case M_REM_3:
13373 s = "mfhi";
13374 do_div3:
7d10b47d 13375 start_noreorder ();
c0ebe874 13376 macro_build (NULL, dbl ? "ddiv" : "div", "0,x,y", op[1], op[2]);
252b5132 13377 expr1.X_add_number = 2;
c0ebe874 13378 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13379 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13380
252b5132
RH
13381 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13382 since that causes an overflow. We should do that as well,
13383 but I don't see how to do the comparisons without a temporary
13384 register. */
7d10b47d 13385 end_noreorder ();
c0ebe874 13386 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13387 break;
13388
13389 case M_DIVU_3:
13390 s = "divu";
13391 s2 = "mflo";
13392 goto do_divu3;
13393 case M_REMU_3:
13394 s = "divu";
13395 s2 = "mfhi";
13396 goto do_divu3;
13397 case M_DDIVU_3:
13398 s = "ddivu";
13399 s2 = "mflo";
13400 goto do_divu3;
13401 case M_DREMU_3:
13402 s = "ddivu";
13403 s2 = "mfhi";
13404 do_divu3:
7d10b47d 13405 start_noreorder ();
c0ebe874 13406 macro_build (NULL, s, "0,x,y", op[1], op[2]);
252b5132 13407 expr1.X_add_number = 2;
c0ebe874 13408 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13409 macro_build (NULL, "break", "6", 7);
7d10b47d 13410 end_noreorder ();
c0ebe874 13411 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13412 break;
13413
13414 case M_DMUL:
13415 dbl = 1;
13416 case M_MUL:
c0ebe874
RS
13417 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13418 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13419 break;
252b5132
RH
13420
13421 case M_DSUBU_I:
13422 dbl = 1;
13423 goto do_subu;
13424 case M_SUBU_I:
13425 do_subu:
252b5132 13426 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13427 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,4", op[0], op[1]);
252b5132
RH
13428 break;
13429
13430 case M_SUBU_I_2:
252b5132 13431 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13432 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13433 break;
13434
13435 case M_DSUBU_I_2:
252b5132 13436 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13437 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13438 break;
13439
13440 case M_BEQ:
13441 s = "cmp";
13442 s2 = "bteqz";
13443 goto do_branch;
13444 case M_BNE:
13445 s = "cmp";
13446 s2 = "btnez";
13447 goto do_branch;
13448 case M_BLT:
13449 s = "slt";
13450 s2 = "btnez";
13451 goto do_branch;
13452 case M_BLTU:
13453 s = "sltu";
13454 s2 = "btnez";
13455 goto do_branch;
13456 case M_BLE:
13457 s = "slt";
13458 s2 = "bteqz";
13459 goto do_reverse_branch;
13460 case M_BLEU:
13461 s = "sltu";
13462 s2 = "bteqz";
13463 goto do_reverse_branch;
13464 case M_BGE:
13465 s = "slt";
13466 s2 = "bteqz";
13467 goto do_branch;
13468 case M_BGEU:
13469 s = "sltu";
13470 s2 = "bteqz";
13471 goto do_branch;
13472 case M_BGT:
13473 s = "slt";
13474 s2 = "btnez";
13475 goto do_reverse_branch;
13476 case M_BGTU:
13477 s = "sltu";
13478 s2 = "btnez";
13479
13480 do_reverse_branch:
c0ebe874
RS
13481 tmp = op[1];
13482 op[1] = op[0];
13483 op[0] = tmp;
252b5132
RH
13484
13485 do_branch:
c0ebe874 13486 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13487 macro_build (&offset_expr, s2, "p");
252b5132
RH
13488 break;
13489
13490 case M_BEQ_I:
13491 s = "cmpi";
13492 s2 = "bteqz";
13493 s3 = "x,U";
13494 goto do_branch_i;
13495 case M_BNE_I:
13496 s = "cmpi";
13497 s2 = "btnez";
13498 s3 = "x,U";
13499 goto do_branch_i;
13500 case M_BLT_I:
13501 s = "slti";
13502 s2 = "btnez";
13503 s3 = "x,8";
13504 goto do_branch_i;
13505 case M_BLTU_I:
13506 s = "sltiu";
13507 s2 = "btnez";
13508 s3 = "x,8";
13509 goto do_branch_i;
13510 case M_BLE_I:
13511 s = "slti";
13512 s2 = "btnez";
13513 s3 = "x,8";
13514 goto do_addone_branch_i;
13515 case M_BLEU_I:
13516 s = "sltiu";
13517 s2 = "btnez";
13518 s3 = "x,8";
13519 goto do_addone_branch_i;
13520 case M_BGE_I:
13521 s = "slti";
13522 s2 = "bteqz";
13523 s3 = "x,8";
13524 goto do_branch_i;
13525 case M_BGEU_I:
13526 s = "sltiu";
13527 s2 = "bteqz";
13528 s3 = "x,8";
13529 goto do_branch_i;
13530 case M_BGT_I:
13531 s = "slti";
13532 s2 = "bteqz";
13533 s3 = "x,8";
13534 goto do_addone_branch_i;
13535 case M_BGTU_I:
13536 s = "sltiu";
13537 s2 = "bteqz";
13538 s3 = "x,8";
13539
13540 do_addone_branch_i:
252b5132
RH
13541 ++imm_expr.X_add_number;
13542
13543 do_branch_i:
c0ebe874 13544 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13545 macro_build (&offset_expr, s2, "p");
252b5132
RH
13546 break;
13547
13548 case M_ABS:
13549 expr1.X_add_number = 0;
c0ebe874
RS
13550 macro_build (&expr1, "slti", "x,8", op[1]);
13551 if (op[0] != op[1])
13552 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13553 expr1.X_add_number = 2;
67c0d1eb 13554 macro_build (&expr1, "bteqz", "p");
c0ebe874 13555 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13556 break;
252b5132
RH
13557 }
13558}
13559
14daeee3
RS
13560/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13561 opcode bits in *OPCODE_EXTRA. */
13562
13563static struct mips_opcode *
13564mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13565 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13566{
13567 char *name, *dot, *p;
13568 unsigned int mask, suffix;
da8bca91 13569 ssize_t opend;
14daeee3
RS
13570 struct mips_opcode *insn;
13571
13572 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13573 name = xstrndup (start, length);
14daeee3
RS
13574
13575 /* Look up the instruction as-is. */
13576 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13577 if (insn)
e1fa0163 13578 goto end;
14daeee3
RS
13579
13580 dot = strchr (name, '.');
13581 if (dot && dot[1])
13582 {
13583 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13584 p = mips_parse_vu0_channels (dot + 1, &mask);
13585 if (*p == 0 && mask != 0)
13586 {
13587 *dot = 0;
13588 insn = (struct mips_opcode *) hash_find (hash, name);
13589 *dot = '.';
13590 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13591 {
13592 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13593 goto end;
14daeee3
RS
13594 }
13595 }
13596 }
13597
13598 if (mips_opts.micromips)
13599 {
13600 /* See if there's an instruction size override suffix,
13601 either `16' or `32', at the end of the mnemonic proper,
13602 that defines the operation, i.e. before the first `.'
13603 character if any. Strip it and retry. */
13604 opend = dot != NULL ? dot - name : length;
13605 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13606 suffix = 2;
13607 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13608 suffix = 4;
13609 else
13610 suffix = 0;
13611 if (suffix)
13612 {
13613 memcpy (name + opend - 2, name + opend, length - opend + 1);
13614 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13615 if (insn)
14daeee3
RS
13616 {
13617 forced_insn_length = suffix;
e1fa0163 13618 goto end;
14daeee3
RS
13619 }
13620 }
13621 }
13622
e1fa0163
NC
13623 insn = NULL;
13624 end:
13625 free (name);
13626 return insn;
14daeee3
RS
13627}
13628
77bd4346 13629/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
13630 is a macro, set imm_expr and offset_expr to the values associated
13631 with "I" and "A" operands respectively. Otherwise store the value
13632 of the relocatable field (if any) in offset_expr. In both cases
13633 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
13634
13635static void
60f20e8b 13636mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 13637{
60f20e8b 13638 const struct mips_opcode *first, *past;
df58fc94 13639 struct hash_control *hash;
a92713e6 13640 char format;
14daeee3 13641 size_t end;
a92713e6 13642 struct mips_operand_token *tokens;
14daeee3 13643 unsigned int opcode_extra;
252b5132 13644
df58fc94
RS
13645 if (mips_opts.micromips)
13646 {
13647 hash = micromips_op_hash;
13648 past = &micromips_opcodes[bfd_micromips_num_opcodes];
13649 }
13650 else
13651 {
13652 hash = op_hash;
13653 past = &mips_opcodes[NUMOPCODES];
13654 }
13655 forced_insn_length = 0;
14daeee3 13656 opcode_extra = 0;
252b5132 13657
df58fc94 13658 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
13659 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
13660 continue;
bdaaa2e1 13661
60f20e8b
RS
13662 first = mips_lookup_insn (hash, str, end, &opcode_extra);
13663 if (first == NULL)
252b5132 13664 {
1661c76c 13665 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 13666 return;
252b5132
RH
13667 }
13668
60f20e8b 13669 if (strcmp (first->name, "li.s") == 0)
a92713e6 13670 format = 'f';
60f20e8b 13671 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
13672 format = 'd';
13673 else
13674 format = 0;
13675 tokens = mips_parse_arguments (str + end, format);
13676 if (!tokens)
13677 return;
13678
60f20e8b
RS
13679 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
13680 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 13681 set_insn_error (0, _("invalid operands"));
df58fc94 13682
e3de51ce 13683 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13684}
13685
77bd4346
RS
13686/* As for mips_ip, but used when assembling MIPS16 code.
13687 Also set forced_insn_length to the resulting instruction size in
13688 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
13689
13690static void
60f20e8b 13691mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 13692{
1a00e612 13693 char *end, *s, c;
60f20e8b 13694 struct mips_opcode *first;
a92713e6 13695 struct mips_operand_token *tokens;
252b5132 13696
df58fc94 13697 forced_insn_length = 0;
252b5132 13698
3882b010 13699 for (s = str; ISLOWER (*s); ++s)
252b5132 13700 ;
1a00e612
RS
13701 end = s;
13702 c = *end;
13703 switch (c)
252b5132
RH
13704 {
13705 case '\0':
13706 break;
13707
13708 case ' ':
1a00e612 13709 s++;
252b5132
RH
13710 break;
13711
13712 case '.':
13713 if (s[1] == 't' && s[2] == ' ')
13714 {
df58fc94 13715 forced_insn_length = 2;
252b5132
RH
13716 s += 3;
13717 break;
13718 }
13719 else if (s[1] == 'e' && s[2] == ' ')
13720 {
df58fc94 13721 forced_insn_length = 4;
252b5132
RH
13722 s += 3;
13723 break;
13724 }
13725 /* Fall through. */
13726 default:
1661c76c 13727 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13728 return;
13729 }
13730
df58fc94
RS
13731 if (mips_opts.noautoextend && !forced_insn_length)
13732 forced_insn_length = 2;
252b5132 13733
1a00e612 13734 *end = 0;
60f20e8b 13735 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
13736 *end = c;
13737
60f20e8b 13738 if (!first)
252b5132 13739 {
1661c76c 13740 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
13741 return;
13742 }
13743
a92713e6
RS
13744 tokens = mips_parse_arguments (s, 0);
13745 if (!tokens)
13746 return;
13747
60f20e8b 13748 if (!match_mips16_insns (insn, first, tokens))
1661c76c 13749 set_insn_error (0, _("invalid operands"));
252b5132 13750
e3de51ce 13751 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
13752}
13753
b886a2ab
RS
13754/* Marshal immediate value VAL for an extended MIPS16 instruction.
13755 NBITS is the number of significant bits in VAL. */
13756
13757static unsigned long
13758mips16_immed_extend (offsetT val, unsigned int nbits)
13759{
13760 int extval;
13761 if (nbits == 16)
13762 {
13763 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
13764 val &= 0x1f;
13765 }
13766 else if (nbits == 15)
13767 {
13768 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
13769 val &= 0xf;
13770 }
13771 else
13772 {
13773 extval = ((val & 0x1f) << 6) | (val & 0x20);
13774 val = 0;
13775 }
13776 return (extval << 16) | val;
13777}
13778
3ccad066
RS
13779/* Like decode_mips16_operand, but require the operand to be defined and
13780 require it to be an integer. */
13781
13782static const struct mips_int_operand *
13783mips16_immed_operand (int type, bfd_boolean extended_p)
13784{
13785 const struct mips_operand *operand;
13786
13787 operand = decode_mips16_operand (type, extended_p);
13788 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
13789 abort ();
13790 return (const struct mips_int_operand *) operand;
13791}
13792
13793/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
13794
13795static bfd_boolean
13796mips16_immed_in_range_p (const struct mips_int_operand *operand,
13797 bfd_reloc_code_real_type reloc, offsetT sval)
13798{
13799 int min_val, max_val;
13800
13801 min_val = mips_int_operand_min (operand);
13802 max_val = mips_int_operand_max (operand);
13803 if (reloc != BFD_RELOC_UNUSED)
13804 {
13805 if (min_val < 0)
13806 sval = SEXT_16BIT (sval);
13807 else
13808 sval &= 0xffff;
13809 }
13810
13811 return (sval >= min_val
13812 && sval <= max_val
13813 && (sval & ((1 << operand->shift) - 1)) == 0);
13814}
13815
5c04167a
RS
13816/* Install immediate value VAL into MIPS16 instruction *INSN,
13817 extending it if necessary. The instruction in *INSN may
13818 already be extended.
13819
43c0598f
RS
13820 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
13821 if none. In the former case, VAL is a 16-bit number with no
13822 defined signedness.
13823
13824 TYPE is the type of the immediate field. USER_INSN_LENGTH
13825 is the length that the user requested, or 0 if none. */
252b5132
RH
13826
13827static void
3b4dbbbf 13828mips16_immed (const char *file, unsigned int line, int type,
43c0598f 13829 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 13830 unsigned int user_insn_length, unsigned long *insn)
252b5132 13831{
3ccad066
RS
13832 const struct mips_int_operand *operand;
13833 unsigned int uval, length;
252b5132 13834
3ccad066
RS
13835 operand = mips16_immed_operand (type, FALSE);
13836 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
13837 {
13838 /* We need an extended instruction. */
13839 if (user_insn_length == 2)
13840 as_bad_where (file, line, _("invalid unextended operand value"));
13841 else
13842 *insn |= MIPS16_EXTEND;
13843 }
13844 else if (user_insn_length == 4)
13845 {
13846 /* The operand doesn't force an unextended instruction to be extended.
13847 Warn if the user wanted an extended instruction anyway. */
13848 *insn |= MIPS16_EXTEND;
13849 as_warn_where (file, line,
13850 _("extended operand requested but not required"));
13851 }
252b5132 13852
3ccad066
RS
13853 length = mips16_opcode_length (*insn);
13854 if (length == 4)
252b5132 13855 {
3ccad066
RS
13856 operand = mips16_immed_operand (type, TRUE);
13857 if (!mips16_immed_in_range_p (operand, reloc, val))
13858 as_bad_where (file, line,
13859 _("operand value out of range for instruction"));
252b5132 13860 }
3ccad066
RS
13861 uval = ((unsigned int) val >> operand->shift) - operand->bias;
13862 if (length == 2)
13863 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 13864 else
3ccad066 13865 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
13866}
13867\f
d6f16593 13868struct percent_op_match
ad8d3bb3 13869{
5e0116d5
RS
13870 const char *str;
13871 bfd_reloc_code_real_type reloc;
d6f16593
MR
13872};
13873
13874static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 13875{
5e0116d5 13876 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
13877 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
13878 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
13879 {"%call16", BFD_RELOC_MIPS_CALL16},
13880 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
13881 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
13882 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
13883 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
13884 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
13885 {"%got", BFD_RELOC_MIPS_GOT16},
13886 {"%gp_rel", BFD_RELOC_GPREL16},
13887 {"%half", BFD_RELOC_16},
13888 {"%highest", BFD_RELOC_MIPS_HIGHEST},
13889 {"%higher", BFD_RELOC_MIPS_HIGHER},
13890 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
13891 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
13892 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
13893 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
13894 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
13895 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
13896 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
13897 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
13898 {"%hi", BFD_RELOC_HI16_S},
13899 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
13900 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
13901};
13902
d6f16593
MR
13903static const struct percent_op_match mips16_percent_op[] =
13904{
13905 {"%lo", BFD_RELOC_MIPS16_LO16},
13906 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
13907 {"%got", BFD_RELOC_MIPS16_GOT16},
13908 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
13909 {"%hi", BFD_RELOC_MIPS16_HI16_S},
13910 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
13911 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
13912 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
13913 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
13914 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
13915 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
13916 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
13917};
13918
252b5132 13919
5e0116d5
RS
13920/* Return true if *STR points to a relocation operator. When returning true,
13921 move *STR over the operator and store its relocation code in *RELOC.
13922 Leave both *STR and *RELOC alone when returning false. */
13923
13924static bfd_boolean
17a2f251 13925parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 13926{
d6f16593
MR
13927 const struct percent_op_match *percent_op;
13928 size_t limit, i;
13929
13930 if (mips_opts.mips16)
13931 {
13932 percent_op = mips16_percent_op;
13933 limit = ARRAY_SIZE (mips16_percent_op);
13934 }
13935 else
13936 {
13937 percent_op = mips_percent_op;
13938 limit = ARRAY_SIZE (mips_percent_op);
13939 }
76b3015f 13940
d6f16593 13941 for (i = 0; i < limit; i++)
5e0116d5 13942 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 13943 {
3f98094e
DJ
13944 int len = strlen (percent_op[i].str);
13945
13946 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
13947 continue;
13948
5e0116d5
RS
13949 *str += strlen (percent_op[i].str);
13950 *reloc = percent_op[i].reloc;
394f9b3a 13951
5e0116d5
RS
13952 /* Check whether the output BFD supports this relocation.
13953 If not, issue an error and fall back on something safe. */
13954 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 13955 {
20203fb9 13956 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 13957 percent_op[i].str);
01a3f561 13958 *reloc = BFD_RELOC_UNUSED;
394f9b3a 13959 }
5e0116d5 13960 return TRUE;
394f9b3a 13961 }
5e0116d5 13962 return FALSE;
394f9b3a 13963}
ad8d3bb3 13964
ad8d3bb3 13965
5e0116d5
RS
13966/* Parse string STR as a 16-bit relocatable operand. Store the
13967 expression in *EP and the relocations in the array starting
13968 at RELOC. Return the number of relocation operators used.
ad8d3bb3 13969
01a3f561 13970 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 13971
5e0116d5 13972static size_t
17a2f251
TS
13973my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
13974 char *str)
ad8d3bb3 13975{
5e0116d5
RS
13976 bfd_reloc_code_real_type reversed_reloc[3];
13977 size_t reloc_index, i;
09b8f35a
RS
13978 int crux_depth, str_depth;
13979 char *crux;
5e0116d5
RS
13980
13981 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
13982 in REVERSED_RELOC. End the loop with CRUX pointing to the start
13983 of the main expression and with CRUX_DEPTH containing the number
13984 of open brackets at that point. */
13985 reloc_index = -1;
13986 str_depth = 0;
13987 do
fb1b3232 13988 {
09b8f35a
RS
13989 reloc_index++;
13990 crux = str;
13991 crux_depth = str_depth;
13992
13993 /* Skip over whitespace and brackets, keeping count of the number
13994 of brackets. */
13995 while (*str == ' ' || *str == '\t' || *str == '(')
13996 if (*str++ == '(')
13997 str_depth++;
5e0116d5 13998 }
09b8f35a
RS
13999 while (*str == '%'
14000 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14001 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14002
09b8f35a 14003 my_getExpression (ep, crux);
5e0116d5 14004 str = expr_end;
394f9b3a 14005
5e0116d5 14006 /* Match every open bracket. */
09b8f35a 14007 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14008 if (*str++ == ')')
09b8f35a 14009 crux_depth--;
394f9b3a 14010
09b8f35a 14011 if (crux_depth > 0)
20203fb9 14012 as_bad (_("unclosed '('"));
394f9b3a 14013
5e0116d5 14014 expr_end = str;
252b5132 14015
01a3f561 14016 if (reloc_index != 0)
64bdfcaf
RS
14017 {
14018 prev_reloc_op_frag = frag_now;
14019 for (i = 0; i < reloc_index; i++)
14020 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14021 }
fb1b3232 14022
5e0116d5 14023 return reloc_index;
252b5132
RH
14024}
14025
14026static void
17a2f251 14027my_getExpression (expressionS *ep, char *str)
252b5132
RH
14028{
14029 char *save_in;
14030
14031 save_in = input_line_pointer;
14032 input_line_pointer = str;
14033 expression (ep);
14034 expr_end = input_line_pointer;
14035 input_line_pointer = save_in;
252b5132
RH
14036}
14037
6d4af3c2 14038const char *
17a2f251 14039md_atof (int type, char *litP, int *sizeP)
252b5132 14040{
499ac353 14041 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14042}
14043
14044void
17a2f251 14045md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14046{
14047 if (target_big_endian)
14048 number_to_chars_bigendian (buf, val, n);
14049 else
14050 number_to_chars_littleendian (buf, val, n);
14051}
14052\f
e013f690
TS
14053static int support_64bit_objects(void)
14054{
14055 const char **list, **l;
aa3d8fdf 14056 int yes;
e013f690
TS
14057
14058 list = bfd_target_list ();
14059 for (l = list; *l != NULL; l++)
aeffff67
RS
14060 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14061 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14062 break;
aa3d8fdf 14063 yes = (*l != NULL);
e013f690 14064 free (list);
aa3d8fdf 14065 return yes;
e013f690
TS
14066}
14067
316f5878
RS
14068/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14069 NEW_VALUE. Warn if another value was already specified. Note:
14070 we have to defer parsing the -march and -mtune arguments in order
14071 to handle 'from-abi' correctly, since the ABI might be specified
14072 in a later argument. */
14073
14074static void
17a2f251 14075mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14076{
14077 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14078 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14079 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14080 new_value);
14081
14082 *string_ptr = new_value;
14083}
14084
252b5132 14085int
17b9d67d 14086md_parse_option (int c, const char *arg)
252b5132 14087{
c6278170
RS
14088 unsigned int i;
14089
14090 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14091 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14092 {
919731af 14093 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14094 c == mips_ases[i].option_on);
14095 return 1;
14096 }
14097
252b5132
RH
14098 switch (c)
14099 {
119d663a
NC
14100 case OPTION_CONSTRUCT_FLOATS:
14101 mips_disable_float_construction = 0;
14102 break;
bdaaa2e1 14103
119d663a
NC
14104 case OPTION_NO_CONSTRUCT_FLOATS:
14105 mips_disable_float_construction = 1;
14106 break;
bdaaa2e1 14107
252b5132
RH
14108 case OPTION_TRAP:
14109 mips_trap = 1;
14110 break;
14111
14112 case OPTION_BREAK:
14113 mips_trap = 0;
14114 break;
14115
14116 case OPTION_EB:
14117 target_big_endian = 1;
14118 break;
14119
14120 case OPTION_EL:
14121 target_big_endian = 0;
14122 break;
14123
14124 case 'O':
4ffff32f
TS
14125 if (arg == NULL)
14126 mips_optimize = 1;
14127 else if (arg[0] == '0')
14128 mips_optimize = 0;
14129 else if (arg[0] == '1')
252b5132
RH
14130 mips_optimize = 1;
14131 else
14132 mips_optimize = 2;
14133 break;
14134
14135 case 'g':
14136 if (arg == NULL)
14137 mips_debug = 2;
14138 else
14139 mips_debug = atoi (arg);
252b5132
RH
14140 break;
14141
14142 case OPTION_MIPS1:
0b35dfee 14143 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14144 break;
14145
14146 case OPTION_MIPS2:
0b35dfee 14147 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14148 break;
14149
14150 case OPTION_MIPS3:
0b35dfee 14151 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14152 break;
14153
14154 case OPTION_MIPS4:
0b35dfee 14155 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14156 break;
14157
84ea6cf2 14158 case OPTION_MIPS5:
0b35dfee 14159 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14160 break;
14161
e7af610e 14162 case OPTION_MIPS32:
0b35dfee 14163 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14164 break;
14165
af7ee8bf 14166 case OPTION_MIPS32R2:
0b35dfee 14167 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14168 break;
14169
ae52f483 14170 case OPTION_MIPS32R3:
0ae19f05 14171 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14172 break;
14173
14174 case OPTION_MIPS32R5:
0ae19f05 14175 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14176 break;
14177
7361da2c
AB
14178 case OPTION_MIPS32R6:
14179 file_mips_opts.isa = ISA_MIPS32R6;
14180 break;
14181
5f74bc13 14182 case OPTION_MIPS64R2:
0b35dfee 14183 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14184 break;
14185
ae52f483 14186 case OPTION_MIPS64R3:
0ae19f05 14187 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14188 break;
14189
14190 case OPTION_MIPS64R5:
0ae19f05 14191 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14192 break;
14193
7361da2c
AB
14194 case OPTION_MIPS64R6:
14195 file_mips_opts.isa = ISA_MIPS64R6;
14196 break;
14197
84ea6cf2 14198 case OPTION_MIPS64:
0b35dfee 14199 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14200 break;
14201
ec68c924 14202 case OPTION_MTUNE:
316f5878
RS
14203 mips_set_option_string (&mips_tune_string, arg);
14204 break;
ec68c924 14205
316f5878
RS
14206 case OPTION_MARCH:
14207 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14208 break;
14209
14210 case OPTION_M4650:
316f5878
RS
14211 mips_set_option_string (&mips_arch_string, "4650");
14212 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14213 break;
14214
14215 case OPTION_NO_M4650:
14216 break;
14217
14218 case OPTION_M4010:
316f5878
RS
14219 mips_set_option_string (&mips_arch_string, "4010");
14220 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14221 break;
14222
14223 case OPTION_NO_M4010:
14224 break;
14225
14226 case OPTION_M4100:
316f5878
RS
14227 mips_set_option_string (&mips_arch_string, "4100");
14228 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14229 break;
14230
14231 case OPTION_NO_M4100:
14232 break;
14233
252b5132 14234 case OPTION_M3900:
316f5878
RS
14235 mips_set_option_string (&mips_arch_string, "3900");
14236 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14237 break;
bdaaa2e1 14238
252b5132
RH
14239 case OPTION_NO_M3900:
14240 break;
14241
df58fc94 14242 case OPTION_MICROMIPS:
919731af 14243 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14244 {
14245 as_bad (_("-mmicromips cannot be used with -mips16"));
14246 return 0;
14247 }
919731af 14248 file_mips_opts.micromips = 1;
df58fc94
RS
14249 mips_no_prev_insn ();
14250 break;
14251
14252 case OPTION_NO_MICROMIPS:
919731af 14253 file_mips_opts.micromips = 0;
df58fc94
RS
14254 mips_no_prev_insn ();
14255 break;
14256
252b5132 14257 case OPTION_MIPS16:
919731af 14258 if (file_mips_opts.micromips == 1)
df58fc94
RS
14259 {
14260 as_bad (_("-mips16 cannot be used with -micromips"));
14261 return 0;
14262 }
919731af 14263 file_mips_opts.mips16 = 1;
7d10b47d 14264 mips_no_prev_insn ();
252b5132
RH
14265 break;
14266
14267 case OPTION_NO_MIPS16:
919731af 14268 file_mips_opts.mips16 = 0;
7d10b47d 14269 mips_no_prev_insn ();
252b5132
RH
14270 break;
14271
6a32d874
CM
14272 case OPTION_FIX_24K:
14273 mips_fix_24k = 1;
14274 break;
14275
14276 case OPTION_NO_FIX_24K:
14277 mips_fix_24k = 0;
14278 break;
14279
a8d14a88
CM
14280 case OPTION_FIX_RM7000:
14281 mips_fix_rm7000 = 1;
14282 break;
14283
14284 case OPTION_NO_FIX_RM7000:
14285 mips_fix_rm7000 = 0;
14286 break;
14287
c67a084a
NC
14288 case OPTION_FIX_LOONGSON2F_JUMP:
14289 mips_fix_loongson2f_jump = TRUE;
14290 break;
14291
14292 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14293 mips_fix_loongson2f_jump = FALSE;
14294 break;
14295
14296 case OPTION_FIX_LOONGSON2F_NOP:
14297 mips_fix_loongson2f_nop = TRUE;
14298 break;
14299
14300 case OPTION_NO_FIX_LOONGSON2F_NOP:
14301 mips_fix_loongson2f_nop = FALSE;
14302 break;
14303
d766e8ec
RS
14304 case OPTION_FIX_VR4120:
14305 mips_fix_vr4120 = 1;
60b63b72
RS
14306 break;
14307
d766e8ec
RS
14308 case OPTION_NO_FIX_VR4120:
14309 mips_fix_vr4120 = 0;
60b63b72
RS
14310 break;
14311
7d8e00cf
RS
14312 case OPTION_FIX_VR4130:
14313 mips_fix_vr4130 = 1;
14314 break;
14315
14316 case OPTION_NO_FIX_VR4130:
14317 mips_fix_vr4130 = 0;
14318 break;
14319
d954098f
DD
14320 case OPTION_FIX_CN63XXP1:
14321 mips_fix_cn63xxp1 = TRUE;
14322 break;
14323
14324 case OPTION_NO_FIX_CN63XXP1:
14325 mips_fix_cn63xxp1 = FALSE;
14326 break;
14327
4a6a3df4
AO
14328 case OPTION_RELAX_BRANCH:
14329 mips_relax_branch = 1;
14330 break;
14331
14332 case OPTION_NO_RELAX_BRANCH:
14333 mips_relax_branch = 0;
14334 break;
14335
833794fc 14336 case OPTION_INSN32:
919731af 14337 file_mips_opts.insn32 = TRUE;
833794fc
MR
14338 break;
14339
14340 case OPTION_NO_INSN32:
919731af 14341 file_mips_opts.insn32 = FALSE;
833794fc
MR
14342 break;
14343
aa6975fb
ILT
14344 case OPTION_MSHARED:
14345 mips_in_shared = TRUE;
14346 break;
14347
14348 case OPTION_MNO_SHARED:
14349 mips_in_shared = FALSE;
14350 break;
14351
aed1a261 14352 case OPTION_MSYM32:
919731af 14353 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14354 break;
14355
14356 case OPTION_MNO_SYM32:
919731af 14357 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14358 break;
14359
252b5132
RH
14360 /* When generating ELF code, we permit -KPIC and -call_shared to
14361 select SVR4_PIC, and -non_shared to select no PIC. This is
14362 intended to be compatible with Irix 5. */
14363 case OPTION_CALL_SHARED:
252b5132 14364 mips_pic = SVR4_PIC;
143d77c5 14365 mips_abicalls = TRUE;
252b5132
RH
14366 break;
14367
861fb55a 14368 case OPTION_CALL_NONPIC:
861fb55a
DJ
14369 mips_pic = NO_PIC;
14370 mips_abicalls = TRUE;
14371 break;
14372
252b5132 14373 case OPTION_NON_SHARED:
252b5132 14374 mips_pic = NO_PIC;
143d77c5 14375 mips_abicalls = FALSE;
252b5132
RH
14376 break;
14377
44075ae2
TS
14378 /* The -xgot option tells the assembler to use 32 bit offsets
14379 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14380 compatibility. */
14381 case OPTION_XGOT:
14382 mips_big_got = 1;
14383 break;
14384
14385 case 'G':
6caf9ef4
TS
14386 g_switch_value = atoi (arg);
14387 g_switch_seen = 1;
252b5132
RH
14388 break;
14389
34ba82a8
TS
14390 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14391 and -mabi=64. */
252b5132 14392 case OPTION_32:
f3ded42a 14393 mips_abi = O32_ABI;
252b5132
RH
14394 break;
14395
e013f690 14396 case OPTION_N32:
316f5878 14397 mips_abi = N32_ABI;
e013f690 14398 break;
252b5132 14399
e013f690 14400 case OPTION_64:
316f5878 14401 mips_abi = N64_ABI;
f43abd2b 14402 if (!support_64bit_objects())
1661c76c 14403 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14404 break;
14405
c97ef257 14406 case OPTION_GP32:
bad1aba3 14407 file_mips_opts.gp = 32;
c97ef257
AH
14408 break;
14409
14410 case OPTION_GP64:
bad1aba3 14411 file_mips_opts.gp = 64;
c97ef257 14412 break;
252b5132 14413
ca4e0257 14414 case OPTION_FP32:
0b35dfee 14415 file_mips_opts.fp = 32;
316f5878
RS
14416 break;
14417
351cdf24
MF
14418 case OPTION_FPXX:
14419 file_mips_opts.fp = 0;
14420 break;
14421
316f5878 14422 case OPTION_FP64:
0b35dfee 14423 file_mips_opts.fp = 64;
ca4e0257
RS
14424 break;
14425
351cdf24
MF
14426 case OPTION_ODD_SPREG:
14427 file_mips_opts.oddspreg = 1;
14428 break;
14429
14430 case OPTION_NO_ODD_SPREG:
14431 file_mips_opts.oddspreg = 0;
14432 break;
14433
037b32b9 14434 case OPTION_SINGLE_FLOAT:
0b35dfee 14435 file_mips_opts.single_float = 1;
037b32b9
AN
14436 break;
14437
14438 case OPTION_DOUBLE_FLOAT:
0b35dfee 14439 file_mips_opts.single_float = 0;
037b32b9
AN
14440 break;
14441
14442 case OPTION_SOFT_FLOAT:
0b35dfee 14443 file_mips_opts.soft_float = 1;
037b32b9
AN
14444 break;
14445
14446 case OPTION_HARD_FLOAT:
0b35dfee 14447 file_mips_opts.soft_float = 0;
037b32b9
AN
14448 break;
14449
252b5132 14450 case OPTION_MABI:
e013f690 14451 if (strcmp (arg, "32") == 0)
316f5878 14452 mips_abi = O32_ABI;
e013f690 14453 else if (strcmp (arg, "o64") == 0)
316f5878 14454 mips_abi = O64_ABI;
e013f690 14455 else if (strcmp (arg, "n32") == 0)
316f5878 14456 mips_abi = N32_ABI;
e013f690
TS
14457 else if (strcmp (arg, "64") == 0)
14458 {
316f5878 14459 mips_abi = N64_ABI;
e013f690 14460 if (! support_64bit_objects())
1661c76c 14461 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14462 "format"));
14463 }
14464 else if (strcmp (arg, "eabi") == 0)
316f5878 14465 mips_abi = EABI_ABI;
e013f690 14466 else
da0e507f
TS
14467 {
14468 as_fatal (_("invalid abi -mabi=%s"), arg);
14469 return 0;
14470 }
252b5132
RH
14471 break;
14472
6b76fefe 14473 case OPTION_M7000_HILO_FIX:
b34976b6 14474 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14475 break;
14476
9ee72ff1 14477 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14478 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14479 break;
14480
ecb4347a 14481 case OPTION_MDEBUG:
b34976b6 14482 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14483 break;
14484
14485 case OPTION_NO_MDEBUG:
b34976b6 14486 mips_flag_mdebug = FALSE;
ecb4347a 14487 break;
dcd410fe
RO
14488
14489 case OPTION_PDR:
14490 mips_flag_pdr = TRUE;
14491 break;
14492
14493 case OPTION_NO_PDR:
14494 mips_flag_pdr = FALSE;
14495 break;
0a44bf69
RS
14496
14497 case OPTION_MVXWORKS_PIC:
14498 mips_pic = VXWORKS_PIC;
14499 break;
ecb4347a 14500
ba92f887
MR
14501 case OPTION_NAN:
14502 if (strcmp (arg, "2008") == 0)
7361da2c 14503 mips_nan2008 = 1;
ba92f887 14504 else if (strcmp (arg, "legacy") == 0)
7361da2c 14505 mips_nan2008 = 0;
ba92f887
MR
14506 else
14507 {
1661c76c 14508 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14509 return 0;
14510 }
14511 break;
14512
252b5132
RH
14513 default:
14514 return 0;
14515 }
14516
c67a084a
NC
14517 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14518
252b5132
RH
14519 return 1;
14520}
316f5878 14521\f
919731af 14522/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14523
316f5878 14524static void
17a2f251 14525mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14526{
14527 if (info != 0)
fef14a42 14528 mips_tune = info->cpu;
316f5878 14529}
80cc45a5 14530
34ba82a8 14531
252b5132 14532void
17a2f251 14533mips_after_parse_args (void)
e9670677 14534{
fef14a42
TS
14535 const struct mips_cpu_info *arch_info = 0;
14536 const struct mips_cpu_info *tune_info = 0;
14537
e9670677 14538 /* GP relative stuff not working for PE */
6caf9ef4 14539 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14540 {
6caf9ef4 14541 if (g_switch_seen && g_switch_value != 0)
1661c76c 14542 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14543 g_switch_value = 0;
14544 }
14545
cac012d6
AO
14546 if (mips_abi == NO_ABI)
14547 mips_abi = MIPS_DEFAULT_ABI;
14548
919731af 14549 /* The following code determines the architecture.
22923709
RS
14550 Similar code was added to GCC 3.3 (see override_options() in
14551 config/mips/mips.c). The GAS and GCC code should be kept in sync
14552 as much as possible. */
e9670677 14553
316f5878 14554 if (mips_arch_string != 0)
fef14a42 14555 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14556
0b35dfee 14557 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14558 {
0b35dfee 14559 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14560 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14561 the -march selection (if any). */
fef14a42 14562 if (arch_info != 0)
e9670677 14563 {
316f5878
RS
14564 /* -march takes precedence over -mipsN, since it is more descriptive.
14565 There's no harm in specifying both as long as the ISA levels
14566 are the same. */
0b35dfee 14567 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14568 as_bad (_("-%s conflicts with the other architecture options,"
14569 " which imply -%s"),
0b35dfee 14570 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14571 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14572 }
316f5878 14573 else
0b35dfee 14574 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14575 }
14576
fef14a42 14577 if (arch_info == 0)
95bfe26e
MF
14578 {
14579 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14580 gas_assert (arch_info);
14581 }
e9670677 14582
fef14a42 14583 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14584 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14585 arch_info->name);
14586
919731af 14587 file_mips_opts.arch = arch_info->cpu;
14588 file_mips_opts.isa = arch_info->isa;
14589
14590 /* Set up initial mips_opts state. */
14591 mips_opts = file_mips_opts;
14592
14593 /* The register size inference code is now placed in
14594 file_mips_check_options. */
fef14a42 14595
0b35dfee 14596 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14597 processor. */
fef14a42
TS
14598 if (mips_tune_string != 0)
14599 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14600
fef14a42
TS
14601 if (tune_info == 0)
14602 mips_set_tune (arch_info);
14603 else
14604 mips_set_tune (tune_info);
e9670677 14605
ecb4347a 14606 if (mips_flag_mdebug < 0)
e8044f35 14607 mips_flag_mdebug = 0;
e9670677
MR
14608}
14609\f
14610void
17a2f251 14611mips_init_after_args (void)
252b5132
RH
14612{
14613 /* initialize opcodes */
14614 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 14615 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
14616}
14617
14618long
17a2f251 14619md_pcrel_from (fixS *fixP)
252b5132 14620{
a7ebbfdf
TS
14621 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
14622 switch (fixP->fx_r_type)
14623 {
df58fc94
RS
14624 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14625 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14626 /* Return the address of the delay slot. */
14627 return addr + 2;
14628
14629 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14630 case BFD_RELOC_MICROMIPS_JMP:
a7ebbfdf 14631 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
14632 case BFD_RELOC_MIPS_21_PCREL_S2:
14633 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
14634 case BFD_RELOC_MIPS_JMP:
14635 /* Return the address of the delay slot. */
14636 return addr + 4;
df58fc94 14637
51f6035b
MR
14638 case BFD_RELOC_MIPS_18_PCREL_S3:
14639 /* Return the aligned address of the doubleword containing
14640 the instruction. */
14641 return addr & ~7;
14642
a7ebbfdf
TS
14643 default:
14644 return addr;
14645 }
252b5132
RH
14646}
14647
252b5132
RH
14648/* This is called before the symbol table is processed. In order to
14649 work with gcc when using mips-tfile, we must keep all local labels.
14650 However, in other cases, we want to discard them. If we were
14651 called with -g, but we didn't see any debugging information, it may
14652 mean that gcc is smuggling debugging information through to
14653 mips-tfile, in which case we must generate all local labels. */
14654
14655void
17a2f251 14656mips_frob_file_before_adjust (void)
252b5132
RH
14657{
14658#ifndef NO_ECOFF_DEBUGGING
14659 if (ECOFF_DEBUGGING
14660 && mips_debug != 0
14661 && ! ecoff_debugging_seen)
14662 flag_keep_locals = 1;
14663#endif
14664}
14665
3b91255e 14666/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 14667 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
14668 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
14669 relocation operators.
14670
14671 For our purposes, a %lo() expression matches a %got() or %hi()
14672 expression if:
14673
14674 (a) it refers to the same symbol; and
14675 (b) the offset applied in the %lo() expression is no lower than
14676 the offset applied in the %got() or %hi().
14677
14678 (b) allows us to cope with code like:
14679
14680 lui $4,%hi(foo)
14681 lh $4,%lo(foo+2)($4)
14682
14683 ...which is legal on RELA targets, and has a well-defined behaviour
14684 if the user knows that adding 2 to "foo" will not induce a carry to
14685 the high 16 bits.
14686
14687 When several %lo()s match a particular %got() or %hi(), we use the
14688 following rules to distinguish them:
14689
14690 (1) %lo()s with smaller offsets are a better match than %lo()s with
14691 higher offsets.
14692
14693 (2) %lo()s with no matching %got() or %hi() are better than those
14694 that already have a matching %got() or %hi().
14695
14696 (3) later %lo()s are better than earlier %lo()s.
14697
14698 These rules are applied in order.
14699
14700 (1) means, among other things, that %lo()s with identical offsets are
14701 chosen if they exist.
14702
14703 (2) means that we won't associate several high-part relocations with
14704 the same low-part relocation unless there's no alternative. Having
14705 several high parts for the same low part is a GNU extension; this rule
14706 allows careful users to avoid it.
14707
14708 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
14709 with the last high-part relocation being at the front of the list.
14710 It therefore makes sense to choose the last matching low-part
14711 relocation, all other things being equal. It's also easier
14712 to code that way. */
252b5132
RH
14713
14714void
17a2f251 14715mips_frob_file (void)
252b5132
RH
14716{
14717 struct mips_hi_fixup *l;
35903be0 14718 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
14719
14720 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
14721 {
14722 segment_info_type *seginfo;
3b91255e
RS
14723 bfd_boolean matched_lo_p;
14724 fixS **hi_pos, **lo_pos, **pos;
252b5132 14725
9c2799c2 14726 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 14727
5919d012 14728 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
14729 there isn't supposed to be a matching LO. Ignore %gots against
14730 constants; we'll report an error for those later. */
738e5348 14731 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab
RS
14732 && !(l->fixp->fx_addsy
14733 && pic_need_relax (l->fixp->fx_addsy, l->seg)))
5919d012
RS
14734 continue;
14735
14736 /* Check quickly whether the next fixup happens to be a matching %lo. */
14737 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
14738 continue;
14739
252b5132 14740 seginfo = seg_info (l->seg);
252b5132 14741
3b91255e
RS
14742 /* Set HI_POS to the position of this relocation in the chain.
14743 Set LO_POS to the position of the chosen low-part relocation.
14744 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
14745 relocation that matches an immediately-preceding high-part
14746 relocation. */
14747 hi_pos = NULL;
14748 lo_pos = NULL;
14749 matched_lo_p = FALSE;
738e5348 14750 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 14751
3b91255e
RS
14752 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
14753 {
14754 if (*pos == l->fixp)
14755 hi_pos = pos;
14756
35903be0 14757 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 14758 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
14759 && (*pos)->fx_offset >= l->fixp->fx_offset
14760 && (lo_pos == NULL
14761 || (*pos)->fx_offset < (*lo_pos)->fx_offset
14762 || (!matched_lo_p
14763 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
14764 lo_pos = pos;
14765
14766 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
14767 && fixup_has_matching_lo_p (*pos));
14768 }
14769
14770 /* If we found a match, remove the high-part relocation from its
14771 current position and insert it before the low-part relocation.
14772 Make the offsets match so that fixup_has_matching_lo_p()
14773 will return true.
14774
14775 We don't warn about unmatched high-part relocations since some
14776 versions of gcc have been known to emit dead "lui ...%hi(...)"
14777 instructions. */
14778 if (lo_pos != NULL)
14779 {
14780 l->fixp->fx_offset = (*lo_pos)->fx_offset;
14781 if (l->fixp->fx_next != *lo_pos)
252b5132 14782 {
3b91255e
RS
14783 *hi_pos = l->fixp->fx_next;
14784 l->fixp->fx_next = *lo_pos;
14785 *lo_pos = l->fixp;
252b5132 14786 }
252b5132
RH
14787 }
14788 }
14789}
14790
252b5132 14791int
17a2f251 14792mips_force_relocation (fixS *fixp)
252b5132 14793{
ae6063d4 14794 if (generic_force_reloc (fixp))
252b5132
RH
14795 return 1;
14796
df58fc94
RS
14797 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
14798 so that the linker relaxation can update targets. */
14799 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
14800 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
14801 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
14802 return 1;
14803
7361da2c 14804 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 14805 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
14806 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
14807 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
14808 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
14809 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
14810 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
14811 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
14812 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
14813 return 1;
14814
3e722fb5 14815 return 0;
252b5132
RH
14816}
14817
b886a2ab
RS
14818/* Read the instruction associated with RELOC from BUF. */
14819
14820static unsigned int
14821read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
14822{
14823 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14824 return read_compressed_insn (buf, 4);
14825 else
14826 return read_insn (buf);
14827}
14828
14829/* Write instruction INSN to BUF, given that it has been relocated
14830 by RELOC. */
14831
14832static void
14833write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
14834 unsigned long insn)
14835{
14836 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
14837 write_compressed_insn (buf, insn, 4);
14838 else
14839 write_insn (buf, insn);
14840}
14841
252b5132
RH
14842/* Apply a fixup to the object file. */
14843
94f592af 14844void
55cf6793 14845md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 14846{
4d68580a 14847 char *buf;
b886a2ab 14848 unsigned long insn;
a7ebbfdf 14849 reloc_howto_type *howto;
252b5132 14850
d56a8dda
RS
14851 if (fixP->fx_pcrel)
14852 switch (fixP->fx_r_type)
14853 {
14854 case BFD_RELOC_16_PCREL_S2:
14855 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
14856 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
14857 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
14858 case BFD_RELOC_32_PCREL:
7361da2c
AB
14859 case BFD_RELOC_MIPS_21_PCREL_S2:
14860 case BFD_RELOC_MIPS_26_PCREL_S2:
14861 case BFD_RELOC_MIPS_18_PCREL_S3:
14862 case BFD_RELOC_MIPS_19_PCREL_S2:
14863 case BFD_RELOC_HI16_S_PCREL:
14864 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
14865 break;
14866
14867 case BFD_RELOC_32:
14868 fixP->fx_r_type = BFD_RELOC_32_PCREL;
14869 break;
14870
14871 default:
14872 as_bad_where (fixP->fx_file, fixP->fx_line,
14873 _("PC-relative reference to a different section"));
14874 break;
14875 }
14876
14877 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
14878 that have no MIPS ELF equivalent. */
14879 if (fixP->fx_r_type != BFD_RELOC_8)
14880 {
14881 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
14882 if (!howto)
14883 return;
14884 }
65551fa4 14885
df58fc94
RS
14886 gas_assert (fixP->fx_size == 2
14887 || fixP->fx_size == 4
d56a8dda 14888 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
14889 || fixP->fx_r_type == BFD_RELOC_16
14890 || fixP->fx_r_type == BFD_RELOC_64
14891 || fixP->fx_r_type == BFD_RELOC_CTOR
14892 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 14893 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
14894 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
14895 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
14896 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
14897 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 14898
4d68580a 14899 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 14900
b1dca8ee
RS
14901 /* Don't treat parts of a composite relocation as done. There are two
14902 reasons for this:
14903
14904 (1) The second and third parts will be against 0 (RSS_UNDEF) but
14905 should nevertheless be emitted if the first part is.
14906
14907 (2) In normal usage, composite relocations are never assembly-time
14908 constants. The easiest way of dealing with the pathological
14909 exceptions is to generate a relocation against STN_UNDEF and
14910 leave everything up to the linker. */
3994f87e 14911 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
14912 fixP->fx_done = 1;
14913
14914 switch (fixP->fx_r_type)
14915 {
3f98094e
DJ
14916 case BFD_RELOC_MIPS_TLS_GD:
14917 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
14918 case BFD_RELOC_MIPS_TLS_DTPREL32:
14919 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
14920 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
14921 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
14922 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
14923 case BFD_RELOC_MIPS_TLS_TPREL32:
14924 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
14925 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
14926 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
14927 case BFD_RELOC_MICROMIPS_TLS_GD:
14928 case BFD_RELOC_MICROMIPS_TLS_LDM:
14929 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
14930 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
14931 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
14932 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
14933 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
14934 case BFD_RELOC_MIPS16_TLS_GD:
14935 case BFD_RELOC_MIPS16_TLS_LDM:
14936 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
14937 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
14938 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
14939 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
14940 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
14941 if (fixP->fx_addsy)
14942 S_SET_THREAD_LOCAL (fixP->fx_addsy);
14943 else
14944 as_bad_where (fixP->fx_file, fixP->fx_line,
14945 _("TLS relocation against a constant"));
14946 break;
3f98094e 14947
252b5132 14948 case BFD_RELOC_MIPS_JMP:
e369bcce
TS
14949 case BFD_RELOC_MIPS_SHIFT5:
14950 case BFD_RELOC_MIPS_SHIFT6:
14951 case BFD_RELOC_MIPS_GOT_DISP:
14952 case BFD_RELOC_MIPS_GOT_PAGE:
14953 case BFD_RELOC_MIPS_GOT_OFST:
14954 case BFD_RELOC_MIPS_SUB:
14955 case BFD_RELOC_MIPS_INSERT_A:
14956 case BFD_RELOC_MIPS_INSERT_B:
14957 case BFD_RELOC_MIPS_DELETE:
14958 case BFD_RELOC_MIPS_HIGHEST:
14959 case BFD_RELOC_MIPS_HIGHER:
14960 case BFD_RELOC_MIPS_SCN_DISP:
14961 case BFD_RELOC_MIPS_REL16:
14962 case BFD_RELOC_MIPS_RELGOT:
14963 case BFD_RELOC_MIPS_JALR:
252b5132
RH
14964 case BFD_RELOC_HI16:
14965 case BFD_RELOC_HI16_S:
b886a2ab 14966 case BFD_RELOC_LO16:
cdf6fd85 14967 case BFD_RELOC_GPREL16:
252b5132
RH
14968 case BFD_RELOC_MIPS_LITERAL:
14969 case BFD_RELOC_MIPS_CALL16:
14970 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 14971 case BFD_RELOC_GPREL32:
252b5132
RH
14972 case BFD_RELOC_MIPS_GOT_HI16:
14973 case BFD_RELOC_MIPS_GOT_LO16:
14974 case BFD_RELOC_MIPS_CALL_HI16:
14975 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
14976 case BFD_RELOC_HI16_S_PCREL:
14977 case BFD_RELOC_LO16_PCREL:
252b5132 14978 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
14979 case BFD_RELOC_MIPS16_GOT16:
14980 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
14981 case BFD_RELOC_MIPS16_HI16:
14982 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 14983 case BFD_RELOC_MIPS16_LO16:
252b5132 14984 case BFD_RELOC_MIPS16_JMP:
df58fc94
RS
14985 case BFD_RELOC_MICROMIPS_JMP:
14986 case BFD_RELOC_MICROMIPS_GOT_DISP:
14987 case BFD_RELOC_MICROMIPS_GOT_PAGE:
14988 case BFD_RELOC_MICROMIPS_GOT_OFST:
14989 case BFD_RELOC_MICROMIPS_SUB:
14990 case BFD_RELOC_MICROMIPS_HIGHEST:
14991 case BFD_RELOC_MICROMIPS_HIGHER:
14992 case BFD_RELOC_MICROMIPS_SCN_DISP:
14993 case BFD_RELOC_MICROMIPS_JALR:
14994 case BFD_RELOC_MICROMIPS_HI16:
14995 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 14996 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
14997 case BFD_RELOC_MICROMIPS_GPREL16:
14998 case BFD_RELOC_MICROMIPS_LITERAL:
14999 case BFD_RELOC_MICROMIPS_CALL16:
15000 case BFD_RELOC_MICROMIPS_GOT16:
15001 case BFD_RELOC_MICROMIPS_GOT_HI16:
15002 case BFD_RELOC_MICROMIPS_GOT_LO16:
15003 case BFD_RELOC_MICROMIPS_CALL_HI16:
15004 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15005 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15006 if (fixP->fx_done)
15007 {
15008 offsetT value;
15009
15010 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15011 {
15012 insn = read_reloc_insn (buf, fixP->fx_r_type);
15013 if (mips16_reloc_p (fixP->fx_r_type))
15014 insn |= mips16_immed_extend (value, 16);
15015 else
15016 insn |= (value & 0xffff);
15017 write_reloc_insn (buf, fixP->fx_r_type, insn);
15018 }
15019 else
15020 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15021 _("unsupported constant in relocation"));
b886a2ab 15022 }
252b5132
RH
15023 break;
15024
252b5132
RH
15025 case BFD_RELOC_64:
15026 /* This is handled like BFD_RELOC_32, but we output a sign
15027 extended value if we are only 32 bits. */
3e722fb5 15028 if (fixP->fx_done)
252b5132
RH
15029 {
15030 if (8 <= sizeof (valueT))
4d68580a 15031 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15032 else
15033 {
a7ebbfdf 15034 valueT hiv;
252b5132 15035
a7ebbfdf 15036 if ((*valP & 0x80000000) != 0)
252b5132
RH
15037 hiv = 0xffffffff;
15038 else
15039 hiv = 0;
4d68580a
RS
15040 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15041 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15042 }
15043 }
15044 break;
15045
056350c6 15046 case BFD_RELOC_RVA:
252b5132 15047 case BFD_RELOC_32:
b47468a6 15048 case BFD_RELOC_32_PCREL:
252b5132 15049 case BFD_RELOC_16:
d56a8dda 15050 case BFD_RELOC_8:
252b5132 15051 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15052 value now. This can happen if we have a .word which is not
15053 resolved when it appears but is later defined. */
252b5132 15054 if (fixP->fx_done)
4d68580a 15055 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15056 break;
15057
7361da2c 15058 case BFD_RELOC_MIPS_21_PCREL_S2:
41947d9e
MR
15059 if ((*valP & 0x3) != 0)
15060 as_bad_where (fixP->fx_file, fixP->fx_line,
15061 _("branch to misaligned address (%lx)"), (long) *valP);
15062 if (!fixP->fx_done)
15063 break;
15064
15065 if (*valP + 0x400000 <= 0x7fffff)
15066 {
15067 insn = read_insn (buf);
15068 insn |= (*valP >> 2) & 0x1fffff;
15069 write_insn (buf, insn);
15070 }
15071 else
15072 as_bad_where (fixP->fx_file, fixP->fx_line,
15073 _("branch out of range"));
15074 break;
15075
7361da2c
AB
15076 case BFD_RELOC_MIPS_26_PCREL_S2:
15077 if ((*valP & 0x3) != 0)
15078 as_bad_where (fixP->fx_file, fixP->fx_line,
15079 _("branch to misaligned address (%lx)"), (long) *valP);
41947d9e
MR
15080 if (!fixP->fx_done)
15081 break;
7361da2c 15082
41947d9e
MR
15083 if (*valP + 0x8000000 <= 0xfffffff)
15084 {
15085 insn = read_insn (buf);
15086 insn |= (*valP >> 2) & 0x3ffffff;
15087 write_insn (buf, insn);
15088 }
15089 else
15090 as_bad_where (fixP->fx_file, fixP->fx_line,
15091 _("branch out of range"));
7361da2c
AB
15092 break;
15093
15094 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15095 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15096 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15097 _("PC-relative access using misaligned symbol (%lx)"),
15098 (long) S_GET_VALUE (fixP->fx_addsy));
15099 if ((fixP->fx_offset & 0x7) != 0)
15100 as_bad_where (fixP->fx_file, fixP->fx_line,
15101 _("PC-relative access using misaligned offset (%lx)"),
15102 (long) fixP->fx_offset);
41947d9e
MR
15103 if (!fixP->fx_done)
15104 break;
7361da2c 15105
41947d9e
MR
15106 if (*valP + 0x100000 <= 0x1fffff)
15107 {
15108 insn = read_insn (buf);
15109 insn |= (*valP >> 3) & 0x3ffff;
15110 write_insn (buf, insn);
15111 }
15112 else
15113 as_bad_where (fixP->fx_file, fixP->fx_line,
15114 _("PC-relative access out of range"));
7361da2c
AB
15115 break;
15116
15117 case BFD_RELOC_MIPS_19_PCREL_S2:
15118 if ((*valP & 0x3) != 0)
15119 as_bad_where (fixP->fx_file, fixP->fx_line,
15120 _("PC-relative access to misaligned address (%lx)"),
717ba204 15121 (long) *valP);
41947d9e
MR
15122 if (!fixP->fx_done)
15123 break;
7361da2c 15124
41947d9e
MR
15125 if (*valP + 0x100000 <= 0x1fffff)
15126 {
15127 insn = read_insn (buf);
15128 insn |= (*valP >> 2) & 0x7ffff;
15129 write_insn (buf, insn);
15130 }
15131 else
15132 as_bad_where (fixP->fx_file, fixP->fx_line,
15133 _("PC-relative access out of range"));
7361da2c
AB
15134 break;
15135
252b5132 15136 case BFD_RELOC_16_PCREL_S2:
a7ebbfdf 15137 if ((*valP & 0x3) != 0)
cb56d3d3 15138 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15139 _("branch to misaligned address (%lx)"), (long) *valP);
cb56d3d3 15140
54f4ddb3
TS
15141 /* We need to save the bits in the instruction since fixup_segment()
15142 might be deleting the relocation entry (i.e., a branch within
15143 the current segment). */
a7ebbfdf 15144 if (! fixP->fx_done)
bb2d6cd7 15145 break;
252b5132 15146
54f4ddb3 15147 /* Update old instruction data. */
4d68580a 15148 insn = read_insn (buf);
252b5132 15149
a7ebbfdf
TS
15150 if (*valP + 0x20000 <= 0x3ffff)
15151 {
15152 insn |= (*valP >> 2) & 0xffff;
4d68580a 15153 write_insn (buf, insn);
a7ebbfdf
TS
15154 }
15155 else if (mips_pic == NO_PIC
15156 && fixP->fx_done
15157 && fixP->fx_frag->fr_address >= text_section->vma
15158 && (fixP->fx_frag->fr_address
587aac4e 15159 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15160 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15161 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15162 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15163 {
15164 /* The branch offset is too large. If this is an
15165 unconditional branch, and we are not generating PIC code,
15166 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15167 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15168 insn = 0x0c000000; /* jal */
252b5132 15169 else
a7ebbfdf
TS
15170 insn = 0x08000000; /* j */
15171 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15172 fixP->fx_done = 0;
15173 fixP->fx_addsy = section_symbol (text_section);
15174 *valP += md_pcrel_from (fixP);
4d68580a 15175 write_insn (buf, insn);
a7ebbfdf
TS
15176 }
15177 else
15178 {
15179 /* If we got here, we have branch-relaxation disabled,
15180 and there's nothing we can do to fix this instruction
15181 without turning it into a longer sequence. */
15182 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15183 _("branch out of range"));
252b5132 15184 }
252b5132
RH
15185 break;
15186
df58fc94
RS
15187 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15188 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15189 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15190 /* We adjust the offset back to even. */
15191 if ((*valP & 0x1) != 0)
15192 --(*valP);
15193
15194 if (! fixP->fx_done)
15195 break;
15196
15197 /* Should never visit here, because we keep the relocation. */
15198 abort ();
15199 break;
15200
252b5132
RH
15201 case BFD_RELOC_VTABLE_INHERIT:
15202 fixP->fx_done = 0;
15203 if (fixP->fx_addsy
15204 && !S_IS_DEFINED (fixP->fx_addsy)
15205 && !S_IS_WEAK (fixP->fx_addsy))
15206 S_SET_WEAK (fixP->fx_addsy);
15207 break;
15208
2f0c68f2 15209 case BFD_RELOC_NONE:
252b5132
RH
15210 case BFD_RELOC_VTABLE_ENTRY:
15211 fixP->fx_done = 0;
15212 break;
15213
15214 default:
b37df7c4 15215 abort ();
252b5132 15216 }
a7ebbfdf
TS
15217
15218 /* Remember value for tc_gen_reloc. */
15219 fixP->fx_addnumber = *valP;
252b5132
RH
15220}
15221
252b5132 15222static symbolS *
17a2f251 15223get_symbol (void)
252b5132
RH
15224{
15225 int c;
15226 char *name;
15227 symbolS *p;
15228
d02603dc 15229 c = get_symbol_name (&name);
252b5132 15230 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15231 (void) restore_line_pointer (c);
252b5132
RH
15232 return p;
15233}
15234
742a56fe
RS
15235/* Align the current frag to a given power of two. If a particular
15236 fill byte should be used, FILL points to an integer that contains
15237 that byte, otherwise FILL is null.
15238
462427c4
RS
15239 This function used to have the comment:
15240
15241 The MIPS assembler also automatically adjusts any preceding label.
15242
15243 The implementation therefore applied the adjustment to a maximum of
15244 one label. However, other label adjustments are applied to batches
15245 of labels, and adjusting just one caused problems when new labels
15246 were added for the sake of debugging or unwind information.
15247 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15248
15249static void
462427c4 15250mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15251{
7d10b47d 15252 mips_emit_delays ();
df58fc94 15253 mips_record_compressed_mode ();
742a56fe
RS
15254 if (fill == NULL && subseg_text_p (now_seg))
15255 frag_align_code (to, 0);
15256 else
15257 frag_align (to, fill ? *fill : 0, 0);
252b5132 15258 record_alignment (now_seg, to);
462427c4 15259 mips_move_labels (labels, FALSE);
252b5132
RH
15260}
15261
15262/* Align to a given power of two. .align 0 turns off the automatic
15263 alignment used by the data creating pseudo-ops. */
15264
15265static void
17a2f251 15266s_align (int x ATTRIBUTE_UNUSED)
252b5132 15267{
742a56fe 15268 int temp, fill_value, *fill_ptr;
49954fb4 15269 long max_alignment = 28;
252b5132 15270
54f4ddb3 15271 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15272 to the aligned address.
54f4ddb3 15273 o It's not documented but auto alignment is reinstated by
252b5132 15274 a .align pseudo instruction.
54f4ddb3 15275 o Note also that after auto alignment is turned off the mips assembler
252b5132 15276 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15277 We don't. */
252b5132
RH
15278
15279 temp = get_absolute_expression ();
15280 if (temp > max_alignment)
1661c76c 15281 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15282 else if (temp < 0)
15283 {
1661c76c 15284 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15285 temp = 0;
15286 }
15287 if (*input_line_pointer == ',')
15288 {
f9419b05 15289 ++input_line_pointer;
742a56fe
RS
15290 fill_value = get_absolute_expression ();
15291 fill_ptr = &fill_value;
252b5132
RH
15292 }
15293 else
742a56fe 15294 fill_ptr = 0;
252b5132
RH
15295 if (temp)
15296 {
a8dbcb85
TS
15297 segment_info_type *si = seg_info (now_seg);
15298 struct insn_label_list *l = si->label_list;
54f4ddb3 15299 /* Auto alignment should be switched on by next section change. */
252b5132 15300 auto_align = 1;
462427c4 15301 mips_align (temp, fill_ptr, l);
252b5132
RH
15302 }
15303 else
15304 {
15305 auto_align = 0;
15306 }
15307
15308 demand_empty_rest_of_line ();
15309}
15310
252b5132 15311static void
17a2f251 15312s_change_sec (int sec)
252b5132
RH
15313{
15314 segT seg;
15315
252b5132
RH
15316 /* The ELF backend needs to know that we are changing sections, so
15317 that .previous works correctly. We could do something like check
b6ff326e 15318 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15319 as it would not be appropriate to use it in the section changing
15320 functions in read.c, since obj-elf.c intercepts those. FIXME:
15321 This should be cleaner, somehow. */
f3ded42a 15322 obj_elf_section_change_hook ();
252b5132 15323
7d10b47d 15324 mips_emit_delays ();
6a32d874 15325
252b5132
RH
15326 switch (sec)
15327 {
15328 case 't':
15329 s_text (0);
15330 break;
15331 case 'd':
15332 s_data (0);
15333 break;
15334 case 'b':
15335 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15336 demand_empty_rest_of_line ();
15337 break;
15338
15339 case 'r':
4d0d148d
TS
15340 seg = subseg_new (RDATA_SECTION_NAME,
15341 (subsegT) get_absolute_expression ());
f3ded42a
RS
15342 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15343 | SEC_READONLY | SEC_RELOC
15344 | SEC_DATA));
15345 if (strncmp (TARGET_OS, "elf", 3) != 0)
15346 record_alignment (seg, 4);
4d0d148d 15347 demand_empty_rest_of_line ();
252b5132
RH
15348 break;
15349
15350 case 's':
4d0d148d 15351 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
15352 bfd_set_section_flags (stdoutput, seg,
15353 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
15354 if (strncmp (TARGET_OS, "elf", 3) != 0)
15355 record_alignment (seg, 4);
4d0d148d
TS
15356 demand_empty_rest_of_line ();
15357 break;
998b3c36
MR
15358
15359 case 'B':
15360 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
15361 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
15362 if (strncmp (TARGET_OS, "elf", 3) != 0)
15363 record_alignment (seg, 4);
998b3c36
MR
15364 demand_empty_rest_of_line ();
15365 break;
252b5132
RH
15366 }
15367
15368 auto_align = 1;
15369}
b34976b6 15370
cca86cc8 15371void
17a2f251 15372s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 15373{
d02603dc 15374 char *saved_ilp;
cca86cc8 15375 char *section_name;
d02603dc 15376 char c, endc;
684022ea 15377 char next_c = 0;
cca86cc8
SC
15378 int section_type;
15379 int section_flag;
15380 int section_entry_size;
15381 int section_alignment;
b34976b6 15382
d02603dc
NC
15383 saved_ilp = input_line_pointer;
15384 endc = get_symbol_name (&section_name);
15385 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 15386 if (c)
d02603dc 15387 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 15388
4cf0dd0d
TS
15389 /* Do we have .section Name<,"flags">? */
15390 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 15391 {
d02603dc
NC
15392 /* Just after name is now '\0'. */
15393 (void) restore_line_pointer (endc);
15394 input_line_pointer = saved_ilp;
cca86cc8
SC
15395 obj_elf_section (ignore);
15396 return;
15397 }
d02603dc
NC
15398
15399 section_name = xstrdup (section_name);
15400 c = restore_line_pointer (endc);
15401
cca86cc8
SC
15402 input_line_pointer++;
15403
15404 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
15405 if (c == ',')
15406 section_type = get_absolute_expression ();
15407 else
15408 section_type = 0;
d02603dc 15409
cca86cc8
SC
15410 if (*input_line_pointer++ == ',')
15411 section_flag = get_absolute_expression ();
15412 else
15413 section_flag = 0;
d02603dc 15414
cca86cc8
SC
15415 if (*input_line_pointer++ == ',')
15416 section_entry_size = get_absolute_expression ();
15417 else
15418 section_entry_size = 0;
d02603dc 15419
cca86cc8
SC
15420 if (*input_line_pointer++ == ',')
15421 section_alignment = get_absolute_expression ();
15422 else
15423 section_alignment = 0;
d02603dc 15424
87975d2a
AM
15425 /* FIXME: really ignore? */
15426 (void) section_alignment;
cca86cc8 15427
8ab8a5c8
RS
15428 /* When using the generic form of .section (as implemented by obj-elf.c),
15429 there's no way to set the section type to SHT_MIPS_DWARF. Users have
15430 traditionally had to fall back on the more common @progbits instead.
15431
15432 There's nothing really harmful in this, since bfd will correct
15433 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 15434 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
15435 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
15436
15437 Even so, we shouldn't force users of the MIPS .section syntax to
15438 incorrectly label the sections as SHT_PROGBITS. The best compromise
15439 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
15440 generic type-checking code. */
15441 if (section_type == SHT_MIPS_DWARF)
15442 section_type = SHT_PROGBITS;
15443
cca86cc8
SC
15444 obj_elf_change_section (section_name, section_type, section_flag,
15445 section_entry_size, 0, 0, 0);
a816d1ed
AO
15446
15447 if (now_seg->name != section_name)
15448 free (section_name);
cca86cc8 15449}
252b5132
RH
15450
15451void
17a2f251 15452mips_enable_auto_align (void)
252b5132
RH
15453{
15454 auto_align = 1;
15455}
15456
15457static void
17a2f251 15458s_cons (int log_size)
252b5132 15459{
a8dbcb85
TS
15460 segment_info_type *si = seg_info (now_seg);
15461 struct insn_label_list *l = si->label_list;
252b5132 15462
7d10b47d 15463 mips_emit_delays ();
252b5132 15464 if (log_size > 0 && auto_align)
462427c4 15465 mips_align (log_size, 0, l);
252b5132 15466 cons (1 << log_size);
a1facbec 15467 mips_clear_insn_labels ();
252b5132
RH
15468}
15469
15470static void
17a2f251 15471s_float_cons (int type)
252b5132 15472{
a8dbcb85
TS
15473 segment_info_type *si = seg_info (now_seg);
15474 struct insn_label_list *l = si->label_list;
252b5132 15475
7d10b47d 15476 mips_emit_delays ();
252b5132
RH
15477
15478 if (auto_align)
49309057
ILT
15479 {
15480 if (type == 'd')
462427c4 15481 mips_align (3, 0, l);
49309057 15482 else
462427c4 15483 mips_align (2, 0, l);
49309057 15484 }
252b5132 15485
252b5132 15486 float_cons (type);
a1facbec 15487 mips_clear_insn_labels ();
252b5132
RH
15488}
15489
15490/* Handle .globl. We need to override it because on Irix 5 you are
15491 permitted to say
15492 .globl foo .text
15493 where foo is an undefined symbol, to mean that foo should be
15494 considered to be the address of a function. */
15495
15496static void
17a2f251 15497s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
15498{
15499 char *name;
15500 int c;
15501 symbolS *symbolP;
15502 flagword flag;
15503
8a06b769 15504 do
252b5132 15505 {
d02603dc 15506 c = get_symbol_name (&name);
8a06b769
TS
15507 symbolP = symbol_find_or_make (name);
15508 S_SET_EXTERNAL (symbolP);
15509
252b5132 15510 *input_line_pointer = c;
d02603dc 15511 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 15512
8a06b769
TS
15513 /* On Irix 5, every global symbol that is not explicitly labelled as
15514 being a function is apparently labelled as being an object. */
15515 flag = BSF_OBJECT;
252b5132 15516
8a06b769
TS
15517 if (!is_end_of_line[(unsigned char) *input_line_pointer]
15518 && (*input_line_pointer != ','))
15519 {
15520 char *secname;
15521 asection *sec;
15522
d02603dc 15523 c = get_symbol_name (&secname);
8a06b769
TS
15524 sec = bfd_get_section_by_name (stdoutput, secname);
15525 if (sec == NULL)
15526 as_bad (_("%s: no such section"), secname);
d02603dc 15527 (void) restore_line_pointer (c);
8a06b769
TS
15528
15529 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
15530 flag = BSF_FUNCTION;
15531 }
15532
15533 symbol_get_bfdsym (symbolP)->flags |= flag;
15534
15535 c = *input_line_pointer;
15536 if (c == ',')
15537 {
15538 input_line_pointer++;
15539 SKIP_WHITESPACE ();
15540 if (is_end_of_line[(unsigned char) *input_line_pointer])
15541 c = '\n';
15542 }
15543 }
15544 while (c == ',');
252b5132 15545
252b5132
RH
15546 demand_empty_rest_of_line ();
15547}
15548
15549static void
17a2f251 15550s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
15551{
15552 char *opt;
15553 char c;
15554
d02603dc 15555 c = get_symbol_name (&opt);
252b5132
RH
15556
15557 if (*opt == 'O')
15558 {
15559 /* FIXME: What does this mean? */
15560 }
41a1578e 15561 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
15562 {
15563 int i;
15564
15565 i = atoi (opt + 3);
668c5ebc
MR
15566 if (i != 0 && i != 2)
15567 as_bad (_(".option pic%d not supported"), i);
15568 else if (mips_pic == VXWORKS_PIC)
15569 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
15570 else if (i == 0)
252b5132
RH
15571 mips_pic = NO_PIC;
15572 else if (i == 2)
143d77c5 15573 {
8b828383 15574 mips_pic = SVR4_PIC;
143d77c5
EC
15575 mips_abicalls = TRUE;
15576 }
252b5132 15577
4d0d148d 15578 if (mips_pic == SVR4_PIC)
252b5132
RH
15579 {
15580 if (g_switch_seen && g_switch_value != 0)
15581 as_warn (_("-G may not be used with SVR4 PIC code"));
15582 g_switch_value = 0;
15583 bfd_set_gp_size (stdoutput, 0);
15584 }
15585 }
15586 else
1661c76c 15587 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 15588
d02603dc 15589 (void) restore_line_pointer (c);
252b5132
RH
15590 demand_empty_rest_of_line ();
15591}
15592
15593/* This structure is used to hold a stack of .set values. */
15594
e972090a
NC
15595struct mips_option_stack
15596{
252b5132
RH
15597 struct mips_option_stack *next;
15598 struct mips_set_options options;
15599};
15600
15601static struct mips_option_stack *mips_opts_stack;
15602
22522f88
MR
15603/* Return status for .set/.module option handling. */
15604
15605enum code_option_type
15606{
15607 /* Unrecognized option. */
15608 OPTION_TYPE_BAD = -1,
15609
15610 /* Ordinary option. */
15611 OPTION_TYPE_NORMAL,
15612
15613 /* ISA changing option. */
15614 OPTION_TYPE_ISA
15615};
15616
15617/* Handle common .set/.module options. Return status indicating option
15618 type. */
15619
15620static enum code_option_type
919731af 15621parse_code_option (char * name)
252b5132 15622{
22522f88 15623 bfd_boolean isa_set = FALSE;
c6278170 15624 const struct mips_ase *ase;
22522f88 15625
919731af 15626 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
15627 {
15628 char *s = name + 3;
15629
15630 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 15631 as_bad (_("unrecognized register name `%s'"), s);
741fe287 15632 }
252b5132 15633 else if (strcmp (name, "at") == 0)
919731af 15634 mips_opts.at = ATREG;
252b5132 15635 else if (strcmp (name, "noat") == 0)
919731af 15636 mips_opts.at = ZERO;
252b5132 15637 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 15638 mips_opts.nomove = 0;
252b5132 15639 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 15640 mips_opts.nomove = 1;
252b5132 15641 else if (strcmp (name, "bopt") == 0)
919731af 15642 mips_opts.nobopt = 0;
252b5132 15643 else if (strcmp (name, "nobopt") == 0)
919731af 15644 mips_opts.nobopt = 1;
ad3fea08 15645 else if (strcmp (name, "gp=32") == 0)
bad1aba3 15646 mips_opts.gp = 32;
ad3fea08 15647 else if (strcmp (name, "gp=64") == 0)
919731af 15648 mips_opts.gp = 64;
ad3fea08 15649 else if (strcmp (name, "fp=32") == 0)
0b35dfee 15650 mips_opts.fp = 32;
351cdf24
MF
15651 else if (strcmp (name, "fp=xx") == 0)
15652 mips_opts.fp = 0;
ad3fea08 15653 else if (strcmp (name, "fp=64") == 0)
919731af 15654 mips_opts.fp = 64;
037b32b9
AN
15655 else if (strcmp (name, "softfloat") == 0)
15656 mips_opts.soft_float = 1;
15657 else if (strcmp (name, "hardfloat") == 0)
15658 mips_opts.soft_float = 0;
15659 else if (strcmp (name, "singlefloat") == 0)
15660 mips_opts.single_float = 1;
15661 else if (strcmp (name, "doublefloat") == 0)
15662 mips_opts.single_float = 0;
351cdf24
MF
15663 else if (strcmp (name, "nooddspreg") == 0)
15664 mips_opts.oddspreg = 0;
15665 else if (strcmp (name, "oddspreg") == 0)
15666 mips_opts.oddspreg = 1;
252b5132
RH
15667 else if (strcmp (name, "mips16") == 0
15668 || strcmp (name, "MIPS-16") == 0)
919731af 15669 mips_opts.mips16 = 1;
252b5132
RH
15670 else if (strcmp (name, "nomips16") == 0
15671 || strcmp (name, "noMIPS-16") == 0)
15672 mips_opts.mips16 = 0;
df58fc94 15673 else if (strcmp (name, "micromips") == 0)
919731af 15674 mips_opts.micromips = 1;
df58fc94
RS
15675 else if (strcmp (name, "nomicromips") == 0)
15676 mips_opts.micromips = 0;
c6278170
RS
15677 else if (name[0] == 'n'
15678 && name[1] == 'o'
15679 && (ase = mips_lookup_ase (name + 2)))
919731af 15680 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 15681 else if ((ase = mips_lookup_ase (name)))
919731af 15682 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 15683 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 15684 {
1a2c1fad
CD
15685 /* Permit the user to change the ISA and architecture on the fly.
15686 Needless to say, misuse can cause serious problems. */
919731af 15687 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
15688 {
15689 const struct mips_cpu_info *p;
15690
919731af 15691 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
15692 if (!p)
15693 as_bad (_("unknown architecture %s"), name + 5);
15694 else
15695 {
15696 mips_opts.arch = p->cpu;
15697 mips_opts.isa = p->isa;
22522f88 15698 isa_set = TRUE;
1a2c1fad
CD
15699 }
15700 }
81a21e38
TS
15701 else if (strncmp (name, "mips", 4) == 0)
15702 {
15703 const struct mips_cpu_info *p;
15704
919731af 15705 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
15706 if (!p)
15707 as_bad (_("unknown ISA level %s"), name + 4);
15708 else
15709 {
15710 mips_opts.arch = p->cpu;
15711 mips_opts.isa = p->isa;
22522f88 15712 isa_set = TRUE;
81a21e38
TS
15713 }
15714 }
af7ee8bf 15715 else
81a21e38 15716 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
15717 }
15718 else if (strcmp (name, "autoextend") == 0)
15719 mips_opts.noautoextend = 0;
15720 else if (strcmp (name, "noautoextend") == 0)
15721 mips_opts.noautoextend = 1;
833794fc
MR
15722 else if (strcmp (name, "insn32") == 0)
15723 mips_opts.insn32 = TRUE;
15724 else if (strcmp (name, "noinsn32") == 0)
15725 mips_opts.insn32 = FALSE;
919731af 15726 else if (strcmp (name, "sym32") == 0)
15727 mips_opts.sym32 = TRUE;
15728 else if (strcmp (name, "nosym32") == 0)
15729 mips_opts.sym32 = FALSE;
15730 else
22522f88
MR
15731 return OPTION_TYPE_BAD;
15732
15733 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 15734}
15735
15736/* Handle the .set pseudo-op. */
15737
15738static void
15739s_mipsset (int x ATTRIBUTE_UNUSED)
15740{
22522f88 15741 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 15742 char *name = input_line_pointer, ch;
919731af 15743
15744 file_mips_check_options ();
15745
15746 while (!is_end_of_line[(unsigned char) *input_line_pointer])
15747 ++input_line_pointer;
15748 ch = *input_line_pointer;
15749 *input_line_pointer = '\0';
15750
15751 if (strchr (name, ','))
15752 {
15753 /* Generic ".set" directive; use the generic handler. */
15754 *input_line_pointer = ch;
15755 input_line_pointer = name;
15756 s_set (0);
15757 return;
15758 }
15759
15760 if (strcmp (name, "reorder") == 0)
15761 {
15762 if (mips_opts.noreorder)
15763 end_noreorder ();
15764 }
15765 else if (strcmp (name, "noreorder") == 0)
15766 {
15767 if (!mips_opts.noreorder)
15768 start_noreorder ();
15769 }
15770 else if (strcmp (name, "macro") == 0)
15771 mips_opts.warn_about_macros = 0;
15772 else if (strcmp (name, "nomacro") == 0)
15773 {
15774 if (mips_opts.noreorder == 0)
15775 as_bad (_("`noreorder' must be set before `nomacro'"));
15776 mips_opts.warn_about_macros = 1;
15777 }
15778 else if (strcmp (name, "gp=default") == 0)
15779 mips_opts.gp = file_mips_opts.gp;
15780 else if (strcmp (name, "fp=default") == 0)
15781 mips_opts.fp = file_mips_opts.fp;
15782 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
15783 {
15784 mips_opts.isa = file_mips_opts.isa;
15785 mips_opts.arch = file_mips_opts.arch;
15786 mips_opts.gp = file_mips_opts.gp;
15787 mips_opts.fp = file_mips_opts.fp;
15788 }
252b5132
RH
15789 else if (strcmp (name, "push") == 0)
15790 {
15791 struct mips_option_stack *s;
15792
325801bd 15793 s = XNEW (struct mips_option_stack);
252b5132
RH
15794 s->next = mips_opts_stack;
15795 s->options = mips_opts;
15796 mips_opts_stack = s;
15797 }
15798 else if (strcmp (name, "pop") == 0)
15799 {
15800 struct mips_option_stack *s;
15801
15802 s = mips_opts_stack;
15803 if (s == NULL)
15804 as_bad (_(".set pop with no .set push"));
15805 else
15806 {
15807 /* If we're changing the reorder mode we need to handle
15808 delay slots correctly. */
15809 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 15810 start_noreorder ();
252b5132 15811 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 15812 end_noreorder ();
252b5132
RH
15813
15814 mips_opts = s->options;
15815 mips_opts_stack = s->next;
15816 free (s);
15817 }
15818 }
22522f88
MR
15819 else
15820 {
15821 type = parse_code_option (name);
15822 if (type == OPTION_TYPE_BAD)
15823 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
15824 }
919731af 15825
15826 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
15827 registers based on what is supported by the arch/cpu. */
22522f88 15828 if (type == OPTION_TYPE_ISA)
e6559e01 15829 {
919731af 15830 switch (mips_opts.isa)
15831 {
15832 case 0:
15833 break;
15834 case ISA_MIPS1:
351cdf24
MF
15835 /* MIPS I cannot support FPXX. */
15836 mips_opts.fp = 32;
15837 /* fall-through. */
919731af 15838 case ISA_MIPS2:
15839 case ISA_MIPS32:
15840 case ISA_MIPS32R2:
15841 case ISA_MIPS32R3:
15842 case ISA_MIPS32R5:
15843 mips_opts.gp = 32;
351cdf24
MF
15844 if (mips_opts.fp != 0)
15845 mips_opts.fp = 32;
919731af 15846 break;
7361da2c
AB
15847 case ISA_MIPS32R6:
15848 mips_opts.gp = 32;
15849 mips_opts.fp = 64;
15850 break;
919731af 15851 case ISA_MIPS3:
15852 case ISA_MIPS4:
15853 case ISA_MIPS5:
15854 case ISA_MIPS64:
15855 case ISA_MIPS64R2:
15856 case ISA_MIPS64R3:
15857 case ISA_MIPS64R5:
7361da2c 15858 case ISA_MIPS64R6:
919731af 15859 mips_opts.gp = 64;
351cdf24
MF
15860 if (mips_opts.fp != 0)
15861 {
15862 if (mips_opts.arch == CPU_R5900)
15863 mips_opts.fp = 32;
15864 else
15865 mips_opts.fp = 64;
15866 }
919731af 15867 break;
15868 default:
15869 as_bad (_("unknown ISA level %s"), name + 4);
15870 break;
15871 }
e6559e01 15872 }
919731af 15873
15874 mips_check_options (&mips_opts, FALSE);
15875
15876 mips_check_isa_supports_ases ();
15877 *input_line_pointer = ch;
15878 demand_empty_rest_of_line ();
15879}
15880
15881/* Handle the .module pseudo-op. */
15882
15883static void
15884s_module (int ignore ATTRIBUTE_UNUSED)
15885{
15886 char *name = input_line_pointer, ch;
15887
15888 while (!is_end_of_line[(unsigned char) *input_line_pointer])
15889 ++input_line_pointer;
15890 ch = *input_line_pointer;
15891 *input_line_pointer = '\0';
15892
15893 if (!file_mips_opts_checked)
252b5132 15894 {
22522f88 15895 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 15896 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
15897
15898 /* Update module level settings from mips_opts. */
15899 file_mips_opts = mips_opts;
252b5132 15900 }
919731af 15901 else
15902 as_bad (_(".module is not permitted after generating code"));
15903
252b5132
RH
15904 *input_line_pointer = ch;
15905 demand_empty_rest_of_line ();
15906}
15907
15908/* Handle the .abicalls pseudo-op. I believe this is equivalent to
15909 .option pic2. It means to generate SVR4 PIC calls. */
15910
15911static void
17a2f251 15912s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15913{
15914 mips_pic = SVR4_PIC;
143d77c5 15915 mips_abicalls = TRUE;
4d0d148d
TS
15916
15917 if (g_switch_seen && g_switch_value != 0)
15918 as_warn (_("-G may not be used with SVR4 PIC code"));
15919 g_switch_value = 0;
15920
252b5132
RH
15921 bfd_set_gp_size (stdoutput, 0);
15922 demand_empty_rest_of_line ();
15923}
15924
15925/* Handle the .cpload pseudo-op. This is used when generating SVR4
15926 PIC code. It sets the $gp register for the function based on the
15927 function address, which is in the register named in the argument.
15928 This uses a relocation against _gp_disp, which is handled specially
15929 by the linker. The result is:
15930 lui $gp,%hi(_gp_disp)
15931 addiu $gp,$gp,%lo(_gp_disp)
15932 addu $gp,$gp,.cpload argument
aa6975fb
ILT
15933 The .cpload argument is normally $25 == $t9.
15934
15935 The -mno-shared option changes this to:
bbe506e8
TS
15936 lui $gp,%hi(__gnu_local_gp)
15937 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
15938 and the argument is ignored. This saves an instruction, but the
15939 resulting code is not position independent; it uses an absolute
bbe506e8
TS
15940 address for __gnu_local_gp. Thus code assembled with -mno-shared
15941 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
15942
15943static void
17a2f251 15944s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
15945{
15946 expressionS ex;
aa6975fb
ILT
15947 int reg;
15948 int in_shared;
252b5132 15949
919731af 15950 file_mips_check_options ();
15951
6478892d
TS
15952 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
15953 .cpload is ignored. */
15954 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
15955 {
15956 s_ignore (0);
15957 return;
15958 }
15959
a276b80c
MR
15960 if (mips_opts.mips16)
15961 {
15962 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
15963 ignore_rest_of_line ();
15964 return;
15965 }
15966
d3ecfc59 15967 /* .cpload should be in a .set noreorder section. */
252b5132
RH
15968 if (mips_opts.noreorder == 0)
15969 as_warn (_(".cpload not in noreorder section"));
15970
aa6975fb
ILT
15971 reg = tc_get_register (0);
15972
15973 /* If we need to produce a 64-bit address, we are better off using
15974 the default instruction sequence. */
aed1a261 15975 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 15976
252b5132 15977 ex.X_op = O_symbol;
bbe506e8
TS
15978 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
15979 "__gnu_local_gp");
252b5132
RH
15980 ex.X_op_symbol = NULL;
15981 ex.X_add_number = 0;
15982
15983 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 15984 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 15985
8a75745d
MR
15986 mips_mark_labels ();
15987 mips_assembling_insn = TRUE;
15988
584892a6 15989 macro_start ();
67c0d1eb
RS
15990 macro_build_lui (&ex, mips_gp_register);
15991 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 15992 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
15993 if (in_shared)
15994 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
15995 mips_gp_register, reg);
584892a6 15996 macro_end ();
252b5132 15997
8a75745d 15998 mips_assembling_insn = FALSE;
252b5132
RH
15999 demand_empty_rest_of_line ();
16000}
16001
6478892d
TS
16002/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16003 .cpsetup $reg1, offset|$reg2, label
16004
16005 If offset is given, this results in:
16006 sd $gp, offset($sp)
956cd1d6 16007 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16008 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16009 daddu $gp, $gp, $reg1
6478892d
TS
16010
16011 If $reg2 is given, this results in:
40fc1451 16012 or $reg2, $gp, $0
956cd1d6 16013 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16014 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16015 daddu $gp, $gp, $reg1
aa6975fb
ILT
16016 $reg1 is normally $25 == $t9.
16017
16018 The -mno-shared option replaces the last three instructions with
16019 lui $gp,%hi(_gp)
54f4ddb3 16020 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16021
6478892d 16022static void
17a2f251 16023s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16024{
16025 expressionS ex_off;
16026 expressionS ex_sym;
16027 int reg1;
6478892d 16028
919731af 16029 file_mips_check_options ();
16030
8586fc66 16031 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16032 We also need NewABI support. */
16033 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16034 {
16035 s_ignore (0);
16036 return;
16037 }
16038
a276b80c
MR
16039 if (mips_opts.mips16)
16040 {
16041 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16042 ignore_rest_of_line ();
16043 return;
16044 }
16045
6478892d
TS
16046 reg1 = tc_get_register (0);
16047 SKIP_WHITESPACE ();
16048 if (*input_line_pointer != ',')
16049 {
16050 as_bad (_("missing argument separator ',' for .cpsetup"));
16051 return;
16052 }
16053 else
80245285 16054 ++input_line_pointer;
6478892d
TS
16055 SKIP_WHITESPACE ();
16056 if (*input_line_pointer == '$')
80245285
TS
16057 {
16058 mips_cpreturn_register = tc_get_register (0);
16059 mips_cpreturn_offset = -1;
16060 }
6478892d 16061 else
80245285
TS
16062 {
16063 mips_cpreturn_offset = get_absolute_expression ();
16064 mips_cpreturn_register = -1;
16065 }
6478892d
TS
16066 SKIP_WHITESPACE ();
16067 if (*input_line_pointer != ',')
16068 {
16069 as_bad (_("missing argument separator ',' for .cpsetup"));
16070 return;
16071 }
16072 else
f9419b05 16073 ++input_line_pointer;
6478892d 16074 SKIP_WHITESPACE ();
f21f8242 16075 expression (&ex_sym);
6478892d 16076
8a75745d
MR
16077 mips_mark_labels ();
16078 mips_assembling_insn = TRUE;
16079
584892a6 16080 macro_start ();
6478892d
TS
16081 if (mips_cpreturn_register == -1)
16082 {
16083 ex_off.X_op = O_constant;
16084 ex_off.X_add_symbol = NULL;
16085 ex_off.X_op_symbol = NULL;
16086 ex_off.X_add_number = mips_cpreturn_offset;
16087
67c0d1eb 16088 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16089 BFD_RELOC_LO16, SP);
6478892d
TS
16090 }
16091 else
40fc1451 16092 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16093
aed1a261 16094 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16095 {
df58fc94 16096 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16097 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16098 BFD_RELOC_HI16_S);
16099
16100 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16101 mips_gp_register, -1, BFD_RELOC_GPREL16,
16102 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16103
16104 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16105 mips_gp_register, reg1);
16106 }
16107 else
16108 {
16109 expressionS ex;
16110
16111 ex.X_op = O_symbol;
4184909a 16112 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16113 ex.X_op_symbol = NULL;
16114 ex.X_add_number = 0;
6e1304d8 16115
aa6975fb
ILT
16116 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16117 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16118
16119 macro_build_lui (&ex, mips_gp_register);
16120 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16121 mips_gp_register, BFD_RELOC_LO16);
16122 }
f21f8242 16123
584892a6 16124 macro_end ();
6478892d 16125
8a75745d 16126 mips_assembling_insn = FALSE;
6478892d
TS
16127 demand_empty_rest_of_line ();
16128}
16129
16130static void
17a2f251 16131s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16132{
919731af 16133 file_mips_check_options ();
16134
6478892d 16135 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16136 .cplocal is ignored. */
6478892d
TS
16137 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16138 {
16139 s_ignore (0);
16140 return;
16141 }
16142
a276b80c
MR
16143 if (mips_opts.mips16)
16144 {
16145 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16146 ignore_rest_of_line ();
16147 return;
16148 }
16149
6478892d 16150 mips_gp_register = tc_get_register (0);
85b51719 16151 demand_empty_rest_of_line ();
6478892d
TS
16152}
16153
252b5132
RH
16154/* Handle the .cprestore pseudo-op. This stores $gp into a given
16155 offset from $sp. The offset is remembered, and after making a PIC
16156 call $gp is restored from that location. */
16157
16158static void
17a2f251 16159s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16160{
16161 expressionS ex;
252b5132 16162
919731af 16163 file_mips_check_options ();
16164
6478892d 16165 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16166 .cprestore is ignored. */
6478892d 16167 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16168 {
16169 s_ignore (0);
16170 return;
16171 }
16172
a276b80c
MR
16173 if (mips_opts.mips16)
16174 {
16175 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16176 ignore_rest_of_line ();
16177 return;
16178 }
16179
252b5132 16180 mips_cprestore_offset = get_absolute_expression ();
7a621144 16181 mips_cprestore_valid = 1;
252b5132
RH
16182
16183 ex.X_op = O_constant;
16184 ex.X_add_symbol = NULL;
16185 ex.X_op_symbol = NULL;
16186 ex.X_add_number = mips_cprestore_offset;
16187
8a75745d
MR
16188 mips_mark_labels ();
16189 mips_assembling_insn = TRUE;
16190
584892a6 16191 macro_start ();
67c0d1eb
RS
16192 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16193 SP, HAVE_64BIT_ADDRESSES);
584892a6 16194 macro_end ();
252b5132 16195
8a75745d 16196 mips_assembling_insn = FALSE;
252b5132
RH
16197 demand_empty_rest_of_line ();
16198}
16199
6478892d 16200/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16201 was given in the preceding .cpsetup, it results in:
6478892d 16202 ld $gp, offset($sp)
76b3015f 16203
6478892d 16204 If a register $reg2 was given there, it results in:
40fc1451 16205 or $gp, $reg2, $0 */
54f4ddb3 16206
6478892d 16207static void
17a2f251 16208s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16209{
16210 expressionS ex;
6478892d 16211
919731af 16212 file_mips_check_options ();
16213
6478892d
TS
16214 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16215 We also need NewABI support. */
16216 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16217 {
16218 s_ignore (0);
16219 return;
16220 }
16221
a276b80c
MR
16222 if (mips_opts.mips16)
16223 {
16224 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16225 ignore_rest_of_line ();
16226 return;
16227 }
16228
8a75745d
MR
16229 mips_mark_labels ();
16230 mips_assembling_insn = TRUE;
16231
584892a6 16232 macro_start ();
6478892d
TS
16233 if (mips_cpreturn_register == -1)
16234 {
16235 ex.X_op = O_constant;
16236 ex.X_add_symbol = NULL;
16237 ex.X_op_symbol = NULL;
16238 ex.X_add_number = mips_cpreturn_offset;
16239
67c0d1eb 16240 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16241 }
16242 else
40fc1451
SD
16243 move_register (mips_gp_register, mips_cpreturn_register);
16244
584892a6 16245 macro_end ();
6478892d 16246
8a75745d 16247 mips_assembling_insn = FALSE;
6478892d
TS
16248 demand_empty_rest_of_line ();
16249}
16250
d0f13682
CLT
16251/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16252 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16253 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16254 debug information or MIPS16 TLS. */
741d6ea8
JM
16255
16256static void
d0f13682
CLT
16257s_tls_rel_directive (const size_t bytes, const char *dirstr,
16258 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16259{
16260 expressionS ex;
16261 char *p;
16262
16263 expression (&ex);
16264
16265 if (ex.X_op != O_symbol)
16266 {
1661c76c 16267 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16268 ignore_rest_of_line ();
16269 }
16270
16271 p = frag_more (bytes);
16272 md_number_to_chars (p, 0, bytes);
d0f13682 16273 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16274 demand_empty_rest_of_line ();
de64cffd 16275 mips_clear_insn_labels ();
741d6ea8
JM
16276}
16277
16278/* Handle .dtprelword. */
16279
16280static void
16281s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16282{
d0f13682 16283 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16284}
16285
16286/* Handle .dtpreldword. */
16287
16288static void
16289s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16290{
d0f13682
CLT
16291 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16292}
16293
16294/* Handle .tprelword. */
16295
16296static void
16297s_tprelword (int ignore ATTRIBUTE_UNUSED)
16298{
16299 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16300}
16301
16302/* Handle .tpreldword. */
16303
16304static void
16305s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16306{
16307 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16308}
16309
6478892d
TS
16310/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16311 code. It sets the offset to use in gp_rel relocations. */
16312
16313static void
17a2f251 16314s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16315{
16316 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16317 We also need NewABI support. */
16318 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16319 {
16320 s_ignore (0);
16321 return;
16322 }
16323
def2e0dd 16324 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16325
16326 demand_empty_rest_of_line ();
16327}
16328
252b5132
RH
16329/* Handle the .gpword pseudo-op. This is used when generating PIC
16330 code. It generates a 32 bit GP relative reloc. */
16331
16332static void
17a2f251 16333s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16334{
a8dbcb85
TS
16335 segment_info_type *si;
16336 struct insn_label_list *l;
252b5132
RH
16337 expressionS ex;
16338 char *p;
16339
16340 /* When not generating PIC code, this is treated as .word. */
16341 if (mips_pic != SVR4_PIC)
16342 {
16343 s_cons (2);
16344 return;
16345 }
16346
a8dbcb85
TS
16347 si = seg_info (now_seg);
16348 l = si->label_list;
7d10b47d 16349 mips_emit_delays ();
252b5132 16350 if (auto_align)
462427c4 16351 mips_align (2, 0, l);
252b5132
RH
16352
16353 expression (&ex);
a1facbec 16354 mips_clear_insn_labels ();
252b5132
RH
16355
16356 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16357 {
1661c76c 16358 as_bad (_("unsupported use of .gpword"));
252b5132
RH
16359 ignore_rest_of_line ();
16360 }
16361
16362 p = frag_more (4);
17a2f251 16363 md_number_to_chars (p, 0, 4);
b34976b6 16364 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 16365 BFD_RELOC_GPREL32);
252b5132
RH
16366
16367 demand_empty_rest_of_line ();
16368}
16369
10181a0d 16370static void
17a2f251 16371s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 16372{
a8dbcb85
TS
16373 segment_info_type *si;
16374 struct insn_label_list *l;
10181a0d
AO
16375 expressionS ex;
16376 char *p;
16377
16378 /* When not generating PIC code, this is treated as .dword. */
16379 if (mips_pic != SVR4_PIC)
16380 {
16381 s_cons (3);
16382 return;
16383 }
16384
a8dbcb85
TS
16385 si = seg_info (now_seg);
16386 l = si->label_list;
7d10b47d 16387 mips_emit_delays ();
10181a0d 16388 if (auto_align)
462427c4 16389 mips_align (3, 0, l);
10181a0d
AO
16390
16391 expression (&ex);
a1facbec 16392 mips_clear_insn_labels ();
10181a0d
AO
16393
16394 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16395 {
1661c76c 16396 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
16397 ignore_rest_of_line ();
16398 }
16399
16400 p = frag_more (8);
17a2f251 16401 md_number_to_chars (p, 0, 8);
a105a300 16402 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 16403 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
16404
16405 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
16406 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
16407 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
16408
16409 demand_empty_rest_of_line ();
16410}
16411
a3f278e2
CM
16412/* Handle the .ehword pseudo-op. This is used when generating unwinding
16413 tables. It generates a R_MIPS_EH reloc. */
16414
16415static void
16416s_ehword (int ignore ATTRIBUTE_UNUSED)
16417{
16418 expressionS ex;
16419 char *p;
16420
16421 mips_emit_delays ();
16422
16423 expression (&ex);
16424 mips_clear_insn_labels ();
16425
16426 if (ex.X_op != O_symbol || ex.X_add_number != 0)
16427 {
1661c76c 16428 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
16429 ignore_rest_of_line ();
16430 }
16431
16432 p = frag_more (4);
16433 md_number_to_chars (p, 0, 4);
16434 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 16435 BFD_RELOC_32_PCREL);
a3f278e2
CM
16436
16437 demand_empty_rest_of_line ();
16438}
16439
252b5132
RH
16440/* Handle the .cpadd pseudo-op. This is used when dealing with switch
16441 tables in SVR4 PIC code. */
16442
16443static void
17a2f251 16444s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 16445{
252b5132
RH
16446 int reg;
16447
919731af 16448 file_mips_check_options ();
16449
10181a0d
AO
16450 /* This is ignored when not generating SVR4 PIC code. */
16451 if (mips_pic != SVR4_PIC)
252b5132
RH
16452 {
16453 s_ignore (0);
16454 return;
16455 }
16456
8a75745d
MR
16457 mips_mark_labels ();
16458 mips_assembling_insn = TRUE;
16459
252b5132 16460 /* Add $gp to the register named as an argument. */
584892a6 16461 macro_start ();
252b5132 16462 reg = tc_get_register (0);
67c0d1eb 16463 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 16464 macro_end ();
252b5132 16465
8a75745d 16466 mips_assembling_insn = FALSE;
bdaaa2e1 16467 demand_empty_rest_of_line ();
252b5132
RH
16468}
16469
16470/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 16471 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
16472 such as generating jalx instructions when needed. We also make
16473 them odd for the duration of the assembly, in order to generate the
16474 right sort of code. We will make them even in the adjust_symtab
16475 routine, while leaving them marked. This is convenient for the
16476 debugger and the disassembler. The linker knows to make them odd
16477 again. */
16478
16479static void
17a2f251 16480s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 16481{
7bb01e2d
MR
16482 file_mips_check_options ();
16483 file_ase_mips16 |= mips_opts.mips16;
16484 file_ase_micromips |= mips_opts.micromips;
16485
df58fc94 16486 mips_mark_labels ();
252b5132
RH
16487
16488 demand_empty_rest_of_line ();
16489}
16490
ba92f887
MR
16491/* Handle the .nan pseudo-op. */
16492
16493static void
16494s_nan (int ignore ATTRIBUTE_UNUSED)
16495{
16496 static const char str_legacy[] = "legacy";
16497 static const char str_2008[] = "2008";
16498 size_t i;
16499
16500 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
16501
16502 if (i == sizeof (str_2008) - 1
16503 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 16504 mips_nan2008 = 1;
ba92f887
MR
16505 else if (i == sizeof (str_legacy) - 1
16506 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
16507 {
16508 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
16509 mips_nan2008 = 0;
16510 else
16511 as_bad (_("`%s' does not support legacy NaN"),
16512 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
16513 }
ba92f887 16514 else
1661c76c 16515 as_bad (_("bad .nan directive"));
ba92f887
MR
16516
16517 input_line_pointer += i;
16518 demand_empty_rest_of_line ();
16519}
16520
754e2bb9
RS
16521/* Handle a .stab[snd] directive. Ideally these directives would be
16522 implemented in a transparent way, so that removing them would not
16523 have any effect on the generated instructions. However, s_stab
16524 internally changes the section, so in practice we need to decide
16525 now whether the preceding label marks compressed code. We do not
16526 support changing the compression mode of a label after a .stab*
16527 directive, such as in:
16528
16529 foo:
134c0c8b 16530 .stabs ...
754e2bb9
RS
16531 .set mips16
16532
16533 so the current mode wins. */
252b5132
RH
16534
16535static void
17a2f251 16536s_mips_stab (int type)
252b5132 16537{
754e2bb9 16538 mips_mark_labels ();
252b5132
RH
16539 s_stab (type);
16540}
16541
54f4ddb3 16542/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
16543
16544static void
17a2f251 16545s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16546{
16547 char *name;
16548 int c;
16549 symbolS *symbolP;
16550 expressionS exp;
16551
d02603dc 16552 c = get_symbol_name (&name);
252b5132
RH
16553 symbolP = symbol_find_or_make (name);
16554 S_SET_WEAK (symbolP);
16555 *input_line_pointer = c;
16556
d02603dc 16557 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
16558
16559 if (! is_end_of_line[(unsigned char) *input_line_pointer])
16560 {
16561 if (S_IS_DEFINED (symbolP))
16562 {
20203fb9 16563 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
16564 S_GET_NAME (symbolP));
16565 ignore_rest_of_line ();
16566 return;
16567 }
bdaaa2e1 16568
252b5132
RH
16569 if (*input_line_pointer == ',')
16570 {
16571 ++input_line_pointer;
16572 SKIP_WHITESPACE ();
16573 }
bdaaa2e1 16574
252b5132
RH
16575 expression (&exp);
16576 if (exp.X_op != O_symbol)
16577 {
20203fb9 16578 as_bad (_("bad .weakext directive"));
98d3f06f 16579 ignore_rest_of_line ();
252b5132
RH
16580 return;
16581 }
49309057 16582 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
16583 }
16584
16585 demand_empty_rest_of_line ();
16586}
16587
16588/* Parse a register string into a number. Called from the ECOFF code
16589 to parse .frame. The argument is non-zero if this is the frame
16590 register, so that we can record it in mips_frame_reg. */
16591
16592int
17a2f251 16593tc_get_register (int frame)
252b5132 16594{
707bfff6 16595 unsigned int reg;
252b5132
RH
16596
16597 SKIP_WHITESPACE ();
707bfff6
TS
16598 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
16599 reg = 0;
252b5132 16600 if (frame)
7a621144
DJ
16601 {
16602 mips_frame_reg = reg != 0 ? reg : SP;
16603 mips_frame_reg_valid = 1;
16604 mips_cprestore_valid = 0;
16605 }
252b5132
RH
16606 return reg;
16607}
16608
16609valueT
17a2f251 16610md_section_align (asection *seg, valueT addr)
252b5132
RH
16611{
16612 int align = bfd_get_section_alignment (stdoutput, seg);
16613
f3ded42a
RS
16614 /* We don't need to align ELF sections to the full alignment.
16615 However, Irix 5 may prefer that we align them at least to a 16
16616 byte boundary. We don't bother to align the sections if we
16617 are targeted for an embedded system. */
16618 if (strncmp (TARGET_OS, "elf", 3) == 0)
16619 return addr;
16620 if (align > 4)
16621 align = 4;
252b5132 16622
8d3842cd 16623 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
16624}
16625
16626/* Utility routine, called from above as well. If called while the
16627 input file is still being read, it's only an approximation. (For
16628 example, a symbol may later become defined which appeared to be
16629 undefined earlier.) */
16630
16631static int
17a2f251 16632nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
16633{
16634 if (sym == 0)
16635 return 0;
16636
4d0d148d 16637 if (g_switch_value > 0)
252b5132
RH
16638 {
16639 const char *symname;
16640 int change;
16641
c9914766 16642 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
16643 register. It can be if it is smaller than the -G size or if
16644 it is in the .sdata or .sbss section. Certain symbols can
c9914766 16645 not be referenced off the $gp, although it appears as though
252b5132
RH
16646 they can. */
16647 symname = S_GET_NAME (sym);
16648 if (symname != (const char *) NULL
16649 && (strcmp (symname, "eprol") == 0
16650 || strcmp (symname, "etext") == 0
16651 || strcmp (symname, "_gp") == 0
16652 || strcmp (symname, "edata") == 0
16653 || strcmp (symname, "_fbss") == 0
16654 || strcmp (symname, "_fdata") == 0
16655 || strcmp (symname, "_ftext") == 0
16656 || strcmp (symname, "end") == 0
16657 || strcmp (symname, "_gp_disp") == 0))
16658 change = 1;
16659 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
16660 && (0
16661#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
16662 || (symbol_get_obj (sym)->ecoff_extern_size != 0
16663 && (symbol_get_obj (sym)->ecoff_extern_size
16664 <= g_switch_value))
252b5132
RH
16665#endif
16666 /* We must defer this decision until after the whole
16667 file has been read, since there might be a .extern
16668 after the first use of this symbol. */
16669 || (before_relaxing
16670#ifndef NO_ECOFF_DEBUGGING
49309057 16671 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
16672#endif
16673 && S_GET_VALUE (sym) == 0)
16674 || (S_GET_VALUE (sym) != 0
16675 && S_GET_VALUE (sym) <= g_switch_value)))
16676 change = 0;
16677 else
16678 {
16679 const char *segname;
16680
16681 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 16682 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
16683 && strcmp (segname, ".lit4") != 0);
16684 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
16685 && strcmp (segname, ".sbss") != 0
16686 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
16687 && strncmp (segname, ".sbss.", 6) != 0
16688 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 16689 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
16690 }
16691 return change;
16692 }
16693 else
c9914766 16694 /* We are not optimizing for the $gp register. */
252b5132
RH
16695 return 1;
16696}
16697
5919d012
RS
16698
16699/* Return true if the given symbol should be considered local for SVR4 PIC. */
16700
16701static bfd_boolean
17a2f251 16702pic_need_relax (symbolS *sym, asection *segtype)
5919d012
RS
16703{
16704 asection *symsec;
5919d012
RS
16705
16706 /* Handle the case of a symbol equated to another symbol. */
16707 while (symbol_equated_reloc_p (sym))
16708 {
16709 symbolS *n;
16710
5f0fe04b 16711 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
16712 n = symbol_get_value_expression (sym)->X_add_symbol;
16713 if (n == sym)
16714 break;
16715 sym = n;
16716 }
16717
df1f3cda
DD
16718 if (symbol_section_p (sym))
16719 return TRUE;
16720
5919d012
RS
16721 symsec = S_GET_SEGMENT (sym);
16722
5919d012 16723 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
16724 return (!bfd_is_und_section (symsec)
16725 && !bfd_is_abs_section (symsec)
5f0fe04b
TS
16726 && !bfd_is_com_section (symsec)
16727 && !s_is_linkonce (sym, segtype)
5919d012 16728 /* A global or weak symbol is treated as external. */
f3ded42a 16729 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012
RS
16730}
16731
16732
252b5132
RH
16733/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
16734 extended opcode. SEC is the section the frag is in. */
16735
16736static int
17a2f251 16737mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132
RH
16738{
16739 int type;
3ccad066 16740 const struct mips_int_operand *operand;
252b5132 16741 offsetT val;
252b5132 16742 segT symsec;
98aa84af 16743 fragS *sym_frag;
252b5132
RH
16744
16745 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
16746 return 0;
16747 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
16748 return 1;
16749
88a7ef16 16750 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 16751 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 16752 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
16753 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
16754 || (operand->root.type == OP_PCREL
16755 ? sec != symsec
16756 : !bfd_is_abs_section (symsec)))
16757 return 1;
252b5132 16758
98aa84af 16759 sym_frag = symbol_get_frag (fragp->fr_symbol);
88a7ef16 16760 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 16761
3ccad066 16762 if (operand->root.type == OP_PCREL)
252b5132 16763 {
3ccad066 16764 const struct mips_pcrel_operand *pcrel_op;
252b5132 16765 addressT addr;
3ccad066 16766 offsetT maxtiny;
252b5132 16767
88a7ef16
MR
16768 if (RELAX_MIPS16_LONG_BRANCH (fragp->fr_subtype))
16769 return 1;
252b5132 16770
88a7ef16 16771 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 16772
88a7ef16
MR
16773 /* If the relax_marker of the symbol fragment differs from the
16774 relax_marker of this fragment, we have not yet adjusted the
16775 symbol fragment fr_address. We want to add in STRETCH in
16776 order to get a better estimate of the address. This
16777 particularly matters because of the shift bits. */
252b5132 16778 if (stretch != 0
98aa84af 16779 && sym_frag->relax_marker != fragp->relax_marker)
252b5132
RH
16780 {
16781 fragS *f;
16782
16783 /* Adjust stretch for any alignment frag. Note that if have
16784 been expanding the earlier code, the symbol may be
16785 defined in what appears to be an earlier frag. FIXME:
16786 This doesn't handle the fr_subtype field, which specifies
16787 a maximum number of bytes to skip when doing an
16788 alignment. */
98aa84af 16789 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
252b5132
RH
16790 {
16791 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
16792 {
16793 if (stretch < 0)
16794 stretch = - ((- stretch)
16795 & ~ ((1 << (int) f->fr_offset) - 1));
16796 else
16797 stretch &= ~ ((1 << (int) f->fr_offset) - 1);
16798 if (stretch == 0)
16799 break;
16800 }
16801 }
16802 if (f != NULL)
16803 val += stretch;
16804 }
16805
16806 addr = fragp->fr_address + fragp->fr_fix;
16807
16808 /* The base address rules are complicated. The base address of
16809 a branch is the following instruction. The base address of a
16810 PC relative load or add is the instruction itself, but if it
16811 is in a delay slot (in which case it can not be extended) use
16812 the address of the instruction whose delay slot it is in. */
3ccad066 16813 if (pcrel_op->include_isa_bit)
252b5132
RH
16814 {
16815 addr += 2;
16816
16817 /* If we are currently assuming that this frag should be
16818 extended, then, the current address is two bytes
bdaaa2e1 16819 higher. */
252b5132
RH
16820 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
16821 addr += 2;
16822
16823 /* Ignore the low bit in the target, since it will be set
16824 for a text label. */
3ccad066 16825 val &= -2;
252b5132
RH
16826 }
16827 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
16828 addr -= 4;
16829 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
16830 addr -= 2;
16831
3ccad066 16832 val -= addr & -(1 << pcrel_op->align_log2);
252b5132
RH
16833
16834 /* If any of the shifted bits are set, we must use an extended
16835 opcode. If the address depends on the size of this
16836 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
16837 use an extended opcode. */
16838 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
16839 {
16840 fragp->fr_subtype =
16841 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16842 return 1;
16843 }
16844
16845 /* If we are about to mark a frag as extended because the value
3ccad066
RS
16846 is precisely the next value above maxtiny, then there is a
16847 chance of an infinite loop as in the following code:
252b5132
RH
16848 la $4,foo
16849 .skip 1020
16850 .align 2
16851 foo:
16852 In this case when the la is extended, foo is 0x3fc bytes
16853 away, so the la can be shrunk, but then foo is 0x400 away, so
16854 the la must be extended. To avoid this loop, we mark the
16855 frag as extended if it was small, and is about to become
3ccad066
RS
16856 extended with the next value above maxtiny. */
16857 maxtiny = mips_int_operand_max (operand);
16858 if (val == maxtiny + (1 << operand->shift)
88a7ef16 16859 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
16860 {
16861 fragp->fr_subtype =
16862 RELAX_MIPS16_MARK_LONG_BRANCH (fragp->fr_subtype);
16863 return 1;
16864 }
16865 }
252b5132 16866
3ccad066 16867 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
16868}
16869
4a6a3df4
AO
16870/* Compute the length of a branch sequence, and adjust the
16871 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
16872 worst-case length is computed, with UPDATE being used to indicate
16873 whether an unconditional (-1), branch-likely (+1) or regular (0)
16874 branch is to be computed. */
16875static int
17a2f251 16876relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 16877{
b34976b6 16878 bfd_boolean toofar;
4a6a3df4
AO
16879 int length;
16880
16881 if (fragp
16882 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 16883 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
16884 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16885 {
16886 addressT addr;
16887 offsetT val;
16888
16889 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16890
16891 addr = fragp->fr_address + fragp->fr_fix + 4;
16892
16893 val -= addr;
16894
16895 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
16896 }
4a6a3df4 16897 else
c1f61bd2
MR
16898 /* If the symbol is not defined or it's in a different segment,
16899 we emit the long sequence. */
b34976b6 16900 toofar = TRUE;
4a6a3df4
AO
16901
16902 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
16903 fragp->fr_subtype
66b3e8da
MR
16904 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
16905 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
16906 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
16907 RELAX_BRANCH_LINK (fragp->fr_subtype),
16908 toofar);
16909
16910 length = 4;
16911 if (toofar)
16912 {
16913 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
16914 length += 8;
16915
16916 if (mips_pic != NO_PIC)
16917 {
16918 /* Additional space for PIC loading of target address. */
16919 length += 8;
16920 if (mips_opts.isa == ISA_MIPS1)
16921 /* Additional space for $at-stabilizing nop. */
16922 length += 4;
16923 }
16924
16925 /* If branch is conditional. */
16926 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
16927 length += 8;
16928 }
b34976b6 16929
4a6a3df4
AO
16930 return length;
16931}
16932
df58fc94
RS
16933/* Compute the length of a branch sequence, and adjust the
16934 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
16935 worst-case length is computed, with UPDATE being used to indicate
16936 whether an unconditional (-1), or regular (0) branch is to be
16937 computed. */
16938
16939static int
16940relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
16941{
16942 bfd_boolean toofar;
16943 int length;
16944
16945 if (fragp
16946 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 16947 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
16948 && sec == S_GET_SEGMENT (fragp->fr_symbol))
16949 {
16950 addressT addr;
16951 offsetT val;
16952
16953 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
16954 /* Ignore the low bit in the target, since it will be set
16955 for a text label. */
16956 if ((val & 1) != 0)
16957 --val;
16958
16959 addr = fragp->fr_address + fragp->fr_fix + 4;
16960
16961 val -= addr;
16962
16963 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
16964 }
df58fc94 16965 else
c1f61bd2
MR
16966 /* If the symbol is not defined or it's in a different segment,
16967 we emit the long sequence. */
df58fc94
RS
16968 toofar = TRUE;
16969
16970 if (fragp && update
16971 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
16972 fragp->fr_subtype = (toofar
16973 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
16974 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
16975
16976 length = 4;
16977 if (toofar)
16978 {
16979 bfd_boolean compact_known = fragp != NULL;
16980 bfd_boolean compact = FALSE;
16981 bfd_boolean uncond;
16982
16983 if (compact_known)
16984 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
16985 if (fragp)
16986 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
16987 else
16988 uncond = update < 0;
16989
16990 /* If label is out of range, we turn branch <br>:
16991
16992 <br> label # 4 bytes
16993 0:
16994
16995 into:
16996
16997 j label # 4 bytes
16998 nop # 2 bytes if compact && !PIC
16999 0:
17000 */
17001 if (mips_pic == NO_PIC && (!compact_known || compact))
17002 length += 2;
17003
17004 /* If assembling PIC code, we further turn:
17005
17006 j label # 4 bytes
17007
17008 into:
17009
17010 lw/ld at, %got(label)(gp) # 4 bytes
17011 d/addiu at, %lo(label) # 4 bytes
17012 jr/c at # 2 bytes
17013 */
17014 if (mips_pic != NO_PIC)
17015 length += 6;
17016
17017 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17018
17019 <brneg> 0f # 4 bytes
17020 nop # 2 bytes if !compact
17021 */
17022 if (!uncond)
17023 length += (compact_known && compact) ? 4 : 6;
17024 }
17025
17026 return length;
17027}
17028
17029/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17030 bit accordingly. */
17031
17032static int
17033relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17034{
17035 bfd_boolean toofar;
17036
df58fc94
RS
17037 if (fragp
17038 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17039 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17040 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17041 {
17042 addressT addr;
17043 offsetT val;
17044 int type;
17045
17046 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17047 /* Ignore the low bit in the target, since it will be set
17048 for a text label. */
17049 if ((val & 1) != 0)
17050 --val;
17051
17052 /* Assume this is a 2-byte branch. */
17053 addr = fragp->fr_address + fragp->fr_fix + 2;
17054
17055 /* We try to avoid the infinite loop by not adding 2 more bytes for
17056 long branches. */
17057
17058 val -= addr;
17059
17060 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17061 if (type == 'D')
17062 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17063 else if (type == 'E')
17064 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17065 else
17066 abort ();
17067 }
17068 else
17069 /* If the symbol is not defined or it's in a different segment,
17070 we emit a normal 32-bit branch. */
17071 toofar = TRUE;
17072
17073 if (fragp && update
17074 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17075 fragp->fr_subtype
17076 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17077 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17078
17079 if (toofar)
17080 return 4;
17081
17082 return 2;
17083}
17084
252b5132
RH
17085/* Estimate the size of a frag before relaxing. Unless this is the
17086 mips16, we are not really relaxing here, and the final size is
17087 encoded in the subtype information. For the mips16, we have to
17088 decide whether we are using an extended opcode or not. */
17089
252b5132 17090int
17a2f251 17091md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17092{
5919d012 17093 int change;
252b5132 17094
4a6a3df4
AO
17095 if (RELAX_BRANCH_P (fragp->fr_subtype))
17096 {
17097
b34976b6
AM
17098 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17099
4a6a3df4
AO
17100 return fragp->fr_var;
17101 }
17102
252b5132 17103 if (RELAX_MIPS16_P (fragp->fr_subtype))
177b4a6a
AO
17104 /* We don't want to modify the EXTENDED bit here; it might get us
17105 into infinite loops. We change it only in mips_relax_frag(). */
17106 return (RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2);
252b5132 17107
df58fc94
RS
17108 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17109 {
17110 int length = 4;
17111
17112 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17113 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17114 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17115 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17116 fragp->fr_var = length;
17117
17118 return length;
17119 }
17120
252b5132 17121 if (mips_pic == NO_PIC)
5919d012 17122 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132 17123 else if (mips_pic == SVR4_PIC)
5919d012 17124 change = pic_need_relax (fragp->fr_symbol, segtype);
0a44bf69
RS
17125 else if (mips_pic == VXWORKS_PIC)
17126 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17127 change = 0;
252b5132
RH
17128 else
17129 abort ();
17130
17131 if (change)
17132 {
4d7206a2 17133 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17134 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17135 }
4d7206a2
RS
17136 else
17137 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17138}
17139
17140/* This is called to see whether a reloc against a defined symbol
de7e6852 17141 should be converted into a reloc against a section. */
252b5132
RH
17142
17143int
17a2f251 17144mips_fix_adjustable (fixS *fixp)
252b5132 17145{
252b5132
RH
17146 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17147 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17148 return 0;
a161fe53 17149
252b5132
RH
17150 if (fixp->fx_addsy == NULL)
17151 return 1;
a161fe53 17152
2f0c68f2
CM
17153 /* Allow relocs used for EH tables. */
17154 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17155 return 1;
17156
de7e6852
RS
17157 /* If symbol SYM is in a mergeable section, relocations of the form
17158 SYM + 0 can usually be made section-relative. The mergeable data
17159 is then identified by the section offset rather than by the symbol.
17160
17161 However, if we're generating REL LO16 relocations, the offset is split
17162 between the LO16 and parterning high part relocation. The linker will
17163 need to recalculate the complete offset in order to correctly identify
17164 the merge data.
17165
17166 The linker has traditionally not looked for the parterning high part
17167 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17168 placed anywhere. Rather than break backwards compatibility by changing
17169 this, it seems better not to force the issue, and instead keep the
17170 original symbol. This will work with either linker behavior. */
738e5348 17171 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17172 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17173 && HAVE_IN_PLACE_ADDENDS
17174 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17175 return 0;
17176
97f50151
MR
17177 /* There is no place to store an in-place offset for JALR relocations. */
17178 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17179 return 0;
17180
17181 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17182 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17183 start address of the symbol's containing section.
17184
17185 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17186 section relative to allow linker relaxations to be performed later on. */
97f50151 17187 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17188 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17189 return 0;
17190
b314ec0e
RS
17191 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17192 to a floating-point stub. The same is true for non-R_MIPS16_26
17193 relocations against MIPS16 functions; in this case, the stub becomes
17194 the function's canonical address.
17195
17196 Floating-point stubs are stored in unique .mips16.call.* or
17197 .mips16.fn.* sections. If a stub T for function F is in section S,
17198 the first relocation in section S must be against F; this is how the
17199 linker determines the target function. All relocations that might
17200 resolve to T must also be against F. We therefore have the following
17201 restrictions, which are given in an intentionally-redundant way:
17202
17203 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17204 symbols.
17205
17206 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17207 if that stub might be used.
17208
17209 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17210 symbols.
17211
17212 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17213 that stub might be used.
17214
17215 There is a further restriction:
17216
df58fc94 17217 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c
MR
17218 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
17219 R_MIPS_PC21_S2, R_MIPS_PC16, R_MICROMIPS_PC16_S1,
17220 R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1) against MIPS16 or
17221 microMIPS symbols because we need to keep the MIPS16 or
17222 microMIPS symbol for the purpose of mode mismatch detection
17223 and JAL to JALX instruction conversion in the linker.
b314ec0e 17224
df58fc94 17225 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 17226 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 17227 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
17228
17229 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17230 relocation against some symbol R, no relocation against R may be
17231 reduced. (Note that this deals with (2) as well as (1) because
17232 relocations against global symbols will never be reduced on ELF
17233 targets.) This approach is a little simpler than trying to detect
17234 stub sections, and gives the "all or nothing" per-symbol consistency
17235 that we have for MIPS16 symbols. */
f3ded42a 17236 if (fixp->fx_subsy == NULL
30c09090 17237 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 17238 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
17239 && (jmp_reloc_p (fixp->fx_r_type)
17240 || b_reloc_p (fixp->fx_r_type)))
44d3da23 17241 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 17242 return 0;
a161fe53 17243
252b5132
RH
17244 return 1;
17245}
17246
17247/* Translate internal representation of relocation info to BFD target
17248 format. */
17249
17250arelent **
17a2f251 17251tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
17252{
17253 static arelent *retval[4];
17254 arelent *reloc;
17255 bfd_reloc_code_real_type code;
17256
4b0cff4e 17257 memset (retval, 0, sizeof(retval));
325801bd
TS
17258 reloc = retval[0] = XCNEW (arelent);
17259 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 17260 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
17261 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
17262
bad36eac
DJ
17263 if (fixp->fx_pcrel)
17264 {
df58fc94
RS
17265 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
17266 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
17267 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 17268 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
17269 || fixp->fx_r_type == BFD_RELOC_32_PCREL
17270 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
17271 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
17272 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
17273 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
17274 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
17275 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
17276
17277 /* At this point, fx_addnumber is "symbol offset - pcrel address".
17278 Relocations want only the symbol offset. */
51f6035b
MR
17279 switch (fixp->fx_r_type)
17280 {
17281 case BFD_RELOC_MIPS_18_PCREL_S3:
17282 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
17283 break;
17284 default:
17285 reloc->addend = fixp->fx_addnumber + reloc->address;
17286 break;
17287 }
bad36eac 17288 }
17c6c9d9
MR
17289 else if (HAVE_IN_PLACE_ADDENDS
17290 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
17291 && (read_compressed_insn (fixp->fx_frag->fr_literal
17292 + fixp->fx_where, 4) >> 26) == 0x3c)
17293 {
17294 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
17295 addend accordingly. */
17296 reloc->addend = fixp->fx_addnumber >> 1;
17297 }
bad36eac
DJ
17298 else
17299 reloc->addend = fixp->fx_addnumber;
252b5132 17300
438c16b8
TS
17301 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
17302 entry to be used in the relocation's section offset. */
17303 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
17304 {
17305 reloc->address = reloc->addend;
17306 reloc->addend = 0;
17307 }
17308
252b5132 17309 code = fixp->fx_r_type;
252b5132 17310
bad36eac 17311 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
17312 if (reloc->howto == NULL)
17313 {
17314 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
17315 _("cannot represent %s relocation in this object file"
17316 " format"),
252b5132
RH
17317 bfd_get_reloc_code_name (code));
17318 retval[0] = NULL;
17319 }
17320
17321 return retval;
17322}
17323
17324/* Relax a machine dependent frag. This returns the amount by which
17325 the current size of the frag should change. */
17326
17327int
17a2f251 17328mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 17329{
4a6a3df4
AO
17330 if (RELAX_BRANCH_P (fragp->fr_subtype))
17331 {
17332 offsetT old_var = fragp->fr_var;
b34976b6
AM
17333
17334 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
17335
17336 return fragp->fr_var - old_var;
17337 }
17338
df58fc94
RS
17339 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17340 {
17341 offsetT old_var = fragp->fr_var;
17342 offsetT new_var = 4;
17343
17344 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17345 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
17346 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17347 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
17348 fragp->fr_var = new_var;
17349
17350 return new_var - old_var;
17351 }
17352
252b5132
RH
17353 if (! RELAX_MIPS16_P (fragp->fr_subtype))
17354 return 0;
17355
88a7ef16 17356 if (mips16_extended_frag (fragp, sec, stretch))
252b5132
RH
17357 {
17358 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17359 return 0;
17360 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
17361 return 2;
17362 }
17363 else
17364 {
17365 if (! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17366 return 0;
17367 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
17368 return -2;
17369 }
17370
17371 return 0;
17372}
17373
17374/* Convert a machine dependent frag. */
17375
17376void
17a2f251 17377md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 17378{
4a6a3df4
AO
17379 if (RELAX_BRANCH_P (fragp->fr_subtype))
17380 {
4d68580a 17381 char *buf;
4a6a3df4
AO
17382 unsigned long insn;
17383 expressionS exp;
17384 fixS *fixp;
b34976b6 17385
4d68580a
RS
17386 buf = fragp->fr_literal + fragp->fr_fix;
17387 insn = read_insn (buf);
b34976b6 17388
4a6a3df4
AO
17389 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17390 {
17391 /* We generate a fixup instead of applying it right now
17392 because, if there are linker relaxations, we're going to
17393 need the relocations. */
17394 exp.X_op = O_symbol;
17395 exp.X_add_symbol = fragp->fr_symbol;
17396 exp.X_add_number = fragp->fr_offset;
17397
4d68580a
RS
17398 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17399 BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
17400 fixp->fx_file = fragp->fr_file;
17401 fixp->fx_line = fragp->fr_line;
b34976b6 17402
4d68580a 17403 buf = write_insn (buf, insn);
4a6a3df4
AO
17404 }
17405 else
17406 {
17407 int i;
17408
17409 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 17410 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
17411
17412 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
17413 goto uncond;
17414
17415 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17416 {
17417 /* Reverse the branch. */
17418 switch ((insn >> 28) & 0xf)
17419 {
17420 case 4:
56d438b1
CF
17421 if ((insn & 0xff000000) == 0x47000000
17422 || (insn & 0xff600000) == 0x45600000)
17423 {
17424 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
17425 reversed by tweaking bit 23. */
17426 insn ^= 0x00800000;
17427 }
17428 else
17429 {
17430 /* bc[0-3][tf]l? instructions can have the condition
17431 reversed by tweaking a single TF bit, and their
17432 opcodes all have 0x4???????. */
17433 gas_assert ((insn & 0xf3e00000) == 0x41000000);
17434 insn ^= 0x00010000;
17435 }
4a6a3df4
AO
17436 break;
17437
17438 case 0:
17439 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 17440 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 17441 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
17442 insn ^= 0x00010000;
17443 break;
b34976b6 17444
4a6a3df4
AO
17445 case 1:
17446 /* beq 0x10000000 bne 0x14000000
54f4ddb3 17447 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
17448 insn ^= 0x04000000;
17449 break;
17450
17451 default:
17452 abort ();
17453 }
17454 }
17455
17456 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
17457 {
17458 /* Clear the and-link bit. */
9c2799c2 17459 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 17460
54f4ddb3
TS
17461 /* bltzal 0x04100000 bgezal 0x04110000
17462 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
17463 insn &= ~0x00100000;
17464 }
17465
17466 /* Branch over the branch (if the branch was likely) or the
17467 full jump (not likely case). Compute the offset from the
17468 current instruction to branch to. */
17469 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17470 i = 16;
17471 else
17472 {
17473 /* How many bytes in instructions we've already emitted? */
4d68580a 17474 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
17475 /* How many bytes in instructions from here to the end? */
17476 i = fragp->fr_var - i;
17477 }
17478 /* Convert to instruction count. */
17479 i >>= 2;
17480 /* Branch counts from the next instruction. */
b34976b6 17481 i--;
4a6a3df4
AO
17482 insn |= i;
17483 /* Branch over the jump. */
4d68580a 17484 buf = write_insn (buf, insn);
4a6a3df4 17485
54f4ddb3 17486 /* nop */
4d68580a 17487 buf = write_insn (buf, 0);
4a6a3df4
AO
17488
17489 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
17490 {
17491 /* beql $0, $0, 2f */
17492 insn = 0x50000000;
17493 /* Compute the PC offset from the current instruction to
17494 the end of the variable frag. */
17495 /* How many bytes in instructions we've already emitted? */
4d68580a 17496 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
17497 /* How many bytes in instructions from here to the end? */
17498 i = fragp->fr_var - i;
17499 /* Convert to instruction count. */
17500 i >>= 2;
17501 /* Don't decrement i, because we want to branch over the
17502 delay slot. */
4a6a3df4 17503 insn |= i;
4a6a3df4 17504
4d68580a
RS
17505 buf = write_insn (buf, insn);
17506 buf = write_insn (buf, 0);
4a6a3df4
AO
17507 }
17508
17509 uncond:
17510 if (mips_pic == NO_PIC)
17511 {
17512 /* j or jal. */
17513 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
17514 ? 0x0c000000 : 0x08000000);
17515 exp.X_op = O_symbol;
17516 exp.X_add_symbol = fragp->fr_symbol;
17517 exp.X_add_number = fragp->fr_offset;
17518
4d68580a
RS
17519 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17520 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
17521 fixp->fx_file = fragp->fr_file;
17522 fixp->fx_line = fragp->fr_line;
17523
4d68580a 17524 buf = write_insn (buf, insn);
4a6a3df4
AO
17525 }
17526 else
17527 {
66b3e8da
MR
17528 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
17529
4a6a3df4 17530 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
17531 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
17532 insn |= at << OP_SH_RT;
4a6a3df4
AO
17533 exp.X_op = O_symbol;
17534 exp.X_add_symbol = fragp->fr_symbol;
17535 exp.X_add_number = fragp->fr_offset;
17536
17537 if (fragp->fr_offset)
17538 {
17539 exp.X_add_symbol = make_expr_symbol (&exp);
17540 exp.X_add_number = 0;
17541 }
17542
4d68580a
RS
17543 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17544 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
17545 fixp->fx_file = fragp->fr_file;
17546 fixp->fx_line = fragp->fr_line;
17547
4d68580a 17548 buf = write_insn (buf, insn);
b34976b6 17549
4a6a3df4 17550 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
17551 /* nop */
17552 buf = write_insn (buf, 0);
4a6a3df4
AO
17553
17554 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
17555 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
17556 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 17557
4d68580a
RS
17558 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp,
17559 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
17560 fixp->fx_file = fragp->fr_file;
17561 fixp->fx_line = fragp->fr_line;
b34976b6 17562
4d68580a 17563 buf = write_insn (buf, insn);
4a6a3df4
AO
17564
17565 /* j(al)r $at. */
17566 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 17567 insn = 0x0000f809;
4a6a3df4 17568 else
66b3e8da
MR
17569 insn = 0x00000008;
17570 insn |= at << OP_SH_RS;
4a6a3df4 17571
4d68580a 17572 buf = write_insn (buf, insn);
4a6a3df4
AO
17573 }
17574 }
17575
4a6a3df4 17576 fragp->fr_fix += fragp->fr_var;
4d68580a 17577 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
17578 return;
17579 }
17580
df58fc94
RS
17581 /* Relax microMIPS branches. */
17582 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17583 {
4d68580a 17584 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
17585 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17586 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17587 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 17588 bfd_boolean short_ds;
df58fc94
RS
17589 unsigned long insn;
17590 expressionS exp;
17591 fixS *fixp;
17592
17593 exp.X_op = O_symbol;
17594 exp.X_add_symbol = fragp->fr_symbol;
17595 exp.X_add_number = fragp->fr_offset;
17596
17597 fragp->fr_fix += fragp->fr_var;
17598
17599 /* Handle 16-bit branches that fit or are forced to fit. */
17600 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17601 {
17602 /* We generate a fixup instead of applying it right now,
17603 because if there is linker relaxation, we're going to
17604 need the relocations. */
17605 if (type == 'D')
4d68580a 17606 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
17607 BFD_RELOC_MICROMIPS_10_PCREL_S1);
17608 else if (type == 'E')
4d68580a 17609 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 2, &exp, TRUE,
df58fc94
RS
17610 BFD_RELOC_MICROMIPS_7_PCREL_S1);
17611 else
17612 abort ();
17613
17614 fixp->fx_file = fragp->fr_file;
17615 fixp->fx_line = fragp->fr_line;
17616
17617 /* These relocations can have an addend that won't fit in
17618 2 octets. */
17619 fixp->fx_no_overflow = 1;
17620
17621 return;
17622 }
17623
2309ddf2 17624 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
17625 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17626 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17627 {
17628 /* We generate a fixup instead of applying it right now,
17629 because if there is linker relaxation, we're going to
17630 need the relocations. */
4d68580a
RS
17631 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, TRUE,
17632 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
17633 fixp->fx_file = fragp->fr_file;
17634 fixp->fx_line = fragp->fr_line;
17635
17636 if (type == 0)
17637 return;
17638 }
17639
17640 /* Relax 16-bit branches to 32-bit branches. */
17641 if (type != 0)
17642 {
4d68580a 17643 insn = read_compressed_insn (buf, 2);
df58fc94
RS
17644
17645 if ((insn & 0xfc00) == 0xcc00) /* b16 */
17646 insn = 0x94000000; /* beq */
17647 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
17648 {
17649 unsigned long regno;
17650
17651 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
17652 regno = micromips_to_32_reg_d_map [regno];
17653 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
17654 insn |= regno << MICROMIPSOP_SH_RS;
17655 }
17656 else
17657 abort ();
17658
17659 /* Nothing else to do, just write it out. */
17660 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
17661 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17662 {
4d68580a
RS
17663 buf = write_compressed_insn (buf, insn, 4);
17664 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17665 return;
17666 }
17667 }
17668 else
4d68580a 17669 insn = read_compressed_insn (buf, 4);
df58fc94
RS
17670
17671 /* Relax 32-bit branches to a sequence of instructions. */
17672 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 17673 _("relaxed out-of-range branch into a jump"));
df58fc94 17674
2309ddf2
MR
17675 /* Set the short-delay-slot bit. */
17676 short_ds = al && (insn & 0x02000000) != 0;
df58fc94
RS
17677
17678 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
17679 {
17680 symbolS *l;
17681
17682 /* Reverse the branch. */
17683 if ((insn & 0xfc000000) == 0x94000000 /* beq */
17684 || (insn & 0xfc000000) == 0xb4000000) /* bne */
17685 insn ^= 0x20000000;
17686 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
17687 || (insn & 0xffe00000) == 0x40400000 /* bgez */
17688 || (insn & 0xffe00000) == 0x40800000 /* blez */
17689 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
17690 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
17691 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
17692 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
17693 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
17694 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
17695 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
17696 insn ^= 0x00400000;
17697 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
17698 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
17699 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
17700 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
17701 insn ^= 0x00200000;
56d438b1
CF
17702 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
17703 BNZ.df */
17704 || (insn & 0xff600000) == 0x81600000) /* BZ.V
17705 BNZ.V */
17706 insn ^= 0x00800000;
df58fc94
RS
17707 else
17708 abort ();
17709
17710 if (al)
17711 {
17712 /* Clear the and-link and short-delay-slot bits. */
17713 gas_assert ((insn & 0xfda00000) == 0x40200000);
17714
17715 /* bltzal 0x40200000 bgezal 0x40600000 */
17716 /* bltzals 0x42200000 bgezals 0x42600000 */
17717 insn &= ~0x02200000;
17718 }
17719
17720 /* Make a label at the end for use with the branch. */
17721 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
17722 micromips_label_inc ();
f3ded42a 17723 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
17724
17725 /* Refer to it. */
4d68580a
RS
17726 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
17727 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
17728 fixp->fx_file = fragp->fr_file;
17729 fixp->fx_line = fragp->fr_line;
17730
17731 /* Branch over the jump. */
4d68580a 17732 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17733 if (!compact)
4d68580a
RS
17734 /* nop */
17735 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17736 }
17737
17738 if (mips_pic == NO_PIC)
17739 {
2309ddf2
MR
17740 unsigned long jal = short_ds ? 0x74000000 : 0xf4000000; /* jal/s */
17741
df58fc94
RS
17742 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
17743 insn = al ? jal : 0xd4000000;
17744
4d68580a
RS
17745 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17746 BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
17747 fixp->fx_file = fragp->fr_file;
17748 fixp->fx_line = fragp->fr_line;
17749
4d68580a 17750 buf = write_compressed_insn (buf, insn, 4);
df58fc94 17751 if (compact)
4d68580a
RS
17752 /* nop */
17753 buf = write_compressed_insn (buf, 0x0c00, 2);
df58fc94
RS
17754 }
17755 else
17756 {
17757 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
2309ddf2
MR
17758 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
17759 unsigned long jr = compact ? 0x45a0 : 0x4580; /* jr/c */
df58fc94
RS
17760
17761 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
17762 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
17763 insn |= at << MICROMIPSOP_SH_RT;
17764
17765 if (exp.X_add_number)
17766 {
17767 exp.X_add_symbol = make_expr_symbol (&exp);
17768 exp.X_add_number = 0;
17769 }
17770
4d68580a
RS
17771 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17772 BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
17773 fixp->fx_file = fragp->fr_file;
17774 fixp->fx_line = fragp->fr_line;
17775
4d68580a 17776 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17777
17778 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
17779 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
17780 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
17781
4d68580a
RS
17782 fixp = fix_new_exp (fragp, buf - fragp->fr_literal, 4, &exp, FALSE,
17783 BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
17784 fixp->fx_file = fragp->fr_file;
17785 fixp->fx_line = fragp->fr_line;
17786
4d68580a 17787 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
17788
17789 /* jr/jrc/jalr/jalrs $at */
17790 insn = al ? jalr : jr;
17791 insn |= at << MICROMIPSOP_SH_MJ;
17792
4d68580a 17793 buf = write_compressed_insn (buf, insn, 2);
df58fc94
RS
17794 }
17795
4d68580a 17796 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
17797 return;
17798 }
17799
252b5132
RH
17800 if (RELAX_MIPS16_P (fragp->fr_subtype))
17801 {
17802 int type;
3ccad066 17803 const struct mips_int_operand *operand;
252b5132 17804 offsetT val;
5c04167a
RS
17805 char *buf;
17806 unsigned int user_length, length;
252b5132 17807 unsigned long insn;
5c04167a 17808 bfd_boolean ext;
88a7ef16 17809 segT symsec;
252b5132
RH
17810
17811 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17812 operand = mips16_immed_operand (type, FALSE);
252b5132 17813
5c04167a 17814 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 17815 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
3ccad066 17816 if (operand->root.type == OP_PCREL)
252b5132 17817 {
3ccad066 17818 const struct mips_pcrel_operand *pcrel_op;
252b5132
RH
17819 addressT addr;
17820
3ccad066 17821 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132
RH
17822 addr = fragp->fr_address + fragp->fr_fix;
17823
17824 /* The rules for the base address of a PC relative reloc are
17825 complicated; see mips16_extended_frag. */
3ccad066 17826 if (pcrel_op->include_isa_bit)
252b5132
RH
17827 {
17828 addr += 2;
17829 if (ext)
17830 addr += 2;
17831 /* Ignore the low bit in the target, since it will be
17832 set for a text label. */
3ccad066 17833 val &= -2;
252b5132
RH
17834 }
17835 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17836 addr -= 4;
17837 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17838 addr -= 2;
17839
3ccad066 17840 addr &= -(1 << pcrel_op->align_log2);
252b5132
RH
17841 val -= addr;
17842
17843 /* Make sure the section winds up with the alignment we have
17844 assumed. */
3ccad066
RS
17845 if (operand->shift > 0)
17846 record_alignment (asec, operand->shift);
252b5132
RH
17847 }
17848
17849 if (ext
17850 && (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
17851 || RELAX_MIPS16_DSLOT (fragp->fr_subtype)))
17852 as_warn_where (fragp->fr_file, fragp->fr_line,
17853 _("extended instruction in delay slot"));
17854
5c04167a 17855 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 17856
4d68580a 17857 insn = read_compressed_insn (buf, 2);
5c04167a
RS
17858 if (ext)
17859 insn |= MIPS16_EXTEND;
252b5132 17860
5c04167a
RS
17861 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17862 user_length = 4;
17863 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17864 user_length = 2;
17865 else
17866 user_length = 0;
17867
88a7ef16
MR
17868 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17869 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17870 || (operand->root.type == OP_PCREL
17871 ? asec != symsec
17872 : !bfd_is_abs_section (symsec)))
17873 as_bad_where (fragp->fr_file, fragp->fr_line,
17874 _("unsupported relocation"));
17875 else
17876 mips16_immed (fragp->fr_file, fragp->fr_line, type,
17877 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 17878
5c04167a
RS
17879 length = (ext ? 4 : 2);
17880 gas_assert (mips16_opcode_length (insn) == length);
17881 write_compressed_insn (buf, insn, length);
17882 fragp->fr_fix += length;
252b5132
RH
17883 }
17884 else
17885 {
df58fc94
RS
17886 relax_substateT subtype = fragp->fr_subtype;
17887 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
17888 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
17889 int first, second;
17890 fixS *fixp;
252b5132 17891
df58fc94
RS
17892 first = RELAX_FIRST (subtype);
17893 second = RELAX_SECOND (subtype);
4d7206a2 17894 fixp = (fixS *) fragp->fr_opcode;
252b5132 17895
df58fc94
RS
17896 /* If the delay slot chosen does not match the size of the instruction,
17897 then emit a warning. */
17898 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
17899 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
17900 {
17901 relax_substateT s;
17902 const char *msg;
17903
17904 s = subtype & (RELAX_DELAY_SLOT_16BIT
17905 | RELAX_DELAY_SLOT_SIZE_FIRST
17906 | RELAX_DELAY_SLOT_SIZE_SECOND);
17907 msg = macro_warning (s);
17908 if (msg != NULL)
db9b2be4 17909 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
17910 subtype &= ~s;
17911 }
17912
584892a6 17913 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 17914 if (use_second == second_longer)
584892a6 17915 {
df58fc94
RS
17916 relax_substateT s;
17917 const char *msg;
17918
17919 s = (subtype
17920 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
17921 msg = macro_warning (s);
17922 if (msg != NULL)
db9b2be4 17923 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 17924 subtype &= ~s;
584892a6
RS
17925 }
17926
4d7206a2
RS
17927 /* Go through all the fixups for the first sequence. Disable them
17928 (by marking them as done) if we're going to use the second
17929 sequence instead. */
17930 while (fixp
17931 && fixp->fx_frag == fragp
17932 && fixp->fx_where < fragp->fr_fix - second)
17933 {
df58fc94 17934 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17935 fixp->fx_done = 1;
17936 fixp = fixp->fx_next;
17937 }
252b5132 17938
4d7206a2
RS
17939 /* Go through the fixups for the second sequence. Disable them if
17940 we're going to use the first sequence, otherwise adjust their
17941 addresses to account for the relaxation. */
17942 while (fixp && fixp->fx_frag == fragp)
17943 {
df58fc94 17944 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17945 fixp->fx_where -= first;
17946 else
17947 fixp->fx_done = 1;
17948 fixp = fixp->fx_next;
17949 }
17950
17951 /* Now modify the frag contents. */
df58fc94 17952 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
17953 {
17954 char *start;
17955
17956 start = fragp->fr_literal + fragp->fr_fix - first - second;
17957 memmove (start, start + first, second);
17958 fragp->fr_fix -= first;
17959 }
17960 else
17961 fragp->fr_fix -= second;
252b5132
RH
17962 }
17963}
17964
252b5132
RH
17965/* This function is called after the relocs have been generated.
17966 We've been storing mips16 text labels as odd. Here we convert them
17967 back to even for the convenience of the debugger. */
17968
17969void
17a2f251 17970mips_frob_file_after_relocs (void)
252b5132
RH
17971{
17972 asymbol **syms;
17973 unsigned int count, i;
17974
252b5132
RH
17975 syms = bfd_get_outsymbols (stdoutput);
17976 count = bfd_get_symcount (stdoutput);
17977 for (i = 0; i < count; i++, syms++)
df58fc94
RS
17978 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
17979 && ((*syms)->value & 1) != 0)
17980 {
17981 (*syms)->value &= ~1;
17982 /* If the symbol has an odd size, it was probably computed
17983 incorrectly, so adjust that as well. */
17984 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
17985 ++elf_symbol (*syms)->internal_elf_sym.st_size;
17986 }
252b5132
RH
17987}
17988
a1facbec
MR
17989/* This function is called whenever a label is defined, including fake
17990 labels instantiated off the dot special symbol. It is used when
17991 handling branch delays; if a branch has a label, we assume we cannot
17992 move it. This also bumps the value of the symbol by 1 in compressed
17993 code. */
252b5132 17994
e1b47bd5 17995static void
a1facbec 17996mips_record_label (symbolS *sym)
252b5132 17997{
a8dbcb85 17998 segment_info_type *si = seg_info (now_seg);
252b5132
RH
17999 struct insn_label_list *l;
18000
18001 if (free_insn_labels == NULL)
325801bd 18002 l = XNEW (struct insn_label_list);
252b5132
RH
18003 else
18004 {
18005 l = free_insn_labels;
18006 free_insn_labels = l->next;
18007 }
18008
18009 l->label = sym;
a8dbcb85
TS
18010 l->next = si->label_list;
18011 si->label_list = l;
a1facbec 18012}
07a53e5c 18013
a1facbec
MR
18014/* This function is called as tc_frob_label() whenever a label is defined
18015 and adds a DWARF-2 record we only want for true labels. */
18016
18017void
18018mips_define_label (symbolS *sym)
18019{
18020 mips_record_label (sym);
07a53e5c 18021 dwarf2_emit_label (sym);
252b5132 18022}
e1b47bd5
RS
18023
18024/* This function is called by tc_new_dot_label whenever a new dot symbol
18025 is defined. */
18026
18027void
18028mips_add_dot_label (symbolS *sym)
18029{
18030 mips_record_label (sym);
18031 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18032 mips_compressed_mark_label (sym);
18033}
252b5132 18034\f
351cdf24
MF
18035/* Converting ASE flags from internal to .MIPS.abiflags values. */
18036static unsigned int
18037mips_convert_ase_flags (int ase)
18038{
18039 unsigned int ext_ases = 0;
18040
18041 if (ase & ASE_DSP)
18042 ext_ases |= AFL_ASE_DSP;
18043 if (ase & ASE_DSPR2)
18044 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
18045 if (ase & ASE_DSPR3)
18046 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
18047 if (ase & ASE_EVA)
18048 ext_ases |= AFL_ASE_EVA;
18049 if (ase & ASE_MCU)
18050 ext_ases |= AFL_ASE_MCU;
18051 if (ase & ASE_MDMX)
18052 ext_ases |= AFL_ASE_MDMX;
18053 if (ase & ASE_MIPS3D)
18054 ext_ases |= AFL_ASE_MIPS3D;
18055 if (ase & ASE_MT)
18056 ext_ases |= AFL_ASE_MT;
18057 if (ase & ASE_SMARTMIPS)
18058 ext_ases |= AFL_ASE_SMARTMIPS;
18059 if (ase & ASE_VIRT)
18060 ext_ases |= AFL_ASE_VIRT;
18061 if (ase & ASE_MSA)
18062 ext_ases |= AFL_ASE_MSA;
18063 if (ase & ASE_XPA)
18064 ext_ases |= AFL_ASE_XPA;
18065
18066 return ext_ases;
18067}
252b5132
RH
18068/* Some special processing for a MIPS ELF file. */
18069
18070void
17a2f251 18071mips_elf_final_processing (void)
252b5132 18072{
351cdf24
MF
18073 int fpabi;
18074 Elf_Internal_ABIFlags_v0 flags;
18075
18076 flags.version = 0;
18077 flags.isa_rev = 0;
18078 switch (file_mips_opts.isa)
18079 {
18080 case INSN_ISA1:
18081 flags.isa_level = 1;
18082 break;
18083 case INSN_ISA2:
18084 flags.isa_level = 2;
18085 break;
18086 case INSN_ISA3:
18087 flags.isa_level = 3;
18088 break;
18089 case INSN_ISA4:
18090 flags.isa_level = 4;
18091 break;
18092 case INSN_ISA5:
18093 flags.isa_level = 5;
18094 break;
18095 case INSN_ISA32:
18096 flags.isa_level = 32;
18097 flags.isa_rev = 1;
18098 break;
18099 case INSN_ISA32R2:
18100 flags.isa_level = 32;
18101 flags.isa_rev = 2;
18102 break;
18103 case INSN_ISA32R3:
18104 flags.isa_level = 32;
18105 flags.isa_rev = 3;
18106 break;
18107 case INSN_ISA32R5:
18108 flags.isa_level = 32;
18109 flags.isa_rev = 5;
18110 break;
09c14161
MF
18111 case INSN_ISA32R6:
18112 flags.isa_level = 32;
18113 flags.isa_rev = 6;
18114 break;
351cdf24
MF
18115 case INSN_ISA64:
18116 flags.isa_level = 64;
18117 flags.isa_rev = 1;
18118 break;
18119 case INSN_ISA64R2:
18120 flags.isa_level = 64;
18121 flags.isa_rev = 2;
18122 break;
18123 case INSN_ISA64R3:
18124 flags.isa_level = 64;
18125 flags.isa_rev = 3;
18126 break;
18127 case INSN_ISA64R5:
18128 flags.isa_level = 64;
18129 flags.isa_rev = 5;
18130 break;
09c14161
MF
18131 case INSN_ISA64R6:
18132 flags.isa_level = 64;
18133 flags.isa_rev = 6;
18134 break;
351cdf24
MF
18135 }
18136
18137 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
18138 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
18139 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
18140 : (file_mips_opts.fp == 64) ? AFL_REG_64
18141 : AFL_REG_32;
18142 flags.cpr2_size = AFL_REG_NONE;
18143 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18144 Tag_GNU_MIPS_ABI_FP);
18145 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
18146 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
18147 if (file_ase_mips16)
18148 flags.ases |= AFL_ASE_MIPS16;
18149 if (file_ase_micromips)
18150 flags.ases |= AFL_ASE_MICROMIPS;
18151 flags.flags1 = 0;
18152 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
18153 || file_mips_opts.fp == 64)
18154 && file_mips_opts.oddspreg)
18155 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
18156 flags.flags2 = 0;
18157
18158 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
18159 ((Elf_External_ABIFlags_v0 *)
18160 mips_flags_frag));
18161
252b5132 18162 /* Write out the register information. */
316f5878 18163 if (mips_abi != N64_ABI)
252b5132
RH
18164 {
18165 Elf32_RegInfo s;
18166
18167 s.ri_gprmask = mips_gprmask;
18168 s.ri_cprmask[0] = mips_cprmask[0];
18169 s.ri_cprmask[1] = mips_cprmask[1];
18170 s.ri_cprmask[2] = mips_cprmask[2];
18171 s.ri_cprmask[3] = mips_cprmask[3];
18172 /* The gp_value field is set by the MIPS ELF backend. */
18173
18174 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
18175 ((Elf32_External_RegInfo *)
18176 mips_regmask_frag));
18177 }
18178 else
18179 {
18180 Elf64_Internal_RegInfo s;
18181
18182 s.ri_gprmask = mips_gprmask;
18183 s.ri_pad = 0;
18184 s.ri_cprmask[0] = mips_cprmask[0];
18185 s.ri_cprmask[1] = mips_cprmask[1];
18186 s.ri_cprmask[2] = mips_cprmask[2];
18187 s.ri_cprmask[3] = mips_cprmask[3];
18188 /* The gp_value field is set by the MIPS ELF backend. */
18189
18190 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
18191 ((Elf64_External_RegInfo *)
18192 mips_regmask_frag));
18193 }
18194
18195 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
18196 sort of BFD interface for this. */
18197 if (mips_any_noreorder)
18198 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
18199 if (mips_pic != NO_PIC)
143d77c5 18200 {
8b828383 18201 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
18202 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
18203 }
18204 if (mips_abicalls)
18205 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 18206
b015e599
AP
18207 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
18208 defined at present; this might need to change in future. */
a4672219
TS
18209 if (file_ase_mips16)
18210 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
18211 if (file_ase_micromips)
18212 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 18213 if (file_mips_opts.ase & ASE_MDMX)
deec1734 18214 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 18215
bdaaa2e1 18216 /* Set the MIPS ELF ABI flags. */
316f5878 18217 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 18218 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 18219 else if (mips_abi == O64_ABI)
252b5132 18220 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 18221 else if (mips_abi == EABI_ABI)
252b5132 18222 {
bad1aba3 18223 if (file_mips_opts.gp == 64)
252b5132
RH
18224 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
18225 else
18226 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
18227 }
316f5878 18228 else if (mips_abi == N32_ABI)
be00bddd
TS
18229 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ABI2;
18230
c9914766 18231 /* Nothing to do for N64_ABI. */
252b5132
RH
18232
18233 if (mips_32bitmode)
18234 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 18235
7361da2c 18236 if (mips_nan2008 == 1)
ba92f887
MR
18237 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
18238
ad3fea08 18239 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
18240 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
18241 Tag_GNU_MIPS_ABI_FP);
18242 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 18243 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 18244}
252b5132 18245\f
beae10d5 18246typedef struct proc {
9b2f1d35
EC
18247 symbolS *func_sym;
18248 symbolS *func_end_sym;
beae10d5
KH
18249 unsigned long reg_mask;
18250 unsigned long reg_offset;
18251 unsigned long fpreg_mask;
18252 unsigned long fpreg_offset;
18253 unsigned long frame_offset;
18254 unsigned long frame_reg;
18255 unsigned long pc_reg;
18256} procS;
252b5132
RH
18257
18258static procS cur_proc;
18259static procS *cur_proc_ptr;
18260static int numprocs;
18261
df58fc94
RS
18262/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
18263 as "2", and a normal nop as "0". */
18264
18265#define NOP_OPCODE_MIPS 0
18266#define NOP_OPCODE_MIPS16 1
18267#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
18268
18269char
18270mips_nop_opcode (void)
18271{
df58fc94
RS
18272 if (seg_info (now_seg)->tc_segment_info_data.micromips)
18273 return NOP_OPCODE_MICROMIPS;
18274 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
18275 return NOP_OPCODE_MIPS16;
18276 else
18277 return NOP_OPCODE_MIPS;
742a56fe
RS
18278}
18279
df58fc94
RS
18280/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
18281 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 18282
0a9ef439 18283void
17a2f251 18284mips_handle_align (fragS *fragp)
a19d8eb0 18285{
df58fc94 18286 char nop_opcode;
742a56fe 18287 char *p;
c67a084a
NC
18288 int bytes, size, excess;
18289 valueT opcode;
742a56fe 18290
0a9ef439
RH
18291 if (fragp->fr_type != rs_align_code)
18292 return;
18293
742a56fe 18294 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
18295 nop_opcode = *p;
18296 switch (nop_opcode)
a19d8eb0 18297 {
df58fc94
RS
18298 case NOP_OPCODE_MICROMIPS:
18299 opcode = micromips_nop32_insn.insn_opcode;
18300 size = 4;
18301 break;
18302 case NOP_OPCODE_MIPS16:
c67a084a
NC
18303 opcode = mips16_nop_insn.insn_opcode;
18304 size = 2;
df58fc94
RS
18305 break;
18306 case NOP_OPCODE_MIPS:
18307 default:
c67a084a
NC
18308 opcode = nop_insn.insn_opcode;
18309 size = 4;
df58fc94 18310 break;
c67a084a 18311 }
a19d8eb0 18312
c67a084a
NC
18313 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
18314 excess = bytes % size;
df58fc94
RS
18315
18316 /* Handle the leading part if we're not inserting a whole number of
18317 instructions, and make it the end of the fixed part of the frag.
18318 Try to fit in a short microMIPS NOP if applicable and possible,
18319 and use zeroes otherwise. */
18320 gas_assert (excess < 4);
18321 fragp->fr_fix += excess;
18322 switch (excess)
c67a084a 18323 {
df58fc94
RS
18324 case 3:
18325 *p++ = '\0';
18326 /* Fall through. */
18327 case 2:
833794fc 18328 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 18329 {
4d68580a 18330 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
18331 break;
18332 }
18333 *p++ = '\0';
18334 /* Fall through. */
18335 case 1:
18336 *p++ = '\0';
18337 /* Fall through. */
18338 case 0:
18339 break;
a19d8eb0 18340 }
c67a084a
NC
18341
18342 md_number_to_chars (p, opcode, size);
18343 fragp->fr_var = size;
a19d8eb0
CP
18344}
18345
252b5132 18346static long
17a2f251 18347get_number (void)
252b5132
RH
18348{
18349 int negative = 0;
18350 long val = 0;
18351
18352 if (*input_line_pointer == '-')
18353 {
18354 ++input_line_pointer;
18355 negative = 1;
18356 }
3882b010 18357 if (!ISDIGIT (*input_line_pointer))
956cd1d6 18358 as_bad (_("expected simple number"));
252b5132
RH
18359 if (input_line_pointer[0] == '0')
18360 {
18361 if (input_line_pointer[1] == 'x')
18362 {
18363 input_line_pointer += 2;
3882b010 18364 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
18365 {
18366 val <<= 4;
18367 val |= hex_value (*input_line_pointer++);
18368 }
18369 return negative ? -val : val;
18370 }
18371 else
18372 {
18373 ++input_line_pointer;
3882b010 18374 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18375 {
18376 val <<= 3;
18377 val |= *input_line_pointer++ - '0';
18378 }
18379 return negative ? -val : val;
18380 }
18381 }
3882b010 18382 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
18383 {
18384 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
18385 *input_line_pointer, *input_line_pointer);
956cd1d6 18386 as_warn (_("invalid number"));
252b5132
RH
18387 return -1;
18388 }
3882b010 18389 while (ISDIGIT (*input_line_pointer))
252b5132
RH
18390 {
18391 val *= 10;
18392 val += *input_line_pointer++ - '0';
18393 }
18394 return negative ? -val : val;
18395}
18396
18397/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
18398 is an initial number which is the ECOFF file index. In the non-ECOFF
18399 case .file implies DWARF-2. */
18400
18401static void
17a2f251 18402s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 18403{
ecb4347a
DJ
18404 static int first_file_directive = 0;
18405
c5dd6aab
DJ
18406 if (ECOFF_DEBUGGING)
18407 {
18408 get_number ();
18409 s_app_file (0);
18410 }
18411 else
ecb4347a
DJ
18412 {
18413 char *filename;
18414
18415 filename = dwarf2_directive_file (0);
18416
18417 /* Versions of GCC up to 3.1 start files with a ".file"
18418 directive even for stabs output. Make sure that this
18419 ".file" is handled. Note that you need a version of GCC
18420 after 3.1 in order to support DWARF-2 on MIPS. */
18421 if (filename != NULL && ! first_file_directive)
18422 {
18423 (void) new_logical_line (filename, -1);
c04f5787 18424 s_app_file_string (filename, 0);
ecb4347a
DJ
18425 }
18426 first_file_directive = 1;
18427 }
c5dd6aab
DJ
18428}
18429
18430/* The .loc directive, implying DWARF-2. */
252b5132
RH
18431
18432static void
17a2f251 18433s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 18434{
c5dd6aab
DJ
18435 if (!ECOFF_DEBUGGING)
18436 dwarf2_directive_loc (0);
252b5132
RH
18437}
18438
252b5132
RH
18439/* The .end directive. */
18440
18441static void
17a2f251 18442s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
18443{
18444 symbolS *p;
252b5132 18445
7a621144
DJ
18446 /* Following functions need their own .frame and .cprestore directives. */
18447 mips_frame_reg_valid = 0;
18448 mips_cprestore_valid = 0;
18449
252b5132
RH
18450 if (!is_end_of_line[(unsigned char) *input_line_pointer])
18451 {
18452 p = get_symbol ();
18453 demand_empty_rest_of_line ();
18454 }
18455 else
18456 p = NULL;
18457
14949570 18458 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
18459 as_warn (_(".end not in text section"));
18460
18461 if (!cur_proc_ptr)
18462 {
1661c76c 18463 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
18464 demand_empty_rest_of_line ();
18465 return;
18466 }
18467
18468 if (p != NULL)
18469 {
9c2799c2 18470 gas_assert (S_GET_NAME (p));
9b2f1d35 18471 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 18472 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
18473
18474 if (debug_type == DEBUG_STABS)
18475 stabs_generate_asm_endfunc (S_GET_NAME (p),
18476 S_GET_NAME (p));
252b5132
RH
18477 }
18478 else
18479 as_warn (_(".end directive missing or unknown symbol"));
18480
9b2f1d35
EC
18481 /* Create an expression to calculate the size of the function. */
18482 if (p && cur_proc_ptr)
18483 {
18484 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 18485 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
18486
18487 obj->size = exp;
18488 exp->X_op = O_subtract;
18489 exp->X_add_symbol = symbol_temp_new_now ();
18490 exp->X_op_symbol = p;
18491 exp->X_add_number = 0;
18492
18493 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
18494 }
18495
ecb4347a 18496 /* Generate a .pdr section. */
f3ded42a 18497 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
18498 {
18499 segT saved_seg = now_seg;
18500 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
18501 expressionS exp;
18502 char *fragp;
252b5132 18503
252b5132 18504#ifdef md_flush_pending_output
ecb4347a 18505 md_flush_pending_output ();
252b5132
RH
18506#endif
18507
9c2799c2 18508 gas_assert (pdr_seg);
ecb4347a 18509 subseg_set (pdr_seg, 0);
252b5132 18510
ecb4347a
DJ
18511 /* Write the symbol. */
18512 exp.X_op = O_symbol;
18513 exp.X_add_symbol = p;
18514 exp.X_add_number = 0;
18515 emit_expr (&exp, 4);
252b5132 18516
ecb4347a 18517 fragp = frag_more (7 * 4);
252b5132 18518
17a2f251
TS
18519 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
18520 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
18521 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
18522 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
18523 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
18524 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
18525 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 18526
ecb4347a
DJ
18527 subseg_set (saved_seg, saved_subseg);
18528 }
252b5132
RH
18529
18530 cur_proc_ptr = NULL;
18531}
18532
18533/* The .aent and .ent directives. */
18534
18535static void
17a2f251 18536s_mips_ent (int aent)
252b5132 18537{
252b5132 18538 symbolS *symbolP;
252b5132
RH
18539
18540 symbolP = get_symbol ();
18541 if (*input_line_pointer == ',')
f9419b05 18542 ++input_line_pointer;
252b5132 18543 SKIP_WHITESPACE ();
3882b010 18544 if (ISDIGIT (*input_line_pointer)
d9a62219 18545 || *input_line_pointer == '-')
874e8986 18546 get_number ();
252b5132 18547
14949570 18548 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 18549 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
18550
18551 if (!aent && cur_proc_ptr)
9a41af64 18552 as_warn (_("missing .end"));
252b5132
RH
18553
18554 if (!aent)
18555 {
7a621144
DJ
18556 /* This function needs its own .frame and .cprestore directives. */
18557 mips_frame_reg_valid = 0;
18558 mips_cprestore_valid = 0;
18559
252b5132
RH
18560 cur_proc_ptr = &cur_proc;
18561 memset (cur_proc_ptr, '\0', sizeof (procS));
18562
9b2f1d35 18563 cur_proc_ptr->func_sym = symbolP;
252b5132 18564
f9419b05 18565 ++numprocs;
ecb4347a
DJ
18566
18567 if (debug_type == DEBUG_STABS)
18568 stabs_generate_asm_func (S_GET_NAME (symbolP),
18569 S_GET_NAME (symbolP));
252b5132
RH
18570 }
18571
7c0fc524
MR
18572 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
18573
252b5132
RH
18574 demand_empty_rest_of_line ();
18575}
18576
18577/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 18578 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 18579 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 18580 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
18581 symbol table (in the mdebug section). */
18582
18583static void
17a2f251 18584s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 18585{
f3ded42a
RS
18586 if (ECOFF_DEBUGGING)
18587 s_ignore (ignore);
18588 else
ecb4347a
DJ
18589 {
18590 long val;
252b5132 18591
ecb4347a
DJ
18592 if (cur_proc_ptr == (procS *) NULL)
18593 {
18594 as_warn (_(".frame outside of .ent"));
18595 demand_empty_rest_of_line ();
18596 return;
18597 }
252b5132 18598
ecb4347a
DJ
18599 cur_proc_ptr->frame_reg = tc_get_register (1);
18600
18601 SKIP_WHITESPACE ();
18602 if (*input_line_pointer++ != ','
18603 || get_absolute_expression_and_terminator (&val) != ',')
18604 {
1661c76c 18605 as_warn (_("bad .frame directive"));
ecb4347a
DJ
18606 --input_line_pointer;
18607 demand_empty_rest_of_line ();
18608 return;
18609 }
252b5132 18610
ecb4347a
DJ
18611 cur_proc_ptr->frame_offset = val;
18612 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 18613
252b5132 18614 demand_empty_rest_of_line ();
252b5132 18615 }
252b5132
RH
18616}
18617
bdaaa2e1
KH
18618/* The .fmask and .mask directives. If the mdebug section is present
18619 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 18620 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 18621 information correctly. We can't use the ecoff routines because they
252b5132
RH
18622 make reference to the ecoff symbol table (in the mdebug section). */
18623
18624static void
17a2f251 18625s_mips_mask (int reg_type)
252b5132 18626{
f3ded42a
RS
18627 if (ECOFF_DEBUGGING)
18628 s_ignore (reg_type);
18629 else
252b5132 18630 {
ecb4347a 18631 long mask, off;
252b5132 18632
ecb4347a
DJ
18633 if (cur_proc_ptr == (procS *) NULL)
18634 {
18635 as_warn (_(".mask/.fmask outside of .ent"));
18636 demand_empty_rest_of_line ();
18637 return;
18638 }
252b5132 18639
ecb4347a
DJ
18640 if (get_absolute_expression_and_terminator (&mask) != ',')
18641 {
1661c76c 18642 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
18643 --input_line_pointer;
18644 demand_empty_rest_of_line ();
18645 return;
18646 }
252b5132 18647
ecb4347a
DJ
18648 off = get_absolute_expression ();
18649
18650 if (reg_type == 'F')
18651 {
18652 cur_proc_ptr->fpreg_mask = mask;
18653 cur_proc_ptr->fpreg_offset = off;
18654 }
18655 else
18656 {
18657 cur_proc_ptr->reg_mask = mask;
18658 cur_proc_ptr->reg_offset = off;
18659 }
18660
18661 demand_empty_rest_of_line ();
252b5132 18662 }
252b5132
RH
18663}
18664
316f5878
RS
18665/* A table describing all the processors gas knows about. Names are
18666 matched in the order listed.
e7af610e 18667
316f5878
RS
18668 To ease comparison, please keep this table in the same order as
18669 gcc's mips_cpu_info_table[]. */
e972090a
NC
18670static const struct mips_cpu_info mips_cpu_info_table[] =
18671{
316f5878 18672 /* Entries for generic ISAs */
d16afab6
RS
18673 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
18674 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
18675 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
18676 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
18677 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
18678 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
18679 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
18680 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
18681 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 18682 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
18683 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
18684 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
18685 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
18686 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 18687 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
18688
18689 /* MIPS I */
d16afab6
RS
18690 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
18691 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
18692 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
18693
18694 /* MIPS II */
d16afab6 18695 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
18696
18697 /* MIPS III */
d16afab6
RS
18698 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
18699 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
18700 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
18701 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
18702 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
18703 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
18704 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
18705 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
18706 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
18707 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
18708 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
18709 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
18710 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 18711 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
18712 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
18713 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
18714
18715 /* MIPS IV */
d16afab6
RS
18716 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
18717 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
18718 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
18719 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
18720 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
18721 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
18722 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
18723 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
18724 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
18725 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
18726 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
18727 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
18728 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
18729 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
18730 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
18731
18732 /* MIPS 32 */
d16afab6
RS
18733 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
18734 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
18735 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
18736 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
18737
18738 /* MIPS 32 Release 2 */
d16afab6
RS
18739 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18740 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18741 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18742 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
18743 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18744 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18745 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
18746 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
18747 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
18748 ISA_MIPS32R2, CPU_MIPS32R2 },
18749 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
18750 ISA_MIPS32R2, CPU_MIPS32R2 },
18751 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18752 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18753 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18754 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 18755 /* Deprecated forms of the above. */
d16afab6
RS
18756 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
18757 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 18758 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
18759 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
18760 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
18761 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
18762 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 18763 /* Deprecated forms of the above. */
d16afab6
RS
18764 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
18765 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 18766 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
18767 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18768 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18769 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18770 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 18771 /* Deprecated forms of the above. */
d16afab6
RS
18772 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18773 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 18774 /* 34Kn is a 34kc without DSP. */
d16afab6 18775 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 18776 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
18777 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
18778 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
18779 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
18780 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
18781 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 18782 /* Deprecated forms of the above. */
d16afab6
RS
18783 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
18784 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 18785 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
18786 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18787 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18788 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
18789 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
18790 /* interaptiv is the new name for 1004kf */
18791 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
c6e5c03a
RS
18792 /* M5100 family */
18793 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
18794 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 18795 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 18796 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 18797
316f5878 18798 /* MIPS 64 */
d16afab6
RS
18799 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
18800 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
18801 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
18802 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 18803
c7a23324 18804 /* Broadcom SB-1 CPU core */
d16afab6 18805 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 18806 /* Broadcom SB-1A CPU core */
d16afab6 18807 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 18808
4ba154f5 18809 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 18810
ed163775
MR
18811 /* MIPS 64 Release 2 */
18812
967344c6 18813 /* Cavium Networks Octeon CPU core */
d16afab6
RS
18814 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
18815 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
18816 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 18817 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 18818
52b6b6b9 18819 /* RMI Xlr */
d16afab6 18820 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 18821
55a36193
MK
18822 /* Broadcom XLP.
18823 XLP is mostly like XLR, with the prominent exception that it is
18824 MIPS64R2 rather than MIPS64. */
d16afab6 18825 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 18826
a4968f42 18827 /* MIPS 64 Release 6 */
7ef0d297 18828 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 18829 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 18830
316f5878 18831 /* End marker */
d16afab6 18832 { NULL, 0, 0, 0, 0 }
316f5878 18833};
e7af610e 18834
84ea6cf2 18835
316f5878
RS
18836/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
18837 with a final "000" replaced by "k". Ignore case.
e7af610e 18838
316f5878 18839 Note: this function is shared between GCC and GAS. */
c6c98b38 18840
b34976b6 18841static bfd_boolean
17a2f251 18842mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18843{
18844 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
18845 given++, canonical++;
18846
18847 return ((*given == 0 && *canonical == 0)
18848 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
18849}
18850
18851
18852/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
18853 CPU name. We've traditionally allowed a lot of variation here.
18854
18855 Note: this function is shared between GCC and GAS. */
18856
b34976b6 18857static bfd_boolean
17a2f251 18858mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
18859{
18860 /* First see if the name matches exactly, or with a final "000"
18861 turned into "k". */
18862 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 18863 return TRUE;
316f5878
RS
18864
18865 /* If not, try comparing based on numerical designation alone.
18866 See if GIVEN is an unadorned number, or 'r' followed by a number. */
18867 if (TOLOWER (*given) == 'r')
18868 given++;
18869 if (!ISDIGIT (*given))
b34976b6 18870 return FALSE;
316f5878
RS
18871
18872 /* Skip over some well-known prefixes in the canonical name,
18873 hoping to find a number there too. */
18874 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
18875 canonical += 2;
18876 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
18877 canonical += 2;
18878 else if (TOLOWER (canonical[0]) == 'r')
18879 canonical += 1;
18880
18881 return mips_strict_matching_cpu_name_p (canonical, given);
18882}
18883
18884
18885/* Parse an option that takes the name of a processor as its argument.
18886 OPTION is the name of the option and CPU_STRING is the argument.
18887 Return the corresponding processor enumeration if the CPU_STRING is
18888 recognized, otherwise report an error and return null.
18889
18890 A similar function exists in GCC. */
e7af610e
NC
18891
18892static const struct mips_cpu_info *
17a2f251 18893mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 18894{
316f5878 18895 const struct mips_cpu_info *p;
e7af610e 18896
316f5878
RS
18897 /* 'from-abi' selects the most compatible architecture for the given
18898 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
18899 EABIs, we have to decide whether we're using the 32-bit or 64-bit
18900 version. Look first at the -mgp options, if given, otherwise base
18901 the choice on MIPS_DEFAULT_64BIT.
e7af610e 18902
316f5878
RS
18903 Treat NO_ABI like the EABIs. One reason to do this is that the
18904 plain 'mips' and 'mips64' configs have 'from-abi' as their default
18905 architecture. This code picks MIPS I for 'mips' and MIPS III for
18906 'mips64', just as we did in the days before 'from-abi'. */
18907 if (strcasecmp (cpu_string, "from-abi") == 0)
18908 {
18909 if (ABI_NEEDS_32BIT_REGS (mips_abi))
18910 return mips_cpu_info_from_isa (ISA_MIPS1);
18911
18912 if (ABI_NEEDS_64BIT_REGS (mips_abi))
18913 return mips_cpu_info_from_isa (ISA_MIPS3);
18914
bad1aba3 18915 if (file_mips_opts.gp >= 0)
18916 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 18917 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
18918
18919 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
18920 ? ISA_MIPS3
18921 : ISA_MIPS1);
18922 }
18923
18924 /* 'default' has traditionally been a no-op. Probably not very useful. */
18925 if (strcasecmp (cpu_string, "default") == 0)
18926 return 0;
18927
18928 for (p = mips_cpu_info_table; p->name != 0; p++)
18929 if (mips_matching_cpu_name_p (p->name, cpu_string))
18930 return p;
18931
1661c76c 18932 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 18933 return 0;
e7af610e
NC
18934}
18935
316f5878
RS
18936/* Return the canonical processor information for ISA (a member of the
18937 ISA_MIPS* enumeration). */
18938
e7af610e 18939static const struct mips_cpu_info *
17a2f251 18940mips_cpu_info_from_isa (int isa)
e7af610e
NC
18941{
18942 int i;
18943
18944 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 18945 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 18946 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
18947 return (&mips_cpu_info_table[i]);
18948
e972090a 18949 return NULL;
e7af610e 18950}
fef14a42
TS
18951
18952static const struct mips_cpu_info *
17a2f251 18953mips_cpu_info_from_arch (int arch)
fef14a42
TS
18954{
18955 int i;
18956
18957 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
18958 if (arch == mips_cpu_info_table[i].cpu)
18959 return (&mips_cpu_info_table[i]);
18960
18961 return NULL;
18962}
316f5878
RS
18963\f
18964static void
17a2f251 18965show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
18966{
18967 if (*first_p)
18968 {
18969 fprintf (stream, "%24s", "");
18970 *col_p = 24;
18971 }
18972 else
18973 {
18974 fprintf (stream, ", ");
18975 *col_p += 2;
18976 }
e7af610e 18977
316f5878
RS
18978 if (*col_p + strlen (string) > 72)
18979 {
18980 fprintf (stream, "\n%24s", "");
18981 *col_p = 24;
18982 }
18983
18984 fprintf (stream, "%s", string);
18985 *col_p += strlen (string);
18986
18987 *first_p = 0;
18988}
18989
18990void
17a2f251 18991md_show_usage (FILE *stream)
e7af610e 18992{
316f5878
RS
18993 int column, first;
18994 size_t i;
18995
18996 fprintf (stream, _("\
18997MIPS options:\n\
316f5878
RS
18998-EB generate big endian output\n\
18999-EL generate little endian output\n\
19000-g, -g2 do not remove unneeded NOPs or swap branches\n\
19001-G NUM allow referencing objects up to NUM bytes\n\
19002 implicitly with the gp register [default 8]\n"));
19003 fprintf (stream, _("\
19004-mips1 generate MIPS ISA I instructions\n\
19005-mips2 generate MIPS ISA II instructions\n\
19006-mips3 generate MIPS ISA III instructions\n\
19007-mips4 generate MIPS ISA IV instructions\n\
19008-mips5 generate MIPS ISA V instructions\n\
19009-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19010-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19011-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19012-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19013-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19014-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19015-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19016-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19017-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19018-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19019-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19020
19021 first = 1;
e7af610e
NC
19022
19023 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19024 show (stream, mips_cpu_info_table[i].name, &column, &first);
19025 show (stream, "from-abi", &column, &first);
19026 fputc ('\n', stream);
e7af610e 19027
316f5878
RS
19028 fprintf (stream, _("\
19029-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19030-no-mCPU don't generate code specific to CPU.\n\
19031 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19032
19033 first = 1;
19034
19035 show (stream, "3900", &column, &first);
19036 show (stream, "4010", &column, &first);
19037 show (stream, "4100", &column, &first);
19038 show (stream, "4650", &column, &first);
19039 fputc ('\n', stream);
19040
19041 fprintf (stream, _("\
19042-mips16 generate mips16 instructions\n\
19043-no-mips16 do not generate mips16 instructions\n"));
19044 fprintf (stream, _("\
df58fc94
RS
19045-mmicromips generate microMIPS instructions\n\
19046-mno-micromips do not generate microMIPS instructions\n"));
19047 fprintf (stream, _("\
e16bfa71 19048-msmartmips generate smartmips instructions\n\
3739860c 19049-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 19050 fprintf (stream, _("\
74cd071d
CF
19051-mdsp generate DSP instructions\n\
19052-mno-dsp do not generate DSP instructions\n"));
19053 fprintf (stream, _("\
8b082fb1
TS
19054-mdspr2 generate DSP R2 instructions\n\
19055-mno-dspr2 do not generate DSP R2 instructions\n"));
19056 fprintf (stream, _("\
8f4f9071
MF
19057-mdspr3 generate DSP R3 instructions\n\
19058-mno-dspr3 do not generate DSP R3 instructions\n"));
19059 fprintf (stream, _("\
ef2e4d86
CF
19060-mmt generate MT instructions\n\
19061-mno-mt do not generate MT instructions\n"));
19062 fprintf (stream, _("\
dec0624d
MR
19063-mmcu generate MCU instructions\n\
19064-mno-mcu do not generate MCU instructions\n"));
19065 fprintf (stream, _("\
56d438b1
CF
19066-mmsa generate MSA instructions\n\
19067-mno-msa do not generate MSA instructions\n"));
19068 fprintf (stream, _("\
7d64c587
AB
19069-mxpa generate eXtended Physical Address (XPA) instructions\n\
19070-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
19071 fprintf (stream, _("\
b015e599
AP
19072-mvirt generate Virtualization instructions\n\
19073-mno-virt do not generate Virtualization instructions\n"));
19074 fprintf (stream, _("\
833794fc
MR
19075-minsn32 only generate 32-bit microMIPS instructions\n\
19076-mno-insn32 generate all microMIPS instructions\n"));
19077 fprintf (stream, _("\
c67a084a
NC
19078-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
19079-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 19080-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 19081-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 19082-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 19083-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
19084-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
19085-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 19086-msym32 assume all symbols have 32-bit values\n\
316f5878
RS
19087-O0 remove unneeded NOPs, do not swap branches\n\
19088-O remove unneeded NOPs and swap branches\n\
316f5878
RS
19089--trap, --no-break trap exception on div by 0 and mult overflow\n\
19090--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
19091 fprintf (stream, _("\
19092-mhard-float allow floating-point instructions\n\
19093-msoft-float do not allow floating-point instructions\n\
19094-msingle-float only allow 32-bit floating-point operations\n\
19095-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 19096--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887
MR
19097--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
19098-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
19099
19100 first = 1;
19101
19102 show (stream, "legacy", &column, &first);
19103 show (stream, "2008", &column, &first);
19104
19105 fputc ('\n', stream);
19106
316f5878
RS
19107 fprintf (stream, _("\
19108-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 19109-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 19110-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 19111-non_shared do not generate code that can operate with DSOs\n\
316f5878 19112-xgot assume a 32 bit GOT\n\
dcd410fe 19113-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 19114-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 19115 position dependent (non shared) code\n\
316f5878
RS
19116-mabi=ABI create ABI conformant object file for:\n"));
19117
19118 first = 1;
19119
19120 show (stream, "32", &column, &first);
19121 show (stream, "o64", &column, &first);
19122 show (stream, "n32", &column, &first);
19123 show (stream, "64", &column, &first);
19124 show (stream, "eabi", &column, &first);
19125
19126 fputc ('\n', stream);
19127
19128 fprintf (stream, _("\
19129-32 create o32 ABI object file (default)\n\
19130-n32 create n32 ABI object file\n\
19131-64 create 64 ABI object file\n"));
e7af610e 19132}
14e777e0 19133
1575952e 19134#ifdef TE_IRIX
14e777e0 19135enum dwarf2_format
413a266c 19136mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 19137{
369943fe 19138 if (HAVE_64BIT_SYMBOLS)
1575952e 19139 return dwarf2_format_64bit_irix;
14e777e0
KB
19140 else
19141 return dwarf2_format_32bit;
19142}
1575952e 19143#endif
73369e65
EC
19144
19145int
19146mips_dwarf2_addr_size (void)
19147{
6b6b3450 19148 if (HAVE_64BIT_OBJECTS)
73369e65 19149 return 8;
73369e65
EC
19150 else
19151 return 4;
19152}
5862107c
EC
19153
19154/* Standard calling conventions leave the CFA at SP on entry. */
19155void
19156mips_cfi_frame_initial_instructions (void)
19157{
19158 cfi_add_CFA_def_cfa_register (SP);
19159}
19160
707bfff6
TS
19161int
19162tc_mips_regname_to_dw2regnum (char *regname)
19163{
19164 unsigned int regnum = -1;
19165 unsigned int reg;
19166
19167 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
19168 regnum = reg;
19169
19170 return regnum;
19171}
263b2574 19172
19173/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
19174 Given a symbolic attribute NAME, return the proper integer value.
19175 Returns -1 if the attribute is not known. */
19176
19177int
19178mips_convert_symbolic_attribute (const char *name)
19179{
19180 static const struct
19181 {
19182 const char * name;
19183 const int tag;
19184 }
19185 attribute_table[] =
19186 {
19187#define T(tag) {#tag, tag}
19188 T (Tag_GNU_MIPS_ABI_FP),
19189 T (Tag_GNU_MIPS_ABI_MSA),
19190#undef T
19191 };
19192 unsigned int i;
19193
19194 if (name == NULL)
19195 return -1;
19196
19197 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
19198 if (streq (name, attribute_table[i].name))
19199 return attribute_table[i].tag;
19200
19201 return -1;
19202}
fd5c94ab
RS
19203
19204void
19205md_mips_end (void)
19206{
351cdf24
MF
19207 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
19208
fd5c94ab
RS
19209 mips_emit_delays ();
19210 if (cur_proc_ptr)
19211 as_warn (_("missing .end at end of assembly"));
919731af 19212
19213 /* Just in case no code was emitted, do the consistency check. */
19214 file_mips_check_options ();
351cdf24
MF
19215
19216 /* Set a floating-point ABI if the user did not. */
19217 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
19218 {
19219 /* Perform consistency checks on the floating-point ABI. */
19220 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19221 Tag_GNU_MIPS_ABI_FP);
19222 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
19223 check_fpabi (fpabi);
19224 }
19225 else
19226 {
19227 /* Soft-float gets precedence over single-float, the two options should
19228 not be used together so this should not matter. */
19229 if (file_mips_opts.soft_float == 1)
19230 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
19231 /* Single-float gets precedence over all double_float cases. */
19232 else if (file_mips_opts.single_float == 1)
19233 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
19234 else
19235 {
19236 switch (file_mips_opts.fp)
19237 {
19238 case 32:
19239 if (file_mips_opts.gp == 32)
19240 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19241 break;
19242 case 0:
19243 fpabi = Val_GNU_MIPS_ABI_FP_XX;
19244 break;
19245 case 64:
19246 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
19247 fpabi = Val_GNU_MIPS_ABI_FP_64A;
19248 else if (file_mips_opts.gp == 32)
19249 fpabi = Val_GNU_MIPS_ABI_FP_64;
19250 else
19251 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
19252 break;
19253 }
19254 }
19255
19256 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19257 Tag_GNU_MIPS_ABI_FP, fpabi);
19258 }
fd5c94ab 19259}
2f0c68f2
CM
19260
19261/* Returns the relocation type required for a particular CFI encoding. */
19262
19263bfd_reloc_code_real_type
19264mips_cfi_reloc_for_encoding (int encoding)
19265{
19266 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
19267 return BFD_RELOC_32_PCREL;
19268 else return BFD_RELOC_NONE;
19269}
This page took 2.805345 seconds and 4 git commands to generate.