MIPS/LD: Fix crashing with a discarded dynamic relocation section
[deliverable/binutils-gdb.git] / gas / config / tc-mips.c
CommitLineData
252b5132 1/* tc-mips.c -- assemble code for a MIPS chip.
219d1afa 2 Copyright (C) 1993-2018 Free Software Foundation, Inc.
252b5132
RH
3 Contributed by the OSF and Ralph Campbell.
4 Written by Keith Knowles and Ralph Campbell, working independently.
5 Modified for ECOFF and R4000 support by Ian Lance Taylor of Cygnus
6 Support.
7
8 This file is part of GAS.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
ec2655a6 12 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
4b4da160
NC
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
252b5132
RH
24
25#include "as.h"
26#include "config.h"
27#include "subsegs.h"
3882b010 28#include "safe-ctype.h"
252b5132 29
252b5132
RH
30#include "opcode/mips.h"
31#include "itbl-ops.h"
c5dd6aab 32#include "dwarf2dbg.h"
5862107c 33#include "dw2gencfi.h"
252b5132 34
42429eac
RS
35/* Check assumptions made in this file. */
36typedef char static_assert1[sizeof (offsetT) < 8 ? -1 : 1];
37typedef char static_assert2[sizeof (valueT) < 8 ? -1 : 1];
38
252b5132
RH
39#ifdef DEBUG
40#define DBG(x) printf x
41#else
42#define DBG(x)
43#endif
44
263b2574 45#define streq(a, b) (strcmp (a, b) == 0)
46
9e12b7a2
RS
47#define SKIP_SPACE_TABS(S) \
48 do { while (*(S) == ' ' || *(S) == '\t') ++(S); } while (0)
49
252b5132 50/* Clean up namespace so we can include obj-elf.h too. */
17a2f251
TS
51static int mips_output_flavor (void);
52static int mips_output_flavor (void) { return OUTPUT_FLAVOR; }
252b5132
RH
53#undef OBJ_PROCESS_STAB
54#undef OUTPUT_FLAVOR
55#undef S_GET_ALIGN
56#undef S_GET_SIZE
57#undef S_SET_ALIGN
58#undef S_SET_SIZE
252b5132
RH
59#undef obj_frob_file
60#undef obj_frob_file_after_relocs
61#undef obj_frob_symbol
62#undef obj_pop_insert
63#undef obj_sec_sym_ok_for_reloc
64#undef OBJ_COPY_SYMBOL_ATTRIBUTES
65
66#include "obj-elf.h"
67/* Fix any of them that we actually care about. */
68#undef OUTPUT_FLAVOR
69#define OUTPUT_FLAVOR mips_output_flavor()
252b5132 70
252b5132 71#include "elf/mips.h"
252b5132
RH
72
73#ifndef ECOFF_DEBUGGING
74#define NO_ECOFF_DEBUGGING
75#define ECOFF_DEBUGGING 0
76#endif
77
ecb4347a
DJ
78int mips_flag_mdebug = -1;
79
dcd410fe
RO
80/* Control generation of .pdr sections. Off by default on IRIX: the native
81 linker doesn't know about and discards them, but relocations against them
82 remain, leading to rld crashes. */
83#ifdef TE_IRIX
84int mips_flag_pdr = FALSE;
85#else
86int mips_flag_pdr = TRUE;
87#endif
88
252b5132
RH
89#include "ecoff.h"
90
252b5132 91static char *mips_regmask_frag;
351cdf24 92static char *mips_flags_frag;
252b5132 93
85b51719 94#define ZERO 0
741fe287 95#define ATREG 1
df58fc94
RS
96#define S0 16
97#define S7 23
252b5132
RH
98#define TREG 24
99#define PIC_CALL_REG 25
100#define KT0 26
101#define KT1 27
102#define GP 28
103#define SP 29
104#define FP 30
105#define RA 31
106
107#define ILLEGAL_REG (32)
108
741fe287
MR
109#define AT mips_opts.at
110
252b5132
RH
111extern int target_big_endian;
112
252b5132 113/* The name of the readonly data section. */
e8044f35 114#define RDATA_SECTION_NAME ".rodata"
252b5132 115
a4e06468
RS
116/* Ways in which an instruction can be "appended" to the output. */
117enum append_method {
118 /* Just add it normally. */
119 APPEND_ADD,
120
121 /* Add it normally and then add a nop. */
122 APPEND_ADD_WITH_NOP,
123
124 /* Turn an instruction with a delay slot into a "compact" version. */
125 APPEND_ADD_COMPACT,
126
127 /* Insert the instruction before the last one. */
128 APPEND_SWAP
129};
130
47e39b9d
RS
131/* Information about an instruction, including its format, operands
132 and fixups. */
133struct mips_cl_insn
134{
135 /* The opcode's entry in mips_opcodes or mips16_opcodes. */
136 const struct mips_opcode *insn_mo;
137
47e39b9d 138 /* The 16-bit or 32-bit bitstring of the instruction itself. This is
5c04167a
RS
139 a copy of INSN_MO->match with the operands filled in. If we have
140 decided to use an extended MIPS16 instruction, this includes the
141 extension. */
47e39b9d
RS
142 unsigned long insn_opcode;
143
144 /* The frag that contains the instruction. */
145 struct frag *frag;
146
147 /* The offset into FRAG of the first instruction byte. */
148 long where;
149
150 /* The relocs associated with the instruction, if any. */
151 fixS *fixp[3];
152
a38419a5
RS
153 /* True if this entry cannot be moved from its current position. */
154 unsigned int fixed_p : 1;
47e39b9d 155
708587a4 156 /* True if this instruction occurred in a .set noreorder block. */
47e39b9d
RS
157 unsigned int noreorder_p : 1;
158
2fa15973
RS
159 /* True for mips16 instructions that jump to an absolute address. */
160 unsigned int mips16_absolute_jump_p : 1;
15be625d
CM
161
162 /* True if this instruction is complete. */
163 unsigned int complete_p : 1;
e407c74b
NC
164
165 /* True if this instruction is cleared from history by unconditional
166 branch. */
167 unsigned int cleared_p : 1;
47e39b9d
RS
168};
169
a325df1d
TS
170/* The ABI to use. */
171enum mips_abi_level
172{
173 NO_ABI = 0,
174 O32_ABI,
175 O64_ABI,
176 N32_ABI,
177 N64_ABI,
178 EABI_ABI
179};
180
181/* MIPS ABI we are using for this output file. */
316f5878 182static enum mips_abi_level mips_abi = NO_ABI;
a325df1d 183
143d77c5
EC
184/* Whether or not we have code that can call pic code. */
185int mips_abicalls = FALSE;
186
aa6975fb
ILT
187/* Whether or not we have code which can be put into a shared
188 library. */
189static bfd_boolean mips_in_shared = TRUE;
190
252b5132
RH
191/* This is the set of options which may be modified by the .set
192 pseudo-op. We use a struct so that .set push and .set pop are more
193 reliable. */
194
e972090a
NC
195struct mips_set_options
196{
252b5132
RH
197 /* MIPS ISA (Instruction Set Architecture) level. This is set to -1
198 if it has not been initialized. Changed by `.set mipsN', and the
199 -mipsN command line option, and the default CPU. */
200 int isa;
846ef2d0
RS
201 /* Enabled Application Specific Extensions (ASEs). Changed by `.set
202 <asename>', by command line options, and based on the default
203 architecture. */
204 int ase;
252b5132
RH
205 /* Whether we are assembling for the mips16 processor. 0 if we are
206 not, 1 if we are, and -1 if the value has not been initialized.
207 Changed by `.set mips16' and `.set nomips16', and the -mips16 and
208 -nomips16 command line options, and the default CPU. */
209 int mips16;
df58fc94
RS
210 /* Whether we are assembling for the mipsMIPS ASE. 0 if we are not,
211 1 if we are, and -1 if the value has not been initialized. Changed
212 by `.set micromips' and `.set nomicromips', and the -mmicromips
213 and -mno-micromips command line options, and the default CPU. */
214 int micromips;
252b5132
RH
215 /* Non-zero if we should not reorder instructions. Changed by `.set
216 reorder' and `.set noreorder'. */
217 int noreorder;
741fe287
MR
218 /* Non-zero if we should not permit the register designated "assembler
219 temporary" to be used in instructions. The value is the register
220 number, normally $at ($1). Changed by `.set at=REG', `.set noat'
221 (same as `.set at=$0') and `.set at' (same as `.set at=$1'). */
222 unsigned int at;
252b5132
RH
223 /* Non-zero if we should warn when a macro instruction expands into
224 more than one machine instruction. Changed by `.set nomacro' and
225 `.set macro'. */
226 int warn_about_macros;
227 /* Non-zero if we should not move instructions. Changed by `.set
228 move', `.set volatile', `.set nomove', and `.set novolatile'. */
229 int nomove;
230 /* Non-zero if we should not optimize branches by moving the target
231 of the branch into the delay slot. Actually, we don't perform
232 this optimization anyhow. Changed by `.set bopt' and `.set
233 nobopt'. */
234 int nobopt;
235 /* Non-zero if we should not autoextend mips16 instructions.
236 Changed by `.set autoextend' and `.set noautoextend'. */
237 int noautoextend;
833794fc
MR
238 /* True if we should only emit 32-bit microMIPS instructions.
239 Changed by `.set insn32' and `.set noinsn32', and the -minsn32
240 and -mno-insn32 command line options. */
241 bfd_boolean insn32;
a325df1d
TS
242 /* Restrict general purpose registers and floating point registers
243 to 32 bit. This is initially determined when -mgp32 or -mfp32
244 is passed but can changed if the assembler code uses .set mipsN. */
bad1aba3 245 int gp;
0b35dfee 246 int fp;
fef14a42
TS
247 /* MIPS architecture (CPU) type. Changed by .set arch=FOO, the -march
248 command line option, and the default CPU. */
249 int arch;
aed1a261
RS
250 /* True if ".set sym32" is in effect. */
251 bfd_boolean sym32;
037b32b9
AN
252 /* True if floating-point operations are not allowed. Changed by .set
253 softfloat or .set hardfloat, by command line options -msoft-float or
254 -mhard-float. The default is false. */
255 bfd_boolean soft_float;
256
257 /* True if only single-precision floating-point operations are allowed.
258 Changed by .set singlefloat or .set doublefloat, command-line options
259 -msingle-float or -mdouble-float. The default is false. */
260 bfd_boolean single_float;
351cdf24
MF
261
262 /* 1 if single-precision operations on odd-numbered registers are
263 allowed. */
264 int oddspreg;
252b5132
RH
265};
266
919731af 267/* Specifies whether module level options have been checked yet. */
268static bfd_boolean file_mips_opts_checked = FALSE;
269
7361da2c
AB
270/* Do we support nan2008? 0 if we don't, 1 if we do, and -1 if the
271 value has not been initialized. Changed by `.nan legacy' and
272 `.nan 2008', and the -mnan=legacy and -mnan=2008 command line
273 options, and the default CPU. */
274static int mips_nan2008 = -1;
a325df1d 275
0b35dfee 276/* This is the struct we use to hold the module level set of options.
bad1aba3 277 Note that we must set the isa field to ISA_UNKNOWN and the ASE, gp and
0b35dfee 278 fp fields to -1 to indicate that they have not been initialized. */
037b32b9 279
0b35dfee 280static struct mips_set_options file_mips_opts =
281{
282 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
283 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
284 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 285 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 286 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
0b35dfee 287};
252b5132 288
0b35dfee 289/* This is similar to file_mips_opts, but for the current set of options. */
ba92f887 290
e972090a
NC
291static struct mips_set_options mips_opts =
292{
846ef2d0 293 /* isa */ ISA_UNKNOWN, /* ase */ 0, /* mips16 */ -1, /* micromips */ -1,
b015e599 294 /* noreorder */ 0, /* at */ ATREG, /* warn_about_macros */ 0,
833794fc 295 /* nomove */ 0, /* nobopt */ 0, /* noautoextend */ 0, /* insn32 */ FALSE,
bad1aba3 296 /* gp */ -1, /* fp */ -1, /* arch */ CPU_UNKNOWN, /* sym32 */ FALSE,
351cdf24 297 /* soft_float */ FALSE, /* single_float */ FALSE, /* oddspreg */ -1
e7af610e 298};
252b5132 299
846ef2d0
RS
300/* Which bits of file_ase were explicitly set or cleared by ASE options. */
301static unsigned int file_ase_explicit;
302
252b5132
RH
303/* These variables are filled in with the masks of registers used.
304 The object format code reads them and puts them in the appropriate
305 place. */
306unsigned long mips_gprmask;
307unsigned long mips_cprmask[4];
308
738f4d98 309/* True if any MIPS16 code was produced. */
a4672219
TS
310static int file_ase_mips16;
311
3994f87e
TS
312#define ISA_SUPPORTS_MIPS16E (mips_opts.isa == ISA_MIPS32 \
313 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
314 || mips_opts.isa == ISA_MIPS32R3 \
315 || mips_opts.isa == ISA_MIPS32R5 \
3994f87e 316 || mips_opts.isa == ISA_MIPS64 \
ae52f483
AB
317 || mips_opts.isa == ISA_MIPS64R2 \
318 || mips_opts.isa == ISA_MIPS64R3 \
319 || mips_opts.isa == ISA_MIPS64R5)
3994f87e 320
df58fc94
RS
321/* True if any microMIPS code was produced. */
322static int file_ase_micromips;
323
b12dd2e4
CF
324/* True if we want to create R_MIPS_JALR for jalr $25. */
325#ifdef TE_IRIX
1180b5a4 326#define MIPS_JALR_HINT_P(EXPR) HAVE_NEWABI
b12dd2e4 327#else
1180b5a4
RS
328/* As a GNU extension, we use R_MIPS_JALR for o32 too. However,
329 because there's no place for any addend, the only acceptable
330 expression is a bare symbol. */
331#define MIPS_JALR_HINT_P(EXPR) \
332 (!HAVE_IN_PLACE_ADDENDS \
333 || ((EXPR)->X_op == O_symbol && (EXPR)->X_add_number == 0))
b12dd2e4
CF
334#endif
335
ec68c924 336/* The argument of the -march= flag. The architecture we are assembling. */
316f5878 337static const char *mips_arch_string;
ec68c924
EC
338
339/* The argument of the -mtune= flag. The architecture for which we
340 are optimizing. */
341static int mips_tune = CPU_UNKNOWN;
316f5878 342static const char *mips_tune_string;
ec68c924 343
316f5878 344/* True when generating 32-bit code for a 64-bit processor. */
252b5132
RH
345static int mips_32bitmode = 0;
346
316f5878
RS
347/* True if the given ABI requires 32-bit registers. */
348#define ABI_NEEDS_32BIT_REGS(ABI) ((ABI) == O32_ABI)
349
350/* Likewise 64-bit registers. */
707bfff6 351#define ABI_NEEDS_64BIT_REGS(ABI) \
134c0c8b 352 ((ABI) == N32_ABI \
707bfff6 353 || (ABI) == N64_ABI \
316f5878
RS
354 || (ABI) == O64_ABI)
355
7361da2c
AB
356#define ISA_IS_R6(ISA) \
357 ((ISA) == ISA_MIPS32R6 \
358 || (ISA) == ISA_MIPS64R6)
359
ad3fea08 360/* Return true if ISA supports 64 bit wide gp registers. */
707bfff6
TS
361#define ISA_HAS_64BIT_REGS(ISA) \
362 ((ISA) == ISA_MIPS3 \
363 || (ISA) == ISA_MIPS4 \
364 || (ISA) == ISA_MIPS5 \
365 || (ISA) == ISA_MIPS64 \
ae52f483
AB
366 || (ISA) == ISA_MIPS64R2 \
367 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
368 || (ISA) == ISA_MIPS64R5 \
369 || (ISA) == ISA_MIPS64R6)
9ce8a5dd 370
ad3fea08
TS
371/* Return true if ISA supports 64 bit wide float registers. */
372#define ISA_HAS_64BIT_FPRS(ISA) \
373 ((ISA) == ISA_MIPS3 \
374 || (ISA) == ISA_MIPS4 \
375 || (ISA) == ISA_MIPS5 \
376 || (ISA) == ISA_MIPS32R2 \
ae52f483
AB
377 || (ISA) == ISA_MIPS32R3 \
378 || (ISA) == ISA_MIPS32R5 \
7361da2c 379 || (ISA) == ISA_MIPS32R6 \
ad3fea08 380 || (ISA) == ISA_MIPS64 \
ae52f483
AB
381 || (ISA) == ISA_MIPS64R2 \
382 || (ISA) == ISA_MIPS64R3 \
7361da2c
AB
383 || (ISA) == ISA_MIPS64R5 \
384 || (ISA) == ISA_MIPS64R6)
ad3fea08 385
af7ee8bf
CD
386/* Return true if ISA supports 64-bit right rotate (dror et al.)
387 instructions. */
707bfff6 388#define ISA_HAS_DROR(ISA) \
df58fc94 389 ((ISA) == ISA_MIPS64R2 \
ae52f483
AB
390 || (ISA) == ISA_MIPS64R3 \
391 || (ISA) == ISA_MIPS64R5 \
7361da2c 392 || (ISA) == ISA_MIPS64R6 \
df58fc94
RS
393 || (mips_opts.micromips \
394 && ISA_HAS_64BIT_REGS (ISA)) \
395 )
af7ee8bf
CD
396
397/* Return true if ISA supports 32-bit right rotate (ror et al.)
398 instructions. */
707bfff6
TS
399#define ISA_HAS_ROR(ISA) \
400 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
401 || (ISA) == ISA_MIPS32R3 \
402 || (ISA) == ISA_MIPS32R5 \
7361da2c 403 || (ISA) == ISA_MIPS32R6 \
707bfff6 404 || (ISA) == ISA_MIPS64R2 \
ae52f483
AB
405 || (ISA) == ISA_MIPS64R3 \
406 || (ISA) == ISA_MIPS64R5 \
7361da2c 407 || (ISA) == ISA_MIPS64R6 \
846ef2d0 408 || (mips_opts.ase & ASE_SMARTMIPS) \
df58fc94
RS
409 || mips_opts.micromips \
410 )
707bfff6 411
7455baf8 412/* Return true if ISA supports single-precision floats in odd registers. */
351cdf24
MF
413#define ISA_HAS_ODD_SINGLE_FPR(ISA, CPU)\
414 (((ISA) == ISA_MIPS32 \
415 || (ISA) == ISA_MIPS32R2 \
416 || (ISA) == ISA_MIPS32R3 \
417 || (ISA) == ISA_MIPS32R5 \
7361da2c 418 || (ISA) == ISA_MIPS32R6 \
351cdf24
MF
419 || (ISA) == ISA_MIPS64 \
420 || (ISA) == ISA_MIPS64R2 \
421 || (ISA) == ISA_MIPS64R3 \
422 || (ISA) == ISA_MIPS64R5 \
7361da2c 423 || (ISA) == ISA_MIPS64R6 \
351cdf24
MF
424 || (CPU) == CPU_R5900) \
425 && (CPU) != CPU_LOONGSON_3A)
af7ee8bf 426
ad3fea08
TS
427/* Return true if ISA supports move to/from high part of a 64-bit
428 floating-point register. */
429#define ISA_HAS_MXHC1(ISA) \
430 ((ISA) == ISA_MIPS32R2 \
ae52f483
AB
431 || (ISA) == ISA_MIPS32R3 \
432 || (ISA) == ISA_MIPS32R5 \
7361da2c
AB
433 || (ISA) == ISA_MIPS32R6 \
434 || (ISA) == ISA_MIPS64R2 \
435 || (ISA) == ISA_MIPS64R3 \
436 || (ISA) == ISA_MIPS64R5 \
437 || (ISA) == ISA_MIPS64R6)
438
439/* Return true if ISA supports legacy NAN. */
440#define ISA_HAS_LEGACY_NAN(ISA) \
441 ((ISA) == ISA_MIPS1 \
442 || (ISA) == ISA_MIPS2 \
443 || (ISA) == ISA_MIPS3 \
444 || (ISA) == ISA_MIPS4 \
445 || (ISA) == ISA_MIPS5 \
446 || (ISA) == ISA_MIPS32 \
447 || (ISA) == ISA_MIPS32R2 \
448 || (ISA) == ISA_MIPS32R3 \
449 || (ISA) == ISA_MIPS32R5 \
450 || (ISA) == ISA_MIPS64 \
ae52f483
AB
451 || (ISA) == ISA_MIPS64R2 \
452 || (ISA) == ISA_MIPS64R3 \
453 || (ISA) == ISA_MIPS64R5)
ad3fea08 454
bad1aba3 455#define GPR_SIZE \
456 (mips_opts.gp == 64 && !ISA_HAS_64BIT_REGS (mips_opts.isa) \
457 ? 32 \
458 : mips_opts.gp)
ca4e0257 459
bad1aba3 460#define FPR_SIZE \
461 (mips_opts.fp == 64 && !ISA_HAS_64BIT_FPRS (mips_opts.isa) \
462 ? 32 \
463 : mips_opts.fp)
ca4e0257 464
316f5878 465#define HAVE_NEWABI (mips_abi == N32_ABI || mips_abi == N64_ABI)
e013f690 466
316f5878 467#define HAVE_64BIT_OBJECTS (mips_abi == N64_ABI)
e013f690 468
3b91255e
RS
469/* True if relocations are stored in-place. */
470#define HAVE_IN_PLACE_ADDENDS (!HAVE_NEWABI)
471
aed1a261
RS
472/* The ABI-derived address size. */
473#define HAVE_64BIT_ADDRESSES \
bad1aba3 474 (GPR_SIZE == 64 && (mips_abi == EABI_ABI || mips_abi == N64_ABI))
aed1a261 475#define HAVE_32BIT_ADDRESSES (!HAVE_64BIT_ADDRESSES)
e013f690 476
aed1a261
RS
477/* The size of symbolic constants (i.e., expressions of the form
478 "SYMBOL" or "SYMBOL + OFFSET"). */
479#define HAVE_32BIT_SYMBOLS \
480 (HAVE_32BIT_ADDRESSES || !HAVE_64BIT_OBJECTS || mips_opts.sym32)
481#define HAVE_64BIT_SYMBOLS (!HAVE_32BIT_SYMBOLS)
ca4e0257 482
b7c7d6c1
TS
483/* Addresses are loaded in different ways, depending on the address size
484 in use. The n32 ABI Documentation also mandates the use of additions
485 with overflow checking, but existing implementations don't follow it. */
f899b4b8 486#define ADDRESS_ADD_INSN \
b7c7d6c1 487 (HAVE_32BIT_ADDRESSES ? "addu" : "daddu")
f899b4b8
TS
488
489#define ADDRESS_ADDI_INSN \
b7c7d6c1 490 (HAVE_32BIT_ADDRESSES ? "addiu" : "daddiu")
f899b4b8
TS
491
492#define ADDRESS_LOAD_INSN \
493 (HAVE_32BIT_ADDRESSES ? "lw" : "ld")
494
495#define ADDRESS_STORE_INSN \
496 (HAVE_32BIT_ADDRESSES ? "sw" : "sd")
497
a4672219 498/* Return true if the given CPU supports the MIPS16 ASE. */
3396de36
TS
499#define CPU_HAS_MIPS16(cpu) \
500 (strncmp (TARGET_CPU, "mips16", sizeof ("mips16") - 1) == 0 \
501 || strncmp (TARGET_CANONICAL, "mips-lsi-elf", sizeof ("mips-lsi-elf") - 1) == 0)
a4672219 502
2309ddf2 503/* Return true if the given CPU supports the microMIPS ASE. */
df58fc94
RS
504#define CPU_HAS_MICROMIPS(cpu) 0
505
60b63b72
RS
506/* True if CPU has a dror instruction. */
507#define CPU_HAS_DROR(CPU) ((CPU) == CPU_VR5400 || (CPU) == CPU_VR5500)
508
509/* True if CPU has a ror instruction. */
510#define CPU_HAS_ROR(CPU) CPU_HAS_DROR (CPU)
511
dd6a37e7 512/* True if CPU is in the Octeon family */
2c629856
N
513#define CPU_IS_OCTEON(CPU) ((CPU) == CPU_OCTEON || (CPU) == CPU_OCTEONP \
514 || (CPU) == CPU_OCTEON2 || (CPU) == CPU_OCTEON3)
dd6a37e7 515
dd3cbb7e 516/* True if CPU has seq/sne and seqi/snei instructions. */
dd6a37e7 517#define CPU_HAS_SEQ(CPU) (CPU_IS_OCTEON (CPU))
dd3cbb7e 518
0aa27725
RS
519/* True, if CPU has support for ldc1 and sdc1. */
520#define CPU_HAS_LDC1_SDC1(CPU) \
521 ((mips_opts.isa != ISA_MIPS1) && ((CPU) != CPU_R5900))
522
c8978940
CD
523/* True if mflo and mfhi can be immediately followed by instructions
524 which write to the HI and LO registers.
525
526 According to MIPS specifications, MIPS ISAs I, II, and III need
527 (at least) two instructions between the reads of HI/LO and
528 instructions which write them, and later ISAs do not. Contradicting
529 the MIPS specifications, some MIPS IV processor user manuals (e.g.
530 the UM for the NEC Vr5000) document needing the instructions between
531 HI/LO reads and writes, as well. Therefore, we declare only MIPS32,
532 MIPS64 and later ISAs to have the interlocks, plus any specific
533 earlier-ISA CPUs for which CPU documentation declares that the
534 instructions are really interlocked. */
535#define hilo_interlocks \
536 (mips_opts.isa == ISA_MIPS32 \
537 || mips_opts.isa == ISA_MIPS32R2 \
ae52f483
AB
538 || mips_opts.isa == ISA_MIPS32R3 \
539 || mips_opts.isa == ISA_MIPS32R5 \
7361da2c 540 || mips_opts.isa == ISA_MIPS32R6 \
c8978940
CD
541 || mips_opts.isa == ISA_MIPS64 \
542 || mips_opts.isa == ISA_MIPS64R2 \
ae52f483
AB
543 || mips_opts.isa == ISA_MIPS64R3 \
544 || mips_opts.isa == ISA_MIPS64R5 \
7361da2c 545 || mips_opts.isa == ISA_MIPS64R6 \
c8978940 546 || mips_opts.arch == CPU_R4010 \
e407c74b 547 || mips_opts.arch == CPU_R5900 \
c8978940
CD
548 || mips_opts.arch == CPU_R10000 \
549 || mips_opts.arch == CPU_R12000 \
3aa3176b
TS
550 || mips_opts.arch == CPU_R14000 \
551 || mips_opts.arch == CPU_R16000 \
c8978940 552 || mips_opts.arch == CPU_RM7000 \
c8978940 553 || mips_opts.arch == CPU_VR5500 \
df58fc94 554 || mips_opts.micromips \
c8978940 555 )
252b5132
RH
556
557/* Whether the processor uses hardware interlocks to protect reads
81912461
ILT
558 from the GPRs after they are loaded from memory, and thus does not
559 require nops to be inserted. This applies to instructions marked
67dc82bc 560 INSN_LOAD_MEMORY. These nops are only required at MIPS ISA
df58fc94
RS
561 level I and microMIPS mode instructions are always interlocked. */
562#define gpr_interlocks \
563 (mips_opts.isa != ISA_MIPS1 \
564 || mips_opts.arch == CPU_R3900 \
e407c74b 565 || mips_opts.arch == CPU_R5900 \
df58fc94
RS
566 || mips_opts.micromips \
567 )
252b5132 568
81912461
ILT
569/* Whether the processor uses hardware interlocks to avoid delays
570 required by coprocessor instructions, and thus does not require
571 nops to be inserted. This applies to instructions marked
43885403
MF
572 INSN_LOAD_COPROC, INSN_COPROC_MOVE, and to delays between
573 instructions marked INSN_WRITE_COND_CODE and ones marked
81912461 574 INSN_READ_COND_CODE. These nops are only required at MIPS ISA
df58fc94
RS
575 levels I, II, and III and microMIPS mode instructions are always
576 interlocked. */
bdaaa2e1 577/* Itbl support may require additional care here. */
81912461
ILT
578#define cop_interlocks \
579 ((mips_opts.isa != ISA_MIPS1 \
580 && mips_opts.isa != ISA_MIPS2 \
581 && mips_opts.isa != ISA_MIPS3) \
582 || mips_opts.arch == CPU_R4300 \
df58fc94 583 || mips_opts.micromips \
81912461
ILT
584 )
585
586/* Whether the processor uses hardware interlocks to protect reads
587 from coprocessor registers after they are loaded from memory, and
588 thus does not require nops to be inserted. This applies to
589 instructions marked INSN_COPROC_MEMORY_DELAY. These nops are only
df58fc94
RS
590 requires at MIPS ISA level I and microMIPS mode instructions are
591 always interlocked. */
592#define cop_mem_interlocks \
593 (mips_opts.isa != ISA_MIPS1 \
594 || mips_opts.micromips \
595 )
252b5132 596
6b76fefe
CM
597/* Is this a mfhi or mflo instruction? */
598#define MF_HILO_INSN(PINFO) \
b19e8a9b
AN
599 ((PINFO & INSN_READ_HI) || (PINFO & INSN_READ_LO))
600
df58fc94
RS
601/* Whether code compression (either of the MIPS16 or the microMIPS ASEs)
602 has been selected. This implies, in particular, that addresses of text
603 labels have their LSB set. */
604#define HAVE_CODE_COMPRESSION \
605 ((mips_opts.mips16 | mips_opts.micromips) != 0)
606
42429eac 607/* The minimum and maximum signed values that can be stored in a GPR. */
bad1aba3 608#define GPR_SMAX ((offsetT) (((valueT) 1 << (GPR_SIZE - 1)) - 1))
42429eac
RS
609#define GPR_SMIN (-GPR_SMAX - 1)
610
252b5132
RH
611/* MIPS PIC level. */
612
a161fe53 613enum mips_pic_level mips_pic;
252b5132 614
c9914766 615/* 1 if we should generate 32 bit offsets from the $gp register in
252b5132 616 SVR4_PIC mode. Currently has no meaning in other modes. */
c9914766 617static int mips_big_got = 0;
252b5132
RH
618
619/* 1 if trap instructions should used for overflow rather than break
620 instructions. */
c9914766 621static int mips_trap = 0;
252b5132 622
119d663a 623/* 1 if double width floating point constants should not be constructed
b6ff326e 624 by assembling two single width halves into two single width floating
119d663a
NC
625 point registers which just happen to alias the double width destination
626 register. On some architectures this aliasing can be disabled by a bit
d547a75e 627 in the status register, and the setting of this bit cannot be determined
119d663a
NC
628 automatically at assemble time. */
629static int mips_disable_float_construction;
630
252b5132
RH
631/* Non-zero if any .set noreorder directives were used. */
632
633static int mips_any_noreorder;
634
6b76fefe
CM
635/* Non-zero if nops should be inserted when the register referenced in
636 an mfhi/mflo instruction is read in the next two instructions. */
637static int mips_7000_hilo_fix;
638
02ffd3e4 639/* The size of objects in the small data section. */
156c2f8b 640static unsigned int g_switch_value = 8;
252b5132
RH
641/* Whether the -G option was used. */
642static int g_switch_seen = 0;
643
644#define N_RMASK 0xc4
645#define N_VFP 0xd4
646
647/* If we can determine in advance that GP optimization won't be
648 possible, we can skip the relaxation stuff that tries to produce
649 GP-relative references. This makes delay slot optimization work
650 better.
651
652 This function can only provide a guess, but it seems to work for
fba2b7f9
GK
653 gcc output. It needs to guess right for gcc, otherwise gcc
654 will put what it thinks is a GP-relative instruction in a branch
655 delay slot.
252b5132
RH
656
657 I don't know if a fix is needed for the SVR4_PIC mode. I've only
658 fixed it for the non-PIC mode. KR 95/04/07 */
17a2f251 659static int nopic_need_relax (symbolS *, int);
252b5132
RH
660
661/* handle of the OPCODE hash table */
662static struct hash_control *op_hash = NULL;
663
664/* The opcode hash table we use for the mips16. */
665static struct hash_control *mips16_op_hash = NULL;
666
df58fc94
RS
667/* The opcode hash table we use for the microMIPS ASE. */
668static struct hash_control *micromips_op_hash = NULL;
669
252b5132
RH
670/* This array holds the chars that always start a comment. If the
671 pre-processor is disabled, these aren't very useful */
672const char comment_chars[] = "#";
673
674/* This array holds the chars that only start a comment at the beginning of
675 a line. If the line seems to have the form '# 123 filename'
676 .line and .file directives will appear in the pre-processed output */
677/* Note that input_file.c hand checks for '#' at the beginning of the
678 first line of the input file. This is because the compiler outputs
bdaaa2e1 679 #NO_APP at the beginning of its output. */
252b5132
RH
680/* Also note that C style comments are always supported. */
681const char line_comment_chars[] = "#";
682
bdaaa2e1 683/* This array holds machine specific line separator characters. */
63a0b638 684const char line_separator_chars[] = ";";
252b5132
RH
685
686/* Chars that can be used to separate mant from exp in floating point nums */
687const char EXP_CHARS[] = "eE";
688
689/* Chars that mean this number is a floating point constant */
690/* As in 0f12.456 */
691/* or 0d1.2345e12 */
692const char FLT_CHARS[] = "rRsSfFdDxXpP";
693
694/* Also be aware that MAXIMUM_NUMBER_OF_CHARS_FOR_FLOAT may have to be
695 changed in read.c . Ideally it shouldn't have to know about it at all,
696 but nothing is ideal around here.
697 */
698
e3de51ce
RS
699/* Types of printf format used for instruction-related error messages.
700 "I" means int ("%d") and "S" means string ("%s"). */
701enum mips_insn_error_format {
702 ERR_FMT_PLAIN,
703 ERR_FMT_I,
704 ERR_FMT_SS,
705};
706
707/* Information about an error that was found while assembling the current
708 instruction. */
709struct mips_insn_error {
710 /* We sometimes need to match an instruction against more than one
711 opcode table entry. Errors found during this matching are reported
712 against a particular syntactic argument rather than against the
713 instruction as a whole. We grade these messages so that errors
714 against argument N have a greater priority than an error against
715 any argument < N, since the former implies that arguments up to N
716 were acceptable and that the opcode entry was therefore a closer match.
717 If several matches report an error against the same argument,
718 we only use that error if it is the same in all cases.
719
720 min_argnum is the minimum argument number for which an error message
721 should be accepted. It is 0 if MSG is against the instruction as
722 a whole. */
723 int min_argnum;
724
725 /* The printf()-style message, including its format and arguments. */
726 enum mips_insn_error_format format;
727 const char *msg;
728 union {
729 int i;
730 const char *ss[2];
731 } u;
732};
733
734/* The error that should be reported for the current instruction. */
735static struct mips_insn_error insn_error;
252b5132
RH
736
737static int auto_align = 1;
738
739/* When outputting SVR4 PIC code, the assembler needs to know the
740 offset in the stack frame from which to restore the $gp register.
741 This is set by the .cprestore pseudo-op, and saved in this
742 variable. */
743static offsetT mips_cprestore_offset = -1;
744
67c1ffbe 745/* Similar for NewABI PIC code, where $gp is callee-saved. NewABI has some
6478892d 746 more optimizations, it can use a register value instead of a memory-saved
956cd1d6 747 offset and even an other register than $gp as global pointer. */
6478892d
TS
748static offsetT mips_cpreturn_offset = -1;
749static int mips_cpreturn_register = -1;
750static int mips_gp_register = GP;
def2e0dd 751static int mips_gprel_offset = 0;
6478892d 752
7a621144
DJ
753/* Whether mips_cprestore_offset has been set in the current function
754 (or whether it has already been warned about, if not). */
755static int mips_cprestore_valid = 0;
756
252b5132
RH
757/* This is the register which holds the stack frame, as set by the
758 .frame pseudo-op. This is needed to implement .cprestore. */
759static int mips_frame_reg = SP;
760
7a621144
DJ
761/* Whether mips_frame_reg has been set in the current function
762 (or whether it has already been warned about, if not). */
763static int mips_frame_reg_valid = 0;
764
252b5132
RH
765/* To output NOP instructions correctly, we need to keep information
766 about the previous two instructions. */
767
768/* Whether we are optimizing. The default value of 2 means to remove
769 unneeded NOPs and swap branch instructions when possible. A value
770 of 1 means to not swap branches. A value of 0 means to always
771 insert NOPs. */
772static int mips_optimize = 2;
773
774/* Debugging level. -g sets this to 2. -gN sets this to N. -g0 is
775 equivalent to seeing no -g option at all. */
776static int mips_debug = 0;
777
7d8e00cf
RS
778/* The maximum number of NOPs needed to avoid the VR4130 mflo/mfhi errata. */
779#define MAX_VR4130_NOPS 4
780
781/* The maximum number of NOPs needed to fill delay slots. */
782#define MAX_DELAY_NOPS 2
783
784/* The maximum number of NOPs needed for any purpose. */
785#define MAX_NOPS 4
71400594
RS
786
787/* A list of previous instructions, with index 0 being the most recent.
788 We need to look back MAX_NOPS instructions when filling delay slots
789 or working around processor errata. We need to look back one
790 instruction further if we're thinking about using history[0] to
791 fill a branch delay slot. */
792static struct mips_cl_insn history[1 + MAX_NOPS];
252b5132 793
fc76e730 794/* Arrays of operands for each instruction. */
14daeee3 795#define MAX_OPERANDS 6
fc76e730
RS
796struct mips_operand_array {
797 const struct mips_operand *operand[MAX_OPERANDS];
798};
799static struct mips_operand_array *mips_operands;
800static struct mips_operand_array *mips16_operands;
801static struct mips_operand_array *micromips_operands;
802
1e915849 803/* Nop instructions used by emit_nop. */
df58fc94
RS
804static struct mips_cl_insn nop_insn;
805static struct mips_cl_insn mips16_nop_insn;
806static struct mips_cl_insn micromips_nop16_insn;
807static struct mips_cl_insn micromips_nop32_insn;
1e915849
RS
808
809/* The appropriate nop for the current mode. */
833794fc
MR
810#define NOP_INSN (mips_opts.mips16 \
811 ? &mips16_nop_insn \
812 : (mips_opts.micromips \
813 ? (mips_opts.insn32 \
814 ? &micromips_nop32_insn \
815 : &micromips_nop16_insn) \
816 : &nop_insn))
df58fc94
RS
817
818/* The size of NOP_INSN in bytes. */
833794fc
MR
819#define NOP_INSN_SIZE ((mips_opts.mips16 \
820 || (mips_opts.micromips && !mips_opts.insn32)) \
821 ? 2 : 4)
252b5132 822
252b5132
RH
823/* If this is set, it points to a frag holding nop instructions which
824 were inserted before the start of a noreorder section. If those
825 nops turn out to be unnecessary, the size of the frag can be
826 decreased. */
827static fragS *prev_nop_frag;
828
829/* The number of nop instructions we created in prev_nop_frag. */
830static int prev_nop_frag_holds;
831
832/* The number of nop instructions that we know we need in
bdaaa2e1 833 prev_nop_frag. */
252b5132
RH
834static int prev_nop_frag_required;
835
836/* The number of instructions we've seen since prev_nop_frag. */
837static int prev_nop_frag_since;
838
e8044f35
RS
839/* Relocations against symbols are sometimes done in two parts, with a HI
840 relocation and a LO relocation. Each relocation has only 16 bits of
841 space to store an addend. This means that in order for the linker to
842 handle carries correctly, it must be able to locate both the HI and
843 the LO relocation. This means that the relocations must appear in
844 order in the relocation table.
252b5132
RH
845
846 In order to implement this, we keep track of each unmatched HI
847 relocation. We then sort them so that they immediately precede the
bdaaa2e1 848 corresponding LO relocation. */
252b5132 849
e972090a
NC
850struct mips_hi_fixup
851{
252b5132
RH
852 /* Next HI fixup. */
853 struct mips_hi_fixup *next;
854 /* This fixup. */
855 fixS *fixp;
856 /* The section this fixup is in. */
857 segT seg;
858};
859
860/* The list of unmatched HI relocs. */
861
862static struct mips_hi_fixup *mips_hi_fixup_list;
863
64bdfcaf
RS
864/* The frag containing the last explicit relocation operator.
865 Null if explicit relocations have not been used. */
866
867static fragS *prev_reloc_op_frag;
868
252b5132
RH
869/* Map mips16 register numbers to normal MIPS register numbers. */
870
e972090a
NC
871static const unsigned int mips16_to_32_reg_map[] =
872{
252b5132
RH
873 16, 17, 2, 3, 4, 5, 6, 7
874};
60b63b72 875
df58fc94
RS
876/* Map microMIPS register numbers to normal MIPS register numbers. */
877
df58fc94 878#define micromips_to_32_reg_d_map mips16_to_32_reg_map
df58fc94
RS
879
880/* The microMIPS registers with type h. */
e76ff5ab 881static const unsigned int micromips_to_32_reg_h_map1[] =
df58fc94
RS
882{
883 5, 5, 6, 4, 4, 4, 4, 4
884};
e76ff5ab 885static const unsigned int micromips_to_32_reg_h_map2[] =
df58fc94
RS
886{
887 6, 7, 7, 21, 22, 5, 6, 7
888};
889
df58fc94
RS
890/* The microMIPS registers with type m. */
891static const unsigned int micromips_to_32_reg_m_map[] =
892{
893 0, 17, 2, 3, 16, 18, 19, 20
894};
895
896#define micromips_to_32_reg_n_map micromips_to_32_reg_m_map
897
71400594
RS
898/* Classifies the kind of instructions we're interested in when
899 implementing -mfix-vr4120. */
c67a084a
NC
900enum fix_vr4120_class
901{
71400594
RS
902 FIX_VR4120_MACC,
903 FIX_VR4120_DMACC,
904 FIX_VR4120_MULT,
905 FIX_VR4120_DMULT,
906 FIX_VR4120_DIV,
907 FIX_VR4120_MTHILO,
908 NUM_FIX_VR4120_CLASSES
909};
910
c67a084a
NC
911/* ...likewise -mfix-loongson2f-jump. */
912static bfd_boolean mips_fix_loongson2f_jump;
913
914/* ...likewise -mfix-loongson2f-nop. */
915static bfd_boolean mips_fix_loongson2f_nop;
916
917/* True if -mfix-loongson2f-nop or -mfix-loongson2f-jump passed. */
918static bfd_boolean mips_fix_loongson2f;
919
71400594
RS
920/* Given two FIX_VR4120_* values X and Y, bit Y of element X is set if
921 there must be at least one other instruction between an instruction
922 of type X and an instruction of type Y. */
923static unsigned int vr4120_conflicts[NUM_FIX_VR4120_CLASSES];
924
925/* True if -mfix-vr4120 is in force. */
d766e8ec 926static int mips_fix_vr4120;
4a6a3df4 927
7d8e00cf
RS
928/* ...likewise -mfix-vr4130. */
929static int mips_fix_vr4130;
930
6a32d874
CM
931/* ...likewise -mfix-24k. */
932static int mips_fix_24k;
933
a8d14a88
CM
934/* ...likewise -mfix-rm7000 */
935static int mips_fix_rm7000;
936
d954098f
DD
937/* ...likewise -mfix-cn63xxp1 */
938static bfd_boolean mips_fix_cn63xxp1;
939
4a6a3df4
AO
940/* We don't relax branches by default, since this causes us to expand
941 `la .l2 - .l1' if there's a branch between .l1 and .l2, because we
942 fail to compute the offset before expanding the macro to the most
943 efficient expansion. */
944
945static int mips_relax_branch;
8b10b0b3
MR
946
947/* TRUE if checks are suppressed for invalid branches between ISA modes.
948 Needed for broken assembly produced by some GCC versions and some
949 sloppy code out there, where branches to data labels are present. */
950static bfd_boolean mips_ignore_branch_isa;
252b5132 951\f
4d7206a2
RS
952/* The expansion of many macros depends on the type of symbol that
953 they refer to. For example, when generating position-dependent code,
954 a macro that refers to a symbol may have two different expansions,
955 one which uses GP-relative addresses and one which uses absolute
956 addresses. When generating SVR4-style PIC, a macro may have
957 different expansions for local and global symbols.
958
959 We handle these situations by generating both sequences and putting
960 them in variant frags. In position-dependent code, the first sequence
961 will be the GP-relative one and the second sequence will be the
962 absolute one. In SVR4 PIC, the first sequence will be for global
963 symbols and the second will be for local symbols.
964
584892a6
RS
965 The frag's "subtype" is RELAX_ENCODE (FIRST, SECOND), where FIRST and
966 SECOND are the lengths of the two sequences in bytes. These fields
967 can be extracted using RELAX_FIRST() and RELAX_SECOND(). In addition,
968 the subtype has the following flags:
4d7206a2 969
ce8ad872
MR
970 RELAX_PIC
971 Set if generating PIC code.
972
584892a6
RS
973 RELAX_USE_SECOND
974 Set if it has been decided that we should use the second
975 sequence instead of the first.
976
977 RELAX_SECOND_LONGER
978 Set in the first variant frag if the macro's second implementation
979 is longer than its first. This refers to the macro as a whole,
980 not an individual relaxation.
981
982 RELAX_NOMACRO
983 Set in the first variant frag if the macro appeared in a .set nomacro
984 block and if one alternative requires a warning but the other does not.
985
986 RELAX_DELAY_SLOT
987 Like RELAX_NOMACRO, but indicates that the macro appears in a branch
988 delay slot.
4d7206a2 989
df58fc94
RS
990 RELAX_DELAY_SLOT_16BIT
991 Like RELAX_DELAY_SLOT, but indicates that the delay slot requires a
992 16-bit instruction.
993
994 RELAX_DELAY_SLOT_SIZE_FIRST
995 Like RELAX_DELAY_SLOT, but indicates that the first implementation of
996 the macro is of the wrong size for the branch delay slot.
997
998 RELAX_DELAY_SLOT_SIZE_SECOND
999 Like RELAX_DELAY_SLOT, but indicates that the second implementation of
1000 the macro is of the wrong size for the branch delay slot.
1001
4d7206a2
RS
1002 The frag's "opcode" points to the first fixup for relaxable code.
1003
1004 Relaxable macros are generated using a sequence such as:
1005
1006 relax_start (SYMBOL);
1007 ... generate first expansion ...
1008 relax_switch ();
1009 ... generate second expansion ...
1010 relax_end ();
1011
1012 The code and fixups for the unwanted alternative are discarded
1013 by md_convert_frag. */
ce8ad872
MR
1014#define RELAX_ENCODE(FIRST, SECOND, PIC) \
1015 (((FIRST) << 8) | (SECOND) | ((PIC) ? 0x10000 : 0))
4d7206a2 1016
584892a6
RS
1017#define RELAX_FIRST(X) (((X) >> 8) & 0xff)
1018#define RELAX_SECOND(X) ((X) & 0xff)
ce8ad872
MR
1019#define RELAX_PIC(X) (((X) & 0x10000) != 0)
1020#define RELAX_USE_SECOND 0x20000
1021#define RELAX_SECOND_LONGER 0x40000
1022#define RELAX_NOMACRO 0x80000
1023#define RELAX_DELAY_SLOT 0x100000
1024#define RELAX_DELAY_SLOT_16BIT 0x200000
1025#define RELAX_DELAY_SLOT_SIZE_FIRST 0x400000
1026#define RELAX_DELAY_SLOT_SIZE_SECOND 0x800000
252b5132 1027
4a6a3df4
AO
1028/* Branch without likely bit. If label is out of range, we turn:
1029
134c0c8b 1030 beq reg1, reg2, label
4a6a3df4
AO
1031 delay slot
1032
1033 into
1034
1035 bne reg1, reg2, 0f
1036 nop
1037 j label
1038 0: delay slot
1039
1040 with the following opcode replacements:
1041
1042 beq <-> bne
1043 blez <-> bgtz
1044 bltz <-> bgez
1045 bc1f <-> bc1t
1046
1047 bltzal <-> bgezal (with jal label instead of j label)
1048
1049 Even though keeping the delay slot instruction in the delay slot of
1050 the branch would be more efficient, it would be very tricky to do
1051 correctly, because we'd have to introduce a variable frag *after*
1052 the delay slot instruction, and expand that instead. Let's do it
1053 the easy way for now, even if the branch-not-taken case now costs
1054 one additional instruction. Out-of-range branches are not supposed
1055 to be common, anyway.
1056
1057 Branch likely. If label is out of range, we turn:
1058
1059 beql reg1, reg2, label
1060 delay slot (annulled if branch not taken)
1061
1062 into
1063
1064 beql reg1, reg2, 1f
1065 nop
1066 beql $0, $0, 2f
1067 nop
1068 1: j[al] label
1069 delay slot (executed only if branch taken)
1070 2:
1071
1072 It would be possible to generate a shorter sequence by losing the
1073 likely bit, generating something like:
b34976b6 1074
4a6a3df4
AO
1075 bne reg1, reg2, 0f
1076 nop
1077 j[al] label
1078 delay slot (executed only if branch taken)
1079 0:
1080
1081 beql -> bne
1082 bnel -> beq
1083 blezl -> bgtz
1084 bgtzl -> blez
1085 bltzl -> bgez
1086 bgezl -> bltz
1087 bc1fl -> bc1t
1088 bc1tl -> bc1f
1089
1090 bltzall -> bgezal (with jal label instead of j label)
1091 bgezall -> bltzal (ditto)
1092
1093
1094 but it's not clear that it would actually improve performance. */
ce8ad872
MR
1095#define RELAX_BRANCH_ENCODE(at, pic, \
1096 uncond, likely, link, toofar) \
66b3e8da
MR
1097 ((relax_substateT) \
1098 (0xc0000000 \
1099 | ((at) & 0x1f) \
ce8ad872
MR
1100 | ((pic) ? 0x20 : 0) \
1101 | ((toofar) ? 0x40 : 0) \
1102 | ((link) ? 0x80 : 0) \
1103 | ((likely) ? 0x100 : 0) \
1104 | ((uncond) ? 0x200 : 0)))
4a6a3df4 1105#define RELAX_BRANCH_P(i) (((i) & 0xf0000000) == 0xc0000000)
ce8ad872
MR
1106#define RELAX_BRANCH_UNCOND(i) (((i) & 0x200) != 0)
1107#define RELAX_BRANCH_LIKELY(i) (((i) & 0x100) != 0)
1108#define RELAX_BRANCH_LINK(i) (((i) & 0x80) != 0)
1109#define RELAX_BRANCH_TOOFAR(i) (((i) & 0x40) != 0)
1110#define RELAX_BRANCH_PIC(i) (((i) & 0x20) != 0)
66b3e8da 1111#define RELAX_BRANCH_AT(i) ((i) & 0x1f)
4a6a3df4 1112
252b5132
RH
1113/* For mips16 code, we use an entirely different form of relaxation.
1114 mips16 supports two versions of most instructions which take
1115 immediate values: a small one which takes some small value, and a
1116 larger one which takes a 16 bit value. Since branches also follow
1117 this pattern, relaxing these values is required.
1118
1119 We can assemble both mips16 and normal MIPS code in a single
1120 object. Therefore, we need to support this type of relaxation at
1121 the same time that we support the relaxation described above. We
1122 use the high bit of the subtype field to distinguish these cases.
1123
1124 The information we store for this type of relaxation is the
1125 argument code found in the opcode file for this relocation, whether
1126 the user explicitly requested a small or extended form, and whether
1127 the relocation is in a jump or jal delay slot. That tells us the
1128 size of the value, and how it should be stored. We also store
1129 whether the fragment is considered to be extended or not. We also
1130 store whether this is known to be a branch to a different section,
1131 whether we have tried to relax this frag yet, and whether we have
1132 ever extended a PC relative fragment because of a shift count. */
25499ac7 1133#define RELAX_MIPS16_ENCODE(type, e2, pic, sym32, nomacro, \
8507b6e7
MR
1134 small, ext, \
1135 dslot, jal_dslot) \
252b5132
RH
1136 (0x80000000 \
1137 | ((type) & 0xff) \
25499ac7
MR
1138 | ((e2) ? 0x100 : 0) \
1139 | ((pic) ? 0x200 : 0) \
1140 | ((sym32) ? 0x400 : 0) \
1141 | ((nomacro) ? 0x800 : 0) \
1142 | ((small) ? 0x1000 : 0) \
1143 | ((ext) ? 0x2000 : 0) \
1144 | ((dslot) ? 0x4000 : 0) \
1145 | ((jal_dslot) ? 0x8000 : 0))
8507b6e7 1146
4a6a3df4 1147#define RELAX_MIPS16_P(i) (((i) & 0xc0000000) == 0x80000000)
252b5132 1148#define RELAX_MIPS16_TYPE(i) ((i) & 0xff)
25499ac7
MR
1149#define RELAX_MIPS16_E2(i) (((i) & 0x100) != 0)
1150#define RELAX_MIPS16_PIC(i) (((i) & 0x200) != 0)
1151#define RELAX_MIPS16_SYM32(i) (((i) & 0x400) != 0)
1152#define RELAX_MIPS16_NOMACRO(i) (((i) & 0x800) != 0)
1153#define RELAX_MIPS16_USER_SMALL(i) (((i) & 0x1000) != 0)
1154#define RELAX_MIPS16_USER_EXT(i) (((i) & 0x2000) != 0)
1155#define RELAX_MIPS16_DSLOT(i) (((i) & 0x4000) != 0)
1156#define RELAX_MIPS16_JAL_DSLOT(i) (((i) & 0x8000) != 0)
1157
1158#define RELAX_MIPS16_EXTENDED(i) (((i) & 0x10000) != 0)
1159#define RELAX_MIPS16_MARK_EXTENDED(i) ((i) | 0x10000)
1160#define RELAX_MIPS16_CLEAR_EXTENDED(i) ((i) & ~0x10000)
1161#define RELAX_MIPS16_ALWAYS_EXTENDED(i) (((i) & 0x20000) != 0)
1162#define RELAX_MIPS16_MARK_ALWAYS_EXTENDED(i) ((i) | 0x20000)
1163#define RELAX_MIPS16_CLEAR_ALWAYS_EXTENDED(i) ((i) & ~0x20000)
1164#define RELAX_MIPS16_MACRO(i) (((i) & 0x40000) != 0)
1165#define RELAX_MIPS16_MARK_MACRO(i) ((i) | 0x40000)
1166#define RELAX_MIPS16_CLEAR_MACRO(i) ((i) & ~0x40000)
885add95 1167
df58fc94
RS
1168/* For microMIPS code, we use relaxation similar to one we use for
1169 MIPS16 code. Some instructions that take immediate values support
1170 two encodings: a small one which takes some small value, and a
1171 larger one which takes a 16 bit value. As some branches also follow
1172 this pattern, relaxing these values is required.
1173
1174 We can assemble both microMIPS and normal MIPS code in a single
1175 object. Therefore, we need to support this type of relaxation at
1176 the same time that we support the relaxation described above. We
1177 use one of the high bits of the subtype field to distinguish these
1178 cases.
1179
1180 The information we store for this type of relaxation is the argument
1181 code found in the opcode file for this relocation, the register
8484fb75
MR
1182 selected as the assembler temporary, whether in the 32-bit
1183 instruction mode, whether the branch is unconditional, whether it is
7bd374a4
MR
1184 compact, whether there is no delay-slot instruction available to fill
1185 in, whether it stores the link address implicitly in $ra, whether
1186 relaxation of out-of-range 32-bit branches to a sequence of
8484fb75
MR
1187 instructions is enabled, and whether the displacement of a branch is
1188 too large to fit as an immediate argument of a 16-bit and a 32-bit
1189 branch, respectively. */
ce8ad872 1190#define RELAX_MICROMIPS_ENCODE(type, at, insn32, pic, \
7bd374a4 1191 uncond, compact, link, nods, \
40209cad
MR
1192 relax32, toofar16, toofar32) \
1193 (0x40000000 \
1194 | ((type) & 0xff) \
1195 | (((at) & 0x1f) << 8) \
8484fb75 1196 | ((insn32) ? 0x2000 : 0) \
ce8ad872
MR
1197 | ((pic) ? 0x4000 : 0) \
1198 | ((uncond) ? 0x8000 : 0) \
1199 | ((compact) ? 0x10000 : 0) \
1200 | ((link) ? 0x20000 : 0) \
1201 | ((nods) ? 0x40000 : 0) \
1202 | ((relax32) ? 0x80000 : 0) \
1203 | ((toofar16) ? 0x100000 : 0) \
1204 | ((toofar32) ? 0x200000 : 0))
df58fc94
RS
1205#define RELAX_MICROMIPS_P(i) (((i) & 0xc0000000) == 0x40000000)
1206#define RELAX_MICROMIPS_TYPE(i) ((i) & 0xff)
1207#define RELAX_MICROMIPS_AT(i) (((i) >> 8) & 0x1f)
8484fb75 1208#define RELAX_MICROMIPS_INSN32(i) (((i) & 0x2000) != 0)
ce8ad872
MR
1209#define RELAX_MICROMIPS_PIC(i) (((i) & 0x4000) != 0)
1210#define RELAX_MICROMIPS_UNCOND(i) (((i) & 0x8000) != 0)
1211#define RELAX_MICROMIPS_COMPACT(i) (((i) & 0x10000) != 0)
1212#define RELAX_MICROMIPS_LINK(i) (((i) & 0x20000) != 0)
1213#define RELAX_MICROMIPS_NODS(i) (((i) & 0x40000) != 0)
1214#define RELAX_MICROMIPS_RELAX32(i) (((i) & 0x80000) != 0)
1215
1216#define RELAX_MICROMIPS_TOOFAR16(i) (((i) & 0x100000) != 0)
1217#define RELAX_MICROMIPS_MARK_TOOFAR16(i) ((i) | 0x100000)
1218#define RELAX_MICROMIPS_CLEAR_TOOFAR16(i) ((i) & ~0x100000)
1219#define RELAX_MICROMIPS_TOOFAR32(i) (((i) & 0x200000) != 0)
1220#define RELAX_MICROMIPS_MARK_TOOFAR32(i) ((i) | 0x200000)
1221#define RELAX_MICROMIPS_CLEAR_TOOFAR32(i) ((i) & ~0x200000)
df58fc94 1222
43c0598f
RS
1223/* Sign-extend 16-bit value X. */
1224#define SEXT_16BIT(X) ((((X) + 0x8000) & 0xffff) - 0x8000)
1225
885add95
CD
1226/* Is the given value a sign-extended 32-bit value? */
1227#define IS_SEXT_32BIT_NUM(x) \
1228 (((x) &~ (offsetT) 0x7fffffff) == 0 \
1229 || (((x) &~ (offsetT) 0x7fffffff) == ~ (offsetT) 0x7fffffff))
1230
1231/* Is the given value a sign-extended 16-bit value? */
1232#define IS_SEXT_16BIT_NUM(x) \
1233 (((x) &~ (offsetT) 0x7fff) == 0 \
1234 || (((x) &~ (offsetT) 0x7fff) == ~ (offsetT) 0x7fff))
1235
df58fc94
RS
1236/* Is the given value a sign-extended 12-bit value? */
1237#define IS_SEXT_12BIT_NUM(x) \
1238 (((((x) & 0xfff) ^ 0x800LL) - 0x800LL) == (x))
1239
7f3c4072
CM
1240/* Is the given value a sign-extended 9-bit value? */
1241#define IS_SEXT_9BIT_NUM(x) \
1242 (((((x) & 0x1ff) ^ 0x100LL) - 0x100LL) == (x))
1243
2051e8c4
MR
1244/* Is the given value a zero-extended 32-bit value? Or a negated one? */
1245#define IS_ZEXT_32BIT_NUM(x) \
1246 (((x) &~ (offsetT) 0xffffffff) == 0 \
1247 || (((x) &~ (offsetT) 0xffffffff) == ~ (offsetT) 0xffffffff))
1248
bf12938e
RS
1249/* Extract bits MASK << SHIFT from STRUCT and shift them right
1250 SHIFT places. */
1251#define EXTRACT_BITS(STRUCT, MASK, SHIFT) \
1252 (((STRUCT) >> (SHIFT)) & (MASK))
1253
bf12938e 1254/* Extract the operand given by FIELD from mips_cl_insn INSN. */
df58fc94
RS
1255#define EXTRACT_OPERAND(MICROMIPS, FIELD, INSN) \
1256 (!(MICROMIPS) \
1257 ? EXTRACT_BITS ((INSN).insn_opcode, OP_MASK_##FIELD, OP_SH_##FIELD) \
1258 : EXTRACT_BITS ((INSN).insn_opcode, \
1259 MICROMIPSOP_MASK_##FIELD, MICROMIPSOP_SH_##FIELD))
bf12938e
RS
1260#define MIPS16_EXTRACT_OPERAND(FIELD, INSN) \
1261 EXTRACT_BITS ((INSN).insn_opcode, \
1262 MIPS16OP_MASK_##FIELD, \
1263 MIPS16OP_SH_##FIELD)
5c04167a
RS
1264
1265/* The MIPS16 EXTEND opcode, shifted left 16 places. */
1266#define MIPS16_EXTEND (0xf000U << 16)
4d7206a2 1267\f
df58fc94
RS
1268/* Whether or not we are emitting a branch-likely macro. */
1269static bfd_boolean emit_branch_likely_macro = FALSE;
1270
4d7206a2
RS
1271/* Global variables used when generating relaxable macros. See the
1272 comment above RELAX_ENCODE for more details about how relaxation
1273 is used. */
1274static struct {
1275 /* 0 if we're not emitting a relaxable macro.
1276 1 if we're emitting the first of the two relaxation alternatives.
1277 2 if we're emitting the second alternative. */
1278 int sequence;
1279
1280 /* The first relaxable fixup in the current frag. (In other words,
1281 the first fixup that refers to relaxable code.) */
1282 fixS *first_fixup;
1283
1284 /* sizes[0] says how many bytes of the first alternative are stored in
1285 the current frag. Likewise sizes[1] for the second alternative. */
1286 unsigned int sizes[2];
1287
1288 /* The symbol on which the choice of sequence depends. */
1289 symbolS *symbol;
1290} mips_relax;
252b5132 1291\f
584892a6
RS
1292/* Global variables used to decide whether a macro needs a warning. */
1293static struct {
1294 /* True if the macro is in a branch delay slot. */
1295 bfd_boolean delay_slot_p;
1296
df58fc94
RS
1297 /* Set to the length in bytes required if the macro is in a delay slot
1298 that requires a specific length of instruction, otherwise zero. */
1299 unsigned int delay_slot_length;
1300
584892a6
RS
1301 /* For relaxable macros, sizes[0] is the length of the first alternative
1302 in bytes and sizes[1] is the length of the second alternative.
1303 For non-relaxable macros, both elements give the length of the
1304 macro in bytes. */
1305 unsigned int sizes[2];
1306
df58fc94
RS
1307 /* For relaxable macros, first_insn_sizes[0] is the length of the first
1308 instruction of the first alternative in bytes and first_insn_sizes[1]
1309 is the length of the first instruction of the second alternative.
1310 For non-relaxable macros, both elements give the length of the first
1311 instruction in bytes.
1312
1313 Set to zero if we haven't yet seen the first instruction. */
1314 unsigned int first_insn_sizes[2];
1315
1316 /* For relaxable macros, insns[0] is the number of instructions for the
1317 first alternative and insns[1] is the number of instructions for the
1318 second alternative.
1319
1320 For non-relaxable macros, both elements give the number of
1321 instructions for the macro. */
1322 unsigned int insns[2];
1323
584892a6
RS
1324 /* The first variant frag for this macro. */
1325 fragS *first_frag;
1326} mips_macro_warning;
1327\f
252b5132
RH
1328/* Prototypes for static functions. */
1329
252b5132
RH
1330enum mips_regclass { MIPS_GR_REG, MIPS_FP_REG, MIPS16_REG };
1331
b34976b6 1332static void append_insn
df58fc94
RS
1333 (struct mips_cl_insn *, expressionS *, bfd_reloc_code_real_type *,
1334 bfd_boolean expansionp);
7d10b47d 1335static void mips_no_prev_insn (void);
c67a084a 1336static void macro_build (expressionS *, const char *, const char *, ...);
b34976b6 1337static void mips16_macro_build
03ea81db 1338 (expressionS *, const char *, const char *, va_list *);
67c0d1eb 1339static void load_register (int, expressionS *, int);
584892a6
RS
1340static void macro_start (void);
1341static void macro_end (void);
833794fc 1342static void macro (struct mips_cl_insn *ip, char *str);
17a2f251 1343static void mips16_macro (struct mips_cl_insn * ip);
17a2f251
TS
1344static void mips_ip (char *str, struct mips_cl_insn * ip);
1345static void mips16_ip (char *str, struct mips_cl_insn * ip);
25499ac7 1346static unsigned long mips16_immed_extend (offsetT, unsigned int);
b34976b6 1347static void mips16_immed
3b4dbbbf 1348 (const char *, unsigned int, int, bfd_reloc_code_real_type, offsetT,
43c0598f 1349 unsigned int, unsigned long *);
5e0116d5 1350static size_t my_getSmallExpression
17a2f251
TS
1351 (expressionS *, bfd_reloc_code_real_type *, char *);
1352static void my_getExpression (expressionS *, char *);
1353static void s_align (int);
1354static void s_change_sec (int);
1355static void s_change_section (int);
1356static void s_cons (int);
1357static void s_float_cons (int);
1358static void s_mips_globl (int);
1359static void s_option (int);
1360static void s_mipsset (int);
1361static void s_abicalls (int);
1362static void s_cpload (int);
1363static void s_cpsetup (int);
1364static void s_cplocal (int);
1365static void s_cprestore (int);
1366static void s_cpreturn (int);
741d6ea8
JM
1367static void s_dtprelword (int);
1368static void s_dtpreldword (int);
d0f13682
CLT
1369static void s_tprelword (int);
1370static void s_tpreldword (int);
17a2f251
TS
1371static void s_gpvalue (int);
1372static void s_gpword (int);
1373static void s_gpdword (int);
a3f278e2 1374static void s_ehword (int);
17a2f251
TS
1375static void s_cpadd (int);
1376static void s_insn (int);
ba92f887 1377static void s_nan (int);
919731af 1378static void s_module (int);
17a2f251
TS
1379static void s_mips_ent (int);
1380static void s_mips_end (int);
1381static void s_mips_frame (int);
1382static void s_mips_mask (int reg_type);
1383static void s_mips_stab (int);
1384static void s_mips_weakext (int);
1385static void s_mips_file (int);
1386static void s_mips_loc (int);
9e009953 1387static bfd_boolean pic_need_relax (symbolS *);
4a6a3df4 1388static int relaxed_branch_length (fragS *, asection *, int);
df58fc94
RS
1389static int relaxed_micromips_16bit_branch_length (fragS *, asection *, int);
1390static int relaxed_micromips_32bit_branch_length (fragS *, asection *, int);
919731af 1391static void file_mips_check_options (void);
e7af610e
NC
1392
1393/* Table and functions used to map between CPU/ISA names, and
1394 ISA levels, and CPU numbers. */
1395
e972090a
NC
1396struct mips_cpu_info
1397{
e7af610e 1398 const char *name; /* CPU or ISA name. */
d16afab6
RS
1399 int flags; /* MIPS_CPU_* flags. */
1400 int ase; /* Set of ASEs implemented by the CPU. */
e7af610e
NC
1401 int isa; /* ISA level. */
1402 int cpu; /* CPU number (default CPU if ISA). */
1403};
1404
ad3fea08 1405#define MIPS_CPU_IS_ISA 0x0001 /* Is this an ISA? (If 0, a CPU.) */
ad3fea08 1406
17a2f251
TS
1407static const struct mips_cpu_info *mips_parse_cpu (const char *, const char *);
1408static const struct mips_cpu_info *mips_cpu_info_from_isa (int);
1409static const struct mips_cpu_info *mips_cpu_info_from_arch (int);
252b5132 1410\f
c31f3936
RS
1411/* Command-line options. */
1412const char *md_shortopts = "O::g::G:";
1413
1414enum options
1415 {
1416 OPTION_MARCH = OPTION_MD_BASE,
1417 OPTION_MTUNE,
1418 OPTION_MIPS1,
1419 OPTION_MIPS2,
1420 OPTION_MIPS3,
1421 OPTION_MIPS4,
1422 OPTION_MIPS5,
1423 OPTION_MIPS32,
1424 OPTION_MIPS64,
1425 OPTION_MIPS32R2,
ae52f483
AB
1426 OPTION_MIPS32R3,
1427 OPTION_MIPS32R5,
7361da2c 1428 OPTION_MIPS32R6,
c31f3936 1429 OPTION_MIPS64R2,
ae52f483
AB
1430 OPTION_MIPS64R3,
1431 OPTION_MIPS64R5,
7361da2c 1432 OPTION_MIPS64R6,
c31f3936
RS
1433 OPTION_MIPS16,
1434 OPTION_NO_MIPS16,
1435 OPTION_MIPS3D,
1436 OPTION_NO_MIPS3D,
1437 OPTION_MDMX,
1438 OPTION_NO_MDMX,
1439 OPTION_DSP,
1440 OPTION_NO_DSP,
1441 OPTION_MT,
1442 OPTION_NO_MT,
1443 OPTION_VIRT,
1444 OPTION_NO_VIRT,
56d438b1
CF
1445 OPTION_MSA,
1446 OPTION_NO_MSA,
c31f3936
RS
1447 OPTION_SMARTMIPS,
1448 OPTION_NO_SMARTMIPS,
1449 OPTION_DSPR2,
1450 OPTION_NO_DSPR2,
8f4f9071
MF
1451 OPTION_DSPR3,
1452 OPTION_NO_DSPR3,
c31f3936
RS
1453 OPTION_EVA,
1454 OPTION_NO_EVA,
7d64c587
AB
1455 OPTION_XPA,
1456 OPTION_NO_XPA,
c31f3936
RS
1457 OPTION_MICROMIPS,
1458 OPTION_NO_MICROMIPS,
1459 OPTION_MCU,
1460 OPTION_NO_MCU,
25499ac7
MR
1461 OPTION_MIPS16E2,
1462 OPTION_NO_MIPS16E2,
730c3174
SE
1463 OPTION_CRC,
1464 OPTION_NO_CRC,
c31f3936
RS
1465 OPTION_M4650,
1466 OPTION_NO_M4650,
1467 OPTION_M4010,
1468 OPTION_NO_M4010,
1469 OPTION_M4100,
1470 OPTION_NO_M4100,
1471 OPTION_M3900,
1472 OPTION_NO_M3900,
1473 OPTION_M7000_HILO_FIX,
1474 OPTION_MNO_7000_HILO_FIX,
1475 OPTION_FIX_24K,
1476 OPTION_NO_FIX_24K,
a8d14a88
CM
1477 OPTION_FIX_RM7000,
1478 OPTION_NO_FIX_RM7000,
c31f3936
RS
1479 OPTION_FIX_LOONGSON2F_JUMP,
1480 OPTION_NO_FIX_LOONGSON2F_JUMP,
1481 OPTION_FIX_LOONGSON2F_NOP,
1482 OPTION_NO_FIX_LOONGSON2F_NOP,
1483 OPTION_FIX_VR4120,
1484 OPTION_NO_FIX_VR4120,
1485 OPTION_FIX_VR4130,
1486 OPTION_NO_FIX_VR4130,
1487 OPTION_FIX_CN63XXP1,
1488 OPTION_NO_FIX_CN63XXP1,
1489 OPTION_TRAP,
1490 OPTION_BREAK,
1491 OPTION_EB,
1492 OPTION_EL,
1493 OPTION_FP32,
1494 OPTION_GP32,
1495 OPTION_CONSTRUCT_FLOATS,
1496 OPTION_NO_CONSTRUCT_FLOATS,
1497 OPTION_FP64,
351cdf24 1498 OPTION_FPXX,
c31f3936
RS
1499 OPTION_GP64,
1500 OPTION_RELAX_BRANCH,
1501 OPTION_NO_RELAX_BRANCH,
8b10b0b3
MR
1502 OPTION_IGNORE_BRANCH_ISA,
1503 OPTION_NO_IGNORE_BRANCH_ISA,
833794fc
MR
1504 OPTION_INSN32,
1505 OPTION_NO_INSN32,
c31f3936
RS
1506 OPTION_MSHARED,
1507 OPTION_MNO_SHARED,
1508 OPTION_MSYM32,
1509 OPTION_MNO_SYM32,
1510 OPTION_SOFT_FLOAT,
1511 OPTION_HARD_FLOAT,
1512 OPTION_SINGLE_FLOAT,
1513 OPTION_DOUBLE_FLOAT,
1514 OPTION_32,
c31f3936
RS
1515 OPTION_CALL_SHARED,
1516 OPTION_CALL_NONPIC,
1517 OPTION_NON_SHARED,
1518 OPTION_XGOT,
1519 OPTION_MABI,
1520 OPTION_N32,
1521 OPTION_64,
1522 OPTION_MDEBUG,
1523 OPTION_NO_MDEBUG,
1524 OPTION_PDR,
1525 OPTION_NO_PDR,
1526 OPTION_MVXWORKS_PIC,
ba92f887 1527 OPTION_NAN,
351cdf24
MF
1528 OPTION_ODD_SPREG,
1529 OPTION_NO_ODD_SPREG,
6f20c942
FS
1530 OPTION_GINV,
1531 OPTION_NO_GINV,
c31f3936
RS
1532 OPTION_END_OF_ENUM
1533 };
1534
1535struct option md_longopts[] =
1536{
1537 /* Options which specify architecture. */
1538 {"march", required_argument, NULL, OPTION_MARCH},
1539 {"mtune", required_argument, NULL, OPTION_MTUNE},
1540 {"mips0", no_argument, NULL, OPTION_MIPS1},
1541 {"mips1", no_argument, NULL, OPTION_MIPS1},
1542 {"mips2", no_argument, NULL, OPTION_MIPS2},
1543 {"mips3", no_argument, NULL, OPTION_MIPS3},
1544 {"mips4", no_argument, NULL, OPTION_MIPS4},
1545 {"mips5", no_argument, NULL, OPTION_MIPS5},
1546 {"mips32", no_argument, NULL, OPTION_MIPS32},
1547 {"mips64", no_argument, NULL, OPTION_MIPS64},
1548 {"mips32r2", no_argument, NULL, OPTION_MIPS32R2},
ae52f483
AB
1549 {"mips32r3", no_argument, NULL, OPTION_MIPS32R3},
1550 {"mips32r5", no_argument, NULL, OPTION_MIPS32R5},
7361da2c 1551 {"mips32r6", no_argument, NULL, OPTION_MIPS32R6},
c31f3936 1552 {"mips64r2", no_argument, NULL, OPTION_MIPS64R2},
ae52f483
AB
1553 {"mips64r3", no_argument, NULL, OPTION_MIPS64R3},
1554 {"mips64r5", no_argument, NULL, OPTION_MIPS64R5},
7361da2c 1555 {"mips64r6", no_argument, NULL, OPTION_MIPS64R6},
c31f3936
RS
1556
1557 /* Options which specify Application Specific Extensions (ASEs). */
1558 {"mips16", no_argument, NULL, OPTION_MIPS16},
1559 {"no-mips16", no_argument, NULL, OPTION_NO_MIPS16},
1560 {"mips3d", no_argument, NULL, OPTION_MIPS3D},
1561 {"no-mips3d", no_argument, NULL, OPTION_NO_MIPS3D},
1562 {"mdmx", no_argument, NULL, OPTION_MDMX},
1563 {"no-mdmx", no_argument, NULL, OPTION_NO_MDMX},
1564 {"mdsp", no_argument, NULL, OPTION_DSP},
1565 {"mno-dsp", no_argument, NULL, OPTION_NO_DSP},
1566 {"mmt", no_argument, NULL, OPTION_MT},
1567 {"mno-mt", no_argument, NULL, OPTION_NO_MT},
1568 {"msmartmips", no_argument, NULL, OPTION_SMARTMIPS},
1569 {"mno-smartmips", no_argument, NULL, OPTION_NO_SMARTMIPS},
1570 {"mdspr2", no_argument, NULL, OPTION_DSPR2},
1571 {"mno-dspr2", no_argument, NULL, OPTION_NO_DSPR2},
8f4f9071
MF
1572 {"mdspr3", no_argument, NULL, OPTION_DSPR3},
1573 {"mno-dspr3", no_argument, NULL, OPTION_NO_DSPR3},
c31f3936
RS
1574 {"meva", no_argument, NULL, OPTION_EVA},
1575 {"mno-eva", no_argument, NULL, OPTION_NO_EVA},
1576 {"mmicromips", no_argument, NULL, OPTION_MICROMIPS},
1577 {"mno-micromips", no_argument, NULL, OPTION_NO_MICROMIPS},
1578 {"mmcu", no_argument, NULL, OPTION_MCU},
1579 {"mno-mcu", no_argument, NULL, OPTION_NO_MCU},
1580 {"mvirt", no_argument, NULL, OPTION_VIRT},
1581 {"mno-virt", no_argument, NULL, OPTION_NO_VIRT},
56d438b1
CF
1582 {"mmsa", no_argument, NULL, OPTION_MSA},
1583 {"mno-msa", no_argument, NULL, OPTION_NO_MSA},
7d64c587
AB
1584 {"mxpa", no_argument, NULL, OPTION_XPA},
1585 {"mno-xpa", no_argument, NULL, OPTION_NO_XPA},
25499ac7
MR
1586 {"mmips16e2", no_argument, NULL, OPTION_MIPS16E2},
1587 {"mno-mips16e2", no_argument, NULL, OPTION_NO_MIPS16E2},
730c3174
SE
1588 {"mcrc", no_argument, NULL, OPTION_CRC},
1589 {"mno-crc", no_argument, NULL, OPTION_NO_CRC},
6f20c942
FS
1590 {"mginv", no_argument, NULL, OPTION_GINV},
1591 {"mno-ginv", no_argument, NULL, OPTION_NO_GINV},
c31f3936
RS
1592
1593 /* Old-style architecture options. Don't add more of these. */
1594 {"m4650", no_argument, NULL, OPTION_M4650},
1595 {"no-m4650", no_argument, NULL, OPTION_NO_M4650},
1596 {"m4010", no_argument, NULL, OPTION_M4010},
1597 {"no-m4010", no_argument, NULL, OPTION_NO_M4010},
1598 {"m4100", no_argument, NULL, OPTION_M4100},
1599 {"no-m4100", no_argument, NULL, OPTION_NO_M4100},
1600 {"m3900", no_argument, NULL, OPTION_M3900},
1601 {"no-m3900", no_argument, NULL, OPTION_NO_M3900},
1602
1603 /* Options which enable bug fixes. */
1604 {"mfix7000", no_argument, NULL, OPTION_M7000_HILO_FIX},
1605 {"no-fix-7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1606 {"mno-fix7000", no_argument, NULL, OPTION_MNO_7000_HILO_FIX},
1607 {"mfix-loongson2f-jump", no_argument, NULL, OPTION_FIX_LOONGSON2F_JUMP},
1608 {"mno-fix-loongson2f-jump", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_JUMP},
1609 {"mfix-loongson2f-nop", no_argument, NULL, OPTION_FIX_LOONGSON2F_NOP},
1610 {"mno-fix-loongson2f-nop", no_argument, NULL, OPTION_NO_FIX_LOONGSON2F_NOP},
1611 {"mfix-vr4120", no_argument, NULL, OPTION_FIX_VR4120},
1612 {"mno-fix-vr4120", no_argument, NULL, OPTION_NO_FIX_VR4120},
1613 {"mfix-vr4130", no_argument, NULL, OPTION_FIX_VR4130},
1614 {"mno-fix-vr4130", no_argument, NULL, OPTION_NO_FIX_VR4130},
1615 {"mfix-24k", no_argument, NULL, OPTION_FIX_24K},
1616 {"mno-fix-24k", no_argument, NULL, OPTION_NO_FIX_24K},
a8d14a88
CM
1617 {"mfix-rm7000", no_argument, NULL, OPTION_FIX_RM7000},
1618 {"mno-fix-rm7000", no_argument, NULL, OPTION_NO_FIX_RM7000},
c31f3936
RS
1619 {"mfix-cn63xxp1", no_argument, NULL, OPTION_FIX_CN63XXP1},
1620 {"mno-fix-cn63xxp1", no_argument, NULL, OPTION_NO_FIX_CN63XXP1},
1621
1622 /* Miscellaneous options. */
1623 {"trap", no_argument, NULL, OPTION_TRAP},
1624 {"no-break", no_argument, NULL, OPTION_TRAP},
1625 {"break", no_argument, NULL, OPTION_BREAK},
1626 {"no-trap", no_argument, NULL, OPTION_BREAK},
1627 {"EB", no_argument, NULL, OPTION_EB},
1628 {"EL", no_argument, NULL, OPTION_EL},
1629 {"mfp32", no_argument, NULL, OPTION_FP32},
1630 {"mgp32", no_argument, NULL, OPTION_GP32},
1631 {"construct-floats", no_argument, NULL, OPTION_CONSTRUCT_FLOATS},
1632 {"no-construct-floats", no_argument, NULL, OPTION_NO_CONSTRUCT_FLOATS},
1633 {"mfp64", no_argument, NULL, OPTION_FP64},
351cdf24 1634 {"mfpxx", no_argument, NULL, OPTION_FPXX},
c31f3936
RS
1635 {"mgp64", no_argument, NULL, OPTION_GP64},
1636 {"relax-branch", no_argument, NULL, OPTION_RELAX_BRANCH},
1637 {"no-relax-branch", no_argument, NULL, OPTION_NO_RELAX_BRANCH},
8b10b0b3
MR
1638 {"mignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA},
1639 {"mno-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA},
833794fc
MR
1640 {"minsn32", no_argument, NULL, OPTION_INSN32},
1641 {"mno-insn32", no_argument, NULL, OPTION_NO_INSN32},
c31f3936
RS
1642 {"mshared", no_argument, NULL, OPTION_MSHARED},
1643 {"mno-shared", no_argument, NULL, OPTION_MNO_SHARED},
1644 {"msym32", no_argument, NULL, OPTION_MSYM32},
1645 {"mno-sym32", no_argument, NULL, OPTION_MNO_SYM32},
1646 {"msoft-float", no_argument, NULL, OPTION_SOFT_FLOAT},
1647 {"mhard-float", no_argument, NULL, OPTION_HARD_FLOAT},
1648 {"msingle-float", no_argument, NULL, OPTION_SINGLE_FLOAT},
1649 {"mdouble-float", no_argument, NULL, OPTION_DOUBLE_FLOAT},
351cdf24
MF
1650 {"modd-spreg", no_argument, NULL, OPTION_ODD_SPREG},
1651 {"mno-odd-spreg", no_argument, NULL, OPTION_NO_ODD_SPREG},
c31f3936
RS
1652
1653 /* Strictly speaking this next option is ELF specific,
1654 but we allow it for other ports as well in order to
1655 make testing easier. */
1656 {"32", no_argument, NULL, OPTION_32},
1657
1658 /* ELF-specific options. */
c31f3936
RS
1659 {"KPIC", no_argument, NULL, OPTION_CALL_SHARED},
1660 {"call_shared", no_argument, NULL, OPTION_CALL_SHARED},
1661 {"call_nonpic", no_argument, NULL, OPTION_CALL_NONPIC},
1662 {"non_shared", no_argument, NULL, OPTION_NON_SHARED},
1663 {"xgot", no_argument, NULL, OPTION_XGOT},
1664 {"mabi", required_argument, NULL, OPTION_MABI},
1665 {"n32", no_argument, NULL, OPTION_N32},
1666 {"64", no_argument, NULL, OPTION_64},
1667 {"mdebug", no_argument, NULL, OPTION_MDEBUG},
1668 {"no-mdebug", no_argument, NULL, OPTION_NO_MDEBUG},
1669 {"mpdr", no_argument, NULL, OPTION_PDR},
1670 {"mno-pdr", no_argument, NULL, OPTION_NO_PDR},
1671 {"mvxworks-pic", no_argument, NULL, OPTION_MVXWORKS_PIC},
ba92f887 1672 {"mnan", required_argument, NULL, OPTION_NAN},
c31f3936
RS
1673
1674 {NULL, no_argument, NULL, 0}
1675};
1676size_t md_longopts_size = sizeof (md_longopts);
1677\f
c6278170
RS
1678/* Information about either an Application Specific Extension or an
1679 optional architecture feature that, for simplicity, we treat in the
1680 same way as an ASE. */
1681struct mips_ase
1682{
1683 /* The name of the ASE, used in both the command-line and .set options. */
1684 const char *name;
1685
1686 /* The associated ASE_* flags. If the ASE is available on both 32-bit
1687 and 64-bit architectures, the flags here refer to the subset that
1688 is available on both. */
1689 unsigned int flags;
1690
1691 /* The ASE_* flag used for instructions that are available on 64-bit
1692 architectures but that are not included in FLAGS. */
1693 unsigned int flags64;
1694
1695 /* The command-line options that turn the ASE on and off. */
1696 int option_on;
1697 int option_off;
1698
1699 /* The minimum required architecture revisions for MIPS32, MIPS64,
1700 microMIPS32 and microMIPS64, or -1 if the extension isn't supported. */
1701 int mips32_rev;
1702 int mips64_rev;
1703 int micromips32_rev;
1704 int micromips64_rev;
7361da2c
AB
1705
1706 /* The architecture where the ASE was removed or -1 if the extension has not
1707 been removed. */
1708 int rem_rev;
c6278170
RS
1709};
1710
1711/* A table of all supported ASEs. */
1712static const struct mips_ase mips_ases[] = {
1713 { "dsp", ASE_DSP, ASE_DSP64,
1714 OPTION_DSP, OPTION_NO_DSP,
7361da2c
AB
1715 2, 2, 2, 2,
1716 -1 },
c6278170
RS
1717
1718 { "dspr2", ASE_DSP | ASE_DSPR2, 0,
1719 OPTION_DSPR2, OPTION_NO_DSPR2,
7361da2c
AB
1720 2, 2, 2, 2,
1721 -1 },
c6278170 1722
8f4f9071
MF
1723 { "dspr3", ASE_DSP | ASE_DSPR2 | ASE_DSPR3, 0,
1724 OPTION_DSPR3, OPTION_NO_DSPR3,
1725 6, 6, -1, -1,
1726 -1 },
1727
c6278170
RS
1728 { "eva", ASE_EVA, 0,
1729 OPTION_EVA, OPTION_NO_EVA,
7361da2c
AB
1730 2, 2, 2, 2,
1731 -1 },
c6278170
RS
1732
1733 { "mcu", ASE_MCU, 0,
1734 OPTION_MCU, OPTION_NO_MCU,
7361da2c
AB
1735 2, 2, 2, 2,
1736 -1 },
c6278170
RS
1737
1738 /* Deprecated in MIPS64r5, but we don't implement that yet. */
1739 { "mdmx", ASE_MDMX, 0,
1740 OPTION_MDMX, OPTION_NO_MDMX,
7361da2c
AB
1741 -1, 1, -1, -1,
1742 6 },
c6278170
RS
1743
1744 /* Requires 64-bit FPRs, so the minimum MIPS32 revision is 2. */
1745 { "mips3d", ASE_MIPS3D, 0,
1746 OPTION_MIPS3D, OPTION_NO_MIPS3D,
7361da2c
AB
1747 2, 1, -1, -1,
1748 6 },
c6278170
RS
1749
1750 { "mt", ASE_MT, 0,
1751 OPTION_MT, OPTION_NO_MT,
7361da2c
AB
1752 2, 2, -1, -1,
1753 -1 },
c6278170
RS
1754
1755 { "smartmips", ASE_SMARTMIPS, 0,
1756 OPTION_SMARTMIPS, OPTION_NO_SMARTMIPS,
7361da2c
AB
1757 1, -1, -1, -1,
1758 6 },
c6278170
RS
1759
1760 { "virt", ASE_VIRT, ASE_VIRT64,
1761 OPTION_VIRT, OPTION_NO_VIRT,
7361da2c
AB
1762 2, 2, 2, 2,
1763 -1 },
56d438b1
CF
1764
1765 { "msa", ASE_MSA, ASE_MSA64,
1766 OPTION_MSA, OPTION_NO_MSA,
7361da2c
AB
1767 2, 2, 2, 2,
1768 -1 },
7d64c587
AB
1769
1770 { "xpa", ASE_XPA, 0,
1771 OPTION_XPA, OPTION_NO_XPA,
909b4e3d 1772 2, 2, 2, 2,
7361da2c 1773 -1 },
25499ac7
MR
1774
1775 { "mips16e2", ASE_MIPS16E2, 0,
1776 OPTION_MIPS16E2, OPTION_NO_MIPS16E2,
1777 2, 2, -1, -1,
1778 6 },
730c3174
SE
1779
1780 { "crc", ASE_CRC, ASE_CRC64,
1781 OPTION_CRC, OPTION_NO_CRC,
1782 6, 6, -1, -1,
1783 -1 },
6f20c942
FS
1784
1785 { "ginv", ASE_GINV, 0,
1786 OPTION_GINV, OPTION_NO_GINV,
1787 6, 6, 6, 6,
1788 -1 },
c6278170
RS
1789};
1790
1791/* The set of ASEs that require -mfp64. */
82bda27b 1792#define FP64_ASES (ASE_MIPS3D | ASE_MDMX | ASE_MSA)
c6278170
RS
1793
1794/* Groups of ASE_* flags that represent different revisions of an ASE. */
1795static const unsigned int mips_ase_groups[] = {
8f4f9071 1796 ASE_DSP | ASE_DSPR2 | ASE_DSPR3
c6278170
RS
1797};
1798\f
252b5132
RH
1799/* Pseudo-op table.
1800
1801 The following pseudo-ops from the Kane and Heinrich MIPS book
1802 should be defined here, but are currently unsupported: .alias,
1803 .galive, .gjaldef, .gjrlive, .livereg, .noalias.
1804
1805 The following pseudo-ops from the Kane and Heinrich MIPS book are
1806 specific to the type of debugging information being generated, and
1807 should be defined by the object format: .aent, .begin, .bend,
1808 .bgnb, .end, .endb, .ent, .fmask, .frame, .loc, .mask, .verstamp,
1809 .vreg.
1810
1811 The following pseudo-ops from the Kane and Heinrich MIPS book are
1812 not MIPS CPU specific, but are also not specific to the object file
1813 format. This file is probably the best place to define them, but
d84bcf09 1814 they are not currently supported: .asm0, .endr, .lab, .struct. */
252b5132 1815
e972090a
NC
1816static const pseudo_typeS mips_pseudo_table[] =
1817{
beae10d5 1818 /* MIPS specific pseudo-ops. */
252b5132
RH
1819 {"option", s_option, 0},
1820 {"set", s_mipsset, 0},
1821 {"rdata", s_change_sec, 'r'},
1822 {"sdata", s_change_sec, 's'},
1823 {"livereg", s_ignore, 0},
1824 {"abicalls", s_abicalls, 0},
1825 {"cpload", s_cpload, 0},
6478892d
TS
1826 {"cpsetup", s_cpsetup, 0},
1827 {"cplocal", s_cplocal, 0},
252b5132 1828 {"cprestore", s_cprestore, 0},
6478892d 1829 {"cpreturn", s_cpreturn, 0},
741d6ea8
JM
1830 {"dtprelword", s_dtprelword, 0},
1831 {"dtpreldword", s_dtpreldword, 0},
d0f13682
CLT
1832 {"tprelword", s_tprelword, 0},
1833 {"tpreldword", s_tpreldword, 0},
6478892d 1834 {"gpvalue", s_gpvalue, 0},
252b5132 1835 {"gpword", s_gpword, 0},
10181a0d 1836 {"gpdword", s_gpdword, 0},
a3f278e2 1837 {"ehword", s_ehword, 0},
252b5132
RH
1838 {"cpadd", s_cpadd, 0},
1839 {"insn", s_insn, 0},
ba92f887 1840 {"nan", s_nan, 0},
919731af 1841 {"module", s_module, 0},
252b5132 1842
beae10d5 1843 /* Relatively generic pseudo-ops that happen to be used on MIPS
252b5132 1844 chips. */
38a57ae7 1845 {"asciiz", stringer, 8 + 1},
252b5132
RH
1846 {"bss", s_change_sec, 'b'},
1847 {"err", s_err, 0},
1848 {"half", s_cons, 1},
1849 {"dword", s_cons, 3},
1850 {"weakext", s_mips_weakext, 0},
7c752c2a
TS
1851 {"origin", s_org, 0},
1852 {"repeat", s_rept, 0},
252b5132 1853
998b3c36
MR
1854 /* For MIPS this is non-standard, but we define it for consistency. */
1855 {"sbss", s_change_sec, 'B'},
1856
beae10d5 1857 /* These pseudo-ops are defined in read.c, but must be overridden
252b5132
RH
1858 here for one reason or another. */
1859 {"align", s_align, 0},
1860 {"byte", s_cons, 0},
1861 {"data", s_change_sec, 'd'},
1862 {"double", s_float_cons, 'd'},
1863 {"float", s_float_cons, 'f'},
1864 {"globl", s_mips_globl, 0},
1865 {"global", s_mips_globl, 0},
1866 {"hword", s_cons, 1},
1867 {"int", s_cons, 2},
1868 {"long", s_cons, 2},
1869 {"octa", s_cons, 4},
1870 {"quad", s_cons, 3},
cca86cc8 1871 {"section", s_change_section, 0},
252b5132
RH
1872 {"short", s_cons, 1},
1873 {"single", s_float_cons, 'f'},
754e2bb9 1874 {"stabd", s_mips_stab, 'd'},
252b5132 1875 {"stabn", s_mips_stab, 'n'},
754e2bb9 1876 {"stabs", s_mips_stab, 's'},
252b5132
RH
1877 {"text", s_change_sec, 't'},
1878 {"word", s_cons, 2},
add56521 1879
add56521 1880 { "extern", ecoff_directive_extern, 0},
add56521 1881
43841e91 1882 { NULL, NULL, 0 },
252b5132
RH
1883};
1884
e972090a
NC
1885static const pseudo_typeS mips_nonecoff_pseudo_table[] =
1886{
beae10d5
KH
1887 /* These pseudo-ops should be defined by the object file format.
1888 However, a.out doesn't support them, so we have versions here. */
252b5132
RH
1889 {"aent", s_mips_ent, 1},
1890 {"bgnb", s_ignore, 0},
1891 {"end", s_mips_end, 0},
1892 {"endb", s_ignore, 0},
1893 {"ent", s_mips_ent, 0},
c5dd6aab 1894 {"file", s_mips_file, 0},
252b5132
RH
1895 {"fmask", s_mips_mask, 'F'},
1896 {"frame", s_mips_frame, 0},
c5dd6aab 1897 {"loc", s_mips_loc, 0},
252b5132
RH
1898 {"mask", s_mips_mask, 'R'},
1899 {"verstamp", s_ignore, 0},
43841e91 1900 { NULL, NULL, 0 },
252b5132
RH
1901};
1902
3ae8dd8d
MR
1903/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
1904 purpose of the `.dc.a' internal pseudo-op. */
1905
1906int
1907mips_address_bytes (void)
1908{
919731af 1909 file_mips_check_options ();
3ae8dd8d
MR
1910 return HAVE_64BIT_ADDRESSES ? 8 : 4;
1911}
1912
17a2f251 1913extern void pop_insert (const pseudo_typeS *);
252b5132
RH
1914
1915void
17a2f251 1916mips_pop_insert (void)
252b5132
RH
1917{
1918 pop_insert (mips_pseudo_table);
1919 if (! ECOFF_DEBUGGING)
1920 pop_insert (mips_nonecoff_pseudo_table);
1921}
1922\f
1923/* Symbols labelling the current insn. */
1924
e972090a
NC
1925struct insn_label_list
1926{
252b5132
RH
1927 struct insn_label_list *next;
1928 symbolS *label;
1929};
1930
252b5132 1931static struct insn_label_list *free_insn_labels;
742a56fe 1932#define label_list tc_segment_info_data.labels
252b5132 1933
17a2f251 1934static void mips_clear_insn_labels (void);
df58fc94
RS
1935static void mips_mark_labels (void);
1936static void mips_compressed_mark_labels (void);
252b5132
RH
1937
1938static inline void
17a2f251 1939mips_clear_insn_labels (void)
252b5132 1940{
ed9e98c2 1941 struct insn_label_list **pl;
a8dbcb85 1942 segment_info_type *si;
252b5132 1943
a8dbcb85
TS
1944 if (now_seg)
1945 {
1946 for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
1947 ;
3739860c 1948
a8dbcb85
TS
1949 si = seg_info (now_seg);
1950 *pl = si->label_list;
1951 si->label_list = NULL;
1952 }
252b5132 1953}
a8dbcb85 1954
df58fc94
RS
1955/* Mark instruction labels in MIPS16/microMIPS mode. */
1956
1957static inline void
1958mips_mark_labels (void)
1959{
1960 if (HAVE_CODE_COMPRESSION)
1961 mips_compressed_mark_labels ();
1962}
252b5132
RH
1963\f
1964static char *expr_end;
1965
e423441d 1966/* An expression in a macro instruction. This is set by mips_ip and
b0e6f033 1967 mips16_ip and when populated is always an O_constant. */
252b5132
RH
1968
1969static expressionS imm_expr;
252b5132 1970
77bd4346
RS
1971/* The relocatable field in an instruction and the relocs associated
1972 with it. These variables are used for instructions like LUI and
1973 JAL as well as true offsets. They are also used for address
1974 operands in macros. */
252b5132 1975
77bd4346 1976static expressionS offset_expr;
f6688943
TS
1977static bfd_reloc_code_real_type offset_reloc[3]
1978 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 1979
df58fc94
RS
1980/* This is set to the resulting size of the instruction to be produced
1981 by mips16_ip if an explicit extension is used or by mips_ip if an
1982 explicit size is supplied. */
252b5132 1983
df58fc94 1984static unsigned int forced_insn_length;
252b5132 1985
e1b47bd5
RS
1986/* True if we are assembling an instruction. All dot symbols defined during
1987 this time should be treated as code labels. */
1988
1989static bfd_boolean mips_assembling_insn;
1990
ecb4347a
DJ
1991/* The pdr segment for per procedure frame/regmask info. Not used for
1992 ECOFF debugging. */
252b5132
RH
1993
1994static segT pdr_seg;
252b5132 1995
e013f690
TS
1996/* The default target format to use. */
1997
aeffff67
RS
1998#if defined (TE_FreeBSD)
1999#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips-freebsd"
2000#elif defined (TE_TMIPS)
2001#define ELF_TARGET(PREFIX, ENDIAN) PREFIX "trad" ENDIAN "mips"
2002#else
2003#define ELF_TARGET(PREFIX, ENDIAN) PREFIX ENDIAN "mips"
2004#endif
2005
e013f690 2006const char *
17a2f251 2007mips_target_format (void)
e013f690
TS
2008{
2009 switch (OUTPUT_FLAVOR)
2010 {
e013f690 2011 case bfd_target_elf_flavour:
0a44bf69
RS
2012#ifdef TE_VXWORKS
2013 if (!HAVE_64BIT_OBJECTS && !HAVE_NEWABI)
2014 return (target_big_endian
2015 ? "elf32-bigmips-vxworks"
2016 : "elf32-littlemips-vxworks");
2017#endif
e013f690 2018 return (target_big_endian
cfe86eaa 2019 ? (HAVE_64BIT_OBJECTS
aeffff67 2020 ? ELF_TARGET ("elf64-", "big")
cfe86eaa 2021 : (HAVE_NEWABI
aeffff67
RS
2022 ? ELF_TARGET ("elf32-n", "big")
2023 : ELF_TARGET ("elf32-", "big")))
cfe86eaa 2024 : (HAVE_64BIT_OBJECTS
aeffff67 2025 ? ELF_TARGET ("elf64-", "little")
cfe86eaa 2026 : (HAVE_NEWABI
aeffff67
RS
2027 ? ELF_TARGET ("elf32-n", "little")
2028 : ELF_TARGET ("elf32-", "little"))));
e013f690
TS
2029 default:
2030 abort ();
2031 return NULL;
2032 }
2033}
2034
c6278170
RS
2035/* Return the ISA revision that is currently in use, or 0 if we are
2036 generating code for MIPS V or below. */
2037
2038static int
2039mips_isa_rev (void)
2040{
2041 if (mips_opts.isa == ISA_MIPS32R2 || mips_opts.isa == ISA_MIPS64R2)
2042 return 2;
2043
ae52f483
AB
2044 if (mips_opts.isa == ISA_MIPS32R3 || mips_opts.isa == ISA_MIPS64R3)
2045 return 3;
2046
2047 if (mips_opts.isa == ISA_MIPS32R5 || mips_opts.isa == ISA_MIPS64R5)
2048 return 5;
2049
7361da2c
AB
2050 if (mips_opts.isa == ISA_MIPS32R6 || mips_opts.isa == ISA_MIPS64R6)
2051 return 6;
2052
c6278170
RS
2053 /* microMIPS implies revision 2 or above. */
2054 if (mips_opts.micromips)
2055 return 2;
2056
2057 if (mips_opts.isa == ISA_MIPS32 || mips_opts.isa == ISA_MIPS64)
2058 return 1;
2059
2060 return 0;
2061}
2062
2063/* Return the mask of all ASEs that are revisions of those in FLAGS. */
2064
2065static unsigned int
2066mips_ase_mask (unsigned int flags)
2067{
2068 unsigned int i;
2069
2070 for (i = 0; i < ARRAY_SIZE (mips_ase_groups); i++)
2071 if (flags & mips_ase_groups[i])
2072 flags |= mips_ase_groups[i];
2073 return flags;
2074}
2075
2076/* Check whether the current ISA supports ASE. Issue a warning if
2077 appropriate. */
2078
2079static void
2080mips_check_isa_supports_ase (const struct mips_ase *ase)
2081{
2082 const char *base;
2083 int min_rev, size;
2084 static unsigned int warned_isa;
2085 static unsigned int warned_fp32;
2086
2087 if (ISA_HAS_64BIT_REGS (mips_opts.isa))
2088 min_rev = mips_opts.micromips ? ase->micromips64_rev : ase->mips64_rev;
2089 else
2090 min_rev = mips_opts.micromips ? ase->micromips32_rev : ase->mips32_rev;
2091 if ((min_rev < 0 || mips_isa_rev () < min_rev)
2092 && (warned_isa & ase->flags) != ase->flags)
2093 {
2094 warned_isa |= ase->flags;
2095 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2096 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2097 if (min_rev < 0)
1661c76c 2098 as_warn (_("the %d-bit %s architecture does not support the"
c6278170
RS
2099 " `%s' extension"), size, base, ase->name);
2100 else
1661c76c 2101 as_warn (_("the `%s' extension requires %s%d revision %d or greater"),
c6278170
RS
2102 ase->name, base, size, min_rev);
2103 }
7361da2c
AB
2104 else if ((ase->rem_rev > 0 && mips_isa_rev () >= ase->rem_rev)
2105 && (warned_isa & ase->flags) != ase->flags)
2106 {
2107 warned_isa |= ase->flags;
2108 base = mips_opts.micromips ? "microMIPS" : "MIPS";
2109 size = ISA_HAS_64BIT_REGS (mips_opts.isa) ? 64 : 32;
2110 as_warn (_("the `%s' extension was removed in %s%d revision %d"),
2111 ase->name, base, size, ase->rem_rev);
2112 }
2113
c6278170 2114 if ((ase->flags & FP64_ASES)
0b35dfee 2115 && mips_opts.fp != 64
c6278170
RS
2116 && (warned_fp32 & ase->flags) != ase->flags)
2117 {
2118 warned_fp32 |= ase->flags;
1661c76c 2119 as_warn (_("the `%s' extension requires 64-bit FPRs"), ase->name);
c6278170
RS
2120 }
2121}
2122
2123/* Check all enabled ASEs to see whether they are supported by the
2124 chosen architecture. */
2125
2126static void
2127mips_check_isa_supports_ases (void)
2128{
2129 unsigned int i, mask;
2130
2131 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2132 {
2133 mask = mips_ase_mask (mips_ases[i].flags);
2134 if ((mips_opts.ase & mask) == mips_ases[i].flags)
2135 mips_check_isa_supports_ase (&mips_ases[i]);
2136 }
2137}
2138
2139/* Set the state of ASE to ENABLED_P. Return the mask of ASE_* flags
2140 that were affected. */
2141
2142static unsigned int
919731af 2143mips_set_ase (const struct mips_ase *ase, struct mips_set_options *opts,
2144 bfd_boolean enabled_p)
c6278170
RS
2145{
2146 unsigned int mask;
2147
2148 mask = mips_ase_mask (ase->flags);
919731af 2149 opts->ase &= ~mask;
92cebb3d
MR
2150
2151 /* Clear combination ASE flags, which need to be recalculated based on
2152 updated regular ASE settings. */
9785fc2a 2153 opts->ase &= ~(ASE_MIPS16E2_MT | ASE_XPA_VIRT);
92cebb3d 2154
c6278170 2155 if (enabled_p)
919731af 2156 opts->ase |= ase->flags;
25499ac7 2157
9785fc2a
MR
2158 /* The Virtualization ASE has eXtended Physical Addressing (XPA)
2159 instructions which are only valid when both ASEs are enabled.
2160 This sets the ASE_XPA_VIRT flag when both ASEs are present. */
2161 if ((opts->ase & (ASE_XPA | ASE_VIRT)) == (ASE_XPA | ASE_VIRT))
2162 {
2163 opts->ase |= ASE_XPA_VIRT;
2164 mask |= ASE_XPA_VIRT;
2165 }
25499ac7
MR
2166 if ((opts->ase & (ASE_MIPS16E2 | ASE_MT)) == (ASE_MIPS16E2 | ASE_MT))
2167 {
2168 opts->ase |= ASE_MIPS16E2_MT;
2169 mask |= ASE_MIPS16E2_MT;
2170 }
2171
c6278170
RS
2172 return mask;
2173}
2174
2175/* Return the ASE called NAME, or null if none. */
2176
2177static const struct mips_ase *
2178mips_lookup_ase (const char *name)
2179{
2180 unsigned int i;
2181
2182 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
2183 if (strcmp (name, mips_ases[i].name) == 0)
2184 return &mips_ases[i];
2185 return NULL;
2186}
2187
df58fc94 2188/* Return the length of a microMIPS instruction in bytes. If bits of
100b4f2e
MR
2189 the mask beyond the low 16 are 0, then it is a 16-bit instruction,
2190 otherwise it is a 32-bit instruction. */
df58fc94
RS
2191
2192static inline unsigned int
2193micromips_insn_length (const struct mips_opcode *mo)
2194{
7fd53920 2195 return mips_opcode_32bit_p (mo) ? 4 : 2;
df58fc94
RS
2196}
2197
5c04167a
RS
2198/* Return the length of MIPS16 instruction OPCODE. */
2199
2200static inline unsigned int
2201mips16_opcode_length (unsigned long opcode)
2202{
2203 return (opcode >> 16) == 0 ? 2 : 4;
2204}
2205
1e915849
RS
2206/* Return the length of instruction INSN. */
2207
2208static inline unsigned int
2209insn_length (const struct mips_cl_insn *insn)
2210{
df58fc94
RS
2211 if (mips_opts.micromips)
2212 return micromips_insn_length (insn->insn_mo);
2213 else if (mips_opts.mips16)
5c04167a 2214 return mips16_opcode_length (insn->insn_opcode);
df58fc94 2215 else
1e915849 2216 return 4;
1e915849
RS
2217}
2218
2219/* Initialise INSN from opcode entry MO. Leave its position unspecified. */
2220
2221static void
2222create_insn (struct mips_cl_insn *insn, const struct mips_opcode *mo)
2223{
2224 size_t i;
2225
2226 insn->insn_mo = mo;
1e915849
RS
2227 insn->insn_opcode = mo->match;
2228 insn->frag = NULL;
2229 insn->where = 0;
2230 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2231 insn->fixp[i] = NULL;
2232 insn->fixed_p = (mips_opts.noreorder > 0);
2233 insn->noreorder_p = (mips_opts.noreorder > 0);
2234 insn->mips16_absolute_jump_p = 0;
15be625d 2235 insn->complete_p = 0;
e407c74b 2236 insn->cleared_p = 0;
1e915849
RS
2237}
2238
fc76e730
RS
2239/* Get a list of all the operands in INSN. */
2240
2241static const struct mips_operand_array *
2242insn_operands (const struct mips_cl_insn *insn)
2243{
2244 if (insn->insn_mo >= &mips_opcodes[0]
2245 && insn->insn_mo < &mips_opcodes[NUMOPCODES])
2246 return &mips_operands[insn->insn_mo - &mips_opcodes[0]];
2247
2248 if (insn->insn_mo >= &mips16_opcodes[0]
2249 && insn->insn_mo < &mips16_opcodes[bfd_mips16_num_opcodes])
2250 return &mips16_operands[insn->insn_mo - &mips16_opcodes[0]];
2251
2252 if (insn->insn_mo >= &micromips_opcodes[0]
2253 && insn->insn_mo < &micromips_opcodes[bfd_micromips_num_opcodes])
2254 return &micromips_operands[insn->insn_mo - &micromips_opcodes[0]];
2255
2256 abort ();
2257}
2258
2259/* Get a description of operand OPNO of INSN. */
2260
2261static const struct mips_operand *
2262insn_opno (const struct mips_cl_insn *insn, unsigned opno)
2263{
2264 const struct mips_operand_array *operands;
2265
2266 operands = insn_operands (insn);
2267 if (opno >= MAX_OPERANDS || !operands->operand[opno])
2268 abort ();
2269 return operands->operand[opno];
2270}
2271
e077a1c8
RS
2272/* Install UVAL as the value of OPERAND in INSN. */
2273
2274static inline void
2275insn_insert_operand (struct mips_cl_insn *insn,
2276 const struct mips_operand *operand, unsigned int uval)
2277{
25499ac7
MR
2278 if (mips_opts.mips16
2279 && operand->type == OP_INT && operand->lsb == 0
2280 && mips_opcode_32bit_p (insn->insn_mo))
2281 insn->insn_opcode |= mips16_immed_extend (uval, operand->size);
2282 else
2283 insn->insn_opcode = mips_insert_operand (operand, insn->insn_opcode, uval);
e077a1c8
RS
2284}
2285
fc76e730
RS
2286/* Extract the value of OPERAND from INSN. */
2287
2288static inline unsigned
2289insn_extract_operand (const struct mips_cl_insn *insn,
2290 const struct mips_operand *operand)
2291{
2292 return mips_extract_operand (operand, insn->insn_opcode);
2293}
2294
df58fc94 2295/* Record the current MIPS16/microMIPS mode in now_seg. */
742a56fe
RS
2296
2297static void
df58fc94 2298mips_record_compressed_mode (void)
742a56fe
RS
2299{
2300 segment_info_type *si;
2301
2302 si = seg_info (now_seg);
2303 if (si->tc_segment_info_data.mips16 != mips_opts.mips16)
2304 si->tc_segment_info_data.mips16 = mips_opts.mips16;
df58fc94
RS
2305 if (si->tc_segment_info_data.micromips != mips_opts.micromips)
2306 si->tc_segment_info_data.micromips = mips_opts.micromips;
742a56fe
RS
2307}
2308
4d68580a
RS
2309/* Read a standard MIPS instruction from BUF. */
2310
2311static unsigned long
2312read_insn (char *buf)
2313{
2314 if (target_big_endian)
2315 return bfd_getb32 ((bfd_byte *) buf);
2316 else
2317 return bfd_getl32 ((bfd_byte *) buf);
2318}
2319
2320/* Write standard MIPS instruction INSN to BUF. Return a pointer to
2321 the next byte. */
2322
2323static char *
2324write_insn (char *buf, unsigned int insn)
2325{
2326 md_number_to_chars (buf, insn, 4);
2327 return buf + 4;
2328}
2329
2330/* Read a microMIPS or MIPS16 opcode from BUF, given that it
2331 has length LENGTH. */
2332
2333static unsigned long
2334read_compressed_insn (char *buf, unsigned int length)
2335{
2336 unsigned long insn;
2337 unsigned int i;
2338
2339 insn = 0;
2340 for (i = 0; i < length; i += 2)
2341 {
2342 insn <<= 16;
2343 if (target_big_endian)
2344 insn |= bfd_getb16 ((char *) buf);
2345 else
2346 insn |= bfd_getl16 ((char *) buf);
2347 buf += 2;
2348 }
2349 return insn;
2350}
2351
5c04167a
RS
2352/* Write microMIPS or MIPS16 instruction INSN to BUF, given that the
2353 instruction is LENGTH bytes long. Return a pointer to the next byte. */
2354
2355static char *
2356write_compressed_insn (char *buf, unsigned int insn, unsigned int length)
2357{
2358 unsigned int i;
2359
2360 for (i = 0; i < length; i += 2)
2361 md_number_to_chars (buf + i, insn >> ((length - i - 2) * 8), 2);
2362 return buf + length;
2363}
2364
1e915849
RS
2365/* Install INSN at the location specified by its "frag" and "where" fields. */
2366
2367static void
2368install_insn (const struct mips_cl_insn *insn)
2369{
2370 char *f = insn->frag->fr_literal + insn->where;
5c04167a
RS
2371 if (HAVE_CODE_COMPRESSION)
2372 write_compressed_insn (f, insn->insn_opcode, insn_length (insn));
1e915849 2373 else
4d68580a 2374 write_insn (f, insn->insn_opcode);
df58fc94 2375 mips_record_compressed_mode ();
1e915849
RS
2376}
2377
2378/* Move INSN to offset WHERE in FRAG. Adjust the fixups accordingly
2379 and install the opcode in the new location. */
2380
2381static void
2382move_insn (struct mips_cl_insn *insn, fragS *frag, long where)
2383{
2384 size_t i;
2385
2386 insn->frag = frag;
2387 insn->where = where;
2388 for (i = 0; i < ARRAY_SIZE (insn->fixp); i++)
2389 if (insn->fixp[i] != NULL)
2390 {
2391 insn->fixp[i]->fx_frag = frag;
2392 insn->fixp[i]->fx_where = where;
2393 }
2394 install_insn (insn);
2395}
2396
2397/* Add INSN to the end of the output. */
2398
2399static void
2400add_fixed_insn (struct mips_cl_insn *insn)
2401{
2402 char *f = frag_more (insn_length (insn));
2403 move_insn (insn, frag_now, f - frag_now->fr_literal);
2404}
2405
2406/* Start a variant frag and move INSN to the start of the variant part,
2407 marking it as fixed. The other arguments are as for frag_var. */
2408
2409static void
2410add_relaxed_insn (struct mips_cl_insn *insn, int max_chars, int var,
2411 relax_substateT subtype, symbolS *symbol, offsetT offset)
2412{
2413 frag_grow (max_chars);
2414 move_insn (insn, frag_now, frag_more (0) - frag_now->fr_literal);
2415 insn->fixed_p = 1;
2416 frag_var (rs_machine_dependent, max_chars, var,
2417 subtype, symbol, offset, NULL);
2418}
2419
2420/* Insert N copies of INSN into the history buffer, starting at
2421 position FIRST. Neither FIRST nor N need to be clipped. */
2422
2423static void
2424insert_into_history (unsigned int first, unsigned int n,
2425 const struct mips_cl_insn *insn)
2426{
2427 if (mips_relax.sequence != 2)
2428 {
2429 unsigned int i;
2430
2431 for (i = ARRAY_SIZE (history); i-- > first;)
2432 if (i >= first + n)
2433 history[i] = history[i - n];
2434 else
2435 history[i] = *insn;
2436 }
2437}
2438
e3de51ce
RS
2439/* Clear the error in insn_error. */
2440
2441static void
2442clear_insn_error (void)
2443{
2444 memset (&insn_error, 0, sizeof (insn_error));
2445}
2446
2447/* Possibly record error message MSG for the current instruction.
2448 If the error is about a particular argument, ARGNUM is the 1-based
2449 number of that argument, otherwise it is 0. FORMAT is the format
2450 of MSG. Return true if MSG was used, false if the current message
2451 was kept. */
2452
2453static bfd_boolean
2454set_insn_error_format (int argnum, enum mips_insn_error_format format,
2455 const char *msg)
2456{
2457 if (argnum == 0)
2458 {
2459 /* Give priority to errors against specific arguments, and to
2460 the first whole-instruction message. */
2461 if (insn_error.msg)
2462 return FALSE;
2463 }
2464 else
2465 {
2466 /* Keep insn_error if it is against a later argument. */
2467 if (argnum < insn_error.min_argnum)
2468 return FALSE;
2469
2470 /* If both errors are against the same argument but are different,
2471 give up on reporting a specific error for this argument.
2472 See the comment about mips_insn_error for details. */
2473 if (argnum == insn_error.min_argnum
2474 && insn_error.msg
2475 && strcmp (insn_error.msg, msg) != 0)
2476 {
2477 insn_error.msg = 0;
2478 insn_error.min_argnum += 1;
2479 return FALSE;
2480 }
2481 }
2482 insn_error.min_argnum = argnum;
2483 insn_error.format = format;
2484 insn_error.msg = msg;
2485 return TRUE;
2486}
2487
2488/* Record an instruction error with no % format fields. ARGNUM and MSG are
2489 as for set_insn_error_format. */
2490
2491static void
2492set_insn_error (int argnum, const char *msg)
2493{
2494 set_insn_error_format (argnum, ERR_FMT_PLAIN, msg);
2495}
2496
2497/* Record an instruction error with one %d field I. ARGNUM and MSG are
2498 as for set_insn_error_format. */
2499
2500static void
2501set_insn_error_i (int argnum, const char *msg, int i)
2502{
2503 if (set_insn_error_format (argnum, ERR_FMT_I, msg))
2504 insn_error.u.i = i;
2505}
2506
2507/* Record an instruction error with two %s fields S1 and S2. ARGNUM and MSG
2508 are as for set_insn_error_format. */
2509
2510static void
2511set_insn_error_ss (int argnum, const char *msg, const char *s1, const char *s2)
2512{
2513 if (set_insn_error_format (argnum, ERR_FMT_SS, msg))
2514 {
2515 insn_error.u.ss[0] = s1;
2516 insn_error.u.ss[1] = s2;
2517 }
2518}
2519
2520/* Report the error in insn_error, which is against assembly code STR. */
2521
2522static void
2523report_insn_error (const char *str)
2524{
e1fa0163 2525 const char *msg = concat (insn_error.msg, " `%s'", NULL);
e3de51ce 2526
e3de51ce
RS
2527 switch (insn_error.format)
2528 {
2529 case ERR_FMT_PLAIN:
2530 as_bad (msg, str);
2531 break;
2532
2533 case ERR_FMT_I:
2534 as_bad (msg, insn_error.u.i, str);
2535 break;
2536
2537 case ERR_FMT_SS:
2538 as_bad (msg, insn_error.u.ss[0], insn_error.u.ss[1], str);
2539 break;
2540 }
e1fa0163
NC
2541
2542 free ((char *) msg);
e3de51ce
RS
2543}
2544
71400594
RS
2545/* Initialize vr4120_conflicts. There is a bit of duplication here:
2546 the idea is to make it obvious at a glance that each errata is
2547 included. */
2548
2549static void
2550init_vr4120_conflicts (void)
2551{
2552#define CONFLICT(FIRST, SECOND) \
2553 vr4120_conflicts[FIX_VR4120_##FIRST] |= 1 << FIX_VR4120_##SECOND
2554
2555 /* Errata 21 - [D]DIV[U] after [D]MACC */
2556 CONFLICT (MACC, DIV);
2557 CONFLICT (DMACC, DIV);
2558
2559 /* Errata 23 - Continuous DMULT[U]/DMACC instructions. */
2560 CONFLICT (DMULT, DMULT);
2561 CONFLICT (DMULT, DMACC);
2562 CONFLICT (DMACC, DMULT);
2563 CONFLICT (DMACC, DMACC);
2564
2565 /* Errata 24 - MT{LO,HI} after [D]MACC */
2566 CONFLICT (MACC, MTHILO);
2567 CONFLICT (DMACC, MTHILO);
2568
2569 /* VR4181A errata MD(1): "If a MULT, MULTU, DMULT or DMULTU
2570 instruction is executed immediately after a MACC or DMACC
2571 instruction, the result of [either instruction] is incorrect." */
2572 CONFLICT (MACC, MULT);
2573 CONFLICT (MACC, DMULT);
2574 CONFLICT (DMACC, MULT);
2575 CONFLICT (DMACC, DMULT);
2576
2577 /* VR4181A errata MD(4): "If a MACC or DMACC instruction is
2578 executed immediately after a DMULT, DMULTU, DIV, DIVU,
2579 DDIV or DDIVU instruction, the result of the MACC or
2580 DMACC instruction is incorrect.". */
2581 CONFLICT (DMULT, MACC);
2582 CONFLICT (DMULT, DMACC);
2583 CONFLICT (DIV, MACC);
2584 CONFLICT (DIV, DMACC);
2585
2586#undef CONFLICT
2587}
2588
707bfff6
TS
2589struct regname {
2590 const char *name;
2591 unsigned int num;
2592};
2593
14daeee3 2594#define RNUM_MASK 0x00000ff
56d438b1 2595#define RTYPE_MASK 0x0ffff00
14daeee3
RS
2596#define RTYPE_NUM 0x0000100
2597#define RTYPE_FPU 0x0000200
2598#define RTYPE_FCC 0x0000400
2599#define RTYPE_VEC 0x0000800
2600#define RTYPE_GP 0x0001000
2601#define RTYPE_CP0 0x0002000
2602#define RTYPE_PC 0x0004000
2603#define RTYPE_ACC 0x0008000
2604#define RTYPE_CCC 0x0010000
2605#define RTYPE_VI 0x0020000
2606#define RTYPE_VF 0x0040000
2607#define RTYPE_R5900_I 0x0080000
2608#define RTYPE_R5900_Q 0x0100000
2609#define RTYPE_R5900_R 0x0200000
2610#define RTYPE_R5900_ACC 0x0400000
56d438b1 2611#define RTYPE_MSA 0x0800000
14daeee3 2612#define RWARN 0x8000000
707bfff6
TS
2613
2614#define GENERIC_REGISTER_NUMBERS \
2615 {"$0", RTYPE_NUM | 0}, \
2616 {"$1", RTYPE_NUM | 1}, \
2617 {"$2", RTYPE_NUM | 2}, \
2618 {"$3", RTYPE_NUM | 3}, \
2619 {"$4", RTYPE_NUM | 4}, \
2620 {"$5", RTYPE_NUM | 5}, \
2621 {"$6", RTYPE_NUM | 6}, \
2622 {"$7", RTYPE_NUM | 7}, \
2623 {"$8", RTYPE_NUM | 8}, \
2624 {"$9", RTYPE_NUM | 9}, \
2625 {"$10", RTYPE_NUM | 10}, \
2626 {"$11", RTYPE_NUM | 11}, \
2627 {"$12", RTYPE_NUM | 12}, \
2628 {"$13", RTYPE_NUM | 13}, \
2629 {"$14", RTYPE_NUM | 14}, \
2630 {"$15", RTYPE_NUM | 15}, \
2631 {"$16", RTYPE_NUM | 16}, \
2632 {"$17", RTYPE_NUM | 17}, \
2633 {"$18", RTYPE_NUM | 18}, \
2634 {"$19", RTYPE_NUM | 19}, \
2635 {"$20", RTYPE_NUM | 20}, \
2636 {"$21", RTYPE_NUM | 21}, \
2637 {"$22", RTYPE_NUM | 22}, \
2638 {"$23", RTYPE_NUM | 23}, \
2639 {"$24", RTYPE_NUM | 24}, \
2640 {"$25", RTYPE_NUM | 25}, \
2641 {"$26", RTYPE_NUM | 26}, \
2642 {"$27", RTYPE_NUM | 27}, \
2643 {"$28", RTYPE_NUM | 28}, \
2644 {"$29", RTYPE_NUM | 29}, \
2645 {"$30", RTYPE_NUM | 30}, \
3739860c 2646 {"$31", RTYPE_NUM | 31}
707bfff6
TS
2647
2648#define FPU_REGISTER_NAMES \
2649 {"$f0", RTYPE_FPU | 0}, \
2650 {"$f1", RTYPE_FPU | 1}, \
2651 {"$f2", RTYPE_FPU | 2}, \
2652 {"$f3", RTYPE_FPU | 3}, \
2653 {"$f4", RTYPE_FPU | 4}, \
2654 {"$f5", RTYPE_FPU | 5}, \
2655 {"$f6", RTYPE_FPU | 6}, \
2656 {"$f7", RTYPE_FPU | 7}, \
2657 {"$f8", RTYPE_FPU | 8}, \
2658 {"$f9", RTYPE_FPU | 9}, \
2659 {"$f10", RTYPE_FPU | 10}, \
2660 {"$f11", RTYPE_FPU | 11}, \
2661 {"$f12", RTYPE_FPU | 12}, \
2662 {"$f13", RTYPE_FPU | 13}, \
2663 {"$f14", RTYPE_FPU | 14}, \
2664 {"$f15", RTYPE_FPU | 15}, \
2665 {"$f16", RTYPE_FPU | 16}, \
2666 {"$f17", RTYPE_FPU | 17}, \
2667 {"$f18", RTYPE_FPU | 18}, \
2668 {"$f19", RTYPE_FPU | 19}, \
2669 {"$f20", RTYPE_FPU | 20}, \
2670 {"$f21", RTYPE_FPU | 21}, \
2671 {"$f22", RTYPE_FPU | 22}, \
2672 {"$f23", RTYPE_FPU | 23}, \
2673 {"$f24", RTYPE_FPU | 24}, \
2674 {"$f25", RTYPE_FPU | 25}, \
2675 {"$f26", RTYPE_FPU | 26}, \
2676 {"$f27", RTYPE_FPU | 27}, \
2677 {"$f28", RTYPE_FPU | 28}, \
2678 {"$f29", RTYPE_FPU | 29}, \
2679 {"$f30", RTYPE_FPU | 30}, \
2680 {"$f31", RTYPE_FPU | 31}
2681
2682#define FPU_CONDITION_CODE_NAMES \
2683 {"$fcc0", RTYPE_FCC | 0}, \
2684 {"$fcc1", RTYPE_FCC | 1}, \
2685 {"$fcc2", RTYPE_FCC | 2}, \
2686 {"$fcc3", RTYPE_FCC | 3}, \
2687 {"$fcc4", RTYPE_FCC | 4}, \
2688 {"$fcc5", RTYPE_FCC | 5}, \
2689 {"$fcc6", RTYPE_FCC | 6}, \
2690 {"$fcc7", RTYPE_FCC | 7}
2691
2692#define COPROC_CONDITION_CODE_NAMES \
2693 {"$cc0", RTYPE_FCC | RTYPE_CCC | 0}, \
2694 {"$cc1", RTYPE_FCC | RTYPE_CCC | 1}, \
2695 {"$cc2", RTYPE_FCC | RTYPE_CCC | 2}, \
2696 {"$cc3", RTYPE_FCC | RTYPE_CCC | 3}, \
2697 {"$cc4", RTYPE_FCC | RTYPE_CCC | 4}, \
2698 {"$cc5", RTYPE_FCC | RTYPE_CCC | 5}, \
2699 {"$cc6", RTYPE_FCC | RTYPE_CCC | 6}, \
2700 {"$cc7", RTYPE_FCC | RTYPE_CCC | 7}
2701
2702#define N32N64_SYMBOLIC_REGISTER_NAMES \
2703 {"$a4", RTYPE_GP | 8}, \
2704 {"$a5", RTYPE_GP | 9}, \
2705 {"$a6", RTYPE_GP | 10}, \
2706 {"$a7", RTYPE_GP | 11}, \
2707 {"$ta0", RTYPE_GP | 8}, /* alias for $a4 */ \
2708 {"$ta1", RTYPE_GP | 9}, /* alias for $a5 */ \
2709 {"$ta2", RTYPE_GP | 10}, /* alias for $a6 */ \
2710 {"$ta3", RTYPE_GP | 11}, /* alias for $a7 */ \
2711 {"$t0", RTYPE_GP | 12}, \
2712 {"$t1", RTYPE_GP | 13}, \
2713 {"$t2", RTYPE_GP | 14}, \
2714 {"$t3", RTYPE_GP | 15}
2715
2716#define O32_SYMBOLIC_REGISTER_NAMES \
2717 {"$t0", RTYPE_GP | 8}, \
2718 {"$t1", RTYPE_GP | 9}, \
2719 {"$t2", RTYPE_GP | 10}, \
2720 {"$t3", RTYPE_GP | 11}, \
2721 {"$t4", RTYPE_GP | 12}, \
2722 {"$t5", RTYPE_GP | 13}, \
2723 {"$t6", RTYPE_GP | 14}, \
2724 {"$t7", RTYPE_GP | 15}, \
2725 {"$ta0", RTYPE_GP | 12}, /* alias for $t4 */ \
2726 {"$ta1", RTYPE_GP | 13}, /* alias for $t5 */ \
2727 {"$ta2", RTYPE_GP | 14}, /* alias for $t6 */ \
3739860c 2728 {"$ta3", RTYPE_GP | 15} /* alias for $t7 */
707bfff6
TS
2729
2730/* Remaining symbolic register names */
2731#define SYMBOLIC_REGISTER_NAMES \
2732 {"$zero", RTYPE_GP | 0}, \
2733 {"$at", RTYPE_GP | 1}, \
2734 {"$AT", RTYPE_GP | 1}, \
2735 {"$v0", RTYPE_GP | 2}, \
2736 {"$v1", RTYPE_GP | 3}, \
2737 {"$a0", RTYPE_GP | 4}, \
2738 {"$a1", RTYPE_GP | 5}, \
2739 {"$a2", RTYPE_GP | 6}, \
2740 {"$a3", RTYPE_GP | 7}, \
2741 {"$s0", RTYPE_GP | 16}, \
2742 {"$s1", RTYPE_GP | 17}, \
2743 {"$s2", RTYPE_GP | 18}, \
2744 {"$s3", RTYPE_GP | 19}, \
2745 {"$s4", RTYPE_GP | 20}, \
2746 {"$s5", RTYPE_GP | 21}, \
2747 {"$s6", RTYPE_GP | 22}, \
2748 {"$s7", RTYPE_GP | 23}, \
2749 {"$t8", RTYPE_GP | 24}, \
2750 {"$t9", RTYPE_GP | 25}, \
2751 {"$k0", RTYPE_GP | 26}, \
2752 {"$kt0", RTYPE_GP | 26}, \
2753 {"$k1", RTYPE_GP | 27}, \
2754 {"$kt1", RTYPE_GP | 27}, \
2755 {"$gp", RTYPE_GP | 28}, \
2756 {"$sp", RTYPE_GP | 29}, \
2757 {"$s8", RTYPE_GP | 30}, \
2758 {"$fp", RTYPE_GP | 30}, \
2759 {"$ra", RTYPE_GP | 31}
2760
2761#define MIPS16_SPECIAL_REGISTER_NAMES \
2762 {"$pc", RTYPE_PC | 0}
2763
2764#define MDMX_VECTOR_REGISTER_NAMES \
2765 /* {"$v0", RTYPE_VEC | 0}, clash with REG 2 above */ \
2766 /* {"$v1", RTYPE_VEC | 1}, clash with REG 3 above */ \
2767 {"$v2", RTYPE_VEC | 2}, \
2768 {"$v3", RTYPE_VEC | 3}, \
2769 {"$v4", RTYPE_VEC | 4}, \
2770 {"$v5", RTYPE_VEC | 5}, \
2771 {"$v6", RTYPE_VEC | 6}, \
2772 {"$v7", RTYPE_VEC | 7}, \
2773 {"$v8", RTYPE_VEC | 8}, \
2774 {"$v9", RTYPE_VEC | 9}, \
2775 {"$v10", RTYPE_VEC | 10}, \
2776 {"$v11", RTYPE_VEC | 11}, \
2777 {"$v12", RTYPE_VEC | 12}, \
2778 {"$v13", RTYPE_VEC | 13}, \
2779 {"$v14", RTYPE_VEC | 14}, \
2780 {"$v15", RTYPE_VEC | 15}, \
2781 {"$v16", RTYPE_VEC | 16}, \
2782 {"$v17", RTYPE_VEC | 17}, \
2783 {"$v18", RTYPE_VEC | 18}, \
2784 {"$v19", RTYPE_VEC | 19}, \
2785 {"$v20", RTYPE_VEC | 20}, \
2786 {"$v21", RTYPE_VEC | 21}, \
2787 {"$v22", RTYPE_VEC | 22}, \
2788 {"$v23", RTYPE_VEC | 23}, \
2789 {"$v24", RTYPE_VEC | 24}, \
2790 {"$v25", RTYPE_VEC | 25}, \
2791 {"$v26", RTYPE_VEC | 26}, \
2792 {"$v27", RTYPE_VEC | 27}, \
2793 {"$v28", RTYPE_VEC | 28}, \
2794 {"$v29", RTYPE_VEC | 29}, \
2795 {"$v30", RTYPE_VEC | 30}, \
2796 {"$v31", RTYPE_VEC | 31}
2797
14daeee3
RS
2798#define R5900_I_NAMES \
2799 {"$I", RTYPE_R5900_I | 0}
2800
2801#define R5900_Q_NAMES \
2802 {"$Q", RTYPE_R5900_Q | 0}
2803
2804#define R5900_R_NAMES \
2805 {"$R", RTYPE_R5900_R | 0}
2806
2807#define R5900_ACC_NAMES \
2808 {"$ACC", RTYPE_R5900_ACC | 0 }
2809
707bfff6
TS
2810#define MIPS_DSP_ACCUMULATOR_NAMES \
2811 {"$ac0", RTYPE_ACC | 0}, \
2812 {"$ac1", RTYPE_ACC | 1}, \
2813 {"$ac2", RTYPE_ACC | 2}, \
2814 {"$ac3", RTYPE_ACC | 3}
2815
2816static const struct regname reg_names[] = {
2817 GENERIC_REGISTER_NUMBERS,
2818 FPU_REGISTER_NAMES,
2819 FPU_CONDITION_CODE_NAMES,
2820 COPROC_CONDITION_CODE_NAMES,
2821
2822 /* The $txx registers depends on the abi,
2823 these will be added later into the symbol table from
3739860c 2824 one of the tables below once mips_abi is set after
707bfff6
TS
2825 parsing of arguments from the command line. */
2826 SYMBOLIC_REGISTER_NAMES,
2827
2828 MIPS16_SPECIAL_REGISTER_NAMES,
2829 MDMX_VECTOR_REGISTER_NAMES,
14daeee3
RS
2830 R5900_I_NAMES,
2831 R5900_Q_NAMES,
2832 R5900_R_NAMES,
2833 R5900_ACC_NAMES,
707bfff6
TS
2834 MIPS_DSP_ACCUMULATOR_NAMES,
2835 {0, 0}
2836};
2837
2838static const struct regname reg_names_o32[] = {
2839 O32_SYMBOLIC_REGISTER_NAMES,
2840 {0, 0}
2841};
2842
2843static const struct regname reg_names_n32n64[] = {
2844 N32N64_SYMBOLIC_REGISTER_NAMES,
2845 {0, 0}
2846};
2847
a92713e6
RS
2848/* Register symbols $v0 and $v1 map to GPRs 2 and 3, but they can also be
2849 interpreted as vector registers 0 and 1. If SYMVAL is the value of one
2850 of these register symbols, return the associated vector register,
2851 otherwise return SYMVAL itself. */
df58fc94 2852
a92713e6
RS
2853static unsigned int
2854mips_prefer_vec_regno (unsigned int symval)
707bfff6 2855{
a92713e6
RS
2856 if ((symval & -2) == (RTYPE_GP | 2))
2857 return RTYPE_VEC | (symval & 1);
2858 return symval;
2859}
2860
14daeee3
RS
2861/* Return true if string [S, E) is a valid register name, storing its
2862 symbol value in *SYMVAL_PTR if so. */
a92713e6
RS
2863
2864static bfd_boolean
14daeee3 2865mips_parse_register_1 (char *s, char *e, unsigned int *symval_ptr)
a92713e6 2866{
707bfff6 2867 char save_c;
14daeee3 2868 symbolS *symbol;
707bfff6
TS
2869
2870 /* Terminate name. */
2871 save_c = *e;
2872 *e = '\0';
2873
a92713e6
RS
2874 /* Look up the name. */
2875 symbol = symbol_find (s);
2876 *e = save_c;
2877
2878 if (!symbol || S_GET_SEGMENT (symbol) != reg_section)
2879 return FALSE;
2880
14daeee3
RS
2881 *symval_ptr = S_GET_VALUE (symbol);
2882 return TRUE;
2883}
2884
2885/* Return true if the string at *SPTR is a valid register name. Allow it
2886 to have a VU0-style channel suffix of the form x?y?z?w? if CHANNELS_PTR
2887 is nonnull.
2888
2889 When returning true, move *SPTR past the register, store the
2890 register's symbol value in *SYMVAL_PTR and the channel mask in
2891 *CHANNELS_PTR (if nonnull). The symbol value includes the register
2892 number (RNUM_MASK) and register type (RTYPE_MASK). The channel mask
2893 is a 4-bit value of the form XYZW and is 0 if no suffix was given. */
2894
2895static bfd_boolean
2896mips_parse_register (char **sptr, unsigned int *symval_ptr,
2897 unsigned int *channels_ptr)
2898{
2899 char *s, *e, *m;
2900 const char *q;
2901 unsigned int channels, symval, bit;
2902
2903 /* Find end of name. */
2904 s = e = *sptr;
2905 if (is_name_beginner (*e))
2906 ++e;
2907 while (is_part_of_name (*e))
2908 ++e;
2909
2910 channels = 0;
2911 if (!mips_parse_register_1 (s, e, &symval))
2912 {
2913 if (!channels_ptr)
2914 return FALSE;
2915
2916 /* Eat characters from the end of the string that are valid
2917 channel suffixes. The preceding register must be $ACC or
2918 end with a digit, so there is no ambiguity. */
2919 bit = 1;
2920 m = e;
2921 for (q = "wzyx"; *q; q++, bit <<= 1)
2922 if (m > s && m[-1] == *q)
2923 {
2924 --m;
2925 channels |= bit;
2926 }
2927
2928 if (channels == 0
2929 || !mips_parse_register_1 (s, m, &symval)
2930 || (symval & (RTYPE_VI | RTYPE_VF | RTYPE_R5900_ACC)) == 0)
2931 return FALSE;
2932 }
2933
a92713e6 2934 *sptr = e;
14daeee3
RS
2935 *symval_ptr = symval;
2936 if (channels_ptr)
2937 *channels_ptr = channels;
a92713e6
RS
2938 return TRUE;
2939}
2940
2941/* Check if SPTR points at a valid register specifier according to TYPES.
2942 If so, then return 1, advance S to consume the specifier and store
2943 the register's number in REGNOP, otherwise return 0. */
2944
2945static int
2946reg_lookup (char **s, unsigned int types, unsigned int *regnop)
2947{
2948 unsigned int regno;
2949
14daeee3 2950 if (mips_parse_register (s, &regno, NULL))
707bfff6 2951 {
a92713e6
RS
2952 if (types & RTYPE_VEC)
2953 regno = mips_prefer_vec_regno (regno);
2954 if (regno & types)
2955 regno &= RNUM_MASK;
2956 else
2957 regno = ~0;
707bfff6 2958 }
a92713e6 2959 else
707bfff6 2960 {
a92713e6 2961 if (types & RWARN)
1661c76c 2962 as_warn (_("unrecognized register name `%s'"), *s);
a92713e6 2963 regno = ~0;
707bfff6 2964 }
707bfff6 2965 if (regnop)
a92713e6
RS
2966 *regnop = regno;
2967 return regno <= RNUM_MASK;
707bfff6
TS
2968}
2969
14daeee3
RS
2970/* Parse a VU0 "x?y?z?w?" channel mask at S and store the associated
2971 mask in *CHANNELS. Return a pointer to the first unconsumed character. */
2972
2973static char *
2974mips_parse_vu0_channels (char *s, unsigned int *channels)
2975{
2976 unsigned int i;
2977
2978 *channels = 0;
2979 for (i = 0; i < 4; i++)
2980 if (*s == "xyzw"[i])
2981 {
2982 *channels |= 1 << (3 - i);
2983 ++s;
2984 }
2985 return s;
2986}
2987
a92713e6
RS
2988/* Token types for parsed operand lists. */
2989enum mips_operand_token_type {
2990 /* A plain register, e.g. $f2. */
2991 OT_REG,
df58fc94 2992
14daeee3
RS
2993 /* A 4-bit XYZW channel mask. */
2994 OT_CHANNELS,
2995
56d438b1
CF
2996 /* A constant vector index, e.g. [1]. */
2997 OT_INTEGER_INDEX,
2998
2999 /* A register vector index, e.g. [$2]. */
3000 OT_REG_INDEX,
df58fc94 3001
a92713e6
RS
3002 /* A continuous range of registers, e.g. $s0-$s4. */
3003 OT_REG_RANGE,
3004
3005 /* A (possibly relocated) expression. */
3006 OT_INTEGER,
3007
3008 /* A floating-point value. */
3009 OT_FLOAT,
3010
3011 /* A single character. This can be '(', ')' or ',', but '(' only appears
3012 before OT_REGs. */
3013 OT_CHAR,
3014
14daeee3
RS
3015 /* A doubled character, either "--" or "++". */
3016 OT_DOUBLE_CHAR,
3017
a92713e6
RS
3018 /* The end of the operand list. */
3019 OT_END
3020};
3021
3022/* A parsed operand token. */
3023struct mips_operand_token
3024{
3025 /* The type of token. */
3026 enum mips_operand_token_type type;
3027 union
3028 {
56d438b1 3029 /* The register symbol value for an OT_REG or OT_REG_INDEX. */
a92713e6
RS
3030 unsigned int regno;
3031
14daeee3
RS
3032 /* The 4-bit channel mask for an OT_CHANNEL_SUFFIX. */
3033 unsigned int channels;
3034
56d438b1
CF
3035 /* The integer value of an OT_INTEGER_INDEX. */
3036 addressT index;
a92713e6
RS
3037
3038 /* The two register symbol values involved in an OT_REG_RANGE. */
3039 struct {
3040 unsigned int regno1;
3041 unsigned int regno2;
3042 } reg_range;
3043
3044 /* The value of an OT_INTEGER. The value is represented as an
3045 expression and the relocation operators that were applied to
3046 that expression. The reloc entries are BFD_RELOC_UNUSED if no
3047 relocation operators were used. */
3048 struct {
3049 expressionS value;
3050 bfd_reloc_code_real_type relocs[3];
3051 } integer;
3052
3053 /* The binary data for an OT_FLOAT constant, and the number of bytes
3054 in the constant. */
3055 struct {
3056 unsigned char data[8];
3057 int length;
3058 } flt;
3059
14daeee3 3060 /* The character represented by an OT_CHAR or OT_DOUBLE_CHAR. */
a92713e6
RS
3061 char ch;
3062 } u;
3063};
3064
3065/* An obstack used to construct lists of mips_operand_tokens. */
3066static struct obstack mips_operand_tokens;
3067
3068/* Give TOKEN type TYPE and add it to mips_operand_tokens. */
3069
3070static void
3071mips_add_token (struct mips_operand_token *token,
3072 enum mips_operand_token_type type)
3073{
3074 token->type = type;
3075 obstack_grow (&mips_operand_tokens, token, sizeof (*token));
3076}
3077
3078/* Check whether S is '(' followed by a register name. Add OT_CHAR
3079 and OT_REG tokens for them if so, and return a pointer to the first
3080 unconsumed character. Return null otherwise. */
3081
3082static char *
3083mips_parse_base_start (char *s)
3084{
3085 struct mips_operand_token token;
14daeee3
RS
3086 unsigned int regno, channels;
3087 bfd_boolean decrement_p;
df58fc94 3088
a92713e6
RS
3089 if (*s != '(')
3090 return 0;
3091
3092 ++s;
3093 SKIP_SPACE_TABS (s);
14daeee3
RS
3094
3095 /* Only match "--" as part of a base expression. In other contexts "--X"
3096 is a double negative. */
3097 decrement_p = (s[0] == '-' && s[1] == '-');
3098 if (decrement_p)
3099 {
3100 s += 2;
3101 SKIP_SPACE_TABS (s);
3102 }
3103
3104 /* Allow a channel specifier because that leads to better error messages
3105 than treating something like "$vf0x++" as an expression. */
3106 if (!mips_parse_register (&s, &regno, &channels))
a92713e6
RS
3107 return 0;
3108
3109 token.u.ch = '(';
3110 mips_add_token (&token, OT_CHAR);
3111
14daeee3
RS
3112 if (decrement_p)
3113 {
3114 token.u.ch = '-';
3115 mips_add_token (&token, OT_DOUBLE_CHAR);
3116 }
3117
a92713e6
RS
3118 token.u.regno = regno;
3119 mips_add_token (&token, OT_REG);
3120
14daeee3
RS
3121 if (channels)
3122 {
3123 token.u.channels = channels;
3124 mips_add_token (&token, OT_CHANNELS);
3125 }
3126
3127 /* For consistency, only match "++" as part of base expressions too. */
3128 SKIP_SPACE_TABS (s);
3129 if (s[0] == '+' && s[1] == '+')
3130 {
3131 s += 2;
3132 token.u.ch = '+';
3133 mips_add_token (&token, OT_DOUBLE_CHAR);
3134 }
3135
a92713e6
RS
3136 return s;
3137}
3138
3139/* Parse one or more tokens from S. Return a pointer to the first
3140 unconsumed character on success. Return null if an error was found
3141 and store the error text in insn_error. FLOAT_FORMAT is as for
3142 mips_parse_arguments. */
3143
3144static char *
3145mips_parse_argument_token (char *s, char float_format)
3146{
6d4af3c2
AM
3147 char *end, *save_in;
3148 const char *err;
14daeee3 3149 unsigned int regno1, regno2, channels;
a92713e6
RS
3150 struct mips_operand_token token;
3151
3152 /* First look for "($reg", since we want to treat that as an
3153 OT_CHAR and OT_REG rather than an expression. */
3154 end = mips_parse_base_start (s);
3155 if (end)
3156 return end;
3157
3158 /* Handle other characters that end up as OT_CHARs. */
3159 if (*s == ')' || *s == ',')
3160 {
3161 token.u.ch = *s;
3162 mips_add_token (&token, OT_CHAR);
3163 ++s;
3164 return s;
3165 }
3166
3167 /* Handle tokens that start with a register. */
14daeee3 3168 if (mips_parse_register (&s, &regno1, &channels))
df58fc94 3169 {
14daeee3
RS
3170 if (channels)
3171 {
3172 /* A register and a VU0 channel suffix. */
3173 token.u.regno = regno1;
3174 mips_add_token (&token, OT_REG);
3175
3176 token.u.channels = channels;
3177 mips_add_token (&token, OT_CHANNELS);
3178 return s;
3179 }
3180
a92713e6
RS
3181 SKIP_SPACE_TABS (s);
3182 if (*s == '-')
df58fc94 3183 {
a92713e6
RS
3184 /* A register range. */
3185 ++s;
3186 SKIP_SPACE_TABS (s);
14daeee3 3187 if (!mips_parse_register (&s, &regno2, NULL))
a92713e6 3188 {
1661c76c 3189 set_insn_error (0, _("invalid register range"));
a92713e6
RS
3190 return 0;
3191 }
df58fc94 3192
a92713e6
RS
3193 token.u.reg_range.regno1 = regno1;
3194 token.u.reg_range.regno2 = regno2;
3195 mips_add_token (&token, OT_REG_RANGE);
3196 return s;
3197 }
a92713e6 3198
56d438b1
CF
3199 /* Add the register itself. */
3200 token.u.regno = regno1;
3201 mips_add_token (&token, OT_REG);
3202
3203 /* Check for a vector index. */
3204 if (*s == '[')
3205 {
a92713e6
RS
3206 ++s;
3207 SKIP_SPACE_TABS (s);
56d438b1
CF
3208 if (mips_parse_register (&s, &token.u.regno, NULL))
3209 mips_add_token (&token, OT_REG_INDEX);
3210 else
a92713e6 3211 {
56d438b1
CF
3212 expressionS element;
3213
3214 my_getExpression (&element, s);
3215 if (element.X_op != O_constant)
3216 {
3217 set_insn_error (0, _("vector element must be constant"));
3218 return 0;
3219 }
3220 s = expr_end;
3221 token.u.index = element.X_add_number;
3222 mips_add_token (&token, OT_INTEGER_INDEX);
a92713e6 3223 }
a92713e6
RS
3224 SKIP_SPACE_TABS (s);
3225 if (*s != ']')
3226 {
1661c76c 3227 set_insn_error (0, _("missing `]'"));
a92713e6
RS
3228 return 0;
3229 }
3230 ++s;
df58fc94 3231 }
a92713e6 3232 return s;
df58fc94
RS
3233 }
3234
a92713e6
RS
3235 if (float_format)
3236 {
3237 /* First try to treat expressions as floats. */
3238 save_in = input_line_pointer;
3239 input_line_pointer = s;
3240 err = md_atof (float_format, (char *) token.u.flt.data,
3241 &token.u.flt.length);
3242 end = input_line_pointer;
3243 input_line_pointer = save_in;
3244 if (err && *err)
3245 {
e3de51ce 3246 set_insn_error (0, err);
a92713e6
RS
3247 return 0;
3248 }
3249 if (s != end)
3250 {
3251 mips_add_token (&token, OT_FLOAT);
3252 return end;
3253 }
3254 }
3255
3256 /* Treat everything else as an integer expression. */
3257 token.u.integer.relocs[0] = BFD_RELOC_UNUSED;
3258 token.u.integer.relocs[1] = BFD_RELOC_UNUSED;
3259 token.u.integer.relocs[2] = BFD_RELOC_UNUSED;
3260 my_getSmallExpression (&token.u.integer.value, token.u.integer.relocs, s);
3261 s = expr_end;
3262 mips_add_token (&token, OT_INTEGER);
3263 return s;
3264}
3265
3266/* S points to the operand list for an instruction. FLOAT_FORMAT is 'f'
3267 if expressions should be treated as 32-bit floating-point constants,
3268 'd' if they should be treated as 64-bit floating-point constants,
3269 or 0 if they should be treated as integer expressions (the usual case).
3270
3271 Return a list of tokens on success, otherwise return 0. The caller
3272 must obstack_free the list after use. */
3273
3274static struct mips_operand_token *
3275mips_parse_arguments (char *s, char float_format)
3276{
3277 struct mips_operand_token token;
3278
3279 SKIP_SPACE_TABS (s);
3280 while (*s)
3281 {
3282 s = mips_parse_argument_token (s, float_format);
3283 if (!s)
3284 {
3285 obstack_free (&mips_operand_tokens,
3286 obstack_finish (&mips_operand_tokens));
3287 return 0;
3288 }
3289 SKIP_SPACE_TABS (s);
3290 }
3291 mips_add_token (&token, OT_END);
3292 return (struct mips_operand_token *) obstack_finish (&mips_operand_tokens);
df58fc94
RS
3293}
3294
d301a56b
RS
3295/* Return TRUE if opcode MO is valid on the currently selected ISA, ASE
3296 and architecture. Use is_opcode_valid_16 for MIPS16 opcodes. */
037b32b9
AN
3297
3298static bfd_boolean
f79e2745 3299is_opcode_valid (const struct mips_opcode *mo)
037b32b9
AN
3300{
3301 int isa = mips_opts.isa;
846ef2d0 3302 int ase = mips_opts.ase;
037b32b9 3303 int fp_s, fp_d;
c6278170 3304 unsigned int i;
037b32b9 3305
be0fcbee 3306 if (ISA_HAS_64BIT_REGS (isa))
c6278170
RS
3307 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3308 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3309 ase |= mips_ases[i].flags64;
037b32b9 3310
d301a56b 3311 if (!opcode_is_member (mo, isa, ase, mips_opts.arch))
037b32b9
AN
3312 return FALSE;
3313
3314 /* Check whether the instruction or macro requires single-precision or
3315 double-precision floating-point support. Note that this information is
3316 stored differently in the opcode table for insns and macros. */
3317 if (mo->pinfo == INSN_MACRO)
3318 {
3319 fp_s = mo->pinfo2 & INSN2_M_FP_S;
3320 fp_d = mo->pinfo2 & INSN2_M_FP_D;
3321 }
3322 else
3323 {
3324 fp_s = mo->pinfo & FP_S;
3325 fp_d = mo->pinfo & FP_D;
3326 }
3327
3328 if (fp_d && (mips_opts.soft_float || mips_opts.single_float))
3329 return FALSE;
3330
3331 if (fp_s && mips_opts.soft_float)
3332 return FALSE;
3333
3334 return TRUE;
3335}
3336
3337/* Return TRUE if the MIPS16 opcode MO is valid on the currently
3338 selected ISA and architecture. */
3339
3340static bfd_boolean
3341is_opcode_valid_16 (const struct mips_opcode *mo)
3342{
25499ac7
MR
3343 int isa = mips_opts.isa;
3344 int ase = mips_opts.ase;
3345 unsigned int i;
3346
3347 if (ISA_HAS_64BIT_REGS (isa))
3348 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
3349 if ((ase & mips_ases[i].flags) == mips_ases[i].flags)
3350 ase |= mips_ases[i].flags64;
3351
3352 return opcode_is_member (mo, isa, ase, mips_opts.arch);
037b32b9
AN
3353}
3354
df58fc94 3355/* Return TRUE if the size of the microMIPS opcode MO matches one
7fd53920
MR
3356 explicitly requested. Always TRUE in the standard MIPS mode.
3357 Use is_size_valid_16 for MIPS16 opcodes. */
df58fc94
RS
3358
3359static bfd_boolean
3360is_size_valid (const struct mips_opcode *mo)
3361{
3362 if (!mips_opts.micromips)
3363 return TRUE;
3364
833794fc
MR
3365 if (mips_opts.insn32)
3366 {
3367 if (mo->pinfo != INSN_MACRO && micromips_insn_length (mo) != 4)
3368 return FALSE;
3369 if ((mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0)
3370 return FALSE;
3371 }
df58fc94
RS
3372 if (!forced_insn_length)
3373 return TRUE;
3374 if (mo->pinfo == INSN_MACRO)
3375 return FALSE;
3376 return forced_insn_length == micromips_insn_length (mo);
3377}
3378
7fd53920
MR
3379/* Return TRUE if the size of the MIPS16 opcode MO matches one
3380 explicitly requested. */
3381
3382static bfd_boolean
3383is_size_valid_16 (const struct mips_opcode *mo)
3384{
3385 if (!forced_insn_length)
3386 return TRUE;
3387 if (mo->pinfo == INSN_MACRO)
3388 return FALSE;
3389 if (forced_insn_length == 2 && mips_opcode_32bit_p (mo))
3390 return FALSE;
0674ee5d
MR
3391 if (forced_insn_length == 4 && (mo->pinfo2 & INSN2_SHORT_ONLY))
3392 return FALSE;
7fd53920
MR
3393 return TRUE;
3394}
3395
df58fc94 3396/* Return TRUE if the microMIPS opcode MO is valid for the delay slot
e64af278
MR
3397 of the preceding instruction. Always TRUE in the standard MIPS mode.
3398
3399 We don't accept macros in 16-bit delay slots to avoid a case where
3400 a macro expansion fails because it relies on a preceding 32-bit real
3401 instruction to have matched and does not handle the operands correctly.
3402 The only macros that may expand to 16-bit instructions are JAL that
3403 cannot be placed in a delay slot anyway, and corner cases of BALIGN
3404 and BGT (that likewise cannot be placed in a delay slot) that decay to
3405 a NOP. In all these cases the macros precede any corresponding real
3406 instruction definitions in the opcode table, so they will match in the
3407 second pass where the size of the delay slot is ignored and therefore
3408 produce correct code. */
df58fc94
RS
3409
3410static bfd_boolean
3411is_delay_slot_valid (const struct mips_opcode *mo)
3412{
3413 if (!mips_opts.micromips)
3414 return TRUE;
3415
3416 if (mo->pinfo == INSN_MACRO)
c06dec14 3417 return (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) == 0;
df58fc94
RS
3418 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
3419 && micromips_insn_length (mo) != 4)
3420 return FALSE;
3421 if ((history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
3422 && micromips_insn_length (mo) != 2)
3423 return FALSE;
3424
3425 return TRUE;
3426}
3427
fc76e730
RS
3428/* For consistency checking, verify that all bits of OPCODE are specified
3429 either by the match/mask part of the instruction definition, or by the
3430 operand list. Also build up a list of operands in OPERANDS.
3431
3432 INSN_BITS says which bits of the instruction are significant.
3433 If OPCODE is a standard or microMIPS instruction, DECODE_OPERAND
3434 provides the mips_operand description of each operand. DECODE_OPERAND
3435 is null for MIPS16 instructions. */
ab902481
RS
3436
3437static int
3438validate_mips_insn (const struct mips_opcode *opcode,
3439 unsigned long insn_bits,
fc76e730
RS
3440 const struct mips_operand *(*decode_operand) (const char *),
3441 struct mips_operand_array *operands)
ab902481
RS
3442{
3443 const char *s;
fc76e730 3444 unsigned long used_bits, doubled, undefined, opno, mask;
ab902481
RS
3445 const struct mips_operand *operand;
3446
fc76e730
RS
3447 mask = (opcode->pinfo == INSN_MACRO ? 0 : opcode->mask);
3448 if ((mask & opcode->match) != opcode->match)
ab902481
RS
3449 {
3450 as_bad (_("internal: bad mips opcode (mask error): %s %s"),
3451 opcode->name, opcode->args);
3452 return 0;
3453 }
3454 used_bits = 0;
fc76e730 3455 opno = 0;
14daeee3
RS
3456 if (opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX)
3457 used_bits = mips_insert_operand (&mips_vu0_channel_mask, used_bits, -1);
ab902481
RS
3458 for (s = opcode->args; *s; ++s)
3459 switch (*s)
3460 {
3461 case ',':
3462 case '(':
3463 case ')':
3464 break;
3465
14daeee3
RS
3466 case '#':
3467 s++;
3468 break;
3469
ab902481 3470 default:
fc76e730 3471 if (!decode_operand)
7fd53920 3472 operand = decode_mips16_operand (*s, mips_opcode_32bit_p (opcode));
fc76e730
RS
3473 else
3474 operand = decode_operand (s);
3475 if (!operand && opcode->pinfo != INSN_MACRO)
ab902481
RS
3476 {
3477 as_bad (_("internal: unknown operand type: %s %s"),
3478 opcode->name, opcode->args);
3479 return 0;
3480 }
fc76e730
RS
3481 gas_assert (opno < MAX_OPERANDS);
3482 operands->operand[opno] = operand;
25499ac7
MR
3483 if (!decode_operand && operand
3484 && operand->type == OP_INT && operand->lsb == 0
3485 && mips_opcode_32bit_p (opcode))
3486 used_bits |= mips16_immed_extend (-1, operand->size);
3487 else if (operand && operand->type != OP_VU0_MATCH_SUFFIX)
fc76e730 3488 {
14daeee3 3489 used_bits = mips_insert_operand (operand, used_bits, -1);
fc76e730
RS
3490 if (operand->type == OP_MDMX_IMM_REG)
3491 /* Bit 5 is the format selector (OB vs QH). The opcode table
3492 has separate entries for each format. */
3493 used_bits &= ~(1 << (operand->lsb + 5));
3494 if (operand->type == OP_ENTRY_EXIT_LIST)
3495 used_bits &= ~(mask & 0x700);
38bf472a
MR
3496 /* interAptiv MR2 SAVE/RESTORE instructions have a discontiguous
3497 operand field that cannot be fully described with LSB/SIZE. */
3498 if (operand->type == OP_SAVE_RESTORE_LIST && operand->lsb == 6)
3499 used_bits &= ~0x6000;
fc76e730 3500 }
ab902481 3501 /* Skip prefix characters. */
7361da2c 3502 if (decode_operand && (*s == '+' || *s == 'm' || *s == '-'))
ab902481 3503 ++s;
fc76e730 3504 opno += 1;
ab902481
RS
3505 break;
3506 }
fc76e730 3507 doubled = used_bits & mask & insn_bits;
ab902481
RS
3508 if (doubled)
3509 {
3510 as_bad (_("internal: bad mips opcode (bits 0x%08lx doubly defined):"
3511 " %s %s"), doubled, opcode->name, opcode->args);
3512 return 0;
3513 }
fc76e730 3514 used_bits |= mask;
ab902481 3515 undefined = ~used_bits & insn_bits;
fc76e730 3516 if (opcode->pinfo != INSN_MACRO && undefined)
ab902481
RS
3517 {
3518 as_bad (_("internal: bad mips opcode (bits 0x%08lx undefined): %s %s"),
3519 undefined, opcode->name, opcode->args);
3520 return 0;
3521 }
3522 used_bits &= ~insn_bits;
3523 if (used_bits)
3524 {
3525 as_bad (_("internal: bad mips opcode (bits 0x%08lx defined): %s %s"),
3526 used_bits, opcode->name, opcode->args);
3527 return 0;
3528 }
3529 return 1;
3530}
3531
fc76e730
RS
3532/* The MIPS16 version of validate_mips_insn. */
3533
3534static int
3535validate_mips16_insn (const struct mips_opcode *opcode,
3536 struct mips_operand_array *operands)
3537{
7fd53920 3538 unsigned long insn_bits = mips_opcode_32bit_p (opcode) ? 0xffffffff : 0xffff;
fc76e730 3539
7fd53920 3540 return validate_mips_insn (opcode, insn_bits, 0, operands);
fc76e730
RS
3541}
3542
ab902481
RS
3543/* The microMIPS version of validate_mips_insn. */
3544
3545static int
fc76e730
RS
3546validate_micromips_insn (const struct mips_opcode *opc,
3547 struct mips_operand_array *operands)
ab902481
RS
3548{
3549 unsigned long insn_bits;
3550 unsigned long major;
3551 unsigned int length;
3552
fc76e730
RS
3553 if (opc->pinfo == INSN_MACRO)
3554 return validate_mips_insn (opc, 0xffffffff, decode_micromips_operand,
3555 operands);
3556
ab902481
RS
3557 length = micromips_insn_length (opc);
3558 if (length != 2 && length != 4)
3559 {
1661c76c 3560 as_bad (_("internal error: bad microMIPS opcode (incorrect length: %u): "
ab902481
RS
3561 "%s %s"), length, opc->name, opc->args);
3562 return 0;
3563 }
3564 major = opc->match >> (10 + 8 * (length - 2));
3565 if ((length == 2 && (major & 7) != 1 && (major & 6) != 2)
3566 || (length == 4 && (major & 7) != 0 && (major & 4) != 4))
3567 {
1661c76c 3568 as_bad (_("internal error: bad microMIPS opcode "
ab902481
RS
3569 "(opcode/length mismatch): %s %s"), opc->name, opc->args);
3570 return 0;
3571 }
3572
3573 /* Shift piecewise to avoid an overflow where unsigned long is 32-bit. */
3574 insn_bits = 1 << 4 * length;
3575 insn_bits <<= 4 * length;
3576 insn_bits -= 1;
fc76e730
RS
3577 return validate_mips_insn (opc, insn_bits, decode_micromips_operand,
3578 operands);
ab902481
RS
3579}
3580
707bfff6
TS
3581/* This function is called once, at assembler startup time. It should set up
3582 all the tables, etc. that the MD part of the assembler will need. */
156c2f8b 3583
252b5132 3584void
17a2f251 3585md_begin (void)
252b5132 3586{
3994f87e 3587 const char *retval = NULL;
156c2f8b 3588 int i = 0;
252b5132 3589 int broken = 0;
1f25f5d3 3590
0a44bf69
RS
3591 if (mips_pic != NO_PIC)
3592 {
3593 if (g_switch_seen && g_switch_value != 0)
3594 as_bad (_("-G may not be used in position-independent code"));
3595 g_switch_value = 0;
3596 }
00acd688
CM
3597 else if (mips_abicalls)
3598 {
3599 if (g_switch_seen && g_switch_value != 0)
3600 as_bad (_("-G may not be used with abicalls"));
3601 g_switch_value = 0;
3602 }
0a44bf69 3603
0b35dfee 3604 if (! bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
1661c76c 3605 as_warn (_("could not set architecture and machine"));
252b5132 3606
252b5132
RH
3607 op_hash = hash_new ();
3608
fc76e730 3609 mips_operands = XCNEWVEC (struct mips_operand_array, NUMOPCODES);
252b5132
RH
3610 for (i = 0; i < NUMOPCODES;)
3611 {
3612 const char *name = mips_opcodes[i].name;
3613
17a2f251 3614 retval = hash_insert (op_hash, name, (void *) &mips_opcodes[i]);
252b5132
RH
3615 if (retval != NULL)
3616 {
3617 fprintf (stderr, _("internal error: can't hash `%s': %s\n"),
3618 mips_opcodes[i].name, retval);
3619 /* Probably a memory allocation problem? Give up now. */
1661c76c 3620 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3621 }
3622 do
3623 {
fc76e730
RS
3624 if (!validate_mips_insn (&mips_opcodes[i], 0xffffffff,
3625 decode_mips_operand, &mips_operands[i]))
3626 broken = 1;
3627 if (nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
252b5132 3628 {
fc76e730
RS
3629 create_insn (&nop_insn, mips_opcodes + i);
3630 if (mips_fix_loongson2f_nop)
3631 nop_insn.insn_opcode = LOONGSON2F_NOP_INSN;
3632 nop_insn.fixed_p = 1;
252b5132
RH
3633 }
3634 ++i;
3635 }
3636 while ((i < NUMOPCODES) && !strcmp (mips_opcodes[i].name, name));
3637 }
3638
3639 mips16_op_hash = hash_new ();
fc76e730
RS
3640 mips16_operands = XCNEWVEC (struct mips_operand_array,
3641 bfd_mips16_num_opcodes);
252b5132
RH
3642
3643 i = 0;
3644 while (i < bfd_mips16_num_opcodes)
3645 {
3646 const char *name = mips16_opcodes[i].name;
3647
17a2f251 3648 retval = hash_insert (mips16_op_hash, name, (void *) &mips16_opcodes[i]);
252b5132
RH
3649 if (retval != NULL)
3650 as_fatal (_("internal: can't hash `%s': %s"),
3651 mips16_opcodes[i].name, retval);
3652 do
3653 {
fc76e730
RS
3654 if (!validate_mips16_insn (&mips16_opcodes[i], &mips16_operands[i]))
3655 broken = 1;
1e915849
RS
3656 if (mips16_nop_insn.insn_mo == NULL && strcmp (name, "nop") == 0)
3657 {
3658 create_insn (&mips16_nop_insn, mips16_opcodes + i);
3659 mips16_nop_insn.fixed_p = 1;
3660 }
252b5132
RH
3661 ++i;
3662 }
3663 while (i < bfd_mips16_num_opcodes
3664 && strcmp (mips16_opcodes[i].name, name) == 0);
3665 }
3666
df58fc94 3667 micromips_op_hash = hash_new ();
fc76e730
RS
3668 micromips_operands = XCNEWVEC (struct mips_operand_array,
3669 bfd_micromips_num_opcodes);
df58fc94
RS
3670
3671 i = 0;
3672 while (i < bfd_micromips_num_opcodes)
3673 {
3674 const char *name = micromips_opcodes[i].name;
3675
3676 retval = hash_insert (micromips_op_hash, name,
3677 (void *) &micromips_opcodes[i]);
3678 if (retval != NULL)
3679 as_fatal (_("internal: can't hash `%s': %s"),
3680 micromips_opcodes[i].name, retval);
3681 do
fc76e730
RS
3682 {
3683 struct mips_cl_insn *micromips_nop_insn;
3684
3685 if (!validate_micromips_insn (&micromips_opcodes[i],
3686 &micromips_operands[i]))
3687 broken = 1;
3688
3689 if (micromips_opcodes[i].pinfo != INSN_MACRO)
3690 {
3691 if (micromips_insn_length (micromips_opcodes + i) == 2)
3692 micromips_nop_insn = &micromips_nop16_insn;
3693 else if (micromips_insn_length (micromips_opcodes + i) == 4)
3694 micromips_nop_insn = &micromips_nop32_insn;
3695 else
3696 continue;
3697
3698 if (micromips_nop_insn->insn_mo == NULL
3699 && strcmp (name, "nop") == 0)
3700 {
3701 create_insn (micromips_nop_insn, micromips_opcodes + i);
3702 micromips_nop_insn->fixed_p = 1;
3703 }
3704 }
3705 }
df58fc94
RS
3706 while (++i < bfd_micromips_num_opcodes
3707 && strcmp (micromips_opcodes[i].name, name) == 0);
3708 }
3709
252b5132 3710 if (broken)
1661c76c 3711 as_fatal (_("broken assembler, no assembly attempted"));
252b5132
RH
3712
3713 /* We add all the general register names to the symbol table. This
3714 helps us detect invalid uses of them. */
3739860c 3715 for (i = 0; reg_names[i].name; i++)
707bfff6 3716 symbol_table_insert (symbol_new (reg_names[i].name, reg_section,
8fc4ee9b 3717 reg_names[i].num, /* & RNUM_MASK, */
707bfff6
TS
3718 &zero_address_frag));
3719 if (HAVE_NEWABI)
3739860c 3720 for (i = 0; reg_names_n32n64[i].name; i++)
707bfff6 3721 symbol_table_insert (symbol_new (reg_names_n32n64[i].name, reg_section,
8fc4ee9b 3722 reg_names_n32n64[i].num, /* & RNUM_MASK, */
252b5132 3723 &zero_address_frag));
707bfff6 3724 else
3739860c 3725 for (i = 0; reg_names_o32[i].name; i++)
707bfff6 3726 symbol_table_insert (symbol_new (reg_names_o32[i].name, reg_section,
8fc4ee9b 3727 reg_names_o32[i].num, /* & RNUM_MASK, */
6047c971 3728 &zero_address_frag));
6047c971 3729
14daeee3
RS
3730 for (i = 0; i < 32; i++)
3731 {
92fce9bd 3732 char regname[6];
14daeee3
RS
3733
3734 /* R5900 VU0 floating-point register. */
92fce9bd 3735 sprintf (regname, "$vf%d", i);
14daeee3
RS
3736 symbol_table_insert (symbol_new (regname, reg_section,
3737 RTYPE_VF | i, &zero_address_frag));
3738
3739 /* R5900 VU0 integer register. */
92fce9bd 3740 sprintf (regname, "$vi%d", i);
14daeee3
RS
3741 symbol_table_insert (symbol_new (regname, reg_section,
3742 RTYPE_VI | i, &zero_address_frag));
3743
56d438b1 3744 /* MSA register. */
92fce9bd 3745 sprintf (regname, "$w%d", i);
56d438b1
CF
3746 symbol_table_insert (symbol_new (regname, reg_section,
3747 RTYPE_MSA | i, &zero_address_frag));
14daeee3
RS
3748 }
3749
a92713e6
RS
3750 obstack_init (&mips_operand_tokens);
3751
7d10b47d 3752 mips_no_prev_insn ();
252b5132
RH
3753
3754 mips_gprmask = 0;
3755 mips_cprmask[0] = 0;
3756 mips_cprmask[1] = 0;
3757 mips_cprmask[2] = 0;
3758 mips_cprmask[3] = 0;
3759
3760 /* set the default alignment for the text section (2**2) */
3761 record_alignment (text_section, 2);
3762
4d0d148d 3763 bfd_set_gp_size (stdoutput, g_switch_value);
252b5132 3764
f3ded42a
RS
3765 /* On a native system other than VxWorks, sections must be aligned
3766 to 16 byte boundaries. When configured for an embedded ELF
3767 target, we don't bother. */
3768 if (strncmp (TARGET_OS, "elf", 3) != 0
3769 && strncmp (TARGET_OS, "vxworks", 7) != 0)
252b5132 3770 {
f3ded42a
RS
3771 (void) bfd_set_section_alignment (stdoutput, text_section, 4);
3772 (void) bfd_set_section_alignment (stdoutput, data_section, 4);
3773 (void) bfd_set_section_alignment (stdoutput, bss_section, 4);
3774 }
252b5132 3775
f3ded42a
RS
3776 /* Create a .reginfo section for register masks and a .mdebug
3777 section for debugging information. */
3778 {
3779 segT seg;
3780 subsegT subseg;
3781 flagword flags;
3782 segT sec;
3783
3784 seg = now_seg;
3785 subseg = now_subseg;
3786
3787 /* The ABI says this section should be loaded so that the
3788 running program can access it. However, we don't load it
3789 if we are configured for an embedded target */
3790 flags = SEC_READONLY | SEC_DATA;
3791 if (strncmp (TARGET_OS, "elf", 3) != 0)
3792 flags |= SEC_ALLOC | SEC_LOAD;
3793
3794 if (mips_abi != N64_ABI)
252b5132 3795 {
f3ded42a 3796 sec = subseg_new (".reginfo", (subsegT) 0);
bdaaa2e1 3797
f3ded42a
RS
3798 bfd_set_section_flags (stdoutput, sec, flags);
3799 bfd_set_section_alignment (stdoutput, sec, HAVE_NEWABI ? 3 : 2);
252b5132 3800
f3ded42a
RS
3801 mips_regmask_frag = frag_more (sizeof (Elf32_External_RegInfo));
3802 }
3803 else
3804 {
3805 /* The 64-bit ABI uses a .MIPS.options section rather than
3806 .reginfo section. */
3807 sec = subseg_new (".MIPS.options", (subsegT) 0);
3808 bfd_set_section_flags (stdoutput, sec, flags);
3809 bfd_set_section_alignment (stdoutput, sec, 3);
252b5132 3810
f3ded42a
RS
3811 /* Set up the option header. */
3812 {
3813 Elf_Internal_Options opthdr;
3814 char *f;
3815
3816 opthdr.kind = ODK_REGINFO;
3817 opthdr.size = (sizeof (Elf_External_Options)
3818 + sizeof (Elf64_External_RegInfo));
3819 opthdr.section = 0;
3820 opthdr.info = 0;
3821 f = frag_more (sizeof (Elf_External_Options));
3822 bfd_mips_elf_swap_options_out (stdoutput, &opthdr,
3823 (Elf_External_Options *) f);
3824
3825 mips_regmask_frag = frag_more (sizeof (Elf64_External_RegInfo));
3826 }
3827 }
252b5132 3828
351cdf24
MF
3829 sec = subseg_new (".MIPS.abiflags", (subsegT) 0);
3830 bfd_set_section_flags (stdoutput, sec,
3831 SEC_READONLY | SEC_DATA | SEC_ALLOC | SEC_LOAD);
3832 bfd_set_section_alignment (stdoutput, sec, 3);
3833 mips_flags_frag = frag_more (sizeof (Elf_External_ABIFlags_v0));
3834
f3ded42a
RS
3835 if (ECOFF_DEBUGGING)
3836 {
3837 sec = subseg_new (".mdebug", (subsegT) 0);
3838 (void) bfd_set_section_flags (stdoutput, sec,
3839 SEC_HAS_CONTENTS | SEC_READONLY);
3840 (void) bfd_set_section_alignment (stdoutput, sec, 2);
252b5132 3841 }
f3ded42a
RS
3842 else if (mips_flag_pdr)
3843 {
3844 pdr_seg = subseg_new (".pdr", (subsegT) 0);
3845 (void) bfd_set_section_flags (stdoutput, pdr_seg,
3846 SEC_READONLY | SEC_RELOC
3847 | SEC_DEBUGGING);
3848 (void) bfd_set_section_alignment (stdoutput, pdr_seg, 2);
3849 }
3850
3851 subseg_set (seg, subseg);
3852 }
252b5132 3853
71400594
RS
3854 if (mips_fix_vr4120)
3855 init_vr4120_conflicts ();
252b5132
RH
3856}
3857
351cdf24
MF
3858static inline void
3859fpabi_incompatible_with (int fpabi, const char *what)
3860{
3861 as_warn (_(".gnu_attribute %d,%d is incompatible with `%s'"),
3862 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3863}
3864
3865static inline void
3866fpabi_requires (int fpabi, const char *what)
3867{
3868 as_warn (_(".gnu_attribute %d,%d requires `%s'"),
3869 Tag_GNU_MIPS_ABI_FP, fpabi, what);
3870}
3871
3872/* Check -mabi and register sizes against the specified FP ABI. */
3873static void
3874check_fpabi (int fpabi)
3875{
351cdf24
MF
3876 switch (fpabi)
3877 {
3878 case Val_GNU_MIPS_ABI_FP_DOUBLE:
ea79f94a
MF
3879 if (file_mips_opts.soft_float)
3880 fpabi_incompatible_with (fpabi, "softfloat");
3881 else if (file_mips_opts.single_float)
3882 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3883 if (file_mips_opts.gp == 64 && file_mips_opts.fp == 32)
3884 fpabi_incompatible_with (fpabi, "gp=64 fp=32");
3885 else if (file_mips_opts.gp == 32 && file_mips_opts.fp == 64)
3886 fpabi_incompatible_with (fpabi, "gp=32 fp=64");
351cdf24
MF
3887 break;
3888
3889 case Val_GNU_MIPS_ABI_FP_XX:
3890 if (mips_abi != O32_ABI)
3891 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3892 else if (file_mips_opts.soft_float)
3893 fpabi_incompatible_with (fpabi, "softfloat");
3894 else if (file_mips_opts.single_float)
3895 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3896 else if (file_mips_opts.fp != 0)
3897 fpabi_requires (fpabi, "fp=xx");
351cdf24
MF
3898 break;
3899
3900 case Val_GNU_MIPS_ABI_FP_64A:
3901 case Val_GNU_MIPS_ABI_FP_64:
3902 if (mips_abi != O32_ABI)
3903 fpabi_requires (fpabi, "-mabi=32");
ea79f94a
MF
3904 else if (file_mips_opts.soft_float)
3905 fpabi_incompatible_with (fpabi, "softfloat");
3906 else if (file_mips_opts.single_float)
3907 fpabi_incompatible_with (fpabi, "singlefloat");
351cdf24
MF
3908 else if (file_mips_opts.fp != 64)
3909 fpabi_requires (fpabi, "fp=64");
3910 else if (fpabi == Val_GNU_MIPS_ABI_FP_64 && !file_mips_opts.oddspreg)
3911 fpabi_incompatible_with (fpabi, "nooddspreg");
3912 else if (fpabi == Val_GNU_MIPS_ABI_FP_64A && file_mips_opts.oddspreg)
3913 fpabi_requires (fpabi, "nooddspreg");
351cdf24
MF
3914 break;
3915
3916 case Val_GNU_MIPS_ABI_FP_SINGLE:
3917 if (file_mips_opts.soft_float)
3918 fpabi_incompatible_with (fpabi, "softfloat");
3919 else if (!file_mips_opts.single_float)
3920 fpabi_requires (fpabi, "singlefloat");
3921 break;
3922
3923 case Val_GNU_MIPS_ABI_FP_SOFT:
3924 if (!file_mips_opts.soft_float)
3925 fpabi_requires (fpabi, "softfloat");
3926 break;
3927
3928 case Val_GNU_MIPS_ABI_FP_OLD_64:
3929 as_warn (_(".gnu_attribute %d,%d is no longer supported"),
3930 Tag_GNU_MIPS_ABI_FP, fpabi);
3931 break;
3932
3350cc01
CM
3933 case Val_GNU_MIPS_ABI_FP_NAN2008:
3934 /* Silently ignore compatibility value. */
3935 break;
3936
351cdf24
MF
3937 default:
3938 as_warn (_(".gnu_attribute %d,%d is not a recognized"
3939 " floating-point ABI"), Tag_GNU_MIPS_ABI_FP, fpabi);
3940 break;
3941 }
351cdf24
MF
3942}
3943
919731af 3944/* Perform consistency checks on the current options. */
3945
3946static void
3947mips_check_options (struct mips_set_options *opts, bfd_boolean abi_checks)
3948{
3949 /* Check the size of integer registers agrees with the ABI and ISA. */
3950 if (opts->gp == 64 && !ISA_HAS_64BIT_REGS (opts->isa))
3951 as_bad (_("`gp=64' used with a 32-bit processor"));
3952 else if (abi_checks
3953 && opts->gp == 32 && ABI_NEEDS_64BIT_REGS (mips_abi))
3954 as_bad (_("`gp=32' used with a 64-bit ABI"));
3955 else if (abi_checks
3956 && opts->gp == 64 && ABI_NEEDS_32BIT_REGS (mips_abi))
3957 as_bad (_("`gp=64' used with a 32-bit ABI"));
3958
3959 /* Check the size of the float registers agrees with the ABI and ISA. */
3960 switch (opts->fp)
3961 {
351cdf24
MF
3962 case 0:
3963 if (!CPU_HAS_LDC1_SDC1 (opts->arch))
3964 as_bad (_("`fp=xx' used with a cpu lacking ldc1/sdc1 instructions"));
3965 else if (opts->single_float == 1)
3966 as_bad (_("`fp=xx' cannot be used with `singlefloat'"));
3967 break;
919731af 3968 case 64:
3969 if (!ISA_HAS_64BIT_FPRS (opts->isa))
3970 as_bad (_("`fp=64' used with a 32-bit fpu"));
3971 else if (abi_checks
3972 && ABI_NEEDS_32BIT_REGS (mips_abi)
3973 && !ISA_HAS_MXHC1 (opts->isa))
3974 as_warn (_("`fp=64' used with a 32-bit ABI"));
3975 break;
3976 case 32:
3977 if (abi_checks
3978 && ABI_NEEDS_64BIT_REGS (mips_abi))
3979 as_warn (_("`fp=32' used with a 64-bit ABI"));
5f4678bb 3980 if (ISA_IS_R6 (opts->isa) && opts->single_float == 0)
7361da2c 3981 as_bad (_("`fp=32' used with a MIPS R6 cpu"));
919731af 3982 break;
3983 default:
3984 as_bad (_("Unknown size of floating point registers"));
3985 break;
3986 }
3987
351cdf24
MF
3988 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !opts->oddspreg)
3989 as_bad (_("`nooddspreg` cannot be used with a 64-bit ABI"));
3990
919731af 3991 if (opts->micromips == 1 && opts->mips16 == 1)
1357373c 3992 as_bad (_("`%s' cannot be used with `%s'"), "mips16", "micromips");
5f4678bb 3993 else if (ISA_IS_R6 (opts->isa)
7361da2c
AB
3994 && (opts->micromips == 1
3995 || opts->mips16 == 1))
1357373c 3996 as_fatal (_("`%s' cannot be used with `%s'"),
7361da2c 3997 opts->micromips ? "micromips" : "mips16",
5f4678bb 3998 mips_cpu_info_from_isa (opts->isa)->name);
7361da2c
AB
3999
4000 if (ISA_IS_R6 (opts->isa) && mips_relax_branch)
4001 as_fatal (_("branch relaxation is not supported in `%s'"),
4002 mips_cpu_info_from_isa (opts->isa)->name);
919731af 4003}
4004
4005/* Perform consistency checks on the module level options exactly once.
4006 This is a deferred check that happens:
4007 at the first .set directive
4008 or, at the first pseudo op that generates code (inc .dc.a)
4009 or, at the first instruction
4010 or, at the end. */
4011
4012static void
4013file_mips_check_options (void)
4014{
4015 const struct mips_cpu_info *arch_info = 0;
4016
4017 if (file_mips_opts_checked)
4018 return;
4019
4020 /* The following code determines the register size.
4021 Similar code was added to GCC 3.3 (see override_options() in
4022 config/mips/mips.c). The GAS and GCC code should be kept in sync
4023 as much as possible. */
4024
4025 if (file_mips_opts.gp < 0)
4026 {
4027 /* Infer the integer register size from the ABI and processor.
4028 Restrict ourselves to 32-bit registers if that's all the
4029 processor has, or if the ABI cannot handle 64-bit registers. */
4030 file_mips_opts.gp = (ABI_NEEDS_32BIT_REGS (mips_abi)
4031 || !ISA_HAS_64BIT_REGS (file_mips_opts.isa))
4032 ? 32 : 64;
4033 }
4034
4035 if (file_mips_opts.fp < 0)
4036 {
4037 /* No user specified float register size.
4038 ??? GAS treats single-float processors as though they had 64-bit
4039 float registers (although it complains when double-precision
4040 instructions are used). As things stand, saying they have 32-bit
4041 registers would lead to spurious "register must be even" messages.
4042 So here we assume float registers are never smaller than the
4043 integer ones. */
4044 if (file_mips_opts.gp == 64)
4045 /* 64-bit integer registers implies 64-bit float registers. */
4046 file_mips_opts.fp = 64;
4047 else if ((file_mips_opts.ase & FP64_ASES)
4048 && ISA_HAS_64BIT_FPRS (file_mips_opts.isa))
4049 /* Handle ASEs that require 64-bit float registers, if possible. */
4050 file_mips_opts.fp = 64;
7361da2c
AB
4051 else if (ISA_IS_R6 (mips_opts.isa))
4052 /* R6 implies 64-bit float registers. */
4053 file_mips_opts.fp = 64;
919731af 4054 else
4055 /* 32-bit float registers. */
4056 file_mips_opts.fp = 32;
4057 }
4058
4059 arch_info = mips_cpu_info_from_arch (file_mips_opts.arch);
4060
351cdf24
MF
4061 /* Disable operations on odd-numbered floating-point registers by default
4062 when using the FPXX ABI. */
4063 if (file_mips_opts.oddspreg < 0)
4064 {
4065 if (file_mips_opts.fp == 0)
4066 file_mips_opts.oddspreg = 0;
4067 else
4068 file_mips_opts.oddspreg = 1;
4069 }
4070
919731af 4071 /* End of GCC-shared inference code. */
4072
4073 /* This flag is set when we have a 64-bit capable CPU but use only
4074 32-bit wide registers. Note that EABI does not use it. */
4075 if (ISA_HAS_64BIT_REGS (file_mips_opts.isa)
4076 && ((mips_abi == NO_ABI && file_mips_opts.gp == 32)
4077 || mips_abi == O32_ABI))
4078 mips_32bitmode = 1;
4079
4080 if (file_mips_opts.isa == ISA_MIPS1 && mips_trap)
4081 as_bad (_("trap exception not supported at ISA 1"));
4082
4083 /* If the selected architecture includes support for ASEs, enable
4084 generation of code for them. */
4085 if (file_mips_opts.mips16 == -1)
4086 file_mips_opts.mips16 = (CPU_HAS_MIPS16 (file_mips_opts.arch)) ? 1 : 0;
4087 if (file_mips_opts.micromips == -1)
4088 file_mips_opts.micromips = (CPU_HAS_MICROMIPS (file_mips_opts.arch))
4089 ? 1 : 0;
4090
7361da2c
AB
4091 if (mips_nan2008 == -1)
4092 mips_nan2008 = (ISA_HAS_LEGACY_NAN (file_mips_opts.isa)) ? 0 : 1;
4093 else if (!ISA_HAS_LEGACY_NAN (file_mips_opts.isa) && mips_nan2008 == 0)
4094 as_fatal (_("`%s' does not support legacy NaN"),
4095 mips_cpu_info_from_arch (file_mips_opts.arch)->name);
4096
919731af 4097 /* Some ASEs require 64-bit FPRs, so -mfp32 should stop those ASEs from
4098 being selected implicitly. */
4099 if (file_mips_opts.fp != 64)
4100 file_ase_explicit |= ASE_MIPS3D | ASE_MDMX | ASE_MSA;
4101
4102 /* If the user didn't explicitly select or deselect a particular ASE,
4103 use the default setting for the CPU. */
4104 file_mips_opts.ase |= (arch_info->ase & ~file_ase_explicit);
4105
4106 /* Set up the current options. These may change throughout assembly. */
4107 mips_opts = file_mips_opts;
4108
4109 mips_check_isa_supports_ases ();
4110 mips_check_options (&file_mips_opts, TRUE);
4111 file_mips_opts_checked = TRUE;
4112
4113 if (!bfd_set_arch_mach (stdoutput, bfd_arch_mips, file_mips_opts.arch))
4114 as_warn (_("could not set architecture and machine"));
4115}
4116
252b5132 4117void
17a2f251 4118md_assemble (char *str)
252b5132
RH
4119{
4120 struct mips_cl_insn insn;
f6688943
TS
4121 bfd_reloc_code_real_type unused_reloc[3]
4122 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 4123
919731af 4124 file_mips_check_options ();
4125
252b5132 4126 imm_expr.X_op = O_absent;
252b5132 4127 offset_expr.X_op = O_absent;
f6688943
TS
4128 offset_reloc[0] = BFD_RELOC_UNUSED;
4129 offset_reloc[1] = BFD_RELOC_UNUSED;
4130 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132 4131
e1b47bd5
RS
4132 mips_mark_labels ();
4133 mips_assembling_insn = TRUE;
e3de51ce 4134 clear_insn_error ();
e1b47bd5 4135
252b5132
RH
4136 if (mips_opts.mips16)
4137 mips16_ip (str, &insn);
4138 else
4139 {
4140 mips_ip (str, &insn);
beae10d5
KH
4141 DBG ((_("returned from mips_ip(%s) insn_opcode = 0x%x\n"),
4142 str, insn.insn_opcode));
252b5132
RH
4143 }
4144
e3de51ce
RS
4145 if (insn_error.msg)
4146 report_insn_error (str);
e1b47bd5 4147 else if (insn.insn_mo->pinfo == INSN_MACRO)
252b5132 4148 {
584892a6 4149 macro_start ();
252b5132
RH
4150 if (mips_opts.mips16)
4151 mips16_macro (&insn);
4152 else
833794fc 4153 macro (&insn, str);
584892a6 4154 macro_end ();
252b5132
RH
4155 }
4156 else
4157 {
77bd4346 4158 if (offset_expr.X_op != O_absent)
df58fc94 4159 append_insn (&insn, &offset_expr, offset_reloc, FALSE);
252b5132 4160 else
df58fc94 4161 append_insn (&insn, NULL, unused_reloc, FALSE);
252b5132 4162 }
e1b47bd5
RS
4163
4164 mips_assembling_insn = FALSE;
252b5132
RH
4165}
4166
738e5348
RS
4167/* Convenience functions for abstracting away the differences between
4168 MIPS16 and non-MIPS16 relocations. */
4169
4170static inline bfd_boolean
4171mips16_reloc_p (bfd_reloc_code_real_type reloc)
4172{
4173 switch (reloc)
4174 {
4175 case BFD_RELOC_MIPS16_JMP:
4176 case BFD_RELOC_MIPS16_GPREL:
4177 case BFD_RELOC_MIPS16_GOT16:
4178 case BFD_RELOC_MIPS16_CALL16:
4179 case BFD_RELOC_MIPS16_HI16_S:
4180 case BFD_RELOC_MIPS16_HI16:
4181 case BFD_RELOC_MIPS16_LO16:
c9775dde 4182 case BFD_RELOC_MIPS16_16_PCREL_S1:
738e5348
RS
4183 return TRUE;
4184
4185 default:
4186 return FALSE;
4187 }
4188}
4189
df58fc94
RS
4190static inline bfd_boolean
4191micromips_reloc_p (bfd_reloc_code_real_type reloc)
4192{
4193 switch (reloc)
4194 {
4195 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4196 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4197 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
4198 case BFD_RELOC_MICROMIPS_GPREL16:
4199 case BFD_RELOC_MICROMIPS_JMP:
4200 case BFD_RELOC_MICROMIPS_HI16:
4201 case BFD_RELOC_MICROMIPS_HI16_S:
4202 case BFD_RELOC_MICROMIPS_LO16:
4203 case BFD_RELOC_MICROMIPS_LITERAL:
4204 case BFD_RELOC_MICROMIPS_GOT16:
4205 case BFD_RELOC_MICROMIPS_CALL16:
4206 case BFD_RELOC_MICROMIPS_GOT_HI16:
4207 case BFD_RELOC_MICROMIPS_GOT_LO16:
4208 case BFD_RELOC_MICROMIPS_CALL_HI16:
4209 case BFD_RELOC_MICROMIPS_CALL_LO16:
4210 case BFD_RELOC_MICROMIPS_SUB:
4211 case BFD_RELOC_MICROMIPS_GOT_PAGE:
4212 case BFD_RELOC_MICROMIPS_GOT_OFST:
4213 case BFD_RELOC_MICROMIPS_GOT_DISP:
4214 case BFD_RELOC_MICROMIPS_HIGHEST:
4215 case BFD_RELOC_MICROMIPS_HIGHER:
4216 case BFD_RELOC_MICROMIPS_SCN_DISP:
4217 case BFD_RELOC_MICROMIPS_JALR:
4218 return TRUE;
4219
4220 default:
4221 return FALSE;
4222 }
4223}
4224
2309ddf2
MR
4225static inline bfd_boolean
4226jmp_reloc_p (bfd_reloc_code_real_type reloc)
4227{
4228 return reloc == BFD_RELOC_MIPS_JMP || reloc == BFD_RELOC_MICROMIPS_JMP;
4229}
4230
0e9c5a5c
MR
4231static inline bfd_boolean
4232b_reloc_p (bfd_reloc_code_real_type reloc)
4233{
4234 return (reloc == BFD_RELOC_MIPS_26_PCREL_S2
4235 || reloc == BFD_RELOC_MIPS_21_PCREL_S2
4236 || reloc == BFD_RELOC_16_PCREL_S2
c9775dde 4237 || reloc == BFD_RELOC_MIPS16_16_PCREL_S1
0e9c5a5c
MR
4238 || reloc == BFD_RELOC_MICROMIPS_16_PCREL_S1
4239 || reloc == BFD_RELOC_MICROMIPS_10_PCREL_S1
4240 || reloc == BFD_RELOC_MICROMIPS_7_PCREL_S1);
4241}
4242
738e5348
RS
4243static inline bfd_boolean
4244got16_reloc_p (bfd_reloc_code_real_type reloc)
4245{
2309ddf2 4246 return (reloc == BFD_RELOC_MIPS_GOT16 || reloc == BFD_RELOC_MIPS16_GOT16
df58fc94 4247 || reloc == BFD_RELOC_MICROMIPS_GOT16);
738e5348
RS
4248}
4249
4250static inline bfd_boolean
4251hi16_reloc_p (bfd_reloc_code_real_type reloc)
4252{
2309ddf2 4253 return (reloc == BFD_RELOC_HI16_S || reloc == BFD_RELOC_MIPS16_HI16_S
df58fc94 4254 || reloc == BFD_RELOC_MICROMIPS_HI16_S);
738e5348
RS
4255}
4256
4257static inline bfd_boolean
4258lo16_reloc_p (bfd_reloc_code_real_type reloc)
4259{
2309ddf2 4260 return (reloc == BFD_RELOC_LO16 || reloc == BFD_RELOC_MIPS16_LO16
df58fc94
RS
4261 || reloc == BFD_RELOC_MICROMIPS_LO16);
4262}
4263
df58fc94
RS
4264static inline bfd_boolean
4265jalr_reloc_p (bfd_reloc_code_real_type reloc)
4266{
2309ddf2 4267 return reloc == BFD_RELOC_MIPS_JALR || reloc == BFD_RELOC_MICROMIPS_JALR;
738e5348
RS
4268}
4269
f2ae14a1
RS
4270static inline bfd_boolean
4271gprel16_reloc_p (bfd_reloc_code_real_type reloc)
4272{
4273 return (reloc == BFD_RELOC_GPREL16 || reloc == BFD_RELOC_MIPS16_GPREL
4274 || reloc == BFD_RELOC_MICROMIPS_GPREL16);
4275}
4276
2de39019
CM
4277/* Return true if RELOC is a PC-relative relocation that does not have
4278 full address range. */
4279
4280static inline bfd_boolean
4281limited_pcrel_reloc_p (bfd_reloc_code_real_type reloc)
4282{
4283 switch (reloc)
4284 {
4285 case BFD_RELOC_16_PCREL_S2:
c9775dde 4286 case BFD_RELOC_MIPS16_16_PCREL_S1:
2de39019
CM
4287 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
4288 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
4289 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
7361da2c
AB
4290 case BFD_RELOC_MIPS_21_PCREL_S2:
4291 case BFD_RELOC_MIPS_26_PCREL_S2:
4292 case BFD_RELOC_MIPS_18_PCREL_S3:
4293 case BFD_RELOC_MIPS_19_PCREL_S2:
2de39019
CM
4294 return TRUE;
4295
b47468a6 4296 case BFD_RELOC_32_PCREL:
7361da2c
AB
4297 case BFD_RELOC_HI16_S_PCREL:
4298 case BFD_RELOC_LO16_PCREL:
b47468a6
CM
4299 return HAVE_64BIT_ADDRESSES;
4300
2de39019
CM
4301 default:
4302 return FALSE;
4303 }
4304}
b47468a6 4305
5919d012 4306/* Return true if the given relocation might need a matching %lo().
0a44bf69
RS
4307 This is only "might" because SVR4 R_MIPS_GOT16 relocations only
4308 need a matching %lo() when applied to local symbols. */
5919d012
RS
4309
4310static inline bfd_boolean
17a2f251 4311reloc_needs_lo_p (bfd_reloc_code_real_type reloc)
5919d012 4312{
3b91255e 4313 return (HAVE_IN_PLACE_ADDENDS
738e5348 4314 && (hi16_reloc_p (reloc)
0a44bf69
RS
4315 /* VxWorks R_MIPS_GOT16 relocs never need a matching %lo();
4316 all GOT16 relocations evaluate to "G". */
738e5348
RS
4317 || (got16_reloc_p (reloc) && mips_pic != VXWORKS_PIC)));
4318}
4319
4320/* Return the type of %lo() reloc needed by RELOC, given that
4321 reloc_needs_lo_p. */
4322
4323static inline bfd_reloc_code_real_type
4324matching_lo_reloc (bfd_reloc_code_real_type reloc)
4325{
df58fc94
RS
4326 return (mips16_reloc_p (reloc) ? BFD_RELOC_MIPS16_LO16
4327 : (micromips_reloc_p (reloc) ? BFD_RELOC_MICROMIPS_LO16
4328 : BFD_RELOC_LO16));
5919d012
RS
4329}
4330
4331/* Return true if the given fixup is followed by a matching R_MIPS_LO16
4332 relocation. */
4333
4334static inline bfd_boolean
17a2f251 4335fixup_has_matching_lo_p (fixS *fixp)
5919d012
RS
4336{
4337 return (fixp->fx_next != NULL
738e5348 4338 && fixp->fx_next->fx_r_type == matching_lo_reloc (fixp->fx_r_type)
5919d012
RS
4339 && fixp->fx_addsy == fixp->fx_next->fx_addsy
4340 && fixp->fx_offset == fixp->fx_next->fx_offset);
4341}
4342
462427c4
RS
4343/* Move all labels in LABELS to the current insertion point. TEXT_P
4344 says whether the labels refer to text or data. */
404a8071
RS
4345
4346static void
462427c4 4347mips_move_labels (struct insn_label_list *labels, bfd_boolean text_p)
404a8071
RS
4348{
4349 struct insn_label_list *l;
4350 valueT val;
4351
462427c4 4352 for (l = labels; l != NULL; l = l->next)
404a8071 4353 {
9c2799c2 4354 gas_assert (S_GET_SEGMENT (l->label) == now_seg);
404a8071
RS
4355 symbol_set_frag (l->label, frag_now);
4356 val = (valueT) frag_now_fix ();
df58fc94 4357 /* MIPS16/microMIPS text labels are stored as odd. */
462427c4 4358 if (text_p && HAVE_CODE_COMPRESSION)
404a8071
RS
4359 ++val;
4360 S_SET_VALUE (l->label, val);
4361 }
4362}
4363
462427c4
RS
4364/* Move all labels in insn_labels to the current insertion point
4365 and treat them as text labels. */
4366
4367static void
4368mips_move_text_labels (void)
4369{
4370 mips_move_labels (seg_info (now_seg)->label_list, TRUE);
4371}
4372
9e009953
MR
4373/* Duplicate the test for LINK_ONCE sections as in `adjust_reloc_syms'. */
4374
5f0fe04b
TS
4375static bfd_boolean
4376s_is_linkonce (symbolS *sym, segT from_seg)
4377{
4378 bfd_boolean linkonce = FALSE;
4379 segT symseg = S_GET_SEGMENT (sym);
4380
4381 if (symseg != from_seg && !S_IS_LOCAL (sym))
4382 {
4383 if ((bfd_get_section_flags (stdoutput, symseg) & SEC_LINK_ONCE))
4384 linkonce = TRUE;
5f0fe04b
TS
4385 /* The GNU toolchain uses an extension for ELF: a section
4386 beginning with the magic string .gnu.linkonce is a
4387 linkonce section. */
4388 if (strncmp (segment_name (symseg), ".gnu.linkonce",
4389 sizeof ".gnu.linkonce" - 1) == 0)
4390 linkonce = TRUE;
5f0fe04b
TS
4391 }
4392 return linkonce;
4393}
4394
e1b47bd5 4395/* Mark MIPS16 or microMIPS instruction label LABEL. This permits the
df58fc94
RS
4396 linker to handle them specially, such as generating jalx instructions
4397 when needed. We also make them odd for the duration of the assembly,
4398 in order to generate the right sort of code. We will make them even
252b5132
RH
4399 in the adjust_symtab routine, while leaving them marked. This is
4400 convenient for the debugger and the disassembler. The linker knows
4401 to make them odd again. */
4402
4403static void
e1b47bd5 4404mips_compressed_mark_label (symbolS *label)
252b5132 4405{
df58fc94 4406 gas_assert (HAVE_CODE_COMPRESSION);
a8dbcb85 4407
f3ded42a
RS
4408 if (mips_opts.mips16)
4409 S_SET_OTHER (label, ELF_ST_SET_MIPS16 (S_GET_OTHER (label)));
4410 else
4411 S_SET_OTHER (label, ELF_ST_SET_MICROMIPS (S_GET_OTHER (label)));
e1b47bd5
RS
4412 if ((S_GET_VALUE (label) & 1) == 0
4413 /* Don't adjust the address if the label is global or weak, or
4414 in a link-once section, since we'll be emitting symbol reloc
4415 references to it which will be patched up by the linker, and
4416 the final value of the symbol may or may not be MIPS16/microMIPS. */
4417 && !S_IS_WEAK (label)
4418 && !S_IS_EXTERNAL (label)
4419 && !s_is_linkonce (label, now_seg))
4420 S_SET_VALUE (label, S_GET_VALUE (label) | 1);
4421}
4422
4423/* Mark preceding MIPS16 or microMIPS instruction labels. */
4424
4425static void
4426mips_compressed_mark_labels (void)
4427{
4428 struct insn_label_list *l;
4429
4430 for (l = seg_info (now_seg)->label_list; l != NULL; l = l->next)
4431 mips_compressed_mark_label (l->label);
252b5132
RH
4432}
4433
4d7206a2
RS
4434/* End the current frag. Make it a variant frag and record the
4435 relaxation info. */
4436
4437static void
4438relax_close_frag (void)
4439{
584892a6 4440 mips_macro_warning.first_frag = frag_now;
4d7206a2 4441 frag_var (rs_machine_dependent, 0, 0,
ce8ad872
MR
4442 RELAX_ENCODE (mips_relax.sizes[0], mips_relax.sizes[1],
4443 mips_pic != NO_PIC),
4d7206a2
RS
4444 mips_relax.symbol, 0, (char *) mips_relax.first_fixup);
4445
4446 memset (&mips_relax.sizes, 0, sizeof (mips_relax.sizes));
4447 mips_relax.first_fixup = 0;
4448}
4449
4450/* Start a new relaxation sequence whose expansion depends on SYMBOL.
4451 See the comment above RELAX_ENCODE for more details. */
4452
4453static void
4454relax_start (symbolS *symbol)
4455{
9c2799c2 4456 gas_assert (mips_relax.sequence == 0);
4d7206a2
RS
4457 mips_relax.sequence = 1;
4458 mips_relax.symbol = symbol;
4459}
4460
4461/* Start generating the second version of a relaxable sequence.
4462 See the comment above RELAX_ENCODE for more details. */
252b5132
RH
4463
4464static void
4d7206a2
RS
4465relax_switch (void)
4466{
9c2799c2 4467 gas_assert (mips_relax.sequence == 1);
4d7206a2
RS
4468 mips_relax.sequence = 2;
4469}
4470
4471/* End the current relaxable sequence. */
4472
4473static void
4474relax_end (void)
4475{
9c2799c2 4476 gas_assert (mips_relax.sequence == 2);
4d7206a2
RS
4477 relax_close_frag ();
4478 mips_relax.sequence = 0;
4479}
4480
11625dd8
RS
4481/* Return true if IP is a delayed branch or jump. */
4482
4483static inline bfd_boolean
4484delayed_branch_p (const struct mips_cl_insn *ip)
4485{
4486 return (ip->insn_mo->pinfo & (INSN_UNCOND_BRANCH_DELAY
4487 | INSN_COND_BRANCH_DELAY
4488 | INSN_COND_BRANCH_LIKELY)) != 0;
4489}
4490
4491/* Return true if IP is a compact branch or jump. */
4492
4493static inline bfd_boolean
4494compact_branch_p (const struct mips_cl_insn *ip)
4495{
26545944
RS
4496 return (ip->insn_mo->pinfo2 & (INSN2_UNCOND_BRANCH
4497 | INSN2_COND_BRANCH)) != 0;
11625dd8
RS
4498}
4499
4500/* Return true if IP is an unconditional branch or jump. */
4501
4502static inline bfd_boolean
4503uncond_branch_p (const struct mips_cl_insn *ip)
4504{
4505 return ((ip->insn_mo->pinfo & INSN_UNCOND_BRANCH_DELAY) != 0
26545944 4506 || (ip->insn_mo->pinfo2 & INSN2_UNCOND_BRANCH) != 0);
11625dd8
RS
4507}
4508
4509/* Return true if IP is a branch-likely instruction. */
4510
4511static inline bfd_boolean
4512branch_likely_p (const struct mips_cl_insn *ip)
4513{
4514 return (ip->insn_mo->pinfo & INSN_COND_BRANCH_LIKELY) != 0;
4515}
4516
14fe068b
RS
4517/* Return the type of nop that should be used to fill the delay slot
4518 of delayed branch IP. */
4519
4520static struct mips_cl_insn *
4521get_delay_slot_nop (const struct mips_cl_insn *ip)
4522{
4523 if (mips_opts.micromips
4524 && (ip->insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
4525 return &micromips_nop32_insn;
4526 return NOP_INSN;
4527}
4528
fc76e730
RS
4529/* Return a mask that has bit N set if OPCODE reads the register(s)
4530 in operand N. */
df58fc94
RS
4531
4532static unsigned int
fc76e730 4533insn_read_mask (const struct mips_opcode *opcode)
df58fc94 4534{
fc76e730
RS
4535 return (opcode->pinfo & INSN_READ_ALL) >> INSN_READ_SHIFT;
4536}
df58fc94 4537
fc76e730
RS
4538/* Return a mask that has bit N set if OPCODE writes to the register(s)
4539 in operand N. */
4540
4541static unsigned int
4542insn_write_mask (const struct mips_opcode *opcode)
4543{
4544 return (opcode->pinfo & INSN_WRITE_ALL) >> INSN_WRITE_SHIFT;
4545}
4546
4547/* Return a mask of the registers specified by operand OPERAND of INSN.
4548 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4549 is set. */
4550
4551static unsigned int
4552operand_reg_mask (const struct mips_cl_insn *insn,
4553 const struct mips_operand *operand,
4554 unsigned int type_mask)
4555{
4556 unsigned int uval, vsel;
4557
4558 switch (operand->type)
df58fc94 4559 {
fc76e730
RS
4560 case OP_INT:
4561 case OP_MAPPED_INT:
4562 case OP_MSB:
4563 case OP_PCREL:
4564 case OP_PERF_REG:
4565 case OP_ADDIUSP_INT:
4566 case OP_ENTRY_EXIT_LIST:
4567 case OP_REPEAT_DEST_REG:
4568 case OP_REPEAT_PREV_REG:
4569 case OP_PC:
14daeee3
RS
4570 case OP_VU0_SUFFIX:
4571 case OP_VU0_MATCH_SUFFIX:
56d438b1 4572 case OP_IMM_INDEX:
fc76e730
RS
4573 abort ();
4574
25499ac7
MR
4575 case OP_REG28:
4576 return 1 << 28;
4577
fc76e730 4578 case OP_REG:
0f35dbc4 4579 case OP_OPTIONAL_REG:
fc76e730
RS
4580 {
4581 const struct mips_reg_operand *reg_op;
4582
4583 reg_op = (const struct mips_reg_operand *) operand;
4584 if (!(type_mask & (1 << reg_op->reg_type)))
4585 return 0;
4586 uval = insn_extract_operand (insn, operand);
4587 return 1 << mips_decode_reg_operand (reg_op, uval);
4588 }
4589
4590 case OP_REG_PAIR:
4591 {
4592 const struct mips_reg_pair_operand *pair_op;
4593
4594 pair_op = (const struct mips_reg_pair_operand *) operand;
4595 if (!(type_mask & (1 << pair_op->reg_type)))
4596 return 0;
4597 uval = insn_extract_operand (insn, operand);
4598 return (1 << pair_op->reg1_map[uval]) | (1 << pair_op->reg2_map[uval]);
4599 }
4600
4601 case OP_CLO_CLZ_DEST:
4602 if (!(type_mask & (1 << OP_REG_GP)))
4603 return 0;
4604 uval = insn_extract_operand (insn, operand);
4605 return (1 << (uval & 31)) | (1 << (uval >> 5));
4606
7361da2c
AB
4607 case OP_SAME_RS_RT:
4608 if (!(type_mask & (1 << OP_REG_GP)))
4609 return 0;
4610 uval = insn_extract_operand (insn, operand);
4611 gas_assert ((uval & 31) == (uval >> 5));
4612 return 1 << (uval & 31);
4613
4614 case OP_CHECK_PREV:
4615 case OP_NON_ZERO_REG:
4616 if (!(type_mask & (1 << OP_REG_GP)))
4617 return 0;
4618 uval = insn_extract_operand (insn, operand);
4619 return 1 << (uval & 31);
4620
fc76e730
RS
4621 case OP_LWM_SWM_LIST:
4622 abort ();
4623
4624 case OP_SAVE_RESTORE_LIST:
4625 abort ();
4626
4627 case OP_MDMX_IMM_REG:
4628 if (!(type_mask & (1 << OP_REG_VEC)))
4629 return 0;
4630 uval = insn_extract_operand (insn, operand);
4631 vsel = uval >> 5;
4632 if ((vsel & 0x18) == 0x18)
4633 return 0;
4634 return 1 << (uval & 31);
56d438b1
CF
4635
4636 case OP_REG_INDEX:
4637 if (!(type_mask & (1 << OP_REG_GP)))
4638 return 0;
4639 return 1 << insn_extract_operand (insn, operand);
df58fc94 4640 }
fc76e730
RS
4641 abort ();
4642}
4643
4644/* Return a mask of the registers specified by operands OPNO_MASK of INSN,
4645 where bit N of OPNO_MASK is set if operand N should be included.
4646 Ignore registers of type OP_REG_<t> unless bit OP_REG_<t> of TYPE_MASK
4647 is set. */
4648
4649static unsigned int
4650insn_reg_mask (const struct mips_cl_insn *insn,
4651 unsigned int type_mask, unsigned int opno_mask)
4652{
4653 unsigned int opno, reg_mask;
4654
4655 opno = 0;
4656 reg_mask = 0;
4657 while (opno_mask != 0)
4658 {
4659 if (opno_mask & 1)
4660 reg_mask |= operand_reg_mask (insn, insn_opno (insn, opno), type_mask);
4661 opno_mask >>= 1;
4662 opno += 1;
4663 }
4664 return reg_mask;
df58fc94
RS
4665}
4666
4c260379
RS
4667/* Return the mask of core registers that IP reads. */
4668
4669static unsigned int
4670gpr_read_mask (const struct mips_cl_insn *ip)
4671{
4672 unsigned long pinfo, pinfo2;
4673 unsigned int mask;
4674
fc76e730 4675 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_read_mask (ip->insn_mo));
4c260379
RS
4676 pinfo = ip->insn_mo->pinfo;
4677 pinfo2 = ip->insn_mo->pinfo2;
fc76e730 4678 if (pinfo & INSN_UDI)
4c260379 4679 {
fc76e730
RS
4680 /* UDI instructions have traditionally been assumed to read RS
4681 and RT. */
4682 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RT, *ip);
4683 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RS, *ip);
4c260379 4684 }
fc76e730
RS
4685 if (pinfo & INSN_READ_GPR_24)
4686 mask |= 1 << 24;
4687 if (pinfo2 & INSN2_READ_GPR_16)
4688 mask |= 1 << 16;
4689 if (pinfo2 & INSN2_READ_SP)
4690 mask |= 1 << SP;
26545944 4691 if (pinfo2 & INSN2_READ_GPR_31)
fc76e730 4692 mask |= 1 << 31;
fe35f09f
RS
4693 /* Don't include register 0. */
4694 return mask & ~1;
4c260379
RS
4695}
4696
4697/* Return the mask of core registers that IP writes. */
4698
4699static unsigned int
4700gpr_write_mask (const struct mips_cl_insn *ip)
4701{
4702 unsigned long pinfo, pinfo2;
4703 unsigned int mask;
4704
fc76e730 4705 mask = insn_reg_mask (ip, 1 << OP_REG_GP, insn_write_mask (ip->insn_mo));
4c260379
RS
4706 pinfo = ip->insn_mo->pinfo;
4707 pinfo2 = ip->insn_mo->pinfo2;
fc76e730
RS
4708 if (pinfo & INSN_WRITE_GPR_24)
4709 mask |= 1 << 24;
4710 if (pinfo & INSN_WRITE_GPR_31)
4711 mask |= 1 << 31;
4712 if (pinfo & INSN_UDI)
4713 /* UDI instructions have traditionally been assumed to write to RD. */
4714 mask |= 1 << EXTRACT_OPERAND (mips_opts.micromips, RD, *ip);
4715 if (pinfo2 & INSN2_WRITE_SP)
4716 mask |= 1 << SP;
fe35f09f
RS
4717 /* Don't include register 0. */
4718 return mask & ~1;
4c260379
RS
4719}
4720
4721/* Return the mask of floating-point registers that IP reads. */
4722
4723static unsigned int
4724fpr_read_mask (const struct mips_cl_insn *ip)
4725{
fc76e730 4726 unsigned long pinfo;
4c260379
RS
4727 unsigned int mask;
4728
9d5de888
CF
4729 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4730 | (1 << OP_REG_MSA)),
fc76e730 4731 insn_read_mask (ip->insn_mo));
4c260379 4732 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4733 /* Conservatively treat all operands to an FP_D instruction are doubles.
4734 (This is overly pessimistic for things like cvt.d.s.) */
bad1aba3 4735 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4736 mask |= mask << 1;
4737 return mask;
4738}
4739
4740/* Return the mask of floating-point registers that IP writes. */
4741
4742static unsigned int
4743fpr_write_mask (const struct mips_cl_insn *ip)
4744{
fc76e730 4745 unsigned long pinfo;
4c260379
RS
4746 unsigned int mask;
4747
9d5de888
CF
4748 mask = insn_reg_mask (ip, ((1 << OP_REG_FP) | (1 << OP_REG_VEC)
4749 | (1 << OP_REG_MSA)),
fc76e730 4750 insn_write_mask (ip->insn_mo));
4c260379 4751 pinfo = ip->insn_mo->pinfo;
4c260379
RS
4752 /* Conservatively treat all operands to an FP_D instruction are doubles.
4753 (This is overly pessimistic for things like cvt.s.d.) */
bad1aba3 4754 if (FPR_SIZE != 64 && (pinfo & FP_D))
4c260379
RS
4755 mask |= mask << 1;
4756 return mask;
4757}
4758
a1d78564
RS
4759/* Operand OPNUM of INSN is an odd-numbered floating-point register.
4760 Check whether that is allowed. */
4761
4762static bfd_boolean
4763mips_oddfpreg_ok (const struct mips_opcode *insn, int opnum)
4764{
4765 const char *s = insn->name;
351cdf24
MF
4766 bfd_boolean oddspreg = (ISA_HAS_ODD_SINGLE_FPR (mips_opts.isa, mips_opts.arch)
4767 || FPR_SIZE == 64)
4768 && mips_opts.oddspreg;
a1d78564
RS
4769
4770 if (insn->pinfo == INSN_MACRO)
4771 /* Let a macro pass, we'll catch it later when it is expanded. */
4772 return TRUE;
4773
351cdf24
MF
4774 /* Single-precision coprocessor loads and moves are OK for 32-bit registers,
4775 otherwise it depends on oddspreg. */
4776 if ((insn->pinfo & FP_S)
4777 && (insn->pinfo & (INSN_LOAD_MEMORY | INSN_STORE_MEMORY
43885403 4778 | INSN_LOAD_COPROC | INSN_COPROC_MOVE)))
351cdf24 4779 return FPR_SIZE == 32 || oddspreg;
a1d78564 4780
351cdf24
MF
4781 /* Allow odd registers for single-precision ops and double-precision if the
4782 floating-point registers are 64-bit wide. */
4783 switch (insn->pinfo & (FP_S | FP_D))
4784 {
4785 case FP_S:
4786 case 0:
4787 return oddspreg;
4788 case FP_D:
4789 return FPR_SIZE == 64;
4790 default:
4791 break;
a1d78564
RS
4792 }
4793
351cdf24
MF
4794 /* Cvt.w.x and cvt.x.w allow an odd register for a 'w' or 's' operand. */
4795 s = strchr (insn->name, '.');
4796 if (s != NULL && opnum == 2)
4797 s = strchr (s + 1, '.');
4798 if (s != NULL && (s[1] == 'w' || s[1] == 's'))
4799 return oddspreg;
a1d78564 4800
351cdf24 4801 return FPR_SIZE == 64;
a1d78564
RS
4802}
4803
a1d78564
RS
4804/* Information about an instruction argument that we're trying to match. */
4805struct mips_arg_info
4806{
4807 /* The instruction so far. */
4808 struct mips_cl_insn *insn;
4809
a92713e6
RS
4810 /* The first unconsumed operand token. */
4811 struct mips_operand_token *token;
4812
a1d78564
RS
4813 /* The 1-based operand number, in terms of insn->insn_mo->args. */
4814 int opnum;
4815
4816 /* The 1-based argument number, for error reporting. This does not
4817 count elided optional registers, etc.. */
4818 int argnum;
4819
4820 /* The last OP_REG operand seen, or ILLEGAL_REG if none. */
4821 unsigned int last_regno;
4822
4823 /* If the first operand was an OP_REG, this is the register that it
4824 specified, otherwise it is ILLEGAL_REG. */
4825 unsigned int dest_regno;
4826
4827 /* The value of the last OP_INT operand. Only used for OP_MSB,
4828 where it gives the lsb position. */
4829 unsigned int last_op_int;
4830
60f20e8b 4831 /* If true, match routines should assume that no later instruction
2b0f3761 4832 alternative matches and should therefore be as accommodating as
60f20e8b
RS
4833 possible. Match routines should not report errors if something
4834 is only invalid for !LAX_MATCH. */
4835 bfd_boolean lax_match;
a1d78564 4836
a1d78564
RS
4837 /* True if a reference to the current AT register was seen. */
4838 bfd_boolean seen_at;
4839};
4840
1a00e612
RS
4841/* Record that the argument is out of range. */
4842
4843static void
4844match_out_of_range (struct mips_arg_info *arg)
4845{
4846 set_insn_error_i (arg->argnum, _("operand %d out of range"), arg->argnum);
4847}
4848
4849/* Record that the argument isn't constant but needs to be. */
4850
4851static void
4852match_not_constant (struct mips_arg_info *arg)
4853{
4854 set_insn_error_i (arg->argnum, _("operand %d must be constant"),
4855 arg->argnum);
4856}
4857
a92713e6
RS
4858/* Try to match an OT_CHAR token for character CH. Consume the token
4859 and return true on success, otherwise return false. */
a1d78564 4860
a92713e6
RS
4861static bfd_boolean
4862match_char (struct mips_arg_info *arg, char ch)
a1d78564 4863{
a92713e6
RS
4864 if (arg->token->type == OT_CHAR && arg->token->u.ch == ch)
4865 {
4866 ++arg->token;
4867 if (ch == ',')
4868 arg->argnum += 1;
4869 return TRUE;
4870 }
4871 return FALSE;
4872}
a1d78564 4873
a92713e6
RS
4874/* Try to get an expression from the next tokens in ARG. Consume the
4875 tokens and return true on success, storing the expression value in
4876 VALUE and relocation types in R. */
4877
4878static bfd_boolean
4879match_expression (struct mips_arg_info *arg, expressionS *value,
4880 bfd_reloc_code_real_type *r)
4881{
d436c1c2
RS
4882 /* If the next token is a '(' that was parsed as being part of a base
4883 expression, assume we have an elided offset. The later match will fail
4884 if this turns out to be wrong. */
4885 if (arg->token->type == OT_CHAR && arg->token->u.ch == '(')
a1d78564 4886 {
d436c1c2
RS
4887 value->X_op = O_constant;
4888 value->X_add_number = 0;
4889 r[0] = r[1] = r[2] = BFD_RELOC_UNUSED;
a92713e6
RS
4890 return TRUE;
4891 }
4892
d436c1c2
RS
4893 /* Reject register-based expressions such as "0+$2" and "(($2))".
4894 For plain registers the default error seems more appropriate. */
4895 if (arg->token->type == OT_INTEGER
4896 && arg->token->u.integer.value.X_op == O_register)
a92713e6 4897 {
d436c1c2
RS
4898 set_insn_error (arg->argnum, _("register value used as expression"));
4899 return FALSE;
a1d78564 4900 }
d436c1c2
RS
4901
4902 if (arg->token->type == OT_INTEGER)
a92713e6 4903 {
d436c1c2
RS
4904 *value = arg->token->u.integer.value;
4905 memcpy (r, arg->token->u.integer.relocs, 3 * sizeof (*r));
4906 ++arg->token;
4907 return TRUE;
a92713e6 4908 }
a92713e6 4909
d436c1c2
RS
4910 set_insn_error_i
4911 (arg->argnum, _("operand %d must be an immediate expression"),
4912 arg->argnum);
4913 return FALSE;
a92713e6
RS
4914}
4915
4916/* Try to get a constant expression from the next tokens in ARG. Consume
de194d85 4917 the tokens and return true on success, storing the constant value
a54d5f8b 4918 in *VALUE. */
a92713e6
RS
4919
4920static bfd_boolean
1a00e612 4921match_const_int (struct mips_arg_info *arg, offsetT *value)
a92713e6
RS
4922{
4923 expressionS ex;
4924 bfd_reloc_code_real_type r[3];
a1d78564 4925
a92713e6
RS
4926 if (!match_expression (arg, &ex, r))
4927 return FALSE;
4928
4929 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_constant)
a1d78564
RS
4930 *value = ex.X_add_number;
4931 else
4932 {
c96425c5
MR
4933 if (r[0] == BFD_RELOC_UNUSED && ex.X_op == O_big)
4934 match_out_of_range (arg);
4935 else
4936 match_not_constant (arg);
1a00e612 4937 return FALSE;
a1d78564 4938 }
a92713e6 4939 return TRUE;
a1d78564
RS
4940}
4941
4942/* Return the RTYPE_* flags for a register operand of type TYPE that
4943 appears in instruction OPCODE. */
4944
4945static unsigned int
4946convert_reg_type (const struct mips_opcode *opcode,
4947 enum mips_reg_operand_type type)
4948{
4949 switch (type)
4950 {
4951 case OP_REG_GP:
4952 return RTYPE_NUM | RTYPE_GP;
4953
4954 case OP_REG_FP:
4955 /* Allow vector register names for MDMX if the instruction is a 64-bit
4956 FPR load, store or move (including moves to and from GPRs). */
4957 if ((mips_opts.ase & ASE_MDMX)
4958 && (opcode->pinfo & FP_D)
43885403 4959 && (opcode->pinfo & (INSN_COPROC_MOVE
a1d78564 4960 | INSN_COPROC_MEMORY_DELAY
43885403 4961 | INSN_LOAD_COPROC
67dc82bc 4962 | INSN_LOAD_MEMORY
a1d78564
RS
4963 | INSN_STORE_MEMORY)))
4964 return RTYPE_FPU | RTYPE_VEC;
4965 return RTYPE_FPU;
4966
4967 case OP_REG_CCC:
4968 if (opcode->pinfo & (FP_D | FP_S))
4969 return RTYPE_CCC | RTYPE_FCC;
4970 return RTYPE_CCC;
4971
4972 case OP_REG_VEC:
4973 if (opcode->membership & INSN_5400)
4974 return RTYPE_FPU;
4975 return RTYPE_FPU | RTYPE_VEC;
4976
4977 case OP_REG_ACC:
4978 return RTYPE_ACC;
4979
4980 case OP_REG_COPRO:
4981 if (opcode->name[strlen (opcode->name) - 1] == '0')
4982 return RTYPE_NUM | RTYPE_CP0;
4983 return RTYPE_NUM;
4984
4985 case OP_REG_HW:
4986 return RTYPE_NUM;
14daeee3
RS
4987
4988 case OP_REG_VI:
4989 return RTYPE_NUM | RTYPE_VI;
4990
4991 case OP_REG_VF:
4992 return RTYPE_NUM | RTYPE_VF;
4993
4994 case OP_REG_R5900_I:
4995 return RTYPE_R5900_I;
4996
4997 case OP_REG_R5900_Q:
4998 return RTYPE_R5900_Q;
4999
5000 case OP_REG_R5900_R:
5001 return RTYPE_R5900_R;
5002
5003 case OP_REG_R5900_ACC:
5004 return RTYPE_R5900_ACC;
56d438b1
CF
5005
5006 case OP_REG_MSA:
5007 return RTYPE_MSA;
5008
5009 case OP_REG_MSA_CTRL:
5010 return RTYPE_NUM;
a1d78564
RS
5011 }
5012 abort ();
5013}
5014
5015/* ARG is register REGNO, of type TYPE. Warn about any dubious registers. */
5016
5017static void
5018check_regno (struct mips_arg_info *arg,
5019 enum mips_reg_operand_type type, unsigned int regno)
5020{
5021 if (AT && type == OP_REG_GP && regno == AT)
5022 arg->seen_at = TRUE;
5023
5024 if (type == OP_REG_FP
5025 && (regno & 1) != 0
a1d78564 5026 && !mips_oddfpreg_ok (arg->insn->insn_mo, arg->opnum))
351cdf24
MF
5027 {
5028 /* This was a warning prior to introducing O32 FPXX and FP64 support
5029 so maintain a warning for FP32 but raise an error for the new
5030 cases. */
5031 if (FPR_SIZE == 32)
5032 as_warn (_("float register should be even, was %d"), regno);
5033 else
5034 as_bad (_("float register should be even, was %d"), regno);
5035 }
a1d78564
RS
5036
5037 if (type == OP_REG_CCC)
5038 {
5039 const char *name;
5040 size_t length;
5041
5042 name = arg->insn->insn_mo->name;
5043 length = strlen (name);
5044 if ((regno & 1) != 0
5045 && ((length >= 3 && strcmp (name + length - 3, ".ps") == 0)
5046 || (length >= 5 && strncmp (name + length - 5, "any2", 4) == 0)))
1661c76c 5047 as_warn (_("condition code register should be even for %s, was %d"),
a1d78564
RS
5048 name, regno);
5049
5050 if ((regno & 3) != 0
5051 && (length >= 5 && strncmp (name + length - 5, "any4", 4) == 0))
1661c76c 5052 as_warn (_("condition code register should be 0 or 4 for %s, was %d"),
a1d78564
RS
5053 name, regno);
5054 }
5055}
5056
a92713e6
RS
5057/* ARG is a register with symbol value SYMVAL. Try to interpret it as
5058 a register of type TYPE. Return true on success, storing the register
5059 number in *REGNO and warning about any dubious uses. */
5060
5061static bfd_boolean
5062match_regno (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5063 unsigned int symval, unsigned int *regno)
5064{
5065 if (type == OP_REG_VEC)
5066 symval = mips_prefer_vec_regno (symval);
5067 if (!(symval & convert_reg_type (arg->insn->insn_mo, type)))
5068 return FALSE;
5069
5070 *regno = symval & RNUM_MASK;
5071 check_regno (arg, type, *regno);
5072 return TRUE;
5073}
5074
5075/* Try to interpret the next token in ARG as a register of type TYPE.
5076 Consume the token and return true on success, storing the register
5077 number in *REGNO. Return false on failure. */
5078
5079static bfd_boolean
5080match_reg (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5081 unsigned int *regno)
5082{
5083 if (arg->token->type == OT_REG
5084 && match_regno (arg, type, arg->token->u.regno, regno))
5085 {
5086 ++arg->token;
5087 return TRUE;
5088 }
5089 return FALSE;
5090}
5091
5092/* Try to interpret the next token in ARG as a range of registers of type TYPE.
5093 Consume the token and return true on success, storing the register numbers
5094 in *REGNO1 and *REGNO2. Return false on failure. */
5095
5096static bfd_boolean
5097match_reg_range (struct mips_arg_info *arg, enum mips_reg_operand_type type,
5098 unsigned int *regno1, unsigned int *regno2)
5099{
5100 if (match_reg (arg, type, regno1))
5101 {
5102 *regno2 = *regno1;
5103 return TRUE;
5104 }
5105 if (arg->token->type == OT_REG_RANGE
5106 && match_regno (arg, type, arg->token->u.reg_range.regno1, regno1)
5107 && match_regno (arg, type, arg->token->u.reg_range.regno2, regno2)
5108 && *regno1 <= *regno2)
5109 {
5110 ++arg->token;
5111 return TRUE;
5112 }
5113 return FALSE;
5114}
5115
a1d78564
RS
5116/* OP_INT matcher. */
5117
a92713e6 5118static bfd_boolean
a1d78564 5119match_int_operand (struct mips_arg_info *arg,
a92713e6 5120 const struct mips_operand *operand_base)
a1d78564
RS
5121{
5122 const struct mips_int_operand *operand;
3ccad066 5123 unsigned int uval;
a1d78564
RS
5124 int min_val, max_val, factor;
5125 offsetT sval;
a1d78564
RS
5126
5127 operand = (const struct mips_int_operand *) operand_base;
5128 factor = 1 << operand->shift;
3ccad066
RS
5129 min_val = mips_int_operand_min (operand);
5130 max_val = mips_int_operand_max (operand);
a1d78564 5131
d436c1c2
RS
5132 if (operand_base->lsb == 0
5133 && operand_base->size == 16
5134 && operand->shift == 0
5135 && operand->bias == 0
5136 && (operand->max_val == 32767 || operand->max_val == 65535))
a1d78564
RS
5137 {
5138 /* The operand can be relocated. */
a92713e6
RS
5139 if (!match_expression (arg, &offset_expr, offset_reloc))
5140 return FALSE;
5141
c96425c5
MR
5142 if (offset_expr.X_op == O_big)
5143 {
5144 match_out_of_range (arg);
5145 return FALSE;
5146 }
5147
a92713e6 5148 if (offset_reloc[0] != BFD_RELOC_UNUSED)
33eaf5de 5149 /* Relocation operators were used. Accept the argument and
a1d78564
RS
5150 leave the relocation value in offset_expr and offset_relocs
5151 for the caller to process. */
a92713e6
RS
5152 return TRUE;
5153
5154 if (offset_expr.X_op != O_constant)
a1d78564 5155 {
60f20e8b
RS
5156 /* Accept non-constant operands if no later alternative matches,
5157 leaving it for the caller to process. */
5158 if (!arg->lax_match)
602b88e3
MR
5159 {
5160 match_not_constant (arg);
5161 return FALSE;
5162 }
a92713e6
RS
5163 offset_reloc[0] = BFD_RELOC_LO16;
5164 return TRUE;
a1d78564 5165 }
a92713e6 5166
a1d78564
RS
5167 /* Clear the global state; we're going to install the operand
5168 ourselves. */
a92713e6 5169 sval = offset_expr.X_add_number;
a1d78564 5170 offset_expr.X_op = O_absent;
60f20e8b
RS
5171
5172 /* For compatibility with older assemblers, we accept
5173 0x8000-0xffff as signed 16-bit numbers when only
5174 signed numbers are allowed. */
5175 if (sval > max_val)
5176 {
5177 max_val = ((1 << operand_base->size) - 1) << operand->shift;
5178 if (!arg->lax_match && sval <= max_val)
20c59b84
MR
5179 {
5180 match_out_of_range (arg);
5181 return FALSE;
5182 }
60f20e8b 5183 }
a1d78564
RS
5184 }
5185 else
5186 {
1a00e612 5187 if (!match_const_int (arg, &sval))
a92713e6 5188 return FALSE;
a1d78564
RS
5189 }
5190
5191 arg->last_op_int = sval;
5192
1a00e612 5193 if (sval < min_val || sval > max_val || sval % factor)
a1d78564 5194 {
1a00e612
RS
5195 match_out_of_range (arg);
5196 return FALSE;
a1d78564
RS
5197 }
5198
5199 uval = (unsigned int) sval >> operand->shift;
5200 uval -= operand->bias;
5201
5202 /* Handle -mfix-cn63xxp1. */
5203 if (arg->opnum == 1
5204 && mips_fix_cn63xxp1
5205 && !mips_opts.micromips
5206 && strcmp ("pref", arg->insn->insn_mo->name) == 0)
5207 switch (uval)
5208 {
5209 case 5:
5210 case 25:
5211 case 26:
5212 case 27:
5213 case 28:
5214 case 29:
5215 case 30:
5216 case 31:
5217 /* These are ok. */
5218 break;
5219
5220 default:
5221 /* The rest must be changed to 28. */
5222 uval = 28;
5223 break;
5224 }
5225
5226 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5227 return TRUE;
a1d78564
RS
5228}
5229
5230/* OP_MAPPED_INT matcher. */
5231
a92713e6 5232static bfd_boolean
a1d78564 5233match_mapped_int_operand (struct mips_arg_info *arg,
a92713e6 5234 const struct mips_operand *operand_base)
a1d78564
RS
5235{
5236 const struct mips_mapped_int_operand *operand;
5237 unsigned int uval, num_vals;
5238 offsetT sval;
5239
5240 operand = (const struct mips_mapped_int_operand *) operand_base;
1a00e612 5241 if (!match_const_int (arg, &sval))
a92713e6 5242 return FALSE;
a1d78564
RS
5243
5244 num_vals = 1 << operand_base->size;
5245 for (uval = 0; uval < num_vals; uval++)
5246 if (operand->int_map[uval] == sval)
5247 break;
5248 if (uval == num_vals)
1a00e612
RS
5249 {
5250 match_out_of_range (arg);
5251 return FALSE;
5252 }
a1d78564
RS
5253
5254 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5255 return TRUE;
a1d78564
RS
5256}
5257
5258/* OP_MSB matcher. */
5259
a92713e6 5260static bfd_boolean
a1d78564 5261match_msb_operand (struct mips_arg_info *arg,
a92713e6 5262 const struct mips_operand *operand_base)
a1d78564
RS
5263{
5264 const struct mips_msb_operand *operand;
5265 int min_val, max_val, max_high;
5266 offsetT size, sval, high;
5267
5268 operand = (const struct mips_msb_operand *) operand_base;
5269 min_val = operand->bias;
5270 max_val = min_val + (1 << operand_base->size) - 1;
5271 max_high = operand->opsize;
5272
1a00e612 5273 if (!match_const_int (arg, &size))
a92713e6 5274 return FALSE;
a1d78564
RS
5275
5276 high = size + arg->last_op_int;
5277 sval = operand->add_lsb ? high : size;
5278
5279 if (size < 0 || high > max_high || sval < min_val || sval > max_val)
5280 {
1a00e612
RS
5281 match_out_of_range (arg);
5282 return FALSE;
a1d78564
RS
5283 }
5284 insn_insert_operand (arg->insn, operand_base, sval - min_val);
a92713e6 5285 return TRUE;
a1d78564
RS
5286}
5287
5288/* OP_REG matcher. */
5289
a92713e6 5290static bfd_boolean
a1d78564 5291match_reg_operand (struct mips_arg_info *arg,
a92713e6 5292 const struct mips_operand *operand_base)
a1d78564
RS
5293{
5294 const struct mips_reg_operand *operand;
a92713e6 5295 unsigned int regno, uval, num_vals;
a1d78564
RS
5296
5297 operand = (const struct mips_reg_operand *) operand_base;
a92713e6
RS
5298 if (!match_reg (arg, operand->reg_type, &regno))
5299 return FALSE;
a1d78564
RS
5300
5301 if (operand->reg_map)
5302 {
5303 num_vals = 1 << operand->root.size;
5304 for (uval = 0; uval < num_vals; uval++)
5305 if (operand->reg_map[uval] == regno)
5306 break;
5307 if (num_vals == uval)
a92713e6 5308 return FALSE;
a1d78564
RS
5309 }
5310 else
5311 uval = regno;
5312
a1d78564
RS
5313 arg->last_regno = regno;
5314 if (arg->opnum == 1)
5315 arg->dest_regno = regno;
5316 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5317 return TRUE;
a1d78564
RS
5318}
5319
5320/* OP_REG_PAIR matcher. */
5321
a92713e6 5322static bfd_boolean
a1d78564 5323match_reg_pair_operand (struct mips_arg_info *arg,
a92713e6 5324 const struct mips_operand *operand_base)
a1d78564
RS
5325{
5326 const struct mips_reg_pair_operand *operand;
a92713e6 5327 unsigned int regno1, regno2, uval, num_vals;
a1d78564
RS
5328
5329 operand = (const struct mips_reg_pair_operand *) operand_base;
a92713e6
RS
5330 if (!match_reg (arg, operand->reg_type, &regno1)
5331 || !match_char (arg, ',')
5332 || !match_reg (arg, operand->reg_type, &regno2))
5333 return FALSE;
a1d78564
RS
5334
5335 num_vals = 1 << operand_base->size;
5336 for (uval = 0; uval < num_vals; uval++)
5337 if (operand->reg1_map[uval] == regno1 && operand->reg2_map[uval] == regno2)
5338 break;
5339 if (uval == num_vals)
a92713e6 5340 return FALSE;
a1d78564 5341
a1d78564 5342 insn_insert_operand (arg->insn, operand_base, uval);
a92713e6 5343 return TRUE;
a1d78564
RS
5344}
5345
5346/* OP_PCREL matcher. The caller chooses the relocation type. */
5347
a92713e6
RS
5348static bfd_boolean
5349match_pcrel_operand (struct mips_arg_info *arg)
a1d78564 5350{
a92713e6
RS
5351 bfd_reloc_code_real_type r[3];
5352
5353 return match_expression (arg, &offset_expr, r) && r[0] == BFD_RELOC_UNUSED;
a1d78564
RS
5354}
5355
5356/* OP_PERF_REG matcher. */
5357
a92713e6 5358static bfd_boolean
a1d78564 5359match_perf_reg_operand (struct mips_arg_info *arg,
a92713e6 5360 const struct mips_operand *operand)
a1d78564
RS
5361{
5362 offsetT sval;
5363
1a00e612 5364 if (!match_const_int (arg, &sval))
a92713e6 5365 return FALSE;
a1d78564
RS
5366
5367 if (sval != 0
5368 && (sval != 1
5369 || (mips_opts.arch == CPU_R5900
5370 && (strcmp (arg->insn->insn_mo->name, "mfps") == 0
5371 || strcmp (arg->insn->insn_mo->name, "mtps") == 0))))
5372 {
1a00e612
RS
5373 set_insn_error (arg->argnum, _("invalid performance register"));
5374 return FALSE;
a1d78564
RS
5375 }
5376
5377 insn_insert_operand (arg->insn, operand, sval);
a92713e6 5378 return TRUE;
a1d78564
RS
5379}
5380
5381/* OP_ADDIUSP matcher. */
5382
a92713e6 5383static bfd_boolean
a1d78564 5384match_addiusp_operand (struct mips_arg_info *arg,
a92713e6 5385 const struct mips_operand *operand)
a1d78564
RS
5386{
5387 offsetT sval;
5388 unsigned int uval;
5389
1a00e612 5390 if (!match_const_int (arg, &sval))
a92713e6 5391 return FALSE;
a1d78564
RS
5392
5393 if (sval % 4)
1a00e612
RS
5394 {
5395 match_out_of_range (arg);
5396 return FALSE;
5397 }
a1d78564
RS
5398
5399 sval /= 4;
5400 if (!(sval >= -258 && sval <= 257) || (sval >= -2 && sval <= 1))
1a00e612
RS
5401 {
5402 match_out_of_range (arg);
5403 return FALSE;
5404 }
a1d78564
RS
5405
5406 uval = (unsigned int) sval;
5407 uval = ((uval >> 1) & ~0xff) | (uval & 0xff);
5408 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5409 return TRUE;
a1d78564
RS
5410}
5411
5412/* OP_CLO_CLZ_DEST matcher. */
5413
a92713e6 5414static bfd_boolean
a1d78564 5415match_clo_clz_dest_operand (struct mips_arg_info *arg,
a92713e6 5416 const struct mips_operand *operand)
a1d78564
RS
5417{
5418 unsigned int regno;
5419
a92713e6
RS
5420 if (!match_reg (arg, OP_REG_GP, &regno))
5421 return FALSE;
a1d78564 5422
a1d78564 5423 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
a92713e6 5424 return TRUE;
a1d78564
RS
5425}
5426
7361da2c
AB
5427/* OP_CHECK_PREV matcher. */
5428
5429static bfd_boolean
5430match_check_prev_operand (struct mips_arg_info *arg,
5431 const struct mips_operand *operand_base)
5432{
5433 const struct mips_check_prev_operand *operand;
5434 unsigned int regno;
5435
5436 operand = (const struct mips_check_prev_operand *) operand_base;
5437
5438 if (!match_reg (arg, OP_REG_GP, &regno))
5439 return FALSE;
5440
5441 if (!operand->zero_ok && regno == 0)
5442 return FALSE;
5443
5444 if ((operand->less_than_ok && regno < arg->last_regno)
5445 || (operand->greater_than_ok && regno > arg->last_regno)
5446 || (operand->equal_ok && regno == arg->last_regno))
5447 {
5448 arg->last_regno = regno;
5449 insn_insert_operand (arg->insn, operand_base, regno);
5450 return TRUE;
5451 }
5452
5453 return FALSE;
5454}
5455
5456/* OP_SAME_RS_RT matcher. */
5457
5458static bfd_boolean
5459match_same_rs_rt_operand (struct mips_arg_info *arg,
5460 const struct mips_operand *operand)
5461{
5462 unsigned int regno;
5463
5464 if (!match_reg (arg, OP_REG_GP, &regno))
5465 return FALSE;
5466
5467 if (regno == 0)
5468 {
5469 set_insn_error (arg->argnum, _("the source register must not be $0"));
5470 return FALSE;
5471 }
5472
5473 arg->last_regno = regno;
5474
5475 insn_insert_operand (arg->insn, operand, regno | (regno << 5));
5476 return TRUE;
5477}
5478
a1d78564
RS
5479/* OP_LWM_SWM_LIST matcher. */
5480
a92713e6 5481static bfd_boolean
a1d78564 5482match_lwm_swm_list_operand (struct mips_arg_info *arg,
a92713e6 5483 const struct mips_operand *operand)
a1d78564 5484{
a92713e6
RS
5485 unsigned int reglist, sregs, ra, regno1, regno2;
5486 struct mips_arg_info reset;
a1d78564 5487
a92713e6
RS
5488 reglist = 0;
5489 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5490 return FALSE;
5491 do
5492 {
5493 if (regno2 == FP && regno1 >= S0 && regno1 <= S7)
5494 {
5495 reglist |= 1 << FP;
5496 regno2 = S7;
5497 }
5498 reglist |= ((1U << regno2 << 1) - 1) & -(1U << regno1);
5499 reset = *arg;
5500 }
5501 while (match_char (arg, ',')
5502 && match_reg_range (arg, OP_REG_GP, &regno1, &regno2));
5503 *arg = reset;
a1d78564
RS
5504
5505 if (operand->size == 2)
5506 {
5507 /* The list must include both ra and s0-sN, for 0 <= N <= 3. E.g.:
5508
5509 s0, ra
5510 s0, s1, ra, s2, s3
5511 s0-s2, ra
5512
5513 and any permutations of these. */
5514 if ((reglist & 0xfff1ffff) != 0x80010000)
a92713e6 5515 return FALSE;
a1d78564
RS
5516
5517 sregs = (reglist >> 17) & 7;
5518 ra = 0;
5519 }
5520 else
5521 {
5522 /* The list must include at least one of ra and s0-sN,
5523 for 0 <= N <= 8. (Note that there is a gap between s7 and s8,
5524 which are $23 and $30 respectively.) E.g.:
5525
5526 ra
5527 s0
5528 ra, s0, s1, s2
5529 s0-s8
5530 s0-s5, ra
5531
5532 and any permutations of these. */
5533 if ((reglist & 0x3f00ffff) != 0)
a92713e6 5534 return FALSE;
a1d78564
RS
5535
5536 ra = (reglist >> 27) & 0x10;
5537 sregs = ((reglist >> 22) & 0x100) | ((reglist >> 16) & 0xff);
5538 }
5539 sregs += 1;
5540 if ((sregs & -sregs) != sregs)
a92713e6 5541 return FALSE;
a1d78564
RS
5542
5543 insn_insert_operand (arg->insn, operand, (ffs (sregs) - 1) | ra);
a92713e6 5544 return TRUE;
a1d78564
RS
5545}
5546
364215c8
RS
5547/* OP_ENTRY_EXIT_LIST matcher. */
5548
a92713e6 5549static unsigned int
364215c8 5550match_entry_exit_operand (struct mips_arg_info *arg,
a92713e6 5551 const struct mips_operand *operand)
364215c8
RS
5552{
5553 unsigned int mask;
5554 bfd_boolean is_exit;
5555
5556 /* The format is the same for both ENTRY and EXIT, but the constraints
5557 are different. */
5558 is_exit = strcmp (arg->insn->insn_mo->name, "exit") == 0;
5559 mask = (is_exit ? 7 << 3 : 0);
a92713e6 5560 do
364215c8
RS
5561 {
5562 unsigned int regno1, regno2;
5563 bfd_boolean is_freg;
5564
a92713e6 5565 if (match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
364215c8 5566 is_freg = FALSE;
a92713e6 5567 else if (match_reg_range (arg, OP_REG_FP, &regno1, &regno2))
364215c8
RS
5568 is_freg = TRUE;
5569 else
a92713e6 5570 return FALSE;
364215c8
RS
5571
5572 if (is_exit && is_freg && regno1 == 0 && regno2 < 2)
5573 {
5574 mask &= ~(7 << 3);
5575 mask |= (5 + regno2) << 3;
5576 }
5577 else if (!is_exit && regno1 == 4 && regno2 >= 4 && regno2 <= 7)
5578 mask |= (regno2 - 3) << 3;
5579 else if (regno1 == 16 && regno2 >= 16 && regno2 <= 17)
5580 mask |= (regno2 - 15) << 1;
5581 else if (regno1 == RA && regno2 == RA)
5582 mask |= 1;
5583 else
a92713e6 5584 return FALSE;
364215c8 5585 }
a92713e6
RS
5586 while (match_char (arg, ','));
5587
364215c8 5588 insn_insert_operand (arg->insn, operand, mask);
a92713e6 5589 return TRUE;
364215c8
RS
5590}
5591
38bf472a
MR
5592/* Encode regular MIPS SAVE/RESTORE instruction operands according to
5593 the argument register mask AMASK, the number of static registers
5594 saved NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5595 respectively, and the frame size FRAME_SIZE. */
5596
5597static unsigned int
5598mips_encode_save_restore (unsigned int amask, unsigned int nsreg,
5599 unsigned int ra, unsigned int s0, unsigned int s1,
5600 unsigned int frame_size)
5601{
5602 return ((nsreg << 23) | ((frame_size & 0xf0) << 15) | (amask << 15)
5603 | (ra << 12) | (s0 << 11) | (s1 << 10) | ((frame_size & 0xf) << 6));
5604}
5605
5606/* Encode MIPS16 SAVE/RESTORE instruction operands according to the
5607 argument register mask AMASK, the number of static registers saved
5608 NSREG, the $ra, $s0 and $s1 register specifiers RA, S0 and S1
5609 respectively, and the frame size FRAME_SIZE. */
5610
5611static unsigned int
5612mips16_encode_save_restore (unsigned int amask, unsigned int nsreg,
5613 unsigned int ra, unsigned int s0, unsigned int s1,
5614 unsigned int frame_size)
5615{
5616 unsigned int args;
5617
5618 args = (ra << 6) | (s0 << 5) | (s1 << 4) | (frame_size & 0xf);
5619 if (nsreg || amask || frame_size == 0 || frame_size > 16)
5620 args |= (MIPS16_EXTEND | (nsreg << 24) | (amask << 16)
5621 | ((frame_size & 0xf0) << 16));
5622 return args;
5623}
5624
364215c8
RS
5625/* OP_SAVE_RESTORE_LIST matcher. */
5626
a92713e6
RS
5627static bfd_boolean
5628match_save_restore_list_operand (struct mips_arg_info *arg)
364215c8
RS
5629{
5630 unsigned int opcode, args, statics, sregs;
5631 unsigned int num_frame_sizes, num_args, num_statics, num_sregs;
38bf472a 5632 unsigned int arg_mask, ra, s0, s1;
364215c8 5633 offsetT frame_size;
364215c8 5634
364215c8
RS
5635 opcode = arg->insn->insn_opcode;
5636 frame_size = 0;
5637 num_frame_sizes = 0;
5638 args = 0;
5639 statics = 0;
5640 sregs = 0;
38bf472a
MR
5641 ra = 0;
5642 s0 = 0;
5643 s1 = 0;
a92713e6 5644 do
364215c8
RS
5645 {
5646 unsigned int regno1, regno2;
5647
a92713e6 5648 if (arg->token->type == OT_INTEGER)
364215c8
RS
5649 {
5650 /* Handle the frame size. */
1a00e612 5651 if (!match_const_int (arg, &frame_size))
a92713e6 5652 return FALSE;
364215c8 5653 num_frame_sizes += 1;
364215c8
RS
5654 }
5655 else
5656 {
a92713e6
RS
5657 if (!match_reg_range (arg, OP_REG_GP, &regno1, &regno2))
5658 return FALSE;
364215c8
RS
5659
5660 while (regno1 <= regno2)
5661 {
5662 if (regno1 >= 4 && regno1 <= 7)
5663 {
5664 if (num_frame_sizes == 0)
5665 /* args $a0-$a3 */
5666 args |= 1 << (regno1 - 4);
5667 else
5668 /* statics $a0-$a3 */
5669 statics |= 1 << (regno1 - 4);
5670 }
5671 else if (regno1 >= 16 && regno1 <= 23)
5672 /* $s0-$s7 */
5673 sregs |= 1 << (regno1 - 16);
5674 else if (regno1 == 30)
5675 /* $s8 */
5676 sregs |= 1 << 8;
5677 else if (regno1 == 31)
5678 /* Add $ra to insn. */
38bf472a 5679 ra = 1;
364215c8 5680 else
a92713e6 5681 return FALSE;
364215c8
RS
5682 regno1 += 1;
5683 if (regno1 == 24)
5684 regno1 = 30;
5685 }
5686 }
364215c8 5687 }
a92713e6 5688 while (match_char (arg, ','));
364215c8
RS
5689
5690 /* Encode args/statics combination. */
5691 if (args & statics)
a92713e6 5692 return FALSE;
364215c8
RS
5693 else if (args == 0xf)
5694 /* All $a0-$a3 are args. */
38bf472a 5695 arg_mask = MIPS_SVRS_ALL_ARGS;
364215c8
RS
5696 else if (statics == 0xf)
5697 /* All $a0-$a3 are statics. */
38bf472a 5698 arg_mask = MIPS_SVRS_ALL_STATICS;
364215c8
RS
5699 else
5700 {
5701 /* Count arg registers. */
5702 num_args = 0;
5703 while (args & 0x1)
5704 {
5705 args >>= 1;
5706 num_args += 1;
5707 }
5708 if (args != 0)
a92713e6 5709 return FALSE;
364215c8
RS
5710
5711 /* Count static registers. */
5712 num_statics = 0;
5713 while (statics & 0x8)
5714 {
5715 statics = (statics << 1) & 0xf;
5716 num_statics += 1;
5717 }
5718 if (statics != 0)
a92713e6 5719 return FALSE;
364215c8
RS
5720
5721 /* Encode args/statics. */
38bf472a 5722 arg_mask = (num_args << 2) | num_statics;
364215c8
RS
5723 }
5724
5725 /* Encode $s0/$s1. */
5726 if (sregs & (1 << 0)) /* $s0 */
38bf472a 5727 s0 = 1;
364215c8 5728 if (sregs & (1 << 1)) /* $s1 */
38bf472a 5729 s1 = 1;
364215c8
RS
5730 sregs >>= 2;
5731
5732 /* Encode $s2-$s8. */
5733 num_sregs = 0;
5734 while (sregs & 1)
5735 {
5736 sregs >>= 1;
5737 num_sregs += 1;
5738 }
5739 if (sregs != 0)
a92713e6 5740 return FALSE;
364215c8
RS
5741
5742 /* Encode frame size. */
5743 if (num_frame_sizes == 0)
1a00e612
RS
5744 {
5745 set_insn_error (arg->argnum, _("missing frame size"));
5746 return FALSE;
5747 }
5748 if (num_frame_sizes > 1)
5749 {
5750 set_insn_error (arg->argnum, _("frame size specified twice"));
5751 return FALSE;
5752 }
5753 if ((frame_size & 7) != 0 || frame_size < 0 || frame_size > 0xff * 8)
5754 {
5755 set_insn_error (arg->argnum, _("invalid frame size"));
5756 return FALSE;
5757 }
38bf472a 5758 frame_size /= 8;
364215c8 5759
364215c8 5760 /* Finally build the instruction. */
38bf472a
MR
5761 if (mips_opts.mips16)
5762 opcode |= mips16_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5763 frame_size);
5764 else if (!mips_opts.micromips)
5765 opcode |= mips_encode_save_restore (arg_mask, num_sregs, ra, s0, s1,
5766 frame_size);
5767 else
5768 abort ();
5769
364215c8 5770 arg->insn->insn_opcode = opcode;
a92713e6 5771 return TRUE;
364215c8
RS
5772}
5773
a1d78564
RS
5774/* OP_MDMX_IMM_REG matcher. */
5775
a92713e6 5776static bfd_boolean
a1d78564 5777match_mdmx_imm_reg_operand (struct mips_arg_info *arg,
a92713e6 5778 const struct mips_operand *operand)
a1d78564 5779{
a92713e6 5780 unsigned int regno, uval;
a1d78564
RS
5781 bfd_boolean is_qh;
5782 const struct mips_opcode *opcode;
5783
5784 /* The mips_opcode records whether this is an octobyte or quadhalf
5785 instruction. Start out with that bit in place. */
5786 opcode = arg->insn->insn_mo;
5787 uval = mips_extract_operand (operand, opcode->match);
5788 is_qh = (uval != 0);
5789
56d438b1 5790 if (arg->token->type == OT_REG)
a1d78564
RS
5791 {
5792 if ((opcode->membership & INSN_5400)
5793 && strcmp (opcode->name, "rzu.ob") == 0)
5794 {
1a00e612
RS
5795 set_insn_error_i (arg->argnum, _("operand %d must be an immediate"),
5796 arg->argnum);
5797 return FALSE;
a1d78564
RS
5798 }
5799
56d438b1
CF
5800 if (!match_regno (arg, OP_REG_VEC, arg->token->u.regno, &regno))
5801 return FALSE;
5802 ++arg->token;
5803
a1d78564
RS
5804 /* Check whether this is a vector register or a broadcast of
5805 a single element. */
56d438b1 5806 if (arg->token->type == OT_INTEGER_INDEX)
a1d78564 5807 {
56d438b1 5808 if (arg->token->u.index > (is_qh ? 3 : 7))
a1d78564 5809 {
1a00e612
RS
5810 set_insn_error (arg->argnum, _("invalid element selector"));
5811 return FALSE;
a1d78564 5812 }
56d438b1
CF
5813 uval |= arg->token->u.index << (is_qh ? 2 : 1) << 5;
5814 ++arg->token;
a1d78564
RS
5815 }
5816 else
5817 {
5818 /* A full vector. */
5819 if ((opcode->membership & INSN_5400)
5820 && (strcmp (opcode->name, "sll.ob") == 0
5821 || strcmp (opcode->name, "srl.ob") == 0))
5822 {
1a00e612
RS
5823 set_insn_error_i (arg->argnum, _("operand %d must be scalar"),
5824 arg->argnum);
5825 return FALSE;
a1d78564
RS
5826 }
5827
5828 if (is_qh)
5829 uval |= MDMX_FMTSEL_VEC_QH << 5;
5830 else
5831 uval |= MDMX_FMTSEL_VEC_OB << 5;
5832 }
a1d78564
RS
5833 uval |= regno;
5834 }
5835 else
5836 {
5837 offsetT sval;
5838
1a00e612 5839 if (!match_const_int (arg, &sval))
a92713e6 5840 return FALSE;
a1d78564
RS
5841 if (sval < 0 || sval > 31)
5842 {
1a00e612
RS
5843 match_out_of_range (arg);
5844 return FALSE;
a1d78564
RS
5845 }
5846 uval |= (sval & 31);
5847 if (is_qh)
5848 uval |= MDMX_FMTSEL_IMM_QH << 5;
5849 else
5850 uval |= MDMX_FMTSEL_IMM_OB << 5;
5851 }
5852 insn_insert_operand (arg->insn, operand, uval);
a92713e6 5853 return TRUE;
a1d78564
RS
5854}
5855
56d438b1
CF
5856/* OP_IMM_INDEX matcher. */
5857
5858static bfd_boolean
5859match_imm_index_operand (struct mips_arg_info *arg,
5860 const struct mips_operand *operand)
5861{
5862 unsigned int max_val;
5863
5864 if (arg->token->type != OT_INTEGER_INDEX)
5865 return FALSE;
5866
5867 max_val = (1 << operand->size) - 1;
5868 if (arg->token->u.index > max_val)
5869 {
5870 match_out_of_range (arg);
5871 return FALSE;
5872 }
5873 insn_insert_operand (arg->insn, operand, arg->token->u.index);
5874 ++arg->token;
5875 return TRUE;
5876}
5877
5878/* OP_REG_INDEX matcher. */
5879
5880static bfd_boolean
5881match_reg_index_operand (struct mips_arg_info *arg,
5882 const struct mips_operand *operand)
5883{
5884 unsigned int regno;
5885
5886 if (arg->token->type != OT_REG_INDEX)
5887 return FALSE;
5888
5889 if (!match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno))
5890 return FALSE;
5891
5892 insn_insert_operand (arg->insn, operand, regno);
5893 ++arg->token;
5894 return TRUE;
5895}
5896
a1d78564
RS
5897/* OP_PC matcher. */
5898
a92713e6
RS
5899static bfd_boolean
5900match_pc_operand (struct mips_arg_info *arg)
a1d78564 5901{
a92713e6
RS
5902 if (arg->token->type == OT_REG && (arg->token->u.regno & RTYPE_PC))
5903 {
5904 ++arg->token;
5905 return TRUE;
5906 }
5907 return FALSE;
a1d78564
RS
5908}
5909
25499ac7
MR
5910/* OP_REG28 matcher. */
5911
5912static bfd_boolean
5913match_reg28_operand (struct mips_arg_info *arg)
5914{
5915 unsigned int regno;
5916
5917 if (arg->token->type == OT_REG
5918 && match_regno (arg, OP_REG_GP, arg->token->u.regno, &regno)
5919 && regno == GP)
5920 {
5921 ++arg->token;
5922 return TRUE;
5923 }
5924 return FALSE;
5925}
5926
7361da2c
AB
5927/* OP_NON_ZERO_REG matcher. */
5928
5929static bfd_boolean
5930match_non_zero_reg_operand (struct mips_arg_info *arg,
5931 const struct mips_operand *operand)
5932{
5933 unsigned int regno;
5934
5935 if (!match_reg (arg, OP_REG_GP, &regno))
5936 return FALSE;
5937
5938 if (regno == 0)
5939 return FALSE;
5940
5941 arg->last_regno = regno;
5942 insn_insert_operand (arg->insn, operand, regno);
5943 return TRUE;
5944}
5945
a1d78564
RS
5946/* OP_REPEAT_DEST_REG and OP_REPEAT_PREV_REG matcher. OTHER_REGNO is the
5947 register that we need to match. */
5948
a92713e6
RS
5949static bfd_boolean
5950match_tied_reg_operand (struct mips_arg_info *arg, unsigned int other_regno)
a1d78564
RS
5951{
5952 unsigned int regno;
5953
a92713e6 5954 return match_reg (arg, OP_REG_GP, &regno) && regno == other_regno;
a1d78564
RS
5955}
5956
33f46696
MR
5957/* Try to match a floating-point constant from ARG for LI.S or LI.D.
5958 LENGTH is the length of the value in bytes (4 for float, 8 for double)
5959 and USING_GPRS says whether the destination is a GPR rather than an FPR.
89565f1b
RS
5960
5961 Return the constant in IMM and OFFSET as follows:
5962
5963 - If the constant should be loaded via memory, set IMM to O_absent and
5964 OFFSET to the memory address.
5965
5966 - Otherwise, if the constant should be loaded into two 32-bit registers,
5967 set IMM to the O_constant to load into the high register and OFFSET
5968 to the corresponding value for the low register.
5969
5970 - Otherwise, set IMM to the full O_constant and set OFFSET to O_absent.
5971
5972 These constants only appear as the last operand in an instruction,
5973 and every instruction that accepts them in any variant accepts them
5974 in all variants. This means we don't have to worry about backing out
5975 any changes if the instruction does not match. We just match
5976 unconditionally and report an error if the constant is invalid. */
5977
a92713e6
RS
5978static bfd_boolean
5979match_float_constant (struct mips_arg_info *arg, expressionS *imm,
5980 expressionS *offset, int length, bfd_boolean using_gprs)
89565f1b 5981{
a92713e6 5982 char *p;
89565f1b
RS
5983 segT seg, new_seg;
5984 subsegT subseg;
5985 const char *newname;
a92713e6 5986 unsigned char *data;
89565f1b
RS
5987
5988 /* Where the constant is placed is based on how the MIPS assembler
5989 does things:
5990
5991 length == 4 && using_gprs -- immediate value only
5992 length == 8 && using_gprs -- .rdata or immediate value
5993 length == 4 && !using_gprs -- .lit4 or immediate value
5994 length == 8 && !using_gprs -- .lit8 or immediate value
5995
5996 The .lit4 and .lit8 sections are only used if permitted by the
5997 -G argument. */
a92713e6 5998 if (arg->token->type != OT_FLOAT)
1a00e612
RS
5999 {
6000 set_insn_error (arg->argnum, _("floating-point expression required"));
6001 return FALSE;
6002 }
a92713e6
RS
6003
6004 gas_assert (arg->token->u.flt.length == length);
6005 data = arg->token->u.flt.data;
6006 ++arg->token;
89565f1b
RS
6007
6008 /* Handle 32-bit constants for which an immediate value is best. */
6009 if (length == 4
6010 && (using_gprs
6011 || g_switch_value < 4
6012 || (data[0] == 0 && data[1] == 0)
6013 || (data[2] == 0 && data[3] == 0)))
6014 {
6015 imm->X_op = O_constant;
6016 if (!target_big_endian)
6017 imm->X_add_number = bfd_getl32 (data);
6018 else
6019 imm->X_add_number = bfd_getb32 (data);
6020 offset->X_op = O_absent;
a92713e6 6021 return TRUE;
89565f1b
RS
6022 }
6023
6024 /* Handle 64-bit constants for which an immediate value is best. */
6025 if (length == 8
6026 && !mips_disable_float_construction
351cdf24
MF
6027 /* Constants can only be constructed in GPRs and copied to FPRs if the
6028 GPRs are at least as wide as the FPRs or MTHC1 is available.
6029 Unlike most tests for 32-bit floating-point registers this check
6030 specifically looks for GPR_SIZE == 32 as the FPXX ABI does not
6031 permit 64-bit moves without MXHC1.
6032 Force the constant into memory otherwise. */
6033 && (using_gprs
6034 || GPR_SIZE == 64
6035 || ISA_HAS_MXHC1 (mips_opts.isa)
6036 || FPR_SIZE == 32)
89565f1b
RS
6037 && ((data[0] == 0 && data[1] == 0)
6038 || (data[2] == 0 && data[3] == 0))
6039 && ((data[4] == 0 && data[5] == 0)
6040 || (data[6] == 0 && data[7] == 0)))
6041 {
6042 /* The value is simple enough to load with a couple of instructions.
6043 If using 32-bit registers, set IMM to the high order 32 bits and
6044 OFFSET to the low order 32 bits. Otherwise, set IMM to the entire
6045 64 bit constant. */
351cdf24 6046 if (GPR_SIZE == 32 || (!using_gprs && FPR_SIZE != 64))
89565f1b
RS
6047 {
6048 imm->X_op = O_constant;
6049 offset->X_op = O_constant;
6050 if (!target_big_endian)
6051 {
6052 imm->X_add_number = bfd_getl32 (data + 4);
6053 offset->X_add_number = bfd_getl32 (data);
6054 }
6055 else
6056 {
6057 imm->X_add_number = bfd_getb32 (data);
6058 offset->X_add_number = bfd_getb32 (data + 4);
6059 }
6060 if (offset->X_add_number == 0)
6061 offset->X_op = O_absent;
6062 }
6063 else
6064 {
6065 imm->X_op = O_constant;
6066 if (!target_big_endian)
6067 imm->X_add_number = bfd_getl64 (data);
6068 else
6069 imm->X_add_number = bfd_getb64 (data);
6070 offset->X_op = O_absent;
6071 }
a92713e6 6072 return TRUE;
89565f1b
RS
6073 }
6074
6075 /* Switch to the right section. */
6076 seg = now_seg;
6077 subseg = now_subseg;
6078 if (length == 4)
6079 {
6080 gas_assert (!using_gprs && g_switch_value >= 4);
6081 newname = ".lit4";
6082 }
6083 else
6084 {
6085 if (using_gprs || g_switch_value < 8)
6086 newname = RDATA_SECTION_NAME;
6087 else
6088 newname = ".lit8";
6089 }
6090
6091 new_seg = subseg_new (newname, (subsegT) 0);
6092 bfd_set_section_flags (stdoutput, new_seg,
6093 SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_DATA);
6094 frag_align (length == 4 ? 2 : 3, 0, 0);
6095 if (strncmp (TARGET_OS, "elf", 3) != 0)
6096 record_alignment (new_seg, 4);
6097 else
6098 record_alignment (new_seg, length == 4 ? 2 : 3);
6099 if (seg == now_seg)
1661c76c 6100 as_bad (_("cannot use `%s' in this section"), arg->insn->insn_mo->name);
89565f1b
RS
6101
6102 /* Set the argument to the current address in the section. */
6103 imm->X_op = O_absent;
6104 offset->X_op = O_symbol;
6105 offset->X_add_symbol = symbol_temp_new_now ();
6106 offset->X_add_number = 0;
6107
6108 /* Put the floating point number into the section. */
6109 p = frag_more (length);
6110 memcpy (p, data, length);
6111
6112 /* Switch back to the original section. */
6113 subseg_set (seg, subseg);
a92713e6 6114 return TRUE;
89565f1b
RS
6115}
6116
14daeee3
RS
6117/* OP_VU0_SUFFIX and OP_VU0_MATCH_SUFFIX matcher; MATCH_P selects between
6118 them. */
6119
6120static bfd_boolean
6121match_vu0_suffix_operand (struct mips_arg_info *arg,
6122 const struct mips_operand *operand,
6123 bfd_boolean match_p)
6124{
6125 unsigned int uval;
6126
6127 /* The operand can be an XYZW mask or a single 2-bit channel index
6128 (with X being 0). */
6129 gas_assert (operand->size == 2 || operand->size == 4);
6130
ee5734f0 6131 /* The suffix can be omitted when it is already part of the opcode. */
14daeee3 6132 if (arg->token->type != OT_CHANNELS)
ee5734f0 6133 return match_p;
14daeee3
RS
6134
6135 uval = arg->token->u.channels;
6136 if (operand->size == 2)
6137 {
6138 /* Check that a single bit is set and convert it into a 2-bit index. */
6139 if ((uval & -uval) != uval)
6140 return FALSE;
6141 uval = 4 - ffs (uval);
6142 }
6143
6144 if (match_p && insn_extract_operand (arg->insn, operand) != uval)
6145 return FALSE;
6146
6147 ++arg->token;
6148 if (!match_p)
6149 insn_insert_operand (arg->insn, operand, uval);
6150 return TRUE;
6151}
6152
33f46696
MR
6153/* Try to match a token from ARG against OPERAND. Consume the token
6154 and return true on success, otherwise return false. */
a1d78564 6155
a92713e6 6156static bfd_boolean
a1d78564 6157match_operand (struct mips_arg_info *arg,
a92713e6 6158 const struct mips_operand *operand)
a1d78564
RS
6159{
6160 switch (operand->type)
6161 {
6162 case OP_INT:
a92713e6 6163 return match_int_operand (arg, operand);
a1d78564
RS
6164
6165 case OP_MAPPED_INT:
a92713e6 6166 return match_mapped_int_operand (arg, operand);
a1d78564
RS
6167
6168 case OP_MSB:
a92713e6 6169 return match_msb_operand (arg, operand);
a1d78564
RS
6170
6171 case OP_REG:
0f35dbc4 6172 case OP_OPTIONAL_REG:
a92713e6 6173 return match_reg_operand (arg, operand);
a1d78564
RS
6174
6175 case OP_REG_PAIR:
a92713e6 6176 return match_reg_pair_operand (arg, operand);
a1d78564
RS
6177
6178 case OP_PCREL:
a92713e6 6179 return match_pcrel_operand (arg);
a1d78564
RS
6180
6181 case OP_PERF_REG:
a92713e6 6182 return match_perf_reg_operand (arg, operand);
a1d78564
RS
6183
6184 case OP_ADDIUSP_INT:
a92713e6 6185 return match_addiusp_operand (arg, operand);
a1d78564
RS
6186
6187 case OP_CLO_CLZ_DEST:
a92713e6 6188 return match_clo_clz_dest_operand (arg, operand);
a1d78564
RS
6189
6190 case OP_LWM_SWM_LIST:
a92713e6 6191 return match_lwm_swm_list_operand (arg, operand);
a1d78564
RS
6192
6193 case OP_ENTRY_EXIT_LIST:
a92713e6 6194 return match_entry_exit_operand (arg, operand);
364215c8 6195
a1d78564 6196 case OP_SAVE_RESTORE_LIST:
a92713e6 6197 return match_save_restore_list_operand (arg);
a1d78564
RS
6198
6199 case OP_MDMX_IMM_REG:
a92713e6 6200 return match_mdmx_imm_reg_operand (arg, operand);
a1d78564
RS
6201
6202 case OP_REPEAT_DEST_REG:
a92713e6 6203 return match_tied_reg_operand (arg, arg->dest_regno);
a1d78564
RS
6204
6205 case OP_REPEAT_PREV_REG:
a92713e6 6206 return match_tied_reg_operand (arg, arg->last_regno);
a1d78564
RS
6207
6208 case OP_PC:
a92713e6 6209 return match_pc_operand (arg);
14daeee3 6210
25499ac7
MR
6211 case OP_REG28:
6212 return match_reg28_operand (arg);
6213
14daeee3
RS
6214 case OP_VU0_SUFFIX:
6215 return match_vu0_suffix_operand (arg, operand, FALSE);
6216
6217 case OP_VU0_MATCH_SUFFIX:
6218 return match_vu0_suffix_operand (arg, operand, TRUE);
56d438b1
CF
6219
6220 case OP_IMM_INDEX:
6221 return match_imm_index_operand (arg, operand);
6222
6223 case OP_REG_INDEX:
6224 return match_reg_index_operand (arg, operand);
7361da2c
AB
6225
6226 case OP_SAME_RS_RT:
6227 return match_same_rs_rt_operand (arg, operand);
6228
6229 case OP_CHECK_PREV:
6230 return match_check_prev_operand (arg, operand);
6231
6232 case OP_NON_ZERO_REG:
6233 return match_non_zero_reg_operand (arg, operand);
a1d78564
RS
6234 }
6235 abort ();
6236}
6237
6238/* ARG is the state after successfully matching an instruction.
6239 Issue any queued-up warnings. */
6240
6241static void
6242check_completed_insn (struct mips_arg_info *arg)
6243{
6244 if (arg->seen_at)
6245 {
6246 if (AT == ATREG)
1661c76c 6247 as_warn (_("used $at without \".set noat\""));
a1d78564 6248 else
1661c76c 6249 as_warn (_("used $%u with \".set at=$%u\""), AT, AT);
a1d78564
RS
6250 }
6251}
a1d78564 6252
85fcb30f
RS
6253/* Return true if modifying general-purpose register REG needs a delay. */
6254
6255static bfd_boolean
6256reg_needs_delay (unsigned int reg)
6257{
6258 unsigned long prev_pinfo;
6259
6260 prev_pinfo = history[0].insn_mo->pinfo;
6261 if (!mips_opts.noreorder
67dc82bc 6262 && (((prev_pinfo & INSN_LOAD_MEMORY) && !gpr_interlocks)
43885403 6263 || ((prev_pinfo & INSN_LOAD_COPROC) && !cop_interlocks))
85fcb30f
RS
6264 && (gpr_write_mask (&history[0]) & (1 << reg)))
6265 return TRUE;
6266
6267 return FALSE;
6268}
6269
71400594
RS
6270/* Classify an instruction according to the FIX_VR4120_* enumeration.
6271 Return NUM_FIX_VR4120_CLASSES if the instruction isn't affected
6272 by VR4120 errata. */
4d7206a2 6273
71400594
RS
6274static unsigned int
6275classify_vr4120_insn (const char *name)
252b5132 6276{
71400594
RS
6277 if (strncmp (name, "macc", 4) == 0)
6278 return FIX_VR4120_MACC;
6279 if (strncmp (name, "dmacc", 5) == 0)
6280 return FIX_VR4120_DMACC;
6281 if (strncmp (name, "mult", 4) == 0)
6282 return FIX_VR4120_MULT;
6283 if (strncmp (name, "dmult", 5) == 0)
6284 return FIX_VR4120_DMULT;
6285 if (strstr (name, "div"))
6286 return FIX_VR4120_DIV;
6287 if (strcmp (name, "mtlo") == 0 || strcmp (name, "mthi") == 0)
6288 return FIX_VR4120_MTHILO;
6289 return NUM_FIX_VR4120_CLASSES;
6290}
252b5132 6291
a8d14a88
CM
6292#define INSN_ERET 0x42000018
6293#define INSN_DERET 0x4200001f
6294#define INSN_DMULT 0x1c
6295#define INSN_DMULTU 0x1d
ff239038 6296
71400594
RS
6297/* Return the number of instructions that must separate INSN1 and INSN2,
6298 where INSN1 is the earlier instruction. Return the worst-case value
6299 for any INSN2 if INSN2 is null. */
252b5132 6300
71400594
RS
6301static unsigned int
6302insns_between (const struct mips_cl_insn *insn1,
6303 const struct mips_cl_insn *insn2)
6304{
6305 unsigned long pinfo1, pinfo2;
4c260379 6306 unsigned int mask;
71400594 6307
85fcb30f
RS
6308 /* If INFO2 is null, pessimistically assume that all flags are set for
6309 the second instruction. */
71400594
RS
6310 pinfo1 = insn1->insn_mo->pinfo;
6311 pinfo2 = insn2 ? insn2->insn_mo->pinfo : ~0U;
252b5132 6312
71400594
RS
6313 /* For most targets, write-after-read dependencies on the HI and LO
6314 registers must be separated by at least two instructions. */
6315 if (!hilo_interlocks)
252b5132 6316 {
71400594
RS
6317 if ((pinfo1 & INSN_READ_LO) && (pinfo2 & INSN_WRITE_LO))
6318 return 2;
6319 if ((pinfo1 & INSN_READ_HI) && (pinfo2 & INSN_WRITE_HI))
6320 return 2;
6321 }
6322
6323 /* If we're working around r7000 errata, there must be two instructions
6324 between an mfhi or mflo and any instruction that uses the result. */
6325 if (mips_7000_hilo_fix
df58fc94 6326 && !mips_opts.micromips
71400594 6327 && MF_HILO_INSN (pinfo1)
85fcb30f 6328 && (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1))))
71400594
RS
6329 return 2;
6330
ff239038
CM
6331 /* If we're working around 24K errata, one instruction is required
6332 if an ERET or DERET is followed by a branch instruction. */
df58fc94 6333 if (mips_fix_24k && !mips_opts.micromips)
ff239038
CM
6334 {
6335 if (insn1->insn_opcode == INSN_ERET
6336 || insn1->insn_opcode == INSN_DERET)
6337 {
6338 if (insn2 == NULL
6339 || insn2->insn_opcode == INSN_ERET
6340 || insn2->insn_opcode == INSN_DERET
11625dd8 6341 || delayed_branch_p (insn2))
ff239038
CM
6342 return 1;
6343 }
6344 }
6345
a8d14a88
CM
6346 /* If we're working around PMC RM7000 errata, there must be three
6347 nops between a dmult and a load instruction. */
6348 if (mips_fix_rm7000 && !mips_opts.micromips)
6349 {
6350 if ((insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULT
6351 || (insn1->insn_opcode & insn1->insn_mo->mask) == INSN_DMULTU)
6352 {
6353 if (pinfo2 & INSN_LOAD_MEMORY)
6354 return 3;
6355 }
6356 }
6357
71400594
RS
6358 /* If working around VR4120 errata, check for combinations that need
6359 a single intervening instruction. */
df58fc94 6360 if (mips_fix_vr4120 && !mips_opts.micromips)
71400594
RS
6361 {
6362 unsigned int class1, class2;
252b5132 6363
71400594
RS
6364 class1 = classify_vr4120_insn (insn1->insn_mo->name);
6365 if (class1 != NUM_FIX_VR4120_CLASSES && vr4120_conflicts[class1] != 0)
252b5132 6366 {
71400594
RS
6367 if (insn2 == NULL)
6368 return 1;
6369 class2 = classify_vr4120_insn (insn2->insn_mo->name);
6370 if (vr4120_conflicts[class1] & (1 << class2))
6371 return 1;
252b5132 6372 }
71400594
RS
6373 }
6374
df58fc94 6375 if (!HAVE_CODE_COMPRESSION)
71400594
RS
6376 {
6377 /* Check for GPR or coprocessor load delays. All such delays
6378 are on the RT register. */
6379 /* Itbl support may require additional care here. */
67dc82bc 6380 if ((!gpr_interlocks && (pinfo1 & INSN_LOAD_MEMORY))
43885403 6381 || (!cop_interlocks && (pinfo1 & INSN_LOAD_COPROC)))
252b5132 6382 {
85fcb30f 6383 if (insn2 == NULL || (gpr_read_mask (insn2) & gpr_write_mask (insn1)))
71400594
RS
6384 return 1;
6385 }
6386
6387 /* Check for generic coprocessor hazards.
6388
6389 This case is not handled very well. There is no special
6390 knowledge of CP0 handling, and the coprocessors other than
6391 the floating point unit are not distinguished at all. */
6392 /* Itbl support may require additional care here. FIXME!
6393 Need to modify this to include knowledge about
6394 user specified delays! */
43885403 6395 else if ((!cop_interlocks && (pinfo1 & INSN_COPROC_MOVE))
71400594
RS
6396 || (!cop_mem_interlocks && (pinfo1 & INSN_COPROC_MEMORY_DELAY)))
6397 {
6398 /* Handle cases where INSN1 writes to a known general coprocessor
6399 register. There must be a one instruction delay before INSN2
6400 if INSN2 reads that register, otherwise no delay is needed. */
4c260379
RS
6401 mask = fpr_write_mask (insn1);
6402 if (mask != 0)
252b5132 6403 {
4c260379 6404 if (!insn2 || (mask & fpr_read_mask (insn2)) != 0)
71400594 6405 return 1;
252b5132
RH
6406 }
6407 else
6408 {
71400594
RS
6409 /* Read-after-write dependencies on the control registers
6410 require a two-instruction gap. */
6411 if ((pinfo1 & INSN_WRITE_COND_CODE)
6412 && (pinfo2 & INSN_READ_COND_CODE))
6413 return 2;
6414
6415 /* We don't know exactly what INSN1 does. If INSN2 is
6416 also a coprocessor instruction, assume there must be
6417 a one instruction gap. */
6418 if (pinfo2 & INSN_COP)
6419 return 1;
252b5132
RH
6420 }
6421 }
6b76fefe 6422
71400594
RS
6423 /* Check for read-after-write dependencies on the coprocessor
6424 control registers in cases where INSN1 does not need a general
6425 coprocessor delay. This means that INSN1 is a floating point
6426 comparison instruction. */
6427 /* Itbl support may require additional care here. */
6428 else if (!cop_interlocks
6429 && (pinfo1 & INSN_WRITE_COND_CODE)
6430 && (pinfo2 & INSN_READ_COND_CODE))
6431 return 1;
6432 }
6b76fefe 6433
7361da2c
AB
6434 /* Forbidden slots can not contain Control Transfer Instructions (CTIs)
6435 CTIs include all branches and jumps, nal, eret, eretnc, deret, wait
6436 and pause. */
6437 if ((insn1->insn_mo->pinfo2 & INSN2_FORBIDDEN_SLOT)
6438 && ((pinfo2 & INSN_NO_DELAY_SLOT)
6439 || (insn2 && delayed_branch_p (insn2))))
6440 return 1;
6441
71400594
RS
6442 return 0;
6443}
6b76fefe 6444
7d8e00cf
RS
6445/* Return the number of nops that would be needed to work around the
6446 VR4130 mflo/mfhi errata if instruction INSN immediately followed
932d1a1b
RS
6447 the MAX_VR4130_NOPS instructions described by HIST. Ignore hazards
6448 that are contained within the first IGNORE instructions of HIST. */
7d8e00cf
RS
6449
6450static int
932d1a1b 6451nops_for_vr4130 (int ignore, const struct mips_cl_insn *hist,
7d8e00cf
RS
6452 const struct mips_cl_insn *insn)
6453{
4c260379
RS
6454 int i, j;
6455 unsigned int mask;
7d8e00cf
RS
6456
6457 /* Check if the instruction writes to HI or LO. MTHI and MTLO
6458 are not affected by the errata. */
6459 if (insn != 0
6460 && ((insn->insn_mo->pinfo & (INSN_WRITE_HI | INSN_WRITE_LO)) == 0
6461 || strcmp (insn->insn_mo->name, "mtlo") == 0
6462 || strcmp (insn->insn_mo->name, "mthi") == 0))
6463 return 0;
6464
6465 /* Search for the first MFLO or MFHI. */
6466 for (i = 0; i < MAX_VR4130_NOPS; i++)
91d6fa6a 6467 if (MF_HILO_INSN (hist[i].insn_mo->pinfo))
7d8e00cf
RS
6468 {
6469 /* Extract the destination register. */
4c260379 6470 mask = gpr_write_mask (&hist[i]);
7d8e00cf
RS
6471
6472 /* No nops are needed if INSN reads that register. */
4c260379 6473 if (insn != NULL && (gpr_read_mask (insn) & mask) != 0)
7d8e00cf
RS
6474 return 0;
6475
6476 /* ...or if any of the intervening instructions do. */
6477 for (j = 0; j < i; j++)
4c260379 6478 if (gpr_read_mask (&hist[j]) & mask)
7d8e00cf
RS
6479 return 0;
6480
932d1a1b
RS
6481 if (i >= ignore)
6482 return MAX_VR4130_NOPS - i;
7d8e00cf
RS
6483 }
6484 return 0;
6485}
6486
134c0c8b
MR
6487#define BASE_REG_EQ(INSN1, INSN2) \
6488 ((((INSN1) >> OP_SH_RS) & OP_MASK_RS) \
15be625d
CM
6489 == (((INSN2) >> OP_SH_RS) & OP_MASK_RS))
6490
6491/* Return the minimum alignment for this store instruction. */
6492
6493static int
6494fix_24k_align_to (const struct mips_opcode *mo)
6495{
6496 if (strcmp (mo->name, "sh") == 0)
6497 return 2;
6498
6499 if (strcmp (mo->name, "swc1") == 0
6500 || strcmp (mo->name, "swc2") == 0
6501 || strcmp (mo->name, "sw") == 0
6502 || strcmp (mo->name, "sc") == 0
6503 || strcmp (mo->name, "s.s") == 0)
6504 return 4;
6505
6506 if (strcmp (mo->name, "sdc1") == 0
6507 || strcmp (mo->name, "sdc2") == 0
6508 || strcmp (mo->name, "s.d") == 0)
6509 return 8;
6510
6511 /* sb, swl, swr */
6512 return 1;
6513}
6514
6515struct fix_24k_store_info
6516 {
6517 /* Immediate offset, if any, for this store instruction. */
6518 short off;
6519 /* Alignment required by this store instruction. */
6520 int align_to;
6521 /* True for register offsets. */
6522 int register_offset;
6523 };
6524
6525/* Comparison function used by qsort. */
6526
6527static int
6528fix_24k_sort (const void *a, const void *b)
6529{
6530 const struct fix_24k_store_info *pos1 = a;
6531 const struct fix_24k_store_info *pos2 = b;
6532
6533 return (pos1->off - pos2->off);
6534}
6535
6536/* INSN is a store instruction. Try to record the store information
6537 in STINFO. Return false if the information isn't known. */
6538
6539static bfd_boolean
6540fix_24k_record_store_info (struct fix_24k_store_info *stinfo,
ab9794cf 6541 const struct mips_cl_insn *insn)
15be625d
CM
6542{
6543 /* The instruction must have a known offset. */
6544 if (!insn->complete_p || !strstr (insn->insn_mo->args, "o("))
6545 return FALSE;
6546
6547 stinfo->off = (insn->insn_opcode >> OP_SH_IMMEDIATE) & OP_MASK_IMMEDIATE;
6548 stinfo->align_to = fix_24k_align_to (insn->insn_mo);
6549 return TRUE;
6550}
6551
932d1a1b
RS
6552/* Return the number of nops that would be needed to work around the 24k
6553 "lost data on stores during refill" errata if instruction INSN
6554 immediately followed the 2 instructions described by HIST.
6555 Ignore hazards that are contained within the first IGNORE
6556 instructions of HIST.
6557
6558 Problem: The FSB (fetch store buffer) acts as an intermediate buffer
6559 for the data cache refills and store data. The following describes
6560 the scenario where the store data could be lost.
6561
6562 * A data cache miss, due to either a load or a store, causing fill
6563 data to be supplied by the memory subsystem
6564 * The first three doublewords of fill data are returned and written
6565 into the cache
6566 * A sequence of four stores occurs in consecutive cycles around the
6567 final doubleword of the fill:
6568 * Store A
6569 * Store B
6570 * Store C
6571 * Zero, One or more instructions
6572 * Store D
6573
6574 The four stores A-D must be to different doublewords of the line that
6575 is being filled. The fourth instruction in the sequence above permits
6576 the fill of the final doubleword to be transferred from the FSB into
6577 the cache. In the sequence above, the stores may be either integer
6578 (sb, sh, sw, swr, swl, sc) or coprocessor (swc1/swc2, sdc1/sdc2,
6579 swxc1, sdxc1, suxc1) stores, as long as the four stores are to
6580 different doublewords on the line. If the floating point unit is
6581 running in 1:2 mode, it is not possible to create the sequence above
6582 using only floating point store instructions.
15be625d
CM
6583
6584 In this case, the cache line being filled is incorrectly marked
6585 invalid, thereby losing the data from any store to the line that
6586 occurs between the original miss and the completion of the five
6587 cycle sequence shown above.
6588
932d1a1b 6589 The workarounds are:
15be625d 6590
932d1a1b
RS
6591 * Run the data cache in write-through mode.
6592 * Insert a non-store instruction between
6593 Store A and Store B or Store B and Store C. */
3739860c 6594
15be625d 6595static int
932d1a1b 6596nops_for_24k (int ignore, const struct mips_cl_insn *hist,
15be625d
CM
6597 const struct mips_cl_insn *insn)
6598{
6599 struct fix_24k_store_info pos[3];
6600 int align, i, base_offset;
6601
932d1a1b
RS
6602 if (ignore >= 2)
6603 return 0;
6604
ab9794cf
RS
6605 /* If the previous instruction wasn't a store, there's nothing to
6606 worry about. */
15be625d
CM
6607 if ((hist[0].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
6608 return 0;
6609
ab9794cf
RS
6610 /* If the instructions after the previous one are unknown, we have
6611 to assume the worst. */
6612 if (!insn)
15be625d
CM
6613 return 1;
6614
ab9794cf
RS
6615 /* Check whether we are dealing with three consecutive stores. */
6616 if ((insn->insn_mo->pinfo & INSN_STORE_MEMORY) == 0
6617 || (hist[1].insn_mo->pinfo & INSN_STORE_MEMORY) == 0)
15be625d
CM
6618 return 0;
6619
6620 /* If we don't know the relationship between the store addresses,
6621 assume the worst. */
ab9794cf 6622 if (!BASE_REG_EQ (insn->insn_opcode, hist[0].insn_opcode)
15be625d
CM
6623 || !BASE_REG_EQ (insn->insn_opcode, hist[1].insn_opcode))
6624 return 1;
6625
6626 if (!fix_24k_record_store_info (&pos[0], insn)
6627 || !fix_24k_record_store_info (&pos[1], &hist[0])
6628 || !fix_24k_record_store_info (&pos[2], &hist[1]))
6629 return 1;
6630
6631 qsort (&pos, 3, sizeof (struct fix_24k_store_info), fix_24k_sort);
6632
6633 /* Pick a value of ALIGN and X such that all offsets are adjusted by
6634 X bytes and such that the base register + X is known to be aligned
6635 to align bytes. */
6636
6637 if (((insn->insn_opcode >> OP_SH_RS) & OP_MASK_RS) == SP)
6638 align = 8;
6639 else
6640 {
6641 align = pos[0].align_to;
6642 base_offset = pos[0].off;
6643 for (i = 1; i < 3; i++)
6644 if (align < pos[i].align_to)
6645 {
6646 align = pos[i].align_to;
6647 base_offset = pos[i].off;
6648 }
6649 for (i = 0; i < 3; i++)
6650 pos[i].off -= base_offset;
6651 }
6652
6653 pos[0].off &= ~align + 1;
6654 pos[1].off &= ~align + 1;
6655 pos[2].off &= ~align + 1;
6656
6657 /* If any two stores write to the same chunk, they also write to the
6658 same doubleword. The offsets are still sorted at this point. */
6659 if (pos[0].off == pos[1].off || pos[1].off == pos[2].off)
6660 return 0;
6661
6662 /* A range of at least 9 bytes is needed for the stores to be in
6663 non-overlapping doublewords. */
6664 if (pos[2].off - pos[0].off <= 8)
6665 return 0;
6666
6667 if (pos[2].off - pos[1].off >= 24
6668 || pos[1].off - pos[0].off >= 24
6669 || pos[2].off - pos[0].off >= 32)
6670 return 0;
6671
6672 return 1;
6673}
6674
71400594 6675/* Return the number of nops that would be needed if instruction INSN
91d6fa6a 6676 immediately followed the MAX_NOPS instructions given by HIST,
932d1a1b
RS
6677 where HIST[0] is the most recent instruction. Ignore hazards
6678 between INSN and the first IGNORE instructions in HIST.
6679
6680 If INSN is null, return the worse-case number of nops for any
6681 instruction. */
bdaaa2e1 6682
71400594 6683static int
932d1a1b 6684nops_for_insn (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6685 const struct mips_cl_insn *insn)
6686{
6687 int i, nops, tmp_nops;
bdaaa2e1 6688
71400594 6689 nops = 0;
932d1a1b 6690 for (i = ignore; i < MAX_DELAY_NOPS; i++)
65b02341 6691 {
91d6fa6a 6692 tmp_nops = insns_between (hist + i, insn) - i;
65b02341
RS
6693 if (tmp_nops > nops)
6694 nops = tmp_nops;
6695 }
7d8e00cf 6696
df58fc94 6697 if (mips_fix_vr4130 && !mips_opts.micromips)
7d8e00cf 6698 {
932d1a1b 6699 tmp_nops = nops_for_vr4130 (ignore, hist, insn);
7d8e00cf
RS
6700 if (tmp_nops > nops)
6701 nops = tmp_nops;
6702 }
6703
df58fc94 6704 if (mips_fix_24k && !mips_opts.micromips)
15be625d 6705 {
932d1a1b 6706 tmp_nops = nops_for_24k (ignore, hist, insn);
15be625d
CM
6707 if (tmp_nops > nops)
6708 nops = tmp_nops;
6709 }
6710
71400594
RS
6711 return nops;
6712}
252b5132 6713
71400594 6714/* The variable arguments provide NUM_INSNS extra instructions that
91d6fa6a 6715 might be added to HIST. Return the largest number of nops that
932d1a1b
RS
6716 would be needed after the extended sequence, ignoring hazards
6717 in the first IGNORE instructions. */
252b5132 6718
71400594 6719static int
932d1a1b
RS
6720nops_for_sequence (int num_insns, int ignore,
6721 const struct mips_cl_insn *hist, ...)
71400594
RS
6722{
6723 va_list args;
6724 struct mips_cl_insn buffer[MAX_NOPS];
6725 struct mips_cl_insn *cursor;
6726 int nops;
6727
91d6fa6a 6728 va_start (args, hist);
71400594 6729 cursor = buffer + num_insns;
91d6fa6a 6730 memcpy (cursor, hist, (MAX_NOPS - num_insns) * sizeof (*cursor));
71400594
RS
6731 while (cursor > buffer)
6732 *--cursor = *va_arg (args, const struct mips_cl_insn *);
6733
932d1a1b 6734 nops = nops_for_insn (ignore, buffer, NULL);
71400594
RS
6735 va_end (args);
6736 return nops;
6737}
252b5132 6738
71400594
RS
6739/* Like nops_for_insn, but if INSN is a branch, take into account the
6740 worst-case delay for the branch target. */
252b5132 6741
71400594 6742static int
932d1a1b 6743nops_for_insn_or_target (int ignore, const struct mips_cl_insn *hist,
71400594
RS
6744 const struct mips_cl_insn *insn)
6745{
6746 int nops, tmp_nops;
60b63b72 6747
932d1a1b 6748 nops = nops_for_insn (ignore, hist, insn);
11625dd8 6749 if (delayed_branch_p (insn))
71400594 6750 {
932d1a1b 6751 tmp_nops = nops_for_sequence (2, ignore ? ignore + 2 : 0,
14fe068b 6752 hist, insn, get_delay_slot_nop (insn));
71400594
RS
6753 if (tmp_nops > nops)
6754 nops = tmp_nops;
6755 }
11625dd8 6756 else if (compact_branch_p (insn))
71400594 6757 {
932d1a1b 6758 tmp_nops = nops_for_sequence (1, ignore ? ignore + 1 : 0, hist, insn);
71400594
RS
6759 if (tmp_nops > nops)
6760 nops = tmp_nops;
6761 }
6762 return nops;
6763}
6764
c67a084a
NC
6765/* Fix NOP issue: Replace nops by "or at,at,zero". */
6766
6767static void
6768fix_loongson2f_nop (struct mips_cl_insn * ip)
6769{
df58fc94 6770 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6771 if (strcmp (ip->insn_mo->name, "nop") == 0)
6772 ip->insn_opcode = LOONGSON2F_NOP_INSN;
6773}
6774
6775/* Fix Jump Issue: Eliminate instruction fetch from outside 256M region
6776 jr target pc &= 'hffff_ffff_cfff_ffff. */
6777
6778static void
6779fix_loongson2f_jump (struct mips_cl_insn * ip)
6780{
df58fc94 6781 gas_assert (!HAVE_CODE_COMPRESSION);
c67a084a
NC
6782 if (strcmp (ip->insn_mo->name, "j") == 0
6783 || strcmp (ip->insn_mo->name, "jr") == 0
6784 || strcmp (ip->insn_mo->name, "jalr") == 0)
6785 {
6786 int sreg;
6787 expressionS ep;
6788
6789 if (! mips_opts.at)
6790 return;
6791
df58fc94 6792 sreg = EXTRACT_OPERAND (0, RS, *ip);
c67a084a
NC
6793 if (sreg == ZERO || sreg == KT0 || sreg == KT1 || sreg == ATREG)
6794 return;
6795
6796 ep.X_op = O_constant;
6797 ep.X_add_number = 0xcfff0000;
6798 macro_build (&ep, "lui", "t,u", ATREG, BFD_RELOC_HI16);
6799 ep.X_add_number = 0xffff;
6800 macro_build (&ep, "ori", "t,r,i", ATREG, ATREG, BFD_RELOC_LO16);
6801 macro_build (NULL, "and", "d,v,t", sreg, sreg, ATREG);
6802 }
6803}
6804
6805static void
6806fix_loongson2f (struct mips_cl_insn * ip)
6807{
6808 if (mips_fix_loongson2f_nop)
6809 fix_loongson2f_nop (ip);
6810
6811 if (mips_fix_loongson2f_jump)
6812 fix_loongson2f_jump (ip);
6813}
6814
a4e06468
RS
6815/* IP is a branch that has a delay slot, and we need to fill it
6816 automatically. Return true if we can do that by swapping IP
e407c74b
NC
6817 with the previous instruction.
6818 ADDRESS_EXPR is an operand of the instruction to be used with
6819 RELOC_TYPE. */
a4e06468
RS
6820
6821static bfd_boolean
e407c74b 6822can_swap_branch_p (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 6823 bfd_reloc_code_real_type *reloc_type)
a4e06468 6824{
2b0c8b40 6825 unsigned long pinfo, pinfo2, prev_pinfo, prev_pinfo2;
a4e06468 6826 unsigned int gpr_read, gpr_write, prev_gpr_read, prev_gpr_write;
9d5de888 6827 unsigned int fpr_read, prev_fpr_write;
a4e06468
RS
6828
6829 /* -O2 and above is required for this optimization. */
6830 if (mips_optimize < 2)
6831 return FALSE;
6832
6833 /* If we have seen .set volatile or .set nomove, don't optimize. */
6834 if (mips_opts.nomove)
6835 return FALSE;
6836
6837 /* We can't swap if the previous instruction's position is fixed. */
6838 if (history[0].fixed_p)
6839 return FALSE;
6840
6841 /* If the previous previous insn was in a .set noreorder, we can't
6842 swap. Actually, the MIPS assembler will swap in this situation.
6843 However, gcc configured -with-gnu-as will generate code like
6844
6845 .set noreorder
6846 lw $4,XXX
6847 .set reorder
6848 INSN
6849 bne $4,$0,foo
6850
6851 in which we can not swap the bne and INSN. If gcc is not configured
6852 -with-gnu-as, it does not output the .set pseudo-ops. */
6853 if (history[1].noreorder_p)
6854 return FALSE;
6855
87333bb7
MR
6856 /* If the previous instruction had a fixup in mips16 mode, we can not swap.
6857 This means that the previous instruction was a 4-byte one anyhow. */
a4e06468
RS
6858 if (mips_opts.mips16 && history[0].fixp[0])
6859 return FALSE;
6860
6861 /* If the branch is itself the target of a branch, we can not swap.
6862 We cheat on this; all we check for is whether there is a label on
6863 this instruction. If there are any branches to anything other than
6864 a label, users must use .set noreorder. */
6865 if (seg_info (now_seg)->label_list)
6866 return FALSE;
6867
6868 /* If the previous instruction is in a variant frag other than this
2309ddf2 6869 branch's one, we cannot do the swap. This does not apply to
9301f9c3
MR
6870 MIPS16 code, which uses variant frags for different purposes. */
6871 if (!mips_opts.mips16
a4e06468
RS
6872 && history[0].frag
6873 && history[0].frag->fr_type == rs_machine_dependent)
6874 return FALSE;
6875
bcd530a7
RS
6876 /* We do not swap with instructions that cannot architecturally
6877 be placed in a branch delay slot, such as SYNC or ERET. We
6878 also refrain from swapping with a trap instruction, since it
6879 complicates trap handlers to have the trap instruction be in
6880 a delay slot. */
a4e06468 6881 prev_pinfo = history[0].insn_mo->pinfo;
bcd530a7 6882 if (prev_pinfo & INSN_NO_DELAY_SLOT)
a4e06468
RS
6883 return FALSE;
6884
6885 /* Check for conflicts between the branch and the instructions
6886 before the candidate delay slot. */
6887 if (nops_for_insn (0, history + 1, ip) > 0)
6888 return FALSE;
6889
6890 /* Check for conflicts between the swapped sequence and the
6891 target of the branch. */
6892 if (nops_for_sequence (2, 0, history + 1, ip, history) > 0)
6893 return FALSE;
6894
6895 /* If the branch reads a register that the previous
6896 instruction sets, we can not swap. */
6897 gpr_read = gpr_read_mask (ip);
6898 prev_gpr_write = gpr_write_mask (&history[0]);
6899 if (gpr_read & prev_gpr_write)
6900 return FALSE;
6901
9d5de888
CF
6902 fpr_read = fpr_read_mask (ip);
6903 prev_fpr_write = fpr_write_mask (&history[0]);
6904 if (fpr_read & prev_fpr_write)
6905 return FALSE;
6906
a4e06468
RS
6907 /* If the branch writes a register that the previous
6908 instruction sets, we can not swap. */
6909 gpr_write = gpr_write_mask (ip);
6910 if (gpr_write & prev_gpr_write)
6911 return FALSE;
6912
6913 /* If the branch writes a register that the previous
6914 instruction reads, we can not swap. */
6915 prev_gpr_read = gpr_read_mask (&history[0]);
6916 if (gpr_write & prev_gpr_read)
6917 return FALSE;
6918
6919 /* If one instruction sets a condition code and the
6920 other one uses a condition code, we can not swap. */
6921 pinfo = ip->insn_mo->pinfo;
6922 if ((pinfo & INSN_READ_COND_CODE)
6923 && (prev_pinfo & INSN_WRITE_COND_CODE))
6924 return FALSE;
6925 if ((pinfo & INSN_WRITE_COND_CODE)
6926 && (prev_pinfo & INSN_READ_COND_CODE))
6927 return FALSE;
6928
6929 /* If the previous instruction uses the PC, we can not swap. */
2b0c8b40 6930 prev_pinfo2 = history[0].insn_mo->pinfo2;
26545944 6931 if (prev_pinfo2 & INSN2_READ_PC)
2b0c8b40 6932 return FALSE;
a4e06468 6933
df58fc94
RS
6934 /* If the previous instruction has an incorrect size for a fixed
6935 branch delay slot in microMIPS mode, we cannot swap. */
2309ddf2
MR
6936 pinfo2 = ip->insn_mo->pinfo2;
6937 if (mips_opts.micromips
6938 && (pinfo2 & INSN2_BRANCH_DELAY_16BIT)
6939 && insn_length (history) != 2)
6940 return FALSE;
6941 if (mips_opts.micromips
6942 && (pinfo2 & INSN2_BRANCH_DELAY_32BIT)
6943 && insn_length (history) != 4)
6944 return FALSE;
6945
e407c74b
NC
6946 /* On R5900 short loops need to be fixed by inserting a nop in
6947 the branch delay slots.
6948 A short loop can be terminated too early. */
6949 if (mips_opts.arch == CPU_R5900
6950 /* Check if instruction has a parameter, ignore "j $31". */
6951 && (address_expr != NULL)
6952 /* Parameter must be 16 bit. */
6953 && (*reloc_type == BFD_RELOC_16_PCREL_S2)
6954 /* Branch to same segment. */
41065f5e 6955 && (S_GET_SEGMENT (address_expr->X_add_symbol) == now_seg)
e407c74b 6956 /* Branch to same code fragment. */
41065f5e 6957 && (symbol_get_frag (address_expr->X_add_symbol) == frag_now)
e407c74b 6958 /* Can only calculate branch offset if value is known. */
41065f5e 6959 && symbol_constant_p (address_expr->X_add_symbol)
e407c74b
NC
6960 /* Check if branch is really conditional. */
6961 && !((ip->insn_opcode & 0xffff0000) == 0x10000000 /* beq $0,$0 */
6962 || (ip->insn_opcode & 0xffff0000) == 0x04010000 /* bgez $0 */
6963 || (ip->insn_opcode & 0xffff0000) == 0x04110000)) /* bgezal $0 */
6964 {
6965 int distance;
6966 /* Check if loop is shorter than 6 instructions including
6967 branch and delay slot. */
41065f5e 6968 distance = frag_now_fix () - S_GET_VALUE (address_expr->X_add_symbol);
e407c74b
NC
6969 if (distance <= 20)
6970 {
6971 int i;
6972 int rv;
6973
6974 rv = FALSE;
6975 /* When the loop includes branches or jumps,
6976 it is not a short loop. */
6977 for (i = 0; i < (distance / 4); i++)
6978 {
6979 if ((history[i].cleared_p)
41065f5e 6980 || delayed_branch_p (&history[i]))
e407c74b
NC
6981 {
6982 rv = TRUE;
6983 break;
6984 }
6985 }
535b785f 6986 if (!rv)
e407c74b
NC
6987 {
6988 /* Insert nop after branch to fix short loop. */
6989 return FALSE;
6990 }
6991 }
6992 }
6993
a4e06468
RS
6994 return TRUE;
6995}
6996
e407c74b
NC
6997/* Decide how we should add IP to the instruction stream.
6998 ADDRESS_EXPR is an operand of the instruction to be used with
6999 RELOC_TYPE. */
a4e06468
RS
7000
7001static enum append_method
e407c74b 7002get_append_method (struct mips_cl_insn *ip, expressionS *address_expr,
26545944 7003 bfd_reloc_code_real_type *reloc_type)
a4e06468 7004{
a4e06468
RS
7005 /* The relaxed version of a macro sequence must be inherently
7006 hazard-free. */
7007 if (mips_relax.sequence == 2)
7008 return APPEND_ADD;
7009
3b821a28 7010 /* We must not dabble with instructions in a ".set noreorder" block. */
a4e06468
RS
7011 if (mips_opts.noreorder)
7012 return APPEND_ADD;
7013
7014 /* Otherwise, it's our responsibility to fill branch delay slots. */
11625dd8 7015 if (delayed_branch_p (ip))
a4e06468 7016 {
e407c74b
NC
7017 if (!branch_likely_p (ip)
7018 && can_swap_branch_p (ip, address_expr, reloc_type))
a4e06468
RS
7019 return APPEND_SWAP;
7020
7021 if (mips_opts.mips16
7022 && ISA_SUPPORTS_MIPS16E
fc76e730 7023 && gpr_read_mask (ip) != 0)
a4e06468
RS
7024 return APPEND_ADD_COMPACT;
7025
7bd374a4
MR
7026 if (mips_opts.micromips
7027 && ((ip->insn_opcode & 0xffe0) == 0x4580
7028 || (!forced_insn_length
7029 && ((ip->insn_opcode & 0xfc00) == 0xcc00
7030 || (ip->insn_opcode & 0xdc00) == 0x8c00))
7031 || (ip->insn_opcode & 0xdfe00000) == 0x94000000
7032 || (ip->insn_opcode & 0xdc1f0000) == 0x94000000))
7033 return APPEND_ADD_COMPACT;
7034
a4e06468
RS
7035 return APPEND_ADD_WITH_NOP;
7036 }
7037
a4e06468
RS
7038 return APPEND_ADD;
7039}
7040
7bd374a4
MR
7041/* IP is an instruction whose opcode we have just changed, END points
7042 to the end of the opcode table processed. Point IP->insn_mo to the
7043 new opcode's definition. */
ceb94aa5
RS
7044
7045static void
7bd374a4 7046find_altered_opcode (struct mips_cl_insn *ip, const struct mips_opcode *end)
ceb94aa5 7047{
7bd374a4 7048 const struct mips_opcode *mo;
ceb94aa5 7049
ceb94aa5 7050 for (mo = ip->insn_mo; mo < end; mo++)
7bd374a4
MR
7051 if (mo->pinfo != INSN_MACRO
7052 && (ip->insn_opcode & mo->mask) == mo->match)
ceb94aa5
RS
7053 {
7054 ip->insn_mo = mo;
7055 return;
7056 }
7057 abort ();
7058}
7059
7bd374a4
MR
7060/* IP is a MIPS16 instruction whose opcode we have just changed.
7061 Point IP->insn_mo to the new opcode's definition. */
7062
7063static void
7064find_altered_mips16_opcode (struct mips_cl_insn *ip)
7065{
7066 find_altered_opcode (ip, &mips16_opcodes[bfd_mips16_num_opcodes]);
7067}
7068
7069/* IP is a microMIPS instruction whose opcode we have just changed.
7070 Point IP->insn_mo to the new opcode's definition. */
7071
7072static void
7073find_altered_micromips_opcode (struct mips_cl_insn *ip)
7074{
7075 find_altered_opcode (ip, &micromips_opcodes[bfd_micromips_num_opcodes]);
7076}
7077
df58fc94
RS
7078/* For microMIPS macros, we need to generate a local number label
7079 as the target of branches. */
7080#define MICROMIPS_LABEL_CHAR '\037'
7081static unsigned long micromips_target_label;
7082static char micromips_target_name[32];
7083
7084static char *
7085micromips_label_name (void)
7086{
7087 char *p = micromips_target_name;
7088 char symbol_name_temporary[24];
7089 unsigned long l;
7090 int i;
7091
7092 if (*p)
7093 return p;
7094
7095 i = 0;
7096 l = micromips_target_label;
7097#ifdef LOCAL_LABEL_PREFIX
7098 *p++ = LOCAL_LABEL_PREFIX;
7099#endif
7100 *p++ = 'L';
7101 *p++ = MICROMIPS_LABEL_CHAR;
7102 do
7103 {
7104 symbol_name_temporary[i++] = l % 10 + '0';
7105 l /= 10;
7106 }
7107 while (l != 0);
7108 while (i > 0)
7109 *p++ = symbol_name_temporary[--i];
7110 *p = '\0';
7111
7112 return micromips_target_name;
7113}
7114
7115static void
7116micromips_label_expr (expressionS *label_expr)
7117{
7118 label_expr->X_op = O_symbol;
7119 label_expr->X_add_symbol = symbol_find_or_make (micromips_label_name ());
7120 label_expr->X_add_number = 0;
7121}
7122
7123static void
7124micromips_label_inc (void)
7125{
7126 micromips_target_label++;
7127 *micromips_target_name = '\0';
7128}
7129
7130static void
7131micromips_add_label (void)
7132{
7133 symbolS *s;
7134
7135 s = colon (micromips_label_name ());
7136 micromips_label_inc ();
f3ded42a 7137 S_SET_OTHER (s, ELF_ST_SET_MICROMIPS (S_GET_OTHER (s)));
df58fc94
RS
7138}
7139
7140/* If assembling microMIPS code, then return the microMIPS reloc
7141 corresponding to the requested one if any. Otherwise return
7142 the reloc unchanged. */
7143
7144static bfd_reloc_code_real_type
7145micromips_map_reloc (bfd_reloc_code_real_type reloc)
7146{
7147 static const bfd_reloc_code_real_type relocs[][2] =
7148 {
7149 /* Keep sorted incrementally by the left-hand key. */
7150 { BFD_RELOC_16_PCREL_S2, BFD_RELOC_MICROMIPS_16_PCREL_S1 },
7151 { BFD_RELOC_GPREL16, BFD_RELOC_MICROMIPS_GPREL16 },
7152 { BFD_RELOC_MIPS_JMP, BFD_RELOC_MICROMIPS_JMP },
7153 { BFD_RELOC_HI16, BFD_RELOC_MICROMIPS_HI16 },
7154 { BFD_RELOC_HI16_S, BFD_RELOC_MICROMIPS_HI16_S },
7155 { BFD_RELOC_LO16, BFD_RELOC_MICROMIPS_LO16 },
7156 { BFD_RELOC_MIPS_LITERAL, BFD_RELOC_MICROMIPS_LITERAL },
7157 { BFD_RELOC_MIPS_GOT16, BFD_RELOC_MICROMIPS_GOT16 },
7158 { BFD_RELOC_MIPS_CALL16, BFD_RELOC_MICROMIPS_CALL16 },
7159 { BFD_RELOC_MIPS_GOT_HI16, BFD_RELOC_MICROMIPS_GOT_HI16 },
7160 { BFD_RELOC_MIPS_GOT_LO16, BFD_RELOC_MICROMIPS_GOT_LO16 },
7161 { BFD_RELOC_MIPS_CALL_HI16, BFD_RELOC_MICROMIPS_CALL_HI16 },
7162 { BFD_RELOC_MIPS_CALL_LO16, BFD_RELOC_MICROMIPS_CALL_LO16 },
7163 { BFD_RELOC_MIPS_SUB, BFD_RELOC_MICROMIPS_SUB },
7164 { BFD_RELOC_MIPS_GOT_PAGE, BFD_RELOC_MICROMIPS_GOT_PAGE },
7165 { BFD_RELOC_MIPS_GOT_OFST, BFD_RELOC_MICROMIPS_GOT_OFST },
7166 { BFD_RELOC_MIPS_GOT_DISP, BFD_RELOC_MICROMIPS_GOT_DISP },
7167 { BFD_RELOC_MIPS_HIGHEST, BFD_RELOC_MICROMIPS_HIGHEST },
7168 { BFD_RELOC_MIPS_HIGHER, BFD_RELOC_MICROMIPS_HIGHER },
7169 { BFD_RELOC_MIPS_SCN_DISP, BFD_RELOC_MICROMIPS_SCN_DISP },
7170 { BFD_RELOC_MIPS_TLS_GD, BFD_RELOC_MICROMIPS_TLS_GD },
7171 { BFD_RELOC_MIPS_TLS_LDM, BFD_RELOC_MICROMIPS_TLS_LDM },
7172 { BFD_RELOC_MIPS_TLS_DTPREL_HI16, BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16 },
7173 { BFD_RELOC_MIPS_TLS_DTPREL_LO16, BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16 },
7174 { BFD_RELOC_MIPS_TLS_GOTTPREL, BFD_RELOC_MICROMIPS_TLS_GOTTPREL },
7175 { BFD_RELOC_MIPS_TLS_TPREL_HI16, BFD_RELOC_MICROMIPS_TLS_TPREL_HI16 },
7176 { BFD_RELOC_MIPS_TLS_TPREL_LO16, BFD_RELOC_MICROMIPS_TLS_TPREL_LO16 }
7177 };
7178 bfd_reloc_code_real_type r;
7179 size_t i;
7180
7181 if (!mips_opts.micromips)
7182 return reloc;
7183 for (i = 0; i < ARRAY_SIZE (relocs); i++)
7184 {
7185 r = relocs[i][0];
7186 if (r > reloc)
7187 return reloc;
7188 if (r == reloc)
7189 return relocs[i][1];
7190 }
7191 return reloc;
7192}
7193
b886a2ab
RS
7194/* Try to resolve relocation RELOC against constant OPERAND at assembly time.
7195 Return true on success, storing the resolved value in RESULT. */
7196
7197static bfd_boolean
7198calculate_reloc (bfd_reloc_code_real_type reloc, offsetT operand,
7199 offsetT *result)
7200{
7201 switch (reloc)
7202 {
7203 case BFD_RELOC_MIPS_HIGHEST:
7204 case BFD_RELOC_MICROMIPS_HIGHEST:
7205 *result = ((operand + 0x800080008000ull) >> 48) & 0xffff;
7206 return TRUE;
7207
7208 case BFD_RELOC_MIPS_HIGHER:
7209 case BFD_RELOC_MICROMIPS_HIGHER:
7210 *result = ((operand + 0x80008000ull) >> 32) & 0xffff;
7211 return TRUE;
7212
7213 case BFD_RELOC_HI16_S:
41947d9e 7214 case BFD_RELOC_HI16_S_PCREL:
b886a2ab
RS
7215 case BFD_RELOC_MICROMIPS_HI16_S:
7216 case BFD_RELOC_MIPS16_HI16_S:
7217 *result = ((operand + 0x8000) >> 16) & 0xffff;
7218 return TRUE;
7219
7220 case BFD_RELOC_HI16:
7221 case BFD_RELOC_MICROMIPS_HI16:
7222 case BFD_RELOC_MIPS16_HI16:
7223 *result = (operand >> 16) & 0xffff;
7224 return TRUE;
7225
7226 case BFD_RELOC_LO16:
41947d9e 7227 case BFD_RELOC_LO16_PCREL:
b886a2ab
RS
7228 case BFD_RELOC_MICROMIPS_LO16:
7229 case BFD_RELOC_MIPS16_LO16:
7230 *result = operand & 0xffff;
7231 return TRUE;
7232
7233 case BFD_RELOC_UNUSED:
7234 *result = operand;
7235 return TRUE;
7236
7237 default:
7238 return FALSE;
7239 }
7240}
7241
71400594
RS
7242/* Output an instruction. IP is the instruction information.
7243 ADDRESS_EXPR is an operand of the instruction to be used with
df58fc94
RS
7244 RELOC_TYPE. EXPANSIONP is true if the instruction is part of
7245 a macro expansion. */
71400594
RS
7246
7247static void
7248append_insn (struct mips_cl_insn *ip, expressionS *address_expr,
df58fc94 7249 bfd_reloc_code_real_type *reloc_type, bfd_boolean expansionp)
71400594 7250{
14fe068b 7251 unsigned long prev_pinfo2, pinfo;
71400594 7252 bfd_boolean relaxed_branch = FALSE;
a4e06468 7253 enum append_method method;
2309ddf2 7254 bfd_boolean relax32;
2b0c8b40 7255 int branch_disp;
71400594 7256
2309ddf2 7257 if (mips_fix_loongson2f && !HAVE_CODE_COMPRESSION)
c67a084a
NC
7258 fix_loongson2f (ip);
7259
738f4d98 7260 file_ase_mips16 |= mips_opts.mips16;
df58fc94 7261 file_ase_micromips |= mips_opts.micromips;
738f4d98 7262
df58fc94 7263 prev_pinfo2 = history[0].insn_mo->pinfo2;
71400594 7264 pinfo = ip->insn_mo->pinfo;
df58fc94 7265
7bd374a4
MR
7266 /* Don't raise alarm about `nods' frags as they'll fill in the right
7267 kind of nop in relaxation if required. */
df58fc94
RS
7268 if (mips_opts.micromips
7269 && !expansionp
7bd374a4
MR
7270 && !(history[0].frag
7271 && history[0].frag->fr_type == rs_machine_dependent
7272 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
7273 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
df58fc94
RS
7274 && (((prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0
7275 && micromips_insn_length (ip->insn_mo) != 2)
7276 || ((prev_pinfo2 & INSN2_BRANCH_DELAY_32BIT) != 0
7277 && micromips_insn_length (ip->insn_mo) != 4)))
1661c76c 7278 as_warn (_("wrong size instruction in a %u-bit branch delay slot"),
df58fc94 7279 (prev_pinfo2 & INSN2_BRANCH_DELAY_16BIT) != 0 ? 16 : 32);
71400594 7280
15be625d
CM
7281 if (address_expr == NULL)
7282 ip->complete_p = 1;
b886a2ab
RS
7283 else if (reloc_type[0] <= BFD_RELOC_UNUSED
7284 && reloc_type[1] == BFD_RELOC_UNUSED
7285 && reloc_type[2] == BFD_RELOC_UNUSED
15be625d
CM
7286 && address_expr->X_op == O_constant)
7287 {
15be625d
CM
7288 switch (*reloc_type)
7289 {
15be625d 7290 case BFD_RELOC_MIPS_JMP:
df58fc94
RS
7291 {
7292 int shift;
7293
17c6c9d9
MR
7294 /* Shift is 2, unusually, for microMIPS JALX. */
7295 shift = (mips_opts.micromips
7296 && strcmp (ip->insn_mo->name, "jalx") != 0) ? 1 : 2;
df58fc94
RS
7297 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7298 as_bad (_("jump to misaligned address (0x%lx)"),
7299 (unsigned long) address_expr->X_add_number);
7300 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7301 & 0x3ffffff);
335574df 7302 ip->complete_p = 1;
df58fc94 7303 }
15be625d
CM
7304 break;
7305
7306 case BFD_RELOC_MIPS16_JMP:
7307 if ((address_expr->X_add_number & 3) != 0)
7308 as_bad (_("jump to misaligned address (0x%lx)"),
7309 (unsigned long) address_expr->X_add_number);
7310 ip->insn_opcode |=
7311 (((address_expr->X_add_number & 0x7c0000) << 3)
7312 | ((address_expr->X_add_number & 0xf800000) >> 7)
7313 | ((address_expr->X_add_number & 0x3fffc) >> 2));
335574df 7314 ip->complete_p = 1;
15be625d
CM
7315 break;
7316
7317 case BFD_RELOC_16_PCREL_S2:
df58fc94
RS
7318 {
7319 int shift;
7320
7321 shift = mips_opts.micromips ? 1 : 2;
7322 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7323 as_bad (_("branch to misaligned address (0x%lx)"),
7324 (unsigned long) address_expr->X_add_number);
7325 if (!mips_relax_branch)
7326 {
7327 if ((address_expr->X_add_number + (1 << (shift + 15)))
7328 & ~((1 << (shift + 16)) - 1))
7329 as_bad (_("branch address range overflow (0x%lx)"),
7330 (unsigned long) address_expr->X_add_number);
7331 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7332 & 0xffff);
7333 }
df58fc94 7334 }
15be625d
CM
7335 break;
7336
7361da2c
AB
7337 case BFD_RELOC_MIPS_21_PCREL_S2:
7338 {
7339 int shift;
7340
7341 shift = 2;
7342 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7343 as_bad (_("branch to misaligned address (0x%lx)"),
7344 (unsigned long) address_expr->X_add_number);
7345 if ((address_expr->X_add_number + (1 << (shift + 20)))
7346 & ~((1 << (shift + 21)) - 1))
7347 as_bad (_("branch address range overflow (0x%lx)"),
7348 (unsigned long) address_expr->X_add_number);
7349 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7350 & 0x1fffff);
7351 }
7352 break;
7353
7354 case BFD_RELOC_MIPS_26_PCREL_S2:
7355 {
7356 int shift;
7357
7358 shift = 2;
7359 if ((address_expr->X_add_number & ((1 << shift) - 1)) != 0)
7360 as_bad (_("branch to misaligned address (0x%lx)"),
7361 (unsigned long) address_expr->X_add_number);
7362 if ((address_expr->X_add_number + (1 << (shift + 25)))
7363 & ~((1 << (shift + 26)) - 1))
7364 as_bad (_("branch address range overflow (0x%lx)"),
7365 (unsigned long) address_expr->X_add_number);
7366 ip->insn_opcode |= ((address_expr->X_add_number >> shift)
7367 & 0x3ffffff);
7368 }
7369 break;
7370
15be625d 7371 default:
b886a2ab
RS
7372 {
7373 offsetT value;
7374
7375 if (calculate_reloc (*reloc_type, address_expr->X_add_number,
7376 &value))
7377 {
7378 ip->insn_opcode |= value & 0xffff;
7379 ip->complete_p = 1;
7380 }
7381 }
7382 break;
7383 }
15be625d
CM
7384 }
7385
71400594
RS
7386 if (mips_relax.sequence != 2 && !mips_opts.noreorder)
7387 {
7388 /* There are a lot of optimizations we could do that we don't.
7389 In particular, we do not, in general, reorder instructions.
7390 If you use gcc with optimization, it will reorder
7391 instructions and generally do much more optimization then we
7392 do here; repeating all that work in the assembler would only
7393 benefit hand written assembly code, and does not seem worth
7394 it. */
7395 int nops = (mips_optimize == 0
932d1a1b
RS
7396 ? nops_for_insn (0, history, NULL)
7397 : nops_for_insn_or_target (0, history, ip));
71400594 7398 if (nops > 0)
252b5132
RH
7399 {
7400 fragS *old_frag;
7401 unsigned long old_frag_offset;
7402 int i;
252b5132
RH
7403
7404 old_frag = frag_now;
7405 old_frag_offset = frag_now_fix ();
7406
7407 for (i = 0; i < nops; i++)
14fe068b
RS
7408 add_fixed_insn (NOP_INSN);
7409 insert_into_history (0, nops, NOP_INSN);
252b5132
RH
7410
7411 if (listing)
7412 {
7413 listing_prev_line ();
7414 /* We may be at the start of a variant frag. In case we
7415 are, make sure there is enough space for the frag
7416 after the frags created by listing_prev_line. The
7417 argument to frag_grow here must be at least as large
7418 as the argument to all other calls to frag_grow in
7419 this file. We don't have to worry about being in the
7420 middle of a variant frag, because the variants insert
7421 all needed nop instructions themselves. */
7422 frag_grow (40);
7423 }
7424
462427c4 7425 mips_move_text_labels ();
252b5132
RH
7426
7427#ifndef NO_ECOFF_DEBUGGING
7428 if (ECOFF_DEBUGGING)
7429 ecoff_fix_loc (old_frag, old_frag_offset);
7430#endif
7431 }
71400594
RS
7432 }
7433 else if (mips_relax.sequence != 2 && prev_nop_frag != NULL)
7434 {
932d1a1b
RS
7435 int nops;
7436
7437 /* Work out how many nops in prev_nop_frag are needed by IP,
7438 ignoring hazards generated by the first prev_nop_frag_since
7439 instructions. */
7440 nops = nops_for_insn_or_target (prev_nop_frag_since, history, ip);
9c2799c2 7441 gas_assert (nops <= prev_nop_frag_holds);
252b5132 7442
71400594
RS
7443 /* Enforce NOPS as a minimum. */
7444 if (nops > prev_nop_frag_required)
7445 prev_nop_frag_required = nops;
252b5132 7446
71400594
RS
7447 if (prev_nop_frag_holds == prev_nop_frag_required)
7448 {
7449 /* Settle for the current number of nops. Update the history
7450 accordingly (for the benefit of any future .set reorder code). */
7451 prev_nop_frag = NULL;
7452 insert_into_history (prev_nop_frag_since,
7453 prev_nop_frag_holds, NOP_INSN);
7454 }
7455 else
7456 {
7457 /* Allow this instruction to replace one of the nops that was
7458 tentatively added to prev_nop_frag. */
df58fc94 7459 prev_nop_frag->fr_fix -= NOP_INSN_SIZE;
71400594
RS
7460 prev_nop_frag_holds--;
7461 prev_nop_frag_since++;
252b5132
RH
7462 }
7463 }
7464
e407c74b 7465 method = get_append_method (ip, address_expr, reloc_type);
2b0c8b40 7466 branch_disp = method == APPEND_SWAP ? insn_length (history) : 0;
a4e06468 7467
e410add4
RS
7468 dwarf2_emit_insn (0);
7469 /* We want MIPS16 and microMIPS debug info to use ISA-encoded addresses,
7470 so "move" the instruction address accordingly.
7471
7472 Also, it doesn't seem appropriate for the assembler to reorder .loc
7473 entries. If this instruction is a branch that we are going to swap
7474 with the previous instruction, the two instructions should be
7475 treated as a unit, and the debug information for both instructions
7476 should refer to the start of the branch sequence. Using the
7477 current position is certainly wrong when swapping a 32-bit branch
7478 and a 16-bit delay slot, since the current position would then be
7479 in the middle of a branch. */
7480 dwarf2_move_insn ((HAVE_CODE_COMPRESSION ? 1 : 0) - branch_disp);
58e2ea4d 7481
df58fc94
RS
7482 relax32 = (mips_relax_branch
7483 /* Don't try branch relaxation within .set nomacro, or within
7484 .set noat if we use $at for PIC computations. If it turns
7485 out that the branch was out-of-range, we'll get an error. */
7486 && !mips_opts.warn_about_macros
7487 && (mips_opts.at || mips_pic == NO_PIC)
3bf0dbfb
MR
7488 /* Don't relax BPOSGE32/64 or BC1ANY2T/F and BC1ANY4T/F
7489 as they have no complementing branches. */
7490 && !(ip->insn_mo->ase & (ASE_MIPS3D | ASE_DSP64 | ASE_DSP)));
df58fc94
RS
7491
7492 if (!HAVE_CODE_COMPRESSION
7493 && address_expr
7494 && relax32
0b25d3e6 7495 && *reloc_type == BFD_RELOC_16_PCREL_S2
11625dd8 7496 && delayed_branch_p (ip))
4a6a3df4 7497 {
895921c9 7498 relaxed_branch = TRUE;
1e915849
RS
7499 add_relaxed_insn (ip, (relaxed_branch_length
7500 (NULL, NULL,
11625dd8
RS
7501 uncond_branch_p (ip) ? -1
7502 : branch_likely_p (ip) ? 1
1e915849
RS
7503 : 0)), 4,
7504 RELAX_BRANCH_ENCODE
ce8ad872 7505 (AT, mips_pic != NO_PIC,
11625dd8
RS
7506 uncond_branch_p (ip),
7507 branch_likely_p (ip),
1e915849
RS
7508 pinfo & INSN_WRITE_GPR_31,
7509 0),
7510 address_expr->X_add_symbol,
7511 address_expr->X_add_number);
4a6a3df4
AO
7512 *reloc_type = BFD_RELOC_UNUSED;
7513 }
df58fc94
RS
7514 else if (mips_opts.micromips
7515 && address_expr
7516 && ((relax32 && *reloc_type == BFD_RELOC_16_PCREL_S2)
7517 || *reloc_type > BFD_RELOC_UNUSED)
40209cad
MR
7518 && (delayed_branch_p (ip) || compact_branch_p (ip))
7519 /* Don't try branch relaxation when users specify
7520 16-bit/32-bit instructions. */
7521 && !forced_insn_length)
df58fc94 7522 {
7bd374a4
MR
7523 bfd_boolean relax16 = (method != APPEND_ADD_COMPACT
7524 && *reloc_type > BFD_RELOC_UNUSED);
df58fc94 7525 int type = relax16 ? *reloc_type - BFD_RELOC_UNUSED : 0;
11625dd8 7526 int uncond = uncond_branch_p (ip) ? -1 : 0;
7bd374a4
MR
7527 int compact = compact_branch_p (ip) || method == APPEND_ADD_COMPACT;
7528 int nods = method == APPEND_ADD_WITH_NOP;
df58fc94 7529 int al = pinfo & INSN_WRITE_GPR_31;
7bd374a4 7530 int length32 = nods ? 8 : 4;
df58fc94
RS
7531
7532 gas_assert (address_expr != NULL);
7533 gas_assert (!mips_relax.sequence);
7534
2b0c8b40 7535 relaxed_branch = TRUE;
7bd374a4
MR
7536 if (nods)
7537 method = APPEND_ADD;
7538 if (relax32)
7539 length32 = relaxed_micromips_32bit_branch_length (NULL, NULL, uncond);
7540 add_relaxed_insn (ip, length32, relax16 ? 2 : 4,
8484fb75 7541 RELAX_MICROMIPS_ENCODE (type, AT, mips_opts.insn32,
ce8ad872 7542 mips_pic != NO_PIC,
7bd374a4 7543 uncond, compact, al, nods,
40209cad 7544 relax32, 0, 0),
df58fc94
RS
7545 address_expr->X_add_symbol,
7546 address_expr->X_add_number);
7547 *reloc_type = BFD_RELOC_UNUSED;
7548 }
7549 else if (mips_opts.mips16 && *reloc_type > BFD_RELOC_UNUSED)
252b5132 7550 {
7fd53920
MR
7551 bfd_boolean require_unextended;
7552 bfd_boolean require_extended;
88a7ef16
MR
7553 symbolS *symbol;
7554 offsetT offset;
7555
7fd53920
MR
7556 if (forced_insn_length != 0)
7557 {
7558 require_unextended = forced_insn_length == 2;
7559 require_extended = forced_insn_length == 4;
7560 }
7561 else
7562 {
7563 require_unextended = (mips_opts.noautoextend
7564 && !mips_opcode_32bit_p (ip->insn_mo));
7565 require_extended = 0;
7566 }
7567
252b5132 7568 /* We need to set up a variant frag. */
df58fc94 7569 gas_assert (address_expr != NULL);
88a7ef16
MR
7570 /* Pass any `O_symbol' expression unchanged as an `expr_section'
7571 symbol created by `make_expr_symbol' may not get a necessary
7572 external relocation produced. */
7573 if (address_expr->X_op == O_symbol)
7574 {
7575 symbol = address_expr->X_add_symbol;
7576 offset = address_expr->X_add_number;
7577 }
7578 else
7579 {
7580 symbol = make_expr_symbol (address_expr);
82d808ed 7581 symbol_append (symbol, symbol_lastP, &symbol_rootP, &symbol_lastP);
88a7ef16
MR
7582 offset = 0;
7583 }
8507b6e7 7584 add_relaxed_insn (ip, 12, 0,
1e915849
RS
7585 RELAX_MIPS16_ENCODE
7586 (*reloc_type - BFD_RELOC_UNUSED,
25499ac7 7587 mips_opts.ase & ASE_MIPS16E2,
8507b6e7
MR
7588 mips_pic != NO_PIC,
7589 HAVE_32BIT_SYMBOLS,
7590 mips_opts.warn_about_macros,
7fd53920 7591 require_unextended, require_extended,
11625dd8 7592 delayed_branch_p (&history[0]),
1e915849 7593 history[0].mips16_absolute_jump_p),
88a7ef16 7594 symbol, offset);
252b5132 7595 }
5c04167a 7596 else if (mips_opts.mips16 && insn_length (ip) == 2)
9497f5ac 7597 {
11625dd8 7598 if (!delayed_branch_p (ip))
b8ee1a6e
DU
7599 /* Make sure there is enough room to swap this instruction with
7600 a following jump instruction. */
7601 frag_grow (6);
1e915849 7602 add_fixed_insn (ip);
252b5132
RH
7603 }
7604 else
7605 {
7606 if (mips_opts.mips16
7607 && mips_opts.noreorder
11625dd8 7608 && delayed_branch_p (&history[0]))
252b5132
RH
7609 as_warn (_("extended instruction in delay slot"));
7610
4d7206a2
RS
7611 if (mips_relax.sequence)
7612 {
7613 /* If we've reached the end of this frag, turn it into a variant
7614 frag and record the information for the instructions we've
7615 written so far. */
7616 if (frag_room () < 4)
7617 relax_close_frag ();
df58fc94 7618 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (ip);
4d7206a2
RS
7619 }
7620
584892a6 7621 if (mips_relax.sequence != 2)
df58fc94
RS
7622 {
7623 if (mips_macro_warning.first_insn_sizes[0] == 0)
7624 mips_macro_warning.first_insn_sizes[0] = insn_length (ip);
7625 mips_macro_warning.sizes[0] += insn_length (ip);
7626 mips_macro_warning.insns[0]++;
7627 }
584892a6 7628 if (mips_relax.sequence != 1)
df58fc94
RS
7629 {
7630 if (mips_macro_warning.first_insn_sizes[1] == 0)
7631 mips_macro_warning.first_insn_sizes[1] = insn_length (ip);
7632 mips_macro_warning.sizes[1] += insn_length (ip);
7633 mips_macro_warning.insns[1]++;
7634 }
584892a6 7635
1e915849
RS
7636 if (mips_opts.mips16)
7637 {
7638 ip->fixed_p = 1;
7639 ip->mips16_absolute_jump_p = (*reloc_type == BFD_RELOC_MIPS16_JMP);
7640 }
7641 add_fixed_insn (ip);
252b5132
RH
7642 }
7643
9fe77896 7644 if (!ip->complete_p && *reloc_type < BFD_RELOC_UNUSED)
252b5132 7645 {
df58fc94 7646 bfd_reloc_code_real_type final_type[3];
2309ddf2 7647 reloc_howto_type *howto0;
9fe77896
RS
7648 reloc_howto_type *howto;
7649 int i;
34ce925e 7650
df58fc94
RS
7651 /* Perform any necessary conversion to microMIPS relocations
7652 and find out how many relocations there actually are. */
7653 for (i = 0; i < 3 && reloc_type[i] != BFD_RELOC_UNUSED; i++)
7654 final_type[i] = micromips_map_reloc (reloc_type[i]);
7655
9fe77896
RS
7656 /* In a compound relocation, it is the final (outermost)
7657 operator that determines the relocated field. */
2309ddf2 7658 howto = howto0 = bfd_reloc_type_lookup (stdoutput, final_type[i - 1]);
e8044f35
RS
7659 if (!howto)
7660 abort ();
2309ddf2
MR
7661
7662 if (i > 1)
7663 howto0 = bfd_reloc_type_lookup (stdoutput, final_type[0]);
9fe77896
RS
7664 ip->fixp[0] = fix_new_exp (ip->frag, ip->where,
7665 bfd_get_reloc_size (howto),
7666 address_expr,
2309ddf2
MR
7667 howto0 && howto0->pc_relative,
7668 final_type[0]);
ce8ad872
MR
7669 /* Record non-PIC mode in `fx_tcbit2' for `md_apply_fix'. */
7670 ip->fixp[0]->fx_tcbit2 = mips_pic == NO_PIC;
9fe77896
RS
7671
7672 /* Tag symbols that have a R_MIPS16_26 relocation against them. */
2309ddf2 7673 if (final_type[0] == BFD_RELOC_MIPS16_JMP && ip->fixp[0]->fx_addsy)
9fe77896
RS
7674 *symbol_get_tc (ip->fixp[0]->fx_addsy) = 1;
7675
7676 /* These relocations can have an addend that won't fit in
7677 4 octets for 64bit assembly. */
bad1aba3 7678 if (GPR_SIZE == 64
9fe77896
RS
7679 && ! howto->partial_inplace
7680 && (reloc_type[0] == BFD_RELOC_16
7681 || reloc_type[0] == BFD_RELOC_32
7682 || reloc_type[0] == BFD_RELOC_MIPS_JMP
7683 || reloc_type[0] == BFD_RELOC_GPREL16
7684 || reloc_type[0] == BFD_RELOC_MIPS_LITERAL
7685 || reloc_type[0] == BFD_RELOC_GPREL32
7686 || reloc_type[0] == BFD_RELOC_64
7687 || reloc_type[0] == BFD_RELOC_CTOR
7688 || reloc_type[0] == BFD_RELOC_MIPS_SUB
7689 || reloc_type[0] == BFD_RELOC_MIPS_HIGHEST
7690 || reloc_type[0] == BFD_RELOC_MIPS_HIGHER
7691 || reloc_type[0] == BFD_RELOC_MIPS_SCN_DISP
7692 || reloc_type[0] == BFD_RELOC_MIPS_REL16
7693 || reloc_type[0] == BFD_RELOC_MIPS_RELGOT
7694 || reloc_type[0] == BFD_RELOC_MIPS16_GPREL
7695 || hi16_reloc_p (reloc_type[0])
7696 || lo16_reloc_p (reloc_type[0])))
7697 ip->fixp[0]->fx_no_overflow = 1;
7698
ddaf2c41
MR
7699 /* These relocations can have an addend that won't fit in 2 octets. */
7700 if (reloc_type[0] == BFD_RELOC_MICROMIPS_7_PCREL_S1
7701 || reloc_type[0] == BFD_RELOC_MICROMIPS_10_PCREL_S1)
7702 ip->fixp[0]->fx_no_overflow = 1;
7703
9fe77896
RS
7704 if (mips_relax.sequence)
7705 {
7706 if (mips_relax.first_fixup == 0)
7707 mips_relax.first_fixup = ip->fixp[0];
7708 }
7709 else if (reloc_needs_lo_p (*reloc_type))
7710 {
7711 struct mips_hi_fixup *hi_fixup;
7712
7713 /* Reuse the last entry if it already has a matching %lo. */
7714 hi_fixup = mips_hi_fixup_list;
7715 if (hi_fixup == 0
7716 || !fixup_has_matching_lo_p (hi_fixup->fixp))
4d7206a2 7717 {
325801bd 7718 hi_fixup = XNEW (struct mips_hi_fixup);
9fe77896
RS
7719 hi_fixup->next = mips_hi_fixup_list;
7720 mips_hi_fixup_list = hi_fixup;
4d7206a2 7721 }
9fe77896
RS
7722 hi_fixup->fixp = ip->fixp[0];
7723 hi_fixup->seg = now_seg;
7724 }
252b5132 7725
9fe77896
RS
7726 /* Add fixups for the second and third relocations, if given.
7727 Note that the ABI allows the second relocation to be
7728 against RSS_UNDEF, RSS_GP, RSS_GP0 or RSS_LOC. At the
7729 moment we only use RSS_UNDEF, but we could add support
7730 for the others if it ever becomes necessary. */
7731 for (i = 1; i < 3; i++)
7732 if (reloc_type[i] != BFD_RELOC_UNUSED)
7733 {
7734 ip->fixp[i] = fix_new (ip->frag, ip->where,
7735 ip->fixp[0]->fx_size, NULL, 0,
df58fc94 7736 FALSE, final_type[i]);
f6688943 7737
9fe77896
RS
7738 /* Use fx_tcbit to mark compound relocs. */
7739 ip->fixp[0]->fx_tcbit = 1;
7740 ip->fixp[i]->fx_tcbit = 1;
7741 }
252b5132 7742 }
252b5132
RH
7743
7744 /* Update the register mask information. */
4c260379
RS
7745 mips_gprmask |= gpr_read_mask (ip) | gpr_write_mask (ip);
7746 mips_cprmask[1] |= fpr_read_mask (ip) | fpr_write_mask (ip);
252b5132 7747
a4e06468 7748 switch (method)
252b5132 7749 {
a4e06468
RS
7750 case APPEND_ADD:
7751 insert_into_history (0, 1, ip);
7752 break;
7753
7754 case APPEND_ADD_WITH_NOP:
14fe068b
RS
7755 {
7756 struct mips_cl_insn *nop;
7757
7758 insert_into_history (0, 1, ip);
7759 nop = get_delay_slot_nop (ip);
7760 add_fixed_insn (nop);
7761 insert_into_history (0, 1, nop);
7762 if (mips_relax.sequence)
7763 mips_relax.sizes[mips_relax.sequence - 1] += insn_length (nop);
7764 }
a4e06468
RS
7765 break;
7766
7767 case APPEND_ADD_COMPACT:
7768 /* Convert MIPS16 jr/jalr into a "compact" jump. */
7bd374a4
MR
7769 if (mips_opts.mips16)
7770 {
7771 ip->insn_opcode |= 0x0080;
7772 find_altered_mips16_opcode (ip);
7773 }
7774 /* Convert microMIPS instructions. */
7775 else if (mips_opts.micromips)
7776 {
7777 /* jr16->jrc */
7778 if ((ip->insn_opcode & 0xffe0) == 0x4580)
7779 ip->insn_opcode |= 0x0020;
7780 /* b16->bc */
7781 else if ((ip->insn_opcode & 0xfc00) == 0xcc00)
7782 ip->insn_opcode = 0x40e00000;
7783 /* beqz16->beqzc, bnez16->bnezc */
7784 else if ((ip->insn_opcode & 0xdc00) == 0x8c00)
7785 {
7786 unsigned long regno;
7787
7788 regno = ip->insn_opcode >> MICROMIPSOP_SH_MD;
7789 regno &= MICROMIPSOP_MASK_MD;
7790 regno = micromips_to_32_reg_d_map[regno];
7791 ip->insn_opcode = (((ip->insn_opcode << 9) & 0x00400000)
7792 | (regno << MICROMIPSOP_SH_RS)
7793 | 0x40a00000) ^ 0x00400000;
7794 }
7795 /* beqz->beqzc, bnez->bnezc */
7796 else if ((ip->insn_opcode & 0xdfe00000) == 0x94000000)
7797 ip->insn_opcode = ((ip->insn_opcode & 0x001f0000)
7798 | ((ip->insn_opcode >> 7) & 0x00400000)
7799 | 0x40a00000) ^ 0x00400000;
7800 /* beq $0->beqzc, bne $0->bnezc */
7801 else if ((ip->insn_opcode & 0xdc1f0000) == 0x94000000)
7802 ip->insn_opcode = (((ip->insn_opcode >>
7803 (MICROMIPSOP_SH_RT - MICROMIPSOP_SH_RS))
7804 & (MICROMIPSOP_MASK_RS << MICROMIPSOP_SH_RS))
7805 | ((ip->insn_opcode >> 7) & 0x00400000)
7806 | 0x40a00000) ^ 0x00400000;
7807 else
7808 abort ();
7809 find_altered_micromips_opcode (ip);
7810 }
7811 else
7812 abort ();
a4e06468
RS
7813 install_insn (ip);
7814 insert_into_history (0, 1, ip);
7815 break;
7816
7817 case APPEND_SWAP:
7818 {
7819 struct mips_cl_insn delay = history[0];
99e7978b
MF
7820
7821 if (relaxed_branch || delay.frag != ip->frag)
a4e06468
RS
7822 {
7823 /* Add the delay slot instruction to the end of the
7824 current frag and shrink the fixed part of the
7825 original frag. If the branch occupies the tail of
7826 the latter, move it backwards to cover the gap. */
2b0c8b40 7827 delay.frag->fr_fix -= branch_disp;
a4e06468 7828 if (delay.frag == ip->frag)
2b0c8b40 7829 move_insn (ip, ip->frag, ip->where - branch_disp);
a4e06468
RS
7830 add_fixed_insn (&delay);
7831 }
7832 else
7833 {
5e35670b
MR
7834 /* If this is not a relaxed branch and we are in the
7835 same frag, then just swap the instructions. */
7836 move_insn (ip, delay.frag, delay.where);
7837 move_insn (&delay, ip->frag, ip->where + insn_length (ip));
a4e06468
RS
7838 }
7839 history[0] = *ip;
7840 delay.fixed_p = 1;
7841 insert_into_history (0, 1, &delay);
7842 }
7843 break;
252b5132
RH
7844 }
7845
13408f1e 7846 /* If we have just completed an unconditional branch, clear the history. */
11625dd8
RS
7847 if ((delayed_branch_p (&history[1]) && uncond_branch_p (&history[1]))
7848 || (compact_branch_p (&history[0]) && uncond_branch_p (&history[0])))
e407c74b
NC
7849 {
7850 unsigned int i;
7851
79850f26 7852 mips_no_prev_insn ();
13408f1e 7853
e407c74b 7854 for (i = 0; i < ARRAY_SIZE (history); i++)
79850f26 7855 history[i].cleared_p = 1;
e407c74b
NC
7856 }
7857
df58fc94
RS
7858 /* We need to emit a label at the end of branch-likely macros. */
7859 if (emit_branch_likely_macro)
7860 {
7861 emit_branch_likely_macro = FALSE;
7862 micromips_add_label ();
7863 }
7864
252b5132
RH
7865 /* We just output an insn, so the next one doesn't have a label. */
7866 mips_clear_insn_labels ();
252b5132
RH
7867}
7868
e407c74b
NC
7869/* Forget that there was any previous instruction or label.
7870 When BRANCH is true, the branch history is also flushed. */
252b5132
RH
7871
7872static void
7d10b47d 7873mips_no_prev_insn (void)
252b5132 7874{
7d10b47d
RS
7875 prev_nop_frag = NULL;
7876 insert_into_history (0, ARRAY_SIZE (history), NOP_INSN);
252b5132
RH
7877 mips_clear_insn_labels ();
7878}
7879
7d10b47d
RS
7880/* This function must be called before we emit something other than
7881 instructions. It is like mips_no_prev_insn except that it inserts
7882 any NOPS that might be needed by previous instructions. */
252b5132 7883
7d10b47d
RS
7884void
7885mips_emit_delays (void)
252b5132
RH
7886{
7887 if (! mips_opts.noreorder)
7888 {
932d1a1b 7889 int nops = nops_for_insn (0, history, NULL);
252b5132
RH
7890 if (nops > 0)
7891 {
7d10b47d
RS
7892 while (nops-- > 0)
7893 add_fixed_insn (NOP_INSN);
462427c4 7894 mips_move_text_labels ();
7d10b47d
RS
7895 }
7896 }
7897 mips_no_prev_insn ();
7898}
7899
7900/* Start a (possibly nested) noreorder block. */
7901
7902static void
7903start_noreorder (void)
7904{
7905 if (mips_opts.noreorder == 0)
7906 {
7907 unsigned int i;
7908 int nops;
7909
7910 /* None of the instructions before the .set noreorder can be moved. */
7911 for (i = 0; i < ARRAY_SIZE (history); i++)
7912 history[i].fixed_p = 1;
7913
7914 /* Insert any nops that might be needed between the .set noreorder
7915 block and the previous instructions. We will later remove any
7916 nops that turn out not to be needed. */
932d1a1b 7917 nops = nops_for_insn (0, history, NULL);
7d10b47d
RS
7918 if (nops > 0)
7919 {
7920 if (mips_optimize != 0)
252b5132
RH
7921 {
7922 /* Record the frag which holds the nop instructions, so
7923 that we can remove them if we don't need them. */
df58fc94 7924 frag_grow (nops * NOP_INSN_SIZE);
252b5132
RH
7925 prev_nop_frag = frag_now;
7926 prev_nop_frag_holds = nops;
7927 prev_nop_frag_required = 0;
7928 prev_nop_frag_since = 0;
7929 }
7930
7931 for (; nops > 0; --nops)
1e915849 7932 add_fixed_insn (NOP_INSN);
252b5132 7933
7d10b47d
RS
7934 /* Move on to a new frag, so that it is safe to simply
7935 decrease the size of prev_nop_frag. */
7936 frag_wane (frag_now);
7937 frag_new (0);
462427c4 7938 mips_move_text_labels ();
252b5132 7939 }
df58fc94 7940 mips_mark_labels ();
7d10b47d 7941 mips_clear_insn_labels ();
252b5132 7942 }
7d10b47d
RS
7943 mips_opts.noreorder++;
7944 mips_any_noreorder = 1;
7945}
252b5132 7946
7d10b47d 7947/* End a nested noreorder block. */
252b5132 7948
7d10b47d
RS
7949static void
7950end_noreorder (void)
7951{
7952 mips_opts.noreorder--;
7953 if (mips_opts.noreorder == 0 && prev_nop_frag != NULL)
7954 {
7955 /* Commit to inserting prev_nop_frag_required nops and go back to
7956 handling nop insertion the .set reorder way. */
7957 prev_nop_frag->fr_fix -= ((prev_nop_frag_holds - prev_nop_frag_required)
df58fc94 7958 * NOP_INSN_SIZE);
7d10b47d
RS
7959 insert_into_history (prev_nop_frag_since,
7960 prev_nop_frag_required, NOP_INSN);
7961 prev_nop_frag = NULL;
7962 }
252b5132
RH
7963}
7964
97d87491
RS
7965/* Sign-extend 32-bit mode constants that have bit 31 set and all
7966 higher bits unset. */
7967
7968static void
7969normalize_constant_expr (expressionS *ex)
7970{
7971 if (ex->X_op == O_constant
7972 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7973 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7974 - 0x80000000);
7975}
7976
7977/* Sign-extend 32-bit mode address offsets that have bit 31 set and
7978 all higher bits unset. */
7979
7980static void
7981normalize_address_expr (expressionS *ex)
7982{
7983 if (((ex->X_op == O_constant && HAVE_32BIT_ADDRESSES)
7984 || (ex->X_op == O_symbol && HAVE_32BIT_SYMBOLS))
7985 && IS_ZEXT_32BIT_NUM (ex->X_add_number))
7986 ex->X_add_number = (((ex->X_add_number & 0xffffffff) ^ 0x80000000)
7987 - 0x80000000);
7988}
7989
7990/* Try to match TOKENS against OPCODE, storing the result in INSN.
7991 Return true if the match was successful.
7992
7993 OPCODE_EXTRA is a value that should be ORed into the opcode
7994 (used for VU0 channel suffixes, etc.). MORE_ALTS is true if
7995 there are more alternatives after OPCODE and SOFT_MATCH is
7996 as for mips_arg_info. */
7997
7998static bfd_boolean
7999match_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
8000 struct mips_operand_token *tokens, unsigned int opcode_extra,
60f20e8b 8001 bfd_boolean lax_match, bfd_boolean complete_p)
97d87491
RS
8002{
8003 const char *args;
8004 struct mips_arg_info arg;
8005 const struct mips_operand *operand;
8006 char c;
8007
8008 imm_expr.X_op = O_absent;
97d87491
RS
8009 offset_expr.X_op = O_absent;
8010 offset_reloc[0] = BFD_RELOC_UNUSED;
8011 offset_reloc[1] = BFD_RELOC_UNUSED;
8012 offset_reloc[2] = BFD_RELOC_UNUSED;
8013
8014 create_insn (insn, opcode);
60f20e8b
RS
8015 /* When no opcode suffix is specified, assume ".xyzw". */
8016 if ((opcode->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0 && opcode_extra == 0)
8017 insn->insn_opcode |= 0xf << mips_vu0_channel_mask.lsb;
8018 else
8019 insn->insn_opcode |= opcode_extra;
97d87491
RS
8020 memset (&arg, 0, sizeof (arg));
8021 arg.insn = insn;
8022 arg.token = tokens;
8023 arg.argnum = 1;
8024 arg.last_regno = ILLEGAL_REG;
8025 arg.dest_regno = ILLEGAL_REG;
60f20e8b 8026 arg.lax_match = lax_match;
97d87491
RS
8027 for (args = opcode->args;; ++args)
8028 {
8029 if (arg.token->type == OT_END)
8030 {
8031 /* Handle unary instructions in which only one operand is given.
8032 The source is then the same as the destination. */
8033 if (arg.opnum == 1 && *args == ',')
8034 {
8035 operand = (mips_opts.micromips
8036 ? decode_micromips_operand (args + 1)
8037 : decode_mips_operand (args + 1));
8038 if (operand && mips_optional_operand_p (operand))
8039 {
8040 arg.token = tokens;
8041 arg.argnum = 1;
8042 continue;
8043 }
8044 }
8045
8046 /* Treat elided base registers as $0. */
8047 if (strcmp (args, "(b)") == 0)
8048 args += 3;
8049
8050 if (args[0] == '+')
8051 switch (args[1])
8052 {
8053 case 'K':
8054 case 'N':
8055 /* The register suffix is optional. */
8056 args += 2;
8057 break;
8058 }
8059
8060 /* Fail the match if there were too few operands. */
8061 if (*args)
8062 return FALSE;
8063
8064 /* Successful match. */
60f20e8b
RS
8065 if (!complete_p)
8066 return TRUE;
e3de51ce 8067 clear_insn_error ();
97d87491
RS
8068 if (arg.dest_regno == arg.last_regno
8069 && strncmp (insn->insn_mo->name, "jalr", 4) == 0)
8070 {
8071 if (arg.opnum == 2)
e3de51ce 8072 set_insn_error
1661c76c 8073 (0, _("source and destination must be different"));
97d87491 8074 else if (arg.last_regno == 31)
e3de51ce 8075 set_insn_error
1661c76c 8076 (0, _("a destination register must be supplied"));
97d87491 8077 }
173d3447
CF
8078 else if (arg.last_regno == 31
8079 && (strncmp (insn->insn_mo->name, "bltzal", 6) == 0
8080 || strncmp (insn->insn_mo->name, "bgezal", 6) == 0))
8081 set_insn_error (0, _("the source register must not be $31"));
97d87491
RS
8082 check_completed_insn (&arg);
8083 return TRUE;
8084 }
8085
8086 /* Fail the match if the line has too many operands. */
8087 if (*args == 0)
8088 return FALSE;
8089
8090 /* Handle characters that need to match exactly. */
8091 if (*args == '(' || *args == ')' || *args == ',')
8092 {
8093 if (match_char (&arg, *args))
8094 continue;
8095 return FALSE;
8096 }
8097 if (*args == '#')
8098 {
8099 ++args;
8100 if (arg.token->type == OT_DOUBLE_CHAR
8101 && arg.token->u.ch == *args)
8102 {
8103 ++arg.token;
8104 continue;
8105 }
8106 return FALSE;
8107 }
8108
8109 /* Handle special macro operands. Work out the properties of
8110 other operands. */
8111 arg.opnum += 1;
97d87491
RS
8112 switch (*args)
8113 {
7361da2c
AB
8114 case '-':
8115 switch (args[1])
8116 {
8117 case 'A':
8118 *offset_reloc = BFD_RELOC_MIPS_19_PCREL_S2;
8119 break;
8120
8121 case 'B':
8122 *offset_reloc = BFD_RELOC_MIPS_18_PCREL_S3;
8123 break;
8124 }
8125 break;
8126
97d87491
RS
8127 case '+':
8128 switch (args[1])
8129 {
97d87491
RS
8130 case 'i':
8131 *offset_reloc = BFD_RELOC_MIPS_JMP;
8132 break;
7361da2c
AB
8133
8134 case '\'':
8135 *offset_reloc = BFD_RELOC_MIPS_26_PCREL_S2;
8136 break;
8137
8138 case '\"':
8139 *offset_reloc = BFD_RELOC_MIPS_21_PCREL_S2;
8140 break;
97d87491
RS
8141 }
8142 break;
8143
97d87491 8144 case 'I':
1a00e612
RS
8145 if (!match_const_int (&arg, &imm_expr.X_add_number))
8146 return FALSE;
8147 imm_expr.X_op = O_constant;
bad1aba3 8148 if (GPR_SIZE == 32)
97d87491
RS
8149 normalize_constant_expr (&imm_expr);
8150 continue;
8151
8152 case 'A':
8153 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8154 {
8155 /* Assume that the offset has been elided and that what
8156 we saw was a base register. The match will fail later
8157 if that assumption turns out to be wrong. */
8158 offset_expr.X_op = O_constant;
8159 offset_expr.X_add_number = 0;
8160 }
97d87491 8161 else
1a00e612
RS
8162 {
8163 if (!match_expression (&arg, &offset_expr, offset_reloc))
8164 return FALSE;
8165 normalize_address_expr (&offset_expr);
8166 }
97d87491
RS
8167 continue;
8168
8169 case 'F':
8170 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8171 8, TRUE))
1a00e612 8172 return FALSE;
97d87491
RS
8173 continue;
8174
8175 case 'L':
8176 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8177 8, FALSE))
1a00e612 8178 return FALSE;
97d87491
RS
8179 continue;
8180
8181 case 'f':
8182 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8183 4, TRUE))
1a00e612 8184 return FALSE;
97d87491
RS
8185 continue;
8186
8187 case 'l':
8188 if (!match_float_constant (&arg, &imm_expr, &offset_expr,
8189 4, FALSE))
1a00e612 8190 return FALSE;
97d87491
RS
8191 continue;
8192
97d87491
RS
8193 case 'p':
8194 *offset_reloc = BFD_RELOC_16_PCREL_S2;
8195 break;
8196
8197 case 'a':
8198 *offset_reloc = BFD_RELOC_MIPS_JMP;
8199 break;
8200
8201 case 'm':
8202 gas_assert (mips_opts.micromips);
8203 c = args[1];
8204 switch (c)
8205 {
8206 case 'D':
8207 case 'E':
8208 if (!forced_insn_length)
8209 *offset_reloc = (int) BFD_RELOC_UNUSED + c;
8210 else if (c == 'D')
8211 *offset_reloc = BFD_RELOC_MICROMIPS_10_PCREL_S1;
8212 else
8213 *offset_reloc = BFD_RELOC_MICROMIPS_7_PCREL_S1;
8214 break;
8215 }
8216 break;
8217 }
8218
8219 operand = (mips_opts.micromips
8220 ? decode_micromips_operand (args)
8221 : decode_mips_operand (args));
8222 if (!operand)
8223 abort ();
8224
8225 /* Skip prefixes. */
7361da2c 8226 if (*args == '+' || *args == 'm' || *args == '-')
97d87491
RS
8227 args++;
8228
8229 if (mips_optional_operand_p (operand)
8230 && args[1] == ','
8231 && (arg.token[0].type != OT_REG
8232 || arg.token[1].type == OT_END))
8233 {
8234 /* Assume that the register has been elided and is the
8235 same as the first operand. */
8236 arg.token = tokens;
8237 arg.argnum = 1;
8238 }
8239
8240 if (!match_operand (&arg, operand))
8241 return FALSE;
8242 }
8243}
8244
8245/* Like match_insn, but for MIPS16. */
8246
8247static bfd_boolean
8248match_mips16_insn (struct mips_cl_insn *insn, const struct mips_opcode *opcode,
1a00e612 8249 struct mips_operand_token *tokens)
97d87491
RS
8250{
8251 const char *args;
8252 const struct mips_operand *operand;
8253 const struct mips_operand *ext_operand;
82d808ed 8254 bfd_boolean pcrel = FALSE;
7fd53920 8255 int required_insn_length;
97d87491
RS
8256 struct mips_arg_info arg;
8257 int relax_char;
8258
7fd53920
MR
8259 if (forced_insn_length)
8260 required_insn_length = forced_insn_length;
8261 else if (mips_opts.noautoextend && !mips_opcode_32bit_p (opcode))
8262 required_insn_length = 2;
8263 else
8264 required_insn_length = 0;
8265
97d87491
RS
8266 create_insn (insn, opcode);
8267 imm_expr.X_op = O_absent;
97d87491
RS
8268 offset_expr.X_op = O_absent;
8269 offset_reloc[0] = BFD_RELOC_UNUSED;
8270 offset_reloc[1] = BFD_RELOC_UNUSED;
8271 offset_reloc[2] = BFD_RELOC_UNUSED;
8272 relax_char = 0;
8273
8274 memset (&arg, 0, sizeof (arg));
8275 arg.insn = insn;
8276 arg.token = tokens;
8277 arg.argnum = 1;
8278 arg.last_regno = ILLEGAL_REG;
8279 arg.dest_regno = ILLEGAL_REG;
97d87491
RS
8280 relax_char = 0;
8281 for (args = opcode->args;; ++args)
8282 {
8283 int c;
8284
8285 if (arg.token->type == OT_END)
8286 {
8287 offsetT value;
8288
8289 /* Handle unary instructions in which only one operand is given.
8290 The source is then the same as the destination. */
8291 if (arg.opnum == 1 && *args == ',')
8292 {
8293 operand = decode_mips16_operand (args[1], FALSE);
8294 if (operand && mips_optional_operand_p (operand))
8295 {
8296 arg.token = tokens;
8297 arg.argnum = 1;
8298 continue;
8299 }
8300 }
8301
8302 /* Fail the match if there were too few operands. */
8303 if (*args)
8304 return FALSE;
8305
8306 /* Successful match. Stuff the immediate value in now, if
8307 we can. */
e3de51ce 8308 clear_insn_error ();
97d87491
RS
8309 if (opcode->pinfo == INSN_MACRO)
8310 {
8311 gas_assert (relax_char == 0 || relax_char == 'p');
8312 gas_assert (*offset_reloc == BFD_RELOC_UNUSED);
8313 }
8314 else if (relax_char
8315 && offset_expr.X_op == O_constant
82d808ed 8316 && !pcrel
97d87491
RS
8317 && calculate_reloc (*offset_reloc,
8318 offset_expr.X_add_number,
8319 &value))
8320 {
8321 mips16_immed (NULL, 0, relax_char, *offset_reloc, value,
7fd53920 8322 required_insn_length, &insn->insn_opcode);
97d87491
RS
8323 offset_expr.X_op = O_absent;
8324 *offset_reloc = BFD_RELOC_UNUSED;
8325 }
8326 else if (relax_char && *offset_reloc != BFD_RELOC_UNUSED)
8327 {
7fd53920 8328 if (required_insn_length == 2)
e3de51ce 8329 set_insn_error (0, _("invalid unextended operand value"));
25499ac7 8330 else if (!mips_opcode_32bit_p (opcode))
1da43acc
MR
8331 {
8332 forced_insn_length = 4;
8333 insn->insn_opcode |= MIPS16_EXTEND;
8334 }
97d87491
RS
8335 }
8336 else if (relax_char)
8337 *offset_reloc = (int) BFD_RELOC_UNUSED + relax_char;
8338
8339 check_completed_insn (&arg);
8340 return TRUE;
8341 }
8342
8343 /* Fail the match if the line has too many operands. */
8344 if (*args == 0)
8345 return FALSE;
8346
8347 /* Handle characters that need to match exactly. */
8348 if (*args == '(' || *args == ')' || *args == ',')
8349 {
8350 if (match_char (&arg, *args))
8351 continue;
8352 return FALSE;
8353 }
8354
8355 arg.opnum += 1;
8356 c = *args;
8357 switch (c)
8358 {
8359 case 'p':
8360 case 'q':
8361 case 'A':
8362 case 'B':
8363 case 'E':
25499ac7
MR
8364 case 'V':
8365 case 'u':
97d87491
RS
8366 relax_char = c;
8367 break;
8368
8369 case 'I':
1a00e612
RS
8370 if (!match_const_int (&arg, &imm_expr.X_add_number))
8371 return FALSE;
8372 imm_expr.X_op = O_constant;
bad1aba3 8373 if (GPR_SIZE == 32)
97d87491
RS
8374 normalize_constant_expr (&imm_expr);
8375 continue;
8376
8377 case 'a':
8378 case 'i':
8379 *offset_reloc = BFD_RELOC_MIPS16_JMP;
97d87491
RS
8380 break;
8381 }
8382
7fd53920 8383 operand = decode_mips16_operand (c, mips_opcode_32bit_p (opcode));
97d87491
RS
8384 if (!operand)
8385 abort ();
8386
82d808ed
MR
8387 if (operand->type == OP_PCREL)
8388 pcrel = TRUE;
8389 else
97d87491
RS
8390 {
8391 ext_operand = decode_mips16_operand (c, TRUE);
8392 if (operand != ext_operand)
8393 {
8394 if (arg.token->type == OT_CHAR && arg.token->u.ch == '(')
8395 {
8396 offset_expr.X_op = O_constant;
8397 offset_expr.X_add_number = 0;
8398 relax_char = c;
8399 continue;
8400 }
8401
1a7bf198 8402 if (!match_expression (&arg, &offset_expr, offset_reloc))
97d87491
RS
8403 return FALSE;
8404
8405 /* '8' is used for SLTI(U) and has traditionally not
8406 been allowed to take relocation operators. */
8407 if (offset_reloc[0] != BFD_RELOC_UNUSED
8408 && (ext_operand->size != 16 || c == '8'))
e295202f
MR
8409 {
8410 match_not_constant (&arg);
8411 return FALSE;
8412 }
97d87491 8413
c96425c5
MR
8414 if (offset_expr.X_op == O_big)
8415 {
8416 match_out_of_range (&arg);
8417 return FALSE;
8418 }
8419
97d87491
RS
8420 relax_char = c;
8421 continue;
8422 }
8423 }
8424
8425 if (mips_optional_operand_p (operand)
8426 && args[1] == ','
8427 && (arg.token[0].type != OT_REG
8428 || arg.token[1].type == OT_END))
8429 {
8430 /* Assume that the register has been elided and is the
8431 same as the first operand. */
8432 arg.token = tokens;
8433 arg.argnum = 1;
8434 }
8435
8436 if (!match_operand (&arg, operand))
8437 return FALSE;
8438 }
8439}
8440
60f20e8b
RS
8441/* Record that the current instruction is invalid for the current ISA. */
8442
8443static void
8444match_invalid_for_isa (void)
8445{
8446 set_insn_error_ss
1661c76c 8447 (0, _("opcode not supported on this processor: %s (%s)"),
60f20e8b
RS
8448 mips_cpu_info_from_arch (mips_opts.arch)->name,
8449 mips_cpu_info_from_isa (mips_opts.isa)->name);
8450}
8451
8452/* Try to match TOKENS against a series of opcode entries, starting at FIRST.
8453 Return true if a definite match or failure was found, storing any match
8454 in INSN. OPCODE_EXTRA is a value that should be ORed into the opcode
8455 (to handle things like VU0 suffixes). LAX_MATCH is true if we have already
8456 tried and failed to match under normal conditions and now want to try a
8457 more relaxed match. */
8458
8459static bfd_boolean
8460match_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8461 const struct mips_opcode *past, struct mips_operand_token *tokens,
8462 int opcode_extra, bfd_boolean lax_match)
8463{
8464 const struct mips_opcode *opcode;
8465 const struct mips_opcode *invalid_delay_slot;
8466 bfd_boolean seen_valid_for_isa, seen_valid_for_size;
8467
8468 /* Search for a match, ignoring alternatives that don't satisfy the
8469 current ISA or forced_length. */
8470 invalid_delay_slot = 0;
8471 seen_valid_for_isa = FALSE;
8472 seen_valid_for_size = FALSE;
8473 opcode = first;
8474 do
8475 {
8476 gas_assert (strcmp (opcode->name, first->name) == 0);
8477 if (is_opcode_valid (opcode))
8478 {
8479 seen_valid_for_isa = TRUE;
8480 if (is_size_valid (opcode))
8481 {
8482 bfd_boolean delay_slot_ok;
8483
8484 seen_valid_for_size = TRUE;
8485 delay_slot_ok = is_delay_slot_valid (opcode);
8486 if (match_insn (insn, opcode, tokens, opcode_extra,
8487 lax_match, delay_slot_ok))
8488 {
8489 if (!delay_slot_ok)
8490 {
8491 if (!invalid_delay_slot)
8492 invalid_delay_slot = opcode;
8493 }
8494 else
8495 return TRUE;
8496 }
8497 }
8498 }
8499 ++opcode;
8500 }
8501 while (opcode < past && strcmp (opcode->name, first->name) == 0);
8502
8503 /* If the only matches we found had the wrong length for the delay slot,
8504 pick the first such match. We'll issue an appropriate warning later. */
8505 if (invalid_delay_slot)
8506 {
8507 if (match_insn (insn, invalid_delay_slot, tokens, opcode_extra,
8508 lax_match, TRUE))
8509 return TRUE;
8510 abort ();
8511 }
8512
8513 /* Handle the case where we didn't try to match an instruction because
8514 all the alternatives were incompatible with the current ISA. */
8515 if (!seen_valid_for_isa)
8516 {
8517 match_invalid_for_isa ();
8518 return TRUE;
8519 }
8520
8521 /* Handle the case where we didn't try to match an instruction because
8522 all the alternatives were of the wrong size. */
8523 if (!seen_valid_for_size)
8524 {
8525 if (mips_opts.insn32)
1661c76c 8526 set_insn_error (0, _("opcode not supported in the `insn32' mode"));
60f20e8b
RS
8527 else
8528 set_insn_error_i
1661c76c 8529 (0, _("unrecognized %d-bit version of microMIPS opcode"),
60f20e8b
RS
8530 8 * forced_insn_length);
8531 return TRUE;
8532 }
8533
8534 return FALSE;
8535}
8536
8537/* Like match_insns, but for MIPS16. */
8538
8539static bfd_boolean
8540match_mips16_insns (struct mips_cl_insn *insn, const struct mips_opcode *first,
8541 struct mips_operand_token *tokens)
8542{
8543 const struct mips_opcode *opcode;
8544 bfd_boolean seen_valid_for_isa;
7fd53920 8545 bfd_boolean seen_valid_for_size;
60f20e8b
RS
8546
8547 /* Search for a match, ignoring alternatives that don't satisfy the
8548 current ISA. There are no separate entries for extended forms so
8549 we deal with forced_length later. */
8550 seen_valid_for_isa = FALSE;
7fd53920 8551 seen_valid_for_size = FALSE;
60f20e8b
RS
8552 opcode = first;
8553 do
8554 {
8555 gas_assert (strcmp (opcode->name, first->name) == 0);
8556 if (is_opcode_valid_16 (opcode))
8557 {
8558 seen_valid_for_isa = TRUE;
7fd53920
MR
8559 if (is_size_valid_16 (opcode))
8560 {
8561 seen_valid_for_size = TRUE;
8562 if (match_mips16_insn (insn, opcode, tokens))
8563 return TRUE;
8564 }
60f20e8b
RS
8565 }
8566 ++opcode;
8567 }
8568 while (opcode < &mips16_opcodes[bfd_mips16_num_opcodes]
8569 && strcmp (opcode->name, first->name) == 0);
8570
8571 /* Handle the case where we didn't try to match an instruction because
8572 all the alternatives were incompatible with the current ISA. */
8573 if (!seen_valid_for_isa)
8574 {
8575 match_invalid_for_isa ();
8576 return TRUE;
8577 }
8578
7fd53920
MR
8579 /* Handle the case where we didn't try to match an instruction because
8580 all the alternatives were of the wrong size. */
8581 if (!seen_valid_for_size)
8582 {
8583 if (forced_insn_length == 2)
8584 set_insn_error
8585 (0, _("unrecognized unextended version of MIPS16 opcode"));
8586 else
8587 set_insn_error
8588 (0, _("unrecognized extended version of MIPS16 opcode"));
8589 return TRUE;
8590 }
8591
60f20e8b
RS
8592 return FALSE;
8593}
8594
584892a6
RS
8595/* Set up global variables for the start of a new macro. */
8596
8597static void
8598macro_start (void)
8599{
8600 memset (&mips_macro_warning.sizes, 0, sizeof (mips_macro_warning.sizes));
df58fc94
RS
8601 memset (&mips_macro_warning.first_insn_sizes, 0,
8602 sizeof (mips_macro_warning.first_insn_sizes));
8603 memset (&mips_macro_warning.insns, 0, sizeof (mips_macro_warning.insns));
584892a6 8604 mips_macro_warning.delay_slot_p = (mips_opts.noreorder
11625dd8 8605 && delayed_branch_p (&history[0]));
7bd374a4
MR
8606 if (history[0].frag
8607 && history[0].frag->fr_type == rs_machine_dependent
8608 && RELAX_MICROMIPS_P (history[0].frag->fr_subtype)
8609 && RELAX_MICROMIPS_NODS (history[0].frag->fr_subtype))
8610 mips_macro_warning.delay_slot_length = 0;
8611 else
8612 switch (history[0].insn_mo->pinfo2
8613 & (INSN2_BRANCH_DELAY_32BIT | INSN2_BRANCH_DELAY_16BIT))
8614 {
8615 case INSN2_BRANCH_DELAY_32BIT:
8616 mips_macro_warning.delay_slot_length = 4;
8617 break;
8618 case INSN2_BRANCH_DELAY_16BIT:
8619 mips_macro_warning.delay_slot_length = 2;
8620 break;
8621 default:
8622 mips_macro_warning.delay_slot_length = 0;
8623 break;
8624 }
df58fc94 8625 mips_macro_warning.first_frag = NULL;
584892a6
RS
8626}
8627
df58fc94
RS
8628/* Given that a macro is longer than one instruction or of the wrong size,
8629 return the appropriate warning for it. Return null if no warning is
8630 needed. SUBTYPE is a bitmask of RELAX_DELAY_SLOT, RELAX_DELAY_SLOT_16BIT,
8631 RELAX_DELAY_SLOT_SIZE_FIRST, RELAX_DELAY_SLOT_SIZE_SECOND,
8632 and RELAX_NOMACRO. */
584892a6
RS
8633
8634static const char *
8635macro_warning (relax_substateT subtype)
8636{
8637 if (subtype & RELAX_DELAY_SLOT)
1661c76c 8638 return _("macro instruction expanded into multiple instructions"
584892a6
RS
8639 " in a branch delay slot");
8640 else if (subtype & RELAX_NOMACRO)
1661c76c 8641 return _("macro instruction expanded into multiple instructions");
df58fc94
RS
8642 else if (subtype & (RELAX_DELAY_SLOT_SIZE_FIRST
8643 | RELAX_DELAY_SLOT_SIZE_SECOND))
8644 return ((subtype & RELAX_DELAY_SLOT_16BIT)
1661c76c 8645 ? _("macro instruction expanded into a wrong size instruction"
df58fc94 8646 " in a 16-bit branch delay slot")
1661c76c 8647 : _("macro instruction expanded into a wrong size instruction"
df58fc94 8648 " in a 32-bit branch delay slot"));
584892a6
RS
8649 else
8650 return 0;
8651}
8652
8653/* Finish up a macro. Emit warnings as appropriate. */
8654
8655static void
8656macro_end (void)
8657{
df58fc94
RS
8658 /* Relaxation warning flags. */
8659 relax_substateT subtype = 0;
8660
8661 /* Check delay slot size requirements. */
8662 if (mips_macro_warning.delay_slot_length == 2)
8663 subtype |= RELAX_DELAY_SLOT_16BIT;
8664 if (mips_macro_warning.delay_slot_length != 0)
584892a6 8665 {
df58fc94
RS
8666 if (mips_macro_warning.delay_slot_length
8667 != mips_macro_warning.first_insn_sizes[0])
8668 subtype |= RELAX_DELAY_SLOT_SIZE_FIRST;
8669 if (mips_macro_warning.delay_slot_length
8670 != mips_macro_warning.first_insn_sizes[1])
8671 subtype |= RELAX_DELAY_SLOT_SIZE_SECOND;
8672 }
584892a6 8673
df58fc94
RS
8674 /* Check instruction count requirements. */
8675 if (mips_macro_warning.insns[0] > 1 || mips_macro_warning.insns[1] > 1)
8676 {
8677 if (mips_macro_warning.insns[1] > mips_macro_warning.insns[0])
584892a6
RS
8678 subtype |= RELAX_SECOND_LONGER;
8679 if (mips_opts.warn_about_macros)
8680 subtype |= RELAX_NOMACRO;
8681 if (mips_macro_warning.delay_slot_p)
8682 subtype |= RELAX_DELAY_SLOT;
df58fc94 8683 }
584892a6 8684
df58fc94
RS
8685 /* If both alternatives fail to fill a delay slot correctly,
8686 emit the warning now. */
8687 if ((subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0
8688 && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0)
8689 {
8690 relax_substateT s;
8691 const char *msg;
8692
8693 s = subtype & (RELAX_DELAY_SLOT_16BIT
8694 | RELAX_DELAY_SLOT_SIZE_FIRST
8695 | RELAX_DELAY_SLOT_SIZE_SECOND);
8696 msg = macro_warning (s);
8697 if (msg != NULL)
8698 as_warn ("%s", msg);
8699 subtype &= ~s;
8700 }
8701
8702 /* If both implementations are longer than 1 instruction, then emit the
8703 warning now. */
8704 if (mips_macro_warning.insns[0] > 1 && mips_macro_warning.insns[1] > 1)
8705 {
8706 relax_substateT s;
8707 const char *msg;
8708
8709 s = subtype & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT);
8710 msg = macro_warning (s);
8711 if (msg != NULL)
8712 as_warn ("%s", msg);
8713 subtype &= ~s;
584892a6 8714 }
df58fc94
RS
8715
8716 /* If any flags still set, then one implementation might need a warning
8717 and the other either will need one of a different kind or none at all.
8718 Pass any remaining flags over to relaxation. */
8719 if (mips_macro_warning.first_frag != NULL)
8720 mips_macro_warning.first_frag->fr_subtype |= subtype;
584892a6
RS
8721}
8722
df58fc94
RS
8723/* Instruction operand formats used in macros that vary between
8724 standard MIPS and microMIPS code. */
8725
833794fc 8726static const char * const brk_fmt[2][2] = { { "c", "c" }, { "mF", "c" } };
df58fc94
RS
8727static const char * const cop12_fmt[2] = { "E,o(b)", "E,~(b)" };
8728static const char * const jalr_fmt[2] = { "d,s", "t,s" };
8729static const char * const lui_fmt[2] = { "t,u", "s,u" };
8730static const char * const mem12_fmt[2] = { "t,o(b)", "t,~(b)" };
833794fc 8731static const char * const mfhl_fmt[2][2] = { { "d", "d" }, { "mj", "s" } };
df58fc94
RS
8732static const char * const shft_fmt[2] = { "d,w,<", "t,r,<" };
8733static const char * const trap_fmt[2] = { "s,t,q", "s,t,|" };
8734
833794fc 8735#define BRK_FMT (brk_fmt[mips_opts.micromips][mips_opts.insn32])
7361da2c
AB
8736#define COP12_FMT (ISA_IS_R6 (mips_opts.isa) ? "E,+:(d)" \
8737 : cop12_fmt[mips_opts.micromips])
df58fc94
RS
8738#define JALR_FMT (jalr_fmt[mips_opts.micromips])
8739#define LUI_FMT (lui_fmt[mips_opts.micromips])
8740#define MEM12_FMT (mem12_fmt[mips_opts.micromips])
7361da2c
AB
8741#define LL_SC_FMT (ISA_IS_R6 (mips_opts.isa) ? "t,+j(b)" \
8742 : mem12_fmt[mips_opts.micromips])
833794fc 8743#define MFHL_FMT (mfhl_fmt[mips_opts.micromips][mips_opts.insn32])
df58fc94
RS
8744#define SHFT_FMT (shft_fmt[mips_opts.micromips])
8745#define TRAP_FMT (trap_fmt[mips_opts.micromips])
8746
6e1304d8
RS
8747/* Read a macro's relocation codes from *ARGS and store them in *R.
8748 The first argument in *ARGS will be either the code for a single
8749 relocation or -1 followed by the three codes that make up a
8750 composite relocation. */
8751
8752static void
8753macro_read_relocs (va_list *args, bfd_reloc_code_real_type *r)
8754{
8755 int i, next;
8756
8757 next = va_arg (*args, int);
8758 if (next >= 0)
8759 r[0] = (bfd_reloc_code_real_type) next;
8760 else
f2ae14a1
RS
8761 {
8762 for (i = 0; i < 3; i++)
8763 r[i] = (bfd_reloc_code_real_type) va_arg (*args, int);
8764 /* This function is only used for 16-bit relocation fields.
8765 To make the macro code simpler, treat an unrelocated value
8766 in the same way as BFD_RELOC_LO16. */
8767 if (r[0] == BFD_RELOC_UNUSED)
8768 r[0] = BFD_RELOC_LO16;
8769 }
6e1304d8
RS
8770}
8771
252b5132
RH
8772/* Build an instruction created by a macro expansion. This is passed
8773 a pointer to the count of instructions created so far, an
8774 expression, the name of the instruction to build, an operand format
8775 string, and corresponding arguments. */
8776
252b5132 8777static void
67c0d1eb 8778macro_build (expressionS *ep, const char *name, const char *fmt, ...)
252b5132 8779{
df58fc94 8780 const struct mips_opcode *mo = NULL;
f6688943 8781 bfd_reloc_code_real_type r[3];
df58fc94 8782 const struct mips_opcode *amo;
e077a1c8 8783 const struct mips_operand *operand;
df58fc94
RS
8784 struct hash_control *hash;
8785 struct mips_cl_insn insn;
252b5132 8786 va_list args;
e077a1c8 8787 unsigned int uval;
252b5132 8788
252b5132 8789 va_start (args, fmt);
252b5132 8790
252b5132
RH
8791 if (mips_opts.mips16)
8792 {
03ea81db 8793 mips16_macro_build (ep, name, fmt, &args);
252b5132
RH
8794 va_end (args);
8795 return;
8796 }
8797
f6688943
TS
8798 r[0] = BFD_RELOC_UNUSED;
8799 r[1] = BFD_RELOC_UNUSED;
8800 r[2] = BFD_RELOC_UNUSED;
df58fc94
RS
8801 hash = mips_opts.micromips ? micromips_op_hash : op_hash;
8802 amo = (struct mips_opcode *) hash_find (hash, name);
8803 gas_assert (amo);
8804 gas_assert (strcmp (name, amo->name) == 0);
1e915849 8805
df58fc94 8806 do
8b082fb1
TS
8807 {
8808 /* Search until we get a match for NAME. It is assumed here that
df58fc94 8809 macros will never generate MDMX, MIPS-3D, or MT instructions.
33eaf5de 8810 We try to match an instruction that fulfills the branch delay
df58fc94
RS
8811 slot instruction length requirement (if any) of the previous
8812 instruction. While doing this we record the first instruction
8813 seen that matches all the other conditions and use it anyway
8814 if the requirement cannot be met; we will issue an appropriate
8815 warning later on. */
8816 if (strcmp (fmt, amo->args) == 0
8817 && amo->pinfo != INSN_MACRO
8818 && is_opcode_valid (amo)
8819 && is_size_valid (amo))
8820 {
8821 if (is_delay_slot_valid (amo))
8822 {
8823 mo = amo;
8824 break;
8825 }
8826 else if (!mo)
8827 mo = amo;
8828 }
8b082fb1 8829
df58fc94
RS
8830 ++amo;
8831 gas_assert (amo->name);
252b5132 8832 }
df58fc94 8833 while (strcmp (name, amo->name) == 0);
252b5132 8834
df58fc94 8835 gas_assert (mo);
1e915849 8836 create_insn (&insn, mo);
e077a1c8 8837 for (; *fmt; ++fmt)
252b5132 8838 {
e077a1c8 8839 switch (*fmt)
252b5132 8840 {
252b5132
RH
8841 case ',':
8842 case '(':
8843 case ')':
252b5132 8844 case 'z':
e077a1c8 8845 break;
252b5132
RH
8846
8847 case 'i':
8848 case 'j':
6e1304d8 8849 macro_read_relocs (&args, r);
9c2799c2 8850 gas_assert (*r == BFD_RELOC_GPREL16
e391c024
RS
8851 || *r == BFD_RELOC_MIPS_HIGHER
8852 || *r == BFD_RELOC_HI16_S
8853 || *r == BFD_RELOC_LO16
14c80123
MR
8854 || *r == BFD_RELOC_MIPS_GOT_OFST
8855 || (mips_opts.micromips
8856 && (*r == BFD_RELOC_16
8857 || *r == BFD_RELOC_MIPS_GOT16
8858 || *r == BFD_RELOC_MIPS_CALL16
8859 || *r == BFD_RELOC_MIPS_GOT_HI16
8860 || *r == BFD_RELOC_MIPS_GOT_LO16
8861 || *r == BFD_RELOC_MIPS_CALL_HI16
8862 || *r == BFD_RELOC_MIPS_CALL_LO16
8863 || *r == BFD_RELOC_MIPS_SUB
8864 || *r == BFD_RELOC_MIPS_GOT_PAGE
8865 || *r == BFD_RELOC_MIPS_HIGHEST
8866 || *r == BFD_RELOC_MIPS_GOT_DISP
8867 || *r == BFD_RELOC_MIPS_TLS_GD
8868 || *r == BFD_RELOC_MIPS_TLS_LDM
8869 || *r == BFD_RELOC_MIPS_TLS_DTPREL_HI16
8870 || *r == BFD_RELOC_MIPS_TLS_DTPREL_LO16
8871 || *r == BFD_RELOC_MIPS_TLS_GOTTPREL
8872 || *r == BFD_RELOC_MIPS_TLS_TPREL_HI16
8873 || *r == BFD_RELOC_MIPS_TLS_TPREL_LO16)));
e077a1c8 8874 break;
e391c024
RS
8875
8876 case 'o':
8877 macro_read_relocs (&args, r);
e077a1c8 8878 break;
252b5132
RH
8879
8880 case 'u':
6e1304d8 8881 macro_read_relocs (&args, r);
9c2799c2 8882 gas_assert (ep != NULL
90ecf173
MR
8883 && (ep->X_op == O_constant
8884 || (ep->X_op == O_symbol
8885 && (*r == BFD_RELOC_MIPS_HIGHEST
8886 || *r == BFD_RELOC_HI16_S
8887 || *r == BFD_RELOC_HI16
8888 || *r == BFD_RELOC_GPREL16
8889 || *r == BFD_RELOC_MIPS_GOT_HI16
8890 || *r == BFD_RELOC_MIPS_CALL_HI16))));
e077a1c8 8891 break;
252b5132
RH
8892
8893 case 'p':
9c2799c2 8894 gas_assert (ep != NULL);
bad36eac 8895
252b5132
RH
8896 /*
8897 * This allows macro() to pass an immediate expression for
8898 * creating short branches without creating a symbol.
bad36eac
DJ
8899 *
8900 * We don't allow branch relaxation for these branches, as
8901 * they should only appear in ".set nomacro" anyway.
252b5132
RH
8902 */
8903 if (ep->X_op == O_constant)
8904 {
df58fc94
RS
8905 /* For microMIPS we always use relocations for branches.
8906 So we should not resolve immediate values. */
8907 gas_assert (!mips_opts.micromips);
8908
bad36eac
DJ
8909 if ((ep->X_add_number & 3) != 0)
8910 as_bad (_("branch to misaligned address (0x%lx)"),
8911 (unsigned long) ep->X_add_number);
8912 if ((ep->X_add_number + 0x20000) & ~0x3ffff)
8913 as_bad (_("branch address range overflow (0x%lx)"),
8914 (unsigned long) ep->X_add_number);
252b5132
RH
8915 insn.insn_opcode |= (ep->X_add_number >> 2) & 0xffff;
8916 ep = NULL;
8917 }
8918 else
0b25d3e6 8919 *r = BFD_RELOC_16_PCREL_S2;
e077a1c8 8920 break;
252b5132
RH
8921
8922 case 'a':
9c2799c2 8923 gas_assert (ep != NULL);
f6688943 8924 *r = BFD_RELOC_MIPS_JMP;
e077a1c8 8925 break;
d43b4baf 8926
252b5132 8927 default:
e077a1c8
RS
8928 operand = (mips_opts.micromips
8929 ? decode_micromips_operand (fmt)
8930 : decode_mips_operand (fmt));
8931 if (!operand)
8932 abort ();
8933
8934 uval = va_arg (args, int);
8935 if (operand->type == OP_CLO_CLZ_DEST)
8936 uval |= (uval << 5);
8937 insn_insert_operand (&insn, operand, uval);
8938
7361da2c 8939 if (*fmt == '+' || *fmt == 'm' || *fmt == '-')
e077a1c8
RS
8940 ++fmt;
8941 break;
252b5132 8942 }
252b5132
RH
8943 }
8944 va_end (args);
9c2799c2 8945 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 8946
df58fc94 8947 append_insn (&insn, ep, r, TRUE);
252b5132
RH
8948}
8949
8950static void
67c0d1eb 8951mips16_macro_build (expressionS *ep, const char *name, const char *fmt,
03ea81db 8952 va_list *args)
252b5132 8953{
1e915849 8954 struct mips_opcode *mo;
252b5132 8955 struct mips_cl_insn insn;
e077a1c8 8956 const struct mips_operand *operand;
f6688943
TS
8957 bfd_reloc_code_real_type r[3]
8958 = {BFD_RELOC_UNUSED, BFD_RELOC_UNUSED, BFD_RELOC_UNUSED};
252b5132 8959
1e915849 8960 mo = (struct mips_opcode *) hash_find (mips16_op_hash, name);
9c2799c2
NC
8961 gas_assert (mo);
8962 gas_assert (strcmp (name, mo->name) == 0);
252b5132 8963
1e915849 8964 while (strcmp (fmt, mo->args) != 0 || mo->pinfo == INSN_MACRO)
252b5132 8965 {
1e915849 8966 ++mo;
9c2799c2
NC
8967 gas_assert (mo->name);
8968 gas_assert (strcmp (name, mo->name) == 0);
252b5132
RH
8969 }
8970
1e915849 8971 create_insn (&insn, mo);
e077a1c8 8972 for (; *fmt; ++fmt)
252b5132
RH
8973 {
8974 int c;
8975
e077a1c8 8976 c = *fmt;
252b5132
RH
8977 switch (c)
8978 {
252b5132
RH
8979 case ',':
8980 case '(':
8981 case ')':
e077a1c8 8982 break;
252b5132 8983
d8722d76 8984 case '.':
252b5132
RH
8985 case 'S':
8986 case 'P':
8987 case 'R':
e077a1c8 8988 break;
252b5132
RH
8989
8990 case '<':
252b5132 8991 case '5':
d8722d76 8992 case 'F':
252b5132
RH
8993 case 'H':
8994 case 'W':
8995 case 'D':
8996 case 'j':
8997 case '8':
8998 case 'V':
8999 case 'C':
9000 case 'U':
9001 case 'k':
9002 case 'K':
9003 case 'p':
9004 case 'q':
9005 {
b886a2ab
RS
9006 offsetT value;
9007
9c2799c2 9008 gas_assert (ep != NULL);
252b5132
RH
9009
9010 if (ep->X_op != O_constant)
874e8986 9011 *r = (int) BFD_RELOC_UNUSED + c;
b886a2ab 9012 else if (calculate_reloc (*r, ep->X_add_number, &value))
252b5132 9013 {
b886a2ab 9014 mips16_immed (NULL, 0, c, *r, value, 0, &insn.insn_opcode);
252b5132 9015 ep = NULL;
f6688943 9016 *r = BFD_RELOC_UNUSED;
252b5132
RH
9017 }
9018 }
e077a1c8 9019 break;
252b5132 9020
e077a1c8
RS
9021 default:
9022 operand = decode_mips16_operand (c, FALSE);
9023 if (!operand)
9024 abort ();
252b5132 9025
4a06e5a2 9026 insn_insert_operand (&insn, operand, va_arg (*args, int));
e077a1c8
RS
9027 break;
9028 }
252b5132
RH
9029 }
9030
9c2799c2 9031 gas_assert (*r == BFD_RELOC_UNUSED ? ep == NULL : ep != NULL);
252b5132 9032
df58fc94 9033 append_insn (&insn, ep, r, TRUE);
252b5132
RH
9034}
9035
438c16b8
TS
9036/*
9037 * Generate a "jalr" instruction with a relocation hint to the called
9038 * function. This occurs in NewABI PIC code.
9039 */
9040static void
df58fc94 9041macro_build_jalr (expressionS *ep, int cprestore)
438c16b8 9042{
df58fc94
RS
9043 static const bfd_reloc_code_real_type jalr_relocs[2]
9044 = { BFD_RELOC_MIPS_JALR, BFD_RELOC_MICROMIPS_JALR };
9045 bfd_reloc_code_real_type jalr_reloc = jalr_relocs[mips_opts.micromips];
9046 const char *jalr;
685736be 9047 char *f = NULL;
b34976b6 9048
1180b5a4 9049 if (MIPS_JALR_HINT_P (ep))
f21f8242 9050 {
cc3d92a5 9051 frag_grow (8);
f21f8242
AO
9052 f = frag_more (0);
9053 }
2906b037 9054 if (mips_opts.micromips)
df58fc94 9055 {
833794fc
MR
9056 jalr = ((mips_opts.noreorder && !cprestore) || mips_opts.insn32
9057 ? "jalr" : "jalrs");
e64af278 9058 if (MIPS_JALR_HINT_P (ep)
833794fc 9059 || mips_opts.insn32
e64af278 9060 || (history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
df58fc94
RS
9061 macro_build (NULL, jalr, "t,s", RA, PIC_CALL_REG);
9062 else
9063 macro_build (NULL, jalr, "mj", PIC_CALL_REG);
9064 }
2906b037
MR
9065 else
9066 macro_build (NULL, "jalr", "d,s", RA, PIC_CALL_REG);
1180b5a4 9067 if (MIPS_JALR_HINT_P (ep))
df58fc94 9068 fix_new_exp (frag_now, f - frag_now->fr_literal, 4, ep, FALSE, jalr_reloc);
438c16b8
TS
9069}
9070
252b5132
RH
9071/*
9072 * Generate a "lui" instruction.
9073 */
9074static void
67c0d1eb 9075macro_build_lui (expressionS *ep, int regnum)
252b5132 9076{
9c2799c2 9077 gas_assert (! mips_opts.mips16);
252b5132 9078
df58fc94 9079 if (ep->X_op != O_constant)
252b5132 9080 {
9c2799c2 9081 gas_assert (ep->X_op == O_symbol);
bbe506e8
TS
9082 /* _gp_disp is a special case, used from s_cpload.
9083 __gnu_local_gp is used if mips_no_shared. */
9c2799c2 9084 gas_assert (mips_pic == NO_PIC
78e1bb40 9085 || (! HAVE_NEWABI
aa6975fb
ILT
9086 && strcmp (S_GET_NAME (ep->X_add_symbol), "_gp_disp") == 0)
9087 || (! mips_in_shared
bbe506e8
TS
9088 && strcmp (S_GET_NAME (ep->X_add_symbol),
9089 "__gnu_local_gp") == 0));
252b5132
RH
9090 }
9091
df58fc94 9092 macro_build (ep, "lui", LUI_FMT, regnum, BFD_RELOC_HI16_S);
252b5132
RH
9093}
9094
885add95
CD
9095/* Generate a sequence of instructions to do a load or store from a constant
9096 offset off of a base register (breg) into/from a target register (treg),
9097 using AT if necessary. */
9098static void
67c0d1eb
RS
9099macro_build_ldst_constoffset (expressionS *ep, const char *op,
9100 int treg, int breg, int dbl)
885add95 9101{
9c2799c2 9102 gas_assert (ep->X_op == O_constant);
885add95 9103
256ab948 9104 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9105 if (!dbl)
9106 normalize_constant_expr (ep);
256ab948 9107
67c1ffbe 9108 /* Right now, this routine can only handle signed 32-bit constants. */
ecd13cd3 9109 if (! IS_SEXT_32BIT_NUM(ep->X_add_number + 0x8000))
885add95
CD
9110 as_warn (_("operand overflow"));
9111
9112 if (IS_SEXT_16BIT_NUM(ep->X_add_number))
9113 {
9114 /* Signed 16-bit offset will fit in the op. Easy! */
67c0d1eb 9115 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, breg);
885add95
CD
9116 }
9117 else
9118 {
9119 /* 32-bit offset, need multiple instructions and AT, like:
9120 lui $tempreg,const_hi (BFD_RELOC_HI16_S)
9121 addu $tempreg,$tempreg,$breg
9122 <op> $treg,const_lo($tempreg) (BFD_RELOC_LO16)
9123 to handle the complete offset. */
67c0d1eb
RS
9124 macro_build_lui (ep, AT);
9125 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, AT, breg);
9126 macro_build (ep, op, "t,o(b)", treg, BFD_RELOC_LO16, AT);
885add95 9127
741fe287 9128 if (!mips_opts.at)
1661c76c 9129 as_bad (_("macro used $at after \".set noat\""));
885add95
CD
9130 }
9131}
9132
252b5132
RH
9133/* set_at()
9134 * Generates code to set the $at register to true (one)
9135 * if reg is less than the immediate expression.
9136 */
9137static void
67c0d1eb 9138set_at (int reg, int unsignedp)
252b5132 9139{
b0e6f033 9140 if (imm_expr.X_add_number >= -0x8000
252b5132 9141 && imm_expr.X_add_number < 0x8000)
67c0d1eb
RS
9142 macro_build (&imm_expr, unsignedp ? "sltiu" : "slti", "t,r,j",
9143 AT, reg, BFD_RELOC_LO16);
252b5132
RH
9144 else
9145 {
bad1aba3 9146 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 9147 macro_build (NULL, unsignedp ? "sltu" : "slt", "d,v,t", AT, reg, AT);
252b5132
RH
9148 }
9149}
9150
252b5132
RH
9151/* Count the leading zeroes by performing a binary chop. This is a
9152 bulky bit of source, but performance is a LOT better for the
9153 majority of values than a simple loop to count the bits:
9154 for (lcnt = 0; (lcnt < 32); lcnt++)
9155 if ((v) & (1 << (31 - lcnt)))
9156 break;
9157 However it is not code size friendly, and the gain will drop a bit
9158 on certain cached systems.
9159*/
9160#define COUNT_TOP_ZEROES(v) \
9161 (((v) & ~0xffff) == 0 \
9162 ? ((v) & ~0xff) == 0 \
9163 ? ((v) & ~0xf) == 0 \
9164 ? ((v) & ~0x3) == 0 \
9165 ? ((v) & ~0x1) == 0 \
9166 ? !(v) \
9167 ? 32 \
9168 : 31 \
9169 : 30 \
9170 : ((v) & ~0x7) == 0 \
9171 ? 29 \
9172 : 28 \
9173 : ((v) & ~0x3f) == 0 \
9174 ? ((v) & ~0x1f) == 0 \
9175 ? 27 \
9176 : 26 \
9177 : ((v) & ~0x7f) == 0 \
9178 ? 25 \
9179 : 24 \
9180 : ((v) & ~0xfff) == 0 \
9181 ? ((v) & ~0x3ff) == 0 \
9182 ? ((v) & ~0x1ff) == 0 \
9183 ? 23 \
9184 : 22 \
9185 : ((v) & ~0x7ff) == 0 \
9186 ? 21 \
9187 : 20 \
9188 : ((v) & ~0x3fff) == 0 \
9189 ? ((v) & ~0x1fff) == 0 \
9190 ? 19 \
9191 : 18 \
9192 : ((v) & ~0x7fff) == 0 \
9193 ? 17 \
9194 : 16 \
9195 : ((v) & ~0xffffff) == 0 \
9196 ? ((v) & ~0xfffff) == 0 \
9197 ? ((v) & ~0x3ffff) == 0 \
9198 ? ((v) & ~0x1ffff) == 0 \
9199 ? 15 \
9200 : 14 \
9201 : ((v) & ~0x7ffff) == 0 \
9202 ? 13 \
9203 : 12 \
9204 : ((v) & ~0x3fffff) == 0 \
9205 ? ((v) & ~0x1fffff) == 0 \
9206 ? 11 \
9207 : 10 \
9208 : ((v) & ~0x7fffff) == 0 \
9209 ? 9 \
9210 : 8 \
9211 : ((v) & ~0xfffffff) == 0 \
9212 ? ((v) & ~0x3ffffff) == 0 \
9213 ? ((v) & ~0x1ffffff) == 0 \
9214 ? 7 \
9215 : 6 \
9216 : ((v) & ~0x7ffffff) == 0 \
9217 ? 5 \
9218 : 4 \
9219 : ((v) & ~0x3fffffff) == 0 \
9220 ? ((v) & ~0x1fffffff) == 0 \
9221 ? 3 \
9222 : 2 \
9223 : ((v) & ~0x7fffffff) == 0 \
9224 ? 1 \
9225 : 0)
9226
9227/* load_register()
67c1ffbe 9228 * This routine generates the least number of instructions necessary to load
252b5132
RH
9229 * an absolute expression value into a register.
9230 */
9231static void
67c0d1eb 9232load_register (int reg, expressionS *ep, int dbl)
252b5132
RH
9233{
9234 int freg;
9235 expressionS hi32, lo32;
9236
9237 if (ep->X_op != O_big)
9238 {
9c2799c2 9239 gas_assert (ep->X_op == O_constant);
256ab948
TS
9240
9241 /* Sign-extending 32-bit constants makes their handling easier. */
2051e8c4
MR
9242 if (!dbl)
9243 normalize_constant_expr (ep);
256ab948
TS
9244
9245 if (IS_SEXT_16BIT_NUM (ep->X_add_number))
252b5132
RH
9246 {
9247 /* We can handle 16 bit signed values with an addiu to
9248 $zero. No need to ever use daddiu here, since $zero and
9249 the result are always correct in 32 bit mode. */
67c0d1eb 9250 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9251 return;
9252 }
9253 else if (ep->X_add_number >= 0 && ep->X_add_number < 0x10000)
9254 {
9255 /* We can handle 16 bit unsigned values with an ori to
9256 $zero. */
67c0d1eb 9257 macro_build (ep, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9258 return;
9259 }
256ab948 9260 else if ((IS_SEXT_32BIT_NUM (ep->X_add_number)))
252b5132
RH
9261 {
9262 /* 32 bit values require an lui. */
df58fc94 9263 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9264 if ((ep->X_add_number & 0xffff) != 0)
67c0d1eb 9265 macro_build (ep, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
252b5132
RH
9266 return;
9267 }
9268 }
9269
9270 /* The value is larger than 32 bits. */
9271
bad1aba3 9272 if (!dbl || GPR_SIZE == 32)
252b5132 9273 {
55e08f71
NC
9274 char value[32];
9275
9276 sprintf_vma (value, ep->X_add_number);
1661c76c 9277 as_bad (_("number (0x%s) larger than 32 bits"), value);
67c0d1eb 9278 macro_build (ep, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
252b5132
RH
9279 return;
9280 }
9281
9282 if (ep->X_op != O_big)
9283 {
9284 hi32 = *ep;
9285 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9286 hi32.X_add_number = (valueT) hi32.X_add_number >> 16;
9287 hi32.X_add_number &= 0xffffffff;
9288 lo32 = *ep;
9289 lo32.X_add_number &= 0xffffffff;
9290 }
9291 else
9292 {
9c2799c2 9293 gas_assert (ep->X_add_number > 2);
252b5132
RH
9294 if (ep->X_add_number == 3)
9295 generic_bignum[3] = 0;
9296 else if (ep->X_add_number > 4)
1661c76c 9297 as_bad (_("number larger than 64 bits"));
252b5132
RH
9298 lo32.X_op = O_constant;
9299 lo32.X_add_number = generic_bignum[0] + (generic_bignum[1] << 16);
9300 hi32.X_op = O_constant;
9301 hi32.X_add_number = generic_bignum[2] + (generic_bignum[3] << 16);
9302 }
9303
9304 if (hi32.X_add_number == 0)
9305 freg = 0;
9306 else
9307 {
9308 int shift, bit;
9309 unsigned long hi, lo;
9310
956cd1d6 9311 if (hi32.X_add_number == (offsetT) 0xffffffff)
beae10d5
KH
9312 {
9313 if ((lo32.X_add_number & 0xffff8000) == 0xffff8000)
9314 {
67c0d1eb 9315 macro_build (&lo32, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9316 return;
9317 }
9318 if (lo32.X_add_number & 0x80000000)
9319 {
df58fc94 9320 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
252b5132 9321 if (lo32.X_add_number & 0xffff)
67c0d1eb 9322 macro_build (&lo32, "ori", "t,r,i", reg, reg, BFD_RELOC_LO16);
beae10d5
KH
9323 return;
9324 }
9325 }
252b5132
RH
9326
9327 /* Check for 16bit shifted constant. We know that hi32 is
9328 non-zero, so start the mask on the first bit of the hi32
9329 value. */
9330 shift = 17;
9331 do
beae10d5
KH
9332 {
9333 unsigned long himask, lomask;
9334
9335 if (shift < 32)
9336 {
9337 himask = 0xffff >> (32 - shift);
9338 lomask = (0xffff << shift) & 0xffffffff;
9339 }
9340 else
9341 {
9342 himask = 0xffff << (shift - 32);
9343 lomask = 0;
9344 }
9345 if ((hi32.X_add_number & ~(offsetT) himask) == 0
9346 && (lo32.X_add_number & ~(offsetT) lomask) == 0)
9347 {
9348 expressionS tmp;
9349
9350 tmp.X_op = O_constant;
9351 if (shift < 32)
9352 tmp.X_add_number = ((hi32.X_add_number << (32 - shift))
9353 | (lo32.X_add_number >> shift));
9354 else
9355 tmp.X_add_number = hi32.X_add_number >> (shift - 32);
67c0d1eb 9356 macro_build (&tmp, "ori", "t,r,i", reg, 0, BFD_RELOC_LO16);
df58fc94 9357 macro_build (NULL, (shift >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9358 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9359 return;
9360 }
f9419b05 9361 ++shift;
beae10d5
KH
9362 }
9363 while (shift <= (64 - 16));
252b5132
RH
9364
9365 /* Find the bit number of the lowest one bit, and store the
9366 shifted value in hi/lo. */
9367 hi = (unsigned long) (hi32.X_add_number & 0xffffffff);
9368 lo = (unsigned long) (lo32.X_add_number & 0xffffffff);
9369 if (lo != 0)
9370 {
9371 bit = 0;
9372 while ((lo & 1) == 0)
9373 {
9374 lo >>= 1;
9375 ++bit;
9376 }
9377 lo |= (hi & (((unsigned long) 1 << bit) - 1)) << (32 - bit);
9378 hi >>= bit;
9379 }
9380 else
9381 {
9382 bit = 32;
9383 while ((hi & 1) == 0)
9384 {
9385 hi >>= 1;
9386 ++bit;
9387 }
9388 lo = hi;
9389 hi = 0;
9390 }
9391
9392 /* Optimize if the shifted value is a (power of 2) - 1. */
9393 if ((hi == 0 && ((lo + 1) & lo) == 0)
9394 || (lo == 0xffffffff && ((hi + 1) & hi) == 0))
beae10d5
KH
9395 {
9396 shift = COUNT_TOP_ZEROES ((unsigned int) hi32.X_add_number);
252b5132 9397 if (shift != 0)
beae10d5 9398 {
252b5132
RH
9399 expressionS tmp;
9400
9401 /* This instruction will set the register to be all
9402 ones. */
beae10d5
KH
9403 tmp.X_op = O_constant;
9404 tmp.X_add_number = (offsetT) -1;
67c0d1eb 9405 macro_build (&tmp, "addiu", "t,r,j", reg, 0, BFD_RELOC_LO16);
beae10d5
KH
9406 if (bit != 0)
9407 {
9408 bit += shift;
df58fc94 9409 macro_build (NULL, (bit >= 32) ? "dsll32" : "dsll", SHFT_FMT,
67c0d1eb 9410 reg, reg, (bit >= 32) ? bit - 32 : bit);
beae10d5 9411 }
df58fc94 9412 macro_build (NULL, (shift >= 32) ? "dsrl32" : "dsrl", SHFT_FMT,
67c0d1eb 9413 reg, reg, (shift >= 32) ? shift - 32 : shift);
beae10d5
KH
9414 return;
9415 }
9416 }
252b5132
RH
9417
9418 /* Sign extend hi32 before calling load_register, because we can
9419 generally get better code when we load a sign extended value. */
9420 if ((hi32.X_add_number & 0x80000000) != 0)
beae10d5 9421 hi32.X_add_number |= ~(offsetT) 0xffffffff;
67c0d1eb 9422 load_register (reg, &hi32, 0);
252b5132
RH
9423 freg = reg;
9424 }
9425 if ((lo32.X_add_number & 0xffff0000) == 0)
9426 {
9427 if (freg != 0)
9428 {
df58fc94 9429 macro_build (NULL, "dsll32", SHFT_FMT, reg, freg, 0);
252b5132
RH
9430 freg = reg;
9431 }
9432 }
9433 else
9434 {
9435 expressionS mid16;
9436
956cd1d6 9437 if ((freg == 0) && (lo32.X_add_number == (offsetT) 0xffffffff))
beae10d5 9438 {
df58fc94
RS
9439 macro_build (&lo32, "lui", LUI_FMT, reg, BFD_RELOC_HI16);
9440 macro_build (NULL, "dsrl32", SHFT_FMT, reg, reg, 0);
beae10d5
KH
9441 return;
9442 }
252b5132
RH
9443
9444 if (freg != 0)
9445 {
df58fc94 9446 macro_build (NULL, "dsll", SHFT_FMT, reg, freg, 16);
252b5132
RH
9447 freg = reg;
9448 }
9449 mid16 = lo32;
9450 mid16.X_add_number >>= 16;
67c0d1eb 9451 macro_build (&mid16, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
df58fc94 9452 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
252b5132
RH
9453 freg = reg;
9454 }
9455 if ((lo32.X_add_number & 0xffff) != 0)
67c0d1eb 9456 macro_build (&lo32, "ori", "t,r,i", reg, freg, BFD_RELOC_LO16);
252b5132
RH
9457}
9458
269137b2
TS
9459static inline void
9460load_delay_nop (void)
9461{
9462 if (!gpr_interlocks)
9463 macro_build (NULL, "nop", "");
9464}
9465
252b5132
RH
9466/* Load an address into a register. */
9467
9468static void
67c0d1eb 9469load_address (int reg, expressionS *ep, int *used_at)
252b5132 9470{
252b5132
RH
9471 if (ep->X_op != O_constant
9472 && ep->X_op != O_symbol)
9473 {
9474 as_bad (_("expression too complex"));
9475 ep->X_op = O_constant;
9476 }
9477
9478 if (ep->X_op == O_constant)
9479 {
67c0d1eb 9480 load_register (reg, ep, HAVE_64BIT_ADDRESSES);
252b5132
RH
9481 return;
9482 }
9483
9484 if (mips_pic == NO_PIC)
9485 {
9486 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 9487 addiu $reg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
9488 Otherwise we want
9489 lui $reg,<sym> (BFD_RELOC_HI16_S)
9490 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
d6bc6245 9491 If we have an addend, we always use the latter form.
76b3015f 9492
d6bc6245
TS
9493 With 64bit address space and a usable $at we want
9494 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9495 lui $at,<sym> (BFD_RELOC_HI16_S)
9496 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9497 daddiu $at,<sym> (BFD_RELOC_LO16)
9498 dsll32 $reg,0
3a482fd5 9499 daddu $reg,$reg,$at
76b3015f 9500
c03099e6 9501 If $at is already in use, we use a path which is suboptimal
d6bc6245
TS
9502 on superscalar processors.
9503 lui $reg,<sym> (BFD_RELOC_MIPS_HIGHEST)
9504 daddiu $reg,<sym> (BFD_RELOC_MIPS_HIGHER)
9505 dsll $reg,16
9506 daddiu $reg,<sym> (BFD_RELOC_HI16_S)
9507 dsll $reg,16
9508 daddiu $reg,<sym> (BFD_RELOC_LO16)
6caf9ef4
TS
9509
9510 For GP relative symbols in 64bit address space we can use
9511 the same sequence as in 32bit address space. */
aed1a261 9512 if (HAVE_64BIT_SYMBOLS)
d6bc6245 9513 {
6caf9ef4
TS
9514 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
9515 && !nopic_need_relax (ep->X_add_symbol, 1))
9516 {
9517 relax_start (ep->X_add_symbol);
9518 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
9519 mips_gp_register, BFD_RELOC_GPREL16);
9520 relax_switch ();
9521 }
d6bc6245 9522
741fe287 9523 if (*used_at == 0 && mips_opts.at)
d6bc6245 9524 {
df58fc94
RS
9525 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
9526 macro_build (ep, "lui", LUI_FMT, AT, BFD_RELOC_HI16_S);
67c0d1eb
RS
9527 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9528 BFD_RELOC_MIPS_HIGHER);
9529 macro_build (ep, "daddiu", "t,r,j", AT, AT, BFD_RELOC_LO16);
df58fc94 9530 macro_build (NULL, "dsll32", SHFT_FMT, reg, reg, 0);
67c0d1eb 9531 macro_build (NULL, "daddu", "d,v,t", reg, reg, AT);
d6bc6245
TS
9532 *used_at = 1;
9533 }
9534 else
9535 {
df58fc94 9536 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb
RS
9537 macro_build (ep, "daddiu", "t,r,j", reg, reg,
9538 BFD_RELOC_MIPS_HIGHER);
df58fc94 9539 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9540 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_HI16_S);
df58fc94 9541 macro_build (NULL, "dsll", SHFT_FMT, reg, reg, 16);
67c0d1eb 9542 macro_build (ep, "daddiu", "t,r,j", reg, reg, BFD_RELOC_LO16);
d6bc6245 9543 }
6caf9ef4
TS
9544
9545 if (mips_relax.sequence)
9546 relax_end ();
d6bc6245 9547 }
252b5132
RH
9548 else
9549 {
d6bc6245 9550 if ((valueT) ep->X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 9551 && !nopic_need_relax (ep->X_add_symbol, 1))
d6bc6245 9552 {
4d7206a2 9553 relax_start (ep->X_add_symbol);
67c0d1eb 9554 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg,
17a2f251 9555 mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 9556 relax_switch ();
d6bc6245 9557 }
67c0d1eb
RS
9558 macro_build_lui (ep, reg);
9559 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j",
9560 reg, reg, BFD_RELOC_LO16);
4d7206a2
RS
9561 if (mips_relax.sequence)
9562 relax_end ();
d6bc6245 9563 }
252b5132 9564 }
0a44bf69 9565 else if (!mips_big_got)
252b5132
RH
9566 {
9567 expressionS ex;
9568
9569 /* If this is a reference to an external symbol, we want
9570 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9571 Otherwise we want
9572 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9573 nop
9574 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
f5040a92
AO
9575 If there is a constant, it must be added in after.
9576
ed6fb7bd 9577 If we have NewABI, we want
f5040a92
AO
9578 lw $reg,<sym+cst>($gp) (BFD_RELOC_MIPS_GOT_DISP)
9579 unless we're referencing a global symbol with a non-zero
9580 offset, in which case cst must be added separately. */
ed6fb7bd
SC
9581 if (HAVE_NEWABI)
9582 {
f5040a92
AO
9583 if (ep->X_add_number)
9584 {
4d7206a2 9585 ex.X_add_number = ep->X_add_number;
f5040a92 9586 ep->X_add_number = 0;
4d7206a2 9587 relax_start (ep->X_add_symbol);
67c0d1eb
RS
9588 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9589 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
9590 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9591 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9592 ex.X_op = O_constant;
67c0d1eb 9593 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9594 reg, reg, BFD_RELOC_LO16);
f5040a92 9595 ep->X_add_number = ex.X_add_number;
4d7206a2 9596 relax_switch ();
f5040a92 9597 }
67c0d1eb 9598 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9599 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2
RS
9600 if (mips_relax.sequence)
9601 relax_end ();
ed6fb7bd
SC
9602 }
9603 else
9604 {
f5040a92
AO
9605 ex.X_add_number = ep->X_add_number;
9606 ep->X_add_number = 0;
67c0d1eb
RS
9607 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
9608 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9609 load_delay_nop ();
4d7206a2
RS
9610 relax_start (ep->X_add_symbol);
9611 relax_switch ();
67c0d1eb 9612 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9613 BFD_RELOC_LO16);
4d7206a2 9614 relax_end ();
ed6fb7bd 9615
f5040a92
AO
9616 if (ex.X_add_number != 0)
9617 {
9618 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9619 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9620 ex.X_op = O_constant;
67c0d1eb 9621 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 9622 reg, reg, BFD_RELOC_LO16);
f5040a92 9623 }
252b5132
RH
9624 }
9625 }
0a44bf69 9626 else if (mips_big_got)
252b5132
RH
9627 {
9628 expressionS ex;
252b5132
RH
9629
9630 /* This is the large GOT case. If this is a reference to an
9631 external symbol, we want
9632 lui $reg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
9633 addu $reg,$reg,$gp
9634 lw $reg,<sym>($reg) (BFD_RELOC_MIPS_GOT_LO16)
f5040a92
AO
9635
9636 Otherwise, for a reference to a local symbol in old ABI, we want
252b5132
RH
9637 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
9638 nop
9639 addiu $reg,$reg,<sym> (BFD_RELOC_LO16)
684022ea 9640 If there is a constant, it must be added in after.
f5040a92
AO
9641
9642 In the NewABI, for local symbols, with or without offsets, we want:
438c16b8
TS
9643 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
9644 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 9645 */
438c16b8
TS
9646 if (HAVE_NEWABI)
9647 {
4d7206a2 9648 ex.X_add_number = ep->X_add_number;
f5040a92 9649 ep->X_add_number = 0;
4d7206a2 9650 relax_start (ep->X_add_symbol);
df58fc94 9651 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9652 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9653 reg, reg, mips_gp_register);
9654 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9655 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
f5040a92
AO
9656 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9657 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9658 else if (ex.X_add_number)
9659 {
9660 ex.X_op = O_constant;
67c0d1eb
RS
9661 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9662 BFD_RELOC_LO16);
f5040a92
AO
9663 }
9664
9665 ep->X_add_number = ex.X_add_number;
4d7206a2 9666 relax_switch ();
67c0d1eb 9667 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9668 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
67c0d1eb
RS
9669 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9670 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 9671 relax_end ();
438c16b8 9672 }
252b5132 9673 else
438c16b8 9674 {
f5040a92
AO
9675 ex.X_add_number = ep->X_add_number;
9676 ep->X_add_number = 0;
4d7206a2 9677 relax_start (ep->X_add_symbol);
df58fc94 9678 macro_build (ep, "lui", LUI_FMT, reg, BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
9679 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
9680 reg, reg, mips_gp_register);
9681 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)",
9682 reg, BFD_RELOC_MIPS_GOT_LO16, reg);
4d7206a2
RS
9683 relax_switch ();
9684 if (reg_needs_delay (mips_gp_register))
438c16b8
TS
9685 {
9686 /* We need a nop before loading from $gp. This special
9687 check is required because the lui which starts the main
9688 instruction stream does not refer to $gp, and so will not
9689 insert the nop which may be required. */
67c0d1eb 9690 macro_build (NULL, "nop", "");
438c16b8 9691 }
67c0d1eb 9692 macro_build (ep, ADDRESS_LOAD_INSN, "t,o(b)", reg,
17a2f251 9693 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 9694 load_delay_nop ();
67c0d1eb 9695 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
17a2f251 9696 BFD_RELOC_LO16);
4d7206a2 9697 relax_end ();
438c16b8 9698
f5040a92
AO
9699 if (ex.X_add_number != 0)
9700 {
9701 if (ex.X_add_number < -0x8000 || ex.X_add_number >= 0x8000)
9702 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
9703 ex.X_op = O_constant;
67c0d1eb
RS
9704 macro_build (&ex, ADDRESS_ADDI_INSN, "t,r,j", reg, reg,
9705 BFD_RELOC_LO16);
f5040a92 9706 }
252b5132
RH
9707 }
9708 }
252b5132
RH
9709 else
9710 abort ();
8fc2e39e 9711
741fe287 9712 if (!mips_opts.at && *used_at == 1)
1661c76c 9713 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
9714}
9715
ea1fb5dc
RS
9716/* Move the contents of register SOURCE into register DEST. */
9717
9718static void
67c0d1eb 9719move_register (int dest, int source)
ea1fb5dc 9720{
df58fc94
RS
9721 /* Prefer to use a 16-bit microMIPS instruction unless the previous
9722 instruction specifically requires a 32-bit one. */
9723 if (mips_opts.micromips
833794fc 9724 && !mips_opts.insn32
df58fc94 9725 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
7951ca42 9726 macro_build (NULL, "move", "mp,mj", dest, source);
df58fc94 9727 else
40fc1451 9728 macro_build (NULL, "or", "d,v,t", dest, source, 0);
ea1fb5dc
RS
9729}
9730
4d7206a2 9731/* Emit an SVR4 PIC sequence to load address LOCAL into DEST, where
f6a22291
MR
9732 LOCAL is the sum of a symbol and a 16-bit or 32-bit displacement.
9733 The two alternatives are:
4d7206a2 9734
33eaf5de 9735 Global symbol Local symbol
4d7206a2
RS
9736 ------------- ------------
9737 lw DEST,%got(SYMBOL) lw DEST,%got(SYMBOL + OFFSET)
9738 ... ...
9739 addiu DEST,DEST,OFFSET addiu DEST,DEST,%lo(SYMBOL + OFFSET)
9740
9741 load_got_offset emits the first instruction and add_got_offset
f6a22291
MR
9742 emits the second for a 16-bit offset or add_got_offset_hilo emits
9743 a sequence to add a 32-bit offset using a scratch register. */
4d7206a2
RS
9744
9745static void
67c0d1eb 9746load_got_offset (int dest, expressionS *local)
4d7206a2
RS
9747{
9748 expressionS global;
9749
9750 global = *local;
9751 global.X_add_number = 0;
9752
9753 relax_start (local->X_add_symbol);
67c0d1eb
RS
9754 macro_build (&global, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9755 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2 9756 relax_switch ();
67c0d1eb
RS
9757 macro_build (local, ADDRESS_LOAD_INSN, "t,o(b)", dest,
9758 BFD_RELOC_MIPS_GOT16, mips_gp_register);
4d7206a2
RS
9759 relax_end ();
9760}
9761
9762static void
67c0d1eb 9763add_got_offset (int dest, expressionS *local)
4d7206a2
RS
9764{
9765 expressionS global;
9766
9767 global.X_op = O_constant;
9768 global.X_op_symbol = NULL;
9769 global.X_add_symbol = NULL;
9770 global.X_add_number = local->X_add_number;
9771
9772 relax_start (local->X_add_symbol);
67c0d1eb 9773 macro_build (&global, ADDRESS_ADDI_INSN, "t,r,j",
4d7206a2
RS
9774 dest, dest, BFD_RELOC_LO16);
9775 relax_switch ();
67c0d1eb 9776 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", dest, dest, BFD_RELOC_LO16);
4d7206a2
RS
9777 relax_end ();
9778}
9779
f6a22291
MR
9780static void
9781add_got_offset_hilo (int dest, expressionS *local, int tmp)
9782{
9783 expressionS global;
9784 int hold_mips_optimize;
9785
9786 global.X_op = O_constant;
9787 global.X_op_symbol = NULL;
9788 global.X_add_symbol = NULL;
9789 global.X_add_number = local->X_add_number;
9790
9791 relax_start (local->X_add_symbol);
9792 load_register (tmp, &global, HAVE_64BIT_ADDRESSES);
9793 relax_switch ();
9794 /* Set mips_optimize around the lui instruction to avoid
9795 inserting an unnecessary nop after the lw. */
9796 hold_mips_optimize = mips_optimize;
9797 mips_optimize = 2;
9798 macro_build_lui (&global, tmp);
9799 mips_optimize = hold_mips_optimize;
9800 macro_build (local, ADDRESS_ADDI_INSN, "t,r,j", tmp, tmp, BFD_RELOC_LO16);
9801 relax_end ();
9802
9803 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dest, dest, tmp);
9804}
9805
df58fc94
RS
9806/* Emit a sequence of instructions to emulate a branch likely operation.
9807 BR is an ordinary branch corresponding to one to be emulated. BRNEG
9808 is its complementing branch with the original condition negated.
9809 CALL is set if the original branch specified the link operation.
9810 EP, FMT, SREG and TREG specify the usual macro_build() parameters.
9811
9812 Code like this is produced in the noreorder mode:
9813
9814 BRNEG <args>, 1f
9815 nop
9816 b <sym>
9817 delay slot (executed only if branch taken)
9818 1:
9819
9820 or, if CALL is set:
9821
9822 BRNEG <args>, 1f
9823 nop
9824 bal <sym>
9825 delay slot (executed only if branch taken)
9826 1:
9827
9828 In the reorder mode the delay slot would be filled with a nop anyway,
9829 so code produced is simply:
9830
9831 BR <args>, <sym>
9832 nop
9833
9834 This function is used when producing code for the microMIPS ASE that
9835 does not implement branch likely instructions in hardware. */
9836
9837static void
9838macro_build_branch_likely (const char *br, const char *brneg,
9839 int call, expressionS *ep, const char *fmt,
9840 unsigned int sreg, unsigned int treg)
9841{
9842 int noreorder = mips_opts.noreorder;
9843 expressionS expr1;
9844
9845 gas_assert (mips_opts.micromips);
9846 start_noreorder ();
9847 if (noreorder)
9848 {
9849 micromips_label_expr (&expr1);
9850 macro_build (&expr1, brneg, fmt, sreg, treg);
9851 macro_build (NULL, "nop", "");
9852 macro_build (ep, call ? "bal" : "b", "p");
9853
9854 /* Set to true so that append_insn adds a label. */
9855 emit_branch_likely_macro = TRUE;
9856 }
9857 else
9858 {
9859 macro_build (ep, br, fmt, sreg, treg);
9860 macro_build (NULL, "nop", "");
9861 }
9862 end_noreorder ();
9863}
9864
9865/* Emit a coprocessor branch-likely macro specified by TYPE, using CC as
9866 the condition code tested. EP specifies the branch target. */
9867
9868static void
9869macro_build_branch_ccl (int type, expressionS *ep, unsigned int cc)
9870{
9871 const int call = 0;
9872 const char *brneg;
9873 const char *br;
9874
9875 switch (type)
9876 {
9877 case M_BC1FL:
9878 br = "bc1f";
9879 brneg = "bc1t";
9880 break;
9881 case M_BC1TL:
9882 br = "bc1t";
9883 brneg = "bc1f";
9884 break;
9885 case M_BC2FL:
9886 br = "bc2f";
9887 brneg = "bc2t";
9888 break;
9889 case M_BC2TL:
9890 br = "bc2t";
9891 brneg = "bc2f";
9892 break;
9893 default:
9894 abort ();
9895 }
9896 macro_build_branch_likely (br, brneg, call, ep, "N,p", cc, ZERO);
9897}
9898
9899/* Emit a two-argument branch macro specified by TYPE, using SREG as
9900 the register tested. EP specifies the branch target. */
9901
9902static void
9903macro_build_branch_rs (int type, expressionS *ep, unsigned int sreg)
9904{
9905 const char *brneg = NULL;
9906 const char *br;
9907 int call = 0;
9908
9909 switch (type)
9910 {
9911 case M_BGEZ:
9912 br = "bgez";
9913 break;
9914 case M_BGEZL:
9915 br = mips_opts.micromips ? "bgez" : "bgezl";
9916 brneg = "bltz";
9917 break;
9918 case M_BGEZALL:
9919 gas_assert (mips_opts.micromips);
833794fc 9920 br = mips_opts.insn32 ? "bgezal" : "bgezals";
df58fc94
RS
9921 brneg = "bltz";
9922 call = 1;
9923 break;
9924 case M_BGTZ:
9925 br = "bgtz";
9926 break;
9927 case M_BGTZL:
9928 br = mips_opts.micromips ? "bgtz" : "bgtzl";
9929 brneg = "blez";
9930 break;
9931 case M_BLEZ:
9932 br = "blez";
9933 break;
9934 case M_BLEZL:
9935 br = mips_opts.micromips ? "blez" : "blezl";
9936 brneg = "bgtz";
9937 break;
9938 case M_BLTZ:
9939 br = "bltz";
9940 break;
9941 case M_BLTZL:
9942 br = mips_opts.micromips ? "bltz" : "bltzl";
9943 brneg = "bgez";
9944 break;
9945 case M_BLTZALL:
9946 gas_assert (mips_opts.micromips);
833794fc 9947 br = mips_opts.insn32 ? "bltzal" : "bltzals";
df58fc94
RS
9948 brneg = "bgez";
9949 call = 1;
9950 break;
9951 default:
9952 abort ();
9953 }
9954 if (mips_opts.micromips && brneg)
9955 macro_build_branch_likely (br, brneg, call, ep, "s,p", sreg, ZERO);
9956 else
9957 macro_build (ep, br, "s,p", sreg);
9958}
9959
9960/* Emit a three-argument branch macro specified by TYPE, using SREG and
9961 TREG as the registers tested. EP specifies the branch target. */
9962
9963static void
9964macro_build_branch_rsrt (int type, expressionS *ep,
9965 unsigned int sreg, unsigned int treg)
9966{
9967 const char *brneg = NULL;
9968 const int call = 0;
9969 const char *br;
9970
9971 switch (type)
9972 {
9973 case M_BEQ:
9974 case M_BEQ_I:
9975 br = "beq";
9976 break;
9977 case M_BEQL:
9978 case M_BEQL_I:
9979 br = mips_opts.micromips ? "beq" : "beql";
9980 brneg = "bne";
9981 break;
9982 case M_BNE:
9983 case M_BNE_I:
9984 br = "bne";
9985 break;
9986 case M_BNEL:
9987 case M_BNEL_I:
9988 br = mips_opts.micromips ? "bne" : "bnel";
9989 brneg = "beq";
9990 break;
9991 default:
9992 abort ();
9993 }
9994 if (mips_opts.micromips && brneg)
9995 macro_build_branch_likely (br, brneg, call, ep, "s,t,p", sreg, treg);
9996 else
9997 macro_build (ep, br, "s,t,p", sreg, treg);
9998}
9999
f2ae14a1
RS
10000/* Return the high part that should be loaded in order to make the low
10001 part of VALUE accessible using an offset of OFFBITS bits. */
10002
10003static offsetT
10004offset_high_part (offsetT value, unsigned int offbits)
10005{
10006 offsetT bias;
10007 addressT low_mask;
10008
10009 if (offbits == 0)
10010 return value;
10011 bias = 1 << (offbits - 1);
10012 low_mask = bias * 2 - 1;
10013 return (value + bias) & ~low_mask;
10014}
10015
10016/* Return true if the value stored in offset_expr and offset_reloc
10017 fits into a signed offset of OFFBITS bits. RANGE is the maximum
10018 amount that the caller wants to add without inducing overflow
10019 and ALIGN is the known alignment of the value in bytes. */
10020
10021static bfd_boolean
10022small_offset_p (unsigned int range, unsigned int align, unsigned int offbits)
10023{
10024 if (offbits == 16)
10025 {
10026 /* Accept any relocation operator if overflow isn't a concern. */
10027 if (range < align && *offset_reloc != BFD_RELOC_UNUSED)
10028 return TRUE;
10029
10030 /* These relocations are guaranteed not to overflow in correct links. */
10031 if (*offset_reloc == BFD_RELOC_MIPS_LITERAL
10032 || gprel16_reloc_p (*offset_reloc))
10033 return TRUE;
10034 }
10035 if (offset_expr.X_op == O_constant
10036 && offset_high_part (offset_expr.X_add_number, offbits) == 0
10037 && offset_high_part (offset_expr.X_add_number + range, offbits) == 0)
10038 return TRUE;
10039 return FALSE;
10040}
10041
252b5132
RH
10042/*
10043 * Build macros
10044 * This routine implements the seemingly endless macro or synthesized
10045 * instructions and addressing modes in the mips assembly language. Many
10046 * of these macros are simple and are similar to each other. These could
67c1ffbe 10047 * probably be handled by some kind of table or grammar approach instead of
252b5132
RH
10048 * this verbose method. Others are not simple macros but are more like
10049 * optimizing code generation.
10050 * One interesting optimization is when several store macros appear
67c1ffbe 10051 * consecutively that would load AT with the upper half of the same address.
2b0f3761 10052 * The ensuing load upper instructions are omitted. This implies some kind
252b5132
RH
10053 * of global optimization. We currently only optimize within a single macro.
10054 * For many of the load and store macros if the address is specified as a
10055 * constant expression in the first 64k of memory (ie ld $2,0x4000c) we
10056 * first load register 'at' with zero and use it as the base register. The
10057 * mips assembler simply uses register $zero. Just one tiny optimization
10058 * we're missing.
10059 */
10060static void
833794fc 10061macro (struct mips_cl_insn *ip, char *str)
252b5132 10062{
c0ebe874
RS
10063 const struct mips_operand_array *operands;
10064 unsigned int breg, i;
741fe287 10065 unsigned int tempreg;
252b5132 10066 int mask;
43841e91 10067 int used_at = 0;
df58fc94 10068 expressionS label_expr;
252b5132 10069 expressionS expr1;
df58fc94 10070 expressionS *ep;
252b5132
RH
10071 const char *s;
10072 const char *s2;
10073 const char *fmt;
10074 int likely = 0;
252b5132 10075 int coproc = 0;
7f3c4072 10076 int offbits = 16;
1abe91b1 10077 int call = 0;
df58fc94
RS
10078 int jals = 0;
10079 int dbl = 0;
10080 int imm = 0;
10081 int ust = 0;
10082 int lp = 0;
f2ae14a1 10083 bfd_boolean large_offset;
252b5132 10084 int off;
252b5132 10085 int hold_mips_optimize;
f2ae14a1 10086 unsigned int align;
c0ebe874 10087 unsigned int op[MAX_OPERANDS];
252b5132 10088
9c2799c2 10089 gas_assert (! mips_opts.mips16);
252b5132 10090
c0ebe874
RS
10091 operands = insn_operands (ip);
10092 for (i = 0; i < MAX_OPERANDS; i++)
10093 if (operands->operand[i])
10094 op[i] = insn_extract_operand (ip, operands->operand[i]);
10095 else
10096 op[i] = -1;
10097
252b5132
RH
10098 mask = ip->insn_mo->mask;
10099
df58fc94
RS
10100 label_expr.X_op = O_constant;
10101 label_expr.X_op_symbol = NULL;
10102 label_expr.X_add_symbol = NULL;
10103 label_expr.X_add_number = 0;
10104
252b5132
RH
10105 expr1.X_op = O_constant;
10106 expr1.X_op_symbol = NULL;
10107 expr1.X_add_symbol = NULL;
10108 expr1.X_add_number = 1;
f2ae14a1 10109 align = 1;
252b5132
RH
10110
10111 switch (mask)
10112 {
10113 case M_DABS:
10114 dbl = 1;
1a0670f3 10115 /* Fall through. */
252b5132 10116 case M_ABS:
df58fc94
RS
10117 /* bgez $a0,1f
10118 move v0,$a0
10119 sub v0,$zero,$a0
10120 1:
10121 */
252b5132 10122
7d10b47d 10123 start_noreorder ();
252b5132 10124
df58fc94
RS
10125 if (mips_opts.micromips)
10126 micromips_label_expr (&label_expr);
10127 else
10128 label_expr.X_add_number = 8;
c0ebe874
RS
10129 macro_build (&label_expr, "bgez", "s,p", op[1]);
10130 if (op[0] == op[1])
a605d2b3 10131 macro_build (NULL, "nop", "");
252b5132 10132 else
c0ebe874
RS
10133 move_register (op[0], op[1]);
10134 macro_build (NULL, dbl ? "dsub" : "sub", "d,v,t", op[0], 0, op[1]);
df58fc94
RS
10135 if (mips_opts.micromips)
10136 micromips_add_label ();
252b5132 10137
7d10b47d 10138 end_noreorder ();
8fc2e39e 10139 break;
252b5132
RH
10140
10141 case M_ADD_I:
10142 s = "addi";
10143 s2 = "add";
10144 goto do_addi;
10145 case M_ADDU_I:
10146 s = "addiu";
10147 s2 = "addu";
10148 goto do_addi;
10149 case M_DADD_I:
10150 dbl = 1;
10151 s = "daddi";
10152 s2 = "dadd";
df58fc94
RS
10153 if (!mips_opts.micromips)
10154 goto do_addi;
b0e6f033 10155 if (imm_expr.X_add_number >= -0x200
df58fc94
RS
10156 && imm_expr.X_add_number < 0x200)
10157 {
b0e6f033
RS
10158 macro_build (NULL, s, "t,r,.", op[0], op[1],
10159 (int) imm_expr.X_add_number);
df58fc94
RS
10160 break;
10161 }
10162 goto do_addi_i;
252b5132
RH
10163 case M_DADDU_I:
10164 dbl = 1;
10165 s = "daddiu";
10166 s2 = "daddu";
10167 do_addi:
b0e6f033 10168 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
10169 && imm_expr.X_add_number < 0x8000)
10170 {
c0ebe874 10171 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 10172 break;
252b5132 10173 }
df58fc94 10174 do_addi_i:
8fc2e39e 10175 used_at = 1;
67c0d1eb 10176 load_register (AT, &imm_expr, dbl);
c0ebe874 10177 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10178 break;
10179
10180 case M_AND_I:
10181 s = "andi";
10182 s2 = "and";
10183 goto do_bit;
10184 case M_OR_I:
10185 s = "ori";
10186 s2 = "or";
10187 goto do_bit;
10188 case M_NOR_I:
10189 s = "";
10190 s2 = "nor";
10191 goto do_bit;
10192 case M_XOR_I:
10193 s = "xori";
10194 s2 = "xor";
10195 do_bit:
b0e6f033 10196 if (imm_expr.X_add_number >= 0
252b5132
RH
10197 && imm_expr.X_add_number < 0x10000)
10198 {
10199 if (mask != M_NOR_I)
c0ebe874 10200 macro_build (&imm_expr, s, "t,r,i", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
10201 else
10202 {
67c0d1eb 10203 macro_build (&imm_expr, "ori", "t,r,i",
c0ebe874
RS
10204 op[0], op[1], BFD_RELOC_LO16);
10205 macro_build (NULL, "nor", "d,v,t", op[0], op[0], 0);
252b5132 10206 }
8fc2e39e 10207 break;
252b5132
RH
10208 }
10209
8fc2e39e 10210 used_at = 1;
bad1aba3 10211 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 10212 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
10213 break;
10214
8b082fb1
TS
10215 case M_BALIGN:
10216 switch (imm_expr.X_add_number)
10217 {
10218 case 0:
10219 macro_build (NULL, "nop", "");
10220 break;
10221 case 2:
c0ebe874 10222 macro_build (NULL, "packrl.ph", "d,s,t", op[0], op[0], op[1]);
8b082fb1 10223 break;
03f66e8a
MR
10224 case 1:
10225 case 3:
c0ebe874 10226 macro_build (NULL, "balign", "t,s,2", op[0], op[1],
90ecf173 10227 (int) imm_expr.X_add_number);
8b082fb1 10228 break;
03f66e8a
MR
10229 default:
10230 as_bad (_("BALIGN immediate not 0, 1, 2 or 3 (%lu)"),
10231 (unsigned long) imm_expr.X_add_number);
10232 break;
8b082fb1
TS
10233 }
10234 break;
10235
df58fc94
RS
10236 case M_BC1FL:
10237 case M_BC1TL:
10238 case M_BC2FL:
10239 case M_BC2TL:
10240 gas_assert (mips_opts.micromips);
10241 macro_build_branch_ccl (mask, &offset_expr,
10242 EXTRACT_OPERAND (1, BCC, *ip));
10243 break;
10244
252b5132 10245 case M_BEQ_I:
252b5132 10246 case M_BEQL_I:
252b5132 10247 case M_BNE_I:
252b5132 10248 case M_BNEL_I:
b0e6f033 10249 if (imm_expr.X_add_number == 0)
c0ebe874 10250 op[1] = 0;
df58fc94 10251 else
252b5132 10252 {
c0ebe874 10253 op[1] = AT;
df58fc94 10254 used_at = 1;
bad1aba3 10255 load_register (op[1], &imm_expr, GPR_SIZE == 64);
252b5132 10256 }
df58fc94
RS
10257 /* Fall through. */
10258 case M_BEQL:
10259 case M_BNEL:
c0ebe874 10260 macro_build_branch_rsrt (mask, &offset_expr, op[0], op[1]);
252b5132
RH
10261 break;
10262
10263 case M_BGEL:
10264 likely = 1;
1a0670f3 10265 /* Fall through. */
252b5132 10266 case M_BGE:
c0ebe874
RS
10267 if (op[1] == 0)
10268 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[0]);
10269 else if (op[0] == 0)
10270 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[1]);
df58fc94 10271 else
252b5132 10272 {
df58fc94 10273 used_at = 1;
c0ebe874 10274 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10275 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10276 &offset_expr, AT, ZERO);
252b5132 10277 }
df58fc94
RS
10278 break;
10279
10280 case M_BGEZL:
10281 case M_BGEZALL:
10282 case M_BGTZL:
10283 case M_BLEZL:
10284 case M_BLTZL:
10285 case M_BLTZALL:
c0ebe874 10286 macro_build_branch_rs (mask, &offset_expr, op[0]);
252b5132
RH
10287 break;
10288
10289 case M_BGTL_I:
10290 likely = 1;
1a0670f3 10291 /* Fall through. */
252b5132 10292 case M_BGT_I:
90ecf173 10293 /* Check for > max integer. */
b0e6f033 10294 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132
RH
10295 {
10296 do_false:
90ecf173 10297 /* Result is always false. */
252b5132 10298 if (! likely)
a605d2b3 10299 macro_build (NULL, "nop", "");
252b5132 10300 else
df58fc94 10301 macro_build_branch_rsrt (M_BNEL, &offset_expr, ZERO, ZERO);
8fc2e39e 10302 break;
252b5132 10303 }
f9419b05 10304 ++imm_expr.X_add_number;
252b5132
RH
10305 /* FALLTHROUGH */
10306 case M_BGE_I:
10307 case M_BGEL_I:
10308 if (mask == M_BGEL_I)
10309 likely = 1;
b0e6f033 10310 if (imm_expr.X_add_number == 0)
252b5132 10311 {
df58fc94 10312 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ,
c0ebe874 10313 &offset_expr, op[0]);
8fc2e39e 10314 break;
252b5132 10315 }
b0e6f033 10316 if (imm_expr.X_add_number == 1)
252b5132 10317 {
df58fc94 10318 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ,
c0ebe874 10319 &offset_expr, op[0]);
8fc2e39e 10320 break;
252b5132 10321 }
b0e6f033 10322 if (imm_expr.X_add_number <= GPR_SMIN)
252b5132
RH
10323 {
10324 do_true:
10325 /* result is always true */
1661c76c 10326 as_warn (_("branch %s is always true"), ip->insn_mo->name);
67c0d1eb 10327 macro_build (&offset_expr, "b", "p");
8fc2e39e 10328 break;
252b5132 10329 }
8fc2e39e 10330 used_at = 1;
c0ebe874 10331 set_at (op[0], 0);
df58fc94
RS
10332 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10333 &offset_expr, AT, ZERO);
252b5132
RH
10334 break;
10335
10336 case M_BGEUL:
10337 likely = 1;
1a0670f3 10338 /* Fall through. */
252b5132 10339 case M_BGEU:
c0ebe874 10340 if (op[1] == 0)
252b5132 10341 goto do_true;
c0ebe874 10342 else if (op[0] == 0)
df58fc94 10343 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10344 &offset_expr, ZERO, op[1]);
df58fc94 10345 else
252b5132 10346 {
df58fc94 10347 used_at = 1;
c0ebe874 10348 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10349 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10350 &offset_expr, AT, ZERO);
252b5132 10351 }
252b5132
RH
10352 break;
10353
10354 case M_BGTUL_I:
10355 likely = 1;
1a0670f3 10356 /* Fall through. */
252b5132 10357 case M_BGTU_I:
c0ebe874 10358 if (op[0] == 0
bad1aba3 10359 || (GPR_SIZE == 32
f01dc953 10360 && imm_expr.X_add_number == -1))
252b5132 10361 goto do_false;
f9419b05 10362 ++imm_expr.X_add_number;
252b5132
RH
10363 /* FALLTHROUGH */
10364 case M_BGEU_I:
10365 case M_BGEUL_I:
10366 if (mask == M_BGEUL_I)
10367 likely = 1;
b0e6f033 10368 if (imm_expr.X_add_number == 0)
252b5132 10369 goto do_true;
b0e6f033 10370 else if (imm_expr.X_add_number == 1)
df58fc94 10371 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10372 &offset_expr, op[0], ZERO);
df58fc94 10373 else
252b5132 10374 {
df58fc94 10375 used_at = 1;
c0ebe874 10376 set_at (op[0], 1);
df58fc94
RS
10377 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10378 &offset_expr, AT, ZERO);
252b5132 10379 }
252b5132
RH
10380 break;
10381
10382 case M_BGTL:
10383 likely = 1;
1a0670f3 10384 /* Fall through. */
252b5132 10385 case M_BGT:
c0ebe874
RS
10386 if (op[1] == 0)
10387 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[0]);
10388 else if (op[0] == 0)
10389 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[1]);
df58fc94 10390 else
252b5132 10391 {
df58fc94 10392 used_at = 1;
c0ebe874 10393 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10394 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10395 &offset_expr, AT, ZERO);
252b5132 10396 }
252b5132
RH
10397 break;
10398
10399 case M_BGTUL:
10400 likely = 1;
1a0670f3 10401 /* Fall through. */
252b5132 10402 case M_BGTU:
c0ebe874 10403 if (op[1] == 0)
df58fc94 10404 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874
RS
10405 &offset_expr, op[0], ZERO);
10406 else if (op[0] == 0)
df58fc94
RS
10407 goto do_false;
10408 else
252b5132 10409 {
df58fc94 10410 used_at = 1;
c0ebe874 10411 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10412 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10413 &offset_expr, AT, ZERO);
252b5132 10414 }
252b5132
RH
10415 break;
10416
10417 case M_BLEL:
10418 likely = 1;
1a0670f3 10419 /* Fall through. */
252b5132 10420 case M_BLE:
c0ebe874
RS
10421 if (op[1] == 0)
10422 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
10423 else if (op[0] == 0)
10424 macro_build_branch_rs (likely ? M_BGEZL : M_BGEZ, &offset_expr, op[1]);
df58fc94 10425 else
252b5132 10426 {
df58fc94 10427 used_at = 1;
c0ebe874 10428 macro_build (NULL, "slt", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10429 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10430 &offset_expr, AT, ZERO);
252b5132 10431 }
252b5132
RH
10432 break;
10433
10434 case M_BLEL_I:
10435 likely = 1;
1a0670f3 10436 /* Fall through. */
252b5132 10437 case M_BLE_I:
b0e6f033 10438 if (imm_expr.X_add_number >= GPR_SMAX)
252b5132 10439 goto do_true;
f9419b05 10440 ++imm_expr.X_add_number;
252b5132
RH
10441 /* FALLTHROUGH */
10442 case M_BLT_I:
10443 case M_BLTL_I:
10444 if (mask == M_BLTL_I)
10445 likely = 1;
b0e6f033 10446 if (imm_expr.X_add_number == 0)
c0ebe874 10447 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
b0e6f033 10448 else if (imm_expr.X_add_number == 1)
c0ebe874 10449 macro_build_branch_rs (likely ? M_BLEZL : M_BLEZ, &offset_expr, op[0]);
df58fc94 10450 else
252b5132 10451 {
df58fc94 10452 used_at = 1;
c0ebe874 10453 set_at (op[0], 0);
df58fc94
RS
10454 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10455 &offset_expr, AT, ZERO);
252b5132 10456 }
252b5132
RH
10457 break;
10458
10459 case M_BLEUL:
10460 likely = 1;
1a0670f3 10461 /* Fall through. */
252b5132 10462 case M_BLEU:
c0ebe874 10463 if (op[1] == 0)
df58fc94 10464 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874
RS
10465 &offset_expr, op[0], ZERO);
10466 else if (op[0] == 0)
df58fc94
RS
10467 goto do_true;
10468 else
252b5132 10469 {
df58fc94 10470 used_at = 1;
c0ebe874 10471 macro_build (NULL, "sltu", "d,v,t", AT, op[1], op[0]);
df58fc94
RS
10472 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
10473 &offset_expr, AT, ZERO);
252b5132 10474 }
252b5132
RH
10475 break;
10476
10477 case M_BLEUL_I:
10478 likely = 1;
1a0670f3 10479 /* Fall through. */
252b5132 10480 case M_BLEU_I:
c0ebe874 10481 if (op[0] == 0
bad1aba3 10482 || (GPR_SIZE == 32
f01dc953 10483 && imm_expr.X_add_number == -1))
252b5132 10484 goto do_true;
f9419b05 10485 ++imm_expr.X_add_number;
252b5132
RH
10486 /* FALLTHROUGH */
10487 case M_BLTU_I:
10488 case M_BLTUL_I:
10489 if (mask == M_BLTUL_I)
10490 likely = 1;
b0e6f033 10491 if (imm_expr.X_add_number == 0)
252b5132 10492 goto do_false;
b0e6f033 10493 else if (imm_expr.X_add_number == 1)
df58fc94 10494 macro_build_branch_rsrt (likely ? M_BEQL : M_BEQ,
c0ebe874 10495 &offset_expr, op[0], ZERO);
df58fc94 10496 else
252b5132 10497 {
df58fc94 10498 used_at = 1;
c0ebe874 10499 set_at (op[0], 1);
df58fc94
RS
10500 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10501 &offset_expr, AT, ZERO);
252b5132 10502 }
252b5132
RH
10503 break;
10504
10505 case M_BLTL:
10506 likely = 1;
1a0670f3 10507 /* Fall through. */
252b5132 10508 case M_BLT:
c0ebe874
RS
10509 if (op[1] == 0)
10510 macro_build_branch_rs (likely ? M_BLTZL : M_BLTZ, &offset_expr, op[0]);
10511 else if (op[0] == 0)
10512 macro_build_branch_rs (likely ? M_BGTZL : M_BGTZ, &offset_expr, op[1]);
df58fc94 10513 else
252b5132 10514 {
df58fc94 10515 used_at = 1;
c0ebe874 10516 macro_build (NULL, "slt", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10517 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10518 &offset_expr, AT, ZERO);
252b5132 10519 }
252b5132
RH
10520 break;
10521
10522 case M_BLTUL:
10523 likely = 1;
1a0670f3 10524 /* Fall through. */
252b5132 10525 case M_BLTU:
c0ebe874 10526 if (op[1] == 0)
252b5132 10527 goto do_false;
c0ebe874 10528 else if (op[0] == 0)
df58fc94 10529 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
c0ebe874 10530 &offset_expr, ZERO, op[1]);
df58fc94 10531 else
252b5132 10532 {
df58fc94 10533 used_at = 1;
c0ebe874 10534 macro_build (NULL, "sltu", "d,v,t", AT, op[0], op[1]);
df58fc94
RS
10535 macro_build_branch_rsrt (likely ? M_BNEL : M_BNE,
10536 &offset_expr, AT, ZERO);
252b5132 10537 }
252b5132
RH
10538 break;
10539
10540 case M_DDIV_3:
10541 dbl = 1;
1a0670f3 10542 /* Fall through. */
252b5132
RH
10543 case M_DIV_3:
10544 s = "mflo";
10545 goto do_div3;
10546 case M_DREM_3:
10547 dbl = 1;
1a0670f3 10548 /* Fall through. */
252b5132
RH
10549 case M_REM_3:
10550 s = "mfhi";
10551 do_div3:
c0ebe874 10552 if (op[2] == 0)
252b5132 10553 {
1661c76c 10554 as_warn (_("divide by zero"));
252b5132 10555 if (mips_trap)
df58fc94 10556 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10557 else
df58fc94 10558 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10559 break;
252b5132
RH
10560 }
10561
7d10b47d 10562 start_noreorder ();
252b5132
RH
10563 if (mips_trap)
10564 {
c0ebe874
RS
10565 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10566 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
252b5132
RH
10567 }
10568 else
10569 {
df58fc94
RS
10570 if (mips_opts.micromips)
10571 micromips_label_expr (&label_expr);
10572 else
10573 label_expr.X_add_number = 8;
c0ebe874
RS
10574 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10575 macro_build (NULL, dbl ? "ddiv" : "div", "z,s,t", op[1], op[2]);
df58fc94
RS
10576 macro_build (NULL, "break", BRK_FMT, 7);
10577 if (mips_opts.micromips)
10578 micromips_add_label ();
252b5132
RH
10579 }
10580 expr1.X_add_number = -1;
8fc2e39e 10581 used_at = 1;
f6a22291 10582 load_register (AT, &expr1, dbl);
df58fc94
RS
10583 if (mips_opts.micromips)
10584 micromips_label_expr (&label_expr);
10585 else
10586 label_expr.X_add_number = mips_trap ? (dbl ? 12 : 8) : (dbl ? 20 : 16);
c0ebe874 10587 macro_build (&label_expr, "bne", "s,t,p", op[2], AT);
252b5132
RH
10588 if (dbl)
10589 {
10590 expr1.X_add_number = 1;
f6a22291 10591 load_register (AT, &expr1, dbl);
df58fc94 10592 macro_build (NULL, "dsll32", SHFT_FMT, AT, AT, 31);
252b5132
RH
10593 }
10594 else
10595 {
10596 expr1.X_add_number = 0x80000000;
df58fc94 10597 macro_build (&expr1, "lui", LUI_FMT, AT, BFD_RELOC_HI16);
252b5132
RH
10598 }
10599 if (mips_trap)
10600 {
c0ebe874 10601 macro_build (NULL, "teq", TRAP_FMT, op[1], AT, 6);
252b5132
RH
10602 /* We want to close the noreorder block as soon as possible, so
10603 that later insns are available for delay slot filling. */
7d10b47d 10604 end_noreorder ();
252b5132
RH
10605 }
10606 else
10607 {
df58fc94
RS
10608 if (mips_opts.micromips)
10609 micromips_label_expr (&label_expr);
10610 else
10611 label_expr.X_add_number = 8;
c0ebe874 10612 macro_build (&label_expr, "bne", "s,t,p", op[1], AT);
a605d2b3 10613 macro_build (NULL, "nop", "");
252b5132
RH
10614
10615 /* We want to close the noreorder block as soon as possible, so
10616 that later insns are available for delay slot filling. */
7d10b47d 10617 end_noreorder ();
252b5132 10618
df58fc94 10619 macro_build (NULL, "break", BRK_FMT, 6);
252b5132 10620 }
df58fc94
RS
10621 if (mips_opts.micromips)
10622 micromips_add_label ();
c0ebe874 10623 macro_build (NULL, s, MFHL_FMT, op[0]);
252b5132
RH
10624 break;
10625
10626 case M_DIV_3I:
10627 s = "div";
10628 s2 = "mflo";
10629 goto do_divi;
10630 case M_DIVU_3I:
10631 s = "divu";
10632 s2 = "mflo";
10633 goto do_divi;
10634 case M_REM_3I:
10635 s = "div";
10636 s2 = "mfhi";
10637 goto do_divi;
10638 case M_REMU_3I:
10639 s = "divu";
10640 s2 = "mfhi";
10641 goto do_divi;
10642 case M_DDIV_3I:
10643 dbl = 1;
10644 s = "ddiv";
10645 s2 = "mflo";
10646 goto do_divi;
10647 case M_DDIVU_3I:
10648 dbl = 1;
10649 s = "ddivu";
10650 s2 = "mflo";
10651 goto do_divi;
10652 case M_DREM_3I:
10653 dbl = 1;
10654 s = "ddiv";
10655 s2 = "mfhi";
10656 goto do_divi;
10657 case M_DREMU_3I:
10658 dbl = 1;
10659 s = "ddivu";
10660 s2 = "mfhi";
10661 do_divi:
b0e6f033 10662 if (imm_expr.X_add_number == 0)
252b5132 10663 {
1661c76c 10664 as_warn (_("divide by zero"));
252b5132 10665 if (mips_trap)
df58fc94 10666 macro_build (NULL, "teq", TRAP_FMT, ZERO, ZERO, 7);
252b5132 10667 else
df58fc94 10668 macro_build (NULL, "break", BRK_FMT, 7);
8fc2e39e 10669 break;
252b5132 10670 }
b0e6f033 10671 if (imm_expr.X_add_number == 1)
252b5132
RH
10672 {
10673 if (strcmp (s2, "mflo") == 0)
c0ebe874 10674 move_register (op[0], op[1]);
252b5132 10675 else
c0ebe874 10676 move_register (op[0], ZERO);
8fc2e39e 10677 break;
252b5132 10678 }
b0e6f033 10679 if (imm_expr.X_add_number == -1 && s[strlen (s) - 1] != 'u')
252b5132
RH
10680 {
10681 if (strcmp (s2, "mflo") == 0)
c0ebe874 10682 macro_build (NULL, dbl ? "dneg" : "neg", "d,w", op[0], op[1]);
252b5132 10683 else
c0ebe874 10684 move_register (op[0], ZERO);
8fc2e39e 10685 break;
252b5132
RH
10686 }
10687
8fc2e39e 10688 used_at = 1;
67c0d1eb 10689 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
10690 macro_build (NULL, s, "z,s,t", op[1], AT);
10691 macro_build (NULL, s2, MFHL_FMT, op[0]);
252b5132
RH
10692 break;
10693
10694 case M_DIVU_3:
10695 s = "divu";
10696 s2 = "mflo";
10697 goto do_divu3;
10698 case M_REMU_3:
10699 s = "divu";
10700 s2 = "mfhi";
10701 goto do_divu3;
10702 case M_DDIVU_3:
10703 s = "ddivu";
10704 s2 = "mflo";
10705 goto do_divu3;
10706 case M_DREMU_3:
10707 s = "ddivu";
10708 s2 = "mfhi";
10709 do_divu3:
7d10b47d 10710 start_noreorder ();
252b5132
RH
10711 if (mips_trap)
10712 {
c0ebe874
RS
10713 macro_build (NULL, "teq", TRAP_FMT, op[2], ZERO, 7);
10714 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10715 /* We want to close the noreorder block as soon as possible, so
10716 that later insns are available for delay slot filling. */
7d10b47d 10717 end_noreorder ();
252b5132
RH
10718 }
10719 else
10720 {
df58fc94
RS
10721 if (mips_opts.micromips)
10722 micromips_label_expr (&label_expr);
10723 else
10724 label_expr.X_add_number = 8;
c0ebe874
RS
10725 macro_build (&label_expr, "bne", "s,t,p", op[2], ZERO);
10726 macro_build (NULL, s, "z,s,t", op[1], op[2]);
252b5132
RH
10727
10728 /* We want to close the noreorder block as soon as possible, so
10729 that later insns are available for delay slot filling. */
7d10b47d 10730 end_noreorder ();
df58fc94
RS
10731 macro_build (NULL, "break", BRK_FMT, 7);
10732 if (mips_opts.micromips)
10733 micromips_add_label ();
252b5132 10734 }
c0ebe874 10735 macro_build (NULL, s2, MFHL_FMT, op[0]);
8fc2e39e 10736 break;
252b5132 10737
1abe91b1
MR
10738 case M_DLCA_AB:
10739 dbl = 1;
1a0670f3 10740 /* Fall through. */
1abe91b1
MR
10741 case M_LCA_AB:
10742 call = 1;
10743 goto do_la;
252b5132
RH
10744 case M_DLA_AB:
10745 dbl = 1;
1a0670f3 10746 /* Fall through. */
252b5132 10747 case M_LA_AB:
1abe91b1 10748 do_la:
252b5132
RH
10749 /* Load the address of a symbol into a register. If breg is not
10750 zero, we then add a base register to it. */
10751
c0ebe874 10752 breg = op[2];
bad1aba3 10753 if (dbl && GPR_SIZE == 32)
ece794d9
MF
10754 as_warn (_("dla used to load 32-bit register; recommend using la "
10755 "instead"));
3bec30a8 10756
90ecf173 10757 if (!dbl && HAVE_64BIT_OBJECTS)
ece794d9
MF
10758 as_warn (_("la used to load 64-bit address; recommend using dla "
10759 "instead"));
3bec30a8 10760
f2ae14a1 10761 if (small_offset_p (0, align, 16))
0c11417f 10762 {
c0ebe874 10763 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", op[0], breg,
f2ae14a1 10764 -1, offset_reloc[0], offset_reloc[1], offset_reloc[2]);
8fc2e39e 10765 break;
0c11417f
MR
10766 }
10767
c0ebe874 10768 if (mips_opts.at && (op[0] == breg))
afdbd6d0
CD
10769 {
10770 tempreg = AT;
10771 used_at = 1;
10772 }
10773 else
c0ebe874 10774 tempreg = op[0];
afdbd6d0 10775
252b5132
RH
10776 if (offset_expr.X_op != O_symbol
10777 && offset_expr.X_op != O_constant)
10778 {
1661c76c 10779 as_bad (_("expression too complex"));
252b5132
RH
10780 offset_expr.X_op = O_constant;
10781 }
10782
252b5132 10783 if (offset_expr.X_op == O_constant)
aed1a261 10784 load_register (tempreg, &offset_expr, HAVE_64BIT_ADDRESSES);
252b5132
RH
10785 else if (mips_pic == NO_PIC)
10786 {
d6bc6245 10787 /* If this is a reference to a GP relative symbol, we want
cdf6fd85 10788 addiu $tempreg,$gp,<sym> (BFD_RELOC_GPREL16)
252b5132
RH
10789 Otherwise we want
10790 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
10791 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10792 If we have a constant, we need two instructions anyhow,
d6bc6245 10793 so we may as well always use the latter form.
76b3015f 10794
6caf9ef4
TS
10795 With 64bit address space and a usable $at we want
10796 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10797 lui $at,<sym> (BFD_RELOC_HI16_S)
10798 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10799 daddiu $at,<sym> (BFD_RELOC_LO16)
10800 dsll32 $tempreg,0
10801 daddu $tempreg,$tempreg,$at
10802
10803 If $at is already in use, we use a path which is suboptimal
10804 on superscalar processors.
10805 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
10806 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
10807 dsll $tempreg,16
10808 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
10809 dsll $tempreg,16
10810 daddiu $tempreg,<sym> (BFD_RELOC_LO16)
10811
10812 For GP relative symbols in 64bit address space we can use
10813 the same sequence as in 32bit address space. */
aed1a261 10814 if (HAVE_64BIT_SYMBOLS)
252b5132 10815 {
6caf9ef4
TS
10816 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
10817 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
10818 {
10819 relax_start (offset_expr.X_add_symbol);
10820 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10821 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
10822 relax_switch ();
10823 }
d6bc6245 10824
741fe287 10825 if (used_at == 0 && mips_opts.at)
98d3f06f 10826 {
df58fc94 10827 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10828 tempreg, BFD_RELOC_MIPS_HIGHEST);
df58fc94 10829 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10830 AT, BFD_RELOC_HI16_S);
67c0d1eb 10831 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10832 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
67c0d1eb 10833 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10834 AT, AT, BFD_RELOC_LO16);
df58fc94 10835 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 10836 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
98d3f06f
KH
10837 used_at = 1;
10838 }
10839 else
10840 {
df58fc94 10841 macro_build (&offset_expr, "lui", LUI_FMT,
17a2f251 10842 tempreg, BFD_RELOC_MIPS_HIGHEST);
67c0d1eb 10843 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10844 tempreg, tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 10845 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10846 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10847 tempreg, tempreg, BFD_RELOC_HI16_S);
df58fc94 10848 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb 10849 macro_build (&offset_expr, "daddiu", "t,r,j",
17a2f251 10850 tempreg, tempreg, BFD_RELOC_LO16);
98d3f06f 10851 }
6caf9ef4
TS
10852
10853 if (mips_relax.sequence)
10854 relax_end ();
98d3f06f
KH
10855 }
10856 else
10857 {
10858 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 10859 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
98d3f06f 10860 {
4d7206a2 10861 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10862 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10863 tempreg, mips_gp_register, BFD_RELOC_GPREL16);
4d7206a2 10864 relax_switch ();
98d3f06f 10865 }
6943caf0 10866 if (!IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
1661c76c 10867 as_bad (_("offset too large"));
67c0d1eb
RS
10868 macro_build_lui (&offset_expr, tempreg);
10869 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10870 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2
RS
10871 if (mips_relax.sequence)
10872 relax_end ();
98d3f06f 10873 }
252b5132 10874 }
0a44bf69 10875 else if (!mips_big_got && !HAVE_NEWABI)
252b5132 10876 {
9117d219
NC
10877 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
10878
252b5132
RH
10879 /* If this is a reference to an external symbol, and there
10880 is no constant, we want
10881 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
1abe91b1 10882 or for lca or if tempreg is PIC_CALL_REG
9117d219 10883 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
252b5132
RH
10884 For a local symbol, we want
10885 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10886 nop
10887 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
10888
10889 If we have a small constant, and this is a reference to
10890 an external symbol, we want
10891 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10892 nop
10893 addiu $tempreg,$tempreg,<constant>
10894 For a local symbol, we want the same instruction
10895 sequence, but we output a BFD_RELOC_LO16 reloc on the
10896 addiu instruction.
10897
10898 If we have a large constant, and this is a reference to
10899 an external symbol, we want
10900 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
10901 lui $at,<hiconstant>
10902 addiu $at,$at,<loconstant>
10903 addu $tempreg,$tempreg,$at
10904 For a local symbol, we want the same instruction
10905 sequence, but we output a BFD_RELOC_LO16 reloc on the
ed6fb7bd 10906 addiu instruction.
ed6fb7bd
SC
10907 */
10908
4d7206a2 10909 if (offset_expr.X_add_number == 0)
252b5132 10910 {
0a44bf69
RS
10911 if (mips_pic == SVR4_PIC
10912 && breg == 0
10913 && (call || tempreg == PIC_CALL_REG))
4d7206a2
RS
10914 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL16;
10915
10916 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
10917 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
10918 lw_reloc_type, mips_gp_register);
4d7206a2 10919 if (breg != 0)
252b5132
RH
10920 {
10921 /* We're going to put in an addu instruction using
10922 tempreg, so we may as well insert the nop right
10923 now. */
269137b2 10924 load_delay_nop ();
252b5132 10925 }
4d7206a2 10926 relax_switch ();
67c0d1eb
RS
10927 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
10928 tempreg, BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 10929 load_delay_nop ();
67c0d1eb
RS
10930 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
10931 tempreg, tempreg, BFD_RELOC_LO16);
4d7206a2 10932 relax_end ();
252b5132
RH
10933 /* FIXME: If breg == 0, and the next instruction uses
10934 $tempreg, then if this variant case is used an extra
10935 nop will be generated. */
10936 }
4d7206a2
RS
10937 else if (offset_expr.X_add_number >= -0x8000
10938 && offset_expr.X_add_number < 0x8000)
252b5132 10939 {
67c0d1eb 10940 load_got_offset (tempreg, &offset_expr);
269137b2 10941 load_delay_nop ();
67c0d1eb 10942 add_got_offset (tempreg, &offset_expr);
252b5132
RH
10943 }
10944 else
10945 {
4d7206a2
RS
10946 expr1.X_add_number = offset_expr.X_add_number;
10947 offset_expr.X_add_number =
43c0598f 10948 SEXT_16BIT (offset_expr.X_add_number);
67c0d1eb 10949 load_got_offset (tempreg, &offset_expr);
f6a22291 10950 offset_expr.X_add_number = expr1.X_add_number;
252b5132
RH
10951 /* If we are going to add in a base register, and the
10952 target register and the base register are the same,
10953 then we are using AT as a temporary register. Since
10954 we want to load the constant into AT, we add our
10955 current AT (from the global offset table) and the
10956 register into the register now, and pretend we were
10957 not using a base register. */
c0ebe874 10958 if (breg == op[0])
252b5132 10959 {
269137b2 10960 load_delay_nop ();
67c0d1eb 10961 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 10962 op[0], AT, breg);
252b5132 10963 breg = 0;
c0ebe874 10964 tempreg = op[0];
252b5132 10965 }
f6a22291 10966 add_got_offset_hilo (tempreg, &offset_expr, AT);
252b5132
RH
10967 used_at = 1;
10968 }
10969 }
0a44bf69 10970 else if (!mips_big_got && HAVE_NEWABI)
f5040a92 10971 {
67c0d1eb 10972 int add_breg_early = 0;
f5040a92
AO
10973
10974 /* If this is a reference to an external, and there is no
10975 constant, or local symbol (*), with or without a
10976 constant, we want
10977 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
1abe91b1 10978 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
10979 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
10980
10981 If we have a small constant, and this is a reference to
10982 an external symbol, we want
10983 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10984 addiu $tempreg,$tempreg,<constant>
10985
10986 If we have a large constant, and this is a reference to
10987 an external symbol, we want
10988 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_DISP)
10989 lui $at,<hiconstant>
10990 addiu $at,$at,<loconstant>
10991 addu $tempreg,$tempreg,$at
10992
10993 (*) Other assemblers seem to prefer GOT_PAGE/GOT_OFST for
10994 local symbols, even though it introduces an additional
10995 instruction. */
10996
f5040a92
AO
10997 if (offset_expr.X_add_number)
10998 {
4d7206a2 10999 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11000 offset_expr.X_add_number = 0;
11001
4d7206a2 11002 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11003 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11004 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11005
11006 if (expr1.X_add_number >= -0x8000
11007 && expr1.X_add_number < 0x8000)
11008 {
67c0d1eb
RS
11009 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
11010 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11011 }
ecd13cd3 11012 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11013 {
c0ebe874
RS
11014 unsigned int dreg;
11015
f5040a92
AO
11016 /* If we are going to add in a base register, and the
11017 target register and the base register are the same,
11018 then we are using AT as a temporary register. Since
11019 we want to load the constant into AT, we add our
11020 current AT (from the global offset table) and the
11021 register into the register now, and pretend we were
11022 not using a base register. */
c0ebe874 11023 if (breg != op[0])
f5040a92
AO
11024 dreg = tempreg;
11025 else
11026 {
9c2799c2 11027 gas_assert (tempreg == AT);
67c0d1eb 11028 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11029 op[0], AT, breg);
11030 dreg = op[0];
67c0d1eb 11031 add_breg_early = 1;
f5040a92
AO
11032 }
11033
f6a22291 11034 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11035 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11036 dreg, dreg, AT);
f5040a92 11037
f5040a92
AO
11038 used_at = 1;
11039 }
11040 else
11041 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11042
4d7206a2 11043 relax_switch ();
f5040a92
AO
11044 offset_expr.X_add_number = expr1.X_add_number;
11045
67c0d1eb
RS
11046 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11047 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
11048 if (add_breg_early)
f5040a92 11049 {
67c0d1eb 11050 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11051 op[0], tempreg, breg);
f5040a92 11052 breg = 0;
c0ebe874 11053 tempreg = op[0];
f5040a92 11054 }
4d7206a2 11055 relax_end ();
f5040a92 11056 }
4d7206a2 11057 else if (breg == 0 && (call || tempreg == PIC_CALL_REG))
f5040a92 11058 {
4d7206a2 11059 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11060 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11061 BFD_RELOC_MIPS_CALL16, mips_gp_register);
4d7206a2 11062 relax_switch ();
67c0d1eb
RS
11063 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11064 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
4d7206a2 11065 relax_end ();
f5040a92 11066 }
4d7206a2 11067 else
f5040a92 11068 {
67c0d1eb
RS
11069 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11070 BFD_RELOC_MIPS_GOT_DISP, mips_gp_register);
f5040a92
AO
11071 }
11072 }
0a44bf69 11073 else if (mips_big_got && !HAVE_NEWABI)
252b5132 11074 {
67c0d1eb 11075 int gpdelay;
9117d219
NC
11076 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11077 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
ed6fb7bd 11078 int local_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
252b5132
RH
11079
11080 /* This is the large GOT case. If this is a reference to an
11081 external symbol, and there is no constant, we want
11082 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11083 addu $tempreg,$tempreg,$gp
11084 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11085 or for lca or if tempreg is PIC_CALL_REG
9117d219
NC
11086 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11087 addu $tempreg,$tempreg,$gp
11088 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
252b5132
RH
11089 For a local symbol, we want
11090 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11091 nop
11092 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
11093
11094 If we have a small constant, and this is a reference to
11095 an external symbol, we want
11096 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11097 addu $tempreg,$tempreg,$gp
11098 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11099 nop
11100 addiu $tempreg,$tempreg,<constant>
11101 For a local symbol, we want
11102 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11103 nop
11104 addiu $tempreg,$tempreg,<constant> (BFD_RELOC_LO16)
11105
11106 If we have a large constant, and this is a reference to
11107 an external symbol, we want
11108 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11109 addu $tempreg,$tempreg,$gp
11110 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11111 lui $at,<hiconstant>
11112 addiu $at,$at,<loconstant>
11113 addu $tempreg,$tempreg,$at
11114 For a local symbol, we want
11115 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11116 lui $at,<hiconstant>
11117 addiu $at,$at,<loconstant> (BFD_RELOC_LO16)
11118 addu $tempreg,$tempreg,$at
f5040a92 11119 */
438c16b8 11120
252b5132
RH
11121 expr1.X_add_number = offset_expr.X_add_number;
11122 offset_expr.X_add_number = 0;
4d7206a2 11123 relax_start (offset_expr.X_add_symbol);
67c0d1eb 11124 gpdelay = reg_needs_delay (mips_gp_register);
1abe91b1
MR
11125 if (expr1.X_add_number == 0 && breg == 0
11126 && (call || tempreg == PIC_CALL_REG))
9117d219
NC
11127 {
11128 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11129 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11130 }
df58fc94 11131 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11132 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11133 tempreg, tempreg, mips_gp_register);
67c0d1eb 11134 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 11135 tempreg, lw_reloc_type, tempreg);
252b5132
RH
11136 if (expr1.X_add_number == 0)
11137 {
67c0d1eb 11138 if (breg != 0)
252b5132
RH
11139 {
11140 /* We're going to put in an addu instruction using
11141 tempreg, so we may as well insert the nop right
11142 now. */
269137b2 11143 load_delay_nop ();
252b5132 11144 }
252b5132
RH
11145 }
11146 else if (expr1.X_add_number >= -0x8000
11147 && expr1.X_add_number < 0x8000)
11148 {
269137b2 11149 load_delay_nop ();
67c0d1eb 11150 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11151 tempreg, tempreg, BFD_RELOC_LO16);
252b5132
RH
11152 }
11153 else
11154 {
c0ebe874
RS
11155 unsigned int dreg;
11156
252b5132
RH
11157 /* If we are going to add in a base register, and the
11158 target register and the base register are the same,
11159 then we are using AT as a temporary register. Since
11160 we want to load the constant into AT, we add our
11161 current AT (from the global offset table) and the
11162 register into the register now, and pretend we were
11163 not using a base register. */
c0ebe874 11164 if (breg != op[0])
67c0d1eb 11165 dreg = tempreg;
252b5132
RH
11166 else
11167 {
9c2799c2 11168 gas_assert (tempreg == AT);
269137b2 11169 load_delay_nop ();
67c0d1eb 11170 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11171 op[0], AT, breg);
11172 dreg = op[0];
252b5132
RH
11173 }
11174
f6a22291 11175 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11176 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
252b5132 11177
252b5132
RH
11178 used_at = 1;
11179 }
43c0598f 11180 offset_expr.X_add_number = SEXT_16BIT (expr1.X_add_number);
4d7206a2 11181 relax_switch ();
252b5132 11182
67c0d1eb 11183 if (gpdelay)
252b5132
RH
11184 {
11185 /* This is needed because this instruction uses $gp, but
f5040a92 11186 the first instruction on the main stream does not. */
67c0d1eb 11187 macro_build (NULL, "nop", "");
252b5132 11188 }
ed6fb7bd 11189
67c0d1eb
RS
11190 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11191 local_reloc_type, mips_gp_register);
f5040a92 11192 if (expr1.X_add_number >= -0x8000
252b5132
RH
11193 && expr1.X_add_number < 0x8000)
11194 {
269137b2 11195 load_delay_nop ();
67c0d1eb
RS
11196 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11197 tempreg, tempreg, BFD_RELOC_LO16);
252b5132 11198 /* FIXME: If add_number is 0, and there was no base
f5040a92
AO
11199 register, the external symbol case ended with a load,
11200 so if the symbol turns out to not be external, and
11201 the next instruction uses tempreg, an unnecessary nop
11202 will be inserted. */
252b5132
RH
11203 }
11204 else
11205 {
c0ebe874 11206 if (breg == op[0])
252b5132
RH
11207 {
11208 /* We must add in the base register now, as in the
f5040a92 11209 external symbol case. */
9c2799c2 11210 gas_assert (tempreg == AT);
269137b2 11211 load_delay_nop ();
67c0d1eb 11212 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11213 op[0], AT, breg);
11214 tempreg = op[0];
252b5132 11215 /* We set breg to 0 because we have arranged to add
f5040a92 11216 it in in both cases. */
252b5132
RH
11217 breg = 0;
11218 }
11219
67c0d1eb
RS
11220 macro_build_lui (&expr1, AT);
11221 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11222 AT, AT, BFD_RELOC_LO16);
67c0d1eb 11223 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11224 tempreg, tempreg, AT);
8fc2e39e 11225 used_at = 1;
252b5132 11226 }
4d7206a2 11227 relax_end ();
252b5132 11228 }
0a44bf69 11229 else if (mips_big_got && HAVE_NEWABI)
f5040a92 11230 {
f5040a92
AO
11231 int lui_reloc_type = (int) BFD_RELOC_MIPS_GOT_HI16;
11232 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT_LO16;
67c0d1eb 11233 int add_breg_early = 0;
f5040a92
AO
11234
11235 /* This is the large GOT case. If this is a reference to an
11236 external symbol, and there is no constant, we want
11237 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11238 add $tempreg,$tempreg,$gp
11239 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
1abe91b1 11240 or for lca or if tempreg is PIC_CALL_REG
f5040a92
AO
11241 lui $tempreg,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11242 add $tempreg,$tempreg,$gp
11243 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_CALL_LO16)
11244
11245 If we have a small constant, and this is a reference to
11246 an external symbol, we want
11247 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11248 add $tempreg,$tempreg,$gp
11249 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11250 addi $tempreg,$tempreg,<constant>
11251
11252 If we have a large constant, and this is a reference to
11253 an external symbol, we want
11254 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
11255 addu $tempreg,$tempreg,$gp
11256 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
11257 lui $at,<hiconstant>
11258 addi $at,$at,<loconstant>
11259 add $tempreg,$tempreg,$at
11260
11261 If we have NewABI, and we know it's a local symbol, we want
11262 lw $reg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
11263 addiu $reg,$reg,<sym> (BFD_RELOC_MIPS_GOT_OFST)
11264 otherwise we have to resort to GOT_HI16/GOT_LO16. */
11265
4d7206a2 11266 relax_start (offset_expr.X_add_symbol);
f5040a92 11267
4d7206a2 11268 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
11269 offset_expr.X_add_number = 0;
11270
1abe91b1
MR
11271 if (expr1.X_add_number == 0 && breg == 0
11272 && (call || tempreg == PIC_CALL_REG))
f5040a92
AO
11273 {
11274 lui_reloc_type = (int) BFD_RELOC_MIPS_CALL_HI16;
11275 lw_reloc_type = (int) BFD_RELOC_MIPS_CALL_LO16;
11276 }
df58fc94 11277 macro_build (&offset_expr, "lui", LUI_FMT, tempreg, lui_reloc_type);
67c0d1eb 11278 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 11279 tempreg, tempreg, mips_gp_register);
67c0d1eb
RS
11280 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11281 tempreg, lw_reloc_type, tempreg);
f5040a92
AO
11282
11283 if (expr1.X_add_number == 0)
4d7206a2 11284 ;
f5040a92
AO
11285 else if (expr1.X_add_number >= -0x8000
11286 && expr1.X_add_number < 0x8000)
11287 {
67c0d1eb 11288 macro_build (&expr1, ADDRESS_ADDI_INSN, "t,r,j",
17a2f251 11289 tempreg, tempreg, BFD_RELOC_LO16);
f5040a92 11290 }
ecd13cd3 11291 else if (IS_SEXT_32BIT_NUM (expr1.X_add_number + 0x8000))
f5040a92 11292 {
c0ebe874
RS
11293 unsigned int dreg;
11294
f5040a92
AO
11295 /* If we are going to add in a base register, and the
11296 target register and the base register are the same,
11297 then we are using AT as a temporary register. Since
11298 we want to load the constant into AT, we add our
11299 current AT (from the global offset table) and the
11300 register into the register now, and pretend we were
11301 not using a base register. */
c0ebe874 11302 if (breg != op[0])
f5040a92
AO
11303 dreg = tempreg;
11304 else
11305 {
9c2799c2 11306 gas_assert (tempreg == AT);
67c0d1eb 11307 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874
RS
11308 op[0], AT, breg);
11309 dreg = op[0];
67c0d1eb 11310 add_breg_early = 1;
f5040a92
AO
11311 }
11312
f6a22291 11313 load_register (AT, &expr1, HAVE_64BIT_ADDRESSES);
67c0d1eb 11314 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", dreg, dreg, AT);
f5040a92 11315
f5040a92
AO
11316 used_at = 1;
11317 }
11318 else
11319 as_bad (_("PIC code offset overflow (max 32 signed bits)"));
11320
4d7206a2 11321 relax_switch ();
f5040a92 11322 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
11323 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
11324 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
11325 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
11326 tempreg, BFD_RELOC_MIPS_GOT_OFST);
11327 if (add_breg_early)
f5040a92 11328 {
67c0d1eb 11329 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
c0ebe874 11330 op[0], tempreg, breg);
f5040a92 11331 breg = 0;
c0ebe874 11332 tempreg = op[0];
f5040a92 11333 }
4d7206a2 11334 relax_end ();
f5040a92 11335 }
252b5132
RH
11336 else
11337 abort ();
11338
11339 if (breg != 0)
c0ebe874 11340 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", op[0], tempreg, breg);
252b5132
RH
11341 break;
11342
52b6b6b9 11343 case M_MSGSND:
df58fc94 11344 gas_assert (!mips_opts.micromips);
c0ebe874 11345 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x01);
c7af4273 11346 break;
52b6b6b9
JM
11347
11348 case M_MSGLD:
df58fc94 11349 gas_assert (!mips_opts.micromips);
c8276761 11350 macro_build (NULL, "c2", "C", 0x02);
c7af4273 11351 break;
52b6b6b9
JM
11352
11353 case M_MSGLD_T:
df58fc94 11354 gas_assert (!mips_opts.micromips);
c0ebe874 11355 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x02);
c7af4273 11356 break;
52b6b6b9
JM
11357
11358 case M_MSGWAIT:
df58fc94 11359 gas_assert (!mips_opts.micromips);
52b6b6b9 11360 macro_build (NULL, "c2", "C", 3);
c7af4273 11361 break;
52b6b6b9
JM
11362
11363 case M_MSGWAIT_T:
df58fc94 11364 gas_assert (!mips_opts.micromips);
c0ebe874 11365 macro_build (NULL, "c2", "C", (op[0] << 16) | 0x03);
c7af4273 11366 break;
52b6b6b9 11367
252b5132
RH
11368 case M_J_A:
11369 /* The j instruction may not be used in PIC code, since it
11370 requires an absolute address. We convert it to a b
11371 instruction. */
11372 if (mips_pic == NO_PIC)
67c0d1eb 11373 macro_build (&offset_expr, "j", "a");
252b5132 11374 else
67c0d1eb 11375 macro_build (&offset_expr, "b", "p");
8fc2e39e 11376 break;
252b5132
RH
11377
11378 /* The jal instructions must be handled as macros because when
11379 generating PIC code they expand to multi-instruction
11380 sequences. Normally they are simple instructions. */
df58fc94 11381 case M_JALS_1:
c0ebe874
RS
11382 op[1] = op[0];
11383 op[0] = RA;
df58fc94
RS
11384 /* Fall through. */
11385 case M_JALS_2:
11386 gas_assert (mips_opts.micromips);
833794fc
MR
11387 if (mips_opts.insn32)
11388 {
1661c76c 11389 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11390 break;
11391 }
df58fc94
RS
11392 jals = 1;
11393 goto jal;
252b5132 11394 case M_JAL_1:
c0ebe874
RS
11395 op[1] = op[0];
11396 op[0] = RA;
252b5132
RH
11397 /* Fall through. */
11398 case M_JAL_2:
df58fc94 11399 jal:
3e722fb5 11400 if (mips_pic == NO_PIC)
df58fc94
RS
11401 {
11402 s = jals ? "jalrs" : "jalr";
e64af278 11403 if (mips_opts.micromips
833794fc 11404 && !mips_opts.insn32
c0ebe874 11405 && op[0] == RA
e64af278 11406 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11407 macro_build (NULL, s, "mj", op[1]);
df58fc94 11408 else
c0ebe874 11409 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
df58fc94 11410 }
0a44bf69 11411 else
252b5132 11412 {
df58fc94
RS
11413 int cprestore = (mips_pic == SVR4_PIC && !HAVE_NEWABI
11414 && mips_cprestore_offset >= 0);
11415
c0ebe874 11416 if (op[1] != PIC_CALL_REG)
252b5132 11417 as_warn (_("MIPS PIC call to register other than $25"));
bdaaa2e1 11418
833794fc
MR
11419 s = ((mips_opts.micromips
11420 && !mips_opts.insn32
11421 && (!mips_opts.noreorder || cprestore))
df58fc94 11422 ? "jalrs" : "jalr");
e64af278 11423 if (mips_opts.micromips
833794fc 11424 && !mips_opts.insn32
c0ebe874 11425 && op[0] == RA
e64af278 11426 && !(history[0].insn_mo->pinfo2 & INSN2_BRANCH_DELAY_32BIT))
c0ebe874 11427 macro_build (NULL, s, "mj", op[1]);
df58fc94 11428 else
c0ebe874 11429 macro_build (NULL, s, JALR_FMT, op[0], op[1]);
0a44bf69 11430 if (mips_pic == SVR4_PIC && !HAVE_NEWABI)
252b5132 11431 {
6478892d 11432 if (mips_cprestore_offset < 0)
1661c76c 11433 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11434 else
11435 {
90ecf173 11436 if (!mips_frame_reg_valid)
7a621144 11437 {
1661c76c 11438 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11439 /* Quiet this warning. */
11440 mips_frame_reg_valid = 1;
11441 }
90ecf173 11442 if (!mips_cprestore_valid)
7a621144 11443 {
1661c76c 11444 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11445 /* Quiet this warning. */
11446 mips_cprestore_valid = 1;
11447 }
d3fca0b5
MR
11448 if (mips_opts.noreorder)
11449 macro_build (NULL, "nop", "");
6478892d 11450 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11451 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11452 mips_gp_register,
256ab948
TS
11453 mips_frame_reg,
11454 HAVE_64BIT_ADDRESSES);
6478892d 11455 }
252b5132
RH
11456 }
11457 }
252b5132 11458
8fc2e39e 11459 break;
252b5132 11460
df58fc94
RS
11461 case M_JALS_A:
11462 gas_assert (mips_opts.micromips);
833794fc
MR
11463 if (mips_opts.insn32)
11464 {
1661c76c 11465 as_bad (_("opcode not supported in the `insn32' mode `%s'"), str);
833794fc
MR
11466 break;
11467 }
df58fc94
RS
11468 jals = 1;
11469 /* Fall through. */
252b5132
RH
11470 case M_JAL_A:
11471 if (mips_pic == NO_PIC)
df58fc94 11472 macro_build (&offset_expr, jals ? "jals" : "jal", "a");
252b5132
RH
11473 else if (mips_pic == SVR4_PIC)
11474 {
11475 /* If this is a reference to an external symbol, and we are
11476 using a small GOT, we want
11477 lw $25,<sym>($gp) (BFD_RELOC_MIPS_CALL16)
11478 nop
f9419b05 11479 jalr $ra,$25
252b5132
RH
11480 nop
11481 lw $gp,cprestore($sp)
11482 The cprestore value is set using the .cprestore
11483 pseudo-op. If we are using a big GOT, we want
11484 lui $25,<sym> (BFD_RELOC_MIPS_CALL_HI16)
11485 addu $25,$25,$gp
11486 lw $25,<sym>($25) (BFD_RELOC_MIPS_CALL_LO16)
11487 nop
f9419b05 11488 jalr $ra,$25
252b5132
RH
11489 nop
11490 lw $gp,cprestore($sp)
11491 If the symbol is not external, we want
11492 lw $25,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
11493 nop
11494 addiu $25,$25,<sym> (BFD_RELOC_LO16)
f9419b05 11495 jalr $ra,$25
252b5132 11496 nop
438c16b8 11497 lw $gp,cprestore($sp)
f5040a92
AO
11498
11499 For NewABI, we use the same CALL16 or CALL_HI16/CALL_LO16
11500 sequences above, minus nops, unless the symbol is local,
11501 which enables us to use GOT_PAGE/GOT_OFST (big got) or
11502 GOT_DISP. */
438c16b8 11503 if (HAVE_NEWABI)
252b5132 11504 {
90ecf173 11505 if (!mips_big_got)
f5040a92 11506 {
4d7206a2 11507 relax_start (offset_expr.X_add_symbol);
67c0d1eb
RS
11508 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11509 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
f5040a92 11510 mips_gp_register);
4d7206a2 11511 relax_switch ();
67c0d1eb
RS
11512 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11513 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_DISP,
4d7206a2
RS
11514 mips_gp_register);
11515 relax_end ();
f5040a92
AO
11516 }
11517 else
11518 {
4d7206a2 11519 relax_start (offset_expr.X_add_symbol);
df58fc94 11520 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11521 BFD_RELOC_MIPS_CALL_HI16);
11522 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11523 PIC_CALL_REG, mips_gp_register);
11524 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11525 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11526 PIC_CALL_REG);
4d7206a2 11527 relax_switch ();
67c0d1eb
RS
11528 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11529 PIC_CALL_REG, BFD_RELOC_MIPS_GOT_PAGE,
11530 mips_gp_register);
11531 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11532 PIC_CALL_REG, PIC_CALL_REG,
17a2f251 11533 BFD_RELOC_MIPS_GOT_OFST);
4d7206a2 11534 relax_end ();
f5040a92 11535 }
684022ea 11536
df58fc94 11537 macro_build_jalr (&offset_expr, 0);
252b5132
RH
11538 }
11539 else
11540 {
4d7206a2 11541 relax_start (offset_expr.X_add_symbol);
90ecf173 11542 if (!mips_big_got)
438c16b8 11543 {
67c0d1eb
RS
11544 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11545 PIC_CALL_REG, BFD_RELOC_MIPS_CALL16,
17a2f251 11546 mips_gp_register);
269137b2 11547 load_delay_nop ();
4d7206a2 11548 relax_switch ();
438c16b8 11549 }
252b5132 11550 else
252b5132 11551 {
67c0d1eb
RS
11552 int gpdelay;
11553
11554 gpdelay = reg_needs_delay (mips_gp_register);
df58fc94 11555 macro_build (&offset_expr, "lui", LUI_FMT, PIC_CALL_REG,
67c0d1eb
RS
11556 BFD_RELOC_MIPS_CALL_HI16);
11557 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", PIC_CALL_REG,
11558 PIC_CALL_REG, mips_gp_register);
11559 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11560 PIC_CALL_REG, BFD_RELOC_MIPS_CALL_LO16,
11561 PIC_CALL_REG);
269137b2 11562 load_delay_nop ();
4d7206a2 11563 relax_switch ();
67c0d1eb
RS
11564 if (gpdelay)
11565 macro_build (NULL, "nop", "");
252b5132 11566 }
67c0d1eb
RS
11567 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
11568 PIC_CALL_REG, BFD_RELOC_MIPS_GOT16,
4d7206a2 11569 mips_gp_register);
269137b2 11570 load_delay_nop ();
67c0d1eb
RS
11571 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
11572 PIC_CALL_REG, PIC_CALL_REG, BFD_RELOC_LO16);
4d7206a2 11573 relax_end ();
df58fc94 11574 macro_build_jalr (&offset_expr, mips_cprestore_offset >= 0);
438c16b8 11575
6478892d 11576 if (mips_cprestore_offset < 0)
1661c76c 11577 as_warn (_("no .cprestore pseudo-op used in PIC code"));
6478892d
TS
11578 else
11579 {
90ecf173 11580 if (!mips_frame_reg_valid)
7a621144 11581 {
1661c76c 11582 as_warn (_("no .frame pseudo-op used in PIC code"));
7a621144
DJ
11583 /* Quiet this warning. */
11584 mips_frame_reg_valid = 1;
11585 }
90ecf173 11586 if (!mips_cprestore_valid)
7a621144 11587 {
1661c76c 11588 as_warn (_("no .cprestore pseudo-op used in PIC code"));
7a621144
DJ
11589 /* Quiet this warning. */
11590 mips_cprestore_valid = 1;
11591 }
6478892d 11592 if (mips_opts.noreorder)
67c0d1eb 11593 macro_build (NULL, "nop", "");
6478892d 11594 expr1.X_add_number = mips_cprestore_offset;
134c0c8b 11595 macro_build_ldst_constoffset (&expr1, ADDRESS_LOAD_INSN,
f899b4b8 11596 mips_gp_register,
256ab948
TS
11597 mips_frame_reg,
11598 HAVE_64BIT_ADDRESSES);
6478892d 11599 }
252b5132
RH
11600 }
11601 }
0a44bf69 11602 else if (mips_pic == VXWORKS_PIC)
1661c76c 11603 as_bad (_("non-PIC jump used in PIC library"));
252b5132
RH
11604 else
11605 abort ();
11606
8fc2e39e 11607 break;
252b5132 11608
7f3c4072 11609 case M_LBUE_AB:
7f3c4072
CM
11610 s = "lbue";
11611 fmt = "t,+j(b)";
11612 offbits = 9;
11613 goto ld_st;
11614 case M_LHUE_AB:
7f3c4072
CM
11615 s = "lhue";
11616 fmt = "t,+j(b)";
11617 offbits = 9;
11618 goto ld_st;
11619 case M_LBE_AB:
7f3c4072
CM
11620 s = "lbe";
11621 fmt = "t,+j(b)";
11622 offbits = 9;
11623 goto ld_st;
11624 case M_LHE_AB:
7f3c4072
CM
11625 s = "lhe";
11626 fmt = "t,+j(b)";
11627 offbits = 9;
11628 goto ld_st;
11629 case M_LLE_AB:
7f3c4072
CM
11630 s = "lle";
11631 fmt = "t,+j(b)";
11632 offbits = 9;
11633 goto ld_st;
11634 case M_LWE_AB:
7f3c4072
CM
11635 s = "lwe";
11636 fmt = "t,+j(b)";
11637 offbits = 9;
11638 goto ld_st;
11639 case M_LWLE_AB:
7f3c4072
CM
11640 s = "lwle";
11641 fmt = "t,+j(b)";
11642 offbits = 9;
11643 goto ld_st;
11644 case M_LWRE_AB:
7f3c4072
CM
11645 s = "lwre";
11646 fmt = "t,+j(b)";
11647 offbits = 9;
11648 goto ld_st;
11649 case M_SBE_AB:
7f3c4072
CM
11650 s = "sbe";
11651 fmt = "t,+j(b)";
11652 offbits = 9;
11653 goto ld_st;
11654 case M_SCE_AB:
7f3c4072
CM
11655 s = "sce";
11656 fmt = "t,+j(b)";
11657 offbits = 9;
11658 goto ld_st;
11659 case M_SHE_AB:
7f3c4072
CM
11660 s = "she";
11661 fmt = "t,+j(b)";
11662 offbits = 9;
11663 goto ld_st;
11664 case M_SWE_AB:
7f3c4072
CM
11665 s = "swe";
11666 fmt = "t,+j(b)";
11667 offbits = 9;
11668 goto ld_st;
11669 case M_SWLE_AB:
7f3c4072
CM
11670 s = "swle";
11671 fmt = "t,+j(b)";
11672 offbits = 9;
11673 goto ld_st;
11674 case M_SWRE_AB:
7f3c4072
CM
11675 s = "swre";
11676 fmt = "t,+j(b)";
11677 offbits = 9;
11678 goto ld_st;
dec0624d 11679 case M_ACLR_AB:
dec0624d 11680 s = "aclr";
dec0624d 11681 fmt = "\\,~(b)";
7f3c4072 11682 offbits = 12;
dec0624d
MR
11683 goto ld_st;
11684 case M_ASET_AB:
dec0624d 11685 s = "aset";
dec0624d 11686 fmt = "\\,~(b)";
7f3c4072 11687 offbits = 12;
dec0624d 11688 goto ld_st;
252b5132
RH
11689 case M_LB_AB:
11690 s = "lb";
df58fc94 11691 fmt = "t,o(b)";
252b5132
RH
11692 goto ld;
11693 case M_LBU_AB:
11694 s = "lbu";
df58fc94 11695 fmt = "t,o(b)";
252b5132
RH
11696 goto ld;
11697 case M_LH_AB:
11698 s = "lh";
df58fc94 11699 fmt = "t,o(b)";
252b5132
RH
11700 goto ld;
11701 case M_LHU_AB:
11702 s = "lhu";
df58fc94 11703 fmt = "t,o(b)";
252b5132
RH
11704 goto ld;
11705 case M_LW_AB:
11706 s = "lw";
df58fc94 11707 fmt = "t,o(b)";
252b5132
RH
11708 goto ld;
11709 case M_LWC0_AB:
df58fc94 11710 gas_assert (!mips_opts.micromips);
252b5132 11711 s = "lwc0";
df58fc94 11712 fmt = "E,o(b)";
bdaaa2e1 11713 /* Itbl support may require additional care here. */
252b5132 11714 coproc = 1;
df58fc94 11715 goto ld_st;
252b5132
RH
11716 case M_LWC1_AB:
11717 s = "lwc1";
df58fc94 11718 fmt = "T,o(b)";
bdaaa2e1 11719 /* Itbl support may require additional care here. */
252b5132 11720 coproc = 1;
df58fc94 11721 goto ld_st;
252b5132
RH
11722 case M_LWC2_AB:
11723 s = "lwc2";
df58fc94 11724 fmt = COP12_FMT;
7361da2c
AB
11725 offbits = (mips_opts.micromips ? 12
11726 : ISA_IS_R6 (mips_opts.isa) ? 11
11727 : 16);
bdaaa2e1 11728 /* Itbl support may require additional care here. */
252b5132 11729 coproc = 1;
df58fc94 11730 goto ld_st;
252b5132 11731 case M_LWC3_AB:
df58fc94 11732 gas_assert (!mips_opts.micromips);
252b5132 11733 s = "lwc3";
df58fc94 11734 fmt = "E,o(b)";
bdaaa2e1 11735 /* Itbl support may require additional care here. */
252b5132 11736 coproc = 1;
df58fc94 11737 goto ld_st;
252b5132
RH
11738 case M_LWL_AB:
11739 s = "lwl";
df58fc94 11740 fmt = MEM12_FMT;
7f3c4072 11741 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11742 goto ld_st;
252b5132
RH
11743 case M_LWR_AB:
11744 s = "lwr";
df58fc94 11745 fmt = MEM12_FMT;
7f3c4072 11746 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11747 goto ld_st;
252b5132 11748 case M_LDC1_AB:
252b5132 11749 s = "ldc1";
df58fc94 11750 fmt = "T,o(b)";
bdaaa2e1 11751 /* Itbl support may require additional care here. */
252b5132 11752 coproc = 1;
df58fc94 11753 goto ld_st;
252b5132
RH
11754 case M_LDC2_AB:
11755 s = "ldc2";
df58fc94 11756 fmt = COP12_FMT;
7361da2c
AB
11757 offbits = (mips_opts.micromips ? 12
11758 : ISA_IS_R6 (mips_opts.isa) ? 11
11759 : 16);
bdaaa2e1 11760 /* Itbl support may require additional care here. */
252b5132 11761 coproc = 1;
df58fc94 11762 goto ld_st;
c77c0862 11763 case M_LQC2_AB:
c77c0862 11764 s = "lqc2";
14daeee3 11765 fmt = "+7,o(b)";
c77c0862
RS
11766 /* Itbl support may require additional care here. */
11767 coproc = 1;
11768 goto ld_st;
252b5132
RH
11769 case M_LDC3_AB:
11770 s = "ldc3";
df58fc94 11771 fmt = "E,o(b)";
bdaaa2e1 11772 /* Itbl support may require additional care here. */
252b5132 11773 coproc = 1;
df58fc94 11774 goto ld_st;
252b5132
RH
11775 case M_LDL_AB:
11776 s = "ldl";
df58fc94 11777 fmt = MEM12_FMT;
7f3c4072 11778 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11779 goto ld_st;
252b5132
RH
11780 case M_LDR_AB:
11781 s = "ldr";
df58fc94 11782 fmt = MEM12_FMT;
7f3c4072 11783 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11784 goto ld_st;
252b5132
RH
11785 case M_LL_AB:
11786 s = "ll";
7361da2c
AB
11787 fmt = LL_SC_FMT;
11788 offbits = (mips_opts.micromips ? 12
11789 : ISA_IS_R6 (mips_opts.isa) ? 9
11790 : 16);
252b5132
RH
11791 goto ld;
11792 case M_LLD_AB:
11793 s = "lld";
7361da2c
AB
11794 fmt = LL_SC_FMT;
11795 offbits = (mips_opts.micromips ? 12
11796 : ISA_IS_R6 (mips_opts.isa) ? 9
11797 : 16);
252b5132
RH
11798 goto ld;
11799 case M_LWU_AB:
11800 s = "lwu";
df58fc94 11801 fmt = MEM12_FMT;
7f3c4072 11802 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11803 goto ld;
11804 case M_LWP_AB:
df58fc94
RS
11805 gas_assert (mips_opts.micromips);
11806 s = "lwp";
11807 fmt = "t,~(b)";
7f3c4072 11808 offbits = 12;
df58fc94
RS
11809 lp = 1;
11810 goto ld;
11811 case M_LDP_AB:
df58fc94
RS
11812 gas_assert (mips_opts.micromips);
11813 s = "ldp";
11814 fmt = "t,~(b)";
7f3c4072 11815 offbits = 12;
df58fc94
RS
11816 lp = 1;
11817 goto ld;
11818 case M_LWM_AB:
df58fc94
RS
11819 gas_assert (mips_opts.micromips);
11820 s = "lwm";
11821 fmt = "n,~(b)";
7f3c4072 11822 offbits = 12;
df58fc94
RS
11823 goto ld_st;
11824 case M_LDM_AB:
df58fc94
RS
11825 gas_assert (mips_opts.micromips);
11826 s = "ldm";
11827 fmt = "n,~(b)";
7f3c4072 11828 offbits = 12;
df58fc94
RS
11829 goto ld_st;
11830
252b5132 11831 ld:
f19ccbda 11832 /* We don't want to use $0 as tempreg. */
c0ebe874 11833 if (op[2] == op[0] + lp || op[0] + lp == ZERO)
df58fc94 11834 goto ld_st;
252b5132 11835 else
c0ebe874 11836 tempreg = op[0] + lp;
df58fc94
RS
11837 goto ld_noat;
11838
252b5132
RH
11839 case M_SB_AB:
11840 s = "sb";
df58fc94
RS
11841 fmt = "t,o(b)";
11842 goto ld_st;
252b5132
RH
11843 case M_SH_AB:
11844 s = "sh";
df58fc94
RS
11845 fmt = "t,o(b)";
11846 goto ld_st;
252b5132
RH
11847 case M_SW_AB:
11848 s = "sw";
df58fc94
RS
11849 fmt = "t,o(b)";
11850 goto ld_st;
252b5132 11851 case M_SWC0_AB:
df58fc94 11852 gas_assert (!mips_opts.micromips);
252b5132 11853 s = "swc0";
df58fc94 11854 fmt = "E,o(b)";
bdaaa2e1 11855 /* Itbl support may require additional care here. */
252b5132 11856 coproc = 1;
df58fc94 11857 goto ld_st;
252b5132
RH
11858 case M_SWC1_AB:
11859 s = "swc1";
df58fc94 11860 fmt = "T,o(b)";
bdaaa2e1 11861 /* Itbl support may require additional care here. */
252b5132 11862 coproc = 1;
df58fc94 11863 goto ld_st;
252b5132
RH
11864 case M_SWC2_AB:
11865 s = "swc2";
df58fc94 11866 fmt = COP12_FMT;
7361da2c
AB
11867 offbits = (mips_opts.micromips ? 12
11868 : ISA_IS_R6 (mips_opts.isa) ? 11
11869 : 16);
bdaaa2e1 11870 /* Itbl support may require additional care here. */
252b5132 11871 coproc = 1;
df58fc94 11872 goto ld_st;
252b5132 11873 case M_SWC3_AB:
df58fc94 11874 gas_assert (!mips_opts.micromips);
252b5132 11875 s = "swc3";
df58fc94 11876 fmt = "E,o(b)";
bdaaa2e1 11877 /* Itbl support may require additional care here. */
252b5132 11878 coproc = 1;
df58fc94 11879 goto ld_st;
252b5132
RH
11880 case M_SWL_AB:
11881 s = "swl";
df58fc94 11882 fmt = MEM12_FMT;
7f3c4072 11883 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11884 goto ld_st;
252b5132
RH
11885 case M_SWR_AB:
11886 s = "swr";
df58fc94 11887 fmt = MEM12_FMT;
7f3c4072 11888 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11889 goto ld_st;
252b5132
RH
11890 case M_SC_AB:
11891 s = "sc";
7361da2c
AB
11892 fmt = LL_SC_FMT;
11893 offbits = (mips_opts.micromips ? 12
11894 : ISA_IS_R6 (mips_opts.isa) ? 9
11895 : 16);
df58fc94 11896 goto ld_st;
252b5132
RH
11897 case M_SCD_AB:
11898 s = "scd";
7361da2c
AB
11899 fmt = LL_SC_FMT;
11900 offbits = (mips_opts.micromips ? 12
11901 : ISA_IS_R6 (mips_opts.isa) ? 9
11902 : 16);
df58fc94 11903 goto ld_st;
d43b4baf
TS
11904 case M_CACHE_AB:
11905 s = "cache";
7361da2c
AB
11906 fmt = (mips_opts.micromips ? "k,~(b)"
11907 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11908 : "k,o(b)");
11909 offbits = (mips_opts.micromips ? 12
11910 : ISA_IS_R6 (mips_opts.isa) ? 9
11911 : 16);
7f3c4072
CM
11912 goto ld_st;
11913 case M_CACHEE_AB:
7f3c4072
CM
11914 s = "cachee";
11915 fmt = "k,+j(b)";
11916 offbits = 9;
df58fc94 11917 goto ld_st;
3eebd5eb
MR
11918 case M_PREF_AB:
11919 s = "pref";
7361da2c
AB
11920 fmt = (mips_opts.micromips ? "k,~(b)"
11921 : ISA_IS_R6 (mips_opts.isa) ? "k,+j(b)"
11922 : "k,o(b)");
11923 offbits = (mips_opts.micromips ? 12
11924 : ISA_IS_R6 (mips_opts.isa) ? 9
11925 : 16);
7f3c4072
CM
11926 goto ld_st;
11927 case M_PREFE_AB:
7f3c4072
CM
11928 s = "prefe";
11929 fmt = "k,+j(b)";
11930 offbits = 9;
df58fc94 11931 goto ld_st;
252b5132 11932 case M_SDC1_AB:
252b5132 11933 s = "sdc1";
df58fc94 11934 fmt = "T,o(b)";
252b5132 11935 coproc = 1;
bdaaa2e1 11936 /* Itbl support may require additional care here. */
df58fc94 11937 goto ld_st;
252b5132
RH
11938 case M_SDC2_AB:
11939 s = "sdc2";
df58fc94 11940 fmt = COP12_FMT;
7361da2c
AB
11941 offbits = (mips_opts.micromips ? 12
11942 : ISA_IS_R6 (mips_opts.isa) ? 11
11943 : 16);
c77c0862
RS
11944 /* Itbl support may require additional care here. */
11945 coproc = 1;
11946 goto ld_st;
11947 case M_SQC2_AB:
c77c0862 11948 s = "sqc2";
14daeee3 11949 fmt = "+7,o(b)";
bdaaa2e1 11950 /* Itbl support may require additional care here. */
252b5132 11951 coproc = 1;
df58fc94 11952 goto ld_st;
252b5132 11953 case M_SDC3_AB:
df58fc94 11954 gas_assert (!mips_opts.micromips);
252b5132 11955 s = "sdc3";
df58fc94 11956 fmt = "E,o(b)";
bdaaa2e1 11957 /* Itbl support may require additional care here. */
252b5132 11958 coproc = 1;
df58fc94 11959 goto ld_st;
252b5132
RH
11960 case M_SDL_AB:
11961 s = "sdl";
df58fc94 11962 fmt = MEM12_FMT;
7f3c4072 11963 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94 11964 goto ld_st;
252b5132
RH
11965 case M_SDR_AB:
11966 s = "sdr";
df58fc94 11967 fmt = MEM12_FMT;
7f3c4072 11968 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
11969 goto ld_st;
11970 case M_SWP_AB:
df58fc94
RS
11971 gas_assert (mips_opts.micromips);
11972 s = "swp";
11973 fmt = "t,~(b)";
7f3c4072 11974 offbits = 12;
df58fc94
RS
11975 goto ld_st;
11976 case M_SDP_AB:
df58fc94
RS
11977 gas_assert (mips_opts.micromips);
11978 s = "sdp";
11979 fmt = "t,~(b)";
7f3c4072 11980 offbits = 12;
df58fc94
RS
11981 goto ld_st;
11982 case M_SWM_AB:
df58fc94
RS
11983 gas_assert (mips_opts.micromips);
11984 s = "swm";
11985 fmt = "n,~(b)";
7f3c4072 11986 offbits = 12;
df58fc94
RS
11987 goto ld_st;
11988 case M_SDM_AB:
df58fc94
RS
11989 gas_assert (mips_opts.micromips);
11990 s = "sdm";
11991 fmt = "n,~(b)";
7f3c4072 11992 offbits = 12;
df58fc94
RS
11993
11994 ld_st:
8fc2e39e 11995 tempreg = AT;
df58fc94 11996 ld_noat:
c0ebe874 11997 breg = op[2];
f2ae14a1
RS
11998 if (small_offset_p (0, align, 16))
11999 {
12000 /* The first case exists for M_LD_AB and M_SD_AB, which are
12001 macros for o32 but which should act like normal instructions
12002 otherwise. */
12003 if (offbits == 16)
c0ebe874 12004 macro_build (&offset_expr, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12005 offset_reloc[1], offset_reloc[2], breg);
12006 else if (small_offset_p (0, align, offbits))
12007 {
12008 if (offbits == 0)
c0ebe874 12009 macro_build (NULL, s, fmt, op[0], breg);
f2ae14a1 12010 else
c0ebe874 12011 macro_build (NULL, s, fmt, op[0],
c8276761 12012 (int) offset_expr.X_add_number, breg);
f2ae14a1
RS
12013 }
12014 else
12015 {
12016 if (tempreg == AT)
12017 used_at = 1;
12018 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j",
12019 tempreg, breg, -1, offset_reloc[0],
12020 offset_reloc[1], offset_reloc[2]);
12021 if (offbits == 0)
c0ebe874 12022 macro_build (NULL, s, fmt, op[0], tempreg);
f2ae14a1 12023 else
c0ebe874 12024 macro_build (NULL, s, fmt, op[0], 0, tempreg);
f2ae14a1
RS
12025 }
12026 break;
12027 }
12028
12029 if (tempreg == AT)
12030 used_at = 1;
12031
252b5132
RH
12032 if (offset_expr.X_op != O_constant
12033 && offset_expr.X_op != O_symbol)
12034 {
1661c76c 12035 as_bad (_("expression too complex"));
252b5132
RH
12036 offset_expr.X_op = O_constant;
12037 }
12038
2051e8c4
MR
12039 if (HAVE_32BIT_ADDRESSES
12040 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12041 {
12042 char value [32];
12043
12044 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12045 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12046 }
2051e8c4 12047
252b5132
RH
12048 /* A constant expression in PIC code can be handled just as it
12049 is in non PIC code. */
aed1a261
RS
12050 if (offset_expr.X_op == O_constant)
12051 {
f2ae14a1
RS
12052 expr1.X_add_number = offset_high_part (offset_expr.X_add_number,
12053 offbits == 0 ? 16 : offbits);
12054 offset_expr.X_add_number -= expr1.X_add_number;
df58fc94 12055
f2ae14a1
RS
12056 load_register (tempreg, &expr1, HAVE_64BIT_ADDRESSES);
12057 if (breg != 0)
12058 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12059 tempreg, tempreg, breg);
7f3c4072 12060 if (offbits == 0)
dd6a37e7 12061 {
f2ae14a1 12062 if (offset_expr.X_add_number != 0)
dd6a37e7 12063 macro_build (&offset_expr, ADDRESS_ADDI_INSN,
f2ae14a1 12064 "t,r,j", tempreg, tempreg, BFD_RELOC_LO16);
c0ebe874 12065 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12066 }
7f3c4072 12067 else if (offbits == 16)
c0ebe874 12068 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
df58fc94 12069 else
c0ebe874 12070 macro_build (NULL, s, fmt, op[0],
c8276761 12071 (int) offset_expr.X_add_number, tempreg);
df58fc94 12072 }
7f3c4072 12073 else if (offbits != 16)
df58fc94 12074 {
7f3c4072 12075 /* The offset field is too narrow to be used for a low-part
2b0f3761 12076 relocation, so load the whole address into the auxiliary
f2ae14a1
RS
12077 register. */
12078 load_address (tempreg, &offset_expr, &used_at);
12079 if (breg != 0)
12080 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12081 tempreg, tempreg, breg);
7f3c4072 12082 if (offbits == 0)
c0ebe874 12083 macro_build (NULL, s, fmt, op[0], tempreg);
dd6a37e7 12084 else
c0ebe874 12085 macro_build (NULL, s, fmt, op[0], 0, tempreg);
aed1a261
RS
12086 }
12087 else if (mips_pic == NO_PIC)
252b5132
RH
12088 {
12089 /* If this is a reference to a GP relative symbol, and there
12090 is no base register, we want
c0ebe874 12091 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12092 Otherwise, if there is no base register, we want
12093 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
c0ebe874 12094 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
252b5132
RH
12095 If we have a constant, we need two instructions anyhow,
12096 so we always use the latter form.
12097
12098 If we have a base register, and this is a reference to a
12099 GP relative symbol, we want
12100 addu $tempreg,$breg,$gp
c0ebe874 12101 <op> op[0],<sym>($tempreg) (BFD_RELOC_GPREL16)
252b5132
RH
12102 Otherwise we want
12103 lui $tempreg,<sym> (BFD_RELOC_HI16_S)
12104 addu $tempreg,$tempreg,$breg
c0ebe874 12105 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245 12106 With a constant we always use the latter case.
76b3015f 12107
d6bc6245
TS
12108 With 64bit address space and no base register and $at usable,
12109 we want
12110 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12111 lui $at,<sym> (BFD_RELOC_HI16_S)
12112 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12113 dsll32 $tempreg,0
12114 daddu $tempreg,$at
c0ebe874 12115 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12116 If we have a base register, we want
12117 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12118 lui $at,<sym> (BFD_RELOC_HI16_S)
12119 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12120 daddu $at,$breg
12121 dsll32 $tempreg,0
12122 daddu $tempreg,$at
c0ebe874 12123 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12124
12125 Without $at we can't generate the optimal path for superscalar
12126 processors here since this would require two temporary registers.
12127 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12128 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12129 dsll $tempreg,16
12130 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12131 dsll $tempreg,16
c0ebe874 12132 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
d6bc6245
TS
12133 If we have a base register, we want
12134 lui $tempreg,<sym> (BFD_RELOC_MIPS_HIGHEST)
12135 daddiu $tempreg,<sym> (BFD_RELOC_MIPS_HIGHER)
12136 dsll $tempreg,16
12137 daddiu $tempreg,<sym> (BFD_RELOC_HI16_S)
12138 dsll $tempreg,16
12139 daddu $tempreg,$tempreg,$breg
c0ebe874 12140 <op> op[0],<sym>($tempreg) (BFD_RELOC_LO16)
6373ee54 12141
6caf9ef4 12142 For GP relative symbols in 64bit address space we can use
aed1a261
RS
12143 the same sequence as in 32bit address space. */
12144 if (HAVE_64BIT_SYMBOLS)
d6bc6245 12145 {
aed1a261 12146 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4
TS
12147 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
12148 {
12149 relax_start (offset_expr.X_add_symbol);
12150 if (breg == 0)
12151 {
c0ebe874 12152 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12153 BFD_RELOC_GPREL16, mips_gp_register);
12154 }
12155 else
12156 {
12157 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
12158 tempreg, breg, mips_gp_register);
c0ebe874 12159 macro_build (&offset_expr, s, fmt, op[0],
6caf9ef4
TS
12160 BFD_RELOC_GPREL16, tempreg);
12161 }
12162 relax_switch ();
12163 }
d6bc6245 12164
741fe287 12165 if (used_at == 0 && mips_opts.at)
d6bc6245 12166 {
df58fc94 12167 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb 12168 BFD_RELOC_MIPS_HIGHEST);
df58fc94 12169 macro_build (&offset_expr, "lui", LUI_FMT, AT,
67c0d1eb
RS
12170 BFD_RELOC_HI16_S);
12171 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12172 tempreg, BFD_RELOC_MIPS_HIGHER);
d6bc6245 12173 if (breg != 0)
67c0d1eb 12174 macro_build (NULL, "daddu", "d,v,t", AT, AT, breg);
df58fc94 12175 macro_build (NULL, "dsll32", SHFT_FMT, tempreg, tempreg, 0);
67c0d1eb 12176 macro_build (NULL, "daddu", "d,v,t", tempreg, tempreg, AT);
c0ebe874 12177 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_LO16,
67c0d1eb 12178 tempreg);
d6bc6245
TS
12179 used_at = 1;
12180 }
12181 else
12182 {
df58fc94 12183 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
67c0d1eb
RS
12184 BFD_RELOC_MIPS_HIGHEST);
12185 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12186 tempreg, BFD_RELOC_MIPS_HIGHER);
df58fc94 12187 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
67c0d1eb
RS
12188 macro_build (&offset_expr, "daddiu", "t,r,j", tempreg,
12189 tempreg, BFD_RELOC_HI16_S);
df58fc94 12190 macro_build (NULL, "dsll", SHFT_FMT, tempreg, tempreg, 16);
d6bc6245 12191 if (breg != 0)
67c0d1eb 12192 macro_build (NULL, "daddu", "d,v,t",
17a2f251 12193 tempreg, tempreg, breg);
c0ebe874 12194 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12195 BFD_RELOC_LO16, tempreg);
d6bc6245 12196 }
6caf9ef4
TS
12197
12198 if (mips_relax.sequence)
12199 relax_end ();
8fc2e39e 12200 break;
d6bc6245 12201 }
256ab948 12202
252b5132
RH
12203 if (breg == 0)
12204 {
67c0d1eb 12205 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12206 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12207 {
4d7206a2 12208 relax_start (offset_expr.X_add_symbol);
c0ebe874 12209 macro_build (&offset_expr, s, fmt, op[0], BFD_RELOC_GPREL16,
67c0d1eb 12210 mips_gp_register);
4d7206a2 12211 relax_switch ();
252b5132 12212 }
67c0d1eb 12213 macro_build_lui (&offset_expr, tempreg);
c0ebe874 12214 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12215 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12216 if (mips_relax.sequence)
12217 relax_end ();
252b5132
RH
12218 }
12219 else
12220 {
67c0d1eb 12221 if ((valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12222 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12223 {
4d7206a2 12224 relax_start (offset_expr.X_add_symbol);
67c0d1eb 12225 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12226 tempreg, breg, mips_gp_register);
c0ebe874 12227 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12228 BFD_RELOC_GPREL16, tempreg);
4d7206a2 12229 relax_switch ();
252b5132 12230 }
67c0d1eb
RS
12231 macro_build_lui (&offset_expr, tempreg);
12232 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12233 tempreg, tempreg, breg);
c0ebe874 12234 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12235 BFD_RELOC_LO16, tempreg);
4d7206a2
RS
12236 if (mips_relax.sequence)
12237 relax_end ();
252b5132
RH
12238 }
12239 }
0a44bf69 12240 else if (!mips_big_got)
252b5132 12241 {
ed6fb7bd 12242 int lw_reloc_type = (int) BFD_RELOC_MIPS_GOT16;
f9419b05 12243
252b5132
RH
12244 /* If this is a reference to an external symbol, we want
12245 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12246 nop
c0ebe874 12247 <op> op[0],0($tempreg)
252b5132
RH
12248 Otherwise we want
12249 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12250 nop
12251 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12252 <op> op[0],0($tempreg)
f5040a92
AO
12253
12254 For NewABI, we want
12255 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12256 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST)
f5040a92 12257
252b5132
RH
12258 If there is a base register, we add it to $tempreg before
12259 the <op>. If there is a constant, we stick it in the
12260 <op> instruction. We don't handle constants larger than
12261 16 bits, because we have no way to load the upper 16 bits
12262 (actually, we could handle them for the subset of cases
12263 in which we are not using $at). */
9c2799c2 12264 gas_assert (offset_expr.X_op == O_symbol);
f5040a92
AO
12265 if (HAVE_NEWABI)
12266 {
67c0d1eb
RS
12267 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12268 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12269 if (breg != 0)
67c0d1eb 12270 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12271 tempreg, tempreg, breg);
c0ebe874 12272 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12273 BFD_RELOC_MIPS_GOT_OFST, tempreg);
f5040a92
AO
12274 break;
12275 }
252b5132
RH
12276 expr1.X_add_number = offset_expr.X_add_number;
12277 offset_expr.X_add_number = 0;
12278 if (expr1.X_add_number < -0x8000
12279 || expr1.X_add_number >= 0x8000)
12280 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb
RS
12281 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12282 lw_reloc_type, mips_gp_register);
269137b2 12283 load_delay_nop ();
4d7206a2
RS
12284 relax_start (offset_expr.X_add_symbol);
12285 relax_switch ();
67c0d1eb
RS
12286 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12287 tempreg, BFD_RELOC_LO16);
4d7206a2 12288 relax_end ();
252b5132 12289 if (breg != 0)
67c0d1eb 12290 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12291 tempreg, tempreg, breg);
c0ebe874 12292 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12293 }
0a44bf69 12294 else if (mips_big_got && !HAVE_NEWABI)
252b5132 12295 {
67c0d1eb 12296 int gpdelay;
252b5132
RH
12297
12298 /* If this is a reference to an external symbol, we want
12299 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12300 addu $tempreg,$tempreg,$gp
12301 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12302 <op> op[0],0($tempreg)
252b5132
RH
12303 Otherwise we want
12304 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12305 nop
12306 addiu $tempreg,$tempreg,<sym> (BFD_RELOC_LO16)
c0ebe874 12307 <op> op[0],0($tempreg)
252b5132
RH
12308 If there is a base register, we add it to $tempreg before
12309 the <op>. If there is a constant, we stick it in the
12310 <op> instruction. We don't handle constants larger than
12311 16 bits, because we have no way to load the upper 16 bits
12312 (actually, we could handle them for the subset of cases
f5040a92 12313 in which we are not using $at). */
9c2799c2 12314 gas_assert (offset_expr.X_op == O_symbol);
252b5132
RH
12315 expr1.X_add_number = offset_expr.X_add_number;
12316 offset_expr.X_add_number = 0;
12317 if (expr1.X_add_number < -0x8000
12318 || expr1.X_add_number >= 0x8000)
12319 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12320 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12321 relax_start (offset_expr.X_add_symbol);
df58fc94 12322 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12323 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12324 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12325 mips_gp_register);
12326 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12327 BFD_RELOC_MIPS_GOT_LO16, tempreg);
4d7206a2 12328 relax_switch ();
67c0d1eb
RS
12329 if (gpdelay)
12330 macro_build (NULL, "nop", "");
12331 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12332 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12333 load_delay_nop ();
67c0d1eb
RS
12334 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", tempreg,
12335 tempreg, BFD_RELOC_LO16);
4d7206a2
RS
12336 relax_end ();
12337
252b5132 12338 if (breg != 0)
67c0d1eb 12339 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12340 tempreg, tempreg, breg);
c0ebe874 12341 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
252b5132 12342 }
0a44bf69 12343 else if (mips_big_got && HAVE_NEWABI)
f5040a92 12344 {
f5040a92
AO
12345 /* If this is a reference to an external symbol, we want
12346 lui $tempreg,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12347 add $tempreg,$tempreg,$gp
12348 lw $tempreg,<sym>($tempreg) (BFD_RELOC_MIPS_GOT_LO16)
c0ebe874 12349 <op> op[0],<ofst>($tempreg)
f5040a92
AO
12350 Otherwise, for local symbols, we want:
12351 lw $tempreg,<sym>($gp) (BFD_RELOC_MIPS_GOT_PAGE)
c0ebe874 12352 <op> op[0],<sym>($tempreg) (BFD_RELOC_MIPS_GOT_OFST) */
9c2799c2 12353 gas_assert (offset_expr.X_op == O_symbol);
4d7206a2 12354 expr1.X_add_number = offset_expr.X_add_number;
f5040a92
AO
12355 offset_expr.X_add_number = 0;
12356 if (expr1.X_add_number < -0x8000
12357 || expr1.X_add_number >= 0x8000)
12358 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
4d7206a2 12359 relax_start (offset_expr.X_add_symbol);
df58fc94 12360 macro_build (&offset_expr, "lui", LUI_FMT, tempreg,
17a2f251 12361 BFD_RELOC_MIPS_GOT_HI16);
67c0d1eb
RS
12362 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", tempreg, tempreg,
12363 mips_gp_register);
12364 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12365 BFD_RELOC_MIPS_GOT_LO16, tempreg);
f5040a92 12366 if (breg != 0)
67c0d1eb 12367 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12368 tempreg, tempreg, breg);
c0ebe874 12369 macro_build (&expr1, s, fmt, op[0], BFD_RELOC_LO16, tempreg);
684022ea 12370
4d7206a2 12371 relax_switch ();
f5040a92 12372 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12373 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", tempreg,
12374 BFD_RELOC_MIPS_GOT_PAGE, mips_gp_register);
f5040a92 12375 if (breg != 0)
67c0d1eb 12376 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12377 tempreg, tempreg, breg);
c0ebe874 12378 macro_build (&offset_expr, s, fmt, op[0],
17a2f251 12379 BFD_RELOC_MIPS_GOT_OFST, tempreg);
4d7206a2 12380 relax_end ();
f5040a92 12381 }
252b5132
RH
12382 else
12383 abort ();
12384
252b5132
RH
12385 break;
12386
833794fc
MR
12387 case M_JRADDIUSP:
12388 gas_assert (mips_opts.micromips);
12389 gas_assert (mips_opts.insn32);
12390 start_noreorder ();
12391 macro_build (NULL, "jr", "s", RA);
c0ebe874 12392 expr1.X_add_number = op[0] << 2;
833794fc
MR
12393 macro_build (&expr1, "addiu", "t,r,j", SP, SP, BFD_RELOC_LO16);
12394 end_noreorder ();
12395 break;
12396
12397 case M_JRC:
12398 gas_assert (mips_opts.micromips);
12399 gas_assert (mips_opts.insn32);
c0ebe874 12400 macro_build (NULL, "jr", "s", op[0]);
833794fc
MR
12401 if (mips_opts.noreorder)
12402 macro_build (NULL, "nop", "");
12403 break;
12404
252b5132
RH
12405 case M_LI:
12406 case M_LI_S:
c0ebe874 12407 load_register (op[0], &imm_expr, 0);
8fc2e39e 12408 break;
252b5132
RH
12409
12410 case M_DLI:
c0ebe874 12411 load_register (op[0], &imm_expr, 1);
8fc2e39e 12412 break;
252b5132
RH
12413
12414 case M_LI_SS:
12415 if (imm_expr.X_op == O_constant)
12416 {
8fc2e39e 12417 used_at = 1;
67c0d1eb 12418 load_register (AT, &imm_expr, 0);
c0ebe874 12419 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12420 break;
12421 }
12422 else
12423 {
b0e6f033
RS
12424 gas_assert (imm_expr.X_op == O_absent
12425 && offset_expr.X_op == O_symbol
90ecf173
MR
12426 && strcmp (segment_name (S_GET_SEGMENT
12427 (offset_expr.X_add_symbol)),
12428 ".lit4") == 0
12429 && offset_expr.X_add_number == 0);
c0ebe874 12430 macro_build (&offset_expr, "lwc1", "T,o(b)", op[0],
17a2f251 12431 BFD_RELOC_MIPS_LITERAL, mips_gp_register);
8fc2e39e 12432 break;
252b5132
RH
12433 }
12434
12435 case M_LI_D:
ca4e0257
RS
12436 /* Check if we have a constant in IMM_EXPR. If the GPRs are 64 bits
12437 wide, IMM_EXPR is the entire value. Otherwise IMM_EXPR is the high
12438 order 32 bits of the value and the low order 32 bits are either
12439 zero or in OFFSET_EXPR. */
b0e6f033 12440 if (imm_expr.X_op == O_constant)
252b5132 12441 {
bad1aba3 12442 if (GPR_SIZE == 64)
c0ebe874 12443 load_register (op[0], &imm_expr, 1);
252b5132
RH
12444 else
12445 {
12446 int hreg, lreg;
12447
12448 if (target_big_endian)
12449 {
c0ebe874
RS
12450 hreg = op[0];
12451 lreg = op[0] + 1;
252b5132
RH
12452 }
12453 else
12454 {
c0ebe874
RS
12455 hreg = op[0] + 1;
12456 lreg = op[0];
252b5132
RH
12457 }
12458
12459 if (hreg <= 31)
67c0d1eb 12460 load_register (hreg, &imm_expr, 0);
252b5132
RH
12461 if (lreg <= 31)
12462 {
12463 if (offset_expr.X_op == O_absent)
67c0d1eb 12464 move_register (lreg, 0);
252b5132
RH
12465 else
12466 {
9c2799c2 12467 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12468 load_register (lreg, &offset_expr, 0);
252b5132
RH
12469 }
12470 }
12471 }
8fc2e39e 12472 break;
252b5132 12473 }
b0e6f033 12474 gas_assert (imm_expr.X_op == O_absent);
252b5132
RH
12475
12476 /* We know that sym is in the .rdata section. First we get the
12477 upper 16 bits of the address. */
12478 if (mips_pic == NO_PIC)
12479 {
67c0d1eb 12480 macro_build_lui (&offset_expr, AT);
8fc2e39e 12481 used_at = 1;
252b5132 12482 }
0a44bf69 12483 else
252b5132 12484 {
67c0d1eb
RS
12485 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12486 BFD_RELOC_MIPS_GOT16, mips_gp_register);
8fc2e39e 12487 used_at = 1;
252b5132 12488 }
bdaaa2e1 12489
252b5132 12490 /* Now we load the register(s). */
bad1aba3 12491 if (GPR_SIZE == 64)
8fc2e39e
TS
12492 {
12493 used_at = 1;
c0ebe874
RS
12494 macro_build (&offset_expr, "ld", "t,o(b)", op[0],
12495 BFD_RELOC_LO16, AT);
8fc2e39e 12496 }
252b5132
RH
12497 else
12498 {
8fc2e39e 12499 used_at = 1;
c0ebe874
RS
12500 macro_build (&offset_expr, "lw", "t,o(b)", op[0],
12501 BFD_RELOC_LO16, AT);
12502 if (op[0] != RA)
252b5132
RH
12503 {
12504 /* FIXME: How in the world do we deal with the possible
12505 overflow here? */
12506 offset_expr.X_add_number += 4;
67c0d1eb 12507 macro_build (&offset_expr, "lw", "t,o(b)",
c0ebe874 12508 op[0] + 1, BFD_RELOC_LO16, AT);
252b5132
RH
12509 }
12510 }
252b5132
RH
12511 break;
12512
12513 case M_LI_DD:
ca4e0257
RS
12514 /* Check if we have a constant in IMM_EXPR. If the FPRs are 64 bits
12515 wide, IMM_EXPR is the entire value and the GPRs are known to be 64
12516 bits wide as well. Otherwise IMM_EXPR is the high order 32 bits of
12517 the value and the low order 32 bits are either zero or in
12518 OFFSET_EXPR. */
b0e6f033 12519 if (imm_expr.X_op == O_constant)
252b5132 12520 {
8fc2e39e 12521 used_at = 1;
bad1aba3 12522 load_register (AT, &imm_expr, FPR_SIZE == 64);
351cdf24
MF
12523 if (FPR_SIZE == 64 && GPR_SIZE == 64)
12524 macro_build (NULL, "dmtc1", "t,S", AT, op[0]);
252b5132
RH
12525 else
12526 {
351cdf24
MF
12527 if (ISA_HAS_MXHC1 (mips_opts.isa))
12528 macro_build (NULL, "mthc1", "t,G", AT, op[0]);
12529 else if (FPR_SIZE != 32)
12530 as_bad (_("Unable to generate `%s' compliant code "
12531 "without mthc1"),
12532 (FPR_SIZE == 64) ? "fp64" : "fpxx");
12533 else
12534 macro_build (NULL, "mtc1", "t,G", AT, op[0] + 1);
252b5132 12535 if (offset_expr.X_op == O_absent)
c0ebe874 12536 macro_build (NULL, "mtc1", "t,G", 0, op[0]);
252b5132
RH
12537 else
12538 {
9c2799c2 12539 gas_assert (offset_expr.X_op == O_constant);
67c0d1eb 12540 load_register (AT, &offset_expr, 0);
c0ebe874 12541 macro_build (NULL, "mtc1", "t,G", AT, op[0]);
252b5132
RH
12542 }
12543 }
12544 break;
12545 }
12546
b0e6f033
RS
12547 gas_assert (imm_expr.X_op == O_absent
12548 && offset_expr.X_op == O_symbol
90ecf173 12549 && offset_expr.X_add_number == 0);
252b5132
RH
12550 s = segment_name (S_GET_SEGMENT (offset_expr.X_add_symbol));
12551 if (strcmp (s, ".lit8") == 0)
134c0c8b
MR
12552 {
12553 op[2] = mips_gp_register;
f2ae14a1
RS
12554 offset_reloc[0] = BFD_RELOC_MIPS_LITERAL;
12555 offset_reloc[1] = BFD_RELOC_UNUSED;
12556 offset_reloc[2] = BFD_RELOC_UNUSED;
252b5132
RH
12557 }
12558 else
12559 {
9c2799c2 12560 gas_assert (strcmp (s, RDATA_SECTION_NAME) == 0);
8fc2e39e 12561 used_at = 1;
0a44bf69 12562 if (mips_pic != NO_PIC)
67c0d1eb
RS
12563 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12564 BFD_RELOC_MIPS_GOT16, mips_gp_register);
252b5132
RH
12565 else
12566 {
12567 /* FIXME: This won't work for a 64 bit address. */
67c0d1eb 12568 macro_build_lui (&offset_expr, AT);
252b5132 12569 }
bdaaa2e1 12570
c0ebe874 12571 op[2] = AT;
f2ae14a1
RS
12572 offset_reloc[0] = BFD_RELOC_LO16;
12573 offset_reloc[1] = BFD_RELOC_UNUSED;
12574 offset_reloc[2] = BFD_RELOC_UNUSED;
134c0c8b 12575 }
f2ae14a1
RS
12576 align = 8;
12577 /* Fall through */
c4a68bea 12578
252b5132
RH
12579 case M_L_DAB:
12580 /*
12581 * The MIPS assembler seems to check for X_add_number not
12582 * being double aligned and generating:
12583 * lui at,%hi(foo+1)
12584 * addu at,at,v1
12585 * addiu at,at,%lo(foo+1)
12586 * lwc1 f2,0(at)
12587 * lwc1 f3,4(at)
12588 * But, the resulting address is the same after relocation so why
12589 * generate the extra instruction?
12590 */
bdaaa2e1 12591 /* Itbl support may require additional care here. */
252b5132 12592 coproc = 1;
df58fc94 12593 fmt = "T,o(b)";
0aa27725 12594 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12595 {
12596 s = "ldc1";
df58fc94 12597 goto ld_st;
252b5132 12598 }
252b5132 12599 s = "lwc1";
252b5132
RH
12600 goto ldd_std;
12601
12602 case M_S_DAB:
df58fc94
RS
12603 gas_assert (!mips_opts.micromips);
12604 /* Itbl support may require additional care here. */
12605 coproc = 1;
12606 fmt = "T,o(b)";
0aa27725 12607 if (CPU_HAS_LDC1_SDC1 (mips_opts.arch))
252b5132
RH
12608 {
12609 s = "sdc1";
df58fc94 12610 goto ld_st;
252b5132 12611 }
252b5132 12612 s = "swc1";
252b5132
RH
12613 goto ldd_std;
12614
e407c74b
NC
12615 case M_LQ_AB:
12616 fmt = "t,o(b)";
12617 s = "lq";
12618 goto ld;
12619
12620 case M_SQ_AB:
12621 fmt = "t,o(b)";
12622 s = "sq";
12623 goto ld_st;
12624
252b5132 12625 case M_LD_AB:
df58fc94 12626 fmt = "t,o(b)";
bad1aba3 12627 if (GPR_SIZE == 64)
252b5132
RH
12628 {
12629 s = "ld";
12630 goto ld;
12631 }
252b5132 12632 s = "lw";
252b5132
RH
12633 goto ldd_std;
12634
12635 case M_SD_AB:
df58fc94 12636 fmt = "t,o(b)";
bad1aba3 12637 if (GPR_SIZE == 64)
252b5132
RH
12638 {
12639 s = "sd";
df58fc94 12640 goto ld_st;
252b5132 12641 }
252b5132 12642 s = "sw";
252b5132
RH
12643
12644 ldd_std:
f2ae14a1
RS
12645 /* Even on a big endian machine $fn comes before $fn+1. We have
12646 to adjust when loading from memory. We set coproc if we must
12647 load $fn+1 first. */
12648 /* Itbl support may require additional care here. */
12649 if (!target_big_endian)
12650 coproc = 0;
12651
c0ebe874 12652 breg = op[2];
f2ae14a1
RS
12653 if (small_offset_p (0, align, 16))
12654 {
12655 ep = &offset_expr;
12656 if (!small_offset_p (4, align, 16))
12657 {
12658 macro_build (&offset_expr, ADDRESS_ADDI_INSN, "t,r,j", AT, breg,
12659 -1, offset_reloc[0], offset_reloc[1],
12660 offset_reloc[2]);
12661 expr1.X_add_number = 0;
12662 ep = &expr1;
12663 breg = AT;
12664 used_at = 1;
12665 offset_reloc[0] = BFD_RELOC_LO16;
12666 offset_reloc[1] = BFD_RELOC_UNUSED;
12667 offset_reloc[2] = BFD_RELOC_UNUSED;
12668 }
c0ebe874 12669 if (strcmp (s, "lw") == 0 && op[0] == breg)
f2ae14a1
RS
12670 {
12671 ep->X_add_number += 4;
c0ebe874 12672 macro_build (ep, s, fmt, op[0] + 1, -1, offset_reloc[0],
f2ae14a1
RS
12673 offset_reloc[1], offset_reloc[2], breg);
12674 ep->X_add_number -= 4;
c0ebe874 12675 macro_build (ep, s, fmt, op[0], -1, offset_reloc[0],
f2ae14a1
RS
12676 offset_reloc[1], offset_reloc[2], breg);
12677 }
12678 else
12679 {
c0ebe874 12680 macro_build (ep, s, fmt, coproc ? op[0] + 1 : op[0], -1,
f2ae14a1
RS
12681 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12682 breg);
12683 ep->X_add_number += 4;
c0ebe874 12684 macro_build (ep, s, fmt, coproc ? op[0] : op[0] + 1, -1,
f2ae14a1
RS
12685 offset_reloc[0], offset_reloc[1], offset_reloc[2],
12686 breg);
12687 }
12688 break;
12689 }
12690
252b5132
RH
12691 if (offset_expr.X_op != O_symbol
12692 && offset_expr.X_op != O_constant)
12693 {
1661c76c 12694 as_bad (_("expression too complex"));
252b5132
RH
12695 offset_expr.X_op = O_constant;
12696 }
12697
2051e8c4
MR
12698 if (HAVE_32BIT_ADDRESSES
12699 && !IS_SEXT_32BIT_NUM (offset_expr.X_add_number))
55e08f71
NC
12700 {
12701 char value [32];
12702
12703 sprintf_vma (value, offset_expr.X_add_number);
1661c76c 12704 as_bad (_("number (0x%s) larger than 32 bits"), value);
55e08f71 12705 }
2051e8c4 12706
90ecf173 12707 if (mips_pic == NO_PIC || offset_expr.X_op == O_constant)
252b5132
RH
12708 {
12709 /* If this is a reference to a GP relative symbol, we want
c0ebe874
RS
12710 <op> op[0],<sym>($gp) (BFD_RELOC_GPREL16)
12711 <op> op[0]+1,<sym>+4($gp) (BFD_RELOC_GPREL16)
252b5132
RH
12712 If we have a base register, we use this
12713 addu $at,$breg,$gp
c0ebe874
RS
12714 <op> op[0],<sym>($at) (BFD_RELOC_GPREL16)
12715 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_GPREL16)
252b5132
RH
12716 If this is not a GP relative symbol, we want
12717 lui $at,<sym> (BFD_RELOC_HI16_S)
c0ebe874
RS
12718 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12719 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12720 If there is a base register, we add it to $at after the
12721 lui instruction. If there is a constant, we always use
12722 the last case. */
39a59cf8
MR
12723 if (offset_expr.X_op == O_symbol
12724 && (valueT) offset_expr.X_add_number <= MAX_GPREL_OFFSET
6caf9ef4 12725 && !nopic_need_relax (offset_expr.X_add_symbol, 1))
252b5132 12726 {
4d7206a2 12727 relax_start (offset_expr.X_add_symbol);
252b5132
RH
12728 if (breg == 0)
12729 {
c9914766 12730 tempreg = mips_gp_register;
252b5132
RH
12731 }
12732 else
12733 {
67c0d1eb 12734 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12735 AT, breg, mips_gp_register);
252b5132 12736 tempreg = AT;
252b5132
RH
12737 used_at = 1;
12738 }
12739
beae10d5 12740 /* Itbl support may require additional care here. */
c0ebe874 12741 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12742 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12743 offset_expr.X_add_number += 4;
12744
12745 /* Set mips_optimize to 2 to avoid inserting an
12746 undesired nop. */
12747 hold_mips_optimize = mips_optimize;
12748 mips_optimize = 2;
beae10d5 12749 /* Itbl support may require additional care here. */
c0ebe874 12750 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12751 BFD_RELOC_GPREL16, tempreg);
252b5132
RH
12752 mips_optimize = hold_mips_optimize;
12753
4d7206a2 12754 relax_switch ();
252b5132 12755
0970e49e 12756 offset_expr.X_add_number -= 4;
252b5132 12757 }
8fc2e39e 12758 used_at = 1;
f2ae14a1
RS
12759 if (offset_high_part (offset_expr.X_add_number, 16)
12760 != offset_high_part (offset_expr.X_add_number + 4, 16))
12761 {
12762 load_address (AT, &offset_expr, &used_at);
12763 offset_expr.X_op = O_constant;
12764 offset_expr.X_add_number = 0;
12765 }
12766 else
12767 macro_build_lui (&offset_expr, AT);
252b5132 12768 if (breg != 0)
67c0d1eb 12769 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12770 /* Itbl support may require additional care here. */
c0ebe874 12771 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12772 BFD_RELOC_LO16, AT);
252b5132
RH
12773 /* FIXME: How do we handle overflow here? */
12774 offset_expr.X_add_number += 4;
beae10d5 12775 /* Itbl support may require additional care here. */
c0ebe874 12776 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12777 BFD_RELOC_LO16, AT);
4d7206a2
RS
12778 if (mips_relax.sequence)
12779 relax_end ();
bdaaa2e1 12780 }
0a44bf69 12781 else if (!mips_big_got)
252b5132 12782 {
252b5132
RH
12783 /* If this is a reference to an external symbol, we want
12784 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12785 nop
c0ebe874
RS
12786 <op> op[0],0($at)
12787 <op> op[0]+1,4($at)
252b5132
RH
12788 Otherwise we want
12789 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12790 nop
c0ebe874
RS
12791 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12792 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12793 If there is a base register we add it to $at before the
12794 lwc1 instructions. If there is a constant we include it
12795 in the lwc1 instructions. */
12796 used_at = 1;
12797 expr1.X_add_number = offset_expr.X_add_number;
252b5132
RH
12798 if (expr1.X_add_number < -0x8000
12799 || expr1.X_add_number >= 0x8000 - 4)
12800 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12801 load_got_offset (AT, &offset_expr);
269137b2 12802 load_delay_nop ();
252b5132 12803 if (breg != 0)
67c0d1eb 12804 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
252b5132
RH
12805
12806 /* Set mips_optimize to 2 to avoid inserting an undesired
12807 nop. */
12808 hold_mips_optimize = mips_optimize;
12809 mips_optimize = 2;
4d7206a2 12810
beae10d5 12811 /* Itbl support may require additional care here. */
4d7206a2 12812 relax_start (offset_expr.X_add_symbol);
c0ebe874 12813 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12814 BFD_RELOC_LO16, AT);
4d7206a2 12815 expr1.X_add_number += 4;
c0ebe874 12816 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12817 BFD_RELOC_LO16, AT);
4d7206a2 12818 relax_switch ();
c0ebe874 12819 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12820 BFD_RELOC_LO16, AT);
4d7206a2 12821 offset_expr.X_add_number += 4;
c0ebe874 12822 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12823 BFD_RELOC_LO16, AT);
4d7206a2 12824 relax_end ();
252b5132 12825
4d7206a2 12826 mips_optimize = hold_mips_optimize;
252b5132 12827 }
0a44bf69 12828 else if (mips_big_got)
252b5132 12829 {
67c0d1eb 12830 int gpdelay;
252b5132
RH
12831
12832 /* If this is a reference to an external symbol, we want
12833 lui $at,<sym> (BFD_RELOC_MIPS_GOT_HI16)
12834 addu $at,$at,$gp
12835 lw $at,<sym>($at) (BFD_RELOC_MIPS_GOT_LO16)
12836 nop
c0ebe874
RS
12837 <op> op[0],0($at)
12838 <op> op[0]+1,4($at)
252b5132
RH
12839 Otherwise we want
12840 lw $at,<sym>($gp) (BFD_RELOC_MIPS_GOT16)
12841 nop
c0ebe874
RS
12842 <op> op[0],<sym>($at) (BFD_RELOC_LO16)
12843 <op> op[0]+1,<sym>+4($at) (BFD_RELOC_LO16)
252b5132
RH
12844 If there is a base register we add it to $at before the
12845 lwc1 instructions. If there is a constant we include it
12846 in the lwc1 instructions. */
12847 used_at = 1;
12848 expr1.X_add_number = offset_expr.X_add_number;
12849 offset_expr.X_add_number = 0;
12850 if (expr1.X_add_number < -0x8000
12851 || expr1.X_add_number >= 0x8000 - 4)
12852 as_bad (_("PIC code offset overflow (max 16 signed bits)"));
67c0d1eb 12853 gpdelay = reg_needs_delay (mips_gp_register);
4d7206a2 12854 relax_start (offset_expr.X_add_symbol);
df58fc94 12855 macro_build (&offset_expr, "lui", LUI_FMT,
67c0d1eb
RS
12856 AT, BFD_RELOC_MIPS_GOT_HI16);
12857 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
17a2f251 12858 AT, AT, mips_gp_register);
67c0d1eb 12859 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)",
17a2f251 12860 AT, BFD_RELOC_MIPS_GOT_LO16, AT);
269137b2 12861 load_delay_nop ();
252b5132 12862 if (breg != 0)
67c0d1eb 12863 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12864 /* Itbl support may require additional care here. */
c0ebe874 12865 macro_build (&expr1, s, fmt, coproc ? op[0] + 1 : op[0],
17a2f251 12866 BFD_RELOC_LO16, AT);
252b5132
RH
12867 expr1.X_add_number += 4;
12868
12869 /* Set mips_optimize to 2 to avoid inserting an undesired
12870 nop. */
12871 hold_mips_optimize = mips_optimize;
12872 mips_optimize = 2;
beae10d5 12873 /* Itbl support may require additional care here. */
c0ebe874 12874 macro_build (&expr1, s, fmt, coproc ? op[0] : op[0] + 1,
17a2f251 12875 BFD_RELOC_LO16, AT);
252b5132
RH
12876 mips_optimize = hold_mips_optimize;
12877 expr1.X_add_number -= 4;
12878
4d7206a2
RS
12879 relax_switch ();
12880 offset_expr.X_add_number = expr1.X_add_number;
67c0d1eb
RS
12881 if (gpdelay)
12882 macro_build (NULL, "nop", "");
12883 macro_build (&offset_expr, ADDRESS_LOAD_INSN, "t,o(b)", AT,
12884 BFD_RELOC_MIPS_GOT16, mips_gp_register);
269137b2 12885 load_delay_nop ();
252b5132 12886 if (breg != 0)
67c0d1eb 12887 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", AT, breg, AT);
beae10d5 12888 /* Itbl support may require additional care here. */
c0ebe874 12889 macro_build (&offset_expr, s, fmt, coproc ? op[0] + 1 : op[0],
67c0d1eb 12890 BFD_RELOC_LO16, AT);
4d7206a2 12891 offset_expr.X_add_number += 4;
252b5132
RH
12892
12893 /* Set mips_optimize to 2 to avoid inserting an undesired
12894 nop. */
12895 hold_mips_optimize = mips_optimize;
12896 mips_optimize = 2;
beae10d5 12897 /* Itbl support may require additional care here. */
c0ebe874 12898 macro_build (&offset_expr, s, fmt, coproc ? op[0] : op[0] + 1,
67c0d1eb 12899 BFD_RELOC_LO16, AT);
252b5132 12900 mips_optimize = hold_mips_optimize;
4d7206a2 12901 relax_end ();
252b5132 12902 }
252b5132
RH
12903 else
12904 abort ();
12905
252b5132 12906 break;
3739860c 12907
dd6a37e7 12908 case M_SAA_AB:
dd6a37e7 12909 s = "saa";
0db377d0 12910 goto saa_saad;
dd6a37e7 12911 case M_SAAD_AB:
dd6a37e7 12912 s = "saad";
0db377d0
MR
12913 saa_saad:
12914 gas_assert (!mips_opts.micromips);
7f3c4072 12915 offbits = 0;
dd6a37e7
AP
12916 fmt = "t,(b)";
12917 goto ld_st;
12918
252b5132
RH
12919 /* New code added to support COPZ instructions.
12920 This code builds table entries out of the macros in mip_opcodes.
12921 R4000 uses interlocks to handle coproc delays.
12922 Other chips (like the R3000) require nops to be inserted for delays.
12923
f72c8c98 12924 FIXME: Currently, we require that the user handle delays.
252b5132
RH
12925 In order to fill delay slots for non-interlocked chips,
12926 we must have a way to specify delays based on the coprocessor.
12927 Eg. 4 cycles if load coproc reg from memory, 1 if in cache, etc.
12928 What are the side-effects of the cop instruction?
12929 What cache support might we have and what are its effects?
12930 Both coprocessor & memory require delays. how long???
bdaaa2e1 12931 What registers are read/set/modified?
252b5132
RH
12932
12933 If an itbl is provided to interpret cop instructions,
bdaaa2e1 12934 this knowledge can be encoded in the itbl spec. */
252b5132
RH
12935
12936 case M_COP0:
12937 s = "c0";
12938 goto copz;
12939 case M_COP1:
12940 s = "c1";
12941 goto copz;
12942 case M_COP2:
12943 s = "c2";
12944 goto copz;
12945 case M_COP3:
12946 s = "c3";
12947 copz:
df58fc94 12948 gas_assert (!mips_opts.micromips);
252b5132
RH
12949 /* For now we just do C (same as Cz). The parameter will be
12950 stored in insn_opcode by mips_ip. */
c8276761 12951 macro_build (NULL, s, "C", (int) ip->insn_opcode);
8fc2e39e 12952 break;
252b5132 12953
ea1fb5dc 12954 case M_MOVE:
c0ebe874 12955 move_register (op[0], op[1]);
8fc2e39e 12956 break;
ea1fb5dc 12957
833794fc
MR
12958 case M_MOVEP:
12959 gas_assert (mips_opts.micromips);
12960 gas_assert (mips_opts.insn32);
c0ebe874
RS
12961 move_register (micromips_to_32_reg_h_map1[op[0]],
12962 micromips_to_32_reg_m_map[op[1]]);
12963 move_register (micromips_to_32_reg_h_map2[op[0]],
12964 micromips_to_32_reg_n_map[op[2]]);
833794fc
MR
12965 break;
12966
252b5132
RH
12967 case M_DMUL:
12968 dbl = 1;
1a0670f3 12969 /* Fall through. */
252b5132 12970 case M_MUL:
e407c74b 12971 if (mips_opts.arch == CPU_R5900)
c0ebe874
RS
12972 macro_build (NULL, dbl ? "dmultu" : "multu", "d,s,t", op[0], op[1],
12973 op[2]);
e407c74b
NC
12974 else
12975 {
c0ebe874
RS
12976 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t", op[1], op[2]);
12977 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
e407c74b 12978 }
8fc2e39e 12979 break;
252b5132
RH
12980
12981 case M_DMUL_I:
12982 dbl = 1;
1a0670f3 12983 /* Fall through. */
252b5132
RH
12984 case M_MUL_I:
12985 /* The MIPS assembler some times generates shifts and adds. I'm
12986 not trying to be that fancy. GCC should do this for us
12987 anyway. */
8fc2e39e 12988 used_at = 1;
67c0d1eb 12989 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
12990 macro_build (NULL, dbl ? "dmult" : "mult", "s,t", op[1], AT);
12991 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
12992 break;
12993
12994 case M_DMULO_I:
12995 dbl = 1;
1a0670f3 12996 /* Fall through. */
252b5132
RH
12997 case M_MULO_I:
12998 imm = 1;
12999 goto do_mulo;
13000
13001 case M_DMULO:
13002 dbl = 1;
1a0670f3 13003 /* Fall through. */
252b5132
RH
13004 case M_MULO:
13005 do_mulo:
7d10b47d 13006 start_noreorder ();
8fc2e39e 13007 used_at = 1;
252b5132 13008 if (imm)
67c0d1eb 13009 load_register (AT, &imm_expr, dbl);
c0ebe874
RS
13010 macro_build (NULL, dbl ? "dmult" : "mult", "s,t",
13011 op[1], imm ? AT : op[2]);
13012 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
13013 macro_build (NULL, dbl ? "dsra32" : "sra", SHFT_FMT, op[0], op[0], 31);
df58fc94 13014 macro_build (NULL, "mfhi", MFHL_FMT, AT);
252b5132 13015 if (mips_trap)
c0ebe874 13016 macro_build (NULL, "tne", TRAP_FMT, op[0], AT, 6);
252b5132
RH
13017 else
13018 {
df58fc94
RS
13019 if (mips_opts.micromips)
13020 micromips_label_expr (&label_expr);
13021 else
13022 label_expr.X_add_number = 8;
c0ebe874 13023 macro_build (&label_expr, "beq", "s,t,p", op[0], AT);
a605d2b3 13024 macro_build (NULL, "nop", "");
df58fc94
RS
13025 macro_build (NULL, "break", BRK_FMT, 6);
13026 if (mips_opts.micromips)
13027 micromips_add_label ();
252b5132 13028 }
7d10b47d 13029 end_noreorder ();
c0ebe874 13030 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132
RH
13031 break;
13032
13033 case M_DMULOU_I:
13034 dbl = 1;
1a0670f3 13035 /* Fall through. */
252b5132
RH
13036 case M_MULOU_I:
13037 imm = 1;
13038 goto do_mulou;
13039
13040 case M_DMULOU:
13041 dbl = 1;
1a0670f3 13042 /* Fall through. */
252b5132
RH
13043 case M_MULOU:
13044 do_mulou:
7d10b47d 13045 start_noreorder ();
8fc2e39e 13046 used_at = 1;
252b5132 13047 if (imm)
67c0d1eb
RS
13048 load_register (AT, &imm_expr, dbl);
13049 macro_build (NULL, dbl ? "dmultu" : "multu", "s,t",
c0ebe874 13050 op[1], imm ? AT : op[2]);
df58fc94 13051 macro_build (NULL, "mfhi", MFHL_FMT, AT);
c0ebe874 13052 macro_build (NULL, "mflo", MFHL_FMT, op[0]);
252b5132 13053 if (mips_trap)
df58fc94 13054 macro_build (NULL, "tne", TRAP_FMT, AT, ZERO, 6);
252b5132
RH
13055 else
13056 {
df58fc94
RS
13057 if (mips_opts.micromips)
13058 micromips_label_expr (&label_expr);
13059 else
13060 label_expr.X_add_number = 8;
13061 macro_build (&label_expr, "beq", "s,t,p", AT, ZERO);
a605d2b3 13062 macro_build (NULL, "nop", "");
df58fc94
RS
13063 macro_build (NULL, "break", BRK_FMT, 6);
13064 if (mips_opts.micromips)
13065 micromips_add_label ();
252b5132 13066 }
7d10b47d 13067 end_noreorder ();
252b5132
RH
13068 break;
13069
771c7ce4 13070 case M_DROL:
fef14a42 13071 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13072 {
c0ebe874 13073 if (op[0] == op[1])
82dd0097
CD
13074 {
13075 tempreg = AT;
13076 used_at = 1;
13077 }
13078 else
c0ebe874
RS
13079 tempreg = op[0];
13080 macro_build (NULL, "dnegu", "d,w", tempreg, op[2]);
13081 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13082 break;
82dd0097 13083 }
8fc2e39e 13084 used_at = 1;
c0ebe874
RS
13085 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13086 macro_build (NULL, "dsrlv", "d,t,s", AT, op[1], AT);
13087 macro_build (NULL, "dsllv", "d,t,s", op[0], op[1], op[2]);
13088 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13089 break;
13090
252b5132 13091 case M_ROL:
fef14a42 13092 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13093 {
c0ebe874 13094 if (op[0] == op[1])
82dd0097
CD
13095 {
13096 tempreg = AT;
13097 used_at = 1;
13098 }
13099 else
c0ebe874
RS
13100 tempreg = op[0];
13101 macro_build (NULL, "negu", "d,w", tempreg, op[2]);
13102 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], tempreg);
8fc2e39e 13103 break;
82dd0097 13104 }
8fc2e39e 13105 used_at = 1;
c0ebe874
RS
13106 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13107 macro_build (NULL, "srlv", "d,t,s", AT, op[1], AT);
13108 macro_build (NULL, "sllv", "d,t,s", op[0], op[1], op[2]);
13109 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13110 break;
13111
771c7ce4
TS
13112 case M_DROL_I:
13113 {
13114 unsigned int rot;
e0471c16
TS
13115 const char *l;
13116 const char *rr;
771c7ce4 13117
771c7ce4 13118 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13119 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
60b63b72
RS
13120 {
13121 rot = (64 - rot) & 0x3f;
13122 if (rot >= 32)
c0ebe874 13123 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
60b63b72 13124 else
c0ebe874 13125 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13126 break;
60b63b72 13127 }
483fc7cd 13128 if (rot == 0)
483fc7cd 13129 {
c0ebe874 13130 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13131 break;
483fc7cd 13132 }
82dd0097 13133 l = (rot < 0x20) ? "dsll" : "dsll32";
91d6fa6a 13134 rr = ((0x40 - rot) < 0x20) ? "dsrl" : "dsrl32";
82dd0097 13135 rot &= 0x1f;
8fc2e39e 13136 used_at = 1;
c0ebe874
RS
13137 macro_build (NULL, l, SHFT_FMT, AT, op[1], rot);
13138 macro_build (NULL, rr, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13139 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13140 }
13141 break;
13142
252b5132 13143 case M_ROL_I:
771c7ce4
TS
13144 {
13145 unsigned int rot;
13146
771c7ce4 13147 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13148 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
60b63b72 13149 {
c0ebe874
RS
13150 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1],
13151 (32 - rot) & 0x1f);
8fc2e39e 13152 break;
60b63b72 13153 }
483fc7cd 13154 if (rot == 0)
483fc7cd 13155 {
c0ebe874 13156 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13157 break;
483fc7cd 13158 }
8fc2e39e 13159 used_at = 1;
c0ebe874
RS
13160 macro_build (NULL, "sll", SHFT_FMT, AT, op[1], rot);
13161 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13162 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13163 }
13164 break;
13165
13166 case M_DROR:
fef14a42 13167 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097 13168 {
c0ebe874 13169 macro_build (NULL, "drorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13170 break;
82dd0097 13171 }
8fc2e39e 13172 used_at = 1;
c0ebe874
RS
13173 macro_build (NULL, "dsubu", "d,v,t", AT, ZERO, op[2]);
13174 macro_build (NULL, "dsllv", "d,t,s", AT, op[1], AT);
13175 macro_build (NULL, "dsrlv", "d,t,s", op[0], op[1], op[2]);
13176 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13177 break;
13178
13179 case M_ROR:
fef14a42 13180 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13181 {
c0ebe874 13182 macro_build (NULL, "rorv", "d,t,s", op[0], op[1], op[2]);
8fc2e39e 13183 break;
82dd0097 13184 }
8fc2e39e 13185 used_at = 1;
c0ebe874
RS
13186 macro_build (NULL, "subu", "d,v,t", AT, ZERO, op[2]);
13187 macro_build (NULL, "sllv", "d,t,s", AT, op[1], AT);
13188 macro_build (NULL, "srlv", "d,t,s", op[0], op[1], op[2]);
13189 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
252b5132
RH
13190 break;
13191
771c7ce4
TS
13192 case M_DROR_I:
13193 {
13194 unsigned int rot;
e0471c16
TS
13195 const char *l;
13196 const char *rr;
771c7ce4 13197
771c7ce4 13198 rot = imm_expr.X_add_number & 0x3f;
fef14a42 13199 if (ISA_HAS_DROR (mips_opts.isa) || CPU_HAS_DROR (mips_opts.arch))
82dd0097
CD
13200 {
13201 if (rot >= 32)
c0ebe874 13202 macro_build (NULL, "dror32", SHFT_FMT, op[0], op[1], rot - 32);
82dd0097 13203 else
c0ebe874 13204 macro_build (NULL, "dror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13205 break;
82dd0097 13206 }
483fc7cd 13207 if (rot == 0)
483fc7cd 13208 {
c0ebe874 13209 macro_build (NULL, "dsrl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13210 break;
483fc7cd 13211 }
91d6fa6a 13212 rr = (rot < 0x20) ? "dsrl" : "dsrl32";
82dd0097
CD
13213 l = ((0x40 - rot) < 0x20) ? "dsll" : "dsll32";
13214 rot &= 0x1f;
8fc2e39e 13215 used_at = 1;
c0ebe874
RS
13216 macro_build (NULL, rr, SHFT_FMT, AT, op[1], rot);
13217 macro_build (NULL, l, SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13218 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4
TS
13219 }
13220 break;
13221
252b5132 13222 case M_ROR_I:
771c7ce4
TS
13223 {
13224 unsigned int rot;
13225
771c7ce4 13226 rot = imm_expr.X_add_number & 0x1f;
fef14a42 13227 if (ISA_HAS_ROR (mips_opts.isa) || CPU_HAS_ROR (mips_opts.arch))
82dd0097 13228 {
c0ebe874 13229 macro_build (NULL, "ror", SHFT_FMT, op[0], op[1], rot);
8fc2e39e 13230 break;
82dd0097 13231 }
483fc7cd 13232 if (rot == 0)
483fc7cd 13233 {
c0ebe874 13234 macro_build (NULL, "srl", SHFT_FMT, op[0], op[1], 0);
8fc2e39e 13235 break;
483fc7cd 13236 }
8fc2e39e 13237 used_at = 1;
c0ebe874
RS
13238 macro_build (NULL, "srl", SHFT_FMT, AT, op[1], rot);
13239 macro_build (NULL, "sll", SHFT_FMT, op[0], op[1], (0x20 - rot) & 0x1f);
13240 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
771c7ce4 13241 }
252b5132
RH
13242 break;
13243
252b5132 13244 case M_SEQ:
c0ebe874
RS
13245 if (op[1] == 0)
13246 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[2], BFD_RELOC_LO16);
13247 else if (op[2] == 0)
13248 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13249 else
13250 {
c0ebe874
RS
13251 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13252 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
252b5132 13253 }
8fc2e39e 13254 break;
252b5132
RH
13255
13256 case M_SEQ_I:
b0e6f033 13257 if (imm_expr.X_add_number == 0)
252b5132 13258 {
c0ebe874 13259 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13260 break;
252b5132 13261 }
c0ebe874 13262 if (op[1] == 0)
252b5132 13263 {
1661c76c 13264 as_warn (_("instruction %s: result is always false"),
252b5132 13265 ip->insn_mo->name);
c0ebe874 13266 move_register (op[0], 0);
8fc2e39e 13267 break;
252b5132 13268 }
dd3cbb7e
NC
13269 if (CPU_HAS_SEQ (mips_opts.arch)
13270 && -512 <= imm_expr.X_add_number
13271 && imm_expr.X_add_number < 512)
13272 {
c0ebe874 13273 macro_build (NULL, "seqi", "t,r,+Q", op[0], op[1],
750bdd57 13274 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13275 break;
13276 }
b0e6f033 13277 if (imm_expr.X_add_number >= 0
252b5132 13278 && imm_expr.X_add_number < 0x10000)
c0ebe874 13279 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1], BFD_RELOC_LO16);
b0e6f033 13280 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13281 && imm_expr.X_add_number < 0)
13282 {
13283 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13284 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13285 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13286 }
dd3cbb7e
NC
13287 else if (CPU_HAS_SEQ (mips_opts.arch))
13288 {
13289 used_at = 1;
bad1aba3 13290 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13291 macro_build (NULL, "seq", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13292 break;
13293 }
252b5132
RH
13294 else
13295 {
bad1aba3 13296 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13297 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13298 used_at = 1;
13299 }
c0ebe874 13300 macro_build (&expr1, "sltiu", "t,r,j", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13301 break;
252b5132 13302
c0ebe874 13303 case M_SGE: /* X >= Y <==> not (X < Y) */
252b5132
RH
13304 s = "slt";
13305 goto sge;
13306 case M_SGEU:
13307 s = "sltu";
13308 sge:
c0ebe874
RS
13309 macro_build (NULL, s, "d,v,t", op[0], op[1], op[2]);
13310 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13311 break;
252b5132 13312
c0ebe874 13313 case M_SGE_I: /* X >= I <==> not (X < I) */
252b5132 13314 case M_SGEU_I:
b0e6f033 13315 if (imm_expr.X_add_number >= -0x8000
252b5132 13316 && imm_expr.X_add_number < 0x8000)
c0ebe874
RS
13317 macro_build (&imm_expr, mask == M_SGE_I ? "slti" : "sltiu", "t,r,j",
13318 op[0], op[1], BFD_RELOC_LO16);
252b5132
RH
13319 else
13320 {
bad1aba3 13321 load_register (AT, &imm_expr, GPR_SIZE == 64);
67c0d1eb 13322 macro_build (NULL, mask == M_SGE_I ? "slt" : "sltu", "d,v,t",
c0ebe874 13323 op[0], op[1], AT);
252b5132
RH
13324 used_at = 1;
13325 }
c0ebe874 13326 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13327 break;
252b5132 13328
c0ebe874 13329 case M_SGT: /* X > Y <==> Y < X */
252b5132
RH
13330 s = "slt";
13331 goto sgt;
13332 case M_SGTU:
13333 s = "sltu";
13334 sgt:
c0ebe874 13335 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
8fc2e39e 13336 break;
252b5132 13337
c0ebe874 13338 case M_SGT_I: /* X > I <==> I < X */
252b5132
RH
13339 s = "slt";
13340 goto sgti;
13341 case M_SGTU_I:
13342 s = "sltu";
13343 sgti:
8fc2e39e 13344 used_at = 1;
bad1aba3 13345 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13346 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
252b5132
RH
13347 break;
13348
c0ebe874 13349 case M_SLE: /* X <= Y <==> Y >= X <==> not (Y < X) */
252b5132
RH
13350 s = "slt";
13351 goto sle;
13352 case M_SLEU:
13353 s = "sltu";
13354 sle:
c0ebe874
RS
13355 macro_build (NULL, s, "d,v,t", op[0], op[2], op[1]);
13356 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
8fc2e39e 13357 break;
252b5132 13358
c0ebe874 13359 case M_SLE_I: /* X <= I <==> I >= X <==> not (I < X) */
252b5132
RH
13360 s = "slt";
13361 goto slei;
13362 case M_SLEU_I:
13363 s = "sltu";
13364 slei:
8fc2e39e 13365 used_at = 1;
bad1aba3 13366 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874
RS
13367 macro_build (NULL, s, "d,v,t", op[0], AT, op[1]);
13368 macro_build (&expr1, "xori", "t,r,i", op[0], op[0], BFD_RELOC_LO16);
252b5132
RH
13369 break;
13370
13371 case M_SLT_I:
b0e6f033 13372 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13373 && imm_expr.X_add_number < 0x8000)
13374 {
c0ebe874
RS
13375 macro_build (&imm_expr, "slti", "t,r,j", op[0], op[1],
13376 BFD_RELOC_LO16);
8fc2e39e 13377 break;
252b5132 13378 }
8fc2e39e 13379 used_at = 1;
bad1aba3 13380 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13381 macro_build (NULL, "slt", "d,v,t", op[0], op[1], AT);
252b5132
RH
13382 break;
13383
13384 case M_SLTU_I:
b0e6f033 13385 if (imm_expr.X_add_number >= -0x8000
252b5132
RH
13386 && imm_expr.X_add_number < 0x8000)
13387 {
c0ebe874 13388 macro_build (&imm_expr, "sltiu", "t,r,j", op[0], op[1],
17a2f251 13389 BFD_RELOC_LO16);
8fc2e39e 13390 break;
252b5132 13391 }
8fc2e39e 13392 used_at = 1;
bad1aba3 13393 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13394 macro_build (NULL, "sltu", "d,v,t", op[0], op[1], AT);
252b5132
RH
13395 break;
13396
13397 case M_SNE:
c0ebe874
RS
13398 if (op[1] == 0)
13399 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[2]);
13400 else if (op[2] == 0)
13401 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
252b5132
RH
13402 else
13403 {
c0ebe874
RS
13404 macro_build (NULL, "xor", "d,v,t", op[0], op[1], op[2]);
13405 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
252b5132 13406 }
8fc2e39e 13407 break;
252b5132
RH
13408
13409 case M_SNE_I:
b0e6f033 13410 if (imm_expr.X_add_number == 0)
252b5132 13411 {
c0ebe874 13412 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[1]);
8fc2e39e 13413 break;
252b5132 13414 }
c0ebe874 13415 if (op[1] == 0)
252b5132 13416 {
1661c76c 13417 as_warn (_("instruction %s: result is always true"),
252b5132 13418 ip->insn_mo->name);
bad1aba3 13419 macro_build (&expr1, GPR_SIZE == 32 ? "addiu" : "daddiu", "t,r,j",
c0ebe874 13420 op[0], 0, BFD_RELOC_LO16);
8fc2e39e 13421 break;
252b5132 13422 }
dd3cbb7e
NC
13423 if (CPU_HAS_SEQ (mips_opts.arch)
13424 && -512 <= imm_expr.X_add_number
13425 && imm_expr.X_add_number < 512)
13426 {
c0ebe874 13427 macro_build (NULL, "snei", "t,r,+Q", op[0], op[1],
750bdd57 13428 (int) imm_expr.X_add_number);
dd3cbb7e
NC
13429 break;
13430 }
b0e6f033 13431 if (imm_expr.X_add_number >= 0
252b5132
RH
13432 && imm_expr.X_add_number < 0x10000)
13433 {
c0ebe874
RS
13434 macro_build (&imm_expr, "xori", "t,r,i", op[0], op[1],
13435 BFD_RELOC_LO16);
252b5132 13436 }
b0e6f033 13437 else if (imm_expr.X_add_number > -0x8000
252b5132
RH
13438 && imm_expr.X_add_number < 0)
13439 {
13440 imm_expr.X_add_number = -imm_expr.X_add_number;
bad1aba3 13441 macro_build (&imm_expr, GPR_SIZE == 32 ? "addiu" : "daddiu",
c0ebe874 13442 "t,r,j", op[0], op[1], BFD_RELOC_LO16);
252b5132 13443 }
dd3cbb7e
NC
13444 else if (CPU_HAS_SEQ (mips_opts.arch))
13445 {
13446 used_at = 1;
bad1aba3 13447 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13448 macro_build (NULL, "sne", "d,v,t", op[0], op[1], AT);
dd3cbb7e
NC
13449 break;
13450 }
252b5132
RH
13451 else
13452 {
bad1aba3 13453 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13454 macro_build (NULL, "xor", "d,v,t", op[0], op[1], AT);
252b5132
RH
13455 used_at = 1;
13456 }
c0ebe874 13457 macro_build (NULL, "sltu", "d,v,t", op[0], 0, op[0]);
8fc2e39e 13458 break;
252b5132 13459
df58fc94
RS
13460 case M_SUB_I:
13461 s = "addi";
13462 s2 = "sub";
13463 goto do_subi;
13464 case M_SUBU_I:
13465 s = "addiu";
13466 s2 = "subu";
13467 goto do_subi;
252b5132
RH
13468 case M_DSUB_I:
13469 dbl = 1;
df58fc94
RS
13470 s = "daddi";
13471 s2 = "dsub";
13472 if (!mips_opts.micromips)
13473 goto do_subi;
b0e6f033 13474 if (imm_expr.X_add_number > -0x200
df58fc94 13475 && imm_expr.X_add_number <= 0x200)
252b5132 13476 {
b0e6f033
RS
13477 macro_build (NULL, s, "t,r,.", op[0], op[1],
13478 (int) -imm_expr.X_add_number);
8fc2e39e 13479 break;
252b5132 13480 }
df58fc94 13481 goto do_subi_i;
252b5132
RH
13482 case M_DSUBU_I:
13483 dbl = 1;
df58fc94
RS
13484 s = "daddiu";
13485 s2 = "dsubu";
13486 do_subi:
b0e6f033 13487 if (imm_expr.X_add_number > -0x8000
252b5132
RH
13488 && imm_expr.X_add_number <= 0x8000)
13489 {
13490 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13491 macro_build (&imm_expr, s, "t,r,j", op[0], op[1], BFD_RELOC_LO16);
8fc2e39e 13492 break;
252b5132 13493 }
df58fc94 13494 do_subi_i:
8fc2e39e 13495 used_at = 1;
67c0d1eb 13496 load_register (AT, &imm_expr, dbl);
c0ebe874 13497 macro_build (NULL, s2, "d,v,t", op[0], op[1], AT);
252b5132
RH
13498 break;
13499
13500 case M_TEQ_I:
13501 s = "teq";
13502 goto trap;
13503 case M_TGE_I:
13504 s = "tge";
13505 goto trap;
13506 case M_TGEU_I:
13507 s = "tgeu";
13508 goto trap;
13509 case M_TLT_I:
13510 s = "tlt";
13511 goto trap;
13512 case M_TLTU_I:
13513 s = "tltu";
13514 goto trap;
13515 case M_TNE_I:
13516 s = "tne";
13517 trap:
8fc2e39e 13518 used_at = 1;
bad1aba3 13519 load_register (AT, &imm_expr, GPR_SIZE == 64);
c0ebe874 13520 macro_build (NULL, s, "s,t", op[0], AT);
252b5132
RH
13521 break;
13522
252b5132 13523 case M_TRUNCWS:
43841e91 13524 case M_TRUNCWD:
df58fc94 13525 gas_assert (!mips_opts.micromips);
0aa27725 13526 gas_assert (mips_opts.isa == ISA_MIPS1);
8fc2e39e 13527 used_at = 1;
252b5132
RH
13528
13529 /*
13530 * Is the double cfc1 instruction a bug in the mips assembler;
13531 * or is there a reason for it?
13532 */
7d10b47d 13533 start_noreorder ();
c0ebe874
RS
13534 macro_build (NULL, "cfc1", "t,G", op[2], RA);
13535 macro_build (NULL, "cfc1", "t,G", op[2], RA);
67c0d1eb 13536 macro_build (NULL, "nop", "");
252b5132 13537 expr1.X_add_number = 3;
c0ebe874 13538 macro_build (&expr1, "ori", "t,r,i", AT, op[2], BFD_RELOC_LO16);
252b5132 13539 expr1.X_add_number = 2;
67c0d1eb
RS
13540 macro_build (&expr1, "xori", "t,r,i", AT, AT, BFD_RELOC_LO16);
13541 macro_build (NULL, "ctc1", "t,G", AT, RA);
13542 macro_build (NULL, "nop", "");
13543 macro_build (NULL, mask == M_TRUNCWD ? "cvt.w.d" : "cvt.w.s", "D,S",
c0ebe874
RS
13544 op[0], op[1]);
13545 macro_build (NULL, "ctc1", "t,G", op[2], RA);
67c0d1eb 13546 macro_build (NULL, "nop", "");
7d10b47d 13547 end_noreorder ();
252b5132
RH
13548 break;
13549
f2ae14a1 13550 case M_ULH_AB:
252b5132 13551 s = "lb";
df58fc94
RS
13552 s2 = "lbu";
13553 off = 1;
13554 goto uld_st;
f2ae14a1 13555 case M_ULHU_AB:
252b5132 13556 s = "lbu";
df58fc94
RS
13557 s2 = "lbu";
13558 off = 1;
13559 goto uld_st;
f2ae14a1 13560 case M_ULW_AB:
df58fc94
RS
13561 s = "lwl";
13562 s2 = "lwr";
7f3c4072 13563 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13564 off = 3;
13565 goto uld_st;
f2ae14a1 13566 case M_ULD_AB:
252b5132
RH
13567 s = "ldl";
13568 s2 = "ldr";
7f3c4072 13569 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13570 off = 7;
df58fc94 13571 goto uld_st;
f2ae14a1 13572 case M_USH_AB:
df58fc94
RS
13573 s = "sb";
13574 s2 = "sb";
13575 off = 1;
13576 ust = 1;
13577 goto uld_st;
f2ae14a1 13578 case M_USW_AB:
df58fc94
RS
13579 s = "swl";
13580 s2 = "swr";
7f3c4072 13581 offbits = (mips_opts.micromips ? 12 : 16);
252b5132 13582 off = 3;
df58fc94
RS
13583 ust = 1;
13584 goto uld_st;
f2ae14a1 13585 case M_USD_AB:
df58fc94
RS
13586 s = "sdl";
13587 s2 = "sdr";
7f3c4072 13588 offbits = (mips_opts.micromips ? 12 : 16);
df58fc94
RS
13589 off = 7;
13590 ust = 1;
13591
13592 uld_st:
c0ebe874 13593 breg = op[2];
f2ae14a1 13594 large_offset = !small_offset_p (off, align, offbits);
df58fc94
RS
13595 ep = &offset_expr;
13596 expr1.X_add_number = 0;
f2ae14a1 13597 if (large_offset)
df58fc94
RS
13598 {
13599 used_at = 1;
13600 tempreg = AT;
f2ae14a1
RS
13601 if (small_offset_p (0, align, 16))
13602 macro_build (ep, ADDRESS_ADDI_INSN, "t,r,j", tempreg, breg, -1,
13603 offset_reloc[0], offset_reloc[1], offset_reloc[2]);
13604 else
13605 {
13606 load_address (tempreg, ep, &used_at);
13607 if (breg != 0)
13608 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t",
13609 tempreg, tempreg, breg);
13610 }
13611 offset_reloc[0] = BFD_RELOC_LO16;
13612 offset_reloc[1] = BFD_RELOC_UNUSED;
13613 offset_reloc[2] = BFD_RELOC_UNUSED;
df58fc94 13614 breg = tempreg;
c0ebe874 13615 tempreg = op[0];
df58fc94
RS
13616 ep = &expr1;
13617 }
c0ebe874 13618 else if (!ust && op[0] == breg)
8fc2e39e
TS
13619 {
13620 used_at = 1;
13621 tempreg = AT;
13622 }
252b5132 13623 else
c0ebe874 13624 tempreg = op[0];
af22f5b2 13625
df58fc94
RS
13626 if (off == 1)
13627 goto ulh_sh;
252b5132 13628
90ecf173 13629 if (!target_big_endian)
df58fc94 13630 ep->X_add_number += off;
f2ae14a1 13631 if (offbits == 12)
c8276761 13632 macro_build (NULL, s, "t,~(b)", tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13633 else
13634 macro_build (ep, s, "t,o(b)", tempreg, -1,
13635 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94 13636
90ecf173 13637 if (!target_big_endian)
df58fc94 13638 ep->X_add_number -= off;
252b5132 13639 else
df58fc94 13640 ep->X_add_number += off;
f2ae14a1 13641 if (offbits == 12)
df58fc94 13642 macro_build (NULL, s2, "t,~(b)",
c8276761 13643 tempreg, (int) ep->X_add_number, breg);
f2ae14a1
RS
13644 else
13645 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13646 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13647
df58fc94 13648 /* If necessary, move the result in tempreg to the final destination. */
c0ebe874 13649 if (!ust && op[0] != tempreg)
df58fc94
RS
13650 {
13651 /* Protect second load's delay slot. */
13652 load_delay_nop ();
c0ebe874 13653 move_register (op[0], tempreg);
df58fc94 13654 }
8fc2e39e 13655 break;
252b5132 13656
df58fc94 13657 ulh_sh:
d6bc6245 13658 used_at = 1;
df58fc94
RS
13659 if (target_big_endian == ust)
13660 ep->X_add_number += off;
c0ebe874 13661 tempreg = ust || large_offset ? op[0] : AT;
f2ae14a1
RS
13662 macro_build (ep, s, "t,o(b)", tempreg, -1,
13663 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
df58fc94
RS
13664
13665 /* For halfword transfers we need a temporary register to shuffle
13666 bytes. Unfortunately for M_USH_A we have none available before
13667 the next store as AT holds the base address. We deal with this
13668 case by clobbering TREG and then restoring it as with ULH. */
c0ebe874 13669 tempreg = ust == large_offset ? op[0] : AT;
df58fc94 13670 if (ust)
c0ebe874 13671 macro_build (NULL, "srl", SHFT_FMT, tempreg, op[0], 8);
df58fc94
RS
13672
13673 if (target_big_endian == ust)
13674 ep->X_add_number -= off;
252b5132 13675 else
df58fc94 13676 ep->X_add_number += off;
f2ae14a1
RS
13677 macro_build (ep, s2, "t,o(b)", tempreg, -1,
13678 offset_reloc[0], offset_reloc[1], offset_reloc[2], breg);
252b5132 13679
df58fc94 13680 /* For M_USH_A re-retrieve the LSB. */
f2ae14a1 13681 if (ust && large_offset)
df58fc94
RS
13682 {
13683 if (target_big_endian)
13684 ep->X_add_number += off;
13685 else
13686 ep->X_add_number -= off;
f2ae14a1
RS
13687 macro_build (&expr1, "lbu", "t,o(b)", AT, -1,
13688 offset_reloc[0], offset_reloc[1], offset_reloc[2], AT);
df58fc94
RS
13689 }
13690 /* For ULH and M_USH_A OR the LSB in. */
f2ae14a1 13691 if (!ust || large_offset)
df58fc94 13692 {
c0ebe874 13693 tempreg = !large_offset ? AT : op[0];
df58fc94 13694 macro_build (NULL, "sll", SHFT_FMT, tempreg, tempreg, 8);
c0ebe874 13695 macro_build (NULL, "or", "d,v,t", op[0], op[0], AT);
df58fc94 13696 }
252b5132
RH
13697 break;
13698
13699 default:
13700 /* FIXME: Check if this is one of the itbl macros, since they
bdaaa2e1 13701 are added dynamically. */
1661c76c 13702 as_bad (_("macro %s not implemented yet"), ip->insn_mo->name);
252b5132
RH
13703 break;
13704 }
741fe287 13705 if (!mips_opts.at && used_at)
1661c76c 13706 as_bad (_("macro used $at after \".set noat\""));
252b5132
RH
13707}
13708
13709/* Implement macros in mips16 mode. */
13710
13711static void
17a2f251 13712mips16_macro (struct mips_cl_insn *ip)
252b5132 13713{
c0ebe874 13714 const struct mips_operand_array *operands;
252b5132 13715 int mask;
c0ebe874 13716 int tmp;
252b5132
RH
13717 expressionS expr1;
13718 int dbl;
13719 const char *s, *s2, *s3;
c0ebe874
RS
13720 unsigned int op[MAX_OPERANDS];
13721 unsigned int i;
252b5132
RH
13722
13723 mask = ip->insn_mo->mask;
13724
c0ebe874
RS
13725 operands = insn_operands (ip);
13726 for (i = 0; i < MAX_OPERANDS; i++)
13727 if (operands->operand[i])
13728 op[i] = insn_extract_operand (ip, operands->operand[i]);
13729 else
13730 op[i] = -1;
252b5132 13731
252b5132
RH
13732 expr1.X_op = O_constant;
13733 expr1.X_op_symbol = NULL;
13734 expr1.X_add_symbol = NULL;
13735 expr1.X_add_number = 1;
13736
13737 dbl = 0;
13738
13739 switch (mask)
13740 {
13741 default:
b37df7c4 13742 abort ();
252b5132
RH
13743
13744 case M_DDIV_3:
13745 dbl = 1;
1a0670f3 13746 /* Fall through. */
252b5132
RH
13747 case M_DIV_3:
13748 s = "mflo";
13749 goto do_div3;
13750 case M_DREM_3:
13751 dbl = 1;
1a0670f3 13752 /* Fall through. */
252b5132
RH
13753 case M_REM_3:
13754 s = "mfhi";
13755 do_div3:
7d10b47d 13756 start_noreorder ();
d8722d76 13757 macro_build (NULL, dbl ? "ddiv" : "div", ".,x,y", op[1], op[2]);
252b5132 13758 expr1.X_add_number = 2;
c0ebe874 13759 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13760 macro_build (NULL, "break", "6", 7);
bdaaa2e1 13761
252b5132
RH
13762 /* FIXME: The normal code checks for of -1 / -0x80000000 here,
13763 since that causes an overflow. We should do that as well,
13764 but I don't see how to do the comparisons without a temporary
13765 register. */
7d10b47d 13766 end_noreorder ();
c0ebe874 13767 macro_build (NULL, s, "x", op[0]);
252b5132
RH
13768 break;
13769
13770 case M_DIVU_3:
13771 s = "divu";
13772 s2 = "mflo";
13773 goto do_divu3;
13774 case M_REMU_3:
13775 s = "divu";
13776 s2 = "mfhi";
13777 goto do_divu3;
13778 case M_DDIVU_3:
13779 s = "ddivu";
13780 s2 = "mflo";
13781 goto do_divu3;
13782 case M_DREMU_3:
13783 s = "ddivu";
13784 s2 = "mfhi";
13785 do_divu3:
7d10b47d 13786 start_noreorder ();
d8722d76 13787 macro_build (NULL, s, ".,x,y", op[1], op[2]);
252b5132 13788 expr1.X_add_number = 2;
c0ebe874 13789 macro_build (&expr1, "bnez", "x,p", op[2]);
67c0d1eb 13790 macro_build (NULL, "break", "6", 7);
7d10b47d 13791 end_noreorder ();
c0ebe874 13792 macro_build (NULL, s2, "x", op[0]);
252b5132
RH
13793 break;
13794
13795 case M_DMUL:
13796 dbl = 1;
1a0670f3 13797 /* Fall through. */
252b5132 13798 case M_MUL:
c0ebe874
RS
13799 macro_build (NULL, dbl ? "dmultu" : "multu", "x,y", op[1], op[2]);
13800 macro_build (NULL, "mflo", "x", op[0]);
8fc2e39e 13801 break;
252b5132
RH
13802
13803 case M_DSUBU_I:
13804 dbl = 1;
13805 goto do_subu;
13806 case M_SUBU_I:
13807 do_subu:
252b5132 13808 imm_expr.X_add_number = -imm_expr.X_add_number;
d8722d76 13809 macro_build (&imm_expr, dbl ? "daddiu" : "addiu", "y,x,F", op[0], op[1]);
252b5132
RH
13810 break;
13811
13812 case M_SUBU_I_2:
252b5132 13813 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13814 macro_build (&imm_expr, "addiu", "x,k", op[0]);
252b5132
RH
13815 break;
13816
13817 case M_DSUBU_I_2:
252b5132 13818 imm_expr.X_add_number = -imm_expr.X_add_number;
c0ebe874 13819 macro_build (&imm_expr, "daddiu", "y,j", op[0]);
252b5132
RH
13820 break;
13821
13822 case M_BEQ:
13823 s = "cmp";
13824 s2 = "bteqz";
13825 goto do_branch;
13826 case M_BNE:
13827 s = "cmp";
13828 s2 = "btnez";
13829 goto do_branch;
13830 case M_BLT:
13831 s = "slt";
13832 s2 = "btnez";
13833 goto do_branch;
13834 case M_BLTU:
13835 s = "sltu";
13836 s2 = "btnez";
13837 goto do_branch;
13838 case M_BLE:
13839 s = "slt";
13840 s2 = "bteqz";
13841 goto do_reverse_branch;
13842 case M_BLEU:
13843 s = "sltu";
13844 s2 = "bteqz";
13845 goto do_reverse_branch;
13846 case M_BGE:
13847 s = "slt";
13848 s2 = "bteqz";
13849 goto do_branch;
13850 case M_BGEU:
13851 s = "sltu";
13852 s2 = "bteqz";
13853 goto do_branch;
13854 case M_BGT:
13855 s = "slt";
13856 s2 = "btnez";
13857 goto do_reverse_branch;
13858 case M_BGTU:
13859 s = "sltu";
13860 s2 = "btnez";
13861
13862 do_reverse_branch:
c0ebe874
RS
13863 tmp = op[1];
13864 op[1] = op[0];
13865 op[0] = tmp;
252b5132
RH
13866
13867 do_branch:
c0ebe874 13868 macro_build (NULL, s, "x,y", op[0], op[1]);
67c0d1eb 13869 macro_build (&offset_expr, s2, "p");
252b5132
RH
13870 break;
13871
13872 case M_BEQ_I:
13873 s = "cmpi";
13874 s2 = "bteqz";
13875 s3 = "x,U";
13876 goto do_branch_i;
13877 case M_BNE_I:
13878 s = "cmpi";
13879 s2 = "btnez";
13880 s3 = "x,U";
13881 goto do_branch_i;
13882 case M_BLT_I:
13883 s = "slti";
13884 s2 = "btnez";
13885 s3 = "x,8";
13886 goto do_branch_i;
13887 case M_BLTU_I:
13888 s = "sltiu";
13889 s2 = "btnez";
13890 s3 = "x,8";
13891 goto do_branch_i;
13892 case M_BLE_I:
13893 s = "slti";
13894 s2 = "btnez";
13895 s3 = "x,8";
13896 goto do_addone_branch_i;
13897 case M_BLEU_I:
13898 s = "sltiu";
13899 s2 = "btnez";
13900 s3 = "x,8";
13901 goto do_addone_branch_i;
13902 case M_BGE_I:
13903 s = "slti";
13904 s2 = "bteqz";
13905 s3 = "x,8";
13906 goto do_branch_i;
13907 case M_BGEU_I:
13908 s = "sltiu";
13909 s2 = "bteqz";
13910 s3 = "x,8";
13911 goto do_branch_i;
13912 case M_BGT_I:
13913 s = "slti";
13914 s2 = "bteqz";
13915 s3 = "x,8";
13916 goto do_addone_branch_i;
13917 case M_BGTU_I:
13918 s = "sltiu";
13919 s2 = "bteqz";
13920 s3 = "x,8";
13921
13922 do_addone_branch_i:
252b5132
RH
13923 ++imm_expr.X_add_number;
13924
13925 do_branch_i:
c0ebe874 13926 macro_build (&imm_expr, s, s3, op[0]);
67c0d1eb 13927 macro_build (&offset_expr, s2, "p");
252b5132
RH
13928 break;
13929
13930 case M_ABS:
13931 expr1.X_add_number = 0;
c0ebe874
RS
13932 macro_build (&expr1, "slti", "x,8", op[1]);
13933 if (op[0] != op[1])
13934 macro_build (NULL, "move", "y,X", op[0], mips16_to_32_reg_map[op[1]]);
252b5132 13935 expr1.X_add_number = 2;
67c0d1eb 13936 macro_build (&expr1, "bteqz", "p");
c0ebe874 13937 macro_build (NULL, "neg", "x,w", op[0], op[0]);
0acfaea6 13938 break;
252b5132
RH
13939 }
13940}
13941
14daeee3
RS
13942/* Look up instruction [START, START + LENGTH) in HASH. Record any extra
13943 opcode bits in *OPCODE_EXTRA. */
13944
13945static struct mips_opcode *
13946mips_lookup_insn (struct hash_control *hash, const char *start,
da8bca91 13947 ssize_t length, unsigned int *opcode_extra)
14daeee3
RS
13948{
13949 char *name, *dot, *p;
13950 unsigned int mask, suffix;
da8bca91 13951 ssize_t opend;
14daeee3
RS
13952 struct mips_opcode *insn;
13953
13954 /* Make a copy of the instruction so that we can fiddle with it. */
4ec9d7d5 13955 name = xstrndup (start, length);
14daeee3
RS
13956
13957 /* Look up the instruction as-is. */
13958 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13959 if (insn)
e1fa0163 13960 goto end;
14daeee3
RS
13961
13962 dot = strchr (name, '.');
13963 if (dot && dot[1])
13964 {
13965 /* Try to interpret the text after the dot as a VU0 channel suffix. */
13966 p = mips_parse_vu0_channels (dot + 1, &mask);
13967 if (*p == 0 && mask != 0)
13968 {
13969 *dot = 0;
13970 insn = (struct mips_opcode *) hash_find (hash, name);
13971 *dot = '.';
13972 if (insn && (insn->pinfo2 & INSN2_VU0_CHANNEL_SUFFIX) != 0)
13973 {
13974 *opcode_extra |= mask << mips_vu0_channel_mask.lsb;
e1fa0163 13975 goto end;
14daeee3
RS
13976 }
13977 }
13978 }
13979
13980 if (mips_opts.micromips)
13981 {
13982 /* See if there's an instruction size override suffix,
13983 either `16' or `32', at the end of the mnemonic proper,
13984 that defines the operation, i.e. before the first `.'
13985 character if any. Strip it and retry. */
13986 opend = dot != NULL ? dot - name : length;
13987 if (opend >= 3 && name[opend - 2] == '1' && name[opend - 1] == '6')
13988 suffix = 2;
13989 else if (name[opend - 2] == '3' && name[opend - 1] == '2')
13990 suffix = 4;
13991 else
13992 suffix = 0;
13993 if (suffix)
13994 {
39334a61 13995 memmove (name + opend - 2, name + opend, length - opend + 1);
14daeee3 13996 insn = (struct mips_opcode *) hash_find (hash, name);
ee5734f0 13997 if (insn)
14daeee3
RS
13998 {
13999 forced_insn_length = suffix;
e1fa0163 14000 goto end;
14daeee3
RS
14001 }
14002 }
14003 }
14004
e1fa0163
NC
14005 insn = NULL;
14006 end:
14007 free (name);
14008 return insn;
14daeee3
RS
14009}
14010
77bd4346 14011/* Assemble an instruction into its binary format. If the instruction
e423441d
RS
14012 is a macro, set imm_expr and offset_expr to the values associated
14013 with "I" and "A" operands respectively. Otherwise store the value
14014 of the relocatable field (if any) in offset_expr. In both cases
14015 set offset_reloc to the relocation operators applied to offset_expr. */
252b5132
RH
14016
14017static void
60f20e8b 14018mips_ip (char *str, struct mips_cl_insn *insn)
252b5132 14019{
60f20e8b 14020 const struct mips_opcode *first, *past;
df58fc94 14021 struct hash_control *hash;
a92713e6 14022 char format;
14daeee3 14023 size_t end;
a92713e6 14024 struct mips_operand_token *tokens;
14daeee3 14025 unsigned int opcode_extra;
252b5132 14026
df58fc94
RS
14027 if (mips_opts.micromips)
14028 {
14029 hash = micromips_op_hash;
14030 past = &micromips_opcodes[bfd_micromips_num_opcodes];
14031 }
14032 else
14033 {
14034 hash = op_hash;
14035 past = &mips_opcodes[NUMOPCODES];
14036 }
14037 forced_insn_length = 0;
14daeee3 14038 opcode_extra = 0;
252b5132 14039
df58fc94 14040 /* We first try to match an instruction up to a space or to the end. */
a40bc9dd
RS
14041 for (end = 0; str[end] != '\0' && !ISSPACE (str[end]); end++)
14042 continue;
bdaaa2e1 14043
60f20e8b
RS
14044 first = mips_lookup_insn (hash, str, end, &opcode_extra);
14045 if (first == NULL)
252b5132 14046 {
1661c76c 14047 set_insn_error (0, _("unrecognized opcode"));
a40bc9dd 14048 return;
252b5132
RH
14049 }
14050
60f20e8b 14051 if (strcmp (first->name, "li.s") == 0)
a92713e6 14052 format = 'f';
60f20e8b 14053 else if (strcmp (first->name, "li.d") == 0)
a92713e6
RS
14054 format = 'd';
14055 else
14056 format = 0;
14057 tokens = mips_parse_arguments (str + end, format);
14058 if (!tokens)
14059 return;
14060
60f20e8b
RS
14061 if (!match_insns (insn, first, past, tokens, opcode_extra, FALSE)
14062 && !match_insns (insn, first, past, tokens, opcode_extra, TRUE))
1661c76c 14063 set_insn_error (0, _("invalid operands"));
df58fc94 14064
e3de51ce 14065 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14066}
14067
77bd4346
RS
14068/* As for mips_ip, but used when assembling MIPS16 code.
14069 Also set forced_insn_length to the resulting instruction size in
14070 bytes if the user explicitly requested a small or extended instruction. */
252b5132
RH
14071
14072static void
60f20e8b 14073mips16_ip (char *str, struct mips_cl_insn *insn)
252b5132 14074{
1a00e612 14075 char *end, *s, c;
60f20e8b 14076 struct mips_opcode *first;
a92713e6 14077 struct mips_operand_token *tokens;
3fb49709 14078 unsigned int l;
252b5132 14079
25499ac7 14080 for (s = str; *s != '\0' && *s != '.' && *s != ' '; ++s)
252b5132 14081 ;
1a00e612
RS
14082 end = s;
14083 c = *end;
3fb49709
MR
14084
14085 l = 0;
1a00e612 14086 switch (c)
252b5132
RH
14087 {
14088 case '\0':
14089 break;
14090
14091 case ' ':
1a00e612 14092 s++;
252b5132
RH
14093 break;
14094
14095 case '.':
3fb49709
MR
14096 s++;
14097 if (*s == 't')
252b5132 14098 {
3fb49709
MR
14099 l = 2;
14100 s++;
252b5132 14101 }
3fb49709 14102 else if (*s == 'e')
252b5132 14103 {
3fb49709
MR
14104 l = 4;
14105 s++;
252b5132 14106 }
3fb49709
MR
14107 if (*s == '\0')
14108 break;
14109 else if (*s++ == ' ')
14110 break;
1661c76c 14111 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14112 return;
14113 }
3fb49709 14114 forced_insn_length = l;
252b5132 14115
1a00e612 14116 *end = 0;
60f20e8b 14117 first = (struct mips_opcode *) hash_find (mips16_op_hash, str);
1a00e612
RS
14118 *end = c;
14119
60f20e8b 14120 if (!first)
252b5132 14121 {
1661c76c 14122 set_insn_error (0, _("unrecognized opcode"));
252b5132
RH
14123 return;
14124 }
14125
a92713e6
RS
14126 tokens = mips_parse_arguments (s, 0);
14127 if (!tokens)
14128 return;
14129
60f20e8b 14130 if (!match_mips16_insns (insn, first, tokens))
1661c76c 14131 set_insn_error (0, _("invalid operands"));
252b5132 14132
e3de51ce 14133 obstack_free (&mips_operand_tokens, tokens);
252b5132
RH
14134}
14135
b886a2ab
RS
14136/* Marshal immediate value VAL for an extended MIPS16 instruction.
14137 NBITS is the number of significant bits in VAL. */
14138
14139static unsigned long
14140mips16_immed_extend (offsetT val, unsigned int nbits)
14141{
14142 int extval;
25499ac7
MR
14143
14144 extval = 0;
14145 val &= (1U << nbits) - 1;
14146 if (nbits == 16 || nbits == 9)
b886a2ab
RS
14147 {
14148 extval = ((val >> 11) & 0x1f) | (val & 0x7e0);
14149 val &= 0x1f;
14150 }
14151 else if (nbits == 15)
14152 {
14153 extval = ((val >> 11) & 0xf) | (val & 0x7f0);
14154 val &= 0xf;
14155 }
25499ac7 14156 else if (nbits == 6)
b886a2ab
RS
14157 {
14158 extval = ((val & 0x1f) << 6) | (val & 0x20);
14159 val = 0;
14160 }
14161 return (extval << 16) | val;
14162}
14163
3ccad066
RS
14164/* Like decode_mips16_operand, but require the operand to be defined and
14165 require it to be an integer. */
14166
14167static const struct mips_int_operand *
14168mips16_immed_operand (int type, bfd_boolean extended_p)
14169{
14170 const struct mips_operand *operand;
14171
14172 operand = decode_mips16_operand (type, extended_p);
14173 if (!operand || (operand->type != OP_INT && operand->type != OP_PCREL))
14174 abort ();
14175 return (const struct mips_int_operand *) operand;
14176}
14177
14178/* Return true if SVAL fits OPERAND. RELOC is as for mips16_immed. */
14179
14180static bfd_boolean
14181mips16_immed_in_range_p (const struct mips_int_operand *operand,
14182 bfd_reloc_code_real_type reloc, offsetT sval)
14183{
14184 int min_val, max_val;
14185
14186 min_val = mips_int_operand_min (operand);
14187 max_val = mips_int_operand_max (operand);
14188 if (reloc != BFD_RELOC_UNUSED)
14189 {
14190 if (min_val < 0)
14191 sval = SEXT_16BIT (sval);
14192 else
14193 sval &= 0xffff;
14194 }
14195
14196 return (sval >= min_val
14197 && sval <= max_val
14198 && (sval & ((1 << operand->shift) - 1)) == 0);
14199}
14200
5c04167a
RS
14201/* Install immediate value VAL into MIPS16 instruction *INSN,
14202 extending it if necessary. The instruction in *INSN may
14203 already be extended.
14204
43c0598f
RS
14205 RELOC is the relocation that produced VAL, or BFD_RELOC_UNUSED
14206 if none. In the former case, VAL is a 16-bit number with no
14207 defined signedness.
14208
14209 TYPE is the type of the immediate field. USER_INSN_LENGTH
14210 is the length that the user requested, or 0 if none. */
252b5132
RH
14211
14212static void
3b4dbbbf 14213mips16_immed (const char *file, unsigned int line, int type,
43c0598f 14214 bfd_reloc_code_real_type reloc, offsetT val,
5c04167a 14215 unsigned int user_insn_length, unsigned long *insn)
252b5132 14216{
3ccad066
RS
14217 const struct mips_int_operand *operand;
14218 unsigned int uval, length;
252b5132 14219
3ccad066
RS
14220 operand = mips16_immed_operand (type, FALSE);
14221 if (!mips16_immed_in_range_p (operand, reloc, val))
5c04167a
RS
14222 {
14223 /* We need an extended instruction. */
14224 if (user_insn_length == 2)
14225 as_bad_where (file, line, _("invalid unextended operand value"));
14226 else
14227 *insn |= MIPS16_EXTEND;
14228 }
14229 else if (user_insn_length == 4)
14230 {
14231 /* The operand doesn't force an unextended instruction to be extended.
14232 Warn if the user wanted an extended instruction anyway. */
14233 *insn |= MIPS16_EXTEND;
14234 as_warn_where (file, line,
14235 _("extended operand requested but not required"));
14236 }
252b5132 14237
3ccad066
RS
14238 length = mips16_opcode_length (*insn);
14239 if (length == 4)
252b5132 14240 {
3ccad066
RS
14241 operand = mips16_immed_operand (type, TRUE);
14242 if (!mips16_immed_in_range_p (operand, reloc, val))
14243 as_bad_where (file, line,
14244 _("operand value out of range for instruction"));
252b5132 14245 }
3ccad066 14246 uval = ((unsigned int) val >> operand->shift) - operand->bias;
bdd15286 14247 if (length == 2 || operand->root.lsb != 0)
3ccad066 14248 *insn = mips_insert_operand (&operand->root, *insn, uval);
252b5132 14249 else
3ccad066 14250 *insn |= mips16_immed_extend (uval, operand->root.size);
252b5132
RH
14251}
14252\f
d6f16593 14253struct percent_op_match
ad8d3bb3 14254{
5e0116d5
RS
14255 const char *str;
14256 bfd_reloc_code_real_type reloc;
d6f16593
MR
14257};
14258
14259static const struct percent_op_match mips_percent_op[] =
ad8d3bb3 14260{
5e0116d5 14261 {"%lo", BFD_RELOC_LO16},
5e0116d5
RS
14262 {"%call_hi", BFD_RELOC_MIPS_CALL_HI16},
14263 {"%call_lo", BFD_RELOC_MIPS_CALL_LO16},
14264 {"%call16", BFD_RELOC_MIPS_CALL16},
14265 {"%got_disp", BFD_RELOC_MIPS_GOT_DISP},
14266 {"%got_page", BFD_RELOC_MIPS_GOT_PAGE},
14267 {"%got_ofst", BFD_RELOC_MIPS_GOT_OFST},
14268 {"%got_hi", BFD_RELOC_MIPS_GOT_HI16},
14269 {"%got_lo", BFD_RELOC_MIPS_GOT_LO16},
14270 {"%got", BFD_RELOC_MIPS_GOT16},
14271 {"%gp_rel", BFD_RELOC_GPREL16},
be3f1006 14272 {"%gprel", BFD_RELOC_GPREL16},
5e0116d5
RS
14273 {"%half", BFD_RELOC_16},
14274 {"%highest", BFD_RELOC_MIPS_HIGHEST},
14275 {"%higher", BFD_RELOC_MIPS_HIGHER},
14276 {"%neg", BFD_RELOC_MIPS_SUB},
3f98094e
DJ
14277 {"%tlsgd", BFD_RELOC_MIPS_TLS_GD},
14278 {"%tlsldm", BFD_RELOC_MIPS_TLS_LDM},
14279 {"%dtprel_hi", BFD_RELOC_MIPS_TLS_DTPREL_HI16},
14280 {"%dtprel_lo", BFD_RELOC_MIPS_TLS_DTPREL_LO16},
14281 {"%tprel_hi", BFD_RELOC_MIPS_TLS_TPREL_HI16},
14282 {"%tprel_lo", BFD_RELOC_MIPS_TLS_TPREL_LO16},
14283 {"%gottprel", BFD_RELOC_MIPS_TLS_GOTTPREL},
7361da2c
AB
14284 {"%hi", BFD_RELOC_HI16_S},
14285 {"%pcrel_hi", BFD_RELOC_HI16_S_PCREL},
14286 {"%pcrel_lo", BFD_RELOC_LO16_PCREL}
ad8d3bb3
TS
14287};
14288
d6f16593
MR
14289static const struct percent_op_match mips16_percent_op[] =
14290{
14291 {"%lo", BFD_RELOC_MIPS16_LO16},
be3f1006 14292 {"%gp_rel", BFD_RELOC_MIPS16_GPREL},
d6f16593 14293 {"%gprel", BFD_RELOC_MIPS16_GPREL},
738e5348
RS
14294 {"%got", BFD_RELOC_MIPS16_GOT16},
14295 {"%call16", BFD_RELOC_MIPS16_CALL16},
d0f13682
CLT
14296 {"%hi", BFD_RELOC_MIPS16_HI16_S},
14297 {"%tlsgd", BFD_RELOC_MIPS16_TLS_GD},
14298 {"%tlsldm", BFD_RELOC_MIPS16_TLS_LDM},
14299 {"%dtprel_hi", BFD_RELOC_MIPS16_TLS_DTPREL_HI16},
14300 {"%dtprel_lo", BFD_RELOC_MIPS16_TLS_DTPREL_LO16},
14301 {"%tprel_hi", BFD_RELOC_MIPS16_TLS_TPREL_HI16},
14302 {"%tprel_lo", BFD_RELOC_MIPS16_TLS_TPREL_LO16},
14303 {"%gottprel", BFD_RELOC_MIPS16_TLS_GOTTPREL}
d6f16593
MR
14304};
14305
252b5132 14306
5e0116d5
RS
14307/* Return true if *STR points to a relocation operator. When returning true,
14308 move *STR over the operator and store its relocation code in *RELOC.
14309 Leave both *STR and *RELOC alone when returning false. */
14310
14311static bfd_boolean
17a2f251 14312parse_relocation (char **str, bfd_reloc_code_real_type *reloc)
252b5132 14313{
d6f16593
MR
14314 const struct percent_op_match *percent_op;
14315 size_t limit, i;
14316
14317 if (mips_opts.mips16)
14318 {
14319 percent_op = mips16_percent_op;
14320 limit = ARRAY_SIZE (mips16_percent_op);
14321 }
14322 else
14323 {
14324 percent_op = mips_percent_op;
14325 limit = ARRAY_SIZE (mips_percent_op);
14326 }
76b3015f 14327
d6f16593 14328 for (i = 0; i < limit; i++)
5e0116d5 14329 if (strncasecmp (*str, percent_op[i].str, strlen (percent_op[i].str)) == 0)
394f9b3a 14330 {
3f98094e
DJ
14331 int len = strlen (percent_op[i].str);
14332
14333 if (!ISSPACE ((*str)[len]) && (*str)[len] != '(')
14334 continue;
14335
5e0116d5
RS
14336 *str += strlen (percent_op[i].str);
14337 *reloc = percent_op[i].reloc;
394f9b3a 14338
5e0116d5
RS
14339 /* Check whether the output BFD supports this relocation.
14340 If not, issue an error and fall back on something safe. */
14341 if (!bfd_reloc_type_lookup (stdoutput, percent_op[i].reloc))
394f9b3a 14342 {
20203fb9 14343 as_bad (_("relocation %s isn't supported by the current ABI"),
5e0116d5 14344 percent_op[i].str);
01a3f561 14345 *reloc = BFD_RELOC_UNUSED;
394f9b3a 14346 }
5e0116d5 14347 return TRUE;
394f9b3a 14348 }
5e0116d5 14349 return FALSE;
394f9b3a 14350}
ad8d3bb3 14351
ad8d3bb3 14352
5e0116d5
RS
14353/* Parse string STR as a 16-bit relocatable operand. Store the
14354 expression in *EP and the relocations in the array starting
14355 at RELOC. Return the number of relocation operators used.
ad8d3bb3 14356
01a3f561 14357 On exit, EXPR_END points to the first character after the expression. */
ad8d3bb3 14358
5e0116d5 14359static size_t
17a2f251
TS
14360my_getSmallExpression (expressionS *ep, bfd_reloc_code_real_type *reloc,
14361 char *str)
ad8d3bb3 14362{
5e0116d5
RS
14363 bfd_reloc_code_real_type reversed_reloc[3];
14364 size_t reloc_index, i;
09b8f35a
RS
14365 int crux_depth, str_depth;
14366 char *crux;
5e0116d5
RS
14367
14368 /* Search for the start of the main expression, recoding relocations
09b8f35a
RS
14369 in REVERSED_RELOC. End the loop with CRUX pointing to the start
14370 of the main expression and with CRUX_DEPTH containing the number
14371 of open brackets at that point. */
14372 reloc_index = -1;
14373 str_depth = 0;
14374 do
fb1b3232 14375 {
09b8f35a
RS
14376 reloc_index++;
14377 crux = str;
14378 crux_depth = str_depth;
14379
14380 /* Skip over whitespace and brackets, keeping count of the number
14381 of brackets. */
14382 while (*str == ' ' || *str == '\t' || *str == '(')
14383 if (*str++ == '(')
14384 str_depth++;
5e0116d5 14385 }
09b8f35a
RS
14386 while (*str == '%'
14387 && reloc_index < (HAVE_NEWABI ? 3 : 1)
14388 && parse_relocation (&str, &reversed_reloc[reloc_index]));
ad8d3bb3 14389
09b8f35a 14390 my_getExpression (ep, crux);
5e0116d5 14391 str = expr_end;
394f9b3a 14392
5e0116d5 14393 /* Match every open bracket. */
09b8f35a 14394 while (crux_depth > 0 && (*str == ')' || *str == ' ' || *str == '\t'))
5e0116d5 14395 if (*str++ == ')')
09b8f35a 14396 crux_depth--;
394f9b3a 14397
09b8f35a 14398 if (crux_depth > 0)
20203fb9 14399 as_bad (_("unclosed '('"));
394f9b3a 14400
5e0116d5 14401 expr_end = str;
252b5132 14402
01a3f561 14403 if (reloc_index != 0)
64bdfcaf
RS
14404 {
14405 prev_reloc_op_frag = frag_now;
14406 for (i = 0; i < reloc_index; i++)
14407 reloc[i] = reversed_reloc[reloc_index - 1 - i];
14408 }
fb1b3232 14409
5e0116d5 14410 return reloc_index;
252b5132
RH
14411}
14412
14413static void
17a2f251 14414my_getExpression (expressionS *ep, char *str)
252b5132
RH
14415{
14416 char *save_in;
14417
14418 save_in = input_line_pointer;
14419 input_line_pointer = str;
14420 expression (ep);
14421 expr_end = input_line_pointer;
14422 input_line_pointer = save_in;
252b5132
RH
14423}
14424
6d4af3c2 14425const char *
17a2f251 14426md_atof (int type, char *litP, int *sizeP)
252b5132 14427{
499ac353 14428 return ieee_md_atof (type, litP, sizeP, target_big_endian);
252b5132
RH
14429}
14430
14431void
17a2f251 14432md_number_to_chars (char *buf, valueT val, int n)
252b5132
RH
14433{
14434 if (target_big_endian)
14435 number_to_chars_bigendian (buf, val, n);
14436 else
14437 number_to_chars_littleendian (buf, val, n);
14438}
14439\f
e013f690
TS
14440static int support_64bit_objects(void)
14441{
14442 const char **list, **l;
aa3d8fdf 14443 int yes;
e013f690
TS
14444
14445 list = bfd_target_list ();
14446 for (l = list; *l != NULL; l++)
aeffff67
RS
14447 if (strcmp (*l, ELF_TARGET ("elf64-", "big")) == 0
14448 || strcmp (*l, ELF_TARGET ("elf64-", "little")) == 0)
e013f690 14449 break;
aa3d8fdf 14450 yes = (*l != NULL);
e013f690 14451 free (list);
aa3d8fdf 14452 return yes;
e013f690
TS
14453}
14454
316f5878
RS
14455/* Set STRING_PTR (either &mips_arch_string or &mips_tune_string) to
14456 NEW_VALUE. Warn if another value was already specified. Note:
14457 we have to defer parsing the -march and -mtune arguments in order
14458 to handle 'from-abi' correctly, since the ABI might be specified
14459 in a later argument. */
14460
14461static void
17a2f251 14462mips_set_option_string (const char **string_ptr, const char *new_value)
316f5878
RS
14463{
14464 if (*string_ptr != 0 && strcasecmp (*string_ptr, new_value) != 0)
1661c76c 14465 as_warn (_("a different %s was already specified, is now %s"),
316f5878
RS
14466 string_ptr == &mips_arch_string ? "-march" : "-mtune",
14467 new_value);
14468
14469 *string_ptr = new_value;
14470}
14471
252b5132 14472int
17b9d67d 14473md_parse_option (int c, const char *arg)
252b5132 14474{
c6278170
RS
14475 unsigned int i;
14476
14477 for (i = 0; i < ARRAY_SIZE (mips_ases); i++)
14478 if (c == mips_ases[i].option_on || c == mips_ases[i].option_off)
14479 {
919731af 14480 file_ase_explicit |= mips_set_ase (&mips_ases[i], &file_mips_opts,
c6278170
RS
14481 c == mips_ases[i].option_on);
14482 return 1;
14483 }
14484
252b5132
RH
14485 switch (c)
14486 {
119d663a
NC
14487 case OPTION_CONSTRUCT_FLOATS:
14488 mips_disable_float_construction = 0;
14489 break;
bdaaa2e1 14490
119d663a
NC
14491 case OPTION_NO_CONSTRUCT_FLOATS:
14492 mips_disable_float_construction = 1;
14493 break;
bdaaa2e1 14494
252b5132
RH
14495 case OPTION_TRAP:
14496 mips_trap = 1;
14497 break;
14498
14499 case OPTION_BREAK:
14500 mips_trap = 0;
14501 break;
14502
14503 case OPTION_EB:
14504 target_big_endian = 1;
14505 break;
14506
14507 case OPTION_EL:
14508 target_big_endian = 0;
14509 break;
14510
14511 case 'O':
4ffff32f
TS
14512 if (arg == NULL)
14513 mips_optimize = 1;
14514 else if (arg[0] == '0')
14515 mips_optimize = 0;
14516 else if (arg[0] == '1')
252b5132
RH
14517 mips_optimize = 1;
14518 else
14519 mips_optimize = 2;
14520 break;
14521
14522 case 'g':
14523 if (arg == NULL)
14524 mips_debug = 2;
14525 else
14526 mips_debug = atoi (arg);
252b5132
RH
14527 break;
14528
14529 case OPTION_MIPS1:
0b35dfee 14530 file_mips_opts.isa = ISA_MIPS1;
252b5132
RH
14531 break;
14532
14533 case OPTION_MIPS2:
0b35dfee 14534 file_mips_opts.isa = ISA_MIPS2;
252b5132
RH
14535 break;
14536
14537 case OPTION_MIPS3:
0b35dfee 14538 file_mips_opts.isa = ISA_MIPS3;
252b5132
RH
14539 break;
14540
14541 case OPTION_MIPS4:
0b35dfee 14542 file_mips_opts.isa = ISA_MIPS4;
e7af610e
NC
14543 break;
14544
84ea6cf2 14545 case OPTION_MIPS5:
0b35dfee 14546 file_mips_opts.isa = ISA_MIPS5;
84ea6cf2
NC
14547 break;
14548
e7af610e 14549 case OPTION_MIPS32:
0b35dfee 14550 file_mips_opts.isa = ISA_MIPS32;
252b5132
RH
14551 break;
14552
af7ee8bf 14553 case OPTION_MIPS32R2:
0b35dfee 14554 file_mips_opts.isa = ISA_MIPS32R2;
af7ee8bf
CD
14555 break;
14556
ae52f483 14557 case OPTION_MIPS32R3:
0ae19f05 14558 file_mips_opts.isa = ISA_MIPS32R3;
ae52f483
AB
14559 break;
14560
14561 case OPTION_MIPS32R5:
0ae19f05 14562 file_mips_opts.isa = ISA_MIPS32R5;
ae52f483
AB
14563 break;
14564
7361da2c
AB
14565 case OPTION_MIPS32R6:
14566 file_mips_opts.isa = ISA_MIPS32R6;
14567 break;
14568
5f74bc13 14569 case OPTION_MIPS64R2:
0b35dfee 14570 file_mips_opts.isa = ISA_MIPS64R2;
5f74bc13
CD
14571 break;
14572
ae52f483 14573 case OPTION_MIPS64R3:
0ae19f05 14574 file_mips_opts.isa = ISA_MIPS64R3;
ae52f483
AB
14575 break;
14576
14577 case OPTION_MIPS64R5:
0ae19f05 14578 file_mips_opts.isa = ISA_MIPS64R5;
ae52f483
AB
14579 break;
14580
7361da2c
AB
14581 case OPTION_MIPS64R6:
14582 file_mips_opts.isa = ISA_MIPS64R6;
14583 break;
14584
84ea6cf2 14585 case OPTION_MIPS64:
0b35dfee 14586 file_mips_opts.isa = ISA_MIPS64;
84ea6cf2
NC
14587 break;
14588
ec68c924 14589 case OPTION_MTUNE:
316f5878
RS
14590 mips_set_option_string (&mips_tune_string, arg);
14591 break;
ec68c924 14592
316f5878
RS
14593 case OPTION_MARCH:
14594 mips_set_option_string (&mips_arch_string, arg);
252b5132
RH
14595 break;
14596
14597 case OPTION_M4650:
316f5878
RS
14598 mips_set_option_string (&mips_arch_string, "4650");
14599 mips_set_option_string (&mips_tune_string, "4650");
252b5132
RH
14600 break;
14601
14602 case OPTION_NO_M4650:
14603 break;
14604
14605 case OPTION_M4010:
316f5878
RS
14606 mips_set_option_string (&mips_arch_string, "4010");
14607 mips_set_option_string (&mips_tune_string, "4010");
252b5132
RH
14608 break;
14609
14610 case OPTION_NO_M4010:
14611 break;
14612
14613 case OPTION_M4100:
316f5878
RS
14614 mips_set_option_string (&mips_arch_string, "4100");
14615 mips_set_option_string (&mips_tune_string, "4100");
252b5132
RH
14616 break;
14617
14618 case OPTION_NO_M4100:
14619 break;
14620
252b5132 14621 case OPTION_M3900:
316f5878
RS
14622 mips_set_option_string (&mips_arch_string, "3900");
14623 mips_set_option_string (&mips_tune_string, "3900");
252b5132 14624 break;
bdaaa2e1 14625
252b5132
RH
14626 case OPTION_NO_M3900:
14627 break;
14628
df58fc94 14629 case OPTION_MICROMIPS:
919731af 14630 if (file_mips_opts.mips16 == 1)
df58fc94
RS
14631 {
14632 as_bad (_("-mmicromips cannot be used with -mips16"));
14633 return 0;
14634 }
919731af 14635 file_mips_opts.micromips = 1;
df58fc94
RS
14636 mips_no_prev_insn ();
14637 break;
14638
14639 case OPTION_NO_MICROMIPS:
919731af 14640 file_mips_opts.micromips = 0;
df58fc94
RS
14641 mips_no_prev_insn ();
14642 break;
14643
252b5132 14644 case OPTION_MIPS16:
919731af 14645 if (file_mips_opts.micromips == 1)
df58fc94
RS
14646 {
14647 as_bad (_("-mips16 cannot be used with -micromips"));
14648 return 0;
14649 }
919731af 14650 file_mips_opts.mips16 = 1;
7d10b47d 14651 mips_no_prev_insn ();
252b5132
RH
14652 break;
14653
14654 case OPTION_NO_MIPS16:
919731af 14655 file_mips_opts.mips16 = 0;
7d10b47d 14656 mips_no_prev_insn ();
252b5132
RH
14657 break;
14658
6a32d874
CM
14659 case OPTION_FIX_24K:
14660 mips_fix_24k = 1;
14661 break;
14662
14663 case OPTION_NO_FIX_24K:
14664 mips_fix_24k = 0;
14665 break;
14666
a8d14a88
CM
14667 case OPTION_FIX_RM7000:
14668 mips_fix_rm7000 = 1;
14669 break;
14670
14671 case OPTION_NO_FIX_RM7000:
14672 mips_fix_rm7000 = 0;
14673 break;
14674
c67a084a
NC
14675 case OPTION_FIX_LOONGSON2F_JUMP:
14676 mips_fix_loongson2f_jump = TRUE;
14677 break;
14678
14679 case OPTION_NO_FIX_LOONGSON2F_JUMP:
14680 mips_fix_loongson2f_jump = FALSE;
14681 break;
14682
14683 case OPTION_FIX_LOONGSON2F_NOP:
14684 mips_fix_loongson2f_nop = TRUE;
14685 break;
14686
14687 case OPTION_NO_FIX_LOONGSON2F_NOP:
14688 mips_fix_loongson2f_nop = FALSE;
14689 break;
14690
d766e8ec
RS
14691 case OPTION_FIX_VR4120:
14692 mips_fix_vr4120 = 1;
60b63b72
RS
14693 break;
14694
d766e8ec
RS
14695 case OPTION_NO_FIX_VR4120:
14696 mips_fix_vr4120 = 0;
60b63b72
RS
14697 break;
14698
7d8e00cf
RS
14699 case OPTION_FIX_VR4130:
14700 mips_fix_vr4130 = 1;
14701 break;
14702
14703 case OPTION_NO_FIX_VR4130:
14704 mips_fix_vr4130 = 0;
14705 break;
14706
d954098f
DD
14707 case OPTION_FIX_CN63XXP1:
14708 mips_fix_cn63xxp1 = TRUE;
14709 break;
14710
14711 case OPTION_NO_FIX_CN63XXP1:
14712 mips_fix_cn63xxp1 = FALSE;
14713 break;
14714
4a6a3df4
AO
14715 case OPTION_RELAX_BRANCH:
14716 mips_relax_branch = 1;
14717 break;
14718
14719 case OPTION_NO_RELAX_BRANCH:
14720 mips_relax_branch = 0;
14721 break;
14722
8b10b0b3
MR
14723 case OPTION_IGNORE_BRANCH_ISA:
14724 mips_ignore_branch_isa = TRUE;
14725 break;
14726
14727 case OPTION_NO_IGNORE_BRANCH_ISA:
14728 mips_ignore_branch_isa = FALSE;
14729 break;
14730
833794fc 14731 case OPTION_INSN32:
919731af 14732 file_mips_opts.insn32 = TRUE;
833794fc
MR
14733 break;
14734
14735 case OPTION_NO_INSN32:
919731af 14736 file_mips_opts.insn32 = FALSE;
833794fc
MR
14737 break;
14738
aa6975fb
ILT
14739 case OPTION_MSHARED:
14740 mips_in_shared = TRUE;
14741 break;
14742
14743 case OPTION_MNO_SHARED:
14744 mips_in_shared = FALSE;
14745 break;
14746
aed1a261 14747 case OPTION_MSYM32:
919731af 14748 file_mips_opts.sym32 = TRUE;
aed1a261
RS
14749 break;
14750
14751 case OPTION_MNO_SYM32:
919731af 14752 file_mips_opts.sym32 = FALSE;
aed1a261
RS
14753 break;
14754
252b5132
RH
14755 /* When generating ELF code, we permit -KPIC and -call_shared to
14756 select SVR4_PIC, and -non_shared to select no PIC. This is
14757 intended to be compatible with Irix 5. */
14758 case OPTION_CALL_SHARED:
252b5132 14759 mips_pic = SVR4_PIC;
143d77c5 14760 mips_abicalls = TRUE;
252b5132
RH
14761 break;
14762
861fb55a 14763 case OPTION_CALL_NONPIC:
861fb55a
DJ
14764 mips_pic = NO_PIC;
14765 mips_abicalls = TRUE;
14766 break;
14767
252b5132 14768 case OPTION_NON_SHARED:
252b5132 14769 mips_pic = NO_PIC;
143d77c5 14770 mips_abicalls = FALSE;
252b5132
RH
14771 break;
14772
44075ae2
TS
14773 /* The -xgot option tells the assembler to use 32 bit offsets
14774 when accessing the got in SVR4_PIC mode. It is for Irix
252b5132
RH
14775 compatibility. */
14776 case OPTION_XGOT:
14777 mips_big_got = 1;
14778 break;
14779
14780 case 'G':
6caf9ef4
TS
14781 g_switch_value = atoi (arg);
14782 g_switch_seen = 1;
252b5132
RH
14783 break;
14784
34ba82a8
TS
14785 /* The -32, -n32 and -64 options are shortcuts for -mabi=32, -mabi=n32
14786 and -mabi=64. */
252b5132 14787 case OPTION_32:
f3ded42a 14788 mips_abi = O32_ABI;
252b5132
RH
14789 break;
14790
e013f690 14791 case OPTION_N32:
316f5878 14792 mips_abi = N32_ABI;
e013f690 14793 break;
252b5132 14794
e013f690 14795 case OPTION_64:
316f5878 14796 mips_abi = N64_ABI;
f43abd2b 14797 if (!support_64bit_objects())
1661c76c 14798 as_fatal (_("no compiled in support for 64 bit object file format"));
252b5132
RH
14799 break;
14800
c97ef257 14801 case OPTION_GP32:
bad1aba3 14802 file_mips_opts.gp = 32;
c97ef257
AH
14803 break;
14804
14805 case OPTION_GP64:
bad1aba3 14806 file_mips_opts.gp = 64;
c97ef257 14807 break;
252b5132 14808
ca4e0257 14809 case OPTION_FP32:
0b35dfee 14810 file_mips_opts.fp = 32;
316f5878
RS
14811 break;
14812
351cdf24
MF
14813 case OPTION_FPXX:
14814 file_mips_opts.fp = 0;
14815 break;
14816
316f5878 14817 case OPTION_FP64:
0b35dfee 14818 file_mips_opts.fp = 64;
ca4e0257
RS
14819 break;
14820
351cdf24
MF
14821 case OPTION_ODD_SPREG:
14822 file_mips_opts.oddspreg = 1;
14823 break;
14824
14825 case OPTION_NO_ODD_SPREG:
14826 file_mips_opts.oddspreg = 0;
14827 break;
14828
037b32b9 14829 case OPTION_SINGLE_FLOAT:
0b35dfee 14830 file_mips_opts.single_float = 1;
037b32b9
AN
14831 break;
14832
14833 case OPTION_DOUBLE_FLOAT:
0b35dfee 14834 file_mips_opts.single_float = 0;
037b32b9
AN
14835 break;
14836
14837 case OPTION_SOFT_FLOAT:
0b35dfee 14838 file_mips_opts.soft_float = 1;
037b32b9
AN
14839 break;
14840
14841 case OPTION_HARD_FLOAT:
0b35dfee 14842 file_mips_opts.soft_float = 0;
037b32b9
AN
14843 break;
14844
252b5132 14845 case OPTION_MABI:
e013f690 14846 if (strcmp (arg, "32") == 0)
316f5878 14847 mips_abi = O32_ABI;
e013f690 14848 else if (strcmp (arg, "o64") == 0)
316f5878 14849 mips_abi = O64_ABI;
e013f690 14850 else if (strcmp (arg, "n32") == 0)
316f5878 14851 mips_abi = N32_ABI;
e013f690
TS
14852 else if (strcmp (arg, "64") == 0)
14853 {
316f5878 14854 mips_abi = N64_ABI;
e013f690 14855 if (! support_64bit_objects())
1661c76c 14856 as_fatal (_("no compiled in support for 64 bit object file "
e013f690
TS
14857 "format"));
14858 }
14859 else if (strcmp (arg, "eabi") == 0)
316f5878 14860 mips_abi = EABI_ABI;
e013f690 14861 else
da0e507f
TS
14862 {
14863 as_fatal (_("invalid abi -mabi=%s"), arg);
14864 return 0;
14865 }
252b5132
RH
14866 break;
14867
6b76fefe 14868 case OPTION_M7000_HILO_FIX:
b34976b6 14869 mips_7000_hilo_fix = TRUE;
6b76fefe
CM
14870 break;
14871
9ee72ff1 14872 case OPTION_MNO_7000_HILO_FIX:
b34976b6 14873 mips_7000_hilo_fix = FALSE;
6b76fefe
CM
14874 break;
14875
ecb4347a 14876 case OPTION_MDEBUG:
b34976b6 14877 mips_flag_mdebug = TRUE;
ecb4347a
DJ
14878 break;
14879
14880 case OPTION_NO_MDEBUG:
b34976b6 14881 mips_flag_mdebug = FALSE;
ecb4347a 14882 break;
dcd410fe
RO
14883
14884 case OPTION_PDR:
14885 mips_flag_pdr = TRUE;
14886 break;
14887
14888 case OPTION_NO_PDR:
14889 mips_flag_pdr = FALSE;
14890 break;
0a44bf69
RS
14891
14892 case OPTION_MVXWORKS_PIC:
14893 mips_pic = VXWORKS_PIC;
14894 break;
ecb4347a 14895
ba92f887
MR
14896 case OPTION_NAN:
14897 if (strcmp (arg, "2008") == 0)
7361da2c 14898 mips_nan2008 = 1;
ba92f887 14899 else if (strcmp (arg, "legacy") == 0)
7361da2c 14900 mips_nan2008 = 0;
ba92f887
MR
14901 else
14902 {
1661c76c 14903 as_fatal (_("invalid NaN setting -mnan=%s"), arg);
ba92f887
MR
14904 return 0;
14905 }
14906 break;
14907
252b5132
RH
14908 default:
14909 return 0;
14910 }
14911
c67a084a
NC
14912 mips_fix_loongson2f = mips_fix_loongson2f_nop || mips_fix_loongson2f_jump;
14913
252b5132
RH
14914 return 1;
14915}
316f5878 14916\f
919731af 14917/* Set up globals to tune for the ISA or processor described by INFO. */
252b5132 14918
316f5878 14919static void
17a2f251 14920mips_set_tune (const struct mips_cpu_info *info)
316f5878
RS
14921{
14922 if (info != 0)
fef14a42 14923 mips_tune = info->cpu;
316f5878 14924}
80cc45a5 14925
34ba82a8 14926
252b5132 14927void
17a2f251 14928mips_after_parse_args (void)
e9670677 14929{
fef14a42
TS
14930 const struct mips_cpu_info *arch_info = 0;
14931 const struct mips_cpu_info *tune_info = 0;
14932
e9670677 14933 /* GP relative stuff not working for PE */
6caf9ef4 14934 if (strncmp (TARGET_OS, "pe", 2) == 0)
e9670677 14935 {
6caf9ef4 14936 if (g_switch_seen && g_switch_value != 0)
1661c76c 14937 as_bad (_("-G not supported in this configuration"));
e9670677
MR
14938 g_switch_value = 0;
14939 }
14940
cac012d6
AO
14941 if (mips_abi == NO_ABI)
14942 mips_abi = MIPS_DEFAULT_ABI;
14943
919731af 14944 /* The following code determines the architecture.
22923709
RS
14945 Similar code was added to GCC 3.3 (see override_options() in
14946 config/mips/mips.c). The GAS and GCC code should be kept in sync
14947 as much as possible. */
e9670677 14948
316f5878 14949 if (mips_arch_string != 0)
fef14a42 14950 arch_info = mips_parse_cpu ("-march", mips_arch_string);
e9670677 14951
0b35dfee 14952 if (file_mips_opts.isa != ISA_UNKNOWN)
e9670677 14953 {
0b35dfee 14954 /* Handle -mipsN. At this point, file_mips_opts.isa contains the
fef14a42 14955 ISA level specified by -mipsN, while arch_info->isa contains
316f5878 14956 the -march selection (if any). */
fef14a42 14957 if (arch_info != 0)
e9670677 14958 {
316f5878
RS
14959 /* -march takes precedence over -mipsN, since it is more descriptive.
14960 There's no harm in specifying both as long as the ISA levels
14961 are the same. */
0b35dfee 14962 if (file_mips_opts.isa != arch_info->isa)
1661c76c
RS
14963 as_bad (_("-%s conflicts with the other architecture options,"
14964 " which imply -%s"),
0b35dfee 14965 mips_cpu_info_from_isa (file_mips_opts.isa)->name,
fef14a42 14966 mips_cpu_info_from_isa (arch_info->isa)->name);
e9670677 14967 }
316f5878 14968 else
0b35dfee 14969 arch_info = mips_cpu_info_from_isa (file_mips_opts.isa);
e9670677
MR
14970 }
14971
fef14a42 14972 if (arch_info == 0)
95bfe26e
MF
14973 {
14974 arch_info = mips_parse_cpu ("default CPU", MIPS_CPU_STRING_DEFAULT);
14975 gas_assert (arch_info);
14976 }
e9670677 14977
fef14a42 14978 if (ABI_NEEDS_64BIT_REGS (mips_abi) && !ISA_HAS_64BIT_REGS (arch_info->isa))
20203fb9 14979 as_bad (_("-march=%s is not compatible with the selected ABI"),
fef14a42
TS
14980 arch_info->name);
14981
919731af 14982 file_mips_opts.arch = arch_info->cpu;
14983 file_mips_opts.isa = arch_info->isa;
14984
14985 /* Set up initial mips_opts state. */
14986 mips_opts = file_mips_opts;
14987
14988 /* The register size inference code is now placed in
14989 file_mips_check_options. */
fef14a42 14990
0b35dfee 14991 /* Optimize for file_mips_opts.arch, unless -mtune selects a different
14992 processor. */
fef14a42
TS
14993 if (mips_tune_string != 0)
14994 tune_info = mips_parse_cpu ("-mtune", mips_tune_string);
e9670677 14995
fef14a42
TS
14996 if (tune_info == 0)
14997 mips_set_tune (arch_info);
14998 else
14999 mips_set_tune (tune_info);
e9670677 15000
ecb4347a 15001 if (mips_flag_mdebug < 0)
e8044f35 15002 mips_flag_mdebug = 0;
e9670677
MR
15003}
15004\f
15005void
17a2f251 15006mips_init_after_args (void)
252b5132
RH
15007{
15008 /* initialize opcodes */
15009 bfd_mips_num_opcodes = bfd_mips_num_builtin_opcodes;
beae10d5 15010 mips_opcodes = (struct mips_opcode *) mips_builtin_opcodes;
252b5132
RH
15011}
15012
15013long
17a2f251 15014md_pcrel_from (fixS *fixP)
252b5132 15015{
a7ebbfdf
TS
15016 valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
15017 switch (fixP->fx_r_type)
15018 {
df58fc94
RS
15019 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15020 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15021 /* Return the address of the delay slot. */
15022 return addr + 2;
15023
15024 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15025 case BFD_RELOC_MICROMIPS_JMP:
c9775dde 15026 case BFD_RELOC_MIPS16_16_PCREL_S1:
a7ebbfdf 15027 case BFD_RELOC_16_PCREL_S2:
7361da2c
AB
15028 case BFD_RELOC_MIPS_21_PCREL_S2:
15029 case BFD_RELOC_MIPS_26_PCREL_S2:
a7ebbfdf
TS
15030 case BFD_RELOC_MIPS_JMP:
15031 /* Return the address of the delay slot. */
15032 return addr + 4;
df58fc94 15033
51f6035b
MR
15034 case BFD_RELOC_MIPS_18_PCREL_S3:
15035 /* Return the aligned address of the doubleword containing
15036 the instruction. */
15037 return addr & ~7;
15038
a7ebbfdf
TS
15039 default:
15040 return addr;
15041 }
252b5132
RH
15042}
15043
252b5132
RH
15044/* This is called before the symbol table is processed. In order to
15045 work with gcc when using mips-tfile, we must keep all local labels.
15046 However, in other cases, we want to discard them. If we were
15047 called with -g, but we didn't see any debugging information, it may
15048 mean that gcc is smuggling debugging information through to
15049 mips-tfile, in which case we must generate all local labels. */
15050
15051void
17a2f251 15052mips_frob_file_before_adjust (void)
252b5132
RH
15053{
15054#ifndef NO_ECOFF_DEBUGGING
15055 if (ECOFF_DEBUGGING
15056 && mips_debug != 0
15057 && ! ecoff_debugging_seen)
15058 flag_keep_locals = 1;
15059#endif
15060}
15061
3b91255e 15062/* Sort any unmatched HI16 and GOT16 relocs so that they immediately precede
55cf6793 15063 the corresponding LO16 reloc. This is called before md_apply_fix and
3b91255e
RS
15064 tc_gen_reloc. Unmatched relocs can only be generated by use of explicit
15065 relocation operators.
15066
15067 For our purposes, a %lo() expression matches a %got() or %hi()
15068 expression if:
15069
15070 (a) it refers to the same symbol; and
15071 (b) the offset applied in the %lo() expression is no lower than
15072 the offset applied in the %got() or %hi().
15073
15074 (b) allows us to cope with code like:
15075
15076 lui $4,%hi(foo)
15077 lh $4,%lo(foo+2)($4)
15078
15079 ...which is legal on RELA targets, and has a well-defined behaviour
15080 if the user knows that adding 2 to "foo" will not induce a carry to
15081 the high 16 bits.
15082
15083 When several %lo()s match a particular %got() or %hi(), we use the
15084 following rules to distinguish them:
15085
15086 (1) %lo()s with smaller offsets are a better match than %lo()s with
15087 higher offsets.
15088
15089 (2) %lo()s with no matching %got() or %hi() are better than those
15090 that already have a matching %got() or %hi().
15091
15092 (3) later %lo()s are better than earlier %lo()s.
15093
15094 These rules are applied in order.
15095
15096 (1) means, among other things, that %lo()s with identical offsets are
15097 chosen if they exist.
15098
15099 (2) means that we won't associate several high-part relocations with
15100 the same low-part relocation unless there's no alternative. Having
15101 several high parts for the same low part is a GNU extension; this rule
15102 allows careful users to avoid it.
15103
15104 (3) is purely cosmetic. mips_hi_fixup_list is is in reverse order,
15105 with the last high-part relocation being at the front of the list.
15106 It therefore makes sense to choose the last matching low-part
15107 relocation, all other things being equal. It's also easier
15108 to code that way. */
252b5132
RH
15109
15110void
17a2f251 15111mips_frob_file (void)
252b5132
RH
15112{
15113 struct mips_hi_fixup *l;
35903be0 15114 bfd_reloc_code_real_type looking_for_rtype = BFD_RELOC_UNUSED;
252b5132
RH
15115
15116 for (l = mips_hi_fixup_list; l != NULL; l = l->next)
15117 {
15118 segment_info_type *seginfo;
3b91255e
RS
15119 bfd_boolean matched_lo_p;
15120 fixS **hi_pos, **lo_pos, **pos;
252b5132 15121
9c2799c2 15122 gas_assert (reloc_needs_lo_p (l->fixp->fx_r_type));
252b5132 15123
5919d012 15124 /* If a GOT16 relocation turns out to be against a global symbol,
b886a2ab
RS
15125 there isn't supposed to be a matching LO. Ignore %gots against
15126 constants; we'll report an error for those later. */
738e5348 15127 if (got16_reloc_p (l->fixp->fx_r_type)
b886a2ab 15128 && !(l->fixp->fx_addsy
9e009953 15129 && pic_need_relax (l->fixp->fx_addsy)))
5919d012
RS
15130 continue;
15131
15132 /* Check quickly whether the next fixup happens to be a matching %lo. */
15133 if (fixup_has_matching_lo_p (l->fixp))
252b5132
RH
15134 continue;
15135
252b5132 15136 seginfo = seg_info (l->seg);
252b5132 15137
3b91255e
RS
15138 /* Set HI_POS to the position of this relocation in the chain.
15139 Set LO_POS to the position of the chosen low-part relocation.
15140 MATCHED_LO_P is true on entry to the loop if *POS is a low-part
15141 relocation that matches an immediately-preceding high-part
15142 relocation. */
15143 hi_pos = NULL;
15144 lo_pos = NULL;
15145 matched_lo_p = FALSE;
738e5348 15146 looking_for_rtype = matching_lo_reloc (l->fixp->fx_r_type);
35903be0 15147
3b91255e
RS
15148 for (pos = &seginfo->fix_root; *pos != NULL; pos = &(*pos)->fx_next)
15149 {
15150 if (*pos == l->fixp)
15151 hi_pos = pos;
15152
35903be0 15153 if ((*pos)->fx_r_type == looking_for_rtype
30cfc97a 15154 && symbol_same_p ((*pos)->fx_addsy, l->fixp->fx_addsy)
3b91255e
RS
15155 && (*pos)->fx_offset >= l->fixp->fx_offset
15156 && (lo_pos == NULL
15157 || (*pos)->fx_offset < (*lo_pos)->fx_offset
15158 || (!matched_lo_p
15159 && (*pos)->fx_offset == (*lo_pos)->fx_offset)))
15160 lo_pos = pos;
15161
15162 matched_lo_p = (reloc_needs_lo_p ((*pos)->fx_r_type)
15163 && fixup_has_matching_lo_p (*pos));
15164 }
15165
15166 /* If we found a match, remove the high-part relocation from its
15167 current position and insert it before the low-part relocation.
15168 Make the offsets match so that fixup_has_matching_lo_p()
15169 will return true.
15170
15171 We don't warn about unmatched high-part relocations since some
15172 versions of gcc have been known to emit dead "lui ...%hi(...)"
15173 instructions. */
15174 if (lo_pos != NULL)
15175 {
15176 l->fixp->fx_offset = (*lo_pos)->fx_offset;
15177 if (l->fixp->fx_next != *lo_pos)
252b5132 15178 {
3b91255e
RS
15179 *hi_pos = l->fixp->fx_next;
15180 l->fixp->fx_next = *lo_pos;
15181 *lo_pos = l->fixp;
252b5132 15182 }
252b5132
RH
15183 }
15184 }
15185}
15186
252b5132 15187int
17a2f251 15188mips_force_relocation (fixS *fixp)
252b5132 15189{
ae6063d4 15190 if (generic_force_reloc (fixp))
252b5132
RH
15191 return 1;
15192
df58fc94
RS
15193 /* We want to keep BFD_RELOC_MICROMIPS_*_PCREL_S1 relocation,
15194 so that the linker relaxation can update targets. */
15195 if (fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
15196 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
15197 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1)
15198 return 1;
15199
5caa2b07
MR
15200 /* We want to keep BFD_RELOC_16_PCREL_S2 BFD_RELOC_MIPS_21_PCREL_S2
15201 and BFD_RELOC_MIPS_26_PCREL_S2 relocations against MIPS16 and
15202 microMIPS symbols so that we can do cross-mode branch diagnostics
15203 and BAL to JALX conversion by the linker. */
15204 if ((fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
9d862524
MR
15205 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15206 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2)
15207 && fixp->fx_addsy
15208 && ELF_ST_IS_COMPRESSED (S_GET_OTHER (fixp->fx_addsy)))
15209 return 1;
15210
7361da2c 15211 /* We want all PC-relative relocations to be kept for R6 relaxation. */
912815f0 15212 if (ISA_IS_R6 (file_mips_opts.isa)
7361da2c
AB
15213 && (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
15214 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
15215 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
15216 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
15217 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
15218 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
15219 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL))
15220 return 1;
15221
3e722fb5 15222 return 0;
252b5132
RH
15223}
15224
b416ba9b
MR
15225/* Implement TC_FORCE_RELOCATION_ABS. */
15226
15227bfd_boolean
15228mips_force_relocation_abs (fixS *fixp)
15229{
15230 if (generic_force_reloc (fixp))
15231 return TRUE;
15232
15233 /* These relocations do not have enough bits in the in-place addend
15234 to hold an arbitrary absolute section's offset. */
15235 if (HAVE_IN_PLACE_ADDENDS && limited_pcrel_reloc_p (fixp->fx_r_type))
15236 return TRUE;
15237
15238 return FALSE;
15239}
15240
b886a2ab
RS
15241/* Read the instruction associated with RELOC from BUF. */
15242
15243static unsigned int
15244read_reloc_insn (char *buf, bfd_reloc_code_real_type reloc)
15245{
15246 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15247 return read_compressed_insn (buf, 4);
15248 else
15249 return read_insn (buf);
15250}
15251
15252/* Write instruction INSN to BUF, given that it has been relocated
15253 by RELOC. */
15254
15255static void
15256write_reloc_insn (char *buf, bfd_reloc_code_real_type reloc,
15257 unsigned long insn)
15258{
15259 if (mips16_reloc_p (reloc) || micromips_reloc_p (reloc))
15260 write_compressed_insn (buf, insn, 4);
15261 else
15262 write_insn (buf, insn);
15263}
15264
9d862524
MR
15265/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15266 to a symbol in another ISA mode, which cannot be converted to JALX. */
15267
15268static bfd_boolean
15269fix_bad_cross_mode_jump_p (fixS *fixP)
15270{
15271 unsigned long opcode;
15272 int other;
15273 char *buf;
15274
15275 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15276 return FALSE;
15277
15278 other = S_GET_OTHER (fixP->fx_addsy);
15279 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15280 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15281 switch (fixP->fx_r_type)
15282 {
15283 case BFD_RELOC_MIPS_JMP:
15284 return opcode != 0x1d && opcode != 0x03 && ELF_ST_IS_COMPRESSED (other);
15285 case BFD_RELOC_MICROMIPS_JMP:
15286 return opcode != 0x3c && opcode != 0x3d && !ELF_ST_IS_MICROMIPS (other);
15287 default:
15288 return FALSE;
15289 }
15290}
15291
15292/* Return TRUE if the instruction pointed to by FIXP is an invalid JALX
15293 jump to a symbol in the same ISA mode. */
15294
15295static bfd_boolean
15296fix_bad_same_mode_jalx_p (fixS *fixP)
15297{
15298 unsigned long opcode;
15299 int other;
15300 char *buf;
15301
15302 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15303 return FALSE;
15304
15305 other = S_GET_OTHER (fixP->fx_addsy);
15306 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15307 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 26;
15308 switch (fixP->fx_r_type)
15309 {
15310 case BFD_RELOC_MIPS_JMP:
15311 return opcode == 0x1d && !ELF_ST_IS_COMPRESSED (other);
15312 case BFD_RELOC_MIPS16_JMP:
15313 return opcode == 0x07 && ELF_ST_IS_COMPRESSED (other);
15314 case BFD_RELOC_MICROMIPS_JMP:
15315 return opcode == 0x3c && ELF_ST_IS_COMPRESSED (other);
15316 default:
15317 return FALSE;
15318 }
15319}
15320
15321/* Return TRUE if the instruction pointed to by FIXP is an invalid jump
15322 to a symbol whose value plus addend is not aligned according to the
15323 ultimate (after linker relaxation) jump instruction's immediate field
15324 requirement, either to (1 << SHIFT), or, for jumps from microMIPS to
15325 regular MIPS code, to (1 << 2). */
15326
15327static bfd_boolean
15328fix_bad_misaligned_jump_p (fixS *fixP, int shift)
15329{
15330 bfd_boolean micro_to_mips_p;
15331 valueT val;
15332 int other;
15333
15334 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15335 return FALSE;
15336
15337 other = S_GET_OTHER (fixP->fx_addsy);
15338 val = S_GET_VALUE (fixP->fx_addsy) | ELF_ST_IS_COMPRESSED (other);
15339 val += fixP->fx_offset;
15340 micro_to_mips_p = (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15341 && !ELF_ST_IS_MICROMIPS (other));
15342 return ((val & ((1 << (micro_to_mips_p ? 2 : shift)) - 1))
15343 != ELF_ST_IS_COMPRESSED (other));
15344}
15345
15346/* Return TRUE if the instruction pointed to by FIXP is an invalid branch
15347 to a symbol whose annotation indicates another ISA mode. For absolute
a6ebf616
MR
15348 symbols check the ISA bit instead.
15349
15350 We accept BFD_RELOC_16_PCREL_S2 relocations against MIPS16 and microMIPS
15351 symbols or BFD_RELOC_MICROMIPS_16_PCREL_S1 relocations against regular
15352 MIPS symbols and associated with BAL instructions as these instructions
de194d85 15353 may be converted to JALX by the linker. */
9d862524
MR
15354
15355static bfd_boolean
15356fix_bad_cross_mode_branch_p (fixS *fixP)
15357{
15358 bfd_boolean absolute_p;
15359 unsigned long opcode;
15360 asection *symsec;
15361 valueT val;
15362 int other;
15363 char *buf;
15364
8b10b0b3
MR
15365 if (mips_ignore_branch_isa)
15366 return FALSE;
15367
9d862524
MR
15368 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15369 return FALSE;
15370
15371 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15372 absolute_p = bfd_is_abs_section (symsec);
15373
15374 val = S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset;
15375 other = S_GET_OTHER (fixP->fx_addsy);
15376
15377 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
15378 opcode = read_reloc_insn (buf, fixP->fx_r_type) >> 16;
15379 switch (fixP->fx_r_type)
15380 {
15381 case BFD_RELOC_16_PCREL_S2:
a6ebf616
MR
15382 return ((absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other))
15383 && opcode != 0x0411);
15384 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15385 return ((absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other))
15386 && opcode != 0x4060);
9d862524
MR
15387 case BFD_RELOC_MIPS_21_PCREL_S2:
15388 case BFD_RELOC_MIPS_26_PCREL_S2:
15389 return absolute_p ? val & 1 : ELF_ST_IS_COMPRESSED (other);
15390 case BFD_RELOC_MIPS16_16_PCREL_S1:
15391 return absolute_p ? !(val & 1) : !ELF_ST_IS_MIPS16 (other);
15392 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15393 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
9d862524
MR
15394 return absolute_p ? !(val & 1) : !ELF_ST_IS_MICROMIPS (other);
15395 default:
15396 abort ();
15397 }
15398}
15399
15400/* Return TRUE if the symbol plus addend associated with a regular MIPS
15401 branch instruction pointed to by FIXP is not aligned according to the
15402 branch instruction's immediate field requirement. We need the addend
15403 to preserve the ISA bit and also the sum must not have bit 2 set. We
15404 must explicitly OR in the ISA bit from symbol annotation as the bit
15405 won't be set in the symbol's value then. */
15406
15407static bfd_boolean
15408fix_bad_misaligned_branch_p (fixS *fixP)
15409{
15410 bfd_boolean absolute_p;
15411 asection *symsec;
15412 valueT isa_bit;
15413 valueT val;
15414 valueT off;
15415 int other;
15416
15417 if (!fixP->fx_addsy || S_FORCE_RELOC (fixP->fx_addsy, TRUE))
15418 return FALSE;
15419
15420 symsec = S_GET_SEGMENT (fixP->fx_addsy);
15421 absolute_p = bfd_is_abs_section (symsec);
15422
15423 val = S_GET_VALUE (fixP->fx_addsy);
15424 other = S_GET_OTHER (fixP->fx_addsy);
15425 off = fixP->fx_offset;
15426
15427 isa_bit = absolute_p ? (val + off) & 1 : ELF_ST_IS_COMPRESSED (other);
15428 val |= ELF_ST_IS_COMPRESSED (other);
15429 val += off;
15430 return (val & 0x3) != isa_bit;
15431}
15432
15433/* Make the necessary checks on a regular MIPS branch pointed to by FIXP
15434 and its calculated value VAL. */
15435
15436static void
15437fix_validate_branch (fixS *fixP, valueT val)
15438{
15439 if (fixP->fx_done && (val & 0x3) != 0)
15440 as_bad_where (fixP->fx_file, fixP->fx_line,
15441 _("branch to misaligned address (0x%lx)"),
15442 (long) (val + md_pcrel_from (fixP)));
15443 else if (fix_bad_cross_mode_branch_p (fixP))
15444 as_bad_where (fixP->fx_file, fixP->fx_line,
15445 _("branch to a symbol in another ISA mode"));
15446 else if (fix_bad_misaligned_branch_p (fixP))
15447 as_bad_where (fixP->fx_file, fixP->fx_line,
15448 _("branch to misaligned address (0x%lx)"),
15449 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15450 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x3) != 0)
15451 as_bad_where (fixP->fx_file, fixP->fx_line,
15452 _("cannot encode misaligned addend "
15453 "in the relocatable field (0x%lx)"),
15454 (long) fixP->fx_offset);
15455}
15456
252b5132
RH
15457/* Apply a fixup to the object file. */
15458
94f592af 15459void
55cf6793 15460md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 15461{
4d68580a 15462 char *buf;
b886a2ab 15463 unsigned long insn;
a7ebbfdf 15464 reloc_howto_type *howto;
252b5132 15465
d56a8dda
RS
15466 if (fixP->fx_pcrel)
15467 switch (fixP->fx_r_type)
15468 {
15469 case BFD_RELOC_16_PCREL_S2:
c9775dde 15470 case BFD_RELOC_MIPS16_16_PCREL_S1:
d56a8dda
RS
15471 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15472 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15473 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
15474 case BFD_RELOC_32_PCREL:
7361da2c
AB
15475 case BFD_RELOC_MIPS_21_PCREL_S2:
15476 case BFD_RELOC_MIPS_26_PCREL_S2:
15477 case BFD_RELOC_MIPS_18_PCREL_S3:
15478 case BFD_RELOC_MIPS_19_PCREL_S2:
15479 case BFD_RELOC_HI16_S_PCREL:
15480 case BFD_RELOC_LO16_PCREL:
d56a8dda
RS
15481 break;
15482
15483 case BFD_RELOC_32:
15484 fixP->fx_r_type = BFD_RELOC_32_PCREL;
15485 break;
15486
15487 default:
15488 as_bad_where (fixP->fx_file, fixP->fx_line,
15489 _("PC-relative reference to a different section"));
15490 break;
15491 }
15492
15493 /* Handle BFD_RELOC_8, since it's easy. Punt on other bfd relocations
15494 that have no MIPS ELF equivalent. */
15495 if (fixP->fx_r_type != BFD_RELOC_8)
15496 {
15497 howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
15498 if (!howto)
15499 return;
15500 }
65551fa4 15501
df58fc94
RS
15502 gas_assert (fixP->fx_size == 2
15503 || fixP->fx_size == 4
d56a8dda 15504 || fixP->fx_r_type == BFD_RELOC_8
90ecf173
MR
15505 || fixP->fx_r_type == BFD_RELOC_16
15506 || fixP->fx_r_type == BFD_RELOC_64
15507 || fixP->fx_r_type == BFD_RELOC_CTOR
15508 || fixP->fx_r_type == BFD_RELOC_MIPS_SUB
df58fc94 15509 || fixP->fx_r_type == BFD_RELOC_MICROMIPS_SUB
90ecf173
MR
15510 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
15511 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
2f0c68f2
CM
15512 || fixP->fx_r_type == BFD_RELOC_MIPS_TLS_DTPREL64
15513 || fixP->fx_r_type == BFD_RELOC_NONE);
252b5132 15514
4d68580a 15515 buf = fixP->fx_frag->fr_literal + fixP->fx_where;
252b5132 15516
b1dca8ee
RS
15517 /* Don't treat parts of a composite relocation as done. There are two
15518 reasons for this:
15519
15520 (1) The second and third parts will be against 0 (RSS_UNDEF) but
15521 should nevertheless be emitted if the first part is.
15522
15523 (2) In normal usage, composite relocations are never assembly-time
15524 constants. The easiest way of dealing with the pathological
15525 exceptions is to generate a relocation against STN_UNDEF and
15526 leave everything up to the linker. */
3994f87e 15527 if (fixP->fx_addsy == NULL && !fixP->fx_pcrel && fixP->fx_tcbit == 0)
252b5132
RH
15528 fixP->fx_done = 1;
15529
15530 switch (fixP->fx_r_type)
15531 {
3f98094e
DJ
15532 case BFD_RELOC_MIPS_TLS_GD:
15533 case BFD_RELOC_MIPS_TLS_LDM:
741d6ea8
JM
15534 case BFD_RELOC_MIPS_TLS_DTPREL32:
15535 case BFD_RELOC_MIPS_TLS_DTPREL64:
3f98094e
DJ
15536 case BFD_RELOC_MIPS_TLS_DTPREL_HI16:
15537 case BFD_RELOC_MIPS_TLS_DTPREL_LO16:
15538 case BFD_RELOC_MIPS_TLS_GOTTPREL:
d0f13682
CLT
15539 case BFD_RELOC_MIPS_TLS_TPREL32:
15540 case BFD_RELOC_MIPS_TLS_TPREL64:
3f98094e
DJ
15541 case BFD_RELOC_MIPS_TLS_TPREL_HI16:
15542 case BFD_RELOC_MIPS_TLS_TPREL_LO16:
df58fc94
RS
15543 case BFD_RELOC_MICROMIPS_TLS_GD:
15544 case BFD_RELOC_MICROMIPS_TLS_LDM:
15545 case BFD_RELOC_MICROMIPS_TLS_DTPREL_HI16:
15546 case BFD_RELOC_MICROMIPS_TLS_DTPREL_LO16:
15547 case BFD_RELOC_MICROMIPS_TLS_GOTTPREL:
15548 case BFD_RELOC_MICROMIPS_TLS_TPREL_HI16:
15549 case BFD_RELOC_MICROMIPS_TLS_TPREL_LO16:
d0f13682
CLT
15550 case BFD_RELOC_MIPS16_TLS_GD:
15551 case BFD_RELOC_MIPS16_TLS_LDM:
15552 case BFD_RELOC_MIPS16_TLS_DTPREL_HI16:
15553 case BFD_RELOC_MIPS16_TLS_DTPREL_LO16:
15554 case BFD_RELOC_MIPS16_TLS_GOTTPREL:
15555 case BFD_RELOC_MIPS16_TLS_TPREL_HI16:
15556 case BFD_RELOC_MIPS16_TLS_TPREL_LO16:
4512dafa
MR
15557 if (fixP->fx_addsy)
15558 S_SET_THREAD_LOCAL (fixP->fx_addsy);
15559 else
15560 as_bad_where (fixP->fx_file, fixP->fx_line,
15561 _("TLS relocation against a constant"));
15562 break;
3f98094e 15563
252b5132 15564 case BFD_RELOC_MIPS_JMP:
9d862524
MR
15565 case BFD_RELOC_MIPS16_JMP:
15566 case BFD_RELOC_MICROMIPS_JMP:
15567 {
15568 int shift;
15569
15570 gas_assert (!fixP->fx_done);
15571
15572 /* Shift is 2, unusually, for microMIPS JALX. */
15573 if (fixP->fx_r_type == BFD_RELOC_MICROMIPS_JMP
15574 && (read_compressed_insn (buf, 4) >> 26) != 0x3c)
15575 shift = 1;
15576 else
15577 shift = 2;
15578
15579 if (fix_bad_cross_mode_jump_p (fixP))
15580 as_bad_where (fixP->fx_file, fixP->fx_line,
15581 _("jump to a symbol in another ISA mode"));
15582 else if (fix_bad_same_mode_jalx_p (fixP))
15583 as_bad_where (fixP->fx_file, fixP->fx_line,
15584 _("JALX to a symbol in the same ISA mode"));
15585 else if (fix_bad_misaligned_jump_p (fixP, shift))
15586 as_bad_where (fixP->fx_file, fixP->fx_line,
15587 _("jump to misaligned address (0x%lx)"),
15588 (long) (S_GET_VALUE (fixP->fx_addsy)
15589 + fixP->fx_offset));
15590 else if (HAVE_IN_PLACE_ADDENDS
15591 && (fixP->fx_offset & ((1 << shift) - 1)) != 0)
15592 as_bad_where (fixP->fx_file, fixP->fx_line,
15593 _("cannot encode misaligned addend "
15594 "in the relocatable field (0x%lx)"),
15595 (long) fixP->fx_offset);
15596 }
15597 /* Fall through. */
15598
e369bcce
TS
15599 case BFD_RELOC_MIPS_SHIFT5:
15600 case BFD_RELOC_MIPS_SHIFT6:
15601 case BFD_RELOC_MIPS_GOT_DISP:
15602 case BFD_RELOC_MIPS_GOT_PAGE:
15603 case BFD_RELOC_MIPS_GOT_OFST:
15604 case BFD_RELOC_MIPS_SUB:
15605 case BFD_RELOC_MIPS_INSERT_A:
15606 case BFD_RELOC_MIPS_INSERT_B:
15607 case BFD_RELOC_MIPS_DELETE:
15608 case BFD_RELOC_MIPS_HIGHEST:
15609 case BFD_RELOC_MIPS_HIGHER:
15610 case BFD_RELOC_MIPS_SCN_DISP:
15611 case BFD_RELOC_MIPS_REL16:
15612 case BFD_RELOC_MIPS_RELGOT:
15613 case BFD_RELOC_MIPS_JALR:
252b5132
RH
15614 case BFD_RELOC_HI16:
15615 case BFD_RELOC_HI16_S:
b886a2ab 15616 case BFD_RELOC_LO16:
cdf6fd85 15617 case BFD_RELOC_GPREL16:
252b5132
RH
15618 case BFD_RELOC_MIPS_LITERAL:
15619 case BFD_RELOC_MIPS_CALL16:
15620 case BFD_RELOC_MIPS_GOT16:
cdf6fd85 15621 case BFD_RELOC_GPREL32:
252b5132
RH
15622 case BFD_RELOC_MIPS_GOT_HI16:
15623 case BFD_RELOC_MIPS_GOT_LO16:
15624 case BFD_RELOC_MIPS_CALL_HI16:
15625 case BFD_RELOC_MIPS_CALL_LO16:
41947d9e
MR
15626 case BFD_RELOC_HI16_S_PCREL:
15627 case BFD_RELOC_LO16_PCREL:
252b5132 15628 case BFD_RELOC_MIPS16_GPREL:
738e5348
RS
15629 case BFD_RELOC_MIPS16_GOT16:
15630 case BFD_RELOC_MIPS16_CALL16:
d6f16593
MR
15631 case BFD_RELOC_MIPS16_HI16:
15632 case BFD_RELOC_MIPS16_HI16_S:
b886a2ab 15633 case BFD_RELOC_MIPS16_LO16:
df58fc94
RS
15634 case BFD_RELOC_MICROMIPS_GOT_DISP:
15635 case BFD_RELOC_MICROMIPS_GOT_PAGE:
15636 case BFD_RELOC_MICROMIPS_GOT_OFST:
15637 case BFD_RELOC_MICROMIPS_SUB:
15638 case BFD_RELOC_MICROMIPS_HIGHEST:
15639 case BFD_RELOC_MICROMIPS_HIGHER:
15640 case BFD_RELOC_MICROMIPS_SCN_DISP:
15641 case BFD_RELOC_MICROMIPS_JALR:
15642 case BFD_RELOC_MICROMIPS_HI16:
15643 case BFD_RELOC_MICROMIPS_HI16_S:
b886a2ab 15644 case BFD_RELOC_MICROMIPS_LO16:
df58fc94
RS
15645 case BFD_RELOC_MICROMIPS_GPREL16:
15646 case BFD_RELOC_MICROMIPS_LITERAL:
15647 case BFD_RELOC_MICROMIPS_CALL16:
15648 case BFD_RELOC_MICROMIPS_GOT16:
15649 case BFD_RELOC_MICROMIPS_GOT_HI16:
15650 case BFD_RELOC_MICROMIPS_GOT_LO16:
15651 case BFD_RELOC_MICROMIPS_CALL_HI16:
15652 case BFD_RELOC_MICROMIPS_CALL_LO16:
067ec077 15653 case BFD_RELOC_MIPS_EH:
b886a2ab
RS
15654 if (fixP->fx_done)
15655 {
15656 offsetT value;
15657
15658 if (calculate_reloc (fixP->fx_r_type, *valP, &value))
15659 {
15660 insn = read_reloc_insn (buf, fixP->fx_r_type);
15661 if (mips16_reloc_p (fixP->fx_r_type))
15662 insn |= mips16_immed_extend (value, 16);
15663 else
15664 insn |= (value & 0xffff);
15665 write_reloc_insn (buf, fixP->fx_r_type, insn);
15666 }
15667 else
15668 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15669 _("unsupported constant in relocation"));
b886a2ab 15670 }
252b5132
RH
15671 break;
15672
252b5132
RH
15673 case BFD_RELOC_64:
15674 /* This is handled like BFD_RELOC_32, but we output a sign
15675 extended value if we are only 32 bits. */
3e722fb5 15676 if (fixP->fx_done)
252b5132
RH
15677 {
15678 if (8 <= sizeof (valueT))
4d68580a 15679 md_number_to_chars (buf, *valP, 8);
252b5132
RH
15680 else
15681 {
a7ebbfdf 15682 valueT hiv;
252b5132 15683
a7ebbfdf 15684 if ((*valP & 0x80000000) != 0)
252b5132
RH
15685 hiv = 0xffffffff;
15686 else
15687 hiv = 0;
4d68580a
RS
15688 md_number_to_chars (buf + (target_big_endian ? 4 : 0), *valP, 4);
15689 md_number_to_chars (buf + (target_big_endian ? 0 : 4), hiv, 4);
252b5132
RH
15690 }
15691 }
15692 break;
15693
056350c6 15694 case BFD_RELOC_RVA:
252b5132 15695 case BFD_RELOC_32:
b47468a6 15696 case BFD_RELOC_32_PCREL:
252b5132 15697 case BFD_RELOC_16:
d56a8dda 15698 case BFD_RELOC_8:
252b5132 15699 /* If we are deleting this reloc entry, we must fill in the
54f4ddb3
TS
15700 value now. This can happen if we have a .word which is not
15701 resolved when it appears but is later defined. */
252b5132 15702 if (fixP->fx_done)
4d68580a 15703 md_number_to_chars (buf, *valP, fixP->fx_size);
252b5132
RH
15704 break;
15705
7361da2c 15706 case BFD_RELOC_MIPS_21_PCREL_S2:
9d862524 15707 fix_validate_branch (fixP, *valP);
41947d9e
MR
15708 if (!fixP->fx_done)
15709 break;
15710
15711 if (*valP + 0x400000 <= 0x7fffff)
15712 {
15713 insn = read_insn (buf);
15714 insn |= (*valP >> 2) & 0x1fffff;
15715 write_insn (buf, insn);
15716 }
15717 else
15718 as_bad_where (fixP->fx_file, fixP->fx_line,
15719 _("branch out of range"));
15720 break;
15721
7361da2c 15722 case BFD_RELOC_MIPS_26_PCREL_S2:
9d862524 15723 fix_validate_branch (fixP, *valP);
41947d9e
MR
15724 if (!fixP->fx_done)
15725 break;
7361da2c 15726
41947d9e
MR
15727 if (*valP + 0x8000000 <= 0xfffffff)
15728 {
15729 insn = read_insn (buf);
15730 insn |= (*valP >> 2) & 0x3ffffff;
15731 write_insn (buf, insn);
15732 }
15733 else
15734 as_bad_where (fixP->fx_file, fixP->fx_line,
15735 _("branch out of range"));
7361da2c
AB
15736 break;
15737
15738 case BFD_RELOC_MIPS_18_PCREL_S3:
717ba204 15739 if (fixP->fx_addsy && (S_GET_VALUE (fixP->fx_addsy) & 0x7) != 0)
7361da2c 15740 as_bad_where (fixP->fx_file, fixP->fx_line,
0866e94c
MF
15741 _("PC-relative access using misaligned symbol (%lx)"),
15742 (long) S_GET_VALUE (fixP->fx_addsy));
15743 if ((fixP->fx_offset & 0x7) != 0)
15744 as_bad_where (fixP->fx_file, fixP->fx_line,
15745 _("PC-relative access using misaligned offset (%lx)"),
15746 (long) fixP->fx_offset);
41947d9e
MR
15747 if (!fixP->fx_done)
15748 break;
7361da2c 15749
41947d9e
MR
15750 if (*valP + 0x100000 <= 0x1fffff)
15751 {
15752 insn = read_insn (buf);
15753 insn |= (*valP >> 3) & 0x3ffff;
15754 write_insn (buf, insn);
15755 }
15756 else
15757 as_bad_where (fixP->fx_file, fixP->fx_line,
15758 _("PC-relative access out of range"));
7361da2c
AB
15759 break;
15760
15761 case BFD_RELOC_MIPS_19_PCREL_S2:
15762 if ((*valP & 0x3) != 0)
15763 as_bad_where (fixP->fx_file, fixP->fx_line,
15764 _("PC-relative access to misaligned address (%lx)"),
717ba204 15765 (long) *valP);
41947d9e
MR
15766 if (!fixP->fx_done)
15767 break;
7361da2c 15768
41947d9e
MR
15769 if (*valP + 0x100000 <= 0x1fffff)
15770 {
15771 insn = read_insn (buf);
15772 insn |= (*valP >> 2) & 0x7ffff;
15773 write_insn (buf, insn);
15774 }
15775 else
15776 as_bad_where (fixP->fx_file, fixP->fx_line,
15777 _("PC-relative access out of range"));
7361da2c
AB
15778 break;
15779
252b5132 15780 case BFD_RELOC_16_PCREL_S2:
9d862524 15781 fix_validate_branch (fixP, *valP);
cb56d3d3 15782
54f4ddb3
TS
15783 /* We need to save the bits in the instruction since fixup_segment()
15784 might be deleting the relocation entry (i.e., a branch within
15785 the current segment). */
a7ebbfdf 15786 if (! fixP->fx_done)
bb2d6cd7 15787 break;
252b5132 15788
54f4ddb3 15789 /* Update old instruction data. */
4d68580a 15790 insn = read_insn (buf);
252b5132 15791
a7ebbfdf
TS
15792 if (*valP + 0x20000 <= 0x3ffff)
15793 {
15794 insn |= (*valP >> 2) & 0xffff;
4d68580a 15795 write_insn (buf, insn);
a7ebbfdf 15796 }
ce8ad872 15797 else if (fixP->fx_tcbit2
a7ebbfdf
TS
15798 && fixP->fx_done
15799 && fixP->fx_frag->fr_address >= text_section->vma
15800 && (fixP->fx_frag->fr_address
587aac4e 15801 < text_section->vma + bfd_get_section_size (text_section))
a7ebbfdf
TS
15802 && ((insn & 0xffff0000) == 0x10000000 /* beq $0,$0 */
15803 || (insn & 0xffff0000) == 0x04010000 /* bgez $0 */
15804 || (insn & 0xffff0000) == 0x04110000)) /* bgezal $0 */
252b5132
RH
15805 {
15806 /* The branch offset is too large. If this is an
15807 unconditional branch, and we are not generating PIC code,
15808 we can convert it to an absolute jump instruction. */
a7ebbfdf
TS
15809 if ((insn & 0xffff0000) == 0x04110000) /* bgezal $0 */
15810 insn = 0x0c000000; /* jal */
252b5132 15811 else
a7ebbfdf
TS
15812 insn = 0x08000000; /* j */
15813 fixP->fx_r_type = BFD_RELOC_MIPS_JMP;
15814 fixP->fx_done = 0;
15815 fixP->fx_addsy = section_symbol (text_section);
15816 *valP += md_pcrel_from (fixP);
4d68580a 15817 write_insn (buf, insn);
a7ebbfdf
TS
15818 }
15819 else
15820 {
15821 /* If we got here, we have branch-relaxation disabled,
15822 and there's nothing we can do to fix this instruction
15823 without turning it into a longer sequence. */
15824 as_bad_where (fixP->fx_file, fixP->fx_line,
1661c76c 15825 _("branch out of range"));
252b5132 15826 }
252b5132
RH
15827 break;
15828
c9775dde 15829 case BFD_RELOC_MIPS16_16_PCREL_S1:
df58fc94
RS
15830 case BFD_RELOC_MICROMIPS_7_PCREL_S1:
15831 case BFD_RELOC_MICROMIPS_10_PCREL_S1:
15832 case BFD_RELOC_MICROMIPS_16_PCREL_S1:
96e9ba5f 15833 gas_assert (!fixP->fx_done);
9d862524
MR
15834 if (fix_bad_cross_mode_branch_p (fixP))
15835 as_bad_where (fixP->fx_file, fixP->fx_line,
15836 _("branch to a symbol in another ISA mode"));
15837 else if (fixP->fx_addsy
15838 && !S_FORCE_RELOC (fixP->fx_addsy, TRUE)
15839 && !bfd_is_abs_section (S_GET_SEGMENT (fixP->fx_addsy))
15840 && (fixP->fx_offset & 0x1) != 0)
15841 as_bad_where (fixP->fx_file, fixP->fx_line,
15842 _("branch to misaligned address (0x%lx)"),
15843 (long) (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset));
15844 else if (HAVE_IN_PLACE_ADDENDS && (fixP->fx_offset & 0x1) != 0)
15845 as_bad_where (fixP->fx_file, fixP->fx_line,
15846 _("cannot encode misaligned addend "
15847 "in the relocatable field (0x%lx)"),
15848 (long) fixP->fx_offset);
df58fc94
RS
15849 break;
15850
252b5132
RH
15851 case BFD_RELOC_VTABLE_INHERIT:
15852 fixP->fx_done = 0;
15853 if (fixP->fx_addsy
15854 && !S_IS_DEFINED (fixP->fx_addsy)
15855 && !S_IS_WEAK (fixP->fx_addsy))
15856 S_SET_WEAK (fixP->fx_addsy);
15857 break;
15858
2f0c68f2 15859 case BFD_RELOC_NONE:
252b5132
RH
15860 case BFD_RELOC_VTABLE_ENTRY:
15861 fixP->fx_done = 0;
15862 break;
15863
15864 default:
b37df7c4 15865 abort ();
252b5132 15866 }
a7ebbfdf
TS
15867
15868 /* Remember value for tc_gen_reloc. */
15869 fixP->fx_addnumber = *valP;
252b5132
RH
15870}
15871
252b5132 15872static symbolS *
17a2f251 15873get_symbol (void)
252b5132
RH
15874{
15875 int c;
15876 char *name;
15877 symbolS *p;
15878
d02603dc 15879 c = get_symbol_name (&name);
252b5132 15880 p = (symbolS *) symbol_find_or_make (name);
d02603dc 15881 (void) restore_line_pointer (c);
252b5132
RH
15882 return p;
15883}
15884
742a56fe
RS
15885/* Align the current frag to a given power of two. If a particular
15886 fill byte should be used, FILL points to an integer that contains
15887 that byte, otherwise FILL is null.
15888
462427c4
RS
15889 This function used to have the comment:
15890
15891 The MIPS assembler also automatically adjusts any preceding label.
15892
15893 The implementation therefore applied the adjustment to a maximum of
15894 one label. However, other label adjustments are applied to batches
15895 of labels, and adjusting just one caused problems when new labels
15896 were added for the sake of debugging or unwind information.
15897 We therefore adjust all preceding labels (given as LABELS) instead. */
252b5132
RH
15898
15899static void
462427c4 15900mips_align (int to, int *fill, struct insn_label_list *labels)
252b5132 15901{
7d10b47d 15902 mips_emit_delays ();
df58fc94 15903 mips_record_compressed_mode ();
742a56fe
RS
15904 if (fill == NULL && subseg_text_p (now_seg))
15905 frag_align_code (to, 0);
15906 else
15907 frag_align (to, fill ? *fill : 0, 0);
252b5132 15908 record_alignment (now_seg, to);
462427c4 15909 mips_move_labels (labels, FALSE);
252b5132
RH
15910}
15911
15912/* Align to a given power of two. .align 0 turns off the automatic
15913 alignment used by the data creating pseudo-ops. */
15914
15915static void
17a2f251 15916s_align (int x ATTRIBUTE_UNUSED)
252b5132 15917{
742a56fe 15918 int temp, fill_value, *fill_ptr;
49954fb4 15919 long max_alignment = 28;
252b5132 15920
54f4ddb3 15921 /* o Note that the assembler pulls down any immediately preceding label
252b5132 15922 to the aligned address.
54f4ddb3 15923 o It's not documented but auto alignment is reinstated by
252b5132 15924 a .align pseudo instruction.
54f4ddb3 15925 o Note also that after auto alignment is turned off the mips assembler
252b5132 15926 issues an error on attempt to assemble an improperly aligned data item.
54f4ddb3 15927 We don't. */
252b5132
RH
15928
15929 temp = get_absolute_expression ();
15930 if (temp > max_alignment)
1661c76c 15931 as_bad (_("alignment too large, %d assumed"), temp = max_alignment);
252b5132
RH
15932 else if (temp < 0)
15933 {
1661c76c 15934 as_warn (_("alignment negative, 0 assumed"));
252b5132
RH
15935 temp = 0;
15936 }
15937 if (*input_line_pointer == ',')
15938 {
f9419b05 15939 ++input_line_pointer;
742a56fe
RS
15940 fill_value = get_absolute_expression ();
15941 fill_ptr = &fill_value;
252b5132
RH
15942 }
15943 else
742a56fe 15944 fill_ptr = 0;
252b5132
RH
15945 if (temp)
15946 {
a8dbcb85
TS
15947 segment_info_type *si = seg_info (now_seg);
15948 struct insn_label_list *l = si->label_list;
54f4ddb3 15949 /* Auto alignment should be switched on by next section change. */
252b5132 15950 auto_align = 1;
462427c4 15951 mips_align (temp, fill_ptr, l);
252b5132
RH
15952 }
15953 else
15954 {
15955 auto_align = 0;
15956 }
15957
15958 demand_empty_rest_of_line ();
15959}
15960
252b5132 15961static void
17a2f251 15962s_change_sec (int sec)
252b5132
RH
15963{
15964 segT seg;
15965
252b5132
RH
15966 /* The ELF backend needs to know that we are changing sections, so
15967 that .previous works correctly. We could do something like check
b6ff326e 15968 for an obj_section_change_hook macro, but that might be confusing
252b5132
RH
15969 as it would not be appropriate to use it in the section changing
15970 functions in read.c, since obj-elf.c intercepts those. FIXME:
15971 This should be cleaner, somehow. */
f3ded42a 15972 obj_elf_section_change_hook ();
252b5132 15973
7d10b47d 15974 mips_emit_delays ();
6a32d874 15975
252b5132
RH
15976 switch (sec)
15977 {
15978 case 't':
15979 s_text (0);
15980 break;
15981 case 'd':
15982 s_data (0);
15983 break;
15984 case 'b':
15985 subseg_set (bss_section, (subsegT) get_absolute_expression ());
15986 demand_empty_rest_of_line ();
15987 break;
15988
15989 case 'r':
4d0d148d
TS
15990 seg = subseg_new (RDATA_SECTION_NAME,
15991 (subsegT) get_absolute_expression ());
f3ded42a
RS
15992 bfd_set_section_flags (stdoutput, seg, (SEC_ALLOC | SEC_LOAD
15993 | SEC_READONLY | SEC_RELOC
15994 | SEC_DATA));
15995 if (strncmp (TARGET_OS, "elf", 3) != 0)
15996 record_alignment (seg, 4);
4d0d148d 15997 demand_empty_rest_of_line ();
252b5132
RH
15998 break;
15999
16000 case 's':
4d0d148d 16001 seg = subseg_new (".sdata", (subsegT) get_absolute_expression ());
f3ded42a
RS
16002 bfd_set_section_flags (stdoutput, seg,
16003 SEC_ALLOC | SEC_LOAD | SEC_RELOC | SEC_DATA);
16004 if (strncmp (TARGET_OS, "elf", 3) != 0)
16005 record_alignment (seg, 4);
4d0d148d
TS
16006 demand_empty_rest_of_line ();
16007 break;
998b3c36
MR
16008
16009 case 'B':
16010 seg = subseg_new (".sbss", (subsegT) get_absolute_expression ());
f3ded42a
RS
16011 bfd_set_section_flags (stdoutput, seg, SEC_ALLOC);
16012 if (strncmp (TARGET_OS, "elf", 3) != 0)
16013 record_alignment (seg, 4);
998b3c36
MR
16014 demand_empty_rest_of_line ();
16015 break;
252b5132
RH
16016 }
16017
16018 auto_align = 1;
16019}
b34976b6 16020
cca86cc8 16021void
17a2f251 16022s_change_section (int ignore ATTRIBUTE_UNUSED)
cca86cc8 16023{
d02603dc 16024 char *saved_ilp;
cca86cc8 16025 char *section_name;
d02603dc 16026 char c, endc;
684022ea 16027 char next_c = 0;
cca86cc8
SC
16028 int section_type;
16029 int section_flag;
16030 int section_entry_size;
16031 int section_alignment;
b34976b6 16032
d02603dc
NC
16033 saved_ilp = input_line_pointer;
16034 endc = get_symbol_name (&section_name);
16035 c = (endc == '"' ? input_line_pointer[1] : endc);
a816d1ed 16036 if (c)
d02603dc 16037 next_c = input_line_pointer [(endc == '"' ? 2 : 1)];
cca86cc8 16038
4cf0dd0d
TS
16039 /* Do we have .section Name<,"flags">? */
16040 if (c != ',' || (c == ',' && next_c == '"'))
cca86cc8 16041 {
d02603dc
NC
16042 /* Just after name is now '\0'. */
16043 (void) restore_line_pointer (endc);
16044 input_line_pointer = saved_ilp;
cca86cc8
SC
16045 obj_elf_section (ignore);
16046 return;
16047 }
d02603dc
NC
16048
16049 section_name = xstrdup (section_name);
16050 c = restore_line_pointer (endc);
16051
cca86cc8
SC
16052 input_line_pointer++;
16053
16054 /* Do we have .section Name<,type><,flag><,entry_size><,alignment> */
16055 if (c == ',')
16056 section_type = get_absolute_expression ();
16057 else
16058 section_type = 0;
d02603dc 16059
cca86cc8
SC
16060 if (*input_line_pointer++ == ',')
16061 section_flag = get_absolute_expression ();
16062 else
16063 section_flag = 0;
d02603dc 16064
cca86cc8
SC
16065 if (*input_line_pointer++ == ',')
16066 section_entry_size = get_absolute_expression ();
16067 else
16068 section_entry_size = 0;
d02603dc 16069
cca86cc8
SC
16070 if (*input_line_pointer++ == ',')
16071 section_alignment = get_absolute_expression ();
16072 else
16073 section_alignment = 0;
d02603dc 16074
87975d2a
AM
16075 /* FIXME: really ignore? */
16076 (void) section_alignment;
cca86cc8 16077
8ab8a5c8
RS
16078 /* When using the generic form of .section (as implemented by obj-elf.c),
16079 there's no way to set the section type to SHT_MIPS_DWARF. Users have
16080 traditionally had to fall back on the more common @progbits instead.
16081
16082 There's nothing really harmful in this, since bfd will correct
16083 SHT_PROGBITS to SHT_MIPS_DWARF before writing out the file. But it
708587a4 16084 means that, for backwards compatibility, the special_section entries
8ab8a5c8
RS
16085 for dwarf sections must use SHT_PROGBITS rather than SHT_MIPS_DWARF.
16086
16087 Even so, we shouldn't force users of the MIPS .section syntax to
16088 incorrectly label the sections as SHT_PROGBITS. The best compromise
16089 seems to be to map SHT_MIPS_DWARF to SHT_PROGBITS before calling the
16090 generic type-checking code. */
16091 if (section_type == SHT_MIPS_DWARF)
16092 section_type = SHT_PROGBITS;
16093
a91e1603 16094 obj_elf_change_section (section_name, section_type, 0, section_flag,
cca86cc8 16095 section_entry_size, 0, 0, 0);
a816d1ed
AO
16096
16097 if (now_seg->name != section_name)
16098 free (section_name);
cca86cc8 16099}
252b5132
RH
16100
16101void
17a2f251 16102mips_enable_auto_align (void)
252b5132
RH
16103{
16104 auto_align = 1;
16105}
16106
16107static void
17a2f251 16108s_cons (int log_size)
252b5132 16109{
a8dbcb85
TS
16110 segment_info_type *si = seg_info (now_seg);
16111 struct insn_label_list *l = si->label_list;
252b5132 16112
7d10b47d 16113 mips_emit_delays ();
252b5132 16114 if (log_size > 0 && auto_align)
462427c4 16115 mips_align (log_size, 0, l);
252b5132 16116 cons (1 << log_size);
a1facbec 16117 mips_clear_insn_labels ();
252b5132
RH
16118}
16119
16120static void
17a2f251 16121s_float_cons (int type)
252b5132 16122{
a8dbcb85
TS
16123 segment_info_type *si = seg_info (now_seg);
16124 struct insn_label_list *l = si->label_list;
252b5132 16125
7d10b47d 16126 mips_emit_delays ();
252b5132
RH
16127
16128 if (auto_align)
49309057
ILT
16129 {
16130 if (type == 'd')
462427c4 16131 mips_align (3, 0, l);
49309057 16132 else
462427c4 16133 mips_align (2, 0, l);
49309057 16134 }
252b5132 16135
252b5132 16136 float_cons (type);
a1facbec 16137 mips_clear_insn_labels ();
252b5132
RH
16138}
16139
16140/* Handle .globl. We need to override it because on Irix 5 you are
16141 permitted to say
16142 .globl foo .text
16143 where foo is an undefined symbol, to mean that foo should be
16144 considered to be the address of a function. */
16145
16146static void
17a2f251 16147s_mips_globl (int x ATTRIBUTE_UNUSED)
252b5132
RH
16148{
16149 char *name;
16150 int c;
16151 symbolS *symbolP;
16152 flagword flag;
16153
8a06b769 16154 do
252b5132 16155 {
d02603dc 16156 c = get_symbol_name (&name);
8a06b769
TS
16157 symbolP = symbol_find_or_make (name);
16158 S_SET_EXTERNAL (symbolP);
16159
252b5132 16160 *input_line_pointer = c;
d02603dc 16161 SKIP_WHITESPACE_AFTER_NAME ();
252b5132 16162
8a06b769
TS
16163 /* On Irix 5, every global symbol that is not explicitly labelled as
16164 being a function is apparently labelled as being an object. */
16165 flag = BSF_OBJECT;
252b5132 16166
8a06b769
TS
16167 if (!is_end_of_line[(unsigned char) *input_line_pointer]
16168 && (*input_line_pointer != ','))
16169 {
16170 char *secname;
16171 asection *sec;
16172
d02603dc 16173 c = get_symbol_name (&secname);
8a06b769
TS
16174 sec = bfd_get_section_by_name (stdoutput, secname);
16175 if (sec == NULL)
16176 as_bad (_("%s: no such section"), secname);
d02603dc 16177 (void) restore_line_pointer (c);
8a06b769
TS
16178
16179 if (sec != NULL && (sec->flags & SEC_CODE) != 0)
16180 flag = BSF_FUNCTION;
16181 }
16182
16183 symbol_get_bfdsym (symbolP)->flags |= flag;
16184
16185 c = *input_line_pointer;
16186 if (c == ',')
16187 {
16188 input_line_pointer++;
16189 SKIP_WHITESPACE ();
16190 if (is_end_of_line[(unsigned char) *input_line_pointer])
16191 c = '\n';
16192 }
16193 }
16194 while (c == ',');
252b5132 16195
252b5132
RH
16196 demand_empty_rest_of_line ();
16197}
16198
16199static void
17a2f251 16200s_option (int x ATTRIBUTE_UNUSED)
252b5132
RH
16201{
16202 char *opt;
16203 char c;
16204
d02603dc 16205 c = get_symbol_name (&opt);
252b5132
RH
16206
16207 if (*opt == 'O')
16208 {
16209 /* FIXME: What does this mean? */
16210 }
41a1578e 16211 else if (strncmp (opt, "pic", 3) == 0 && ISDIGIT (opt[3]) && opt[4] == '\0')
252b5132
RH
16212 {
16213 int i;
16214
16215 i = atoi (opt + 3);
668c5ebc
MR
16216 if (i != 0 && i != 2)
16217 as_bad (_(".option pic%d not supported"), i);
16218 else if (mips_pic == VXWORKS_PIC)
16219 as_bad (_(".option pic%d not supported in VxWorks PIC mode"), i);
16220 else if (i == 0)
252b5132
RH
16221 mips_pic = NO_PIC;
16222 else if (i == 2)
143d77c5 16223 {
8b828383 16224 mips_pic = SVR4_PIC;
143d77c5
EC
16225 mips_abicalls = TRUE;
16226 }
252b5132 16227
4d0d148d 16228 if (mips_pic == SVR4_PIC)
252b5132
RH
16229 {
16230 if (g_switch_seen && g_switch_value != 0)
16231 as_warn (_("-G may not be used with SVR4 PIC code"));
16232 g_switch_value = 0;
16233 bfd_set_gp_size (stdoutput, 0);
16234 }
16235 }
16236 else
1661c76c 16237 as_warn (_("unrecognized option \"%s\""), opt);
252b5132 16238
d02603dc 16239 (void) restore_line_pointer (c);
252b5132
RH
16240 demand_empty_rest_of_line ();
16241}
16242
16243/* This structure is used to hold a stack of .set values. */
16244
e972090a
NC
16245struct mips_option_stack
16246{
252b5132
RH
16247 struct mips_option_stack *next;
16248 struct mips_set_options options;
16249};
16250
16251static struct mips_option_stack *mips_opts_stack;
16252
22522f88
MR
16253/* Return status for .set/.module option handling. */
16254
16255enum code_option_type
16256{
16257 /* Unrecognized option. */
16258 OPTION_TYPE_BAD = -1,
16259
16260 /* Ordinary option. */
16261 OPTION_TYPE_NORMAL,
16262
16263 /* ISA changing option. */
16264 OPTION_TYPE_ISA
16265};
16266
16267/* Handle common .set/.module options. Return status indicating option
16268 type. */
16269
16270static enum code_option_type
919731af 16271parse_code_option (char * name)
252b5132 16272{
22522f88 16273 bfd_boolean isa_set = FALSE;
c6278170 16274 const struct mips_ase *ase;
22522f88 16275
919731af 16276 if (strncmp (name, "at=", 3) == 0)
741fe287
MR
16277 {
16278 char *s = name + 3;
16279
16280 if (!reg_lookup (&s, RTYPE_NUM | RTYPE_GP, &mips_opts.at))
1661c76c 16281 as_bad (_("unrecognized register name `%s'"), s);
741fe287 16282 }
252b5132 16283 else if (strcmp (name, "at") == 0)
919731af 16284 mips_opts.at = ATREG;
252b5132 16285 else if (strcmp (name, "noat") == 0)
919731af 16286 mips_opts.at = ZERO;
252b5132 16287 else if (strcmp (name, "move") == 0 || strcmp (name, "novolatile") == 0)
919731af 16288 mips_opts.nomove = 0;
252b5132 16289 else if (strcmp (name, "nomove") == 0 || strcmp (name, "volatile") == 0)
919731af 16290 mips_opts.nomove = 1;
252b5132 16291 else if (strcmp (name, "bopt") == 0)
919731af 16292 mips_opts.nobopt = 0;
252b5132 16293 else if (strcmp (name, "nobopt") == 0)
919731af 16294 mips_opts.nobopt = 1;
ad3fea08 16295 else if (strcmp (name, "gp=32") == 0)
bad1aba3 16296 mips_opts.gp = 32;
ad3fea08 16297 else if (strcmp (name, "gp=64") == 0)
919731af 16298 mips_opts.gp = 64;
ad3fea08 16299 else if (strcmp (name, "fp=32") == 0)
0b35dfee 16300 mips_opts.fp = 32;
351cdf24
MF
16301 else if (strcmp (name, "fp=xx") == 0)
16302 mips_opts.fp = 0;
ad3fea08 16303 else if (strcmp (name, "fp=64") == 0)
919731af 16304 mips_opts.fp = 64;
037b32b9
AN
16305 else if (strcmp (name, "softfloat") == 0)
16306 mips_opts.soft_float = 1;
16307 else if (strcmp (name, "hardfloat") == 0)
16308 mips_opts.soft_float = 0;
16309 else if (strcmp (name, "singlefloat") == 0)
16310 mips_opts.single_float = 1;
16311 else if (strcmp (name, "doublefloat") == 0)
16312 mips_opts.single_float = 0;
351cdf24
MF
16313 else if (strcmp (name, "nooddspreg") == 0)
16314 mips_opts.oddspreg = 0;
16315 else if (strcmp (name, "oddspreg") == 0)
16316 mips_opts.oddspreg = 1;
252b5132
RH
16317 else if (strcmp (name, "mips16") == 0
16318 || strcmp (name, "MIPS-16") == 0)
919731af 16319 mips_opts.mips16 = 1;
252b5132
RH
16320 else if (strcmp (name, "nomips16") == 0
16321 || strcmp (name, "noMIPS-16") == 0)
16322 mips_opts.mips16 = 0;
df58fc94 16323 else if (strcmp (name, "micromips") == 0)
919731af 16324 mips_opts.micromips = 1;
df58fc94
RS
16325 else if (strcmp (name, "nomicromips") == 0)
16326 mips_opts.micromips = 0;
c6278170
RS
16327 else if (name[0] == 'n'
16328 && name[1] == 'o'
16329 && (ase = mips_lookup_ase (name + 2)))
919731af 16330 mips_set_ase (ase, &mips_opts, FALSE);
c6278170 16331 else if ((ase = mips_lookup_ase (name)))
919731af 16332 mips_set_ase (ase, &mips_opts, TRUE);
1a2c1fad 16333 else if (strncmp (name, "mips", 4) == 0 || strncmp (name, "arch=", 5) == 0)
252b5132 16334 {
1a2c1fad
CD
16335 /* Permit the user to change the ISA and architecture on the fly.
16336 Needless to say, misuse can cause serious problems. */
919731af 16337 if (strncmp (name, "arch=", 5) == 0)
1a2c1fad
CD
16338 {
16339 const struct mips_cpu_info *p;
16340
919731af 16341 p = mips_parse_cpu ("internal use", name + 5);
1a2c1fad
CD
16342 if (!p)
16343 as_bad (_("unknown architecture %s"), name + 5);
16344 else
16345 {
16346 mips_opts.arch = p->cpu;
16347 mips_opts.isa = p->isa;
22522f88 16348 isa_set = TRUE;
1a2c1fad
CD
16349 }
16350 }
81a21e38
TS
16351 else if (strncmp (name, "mips", 4) == 0)
16352 {
16353 const struct mips_cpu_info *p;
16354
919731af 16355 p = mips_parse_cpu ("internal use", name);
81a21e38
TS
16356 if (!p)
16357 as_bad (_("unknown ISA level %s"), name + 4);
16358 else
16359 {
16360 mips_opts.arch = p->cpu;
16361 mips_opts.isa = p->isa;
22522f88 16362 isa_set = TRUE;
81a21e38
TS
16363 }
16364 }
af7ee8bf 16365 else
81a21e38 16366 as_bad (_("unknown ISA or architecture %s"), name);
252b5132
RH
16367 }
16368 else if (strcmp (name, "autoextend") == 0)
16369 mips_opts.noautoextend = 0;
16370 else if (strcmp (name, "noautoextend") == 0)
16371 mips_opts.noautoextend = 1;
833794fc
MR
16372 else if (strcmp (name, "insn32") == 0)
16373 mips_opts.insn32 = TRUE;
16374 else if (strcmp (name, "noinsn32") == 0)
16375 mips_opts.insn32 = FALSE;
919731af 16376 else if (strcmp (name, "sym32") == 0)
16377 mips_opts.sym32 = TRUE;
16378 else if (strcmp (name, "nosym32") == 0)
16379 mips_opts.sym32 = FALSE;
16380 else
22522f88
MR
16381 return OPTION_TYPE_BAD;
16382
16383 return isa_set ? OPTION_TYPE_ISA : OPTION_TYPE_NORMAL;
919731af 16384}
16385
16386/* Handle the .set pseudo-op. */
16387
16388static void
16389s_mipsset (int x ATTRIBUTE_UNUSED)
16390{
22522f88 16391 enum code_option_type type = OPTION_TYPE_NORMAL;
919731af 16392 char *name = input_line_pointer, ch;
919731af 16393
16394 file_mips_check_options ();
16395
16396 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16397 ++input_line_pointer;
16398 ch = *input_line_pointer;
16399 *input_line_pointer = '\0';
16400
16401 if (strchr (name, ','))
16402 {
16403 /* Generic ".set" directive; use the generic handler. */
16404 *input_line_pointer = ch;
16405 input_line_pointer = name;
16406 s_set (0);
16407 return;
16408 }
16409
16410 if (strcmp (name, "reorder") == 0)
16411 {
16412 if (mips_opts.noreorder)
16413 end_noreorder ();
16414 }
16415 else if (strcmp (name, "noreorder") == 0)
16416 {
16417 if (!mips_opts.noreorder)
16418 start_noreorder ();
16419 }
16420 else if (strcmp (name, "macro") == 0)
16421 mips_opts.warn_about_macros = 0;
16422 else if (strcmp (name, "nomacro") == 0)
16423 {
16424 if (mips_opts.noreorder == 0)
16425 as_bad (_("`noreorder' must be set before `nomacro'"));
16426 mips_opts.warn_about_macros = 1;
16427 }
16428 else if (strcmp (name, "gp=default") == 0)
16429 mips_opts.gp = file_mips_opts.gp;
16430 else if (strcmp (name, "fp=default") == 0)
16431 mips_opts.fp = file_mips_opts.fp;
16432 else if (strcmp (name, "mips0") == 0 || strcmp (name, "arch=default") == 0)
16433 {
16434 mips_opts.isa = file_mips_opts.isa;
16435 mips_opts.arch = file_mips_opts.arch;
16436 mips_opts.gp = file_mips_opts.gp;
16437 mips_opts.fp = file_mips_opts.fp;
16438 }
252b5132
RH
16439 else if (strcmp (name, "push") == 0)
16440 {
16441 struct mips_option_stack *s;
16442
325801bd 16443 s = XNEW (struct mips_option_stack);
252b5132
RH
16444 s->next = mips_opts_stack;
16445 s->options = mips_opts;
16446 mips_opts_stack = s;
16447 }
16448 else if (strcmp (name, "pop") == 0)
16449 {
16450 struct mips_option_stack *s;
16451
16452 s = mips_opts_stack;
16453 if (s == NULL)
16454 as_bad (_(".set pop with no .set push"));
16455 else
16456 {
16457 /* If we're changing the reorder mode we need to handle
16458 delay slots correctly. */
16459 if (s->options.noreorder && ! mips_opts.noreorder)
7d10b47d 16460 start_noreorder ();
252b5132 16461 else if (! s->options.noreorder && mips_opts.noreorder)
7d10b47d 16462 end_noreorder ();
252b5132
RH
16463
16464 mips_opts = s->options;
16465 mips_opts_stack = s->next;
16466 free (s);
16467 }
16468 }
22522f88
MR
16469 else
16470 {
16471 type = parse_code_option (name);
16472 if (type == OPTION_TYPE_BAD)
16473 as_warn (_("tried to set unrecognized symbol: %s\n"), name);
16474 }
919731af 16475
16476 /* The use of .set [arch|cpu]= historically 'fixes' the width of gp and fp
16477 registers based on what is supported by the arch/cpu. */
22522f88 16478 if (type == OPTION_TYPE_ISA)
e6559e01 16479 {
919731af 16480 switch (mips_opts.isa)
16481 {
16482 case 0:
16483 break;
16484 case ISA_MIPS1:
351cdf24
MF
16485 /* MIPS I cannot support FPXX. */
16486 mips_opts.fp = 32;
16487 /* fall-through. */
919731af 16488 case ISA_MIPS2:
16489 case ISA_MIPS32:
16490 case ISA_MIPS32R2:
16491 case ISA_MIPS32R3:
16492 case ISA_MIPS32R5:
16493 mips_opts.gp = 32;
351cdf24
MF
16494 if (mips_opts.fp != 0)
16495 mips_opts.fp = 32;
919731af 16496 break;
7361da2c
AB
16497 case ISA_MIPS32R6:
16498 mips_opts.gp = 32;
16499 mips_opts.fp = 64;
16500 break;
919731af 16501 case ISA_MIPS3:
16502 case ISA_MIPS4:
16503 case ISA_MIPS5:
16504 case ISA_MIPS64:
16505 case ISA_MIPS64R2:
16506 case ISA_MIPS64R3:
16507 case ISA_MIPS64R5:
7361da2c 16508 case ISA_MIPS64R6:
919731af 16509 mips_opts.gp = 64;
351cdf24
MF
16510 if (mips_opts.fp != 0)
16511 {
16512 if (mips_opts.arch == CPU_R5900)
16513 mips_opts.fp = 32;
16514 else
16515 mips_opts.fp = 64;
16516 }
919731af 16517 break;
16518 default:
16519 as_bad (_("unknown ISA level %s"), name + 4);
16520 break;
16521 }
e6559e01 16522 }
919731af 16523
16524 mips_check_options (&mips_opts, FALSE);
16525
16526 mips_check_isa_supports_ases ();
16527 *input_line_pointer = ch;
16528 demand_empty_rest_of_line ();
16529}
16530
16531/* Handle the .module pseudo-op. */
16532
16533static void
16534s_module (int ignore ATTRIBUTE_UNUSED)
16535{
16536 char *name = input_line_pointer, ch;
16537
16538 while (!is_end_of_line[(unsigned char) *input_line_pointer])
16539 ++input_line_pointer;
16540 ch = *input_line_pointer;
16541 *input_line_pointer = '\0';
16542
16543 if (!file_mips_opts_checked)
252b5132 16544 {
22522f88 16545 if (parse_code_option (name) == OPTION_TYPE_BAD)
919731af 16546 as_bad (_(".module used with unrecognized symbol: %s\n"), name);
16547
16548 /* Update module level settings from mips_opts. */
16549 file_mips_opts = mips_opts;
252b5132 16550 }
919731af 16551 else
16552 as_bad (_(".module is not permitted after generating code"));
16553
252b5132
RH
16554 *input_line_pointer = ch;
16555 demand_empty_rest_of_line ();
16556}
16557
16558/* Handle the .abicalls pseudo-op. I believe this is equivalent to
16559 .option pic2. It means to generate SVR4 PIC calls. */
16560
16561static void
17a2f251 16562s_abicalls (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16563{
16564 mips_pic = SVR4_PIC;
143d77c5 16565 mips_abicalls = TRUE;
4d0d148d
TS
16566
16567 if (g_switch_seen && g_switch_value != 0)
16568 as_warn (_("-G may not be used with SVR4 PIC code"));
16569 g_switch_value = 0;
16570
252b5132
RH
16571 bfd_set_gp_size (stdoutput, 0);
16572 demand_empty_rest_of_line ();
16573}
16574
16575/* Handle the .cpload pseudo-op. This is used when generating SVR4
16576 PIC code. It sets the $gp register for the function based on the
16577 function address, which is in the register named in the argument.
16578 This uses a relocation against _gp_disp, which is handled specially
16579 by the linker. The result is:
16580 lui $gp,%hi(_gp_disp)
16581 addiu $gp,$gp,%lo(_gp_disp)
16582 addu $gp,$gp,.cpload argument
aa6975fb
ILT
16583 The .cpload argument is normally $25 == $t9.
16584
16585 The -mno-shared option changes this to:
bbe506e8
TS
16586 lui $gp,%hi(__gnu_local_gp)
16587 addiu $gp,$gp,%lo(__gnu_local_gp)
aa6975fb
ILT
16588 and the argument is ignored. This saves an instruction, but the
16589 resulting code is not position independent; it uses an absolute
bbe506e8
TS
16590 address for __gnu_local_gp. Thus code assembled with -mno-shared
16591 can go into an ordinary executable, but not into a shared library. */
252b5132
RH
16592
16593static void
17a2f251 16594s_cpload (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16595{
16596 expressionS ex;
aa6975fb
ILT
16597 int reg;
16598 int in_shared;
252b5132 16599
919731af 16600 file_mips_check_options ();
16601
6478892d
TS
16602 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
16603 .cpload is ignored. */
16604 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16605 {
16606 s_ignore (0);
16607 return;
16608 }
16609
a276b80c
MR
16610 if (mips_opts.mips16)
16611 {
16612 as_bad (_("%s not supported in MIPS16 mode"), ".cpload");
16613 ignore_rest_of_line ();
16614 return;
16615 }
16616
d3ecfc59 16617 /* .cpload should be in a .set noreorder section. */
252b5132
RH
16618 if (mips_opts.noreorder == 0)
16619 as_warn (_(".cpload not in noreorder section"));
16620
aa6975fb
ILT
16621 reg = tc_get_register (0);
16622
16623 /* If we need to produce a 64-bit address, we are better off using
16624 the default instruction sequence. */
aed1a261 16625 in_shared = mips_in_shared || HAVE_64BIT_SYMBOLS;
aa6975fb 16626
252b5132 16627 ex.X_op = O_symbol;
bbe506e8
TS
16628 ex.X_add_symbol = symbol_find_or_make (in_shared ? "_gp_disp" :
16629 "__gnu_local_gp");
252b5132
RH
16630 ex.X_op_symbol = NULL;
16631 ex.X_add_number = 0;
16632
16633 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
49309057 16634 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
252b5132 16635
8a75745d
MR
16636 mips_mark_labels ();
16637 mips_assembling_insn = TRUE;
16638
584892a6 16639 macro_start ();
67c0d1eb
RS
16640 macro_build_lui (&ex, mips_gp_register);
16641 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
17a2f251 16642 mips_gp_register, BFD_RELOC_LO16);
aa6975fb
ILT
16643 if (in_shared)
16644 macro_build (NULL, "addu", "d,v,t", mips_gp_register,
16645 mips_gp_register, reg);
584892a6 16646 macro_end ();
252b5132 16647
8a75745d 16648 mips_assembling_insn = FALSE;
252b5132
RH
16649 demand_empty_rest_of_line ();
16650}
16651
6478892d
TS
16652/* Handle the .cpsetup pseudo-op defined for NewABI PIC code. The syntax is:
16653 .cpsetup $reg1, offset|$reg2, label
16654
16655 If offset is given, this results in:
16656 sd $gp, offset($sp)
956cd1d6 16657 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16658 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16659 daddu $gp, $gp, $reg1
6478892d
TS
16660
16661 If $reg2 is given, this results in:
40fc1451 16662 or $reg2, $gp, $0
956cd1d6 16663 lui $gp, %hi(%neg(%gp_rel(label)))
698b7d9d
TS
16664 addiu $gp, $gp, %lo(%neg(%gp_rel(label)))
16665 daddu $gp, $gp, $reg1
aa6975fb
ILT
16666 $reg1 is normally $25 == $t9.
16667
16668 The -mno-shared option replaces the last three instructions with
16669 lui $gp,%hi(_gp)
54f4ddb3 16670 addiu $gp,$gp,%lo(_gp) */
aa6975fb 16671
6478892d 16672static void
17a2f251 16673s_cpsetup (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16674{
16675 expressionS ex_off;
16676 expressionS ex_sym;
16677 int reg1;
6478892d 16678
919731af 16679 file_mips_check_options ();
16680
8586fc66 16681 /* If we are not generating SVR4 PIC code, .cpsetup is ignored.
6478892d
TS
16682 We also need NewABI support. */
16683 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16684 {
16685 s_ignore (0);
16686 return;
16687 }
16688
a276b80c
MR
16689 if (mips_opts.mips16)
16690 {
16691 as_bad (_("%s not supported in MIPS16 mode"), ".cpsetup");
16692 ignore_rest_of_line ();
16693 return;
16694 }
16695
6478892d
TS
16696 reg1 = tc_get_register (0);
16697 SKIP_WHITESPACE ();
16698 if (*input_line_pointer != ',')
16699 {
16700 as_bad (_("missing argument separator ',' for .cpsetup"));
16701 return;
16702 }
16703 else
80245285 16704 ++input_line_pointer;
6478892d
TS
16705 SKIP_WHITESPACE ();
16706 if (*input_line_pointer == '$')
80245285
TS
16707 {
16708 mips_cpreturn_register = tc_get_register (0);
16709 mips_cpreturn_offset = -1;
16710 }
6478892d 16711 else
80245285
TS
16712 {
16713 mips_cpreturn_offset = get_absolute_expression ();
16714 mips_cpreturn_register = -1;
16715 }
6478892d
TS
16716 SKIP_WHITESPACE ();
16717 if (*input_line_pointer != ',')
16718 {
16719 as_bad (_("missing argument separator ',' for .cpsetup"));
16720 return;
16721 }
16722 else
f9419b05 16723 ++input_line_pointer;
6478892d 16724 SKIP_WHITESPACE ();
f21f8242 16725 expression (&ex_sym);
6478892d 16726
8a75745d
MR
16727 mips_mark_labels ();
16728 mips_assembling_insn = TRUE;
16729
584892a6 16730 macro_start ();
6478892d
TS
16731 if (mips_cpreturn_register == -1)
16732 {
16733 ex_off.X_op = O_constant;
16734 ex_off.X_add_symbol = NULL;
16735 ex_off.X_op_symbol = NULL;
16736 ex_off.X_add_number = mips_cpreturn_offset;
16737
67c0d1eb 16738 macro_build (&ex_off, "sd", "t,o(b)", mips_gp_register,
17a2f251 16739 BFD_RELOC_LO16, SP);
6478892d
TS
16740 }
16741 else
40fc1451 16742 move_register (mips_cpreturn_register, mips_gp_register);
6478892d 16743
aed1a261 16744 if (mips_in_shared || HAVE_64BIT_SYMBOLS)
aa6975fb 16745 {
df58fc94 16746 macro_build (&ex_sym, "lui", LUI_FMT, mips_gp_register,
aa6975fb
ILT
16747 -1, BFD_RELOC_GPREL16, BFD_RELOC_MIPS_SUB,
16748 BFD_RELOC_HI16_S);
16749
16750 macro_build (&ex_sym, "addiu", "t,r,j", mips_gp_register,
16751 mips_gp_register, -1, BFD_RELOC_GPREL16,
16752 BFD_RELOC_MIPS_SUB, BFD_RELOC_LO16);
16753
16754 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", mips_gp_register,
16755 mips_gp_register, reg1);
16756 }
16757 else
16758 {
16759 expressionS ex;
16760
16761 ex.X_op = O_symbol;
4184909a 16762 ex.X_add_symbol = symbol_find_or_make ("__gnu_local_gp");
aa6975fb
ILT
16763 ex.X_op_symbol = NULL;
16764 ex.X_add_number = 0;
6e1304d8 16765
aa6975fb
ILT
16766 /* In ELF, this symbol is implicitly an STT_OBJECT symbol. */
16767 symbol_get_bfdsym (ex.X_add_symbol)->flags |= BSF_OBJECT;
16768
16769 macro_build_lui (&ex, mips_gp_register);
16770 macro_build (&ex, "addiu", "t,r,j", mips_gp_register,
16771 mips_gp_register, BFD_RELOC_LO16);
16772 }
f21f8242 16773
584892a6 16774 macro_end ();
6478892d 16775
8a75745d 16776 mips_assembling_insn = FALSE;
6478892d
TS
16777 demand_empty_rest_of_line ();
16778}
16779
16780static void
17a2f251 16781s_cplocal (int ignore ATTRIBUTE_UNUSED)
6478892d 16782{
919731af 16783 file_mips_check_options ();
16784
6478892d 16785 /* If we are not generating SVR4 PIC code, or if this is not NewABI code,
54f4ddb3 16786 .cplocal is ignored. */
6478892d
TS
16787 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16788 {
16789 s_ignore (0);
16790 return;
16791 }
16792
a276b80c
MR
16793 if (mips_opts.mips16)
16794 {
16795 as_bad (_("%s not supported in MIPS16 mode"), ".cplocal");
16796 ignore_rest_of_line ();
16797 return;
16798 }
16799
6478892d 16800 mips_gp_register = tc_get_register (0);
85b51719 16801 demand_empty_rest_of_line ();
6478892d
TS
16802}
16803
252b5132
RH
16804/* Handle the .cprestore pseudo-op. This stores $gp into a given
16805 offset from $sp. The offset is remembered, and after making a PIC
16806 call $gp is restored from that location. */
16807
16808static void
17a2f251 16809s_cprestore (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
16810{
16811 expressionS ex;
252b5132 16812
919731af 16813 file_mips_check_options ();
16814
6478892d 16815 /* If we are not generating SVR4 PIC code, or if this is NewABI code,
c9914766 16816 .cprestore is ignored. */
6478892d 16817 if (mips_pic != SVR4_PIC || HAVE_NEWABI)
252b5132
RH
16818 {
16819 s_ignore (0);
16820 return;
16821 }
16822
a276b80c
MR
16823 if (mips_opts.mips16)
16824 {
16825 as_bad (_("%s not supported in MIPS16 mode"), ".cprestore");
16826 ignore_rest_of_line ();
16827 return;
16828 }
16829
252b5132 16830 mips_cprestore_offset = get_absolute_expression ();
7a621144 16831 mips_cprestore_valid = 1;
252b5132
RH
16832
16833 ex.X_op = O_constant;
16834 ex.X_add_symbol = NULL;
16835 ex.X_op_symbol = NULL;
16836 ex.X_add_number = mips_cprestore_offset;
16837
8a75745d
MR
16838 mips_mark_labels ();
16839 mips_assembling_insn = TRUE;
16840
584892a6 16841 macro_start ();
67c0d1eb
RS
16842 macro_build_ldst_constoffset (&ex, ADDRESS_STORE_INSN, mips_gp_register,
16843 SP, HAVE_64BIT_ADDRESSES);
584892a6 16844 macro_end ();
252b5132 16845
8a75745d 16846 mips_assembling_insn = FALSE;
252b5132
RH
16847 demand_empty_rest_of_line ();
16848}
16849
6478892d 16850/* Handle the .cpreturn pseudo-op defined for NewABI PIC code. If an offset
67c1ffbe 16851 was given in the preceding .cpsetup, it results in:
6478892d 16852 ld $gp, offset($sp)
76b3015f 16853
6478892d 16854 If a register $reg2 was given there, it results in:
40fc1451 16855 or $gp, $reg2, $0 */
54f4ddb3 16856
6478892d 16857static void
17a2f251 16858s_cpreturn (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16859{
16860 expressionS ex;
6478892d 16861
919731af 16862 file_mips_check_options ();
16863
6478892d
TS
16864 /* If we are not generating SVR4 PIC code, .cpreturn is ignored.
16865 We also need NewABI support. */
16866 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16867 {
16868 s_ignore (0);
16869 return;
16870 }
16871
a276b80c
MR
16872 if (mips_opts.mips16)
16873 {
16874 as_bad (_("%s not supported in MIPS16 mode"), ".cpreturn");
16875 ignore_rest_of_line ();
16876 return;
16877 }
16878
8a75745d
MR
16879 mips_mark_labels ();
16880 mips_assembling_insn = TRUE;
16881
584892a6 16882 macro_start ();
6478892d
TS
16883 if (mips_cpreturn_register == -1)
16884 {
16885 ex.X_op = O_constant;
16886 ex.X_add_symbol = NULL;
16887 ex.X_op_symbol = NULL;
16888 ex.X_add_number = mips_cpreturn_offset;
16889
67c0d1eb 16890 macro_build (&ex, "ld", "t,o(b)", mips_gp_register, BFD_RELOC_LO16, SP);
6478892d
TS
16891 }
16892 else
40fc1451
SD
16893 move_register (mips_gp_register, mips_cpreturn_register);
16894
584892a6 16895 macro_end ();
6478892d 16896
8a75745d 16897 mips_assembling_insn = FALSE;
6478892d
TS
16898 demand_empty_rest_of_line ();
16899}
16900
d0f13682
CLT
16901/* Handle a .dtprelword, .dtpreldword, .tprelword, or .tpreldword
16902 pseudo-op; DIRSTR says which. The pseudo-op generates a BYTES-size
16903 DTP- or TP-relative relocation of type RTYPE, for use in either DWARF
16904 debug information or MIPS16 TLS. */
741d6ea8
JM
16905
16906static void
d0f13682
CLT
16907s_tls_rel_directive (const size_t bytes, const char *dirstr,
16908 bfd_reloc_code_real_type rtype)
741d6ea8
JM
16909{
16910 expressionS ex;
16911 char *p;
16912
16913 expression (&ex);
16914
16915 if (ex.X_op != O_symbol)
16916 {
1661c76c 16917 as_bad (_("unsupported use of %s"), dirstr);
741d6ea8
JM
16918 ignore_rest_of_line ();
16919 }
16920
16921 p = frag_more (bytes);
16922 md_number_to_chars (p, 0, bytes);
d0f13682 16923 fix_new_exp (frag_now, p - frag_now->fr_literal, bytes, &ex, FALSE, rtype);
741d6ea8 16924 demand_empty_rest_of_line ();
de64cffd 16925 mips_clear_insn_labels ();
741d6ea8
JM
16926}
16927
16928/* Handle .dtprelword. */
16929
16930static void
16931s_dtprelword (int ignore ATTRIBUTE_UNUSED)
16932{
d0f13682 16933 s_tls_rel_directive (4, ".dtprelword", BFD_RELOC_MIPS_TLS_DTPREL32);
741d6ea8
JM
16934}
16935
16936/* Handle .dtpreldword. */
16937
16938static void
16939s_dtpreldword (int ignore ATTRIBUTE_UNUSED)
16940{
d0f13682
CLT
16941 s_tls_rel_directive (8, ".dtpreldword", BFD_RELOC_MIPS_TLS_DTPREL64);
16942}
16943
16944/* Handle .tprelword. */
16945
16946static void
16947s_tprelword (int ignore ATTRIBUTE_UNUSED)
16948{
16949 s_tls_rel_directive (4, ".tprelword", BFD_RELOC_MIPS_TLS_TPREL32);
16950}
16951
16952/* Handle .tpreldword. */
16953
16954static void
16955s_tpreldword (int ignore ATTRIBUTE_UNUSED)
16956{
16957 s_tls_rel_directive (8, ".tpreldword", BFD_RELOC_MIPS_TLS_TPREL64);
741d6ea8
JM
16958}
16959
6478892d
TS
16960/* Handle the .gpvalue pseudo-op. This is used when generating NewABI PIC
16961 code. It sets the offset to use in gp_rel relocations. */
16962
16963static void
17a2f251 16964s_gpvalue (int ignore ATTRIBUTE_UNUSED)
6478892d
TS
16965{
16966 /* If we are not generating SVR4 PIC code, .gpvalue is ignored.
16967 We also need NewABI support. */
16968 if (mips_pic != SVR4_PIC || ! HAVE_NEWABI)
16969 {
16970 s_ignore (0);
16971 return;
16972 }
16973
def2e0dd 16974 mips_gprel_offset = get_absolute_expression ();
6478892d
TS
16975
16976 demand_empty_rest_of_line ();
16977}
16978
252b5132
RH
16979/* Handle the .gpword pseudo-op. This is used when generating PIC
16980 code. It generates a 32 bit GP relative reloc. */
16981
16982static void
17a2f251 16983s_gpword (int ignore ATTRIBUTE_UNUSED)
252b5132 16984{
a8dbcb85
TS
16985 segment_info_type *si;
16986 struct insn_label_list *l;
252b5132
RH
16987 expressionS ex;
16988 char *p;
16989
16990 /* When not generating PIC code, this is treated as .word. */
16991 if (mips_pic != SVR4_PIC)
16992 {
16993 s_cons (2);
16994 return;
16995 }
16996
a8dbcb85
TS
16997 si = seg_info (now_seg);
16998 l = si->label_list;
7d10b47d 16999 mips_emit_delays ();
252b5132 17000 if (auto_align)
462427c4 17001 mips_align (2, 0, l);
252b5132
RH
17002
17003 expression (&ex);
a1facbec 17004 mips_clear_insn_labels ();
252b5132
RH
17005
17006 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17007 {
1661c76c 17008 as_bad (_("unsupported use of .gpword"));
252b5132
RH
17009 ignore_rest_of_line ();
17010 }
17011
17012 p = frag_more (4);
17a2f251 17013 md_number_to_chars (p, 0, 4);
b34976b6 17014 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
cdf6fd85 17015 BFD_RELOC_GPREL32);
252b5132
RH
17016
17017 demand_empty_rest_of_line ();
17018}
17019
10181a0d 17020static void
17a2f251 17021s_gpdword (int ignore ATTRIBUTE_UNUSED)
10181a0d 17022{
a8dbcb85
TS
17023 segment_info_type *si;
17024 struct insn_label_list *l;
10181a0d
AO
17025 expressionS ex;
17026 char *p;
17027
17028 /* When not generating PIC code, this is treated as .dword. */
17029 if (mips_pic != SVR4_PIC)
17030 {
17031 s_cons (3);
17032 return;
17033 }
17034
a8dbcb85
TS
17035 si = seg_info (now_seg);
17036 l = si->label_list;
7d10b47d 17037 mips_emit_delays ();
10181a0d 17038 if (auto_align)
462427c4 17039 mips_align (3, 0, l);
10181a0d
AO
17040
17041 expression (&ex);
a1facbec 17042 mips_clear_insn_labels ();
10181a0d
AO
17043
17044 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17045 {
1661c76c 17046 as_bad (_("unsupported use of .gpdword"));
10181a0d
AO
17047 ignore_rest_of_line ();
17048 }
17049
17050 p = frag_more (8);
17a2f251 17051 md_number_to_chars (p, 0, 8);
a105a300 17052 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
6e1304d8 17053 BFD_RELOC_GPREL32)->fx_tcbit = 1;
10181a0d
AO
17054
17055 /* GPREL32 composed with 64 gives a 64-bit GP offset. */
6e1304d8
RS
17056 fix_new (frag_now, p - frag_now->fr_literal, 8, NULL, 0,
17057 FALSE, BFD_RELOC_64)->fx_tcbit = 1;
10181a0d
AO
17058
17059 demand_empty_rest_of_line ();
17060}
17061
a3f278e2
CM
17062/* Handle the .ehword pseudo-op. This is used when generating unwinding
17063 tables. It generates a R_MIPS_EH reloc. */
17064
17065static void
17066s_ehword (int ignore ATTRIBUTE_UNUSED)
17067{
17068 expressionS ex;
17069 char *p;
17070
17071 mips_emit_delays ();
17072
17073 expression (&ex);
17074 mips_clear_insn_labels ();
17075
17076 if (ex.X_op != O_symbol || ex.X_add_number != 0)
17077 {
1661c76c 17078 as_bad (_("unsupported use of .ehword"));
a3f278e2
CM
17079 ignore_rest_of_line ();
17080 }
17081
17082 p = frag_more (4);
17083 md_number_to_chars (p, 0, 4);
17084 fix_new_exp (frag_now, p - frag_now->fr_literal, 4, &ex, FALSE,
2f0c68f2 17085 BFD_RELOC_32_PCREL);
a3f278e2
CM
17086
17087 demand_empty_rest_of_line ();
17088}
17089
252b5132
RH
17090/* Handle the .cpadd pseudo-op. This is used when dealing with switch
17091 tables in SVR4 PIC code. */
17092
17093static void
17a2f251 17094s_cpadd (int ignore ATTRIBUTE_UNUSED)
252b5132 17095{
252b5132
RH
17096 int reg;
17097
919731af 17098 file_mips_check_options ();
17099
10181a0d
AO
17100 /* This is ignored when not generating SVR4 PIC code. */
17101 if (mips_pic != SVR4_PIC)
252b5132
RH
17102 {
17103 s_ignore (0);
17104 return;
17105 }
17106
8a75745d
MR
17107 mips_mark_labels ();
17108 mips_assembling_insn = TRUE;
17109
252b5132 17110 /* Add $gp to the register named as an argument. */
584892a6 17111 macro_start ();
252b5132 17112 reg = tc_get_register (0);
67c0d1eb 17113 macro_build (NULL, ADDRESS_ADD_INSN, "d,v,t", reg, reg, mips_gp_register);
584892a6 17114 macro_end ();
252b5132 17115
8a75745d 17116 mips_assembling_insn = FALSE;
bdaaa2e1 17117 demand_empty_rest_of_line ();
252b5132
RH
17118}
17119
17120/* Handle the .insn pseudo-op. This marks instruction labels in
df58fc94 17121 mips16/micromips mode. This permits the linker to handle them specially,
252b5132
RH
17122 such as generating jalx instructions when needed. We also make
17123 them odd for the duration of the assembly, in order to generate the
17124 right sort of code. We will make them even in the adjust_symtab
17125 routine, while leaving them marked. This is convenient for the
17126 debugger and the disassembler. The linker knows to make them odd
17127 again. */
17128
17129static void
17a2f251 17130s_insn (int ignore ATTRIBUTE_UNUSED)
252b5132 17131{
7bb01e2d
MR
17132 file_mips_check_options ();
17133 file_ase_mips16 |= mips_opts.mips16;
17134 file_ase_micromips |= mips_opts.micromips;
17135
df58fc94 17136 mips_mark_labels ();
252b5132
RH
17137
17138 demand_empty_rest_of_line ();
17139}
17140
ba92f887
MR
17141/* Handle the .nan pseudo-op. */
17142
17143static void
17144s_nan (int ignore ATTRIBUTE_UNUSED)
17145{
17146 static const char str_legacy[] = "legacy";
17147 static const char str_2008[] = "2008";
17148 size_t i;
17149
17150 for (i = 0; !is_end_of_line[(unsigned char) input_line_pointer[i]]; i++);
17151
17152 if (i == sizeof (str_2008) - 1
17153 && memcmp (input_line_pointer, str_2008, i) == 0)
7361da2c 17154 mips_nan2008 = 1;
ba92f887
MR
17155 else if (i == sizeof (str_legacy) - 1
17156 && memcmp (input_line_pointer, str_legacy, i) == 0)
7361da2c
AB
17157 {
17158 if (ISA_HAS_LEGACY_NAN (file_mips_opts.isa))
17159 mips_nan2008 = 0;
17160 else
17161 as_bad (_("`%s' does not support legacy NaN"),
17162 mips_cpu_info_from_isa (file_mips_opts.isa)->name);
17163 }
ba92f887 17164 else
1661c76c 17165 as_bad (_("bad .nan directive"));
ba92f887
MR
17166
17167 input_line_pointer += i;
17168 demand_empty_rest_of_line ();
17169}
17170
754e2bb9
RS
17171/* Handle a .stab[snd] directive. Ideally these directives would be
17172 implemented in a transparent way, so that removing them would not
17173 have any effect on the generated instructions. However, s_stab
17174 internally changes the section, so in practice we need to decide
17175 now whether the preceding label marks compressed code. We do not
17176 support changing the compression mode of a label after a .stab*
17177 directive, such as in:
17178
17179 foo:
134c0c8b 17180 .stabs ...
754e2bb9
RS
17181 .set mips16
17182
17183 so the current mode wins. */
252b5132
RH
17184
17185static void
17a2f251 17186s_mips_stab (int type)
252b5132 17187{
42c0794e 17188 file_mips_check_options ();
754e2bb9 17189 mips_mark_labels ();
252b5132
RH
17190 s_stab (type);
17191}
17192
54f4ddb3 17193/* Handle the .weakext pseudo-op as defined in Kane and Heinrich. */
252b5132
RH
17194
17195static void
17a2f251 17196s_mips_weakext (int ignore ATTRIBUTE_UNUSED)
252b5132
RH
17197{
17198 char *name;
17199 int c;
17200 symbolS *symbolP;
17201 expressionS exp;
17202
d02603dc 17203 c = get_symbol_name (&name);
252b5132
RH
17204 symbolP = symbol_find_or_make (name);
17205 S_SET_WEAK (symbolP);
17206 *input_line_pointer = c;
17207
d02603dc 17208 SKIP_WHITESPACE_AFTER_NAME ();
252b5132
RH
17209
17210 if (! is_end_of_line[(unsigned char) *input_line_pointer])
17211 {
17212 if (S_IS_DEFINED (symbolP))
17213 {
20203fb9 17214 as_bad (_("ignoring attempt to redefine symbol %s"),
252b5132
RH
17215 S_GET_NAME (symbolP));
17216 ignore_rest_of_line ();
17217 return;
17218 }
bdaaa2e1 17219
252b5132
RH
17220 if (*input_line_pointer == ',')
17221 {
17222 ++input_line_pointer;
17223 SKIP_WHITESPACE ();
17224 }
bdaaa2e1 17225
252b5132
RH
17226 expression (&exp);
17227 if (exp.X_op != O_symbol)
17228 {
20203fb9 17229 as_bad (_("bad .weakext directive"));
98d3f06f 17230 ignore_rest_of_line ();
252b5132
RH
17231 return;
17232 }
49309057 17233 symbol_set_value_expression (symbolP, &exp);
252b5132
RH
17234 }
17235
17236 demand_empty_rest_of_line ();
17237}
17238
17239/* Parse a register string into a number. Called from the ECOFF code
17240 to parse .frame. The argument is non-zero if this is the frame
17241 register, so that we can record it in mips_frame_reg. */
17242
17243int
17a2f251 17244tc_get_register (int frame)
252b5132 17245{
707bfff6 17246 unsigned int reg;
252b5132
RH
17247
17248 SKIP_WHITESPACE ();
707bfff6
TS
17249 if (! reg_lookup (&input_line_pointer, RWARN | RTYPE_NUM | RTYPE_GP, &reg))
17250 reg = 0;
252b5132 17251 if (frame)
7a621144
DJ
17252 {
17253 mips_frame_reg = reg != 0 ? reg : SP;
17254 mips_frame_reg_valid = 1;
17255 mips_cprestore_valid = 0;
17256 }
252b5132
RH
17257 return reg;
17258}
17259
17260valueT
17a2f251 17261md_section_align (asection *seg, valueT addr)
252b5132
RH
17262{
17263 int align = bfd_get_section_alignment (stdoutput, seg);
17264
f3ded42a
RS
17265 /* We don't need to align ELF sections to the full alignment.
17266 However, Irix 5 may prefer that we align them at least to a 16
17267 byte boundary. We don't bother to align the sections if we
17268 are targeted for an embedded system. */
17269 if (strncmp (TARGET_OS, "elf", 3) == 0)
17270 return addr;
17271 if (align > 4)
17272 align = 4;
252b5132 17273
8d3842cd 17274 return ((addr + (1 << align) - 1) & -(1 << align));
252b5132
RH
17275}
17276
17277/* Utility routine, called from above as well. If called while the
17278 input file is still being read, it's only an approximation. (For
17279 example, a symbol may later become defined which appeared to be
17280 undefined earlier.) */
17281
17282static int
17a2f251 17283nopic_need_relax (symbolS *sym, int before_relaxing)
252b5132
RH
17284{
17285 if (sym == 0)
17286 return 0;
17287
4d0d148d 17288 if (g_switch_value > 0)
252b5132
RH
17289 {
17290 const char *symname;
17291 int change;
17292
c9914766 17293 /* Find out whether this symbol can be referenced off the $gp
252b5132
RH
17294 register. It can be if it is smaller than the -G size or if
17295 it is in the .sdata or .sbss section. Certain symbols can
c9914766 17296 not be referenced off the $gp, although it appears as though
252b5132
RH
17297 they can. */
17298 symname = S_GET_NAME (sym);
17299 if (symname != (const char *) NULL
17300 && (strcmp (symname, "eprol") == 0
17301 || strcmp (symname, "etext") == 0
17302 || strcmp (symname, "_gp") == 0
17303 || strcmp (symname, "edata") == 0
17304 || strcmp (symname, "_fbss") == 0
17305 || strcmp (symname, "_fdata") == 0
17306 || strcmp (symname, "_ftext") == 0
17307 || strcmp (symname, "end") == 0
17308 || strcmp (symname, "_gp_disp") == 0))
17309 change = 1;
17310 else if ((! S_IS_DEFINED (sym) || S_IS_COMMON (sym))
17311 && (0
17312#ifndef NO_ECOFF_DEBUGGING
49309057
ILT
17313 || (symbol_get_obj (sym)->ecoff_extern_size != 0
17314 && (symbol_get_obj (sym)->ecoff_extern_size
17315 <= g_switch_value))
252b5132
RH
17316#endif
17317 /* We must defer this decision until after the whole
17318 file has been read, since there might be a .extern
17319 after the first use of this symbol. */
17320 || (before_relaxing
17321#ifndef NO_ECOFF_DEBUGGING
49309057 17322 && symbol_get_obj (sym)->ecoff_extern_size == 0
252b5132
RH
17323#endif
17324 && S_GET_VALUE (sym) == 0)
17325 || (S_GET_VALUE (sym) != 0
17326 && S_GET_VALUE (sym) <= g_switch_value)))
17327 change = 0;
17328 else
17329 {
17330 const char *segname;
17331
17332 segname = segment_name (S_GET_SEGMENT (sym));
9c2799c2 17333 gas_assert (strcmp (segname, ".lit8") != 0
252b5132
RH
17334 && strcmp (segname, ".lit4") != 0);
17335 change = (strcmp (segname, ".sdata") != 0
fba2b7f9
GK
17336 && strcmp (segname, ".sbss") != 0
17337 && strncmp (segname, ".sdata.", 7) != 0
d4dc2f22
TS
17338 && strncmp (segname, ".sbss.", 6) != 0
17339 && strncmp (segname, ".gnu.linkonce.sb.", 17) != 0
fba2b7f9 17340 && strncmp (segname, ".gnu.linkonce.s.", 16) != 0);
252b5132
RH
17341 }
17342 return change;
17343 }
17344 else
c9914766 17345 /* We are not optimizing for the $gp register. */
252b5132
RH
17346 return 1;
17347}
17348
5919d012
RS
17349
17350/* Return true if the given symbol should be considered local for SVR4 PIC. */
17351
17352static bfd_boolean
9e009953 17353pic_need_relax (symbolS *sym)
5919d012
RS
17354{
17355 asection *symsec;
5919d012
RS
17356
17357 /* Handle the case of a symbol equated to another symbol. */
17358 while (symbol_equated_reloc_p (sym))
17359 {
17360 symbolS *n;
17361
5f0fe04b 17362 /* It's possible to get a loop here in a badly written program. */
5919d012
RS
17363 n = symbol_get_value_expression (sym)->X_add_symbol;
17364 if (n == sym)
17365 break;
17366 sym = n;
17367 }
17368
df1f3cda
DD
17369 if (symbol_section_p (sym))
17370 return TRUE;
17371
5919d012
RS
17372 symsec = S_GET_SEGMENT (sym);
17373
5919d012 17374 /* This must duplicate the test in adjust_reloc_syms. */
45dfa85a
AM
17375 return (!bfd_is_und_section (symsec)
17376 && !bfd_is_abs_section (symsec)
5f0fe04b 17377 && !bfd_is_com_section (symsec)
5919d012 17378 /* A global or weak symbol is treated as external. */
f3ded42a 17379 && (!S_IS_WEAK (sym) && !S_IS_EXTERNAL (sym)));
5919d012 17380}
14f72d45
MR
17381\f
17382/* Given a MIPS16 variant frag FRAGP and PC-relative operand PCREL_OP
17383 convert a section-relative value VAL to the equivalent PC-relative
17384 value. */
17385
17386static offsetT
17387mips16_pcrel_val (fragS *fragp, const struct mips_pcrel_operand *pcrel_op,
17388 offsetT val, long stretch)
17389{
17390 fragS *sym_frag;
17391 addressT addr;
17392
17393 gas_assert (pcrel_op->root.root.type == OP_PCREL);
17394
17395 sym_frag = symbol_get_frag (fragp->fr_symbol);
17396
17397 /* If the relax_marker of the symbol fragment differs from the
17398 relax_marker of this fragment, we have not yet adjusted the
17399 symbol fragment fr_address. We want to add in STRETCH in
17400 order to get a better estimate of the address. This
17401 particularly matters because of the shift bits. */
17402 if (stretch != 0 && sym_frag->relax_marker != fragp->relax_marker)
17403 {
17404 fragS *f;
17405
17406 /* Adjust stretch for any alignment frag. Note that if have
17407 been expanding the earlier code, the symbol may be
17408 defined in what appears to be an earlier frag. FIXME:
17409 This doesn't handle the fr_subtype field, which specifies
17410 a maximum number of bytes to skip when doing an
17411 alignment. */
17412 for (f = fragp; f != NULL && f != sym_frag; f = f->fr_next)
17413 {
17414 if (f->fr_type == rs_align || f->fr_type == rs_align_code)
17415 {
17416 if (stretch < 0)
17417 stretch = -(-stretch & ~((1 << (int) f->fr_offset) - 1));
17418 else
17419 stretch &= ~((1 << (int) f->fr_offset) - 1);
17420 if (stretch == 0)
17421 break;
17422 }
17423 }
17424 if (f != NULL)
17425 val += stretch;
17426 }
17427
17428 addr = fragp->fr_address + fragp->fr_fix;
17429
17430 /* The base address rules are complicated. The base address of
17431 a branch is the following instruction. The base address of a
17432 PC relative load or add is the instruction itself, but if it
17433 is in a delay slot (in which case it can not be extended) use
17434 the address of the instruction whose delay slot it is in. */
17435 if (pcrel_op->include_isa_bit)
17436 {
17437 addr += 2;
17438
17439 /* If we are currently assuming that this frag should be
17440 extended, then the current address is two bytes higher. */
17441 if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
17442 addr += 2;
17443
17444 /* Ignore the low bit in the target, since it will be set
17445 for a text label. */
17446 val &= -2;
17447 }
17448 else if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype))
17449 addr -= 4;
17450 else if (RELAX_MIPS16_DSLOT (fragp->fr_subtype))
17451 addr -= 2;
5919d012 17452
14f72d45
MR
17453 val -= addr & -(1 << pcrel_op->align_log2);
17454
17455 return val;
17456}
5919d012 17457
252b5132
RH
17458/* Given a mips16 variant frag FRAGP, return non-zero if it needs an
17459 extended opcode. SEC is the section the frag is in. */
17460
17461static int
17a2f251 17462mips16_extended_frag (fragS *fragp, asection *sec, long stretch)
252b5132 17463{
3ccad066 17464 const struct mips_int_operand *operand;
252b5132 17465 offsetT val;
252b5132 17466 segT symsec;
14f72d45 17467 int type;
252b5132
RH
17468
17469 if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
17470 return 0;
17471 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17472 return 1;
17473
88a7ef16 17474 symsec = S_GET_SEGMENT (fragp->fr_symbol);
252b5132 17475 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 17476 operand = mips16_immed_operand (type, FALSE);
88a7ef16
MR
17477 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
17478 || (operand->root.type == OP_PCREL
17479 ? sec != symsec
17480 : !bfd_is_abs_section (symsec)))
17481 return 1;
252b5132 17482
88a7ef16 17483 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
252b5132 17484
3ccad066 17485 if (operand->root.type == OP_PCREL)
252b5132 17486 {
3ccad066 17487 const struct mips_pcrel_operand *pcrel_op;
3ccad066 17488 offsetT maxtiny;
252b5132 17489
1425c41d 17490 if (RELAX_MIPS16_ALWAYS_EXTENDED (fragp->fr_subtype))
88a7ef16 17491 return 1;
252b5132 17492
88a7ef16 17493 pcrel_op = (const struct mips_pcrel_operand *) operand;
14f72d45 17494 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
252b5132
RH
17495
17496 /* If any of the shifted bits are set, we must use an extended
17497 opcode. If the address depends on the size of this
17498 instruction, this can lead to a loop, so we arrange to always
88a7ef16
MR
17499 use an extended opcode. */
17500 if ((val & ((1 << operand->shift) - 1)) != 0)
252b5132
RH
17501 {
17502 fragp->fr_subtype =
1425c41d 17503 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17504 return 1;
17505 }
17506
17507 /* If we are about to mark a frag as extended because the value
3ccad066
RS
17508 is precisely the next value above maxtiny, then there is a
17509 chance of an infinite loop as in the following code:
252b5132
RH
17510 la $4,foo
17511 .skip 1020
17512 .align 2
17513 foo:
17514 In this case when the la is extended, foo is 0x3fc bytes
17515 away, so the la can be shrunk, but then foo is 0x400 away, so
17516 the la must be extended. To avoid this loop, we mark the
17517 frag as extended if it was small, and is about to become
3ccad066
RS
17518 extended with the next value above maxtiny. */
17519 maxtiny = mips_int_operand_max (operand);
17520 if (val == maxtiny + (1 << operand->shift)
88a7ef16 17521 && ! RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
252b5132
RH
17522 {
17523 fragp->fr_subtype =
1425c41d 17524 RELAX_MIPS16_MARK_ALWAYS_EXTENDED (fragp->fr_subtype);
252b5132
RH
17525 return 1;
17526 }
17527 }
252b5132 17528
3ccad066 17529 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
252b5132
RH
17530}
17531
8507b6e7
MR
17532/* Given a MIPS16 variant frag FRAGP, return non-zero if it needs
17533 macro expansion. SEC is the section the frag is in. We only
17534 support PC-relative instructions (LA, DLA, LW, LD) here, in
17535 non-PIC code using 32-bit addressing. */
17536
17537static int
17538mips16_macro_frag (fragS *fragp, asection *sec, long stretch)
17539{
17540 const struct mips_pcrel_operand *pcrel_op;
17541 const struct mips_int_operand *operand;
17542 offsetT val;
17543 segT symsec;
17544 int type;
17545
17546 gas_assert (!RELAX_MIPS16_USER_SMALL (fragp->fr_subtype));
17547
17548 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
17549 return 0;
17550 if (!RELAX_MIPS16_SYM32 (fragp->fr_subtype))
17551 return 0;
17552
17553 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
17554 switch (type)
17555 {
17556 case 'A':
17557 case 'B':
17558 case 'E':
17559 symsec = S_GET_SEGMENT (fragp->fr_symbol);
17560 if (bfd_is_abs_section (symsec))
17561 return 1;
17562 if (RELAX_MIPS16_PIC (fragp->fr_subtype))
17563 return 0;
17564 if (S_FORCE_RELOC (fragp->fr_symbol, TRUE) || sec != symsec)
17565 return 1;
17566
17567 operand = mips16_immed_operand (type, TRUE);
17568 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17569 pcrel_op = (const struct mips_pcrel_operand *) operand;
17570 val = mips16_pcrel_val (fragp, pcrel_op, val, stretch);
17571
17572 return !mips16_immed_in_range_p (operand, BFD_RELOC_UNUSED, val);
17573
17574 default:
17575 return 0;
17576 }
17577}
17578
4a6a3df4
AO
17579/* Compute the length of a branch sequence, and adjust the
17580 RELAX_BRANCH_TOOFAR bit accordingly. If FRAGP is NULL, the
17581 worst-case length is computed, with UPDATE being used to indicate
17582 whether an unconditional (-1), branch-likely (+1) or regular (0)
17583 branch is to be computed. */
17584static int
17a2f251 17585relaxed_branch_length (fragS *fragp, asection *sec, int update)
4a6a3df4 17586{
b34976b6 17587 bfd_boolean toofar;
4a6a3df4
AO
17588 int length;
17589
17590 if (fragp
17591 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17592 && !S_IS_WEAK (fragp->fr_symbol)
4a6a3df4
AO
17593 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17594 {
17595 addressT addr;
17596 offsetT val;
17597
17598 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17599
17600 addr = fragp->fr_address + fragp->fr_fix + 4;
17601
17602 val -= addr;
17603
17604 toofar = val < - (0x8000 << 2) || val >= (0x8000 << 2);
17605 }
4a6a3df4 17606 else
c1f61bd2
MR
17607 /* If the symbol is not defined or it's in a different segment,
17608 we emit the long sequence. */
b34976b6 17609 toofar = TRUE;
4a6a3df4
AO
17610
17611 if (fragp && update && toofar != RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
17612 fragp->fr_subtype
66b3e8da 17613 = RELAX_BRANCH_ENCODE (RELAX_BRANCH_AT (fragp->fr_subtype),
ce8ad872 17614 RELAX_BRANCH_PIC (fragp->fr_subtype),
66b3e8da 17615 RELAX_BRANCH_UNCOND (fragp->fr_subtype),
4a6a3df4
AO
17616 RELAX_BRANCH_LIKELY (fragp->fr_subtype),
17617 RELAX_BRANCH_LINK (fragp->fr_subtype),
17618 toofar);
17619
17620 length = 4;
17621 if (toofar)
17622 {
17623 if (fragp ? RELAX_BRANCH_LIKELY (fragp->fr_subtype) : (update > 0))
17624 length += 8;
17625
ce8ad872 17626 if (!fragp || RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
17627 {
17628 /* Additional space for PIC loading of target address. */
17629 length += 8;
17630 if (mips_opts.isa == ISA_MIPS1)
17631 /* Additional space for $at-stabilizing nop. */
17632 length += 4;
17633 }
17634
17635 /* If branch is conditional. */
17636 if (fragp ? !RELAX_BRANCH_UNCOND (fragp->fr_subtype) : (update >= 0))
17637 length += 8;
17638 }
b34976b6 17639
4a6a3df4
AO
17640 return length;
17641}
17642
7bd374a4
MR
17643/* Get a FRAG's branch instruction delay slot size, either from the
17644 short-delay-slot bit of a branch-and-link instruction if AL is TRUE,
17645 or SHORT_INSN_SIZE otherwise. */
17646
17647static int
17648frag_branch_delay_slot_size (fragS *fragp, bfd_boolean al, int short_insn_size)
17649{
17650 char *buf = fragp->fr_literal + fragp->fr_fix;
17651
17652 if (al)
17653 return (read_compressed_insn (buf, 4) & 0x02000000) ? 2 : 4;
17654 else
17655 return short_insn_size;
17656}
17657
df58fc94
RS
17658/* Compute the length of a branch sequence, and adjust the
17659 RELAX_MICROMIPS_TOOFAR32 bit accordingly. If FRAGP is NULL, the
17660 worst-case length is computed, with UPDATE being used to indicate
17661 whether an unconditional (-1), or regular (0) branch is to be
17662 computed. */
17663
17664static int
17665relaxed_micromips_32bit_branch_length (fragS *fragp, asection *sec, int update)
17666{
7bd374a4
MR
17667 bfd_boolean insn32 = TRUE;
17668 bfd_boolean nods = TRUE;
ce8ad872 17669 bfd_boolean pic = TRUE;
7bd374a4
MR
17670 bfd_boolean al = TRUE;
17671 int short_insn_size;
df58fc94
RS
17672 bfd_boolean toofar;
17673 int length;
17674
7bd374a4
MR
17675 if (fragp)
17676 {
17677 insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
17678 nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 17679 pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
7bd374a4
MR
17680 al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
17681 }
17682 short_insn_size = insn32 ? 4 : 2;
17683
df58fc94
RS
17684 if (fragp
17685 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17686 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17687 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17688 {
17689 addressT addr;
17690 offsetT val;
17691
17692 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17693 /* Ignore the low bit in the target, since it will be set
17694 for a text label. */
17695 if ((val & 1) != 0)
17696 --val;
17697
17698 addr = fragp->fr_address + fragp->fr_fix + 4;
17699
17700 val -= addr;
17701
17702 toofar = val < - (0x8000 << 1) || val >= (0x8000 << 1);
17703 }
df58fc94 17704 else
c1f61bd2
MR
17705 /* If the symbol is not defined or it's in a different segment,
17706 we emit the long sequence. */
df58fc94
RS
17707 toofar = TRUE;
17708
17709 if (fragp && update
17710 && toofar != RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
17711 fragp->fr_subtype = (toofar
17712 ? RELAX_MICROMIPS_MARK_TOOFAR32 (fragp->fr_subtype)
17713 : RELAX_MICROMIPS_CLEAR_TOOFAR32 (fragp->fr_subtype));
17714
17715 length = 4;
17716 if (toofar)
17717 {
17718 bfd_boolean compact_known = fragp != NULL;
17719 bfd_boolean compact = FALSE;
17720 bfd_boolean uncond;
17721
df58fc94 17722 if (fragp)
8484fb75
MR
17723 {
17724 compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
17725 uncond = RELAX_MICROMIPS_UNCOND (fragp->fr_subtype);
8484fb75 17726 }
df58fc94
RS
17727 else
17728 uncond = update < 0;
17729
17730 /* If label is out of range, we turn branch <br>:
17731
17732 <br> label # 4 bytes
17733 0:
17734
17735 into:
17736
17737 j label # 4 bytes
8484fb75
MR
17738 nop # 2/4 bytes if
17739 # compact && (!PIC || insn32)
df58fc94
RS
17740 0:
17741 */
ce8ad872 17742 if ((!pic || insn32) && (!compact_known || compact))
8484fb75 17743 length += short_insn_size;
df58fc94
RS
17744
17745 /* If assembling PIC code, we further turn:
17746
17747 j label # 4 bytes
17748
17749 into:
17750
17751 lw/ld at, %got(label)(gp) # 4 bytes
17752 d/addiu at, %lo(label) # 4 bytes
8484fb75 17753 jr/c at # 2/4 bytes
df58fc94 17754 */
ce8ad872 17755 if (pic)
8484fb75 17756 length += 4 + short_insn_size;
df58fc94 17757
7bd374a4
MR
17758 /* Add an extra nop if the jump has no compact form and we need
17759 to fill the delay slot. */
ce8ad872 17760 if ((!pic || al) && nods)
7bd374a4
MR
17761 length += (fragp
17762 ? frag_branch_delay_slot_size (fragp, al, short_insn_size)
17763 : short_insn_size);
17764
df58fc94
RS
17765 /* If branch <br> is conditional, we prepend negated branch <brneg>:
17766
17767 <brneg> 0f # 4 bytes
8484fb75 17768 nop # 2/4 bytes if !compact
df58fc94
RS
17769 */
17770 if (!uncond)
8484fb75 17771 length += (compact_known && compact) ? 4 : 4 + short_insn_size;
df58fc94 17772 }
7bd374a4
MR
17773 else if (nods)
17774 {
17775 /* Add an extra nop to fill the delay slot. */
17776 gas_assert (fragp);
17777 length += frag_branch_delay_slot_size (fragp, al, short_insn_size);
17778 }
df58fc94
RS
17779
17780 return length;
17781}
17782
17783/* Compute the length of a branch, and adjust the RELAX_MICROMIPS_TOOFAR16
17784 bit accordingly. */
17785
17786static int
17787relaxed_micromips_16bit_branch_length (fragS *fragp, asection *sec, int update)
17788{
17789 bfd_boolean toofar;
17790
df58fc94
RS
17791 if (fragp
17792 && S_IS_DEFINED (fragp->fr_symbol)
991f40a9 17793 && !S_IS_WEAK (fragp->fr_symbol)
df58fc94
RS
17794 && sec == S_GET_SEGMENT (fragp->fr_symbol))
17795 {
17796 addressT addr;
17797 offsetT val;
17798 int type;
17799
17800 val = S_GET_VALUE (fragp->fr_symbol) + fragp->fr_offset;
17801 /* Ignore the low bit in the target, since it will be set
17802 for a text label. */
17803 if ((val & 1) != 0)
17804 --val;
17805
17806 /* Assume this is a 2-byte branch. */
17807 addr = fragp->fr_address + fragp->fr_fix + 2;
17808
17809 /* We try to avoid the infinite loop by not adding 2 more bytes for
17810 long branches. */
17811
17812 val -= addr;
17813
17814 type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
17815 if (type == 'D')
17816 toofar = val < - (0x200 << 1) || val >= (0x200 << 1);
17817 else if (type == 'E')
17818 toofar = val < - (0x40 << 1) || val >= (0x40 << 1);
17819 else
17820 abort ();
17821 }
17822 else
17823 /* If the symbol is not defined or it's in a different segment,
17824 we emit a normal 32-bit branch. */
17825 toofar = TRUE;
17826
17827 if (fragp && update
17828 && toofar != RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
17829 fragp->fr_subtype
17830 = toofar ? RELAX_MICROMIPS_MARK_TOOFAR16 (fragp->fr_subtype)
17831 : RELAX_MICROMIPS_CLEAR_TOOFAR16 (fragp->fr_subtype);
17832
17833 if (toofar)
17834 return 4;
17835
17836 return 2;
17837}
17838
252b5132
RH
17839/* Estimate the size of a frag before relaxing. Unless this is the
17840 mips16, we are not really relaxing here, and the final size is
17841 encoded in the subtype information. For the mips16, we have to
17842 decide whether we are using an extended opcode or not. */
17843
252b5132 17844int
17a2f251 17845md_estimate_size_before_relax (fragS *fragp, asection *segtype)
252b5132 17846{
5919d012 17847 int change;
252b5132 17848
4a6a3df4
AO
17849 if (RELAX_BRANCH_P (fragp->fr_subtype))
17850 {
17851
b34976b6
AM
17852 fragp->fr_var = relaxed_branch_length (fragp, segtype, FALSE);
17853
4a6a3df4
AO
17854 return fragp->fr_var;
17855 }
17856
252b5132 17857 if (RELAX_MIPS16_P (fragp->fr_subtype))
8507b6e7
MR
17858 {
17859 /* We don't want to modify the EXTENDED bit here; it might get us
17860 into infinite loops. We change it only in mips_relax_frag(). */
17861 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
25499ac7 17862 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 8 : 12;
8507b6e7
MR
17863 else
17864 return RELAX_MIPS16_EXTENDED (fragp->fr_subtype) ? 4 : 2;
17865 }
252b5132 17866
df58fc94
RS
17867 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
17868 {
17869 int length = 4;
17870
17871 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
17872 length = relaxed_micromips_16bit_branch_length (fragp, segtype, FALSE);
17873 if (length == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
17874 length = relaxed_micromips_32bit_branch_length (fragp, segtype, FALSE);
17875 fragp->fr_var = length;
17876
17877 return length;
17878 }
17879
ce8ad872 17880 if (mips_pic == VXWORKS_PIC)
0a44bf69
RS
17881 /* For vxworks, GOT16 relocations never have a corresponding LO16. */
17882 change = 0;
ce8ad872
MR
17883 else if (RELAX_PIC (fragp->fr_subtype))
17884 change = pic_need_relax (fragp->fr_symbol);
252b5132 17885 else
ce8ad872 17886 change = nopic_need_relax (fragp->fr_symbol, 0);
252b5132
RH
17887
17888 if (change)
17889 {
4d7206a2 17890 fragp->fr_subtype |= RELAX_USE_SECOND;
4d7206a2 17891 return -RELAX_FIRST (fragp->fr_subtype);
252b5132 17892 }
4d7206a2
RS
17893 else
17894 return -RELAX_SECOND (fragp->fr_subtype);
252b5132
RH
17895}
17896
17897/* This is called to see whether a reloc against a defined symbol
de7e6852 17898 should be converted into a reloc against a section. */
252b5132
RH
17899
17900int
17a2f251 17901mips_fix_adjustable (fixS *fixp)
252b5132 17902{
252b5132
RH
17903 if (fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
17904 || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
17905 return 0;
a161fe53 17906
252b5132
RH
17907 if (fixp->fx_addsy == NULL)
17908 return 1;
a161fe53 17909
2f0c68f2
CM
17910 /* Allow relocs used for EH tables. */
17911 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
17912 return 1;
17913
de7e6852
RS
17914 /* If symbol SYM is in a mergeable section, relocations of the form
17915 SYM + 0 can usually be made section-relative. The mergeable data
17916 is then identified by the section offset rather than by the symbol.
17917
17918 However, if we're generating REL LO16 relocations, the offset is split
33eaf5de 17919 between the LO16 and partnering high part relocation. The linker will
de7e6852
RS
17920 need to recalculate the complete offset in order to correctly identify
17921 the merge data.
17922
33eaf5de 17923 The linker has traditionally not looked for the partnering high part
de7e6852
RS
17924 relocation, and has thus allowed orphaned R_MIPS_LO16 relocations to be
17925 placed anywhere. Rather than break backwards compatibility by changing
17926 this, it seems better not to force the issue, and instead keep the
17927 original symbol. This will work with either linker behavior. */
738e5348 17928 if ((lo16_reloc_p (fixp->fx_r_type)
704803a9 17929 || reloc_needs_lo_p (fixp->fx_r_type))
de7e6852
RS
17930 && HAVE_IN_PLACE_ADDENDS
17931 && (S_GET_SEGMENT (fixp->fx_addsy)->flags & SEC_MERGE) != 0)
17932 return 0;
17933
97f50151
MR
17934 /* There is no place to store an in-place offset for JALR relocations. */
17935 if (jalr_reloc_p (fixp->fx_r_type) && HAVE_IN_PLACE_ADDENDS)
17936 return 0;
17937
17938 /* Likewise an in-range offset of limited PC-relative relocations may
2de39019 17939 overflow the in-place relocatable field if recalculated against the
7361da2c
AB
17940 start address of the symbol's containing section.
17941
17942 Also, PC relative relocations for MIPS R6 need to be symbol rather than
17943 section relative to allow linker relaxations to be performed later on. */
97f50151 17944 if (limited_pcrel_reloc_p (fixp->fx_r_type)
912815f0 17945 && (HAVE_IN_PLACE_ADDENDS || ISA_IS_R6 (file_mips_opts.isa)))
1180b5a4
RS
17946 return 0;
17947
b314ec0e
RS
17948 /* R_MIPS16_26 relocations against non-MIPS16 functions might resolve
17949 to a floating-point stub. The same is true for non-R_MIPS16_26
17950 relocations against MIPS16 functions; in this case, the stub becomes
17951 the function's canonical address.
17952
17953 Floating-point stubs are stored in unique .mips16.call.* or
17954 .mips16.fn.* sections. If a stub T for function F is in section S,
17955 the first relocation in section S must be against F; this is how the
17956 linker determines the target function. All relocations that might
17957 resolve to T must also be against F. We therefore have the following
17958 restrictions, which are given in an intentionally-redundant way:
17959
17960 1. We cannot reduce R_MIPS16_26 relocations against non-MIPS16
17961 symbols.
17962
17963 2. We cannot reduce a stub's relocations against non-MIPS16 symbols
17964 if that stub might be used.
17965
17966 3. We cannot reduce non-R_MIPS16_26 relocations against MIPS16
17967 symbols.
17968
17969 4. We cannot reduce a stub's relocations against MIPS16 symbols if
17970 that stub might be used.
17971
17972 There is a further restriction:
17973
df58fc94 17974 5. We cannot reduce jump relocations (R_MIPS_26, R_MIPS16_26 or
0e9c5a5c 17975 R_MICROMIPS_26_S1) or branch relocations (R_MIPS_PC26_S2,
c9775dde
MR
17976 R_MIPS_PC21_S2, R_MIPS_PC16, R_MIPS16_PC16_S1,
17977 R_MICROMIPS_PC16_S1, R_MICROMIPS_PC10_S1 or R_MICROMIPS_PC7_S1)
17978 against MIPS16 or microMIPS symbols because we need to keep the
17979 MIPS16 or microMIPS symbol for the purpose of mode mismatch
a6ebf616
MR
17980 detection and JAL or BAL to JALX instruction conversion in the
17981 linker.
b314ec0e 17982
df58fc94 17983 For simplicity, we deal with (3)-(4) by not reducing _any_ relocation
507dcb32 17984 against a MIPS16 symbol. We deal with (5) by additionally leaving
0e9c5a5c 17985 alone any jump and branch relocations against a microMIPS symbol.
b314ec0e
RS
17986
17987 We deal with (1)-(2) by saying that, if there's a R_MIPS16_26
17988 relocation against some symbol R, no relocation against R may be
17989 reduced. (Note that this deals with (2) as well as (1) because
17990 relocations against global symbols will never be reduced on ELF
17991 targets.) This approach is a little simpler than trying to detect
17992 stub sections, and gives the "all or nothing" per-symbol consistency
17993 that we have for MIPS16 symbols. */
f3ded42a 17994 if (fixp->fx_subsy == NULL
30c09090 17995 && (ELF_ST_IS_MIPS16 (S_GET_OTHER (fixp->fx_addsy))
44d3da23 17996 || (ELF_ST_IS_MICROMIPS (S_GET_OTHER (fixp->fx_addsy))
0e9c5a5c
MR
17997 && (jmp_reloc_p (fixp->fx_r_type)
17998 || b_reloc_p (fixp->fx_r_type)))
44d3da23 17999 || *symbol_get_tc (fixp->fx_addsy)))
252b5132 18000 return 0;
a161fe53 18001
252b5132
RH
18002 return 1;
18003}
18004
18005/* Translate internal representation of relocation info to BFD target
18006 format. */
18007
18008arelent **
17a2f251 18009tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
18010{
18011 static arelent *retval[4];
18012 arelent *reloc;
18013 bfd_reloc_code_real_type code;
18014
4b0cff4e 18015 memset (retval, 0, sizeof(retval));
325801bd
TS
18016 reloc = retval[0] = XCNEW (arelent);
18017 reloc->sym_ptr_ptr = XNEW (asymbol *);
49309057 18018 *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
18019 reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
18020
bad36eac
DJ
18021 if (fixp->fx_pcrel)
18022 {
df58fc94 18023 gas_assert (fixp->fx_r_type == BFD_RELOC_16_PCREL_S2
c9775dde 18024 || fixp->fx_r_type == BFD_RELOC_MIPS16_16_PCREL_S1
df58fc94
RS
18025 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_7_PCREL_S1
18026 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_10_PCREL_S1
b47468a6 18027 || fixp->fx_r_type == BFD_RELOC_MICROMIPS_16_PCREL_S1
7361da2c
AB
18028 || fixp->fx_r_type == BFD_RELOC_32_PCREL
18029 || fixp->fx_r_type == BFD_RELOC_MIPS_21_PCREL_S2
18030 || fixp->fx_r_type == BFD_RELOC_MIPS_26_PCREL_S2
18031 || fixp->fx_r_type == BFD_RELOC_MIPS_18_PCREL_S3
18032 || fixp->fx_r_type == BFD_RELOC_MIPS_19_PCREL_S2
18033 || fixp->fx_r_type == BFD_RELOC_HI16_S_PCREL
18034 || fixp->fx_r_type == BFD_RELOC_LO16_PCREL);
bad36eac
DJ
18035
18036 /* At this point, fx_addnumber is "symbol offset - pcrel address".
18037 Relocations want only the symbol offset. */
51f6035b
MR
18038 switch (fixp->fx_r_type)
18039 {
18040 case BFD_RELOC_MIPS_18_PCREL_S3:
18041 reloc->addend = fixp->fx_addnumber + (reloc->address & ~7);
18042 break;
18043 default:
18044 reloc->addend = fixp->fx_addnumber + reloc->address;
18045 break;
18046 }
bad36eac 18047 }
17c6c9d9
MR
18048 else if (HAVE_IN_PLACE_ADDENDS
18049 && fixp->fx_r_type == BFD_RELOC_MICROMIPS_JMP
18050 && (read_compressed_insn (fixp->fx_frag->fr_literal
18051 + fixp->fx_where, 4) >> 26) == 0x3c)
18052 {
18053 /* Shift is 2, unusually, for microMIPS JALX. Adjust the in-place
18054 addend accordingly. */
18055 reloc->addend = fixp->fx_addnumber >> 1;
18056 }
bad36eac
DJ
18057 else
18058 reloc->addend = fixp->fx_addnumber;
252b5132 18059
438c16b8
TS
18060 /* Since the old MIPS ELF ABI uses Rel instead of Rela, encode the vtable
18061 entry to be used in the relocation's section offset. */
18062 if (! HAVE_NEWABI && fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
252b5132
RH
18063 {
18064 reloc->address = reloc->addend;
18065 reloc->addend = 0;
18066 }
18067
252b5132 18068 code = fixp->fx_r_type;
252b5132 18069
bad36eac 18070 reloc->howto = bfd_reloc_type_lookup (stdoutput, code);
252b5132
RH
18071 if (reloc->howto == NULL)
18072 {
18073 as_bad_where (fixp->fx_file, fixp->fx_line,
1661c76c
RS
18074 _("cannot represent %s relocation in this object file"
18075 " format"),
252b5132
RH
18076 bfd_get_reloc_code_name (code));
18077 retval[0] = NULL;
18078 }
18079
18080 return retval;
18081}
18082
18083/* Relax a machine dependent frag. This returns the amount by which
18084 the current size of the frag should change. */
18085
18086int
17a2f251 18087mips_relax_frag (asection *sec, fragS *fragp, long stretch)
252b5132 18088{
4a6a3df4
AO
18089 if (RELAX_BRANCH_P (fragp->fr_subtype))
18090 {
18091 offsetT old_var = fragp->fr_var;
b34976b6
AM
18092
18093 fragp->fr_var = relaxed_branch_length (fragp, sec, TRUE);
4a6a3df4
AO
18094
18095 return fragp->fr_var - old_var;
18096 }
18097
df58fc94
RS
18098 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18099 {
18100 offsetT old_var = fragp->fr_var;
18101 offsetT new_var = 4;
18102
18103 if (RELAX_MICROMIPS_TYPE (fragp->fr_subtype) != 0)
18104 new_var = relaxed_micromips_16bit_branch_length (fragp, sec, TRUE);
18105 if (new_var == 4 && RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype))
18106 new_var = relaxed_micromips_32bit_branch_length (fragp, sec, TRUE);
18107 fragp->fr_var = new_var;
18108
18109 return new_var - old_var;
18110 }
18111
252b5132
RH
18112 if (! RELAX_MIPS16_P (fragp->fr_subtype))
18113 return 0;
18114
8507b6e7 18115 if (!mips16_extended_frag (fragp, sec, stretch))
252b5132 18116 {
8507b6e7
MR
18117 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18118 {
18119 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
25499ac7 18120 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -6 : -10;
8507b6e7
MR
18121 }
18122 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18123 {
18124 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18125 return -2;
18126 }
18127 else
18128 return 0;
18129 }
18130 else if (!mips16_macro_frag (fragp, sec, stretch))
18131 {
18132 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
18133 {
18134 fragp->fr_subtype = RELAX_MIPS16_CLEAR_MACRO (fragp->fr_subtype);
18135 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
25499ac7 18136 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? -4 : -8;
8507b6e7
MR
18137 }
18138 else if (!RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18139 {
18140 fragp->fr_subtype = RELAX_MIPS16_MARK_EXTENDED (fragp->fr_subtype);
18141 return 2;
18142 }
18143 else
252b5132 18144 return 0;
252b5132
RH
18145 }
18146 else
18147 {
8507b6e7 18148 if (RELAX_MIPS16_MACRO (fragp->fr_subtype))
252b5132 18149 return 0;
8507b6e7
MR
18150 else if (RELAX_MIPS16_EXTENDED (fragp->fr_subtype))
18151 {
18152 fragp->fr_subtype = RELAX_MIPS16_CLEAR_EXTENDED (fragp->fr_subtype);
18153 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18154 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 4 : 8;
8507b6e7
MR
18155 }
18156 else
18157 {
18158 fragp->fr_subtype = RELAX_MIPS16_MARK_MACRO (fragp->fr_subtype);
25499ac7 18159 return RELAX_MIPS16_E2 (fragp->fr_subtype) ? 6 : 10;
8507b6e7 18160 }
252b5132
RH
18161 }
18162
18163 return 0;
18164}
18165
18166/* Convert a machine dependent frag. */
18167
18168void
17a2f251 18169md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT asec, fragS *fragp)
252b5132 18170{
4a6a3df4
AO
18171 if (RELAX_BRANCH_P (fragp->fr_subtype))
18172 {
4d68580a 18173 char *buf;
4a6a3df4 18174 unsigned long insn;
4a6a3df4 18175 fixS *fixp;
b34976b6 18176
4d68580a
RS
18177 buf = fragp->fr_literal + fragp->fr_fix;
18178 insn = read_insn (buf);
b34976b6 18179
4a6a3df4
AO
18180 if (!RELAX_BRANCH_TOOFAR (fragp->fr_subtype))
18181 {
18182 /* We generate a fixup instead of applying it right now
18183 because, if there are linker relaxations, we're going to
18184 need the relocations. */
bbd27b76
MR
18185 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18186 fragp->fr_symbol, fragp->fr_offset,
18187 TRUE, BFD_RELOC_16_PCREL_S2);
4a6a3df4
AO
18188 fixp->fx_file = fragp->fr_file;
18189 fixp->fx_line = fragp->fr_line;
b34976b6 18190
4d68580a 18191 buf = write_insn (buf, insn);
4a6a3df4
AO
18192 }
18193 else
18194 {
18195 int i;
18196
18197 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18198 _("relaxed out-of-range branch into a jump"));
4a6a3df4
AO
18199
18200 if (RELAX_BRANCH_UNCOND (fragp->fr_subtype))
18201 goto uncond;
18202
18203 if (!RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18204 {
18205 /* Reverse the branch. */
18206 switch ((insn >> 28) & 0xf)
18207 {
18208 case 4:
56d438b1
CF
18209 if ((insn & 0xff000000) == 0x47000000
18210 || (insn & 0xff600000) == 0x45600000)
18211 {
18212 /* BZ.df/BNZ.df, BZ.V/BNZ.V can have the condition
18213 reversed by tweaking bit 23. */
18214 insn ^= 0x00800000;
18215 }
18216 else
18217 {
18218 /* bc[0-3][tf]l? instructions can have the condition
18219 reversed by tweaking a single TF bit, and their
18220 opcodes all have 0x4???????. */
18221 gas_assert ((insn & 0xf3e00000) == 0x41000000);
18222 insn ^= 0x00010000;
18223 }
4a6a3df4
AO
18224 break;
18225
18226 case 0:
18227 /* bltz 0x04000000 bgez 0x04010000
54f4ddb3 18228 bltzal 0x04100000 bgezal 0x04110000 */
9c2799c2 18229 gas_assert ((insn & 0xfc0e0000) == 0x04000000);
4a6a3df4
AO
18230 insn ^= 0x00010000;
18231 break;
b34976b6 18232
4a6a3df4
AO
18233 case 1:
18234 /* beq 0x10000000 bne 0x14000000
54f4ddb3 18235 blez 0x18000000 bgtz 0x1c000000 */
4a6a3df4
AO
18236 insn ^= 0x04000000;
18237 break;
18238
18239 default:
18240 abort ();
18241 }
18242 }
18243
18244 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
18245 {
18246 /* Clear the and-link bit. */
9c2799c2 18247 gas_assert ((insn & 0xfc1c0000) == 0x04100000);
4a6a3df4 18248
54f4ddb3
TS
18249 /* bltzal 0x04100000 bgezal 0x04110000
18250 bltzall 0x04120000 bgezall 0x04130000 */
4a6a3df4
AO
18251 insn &= ~0x00100000;
18252 }
18253
18254 /* Branch over the branch (if the branch was likely) or the
18255 full jump (not likely case). Compute the offset from the
18256 current instruction to branch to. */
18257 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18258 i = 16;
18259 else
18260 {
18261 /* How many bytes in instructions we've already emitted? */
4d68580a 18262 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18263 /* How many bytes in instructions from here to the end? */
18264 i = fragp->fr_var - i;
18265 }
18266 /* Convert to instruction count. */
18267 i >>= 2;
18268 /* Branch counts from the next instruction. */
b34976b6 18269 i--;
4a6a3df4
AO
18270 insn |= i;
18271 /* Branch over the jump. */
4d68580a 18272 buf = write_insn (buf, insn);
4a6a3df4 18273
54f4ddb3 18274 /* nop */
4d68580a 18275 buf = write_insn (buf, 0);
4a6a3df4
AO
18276
18277 if (RELAX_BRANCH_LIKELY (fragp->fr_subtype))
18278 {
18279 /* beql $0, $0, 2f */
18280 insn = 0x50000000;
18281 /* Compute the PC offset from the current instruction to
18282 the end of the variable frag. */
18283 /* How many bytes in instructions we've already emitted? */
4d68580a 18284 i = buf - fragp->fr_literal - fragp->fr_fix;
4a6a3df4
AO
18285 /* How many bytes in instructions from here to the end? */
18286 i = fragp->fr_var - i;
18287 /* Convert to instruction count. */
18288 i >>= 2;
18289 /* Don't decrement i, because we want to branch over the
18290 delay slot. */
4a6a3df4 18291 insn |= i;
4a6a3df4 18292
4d68580a
RS
18293 buf = write_insn (buf, insn);
18294 buf = write_insn (buf, 0);
4a6a3df4
AO
18295 }
18296
18297 uncond:
ce8ad872 18298 if (!RELAX_BRANCH_PIC (fragp->fr_subtype))
4a6a3df4
AO
18299 {
18300 /* j or jal. */
18301 insn = (RELAX_BRANCH_LINK (fragp->fr_subtype)
18302 ? 0x0c000000 : 0x08000000);
4a6a3df4 18303
bbd27b76
MR
18304 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18305 fragp->fr_symbol, fragp->fr_offset,
18306 FALSE, BFD_RELOC_MIPS_JMP);
4a6a3df4
AO
18307 fixp->fx_file = fragp->fr_file;
18308 fixp->fx_line = fragp->fr_line;
18309
4d68580a 18310 buf = write_insn (buf, insn);
4a6a3df4
AO
18311 }
18312 else
18313 {
66b3e8da
MR
18314 unsigned long at = RELAX_BRANCH_AT (fragp->fr_subtype);
18315
4a6a3df4 18316 /* lw/ld $at, <sym>($gp) R_MIPS_GOT16 */
66b3e8da
MR
18317 insn = HAVE_64BIT_ADDRESSES ? 0xdf800000 : 0x8f800000;
18318 insn |= at << OP_SH_RT;
4a6a3df4 18319
bbd27b76
MR
18320 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18321 fragp->fr_symbol, fragp->fr_offset,
18322 FALSE, BFD_RELOC_MIPS_GOT16);
4a6a3df4
AO
18323 fixp->fx_file = fragp->fr_file;
18324 fixp->fx_line = fragp->fr_line;
18325
4d68580a 18326 buf = write_insn (buf, insn);
b34976b6 18327
4a6a3df4 18328 if (mips_opts.isa == ISA_MIPS1)
4d68580a
RS
18329 /* nop */
18330 buf = write_insn (buf, 0);
4a6a3df4
AO
18331
18332 /* d/addiu $at, $at, <sym> R_MIPS_LO16 */
66b3e8da
MR
18333 insn = HAVE_64BIT_ADDRESSES ? 0x64000000 : 0x24000000;
18334 insn |= at << OP_SH_RS | at << OP_SH_RT;
4a6a3df4 18335
bbd27b76
MR
18336 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18337 fragp->fr_symbol, fragp->fr_offset,
18338 FALSE, BFD_RELOC_LO16);
4a6a3df4
AO
18339 fixp->fx_file = fragp->fr_file;
18340 fixp->fx_line = fragp->fr_line;
b34976b6 18341
4d68580a 18342 buf = write_insn (buf, insn);
4a6a3df4
AO
18343
18344 /* j(al)r $at. */
18345 if (RELAX_BRANCH_LINK (fragp->fr_subtype))
66b3e8da 18346 insn = 0x0000f809;
4a6a3df4 18347 else
66b3e8da
MR
18348 insn = 0x00000008;
18349 insn |= at << OP_SH_RS;
4a6a3df4 18350
4d68580a 18351 buf = write_insn (buf, insn);
4a6a3df4
AO
18352 }
18353 }
18354
4a6a3df4 18355 fragp->fr_fix += fragp->fr_var;
4d68580a 18356 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
4a6a3df4
AO
18357 return;
18358 }
18359
df58fc94
RS
18360 /* Relax microMIPS branches. */
18361 if (RELAX_MICROMIPS_P (fragp->fr_subtype))
18362 {
4d68580a 18363 char *buf = fragp->fr_literal + fragp->fr_fix;
df58fc94 18364 bfd_boolean compact = RELAX_MICROMIPS_COMPACT (fragp->fr_subtype);
8484fb75 18365 bfd_boolean insn32 = RELAX_MICROMIPS_INSN32 (fragp->fr_subtype);
7bd374a4 18366 bfd_boolean nods = RELAX_MICROMIPS_NODS (fragp->fr_subtype);
ce8ad872 18367 bfd_boolean pic = RELAX_MICROMIPS_PIC (fragp->fr_subtype);
df58fc94
RS
18368 bfd_boolean al = RELAX_MICROMIPS_LINK (fragp->fr_subtype);
18369 int type = RELAX_MICROMIPS_TYPE (fragp->fr_subtype);
2309ddf2 18370 bfd_boolean short_ds;
df58fc94 18371 unsigned long insn;
df58fc94
RS
18372 fixS *fixp;
18373
df58fc94
RS
18374 fragp->fr_fix += fragp->fr_var;
18375
18376 /* Handle 16-bit branches that fit or are forced to fit. */
18377 if (type != 0 && !RELAX_MICROMIPS_TOOFAR16 (fragp->fr_subtype))
18378 {
18379 /* We generate a fixup instead of applying it right now,
18380 because if there is linker relaxation, we're going to
18381 need the relocations. */
834a65aa
MR
18382 switch (type)
18383 {
18384 case 'D':
18385 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18386 fragp->fr_symbol, fragp->fr_offset,
18387 TRUE, BFD_RELOC_MICROMIPS_10_PCREL_S1);
18388 break;
18389 case 'E':
18390 fixp = fix_new (fragp, buf - fragp->fr_literal, 2,
18391 fragp->fr_symbol, fragp->fr_offset,
18392 TRUE, BFD_RELOC_MICROMIPS_7_PCREL_S1);
18393 break;
18394 default:
18395 abort ();
18396 }
df58fc94
RS
18397
18398 fixp->fx_file = fragp->fr_file;
18399 fixp->fx_line = fragp->fr_line;
18400
18401 /* These relocations can have an addend that won't fit in
18402 2 octets. */
18403 fixp->fx_no_overflow = 1;
18404
18405 return;
18406 }
18407
2309ddf2 18408 /* Handle 32-bit branches that fit or are forced to fit. */
df58fc94
RS
18409 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18410 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18411 {
18412 /* We generate a fixup instead of applying it right now,
18413 because if there is linker relaxation, we're going to
18414 need the relocations. */
bbd27b76
MR
18415 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18416 fragp->fr_symbol, fragp->fr_offset,
18417 TRUE, BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18418 fixp->fx_file = fragp->fr_file;
18419 fixp->fx_line = fragp->fr_line;
18420
18421 if (type == 0)
7bd374a4
MR
18422 {
18423 insn = read_compressed_insn (buf, 4);
18424 buf += 4;
18425
18426 if (nods)
18427 {
18428 /* Check the short-delay-slot bit. */
18429 if (!al || (insn & 0x02000000) != 0)
18430 buf = write_compressed_insn (buf, 0x0c00, 2);
18431 else
18432 buf = write_compressed_insn (buf, 0x00000000, 4);
18433 }
18434
18435 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
18436 return;
18437 }
df58fc94
RS
18438 }
18439
18440 /* Relax 16-bit branches to 32-bit branches. */
18441 if (type != 0)
18442 {
4d68580a 18443 insn = read_compressed_insn (buf, 2);
df58fc94
RS
18444
18445 if ((insn & 0xfc00) == 0xcc00) /* b16 */
18446 insn = 0x94000000; /* beq */
18447 else if ((insn & 0xdc00) == 0x8c00) /* beqz16/bnez16 */
18448 {
18449 unsigned long regno;
18450
18451 regno = (insn >> MICROMIPSOP_SH_MD) & MICROMIPSOP_MASK_MD;
18452 regno = micromips_to_32_reg_d_map [regno];
18453 insn = ((insn & 0x2000) << 16) | 0x94000000; /* beq/bne */
18454 insn |= regno << MICROMIPSOP_SH_RS;
18455 }
18456 else
18457 abort ();
18458
18459 /* Nothing else to do, just write it out. */
18460 if (!RELAX_MICROMIPS_RELAX32 (fragp->fr_subtype)
18461 || !RELAX_MICROMIPS_TOOFAR32 (fragp->fr_subtype))
18462 {
4d68580a 18463 buf = write_compressed_insn (buf, insn, 4);
7bd374a4
MR
18464 if (nods)
18465 buf = write_compressed_insn (buf, 0x0c00, 2);
4d68580a 18466 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18467 return;
18468 }
18469 }
18470 else
4d68580a 18471 insn = read_compressed_insn (buf, 4);
df58fc94
RS
18472
18473 /* Relax 32-bit branches to a sequence of instructions. */
18474 as_warn_where (fragp->fr_file, fragp->fr_line,
1661c76c 18475 _("relaxed out-of-range branch into a jump"));
df58fc94 18476
2309ddf2 18477 /* Set the short-delay-slot bit. */
7bd374a4 18478 short_ds = !al || (insn & 0x02000000) != 0;
df58fc94
RS
18479
18480 if (!RELAX_MICROMIPS_UNCOND (fragp->fr_subtype))
18481 {
18482 symbolS *l;
18483
18484 /* Reverse the branch. */
18485 if ((insn & 0xfc000000) == 0x94000000 /* beq */
18486 || (insn & 0xfc000000) == 0xb4000000) /* bne */
18487 insn ^= 0x20000000;
18488 else if ((insn & 0xffe00000) == 0x40000000 /* bltz */
18489 || (insn & 0xffe00000) == 0x40400000 /* bgez */
18490 || (insn & 0xffe00000) == 0x40800000 /* blez */
18491 || (insn & 0xffe00000) == 0x40c00000 /* bgtz */
18492 || (insn & 0xffe00000) == 0x40a00000 /* bnezc */
18493 || (insn & 0xffe00000) == 0x40e00000 /* beqzc */
18494 || (insn & 0xffe00000) == 0x40200000 /* bltzal */
18495 || (insn & 0xffe00000) == 0x40600000 /* bgezal */
18496 || (insn & 0xffe00000) == 0x42200000 /* bltzals */
18497 || (insn & 0xffe00000) == 0x42600000) /* bgezals */
18498 insn ^= 0x00400000;
18499 else if ((insn & 0xffe30000) == 0x43800000 /* bc1f */
18500 || (insn & 0xffe30000) == 0x43a00000 /* bc1t */
18501 || (insn & 0xffe30000) == 0x42800000 /* bc2f */
18502 || (insn & 0xffe30000) == 0x42a00000) /* bc2t */
18503 insn ^= 0x00200000;
56d438b1
CF
18504 else if ((insn & 0xff000000) == 0x83000000 /* BZ.df
18505 BNZ.df */
18506 || (insn & 0xff600000) == 0x81600000) /* BZ.V
18507 BNZ.V */
18508 insn ^= 0x00800000;
df58fc94
RS
18509 else
18510 abort ();
18511
18512 if (al)
18513 {
18514 /* Clear the and-link and short-delay-slot bits. */
18515 gas_assert ((insn & 0xfda00000) == 0x40200000);
18516
18517 /* bltzal 0x40200000 bgezal 0x40600000 */
18518 /* bltzals 0x42200000 bgezals 0x42600000 */
18519 insn &= ~0x02200000;
18520 }
18521
18522 /* Make a label at the end for use with the branch. */
18523 l = symbol_new (micromips_label_name (), asec, fragp->fr_fix, fragp);
18524 micromips_label_inc ();
f3ded42a 18525 S_SET_OTHER (l, ELF_ST_SET_MICROMIPS (S_GET_OTHER (l)));
df58fc94
RS
18526
18527 /* Refer to it. */
4d68580a
RS
18528 fixp = fix_new (fragp, buf - fragp->fr_literal, 4, l, 0, TRUE,
18529 BFD_RELOC_MICROMIPS_16_PCREL_S1);
df58fc94
RS
18530 fixp->fx_file = fragp->fr_file;
18531 fixp->fx_line = fragp->fr_line;
18532
18533 /* Branch over the jump. */
4d68580a 18534 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18535
df58fc94 18536 if (!compact)
8484fb75
MR
18537 {
18538 /* nop */
18539 if (insn32)
18540 buf = write_compressed_insn (buf, 0x00000000, 4);
18541 else
18542 buf = write_compressed_insn (buf, 0x0c00, 2);
18543 }
df58fc94
RS
18544 }
18545
ce8ad872 18546 if (!pic)
df58fc94 18547 {
7bd374a4
MR
18548 unsigned long jal = (short_ds || nods
18549 ? 0x74000000 : 0xf4000000); /* jal/s */
2309ddf2 18550
df58fc94
RS
18551 /* j/jal/jals <sym> R_MICROMIPS_26_S1 */
18552 insn = al ? jal : 0xd4000000;
18553
bbd27b76
MR
18554 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18555 fragp->fr_symbol, fragp->fr_offset,
18556 FALSE, BFD_RELOC_MICROMIPS_JMP);
df58fc94
RS
18557 fixp->fx_file = fragp->fr_file;
18558 fixp->fx_line = fragp->fr_line;
18559
4d68580a 18560 buf = write_compressed_insn (buf, insn, 4);
8484fb75 18561
7bd374a4 18562 if (compact || nods)
8484fb75
MR
18563 {
18564 /* nop */
18565 if (insn32)
18566 buf = write_compressed_insn (buf, 0x00000000, 4);
18567 else
18568 buf = write_compressed_insn (buf, 0x0c00, 2);
18569 }
df58fc94
RS
18570 }
18571 else
18572 {
18573 unsigned long at = RELAX_MICROMIPS_AT (fragp->fr_subtype);
18574
18575 /* lw/ld $at, <sym>($gp) R_MICROMIPS_GOT16 */
18576 insn = HAVE_64BIT_ADDRESSES ? 0xdc1c0000 : 0xfc1c0000;
18577 insn |= at << MICROMIPSOP_SH_RT;
18578
bbd27b76
MR
18579 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18580 fragp->fr_symbol, fragp->fr_offset,
18581 FALSE, BFD_RELOC_MICROMIPS_GOT16);
df58fc94
RS
18582 fixp->fx_file = fragp->fr_file;
18583 fixp->fx_line = fragp->fr_line;
18584
4d68580a 18585 buf = write_compressed_insn (buf, insn, 4);
df58fc94
RS
18586
18587 /* d/addiu $at, $at, <sym> R_MICROMIPS_LO16 */
18588 insn = HAVE_64BIT_ADDRESSES ? 0x5c000000 : 0x30000000;
18589 insn |= at << MICROMIPSOP_SH_RT | at << MICROMIPSOP_SH_RS;
18590
bbd27b76
MR
18591 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18592 fragp->fr_symbol, fragp->fr_offset,
18593 FALSE, BFD_RELOC_MICROMIPS_LO16);
df58fc94
RS
18594 fixp->fx_file = fragp->fr_file;
18595 fixp->fx_line = fragp->fr_line;
18596
4d68580a 18597 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18598
8484fb75
MR
18599 if (insn32)
18600 {
18601 /* jr/jalr $at */
18602 insn = 0x00000f3c | (al ? RA : ZERO) << MICROMIPSOP_SH_RT;
18603 insn |= at << MICROMIPSOP_SH_RS;
18604
18605 buf = write_compressed_insn (buf, insn, 4);
df58fc94 18606
7bd374a4 18607 if (compact || nods)
8484fb75
MR
18608 /* nop */
18609 buf = write_compressed_insn (buf, 0x00000000, 4);
18610 }
18611 else
18612 {
18613 /* jr/jrc/jalr/jalrs $at */
18614 unsigned long jalr = short_ds ? 0x45e0 : 0x45c0; /* jalr/s */
7bd374a4 18615 unsigned long jr = compact || nods ? 0x45a0 : 0x4580; /* jr/c */
8484fb75
MR
18616
18617 insn = al ? jalr : jr;
18618 insn |= at << MICROMIPSOP_SH_MJ;
18619
18620 buf = write_compressed_insn (buf, insn, 2);
7bd374a4
MR
18621 if (al && nods)
18622 {
18623 /* nop */
18624 if (short_ds)
18625 buf = write_compressed_insn (buf, 0x0c00, 2);
18626 else
18627 buf = write_compressed_insn (buf, 0x00000000, 4);
18628 }
8484fb75 18629 }
df58fc94
RS
18630 }
18631
4d68580a 18632 gas_assert (buf == fragp->fr_literal + fragp->fr_fix);
df58fc94
RS
18633 return;
18634 }
18635
252b5132
RH
18636 if (RELAX_MIPS16_P (fragp->fr_subtype))
18637 {
18638 int type;
3ccad066 18639 const struct mips_int_operand *operand;
252b5132 18640 offsetT val;
5c04167a 18641 char *buf;
8507b6e7 18642 unsigned int user_length;
9d862524 18643 bfd_boolean need_reloc;
252b5132 18644 unsigned long insn;
8507b6e7 18645 bfd_boolean mac;
5c04167a 18646 bfd_boolean ext;
88a7ef16 18647 segT symsec;
252b5132
RH
18648
18649 type = RELAX_MIPS16_TYPE (fragp->fr_subtype);
3ccad066 18650 operand = mips16_immed_operand (type, FALSE);
252b5132 18651
8507b6e7 18652 mac = RELAX_MIPS16_MACRO (fragp->fr_subtype);
5c04167a 18653 ext = RELAX_MIPS16_EXTENDED (fragp->fr_subtype);
88a7ef16 18654 val = resolve_symbol_value (fragp->fr_symbol) + fragp->fr_offset;
9d862524
MR
18655
18656 symsec = S_GET_SEGMENT (fragp->fr_symbol);
18657 need_reloc = (S_FORCE_RELOC (fragp->fr_symbol, TRUE)
8507b6e7 18658 || (operand->root.type == OP_PCREL && !mac
9d862524
MR
18659 ? asec != symsec
18660 : !bfd_is_abs_section (symsec)));
18661
8507b6e7 18662 if (operand->root.type == OP_PCREL && !mac)
252b5132 18663 {
3ccad066 18664 const struct mips_pcrel_operand *pcrel_op;
252b5132 18665
3ccad066 18666 pcrel_op = (const struct mips_pcrel_operand *) operand;
252b5132 18667
14f72d45 18668 if (pcrel_op->include_isa_bit && !need_reloc)
252b5132 18669 {
37b2d327
MR
18670 if (!mips_ignore_branch_isa
18671 && !ELF_ST_IS_MIPS16 (S_GET_OTHER (fragp->fr_symbol)))
14f72d45
MR
18672 as_bad_where (fragp->fr_file, fragp->fr_line,
18673 _("branch to a symbol in another ISA mode"));
18674 else if ((fragp->fr_offset & 0x1) != 0)
18675 as_bad_where (fragp->fr_file, fragp->fr_line,
18676 _("branch to misaligned address (0x%lx)"),
18677 (long) val);
252b5132 18678 }
252b5132 18679
14f72d45 18680 val = mips16_pcrel_val (fragp, pcrel_op, val, 0);
252b5132
RH
18681
18682 /* Make sure the section winds up with the alignment we have
18683 assumed. */
3ccad066
RS
18684 if (operand->shift > 0)
18685 record_alignment (asec, operand->shift);
252b5132
RH
18686 }
18687
8507b6e7
MR
18688 if (RELAX_MIPS16_JAL_DSLOT (fragp->fr_subtype)
18689 || RELAX_MIPS16_DSLOT (fragp->fr_subtype))
18690 {
18691 if (mac)
18692 as_warn_where (fragp->fr_file, fragp->fr_line,
18693 _("macro instruction expanded into multiple "
18694 "instructions in a branch delay slot"));
18695 else if (ext)
18696 as_warn_where (fragp->fr_file, fragp->fr_line,
18697 _("extended instruction in a branch delay slot"));
18698 }
18699 else if (RELAX_MIPS16_NOMACRO (fragp->fr_subtype) && mac)
252b5132 18700 as_warn_where (fragp->fr_file, fragp->fr_line,
8507b6e7
MR
18701 _("macro instruction expanded into multiple "
18702 "instructions"));
252b5132 18703
5c04167a 18704 buf = fragp->fr_literal + fragp->fr_fix;
252b5132 18705
4d68580a 18706 insn = read_compressed_insn (buf, 2);
5c04167a
RS
18707 if (ext)
18708 insn |= MIPS16_EXTEND;
252b5132 18709
5c04167a
RS
18710 if (RELAX_MIPS16_USER_EXT (fragp->fr_subtype))
18711 user_length = 4;
18712 else if (RELAX_MIPS16_USER_SMALL (fragp->fr_subtype))
18713 user_length = 2;
18714 else
18715 user_length = 0;
18716
8507b6e7 18717 if (mac)
c9775dde 18718 {
8507b6e7
MR
18719 unsigned long reg;
18720 unsigned long new;
18721 unsigned long op;
25499ac7 18722 bfd_boolean e2;
8507b6e7
MR
18723
18724 gas_assert (type == 'A' || type == 'B' || type == 'E');
18725 gas_assert (RELAX_MIPS16_SYM32 (fragp->fr_subtype));
c9775dde 18726
25499ac7
MR
18727 e2 = RELAX_MIPS16_E2 (fragp->fr_subtype);
18728
8507b6e7 18729 if (need_reloc)
c9775dde 18730 {
8507b6e7
MR
18731 fixS *fixp;
18732
18733 gas_assert (!RELAX_MIPS16_PIC (fragp->fr_subtype));
18734
18735 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18736 fragp->fr_symbol, fragp->fr_offset,
18737 FALSE, BFD_RELOC_MIPS16_HI16_S);
18738 fixp->fx_file = fragp->fr_file;
18739 fixp->fx_line = fragp->fr_line;
18740
25499ac7 18741 fixp = fix_new (fragp, buf - fragp->fr_literal + (e2 ? 4 : 8), 4,
8507b6e7
MR
18742 fragp->fr_symbol, fragp->fr_offset,
18743 FALSE, BFD_RELOC_MIPS16_LO16);
18744 fixp->fx_file = fragp->fr_file;
18745 fixp->fx_line = fragp->fr_line;
18746
18747 val = 0;
18748 }
18749
18750 switch (insn & 0xf800)
18751 {
18752 case 0x0800: /* ADDIU */
18753 reg = (insn >> 8) & 0x7;
18754 op = 0xf0004800 | (reg << 8);
c9775dde 18755 break;
8507b6e7
MR
18756 case 0xb000: /* LW */
18757 reg = (insn >> 8) & 0x7;
18758 op = 0xf0009800 | (reg << 8) | (reg << 5);
c9775dde 18759 break;
8507b6e7
MR
18760 case 0xf800: /* I64 */
18761 reg = (insn >> 5) & 0x7;
18762 switch (insn & 0x0700)
18763 {
18764 case 0x0400: /* LD */
18765 op = 0xf0003800 | (reg << 8) | (reg << 5);
18766 break;
18767 case 0x0600: /* DADDIU */
18768 op = 0xf000fd00 | (reg << 5);
18769 break;
18770 default:
18771 abort ();
18772 }
18773 break;
18774 default:
18775 abort ();
c9775dde 18776 }
8507b6e7 18777
25499ac7 18778 new = (e2 ? 0xf0006820 : 0xf0006800) | (reg << 8); /* LUI/LI */
8507b6e7
MR
18779 new |= mips16_immed_extend ((val + 0x8000) >> 16, 16);
18780 buf = write_compressed_insn (buf, new, 4);
25499ac7
MR
18781 if (!e2)
18782 {
18783 new = 0xf4003000 | (reg << 8) | (reg << 5); /* SLL */
18784 buf = write_compressed_insn (buf, new, 4);
18785 }
8507b6e7
MR
18786 op |= mips16_immed_extend (val, 16);
18787 buf = write_compressed_insn (buf, op, 4);
18788
25499ac7 18789 fragp->fr_fix += e2 ? 8 : 12;
8507b6e7
MR
18790 }
18791 else
18792 {
18793 unsigned int length = ext ? 4 : 2;
18794
18795 if (need_reloc)
c9775dde 18796 {
8507b6e7 18797 bfd_reloc_code_real_type reloc = BFD_RELOC_NONE;
8507b6e7 18798 fixS *fixp;
c9775dde 18799
8507b6e7
MR
18800 switch (type)
18801 {
18802 case 'p':
18803 case 'q':
18804 reloc = BFD_RELOC_MIPS16_16_PCREL_S1;
18805 break;
18806 default:
18807 break;
18808 }
18809 if (mac || reloc == BFD_RELOC_NONE)
18810 as_bad_where (fragp->fr_file, fragp->fr_line,
18811 _("unsupported relocation"));
18812 else if (ext)
18813 {
bbd27b76
MR
18814 fixp = fix_new (fragp, buf - fragp->fr_literal, 4,
18815 fragp->fr_symbol, fragp->fr_offset,
18816 TRUE, reloc);
8507b6e7
MR
18817 fixp->fx_file = fragp->fr_file;
18818 fixp->fx_line = fragp->fr_line;
18819 }
18820 else
18821 as_bad_where (fragp->fr_file, fragp->fr_line,
18822 _("invalid unextended operand value"));
c9775dde 18823 }
eefc3365 18824 else
8507b6e7
MR
18825 mips16_immed (fragp->fr_file, fragp->fr_line, type,
18826 BFD_RELOC_UNUSED, val, user_length, &insn);
252b5132 18827
8507b6e7
MR
18828 gas_assert (mips16_opcode_length (insn) == length);
18829 write_compressed_insn (buf, insn, length);
18830 fragp->fr_fix += length;
18831 }
252b5132
RH
18832 }
18833 else
18834 {
df58fc94
RS
18835 relax_substateT subtype = fragp->fr_subtype;
18836 bfd_boolean second_longer = (subtype & RELAX_SECOND_LONGER) != 0;
18837 bfd_boolean use_second = (subtype & RELAX_USE_SECOND) != 0;
4d7206a2
RS
18838 int first, second;
18839 fixS *fixp;
252b5132 18840
df58fc94
RS
18841 first = RELAX_FIRST (subtype);
18842 second = RELAX_SECOND (subtype);
4d7206a2 18843 fixp = (fixS *) fragp->fr_opcode;
252b5132 18844
df58fc94
RS
18845 /* If the delay slot chosen does not match the size of the instruction,
18846 then emit a warning. */
18847 if ((!use_second && (subtype & RELAX_DELAY_SLOT_SIZE_FIRST) != 0)
18848 || (use_second && (subtype & RELAX_DELAY_SLOT_SIZE_SECOND) != 0))
18849 {
18850 relax_substateT s;
18851 const char *msg;
18852
18853 s = subtype & (RELAX_DELAY_SLOT_16BIT
18854 | RELAX_DELAY_SLOT_SIZE_FIRST
18855 | RELAX_DELAY_SLOT_SIZE_SECOND);
18856 msg = macro_warning (s);
18857 if (msg != NULL)
db9b2be4 18858 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94
RS
18859 subtype &= ~s;
18860 }
18861
584892a6 18862 /* Possibly emit a warning if we've chosen the longer option. */
df58fc94 18863 if (use_second == second_longer)
584892a6 18864 {
df58fc94
RS
18865 relax_substateT s;
18866 const char *msg;
18867
18868 s = (subtype
18869 & (RELAX_SECOND_LONGER | RELAX_NOMACRO | RELAX_DELAY_SLOT));
18870 msg = macro_warning (s);
18871 if (msg != NULL)
db9b2be4 18872 as_warn_where (fragp->fr_file, fragp->fr_line, "%s", msg);
df58fc94 18873 subtype &= ~s;
584892a6
RS
18874 }
18875
4d7206a2
RS
18876 /* Go through all the fixups for the first sequence. Disable them
18877 (by marking them as done) if we're going to use the second
18878 sequence instead. */
18879 while (fixp
18880 && fixp->fx_frag == fragp
18881 && fixp->fx_where < fragp->fr_fix - second)
18882 {
df58fc94 18883 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18884 fixp->fx_done = 1;
18885 fixp = fixp->fx_next;
18886 }
252b5132 18887
4d7206a2
RS
18888 /* Go through the fixups for the second sequence. Disable them if
18889 we're going to use the first sequence, otherwise adjust their
18890 addresses to account for the relaxation. */
18891 while (fixp && fixp->fx_frag == fragp)
18892 {
df58fc94 18893 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18894 fixp->fx_where -= first;
18895 else
18896 fixp->fx_done = 1;
18897 fixp = fixp->fx_next;
18898 }
18899
18900 /* Now modify the frag contents. */
df58fc94 18901 if (subtype & RELAX_USE_SECOND)
4d7206a2
RS
18902 {
18903 char *start;
18904
18905 start = fragp->fr_literal + fragp->fr_fix - first - second;
18906 memmove (start, start + first, second);
18907 fragp->fr_fix -= first;
18908 }
18909 else
18910 fragp->fr_fix -= second;
252b5132
RH
18911 }
18912}
18913
252b5132
RH
18914/* This function is called after the relocs have been generated.
18915 We've been storing mips16 text labels as odd. Here we convert them
18916 back to even for the convenience of the debugger. */
18917
18918void
17a2f251 18919mips_frob_file_after_relocs (void)
252b5132
RH
18920{
18921 asymbol **syms;
18922 unsigned int count, i;
18923
252b5132
RH
18924 syms = bfd_get_outsymbols (stdoutput);
18925 count = bfd_get_symcount (stdoutput);
18926 for (i = 0; i < count; i++, syms++)
df58fc94
RS
18927 if (ELF_ST_IS_COMPRESSED (elf_symbol (*syms)->internal_elf_sym.st_other)
18928 && ((*syms)->value & 1) != 0)
18929 {
18930 (*syms)->value &= ~1;
18931 /* If the symbol has an odd size, it was probably computed
18932 incorrectly, so adjust that as well. */
18933 if ((elf_symbol (*syms)->internal_elf_sym.st_size & 1) != 0)
18934 ++elf_symbol (*syms)->internal_elf_sym.st_size;
18935 }
252b5132
RH
18936}
18937
a1facbec
MR
18938/* This function is called whenever a label is defined, including fake
18939 labels instantiated off the dot special symbol. It is used when
18940 handling branch delays; if a branch has a label, we assume we cannot
18941 move it. This also bumps the value of the symbol by 1 in compressed
18942 code. */
252b5132 18943
e1b47bd5 18944static void
a1facbec 18945mips_record_label (symbolS *sym)
252b5132 18946{
a8dbcb85 18947 segment_info_type *si = seg_info (now_seg);
252b5132
RH
18948 struct insn_label_list *l;
18949
18950 if (free_insn_labels == NULL)
325801bd 18951 l = XNEW (struct insn_label_list);
252b5132
RH
18952 else
18953 {
18954 l = free_insn_labels;
18955 free_insn_labels = l->next;
18956 }
18957
18958 l->label = sym;
a8dbcb85
TS
18959 l->next = si->label_list;
18960 si->label_list = l;
a1facbec 18961}
07a53e5c 18962
a1facbec
MR
18963/* This function is called as tc_frob_label() whenever a label is defined
18964 and adds a DWARF-2 record we only want for true labels. */
18965
18966void
18967mips_define_label (symbolS *sym)
18968{
18969 mips_record_label (sym);
07a53e5c 18970 dwarf2_emit_label (sym);
252b5132 18971}
e1b47bd5
RS
18972
18973/* This function is called by tc_new_dot_label whenever a new dot symbol
18974 is defined. */
18975
18976void
18977mips_add_dot_label (symbolS *sym)
18978{
18979 mips_record_label (sym);
18980 if (mips_assembling_insn && HAVE_CODE_COMPRESSION)
18981 mips_compressed_mark_label (sym);
18982}
252b5132 18983\f
351cdf24
MF
18984/* Converting ASE flags from internal to .MIPS.abiflags values. */
18985static unsigned int
18986mips_convert_ase_flags (int ase)
18987{
18988 unsigned int ext_ases = 0;
18989
18990 if (ase & ASE_DSP)
18991 ext_ases |= AFL_ASE_DSP;
18992 if (ase & ASE_DSPR2)
18993 ext_ases |= AFL_ASE_DSPR2;
8f4f9071
MF
18994 if (ase & ASE_DSPR3)
18995 ext_ases |= AFL_ASE_DSPR3;
351cdf24
MF
18996 if (ase & ASE_EVA)
18997 ext_ases |= AFL_ASE_EVA;
18998 if (ase & ASE_MCU)
18999 ext_ases |= AFL_ASE_MCU;
19000 if (ase & ASE_MDMX)
19001 ext_ases |= AFL_ASE_MDMX;
19002 if (ase & ASE_MIPS3D)
19003 ext_ases |= AFL_ASE_MIPS3D;
19004 if (ase & ASE_MT)
19005 ext_ases |= AFL_ASE_MT;
19006 if (ase & ASE_SMARTMIPS)
19007 ext_ases |= AFL_ASE_SMARTMIPS;
19008 if (ase & ASE_VIRT)
19009 ext_ases |= AFL_ASE_VIRT;
19010 if (ase & ASE_MSA)
19011 ext_ases |= AFL_ASE_MSA;
19012 if (ase & ASE_XPA)
19013 ext_ases |= AFL_ASE_XPA;
25499ac7
MR
19014 if (ase & ASE_MIPS16E2)
19015 ext_ases |= file_ase_mips16 ? AFL_ASE_MIPS16E2 : 0;
730c3174
SE
19016 if (ase & ASE_CRC)
19017 ext_ases |= AFL_ASE_CRC;
6f20c942
FS
19018 if (ase & ASE_GINV)
19019 ext_ases |= AFL_ASE_GINV;
351cdf24
MF
19020
19021 return ext_ases;
19022}
252b5132
RH
19023/* Some special processing for a MIPS ELF file. */
19024
19025void
17a2f251 19026mips_elf_final_processing (void)
252b5132 19027{
351cdf24
MF
19028 int fpabi;
19029 Elf_Internal_ABIFlags_v0 flags;
19030
19031 flags.version = 0;
19032 flags.isa_rev = 0;
19033 switch (file_mips_opts.isa)
19034 {
19035 case INSN_ISA1:
19036 flags.isa_level = 1;
19037 break;
19038 case INSN_ISA2:
19039 flags.isa_level = 2;
19040 break;
19041 case INSN_ISA3:
19042 flags.isa_level = 3;
19043 break;
19044 case INSN_ISA4:
19045 flags.isa_level = 4;
19046 break;
19047 case INSN_ISA5:
19048 flags.isa_level = 5;
19049 break;
19050 case INSN_ISA32:
19051 flags.isa_level = 32;
19052 flags.isa_rev = 1;
19053 break;
19054 case INSN_ISA32R2:
19055 flags.isa_level = 32;
19056 flags.isa_rev = 2;
19057 break;
19058 case INSN_ISA32R3:
19059 flags.isa_level = 32;
19060 flags.isa_rev = 3;
19061 break;
19062 case INSN_ISA32R5:
19063 flags.isa_level = 32;
19064 flags.isa_rev = 5;
19065 break;
09c14161
MF
19066 case INSN_ISA32R6:
19067 flags.isa_level = 32;
19068 flags.isa_rev = 6;
19069 break;
351cdf24
MF
19070 case INSN_ISA64:
19071 flags.isa_level = 64;
19072 flags.isa_rev = 1;
19073 break;
19074 case INSN_ISA64R2:
19075 flags.isa_level = 64;
19076 flags.isa_rev = 2;
19077 break;
19078 case INSN_ISA64R3:
19079 flags.isa_level = 64;
19080 flags.isa_rev = 3;
19081 break;
19082 case INSN_ISA64R5:
19083 flags.isa_level = 64;
19084 flags.isa_rev = 5;
19085 break;
09c14161
MF
19086 case INSN_ISA64R6:
19087 flags.isa_level = 64;
19088 flags.isa_rev = 6;
19089 break;
351cdf24
MF
19090 }
19091
19092 flags.gpr_size = file_mips_opts.gp == 32 ? AFL_REG_32 : AFL_REG_64;
19093 flags.cpr1_size = file_mips_opts.soft_float ? AFL_REG_NONE
19094 : (file_mips_opts.ase & ASE_MSA) ? AFL_REG_128
19095 : (file_mips_opts.fp == 64) ? AFL_REG_64
19096 : AFL_REG_32;
19097 flags.cpr2_size = AFL_REG_NONE;
19098 flags.fp_abi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19099 Tag_GNU_MIPS_ABI_FP);
19100 flags.isa_ext = bfd_mips_isa_ext (stdoutput);
19101 flags.ases = mips_convert_ase_flags (file_mips_opts.ase);
19102 if (file_ase_mips16)
19103 flags.ases |= AFL_ASE_MIPS16;
19104 if (file_ase_micromips)
19105 flags.ases |= AFL_ASE_MICROMIPS;
19106 flags.flags1 = 0;
19107 if ((ISA_HAS_ODD_SINGLE_FPR (file_mips_opts.isa, file_mips_opts.arch)
19108 || file_mips_opts.fp == 64)
19109 && file_mips_opts.oddspreg)
19110 flags.flags1 |= AFL_FLAGS1_ODDSPREG;
19111 flags.flags2 = 0;
19112
19113 bfd_mips_elf_swap_abiflags_v0_out (stdoutput, &flags,
19114 ((Elf_External_ABIFlags_v0 *)
19115 mips_flags_frag));
19116
252b5132 19117 /* Write out the register information. */
316f5878 19118 if (mips_abi != N64_ABI)
252b5132
RH
19119 {
19120 Elf32_RegInfo s;
19121
19122 s.ri_gprmask = mips_gprmask;
19123 s.ri_cprmask[0] = mips_cprmask[0];
19124 s.ri_cprmask[1] = mips_cprmask[1];
19125 s.ri_cprmask[2] = mips_cprmask[2];
19126 s.ri_cprmask[3] = mips_cprmask[3];
19127 /* The gp_value field is set by the MIPS ELF backend. */
19128
19129 bfd_mips_elf32_swap_reginfo_out (stdoutput, &s,
19130 ((Elf32_External_RegInfo *)
19131 mips_regmask_frag));
19132 }
19133 else
19134 {
19135 Elf64_Internal_RegInfo s;
19136
19137 s.ri_gprmask = mips_gprmask;
19138 s.ri_pad = 0;
19139 s.ri_cprmask[0] = mips_cprmask[0];
19140 s.ri_cprmask[1] = mips_cprmask[1];
19141 s.ri_cprmask[2] = mips_cprmask[2];
19142 s.ri_cprmask[3] = mips_cprmask[3];
19143 /* The gp_value field is set by the MIPS ELF backend. */
19144
19145 bfd_mips_elf64_swap_reginfo_out (stdoutput, &s,
19146 ((Elf64_External_RegInfo *)
19147 mips_regmask_frag));
19148 }
19149
19150 /* Set the MIPS ELF flag bits. FIXME: There should probably be some
19151 sort of BFD interface for this. */
19152 if (mips_any_noreorder)
19153 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NOREORDER;
19154 if (mips_pic != NO_PIC)
143d77c5 19155 {
8b828383 19156 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_PIC;
143d77c5
EC
19157 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
19158 }
19159 if (mips_abicalls)
19160 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_CPIC;
252b5132 19161
b015e599
AP
19162 /* Set MIPS ELF flags for ASEs. Note that not all ASEs have flags
19163 defined at present; this might need to change in future. */
a4672219
TS
19164 if (file_ase_mips16)
19165 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_M16;
df58fc94
RS
19166 if (file_ase_micromips)
19167 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MICROMIPS;
919731af 19168 if (file_mips_opts.ase & ASE_MDMX)
deec1734 19169 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_ARCH_ASE_MDMX;
1f25f5d3 19170
bdaaa2e1 19171 /* Set the MIPS ELF ABI flags. */
316f5878 19172 if (mips_abi == O32_ABI && USE_E_MIPS_ABI_O32)
252b5132 19173 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O32;
316f5878 19174 else if (mips_abi == O64_ABI)
252b5132 19175 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_O64;
316f5878 19176 else if (mips_abi == EABI_ABI)
252b5132 19177 {
bad1aba3 19178 if (file_mips_opts.gp == 64)
252b5132
RH
19179 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI64;
19180 else
19181 elf_elfheader (stdoutput)->e_flags |= E_MIPS_ABI_EABI32;
19182 }
be00bddd 19183
defc8e2b 19184 /* Nothing to do for N32_ABI or N64_ABI. */
252b5132
RH
19185
19186 if (mips_32bitmode)
19187 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_32BITMODE;
ad3fea08 19188
7361da2c 19189 if (mips_nan2008 == 1)
ba92f887
MR
19190 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_NAN2008;
19191
ad3fea08 19192 /* 32 bit code with 64 bit FP registers. */
351cdf24
MF
19193 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
19194 Tag_GNU_MIPS_ABI_FP);
19195 if (fpabi == Val_GNU_MIPS_ABI_FP_OLD_64)
f1c38003 19196 elf_elfheader (stdoutput)->e_flags |= EF_MIPS_FP64;
252b5132 19197}
252b5132 19198\f
beae10d5 19199typedef struct proc {
9b2f1d35
EC
19200 symbolS *func_sym;
19201 symbolS *func_end_sym;
beae10d5
KH
19202 unsigned long reg_mask;
19203 unsigned long reg_offset;
19204 unsigned long fpreg_mask;
19205 unsigned long fpreg_offset;
19206 unsigned long frame_offset;
19207 unsigned long frame_reg;
19208 unsigned long pc_reg;
19209} procS;
252b5132
RH
19210
19211static procS cur_proc;
19212static procS *cur_proc_ptr;
19213static int numprocs;
19214
df58fc94
RS
19215/* Implement NOP_OPCODE. We encode a MIPS16 nop as "1", a microMIPS nop
19216 as "2", and a normal nop as "0". */
19217
19218#define NOP_OPCODE_MIPS 0
19219#define NOP_OPCODE_MIPS16 1
19220#define NOP_OPCODE_MICROMIPS 2
742a56fe
RS
19221
19222char
19223mips_nop_opcode (void)
19224{
df58fc94
RS
19225 if (seg_info (now_seg)->tc_segment_info_data.micromips)
19226 return NOP_OPCODE_MICROMIPS;
19227 else if (seg_info (now_seg)->tc_segment_info_data.mips16)
19228 return NOP_OPCODE_MIPS16;
19229 else
19230 return NOP_OPCODE_MIPS;
742a56fe
RS
19231}
19232
df58fc94
RS
19233/* Fill in an rs_align_code fragment. Unlike elsewhere we want to use
19234 32-bit microMIPS NOPs here (if applicable). */
a19d8eb0 19235
0a9ef439 19236void
17a2f251 19237mips_handle_align (fragS *fragp)
a19d8eb0 19238{
df58fc94 19239 char nop_opcode;
742a56fe 19240 char *p;
c67a084a
NC
19241 int bytes, size, excess;
19242 valueT opcode;
742a56fe 19243
0a9ef439
RH
19244 if (fragp->fr_type != rs_align_code)
19245 return;
19246
742a56fe 19247 p = fragp->fr_literal + fragp->fr_fix;
df58fc94
RS
19248 nop_opcode = *p;
19249 switch (nop_opcode)
a19d8eb0 19250 {
df58fc94
RS
19251 case NOP_OPCODE_MICROMIPS:
19252 opcode = micromips_nop32_insn.insn_opcode;
19253 size = 4;
19254 break;
19255 case NOP_OPCODE_MIPS16:
c67a084a
NC
19256 opcode = mips16_nop_insn.insn_opcode;
19257 size = 2;
df58fc94
RS
19258 break;
19259 case NOP_OPCODE_MIPS:
19260 default:
c67a084a
NC
19261 opcode = nop_insn.insn_opcode;
19262 size = 4;
df58fc94 19263 break;
c67a084a 19264 }
a19d8eb0 19265
c67a084a
NC
19266 bytes = fragp->fr_next->fr_address - fragp->fr_address - fragp->fr_fix;
19267 excess = bytes % size;
df58fc94
RS
19268
19269 /* Handle the leading part if we're not inserting a whole number of
19270 instructions, and make it the end of the fixed part of the frag.
19271 Try to fit in a short microMIPS NOP if applicable and possible,
19272 and use zeroes otherwise. */
19273 gas_assert (excess < 4);
19274 fragp->fr_fix += excess;
19275 switch (excess)
c67a084a 19276 {
df58fc94
RS
19277 case 3:
19278 *p++ = '\0';
19279 /* Fall through. */
19280 case 2:
833794fc 19281 if (nop_opcode == NOP_OPCODE_MICROMIPS && !mips_opts.insn32)
df58fc94 19282 {
4d68580a 19283 p = write_compressed_insn (p, micromips_nop16_insn.insn_opcode, 2);
df58fc94
RS
19284 break;
19285 }
19286 *p++ = '\0';
19287 /* Fall through. */
19288 case 1:
19289 *p++ = '\0';
19290 /* Fall through. */
19291 case 0:
19292 break;
a19d8eb0 19293 }
c67a084a
NC
19294
19295 md_number_to_chars (p, opcode, size);
19296 fragp->fr_var = size;
a19d8eb0
CP
19297}
19298
252b5132 19299static long
17a2f251 19300get_number (void)
252b5132
RH
19301{
19302 int negative = 0;
19303 long val = 0;
19304
19305 if (*input_line_pointer == '-')
19306 {
19307 ++input_line_pointer;
19308 negative = 1;
19309 }
3882b010 19310 if (!ISDIGIT (*input_line_pointer))
956cd1d6 19311 as_bad (_("expected simple number"));
252b5132
RH
19312 if (input_line_pointer[0] == '0')
19313 {
19314 if (input_line_pointer[1] == 'x')
19315 {
19316 input_line_pointer += 2;
3882b010 19317 while (ISXDIGIT (*input_line_pointer))
252b5132
RH
19318 {
19319 val <<= 4;
19320 val |= hex_value (*input_line_pointer++);
19321 }
19322 return negative ? -val : val;
19323 }
19324 else
19325 {
19326 ++input_line_pointer;
3882b010 19327 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19328 {
19329 val <<= 3;
19330 val |= *input_line_pointer++ - '0';
19331 }
19332 return negative ? -val : val;
19333 }
19334 }
3882b010 19335 if (!ISDIGIT (*input_line_pointer))
252b5132
RH
19336 {
19337 printf (_(" *input_line_pointer == '%c' 0x%02x\n"),
19338 *input_line_pointer, *input_line_pointer);
956cd1d6 19339 as_warn (_("invalid number"));
252b5132
RH
19340 return -1;
19341 }
3882b010 19342 while (ISDIGIT (*input_line_pointer))
252b5132
RH
19343 {
19344 val *= 10;
19345 val += *input_line_pointer++ - '0';
19346 }
19347 return negative ? -val : val;
19348}
19349
19350/* The .file directive; just like the usual .file directive, but there
c5dd6aab
DJ
19351 is an initial number which is the ECOFF file index. In the non-ECOFF
19352 case .file implies DWARF-2. */
19353
19354static void
17a2f251 19355s_mips_file (int x ATTRIBUTE_UNUSED)
c5dd6aab 19356{
ecb4347a
DJ
19357 static int first_file_directive = 0;
19358
c5dd6aab
DJ
19359 if (ECOFF_DEBUGGING)
19360 {
19361 get_number ();
19362 s_app_file (0);
19363 }
19364 else
ecb4347a
DJ
19365 {
19366 char *filename;
19367
68d20676 19368 filename = dwarf2_directive_filename ();
ecb4347a
DJ
19369
19370 /* Versions of GCC up to 3.1 start files with a ".file"
19371 directive even for stabs output. Make sure that this
19372 ".file" is handled. Note that you need a version of GCC
19373 after 3.1 in order to support DWARF-2 on MIPS. */
19374 if (filename != NULL && ! first_file_directive)
19375 {
19376 (void) new_logical_line (filename, -1);
c04f5787 19377 s_app_file_string (filename, 0);
ecb4347a
DJ
19378 }
19379 first_file_directive = 1;
19380 }
c5dd6aab
DJ
19381}
19382
19383/* The .loc directive, implying DWARF-2. */
252b5132
RH
19384
19385static void
17a2f251 19386s_mips_loc (int x ATTRIBUTE_UNUSED)
252b5132 19387{
c5dd6aab
DJ
19388 if (!ECOFF_DEBUGGING)
19389 dwarf2_directive_loc (0);
252b5132
RH
19390}
19391
252b5132
RH
19392/* The .end directive. */
19393
19394static void
17a2f251 19395s_mips_end (int x ATTRIBUTE_UNUSED)
252b5132
RH
19396{
19397 symbolS *p;
252b5132 19398
7a621144
DJ
19399 /* Following functions need their own .frame and .cprestore directives. */
19400 mips_frame_reg_valid = 0;
19401 mips_cprestore_valid = 0;
19402
252b5132
RH
19403 if (!is_end_of_line[(unsigned char) *input_line_pointer])
19404 {
19405 p = get_symbol ();
19406 demand_empty_rest_of_line ();
19407 }
19408 else
19409 p = NULL;
19410
14949570 19411 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
252b5132
RH
19412 as_warn (_(".end not in text section"));
19413
19414 if (!cur_proc_ptr)
19415 {
1661c76c 19416 as_warn (_(".end directive without a preceding .ent directive"));
252b5132
RH
19417 demand_empty_rest_of_line ();
19418 return;
19419 }
19420
19421 if (p != NULL)
19422 {
9c2799c2 19423 gas_assert (S_GET_NAME (p));
9b2f1d35 19424 if (strcmp (S_GET_NAME (p), S_GET_NAME (cur_proc_ptr->func_sym)))
1661c76c 19425 as_warn (_(".end symbol does not match .ent symbol"));
ecb4347a
DJ
19426
19427 if (debug_type == DEBUG_STABS)
19428 stabs_generate_asm_endfunc (S_GET_NAME (p),
19429 S_GET_NAME (p));
252b5132
RH
19430 }
19431 else
19432 as_warn (_(".end directive missing or unknown symbol"));
19433
9b2f1d35
EC
19434 /* Create an expression to calculate the size of the function. */
19435 if (p && cur_proc_ptr)
19436 {
19437 OBJ_SYMFIELD_TYPE *obj = symbol_get_obj (p);
325801bd 19438 expressionS *exp = XNEW (expressionS);
9b2f1d35
EC
19439
19440 obj->size = exp;
19441 exp->X_op = O_subtract;
19442 exp->X_add_symbol = symbol_temp_new_now ();
19443 exp->X_op_symbol = p;
19444 exp->X_add_number = 0;
19445
19446 cur_proc_ptr->func_end_sym = exp->X_add_symbol;
19447 }
19448
5ff6a06c
MR
19449#ifdef md_flush_pending_output
19450 md_flush_pending_output ();
19451#endif
19452
ecb4347a 19453 /* Generate a .pdr section. */
f3ded42a 19454 if (!ECOFF_DEBUGGING && mips_flag_pdr)
ecb4347a
DJ
19455 {
19456 segT saved_seg = now_seg;
19457 subsegT saved_subseg = now_subseg;
ecb4347a
DJ
19458 expressionS exp;
19459 char *fragp;
252b5132 19460
9c2799c2 19461 gas_assert (pdr_seg);
ecb4347a 19462 subseg_set (pdr_seg, 0);
252b5132 19463
ecb4347a
DJ
19464 /* Write the symbol. */
19465 exp.X_op = O_symbol;
19466 exp.X_add_symbol = p;
19467 exp.X_add_number = 0;
19468 emit_expr (&exp, 4);
252b5132 19469
ecb4347a 19470 fragp = frag_more (7 * 4);
252b5132 19471
17a2f251
TS
19472 md_number_to_chars (fragp, cur_proc_ptr->reg_mask, 4);
19473 md_number_to_chars (fragp + 4, cur_proc_ptr->reg_offset, 4);
19474 md_number_to_chars (fragp + 8, cur_proc_ptr->fpreg_mask, 4);
19475 md_number_to_chars (fragp + 12, cur_proc_ptr->fpreg_offset, 4);
19476 md_number_to_chars (fragp + 16, cur_proc_ptr->frame_offset, 4);
19477 md_number_to_chars (fragp + 20, cur_proc_ptr->frame_reg, 4);
19478 md_number_to_chars (fragp + 24, cur_proc_ptr->pc_reg, 4);
252b5132 19479
ecb4347a
DJ
19480 subseg_set (saved_seg, saved_subseg);
19481 }
252b5132
RH
19482
19483 cur_proc_ptr = NULL;
19484}
19485
19486/* The .aent and .ent directives. */
19487
19488static void
17a2f251 19489s_mips_ent (int aent)
252b5132 19490{
252b5132 19491 symbolS *symbolP;
252b5132
RH
19492
19493 symbolP = get_symbol ();
19494 if (*input_line_pointer == ',')
f9419b05 19495 ++input_line_pointer;
252b5132 19496 SKIP_WHITESPACE ();
3882b010 19497 if (ISDIGIT (*input_line_pointer)
d9a62219 19498 || *input_line_pointer == '-')
874e8986 19499 get_number ();
252b5132 19500
14949570 19501 if ((bfd_get_section_flags (stdoutput, now_seg) & SEC_CODE) == 0)
1661c76c 19502 as_warn (_(".ent or .aent not in text section"));
252b5132
RH
19503
19504 if (!aent && cur_proc_ptr)
9a41af64 19505 as_warn (_("missing .end"));
252b5132
RH
19506
19507 if (!aent)
19508 {
7a621144
DJ
19509 /* This function needs its own .frame and .cprestore directives. */
19510 mips_frame_reg_valid = 0;
19511 mips_cprestore_valid = 0;
19512
252b5132
RH
19513 cur_proc_ptr = &cur_proc;
19514 memset (cur_proc_ptr, '\0', sizeof (procS));
19515
9b2f1d35 19516 cur_proc_ptr->func_sym = symbolP;
252b5132 19517
f9419b05 19518 ++numprocs;
ecb4347a
DJ
19519
19520 if (debug_type == DEBUG_STABS)
19521 stabs_generate_asm_func (S_GET_NAME (symbolP),
19522 S_GET_NAME (symbolP));
252b5132
RH
19523 }
19524
7c0fc524
MR
19525 symbol_get_bfdsym (symbolP)->flags |= BSF_FUNCTION;
19526
252b5132
RH
19527 demand_empty_rest_of_line ();
19528}
19529
19530/* The .frame directive. If the mdebug section is present (IRIX 5 native)
bdaaa2e1 19531 then ecoff.c (ecoff_directive_frame) is used. For embedded targets,
252b5132 19532 s_mips_frame is used so that we can set the PDR information correctly.
bdaaa2e1 19533 We can't use the ecoff routines because they make reference to the ecoff
252b5132
RH
19534 symbol table (in the mdebug section). */
19535
19536static void
17a2f251 19537s_mips_frame (int ignore ATTRIBUTE_UNUSED)
252b5132 19538{
f3ded42a
RS
19539 if (ECOFF_DEBUGGING)
19540 s_ignore (ignore);
19541 else
ecb4347a
DJ
19542 {
19543 long val;
252b5132 19544
ecb4347a
DJ
19545 if (cur_proc_ptr == (procS *) NULL)
19546 {
19547 as_warn (_(".frame outside of .ent"));
19548 demand_empty_rest_of_line ();
19549 return;
19550 }
252b5132 19551
ecb4347a
DJ
19552 cur_proc_ptr->frame_reg = tc_get_register (1);
19553
19554 SKIP_WHITESPACE ();
19555 if (*input_line_pointer++ != ','
19556 || get_absolute_expression_and_terminator (&val) != ',')
19557 {
1661c76c 19558 as_warn (_("bad .frame directive"));
ecb4347a
DJ
19559 --input_line_pointer;
19560 demand_empty_rest_of_line ();
19561 return;
19562 }
252b5132 19563
ecb4347a
DJ
19564 cur_proc_ptr->frame_offset = val;
19565 cur_proc_ptr->pc_reg = tc_get_register (0);
252b5132 19566
252b5132 19567 demand_empty_rest_of_line ();
252b5132 19568 }
252b5132
RH
19569}
19570
bdaaa2e1
KH
19571/* The .fmask and .mask directives. If the mdebug section is present
19572 (IRIX 5 native) then ecoff.c (ecoff_directive_mask) is used. For
252b5132 19573 embedded targets, s_mips_mask is used so that we can set the PDR
bdaaa2e1 19574 information correctly. We can't use the ecoff routines because they
252b5132
RH
19575 make reference to the ecoff symbol table (in the mdebug section). */
19576
19577static void
17a2f251 19578s_mips_mask (int reg_type)
252b5132 19579{
f3ded42a
RS
19580 if (ECOFF_DEBUGGING)
19581 s_ignore (reg_type);
19582 else
252b5132 19583 {
ecb4347a 19584 long mask, off;
252b5132 19585
ecb4347a
DJ
19586 if (cur_proc_ptr == (procS *) NULL)
19587 {
19588 as_warn (_(".mask/.fmask outside of .ent"));
19589 demand_empty_rest_of_line ();
19590 return;
19591 }
252b5132 19592
ecb4347a
DJ
19593 if (get_absolute_expression_and_terminator (&mask) != ',')
19594 {
1661c76c 19595 as_warn (_("bad .mask/.fmask directive"));
ecb4347a
DJ
19596 --input_line_pointer;
19597 demand_empty_rest_of_line ();
19598 return;
19599 }
252b5132 19600
ecb4347a
DJ
19601 off = get_absolute_expression ();
19602
19603 if (reg_type == 'F')
19604 {
19605 cur_proc_ptr->fpreg_mask = mask;
19606 cur_proc_ptr->fpreg_offset = off;
19607 }
19608 else
19609 {
19610 cur_proc_ptr->reg_mask = mask;
19611 cur_proc_ptr->reg_offset = off;
19612 }
19613
19614 demand_empty_rest_of_line ();
252b5132 19615 }
252b5132
RH
19616}
19617
316f5878
RS
19618/* A table describing all the processors gas knows about. Names are
19619 matched in the order listed.
e7af610e 19620
316f5878
RS
19621 To ease comparison, please keep this table in the same order as
19622 gcc's mips_cpu_info_table[]. */
e972090a
NC
19623static const struct mips_cpu_info mips_cpu_info_table[] =
19624{
316f5878 19625 /* Entries for generic ISAs */
d16afab6
RS
19626 { "mips1", MIPS_CPU_IS_ISA, 0, ISA_MIPS1, CPU_R3000 },
19627 { "mips2", MIPS_CPU_IS_ISA, 0, ISA_MIPS2, CPU_R6000 },
19628 { "mips3", MIPS_CPU_IS_ISA, 0, ISA_MIPS3, CPU_R4000 },
19629 { "mips4", MIPS_CPU_IS_ISA, 0, ISA_MIPS4, CPU_R8000 },
19630 { "mips5", MIPS_CPU_IS_ISA, 0, ISA_MIPS5, CPU_MIPS5 },
19631 { "mips32", MIPS_CPU_IS_ISA, 0, ISA_MIPS32, CPU_MIPS32 },
19632 { "mips32r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
ae52f483
AB
19633 { "mips32r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R3, CPU_MIPS32R3 },
19634 { "mips32r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R5, CPU_MIPS32R5 },
7361da2c 19635 { "mips32r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS32R6, CPU_MIPS32R6 },
d16afab6
RS
19636 { "mips64", MIPS_CPU_IS_ISA, 0, ISA_MIPS64, CPU_MIPS64 },
19637 { "mips64r2", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R2, CPU_MIPS64R2 },
ae52f483
AB
19638 { "mips64r3", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R3, CPU_MIPS64R3 },
19639 { "mips64r5", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R5, CPU_MIPS64R5 },
7361da2c 19640 { "mips64r6", MIPS_CPU_IS_ISA, 0, ISA_MIPS64R6, CPU_MIPS64R6 },
316f5878
RS
19641
19642 /* MIPS I */
d16afab6
RS
19643 { "r3000", 0, 0, ISA_MIPS1, CPU_R3000 },
19644 { "r2000", 0, 0, ISA_MIPS1, CPU_R3000 },
19645 { "r3900", 0, 0, ISA_MIPS1, CPU_R3900 },
316f5878
RS
19646
19647 /* MIPS II */
d16afab6 19648 { "r6000", 0, 0, ISA_MIPS2, CPU_R6000 },
316f5878
RS
19649
19650 /* MIPS III */
d16afab6
RS
19651 { "r4000", 0, 0, ISA_MIPS3, CPU_R4000 },
19652 { "r4010", 0, 0, ISA_MIPS2, CPU_R4010 },
19653 { "vr4100", 0, 0, ISA_MIPS3, CPU_VR4100 },
19654 { "vr4111", 0, 0, ISA_MIPS3, CPU_R4111 },
19655 { "vr4120", 0, 0, ISA_MIPS3, CPU_VR4120 },
19656 { "vr4130", 0, 0, ISA_MIPS3, CPU_VR4120 },
19657 { "vr4181", 0, 0, ISA_MIPS3, CPU_R4111 },
19658 { "vr4300", 0, 0, ISA_MIPS3, CPU_R4300 },
19659 { "r4400", 0, 0, ISA_MIPS3, CPU_R4400 },
19660 { "r4600", 0, 0, ISA_MIPS3, CPU_R4600 },
19661 { "orion", 0, 0, ISA_MIPS3, CPU_R4600 },
19662 { "r4650", 0, 0, ISA_MIPS3, CPU_R4650 },
19663 { "r5900", 0, 0, ISA_MIPS3, CPU_R5900 },
b15591bb 19664 /* ST Microelectronics Loongson 2E and 2F cores */
d16afab6
RS
19665 { "loongson2e", 0, 0, ISA_MIPS3, CPU_LOONGSON_2E },
19666 { "loongson2f", 0, 0, ISA_MIPS3, CPU_LOONGSON_2F },
316f5878
RS
19667
19668 /* MIPS IV */
d16afab6
RS
19669 { "r8000", 0, 0, ISA_MIPS4, CPU_R8000 },
19670 { "r10000", 0, 0, ISA_MIPS4, CPU_R10000 },
19671 { "r12000", 0, 0, ISA_MIPS4, CPU_R12000 },
19672 { "r14000", 0, 0, ISA_MIPS4, CPU_R14000 },
19673 { "r16000", 0, 0, ISA_MIPS4, CPU_R16000 },
19674 { "vr5000", 0, 0, ISA_MIPS4, CPU_R5000 },
19675 { "vr5400", 0, 0, ISA_MIPS4, CPU_VR5400 },
19676 { "vr5500", 0, 0, ISA_MIPS4, CPU_VR5500 },
19677 { "rm5200", 0, 0, ISA_MIPS4, CPU_R5000 },
19678 { "rm5230", 0, 0, ISA_MIPS4, CPU_R5000 },
19679 { "rm5231", 0, 0, ISA_MIPS4, CPU_R5000 },
19680 { "rm5261", 0, 0, ISA_MIPS4, CPU_R5000 },
19681 { "rm5721", 0, 0, ISA_MIPS4, CPU_R5000 },
19682 { "rm7000", 0, 0, ISA_MIPS4, CPU_RM7000 },
19683 { "rm9000", 0, 0, ISA_MIPS4, CPU_RM9000 },
316f5878
RS
19684
19685 /* MIPS 32 */
d16afab6
RS
19686 { "4kc", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19687 { "4km", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19688 { "4kp", 0, 0, ISA_MIPS32, CPU_MIPS32 },
19689 { "4ksc", 0, ASE_SMARTMIPS, ISA_MIPS32, CPU_MIPS32 },
ad3fea08
TS
19690
19691 /* MIPS 32 Release 2 */
d16afab6
RS
19692 { "4kec", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19693 { "4kem", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19694 { "4kep", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19695 { "4ksd", 0, ASE_SMARTMIPS, ISA_MIPS32R2, CPU_MIPS32R2 },
19696 { "m4k", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19697 { "m4kp", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19698 { "m14k", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19699 { "m14kc", 0, ASE_MCU, ISA_MIPS32R2, CPU_MIPS32R2 },
19700 { "m14ke", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19701 ISA_MIPS32R2, CPU_MIPS32R2 },
19702 { "m14kec", 0, ASE_DSP | ASE_DSPR2 | ASE_MCU,
19703 ISA_MIPS32R2, CPU_MIPS32R2 },
19704 { "24kc", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19705 { "24kf2_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19706 { "24kf", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19707 { "24kf1_1", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19708 /* Deprecated forms of the above. */
d16afab6
RS
19709 { "24kfx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
19710 { "24kx", 0, 0, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19711 /* 24KE is a 24K with DSP ASE, other ASEs are optional. */
d16afab6
RS
19712 { "24kec", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19713 { "24kef2_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19714 { "24kef", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19715 { "24kef1_1", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19716 /* Deprecated forms of the above. */
d16afab6
RS
19717 { "24kefx", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
19718 { "24kex", 0, ASE_DSP, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19719 /* 34K is a 24K with DSP and MT ASE, other ASEs are optional. */
d16afab6
RS
19720 { "34kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19721 { "34kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19722 { "34kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19723 { "34kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19724 /* Deprecated forms of the above. */
d16afab6
RS
19725 { "34kfx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19726 { "34kx", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
711eefe4 19727 /* 34Kn is a 34kc without DSP. */
d16afab6 19728 { "34kn", 0, ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
01fd108f 19729 /* 74K with DSP and DSPR2 ASE, other ASEs are optional. */
d16afab6
RS
19730 { "74kc", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19731 { "74kf2_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19732 { "74kf", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19733 { "74kf1_1", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19734 { "74kf3_2", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
0fdf1951 19735 /* Deprecated forms of the above. */
d16afab6
RS
19736 { "74kfx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
19737 { "74kx", 0, ASE_DSP | ASE_DSPR2, ISA_MIPS32R2, CPU_MIPS32R2 },
30f8113a 19738 /* 1004K cores are multiprocessor versions of the 34K. */
d16afab6
RS
19739 { "1004kc", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19740 { "1004kf2_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19741 { "1004kf", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
19742 { "1004kf1_1", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
77403ce9
RS
19743 /* interaptiv is the new name for 1004kf */
19744 { "interaptiv", 0, ASE_DSP | ASE_MT, ISA_MIPS32R2, CPU_MIPS32R2 },
38bf472a
MR
19745 { "interaptiv-mr2", 0,
19746 ASE_DSP | ASE_EVA | ASE_MT | ASE_MIPS16E2 | ASE_MIPS16E2_MT,
19747 ISA_MIPS32R3, CPU_INTERAPTIV_MR2 },
c6e5c03a
RS
19748 /* M5100 family */
19749 { "m5100", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
19750 { "m5101", 0, ASE_MCU, ISA_MIPS32R5, CPU_MIPS32R5 },
bbaa46c0 19751 /* P5600 with EVA and Virtualization ASEs, other ASEs are optional. */
134c0c8b 19752 { "p5600", 0, ASE_VIRT | ASE_EVA | ASE_XPA, ISA_MIPS32R5, CPU_MIPS32R5 },
32b26a03 19753
316f5878 19754 /* MIPS 64 */
d16afab6
RS
19755 { "5kc", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19756 { "5kf", 0, 0, ISA_MIPS64, CPU_MIPS64 },
19757 { "20kc", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
19758 { "25kf", 0, ASE_MIPS3D, ISA_MIPS64, CPU_MIPS64 },
ad3fea08 19759
c7a23324 19760 /* Broadcom SB-1 CPU core */
d16afab6 19761 { "sb1", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
1e85aad8 19762 /* Broadcom SB-1A CPU core */
d16afab6 19763 { "sb1a", 0, ASE_MIPS3D | ASE_MDMX, ISA_MIPS64, CPU_SB1 },
3739860c 19764
4ba154f5 19765 { "loongson3a", 0, 0, ISA_MIPS64R2, CPU_LOONGSON_3A },
e7af610e 19766
ed163775
MR
19767 /* MIPS 64 Release 2 */
19768
967344c6 19769 /* Cavium Networks Octeon CPU core */
d16afab6
RS
19770 { "octeon", 0, 0, ISA_MIPS64R2, CPU_OCTEON },
19771 { "octeon+", 0, 0, ISA_MIPS64R2, CPU_OCTEONP },
19772 { "octeon2", 0, 0, ISA_MIPS64R2, CPU_OCTEON2 },
2c629856 19773 { "octeon3", 0, ASE_VIRT | ASE_VIRT64, ISA_MIPS64R5, CPU_OCTEON3 },
967344c6 19774
52b6b6b9 19775 /* RMI Xlr */
d16afab6 19776 { "xlr", 0, 0, ISA_MIPS64, CPU_XLR },
52b6b6b9 19777
55a36193
MK
19778 /* Broadcom XLP.
19779 XLP is mostly like XLR, with the prominent exception that it is
19780 MIPS64R2 rather than MIPS64. */
d16afab6 19781 { "xlp", 0, 0, ISA_MIPS64R2, CPU_XLR },
55a36193 19782
a4968f42 19783 /* MIPS 64 Release 6 */
7ef0d297 19784 { "i6400", 0, ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
a4968f42 19785 { "p6600", 0, ASE_VIRT | ASE_MSA, ISA_MIPS64R6, CPU_MIPS64R6},
7ef0d297 19786
316f5878 19787 /* End marker */
d16afab6 19788 { NULL, 0, 0, 0, 0 }
316f5878 19789};
e7af610e 19790
84ea6cf2 19791
316f5878
RS
19792/* Return true if GIVEN is the same as CANONICAL, or if it is CANONICAL
19793 with a final "000" replaced by "k". Ignore case.
e7af610e 19794
316f5878 19795 Note: this function is shared between GCC and GAS. */
c6c98b38 19796
b34976b6 19797static bfd_boolean
17a2f251 19798mips_strict_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19799{
19800 while (*given != 0 && TOLOWER (*given) == TOLOWER (*canonical))
19801 given++, canonical++;
19802
19803 return ((*given == 0 && *canonical == 0)
19804 || (strcmp (canonical, "000") == 0 && strcasecmp (given, "k") == 0));
19805}
19806
19807
19808/* Return true if GIVEN matches CANONICAL, where GIVEN is a user-supplied
19809 CPU name. We've traditionally allowed a lot of variation here.
19810
19811 Note: this function is shared between GCC and GAS. */
19812
b34976b6 19813static bfd_boolean
17a2f251 19814mips_matching_cpu_name_p (const char *canonical, const char *given)
316f5878
RS
19815{
19816 /* First see if the name matches exactly, or with a final "000"
19817 turned into "k". */
19818 if (mips_strict_matching_cpu_name_p (canonical, given))
b34976b6 19819 return TRUE;
316f5878
RS
19820
19821 /* If not, try comparing based on numerical designation alone.
19822 See if GIVEN is an unadorned number, or 'r' followed by a number. */
19823 if (TOLOWER (*given) == 'r')
19824 given++;
19825 if (!ISDIGIT (*given))
b34976b6 19826 return FALSE;
316f5878
RS
19827
19828 /* Skip over some well-known prefixes in the canonical name,
19829 hoping to find a number there too. */
19830 if (TOLOWER (canonical[0]) == 'v' && TOLOWER (canonical[1]) == 'r')
19831 canonical += 2;
19832 else if (TOLOWER (canonical[0]) == 'r' && TOLOWER (canonical[1]) == 'm')
19833 canonical += 2;
19834 else if (TOLOWER (canonical[0]) == 'r')
19835 canonical += 1;
19836
19837 return mips_strict_matching_cpu_name_p (canonical, given);
19838}
19839
19840
19841/* Parse an option that takes the name of a processor as its argument.
19842 OPTION is the name of the option and CPU_STRING is the argument.
19843 Return the corresponding processor enumeration if the CPU_STRING is
19844 recognized, otherwise report an error and return null.
19845
19846 A similar function exists in GCC. */
e7af610e
NC
19847
19848static const struct mips_cpu_info *
17a2f251 19849mips_parse_cpu (const char *option, const char *cpu_string)
e7af610e 19850{
316f5878 19851 const struct mips_cpu_info *p;
e7af610e 19852
316f5878
RS
19853 /* 'from-abi' selects the most compatible architecture for the given
19854 ABI: MIPS I for 32-bit ABIs and MIPS III for 64-bit ABIs. For the
19855 EABIs, we have to decide whether we're using the 32-bit or 64-bit
19856 version. Look first at the -mgp options, if given, otherwise base
19857 the choice on MIPS_DEFAULT_64BIT.
e7af610e 19858
316f5878
RS
19859 Treat NO_ABI like the EABIs. One reason to do this is that the
19860 plain 'mips' and 'mips64' configs have 'from-abi' as their default
19861 architecture. This code picks MIPS I for 'mips' and MIPS III for
19862 'mips64', just as we did in the days before 'from-abi'. */
19863 if (strcasecmp (cpu_string, "from-abi") == 0)
19864 {
19865 if (ABI_NEEDS_32BIT_REGS (mips_abi))
19866 return mips_cpu_info_from_isa (ISA_MIPS1);
19867
19868 if (ABI_NEEDS_64BIT_REGS (mips_abi))
19869 return mips_cpu_info_from_isa (ISA_MIPS3);
19870
bad1aba3 19871 if (file_mips_opts.gp >= 0)
19872 return mips_cpu_info_from_isa (file_mips_opts.gp == 32
0b35dfee 19873 ? ISA_MIPS1 : ISA_MIPS3);
316f5878
RS
19874
19875 return mips_cpu_info_from_isa (MIPS_DEFAULT_64BIT
19876 ? ISA_MIPS3
19877 : ISA_MIPS1);
19878 }
19879
19880 /* 'default' has traditionally been a no-op. Probably not very useful. */
19881 if (strcasecmp (cpu_string, "default") == 0)
19882 return 0;
19883
19884 for (p = mips_cpu_info_table; p->name != 0; p++)
19885 if (mips_matching_cpu_name_p (p->name, cpu_string))
19886 return p;
19887
1661c76c 19888 as_bad (_("bad value (%s) for %s"), cpu_string, option);
316f5878 19889 return 0;
e7af610e
NC
19890}
19891
316f5878
RS
19892/* Return the canonical processor information for ISA (a member of the
19893 ISA_MIPS* enumeration). */
19894
e7af610e 19895static const struct mips_cpu_info *
17a2f251 19896mips_cpu_info_from_isa (int isa)
e7af610e
NC
19897{
19898 int i;
19899
19900 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
ad3fea08 19901 if ((mips_cpu_info_table[i].flags & MIPS_CPU_IS_ISA)
316f5878 19902 && isa == mips_cpu_info_table[i].isa)
e7af610e
NC
19903 return (&mips_cpu_info_table[i]);
19904
e972090a 19905 return NULL;
e7af610e 19906}
fef14a42
TS
19907
19908static const struct mips_cpu_info *
17a2f251 19909mips_cpu_info_from_arch (int arch)
fef14a42
TS
19910{
19911 int i;
19912
19913 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
19914 if (arch == mips_cpu_info_table[i].cpu)
19915 return (&mips_cpu_info_table[i]);
19916
19917 return NULL;
19918}
316f5878
RS
19919\f
19920static void
17a2f251 19921show (FILE *stream, const char *string, int *col_p, int *first_p)
316f5878
RS
19922{
19923 if (*first_p)
19924 {
19925 fprintf (stream, "%24s", "");
19926 *col_p = 24;
19927 }
19928 else
19929 {
19930 fprintf (stream, ", ");
19931 *col_p += 2;
19932 }
e7af610e 19933
316f5878
RS
19934 if (*col_p + strlen (string) > 72)
19935 {
19936 fprintf (stream, "\n%24s", "");
19937 *col_p = 24;
19938 }
19939
19940 fprintf (stream, "%s", string);
19941 *col_p += strlen (string);
19942
19943 *first_p = 0;
19944}
19945
19946void
17a2f251 19947md_show_usage (FILE *stream)
e7af610e 19948{
316f5878
RS
19949 int column, first;
19950 size_t i;
19951
19952 fprintf (stream, _("\
19953MIPS options:\n\
316f5878
RS
19954-EB generate big endian output\n\
19955-EL generate little endian output\n\
19956-g, -g2 do not remove unneeded NOPs or swap branches\n\
19957-G NUM allow referencing objects up to NUM bytes\n\
19958 implicitly with the gp register [default 8]\n"));
19959 fprintf (stream, _("\
19960-mips1 generate MIPS ISA I instructions\n\
19961-mips2 generate MIPS ISA II instructions\n\
19962-mips3 generate MIPS ISA III instructions\n\
19963-mips4 generate MIPS ISA IV instructions\n\
19964-mips5 generate MIPS ISA V instructions\n\
19965-mips32 generate MIPS32 ISA instructions\n\
af7ee8bf 19966-mips32r2 generate MIPS32 release 2 ISA instructions\n\
ae52f483
AB
19967-mips32r3 generate MIPS32 release 3 ISA instructions\n\
19968-mips32r5 generate MIPS32 release 5 ISA instructions\n\
7361da2c 19969-mips32r6 generate MIPS32 release 6 ISA instructions\n\
316f5878 19970-mips64 generate MIPS64 ISA instructions\n\
5f74bc13 19971-mips64r2 generate MIPS64 release 2 ISA instructions\n\
ae52f483
AB
19972-mips64r3 generate MIPS64 release 3 ISA instructions\n\
19973-mips64r5 generate MIPS64 release 5 ISA instructions\n\
7361da2c 19974-mips64r6 generate MIPS64 release 6 ISA instructions\n\
316f5878
RS
19975-march=CPU/-mtune=CPU generate code/schedule for CPU, where CPU is one of:\n"));
19976
19977 first = 1;
e7af610e
NC
19978
19979 for (i = 0; mips_cpu_info_table[i].name != NULL; i++)
316f5878
RS
19980 show (stream, mips_cpu_info_table[i].name, &column, &first);
19981 show (stream, "from-abi", &column, &first);
19982 fputc ('\n', stream);
e7af610e 19983
316f5878
RS
19984 fprintf (stream, _("\
19985-mCPU equivalent to -march=CPU -mtune=CPU. Deprecated.\n\
19986-no-mCPU don't generate code specific to CPU.\n\
19987 For -mCPU and -no-mCPU, CPU must be one of:\n"));
19988
19989 first = 1;
19990
19991 show (stream, "3900", &column, &first);
19992 show (stream, "4010", &column, &first);
19993 show (stream, "4100", &column, &first);
19994 show (stream, "4650", &column, &first);
19995 fputc ('\n', stream);
19996
19997 fprintf (stream, _("\
19998-mips16 generate mips16 instructions\n\
19999-no-mips16 do not generate mips16 instructions\n"));
20000 fprintf (stream, _("\
f866b262
MR
20001-mmips16e2 generate MIPS16e2 instructions\n\
20002-mno-mips16e2 do not generate MIPS16e2 instructions\n"));
20003 fprintf (stream, _("\
df58fc94
RS
20004-mmicromips generate microMIPS instructions\n\
20005-mno-micromips do not generate microMIPS instructions\n"));
20006 fprintf (stream, _("\
e16bfa71 20007-msmartmips generate smartmips instructions\n\
3739860c 20008-mno-smartmips do not generate smartmips instructions\n"));
e16bfa71 20009 fprintf (stream, _("\
74cd071d
CF
20010-mdsp generate DSP instructions\n\
20011-mno-dsp do not generate DSP instructions\n"));
20012 fprintf (stream, _("\
8b082fb1
TS
20013-mdspr2 generate DSP R2 instructions\n\
20014-mno-dspr2 do not generate DSP R2 instructions\n"));
20015 fprintf (stream, _("\
8f4f9071
MF
20016-mdspr3 generate DSP R3 instructions\n\
20017-mno-dspr3 do not generate DSP R3 instructions\n"));
20018 fprintf (stream, _("\
ef2e4d86
CF
20019-mmt generate MT instructions\n\
20020-mno-mt do not generate MT instructions\n"));
20021 fprintf (stream, _("\
dec0624d
MR
20022-mmcu generate MCU instructions\n\
20023-mno-mcu do not generate MCU instructions\n"));
20024 fprintf (stream, _("\
56d438b1
CF
20025-mmsa generate MSA instructions\n\
20026-mno-msa do not generate MSA instructions\n"));
20027 fprintf (stream, _("\
7d64c587
AB
20028-mxpa generate eXtended Physical Address (XPA) instructions\n\
20029-mno-xpa do not generate eXtended Physical Address (XPA) instructions\n"));
20030 fprintf (stream, _("\
b015e599
AP
20031-mvirt generate Virtualization instructions\n\
20032-mno-virt do not generate Virtualization instructions\n"));
20033 fprintf (stream, _("\
730c3174
SE
20034-mcrc generate CRC instructions\n\
20035-mno-crc do not generate CRC instructions\n"));
20036 fprintf (stream, _("\
6f20c942
FS
20037-mginv generate Global INValidate (GINV) instructions\n\
20038-mno-ginv do not generate Global INValidate instructions\n"));
20039 fprintf (stream, _("\
833794fc
MR
20040-minsn32 only generate 32-bit microMIPS instructions\n\
20041-mno-insn32 generate all microMIPS instructions\n"));
20042 fprintf (stream, _("\
c67a084a
NC
20043-mfix-loongson2f-jump work around Loongson2F JUMP instructions\n\
20044-mfix-loongson2f-nop work around Loongson2F NOP errata\n\
d766e8ec 20045-mfix-vr4120 work around certain VR4120 errata\n\
7d8e00cf 20046-mfix-vr4130 work around VR4130 mflo/mfhi errata\n\
6a32d874 20047-mfix-24k insert a nop after ERET and DERET instructions\n\
d954098f 20048-mfix-cn63xxp1 work around CN63XXP1 PREF errata\n\
316f5878
RS
20049-mgp32 use 32-bit GPRs, regardless of the chosen ISA\n\
20050-mfp32 use 32-bit FPRs, regardless of the chosen ISA\n\
aed1a261 20051-msym32 assume all symbols have 32-bit values\n\
092a534f
MR
20052-O0 do not remove unneeded NOPs, do not swap branches\n\
20053-O, -O1 remove unneeded NOPs, do not swap branches\n\
20054-O2 remove unneeded NOPs and swap branches\n\
316f5878
RS
20055--trap, --no-break trap exception on div by 0 and mult overflow\n\
20056--break, --no-trap break exception on div by 0 and mult overflow\n"));
037b32b9
AN
20057 fprintf (stream, _("\
20058-mhard-float allow floating-point instructions\n\
20059-msoft-float do not allow floating-point instructions\n\
20060-msingle-float only allow 32-bit floating-point operations\n\
20061-mdouble-float allow 32-bit and 64-bit floating-point operations\n\
3bf0dbfb 20062--[no-]construct-floats [dis]allow floating point values to be constructed\n\
ba92f887 20063--[no-]relax-branch [dis]allow out-of-range branches to be relaxed\n\
8b10b0b3
MR
20064-mignore-branch-isa accept invalid branches requiring an ISA mode switch\n\
20065-mno-ignore-branch-isa reject invalid branches requiring an ISA mode switch\n\
ba92f887
MR
20066-mnan=ENCODING select an IEEE 754 NaN encoding convention, either of:\n"));
20067
20068 first = 1;
20069
20070 show (stream, "legacy", &column, &first);
20071 show (stream, "2008", &column, &first);
20072
20073 fputc ('\n', stream);
20074
316f5878
RS
20075 fprintf (stream, _("\
20076-KPIC, -call_shared generate SVR4 position independent code\n\
861fb55a 20077-call_nonpic generate non-PIC code that can operate with DSOs\n\
0c000745 20078-mvxworks-pic generate VxWorks position independent code\n\
861fb55a 20079-non_shared do not generate code that can operate with DSOs\n\
316f5878 20080-xgot assume a 32 bit GOT\n\
dcd410fe 20081-mpdr, -mno-pdr enable/disable creation of .pdr sections\n\
bbe506e8 20082-mshared, -mno-shared disable/enable .cpload optimization for\n\
d821e36b 20083 position dependent (non shared) code\n\
316f5878
RS
20084-mabi=ABI create ABI conformant object file for:\n"));
20085
20086 first = 1;
20087
20088 show (stream, "32", &column, &first);
20089 show (stream, "o64", &column, &first);
20090 show (stream, "n32", &column, &first);
20091 show (stream, "64", &column, &first);
20092 show (stream, "eabi", &column, &first);
20093
20094 fputc ('\n', stream);
20095
20096 fprintf (stream, _("\
b4f6242e
MR
20097-32 create o32 ABI object file%s\n"),
20098 MIPS_DEFAULT_ABI == O32_ABI ? _(" (default)") : "");
20099 fprintf (stream, _("\
20100-n32 create n32 ABI object file%s\n"),
20101 MIPS_DEFAULT_ABI == N32_ABI ? _(" (default)") : "");
20102 fprintf (stream, _("\
20103-64 create 64 ABI object file%s\n"),
20104 MIPS_DEFAULT_ABI == N64_ABI ? _(" (default)") : "");
e7af610e 20105}
14e777e0 20106
1575952e 20107#ifdef TE_IRIX
14e777e0 20108enum dwarf2_format
413a266c 20109mips_dwarf2_format (asection *sec ATTRIBUTE_UNUSED)
14e777e0 20110{
369943fe 20111 if (HAVE_64BIT_SYMBOLS)
1575952e 20112 return dwarf2_format_64bit_irix;
14e777e0
KB
20113 else
20114 return dwarf2_format_32bit;
20115}
1575952e 20116#endif
73369e65
EC
20117
20118int
20119mips_dwarf2_addr_size (void)
20120{
6b6b3450 20121 if (HAVE_64BIT_OBJECTS)
73369e65 20122 return 8;
73369e65
EC
20123 else
20124 return 4;
20125}
5862107c
EC
20126
20127/* Standard calling conventions leave the CFA at SP on entry. */
20128void
20129mips_cfi_frame_initial_instructions (void)
20130{
20131 cfi_add_CFA_def_cfa_register (SP);
20132}
20133
707bfff6
TS
20134int
20135tc_mips_regname_to_dw2regnum (char *regname)
20136{
20137 unsigned int regnum = -1;
20138 unsigned int reg;
20139
20140 if (reg_lookup (&regname, RTYPE_GP | RTYPE_NUM, &reg))
20141 regnum = reg;
20142
20143 return regnum;
20144}
263b2574 20145
20146/* Implement CONVERT_SYMBOLIC_ATTRIBUTE.
20147 Given a symbolic attribute NAME, return the proper integer value.
20148 Returns -1 if the attribute is not known. */
20149
20150int
20151mips_convert_symbolic_attribute (const char *name)
20152{
20153 static const struct
20154 {
20155 const char * name;
20156 const int tag;
20157 }
20158 attribute_table[] =
20159 {
20160#define T(tag) {#tag, tag}
20161 T (Tag_GNU_MIPS_ABI_FP),
20162 T (Tag_GNU_MIPS_ABI_MSA),
20163#undef T
20164 };
20165 unsigned int i;
20166
20167 if (name == NULL)
20168 return -1;
20169
20170 for (i = 0; i < ARRAY_SIZE (attribute_table); i++)
20171 if (streq (name, attribute_table[i].name))
20172 return attribute_table[i].tag;
20173
20174 return -1;
20175}
fd5c94ab
RS
20176
20177void
20178md_mips_end (void)
20179{
351cdf24
MF
20180 int fpabi = Val_GNU_MIPS_ABI_FP_ANY;
20181
fd5c94ab
RS
20182 mips_emit_delays ();
20183 if (cur_proc_ptr)
20184 as_warn (_("missing .end at end of assembly"));
919731af 20185
20186 /* Just in case no code was emitted, do the consistency check. */
20187 file_mips_check_options ();
351cdf24
MF
20188
20189 /* Set a floating-point ABI if the user did not. */
20190 if (obj_elf_seen_attribute (OBJ_ATTR_GNU, Tag_GNU_MIPS_ABI_FP))
20191 {
20192 /* Perform consistency checks on the floating-point ABI. */
20193 fpabi = bfd_elf_get_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20194 Tag_GNU_MIPS_ABI_FP);
20195 if (fpabi != Val_GNU_MIPS_ABI_FP_ANY)
20196 check_fpabi (fpabi);
20197 }
20198 else
20199 {
20200 /* Soft-float gets precedence over single-float, the two options should
20201 not be used together so this should not matter. */
20202 if (file_mips_opts.soft_float == 1)
20203 fpabi = Val_GNU_MIPS_ABI_FP_SOFT;
20204 /* Single-float gets precedence over all double_float cases. */
20205 else if (file_mips_opts.single_float == 1)
20206 fpabi = Val_GNU_MIPS_ABI_FP_SINGLE;
20207 else
20208 {
20209 switch (file_mips_opts.fp)
20210 {
20211 case 32:
20212 if (file_mips_opts.gp == 32)
20213 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20214 break;
20215 case 0:
20216 fpabi = Val_GNU_MIPS_ABI_FP_XX;
20217 break;
20218 case 64:
20219 if (file_mips_opts.gp == 32 && !file_mips_opts.oddspreg)
20220 fpabi = Val_GNU_MIPS_ABI_FP_64A;
20221 else if (file_mips_opts.gp == 32)
20222 fpabi = Val_GNU_MIPS_ABI_FP_64;
20223 else
20224 fpabi = Val_GNU_MIPS_ABI_FP_DOUBLE;
20225 break;
20226 }
20227 }
20228
20229 bfd_elf_add_obj_attr_int (stdoutput, OBJ_ATTR_GNU,
20230 Tag_GNU_MIPS_ABI_FP, fpabi);
20231 }
fd5c94ab 20232}
2f0c68f2
CM
20233
20234/* Returns the relocation type required for a particular CFI encoding. */
20235
20236bfd_reloc_code_real_type
20237mips_cfi_reloc_for_encoding (int encoding)
20238{
20239 if (encoding == (DW_EH_PE_sdata4 | DW_EH_PE_pcrel))
20240 return BFD_RELOC_32_PCREL;
20241 else return BFD_RELOC_NONE;
20242}
This page took 3.426946 seconds and 4 git commands to generate.